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 133*8cf955f3SMark Johnston /* 134*8cf955f3SMark Johnston * Handle jail teardown in a dedicated thread to avoid deadlocks from 135*8cf955f3SMark Johnston * vnet_destroy(). 136*8cf955f3SMark Johnston */ 137*8cf955f3SMark Johnston TASKQUEUE_DEFINE_THREAD(jail_remove); 138*8cf955f3SMark Johnston 139a7ad07bfSEdward Tomasz Napierala /* allprison, allprison_racct and lastprid are protected by allprison_lock. */ 140dc68a633SPawel Jakub Dawidek struct sx allprison_lock; 141b38ff370SJamie Gritton SX_SYSINIT(allprison_lock, &allprison_lock, "allprison"); 142b38ff370SJamie Gritton struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison); 143a7ad07bfSEdward Tomasz Napierala LIST_HEAD(, prison_racct) allprison_racct; 1442110d913SXin LI int lastprid = 0; 145ed31b3f4SJamie Gritton int lastdeadid = 0; 146fd7a8150SMike Barcroft 1477de883c8SJamie Gritton static int get_next_prid(struct prison **insprp); 148ed31b3f4SJamie Gritton static int get_next_deadid(struct prison **insprp); 149f7496dcaSJamie Gritton static int do_jail_attach(struct thread *td, struct prison *pr, int drflags); 150b3059e09SRobert Watson static void prison_complete(void *context, int pending); 151b38ff370SJamie Gritton static void prison_deref(struct prison *pr, int flags); 152c861373bSJamie Gritton static void prison_deref_kill(struct prison *pr, struct prisonlist *freeprison); 153f7496dcaSJamie Gritton static int prison_lock_xlock(struct prison *pr, int flags); 154a9f7455cSJamie Gritton static void prison_cleanup(struct prison *pr); 155f7496dcaSJamie Gritton static void prison_free_not_last(struct prison *pr); 156c861373bSJamie Gritton static void prison_proc_free_not_last(struct prison *pr); 1575ecb5444SMateusz Guzik static void prison_proc_relink(struct prison *opr, struct prison *npr, 1585ecb5444SMateusz Guzik struct proc *p); 1590fe74ae6SJamie Gritton static void prison_set_allow_locked(struct prison *pr, unsigned flag, 1600fe74ae6SJamie Gritton int enable); 1610304c731SJamie Gritton static char *prison_path(struct prison *pr1, struct prison *pr2); 162a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 163a7ad07bfSEdward Tomasz Napierala static void prison_racct_attach(struct prison *pr); 164c34bbd2aSEdward Tomasz Napierala static void prison_racct_modify(struct prison *pr); 165a7ad07bfSEdward Tomasz Napierala static void prison_racct_detach(struct prison *pr); 166a7ad07bfSEdward Tomasz Napierala #endif 167fd7a8150SMike Barcroft 168b38ff370SJamie Gritton /* Flags for prison_deref */ 1692a4b2251SJamie Gritton #define PD_DEREF 0x01 /* Decrement pr_ref */ 1702a4b2251SJamie Gritton #define PD_DEUREF 0x02 /* Decrement pr_uref */ 171c861373bSJamie Gritton #define PD_KILL 0x04 /* Remove jail, kill processes, etc */ 172c861373bSJamie Gritton #define PD_LOCKED 0x10 /* pr_mtx is held */ 173c861373bSJamie Gritton #define PD_LIST_SLOCKED 0x20 /* allprison_lock is held shared */ 174c861373bSJamie Gritton #define PD_LIST_XLOCKED 0x40 /* allprison_lock is held exclusive */ 175589e4c1dSJamie Gritton #define PD_OP_FLAGS 0x07 /* Operation flags */ 176589e4c1dSJamie Gritton #define PD_LOCK_FLAGS 0x70 /* Lock status flags */ 177fd7a8150SMike Barcroft 1780304c731SJamie Gritton /* 1795cc70397SBjoern A. Zeeb * Parameter names corresponding to PR_* flag values. Size values are for kvm 1805cc70397SBjoern A. Zeeb * as we cannot figure out the size of a sparse array, or an array without a 1815cc70397SBjoern A. Zeeb * terminating entry. 1820304c731SJamie Gritton */ 183672756aaSJamie Gritton static struct bool_flags pr_flag_bool[] = { 184672756aaSJamie Gritton {"persist", "nopersist", PR_PERSIST}, 185592bcae8SBjoern A. Zeeb #ifdef INET 186672756aaSJamie Gritton {"ip4.saddrsel", "ip4.nosaddrsel", PR_IP4_SADDRSEL}, 187592bcae8SBjoern A. Zeeb #endif 188592bcae8SBjoern A. Zeeb #ifdef INET6 189672756aaSJamie Gritton {"ip6.saddrsel", "ip6.nosaddrsel", PR_IP6_SADDRSEL}, 190592bcae8SBjoern A. Zeeb #endif 1910304c731SJamie Gritton }; 192672756aaSJamie Gritton const size_t pr_flag_bool_size = sizeof(pr_flag_bool); 1930304c731SJamie Gritton 194672756aaSJamie Gritton static struct jailsys_flags pr_flag_jailsys[] = { 1957cbf7213SJamie Gritton {"host", 0, PR_HOST}, 1967cbf7213SJamie Gritton #ifdef VIMAGE 1977cbf7213SJamie Gritton {"vnet", 0, PR_VNET}, 1987cbf7213SJamie Gritton #endif 1990304c731SJamie Gritton #ifdef INET 2006a3f2779SJamie Gritton {"ip4", PR_IP4_USER, PR_IP4_USER}, 2010304c731SJamie Gritton #endif 2020304c731SJamie Gritton #ifdef INET6 2036a3f2779SJamie Gritton {"ip6", PR_IP6_USER, PR_IP6_USER}, 204679e1390SJamie Gritton #endif 2050304c731SJamie Gritton }; 2065cc70397SBjoern A. Zeeb const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys); 2070304c731SJamie Gritton 2085d58f959SJamie Gritton /* 2095d58f959SJamie Gritton * Make this array full-size so dynamic parameters can be added. 2105d58f959SJamie Gritton * It is protected by prison0.mtx, but lockless reading is allowed 2115d58f959SJamie Gritton * with an atomic check of the flag values. 2125d58f959SJamie Gritton */ 2130e5c6bd4SJamie Gritton static struct bool_flags pr_flag_allow[NBBY * NBPW] = { 214672756aaSJamie Gritton {"allow.set_hostname", "allow.noset_hostname", PR_ALLOW_SET_HOSTNAME}, 215672756aaSJamie Gritton {"allow.sysvipc", "allow.nosysvipc", PR_ALLOW_SYSVIPC}, 216672756aaSJamie Gritton {"allow.raw_sockets", "allow.noraw_sockets", PR_ALLOW_RAW_SOCKETS}, 217672756aaSJamie Gritton {"allow.chflags", "allow.nochflags", PR_ALLOW_CHFLAGS}, 218672756aaSJamie Gritton {"allow.mount", "allow.nomount", PR_ALLOW_MOUNT}, 219672756aaSJamie Gritton {"allow.quotas", "allow.noquotas", PR_ALLOW_QUOTAS}, 220672756aaSJamie Gritton {"allow.socket_af", "allow.nosocket_af", PR_ALLOW_SOCKET_AF}, 221ccd6ac9fSAntoine Brodin {"allow.mlock", "allow.nomlock", PR_ALLOW_MLOCK}, 222672756aaSJamie Gritton {"allow.reserved_ports", "allow.noreserved_ports", 223672756aaSJamie Gritton PR_ALLOW_RESERVED_PORTS}, 224b19d66fdSJamie Gritton {"allow.read_msgbuf", "allow.noread_msgbuf", PR_ALLOW_READ_MSGBUF}, 225b3079544SJamie Gritton {"allow.unprivileged_proc_debug", "allow.nounprivileged_proc_debug", 226b3079544SJamie Gritton PR_ALLOW_UNPRIV_DEBUG}, 22705e1e482SMariusz Zaborski {"allow.suser", "allow.nosuser", PR_ALLOW_SUSER}, 228cbbb2203SRick Macklem #ifdef VIMAGE 229bba7a2e8SRick Macklem {"allow.nfsd", "allow.nonfsd", PR_ALLOW_NFSD}, 230bba7a2e8SRick Macklem #endif 231cb48780dSShawn Webb {"allow.extattr", "allow.noextattr", PR_ALLOW_EXTATTR}, 232d3bb35d4SMariusz Zaborski {"allow.adjtime", "allow.noadjtime", PR_ALLOW_ADJTIME}, 233d3bb35d4SMariusz Zaborski {"allow.settime", "allow.nosettime", PR_ALLOW_SETTIME}, 2340304c731SJamie Gritton }; 2355d58f959SJamie Gritton static unsigned pr_allow_all = PR_ALLOW_ALL_STATIC; 236672756aaSJamie Gritton const size_t pr_flag_allow_size = sizeof(pr_flag_allow); 2370304c731SJamie Gritton 238b3079544SJamie Gritton #define JAIL_DEFAULT_ALLOW (PR_ALLOW_SET_HOSTNAME | \ 239b3079544SJamie Gritton PR_ALLOW_RESERVED_PORTS | \ 24005e1e482SMariusz Zaborski PR_ALLOW_UNPRIV_DEBUG | \ 24105e1e482SMariusz Zaborski PR_ALLOW_SUSER) 24242bebd82SJamie Gritton #define JAIL_DEFAULT_ENFORCE_STATFS 2 243bf3db8aaSMartin Matuska #define JAIL_DEFAULT_DEVFS_RSNUM 0 2440304c731SJamie Gritton static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW; 24542bebd82SJamie Gritton static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 2460cc207a6SMartin Matuska static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM; 2470304c731SJamie Gritton #if defined(INET) || defined(INET6) 248e92e0574SJamie Gritton static unsigned jail_max_af_ips = 255; 2490304c731SJamie Gritton #endif 2500304c731SJamie Gritton 251b96bd95bSIan Lepore /* 252b96bd95bSIan Lepore * Initialize the parts of prison0 that can't be static-initialized with 253b96bd95bSIan Lepore * constants. This is called from proc0_init() after creating thread0 cpuset. 254b96bd95bSIan Lepore */ 255b96bd95bSIan Lepore void 256b96bd95bSIan Lepore prison0_init(void) 257b96bd95bSIan Lepore { 258c3188289SKyle Evans uint8_t *file, *data; 259c3188289SKyle Evans size_t size; 260d2ef3774SJessica Clarke char buf[sizeof(prison0.pr_hostuuid)]; 261d2ef3774SJessica Clarke bool valid; 262b96bd95bSIan Lepore 263b96bd95bSIan Lepore prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset); 264b96bd95bSIan Lepore prison0.pr_osreldate = osreldate; 265b96bd95bSIan Lepore strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease)); 266c3188289SKyle Evans 267c3188289SKyle Evans /* If we have a preloaded hostuuid, use it. */ 268c3188289SKyle Evans file = preload_search_by_type(PRISON0_HOSTUUID_MODULE); 269c3188289SKyle Evans if (file != NULL) { 270c3188289SKyle Evans data = preload_fetch_addr(file); 271c3188289SKyle Evans size = preload_fetch_size(file); 272c3188289SKyle Evans if (data != NULL) { 273c3188289SKyle Evans /* 274c3188289SKyle Evans * The preloaded data may include trailing whitespace, almost 275c3188289SKyle Evans * certainly a newline; skip over any whitespace or 276c3188289SKyle Evans * non-printable characters to be safe. 277c3188289SKyle Evans */ 278c3188289SKyle Evans while (size > 0 && data[size - 1] <= 0x20) { 279b6be9566SColin Percival size--; 280c3188289SKyle Evans } 281d2ef3774SJessica Clarke 282d2ef3774SJessica Clarke valid = false; 283d2ef3774SJessica Clarke 284d2ef3774SJessica Clarke /* 285d2ef3774SJessica Clarke * Not NUL-terminated when passed from loader, but 286d2ef3774SJessica Clarke * validate_uuid requires that due to using sscanf (as 287d2ef3774SJessica Clarke * does the subsequent strlcpy, since it still reads 288d2ef3774SJessica Clarke * past the given size to return the true length); 289d2ef3774SJessica Clarke * bounce to a temporary buffer to fix. 290d2ef3774SJessica Clarke */ 291d2ef3774SJessica Clarke if (size >= sizeof(buf)) 292d2ef3774SJessica Clarke goto done; 293d2ef3774SJessica Clarke 294d2ef3774SJessica Clarke memcpy(buf, data, size); 295d2ef3774SJessica Clarke buf[size] = '\0'; 296d2ef3774SJessica Clarke 297d2ef3774SJessica Clarke if (validate_uuid(buf, size, NULL, 0) != 0) 298d2ef3774SJessica Clarke goto done; 299d2ef3774SJessica Clarke 300d2ef3774SJessica Clarke valid = true; 301d2ef3774SJessica Clarke (void)strlcpy(prison0.pr_hostuuid, buf, 302d2ef3774SJessica Clarke sizeof(prison0.pr_hostuuid)); 303d2ef3774SJessica Clarke 304d2ef3774SJessica Clarke done: 305d2ef3774SJessica Clarke if (bootverbose && !valid) { 306330f110bSColin Percival printf("hostuuid: preload data malformed: '%.*s'\n", 307330f110bSColin Percival (int)size, data); 308c3188289SKyle Evans } 309c3188289SKyle Evans } 310c3188289SKyle Evans } 311c3188289SKyle Evans if (bootverbose) 312c3188289SKyle Evans printf("hostuuid: using %s\n", prison0.pr_hostuuid); 313b96bd95bSIan Lepore } 314b96bd95bSIan Lepore 315116734c4SMatthew Dillon /* 3169ddb7954SMike Barcroft * struct jail_args { 3179ddb7954SMike Barcroft * struct jail *jail; 3189ddb7954SMike Barcroft * }; 319116734c4SMatthew Dillon */ 32075c13541SPoul-Henning Kamp int 321c542c43eSJamie Gritton sys_jail(struct thread *td, struct jail_args *uap) 32275c13541SPoul-Henning Kamp { 323413628a7SBjoern A. Zeeb uint32_t version; 324413628a7SBjoern A. Zeeb int error; 3250304c731SJamie Gritton struct jail j; 326413628a7SBjoern A. Zeeb 327413628a7SBjoern A. Zeeb error = copyin(uap->jail, &version, sizeof(uint32_t)); 328413628a7SBjoern A. Zeeb if (error) 329413628a7SBjoern A. Zeeb return (error); 330413628a7SBjoern A. Zeeb 331413628a7SBjoern A. Zeeb switch (version) { 332413628a7SBjoern A. Zeeb case 0: 333413628a7SBjoern A. Zeeb { 334413628a7SBjoern A. Zeeb struct jail_v0 j0; 335413628a7SBjoern A. Zeeb 3360304c731SJamie Gritton /* FreeBSD single IPv4 jails. */ 3370304c731SJamie Gritton bzero(&j, sizeof(struct jail)); 338413628a7SBjoern A. Zeeb error = copyin(uap->jail, &j0, sizeof(struct jail_v0)); 339413628a7SBjoern A. Zeeb if (error) 340413628a7SBjoern A. Zeeb return (error); 3410304c731SJamie Gritton j.version = j0.version; 3420304c731SJamie Gritton j.path = j0.path; 3430304c731SJamie Gritton j.hostname = j0.hostname; 344b5019bc4SPeter Wemm j.ip4s = htonl(j0.ip_number); /* jail_v0 is host order */ 345413628a7SBjoern A. Zeeb break; 346413628a7SBjoern A. Zeeb } 347413628a7SBjoern A. Zeeb 348413628a7SBjoern A. Zeeb case 1: 349413628a7SBjoern A. Zeeb /* 350413628a7SBjoern A. Zeeb * Version 1 was used by multi-IPv4 jail implementations 351413628a7SBjoern A. Zeeb * that never made it into the official kernel. 352413628a7SBjoern A. Zeeb */ 353413628a7SBjoern A. Zeeb return (EINVAL); 354413628a7SBjoern A. Zeeb 355413628a7SBjoern A. Zeeb case 2: /* JAIL_API_VERSION */ 356413628a7SBjoern A. Zeeb /* FreeBSD multi-IPv4/IPv6,noIP jails. */ 357413628a7SBjoern A. Zeeb error = copyin(uap->jail, &j, sizeof(struct jail)); 358413628a7SBjoern A. Zeeb if (error) 359413628a7SBjoern A. Zeeb return (error); 3600304c731SJamie Gritton break; 3610304c731SJamie Gritton 3620304c731SJamie Gritton default: 3630304c731SJamie Gritton /* Sci-Fi jails are not supported, sorry. */ 3640304c731SJamie Gritton return (EINVAL); 3650304c731SJamie Gritton } 366c542c43eSJamie Gritton return (kern_jail(td, &j)); 3670304c731SJamie Gritton } 3680304c731SJamie Gritton 3690304c731SJamie Gritton int 370c542c43eSJamie Gritton kern_jail(struct thread *td, struct jail *j) 3710304c731SJamie Gritton { 372c542c43eSJamie Gritton struct iovec optiov[2 * (4 + nitems(pr_flag_allow) 373e92e0574SJamie Gritton #ifdef INET 374e92e0574SJamie Gritton + 1 375e92e0574SJamie Gritton #endif 376e92e0574SJamie Gritton #ifdef INET6 377e92e0574SJamie Gritton + 1 378e92e0574SJamie Gritton #endif 379e92e0574SJamie Gritton )]; 3800304c731SJamie Gritton struct uio opt; 3810304c731SJamie Gritton char *u_path, *u_hostname, *u_name; 382672756aaSJamie Gritton struct bool_flags *bf; 3830304c731SJamie Gritton #ifdef INET 384e92e0574SJamie Gritton uint32_t ip4s; 3850304c731SJamie Gritton struct in_addr *u_ip4; 3860304c731SJamie Gritton #endif 3870304c731SJamie Gritton #ifdef INET6 3880304c731SJamie Gritton struct in6_addr *u_ip6; 3890304c731SJamie Gritton #endif 3900304c731SJamie Gritton size_t tmplen; 391c542c43eSJamie Gritton int error, enforce_statfs; 3920304c731SJamie Gritton 3930304c731SJamie Gritton bzero(&optiov, sizeof(optiov)); 3940304c731SJamie Gritton opt.uio_iov = optiov; 3950304c731SJamie Gritton opt.uio_iovcnt = 0; 3960304c731SJamie Gritton opt.uio_offset = -1; 3970304c731SJamie Gritton opt.uio_resid = -1; 3980304c731SJamie Gritton opt.uio_segflg = UIO_SYSSPACE; 3990304c731SJamie Gritton opt.uio_rw = UIO_READ; 4000304c731SJamie Gritton opt.uio_td = td; 4010304c731SJamie Gritton 4020304c731SJamie Gritton /* Set permissions for top-level jails from sysctls. */ 4030304c731SJamie Gritton if (!jailed(td->td_ucred)) { 404672756aaSJamie Gritton for (bf = pr_flag_allow; 4050e5c6bd4SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 4065d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 407672756aaSJamie Gritton bf++) { 408672756aaSJamie Gritton optiov[opt.uio_iovcnt].iov_base = __DECONST(char *, 409672756aaSJamie Gritton (jail_default_allow & bf->flag) 410672756aaSJamie Gritton ? bf->name : bf->noname); 4110304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = 4120304c731SJamie Gritton strlen(optiov[opt.uio_iovcnt].iov_base) + 1; 4130304c731SJamie Gritton opt.uio_iovcnt += 2; 4140304c731SJamie Gritton } 4150304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "enforce_statfs"; 4160304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs"); 4170304c731SJamie Gritton opt.uio_iovcnt++; 4180304c731SJamie Gritton enforce_statfs = jail_default_enforce_statfs; 4190304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = &enforce_statfs; 4200304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs); 4210304c731SJamie Gritton opt.uio_iovcnt++; 4220304c731SJamie Gritton } 4230304c731SJamie Gritton 424b38ff370SJamie Gritton tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN; 425b38ff370SJamie Gritton #ifdef INET 4260304c731SJamie Gritton ip4s = (j->version == 0) ? 1 : j->ip4s; 4270304c731SJamie Gritton if (ip4s > jail_max_af_ips) 428b38ff370SJamie Gritton return (EINVAL); 4290304c731SJamie Gritton tmplen += ip4s * sizeof(struct in_addr); 430b38ff370SJamie Gritton #else 4310304c731SJamie Gritton if (j->ip4s > 0) 432b38ff370SJamie Gritton return (EINVAL); 433b38ff370SJamie Gritton #endif 434b38ff370SJamie Gritton #ifdef INET6 4350304c731SJamie Gritton if (j->ip6s > jail_max_af_ips) 436b38ff370SJamie Gritton return (EINVAL); 4370304c731SJamie Gritton tmplen += j->ip6s * sizeof(struct in6_addr); 438b38ff370SJamie Gritton #else 4390304c731SJamie Gritton if (j->ip6s > 0) 440b38ff370SJamie Gritton return (EINVAL); 441b38ff370SJamie Gritton #endif 442b38ff370SJamie Gritton u_path = malloc(tmplen, M_TEMP, M_WAITOK); 443b38ff370SJamie Gritton u_hostname = u_path + MAXPATHLEN; 444b38ff370SJamie Gritton u_name = u_hostname + MAXHOSTNAMELEN; 445b38ff370SJamie Gritton #ifdef INET 446b38ff370SJamie Gritton u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN); 447b38ff370SJamie Gritton #endif 448b38ff370SJamie Gritton #ifdef INET6 449b38ff370SJamie Gritton #ifdef INET 4500304c731SJamie Gritton u_ip6 = (struct in6_addr *)(u_ip4 + ip4s); 451b38ff370SJamie Gritton #else 452b38ff370SJamie Gritton u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN); 453b38ff370SJamie Gritton #endif 454b38ff370SJamie Gritton #endif 4550304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "path"; 4560304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("path"); 4570304c731SJamie Gritton opt.uio_iovcnt++; 4580304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_path; 4590304c731SJamie Gritton error = copyinstr(j->path, u_path, MAXPATHLEN, 4600304c731SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 461b38ff370SJamie Gritton if (error) { 462b38ff370SJamie Gritton free(u_path, M_TEMP); 463b38ff370SJamie Gritton return (error); 464b38ff370SJamie Gritton } 4650304c731SJamie Gritton opt.uio_iovcnt++; 4660304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "host.hostname"; 4670304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname"); 4680304c731SJamie Gritton opt.uio_iovcnt++; 4690304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_hostname; 4700304c731SJamie Gritton error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN, 4710304c731SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 472b38ff370SJamie Gritton if (error) { 473b38ff370SJamie Gritton free(u_path, M_TEMP); 474b38ff370SJamie Gritton return (error); 475b38ff370SJamie Gritton } 4760304c731SJamie Gritton opt.uio_iovcnt++; 4770304c731SJamie Gritton if (j->jailname != NULL) { 478b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "name"; 479b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("name"); 480b38ff370SJamie Gritton opt.uio_iovcnt++; 481b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_name; 4820304c731SJamie Gritton error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN, 483b38ff370SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 484b38ff370SJamie Gritton if (error) { 485b38ff370SJamie Gritton free(u_path, M_TEMP); 486b38ff370SJamie Gritton return (error); 487b38ff370SJamie Gritton } 488b38ff370SJamie Gritton opt.uio_iovcnt++; 489b38ff370SJamie Gritton } 490b38ff370SJamie Gritton #ifdef INET 491b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "ip4.addr"; 492b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr"); 493b38ff370SJamie Gritton opt.uio_iovcnt++; 494b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_ip4; 4950304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr); 4960304c731SJamie Gritton if (j->version == 0) 4970304c731SJamie Gritton u_ip4->s_addr = j->ip4s; 4980304c731SJamie Gritton else { 4990304c731SJamie Gritton error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len); 500b38ff370SJamie Gritton if (error) { 501b38ff370SJamie Gritton free(u_path, M_TEMP); 502b38ff370SJamie Gritton return (error); 503b38ff370SJamie Gritton } 5040304c731SJamie Gritton } 505b38ff370SJamie Gritton opt.uio_iovcnt++; 506b38ff370SJamie Gritton #endif 507b38ff370SJamie Gritton #ifdef INET6 508b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "ip6.addr"; 509b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr"); 510b38ff370SJamie Gritton opt.uio_iovcnt++; 511b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_ip6; 5120304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr); 5130304c731SJamie Gritton error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len); 514b38ff370SJamie Gritton if (error) { 515b38ff370SJamie Gritton free(u_path, M_TEMP); 516b38ff370SJamie Gritton return (error); 517b38ff370SJamie Gritton } 518b38ff370SJamie Gritton opt.uio_iovcnt++; 519b38ff370SJamie Gritton #endif 52002abd400SPedro F. Giffuni KASSERT(opt.uio_iovcnt <= nitems(optiov), 5210304c731SJamie Gritton ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt)); 522b38ff370SJamie Gritton error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH); 523b38ff370SJamie Gritton free(u_path, M_TEMP); 524b38ff370SJamie Gritton return (error); 525b38ff370SJamie Gritton } 526b38ff370SJamie Gritton 527b38ff370SJamie Gritton /* 528b38ff370SJamie Gritton * struct jail_set_args { 529b38ff370SJamie Gritton * struct iovec *iovp; 530b38ff370SJamie Gritton * unsigned int iovcnt; 531b38ff370SJamie Gritton * int flags; 532b38ff370SJamie Gritton * }; 533b38ff370SJamie Gritton */ 534b38ff370SJamie Gritton int 5358451d0ddSKip Macy sys_jail_set(struct thread *td, struct jail_set_args *uap) 536b38ff370SJamie Gritton { 537b38ff370SJamie Gritton struct uio *auio; 538b38ff370SJamie Gritton int error; 539b38ff370SJamie Gritton 540b38ff370SJamie Gritton /* Check that we have an even number of iovecs. */ 541b38ff370SJamie Gritton if (uap->iovcnt & 1) 542b38ff370SJamie Gritton return (EINVAL); 543b38ff370SJamie Gritton 544b38ff370SJamie Gritton error = copyinuio(uap->iovp, uap->iovcnt, &auio); 545b38ff370SJamie Gritton if (error) 546b38ff370SJamie Gritton return (error); 547b38ff370SJamie Gritton error = kern_jail_set(td, auio, uap->flags); 54861cc4830SAlfredo Mazzinghi freeuio(auio); 549b38ff370SJamie Gritton return (error); 550413628a7SBjoern A. Zeeb } 551413628a7SBjoern A. Zeeb 552eb8dcdeaSGleb Smirnoff #if defined(INET) || defined(INET6) 553eb8dcdeaSGleb Smirnoff typedef int prison_addr_cmp_t(const void *, const void *); 554eb8dcdeaSGleb Smirnoff typedef bool prison_addr_valid_t(const void *); 555eb8dcdeaSGleb Smirnoff static const struct pr_family { 556eb8dcdeaSGleb Smirnoff size_t size; 557eb8dcdeaSGleb Smirnoff prison_addr_cmp_t *cmp; 558eb8dcdeaSGleb Smirnoff prison_addr_valid_t *valid; 559eb8dcdeaSGleb Smirnoff int ip_flag; 560eb8dcdeaSGleb Smirnoff } pr_families[PR_FAMILY_MAX] = { 561eb8dcdeaSGleb Smirnoff #ifdef INET 562eb8dcdeaSGleb Smirnoff [PR_INET] = { 563eb8dcdeaSGleb Smirnoff .size = sizeof(struct in_addr), 564eb8dcdeaSGleb Smirnoff .cmp = prison_qcmp_v4, 565eb8dcdeaSGleb Smirnoff .valid = prison_valid_v4, 566eb8dcdeaSGleb Smirnoff .ip_flag = PR_IP4_USER, 567eb8dcdeaSGleb Smirnoff }, 568eb8dcdeaSGleb Smirnoff #endif 569eb8dcdeaSGleb Smirnoff #ifdef INET6 570eb8dcdeaSGleb Smirnoff [PR_INET6] = { 571eb8dcdeaSGleb Smirnoff .size = sizeof(struct in6_addr), 572eb8dcdeaSGleb Smirnoff .cmp = prison_qcmp_v6, 573eb8dcdeaSGleb Smirnoff .valid = prison_valid_v6, 574eb8dcdeaSGleb Smirnoff .ip_flag = PR_IP6_USER, 575eb8dcdeaSGleb Smirnoff }, 576eb8dcdeaSGleb Smirnoff #endif 577eb8dcdeaSGleb Smirnoff }; 578eb8dcdeaSGleb Smirnoff 579eb8dcdeaSGleb Smirnoff /* 580eb8dcdeaSGleb Smirnoff * Network address lists (pr_addrs) allocation for jails. The addresses 581eb8dcdeaSGleb Smirnoff * are accessed locklessly by the network stack, thus need to be protected by 582eb8dcdeaSGleb Smirnoff * the network epoch. 583eb8dcdeaSGleb Smirnoff */ 584eb8dcdeaSGleb Smirnoff struct prison_ip { 585eb8dcdeaSGleb Smirnoff struct epoch_context ctx; 586eb8dcdeaSGleb Smirnoff uint32_t ips; 587eb8dcdeaSGleb Smirnoff #ifdef FUTURE_C 588500f82d6SZhenlei Huang /* 589500f82d6SZhenlei Huang * XXX Variable-length automatic arrays in union may be 590500f82d6SZhenlei Huang * supported in future C. 591500f82d6SZhenlei Huang */ 592eb8dcdeaSGleb Smirnoff union { 593500f82d6SZhenlei Huang char pr_ip[]; 594eb8dcdeaSGleb Smirnoff struct in_addr pr_ip4[]; 595eb8dcdeaSGleb Smirnoff struct in6_addr pr_ip6[]; 596eb8dcdeaSGleb Smirnoff }; 597eb8dcdeaSGleb Smirnoff #else /* No future C :( */ 598500f82d6SZhenlei Huang char pr_ip[]; 599eb8dcdeaSGleb Smirnoff #endif 600eb8dcdeaSGleb Smirnoff }; 601eb8dcdeaSGleb Smirnoff 602500f82d6SZhenlei Huang static char * 603500f82d6SZhenlei Huang PR_IP(struct prison_ip *pip, const pr_family_t af, int idx) 604500f82d6SZhenlei Huang { 605500f82d6SZhenlei Huang MPASS(pip); 606500f82d6SZhenlei Huang MPASS(af < PR_FAMILY_MAX); 607500f82d6SZhenlei Huang MPASS(idx >= 0 && idx < pip->ips); 608500f82d6SZhenlei Huang 609500f82d6SZhenlei Huang return (pip->pr_ip + pr_families[af].size * idx); 610500f82d6SZhenlei Huang } 611500f82d6SZhenlei Huang 612eb8dcdeaSGleb Smirnoff static struct prison_ip * 613eb8dcdeaSGleb Smirnoff prison_ip_alloc(const pr_family_t af, uint32_t cnt, int flags) 614eb8dcdeaSGleb Smirnoff { 615eb8dcdeaSGleb Smirnoff struct prison_ip *pip; 616eb8dcdeaSGleb Smirnoff 617eb8dcdeaSGleb Smirnoff pip = malloc(sizeof(struct prison_ip) + cnt * pr_families[af].size, 618eb8dcdeaSGleb Smirnoff M_PRISON, flags); 619eb8dcdeaSGleb Smirnoff if (pip != NULL) 620eb8dcdeaSGleb Smirnoff pip->ips = cnt; 621eb8dcdeaSGleb Smirnoff return (pip); 622eb8dcdeaSGleb Smirnoff } 623eb8dcdeaSGleb Smirnoff 624eb8dcdeaSGleb Smirnoff /* 625eb8dcdeaSGleb Smirnoff * Allocate and copyin user supplied address list, sorting and validating. 626eb8dcdeaSGleb Smirnoff * kern_jail_set() helper. 627eb8dcdeaSGleb Smirnoff */ 628eb8dcdeaSGleb Smirnoff static struct prison_ip * 629eb8dcdeaSGleb Smirnoff prison_ip_copyin(const pr_family_t af, void *op, uint32_t cnt) 630eb8dcdeaSGleb Smirnoff { 631eb8dcdeaSGleb Smirnoff prison_addr_cmp_t *const cmp = pr_families[af].cmp; 632eb8dcdeaSGleb Smirnoff const size_t size = pr_families[af].size; 633eb8dcdeaSGleb Smirnoff struct prison_ip *pip; 634eb8dcdeaSGleb Smirnoff 635eb8dcdeaSGleb Smirnoff pip = prison_ip_alloc(af, cnt, M_WAITOK); 636500f82d6SZhenlei Huang bcopy(op, pip->pr_ip, cnt * size); 637eb8dcdeaSGleb Smirnoff /* 638eb8dcdeaSGleb Smirnoff * IP addresses are all sorted but ip[0] to preserve 639eb8dcdeaSGleb Smirnoff * the primary IP address as given from userland. 640eb8dcdeaSGleb Smirnoff * This special IP is used for unbound outgoing 641eb8dcdeaSGleb Smirnoff * connections as well for "loopback" traffic in case 642eb8dcdeaSGleb Smirnoff * source address selection cannot find any more fitting 643eb8dcdeaSGleb Smirnoff * address to connect from. 644eb8dcdeaSGleb Smirnoff */ 645eb8dcdeaSGleb Smirnoff if (cnt > 1) 6462c33b456SZhenlei Huang qsort(PR_IP(pip, af, 1), cnt - 1, size, cmp); 647eb8dcdeaSGleb Smirnoff /* 648eb8dcdeaSGleb Smirnoff * Check for duplicate addresses and do some simple 649eb8dcdeaSGleb Smirnoff * zero and broadcast checks. If users give other bogus 650eb8dcdeaSGleb Smirnoff * addresses it is their problem. 651eb8dcdeaSGleb Smirnoff */ 652eb8dcdeaSGleb Smirnoff for (int i = 0; i < cnt; i++) { 653500f82d6SZhenlei Huang if (!pr_families[af].valid(PR_IP(pip, af, i))) { 654eb8dcdeaSGleb Smirnoff free(pip, M_PRISON); 655eb8dcdeaSGleb Smirnoff return (NULL); 656eb8dcdeaSGleb Smirnoff } 657eb8dcdeaSGleb Smirnoff if (i + 1 < cnt && 658500f82d6SZhenlei Huang (cmp(PR_IP(pip, af, 0), PR_IP(pip, af, i + 1)) == 0 || 659500f82d6SZhenlei Huang cmp(PR_IP(pip, af, i), PR_IP(pip, af, i + 1)) == 0)) { 660eb8dcdeaSGleb Smirnoff free(pip, M_PRISON); 661eb8dcdeaSGleb Smirnoff return (NULL); 662eb8dcdeaSGleb Smirnoff } 663eb8dcdeaSGleb Smirnoff } 664eb8dcdeaSGleb Smirnoff 665eb8dcdeaSGleb Smirnoff return (pip); 666eb8dcdeaSGleb Smirnoff } 667eb8dcdeaSGleb Smirnoff 668eb8dcdeaSGleb Smirnoff /* 669eb8dcdeaSGleb Smirnoff * Allocate and dup parent prison address list. 670eb8dcdeaSGleb Smirnoff * kern_jail_set() helper. 671eb8dcdeaSGleb Smirnoff */ 672eb8dcdeaSGleb Smirnoff static void 673eb8dcdeaSGleb Smirnoff prison_ip_dup(struct prison *ppr, struct prison *pr, const pr_family_t af) 674eb8dcdeaSGleb Smirnoff { 6752c33b456SZhenlei Huang const struct prison_ip *ppip = ppr->pr_addrs[af]; 6762c33b456SZhenlei Huang struct prison_ip *pip; 677eb8dcdeaSGleb Smirnoff 6782c33b456SZhenlei Huang if (ppip != NULL) { 6792c33b456SZhenlei Huang pip = prison_ip_alloc(af, ppip->ips, M_WAITOK); 6802c33b456SZhenlei Huang bcopy(ppip->pr_ip, pip->pr_ip, pip->ips * pr_families[af].size); 6812c33b456SZhenlei Huang pr->pr_addrs[af] = pip; 682eb8dcdeaSGleb Smirnoff } 683eb8dcdeaSGleb Smirnoff } 684eb8dcdeaSGleb Smirnoff 685eb8dcdeaSGleb Smirnoff /* 686eb8dcdeaSGleb Smirnoff * Make sure the new set of IP addresses is a subset of the parent's list. 687eb8dcdeaSGleb Smirnoff * Don't worry about the parent being unlocked, as any setting is done with 688eb8dcdeaSGleb Smirnoff * allprison_lock held. 689eb8dcdeaSGleb Smirnoff * kern_jail_set() helper. 690eb8dcdeaSGleb Smirnoff */ 691eb8dcdeaSGleb Smirnoff static bool 692500f82d6SZhenlei Huang prison_ip_parent_match(struct prison_ip *ppip, struct prison_ip *pip, 693500f82d6SZhenlei Huang const pr_family_t af) 694eb8dcdeaSGleb Smirnoff { 695eb8dcdeaSGleb Smirnoff prison_addr_cmp_t *const cmp = pr_families[af].cmp; 696eb8dcdeaSGleb Smirnoff int i, j; 697eb8dcdeaSGleb Smirnoff 698eb8dcdeaSGleb Smirnoff if (ppip == NULL) 699eb8dcdeaSGleb Smirnoff return (false); 700eb8dcdeaSGleb Smirnoff 701eb8dcdeaSGleb Smirnoff for (i = 0; i < ppip->ips; i++) 702500f82d6SZhenlei Huang if (cmp(PR_IP(pip, af, 0), PR_IP(ppip, af, i)) == 0) 703eb8dcdeaSGleb Smirnoff break; 704eb8dcdeaSGleb Smirnoff 705eb8dcdeaSGleb Smirnoff if (i == ppip->ips) 706eb8dcdeaSGleb Smirnoff /* Main address not present in parent. */ 707eb8dcdeaSGleb Smirnoff return (false); 708eb8dcdeaSGleb Smirnoff 709eb8dcdeaSGleb Smirnoff if (pip->ips > 1) { 710eb8dcdeaSGleb Smirnoff for (i = j = 1; i < pip->ips; i++) { 711500f82d6SZhenlei Huang if (cmp(PR_IP(pip, af, i), PR_IP(ppip, af, 0)) == 0) 712eb8dcdeaSGleb Smirnoff /* Equals to parent primary address. */ 713eb8dcdeaSGleb Smirnoff continue; 714eb8dcdeaSGleb Smirnoff for (; j < ppip->ips; j++) 715500f82d6SZhenlei Huang if (cmp(PR_IP(pip, af, i), 716500f82d6SZhenlei Huang PR_IP(ppip, af, j)) == 0) 717eb8dcdeaSGleb Smirnoff break; 718eb8dcdeaSGleb Smirnoff if (j == ppip->ips) 719eb8dcdeaSGleb Smirnoff break; 720eb8dcdeaSGleb Smirnoff } 721eb8dcdeaSGleb Smirnoff if (j == ppip->ips) 722eb8dcdeaSGleb Smirnoff /* Address not present in parent. */ 723eb8dcdeaSGleb Smirnoff return (false); 724eb8dcdeaSGleb Smirnoff } 725eb8dcdeaSGleb Smirnoff return (true); 726eb8dcdeaSGleb Smirnoff } 727eb8dcdeaSGleb Smirnoff 728eb8dcdeaSGleb Smirnoff /* 729eb8dcdeaSGleb Smirnoff * Check for conflicting IP addresses. We permit them if there is no more 730eb8dcdeaSGleb Smirnoff * than one IP on each jail. If there is a duplicate on a jail with more 731eb8dcdeaSGleb Smirnoff * than one IP stop checking and return error. 732eb8dcdeaSGleb Smirnoff * kern_jail_set() helper. 733eb8dcdeaSGleb Smirnoff */ 734eb8dcdeaSGleb Smirnoff static bool 735eb8dcdeaSGleb Smirnoff prison_ip_conflict_check(const struct prison *ppr, const struct prison *pr, 736500f82d6SZhenlei Huang struct prison_ip *pip, pr_family_t af) 737eb8dcdeaSGleb Smirnoff { 738eb8dcdeaSGleb Smirnoff const struct prison *tppr, *tpr; 739eb8dcdeaSGleb Smirnoff int descend; 740eb8dcdeaSGleb Smirnoff 741eb8dcdeaSGleb Smirnoff #ifdef VIMAGE 742eb8dcdeaSGleb Smirnoff for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent) 743eb8dcdeaSGleb Smirnoff if (tppr->pr_flags & PR_VNET) 744eb8dcdeaSGleb Smirnoff break; 745eb8dcdeaSGleb Smirnoff #else 746eb8dcdeaSGleb Smirnoff tppr = &prison0; 747eb8dcdeaSGleb Smirnoff #endif 748eb8dcdeaSGleb Smirnoff FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 749eb8dcdeaSGleb Smirnoff if (tpr == pr || 750eb8dcdeaSGleb Smirnoff #ifdef VIMAGE 751eb8dcdeaSGleb Smirnoff (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 752eb8dcdeaSGleb Smirnoff #endif 753eb8dcdeaSGleb Smirnoff !prison_isalive(tpr)) { 754eb8dcdeaSGleb Smirnoff descend = 0; 755eb8dcdeaSGleb Smirnoff continue; 756eb8dcdeaSGleb Smirnoff } 757eb8dcdeaSGleb Smirnoff if (!(tpr->pr_flags & pr_families[af].ip_flag)) 758eb8dcdeaSGleb Smirnoff continue; 759eb8dcdeaSGleb Smirnoff descend = 0; 760eb8dcdeaSGleb Smirnoff if (tpr->pr_addrs[af] == NULL || 761eb8dcdeaSGleb Smirnoff (pip->ips == 1 && tpr->pr_addrs[af]->ips == 1)) 762eb8dcdeaSGleb Smirnoff continue; 763eb8dcdeaSGleb Smirnoff for (int i = 0; i < pip->ips; i++) 764500f82d6SZhenlei Huang if (prison_ip_check(tpr, af, PR_IP(pip, af, i)) == 0) 765eb8dcdeaSGleb Smirnoff return (false); 766eb8dcdeaSGleb Smirnoff } 767eb8dcdeaSGleb Smirnoff 768eb8dcdeaSGleb Smirnoff return (true); 769eb8dcdeaSGleb Smirnoff } 770eb8dcdeaSGleb Smirnoff 771eb8dcdeaSGleb Smirnoff _Static_assert(offsetof(struct prison_ip, ctx) == 0, 772eb8dcdeaSGleb Smirnoff "prison must start with epoch context"); 773eb8dcdeaSGleb Smirnoff static void 774eb8dcdeaSGleb Smirnoff prison_ip_free_deferred(epoch_context_t ctx) 775eb8dcdeaSGleb Smirnoff { 776eb8dcdeaSGleb Smirnoff 777eb8dcdeaSGleb Smirnoff free(ctx, M_PRISON); 778eb8dcdeaSGleb Smirnoff } 779eb8dcdeaSGleb Smirnoff 780eb8dcdeaSGleb Smirnoff static void 781eb8dcdeaSGleb Smirnoff prison_ip_free(struct prison_ip *pip) 782eb8dcdeaSGleb Smirnoff { 783eb8dcdeaSGleb Smirnoff 784eb8dcdeaSGleb Smirnoff if (pip != NULL) 785eb8dcdeaSGleb Smirnoff NET_EPOCH_CALL(prison_ip_free_deferred, &pip->ctx); 786eb8dcdeaSGleb Smirnoff } 787eb8dcdeaSGleb Smirnoff 788eb8dcdeaSGleb Smirnoff static void 789eb8dcdeaSGleb Smirnoff prison_ip_set(struct prison *pr, const pr_family_t af, struct prison_ip *new) 790eb8dcdeaSGleb Smirnoff { 791eb8dcdeaSGleb Smirnoff struct prison_ip **mem, *old; 792eb8dcdeaSGleb Smirnoff 793eb8dcdeaSGleb Smirnoff mtx_assert(&pr->pr_mtx, MA_OWNED); 794eb8dcdeaSGleb Smirnoff 795eb8dcdeaSGleb Smirnoff mem = &pr->pr_addrs[af]; 796eb8dcdeaSGleb Smirnoff 797eb8dcdeaSGleb Smirnoff old = *mem; 79827202b98SMark Johnston atomic_store_ptr(mem, new); 799eb8dcdeaSGleb Smirnoff prison_ip_free(old); 800eb8dcdeaSGleb Smirnoff } 801eb8dcdeaSGleb Smirnoff 802eb8dcdeaSGleb Smirnoff /* 803eb8dcdeaSGleb Smirnoff * Restrict a prison's IP address list with its parent's, possibly replacing 8048bce8d28SZhenlei Huang * it. Return true if succeed, otherwise should redo. 805eb8dcdeaSGleb Smirnoff * kern_jail_set() helper. 806eb8dcdeaSGleb Smirnoff */ 807eb8dcdeaSGleb Smirnoff static bool 808eb8dcdeaSGleb Smirnoff prison_ip_restrict(struct prison *pr, const pr_family_t af, 8098bce8d28SZhenlei Huang struct prison_ip **newp) 810eb8dcdeaSGleb Smirnoff { 811500f82d6SZhenlei Huang struct prison_ip *ppip = pr->pr_parent->pr_addrs[af]; 812500f82d6SZhenlei Huang struct prison_ip *pip = pr->pr_addrs[af]; 813eb8dcdeaSGleb Smirnoff int (*const cmp)(const void *, const void *) = pr_families[af].cmp; 814eb8dcdeaSGleb Smirnoff const size_t size = pr_families[af].size; 8158bce8d28SZhenlei Huang struct prison_ip *new = newp != NULL ? *newp : NULL; 816eb8dcdeaSGleb Smirnoff uint32_t ips; 817eb8dcdeaSGleb Smirnoff 818eb8dcdeaSGleb Smirnoff mtx_assert(&pr->pr_mtx, MA_OWNED); 819eb8dcdeaSGleb Smirnoff 820eb8dcdeaSGleb Smirnoff /* 821eb8dcdeaSGleb Smirnoff * Due to epoch-synchronized access to the IP address lists we always 822eb8dcdeaSGleb Smirnoff * allocate a new list even if the old one has enough space. We could 823eb8dcdeaSGleb Smirnoff * atomically update an IPv4 address inside a list, but that would 824eb8dcdeaSGleb Smirnoff * screw up sorting, and in case of IPv6 we can't even atomically write 825eb8dcdeaSGleb Smirnoff * one. 826eb8dcdeaSGleb Smirnoff */ 82789ddfbbaSZhenlei Huang if (ppip == NULL) { 82889ddfbbaSZhenlei Huang if (pip != NULL) 829eb8dcdeaSGleb Smirnoff prison_ip_set(pr, af, NULL); 8308bce8d28SZhenlei Huang return (true); 831eb8dcdeaSGleb Smirnoff } 83289ddfbbaSZhenlei Huang 833eb8dcdeaSGleb Smirnoff if (!(pr->pr_flags & pr_families[af].ip_flag)) { 83489ddfbbaSZhenlei Huang if (new == NULL) { 83589ddfbbaSZhenlei Huang new = prison_ip_alloc(af, ppip->ips, M_NOWAIT); 83689ddfbbaSZhenlei Huang if (new == NULL) 8378bce8d28SZhenlei Huang return (false); /* Redo */ 8388bce8d28SZhenlei Huang } 839eb8dcdeaSGleb Smirnoff /* This has no user settings, so just copy the parent's list. */ 84089ddfbbaSZhenlei Huang MPASS(new->ips == ppip->ips); 841500f82d6SZhenlei Huang bcopy(ppip->pr_ip, new->pr_ip, ppip->ips * size); 84289ddfbbaSZhenlei Huang prison_ip_set(pr, af, new); 8438bce8d28SZhenlei Huang if (newp != NULL) 8448bce8d28SZhenlei Huang *newp = NULL; /* Used */ 84589ddfbbaSZhenlei Huang } else if (pip != NULL) { 846eb8dcdeaSGleb Smirnoff /* Remove addresses that aren't in the parent. */ 847eb8dcdeaSGleb Smirnoff int i; 848eb8dcdeaSGleb Smirnoff 849eb8dcdeaSGleb Smirnoff i = 0; /* index in pip */ 850eb8dcdeaSGleb Smirnoff ips = 0; /* index in new */ 851eb8dcdeaSGleb Smirnoff 85289ddfbbaSZhenlei Huang if (new == NULL) { 85389ddfbbaSZhenlei Huang new = prison_ip_alloc(af, pip->ips, M_NOWAIT); 85489ddfbbaSZhenlei Huang if (new == NULL) 8558bce8d28SZhenlei Huang return (false); /* Redo */ 85689ddfbbaSZhenlei Huang } 85789ddfbbaSZhenlei Huang 858eb8dcdeaSGleb Smirnoff for (int pi = 0; pi < ppip->ips; pi++) 859500f82d6SZhenlei Huang if (cmp(PR_IP(pip, af, 0), PR_IP(ppip, af, pi)) == 0) { 860eb8dcdeaSGleb Smirnoff /* Found our primary address in parent. */ 861500f82d6SZhenlei Huang bcopy(PR_IP(pip, af, i), PR_IP(new, af, ips), 862500f82d6SZhenlei Huang size); 863eb8dcdeaSGleb Smirnoff i++; 864eb8dcdeaSGleb Smirnoff ips++; 865eb8dcdeaSGleb Smirnoff break; 866eb8dcdeaSGleb Smirnoff } 867eb8dcdeaSGleb Smirnoff for (int pi = 1; i < pip->ips; ) { 868eb8dcdeaSGleb Smirnoff /* Check against primary, which is unsorted. */ 869500f82d6SZhenlei Huang if (cmp(PR_IP(pip, af, i), PR_IP(ppip, af, 0)) == 0) { 870eb8dcdeaSGleb Smirnoff /* Matches parent's primary address. */ 871500f82d6SZhenlei Huang bcopy(PR_IP(pip, af, i), PR_IP(new, af, ips), 872500f82d6SZhenlei Huang size); 873eb8dcdeaSGleb Smirnoff i++; 874eb8dcdeaSGleb Smirnoff ips++; 875eb8dcdeaSGleb Smirnoff continue; 876eb8dcdeaSGleb Smirnoff } 877eb8dcdeaSGleb Smirnoff /* The rest are sorted. */ 878eb8dcdeaSGleb Smirnoff switch (pi >= ppip->ips ? -1 : 879500f82d6SZhenlei Huang cmp(PR_IP(pip, af, i), PR_IP(ppip, af, pi))) { 880eb8dcdeaSGleb Smirnoff case -1: 881eb8dcdeaSGleb Smirnoff i++; 882eb8dcdeaSGleb Smirnoff break; 883eb8dcdeaSGleb Smirnoff case 0: 884500f82d6SZhenlei Huang bcopy(PR_IP(pip, af, i), PR_IP(new, af, ips), 885500f82d6SZhenlei Huang size); 886eb8dcdeaSGleb Smirnoff i++; 887eb8dcdeaSGleb Smirnoff pi++; 888eb8dcdeaSGleb Smirnoff ips++; 889eb8dcdeaSGleb Smirnoff break; 890eb8dcdeaSGleb Smirnoff case 1: 891eb8dcdeaSGleb Smirnoff pi++; 892eb8dcdeaSGleb Smirnoff break; 893eb8dcdeaSGleb Smirnoff } 894eb8dcdeaSGleb Smirnoff } 895eb8dcdeaSGleb Smirnoff if (ips == 0) { 8968bce8d28SZhenlei Huang if (newp == NULL || *newp == NULL) 897eb8dcdeaSGleb Smirnoff prison_ip_free(new); 898eb8dcdeaSGleb Smirnoff new = NULL; 89989ddfbbaSZhenlei Huang } else { 90089ddfbbaSZhenlei Huang /* Shrink to real size */ 90189ddfbbaSZhenlei Huang KASSERT((new->ips >= ips), 90289ddfbbaSZhenlei Huang ("Out-of-bounds write to prison_ip %p", new)); 90389ddfbbaSZhenlei Huang new->ips = ips; 904eb8dcdeaSGleb Smirnoff } 905eb8dcdeaSGleb Smirnoff prison_ip_set(pr, af, new); 9068bce8d28SZhenlei Huang if (newp != NULL) 9078bce8d28SZhenlei Huang *newp = NULL; /* Used */ 90889ddfbbaSZhenlei Huang } 9098bce8d28SZhenlei Huang return (true); 910eb8dcdeaSGleb Smirnoff } 911eb8dcdeaSGleb Smirnoff 912eb8dcdeaSGleb Smirnoff /* 913eb8dcdeaSGleb Smirnoff * Fast-path check if an address belongs to a prison. 914eb8dcdeaSGleb Smirnoff */ 915eb8dcdeaSGleb Smirnoff int 916eb8dcdeaSGleb Smirnoff prison_ip_check(const struct prison *pr, const pr_family_t af, 917eb8dcdeaSGleb Smirnoff const void *addr) 918eb8dcdeaSGleb Smirnoff { 919eb8dcdeaSGleb Smirnoff int (*const cmp)(const void *, const void *) = pr_families[af].cmp; 920500f82d6SZhenlei Huang struct prison_ip *pip; 921eb8dcdeaSGleb Smirnoff int i, a, z, d; 922eb8dcdeaSGleb Smirnoff 923eb8dcdeaSGleb Smirnoff MPASS(mtx_owned(&pr->pr_mtx) || 924eb8dcdeaSGleb Smirnoff in_epoch(net_epoch_preempt) || 925eb8dcdeaSGleb Smirnoff sx_xlocked(&allprison_lock)); 926eb8dcdeaSGleb Smirnoff 92727202b98SMark Johnston pip = atomic_load_ptr(&pr->pr_addrs[af]); 928eb8dcdeaSGleb Smirnoff if (__predict_false(pip == NULL)) 929eb8dcdeaSGleb Smirnoff return (EAFNOSUPPORT); 930eb8dcdeaSGleb Smirnoff 931eb8dcdeaSGleb Smirnoff /* Check the primary IP. */ 932500f82d6SZhenlei Huang if (cmp(PR_IP(pip, af, 0), addr) == 0) 933eb8dcdeaSGleb Smirnoff return (0); 934eb8dcdeaSGleb Smirnoff 935eb8dcdeaSGleb Smirnoff /* 936eb8dcdeaSGleb Smirnoff * All the other IPs are sorted so we can do a binary search. 937eb8dcdeaSGleb Smirnoff */ 938eb8dcdeaSGleb Smirnoff a = 0; 939eb8dcdeaSGleb Smirnoff z = pip->ips - 2; 940eb8dcdeaSGleb Smirnoff while (a <= z) { 941eb8dcdeaSGleb Smirnoff i = (a + z) / 2; 942500f82d6SZhenlei Huang d = cmp(PR_IP(pip, af, i + 1), addr); 943eb8dcdeaSGleb Smirnoff if (d > 0) 944eb8dcdeaSGleb Smirnoff z = i - 1; 945eb8dcdeaSGleb Smirnoff else if (d < 0) 946eb8dcdeaSGleb Smirnoff a = i + 1; 947eb8dcdeaSGleb Smirnoff else 948eb8dcdeaSGleb Smirnoff return (0); 949eb8dcdeaSGleb Smirnoff } 950eb8dcdeaSGleb Smirnoff 951eb8dcdeaSGleb Smirnoff return (EADDRNOTAVAIL); 952eb8dcdeaSGleb Smirnoff } 953eb8dcdeaSGleb Smirnoff 954eb8dcdeaSGleb Smirnoff /* 955eb8dcdeaSGleb Smirnoff * Grab primary IP. Historically required mutex, but nothing prevents 956eb8dcdeaSGleb Smirnoff * us to support epoch-protected access. Is it used in fast path? 957eb8dcdeaSGleb Smirnoff * in{6}_jail.c helper 958eb8dcdeaSGleb Smirnoff */ 959eb8dcdeaSGleb Smirnoff const void * 960eb8dcdeaSGleb Smirnoff prison_ip_get0(const struct prison *pr, const pr_family_t af) 961eb8dcdeaSGleb Smirnoff { 962eb8dcdeaSGleb Smirnoff const struct prison_ip *pip = pr->pr_addrs[af]; 963eb8dcdeaSGleb Smirnoff 964eb8dcdeaSGleb Smirnoff mtx_assert(&pr->pr_mtx, MA_OWNED); 965eb8dcdeaSGleb Smirnoff MPASS(pip); 966eb8dcdeaSGleb Smirnoff 967500f82d6SZhenlei Huang return (pip->pr_ip); 968eb8dcdeaSGleb Smirnoff } 969eb8dcdeaSGleb Smirnoff 970eb8dcdeaSGleb Smirnoff u_int 971eb8dcdeaSGleb Smirnoff prison_ip_cnt(const struct prison *pr, const pr_family_t af) 972eb8dcdeaSGleb Smirnoff { 973eb8dcdeaSGleb Smirnoff 974eb8dcdeaSGleb Smirnoff return (pr->pr_addrs[af]->ips); 975eb8dcdeaSGleb Smirnoff } 976eb8dcdeaSGleb Smirnoff #endif /* defined(INET) || defined(INET6) */ 977eb8dcdeaSGleb Smirnoff 978413628a7SBjoern A. Zeeb int 979b38ff370SJamie Gritton kern_jail_set(struct thread *td, struct uio *optuio, int flags) 980413628a7SBjoern A. Zeeb { 981fd7a8150SMike Barcroft struct nameidata nd; 982b38ff370SJamie Gritton #ifdef INET 983eb8dcdeaSGleb Smirnoff struct prison_ip *ip4; 984413628a7SBjoern A. Zeeb #endif 985b38ff370SJamie Gritton #ifdef INET6 986eb8dcdeaSGleb Smirnoff struct prison_ip *ip6; 987b38ff370SJamie Gritton #endif 988b38ff370SJamie Gritton struct vfsopt *opt; 989b38ff370SJamie Gritton struct vfsoptlist *opts; 990ed31b3f4SJamie Gritton struct prison *pr, *deadpr, *dinspr, *inspr, *mypr, *ppr, *tpr; 991b38ff370SJamie Gritton struct vnode *root; 992babbbb9cSJamie Gritton char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid; 993b96bd95bSIan Lepore char *g_path, *osrelstr; 994672756aaSJamie Gritton struct bool_flags *bf; 995672756aaSJamie Gritton struct jailsys_flags *jsf; 9960304c731SJamie Gritton #if defined(INET) || defined(INET6) 997b38ff370SJamie Gritton void *op; 9980304c731SJamie Gritton #endif 99976ca6f88SJamie Gritton unsigned long hid; 1000b6f47c23SJamie Gritton size_t namelen, onamelen, pnamelen; 1001ed31b3f4SJamie Gritton int created, cuflags, descend, drflags, enforce; 1002cc5fd8c7SJamie Gritton int error, errmsg_len, errmsg_pos; 10030cc207a6SMartin Matuska int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel; 1004ed31b3f4SJamie Gritton int deadid, jid, jsys, len, level; 1005b96bd95bSIan Lepore int childmax, osreldt, rsnum, slevel; 1006413628a7SBjoern A. Zeeb #ifdef INET 10078bce8d28SZhenlei Huang int ip4s; 10088bce8d28SZhenlei Huang bool redo_ip4; 1009413628a7SBjoern A. Zeeb #endif 1010b38ff370SJamie Gritton #ifdef INET6 10118bce8d28SZhenlei Huang int ip6s; 10128bce8d28SZhenlei Huang bool redo_ip6; 1013b38ff370SJamie Gritton #endif 10146beb3bb4SKirk McKusick uint64_t pr_allow, ch_allow, pr_flags, ch_flags; 1015b3079544SJamie Gritton uint64_t pr_allow_diff; 10166beb3bb4SKirk McKusick unsigned tallow; 1017b38ff370SJamie Gritton char numbuf[12]; 1018b38ff370SJamie Gritton 1019b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_SET); 1020b38ff370SJamie Gritton if (!error && (flags & JAIL_ATTACH)) 1021b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 1022b38ff370SJamie Gritton if (error) 102375c13541SPoul-Henning Kamp return (error); 1024b6f47c23SJamie Gritton mypr = td->td_ucred->cr_prison; 1025b97457e2SJamie Gritton if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0) 10260304c731SJamie Gritton return (EPERM); 1027b38ff370SJamie Gritton if (flags & ~JAIL_SET_MASK) 1028b38ff370SJamie Gritton return (EINVAL); 1029b38ff370SJamie Gritton 1030b38ff370SJamie Gritton /* 1031b38ff370SJamie Gritton * Check all the parameters before committing to anything. Not all 1032b38ff370SJamie Gritton * errors can be caught early, but we may as well try. Also, this 1033b38ff370SJamie Gritton * takes care of some expensive stuff (path lookup) before getting 1034b38ff370SJamie Gritton * the allprison lock. 1035b38ff370SJamie Gritton * 1036b38ff370SJamie Gritton * XXX Jails are not filesystems, and jail parameters are not mount 1037b38ff370SJamie Gritton * options. But it makes more sense to re-use the vfsopt code 1038b38ff370SJamie Gritton * than duplicate it under a different name. 1039b38ff370SJamie Gritton */ 1040b38ff370SJamie Gritton error = vfs_buildopts(optuio, &opts); 1041b38ff370SJamie Gritton if (error) 1042b38ff370SJamie Gritton return (error); 1043b38ff370SJamie Gritton #ifdef INET 1044b38ff370SJamie Gritton ip4 = NULL; 1045b38ff370SJamie Gritton #endif 1046b38ff370SJamie Gritton #ifdef INET6 1047b38ff370SJamie Gritton ip6 = NULL; 1048b38ff370SJamie Gritton #endif 10496dfe0a3dSMartin Matuska g_path = NULL; 1050b38ff370SJamie Gritton 1051b6f47c23SJamie Gritton cuflags = flags & (JAIL_CREATE | JAIL_UPDATE); 1052b6f47c23SJamie Gritton if (!cuflags) { 1053b6f47c23SJamie Gritton error = EINVAL; 1054b6f47c23SJamie Gritton vfs_opterror(opts, "no valid operation (create or update)"); 1055b6f47c23SJamie Gritton goto done_errmsg; 1056b6f47c23SJamie Gritton } 1057b6f47c23SJamie Gritton 1058b38ff370SJamie Gritton error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 1059b38ff370SJamie Gritton if (error == ENOENT) 1060b38ff370SJamie Gritton jid = 0; 1061b38ff370SJamie Gritton else if (error != 0) 1062b38ff370SJamie Gritton goto done_free; 1063b38ff370SJamie Gritton 1064b38ff370SJamie Gritton error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel)); 1065b38ff370SJamie Gritton if (error == ENOENT) 1066b38ff370SJamie Gritton gotslevel = 0; 1067b38ff370SJamie Gritton else if (error != 0) 1068b38ff370SJamie Gritton goto done_free; 1069b38ff370SJamie Gritton else 1070b38ff370SJamie Gritton gotslevel = 1; 1071b38ff370SJamie Gritton 1072b97457e2SJamie Gritton error = 1073b97457e2SJamie Gritton vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax)); 1074b97457e2SJamie Gritton if (error == ENOENT) 1075b97457e2SJamie Gritton gotchildmax = 0; 1076b97457e2SJamie Gritton else if (error != 0) 1077b97457e2SJamie Gritton goto done_free; 1078b97457e2SJamie Gritton else 1079b97457e2SJamie Gritton gotchildmax = 1; 1080b97457e2SJamie Gritton 10810304c731SJamie Gritton error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce)); 1082f337198dSJamie Gritton if (error == ENOENT) 1083f337198dSJamie Gritton gotenforce = 0; 1084f337198dSJamie Gritton else if (error != 0) 10850304c731SJamie Gritton goto done_free; 1086f337198dSJamie Gritton else if (enforce < 0 || enforce > 2) { 1087f337198dSJamie Gritton error = EINVAL; 1088f337198dSJamie Gritton goto done_free; 1089f337198dSJamie Gritton } else 1090f337198dSJamie Gritton gotenforce = 1; 10910304c731SJamie Gritton 10920cc207a6SMartin Matuska error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum)); 10930cc207a6SMartin Matuska if (error == ENOENT) 10940cc207a6SMartin Matuska gotrsnum = 0; 10950cc207a6SMartin Matuska else if (error != 0) 10960cc207a6SMartin Matuska goto done_free; 10970cc207a6SMartin Matuska else 10980cc207a6SMartin Matuska gotrsnum = 1; 10990cc207a6SMartin Matuska 1100b38ff370SJamie Gritton pr_flags = ch_flags = 0; 1101672756aaSJamie Gritton for (bf = pr_flag_bool; 1102672756aaSJamie Gritton bf < pr_flag_bool + nitems(pr_flag_bool); 1103672756aaSJamie Gritton bf++) { 1104672756aaSJamie Gritton vfs_flagopt(opts, bf->name, &pr_flags, bf->flag); 1105672756aaSJamie Gritton vfs_flagopt(opts, bf->noname, &ch_flags, bf->flag); 11060304c731SJamie Gritton } 1107b38ff370SJamie Gritton ch_flags |= pr_flags; 1108672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 1109672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 1110672756aaSJamie Gritton jsf++) { 1111672756aaSJamie Gritton error = vfs_copyopt(opts, jsf->name, &jsys, sizeof(jsys)); 11127cbf7213SJamie Gritton if (error == ENOENT) 11137cbf7213SJamie Gritton continue; 11147cbf7213SJamie Gritton if (error != 0) 11157cbf7213SJamie Gritton goto done_free; 11167cbf7213SJamie Gritton switch (jsys) { 11177cbf7213SJamie Gritton case JAIL_SYS_DISABLE: 1118672756aaSJamie Gritton if (!jsf->disable) { 11197cbf7213SJamie Gritton error = EINVAL; 11207cbf7213SJamie Gritton goto done_free; 11217cbf7213SJamie Gritton } 1122672756aaSJamie Gritton pr_flags |= jsf->disable; 11237cbf7213SJamie Gritton break; 11247cbf7213SJamie Gritton case JAIL_SYS_NEW: 1125672756aaSJamie Gritton pr_flags |= jsf->new; 11267cbf7213SJamie Gritton break; 11277cbf7213SJamie Gritton case JAIL_SYS_INHERIT: 11287cbf7213SJamie Gritton break; 11297cbf7213SJamie Gritton default: 11307cbf7213SJamie Gritton error = EINVAL; 11317cbf7213SJamie Gritton goto done_free; 11327cbf7213SJamie Gritton } 1133672756aaSJamie Gritton ch_flags |= jsf->new | jsf->disable; 11347cbf7213SJamie Gritton } 11351158508aSJamie Gritton if ((flags & (JAIL_CREATE | JAIL_ATTACH)) == JAIL_CREATE 11364affa14cSJamie Gritton && !(pr_flags & PR_PERSIST)) { 11374affa14cSJamie Gritton error = EINVAL; 11384affa14cSJamie Gritton vfs_opterror(opts, "new jail must persist or attach"); 11394affa14cSJamie Gritton goto done_errmsg; 11404affa14cSJamie Gritton } 1141679e1390SJamie Gritton #ifdef VIMAGE 1142679e1390SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) { 1143679e1390SJamie Gritton error = EINVAL; 1144679e1390SJamie Gritton vfs_opterror(opts, "vnet cannot be changed after creation"); 1145679e1390SJamie Gritton goto done_errmsg; 1146679e1390SJamie Gritton } 1147679e1390SJamie Gritton #endif 11482b0d6f81SJamie Gritton #ifdef INET 11492b0d6f81SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) { 11502b0d6f81SJamie Gritton error = EINVAL; 11512b0d6f81SJamie Gritton vfs_opterror(opts, "ip4 cannot be changed after creation"); 11522b0d6f81SJamie Gritton goto done_errmsg; 11532b0d6f81SJamie Gritton } 11542b0d6f81SJamie Gritton #endif 11552b0d6f81SJamie Gritton #ifdef INET6 11562b0d6f81SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) { 11572b0d6f81SJamie Gritton error = EINVAL; 11582b0d6f81SJamie Gritton vfs_opterror(opts, "ip6 cannot be changed after creation"); 11592b0d6f81SJamie Gritton goto done_errmsg; 11602b0d6f81SJamie Gritton } 11612b0d6f81SJamie Gritton #endif 1162b38ff370SJamie Gritton 11630304c731SJamie Gritton pr_allow = ch_allow = 0; 1164672756aaSJamie Gritton for (bf = pr_flag_allow; 11655d58f959SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 11665d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 1167672756aaSJamie Gritton bf++) { 1168672756aaSJamie Gritton vfs_flagopt(opts, bf->name, &pr_allow, bf->flag); 1169672756aaSJamie Gritton vfs_flagopt(opts, bf->noname, &ch_allow, bf->flag); 11700304c731SJamie Gritton } 11710304c731SJamie Gritton ch_allow |= pr_allow; 11720304c731SJamie Gritton 1173b38ff370SJamie Gritton error = vfs_getopt(opts, "name", (void **)&name, &len); 1174b38ff370SJamie Gritton if (error == ENOENT) 1175b38ff370SJamie Gritton name = NULL; 1176b38ff370SJamie Gritton else if (error != 0) 1177b38ff370SJamie Gritton goto done_free; 1178b38ff370SJamie Gritton else { 1179b38ff370SJamie Gritton if (len == 0 || name[len - 1] != '\0') { 1180b38ff370SJamie Gritton error = EINVAL; 1181b38ff370SJamie Gritton goto done_free; 1182b38ff370SJamie Gritton } 1183b38ff370SJamie Gritton if (len > MAXHOSTNAMELEN) { 1184b38ff370SJamie Gritton error = ENAMETOOLONG; 1185b38ff370SJamie Gritton goto done_free; 1186b38ff370SJamie Gritton } 1187b38ff370SJamie Gritton } 1188b38ff370SJamie Gritton 1189b38ff370SJamie Gritton error = vfs_getopt(opts, "host.hostname", (void **)&host, &len); 1190b38ff370SJamie Gritton if (error == ENOENT) 1191b38ff370SJamie Gritton host = NULL; 1192b38ff370SJamie Gritton else if (error != 0) 1193b38ff370SJamie Gritton goto done_free; 1194b38ff370SJamie Gritton else { 119576ca6f88SJamie Gritton ch_flags |= PR_HOST; 119676ca6f88SJamie Gritton pr_flags |= PR_HOST; 1197b38ff370SJamie Gritton if (len == 0 || host[len - 1] != '\0') { 1198b38ff370SJamie Gritton error = EINVAL; 1199b38ff370SJamie Gritton goto done_free; 1200b38ff370SJamie Gritton } 1201b38ff370SJamie Gritton if (len > MAXHOSTNAMELEN) { 1202b38ff370SJamie Gritton error = ENAMETOOLONG; 1203b38ff370SJamie Gritton goto done_free; 1204b38ff370SJamie Gritton } 1205b38ff370SJamie Gritton } 1206b38ff370SJamie Gritton 120776ca6f88SJamie Gritton error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len); 120876ca6f88SJamie Gritton if (error == ENOENT) 120976ca6f88SJamie Gritton domain = NULL; 121076ca6f88SJamie Gritton else if (error != 0) 121176ca6f88SJamie Gritton goto done_free; 121276ca6f88SJamie Gritton else { 121376ca6f88SJamie Gritton ch_flags |= PR_HOST; 121476ca6f88SJamie Gritton pr_flags |= PR_HOST; 121576ca6f88SJamie Gritton if (len == 0 || domain[len - 1] != '\0') { 121676ca6f88SJamie Gritton error = EINVAL; 121776ca6f88SJamie Gritton goto done_free; 121876ca6f88SJamie Gritton } 121976ca6f88SJamie Gritton if (len > MAXHOSTNAMELEN) { 122076ca6f88SJamie Gritton error = ENAMETOOLONG; 122176ca6f88SJamie Gritton goto done_free; 122276ca6f88SJamie Gritton } 122376ca6f88SJamie Gritton } 122476ca6f88SJamie Gritton 122576ca6f88SJamie Gritton error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len); 122676ca6f88SJamie Gritton if (error == ENOENT) 122776ca6f88SJamie Gritton uuid = NULL; 122876ca6f88SJamie Gritton else if (error != 0) 122976ca6f88SJamie Gritton goto done_free; 123076ca6f88SJamie Gritton else { 123176ca6f88SJamie Gritton ch_flags |= PR_HOST; 123276ca6f88SJamie Gritton pr_flags |= PR_HOST; 123376ca6f88SJamie Gritton if (len == 0 || uuid[len - 1] != '\0') { 123476ca6f88SJamie Gritton error = EINVAL; 123576ca6f88SJamie Gritton goto done_free; 123676ca6f88SJamie Gritton } 123776ca6f88SJamie Gritton if (len > HOSTUUIDLEN) { 123876ca6f88SJamie Gritton error = ENAMETOOLONG; 123976ca6f88SJamie Gritton goto done_free; 124076ca6f88SJamie Gritton } 124176ca6f88SJamie Gritton } 124276ca6f88SJamie Gritton 1243841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32 1244a5c1afadSDmitry Chagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 124576ca6f88SJamie Gritton uint32_t hid32; 124676ca6f88SJamie Gritton 124776ca6f88SJamie Gritton error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32)); 124876ca6f88SJamie Gritton hid = hid32; 124976ca6f88SJamie Gritton } else 125076ca6f88SJamie Gritton #endif 125176ca6f88SJamie Gritton error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid)); 125276ca6f88SJamie Gritton if (error == ENOENT) 125376ca6f88SJamie Gritton gothid = 0; 125476ca6f88SJamie Gritton else if (error != 0) 125576ca6f88SJamie Gritton goto done_free; 125676ca6f88SJamie Gritton else { 125776ca6f88SJamie Gritton gothid = 1; 125876ca6f88SJamie Gritton ch_flags |= PR_HOST; 125976ca6f88SJamie Gritton pr_flags |= PR_HOST; 126076ca6f88SJamie Gritton } 126176ca6f88SJamie Gritton 1262b38ff370SJamie Gritton #ifdef INET 1263b38ff370SJamie Gritton error = vfs_getopt(opts, "ip4.addr", &op, &ip4s); 1264b38ff370SJamie Gritton if (error == ENOENT) 12650e5e396eSJamie Gritton ip4s = 0; 1266b38ff370SJamie Gritton else if (error != 0) 1267b38ff370SJamie Gritton goto done_free; 1268eb8dcdeaSGleb Smirnoff else if (ip4s & (sizeof(struct in_addr) - 1)) { 1269b38ff370SJamie Gritton error = EINVAL; 1270b38ff370SJamie Gritton goto done_free; 12710304c731SJamie Gritton } else { 12726a3f2779SJamie Gritton ch_flags |= PR_IP4_USER; 12736a3f2779SJamie Gritton pr_flags |= PR_IP4_USER; 12746a3f2779SJamie Gritton if (ip4s > 0) { 1275eb8dcdeaSGleb Smirnoff ip4s /= sizeof(struct in_addr); 1276b38ff370SJamie Gritton if (ip4s > jail_max_af_ips) { 1277b38ff370SJamie Gritton error = EINVAL; 1278b38ff370SJamie Gritton vfs_opterror(opts, "too many IPv4 addresses"); 1279b38ff370SJamie Gritton goto done_errmsg; 1280b38ff370SJamie Gritton } 1281eb8dcdeaSGleb Smirnoff ip4 = prison_ip_copyin(PR_INET, op, ip4s); 1282eb8dcdeaSGleb Smirnoff if (ip4 == NULL) { 1283b38ff370SJamie Gritton error = EINVAL; 1284b38ff370SJamie Gritton goto done_free; 1285b38ff370SJamie Gritton } 1286b38ff370SJamie Gritton } 12870304c731SJamie Gritton } 1288b38ff370SJamie Gritton #endif 1289b38ff370SJamie Gritton 1290b38ff370SJamie Gritton #ifdef INET6 1291b38ff370SJamie Gritton error = vfs_getopt(opts, "ip6.addr", &op, &ip6s); 1292b38ff370SJamie Gritton if (error == ENOENT) 12930e5e396eSJamie Gritton ip6s = 0; 1294b38ff370SJamie Gritton else if (error != 0) 1295b38ff370SJamie Gritton goto done_free; 1296eb8dcdeaSGleb Smirnoff else if (ip6s & (sizeof(struct in6_addr) - 1)) { 1297b38ff370SJamie Gritton error = EINVAL; 1298b38ff370SJamie Gritton goto done_free; 12990304c731SJamie Gritton } else { 13006a3f2779SJamie Gritton ch_flags |= PR_IP6_USER; 13016a3f2779SJamie Gritton pr_flags |= PR_IP6_USER; 13026a3f2779SJamie Gritton if (ip6s > 0) { 1303eb8dcdeaSGleb Smirnoff ip6s /= sizeof(struct in6_addr); 1304b38ff370SJamie Gritton if (ip6s > jail_max_af_ips) { 1305b38ff370SJamie Gritton error = EINVAL; 1306b38ff370SJamie Gritton vfs_opterror(opts, "too many IPv6 addresses"); 1307b38ff370SJamie Gritton goto done_errmsg; 1308b38ff370SJamie Gritton } 1309eb8dcdeaSGleb Smirnoff ip6 = prison_ip_copyin(PR_INET6, op, ip6s); 1310eb8dcdeaSGleb Smirnoff if (ip6 == NULL) { 1311b38ff370SJamie Gritton error = EINVAL; 1312b38ff370SJamie Gritton goto done_free; 1313b38ff370SJamie Gritton } 1314b38ff370SJamie Gritton } 13150304c731SJamie Gritton } 1316b38ff370SJamie Gritton #endif 1317b38ff370SJamie Gritton 1318bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6)) 1319bdfc8cc4SJamie Gritton if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 1320bdfc8cc4SJamie Gritton error = EINVAL; 1321bdfc8cc4SJamie Gritton vfs_opterror(opts, 1322bdfc8cc4SJamie Gritton "vnet jails cannot have IP address restrictions"); 1323bdfc8cc4SJamie Gritton goto done_errmsg; 1324bdfc8cc4SJamie Gritton } 1325bdfc8cc4SJamie Gritton #endif 1326bdfc8cc4SJamie Gritton 1327cf0313c6SJamie Gritton error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len); 1328cf0313c6SJamie Gritton if (error == ENOENT) 1329cf0313c6SJamie Gritton osrelstr = NULL; 1330cf0313c6SJamie Gritton else if (error != 0) 1331cf0313c6SJamie Gritton goto done_free; 1332cf0313c6SJamie Gritton else { 1333cf0313c6SJamie Gritton if (flags & JAIL_UPDATE) { 1334cf0313c6SJamie Gritton error = EINVAL; 1335cf0313c6SJamie Gritton vfs_opterror(opts, 1336cf0313c6SJamie Gritton "osrelease cannot be changed after creation"); 1337cf0313c6SJamie Gritton goto done_errmsg; 1338cf0313c6SJamie Gritton } 13391b786d01SBjoern A. Zeeb if (len == 0 || osrelstr[len - 1] != '\0') { 1340cf0313c6SJamie Gritton error = EINVAL; 13411b786d01SBjoern A. Zeeb goto done_free; 13421b786d01SBjoern A. Zeeb } 13431b786d01SBjoern A. Zeeb if (len >= OSRELEASELEN) { 13441b786d01SBjoern A. Zeeb error = ENAMETOOLONG; 1345cf0313c6SJamie Gritton vfs_opterror(opts, 1346cf0313c6SJamie Gritton "osrelease string must be 1-%d bytes long", 1347cf0313c6SJamie Gritton OSRELEASELEN - 1); 1348cf0313c6SJamie Gritton goto done_errmsg; 1349cf0313c6SJamie Gritton } 1350cf0313c6SJamie Gritton } 1351cf0313c6SJamie Gritton 1352cf0313c6SJamie Gritton error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt)); 1353cf0313c6SJamie Gritton if (error == ENOENT) 1354cf0313c6SJamie Gritton osreldt = 0; 1355cf0313c6SJamie Gritton else if (error != 0) 1356cf0313c6SJamie Gritton goto done_free; 1357cf0313c6SJamie Gritton else { 1358cf0313c6SJamie Gritton if (flags & JAIL_UPDATE) { 1359cf0313c6SJamie Gritton error = EINVAL; 1360cf0313c6SJamie Gritton vfs_opterror(opts, 1361cf0313c6SJamie Gritton "osreldate cannot be changed after creation"); 1362cf0313c6SJamie Gritton goto done_errmsg; 1363cf0313c6SJamie Gritton } 1364cf0313c6SJamie Gritton if (osreldt == 0) { 1365cf0313c6SJamie Gritton error = EINVAL; 1366cf0313c6SJamie Gritton vfs_opterror(opts, "osreldate cannot be 0"); 1367cf0313c6SJamie Gritton goto done_errmsg; 1368cf0313c6SJamie Gritton } 1369cf0313c6SJamie Gritton } 1370cf0313c6SJamie Gritton 1371b38ff370SJamie Gritton root = NULL; 1372b38ff370SJamie Gritton error = vfs_getopt(opts, "path", (void **)&path, &len); 1373b38ff370SJamie Gritton if (error == ENOENT) 1374b38ff370SJamie Gritton path = NULL; 1375b38ff370SJamie Gritton else if (error != 0) 1376b38ff370SJamie Gritton goto done_free; 1377b38ff370SJamie Gritton else { 1378b38ff370SJamie Gritton if (flags & JAIL_UPDATE) { 1379b38ff370SJamie Gritton error = EINVAL; 1380b38ff370SJamie Gritton vfs_opterror(opts, 1381b38ff370SJamie Gritton "path cannot be changed after creation"); 1382b38ff370SJamie Gritton goto done_errmsg; 1383b38ff370SJamie Gritton } 1384b38ff370SJamie Gritton if (len == 0 || path[len - 1] != '\0') { 1385b38ff370SJamie Gritton error = EINVAL; 1386b38ff370SJamie Gritton goto done_free; 1387b38ff370SJamie Gritton } 13887e1d3eefSMateusz Guzik NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path); 1389b38ff370SJamie Gritton error = namei(&nd); 1390b38ff370SJamie Gritton if (error) 1391b38ff370SJamie Gritton goto done_free; 1392b38ff370SJamie Gritton root = nd.ni_vp; 1393bb92cd7bSMateusz Guzik NDFREE_PNBUF(&nd); 13946dfe0a3dSMartin Matuska g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 13956dfe0a3dSMartin Matuska strlcpy(g_path, path, MAXPATHLEN); 13966dfe0a3dSMartin Matuska error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN); 13973eb6b656SMateusz Guzik if (error == 0) { 13986dfe0a3dSMartin Matuska path = g_path; 13996dfe0a3dSMartin Matuska } else { 1400f6e633a9SMartin Matuska /* exit on other errors */ 1401b38ff370SJamie Gritton goto done_free; 1402b38ff370SJamie Gritton } 1403f6e633a9SMartin Matuska if (root->v_type != VDIR) { 1404f6e633a9SMartin Matuska error = ENOTDIR; 1405f6e633a9SMartin Matuska vput(root); 1406f6e633a9SMartin Matuska goto done_free; 1407f6e633a9SMartin Matuska } 1408b249ce48SMateusz Guzik VOP_UNLOCK(root); 1409b38ff370SJamie Gritton } 1410b38ff370SJamie Gritton 1411b38ff370SJamie Gritton /* 1412b6f47c23SJamie Gritton * Find the specified jail, or at least its parent. 1413b38ff370SJamie Gritton * This abuses the file error codes ENOENT and EEXIST. 1414b38ff370SJamie Gritton */ 1415b38ff370SJamie Gritton pr = NULL; 14167de883c8SJamie Gritton inspr = NULL; 1417ed31b3f4SJamie Gritton deadpr = NULL; 1418babbbb9cSJamie Gritton if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) { 1419babbbb9cSJamie Gritton namelc = strrchr(name, '.'); 1420babbbb9cSJamie Gritton jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10); 1421babbbb9cSJamie Gritton if (*p != '\0') 1422babbbb9cSJamie Gritton jid = 0; 1423babbbb9cSJamie Gritton } 1424b6f47c23SJamie Gritton sx_xlock(&allprison_lock); 1425108a9384SJamie Gritton drflags = PD_LIST_XLOCKED; 14260a2a96f3SJamie Gritton ppr = mypr; 14270a2a96f3SJamie Gritton if (!prison_isalive(ppr)) { 14280a2a96f3SJamie Gritton /* This jail is dying. This process will surely follow. */ 14290a2a96f3SJamie Gritton error = EAGAIN; 14300a2a96f3SJamie Gritton goto done_deref; 14310a2a96f3SJamie Gritton } 1432b38ff370SJamie Gritton if (jid != 0) { 1433b38ff370SJamie Gritton if (jid < 0) { 1434b38ff370SJamie Gritton error = EINVAL; 1435b38ff370SJamie Gritton vfs_opterror(opts, "negative jid"); 14362a4b2251SJamie Gritton goto done_deref; 1437b38ff370SJamie Gritton } 14387de883c8SJamie Gritton /* 14397de883c8SJamie Gritton * See if a requested jid already exists. Keep track of 14407de883c8SJamie Gritton * where it can be inserted later. 14417de883c8SJamie Gritton */ 14427de883c8SJamie Gritton TAILQ_FOREACH(inspr, &allprison, pr_list) { 1443f7496dcaSJamie Gritton if (inspr->pr_id < jid) 1444f7496dcaSJamie Gritton continue; 1445f7496dcaSJamie Gritton if (inspr->pr_id > jid) 1446f7496dcaSJamie Gritton break; 1447ed31b3f4SJamie Gritton if (prison_isalive(inspr)) { 14487de883c8SJamie Gritton pr = inspr; 1449f7496dcaSJamie Gritton mtx_lock(&pr->pr_mtx); 14502a4b2251SJamie Gritton drflags |= PD_LOCKED; 1451ed31b3f4SJamie Gritton } else { 1452ed31b3f4SJamie Gritton /* Note a dying jail to handle later. */ 1453ed31b3f4SJamie Gritton deadpr = inspr; 1454ed31b3f4SJamie Gritton } 14557de883c8SJamie Gritton inspr = NULL; 14567de883c8SJamie Gritton break; 14577de883c8SJamie Gritton } 1458ed31b3f4SJamie Gritton if (cuflags == JAIL_CREATE && pr != NULL) { 14597de883c8SJamie Gritton /* 14607de883c8SJamie Gritton * Even creators that cannot see the jail will 14617de883c8SJamie Gritton * get EEXIST. 14627de883c8SJamie Gritton */ 1463b38ff370SJamie Gritton error = EEXIST; 1464ed31b3f4SJamie Gritton vfs_opterror(opts, "jail %d already exists", jid); 14652a4b2251SJamie Gritton goto done_deref; 1466b38ff370SJamie Gritton } 1467ed31b3f4SJamie Gritton if ((pr == NULL) 1468ed31b3f4SJamie Gritton ? cuflags == JAIL_UPDATE 1469ed31b3f4SJamie Gritton : !prison_ischild(mypr, pr)) { 14707de883c8SJamie Gritton /* 1471ed31b3f4SJamie Gritton * Updaters get ENOENT for nonexistent jails, 1472ed31b3f4SJamie Gritton * or for jails they cannot see. The latter 1473ed31b3f4SJamie Gritton * case is true even for CREATE | UPDATE, 14747de883c8SJamie Gritton * which normally cannot give this error. 14757de883c8SJamie Gritton */ 14762a4b2251SJamie Gritton error = ENOENT; 14772a4b2251SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 14782a4b2251SJamie Gritton goto done_deref; 1479f7496dcaSJamie Gritton } 1480b38ff370SJamie Gritton } 1481b38ff370SJamie Gritton /* 1482b38ff370SJamie Gritton * If the caller provided a name, look for a jail by that name. 1483b38ff370SJamie Gritton * This has different semantics for creates and updates keyed by jid 1484b38ff370SJamie Gritton * (where the name must not already exist in a different jail), 1485b38ff370SJamie Gritton * and updates keyed by the name itself (where the name must exist 1486b38ff370SJamie Gritton * because that is the jail being updated). 1487b38ff370SJamie Gritton */ 1488b6f47c23SJamie Gritton namelc = NULL; 1489b38ff370SJamie Gritton if (name != NULL) { 1490babbbb9cSJamie Gritton namelc = strrchr(name, '.'); 1491babbbb9cSJamie Gritton if (namelc == NULL) 1492babbbb9cSJamie Gritton namelc = name; 1493babbbb9cSJamie Gritton else { 14940304c731SJamie Gritton /* 14950304c731SJamie Gritton * This is a hierarchical name. Split it into the 14960304c731SJamie Gritton * parent and child names, and make sure the parent 14970304c731SJamie Gritton * exists or matches an already found jail. 14980304c731SJamie Gritton */ 14990304c731SJamie Gritton if (pr != NULL) { 1500babbbb9cSJamie Gritton if (strncmp(name, ppr->pr_name, namelc - name) 1501babbbb9cSJamie Gritton || ppr->pr_name[namelc - name] != '\0') { 15020304c731SJamie Gritton error = EINVAL; 15030304c731SJamie Gritton vfs_opterror(opts, 15040304c731SJamie Gritton "cannot change jail's parent"); 15052a4b2251SJamie Gritton goto done_deref; 15060304c731SJamie Gritton } 15070304c731SJamie Gritton } else { 1508b6f47c23SJamie Gritton *namelc = '\0'; 15090304c731SJamie Gritton ppr = prison_find_name(mypr, name); 15100304c731SJamie Gritton if (ppr == NULL) { 15110304c731SJamie Gritton error = ENOENT; 15120304c731SJamie Gritton vfs_opterror(opts, 15130304c731SJamie Gritton "jail \"%s\" not found", name); 15142a4b2251SJamie Gritton goto done_deref; 15150304c731SJamie Gritton } 1516c050ea80SJamie Gritton mtx_unlock(&ppr->pr_mtx); 15170a2a96f3SJamie Gritton if (!prison_isalive(ppr)) { 1518c050ea80SJamie Gritton error = ENOENT; 1519c050ea80SJamie Gritton vfs_opterror(opts, 1520c050ea80SJamie Gritton "jail \"%s\" is dying", name); 1521c050ea80SJamie Gritton goto done_deref; 1522c050ea80SJamie Gritton } 1523b6f47c23SJamie Gritton *namelc = '.'; 15240304c731SJamie Gritton } 1525b6f47c23SJamie Gritton namelc++; 15260304c731SJamie Gritton } 1527b6f47c23SJamie Gritton if (namelc[0] != '\0') { 1528b6f47c23SJamie Gritton pnamelen = 15290304c731SJamie Gritton (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 15300304c731SJamie Gritton FOREACH_PRISON_CHILD(ppr, tpr) { 1531ed31b3f4SJamie Gritton if (tpr == pr || !prison_isalive(tpr) || 1532ed31b3f4SJamie Gritton strcmp(tpr->pr_name + pnamelen, namelc)) 1533ed31b3f4SJamie Gritton continue; 1534ed31b3f4SJamie Gritton if (cuflags == JAIL_CREATE || pr != NULL) { 1535b38ff370SJamie Gritton /* 1536ed31b3f4SJamie Gritton * Create, or update(jid): name must 1537ed31b3f4SJamie Gritton * not exist in an active sibling jail. 1538b38ff370SJamie Gritton */ 1539ed31b3f4SJamie Gritton error = EEXIST; 1540ed31b3f4SJamie Gritton vfs_opterror(opts, 1541ed31b3f4SJamie Gritton "jail \"%s\" already exists", name); 1542ed31b3f4SJamie Gritton goto done_deref; 1543ed31b3f4SJamie Gritton } 1544ed31b3f4SJamie Gritton /* Use this jail for updates. */ 1545b38ff370SJamie Gritton pr = tpr; 1546f7496dcaSJamie Gritton mtx_lock(&pr->pr_mtx); 154783bc72a0SJamie Gritton drflags |= PD_LOCKED; 1548b38ff370SJamie Gritton break; 1549b38ff370SJamie Gritton } 1550b38ff370SJamie Gritton /* 1551ed31b3f4SJamie Gritton * Update: name must exist if no jid is specified. 1552ed31b3f4SJamie Gritton * As with the jid case, the jail must be currently 1553ed31b3f4SJamie Gritton * visible, or else even CREATE | UPDATE will get 1554ed31b3f4SJamie Gritton * an error. 1555b38ff370SJamie Gritton */ 1556ed31b3f4SJamie Gritton if ((pr == NULL) 1557ed31b3f4SJamie Gritton ? cuflags == JAIL_UPDATE 1558ed31b3f4SJamie Gritton : !prison_isalive(pr)) { 1559b38ff370SJamie Gritton error = ENOENT; 1560b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", 1561b38ff370SJamie Gritton name); 15622a4b2251SJamie Gritton goto done_deref; 1563b38ff370SJamie Gritton } 1564b38ff370SJamie Gritton } 1565b38ff370SJamie Gritton } 1566b38ff370SJamie Gritton /* Update: must provide a jid or name. */ 1567b38ff370SJamie Gritton else if (cuflags == JAIL_UPDATE && pr == NULL) { 1568b38ff370SJamie Gritton error = ENOENT; 1569b38ff370SJamie Gritton vfs_opterror(opts, "update specified no jail"); 15702a4b2251SJamie Gritton goto done_deref; 1571b38ff370SJamie Gritton } 1572b38ff370SJamie Gritton 1573b38ff370SJamie Gritton /* If there's no prison to update, create a new one and link it in. */ 15742a4b2251SJamie Gritton created = pr == NULL; 15752a4b2251SJamie Gritton if (created) { 1576b97457e2SJamie Gritton for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent) 1577b97457e2SJamie Gritton if (tpr->pr_childcount >= tpr->pr_childmax) { 1578b97457e2SJamie Gritton error = EPERM; 1579b97457e2SJamie Gritton vfs_opterror(opts, "prison limit exceeded"); 15802a4b2251SJamie Gritton goto done_deref; 1581b97457e2SJamie Gritton } 1582ed31b3f4SJamie Gritton 1583ed31b3f4SJamie Gritton if (deadpr != NULL) { 1584ed31b3f4SJamie Gritton /* 1585ed31b3f4SJamie Gritton * The prison being created has the same ID as a dying 1586ed31b3f4SJamie Gritton * one. Handle this by giving the dying jail a new ID. 1587ed31b3f4SJamie Gritton * This may cause some confusion to user space, but 1588ed31b3f4SJamie Gritton * only to those listing dying jails. 1589ed31b3f4SJamie Gritton */ 1590ed31b3f4SJamie Gritton deadid = get_next_deadid(&dinspr); 1591ed31b3f4SJamie Gritton if (deadid == 0) { 1592ed31b3f4SJamie Gritton error = EAGAIN; 1593ed31b3f4SJamie Gritton vfs_opterror(opts, "no available jail IDs"); 1594ed31b3f4SJamie Gritton goto done_deref; 1595ed31b3f4SJamie Gritton } 1596ed31b3f4SJamie Gritton mtx_lock(&deadpr->pr_mtx); 1597ed31b3f4SJamie Gritton deadpr->pr_id = deadid; 1598ed31b3f4SJamie Gritton mtx_unlock(&deadpr->pr_mtx); 1599ed31b3f4SJamie Gritton if (dinspr == deadpr) 1600ed31b3f4SJamie Gritton inspr = deadpr; 1601ed31b3f4SJamie Gritton else { 1602ed31b3f4SJamie Gritton inspr = TAILQ_NEXT(deadpr, pr_list); 1603ed31b3f4SJamie Gritton TAILQ_REMOVE(&allprison, deadpr, pr_list); 1604ed31b3f4SJamie Gritton if (dinspr != NULL) 1605ed31b3f4SJamie Gritton TAILQ_INSERT_AFTER(&allprison, dinspr, 1606ed31b3f4SJamie Gritton deadpr, pr_list); 1607ed31b3f4SJamie Gritton else 1608ed31b3f4SJamie Gritton TAILQ_INSERT_HEAD(&allprison, deadpr, 1609ed31b3f4SJamie Gritton pr_list); 1610ed31b3f4SJamie Gritton } 1611ed31b3f4SJamie Gritton } 16127de883c8SJamie Gritton if (jid == 0 && (jid = get_next_prid(&inspr)) == 0) { 1613b38ff370SJamie Gritton error = EAGAIN; 16147de883c8SJamie Gritton vfs_opterror(opts, "no available jail IDs"); 16152a4b2251SJamie Gritton goto done_deref; 1616b38ff370SJamie Gritton } 16172a4b2251SJamie Gritton 16182a4b2251SJamie Gritton pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO); 16191158508aSJamie Gritton pr->pr_state = PRISON_STATE_INVALID; 16201158508aSJamie Gritton refcount_init(&pr->pr_ref, 1); 1621f7496dcaSJamie Gritton refcount_init(&pr->pr_uref, 0); 16221158508aSJamie Gritton drflags |= PD_DEREF; 16232a4b2251SJamie Gritton LIST_INIT(&pr->pr_children); 16242a4b2251SJamie Gritton mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK); 16252a4b2251SJamie Gritton TASK_INIT(&pr->pr_task, 0, prison_complete, pr); 16262a4b2251SJamie Gritton 16277de883c8SJamie Gritton pr->pr_id = jid; 16287de883c8SJamie Gritton if (inspr != NULL) 16297de883c8SJamie Gritton TAILQ_INSERT_BEFORE(inspr, pr, pr_list); 16307de883c8SJamie Gritton else 1631b38ff370SJamie Gritton TAILQ_INSERT_TAIL(&allprison, pr, pr_list); 16327de883c8SJamie Gritton 16337de883c8SJamie Gritton pr->pr_parent = ppr; 16340a2a96f3SJamie Gritton prison_hold(ppr); 16350a2a96f3SJamie Gritton prison_proc_hold(ppr); 16360304c731SJamie Gritton LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling); 16370304c731SJamie Gritton for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 1638b97457e2SJamie Gritton tpr->pr_childcount++; 1639b38ff370SJamie Gritton 16400304c731SJamie Gritton /* Set some default values, and inherit some from the parent. */ 1641b6f47c23SJamie Gritton if (namelc == NULL) 1642b6f47c23SJamie Gritton namelc = ""; 1643b38ff370SJamie Gritton if (path == NULL) { 1644b38ff370SJamie Gritton path = "/"; 16450304c731SJamie Gritton root = mypr->pr_root; 1646b38ff370SJamie Gritton vref(root); 1647b38ff370SJamie Gritton } 16488986e3a0SJamie Gritton strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN); 16498986e3a0SJamie Gritton pr->pr_flags |= PR_HOST; 1650bdfc8cc4SJamie Gritton #if defined(INET) || defined(INET6) 1651bdfc8cc4SJamie Gritton #ifdef VIMAGE 1652bdfc8cc4SJamie Gritton if (!(pr_flags & PR_VNET)) 1653bdfc8cc4SJamie Gritton #endif 1654bdfc8cc4SJamie Gritton { 16550304c731SJamie Gritton #ifdef INET 16562b0d6f81SJamie Gritton if (!(ch_flags & PR_IP4_USER)) 16576a3f2779SJamie Gritton pr->pr_flags |= PR_IP4 | PR_IP4_USER; 16582b0d6f81SJamie Gritton else if (!(pr_flags & PR_IP4_USER)) { 16592b0d6f81SJamie Gritton pr->pr_flags |= ppr->pr_flags & PR_IP4; 1660eb8dcdeaSGleb Smirnoff prison_ip_dup(ppr, pr, PR_INET); 16612b0d6f81SJamie Gritton } 16620304c731SJamie Gritton #endif 16630304c731SJamie Gritton #ifdef INET6 16642b0d6f81SJamie Gritton if (!(ch_flags & PR_IP6_USER)) 16656a3f2779SJamie Gritton pr->pr_flags |= PR_IP6 | PR_IP6_USER; 16662b0d6f81SJamie Gritton else if (!(pr_flags & PR_IP6_USER)) { 16672b0d6f81SJamie Gritton pr->pr_flags |= ppr->pr_flags & PR_IP6; 1668eb8dcdeaSGleb Smirnoff prison_ip_dup(ppr, pr, PR_INET6); 16692b0d6f81SJamie Gritton } 16700304c731SJamie Gritton #endif 1671bdfc8cc4SJamie Gritton } 1672bdfc8cc4SJamie Gritton #endif 1673592bcae8SBjoern A. Zeeb /* Source address selection is always on by default. */ 1674592bcae8SBjoern A. Zeeb pr->pr_flags |= _PR_IP_SADDRSEL; 1675592bcae8SBjoern A. Zeeb 16760304c731SJamie Gritton pr->pr_securelevel = ppr->pr_securelevel; 16770304c731SJamie Gritton pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow; 1678af10bf05SBjoern A. Zeeb pr->pr_enforce_statfs = jail_default_enforce_statfs; 1679bf3db8aaSMartin Matuska pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum; 1680b38ff370SJamie Gritton 1681b96bd95bSIan Lepore pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate; 1682b96bd95bSIan Lepore if (osrelstr == NULL) 16831b786d01SBjoern A. Zeeb strlcpy(pr->pr_osrelease, ppr->pr_osrelease, 16841b786d01SBjoern A. Zeeb sizeof(pr->pr_osrelease)); 1685b96bd95bSIan Lepore else 16861b786d01SBjoern A. Zeeb strlcpy(pr->pr_osrelease, osrelstr, 16871b786d01SBjoern A. Zeeb sizeof(pr->pr_osrelease)); 1688b96bd95bSIan Lepore 1689679e1390SJamie Gritton #ifdef VIMAGE 1690679e1390SJamie Gritton /* Allocate a new vnet if specified. */ 1691679e1390SJamie Gritton pr->pr_vnet = (pr_flags & PR_VNET) 1692679e1390SJamie Gritton ? vnet_alloc() : ppr->pr_vnet; 1693679e1390SJamie Gritton #endif 1694b38ff370SJamie Gritton /* 1695b38ff370SJamie Gritton * Allocate a dedicated cpuset for each jail. 16968771ff75SGordon Bergling * Unlike other initial settings, this may return an error. 1697b38ff370SJamie Gritton */ 16980304c731SJamie Gritton error = cpuset_create_root(ppr, &pr->pr_cpuset); 16992a4b2251SJamie Gritton if (error) 17002a4b2251SJamie Gritton goto done_deref; 1701b38ff370SJamie Gritton 1702b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 17032a4b2251SJamie Gritton drflags |= PD_LOCKED; 1704b38ff370SJamie Gritton } else { 17052b0d6f81SJamie Gritton /* 17062b0d6f81SJamie Gritton * Grab a reference for existing prisons, to ensure they 17072b0d6f81SJamie Gritton * continue to exist for the duration of the call. 17082b0d6f81SJamie Gritton */ 17096754ae25SJamie Gritton prison_hold(pr); 17102a4b2251SJamie Gritton drflags |= PD_DEREF; 1711bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6)) 1712bdfc8cc4SJamie Gritton if ((pr->pr_flags & PR_VNET) && 1713bdfc8cc4SJamie Gritton (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 1714bdfc8cc4SJamie Gritton error = EINVAL; 1715bdfc8cc4SJamie Gritton vfs_opterror(opts, 1716bdfc8cc4SJamie Gritton "vnet jails cannot have IP address restrictions"); 17172a4b2251SJamie Gritton goto done_deref; 1718bdfc8cc4SJamie Gritton } 1719bdfc8cc4SJamie Gritton #endif 17202b0d6f81SJamie Gritton #ifdef INET 17212b0d6f81SJamie Gritton if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 17222b0d6f81SJamie Gritton error = EINVAL; 17232b0d6f81SJamie Gritton vfs_opterror(opts, 17242b0d6f81SJamie Gritton "ip4 cannot be changed after creation"); 17252a4b2251SJamie Gritton goto done_deref; 17262b0d6f81SJamie Gritton } 17272b0d6f81SJamie Gritton #endif 17282b0d6f81SJamie Gritton #ifdef INET6 17292b0d6f81SJamie Gritton if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 17302b0d6f81SJamie Gritton error = EINVAL; 17312b0d6f81SJamie Gritton vfs_opterror(opts, 17322b0d6f81SJamie Gritton "ip6 cannot be changed after creation"); 17332a4b2251SJamie Gritton goto done_deref; 17342b0d6f81SJamie Gritton } 17352b0d6f81SJamie Gritton #endif 1736b38ff370SJamie Gritton } 1737b38ff370SJamie Gritton 1738b38ff370SJamie Gritton /* Do final error checking before setting anything. */ 17390304c731SJamie Gritton if (gotslevel) { 17400304c731SJamie Gritton if (slevel < ppr->pr_securelevel) { 17410304c731SJamie Gritton error = EPERM; 17422a4b2251SJamie Gritton goto done_deref; 17430304c731SJamie Gritton } 17440304c731SJamie Gritton } 1745b97457e2SJamie Gritton if (gotchildmax) { 1746b97457e2SJamie Gritton if (childmax >= ppr->pr_childmax) { 1747b97457e2SJamie Gritton error = EPERM; 17482a4b2251SJamie Gritton goto done_deref; 1749b97457e2SJamie Gritton } 1750b97457e2SJamie Gritton } 17510304c731SJamie Gritton if (gotenforce) { 17520304c731SJamie Gritton if (enforce < ppr->pr_enforce_statfs) { 17530304c731SJamie Gritton error = EPERM; 17542a4b2251SJamie Gritton goto done_deref; 17550304c731SJamie Gritton } 17560304c731SJamie Gritton } 17570cc207a6SMartin Matuska if (gotrsnum) { 17580cc207a6SMartin Matuska /* 17590cc207a6SMartin Matuska * devfs_rsnum is a uint16_t 17600cc207a6SMartin Matuska */ 1761bf3db8aaSMartin Matuska if (rsnum < 0 || rsnum > 65535) { 17620cc207a6SMartin Matuska error = EINVAL; 17632a4b2251SJamie Gritton goto done_deref; 17640cc207a6SMartin Matuska } 17650cc207a6SMartin Matuska /* 1766bf3db8aaSMartin Matuska * Nested jails always inherit parent's devfs ruleset 17670cc207a6SMartin Matuska */ 17680cc207a6SMartin Matuska if (jailed(td->td_ucred)) { 17690cc207a6SMartin Matuska if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) { 17700cc207a6SMartin Matuska error = EPERM; 17712a4b2251SJamie Gritton goto done_deref; 1772bf3db8aaSMartin Matuska } else 17730cc207a6SMartin Matuska rsnum = ppr->pr_devfs_rsnum; 17740cc207a6SMartin Matuska } 17750cc207a6SMartin Matuska } 1776b38ff370SJamie Gritton #ifdef INET 17772b0d6f81SJamie Gritton if (ip4s > 0) { 1778eb8dcdeaSGleb Smirnoff if ((ppr->pr_flags & PR_IP4) && 1779eb8dcdeaSGleb Smirnoff !prison_ip_parent_match(ppr->pr_addrs[PR_INET], ip4, 1780eb8dcdeaSGleb Smirnoff PR_INET)) { 17810304c731SJamie Gritton error = EPERM; 17822a4b2251SJamie Gritton goto done_deref; 17830304c731SJamie Gritton } 1784eb8dcdeaSGleb Smirnoff if (!prison_ip_conflict_check(ppr, pr, ip4, PR_INET)) { 17850304c731SJamie Gritton error = EADDRINUSE; 1786eb8dcdeaSGleb Smirnoff vfs_opterror(opts, "IPv4 addresses clash"); 17872a4b2251SJamie Gritton goto done_deref; 1788b38ff370SJamie Gritton } 17890304c731SJamie Gritton } 1790b38ff370SJamie Gritton #endif 1791b38ff370SJamie Gritton #ifdef INET6 17922b0d6f81SJamie Gritton if (ip6s > 0) { 1793eb8dcdeaSGleb Smirnoff if ((ppr->pr_flags & PR_IP6) && 1794eb8dcdeaSGleb Smirnoff !prison_ip_parent_match(ppr->pr_addrs[PR_INET6], ip6, 1795eb8dcdeaSGleb Smirnoff PR_INET6)) { 17960304c731SJamie Gritton error = EPERM; 17972a4b2251SJamie Gritton goto done_deref; 17980304c731SJamie Gritton } 1799eb8dcdeaSGleb Smirnoff if (!prison_ip_conflict_check(ppr, pr, ip6, PR_INET6)) { 18000304c731SJamie Gritton error = EADDRINUSE; 1801eb8dcdeaSGleb Smirnoff vfs_opterror(opts, "IPv6 addresses clash"); 18022a4b2251SJamie Gritton goto done_deref; 1803b38ff370SJamie Gritton } 18040304c731SJamie Gritton } 1805b38ff370SJamie Gritton #endif 18060304c731SJamie Gritton onamelen = namelen = 0; 1807b6f47c23SJamie Gritton if (namelc != NULL) { 18086ab6058eSJamie Gritton /* Give a default name of the jid. Also allow the name to be 18096ab6058eSJamie Gritton * explicitly the jid - but not any other number, and only in 18106ab6058eSJamie Gritton * normal form (no leading zero/etc). 18116ab6058eSJamie Gritton */ 1812b6f47c23SJamie Gritton if (namelc[0] == '\0') 1813b6f47c23SJamie Gritton snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid); 18146ab6058eSJamie Gritton else if ((strtoul(namelc, &p, 10) != jid || 18156ab6058eSJamie Gritton namelc[0] < '1' || namelc[0] > '9') && *p == '\0') { 1816b38ff370SJamie Gritton error = EINVAL; 1817babbbb9cSJamie Gritton vfs_opterror(opts, 1818babbbb9cSJamie Gritton "name cannot be numeric (unless it is the jid)"); 18192a4b2251SJamie Gritton goto done_deref; 1820b38ff370SJamie Gritton } 1821b38ff370SJamie Gritton /* 18220304c731SJamie Gritton * Make sure the name isn't too long for the prison or its 18230304c731SJamie Gritton * children. 1824b38ff370SJamie Gritton */ 1825b6f47c23SJamie Gritton pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 1826b6f47c23SJamie Gritton onamelen = strlen(pr->pr_name + pnamelen); 1827b6f47c23SJamie Gritton namelen = strlen(namelc); 1828b6f47c23SJamie Gritton if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) { 18290304c731SJamie Gritton error = ENAMETOOLONG; 18302a4b2251SJamie Gritton goto done_deref; 18310304c731SJamie Gritton } 18320304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 18330304c731SJamie Gritton if (strlen(tpr->pr_name) + (namelen - onamelen) >= 18340304c731SJamie Gritton sizeof(pr->pr_name)) { 18350304c731SJamie Gritton error = ENAMETOOLONG; 18362a4b2251SJamie Gritton goto done_deref; 18370304c731SJamie Gritton } 18380304c731SJamie Gritton } 18390304c731SJamie Gritton } 1840b3079544SJamie Gritton pr_allow_diff = pr_allow & ~ppr->pr_allow; 1841b3079544SJamie Gritton if (pr_allow_diff & ~PR_ALLOW_DIFFERENCES) { 18420304c731SJamie Gritton error = EPERM; 18432a4b2251SJamie Gritton goto done_deref; 1844b38ff370SJamie Gritton } 1845b38ff370SJamie Gritton 1846b6f47c23SJamie Gritton /* 1847b6f47c23SJamie Gritton * Let modules check their parameters. This requires unlocking and 1848b6f47c23SJamie Gritton * then re-locking the prison, but this is still a valid state as long 1849b6f47c23SJamie Gritton * as allprison_lock remains xlocked. 1850b6f47c23SJamie Gritton */ 1851b6f47c23SJamie Gritton mtx_unlock(&pr->pr_mtx); 18522a4b2251SJamie Gritton drflags &= ~PD_LOCKED; 1853b6f47c23SJamie Gritton error = osd_jail_call(pr, PR_METHOD_CHECK, opts); 18542a4b2251SJamie Gritton if (error != 0) 18552a4b2251SJamie Gritton goto done_deref; 1856b6f47c23SJamie Gritton mtx_lock(&pr->pr_mtx); 18572a4b2251SJamie Gritton drflags |= PD_LOCKED; 1858b6f47c23SJamie Gritton 1859b6f47c23SJamie Gritton /* At this point, all valid parameters should have been noted. */ 1860b6f47c23SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 1861b6f47c23SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 1862b6f47c23SJamie Gritton error = EINVAL; 1863b6f47c23SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 18642a4b2251SJamie Gritton goto done_deref; 1865b6f47c23SJamie Gritton } 1866b6f47c23SJamie Gritton } 1867b6f47c23SJamie Gritton 1868b38ff370SJamie Gritton /* Set the parameters of the prison. */ 1869b38ff370SJamie Gritton #ifdef INET 18708bce8d28SZhenlei Huang redo_ip4 = false; 18710304c731SJamie Gritton if (pr_flags & PR_IP4_USER) { 18720304c731SJamie Gritton pr->pr_flags |= PR_IP4; 1873eb8dcdeaSGleb Smirnoff prison_ip_set(pr, PR_INET, ip4); 1874b38ff370SJamie Gritton ip4 = NULL; 18750304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1876bdfc8cc4SJamie Gritton #ifdef VIMAGE 1877bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1878bdfc8cc4SJamie Gritton descend = 0; 1879bdfc8cc4SJamie Gritton continue; 1880bdfc8cc4SJamie Gritton } 1881bdfc8cc4SJamie Gritton #endif 18828bce8d28SZhenlei Huang if (!prison_ip_restrict(tpr, PR_INET, NULL)) { 18838bce8d28SZhenlei Huang redo_ip4 = true; 18840304c731SJamie Gritton descend = 0; 18850304c731SJamie Gritton } 18860304c731SJamie Gritton } 18870304c731SJamie Gritton } 1888b38ff370SJamie Gritton #endif 1889b38ff370SJamie Gritton #ifdef INET6 18908bce8d28SZhenlei Huang redo_ip6 = false; 18910304c731SJamie Gritton if (pr_flags & PR_IP6_USER) { 18920304c731SJamie Gritton pr->pr_flags |= PR_IP6; 1893eb8dcdeaSGleb Smirnoff prison_ip_set(pr, PR_INET6, ip6); 1894b38ff370SJamie Gritton ip6 = NULL; 18950304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1896bdfc8cc4SJamie Gritton #ifdef VIMAGE 1897bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1898bdfc8cc4SJamie Gritton descend = 0; 1899bdfc8cc4SJamie Gritton continue; 1900bdfc8cc4SJamie Gritton } 1901bdfc8cc4SJamie Gritton #endif 19028bce8d28SZhenlei Huang if (!prison_ip_restrict(tpr, PR_INET6, NULL)) { 19038bce8d28SZhenlei Huang redo_ip6 = true; 19040304c731SJamie Gritton descend = 0; 19050304c731SJamie Gritton } 19060304c731SJamie Gritton } 19070304c731SJamie Gritton } 1908b38ff370SJamie Gritton #endif 19090304c731SJamie Gritton if (gotslevel) { 1910b38ff370SJamie Gritton pr->pr_securelevel = slevel; 19110304c731SJamie Gritton /* Set all child jails to be at least this level. */ 19120304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 19130304c731SJamie Gritton if (tpr->pr_securelevel < slevel) 19140304c731SJamie Gritton tpr->pr_securelevel = slevel; 19150304c731SJamie Gritton } 1916b97457e2SJamie Gritton if (gotchildmax) { 1917b97457e2SJamie Gritton pr->pr_childmax = childmax; 1918b97457e2SJamie Gritton /* Set all child jails to under this limit. */ 1919b97457e2SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level) 1920b97457e2SJamie Gritton if (tpr->pr_childmax > childmax - level) 1921b97457e2SJamie Gritton tpr->pr_childmax = childmax > level 1922b97457e2SJamie Gritton ? childmax - level : 0; 1923b97457e2SJamie Gritton } 19240304c731SJamie Gritton if (gotenforce) { 19250304c731SJamie Gritton pr->pr_enforce_statfs = enforce; 19260304c731SJamie Gritton /* Pass this restriction on to the children. */ 19270304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 19280304c731SJamie Gritton if (tpr->pr_enforce_statfs < enforce) 19290304c731SJamie Gritton tpr->pr_enforce_statfs = enforce; 19300304c731SJamie Gritton } 19310cc207a6SMartin Matuska if (gotrsnum) { 19320cc207a6SMartin Matuska pr->pr_devfs_rsnum = rsnum; 19330cc207a6SMartin Matuska /* Pass this restriction on to the children. */ 19340cc207a6SMartin Matuska FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 19350cc207a6SMartin Matuska tpr->pr_devfs_rsnum = rsnum; 19360cc207a6SMartin Matuska } 1937b6f47c23SJamie Gritton if (namelc != NULL) { 19380304c731SJamie Gritton if (ppr == &prison0) 1939b6f47c23SJamie Gritton strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name)); 19400304c731SJamie Gritton else 19410304c731SJamie Gritton snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s", 1942b6f47c23SJamie Gritton ppr->pr_name, namelc); 19430304c731SJamie Gritton /* Change this component of child names. */ 19440304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 19450304c731SJamie Gritton bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen, 19460304c731SJamie Gritton strlen(tpr->pr_name + onamelen) + 1); 19470304c731SJamie Gritton bcopy(pr->pr_name, tpr->pr_name, namelen); 19480304c731SJamie Gritton } 19490304c731SJamie Gritton } 1950b38ff370SJamie Gritton if (path != NULL) { 19510304c731SJamie Gritton /* Try to keep a real-rooted full pathname. */ 1952b38ff370SJamie Gritton strlcpy(pr->pr_path, path, sizeof(pr->pr_path)); 1953b38ff370SJamie Gritton pr->pr_root = root; 19542a4b2251SJamie Gritton root = NULL; 1955b38ff370SJamie Gritton } 195676ca6f88SJamie Gritton if (PR_HOST & ch_flags & ~pr_flags) { 195776ca6f88SJamie Gritton if (pr->pr_flags & PR_HOST) { 195876ca6f88SJamie Gritton /* 195976ca6f88SJamie Gritton * Copy the parent's host info. As with pr_ip4 above, 196076ca6f88SJamie Gritton * the lack of a lock on the parent is not a problem; 196176ca6f88SJamie Gritton * it is always set with allprison_lock at least 196276ca6f88SJamie Gritton * shared, and is held exclusively here. 196376ca6f88SJamie Gritton */ 1964c1f19219SJamie Gritton strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname, 1965c1f19219SJamie Gritton sizeof(pr->pr_hostname)); 1966c1f19219SJamie Gritton strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname, 1967c1f19219SJamie Gritton sizeof(pr->pr_domainname)); 1968c1f19219SJamie Gritton strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid, 1969c1f19219SJamie Gritton sizeof(pr->pr_hostuuid)); 197076ca6f88SJamie Gritton pr->pr_hostid = pr->pr_parent->pr_hostid; 197176ca6f88SJamie Gritton } 197276ca6f88SJamie Gritton } else if (host != NULL || domain != NULL || uuid != NULL || gothid) { 197376ca6f88SJamie Gritton /* Set this prison, and any descendants without PR_HOST. */ 1974b38ff370SJamie Gritton if (host != NULL) 1975c1f19219SJamie Gritton strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname)); 197676ca6f88SJamie Gritton if (domain != NULL) 1977c1f19219SJamie Gritton strlcpy(pr->pr_domainname, domain, 1978c1f19219SJamie Gritton sizeof(pr->pr_domainname)); 197976ca6f88SJamie Gritton if (uuid != NULL) 1980c1f19219SJamie Gritton strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid)); 198176ca6f88SJamie Gritton if (gothid) 198276ca6f88SJamie Gritton pr->pr_hostid = hid; 198376ca6f88SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 198476ca6f88SJamie Gritton if (tpr->pr_flags & PR_HOST) 198576ca6f88SJamie Gritton descend = 0; 198676ca6f88SJamie Gritton else { 198776ca6f88SJamie Gritton if (host != NULL) 1988c1f19219SJamie Gritton strlcpy(tpr->pr_hostname, 1989c1f19219SJamie Gritton pr->pr_hostname, 1990c1f19219SJamie Gritton sizeof(tpr->pr_hostname)); 199176ca6f88SJamie Gritton if (domain != NULL) 1992c1f19219SJamie Gritton strlcpy(tpr->pr_domainname, 1993c1f19219SJamie Gritton pr->pr_domainname, 1994c1f19219SJamie Gritton sizeof(tpr->pr_domainname)); 199576ca6f88SJamie Gritton if (uuid != NULL) 1996c1f19219SJamie Gritton strlcpy(tpr->pr_hostuuid, 1997c1f19219SJamie Gritton pr->pr_hostuuid, 1998c1f19219SJamie Gritton sizeof(tpr->pr_hostuuid)); 199976ca6f88SJamie Gritton if (gothid) 200076ca6f88SJamie Gritton tpr->pr_hostid = hid; 200176ca6f88SJamie Gritton } 200276ca6f88SJamie Gritton } 200376ca6f88SJamie Gritton } 20040304c731SJamie Gritton pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow; 20050fe74ae6SJamie Gritton if ((tallow = ch_allow & ~pr_allow)) 20060fe74ae6SJamie Gritton prison_set_allow_locked(pr, tallow, 0); 2007b38ff370SJamie Gritton /* 2008b38ff370SJamie Gritton * Persistent prisons get an extra reference, and prisons losing their 20091158508aSJamie Gritton * persist flag lose that reference. 2010b38ff370SJamie Gritton */ 20111158508aSJamie Gritton if (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags)) { 2012b38ff370SJamie Gritton if (pr_flags & PR_PERSIST) { 20136754ae25SJamie Gritton prison_hold(pr); 20141158508aSJamie Gritton /* 2015ed31b3f4SJamie Gritton * This may be a new prison's first user reference, 2016ed31b3f4SJamie Gritton * but wait to call it alive until after OSD calls 2017ed31b3f4SJamie Gritton * have had a chance to run (and perhaps to fail). 20181158508aSJamie Gritton */ 20196754ae25SJamie Gritton refcount_acquire(&pr->pr_uref); 2020b38ff370SJamie Gritton } else { 202139c8ef90SJamie Gritton drflags |= PD_DEUREF; 2022f7496dcaSJamie Gritton prison_free_not_last(pr); 2023b38ff370SJamie Gritton } 2024b38ff370SJamie Gritton } 2025b38ff370SJamie Gritton pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags; 2026b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 20272a4b2251SJamie Gritton drflags &= ~PD_LOCKED; 2028c861373bSJamie Gritton /* 2029c861373bSJamie Gritton * Any errors past this point will need to de-persist newly created 2030c861373bSJamie Gritton * prisons, as well as call remove methods. 2031c861373bSJamie Gritton */ 2032ed31b3f4SJamie Gritton if (created) 2033c861373bSJamie Gritton drflags |= PD_KILL; 2034b38ff370SJamie Gritton 2035a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 20364b5c9cf6SEdward Tomasz Napierala if (racct_enable && created) 2037a7ad07bfSEdward Tomasz Napierala prison_racct_attach(pr); 2038a7ad07bfSEdward Tomasz Napierala #endif 2039a7ad07bfSEdward Tomasz Napierala 20400304c731SJamie Gritton /* Locks may have prevented a complete restriction of child IP 20410304c731SJamie Gritton * addresses. If so, allocate some more memory and try again. 20420304c731SJamie Gritton */ 20430304c731SJamie Gritton #ifdef INET 20440304c731SJamie Gritton while (redo_ip4) { 2045eb8dcdeaSGleb Smirnoff ip4s = pr->pr_addrs[PR_INET]->ips; 20468bce8d28SZhenlei Huang MPASS(ip4 == NULL); 2047eb8dcdeaSGleb Smirnoff ip4 = prison_ip_alloc(PR_INET, ip4s, M_WAITOK); 20480304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 20498bce8d28SZhenlei Huang redo_ip4 = false; 20500304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 2051bdfc8cc4SJamie Gritton #ifdef VIMAGE 2052bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 2053bdfc8cc4SJamie Gritton descend = 0; 2054bdfc8cc4SJamie Gritton continue; 2055bdfc8cc4SJamie Gritton } 2056bdfc8cc4SJamie Gritton #endif 2057b2d76b52SZhenlei Huang if (!prison_ip_restrict(tpr, PR_INET, &ip4)) 2058b2d76b52SZhenlei Huang redo_ip4 = true; 20590304c731SJamie Gritton } 20600304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 20610304c731SJamie Gritton } 20620304c731SJamie Gritton #endif 20630304c731SJamie Gritton #ifdef INET6 20640304c731SJamie Gritton while (redo_ip6) { 2065eb8dcdeaSGleb Smirnoff ip6s = pr->pr_addrs[PR_INET6]->ips; 20668bce8d28SZhenlei Huang MPASS(ip6 == NULL); 2067eb8dcdeaSGleb Smirnoff ip6 = prison_ip_alloc(PR_INET6, ip6s, M_WAITOK); 20680304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 20698bce8d28SZhenlei Huang redo_ip6 = false; 20700304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 2071bdfc8cc4SJamie Gritton #ifdef VIMAGE 2072bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 2073bdfc8cc4SJamie Gritton descend = 0; 2074bdfc8cc4SJamie Gritton continue; 2075bdfc8cc4SJamie Gritton } 2076bdfc8cc4SJamie Gritton #endif 2077b2d76b52SZhenlei Huang if (!prison_ip_restrict(tpr, PR_INET6, &ip6)) 2078b2d76b52SZhenlei Huang redo_ip6 = true; 20790304c731SJamie Gritton } 20800304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 20810304c731SJamie Gritton } 20820304c731SJamie Gritton #endif 20830304c731SJamie Gritton 2084b38ff370SJamie Gritton /* Let the modules do their work. */ 2085ed31b3f4SJamie Gritton if (created) { 2086b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_CREATE, opts); 2087c861373bSJamie Gritton if (error) 20882a4b2251SJamie Gritton goto done_deref; 2089b38ff370SJamie Gritton } 2090b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_SET, opts); 2091c861373bSJamie Gritton if (error) 20922a4b2251SJamie Gritton goto done_deref; 2093b38ff370SJamie Gritton 20941158508aSJamie Gritton /* 20951158508aSJamie Gritton * A new prison is now ready to be seen; either it has gained a user 20961158508aSJamie Gritton * reference via persistence, or is about to gain one via attachment. 20971158508aSJamie Gritton */ 2098ed31b3f4SJamie Gritton if (created) { 20991158508aSJamie Gritton drflags = prison_lock_xlock(pr, drflags); 21001158508aSJamie Gritton pr->pr_state = PRISON_STATE_ALIVE; 21011158508aSJamie Gritton } 21021158508aSJamie Gritton 2103b38ff370SJamie Gritton /* Attach this process to the prison if requested. */ 2104b38ff370SJamie Gritton if (flags & JAIL_ATTACH) { 2105c861373bSJamie Gritton error = do_jail_attach(td, pr, 2106589e4c1dSJamie Gritton prison_lock_xlock(pr, drflags & PD_LOCK_FLAGS)); 2107f7496dcaSJamie Gritton drflags &= ~(PD_LOCKED | PD_LIST_XLOCKED); 2108b38ff370SJamie Gritton if (error) { 2109b38ff370SJamie Gritton vfs_opterror(opts, "attach failed"); 21102a4b2251SJamie Gritton goto done_deref; 2111b38ff370SJamie Gritton } 2112b38ff370SJamie Gritton } 2113b38ff370SJamie Gritton 21141fb24974SEdward Tomasz Napierala #ifdef RACCT 21154b5c9cf6SEdward Tomasz Napierala if (racct_enable && !created) { 2116701d6b50SJamie Gritton if (drflags & PD_LOCKED) { 2117701d6b50SJamie Gritton mtx_unlock(&pr->pr_mtx); 2118701d6b50SJamie Gritton drflags &= ~PD_LOCKED; 2119701d6b50SJamie Gritton } 2120f7496dcaSJamie Gritton if (drflags & PD_LIST_XLOCKED) { 2121f7496dcaSJamie Gritton sx_xunlock(&allprison_lock); 2122f7496dcaSJamie Gritton drflags &= ~PD_LIST_XLOCKED; 2123b4e87a63SJamie Gritton } 21241fb24974SEdward Tomasz Napierala prison_racct_modify(pr); 21251fb24974SEdward Tomasz Napierala } 21261fb24974SEdward Tomasz Napierala #endif 21271fb24974SEdward Tomasz Napierala 2128ed31b3f4SJamie Gritton if (created && pr != &prison0 && (pr->pr_allow & PR_ALLOW_NFSD) != 0 && 2129bba7a2e8SRick Macklem (pr->pr_root->v_vflag & VV_ROOT) == 0) 2130bba7a2e8SRick Macklem printf("Warning jail jid=%d: mountd/nfsd requires a separate" 2131bba7a2e8SRick Macklem " file system\n", pr->pr_id); 2132bba7a2e8SRick Macklem 2133c861373bSJamie Gritton drflags &= ~PD_KILL; 21341fb24974SEdward Tomasz Napierala td->td_retval[0] = pr->pr_id; 21351fb24974SEdward Tomasz Napierala 21362a4b2251SJamie Gritton done_deref: 21372a4b2251SJamie Gritton /* Release any temporary prison holds and/or locks. */ 21382a4b2251SJamie Gritton if (pr != NULL) 21392a4b2251SJamie Gritton prison_deref(pr, drflags); 21402a4b2251SJamie Gritton else if (drflags & PD_LIST_SLOCKED) 2141b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 21422a4b2251SJamie Gritton else if (drflags & PD_LIST_XLOCKED) 2143b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 21445050aa86SKonstantin Belousov if (root != NULL) 2145b38ff370SJamie Gritton vrele(root); 2146b38ff370SJamie Gritton done_errmsg: 2147b38ff370SJamie Gritton if (error) { 21482a4b2251SJamie Gritton /* Write the error message back to userspace. */ 2149176ff3a0SJamie Gritton if (vfs_getopt(opts, "errmsg", (void **)&errmsg, 2150176ff3a0SJamie Gritton &errmsg_len) == 0 && errmsg_len > 0) { 2151b38ff370SJamie Gritton errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1; 2152b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 2153b38ff370SJamie Gritton bcopy(errmsg, 2154b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2155b38ff370SJamie Gritton errmsg_len); 2156b38ff370SJamie Gritton else 2157abbc260fSMark Johnston (void)copyout(errmsg, 2158b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2159b38ff370SJamie Gritton errmsg_len); 2160b38ff370SJamie Gritton } 2161b38ff370SJamie Gritton } 2162b38ff370SJamie Gritton done_free: 2163b38ff370SJamie Gritton #ifdef INET 2164eb8dcdeaSGleb Smirnoff prison_ip_free(ip4); 2165b38ff370SJamie Gritton #endif 2166b38ff370SJamie Gritton #ifdef INET6 2167eb8dcdeaSGleb Smirnoff prison_ip_free(ip6); 2168b38ff370SJamie Gritton #endif 21696dfe0a3dSMartin Matuska if (g_path != NULL) 21706dfe0a3dSMartin Matuska free(g_path, M_TEMP); 2171b38ff370SJamie Gritton vfs_freeopts(opts); 2172b38ff370SJamie Gritton return (error); 2173b38ff370SJamie Gritton } 2174b38ff370SJamie Gritton 2175b38ff370SJamie Gritton /* 21767de883c8SJamie Gritton * Find the next available prison ID. Return the ID on success, or zero 21777de883c8SJamie Gritton * on failure. Also set a pointer to the allprison list entry the prison 21787de883c8SJamie Gritton * should be inserted before. 21797de883c8SJamie Gritton */ 21807de883c8SJamie Gritton static int 21817de883c8SJamie Gritton get_next_prid(struct prison **insprp) 21827de883c8SJamie Gritton { 21837de883c8SJamie Gritton struct prison *inspr; 21847de883c8SJamie Gritton int jid, maxid; 21857de883c8SJamie Gritton 21867de883c8SJamie Gritton jid = lastprid % JAIL_MAX + 1; 21877de883c8SJamie Gritton if (TAILQ_EMPTY(&allprison) || 21887de883c8SJamie Gritton TAILQ_LAST(&allprison, prisonlist)->pr_id < jid) { 21897de883c8SJamie Gritton /* 21907de883c8SJamie Gritton * A common case is for all jails to be implicitly numbered, 21917de883c8SJamie Gritton * which means they'll go on the end of the list, at least 21927de883c8SJamie Gritton * for the first JAIL_MAX times. 21937de883c8SJamie Gritton */ 21947de883c8SJamie Gritton inspr = NULL; 21957de883c8SJamie Gritton } else { 21967de883c8SJamie Gritton /* 21977de883c8SJamie Gritton * Take two passes through the allprison list: first starting 21987de883c8SJamie Gritton * with the proposed jid, then ending with it. 21997de883c8SJamie Gritton */ 22007de883c8SJamie Gritton for (maxid = JAIL_MAX; maxid != 0; ) { 22017de883c8SJamie Gritton TAILQ_FOREACH(inspr, &allprison, pr_list) { 22027de883c8SJamie Gritton if (inspr->pr_id < jid) 22037de883c8SJamie Gritton continue; 2204f7496dcaSJamie Gritton if (inspr->pr_id > jid) { 2205f7496dcaSJamie Gritton /* Found an opening. */ 22067de883c8SJamie Gritton maxid = 0; 22077de883c8SJamie Gritton break; 22087de883c8SJamie Gritton } 22097de883c8SJamie Gritton if (++jid > maxid) { 22107de883c8SJamie Gritton if (lastprid == maxid || lastprid == 0) 22117de883c8SJamie Gritton { 22127de883c8SJamie Gritton /* 22137de883c8SJamie Gritton * The entire legal range 22147de883c8SJamie Gritton * has been traversed 22157de883c8SJamie Gritton */ 22167de883c8SJamie Gritton return 0; 22177de883c8SJamie Gritton } 22187de883c8SJamie Gritton /* Try again from the start. */ 22197de883c8SJamie Gritton jid = 1; 22207de883c8SJamie Gritton maxid = lastprid; 22217de883c8SJamie Gritton break; 22227de883c8SJamie Gritton } 22237de883c8SJamie Gritton } 22247de883c8SJamie Gritton if (inspr == NULL) { 22257de883c8SJamie Gritton /* Found room at the end of the list. */ 22267de883c8SJamie Gritton break; 22277de883c8SJamie Gritton } 22287de883c8SJamie Gritton } 22297de883c8SJamie Gritton } 22307de883c8SJamie Gritton *insprp = inspr; 22317de883c8SJamie Gritton lastprid = jid; 22327de883c8SJamie Gritton return (jid); 22337de883c8SJamie Gritton } 22347de883c8SJamie Gritton 22357de883c8SJamie Gritton /* 2236ed31b3f4SJamie Gritton * Find the next available ID for a renumbered dead prison. This is the same 2237ed31b3f4SJamie Gritton * as get_next_prid, but counting backward from the end of the range. 2238ed31b3f4SJamie Gritton */ 2239ed31b3f4SJamie Gritton static int 2240ed31b3f4SJamie Gritton get_next_deadid(struct prison **dinsprp) 2241ed31b3f4SJamie Gritton { 2242ed31b3f4SJamie Gritton struct prison *dinspr; 2243ed31b3f4SJamie Gritton int deadid, minid; 2244ed31b3f4SJamie Gritton 2245ed31b3f4SJamie Gritton deadid = lastdeadid ? lastdeadid - 1 : JAIL_MAX; 2246ed31b3f4SJamie Gritton /* 2247ed31b3f4SJamie Gritton * Take two reverse passes through the allprison list: first 2248ed31b3f4SJamie Gritton * starting with the proposed deadid, then ending with it. 2249ed31b3f4SJamie Gritton */ 2250ed31b3f4SJamie Gritton for (minid = 1; minid != 0; ) { 2251ed31b3f4SJamie Gritton TAILQ_FOREACH_REVERSE(dinspr, &allprison, prisonlist, pr_list) { 2252ed31b3f4SJamie Gritton if (dinspr->pr_id > deadid) 2253ed31b3f4SJamie Gritton continue; 2254ed31b3f4SJamie Gritton if (dinspr->pr_id < deadid) { 2255ed31b3f4SJamie Gritton /* Found an opening. */ 2256ed31b3f4SJamie Gritton minid = 0; 2257ed31b3f4SJamie Gritton break; 2258ed31b3f4SJamie Gritton } 2259ed31b3f4SJamie Gritton if (--deadid < minid) { 2260ed31b3f4SJamie Gritton if (lastdeadid == minid || lastdeadid == 0) 2261ed31b3f4SJamie Gritton { 2262ed31b3f4SJamie Gritton /* 2263ed31b3f4SJamie Gritton * The entire legal range 2264ed31b3f4SJamie Gritton * has been traversed 2265ed31b3f4SJamie Gritton */ 2266ed31b3f4SJamie Gritton return 0; 2267ed31b3f4SJamie Gritton } 2268ed31b3f4SJamie Gritton /* Try again from the end. */ 2269ed31b3f4SJamie Gritton deadid = JAIL_MAX; 2270ed31b3f4SJamie Gritton minid = lastdeadid; 2271ed31b3f4SJamie Gritton break; 2272ed31b3f4SJamie Gritton } 2273ed31b3f4SJamie Gritton } 2274ed31b3f4SJamie Gritton if (dinspr == NULL) { 2275ed31b3f4SJamie Gritton /* Found room at the beginning of the list. */ 2276ed31b3f4SJamie Gritton break; 2277ed31b3f4SJamie Gritton } 2278ed31b3f4SJamie Gritton } 2279ed31b3f4SJamie Gritton *dinsprp = dinspr; 2280ed31b3f4SJamie Gritton lastdeadid = deadid; 2281ed31b3f4SJamie Gritton return (deadid); 2282ed31b3f4SJamie Gritton } 2283ed31b3f4SJamie Gritton 2284ed31b3f4SJamie Gritton /* 2285b38ff370SJamie Gritton * struct jail_get_args { 2286b38ff370SJamie Gritton * struct iovec *iovp; 2287b38ff370SJamie Gritton * unsigned int iovcnt; 2288b38ff370SJamie Gritton * int flags; 2289b38ff370SJamie Gritton * }; 2290b38ff370SJamie Gritton */ 2291b38ff370SJamie Gritton int 22928451d0ddSKip Macy sys_jail_get(struct thread *td, struct jail_get_args *uap) 2293b38ff370SJamie Gritton { 2294b38ff370SJamie Gritton struct uio *auio; 2295b38ff370SJamie Gritton int error; 2296b38ff370SJamie Gritton 2297b38ff370SJamie Gritton /* Check that we have an even number of iovecs. */ 2298b38ff370SJamie Gritton if (uap->iovcnt & 1) 2299b38ff370SJamie Gritton return (EINVAL); 2300b38ff370SJamie Gritton 2301b38ff370SJamie Gritton error = copyinuio(uap->iovp, uap->iovcnt, &auio); 2302b38ff370SJamie Gritton if (error) 2303b38ff370SJamie Gritton return (error); 2304b38ff370SJamie Gritton error = kern_jail_get(td, auio, uap->flags); 2305b38ff370SJamie Gritton if (error == 0) 2306b38ff370SJamie Gritton error = copyout(auio->uio_iov, uap->iovp, 2307b38ff370SJamie Gritton uap->iovcnt * sizeof(struct iovec)); 230861cc4830SAlfredo Mazzinghi freeuio(auio); 2309b38ff370SJamie Gritton return (error); 2310b38ff370SJamie Gritton } 2311b38ff370SJamie Gritton 2312b38ff370SJamie Gritton int 2313b38ff370SJamie Gritton kern_jail_get(struct thread *td, struct uio *optuio, int flags) 2314b38ff370SJamie Gritton { 2315672756aaSJamie Gritton struct bool_flags *bf; 2316672756aaSJamie Gritton struct jailsys_flags *jsf; 23170304c731SJamie Gritton struct prison *pr, *mypr; 2318b38ff370SJamie Gritton struct vfsopt *opt; 2319b38ff370SJamie Gritton struct vfsoptlist *opts; 2320b38ff370SJamie Gritton char *errmsg, *name; 23212a4b2251SJamie Gritton int drflags, error, errmsg_len, errmsg_pos, i, jid, len, pos; 2322672756aaSJamie Gritton unsigned f; 2323b38ff370SJamie Gritton 2324b38ff370SJamie Gritton if (flags & ~JAIL_GET_MASK) 2325b38ff370SJamie Gritton return (EINVAL); 2326b38ff370SJamie Gritton 2327b38ff370SJamie Gritton /* Get the parameter list. */ 2328b38ff370SJamie Gritton error = vfs_buildopts(optuio, &opts); 2329b38ff370SJamie Gritton if (error) 2330b38ff370SJamie Gritton return (error); 2331b38ff370SJamie Gritton errmsg_pos = vfs_getopt_pos(opts, "errmsg"); 23320304c731SJamie Gritton mypr = td->td_ucred->cr_prison; 23332a4b2251SJamie Gritton pr = NULL; 23341e2a13e6SJamie Gritton 2335b38ff370SJamie Gritton /* 2336b38ff370SJamie Gritton * Find the prison specified by one of: lastjid, jid, name. 2337b38ff370SJamie Gritton */ 2338b38ff370SJamie Gritton sx_slock(&allprison_lock); 23392a4b2251SJamie Gritton drflags = PD_LIST_SLOCKED; 2340b38ff370SJamie Gritton error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid)); 2341b38ff370SJamie Gritton if (error == 0) { 2342b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 2343f7496dcaSJamie Gritton if (pr->pr_id > jid && 2344f7496dcaSJamie Gritton ((flags & JAIL_DYING) || prison_isalive(pr)) && 2345f7496dcaSJamie Gritton prison_ischild(mypr, pr)) { 2346b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 23472a4b2251SJamie Gritton drflags |= PD_LOCKED; 2348b38ff370SJamie Gritton goto found_prison; 23492a4b2251SJamie Gritton } 2350f7496dcaSJamie Gritton } 2351b38ff370SJamie Gritton error = ENOENT; 2352b38ff370SJamie Gritton vfs_opterror(opts, "no jail after %d", jid); 23532a4b2251SJamie Gritton goto done; 2354b38ff370SJamie Gritton } else if (error != ENOENT) 23552a4b2251SJamie Gritton goto done; 2356b38ff370SJamie Gritton 2357b38ff370SJamie Gritton error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 2358b38ff370SJamie Gritton if (error == 0) { 2359b38ff370SJamie Gritton if (jid != 0) { 23600304c731SJamie Gritton pr = prison_find_child(mypr, jid); 2361b38ff370SJamie Gritton if (pr != NULL) { 23622a4b2251SJamie Gritton drflags |= PD_LOCKED; 236376ad42abSJamie Gritton if (!(prison_isalive(pr) || 236476ad42abSJamie Gritton (flags & JAIL_DYING))) { 2365b38ff370SJamie Gritton error = ENOENT; 2366b38ff370SJamie Gritton vfs_opterror(opts, "jail %d is dying", 2367b38ff370SJamie Gritton jid); 23682a4b2251SJamie Gritton goto done; 2369b38ff370SJamie Gritton } 2370b38ff370SJamie Gritton goto found_prison; 2371b38ff370SJamie Gritton } 2372b38ff370SJamie Gritton error = ENOENT; 2373b38ff370SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 23742a4b2251SJamie Gritton goto done; 2375b38ff370SJamie Gritton } 2376b38ff370SJamie Gritton } else if (error != ENOENT) 23772a4b2251SJamie Gritton goto done; 2378b38ff370SJamie Gritton 2379b38ff370SJamie Gritton error = vfs_getopt(opts, "name", (void **)&name, &len); 2380b38ff370SJamie Gritton if (error == 0) { 2381b38ff370SJamie Gritton if (len == 0 || name[len - 1] != '\0') { 2382b38ff370SJamie Gritton error = EINVAL; 23832a4b2251SJamie Gritton goto done; 2384b38ff370SJamie Gritton } 23850304c731SJamie Gritton pr = prison_find_name(mypr, name); 2386b38ff370SJamie Gritton if (pr != NULL) { 23872a4b2251SJamie Gritton drflags |= PD_LOCKED; 238876ad42abSJamie Gritton if (!(prison_isalive(pr) || (flags & JAIL_DYING))) { 2389b38ff370SJamie Gritton error = ENOENT; 2390b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" is dying", 2391b38ff370SJamie Gritton name); 23922a4b2251SJamie Gritton goto done; 2393b38ff370SJamie Gritton } 2394b38ff370SJamie Gritton goto found_prison; 2395b38ff370SJamie Gritton } 2396b38ff370SJamie Gritton error = ENOENT; 2397b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", name); 23982a4b2251SJamie Gritton goto done; 2399b38ff370SJamie Gritton } else if (error != ENOENT) 24002a4b2251SJamie Gritton goto done; 2401b38ff370SJamie Gritton 2402b38ff370SJamie Gritton vfs_opterror(opts, "no jail specified"); 2403b38ff370SJamie Gritton error = ENOENT; 24042a4b2251SJamie Gritton goto done; 2405b38ff370SJamie Gritton 2406b38ff370SJamie Gritton found_prison: 2407b38ff370SJamie Gritton /* Get the parameters of the prison. */ 24086754ae25SJamie Gritton prison_hold(pr); 24092a4b2251SJamie Gritton drflags |= PD_DEREF; 2410b38ff370SJamie Gritton td->td_retval[0] = pr->pr_id; 2411b38ff370SJamie Gritton error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id)); 2412b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24132a4b2251SJamie Gritton goto done; 24140304c731SJamie Gritton i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id; 24150304c731SJamie Gritton error = vfs_setopt(opts, "parent", &i, sizeof(i)); 2416b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24172a4b2251SJamie Gritton goto done; 24180304c731SJamie Gritton error = vfs_setopts(opts, "name", prison_name(mypr, pr)); 24190304c731SJamie Gritton if (error != 0 && error != ENOENT) 24202a4b2251SJamie Gritton goto done; 24210304c731SJamie Gritton error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id, 2422b38ff370SJamie Gritton sizeof(pr->pr_cpuset->cs_id)); 2423b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24242a4b2251SJamie Gritton goto done; 24250304c731SJamie Gritton error = vfs_setopts(opts, "path", prison_path(mypr, pr)); 2426b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24272a4b2251SJamie Gritton goto done; 2428b38ff370SJamie Gritton #ifdef INET 2429500f82d6SZhenlei Huang error = vfs_setopt_part(opts, "ip4.addr", pr->pr_addrs[PR_INET]->pr_ip, 2430eb8dcdeaSGleb Smirnoff pr->pr_addrs[PR_INET] ? pr->pr_addrs[PR_INET]->ips * 2431eb8dcdeaSGleb Smirnoff pr_families[PR_INET].size : 0 ); 2432b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24332a4b2251SJamie Gritton goto done; 2434b38ff370SJamie Gritton #endif 2435b38ff370SJamie Gritton #ifdef INET6 2436500f82d6SZhenlei Huang error = vfs_setopt_part(opts, "ip6.addr", pr->pr_addrs[PR_INET6]->pr_ip, 2437eb8dcdeaSGleb Smirnoff pr->pr_addrs[PR_INET6] ? pr->pr_addrs[PR_INET6]->ips * 2438eb8dcdeaSGleb Smirnoff pr_families[PR_INET6].size : 0 ); 2439b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24402a4b2251SJamie Gritton goto done; 2441b38ff370SJamie Gritton #endif 2442b38ff370SJamie Gritton error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel, 2443b38ff370SJamie Gritton sizeof(pr->pr_securelevel)); 2444b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24452a4b2251SJamie Gritton goto done; 2446b97457e2SJamie Gritton error = vfs_setopt(opts, "children.cur", &pr->pr_childcount, 2447b97457e2SJamie Gritton sizeof(pr->pr_childcount)); 2448b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 24492a4b2251SJamie Gritton goto done; 2450b97457e2SJamie Gritton error = vfs_setopt(opts, "children.max", &pr->pr_childmax, 2451b97457e2SJamie Gritton sizeof(pr->pr_childmax)); 2452b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 24532a4b2251SJamie Gritton goto done; 2454c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostname", pr->pr_hostname); 2455b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24562a4b2251SJamie Gritton goto done; 2457c1f19219SJamie Gritton error = vfs_setopts(opts, "host.domainname", pr->pr_domainname); 245876ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 24592a4b2251SJamie Gritton goto done; 2460c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid); 246176ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 24622a4b2251SJamie Gritton goto done; 2463841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32 2464a5c1afadSDmitry Chagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 246576ca6f88SJamie Gritton uint32_t hid32 = pr->pr_hostid; 246676ca6f88SJamie Gritton 246776ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32)); 246876ca6f88SJamie Gritton } else 246976ca6f88SJamie Gritton #endif 247076ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid, 247176ca6f88SJamie Gritton sizeof(pr->pr_hostid)); 247276ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 24732a4b2251SJamie Gritton goto done; 24740304c731SJamie Gritton error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs, 24750304c731SJamie Gritton sizeof(pr->pr_enforce_statfs)); 24760304c731SJamie Gritton if (error != 0 && error != ENOENT) 24772a4b2251SJamie Gritton goto done; 24780cc207a6SMartin Matuska error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum, 24790cc207a6SMartin Matuska sizeof(pr->pr_devfs_rsnum)); 24800cc207a6SMartin Matuska if (error != 0 && error != ENOENT) 24812a4b2251SJamie Gritton goto done; 2482672756aaSJamie Gritton for (bf = pr_flag_bool; 2483672756aaSJamie Gritton bf < pr_flag_bool + nitems(pr_flag_bool); 2484672756aaSJamie Gritton bf++) { 2485672756aaSJamie Gritton i = (pr->pr_flags & bf->flag) ? 1 : 0; 2486672756aaSJamie Gritton error = vfs_setopt(opts, bf->name, &i, sizeof(i)); 2487b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24882a4b2251SJamie Gritton goto done; 2489b38ff370SJamie Gritton i = !i; 2490672756aaSJamie Gritton error = vfs_setopt(opts, bf->noname, &i, sizeof(i)); 2491b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24922a4b2251SJamie Gritton goto done; 24930304c731SJamie Gritton } 2494672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 2495672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 2496672756aaSJamie Gritton jsf++) { 2497672756aaSJamie Gritton f = pr->pr_flags & (jsf->disable | jsf->new); 2498672756aaSJamie Gritton i = (f != 0 && f == jsf->disable) ? JAIL_SYS_DISABLE 2499672756aaSJamie Gritton : (f == jsf->new) ? JAIL_SYS_NEW 25007cbf7213SJamie Gritton : JAIL_SYS_INHERIT; 2501672756aaSJamie Gritton error = vfs_setopt(opts, jsf->name, &i, sizeof(i)); 25027cbf7213SJamie Gritton if (error != 0 && error != ENOENT) 25032a4b2251SJamie Gritton goto done; 25047cbf7213SJamie Gritton } 2505672756aaSJamie Gritton for (bf = pr_flag_allow; 25065d58f959SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 25075d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 2508672756aaSJamie Gritton bf++) { 2509672756aaSJamie Gritton i = (pr->pr_allow & bf->flag) ? 1 : 0; 2510672756aaSJamie Gritton error = vfs_setopt(opts, bf->name, &i, sizeof(i)); 25110304c731SJamie Gritton if (error != 0 && error != ENOENT) 25122a4b2251SJamie Gritton goto done; 25130304c731SJamie Gritton i = !i; 2514672756aaSJamie Gritton error = vfs_setopt(opts, bf->noname, &i, sizeof(i)); 25150304c731SJamie Gritton if (error != 0 && error != ENOENT) 25162a4b2251SJamie Gritton goto done; 25170304c731SJamie Gritton } 251876ad42abSJamie Gritton i = !prison_isalive(pr); 2519b38ff370SJamie Gritton error = vfs_setopt(opts, "dying", &i, sizeof(i)); 2520b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 25212a4b2251SJamie Gritton goto done; 2522b38ff370SJamie Gritton i = !i; 2523b38ff370SJamie Gritton error = vfs_setopt(opts, "nodying", &i, sizeof(i)); 2524b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 25252a4b2251SJamie Gritton goto done; 2526bd96bd15SIan Lepore error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate, 2527bd96bd15SIan Lepore sizeof(pr->pr_osreldate)); 2528a1a4c1b0SIan Lepore if (error != 0 && error != ENOENT) 25292a4b2251SJamie Gritton goto done; 2530a1a4c1b0SIan Lepore error = vfs_setopts(opts, "osrelease", pr->pr_osrelease); 2531a1a4c1b0SIan Lepore if (error != 0 && error != ENOENT) 25322a4b2251SJamie Gritton goto done; 2533b38ff370SJamie Gritton 2534b38ff370SJamie Gritton /* Get the module parameters. */ 2535b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 25362a4b2251SJamie Gritton drflags &= ~PD_LOCKED; 2537b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_GET, opts); 2538b38ff370SJamie Gritton if (error) 25392a4b2251SJamie Gritton goto done; 25402a4b2251SJamie Gritton prison_deref(pr, drflags); 25412a4b2251SJamie Gritton pr = NULL; 25422a4b2251SJamie Gritton drflags = 0; 2543b38ff370SJamie Gritton 2544b38ff370SJamie Gritton /* By now, all parameters should have been noted. */ 2545b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2546b38ff370SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 2547b38ff370SJamie Gritton error = EINVAL; 2548b38ff370SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 25492a4b2251SJamie Gritton goto done; 2550b38ff370SJamie Gritton } 2551b38ff370SJamie Gritton } 2552b38ff370SJamie Gritton 2553b38ff370SJamie Gritton /* Write the fetched parameters back to userspace. */ 2554b38ff370SJamie Gritton error = 0; 2555b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2556b38ff370SJamie Gritton if (opt->pos >= 0 && opt->pos != errmsg_pos) { 2557b38ff370SJamie Gritton pos = 2 * opt->pos + 1; 2558b38ff370SJamie Gritton optuio->uio_iov[pos].iov_len = opt->len; 2559b38ff370SJamie Gritton if (opt->value != NULL) { 2560b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) { 2561b38ff370SJamie Gritton bcopy(opt->value, 2562b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2563b38ff370SJamie Gritton opt->len); 2564b38ff370SJamie Gritton } else { 2565b38ff370SJamie Gritton error = copyout(opt->value, 2566b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2567b38ff370SJamie Gritton opt->len); 2568b38ff370SJamie Gritton if (error) 2569b38ff370SJamie Gritton break; 2570b38ff370SJamie Gritton } 2571b38ff370SJamie Gritton } 2572b38ff370SJamie Gritton } 2573b38ff370SJamie Gritton } 2574b38ff370SJamie Gritton 25752a4b2251SJamie Gritton done: 25762a4b2251SJamie Gritton /* Release any temporary prison holds and/or locks. */ 25772a4b2251SJamie Gritton if (pr != NULL) 25782a4b2251SJamie Gritton prison_deref(pr, drflags); 25792a4b2251SJamie Gritton else if (drflags & PD_LIST_SLOCKED) 2580b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2581b38ff370SJamie Gritton if (error && errmsg_pos >= 0) { 25822a4b2251SJamie Gritton /* Write the error message back to userspace. */ 2583b38ff370SJamie Gritton vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 2584b38ff370SJamie Gritton errmsg_pos = 2 * errmsg_pos + 1; 2585b38ff370SJamie Gritton if (errmsg_len > 0) { 2586b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 2587b38ff370SJamie Gritton bcopy(errmsg, 2588b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2589b38ff370SJamie Gritton errmsg_len); 2590b38ff370SJamie Gritton else 2591abbc260fSMark Johnston (void)copyout(errmsg, 2592b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2593b38ff370SJamie Gritton errmsg_len); 2594b38ff370SJamie Gritton } 2595b38ff370SJamie Gritton } 2596b38ff370SJamie Gritton vfs_freeopts(opts); 2597b38ff370SJamie Gritton return (error); 2598b38ff370SJamie Gritton } 2599b38ff370SJamie Gritton 2600b38ff370SJamie Gritton /* 2601b38ff370SJamie Gritton * struct jail_remove_args { 2602b38ff370SJamie Gritton * int jid; 2603b38ff370SJamie Gritton * }; 2604b38ff370SJamie Gritton */ 2605b38ff370SJamie Gritton int 26068451d0ddSKip Macy sys_jail_remove(struct thread *td, struct jail_remove_args *uap) 2607b38ff370SJamie Gritton { 2608c861373bSJamie Gritton struct prison *pr; 2609c861373bSJamie Gritton int error; 2610b38ff370SJamie Gritton 2611b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_REMOVE); 2612b38ff370SJamie Gritton if (error) 2613b38ff370SJamie Gritton return (error); 2614b38ff370SJamie Gritton 2615b38ff370SJamie Gritton sx_xlock(&allprison_lock); 26160304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2617b38ff370SJamie Gritton if (pr == NULL) { 2618b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2619b38ff370SJamie Gritton return (EINVAL); 2620b38ff370SJamie Gritton } 2621c861373bSJamie Gritton if (!prison_isalive(pr)) { 2622c861373bSJamie Gritton /* Silently ignore already-dying prisons. */ 26230304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 2624c861373bSJamie Gritton sx_xunlock(&allprison_lock); 26250304c731SJamie Gritton return (0); 26260304c731SJamie Gritton } 2627c861373bSJamie Gritton prison_deref(pr, PD_KILL | PD_LOCKED | PD_LIST_XLOCKED); 2628c861373bSJamie Gritton return (0); 262975c13541SPoul-Henning Kamp } 263075c13541SPoul-Henning Kamp 2631fd7a8150SMike Barcroft /* 26329ddb7954SMike Barcroft * struct jail_attach_args { 26339ddb7954SMike Barcroft * int jid; 26349ddb7954SMike Barcroft * }; 2635fd7a8150SMike Barcroft */ 2636fd7a8150SMike Barcroft int 26378451d0ddSKip Macy sys_jail_attach(struct thread *td, struct jail_attach_args *uap) 2638fd7a8150SMike Barcroft { 2639b38ff370SJamie Gritton struct prison *pr; 2640b38ff370SJamie Gritton int error; 2641b38ff370SJamie Gritton 2642b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 2643b38ff370SJamie Gritton if (error) 2644b38ff370SJamie Gritton return (error); 2645b38ff370SJamie Gritton 2646f7496dcaSJamie Gritton sx_slock(&allprison_lock); 26470304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2648b38ff370SJamie Gritton if (pr == NULL) { 2649b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2650b38ff370SJamie Gritton return (EINVAL); 2651b38ff370SJamie Gritton } 2652b38ff370SJamie Gritton 265376ad42abSJamie Gritton /* Do not allow a process to attach to a prison that is not alive. */ 265476ad42abSJamie Gritton if (!prison_isalive(pr)) { 2655b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2656b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2657b38ff370SJamie Gritton return (EINVAL); 2658b38ff370SJamie Gritton } 2659b38ff370SJamie Gritton 2660f7496dcaSJamie Gritton return (do_jail_attach(td, pr, PD_LOCKED | PD_LIST_SLOCKED)); 2661b38ff370SJamie Gritton } 2662b38ff370SJamie Gritton 2663b38ff370SJamie Gritton static int 2664f7496dcaSJamie Gritton do_jail_attach(struct thread *td, struct prison *pr, int drflags) 2665b38ff370SJamie Gritton { 2666fd7a8150SMike Barcroft struct proc *p; 2667fd7a8150SMike Barcroft struct ucred *newcred, *oldcred; 26685050aa86SKonstantin Belousov int error; 2669fd7a8150SMike Barcroft 2670f7496dcaSJamie Gritton mtx_assert(&pr->pr_mtx, MA_OWNED); 2671f7496dcaSJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 2672589e4c1dSJamie Gritton drflags &= PD_LOCK_FLAGS; 267357f22bd4SJacques Vidrine /* 267457f22bd4SJacques Vidrine * XXX: Note that there is a slight race here if two threads 267557f22bd4SJacques Vidrine * in the same privileged process attempt to attach to two 267657f22bd4SJacques Vidrine * different jails at the same time. It is important for 267757f22bd4SJacques Vidrine * user processes not to do this, or they might end up with 267857f22bd4SJacques Vidrine * a process root from one prison, but attached to the jail 267957f22bd4SJacques Vidrine * of another. 268057f22bd4SJacques Vidrine */ 26811158508aSJamie Gritton prison_hold(pr); 26826754ae25SJamie Gritton refcount_acquire(&pr->pr_uref); 2683f7496dcaSJamie Gritton drflags |= PD_DEREF | PD_DEUREF; 2684fd7a8150SMike Barcroft mtx_unlock(&pr->pr_mtx); 2685f7496dcaSJamie Gritton drflags &= ~PD_LOCKED; 2686b38ff370SJamie Gritton 2687b38ff370SJamie Gritton /* Let modules do whatever they need to prepare for attaching. */ 2688b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_ATTACH, td); 2689b38ff370SJamie Gritton if (error) { 2690f7496dcaSJamie Gritton prison_deref(pr, drflags); 2691b38ff370SJamie Gritton return (error); 2692b38ff370SJamie Gritton } 2693f7496dcaSJamie Gritton sx_unlock(&allprison_lock); 2694f7496dcaSJamie Gritton drflags &= ~(PD_LIST_SLOCKED | PD_LIST_XLOCKED); 2695fd7a8150SMike Barcroft 2696413628a7SBjoern A. Zeeb /* 2697413628a7SBjoern A. Zeeb * Reparent the newly attached process to this jail. 2698413628a7SBjoern A. Zeeb */ 2699b38ff370SJamie Gritton p = td->td_proc; 2700413628a7SBjoern A. Zeeb error = cpuset_setproc_update_set(p, pr->pr_cpuset); 2701413628a7SBjoern A. Zeeb if (error) 2702b38ff370SJamie Gritton goto e_revert_osd; 2703413628a7SBjoern A. Zeeb 2704cb05b60aSAttilio Rao vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY); 2705fd7a8150SMike Barcroft if ((error = change_dir(pr->pr_root, td)) != 0) 2706fd7a8150SMike Barcroft goto e_unlock; 2707fd7a8150SMike Barcroft #ifdef MAC 270830d239bcSRobert Watson if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root))) 2709fd7a8150SMike Barcroft goto e_unlock; 2710fd7a8150SMike Barcroft #endif 2711b249ce48SMateusz Guzik VOP_UNLOCK(pr->pr_root); 2712d4380c0cSJamie Gritton if ((error = pwd_chroot_chdir(td, pr->pr_root))) 27135050aa86SKonstantin Belousov goto e_revert_osd; 2714fd7a8150SMike Barcroft 2715fd7a8150SMike Barcroft newcred = crget(); 2716fd7a8150SMike Barcroft PROC_LOCK(p); 27171fb6767dSJamie Gritton oldcred = crcopysafe(p, newcred); 271869c4ee54SJohn Baldwin newcred->cr_prison = pr; 2719daf63fd2SMateusz Guzik proc_set_cred(p, newcred); 27201fb6767dSJamie Gritton setsugid(p); 2721097055e2SEdward Tomasz Napierala #ifdef RACCT 2722097055e2SEdward Tomasz Napierala racct_proc_ucred_changed(p, oldcred, newcred); 2723f87beb93SAndriy Gapon crhold(newcred); 2724f87beb93SAndriy Gapon #endif 2725f87beb93SAndriy Gapon PROC_UNLOCK(p); 2726f87beb93SAndriy Gapon #ifdef RCTL 2727f87beb93SAndriy Gapon rctl_proc_ucred_changed(p, newcred); 2728f87beb93SAndriy Gapon crfree(newcred); 2729097055e2SEdward Tomasz Napierala #endif 27305ecb5444SMateusz Guzik prison_proc_relink(oldcred->cr_prison, pr, p); 2731f7496dcaSJamie Gritton prison_deref(oldcred->cr_prison, drflags); 2732fd7a8150SMike Barcroft crfree(oldcred); 2733cc7b7306SJamie Gritton 2734cc7b7306SJamie Gritton /* 2735cc7b7306SJamie Gritton * If the prison was killed while changing credentials, die along 2736cc7b7306SJamie Gritton * with it. 2737cc7b7306SJamie Gritton */ 2738cc7b7306SJamie Gritton if (!prison_isalive(pr)) { 2739cc7b7306SJamie Gritton PROC_LOCK(p); 2740cc7b7306SJamie Gritton kern_psignal(p, SIGKILL); 2741cc7b7306SJamie Gritton PROC_UNLOCK(p); 2742cc7b7306SJamie Gritton } 2743cc7b7306SJamie Gritton 2744fd7a8150SMike Barcroft return (0); 27451fb6767dSJamie Gritton 2746fd7a8150SMike Barcroft e_unlock: 2747b249ce48SMateusz Guzik VOP_UNLOCK(pr->pr_root); 2748b38ff370SJamie Gritton e_revert_osd: 2749b38ff370SJamie Gritton /* Tell modules this thread is still in its old jail after all. */ 27507f4e7248SJamie Gritton sx_slock(&allprison_lock); 2751f7496dcaSJamie Gritton drflags |= PD_LIST_SLOCKED; 27521fb6767dSJamie Gritton (void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td); 2753f7496dcaSJamie Gritton prison_deref(pr, drflags); 2754fd7a8150SMike Barcroft return (error); 2755fd7a8150SMike Barcroft } 2756fd7a8150SMike Barcroft 2757fd7a8150SMike Barcroft /* 2758fd7a8150SMike Barcroft * Returns a locked prison instance, or NULL on failure. 2759fd7a8150SMike Barcroft */ 276054b369c1SPawel Jakub Dawidek struct prison * 2761fd7a8150SMike Barcroft prison_find(int prid) 2762fd7a8150SMike Barcroft { 2763fd7a8150SMike Barcroft struct prison *pr; 2764fd7a8150SMike Barcroft 2765dc68a633SPawel Jakub Dawidek sx_assert(&allprison_lock, SX_LOCKED); 2766b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 2767f7496dcaSJamie Gritton if (pr->pr_id < prid) 2768f7496dcaSJamie Gritton continue; 27697de883c8SJamie Gritton if (pr->pr_id > prid) 27707de883c8SJamie Gritton break; 2771f7496dcaSJamie Gritton KASSERT(prison_isvalid(pr), ("Found invalid prison %p", pr)); 2772f7496dcaSJamie Gritton mtx_lock(&pr->pr_mtx); 2773f7496dcaSJamie Gritton return (pr); 2774fd7a8150SMike Barcroft } 2775fd7a8150SMike Barcroft return (NULL); 2776fd7a8150SMike Barcroft } 2777fd7a8150SMike Barcroft 2778b38ff370SJamie Gritton /* 27790304c731SJamie Gritton * Find a prison that is a descendant of mypr. Returns a locked prison or NULL. 2780b38ff370SJamie Gritton */ 2781b38ff370SJamie Gritton struct prison * 27820304c731SJamie Gritton prison_find_child(struct prison *mypr, int prid) 2783b38ff370SJamie Gritton { 27840304c731SJamie Gritton struct prison *pr; 27850304c731SJamie Gritton int descend; 2786b38ff370SJamie Gritton 2787b38ff370SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 27880304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 27890304c731SJamie Gritton if (pr->pr_id == prid) { 2790f7496dcaSJamie Gritton KASSERT(prison_isvalid(pr), 2791f7496dcaSJamie Gritton ("Found invalid prison %p", pr)); 27920304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 27930304c731SJamie Gritton return (pr); 27940304c731SJamie Gritton } 27950304c731SJamie Gritton } 27960304c731SJamie Gritton return (NULL); 27970304c731SJamie Gritton } 27980304c731SJamie Gritton 27990304c731SJamie Gritton /* 28000304c731SJamie Gritton * Look for the name relative to mypr. Returns a locked prison or NULL. 28010304c731SJamie Gritton */ 28020304c731SJamie Gritton struct prison * 28030304c731SJamie Gritton prison_find_name(struct prison *mypr, const char *name) 28040304c731SJamie Gritton { 28050304c731SJamie Gritton struct prison *pr, *deadpr; 28060304c731SJamie Gritton size_t mylen; 28070304c731SJamie Gritton int descend; 28080304c731SJamie Gritton 28090304c731SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 28100304c731SJamie Gritton mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1; 2811b38ff370SJamie Gritton deadpr = NULL; 28120304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 28130304c731SJamie Gritton if (!strcmp(pr->pr_name + mylen, name)) { 2814f7496dcaSJamie Gritton KASSERT(prison_isvalid(pr), 2815f7496dcaSJamie Gritton ("Found invalid prison %p", pr)); 2816f7496dcaSJamie Gritton if (prison_isalive(pr)) { 2817b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 2818b38ff370SJamie Gritton return (pr); 2819f7496dcaSJamie Gritton } 2820b38ff370SJamie Gritton deadpr = pr; 2821b38ff370SJamie Gritton } 2822b38ff370SJamie Gritton } 28230304c731SJamie Gritton /* There was no valid prison - perhaps there was a dying one. */ 2824f7496dcaSJamie Gritton if (deadpr != NULL) 2825b38ff370SJamie Gritton mtx_lock(&deadpr->pr_mtx); 2826b38ff370SJamie Gritton return (deadpr); 2827b38ff370SJamie Gritton } 2828b38ff370SJamie Gritton 2829b38ff370SJamie Gritton /* 28300fe74ae6SJamie Gritton * See if a prison has the specific flag set. The prison should be locked, 28310fe74ae6SJamie Gritton * unless checking for flags that are only set at jail creation (such as 28320fe74ae6SJamie Gritton * PR_IP4 and PR_IP6), or only the single bit is examined, without regard 28330fe74ae6SJamie Gritton * to any other prison data. 28340304c731SJamie Gritton */ 28350b0ae2e4SMina Galić bool 28360304c731SJamie Gritton prison_flag(struct ucred *cred, unsigned flag) 28370304c731SJamie Gritton { 28380304c731SJamie Gritton 28390b0ae2e4SMina Galić return ((cred->cr_prison->pr_flags & flag) != 0); 28400304c731SJamie Gritton } 28410304c731SJamie Gritton 28420b0ae2e4SMina Galić /* 28430b0ae2e4SMina Galić * See if a prison has the specific allow flag set. 28440b0ae2e4SMina Galić * The prison *should* be locked, or only a single bit is examined, without 28450b0ae2e4SMina Galić * regard to any other prison data. 28460b0ae2e4SMina Galić */ 28470b0ae2e4SMina Galić bool 28480304c731SJamie Gritton prison_allow(struct ucred *cred, unsigned flag) 28490304c731SJamie Gritton { 28500304c731SJamie Gritton 28510fe74ae6SJamie Gritton return ((cred->cr_prison->pr_allow & flag) != 0); 28520304c731SJamie Gritton } 28530304c731SJamie Gritton 28540304c731SJamie Gritton /* 2855effad35eSJamie Gritton * Hold a prison reference, by incrementing pr_ref. It is generally 2856effad35eSJamie Gritton * an error to hold a prison that does not already have a reference. 2857effad35eSJamie Gritton * A prison record will remain valid as long as it has at least one 2858effad35eSJamie Gritton * reference, and will not be removed as long as either the prison 2859effad35eSJamie Gritton * mutex or the allprison lock is held (allprison_lock may be shared). 2860effad35eSJamie Gritton */ 2861effad35eSJamie Gritton void 2862effad35eSJamie Gritton prison_hold_locked(struct prison *pr) 2863effad35eSJamie Gritton { 2864effad35eSJamie Gritton 28656754ae25SJamie Gritton /* Locking is no longer required. */ 28666754ae25SJamie Gritton prison_hold(pr); 2867effad35eSJamie Gritton } 2868effad35eSJamie Gritton 2869effad35eSJamie Gritton void 2870effad35eSJamie Gritton prison_hold(struct prison *pr) 2871effad35eSJamie Gritton { 28726754ae25SJamie Gritton #ifdef INVARIANTS 28736754ae25SJamie Gritton int was_valid = refcount_acquire_if_not_zero(&pr->pr_ref); 2874effad35eSJamie Gritton 28756754ae25SJamie Gritton KASSERT(was_valid, 28766754ae25SJamie Gritton ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id)); 28776754ae25SJamie Gritton #else 28786754ae25SJamie Gritton refcount_acquire(&pr->pr_ref); 28796754ae25SJamie Gritton #endif 2880effad35eSJamie Gritton } 2881effad35eSJamie Gritton 2882effad35eSJamie Gritton /* 2883effad35eSJamie Gritton * Remove a prison reference. If that was the last reference, the 2884f7496dcaSJamie Gritton * prison will be removed (at a later time). 2885b38ff370SJamie Gritton */ 288691421ba2SRobert Watson void 28871ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr) 288891421ba2SRobert Watson { 288991421ba2SRobert Watson 28901ba4a712SPawel Jakub Dawidek mtx_assert(&pr->pr_mtx, MA_OWNED); 2891effad35eSJamie Gritton /* 2892f7496dcaSJamie Gritton * Locking is no longer required, but unlock because the caller 2893f7496dcaSJamie Gritton * expects it. 2894effad35eSJamie Gritton */ 2895f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 2896f7496dcaSJamie Gritton prison_free(pr); 2897effad35eSJamie Gritton } 2898c2cda609SPawel Jakub Dawidek 28991ba4a712SPawel Jakub Dawidek void 29001ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr) 29011ba4a712SPawel Jakub Dawidek { 29021ba4a712SPawel Jakub Dawidek 29036754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 29046754ae25SJamie Gritton ("Trying to free dead prison %p (jid=%d).", 29056754ae25SJamie Gritton pr, pr->pr_id)); 2906f7496dcaSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_ref)) { 2907f7496dcaSJamie Gritton /* 2908f7496dcaSJamie Gritton * Don't remove the last reference in this context, 2909f7496dcaSJamie Gritton * in case there are locks held. 2910f7496dcaSJamie Gritton */ 2911*8cf955f3SMark Johnston taskqueue_enqueue(taskqueue_jail_remove, &pr->pr_task); 2912f7496dcaSJamie Gritton } 2913f7496dcaSJamie Gritton } 2914f7496dcaSJamie Gritton 2915f7496dcaSJamie Gritton static void 2916f7496dcaSJamie Gritton prison_free_not_last(struct prison *pr) 2917f7496dcaSJamie Gritton { 2918f7496dcaSJamie Gritton #ifdef INVARIANTS 2919f7496dcaSJamie Gritton int lastref; 2920f7496dcaSJamie Gritton 2921f7496dcaSJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 2922f7496dcaSJamie Gritton ("Trying to free dead prison %p (jid=%d).", 2923f7496dcaSJamie Gritton pr, pr->pr_id)); 2924f7496dcaSJamie Gritton lastref = refcount_release(&pr->pr_ref); 2925f7496dcaSJamie Gritton KASSERT(!lastref, 2926f7496dcaSJamie Gritton ("prison_free_not_last freed last ref on prison %p (jid=%d).", 2927f7496dcaSJamie Gritton pr, pr->pr_id)); 2928f7496dcaSJamie Gritton #else 2929ee9b37aeSMateusz Guzik refcount_release(&pr->pr_ref); 2930f7496dcaSJamie Gritton #endif 29311ba4a712SPawel Jakub Dawidek } 29321ba4a712SPawel Jakub Dawidek 293373d9e52dSJamie Gritton /* 2934f171938cSGordon Bergling * Hold a prison for user visibility, by incrementing pr_uref. 2935effad35eSJamie Gritton * It is generally an error to hold a prison that isn't already 293649a033d8SGordon Bergling * user-visible, except through the jail system calls. It is also 2937effad35eSJamie Gritton * an error to hold an invalid prison. A prison record will remain 2938effad35eSJamie Gritton * alive as long as it has at least one user reference, and will not 2939f7496dcaSJamie Gritton * be set to the dying state until the prison mutex and allprison_lock 2940f7496dcaSJamie Gritton * are both freed. 2941effad35eSJamie Gritton */ 2942effad35eSJamie Gritton void 2943effad35eSJamie Gritton prison_proc_hold(struct prison *pr) 2944effad35eSJamie Gritton { 29456754ae25SJamie Gritton #ifdef INVARIANTS 29466754ae25SJamie Gritton int was_alive = refcount_acquire_if_not_zero(&pr->pr_uref); 2947effad35eSJamie Gritton 29486754ae25SJamie Gritton KASSERT(was_alive, 2949effad35eSJamie Gritton ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id)); 29506754ae25SJamie Gritton #else 29516754ae25SJamie Gritton refcount_acquire(&pr->pr_uref); 29526754ae25SJamie Gritton #endif 2953effad35eSJamie Gritton } 2954effad35eSJamie Gritton 2955effad35eSJamie Gritton /* 2956effad35eSJamie Gritton * Remove a prison user reference. If it was the last reference, the 2957effad35eSJamie Gritton * prison will be considered "dying", and may be removed once all of 2958effad35eSJamie Gritton * its references are dropped. 2959effad35eSJamie Gritton */ 2960effad35eSJamie Gritton void 2961effad35eSJamie Gritton prison_proc_free(struct prison *pr) 2962effad35eSJamie Gritton { 2963effad35eSJamie Gritton 29646754ae25SJamie Gritton /* 29656754ae25SJamie Gritton * Locking is only required when releasing the last reference. 29666754ae25SJamie Gritton * This allows assurance that a locked prison will remain alive 29676754ae25SJamie Gritton * until it is unlocked. 29686754ae25SJamie Gritton */ 29696754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_uref) > 0, 2970effad35eSJamie Gritton ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id)); 2971195cd6aeSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_uref)) { 2972effad35eSJamie Gritton /* 2973effad35eSJamie Gritton * Don't remove the last user reference in this context, 2974effad35eSJamie Gritton * which is expected to be a process that is not only locked, 2975effad35eSJamie Gritton * but also half dead. Add a reference so any calls to 2976effad35eSJamie Gritton * prison_free() won't re-submit the task. 2977effad35eSJamie Gritton */ 2978f7496dcaSJamie Gritton prison_hold(pr); 29791158508aSJamie Gritton mtx_lock(&pr->pr_mtx); 29801158508aSJamie Gritton KASSERT(!(pr->pr_flags & PR_COMPLETE_PROC), 29811158508aSJamie Gritton ("Redundant last reference in prison_proc_free (jid=%d)", 29821158508aSJamie Gritton pr->pr_id)); 29831158508aSJamie Gritton pr->pr_flags |= PR_COMPLETE_PROC; 29841158508aSJamie Gritton mtx_unlock(&pr->pr_mtx); 2985*8cf955f3SMark Johnston taskqueue_enqueue(taskqueue_jail_remove, &pr->pr_task); 2986effad35eSJamie Gritton } 2987effad35eSJamie Gritton } 2988effad35eSJamie Gritton 2989c861373bSJamie Gritton static void 2990c861373bSJamie Gritton prison_proc_free_not_last(struct prison *pr) 2991c861373bSJamie Gritton { 2992c861373bSJamie Gritton #ifdef INVARIANTS 2993c861373bSJamie Gritton int lastref; 2994c861373bSJamie Gritton 2995c861373bSJamie Gritton KASSERT(refcount_load(&pr->pr_uref) > 0, 2996c861373bSJamie Gritton ("Trying to free dead prison %p (jid=%d).", 2997c861373bSJamie Gritton pr, pr->pr_id)); 2998c861373bSJamie Gritton lastref = refcount_release(&pr->pr_uref); 2999c861373bSJamie Gritton KASSERT(!lastref, 3000c861373bSJamie Gritton ("prison_proc_free_not_last freed last uref on prison %p (jid=%d).", 3001c861373bSJamie Gritton pr, pr->pr_id)); 3002c861373bSJamie Gritton #else 3003c861373bSJamie Gritton refcount_release(&pr->pr_uref); 3004c861373bSJamie Gritton #endif 3005c861373bSJamie Gritton } 3006c861373bSJamie Gritton 30075ecb5444SMateusz Guzik void 30085ecb5444SMateusz Guzik prison_proc_link(struct prison *pr, struct proc *p) 30095ecb5444SMateusz Guzik { 30105ecb5444SMateusz Guzik 30115ecb5444SMateusz Guzik sx_assert(&allproc_lock, SA_XLOCKED); 30125ecb5444SMateusz Guzik LIST_INSERT_HEAD(&pr->pr_proclist, p, p_jaillist); 30135ecb5444SMateusz Guzik } 30145ecb5444SMateusz Guzik 30155ecb5444SMateusz Guzik void 30165ecb5444SMateusz Guzik prison_proc_unlink(struct prison *pr, struct proc *p) 30175ecb5444SMateusz Guzik { 30185ecb5444SMateusz Guzik 30195ecb5444SMateusz Guzik sx_assert(&allproc_lock, SA_XLOCKED); 30205ecb5444SMateusz Guzik LIST_REMOVE(p, p_jaillist); 30215ecb5444SMateusz Guzik } 30225ecb5444SMateusz Guzik 30235ecb5444SMateusz Guzik static void 30245ecb5444SMateusz Guzik prison_proc_relink(struct prison *opr, struct prison *npr, struct proc *p) 30255ecb5444SMateusz Guzik { 30265ecb5444SMateusz Guzik 30275ecb5444SMateusz Guzik sx_xlock(&allproc_lock); 30285ecb5444SMateusz Guzik prison_proc_unlink(opr, p); 30295ecb5444SMateusz Guzik prison_proc_link(npr, p); 30305ecb5444SMateusz Guzik sx_xunlock(&allproc_lock); 30315ecb5444SMateusz Guzik } 30325ecb5444SMateusz Guzik 3033effad35eSJamie Gritton /* 303473d9e52dSJamie Gritton * Complete a call to either prison_free or prison_proc_free. 303573d9e52dSJamie Gritton */ 3036c2cda609SPawel Jakub Dawidek static void 3037c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending) 3038c2cda609SPawel Jakub Dawidek { 303973d9e52dSJamie Gritton struct prison *pr = context; 3040f7496dcaSJamie Gritton int drflags; 3041b38ff370SJamie Gritton 3042effad35eSJamie Gritton /* 30431158508aSJamie Gritton * This could be called to release the last reference, or the last 30441158508aSJamie Gritton * user reference (plus the reference held in prison_proc_free). 3045effad35eSJamie Gritton */ 3046f7496dcaSJamie Gritton drflags = prison_lock_xlock(pr, PD_DEREF); 30471158508aSJamie Gritton if (pr->pr_flags & PR_COMPLETE_PROC) { 30481158508aSJamie Gritton pr->pr_flags &= ~PR_COMPLETE_PROC; 3049f7496dcaSJamie Gritton drflags |= PD_DEUREF; 30501158508aSJamie Gritton } 3051f7496dcaSJamie Gritton prison_deref(pr, drflags); 3052b38ff370SJamie Gritton } 3053b38ff370SJamie Gritton 30545ecb5444SMateusz Guzik static void 30555ecb5444SMateusz Guzik prison_kill_processes_cb(struct proc *p, void *arg __unused) 30565ecb5444SMateusz Guzik { 30575ecb5444SMateusz Guzik 30585ecb5444SMateusz Guzik kern_psignal(p, SIGKILL); 30595ecb5444SMateusz Guzik } 30605ecb5444SMateusz Guzik 30615ecb5444SMateusz Guzik /* 30625ecb5444SMateusz Guzik * Note the iteration does not guarantee acting on all processes. 30635ecb5444SMateusz Guzik * Most notably there may be fork or jail_attach in progress. 30645ecb5444SMateusz Guzik */ 30655ecb5444SMateusz Guzik void 30665ecb5444SMateusz Guzik prison_proc_iterate(struct prison *pr, void (*cb)(struct proc *, void *), 30675ecb5444SMateusz Guzik void *cbarg) 30685ecb5444SMateusz Guzik { 30695ecb5444SMateusz Guzik struct prison *ppr; 30705ecb5444SMateusz Guzik struct proc *p; 30715ecb5444SMateusz Guzik 30725ecb5444SMateusz Guzik if (atomic_load_int(&pr->pr_childcount) == 0) { 30735ecb5444SMateusz Guzik sx_slock(&allproc_lock); 30745ecb5444SMateusz Guzik LIST_FOREACH(p, &pr->pr_proclist, p_jaillist) { 30755ecb5444SMateusz Guzik if (p->p_state == PRS_NEW) 30765ecb5444SMateusz Guzik continue; 30775ecb5444SMateusz Guzik PROC_LOCK(p); 30785ecb5444SMateusz Guzik cb(p, cbarg); 30795ecb5444SMateusz Guzik PROC_UNLOCK(p); 30805ecb5444SMateusz Guzik } 30815ecb5444SMateusz Guzik sx_sunlock(&allproc_lock); 30825ecb5444SMateusz Guzik if (atomic_load_int(&pr->pr_childcount) == 0) 30835ecb5444SMateusz Guzik return; 30845ecb5444SMateusz Guzik /* 30855ecb5444SMateusz Guzik * Some jails popped up during the iteration, fall through to a 30865ecb5444SMateusz Guzik * system-wide search. 30875ecb5444SMateusz Guzik */ 30885ecb5444SMateusz Guzik } 30895ecb5444SMateusz Guzik 30905ecb5444SMateusz Guzik sx_slock(&allproc_lock); 30915ecb5444SMateusz Guzik FOREACH_PROC_IN_SYSTEM(p) { 30925ecb5444SMateusz Guzik PROC_LOCK(p); 30935ecb5444SMateusz Guzik if (p->p_state != PRS_NEW && p->p_ucred != NULL) { 3094831531a8SKonstantin Belousov for (ppr = p->p_ucred->cr_prison; ppr != NULL; 30955ecb5444SMateusz Guzik ppr = ppr->pr_parent) { 30965ecb5444SMateusz Guzik if (ppr == pr) { 30975ecb5444SMateusz Guzik cb(p, cbarg); 30985ecb5444SMateusz Guzik break; 30995ecb5444SMateusz Guzik } 31005ecb5444SMateusz Guzik } 31015ecb5444SMateusz Guzik } 31025ecb5444SMateusz Guzik PROC_UNLOCK(p); 31035ecb5444SMateusz Guzik } 31045ecb5444SMateusz Guzik sx_sunlock(&allproc_lock); 31055ecb5444SMateusz Guzik } 31065ecb5444SMateusz Guzik 3107b38ff370SJamie Gritton /* 3108effad35eSJamie Gritton * Remove a prison reference and/or user reference (usually). 3109effad35eSJamie Gritton * This assumes context that allows sleeping (for allprison_lock), 3110effad35eSJamie Gritton * with no non-sleeping locks held, except perhaps the prison itself. 3111effad35eSJamie Gritton * If there are no more references, release and delist the prison. 3112effad35eSJamie Gritton * On completion, the prison lock and the allprison lock are both 3113effad35eSJamie Gritton * unlocked. 3114b38ff370SJamie Gritton */ 3115b38ff370SJamie Gritton static void 3116b38ff370SJamie Gritton prison_deref(struct prison *pr, int flags) 3117b38ff370SJamie Gritton { 31186e1d1bfcSJamie Gritton struct prisonlist freeprison; 3119c861373bSJamie Gritton struct prison *killpr, *rpr, *ppr, *tpr; 3120c2cda609SPawel Jakub Dawidek 3121c861373bSJamie Gritton killpr = NULL; 31226e1d1bfcSJamie Gritton TAILQ_INIT(&freeprison); 31236e1d1bfcSJamie Gritton /* 31246e1d1bfcSJamie Gritton * Release this prison as requested, which may cause its parent 31256e1d1bfcSJamie Gritton * to be released, and then maybe its grandparent, etc. 31266e1d1bfcSJamie Gritton */ 31270304c731SJamie Gritton for (;;) { 3128c861373bSJamie Gritton if (flags & PD_KILL) { 3129c861373bSJamie Gritton /* Kill the prison and its descendents. */ 3130589e4c1dSJamie Gritton KASSERT(pr != &prison0, 3131589e4c1dSJamie Gritton ("prison_deref trying to kill prison0")); 3132c861373bSJamie Gritton if (!(flags & PD_DEREF)) { 3133c861373bSJamie Gritton prison_hold(pr); 3134c861373bSJamie Gritton flags |= PD_DEREF; 3135c861373bSJamie Gritton } 3136c861373bSJamie Gritton flags = prison_lock_xlock(pr, flags); 3137c861373bSJamie Gritton prison_deref_kill(pr, &freeprison); 3138c861373bSJamie Gritton } 3139e6d5cb63SJamie Gritton if (flags & PD_DEUREF) { 3140f7496dcaSJamie Gritton /* Drop a user reference. */ 31416754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_uref) > 0, 314273d9e52dSJamie Gritton ("prison_deref PD_DEUREF on a dead prison (jid=%d)", 314373d9e52dSJamie Gritton pr->pr_id)); 3144f7496dcaSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_uref)) { 3145f7496dcaSJamie Gritton if (!(flags & PD_DEREF)) { 3146f7496dcaSJamie Gritton prison_hold(pr); 3147f7496dcaSJamie Gritton flags |= PD_DEREF; 3148f7496dcaSJamie Gritton } 3149f7496dcaSJamie Gritton flags = prison_lock_xlock(pr, flags); 31501158508aSJamie Gritton if (refcount_release(&pr->pr_uref) && 31511158508aSJamie Gritton pr->pr_state == PRISON_STATE_ALIVE) { 3152f7496dcaSJamie Gritton /* 3153f7496dcaSJamie Gritton * When the last user references goes, 3154f7496dcaSJamie Gritton * this becomes a dying prison. 3155f7496dcaSJamie Gritton */ 3156f7496dcaSJamie Gritton KASSERT( 3157f7496dcaSJamie Gritton refcount_load(&prison0.pr_uref) > 0, 31586754ae25SJamie Gritton ("prison0 pr_uref=0")); 31591158508aSJamie Gritton pr->pr_state = PRISON_STATE_DYING; 3160f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 3161f7496dcaSJamie Gritton flags &= ~PD_LOCKED; 3162a9f7455cSJamie Gritton prison_cleanup(pr); 3163f7496dcaSJamie Gritton } 3164f7496dcaSJamie Gritton } 3165f7496dcaSJamie Gritton } 3166c861373bSJamie Gritton if (flags & PD_KILL) { 3167c861373bSJamie Gritton /* 3168c861373bSJamie Gritton * Any remaining user references are probably processes 3169c861373bSJamie Gritton * that need to be killed, either in this prison or its 3170c861373bSJamie Gritton * descendants. 3171c861373bSJamie Gritton */ 3172c861373bSJamie Gritton if (refcount_load(&pr->pr_uref) > 0) 3173c861373bSJamie Gritton killpr = pr; 3174589e4c1dSJamie Gritton /* Make sure the parent prison doesn't get killed. */ 3175589e4c1dSJamie Gritton flags &= ~PD_KILL; 3176c861373bSJamie Gritton } 317773d9e52dSJamie Gritton if (flags & PD_DEREF) { 3178f7496dcaSJamie Gritton /* Drop a reference. */ 31796754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 318073d9e52dSJamie Gritton ("prison_deref PD_DEREF on a dead prison (jid=%d)", 318173d9e52dSJamie Gritton pr->pr_id)); 3182f7496dcaSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_ref)) { 3183f7496dcaSJamie Gritton flags = prison_lock_xlock(pr, flags); 3184f7496dcaSJamie Gritton if (refcount_release(&pr->pr_ref)) { 3185cc5fd8c7SJamie Gritton /* 3186f7496dcaSJamie Gritton * When the last reference goes, 3187f7496dcaSJamie Gritton * unlink the prison and set it aside. 3188cc5fd8c7SJamie Gritton */ 3189f7496dcaSJamie Gritton KASSERT( 3190f7496dcaSJamie Gritton refcount_load(&pr->pr_uref) == 0, 3191f7496dcaSJamie Gritton ("prison_deref: last ref, " 3192f7496dcaSJamie Gritton "but still has %d urefs (jid=%d)", 3193f7496dcaSJamie Gritton pr->pr_uref, pr->pr_id)); 3194f7496dcaSJamie Gritton KASSERT( 3195f7496dcaSJamie Gritton refcount_load(&prison0.pr_ref) != 0, 3196f7496dcaSJamie Gritton ("prison0 pr_ref=0")); 31971158508aSJamie Gritton pr->pr_state = PRISON_STATE_INVALID; 3198b38ff370SJamie Gritton TAILQ_REMOVE(&allprison, pr, pr_list); 31990304c731SJamie Gritton LIST_REMOVE(pr, pr_sibling); 3200f7496dcaSJamie Gritton TAILQ_INSERT_TAIL(&freeprison, pr, 3201f7496dcaSJamie Gritton pr_list); 3202f7496dcaSJamie Gritton for (ppr = pr->pr_parent; 3203f7496dcaSJamie Gritton ppr != NULL; 3204f7496dcaSJamie Gritton ppr = ppr->pr_parent) 3205f7496dcaSJamie Gritton ppr->pr_childcount--; 3206f7496dcaSJamie Gritton /* 3207f7496dcaSJamie Gritton * Removing a prison frees references 3208f7496dcaSJamie Gritton * from its parent. 3209f7496dcaSJamie Gritton */ 3210f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 3211f7496dcaSJamie Gritton flags &= ~PD_LOCKED; 32126e1d1bfcSJamie Gritton pr = pr->pr_parent; 32136e1d1bfcSJamie Gritton flags |= PD_DEREF | PD_DEUREF; 3214f7496dcaSJamie Gritton continue; 3215f7496dcaSJamie Gritton } 3216f7496dcaSJamie Gritton } 3217f7496dcaSJamie Gritton } 3218f7496dcaSJamie Gritton break; 32196e1d1bfcSJamie Gritton } 32206e1d1bfcSJamie Gritton 32216e1d1bfcSJamie Gritton /* Release all the prison locks. */ 3222f7496dcaSJamie Gritton if (flags & PD_LOCKED) 3223f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 32246e1d1bfcSJamie Gritton if (flags & PD_LIST_SLOCKED) 32256e1d1bfcSJamie Gritton sx_sunlock(&allprison_lock); 32266e1d1bfcSJamie Gritton else if (flags & PD_LIST_XLOCKED) 32276e1d1bfcSJamie Gritton sx_xunlock(&allprison_lock); 32286e1d1bfcSJamie Gritton 3229c861373bSJamie Gritton /* Kill any processes attached to a killed prison. */ 32305ecb5444SMateusz Guzik if (killpr != NULL) 32315ecb5444SMateusz Guzik prison_proc_iterate(killpr, prison_kill_processes_cb, NULL); 3232c861373bSJamie Gritton 32336e1d1bfcSJamie Gritton /* 32346e1d1bfcSJamie Gritton * Finish removing any unreferenced prisons, which couldn't happen 32356e1d1bfcSJamie Gritton * while allprison_lock was held (to avoid a LOR on vrele). 32366e1d1bfcSJamie Gritton */ 32376e1d1bfcSJamie Gritton TAILQ_FOREACH_SAFE(rpr, &freeprison, pr_list, tpr) { 32386e1d1bfcSJamie Gritton #ifdef VIMAGE 32396e1d1bfcSJamie Gritton if (rpr->pr_vnet != rpr->pr_parent->pr_vnet) 32406e1d1bfcSJamie Gritton vnet_destroy(rpr->pr_vnet); 32416e1d1bfcSJamie Gritton #endif 32426e1d1bfcSJamie Gritton if (rpr->pr_root != NULL) 32436e1d1bfcSJamie Gritton vrele(rpr->pr_root); 32446e1d1bfcSJamie Gritton mtx_destroy(&rpr->pr_mtx); 32456e1d1bfcSJamie Gritton #ifdef INET 3246eb8dcdeaSGleb Smirnoff prison_ip_free(rpr->pr_addrs[PR_INET]); 32476e1d1bfcSJamie Gritton #endif 32486e1d1bfcSJamie Gritton #ifdef INET6 3249eb8dcdeaSGleb Smirnoff prison_ip_free(rpr->pr_addrs[PR_INET6]); 32506e1d1bfcSJamie Gritton #endif 32516e1d1bfcSJamie Gritton if (rpr->pr_cpuset != NULL) 32526e1d1bfcSJamie Gritton cpuset_rel(rpr->pr_cpuset); 32536e1d1bfcSJamie Gritton osd_jail_exit(rpr); 32546e1d1bfcSJamie Gritton #ifdef RACCT 32556e1d1bfcSJamie Gritton if (racct_enable) 32566e1d1bfcSJamie Gritton prison_racct_detach(rpr); 32576e1d1bfcSJamie Gritton #endif 3258f7496dcaSJamie Gritton TAILQ_REMOVE(&freeprison, rpr, pr_list); 32596e1d1bfcSJamie Gritton free(rpr, M_PRISON); 32600304c731SJamie Gritton } 3261b3059e09SRobert Watson } 3262b3059e09SRobert Watson 3263413628a7SBjoern A. Zeeb /* 3264c861373bSJamie Gritton * Kill the prison and its descendants. Mark them as dying, clear the 3265c861373bSJamie Gritton * persist flag, and call module remove methods. 3266c861373bSJamie Gritton */ 3267c861373bSJamie Gritton static void 3268c861373bSJamie Gritton prison_deref_kill(struct prison *pr, struct prisonlist *freeprison) 3269c861373bSJamie Gritton { 3270c861373bSJamie Gritton struct prison *cpr, *ppr, *rpr; 3271c861373bSJamie Gritton bool descend; 3272c861373bSJamie Gritton 3273c861373bSJamie Gritton /* 3274c861373bSJamie Gritton * Unlike the descendants, the target prison can be killed 3275c861373bSJamie Gritton * even if it is currently dying. This is useful for failed 3276c861373bSJamie Gritton * creation in jail_set(2). 3277c861373bSJamie Gritton */ 3278c861373bSJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 3279c861373bSJamie Gritton ("Trying to kill dead prison %p (jid=%d).", 3280c861373bSJamie Gritton pr, pr->pr_id)); 3281c861373bSJamie Gritton refcount_acquire(&pr->pr_uref); 3282c861373bSJamie Gritton pr->pr_state = PRISON_STATE_DYING; 3283c861373bSJamie Gritton mtx_unlock(&pr->pr_mtx); 3284c861373bSJamie Gritton 3285c861373bSJamie Gritton rpr = NULL; 3286c861373bSJamie Gritton FOREACH_PRISON_DESCENDANT_PRE_POST(pr, cpr, descend) { 3287c861373bSJamie Gritton if (descend) { 3288c861373bSJamie Gritton if (!prison_isalive(cpr)) { 3289c861373bSJamie Gritton descend = false; 3290c861373bSJamie Gritton continue; 3291c861373bSJamie Gritton } 3292c861373bSJamie Gritton prison_hold(cpr); 3293c861373bSJamie Gritton prison_proc_hold(cpr); 3294c861373bSJamie Gritton mtx_lock(&cpr->pr_mtx); 3295c861373bSJamie Gritton cpr->pr_state = PRISON_STATE_DYING; 3296c861373bSJamie Gritton cpr->pr_flags |= PR_REMOVE; 3297c861373bSJamie Gritton mtx_unlock(&cpr->pr_mtx); 3298c861373bSJamie Gritton continue; 3299c861373bSJamie Gritton } 3300c861373bSJamie Gritton if (!(cpr->pr_flags & PR_REMOVE)) 3301c861373bSJamie Gritton continue; 3302a9f7455cSJamie Gritton prison_cleanup(cpr); 3303c861373bSJamie Gritton mtx_lock(&cpr->pr_mtx); 3304c861373bSJamie Gritton cpr->pr_flags &= ~PR_REMOVE; 3305c861373bSJamie Gritton if (cpr->pr_flags & PR_PERSIST) { 3306c861373bSJamie Gritton cpr->pr_flags &= ~PR_PERSIST; 3307c861373bSJamie Gritton prison_proc_free_not_last(cpr); 3308c861373bSJamie Gritton prison_free_not_last(cpr); 3309c861373bSJamie Gritton } 3310c861373bSJamie Gritton (void)refcount_release(&cpr->pr_uref); 3311c861373bSJamie Gritton if (refcount_release(&cpr->pr_ref)) { 3312c861373bSJamie Gritton /* 3313c861373bSJamie Gritton * When the last reference goes, unlink the prison 3314c861373bSJamie Gritton * and set it aside for prison_deref() to handle. 3315c861373bSJamie Gritton * Delay unlinking the sibling list to keep the loop 3316c861373bSJamie Gritton * safe. 3317c861373bSJamie Gritton */ 3318c861373bSJamie Gritton if (rpr != NULL) 3319c861373bSJamie Gritton LIST_REMOVE(rpr, pr_sibling); 3320c861373bSJamie Gritton rpr = cpr; 3321c861373bSJamie Gritton rpr->pr_state = PRISON_STATE_INVALID; 3322c861373bSJamie Gritton TAILQ_REMOVE(&allprison, rpr, pr_list); 3323c861373bSJamie Gritton TAILQ_INSERT_TAIL(freeprison, rpr, pr_list); 3324c861373bSJamie Gritton /* 3325c861373bSJamie Gritton * Removing a prison frees references from its parent. 3326c861373bSJamie Gritton */ 3327c861373bSJamie Gritton ppr = rpr->pr_parent; 3328c861373bSJamie Gritton prison_proc_free_not_last(ppr); 3329c861373bSJamie Gritton prison_free_not_last(ppr); 3330c861373bSJamie Gritton for (; ppr != NULL; ppr = ppr->pr_parent) 3331c861373bSJamie Gritton ppr->pr_childcount--; 3332c861373bSJamie Gritton } 3333c861373bSJamie Gritton mtx_unlock(&cpr->pr_mtx); 3334c861373bSJamie Gritton } 3335c861373bSJamie Gritton if (rpr != NULL) 3336c861373bSJamie Gritton LIST_REMOVE(rpr, pr_sibling); 3337c861373bSJamie Gritton 3338a9f7455cSJamie Gritton prison_cleanup(pr); 3339c861373bSJamie Gritton mtx_lock(&pr->pr_mtx); 3340c861373bSJamie Gritton if (pr->pr_flags & PR_PERSIST) { 3341c861373bSJamie Gritton pr->pr_flags &= ~PR_PERSIST; 3342c861373bSJamie Gritton prison_proc_free_not_last(pr); 3343c861373bSJamie Gritton prison_free_not_last(pr); 3344c861373bSJamie Gritton } 3345c861373bSJamie Gritton (void)refcount_release(&pr->pr_uref); 3346c861373bSJamie Gritton } 3347c861373bSJamie Gritton 3348c861373bSJamie Gritton /* 3349f7496dcaSJamie Gritton * Given the current locking state in the flags, make sure allprison_lock 3350f7496dcaSJamie Gritton * is held exclusive, and the prison is locked. Return flags indicating 3351f7496dcaSJamie Gritton * the new state. 3352f7496dcaSJamie Gritton */ 3353f7496dcaSJamie Gritton static int 3354f7496dcaSJamie Gritton prison_lock_xlock(struct prison *pr, int flags) 3355f7496dcaSJamie Gritton { 3356f7496dcaSJamie Gritton 3357f7496dcaSJamie Gritton if (!(flags & PD_LIST_XLOCKED)) { 3358f7496dcaSJamie Gritton /* 3359f7496dcaSJamie Gritton * Get allprison_lock, which may be an upgrade, 3360f7496dcaSJamie Gritton * and may require unlocking the prison. 3361f7496dcaSJamie Gritton */ 3362f7496dcaSJamie Gritton if (flags & PD_LOCKED) { 3363f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 3364f7496dcaSJamie Gritton flags &= ~PD_LOCKED; 3365f7496dcaSJamie Gritton } 3366f7496dcaSJamie Gritton if (flags & PD_LIST_SLOCKED) { 3367f7496dcaSJamie Gritton if (!sx_try_upgrade(&allprison_lock)) { 3368f7496dcaSJamie Gritton sx_sunlock(&allprison_lock); 3369f7496dcaSJamie Gritton sx_xlock(&allprison_lock); 3370f7496dcaSJamie Gritton } 3371f7496dcaSJamie Gritton flags &= ~PD_LIST_SLOCKED; 3372f7496dcaSJamie Gritton } else 3373f7496dcaSJamie Gritton sx_xlock(&allprison_lock); 3374f7496dcaSJamie Gritton flags |= PD_LIST_XLOCKED; 3375f7496dcaSJamie Gritton } 3376f7496dcaSJamie Gritton if (!(flags & PD_LOCKED)) { 3377f7496dcaSJamie Gritton /* Lock the prison mutex. */ 3378f7496dcaSJamie Gritton mtx_lock(&pr->pr_mtx); 3379f7496dcaSJamie Gritton flags |= PD_LOCKED; 3380f7496dcaSJamie Gritton } 3381f7496dcaSJamie Gritton return flags; 3382f7496dcaSJamie Gritton } 3383f7496dcaSJamie Gritton 3384f7496dcaSJamie Gritton /* 3385a9f7455cSJamie Gritton * Release a prison's resources when it starts dying (when the last user 3386a9f7455cSJamie Gritton * reference is dropped, or when it is killed). 3387a9f7455cSJamie Gritton */ 3388a9f7455cSJamie Gritton static void 3389a9f7455cSJamie Gritton prison_cleanup(struct prison *pr) 3390a9f7455cSJamie Gritton { 3391a9f7455cSJamie Gritton sx_assert(&allprison_lock, SA_XLOCKED); 3392a9f7455cSJamie Gritton mtx_assert(&pr->pr_mtx, MA_NOTOWNED); 339388175af8SRick Macklem vfs_exjail_delete(pr); 33947060da62SJamie Gritton shm_remove_prison(pr); 3395a9f7455cSJamie Gritton (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL); 3396a9f7455cSJamie Gritton } 3397a9f7455cSJamie Gritton 3398a9f7455cSJamie Gritton /* 33990fe74ae6SJamie Gritton * Set or clear a permission bit in the pr_allow field, passing restrictions 34000fe74ae6SJamie Gritton * (cleared permission) down to child jails. 34010fe74ae6SJamie Gritton */ 34020fe74ae6SJamie Gritton void 34030fe74ae6SJamie Gritton prison_set_allow(struct ucred *cred, unsigned flag, int enable) 34040fe74ae6SJamie Gritton { 34050fe74ae6SJamie Gritton struct prison *pr; 34060fe74ae6SJamie Gritton 34070fe74ae6SJamie Gritton pr = cred->cr_prison; 34080fe74ae6SJamie Gritton sx_slock(&allprison_lock); 34090fe74ae6SJamie Gritton mtx_lock(&pr->pr_mtx); 34100fe74ae6SJamie Gritton prison_set_allow_locked(pr, flag, enable); 34110fe74ae6SJamie Gritton mtx_unlock(&pr->pr_mtx); 34120fe74ae6SJamie Gritton sx_sunlock(&allprison_lock); 34130fe74ae6SJamie Gritton } 34140fe74ae6SJamie Gritton 34150fe74ae6SJamie Gritton static void 34160fe74ae6SJamie Gritton prison_set_allow_locked(struct prison *pr, unsigned flag, int enable) 34170fe74ae6SJamie Gritton { 34180fe74ae6SJamie Gritton struct prison *cpr; 34190fe74ae6SJamie Gritton int descend; 34200fe74ae6SJamie Gritton 34210fe74ae6SJamie Gritton if (enable != 0) 34220fe74ae6SJamie Gritton pr->pr_allow |= flag; 34230fe74ae6SJamie Gritton else { 34240fe74ae6SJamie Gritton pr->pr_allow &= ~flag; 34250fe74ae6SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, cpr, descend) 34260fe74ae6SJamie Gritton cpr->pr_allow &= ~flag; 34270fe74ae6SJamie Gritton } 34280fe74ae6SJamie Gritton } 34290fe74ae6SJamie Gritton 34300fe74ae6SJamie Gritton /* 3431ca04ba64SJamie Gritton * Check if a jail supports the given address family. 3432ca04ba64SJamie Gritton * 3433ca04ba64SJamie Gritton * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT 3434ca04ba64SJamie Gritton * if not. 3435ca04ba64SJamie Gritton */ 3436ca04ba64SJamie Gritton int 3437ca04ba64SJamie Gritton prison_check_af(struct ucred *cred, int af) 3438ca04ba64SJamie Gritton { 34390304c731SJamie Gritton struct prison *pr; 3440ca04ba64SJamie Gritton int error; 3441ca04ba64SJamie Gritton 3442ca04ba64SJamie Gritton KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3443ca04ba64SJamie Gritton 34440304c731SJamie Gritton pr = cred->cr_prison; 3445499650a0SJamie Gritton #ifdef VIMAGE 34466bb79563SJamie Gritton /* Prisons with their own network stack are not limited. */ 3447de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 34486bb79563SJamie Gritton return (0); 3449499650a0SJamie Gritton #endif 34506bb79563SJamie Gritton 3451ca04ba64SJamie Gritton error = 0; 3452ca04ba64SJamie Gritton switch (af) 3453ca04ba64SJamie Gritton { 3454ca04ba64SJamie Gritton #ifdef INET 3455ca04ba64SJamie Gritton case AF_INET: 34560304c731SJamie Gritton if (pr->pr_flags & PR_IP4) 34570304c731SJamie Gritton { 34580304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 3459eb8dcdeaSGleb Smirnoff if ((pr->pr_flags & PR_IP4) && 3460eb8dcdeaSGleb Smirnoff pr->pr_addrs[PR_INET] == NULL) 3461ca04ba64SJamie Gritton error = EAFNOSUPPORT; 34620304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 34630304c731SJamie Gritton } 3464ca04ba64SJamie Gritton break; 3465ca04ba64SJamie Gritton #endif 3466ca04ba64SJamie Gritton #ifdef INET6 3467ca04ba64SJamie Gritton case AF_INET6: 34680304c731SJamie Gritton if (pr->pr_flags & PR_IP6) 34690304c731SJamie Gritton { 34700304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 3471eb8dcdeaSGleb Smirnoff if ((pr->pr_flags & PR_IP6) && 3472eb8dcdeaSGleb Smirnoff pr->pr_addrs[PR_INET6] == NULL) 3473ca04ba64SJamie Gritton error = EAFNOSUPPORT; 34740304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 34750304c731SJamie Gritton } 3476ca04ba64SJamie Gritton break; 3477ca04ba64SJamie Gritton #endif 3478ca04ba64SJamie Gritton case AF_LOCAL: 3479ca04ba64SJamie Gritton case AF_ROUTE: 348004f75b98SAlexander V. Chernikov case AF_NETLINK: 3481ca04ba64SJamie Gritton break; 3482ca04ba64SJamie Gritton default: 34830304c731SJamie Gritton if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF)) 3484ca04ba64SJamie Gritton error = EAFNOSUPPORT; 3485ca04ba64SJamie Gritton } 3486ca04ba64SJamie Gritton return (error); 3487ca04ba64SJamie Gritton } 3488ca04ba64SJamie Gritton 3489ca04ba64SJamie Gritton /* 3490413628a7SBjoern A. Zeeb * Check if given address belongs to the jail referenced by cred (wrapper to 3491413628a7SBjoern A. Zeeb * prison_check_ip[46]). 3492413628a7SBjoern A. Zeeb * 34930304c731SJamie Gritton * Returns 0 if jail doesn't restrict the address family or if address belongs 34940304c731SJamie Gritton * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if 34950304c731SJamie Gritton * the jail doesn't allow the address family. IPv4 Address passed in in NBO. 3496413628a7SBjoern A. Zeeb */ 3497413628a7SBjoern A. Zeeb int 3498c83dda36SAlexander V. Chernikov prison_if(struct ucred *cred, const struct sockaddr *sa) 349975c13541SPoul-Henning Kamp { 3500413628a7SBjoern A. Zeeb #ifdef INET 3501c83dda36SAlexander V. Chernikov const struct sockaddr_in *sai; 3502413628a7SBjoern A. Zeeb #endif 3503413628a7SBjoern A. Zeeb #ifdef INET6 3504c83dda36SAlexander V. Chernikov const struct sockaddr_in6 *sai6; 3505413628a7SBjoern A. Zeeb #endif 3506b89e82ddSJamie Gritton int error; 350775c13541SPoul-Henning Kamp 3508413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3509413628a7SBjoern A. Zeeb KASSERT(sa != NULL, ("%s: sa is NULL", __func__)); 3510413628a7SBjoern A. Zeeb 3511de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE 3512de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 3513de0bd6f7SBjoern A. Zeeb return (0); 3514de0bd6f7SBjoern A. Zeeb #endif 3515de0bd6f7SBjoern A. Zeeb 3516b89e82ddSJamie Gritton error = 0; 3517413628a7SBjoern A. Zeeb switch (sa->sa_family) 3518413628a7SBjoern A. Zeeb { 3519413628a7SBjoern A. Zeeb #ifdef INET 3520413628a7SBjoern A. Zeeb case AF_INET: 3521c83dda36SAlexander V. Chernikov sai = (const struct sockaddr_in *)sa; 3522b89e82ddSJamie Gritton error = prison_check_ip4(cred, &sai->sin_addr); 3523413628a7SBjoern A. Zeeb break; 3524413628a7SBjoern A. Zeeb #endif 3525413628a7SBjoern A. Zeeb #ifdef INET6 3526413628a7SBjoern A. Zeeb case AF_INET6: 3527c83dda36SAlexander V. Chernikov sai6 = (const struct sockaddr_in6 *)sa; 3528b89e82ddSJamie Gritton error = prison_check_ip6(cred, &sai6->sin6_addr); 3529413628a7SBjoern A. Zeeb break; 3530413628a7SBjoern A. Zeeb #endif 3531413628a7SBjoern A. Zeeb default: 35320304c731SJamie Gritton if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF)) 3533b89e82ddSJamie Gritton error = EAFNOSUPPORT; 3534413628a7SBjoern A. Zeeb } 3535b89e82ddSJamie Gritton return (error); 353675c13541SPoul-Henning Kamp } 353791421ba2SRobert Watson 353891421ba2SRobert Watson /* 353991421ba2SRobert Watson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH. 354091421ba2SRobert Watson */ 354191421ba2SRobert Watson int 35429ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2) 354391421ba2SRobert Watson { 354491421ba2SRobert Watson 35450304c731SJamie Gritton return ((cred1->cr_prison == cred2->cr_prison || 35460304c731SJamie Gritton prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH); 35470304c731SJamie Gritton } 354891421ba2SRobert Watson 35490304c731SJamie Gritton /* 3550bba7a2e8SRick Macklem * For mountd/nfsd to run within a prison, it must be: 3551bba7a2e8SRick Macklem * - A vnet prison. 3552bba7a2e8SRick Macklem * - PR_ALLOW_NFSD must be set on it. 3553bba7a2e8SRick Macklem * - The root directory (pr_root) of the prison must be 3554bba7a2e8SRick Macklem * a file system mount point, so the mountd can hang 3555bba7a2e8SRick Macklem * export information on it. 355699187c3aSRick Macklem * - The prison's enforce_statfs cannot be 0, so that 355799187c3aSRick Macklem * mountd(8) can do exports. 3558bba7a2e8SRick Macklem */ 3559bba7a2e8SRick Macklem bool 3560bba7a2e8SRick Macklem prison_check_nfsd(struct ucred *cred) 3561bba7a2e8SRick Macklem { 3562bba7a2e8SRick Macklem 3563bba7a2e8SRick Macklem if (jailed_without_vnet(cred)) 3564bba7a2e8SRick Macklem return (false); 3565bba7a2e8SRick Macklem if (!prison_allow(cred, PR_ALLOW_NFSD)) 3566bba7a2e8SRick Macklem return (false); 3567bba7a2e8SRick Macklem if ((cred->cr_prison->pr_root->v_vflag & VV_ROOT) == 0) 3568bba7a2e8SRick Macklem return (false); 356999187c3aSRick Macklem if (cred->cr_prison->pr_enforce_statfs == 0) 357099187c3aSRick Macklem return (false); 3571bba7a2e8SRick Macklem return (true); 3572bba7a2e8SRick Macklem } 3573bba7a2e8SRick Macklem 3574bba7a2e8SRick Macklem /* 35750b0ae2e4SMina Galić * Return true if p2 is a child of p1, otherwise false. 35760304c731SJamie Gritton */ 35770b0ae2e4SMina Galić bool 35780304c731SJamie Gritton prison_ischild(struct prison *pr1, struct prison *pr2) 35790304c731SJamie Gritton { 35800304c731SJamie Gritton 35810304c731SJamie Gritton for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent) 35820304c731SJamie Gritton if (pr1 == pr2) 35830b0ae2e4SMina Galić return (true); 35840b0ae2e4SMina Galić return (false); 358591421ba2SRobert Watson } 358691421ba2SRobert Watson 358791421ba2SRobert Watson /* 3588f7496dcaSJamie Gritton * Return true if the prison is currently alive. A prison is alive if it 3589f7496dcaSJamie Gritton * holds user references and it isn't being removed. 359076ad42abSJamie Gritton */ 359176ad42abSJamie Gritton bool 3592eb8dcdeaSGleb Smirnoff prison_isalive(const struct prison *pr) 359376ad42abSJamie Gritton { 359476ad42abSJamie Gritton 35951158508aSJamie Gritton if (__predict_false(pr->pr_state != PRISON_STATE_ALIVE)) 3596cc7b7306SJamie Gritton return (false); 359776ad42abSJamie Gritton return (true); 359876ad42abSJamie Gritton } 359976ad42abSJamie Gritton 360076ad42abSJamie Gritton /* 360176ad42abSJamie Gritton * Return true if the prison is currently valid. A prison is valid if it has 360276ad42abSJamie Gritton * been fully created, and is not being destroyed. Note that dying prisons 3603f7496dcaSJamie Gritton * are still considered valid. Invalid prisons won't be found under normal 3604f7496dcaSJamie Gritton * circumstances, as they're only put in that state by functions that have 3605f7496dcaSJamie Gritton * an exclusive hold on allprison_lock. 360676ad42abSJamie Gritton */ 360776ad42abSJamie Gritton bool 360876ad42abSJamie Gritton prison_isvalid(struct prison *pr) 360976ad42abSJamie Gritton { 361076ad42abSJamie Gritton 36111158508aSJamie Gritton if (__predict_false(pr->pr_state == PRISON_STATE_INVALID)) 36121158508aSJamie Gritton return (false); 36136754ae25SJamie Gritton if (__predict_false(refcount_load(&pr->pr_ref) == 0)) 361476ad42abSJamie Gritton return (false); 361576ad42abSJamie Gritton return (true); 361676ad42abSJamie Gritton } 361776ad42abSJamie Gritton 361876ad42abSJamie Gritton /* 36190b0ae2e4SMina Galić * Return true if the passed credential is in a jail and that jail does not 36200b0ae2e4SMina Galić * have its own virtual network stack, otherwise false. 3621de0bd6f7SBjoern A. Zeeb */ 36220b0ae2e4SMina Galić bool 3623de0bd6f7SBjoern A. Zeeb jailed_without_vnet(struct ucred *cred) 3624de0bd6f7SBjoern A. Zeeb { 3625de0bd6f7SBjoern A. Zeeb 3626de0bd6f7SBjoern A. Zeeb if (!jailed(cred)) 36270b0ae2e4SMina Galić return (false); 3628de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE 3629de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 36300b0ae2e4SMina Galić return (false); 3631de0bd6f7SBjoern A. Zeeb #endif 3632de0bd6f7SBjoern A. Zeeb 36330b0ae2e4SMina Galić return (true); 3634de0bd6f7SBjoern A. Zeeb } 3635de0bd6f7SBjoern A. Zeeb 3636de0bd6f7SBjoern A. Zeeb /* 36377455b100SJamie Gritton * Return the correct hostname (domainname, et al) for the passed credential. 36389484d0c0SRobert Drehmel */ 3639ad1ff099SRobert Drehmel void 36409ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size) 36419484d0c0SRobert Drehmel { 364276ca6f88SJamie Gritton struct prison *pr; 36439484d0c0SRobert Drehmel 36447455b100SJamie Gritton /* 36457455b100SJamie Gritton * A NULL credential can be used to shortcut to the physical 36467455b100SJamie Gritton * system's hostname. 36477455b100SJamie Gritton */ 364876ca6f88SJamie Gritton pr = (cred != NULL) ? cred->cr_prison : &prison0; 364976ca6f88SJamie Gritton mtx_lock(&pr->pr_mtx); 3650c1f19219SJamie Gritton strlcpy(buf, pr->pr_hostname, size); 365176ca6f88SJamie Gritton mtx_unlock(&pr->pr_mtx); 36529484d0c0SRobert Drehmel } 3653fd7a8150SMike Barcroft 36547455b100SJamie Gritton void 36557455b100SJamie Gritton getcreddomainname(struct ucred *cred, char *buf, size_t size) 36567455b100SJamie Gritton { 36577455b100SJamie Gritton 36587455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 3659c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_domainname, size); 36607455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 36617455b100SJamie Gritton } 36627455b100SJamie Gritton 36637455b100SJamie Gritton void 36647455b100SJamie Gritton getcredhostuuid(struct ucred *cred, char *buf, size_t size) 36657455b100SJamie Gritton { 36667455b100SJamie Gritton 36677455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 3668c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_hostuuid, size); 36697455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 36707455b100SJamie Gritton } 36717455b100SJamie Gritton 36727455b100SJamie Gritton void 36737455b100SJamie Gritton getcredhostid(struct ucred *cred, unsigned long *hostid) 36747455b100SJamie Gritton { 36757455b100SJamie Gritton 36767455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 36777455b100SJamie Gritton *hostid = cred->cr_prison->pr_hostid; 36787455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 36797455b100SJamie Gritton } 36807455b100SJamie Gritton 36813f8bc99cSKristof Provost void 36823f8bc99cSKristof Provost getjailname(struct ucred *cred, char *name, size_t len) 36833f8bc99cSKristof Provost { 36843f8bc99cSKristof Provost 36853f8bc99cSKristof Provost mtx_lock(&cred->cr_prison->pr_mtx); 36863f8bc99cSKristof Provost strlcpy(name, cred->cr_prison->pr_name, len); 36873f8bc99cSKristof Provost mtx_unlock(&cred->cr_prison->pr_mtx); 36883f8bc99cSKristof Provost } 36893f8bc99cSKristof Provost 3690eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE 3691eb79e1c7SBjoern A. Zeeb /* 3692eb79e1c7SBjoern A. Zeeb * Determine whether the prison represented by cred owns 3693eb79e1c7SBjoern A. Zeeb * its vnet rather than having it inherited. 3694eb79e1c7SBjoern A. Zeeb * 36950b0ae2e4SMina Galić * Returns true in case the prison owns the vnet, false otherwise. 3696eb79e1c7SBjoern A. Zeeb */ 36970b0ae2e4SMina Galić bool 3698eb79e1c7SBjoern A. Zeeb prison_owns_vnet(struct ucred *cred) 3699eb79e1c7SBjoern A. Zeeb { 3700eb79e1c7SBjoern A. Zeeb 3701eb79e1c7SBjoern A. Zeeb /* 3702eb79e1c7SBjoern A. Zeeb * vnets cannot be added/removed after jail creation, 3703eb79e1c7SBjoern A. Zeeb * so no need to lock here. 3704eb79e1c7SBjoern A. Zeeb */ 37050b0ae2e4SMina Galić return ((cred->cr_prison->pr_flags & PR_VNET) != 0); 3706eb79e1c7SBjoern A. Zeeb } 3707eb79e1c7SBjoern A. Zeeb #endif 3708eb79e1c7SBjoern A. Zeeb 3709f08df373SRobert Watson /* 3710820a0de9SPawel Jakub Dawidek * Determine whether the subject represented by cred can "see" 3711820a0de9SPawel Jakub Dawidek * status of a mount point. 3712820a0de9SPawel Jakub Dawidek * Returns: 0 for permitted, ENOENT otherwise. 3713820a0de9SPawel Jakub Dawidek * XXX: This function should be called cr_canseemount() and should be 3714820a0de9SPawel Jakub Dawidek * placed in kern_prot.c. 3715f08df373SRobert Watson */ 3716f08df373SRobert Watson int 3717820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp) 3718f08df373SRobert Watson { 3719820a0de9SPawel Jakub Dawidek struct prison *pr; 3720820a0de9SPawel Jakub Dawidek struct statfs *sp; 3721820a0de9SPawel Jakub Dawidek size_t len; 3722f08df373SRobert Watson 3723820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 37240304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 37250304c731SJamie Gritton return (0); 3726820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) 3727820a0de9SPawel Jakub Dawidek return (0); 37280304c731SJamie Gritton if (pr->pr_enforce_statfs == 2) 3729820a0de9SPawel Jakub Dawidek return (ENOENT); 3730820a0de9SPawel Jakub Dawidek /* 3731820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 3732820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 3733820a0de9SPawel Jakub Dawidek * This is ugly check, but this is the only situation when jail's 3734820a0de9SPawel Jakub Dawidek * directory ends with '/'. 3735820a0de9SPawel Jakub Dawidek */ 3736820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 3737820a0de9SPawel Jakub Dawidek return (0); 3738820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 3739820a0de9SPawel Jakub Dawidek sp = &mp->mnt_stat; 3740820a0de9SPawel Jakub Dawidek if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0) 3741820a0de9SPawel Jakub Dawidek return (ENOENT); 3742820a0de9SPawel Jakub Dawidek /* 3743820a0de9SPawel Jakub Dawidek * Be sure that we don't have situation where jail's root directory 3744820a0de9SPawel Jakub Dawidek * is "/some/path" and mount point is "/some/pathpath". 3745820a0de9SPawel Jakub Dawidek */ 3746820a0de9SPawel Jakub Dawidek if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/') 3747820a0de9SPawel Jakub Dawidek return (ENOENT); 3748f08df373SRobert Watson return (0); 3749f08df373SRobert Watson } 3750820a0de9SPawel Jakub Dawidek 3751820a0de9SPawel Jakub Dawidek void 3752820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp) 3753820a0de9SPawel Jakub Dawidek { 3754820a0de9SPawel Jakub Dawidek char jpath[MAXPATHLEN]; 3755820a0de9SPawel Jakub Dawidek struct prison *pr; 3756820a0de9SPawel Jakub Dawidek size_t len; 3757820a0de9SPawel Jakub Dawidek 3758820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 37590304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 37600304c731SJamie Gritton return; 3761820a0de9SPawel Jakub Dawidek if (prison_canseemount(cred, mp) != 0) { 3762820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3763820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, "[restricted]", 3764820a0de9SPawel Jakub Dawidek sizeof(sp->f_mntonname)); 3765820a0de9SPawel Jakub Dawidek return; 3766820a0de9SPawel Jakub Dawidek } 3767820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) { 3768820a0de9SPawel Jakub Dawidek /* 3769820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3770820a0de9SPawel Jakub Dawidek * the valid path left there. 3771820a0de9SPawel Jakub Dawidek */ 3772820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3773820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3774820a0de9SPawel Jakub Dawidek return; 3775820a0de9SPawel Jakub Dawidek } 3776820a0de9SPawel Jakub Dawidek /* 3777820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 3778820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 3779820a0de9SPawel Jakub Dawidek */ 3780820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 3781820a0de9SPawel Jakub Dawidek return; 3782820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 3783820a0de9SPawel Jakub Dawidek strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath)); 3784820a0de9SPawel Jakub Dawidek /* 3785820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3786820a0de9SPawel Jakub Dawidek * the valid path left there. 3787820a0de9SPawel Jakub Dawidek */ 3788820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3789820a0de9SPawel Jakub Dawidek if (*jpath == '\0') { 3790820a0de9SPawel Jakub Dawidek /* Should never happen. */ 3791820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3792820a0de9SPawel Jakub Dawidek } else { 3793820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname)); 3794820a0de9SPawel Jakub Dawidek } 3795f08df373SRobert Watson } 3796f08df373SRobert Watson 3797800c9408SRobert Watson /* 3798800c9408SRobert Watson * Check with permission for a specific privilege is granted within jail. We 3799800c9408SRobert Watson * have a specific list of accepted privileges; the rest are denied. 3800800c9408SRobert Watson */ 3801800c9408SRobert Watson int 3802800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv) 3803800c9408SRobert Watson { 38040fe74ae6SJamie Gritton struct prison *pr; 38050fe74ae6SJamie Gritton int error; 3806800c9408SRobert Watson 38077b2ff0dcSMateusz Guzik /* 38087b2ff0dcSMateusz Guzik * Some policies have custom handlers. This routine should not be 38097b2ff0dcSMateusz Guzik * called for them. See priv_check_cred(). 38107b2ff0dcSMateusz Guzik */ 38117b2ff0dcSMateusz Guzik switch (priv) { 3812a459a6cfSMateusz Guzik case PRIV_VFS_LOOKUP: 38137b2ff0dcSMateusz Guzik case PRIV_VFS_GENERATION: 38147b2ff0dcSMateusz Guzik KASSERT(0, ("prison_priv_check instead of a custom handler " 38157b2ff0dcSMateusz Guzik "called for %d\n", priv)); 38167b2ff0dcSMateusz Guzik } 38177b2ff0dcSMateusz Guzik 3818800c9408SRobert Watson if (!jailed(cred)) 3819800c9408SRobert Watson return (0); 3820800c9408SRobert Watson 38216bb79563SJamie Gritton #ifdef VIMAGE 38226bb79563SJamie Gritton /* 38236bb79563SJamie Gritton * Privileges specific to prisons with a virtual network stack. 38246bb79563SJamie Gritton * There might be a duplicate entry here in case the privilege 38256bb79563SJamie Gritton * is only granted conditionally in the legacy jail case. 38266bb79563SJamie Gritton */ 38276bb79563SJamie Gritton switch (priv) { 38286bb79563SJamie Gritton /* 38296bb79563SJamie Gritton * NFS-specific privileges. 38306bb79563SJamie Gritton */ 38316bb79563SJamie Gritton case PRIV_NFS_DAEMON: 3832bba7a2e8SRick Macklem case PRIV_VFS_GETFH: 3833bba7a2e8SRick Macklem case PRIV_VFS_MOUNT_EXPORTED: 3834bba7a2e8SRick Macklem if (!prison_check_nfsd(cred)) 3835bba7a2e8SRick Macklem return (EPERM); 3836bba7a2e8SRick Macklem #ifdef notyet 38376bb79563SJamie Gritton case PRIV_NFS_LOCKD: 38386bb79563SJamie Gritton #endif 38396bb79563SJamie Gritton /* 38406bb79563SJamie Gritton * Network stack privileges. 38416bb79563SJamie Gritton */ 38426bb79563SJamie Gritton case PRIV_NET_BRIDGE: 38436bb79563SJamie Gritton case PRIV_NET_GRE: 38446bb79563SJamie Gritton case PRIV_NET_BPF: 38456bb79563SJamie Gritton case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */ 38466bb79563SJamie Gritton case PRIV_NET_ROUTE: 38476bb79563SJamie Gritton case PRIV_NET_TAP: 38486bb79563SJamie Gritton case PRIV_NET_SETIFMTU: 38496bb79563SJamie Gritton case PRIV_NET_SETIFFLAGS: 38506bb79563SJamie Gritton case PRIV_NET_SETIFCAP: 3851215940b3SXin LI case PRIV_NET_SETIFDESCR: 38526bb79563SJamie Gritton case PRIV_NET_SETIFNAME : 38536bb79563SJamie Gritton case PRIV_NET_SETIFMETRIC: 38546bb79563SJamie Gritton case PRIV_NET_SETIFPHYS: 38556bb79563SJamie Gritton case PRIV_NET_SETIFMAC: 3856389474c1SKonstantin Belousov case PRIV_NET_SETLANPCP: 38576bb79563SJamie Gritton case PRIV_NET_ADDMULTI: 38586bb79563SJamie Gritton case PRIV_NET_DELMULTI: 38596bb79563SJamie Gritton case PRIV_NET_HWIOCTL: 38606bb79563SJamie Gritton case PRIV_NET_SETLLADDR: 38616bb79563SJamie Gritton case PRIV_NET_ADDIFGROUP: 38626bb79563SJamie Gritton case PRIV_NET_DELIFGROUP: 38636bb79563SJamie Gritton case PRIV_NET_IFCREATE: 38646bb79563SJamie Gritton case PRIV_NET_IFDESTROY: 38656bb79563SJamie Gritton case PRIV_NET_ADDIFADDR: 38666bb79563SJamie Gritton case PRIV_NET_DELIFADDR: 38676bb79563SJamie Gritton case PRIV_NET_LAGG: 38686bb79563SJamie Gritton case PRIV_NET_GIF: 38696bb79563SJamie Gritton case PRIV_NET_SETIFVNET: 387035fd7bc0SBjoern A. Zeeb case PRIV_NET_SETIFFIB: 3871ab91feabSKristof Provost case PRIV_NET_OVPN: 387243f8c763SZhenlei Huang case PRIV_NET_ME: 3873744bfb21SJohn Baldwin case PRIV_NET_WG: 38746bb79563SJamie Gritton 38756bb79563SJamie Gritton /* 38766bb79563SJamie Gritton * 802.11-related privileges. 38776bb79563SJamie Gritton */ 3878f7d38a13SAdrian Chadd case PRIV_NET80211_VAP_GETKEY: 3879f7d38a13SAdrian Chadd case PRIV_NET80211_VAP_MANAGE: 38806bb79563SJamie Gritton 38816bb79563SJamie Gritton #ifdef notyet 38826bb79563SJamie Gritton /* 38836bb79563SJamie Gritton * ATM privileges. 38846bb79563SJamie Gritton */ 38856bb79563SJamie Gritton case PRIV_NETATM_CFG: 38866bb79563SJamie Gritton case PRIV_NETATM_ADD: 38876bb79563SJamie Gritton case PRIV_NETATM_DEL: 38886bb79563SJamie Gritton case PRIV_NETATM_SET: 38896bb79563SJamie Gritton 38906bb79563SJamie Gritton /* 38916bb79563SJamie Gritton * Bluetooth privileges. 38926bb79563SJamie Gritton */ 38936bb79563SJamie Gritton case PRIV_NETBLUETOOTH_RAW: 38946bb79563SJamie Gritton #endif 38956bb79563SJamie Gritton 38966bb79563SJamie Gritton /* 38976bb79563SJamie Gritton * Netgraph and netgraph module privileges. 38986bb79563SJamie Gritton */ 38996bb79563SJamie Gritton case PRIV_NETGRAPH_CONTROL: 39006bb79563SJamie Gritton #ifdef notyet 39016bb79563SJamie Gritton case PRIV_NETGRAPH_TTY: 39026bb79563SJamie Gritton #endif 39036bb79563SJamie Gritton 39046bb79563SJamie Gritton /* 39056bb79563SJamie Gritton * IPv4 and IPv6 privileges. 39066bb79563SJamie Gritton */ 39076bb79563SJamie Gritton case PRIV_NETINET_IPFW: 39086bb79563SJamie Gritton case PRIV_NETINET_DIVERT: 39096bb79563SJamie Gritton case PRIV_NETINET_PF: 39106bb79563SJamie Gritton case PRIV_NETINET_DUMMYNET: 39116bb79563SJamie Gritton case PRIV_NETINET_CARP: 39126bb79563SJamie Gritton case PRIV_NETINET_MROUTE: 39136bb79563SJamie Gritton case PRIV_NETINET_RAW: 39146bb79563SJamie Gritton case PRIV_NETINET_ADDRCTRL6: 39156bb79563SJamie Gritton case PRIV_NETINET_ND6: 39166bb79563SJamie Gritton case PRIV_NETINET_SCOPE6: 39176bb79563SJamie Gritton case PRIV_NETINET_ALIFETIME6: 39186bb79563SJamie Gritton case PRIV_NETINET_IPSEC: 39196bb79563SJamie Gritton case PRIV_NETINET_BINDANY: 39206bb79563SJamie Gritton 39216bb79563SJamie Gritton #ifdef notyet 39226bb79563SJamie Gritton /* 39236bb79563SJamie Gritton * NCP privileges. 39246bb79563SJamie Gritton */ 39256bb79563SJamie Gritton case PRIV_NETNCP: 39266bb79563SJamie Gritton 39276bb79563SJamie Gritton /* 39286bb79563SJamie Gritton * SMB privileges. 39296bb79563SJamie Gritton */ 39306bb79563SJamie Gritton case PRIV_NETSMB: 39316bb79563SJamie Gritton #endif 39326bb79563SJamie Gritton 39336bb79563SJamie Gritton /* 39346bb79563SJamie Gritton * No default: or deny here. 39356bb79563SJamie Gritton * In case of no permit fall through to next switch(). 39366bb79563SJamie Gritton */ 39376bb79563SJamie Gritton if (cred->cr_prison->pr_flags & PR_VNET) 39386bb79563SJamie Gritton return (0); 39396bb79563SJamie Gritton } 39406bb79563SJamie Gritton #endif /* VIMAGE */ 39416bb79563SJamie Gritton 3942800c9408SRobert Watson switch (priv) { 3943800c9408SRobert Watson /* 3944800c9408SRobert Watson * Allow ktrace privileges for root in jail. 3945800c9408SRobert Watson */ 3946800c9408SRobert Watson case PRIV_KTRACE: 3947800c9408SRobert Watson 3948c3c1b5e6SRobert Watson #if 0 3949800c9408SRobert Watson /* 3950800c9408SRobert Watson * Allow jailed processes to configure audit identity and 3951800c9408SRobert Watson * submit audit records (login, etc). In the future we may 3952800c9408SRobert Watson * want to further refine the relationship between audit and 3953800c9408SRobert Watson * jail. 3954800c9408SRobert Watson */ 3955800c9408SRobert Watson case PRIV_AUDIT_GETAUDIT: 3956800c9408SRobert Watson case PRIV_AUDIT_SETAUDIT: 3957800c9408SRobert Watson case PRIV_AUDIT_SUBMIT: 3958c3c1b5e6SRobert Watson #endif 3959800c9408SRobert Watson 3960800c9408SRobert Watson /* 3961800c9408SRobert Watson * Allow jailed processes to manipulate process UNIX 3962800c9408SRobert Watson * credentials in any way they see fit. 3963800c9408SRobert Watson */ 3964ddb3eb4eSOlivier Certner case PRIV_CRED_SETCRED: 3965800c9408SRobert Watson case PRIV_CRED_SETUID: 3966800c9408SRobert Watson case PRIV_CRED_SETEUID: 3967800c9408SRobert Watson case PRIV_CRED_SETGID: 3968800c9408SRobert Watson case PRIV_CRED_SETEGID: 3969800c9408SRobert Watson case PRIV_CRED_SETGROUPS: 3970800c9408SRobert Watson case PRIV_CRED_SETREUID: 3971800c9408SRobert Watson case PRIV_CRED_SETREGID: 3972800c9408SRobert Watson case PRIV_CRED_SETRESUID: 3973800c9408SRobert Watson case PRIV_CRED_SETRESGID: 3974800c9408SRobert Watson 3975800c9408SRobert Watson /* 3976800c9408SRobert Watson * Jail implements visibility constraints already, so allow 3977800c9408SRobert Watson * jailed root to override uid/gid-based constraints. 3978800c9408SRobert Watson */ 3979800c9408SRobert Watson case PRIV_SEEOTHERGIDS: 3980800c9408SRobert Watson case PRIV_SEEOTHERUIDS: 39817974ca1cSOlivier Certner case PRIV_SEEJAILPROC: 3982800c9408SRobert Watson 3983800c9408SRobert Watson /* 3984800c9408SRobert Watson * Jail implements inter-process debugging limits already, so 3985800c9408SRobert Watson * allow jailed root various debugging privileges. 3986800c9408SRobert Watson */ 3987800c9408SRobert Watson case PRIV_DEBUG_DIFFCRED: 3988800c9408SRobert Watson case PRIV_DEBUG_SUGID: 3989800c9408SRobert Watson case PRIV_DEBUG_UNPRIV: 3990800c9408SRobert Watson 3991800c9408SRobert Watson /* 3992800c9408SRobert Watson * Allow jail to set various resource limits and login 3993800c9408SRobert Watson * properties, and for now, exceed process resource limits. 3994800c9408SRobert Watson */ 3995800c9408SRobert Watson case PRIV_PROC_LIMIT: 3996800c9408SRobert Watson case PRIV_PROC_SETLOGIN: 3997800c9408SRobert Watson case PRIV_PROC_SETRLIMIT: 3998800c9408SRobert Watson 3999800c9408SRobert Watson /* 4000800c9408SRobert Watson * System V and POSIX IPC privileges are granted in jail. 4001800c9408SRobert Watson */ 4002800c9408SRobert Watson case PRIV_IPC_READ: 4003800c9408SRobert Watson case PRIV_IPC_WRITE: 4004800c9408SRobert Watson case PRIV_IPC_ADMIN: 4005800c9408SRobert Watson case PRIV_IPC_MSGSIZE: 4006800c9408SRobert Watson case PRIV_MQ_ADMIN: 4007800c9408SRobert Watson 4008800c9408SRobert Watson /* 40090304c731SJamie Gritton * Jail operations within a jail work on child jails. 40100304c731SJamie Gritton */ 40110304c731SJamie Gritton case PRIV_JAIL_ATTACH: 40120304c731SJamie Gritton case PRIV_JAIL_SET: 40130304c731SJamie Gritton case PRIV_JAIL_REMOVE: 40140304c731SJamie Gritton 40150304c731SJamie Gritton /* 4016800c9408SRobert Watson * Jail implements its own inter-process limits, so allow 4017800c9408SRobert Watson * root processes in jail to change scheduling on other 4018800c9408SRobert Watson * processes in the same jail. Likewise for signalling. 4019800c9408SRobert Watson */ 4020800c9408SRobert Watson case PRIV_SCHED_DIFFCRED: 4021413628a7SBjoern A. Zeeb case PRIV_SCHED_CPUSET: 4022800c9408SRobert Watson case PRIV_SIGNAL_DIFFCRED: 4023800c9408SRobert Watson case PRIV_SIGNAL_SUGID: 4024800c9408SRobert Watson 4025800c9408SRobert Watson /* 4026800c9408SRobert Watson * Allow jailed processes to write to sysctls marked as jail 4027800c9408SRobert Watson * writable. 4028800c9408SRobert Watson */ 4029800c9408SRobert Watson case PRIV_SYSCTL_WRITEJAIL: 4030800c9408SRobert Watson 4031800c9408SRobert Watson /* 4032800c9408SRobert Watson * Allow root in jail to manage a variety of quota 4033e82d0201SRobert Watson * properties. These should likely be conditional on a 4034e82d0201SRobert Watson * configuration option. 4035800c9408SRobert Watson */ 403695b091d2SRobert Watson case PRIV_VFS_GETQUOTA: 403795b091d2SRobert Watson case PRIV_VFS_SETQUOTA: 4038800c9408SRobert Watson 4039800c9408SRobert Watson /* 4040800c9408SRobert Watson * Since Jail relies on chroot() to implement file system 4041800c9408SRobert Watson * protections, grant many VFS privileges to root in jail. 4042800c9408SRobert Watson * Be careful to exclude mount-related and NFS-related 4043800c9408SRobert Watson * privileges. 4044800c9408SRobert Watson */ 4045800c9408SRobert Watson case PRIV_VFS_READ: 4046800c9408SRobert Watson case PRIV_VFS_WRITE: 4047800c9408SRobert Watson case PRIV_VFS_ADMIN: 4048800c9408SRobert Watson case PRIV_VFS_EXEC: 4049800c9408SRobert Watson case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */ 4050800c9408SRobert Watson case PRIV_VFS_CHFLAGS_DEV: 4051800c9408SRobert Watson case PRIV_VFS_CHOWN: 4052800c9408SRobert Watson case PRIV_VFS_CHROOT: 4053bb531912SPawel Jakub Dawidek case PRIV_VFS_RETAINSUGID: 4054800c9408SRobert Watson case PRIV_VFS_FCHROOT: 4055800c9408SRobert Watson case PRIV_VFS_LINK: 4056800c9408SRobert Watson case PRIV_VFS_SETGID: 4057e41966dcSRobert Watson case PRIV_VFS_STAT: 4058800c9408SRobert Watson case PRIV_VFS_STICKYFILE: 4059bb56d716SJamie Gritton 4060bb56d716SJamie Gritton /* 4061bb56d716SJamie Gritton * As in the non-jail case, non-root users are expected to be 406270de1003SGordon Bergling * able to read kernel/physical memory (provided /dev/[k]mem 4063bb56d716SJamie Gritton * exists in the jail and they have permission to access it). 4064bb56d716SJamie Gritton */ 4065bb56d716SJamie Gritton case PRIV_KMEM_READ: 4066800c9408SRobert Watson return (0); 4067800c9408SRobert Watson 4068800c9408SRobert Watson /* 4069800c9408SRobert Watson * Depending on the global setting, allow privilege of 4070800c9408SRobert Watson * setting system flags. 4071800c9408SRobert Watson */ 4072800c9408SRobert Watson case PRIV_VFS_SYSFLAGS: 40730304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS) 4074800c9408SRobert Watson return (0); 4075800c9408SRobert Watson else 4076800c9408SRobert Watson return (EPERM); 4077800c9408SRobert Watson 4078800c9408SRobert Watson /* 4079f3a8d2f9SPawel Jakub Dawidek * Depending on the global setting, allow privilege of 4080f3a8d2f9SPawel Jakub Dawidek * mounting/unmounting file systems. 4081f3a8d2f9SPawel Jakub Dawidek */ 4082f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT: 4083f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_UNMOUNT: 4084f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT_NONUSER: 408524b0502eSPawel Jakub Dawidek case PRIV_VFS_MOUNT_OWNER: 40860fe74ae6SJamie Gritton pr = cred->cr_prison; 40870fe74ae6SJamie Gritton prison_lock(pr); 40880fe74ae6SJamie Gritton if (pr->pr_allow & PR_ALLOW_MOUNT && pr->pr_enforce_statfs < 2) 40890fe74ae6SJamie Gritton error = 0; 4090f3a8d2f9SPawel Jakub Dawidek else 40910fe74ae6SJamie Gritton error = EPERM; 40920fe74ae6SJamie Gritton prison_unlock(pr); 40930fe74ae6SJamie Gritton return (error); 4094f3a8d2f9SPawel Jakub Dawidek 4095f3a8d2f9SPawel Jakub Dawidek /* 409663619b6dSKyle Evans * Jails should hold no disposition on the PRIV_VFS_READ_DIR 409763619b6dSKyle Evans * policy. priv_check_cred will not specifically allow it, and 409863619b6dSKyle Evans * we may want a MAC policy to allow it. 409963619b6dSKyle Evans */ 410063619b6dSKyle Evans case PRIV_VFS_READ_DIR: 410163619b6dSKyle Evans return (0); 410263619b6dSKyle Evans 410363619b6dSKyle Evans /* 4104cb48780dSShawn Webb * Conditionally allow privileged process in the jail to 4105cb48780dSShawn Webb * manipulate filesystem extended attributes in the system 4106cb48780dSShawn Webb * namespace. 4107cb48780dSShawn Webb */ 4108cb48780dSShawn Webb case PRIV_VFS_EXTATTR_SYSTEM: 4109cb48780dSShawn Webb if ((cred->cr_prison->pr_allow & PR_ALLOW_EXTATTR) != 0) 4110cb48780dSShawn Webb return (0); 4111cb48780dSShawn Webb else 4112cb48780dSShawn Webb return (EPERM); 4113cb48780dSShawn Webb 4114cb48780dSShawn Webb /* 4115ccd6ac9fSAntoine Brodin * Conditionnaly allow locking (unlocking) physical pages 4116ccd6ac9fSAntoine Brodin * in memory. 4117ccd6ac9fSAntoine Brodin */ 4118ccd6ac9fSAntoine Brodin case PRIV_VM_MLOCK: 4119ccd6ac9fSAntoine Brodin case PRIV_VM_MUNLOCK: 4120ccd6ac9fSAntoine Brodin if (cred->cr_prison->pr_allow & PR_ALLOW_MLOCK) 4121ccd6ac9fSAntoine Brodin return (0); 4122ccd6ac9fSAntoine Brodin else 4123ccd6ac9fSAntoine Brodin return (EPERM); 4124ccd6ac9fSAntoine Brodin 4125ccd6ac9fSAntoine Brodin /* 4126e28f9b7dSAllan Jude * Conditionally allow jailed root to bind reserved ports. 4127800c9408SRobert Watson */ 4128800c9408SRobert Watson case PRIV_NETINET_RESERVEDPORT: 4129e28f9b7dSAllan Jude if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS) 4130e28f9b7dSAllan Jude return (0); 4131e28f9b7dSAllan Jude else 4132e28f9b7dSAllan Jude return (EPERM); 4133e28f9b7dSAllan Jude 4134e28f9b7dSAllan Jude /* 4135e28f9b7dSAllan Jude * Allow jailed root to reuse in-use ports. 4136e28f9b7dSAllan Jude */ 41374b084056SRobert Watson case PRIV_NETINET_REUSEPORT: 4138800c9408SRobert Watson return (0); 4139800c9408SRobert Watson 4140800c9408SRobert Watson /* 4141e3043798SPedro F. Giffuni * Allow jailed root to set certain IPv4/6 (option) headers. 414279ba3952SBjoern A. Zeeb */ 414379ba3952SBjoern A. Zeeb case PRIV_NETINET_SETHDROPTS: 414479ba3952SBjoern A. Zeeb return (0); 414579ba3952SBjoern A. Zeeb 414679ba3952SBjoern A. Zeeb /* 4147800c9408SRobert Watson * Conditionally allow creating raw sockets in jail. 4148800c9408SRobert Watson */ 4149800c9408SRobert Watson case PRIV_NETINET_RAW: 41500304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS) 4151800c9408SRobert Watson return (0); 4152800c9408SRobert Watson else 4153800c9408SRobert Watson return (EPERM); 4154800c9408SRobert Watson 4155800c9408SRobert Watson /* 4156800c9408SRobert Watson * Since jail implements its own visibility limits on netstat 4157800c9408SRobert Watson * sysctls, allow getcred. This allows identd to work in 4158800c9408SRobert Watson * jail. 4159800c9408SRobert Watson */ 4160800c9408SRobert Watson case PRIV_NETINET_GETCRED: 4161800c9408SRobert Watson return (0); 4162800c9408SRobert Watson 41632bfc50bcSEdward Tomasz Napierala /* 41642bfc50bcSEdward Tomasz Napierala * Allow jailed root to set loginclass. 41652bfc50bcSEdward Tomasz Napierala */ 41662bfc50bcSEdward Tomasz Napierala case PRIV_PROC_SETLOGINCLASS: 41672bfc50bcSEdward Tomasz Napierala return (0); 41682bfc50bcSEdward Tomasz Napierala 4169b19d66fdSJamie Gritton /* 41704520f617SJamie Gritton * Do not allow a process inside a jail to read the kernel 4171b19d66fdSJamie Gritton * message buffer unless explicitly permitted. 4172b19d66fdSJamie Gritton */ 4173b19d66fdSJamie Gritton case PRIV_MSGBUF: 4174b19d66fdSJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_READ_MSGBUF) 4175b19d66fdSJamie Gritton return (0); 4176b19d66fdSJamie Gritton return (EPERM); 4177b19d66fdSJamie Gritton 4178d3bb35d4SMariusz Zaborski /* 4179d3bb35d4SMariusz Zaborski * Conditionally allow privileged process in the jail adjust 4180d3bb35d4SMariusz Zaborski * machine time. 4181d3bb35d4SMariusz Zaborski */ 4182d3bb35d4SMariusz Zaborski case PRIV_ADJTIME: 4183d3bb35d4SMariusz Zaborski case PRIV_NTP_ADJTIME: 4184d3bb35d4SMariusz Zaborski if (cred->cr_prison->pr_allow & 4185d3bb35d4SMariusz Zaborski (PR_ALLOW_ADJTIME | PR_ALLOW_SETTIME)) { 4186d3bb35d4SMariusz Zaborski return (0); 4187d3bb35d4SMariusz Zaborski } 4188d3bb35d4SMariusz Zaborski return (EPERM); 4189d3bb35d4SMariusz Zaborski 4190d3bb35d4SMariusz Zaborski /* 4191d3bb35d4SMariusz Zaborski * Conditionally allow privileged process in the jail set 4192d3bb35d4SMariusz Zaborski * machine time. 4193d3bb35d4SMariusz Zaborski */ 4194d3bb35d4SMariusz Zaborski case PRIV_CLOCK_SETTIME: 4195d3bb35d4SMariusz Zaborski if (cred->cr_prison->pr_allow & PR_ALLOW_SETTIME) 4196d3bb35d4SMariusz Zaborski return (0); 4197d3bb35d4SMariusz Zaborski else 4198d3bb35d4SMariusz Zaborski return (EPERM); 4199d3bb35d4SMariusz Zaborski 4200800c9408SRobert Watson default: 4201800c9408SRobert Watson /* 4202800c9408SRobert Watson * In all remaining cases, deny the privilege request. This 4203800c9408SRobert Watson * includes almost all network privileges, many system 4204800c9408SRobert Watson * configuration privileges. 4205800c9408SRobert Watson */ 4206800c9408SRobert Watson return (EPERM); 4207800c9408SRobert Watson } 4208800c9408SRobert Watson } 4209800c9408SRobert Watson 42100304c731SJamie Gritton /* 42110304c731SJamie Gritton * Return the part of pr2's name that is relative to pr1, or the whole name 42120304c731SJamie Gritton * if it does not directly follow. 42130304c731SJamie Gritton */ 42140304c731SJamie Gritton 42150304c731SJamie Gritton char * 42160304c731SJamie Gritton prison_name(struct prison *pr1, struct prison *pr2) 42170304c731SJamie Gritton { 42180304c731SJamie Gritton char *name; 42190304c731SJamie Gritton 42200304c731SJamie Gritton /* Jails see themselves as "0" (if they see themselves at all). */ 42210304c731SJamie Gritton if (pr1 == pr2) 42220304c731SJamie Gritton return "0"; 42230304c731SJamie Gritton name = pr2->pr_name; 42240304c731SJamie Gritton if (prison_ischild(pr1, pr2)) { 42250304c731SJamie Gritton /* 42260304c731SJamie Gritton * pr1 isn't locked (and allprison_lock may not be either) 42270304c731SJamie Gritton * so its length can't be counted on. But the number of dots 42280304c731SJamie Gritton * can be counted on - and counted. 42290304c731SJamie Gritton */ 42300304c731SJamie Gritton for (; pr1 != &prison0; pr1 = pr1->pr_parent) 42310304c731SJamie Gritton name = strchr(name, '.') + 1; 42320304c731SJamie Gritton } 42330304c731SJamie Gritton return (name); 42340304c731SJamie Gritton } 42350304c731SJamie Gritton 42360304c731SJamie Gritton /* 42370304c731SJamie Gritton * Return the part of pr2's path that is relative to pr1, or the whole path 42380304c731SJamie Gritton * if it does not directly follow. 42390304c731SJamie Gritton */ 42400304c731SJamie Gritton static char * 42410304c731SJamie Gritton prison_path(struct prison *pr1, struct prison *pr2) 42420304c731SJamie Gritton { 42430304c731SJamie Gritton char *path1, *path2; 42440304c731SJamie Gritton int len1; 42450304c731SJamie Gritton 42460304c731SJamie Gritton path1 = pr1->pr_path; 42470304c731SJamie Gritton path2 = pr2->pr_path; 42480304c731SJamie Gritton if (!strcmp(path1, "/")) 42490304c731SJamie Gritton return (path2); 42500304c731SJamie Gritton len1 = strlen(path1); 42510304c731SJamie Gritton if (strncmp(path1, path2, len1)) 42520304c731SJamie Gritton return (path2); 42530304c731SJamie Gritton if (path2[len1] == '\0') 42540304c731SJamie Gritton return "/"; 42550304c731SJamie Gritton if (path2[len1] == '/') 42560304c731SJamie Gritton return (path2 + len1); 42570304c731SJamie Gritton return (path2); 42580304c731SJamie Gritton } 42590304c731SJamie Gritton 42600304c731SJamie Gritton /* 42610304c731SJamie Gritton * Jail-related sysctls. 42620304c731SJamie Gritton */ 42637029da5cSPawel Biernacki static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 42640304c731SJamie Gritton "Jails"); 42650304c731SJamie Gritton 4266eb8dcdeaSGleb Smirnoff #if defined(INET) || defined(INET6) 4267eb8dcdeaSGleb Smirnoff /* 4268eb8dcdeaSGleb Smirnoff * Copy address array to memory that would be then SYSCTL_OUT-ed. 4269eb8dcdeaSGleb Smirnoff * sysctl_jail_list() helper. 4270eb8dcdeaSGleb Smirnoff */ 4271eb8dcdeaSGleb Smirnoff static void 4272eb8dcdeaSGleb Smirnoff prison_ip_copyout(struct prison *pr, const pr_family_t af, void **out, int *len) 4273eb8dcdeaSGleb Smirnoff { 42742c33b456SZhenlei Huang const struct prison_ip *pip; 4275eb8dcdeaSGleb Smirnoff const size_t size = pr_families[af].size; 4276eb8dcdeaSGleb Smirnoff 4277eb8dcdeaSGleb Smirnoff again: 4278eb8dcdeaSGleb Smirnoff mtx_assert(&pr->pr_mtx, MA_OWNED); 42792c33b456SZhenlei Huang if ((pip = pr->pr_addrs[af]) != NULL) { 42802c33b456SZhenlei Huang if (*len < pip->ips) { 42812c33b456SZhenlei Huang *len = pip->ips; 4282eb8dcdeaSGleb Smirnoff mtx_unlock(&pr->pr_mtx); 4283eb8dcdeaSGleb Smirnoff *out = realloc(*out, *len * size, M_TEMP, M_WAITOK); 4284eb8dcdeaSGleb Smirnoff mtx_lock(&pr->pr_mtx); 4285eb8dcdeaSGleb Smirnoff goto again; 4286eb8dcdeaSGleb Smirnoff } 42872c33b456SZhenlei Huang bcopy(pip->pr_ip, *out, pip->ips * size); 4288eb8dcdeaSGleb Smirnoff } 4289eb8dcdeaSGleb Smirnoff } 4290eb8dcdeaSGleb Smirnoff #endif 4291eb8dcdeaSGleb Smirnoff 4292fd7a8150SMike Barcroft static int 4293fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS) 4294fd7a8150SMike Barcroft { 4295b38ff370SJamie Gritton struct xprison *xp; 42960304c731SJamie Gritton struct prison *pr, *cpr; 4297b38ff370SJamie Gritton #ifdef INET 4298b38ff370SJamie Gritton struct in_addr *ip4 = NULL; 4299b38ff370SJamie Gritton int ip4s = 0; 4300b38ff370SJamie Gritton #endif 4301b38ff370SJamie Gritton #ifdef INET6 43023beefaedSColin Percival struct in6_addr *ip6 = NULL; 4303b38ff370SJamie Gritton int ip6s = 0; 4304b38ff370SJamie Gritton #endif 43050304c731SJamie Gritton int descend, error; 4306fd7a8150SMike Barcroft 4307b38ff370SJamie Gritton xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK); 43080304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 4309b38ff370SJamie Gritton error = 0; 4310dc68a633SPawel Jakub Dawidek sx_slock(&allprison_lock); 43110304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 43120304c731SJamie Gritton mtx_lock(&cpr->pr_mtx); 4313413628a7SBjoern A. Zeeb #ifdef INET 4314eb8dcdeaSGleb Smirnoff prison_ip_copyout(cpr, PR_INET, (void **)&ip4, &ip4s); 4315413628a7SBjoern A. Zeeb #endif 4316413628a7SBjoern A. Zeeb #ifdef INET6 4317eb8dcdeaSGleb Smirnoff prison_ip_copyout(cpr, PR_INET6, (void **)&ip6, &ip6s); 4318b38ff370SJamie Gritton #endif 4319b38ff370SJamie Gritton bzero(xp, sizeof(*xp)); 4320fd7a8150SMike Barcroft xp->pr_version = XPRISON_VERSION; 43210304c731SJamie Gritton xp->pr_id = cpr->pr_id; 43221158508aSJamie Gritton xp->pr_state = cpr->pr_state; 43230304c731SJamie Gritton strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path)); 4324c1f19219SJamie Gritton strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host)); 43250304c731SJamie Gritton strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name)); 4326413628a7SBjoern A. Zeeb #ifdef INET 4327eb8dcdeaSGleb Smirnoff xp->pr_ip4s = ip4s; 4328413628a7SBjoern A. Zeeb #endif 4329413628a7SBjoern A. Zeeb #ifdef INET6 4330eb8dcdeaSGleb Smirnoff xp->pr_ip6s = ip6s; 4331413628a7SBjoern A. Zeeb #endif 43320304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 4333b38ff370SJamie Gritton error = SYSCTL_OUT(req, xp, sizeof(*xp)); 4334b38ff370SJamie Gritton if (error) 4335b38ff370SJamie Gritton break; 4336413628a7SBjoern A. Zeeb #ifdef INET 4337b38ff370SJamie Gritton if (xp->pr_ip4s > 0) { 4338b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip4, 4339b38ff370SJamie Gritton xp->pr_ip4s * sizeof(struct in_addr)); 4340b38ff370SJamie Gritton if (error) 4341b38ff370SJamie Gritton break; 4342413628a7SBjoern A. Zeeb } 4343413628a7SBjoern A. Zeeb #endif 4344413628a7SBjoern A. Zeeb #ifdef INET6 4345b38ff370SJamie Gritton if (xp->pr_ip6s > 0) { 4346b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip6, 4347b38ff370SJamie Gritton xp->pr_ip6s * sizeof(struct in6_addr)); 4348b38ff370SJamie Gritton if (error) 4349b38ff370SJamie Gritton break; 4350413628a7SBjoern A. Zeeb } 4351413628a7SBjoern A. Zeeb #endif 4352fd7a8150SMike Barcroft } 4353dc68a633SPawel Jakub Dawidek sx_sunlock(&allprison_lock); 4354b38ff370SJamie Gritton free(xp, M_TEMP); 4355b38ff370SJamie Gritton #ifdef INET 4356b38ff370SJamie Gritton free(ip4, M_TEMP); 4357b38ff370SJamie Gritton #endif 4358b38ff370SJamie Gritton #ifdef INET6 4359b38ff370SJamie Gritton free(ip6, M_TEMP); 4360b38ff370SJamie Gritton #endif 4361fd7a8150SMike Barcroft return (error); 4362fd7a8150SMike Barcroft } 4363fd7a8150SMike Barcroft 4364f3b86a5fSEd Schouten SYSCTL_OID(_security_jail, OID_AUTO, list, 4365f3b86a5fSEd Schouten CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4366f3b86a5fSEd Schouten sysctl_jail_list, "S", "List of active jails"); 4367461167c2SPawel Jakub Dawidek 4368461167c2SPawel Jakub Dawidek static int 4369461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS) 4370461167c2SPawel Jakub Dawidek { 4371461167c2SPawel Jakub Dawidek int error, injail; 4372461167c2SPawel Jakub Dawidek 4373461167c2SPawel Jakub Dawidek injail = jailed(req->td->td_ucred); 4374461167c2SPawel Jakub Dawidek error = SYSCTL_OUT(req, &injail, sizeof(injail)); 4375461167c2SPawel Jakub Dawidek 4376461167c2SPawel Jakub Dawidek return (error); 4377461167c2SPawel Jakub Dawidek } 43780304c731SJamie Gritton 4379f3b86a5fSEd Schouten SYSCTL_PROC(_security_jail, OID_AUTO, jailed, 4380f3b86a5fSEd Schouten CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4381f3b86a5fSEd Schouten sysctl_jail_jailed, "I", "Process in jail?"); 4382413628a7SBjoern A. Zeeb 4383761d2bb5SJamie Gritton static int 4384761d2bb5SJamie Gritton sysctl_jail_vnet(SYSCTL_HANDLER_ARGS) 4385761d2bb5SJamie Gritton { 4386761d2bb5SJamie Gritton int error, havevnet; 4387761d2bb5SJamie Gritton #ifdef VIMAGE 4388761d2bb5SJamie Gritton struct ucred *cred = req->td->td_ucred; 4389761d2bb5SJamie Gritton 4390761d2bb5SJamie Gritton havevnet = jailed(cred) && prison_owns_vnet(cred); 4391761d2bb5SJamie Gritton #else 4392761d2bb5SJamie Gritton havevnet = 0; 4393761d2bb5SJamie Gritton #endif 4394761d2bb5SJamie Gritton error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet)); 4395761d2bb5SJamie Gritton 4396761d2bb5SJamie Gritton return (error); 4397761d2bb5SJamie Gritton } 4398761d2bb5SJamie Gritton 4399761d2bb5SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, vnet, 4400761d2bb5SJamie Gritton CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4401bb8f1623SBjoern A. Zeeb sysctl_jail_vnet, "I", "Jail owns vnet?"); 4402761d2bb5SJamie Gritton 44030304c731SJamie Gritton #if defined(INET) || defined(INET6) 4404e92e0574SJamie Gritton SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW, 44050304c731SJamie Gritton &jail_max_af_ips, 0, 4406c542c43eSJamie Gritton "Number of IP addresses a jail may have at most per address family (deprecated)"); 44070304c731SJamie Gritton #endif 44080304c731SJamie Gritton 44090304c731SJamie Gritton /* 4410c542c43eSJamie Gritton * Default parameters for jail(2) compatibility. For historical reasons, 4411c542c43eSJamie Gritton * the sysctl names have varying similarity to the parameter names. Prisons 4412c542c43eSJamie Gritton * just see their own parameters, and can't change them. 44130304c731SJamie Gritton */ 44140304c731SJamie Gritton static int 44150304c731SJamie Gritton sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS) 44160304c731SJamie Gritton { 44170fe74ae6SJamie Gritton int error, i; 44180304c731SJamie Gritton 44190304c731SJamie Gritton /* Get the current flag value, and convert it to a boolean. */ 44200fe74ae6SJamie Gritton if (req->td->td_ucred->cr_prison == &prison0) { 44210fe74ae6SJamie Gritton mtx_lock(&prison0.pr_mtx); 44220fe74ae6SJamie Gritton i = (jail_default_allow & arg2) != 0; 44230fe74ae6SJamie Gritton mtx_unlock(&prison0.pr_mtx); 44240fe74ae6SJamie Gritton } else 44250fe74ae6SJamie Gritton i = prison_allow(req->td->td_ucred, arg2); 44260fe74ae6SJamie Gritton 44270304c731SJamie Gritton if (arg1 != NULL) 44280304c731SJamie Gritton i = !i; 44290304c731SJamie Gritton error = sysctl_handle_int(oidp, &i, 0, req); 4430c542c43eSJamie Gritton if (error || !req->newptr) 44310304c731SJamie Gritton return (error); 44320304c731SJamie Gritton i = i ? arg2 : 0; 44330304c731SJamie Gritton if (arg1 != NULL) 44340304c731SJamie Gritton i ^= arg2; 44350304c731SJamie Gritton /* 44360304c731SJamie Gritton * The sysctls don't have CTLFLAGS_PRISON, so assume prison0 44370304c731SJamie Gritton * for writing. 44380304c731SJamie Gritton */ 44390304c731SJamie Gritton mtx_lock(&prison0.pr_mtx); 44400304c731SJamie Gritton jail_default_allow = (jail_default_allow & ~arg2) | i; 44410304c731SJamie Gritton mtx_unlock(&prison0.pr_mtx); 44420304c731SJamie Gritton return (0); 44430304c731SJamie Gritton } 44440304c731SJamie Gritton 44450304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed, 4446c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44470304c731SJamie Gritton NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I", 4448c542c43eSJamie Gritton "Processes in jail can set their hostnames (deprecated)"); 44490304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only, 4450c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44510304c731SJamie Gritton (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I", 4452c542c43eSJamie Gritton "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)"); 44530304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed, 4454c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44550304c731SJamie Gritton NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I", 4456c542c43eSJamie Gritton "Processes in jail can use System V IPC primitives (deprecated)"); 44570304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets, 4458c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44590304c731SJamie Gritton NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I", 4460c542c43eSJamie Gritton "Prison root can create raw sockets (deprecated)"); 44610304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed, 4462c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44630304c731SJamie Gritton NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I", 4464c542c43eSJamie Gritton "Processes in jail can alter system file flags (deprecated)"); 44650304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed, 4466c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44670304c731SJamie Gritton NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I", 4468c542c43eSJamie Gritton "Processes in jail can mount/unmount jail-friendly file systems (deprecated)"); 44699fd97868SBaptiste Daroussin SYSCTL_PROC(_security_jail, OID_AUTO, mlock_allowed, 44709fd97868SBaptiste Daroussin CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44719fd97868SBaptiste Daroussin NULL, PR_ALLOW_MLOCK, sysctl_jail_default_allow, "I", 44729fd97868SBaptiste Daroussin "Processes in jail can lock/unlock physical pages in memory"); 44730304c731SJamie Gritton 44740304c731SJamie Gritton static int 44750304c731SJamie Gritton sysctl_jail_default_level(SYSCTL_HANDLER_ARGS) 44760304c731SJamie Gritton { 44770304c731SJamie Gritton struct prison *pr; 44780304c731SJamie Gritton int level, error; 44790304c731SJamie Gritton 44800304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 44810304c731SJamie Gritton level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2); 44820304c731SJamie Gritton error = sysctl_handle_int(oidp, &level, 0, req); 4483c542c43eSJamie Gritton if (error || !req->newptr) 44840304c731SJamie Gritton return (error); 44850304c731SJamie Gritton *(int *)arg1 = level; 44860304c731SJamie Gritton return (0); 44870304c731SJamie Gritton } 44880304c731SJamie Gritton 44890304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs, 4490c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4491c542c43eSJamie Gritton &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs), 44920304c731SJamie Gritton sysctl_jail_default_level, "I", 4493c542c43eSJamie Gritton "Processes in jail cannot see all mounted file systems (deprecated)"); 4494c542c43eSJamie Gritton 44950cc207a6SMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset, 4496c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 4497c542c43eSJamie Gritton &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum), 44980cc207a6SMartin Matuska sysctl_jail_default_level, "I", 4499c542c43eSJamie Gritton "Ruleset for the devfs filesystem in jail (deprecated)"); 45000cc207a6SMartin Matuska 4501ab0841bdSJamie Gritton SYSCTL_NODE(_security_jail, OID_AUTO, children, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 4502ab0841bdSJamie Gritton "Limits and stats of child jails"); 4503ab0841bdSJamie Gritton 4504ab0841bdSJamie Gritton static int 4505ab0841bdSJamie Gritton sysctl_jail_children(SYSCTL_HANDLER_ARGS) 4506ab0841bdSJamie Gritton { 4507ab0841bdSJamie Gritton struct prison *pr; 4508ab0841bdSJamie Gritton int i; 4509ab0841bdSJamie Gritton 4510ab0841bdSJamie Gritton pr = req->td->td_ucred->cr_prison; 4511ab0841bdSJamie Gritton 4512ab0841bdSJamie Gritton switch (oidp->oid_kind & CTLTYPE) { 4513ab0841bdSJamie Gritton case CTLTYPE_INT: 4514ab0841bdSJamie Gritton i = *(int *)((char *)pr + arg2); 4515ab0841bdSJamie Gritton return (SYSCTL_OUT(req, &i, sizeof(i))); 4516ab0841bdSJamie Gritton } 4517ab0841bdSJamie Gritton 4518ab0841bdSJamie Gritton return (0); 4519ab0841bdSJamie Gritton } 4520ab0841bdSJamie Gritton 4521ab0841bdSJamie Gritton SYSCTL_PROC(_security_jail_children, OID_AUTO, max, 4522ab0841bdSJamie Gritton CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 4523ab0841bdSJamie Gritton NULL, offsetof(struct prison, pr_childmax), sysctl_jail_children, 4524ab0841bdSJamie Gritton "I", "Maximum number of child jails"); 4525ab0841bdSJamie Gritton SYSCTL_PROC(_security_jail_children, OID_AUTO, cur, 4526ab0841bdSJamie Gritton CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 4527ab0841bdSJamie Gritton NULL, offsetof(struct prison, pr_childcount), sysctl_jail_children, 4528ab0841bdSJamie Gritton "I", "Current number of child jails"); 4529ab0841bdSJamie Gritton 45300304c731SJamie Gritton /* 45310304c731SJamie Gritton * Nodes to describe jail parameters. Maximum length of string parameters 45320304c731SJamie Gritton * is returned in the string itself, and the other parameters exist merely 45330304c731SJamie Gritton * to make themselves and their types known. 45340304c731SJamie Gritton */ 45357029da5cSPawel Biernacki SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 45360304c731SJamie Gritton "Jail parameters"); 45370304c731SJamie Gritton 45380304c731SJamie Gritton int 45390304c731SJamie Gritton sysctl_jail_param(SYSCTL_HANDLER_ARGS) 45400304c731SJamie Gritton { 45410304c731SJamie Gritton int i; 45420304c731SJamie Gritton long l; 45430304c731SJamie Gritton size_t s; 45440304c731SJamie Gritton char numbuf[12]; 45450304c731SJamie Gritton 45460304c731SJamie Gritton switch (oidp->oid_kind & CTLTYPE) 45470304c731SJamie Gritton { 45480304c731SJamie Gritton case CTLTYPE_LONG: 45490304c731SJamie Gritton case CTLTYPE_ULONG: 45500304c731SJamie Gritton l = 0; 45510304c731SJamie Gritton #ifdef SCTL_MASK32 45520304c731SJamie Gritton if (!(req->flags & SCTL_MASK32)) 45530304c731SJamie Gritton #endif 45540304c731SJamie Gritton return (SYSCTL_OUT(req, &l, sizeof(l))); 45550304c731SJamie Gritton case CTLTYPE_INT: 45560304c731SJamie Gritton case CTLTYPE_UINT: 45570304c731SJamie Gritton i = 0; 45580304c731SJamie Gritton return (SYSCTL_OUT(req, &i, sizeof(i))); 45590304c731SJamie Gritton case CTLTYPE_STRING: 4560e4cd31ddSJeff Roberson snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2); 45610304c731SJamie Gritton return 45620304c731SJamie Gritton (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req)); 45630304c731SJamie Gritton case CTLTYPE_STRUCT: 45640304c731SJamie Gritton s = (size_t)arg2; 45650304c731SJamie Gritton return (SYSCTL_OUT(req, &s, sizeof(s))); 45660304c731SJamie Gritton } 45670304c731SJamie Gritton return (0); 45680304c731SJamie Gritton } 45690304c731SJamie Gritton 4570b96bd95bSIan Lepore /* 4571b96bd95bSIan Lepore * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at 4572b96bd95bSIan Lepore * jail creation time but cannot be changed in an existing jail. 4573b96bd95bSIan Lepore */ 45740304c731SJamie Gritton SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID"); 45750304c731SJamie Gritton SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID"); 45760304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name"); 45770304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path"); 45780304c731SJamie Gritton SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW, 45790304c731SJamie Gritton "I", "Jail secure level"); 4580b96bd95bSIan Lepore SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I", 4581b96bd95bSIan Lepore "Jail value for kern.osreldate and uname -K"); 4582b96bd95bSIan Lepore SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN, 4583b96bd95bSIan Lepore "Jail value for kern.osrelease and uname -r"); 45840304c731SJamie Gritton SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW, 45850304c731SJamie Gritton "I", "Jail cannot see all mounted file systems"); 45860cc207a6SMartin Matuska SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW, 45870cc207a6SMartin Matuska "I", "Ruleset for in-jail devfs mounts"); 45880304c731SJamie Gritton SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW, 45890304c731SJamie Gritton "B", "Jail persistence"); 4590679e1390SJamie Gritton #ifdef VIMAGE 4591679e1390SJamie Gritton SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN, 45927cbf7213SJamie Gritton "E,jailsys", "Virtual network stack"); 4593679e1390SJamie Gritton #endif 45940304c731SJamie Gritton SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD, 45950304c731SJamie Gritton "B", "Jail is in the process of shutting down"); 45960304c731SJamie Gritton 4597b97457e2SJamie Gritton SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails"); 4598b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD, 4599b97457e2SJamie Gritton "I", "Current number of child jails"); 4600b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW, 4601b97457e2SJamie Gritton "I", "Maximum number of child jails"); 4602b97457e2SJamie Gritton 46037cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info"); 46040304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN, 46050304c731SJamie Gritton "Jail hostname"); 460676ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN, 460776ca6f88SJamie Gritton "Jail NIS domainname"); 460876ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN, 460976ca6f88SJamie Gritton "Jail host UUID"); 461076ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW, 461176ca6f88SJamie Gritton "LU", "Jail host ID"); 46120304c731SJamie Gritton 46130304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset"); 46140304c731SJamie Gritton SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID"); 46150304c731SJamie Gritton 46160304c731SJamie Gritton #ifdef INET 46172b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN, 46182b0d6f81SJamie Gritton "Jail IPv4 address virtualization"); 46190304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr), 46200304c731SJamie Gritton "S,in_addr,a", "Jail IPv4 addresses"); 4621592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4622592bcae8SBjoern A. Zeeb "B", "Do (not) use IPv4 source address selection rather than the " 4623592bcae8SBjoern A. Zeeb "primary jail IPv4 address."); 46240304c731SJamie Gritton #endif 46250304c731SJamie Gritton #ifdef INET6 46262b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN, 46272b0d6f81SJamie Gritton "Jail IPv6 address virtualization"); 46280304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr), 46290304c731SJamie Gritton "S,in6_addr,a", "Jail IPv6 addresses"); 4630592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4631592bcae8SBjoern A. Zeeb "B", "Do (not) use IPv6 source address selection rather than the " 4632592bcae8SBjoern A. Zeeb "primary jail IPv6 address."); 46330304c731SJamie Gritton #endif 46340304c731SJamie Gritton 46350304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags"); 46360304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW, 46370304c731SJamie Gritton "B", "Jail may set hostname"); 46380304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW, 46390304c731SJamie Gritton "B", "Jail may use SYSV IPC"); 46400304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW, 46410304c731SJamie Gritton "B", "Jail may create raw sockets"); 46420304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW, 46430304c731SJamie Gritton "B", "Jail may alter system file flags"); 46440304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW, 46450304c731SJamie Gritton "B", "Jail may set file quotas"); 46460304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW, 46470304c731SJamie Gritton "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route"); 4648ccd6ac9fSAntoine Brodin SYSCTL_JAIL_PARAM(_allow, mlock, CTLTYPE_INT | CTLFLAG_RW, 4649ccd6ac9fSAntoine Brodin "B", "Jail may lock (unlock) physical pages in memory"); 4650e28f9b7dSAllan Jude SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW, 4651e28f9b7dSAllan Jude "B", "Jail may bind sockets to reserved ports"); 4652b19d66fdSJamie Gritton SYSCTL_JAIL_PARAM(_allow, read_msgbuf, CTLTYPE_INT | CTLFLAG_RW, 4653b19d66fdSJamie Gritton "B", "Jail may read the kernel message buffer"); 4654b3079544SJamie Gritton SYSCTL_JAIL_PARAM(_allow, unprivileged_proc_debug, CTLTYPE_INT | CTLFLAG_RW, 4655b3079544SJamie Gritton "B", "Unprivileged processes may use process debugging facilities"); 465605e1e482SMariusz Zaborski SYSCTL_JAIL_PARAM(_allow, suser, CTLTYPE_INT | CTLFLAG_RW, 465705e1e482SMariusz Zaborski "B", "Processes in jail with uid 0 have privilege"); 4658cbbb2203SRick Macklem #ifdef VIMAGE 4659bba7a2e8SRick Macklem SYSCTL_JAIL_PARAM(_allow, nfsd, CTLTYPE_INT | CTLFLAG_RW, 4660bba7a2e8SRick Macklem "B", "Mountd/nfsd may run in the jail"); 4661bba7a2e8SRick Macklem #endif 4662cb48780dSShawn Webb SYSCTL_JAIL_PARAM(_allow, extattr, CTLTYPE_INT | CTLFLAG_RW, 4663cb48780dSShawn Webb "B", "Jail may set system-level filesystem extended attributes"); 4664d3bb35d4SMariusz Zaborski SYSCTL_JAIL_PARAM(_allow, adjtime, CTLTYPE_INT | CTLFLAG_RW, 4665d3bb35d4SMariusz Zaborski "B", "Jail may adjust system time"); 4666d3bb35d4SMariusz Zaborski SYSCTL_JAIL_PARAM(_allow, settime, CTLTYPE_INT | CTLFLAG_RW, 4667d3bb35d4SMariusz Zaborski "B", "Jail may set system time"); 46680304c731SJamie Gritton 4669bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags"); 4670bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW, 4671bf3db8aaSMartin Matuska "B", "Jail may mount/unmount jail-friendly file systems in general"); 46720e5c6bd4SJamie Gritton 46730e5c6bd4SJamie Gritton /* 46740a172404SJamie Gritton * Add a dynamic parameter allow.<name>, or allow.<prefix>.<name>. Return 46750a172404SJamie Gritton * its associated bit in the pr_allow bitmask, or zero if the parameter was 46760a172404SJamie Gritton * not created. 46770e5c6bd4SJamie Gritton */ 46780a172404SJamie Gritton unsigned 46790a172404SJamie Gritton prison_add_allow(const char *prefix, const char *name, const char *prefix_descr, 46800a172404SJamie Gritton const char *descr) 46810e5c6bd4SJamie Gritton { 46820e5c6bd4SJamie Gritton struct bool_flags *bf; 46830a172404SJamie Gritton struct sysctl_oid *parent; 46840a172404SJamie Gritton char *allow_name, *allow_noname, *allowed; 4685c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR 4686c542c43eSJamie Gritton char *descr_deprecated; 4687c542c43eSJamie Gritton #endif 46885d58f959SJamie Gritton u_int allow_flag; 46890e5c6bd4SJamie Gritton 46900a172404SJamie Gritton if (prefix 46910a172404SJamie Gritton ? asprintf(&allow_name, M_PRISON, "allow.%s.%s", prefix, name) 46920a172404SJamie Gritton < 0 || 46930a172404SJamie Gritton asprintf(&allow_noname, M_PRISON, "allow.%s.no%s", prefix, name) 46940a172404SJamie Gritton < 0 46950a172404SJamie Gritton : asprintf(&allow_name, M_PRISON, "allow.%s", name) < 0 || 46960a172404SJamie Gritton asprintf(&allow_noname, M_PRISON, "allow.no%s", name) < 0) { 46970e5c6bd4SJamie Gritton free(allow_name, M_PRISON); 46980a172404SJamie Gritton return 0; 46990e5c6bd4SJamie Gritton } 47000e5c6bd4SJamie Gritton 47010e5c6bd4SJamie Gritton /* 47020a172404SJamie Gritton * See if this parameter has already beed added, i.e. a module was 47030a172404SJamie Gritton * previously loaded/unloaded. 47040e5c6bd4SJamie Gritton */ 47050e5c6bd4SJamie Gritton mtx_lock(&prison0.pr_mtx); 47060e5c6bd4SJamie Gritton for (bf = pr_flag_allow; 47075d58f959SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 47085d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 47090e5c6bd4SJamie Gritton bf++) { 47100e5c6bd4SJamie Gritton if (strcmp(bf->name, allow_name) == 0) { 47110a172404SJamie Gritton allow_flag = bf->flag; 47120e5c6bd4SJamie Gritton goto no_add; 47130e5c6bd4SJamie Gritton } 47140e5c6bd4SJamie Gritton } 47150e5c6bd4SJamie Gritton 47160e5c6bd4SJamie Gritton /* 47175d58f959SJamie Gritton * Find a free bit in pr_allow_all, failing if there are none 47180e5c6bd4SJamie Gritton * (which shouldn't happen as long as we keep track of how many 47190a172404SJamie Gritton * potential dynamic flags exist). 47200e5c6bd4SJamie Gritton */ 47210e5c6bd4SJamie Gritton for (allow_flag = 1;; allow_flag <<= 1) { 47220e5c6bd4SJamie Gritton if (allow_flag == 0) 47230e5c6bd4SJamie Gritton goto no_add; 47245d58f959SJamie Gritton if ((pr_allow_all & allow_flag) == 0) 47250e5c6bd4SJamie Gritton break; 47260e5c6bd4SJamie Gritton } 47270e5c6bd4SJamie Gritton 47285d58f959SJamie Gritton /* Note the parameter in the next open slot in pr_flag_allow. */ 47295d58f959SJamie Gritton for (bf = pr_flag_allow; ; bf++) { 47300e5c6bd4SJamie Gritton if (bf == pr_flag_allow + nitems(pr_flag_allow)) { 47310e5c6bd4SJamie Gritton /* This should never happen, but is not fatal. */ 47320a172404SJamie Gritton allow_flag = 0; 47330e5c6bd4SJamie Gritton goto no_add; 47340e5c6bd4SJamie Gritton } 47355d58f959SJamie Gritton if (atomic_load_int(&bf->flag) == 0) 47365d58f959SJamie Gritton break; 47375d58f959SJamie Gritton } 47380e5c6bd4SJamie Gritton bf->name = allow_name; 47390e5c6bd4SJamie Gritton bf->noname = allow_noname; 47405d58f959SJamie Gritton pr_allow_all |= allow_flag; 47415d58f959SJamie Gritton /* 47425d58f959SJamie Gritton * prison0 always has permission for the new parameter. 47435d58f959SJamie Gritton * Other jails must have it granted to them. 47445d58f959SJamie Gritton */ 47455d58f959SJamie Gritton prison0.pr_allow |= allow_flag; 47465d58f959SJamie Gritton /* The flag indicates a valid entry, so make sure it is set last. */ 47475d58f959SJamie Gritton atomic_store_rel_int(&bf->flag, allow_flag); 47480e5c6bd4SJamie Gritton mtx_unlock(&prison0.pr_mtx); 47490e5c6bd4SJamie Gritton 4750c542c43eSJamie Gritton /* 47514771011bSGordon Bergling * Create sysctls for the parameter, and the back-compat global 4752c542c43eSJamie Gritton * permission. 4753c542c43eSJamie Gritton */ 47540a172404SJamie Gritton parent = prefix 47550a172404SJamie Gritton ? SYSCTL_ADD_NODE(NULL, 47560a172404SJamie Gritton SYSCTL_CHILDREN(&sysctl___security_jail_param_allow), 47577029da5cSPawel Biernacki OID_AUTO, prefix, CTLFLAG_MPSAFE, 0, prefix_descr) 47580a172404SJamie Gritton : &sysctl___security_jail_param_allow; 47590a172404SJamie Gritton (void)SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(parent), OID_AUTO, 47600a172404SJamie Gritton name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 47610e5c6bd4SJamie Gritton NULL, 0, sysctl_jail_param, "B", descr); 47620a172404SJamie Gritton if ((prefix 47630a172404SJamie Gritton ? asprintf(&allowed, M_TEMP, "%s_%s_allowed", prefix, name) 47640a172404SJamie Gritton : asprintf(&allowed, M_TEMP, "%s_allowed", name)) >= 0) { 4765c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR 4766c542c43eSJamie Gritton (void)asprintf(&descr_deprecated, M_TEMP, "%s (deprecated)", 4767c542c43eSJamie Gritton descr); 4768c542c43eSJamie Gritton #endif 47690e5c6bd4SJamie Gritton (void)SYSCTL_ADD_PROC(NULL, 47700a172404SJamie Gritton SYSCTL_CHILDREN(&sysctl___security_jail), OID_AUTO, allowed, 4771c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, allow_flag, 4772c542c43eSJamie Gritton sysctl_jail_default_allow, "I", descr_deprecated); 4773c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR 4774c542c43eSJamie Gritton free(descr_deprecated, M_TEMP); 4775c542c43eSJamie Gritton #endif 47760a172404SJamie Gritton free(allowed, M_TEMP); 47770e5c6bd4SJamie Gritton } 47780a172404SJamie Gritton return allow_flag; 47790e5c6bd4SJamie Gritton 47800e5c6bd4SJamie Gritton no_add: 47810e5c6bd4SJamie Gritton mtx_unlock(&prison0.pr_mtx); 47820e5c6bd4SJamie Gritton free(allow_name, M_PRISON); 47830e5c6bd4SJamie Gritton free(allow_noname, M_PRISON); 47840a172404SJamie Gritton return allow_flag; 47850a172404SJamie Gritton } 47860a172404SJamie Gritton 47870a172404SJamie Gritton /* 47880a172404SJamie Gritton * The VFS system will register jail-aware filesystems here. They each get 47890a172404SJamie Gritton * a parameter allow.mount.xxxfs and a flag to check when a jailed user 47900a172404SJamie Gritton * attempts to mount. 47910a172404SJamie Gritton */ 47920a172404SJamie Gritton void 47930a172404SJamie Gritton prison_add_vfs(struct vfsconf *vfsp) 47940a172404SJamie Gritton { 47950a172404SJamie Gritton #ifdef NO_SYSCTL_DESCR 47960a172404SJamie Gritton 47970a172404SJamie Gritton vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name, 47980a172404SJamie Gritton NULL, NULL); 47990a172404SJamie Gritton #else 48000a172404SJamie Gritton char *descr; 48010a172404SJamie Gritton 48020a172404SJamie Gritton (void)asprintf(&descr, M_TEMP, "Jail may mount the %s file system", 48030a172404SJamie Gritton vfsp->vfc_name); 48040a172404SJamie Gritton vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name, 48050a172404SJamie Gritton NULL, descr); 48060a172404SJamie Gritton free(descr, M_TEMP); 48070a172404SJamie Gritton #endif 48080e5c6bd4SJamie Gritton } 4809bf3db8aaSMartin Matuska 48104b5c9cf6SEdward Tomasz Napierala #ifdef RACCT 4811097055e2SEdward Tomasz Napierala void 4812097055e2SEdward Tomasz Napierala prison_racct_foreach(void (*callback)(struct racct *racct, 481315db3c07SEdward Tomasz Napierala void *arg2, void *arg3), void (*pre)(void), void (*post)(void), 481415db3c07SEdward Tomasz Napierala void *arg2, void *arg3) 4815097055e2SEdward Tomasz Napierala { 4816a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4817097055e2SEdward Tomasz Napierala 48184b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 48194b5c9cf6SEdward Tomasz Napierala 4820097055e2SEdward Tomasz Napierala sx_slock(&allprison_lock); 482115db3c07SEdward Tomasz Napierala if (pre != NULL) 482215db3c07SEdward Tomasz Napierala (pre)(); 4823a7ad07bfSEdward Tomasz Napierala LIST_FOREACH(prr, &allprison_racct, prr_next) 4824a7ad07bfSEdward Tomasz Napierala (callback)(prr->prr_racct, arg2, arg3); 482515db3c07SEdward Tomasz Napierala if (post != NULL) 482615db3c07SEdward Tomasz Napierala (post)(); 4827097055e2SEdward Tomasz Napierala sx_sunlock(&allprison_lock); 4828097055e2SEdward Tomasz Napierala } 48290304c731SJamie Gritton 4830a7ad07bfSEdward Tomasz Napierala static struct prison_racct * 4831a7ad07bfSEdward Tomasz Napierala prison_racct_find_locked(const char *name) 4832a7ad07bfSEdward Tomasz Napierala { 4833a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4834a7ad07bfSEdward Tomasz Napierala 48354b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4836a7ad07bfSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4837a7ad07bfSEdward Tomasz Napierala 4838a7ad07bfSEdward Tomasz Napierala if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN) 4839a7ad07bfSEdward Tomasz Napierala return (NULL); 4840a7ad07bfSEdward Tomasz Napierala 4841a7ad07bfSEdward Tomasz Napierala LIST_FOREACH(prr, &allprison_racct, prr_next) { 4842a7ad07bfSEdward Tomasz Napierala if (strcmp(name, prr->prr_name) != 0) 4843a7ad07bfSEdward Tomasz Napierala continue; 4844a7ad07bfSEdward Tomasz Napierala 4845a7ad07bfSEdward Tomasz Napierala /* Found prison_racct with a matching name? */ 4846a7ad07bfSEdward Tomasz Napierala prison_racct_hold(prr); 4847a7ad07bfSEdward Tomasz Napierala return (prr); 4848a7ad07bfSEdward Tomasz Napierala } 4849a7ad07bfSEdward Tomasz Napierala 4850a7ad07bfSEdward Tomasz Napierala /* Add new prison_racct. */ 4851a7ad07bfSEdward Tomasz Napierala prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK); 4852a7ad07bfSEdward Tomasz Napierala racct_create(&prr->prr_racct); 4853a7ad07bfSEdward Tomasz Napierala 4854a7ad07bfSEdward Tomasz Napierala strcpy(prr->prr_name, name); 4855a7ad07bfSEdward Tomasz Napierala refcount_init(&prr->prr_refcount, 1); 4856a7ad07bfSEdward Tomasz Napierala LIST_INSERT_HEAD(&allprison_racct, prr, prr_next); 4857a7ad07bfSEdward Tomasz Napierala 4858a7ad07bfSEdward Tomasz Napierala return (prr); 4859a7ad07bfSEdward Tomasz Napierala } 4860a7ad07bfSEdward Tomasz Napierala 4861a7ad07bfSEdward Tomasz Napierala struct prison_racct * 4862a7ad07bfSEdward Tomasz Napierala prison_racct_find(const char *name) 4863a7ad07bfSEdward Tomasz Napierala { 4864a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4865a7ad07bfSEdward Tomasz Napierala 48664b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 48674b5c9cf6SEdward Tomasz Napierala 4868a7ad07bfSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4869a7ad07bfSEdward Tomasz Napierala prr = prison_racct_find_locked(name); 4870a7ad07bfSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4871a7ad07bfSEdward Tomasz Napierala return (prr); 4872a7ad07bfSEdward Tomasz Napierala } 4873a7ad07bfSEdward Tomasz Napierala 4874a7ad07bfSEdward Tomasz Napierala void 4875a7ad07bfSEdward Tomasz Napierala prison_racct_hold(struct prison_racct *prr) 4876a7ad07bfSEdward Tomasz Napierala { 4877a7ad07bfSEdward Tomasz Napierala 48784b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 48794b5c9cf6SEdward Tomasz Napierala 4880a7ad07bfSEdward Tomasz Napierala refcount_acquire(&prr->prr_refcount); 4881a7ad07bfSEdward Tomasz Napierala } 4882a7ad07bfSEdward Tomasz Napierala 4883c34bbd2aSEdward Tomasz Napierala static void 4884c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(struct prison_racct *prr) 4885c34bbd2aSEdward Tomasz Napierala { 4886c34bbd2aSEdward Tomasz Napierala 48874b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4888c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4889c34bbd2aSEdward Tomasz Napierala 4890c34bbd2aSEdward Tomasz Napierala if (refcount_release(&prr->prr_refcount)) { 4891c34bbd2aSEdward Tomasz Napierala racct_destroy(&prr->prr_racct); 4892c34bbd2aSEdward Tomasz Napierala LIST_REMOVE(prr, prr_next); 4893c34bbd2aSEdward Tomasz Napierala free(prr, M_PRISON_RACCT); 4894c34bbd2aSEdward Tomasz Napierala } 4895c34bbd2aSEdward Tomasz Napierala } 4896c34bbd2aSEdward Tomasz Napierala 4897a7ad07bfSEdward Tomasz Napierala void 4898a7ad07bfSEdward Tomasz Napierala prison_racct_free(struct prison_racct *prr) 4899a7ad07bfSEdward Tomasz Napierala { 4900a7ad07bfSEdward Tomasz Napierala 49014b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4902c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_UNLOCKED); 4903c34bbd2aSEdward Tomasz Napierala 49046ff4688bSMateusz Guzik if (refcount_release_if_not_last(&prr->prr_refcount)) 4905a7ad07bfSEdward Tomasz Napierala return; 4906a7ad07bfSEdward Tomasz Napierala 4907a7ad07bfSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4908c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(prr); 4909a7ad07bfSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4910a7ad07bfSEdward Tomasz Napierala } 4911a7ad07bfSEdward Tomasz Napierala 4912a7ad07bfSEdward Tomasz Napierala static void 4913a7ad07bfSEdward Tomasz Napierala prison_racct_attach(struct prison *pr) 4914a7ad07bfSEdward Tomasz Napierala { 4915a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4916a7ad07bfSEdward Tomasz Napierala 49174b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4918c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4919c34bbd2aSEdward Tomasz Napierala 4920a7ad07bfSEdward Tomasz Napierala prr = prison_racct_find_locked(pr->pr_name); 4921a7ad07bfSEdward Tomasz Napierala KASSERT(prr != NULL, ("cannot find prison_racct")); 4922a7ad07bfSEdward Tomasz Napierala 4923a7ad07bfSEdward Tomasz Napierala pr->pr_prison_racct = prr; 4924a7ad07bfSEdward Tomasz Napierala } 4925a7ad07bfSEdward Tomasz Napierala 4926c34bbd2aSEdward Tomasz Napierala /* 4927c34bbd2aSEdward Tomasz Napierala * Handle jail renaming. From the racct point of view, renaming means 4928c34bbd2aSEdward Tomasz Napierala * moving from one prison_racct to another. 4929c34bbd2aSEdward Tomasz Napierala */ 4930c34bbd2aSEdward Tomasz Napierala static void 4931c34bbd2aSEdward Tomasz Napierala prison_racct_modify(struct prison *pr) 4932c34bbd2aSEdward Tomasz Napierala { 4933dbadb015SKonstantin Belousov #ifdef RCTL 4934c34bbd2aSEdward Tomasz Napierala struct proc *p; 4935c34bbd2aSEdward Tomasz Napierala struct ucred *cred; 4936dbadb015SKonstantin Belousov #endif 4937c34bbd2aSEdward Tomasz Napierala struct prison_racct *oldprr; 4938c34bbd2aSEdward Tomasz Napierala 49394b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 49404b5c9cf6SEdward Tomasz Napierala 4941c34bbd2aSEdward Tomasz Napierala sx_slock(&allproc_lock); 4942c34bbd2aSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4943c34bbd2aSEdward Tomasz Napierala 4944e30345e7SEdward Tomasz Napierala if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) { 4945e30345e7SEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4946e30345e7SEdward Tomasz Napierala sx_sunlock(&allproc_lock); 4947c34bbd2aSEdward Tomasz Napierala return; 4948e30345e7SEdward Tomasz Napierala } 4949c34bbd2aSEdward Tomasz Napierala 4950c34bbd2aSEdward Tomasz Napierala oldprr = pr->pr_prison_racct; 4951c34bbd2aSEdward Tomasz Napierala pr->pr_prison_racct = NULL; 4952c34bbd2aSEdward Tomasz Napierala 4953c34bbd2aSEdward Tomasz Napierala prison_racct_attach(pr); 4954c34bbd2aSEdward Tomasz Napierala 4955c34bbd2aSEdward Tomasz Napierala /* 4956c34bbd2aSEdward Tomasz Napierala * Move resource utilisation records. 4957c34bbd2aSEdward Tomasz Napierala */ 4958c34bbd2aSEdward Tomasz Napierala racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct); 4959c34bbd2aSEdward Tomasz Napierala 4960f87beb93SAndriy Gapon #ifdef RCTL 4961c34bbd2aSEdward Tomasz Napierala /* 4962c34bbd2aSEdward Tomasz Napierala * Force rctl to reattach rules to processes. 4963c34bbd2aSEdward Tomasz Napierala */ 4964c34bbd2aSEdward Tomasz Napierala FOREACH_PROC_IN_SYSTEM(p) { 4965c34bbd2aSEdward Tomasz Napierala PROC_LOCK(p); 4966c34bbd2aSEdward Tomasz Napierala cred = crhold(p->p_ucred); 4967c34bbd2aSEdward Tomasz Napierala PROC_UNLOCK(p); 4968f87beb93SAndriy Gapon rctl_proc_ucred_changed(p, cred); 4969c34bbd2aSEdward Tomasz Napierala crfree(cred); 4970c34bbd2aSEdward Tomasz Napierala } 4971f87beb93SAndriy Gapon #endif 4972c34bbd2aSEdward Tomasz Napierala 4973c34bbd2aSEdward Tomasz Napierala sx_sunlock(&allproc_lock); 4974c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(oldprr); 4975c34bbd2aSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4976c34bbd2aSEdward Tomasz Napierala } 4977c34bbd2aSEdward Tomasz Napierala 4978a7ad07bfSEdward Tomasz Napierala static void 4979a7ad07bfSEdward Tomasz Napierala prison_racct_detach(struct prison *pr) 4980a7ad07bfSEdward Tomasz Napierala { 4981c34bbd2aSEdward Tomasz Napierala 49824b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4983c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_UNLOCKED); 4984c34bbd2aSEdward Tomasz Napierala 4985af3c786cSMateusz Guzik if (pr->pr_prison_racct == NULL) 4986af3c786cSMateusz Guzik return; 4987a7ad07bfSEdward Tomasz Napierala prison_racct_free(pr->pr_prison_racct); 4988a7ad07bfSEdward Tomasz Napierala pr->pr_prison_racct = NULL; 4989a7ad07bfSEdward Tomasz Napierala } 4990a7ad07bfSEdward Tomasz Napierala #endif /* RACCT */ 4991a7ad07bfSEdward Tomasz Napierala 4992413628a7SBjoern A. Zeeb #ifdef DDB 4993b38ff370SJamie Gritton 4994b38ff370SJamie Gritton static void 4995b38ff370SJamie Gritton db_show_prison(struct prison *pr) 4996413628a7SBjoern A. Zeeb { 4997672756aaSJamie Gritton struct bool_flags *bf; 4998672756aaSJamie Gritton struct jailsys_flags *jsf; 4999b38ff370SJamie Gritton #if defined(INET) || defined(INET6) 5000b38ff370SJamie Gritton int ii; 5001500f82d6SZhenlei Huang struct prison_ip *pip; 5002413628a7SBjoern A. Zeeb #endif 5003672756aaSJamie Gritton unsigned f; 50048144690aSEric van Gyzen #ifdef INET 50058144690aSEric van Gyzen char ip4buf[INET_ADDRSTRLEN]; 50068144690aSEric van Gyzen #endif 5007413628a7SBjoern A. Zeeb #ifdef INET6 5008413628a7SBjoern A. Zeeb char ip6buf[INET6_ADDRSTRLEN]; 5009413628a7SBjoern A. Zeeb #endif 5010413628a7SBjoern A. Zeeb 5011b38ff370SJamie Gritton db_printf("prison %p:\n", pr); 5012b38ff370SJamie Gritton db_printf(" jid = %d\n", pr->pr_id); 5013b38ff370SJamie Gritton db_printf(" name = %s\n", pr->pr_name); 50140304c731SJamie Gritton db_printf(" parent = %p\n", pr->pr_parent); 5015b38ff370SJamie Gritton db_printf(" ref = %d\n", pr->pr_ref); 5016b38ff370SJamie Gritton db_printf(" uref = %d\n", pr->pr_uref); 50171158508aSJamie Gritton db_printf(" state = %s\n", 50181158508aSJamie Gritton pr->pr_state == PRISON_STATE_ALIVE ? "alive" : 50191158508aSJamie Gritton pr->pr_state == PRISON_STATE_DYING ? "dying" : 50201158508aSJamie Gritton "invalid"); 5021b38ff370SJamie Gritton db_printf(" path = %s\n", pr->pr_path); 5022b38ff370SJamie Gritton db_printf(" cpuset = %d\n", pr->pr_cpuset 5023b38ff370SJamie Gritton ? pr->pr_cpuset->cs_id : -1); 5024679e1390SJamie Gritton #ifdef VIMAGE 5025679e1390SJamie Gritton db_printf(" vnet = %p\n", pr->pr_vnet); 5026679e1390SJamie Gritton #endif 5027b38ff370SJamie Gritton db_printf(" root = %p\n", pr->pr_root); 5028b38ff370SJamie Gritton db_printf(" securelevel = %d\n", pr->pr_securelevel); 50290cc207a6SMartin Matuska db_printf(" devfs_rsnum = %d\n", pr->pr_devfs_rsnum); 5030fe0518e9SBjoern A. Zeeb db_printf(" children.max = %d\n", pr->pr_childmax); 5031fe0518e9SBjoern A. Zeeb db_printf(" children.cur = %d\n", pr->pr_childcount); 50320304c731SJamie Gritton db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children)); 50330304c731SJamie Gritton db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling)); 5034fe0518e9SBjoern A. Zeeb db_printf(" flags = 0x%x", pr->pr_flags); 5035672756aaSJamie Gritton for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++) 5036672756aaSJamie Gritton if (pr->pr_flags & bf->flag) 5037672756aaSJamie Gritton db_printf(" %s", bf->name); 5038672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 5039672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 5040672756aaSJamie Gritton jsf++) { 5041672756aaSJamie Gritton f = pr->pr_flags & (jsf->disable | jsf->new); 5042672756aaSJamie Gritton db_printf(" %-16s= %s\n", jsf->name, 5043672756aaSJamie Gritton (f != 0 && f == jsf->disable) ? "disable" 5044672756aaSJamie Gritton : (f == jsf->new) ? "new" 50457cbf7213SJamie Gritton : "inherit"); 50467cbf7213SJamie Gritton } 5047fe0518e9SBjoern A. Zeeb db_printf(" allow = 0x%x", pr->pr_allow); 5048672756aaSJamie Gritton for (bf = pr_flag_allow; 50495d58f959SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 50505d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 5051672756aaSJamie Gritton bf++) 5052672756aaSJamie Gritton if (pr->pr_allow & bf->flag) 5053672756aaSJamie Gritton db_printf(" %s", bf->name); 5054b38ff370SJamie Gritton db_printf("\n"); 50550304c731SJamie Gritton db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs); 5056c1f19219SJamie Gritton db_printf(" host.hostname = %s\n", pr->pr_hostname); 5057c1f19219SJamie Gritton db_printf(" host.domainname = %s\n", pr->pr_domainname); 5058c1f19219SJamie Gritton db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid); 505976ca6f88SJamie Gritton db_printf(" host.hostid = %lu\n", pr->pr_hostid); 5060413628a7SBjoern A. Zeeb #ifdef INET 5061500f82d6SZhenlei Huang if ((pip = pr->pr_addrs[PR_INET]) != NULL) { 5062500f82d6SZhenlei Huang db_printf(" ip4s = %d\n", pip->ips); 5063500f82d6SZhenlei Huang for (ii = 0; ii < pip->ips; ii++) 5064b38ff370SJamie Gritton db_printf(" %s %s\n", 5065fe0518e9SBjoern A. Zeeb ii == 0 ? "ip4.addr =" : " ", 5066eb8dcdeaSGleb Smirnoff inet_ntoa_r( 5067500f82d6SZhenlei Huang *(const struct in_addr *)PR_IP(pip, PR_INET, ii), 5068eb8dcdeaSGleb Smirnoff ip4buf)); 5069eb8dcdeaSGleb Smirnoff } 5070413628a7SBjoern A. Zeeb #endif 5071413628a7SBjoern A. Zeeb #ifdef INET6 5072500f82d6SZhenlei Huang if ((pip = pr->pr_addrs[PR_INET6]) != NULL) { 5073500f82d6SZhenlei Huang db_printf(" ip6s = %d\n", pip->ips); 5074500f82d6SZhenlei Huang for (ii = 0; ii < pip->ips; ii++) 5075b38ff370SJamie Gritton db_printf(" %s %s\n", 5076fe0518e9SBjoern A. Zeeb ii == 0 ? "ip6.addr =" : " ", 5077eb8dcdeaSGleb Smirnoff ip6_sprintf(ip6buf, 5078500f82d6SZhenlei Huang (const struct in6_addr *)PR_IP(pip, PR_INET6, ii))); 5079eb8dcdeaSGleb Smirnoff } 5080b38ff370SJamie Gritton #endif 5081b38ff370SJamie Gritton } 5082b38ff370SJamie Gritton 5083b38ff370SJamie Gritton DB_SHOW_COMMAND(prison, db_show_prison_command) 5084b38ff370SJamie Gritton { 5085b38ff370SJamie Gritton struct prison *pr; 5086b38ff370SJamie Gritton 5087b38ff370SJamie Gritton if (!have_addr) { 50880304c731SJamie Gritton /* 50890304c731SJamie Gritton * Show all prisons in the list, and prison0 which is not 50900304c731SJamie Gritton * listed. 50910304c731SJamie Gritton */ 50920304c731SJamie Gritton db_show_prison(&prison0); 50930304c731SJamie Gritton if (!db_pager_quit) { 5094b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 5095b38ff370SJamie Gritton db_show_prison(pr); 5096413628a7SBjoern A. Zeeb if (db_pager_quit) 5097413628a7SBjoern A. Zeeb break; 5098413628a7SBjoern A. Zeeb } 50990304c731SJamie Gritton } 5100b38ff370SJamie Gritton return; 5101413628a7SBjoern A. Zeeb } 5102b38ff370SJamie Gritton 51030304c731SJamie Gritton if (addr == 0) 51040304c731SJamie Gritton pr = &prison0; 51050304c731SJamie Gritton else { 5106b38ff370SJamie Gritton /* Look for a prison with the ID and with references. */ 5107b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 5108b38ff370SJamie Gritton if (pr->pr_id == addr && pr->pr_ref > 0) 5109b38ff370SJamie Gritton break; 5110b38ff370SJamie Gritton if (pr == NULL) 5111b38ff370SJamie Gritton /* Look again, without requiring a reference. */ 5112b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 5113b38ff370SJamie Gritton if (pr->pr_id == addr) 5114b38ff370SJamie Gritton break; 5115b38ff370SJamie Gritton if (pr == NULL) 5116b38ff370SJamie Gritton /* Assume address points to a valid prison. */ 5117b38ff370SJamie Gritton pr = (struct prison *)addr; 51180304c731SJamie Gritton } 5119b38ff370SJamie Gritton db_show_prison(pr); 5120b38ff370SJamie Gritton } 5121b38ff370SJamie Gritton 5122413628a7SBjoern A. Zeeb #endif /* DDB */ 5123