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" 37bba7a2e8SRick Macklem #include "opt_nfs.h" 3846e3b1cbSPawel Jakub Dawidek 3975c13541SPoul-Henning Kamp #include <sys/param.h> 4075c13541SPoul-Henning Kamp #include <sys/types.h> 4175c13541SPoul-Henning Kamp #include <sys/kernel.h> 4275c13541SPoul-Henning Kamp #include <sys/systm.h> 4375c13541SPoul-Henning Kamp #include <sys/errno.h> 4475c13541SPoul-Henning Kamp #include <sys/sysproto.h> 4575c13541SPoul-Henning Kamp #include <sys/malloc.h> 460304c731SJamie Gritton #include <sys/osd.h> 47800c9408SRobert Watson #include <sys/priv.h> 4875c13541SPoul-Henning Kamp #include <sys/proc.h> 49eb8dcdeaSGleb Smirnoff #include <sys/epoch.h> 50b3059e09SRobert Watson #include <sys/taskqueue.h> 5157b4252eSKonstantin Belousov #include <sys/fcntl.h> 5275c13541SPoul-Henning Kamp #include <sys/jail.h> 53c3188289SKyle Evans #include <sys/linker.h> 5401137630SRobert Watson #include <sys/lock.h> 557060da62SJamie Gritton #include <sys/mman.h> 5601137630SRobert Watson #include <sys/mutex.h> 57097055e2SEdward Tomasz Napierala #include <sys/racct.h> 58f87beb93SAndriy Gapon #include <sys/rctl.h> 59a7ad07bfSEdward Tomasz Napierala #include <sys/refcount.h> 60dc68a633SPawel Jakub Dawidek #include <sys/sx.h> 6176ca6f88SJamie Gritton #include <sys/sysent.h> 62fd7a8150SMike Barcroft #include <sys/namei.h> 63820a0de9SPawel Jakub Dawidek #include <sys/mount.h> 64fd7a8150SMike Barcroft #include <sys/queue.h> 6575c13541SPoul-Henning Kamp #include <sys/socket.h> 66fd7a8150SMike Barcroft #include <sys/syscallsubr.h> 6783f1e257SRobert Watson #include <sys/sysctl.h> 68c3188289SKyle Evans #include <sys/uuid.h> 69fd7a8150SMike Barcroft #include <sys/vnode.h> 70530c0060SRobert Watson 7175c13541SPoul-Henning Kamp #include <net/if.h> 72530c0060SRobert Watson #include <net/vnet.h> 73530c0060SRobert Watson 7475c13541SPoul-Henning Kamp #include <netinet/in.h> 75530c0060SRobert Watson 76413628a7SBjoern A. Zeeb #ifdef DDB 77413628a7SBjoern A. Zeeb #include <ddb/ddb.h> 78413628a7SBjoern A. Zeeb #endif /* DDB */ 7975c13541SPoul-Henning Kamp 80aed55708SRobert Watson #include <security/mac/mac_framework.h> 81aed55708SRobert Watson 82c3188289SKyle Evans #define PRISON0_HOSTUUID_MODULE "hostuuid" 838986e3a0SJamie Gritton 8475c13541SPoul-Henning Kamp MALLOC_DEFINE(M_PRISON, "prison", "Prison structures"); 85d745c852SEd Schouten static MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures"); 8675c13541SPoul-Henning Kamp 87592bcae8SBjoern A. Zeeb /* Keep struct prison prison0 and some code in kern_jail_set() readable. */ 88592bcae8SBjoern A. Zeeb #ifdef INET 89592bcae8SBjoern A. Zeeb #ifdef INET6 90592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL|PR_IP6_SADDRSEL 91592bcae8SBjoern A. Zeeb #else 92592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL 93592bcae8SBjoern A. Zeeb #endif 94592bcae8SBjoern A. Zeeb #else /* !INET */ 95592bcae8SBjoern A. Zeeb #ifdef INET6 96592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL PR_IP6_SADDRSEL 97592bcae8SBjoern A. Zeeb #else 98592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL 0 99592bcae8SBjoern A. Zeeb #endif 100592bcae8SBjoern A. Zeeb #endif 101592bcae8SBjoern A. Zeeb 1020304c731SJamie Gritton /* prison0 describes what is "real" about the system. */ 1030304c731SJamie Gritton struct prison prison0 = { 1040304c731SJamie Gritton .pr_id = 0, 1050304c731SJamie Gritton .pr_name = "0", 1060304c731SJamie Gritton .pr_ref = 1, 1070304c731SJamie Gritton .pr_uref = 1, 1080304c731SJamie Gritton .pr_path = "/", 1090304c731SJamie Gritton .pr_securelevel = -1, 1100cc207a6SMartin Matuska .pr_devfs_rsnum = 0, 1111158508aSJamie Gritton .pr_state = PRISON_STATE_ALIVE, 112b97457e2SJamie Gritton .pr_childmax = JAIL_MAX, 1138986e3a0SJamie Gritton .pr_hostuuid = DEFAULT_HOSTUUID, 11413e403fdSAntoine Brodin .pr_children = LIST_HEAD_INITIALIZER(prison0.pr_children), 115eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE 116592bcae8SBjoern A. Zeeb .pr_flags = PR_HOST|PR_VNET|_PR_IP_SADDRSEL, 117eb79e1c7SBjoern A. Zeeb #else 118592bcae8SBjoern A. Zeeb .pr_flags = PR_HOST|_PR_IP_SADDRSEL, 119eb79e1c7SBjoern A. Zeeb #endif 1200e5c6bd4SJamie Gritton .pr_allow = PR_ALLOW_ALL_STATIC, 1210304c731SJamie Gritton }; 1220304c731SJamie Gritton MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF); 12383f1e257SRobert Watson 124672756aaSJamie Gritton struct bool_flags { 125672756aaSJamie Gritton const char *name; 126672756aaSJamie Gritton const char *noname; 1275d58f959SJamie Gritton volatile u_int flag; 128672756aaSJamie Gritton }; 129672756aaSJamie Gritton struct jailsys_flags { 130672756aaSJamie Gritton const char *name; 131672756aaSJamie Gritton unsigned disable; 132672756aaSJamie Gritton unsigned new; 133672756aaSJamie Gritton }; 134672756aaSJamie Gritton 135a7ad07bfSEdward Tomasz Napierala /* allprison, allprison_racct and lastprid are protected by allprison_lock. */ 136dc68a633SPawel Jakub Dawidek struct sx allprison_lock; 137b38ff370SJamie Gritton SX_SYSINIT(allprison_lock, &allprison_lock, "allprison"); 138b38ff370SJamie Gritton struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison); 139a7ad07bfSEdward Tomasz Napierala LIST_HEAD(, prison_racct) allprison_racct; 1402110d913SXin LI int lastprid = 0; 141fd7a8150SMike Barcroft 1427de883c8SJamie Gritton static int get_next_prid(struct prison **insprp); 143f7496dcaSJamie Gritton static int do_jail_attach(struct thread *td, struct prison *pr, int drflags); 144b3059e09SRobert Watson static void prison_complete(void *context, int pending); 145b38ff370SJamie Gritton static void prison_deref(struct prison *pr, int flags); 146c861373bSJamie Gritton static void prison_deref_kill(struct prison *pr, struct prisonlist *freeprison); 147f7496dcaSJamie Gritton static int prison_lock_xlock(struct prison *pr, int flags); 148a9f7455cSJamie Gritton static void prison_cleanup(struct prison *pr); 149f7496dcaSJamie Gritton static void prison_free_not_last(struct prison *pr); 150c861373bSJamie Gritton static void prison_proc_free_not_last(struct prison *pr); 1515ecb5444SMateusz Guzik static void prison_proc_relink(struct prison *opr, struct prison *npr, 1525ecb5444SMateusz Guzik struct proc *p); 1530fe74ae6SJamie Gritton static void prison_set_allow_locked(struct prison *pr, unsigned flag, 1540fe74ae6SJamie Gritton int enable); 1550304c731SJamie Gritton static char *prison_path(struct prison *pr1, struct prison *pr2); 156a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 157a7ad07bfSEdward Tomasz Napierala static void prison_racct_attach(struct prison *pr); 158c34bbd2aSEdward Tomasz Napierala static void prison_racct_modify(struct prison *pr); 159a7ad07bfSEdward Tomasz Napierala static void prison_racct_detach(struct prison *pr); 160a7ad07bfSEdward Tomasz Napierala #endif 161fd7a8150SMike Barcroft 162b38ff370SJamie Gritton /* Flags for prison_deref */ 1632a4b2251SJamie Gritton #define PD_DEREF 0x01 /* Decrement pr_ref */ 1642a4b2251SJamie Gritton #define PD_DEUREF 0x02 /* Decrement pr_uref */ 165c861373bSJamie Gritton #define PD_KILL 0x04 /* Remove jail, kill processes, etc */ 166c861373bSJamie Gritton #define PD_LOCKED 0x10 /* pr_mtx is held */ 167c861373bSJamie Gritton #define PD_LIST_SLOCKED 0x20 /* allprison_lock is held shared */ 168c861373bSJamie Gritton #define PD_LIST_XLOCKED 0x40 /* allprison_lock is held exclusive */ 169589e4c1dSJamie Gritton #define PD_OP_FLAGS 0x07 /* Operation flags */ 170589e4c1dSJamie Gritton #define PD_LOCK_FLAGS 0x70 /* Lock status flags */ 171fd7a8150SMike Barcroft 1720304c731SJamie Gritton /* 1735cc70397SBjoern A. Zeeb * Parameter names corresponding to PR_* flag values. Size values are for kvm 1745cc70397SBjoern A. Zeeb * as we cannot figure out the size of a sparse array, or an array without a 1755cc70397SBjoern A. Zeeb * terminating entry. 1760304c731SJamie Gritton */ 177672756aaSJamie Gritton static struct bool_flags pr_flag_bool[] = { 178672756aaSJamie Gritton {"persist", "nopersist", PR_PERSIST}, 179592bcae8SBjoern A. Zeeb #ifdef INET 180672756aaSJamie Gritton {"ip4.saddrsel", "ip4.nosaddrsel", PR_IP4_SADDRSEL}, 181592bcae8SBjoern A. Zeeb #endif 182592bcae8SBjoern A. Zeeb #ifdef INET6 183672756aaSJamie Gritton {"ip6.saddrsel", "ip6.nosaddrsel", PR_IP6_SADDRSEL}, 184592bcae8SBjoern A. Zeeb #endif 1850304c731SJamie Gritton }; 186672756aaSJamie Gritton const size_t pr_flag_bool_size = sizeof(pr_flag_bool); 1870304c731SJamie Gritton 188672756aaSJamie Gritton static struct jailsys_flags pr_flag_jailsys[] = { 1897cbf7213SJamie Gritton {"host", 0, PR_HOST}, 1907cbf7213SJamie Gritton #ifdef VIMAGE 1917cbf7213SJamie Gritton {"vnet", 0, PR_VNET}, 1927cbf7213SJamie Gritton #endif 1930304c731SJamie Gritton #ifdef INET 1946a3f2779SJamie Gritton {"ip4", PR_IP4_USER, PR_IP4_USER}, 1950304c731SJamie Gritton #endif 1960304c731SJamie Gritton #ifdef INET6 1976a3f2779SJamie Gritton {"ip6", PR_IP6_USER, PR_IP6_USER}, 198679e1390SJamie Gritton #endif 1990304c731SJamie Gritton }; 2005cc70397SBjoern A. Zeeb const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys); 2010304c731SJamie Gritton 2025d58f959SJamie Gritton /* 2035d58f959SJamie Gritton * Make this array full-size so dynamic parameters can be added. 2045d58f959SJamie Gritton * It is protected by prison0.mtx, but lockless reading is allowed 2055d58f959SJamie Gritton * with an atomic check of the flag values. 2065d58f959SJamie Gritton */ 2070e5c6bd4SJamie Gritton static struct bool_flags pr_flag_allow[NBBY * NBPW] = { 208672756aaSJamie Gritton {"allow.set_hostname", "allow.noset_hostname", PR_ALLOW_SET_HOSTNAME}, 209672756aaSJamie Gritton {"allow.sysvipc", "allow.nosysvipc", PR_ALLOW_SYSVIPC}, 210672756aaSJamie Gritton {"allow.raw_sockets", "allow.noraw_sockets", PR_ALLOW_RAW_SOCKETS}, 211672756aaSJamie Gritton {"allow.chflags", "allow.nochflags", PR_ALLOW_CHFLAGS}, 212672756aaSJamie Gritton {"allow.mount", "allow.nomount", PR_ALLOW_MOUNT}, 213672756aaSJamie Gritton {"allow.quotas", "allow.noquotas", PR_ALLOW_QUOTAS}, 214672756aaSJamie Gritton {"allow.socket_af", "allow.nosocket_af", PR_ALLOW_SOCKET_AF}, 215ccd6ac9fSAntoine Brodin {"allow.mlock", "allow.nomlock", PR_ALLOW_MLOCK}, 216672756aaSJamie Gritton {"allow.reserved_ports", "allow.noreserved_ports", 217672756aaSJamie Gritton PR_ALLOW_RESERVED_PORTS}, 218b19d66fdSJamie Gritton {"allow.read_msgbuf", "allow.noread_msgbuf", PR_ALLOW_READ_MSGBUF}, 219b3079544SJamie Gritton {"allow.unprivileged_proc_debug", "allow.nounprivileged_proc_debug", 220b3079544SJamie Gritton PR_ALLOW_UNPRIV_DEBUG}, 22105e1e482SMariusz Zaborski {"allow.suser", "allow.nosuser", PR_ALLOW_SUSER}, 222bba7a2e8SRick Macklem #if defined(VNET_NFSD) && defined(VIMAGE) && defined(NFSD) 223bba7a2e8SRick Macklem {"allow.nfsd", "allow.nonfsd", PR_ALLOW_NFSD}, 224bba7a2e8SRick Macklem #endif 2250304c731SJamie Gritton }; 2265d58f959SJamie Gritton static unsigned pr_allow_all = PR_ALLOW_ALL_STATIC; 227672756aaSJamie Gritton const size_t pr_flag_allow_size = sizeof(pr_flag_allow); 2280304c731SJamie Gritton 229b3079544SJamie Gritton #define JAIL_DEFAULT_ALLOW (PR_ALLOW_SET_HOSTNAME | \ 230b3079544SJamie Gritton PR_ALLOW_RESERVED_PORTS | \ 23105e1e482SMariusz Zaborski PR_ALLOW_UNPRIV_DEBUG | \ 23205e1e482SMariusz Zaborski PR_ALLOW_SUSER) 23342bebd82SJamie Gritton #define JAIL_DEFAULT_ENFORCE_STATFS 2 234bf3db8aaSMartin Matuska #define JAIL_DEFAULT_DEVFS_RSNUM 0 2350304c731SJamie Gritton static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW; 23642bebd82SJamie Gritton static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 2370cc207a6SMartin Matuska static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM; 2380304c731SJamie Gritton #if defined(INET) || defined(INET6) 239e92e0574SJamie Gritton static unsigned jail_max_af_ips = 255; 2400304c731SJamie Gritton #endif 2410304c731SJamie Gritton 242b96bd95bSIan Lepore /* 243b96bd95bSIan Lepore * Initialize the parts of prison0 that can't be static-initialized with 244b96bd95bSIan Lepore * constants. This is called from proc0_init() after creating thread0 cpuset. 245b96bd95bSIan Lepore */ 246b96bd95bSIan Lepore void 247b96bd95bSIan Lepore prison0_init(void) 248b96bd95bSIan Lepore { 249c3188289SKyle Evans uint8_t *file, *data; 250c3188289SKyle Evans size_t size; 251d2ef3774SJessica Clarke char buf[sizeof(prison0.pr_hostuuid)]; 252d2ef3774SJessica Clarke bool valid; 253b96bd95bSIan Lepore 254b96bd95bSIan Lepore prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset); 255b96bd95bSIan Lepore prison0.pr_osreldate = osreldate; 256b96bd95bSIan Lepore strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease)); 257c3188289SKyle Evans 258c3188289SKyle Evans /* If we have a preloaded hostuuid, use it. */ 259c3188289SKyle Evans file = preload_search_by_type(PRISON0_HOSTUUID_MODULE); 260c3188289SKyle Evans if (file != NULL) { 261c3188289SKyle Evans data = preload_fetch_addr(file); 262c3188289SKyle Evans size = preload_fetch_size(file); 263c3188289SKyle Evans if (data != NULL) { 264c3188289SKyle Evans /* 265c3188289SKyle Evans * The preloaded data may include trailing whitespace, almost 266c3188289SKyle Evans * certainly a newline; skip over any whitespace or 267c3188289SKyle Evans * non-printable characters to be safe. 268c3188289SKyle Evans */ 269c3188289SKyle Evans while (size > 0 && data[size - 1] <= 0x20) { 270b6be9566SColin Percival size--; 271c3188289SKyle Evans } 272d2ef3774SJessica Clarke 273d2ef3774SJessica Clarke valid = false; 274d2ef3774SJessica Clarke 275d2ef3774SJessica Clarke /* 276d2ef3774SJessica Clarke * Not NUL-terminated when passed from loader, but 277d2ef3774SJessica Clarke * validate_uuid requires that due to using sscanf (as 278d2ef3774SJessica Clarke * does the subsequent strlcpy, since it still reads 279d2ef3774SJessica Clarke * past the given size to return the true length); 280d2ef3774SJessica Clarke * bounce to a temporary buffer to fix. 281d2ef3774SJessica Clarke */ 282d2ef3774SJessica Clarke if (size >= sizeof(buf)) 283d2ef3774SJessica Clarke goto done; 284d2ef3774SJessica Clarke 285d2ef3774SJessica Clarke memcpy(buf, data, size); 286d2ef3774SJessica Clarke buf[size] = '\0'; 287d2ef3774SJessica Clarke 288d2ef3774SJessica Clarke if (validate_uuid(buf, size, NULL, 0) != 0) 289d2ef3774SJessica Clarke goto done; 290d2ef3774SJessica Clarke 291d2ef3774SJessica Clarke valid = true; 292d2ef3774SJessica Clarke (void)strlcpy(prison0.pr_hostuuid, buf, 293d2ef3774SJessica Clarke sizeof(prison0.pr_hostuuid)); 294d2ef3774SJessica Clarke 295d2ef3774SJessica Clarke done: 296d2ef3774SJessica Clarke if (bootverbose && !valid) { 297330f110bSColin Percival printf("hostuuid: preload data malformed: '%.*s'\n", 298330f110bSColin Percival (int)size, data); 299c3188289SKyle Evans } 300c3188289SKyle Evans } 301c3188289SKyle Evans } 302c3188289SKyle Evans if (bootverbose) 303c3188289SKyle Evans printf("hostuuid: using %s\n", prison0.pr_hostuuid); 304b96bd95bSIan Lepore } 305b96bd95bSIan Lepore 306116734c4SMatthew Dillon /* 3079ddb7954SMike Barcroft * struct jail_args { 3089ddb7954SMike Barcroft * struct jail *jail; 3099ddb7954SMike Barcroft * }; 310116734c4SMatthew Dillon */ 31175c13541SPoul-Henning Kamp int 312c542c43eSJamie Gritton sys_jail(struct thread *td, struct jail_args *uap) 31375c13541SPoul-Henning Kamp { 314413628a7SBjoern A. Zeeb uint32_t version; 315413628a7SBjoern A. Zeeb int error; 3160304c731SJamie Gritton struct jail j; 317413628a7SBjoern A. Zeeb 318413628a7SBjoern A. Zeeb error = copyin(uap->jail, &version, sizeof(uint32_t)); 319413628a7SBjoern A. Zeeb if (error) 320413628a7SBjoern A. Zeeb return (error); 321413628a7SBjoern A. Zeeb 322413628a7SBjoern A. Zeeb switch (version) { 323413628a7SBjoern A. Zeeb case 0: 324413628a7SBjoern A. Zeeb { 325413628a7SBjoern A. Zeeb struct jail_v0 j0; 326413628a7SBjoern A. Zeeb 3270304c731SJamie Gritton /* FreeBSD single IPv4 jails. */ 3280304c731SJamie Gritton bzero(&j, sizeof(struct jail)); 329413628a7SBjoern A. Zeeb error = copyin(uap->jail, &j0, sizeof(struct jail_v0)); 330413628a7SBjoern A. Zeeb if (error) 331413628a7SBjoern A. Zeeb return (error); 3320304c731SJamie Gritton j.version = j0.version; 3330304c731SJamie Gritton j.path = j0.path; 3340304c731SJamie Gritton j.hostname = j0.hostname; 335b5019bc4SPeter Wemm j.ip4s = htonl(j0.ip_number); /* jail_v0 is host order */ 336413628a7SBjoern A. Zeeb break; 337413628a7SBjoern A. Zeeb } 338413628a7SBjoern A. Zeeb 339413628a7SBjoern A. Zeeb case 1: 340413628a7SBjoern A. Zeeb /* 341413628a7SBjoern A. Zeeb * Version 1 was used by multi-IPv4 jail implementations 342413628a7SBjoern A. Zeeb * that never made it into the official kernel. 343413628a7SBjoern A. Zeeb */ 344413628a7SBjoern A. Zeeb return (EINVAL); 345413628a7SBjoern A. Zeeb 346413628a7SBjoern A. Zeeb case 2: /* JAIL_API_VERSION */ 347413628a7SBjoern A. Zeeb /* FreeBSD multi-IPv4/IPv6,noIP jails. */ 348413628a7SBjoern A. Zeeb error = copyin(uap->jail, &j, sizeof(struct jail)); 349413628a7SBjoern A. Zeeb if (error) 350413628a7SBjoern A. Zeeb return (error); 3510304c731SJamie Gritton break; 3520304c731SJamie Gritton 3530304c731SJamie Gritton default: 3540304c731SJamie Gritton /* Sci-Fi jails are not supported, sorry. */ 3550304c731SJamie Gritton return (EINVAL); 3560304c731SJamie Gritton } 357c542c43eSJamie Gritton return (kern_jail(td, &j)); 3580304c731SJamie Gritton } 3590304c731SJamie Gritton 3600304c731SJamie Gritton int 361c542c43eSJamie Gritton kern_jail(struct thread *td, struct jail *j) 3620304c731SJamie Gritton { 363c542c43eSJamie Gritton struct iovec optiov[2 * (4 + nitems(pr_flag_allow) 364e92e0574SJamie Gritton #ifdef INET 365e92e0574SJamie Gritton + 1 366e92e0574SJamie Gritton #endif 367e92e0574SJamie Gritton #ifdef INET6 368e92e0574SJamie Gritton + 1 369e92e0574SJamie Gritton #endif 370e92e0574SJamie Gritton )]; 3710304c731SJamie Gritton struct uio opt; 3720304c731SJamie Gritton char *u_path, *u_hostname, *u_name; 373672756aaSJamie Gritton struct bool_flags *bf; 3740304c731SJamie Gritton #ifdef INET 375e92e0574SJamie Gritton uint32_t ip4s; 3760304c731SJamie Gritton struct in_addr *u_ip4; 3770304c731SJamie Gritton #endif 3780304c731SJamie Gritton #ifdef INET6 3790304c731SJamie Gritton struct in6_addr *u_ip6; 3800304c731SJamie Gritton #endif 3810304c731SJamie Gritton size_t tmplen; 382c542c43eSJamie Gritton int error, enforce_statfs; 3830304c731SJamie Gritton 3840304c731SJamie Gritton bzero(&optiov, sizeof(optiov)); 3850304c731SJamie Gritton opt.uio_iov = optiov; 3860304c731SJamie Gritton opt.uio_iovcnt = 0; 3870304c731SJamie Gritton opt.uio_offset = -1; 3880304c731SJamie Gritton opt.uio_resid = -1; 3890304c731SJamie Gritton opt.uio_segflg = UIO_SYSSPACE; 3900304c731SJamie Gritton opt.uio_rw = UIO_READ; 3910304c731SJamie Gritton opt.uio_td = td; 3920304c731SJamie Gritton 3930304c731SJamie Gritton /* Set permissions for top-level jails from sysctls. */ 3940304c731SJamie Gritton if (!jailed(td->td_ucred)) { 395672756aaSJamie Gritton for (bf = pr_flag_allow; 3960e5c6bd4SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 3975d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 398672756aaSJamie Gritton bf++) { 399672756aaSJamie Gritton optiov[opt.uio_iovcnt].iov_base = __DECONST(char *, 400672756aaSJamie Gritton (jail_default_allow & bf->flag) 401672756aaSJamie Gritton ? bf->name : bf->noname); 4020304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = 4030304c731SJamie Gritton strlen(optiov[opt.uio_iovcnt].iov_base) + 1; 4040304c731SJamie Gritton opt.uio_iovcnt += 2; 4050304c731SJamie Gritton } 4060304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "enforce_statfs"; 4070304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs"); 4080304c731SJamie Gritton opt.uio_iovcnt++; 4090304c731SJamie Gritton enforce_statfs = jail_default_enforce_statfs; 4100304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = &enforce_statfs; 4110304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs); 4120304c731SJamie Gritton opt.uio_iovcnt++; 4130304c731SJamie Gritton } 4140304c731SJamie Gritton 415b38ff370SJamie Gritton tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN; 416b38ff370SJamie Gritton #ifdef INET 4170304c731SJamie Gritton ip4s = (j->version == 0) ? 1 : j->ip4s; 4180304c731SJamie Gritton if (ip4s > jail_max_af_ips) 419b38ff370SJamie Gritton return (EINVAL); 4200304c731SJamie Gritton tmplen += ip4s * sizeof(struct in_addr); 421b38ff370SJamie Gritton #else 4220304c731SJamie Gritton if (j->ip4s > 0) 423b38ff370SJamie Gritton return (EINVAL); 424b38ff370SJamie Gritton #endif 425b38ff370SJamie Gritton #ifdef INET6 4260304c731SJamie Gritton if (j->ip6s > jail_max_af_ips) 427b38ff370SJamie Gritton return (EINVAL); 4280304c731SJamie Gritton tmplen += j->ip6s * sizeof(struct in6_addr); 429b38ff370SJamie Gritton #else 4300304c731SJamie Gritton if (j->ip6s > 0) 431b38ff370SJamie Gritton return (EINVAL); 432b38ff370SJamie Gritton #endif 433b38ff370SJamie Gritton u_path = malloc(tmplen, M_TEMP, M_WAITOK); 434b38ff370SJamie Gritton u_hostname = u_path + MAXPATHLEN; 435b38ff370SJamie Gritton u_name = u_hostname + MAXHOSTNAMELEN; 436b38ff370SJamie Gritton #ifdef INET 437b38ff370SJamie Gritton u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN); 438b38ff370SJamie Gritton #endif 439b38ff370SJamie Gritton #ifdef INET6 440b38ff370SJamie Gritton #ifdef INET 4410304c731SJamie Gritton u_ip6 = (struct in6_addr *)(u_ip4 + ip4s); 442b38ff370SJamie Gritton #else 443b38ff370SJamie Gritton u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN); 444b38ff370SJamie Gritton #endif 445b38ff370SJamie Gritton #endif 4460304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "path"; 4470304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("path"); 4480304c731SJamie Gritton opt.uio_iovcnt++; 4490304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_path; 4500304c731SJamie Gritton error = copyinstr(j->path, u_path, MAXPATHLEN, 4510304c731SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 452b38ff370SJamie Gritton if (error) { 453b38ff370SJamie Gritton free(u_path, M_TEMP); 454b38ff370SJamie Gritton return (error); 455b38ff370SJamie Gritton } 4560304c731SJamie Gritton opt.uio_iovcnt++; 4570304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "host.hostname"; 4580304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname"); 4590304c731SJamie Gritton opt.uio_iovcnt++; 4600304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_hostname; 4610304c731SJamie Gritton error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN, 4620304c731SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 463b38ff370SJamie Gritton if (error) { 464b38ff370SJamie Gritton free(u_path, M_TEMP); 465b38ff370SJamie Gritton return (error); 466b38ff370SJamie Gritton } 4670304c731SJamie Gritton opt.uio_iovcnt++; 4680304c731SJamie Gritton if (j->jailname != NULL) { 469b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "name"; 470b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("name"); 471b38ff370SJamie Gritton opt.uio_iovcnt++; 472b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_name; 4730304c731SJamie Gritton error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN, 474b38ff370SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 475b38ff370SJamie Gritton if (error) { 476b38ff370SJamie Gritton free(u_path, M_TEMP); 477b38ff370SJamie Gritton return (error); 478b38ff370SJamie Gritton } 479b38ff370SJamie Gritton opt.uio_iovcnt++; 480b38ff370SJamie Gritton } 481b38ff370SJamie Gritton #ifdef INET 482b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "ip4.addr"; 483b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr"); 484b38ff370SJamie Gritton opt.uio_iovcnt++; 485b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_ip4; 4860304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr); 4870304c731SJamie Gritton if (j->version == 0) 4880304c731SJamie Gritton u_ip4->s_addr = j->ip4s; 4890304c731SJamie Gritton else { 4900304c731SJamie Gritton error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len); 491b38ff370SJamie Gritton if (error) { 492b38ff370SJamie Gritton free(u_path, M_TEMP); 493b38ff370SJamie Gritton return (error); 494b38ff370SJamie Gritton } 4950304c731SJamie Gritton } 496b38ff370SJamie Gritton opt.uio_iovcnt++; 497b38ff370SJamie Gritton #endif 498b38ff370SJamie Gritton #ifdef INET6 499b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "ip6.addr"; 500b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr"); 501b38ff370SJamie Gritton opt.uio_iovcnt++; 502b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_ip6; 5030304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr); 5040304c731SJamie Gritton error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len); 505b38ff370SJamie Gritton if (error) { 506b38ff370SJamie Gritton free(u_path, M_TEMP); 507b38ff370SJamie Gritton return (error); 508b38ff370SJamie Gritton } 509b38ff370SJamie Gritton opt.uio_iovcnt++; 510b38ff370SJamie Gritton #endif 51102abd400SPedro F. Giffuni KASSERT(opt.uio_iovcnt <= nitems(optiov), 5120304c731SJamie Gritton ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt)); 513b38ff370SJamie Gritton error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH); 514b38ff370SJamie Gritton free(u_path, M_TEMP); 515b38ff370SJamie Gritton return (error); 516b38ff370SJamie Gritton } 517b38ff370SJamie Gritton 518b38ff370SJamie Gritton /* 519b38ff370SJamie Gritton * struct jail_set_args { 520b38ff370SJamie Gritton * struct iovec *iovp; 521b38ff370SJamie Gritton * unsigned int iovcnt; 522b38ff370SJamie Gritton * int flags; 523b38ff370SJamie Gritton * }; 524b38ff370SJamie Gritton */ 525b38ff370SJamie Gritton int 5268451d0ddSKip Macy sys_jail_set(struct thread *td, struct jail_set_args *uap) 527b38ff370SJamie Gritton { 528b38ff370SJamie Gritton struct uio *auio; 529b38ff370SJamie Gritton int error; 530b38ff370SJamie Gritton 531b38ff370SJamie Gritton /* Check that we have an even number of iovecs. */ 532b38ff370SJamie Gritton if (uap->iovcnt & 1) 533b38ff370SJamie Gritton return (EINVAL); 534b38ff370SJamie Gritton 535b38ff370SJamie Gritton error = copyinuio(uap->iovp, uap->iovcnt, &auio); 536b38ff370SJamie Gritton if (error) 537b38ff370SJamie Gritton return (error); 538b38ff370SJamie Gritton error = kern_jail_set(td, auio, uap->flags); 539b38ff370SJamie Gritton free(auio, M_IOV); 540b38ff370SJamie Gritton return (error); 541413628a7SBjoern A. Zeeb } 542413628a7SBjoern A. Zeeb 543eb8dcdeaSGleb Smirnoff #if defined(INET) || defined(INET6) 544eb8dcdeaSGleb Smirnoff typedef int prison_addr_cmp_t(const void *, const void *); 545eb8dcdeaSGleb Smirnoff typedef bool prison_addr_valid_t(const void *); 546eb8dcdeaSGleb Smirnoff static const struct pr_family { 547eb8dcdeaSGleb Smirnoff size_t size; 548eb8dcdeaSGleb Smirnoff prison_addr_cmp_t *cmp; 549eb8dcdeaSGleb Smirnoff prison_addr_valid_t *valid; 550eb8dcdeaSGleb Smirnoff int ip_flag; 551eb8dcdeaSGleb Smirnoff } pr_families[PR_FAMILY_MAX] = { 552eb8dcdeaSGleb Smirnoff #ifdef INET 553eb8dcdeaSGleb Smirnoff [PR_INET] = { 554eb8dcdeaSGleb Smirnoff .size = sizeof(struct in_addr), 555eb8dcdeaSGleb Smirnoff .cmp = prison_qcmp_v4, 556eb8dcdeaSGleb Smirnoff .valid = prison_valid_v4, 557eb8dcdeaSGleb Smirnoff .ip_flag = PR_IP4_USER, 558eb8dcdeaSGleb Smirnoff }, 559eb8dcdeaSGleb Smirnoff #endif 560eb8dcdeaSGleb Smirnoff #ifdef INET6 561eb8dcdeaSGleb Smirnoff [PR_INET6] = { 562eb8dcdeaSGleb Smirnoff .size = sizeof(struct in6_addr), 563eb8dcdeaSGleb Smirnoff .cmp = prison_qcmp_v6, 564eb8dcdeaSGleb Smirnoff .valid = prison_valid_v6, 565eb8dcdeaSGleb Smirnoff .ip_flag = PR_IP6_USER, 566eb8dcdeaSGleb Smirnoff }, 567eb8dcdeaSGleb Smirnoff #endif 568eb8dcdeaSGleb Smirnoff }; 569eb8dcdeaSGleb Smirnoff 570eb8dcdeaSGleb Smirnoff /* 571eb8dcdeaSGleb Smirnoff * Network address lists (pr_addrs) allocation for jails. The addresses 572eb8dcdeaSGleb Smirnoff * are accessed locklessly by the network stack, thus need to be protected by 573eb8dcdeaSGleb Smirnoff * the network epoch. 574eb8dcdeaSGleb Smirnoff */ 575eb8dcdeaSGleb Smirnoff struct prison_ip { 576eb8dcdeaSGleb Smirnoff struct epoch_context ctx; 577eb8dcdeaSGleb Smirnoff uint32_t ips; 578eb8dcdeaSGleb Smirnoff #ifdef FUTURE_C 579eb8dcdeaSGleb Smirnoff union { 580eb8dcdeaSGleb Smirnoff struct in_addr pr_ip4[]; 581eb8dcdeaSGleb Smirnoff struct in6_addr pr_ip6[]; 582eb8dcdeaSGleb Smirnoff }; 583eb8dcdeaSGleb Smirnoff #else /* No future C :( */ 584eb8dcdeaSGleb Smirnoff #define PR_IP(pip, i) ((const char *)((pip) + 1) + pr_families[af].size * (i)) 585eb8dcdeaSGleb Smirnoff #define PR_IPD(pip, i) ((char *)((pip) + 1) + pr_families[af].size * (i)) 586eb8dcdeaSGleb Smirnoff #endif 587eb8dcdeaSGleb Smirnoff }; 588eb8dcdeaSGleb Smirnoff 589eb8dcdeaSGleb Smirnoff static struct prison_ip * 590eb8dcdeaSGleb Smirnoff prison_ip_alloc(const pr_family_t af, uint32_t cnt, int flags) 591eb8dcdeaSGleb Smirnoff { 592eb8dcdeaSGleb Smirnoff struct prison_ip *pip; 593eb8dcdeaSGleb Smirnoff 594eb8dcdeaSGleb Smirnoff pip = malloc(sizeof(struct prison_ip) + cnt * pr_families[af].size, 595eb8dcdeaSGleb Smirnoff M_PRISON, flags); 596eb8dcdeaSGleb Smirnoff if (pip != NULL) 597eb8dcdeaSGleb Smirnoff pip->ips = cnt; 598eb8dcdeaSGleb Smirnoff return (pip); 599eb8dcdeaSGleb Smirnoff } 600eb8dcdeaSGleb Smirnoff 601eb8dcdeaSGleb Smirnoff /* 602eb8dcdeaSGleb Smirnoff * Allocate and copyin user supplied address list, sorting and validating. 603eb8dcdeaSGleb Smirnoff * kern_jail_set() helper. 604eb8dcdeaSGleb Smirnoff */ 605eb8dcdeaSGleb Smirnoff static struct prison_ip * 606eb8dcdeaSGleb Smirnoff prison_ip_copyin(const pr_family_t af, void *op, uint32_t cnt) 607eb8dcdeaSGleb Smirnoff { 608eb8dcdeaSGleb Smirnoff prison_addr_cmp_t *const cmp = pr_families[af].cmp; 609eb8dcdeaSGleb Smirnoff const size_t size = pr_families[af].size; 610eb8dcdeaSGleb Smirnoff struct prison_ip *pip; 611eb8dcdeaSGleb Smirnoff 612eb8dcdeaSGleb Smirnoff pip = prison_ip_alloc(af, cnt, M_WAITOK); 613eb8dcdeaSGleb Smirnoff bcopy(op, pip + 1, cnt * size); 614eb8dcdeaSGleb Smirnoff /* 615eb8dcdeaSGleb Smirnoff * IP addresses are all sorted but ip[0] to preserve 616eb8dcdeaSGleb Smirnoff * the primary IP address as given from userland. 617eb8dcdeaSGleb Smirnoff * This special IP is used for unbound outgoing 618eb8dcdeaSGleb Smirnoff * connections as well for "loopback" traffic in case 619eb8dcdeaSGleb Smirnoff * source address selection cannot find any more fitting 620eb8dcdeaSGleb Smirnoff * address to connect from. 621eb8dcdeaSGleb Smirnoff */ 622eb8dcdeaSGleb Smirnoff if (cnt > 1) 623eb8dcdeaSGleb Smirnoff qsort((char *)(pip + 1) + size, cnt - 1, size, 624eb8dcdeaSGleb Smirnoff pr_families[af].cmp); 625eb8dcdeaSGleb Smirnoff /* 626eb8dcdeaSGleb Smirnoff * Check for duplicate addresses and do some simple 627eb8dcdeaSGleb Smirnoff * zero and broadcast checks. If users give other bogus 628eb8dcdeaSGleb Smirnoff * addresses it is their problem. 629eb8dcdeaSGleb Smirnoff */ 630eb8dcdeaSGleb Smirnoff for (int i = 0; i < cnt; i++) { 631eb8dcdeaSGleb Smirnoff if (!pr_families[af].valid(PR_IP(pip, i))) { 632eb8dcdeaSGleb Smirnoff free(pip, M_PRISON); 633eb8dcdeaSGleb Smirnoff return (NULL); 634eb8dcdeaSGleb Smirnoff } 635eb8dcdeaSGleb Smirnoff if (i + 1 < cnt && 636eb8dcdeaSGleb Smirnoff (cmp(PR_IP(pip, 0), PR_IP(pip, i + 1)) == 0 || 637eb8dcdeaSGleb Smirnoff cmp(PR_IP(pip, i), PR_IP(pip, i + 1)) == 0)) { 638eb8dcdeaSGleb Smirnoff free(pip, M_PRISON); 639eb8dcdeaSGleb Smirnoff return (NULL); 640eb8dcdeaSGleb Smirnoff } 641eb8dcdeaSGleb Smirnoff } 642eb8dcdeaSGleb Smirnoff 643eb8dcdeaSGleb Smirnoff return (pip); 644eb8dcdeaSGleb Smirnoff } 645eb8dcdeaSGleb Smirnoff 646eb8dcdeaSGleb Smirnoff /* 647eb8dcdeaSGleb Smirnoff * Allocate and dup parent prison address list. 648eb8dcdeaSGleb Smirnoff * kern_jail_set() helper. 649eb8dcdeaSGleb Smirnoff */ 650eb8dcdeaSGleb Smirnoff static void 651eb8dcdeaSGleb Smirnoff prison_ip_dup(struct prison *ppr, struct prison *pr, const pr_family_t af) 652eb8dcdeaSGleb Smirnoff { 653eb8dcdeaSGleb Smirnoff 654eb8dcdeaSGleb Smirnoff if (ppr->pr_addrs[af] != NULL) { 655eb8dcdeaSGleb Smirnoff pr->pr_addrs[af] = prison_ip_alloc(af, 656eb8dcdeaSGleb Smirnoff ppr->pr_addrs[af]->ips, M_WAITOK); 657ddbf879dSZhenlei Huang bcopy(ppr->pr_addrs[af] + 1, pr->pr_addrs[af] + 1, 658eb8dcdeaSGleb Smirnoff pr->pr_addrs[af]->ips * pr_families[af].size); 659eb8dcdeaSGleb Smirnoff } 660eb8dcdeaSGleb Smirnoff } 661eb8dcdeaSGleb Smirnoff 662eb8dcdeaSGleb Smirnoff /* 663eb8dcdeaSGleb Smirnoff * Make sure the new set of IP addresses is a subset of the parent's list. 664eb8dcdeaSGleb Smirnoff * Don't worry about the parent being unlocked, as any setting is done with 665eb8dcdeaSGleb Smirnoff * allprison_lock held. 666eb8dcdeaSGleb Smirnoff * kern_jail_set() helper. 667eb8dcdeaSGleb Smirnoff */ 668eb8dcdeaSGleb Smirnoff static bool 669eb8dcdeaSGleb Smirnoff prison_ip_parent_match(const struct prison_ip *ppip, 670eb8dcdeaSGleb Smirnoff const struct prison_ip *pip, const pr_family_t af) 671eb8dcdeaSGleb Smirnoff { 672eb8dcdeaSGleb Smirnoff prison_addr_cmp_t *const cmp = pr_families[af].cmp; 673eb8dcdeaSGleb Smirnoff int i, j; 674eb8dcdeaSGleb Smirnoff 675eb8dcdeaSGleb Smirnoff if (ppip == NULL) 676eb8dcdeaSGleb Smirnoff return (false); 677eb8dcdeaSGleb Smirnoff 678eb8dcdeaSGleb Smirnoff for (i = 0; i < ppip->ips; i++) 679eb8dcdeaSGleb Smirnoff if (cmp(PR_IP(pip, 0), PR_IP(ppip, i)) == 0) 680eb8dcdeaSGleb Smirnoff break; 681eb8dcdeaSGleb Smirnoff 682eb8dcdeaSGleb Smirnoff if (i == ppip->ips) 683eb8dcdeaSGleb Smirnoff /* Main address not present in parent. */ 684eb8dcdeaSGleb Smirnoff return (false); 685eb8dcdeaSGleb Smirnoff 686eb8dcdeaSGleb Smirnoff if (pip->ips > 1) { 687eb8dcdeaSGleb Smirnoff for (i = j = 1; i < pip->ips; i++) { 688eb8dcdeaSGleb Smirnoff if (cmp(PR_IP(pip, i), PR_IP(ppip, 0)) == 0) 689eb8dcdeaSGleb Smirnoff /* Equals to parent primary address. */ 690eb8dcdeaSGleb Smirnoff continue; 691eb8dcdeaSGleb Smirnoff for (; j < ppip->ips; j++) 692eb8dcdeaSGleb Smirnoff if (cmp(PR_IP(pip, i), PR_IP(ppip, j)) == 0) 693eb8dcdeaSGleb Smirnoff break; 694eb8dcdeaSGleb Smirnoff if (j == ppip->ips) 695eb8dcdeaSGleb Smirnoff break; 696eb8dcdeaSGleb Smirnoff } 697eb8dcdeaSGleb Smirnoff if (j == ppip->ips) 698eb8dcdeaSGleb Smirnoff /* Address not present in parent. */ 699eb8dcdeaSGleb Smirnoff return (false); 700eb8dcdeaSGleb Smirnoff } 701eb8dcdeaSGleb Smirnoff return (true); 702eb8dcdeaSGleb Smirnoff } 703eb8dcdeaSGleb Smirnoff 704eb8dcdeaSGleb Smirnoff /* 705eb8dcdeaSGleb Smirnoff * Check for conflicting IP addresses. We permit them if there is no more 706eb8dcdeaSGleb Smirnoff * than one IP on each jail. If there is a duplicate on a jail with more 707eb8dcdeaSGleb Smirnoff * than one IP stop checking and return error. 708eb8dcdeaSGleb Smirnoff * kern_jail_set() helper. 709eb8dcdeaSGleb Smirnoff */ 710eb8dcdeaSGleb Smirnoff static bool 711eb8dcdeaSGleb Smirnoff prison_ip_conflict_check(const struct prison *ppr, const struct prison *pr, 712eb8dcdeaSGleb Smirnoff const struct prison_ip *pip, pr_family_t af) 713eb8dcdeaSGleb Smirnoff { 714eb8dcdeaSGleb Smirnoff const struct prison *tppr, *tpr; 715eb8dcdeaSGleb Smirnoff int descend; 716eb8dcdeaSGleb Smirnoff 717eb8dcdeaSGleb Smirnoff #ifdef VIMAGE 718eb8dcdeaSGleb Smirnoff for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent) 719eb8dcdeaSGleb Smirnoff if (tppr->pr_flags & PR_VNET) 720eb8dcdeaSGleb Smirnoff break; 721eb8dcdeaSGleb Smirnoff #else 722eb8dcdeaSGleb Smirnoff tppr = &prison0; 723eb8dcdeaSGleb Smirnoff #endif 724eb8dcdeaSGleb Smirnoff FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 725eb8dcdeaSGleb Smirnoff if (tpr == pr || 726eb8dcdeaSGleb Smirnoff #ifdef VIMAGE 727eb8dcdeaSGleb Smirnoff (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 728eb8dcdeaSGleb Smirnoff #endif 729eb8dcdeaSGleb Smirnoff !prison_isalive(tpr)) { 730eb8dcdeaSGleb Smirnoff descend = 0; 731eb8dcdeaSGleb Smirnoff continue; 732eb8dcdeaSGleb Smirnoff } 733eb8dcdeaSGleb Smirnoff if (!(tpr->pr_flags & pr_families[af].ip_flag)) 734eb8dcdeaSGleb Smirnoff continue; 735eb8dcdeaSGleb Smirnoff descend = 0; 736eb8dcdeaSGleb Smirnoff if (tpr->pr_addrs[af] == NULL || 737eb8dcdeaSGleb Smirnoff (pip->ips == 1 && tpr->pr_addrs[af]->ips == 1)) 738eb8dcdeaSGleb Smirnoff continue; 739eb8dcdeaSGleb Smirnoff for (int i = 0; i < pip->ips; i++) 740eb8dcdeaSGleb Smirnoff if (prison_ip_check(tpr, af, PR_IP(pip, i)) == 0) 741eb8dcdeaSGleb Smirnoff return (false); 742eb8dcdeaSGleb Smirnoff } 743eb8dcdeaSGleb Smirnoff 744eb8dcdeaSGleb Smirnoff return (true); 745eb8dcdeaSGleb Smirnoff } 746eb8dcdeaSGleb Smirnoff 747eb8dcdeaSGleb Smirnoff _Static_assert(offsetof(struct prison_ip, ctx) == 0, 748eb8dcdeaSGleb Smirnoff "prison must start with epoch context"); 749eb8dcdeaSGleb Smirnoff static void 750eb8dcdeaSGleb Smirnoff prison_ip_free_deferred(epoch_context_t ctx) 751eb8dcdeaSGleb Smirnoff { 752eb8dcdeaSGleb Smirnoff 753eb8dcdeaSGleb Smirnoff free(ctx, M_PRISON); 754eb8dcdeaSGleb Smirnoff } 755eb8dcdeaSGleb Smirnoff 756eb8dcdeaSGleb Smirnoff static void 757eb8dcdeaSGleb Smirnoff prison_ip_free(struct prison_ip *pip) 758eb8dcdeaSGleb Smirnoff { 759eb8dcdeaSGleb Smirnoff 760eb8dcdeaSGleb Smirnoff if (pip != NULL) 761eb8dcdeaSGleb Smirnoff NET_EPOCH_CALL(prison_ip_free_deferred, &pip->ctx); 762eb8dcdeaSGleb Smirnoff } 763eb8dcdeaSGleb Smirnoff 764eb8dcdeaSGleb Smirnoff static void 765eb8dcdeaSGleb Smirnoff prison_ip_set(struct prison *pr, const pr_family_t af, struct prison_ip *new) 766eb8dcdeaSGleb Smirnoff { 767eb8dcdeaSGleb Smirnoff struct prison_ip **mem, *old; 768eb8dcdeaSGleb Smirnoff 769eb8dcdeaSGleb Smirnoff mtx_assert(&pr->pr_mtx, MA_OWNED); 770eb8dcdeaSGleb Smirnoff 771eb8dcdeaSGleb Smirnoff mem = &pr->pr_addrs[af]; 772eb8dcdeaSGleb Smirnoff 773eb8dcdeaSGleb Smirnoff old = *mem; 774*27202b98SMark Johnston atomic_store_ptr(mem, new); 775eb8dcdeaSGleb Smirnoff prison_ip_free(old); 776eb8dcdeaSGleb Smirnoff } 777eb8dcdeaSGleb Smirnoff 778eb8dcdeaSGleb Smirnoff /* 779eb8dcdeaSGleb Smirnoff * Restrict a prison's IP address list with its parent's, possibly replacing 7808bce8d28SZhenlei Huang * it. Return true if succeed, otherwise should redo. 781eb8dcdeaSGleb Smirnoff * kern_jail_set() helper. 782eb8dcdeaSGleb Smirnoff */ 783eb8dcdeaSGleb Smirnoff static bool 784eb8dcdeaSGleb Smirnoff prison_ip_restrict(struct prison *pr, const pr_family_t af, 7858bce8d28SZhenlei Huang struct prison_ip **newp) 786eb8dcdeaSGleb Smirnoff { 787eb8dcdeaSGleb Smirnoff const struct prison_ip *ppip = pr->pr_parent->pr_addrs[af]; 788eb8dcdeaSGleb Smirnoff const struct prison_ip *pip = pr->pr_addrs[af]; 789eb8dcdeaSGleb Smirnoff int (*const cmp)(const void *, const void *) = pr_families[af].cmp; 790eb8dcdeaSGleb Smirnoff const size_t size = pr_families[af].size; 7918bce8d28SZhenlei Huang struct prison_ip *new = newp != NULL ? *newp : NULL; 792eb8dcdeaSGleb Smirnoff uint32_t ips; 793eb8dcdeaSGleb Smirnoff 794eb8dcdeaSGleb Smirnoff mtx_assert(&pr->pr_mtx, MA_OWNED); 795eb8dcdeaSGleb Smirnoff 796eb8dcdeaSGleb Smirnoff /* 797eb8dcdeaSGleb Smirnoff * Due to epoch-synchronized access to the IP address lists we always 798eb8dcdeaSGleb Smirnoff * allocate a new list even if the old one has enough space. We could 799eb8dcdeaSGleb Smirnoff * atomically update an IPv4 address inside a list, but that would 800eb8dcdeaSGleb Smirnoff * screw up sorting, and in case of IPv6 we can't even atomically write 801eb8dcdeaSGleb Smirnoff * one. 802eb8dcdeaSGleb Smirnoff */ 80389ddfbbaSZhenlei Huang if (ppip == NULL) { 80489ddfbbaSZhenlei Huang if (pip != NULL) 805eb8dcdeaSGleb Smirnoff prison_ip_set(pr, af, NULL); 8068bce8d28SZhenlei Huang return (true); 807eb8dcdeaSGleb Smirnoff } 80889ddfbbaSZhenlei Huang 809eb8dcdeaSGleb Smirnoff if (!(pr->pr_flags & pr_families[af].ip_flag)) { 81089ddfbbaSZhenlei Huang if (new == NULL) { 81189ddfbbaSZhenlei Huang new = prison_ip_alloc(af, ppip->ips, M_NOWAIT); 81289ddfbbaSZhenlei Huang if (new == NULL) 8138bce8d28SZhenlei Huang return (false); /* Redo */ 8148bce8d28SZhenlei Huang } 815eb8dcdeaSGleb Smirnoff /* This has no user settings, so just copy the parent's list. */ 81689ddfbbaSZhenlei Huang MPASS(new->ips == ppip->ips); 81789ddfbbaSZhenlei Huang bcopy(ppip + 1, new + 1, ppip->ips * size); 81889ddfbbaSZhenlei Huang prison_ip_set(pr, af, new); 8198bce8d28SZhenlei Huang if (newp != NULL) 8208bce8d28SZhenlei Huang *newp = NULL; /* Used */ 82189ddfbbaSZhenlei Huang } else if (pip != NULL) { 822eb8dcdeaSGleb Smirnoff /* Remove addresses that aren't in the parent. */ 823eb8dcdeaSGleb Smirnoff int i; 824eb8dcdeaSGleb Smirnoff 825eb8dcdeaSGleb Smirnoff i = 0; /* index in pip */ 826eb8dcdeaSGleb Smirnoff ips = 0; /* index in new */ 827eb8dcdeaSGleb Smirnoff 82889ddfbbaSZhenlei Huang if (new == NULL) { 82989ddfbbaSZhenlei Huang new = prison_ip_alloc(af, pip->ips, M_NOWAIT); 83089ddfbbaSZhenlei Huang if (new == NULL) 8318bce8d28SZhenlei Huang return (false); /* Redo */ 83289ddfbbaSZhenlei Huang } 83389ddfbbaSZhenlei Huang 834eb8dcdeaSGleb Smirnoff for (int pi = 0; pi < ppip->ips; pi++) 835eb8dcdeaSGleb Smirnoff if (cmp(PR_IP(pip, 0), PR_IP(ppip, pi)) == 0) { 836eb8dcdeaSGleb Smirnoff /* Found our primary address in parent. */ 837eb8dcdeaSGleb Smirnoff bcopy(PR_IP(pip, i), PR_IPD(new, ips), size); 838eb8dcdeaSGleb Smirnoff i++; 839eb8dcdeaSGleb Smirnoff ips++; 840eb8dcdeaSGleb Smirnoff break; 841eb8dcdeaSGleb Smirnoff } 842eb8dcdeaSGleb Smirnoff for (int pi = 1; i < pip->ips; ) { 843eb8dcdeaSGleb Smirnoff /* Check against primary, which is unsorted. */ 844eb8dcdeaSGleb Smirnoff if (cmp(PR_IP(pip, i), PR_IP(ppip, 0)) == 0) { 845eb8dcdeaSGleb Smirnoff /* Matches parent's primary address. */ 846eb8dcdeaSGleb Smirnoff bcopy(PR_IP(pip, i), PR_IPD(new, ips), size); 847eb8dcdeaSGleb Smirnoff i++; 848eb8dcdeaSGleb Smirnoff ips++; 849eb8dcdeaSGleb Smirnoff continue; 850eb8dcdeaSGleb Smirnoff } 851eb8dcdeaSGleb Smirnoff /* The rest are sorted. */ 852eb8dcdeaSGleb Smirnoff switch (pi >= ppip->ips ? -1 : 853eb8dcdeaSGleb Smirnoff cmp(PR_IP(pip, i), PR_IP(ppip, pi))) { 854eb8dcdeaSGleb Smirnoff case -1: 855eb8dcdeaSGleb Smirnoff i++; 856eb8dcdeaSGleb Smirnoff break; 857eb8dcdeaSGleb Smirnoff case 0: 858ddbf879dSZhenlei Huang bcopy(PR_IP(pip, i), PR_IPD(new, ips), size); 859eb8dcdeaSGleb Smirnoff i++; 860eb8dcdeaSGleb Smirnoff pi++; 861eb8dcdeaSGleb Smirnoff ips++; 862eb8dcdeaSGleb Smirnoff break; 863eb8dcdeaSGleb Smirnoff case 1: 864eb8dcdeaSGleb Smirnoff pi++; 865eb8dcdeaSGleb Smirnoff break; 866eb8dcdeaSGleb Smirnoff } 867eb8dcdeaSGleb Smirnoff } 868eb8dcdeaSGleb Smirnoff if (ips == 0) { 8698bce8d28SZhenlei Huang if (newp == NULL || *newp == NULL) 870eb8dcdeaSGleb Smirnoff prison_ip_free(new); 871eb8dcdeaSGleb Smirnoff new = NULL; 87289ddfbbaSZhenlei Huang } else { 87389ddfbbaSZhenlei Huang /* Shrink to real size */ 87489ddfbbaSZhenlei Huang KASSERT((new->ips >= ips), 87589ddfbbaSZhenlei Huang ("Out-of-bounds write to prison_ip %p", new)); 87689ddfbbaSZhenlei Huang new->ips = ips; 877eb8dcdeaSGleb Smirnoff } 878eb8dcdeaSGleb Smirnoff prison_ip_set(pr, af, new); 8798bce8d28SZhenlei Huang if (newp != NULL) 8808bce8d28SZhenlei Huang *newp = NULL; /* Used */ 88189ddfbbaSZhenlei Huang } 8828bce8d28SZhenlei Huang return (true); 883eb8dcdeaSGleb Smirnoff } 884eb8dcdeaSGleb Smirnoff 885eb8dcdeaSGleb Smirnoff /* 886eb8dcdeaSGleb Smirnoff * Fast-path check if an address belongs to a prison. 887eb8dcdeaSGleb Smirnoff */ 888eb8dcdeaSGleb Smirnoff int 889eb8dcdeaSGleb Smirnoff prison_ip_check(const struct prison *pr, const pr_family_t af, 890eb8dcdeaSGleb Smirnoff const void *addr) 891eb8dcdeaSGleb Smirnoff { 892eb8dcdeaSGleb Smirnoff int (*const cmp)(const void *, const void *) = pr_families[af].cmp; 893eb8dcdeaSGleb Smirnoff const struct prison_ip *pip; 894eb8dcdeaSGleb Smirnoff int i, a, z, d; 895eb8dcdeaSGleb Smirnoff 896eb8dcdeaSGleb Smirnoff MPASS(mtx_owned(&pr->pr_mtx) || 897eb8dcdeaSGleb Smirnoff in_epoch(net_epoch_preempt) || 898eb8dcdeaSGleb Smirnoff sx_xlocked(&allprison_lock)); 899eb8dcdeaSGleb Smirnoff 900*27202b98SMark Johnston pip = atomic_load_ptr(&pr->pr_addrs[af]); 901eb8dcdeaSGleb Smirnoff if (__predict_false(pip == NULL)) 902eb8dcdeaSGleb Smirnoff return (EAFNOSUPPORT); 903eb8dcdeaSGleb Smirnoff 904eb8dcdeaSGleb Smirnoff /* Check the primary IP. */ 905eb8dcdeaSGleb Smirnoff if (cmp(PR_IP(pip, 0), addr) == 0) 906eb8dcdeaSGleb Smirnoff return (0); 907eb8dcdeaSGleb Smirnoff 908eb8dcdeaSGleb Smirnoff /* 909eb8dcdeaSGleb Smirnoff * All the other IPs are sorted so we can do a binary search. 910eb8dcdeaSGleb Smirnoff */ 911eb8dcdeaSGleb Smirnoff a = 0; 912eb8dcdeaSGleb Smirnoff z = pip->ips - 2; 913eb8dcdeaSGleb Smirnoff while (a <= z) { 914eb8dcdeaSGleb Smirnoff i = (a + z) / 2; 915eb8dcdeaSGleb Smirnoff d = cmp(PR_IP(pip, i + 1), addr); 916eb8dcdeaSGleb Smirnoff if (d > 0) 917eb8dcdeaSGleb Smirnoff z = i - 1; 918eb8dcdeaSGleb Smirnoff else if (d < 0) 919eb8dcdeaSGleb Smirnoff a = i + 1; 920eb8dcdeaSGleb Smirnoff else 921eb8dcdeaSGleb Smirnoff return (0); 922eb8dcdeaSGleb Smirnoff } 923eb8dcdeaSGleb Smirnoff 924eb8dcdeaSGleb Smirnoff return (EADDRNOTAVAIL); 925eb8dcdeaSGleb Smirnoff } 926eb8dcdeaSGleb Smirnoff 927eb8dcdeaSGleb Smirnoff /* 928eb8dcdeaSGleb Smirnoff * Grab primary IP. Historically required mutex, but nothing prevents 929eb8dcdeaSGleb Smirnoff * us to support epoch-protected access. Is it used in fast path? 930eb8dcdeaSGleb Smirnoff * in{6}_jail.c helper 931eb8dcdeaSGleb Smirnoff */ 932eb8dcdeaSGleb Smirnoff const void * 933eb8dcdeaSGleb Smirnoff prison_ip_get0(const struct prison *pr, const pr_family_t af) 934eb8dcdeaSGleb Smirnoff { 935eb8dcdeaSGleb Smirnoff const struct prison_ip *pip = pr->pr_addrs[af]; 936eb8dcdeaSGleb Smirnoff 937eb8dcdeaSGleb Smirnoff mtx_assert(&pr->pr_mtx, MA_OWNED); 938eb8dcdeaSGleb Smirnoff MPASS(pip); 939eb8dcdeaSGleb Smirnoff 940eb8dcdeaSGleb Smirnoff return (pip + 1); 941eb8dcdeaSGleb Smirnoff } 942eb8dcdeaSGleb Smirnoff 943eb8dcdeaSGleb Smirnoff u_int 944eb8dcdeaSGleb Smirnoff prison_ip_cnt(const struct prison *pr, const pr_family_t af) 945eb8dcdeaSGleb Smirnoff { 946eb8dcdeaSGleb Smirnoff 947eb8dcdeaSGleb Smirnoff return (pr->pr_addrs[af]->ips); 948eb8dcdeaSGleb Smirnoff } 949eb8dcdeaSGleb Smirnoff #endif /* defined(INET) || defined(INET6) */ 950eb8dcdeaSGleb Smirnoff 951413628a7SBjoern A. Zeeb int 952b38ff370SJamie Gritton kern_jail_set(struct thread *td, struct uio *optuio, int flags) 953413628a7SBjoern A. Zeeb { 954fd7a8150SMike Barcroft struct nameidata nd; 955b38ff370SJamie Gritton #ifdef INET 956eb8dcdeaSGleb Smirnoff struct prison_ip *ip4; 957413628a7SBjoern A. Zeeb #endif 958b38ff370SJamie Gritton #ifdef INET6 959eb8dcdeaSGleb Smirnoff struct prison_ip *ip6; 960b38ff370SJamie Gritton #endif 961b38ff370SJamie Gritton struct vfsopt *opt; 962b38ff370SJamie Gritton struct vfsoptlist *opts; 9637de883c8SJamie Gritton struct prison *pr, *deadpr, *inspr, *mypr, *ppr, *tpr; 964b38ff370SJamie Gritton struct vnode *root; 965babbbb9cSJamie Gritton char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid; 966b96bd95bSIan Lepore char *g_path, *osrelstr; 967672756aaSJamie Gritton struct bool_flags *bf; 968672756aaSJamie Gritton struct jailsys_flags *jsf; 9690304c731SJamie Gritton #if defined(INET) || defined(INET6) 970b38ff370SJamie Gritton void *op; 9710304c731SJamie Gritton #endif 97276ca6f88SJamie Gritton unsigned long hid; 973b6f47c23SJamie Gritton size_t namelen, onamelen, pnamelen; 9742a4b2251SJamie Gritton int born, created, cuflags, descend, drflags, enforce; 975cc5fd8c7SJamie Gritton int error, errmsg_len, errmsg_pos; 9760cc207a6SMartin Matuska int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel; 977672756aaSJamie Gritton int jid, jsys, len, level; 978b96bd95bSIan Lepore int childmax, osreldt, rsnum, slevel; 979413628a7SBjoern A. Zeeb #ifdef INET 9808bce8d28SZhenlei Huang int ip4s; 9818bce8d28SZhenlei Huang bool redo_ip4; 982413628a7SBjoern A. Zeeb #endif 983b38ff370SJamie Gritton #ifdef INET6 9848bce8d28SZhenlei Huang int ip6s; 9858bce8d28SZhenlei Huang bool redo_ip6; 986b38ff370SJamie Gritton #endif 9876beb3bb4SKirk McKusick uint64_t pr_allow, ch_allow, pr_flags, ch_flags; 988b3079544SJamie Gritton uint64_t pr_allow_diff; 9896beb3bb4SKirk McKusick unsigned tallow; 990b38ff370SJamie Gritton char numbuf[12]; 991b38ff370SJamie Gritton 992b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_SET); 993b38ff370SJamie Gritton if (!error && (flags & JAIL_ATTACH)) 994b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 995b38ff370SJamie Gritton if (error) 99675c13541SPoul-Henning Kamp return (error); 997b6f47c23SJamie Gritton mypr = td->td_ucred->cr_prison; 998b97457e2SJamie Gritton if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0) 9990304c731SJamie Gritton return (EPERM); 1000b38ff370SJamie Gritton if (flags & ~JAIL_SET_MASK) 1001b38ff370SJamie Gritton return (EINVAL); 1002b38ff370SJamie Gritton 1003b38ff370SJamie Gritton /* 1004b38ff370SJamie Gritton * Check all the parameters before committing to anything. Not all 1005b38ff370SJamie Gritton * errors can be caught early, but we may as well try. Also, this 1006b38ff370SJamie Gritton * takes care of some expensive stuff (path lookup) before getting 1007b38ff370SJamie Gritton * the allprison lock. 1008b38ff370SJamie Gritton * 1009b38ff370SJamie Gritton * XXX Jails are not filesystems, and jail parameters are not mount 1010b38ff370SJamie Gritton * options. But it makes more sense to re-use the vfsopt code 1011b38ff370SJamie Gritton * than duplicate it under a different name. 1012b38ff370SJamie Gritton */ 1013b38ff370SJamie Gritton error = vfs_buildopts(optuio, &opts); 1014b38ff370SJamie Gritton if (error) 1015b38ff370SJamie Gritton return (error); 1016b38ff370SJamie Gritton #ifdef INET 1017b38ff370SJamie Gritton ip4 = NULL; 1018b38ff370SJamie Gritton #endif 1019b38ff370SJamie Gritton #ifdef INET6 1020b38ff370SJamie Gritton ip6 = NULL; 1021b38ff370SJamie Gritton #endif 10226dfe0a3dSMartin Matuska g_path = NULL; 1023b38ff370SJamie Gritton 1024b6f47c23SJamie Gritton cuflags = flags & (JAIL_CREATE | JAIL_UPDATE); 1025b6f47c23SJamie Gritton if (!cuflags) { 1026b6f47c23SJamie Gritton error = EINVAL; 1027b6f47c23SJamie Gritton vfs_opterror(opts, "no valid operation (create or update)"); 1028b6f47c23SJamie Gritton goto done_errmsg; 1029b6f47c23SJamie Gritton } 1030b6f47c23SJamie Gritton 1031b38ff370SJamie Gritton error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 1032b38ff370SJamie Gritton if (error == ENOENT) 1033b38ff370SJamie Gritton jid = 0; 1034b38ff370SJamie Gritton else if (error != 0) 1035b38ff370SJamie Gritton goto done_free; 1036b38ff370SJamie Gritton 1037b38ff370SJamie Gritton error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel)); 1038b38ff370SJamie Gritton if (error == ENOENT) 1039b38ff370SJamie Gritton gotslevel = 0; 1040b38ff370SJamie Gritton else if (error != 0) 1041b38ff370SJamie Gritton goto done_free; 1042b38ff370SJamie Gritton else 1043b38ff370SJamie Gritton gotslevel = 1; 1044b38ff370SJamie Gritton 1045b97457e2SJamie Gritton error = 1046b97457e2SJamie Gritton vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax)); 1047b97457e2SJamie Gritton if (error == ENOENT) 1048b97457e2SJamie Gritton gotchildmax = 0; 1049b97457e2SJamie Gritton else if (error != 0) 1050b97457e2SJamie Gritton goto done_free; 1051b97457e2SJamie Gritton else 1052b97457e2SJamie Gritton gotchildmax = 1; 1053b97457e2SJamie Gritton 10540304c731SJamie Gritton error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce)); 1055f337198dSJamie Gritton if (error == ENOENT) 1056f337198dSJamie Gritton gotenforce = 0; 1057f337198dSJamie Gritton else if (error != 0) 10580304c731SJamie Gritton goto done_free; 1059f337198dSJamie Gritton else if (enforce < 0 || enforce > 2) { 1060f337198dSJamie Gritton error = EINVAL; 1061f337198dSJamie Gritton goto done_free; 1062f337198dSJamie Gritton } else 1063f337198dSJamie Gritton gotenforce = 1; 10640304c731SJamie Gritton 10650cc207a6SMartin Matuska error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum)); 10660cc207a6SMartin Matuska if (error == ENOENT) 10670cc207a6SMartin Matuska gotrsnum = 0; 10680cc207a6SMartin Matuska else if (error != 0) 10690cc207a6SMartin Matuska goto done_free; 10700cc207a6SMartin Matuska else 10710cc207a6SMartin Matuska gotrsnum = 1; 10720cc207a6SMartin Matuska 1073b38ff370SJamie Gritton pr_flags = ch_flags = 0; 1074672756aaSJamie Gritton for (bf = pr_flag_bool; 1075672756aaSJamie Gritton bf < pr_flag_bool + nitems(pr_flag_bool); 1076672756aaSJamie Gritton bf++) { 1077672756aaSJamie Gritton vfs_flagopt(opts, bf->name, &pr_flags, bf->flag); 1078672756aaSJamie Gritton vfs_flagopt(opts, bf->noname, &ch_flags, bf->flag); 10790304c731SJamie Gritton } 1080b38ff370SJamie Gritton ch_flags |= pr_flags; 1081672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 1082672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 1083672756aaSJamie Gritton jsf++) { 1084672756aaSJamie Gritton error = vfs_copyopt(opts, jsf->name, &jsys, sizeof(jsys)); 10857cbf7213SJamie Gritton if (error == ENOENT) 10867cbf7213SJamie Gritton continue; 10877cbf7213SJamie Gritton if (error != 0) 10887cbf7213SJamie Gritton goto done_free; 10897cbf7213SJamie Gritton switch (jsys) { 10907cbf7213SJamie Gritton case JAIL_SYS_DISABLE: 1091672756aaSJamie Gritton if (!jsf->disable) { 10927cbf7213SJamie Gritton error = EINVAL; 10937cbf7213SJamie Gritton goto done_free; 10947cbf7213SJamie Gritton } 1095672756aaSJamie Gritton pr_flags |= jsf->disable; 10967cbf7213SJamie Gritton break; 10977cbf7213SJamie Gritton case JAIL_SYS_NEW: 1098672756aaSJamie Gritton pr_flags |= jsf->new; 10997cbf7213SJamie Gritton break; 11007cbf7213SJamie Gritton case JAIL_SYS_INHERIT: 11017cbf7213SJamie Gritton break; 11027cbf7213SJamie Gritton default: 11037cbf7213SJamie Gritton error = EINVAL; 11047cbf7213SJamie Gritton goto done_free; 11057cbf7213SJamie Gritton } 1106672756aaSJamie Gritton ch_flags |= jsf->new | jsf->disable; 11077cbf7213SJamie Gritton } 11081158508aSJamie Gritton if ((flags & (JAIL_CREATE | JAIL_ATTACH)) == JAIL_CREATE 11094affa14cSJamie Gritton && !(pr_flags & PR_PERSIST)) { 11104affa14cSJamie Gritton error = EINVAL; 11114affa14cSJamie Gritton vfs_opterror(opts, "new jail must persist or attach"); 11124affa14cSJamie Gritton goto done_errmsg; 11134affa14cSJamie Gritton } 1114679e1390SJamie Gritton #ifdef VIMAGE 1115679e1390SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) { 1116679e1390SJamie Gritton error = EINVAL; 1117679e1390SJamie Gritton vfs_opterror(opts, "vnet cannot be changed after creation"); 1118679e1390SJamie Gritton goto done_errmsg; 1119679e1390SJamie Gritton } 1120679e1390SJamie Gritton #endif 11212b0d6f81SJamie Gritton #ifdef INET 11222b0d6f81SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) { 11232b0d6f81SJamie Gritton error = EINVAL; 11242b0d6f81SJamie Gritton vfs_opterror(opts, "ip4 cannot be changed after creation"); 11252b0d6f81SJamie Gritton goto done_errmsg; 11262b0d6f81SJamie Gritton } 11272b0d6f81SJamie Gritton #endif 11282b0d6f81SJamie Gritton #ifdef INET6 11292b0d6f81SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) { 11302b0d6f81SJamie Gritton error = EINVAL; 11312b0d6f81SJamie Gritton vfs_opterror(opts, "ip6 cannot be changed after creation"); 11322b0d6f81SJamie Gritton goto done_errmsg; 11332b0d6f81SJamie Gritton } 11342b0d6f81SJamie Gritton #endif 1135b38ff370SJamie Gritton 11360304c731SJamie Gritton pr_allow = ch_allow = 0; 1137672756aaSJamie Gritton for (bf = pr_flag_allow; 11385d58f959SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 11395d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 1140672756aaSJamie Gritton bf++) { 1141672756aaSJamie Gritton vfs_flagopt(opts, bf->name, &pr_allow, bf->flag); 1142672756aaSJamie Gritton vfs_flagopt(opts, bf->noname, &ch_allow, bf->flag); 11430304c731SJamie Gritton } 11440304c731SJamie Gritton ch_allow |= pr_allow; 11450304c731SJamie Gritton 1146b38ff370SJamie Gritton error = vfs_getopt(opts, "name", (void **)&name, &len); 1147b38ff370SJamie Gritton if (error == ENOENT) 1148b38ff370SJamie Gritton name = NULL; 1149b38ff370SJamie Gritton else if (error != 0) 1150b38ff370SJamie Gritton goto done_free; 1151b38ff370SJamie Gritton else { 1152b38ff370SJamie Gritton if (len == 0 || name[len - 1] != '\0') { 1153b38ff370SJamie Gritton error = EINVAL; 1154b38ff370SJamie Gritton goto done_free; 1155b38ff370SJamie Gritton } 1156b38ff370SJamie Gritton if (len > MAXHOSTNAMELEN) { 1157b38ff370SJamie Gritton error = ENAMETOOLONG; 1158b38ff370SJamie Gritton goto done_free; 1159b38ff370SJamie Gritton } 1160b38ff370SJamie Gritton } 1161b38ff370SJamie Gritton 1162b38ff370SJamie Gritton error = vfs_getopt(opts, "host.hostname", (void **)&host, &len); 1163b38ff370SJamie Gritton if (error == ENOENT) 1164b38ff370SJamie Gritton host = NULL; 1165b38ff370SJamie Gritton else if (error != 0) 1166b38ff370SJamie Gritton goto done_free; 1167b38ff370SJamie Gritton else { 116876ca6f88SJamie Gritton ch_flags |= PR_HOST; 116976ca6f88SJamie Gritton pr_flags |= PR_HOST; 1170b38ff370SJamie Gritton if (len == 0 || host[len - 1] != '\0') { 1171b38ff370SJamie Gritton error = EINVAL; 1172b38ff370SJamie Gritton goto done_free; 1173b38ff370SJamie Gritton } 1174b38ff370SJamie Gritton if (len > MAXHOSTNAMELEN) { 1175b38ff370SJamie Gritton error = ENAMETOOLONG; 1176b38ff370SJamie Gritton goto done_free; 1177b38ff370SJamie Gritton } 1178b38ff370SJamie Gritton } 1179b38ff370SJamie Gritton 118076ca6f88SJamie Gritton error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len); 118176ca6f88SJamie Gritton if (error == ENOENT) 118276ca6f88SJamie Gritton domain = NULL; 118376ca6f88SJamie Gritton else if (error != 0) 118476ca6f88SJamie Gritton goto done_free; 118576ca6f88SJamie Gritton else { 118676ca6f88SJamie Gritton ch_flags |= PR_HOST; 118776ca6f88SJamie Gritton pr_flags |= PR_HOST; 118876ca6f88SJamie Gritton if (len == 0 || domain[len - 1] != '\0') { 118976ca6f88SJamie Gritton error = EINVAL; 119076ca6f88SJamie Gritton goto done_free; 119176ca6f88SJamie Gritton } 119276ca6f88SJamie Gritton if (len > MAXHOSTNAMELEN) { 119376ca6f88SJamie Gritton error = ENAMETOOLONG; 119476ca6f88SJamie Gritton goto done_free; 119576ca6f88SJamie Gritton } 119676ca6f88SJamie Gritton } 119776ca6f88SJamie Gritton 119876ca6f88SJamie Gritton error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len); 119976ca6f88SJamie Gritton if (error == ENOENT) 120076ca6f88SJamie Gritton uuid = NULL; 120176ca6f88SJamie Gritton else if (error != 0) 120276ca6f88SJamie Gritton goto done_free; 120376ca6f88SJamie Gritton else { 120476ca6f88SJamie Gritton ch_flags |= PR_HOST; 120576ca6f88SJamie Gritton pr_flags |= PR_HOST; 120676ca6f88SJamie Gritton if (len == 0 || uuid[len - 1] != '\0') { 120776ca6f88SJamie Gritton error = EINVAL; 120876ca6f88SJamie Gritton goto done_free; 120976ca6f88SJamie Gritton } 121076ca6f88SJamie Gritton if (len > HOSTUUIDLEN) { 121176ca6f88SJamie Gritton error = ENAMETOOLONG; 121276ca6f88SJamie Gritton goto done_free; 121376ca6f88SJamie Gritton } 121476ca6f88SJamie Gritton } 121576ca6f88SJamie Gritton 1216841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32 1217a5c1afadSDmitry Chagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 121876ca6f88SJamie Gritton uint32_t hid32; 121976ca6f88SJamie Gritton 122076ca6f88SJamie Gritton error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32)); 122176ca6f88SJamie Gritton hid = hid32; 122276ca6f88SJamie Gritton } else 122376ca6f88SJamie Gritton #endif 122476ca6f88SJamie Gritton error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid)); 122576ca6f88SJamie Gritton if (error == ENOENT) 122676ca6f88SJamie Gritton gothid = 0; 122776ca6f88SJamie Gritton else if (error != 0) 122876ca6f88SJamie Gritton goto done_free; 122976ca6f88SJamie Gritton else { 123076ca6f88SJamie Gritton gothid = 1; 123176ca6f88SJamie Gritton ch_flags |= PR_HOST; 123276ca6f88SJamie Gritton pr_flags |= PR_HOST; 123376ca6f88SJamie Gritton } 123476ca6f88SJamie Gritton 1235b38ff370SJamie Gritton #ifdef INET 1236b38ff370SJamie Gritton error = vfs_getopt(opts, "ip4.addr", &op, &ip4s); 1237b38ff370SJamie Gritton if (error == ENOENT) 12380e5e396eSJamie Gritton ip4s = 0; 1239b38ff370SJamie Gritton else if (error != 0) 1240b38ff370SJamie Gritton goto done_free; 1241eb8dcdeaSGleb Smirnoff else if (ip4s & (sizeof(struct in_addr) - 1)) { 1242b38ff370SJamie Gritton error = EINVAL; 1243b38ff370SJamie Gritton goto done_free; 12440304c731SJamie Gritton } else { 12456a3f2779SJamie Gritton ch_flags |= PR_IP4_USER; 12466a3f2779SJamie Gritton pr_flags |= PR_IP4_USER; 12476a3f2779SJamie Gritton if (ip4s > 0) { 1248eb8dcdeaSGleb Smirnoff ip4s /= sizeof(struct in_addr); 1249b38ff370SJamie Gritton if (ip4s > jail_max_af_ips) { 1250b38ff370SJamie Gritton error = EINVAL; 1251b38ff370SJamie Gritton vfs_opterror(opts, "too many IPv4 addresses"); 1252b38ff370SJamie Gritton goto done_errmsg; 1253b38ff370SJamie Gritton } 1254eb8dcdeaSGleb Smirnoff ip4 = prison_ip_copyin(PR_INET, op, ip4s); 1255eb8dcdeaSGleb Smirnoff if (ip4 == NULL) { 1256b38ff370SJamie Gritton error = EINVAL; 1257b38ff370SJamie Gritton goto done_free; 1258b38ff370SJamie Gritton } 1259b38ff370SJamie Gritton } 12600304c731SJamie Gritton } 1261b38ff370SJamie Gritton #endif 1262b38ff370SJamie Gritton 1263b38ff370SJamie Gritton #ifdef INET6 1264b38ff370SJamie Gritton error = vfs_getopt(opts, "ip6.addr", &op, &ip6s); 1265b38ff370SJamie Gritton if (error == ENOENT) 12660e5e396eSJamie Gritton ip6s = 0; 1267b38ff370SJamie Gritton else if (error != 0) 1268b38ff370SJamie Gritton goto done_free; 1269eb8dcdeaSGleb Smirnoff else if (ip6s & (sizeof(struct in6_addr) - 1)) { 1270b38ff370SJamie Gritton error = EINVAL; 1271b38ff370SJamie Gritton goto done_free; 12720304c731SJamie Gritton } else { 12736a3f2779SJamie Gritton ch_flags |= PR_IP6_USER; 12746a3f2779SJamie Gritton pr_flags |= PR_IP6_USER; 12756a3f2779SJamie Gritton if (ip6s > 0) { 1276eb8dcdeaSGleb Smirnoff ip6s /= sizeof(struct in6_addr); 1277b38ff370SJamie Gritton if (ip6s > jail_max_af_ips) { 1278b38ff370SJamie Gritton error = EINVAL; 1279b38ff370SJamie Gritton vfs_opterror(opts, "too many IPv6 addresses"); 1280b38ff370SJamie Gritton goto done_errmsg; 1281b38ff370SJamie Gritton } 1282eb8dcdeaSGleb Smirnoff ip6 = prison_ip_copyin(PR_INET6, op, ip6s); 1283eb8dcdeaSGleb Smirnoff if (ip6 == NULL) { 1284b38ff370SJamie Gritton error = EINVAL; 1285b38ff370SJamie Gritton goto done_free; 1286b38ff370SJamie Gritton } 1287b38ff370SJamie Gritton } 12880304c731SJamie Gritton } 1289b38ff370SJamie Gritton #endif 1290b38ff370SJamie Gritton 1291bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6)) 1292bdfc8cc4SJamie Gritton if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 1293bdfc8cc4SJamie Gritton error = EINVAL; 1294bdfc8cc4SJamie Gritton vfs_opterror(opts, 1295bdfc8cc4SJamie Gritton "vnet jails cannot have IP address restrictions"); 1296bdfc8cc4SJamie Gritton goto done_errmsg; 1297bdfc8cc4SJamie Gritton } 1298bdfc8cc4SJamie Gritton #endif 1299bdfc8cc4SJamie Gritton 1300cf0313c6SJamie Gritton error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len); 1301cf0313c6SJamie Gritton if (error == ENOENT) 1302cf0313c6SJamie Gritton osrelstr = NULL; 1303cf0313c6SJamie Gritton else if (error != 0) 1304cf0313c6SJamie Gritton goto done_free; 1305cf0313c6SJamie Gritton else { 1306cf0313c6SJamie Gritton if (flags & JAIL_UPDATE) { 1307cf0313c6SJamie Gritton error = EINVAL; 1308cf0313c6SJamie Gritton vfs_opterror(opts, 1309cf0313c6SJamie Gritton "osrelease cannot be changed after creation"); 1310cf0313c6SJamie Gritton goto done_errmsg; 1311cf0313c6SJamie Gritton } 13121b786d01SBjoern A. Zeeb if (len == 0 || osrelstr[len - 1] != '\0') { 1313cf0313c6SJamie Gritton error = EINVAL; 13141b786d01SBjoern A. Zeeb goto done_free; 13151b786d01SBjoern A. Zeeb } 13161b786d01SBjoern A. Zeeb if (len >= OSRELEASELEN) { 13171b786d01SBjoern A. Zeeb error = ENAMETOOLONG; 1318cf0313c6SJamie Gritton vfs_opterror(opts, 1319cf0313c6SJamie Gritton "osrelease string must be 1-%d bytes long", 1320cf0313c6SJamie Gritton OSRELEASELEN - 1); 1321cf0313c6SJamie Gritton goto done_errmsg; 1322cf0313c6SJamie Gritton } 1323cf0313c6SJamie Gritton } 1324cf0313c6SJamie Gritton 1325cf0313c6SJamie Gritton error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt)); 1326cf0313c6SJamie Gritton if (error == ENOENT) 1327cf0313c6SJamie Gritton osreldt = 0; 1328cf0313c6SJamie Gritton else if (error != 0) 1329cf0313c6SJamie Gritton goto done_free; 1330cf0313c6SJamie Gritton else { 1331cf0313c6SJamie Gritton if (flags & JAIL_UPDATE) { 1332cf0313c6SJamie Gritton error = EINVAL; 1333cf0313c6SJamie Gritton vfs_opterror(opts, 1334cf0313c6SJamie Gritton "osreldate cannot be changed after creation"); 1335cf0313c6SJamie Gritton goto done_errmsg; 1336cf0313c6SJamie Gritton } 1337cf0313c6SJamie Gritton if (osreldt == 0) { 1338cf0313c6SJamie Gritton error = EINVAL; 1339cf0313c6SJamie Gritton vfs_opterror(opts, "osreldate cannot be 0"); 1340cf0313c6SJamie Gritton goto done_errmsg; 1341cf0313c6SJamie Gritton } 1342cf0313c6SJamie Gritton } 1343cf0313c6SJamie Gritton 1344b38ff370SJamie Gritton root = NULL; 1345b38ff370SJamie Gritton error = vfs_getopt(opts, "path", (void **)&path, &len); 1346b38ff370SJamie Gritton if (error == ENOENT) 1347b38ff370SJamie Gritton path = NULL; 1348b38ff370SJamie Gritton else if (error != 0) 1349b38ff370SJamie Gritton goto done_free; 1350b38ff370SJamie Gritton else { 1351b38ff370SJamie Gritton if (flags & JAIL_UPDATE) { 1352b38ff370SJamie Gritton error = EINVAL; 1353b38ff370SJamie Gritton vfs_opterror(opts, 1354b38ff370SJamie Gritton "path cannot be changed after creation"); 1355b38ff370SJamie Gritton goto done_errmsg; 1356b38ff370SJamie Gritton } 1357b38ff370SJamie Gritton if (len == 0 || path[len - 1] != '\0') { 1358b38ff370SJamie Gritton error = EINVAL; 1359b38ff370SJamie Gritton goto done_free; 1360b38ff370SJamie Gritton } 13617e1d3eefSMateusz Guzik NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path); 1362b38ff370SJamie Gritton error = namei(&nd); 1363b38ff370SJamie Gritton if (error) 1364b38ff370SJamie Gritton goto done_free; 1365b38ff370SJamie Gritton root = nd.ni_vp; 1366bb92cd7bSMateusz Guzik NDFREE_PNBUF(&nd); 13676dfe0a3dSMartin Matuska g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 13686dfe0a3dSMartin Matuska strlcpy(g_path, path, MAXPATHLEN); 13696dfe0a3dSMartin Matuska error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN); 13703eb6b656SMateusz Guzik if (error == 0) { 13716dfe0a3dSMartin Matuska path = g_path; 13726dfe0a3dSMartin Matuska } else { 1373f6e633a9SMartin Matuska /* exit on other errors */ 1374b38ff370SJamie Gritton goto done_free; 1375b38ff370SJamie Gritton } 1376f6e633a9SMartin Matuska if (root->v_type != VDIR) { 1377f6e633a9SMartin Matuska error = ENOTDIR; 1378f6e633a9SMartin Matuska vput(root); 1379f6e633a9SMartin Matuska goto done_free; 1380f6e633a9SMartin Matuska } 1381b249ce48SMateusz Guzik VOP_UNLOCK(root); 1382b38ff370SJamie Gritton } 1383b38ff370SJamie Gritton 1384b38ff370SJamie Gritton /* 1385b6f47c23SJamie Gritton * Find the specified jail, or at least its parent. 1386b38ff370SJamie Gritton * This abuses the file error codes ENOENT and EEXIST. 1387b38ff370SJamie Gritton */ 1388b38ff370SJamie Gritton pr = NULL; 13897de883c8SJamie Gritton inspr = NULL; 1390babbbb9cSJamie Gritton if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) { 1391babbbb9cSJamie Gritton namelc = strrchr(name, '.'); 1392babbbb9cSJamie Gritton jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10); 1393babbbb9cSJamie Gritton if (*p != '\0') 1394babbbb9cSJamie Gritton jid = 0; 1395babbbb9cSJamie Gritton } 1396b6f47c23SJamie Gritton sx_xlock(&allprison_lock); 1397108a9384SJamie Gritton drflags = PD_LIST_XLOCKED; 13980a2a96f3SJamie Gritton ppr = mypr; 13990a2a96f3SJamie Gritton if (!prison_isalive(ppr)) { 14000a2a96f3SJamie Gritton /* This jail is dying. This process will surely follow. */ 14010a2a96f3SJamie Gritton error = EAGAIN; 14020a2a96f3SJamie Gritton goto done_deref; 14030a2a96f3SJamie Gritton } 1404b38ff370SJamie Gritton if (jid != 0) { 1405b38ff370SJamie Gritton if (jid < 0) { 1406b38ff370SJamie Gritton error = EINVAL; 1407b38ff370SJamie Gritton vfs_opterror(opts, "negative jid"); 14082a4b2251SJamie Gritton goto done_deref; 1409b38ff370SJamie Gritton } 14107de883c8SJamie Gritton /* 14117de883c8SJamie Gritton * See if a requested jid already exists. Keep track of 14127de883c8SJamie Gritton * where it can be inserted later. 14137de883c8SJamie Gritton */ 14147de883c8SJamie Gritton TAILQ_FOREACH(inspr, &allprison, pr_list) { 1415f7496dcaSJamie Gritton if (inspr->pr_id < jid) 1416f7496dcaSJamie Gritton continue; 1417f7496dcaSJamie Gritton if (inspr->pr_id > jid) 1418f7496dcaSJamie Gritton break; 14197de883c8SJamie Gritton pr = inspr; 1420f7496dcaSJamie Gritton mtx_lock(&pr->pr_mtx); 14212a4b2251SJamie Gritton drflags |= PD_LOCKED; 14227de883c8SJamie Gritton inspr = NULL; 14237de883c8SJamie Gritton break; 14247de883c8SJamie Gritton } 1425b38ff370SJamie Gritton if (pr != NULL) { 1426b38ff370SJamie Gritton /* Create: jid must not exist. */ 1427b38ff370SJamie Gritton if (cuflags == JAIL_CREATE) { 14287de883c8SJamie Gritton /* 14297de883c8SJamie Gritton * Even creators that cannot see the jail will 14307de883c8SJamie Gritton * get EEXIST. 14317de883c8SJamie Gritton */ 1432b38ff370SJamie Gritton error = EEXIST; 1433b38ff370SJamie Gritton vfs_opterror(opts, "jail %d already exists", 1434b38ff370SJamie Gritton jid); 14352a4b2251SJamie Gritton goto done_deref; 1436b38ff370SJamie Gritton } 14370304c731SJamie Gritton if (!prison_ischild(mypr, pr)) { 14387de883c8SJamie Gritton /* 14397de883c8SJamie Gritton * Updaters get ENOENT if they cannot see the 14407de883c8SJamie Gritton * jail. This is true even for CREATE | UPDATE, 14417de883c8SJamie Gritton * which normally cannot give this error. 14427de883c8SJamie Gritton */ 14432a4b2251SJamie Gritton error = ENOENT; 14442a4b2251SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 14452a4b2251SJamie Gritton goto done_deref; 1446f7496dcaSJamie Gritton } 14470a2a96f3SJamie Gritton ppr = pr->pr_parent; 14480a2a96f3SJamie Gritton if (!prison_isalive(ppr)) { 14490a2a96f3SJamie Gritton error = ENOENT; 14500a2a96f3SJamie Gritton vfs_opterror(opts, "jail %d is dying", 14510a2a96f3SJamie Gritton ppr->pr_id); 14520a2a96f3SJamie Gritton goto done_deref; 14530a2a96f3SJamie Gritton } 1454f7496dcaSJamie Gritton if (!prison_isalive(pr)) { 1455b38ff370SJamie Gritton if (!(flags & JAIL_DYING)) { 1456b38ff370SJamie Gritton error = ENOENT; 1457b38ff370SJamie Gritton vfs_opterror(opts, "jail %d is dying", 1458b38ff370SJamie Gritton jid); 14592a4b2251SJamie Gritton goto done_deref; 1460f7496dcaSJamie Gritton } 1461f7496dcaSJamie Gritton if ((flags & JAIL_ATTACH) || 1462b38ff370SJamie Gritton (pr_flags & PR_PERSIST)) { 1463b38ff370SJamie Gritton /* 1464b38ff370SJamie Gritton * A dying jail might be resurrected 1465b38ff370SJamie Gritton * (via attach or persist), but first 1466b38ff370SJamie Gritton * it must determine if another jail 1467b38ff370SJamie Gritton * has claimed its name. Accomplish 1468b38ff370SJamie Gritton * this by implicitly re-setting the 1469b38ff370SJamie Gritton * name. 1470b38ff370SJamie Gritton */ 1471b38ff370SJamie Gritton if (name == NULL) 14720304c731SJamie Gritton name = prison_name(mypr, pr); 1473b38ff370SJamie Gritton } 1474b38ff370SJamie Gritton } 14752a4b2251SJamie Gritton } else { 1476b38ff370SJamie Gritton /* Update: jid must exist. */ 1477b38ff370SJamie Gritton if (cuflags == JAIL_UPDATE) { 1478b38ff370SJamie Gritton error = ENOENT; 1479b38ff370SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 14802a4b2251SJamie Gritton goto done_deref; 1481b38ff370SJamie Gritton } 1482b38ff370SJamie Gritton } 1483b38ff370SJamie Gritton } 1484b38ff370SJamie Gritton /* 1485b38ff370SJamie Gritton * If the caller provided a name, look for a jail by that name. 1486b38ff370SJamie Gritton * This has different semantics for creates and updates keyed by jid 1487b38ff370SJamie Gritton * (where the name must not already exist in a different jail), 1488b38ff370SJamie Gritton * and updates keyed by the name itself (where the name must exist 1489b38ff370SJamie Gritton * because that is the jail being updated). 1490b38ff370SJamie Gritton */ 1491b6f47c23SJamie Gritton namelc = NULL; 1492b38ff370SJamie Gritton if (name != NULL) { 1493babbbb9cSJamie Gritton namelc = strrchr(name, '.'); 1494babbbb9cSJamie Gritton if (namelc == NULL) 1495babbbb9cSJamie Gritton namelc = name; 1496babbbb9cSJamie Gritton else { 14970304c731SJamie Gritton /* 14980304c731SJamie Gritton * This is a hierarchical name. Split it into the 14990304c731SJamie Gritton * parent and child names, and make sure the parent 15000304c731SJamie Gritton * exists or matches an already found jail. 15010304c731SJamie Gritton */ 15020304c731SJamie Gritton if (pr != NULL) { 1503babbbb9cSJamie Gritton if (strncmp(name, ppr->pr_name, namelc - name) 1504babbbb9cSJamie Gritton || ppr->pr_name[namelc - name] != '\0') { 15050304c731SJamie Gritton error = EINVAL; 15060304c731SJamie Gritton vfs_opterror(opts, 15070304c731SJamie Gritton "cannot change jail's parent"); 15082a4b2251SJamie Gritton goto done_deref; 15090304c731SJamie Gritton } 15100304c731SJamie Gritton } else { 1511b6f47c23SJamie Gritton *namelc = '\0'; 15120304c731SJamie Gritton ppr = prison_find_name(mypr, name); 15130304c731SJamie Gritton if (ppr == NULL) { 15140304c731SJamie Gritton error = ENOENT; 15150304c731SJamie Gritton vfs_opterror(opts, 15160304c731SJamie Gritton "jail \"%s\" not found", name); 15172a4b2251SJamie Gritton goto done_deref; 15180304c731SJamie Gritton } 1519c050ea80SJamie Gritton mtx_unlock(&ppr->pr_mtx); 15200a2a96f3SJamie Gritton if (!prison_isalive(ppr)) { 1521c050ea80SJamie Gritton error = ENOENT; 1522c050ea80SJamie Gritton vfs_opterror(opts, 1523c050ea80SJamie Gritton "jail \"%s\" is dying", name); 1524c050ea80SJamie Gritton goto done_deref; 1525c050ea80SJamie Gritton } 1526b6f47c23SJamie Gritton *namelc = '.'; 15270304c731SJamie Gritton } 1528b6f47c23SJamie Gritton namelc++; 15290304c731SJamie Gritton } 1530b6f47c23SJamie Gritton if (namelc[0] != '\0') { 1531b6f47c23SJamie Gritton pnamelen = 15320304c731SJamie Gritton (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 15330304c731SJamie Gritton deadpr = NULL; 15340304c731SJamie Gritton FOREACH_PRISON_CHILD(ppr, tpr) { 15356754ae25SJamie Gritton if (tpr != pr && 1536b6f47c23SJamie Gritton !strcmp(tpr->pr_name + pnamelen, namelc)) { 153776ad42abSJamie Gritton if (prison_isalive(tpr)) { 1538b38ff370SJamie Gritton if (pr == NULL && 1539b38ff370SJamie Gritton cuflags != JAIL_CREATE) { 1540b38ff370SJamie Gritton /* 1541b38ff370SJamie Gritton * Use this jail 1542b38ff370SJamie Gritton * for updates. 1543b38ff370SJamie Gritton */ 1544b38ff370SJamie Gritton pr = tpr; 1545f7496dcaSJamie Gritton mtx_lock(&pr->pr_mtx); 154683bc72a0SJamie Gritton drflags |= PD_LOCKED; 1547b38ff370SJamie Gritton break; 1548b38ff370SJamie Gritton } 1549b38ff370SJamie Gritton /* 1550b38ff370SJamie Gritton * Create, or update(jid): 1551b38ff370SJamie Gritton * name must not exist in an 15520304c731SJamie Gritton * active sibling jail. 1553b38ff370SJamie Gritton */ 1554b38ff370SJamie Gritton error = EEXIST; 1555b38ff370SJamie Gritton vfs_opterror(opts, 1556b38ff370SJamie Gritton "jail \"%s\" already exists", 1557b38ff370SJamie Gritton name); 15582a4b2251SJamie Gritton goto done_deref; 1559b38ff370SJamie Gritton } 156076ad42abSJamie Gritton if (pr == NULL && 1561f7496dcaSJamie Gritton cuflags != JAIL_CREATE) { 156276ad42abSJamie Gritton deadpr = tpr; 1563f7496dcaSJamie Gritton } 1564b38ff370SJamie Gritton } 1565b38ff370SJamie Gritton } 1566b38ff370SJamie Gritton /* If no active jail is found, use a dying one. */ 1567b38ff370SJamie Gritton if (deadpr != NULL && pr == NULL) { 1568b38ff370SJamie Gritton if (flags & JAIL_DYING) { 1569b38ff370SJamie Gritton pr = deadpr; 1570f7496dcaSJamie Gritton mtx_lock(&pr->pr_mtx); 15712a4b2251SJamie Gritton drflags |= PD_LOCKED; 1572b38ff370SJamie Gritton } else if (cuflags == JAIL_UPDATE) { 1573b38ff370SJamie Gritton error = ENOENT; 1574b38ff370SJamie Gritton vfs_opterror(opts, 1575b38ff370SJamie Gritton "jail \"%s\" is dying", name); 15762a4b2251SJamie Gritton goto done_deref; 1577b38ff370SJamie Gritton } 1578b38ff370SJamie Gritton } 1579b38ff370SJamie Gritton /* Update: name must exist if no jid. */ 1580b38ff370SJamie Gritton else if (cuflags == JAIL_UPDATE && pr == NULL) { 1581b38ff370SJamie Gritton error = ENOENT; 1582b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", 1583b38ff370SJamie Gritton name); 15842a4b2251SJamie Gritton goto done_deref; 1585b38ff370SJamie Gritton } 1586b38ff370SJamie Gritton } 1587b38ff370SJamie Gritton } 1588b38ff370SJamie Gritton /* Update: must provide a jid or name. */ 1589b38ff370SJamie Gritton else if (cuflags == JAIL_UPDATE && pr == NULL) { 1590b38ff370SJamie Gritton error = ENOENT; 1591b38ff370SJamie Gritton vfs_opterror(opts, "update specified no jail"); 15922a4b2251SJamie Gritton goto done_deref; 1593b38ff370SJamie Gritton } 1594b38ff370SJamie Gritton 1595b38ff370SJamie Gritton /* If there's no prison to update, create a new one and link it in. */ 15962a4b2251SJamie Gritton created = pr == NULL; 15972a4b2251SJamie Gritton if (created) { 1598b97457e2SJamie Gritton for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent) 1599b97457e2SJamie Gritton if (tpr->pr_childcount >= tpr->pr_childmax) { 1600b97457e2SJamie Gritton error = EPERM; 1601b97457e2SJamie Gritton vfs_opterror(opts, "prison limit exceeded"); 16022a4b2251SJamie Gritton goto done_deref; 1603b97457e2SJamie Gritton } 16047de883c8SJamie Gritton if (jid == 0 && (jid = get_next_prid(&inspr)) == 0) { 1605b38ff370SJamie Gritton error = EAGAIN; 16067de883c8SJamie Gritton vfs_opterror(opts, "no available jail IDs"); 16072a4b2251SJamie Gritton goto done_deref; 1608b38ff370SJamie Gritton } 16092a4b2251SJamie Gritton 16102a4b2251SJamie Gritton pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO); 16111158508aSJamie Gritton pr->pr_state = PRISON_STATE_INVALID; 16121158508aSJamie Gritton refcount_init(&pr->pr_ref, 1); 1613f7496dcaSJamie Gritton refcount_init(&pr->pr_uref, 0); 16141158508aSJamie Gritton drflags |= PD_DEREF; 16152a4b2251SJamie Gritton LIST_INIT(&pr->pr_children); 16162a4b2251SJamie Gritton mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK); 16172a4b2251SJamie Gritton TASK_INIT(&pr->pr_task, 0, prison_complete, pr); 16182a4b2251SJamie Gritton 16197de883c8SJamie Gritton pr->pr_id = jid; 16207de883c8SJamie Gritton if (inspr != NULL) 16217de883c8SJamie Gritton TAILQ_INSERT_BEFORE(inspr, pr, pr_list); 16227de883c8SJamie Gritton else 1623b38ff370SJamie Gritton TAILQ_INSERT_TAIL(&allprison, pr, pr_list); 16247de883c8SJamie Gritton 16257de883c8SJamie Gritton pr->pr_parent = ppr; 16260a2a96f3SJamie Gritton prison_hold(ppr); 16270a2a96f3SJamie Gritton prison_proc_hold(ppr); 16280304c731SJamie Gritton LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling); 16290304c731SJamie Gritton for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 1630b97457e2SJamie Gritton tpr->pr_childcount++; 1631b38ff370SJamie Gritton 16320304c731SJamie Gritton /* Set some default values, and inherit some from the parent. */ 1633b6f47c23SJamie Gritton if (namelc == NULL) 1634b6f47c23SJamie Gritton namelc = ""; 1635b38ff370SJamie Gritton if (path == NULL) { 1636b38ff370SJamie Gritton path = "/"; 16370304c731SJamie Gritton root = mypr->pr_root; 1638b38ff370SJamie Gritton vref(root); 1639b38ff370SJamie Gritton } 16408986e3a0SJamie Gritton strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN); 16418986e3a0SJamie Gritton pr->pr_flags |= PR_HOST; 1642bdfc8cc4SJamie Gritton #if defined(INET) || defined(INET6) 1643bdfc8cc4SJamie Gritton #ifdef VIMAGE 1644bdfc8cc4SJamie Gritton if (!(pr_flags & PR_VNET)) 1645bdfc8cc4SJamie Gritton #endif 1646bdfc8cc4SJamie Gritton { 16470304c731SJamie Gritton #ifdef INET 16482b0d6f81SJamie Gritton if (!(ch_flags & PR_IP4_USER)) 16496a3f2779SJamie Gritton pr->pr_flags |= PR_IP4 | PR_IP4_USER; 16502b0d6f81SJamie Gritton else if (!(pr_flags & PR_IP4_USER)) { 16512b0d6f81SJamie Gritton pr->pr_flags |= ppr->pr_flags & PR_IP4; 1652eb8dcdeaSGleb Smirnoff prison_ip_dup(ppr, pr, PR_INET); 16532b0d6f81SJamie Gritton } 16540304c731SJamie Gritton #endif 16550304c731SJamie Gritton #ifdef INET6 16562b0d6f81SJamie Gritton if (!(ch_flags & PR_IP6_USER)) 16576a3f2779SJamie Gritton pr->pr_flags |= PR_IP6 | PR_IP6_USER; 16582b0d6f81SJamie Gritton else if (!(pr_flags & PR_IP6_USER)) { 16592b0d6f81SJamie Gritton pr->pr_flags |= ppr->pr_flags & PR_IP6; 1660eb8dcdeaSGleb Smirnoff prison_ip_dup(ppr, pr, PR_INET6); 16612b0d6f81SJamie Gritton } 16620304c731SJamie Gritton #endif 1663bdfc8cc4SJamie Gritton } 1664bdfc8cc4SJamie Gritton #endif 1665592bcae8SBjoern A. Zeeb /* Source address selection is always on by default. */ 1666592bcae8SBjoern A. Zeeb pr->pr_flags |= _PR_IP_SADDRSEL; 1667592bcae8SBjoern A. Zeeb 16680304c731SJamie Gritton pr->pr_securelevel = ppr->pr_securelevel; 16690304c731SJamie Gritton pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow; 1670af10bf05SBjoern A. Zeeb pr->pr_enforce_statfs = jail_default_enforce_statfs; 1671bf3db8aaSMartin Matuska pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum; 1672b38ff370SJamie Gritton 1673b96bd95bSIan Lepore pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate; 1674b96bd95bSIan Lepore if (osrelstr == NULL) 16751b786d01SBjoern A. Zeeb strlcpy(pr->pr_osrelease, ppr->pr_osrelease, 16761b786d01SBjoern A. Zeeb sizeof(pr->pr_osrelease)); 1677b96bd95bSIan Lepore else 16781b786d01SBjoern A. Zeeb strlcpy(pr->pr_osrelease, osrelstr, 16791b786d01SBjoern A. Zeeb sizeof(pr->pr_osrelease)); 1680b96bd95bSIan Lepore 1681679e1390SJamie Gritton #ifdef VIMAGE 1682679e1390SJamie Gritton /* Allocate a new vnet if specified. */ 1683679e1390SJamie Gritton pr->pr_vnet = (pr_flags & PR_VNET) 1684679e1390SJamie Gritton ? vnet_alloc() : ppr->pr_vnet; 1685679e1390SJamie Gritton #endif 1686b38ff370SJamie Gritton /* 1687b38ff370SJamie Gritton * Allocate a dedicated cpuset for each jail. 16888771ff75SGordon Bergling * Unlike other initial settings, this may return an error. 1689b38ff370SJamie Gritton */ 16900304c731SJamie Gritton error = cpuset_create_root(ppr, &pr->pr_cpuset); 16912a4b2251SJamie Gritton if (error) 16922a4b2251SJamie Gritton goto done_deref; 1693b38ff370SJamie Gritton 1694b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 16952a4b2251SJamie Gritton drflags |= PD_LOCKED; 1696b38ff370SJamie Gritton } else { 16972b0d6f81SJamie Gritton /* 16982b0d6f81SJamie Gritton * Grab a reference for existing prisons, to ensure they 16992b0d6f81SJamie Gritton * continue to exist for the duration of the call. 17002b0d6f81SJamie Gritton */ 17016754ae25SJamie Gritton prison_hold(pr); 17022a4b2251SJamie Gritton drflags |= PD_DEREF; 1703bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6)) 1704bdfc8cc4SJamie Gritton if ((pr->pr_flags & PR_VNET) && 1705bdfc8cc4SJamie Gritton (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 1706bdfc8cc4SJamie Gritton error = EINVAL; 1707bdfc8cc4SJamie Gritton vfs_opterror(opts, 1708bdfc8cc4SJamie Gritton "vnet jails cannot have IP address restrictions"); 17092a4b2251SJamie Gritton goto done_deref; 1710bdfc8cc4SJamie Gritton } 1711bdfc8cc4SJamie Gritton #endif 17122b0d6f81SJamie Gritton #ifdef INET 17132b0d6f81SJamie Gritton if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 17142b0d6f81SJamie Gritton error = EINVAL; 17152b0d6f81SJamie Gritton vfs_opterror(opts, 17162b0d6f81SJamie Gritton "ip4 cannot be changed after creation"); 17172a4b2251SJamie Gritton goto done_deref; 17182b0d6f81SJamie Gritton } 17192b0d6f81SJamie Gritton #endif 17202b0d6f81SJamie Gritton #ifdef INET6 17212b0d6f81SJamie Gritton if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 17222b0d6f81SJamie Gritton error = EINVAL; 17232b0d6f81SJamie Gritton vfs_opterror(opts, 17242b0d6f81SJamie Gritton "ip6 cannot be changed after creation"); 17252a4b2251SJamie Gritton goto done_deref; 17262b0d6f81SJamie Gritton } 17272b0d6f81SJamie Gritton #endif 1728b38ff370SJamie Gritton } 1729b38ff370SJamie Gritton 1730b38ff370SJamie Gritton /* Do final error checking before setting anything. */ 17310304c731SJamie Gritton if (gotslevel) { 17320304c731SJamie Gritton if (slevel < ppr->pr_securelevel) { 17330304c731SJamie Gritton error = EPERM; 17342a4b2251SJamie Gritton goto done_deref; 17350304c731SJamie Gritton } 17360304c731SJamie Gritton } 1737b97457e2SJamie Gritton if (gotchildmax) { 1738b97457e2SJamie Gritton if (childmax >= ppr->pr_childmax) { 1739b97457e2SJamie Gritton error = EPERM; 17402a4b2251SJamie Gritton goto done_deref; 1741b97457e2SJamie Gritton } 1742b97457e2SJamie Gritton } 17430304c731SJamie Gritton if (gotenforce) { 17440304c731SJamie Gritton if (enforce < ppr->pr_enforce_statfs) { 17450304c731SJamie Gritton error = EPERM; 17462a4b2251SJamie Gritton goto done_deref; 17470304c731SJamie Gritton } 17480304c731SJamie Gritton } 17490cc207a6SMartin Matuska if (gotrsnum) { 17500cc207a6SMartin Matuska /* 17510cc207a6SMartin Matuska * devfs_rsnum is a uint16_t 17520cc207a6SMartin Matuska */ 1753bf3db8aaSMartin Matuska if (rsnum < 0 || rsnum > 65535) { 17540cc207a6SMartin Matuska error = EINVAL; 17552a4b2251SJamie Gritton goto done_deref; 17560cc207a6SMartin Matuska } 17570cc207a6SMartin Matuska /* 1758bf3db8aaSMartin Matuska * Nested jails always inherit parent's devfs ruleset 17590cc207a6SMartin Matuska */ 17600cc207a6SMartin Matuska if (jailed(td->td_ucred)) { 17610cc207a6SMartin Matuska if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) { 17620cc207a6SMartin Matuska error = EPERM; 17632a4b2251SJamie Gritton goto done_deref; 1764bf3db8aaSMartin Matuska } else 17650cc207a6SMartin Matuska rsnum = ppr->pr_devfs_rsnum; 17660cc207a6SMartin Matuska } 17670cc207a6SMartin Matuska } 1768b38ff370SJamie Gritton #ifdef INET 17692b0d6f81SJamie Gritton if (ip4s > 0) { 1770eb8dcdeaSGleb Smirnoff if ((ppr->pr_flags & PR_IP4) && 1771eb8dcdeaSGleb Smirnoff !prison_ip_parent_match(ppr->pr_addrs[PR_INET], ip4, 1772eb8dcdeaSGleb Smirnoff PR_INET)) { 17730304c731SJamie Gritton error = EPERM; 17742a4b2251SJamie Gritton goto done_deref; 17750304c731SJamie Gritton } 1776eb8dcdeaSGleb Smirnoff if (!prison_ip_conflict_check(ppr, pr, ip4, PR_INET)) { 17770304c731SJamie Gritton error = EADDRINUSE; 1778eb8dcdeaSGleb Smirnoff vfs_opterror(opts, "IPv4 addresses clash"); 17792a4b2251SJamie Gritton goto done_deref; 1780b38ff370SJamie Gritton } 17810304c731SJamie Gritton } 1782b38ff370SJamie Gritton #endif 1783b38ff370SJamie Gritton #ifdef INET6 17842b0d6f81SJamie Gritton if (ip6s > 0) { 1785eb8dcdeaSGleb Smirnoff if ((ppr->pr_flags & PR_IP6) && 1786eb8dcdeaSGleb Smirnoff !prison_ip_parent_match(ppr->pr_addrs[PR_INET6], ip6, 1787eb8dcdeaSGleb Smirnoff PR_INET6)) { 17880304c731SJamie Gritton error = EPERM; 17892a4b2251SJamie Gritton goto done_deref; 17900304c731SJamie Gritton } 1791eb8dcdeaSGleb Smirnoff if (!prison_ip_conflict_check(ppr, pr, ip6, PR_INET6)) { 17920304c731SJamie Gritton error = EADDRINUSE; 1793eb8dcdeaSGleb Smirnoff vfs_opterror(opts, "IPv6 addresses clash"); 17942a4b2251SJamie Gritton goto done_deref; 1795b38ff370SJamie Gritton } 17960304c731SJamie Gritton } 1797b38ff370SJamie Gritton #endif 17980304c731SJamie Gritton onamelen = namelen = 0; 1799b6f47c23SJamie Gritton if (namelc != NULL) { 18006ab6058eSJamie Gritton /* Give a default name of the jid. Also allow the name to be 18016ab6058eSJamie Gritton * explicitly the jid - but not any other number, and only in 18026ab6058eSJamie Gritton * normal form (no leading zero/etc). 18036ab6058eSJamie Gritton */ 1804b6f47c23SJamie Gritton if (namelc[0] == '\0') 1805b6f47c23SJamie Gritton snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid); 18066ab6058eSJamie Gritton else if ((strtoul(namelc, &p, 10) != jid || 18076ab6058eSJamie Gritton namelc[0] < '1' || namelc[0] > '9') && *p == '\0') { 1808b38ff370SJamie Gritton error = EINVAL; 1809babbbb9cSJamie Gritton vfs_opterror(opts, 1810babbbb9cSJamie Gritton "name cannot be numeric (unless it is the jid)"); 18112a4b2251SJamie Gritton goto done_deref; 1812b38ff370SJamie Gritton } 1813b38ff370SJamie Gritton /* 18140304c731SJamie Gritton * Make sure the name isn't too long for the prison or its 18150304c731SJamie Gritton * children. 1816b38ff370SJamie Gritton */ 1817b6f47c23SJamie Gritton pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 1818b6f47c23SJamie Gritton onamelen = strlen(pr->pr_name + pnamelen); 1819b6f47c23SJamie Gritton namelen = strlen(namelc); 1820b6f47c23SJamie Gritton if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) { 18210304c731SJamie Gritton error = ENAMETOOLONG; 18222a4b2251SJamie Gritton goto done_deref; 18230304c731SJamie Gritton } 18240304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 18250304c731SJamie Gritton if (strlen(tpr->pr_name) + (namelen - onamelen) >= 18260304c731SJamie Gritton sizeof(pr->pr_name)) { 18270304c731SJamie Gritton error = ENAMETOOLONG; 18282a4b2251SJamie Gritton goto done_deref; 18290304c731SJamie Gritton } 18300304c731SJamie Gritton } 18310304c731SJamie Gritton } 1832b3079544SJamie Gritton pr_allow_diff = pr_allow & ~ppr->pr_allow; 1833b3079544SJamie Gritton if (pr_allow_diff & ~PR_ALLOW_DIFFERENCES) { 18340304c731SJamie Gritton error = EPERM; 18352a4b2251SJamie Gritton goto done_deref; 1836b38ff370SJamie Gritton } 1837b38ff370SJamie Gritton 1838b6f47c23SJamie Gritton /* 1839b6f47c23SJamie Gritton * Let modules check their parameters. This requires unlocking and 1840b6f47c23SJamie Gritton * then re-locking the prison, but this is still a valid state as long 1841b6f47c23SJamie Gritton * as allprison_lock remains xlocked. 1842b6f47c23SJamie Gritton */ 1843b6f47c23SJamie Gritton mtx_unlock(&pr->pr_mtx); 18442a4b2251SJamie Gritton drflags &= ~PD_LOCKED; 1845b6f47c23SJamie Gritton error = osd_jail_call(pr, PR_METHOD_CHECK, opts); 18462a4b2251SJamie Gritton if (error != 0) 18472a4b2251SJamie Gritton goto done_deref; 1848b6f47c23SJamie Gritton mtx_lock(&pr->pr_mtx); 18492a4b2251SJamie Gritton drflags |= PD_LOCKED; 1850b6f47c23SJamie Gritton 1851b6f47c23SJamie Gritton /* At this point, all valid parameters should have been noted. */ 1852b6f47c23SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 1853b6f47c23SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 1854b6f47c23SJamie Gritton error = EINVAL; 1855b6f47c23SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 18562a4b2251SJamie Gritton goto done_deref; 1857b6f47c23SJamie Gritton } 1858b6f47c23SJamie Gritton } 1859b6f47c23SJamie Gritton 1860b38ff370SJamie Gritton /* Set the parameters of the prison. */ 1861b38ff370SJamie Gritton #ifdef INET 18628bce8d28SZhenlei Huang redo_ip4 = false; 18630304c731SJamie Gritton if (pr_flags & PR_IP4_USER) { 18640304c731SJamie Gritton pr->pr_flags |= PR_IP4; 1865eb8dcdeaSGleb Smirnoff prison_ip_set(pr, PR_INET, ip4); 1866b38ff370SJamie Gritton ip4 = NULL; 18670304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1868bdfc8cc4SJamie Gritton #ifdef VIMAGE 1869bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1870bdfc8cc4SJamie Gritton descend = 0; 1871bdfc8cc4SJamie Gritton continue; 1872bdfc8cc4SJamie Gritton } 1873bdfc8cc4SJamie Gritton #endif 18748bce8d28SZhenlei Huang if (!prison_ip_restrict(tpr, PR_INET, NULL)) { 18758bce8d28SZhenlei Huang redo_ip4 = true; 18760304c731SJamie Gritton descend = 0; 18770304c731SJamie Gritton } 18780304c731SJamie Gritton } 18790304c731SJamie Gritton } 1880b38ff370SJamie Gritton #endif 1881b38ff370SJamie Gritton #ifdef INET6 18828bce8d28SZhenlei Huang redo_ip6 = false; 18830304c731SJamie Gritton if (pr_flags & PR_IP6_USER) { 18840304c731SJamie Gritton pr->pr_flags |= PR_IP6; 1885eb8dcdeaSGleb Smirnoff prison_ip_set(pr, PR_INET6, ip6); 1886b38ff370SJamie Gritton ip6 = NULL; 18870304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1888bdfc8cc4SJamie Gritton #ifdef VIMAGE 1889bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1890bdfc8cc4SJamie Gritton descend = 0; 1891bdfc8cc4SJamie Gritton continue; 1892bdfc8cc4SJamie Gritton } 1893bdfc8cc4SJamie Gritton #endif 18948bce8d28SZhenlei Huang if (!prison_ip_restrict(tpr, PR_INET6, NULL)) { 18958bce8d28SZhenlei Huang redo_ip6 = true; 18960304c731SJamie Gritton descend = 0; 18970304c731SJamie Gritton } 18980304c731SJamie Gritton } 18990304c731SJamie Gritton } 1900b38ff370SJamie Gritton #endif 19010304c731SJamie Gritton if (gotslevel) { 1902b38ff370SJamie Gritton pr->pr_securelevel = slevel; 19030304c731SJamie Gritton /* Set all child jails to be at least this level. */ 19040304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 19050304c731SJamie Gritton if (tpr->pr_securelevel < slevel) 19060304c731SJamie Gritton tpr->pr_securelevel = slevel; 19070304c731SJamie Gritton } 1908b97457e2SJamie Gritton if (gotchildmax) { 1909b97457e2SJamie Gritton pr->pr_childmax = childmax; 1910b97457e2SJamie Gritton /* Set all child jails to under this limit. */ 1911b97457e2SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level) 1912b97457e2SJamie Gritton if (tpr->pr_childmax > childmax - level) 1913b97457e2SJamie Gritton tpr->pr_childmax = childmax > level 1914b97457e2SJamie Gritton ? childmax - level : 0; 1915b97457e2SJamie Gritton } 19160304c731SJamie Gritton if (gotenforce) { 19170304c731SJamie Gritton pr->pr_enforce_statfs = enforce; 19180304c731SJamie Gritton /* Pass this restriction on to the children. */ 19190304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 19200304c731SJamie Gritton if (tpr->pr_enforce_statfs < enforce) 19210304c731SJamie Gritton tpr->pr_enforce_statfs = enforce; 19220304c731SJamie Gritton } 19230cc207a6SMartin Matuska if (gotrsnum) { 19240cc207a6SMartin Matuska pr->pr_devfs_rsnum = rsnum; 19250cc207a6SMartin Matuska /* Pass this restriction on to the children. */ 19260cc207a6SMartin Matuska FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 19270cc207a6SMartin Matuska tpr->pr_devfs_rsnum = rsnum; 19280cc207a6SMartin Matuska } 1929b6f47c23SJamie Gritton if (namelc != NULL) { 19300304c731SJamie Gritton if (ppr == &prison0) 1931b6f47c23SJamie Gritton strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name)); 19320304c731SJamie Gritton else 19330304c731SJamie Gritton snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s", 1934b6f47c23SJamie Gritton ppr->pr_name, namelc); 19350304c731SJamie Gritton /* Change this component of child names. */ 19360304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 19370304c731SJamie Gritton bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen, 19380304c731SJamie Gritton strlen(tpr->pr_name + onamelen) + 1); 19390304c731SJamie Gritton bcopy(pr->pr_name, tpr->pr_name, namelen); 19400304c731SJamie Gritton } 19410304c731SJamie Gritton } 1942b38ff370SJamie Gritton if (path != NULL) { 19430304c731SJamie Gritton /* Try to keep a real-rooted full pathname. */ 1944b38ff370SJamie Gritton strlcpy(pr->pr_path, path, sizeof(pr->pr_path)); 1945b38ff370SJamie Gritton pr->pr_root = root; 19462a4b2251SJamie Gritton root = NULL; 1947b38ff370SJamie Gritton } 194876ca6f88SJamie Gritton if (PR_HOST & ch_flags & ~pr_flags) { 194976ca6f88SJamie Gritton if (pr->pr_flags & PR_HOST) { 195076ca6f88SJamie Gritton /* 195176ca6f88SJamie Gritton * Copy the parent's host info. As with pr_ip4 above, 195276ca6f88SJamie Gritton * the lack of a lock on the parent is not a problem; 195376ca6f88SJamie Gritton * it is always set with allprison_lock at least 195476ca6f88SJamie Gritton * shared, and is held exclusively here. 195576ca6f88SJamie Gritton */ 1956c1f19219SJamie Gritton strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname, 1957c1f19219SJamie Gritton sizeof(pr->pr_hostname)); 1958c1f19219SJamie Gritton strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname, 1959c1f19219SJamie Gritton sizeof(pr->pr_domainname)); 1960c1f19219SJamie Gritton strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid, 1961c1f19219SJamie Gritton sizeof(pr->pr_hostuuid)); 196276ca6f88SJamie Gritton pr->pr_hostid = pr->pr_parent->pr_hostid; 196376ca6f88SJamie Gritton } 196476ca6f88SJamie Gritton } else if (host != NULL || domain != NULL || uuid != NULL || gothid) { 196576ca6f88SJamie Gritton /* Set this prison, and any descendants without PR_HOST. */ 1966b38ff370SJamie Gritton if (host != NULL) 1967c1f19219SJamie Gritton strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname)); 196876ca6f88SJamie Gritton if (domain != NULL) 1969c1f19219SJamie Gritton strlcpy(pr->pr_domainname, domain, 1970c1f19219SJamie Gritton sizeof(pr->pr_domainname)); 197176ca6f88SJamie Gritton if (uuid != NULL) 1972c1f19219SJamie Gritton strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid)); 197376ca6f88SJamie Gritton if (gothid) 197476ca6f88SJamie Gritton pr->pr_hostid = hid; 197576ca6f88SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 197676ca6f88SJamie Gritton if (tpr->pr_flags & PR_HOST) 197776ca6f88SJamie Gritton descend = 0; 197876ca6f88SJamie Gritton else { 197976ca6f88SJamie Gritton if (host != NULL) 1980c1f19219SJamie Gritton strlcpy(tpr->pr_hostname, 1981c1f19219SJamie Gritton pr->pr_hostname, 1982c1f19219SJamie Gritton sizeof(tpr->pr_hostname)); 198376ca6f88SJamie Gritton if (domain != NULL) 1984c1f19219SJamie Gritton strlcpy(tpr->pr_domainname, 1985c1f19219SJamie Gritton pr->pr_domainname, 1986c1f19219SJamie Gritton sizeof(tpr->pr_domainname)); 198776ca6f88SJamie Gritton if (uuid != NULL) 1988c1f19219SJamie Gritton strlcpy(tpr->pr_hostuuid, 1989c1f19219SJamie Gritton pr->pr_hostuuid, 1990c1f19219SJamie Gritton sizeof(tpr->pr_hostuuid)); 199176ca6f88SJamie Gritton if (gothid) 199276ca6f88SJamie Gritton tpr->pr_hostid = hid; 199376ca6f88SJamie Gritton } 199476ca6f88SJamie Gritton } 199576ca6f88SJamie Gritton } 19960304c731SJamie Gritton pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow; 19970fe74ae6SJamie Gritton if ((tallow = ch_allow & ~pr_allow)) 19980fe74ae6SJamie Gritton prison_set_allow_locked(pr, tallow, 0); 1999b38ff370SJamie Gritton /* 2000b38ff370SJamie Gritton * Persistent prisons get an extra reference, and prisons losing their 20011158508aSJamie Gritton * persist flag lose that reference. 2002b38ff370SJamie Gritton */ 200376ad42abSJamie Gritton born = !prison_isalive(pr); 20041158508aSJamie Gritton if (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags)) { 2005b38ff370SJamie Gritton if (pr_flags & PR_PERSIST) { 20066754ae25SJamie Gritton prison_hold(pr); 20071158508aSJamie Gritton /* 20081158508aSJamie Gritton * This may make a dead prison alive again, but wait 20091158508aSJamie Gritton * to label it as such until after OSD calls have had 20101158508aSJamie Gritton * a chance to run (and perhaps to fail). 20111158508aSJamie Gritton */ 20126754ae25SJamie Gritton refcount_acquire(&pr->pr_uref); 2013b38ff370SJamie Gritton } else { 201439c8ef90SJamie Gritton drflags |= PD_DEUREF; 2015f7496dcaSJamie Gritton prison_free_not_last(pr); 2016b38ff370SJamie Gritton } 2017b38ff370SJamie Gritton } 2018b38ff370SJamie Gritton pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags; 2019b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 20202a4b2251SJamie Gritton drflags &= ~PD_LOCKED; 2021c861373bSJamie Gritton /* 2022c861373bSJamie Gritton * Any errors past this point will need to de-persist newly created 2023c861373bSJamie Gritton * prisons, as well as call remove methods. 2024c861373bSJamie Gritton */ 2025c861373bSJamie Gritton if (born) 2026c861373bSJamie Gritton drflags |= PD_KILL; 2027b38ff370SJamie Gritton 2028a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 20294b5c9cf6SEdward Tomasz Napierala if (racct_enable && created) 2030a7ad07bfSEdward Tomasz Napierala prison_racct_attach(pr); 2031a7ad07bfSEdward Tomasz Napierala #endif 2032a7ad07bfSEdward Tomasz Napierala 20330304c731SJamie Gritton /* Locks may have prevented a complete restriction of child IP 20340304c731SJamie Gritton * addresses. If so, allocate some more memory and try again. 20350304c731SJamie Gritton */ 20360304c731SJamie Gritton #ifdef INET 20370304c731SJamie Gritton while (redo_ip4) { 2038eb8dcdeaSGleb Smirnoff ip4s = pr->pr_addrs[PR_INET]->ips; 20398bce8d28SZhenlei Huang MPASS(ip4 == NULL); 2040eb8dcdeaSGleb Smirnoff ip4 = prison_ip_alloc(PR_INET, ip4s, M_WAITOK); 20410304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 20428bce8d28SZhenlei Huang redo_ip4 = false; 20430304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 2044bdfc8cc4SJamie Gritton #ifdef VIMAGE 2045bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 2046bdfc8cc4SJamie Gritton descend = 0; 2047bdfc8cc4SJamie Gritton continue; 2048bdfc8cc4SJamie Gritton } 2049bdfc8cc4SJamie Gritton #endif 20508bce8d28SZhenlei Huang redo_ip4 = !prison_ip_restrict(tpr, PR_INET, &ip4); 20510304c731SJamie Gritton } 20520304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 20530304c731SJamie Gritton } 20540304c731SJamie Gritton #endif 20550304c731SJamie Gritton #ifdef INET6 20560304c731SJamie Gritton while (redo_ip6) { 2057eb8dcdeaSGleb Smirnoff ip6s = pr->pr_addrs[PR_INET6]->ips; 20588bce8d28SZhenlei Huang MPASS(ip6 == NULL); 2059eb8dcdeaSGleb Smirnoff ip6 = prison_ip_alloc(PR_INET6, ip6s, M_WAITOK); 20600304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 20618bce8d28SZhenlei Huang redo_ip6 = false; 20620304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 2063bdfc8cc4SJamie Gritton #ifdef VIMAGE 2064bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 2065bdfc8cc4SJamie Gritton descend = 0; 2066bdfc8cc4SJamie Gritton continue; 2067bdfc8cc4SJamie Gritton } 2068bdfc8cc4SJamie Gritton #endif 20698bce8d28SZhenlei Huang redo_ip6 = !prison_ip_restrict(tpr, PR_INET6, &ip6); 20700304c731SJamie Gritton } 20710304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 20720304c731SJamie Gritton } 20730304c731SJamie Gritton #endif 20740304c731SJamie Gritton 2075b38ff370SJamie Gritton /* Let the modules do their work. */ 2076cc5fd8c7SJamie Gritton if (born) { 2077b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_CREATE, opts); 2078c861373bSJamie Gritton if (error) 20792a4b2251SJamie Gritton goto done_deref; 2080b38ff370SJamie Gritton } 2081b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_SET, opts); 2082c861373bSJamie Gritton if (error) 20832a4b2251SJamie Gritton goto done_deref; 2084b38ff370SJamie Gritton 20851158508aSJamie Gritton /* 20861158508aSJamie Gritton * A new prison is now ready to be seen; either it has gained a user 20871158508aSJamie Gritton * reference via persistence, or is about to gain one via attachment. 20881158508aSJamie Gritton */ 20891158508aSJamie Gritton if (born) { 20901158508aSJamie Gritton drflags = prison_lock_xlock(pr, drflags); 20911158508aSJamie Gritton pr->pr_state = PRISON_STATE_ALIVE; 20921158508aSJamie Gritton } 20931158508aSJamie Gritton 2094b38ff370SJamie Gritton /* Attach this process to the prison if requested. */ 2095b38ff370SJamie Gritton if (flags & JAIL_ATTACH) { 2096c861373bSJamie Gritton error = do_jail_attach(td, pr, 2097589e4c1dSJamie Gritton prison_lock_xlock(pr, drflags & PD_LOCK_FLAGS)); 2098f7496dcaSJamie Gritton drflags &= ~(PD_LOCKED | PD_LIST_XLOCKED); 2099b38ff370SJamie Gritton if (error) { 2100b38ff370SJamie Gritton vfs_opterror(opts, "attach failed"); 21012a4b2251SJamie Gritton goto done_deref; 2102b38ff370SJamie Gritton } 2103b38ff370SJamie Gritton } 2104b38ff370SJamie Gritton 21051fb24974SEdward Tomasz Napierala #ifdef RACCT 21064b5c9cf6SEdward Tomasz Napierala if (racct_enable && !created) { 2107701d6b50SJamie Gritton if (drflags & PD_LOCKED) { 2108701d6b50SJamie Gritton mtx_unlock(&pr->pr_mtx); 2109701d6b50SJamie Gritton drflags &= ~PD_LOCKED; 2110701d6b50SJamie Gritton } 2111f7496dcaSJamie Gritton if (drflags & PD_LIST_XLOCKED) { 2112f7496dcaSJamie Gritton sx_xunlock(&allprison_lock); 2113f7496dcaSJamie Gritton drflags &= ~PD_LIST_XLOCKED; 2114b4e87a63SJamie Gritton } 21151fb24974SEdward Tomasz Napierala prison_racct_modify(pr); 21161fb24974SEdward Tomasz Napierala } 21171fb24974SEdward Tomasz Napierala #endif 21181fb24974SEdward Tomasz Napierala 2119bba7a2e8SRick Macklem #ifdef VNET_NFSD 2120bba7a2e8SRick Macklem if (born && pr != &prison0 && (pr->pr_allow & PR_ALLOW_NFSD) != 0 && 2121bba7a2e8SRick Macklem (pr->pr_root->v_vflag & VV_ROOT) == 0) 2122bba7a2e8SRick Macklem printf("Warning jail jid=%d: mountd/nfsd requires a separate" 2123bba7a2e8SRick Macklem " file system\n", pr->pr_id); 2124bba7a2e8SRick Macklem #endif 2125bba7a2e8SRick Macklem 2126c861373bSJamie Gritton drflags &= ~PD_KILL; 21271fb24974SEdward Tomasz Napierala td->td_retval[0] = pr->pr_id; 21281fb24974SEdward Tomasz Napierala 21292a4b2251SJamie Gritton done_deref: 21302a4b2251SJamie Gritton /* Release any temporary prison holds and/or locks. */ 21312a4b2251SJamie Gritton if (pr != NULL) 21322a4b2251SJamie Gritton prison_deref(pr, drflags); 21332a4b2251SJamie Gritton else if (drflags & PD_LIST_SLOCKED) 2134b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 21352a4b2251SJamie Gritton else if (drflags & PD_LIST_XLOCKED) 2136b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 21375050aa86SKonstantin Belousov if (root != NULL) 2138b38ff370SJamie Gritton vrele(root); 2139b38ff370SJamie Gritton done_errmsg: 2140b38ff370SJamie Gritton if (error) { 21412a4b2251SJamie Gritton /* Write the error message back to userspace. */ 2142176ff3a0SJamie Gritton if (vfs_getopt(opts, "errmsg", (void **)&errmsg, 2143176ff3a0SJamie Gritton &errmsg_len) == 0 && errmsg_len > 0) { 2144b38ff370SJamie Gritton errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1; 2145b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 2146b38ff370SJamie Gritton bcopy(errmsg, 2147b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2148b38ff370SJamie Gritton errmsg_len); 2149b38ff370SJamie Gritton else 2150b38ff370SJamie Gritton copyout(errmsg, 2151b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2152b38ff370SJamie Gritton errmsg_len); 2153b38ff370SJamie Gritton } 2154b38ff370SJamie Gritton } 2155b38ff370SJamie Gritton done_free: 2156b38ff370SJamie Gritton #ifdef INET 2157eb8dcdeaSGleb Smirnoff prison_ip_free(ip4); 2158b38ff370SJamie Gritton #endif 2159b38ff370SJamie Gritton #ifdef INET6 2160eb8dcdeaSGleb Smirnoff prison_ip_free(ip6); 2161b38ff370SJamie Gritton #endif 21626dfe0a3dSMartin Matuska if (g_path != NULL) 21636dfe0a3dSMartin Matuska free(g_path, M_TEMP); 2164b38ff370SJamie Gritton vfs_freeopts(opts); 2165b38ff370SJamie Gritton return (error); 2166b38ff370SJamie Gritton } 2167b38ff370SJamie Gritton 2168b38ff370SJamie Gritton /* 21697de883c8SJamie Gritton * Find the next available prison ID. Return the ID on success, or zero 21707de883c8SJamie Gritton * on failure. Also set a pointer to the allprison list entry the prison 21717de883c8SJamie Gritton * should be inserted before. 21727de883c8SJamie Gritton */ 21737de883c8SJamie Gritton static int 21747de883c8SJamie Gritton get_next_prid(struct prison **insprp) 21757de883c8SJamie Gritton { 21767de883c8SJamie Gritton struct prison *inspr; 21777de883c8SJamie Gritton int jid, maxid; 21787de883c8SJamie Gritton 21797de883c8SJamie Gritton jid = lastprid % JAIL_MAX + 1; 21807de883c8SJamie Gritton if (TAILQ_EMPTY(&allprison) || 21817de883c8SJamie Gritton TAILQ_LAST(&allprison, prisonlist)->pr_id < jid) { 21827de883c8SJamie Gritton /* 21837de883c8SJamie Gritton * A common case is for all jails to be implicitly numbered, 21847de883c8SJamie Gritton * which means they'll go on the end of the list, at least 21857de883c8SJamie Gritton * for the first JAIL_MAX times. 21867de883c8SJamie Gritton */ 21877de883c8SJamie Gritton inspr = NULL; 21887de883c8SJamie Gritton } else { 21897de883c8SJamie Gritton /* 21907de883c8SJamie Gritton * Take two passes through the allprison list: first starting 21917de883c8SJamie Gritton * with the proposed jid, then ending with it. 21927de883c8SJamie Gritton */ 21937de883c8SJamie Gritton for (maxid = JAIL_MAX; maxid != 0; ) { 21947de883c8SJamie Gritton TAILQ_FOREACH(inspr, &allprison, pr_list) { 21957de883c8SJamie Gritton if (inspr->pr_id < jid) 21967de883c8SJamie Gritton continue; 2197f7496dcaSJamie Gritton if (inspr->pr_id > jid) { 2198f7496dcaSJamie Gritton /* Found an opening. */ 21997de883c8SJamie Gritton maxid = 0; 22007de883c8SJamie Gritton break; 22017de883c8SJamie Gritton } 22027de883c8SJamie Gritton if (++jid > maxid) { 22037de883c8SJamie Gritton if (lastprid == maxid || lastprid == 0) 22047de883c8SJamie Gritton { 22057de883c8SJamie Gritton /* 22067de883c8SJamie Gritton * The entire legal range 22077de883c8SJamie Gritton * has been traversed 22087de883c8SJamie Gritton */ 22097de883c8SJamie Gritton return 0; 22107de883c8SJamie Gritton } 22117de883c8SJamie Gritton /* Try again from the start. */ 22127de883c8SJamie Gritton jid = 1; 22137de883c8SJamie Gritton maxid = lastprid; 22147de883c8SJamie Gritton break; 22157de883c8SJamie Gritton } 22167de883c8SJamie Gritton } 22177de883c8SJamie Gritton if (inspr == NULL) { 22187de883c8SJamie Gritton /* Found room at the end of the list. */ 22197de883c8SJamie Gritton break; 22207de883c8SJamie Gritton } 22217de883c8SJamie Gritton } 22227de883c8SJamie Gritton } 22237de883c8SJamie Gritton *insprp = inspr; 22247de883c8SJamie Gritton lastprid = jid; 22257de883c8SJamie Gritton return (jid); 22267de883c8SJamie Gritton } 22277de883c8SJamie Gritton 22287de883c8SJamie Gritton /* 2229b38ff370SJamie Gritton * struct jail_get_args { 2230b38ff370SJamie Gritton * struct iovec *iovp; 2231b38ff370SJamie Gritton * unsigned int iovcnt; 2232b38ff370SJamie Gritton * int flags; 2233b38ff370SJamie Gritton * }; 2234b38ff370SJamie Gritton */ 2235b38ff370SJamie Gritton int 22368451d0ddSKip Macy sys_jail_get(struct thread *td, struct jail_get_args *uap) 2237b38ff370SJamie Gritton { 2238b38ff370SJamie Gritton struct uio *auio; 2239b38ff370SJamie Gritton int error; 2240b38ff370SJamie Gritton 2241b38ff370SJamie Gritton /* Check that we have an even number of iovecs. */ 2242b38ff370SJamie Gritton if (uap->iovcnt & 1) 2243b38ff370SJamie Gritton return (EINVAL); 2244b38ff370SJamie Gritton 2245b38ff370SJamie Gritton error = copyinuio(uap->iovp, uap->iovcnt, &auio); 2246b38ff370SJamie Gritton if (error) 2247b38ff370SJamie Gritton return (error); 2248b38ff370SJamie Gritton error = kern_jail_get(td, auio, uap->flags); 2249b38ff370SJamie Gritton if (error == 0) 2250b38ff370SJamie Gritton error = copyout(auio->uio_iov, uap->iovp, 2251b38ff370SJamie Gritton uap->iovcnt * sizeof (struct iovec)); 2252b38ff370SJamie Gritton free(auio, M_IOV); 2253b38ff370SJamie Gritton return (error); 2254b38ff370SJamie Gritton } 2255b38ff370SJamie Gritton 2256b38ff370SJamie Gritton int 2257b38ff370SJamie Gritton kern_jail_get(struct thread *td, struct uio *optuio, int flags) 2258b38ff370SJamie Gritton { 2259672756aaSJamie Gritton struct bool_flags *bf; 2260672756aaSJamie Gritton struct jailsys_flags *jsf; 22610304c731SJamie Gritton struct prison *pr, *mypr; 2262b38ff370SJamie Gritton struct vfsopt *opt; 2263b38ff370SJamie Gritton struct vfsoptlist *opts; 2264b38ff370SJamie Gritton char *errmsg, *name; 22652a4b2251SJamie Gritton int drflags, error, errmsg_len, errmsg_pos, i, jid, len, pos; 2266672756aaSJamie Gritton unsigned f; 2267b38ff370SJamie Gritton 2268b38ff370SJamie Gritton if (flags & ~JAIL_GET_MASK) 2269b38ff370SJamie Gritton return (EINVAL); 2270b38ff370SJamie Gritton 2271b38ff370SJamie Gritton /* Get the parameter list. */ 2272b38ff370SJamie Gritton error = vfs_buildopts(optuio, &opts); 2273b38ff370SJamie Gritton if (error) 2274b38ff370SJamie Gritton return (error); 2275b38ff370SJamie Gritton errmsg_pos = vfs_getopt_pos(opts, "errmsg"); 22760304c731SJamie Gritton mypr = td->td_ucred->cr_prison; 22772a4b2251SJamie Gritton pr = NULL; 22781e2a13e6SJamie Gritton 2279b38ff370SJamie Gritton /* 2280b38ff370SJamie Gritton * Find the prison specified by one of: lastjid, jid, name. 2281b38ff370SJamie Gritton */ 2282b38ff370SJamie Gritton sx_slock(&allprison_lock); 22832a4b2251SJamie Gritton drflags = PD_LIST_SLOCKED; 2284b38ff370SJamie Gritton error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid)); 2285b38ff370SJamie Gritton if (error == 0) { 2286b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 2287f7496dcaSJamie Gritton if (pr->pr_id > jid && 2288f7496dcaSJamie Gritton ((flags & JAIL_DYING) || prison_isalive(pr)) && 2289f7496dcaSJamie Gritton prison_ischild(mypr, pr)) { 2290b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 22912a4b2251SJamie Gritton drflags |= PD_LOCKED; 2292b38ff370SJamie Gritton goto found_prison; 22932a4b2251SJamie Gritton } 2294f7496dcaSJamie Gritton } 2295b38ff370SJamie Gritton error = ENOENT; 2296b38ff370SJamie Gritton vfs_opterror(opts, "no jail after %d", jid); 22972a4b2251SJamie Gritton goto done; 2298b38ff370SJamie Gritton } else if (error != ENOENT) 22992a4b2251SJamie Gritton goto done; 2300b38ff370SJamie Gritton 2301b38ff370SJamie Gritton error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 2302b38ff370SJamie Gritton if (error == 0) { 2303b38ff370SJamie Gritton if (jid != 0) { 23040304c731SJamie Gritton pr = prison_find_child(mypr, jid); 2305b38ff370SJamie Gritton if (pr != NULL) { 23062a4b2251SJamie Gritton drflags |= PD_LOCKED; 230776ad42abSJamie Gritton if (!(prison_isalive(pr) || 230876ad42abSJamie Gritton (flags & JAIL_DYING))) { 2309b38ff370SJamie Gritton error = ENOENT; 2310b38ff370SJamie Gritton vfs_opterror(opts, "jail %d is dying", 2311b38ff370SJamie Gritton jid); 23122a4b2251SJamie Gritton goto done; 2313b38ff370SJamie Gritton } 2314b38ff370SJamie Gritton goto found_prison; 2315b38ff370SJamie Gritton } 2316b38ff370SJamie Gritton error = ENOENT; 2317b38ff370SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 23182a4b2251SJamie Gritton goto done; 2319b38ff370SJamie Gritton } 2320b38ff370SJamie Gritton } else if (error != ENOENT) 23212a4b2251SJamie Gritton goto done; 2322b38ff370SJamie Gritton 2323b38ff370SJamie Gritton error = vfs_getopt(opts, "name", (void **)&name, &len); 2324b38ff370SJamie Gritton if (error == 0) { 2325b38ff370SJamie Gritton if (len == 0 || name[len - 1] != '\0') { 2326b38ff370SJamie Gritton error = EINVAL; 23272a4b2251SJamie Gritton goto done; 2328b38ff370SJamie Gritton } 23290304c731SJamie Gritton pr = prison_find_name(mypr, name); 2330b38ff370SJamie Gritton if (pr != NULL) { 23312a4b2251SJamie Gritton drflags |= PD_LOCKED; 233276ad42abSJamie Gritton if (!(prison_isalive(pr) || (flags & JAIL_DYING))) { 2333b38ff370SJamie Gritton error = ENOENT; 2334b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" is dying", 2335b38ff370SJamie Gritton name); 23362a4b2251SJamie Gritton goto done; 2337b38ff370SJamie Gritton } 2338b38ff370SJamie Gritton goto found_prison; 2339b38ff370SJamie Gritton } 2340b38ff370SJamie Gritton error = ENOENT; 2341b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", name); 23422a4b2251SJamie Gritton goto done; 2343b38ff370SJamie Gritton } else if (error != ENOENT) 23442a4b2251SJamie Gritton goto done; 2345b38ff370SJamie Gritton 2346b38ff370SJamie Gritton vfs_opterror(opts, "no jail specified"); 2347b38ff370SJamie Gritton error = ENOENT; 23482a4b2251SJamie Gritton goto done; 2349b38ff370SJamie Gritton 2350b38ff370SJamie Gritton found_prison: 2351b38ff370SJamie Gritton /* Get the parameters of the prison. */ 23526754ae25SJamie Gritton prison_hold(pr); 23532a4b2251SJamie Gritton drflags |= PD_DEREF; 2354b38ff370SJamie Gritton td->td_retval[0] = pr->pr_id; 2355b38ff370SJamie Gritton error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id)); 2356b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 23572a4b2251SJamie Gritton goto done; 23580304c731SJamie Gritton i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id; 23590304c731SJamie Gritton error = vfs_setopt(opts, "parent", &i, sizeof(i)); 2360b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 23612a4b2251SJamie Gritton goto done; 23620304c731SJamie Gritton error = vfs_setopts(opts, "name", prison_name(mypr, pr)); 23630304c731SJamie Gritton if (error != 0 && error != ENOENT) 23642a4b2251SJamie Gritton goto done; 23650304c731SJamie Gritton error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id, 2366b38ff370SJamie Gritton sizeof(pr->pr_cpuset->cs_id)); 2367b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 23682a4b2251SJamie Gritton goto done; 23690304c731SJamie Gritton error = vfs_setopts(opts, "path", prison_path(mypr, pr)); 2370b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 23712a4b2251SJamie Gritton goto done; 2372b38ff370SJamie Gritton #ifdef INET 2373eb8dcdeaSGleb Smirnoff error = vfs_setopt_part(opts, "ip4.addr", pr->pr_addrs[PR_INET] + 1, 2374eb8dcdeaSGleb Smirnoff pr->pr_addrs[PR_INET] ? pr->pr_addrs[PR_INET]->ips * 2375eb8dcdeaSGleb Smirnoff pr_families[PR_INET].size : 0 ); 2376b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 23772a4b2251SJamie Gritton goto done; 2378b38ff370SJamie Gritton #endif 2379b38ff370SJamie Gritton #ifdef INET6 2380eb8dcdeaSGleb Smirnoff error = vfs_setopt_part(opts, "ip6.addr", pr->pr_addrs[PR_INET6] + 1, 2381eb8dcdeaSGleb Smirnoff pr->pr_addrs[PR_INET6] ? pr->pr_addrs[PR_INET6]->ips * 2382eb8dcdeaSGleb Smirnoff pr_families[PR_INET6].size : 0 ); 2383b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 23842a4b2251SJamie Gritton goto done; 2385b38ff370SJamie Gritton #endif 2386b38ff370SJamie Gritton error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel, 2387b38ff370SJamie Gritton sizeof(pr->pr_securelevel)); 2388b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 23892a4b2251SJamie Gritton goto done; 2390b97457e2SJamie Gritton error = vfs_setopt(opts, "children.cur", &pr->pr_childcount, 2391b97457e2SJamie Gritton sizeof(pr->pr_childcount)); 2392b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 23932a4b2251SJamie Gritton goto done; 2394b97457e2SJamie Gritton error = vfs_setopt(opts, "children.max", &pr->pr_childmax, 2395b97457e2SJamie Gritton sizeof(pr->pr_childmax)); 2396b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 23972a4b2251SJamie Gritton goto done; 2398c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostname", pr->pr_hostname); 2399b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24002a4b2251SJamie Gritton goto done; 2401c1f19219SJamie Gritton error = vfs_setopts(opts, "host.domainname", pr->pr_domainname); 240276ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 24032a4b2251SJamie Gritton goto done; 2404c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid); 240576ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 24062a4b2251SJamie Gritton goto done; 2407841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32 2408a5c1afadSDmitry Chagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 240976ca6f88SJamie Gritton uint32_t hid32 = pr->pr_hostid; 241076ca6f88SJamie Gritton 241176ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32)); 241276ca6f88SJamie Gritton } else 241376ca6f88SJamie Gritton #endif 241476ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid, 241576ca6f88SJamie Gritton sizeof(pr->pr_hostid)); 241676ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 24172a4b2251SJamie Gritton goto done; 24180304c731SJamie Gritton error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs, 24190304c731SJamie Gritton sizeof(pr->pr_enforce_statfs)); 24200304c731SJamie Gritton if (error != 0 && error != ENOENT) 24212a4b2251SJamie Gritton goto done; 24220cc207a6SMartin Matuska error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum, 24230cc207a6SMartin Matuska sizeof(pr->pr_devfs_rsnum)); 24240cc207a6SMartin Matuska if (error != 0 && error != ENOENT) 24252a4b2251SJamie Gritton goto done; 2426672756aaSJamie Gritton for (bf = pr_flag_bool; 2427672756aaSJamie Gritton bf < pr_flag_bool + nitems(pr_flag_bool); 2428672756aaSJamie Gritton bf++) { 2429672756aaSJamie Gritton i = (pr->pr_flags & bf->flag) ? 1 : 0; 2430672756aaSJamie Gritton error = vfs_setopt(opts, bf->name, &i, sizeof(i)); 2431b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24322a4b2251SJamie Gritton goto done; 2433b38ff370SJamie Gritton i = !i; 2434672756aaSJamie Gritton error = vfs_setopt(opts, bf->noname, &i, sizeof(i)); 2435b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24362a4b2251SJamie Gritton goto done; 24370304c731SJamie Gritton } 2438672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 2439672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 2440672756aaSJamie Gritton jsf++) { 2441672756aaSJamie Gritton f = pr->pr_flags & (jsf->disable | jsf->new); 2442672756aaSJamie Gritton i = (f != 0 && f == jsf->disable) ? JAIL_SYS_DISABLE 2443672756aaSJamie Gritton : (f == jsf->new) ? JAIL_SYS_NEW 24447cbf7213SJamie Gritton : JAIL_SYS_INHERIT; 2445672756aaSJamie Gritton error = vfs_setopt(opts, jsf->name, &i, sizeof(i)); 24467cbf7213SJamie Gritton if (error != 0 && error != ENOENT) 24472a4b2251SJamie Gritton goto done; 24487cbf7213SJamie Gritton } 2449672756aaSJamie Gritton for (bf = pr_flag_allow; 24505d58f959SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 24515d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 2452672756aaSJamie Gritton bf++) { 2453672756aaSJamie Gritton i = (pr->pr_allow & bf->flag) ? 1 : 0; 2454672756aaSJamie Gritton error = vfs_setopt(opts, bf->name, &i, sizeof(i)); 24550304c731SJamie Gritton if (error != 0 && error != ENOENT) 24562a4b2251SJamie Gritton goto done; 24570304c731SJamie Gritton i = !i; 2458672756aaSJamie Gritton error = vfs_setopt(opts, bf->noname, &i, sizeof(i)); 24590304c731SJamie Gritton if (error != 0 && error != ENOENT) 24602a4b2251SJamie Gritton goto done; 24610304c731SJamie Gritton } 246276ad42abSJamie Gritton i = !prison_isalive(pr); 2463b38ff370SJamie Gritton error = vfs_setopt(opts, "dying", &i, sizeof(i)); 2464b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24652a4b2251SJamie Gritton goto done; 2466b38ff370SJamie Gritton i = !i; 2467b38ff370SJamie Gritton error = vfs_setopt(opts, "nodying", &i, sizeof(i)); 2468b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24692a4b2251SJamie Gritton goto done; 2470bd96bd15SIan Lepore error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate, 2471bd96bd15SIan Lepore sizeof(pr->pr_osreldate)); 2472a1a4c1b0SIan Lepore if (error != 0 && error != ENOENT) 24732a4b2251SJamie Gritton goto done; 2474a1a4c1b0SIan Lepore error = vfs_setopts(opts, "osrelease", pr->pr_osrelease); 2475a1a4c1b0SIan Lepore if (error != 0 && error != ENOENT) 24762a4b2251SJamie Gritton goto done; 2477b38ff370SJamie Gritton 2478b38ff370SJamie Gritton /* Get the module parameters. */ 2479b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 24802a4b2251SJamie Gritton drflags &= ~PD_LOCKED; 2481b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_GET, opts); 2482b38ff370SJamie Gritton if (error) 24832a4b2251SJamie Gritton goto done; 24842a4b2251SJamie Gritton prison_deref(pr, drflags); 24852a4b2251SJamie Gritton pr = NULL; 24862a4b2251SJamie Gritton drflags = 0; 2487b38ff370SJamie Gritton 2488b38ff370SJamie Gritton /* By now, all parameters should have been noted. */ 2489b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2490b38ff370SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 2491b38ff370SJamie Gritton error = EINVAL; 2492b38ff370SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 24932a4b2251SJamie Gritton goto done; 2494b38ff370SJamie Gritton } 2495b38ff370SJamie Gritton } 2496b38ff370SJamie Gritton 2497b38ff370SJamie Gritton /* Write the fetched parameters back to userspace. */ 2498b38ff370SJamie Gritton error = 0; 2499b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2500b38ff370SJamie Gritton if (opt->pos >= 0 && opt->pos != errmsg_pos) { 2501b38ff370SJamie Gritton pos = 2 * opt->pos + 1; 2502b38ff370SJamie Gritton optuio->uio_iov[pos].iov_len = opt->len; 2503b38ff370SJamie Gritton if (opt->value != NULL) { 2504b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) { 2505b38ff370SJamie Gritton bcopy(opt->value, 2506b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2507b38ff370SJamie Gritton opt->len); 2508b38ff370SJamie Gritton } else { 2509b38ff370SJamie Gritton error = copyout(opt->value, 2510b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2511b38ff370SJamie Gritton opt->len); 2512b38ff370SJamie Gritton if (error) 2513b38ff370SJamie Gritton break; 2514b38ff370SJamie Gritton } 2515b38ff370SJamie Gritton } 2516b38ff370SJamie Gritton } 2517b38ff370SJamie Gritton } 2518b38ff370SJamie Gritton 25192a4b2251SJamie Gritton done: 25202a4b2251SJamie Gritton /* Release any temporary prison holds and/or locks. */ 25212a4b2251SJamie Gritton if (pr != NULL) 25222a4b2251SJamie Gritton prison_deref(pr, drflags); 25232a4b2251SJamie Gritton else if (drflags & PD_LIST_SLOCKED) 2524b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2525b38ff370SJamie Gritton if (error && errmsg_pos >= 0) { 25262a4b2251SJamie Gritton /* Write the error message back to userspace. */ 2527b38ff370SJamie Gritton vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 2528b38ff370SJamie Gritton errmsg_pos = 2 * errmsg_pos + 1; 2529b38ff370SJamie Gritton if (errmsg_len > 0) { 2530b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 2531b38ff370SJamie Gritton bcopy(errmsg, 2532b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2533b38ff370SJamie Gritton errmsg_len); 2534b38ff370SJamie Gritton else 2535b38ff370SJamie Gritton copyout(errmsg, 2536b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2537b38ff370SJamie Gritton errmsg_len); 2538b38ff370SJamie Gritton } 2539b38ff370SJamie Gritton } 2540b38ff370SJamie Gritton vfs_freeopts(opts); 2541b38ff370SJamie Gritton return (error); 2542b38ff370SJamie Gritton } 2543b38ff370SJamie Gritton 2544b38ff370SJamie Gritton /* 2545b38ff370SJamie Gritton * struct jail_remove_args { 2546b38ff370SJamie Gritton * int jid; 2547b38ff370SJamie Gritton * }; 2548b38ff370SJamie Gritton */ 2549b38ff370SJamie Gritton int 25508451d0ddSKip Macy sys_jail_remove(struct thread *td, struct jail_remove_args *uap) 2551b38ff370SJamie Gritton { 2552c861373bSJamie Gritton struct prison *pr; 2553c861373bSJamie Gritton int error; 2554b38ff370SJamie Gritton 2555b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_REMOVE); 2556b38ff370SJamie Gritton if (error) 2557b38ff370SJamie Gritton return (error); 2558b38ff370SJamie Gritton 2559b38ff370SJamie Gritton sx_xlock(&allprison_lock); 25600304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2561b38ff370SJamie Gritton if (pr == NULL) { 2562b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2563b38ff370SJamie Gritton return (EINVAL); 2564b38ff370SJamie Gritton } 2565c861373bSJamie Gritton if (!prison_isalive(pr)) { 2566c861373bSJamie Gritton /* Silently ignore already-dying prisons. */ 25670304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 2568c861373bSJamie Gritton sx_xunlock(&allprison_lock); 25690304c731SJamie Gritton return (0); 25700304c731SJamie Gritton } 2571c861373bSJamie Gritton prison_deref(pr, PD_KILL | PD_LOCKED | PD_LIST_XLOCKED); 2572c861373bSJamie Gritton return (0); 257375c13541SPoul-Henning Kamp } 257475c13541SPoul-Henning Kamp 2575fd7a8150SMike Barcroft /* 25769ddb7954SMike Barcroft * struct jail_attach_args { 25779ddb7954SMike Barcroft * int jid; 25789ddb7954SMike Barcroft * }; 2579fd7a8150SMike Barcroft */ 2580fd7a8150SMike Barcroft int 25818451d0ddSKip Macy sys_jail_attach(struct thread *td, struct jail_attach_args *uap) 2582fd7a8150SMike Barcroft { 2583b38ff370SJamie Gritton struct prison *pr; 2584b38ff370SJamie Gritton int error; 2585b38ff370SJamie Gritton 2586b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 2587b38ff370SJamie Gritton if (error) 2588b38ff370SJamie Gritton return (error); 2589b38ff370SJamie Gritton 2590f7496dcaSJamie Gritton sx_slock(&allprison_lock); 25910304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2592b38ff370SJamie Gritton if (pr == NULL) { 2593b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2594b38ff370SJamie Gritton return (EINVAL); 2595b38ff370SJamie Gritton } 2596b38ff370SJamie Gritton 259776ad42abSJamie Gritton /* Do not allow a process to attach to a prison that is not alive. */ 259876ad42abSJamie Gritton if (!prison_isalive(pr)) { 2599b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2600b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2601b38ff370SJamie Gritton return (EINVAL); 2602b38ff370SJamie Gritton } 2603b38ff370SJamie Gritton 2604f7496dcaSJamie Gritton return (do_jail_attach(td, pr, PD_LOCKED | PD_LIST_SLOCKED)); 2605b38ff370SJamie Gritton } 2606b38ff370SJamie Gritton 2607b38ff370SJamie Gritton static int 2608f7496dcaSJamie Gritton do_jail_attach(struct thread *td, struct prison *pr, int drflags) 2609b38ff370SJamie Gritton { 2610fd7a8150SMike Barcroft struct proc *p; 2611fd7a8150SMike Barcroft struct ucred *newcred, *oldcred; 26125050aa86SKonstantin Belousov int error; 2613fd7a8150SMike Barcroft 2614f7496dcaSJamie Gritton mtx_assert(&pr->pr_mtx, MA_OWNED); 2615f7496dcaSJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 2616589e4c1dSJamie Gritton drflags &= PD_LOCK_FLAGS; 261757f22bd4SJacques Vidrine /* 261857f22bd4SJacques Vidrine * XXX: Note that there is a slight race here if two threads 261957f22bd4SJacques Vidrine * in the same privileged process attempt to attach to two 262057f22bd4SJacques Vidrine * different jails at the same time. It is important for 262157f22bd4SJacques Vidrine * user processes not to do this, or they might end up with 262257f22bd4SJacques Vidrine * a process root from one prison, but attached to the jail 262357f22bd4SJacques Vidrine * of another. 262457f22bd4SJacques Vidrine */ 26251158508aSJamie Gritton prison_hold(pr); 26266754ae25SJamie Gritton refcount_acquire(&pr->pr_uref); 2627f7496dcaSJamie Gritton drflags |= PD_DEREF | PD_DEUREF; 2628fd7a8150SMike Barcroft mtx_unlock(&pr->pr_mtx); 2629f7496dcaSJamie Gritton drflags &= ~PD_LOCKED; 2630b38ff370SJamie Gritton 2631b38ff370SJamie Gritton /* Let modules do whatever they need to prepare for attaching. */ 2632b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_ATTACH, td); 2633b38ff370SJamie Gritton if (error) { 2634f7496dcaSJamie Gritton prison_deref(pr, drflags); 2635b38ff370SJamie Gritton return (error); 2636b38ff370SJamie Gritton } 2637f7496dcaSJamie Gritton sx_unlock(&allprison_lock); 2638f7496dcaSJamie Gritton drflags &= ~(PD_LIST_SLOCKED | PD_LIST_XLOCKED); 2639fd7a8150SMike Barcroft 2640413628a7SBjoern A. Zeeb /* 2641413628a7SBjoern A. Zeeb * Reparent the newly attached process to this jail. 2642413628a7SBjoern A. Zeeb */ 2643b38ff370SJamie Gritton p = td->td_proc; 2644413628a7SBjoern A. Zeeb error = cpuset_setproc_update_set(p, pr->pr_cpuset); 2645413628a7SBjoern A. Zeeb if (error) 2646b38ff370SJamie Gritton goto e_revert_osd; 2647413628a7SBjoern A. Zeeb 2648cb05b60aSAttilio Rao vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY); 2649fd7a8150SMike Barcroft if ((error = change_dir(pr->pr_root, td)) != 0) 2650fd7a8150SMike Barcroft goto e_unlock; 2651fd7a8150SMike Barcroft #ifdef MAC 265230d239bcSRobert Watson if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root))) 2653fd7a8150SMike Barcroft goto e_unlock; 2654fd7a8150SMike Barcroft #endif 2655b249ce48SMateusz Guzik VOP_UNLOCK(pr->pr_root); 2656d4380c0cSJamie Gritton if ((error = pwd_chroot_chdir(td, pr->pr_root))) 26575050aa86SKonstantin Belousov goto e_revert_osd; 2658fd7a8150SMike Barcroft 2659fd7a8150SMike Barcroft newcred = crget(); 2660fd7a8150SMike Barcroft PROC_LOCK(p); 26611fb6767dSJamie Gritton oldcred = crcopysafe(p, newcred); 266269c4ee54SJohn Baldwin newcred->cr_prison = pr; 2663daf63fd2SMateusz Guzik proc_set_cred(p, newcred); 26641fb6767dSJamie Gritton setsugid(p); 2665097055e2SEdward Tomasz Napierala #ifdef RACCT 2666097055e2SEdward Tomasz Napierala racct_proc_ucred_changed(p, oldcred, newcred); 2667f87beb93SAndriy Gapon crhold(newcred); 2668f87beb93SAndriy Gapon #endif 2669f87beb93SAndriy Gapon PROC_UNLOCK(p); 2670f87beb93SAndriy Gapon #ifdef RCTL 2671f87beb93SAndriy Gapon rctl_proc_ucred_changed(p, newcred); 2672f87beb93SAndriy Gapon crfree(newcred); 2673097055e2SEdward Tomasz Napierala #endif 26745ecb5444SMateusz Guzik prison_proc_relink(oldcred->cr_prison, pr, p); 2675f7496dcaSJamie Gritton prison_deref(oldcred->cr_prison, drflags); 2676fd7a8150SMike Barcroft crfree(oldcred); 2677cc7b7306SJamie Gritton 2678cc7b7306SJamie Gritton /* 2679cc7b7306SJamie Gritton * If the prison was killed while changing credentials, die along 2680cc7b7306SJamie Gritton * with it. 2681cc7b7306SJamie Gritton */ 2682cc7b7306SJamie Gritton if (!prison_isalive(pr)) { 2683cc7b7306SJamie Gritton PROC_LOCK(p); 2684cc7b7306SJamie Gritton kern_psignal(p, SIGKILL); 2685cc7b7306SJamie Gritton PROC_UNLOCK(p); 2686cc7b7306SJamie Gritton } 2687cc7b7306SJamie Gritton 2688fd7a8150SMike Barcroft return (0); 26891fb6767dSJamie Gritton 2690fd7a8150SMike Barcroft e_unlock: 2691b249ce48SMateusz Guzik VOP_UNLOCK(pr->pr_root); 2692b38ff370SJamie Gritton e_revert_osd: 2693b38ff370SJamie Gritton /* Tell modules this thread is still in its old jail after all. */ 26947f4e7248SJamie Gritton sx_slock(&allprison_lock); 2695f7496dcaSJamie Gritton drflags |= PD_LIST_SLOCKED; 26961fb6767dSJamie Gritton (void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td); 2697f7496dcaSJamie Gritton prison_deref(pr, drflags); 2698fd7a8150SMike Barcroft return (error); 2699fd7a8150SMike Barcroft } 2700fd7a8150SMike Barcroft 2701fd7a8150SMike Barcroft /* 2702fd7a8150SMike Barcroft * Returns a locked prison instance, or NULL on failure. 2703fd7a8150SMike Barcroft */ 270454b369c1SPawel Jakub Dawidek struct prison * 2705fd7a8150SMike Barcroft prison_find(int prid) 2706fd7a8150SMike Barcroft { 2707fd7a8150SMike Barcroft struct prison *pr; 2708fd7a8150SMike Barcroft 2709dc68a633SPawel Jakub Dawidek sx_assert(&allprison_lock, SX_LOCKED); 2710b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 2711f7496dcaSJamie Gritton if (pr->pr_id < prid) 2712f7496dcaSJamie Gritton continue; 27137de883c8SJamie Gritton if (pr->pr_id > prid) 27147de883c8SJamie Gritton break; 2715f7496dcaSJamie Gritton KASSERT(prison_isvalid(pr), ("Found invalid prison %p", pr)); 2716f7496dcaSJamie Gritton mtx_lock(&pr->pr_mtx); 2717f7496dcaSJamie Gritton return (pr); 2718fd7a8150SMike Barcroft } 2719fd7a8150SMike Barcroft return (NULL); 2720fd7a8150SMike Barcroft } 2721fd7a8150SMike Barcroft 2722b38ff370SJamie Gritton /* 27230304c731SJamie Gritton * Find a prison that is a descendant of mypr. Returns a locked prison or NULL. 2724b38ff370SJamie Gritton */ 2725b38ff370SJamie Gritton struct prison * 27260304c731SJamie Gritton prison_find_child(struct prison *mypr, int prid) 2727b38ff370SJamie Gritton { 27280304c731SJamie Gritton struct prison *pr; 27290304c731SJamie Gritton int descend; 2730b38ff370SJamie Gritton 2731b38ff370SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 27320304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 27330304c731SJamie Gritton if (pr->pr_id == prid) { 2734f7496dcaSJamie Gritton KASSERT(prison_isvalid(pr), 2735f7496dcaSJamie Gritton ("Found invalid prison %p", pr)); 27360304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 27370304c731SJamie Gritton return (pr); 27380304c731SJamie Gritton } 27390304c731SJamie Gritton } 27400304c731SJamie Gritton return (NULL); 27410304c731SJamie Gritton } 27420304c731SJamie Gritton 27430304c731SJamie Gritton /* 27440304c731SJamie Gritton * Look for the name relative to mypr. Returns a locked prison or NULL. 27450304c731SJamie Gritton */ 27460304c731SJamie Gritton struct prison * 27470304c731SJamie Gritton prison_find_name(struct prison *mypr, const char *name) 27480304c731SJamie Gritton { 27490304c731SJamie Gritton struct prison *pr, *deadpr; 27500304c731SJamie Gritton size_t mylen; 27510304c731SJamie Gritton int descend; 27520304c731SJamie Gritton 27530304c731SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 27540304c731SJamie Gritton mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1; 2755b38ff370SJamie Gritton deadpr = NULL; 27560304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 27570304c731SJamie Gritton if (!strcmp(pr->pr_name + mylen, name)) { 2758f7496dcaSJamie Gritton KASSERT(prison_isvalid(pr), 2759f7496dcaSJamie Gritton ("Found invalid prison %p", pr)); 2760f7496dcaSJamie Gritton if (prison_isalive(pr)) { 2761b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 2762b38ff370SJamie Gritton return (pr); 2763f7496dcaSJamie Gritton } 2764b38ff370SJamie Gritton deadpr = pr; 2765b38ff370SJamie Gritton } 2766b38ff370SJamie Gritton } 27670304c731SJamie Gritton /* There was no valid prison - perhaps there was a dying one. */ 2768f7496dcaSJamie Gritton if (deadpr != NULL) 2769b38ff370SJamie Gritton mtx_lock(&deadpr->pr_mtx); 2770b38ff370SJamie Gritton return (deadpr); 2771b38ff370SJamie Gritton } 2772b38ff370SJamie Gritton 2773b38ff370SJamie Gritton /* 27740fe74ae6SJamie Gritton * See if a prison has the specific flag set. The prison should be locked, 27750fe74ae6SJamie Gritton * unless checking for flags that are only set at jail creation (such as 27760fe74ae6SJamie Gritton * PR_IP4 and PR_IP6), or only the single bit is examined, without regard 27770fe74ae6SJamie Gritton * to any other prison data. 27780304c731SJamie Gritton */ 27790304c731SJamie Gritton int 27800304c731SJamie Gritton prison_flag(struct ucred *cred, unsigned flag) 27810304c731SJamie Gritton { 27820304c731SJamie Gritton 27830304c731SJamie Gritton return (cred->cr_prison->pr_flags & flag); 27840304c731SJamie Gritton } 27850304c731SJamie Gritton 27860304c731SJamie Gritton int 27870304c731SJamie Gritton prison_allow(struct ucred *cred, unsigned flag) 27880304c731SJamie Gritton { 27890304c731SJamie Gritton 27900fe74ae6SJamie Gritton return ((cred->cr_prison->pr_allow & flag) != 0); 27910304c731SJamie Gritton } 27920304c731SJamie Gritton 27930304c731SJamie Gritton /* 2794effad35eSJamie Gritton * Hold a prison reference, by incrementing pr_ref. It is generally 2795effad35eSJamie Gritton * an error to hold a prison that does not already have a reference. 2796effad35eSJamie Gritton * A prison record will remain valid as long as it has at least one 2797effad35eSJamie Gritton * reference, and will not be removed as long as either the prison 2798effad35eSJamie Gritton * mutex or the allprison lock is held (allprison_lock may be shared). 2799effad35eSJamie Gritton */ 2800effad35eSJamie Gritton void 2801effad35eSJamie Gritton prison_hold_locked(struct prison *pr) 2802effad35eSJamie Gritton { 2803effad35eSJamie Gritton 28046754ae25SJamie Gritton /* Locking is no longer required. */ 28056754ae25SJamie Gritton prison_hold(pr); 2806effad35eSJamie Gritton } 2807effad35eSJamie Gritton 2808effad35eSJamie Gritton void 2809effad35eSJamie Gritton prison_hold(struct prison *pr) 2810effad35eSJamie Gritton { 28116754ae25SJamie Gritton #ifdef INVARIANTS 28126754ae25SJamie Gritton int was_valid = refcount_acquire_if_not_zero(&pr->pr_ref); 2813effad35eSJamie Gritton 28146754ae25SJamie Gritton KASSERT(was_valid, 28156754ae25SJamie Gritton ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id)); 28166754ae25SJamie Gritton #else 28176754ae25SJamie Gritton refcount_acquire(&pr->pr_ref); 28186754ae25SJamie Gritton #endif 2819effad35eSJamie Gritton } 2820effad35eSJamie Gritton 2821effad35eSJamie Gritton /* 2822effad35eSJamie Gritton * Remove a prison reference. If that was the last reference, the 2823f7496dcaSJamie Gritton * prison will be removed (at a later time). 2824b38ff370SJamie Gritton */ 282591421ba2SRobert Watson void 28261ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr) 282791421ba2SRobert Watson { 282891421ba2SRobert Watson 28291ba4a712SPawel Jakub Dawidek mtx_assert(&pr->pr_mtx, MA_OWNED); 2830effad35eSJamie Gritton /* 2831f7496dcaSJamie Gritton * Locking is no longer required, but unlock because the caller 2832f7496dcaSJamie Gritton * expects it. 2833effad35eSJamie Gritton */ 2834f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 2835f7496dcaSJamie Gritton prison_free(pr); 2836effad35eSJamie Gritton } 2837c2cda609SPawel Jakub Dawidek 28381ba4a712SPawel Jakub Dawidek void 28391ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr) 28401ba4a712SPawel Jakub Dawidek { 28411ba4a712SPawel Jakub Dawidek 28426754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 28436754ae25SJamie Gritton ("Trying to free dead prison %p (jid=%d).", 28446754ae25SJamie Gritton pr, pr->pr_id)); 2845f7496dcaSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_ref)) { 2846f7496dcaSJamie Gritton /* 2847f7496dcaSJamie Gritton * Don't remove the last reference in this context, 2848f7496dcaSJamie Gritton * in case there are locks held. 2849f7496dcaSJamie Gritton */ 2850f7496dcaSJamie Gritton taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 2851f7496dcaSJamie Gritton } 2852f7496dcaSJamie Gritton } 2853f7496dcaSJamie Gritton 2854f7496dcaSJamie Gritton static void 2855f7496dcaSJamie Gritton prison_free_not_last(struct prison *pr) 2856f7496dcaSJamie Gritton { 2857f7496dcaSJamie Gritton #ifdef INVARIANTS 2858f7496dcaSJamie Gritton int lastref; 2859f7496dcaSJamie Gritton 2860f7496dcaSJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 2861f7496dcaSJamie Gritton ("Trying to free dead prison %p (jid=%d).", 2862f7496dcaSJamie Gritton pr, pr->pr_id)); 2863f7496dcaSJamie Gritton lastref = refcount_release(&pr->pr_ref); 2864f7496dcaSJamie Gritton KASSERT(!lastref, 2865f7496dcaSJamie Gritton ("prison_free_not_last freed last ref on prison %p (jid=%d).", 2866f7496dcaSJamie Gritton pr, pr->pr_id)); 2867f7496dcaSJamie Gritton #else 2868ee9b37aeSMateusz Guzik refcount_release(&pr->pr_ref); 2869f7496dcaSJamie Gritton #endif 28701ba4a712SPawel Jakub Dawidek } 28711ba4a712SPawel Jakub Dawidek 287273d9e52dSJamie Gritton /* 2873f171938cSGordon Bergling * Hold a prison for user visibility, by incrementing pr_uref. 2874effad35eSJamie Gritton * It is generally an error to hold a prison that isn't already 287549a033d8SGordon Bergling * user-visible, except through the jail system calls. It is also 2876effad35eSJamie Gritton * an error to hold an invalid prison. A prison record will remain 2877effad35eSJamie Gritton * alive as long as it has at least one user reference, and will not 2878f7496dcaSJamie Gritton * be set to the dying state until the prison mutex and allprison_lock 2879f7496dcaSJamie Gritton * are both freed. 2880effad35eSJamie Gritton */ 2881effad35eSJamie Gritton void 2882effad35eSJamie Gritton prison_proc_hold(struct prison *pr) 2883effad35eSJamie Gritton { 28846754ae25SJamie Gritton #ifdef INVARIANTS 28856754ae25SJamie Gritton int was_alive = refcount_acquire_if_not_zero(&pr->pr_uref); 2886effad35eSJamie Gritton 28876754ae25SJamie Gritton KASSERT(was_alive, 2888effad35eSJamie Gritton ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id)); 28896754ae25SJamie Gritton #else 28906754ae25SJamie Gritton refcount_acquire(&pr->pr_uref); 28916754ae25SJamie Gritton #endif 2892effad35eSJamie Gritton } 2893effad35eSJamie Gritton 2894effad35eSJamie Gritton /* 2895effad35eSJamie Gritton * Remove a prison user reference. If it was the last reference, the 2896effad35eSJamie Gritton * prison will be considered "dying", and may be removed once all of 2897effad35eSJamie Gritton * its references are dropped. 2898effad35eSJamie Gritton */ 2899effad35eSJamie Gritton void 2900effad35eSJamie Gritton prison_proc_free(struct prison *pr) 2901effad35eSJamie Gritton { 2902effad35eSJamie Gritton 29036754ae25SJamie Gritton /* 29046754ae25SJamie Gritton * Locking is only required when releasing the last reference. 29056754ae25SJamie Gritton * This allows assurance that a locked prison will remain alive 29066754ae25SJamie Gritton * until it is unlocked. 29076754ae25SJamie Gritton */ 29086754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_uref) > 0, 2909effad35eSJamie Gritton ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id)); 2910195cd6aeSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_uref)) { 2911effad35eSJamie Gritton /* 2912effad35eSJamie Gritton * Don't remove the last user reference in this context, 2913effad35eSJamie Gritton * which is expected to be a process that is not only locked, 2914effad35eSJamie Gritton * but also half dead. Add a reference so any calls to 2915effad35eSJamie Gritton * prison_free() won't re-submit the task. 2916effad35eSJamie Gritton */ 2917f7496dcaSJamie Gritton prison_hold(pr); 29181158508aSJamie Gritton mtx_lock(&pr->pr_mtx); 29191158508aSJamie Gritton KASSERT(!(pr->pr_flags & PR_COMPLETE_PROC), 29201158508aSJamie Gritton ("Redundant last reference in prison_proc_free (jid=%d)", 29211158508aSJamie Gritton pr->pr_id)); 29221158508aSJamie Gritton pr->pr_flags |= PR_COMPLETE_PROC; 29231158508aSJamie Gritton mtx_unlock(&pr->pr_mtx); 2924effad35eSJamie Gritton taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 2925effad35eSJamie Gritton } 2926effad35eSJamie Gritton } 2927effad35eSJamie Gritton 2928c861373bSJamie Gritton static void 2929c861373bSJamie Gritton prison_proc_free_not_last(struct prison *pr) 2930c861373bSJamie Gritton { 2931c861373bSJamie Gritton #ifdef INVARIANTS 2932c861373bSJamie Gritton int lastref; 2933c861373bSJamie Gritton 2934c861373bSJamie Gritton KASSERT(refcount_load(&pr->pr_uref) > 0, 2935c861373bSJamie Gritton ("Trying to free dead prison %p (jid=%d).", 2936c861373bSJamie Gritton pr, pr->pr_id)); 2937c861373bSJamie Gritton lastref = refcount_release(&pr->pr_uref); 2938c861373bSJamie Gritton KASSERT(!lastref, 2939c861373bSJamie Gritton ("prison_proc_free_not_last freed last uref on prison %p (jid=%d).", 2940c861373bSJamie Gritton pr, pr->pr_id)); 2941c861373bSJamie Gritton #else 2942c861373bSJamie Gritton refcount_release(&pr->pr_uref); 2943c861373bSJamie Gritton #endif 2944c861373bSJamie Gritton } 2945c861373bSJamie Gritton 29465ecb5444SMateusz Guzik void 29475ecb5444SMateusz Guzik prison_proc_link(struct prison *pr, struct proc *p) 29485ecb5444SMateusz Guzik { 29495ecb5444SMateusz Guzik 29505ecb5444SMateusz Guzik sx_assert(&allproc_lock, SA_XLOCKED); 29515ecb5444SMateusz Guzik LIST_INSERT_HEAD(&pr->pr_proclist, p, p_jaillist); 29525ecb5444SMateusz Guzik } 29535ecb5444SMateusz Guzik 29545ecb5444SMateusz Guzik void 29555ecb5444SMateusz Guzik prison_proc_unlink(struct prison *pr, struct proc *p) 29565ecb5444SMateusz Guzik { 29575ecb5444SMateusz Guzik 29585ecb5444SMateusz Guzik sx_assert(&allproc_lock, SA_XLOCKED); 29595ecb5444SMateusz Guzik LIST_REMOVE(p, p_jaillist); 29605ecb5444SMateusz Guzik } 29615ecb5444SMateusz Guzik 29625ecb5444SMateusz Guzik static void 29635ecb5444SMateusz Guzik prison_proc_relink(struct prison *opr, struct prison *npr, struct proc *p) 29645ecb5444SMateusz Guzik { 29655ecb5444SMateusz Guzik 29665ecb5444SMateusz Guzik sx_xlock(&allproc_lock); 29675ecb5444SMateusz Guzik prison_proc_unlink(opr, p); 29685ecb5444SMateusz Guzik prison_proc_link(npr, p); 29695ecb5444SMateusz Guzik sx_xunlock(&allproc_lock); 29705ecb5444SMateusz Guzik } 29715ecb5444SMateusz Guzik 2972effad35eSJamie Gritton /* 297373d9e52dSJamie Gritton * Complete a call to either prison_free or prison_proc_free. 297473d9e52dSJamie Gritton */ 2975c2cda609SPawel Jakub Dawidek static void 2976c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending) 2977c2cda609SPawel Jakub Dawidek { 297873d9e52dSJamie Gritton struct prison *pr = context; 2979f7496dcaSJamie Gritton int drflags; 2980b38ff370SJamie Gritton 2981effad35eSJamie Gritton /* 29821158508aSJamie Gritton * This could be called to release the last reference, or the last 29831158508aSJamie Gritton * user reference (plus the reference held in prison_proc_free). 2984effad35eSJamie Gritton */ 2985f7496dcaSJamie Gritton drflags = prison_lock_xlock(pr, PD_DEREF); 29861158508aSJamie Gritton if (pr->pr_flags & PR_COMPLETE_PROC) { 29871158508aSJamie Gritton pr->pr_flags &= ~PR_COMPLETE_PROC; 2988f7496dcaSJamie Gritton drflags |= PD_DEUREF; 29891158508aSJamie Gritton } 2990f7496dcaSJamie Gritton prison_deref(pr, drflags); 2991b38ff370SJamie Gritton } 2992b38ff370SJamie Gritton 29935ecb5444SMateusz Guzik static void 29945ecb5444SMateusz Guzik prison_kill_processes_cb(struct proc *p, void *arg __unused) 29955ecb5444SMateusz Guzik { 29965ecb5444SMateusz Guzik 29975ecb5444SMateusz Guzik kern_psignal(p, SIGKILL); 29985ecb5444SMateusz Guzik } 29995ecb5444SMateusz Guzik 30005ecb5444SMateusz Guzik /* 30015ecb5444SMateusz Guzik * Note the iteration does not guarantee acting on all processes. 30025ecb5444SMateusz Guzik * Most notably there may be fork or jail_attach in progress. 30035ecb5444SMateusz Guzik */ 30045ecb5444SMateusz Guzik void 30055ecb5444SMateusz Guzik prison_proc_iterate(struct prison *pr, void (*cb)(struct proc *, void *), 30065ecb5444SMateusz Guzik void *cbarg) 30075ecb5444SMateusz Guzik { 30085ecb5444SMateusz Guzik struct prison *ppr; 30095ecb5444SMateusz Guzik struct proc *p; 30105ecb5444SMateusz Guzik 30115ecb5444SMateusz Guzik if (atomic_load_int(&pr->pr_childcount) == 0) { 30125ecb5444SMateusz Guzik sx_slock(&allproc_lock); 30135ecb5444SMateusz Guzik LIST_FOREACH(p, &pr->pr_proclist, p_jaillist) { 30145ecb5444SMateusz Guzik if (p->p_state == PRS_NEW) 30155ecb5444SMateusz Guzik continue; 30165ecb5444SMateusz Guzik PROC_LOCK(p); 30175ecb5444SMateusz Guzik cb(p, cbarg); 30185ecb5444SMateusz Guzik PROC_UNLOCK(p); 30195ecb5444SMateusz Guzik } 30205ecb5444SMateusz Guzik sx_sunlock(&allproc_lock); 30215ecb5444SMateusz Guzik if (atomic_load_int(&pr->pr_childcount) == 0) 30225ecb5444SMateusz Guzik return; 30235ecb5444SMateusz Guzik /* 30245ecb5444SMateusz Guzik * Some jails popped up during the iteration, fall through to a 30255ecb5444SMateusz Guzik * system-wide search. 30265ecb5444SMateusz Guzik */ 30275ecb5444SMateusz Guzik } 30285ecb5444SMateusz Guzik 30295ecb5444SMateusz Guzik sx_slock(&allproc_lock); 30305ecb5444SMateusz Guzik FOREACH_PROC_IN_SYSTEM(p) { 30315ecb5444SMateusz Guzik PROC_LOCK(p); 30325ecb5444SMateusz Guzik if (p->p_state != PRS_NEW && p->p_ucred != NULL) { 30335ecb5444SMateusz Guzik for (ppr = p->p_ucred->cr_prison; 30345ecb5444SMateusz Guzik ppr != &prison0; 30355ecb5444SMateusz Guzik ppr = ppr->pr_parent) { 30365ecb5444SMateusz Guzik if (ppr == pr) { 30375ecb5444SMateusz Guzik cb(p, cbarg); 30385ecb5444SMateusz Guzik break; 30395ecb5444SMateusz Guzik } 30405ecb5444SMateusz Guzik } 30415ecb5444SMateusz Guzik } 30425ecb5444SMateusz Guzik PROC_UNLOCK(p); 30435ecb5444SMateusz Guzik } 30445ecb5444SMateusz Guzik sx_sunlock(&allproc_lock); 30455ecb5444SMateusz Guzik } 30465ecb5444SMateusz Guzik 3047b38ff370SJamie Gritton /* 3048effad35eSJamie Gritton * Remove a prison reference and/or user reference (usually). 3049effad35eSJamie Gritton * This assumes context that allows sleeping (for allprison_lock), 3050effad35eSJamie Gritton * with no non-sleeping locks held, except perhaps the prison itself. 3051effad35eSJamie Gritton * If there are no more references, release and delist the prison. 3052effad35eSJamie Gritton * On completion, the prison lock and the allprison lock are both 3053effad35eSJamie Gritton * unlocked. 3054b38ff370SJamie Gritton */ 3055b38ff370SJamie Gritton static void 3056b38ff370SJamie Gritton prison_deref(struct prison *pr, int flags) 3057b38ff370SJamie Gritton { 30586e1d1bfcSJamie Gritton struct prisonlist freeprison; 3059c861373bSJamie Gritton struct prison *killpr, *rpr, *ppr, *tpr; 3060c2cda609SPawel Jakub Dawidek 3061c861373bSJamie Gritton killpr = NULL; 30626e1d1bfcSJamie Gritton TAILQ_INIT(&freeprison); 30636e1d1bfcSJamie Gritton /* 30646e1d1bfcSJamie Gritton * Release this prison as requested, which may cause its parent 30656e1d1bfcSJamie Gritton * to be released, and then maybe its grandparent, etc. 30666e1d1bfcSJamie Gritton */ 30670304c731SJamie Gritton for (;;) { 3068c861373bSJamie Gritton if (flags & PD_KILL) { 3069c861373bSJamie Gritton /* Kill the prison and its descendents. */ 3070589e4c1dSJamie Gritton KASSERT(pr != &prison0, 3071589e4c1dSJamie Gritton ("prison_deref trying to kill prison0")); 3072c861373bSJamie Gritton if (!(flags & PD_DEREF)) { 3073c861373bSJamie Gritton prison_hold(pr); 3074c861373bSJamie Gritton flags |= PD_DEREF; 3075c861373bSJamie Gritton } 3076c861373bSJamie Gritton flags = prison_lock_xlock(pr, flags); 3077c861373bSJamie Gritton prison_deref_kill(pr, &freeprison); 3078c861373bSJamie Gritton } 3079e6d5cb63SJamie Gritton if (flags & PD_DEUREF) { 3080f7496dcaSJamie Gritton /* Drop a user reference. */ 30816754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_uref) > 0, 308273d9e52dSJamie Gritton ("prison_deref PD_DEUREF on a dead prison (jid=%d)", 308373d9e52dSJamie Gritton pr->pr_id)); 3084f7496dcaSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_uref)) { 3085f7496dcaSJamie Gritton if (!(flags & PD_DEREF)) { 3086f7496dcaSJamie Gritton prison_hold(pr); 3087f7496dcaSJamie Gritton flags |= PD_DEREF; 3088f7496dcaSJamie Gritton } 3089f7496dcaSJamie Gritton flags = prison_lock_xlock(pr, flags); 30901158508aSJamie Gritton if (refcount_release(&pr->pr_uref) && 30911158508aSJamie Gritton pr->pr_state == PRISON_STATE_ALIVE) { 3092f7496dcaSJamie Gritton /* 3093f7496dcaSJamie Gritton * When the last user references goes, 3094f7496dcaSJamie Gritton * this becomes a dying prison. 3095f7496dcaSJamie Gritton */ 3096f7496dcaSJamie Gritton KASSERT( 3097f7496dcaSJamie Gritton refcount_load(&prison0.pr_uref) > 0, 30986754ae25SJamie Gritton ("prison0 pr_uref=0")); 30991158508aSJamie Gritton pr->pr_state = PRISON_STATE_DYING; 3100f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 3101f7496dcaSJamie Gritton flags &= ~PD_LOCKED; 3102a9f7455cSJamie Gritton prison_cleanup(pr); 3103f7496dcaSJamie Gritton } 3104f7496dcaSJamie Gritton } 3105f7496dcaSJamie Gritton } 3106c861373bSJamie Gritton if (flags & PD_KILL) { 3107c861373bSJamie Gritton /* 3108c861373bSJamie Gritton * Any remaining user references are probably processes 3109c861373bSJamie Gritton * that need to be killed, either in this prison or its 3110c861373bSJamie Gritton * descendants. 3111c861373bSJamie Gritton */ 3112c861373bSJamie Gritton if (refcount_load(&pr->pr_uref) > 0) 3113c861373bSJamie Gritton killpr = pr; 3114589e4c1dSJamie Gritton /* Make sure the parent prison doesn't get killed. */ 3115589e4c1dSJamie Gritton flags &= ~PD_KILL; 3116c861373bSJamie Gritton } 311773d9e52dSJamie Gritton if (flags & PD_DEREF) { 3118f7496dcaSJamie Gritton /* Drop a reference. */ 31196754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 312073d9e52dSJamie Gritton ("prison_deref PD_DEREF on a dead prison (jid=%d)", 312173d9e52dSJamie Gritton pr->pr_id)); 3122f7496dcaSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_ref)) { 3123f7496dcaSJamie Gritton flags = prison_lock_xlock(pr, flags); 3124f7496dcaSJamie Gritton if (refcount_release(&pr->pr_ref)) { 3125cc5fd8c7SJamie Gritton /* 3126f7496dcaSJamie Gritton * When the last reference goes, 3127f7496dcaSJamie Gritton * unlink the prison and set it aside. 3128cc5fd8c7SJamie Gritton */ 3129f7496dcaSJamie Gritton KASSERT( 3130f7496dcaSJamie Gritton refcount_load(&pr->pr_uref) == 0, 3131f7496dcaSJamie Gritton ("prison_deref: last ref, " 3132f7496dcaSJamie Gritton "but still has %d urefs (jid=%d)", 3133f7496dcaSJamie Gritton pr->pr_uref, pr->pr_id)); 3134f7496dcaSJamie Gritton KASSERT( 3135f7496dcaSJamie Gritton refcount_load(&prison0.pr_ref) != 0, 3136f7496dcaSJamie Gritton ("prison0 pr_ref=0")); 31371158508aSJamie Gritton pr->pr_state = PRISON_STATE_INVALID; 3138b38ff370SJamie Gritton TAILQ_REMOVE(&allprison, pr, pr_list); 31390304c731SJamie Gritton LIST_REMOVE(pr, pr_sibling); 3140f7496dcaSJamie Gritton TAILQ_INSERT_TAIL(&freeprison, pr, 3141f7496dcaSJamie Gritton pr_list); 3142f7496dcaSJamie Gritton for (ppr = pr->pr_parent; 3143f7496dcaSJamie Gritton ppr != NULL; 3144f7496dcaSJamie Gritton ppr = ppr->pr_parent) 3145f7496dcaSJamie Gritton ppr->pr_childcount--; 3146f7496dcaSJamie Gritton /* 3147f7496dcaSJamie Gritton * Removing a prison frees references 3148f7496dcaSJamie Gritton * from its parent. 3149f7496dcaSJamie Gritton */ 3150f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 3151f7496dcaSJamie Gritton flags &= ~PD_LOCKED; 31526e1d1bfcSJamie Gritton pr = pr->pr_parent; 31536e1d1bfcSJamie Gritton flags |= PD_DEREF | PD_DEUREF; 3154f7496dcaSJamie Gritton continue; 3155f7496dcaSJamie Gritton } 3156f7496dcaSJamie Gritton } 3157f7496dcaSJamie Gritton } 3158f7496dcaSJamie Gritton break; 31596e1d1bfcSJamie Gritton } 31606e1d1bfcSJamie Gritton 31616e1d1bfcSJamie Gritton /* Release all the prison locks. */ 3162f7496dcaSJamie Gritton if (flags & PD_LOCKED) 3163f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 31646e1d1bfcSJamie Gritton if (flags & PD_LIST_SLOCKED) 31656e1d1bfcSJamie Gritton sx_sunlock(&allprison_lock); 31666e1d1bfcSJamie Gritton else if (flags & PD_LIST_XLOCKED) 31676e1d1bfcSJamie Gritton sx_xunlock(&allprison_lock); 31686e1d1bfcSJamie Gritton 3169c861373bSJamie Gritton /* Kill any processes attached to a killed prison. */ 31705ecb5444SMateusz Guzik if (killpr != NULL) 31715ecb5444SMateusz Guzik prison_proc_iterate(killpr, prison_kill_processes_cb, NULL); 3172c861373bSJamie Gritton 31736e1d1bfcSJamie Gritton /* 31746e1d1bfcSJamie Gritton * Finish removing any unreferenced prisons, which couldn't happen 31756e1d1bfcSJamie Gritton * while allprison_lock was held (to avoid a LOR on vrele). 31766e1d1bfcSJamie Gritton */ 31776e1d1bfcSJamie Gritton TAILQ_FOREACH_SAFE(rpr, &freeprison, pr_list, tpr) { 31786e1d1bfcSJamie Gritton #ifdef VIMAGE 31796e1d1bfcSJamie Gritton if (rpr->pr_vnet != rpr->pr_parent->pr_vnet) 31806e1d1bfcSJamie Gritton vnet_destroy(rpr->pr_vnet); 31816e1d1bfcSJamie Gritton #endif 31826e1d1bfcSJamie Gritton if (rpr->pr_root != NULL) 31836e1d1bfcSJamie Gritton vrele(rpr->pr_root); 31846e1d1bfcSJamie Gritton mtx_destroy(&rpr->pr_mtx); 31856e1d1bfcSJamie Gritton #ifdef INET 3186eb8dcdeaSGleb Smirnoff prison_ip_free(rpr->pr_addrs[PR_INET]); 31876e1d1bfcSJamie Gritton #endif 31886e1d1bfcSJamie Gritton #ifdef INET6 3189eb8dcdeaSGleb Smirnoff prison_ip_free(rpr->pr_addrs[PR_INET6]); 31906e1d1bfcSJamie Gritton #endif 31916e1d1bfcSJamie Gritton if (rpr->pr_cpuset != NULL) 31926e1d1bfcSJamie Gritton cpuset_rel(rpr->pr_cpuset); 31936e1d1bfcSJamie Gritton osd_jail_exit(rpr); 31946e1d1bfcSJamie Gritton #ifdef RACCT 31956e1d1bfcSJamie Gritton if (racct_enable) 31966e1d1bfcSJamie Gritton prison_racct_detach(rpr); 31976e1d1bfcSJamie Gritton #endif 3198f7496dcaSJamie Gritton TAILQ_REMOVE(&freeprison, rpr, pr_list); 31996e1d1bfcSJamie Gritton free(rpr, M_PRISON); 32000304c731SJamie Gritton } 3201b3059e09SRobert Watson } 3202b3059e09SRobert Watson 3203413628a7SBjoern A. Zeeb /* 3204c861373bSJamie Gritton * Kill the prison and its descendants. Mark them as dying, clear the 3205c861373bSJamie Gritton * persist flag, and call module remove methods. 3206c861373bSJamie Gritton */ 3207c861373bSJamie Gritton static void 3208c861373bSJamie Gritton prison_deref_kill(struct prison *pr, struct prisonlist *freeprison) 3209c861373bSJamie Gritton { 3210c861373bSJamie Gritton struct prison *cpr, *ppr, *rpr; 3211c861373bSJamie Gritton bool descend; 3212c861373bSJamie Gritton 3213c861373bSJamie Gritton /* 3214c861373bSJamie Gritton * Unlike the descendants, the target prison can be killed 3215c861373bSJamie Gritton * even if it is currently dying. This is useful for failed 3216c861373bSJamie Gritton * creation in jail_set(2). 3217c861373bSJamie Gritton */ 3218c861373bSJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 3219c861373bSJamie Gritton ("Trying to kill dead prison %p (jid=%d).", 3220c861373bSJamie Gritton pr, pr->pr_id)); 3221c861373bSJamie Gritton refcount_acquire(&pr->pr_uref); 3222c861373bSJamie Gritton pr->pr_state = PRISON_STATE_DYING; 3223c861373bSJamie Gritton mtx_unlock(&pr->pr_mtx); 3224c861373bSJamie Gritton 3225c861373bSJamie Gritton rpr = NULL; 3226c861373bSJamie Gritton FOREACH_PRISON_DESCENDANT_PRE_POST(pr, cpr, descend) { 3227c861373bSJamie Gritton if (descend) { 3228c861373bSJamie Gritton if (!prison_isalive(cpr)) { 3229c861373bSJamie Gritton descend = false; 3230c861373bSJamie Gritton continue; 3231c861373bSJamie Gritton } 3232c861373bSJamie Gritton prison_hold(cpr); 3233c861373bSJamie Gritton prison_proc_hold(cpr); 3234c861373bSJamie Gritton mtx_lock(&cpr->pr_mtx); 3235c861373bSJamie Gritton cpr->pr_state = PRISON_STATE_DYING; 3236c861373bSJamie Gritton cpr->pr_flags |= PR_REMOVE; 3237c861373bSJamie Gritton mtx_unlock(&cpr->pr_mtx); 3238c861373bSJamie Gritton continue; 3239c861373bSJamie Gritton } 3240c861373bSJamie Gritton if (!(cpr->pr_flags & PR_REMOVE)) 3241c861373bSJamie Gritton continue; 3242a9f7455cSJamie Gritton prison_cleanup(cpr); 3243c861373bSJamie Gritton mtx_lock(&cpr->pr_mtx); 3244c861373bSJamie Gritton cpr->pr_flags &= ~PR_REMOVE; 3245c861373bSJamie Gritton if (cpr->pr_flags & PR_PERSIST) { 3246c861373bSJamie Gritton cpr->pr_flags &= ~PR_PERSIST; 3247c861373bSJamie Gritton prison_proc_free_not_last(cpr); 3248c861373bSJamie Gritton prison_free_not_last(cpr); 3249c861373bSJamie Gritton } 3250c861373bSJamie Gritton (void)refcount_release(&cpr->pr_uref); 3251c861373bSJamie Gritton if (refcount_release(&cpr->pr_ref)) { 3252c861373bSJamie Gritton /* 3253c861373bSJamie Gritton * When the last reference goes, unlink the prison 3254c861373bSJamie Gritton * and set it aside for prison_deref() to handle. 3255c861373bSJamie Gritton * Delay unlinking the sibling list to keep the loop 3256c861373bSJamie Gritton * safe. 3257c861373bSJamie Gritton */ 3258c861373bSJamie Gritton if (rpr != NULL) 3259c861373bSJamie Gritton LIST_REMOVE(rpr, pr_sibling); 3260c861373bSJamie Gritton rpr = cpr; 3261c861373bSJamie Gritton rpr->pr_state = PRISON_STATE_INVALID; 3262c861373bSJamie Gritton TAILQ_REMOVE(&allprison, rpr, pr_list); 3263c861373bSJamie Gritton TAILQ_INSERT_TAIL(freeprison, rpr, pr_list); 3264c861373bSJamie Gritton /* 3265c861373bSJamie Gritton * Removing a prison frees references from its parent. 3266c861373bSJamie Gritton */ 3267c861373bSJamie Gritton ppr = rpr->pr_parent; 3268c861373bSJamie Gritton prison_proc_free_not_last(ppr); 3269c861373bSJamie Gritton prison_free_not_last(ppr); 3270c861373bSJamie Gritton for (; ppr != NULL; ppr = ppr->pr_parent) 3271c861373bSJamie Gritton ppr->pr_childcount--; 3272c861373bSJamie Gritton } 3273c861373bSJamie Gritton mtx_unlock(&cpr->pr_mtx); 3274c861373bSJamie Gritton } 3275c861373bSJamie Gritton if (rpr != NULL) 3276c861373bSJamie Gritton LIST_REMOVE(rpr, pr_sibling); 3277c861373bSJamie Gritton 3278a9f7455cSJamie Gritton prison_cleanup(pr); 3279c861373bSJamie Gritton mtx_lock(&pr->pr_mtx); 3280c861373bSJamie Gritton if (pr->pr_flags & PR_PERSIST) { 3281c861373bSJamie Gritton pr->pr_flags &= ~PR_PERSIST; 3282c861373bSJamie Gritton prison_proc_free_not_last(pr); 3283c861373bSJamie Gritton prison_free_not_last(pr); 3284c861373bSJamie Gritton } 3285c861373bSJamie Gritton (void)refcount_release(&pr->pr_uref); 3286c861373bSJamie Gritton } 3287c861373bSJamie Gritton 3288c861373bSJamie Gritton /* 3289f7496dcaSJamie Gritton * Given the current locking state in the flags, make sure allprison_lock 3290f7496dcaSJamie Gritton * is held exclusive, and the prison is locked. Return flags indicating 3291f7496dcaSJamie Gritton * the new state. 3292f7496dcaSJamie Gritton */ 3293f7496dcaSJamie Gritton static int 3294f7496dcaSJamie Gritton prison_lock_xlock(struct prison *pr, int flags) 3295f7496dcaSJamie Gritton { 3296f7496dcaSJamie Gritton 3297f7496dcaSJamie Gritton if (!(flags & PD_LIST_XLOCKED)) { 3298f7496dcaSJamie Gritton /* 3299f7496dcaSJamie Gritton * Get allprison_lock, which may be an upgrade, 3300f7496dcaSJamie Gritton * and may require unlocking the prison. 3301f7496dcaSJamie Gritton */ 3302f7496dcaSJamie Gritton if (flags & PD_LOCKED) { 3303f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 3304f7496dcaSJamie Gritton flags &= ~PD_LOCKED; 3305f7496dcaSJamie Gritton } 3306f7496dcaSJamie Gritton if (flags & PD_LIST_SLOCKED) { 3307f7496dcaSJamie Gritton if (!sx_try_upgrade(&allprison_lock)) { 3308f7496dcaSJamie Gritton sx_sunlock(&allprison_lock); 3309f7496dcaSJamie Gritton sx_xlock(&allprison_lock); 3310f7496dcaSJamie Gritton } 3311f7496dcaSJamie Gritton flags &= ~PD_LIST_SLOCKED; 3312f7496dcaSJamie Gritton } else 3313f7496dcaSJamie Gritton sx_xlock(&allprison_lock); 3314f7496dcaSJamie Gritton flags |= PD_LIST_XLOCKED; 3315f7496dcaSJamie Gritton } 3316f7496dcaSJamie Gritton if (!(flags & PD_LOCKED)) { 3317f7496dcaSJamie Gritton /* Lock the prison mutex. */ 3318f7496dcaSJamie Gritton mtx_lock(&pr->pr_mtx); 3319f7496dcaSJamie Gritton flags |= PD_LOCKED; 3320f7496dcaSJamie Gritton } 3321f7496dcaSJamie Gritton return flags; 3322f7496dcaSJamie Gritton } 3323f7496dcaSJamie Gritton 3324f7496dcaSJamie Gritton /* 3325a9f7455cSJamie Gritton * Release a prison's resources when it starts dying (when the last user 3326a9f7455cSJamie Gritton * reference is dropped, or when it is killed). 3327a9f7455cSJamie Gritton */ 3328a9f7455cSJamie Gritton static void 3329a9f7455cSJamie Gritton prison_cleanup(struct prison *pr) 3330a9f7455cSJamie Gritton { 3331a9f7455cSJamie Gritton sx_assert(&allprison_lock, SA_XLOCKED); 3332a9f7455cSJamie Gritton mtx_assert(&pr->pr_mtx, MA_NOTOWNED); 33337060da62SJamie Gritton shm_remove_prison(pr); 3334a9f7455cSJamie Gritton (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL); 3335a9f7455cSJamie Gritton } 3336a9f7455cSJamie Gritton 3337a9f7455cSJamie Gritton /* 33380fe74ae6SJamie Gritton * Set or clear a permission bit in the pr_allow field, passing restrictions 33390fe74ae6SJamie Gritton * (cleared permission) down to child jails. 33400fe74ae6SJamie Gritton */ 33410fe74ae6SJamie Gritton void 33420fe74ae6SJamie Gritton prison_set_allow(struct ucred *cred, unsigned flag, int enable) 33430fe74ae6SJamie Gritton { 33440fe74ae6SJamie Gritton struct prison *pr; 33450fe74ae6SJamie Gritton 33460fe74ae6SJamie Gritton pr = cred->cr_prison; 33470fe74ae6SJamie Gritton sx_slock(&allprison_lock); 33480fe74ae6SJamie Gritton mtx_lock(&pr->pr_mtx); 33490fe74ae6SJamie Gritton prison_set_allow_locked(pr, flag, enable); 33500fe74ae6SJamie Gritton mtx_unlock(&pr->pr_mtx); 33510fe74ae6SJamie Gritton sx_sunlock(&allprison_lock); 33520fe74ae6SJamie Gritton } 33530fe74ae6SJamie Gritton 33540fe74ae6SJamie Gritton static void 33550fe74ae6SJamie Gritton prison_set_allow_locked(struct prison *pr, unsigned flag, int enable) 33560fe74ae6SJamie Gritton { 33570fe74ae6SJamie Gritton struct prison *cpr; 33580fe74ae6SJamie Gritton int descend; 33590fe74ae6SJamie Gritton 33600fe74ae6SJamie Gritton if (enable != 0) 33610fe74ae6SJamie Gritton pr->pr_allow |= flag; 33620fe74ae6SJamie Gritton else { 33630fe74ae6SJamie Gritton pr->pr_allow &= ~flag; 33640fe74ae6SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, cpr, descend) 33650fe74ae6SJamie Gritton cpr->pr_allow &= ~flag; 33660fe74ae6SJamie Gritton } 33670fe74ae6SJamie Gritton } 33680fe74ae6SJamie Gritton 33690fe74ae6SJamie Gritton /* 3370ca04ba64SJamie Gritton * Check if a jail supports the given address family. 3371ca04ba64SJamie Gritton * 3372ca04ba64SJamie Gritton * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT 3373ca04ba64SJamie Gritton * if not. 3374ca04ba64SJamie Gritton */ 3375ca04ba64SJamie Gritton int 3376ca04ba64SJamie Gritton prison_check_af(struct ucred *cred, int af) 3377ca04ba64SJamie Gritton { 33780304c731SJamie Gritton struct prison *pr; 3379ca04ba64SJamie Gritton int error; 3380ca04ba64SJamie Gritton 3381ca04ba64SJamie Gritton KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3382ca04ba64SJamie Gritton 33830304c731SJamie Gritton pr = cred->cr_prison; 3384499650a0SJamie Gritton #ifdef VIMAGE 33856bb79563SJamie Gritton /* Prisons with their own network stack are not limited. */ 3386de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 33876bb79563SJamie Gritton return (0); 3388499650a0SJamie Gritton #endif 33896bb79563SJamie Gritton 3390ca04ba64SJamie Gritton error = 0; 3391ca04ba64SJamie Gritton switch (af) 3392ca04ba64SJamie Gritton { 3393ca04ba64SJamie Gritton #ifdef INET 3394ca04ba64SJamie Gritton case AF_INET: 33950304c731SJamie Gritton if (pr->pr_flags & PR_IP4) 33960304c731SJamie Gritton { 33970304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 3398eb8dcdeaSGleb Smirnoff if ((pr->pr_flags & PR_IP4) && 3399eb8dcdeaSGleb Smirnoff pr->pr_addrs[PR_INET] == NULL) 3400ca04ba64SJamie Gritton error = EAFNOSUPPORT; 34010304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 34020304c731SJamie Gritton } 3403ca04ba64SJamie Gritton break; 3404ca04ba64SJamie Gritton #endif 3405ca04ba64SJamie Gritton #ifdef INET6 3406ca04ba64SJamie Gritton case AF_INET6: 34070304c731SJamie Gritton if (pr->pr_flags & PR_IP6) 34080304c731SJamie Gritton { 34090304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 3410eb8dcdeaSGleb Smirnoff if ((pr->pr_flags & PR_IP6) && 3411eb8dcdeaSGleb Smirnoff pr->pr_addrs[PR_INET6] == NULL) 3412ca04ba64SJamie Gritton error = EAFNOSUPPORT; 34130304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 34140304c731SJamie Gritton } 3415ca04ba64SJamie Gritton break; 3416ca04ba64SJamie Gritton #endif 3417ca04ba64SJamie Gritton case AF_LOCAL: 3418ca04ba64SJamie Gritton case AF_ROUTE: 3419ca04ba64SJamie Gritton break; 3420ca04ba64SJamie Gritton default: 34210304c731SJamie Gritton if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF)) 3422ca04ba64SJamie Gritton error = EAFNOSUPPORT; 3423ca04ba64SJamie Gritton } 3424ca04ba64SJamie Gritton return (error); 3425ca04ba64SJamie Gritton } 3426ca04ba64SJamie Gritton 3427ca04ba64SJamie Gritton /* 3428413628a7SBjoern A. Zeeb * Check if given address belongs to the jail referenced by cred (wrapper to 3429413628a7SBjoern A. Zeeb * prison_check_ip[46]). 3430413628a7SBjoern A. Zeeb * 34310304c731SJamie Gritton * Returns 0 if jail doesn't restrict the address family or if address belongs 34320304c731SJamie Gritton * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if 34330304c731SJamie Gritton * the jail doesn't allow the address family. IPv4 Address passed in in NBO. 3434413628a7SBjoern A. Zeeb */ 3435413628a7SBjoern A. Zeeb int 3436c83dda36SAlexander V. Chernikov prison_if(struct ucred *cred, const struct sockaddr *sa) 343775c13541SPoul-Henning Kamp { 3438413628a7SBjoern A. Zeeb #ifdef INET 3439c83dda36SAlexander V. Chernikov const struct sockaddr_in *sai; 3440413628a7SBjoern A. Zeeb #endif 3441413628a7SBjoern A. Zeeb #ifdef INET6 3442c83dda36SAlexander V. Chernikov const struct sockaddr_in6 *sai6; 3443413628a7SBjoern A. Zeeb #endif 3444b89e82ddSJamie Gritton int error; 344575c13541SPoul-Henning Kamp 3446413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3447413628a7SBjoern A. Zeeb KASSERT(sa != NULL, ("%s: sa is NULL", __func__)); 3448413628a7SBjoern A. Zeeb 3449de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE 3450de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 3451de0bd6f7SBjoern A. Zeeb return (0); 3452de0bd6f7SBjoern A. Zeeb #endif 3453de0bd6f7SBjoern A. Zeeb 3454b89e82ddSJamie Gritton error = 0; 3455413628a7SBjoern A. Zeeb switch (sa->sa_family) 3456413628a7SBjoern A. Zeeb { 3457413628a7SBjoern A. Zeeb #ifdef INET 3458413628a7SBjoern A. Zeeb case AF_INET: 3459c83dda36SAlexander V. Chernikov sai = (const struct sockaddr_in *)sa; 3460b89e82ddSJamie Gritton error = prison_check_ip4(cred, &sai->sin_addr); 3461413628a7SBjoern A. Zeeb break; 3462413628a7SBjoern A. Zeeb #endif 3463413628a7SBjoern A. Zeeb #ifdef INET6 3464413628a7SBjoern A. Zeeb case AF_INET6: 3465c83dda36SAlexander V. Chernikov sai6 = (const struct sockaddr_in6 *)sa; 3466b89e82ddSJamie Gritton error = prison_check_ip6(cred, &sai6->sin6_addr); 3467413628a7SBjoern A. Zeeb break; 3468413628a7SBjoern A. Zeeb #endif 3469413628a7SBjoern A. Zeeb default: 34700304c731SJamie Gritton if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF)) 3471b89e82ddSJamie Gritton error = EAFNOSUPPORT; 3472413628a7SBjoern A. Zeeb } 3473b89e82ddSJamie Gritton return (error); 347475c13541SPoul-Henning Kamp } 347591421ba2SRobert Watson 347691421ba2SRobert Watson /* 347791421ba2SRobert Watson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH. 347891421ba2SRobert Watson */ 347991421ba2SRobert Watson int 34809ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2) 348191421ba2SRobert Watson { 348291421ba2SRobert Watson 34830304c731SJamie Gritton return ((cred1->cr_prison == cred2->cr_prison || 34840304c731SJamie Gritton prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH); 34850304c731SJamie Gritton } 348691421ba2SRobert Watson 34870304c731SJamie Gritton /* 3488bba7a2e8SRick Macklem * For mountd/nfsd to run within a prison, it must be: 3489bba7a2e8SRick Macklem * - A vnet prison. 3490bba7a2e8SRick Macklem * - PR_ALLOW_NFSD must be set on it. 3491bba7a2e8SRick Macklem * - The root directory (pr_root) of the prison must be 3492bba7a2e8SRick Macklem * a file system mount point, so the mountd can hang 3493bba7a2e8SRick Macklem * export information on it. 349499187c3aSRick Macklem * - The prison's enforce_statfs cannot be 0, so that 349599187c3aSRick Macklem * mountd(8) can do exports. 3496bba7a2e8SRick Macklem */ 3497bba7a2e8SRick Macklem bool 3498bba7a2e8SRick Macklem prison_check_nfsd(struct ucred *cred) 3499bba7a2e8SRick Macklem { 3500bba7a2e8SRick Macklem 3501bba7a2e8SRick Macklem if (jailed_without_vnet(cred)) 3502bba7a2e8SRick Macklem return (false); 3503bba7a2e8SRick Macklem if (!prison_allow(cred, PR_ALLOW_NFSD)) 3504bba7a2e8SRick Macklem return (false); 3505bba7a2e8SRick Macklem if ((cred->cr_prison->pr_root->v_vflag & VV_ROOT) == 0) 3506bba7a2e8SRick Macklem return (false); 350799187c3aSRick Macklem if (cred->cr_prison->pr_enforce_statfs == 0) 350899187c3aSRick Macklem return (false); 3509bba7a2e8SRick Macklem return (true); 3510bba7a2e8SRick Macklem } 3511bba7a2e8SRick Macklem 3512bba7a2e8SRick Macklem /* 35130304c731SJamie Gritton * Return 1 if p2 is a child of p1, otherwise 0. 35140304c731SJamie Gritton */ 35150304c731SJamie Gritton int 35160304c731SJamie Gritton prison_ischild(struct prison *pr1, struct prison *pr2) 35170304c731SJamie Gritton { 35180304c731SJamie Gritton 35190304c731SJamie Gritton for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent) 35200304c731SJamie Gritton if (pr1 == pr2) 35210304c731SJamie Gritton return (1); 352291421ba2SRobert Watson return (0); 352391421ba2SRobert Watson } 352491421ba2SRobert Watson 352591421ba2SRobert Watson /* 3526f7496dcaSJamie Gritton * Return true if the prison is currently alive. A prison is alive if it 3527f7496dcaSJamie Gritton * holds user references and it isn't being removed. 352876ad42abSJamie Gritton */ 352976ad42abSJamie Gritton bool 3530eb8dcdeaSGleb Smirnoff prison_isalive(const struct prison *pr) 353176ad42abSJamie Gritton { 353276ad42abSJamie Gritton 35331158508aSJamie Gritton if (__predict_false(pr->pr_state != PRISON_STATE_ALIVE)) 3534cc7b7306SJamie Gritton return (false); 353576ad42abSJamie Gritton return (true); 353676ad42abSJamie Gritton } 353776ad42abSJamie Gritton 353876ad42abSJamie Gritton /* 353976ad42abSJamie Gritton * Return true if the prison is currently valid. A prison is valid if it has 354076ad42abSJamie Gritton * been fully created, and is not being destroyed. Note that dying prisons 3541f7496dcaSJamie Gritton * are still considered valid. Invalid prisons won't be found under normal 3542f7496dcaSJamie Gritton * circumstances, as they're only put in that state by functions that have 3543f7496dcaSJamie Gritton * an exclusive hold on allprison_lock. 354476ad42abSJamie Gritton */ 354576ad42abSJamie Gritton bool 354676ad42abSJamie Gritton prison_isvalid(struct prison *pr) 354776ad42abSJamie Gritton { 354876ad42abSJamie Gritton 35491158508aSJamie Gritton if (__predict_false(pr->pr_state == PRISON_STATE_INVALID)) 35501158508aSJamie Gritton return (false); 35516754ae25SJamie Gritton if (__predict_false(refcount_load(&pr->pr_ref) == 0)) 355276ad42abSJamie Gritton return (false); 355376ad42abSJamie Gritton return (true); 355476ad42abSJamie Gritton } 355576ad42abSJamie Gritton 355676ad42abSJamie Gritton /* 3557de0bd6f7SBjoern A. Zeeb * Return 1 if the passed credential is in a jail and that jail does not 3558de0bd6f7SBjoern A. Zeeb * have its own virtual network stack, otherwise 0. 3559de0bd6f7SBjoern A. Zeeb */ 3560de0bd6f7SBjoern A. Zeeb int 3561de0bd6f7SBjoern A. Zeeb jailed_without_vnet(struct ucred *cred) 3562de0bd6f7SBjoern A. Zeeb { 3563de0bd6f7SBjoern A. Zeeb 3564de0bd6f7SBjoern A. Zeeb if (!jailed(cred)) 3565de0bd6f7SBjoern A. Zeeb return (0); 3566de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE 3567de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 3568de0bd6f7SBjoern A. Zeeb return (0); 3569de0bd6f7SBjoern A. Zeeb #endif 3570de0bd6f7SBjoern A. Zeeb 3571de0bd6f7SBjoern A. Zeeb return (1); 3572de0bd6f7SBjoern A. Zeeb } 3573de0bd6f7SBjoern A. Zeeb 3574de0bd6f7SBjoern A. Zeeb /* 35757455b100SJamie Gritton * Return the correct hostname (domainname, et al) for the passed credential. 35769484d0c0SRobert Drehmel */ 3577ad1ff099SRobert Drehmel void 35789ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size) 35799484d0c0SRobert Drehmel { 358076ca6f88SJamie Gritton struct prison *pr; 35819484d0c0SRobert Drehmel 35827455b100SJamie Gritton /* 35837455b100SJamie Gritton * A NULL credential can be used to shortcut to the physical 35847455b100SJamie Gritton * system's hostname. 35857455b100SJamie Gritton */ 358676ca6f88SJamie Gritton pr = (cred != NULL) ? cred->cr_prison : &prison0; 358776ca6f88SJamie Gritton mtx_lock(&pr->pr_mtx); 3588c1f19219SJamie Gritton strlcpy(buf, pr->pr_hostname, size); 358976ca6f88SJamie Gritton mtx_unlock(&pr->pr_mtx); 35909484d0c0SRobert Drehmel } 3591fd7a8150SMike Barcroft 35927455b100SJamie Gritton void 35937455b100SJamie Gritton getcreddomainname(struct ucred *cred, char *buf, size_t size) 35947455b100SJamie Gritton { 35957455b100SJamie Gritton 35967455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 3597c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_domainname, size); 35987455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 35997455b100SJamie Gritton } 36007455b100SJamie Gritton 36017455b100SJamie Gritton void 36027455b100SJamie Gritton getcredhostuuid(struct ucred *cred, char *buf, size_t size) 36037455b100SJamie Gritton { 36047455b100SJamie Gritton 36057455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 3606c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_hostuuid, size); 36077455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 36087455b100SJamie Gritton } 36097455b100SJamie Gritton 36107455b100SJamie Gritton void 36117455b100SJamie Gritton getcredhostid(struct ucred *cred, unsigned long *hostid) 36127455b100SJamie Gritton { 36137455b100SJamie Gritton 36147455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 36157455b100SJamie Gritton *hostid = cred->cr_prison->pr_hostid; 36167455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 36177455b100SJamie Gritton } 36187455b100SJamie Gritton 36193f8bc99cSKristof Provost void 36203f8bc99cSKristof Provost getjailname(struct ucred *cred, char *name, size_t len) 36213f8bc99cSKristof Provost { 36223f8bc99cSKristof Provost 36233f8bc99cSKristof Provost mtx_lock(&cred->cr_prison->pr_mtx); 36243f8bc99cSKristof Provost strlcpy(name, cred->cr_prison->pr_name, len); 36253f8bc99cSKristof Provost mtx_unlock(&cred->cr_prison->pr_mtx); 36263f8bc99cSKristof Provost } 36273f8bc99cSKristof Provost 3628eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE 3629eb79e1c7SBjoern A. Zeeb /* 3630eb79e1c7SBjoern A. Zeeb * Determine whether the prison represented by cred owns 3631eb79e1c7SBjoern A. Zeeb * its vnet rather than having it inherited. 3632eb79e1c7SBjoern A. Zeeb * 3633eb79e1c7SBjoern A. Zeeb * Returns 1 in case the prison owns the vnet, 0 otherwise. 3634eb79e1c7SBjoern A. Zeeb */ 3635eb79e1c7SBjoern A. Zeeb int 3636eb79e1c7SBjoern A. Zeeb prison_owns_vnet(struct ucred *cred) 3637eb79e1c7SBjoern A. Zeeb { 3638eb79e1c7SBjoern A. Zeeb 3639eb79e1c7SBjoern A. Zeeb /* 3640eb79e1c7SBjoern A. Zeeb * vnets cannot be added/removed after jail creation, 3641eb79e1c7SBjoern A. Zeeb * so no need to lock here. 3642eb79e1c7SBjoern A. Zeeb */ 3643eb79e1c7SBjoern A. Zeeb return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0); 3644eb79e1c7SBjoern A. Zeeb } 3645eb79e1c7SBjoern A. Zeeb #endif 3646eb79e1c7SBjoern A. Zeeb 3647f08df373SRobert Watson /* 3648820a0de9SPawel Jakub Dawidek * Determine whether the subject represented by cred can "see" 3649820a0de9SPawel Jakub Dawidek * status of a mount point. 3650820a0de9SPawel Jakub Dawidek * Returns: 0 for permitted, ENOENT otherwise. 3651820a0de9SPawel Jakub Dawidek * XXX: This function should be called cr_canseemount() and should be 3652820a0de9SPawel Jakub Dawidek * placed in kern_prot.c. 3653f08df373SRobert Watson */ 3654f08df373SRobert Watson int 3655820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp) 3656f08df373SRobert Watson { 3657820a0de9SPawel Jakub Dawidek struct prison *pr; 3658820a0de9SPawel Jakub Dawidek struct statfs *sp; 3659820a0de9SPawel Jakub Dawidek size_t len; 3660f08df373SRobert Watson 3661820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 36620304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 36630304c731SJamie Gritton return (0); 3664820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) 3665820a0de9SPawel Jakub Dawidek return (0); 36660304c731SJamie Gritton if (pr->pr_enforce_statfs == 2) 3667820a0de9SPawel Jakub Dawidek return (ENOENT); 3668820a0de9SPawel Jakub Dawidek /* 3669820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 3670820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 3671820a0de9SPawel Jakub Dawidek * This is ugly check, but this is the only situation when jail's 3672820a0de9SPawel Jakub Dawidek * directory ends with '/'. 3673820a0de9SPawel Jakub Dawidek */ 3674820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 3675820a0de9SPawel Jakub Dawidek return (0); 3676820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 3677820a0de9SPawel Jakub Dawidek sp = &mp->mnt_stat; 3678820a0de9SPawel Jakub Dawidek if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0) 3679820a0de9SPawel Jakub Dawidek return (ENOENT); 3680820a0de9SPawel Jakub Dawidek /* 3681820a0de9SPawel Jakub Dawidek * Be sure that we don't have situation where jail's root directory 3682820a0de9SPawel Jakub Dawidek * is "/some/path" and mount point is "/some/pathpath". 3683820a0de9SPawel Jakub Dawidek */ 3684820a0de9SPawel Jakub Dawidek if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/') 3685820a0de9SPawel Jakub Dawidek return (ENOENT); 3686f08df373SRobert Watson return (0); 3687f08df373SRobert Watson } 3688820a0de9SPawel Jakub Dawidek 3689820a0de9SPawel Jakub Dawidek void 3690820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp) 3691820a0de9SPawel Jakub Dawidek { 3692820a0de9SPawel Jakub Dawidek char jpath[MAXPATHLEN]; 3693820a0de9SPawel Jakub Dawidek struct prison *pr; 3694820a0de9SPawel Jakub Dawidek size_t len; 3695820a0de9SPawel Jakub Dawidek 3696820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 36970304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 36980304c731SJamie Gritton return; 3699820a0de9SPawel Jakub Dawidek if (prison_canseemount(cred, mp) != 0) { 3700820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3701820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, "[restricted]", 3702820a0de9SPawel Jakub Dawidek sizeof(sp->f_mntonname)); 3703820a0de9SPawel Jakub Dawidek return; 3704820a0de9SPawel Jakub Dawidek } 3705820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) { 3706820a0de9SPawel Jakub Dawidek /* 3707820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3708820a0de9SPawel Jakub Dawidek * the valid path left there. 3709820a0de9SPawel Jakub Dawidek */ 3710820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3711820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3712820a0de9SPawel Jakub Dawidek return; 3713820a0de9SPawel Jakub Dawidek } 3714820a0de9SPawel Jakub Dawidek /* 3715820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 3716820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 3717820a0de9SPawel Jakub Dawidek */ 3718820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 3719820a0de9SPawel Jakub Dawidek return; 3720820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 3721820a0de9SPawel Jakub Dawidek strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath)); 3722820a0de9SPawel Jakub Dawidek /* 3723820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3724820a0de9SPawel Jakub Dawidek * the valid path left there. 3725820a0de9SPawel Jakub Dawidek */ 3726820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3727820a0de9SPawel Jakub Dawidek if (*jpath == '\0') { 3728820a0de9SPawel Jakub Dawidek /* Should never happen. */ 3729820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3730820a0de9SPawel Jakub Dawidek } else { 3731820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname)); 3732820a0de9SPawel Jakub Dawidek } 3733f08df373SRobert Watson } 3734f08df373SRobert Watson 3735800c9408SRobert Watson /* 3736800c9408SRobert Watson * Check with permission for a specific privilege is granted within jail. We 3737800c9408SRobert Watson * have a specific list of accepted privileges; the rest are denied. 3738800c9408SRobert Watson */ 3739800c9408SRobert Watson int 3740800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv) 3741800c9408SRobert Watson { 37420fe74ae6SJamie Gritton struct prison *pr; 37430fe74ae6SJamie Gritton int error; 3744800c9408SRobert Watson 37457b2ff0dcSMateusz Guzik /* 37467b2ff0dcSMateusz Guzik * Some policies have custom handlers. This routine should not be 37477b2ff0dcSMateusz Guzik * called for them. See priv_check_cred(). 37487b2ff0dcSMateusz Guzik */ 37497b2ff0dcSMateusz Guzik switch (priv) { 3750a459a6cfSMateusz Guzik case PRIV_VFS_LOOKUP: 37517b2ff0dcSMateusz Guzik case PRIV_VFS_GENERATION: 37527b2ff0dcSMateusz Guzik KASSERT(0, ("prison_priv_check instead of a custom handler " 37537b2ff0dcSMateusz Guzik "called for %d\n", priv)); 37547b2ff0dcSMateusz Guzik } 37557b2ff0dcSMateusz Guzik 3756800c9408SRobert Watson if (!jailed(cred)) 3757800c9408SRobert Watson return (0); 3758800c9408SRobert Watson 37596bb79563SJamie Gritton #ifdef VIMAGE 37606bb79563SJamie Gritton /* 37616bb79563SJamie Gritton * Privileges specific to prisons with a virtual network stack. 37626bb79563SJamie Gritton * There might be a duplicate entry here in case the privilege 37636bb79563SJamie Gritton * is only granted conditionally in the legacy jail case. 37646bb79563SJamie Gritton */ 37656bb79563SJamie Gritton switch (priv) { 37666bb79563SJamie Gritton /* 37676bb79563SJamie Gritton * NFS-specific privileges. 37686bb79563SJamie Gritton */ 37696bb79563SJamie Gritton case PRIV_NFS_DAEMON: 3770bba7a2e8SRick Macklem case PRIV_VFS_GETFH: 3771bba7a2e8SRick Macklem case PRIV_VFS_MOUNT_EXPORTED: 3772bba7a2e8SRick Macklem #ifdef VNET_NFSD 3773bba7a2e8SRick Macklem if (!prison_check_nfsd(cred)) 3774bba7a2e8SRick Macklem #else 3775bba7a2e8SRick Macklem printf("running nfsd in a prison requires a kernel " 3776bba7a2e8SRick Macklem "built with ''options VNET_NFSD''\n"); 3777bba7a2e8SRick Macklem #endif 3778bba7a2e8SRick Macklem return (EPERM); 3779bba7a2e8SRick Macklem #ifdef notyet 37806bb79563SJamie Gritton case PRIV_NFS_LOCKD: 37816bb79563SJamie Gritton #endif 37826bb79563SJamie Gritton /* 37836bb79563SJamie Gritton * Network stack privileges. 37846bb79563SJamie Gritton */ 37856bb79563SJamie Gritton case PRIV_NET_BRIDGE: 37866bb79563SJamie Gritton case PRIV_NET_GRE: 37876bb79563SJamie Gritton case PRIV_NET_BPF: 37886bb79563SJamie Gritton case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */ 37896bb79563SJamie Gritton case PRIV_NET_ROUTE: 37906bb79563SJamie Gritton case PRIV_NET_TAP: 37916bb79563SJamie Gritton case PRIV_NET_SETIFMTU: 37926bb79563SJamie Gritton case PRIV_NET_SETIFFLAGS: 37936bb79563SJamie Gritton case PRIV_NET_SETIFCAP: 3794215940b3SXin LI case PRIV_NET_SETIFDESCR: 37956bb79563SJamie Gritton case PRIV_NET_SETIFNAME : 37966bb79563SJamie Gritton case PRIV_NET_SETIFMETRIC: 37976bb79563SJamie Gritton case PRIV_NET_SETIFPHYS: 37986bb79563SJamie Gritton case PRIV_NET_SETIFMAC: 3799389474c1SKonstantin Belousov case PRIV_NET_SETLANPCP: 38006bb79563SJamie Gritton case PRIV_NET_ADDMULTI: 38016bb79563SJamie Gritton case PRIV_NET_DELMULTI: 38026bb79563SJamie Gritton case PRIV_NET_HWIOCTL: 38036bb79563SJamie Gritton case PRIV_NET_SETLLADDR: 38046bb79563SJamie Gritton case PRIV_NET_ADDIFGROUP: 38056bb79563SJamie Gritton case PRIV_NET_DELIFGROUP: 38066bb79563SJamie Gritton case PRIV_NET_IFCREATE: 38076bb79563SJamie Gritton case PRIV_NET_IFDESTROY: 38086bb79563SJamie Gritton case PRIV_NET_ADDIFADDR: 38096bb79563SJamie Gritton case PRIV_NET_DELIFADDR: 38106bb79563SJamie Gritton case PRIV_NET_LAGG: 38116bb79563SJamie Gritton case PRIV_NET_GIF: 38126bb79563SJamie Gritton case PRIV_NET_SETIFVNET: 381335fd7bc0SBjoern A. Zeeb case PRIV_NET_SETIFFIB: 3814ab91feabSKristof Provost case PRIV_NET_OVPN: 381543f8c763SZhenlei Huang case PRIV_NET_ME: 3816744bfb21SJohn Baldwin case PRIV_NET_WG: 38176bb79563SJamie Gritton 38186bb79563SJamie Gritton /* 38196bb79563SJamie Gritton * 802.11-related privileges. 38206bb79563SJamie Gritton */ 3821f7d38a13SAdrian Chadd case PRIV_NET80211_VAP_GETKEY: 3822f7d38a13SAdrian Chadd case PRIV_NET80211_VAP_MANAGE: 38236bb79563SJamie Gritton 38246bb79563SJamie Gritton #ifdef notyet 38256bb79563SJamie Gritton /* 38266bb79563SJamie Gritton * ATM privileges. 38276bb79563SJamie Gritton */ 38286bb79563SJamie Gritton case PRIV_NETATM_CFG: 38296bb79563SJamie Gritton case PRIV_NETATM_ADD: 38306bb79563SJamie Gritton case PRIV_NETATM_DEL: 38316bb79563SJamie Gritton case PRIV_NETATM_SET: 38326bb79563SJamie Gritton 38336bb79563SJamie Gritton /* 38346bb79563SJamie Gritton * Bluetooth privileges. 38356bb79563SJamie Gritton */ 38366bb79563SJamie Gritton case PRIV_NETBLUETOOTH_RAW: 38376bb79563SJamie Gritton #endif 38386bb79563SJamie Gritton 38396bb79563SJamie Gritton /* 38406bb79563SJamie Gritton * Netgraph and netgraph module privileges. 38416bb79563SJamie Gritton */ 38426bb79563SJamie Gritton case PRIV_NETGRAPH_CONTROL: 38436bb79563SJamie Gritton #ifdef notyet 38446bb79563SJamie Gritton case PRIV_NETGRAPH_TTY: 38456bb79563SJamie Gritton #endif 38466bb79563SJamie Gritton 38476bb79563SJamie Gritton /* 38486bb79563SJamie Gritton * IPv4 and IPv6 privileges. 38496bb79563SJamie Gritton */ 38506bb79563SJamie Gritton case PRIV_NETINET_IPFW: 38516bb79563SJamie Gritton case PRIV_NETINET_DIVERT: 38526bb79563SJamie Gritton case PRIV_NETINET_PF: 38536bb79563SJamie Gritton case PRIV_NETINET_DUMMYNET: 38546bb79563SJamie Gritton case PRIV_NETINET_CARP: 38556bb79563SJamie Gritton case PRIV_NETINET_MROUTE: 38566bb79563SJamie Gritton case PRIV_NETINET_RAW: 38576bb79563SJamie Gritton case PRIV_NETINET_ADDRCTRL6: 38586bb79563SJamie Gritton case PRIV_NETINET_ND6: 38596bb79563SJamie Gritton case PRIV_NETINET_SCOPE6: 38606bb79563SJamie Gritton case PRIV_NETINET_ALIFETIME6: 38616bb79563SJamie Gritton case PRIV_NETINET_IPSEC: 38626bb79563SJamie Gritton case PRIV_NETINET_BINDANY: 38636bb79563SJamie Gritton 38646bb79563SJamie Gritton #ifdef notyet 38656bb79563SJamie Gritton /* 38666bb79563SJamie Gritton * NCP privileges. 38676bb79563SJamie Gritton */ 38686bb79563SJamie Gritton case PRIV_NETNCP: 38696bb79563SJamie Gritton 38706bb79563SJamie Gritton /* 38716bb79563SJamie Gritton * SMB privileges. 38726bb79563SJamie Gritton */ 38736bb79563SJamie Gritton case PRIV_NETSMB: 38746bb79563SJamie Gritton #endif 38756bb79563SJamie Gritton 38766bb79563SJamie Gritton /* 38776bb79563SJamie Gritton * No default: or deny here. 38786bb79563SJamie Gritton * In case of no permit fall through to next switch(). 38796bb79563SJamie Gritton */ 38806bb79563SJamie Gritton if (cred->cr_prison->pr_flags & PR_VNET) 38816bb79563SJamie Gritton return (0); 38826bb79563SJamie Gritton } 38836bb79563SJamie Gritton #endif /* VIMAGE */ 38846bb79563SJamie Gritton 3885800c9408SRobert Watson switch (priv) { 3886800c9408SRobert Watson /* 3887800c9408SRobert Watson * Allow ktrace privileges for root in jail. 3888800c9408SRobert Watson */ 3889800c9408SRobert Watson case PRIV_KTRACE: 3890800c9408SRobert Watson 3891c3c1b5e6SRobert Watson #if 0 3892800c9408SRobert Watson /* 3893800c9408SRobert Watson * Allow jailed processes to configure audit identity and 3894800c9408SRobert Watson * submit audit records (login, etc). In the future we may 3895800c9408SRobert Watson * want to further refine the relationship between audit and 3896800c9408SRobert Watson * jail. 3897800c9408SRobert Watson */ 3898800c9408SRobert Watson case PRIV_AUDIT_GETAUDIT: 3899800c9408SRobert Watson case PRIV_AUDIT_SETAUDIT: 3900800c9408SRobert Watson case PRIV_AUDIT_SUBMIT: 3901c3c1b5e6SRobert Watson #endif 3902800c9408SRobert Watson 3903800c9408SRobert Watson /* 3904800c9408SRobert Watson * Allow jailed processes to manipulate process UNIX 3905800c9408SRobert Watson * credentials in any way they see fit. 3906800c9408SRobert Watson */ 3907800c9408SRobert Watson case PRIV_CRED_SETUID: 3908800c9408SRobert Watson case PRIV_CRED_SETEUID: 3909800c9408SRobert Watson case PRIV_CRED_SETGID: 3910800c9408SRobert Watson case PRIV_CRED_SETEGID: 3911800c9408SRobert Watson case PRIV_CRED_SETGROUPS: 3912800c9408SRobert Watson case PRIV_CRED_SETREUID: 3913800c9408SRobert Watson case PRIV_CRED_SETREGID: 3914800c9408SRobert Watson case PRIV_CRED_SETRESUID: 3915800c9408SRobert Watson case PRIV_CRED_SETRESGID: 3916800c9408SRobert Watson 3917800c9408SRobert Watson /* 3918800c9408SRobert Watson * Jail implements visibility constraints already, so allow 3919800c9408SRobert Watson * jailed root to override uid/gid-based constraints. 3920800c9408SRobert Watson */ 3921800c9408SRobert Watson case PRIV_SEEOTHERGIDS: 3922800c9408SRobert Watson case PRIV_SEEOTHERUIDS: 3923800c9408SRobert Watson 3924800c9408SRobert Watson /* 3925800c9408SRobert Watson * Jail implements inter-process debugging limits already, so 3926800c9408SRobert Watson * allow jailed root various debugging privileges. 3927800c9408SRobert Watson */ 3928800c9408SRobert Watson case PRIV_DEBUG_DIFFCRED: 3929800c9408SRobert Watson case PRIV_DEBUG_SUGID: 3930800c9408SRobert Watson case PRIV_DEBUG_UNPRIV: 3931800c9408SRobert Watson 3932800c9408SRobert Watson /* 3933800c9408SRobert Watson * Allow jail to set various resource limits and login 3934800c9408SRobert Watson * properties, and for now, exceed process resource limits. 3935800c9408SRobert Watson */ 3936800c9408SRobert Watson case PRIV_PROC_LIMIT: 3937800c9408SRobert Watson case PRIV_PROC_SETLOGIN: 3938800c9408SRobert Watson case PRIV_PROC_SETRLIMIT: 3939800c9408SRobert Watson 3940800c9408SRobert Watson /* 3941800c9408SRobert Watson * System V and POSIX IPC privileges are granted in jail. 3942800c9408SRobert Watson */ 3943800c9408SRobert Watson case PRIV_IPC_READ: 3944800c9408SRobert Watson case PRIV_IPC_WRITE: 3945800c9408SRobert Watson case PRIV_IPC_ADMIN: 3946800c9408SRobert Watson case PRIV_IPC_MSGSIZE: 3947800c9408SRobert Watson case PRIV_MQ_ADMIN: 3948800c9408SRobert Watson 3949800c9408SRobert Watson /* 39500304c731SJamie Gritton * Jail operations within a jail work on child jails. 39510304c731SJamie Gritton */ 39520304c731SJamie Gritton case PRIV_JAIL_ATTACH: 39530304c731SJamie Gritton case PRIV_JAIL_SET: 39540304c731SJamie Gritton case PRIV_JAIL_REMOVE: 39550304c731SJamie Gritton 39560304c731SJamie Gritton /* 3957800c9408SRobert Watson * Jail implements its own inter-process limits, so allow 3958800c9408SRobert Watson * root processes in jail to change scheduling on other 3959800c9408SRobert Watson * processes in the same jail. Likewise for signalling. 3960800c9408SRobert Watson */ 3961800c9408SRobert Watson case PRIV_SCHED_DIFFCRED: 3962413628a7SBjoern A. Zeeb case PRIV_SCHED_CPUSET: 3963800c9408SRobert Watson case PRIV_SIGNAL_DIFFCRED: 3964800c9408SRobert Watson case PRIV_SIGNAL_SUGID: 3965800c9408SRobert Watson 3966800c9408SRobert Watson /* 3967800c9408SRobert Watson * Allow jailed processes to write to sysctls marked as jail 3968800c9408SRobert Watson * writable. 3969800c9408SRobert Watson */ 3970800c9408SRobert Watson case PRIV_SYSCTL_WRITEJAIL: 3971800c9408SRobert Watson 3972800c9408SRobert Watson /* 3973800c9408SRobert Watson * Allow root in jail to manage a variety of quota 3974e82d0201SRobert Watson * properties. These should likely be conditional on a 3975e82d0201SRobert Watson * configuration option. 3976800c9408SRobert Watson */ 397795b091d2SRobert Watson case PRIV_VFS_GETQUOTA: 397895b091d2SRobert Watson case PRIV_VFS_SETQUOTA: 3979800c9408SRobert Watson 3980800c9408SRobert Watson /* 3981800c9408SRobert Watson * Since Jail relies on chroot() to implement file system 3982800c9408SRobert Watson * protections, grant many VFS privileges to root in jail. 3983800c9408SRobert Watson * Be careful to exclude mount-related and NFS-related 3984800c9408SRobert Watson * privileges. 3985800c9408SRobert Watson */ 3986800c9408SRobert Watson case PRIV_VFS_READ: 3987800c9408SRobert Watson case PRIV_VFS_WRITE: 3988800c9408SRobert Watson case PRIV_VFS_ADMIN: 3989800c9408SRobert Watson case PRIV_VFS_EXEC: 3990800c9408SRobert Watson case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */ 3991800c9408SRobert Watson case PRIV_VFS_CHFLAGS_DEV: 3992800c9408SRobert Watson case PRIV_VFS_CHOWN: 3993800c9408SRobert Watson case PRIV_VFS_CHROOT: 3994bb531912SPawel Jakub Dawidek case PRIV_VFS_RETAINSUGID: 3995800c9408SRobert Watson case PRIV_VFS_FCHROOT: 3996800c9408SRobert Watson case PRIV_VFS_LINK: 3997800c9408SRobert Watson case PRIV_VFS_SETGID: 3998e41966dcSRobert Watson case PRIV_VFS_STAT: 3999800c9408SRobert Watson case PRIV_VFS_STICKYFILE: 4000bb56d716SJamie Gritton 4001bb56d716SJamie Gritton /* 4002bb56d716SJamie Gritton * As in the non-jail case, non-root users are expected to be 400370de1003SGordon Bergling * able to read kernel/physical memory (provided /dev/[k]mem 4004bb56d716SJamie Gritton * exists in the jail and they have permission to access it). 4005bb56d716SJamie Gritton */ 4006bb56d716SJamie Gritton case PRIV_KMEM_READ: 4007800c9408SRobert Watson return (0); 4008800c9408SRobert Watson 4009800c9408SRobert Watson /* 4010800c9408SRobert Watson * Depending on the global setting, allow privilege of 4011800c9408SRobert Watson * setting system flags. 4012800c9408SRobert Watson */ 4013800c9408SRobert Watson case PRIV_VFS_SYSFLAGS: 40140304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS) 4015800c9408SRobert Watson return (0); 4016800c9408SRobert Watson else 4017800c9408SRobert Watson return (EPERM); 4018800c9408SRobert Watson 4019800c9408SRobert Watson /* 4020f3a8d2f9SPawel Jakub Dawidek * Depending on the global setting, allow privilege of 4021f3a8d2f9SPawel Jakub Dawidek * mounting/unmounting file systems. 4022f3a8d2f9SPawel Jakub Dawidek */ 4023f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT: 4024f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_UNMOUNT: 4025f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT_NONUSER: 402624b0502eSPawel Jakub Dawidek case PRIV_VFS_MOUNT_OWNER: 40270fe74ae6SJamie Gritton pr = cred->cr_prison; 40280fe74ae6SJamie Gritton prison_lock(pr); 40290fe74ae6SJamie Gritton if (pr->pr_allow & PR_ALLOW_MOUNT && pr->pr_enforce_statfs < 2) 40300fe74ae6SJamie Gritton error = 0; 4031f3a8d2f9SPawel Jakub Dawidek else 40320fe74ae6SJamie Gritton error = EPERM; 40330fe74ae6SJamie Gritton prison_unlock(pr); 40340fe74ae6SJamie Gritton return (error); 4035f3a8d2f9SPawel Jakub Dawidek 4036f3a8d2f9SPawel Jakub Dawidek /* 403763619b6dSKyle Evans * Jails should hold no disposition on the PRIV_VFS_READ_DIR 403863619b6dSKyle Evans * policy. priv_check_cred will not specifically allow it, and 403963619b6dSKyle Evans * we may want a MAC policy to allow it. 404063619b6dSKyle Evans */ 404163619b6dSKyle Evans case PRIV_VFS_READ_DIR: 404263619b6dSKyle Evans return (0); 404363619b6dSKyle Evans 404463619b6dSKyle Evans /* 4045ccd6ac9fSAntoine Brodin * Conditionnaly allow locking (unlocking) physical pages 4046ccd6ac9fSAntoine Brodin * in memory. 4047ccd6ac9fSAntoine Brodin */ 4048ccd6ac9fSAntoine Brodin case PRIV_VM_MLOCK: 4049ccd6ac9fSAntoine Brodin case PRIV_VM_MUNLOCK: 4050ccd6ac9fSAntoine Brodin if (cred->cr_prison->pr_allow & PR_ALLOW_MLOCK) 4051ccd6ac9fSAntoine Brodin return (0); 4052ccd6ac9fSAntoine Brodin else 4053ccd6ac9fSAntoine Brodin return (EPERM); 4054ccd6ac9fSAntoine Brodin 4055ccd6ac9fSAntoine Brodin /* 4056e28f9b7dSAllan Jude * Conditionally allow jailed root to bind reserved ports. 4057800c9408SRobert Watson */ 4058800c9408SRobert Watson case PRIV_NETINET_RESERVEDPORT: 4059e28f9b7dSAllan Jude if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS) 4060e28f9b7dSAllan Jude return (0); 4061e28f9b7dSAllan Jude else 4062e28f9b7dSAllan Jude return (EPERM); 4063e28f9b7dSAllan Jude 4064e28f9b7dSAllan Jude /* 4065e28f9b7dSAllan Jude * Allow jailed root to reuse in-use ports. 4066e28f9b7dSAllan Jude */ 40674b084056SRobert Watson case PRIV_NETINET_REUSEPORT: 4068800c9408SRobert Watson return (0); 4069800c9408SRobert Watson 4070800c9408SRobert Watson /* 4071e3043798SPedro F. Giffuni * Allow jailed root to set certain IPv4/6 (option) headers. 407279ba3952SBjoern A. Zeeb */ 407379ba3952SBjoern A. Zeeb case PRIV_NETINET_SETHDROPTS: 407479ba3952SBjoern A. Zeeb return (0); 407579ba3952SBjoern A. Zeeb 407679ba3952SBjoern A. Zeeb /* 4077800c9408SRobert Watson * Conditionally allow creating raw sockets in jail. 4078800c9408SRobert Watson */ 4079800c9408SRobert Watson case PRIV_NETINET_RAW: 40800304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS) 4081800c9408SRobert Watson return (0); 4082800c9408SRobert Watson else 4083800c9408SRobert Watson return (EPERM); 4084800c9408SRobert Watson 4085800c9408SRobert Watson /* 4086800c9408SRobert Watson * Since jail implements its own visibility limits on netstat 4087800c9408SRobert Watson * sysctls, allow getcred. This allows identd to work in 4088800c9408SRobert Watson * jail. 4089800c9408SRobert Watson */ 4090800c9408SRobert Watson case PRIV_NETINET_GETCRED: 4091800c9408SRobert Watson return (0); 4092800c9408SRobert Watson 40932bfc50bcSEdward Tomasz Napierala /* 40942bfc50bcSEdward Tomasz Napierala * Allow jailed root to set loginclass. 40952bfc50bcSEdward Tomasz Napierala */ 40962bfc50bcSEdward Tomasz Napierala case PRIV_PROC_SETLOGINCLASS: 40972bfc50bcSEdward Tomasz Napierala return (0); 40982bfc50bcSEdward Tomasz Napierala 4099b19d66fdSJamie Gritton /* 41004520f617SJamie Gritton * Do not allow a process inside a jail to read the kernel 4101b19d66fdSJamie Gritton * message buffer unless explicitly permitted. 4102b19d66fdSJamie Gritton */ 4103b19d66fdSJamie Gritton case PRIV_MSGBUF: 4104b19d66fdSJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_READ_MSGBUF) 4105b19d66fdSJamie Gritton return (0); 4106b19d66fdSJamie Gritton return (EPERM); 4107b19d66fdSJamie Gritton 4108800c9408SRobert Watson default: 4109800c9408SRobert Watson /* 4110800c9408SRobert Watson * In all remaining cases, deny the privilege request. This 4111800c9408SRobert Watson * includes almost all network privileges, many system 4112800c9408SRobert Watson * configuration privileges. 4113800c9408SRobert Watson */ 4114800c9408SRobert Watson return (EPERM); 4115800c9408SRobert Watson } 4116800c9408SRobert Watson } 4117800c9408SRobert Watson 41180304c731SJamie Gritton /* 41190304c731SJamie Gritton * Return the part of pr2's name that is relative to pr1, or the whole name 41200304c731SJamie Gritton * if it does not directly follow. 41210304c731SJamie Gritton */ 41220304c731SJamie Gritton 41230304c731SJamie Gritton char * 41240304c731SJamie Gritton prison_name(struct prison *pr1, struct prison *pr2) 41250304c731SJamie Gritton { 41260304c731SJamie Gritton char *name; 41270304c731SJamie Gritton 41280304c731SJamie Gritton /* Jails see themselves as "0" (if they see themselves at all). */ 41290304c731SJamie Gritton if (pr1 == pr2) 41300304c731SJamie Gritton return "0"; 41310304c731SJamie Gritton name = pr2->pr_name; 41320304c731SJamie Gritton if (prison_ischild(pr1, pr2)) { 41330304c731SJamie Gritton /* 41340304c731SJamie Gritton * pr1 isn't locked (and allprison_lock may not be either) 41350304c731SJamie Gritton * so its length can't be counted on. But the number of dots 41360304c731SJamie Gritton * can be counted on - and counted. 41370304c731SJamie Gritton */ 41380304c731SJamie Gritton for (; pr1 != &prison0; pr1 = pr1->pr_parent) 41390304c731SJamie Gritton name = strchr(name, '.') + 1; 41400304c731SJamie Gritton } 41410304c731SJamie Gritton return (name); 41420304c731SJamie Gritton } 41430304c731SJamie Gritton 41440304c731SJamie Gritton /* 41450304c731SJamie Gritton * Return the part of pr2's path that is relative to pr1, or the whole path 41460304c731SJamie Gritton * if it does not directly follow. 41470304c731SJamie Gritton */ 41480304c731SJamie Gritton static char * 41490304c731SJamie Gritton prison_path(struct prison *pr1, struct prison *pr2) 41500304c731SJamie Gritton { 41510304c731SJamie Gritton char *path1, *path2; 41520304c731SJamie Gritton int len1; 41530304c731SJamie Gritton 41540304c731SJamie Gritton path1 = pr1->pr_path; 41550304c731SJamie Gritton path2 = pr2->pr_path; 41560304c731SJamie Gritton if (!strcmp(path1, "/")) 41570304c731SJamie Gritton return (path2); 41580304c731SJamie Gritton len1 = strlen(path1); 41590304c731SJamie Gritton if (strncmp(path1, path2, len1)) 41600304c731SJamie Gritton return (path2); 41610304c731SJamie Gritton if (path2[len1] == '\0') 41620304c731SJamie Gritton return "/"; 41630304c731SJamie Gritton if (path2[len1] == '/') 41640304c731SJamie Gritton return (path2 + len1); 41650304c731SJamie Gritton return (path2); 41660304c731SJamie Gritton } 41670304c731SJamie Gritton 41680304c731SJamie Gritton /* 41690304c731SJamie Gritton * Jail-related sysctls. 41700304c731SJamie Gritton */ 41717029da5cSPawel Biernacki static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 41720304c731SJamie Gritton "Jails"); 41730304c731SJamie Gritton 4174eb8dcdeaSGleb Smirnoff #if defined(INET) || defined(INET6) 4175eb8dcdeaSGleb Smirnoff /* 4176eb8dcdeaSGleb Smirnoff * Copy address array to memory that would be then SYSCTL_OUT-ed. 4177eb8dcdeaSGleb Smirnoff * sysctl_jail_list() helper. 4178eb8dcdeaSGleb Smirnoff */ 4179eb8dcdeaSGleb Smirnoff static void 4180eb8dcdeaSGleb Smirnoff prison_ip_copyout(struct prison *pr, const pr_family_t af, void **out, int *len) 4181eb8dcdeaSGleb Smirnoff { 4182eb8dcdeaSGleb Smirnoff const size_t size = pr_families[af].size; 4183eb8dcdeaSGleb Smirnoff 4184eb8dcdeaSGleb Smirnoff again: 4185eb8dcdeaSGleb Smirnoff mtx_assert(&pr->pr_mtx, MA_OWNED); 4186eb8dcdeaSGleb Smirnoff if (pr->pr_addrs[af] != NULL) { 4187eb8dcdeaSGleb Smirnoff if (*len < pr->pr_addrs[af]->ips) { 4188eb8dcdeaSGleb Smirnoff *len = pr->pr_addrs[af]->ips; 4189eb8dcdeaSGleb Smirnoff mtx_unlock(&pr->pr_mtx); 4190eb8dcdeaSGleb Smirnoff *out = realloc(*out, *len * size, M_TEMP, M_WAITOK); 4191eb8dcdeaSGleb Smirnoff mtx_lock(&pr->pr_mtx); 4192eb8dcdeaSGleb Smirnoff goto again; 4193eb8dcdeaSGleb Smirnoff } 4194eb8dcdeaSGleb Smirnoff bcopy(pr->pr_addrs[af] + 1, *out, pr->pr_addrs[af]->ips * size); 4195eb8dcdeaSGleb Smirnoff } 4196eb8dcdeaSGleb Smirnoff } 4197eb8dcdeaSGleb Smirnoff #endif 4198eb8dcdeaSGleb Smirnoff 4199fd7a8150SMike Barcroft static int 4200fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS) 4201fd7a8150SMike Barcroft { 4202b38ff370SJamie Gritton struct xprison *xp; 42030304c731SJamie Gritton struct prison *pr, *cpr; 4204b38ff370SJamie Gritton #ifdef INET 4205b38ff370SJamie Gritton struct in_addr *ip4 = NULL; 4206b38ff370SJamie Gritton int ip4s = 0; 4207b38ff370SJamie Gritton #endif 4208b38ff370SJamie Gritton #ifdef INET6 42093beefaedSColin Percival struct in6_addr *ip6 = NULL; 4210b38ff370SJamie Gritton int ip6s = 0; 4211b38ff370SJamie Gritton #endif 42120304c731SJamie Gritton int descend, error; 4213fd7a8150SMike Barcroft 4214b38ff370SJamie Gritton xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK); 42150304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 4216b38ff370SJamie Gritton error = 0; 4217dc68a633SPawel Jakub Dawidek sx_slock(&allprison_lock); 42180304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 42190304c731SJamie Gritton mtx_lock(&cpr->pr_mtx); 4220413628a7SBjoern A. Zeeb #ifdef INET 4221eb8dcdeaSGleb Smirnoff prison_ip_copyout(cpr, PR_INET, (void **)&ip4, &ip4s); 4222413628a7SBjoern A. Zeeb #endif 4223413628a7SBjoern A. Zeeb #ifdef INET6 4224eb8dcdeaSGleb Smirnoff prison_ip_copyout(cpr, PR_INET6, (void **)&ip6, &ip6s); 4225b38ff370SJamie Gritton #endif 4226b38ff370SJamie Gritton bzero(xp, sizeof(*xp)); 4227fd7a8150SMike Barcroft xp->pr_version = XPRISON_VERSION; 42280304c731SJamie Gritton xp->pr_id = cpr->pr_id; 42291158508aSJamie Gritton xp->pr_state = cpr->pr_state; 42300304c731SJamie Gritton strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path)); 4231c1f19219SJamie Gritton strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host)); 42320304c731SJamie Gritton strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name)); 4233413628a7SBjoern A. Zeeb #ifdef INET 4234eb8dcdeaSGleb Smirnoff xp->pr_ip4s = ip4s; 4235413628a7SBjoern A. Zeeb #endif 4236413628a7SBjoern A. Zeeb #ifdef INET6 4237eb8dcdeaSGleb Smirnoff xp->pr_ip6s = ip6s; 4238413628a7SBjoern A. Zeeb #endif 42390304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 4240b38ff370SJamie Gritton error = SYSCTL_OUT(req, xp, sizeof(*xp)); 4241b38ff370SJamie Gritton if (error) 4242b38ff370SJamie Gritton break; 4243413628a7SBjoern A. Zeeb #ifdef INET 4244b38ff370SJamie Gritton if (xp->pr_ip4s > 0) { 4245b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip4, 4246b38ff370SJamie Gritton xp->pr_ip4s * sizeof(struct in_addr)); 4247b38ff370SJamie Gritton if (error) 4248b38ff370SJamie Gritton break; 4249413628a7SBjoern A. Zeeb } 4250413628a7SBjoern A. Zeeb #endif 4251413628a7SBjoern A. Zeeb #ifdef INET6 4252b38ff370SJamie Gritton if (xp->pr_ip6s > 0) { 4253b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip6, 4254b38ff370SJamie Gritton xp->pr_ip6s * sizeof(struct in6_addr)); 4255b38ff370SJamie Gritton if (error) 4256b38ff370SJamie Gritton break; 4257413628a7SBjoern A. Zeeb } 4258413628a7SBjoern A. Zeeb #endif 4259fd7a8150SMike Barcroft } 4260dc68a633SPawel Jakub Dawidek sx_sunlock(&allprison_lock); 4261b38ff370SJamie Gritton free(xp, M_TEMP); 4262b38ff370SJamie Gritton #ifdef INET 4263b38ff370SJamie Gritton free(ip4, M_TEMP); 4264b38ff370SJamie Gritton #endif 4265b38ff370SJamie Gritton #ifdef INET6 4266b38ff370SJamie Gritton free(ip6, M_TEMP); 4267b38ff370SJamie Gritton #endif 4268fd7a8150SMike Barcroft return (error); 4269fd7a8150SMike Barcroft } 4270fd7a8150SMike Barcroft 4271f3b86a5fSEd Schouten SYSCTL_OID(_security_jail, OID_AUTO, list, 4272f3b86a5fSEd Schouten CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4273f3b86a5fSEd Schouten sysctl_jail_list, "S", "List of active jails"); 4274461167c2SPawel Jakub Dawidek 4275461167c2SPawel Jakub Dawidek static int 4276461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS) 4277461167c2SPawel Jakub Dawidek { 4278461167c2SPawel Jakub Dawidek int error, injail; 4279461167c2SPawel Jakub Dawidek 4280461167c2SPawel Jakub Dawidek injail = jailed(req->td->td_ucred); 4281461167c2SPawel Jakub Dawidek error = SYSCTL_OUT(req, &injail, sizeof(injail)); 4282461167c2SPawel Jakub Dawidek 4283461167c2SPawel Jakub Dawidek return (error); 4284461167c2SPawel Jakub Dawidek } 42850304c731SJamie Gritton 4286f3b86a5fSEd Schouten SYSCTL_PROC(_security_jail, OID_AUTO, jailed, 4287f3b86a5fSEd Schouten CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4288f3b86a5fSEd Schouten sysctl_jail_jailed, "I", "Process in jail?"); 4289413628a7SBjoern A. Zeeb 4290761d2bb5SJamie Gritton static int 4291761d2bb5SJamie Gritton sysctl_jail_vnet(SYSCTL_HANDLER_ARGS) 4292761d2bb5SJamie Gritton { 4293761d2bb5SJamie Gritton int error, havevnet; 4294761d2bb5SJamie Gritton #ifdef VIMAGE 4295761d2bb5SJamie Gritton struct ucred *cred = req->td->td_ucred; 4296761d2bb5SJamie Gritton 4297761d2bb5SJamie Gritton havevnet = jailed(cred) && prison_owns_vnet(cred); 4298761d2bb5SJamie Gritton #else 4299761d2bb5SJamie Gritton havevnet = 0; 4300761d2bb5SJamie Gritton #endif 4301761d2bb5SJamie Gritton error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet)); 4302761d2bb5SJamie Gritton 4303761d2bb5SJamie Gritton return (error); 4304761d2bb5SJamie Gritton } 4305761d2bb5SJamie Gritton 4306761d2bb5SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, vnet, 4307761d2bb5SJamie Gritton CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4308bb8f1623SBjoern A. Zeeb sysctl_jail_vnet, "I", "Jail owns vnet?"); 4309761d2bb5SJamie Gritton 43100304c731SJamie Gritton #if defined(INET) || defined(INET6) 4311e92e0574SJamie Gritton SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW, 43120304c731SJamie Gritton &jail_max_af_ips, 0, 4313c542c43eSJamie Gritton "Number of IP addresses a jail may have at most per address family (deprecated)"); 43140304c731SJamie Gritton #endif 43150304c731SJamie Gritton 43160304c731SJamie Gritton /* 4317c542c43eSJamie Gritton * Default parameters for jail(2) compatibility. For historical reasons, 4318c542c43eSJamie Gritton * the sysctl names have varying similarity to the parameter names. Prisons 4319c542c43eSJamie Gritton * just see their own parameters, and can't change them. 43200304c731SJamie Gritton */ 43210304c731SJamie Gritton static int 43220304c731SJamie Gritton sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS) 43230304c731SJamie Gritton { 43240fe74ae6SJamie Gritton int error, i; 43250304c731SJamie Gritton 43260304c731SJamie Gritton /* Get the current flag value, and convert it to a boolean. */ 43270fe74ae6SJamie Gritton if (req->td->td_ucred->cr_prison == &prison0) { 43280fe74ae6SJamie Gritton mtx_lock(&prison0.pr_mtx); 43290fe74ae6SJamie Gritton i = (jail_default_allow & arg2) != 0; 43300fe74ae6SJamie Gritton mtx_unlock(&prison0.pr_mtx); 43310fe74ae6SJamie Gritton } else 43320fe74ae6SJamie Gritton i = prison_allow(req->td->td_ucred, arg2); 43330fe74ae6SJamie Gritton 43340304c731SJamie Gritton if (arg1 != NULL) 43350304c731SJamie Gritton i = !i; 43360304c731SJamie Gritton error = sysctl_handle_int(oidp, &i, 0, req); 4337c542c43eSJamie Gritton if (error || !req->newptr) 43380304c731SJamie Gritton return (error); 43390304c731SJamie Gritton i = i ? arg2 : 0; 43400304c731SJamie Gritton if (arg1 != NULL) 43410304c731SJamie Gritton i ^= arg2; 43420304c731SJamie Gritton /* 43430304c731SJamie Gritton * The sysctls don't have CTLFLAGS_PRISON, so assume prison0 43440304c731SJamie Gritton * for writing. 43450304c731SJamie Gritton */ 43460304c731SJamie Gritton mtx_lock(&prison0.pr_mtx); 43470304c731SJamie Gritton jail_default_allow = (jail_default_allow & ~arg2) | i; 43480304c731SJamie Gritton mtx_unlock(&prison0.pr_mtx); 43490304c731SJamie Gritton return (0); 43500304c731SJamie Gritton } 43510304c731SJamie Gritton 43520304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed, 4353c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 43540304c731SJamie Gritton NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I", 4355c542c43eSJamie Gritton "Processes in jail can set their hostnames (deprecated)"); 43560304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only, 4357c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 43580304c731SJamie Gritton (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I", 4359c542c43eSJamie Gritton "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)"); 43600304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed, 4361c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 43620304c731SJamie Gritton NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I", 4363c542c43eSJamie Gritton "Processes in jail can use System V IPC primitives (deprecated)"); 43640304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets, 4365c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 43660304c731SJamie Gritton NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I", 4367c542c43eSJamie Gritton "Prison root can create raw sockets (deprecated)"); 43680304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed, 4369c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 43700304c731SJamie Gritton NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I", 4371c542c43eSJamie Gritton "Processes in jail can alter system file flags (deprecated)"); 43720304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed, 4373c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 43740304c731SJamie Gritton NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I", 4375c542c43eSJamie Gritton "Processes in jail can mount/unmount jail-friendly file systems (deprecated)"); 43760304c731SJamie Gritton 43770304c731SJamie Gritton static int 43780304c731SJamie Gritton sysctl_jail_default_level(SYSCTL_HANDLER_ARGS) 43790304c731SJamie Gritton { 43800304c731SJamie Gritton struct prison *pr; 43810304c731SJamie Gritton int level, error; 43820304c731SJamie Gritton 43830304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 43840304c731SJamie Gritton level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2); 43850304c731SJamie Gritton error = sysctl_handle_int(oidp, &level, 0, req); 4386c542c43eSJamie Gritton if (error || !req->newptr) 43870304c731SJamie Gritton return (error); 43880304c731SJamie Gritton *(int *)arg1 = level; 43890304c731SJamie Gritton return (0); 43900304c731SJamie Gritton } 43910304c731SJamie Gritton 43920304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs, 4393c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4394c542c43eSJamie Gritton &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs), 43950304c731SJamie Gritton sysctl_jail_default_level, "I", 4396c542c43eSJamie Gritton "Processes in jail cannot see all mounted file systems (deprecated)"); 4397c542c43eSJamie Gritton 43980cc207a6SMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset, 4399c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 4400c542c43eSJamie Gritton &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum), 44010cc207a6SMartin Matuska sysctl_jail_default_level, "I", 4402c542c43eSJamie Gritton "Ruleset for the devfs filesystem in jail (deprecated)"); 44030cc207a6SMartin Matuska 44040304c731SJamie Gritton /* 44050304c731SJamie Gritton * Nodes to describe jail parameters. Maximum length of string parameters 44060304c731SJamie Gritton * is returned in the string itself, and the other parameters exist merely 44070304c731SJamie Gritton * to make themselves and their types known. 44080304c731SJamie Gritton */ 44097029da5cSPawel Biernacki SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 44100304c731SJamie Gritton "Jail parameters"); 44110304c731SJamie Gritton 44120304c731SJamie Gritton int 44130304c731SJamie Gritton sysctl_jail_param(SYSCTL_HANDLER_ARGS) 44140304c731SJamie Gritton { 44150304c731SJamie Gritton int i; 44160304c731SJamie Gritton long l; 44170304c731SJamie Gritton size_t s; 44180304c731SJamie Gritton char numbuf[12]; 44190304c731SJamie Gritton 44200304c731SJamie Gritton switch (oidp->oid_kind & CTLTYPE) 44210304c731SJamie Gritton { 44220304c731SJamie Gritton case CTLTYPE_LONG: 44230304c731SJamie Gritton case CTLTYPE_ULONG: 44240304c731SJamie Gritton l = 0; 44250304c731SJamie Gritton #ifdef SCTL_MASK32 44260304c731SJamie Gritton if (!(req->flags & SCTL_MASK32)) 44270304c731SJamie Gritton #endif 44280304c731SJamie Gritton return (SYSCTL_OUT(req, &l, sizeof(l))); 44290304c731SJamie Gritton case CTLTYPE_INT: 44300304c731SJamie Gritton case CTLTYPE_UINT: 44310304c731SJamie Gritton i = 0; 44320304c731SJamie Gritton return (SYSCTL_OUT(req, &i, sizeof(i))); 44330304c731SJamie Gritton case CTLTYPE_STRING: 4434e4cd31ddSJeff Roberson snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2); 44350304c731SJamie Gritton return 44360304c731SJamie Gritton (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req)); 44370304c731SJamie Gritton case CTLTYPE_STRUCT: 44380304c731SJamie Gritton s = (size_t)arg2; 44390304c731SJamie Gritton return (SYSCTL_OUT(req, &s, sizeof(s))); 44400304c731SJamie Gritton } 44410304c731SJamie Gritton return (0); 44420304c731SJamie Gritton } 44430304c731SJamie Gritton 4444b96bd95bSIan Lepore /* 4445b96bd95bSIan Lepore * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at 4446b96bd95bSIan Lepore * jail creation time but cannot be changed in an existing jail. 4447b96bd95bSIan Lepore */ 44480304c731SJamie Gritton SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID"); 44490304c731SJamie Gritton SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID"); 44500304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name"); 44510304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path"); 44520304c731SJamie Gritton SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW, 44530304c731SJamie Gritton "I", "Jail secure level"); 4454b96bd95bSIan Lepore SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I", 4455b96bd95bSIan Lepore "Jail value for kern.osreldate and uname -K"); 4456b96bd95bSIan Lepore SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN, 4457b96bd95bSIan Lepore "Jail value for kern.osrelease and uname -r"); 44580304c731SJamie Gritton SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW, 44590304c731SJamie Gritton "I", "Jail cannot see all mounted file systems"); 44600cc207a6SMartin Matuska SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW, 44610cc207a6SMartin Matuska "I", "Ruleset for in-jail devfs mounts"); 44620304c731SJamie Gritton SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW, 44630304c731SJamie Gritton "B", "Jail persistence"); 4464679e1390SJamie Gritton #ifdef VIMAGE 4465679e1390SJamie Gritton SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN, 44667cbf7213SJamie Gritton "E,jailsys", "Virtual network stack"); 4467679e1390SJamie Gritton #endif 44680304c731SJamie Gritton SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD, 44690304c731SJamie Gritton "B", "Jail is in the process of shutting down"); 44700304c731SJamie Gritton 4471b97457e2SJamie Gritton SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails"); 4472b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD, 4473b97457e2SJamie Gritton "I", "Current number of child jails"); 4474b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW, 4475b97457e2SJamie Gritton "I", "Maximum number of child jails"); 4476b97457e2SJamie Gritton 44777cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info"); 44780304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN, 44790304c731SJamie Gritton "Jail hostname"); 448076ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN, 448176ca6f88SJamie Gritton "Jail NIS domainname"); 448276ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN, 448376ca6f88SJamie Gritton "Jail host UUID"); 448476ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW, 448576ca6f88SJamie Gritton "LU", "Jail host ID"); 44860304c731SJamie Gritton 44870304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset"); 44880304c731SJamie Gritton SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID"); 44890304c731SJamie Gritton 44900304c731SJamie Gritton #ifdef INET 44912b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN, 44922b0d6f81SJamie Gritton "Jail IPv4 address virtualization"); 44930304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr), 44940304c731SJamie Gritton "S,in_addr,a", "Jail IPv4 addresses"); 4495592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4496592bcae8SBjoern A. Zeeb "B", "Do (not) use IPv4 source address selection rather than the " 4497592bcae8SBjoern A. Zeeb "primary jail IPv4 address."); 44980304c731SJamie Gritton #endif 44990304c731SJamie Gritton #ifdef INET6 45002b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN, 45012b0d6f81SJamie Gritton "Jail IPv6 address virtualization"); 45020304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr), 45030304c731SJamie Gritton "S,in6_addr,a", "Jail IPv6 addresses"); 4504592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4505592bcae8SBjoern A. Zeeb "B", "Do (not) use IPv6 source address selection rather than the " 4506592bcae8SBjoern A. Zeeb "primary jail IPv6 address."); 45070304c731SJamie Gritton #endif 45080304c731SJamie Gritton 45090304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags"); 45100304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW, 45110304c731SJamie Gritton "B", "Jail may set hostname"); 45120304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW, 45130304c731SJamie Gritton "B", "Jail may use SYSV IPC"); 45140304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW, 45150304c731SJamie Gritton "B", "Jail may create raw sockets"); 45160304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW, 45170304c731SJamie Gritton "B", "Jail may alter system file flags"); 45180304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW, 45190304c731SJamie Gritton "B", "Jail may set file quotas"); 45200304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW, 45210304c731SJamie Gritton "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route"); 4522ccd6ac9fSAntoine Brodin SYSCTL_JAIL_PARAM(_allow, mlock, CTLTYPE_INT | CTLFLAG_RW, 4523ccd6ac9fSAntoine Brodin "B", "Jail may lock (unlock) physical pages in memory"); 4524e28f9b7dSAllan Jude SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW, 4525e28f9b7dSAllan Jude "B", "Jail may bind sockets to reserved ports"); 4526b19d66fdSJamie Gritton SYSCTL_JAIL_PARAM(_allow, read_msgbuf, CTLTYPE_INT | CTLFLAG_RW, 4527b19d66fdSJamie Gritton "B", "Jail may read the kernel message buffer"); 4528b3079544SJamie Gritton SYSCTL_JAIL_PARAM(_allow, unprivileged_proc_debug, CTLTYPE_INT | CTLFLAG_RW, 4529b3079544SJamie Gritton "B", "Unprivileged processes may use process debugging facilities"); 453005e1e482SMariusz Zaborski SYSCTL_JAIL_PARAM(_allow, suser, CTLTYPE_INT | CTLFLAG_RW, 453105e1e482SMariusz Zaborski "B", "Processes in jail with uid 0 have privilege"); 4532bba7a2e8SRick Macklem #if defined(VNET_NFSD) && defined(VIMAGE) && defined(NFSD) 4533bba7a2e8SRick Macklem SYSCTL_JAIL_PARAM(_allow, nfsd, CTLTYPE_INT | CTLFLAG_RW, 4534bba7a2e8SRick Macklem "B", "Mountd/nfsd may run in the jail"); 4535bba7a2e8SRick Macklem #endif 45360304c731SJamie Gritton 4537bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags"); 4538bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW, 4539bf3db8aaSMartin Matuska "B", "Jail may mount/unmount jail-friendly file systems in general"); 45400e5c6bd4SJamie Gritton 45410e5c6bd4SJamie Gritton /* 45420a172404SJamie Gritton * Add a dynamic parameter allow.<name>, or allow.<prefix>.<name>. Return 45430a172404SJamie Gritton * its associated bit in the pr_allow bitmask, or zero if the parameter was 45440a172404SJamie Gritton * not created. 45450e5c6bd4SJamie Gritton */ 45460a172404SJamie Gritton unsigned 45470a172404SJamie Gritton prison_add_allow(const char *prefix, const char *name, const char *prefix_descr, 45480a172404SJamie Gritton const char *descr) 45490e5c6bd4SJamie Gritton { 45500e5c6bd4SJamie Gritton struct bool_flags *bf; 45510a172404SJamie Gritton struct sysctl_oid *parent; 45520a172404SJamie Gritton char *allow_name, *allow_noname, *allowed; 4553c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR 4554c542c43eSJamie Gritton char *descr_deprecated; 4555c542c43eSJamie Gritton #endif 45565d58f959SJamie Gritton u_int allow_flag; 45570e5c6bd4SJamie Gritton 45580a172404SJamie Gritton if (prefix 45590a172404SJamie Gritton ? asprintf(&allow_name, M_PRISON, "allow.%s.%s", prefix, name) 45600a172404SJamie Gritton < 0 || 45610a172404SJamie Gritton asprintf(&allow_noname, M_PRISON, "allow.%s.no%s", prefix, name) 45620a172404SJamie Gritton < 0 45630a172404SJamie Gritton : asprintf(&allow_name, M_PRISON, "allow.%s", name) < 0 || 45640a172404SJamie Gritton asprintf(&allow_noname, M_PRISON, "allow.no%s", name) < 0) { 45650e5c6bd4SJamie Gritton free(allow_name, M_PRISON); 45660a172404SJamie Gritton return 0; 45670e5c6bd4SJamie Gritton } 45680e5c6bd4SJamie Gritton 45690e5c6bd4SJamie Gritton /* 45700a172404SJamie Gritton * See if this parameter has already beed added, i.e. a module was 45710a172404SJamie Gritton * previously loaded/unloaded. 45720e5c6bd4SJamie Gritton */ 45730e5c6bd4SJamie Gritton mtx_lock(&prison0.pr_mtx); 45740e5c6bd4SJamie Gritton for (bf = pr_flag_allow; 45755d58f959SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 45765d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 45770e5c6bd4SJamie Gritton bf++) { 45780e5c6bd4SJamie Gritton if (strcmp(bf->name, allow_name) == 0) { 45790a172404SJamie Gritton allow_flag = bf->flag; 45800e5c6bd4SJamie Gritton goto no_add; 45810e5c6bd4SJamie Gritton } 45820e5c6bd4SJamie Gritton } 45830e5c6bd4SJamie Gritton 45840e5c6bd4SJamie Gritton /* 45855d58f959SJamie Gritton * Find a free bit in pr_allow_all, failing if there are none 45860e5c6bd4SJamie Gritton * (which shouldn't happen as long as we keep track of how many 45870a172404SJamie Gritton * potential dynamic flags exist). 45880e5c6bd4SJamie Gritton */ 45890e5c6bd4SJamie Gritton for (allow_flag = 1;; allow_flag <<= 1) { 45900e5c6bd4SJamie Gritton if (allow_flag == 0) 45910e5c6bd4SJamie Gritton goto no_add; 45925d58f959SJamie Gritton if ((pr_allow_all & allow_flag) == 0) 45930e5c6bd4SJamie Gritton break; 45940e5c6bd4SJamie Gritton } 45950e5c6bd4SJamie Gritton 45965d58f959SJamie Gritton /* Note the parameter in the next open slot in pr_flag_allow. */ 45975d58f959SJamie Gritton for (bf = pr_flag_allow; ; bf++) { 45980e5c6bd4SJamie Gritton if (bf == pr_flag_allow + nitems(pr_flag_allow)) { 45990e5c6bd4SJamie Gritton /* This should never happen, but is not fatal. */ 46000a172404SJamie Gritton allow_flag = 0; 46010e5c6bd4SJamie Gritton goto no_add; 46020e5c6bd4SJamie Gritton } 46035d58f959SJamie Gritton if (atomic_load_int(&bf->flag) == 0) 46045d58f959SJamie Gritton break; 46055d58f959SJamie Gritton } 46060e5c6bd4SJamie Gritton bf->name = allow_name; 46070e5c6bd4SJamie Gritton bf->noname = allow_noname; 46085d58f959SJamie Gritton pr_allow_all |= allow_flag; 46095d58f959SJamie Gritton /* 46105d58f959SJamie Gritton * prison0 always has permission for the new parameter. 46115d58f959SJamie Gritton * Other jails must have it granted to them. 46125d58f959SJamie Gritton */ 46135d58f959SJamie Gritton prison0.pr_allow |= allow_flag; 46145d58f959SJamie Gritton /* The flag indicates a valid entry, so make sure it is set last. */ 46155d58f959SJamie Gritton atomic_store_rel_int(&bf->flag, allow_flag); 46160e5c6bd4SJamie Gritton mtx_unlock(&prison0.pr_mtx); 46170e5c6bd4SJamie Gritton 4618c542c43eSJamie Gritton /* 46194771011bSGordon Bergling * Create sysctls for the parameter, and the back-compat global 4620c542c43eSJamie Gritton * permission. 4621c542c43eSJamie Gritton */ 46220a172404SJamie Gritton parent = prefix 46230a172404SJamie Gritton ? SYSCTL_ADD_NODE(NULL, 46240a172404SJamie Gritton SYSCTL_CHILDREN(&sysctl___security_jail_param_allow), 46257029da5cSPawel Biernacki OID_AUTO, prefix, CTLFLAG_MPSAFE, 0, prefix_descr) 46260a172404SJamie Gritton : &sysctl___security_jail_param_allow; 46270a172404SJamie Gritton (void)SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(parent), OID_AUTO, 46280a172404SJamie Gritton name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 46290e5c6bd4SJamie Gritton NULL, 0, sysctl_jail_param, "B", descr); 46300a172404SJamie Gritton if ((prefix 46310a172404SJamie Gritton ? asprintf(&allowed, M_TEMP, "%s_%s_allowed", prefix, name) 46320a172404SJamie Gritton : asprintf(&allowed, M_TEMP, "%s_allowed", name)) >= 0) { 4633c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR 4634c542c43eSJamie Gritton (void)asprintf(&descr_deprecated, M_TEMP, "%s (deprecated)", 4635c542c43eSJamie Gritton descr); 4636c542c43eSJamie Gritton #endif 46370e5c6bd4SJamie Gritton (void)SYSCTL_ADD_PROC(NULL, 46380a172404SJamie Gritton SYSCTL_CHILDREN(&sysctl___security_jail), OID_AUTO, allowed, 4639c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, allow_flag, 4640c542c43eSJamie Gritton sysctl_jail_default_allow, "I", descr_deprecated); 4641c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR 4642c542c43eSJamie Gritton free(descr_deprecated, M_TEMP); 4643c542c43eSJamie Gritton #endif 46440a172404SJamie Gritton free(allowed, M_TEMP); 46450e5c6bd4SJamie Gritton } 46460a172404SJamie Gritton return allow_flag; 46470e5c6bd4SJamie Gritton 46480e5c6bd4SJamie Gritton no_add: 46490e5c6bd4SJamie Gritton mtx_unlock(&prison0.pr_mtx); 46500e5c6bd4SJamie Gritton free(allow_name, M_PRISON); 46510e5c6bd4SJamie Gritton free(allow_noname, M_PRISON); 46520a172404SJamie Gritton return allow_flag; 46530a172404SJamie Gritton } 46540a172404SJamie Gritton 46550a172404SJamie Gritton /* 46560a172404SJamie Gritton * The VFS system will register jail-aware filesystems here. They each get 46570a172404SJamie Gritton * a parameter allow.mount.xxxfs and a flag to check when a jailed user 46580a172404SJamie Gritton * attempts to mount. 46590a172404SJamie Gritton */ 46600a172404SJamie Gritton void 46610a172404SJamie Gritton prison_add_vfs(struct vfsconf *vfsp) 46620a172404SJamie Gritton { 46630a172404SJamie Gritton #ifdef NO_SYSCTL_DESCR 46640a172404SJamie Gritton 46650a172404SJamie Gritton vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name, 46660a172404SJamie Gritton NULL, NULL); 46670a172404SJamie Gritton #else 46680a172404SJamie Gritton char *descr; 46690a172404SJamie Gritton 46700a172404SJamie Gritton (void)asprintf(&descr, M_TEMP, "Jail may mount the %s file system", 46710a172404SJamie Gritton vfsp->vfc_name); 46720a172404SJamie Gritton vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name, 46730a172404SJamie Gritton NULL, descr); 46740a172404SJamie Gritton free(descr, M_TEMP); 46750a172404SJamie Gritton #endif 46760e5c6bd4SJamie Gritton } 4677bf3db8aaSMartin Matuska 46784b5c9cf6SEdward Tomasz Napierala #ifdef RACCT 4679097055e2SEdward Tomasz Napierala void 4680097055e2SEdward Tomasz Napierala prison_racct_foreach(void (*callback)(struct racct *racct, 468115db3c07SEdward Tomasz Napierala void *arg2, void *arg3), void (*pre)(void), void (*post)(void), 468215db3c07SEdward Tomasz Napierala void *arg2, void *arg3) 4683097055e2SEdward Tomasz Napierala { 4684a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4685097055e2SEdward Tomasz Napierala 46864b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 46874b5c9cf6SEdward Tomasz Napierala 4688097055e2SEdward Tomasz Napierala sx_slock(&allprison_lock); 468915db3c07SEdward Tomasz Napierala if (pre != NULL) 469015db3c07SEdward Tomasz Napierala (pre)(); 4691a7ad07bfSEdward Tomasz Napierala LIST_FOREACH(prr, &allprison_racct, prr_next) 4692a7ad07bfSEdward Tomasz Napierala (callback)(prr->prr_racct, arg2, arg3); 469315db3c07SEdward Tomasz Napierala if (post != NULL) 469415db3c07SEdward Tomasz Napierala (post)(); 4695097055e2SEdward Tomasz Napierala sx_sunlock(&allprison_lock); 4696097055e2SEdward Tomasz Napierala } 46970304c731SJamie Gritton 4698a7ad07bfSEdward Tomasz Napierala static struct prison_racct * 4699a7ad07bfSEdward Tomasz Napierala prison_racct_find_locked(const char *name) 4700a7ad07bfSEdward Tomasz Napierala { 4701a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4702a7ad07bfSEdward Tomasz Napierala 47034b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4704a7ad07bfSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4705a7ad07bfSEdward Tomasz Napierala 4706a7ad07bfSEdward Tomasz Napierala if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN) 4707a7ad07bfSEdward Tomasz Napierala return (NULL); 4708a7ad07bfSEdward Tomasz Napierala 4709a7ad07bfSEdward Tomasz Napierala LIST_FOREACH(prr, &allprison_racct, prr_next) { 4710a7ad07bfSEdward Tomasz Napierala if (strcmp(name, prr->prr_name) != 0) 4711a7ad07bfSEdward Tomasz Napierala continue; 4712a7ad07bfSEdward Tomasz Napierala 4713a7ad07bfSEdward Tomasz Napierala /* Found prison_racct with a matching name? */ 4714a7ad07bfSEdward Tomasz Napierala prison_racct_hold(prr); 4715a7ad07bfSEdward Tomasz Napierala return (prr); 4716a7ad07bfSEdward Tomasz Napierala } 4717a7ad07bfSEdward Tomasz Napierala 4718a7ad07bfSEdward Tomasz Napierala /* Add new prison_racct. */ 4719a7ad07bfSEdward Tomasz Napierala prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK); 4720a7ad07bfSEdward Tomasz Napierala racct_create(&prr->prr_racct); 4721a7ad07bfSEdward Tomasz Napierala 4722a7ad07bfSEdward Tomasz Napierala strcpy(prr->prr_name, name); 4723a7ad07bfSEdward Tomasz Napierala refcount_init(&prr->prr_refcount, 1); 4724a7ad07bfSEdward Tomasz Napierala LIST_INSERT_HEAD(&allprison_racct, prr, prr_next); 4725a7ad07bfSEdward Tomasz Napierala 4726a7ad07bfSEdward Tomasz Napierala return (prr); 4727a7ad07bfSEdward Tomasz Napierala } 4728a7ad07bfSEdward Tomasz Napierala 4729a7ad07bfSEdward Tomasz Napierala struct prison_racct * 4730a7ad07bfSEdward Tomasz Napierala prison_racct_find(const char *name) 4731a7ad07bfSEdward Tomasz Napierala { 4732a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4733a7ad07bfSEdward Tomasz Napierala 47344b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 47354b5c9cf6SEdward Tomasz Napierala 4736a7ad07bfSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4737a7ad07bfSEdward Tomasz Napierala prr = prison_racct_find_locked(name); 4738a7ad07bfSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4739a7ad07bfSEdward Tomasz Napierala return (prr); 4740a7ad07bfSEdward Tomasz Napierala } 4741a7ad07bfSEdward Tomasz Napierala 4742a7ad07bfSEdward Tomasz Napierala void 4743a7ad07bfSEdward Tomasz Napierala prison_racct_hold(struct prison_racct *prr) 4744a7ad07bfSEdward Tomasz Napierala { 4745a7ad07bfSEdward Tomasz Napierala 47464b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 47474b5c9cf6SEdward Tomasz Napierala 4748a7ad07bfSEdward Tomasz Napierala refcount_acquire(&prr->prr_refcount); 4749a7ad07bfSEdward Tomasz Napierala } 4750a7ad07bfSEdward Tomasz Napierala 4751c34bbd2aSEdward Tomasz Napierala static void 4752c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(struct prison_racct *prr) 4753c34bbd2aSEdward Tomasz Napierala { 4754c34bbd2aSEdward Tomasz Napierala 47554b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4756c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4757c34bbd2aSEdward Tomasz Napierala 4758c34bbd2aSEdward Tomasz Napierala if (refcount_release(&prr->prr_refcount)) { 4759c34bbd2aSEdward Tomasz Napierala racct_destroy(&prr->prr_racct); 4760c34bbd2aSEdward Tomasz Napierala LIST_REMOVE(prr, prr_next); 4761c34bbd2aSEdward Tomasz Napierala free(prr, M_PRISON_RACCT); 4762c34bbd2aSEdward Tomasz Napierala } 4763c34bbd2aSEdward Tomasz Napierala } 4764c34bbd2aSEdward Tomasz Napierala 4765a7ad07bfSEdward Tomasz Napierala void 4766a7ad07bfSEdward Tomasz Napierala prison_racct_free(struct prison_racct *prr) 4767a7ad07bfSEdward Tomasz Napierala { 4768a7ad07bfSEdward Tomasz Napierala 47694b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4770c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_UNLOCKED); 4771c34bbd2aSEdward Tomasz Napierala 47726ff4688bSMateusz Guzik if (refcount_release_if_not_last(&prr->prr_refcount)) 4773a7ad07bfSEdward Tomasz Napierala return; 4774a7ad07bfSEdward Tomasz Napierala 4775a7ad07bfSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4776c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(prr); 4777a7ad07bfSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4778a7ad07bfSEdward Tomasz Napierala } 4779a7ad07bfSEdward Tomasz Napierala 4780a7ad07bfSEdward Tomasz Napierala static void 4781a7ad07bfSEdward Tomasz Napierala prison_racct_attach(struct prison *pr) 4782a7ad07bfSEdward Tomasz Napierala { 4783a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4784a7ad07bfSEdward Tomasz Napierala 47854b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4786c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4787c34bbd2aSEdward Tomasz Napierala 4788a7ad07bfSEdward Tomasz Napierala prr = prison_racct_find_locked(pr->pr_name); 4789a7ad07bfSEdward Tomasz Napierala KASSERT(prr != NULL, ("cannot find prison_racct")); 4790a7ad07bfSEdward Tomasz Napierala 4791a7ad07bfSEdward Tomasz Napierala pr->pr_prison_racct = prr; 4792a7ad07bfSEdward Tomasz Napierala } 4793a7ad07bfSEdward Tomasz Napierala 4794c34bbd2aSEdward Tomasz Napierala /* 4795c34bbd2aSEdward Tomasz Napierala * Handle jail renaming. From the racct point of view, renaming means 4796c34bbd2aSEdward Tomasz Napierala * moving from one prison_racct to another. 4797c34bbd2aSEdward Tomasz Napierala */ 4798c34bbd2aSEdward Tomasz Napierala static void 4799c34bbd2aSEdward Tomasz Napierala prison_racct_modify(struct prison *pr) 4800c34bbd2aSEdward Tomasz Napierala { 4801dbadb015SKonstantin Belousov #ifdef RCTL 4802c34bbd2aSEdward Tomasz Napierala struct proc *p; 4803c34bbd2aSEdward Tomasz Napierala struct ucred *cred; 4804dbadb015SKonstantin Belousov #endif 4805c34bbd2aSEdward Tomasz Napierala struct prison_racct *oldprr; 4806c34bbd2aSEdward Tomasz Napierala 48074b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 48084b5c9cf6SEdward Tomasz Napierala 4809c34bbd2aSEdward Tomasz Napierala sx_slock(&allproc_lock); 4810c34bbd2aSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4811c34bbd2aSEdward Tomasz Napierala 4812e30345e7SEdward Tomasz Napierala if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) { 4813e30345e7SEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4814e30345e7SEdward Tomasz Napierala sx_sunlock(&allproc_lock); 4815c34bbd2aSEdward Tomasz Napierala return; 4816e30345e7SEdward Tomasz Napierala } 4817c34bbd2aSEdward Tomasz Napierala 4818c34bbd2aSEdward Tomasz Napierala oldprr = pr->pr_prison_racct; 4819c34bbd2aSEdward Tomasz Napierala pr->pr_prison_racct = NULL; 4820c34bbd2aSEdward Tomasz Napierala 4821c34bbd2aSEdward Tomasz Napierala prison_racct_attach(pr); 4822c34bbd2aSEdward Tomasz Napierala 4823c34bbd2aSEdward Tomasz Napierala /* 4824c34bbd2aSEdward Tomasz Napierala * Move resource utilisation records. 4825c34bbd2aSEdward Tomasz Napierala */ 4826c34bbd2aSEdward Tomasz Napierala racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct); 4827c34bbd2aSEdward Tomasz Napierala 4828f87beb93SAndriy Gapon #ifdef RCTL 4829c34bbd2aSEdward Tomasz Napierala /* 4830c34bbd2aSEdward Tomasz Napierala * Force rctl to reattach rules to processes. 4831c34bbd2aSEdward Tomasz Napierala */ 4832c34bbd2aSEdward Tomasz Napierala FOREACH_PROC_IN_SYSTEM(p) { 4833c34bbd2aSEdward Tomasz Napierala PROC_LOCK(p); 4834c34bbd2aSEdward Tomasz Napierala cred = crhold(p->p_ucred); 4835c34bbd2aSEdward Tomasz Napierala PROC_UNLOCK(p); 4836f87beb93SAndriy Gapon rctl_proc_ucred_changed(p, cred); 4837c34bbd2aSEdward Tomasz Napierala crfree(cred); 4838c34bbd2aSEdward Tomasz Napierala } 4839f87beb93SAndriy Gapon #endif 4840c34bbd2aSEdward Tomasz Napierala 4841c34bbd2aSEdward Tomasz Napierala sx_sunlock(&allproc_lock); 4842c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(oldprr); 4843c34bbd2aSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4844c34bbd2aSEdward Tomasz Napierala } 4845c34bbd2aSEdward Tomasz Napierala 4846a7ad07bfSEdward Tomasz Napierala static void 4847a7ad07bfSEdward Tomasz Napierala prison_racct_detach(struct prison *pr) 4848a7ad07bfSEdward Tomasz Napierala { 4849c34bbd2aSEdward Tomasz Napierala 48504b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4851c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_UNLOCKED); 4852c34bbd2aSEdward Tomasz Napierala 4853af3c786cSMateusz Guzik if (pr->pr_prison_racct == NULL) 4854af3c786cSMateusz Guzik return; 4855a7ad07bfSEdward Tomasz Napierala prison_racct_free(pr->pr_prison_racct); 4856a7ad07bfSEdward Tomasz Napierala pr->pr_prison_racct = NULL; 4857a7ad07bfSEdward Tomasz Napierala } 4858a7ad07bfSEdward Tomasz Napierala #endif /* RACCT */ 4859a7ad07bfSEdward Tomasz Napierala 4860413628a7SBjoern A. Zeeb #ifdef DDB 4861b38ff370SJamie Gritton 4862b38ff370SJamie Gritton static void 4863b38ff370SJamie Gritton db_show_prison(struct prison *pr) 4864413628a7SBjoern A. Zeeb { 4865672756aaSJamie Gritton struct bool_flags *bf; 4866672756aaSJamie Gritton struct jailsys_flags *jsf; 4867b38ff370SJamie Gritton #if defined(INET) || defined(INET6) 4868b38ff370SJamie Gritton int ii; 4869413628a7SBjoern A. Zeeb #endif 4870672756aaSJamie Gritton unsigned f; 48718144690aSEric van Gyzen #ifdef INET 48728144690aSEric van Gyzen char ip4buf[INET_ADDRSTRLEN]; 48738144690aSEric van Gyzen #endif 4874413628a7SBjoern A. Zeeb #ifdef INET6 4875413628a7SBjoern A. Zeeb char ip6buf[INET6_ADDRSTRLEN]; 4876413628a7SBjoern A. Zeeb #endif 4877413628a7SBjoern A. Zeeb 4878b38ff370SJamie Gritton db_printf("prison %p:\n", pr); 4879b38ff370SJamie Gritton db_printf(" jid = %d\n", pr->pr_id); 4880b38ff370SJamie Gritton db_printf(" name = %s\n", pr->pr_name); 48810304c731SJamie Gritton db_printf(" parent = %p\n", pr->pr_parent); 4882b38ff370SJamie Gritton db_printf(" ref = %d\n", pr->pr_ref); 4883b38ff370SJamie Gritton db_printf(" uref = %d\n", pr->pr_uref); 48841158508aSJamie Gritton db_printf(" state = %s\n", 48851158508aSJamie Gritton pr->pr_state == PRISON_STATE_ALIVE ? "alive" : 48861158508aSJamie Gritton pr->pr_state == PRISON_STATE_DYING ? "dying" : 48871158508aSJamie Gritton "invalid"); 4888b38ff370SJamie Gritton db_printf(" path = %s\n", pr->pr_path); 4889b38ff370SJamie Gritton db_printf(" cpuset = %d\n", pr->pr_cpuset 4890b38ff370SJamie Gritton ? pr->pr_cpuset->cs_id : -1); 4891679e1390SJamie Gritton #ifdef VIMAGE 4892679e1390SJamie Gritton db_printf(" vnet = %p\n", pr->pr_vnet); 4893679e1390SJamie Gritton #endif 4894b38ff370SJamie Gritton db_printf(" root = %p\n", pr->pr_root); 4895b38ff370SJamie Gritton db_printf(" securelevel = %d\n", pr->pr_securelevel); 48960cc207a6SMartin Matuska db_printf(" devfs_rsnum = %d\n", pr->pr_devfs_rsnum); 4897fe0518e9SBjoern A. Zeeb db_printf(" children.max = %d\n", pr->pr_childmax); 4898fe0518e9SBjoern A. Zeeb db_printf(" children.cur = %d\n", pr->pr_childcount); 48990304c731SJamie Gritton db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children)); 49000304c731SJamie Gritton db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling)); 4901fe0518e9SBjoern A. Zeeb db_printf(" flags = 0x%x", pr->pr_flags); 4902672756aaSJamie Gritton for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++) 4903672756aaSJamie Gritton if (pr->pr_flags & bf->flag) 4904672756aaSJamie Gritton db_printf(" %s", bf->name); 4905672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 4906672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 4907672756aaSJamie Gritton jsf++) { 4908672756aaSJamie Gritton f = pr->pr_flags & (jsf->disable | jsf->new); 4909672756aaSJamie Gritton db_printf(" %-16s= %s\n", jsf->name, 4910672756aaSJamie Gritton (f != 0 && f == jsf->disable) ? "disable" 4911672756aaSJamie Gritton : (f == jsf->new) ? "new" 49127cbf7213SJamie Gritton : "inherit"); 49137cbf7213SJamie Gritton } 4914fe0518e9SBjoern A. Zeeb db_printf(" allow = 0x%x", pr->pr_allow); 4915672756aaSJamie Gritton for (bf = pr_flag_allow; 49165d58f959SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 49175d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 4918672756aaSJamie Gritton bf++) 4919672756aaSJamie Gritton if (pr->pr_allow & bf->flag) 4920672756aaSJamie Gritton db_printf(" %s", bf->name); 4921b38ff370SJamie Gritton db_printf("\n"); 49220304c731SJamie Gritton db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs); 4923c1f19219SJamie Gritton db_printf(" host.hostname = %s\n", pr->pr_hostname); 4924c1f19219SJamie Gritton db_printf(" host.domainname = %s\n", pr->pr_domainname); 4925c1f19219SJamie Gritton db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid); 492676ca6f88SJamie Gritton db_printf(" host.hostid = %lu\n", pr->pr_hostid); 4927413628a7SBjoern A. Zeeb #ifdef INET 4928eb8dcdeaSGleb Smirnoff if (pr->pr_addrs[PR_INET] != NULL) { 4929eb8dcdeaSGleb Smirnoff pr_family_t af = PR_INET; 4930eb8dcdeaSGleb Smirnoff 4931eb8dcdeaSGleb Smirnoff db_printf(" ip4s = %d\n", pr->pr_addrs[af]->ips); 4932eb8dcdeaSGleb Smirnoff for (ii = 0; ii < pr->pr_addrs[af]->ips; ii++) 4933b38ff370SJamie Gritton db_printf(" %s %s\n", 4934fe0518e9SBjoern A. Zeeb ii == 0 ? "ip4.addr =" : " ", 4935eb8dcdeaSGleb Smirnoff inet_ntoa_r( 493621ad3e27SZhenlei Huang *(const struct in_addr *)PR_IP(pr->pr_addrs[af], ii), 4937eb8dcdeaSGleb Smirnoff ip4buf)); 4938eb8dcdeaSGleb Smirnoff } 4939413628a7SBjoern A. Zeeb #endif 4940413628a7SBjoern A. Zeeb #ifdef INET6 4941eb8dcdeaSGleb Smirnoff if (pr->pr_addrs[PR_INET6] != NULL) { 4942eb8dcdeaSGleb Smirnoff pr_family_t af = PR_INET6; 4943eb8dcdeaSGleb Smirnoff 4944eb8dcdeaSGleb Smirnoff db_printf(" ip6s = %d\n", pr->pr_addrs[af]->ips); 4945eb8dcdeaSGleb Smirnoff for (ii = 0; ii < pr->pr_addrs[af]->ips; ii++) 4946b38ff370SJamie Gritton db_printf(" %s %s\n", 4947fe0518e9SBjoern A. Zeeb ii == 0 ? "ip6.addr =" : " ", 4948eb8dcdeaSGleb Smirnoff ip6_sprintf(ip6buf, 494921ad3e27SZhenlei Huang (const struct in6_addr *)PR_IP(pr->pr_addrs[af], ii))); 4950eb8dcdeaSGleb Smirnoff } 4951b38ff370SJamie Gritton #endif 4952b38ff370SJamie Gritton } 4953b38ff370SJamie Gritton 4954b38ff370SJamie Gritton DB_SHOW_COMMAND(prison, db_show_prison_command) 4955b38ff370SJamie Gritton { 4956b38ff370SJamie Gritton struct prison *pr; 4957b38ff370SJamie Gritton 4958b38ff370SJamie Gritton if (!have_addr) { 49590304c731SJamie Gritton /* 49600304c731SJamie Gritton * Show all prisons in the list, and prison0 which is not 49610304c731SJamie Gritton * listed. 49620304c731SJamie Gritton */ 49630304c731SJamie Gritton db_show_prison(&prison0); 49640304c731SJamie Gritton if (!db_pager_quit) { 4965b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 4966b38ff370SJamie Gritton db_show_prison(pr); 4967413628a7SBjoern A. Zeeb if (db_pager_quit) 4968413628a7SBjoern A. Zeeb break; 4969413628a7SBjoern A. Zeeb } 49700304c731SJamie Gritton } 4971b38ff370SJamie Gritton return; 4972413628a7SBjoern A. Zeeb } 4973b38ff370SJamie Gritton 49740304c731SJamie Gritton if (addr == 0) 49750304c731SJamie Gritton pr = &prison0; 49760304c731SJamie Gritton else { 4977b38ff370SJamie Gritton /* Look for a prison with the ID and with references. */ 4978b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 4979b38ff370SJamie Gritton if (pr->pr_id == addr && pr->pr_ref > 0) 4980b38ff370SJamie Gritton break; 4981b38ff370SJamie Gritton if (pr == NULL) 4982b38ff370SJamie Gritton /* Look again, without requiring a reference. */ 4983b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 4984b38ff370SJamie Gritton if (pr->pr_id == addr) 4985b38ff370SJamie Gritton break; 4986b38ff370SJamie Gritton if (pr == NULL) 4987b38ff370SJamie Gritton /* Assume address points to a valid prison. */ 4988b38ff370SJamie Gritton pr = (struct prison *)addr; 49890304c731SJamie Gritton } 4990b38ff370SJamie Gritton db_show_prison(pr); 4991b38ff370SJamie Gritton } 4992b38ff370SJamie Gritton 4993413628a7SBjoern A. Zeeb #endif /* DDB */ 4994