19454b2d8SWarner Losh /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 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> 32413628a7SBjoern A. Zeeb #include "opt_ddb.h" 33413628a7SBjoern A. Zeeb #include "opt_inet.h" 34413628a7SBjoern A. Zeeb #include "opt_inet6.h" 35bba7a2e8SRick Macklem #include "opt_nfs.h" 3646e3b1cbSPawel Jakub Dawidek 3775c13541SPoul-Henning Kamp #include <sys/param.h> 3875c13541SPoul-Henning Kamp #include <sys/types.h> 3975c13541SPoul-Henning Kamp #include <sys/kernel.h> 4075c13541SPoul-Henning Kamp #include <sys/systm.h> 4175c13541SPoul-Henning Kamp #include <sys/errno.h> 4275c13541SPoul-Henning Kamp #include <sys/sysproto.h> 4375c13541SPoul-Henning Kamp #include <sys/malloc.h> 440304c731SJamie Gritton #include <sys/osd.h> 45800c9408SRobert Watson #include <sys/priv.h> 4675c13541SPoul-Henning Kamp #include <sys/proc.h> 47eb8dcdeaSGleb Smirnoff #include <sys/epoch.h> 48b3059e09SRobert Watson #include <sys/taskqueue.h> 4957b4252eSKonstantin Belousov #include <sys/fcntl.h> 5075c13541SPoul-Henning Kamp #include <sys/jail.h> 51c3188289SKyle Evans #include <sys/linker.h> 5201137630SRobert Watson #include <sys/lock.h> 537060da62SJamie Gritton #include <sys/mman.h> 5401137630SRobert Watson #include <sys/mutex.h> 55097055e2SEdward Tomasz Napierala #include <sys/racct.h> 56f87beb93SAndriy Gapon #include <sys/rctl.h> 57a7ad07bfSEdward Tomasz Napierala #include <sys/refcount.h> 58dc68a633SPawel Jakub Dawidek #include <sys/sx.h> 5976ca6f88SJamie Gritton #include <sys/sysent.h> 60fd7a8150SMike Barcroft #include <sys/namei.h> 61820a0de9SPawel Jakub Dawidek #include <sys/mount.h> 62fd7a8150SMike Barcroft #include <sys/queue.h> 6375c13541SPoul-Henning Kamp #include <sys/socket.h> 64fd7a8150SMike Barcroft #include <sys/syscallsubr.h> 6583f1e257SRobert Watson #include <sys/sysctl.h> 66c3188289SKyle Evans #include <sys/uuid.h> 67fd7a8150SMike Barcroft #include <sys/vnode.h> 68530c0060SRobert Watson 6975c13541SPoul-Henning Kamp #include <net/if.h> 70530c0060SRobert Watson #include <net/vnet.h> 71530c0060SRobert Watson 7275c13541SPoul-Henning Kamp #include <netinet/in.h> 73530c0060SRobert Watson 74413628a7SBjoern A. Zeeb #ifdef DDB 75413628a7SBjoern A. Zeeb #include <ddb/ddb.h> 76413628a7SBjoern A. Zeeb #endif /* DDB */ 7775c13541SPoul-Henning Kamp 78aed55708SRobert Watson #include <security/mac/mac_framework.h> 79aed55708SRobert Watson 80c3188289SKyle Evans #define PRISON0_HOSTUUID_MODULE "hostuuid" 818986e3a0SJamie Gritton 8275c13541SPoul-Henning Kamp MALLOC_DEFINE(M_PRISON, "prison", "Prison structures"); 83d745c852SEd Schouten static MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures"); 8475c13541SPoul-Henning Kamp 85592bcae8SBjoern A. Zeeb /* Keep struct prison prison0 and some code in kern_jail_set() readable. */ 86592bcae8SBjoern A. Zeeb #ifdef INET 87592bcae8SBjoern A. Zeeb #ifdef INET6 88592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL|PR_IP6_SADDRSEL 89592bcae8SBjoern A. Zeeb #else 90592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL 91592bcae8SBjoern A. Zeeb #endif 92592bcae8SBjoern A. Zeeb #else /* !INET */ 93592bcae8SBjoern A. Zeeb #ifdef INET6 94592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL PR_IP6_SADDRSEL 95592bcae8SBjoern A. Zeeb #else 96592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL 0 97592bcae8SBjoern A. Zeeb #endif 98592bcae8SBjoern A. Zeeb #endif 99592bcae8SBjoern A. Zeeb 1000304c731SJamie Gritton /* prison0 describes what is "real" about the system. */ 1010304c731SJamie Gritton struct prison prison0 = { 1020304c731SJamie Gritton .pr_id = 0, 1030304c731SJamie Gritton .pr_name = "0", 1040304c731SJamie Gritton .pr_ref = 1, 1050304c731SJamie Gritton .pr_uref = 1, 1060304c731SJamie Gritton .pr_path = "/", 1070304c731SJamie Gritton .pr_securelevel = -1, 1080cc207a6SMartin Matuska .pr_devfs_rsnum = 0, 1091158508aSJamie Gritton .pr_state = PRISON_STATE_ALIVE, 110b97457e2SJamie Gritton .pr_childmax = JAIL_MAX, 1118986e3a0SJamie Gritton .pr_hostuuid = DEFAULT_HOSTUUID, 11213e403fdSAntoine Brodin .pr_children = LIST_HEAD_INITIALIZER(prison0.pr_children), 113eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE 114592bcae8SBjoern A. Zeeb .pr_flags = PR_HOST|PR_VNET|_PR_IP_SADDRSEL, 115eb79e1c7SBjoern A. Zeeb #else 116592bcae8SBjoern A. Zeeb .pr_flags = PR_HOST|_PR_IP_SADDRSEL, 117eb79e1c7SBjoern A. Zeeb #endif 1180e5c6bd4SJamie Gritton .pr_allow = PR_ALLOW_ALL_STATIC, 1190304c731SJamie Gritton }; 1200304c731SJamie Gritton MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF); 12183f1e257SRobert Watson 122672756aaSJamie Gritton struct bool_flags { 123672756aaSJamie Gritton const char *name; 124672756aaSJamie Gritton const char *noname; 1255d58f959SJamie Gritton volatile u_int flag; 126672756aaSJamie Gritton }; 127672756aaSJamie Gritton struct jailsys_flags { 128672756aaSJamie Gritton const char *name; 129672756aaSJamie Gritton unsigned disable; 130672756aaSJamie Gritton unsigned new; 131672756aaSJamie Gritton }; 132672756aaSJamie Gritton 133a7ad07bfSEdward Tomasz Napierala /* allprison, allprison_racct and lastprid are protected by allprison_lock. */ 134dc68a633SPawel Jakub Dawidek struct sx allprison_lock; 135b38ff370SJamie Gritton SX_SYSINIT(allprison_lock, &allprison_lock, "allprison"); 136b38ff370SJamie Gritton struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison); 137a7ad07bfSEdward Tomasz Napierala LIST_HEAD(, prison_racct) allprison_racct; 1382110d913SXin LI int lastprid = 0; 139*ed31b3f4SJamie Gritton int lastdeadid = 0; 140fd7a8150SMike Barcroft 1417de883c8SJamie Gritton static int get_next_prid(struct prison **insprp); 142*ed31b3f4SJamie Gritton static int get_next_deadid(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}, 222cbbb2203SRick Macklem #ifdef VIMAGE 223bba7a2e8SRick Macklem {"allow.nfsd", "allow.nonfsd", PR_ALLOW_NFSD}, 224bba7a2e8SRick Macklem #endif 225cb48780dSShawn Webb {"allow.extattr", "allow.noextattr", PR_ALLOW_EXTATTR}, 2260304c731SJamie Gritton }; 2275d58f959SJamie Gritton static unsigned pr_allow_all = PR_ALLOW_ALL_STATIC; 228672756aaSJamie Gritton const size_t pr_flag_allow_size = sizeof(pr_flag_allow); 2290304c731SJamie Gritton 230b3079544SJamie Gritton #define JAIL_DEFAULT_ALLOW (PR_ALLOW_SET_HOSTNAME | \ 231b3079544SJamie Gritton PR_ALLOW_RESERVED_PORTS | \ 23205e1e482SMariusz Zaborski PR_ALLOW_UNPRIV_DEBUG | \ 23305e1e482SMariusz Zaborski PR_ALLOW_SUSER) 23442bebd82SJamie Gritton #define JAIL_DEFAULT_ENFORCE_STATFS 2 235bf3db8aaSMartin Matuska #define JAIL_DEFAULT_DEVFS_RSNUM 0 2360304c731SJamie Gritton static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW; 23742bebd82SJamie Gritton static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 2380cc207a6SMartin Matuska static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM; 2390304c731SJamie Gritton #if defined(INET) || defined(INET6) 240e92e0574SJamie Gritton static unsigned jail_max_af_ips = 255; 2410304c731SJamie Gritton #endif 2420304c731SJamie Gritton 243b96bd95bSIan Lepore /* 244b96bd95bSIan Lepore * Initialize the parts of prison0 that can't be static-initialized with 245b96bd95bSIan Lepore * constants. This is called from proc0_init() after creating thread0 cpuset. 246b96bd95bSIan Lepore */ 247b96bd95bSIan Lepore void 248b96bd95bSIan Lepore prison0_init(void) 249b96bd95bSIan Lepore { 250c3188289SKyle Evans uint8_t *file, *data; 251c3188289SKyle Evans size_t size; 252d2ef3774SJessica Clarke char buf[sizeof(prison0.pr_hostuuid)]; 253d2ef3774SJessica Clarke bool valid; 254b96bd95bSIan Lepore 255b96bd95bSIan Lepore prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset); 256b96bd95bSIan Lepore prison0.pr_osreldate = osreldate; 257b96bd95bSIan Lepore strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease)); 258c3188289SKyle Evans 259c3188289SKyle Evans /* If we have a preloaded hostuuid, use it. */ 260c3188289SKyle Evans file = preload_search_by_type(PRISON0_HOSTUUID_MODULE); 261c3188289SKyle Evans if (file != NULL) { 262c3188289SKyle Evans data = preload_fetch_addr(file); 263c3188289SKyle Evans size = preload_fetch_size(file); 264c3188289SKyle Evans if (data != NULL) { 265c3188289SKyle Evans /* 266c3188289SKyle Evans * The preloaded data may include trailing whitespace, almost 267c3188289SKyle Evans * certainly a newline; skip over any whitespace or 268c3188289SKyle Evans * non-printable characters to be safe. 269c3188289SKyle Evans */ 270c3188289SKyle Evans while (size > 0 && data[size - 1] <= 0x20) { 271b6be9566SColin Percival size--; 272c3188289SKyle Evans } 273d2ef3774SJessica Clarke 274d2ef3774SJessica Clarke valid = false; 275d2ef3774SJessica Clarke 276d2ef3774SJessica Clarke /* 277d2ef3774SJessica Clarke * Not NUL-terminated when passed from loader, but 278d2ef3774SJessica Clarke * validate_uuid requires that due to using sscanf (as 279d2ef3774SJessica Clarke * does the subsequent strlcpy, since it still reads 280d2ef3774SJessica Clarke * past the given size to return the true length); 281d2ef3774SJessica Clarke * bounce to a temporary buffer to fix. 282d2ef3774SJessica Clarke */ 283d2ef3774SJessica Clarke if (size >= sizeof(buf)) 284d2ef3774SJessica Clarke goto done; 285d2ef3774SJessica Clarke 286d2ef3774SJessica Clarke memcpy(buf, data, size); 287d2ef3774SJessica Clarke buf[size] = '\0'; 288d2ef3774SJessica Clarke 289d2ef3774SJessica Clarke if (validate_uuid(buf, size, NULL, 0) != 0) 290d2ef3774SJessica Clarke goto done; 291d2ef3774SJessica Clarke 292d2ef3774SJessica Clarke valid = true; 293d2ef3774SJessica Clarke (void)strlcpy(prison0.pr_hostuuid, buf, 294d2ef3774SJessica Clarke sizeof(prison0.pr_hostuuid)); 295d2ef3774SJessica Clarke 296d2ef3774SJessica Clarke done: 297d2ef3774SJessica Clarke if (bootverbose && !valid) { 298330f110bSColin Percival printf("hostuuid: preload data malformed: '%.*s'\n", 299330f110bSColin Percival (int)size, data); 300c3188289SKyle Evans } 301c3188289SKyle Evans } 302c3188289SKyle Evans } 303c3188289SKyle Evans if (bootverbose) 304c3188289SKyle Evans printf("hostuuid: using %s\n", prison0.pr_hostuuid); 305b96bd95bSIan Lepore } 306b96bd95bSIan Lepore 307116734c4SMatthew Dillon /* 3089ddb7954SMike Barcroft * struct jail_args { 3099ddb7954SMike Barcroft * struct jail *jail; 3109ddb7954SMike Barcroft * }; 311116734c4SMatthew Dillon */ 31275c13541SPoul-Henning Kamp int 313c542c43eSJamie Gritton sys_jail(struct thread *td, struct jail_args *uap) 31475c13541SPoul-Henning Kamp { 315413628a7SBjoern A. Zeeb uint32_t version; 316413628a7SBjoern A. Zeeb int error; 3170304c731SJamie Gritton struct jail j; 318413628a7SBjoern A. Zeeb 319413628a7SBjoern A. Zeeb error = copyin(uap->jail, &version, sizeof(uint32_t)); 320413628a7SBjoern A. Zeeb if (error) 321413628a7SBjoern A. Zeeb return (error); 322413628a7SBjoern A. Zeeb 323413628a7SBjoern A. Zeeb switch (version) { 324413628a7SBjoern A. Zeeb case 0: 325413628a7SBjoern A. Zeeb { 326413628a7SBjoern A. Zeeb struct jail_v0 j0; 327413628a7SBjoern A. Zeeb 3280304c731SJamie Gritton /* FreeBSD single IPv4 jails. */ 3290304c731SJamie Gritton bzero(&j, sizeof(struct jail)); 330413628a7SBjoern A. Zeeb error = copyin(uap->jail, &j0, sizeof(struct jail_v0)); 331413628a7SBjoern A. Zeeb if (error) 332413628a7SBjoern A. Zeeb return (error); 3330304c731SJamie Gritton j.version = j0.version; 3340304c731SJamie Gritton j.path = j0.path; 3350304c731SJamie Gritton j.hostname = j0.hostname; 336b5019bc4SPeter Wemm j.ip4s = htonl(j0.ip_number); /* jail_v0 is host order */ 337413628a7SBjoern A. Zeeb break; 338413628a7SBjoern A. Zeeb } 339413628a7SBjoern A. Zeeb 340413628a7SBjoern A. Zeeb case 1: 341413628a7SBjoern A. Zeeb /* 342413628a7SBjoern A. Zeeb * Version 1 was used by multi-IPv4 jail implementations 343413628a7SBjoern A. Zeeb * that never made it into the official kernel. 344413628a7SBjoern A. Zeeb */ 345413628a7SBjoern A. Zeeb return (EINVAL); 346413628a7SBjoern A. Zeeb 347413628a7SBjoern A. Zeeb case 2: /* JAIL_API_VERSION */ 348413628a7SBjoern A. Zeeb /* FreeBSD multi-IPv4/IPv6,noIP jails. */ 349413628a7SBjoern A. Zeeb error = copyin(uap->jail, &j, sizeof(struct jail)); 350413628a7SBjoern A. Zeeb if (error) 351413628a7SBjoern A. Zeeb return (error); 3520304c731SJamie Gritton break; 3530304c731SJamie Gritton 3540304c731SJamie Gritton default: 3550304c731SJamie Gritton /* Sci-Fi jails are not supported, sorry. */ 3560304c731SJamie Gritton return (EINVAL); 3570304c731SJamie Gritton } 358c542c43eSJamie Gritton return (kern_jail(td, &j)); 3590304c731SJamie Gritton } 3600304c731SJamie Gritton 3610304c731SJamie Gritton int 362c542c43eSJamie Gritton kern_jail(struct thread *td, struct jail *j) 3630304c731SJamie Gritton { 364c542c43eSJamie Gritton struct iovec optiov[2 * (4 + nitems(pr_flag_allow) 365e92e0574SJamie Gritton #ifdef INET 366e92e0574SJamie Gritton + 1 367e92e0574SJamie Gritton #endif 368e92e0574SJamie Gritton #ifdef INET6 369e92e0574SJamie Gritton + 1 370e92e0574SJamie Gritton #endif 371e92e0574SJamie Gritton )]; 3720304c731SJamie Gritton struct uio opt; 3730304c731SJamie Gritton char *u_path, *u_hostname, *u_name; 374672756aaSJamie Gritton struct bool_flags *bf; 3750304c731SJamie Gritton #ifdef INET 376e92e0574SJamie Gritton uint32_t ip4s; 3770304c731SJamie Gritton struct in_addr *u_ip4; 3780304c731SJamie Gritton #endif 3790304c731SJamie Gritton #ifdef INET6 3800304c731SJamie Gritton struct in6_addr *u_ip6; 3810304c731SJamie Gritton #endif 3820304c731SJamie Gritton size_t tmplen; 383c542c43eSJamie Gritton int error, enforce_statfs; 3840304c731SJamie Gritton 3850304c731SJamie Gritton bzero(&optiov, sizeof(optiov)); 3860304c731SJamie Gritton opt.uio_iov = optiov; 3870304c731SJamie Gritton opt.uio_iovcnt = 0; 3880304c731SJamie Gritton opt.uio_offset = -1; 3890304c731SJamie Gritton opt.uio_resid = -1; 3900304c731SJamie Gritton opt.uio_segflg = UIO_SYSSPACE; 3910304c731SJamie Gritton opt.uio_rw = UIO_READ; 3920304c731SJamie Gritton opt.uio_td = td; 3930304c731SJamie Gritton 3940304c731SJamie Gritton /* Set permissions for top-level jails from sysctls. */ 3950304c731SJamie Gritton if (!jailed(td->td_ucred)) { 396672756aaSJamie Gritton for (bf = pr_flag_allow; 3970e5c6bd4SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 3985d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 399672756aaSJamie Gritton bf++) { 400672756aaSJamie Gritton optiov[opt.uio_iovcnt].iov_base = __DECONST(char *, 401672756aaSJamie Gritton (jail_default_allow & bf->flag) 402672756aaSJamie Gritton ? bf->name : bf->noname); 4030304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = 4040304c731SJamie Gritton strlen(optiov[opt.uio_iovcnt].iov_base) + 1; 4050304c731SJamie Gritton opt.uio_iovcnt += 2; 4060304c731SJamie Gritton } 4070304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "enforce_statfs"; 4080304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs"); 4090304c731SJamie Gritton opt.uio_iovcnt++; 4100304c731SJamie Gritton enforce_statfs = jail_default_enforce_statfs; 4110304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = &enforce_statfs; 4120304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs); 4130304c731SJamie Gritton opt.uio_iovcnt++; 4140304c731SJamie Gritton } 4150304c731SJamie Gritton 416b38ff370SJamie Gritton tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN; 417b38ff370SJamie Gritton #ifdef INET 4180304c731SJamie Gritton ip4s = (j->version == 0) ? 1 : j->ip4s; 4190304c731SJamie Gritton if (ip4s > jail_max_af_ips) 420b38ff370SJamie Gritton return (EINVAL); 4210304c731SJamie Gritton tmplen += ip4s * sizeof(struct in_addr); 422b38ff370SJamie Gritton #else 4230304c731SJamie Gritton if (j->ip4s > 0) 424b38ff370SJamie Gritton return (EINVAL); 425b38ff370SJamie Gritton #endif 426b38ff370SJamie Gritton #ifdef INET6 4270304c731SJamie Gritton if (j->ip6s > jail_max_af_ips) 428b38ff370SJamie Gritton return (EINVAL); 4290304c731SJamie Gritton tmplen += j->ip6s * sizeof(struct in6_addr); 430b38ff370SJamie Gritton #else 4310304c731SJamie Gritton if (j->ip6s > 0) 432b38ff370SJamie Gritton return (EINVAL); 433b38ff370SJamie Gritton #endif 434b38ff370SJamie Gritton u_path = malloc(tmplen, M_TEMP, M_WAITOK); 435b38ff370SJamie Gritton u_hostname = u_path + MAXPATHLEN; 436b38ff370SJamie Gritton u_name = u_hostname + MAXHOSTNAMELEN; 437b38ff370SJamie Gritton #ifdef INET 438b38ff370SJamie Gritton u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN); 439b38ff370SJamie Gritton #endif 440b38ff370SJamie Gritton #ifdef INET6 441b38ff370SJamie Gritton #ifdef INET 4420304c731SJamie Gritton u_ip6 = (struct in6_addr *)(u_ip4 + ip4s); 443b38ff370SJamie Gritton #else 444b38ff370SJamie Gritton u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN); 445b38ff370SJamie Gritton #endif 446b38ff370SJamie Gritton #endif 4470304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "path"; 4480304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("path"); 4490304c731SJamie Gritton opt.uio_iovcnt++; 4500304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_path; 4510304c731SJamie Gritton error = copyinstr(j->path, u_path, MAXPATHLEN, 4520304c731SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 453b38ff370SJamie Gritton if (error) { 454b38ff370SJamie Gritton free(u_path, M_TEMP); 455b38ff370SJamie Gritton return (error); 456b38ff370SJamie Gritton } 4570304c731SJamie Gritton opt.uio_iovcnt++; 4580304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "host.hostname"; 4590304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname"); 4600304c731SJamie Gritton opt.uio_iovcnt++; 4610304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_hostname; 4620304c731SJamie Gritton error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN, 4630304c731SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 464b38ff370SJamie Gritton if (error) { 465b38ff370SJamie Gritton free(u_path, M_TEMP); 466b38ff370SJamie Gritton return (error); 467b38ff370SJamie Gritton } 4680304c731SJamie Gritton opt.uio_iovcnt++; 4690304c731SJamie Gritton if (j->jailname != NULL) { 470b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "name"; 471b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("name"); 472b38ff370SJamie Gritton opt.uio_iovcnt++; 473b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_name; 4740304c731SJamie Gritton error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN, 475b38ff370SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 476b38ff370SJamie Gritton if (error) { 477b38ff370SJamie Gritton free(u_path, M_TEMP); 478b38ff370SJamie Gritton return (error); 479b38ff370SJamie Gritton } 480b38ff370SJamie Gritton opt.uio_iovcnt++; 481b38ff370SJamie Gritton } 482b38ff370SJamie Gritton #ifdef INET 483b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "ip4.addr"; 484b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr"); 485b38ff370SJamie Gritton opt.uio_iovcnt++; 486b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_ip4; 4870304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr); 4880304c731SJamie Gritton if (j->version == 0) 4890304c731SJamie Gritton u_ip4->s_addr = j->ip4s; 4900304c731SJamie Gritton else { 4910304c731SJamie Gritton error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len); 492b38ff370SJamie Gritton if (error) { 493b38ff370SJamie Gritton free(u_path, M_TEMP); 494b38ff370SJamie Gritton return (error); 495b38ff370SJamie Gritton } 4960304c731SJamie Gritton } 497b38ff370SJamie Gritton opt.uio_iovcnt++; 498b38ff370SJamie Gritton #endif 499b38ff370SJamie Gritton #ifdef INET6 500b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "ip6.addr"; 501b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr"); 502b38ff370SJamie Gritton opt.uio_iovcnt++; 503b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_ip6; 5040304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr); 5050304c731SJamie Gritton error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len); 506b38ff370SJamie Gritton if (error) { 507b38ff370SJamie Gritton free(u_path, M_TEMP); 508b38ff370SJamie Gritton return (error); 509b38ff370SJamie Gritton } 510b38ff370SJamie Gritton opt.uio_iovcnt++; 511b38ff370SJamie Gritton #endif 51202abd400SPedro F. Giffuni KASSERT(opt.uio_iovcnt <= nitems(optiov), 5130304c731SJamie Gritton ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt)); 514b38ff370SJamie Gritton error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH); 515b38ff370SJamie Gritton free(u_path, M_TEMP); 516b38ff370SJamie Gritton return (error); 517b38ff370SJamie Gritton } 518b38ff370SJamie Gritton 519b38ff370SJamie Gritton /* 520b38ff370SJamie Gritton * struct jail_set_args { 521b38ff370SJamie Gritton * struct iovec *iovp; 522b38ff370SJamie Gritton * unsigned int iovcnt; 523b38ff370SJamie Gritton * int flags; 524b38ff370SJamie Gritton * }; 525b38ff370SJamie Gritton */ 526b38ff370SJamie Gritton int 5278451d0ddSKip Macy sys_jail_set(struct thread *td, struct jail_set_args *uap) 528b38ff370SJamie Gritton { 529b38ff370SJamie Gritton struct uio *auio; 530b38ff370SJamie Gritton int error; 531b38ff370SJamie Gritton 532b38ff370SJamie Gritton /* Check that we have an even number of iovecs. */ 533b38ff370SJamie Gritton if (uap->iovcnt & 1) 534b38ff370SJamie Gritton return (EINVAL); 535b38ff370SJamie Gritton 536b38ff370SJamie Gritton error = copyinuio(uap->iovp, uap->iovcnt, &auio); 537b38ff370SJamie Gritton if (error) 538b38ff370SJamie Gritton return (error); 539b38ff370SJamie Gritton error = kern_jail_set(td, auio, uap->flags); 540b38ff370SJamie Gritton free(auio, M_IOV); 541b38ff370SJamie Gritton return (error); 542413628a7SBjoern A. Zeeb } 543413628a7SBjoern A. Zeeb 544eb8dcdeaSGleb Smirnoff #if defined(INET) || defined(INET6) 545eb8dcdeaSGleb Smirnoff typedef int prison_addr_cmp_t(const void *, const void *); 546eb8dcdeaSGleb Smirnoff typedef bool prison_addr_valid_t(const void *); 547eb8dcdeaSGleb Smirnoff static const struct pr_family { 548eb8dcdeaSGleb Smirnoff size_t size; 549eb8dcdeaSGleb Smirnoff prison_addr_cmp_t *cmp; 550eb8dcdeaSGleb Smirnoff prison_addr_valid_t *valid; 551eb8dcdeaSGleb Smirnoff int ip_flag; 552eb8dcdeaSGleb Smirnoff } pr_families[PR_FAMILY_MAX] = { 553eb8dcdeaSGleb Smirnoff #ifdef INET 554eb8dcdeaSGleb Smirnoff [PR_INET] = { 555eb8dcdeaSGleb Smirnoff .size = sizeof(struct in_addr), 556eb8dcdeaSGleb Smirnoff .cmp = prison_qcmp_v4, 557eb8dcdeaSGleb Smirnoff .valid = prison_valid_v4, 558eb8dcdeaSGleb Smirnoff .ip_flag = PR_IP4_USER, 559eb8dcdeaSGleb Smirnoff }, 560eb8dcdeaSGleb Smirnoff #endif 561eb8dcdeaSGleb Smirnoff #ifdef INET6 562eb8dcdeaSGleb Smirnoff [PR_INET6] = { 563eb8dcdeaSGleb Smirnoff .size = sizeof(struct in6_addr), 564eb8dcdeaSGleb Smirnoff .cmp = prison_qcmp_v6, 565eb8dcdeaSGleb Smirnoff .valid = prison_valid_v6, 566eb8dcdeaSGleb Smirnoff .ip_flag = PR_IP6_USER, 567eb8dcdeaSGleb Smirnoff }, 568eb8dcdeaSGleb Smirnoff #endif 569eb8dcdeaSGleb Smirnoff }; 570eb8dcdeaSGleb Smirnoff 571eb8dcdeaSGleb Smirnoff /* 572eb8dcdeaSGleb Smirnoff * Network address lists (pr_addrs) allocation for jails. The addresses 573eb8dcdeaSGleb Smirnoff * are accessed locklessly by the network stack, thus need to be protected by 574eb8dcdeaSGleb Smirnoff * the network epoch. 575eb8dcdeaSGleb Smirnoff */ 576eb8dcdeaSGleb Smirnoff struct prison_ip { 577eb8dcdeaSGleb Smirnoff struct epoch_context ctx; 578eb8dcdeaSGleb Smirnoff uint32_t ips; 579eb8dcdeaSGleb Smirnoff #ifdef FUTURE_C 580500f82d6SZhenlei Huang /* 581500f82d6SZhenlei Huang * XXX Variable-length automatic arrays in union may be 582500f82d6SZhenlei Huang * supported in future C. 583500f82d6SZhenlei Huang */ 584eb8dcdeaSGleb Smirnoff union { 585500f82d6SZhenlei Huang char pr_ip[]; 586eb8dcdeaSGleb Smirnoff struct in_addr pr_ip4[]; 587eb8dcdeaSGleb Smirnoff struct in6_addr pr_ip6[]; 588eb8dcdeaSGleb Smirnoff }; 589eb8dcdeaSGleb Smirnoff #else /* No future C :( */ 590500f82d6SZhenlei Huang char pr_ip[]; 591eb8dcdeaSGleb Smirnoff #endif 592eb8dcdeaSGleb Smirnoff }; 593eb8dcdeaSGleb Smirnoff 594500f82d6SZhenlei Huang static char * 595500f82d6SZhenlei Huang PR_IP(struct prison_ip *pip, const pr_family_t af, int idx) 596500f82d6SZhenlei Huang { 597500f82d6SZhenlei Huang MPASS(pip); 598500f82d6SZhenlei Huang MPASS(af < PR_FAMILY_MAX); 599500f82d6SZhenlei Huang MPASS(idx >= 0 && idx < pip->ips); 600500f82d6SZhenlei Huang 601500f82d6SZhenlei Huang return (pip->pr_ip + pr_families[af].size * idx); 602500f82d6SZhenlei Huang } 603500f82d6SZhenlei Huang 604eb8dcdeaSGleb Smirnoff static struct prison_ip * 605eb8dcdeaSGleb Smirnoff prison_ip_alloc(const pr_family_t af, uint32_t cnt, int flags) 606eb8dcdeaSGleb Smirnoff { 607eb8dcdeaSGleb Smirnoff struct prison_ip *pip; 608eb8dcdeaSGleb Smirnoff 609eb8dcdeaSGleb Smirnoff pip = malloc(sizeof(struct prison_ip) + cnt * pr_families[af].size, 610eb8dcdeaSGleb Smirnoff M_PRISON, flags); 611eb8dcdeaSGleb Smirnoff if (pip != NULL) 612eb8dcdeaSGleb Smirnoff pip->ips = cnt; 613eb8dcdeaSGleb Smirnoff return (pip); 614eb8dcdeaSGleb Smirnoff } 615eb8dcdeaSGleb Smirnoff 616eb8dcdeaSGleb Smirnoff /* 617eb8dcdeaSGleb Smirnoff * Allocate and copyin user supplied address list, sorting and validating. 618eb8dcdeaSGleb Smirnoff * kern_jail_set() helper. 619eb8dcdeaSGleb Smirnoff */ 620eb8dcdeaSGleb Smirnoff static struct prison_ip * 621eb8dcdeaSGleb Smirnoff prison_ip_copyin(const pr_family_t af, void *op, uint32_t cnt) 622eb8dcdeaSGleb Smirnoff { 623eb8dcdeaSGleb Smirnoff prison_addr_cmp_t *const cmp = pr_families[af].cmp; 624eb8dcdeaSGleb Smirnoff const size_t size = pr_families[af].size; 625eb8dcdeaSGleb Smirnoff struct prison_ip *pip; 626eb8dcdeaSGleb Smirnoff 627eb8dcdeaSGleb Smirnoff pip = prison_ip_alloc(af, cnt, M_WAITOK); 628500f82d6SZhenlei Huang bcopy(op, pip->pr_ip, cnt * size); 629eb8dcdeaSGleb Smirnoff /* 630eb8dcdeaSGleb Smirnoff * IP addresses are all sorted but ip[0] to preserve 631eb8dcdeaSGleb Smirnoff * the primary IP address as given from userland. 632eb8dcdeaSGleb Smirnoff * This special IP is used for unbound outgoing 633eb8dcdeaSGleb Smirnoff * connections as well for "loopback" traffic in case 634eb8dcdeaSGleb Smirnoff * source address selection cannot find any more fitting 635eb8dcdeaSGleb Smirnoff * address to connect from. 636eb8dcdeaSGleb Smirnoff */ 637eb8dcdeaSGleb Smirnoff if (cnt > 1) 6382c33b456SZhenlei Huang qsort(PR_IP(pip, af, 1), cnt - 1, size, cmp); 639eb8dcdeaSGleb Smirnoff /* 640eb8dcdeaSGleb Smirnoff * Check for duplicate addresses and do some simple 641eb8dcdeaSGleb Smirnoff * zero and broadcast checks. If users give other bogus 642eb8dcdeaSGleb Smirnoff * addresses it is their problem. 643eb8dcdeaSGleb Smirnoff */ 644eb8dcdeaSGleb Smirnoff for (int i = 0; i < cnt; i++) { 645500f82d6SZhenlei Huang if (!pr_families[af].valid(PR_IP(pip, af, i))) { 646eb8dcdeaSGleb Smirnoff free(pip, M_PRISON); 647eb8dcdeaSGleb Smirnoff return (NULL); 648eb8dcdeaSGleb Smirnoff } 649eb8dcdeaSGleb Smirnoff if (i + 1 < cnt && 650500f82d6SZhenlei Huang (cmp(PR_IP(pip, af, 0), PR_IP(pip, af, i + 1)) == 0 || 651500f82d6SZhenlei Huang cmp(PR_IP(pip, af, i), PR_IP(pip, af, i + 1)) == 0)) { 652eb8dcdeaSGleb Smirnoff free(pip, M_PRISON); 653eb8dcdeaSGleb Smirnoff return (NULL); 654eb8dcdeaSGleb Smirnoff } 655eb8dcdeaSGleb Smirnoff } 656eb8dcdeaSGleb Smirnoff 657eb8dcdeaSGleb Smirnoff return (pip); 658eb8dcdeaSGleb Smirnoff } 659eb8dcdeaSGleb Smirnoff 660eb8dcdeaSGleb Smirnoff /* 661eb8dcdeaSGleb Smirnoff * Allocate and dup parent prison address list. 662eb8dcdeaSGleb Smirnoff * kern_jail_set() helper. 663eb8dcdeaSGleb Smirnoff */ 664eb8dcdeaSGleb Smirnoff static void 665eb8dcdeaSGleb Smirnoff prison_ip_dup(struct prison *ppr, struct prison *pr, const pr_family_t af) 666eb8dcdeaSGleb Smirnoff { 6672c33b456SZhenlei Huang const struct prison_ip *ppip = ppr->pr_addrs[af]; 6682c33b456SZhenlei Huang struct prison_ip *pip; 669eb8dcdeaSGleb Smirnoff 6702c33b456SZhenlei Huang if (ppip != NULL) { 6712c33b456SZhenlei Huang pip = prison_ip_alloc(af, ppip->ips, M_WAITOK); 6722c33b456SZhenlei Huang bcopy(ppip->pr_ip, pip->pr_ip, pip->ips * pr_families[af].size); 6732c33b456SZhenlei Huang pr->pr_addrs[af] = pip; 674eb8dcdeaSGleb Smirnoff } 675eb8dcdeaSGleb Smirnoff } 676eb8dcdeaSGleb Smirnoff 677eb8dcdeaSGleb Smirnoff /* 678eb8dcdeaSGleb Smirnoff * Make sure the new set of IP addresses is a subset of the parent's list. 679eb8dcdeaSGleb Smirnoff * Don't worry about the parent being unlocked, as any setting is done with 680eb8dcdeaSGleb Smirnoff * allprison_lock held. 681eb8dcdeaSGleb Smirnoff * kern_jail_set() helper. 682eb8dcdeaSGleb Smirnoff */ 683eb8dcdeaSGleb Smirnoff static bool 684500f82d6SZhenlei Huang prison_ip_parent_match(struct prison_ip *ppip, struct prison_ip *pip, 685500f82d6SZhenlei Huang const pr_family_t af) 686eb8dcdeaSGleb Smirnoff { 687eb8dcdeaSGleb Smirnoff prison_addr_cmp_t *const cmp = pr_families[af].cmp; 688eb8dcdeaSGleb Smirnoff int i, j; 689eb8dcdeaSGleb Smirnoff 690eb8dcdeaSGleb Smirnoff if (ppip == NULL) 691eb8dcdeaSGleb Smirnoff return (false); 692eb8dcdeaSGleb Smirnoff 693eb8dcdeaSGleb Smirnoff for (i = 0; i < ppip->ips; i++) 694500f82d6SZhenlei Huang if (cmp(PR_IP(pip, af, 0), PR_IP(ppip, af, i)) == 0) 695eb8dcdeaSGleb Smirnoff break; 696eb8dcdeaSGleb Smirnoff 697eb8dcdeaSGleb Smirnoff if (i == ppip->ips) 698eb8dcdeaSGleb Smirnoff /* Main address not present in parent. */ 699eb8dcdeaSGleb Smirnoff return (false); 700eb8dcdeaSGleb Smirnoff 701eb8dcdeaSGleb Smirnoff if (pip->ips > 1) { 702eb8dcdeaSGleb Smirnoff for (i = j = 1; i < pip->ips; i++) { 703500f82d6SZhenlei Huang if (cmp(PR_IP(pip, af, i), PR_IP(ppip, af, 0)) == 0) 704eb8dcdeaSGleb Smirnoff /* Equals to parent primary address. */ 705eb8dcdeaSGleb Smirnoff continue; 706eb8dcdeaSGleb Smirnoff for (; j < ppip->ips; j++) 707500f82d6SZhenlei Huang if (cmp(PR_IP(pip, af, i), 708500f82d6SZhenlei Huang PR_IP(ppip, af, j)) == 0) 709eb8dcdeaSGleb Smirnoff break; 710eb8dcdeaSGleb Smirnoff if (j == ppip->ips) 711eb8dcdeaSGleb Smirnoff break; 712eb8dcdeaSGleb Smirnoff } 713eb8dcdeaSGleb Smirnoff if (j == ppip->ips) 714eb8dcdeaSGleb Smirnoff /* Address not present in parent. */ 715eb8dcdeaSGleb Smirnoff return (false); 716eb8dcdeaSGleb Smirnoff } 717eb8dcdeaSGleb Smirnoff return (true); 718eb8dcdeaSGleb Smirnoff } 719eb8dcdeaSGleb Smirnoff 720eb8dcdeaSGleb Smirnoff /* 721eb8dcdeaSGleb Smirnoff * Check for conflicting IP addresses. We permit them if there is no more 722eb8dcdeaSGleb Smirnoff * than one IP on each jail. If there is a duplicate on a jail with more 723eb8dcdeaSGleb Smirnoff * than one IP stop checking and return error. 724eb8dcdeaSGleb Smirnoff * kern_jail_set() helper. 725eb8dcdeaSGleb Smirnoff */ 726eb8dcdeaSGleb Smirnoff static bool 727eb8dcdeaSGleb Smirnoff prison_ip_conflict_check(const struct prison *ppr, const struct prison *pr, 728500f82d6SZhenlei Huang struct prison_ip *pip, pr_family_t af) 729eb8dcdeaSGleb Smirnoff { 730eb8dcdeaSGleb Smirnoff const struct prison *tppr, *tpr; 731eb8dcdeaSGleb Smirnoff int descend; 732eb8dcdeaSGleb Smirnoff 733eb8dcdeaSGleb Smirnoff #ifdef VIMAGE 734eb8dcdeaSGleb Smirnoff for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent) 735eb8dcdeaSGleb Smirnoff if (tppr->pr_flags & PR_VNET) 736eb8dcdeaSGleb Smirnoff break; 737eb8dcdeaSGleb Smirnoff #else 738eb8dcdeaSGleb Smirnoff tppr = &prison0; 739eb8dcdeaSGleb Smirnoff #endif 740eb8dcdeaSGleb Smirnoff FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 741eb8dcdeaSGleb Smirnoff if (tpr == pr || 742eb8dcdeaSGleb Smirnoff #ifdef VIMAGE 743eb8dcdeaSGleb Smirnoff (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 744eb8dcdeaSGleb Smirnoff #endif 745eb8dcdeaSGleb Smirnoff !prison_isalive(tpr)) { 746eb8dcdeaSGleb Smirnoff descend = 0; 747eb8dcdeaSGleb Smirnoff continue; 748eb8dcdeaSGleb Smirnoff } 749eb8dcdeaSGleb Smirnoff if (!(tpr->pr_flags & pr_families[af].ip_flag)) 750eb8dcdeaSGleb Smirnoff continue; 751eb8dcdeaSGleb Smirnoff descend = 0; 752eb8dcdeaSGleb Smirnoff if (tpr->pr_addrs[af] == NULL || 753eb8dcdeaSGleb Smirnoff (pip->ips == 1 && tpr->pr_addrs[af]->ips == 1)) 754eb8dcdeaSGleb Smirnoff continue; 755eb8dcdeaSGleb Smirnoff for (int i = 0; i < pip->ips; i++) 756500f82d6SZhenlei Huang if (prison_ip_check(tpr, af, PR_IP(pip, af, i)) == 0) 757eb8dcdeaSGleb Smirnoff return (false); 758eb8dcdeaSGleb Smirnoff } 759eb8dcdeaSGleb Smirnoff 760eb8dcdeaSGleb Smirnoff return (true); 761eb8dcdeaSGleb Smirnoff } 762eb8dcdeaSGleb Smirnoff 763eb8dcdeaSGleb Smirnoff _Static_assert(offsetof(struct prison_ip, ctx) == 0, 764eb8dcdeaSGleb Smirnoff "prison must start with epoch context"); 765eb8dcdeaSGleb Smirnoff static void 766eb8dcdeaSGleb Smirnoff prison_ip_free_deferred(epoch_context_t ctx) 767eb8dcdeaSGleb Smirnoff { 768eb8dcdeaSGleb Smirnoff 769eb8dcdeaSGleb Smirnoff free(ctx, M_PRISON); 770eb8dcdeaSGleb Smirnoff } 771eb8dcdeaSGleb Smirnoff 772eb8dcdeaSGleb Smirnoff static void 773eb8dcdeaSGleb Smirnoff prison_ip_free(struct prison_ip *pip) 774eb8dcdeaSGleb Smirnoff { 775eb8dcdeaSGleb Smirnoff 776eb8dcdeaSGleb Smirnoff if (pip != NULL) 777eb8dcdeaSGleb Smirnoff NET_EPOCH_CALL(prison_ip_free_deferred, &pip->ctx); 778eb8dcdeaSGleb Smirnoff } 779eb8dcdeaSGleb Smirnoff 780eb8dcdeaSGleb Smirnoff static void 781eb8dcdeaSGleb Smirnoff prison_ip_set(struct prison *pr, const pr_family_t af, struct prison_ip *new) 782eb8dcdeaSGleb Smirnoff { 783eb8dcdeaSGleb Smirnoff struct prison_ip **mem, *old; 784eb8dcdeaSGleb Smirnoff 785eb8dcdeaSGleb Smirnoff mtx_assert(&pr->pr_mtx, MA_OWNED); 786eb8dcdeaSGleb Smirnoff 787eb8dcdeaSGleb Smirnoff mem = &pr->pr_addrs[af]; 788eb8dcdeaSGleb Smirnoff 789eb8dcdeaSGleb Smirnoff old = *mem; 79027202b98SMark Johnston atomic_store_ptr(mem, new); 791eb8dcdeaSGleb Smirnoff prison_ip_free(old); 792eb8dcdeaSGleb Smirnoff } 793eb8dcdeaSGleb Smirnoff 794eb8dcdeaSGleb Smirnoff /* 795eb8dcdeaSGleb Smirnoff * Restrict a prison's IP address list with its parent's, possibly replacing 7968bce8d28SZhenlei Huang * it. Return true if succeed, otherwise should redo. 797eb8dcdeaSGleb Smirnoff * kern_jail_set() helper. 798eb8dcdeaSGleb Smirnoff */ 799eb8dcdeaSGleb Smirnoff static bool 800eb8dcdeaSGleb Smirnoff prison_ip_restrict(struct prison *pr, const pr_family_t af, 8018bce8d28SZhenlei Huang struct prison_ip **newp) 802eb8dcdeaSGleb Smirnoff { 803500f82d6SZhenlei Huang struct prison_ip *ppip = pr->pr_parent->pr_addrs[af]; 804500f82d6SZhenlei Huang struct prison_ip *pip = pr->pr_addrs[af]; 805eb8dcdeaSGleb Smirnoff int (*const cmp)(const void *, const void *) = pr_families[af].cmp; 806eb8dcdeaSGleb Smirnoff const size_t size = pr_families[af].size; 8078bce8d28SZhenlei Huang struct prison_ip *new = newp != NULL ? *newp : NULL; 808eb8dcdeaSGleb Smirnoff uint32_t ips; 809eb8dcdeaSGleb Smirnoff 810eb8dcdeaSGleb Smirnoff mtx_assert(&pr->pr_mtx, MA_OWNED); 811eb8dcdeaSGleb Smirnoff 812eb8dcdeaSGleb Smirnoff /* 813eb8dcdeaSGleb Smirnoff * Due to epoch-synchronized access to the IP address lists we always 814eb8dcdeaSGleb Smirnoff * allocate a new list even if the old one has enough space. We could 815eb8dcdeaSGleb Smirnoff * atomically update an IPv4 address inside a list, but that would 816eb8dcdeaSGleb Smirnoff * screw up sorting, and in case of IPv6 we can't even atomically write 817eb8dcdeaSGleb Smirnoff * one. 818eb8dcdeaSGleb Smirnoff */ 81989ddfbbaSZhenlei Huang if (ppip == NULL) { 82089ddfbbaSZhenlei Huang if (pip != NULL) 821eb8dcdeaSGleb Smirnoff prison_ip_set(pr, af, NULL); 8228bce8d28SZhenlei Huang return (true); 823eb8dcdeaSGleb Smirnoff } 82489ddfbbaSZhenlei Huang 825eb8dcdeaSGleb Smirnoff if (!(pr->pr_flags & pr_families[af].ip_flag)) { 82689ddfbbaSZhenlei Huang if (new == NULL) { 82789ddfbbaSZhenlei Huang new = prison_ip_alloc(af, ppip->ips, M_NOWAIT); 82889ddfbbaSZhenlei Huang if (new == NULL) 8298bce8d28SZhenlei Huang return (false); /* Redo */ 8308bce8d28SZhenlei Huang } 831eb8dcdeaSGleb Smirnoff /* This has no user settings, so just copy the parent's list. */ 83289ddfbbaSZhenlei Huang MPASS(new->ips == ppip->ips); 833500f82d6SZhenlei Huang bcopy(ppip->pr_ip, new->pr_ip, ppip->ips * size); 83489ddfbbaSZhenlei Huang prison_ip_set(pr, af, new); 8358bce8d28SZhenlei Huang if (newp != NULL) 8368bce8d28SZhenlei Huang *newp = NULL; /* Used */ 83789ddfbbaSZhenlei Huang } else if (pip != NULL) { 838eb8dcdeaSGleb Smirnoff /* Remove addresses that aren't in the parent. */ 839eb8dcdeaSGleb Smirnoff int i; 840eb8dcdeaSGleb Smirnoff 841eb8dcdeaSGleb Smirnoff i = 0; /* index in pip */ 842eb8dcdeaSGleb Smirnoff ips = 0; /* index in new */ 843eb8dcdeaSGleb Smirnoff 84489ddfbbaSZhenlei Huang if (new == NULL) { 84589ddfbbaSZhenlei Huang new = prison_ip_alloc(af, pip->ips, M_NOWAIT); 84689ddfbbaSZhenlei Huang if (new == NULL) 8478bce8d28SZhenlei Huang return (false); /* Redo */ 84889ddfbbaSZhenlei Huang } 84989ddfbbaSZhenlei Huang 850eb8dcdeaSGleb Smirnoff for (int pi = 0; pi < ppip->ips; pi++) 851500f82d6SZhenlei Huang if (cmp(PR_IP(pip, af, 0), PR_IP(ppip, af, pi)) == 0) { 852eb8dcdeaSGleb Smirnoff /* Found our primary address in parent. */ 853500f82d6SZhenlei Huang bcopy(PR_IP(pip, af, i), PR_IP(new, af, ips), 854500f82d6SZhenlei Huang size); 855eb8dcdeaSGleb Smirnoff i++; 856eb8dcdeaSGleb Smirnoff ips++; 857eb8dcdeaSGleb Smirnoff break; 858eb8dcdeaSGleb Smirnoff } 859eb8dcdeaSGleb Smirnoff for (int pi = 1; i < pip->ips; ) { 860eb8dcdeaSGleb Smirnoff /* Check against primary, which is unsorted. */ 861500f82d6SZhenlei Huang if (cmp(PR_IP(pip, af, i), PR_IP(ppip, af, 0)) == 0) { 862eb8dcdeaSGleb Smirnoff /* Matches parent's primary address. */ 863500f82d6SZhenlei Huang bcopy(PR_IP(pip, af, i), PR_IP(new, af, ips), 864500f82d6SZhenlei Huang size); 865eb8dcdeaSGleb Smirnoff i++; 866eb8dcdeaSGleb Smirnoff ips++; 867eb8dcdeaSGleb Smirnoff continue; 868eb8dcdeaSGleb Smirnoff } 869eb8dcdeaSGleb Smirnoff /* The rest are sorted. */ 870eb8dcdeaSGleb Smirnoff switch (pi >= ppip->ips ? -1 : 871500f82d6SZhenlei Huang cmp(PR_IP(pip, af, i), PR_IP(ppip, af, pi))) { 872eb8dcdeaSGleb Smirnoff case -1: 873eb8dcdeaSGleb Smirnoff i++; 874eb8dcdeaSGleb Smirnoff break; 875eb8dcdeaSGleb Smirnoff case 0: 876500f82d6SZhenlei Huang bcopy(PR_IP(pip, af, i), PR_IP(new, af, ips), 877500f82d6SZhenlei Huang size); 878eb8dcdeaSGleb Smirnoff i++; 879eb8dcdeaSGleb Smirnoff pi++; 880eb8dcdeaSGleb Smirnoff ips++; 881eb8dcdeaSGleb Smirnoff break; 882eb8dcdeaSGleb Smirnoff case 1: 883eb8dcdeaSGleb Smirnoff pi++; 884eb8dcdeaSGleb Smirnoff break; 885eb8dcdeaSGleb Smirnoff } 886eb8dcdeaSGleb Smirnoff } 887eb8dcdeaSGleb Smirnoff if (ips == 0) { 8888bce8d28SZhenlei Huang if (newp == NULL || *newp == NULL) 889eb8dcdeaSGleb Smirnoff prison_ip_free(new); 890eb8dcdeaSGleb Smirnoff new = NULL; 89189ddfbbaSZhenlei Huang } else { 89289ddfbbaSZhenlei Huang /* Shrink to real size */ 89389ddfbbaSZhenlei Huang KASSERT((new->ips >= ips), 89489ddfbbaSZhenlei Huang ("Out-of-bounds write to prison_ip %p", new)); 89589ddfbbaSZhenlei Huang new->ips = ips; 896eb8dcdeaSGleb Smirnoff } 897eb8dcdeaSGleb Smirnoff prison_ip_set(pr, af, new); 8988bce8d28SZhenlei Huang if (newp != NULL) 8998bce8d28SZhenlei Huang *newp = NULL; /* Used */ 90089ddfbbaSZhenlei Huang } 9018bce8d28SZhenlei Huang return (true); 902eb8dcdeaSGleb Smirnoff } 903eb8dcdeaSGleb Smirnoff 904eb8dcdeaSGleb Smirnoff /* 905eb8dcdeaSGleb Smirnoff * Fast-path check if an address belongs to a prison. 906eb8dcdeaSGleb Smirnoff */ 907eb8dcdeaSGleb Smirnoff int 908eb8dcdeaSGleb Smirnoff prison_ip_check(const struct prison *pr, const pr_family_t af, 909eb8dcdeaSGleb Smirnoff const void *addr) 910eb8dcdeaSGleb Smirnoff { 911eb8dcdeaSGleb Smirnoff int (*const cmp)(const void *, const void *) = pr_families[af].cmp; 912500f82d6SZhenlei Huang struct prison_ip *pip; 913eb8dcdeaSGleb Smirnoff int i, a, z, d; 914eb8dcdeaSGleb Smirnoff 915eb8dcdeaSGleb Smirnoff MPASS(mtx_owned(&pr->pr_mtx) || 916eb8dcdeaSGleb Smirnoff in_epoch(net_epoch_preempt) || 917eb8dcdeaSGleb Smirnoff sx_xlocked(&allprison_lock)); 918eb8dcdeaSGleb Smirnoff 91927202b98SMark Johnston pip = atomic_load_ptr(&pr->pr_addrs[af]); 920eb8dcdeaSGleb Smirnoff if (__predict_false(pip == NULL)) 921eb8dcdeaSGleb Smirnoff return (EAFNOSUPPORT); 922eb8dcdeaSGleb Smirnoff 923eb8dcdeaSGleb Smirnoff /* Check the primary IP. */ 924500f82d6SZhenlei Huang if (cmp(PR_IP(pip, af, 0), addr) == 0) 925eb8dcdeaSGleb Smirnoff return (0); 926eb8dcdeaSGleb Smirnoff 927eb8dcdeaSGleb Smirnoff /* 928eb8dcdeaSGleb Smirnoff * All the other IPs are sorted so we can do a binary search. 929eb8dcdeaSGleb Smirnoff */ 930eb8dcdeaSGleb Smirnoff a = 0; 931eb8dcdeaSGleb Smirnoff z = pip->ips - 2; 932eb8dcdeaSGleb Smirnoff while (a <= z) { 933eb8dcdeaSGleb Smirnoff i = (a + z) / 2; 934500f82d6SZhenlei Huang d = cmp(PR_IP(pip, af, i + 1), addr); 935eb8dcdeaSGleb Smirnoff if (d > 0) 936eb8dcdeaSGleb Smirnoff z = i - 1; 937eb8dcdeaSGleb Smirnoff else if (d < 0) 938eb8dcdeaSGleb Smirnoff a = i + 1; 939eb8dcdeaSGleb Smirnoff else 940eb8dcdeaSGleb Smirnoff return (0); 941eb8dcdeaSGleb Smirnoff } 942eb8dcdeaSGleb Smirnoff 943eb8dcdeaSGleb Smirnoff return (EADDRNOTAVAIL); 944eb8dcdeaSGleb Smirnoff } 945eb8dcdeaSGleb Smirnoff 946eb8dcdeaSGleb Smirnoff /* 947eb8dcdeaSGleb Smirnoff * Grab primary IP. Historically required mutex, but nothing prevents 948eb8dcdeaSGleb Smirnoff * us to support epoch-protected access. Is it used in fast path? 949eb8dcdeaSGleb Smirnoff * in{6}_jail.c helper 950eb8dcdeaSGleb Smirnoff */ 951eb8dcdeaSGleb Smirnoff const void * 952eb8dcdeaSGleb Smirnoff prison_ip_get0(const struct prison *pr, const pr_family_t af) 953eb8dcdeaSGleb Smirnoff { 954eb8dcdeaSGleb Smirnoff const struct prison_ip *pip = pr->pr_addrs[af]; 955eb8dcdeaSGleb Smirnoff 956eb8dcdeaSGleb Smirnoff mtx_assert(&pr->pr_mtx, MA_OWNED); 957eb8dcdeaSGleb Smirnoff MPASS(pip); 958eb8dcdeaSGleb Smirnoff 959500f82d6SZhenlei Huang return (pip->pr_ip); 960eb8dcdeaSGleb Smirnoff } 961eb8dcdeaSGleb Smirnoff 962eb8dcdeaSGleb Smirnoff u_int 963eb8dcdeaSGleb Smirnoff prison_ip_cnt(const struct prison *pr, const pr_family_t af) 964eb8dcdeaSGleb Smirnoff { 965eb8dcdeaSGleb Smirnoff 966eb8dcdeaSGleb Smirnoff return (pr->pr_addrs[af]->ips); 967eb8dcdeaSGleb Smirnoff } 968eb8dcdeaSGleb Smirnoff #endif /* defined(INET) || defined(INET6) */ 969eb8dcdeaSGleb Smirnoff 970413628a7SBjoern A. Zeeb int 971b38ff370SJamie Gritton kern_jail_set(struct thread *td, struct uio *optuio, int flags) 972413628a7SBjoern A. Zeeb { 973fd7a8150SMike Barcroft struct nameidata nd; 974b38ff370SJamie Gritton #ifdef INET 975eb8dcdeaSGleb Smirnoff struct prison_ip *ip4; 976413628a7SBjoern A. Zeeb #endif 977b38ff370SJamie Gritton #ifdef INET6 978eb8dcdeaSGleb Smirnoff struct prison_ip *ip6; 979b38ff370SJamie Gritton #endif 980b38ff370SJamie Gritton struct vfsopt *opt; 981b38ff370SJamie Gritton struct vfsoptlist *opts; 982*ed31b3f4SJamie Gritton struct prison *pr, *deadpr, *dinspr, *inspr, *mypr, *ppr, *tpr; 983b38ff370SJamie Gritton struct vnode *root; 984babbbb9cSJamie Gritton char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid; 985b96bd95bSIan Lepore char *g_path, *osrelstr; 986672756aaSJamie Gritton struct bool_flags *bf; 987672756aaSJamie Gritton struct jailsys_flags *jsf; 9880304c731SJamie Gritton #if defined(INET) || defined(INET6) 989b38ff370SJamie Gritton void *op; 9900304c731SJamie Gritton #endif 99176ca6f88SJamie Gritton unsigned long hid; 992b6f47c23SJamie Gritton size_t namelen, onamelen, pnamelen; 993*ed31b3f4SJamie Gritton int created, cuflags, descend, drflags, enforce; 994cc5fd8c7SJamie Gritton int error, errmsg_len, errmsg_pos; 9950cc207a6SMartin Matuska int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel; 996*ed31b3f4SJamie Gritton int deadid, jid, jsys, len, level; 997b96bd95bSIan Lepore int childmax, osreldt, rsnum, slevel; 998413628a7SBjoern A. Zeeb #ifdef INET 9998bce8d28SZhenlei Huang int ip4s; 10008bce8d28SZhenlei Huang bool redo_ip4; 1001413628a7SBjoern A. Zeeb #endif 1002b38ff370SJamie Gritton #ifdef INET6 10038bce8d28SZhenlei Huang int ip6s; 10048bce8d28SZhenlei Huang bool redo_ip6; 1005b38ff370SJamie Gritton #endif 10066beb3bb4SKirk McKusick uint64_t pr_allow, ch_allow, pr_flags, ch_flags; 1007b3079544SJamie Gritton uint64_t pr_allow_diff; 10086beb3bb4SKirk McKusick unsigned tallow; 1009b38ff370SJamie Gritton char numbuf[12]; 1010b38ff370SJamie Gritton 1011b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_SET); 1012b38ff370SJamie Gritton if (!error && (flags & JAIL_ATTACH)) 1013b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 1014b38ff370SJamie Gritton if (error) 101575c13541SPoul-Henning Kamp return (error); 1016b6f47c23SJamie Gritton mypr = td->td_ucred->cr_prison; 1017b97457e2SJamie Gritton if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0) 10180304c731SJamie Gritton return (EPERM); 1019b38ff370SJamie Gritton if (flags & ~JAIL_SET_MASK) 1020b38ff370SJamie Gritton return (EINVAL); 1021b38ff370SJamie Gritton 1022b38ff370SJamie Gritton /* 1023b38ff370SJamie Gritton * Check all the parameters before committing to anything. Not all 1024b38ff370SJamie Gritton * errors can be caught early, but we may as well try. Also, this 1025b38ff370SJamie Gritton * takes care of some expensive stuff (path lookup) before getting 1026b38ff370SJamie Gritton * the allprison lock. 1027b38ff370SJamie Gritton * 1028b38ff370SJamie Gritton * XXX Jails are not filesystems, and jail parameters are not mount 1029b38ff370SJamie Gritton * options. But it makes more sense to re-use the vfsopt code 1030b38ff370SJamie Gritton * than duplicate it under a different name. 1031b38ff370SJamie Gritton */ 1032b38ff370SJamie Gritton error = vfs_buildopts(optuio, &opts); 1033b38ff370SJamie Gritton if (error) 1034b38ff370SJamie Gritton return (error); 1035b38ff370SJamie Gritton #ifdef INET 1036b38ff370SJamie Gritton ip4 = NULL; 1037b38ff370SJamie Gritton #endif 1038b38ff370SJamie Gritton #ifdef INET6 1039b38ff370SJamie Gritton ip6 = NULL; 1040b38ff370SJamie Gritton #endif 10416dfe0a3dSMartin Matuska g_path = NULL; 1042b38ff370SJamie Gritton 1043b6f47c23SJamie Gritton cuflags = flags & (JAIL_CREATE | JAIL_UPDATE); 1044b6f47c23SJamie Gritton if (!cuflags) { 1045b6f47c23SJamie Gritton error = EINVAL; 1046b6f47c23SJamie Gritton vfs_opterror(opts, "no valid operation (create or update)"); 1047b6f47c23SJamie Gritton goto done_errmsg; 1048b6f47c23SJamie Gritton } 1049b6f47c23SJamie Gritton 1050b38ff370SJamie Gritton error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 1051b38ff370SJamie Gritton if (error == ENOENT) 1052b38ff370SJamie Gritton jid = 0; 1053b38ff370SJamie Gritton else if (error != 0) 1054b38ff370SJamie Gritton goto done_free; 1055b38ff370SJamie Gritton 1056b38ff370SJamie Gritton error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel)); 1057b38ff370SJamie Gritton if (error == ENOENT) 1058b38ff370SJamie Gritton gotslevel = 0; 1059b38ff370SJamie Gritton else if (error != 0) 1060b38ff370SJamie Gritton goto done_free; 1061b38ff370SJamie Gritton else 1062b38ff370SJamie Gritton gotslevel = 1; 1063b38ff370SJamie Gritton 1064b97457e2SJamie Gritton error = 1065b97457e2SJamie Gritton vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax)); 1066b97457e2SJamie Gritton if (error == ENOENT) 1067b97457e2SJamie Gritton gotchildmax = 0; 1068b97457e2SJamie Gritton else if (error != 0) 1069b97457e2SJamie Gritton goto done_free; 1070b97457e2SJamie Gritton else 1071b97457e2SJamie Gritton gotchildmax = 1; 1072b97457e2SJamie Gritton 10730304c731SJamie Gritton error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce)); 1074f337198dSJamie Gritton if (error == ENOENT) 1075f337198dSJamie Gritton gotenforce = 0; 1076f337198dSJamie Gritton else if (error != 0) 10770304c731SJamie Gritton goto done_free; 1078f337198dSJamie Gritton else if (enforce < 0 || enforce > 2) { 1079f337198dSJamie Gritton error = EINVAL; 1080f337198dSJamie Gritton goto done_free; 1081f337198dSJamie Gritton } else 1082f337198dSJamie Gritton gotenforce = 1; 10830304c731SJamie Gritton 10840cc207a6SMartin Matuska error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum)); 10850cc207a6SMartin Matuska if (error == ENOENT) 10860cc207a6SMartin Matuska gotrsnum = 0; 10870cc207a6SMartin Matuska else if (error != 0) 10880cc207a6SMartin Matuska goto done_free; 10890cc207a6SMartin Matuska else 10900cc207a6SMartin Matuska gotrsnum = 1; 10910cc207a6SMartin Matuska 1092b38ff370SJamie Gritton pr_flags = ch_flags = 0; 1093672756aaSJamie Gritton for (bf = pr_flag_bool; 1094672756aaSJamie Gritton bf < pr_flag_bool + nitems(pr_flag_bool); 1095672756aaSJamie Gritton bf++) { 1096672756aaSJamie Gritton vfs_flagopt(opts, bf->name, &pr_flags, bf->flag); 1097672756aaSJamie Gritton vfs_flagopt(opts, bf->noname, &ch_flags, bf->flag); 10980304c731SJamie Gritton } 1099b38ff370SJamie Gritton ch_flags |= pr_flags; 1100672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 1101672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 1102672756aaSJamie Gritton jsf++) { 1103672756aaSJamie Gritton error = vfs_copyopt(opts, jsf->name, &jsys, sizeof(jsys)); 11047cbf7213SJamie Gritton if (error == ENOENT) 11057cbf7213SJamie Gritton continue; 11067cbf7213SJamie Gritton if (error != 0) 11077cbf7213SJamie Gritton goto done_free; 11087cbf7213SJamie Gritton switch (jsys) { 11097cbf7213SJamie Gritton case JAIL_SYS_DISABLE: 1110672756aaSJamie Gritton if (!jsf->disable) { 11117cbf7213SJamie Gritton error = EINVAL; 11127cbf7213SJamie Gritton goto done_free; 11137cbf7213SJamie Gritton } 1114672756aaSJamie Gritton pr_flags |= jsf->disable; 11157cbf7213SJamie Gritton break; 11167cbf7213SJamie Gritton case JAIL_SYS_NEW: 1117672756aaSJamie Gritton pr_flags |= jsf->new; 11187cbf7213SJamie Gritton break; 11197cbf7213SJamie Gritton case JAIL_SYS_INHERIT: 11207cbf7213SJamie Gritton break; 11217cbf7213SJamie Gritton default: 11227cbf7213SJamie Gritton error = EINVAL; 11237cbf7213SJamie Gritton goto done_free; 11247cbf7213SJamie Gritton } 1125672756aaSJamie Gritton ch_flags |= jsf->new | jsf->disable; 11267cbf7213SJamie Gritton } 11271158508aSJamie Gritton if ((flags & (JAIL_CREATE | JAIL_ATTACH)) == JAIL_CREATE 11284affa14cSJamie Gritton && !(pr_flags & PR_PERSIST)) { 11294affa14cSJamie Gritton error = EINVAL; 11304affa14cSJamie Gritton vfs_opterror(opts, "new jail must persist or attach"); 11314affa14cSJamie Gritton goto done_errmsg; 11324affa14cSJamie Gritton } 1133679e1390SJamie Gritton #ifdef VIMAGE 1134679e1390SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) { 1135679e1390SJamie Gritton error = EINVAL; 1136679e1390SJamie Gritton vfs_opterror(opts, "vnet cannot be changed after creation"); 1137679e1390SJamie Gritton goto done_errmsg; 1138679e1390SJamie Gritton } 1139679e1390SJamie Gritton #endif 11402b0d6f81SJamie Gritton #ifdef INET 11412b0d6f81SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) { 11422b0d6f81SJamie Gritton error = EINVAL; 11432b0d6f81SJamie Gritton vfs_opterror(opts, "ip4 cannot be changed after creation"); 11442b0d6f81SJamie Gritton goto done_errmsg; 11452b0d6f81SJamie Gritton } 11462b0d6f81SJamie Gritton #endif 11472b0d6f81SJamie Gritton #ifdef INET6 11482b0d6f81SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) { 11492b0d6f81SJamie Gritton error = EINVAL; 11502b0d6f81SJamie Gritton vfs_opterror(opts, "ip6 cannot be changed after creation"); 11512b0d6f81SJamie Gritton goto done_errmsg; 11522b0d6f81SJamie Gritton } 11532b0d6f81SJamie Gritton #endif 1154b38ff370SJamie Gritton 11550304c731SJamie Gritton pr_allow = ch_allow = 0; 1156672756aaSJamie Gritton for (bf = pr_flag_allow; 11575d58f959SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 11585d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 1159672756aaSJamie Gritton bf++) { 1160672756aaSJamie Gritton vfs_flagopt(opts, bf->name, &pr_allow, bf->flag); 1161672756aaSJamie Gritton vfs_flagopt(opts, bf->noname, &ch_allow, bf->flag); 11620304c731SJamie Gritton } 11630304c731SJamie Gritton ch_allow |= pr_allow; 11640304c731SJamie Gritton 1165b38ff370SJamie Gritton error = vfs_getopt(opts, "name", (void **)&name, &len); 1166b38ff370SJamie Gritton if (error == ENOENT) 1167b38ff370SJamie Gritton name = NULL; 1168b38ff370SJamie Gritton else if (error != 0) 1169b38ff370SJamie Gritton goto done_free; 1170b38ff370SJamie Gritton else { 1171b38ff370SJamie Gritton if (len == 0 || name[len - 1] != '\0') { 1172b38ff370SJamie Gritton error = EINVAL; 1173b38ff370SJamie Gritton goto done_free; 1174b38ff370SJamie Gritton } 1175b38ff370SJamie Gritton if (len > MAXHOSTNAMELEN) { 1176b38ff370SJamie Gritton error = ENAMETOOLONG; 1177b38ff370SJamie Gritton goto done_free; 1178b38ff370SJamie Gritton } 1179b38ff370SJamie Gritton } 1180b38ff370SJamie Gritton 1181b38ff370SJamie Gritton error = vfs_getopt(opts, "host.hostname", (void **)&host, &len); 1182b38ff370SJamie Gritton if (error == ENOENT) 1183b38ff370SJamie Gritton host = NULL; 1184b38ff370SJamie Gritton else if (error != 0) 1185b38ff370SJamie Gritton goto done_free; 1186b38ff370SJamie Gritton else { 118776ca6f88SJamie Gritton ch_flags |= PR_HOST; 118876ca6f88SJamie Gritton pr_flags |= PR_HOST; 1189b38ff370SJamie Gritton if (len == 0 || host[len - 1] != '\0') { 1190b38ff370SJamie Gritton error = EINVAL; 1191b38ff370SJamie Gritton goto done_free; 1192b38ff370SJamie Gritton } 1193b38ff370SJamie Gritton if (len > MAXHOSTNAMELEN) { 1194b38ff370SJamie Gritton error = ENAMETOOLONG; 1195b38ff370SJamie Gritton goto done_free; 1196b38ff370SJamie Gritton } 1197b38ff370SJamie Gritton } 1198b38ff370SJamie Gritton 119976ca6f88SJamie Gritton error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len); 120076ca6f88SJamie Gritton if (error == ENOENT) 120176ca6f88SJamie Gritton domain = NULL; 120276ca6f88SJamie Gritton else if (error != 0) 120376ca6f88SJamie Gritton goto done_free; 120476ca6f88SJamie Gritton else { 120576ca6f88SJamie Gritton ch_flags |= PR_HOST; 120676ca6f88SJamie Gritton pr_flags |= PR_HOST; 120776ca6f88SJamie Gritton if (len == 0 || domain[len - 1] != '\0') { 120876ca6f88SJamie Gritton error = EINVAL; 120976ca6f88SJamie Gritton goto done_free; 121076ca6f88SJamie Gritton } 121176ca6f88SJamie Gritton if (len > MAXHOSTNAMELEN) { 121276ca6f88SJamie Gritton error = ENAMETOOLONG; 121376ca6f88SJamie Gritton goto done_free; 121476ca6f88SJamie Gritton } 121576ca6f88SJamie Gritton } 121676ca6f88SJamie Gritton 121776ca6f88SJamie Gritton error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len); 121876ca6f88SJamie Gritton if (error == ENOENT) 121976ca6f88SJamie Gritton uuid = NULL; 122076ca6f88SJamie Gritton else if (error != 0) 122176ca6f88SJamie Gritton goto done_free; 122276ca6f88SJamie Gritton else { 122376ca6f88SJamie Gritton ch_flags |= PR_HOST; 122476ca6f88SJamie Gritton pr_flags |= PR_HOST; 122576ca6f88SJamie Gritton if (len == 0 || uuid[len - 1] != '\0') { 122676ca6f88SJamie Gritton error = EINVAL; 122776ca6f88SJamie Gritton goto done_free; 122876ca6f88SJamie Gritton } 122976ca6f88SJamie Gritton if (len > HOSTUUIDLEN) { 123076ca6f88SJamie Gritton error = ENAMETOOLONG; 123176ca6f88SJamie Gritton goto done_free; 123276ca6f88SJamie Gritton } 123376ca6f88SJamie Gritton } 123476ca6f88SJamie Gritton 1235841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32 1236a5c1afadSDmitry Chagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 123776ca6f88SJamie Gritton uint32_t hid32; 123876ca6f88SJamie Gritton 123976ca6f88SJamie Gritton error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32)); 124076ca6f88SJamie Gritton hid = hid32; 124176ca6f88SJamie Gritton } else 124276ca6f88SJamie Gritton #endif 124376ca6f88SJamie Gritton error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid)); 124476ca6f88SJamie Gritton if (error == ENOENT) 124576ca6f88SJamie Gritton gothid = 0; 124676ca6f88SJamie Gritton else if (error != 0) 124776ca6f88SJamie Gritton goto done_free; 124876ca6f88SJamie Gritton else { 124976ca6f88SJamie Gritton gothid = 1; 125076ca6f88SJamie Gritton ch_flags |= PR_HOST; 125176ca6f88SJamie Gritton pr_flags |= PR_HOST; 125276ca6f88SJamie Gritton } 125376ca6f88SJamie Gritton 1254b38ff370SJamie Gritton #ifdef INET 1255b38ff370SJamie Gritton error = vfs_getopt(opts, "ip4.addr", &op, &ip4s); 1256b38ff370SJamie Gritton if (error == ENOENT) 12570e5e396eSJamie Gritton ip4s = 0; 1258b38ff370SJamie Gritton else if (error != 0) 1259b38ff370SJamie Gritton goto done_free; 1260eb8dcdeaSGleb Smirnoff else if (ip4s & (sizeof(struct in_addr) - 1)) { 1261b38ff370SJamie Gritton error = EINVAL; 1262b38ff370SJamie Gritton goto done_free; 12630304c731SJamie Gritton } else { 12646a3f2779SJamie Gritton ch_flags |= PR_IP4_USER; 12656a3f2779SJamie Gritton pr_flags |= PR_IP4_USER; 12666a3f2779SJamie Gritton if (ip4s > 0) { 1267eb8dcdeaSGleb Smirnoff ip4s /= sizeof(struct in_addr); 1268b38ff370SJamie Gritton if (ip4s > jail_max_af_ips) { 1269b38ff370SJamie Gritton error = EINVAL; 1270b38ff370SJamie Gritton vfs_opterror(opts, "too many IPv4 addresses"); 1271b38ff370SJamie Gritton goto done_errmsg; 1272b38ff370SJamie Gritton } 1273eb8dcdeaSGleb Smirnoff ip4 = prison_ip_copyin(PR_INET, op, ip4s); 1274eb8dcdeaSGleb Smirnoff if (ip4 == NULL) { 1275b38ff370SJamie Gritton error = EINVAL; 1276b38ff370SJamie Gritton goto done_free; 1277b38ff370SJamie Gritton } 1278b38ff370SJamie Gritton } 12790304c731SJamie Gritton } 1280b38ff370SJamie Gritton #endif 1281b38ff370SJamie Gritton 1282b38ff370SJamie Gritton #ifdef INET6 1283b38ff370SJamie Gritton error = vfs_getopt(opts, "ip6.addr", &op, &ip6s); 1284b38ff370SJamie Gritton if (error == ENOENT) 12850e5e396eSJamie Gritton ip6s = 0; 1286b38ff370SJamie Gritton else if (error != 0) 1287b38ff370SJamie Gritton goto done_free; 1288eb8dcdeaSGleb Smirnoff else if (ip6s & (sizeof(struct in6_addr) - 1)) { 1289b38ff370SJamie Gritton error = EINVAL; 1290b38ff370SJamie Gritton goto done_free; 12910304c731SJamie Gritton } else { 12926a3f2779SJamie Gritton ch_flags |= PR_IP6_USER; 12936a3f2779SJamie Gritton pr_flags |= PR_IP6_USER; 12946a3f2779SJamie Gritton if (ip6s > 0) { 1295eb8dcdeaSGleb Smirnoff ip6s /= sizeof(struct in6_addr); 1296b38ff370SJamie Gritton if (ip6s > jail_max_af_ips) { 1297b38ff370SJamie Gritton error = EINVAL; 1298b38ff370SJamie Gritton vfs_opterror(opts, "too many IPv6 addresses"); 1299b38ff370SJamie Gritton goto done_errmsg; 1300b38ff370SJamie Gritton } 1301eb8dcdeaSGleb Smirnoff ip6 = prison_ip_copyin(PR_INET6, op, ip6s); 1302eb8dcdeaSGleb Smirnoff if (ip6 == NULL) { 1303b38ff370SJamie Gritton error = EINVAL; 1304b38ff370SJamie Gritton goto done_free; 1305b38ff370SJamie Gritton } 1306b38ff370SJamie Gritton } 13070304c731SJamie Gritton } 1308b38ff370SJamie Gritton #endif 1309b38ff370SJamie Gritton 1310bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6)) 1311bdfc8cc4SJamie Gritton if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 1312bdfc8cc4SJamie Gritton error = EINVAL; 1313bdfc8cc4SJamie Gritton vfs_opterror(opts, 1314bdfc8cc4SJamie Gritton "vnet jails cannot have IP address restrictions"); 1315bdfc8cc4SJamie Gritton goto done_errmsg; 1316bdfc8cc4SJamie Gritton } 1317bdfc8cc4SJamie Gritton #endif 1318bdfc8cc4SJamie Gritton 1319cf0313c6SJamie Gritton error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len); 1320cf0313c6SJamie Gritton if (error == ENOENT) 1321cf0313c6SJamie Gritton osrelstr = NULL; 1322cf0313c6SJamie Gritton else if (error != 0) 1323cf0313c6SJamie Gritton goto done_free; 1324cf0313c6SJamie Gritton else { 1325cf0313c6SJamie Gritton if (flags & JAIL_UPDATE) { 1326cf0313c6SJamie Gritton error = EINVAL; 1327cf0313c6SJamie Gritton vfs_opterror(opts, 1328cf0313c6SJamie Gritton "osrelease cannot be changed after creation"); 1329cf0313c6SJamie Gritton goto done_errmsg; 1330cf0313c6SJamie Gritton } 13311b786d01SBjoern A. Zeeb if (len == 0 || osrelstr[len - 1] != '\0') { 1332cf0313c6SJamie Gritton error = EINVAL; 13331b786d01SBjoern A. Zeeb goto done_free; 13341b786d01SBjoern A. Zeeb } 13351b786d01SBjoern A. Zeeb if (len >= OSRELEASELEN) { 13361b786d01SBjoern A. Zeeb error = ENAMETOOLONG; 1337cf0313c6SJamie Gritton vfs_opterror(opts, 1338cf0313c6SJamie Gritton "osrelease string must be 1-%d bytes long", 1339cf0313c6SJamie Gritton OSRELEASELEN - 1); 1340cf0313c6SJamie Gritton goto done_errmsg; 1341cf0313c6SJamie Gritton } 1342cf0313c6SJamie Gritton } 1343cf0313c6SJamie Gritton 1344cf0313c6SJamie Gritton error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt)); 1345cf0313c6SJamie Gritton if (error == ENOENT) 1346cf0313c6SJamie Gritton osreldt = 0; 1347cf0313c6SJamie Gritton else if (error != 0) 1348cf0313c6SJamie Gritton goto done_free; 1349cf0313c6SJamie Gritton else { 1350cf0313c6SJamie Gritton if (flags & JAIL_UPDATE) { 1351cf0313c6SJamie Gritton error = EINVAL; 1352cf0313c6SJamie Gritton vfs_opterror(opts, 1353cf0313c6SJamie Gritton "osreldate cannot be changed after creation"); 1354cf0313c6SJamie Gritton goto done_errmsg; 1355cf0313c6SJamie Gritton } 1356cf0313c6SJamie Gritton if (osreldt == 0) { 1357cf0313c6SJamie Gritton error = EINVAL; 1358cf0313c6SJamie Gritton vfs_opterror(opts, "osreldate cannot be 0"); 1359cf0313c6SJamie Gritton goto done_errmsg; 1360cf0313c6SJamie Gritton } 1361cf0313c6SJamie Gritton } 1362cf0313c6SJamie Gritton 1363b38ff370SJamie Gritton root = NULL; 1364b38ff370SJamie Gritton error = vfs_getopt(opts, "path", (void **)&path, &len); 1365b38ff370SJamie Gritton if (error == ENOENT) 1366b38ff370SJamie Gritton path = NULL; 1367b38ff370SJamie Gritton else if (error != 0) 1368b38ff370SJamie Gritton goto done_free; 1369b38ff370SJamie Gritton else { 1370b38ff370SJamie Gritton if (flags & JAIL_UPDATE) { 1371b38ff370SJamie Gritton error = EINVAL; 1372b38ff370SJamie Gritton vfs_opterror(opts, 1373b38ff370SJamie Gritton "path cannot be changed after creation"); 1374b38ff370SJamie Gritton goto done_errmsg; 1375b38ff370SJamie Gritton } 1376b38ff370SJamie Gritton if (len == 0 || path[len - 1] != '\0') { 1377b38ff370SJamie Gritton error = EINVAL; 1378b38ff370SJamie Gritton goto done_free; 1379b38ff370SJamie Gritton } 13807e1d3eefSMateusz Guzik NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path); 1381b38ff370SJamie Gritton error = namei(&nd); 1382b38ff370SJamie Gritton if (error) 1383b38ff370SJamie Gritton goto done_free; 1384b38ff370SJamie Gritton root = nd.ni_vp; 1385bb92cd7bSMateusz Guzik NDFREE_PNBUF(&nd); 13866dfe0a3dSMartin Matuska g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 13876dfe0a3dSMartin Matuska strlcpy(g_path, path, MAXPATHLEN); 13886dfe0a3dSMartin Matuska error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN); 13893eb6b656SMateusz Guzik if (error == 0) { 13906dfe0a3dSMartin Matuska path = g_path; 13916dfe0a3dSMartin Matuska } else { 1392f6e633a9SMartin Matuska /* exit on other errors */ 1393b38ff370SJamie Gritton goto done_free; 1394b38ff370SJamie Gritton } 1395f6e633a9SMartin Matuska if (root->v_type != VDIR) { 1396f6e633a9SMartin Matuska error = ENOTDIR; 1397f6e633a9SMartin Matuska vput(root); 1398f6e633a9SMartin Matuska goto done_free; 1399f6e633a9SMartin Matuska } 1400b249ce48SMateusz Guzik VOP_UNLOCK(root); 1401b38ff370SJamie Gritton } 1402b38ff370SJamie Gritton 1403b38ff370SJamie Gritton /* 1404b6f47c23SJamie Gritton * Find the specified jail, or at least its parent. 1405b38ff370SJamie Gritton * This abuses the file error codes ENOENT and EEXIST. 1406b38ff370SJamie Gritton */ 1407b38ff370SJamie Gritton pr = NULL; 14087de883c8SJamie Gritton inspr = NULL; 1409*ed31b3f4SJamie Gritton deadpr = NULL; 1410babbbb9cSJamie Gritton if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) { 1411babbbb9cSJamie Gritton namelc = strrchr(name, '.'); 1412babbbb9cSJamie Gritton jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10); 1413babbbb9cSJamie Gritton if (*p != '\0') 1414babbbb9cSJamie Gritton jid = 0; 1415babbbb9cSJamie Gritton } 1416b6f47c23SJamie Gritton sx_xlock(&allprison_lock); 1417108a9384SJamie Gritton drflags = PD_LIST_XLOCKED; 14180a2a96f3SJamie Gritton ppr = mypr; 14190a2a96f3SJamie Gritton if (!prison_isalive(ppr)) { 14200a2a96f3SJamie Gritton /* This jail is dying. This process will surely follow. */ 14210a2a96f3SJamie Gritton error = EAGAIN; 14220a2a96f3SJamie Gritton goto done_deref; 14230a2a96f3SJamie Gritton } 1424b38ff370SJamie Gritton if (jid != 0) { 1425b38ff370SJamie Gritton if (jid < 0) { 1426b38ff370SJamie Gritton error = EINVAL; 1427b38ff370SJamie Gritton vfs_opterror(opts, "negative jid"); 14282a4b2251SJamie Gritton goto done_deref; 1429b38ff370SJamie Gritton } 14307de883c8SJamie Gritton /* 14317de883c8SJamie Gritton * See if a requested jid already exists. Keep track of 14327de883c8SJamie Gritton * where it can be inserted later. 14337de883c8SJamie Gritton */ 14347de883c8SJamie Gritton TAILQ_FOREACH(inspr, &allprison, pr_list) { 1435f7496dcaSJamie Gritton if (inspr->pr_id < jid) 1436f7496dcaSJamie Gritton continue; 1437f7496dcaSJamie Gritton if (inspr->pr_id > jid) 1438f7496dcaSJamie Gritton break; 1439*ed31b3f4SJamie Gritton if (prison_isalive(inspr)) { 14407de883c8SJamie Gritton pr = inspr; 1441f7496dcaSJamie Gritton mtx_lock(&pr->pr_mtx); 14422a4b2251SJamie Gritton drflags |= PD_LOCKED; 1443*ed31b3f4SJamie Gritton } else { 1444*ed31b3f4SJamie Gritton /* Note a dying jail to handle later. */ 1445*ed31b3f4SJamie Gritton deadpr = inspr; 1446*ed31b3f4SJamie Gritton } 14477de883c8SJamie Gritton inspr = NULL; 14487de883c8SJamie Gritton break; 14497de883c8SJamie Gritton } 1450*ed31b3f4SJamie Gritton if (cuflags == JAIL_CREATE && pr != NULL) { 14517de883c8SJamie Gritton /* 14527de883c8SJamie Gritton * Even creators that cannot see the jail will 14537de883c8SJamie Gritton * get EEXIST. 14547de883c8SJamie Gritton */ 1455b38ff370SJamie Gritton error = EEXIST; 1456*ed31b3f4SJamie Gritton vfs_opterror(opts, "jail %d already exists", jid); 14572a4b2251SJamie Gritton goto done_deref; 1458b38ff370SJamie Gritton } 1459*ed31b3f4SJamie Gritton if ((pr == NULL) 1460*ed31b3f4SJamie Gritton ? cuflags == JAIL_UPDATE 1461*ed31b3f4SJamie Gritton : !prison_ischild(mypr, pr)) { 14627de883c8SJamie Gritton /* 1463*ed31b3f4SJamie Gritton * Updaters get ENOENT for nonexistent jails, 1464*ed31b3f4SJamie Gritton * or for jails they cannot see. The latter 1465*ed31b3f4SJamie Gritton * case is true even for CREATE | UPDATE, 14667de883c8SJamie Gritton * which normally cannot give this error. 14677de883c8SJamie Gritton */ 14682a4b2251SJamie Gritton error = ENOENT; 14692a4b2251SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 14702a4b2251SJamie Gritton goto done_deref; 1471f7496dcaSJamie Gritton } 1472b38ff370SJamie Gritton } 1473b38ff370SJamie Gritton /* 1474b38ff370SJamie Gritton * If the caller provided a name, look for a jail by that name. 1475b38ff370SJamie Gritton * This has different semantics for creates and updates keyed by jid 1476b38ff370SJamie Gritton * (where the name must not already exist in a different jail), 1477b38ff370SJamie Gritton * and updates keyed by the name itself (where the name must exist 1478b38ff370SJamie Gritton * because that is the jail being updated). 1479b38ff370SJamie Gritton */ 1480b6f47c23SJamie Gritton namelc = NULL; 1481b38ff370SJamie Gritton if (name != NULL) { 1482babbbb9cSJamie Gritton namelc = strrchr(name, '.'); 1483babbbb9cSJamie Gritton if (namelc == NULL) 1484babbbb9cSJamie Gritton namelc = name; 1485babbbb9cSJamie Gritton else { 14860304c731SJamie Gritton /* 14870304c731SJamie Gritton * This is a hierarchical name. Split it into the 14880304c731SJamie Gritton * parent and child names, and make sure the parent 14890304c731SJamie Gritton * exists or matches an already found jail. 14900304c731SJamie Gritton */ 14910304c731SJamie Gritton if (pr != NULL) { 1492babbbb9cSJamie Gritton if (strncmp(name, ppr->pr_name, namelc - name) 1493babbbb9cSJamie Gritton || ppr->pr_name[namelc - name] != '\0') { 14940304c731SJamie Gritton error = EINVAL; 14950304c731SJamie Gritton vfs_opterror(opts, 14960304c731SJamie Gritton "cannot change jail's parent"); 14972a4b2251SJamie Gritton goto done_deref; 14980304c731SJamie Gritton } 14990304c731SJamie Gritton } else { 1500b6f47c23SJamie Gritton *namelc = '\0'; 15010304c731SJamie Gritton ppr = prison_find_name(mypr, name); 15020304c731SJamie Gritton if (ppr == NULL) { 15030304c731SJamie Gritton error = ENOENT; 15040304c731SJamie Gritton vfs_opterror(opts, 15050304c731SJamie Gritton "jail \"%s\" not found", name); 15062a4b2251SJamie Gritton goto done_deref; 15070304c731SJamie Gritton } 1508c050ea80SJamie Gritton mtx_unlock(&ppr->pr_mtx); 15090a2a96f3SJamie Gritton if (!prison_isalive(ppr)) { 1510c050ea80SJamie Gritton error = ENOENT; 1511c050ea80SJamie Gritton vfs_opterror(opts, 1512c050ea80SJamie Gritton "jail \"%s\" is dying", name); 1513c050ea80SJamie Gritton goto done_deref; 1514c050ea80SJamie Gritton } 1515b6f47c23SJamie Gritton *namelc = '.'; 15160304c731SJamie Gritton } 1517b6f47c23SJamie Gritton namelc++; 15180304c731SJamie Gritton } 1519b6f47c23SJamie Gritton if (namelc[0] != '\0') { 1520b6f47c23SJamie Gritton pnamelen = 15210304c731SJamie Gritton (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 15220304c731SJamie Gritton FOREACH_PRISON_CHILD(ppr, tpr) { 1523*ed31b3f4SJamie Gritton if (tpr == pr || !prison_isalive(tpr) || 1524*ed31b3f4SJamie Gritton strcmp(tpr->pr_name + pnamelen, namelc)) 1525*ed31b3f4SJamie Gritton continue; 1526*ed31b3f4SJamie Gritton if (cuflags == JAIL_CREATE || pr != NULL) { 1527b38ff370SJamie Gritton /* 1528*ed31b3f4SJamie Gritton * Create, or update(jid): name must 1529*ed31b3f4SJamie Gritton * not exist in an active sibling jail. 1530b38ff370SJamie Gritton */ 1531*ed31b3f4SJamie Gritton error = EEXIST; 1532*ed31b3f4SJamie Gritton vfs_opterror(opts, 1533*ed31b3f4SJamie Gritton "jail \"%s\" already exists", name); 1534*ed31b3f4SJamie Gritton goto done_deref; 1535*ed31b3f4SJamie Gritton } 1536*ed31b3f4SJamie Gritton /* Use this jail for updates. */ 1537b38ff370SJamie Gritton pr = tpr; 1538f7496dcaSJamie Gritton mtx_lock(&pr->pr_mtx); 153983bc72a0SJamie Gritton drflags |= PD_LOCKED; 1540b38ff370SJamie Gritton break; 1541b38ff370SJamie Gritton } 1542b38ff370SJamie Gritton /* 1543*ed31b3f4SJamie Gritton * Update: name must exist if no jid is specified. 1544*ed31b3f4SJamie Gritton * As with the jid case, the jail must be currently 1545*ed31b3f4SJamie Gritton * visible, or else even CREATE | UPDATE will get 1546*ed31b3f4SJamie Gritton * an error. 1547b38ff370SJamie Gritton */ 1548*ed31b3f4SJamie Gritton if ((pr == NULL) 1549*ed31b3f4SJamie Gritton ? cuflags == JAIL_UPDATE 1550*ed31b3f4SJamie Gritton : !prison_isalive(pr)) { 1551b38ff370SJamie Gritton error = ENOENT; 1552b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", 1553b38ff370SJamie Gritton name); 15542a4b2251SJamie Gritton goto done_deref; 1555b38ff370SJamie Gritton } 1556b38ff370SJamie Gritton } 1557b38ff370SJamie Gritton } 1558b38ff370SJamie Gritton /* Update: must provide a jid or name. */ 1559b38ff370SJamie Gritton else if (cuflags == JAIL_UPDATE && pr == NULL) { 1560b38ff370SJamie Gritton error = ENOENT; 1561b38ff370SJamie Gritton vfs_opterror(opts, "update specified no jail"); 15622a4b2251SJamie Gritton goto done_deref; 1563b38ff370SJamie Gritton } 1564b38ff370SJamie Gritton 1565b38ff370SJamie Gritton /* If there's no prison to update, create a new one and link it in. */ 15662a4b2251SJamie Gritton created = pr == NULL; 15672a4b2251SJamie Gritton if (created) { 1568b97457e2SJamie Gritton for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent) 1569b97457e2SJamie Gritton if (tpr->pr_childcount >= tpr->pr_childmax) { 1570b97457e2SJamie Gritton error = EPERM; 1571b97457e2SJamie Gritton vfs_opterror(opts, "prison limit exceeded"); 15722a4b2251SJamie Gritton goto done_deref; 1573b97457e2SJamie Gritton } 1574*ed31b3f4SJamie Gritton 1575*ed31b3f4SJamie Gritton if (deadpr != NULL) { 1576*ed31b3f4SJamie Gritton /* 1577*ed31b3f4SJamie Gritton * The prison being created has the same ID as a dying 1578*ed31b3f4SJamie Gritton * one. Handle this by giving the dying jail a new ID. 1579*ed31b3f4SJamie Gritton * This may cause some confusion to user space, but 1580*ed31b3f4SJamie Gritton * only to those listing dying jails. 1581*ed31b3f4SJamie Gritton */ 1582*ed31b3f4SJamie Gritton deadid = get_next_deadid(&dinspr); 1583*ed31b3f4SJamie Gritton if (deadid == 0) { 1584*ed31b3f4SJamie Gritton error = EAGAIN; 1585*ed31b3f4SJamie Gritton vfs_opterror(opts, "no available jail IDs"); 1586*ed31b3f4SJamie Gritton goto done_deref; 1587*ed31b3f4SJamie Gritton } 1588*ed31b3f4SJamie Gritton mtx_lock(&deadpr->pr_mtx); 1589*ed31b3f4SJamie Gritton deadpr->pr_id = deadid; 1590*ed31b3f4SJamie Gritton mtx_unlock(&deadpr->pr_mtx); 1591*ed31b3f4SJamie Gritton if (dinspr == deadpr) 1592*ed31b3f4SJamie Gritton inspr = deadpr; 1593*ed31b3f4SJamie Gritton else { 1594*ed31b3f4SJamie Gritton inspr = TAILQ_NEXT(deadpr, pr_list); 1595*ed31b3f4SJamie Gritton TAILQ_REMOVE(&allprison, deadpr, pr_list); 1596*ed31b3f4SJamie Gritton if (dinspr != NULL) 1597*ed31b3f4SJamie Gritton TAILQ_INSERT_AFTER(&allprison, dinspr, 1598*ed31b3f4SJamie Gritton deadpr, pr_list); 1599*ed31b3f4SJamie Gritton else 1600*ed31b3f4SJamie Gritton TAILQ_INSERT_HEAD(&allprison, deadpr, 1601*ed31b3f4SJamie Gritton pr_list); 1602*ed31b3f4SJamie Gritton } 1603*ed31b3f4SJamie 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 */ 20031158508aSJamie Gritton if (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags)) { 2004b38ff370SJamie Gritton if (pr_flags & PR_PERSIST) { 20056754ae25SJamie Gritton prison_hold(pr); 20061158508aSJamie Gritton /* 2007*ed31b3f4SJamie Gritton * This may be a new prison's first user reference, 2008*ed31b3f4SJamie Gritton * but wait to call it alive until after OSD calls 2009*ed31b3f4SJamie Gritton * have had a chance to run (and perhaps to fail). 20101158508aSJamie Gritton */ 20116754ae25SJamie Gritton refcount_acquire(&pr->pr_uref); 2012b38ff370SJamie Gritton } else { 201339c8ef90SJamie Gritton drflags |= PD_DEUREF; 2014f7496dcaSJamie Gritton prison_free_not_last(pr); 2015b38ff370SJamie Gritton } 2016b38ff370SJamie Gritton } 2017b38ff370SJamie Gritton pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags; 2018b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 20192a4b2251SJamie Gritton drflags &= ~PD_LOCKED; 2020c861373bSJamie Gritton /* 2021c861373bSJamie Gritton * Any errors past this point will need to de-persist newly created 2022c861373bSJamie Gritton * prisons, as well as call remove methods. 2023c861373bSJamie Gritton */ 2024*ed31b3f4SJamie Gritton if (created) 2025c861373bSJamie Gritton drflags |= PD_KILL; 2026b38ff370SJamie Gritton 2027a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 20284b5c9cf6SEdward Tomasz Napierala if (racct_enable && created) 2029a7ad07bfSEdward Tomasz Napierala prison_racct_attach(pr); 2030a7ad07bfSEdward Tomasz Napierala #endif 2031a7ad07bfSEdward Tomasz Napierala 20320304c731SJamie Gritton /* Locks may have prevented a complete restriction of child IP 20330304c731SJamie Gritton * addresses. If so, allocate some more memory and try again. 20340304c731SJamie Gritton */ 20350304c731SJamie Gritton #ifdef INET 20360304c731SJamie Gritton while (redo_ip4) { 2037eb8dcdeaSGleb Smirnoff ip4s = pr->pr_addrs[PR_INET]->ips; 20388bce8d28SZhenlei Huang MPASS(ip4 == NULL); 2039eb8dcdeaSGleb Smirnoff ip4 = prison_ip_alloc(PR_INET, ip4s, M_WAITOK); 20400304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 20418bce8d28SZhenlei Huang redo_ip4 = false; 20420304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 2043bdfc8cc4SJamie Gritton #ifdef VIMAGE 2044bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 2045bdfc8cc4SJamie Gritton descend = 0; 2046bdfc8cc4SJamie Gritton continue; 2047bdfc8cc4SJamie Gritton } 2048bdfc8cc4SJamie Gritton #endif 2049b2d76b52SZhenlei Huang if (!prison_ip_restrict(tpr, PR_INET, &ip4)) 2050b2d76b52SZhenlei Huang redo_ip4 = true; 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 2069b2d76b52SZhenlei Huang if (!prison_ip_restrict(tpr, PR_INET6, &ip6)) 2070b2d76b52SZhenlei Huang redo_ip6 = true; 20710304c731SJamie Gritton } 20720304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 20730304c731SJamie Gritton } 20740304c731SJamie Gritton #endif 20750304c731SJamie Gritton 2076b38ff370SJamie Gritton /* Let the modules do their work. */ 2077*ed31b3f4SJamie Gritton if (created) { 2078b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_CREATE, opts); 2079c861373bSJamie Gritton if (error) 20802a4b2251SJamie Gritton goto done_deref; 2081b38ff370SJamie Gritton } 2082b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_SET, opts); 2083c861373bSJamie Gritton if (error) 20842a4b2251SJamie Gritton goto done_deref; 2085b38ff370SJamie Gritton 20861158508aSJamie Gritton /* 20871158508aSJamie Gritton * A new prison is now ready to be seen; either it has gained a user 20881158508aSJamie Gritton * reference via persistence, or is about to gain one via attachment. 20891158508aSJamie Gritton */ 2090*ed31b3f4SJamie Gritton if (created) { 20911158508aSJamie Gritton drflags = prison_lock_xlock(pr, drflags); 20921158508aSJamie Gritton pr->pr_state = PRISON_STATE_ALIVE; 20931158508aSJamie Gritton } 20941158508aSJamie Gritton 2095b38ff370SJamie Gritton /* Attach this process to the prison if requested. */ 2096b38ff370SJamie Gritton if (flags & JAIL_ATTACH) { 2097c861373bSJamie Gritton error = do_jail_attach(td, pr, 2098589e4c1dSJamie Gritton prison_lock_xlock(pr, drflags & PD_LOCK_FLAGS)); 2099f7496dcaSJamie Gritton drflags &= ~(PD_LOCKED | PD_LIST_XLOCKED); 2100b38ff370SJamie Gritton if (error) { 2101b38ff370SJamie Gritton vfs_opterror(opts, "attach failed"); 21022a4b2251SJamie Gritton goto done_deref; 2103b38ff370SJamie Gritton } 2104b38ff370SJamie Gritton } 2105b38ff370SJamie Gritton 21061fb24974SEdward Tomasz Napierala #ifdef RACCT 21074b5c9cf6SEdward Tomasz Napierala if (racct_enable && !created) { 2108701d6b50SJamie Gritton if (drflags & PD_LOCKED) { 2109701d6b50SJamie Gritton mtx_unlock(&pr->pr_mtx); 2110701d6b50SJamie Gritton drflags &= ~PD_LOCKED; 2111701d6b50SJamie Gritton } 2112f7496dcaSJamie Gritton if (drflags & PD_LIST_XLOCKED) { 2113f7496dcaSJamie Gritton sx_xunlock(&allprison_lock); 2114f7496dcaSJamie Gritton drflags &= ~PD_LIST_XLOCKED; 2115b4e87a63SJamie Gritton } 21161fb24974SEdward Tomasz Napierala prison_racct_modify(pr); 21171fb24974SEdward Tomasz Napierala } 21181fb24974SEdward Tomasz Napierala #endif 21191fb24974SEdward Tomasz Napierala 2120*ed31b3f4SJamie Gritton if (created && 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 2125c861373bSJamie Gritton drflags &= ~PD_KILL; 21261fb24974SEdward Tomasz Napierala td->td_retval[0] = pr->pr_id; 21271fb24974SEdward Tomasz Napierala 21282a4b2251SJamie Gritton done_deref: 21292a4b2251SJamie Gritton /* Release any temporary prison holds and/or locks. */ 21302a4b2251SJamie Gritton if (pr != NULL) 21312a4b2251SJamie Gritton prison_deref(pr, drflags); 21322a4b2251SJamie Gritton else if (drflags & PD_LIST_SLOCKED) 2133b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 21342a4b2251SJamie Gritton else if (drflags & PD_LIST_XLOCKED) 2135b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 21365050aa86SKonstantin Belousov if (root != NULL) 2137b38ff370SJamie Gritton vrele(root); 2138b38ff370SJamie Gritton done_errmsg: 2139b38ff370SJamie Gritton if (error) { 21402a4b2251SJamie Gritton /* Write the error message back to userspace. */ 2141176ff3a0SJamie Gritton if (vfs_getopt(opts, "errmsg", (void **)&errmsg, 2142176ff3a0SJamie Gritton &errmsg_len) == 0 && errmsg_len > 0) { 2143b38ff370SJamie Gritton errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1; 2144b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 2145b38ff370SJamie Gritton bcopy(errmsg, 2146b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2147b38ff370SJamie Gritton errmsg_len); 2148b38ff370SJamie Gritton else 2149b38ff370SJamie Gritton copyout(errmsg, 2150b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2151b38ff370SJamie Gritton errmsg_len); 2152b38ff370SJamie Gritton } 2153b38ff370SJamie Gritton } 2154b38ff370SJamie Gritton done_free: 2155b38ff370SJamie Gritton #ifdef INET 2156eb8dcdeaSGleb Smirnoff prison_ip_free(ip4); 2157b38ff370SJamie Gritton #endif 2158b38ff370SJamie Gritton #ifdef INET6 2159eb8dcdeaSGleb Smirnoff prison_ip_free(ip6); 2160b38ff370SJamie Gritton #endif 21616dfe0a3dSMartin Matuska if (g_path != NULL) 21626dfe0a3dSMartin Matuska free(g_path, M_TEMP); 2163b38ff370SJamie Gritton vfs_freeopts(opts); 2164b38ff370SJamie Gritton return (error); 2165b38ff370SJamie Gritton } 2166b38ff370SJamie Gritton 2167b38ff370SJamie Gritton /* 21687de883c8SJamie Gritton * Find the next available prison ID. Return the ID on success, or zero 21697de883c8SJamie Gritton * on failure. Also set a pointer to the allprison list entry the prison 21707de883c8SJamie Gritton * should be inserted before. 21717de883c8SJamie Gritton */ 21727de883c8SJamie Gritton static int 21737de883c8SJamie Gritton get_next_prid(struct prison **insprp) 21747de883c8SJamie Gritton { 21757de883c8SJamie Gritton struct prison *inspr; 21767de883c8SJamie Gritton int jid, maxid; 21777de883c8SJamie Gritton 21787de883c8SJamie Gritton jid = lastprid % JAIL_MAX + 1; 21797de883c8SJamie Gritton if (TAILQ_EMPTY(&allprison) || 21807de883c8SJamie Gritton TAILQ_LAST(&allprison, prisonlist)->pr_id < jid) { 21817de883c8SJamie Gritton /* 21827de883c8SJamie Gritton * A common case is for all jails to be implicitly numbered, 21837de883c8SJamie Gritton * which means they'll go on the end of the list, at least 21847de883c8SJamie Gritton * for the first JAIL_MAX times. 21857de883c8SJamie Gritton */ 21867de883c8SJamie Gritton inspr = NULL; 21877de883c8SJamie Gritton } else { 21887de883c8SJamie Gritton /* 21897de883c8SJamie Gritton * Take two passes through the allprison list: first starting 21907de883c8SJamie Gritton * with the proposed jid, then ending with it. 21917de883c8SJamie Gritton */ 21927de883c8SJamie Gritton for (maxid = JAIL_MAX; maxid != 0; ) { 21937de883c8SJamie Gritton TAILQ_FOREACH(inspr, &allprison, pr_list) { 21947de883c8SJamie Gritton if (inspr->pr_id < jid) 21957de883c8SJamie Gritton continue; 2196f7496dcaSJamie Gritton if (inspr->pr_id > jid) { 2197f7496dcaSJamie Gritton /* Found an opening. */ 21987de883c8SJamie Gritton maxid = 0; 21997de883c8SJamie Gritton break; 22007de883c8SJamie Gritton } 22017de883c8SJamie Gritton if (++jid > maxid) { 22027de883c8SJamie Gritton if (lastprid == maxid || lastprid == 0) 22037de883c8SJamie Gritton { 22047de883c8SJamie Gritton /* 22057de883c8SJamie Gritton * The entire legal range 22067de883c8SJamie Gritton * has been traversed 22077de883c8SJamie Gritton */ 22087de883c8SJamie Gritton return 0; 22097de883c8SJamie Gritton } 22107de883c8SJamie Gritton /* Try again from the start. */ 22117de883c8SJamie Gritton jid = 1; 22127de883c8SJamie Gritton maxid = lastprid; 22137de883c8SJamie Gritton break; 22147de883c8SJamie Gritton } 22157de883c8SJamie Gritton } 22167de883c8SJamie Gritton if (inspr == NULL) { 22177de883c8SJamie Gritton /* Found room at the end of the list. */ 22187de883c8SJamie Gritton break; 22197de883c8SJamie Gritton } 22207de883c8SJamie Gritton } 22217de883c8SJamie Gritton } 22227de883c8SJamie Gritton *insprp = inspr; 22237de883c8SJamie Gritton lastprid = jid; 22247de883c8SJamie Gritton return (jid); 22257de883c8SJamie Gritton } 22267de883c8SJamie Gritton 22277de883c8SJamie Gritton /* 2228*ed31b3f4SJamie Gritton * Find the next available ID for a renumbered dead prison. This is the same 2229*ed31b3f4SJamie Gritton * as get_next_prid, but counting backward from the end of the range. 2230*ed31b3f4SJamie Gritton */ 2231*ed31b3f4SJamie Gritton static int 2232*ed31b3f4SJamie Gritton get_next_deadid(struct prison **dinsprp) 2233*ed31b3f4SJamie Gritton { 2234*ed31b3f4SJamie Gritton struct prison *dinspr; 2235*ed31b3f4SJamie Gritton int deadid, minid; 2236*ed31b3f4SJamie Gritton 2237*ed31b3f4SJamie Gritton deadid = lastdeadid ? lastdeadid - 1 : JAIL_MAX; 2238*ed31b3f4SJamie Gritton /* 2239*ed31b3f4SJamie Gritton * Take two reverse passes through the allprison list: first 2240*ed31b3f4SJamie Gritton * starting with the proposed deadid, then ending with it. 2241*ed31b3f4SJamie Gritton */ 2242*ed31b3f4SJamie Gritton for (minid = 1; minid != 0; ) { 2243*ed31b3f4SJamie Gritton TAILQ_FOREACH_REVERSE(dinspr, &allprison, prisonlist, pr_list) { 2244*ed31b3f4SJamie Gritton if (dinspr->pr_id > deadid) 2245*ed31b3f4SJamie Gritton continue; 2246*ed31b3f4SJamie Gritton if (dinspr->pr_id < deadid) { 2247*ed31b3f4SJamie Gritton /* Found an opening. */ 2248*ed31b3f4SJamie Gritton minid = 0; 2249*ed31b3f4SJamie Gritton break; 2250*ed31b3f4SJamie Gritton } 2251*ed31b3f4SJamie Gritton if (--deadid < minid) { 2252*ed31b3f4SJamie Gritton if (lastdeadid == minid || lastdeadid == 0) 2253*ed31b3f4SJamie Gritton { 2254*ed31b3f4SJamie Gritton /* 2255*ed31b3f4SJamie Gritton * The entire legal range 2256*ed31b3f4SJamie Gritton * has been traversed 2257*ed31b3f4SJamie Gritton */ 2258*ed31b3f4SJamie Gritton return 0; 2259*ed31b3f4SJamie Gritton } 2260*ed31b3f4SJamie Gritton /* Try again from the end. */ 2261*ed31b3f4SJamie Gritton deadid = JAIL_MAX; 2262*ed31b3f4SJamie Gritton minid = lastdeadid; 2263*ed31b3f4SJamie Gritton break; 2264*ed31b3f4SJamie Gritton } 2265*ed31b3f4SJamie Gritton } 2266*ed31b3f4SJamie Gritton if (dinspr == NULL) { 2267*ed31b3f4SJamie Gritton /* Found room at the beginning of the list. */ 2268*ed31b3f4SJamie Gritton break; 2269*ed31b3f4SJamie Gritton } 2270*ed31b3f4SJamie Gritton } 2271*ed31b3f4SJamie Gritton *dinsprp = dinspr; 2272*ed31b3f4SJamie Gritton lastdeadid = deadid; 2273*ed31b3f4SJamie Gritton return (deadid); 2274*ed31b3f4SJamie Gritton } 2275*ed31b3f4SJamie Gritton 2276*ed31b3f4SJamie Gritton /* 2277b38ff370SJamie Gritton * struct jail_get_args { 2278b38ff370SJamie Gritton * struct iovec *iovp; 2279b38ff370SJamie Gritton * unsigned int iovcnt; 2280b38ff370SJamie Gritton * int flags; 2281b38ff370SJamie Gritton * }; 2282b38ff370SJamie Gritton */ 2283b38ff370SJamie Gritton int 22848451d0ddSKip Macy sys_jail_get(struct thread *td, struct jail_get_args *uap) 2285b38ff370SJamie Gritton { 2286b38ff370SJamie Gritton struct uio *auio; 2287b38ff370SJamie Gritton int error; 2288b38ff370SJamie Gritton 2289b38ff370SJamie Gritton /* Check that we have an even number of iovecs. */ 2290b38ff370SJamie Gritton if (uap->iovcnt & 1) 2291b38ff370SJamie Gritton return (EINVAL); 2292b38ff370SJamie Gritton 2293b38ff370SJamie Gritton error = copyinuio(uap->iovp, uap->iovcnt, &auio); 2294b38ff370SJamie Gritton if (error) 2295b38ff370SJamie Gritton return (error); 2296b38ff370SJamie Gritton error = kern_jail_get(td, auio, uap->flags); 2297b38ff370SJamie Gritton if (error == 0) 2298b38ff370SJamie Gritton error = copyout(auio->uio_iov, uap->iovp, 2299b38ff370SJamie Gritton uap->iovcnt * sizeof (struct iovec)); 2300b38ff370SJamie Gritton free(auio, M_IOV); 2301b38ff370SJamie Gritton return (error); 2302b38ff370SJamie Gritton } 2303b38ff370SJamie Gritton 2304b38ff370SJamie Gritton int 2305b38ff370SJamie Gritton kern_jail_get(struct thread *td, struct uio *optuio, int flags) 2306b38ff370SJamie Gritton { 2307672756aaSJamie Gritton struct bool_flags *bf; 2308672756aaSJamie Gritton struct jailsys_flags *jsf; 23090304c731SJamie Gritton struct prison *pr, *mypr; 2310b38ff370SJamie Gritton struct vfsopt *opt; 2311b38ff370SJamie Gritton struct vfsoptlist *opts; 2312b38ff370SJamie Gritton char *errmsg, *name; 23132a4b2251SJamie Gritton int drflags, error, errmsg_len, errmsg_pos, i, jid, len, pos; 2314672756aaSJamie Gritton unsigned f; 2315b38ff370SJamie Gritton 2316b38ff370SJamie Gritton if (flags & ~JAIL_GET_MASK) 2317b38ff370SJamie Gritton return (EINVAL); 2318b38ff370SJamie Gritton 2319b38ff370SJamie Gritton /* Get the parameter list. */ 2320b38ff370SJamie Gritton error = vfs_buildopts(optuio, &opts); 2321b38ff370SJamie Gritton if (error) 2322b38ff370SJamie Gritton return (error); 2323b38ff370SJamie Gritton errmsg_pos = vfs_getopt_pos(opts, "errmsg"); 23240304c731SJamie Gritton mypr = td->td_ucred->cr_prison; 23252a4b2251SJamie Gritton pr = NULL; 23261e2a13e6SJamie Gritton 2327b38ff370SJamie Gritton /* 2328b38ff370SJamie Gritton * Find the prison specified by one of: lastjid, jid, name. 2329b38ff370SJamie Gritton */ 2330b38ff370SJamie Gritton sx_slock(&allprison_lock); 23312a4b2251SJamie Gritton drflags = PD_LIST_SLOCKED; 2332b38ff370SJamie Gritton error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid)); 2333b38ff370SJamie Gritton if (error == 0) { 2334b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 2335f7496dcaSJamie Gritton if (pr->pr_id > jid && 2336f7496dcaSJamie Gritton ((flags & JAIL_DYING) || prison_isalive(pr)) && 2337f7496dcaSJamie Gritton prison_ischild(mypr, pr)) { 2338b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 23392a4b2251SJamie Gritton drflags |= PD_LOCKED; 2340b38ff370SJamie Gritton goto found_prison; 23412a4b2251SJamie Gritton } 2342f7496dcaSJamie Gritton } 2343b38ff370SJamie Gritton error = ENOENT; 2344b38ff370SJamie Gritton vfs_opterror(opts, "no jail after %d", jid); 23452a4b2251SJamie Gritton goto done; 2346b38ff370SJamie Gritton } else if (error != ENOENT) 23472a4b2251SJamie Gritton goto done; 2348b38ff370SJamie Gritton 2349b38ff370SJamie Gritton error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 2350b38ff370SJamie Gritton if (error == 0) { 2351b38ff370SJamie Gritton if (jid != 0) { 23520304c731SJamie Gritton pr = prison_find_child(mypr, jid); 2353b38ff370SJamie Gritton if (pr != NULL) { 23542a4b2251SJamie Gritton drflags |= PD_LOCKED; 235576ad42abSJamie Gritton if (!(prison_isalive(pr) || 235676ad42abSJamie Gritton (flags & JAIL_DYING))) { 2357b38ff370SJamie Gritton error = ENOENT; 2358b38ff370SJamie Gritton vfs_opterror(opts, "jail %d is dying", 2359b38ff370SJamie Gritton jid); 23602a4b2251SJamie Gritton goto done; 2361b38ff370SJamie Gritton } 2362b38ff370SJamie Gritton goto found_prison; 2363b38ff370SJamie Gritton } 2364b38ff370SJamie Gritton error = ENOENT; 2365b38ff370SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 23662a4b2251SJamie Gritton goto done; 2367b38ff370SJamie Gritton } 2368b38ff370SJamie Gritton } else if (error != ENOENT) 23692a4b2251SJamie Gritton goto done; 2370b38ff370SJamie Gritton 2371b38ff370SJamie Gritton error = vfs_getopt(opts, "name", (void **)&name, &len); 2372b38ff370SJamie Gritton if (error == 0) { 2373b38ff370SJamie Gritton if (len == 0 || name[len - 1] != '\0') { 2374b38ff370SJamie Gritton error = EINVAL; 23752a4b2251SJamie Gritton goto done; 2376b38ff370SJamie Gritton } 23770304c731SJamie Gritton pr = prison_find_name(mypr, name); 2378b38ff370SJamie Gritton if (pr != NULL) { 23792a4b2251SJamie Gritton drflags |= PD_LOCKED; 238076ad42abSJamie Gritton if (!(prison_isalive(pr) || (flags & JAIL_DYING))) { 2381b38ff370SJamie Gritton error = ENOENT; 2382b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" is dying", 2383b38ff370SJamie Gritton name); 23842a4b2251SJamie Gritton goto done; 2385b38ff370SJamie Gritton } 2386b38ff370SJamie Gritton goto found_prison; 2387b38ff370SJamie Gritton } 2388b38ff370SJamie Gritton error = ENOENT; 2389b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", name); 23902a4b2251SJamie Gritton goto done; 2391b38ff370SJamie Gritton } else if (error != ENOENT) 23922a4b2251SJamie Gritton goto done; 2393b38ff370SJamie Gritton 2394b38ff370SJamie Gritton vfs_opterror(opts, "no jail specified"); 2395b38ff370SJamie Gritton error = ENOENT; 23962a4b2251SJamie Gritton goto done; 2397b38ff370SJamie Gritton 2398b38ff370SJamie Gritton found_prison: 2399b38ff370SJamie Gritton /* Get the parameters of the prison. */ 24006754ae25SJamie Gritton prison_hold(pr); 24012a4b2251SJamie Gritton drflags |= PD_DEREF; 2402b38ff370SJamie Gritton td->td_retval[0] = pr->pr_id; 2403b38ff370SJamie Gritton error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id)); 2404b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24052a4b2251SJamie Gritton goto done; 24060304c731SJamie Gritton i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id; 24070304c731SJamie Gritton error = vfs_setopt(opts, "parent", &i, sizeof(i)); 2408b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24092a4b2251SJamie Gritton goto done; 24100304c731SJamie Gritton error = vfs_setopts(opts, "name", prison_name(mypr, pr)); 24110304c731SJamie Gritton if (error != 0 && error != ENOENT) 24122a4b2251SJamie Gritton goto done; 24130304c731SJamie Gritton error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id, 2414b38ff370SJamie Gritton sizeof(pr->pr_cpuset->cs_id)); 2415b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24162a4b2251SJamie Gritton goto done; 24170304c731SJamie Gritton error = vfs_setopts(opts, "path", prison_path(mypr, pr)); 2418b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24192a4b2251SJamie Gritton goto done; 2420b38ff370SJamie Gritton #ifdef INET 2421500f82d6SZhenlei Huang error = vfs_setopt_part(opts, "ip4.addr", pr->pr_addrs[PR_INET]->pr_ip, 2422eb8dcdeaSGleb Smirnoff pr->pr_addrs[PR_INET] ? pr->pr_addrs[PR_INET]->ips * 2423eb8dcdeaSGleb Smirnoff pr_families[PR_INET].size : 0 ); 2424b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24252a4b2251SJamie Gritton goto done; 2426b38ff370SJamie Gritton #endif 2427b38ff370SJamie Gritton #ifdef INET6 2428500f82d6SZhenlei Huang error = vfs_setopt_part(opts, "ip6.addr", pr->pr_addrs[PR_INET6]->pr_ip, 2429eb8dcdeaSGleb Smirnoff pr->pr_addrs[PR_INET6] ? pr->pr_addrs[PR_INET6]->ips * 2430eb8dcdeaSGleb Smirnoff pr_families[PR_INET6].size : 0 ); 2431b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24322a4b2251SJamie Gritton goto done; 2433b38ff370SJamie Gritton #endif 2434b38ff370SJamie Gritton error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel, 2435b38ff370SJamie Gritton sizeof(pr->pr_securelevel)); 2436b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24372a4b2251SJamie Gritton goto done; 2438b97457e2SJamie Gritton error = vfs_setopt(opts, "children.cur", &pr->pr_childcount, 2439b97457e2SJamie Gritton sizeof(pr->pr_childcount)); 2440b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 24412a4b2251SJamie Gritton goto done; 2442b97457e2SJamie Gritton error = vfs_setopt(opts, "children.max", &pr->pr_childmax, 2443b97457e2SJamie Gritton sizeof(pr->pr_childmax)); 2444b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 24452a4b2251SJamie Gritton goto done; 2446c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostname", pr->pr_hostname); 2447b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24482a4b2251SJamie Gritton goto done; 2449c1f19219SJamie Gritton error = vfs_setopts(opts, "host.domainname", pr->pr_domainname); 245076ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 24512a4b2251SJamie Gritton goto done; 2452c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid); 245376ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 24542a4b2251SJamie Gritton goto done; 2455841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32 2456a5c1afadSDmitry Chagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 245776ca6f88SJamie Gritton uint32_t hid32 = pr->pr_hostid; 245876ca6f88SJamie Gritton 245976ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32)); 246076ca6f88SJamie Gritton } else 246176ca6f88SJamie Gritton #endif 246276ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid, 246376ca6f88SJamie Gritton sizeof(pr->pr_hostid)); 246476ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 24652a4b2251SJamie Gritton goto done; 24660304c731SJamie Gritton error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs, 24670304c731SJamie Gritton sizeof(pr->pr_enforce_statfs)); 24680304c731SJamie Gritton if (error != 0 && error != ENOENT) 24692a4b2251SJamie Gritton goto done; 24700cc207a6SMartin Matuska error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum, 24710cc207a6SMartin Matuska sizeof(pr->pr_devfs_rsnum)); 24720cc207a6SMartin Matuska if (error != 0 && error != ENOENT) 24732a4b2251SJamie Gritton goto done; 2474672756aaSJamie Gritton for (bf = pr_flag_bool; 2475672756aaSJamie Gritton bf < pr_flag_bool + nitems(pr_flag_bool); 2476672756aaSJamie Gritton bf++) { 2477672756aaSJamie Gritton i = (pr->pr_flags & bf->flag) ? 1 : 0; 2478672756aaSJamie Gritton error = vfs_setopt(opts, bf->name, &i, sizeof(i)); 2479b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24802a4b2251SJamie Gritton goto done; 2481b38ff370SJamie Gritton i = !i; 2482672756aaSJamie Gritton error = vfs_setopt(opts, bf->noname, &i, sizeof(i)); 2483b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24842a4b2251SJamie Gritton goto done; 24850304c731SJamie Gritton } 2486672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 2487672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 2488672756aaSJamie Gritton jsf++) { 2489672756aaSJamie Gritton f = pr->pr_flags & (jsf->disable | jsf->new); 2490672756aaSJamie Gritton i = (f != 0 && f == jsf->disable) ? JAIL_SYS_DISABLE 2491672756aaSJamie Gritton : (f == jsf->new) ? JAIL_SYS_NEW 24927cbf7213SJamie Gritton : JAIL_SYS_INHERIT; 2493672756aaSJamie Gritton error = vfs_setopt(opts, jsf->name, &i, sizeof(i)); 24947cbf7213SJamie Gritton if (error != 0 && error != ENOENT) 24952a4b2251SJamie Gritton goto done; 24967cbf7213SJamie Gritton } 2497672756aaSJamie Gritton for (bf = pr_flag_allow; 24985d58f959SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 24995d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 2500672756aaSJamie Gritton bf++) { 2501672756aaSJamie Gritton i = (pr->pr_allow & bf->flag) ? 1 : 0; 2502672756aaSJamie Gritton error = vfs_setopt(opts, bf->name, &i, sizeof(i)); 25030304c731SJamie Gritton if (error != 0 && error != ENOENT) 25042a4b2251SJamie Gritton goto done; 25050304c731SJamie Gritton i = !i; 2506672756aaSJamie Gritton error = vfs_setopt(opts, bf->noname, &i, sizeof(i)); 25070304c731SJamie Gritton if (error != 0 && error != ENOENT) 25082a4b2251SJamie Gritton goto done; 25090304c731SJamie Gritton } 251076ad42abSJamie Gritton i = !prison_isalive(pr); 2511b38ff370SJamie Gritton error = vfs_setopt(opts, "dying", &i, sizeof(i)); 2512b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 25132a4b2251SJamie Gritton goto done; 2514b38ff370SJamie Gritton i = !i; 2515b38ff370SJamie Gritton error = vfs_setopt(opts, "nodying", &i, sizeof(i)); 2516b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 25172a4b2251SJamie Gritton goto done; 2518bd96bd15SIan Lepore error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate, 2519bd96bd15SIan Lepore sizeof(pr->pr_osreldate)); 2520a1a4c1b0SIan Lepore if (error != 0 && error != ENOENT) 25212a4b2251SJamie Gritton goto done; 2522a1a4c1b0SIan Lepore error = vfs_setopts(opts, "osrelease", pr->pr_osrelease); 2523a1a4c1b0SIan Lepore if (error != 0 && error != ENOENT) 25242a4b2251SJamie Gritton goto done; 2525b38ff370SJamie Gritton 2526b38ff370SJamie Gritton /* Get the module parameters. */ 2527b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 25282a4b2251SJamie Gritton drflags &= ~PD_LOCKED; 2529b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_GET, opts); 2530b38ff370SJamie Gritton if (error) 25312a4b2251SJamie Gritton goto done; 25322a4b2251SJamie Gritton prison_deref(pr, drflags); 25332a4b2251SJamie Gritton pr = NULL; 25342a4b2251SJamie Gritton drflags = 0; 2535b38ff370SJamie Gritton 2536b38ff370SJamie Gritton /* By now, all parameters should have been noted. */ 2537b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2538b38ff370SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 2539b38ff370SJamie Gritton error = EINVAL; 2540b38ff370SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 25412a4b2251SJamie Gritton goto done; 2542b38ff370SJamie Gritton } 2543b38ff370SJamie Gritton } 2544b38ff370SJamie Gritton 2545b38ff370SJamie Gritton /* Write the fetched parameters back to userspace. */ 2546b38ff370SJamie Gritton error = 0; 2547b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2548b38ff370SJamie Gritton if (opt->pos >= 0 && opt->pos != errmsg_pos) { 2549b38ff370SJamie Gritton pos = 2 * opt->pos + 1; 2550b38ff370SJamie Gritton optuio->uio_iov[pos].iov_len = opt->len; 2551b38ff370SJamie Gritton if (opt->value != NULL) { 2552b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) { 2553b38ff370SJamie Gritton bcopy(opt->value, 2554b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2555b38ff370SJamie Gritton opt->len); 2556b38ff370SJamie Gritton } else { 2557b38ff370SJamie Gritton error = copyout(opt->value, 2558b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2559b38ff370SJamie Gritton opt->len); 2560b38ff370SJamie Gritton if (error) 2561b38ff370SJamie Gritton break; 2562b38ff370SJamie Gritton } 2563b38ff370SJamie Gritton } 2564b38ff370SJamie Gritton } 2565b38ff370SJamie Gritton } 2566b38ff370SJamie Gritton 25672a4b2251SJamie Gritton done: 25682a4b2251SJamie Gritton /* Release any temporary prison holds and/or locks. */ 25692a4b2251SJamie Gritton if (pr != NULL) 25702a4b2251SJamie Gritton prison_deref(pr, drflags); 25712a4b2251SJamie Gritton else if (drflags & PD_LIST_SLOCKED) 2572b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2573b38ff370SJamie Gritton if (error && errmsg_pos >= 0) { 25742a4b2251SJamie Gritton /* Write the error message back to userspace. */ 2575b38ff370SJamie Gritton vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 2576b38ff370SJamie Gritton errmsg_pos = 2 * errmsg_pos + 1; 2577b38ff370SJamie Gritton if (errmsg_len > 0) { 2578b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 2579b38ff370SJamie Gritton bcopy(errmsg, 2580b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2581b38ff370SJamie Gritton errmsg_len); 2582b38ff370SJamie Gritton else 2583b38ff370SJamie Gritton copyout(errmsg, 2584b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2585b38ff370SJamie Gritton errmsg_len); 2586b38ff370SJamie Gritton } 2587b38ff370SJamie Gritton } 2588b38ff370SJamie Gritton vfs_freeopts(opts); 2589b38ff370SJamie Gritton return (error); 2590b38ff370SJamie Gritton } 2591b38ff370SJamie Gritton 2592b38ff370SJamie Gritton /* 2593b38ff370SJamie Gritton * struct jail_remove_args { 2594b38ff370SJamie Gritton * int jid; 2595b38ff370SJamie Gritton * }; 2596b38ff370SJamie Gritton */ 2597b38ff370SJamie Gritton int 25988451d0ddSKip Macy sys_jail_remove(struct thread *td, struct jail_remove_args *uap) 2599b38ff370SJamie Gritton { 2600c861373bSJamie Gritton struct prison *pr; 2601c861373bSJamie Gritton int error; 2602b38ff370SJamie Gritton 2603b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_REMOVE); 2604b38ff370SJamie Gritton if (error) 2605b38ff370SJamie Gritton return (error); 2606b38ff370SJamie Gritton 2607b38ff370SJamie Gritton sx_xlock(&allprison_lock); 26080304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2609b38ff370SJamie Gritton if (pr == NULL) { 2610b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2611b38ff370SJamie Gritton return (EINVAL); 2612b38ff370SJamie Gritton } 2613c861373bSJamie Gritton if (!prison_isalive(pr)) { 2614c861373bSJamie Gritton /* Silently ignore already-dying prisons. */ 26150304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 2616c861373bSJamie Gritton sx_xunlock(&allprison_lock); 26170304c731SJamie Gritton return (0); 26180304c731SJamie Gritton } 2619c861373bSJamie Gritton prison_deref(pr, PD_KILL | PD_LOCKED | PD_LIST_XLOCKED); 2620c861373bSJamie Gritton return (0); 262175c13541SPoul-Henning Kamp } 262275c13541SPoul-Henning Kamp 2623fd7a8150SMike Barcroft /* 26249ddb7954SMike Barcroft * struct jail_attach_args { 26259ddb7954SMike Barcroft * int jid; 26269ddb7954SMike Barcroft * }; 2627fd7a8150SMike Barcroft */ 2628fd7a8150SMike Barcroft int 26298451d0ddSKip Macy sys_jail_attach(struct thread *td, struct jail_attach_args *uap) 2630fd7a8150SMike Barcroft { 2631b38ff370SJamie Gritton struct prison *pr; 2632b38ff370SJamie Gritton int error; 2633b38ff370SJamie Gritton 2634b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 2635b38ff370SJamie Gritton if (error) 2636b38ff370SJamie Gritton return (error); 2637b38ff370SJamie Gritton 2638f7496dcaSJamie Gritton sx_slock(&allprison_lock); 26390304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2640b38ff370SJamie Gritton if (pr == NULL) { 2641b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2642b38ff370SJamie Gritton return (EINVAL); 2643b38ff370SJamie Gritton } 2644b38ff370SJamie Gritton 264576ad42abSJamie Gritton /* Do not allow a process to attach to a prison that is not alive. */ 264676ad42abSJamie Gritton if (!prison_isalive(pr)) { 2647b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2648b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2649b38ff370SJamie Gritton return (EINVAL); 2650b38ff370SJamie Gritton } 2651b38ff370SJamie Gritton 2652f7496dcaSJamie Gritton return (do_jail_attach(td, pr, PD_LOCKED | PD_LIST_SLOCKED)); 2653b38ff370SJamie Gritton } 2654b38ff370SJamie Gritton 2655b38ff370SJamie Gritton static int 2656f7496dcaSJamie Gritton do_jail_attach(struct thread *td, struct prison *pr, int drflags) 2657b38ff370SJamie Gritton { 2658fd7a8150SMike Barcroft struct proc *p; 2659fd7a8150SMike Barcroft struct ucred *newcred, *oldcred; 26605050aa86SKonstantin Belousov int error; 2661fd7a8150SMike Barcroft 2662f7496dcaSJamie Gritton mtx_assert(&pr->pr_mtx, MA_OWNED); 2663f7496dcaSJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 2664589e4c1dSJamie Gritton drflags &= PD_LOCK_FLAGS; 266557f22bd4SJacques Vidrine /* 266657f22bd4SJacques Vidrine * XXX: Note that there is a slight race here if two threads 266757f22bd4SJacques Vidrine * in the same privileged process attempt to attach to two 266857f22bd4SJacques Vidrine * different jails at the same time. It is important for 266957f22bd4SJacques Vidrine * user processes not to do this, or they might end up with 267057f22bd4SJacques Vidrine * a process root from one prison, but attached to the jail 267157f22bd4SJacques Vidrine * of another. 267257f22bd4SJacques Vidrine */ 26731158508aSJamie Gritton prison_hold(pr); 26746754ae25SJamie Gritton refcount_acquire(&pr->pr_uref); 2675f7496dcaSJamie Gritton drflags |= PD_DEREF | PD_DEUREF; 2676fd7a8150SMike Barcroft mtx_unlock(&pr->pr_mtx); 2677f7496dcaSJamie Gritton drflags &= ~PD_LOCKED; 2678b38ff370SJamie Gritton 2679b38ff370SJamie Gritton /* Let modules do whatever they need to prepare for attaching. */ 2680b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_ATTACH, td); 2681b38ff370SJamie Gritton if (error) { 2682f7496dcaSJamie Gritton prison_deref(pr, drflags); 2683b38ff370SJamie Gritton return (error); 2684b38ff370SJamie Gritton } 2685f7496dcaSJamie Gritton sx_unlock(&allprison_lock); 2686f7496dcaSJamie Gritton drflags &= ~(PD_LIST_SLOCKED | PD_LIST_XLOCKED); 2687fd7a8150SMike Barcroft 2688413628a7SBjoern A. Zeeb /* 2689413628a7SBjoern A. Zeeb * Reparent the newly attached process to this jail. 2690413628a7SBjoern A. Zeeb */ 2691b38ff370SJamie Gritton p = td->td_proc; 2692413628a7SBjoern A. Zeeb error = cpuset_setproc_update_set(p, pr->pr_cpuset); 2693413628a7SBjoern A. Zeeb if (error) 2694b38ff370SJamie Gritton goto e_revert_osd; 2695413628a7SBjoern A. Zeeb 2696cb05b60aSAttilio Rao vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY); 2697fd7a8150SMike Barcroft if ((error = change_dir(pr->pr_root, td)) != 0) 2698fd7a8150SMike Barcroft goto e_unlock; 2699fd7a8150SMike Barcroft #ifdef MAC 270030d239bcSRobert Watson if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root))) 2701fd7a8150SMike Barcroft goto e_unlock; 2702fd7a8150SMike Barcroft #endif 2703b249ce48SMateusz Guzik VOP_UNLOCK(pr->pr_root); 2704d4380c0cSJamie Gritton if ((error = pwd_chroot_chdir(td, pr->pr_root))) 27055050aa86SKonstantin Belousov goto e_revert_osd; 2706fd7a8150SMike Barcroft 2707fd7a8150SMike Barcroft newcred = crget(); 2708fd7a8150SMike Barcroft PROC_LOCK(p); 27091fb6767dSJamie Gritton oldcred = crcopysafe(p, newcred); 271069c4ee54SJohn Baldwin newcred->cr_prison = pr; 2711daf63fd2SMateusz Guzik proc_set_cred(p, newcred); 27121fb6767dSJamie Gritton setsugid(p); 2713097055e2SEdward Tomasz Napierala #ifdef RACCT 2714097055e2SEdward Tomasz Napierala racct_proc_ucred_changed(p, oldcred, newcred); 2715f87beb93SAndriy Gapon crhold(newcred); 2716f87beb93SAndriy Gapon #endif 2717f87beb93SAndriy Gapon PROC_UNLOCK(p); 2718f87beb93SAndriy Gapon #ifdef RCTL 2719f87beb93SAndriy Gapon rctl_proc_ucred_changed(p, newcred); 2720f87beb93SAndriy Gapon crfree(newcred); 2721097055e2SEdward Tomasz Napierala #endif 27225ecb5444SMateusz Guzik prison_proc_relink(oldcred->cr_prison, pr, p); 2723f7496dcaSJamie Gritton prison_deref(oldcred->cr_prison, drflags); 2724fd7a8150SMike Barcroft crfree(oldcred); 2725cc7b7306SJamie Gritton 2726cc7b7306SJamie Gritton /* 2727cc7b7306SJamie Gritton * If the prison was killed while changing credentials, die along 2728cc7b7306SJamie Gritton * with it. 2729cc7b7306SJamie Gritton */ 2730cc7b7306SJamie Gritton if (!prison_isalive(pr)) { 2731cc7b7306SJamie Gritton PROC_LOCK(p); 2732cc7b7306SJamie Gritton kern_psignal(p, SIGKILL); 2733cc7b7306SJamie Gritton PROC_UNLOCK(p); 2734cc7b7306SJamie Gritton } 2735cc7b7306SJamie Gritton 2736fd7a8150SMike Barcroft return (0); 27371fb6767dSJamie Gritton 2738fd7a8150SMike Barcroft e_unlock: 2739b249ce48SMateusz Guzik VOP_UNLOCK(pr->pr_root); 2740b38ff370SJamie Gritton e_revert_osd: 2741b38ff370SJamie Gritton /* Tell modules this thread is still in its old jail after all. */ 27427f4e7248SJamie Gritton sx_slock(&allprison_lock); 2743f7496dcaSJamie Gritton drflags |= PD_LIST_SLOCKED; 27441fb6767dSJamie Gritton (void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td); 2745f7496dcaSJamie Gritton prison_deref(pr, drflags); 2746fd7a8150SMike Barcroft return (error); 2747fd7a8150SMike Barcroft } 2748fd7a8150SMike Barcroft 2749fd7a8150SMike Barcroft /* 2750fd7a8150SMike Barcroft * Returns a locked prison instance, or NULL on failure. 2751fd7a8150SMike Barcroft */ 275254b369c1SPawel Jakub Dawidek struct prison * 2753fd7a8150SMike Barcroft prison_find(int prid) 2754fd7a8150SMike Barcroft { 2755fd7a8150SMike Barcroft struct prison *pr; 2756fd7a8150SMike Barcroft 2757dc68a633SPawel Jakub Dawidek sx_assert(&allprison_lock, SX_LOCKED); 2758b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 2759f7496dcaSJamie Gritton if (pr->pr_id < prid) 2760f7496dcaSJamie Gritton continue; 27617de883c8SJamie Gritton if (pr->pr_id > prid) 27627de883c8SJamie Gritton break; 2763f7496dcaSJamie Gritton KASSERT(prison_isvalid(pr), ("Found invalid prison %p", pr)); 2764f7496dcaSJamie Gritton mtx_lock(&pr->pr_mtx); 2765f7496dcaSJamie Gritton return (pr); 2766fd7a8150SMike Barcroft } 2767fd7a8150SMike Barcroft return (NULL); 2768fd7a8150SMike Barcroft } 2769fd7a8150SMike Barcroft 2770b38ff370SJamie Gritton /* 27710304c731SJamie Gritton * Find a prison that is a descendant of mypr. Returns a locked prison or NULL. 2772b38ff370SJamie Gritton */ 2773b38ff370SJamie Gritton struct prison * 27740304c731SJamie Gritton prison_find_child(struct prison *mypr, int prid) 2775b38ff370SJamie Gritton { 27760304c731SJamie Gritton struct prison *pr; 27770304c731SJamie Gritton int descend; 2778b38ff370SJamie Gritton 2779b38ff370SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 27800304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 27810304c731SJamie Gritton if (pr->pr_id == prid) { 2782f7496dcaSJamie Gritton KASSERT(prison_isvalid(pr), 2783f7496dcaSJamie Gritton ("Found invalid prison %p", pr)); 27840304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 27850304c731SJamie Gritton return (pr); 27860304c731SJamie Gritton } 27870304c731SJamie Gritton } 27880304c731SJamie Gritton return (NULL); 27890304c731SJamie Gritton } 27900304c731SJamie Gritton 27910304c731SJamie Gritton /* 27920304c731SJamie Gritton * Look for the name relative to mypr. Returns a locked prison or NULL. 27930304c731SJamie Gritton */ 27940304c731SJamie Gritton struct prison * 27950304c731SJamie Gritton prison_find_name(struct prison *mypr, const char *name) 27960304c731SJamie Gritton { 27970304c731SJamie Gritton struct prison *pr, *deadpr; 27980304c731SJamie Gritton size_t mylen; 27990304c731SJamie Gritton int descend; 28000304c731SJamie Gritton 28010304c731SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 28020304c731SJamie Gritton mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1; 2803b38ff370SJamie Gritton deadpr = NULL; 28040304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 28050304c731SJamie Gritton if (!strcmp(pr->pr_name + mylen, name)) { 2806f7496dcaSJamie Gritton KASSERT(prison_isvalid(pr), 2807f7496dcaSJamie Gritton ("Found invalid prison %p", pr)); 2808f7496dcaSJamie Gritton if (prison_isalive(pr)) { 2809b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 2810b38ff370SJamie Gritton return (pr); 2811f7496dcaSJamie Gritton } 2812b38ff370SJamie Gritton deadpr = pr; 2813b38ff370SJamie Gritton } 2814b38ff370SJamie Gritton } 28150304c731SJamie Gritton /* There was no valid prison - perhaps there was a dying one. */ 2816f7496dcaSJamie Gritton if (deadpr != NULL) 2817b38ff370SJamie Gritton mtx_lock(&deadpr->pr_mtx); 2818b38ff370SJamie Gritton return (deadpr); 2819b38ff370SJamie Gritton } 2820b38ff370SJamie Gritton 2821b38ff370SJamie Gritton /* 28220fe74ae6SJamie Gritton * See if a prison has the specific flag set. The prison should be locked, 28230fe74ae6SJamie Gritton * unless checking for flags that are only set at jail creation (such as 28240fe74ae6SJamie Gritton * PR_IP4 and PR_IP6), or only the single bit is examined, without regard 28250fe74ae6SJamie Gritton * to any other prison data. 28260304c731SJamie Gritton */ 28270b0ae2e4SMina Galić bool 28280304c731SJamie Gritton prison_flag(struct ucred *cred, unsigned flag) 28290304c731SJamie Gritton { 28300304c731SJamie Gritton 28310b0ae2e4SMina Galić return ((cred->cr_prison->pr_flags & flag) != 0); 28320304c731SJamie Gritton } 28330304c731SJamie Gritton 28340b0ae2e4SMina Galić /* 28350b0ae2e4SMina Galić * See if a prison has the specific allow flag set. 28360b0ae2e4SMina Galić * The prison *should* be locked, or only a single bit is examined, without 28370b0ae2e4SMina Galić * regard to any other prison data. 28380b0ae2e4SMina Galić */ 28390b0ae2e4SMina Galić bool 28400304c731SJamie Gritton prison_allow(struct ucred *cred, unsigned flag) 28410304c731SJamie Gritton { 28420304c731SJamie Gritton 28430fe74ae6SJamie Gritton return ((cred->cr_prison->pr_allow & flag) != 0); 28440304c731SJamie Gritton } 28450304c731SJamie Gritton 28460304c731SJamie Gritton /* 2847effad35eSJamie Gritton * Hold a prison reference, by incrementing pr_ref. It is generally 2848effad35eSJamie Gritton * an error to hold a prison that does not already have a reference. 2849effad35eSJamie Gritton * A prison record will remain valid as long as it has at least one 2850effad35eSJamie Gritton * reference, and will not be removed as long as either the prison 2851effad35eSJamie Gritton * mutex or the allprison lock is held (allprison_lock may be shared). 2852effad35eSJamie Gritton */ 2853effad35eSJamie Gritton void 2854effad35eSJamie Gritton prison_hold_locked(struct prison *pr) 2855effad35eSJamie Gritton { 2856effad35eSJamie Gritton 28576754ae25SJamie Gritton /* Locking is no longer required. */ 28586754ae25SJamie Gritton prison_hold(pr); 2859effad35eSJamie Gritton } 2860effad35eSJamie Gritton 2861effad35eSJamie Gritton void 2862effad35eSJamie Gritton prison_hold(struct prison *pr) 2863effad35eSJamie Gritton { 28646754ae25SJamie Gritton #ifdef INVARIANTS 28656754ae25SJamie Gritton int was_valid = refcount_acquire_if_not_zero(&pr->pr_ref); 2866effad35eSJamie Gritton 28676754ae25SJamie Gritton KASSERT(was_valid, 28686754ae25SJamie Gritton ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id)); 28696754ae25SJamie Gritton #else 28706754ae25SJamie Gritton refcount_acquire(&pr->pr_ref); 28716754ae25SJamie Gritton #endif 2872effad35eSJamie Gritton } 2873effad35eSJamie Gritton 2874effad35eSJamie Gritton /* 2875effad35eSJamie Gritton * Remove a prison reference. If that was the last reference, the 2876f7496dcaSJamie Gritton * prison will be removed (at a later time). 2877b38ff370SJamie Gritton */ 287891421ba2SRobert Watson void 28791ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr) 288091421ba2SRobert Watson { 288191421ba2SRobert Watson 28821ba4a712SPawel Jakub Dawidek mtx_assert(&pr->pr_mtx, MA_OWNED); 2883effad35eSJamie Gritton /* 2884f7496dcaSJamie Gritton * Locking is no longer required, but unlock because the caller 2885f7496dcaSJamie Gritton * expects it. 2886effad35eSJamie Gritton */ 2887f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 2888f7496dcaSJamie Gritton prison_free(pr); 2889effad35eSJamie Gritton } 2890c2cda609SPawel Jakub Dawidek 28911ba4a712SPawel Jakub Dawidek void 28921ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr) 28931ba4a712SPawel Jakub Dawidek { 28941ba4a712SPawel Jakub Dawidek 28956754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 28966754ae25SJamie Gritton ("Trying to free dead prison %p (jid=%d).", 28976754ae25SJamie Gritton pr, pr->pr_id)); 2898f7496dcaSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_ref)) { 2899f7496dcaSJamie Gritton /* 2900f7496dcaSJamie Gritton * Don't remove the last reference in this context, 2901f7496dcaSJamie Gritton * in case there are locks held. 2902f7496dcaSJamie Gritton */ 2903f7496dcaSJamie Gritton taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 2904f7496dcaSJamie Gritton } 2905f7496dcaSJamie Gritton } 2906f7496dcaSJamie Gritton 2907f7496dcaSJamie Gritton static void 2908f7496dcaSJamie Gritton prison_free_not_last(struct prison *pr) 2909f7496dcaSJamie Gritton { 2910f7496dcaSJamie Gritton #ifdef INVARIANTS 2911f7496dcaSJamie Gritton int lastref; 2912f7496dcaSJamie Gritton 2913f7496dcaSJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 2914f7496dcaSJamie Gritton ("Trying to free dead prison %p (jid=%d).", 2915f7496dcaSJamie Gritton pr, pr->pr_id)); 2916f7496dcaSJamie Gritton lastref = refcount_release(&pr->pr_ref); 2917f7496dcaSJamie Gritton KASSERT(!lastref, 2918f7496dcaSJamie Gritton ("prison_free_not_last freed last ref on prison %p (jid=%d).", 2919f7496dcaSJamie Gritton pr, pr->pr_id)); 2920f7496dcaSJamie Gritton #else 2921ee9b37aeSMateusz Guzik refcount_release(&pr->pr_ref); 2922f7496dcaSJamie Gritton #endif 29231ba4a712SPawel Jakub Dawidek } 29241ba4a712SPawel Jakub Dawidek 292573d9e52dSJamie Gritton /* 2926f171938cSGordon Bergling * Hold a prison for user visibility, by incrementing pr_uref. 2927effad35eSJamie Gritton * It is generally an error to hold a prison that isn't already 292849a033d8SGordon Bergling * user-visible, except through the jail system calls. It is also 2929effad35eSJamie Gritton * an error to hold an invalid prison. A prison record will remain 2930effad35eSJamie Gritton * alive as long as it has at least one user reference, and will not 2931f7496dcaSJamie Gritton * be set to the dying state until the prison mutex and allprison_lock 2932f7496dcaSJamie Gritton * are both freed. 2933effad35eSJamie Gritton */ 2934effad35eSJamie Gritton void 2935effad35eSJamie Gritton prison_proc_hold(struct prison *pr) 2936effad35eSJamie Gritton { 29376754ae25SJamie Gritton #ifdef INVARIANTS 29386754ae25SJamie Gritton int was_alive = refcount_acquire_if_not_zero(&pr->pr_uref); 2939effad35eSJamie Gritton 29406754ae25SJamie Gritton KASSERT(was_alive, 2941effad35eSJamie Gritton ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id)); 29426754ae25SJamie Gritton #else 29436754ae25SJamie Gritton refcount_acquire(&pr->pr_uref); 29446754ae25SJamie Gritton #endif 2945effad35eSJamie Gritton } 2946effad35eSJamie Gritton 2947effad35eSJamie Gritton /* 2948effad35eSJamie Gritton * Remove a prison user reference. If it was the last reference, the 2949effad35eSJamie Gritton * prison will be considered "dying", and may be removed once all of 2950effad35eSJamie Gritton * its references are dropped. 2951effad35eSJamie Gritton */ 2952effad35eSJamie Gritton void 2953effad35eSJamie Gritton prison_proc_free(struct prison *pr) 2954effad35eSJamie Gritton { 2955effad35eSJamie Gritton 29566754ae25SJamie Gritton /* 29576754ae25SJamie Gritton * Locking is only required when releasing the last reference. 29586754ae25SJamie Gritton * This allows assurance that a locked prison will remain alive 29596754ae25SJamie Gritton * until it is unlocked. 29606754ae25SJamie Gritton */ 29616754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_uref) > 0, 2962effad35eSJamie Gritton ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id)); 2963195cd6aeSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_uref)) { 2964effad35eSJamie Gritton /* 2965effad35eSJamie Gritton * Don't remove the last user reference in this context, 2966effad35eSJamie Gritton * which is expected to be a process that is not only locked, 2967effad35eSJamie Gritton * but also half dead. Add a reference so any calls to 2968effad35eSJamie Gritton * prison_free() won't re-submit the task. 2969effad35eSJamie Gritton */ 2970f7496dcaSJamie Gritton prison_hold(pr); 29711158508aSJamie Gritton mtx_lock(&pr->pr_mtx); 29721158508aSJamie Gritton KASSERT(!(pr->pr_flags & PR_COMPLETE_PROC), 29731158508aSJamie Gritton ("Redundant last reference in prison_proc_free (jid=%d)", 29741158508aSJamie Gritton pr->pr_id)); 29751158508aSJamie Gritton pr->pr_flags |= PR_COMPLETE_PROC; 29761158508aSJamie Gritton mtx_unlock(&pr->pr_mtx); 2977effad35eSJamie Gritton taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 2978effad35eSJamie Gritton } 2979effad35eSJamie Gritton } 2980effad35eSJamie Gritton 2981c861373bSJamie Gritton static void 2982c861373bSJamie Gritton prison_proc_free_not_last(struct prison *pr) 2983c861373bSJamie Gritton { 2984c861373bSJamie Gritton #ifdef INVARIANTS 2985c861373bSJamie Gritton int lastref; 2986c861373bSJamie Gritton 2987c861373bSJamie Gritton KASSERT(refcount_load(&pr->pr_uref) > 0, 2988c861373bSJamie Gritton ("Trying to free dead prison %p (jid=%d).", 2989c861373bSJamie Gritton pr, pr->pr_id)); 2990c861373bSJamie Gritton lastref = refcount_release(&pr->pr_uref); 2991c861373bSJamie Gritton KASSERT(!lastref, 2992c861373bSJamie Gritton ("prison_proc_free_not_last freed last uref on prison %p (jid=%d).", 2993c861373bSJamie Gritton pr, pr->pr_id)); 2994c861373bSJamie Gritton #else 2995c861373bSJamie Gritton refcount_release(&pr->pr_uref); 2996c861373bSJamie Gritton #endif 2997c861373bSJamie Gritton } 2998c861373bSJamie Gritton 29995ecb5444SMateusz Guzik void 30005ecb5444SMateusz Guzik prison_proc_link(struct prison *pr, struct proc *p) 30015ecb5444SMateusz Guzik { 30025ecb5444SMateusz Guzik 30035ecb5444SMateusz Guzik sx_assert(&allproc_lock, SA_XLOCKED); 30045ecb5444SMateusz Guzik LIST_INSERT_HEAD(&pr->pr_proclist, p, p_jaillist); 30055ecb5444SMateusz Guzik } 30065ecb5444SMateusz Guzik 30075ecb5444SMateusz Guzik void 30085ecb5444SMateusz Guzik prison_proc_unlink(struct prison *pr, struct proc *p) 30095ecb5444SMateusz Guzik { 30105ecb5444SMateusz Guzik 30115ecb5444SMateusz Guzik sx_assert(&allproc_lock, SA_XLOCKED); 30125ecb5444SMateusz Guzik LIST_REMOVE(p, p_jaillist); 30135ecb5444SMateusz Guzik } 30145ecb5444SMateusz Guzik 30155ecb5444SMateusz Guzik static void 30165ecb5444SMateusz Guzik prison_proc_relink(struct prison *opr, struct prison *npr, struct proc *p) 30175ecb5444SMateusz Guzik { 30185ecb5444SMateusz Guzik 30195ecb5444SMateusz Guzik sx_xlock(&allproc_lock); 30205ecb5444SMateusz Guzik prison_proc_unlink(opr, p); 30215ecb5444SMateusz Guzik prison_proc_link(npr, p); 30225ecb5444SMateusz Guzik sx_xunlock(&allproc_lock); 30235ecb5444SMateusz Guzik } 30245ecb5444SMateusz Guzik 3025effad35eSJamie Gritton /* 302673d9e52dSJamie Gritton * Complete a call to either prison_free or prison_proc_free. 302773d9e52dSJamie Gritton */ 3028c2cda609SPawel Jakub Dawidek static void 3029c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending) 3030c2cda609SPawel Jakub Dawidek { 303173d9e52dSJamie Gritton struct prison *pr = context; 3032f7496dcaSJamie Gritton int drflags; 3033b38ff370SJamie Gritton 3034effad35eSJamie Gritton /* 30351158508aSJamie Gritton * This could be called to release the last reference, or the last 30361158508aSJamie Gritton * user reference (plus the reference held in prison_proc_free). 3037effad35eSJamie Gritton */ 3038f7496dcaSJamie Gritton drflags = prison_lock_xlock(pr, PD_DEREF); 30391158508aSJamie Gritton if (pr->pr_flags & PR_COMPLETE_PROC) { 30401158508aSJamie Gritton pr->pr_flags &= ~PR_COMPLETE_PROC; 3041f7496dcaSJamie Gritton drflags |= PD_DEUREF; 30421158508aSJamie Gritton } 3043f7496dcaSJamie Gritton prison_deref(pr, drflags); 3044b38ff370SJamie Gritton } 3045b38ff370SJamie Gritton 30465ecb5444SMateusz Guzik static void 30475ecb5444SMateusz Guzik prison_kill_processes_cb(struct proc *p, void *arg __unused) 30485ecb5444SMateusz Guzik { 30495ecb5444SMateusz Guzik 30505ecb5444SMateusz Guzik kern_psignal(p, SIGKILL); 30515ecb5444SMateusz Guzik } 30525ecb5444SMateusz Guzik 30535ecb5444SMateusz Guzik /* 30545ecb5444SMateusz Guzik * Note the iteration does not guarantee acting on all processes. 30555ecb5444SMateusz Guzik * Most notably there may be fork or jail_attach in progress. 30565ecb5444SMateusz Guzik */ 30575ecb5444SMateusz Guzik void 30585ecb5444SMateusz Guzik prison_proc_iterate(struct prison *pr, void (*cb)(struct proc *, void *), 30595ecb5444SMateusz Guzik void *cbarg) 30605ecb5444SMateusz Guzik { 30615ecb5444SMateusz Guzik struct prison *ppr; 30625ecb5444SMateusz Guzik struct proc *p; 30635ecb5444SMateusz Guzik 30645ecb5444SMateusz Guzik if (atomic_load_int(&pr->pr_childcount) == 0) { 30655ecb5444SMateusz Guzik sx_slock(&allproc_lock); 30665ecb5444SMateusz Guzik LIST_FOREACH(p, &pr->pr_proclist, p_jaillist) { 30675ecb5444SMateusz Guzik if (p->p_state == PRS_NEW) 30685ecb5444SMateusz Guzik continue; 30695ecb5444SMateusz Guzik PROC_LOCK(p); 30705ecb5444SMateusz Guzik cb(p, cbarg); 30715ecb5444SMateusz Guzik PROC_UNLOCK(p); 30725ecb5444SMateusz Guzik } 30735ecb5444SMateusz Guzik sx_sunlock(&allproc_lock); 30745ecb5444SMateusz Guzik if (atomic_load_int(&pr->pr_childcount) == 0) 30755ecb5444SMateusz Guzik return; 30765ecb5444SMateusz Guzik /* 30775ecb5444SMateusz Guzik * Some jails popped up during the iteration, fall through to a 30785ecb5444SMateusz Guzik * system-wide search. 30795ecb5444SMateusz Guzik */ 30805ecb5444SMateusz Guzik } 30815ecb5444SMateusz Guzik 30825ecb5444SMateusz Guzik sx_slock(&allproc_lock); 30835ecb5444SMateusz Guzik FOREACH_PROC_IN_SYSTEM(p) { 30845ecb5444SMateusz Guzik PROC_LOCK(p); 30855ecb5444SMateusz Guzik if (p->p_state != PRS_NEW && p->p_ucred != NULL) { 30865ecb5444SMateusz Guzik for (ppr = p->p_ucred->cr_prison; 30875ecb5444SMateusz Guzik ppr != &prison0; 30885ecb5444SMateusz Guzik ppr = ppr->pr_parent) { 30895ecb5444SMateusz Guzik if (ppr == pr) { 30905ecb5444SMateusz Guzik cb(p, cbarg); 30915ecb5444SMateusz Guzik break; 30925ecb5444SMateusz Guzik } 30935ecb5444SMateusz Guzik } 30945ecb5444SMateusz Guzik } 30955ecb5444SMateusz Guzik PROC_UNLOCK(p); 30965ecb5444SMateusz Guzik } 30975ecb5444SMateusz Guzik sx_sunlock(&allproc_lock); 30985ecb5444SMateusz Guzik } 30995ecb5444SMateusz Guzik 3100b38ff370SJamie Gritton /* 3101effad35eSJamie Gritton * Remove a prison reference and/or user reference (usually). 3102effad35eSJamie Gritton * This assumes context that allows sleeping (for allprison_lock), 3103effad35eSJamie Gritton * with no non-sleeping locks held, except perhaps the prison itself. 3104effad35eSJamie Gritton * If there are no more references, release and delist the prison. 3105effad35eSJamie Gritton * On completion, the prison lock and the allprison lock are both 3106effad35eSJamie Gritton * unlocked. 3107b38ff370SJamie Gritton */ 3108b38ff370SJamie Gritton static void 3109b38ff370SJamie Gritton prison_deref(struct prison *pr, int flags) 3110b38ff370SJamie Gritton { 31116e1d1bfcSJamie Gritton struct prisonlist freeprison; 3112c861373bSJamie Gritton struct prison *killpr, *rpr, *ppr, *tpr; 3113c2cda609SPawel Jakub Dawidek 3114c861373bSJamie Gritton killpr = NULL; 31156e1d1bfcSJamie Gritton TAILQ_INIT(&freeprison); 31166e1d1bfcSJamie Gritton /* 31176e1d1bfcSJamie Gritton * Release this prison as requested, which may cause its parent 31186e1d1bfcSJamie Gritton * to be released, and then maybe its grandparent, etc. 31196e1d1bfcSJamie Gritton */ 31200304c731SJamie Gritton for (;;) { 3121c861373bSJamie Gritton if (flags & PD_KILL) { 3122c861373bSJamie Gritton /* Kill the prison and its descendents. */ 3123589e4c1dSJamie Gritton KASSERT(pr != &prison0, 3124589e4c1dSJamie Gritton ("prison_deref trying to kill prison0")); 3125c861373bSJamie Gritton if (!(flags & PD_DEREF)) { 3126c861373bSJamie Gritton prison_hold(pr); 3127c861373bSJamie Gritton flags |= PD_DEREF; 3128c861373bSJamie Gritton } 3129c861373bSJamie Gritton flags = prison_lock_xlock(pr, flags); 3130c861373bSJamie Gritton prison_deref_kill(pr, &freeprison); 3131c861373bSJamie Gritton } 3132e6d5cb63SJamie Gritton if (flags & PD_DEUREF) { 3133f7496dcaSJamie Gritton /* Drop a user reference. */ 31346754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_uref) > 0, 313573d9e52dSJamie Gritton ("prison_deref PD_DEUREF on a dead prison (jid=%d)", 313673d9e52dSJamie Gritton pr->pr_id)); 3137f7496dcaSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_uref)) { 3138f7496dcaSJamie Gritton if (!(flags & PD_DEREF)) { 3139f7496dcaSJamie Gritton prison_hold(pr); 3140f7496dcaSJamie Gritton flags |= PD_DEREF; 3141f7496dcaSJamie Gritton } 3142f7496dcaSJamie Gritton flags = prison_lock_xlock(pr, flags); 31431158508aSJamie Gritton if (refcount_release(&pr->pr_uref) && 31441158508aSJamie Gritton pr->pr_state == PRISON_STATE_ALIVE) { 3145f7496dcaSJamie Gritton /* 3146f7496dcaSJamie Gritton * When the last user references goes, 3147f7496dcaSJamie Gritton * this becomes a dying prison. 3148f7496dcaSJamie Gritton */ 3149f7496dcaSJamie Gritton KASSERT( 3150f7496dcaSJamie Gritton refcount_load(&prison0.pr_uref) > 0, 31516754ae25SJamie Gritton ("prison0 pr_uref=0")); 31521158508aSJamie Gritton pr->pr_state = PRISON_STATE_DYING; 3153f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 3154f7496dcaSJamie Gritton flags &= ~PD_LOCKED; 3155a9f7455cSJamie Gritton prison_cleanup(pr); 3156f7496dcaSJamie Gritton } 3157f7496dcaSJamie Gritton } 3158f7496dcaSJamie Gritton } 3159c861373bSJamie Gritton if (flags & PD_KILL) { 3160c861373bSJamie Gritton /* 3161c861373bSJamie Gritton * Any remaining user references are probably processes 3162c861373bSJamie Gritton * that need to be killed, either in this prison or its 3163c861373bSJamie Gritton * descendants. 3164c861373bSJamie Gritton */ 3165c861373bSJamie Gritton if (refcount_load(&pr->pr_uref) > 0) 3166c861373bSJamie Gritton killpr = pr; 3167589e4c1dSJamie Gritton /* Make sure the parent prison doesn't get killed. */ 3168589e4c1dSJamie Gritton flags &= ~PD_KILL; 3169c861373bSJamie Gritton } 317073d9e52dSJamie Gritton if (flags & PD_DEREF) { 3171f7496dcaSJamie Gritton /* Drop a reference. */ 31726754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 317373d9e52dSJamie Gritton ("prison_deref PD_DEREF on a dead prison (jid=%d)", 317473d9e52dSJamie Gritton pr->pr_id)); 3175f7496dcaSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_ref)) { 3176f7496dcaSJamie Gritton flags = prison_lock_xlock(pr, flags); 3177f7496dcaSJamie Gritton if (refcount_release(&pr->pr_ref)) { 3178cc5fd8c7SJamie Gritton /* 3179f7496dcaSJamie Gritton * When the last reference goes, 3180f7496dcaSJamie Gritton * unlink the prison and set it aside. 3181cc5fd8c7SJamie Gritton */ 3182f7496dcaSJamie Gritton KASSERT( 3183f7496dcaSJamie Gritton refcount_load(&pr->pr_uref) == 0, 3184f7496dcaSJamie Gritton ("prison_deref: last ref, " 3185f7496dcaSJamie Gritton "but still has %d urefs (jid=%d)", 3186f7496dcaSJamie Gritton pr->pr_uref, pr->pr_id)); 3187f7496dcaSJamie Gritton KASSERT( 3188f7496dcaSJamie Gritton refcount_load(&prison0.pr_ref) != 0, 3189f7496dcaSJamie Gritton ("prison0 pr_ref=0")); 31901158508aSJamie Gritton pr->pr_state = PRISON_STATE_INVALID; 3191b38ff370SJamie Gritton TAILQ_REMOVE(&allprison, pr, pr_list); 31920304c731SJamie Gritton LIST_REMOVE(pr, pr_sibling); 3193f7496dcaSJamie Gritton TAILQ_INSERT_TAIL(&freeprison, pr, 3194f7496dcaSJamie Gritton pr_list); 3195f7496dcaSJamie Gritton for (ppr = pr->pr_parent; 3196f7496dcaSJamie Gritton ppr != NULL; 3197f7496dcaSJamie Gritton ppr = ppr->pr_parent) 3198f7496dcaSJamie Gritton ppr->pr_childcount--; 3199f7496dcaSJamie Gritton /* 3200f7496dcaSJamie Gritton * Removing a prison frees references 3201f7496dcaSJamie Gritton * from its parent. 3202f7496dcaSJamie Gritton */ 3203f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 3204f7496dcaSJamie Gritton flags &= ~PD_LOCKED; 32056e1d1bfcSJamie Gritton pr = pr->pr_parent; 32066e1d1bfcSJamie Gritton flags |= PD_DEREF | PD_DEUREF; 3207f7496dcaSJamie Gritton continue; 3208f7496dcaSJamie Gritton } 3209f7496dcaSJamie Gritton } 3210f7496dcaSJamie Gritton } 3211f7496dcaSJamie Gritton break; 32126e1d1bfcSJamie Gritton } 32136e1d1bfcSJamie Gritton 32146e1d1bfcSJamie Gritton /* Release all the prison locks. */ 3215f7496dcaSJamie Gritton if (flags & PD_LOCKED) 3216f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 32176e1d1bfcSJamie Gritton if (flags & PD_LIST_SLOCKED) 32186e1d1bfcSJamie Gritton sx_sunlock(&allprison_lock); 32196e1d1bfcSJamie Gritton else if (flags & PD_LIST_XLOCKED) 32206e1d1bfcSJamie Gritton sx_xunlock(&allprison_lock); 32216e1d1bfcSJamie Gritton 3222c861373bSJamie Gritton /* Kill any processes attached to a killed prison. */ 32235ecb5444SMateusz Guzik if (killpr != NULL) 32245ecb5444SMateusz Guzik prison_proc_iterate(killpr, prison_kill_processes_cb, NULL); 3225c861373bSJamie Gritton 32266e1d1bfcSJamie Gritton /* 32276e1d1bfcSJamie Gritton * Finish removing any unreferenced prisons, which couldn't happen 32286e1d1bfcSJamie Gritton * while allprison_lock was held (to avoid a LOR on vrele). 32296e1d1bfcSJamie Gritton */ 32306e1d1bfcSJamie Gritton TAILQ_FOREACH_SAFE(rpr, &freeprison, pr_list, tpr) { 32316e1d1bfcSJamie Gritton #ifdef VIMAGE 32326e1d1bfcSJamie Gritton if (rpr->pr_vnet != rpr->pr_parent->pr_vnet) 32336e1d1bfcSJamie Gritton vnet_destroy(rpr->pr_vnet); 32346e1d1bfcSJamie Gritton #endif 32356e1d1bfcSJamie Gritton if (rpr->pr_root != NULL) 32366e1d1bfcSJamie Gritton vrele(rpr->pr_root); 32376e1d1bfcSJamie Gritton mtx_destroy(&rpr->pr_mtx); 32386e1d1bfcSJamie Gritton #ifdef INET 3239eb8dcdeaSGleb Smirnoff prison_ip_free(rpr->pr_addrs[PR_INET]); 32406e1d1bfcSJamie Gritton #endif 32416e1d1bfcSJamie Gritton #ifdef INET6 3242eb8dcdeaSGleb Smirnoff prison_ip_free(rpr->pr_addrs[PR_INET6]); 32436e1d1bfcSJamie Gritton #endif 32446e1d1bfcSJamie Gritton if (rpr->pr_cpuset != NULL) 32456e1d1bfcSJamie Gritton cpuset_rel(rpr->pr_cpuset); 32466e1d1bfcSJamie Gritton osd_jail_exit(rpr); 32476e1d1bfcSJamie Gritton #ifdef RACCT 32486e1d1bfcSJamie Gritton if (racct_enable) 32496e1d1bfcSJamie Gritton prison_racct_detach(rpr); 32506e1d1bfcSJamie Gritton #endif 3251f7496dcaSJamie Gritton TAILQ_REMOVE(&freeprison, rpr, pr_list); 32526e1d1bfcSJamie Gritton free(rpr, M_PRISON); 32530304c731SJamie Gritton } 3254b3059e09SRobert Watson } 3255b3059e09SRobert Watson 3256413628a7SBjoern A. Zeeb /* 3257c861373bSJamie Gritton * Kill the prison and its descendants. Mark them as dying, clear the 3258c861373bSJamie Gritton * persist flag, and call module remove methods. 3259c861373bSJamie Gritton */ 3260c861373bSJamie Gritton static void 3261c861373bSJamie Gritton prison_deref_kill(struct prison *pr, struct prisonlist *freeprison) 3262c861373bSJamie Gritton { 3263c861373bSJamie Gritton struct prison *cpr, *ppr, *rpr; 3264c861373bSJamie Gritton bool descend; 3265c861373bSJamie Gritton 3266c861373bSJamie Gritton /* 3267c861373bSJamie Gritton * Unlike the descendants, the target prison can be killed 3268c861373bSJamie Gritton * even if it is currently dying. This is useful for failed 3269c861373bSJamie Gritton * creation in jail_set(2). 3270c861373bSJamie Gritton */ 3271c861373bSJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 3272c861373bSJamie Gritton ("Trying to kill dead prison %p (jid=%d).", 3273c861373bSJamie Gritton pr, pr->pr_id)); 3274c861373bSJamie Gritton refcount_acquire(&pr->pr_uref); 3275c861373bSJamie Gritton pr->pr_state = PRISON_STATE_DYING; 3276c861373bSJamie Gritton mtx_unlock(&pr->pr_mtx); 3277c861373bSJamie Gritton 3278c861373bSJamie Gritton rpr = NULL; 3279c861373bSJamie Gritton FOREACH_PRISON_DESCENDANT_PRE_POST(pr, cpr, descend) { 3280c861373bSJamie Gritton if (descend) { 3281c861373bSJamie Gritton if (!prison_isalive(cpr)) { 3282c861373bSJamie Gritton descend = false; 3283c861373bSJamie Gritton continue; 3284c861373bSJamie Gritton } 3285c861373bSJamie Gritton prison_hold(cpr); 3286c861373bSJamie Gritton prison_proc_hold(cpr); 3287c861373bSJamie Gritton mtx_lock(&cpr->pr_mtx); 3288c861373bSJamie Gritton cpr->pr_state = PRISON_STATE_DYING; 3289c861373bSJamie Gritton cpr->pr_flags |= PR_REMOVE; 3290c861373bSJamie Gritton mtx_unlock(&cpr->pr_mtx); 3291c861373bSJamie Gritton continue; 3292c861373bSJamie Gritton } 3293c861373bSJamie Gritton if (!(cpr->pr_flags & PR_REMOVE)) 3294c861373bSJamie Gritton continue; 3295a9f7455cSJamie Gritton prison_cleanup(cpr); 3296c861373bSJamie Gritton mtx_lock(&cpr->pr_mtx); 3297c861373bSJamie Gritton cpr->pr_flags &= ~PR_REMOVE; 3298c861373bSJamie Gritton if (cpr->pr_flags & PR_PERSIST) { 3299c861373bSJamie Gritton cpr->pr_flags &= ~PR_PERSIST; 3300c861373bSJamie Gritton prison_proc_free_not_last(cpr); 3301c861373bSJamie Gritton prison_free_not_last(cpr); 3302c861373bSJamie Gritton } 3303c861373bSJamie Gritton (void)refcount_release(&cpr->pr_uref); 3304c861373bSJamie Gritton if (refcount_release(&cpr->pr_ref)) { 3305c861373bSJamie Gritton /* 3306c861373bSJamie Gritton * When the last reference goes, unlink the prison 3307c861373bSJamie Gritton * and set it aside for prison_deref() to handle. 3308c861373bSJamie Gritton * Delay unlinking the sibling list to keep the loop 3309c861373bSJamie Gritton * safe. 3310c861373bSJamie Gritton */ 3311c861373bSJamie Gritton if (rpr != NULL) 3312c861373bSJamie Gritton LIST_REMOVE(rpr, pr_sibling); 3313c861373bSJamie Gritton rpr = cpr; 3314c861373bSJamie Gritton rpr->pr_state = PRISON_STATE_INVALID; 3315c861373bSJamie Gritton TAILQ_REMOVE(&allprison, rpr, pr_list); 3316c861373bSJamie Gritton TAILQ_INSERT_TAIL(freeprison, rpr, pr_list); 3317c861373bSJamie Gritton /* 3318c861373bSJamie Gritton * Removing a prison frees references from its parent. 3319c861373bSJamie Gritton */ 3320c861373bSJamie Gritton ppr = rpr->pr_parent; 3321c861373bSJamie Gritton prison_proc_free_not_last(ppr); 3322c861373bSJamie Gritton prison_free_not_last(ppr); 3323c861373bSJamie Gritton for (; ppr != NULL; ppr = ppr->pr_parent) 3324c861373bSJamie Gritton ppr->pr_childcount--; 3325c861373bSJamie Gritton } 3326c861373bSJamie Gritton mtx_unlock(&cpr->pr_mtx); 3327c861373bSJamie Gritton } 3328c861373bSJamie Gritton if (rpr != NULL) 3329c861373bSJamie Gritton LIST_REMOVE(rpr, pr_sibling); 3330c861373bSJamie Gritton 3331a9f7455cSJamie Gritton prison_cleanup(pr); 3332c861373bSJamie Gritton mtx_lock(&pr->pr_mtx); 3333c861373bSJamie Gritton if (pr->pr_flags & PR_PERSIST) { 3334c861373bSJamie Gritton pr->pr_flags &= ~PR_PERSIST; 3335c861373bSJamie Gritton prison_proc_free_not_last(pr); 3336c861373bSJamie Gritton prison_free_not_last(pr); 3337c861373bSJamie Gritton } 3338c861373bSJamie Gritton (void)refcount_release(&pr->pr_uref); 3339c861373bSJamie Gritton } 3340c861373bSJamie Gritton 3341c861373bSJamie Gritton /* 3342f7496dcaSJamie Gritton * Given the current locking state in the flags, make sure allprison_lock 3343f7496dcaSJamie Gritton * is held exclusive, and the prison is locked. Return flags indicating 3344f7496dcaSJamie Gritton * the new state. 3345f7496dcaSJamie Gritton */ 3346f7496dcaSJamie Gritton static int 3347f7496dcaSJamie Gritton prison_lock_xlock(struct prison *pr, int flags) 3348f7496dcaSJamie Gritton { 3349f7496dcaSJamie Gritton 3350f7496dcaSJamie Gritton if (!(flags & PD_LIST_XLOCKED)) { 3351f7496dcaSJamie Gritton /* 3352f7496dcaSJamie Gritton * Get allprison_lock, which may be an upgrade, 3353f7496dcaSJamie Gritton * and may require unlocking the prison. 3354f7496dcaSJamie Gritton */ 3355f7496dcaSJamie Gritton if (flags & PD_LOCKED) { 3356f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 3357f7496dcaSJamie Gritton flags &= ~PD_LOCKED; 3358f7496dcaSJamie Gritton } 3359f7496dcaSJamie Gritton if (flags & PD_LIST_SLOCKED) { 3360f7496dcaSJamie Gritton if (!sx_try_upgrade(&allprison_lock)) { 3361f7496dcaSJamie Gritton sx_sunlock(&allprison_lock); 3362f7496dcaSJamie Gritton sx_xlock(&allprison_lock); 3363f7496dcaSJamie Gritton } 3364f7496dcaSJamie Gritton flags &= ~PD_LIST_SLOCKED; 3365f7496dcaSJamie Gritton } else 3366f7496dcaSJamie Gritton sx_xlock(&allprison_lock); 3367f7496dcaSJamie Gritton flags |= PD_LIST_XLOCKED; 3368f7496dcaSJamie Gritton } 3369f7496dcaSJamie Gritton if (!(flags & PD_LOCKED)) { 3370f7496dcaSJamie Gritton /* Lock the prison mutex. */ 3371f7496dcaSJamie Gritton mtx_lock(&pr->pr_mtx); 3372f7496dcaSJamie Gritton flags |= PD_LOCKED; 3373f7496dcaSJamie Gritton } 3374f7496dcaSJamie Gritton return flags; 3375f7496dcaSJamie Gritton } 3376f7496dcaSJamie Gritton 3377f7496dcaSJamie Gritton /* 3378a9f7455cSJamie Gritton * Release a prison's resources when it starts dying (when the last user 3379a9f7455cSJamie Gritton * reference is dropped, or when it is killed). 3380a9f7455cSJamie Gritton */ 3381a9f7455cSJamie Gritton static void 3382a9f7455cSJamie Gritton prison_cleanup(struct prison *pr) 3383a9f7455cSJamie Gritton { 3384a9f7455cSJamie Gritton sx_assert(&allprison_lock, SA_XLOCKED); 3385a9f7455cSJamie Gritton mtx_assert(&pr->pr_mtx, MA_NOTOWNED); 338688175af8SRick Macklem vfs_exjail_delete(pr); 33877060da62SJamie Gritton shm_remove_prison(pr); 3388a9f7455cSJamie Gritton (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL); 3389a9f7455cSJamie Gritton } 3390a9f7455cSJamie Gritton 3391a9f7455cSJamie Gritton /* 33920fe74ae6SJamie Gritton * Set or clear a permission bit in the pr_allow field, passing restrictions 33930fe74ae6SJamie Gritton * (cleared permission) down to child jails. 33940fe74ae6SJamie Gritton */ 33950fe74ae6SJamie Gritton void 33960fe74ae6SJamie Gritton prison_set_allow(struct ucred *cred, unsigned flag, int enable) 33970fe74ae6SJamie Gritton { 33980fe74ae6SJamie Gritton struct prison *pr; 33990fe74ae6SJamie Gritton 34000fe74ae6SJamie Gritton pr = cred->cr_prison; 34010fe74ae6SJamie Gritton sx_slock(&allprison_lock); 34020fe74ae6SJamie Gritton mtx_lock(&pr->pr_mtx); 34030fe74ae6SJamie Gritton prison_set_allow_locked(pr, flag, enable); 34040fe74ae6SJamie Gritton mtx_unlock(&pr->pr_mtx); 34050fe74ae6SJamie Gritton sx_sunlock(&allprison_lock); 34060fe74ae6SJamie Gritton } 34070fe74ae6SJamie Gritton 34080fe74ae6SJamie Gritton static void 34090fe74ae6SJamie Gritton prison_set_allow_locked(struct prison *pr, unsigned flag, int enable) 34100fe74ae6SJamie Gritton { 34110fe74ae6SJamie Gritton struct prison *cpr; 34120fe74ae6SJamie Gritton int descend; 34130fe74ae6SJamie Gritton 34140fe74ae6SJamie Gritton if (enable != 0) 34150fe74ae6SJamie Gritton pr->pr_allow |= flag; 34160fe74ae6SJamie Gritton else { 34170fe74ae6SJamie Gritton pr->pr_allow &= ~flag; 34180fe74ae6SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, cpr, descend) 34190fe74ae6SJamie Gritton cpr->pr_allow &= ~flag; 34200fe74ae6SJamie Gritton } 34210fe74ae6SJamie Gritton } 34220fe74ae6SJamie Gritton 34230fe74ae6SJamie Gritton /* 3424ca04ba64SJamie Gritton * Check if a jail supports the given address family. 3425ca04ba64SJamie Gritton * 3426ca04ba64SJamie Gritton * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT 3427ca04ba64SJamie Gritton * if not. 3428ca04ba64SJamie Gritton */ 3429ca04ba64SJamie Gritton int 3430ca04ba64SJamie Gritton prison_check_af(struct ucred *cred, int af) 3431ca04ba64SJamie Gritton { 34320304c731SJamie Gritton struct prison *pr; 3433ca04ba64SJamie Gritton int error; 3434ca04ba64SJamie Gritton 3435ca04ba64SJamie Gritton KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3436ca04ba64SJamie Gritton 34370304c731SJamie Gritton pr = cred->cr_prison; 3438499650a0SJamie Gritton #ifdef VIMAGE 34396bb79563SJamie Gritton /* Prisons with their own network stack are not limited. */ 3440de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 34416bb79563SJamie Gritton return (0); 3442499650a0SJamie Gritton #endif 34436bb79563SJamie Gritton 3444ca04ba64SJamie Gritton error = 0; 3445ca04ba64SJamie Gritton switch (af) 3446ca04ba64SJamie Gritton { 3447ca04ba64SJamie Gritton #ifdef INET 3448ca04ba64SJamie Gritton case AF_INET: 34490304c731SJamie Gritton if (pr->pr_flags & PR_IP4) 34500304c731SJamie Gritton { 34510304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 3452eb8dcdeaSGleb Smirnoff if ((pr->pr_flags & PR_IP4) && 3453eb8dcdeaSGleb Smirnoff pr->pr_addrs[PR_INET] == NULL) 3454ca04ba64SJamie Gritton error = EAFNOSUPPORT; 34550304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 34560304c731SJamie Gritton } 3457ca04ba64SJamie Gritton break; 3458ca04ba64SJamie Gritton #endif 3459ca04ba64SJamie Gritton #ifdef INET6 3460ca04ba64SJamie Gritton case AF_INET6: 34610304c731SJamie Gritton if (pr->pr_flags & PR_IP6) 34620304c731SJamie Gritton { 34630304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 3464eb8dcdeaSGleb Smirnoff if ((pr->pr_flags & PR_IP6) && 3465eb8dcdeaSGleb Smirnoff pr->pr_addrs[PR_INET6] == NULL) 3466ca04ba64SJamie Gritton error = EAFNOSUPPORT; 34670304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 34680304c731SJamie Gritton } 3469ca04ba64SJamie Gritton break; 3470ca04ba64SJamie Gritton #endif 3471ca04ba64SJamie Gritton case AF_LOCAL: 3472ca04ba64SJamie Gritton case AF_ROUTE: 347304f75b98SAlexander V. Chernikov case AF_NETLINK: 3474ca04ba64SJamie Gritton break; 3475ca04ba64SJamie Gritton default: 34760304c731SJamie Gritton if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF)) 3477ca04ba64SJamie Gritton error = EAFNOSUPPORT; 3478ca04ba64SJamie Gritton } 3479ca04ba64SJamie Gritton return (error); 3480ca04ba64SJamie Gritton } 3481ca04ba64SJamie Gritton 3482ca04ba64SJamie Gritton /* 3483413628a7SBjoern A. Zeeb * Check if given address belongs to the jail referenced by cred (wrapper to 3484413628a7SBjoern A. Zeeb * prison_check_ip[46]). 3485413628a7SBjoern A. Zeeb * 34860304c731SJamie Gritton * Returns 0 if jail doesn't restrict the address family or if address belongs 34870304c731SJamie Gritton * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if 34880304c731SJamie Gritton * the jail doesn't allow the address family. IPv4 Address passed in in NBO. 3489413628a7SBjoern A. Zeeb */ 3490413628a7SBjoern A. Zeeb int 3491c83dda36SAlexander V. Chernikov prison_if(struct ucred *cred, const struct sockaddr *sa) 349275c13541SPoul-Henning Kamp { 3493413628a7SBjoern A. Zeeb #ifdef INET 3494c83dda36SAlexander V. Chernikov const struct sockaddr_in *sai; 3495413628a7SBjoern A. Zeeb #endif 3496413628a7SBjoern A. Zeeb #ifdef INET6 3497c83dda36SAlexander V. Chernikov const struct sockaddr_in6 *sai6; 3498413628a7SBjoern A. Zeeb #endif 3499b89e82ddSJamie Gritton int error; 350075c13541SPoul-Henning Kamp 3501413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3502413628a7SBjoern A. Zeeb KASSERT(sa != NULL, ("%s: sa is NULL", __func__)); 3503413628a7SBjoern A. Zeeb 3504de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE 3505de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 3506de0bd6f7SBjoern A. Zeeb return (0); 3507de0bd6f7SBjoern A. Zeeb #endif 3508de0bd6f7SBjoern A. Zeeb 3509b89e82ddSJamie Gritton error = 0; 3510413628a7SBjoern A. Zeeb switch (sa->sa_family) 3511413628a7SBjoern A. Zeeb { 3512413628a7SBjoern A. Zeeb #ifdef INET 3513413628a7SBjoern A. Zeeb case AF_INET: 3514c83dda36SAlexander V. Chernikov sai = (const struct sockaddr_in *)sa; 3515b89e82ddSJamie Gritton error = prison_check_ip4(cred, &sai->sin_addr); 3516413628a7SBjoern A. Zeeb break; 3517413628a7SBjoern A. Zeeb #endif 3518413628a7SBjoern A. Zeeb #ifdef INET6 3519413628a7SBjoern A. Zeeb case AF_INET6: 3520c83dda36SAlexander V. Chernikov sai6 = (const struct sockaddr_in6 *)sa; 3521b89e82ddSJamie Gritton error = prison_check_ip6(cred, &sai6->sin6_addr); 3522413628a7SBjoern A. Zeeb break; 3523413628a7SBjoern A. Zeeb #endif 3524413628a7SBjoern A. Zeeb default: 35250304c731SJamie Gritton if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF)) 3526b89e82ddSJamie Gritton error = EAFNOSUPPORT; 3527413628a7SBjoern A. Zeeb } 3528b89e82ddSJamie Gritton return (error); 352975c13541SPoul-Henning Kamp } 353091421ba2SRobert Watson 353191421ba2SRobert Watson /* 353291421ba2SRobert Watson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH. 353391421ba2SRobert Watson */ 353491421ba2SRobert Watson int 35359ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2) 353691421ba2SRobert Watson { 353791421ba2SRobert Watson 35380304c731SJamie Gritton return ((cred1->cr_prison == cred2->cr_prison || 35390304c731SJamie Gritton prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH); 35400304c731SJamie Gritton } 354191421ba2SRobert Watson 35420304c731SJamie Gritton /* 3543bba7a2e8SRick Macklem * For mountd/nfsd to run within a prison, it must be: 3544bba7a2e8SRick Macklem * - A vnet prison. 3545bba7a2e8SRick Macklem * - PR_ALLOW_NFSD must be set on it. 3546bba7a2e8SRick Macklem * - The root directory (pr_root) of the prison must be 3547bba7a2e8SRick Macklem * a file system mount point, so the mountd can hang 3548bba7a2e8SRick Macklem * export information on it. 354999187c3aSRick Macklem * - The prison's enforce_statfs cannot be 0, so that 355099187c3aSRick Macklem * mountd(8) can do exports. 3551bba7a2e8SRick Macklem */ 3552bba7a2e8SRick Macklem bool 3553bba7a2e8SRick Macklem prison_check_nfsd(struct ucred *cred) 3554bba7a2e8SRick Macklem { 3555bba7a2e8SRick Macklem 3556bba7a2e8SRick Macklem if (jailed_without_vnet(cred)) 3557bba7a2e8SRick Macklem return (false); 3558bba7a2e8SRick Macklem if (!prison_allow(cred, PR_ALLOW_NFSD)) 3559bba7a2e8SRick Macklem return (false); 3560bba7a2e8SRick Macklem if ((cred->cr_prison->pr_root->v_vflag & VV_ROOT) == 0) 3561bba7a2e8SRick Macklem return (false); 356299187c3aSRick Macklem if (cred->cr_prison->pr_enforce_statfs == 0) 356399187c3aSRick Macklem return (false); 3564bba7a2e8SRick Macklem return (true); 3565bba7a2e8SRick Macklem } 3566bba7a2e8SRick Macklem 3567bba7a2e8SRick Macklem /* 35680b0ae2e4SMina Galić * Return true if p2 is a child of p1, otherwise false. 35690304c731SJamie Gritton */ 35700b0ae2e4SMina Galić bool 35710304c731SJamie Gritton prison_ischild(struct prison *pr1, struct prison *pr2) 35720304c731SJamie Gritton { 35730304c731SJamie Gritton 35740304c731SJamie Gritton for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent) 35750304c731SJamie Gritton if (pr1 == pr2) 35760b0ae2e4SMina Galić return (true); 35770b0ae2e4SMina Galić return (false); 357891421ba2SRobert Watson } 357991421ba2SRobert Watson 358091421ba2SRobert Watson /* 3581f7496dcaSJamie Gritton * Return true if the prison is currently alive. A prison is alive if it 3582f7496dcaSJamie Gritton * holds user references and it isn't being removed. 358376ad42abSJamie Gritton */ 358476ad42abSJamie Gritton bool 3585eb8dcdeaSGleb Smirnoff prison_isalive(const struct prison *pr) 358676ad42abSJamie Gritton { 358776ad42abSJamie Gritton 35881158508aSJamie Gritton if (__predict_false(pr->pr_state != PRISON_STATE_ALIVE)) 3589cc7b7306SJamie Gritton return (false); 359076ad42abSJamie Gritton return (true); 359176ad42abSJamie Gritton } 359276ad42abSJamie Gritton 359376ad42abSJamie Gritton /* 359476ad42abSJamie Gritton * Return true if the prison is currently valid. A prison is valid if it has 359576ad42abSJamie Gritton * been fully created, and is not being destroyed. Note that dying prisons 3596f7496dcaSJamie Gritton * are still considered valid. Invalid prisons won't be found under normal 3597f7496dcaSJamie Gritton * circumstances, as they're only put in that state by functions that have 3598f7496dcaSJamie Gritton * an exclusive hold on allprison_lock. 359976ad42abSJamie Gritton */ 360076ad42abSJamie Gritton bool 360176ad42abSJamie Gritton prison_isvalid(struct prison *pr) 360276ad42abSJamie Gritton { 360376ad42abSJamie Gritton 36041158508aSJamie Gritton if (__predict_false(pr->pr_state == PRISON_STATE_INVALID)) 36051158508aSJamie Gritton return (false); 36066754ae25SJamie Gritton if (__predict_false(refcount_load(&pr->pr_ref) == 0)) 360776ad42abSJamie Gritton return (false); 360876ad42abSJamie Gritton return (true); 360976ad42abSJamie Gritton } 361076ad42abSJamie Gritton 361176ad42abSJamie Gritton /* 36120b0ae2e4SMina Galić * Return true if the passed credential is in a jail and that jail does not 36130b0ae2e4SMina Galić * have its own virtual network stack, otherwise false. 3614de0bd6f7SBjoern A. Zeeb */ 36150b0ae2e4SMina Galić bool 3616de0bd6f7SBjoern A. Zeeb jailed_without_vnet(struct ucred *cred) 3617de0bd6f7SBjoern A. Zeeb { 3618de0bd6f7SBjoern A. Zeeb 3619de0bd6f7SBjoern A. Zeeb if (!jailed(cred)) 36200b0ae2e4SMina Galić return (false); 3621de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE 3622de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 36230b0ae2e4SMina Galić return (false); 3624de0bd6f7SBjoern A. Zeeb #endif 3625de0bd6f7SBjoern A. Zeeb 36260b0ae2e4SMina Galić return (true); 3627de0bd6f7SBjoern A. Zeeb } 3628de0bd6f7SBjoern A. Zeeb 3629de0bd6f7SBjoern A. Zeeb /* 36307455b100SJamie Gritton * Return the correct hostname (domainname, et al) for the passed credential. 36319484d0c0SRobert Drehmel */ 3632ad1ff099SRobert Drehmel void 36339ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size) 36349484d0c0SRobert Drehmel { 363576ca6f88SJamie Gritton struct prison *pr; 36369484d0c0SRobert Drehmel 36377455b100SJamie Gritton /* 36387455b100SJamie Gritton * A NULL credential can be used to shortcut to the physical 36397455b100SJamie Gritton * system's hostname. 36407455b100SJamie Gritton */ 364176ca6f88SJamie Gritton pr = (cred != NULL) ? cred->cr_prison : &prison0; 364276ca6f88SJamie Gritton mtx_lock(&pr->pr_mtx); 3643c1f19219SJamie Gritton strlcpy(buf, pr->pr_hostname, size); 364476ca6f88SJamie Gritton mtx_unlock(&pr->pr_mtx); 36459484d0c0SRobert Drehmel } 3646fd7a8150SMike Barcroft 36477455b100SJamie Gritton void 36487455b100SJamie Gritton getcreddomainname(struct ucred *cred, char *buf, size_t size) 36497455b100SJamie Gritton { 36507455b100SJamie Gritton 36517455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 3652c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_domainname, size); 36537455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 36547455b100SJamie Gritton } 36557455b100SJamie Gritton 36567455b100SJamie Gritton void 36577455b100SJamie Gritton getcredhostuuid(struct ucred *cred, char *buf, size_t size) 36587455b100SJamie Gritton { 36597455b100SJamie Gritton 36607455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 3661c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_hostuuid, size); 36627455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 36637455b100SJamie Gritton } 36647455b100SJamie Gritton 36657455b100SJamie Gritton void 36667455b100SJamie Gritton getcredhostid(struct ucred *cred, unsigned long *hostid) 36677455b100SJamie Gritton { 36687455b100SJamie Gritton 36697455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 36707455b100SJamie Gritton *hostid = cred->cr_prison->pr_hostid; 36717455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 36727455b100SJamie Gritton } 36737455b100SJamie Gritton 36743f8bc99cSKristof Provost void 36753f8bc99cSKristof Provost getjailname(struct ucred *cred, char *name, size_t len) 36763f8bc99cSKristof Provost { 36773f8bc99cSKristof Provost 36783f8bc99cSKristof Provost mtx_lock(&cred->cr_prison->pr_mtx); 36793f8bc99cSKristof Provost strlcpy(name, cred->cr_prison->pr_name, len); 36803f8bc99cSKristof Provost mtx_unlock(&cred->cr_prison->pr_mtx); 36813f8bc99cSKristof Provost } 36823f8bc99cSKristof Provost 3683eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE 3684eb79e1c7SBjoern A. Zeeb /* 3685eb79e1c7SBjoern A. Zeeb * Determine whether the prison represented by cred owns 3686eb79e1c7SBjoern A. Zeeb * its vnet rather than having it inherited. 3687eb79e1c7SBjoern A. Zeeb * 36880b0ae2e4SMina Galić * Returns true in case the prison owns the vnet, false otherwise. 3689eb79e1c7SBjoern A. Zeeb */ 36900b0ae2e4SMina Galić bool 3691eb79e1c7SBjoern A. Zeeb prison_owns_vnet(struct ucred *cred) 3692eb79e1c7SBjoern A. Zeeb { 3693eb79e1c7SBjoern A. Zeeb 3694eb79e1c7SBjoern A. Zeeb /* 3695eb79e1c7SBjoern A. Zeeb * vnets cannot be added/removed after jail creation, 3696eb79e1c7SBjoern A. Zeeb * so no need to lock here. 3697eb79e1c7SBjoern A. Zeeb */ 36980b0ae2e4SMina Galić return ((cred->cr_prison->pr_flags & PR_VNET) != 0); 3699eb79e1c7SBjoern A. Zeeb } 3700eb79e1c7SBjoern A. Zeeb #endif 3701eb79e1c7SBjoern A. Zeeb 3702f08df373SRobert Watson /* 3703820a0de9SPawel Jakub Dawidek * Determine whether the subject represented by cred can "see" 3704820a0de9SPawel Jakub Dawidek * status of a mount point. 3705820a0de9SPawel Jakub Dawidek * Returns: 0 for permitted, ENOENT otherwise. 3706820a0de9SPawel Jakub Dawidek * XXX: This function should be called cr_canseemount() and should be 3707820a0de9SPawel Jakub Dawidek * placed in kern_prot.c. 3708f08df373SRobert Watson */ 3709f08df373SRobert Watson int 3710820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp) 3711f08df373SRobert Watson { 3712820a0de9SPawel Jakub Dawidek struct prison *pr; 3713820a0de9SPawel Jakub Dawidek struct statfs *sp; 3714820a0de9SPawel Jakub Dawidek size_t len; 3715f08df373SRobert Watson 3716820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 37170304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 37180304c731SJamie Gritton return (0); 3719820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) 3720820a0de9SPawel Jakub Dawidek return (0); 37210304c731SJamie Gritton if (pr->pr_enforce_statfs == 2) 3722820a0de9SPawel Jakub Dawidek return (ENOENT); 3723820a0de9SPawel Jakub Dawidek /* 3724820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 3725820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 3726820a0de9SPawel Jakub Dawidek * This is ugly check, but this is the only situation when jail's 3727820a0de9SPawel Jakub Dawidek * directory ends with '/'. 3728820a0de9SPawel Jakub Dawidek */ 3729820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 3730820a0de9SPawel Jakub Dawidek return (0); 3731820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 3732820a0de9SPawel Jakub Dawidek sp = &mp->mnt_stat; 3733820a0de9SPawel Jakub Dawidek if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0) 3734820a0de9SPawel Jakub Dawidek return (ENOENT); 3735820a0de9SPawel Jakub Dawidek /* 3736820a0de9SPawel Jakub Dawidek * Be sure that we don't have situation where jail's root directory 3737820a0de9SPawel Jakub Dawidek * is "/some/path" and mount point is "/some/pathpath". 3738820a0de9SPawel Jakub Dawidek */ 3739820a0de9SPawel Jakub Dawidek if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/') 3740820a0de9SPawel Jakub Dawidek return (ENOENT); 3741f08df373SRobert Watson return (0); 3742f08df373SRobert Watson } 3743820a0de9SPawel Jakub Dawidek 3744820a0de9SPawel Jakub Dawidek void 3745820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp) 3746820a0de9SPawel Jakub Dawidek { 3747820a0de9SPawel Jakub Dawidek char jpath[MAXPATHLEN]; 3748820a0de9SPawel Jakub Dawidek struct prison *pr; 3749820a0de9SPawel Jakub Dawidek size_t len; 3750820a0de9SPawel Jakub Dawidek 3751820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 37520304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 37530304c731SJamie Gritton return; 3754820a0de9SPawel Jakub Dawidek if (prison_canseemount(cred, mp) != 0) { 3755820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3756820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, "[restricted]", 3757820a0de9SPawel Jakub Dawidek sizeof(sp->f_mntonname)); 3758820a0de9SPawel Jakub Dawidek return; 3759820a0de9SPawel Jakub Dawidek } 3760820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) { 3761820a0de9SPawel Jakub Dawidek /* 3762820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3763820a0de9SPawel Jakub Dawidek * the valid path left there. 3764820a0de9SPawel Jakub Dawidek */ 3765820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3766820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3767820a0de9SPawel Jakub Dawidek return; 3768820a0de9SPawel Jakub Dawidek } 3769820a0de9SPawel Jakub Dawidek /* 3770820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 3771820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 3772820a0de9SPawel Jakub Dawidek */ 3773820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 3774820a0de9SPawel Jakub Dawidek return; 3775820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 3776820a0de9SPawel Jakub Dawidek strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath)); 3777820a0de9SPawel Jakub Dawidek /* 3778820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3779820a0de9SPawel Jakub Dawidek * the valid path left there. 3780820a0de9SPawel Jakub Dawidek */ 3781820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3782820a0de9SPawel Jakub Dawidek if (*jpath == '\0') { 3783820a0de9SPawel Jakub Dawidek /* Should never happen. */ 3784820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3785820a0de9SPawel Jakub Dawidek } else { 3786820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname)); 3787820a0de9SPawel Jakub Dawidek } 3788f08df373SRobert Watson } 3789f08df373SRobert Watson 3790800c9408SRobert Watson /* 3791800c9408SRobert Watson * Check with permission for a specific privilege is granted within jail. We 3792800c9408SRobert Watson * have a specific list of accepted privileges; the rest are denied. 3793800c9408SRobert Watson */ 3794800c9408SRobert Watson int 3795800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv) 3796800c9408SRobert Watson { 37970fe74ae6SJamie Gritton struct prison *pr; 37980fe74ae6SJamie Gritton int error; 3799800c9408SRobert Watson 38007b2ff0dcSMateusz Guzik /* 38017b2ff0dcSMateusz Guzik * Some policies have custom handlers. This routine should not be 38027b2ff0dcSMateusz Guzik * called for them. See priv_check_cred(). 38037b2ff0dcSMateusz Guzik */ 38047b2ff0dcSMateusz Guzik switch (priv) { 3805a459a6cfSMateusz Guzik case PRIV_VFS_LOOKUP: 38067b2ff0dcSMateusz Guzik case PRIV_VFS_GENERATION: 38077b2ff0dcSMateusz Guzik KASSERT(0, ("prison_priv_check instead of a custom handler " 38087b2ff0dcSMateusz Guzik "called for %d\n", priv)); 38097b2ff0dcSMateusz Guzik } 38107b2ff0dcSMateusz Guzik 3811800c9408SRobert Watson if (!jailed(cred)) 3812800c9408SRobert Watson return (0); 3813800c9408SRobert Watson 38146bb79563SJamie Gritton #ifdef VIMAGE 38156bb79563SJamie Gritton /* 38166bb79563SJamie Gritton * Privileges specific to prisons with a virtual network stack. 38176bb79563SJamie Gritton * There might be a duplicate entry here in case the privilege 38186bb79563SJamie Gritton * is only granted conditionally in the legacy jail case. 38196bb79563SJamie Gritton */ 38206bb79563SJamie Gritton switch (priv) { 38216bb79563SJamie Gritton /* 38226bb79563SJamie Gritton * NFS-specific privileges. 38236bb79563SJamie Gritton */ 38246bb79563SJamie Gritton case PRIV_NFS_DAEMON: 3825bba7a2e8SRick Macklem case PRIV_VFS_GETFH: 3826bba7a2e8SRick Macklem case PRIV_VFS_MOUNT_EXPORTED: 3827bba7a2e8SRick Macklem if (!prison_check_nfsd(cred)) 3828bba7a2e8SRick Macklem return (EPERM); 3829bba7a2e8SRick Macklem #ifdef notyet 38306bb79563SJamie Gritton case PRIV_NFS_LOCKD: 38316bb79563SJamie Gritton #endif 38326bb79563SJamie Gritton /* 38336bb79563SJamie Gritton * Network stack privileges. 38346bb79563SJamie Gritton */ 38356bb79563SJamie Gritton case PRIV_NET_BRIDGE: 38366bb79563SJamie Gritton case PRIV_NET_GRE: 38376bb79563SJamie Gritton case PRIV_NET_BPF: 38386bb79563SJamie Gritton case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */ 38396bb79563SJamie Gritton case PRIV_NET_ROUTE: 38406bb79563SJamie Gritton case PRIV_NET_TAP: 38416bb79563SJamie Gritton case PRIV_NET_SETIFMTU: 38426bb79563SJamie Gritton case PRIV_NET_SETIFFLAGS: 38436bb79563SJamie Gritton case PRIV_NET_SETIFCAP: 3844215940b3SXin LI case PRIV_NET_SETIFDESCR: 38456bb79563SJamie Gritton case PRIV_NET_SETIFNAME : 38466bb79563SJamie Gritton case PRIV_NET_SETIFMETRIC: 38476bb79563SJamie Gritton case PRIV_NET_SETIFPHYS: 38486bb79563SJamie Gritton case PRIV_NET_SETIFMAC: 3849389474c1SKonstantin Belousov case PRIV_NET_SETLANPCP: 38506bb79563SJamie Gritton case PRIV_NET_ADDMULTI: 38516bb79563SJamie Gritton case PRIV_NET_DELMULTI: 38526bb79563SJamie Gritton case PRIV_NET_HWIOCTL: 38536bb79563SJamie Gritton case PRIV_NET_SETLLADDR: 38546bb79563SJamie Gritton case PRIV_NET_ADDIFGROUP: 38556bb79563SJamie Gritton case PRIV_NET_DELIFGROUP: 38566bb79563SJamie Gritton case PRIV_NET_IFCREATE: 38576bb79563SJamie Gritton case PRIV_NET_IFDESTROY: 38586bb79563SJamie Gritton case PRIV_NET_ADDIFADDR: 38596bb79563SJamie Gritton case PRIV_NET_DELIFADDR: 38606bb79563SJamie Gritton case PRIV_NET_LAGG: 38616bb79563SJamie Gritton case PRIV_NET_GIF: 38626bb79563SJamie Gritton case PRIV_NET_SETIFVNET: 386335fd7bc0SBjoern A. Zeeb case PRIV_NET_SETIFFIB: 3864ab91feabSKristof Provost case PRIV_NET_OVPN: 386543f8c763SZhenlei Huang case PRIV_NET_ME: 3866744bfb21SJohn Baldwin case PRIV_NET_WG: 38676bb79563SJamie Gritton 38686bb79563SJamie Gritton /* 38696bb79563SJamie Gritton * 802.11-related privileges. 38706bb79563SJamie Gritton */ 3871f7d38a13SAdrian Chadd case PRIV_NET80211_VAP_GETKEY: 3872f7d38a13SAdrian Chadd case PRIV_NET80211_VAP_MANAGE: 38736bb79563SJamie Gritton 38746bb79563SJamie Gritton #ifdef notyet 38756bb79563SJamie Gritton /* 38766bb79563SJamie Gritton * ATM privileges. 38776bb79563SJamie Gritton */ 38786bb79563SJamie Gritton case PRIV_NETATM_CFG: 38796bb79563SJamie Gritton case PRIV_NETATM_ADD: 38806bb79563SJamie Gritton case PRIV_NETATM_DEL: 38816bb79563SJamie Gritton case PRIV_NETATM_SET: 38826bb79563SJamie Gritton 38836bb79563SJamie Gritton /* 38846bb79563SJamie Gritton * Bluetooth privileges. 38856bb79563SJamie Gritton */ 38866bb79563SJamie Gritton case PRIV_NETBLUETOOTH_RAW: 38876bb79563SJamie Gritton #endif 38886bb79563SJamie Gritton 38896bb79563SJamie Gritton /* 38906bb79563SJamie Gritton * Netgraph and netgraph module privileges. 38916bb79563SJamie Gritton */ 38926bb79563SJamie Gritton case PRIV_NETGRAPH_CONTROL: 38936bb79563SJamie Gritton #ifdef notyet 38946bb79563SJamie Gritton case PRIV_NETGRAPH_TTY: 38956bb79563SJamie Gritton #endif 38966bb79563SJamie Gritton 38976bb79563SJamie Gritton /* 38986bb79563SJamie Gritton * IPv4 and IPv6 privileges. 38996bb79563SJamie Gritton */ 39006bb79563SJamie Gritton case PRIV_NETINET_IPFW: 39016bb79563SJamie Gritton case PRIV_NETINET_DIVERT: 39026bb79563SJamie Gritton case PRIV_NETINET_PF: 39036bb79563SJamie Gritton case PRIV_NETINET_DUMMYNET: 39046bb79563SJamie Gritton case PRIV_NETINET_CARP: 39056bb79563SJamie Gritton case PRIV_NETINET_MROUTE: 39066bb79563SJamie Gritton case PRIV_NETINET_RAW: 39076bb79563SJamie Gritton case PRIV_NETINET_ADDRCTRL6: 39086bb79563SJamie Gritton case PRIV_NETINET_ND6: 39096bb79563SJamie Gritton case PRIV_NETINET_SCOPE6: 39106bb79563SJamie Gritton case PRIV_NETINET_ALIFETIME6: 39116bb79563SJamie Gritton case PRIV_NETINET_IPSEC: 39126bb79563SJamie Gritton case PRIV_NETINET_BINDANY: 39136bb79563SJamie Gritton 39146bb79563SJamie Gritton #ifdef notyet 39156bb79563SJamie Gritton /* 39166bb79563SJamie Gritton * NCP privileges. 39176bb79563SJamie Gritton */ 39186bb79563SJamie Gritton case PRIV_NETNCP: 39196bb79563SJamie Gritton 39206bb79563SJamie Gritton /* 39216bb79563SJamie Gritton * SMB privileges. 39226bb79563SJamie Gritton */ 39236bb79563SJamie Gritton case PRIV_NETSMB: 39246bb79563SJamie Gritton #endif 39256bb79563SJamie Gritton 39266bb79563SJamie Gritton /* 39276bb79563SJamie Gritton * No default: or deny here. 39286bb79563SJamie Gritton * In case of no permit fall through to next switch(). 39296bb79563SJamie Gritton */ 39306bb79563SJamie Gritton if (cred->cr_prison->pr_flags & PR_VNET) 39316bb79563SJamie Gritton return (0); 39326bb79563SJamie Gritton } 39336bb79563SJamie Gritton #endif /* VIMAGE */ 39346bb79563SJamie Gritton 3935800c9408SRobert Watson switch (priv) { 3936800c9408SRobert Watson /* 3937800c9408SRobert Watson * Allow ktrace privileges for root in jail. 3938800c9408SRobert Watson */ 3939800c9408SRobert Watson case PRIV_KTRACE: 3940800c9408SRobert Watson 3941c3c1b5e6SRobert Watson #if 0 3942800c9408SRobert Watson /* 3943800c9408SRobert Watson * Allow jailed processes to configure audit identity and 3944800c9408SRobert Watson * submit audit records (login, etc). In the future we may 3945800c9408SRobert Watson * want to further refine the relationship between audit and 3946800c9408SRobert Watson * jail. 3947800c9408SRobert Watson */ 3948800c9408SRobert Watson case PRIV_AUDIT_GETAUDIT: 3949800c9408SRobert Watson case PRIV_AUDIT_SETAUDIT: 3950800c9408SRobert Watson case PRIV_AUDIT_SUBMIT: 3951c3c1b5e6SRobert Watson #endif 3952800c9408SRobert Watson 3953800c9408SRobert Watson /* 3954800c9408SRobert Watson * Allow jailed processes to manipulate process UNIX 3955800c9408SRobert Watson * credentials in any way they see fit. 3956800c9408SRobert Watson */ 3957800c9408SRobert Watson case PRIV_CRED_SETUID: 3958800c9408SRobert Watson case PRIV_CRED_SETEUID: 3959800c9408SRobert Watson case PRIV_CRED_SETGID: 3960800c9408SRobert Watson case PRIV_CRED_SETEGID: 3961800c9408SRobert Watson case PRIV_CRED_SETGROUPS: 3962800c9408SRobert Watson case PRIV_CRED_SETREUID: 3963800c9408SRobert Watson case PRIV_CRED_SETREGID: 3964800c9408SRobert Watson case PRIV_CRED_SETRESUID: 3965800c9408SRobert Watson case PRIV_CRED_SETRESGID: 3966800c9408SRobert Watson 3967800c9408SRobert Watson /* 3968800c9408SRobert Watson * Jail implements visibility constraints already, so allow 3969800c9408SRobert Watson * jailed root to override uid/gid-based constraints. 3970800c9408SRobert Watson */ 3971800c9408SRobert Watson case PRIV_SEEOTHERGIDS: 3972800c9408SRobert Watson case PRIV_SEEOTHERUIDS: 39737974ca1cSOlivier Certner case PRIV_SEEJAILPROC: 3974800c9408SRobert Watson 3975800c9408SRobert Watson /* 3976800c9408SRobert Watson * Jail implements inter-process debugging limits already, so 3977800c9408SRobert Watson * allow jailed root various debugging privileges. 3978800c9408SRobert Watson */ 3979800c9408SRobert Watson case PRIV_DEBUG_DIFFCRED: 3980800c9408SRobert Watson case PRIV_DEBUG_SUGID: 3981800c9408SRobert Watson case PRIV_DEBUG_UNPRIV: 3982800c9408SRobert Watson 3983800c9408SRobert Watson /* 3984800c9408SRobert Watson * Allow jail to set various resource limits and login 3985800c9408SRobert Watson * properties, and for now, exceed process resource limits. 3986800c9408SRobert Watson */ 3987800c9408SRobert Watson case PRIV_PROC_LIMIT: 3988800c9408SRobert Watson case PRIV_PROC_SETLOGIN: 3989800c9408SRobert Watson case PRIV_PROC_SETRLIMIT: 3990800c9408SRobert Watson 3991800c9408SRobert Watson /* 3992800c9408SRobert Watson * System V and POSIX IPC privileges are granted in jail. 3993800c9408SRobert Watson */ 3994800c9408SRobert Watson case PRIV_IPC_READ: 3995800c9408SRobert Watson case PRIV_IPC_WRITE: 3996800c9408SRobert Watson case PRIV_IPC_ADMIN: 3997800c9408SRobert Watson case PRIV_IPC_MSGSIZE: 3998800c9408SRobert Watson case PRIV_MQ_ADMIN: 3999800c9408SRobert Watson 4000800c9408SRobert Watson /* 40010304c731SJamie Gritton * Jail operations within a jail work on child jails. 40020304c731SJamie Gritton */ 40030304c731SJamie Gritton case PRIV_JAIL_ATTACH: 40040304c731SJamie Gritton case PRIV_JAIL_SET: 40050304c731SJamie Gritton case PRIV_JAIL_REMOVE: 40060304c731SJamie Gritton 40070304c731SJamie Gritton /* 4008800c9408SRobert Watson * Jail implements its own inter-process limits, so allow 4009800c9408SRobert Watson * root processes in jail to change scheduling on other 4010800c9408SRobert Watson * processes in the same jail. Likewise for signalling. 4011800c9408SRobert Watson */ 4012800c9408SRobert Watson case PRIV_SCHED_DIFFCRED: 4013413628a7SBjoern A. Zeeb case PRIV_SCHED_CPUSET: 4014800c9408SRobert Watson case PRIV_SIGNAL_DIFFCRED: 4015800c9408SRobert Watson case PRIV_SIGNAL_SUGID: 4016800c9408SRobert Watson 4017800c9408SRobert Watson /* 4018800c9408SRobert Watson * Allow jailed processes to write to sysctls marked as jail 4019800c9408SRobert Watson * writable. 4020800c9408SRobert Watson */ 4021800c9408SRobert Watson case PRIV_SYSCTL_WRITEJAIL: 4022800c9408SRobert Watson 4023800c9408SRobert Watson /* 4024800c9408SRobert Watson * Allow root in jail to manage a variety of quota 4025e82d0201SRobert Watson * properties. These should likely be conditional on a 4026e82d0201SRobert Watson * configuration option. 4027800c9408SRobert Watson */ 402895b091d2SRobert Watson case PRIV_VFS_GETQUOTA: 402995b091d2SRobert Watson case PRIV_VFS_SETQUOTA: 4030800c9408SRobert Watson 4031800c9408SRobert Watson /* 4032800c9408SRobert Watson * Since Jail relies on chroot() to implement file system 4033800c9408SRobert Watson * protections, grant many VFS privileges to root in jail. 4034800c9408SRobert Watson * Be careful to exclude mount-related and NFS-related 4035800c9408SRobert Watson * privileges. 4036800c9408SRobert Watson */ 4037800c9408SRobert Watson case PRIV_VFS_READ: 4038800c9408SRobert Watson case PRIV_VFS_WRITE: 4039800c9408SRobert Watson case PRIV_VFS_ADMIN: 4040800c9408SRobert Watson case PRIV_VFS_EXEC: 4041800c9408SRobert Watson case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */ 4042800c9408SRobert Watson case PRIV_VFS_CHFLAGS_DEV: 4043800c9408SRobert Watson case PRIV_VFS_CHOWN: 4044800c9408SRobert Watson case PRIV_VFS_CHROOT: 4045bb531912SPawel Jakub Dawidek case PRIV_VFS_RETAINSUGID: 4046800c9408SRobert Watson case PRIV_VFS_FCHROOT: 4047800c9408SRobert Watson case PRIV_VFS_LINK: 4048800c9408SRobert Watson case PRIV_VFS_SETGID: 4049e41966dcSRobert Watson case PRIV_VFS_STAT: 4050800c9408SRobert Watson case PRIV_VFS_STICKYFILE: 4051bb56d716SJamie Gritton 4052bb56d716SJamie Gritton /* 4053bb56d716SJamie Gritton * As in the non-jail case, non-root users are expected to be 405470de1003SGordon Bergling * able to read kernel/physical memory (provided /dev/[k]mem 4055bb56d716SJamie Gritton * exists in the jail and they have permission to access it). 4056bb56d716SJamie Gritton */ 4057bb56d716SJamie Gritton case PRIV_KMEM_READ: 4058800c9408SRobert Watson return (0); 4059800c9408SRobert Watson 4060800c9408SRobert Watson /* 4061800c9408SRobert Watson * Depending on the global setting, allow privilege of 4062800c9408SRobert Watson * setting system flags. 4063800c9408SRobert Watson */ 4064800c9408SRobert Watson case PRIV_VFS_SYSFLAGS: 40650304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS) 4066800c9408SRobert Watson return (0); 4067800c9408SRobert Watson else 4068800c9408SRobert Watson return (EPERM); 4069800c9408SRobert Watson 4070800c9408SRobert Watson /* 4071f3a8d2f9SPawel Jakub Dawidek * Depending on the global setting, allow privilege of 4072f3a8d2f9SPawel Jakub Dawidek * mounting/unmounting file systems. 4073f3a8d2f9SPawel Jakub Dawidek */ 4074f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT: 4075f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_UNMOUNT: 4076f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT_NONUSER: 407724b0502eSPawel Jakub Dawidek case PRIV_VFS_MOUNT_OWNER: 40780fe74ae6SJamie Gritton pr = cred->cr_prison; 40790fe74ae6SJamie Gritton prison_lock(pr); 40800fe74ae6SJamie Gritton if (pr->pr_allow & PR_ALLOW_MOUNT && pr->pr_enforce_statfs < 2) 40810fe74ae6SJamie Gritton error = 0; 4082f3a8d2f9SPawel Jakub Dawidek else 40830fe74ae6SJamie Gritton error = EPERM; 40840fe74ae6SJamie Gritton prison_unlock(pr); 40850fe74ae6SJamie Gritton return (error); 4086f3a8d2f9SPawel Jakub Dawidek 4087f3a8d2f9SPawel Jakub Dawidek /* 408863619b6dSKyle Evans * Jails should hold no disposition on the PRIV_VFS_READ_DIR 408963619b6dSKyle Evans * policy. priv_check_cred will not specifically allow it, and 409063619b6dSKyle Evans * we may want a MAC policy to allow it. 409163619b6dSKyle Evans */ 409263619b6dSKyle Evans case PRIV_VFS_READ_DIR: 409363619b6dSKyle Evans return (0); 409463619b6dSKyle Evans 409563619b6dSKyle Evans /* 4096cb48780dSShawn Webb * Conditionally allow privileged process in the jail to 4097cb48780dSShawn Webb * manipulate filesystem extended attributes in the system 4098cb48780dSShawn Webb * namespace. 4099cb48780dSShawn Webb */ 4100cb48780dSShawn Webb case PRIV_VFS_EXTATTR_SYSTEM: 4101cb48780dSShawn Webb if ((cred->cr_prison->pr_allow & PR_ALLOW_EXTATTR) != 0) 4102cb48780dSShawn Webb return (0); 4103cb48780dSShawn Webb else 4104cb48780dSShawn Webb return (EPERM); 4105cb48780dSShawn Webb 4106cb48780dSShawn Webb /* 4107ccd6ac9fSAntoine Brodin * Conditionnaly allow locking (unlocking) physical pages 4108ccd6ac9fSAntoine Brodin * in memory. 4109ccd6ac9fSAntoine Brodin */ 4110ccd6ac9fSAntoine Brodin case PRIV_VM_MLOCK: 4111ccd6ac9fSAntoine Brodin case PRIV_VM_MUNLOCK: 4112ccd6ac9fSAntoine Brodin if (cred->cr_prison->pr_allow & PR_ALLOW_MLOCK) 4113ccd6ac9fSAntoine Brodin return (0); 4114ccd6ac9fSAntoine Brodin else 4115ccd6ac9fSAntoine Brodin return (EPERM); 4116ccd6ac9fSAntoine Brodin 4117ccd6ac9fSAntoine Brodin /* 4118e28f9b7dSAllan Jude * Conditionally allow jailed root to bind reserved ports. 4119800c9408SRobert Watson */ 4120800c9408SRobert Watson case PRIV_NETINET_RESERVEDPORT: 4121e28f9b7dSAllan Jude if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS) 4122e28f9b7dSAllan Jude return (0); 4123e28f9b7dSAllan Jude else 4124e28f9b7dSAllan Jude return (EPERM); 4125e28f9b7dSAllan Jude 4126e28f9b7dSAllan Jude /* 4127e28f9b7dSAllan Jude * Allow jailed root to reuse in-use ports. 4128e28f9b7dSAllan Jude */ 41294b084056SRobert Watson case PRIV_NETINET_REUSEPORT: 4130800c9408SRobert Watson return (0); 4131800c9408SRobert Watson 4132800c9408SRobert Watson /* 4133e3043798SPedro F. Giffuni * Allow jailed root to set certain IPv4/6 (option) headers. 413479ba3952SBjoern A. Zeeb */ 413579ba3952SBjoern A. Zeeb case PRIV_NETINET_SETHDROPTS: 413679ba3952SBjoern A. Zeeb return (0); 413779ba3952SBjoern A. Zeeb 413879ba3952SBjoern A. Zeeb /* 4139800c9408SRobert Watson * Conditionally allow creating raw sockets in jail. 4140800c9408SRobert Watson */ 4141800c9408SRobert Watson case PRIV_NETINET_RAW: 41420304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS) 4143800c9408SRobert Watson return (0); 4144800c9408SRobert Watson else 4145800c9408SRobert Watson return (EPERM); 4146800c9408SRobert Watson 4147800c9408SRobert Watson /* 4148800c9408SRobert Watson * Since jail implements its own visibility limits on netstat 4149800c9408SRobert Watson * sysctls, allow getcred. This allows identd to work in 4150800c9408SRobert Watson * jail. 4151800c9408SRobert Watson */ 4152800c9408SRobert Watson case PRIV_NETINET_GETCRED: 4153800c9408SRobert Watson return (0); 4154800c9408SRobert Watson 41552bfc50bcSEdward Tomasz Napierala /* 41562bfc50bcSEdward Tomasz Napierala * Allow jailed root to set loginclass. 41572bfc50bcSEdward Tomasz Napierala */ 41582bfc50bcSEdward Tomasz Napierala case PRIV_PROC_SETLOGINCLASS: 41592bfc50bcSEdward Tomasz Napierala return (0); 41602bfc50bcSEdward Tomasz Napierala 4161b19d66fdSJamie Gritton /* 41624520f617SJamie Gritton * Do not allow a process inside a jail to read the kernel 4163b19d66fdSJamie Gritton * message buffer unless explicitly permitted. 4164b19d66fdSJamie Gritton */ 4165b19d66fdSJamie Gritton case PRIV_MSGBUF: 4166b19d66fdSJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_READ_MSGBUF) 4167b19d66fdSJamie Gritton return (0); 4168b19d66fdSJamie Gritton return (EPERM); 4169b19d66fdSJamie Gritton 4170800c9408SRobert Watson default: 4171800c9408SRobert Watson /* 4172800c9408SRobert Watson * In all remaining cases, deny the privilege request. This 4173800c9408SRobert Watson * includes almost all network privileges, many system 4174800c9408SRobert Watson * configuration privileges. 4175800c9408SRobert Watson */ 4176800c9408SRobert Watson return (EPERM); 4177800c9408SRobert Watson } 4178800c9408SRobert Watson } 4179800c9408SRobert Watson 41800304c731SJamie Gritton /* 41810304c731SJamie Gritton * Return the part of pr2's name that is relative to pr1, or the whole name 41820304c731SJamie Gritton * if it does not directly follow. 41830304c731SJamie Gritton */ 41840304c731SJamie Gritton 41850304c731SJamie Gritton char * 41860304c731SJamie Gritton prison_name(struct prison *pr1, struct prison *pr2) 41870304c731SJamie Gritton { 41880304c731SJamie Gritton char *name; 41890304c731SJamie Gritton 41900304c731SJamie Gritton /* Jails see themselves as "0" (if they see themselves at all). */ 41910304c731SJamie Gritton if (pr1 == pr2) 41920304c731SJamie Gritton return "0"; 41930304c731SJamie Gritton name = pr2->pr_name; 41940304c731SJamie Gritton if (prison_ischild(pr1, pr2)) { 41950304c731SJamie Gritton /* 41960304c731SJamie Gritton * pr1 isn't locked (and allprison_lock may not be either) 41970304c731SJamie Gritton * so its length can't be counted on. But the number of dots 41980304c731SJamie Gritton * can be counted on - and counted. 41990304c731SJamie Gritton */ 42000304c731SJamie Gritton for (; pr1 != &prison0; pr1 = pr1->pr_parent) 42010304c731SJamie Gritton name = strchr(name, '.') + 1; 42020304c731SJamie Gritton } 42030304c731SJamie Gritton return (name); 42040304c731SJamie Gritton } 42050304c731SJamie Gritton 42060304c731SJamie Gritton /* 42070304c731SJamie Gritton * Return the part of pr2's path that is relative to pr1, or the whole path 42080304c731SJamie Gritton * if it does not directly follow. 42090304c731SJamie Gritton */ 42100304c731SJamie Gritton static char * 42110304c731SJamie Gritton prison_path(struct prison *pr1, struct prison *pr2) 42120304c731SJamie Gritton { 42130304c731SJamie Gritton char *path1, *path2; 42140304c731SJamie Gritton int len1; 42150304c731SJamie Gritton 42160304c731SJamie Gritton path1 = pr1->pr_path; 42170304c731SJamie Gritton path2 = pr2->pr_path; 42180304c731SJamie Gritton if (!strcmp(path1, "/")) 42190304c731SJamie Gritton return (path2); 42200304c731SJamie Gritton len1 = strlen(path1); 42210304c731SJamie Gritton if (strncmp(path1, path2, len1)) 42220304c731SJamie Gritton return (path2); 42230304c731SJamie Gritton if (path2[len1] == '\0') 42240304c731SJamie Gritton return "/"; 42250304c731SJamie Gritton if (path2[len1] == '/') 42260304c731SJamie Gritton return (path2 + len1); 42270304c731SJamie Gritton return (path2); 42280304c731SJamie Gritton } 42290304c731SJamie Gritton 42300304c731SJamie Gritton /* 42310304c731SJamie Gritton * Jail-related sysctls. 42320304c731SJamie Gritton */ 42337029da5cSPawel Biernacki static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 42340304c731SJamie Gritton "Jails"); 42350304c731SJamie Gritton 4236eb8dcdeaSGleb Smirnoff #if defined(INET) || defined(INET6) 4237eb8dcdeaSGleb Smirnoff /* 4238eb8dcdeaSGleb Smirnoff * Copy address array to memory that would be then SYSCTL_OUT-ed. 4239eb8dcdeaSGleb Smirnoff * sysctl_jail_list() helper. 4240eb8dcdeaSGleb Smirnoff */ 4241eb8dcdeaSGleb Smirnoff static void 4242eb8dcdeaSGleb Smirnoff prison_ip_copyout(struct prison *pr, const pr_family_t af, void **out, int *len) 4243eb8dcdeaSGleb Smirnoff { 42442c33b456SZhenlei Huang const struct prison_ip *pip; 4245eb8dcdeaSGleb Smirnoff const size_t size = pr_families[af].size; 4246eb8dcdeaSGleb Smirnoff 4247eb8dcdeaSGleb Smirnoff again: 4248eb8dcdeaSGleb Smirnoff mtx_assert(&pr->pr_mtx, MA_OWNED); 42492c33b456SZhenlei Huang if ((pip = pr->pr_addrs[af]) != NULL) { 42502c33b456SZhenlei Huang if (*len < pip->ips) { 42512c33b456SZhenlei Huang *len = pip->ips; 4252eb8dcdeaSGleb Smirnoff mtx_unlock(&pr->pr_mtx); 4253eb8dcdeaSGleb Smirnoff *out = realloc(*out, *len * size, M_TEMP, M_WAITOK); 4254eb8dcdeaSGleb Smirnoff mtx_lock(&pr->pr_mtx); 4255eb8dcdeaSGleb Smirnoff goto again; 4256eb8dcdeaSGleb Smirnoff } 42572c33b456SZhenlei Huang bcopy(pip->pr_ip, *out, pip->ips * size); 4258eb8dcdeaSGleb Smirnoff } 4259eb8dcdeaSGleb Smirnoff } 4260eb8dcdeaSGleb Smirnoff #endif 4261eb8dcdeaSGleb Smirnoff 4262fd7a8150SMike Barcroft static int 4263fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS) 4264fd7a8150SMike Barcroft { 4265b38ff370SJamie Gritton struct xprison *xp; 42660304c731SJamie Gritton struct prison *pr, *cpr; 4267b38ff370SJamie Gritton #ifdef INET 4268b38ff370SJamie Gritton struct in_addr *ip4 = NULL; 4269b38ff370SJamie Gritton int ip4s = 0; 4270b38ff370SJamie Gritton #endif 4271b38ff370SJamie Gritton #ifdef INET6 42723beefaedSColin Percival struct in6_addr *ip6 = NULL; 4273b38ff370SJamie Gritton int ip6s = 0; 4274b38ff370SJamie Gritton #endif 42750304c731SJamie Gritton int descend, error; 4276fd7a8150SMike Barcroft 4277b38ff370SJamie Gritton xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK); 42780304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 4279b38ff370SJamie Gritton error = 0; 4280dc68a633SPawel Jakub Dawidek sx_slock(&allprison_lock); 42810304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 42820304c731SJamie Gritton mtx_lock(&cpr->pr_mtx); 4283413628a7SBjoern A. Zeeb #ifdef INET 4284eb8dcdeaSGleb Smirnoff prison_ip_copyout(cpr, PR_INET, (void **)&ip4, &ip4s); 4285413628a7SBjoern A. Zeeb #endif 4286413628a7SBjoern A. Zeeb #ifdef INET6 4287eb8dcdeaSGleb Smirnoff prison_ip_copyout(cpr, PR_INET6, (void **)&ip6, &ip6s); 4288b38ff370SJamie Gritton #endif 4289b38ff370SJamie Gritton bzero(xp, sizeof(*xp)); 4290fd7a8150SMike Barcroft xp->pr_version = XPRISON_VERSION; 42910304c731SJamie Gritton xp->pr_id = cpr->pr_id; 42921158508aSJamie Gritton xp->pr_state = cpr->pr_state; 42930304c731SJamie Gritton strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path)); 4294c1f19219SJamie Gritton strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host)); 42950304c731SJamie Gritton strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name)); 4296413628a7SBjoern A. Zeeb #ifdef INET 4297eb8dcdeaSGleb Smirnoff xp->pr_ip4s = ip4s; 4298413628a7SBjoern A. Zeeb #endif 4299413628a7SBjoern A. Zeeb #ifdef INET6 4300eb8dcdeaSGleb Smirnoff xp->pr_ip6s = ip6s; 4301413628a7SBjoern A. Zeeb #endif 43020304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 4303b38ff370SJamie Gritton error = SYSCTL_OUT(req, xp, sizeof(*xp)); 4304b38ff370SJamie Gritton if (error) 4305b38ff370SJamie Gritton break; 4306413628a7SBjoern A. Zeeb #ifdef INET 4307b38ff370SJamie Gritton if (xp->pr_ip4s > 0) { 4308b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip4, 4309b38ff370SJamie Gritton xp->pr_ip4s * sizeof(struct in_addr)); 4310b38ff370SJamie Gritton if (error) 4311b38ff370SJamie Gritton break; 4312413628a7SBjoern A. Zeeb } 4313413628a7SBjoern A. Zeeb #endif 4314413628a7SBjoern A. Zeeb #ifdef INET6 4315b38ff370SJamie Gritton if (xp->pr_ip6s > 0) { 4316b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip6, 4317b38ff370SJamie Gritton xp->pr_ip6s * sizeof(struct in6_addr)); 4318b38ff370SJamie Gritton if (error) 4319b38ff370SJamie Gritton break; 4320413628a7SBjoern A. Zeeb } 4321413628a7SBjoern A. Zeeb #endif 4322fd7a8150SMike Barcroft } 4323dc68a633SPawel Jakub Dawidek sx_sunlock(&allprison_lock); 4324b38ff370SJamie Gritton free(xp, M_TEMP); 4325b38ff370SJamie Gritton #ifdef INET 4326b38ff370SJamie Gritton free(ip4, M_TEMP); 4327b38ff370SJamie Gritton #endif 4328b38ff370SJamie Gritton #ifdef INET6 4329b38ff370SJamie Gritton free(ip6, M_TEMP); 4330b38ff370SJamie Gritton #endif 4331fd7a8150SMike Barcroft return (error); 4332fd7a8150SMike Barcroft } 4333fd7a8150SMike Barcroft 4334f3b86a5fSEd Schouten SYSCTL_OID(_security_jail, OID_AUTO, list, 4335f3b86a5fSEd Schouten CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4336f3b86a5fSEd Schouten sysctl_jail_list, "S", "List of active jails"); 4337461167c2SPawel Jakub Dawidek 4338461167c2SPawel Jakub Dawidek static int 4339461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS) 4340461167c2SPawel Jakub Dawidek { 4341461167c2SPawel Jakub Dawidek int error, injail; 4342461167c2SPawel Jakub Dawidek 4343461167c2SPawel Jakub Dawidek injail = jailed(req->td->td_ucred); 4344461167c2SPawel Jakub Dawidek error = SYSCTL_OUT(req, &injail, sizeof(injail)); 4345461167c2SPawel Jakub Dawidek 4346461167c2SPawel Jakub Dawidek return (error); 4347461167c2SPawel Jakub Dawidek } 43480304c731SJamie Gritton 4349f3b86a5fSEd Schouten SYSCTL_PROC(_security_jail, OID_AUTO, jailed, 4350f3b86a5fSEd Schouten CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4351f3b86a5fSEd Schouten sysctl_jail_jailed, "I", "Process in jail?"); 4352413628a7SBjoern A. Zeeb 4353761d2bb5SJamie Gritton static int 4354761d2bb5SJamie Gritton sysctl_jail_vnet(SYSCTL_HANDLER_ARGS) 4355761d2bb5SJamie Gritton { 4356761d2bb5SJamie Gritton int error, havevnet; 4357761d2bb5SJamie Gritton #ifdef VIMAGE 4358761d2bb5SJamie Gritton struct ucred *cred = req->td->td_ucred; 4359761d2bb5SJamie Gritton 4360761d2bb5SJamie Gritton havevnet = jailed(cred) && prison_owns_vnet(cred); 4361761d2bb5SJamie Gritton #else 4362761d2bb5SJamie Gritton havevnet = 0; 4363761d2bb5SJamie Gritton #endif 4364761d2bb5SJamie Gritton error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet)); 4365761d2bb5SJamie Gritton 4366761d2bb5SJamie Gritton return (error); 4367761d2bb5SJamie Gritton } 4368761d2bb5SJamie Gritton 4369761d2bb5SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, vnet, 4370761d2bb5SJamie Gritton CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4371bb8f1623SBjoern A. Zeeb sysctl_jail_vnet, "I", "Jail owns vnet?"); 4372761d2bb5SJamie Gritton 43730304c731SJamie Gritton #if defined(INET) || defined(INET6) 4374e92e0574SJamie Gritton SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW, 43750304c731SJamie Gritton &jail_max_af_ips, 0, 4376c542c43eSJamie Gritton "Number of IP addresses a jail may have at most per address family (deprecated)"); 43770304c731SJamie Gritton #endif 43780304c731SJamie Gritton 43790304c731SJamie Gritton /* 4380c542c43eSJamie Gritton * Default parameters for jail(2) compatibility. For historical reasons, 4381c542c43eSJamie Gritton * the sysctl names have varying similarity to the parameter names. Prisons 4382c542c43eSJamie Gritton * just see their own parameters, and can't change them. 43830304c731SJamie Gritton */ 43840304c731SJamie Gritton static int 43850304c731SJamie Gritton sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS) 43860304c731SJamie Gritton { 43870fe74ae6SJamie Gritton int error, i; 43880304c731SJamie Gritton 43890304c731SJamie Gritton /* Get the current flag value, and convert it to a boolean. */ 43900fe74ae6SJamie Gritton if (req->td->td_ucred->cr_prison == &prison0) { 43910fe74ae6SJamie Gritton mtx_lock(&prison0.pr_mtx); 43920fe74ae6SJamie Gritton i = (jail_default_allow & arg2) != 0; 43930fe74ae6SJamie Gritton mtx_unlock(&prison0.pr_mtx); 43940fe74ae6SJamie Gritton } else 43950fe74ae6SJamie Gritton i = prison_allow(req->td->td_ucred, arg2); 43960fe74ae6SJamie Gritton 43970304c731SJamie Gritton if (arg1 != NULL) 43980304c731SJamie Gritton i = !i; 43990304c731SJamie Gritton error = sysctl_handle_int(oidp, &i, 0, req); 4400c542c43eSJamie Gritton if (error || !req->newptr) 44010304c731SJamie Gritton return (error); 44020304c731SJamie Gritton i = i ? arg2 : 0; 44030304c731SJamie Gritton if (arg1 != NULL) 44040304c731SJamie Gritton i ^= arg2; 44050304c731SJamie Gritton /* 44060304c731SJamie Gritton * The sysctls don't have CTLFLAGS_PRISON, so assume prison0 44070304c731SJamie Gritton * for writing. 44080304c731SJamie Gritton */ 44090304c731SJamie Gritton mtx_lock(&prison0.pr_mtx); 44100304c731SJamie Gritton jail_default_allow = (jail_default_allow & ~arg2) | i; 44110304c731SJamie Gritton mtx_unlock(&prison0.pr_mtx); 44120304c731SJamie Gritton return (0); 44130304c731SJamie Gritton } 44140304c731SJamie Gritton 44150304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed, 4416c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44170304c731SJamie Gritton NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I", 4418c542c43eSJamie Gritton "Processes in jail can set their hostnames (deprecated)"); 44190304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only, 4420c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44210304c731SJamie Gritton (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I", 4422c542c43eSJamie Gritton "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)"); 44230304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed, 4424c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44250304c731SJamie Gritton NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I", 4426c542c43eSJamie Gritton "Processes in jail can use System V IPC primitives (deprecated)"); 44270304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets, 4428c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44290304c731SJamie Gritton NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I", 4430c542c43eSJamie Gritton "Prison root can create raw sockets (deprecated)"); 44310304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed, 4432c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44330304c731SJamie Gritton NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I", 4434c542c43eSJamie Gritton "Processes in jail can alter system file flags (deprecated)"); 44350304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed, 4436c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44370304c731SJamie Gritton NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I", 4438c542c43eSJamie Gritton "Processes in jail can mount/unmount jail-friendly file systems (deprecated)"); 44390304c731SJamie Gritton 44400304c731SJamie Gritton static int 44410304c731SJamie Gritton sysctl_jail_default_level(SYSCTL_HANDLER_ARGS) 44420304c731SJamie Gritton { 44430304c731SJamie Gritton struct prison *pr; 44440304c731SJamie Gritton int level, error; 44450304c731SJamie Gritton 44460304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 44470304c731SJamie Gritton level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2); 44480304c731SJamie Gritton error = sysctl_handle_int(oidp, &level, 0, req); 4449c542c43eSJamie Gritton if (error || !req->newptr) 44500304c731SJamie Gritton return (error); 44510304c731SJamie Gritton *(int *)arg1 = level; 44520304c731SJamie Gritton return (0); 44530304c731SJamie Gritton } 44540304c731SJamie Gritton 44550304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs, 4456c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4457c542c43eSJamie Gritton &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs), 44580304c731SJamie Gritton sysctl_jail_default_level, "I", 4459c542c43eSJamie Gritton "Processes in jail cannot see all mounted file systems (deprecated)"); 4460c542c43eSJamie Gritton 44610cc207a6SMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset, 4462c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 4463c542c43eSJamie Gritton &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum), 44640cc207a6SMartin Matuska sysctl_jail_default_level, "I", 4465c542c43eSJamie Gritton "Ruleset for the devfs filesystem in jail (deprecated)"); 44660cc207a6SMartin Matuska 44670304c731SJamie Gritton /* 44680304c731SJamie Gritton * Nodes to describe jail parameters. Maximum length of string parameters 44690304c731SJamie Gritton * is returned in the string itself, and the other parameters exist merely 44700304c731SJamie Gritton * to make themselves and their types known. 44710304c731SJamie Gritton */ 44727029da5cSPawel Biernacki SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 44730304c731SJamie Gritton "Jail parameters"); 44740304c731SJamie Gritton 44750304c731SJamie Gritton int 44760304c731SJamie Gritton sysctl_jail_param(SYSCTL_HANDLER_ARGS) 44770304c731SJamie Gritton { 44780304c731SJamie Gritton int i; 44790304c731SJamie Gritton long l; 44800304c731SJamie Gritton size_t s; 44810304c731SJamie Gritton char numbuf[12]; 44820304c731SJamie Gritton 44830304c731SJamie Gritton switch (oidp->oid_kind & CTLTYPE) 44840304c731SJamie Gritton { 44850304c731SJamie Gritton case CTLTYPE_LONG: 44860304c731SJamie Gritton case CTLTYPE_ULONG: 44870304c731SJamie Gritton l = 0; 44880304c731SJamie Gritton #ifdef SCTL_MASK32 44890304c731SJamie Gritton if (!(req->flags & SCTL_MASK32)) 44900304c731SJamie Gritton #endif 44910304c731SJamie Gritton return (SYSCTL_OUT(req, &l, sizeof(l))); 44920304c731SJamie Gritton case CTLTYPE_INT: 44930304c731SJamie Gritton case CTLTYPE_UINT: 44940304c731SJamie Gritton i = 0; 44950304c731SJamie Gritton return (SYSCTL_OUT(req, &i, sizeof(i))); 44960304c731SJamie Gritton case CTLTYPE_STRING: 4497e4cd31ddSJeff Roberson snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2); 44980304c731SJamie Gritton return 44990304c731SJamie Gritton (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req)); 45000304c731SJamie Gritton case CTLTYPE_STRUCT: 45010304c731SJamie Gritton s = (size_t)arg2; 45020304c731SJamie Gritton return (SYSCTL_OUT(req, &s, sizeof(s))); 45030304c731SJamie Gritton } 45040304c731SJamie Gritton return (0); 45050304c731SJamie Gritton } 45060304c731SJamie Gritton 4507b96bd95bSIan Lepore /* 4508b96bd95bSIan Lepore * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at 4509b96bd95bSIan Lepore * jail creation time but cannot be changed in an existing jail. 4510b96bd95bSIan Lepore */ 45110304c731SJamie Gritton SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID"); 45120304c731SJamie Gritton SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID"); 45130304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name"); 45140304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path"); 45150304c731SJamie Gritton SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW, 45160304c731SJamie Gritton "I", "Jail secure level"); 4517b96bd95bSIan Lepore SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I", 4518b96bd95bSIan Lepore "Jail value for kern.osreldate and uname -K"); 4519b96bd95bSIan Lepore SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN, 4520b96bd95bSIan Lepore "Jail value for kern.osrelease and uname -r"); 45210304c731SJamie Gritton SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW, 45220304c731SJamie Gritton "I", "Jail cannot see all mounted file systems"); 45230cc207a6SMartin Matuska SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW, 45240cc207a6SMartin Matuska "I", "Ruleset for in-jail devfs mounts"); 45250304c731SJamie Gritton SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW, 45260304c731SJamie Gritton "B", "Jail persistence"); 4527679e1390SJamie Gritton #ifdef VIMAGE 4528679e1390SJamie Gritton SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN, 45297cbf7213SJamie Gritton "E,jailsys", "Virtual network stack"); 4530679e1390SJamie Gritton #endif 45310304c731SJamie Gritton SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD, 45320304c731SJamie Gritton "B", "Jail is in the process of shutting down"); 45330304c731SJamie Gritton 4534b97457e2SJamie Gritton SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails"); 4535b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD, 4536b97457e2SJamie Gritton "I", "Current number of child jails"); 4537b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW, 4538b97457e2SJamie Gritton "I", "Maximum number of child jails"); 4539b97457e2SJamie Gritton 45407cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info"); 45410304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN, 45420304c731SJamie Gritton "Jail hostname"); 454376ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN, 454476ca6f88SJamie Gritton "Jail NIS domainname"); 454576ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN, 454676ca6f88SJamie Gritton "Jail host UUID"); 454776ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW, 454876ca6f88SJamie Gritton "LU", "Jail host ID"); 45490304c731SJamie Gritton 45500304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset"); 45510304c731SJamie Gritton SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID"); 45520304c731SJamie Gritton 45530304c731SJamie Gritton #ifdef INET 45542b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN, 45552b0d6f81SJamie Gritton "Jail IPv4 address virtualization"); 45560304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr), 45570304c731SJamie Gritton "S,in_addr,a", "Jail IPv4 addresses"); 4558592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4559592bcae8SBjoern A. Zeeb "B", "Do (not) use IPv4 source address selection rather than the " 4560592bcae8SBjoern A. Zeeb "primary jail IPv4 address."); 45610304c731SJamie Gritton #endif 45620304c731SJamie Gritton #ifdef INET6 45632b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN, 45642b0d6f81SJamie Gritton "Jail IPv6 address virtualization"); 45650304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr), 45660304c731SJamie Gritton "S,in6_addr,a", "Jail IPv6 addresses"); 4567592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4568592bcae8SBjoern A. Zeeb "B", "Do (not) use IPv6 source address selection rather than the " 4569592bcae8SBjoern A. Zeeb "primary jail IPv6 address."); 45700304c731SJamie Gritton #endif 45710304c731SJamie Gritton 45720304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags"); 45730304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW, 45740304c731SJamie Gritton "B", "Jail may set hostname"); 45750304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW, 45760304c731SJamie Gritton "B", "Jail may use SYSV IPC"); 45770304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW, 45780304c731SJamie Gritton "B", "Jail may create raw sockets"); 45790304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW, 45800304c731SJamie Gritton "B", "Jail may alter system file flags"); 45810304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW, 45820304c731SJamie Gritton "B", "Jail may set file quotas"); 45830304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW, 45840304c731SJamie Gritton "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route"); 4585ccd6ac9fSAntoine Brodin SYSCTL_JAIL_PARAM(_allow, mlock, CTLTYPE_INT | CTLFLAG_RW, 4586ccd6ac9fSAntoine Brodin "B", "Jail may lock (unlock) physical pages in memory"); 4587e28f9b7dSAllan Jude SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW, 4588e28f9b7dSAllan Jude "B", "Jail may bind sockets to reserved ports"); 4589b19d66fdSJamie Gritton SYSCTL_JAIL_PARAM(_allow, read_msgbuf, CTLTYPE_INT | CTLFLAG_RW, 4590b19d66fdSJamie Gritton "B", "Jail may read the kernel message buffer"); 4591b3079544SJamie Gritton SYSCTL_JAIL_PARAM(_allow, unprivileged_proc_debug, CTLTYPE_INT | CTLFLAG_RW, 4592b3079544SJamie Gritton "B", "Unprivileged processes may use process debugging facilities"); 459305e1e482SMariusz Zaborski SYSCTL_JAIL_PARAM(_allow, suser, CTLTYPE_INT | CTLFLAG_RW, 459405e1e482SMariusz Zaborski "B", "Processes in jail with uid 0 have privilege"); 4595cbbb2203SRick Macklem #ifdef VIMAGE 4596bba7a2e8SRick Macklem SYSCTL_JAIL_PARAM(_allow, nfsd, CTLTYPE_INT | CTLFLAG_RW, 4597bba7a2e8SRick Macklem "B", "Mountd/nfsd may run in the jail"); 4598bba7a2e8SRick Macklem #endif 4599cb48780dSShawn Webb SYSCTL_JAIL_PARAM(_allow, extattr, CTLTYPE_INT | CTLFLAG_RW, 4600cb48780dSShawn Webb "B", "Jail may set system-level filesystem extended attributes"); 46010304c731SJamie Gritton 4602bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags"); 4603bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW, 4604bf3db8aaSMartin Matuska "B", "Jail may mount/unmount jail-friendly file systems in general"); 46050e5c6bd4SJamie Gritton 46060e5c6bd4SJamie Gritton /* 46070a172404SJamie Gritton * Add a dynamic parameter allow.<name>, or allow.<prefix>.<name>. Return 46080a172404SJamie Gritton * its associated bit in the pr_allow bitmask, or zero if the parameter was 46090a172404SJamie Gritton * not created. 46100e5c6bd4SJamie Gritton */ 46110a172404SJamie Gritton unsigned 46120a172404SJamie Gritton prison_add_allow(const char *prefix, const char *name, const char *prefix_descr, 46130a172404SJamie Gritton const char *descr) 46140e5c6bd4SJamie Gritton { 46150e5c6bd4SJamie Gritton struct bool_flags *bf; 46160a172404SJamie Gritton struct sysctl_oid *parent; 46170a172404SJamie Gritton char *allow_name, *allow_noname, *allowed; 4618c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR 4619c542c43eSJamie Gritton char *descr_deprecated; 4620c542c43eSJamie Gritton #endif 46215d58f959SJamie Gritton u_int allow_flag; 46220e5c6bd4SJamie Gritton 46230a172404SJamie Gritton if (prefix 46240a172404SJamie Gritton ? asprintf(&allow_name, M_PRISON, "allow.%s.%s", prefix, name) 46250a172404SJamie Gritton < 0 || 46260a172404SJamie Gritton asprintf(&allow_noname, M_PRISON, "allow.%s.no%s", prefix, name) 46270a172404SJamie Gritton < 0 46280a172404SJamie Gritton : asprintf(&allow_name, M_PRISON, "allow.%s", name) < 0 || 46290a172404SJamie Gritton asprintf(&allow_noname, M_PRISON, "allow.no%s", name) < 0) { 46300e5c6bd4SJamie Gritton free(allow_name, M_PRISON); 46310a172404SJamie Gritton return 0; 46320e5c6bd4SJamie Gritton } 46330e5c6bd4SJamie Gritton 46340e5c6bd4SJamie Gritton /* 46350a172404SJamie Gritton * See if this parameter has already beed added, i.e. a module was 46360a172404SJamie Gritton * previously loaded/unloaded. 46370e5c6bd4SJamie Gritton */ 46380e5c6bd4SJamie Gritton mtx_lock(&prison0.pr_mtx); 46390e5c6bd4SJamie Gritton for (bf = pr_flag_allow; 46405d58f959SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 46415d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 46420e5c6bd4SJamie Gritton bf++) { 46430e5c6bd4SJamie Gritton if (strcmp(bf->name, allow_name) == 0) { 46440a172404SJamie Gritton allow_flag = bf->flag; 46450e5c6bd4SJamie Gritton goto no_add; 46460e5c6bd4SJamie Gritton } 46470e5c6bd4SJamie Gritton } 46480e5c6bd4SJamie Gritton 46490e5c6bd4SJamie Gritton /* 46505d58f959SJamie Gritton * Find a free bit in pr_allow_all, failing if there are none 46510e5c6bd4SJamie Gritton * (which shouldn't happen as long as we keep track of how many 46520a172404SJamie Gritton * potential dynamic flags exist). 46530e5c6bd4SJamie Gritton */ 46540e5c6bd4SJamie Gritton for (allow_flag = 1;; allow_flag <<= 1) { 46550e5c6bd4SJamie Gritton if (allow_flag == 0) 46560e5c6bd4SJamie Gritton goto no_add; 46575d58f959SJamie Gritton if ((pr_allow_all & allow_flag) == 0) 46580e5c6bd4SJamie Gritton break; 46590e5c6bd4SJamie Gritton } 46600e5c6bd4SJamie Gritton 46615d58f959SJamie Gritton /* Note the parameter in the next open slot in pr_flag_allow. */ 46625d58f959SJamie Gritton for (bf = pr_flag_allow; ; bf++) { 46630e5c6bd4SJamie Gritton if (bf == pr_flag_allow + nitems(pr_flag_allow)) { 46640e5c6bd4SJamie Gritton /* This should never happen, but is not fatal. */ 46650a172404SJamie Gritton allow_flag = 0; 46660e5c6bd4SJamie Gritton goto no_add; 46670e5c6bd4SJamie Gritton } 46685d58f959SJamie Gritton if (atomic_load_int(&bf->flag) == 0) 46695d58f959SJamie Gritton break; 46705d58f959SJamie Gritton } 46710e5c6bd4SJamie Gritton bf->name = allow_name; 46720e5c6bd4SJamie Gritton bf->noname = allow_noname; 46735d58f959SJamie Gritton pr_allow_all |= allow_flag; 46745d58f959SJamie Gritton /* 46755d58f959SJamie Gritton * prison0 always has permission for the new parameter. 46765d58f959SJamie Gritton * Other jails must have it granted to them. 46775d58f959SJamie Gritton */ 46785d58f959SJamie Gritton prison0.pr_allow |= allow_flag; 46795d58f959SJamie Gritton /* The flag indicates a valid entry, so make sure it is set last. */ 46805d58f959SJamie Gritton atomic_store_rel_int(&bf->flag, allow_flag); 46810e5c6bd4SJamie Gritton mtx_unlock(&prison0.pr_mtx); 46820e5c6bd4SJamie Gritton 4683c542c43eSJamie Gritton /* 46844771011bSGordon Bergling * Create sysctls for the parameter, and the back-compat global 4685c542c43eSJamie Gritton * permission. 4686c542c43eSJamie Gritton */ 46870a172404SJamie Gritton parent = prefix 46880a172404SJamie Gritton ? SYSCTL_ADD_NODE(NULL, 46890a172404SJamie Gritton SYSCTL_CHILDREN(&sysctl___security_jail_param_allow), 46907029da5cSPawel Biernacki OID_AUTO, prefix, CTLFLAG_MPSAFE, 0, prefix_descr) 46910a172404SJamie Gritton : &sysctl___security_jail_param_allow; 46920a172404SJamie Gritton (void)SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(parent), OID_AUTO, 46930a172404SJamie Gritton name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 46940e5c6bd4SJamie Gritton NULL, 0, sysctl_jail_param, "B", descr); 46950a172404SJamie Gritton if ((prefix 46960a172404SJamie Gritton ? asprintf(&allowed, M_TEMP, "%s_%s_allowed", prefix, name) 46970a172404SJamie Gritton : asprintf(&allowed, M_TEMP, "%s_allowed", name)) >= 0) { 4698c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR 4699c542c43eSJamie Gritton (void)asprintf(&descr_deprecated, M_TEMP, "%s (deprecated)", 4700c542c43eSJamie Gritton descr); 4701c542c43eSJamie Gritton #endif 47020e5c6bd4SJamie Gritton (void)SYSCTL_ADD_PROC(NULL, 47030a172404SJamie Gritton SYSCTL_CHILDREN(&sysctl___security_jail), OID_AUTO, allowed, 4704c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, allow_flag, 4705c542c43eSJamie Gritton sysctl_jail_default_allow, "I", descr_deprecated); 4706c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR 4707c542c43eSJamie Gritton free(descr_deprecated, M_TEMP); 4708c542c43eSJamie Gritton #endif 47090a172404SJamie Gritton free(allowed, M_TEMP); 47100e5c6bd4SJamie Gritton } 47110a172404SJamie Gritton return allow_flag; 47120e5c6bd4SJamie Gritton 47130e5c6bd4SJamie Gritton no_add: 47140e5c6bd4SJamie Gritton mtx_unlock(&prison0.pr_mtx); 47150e5c6bd4SJamie Gritton free(allow_name, M_PRISON); 47160e5c6bd4SJamie Gritton free(allow_noname, M_PRISON); 47170a172404SJamie Gritton return allow_flag; 47180a172404SJamie Gritton } 47190a172404SJamie Gritton 47200a172404SJamie Gritton /* 47210a172404SJamie Gritton * The VFS system will register jail-aware filesystems here. They each get 47220a172404SJamie Gritton * a parameter allow.mount.xxxfs and a flag to check when a jailed user 47230a172404SJamie Gritton * attempts to mount. 47240a172404SJamie Gritton */ 47250a172404SJamie Gritton void 47260a172404SJamie Gritton prison_add_vfs(struct vfsconf *vfsp) 47270a172404SJamie Gritton { 47280a172404SJamie Gritton #ifdef NO_SYSCTL_DESCR 47290a172404SJamie Gritton 47300a172404SJamie Gritton vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name, 47310a172404SJamie Gritton NULL, NULL); 47320a172404SJamie Gritton #else 47330a172404SJamie Gritton char *descr; 47340a172404SJamie Gritton 47350a172404SJamie Gritton (void)asprintf(&descr, M_TEMP, "Jail may mount the %s file system", 47360a172404SJamie Gritton vfsp->vfc_name); 47370a172404SJamie Gritton vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name, 47380a172404SJamie Gritton NULL, descr); 47390a172404SJamie Gritton free(descr, M_TEMP); 47400a172404SJamie Gritton #endif 47410e5c6bd4SJamie Gritton } 4742bf3db8aaSMartin Matuska 47434b5c9cf6SEdward Tomasz Napierala #ifdef RACCT 4744097055e2SEdward Tomasz Napierala void 4745097055e2SEdward Tomasz Napierala prison_racct_foreach(void (*callback)(struct racct *racct, 474615db3c07SEdward Tomasz Napierala void *arg2, void *arg3), void (*pre)(void), void (*post)(void), 474715db3c07SEdward Tomasz Napierala void *arg2, void *arg3) 4748097055e2SEdward Tomasz Napierala { 4749a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4750097055e2SEdward Tomasz Napierala 47514b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 47524b5c9cf6SEdward Tomasz Napierala 4753097055e2SEdward Tomasz Napierala sx_slock(&allprison_lock); 475415db3c07SEdward Tomasz Napierala if (pre != NULL) 475515db3c07SEdward Tomasz Napierala (pre)(); 4756a7ad07bfSEdward Tomasz Napierala LIST_FOREACH(prr, &allprison_racct, prr_next) 4757a7ad07bfSEdward Tomasz Napierala (callback)(prr->prr_racct, arg2, arg3); 475815db3c07SEdward Tomasz Napierala if (post != NULL) 475915db3c07SEdward Tomasz Napierala (post)(); 4760097055e2SEdward Tomasz Napierala sx_sunlock(&allprison_lock); 4761097055e2SEdward Tomasz Napierala } 47620304c731SJamie Gritton 4763a7ad07bfSEdward Tomasz Napierala static struct prison_racct * 4764a7ad07bfSEdward Tomasz Napierala prison_racct_find_locked(const char *name) 4765a7ad07bfSEdward Tomasz Napierala { 4766a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4767a7ad07bfSEdward Tomasz Napierala 47684b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4769a7ad07bfSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4770a7ad07bfSEdward Tomasz Napierala 4771a7ad07bfSEdward Tomasz Napierala if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN) 4772a7ad07bfSEdward Tomasz Napierala return (NULL); 4773a7ad07bfSEdward Tomasz Napierala 4774a7ad07bfSEdward Tomasz Napierala LIST_FOREACH(prr, &allprison_racct, prr_next) { 4775a7ad07bfSEdward Tomasz Napierala if (strcmp(name, prr->prr_name) != 0) 4776a7ad07bfSEdward Tomasz Napierala continue; 4777a7ad07bfSEdward Tomasz Napierala 4778a7ad07bfSEdward Tomasz Napierala /* Found prison_racct with a matching name? */ 4779a7ad07bfSEdward Tomasz Napierala prison_racct_hold(prr); 4780a7ad07bfSEdward Tomasz Napierala return (prr); 4781a7ad07bfSEdward Tomasz Napierala } 4782a7ad07bfSEdward Tomasz Napierala 4783a7ad07bfSEdward Tomasz Napierala /* Add new prison_racct. */ 4784a7ad07bfSEdward Tomasz Napierala prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK); 4785a7ad07bfSEdward Tomasz Napierala racct_create(&prr->prr_racct); 4786a7ad07bfSEdward Tomasz Napierala 4787a7ad07bfSEdward Tomasz Napierala strcpy(prr->prr_name, name); 4788a7ad07bfSEdward Tomasz Napierala refcount_init(&prr->prr_refcount, 1); 4789a7ad07bfSEdward Tomasz Napierala LIST_INSERT_HEAD(&allprison_racct, prr, prr_next); 4790a7ad07bfSEdward Tomasz Napierala 4791a7ad07bfSEdward Tomasz Napierala return (prr); 4792a7ad07bfSEdward Tomasz Napierala } 4793a7ad07bfSEdward Tomasz Napierala 4794a7ad07bfSEdward Tomasz Napierala struct prison_racct * 4795a7ad07bfSEdward Tomasz Napierala prison_racct_find(const char *name) 4796a7ad07bfSEdward Tomasz Napierala { 4797a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4798a7ad07bfSEdward Tomasz Napierala 47994b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 48004b5c9cf6SEdward Tomasz Napierala 4801a7ad07bfSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4802a7ad07bfSEdward Tomasz Napierala prr = prison_racct_find_locked(name); 4803a7ad07bfSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4804a7ad07bfSEdward Tomasz Napierala return (prr); 4805a7ad07bfSEdward Tomasz Napierala } 4806a7ad07bfSEdward Tomasz Napierala 4807a7ad07bfSEdward Tomasz Napierala void 4808a7ad07bfSEdward Tomasz Napierala prison_racct_hold(struct prison_racct *prr) 4809a7ad07bfSEdward Tomasz Napierala { 4810a7ad07bfSEdward Tomasz Napierala 48114b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 48124b5c9cf6SEdward Tomasz Napierala 4813a7ad07bfSEdward Tomasz Napierala refcount_acquire(&prr->prr_refcount); 4814a7ad07bfSEdward Tomasz Napierala } 4815a7ad07bfSEdward Tomasz Napierala 4816c34bbd2aSEdward Tomasz Napierala static void 4817c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(struct prison_racct *prr) 4818c34bbd2aSEdward Tomasz Napierala { 4819c34bbd2aSEdward Tomasz Napierala 48204b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4821c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4822c34bbd2aSEdward Tomasz Napierala 4823c34bbd2aSEdward Tomasz Napierala if (refcount_release(&prr->prr_refcount)) { 4824c34bbd2aSEdward Tomasz Napierala racct_destroy(&prr->prr_racct); 4825c34bbd2aSEdward Tomasz Napierala LIST_REMOVE(prr, prr_next); 4826c34bbd2aSEdward Tomasz Napierala free(prr, M_PRISON_RACCT); 4827c34bbd2aSEdward Tomasz Napierala } 4828c34bbd2aSEdward Tomasz Napierala } 4829c34bbd2aSEdward Tomasz Napierala 4830a7ad07bfSEdward Tomasz Napierala void 4831a7ad07bfSEdward Tomasz Napierala prison_racct_free(struct prison_racct *prr) 4832a7ad07bfSEdward Tomasz Napierala { 4833a7ad07bfSEdward Tomasz Napierala 48344b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4835c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_UNLOCKED); 4836c34bbd2aSEdward Tomasz Napierala 48376ff4688bSMateusz Guzik if (refcount_release_if_not_last(&prr->prr_refcount)) 4838a7ad07bfSEdward Tomasz Napierala return; 4839a7ad07bfSEdward Tomasz Napierala 4840a7ad07bfSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4841c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(prr); 4842a7ad07bfSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4843a7ad07bfSEdward Tomasz Napierala } 4844a7ad07bfSEdward Tomasz Napierala 4845a7ad07bfSEdward Tomasz Napierala static void 4846a7ad07bfSEdward Tomasz Napierala prison_racct_attach(struct prison *pr) 4847a7ad07bfSEdward Tomasz Napierala { 4848a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4849a7ad07bfSEdward Tomasz Napierala 48504b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4851c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4852c34bbd2aSEdward Tomasz Napierala 4853a7ad07bfSEdward Tomasz Napierala prr = prison_racct_find_locked(pr->pr_name); 4854a7ad07bfSEdward Tomasz Napierala KASSERT(prr != NULL, ("cannot find prison_racct")); 4855a7ad07bfSEdward Tomasz Napierala 4856a7ad07bfSEdward Tomasz Napierala pr->pr_prison_racct = prr; 4857a7ad07bfSEdward Tomasz Napierala } 4858a7ad07bfSEdward Tomasz Napierala 4859c34bbd2aSEdward Tomasz Napierala /* 4860c34bbd2aSEdward Tomasz Napierala * Handle jail renaming. From the racct point of view, renaming means 4861c34bbd2aSEdward Tomasz Napierala * moving from one prison_racct to another. 4862c34bbd2aSEdward Tomasz Napierala */ 4863c34bbd2aSEdward Tomasz Napierala static void 4864c34bbd2aSEdward Tomasz Napierala prison_racct_modify(struct prison *pr) 4865c34bbd2aSEdward Tomasz Napierala { 4866dbadb015SKonstantin Belousov #ifdef RCTL 4867c34bbd2aSEdward Tomasz Napierala struct proc *p; 4868c34bbd2aSEdward Tomasz Napierala struct ucred *cred; 4869dbadb015SKonstantin Belousov #endif 4870c34bbd2aSEdward Tomasz Napierala struct prison_racct *oldprr; 4871c34bbd2aSEdward Tomasz Napierala 48724b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 48734b5c9cf6SEdward Tomasz Napierala 4874c34bbd2aSEdward Tomasz Napierala sx_slock(&allproc_lock); 4875c34bbd2aSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4876c34bbd2aSEdward Tomasz Napierala 4877e30345e7SEdward Tomasz Napierala if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) { 4878e30345e7SEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4879e30345e7SEdward Tomasz Napierala sx_sunlock(&allproc_lock); 4880c34bbd2aSEdward Tomasz Napierala return; 4881e30345e7SEdward Tomasz Napierala } 4882c34bbd2aSEdward Tomasz Napierala 4883c34bbd2aSEdward Tomasz Napierala oldprr = pr->pr_prison_racct; 4884c34bbd2aSEdward Tomasz Napierala pr->pr_prison_racct = NULL; 4885c34bbd2aSEdward Tomasz Napierala 4886c34bbd2aSEdward Tomasz Napierala prison_racct_attach(pr); 4887c34bbd2aSEdward Tomasz Napierala 4888c34bbd2aSEdward Tomasz Napierala /* 4889c34bbd2aSEdward Tomasz Napierala * Move resource utilisation records. 4890c34bbd2aSEdward Tomasz Napierala */ 4891c34bbd2aSEdward Tomasz Napierala racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct); 4892c34bbd2aSEdward Tomasz Napierala 4893f87beb93SAndriy Gapon #ifdef RCTL 4894c34bbd2aSEdward Tomasz Napierala /* 4895c34bbd2aSEdward Tomasz Napierala * Force rctl to reattach rules to processes. 4896c34bbd2aSEdward Tomasz Napierala */ 4897c34bbd2aSEdward Tomasz Napierala FOREACH_PROC_IN_SYSTEM(p) { 4898c34bbd2aSEdward Tomasz Napierala PROC_LOCK(p); 4899c34bbd2aSEdward Tomasz Napierala cred = crhold(p->p_ucred); 4900c34bbd2aSEdward Tomasz Napierala PROC_UNLOCK(p); 4901f87beb93SAndriy Gapon rctl_proc_ucred_changed(p, cred); 4902c34bbd2aSEdward Tomasz Napierala crfree(cred); 4903c34bbd2aSEdward Tomasz Napierala } 4904f87beb93SAndriy Gapon #endif 4905c34bbd2aSEdward Tomasz Napierala 4906c34bbd2aSEdward Tomasz Napierala sx_sunlock(&allproc_lock); 4907c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(oldprr); 4908c34bbd2aSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4909c34bbd2aSEdward Tomasz Napierala } 4910c34bbd2aSEdward Tomasz Napierala 4911a7ad07bfSEdward Tomasz Napierala static void 4912a7ad07bfSEdward Tomasz Napierala prison_racct_detach(struct prison *pr) 4913a7ad07bfSEdward Tomasz Napierala { 4914c34bbd2aSEdward Tomasz Napierala 49154b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4916c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_UNLOCKED); 4917c34bbd2aSEdward Tomasz Napierala 4918af3c786cSMateusz Guzik if (pr->pr_prison_racct == NULL) 4919af3c786cSMateusz Guzik return; 4920a7ad07bfSEdward Tomasz Napierala prison_racct_free(pr->pr_prison_racct); 4921a7ad07bfSEdward Tomasz Napierala pr->pr_prison_racct = NULL; 4922a7ad07bfSEdward Tomasz Napierala } 4923a7ad07bfSEdward Tomasz Napierala #endif /* RACCT */ 4924a7ad07bfSEdward Tomasz Napierala 4925413628a7SBjoern A. Zeeb #ifdef DDB 4926b38ff370SJamie Gritton 4927b38ff370SJamie Gritton static void 4928b38ff370SJamie Gritton db_show_prison(struct prison *pr) 4929413628a7SBjoern A. Zeeb { 4930672756aaSJamie Gritton struct bool_flags *bf; 4931672756aaSJamie Gritton struct jailsys_flags *jsf; 4932b38ff370SJamie Gritton #if defined(INET) || defined(INET6) 4933b38ff370SJamie Gritton int ii; 4934500f82d6SZhenlei Huang struct prison_ip *pip; 4935413628a7SBjoern A. Zeeb #endif 4936672756aaSJamie Gritton unsigned f; 49378144690aSEric van Gyzen #ifdef INET 49388144690aSEric van Gyzen char ip4buf[INET_ADDRSTRLEN]; 49398144690aSEric van Gyzen #endif 4940413628a7SBjoern A. Zeeb #ifdef INET6 4941413628a7SBjoern A. Zeeb char ip6buf[INET6_ADDRSTRLEN]; 4942413628a7SBjoern A. Zeeb #endif 4943413628a7SBjoern A. Zeeb 4944b38ff370SJamie Gritton db_printf("prison %p:\n", pr); 4945b38ff370SJamie Gritton db_printf(" jid = %d\n", pr->pr_id); 4946b38ff370SJamie Gritton db_printf(" name = %s\n", pr->pr_name); 49470304c731SJamie Gritton db_printf(" parent = %p\n", pr->pr_parent); 4948b38ff370SJamie Gritton db_printf(" ref = %d\n", pr->pr_ref); 4949b38ff370SJamie Gritton db_printf(" uref = %d\n", pr->pr_uref); 49501158508aSJamie Gritton db_printf(" state = %s\n", 49511158508aSJamie Gritton pr->pr_state == PRISON_STATE_ALIVE ? "alive" : 49521158508aSJamie Gritton pr->pr_state == PRISON_STATE_DYING ? "dying" : 49531158508aSJamie Gritton "invalid"); 4954b38ff370SJamie Gritton db_printf(" path = %s\n", pr->pr_path); 4955b38ff370SJamie Gritton db_printf(" cpuset = %d\n", pr->pr_cpuset 4956b38ff370SJamie Gritton ? pr->pr_cpuset->cs_id : -1); 4957679e1390SJamie Gritton #ifdef VIMAGE 4958679e1390SJamie Gritton db_printf(" vnet = %p\n", pr->pr_vnet); 4959679e1390SJamie Gritton #endif 4960b38ff370SJamie Gritton db_printf(" root = %p\n", pr->pr_root); 4961b38ff370SJamie Gritton db_printf(" securelevel = %d\n", pr->pr_securelevel); 49620cc207a6SMartin Matuska db_printf(" devfs_rsnum = %d\n", pr->pr_devfs_rsnum); 4963fe0518e9SBjoern A. Zeeb db_printf(" children.max = %d\n", pr->pr_childmax); 4964fe0518e9SBjoern A. Zeeb db_printf(" children.cur = %d\n", pr->pr_childcount); 49650304c731SJamie Gritton db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children)); 49660304c731SJamie Gritton db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling)); 4967fe0518e9SBjoern A. Zeeb db_printf(" flags = 0x%x", pr->pr_flags); 4968672756aaSJamie Gritton for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++) 4969672756aaSJamie Gritton if (pr->pr_flags & bf->flag) 4970672756aaSJamie Gritton db_printf(" %s", bf->name); 4971672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 4972672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 4973672756aaSJamie Gritton jsf++) { 4974672756aaSJamie Gritton f = pr->pr_flags & (jsf->disable | jsf->new); 4975672756aaSJamie Gritton db_printf(" %-16s= %s\n", jsf->name, 4976672756aaSJamie Gritton (f != 0 && f == jsf->disable) ? "disable" 4977672756aaSJamie Gritton : (f == jsf->new) ? "new" 49787cbf7213SJamie Gritton : "inherit"); 49797cbf7213SJamie Gritton } 4980fe0518e9SBjoern A. Zeeb db_printf(" allow = 0x%x", pr->pr_allow); 4981672756aaSJamie Gritton for (bf = pr_flag_allow; 49825d58f959SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 49835d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 4984672756aaSJamie Gritton bf++) 4985672756aaSJamie Gritton if (pr->pr_allow & bf->flag) 4986672756aaSJamie Gritton db_printf(" %s", bf->name); 4987b38ff370SJamie Gritton db_printf("\n"); 49880304c731SJamie Gritton db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs); 4989c1f19219SJamie Gritton db_printf(" host.hostname = %s\n", pr->pr_hostname); 4990c1f19219SJamie Gritton db_printf(" host.domainname = %s\n", pr->pr_domainname); 4991c1f19219SJamie Gritton db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid); 499276ca6f88SJamie Gritton db_printf(" host.hostid = %lu\n", pr->pr_hostid); 4993413628a7SBjoern A. Zeeb #ifdef INET 4994500f82d6SZhenlei Huang if ((pip = pr->pr_addrs[PR_INET]) != NULL) { 4995500f82d6SZhenlei Huang db_printf(" ip4s = %d\n", pip->ips); 4996500f82d6SZhenlei Huang for (ii = 0; ii < pip->ips; ii++) 4997b38ff370SJamie Gritton db_printf(" %s %s\n", 4998fe0518e9SBjoern A. Zeeb ii == 0 ? "ip4.addr =" : " ", 4999eb8dcdeaSGleb Smirnoff inet_ntoa_r( 5000500f82d6SZhenlei Huang *(const struct in_addr *)PR_IP(pip, PR_INET, ii), 5001eb8dcdeaSGleb Smirnoff ip4buf)); 5002eb8dcdeaSGleb Smirnoff } 5003413628a7SBjoern A. Zeeb #endif 5004413628a7SBjoern A. Zeeb #ifdef INET6 5005500f82d6SZhenlei Huang if ((pip = pr->pr_addrs[PR_INET6]) != NULL) { 5006500f82d6SZhenlei Huang db_printf(" ip6s = %d\n", pip->ips); 5007500f82d6SZhenlei Huang for (ii = 0; ii < pip->ips; ii++) 5008b38ff370SJamie Gritton db_printf(" %s %s\n", 5009fe0518e9SBjoern A. Zeeb ii == 0 ? "ip6.addr =" : " ", 5010eb8dcdeaSGleb Smirnoff ip6_sprintf(ip6buf, 5011500f82d6SZhenlei Huang (const struct in6_addr *)PR_IP(pip, PR_INET6, ii))); 5012eb8dcdeaSGleb Smirnoff } 5013b38ff370SJamie Gritton #endif 5014b38ff370SJamie Gritton } 5015b38ff370SJamie Gritton 5016b38ff370SJamie Gritton DB_SHOW_COMMAND(prison, db_show_prison_command) 5017b38ff370SJamie Gritton { 5018b38ff370SJamie Gritton struct prison *pr; 5019b38ff370SJamie Gritton 5020b38ff370SJamie Gritton if (!have_addr) { 50210304c731SJamie Gritton /* 50220304c731SJamie Gritton * Show all prisons in the list, and prison0 which is not 50230304c731SJamie Gritton * listed. 50240304c731SJamie Gritton */ 50250304c731SJamie Gritton db_show_prison(&prison0); 50260304c731SJamie Gritton if (!db_pager_quit) { 5027b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 5028b38ff370SJamie Gritton db_show_prison(pr); 5029413628a7SBjoern A. Zeeb if (db_pager_quit) 5030413628a7SBjoern A. Zeeb break; 5031413628a7SBjoern A. Zeeb } 50320304c731SJamie Gritton } 5033b38ff370SJamie Gritton return; 5034413628a7SBjoern A. Zeeb } 5035b38ff370SJamie Gritton 50360304c731SJamie Gritton if (addr == 0) 50370304c731SJamie Gritton pr = &prison0; 50380304c731SJamie Gritton else { 5039b38ff370SJamie Gritton /* Look for a prison with the ID and with references. */ 5040b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 5041b38ff370SJamie Gritton if (pr->pr_id == addr && pr->pr_ref > 0) 5042b38ff370SJamie Gritton break; 5043b38ff370SJamie Gritton if (pr == NULL) 5044b38ff370SJamie Gritton /* Look again, without requiring a reference. */ 5045b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 5046b38ff370SJamie Gritton if (pr->pr_id == addr) 5047b38ff370SJamie Gritton break; 5048b38ff370SJamie Gritton if (pr == NULL) 5049b38ff370SJamie Gritton /* Assume address points to a valid prison. */ 5050b38ff370SJamie Gritton pr = (struct prison *)addr; 50510304c731SJamie Gritton } 5052b38ff370SJamie Gritton db_show_prison(pr); 5053b38ff370SJamie Gritton } 5054b38ff370SJamie Gritton 5055413628a7SBjoern A. Zeeb #endif /* DDB */ 5056