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 1338cf955f3SMark Johnston /* 1348cf955f3SMark Johnston * Handle jail teardown in a dedicated thread to avoid deadlocks from 1358cf955f3SMark Johnston * vnet_destroy(). 1368cf955f3SMark Johnston */ 1378cf955f3SMark Johnston TASKQUEUE_DEFINE_THREAD(jail_remove); 1388cf955f3SMark 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 16908c75c15dSMark Johnston /* 16918c75c15dSMark Johnston * Allocate a new vnet if specified. 16928c75c15dSMark Johnston * 16938c75c15dSMark Johnston * Set PR_VNET now if so, so that the vnet is disposed of 16948c75c15dSMark Johnston * properly when the jail is destroyed. 16958c75c15dSMark Johnston */ 16968c75c15dSMark Johnston if (pr_flags & PR_VNET) { 16978c75c15dSMark Johnston pr->pr_flags |= PR_VNET; 16988c75c15dSMark Johnston pr->pr_vnet = vnet_alloc(); 16998c75c15dSMark Johnston } else { 17008c75c15dSMark Johnston pr->pr_vnet = ppr->pr_vnet; 17018c75c15dSMark Johnston } 1702679e1390SJamie Gritton #endif 1703b38ff370SJamie Gritton /* 1704b38ff370SJamie Gritton * Allocate a dedicated cpuset for each jail. 17058771ff75SGordon Bergling * Unlike other initial settings, this may return an error. 1706b38ff370SJamie Gritton */ 17070304c731SJamie Gritton error = cpuset_create_root(ppr, &pr->pr_cpuset); 17082a4b2251SJamie Gritton if (error) 17092a4b2251SJamie Gritton goto done_deref; 1710b38ff370SJamie Gritton 1711b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 17122a4b2251SJamie Gritton drflags |= PD_LOCKED; 1713b38ff370SJamie Gritton } else { 17142b0d6f81SJamie Gritton /* 17152b0d6f81SJamie Gritton * Grab a reference for existing prisons, to ensure they 17162b0d6f81SJamie Gritton * continue to exist for the duration of the call. 17172b0d6f81SJamie Gritton */ 17186754ae25SJamie Gritton prison_hold(pr); 17192a4b2251SJamie Gritton drflags |= PD_DEREF; 1720bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6)) 1721bdfc8cc4SJamie Gritton if ((pr->pr_flags & PR_VNET) && 1722bdfc8cc4SJamie Gritton (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 1723bdfc8cc4SJamie Gritton error = EINVAL; 1724bdfc8cc4SJamie Gritton vfs_opterror(opts, 1725bdfc8cc4SJamie Gritton "vnet jails cannot have IP address restrictions"); 17262a4b2251SJamie Gritton goto done_deref; 1727bdfc8cc4SJamie Gritton } 1728bdfc8cc4SJamie Gritton #endif 17292b0d6f81SJamie Gritton #ifdef INET 17302b0d6f81SJamie Gritton if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 17312b0d6f81SJamie Gritton error = EINVAL; 17322b0d6f81SJamie Gritton vfs_opterror(opts, 17332b0d6f81SJamie Gritton "ip4 cannot be changed after creation"); 17342a4b2251SJamie Gritton goto done_deref; 17352b0d6f81SJamie Gritton } 17362b0d6f81SJamie Gritton #endif 17372b0d6f81SJamie Gritton #ifdef INET6 17382b0d6f81SJamie Gritton if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 17392b0d6f81SJamie Gritton error = EINVAL; 17402b0d6f81SJamie Gritton vfs_opterror(opts, 17412b0d6f81SJamie Gritton "ip6 cannot be changed after creation"); 17422a4b2251SJamie Gritton goto done_deref; 17432b0d6f81SJamie Gritton } 17442b0d6f81SJamie Gritton #endif 1745b38ff370SJamie Gritton } 1746b38ff370SJamie Gritton 1747b38ff370SJamie Gritton /* Do final error checking before setting anything. */ 17480304c731SJamie Gritton if (gotslevel) { 17490304c731SJamie Gritton if (slevel < ppr->pr_securelevel) { 17500304c731SJamie Gritton error = EPERM; 17512a4b2251SJamie Gritton goto done_deref; 17520304c731SJamie Gritton } 17530304c731SJamie Gritton } 1754b97457e2SJamie Gritton if (gotchildmax) { 1755b97457e2SJamie Gritton if (childmax >= ppr->pr_childmax) { 1756b97457e2SJamie Gritton error = EPERM; 17572a4b2251SJamie Gritton goto done_deref; 1758b97457e2SJamie Gritton } 1759b97457e2SJamie Gritton } 17600304c731SJamie Gritton if (gotenforce) { 17610304c731SJamie Gritton if (enforce < ppr->pr_enforce_statfs) { 17620304c731SJamie Gritton error = EPERM; 17632a4b2251SJamie Gritton goto done_deref; 17640304c731SJamie Gritton } 17650304c731SJamie Gritton } 17660cc207a6SMartin Matuska if (gotrsnum) { 17670cc207a6SMartin Matuska /* 17680cc207a6SMartin Matuska * devfs_rsnum is a uint16_t 17690cc207a6SMartin Matuska */ 1770bf3db8aaSMartin Matuska if (rsnum < 0 || rsnum > 65535) { 17710cc207a6SMartin Matuska error = EINVAL; 17722a4b2251SJamie Gritton goto done_deref; 17730cc207a6SMartin Matuska } 17740cc207a6SMartin Matuska /* 1775bf3db8aaSMartin Matuska * Nested jails always inherit parent's devfs ruleset 17760cc207a6SMartin Matuska */ 17770cc207a6SMartin Matuska if (jailed(td->td_ucred)) { 17780cc207a6SMartin Matuska if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) { 17790cc207a6SMartin Matuska error = EPERM; 17802a4b2251SJamie Gritton goto done_deref; 1781bf3db8aaSMartin Matuska } else 17820cc207a6SMartin Matuska rsnum = ppr->pr_devfs_rsnum; 17830cc207a6SMartin Matuska } 17840cc207a6SMartin Matuska } 1785b38ff370SJamie Gritton #ifdef INET 17862b0d6f81SJamie Gritton if (ip4s > 0) { 1787eb8dcdeaSGleb Smirnoff if ((ppr->pr_flags & PR_IP4) && 1788eb8dcdeaSGleb Smirnoff !prison_ip_parent_match(ppr->pr_addrs[PR_INET], ip4, 1789eb8dcdeaSGleb Smirnoff PR_INET)) { 17900304c731SJamie Gritton error = EPERM; 17912a4b2251SJamie Gritton goto done_deref; 17920304c731SJamie Gritton } 1793eb8dcdeaSGleb Smirnoff if (!prison_ip_conflict_check(ppr, pr, ip4, PR_INET)) { 17940304c731SJamie Gritton error = EADDRINUSE; 1795eb8dcdeaSGleb Smirnoff vfs_opterror(opts, "IPv4 addresses clash"); 17962a4b2251SJamie Gritton goto done_deref; 1797b38ff370SJamie Gritton } 17980304c731SJamie Gritton } 1799b38ff370SJamie Gritton #endif 1800b38ff370SJamie Gritton #ifdef INET6 18012b0d6f81SJamie Gritton if (ip6s > 0) { 1802eb8dcdeaSGleb Smirnoff if ((ppr->pr_flags & PR_IP6) && 1803eb8dcdeaSGleb Smirnoff !prison_ip_parent_match(ppr->pr_addrs[PR_INET6], ip6, 1804eb8dcdeaSGleb Smirnoff PR_INET6)) { 18050304c731SJamie Gritton error = EPERM; 18062a4b2251SJamie Gritton goto done_deref; 18070304c731SJamie Gritton } 1808eb8dcdeaSGleb Smirnoff if (!prison_ip_conflict_check(ppr, pr, ip6, PR_INET6)) { 18090304c731SJamie Gritton error = EADDRINUSE; 1810eb8dcdeaSGleb Smirnoff vfs_opterror(opts, "IPv6 addresses clash"); 18112a4b2251SJamie Gritton goto done_deref; 1812b38ff370SJamie Gritton } 18130304c731SJamie Gritton } 1814b38ff370SJamie Gritton #endif 18150304c731SJamie Gritton onamelen = namelen = 0; 1816b6f47c23SJamie Gritton if (namelc != NULL) { 18176ab6058eSJamie Gritton /* Give a default name of the jid. Also allow the name to be 18186ab6058eSJamie Gritton * explicitly the jid - but not any other number, and only in 18196ab6058eSJamie Gritton * normal form (no leading zero/etc). 18206ab6058eSJamie Gritton */ 1821b6f47c23SJamie Gritton if (namelc[0] == '\0') 1822b6f47c23SJamie Gritton snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid); 18236ab6058eSJamie Gritton else if ((strtoul(namelc, &p, 10) != jid || 18246ab6058eSJamie Gritton namelc[0] < '1' || namelc[0] > '9') && *p == '\0') { 1825b38ff370SJamie Gritton error = EINVAL; 1826babbbb9cSJamie Gritton vfs_opterror(opts, 1827babbbb9cSJamie Gritton "name cannot be numeric (unless it is the jid)"); 18282a4b2251SJamie Gritton goto done_deref; 1829b38ff370SJamie Gritton } 1830b38ff370SJamie Gritton /* 18310304c731SJamie Gritton * Make sure the name isn't too long for the prison or its 18320304c731SJamie Gritton * children. 1833b38ff370SJamie Gritton */ 1834b6f47c23SJamie Gritton pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 1835b6f47c23SJamie Gritton onamelen = strlen(pr->pr_name + pnamelen); 1836b6f47c23SJamie Gritton namelen = strlen(namelc); 1837b6f47c23SJamie Gritton if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) { 18380304c731SJamie Gritton error = ENAMETOOLONG; 18392a4b2251SJamie Gritton goto done_deref; 18400304c731SJamie Gritton } 18410304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 18420304c731SJamie Gritton if (strlen(tpr->pr_name) + (namelen - onamelen) >= 18430304c731SJamie Gritton sizeof(pr->pr_name)) { 18440304c731SJamie Gritton error = ENAMETOOLONG; 18452a4b2251SJamie Gritton goto done_deref; 18460304c731SJamie Gritton } 18470304c731SJamie Gritton } 18480304c731SJamie Gritton } 1849b3079544SJamie Gritton pr_allow_diff = pr_allow & ~ppr->pr_allow; 1850b3079544SJamie Gritton if (pr_allow_diff & ~PR_ALLOW_DIFFERENCES) { 18510304c731SJamie Gritton error = EPERM; 18522a4b2251SJamie Gritton goto done_deref; 1853b38ff370SJamie Gritton } 1854b38ff370SJamie Gritton 1855b6f47c23SJamie Gritton /* 1856b6f47c23SJamie Gritton * Let modules check their parameters. This requires unlocking and 1857b6f47c23SJamie Gritton * then re-locking the prison, but this is still a valid state as long 1858b6f47c23SJamie Gritton * as allprison_lock remains xlocked. 1859b6f47c23SJamie Gritton */ 1860b6f47c23SJamie Gritton mtx_unlock(&pr->pr_mtx); 18612a4b2251SJamie Gritton drflags &= ~PD_LOCKED; 1862b6f47c23SJamie Gritton error = osd_jail_call(pr, PR_METHOD_CHECK, opts); 18632a4b2251SJamie Gritton if (error != 0) 18642a4b2251SJamie Gritton goto done_deref; 1865b6f47c23SJamie Gritton mtx_lock(&pr->pr_mtx); 18662a4b2251SJamie Gritton drflags |= PD_LOCKED; 1867b6f47c23SJamie Gritton 1868b6f47c23SJamie Gritton /* At this point, all valid parameters should have been noted. */ 1869b6f47c23SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 1870b6f47c23SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 1871b6f47c23SJamie Gritton error = EINVAL; 1872b6f47c23SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 18732a4b2251SJamie Gritton goto done_deref; 1874b6f47c23SJamie Gritton } 1875b6f47c23SJamie Gritton } 1876b6f47c23SJamie Gritton 1877b38ff370SJamie Gritton /* Set the parameters of the prison. */ 1878b38ff370SJamie Gritton #ifdef INET 18798bce8d28SZhenlei Huang redo_ip4 = false; 18800304c731SJamie Gritton if (pr_flags & PR_IP4_USER) { 18810304c731SJamie Gritton pr->pr_flags |= PR_IP4; 1882eb8dcdeaSGleb Smirnoff prison_ip_set(pr, PR_INET, ip4); 1883b38ff370SJamie Gritton ip4 = NULL; 18840304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1885bdfc8cc4SJamie Gritton #ifdef VIMAGE 1886bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1887bdfc8cc4SJamie Gritton descend = 0; 1888bdfc8cc4SJamie Gritton continue; 1889bdfc8cc4SJamie Gritton } 1890bdfc8cc4SJamie Gritton #endif 18918bce8d28SZhenlei Huang if (!prison_ip_restrict(tpr, PR_INET, NULL)) { 18928bce8d28SZhenlei Huang redo_ip4 = true; 18930304c731SJamie Gritton descend = 0; 18940304c731SJamie Gritton } 18950304c731SJamie Gritton } 18960304c731SJamie Gritton } 1897b38ff370SJamie Gritton #endif 1898b38ff370SJamie Gritton #ifdef INET6 18998bce8d28SZhenlei Huang redo_ip6 = false; 19000304c731SJamie Gritton if (pr_flags & PR_IP6_USER) { 19010304c731SJamie Gritton pr->pr_flags |= PR_IP6; 1902eb8dcdeaSGleb Smirnoff prison_ip_set(pr, PR_INET6, ip6); 1903b38ff370SJamie Gritton ip6 = NULL; 19040304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1905bdfc8cc4SJamie Gritton #ifdef VIMAGE 1906bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1907bdfc8cc4SJamie Gritton descend = 0; 1908bdfc8cc4SJamie Gritton continue; 1909bdfc8cc4SJamie Gritton } 1910bdfc8cc4SJamie Gritton #endif 19118bce8d28SZhenlei Huang if (!prison_ip_restrict(tpr, PR_INET6, NULL)) { 19128bce8d28SZhenlei Huang redo_ip6 = true; 19130304c731SJamie Gritton descend = 0; 19140304c731SJamie Gritton } 19150304c731SJamie Gritton } 19160304c731SJamie Gritton } 1917b38ff370SJamie Gritton #endif 19180304c731SJamie Gritton if (gotslevel) { 1919b38ff370SJamie Gritton pr->pr_securelevel = slevel; 19200304c731SJamie Gritton /* Set all child jails to be at least this level. */ 19210304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 19220304c731SJamie Gritton if (tpr->pr_securelevel < slevel) 19230304c731SJamie Gritton tpr->pr_securelevel = slevel; 19240304c731SJamie Gritton } 1925b97457e2SJamie Gritton if (gotchildmax) { 1926b97457e2SJamie Gritton pr->pr_childmax = childmax; 1927b97457e2SJamie Gritton /* Set all child jails to under this limit. */ 1928b97457e2SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level) 1929b97457e2SJamie Gritton if (tpr->pr_childmax > childmax - level) 1930b97457e2SJamie Gritton tpr->pr_childmax = childmax > level 1931b97457e2SJamie Gritton ? childmax - level : 0; 1932b97457e2SJamie Gritton } 19330304c731SJamie Gritton if (gotenforce) { 19340304c731SJamie Gritton pr->pr_enforce_statfs = enforce; 19350304c731SJamie Gritton /* Pass this restriction on to the children. */ 19360304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 19370304c731SJamie Gritton if (tpr->pr_enforce_statfs < enforce) 19380304c731SJamie Gritton tpr->pr_enforce_statfs = enforce; 19390304c731SJamie Gritton } 19400cc207a6SMartin Matuska if (gotrsnum) { 19410cc207a6SMartin Matuska pr->pr_devfs_rsnum = rsnum; 19420cc207a6SMartin Matuska /* Pass this restriction on to the children. */ 19430cc207a6SMartin Matuska FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 19440cc207a6SMartin Matuska tpr->pr_devfs_rsnum = rsnum; 19450cc207a6SMartin Matuska } 1946b6f47c23SJamie Gritton if (namelc != NULL) { 19470304c731SJamie Gritton if (ppr == &prison0) 1948b6f47c23SJamie Gritton strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name)); 19490304c731SJamie Gritton else 19500304c731SJamie Gritton snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s", 1951b6f47c23SJamie Gritton ppr->pr_name, namelc); 19520304c731SJamie Gritton /* Change this component of child names. */ 19530304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 19540304c731SJamie Gritton bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen, 19550304c731SJamie Gritton strlen(tpr->pr_name + onamelen) + 1); 19560304c731SJamie Gritton bcopy(pr->pr_name, tpr->pr_name, namelen); 19570304c731SJamie Gritton } 19580304c731SJamie Gritton } 1959b38ff370SJamie Gritton if (path != NULL) { 19600304c731SJamie Gritton /* Try to keep a real-rooted full pathname. */ 1961b38ff370SJamie Gritton strlcpy(pr->pr_path, path, sizeof(pr->pr_path)); 1962b38ff370SJamie Gritton pr->pr_root = root; 19632a4b2251SJamie Gritton root = NULL; 1964b38ff370SJamie Gritton } 196576ca6f88SJamie Gritton if (PR_HOST & ch_flags & ~pr_flags) { 196676ca6f88SJamie Gritton if (pr->pr_flags & PR_HOST) { 196776ca6f88SJamie Gritton /* 196876ca6f88SJamie Gritton * Copy the parent's host info. As with pr_ip4 above, 196976ca6f88SJamie Gritton * the lack of a lock on the parent is not a problem; 197076ca6f88SJamie Gritton * it is always set with allprison_lock at least 197176ca6f88SJamie Gritton * shared, and is held exclusively here. 197276ca6f88SJamie Gritton */ 1973c1f19219SJamie Gritton strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname, 1974c1f19219SJamie Gritton sizeof(pr->pr_hostname)); 1975c1f19219SJamie Gritton strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname, 1976c1f19219SJamie Gritton sizeof(pr->pr_domainname)); 1977c1f19219SJamie Gritton strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid, 1978c1f19219SJamie Gritton sizeof(pr->pr_hostuuid)); 197976ca6f88SJamie Gritton pr->pr_hostid = pr->pr_parent->pr_hostid; 198076ca6f88SJamie Gritton } 198176ca6f88SJamie Gritton } else if (host != NULL || domain != NULL || uuid != NULL || gothid) { 198276ca6f88SJamie Gritton /* Set this prison, and any descendants without PR_HOST. */ 1983b38ff370SJamie Gritton if (host != NULL) 1984c1f19219SJamie Gritton strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname)); 198576ca6f88SJamie Gritton if (domain != NULL) 1986c1f19219SJamie Gritton strlcpy(pr->pr_domainname, domain, 1987c1f19219SJamie Gritton sizeof(pr->pr_domainname)); 198876ca6f88SJamie Gritton if (uuid != NULL) 1989c1f19219SJamie Gritton strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid)); 199076ca6f88SJamie Gritton if (gothid) 199176ca6f88SJamie Gritton pr->pr_hostid = hid; 199276ca6f88SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 199376ca6f88SJamie Gritton if (tpr->pr_flags & PR_HOST) 199476ca6f88SJamie Gritton descend = 0; 199576ca6f88SJamie Gritton else { 199676ca6f88SJamie Gritton if (host != NULL) 1997c1f19219SJamie Gritton strlcpy(tpr->pr_hostname, 1998c1f19219SJamie Gritton pr->pr_hostname, 1999c1f19219SJamie Gritton sizeof(tpr->pr_hostname)); 200076ca6f88SJamie Gritton if (domain != NULL) 2001c1f19219SJamie Gritton strlcpy(tpr->pr_domainname, 2002c1f19219SJamie Gritton pr->pr_domainname, 2003c1f19219SJamie Gritton sizeof(tpr->pr_domainname)); 200476ca6f88SJamie Gritton if (uuid != NULL) 2005c1f19219SJamie Gritton strlcpy(tpr->pr_hostuuid, 2006c1f19219SJamie Gritton pr->pr_hostuuid, 2007c1f19219SJamie Gritton sizeof(tpr->pr_hostuuid)); 200876ca6f88SJamie Gritton if (gothid) 200976ca6f88SJamie Gritton tpr->pr_hostid = hid; 201076ca6f88SJamie Gritton } 201176ca6f88SJamie Gritton } 201276ca6f88SJamie Gritton } 20130304c731SJamie Gritton pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow; 20140fe74ae6SJamie Gritton if ((tallow = ch_allow & ~pr_allow)) 20150fe74ae6SJamie Gritton prison_set_allow_locked(pr, tallow, 0); 2016b38ff370SJamie Gritton /* 2017b38ff370SJamie Gritton * Persistent prisons get an extra reference, and prisons losing their 20181158508aSJamie Gritton * persist flag lose that reference. 2019b38ff370SJamie Gritton */ 20201158508aSJamie Gritton if (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags)) { 2021b38ff370SJamie Gritton if (pr_flags & PR_PERSIST) { 20226754ae25SJamie Gritton prison_hold(pr); 20231158508aSJamie Gritton /* 2024ed31b3f4SJamie Gritton * This may be a new prison's first user reference, 2025ed31b3f4SJamie Gritton * but wait to call it alive until after OSD calls 2026ed31b3f4SJamie Gritton * have had a chance to run (and perhaps to fail). 20271158508aSJamie Gritton */ 20286754ae25SJamie Gritton refcount_acquire(&pr->pr_uref); 2029b38ff370SJamie Gritton } else { 203039c8ef90SJamie Gritton drflags |= PD_DEUREF; 2031f7496dcaSJamie Gritton prison_free_not_last(pr); 2032b38ff370SJamie Gritton } 2033b38ff370SJamie Gritton } 2034b38ff370SJamie Gritton pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags; 2035b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 20362a4b2251SJamie Gritton drflags &= ~PD_LOCKED; 2037c861373bSJamie Gritton /* 2038c861373bSJamie Gritton * Any errors past this point will need to de-persist newly created 2039c861373bSJamie Gritton * prisons, as well as call remove methods. 2040c861373bSJamie Gritton */ 2041ed31b3f4SJamie Gritton if (created) 2042c861373bSJamie Gritton drflags |= PD_KILL; 2043b38ff370SJamie Gritton 2044a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 20454b5c9cf6SEdward Tomasz Napierala if (racct_enable && created) 2046a7ad07bfSEdward Tomasz Napierala prison_racct_attach(pr); 2047a7ad07bfSEdward Tomasz Napierala #endif 2048a7ad07bfSEdward Tomasz Napierala 20490304c731SJamie Gritton /* Locks may have prevented a complete restriction of child IP 20500304c731SJamie Gritton * addresses. If so, allocate some more memory and try again. 20510304c731SJamie Gritton */ 20520304c731SJamie Gritton #ifdef INET 20530304c731SJamie Gritton while (redo_ip4) { 2054eb8dcdeaSGleb Smirnoff ip4s = pr->pr_addrs[PR_INET]->ips; 20558bce8d28SZhenlei Huang MPASS(ip4 == NULL); 2056eb8dcdeaSGleb Smirnoff ip4 = prison_ip_alloc(PR_INET, ip4s, M_WAITOK); 20570304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 20588bce8d28SZhenlei Huang redo_ip4 = false; 20590304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 2060bdfc8cc4SJamie Gritton #ifdef VIMAGE 2061bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 2062bdfc8cc4SJamie Gritton descend = 0; 2063bdfc8cc4SJamie Gritton continue; 2064bdfc8cc4SJamie Gritton } 2065bdfc8cc4SJamie Gritton #endif 2066b2d76b52SZhenlei Huang if (!prison_ip_restrict(tpr, PR_INET, &ip4)) 2067b2d76b52SZhenlei Huang redo_ip4 = true; 20680304c731SJamie Gritton } 20690304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 20700304c731SJamie Gritton } 20710304c731SJamie Gritton #endif 20720304c731SJamie Gritton #ifdef INET6 20730304c731SJamie Gritton while (redo_ip6) { 2074eb8dcdeaSGleb Smirnoff ip6s = pr->pr_addrs[PR_INET6]->ips; 20758bce8d28SZhenlei Huang MPASS(ip6 == NULL); 2076eb8dcdeaSGleb Smirnoff ip6 = prison_ip_alloc(PR_INET6, ip6s, M_WAITOK); 20770304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 20788bce8d28SZhenlei Huang redo_ip6 = false; 20790304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 2080bdfc8cc4SJamie Gritton #ifdef VIMAGE 2081bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 2082bdfc8cc4SJamie Gritton descend = 0; 2083bdfc8cc4SJamie Gritton continue; 2084bdfc8cc4SJamie Gritton } 2085bdfc8cc4SJamie Gritton #endif 2086b2d76b52SZhenlei Huang if (!prison_ip_restrict(tpr, PR_INET6, &ip6)) 2087b2d76b52SZhenlei Huang redo_ip6 = true; 20880304c731SJamie Gritton } 20890304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 20900304c731SJamie Gritton } 20910304c731SJamie Gritton #endif 20920304c731SJamie Gritton 2093b38ff370SJamie Gritton /* Let the modules do their work. */ 2094ed31b3f4SJamie Gritton if (created) { 2095b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_CREATE, opts); 2096c861373bSJamie Gritton if (error) 20972a4b2251SJamie Gritton goto done_deref; 2098b38ff370SJamie Gritton } 2099b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_SET, opts); 2100c861373bSJamie Gritton if (error) 21012a4b2251SJamie Gritton goto done_deref; 2102b38ff370SJamie Gritton 21031158508aSJamie Gritton /* 21041158508aSJamie Gritton * A new prison is now ready to be seen; either it has gained a user 21051158508aSJamie Gritton * reference via persistence, or is about to gain one via attachment. 21061158508aSJamie Gritton */ 2107ed31b3f4SJamie Gritton if (created) { 21081158508aSJamie Gritton drflags = prison_lock_xlock(pr, drflags); 21091158508aSJamie Gritton pr->pr_state = PRISON_STATE_ALIVE; 21101158508aSJamie Gritton } 21111158508aSJamie Gritton 2112b38ff370SJamie Gritton /* Attach this process to the prison if requested. */ 2113b38ff370SJamie Gritton if (flags & JAIL_ATTACH) { 2114c861373bSJamie Gritton error = do_jail_attach(td, pr, 2115589e4c1dSJamie Gritton prison_lock_xlock(pr, drflags & PD_LOCK_FLAGS)); 2116f7496dcaSJamie Gritton drflags &= ~(PD_LOCKED | PD_LIST_XLOCKED); 2117b38ff370SJamie Gritton if (error) { 2118b38ff370SJamie Gritton vfs_opterror(opts, "attach failed"); 21192a4b2251SJamie Gritton goto done_deref; 2120b38ff370SJamie Gritton } 2121b38ff370SJamie Gritton } 2122b38ff370SJamie Gritton 21231fb24974SEdward Tomasz Napierala #ifdef RACCT 21244b5c9cf6SEdward Tomasz Napierala if (racct_enable && !created) { 2125701d6b50SJamie Gritton if (drflags & PD_LOCKED) { 2126701d6b50SJamie Gritton mtx_unlock(&pr->pr_mtx); 2127701d6b50SJamie Gritton drflags &= ~PD_LOCKED; 2128701d6b50SJamie Gritton } 2129f7496dcaSJamie Gritton if (drflags & PD_LIST_XLOCKED) { 2130f7496dcaSJamie Gritton sx_xunlock(&allprison_lock); 2131f7496dcaSJamie Gritton drflags &= ~PD_LIST_XLOCKED; 2132b4e87a63SJamie Gritton } 21331fb24974SEdward Tomasz Napierala prison_racct_modify(pr); 21341fb24974SEdward Tomasz Napierala } 21351fb24974SEdward Tomasz Napierala #endif 21361fb24974SEdward Tomasz Napierala 2137ed31b3f4SJamie Gritton if (created && pr != &prison0 && (pr->pr_allow & PR_ALLOW_NFSD) != 0 && 2138bba7a2e8SRick Macklem (pr->pr_root->v_vflag & VV_ROOT) == 0) 2139bba7a2e8SRick Macklem printf("Warning jail jid=%d: mountd/nfsd requires a separate" 2140bba7a2e8SRick Macklem " file system\n", pr->pr_id); 2141bba7a2e8SRick Macklem 2142c861373bSJamie Gritton drflags &= ~PD_KILL; 21431fb24974SEdward Tomasz Napierala td->td_retval[0] = pr->pr_id; 21441fb24974SEdward Tomasz Napierala 21452a4b2251SJamie Gritton done_deref: 21462a4b2251SJamie Gritton /* Release any temporary prison holds and/or locks. */ 21472a4b2251SJamie Gritton if (pr != NULL) 21482a4b2251SJamie Gritton prison_deref(pr, drflags); 21492a4b2251SJamie Gritton else if (drflags & PD_LIST_SLOCKED) 2150b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 21512a4b2251SJamie Gritton else if (drflags & PD_LIST_XLOCKED) 2152b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 21535050aa86SKonstantin Belousov if (root != NULL) 2154b38ff370SJamie Gritton vrele(root); 2155b38ff370SJamie Gritton done_errmsg: 2156b38ff370SJamie Gritton if (error) { 21572a4b2251SJamie Gritton /* Write the error message back to userspace. */ 2158176ff3a0SJamie Gritton if (vfs_getopt(opts, "errmsg", (void **)&errmsg, 2159176ff3a0SJamie Gritton &errmsg_len) == 0 && errmsg_len > 0) { 2160b38ff370SJamie Gritton errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1; 2161b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 2162b38ff370SJamie Gritton bcopy(errmsg, 2163b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2164b38ff370SJamie Gritton errmsg_len); 2165b38ff370SJamie Gritton else 2166abbc260fSMark Johnston (void)copyout(errmsg, 2167b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2168b38ff370SJamie Gritton errmsg_len); 2169b38ff370SJamie Gritton } 2170b38ff370SJamie Gritton } 2171b38ff370SJamie Gritton done_free: 2172b38ff370SJamie Gritton #ifdef INET 2173eb8dcdeaSGleb Smirnoff prison_ip_free(ip4); 2174b38ff370SJamie Gritton #endif 2175b38ff370SJamie Gritton #ifdef INET6 2176eb8dcdeaSGleb Smirnoff prison_ip_free(ip6); 2177b38ff370SJamie Gritton #endif 21786dfe0a3dSMartin Matuska if (g_path != NULL) 21796dfe0a3dSMartin Matuska free(g_path, M_TEMP); 2180b38ff370SJamie Gritton vfs_freeopts(opts); 2181b38ff370SJamie Gritton return (error); 2182b38ff370SJamie Gritton } 2183b38ff370SJamie Gritton 2184b38ff370SJamie Gritton /* 21857de883c8SJamie Gritton * Find the next available prison ID. Return the ID on success, or zero 21867de883c8SJamie Gritton * on failure. Also set a pointer to the allprison list entry the prison 21877de883c8SJamie Gritton * should be inserted before. 21887de883c8SJamie Gritton */ 21897de883c8SJamie Gritton static int 21907de883c8SJamie Gritton get_next_prid(struct prison **insprp) 21917de883c8SJamie Gritton { 21927de883c8SJamie Gritton struct prison *inspr; 21937de883c8SJamie Gritton int jid, maxid; 21947de883c8SJamie Gritton 21957de883c8SJamie Gritton jid = lastprid % JAIL_MAX + 1; 21967de883c8SJamie Gritton if (TAILQ_EMPTY(&allprison) || 21977de883c8SJamie Gritton TAILQ_LAST(&allprison, prisonlist)->pr_id < jid) { 21987de883c8SJamie Gritton /* 21997de883c8SJamie Gritton * A common case is for all jails to be implicitly numbered, 22007de883c8SJamie Gritton * which means they'll go on the end of the list, at least 22017de883c8SJamie Gritton * for the first JAIL_MAX times. 22027de883c8SJamie Gritton */ 22037de883c8SJamie Gritton inspr = NULL; 22047de883c8SJamie Gritton } else { 22057de883c8SJamie Gritton /* 22067de883c8SJamie Gritton * Take two passes through the allprison list: first starting 22077de883c8SJamie Gritton * with the proposed jid, then ending with it. 22087de883c8SJamie Gritton */ 22097de883c8SJamie Gritton for (maxid = JAIL_MAX; maxid != 0; ) { 22107de883c8SJamie Gritton TAILQ_FOREACH(inspr, &allprison, pr_list) { 22117de883c8SJamie Gritton if (inspr->pr_id < jid) 22127de883c8SJamie Gritton continue; 2213f7496dcaSJamie Gritton if (inspr->pr_id > jid) { 2214f7496dcaSJamie Gritton /* Found an opening. */ 22157de883c8SJamie Gritton maxid = 0; 22167de883c8SJamie Gritton break; 22177de883c8SJamie Gritton } 22187de883c8SJamie Gritton if (++jid > maxid) { 22197de883c8SJamie Gritton if (lastprid == maxid || lastprid == 0) 22207de883c8SJamie Gritton { 22217de883c8SJamie Gritton /* 22227de883c8SJamie Gritton * The entire legal range 22237de883c8SJamie Gritton * has been traversed 22247de883c8SJamie Gritton */ 22257de883c8SJamie Gritton return 0; 22267de883c8SJamie Gritton } 22277de883c8SJamie Gritton /* Try again from the start. */ 22287de883c8SJamie Gritton jid = 1; 22297de883c8SJamie Gritton maxid = lastprid; 22307de883c8SJamie Gritton break; 22317de883c8SJamie Gritton } 22327de883c8SJamie Gritton } 22337de883c8SJamie Gritton if (inspr == NULL) { 22347de883c8SJamie Gritton /* Found room at the end of the list. */ 22357de883c8SJamie Gritton break; 22367de883c8SJamie Gritton } 22377de883c8SJamie Gritton } 22387de883c8SJamie Gritton } 22397de883c8SJamie Gritton *insprp = inspr; 22407de883c8SJamie Gritton lastprid = jid; 22417de883c8SJamie Gritton return (jid); 22427de883c8SJamie Gritton } 22437de883c8SJamie Gritton 22447de883c8SJamie Gritton /* 2245ed31b3f4SJamie Gritton * Find the next available ID for a renumbered dead prison. This is the same 2246ed31b3f4SJamie Gritton * as get_next_prid, but counting backward from the end of the range. 2247ed31b3f4SJamie Gritton */ 2248ed31b3f4SJamie Gritton static int 2249ed31b3f4SJamie Gritton get_next_deadid(struct prison **dinsprp) 2250ed31b3f4SJamie Gritton { 2251ed31b3f4SJamie Gritton struct prison *dinspr; 2252ed31b3f4SJamie Gritton int deadid, minid; 2253ed31b3f4SJamie Gritton 2254ed31b3f4SJamie Gritton deadid = lastdeadid ? lastdeadid - 1 : JAIL_MAX; 2255ed31b3f4SJamie Gritton /* 2256ed31b3f4SJamie Gritton * Take two reverse passes through the allprison list: first 2257ed31b3f4SJamie Gritton * starting with the proposed deadid, then ending with it. 2258ed31b3f4SJamie Gritton */ 2259ed31b3f4SJamie Gritton for (minid = 1; minid != 0; ) { 2260ed31b3f4SJamie Gritton TAILQ_FOREACH_REVERSE(dinspr, &allprison, prisonlist, pr_list) { 2261ed31b3f4SJamie Gritton if (dinspr->pr_id > deadid) 2262ed31b3f4SJamie Gritton continue; 2263ed31b3f4SJamie Gritton if (dinspr->pr_id < deadid) { 2264ed31b3f4SJamie Gritton /* Found an opening. */ 2265ed31b3f4SJamie Gritton minid = 0; 2266ed31b3f4SJamie Gritton break; 2267ed31b3f4SJamie Gritton } 2268ed31b3f4SJamie Gritton if (--deadid < minid) { 2269ed31b3f4SJamie Gritton if (lastdeadid == minid || lastdeadid == 0) 2270ed31b3f4SJamie Gritton { 2271ed31b3f4SJamie Gritton /* 2272ed31b3f4SJamie Gritton * The entire legal range 2273ed31b3f4SJamie Gritton * has been traversed 2274ed31b3f4SJamie Gritton */ 2275ed31b3f4SJamie Gritton return 0; 2276ed31b3f4SJamie Gritton } 2277ed31b3f4SJamie Gritton /* Try again from the end. */ 2278ed31b3f4SJamie Gritton deadid = JAIL_MAX; 2279ed31b3f4SJamie Gritton minid = lastdeadid; 2280ed31b3f4SJamie Gritton break; 2281ed31b3f4SJamie Gritton } 2282ed31b3f4SJamie Gritton } 2283ed31b3f4SJamie Gritton if (dinspr == NULL) { 2284ed31b3f4SJamie Gritton /* Found room at the beginning of the list. */ 2285ed31b3f4SJamie Gritton break; 2286ed31b3f4SJamie Gritton } 2287ed31b3f4SJamie Gritton } 2288ed31b3f4SJamie Gritton *dinsprp = dinspr; 2289ed31b3f4SJamie Gritton lastdeadid = deadid; 2290ed31b3f4SJamie Gritton return (deadid); 2291ed31b3f4SJamie Gritton } 2292ed31b3f4SJamie Gritton 2293ed31b3f4SJamie Gritton /* 2294b38ff370SJamie Gritton * struct jail_get_args { 2295b38ff370SJamie Gritton * struct iovec *iovp; 2296b38ff370SJamie Gritton * unsigned int iovcnt; 2297b38ff370SJamie Gritton * int flags; 2298b38ff370SJamie Gritton * }; 2299b38ff370SJamie Gritton */ 2300b38ff370SJamie Gritton int 23018451d0ddSKip Macy sys_jail_get(struct thread *td, struct jail_get_args *uap) 2302b38ff370SJamie Gritton { 2303b38ff370SJamie Gritton struct uio *auio; 2304b38ff370SJamie Gritton int error; 2305b38ff370SJamie Gritton 2306b38ff370SJamie Gritton /* Check that we have an even number of iovecs. */ 2307b38ff370SJamie Gritton if (uap->iovcnt & 1) 2308b38ff370SJamie Gritton return (EINVAL); 2309b38ff370SJamie Gritton 2310b38ff370SJamie Gritton error = copyinuio(uap->iovp, uap->iovcnt, &auio); 2311b38ff370SJamie Gritton if (error) 2312b38ff370SJamie Gritton return (error); 2313b38ff370SJamie Gritton error = kern_jail_get(td, auio, uap->flags); 2314b38ff370SJamie Gritton if (error == 0) 2315b38ff370SJamie Gritton error = copyout(auio->uio_iov, uap->iovp, 2316b38ff370SJamie Gritton uap->iovcnt * sizeof(struct iovec)); 231761cc4830SAlfredo Mazzinghi freeuio(auio); 2318b38ff370SJamie Gritton return (error); 2319b38ff370SJamie Gritton } 2320b38ff370SJamie Gritton 2321b38ff370SJamie Gritton int 2322b38ff370SJamie Gritton kern_jail_get(struct thread *td, struct uio *optuio, int flags) 2323b38ff370SJamie Gritton { 2324672756aaSJamie Gritton struct bool_flags *bf; 2325672756aaSJamie Gritton struct jailsys_flags *jsf; 23260304c731SJamie Gritton struct prison *pr, *mypr; 2327b38ff370SJamie Gritton struct vfsopt *opt; 2328b38ff370SJamie Gritton struct vfsoptlist *opts; 2329b38ff370SJamie Gritton char *errmsg, *name; 23302a4b2251SJamie Gritton int drflags, error, errmsg_len, errmsg_pos, i, jid, len, pos; 2331672756aaSJamie Gritton unsigned f; 2332b38ff370SJamie Gritton 2333b38ff370SJamie Gritton if (flags & ~JAIL_GET_MASK) 2334b38ff370SJamie Gritton return (EINVAL); 2335b38ff370SJamie Gritton 2336b38ff370SJamie Gritton /* Get the parameter list. */ 2337b38ff370SJamie Gritton error = vfs_buildopts(optuio, &opts); 2338b38ff370SJamie Gritton if (error) 2339b38ff370SJamie Gritton return (error); 2340b38ff370SJamie Gritton errmsg_pos = vfs_getopt_pos(opts, "errmsg"); 23410304c731SJamie Gritton mypr = td->td_ucred->cr_prison; 23422a4b2251SJamie Gritton pr = NULL; 23431e2a13e6SJamie Gritton 2344b38ff370SJamie Gritton /* 2345b38ff370SJamie Gritton * Find the prison specified by one of: lastjid, jid, name. 2346b38ff370SJamie Gritton */ 2347b38ff370SJamie Gritton sx_slock(&allprison_lock); 23482a4b2251SJamie Gritton drflags = PD_LIST_SLOCKED; 2349b38ff370SJamie Gritton error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid)); 2350b38ff370SJamie Gritton if (error == 0) { 2351b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 2352f7496dcaSJamie Gritton if (pr->pr_id > jid && 2353f7496dcaSJamie Gritton ((flags & JAIL_DYING) || prison_isalive(pr)) && 2354f7496dcaSJamie Gritton prison_ischild(mypr, pr)) { 2355b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 23562a4b2251SJamie Gritton drflags |= PD_LOCKED; 2357b38ff370SJamie Gritton goto found_prison; 23582a4b2251SJamie Gritton } 2359f7496dcaSJamie Gritton } 2360b38ff370SJamie Gritton error = ENOENT; 2361b38ff370SJamie Gritton vfs_opterror(opts, "no jail after %d", jid); 23622a4b2251SJamie Gritton goto done; 2363b38ff370SJamie Gritton } else if (error != ENOENT) 23642a4b2251SJamie Gritton goto done; 2365b38ff370SJamie Gritton 2366b38ff370SJamie Gritton error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 2367b38ff370SJamie Gritton if (error == 0) { 2368b38ff370SJamie Gritton if (jid != 0) { 23690304c731SJamie Gritton pr = prison_find_child(mypr, jid); 2370b38ff370SJamie Gritton if (pr != NULL) { 23712a4b2251SJamie Gritton drflags |= PD_LOCKED; 237276ad42abSJamie Gritton if (!(prison_isalive(pr) || 237376ad42abSJamie Gritton (flags & JAIL_DYING))) { 2374b38ff370SJamie Gritton error = ENOENT; 2375b38ff370SJamie Gritton vfs_opterror(opts, "jail %d is dying", 2376b38ff370SJamie Gritton jid); 23772a4b2251SJamie Gritton goto done; 2378b38ff370SJamie Gritton } 2379b38ff370SJamie Gritton goto found_prison; 2380b38ff370SJamie Gritton } 2381b38ff370SJamie Gritton error = ENOENT; 2382b38ff370SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 23832a4b2251SJamie Gritton goto done; 2384b38ff370SJamie Gritton } 2385b38ff370SJamie Gritton } else if (error != ENOENT) 23862a4b2251SJamie Gritton goto done; 2387b38ff370SJamie Gritton 2388b38ff370SJamie Gritton error = vfs_getopt(opts, "name", (void **)&name, &len); 2389b38ff370SJamie Gritton if (error == 0) { 2390b38ff370SJamie Gritton if (len == 0 || name[len - 1] != '\0') { 2391b38ff370SJamie Gritton error = EINVAL; 23922a4b2251SJamie Gritton goto done; 2393b38ff370SJamie Gritton } 23940304c731SJamie Gritton pr = prison_find_name(mypr, name); 2395b38ff370SJamie Gritton if (pr != NULL) { 23962a4b2251SJamie Gritton drflags |= PD_LOCKED; 239776ad42abSJamie Gritton if (!(prison_isalive(pr) || (flags & JAIL_DYING))) { 2398b38ff370SJamie Gritton error = ENOENT; 2399b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" is dying", 2400b38ff370SJamie Gritton name); 24012a4b2251SJamie Gritton goto done; 2402b38ff370SJamie Gritton } 2403b38ff370SJamie Gritton goto found_prison; 2404b38ff370SJamie Gritton } 2405b38ff370SJamie Gritton error = ENOENT; 2406b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", name); 24072a4b2251SJamie Gritton goto done; 2408b38ff370SJamie Gritton } else if (error != ENOENT) 24092a4b2251SJamie Gritton goto done; 2410b38ff370SJamie Gritton 2411b38ff370SJamie Gritton vfs_opterror(opts, "no jail specified"); 2412b38ff370SJamie Gritton error = ENOENT; 24132a4b2251SJamie Gritton goto done; 2414b38ff370SJamie Gritton 2415b38ff370SJamie Gritton found_prison: 2416b38ff370SJamie Gritton /* Get the parameters of the prison. */ 24176754ae25SJamie Gritton prison_hold(pr); 24182a4b2251SJamie Gritton drflags |= PD_DEREF; 2419b38ff370SJamie Gritton td->td_retval[0] = pr->pr_id; 2420b38ff370SJamie Gritton error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id)); 2421b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24222a4b2251SJamie Gritton goto done; 24230304c731SJamie Gritton i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id; 24240304c731SJamie Gritton error = vfs_setopt(opts, "parent", &i, sizeof(i)); 2425b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24262a4b2251SJamie Gritton goto done; 24270304c731SJamie Gritton error = vfs_setopts(opts, "name", prison_name(mypr, pr)); 24280304c731SJamie Gritton if (error != 0 && error != ENOENT) 24292a4b2251SJamie Gritton goto done; 24300304c731SJamie Gritton error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id, 2431b38ff370SJamie Gritton sizeof(pr->pr_cpuset->cs_id)); 2432b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24332a4b2251SJamie Gritton goto done; 24340304c731SJamie Gritton error = vfs_setopts(opts, "path", prison_path(mypr, pr)); 2435b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24362a4b2251SJamie Gritton goto done; 2437b38ff370SJamie Gritton #ifdef INET 2438500f82d6SZhenlei Huang error = vfs_setopt_part(opts, "ip4.addr", pr->pr_addrs[PR_INET]->pr_ip, 2439eb8dcdeaSGleb Smirnoff pr->pr_addrs[PR_INET] ? pr->pr_addrs[PR_INET]->ips * 2440eb8dcdeaSGleb Smirnoff pr_families[PR_INET].size : 0 ); 2441b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24422a4b2251SJamie Gritton goto done; 2443b38ff370SJamie Gritton #endif 2444b38ff370SJamie Gritton #ifdef INET6 2445500f82d6SZhenlei Huang error = vfs_setopt_part(opts, "ip6.addr", pr->pr_addrs[PR_INET6]->pr_ip, 2446eb8dcdeaSGleb Smirnoff pr->pr_addrs[PR_INET6] ? pr->pr_addrs[PR_INET6]->ips * 2447eb8dcdeaSGleb Smirnoff pr_families[PR_INET6].size : 0 ); 2448b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24492a4b2251SJamie Gritton goto done; 2450b38ff370SJamie Gritton #endif 2451b38ff370SJamie Gritton error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel, 2452b38ff370SJamie Gritton sizeof(pr->pr_securelevel)); 2453b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24542a4b2251SJamie Gritton goto done; 2455b97457e2SJamie Gritton error = vfs_setopt(opts, "children.cur", &pr->pr_childcount, 2456b97457e2SJamie Gritton sizeof(pr->pr_childcount)); 2457b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 24582a4b2251SJamie Gritton goto done; 2459b97457e2SJamie Gritton error = vfs_setopt(opts, "children.max", &pr->pr_childmax, 2460b97457e2SJamie Gritton sizeof(pr->pr_childmax)); 2461b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 24622a4b2251SJamie Gritton goto done; 2463c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostname", pr->pr_hostname); 2464b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24652a4b2251SJamie Gritton goto done; 2466c1f19219SJamie Gritton error = vfs_setopts(opts, "host.domainname", pr->pr_domainname); 246776ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 24682a4b2251SJamie Gritton goto done; 2469c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid); 247076ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 24712a4b2251SJamie Gritton goto done; 2472841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32 2473a5c1afadSDmitry Chagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 247476ca6f88SJamie Gritton uint32_t hid32 = pr->pr_hostid; 247576ca6f88SJamie Gritton 247676ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32)); 247776ca6f88SJamie Gritton } else 247876ca6f88SJamie Gritton #endif 247976ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid, 248076ca6f88SJamie Gritton sizeof(pr->pr_hostid)); 248176ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 24822a4b2251SJamie Gritton goto done; 24830304c731SJamie Gritton error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs, 24840304c731SJamie Gritton sizeof(pr->pr_enforce_statfs)); 24850304c731SJamie Gritton if (error != 0 && error != ENOENT) 24862a4b2251SJamie Gritton goto done; 24870cc207a6SMartin Matuska error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum, 24880cc207a6SMartin Matuska sizeof(pr->pr_devfs_rsnum)); 24890cc207a6SMartin Matuska if (error != 0 && error != ENOENT) 24902a4b2251SJamie Gritton goto done; 2491672756aaSJamie Gritton for (bf = pr_flag_bool; 2492672756aaSJamie Gritton bf < pr_flag_bool + nitems(pr_flag_bool); 2493672756aaSJamie Gritton bf++) { 2494672756aaSJamie Gritton i = (pr->pr_flags & bf->flag) ? 1 : 0; 2495672756aaSJamie Gritton error = vfs_setopt(opts, bf->name, &i, sizeof(i)); 2496b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24972a4b2251SJamie Gritton goto done; 2498b38ff370SJamie Gritton i = !i; 2499672756aaSJamie Gritton error = vfs_setopt(opts, bf->noname, &i, sizeof(i)); 2500b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 25012a4b2251SJamie Gritton goto done; 25020304c731SJamie Gritton } 2503672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 2504672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 2505672756aaSJamie Gritton jsf++) { 2506672756aaSJamie Gritton f = pr->pr_flags & (jsf->disable | jsf->new); 2507672756aaSJamie Gritton i = (f != 0 && f == jsf->disable) ? JAIL_SYS_DISABLE 2508672756aaSJamie Gritton : (f == jsf->new) ? JAIL_SYS_NEW 25097cbf7213SJamie Gritton : JAIL_SYS_INHERIT; 2510672756aaSJamie Gritton error = vfs_setopt(opts, jsf->name, &i, sizeof(i)); 25117cbf7213SJamie Gritton if (error != 0 && error != ENOENT) 25122a4b2251SJamie Gritton goto done; 25137cbf7213SJamie Gritton } 2514672756aaSJamie Gritton for (bf = pr_flag_allow; 25155d58f959SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 25165d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 2517672756aaSJamie Gritton bf++) { 2518672756aaSJamie Gritton i = (pr->pr_allow & bf->flag) ? 1 : 0; 2519672756aaSJamie Gritton error = vfs_setopt(opts, bf->name, &i, sizeof(i)); 25200304c731SJamie Gritton if (error != 0 && error != ENOENT) 25212a4b2251SJamie Gritton goto done; 25220304c731SJamie Gritton i = !i; 2523672756aaSJamie Gritton error = vfs_setopt(opts, bf->noname, &i, sizeof(i)); 25240304c731SJamie Gritton if (error != 0 && error != ENOENT) 25252a4b2251SJamie Gritton goto done; 25260304c731SJamie Gritton } 252776ad42abSJamie Gritton i = !prison_isalive(pr); 2528b38ff370SJamie Gritton error = vfs_setopt(opts, "dying", &i, sizeof(i)); 2529b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 25302a4b2251SJamie Gritton goto done; 2531b38ff370SJamie Gritton i = !i; 2532b38ff370SJamie Gritton error = vfs_setopt(opts, "nodying", &i, sizeof(i)); 2533b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 25342a4b2251SJamie Gritton goto done; 2535bd96bd15SIan Lepore error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate, 2536bd96bd15SIan Lepore sizeof(pr->pr_osreldate)); 2537a1a4c1b0SIan Lepore if (error != 0 && error != ENOENT) 25382a4b2251SJamie Gritton goto done; 2539a1a4c1b0SIan Lepore error = vfs_setopts(opts, "osrelease", pr->pr_osrelease); 2540a1a4c1b0SIan Lepore if (error != 0 && error != ENOENT) 25412a4b2251SJamie Gritton goto done; 2542b38ff370SJamie Gritton 2543b38ff370SJamie Gritton /* Get the module parameters. */ 2544b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 25452a4b2251SJamie Gritton drflags &= ~PD_LOCKED; 2546b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_GET, opts); 2547b38ff370SJamie Gritton if (error) 25482a4b2251SJamie Gritton goto done; 25492a4b2251SJamie Gritton prison_deref(pr, drflags); 25502a4b2251SJamie Gritton pr = NULL; 25512a4b2251SJamie Gritton drflags = 0; 2552b38ff370SJamie Gritton 2553b38ff370SJamie Gritton /* By now, all parameters should have been noted. */ 2554b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2555*30e6e008SIgor Ostapenko if (!opt->seen && 2556*30e6e008SIgor Ostapenko (strstr(opt->name, JAIL_META_PRIVATE ".") == opt->name || 2557*30e6e008SIgor Ostapenko strstr(opt->name, JAIL_META_SHARED ".") == opt->name)) { 2558*30e6e008SIgor Ostapenko /* Communicate back a missing key. */ 2559*30e6e008SIgor Ostapenko free(opt->value, M_MOUNT); 2560*30e6e008SIgor Ostapenko opt->value = NULL; 2561*30e6e008SIgor Ostapenko opt->len = 0; 2562*30e6e008SIgor Ostapenko continue; 2563*30e6e008SIgor Ostapenko } 2564b38ff370SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 2565b38ff370SJamie Gritton error = EINVAL; 2566b38ff370SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 25672a4b2251SJamie Gritton goto done; 2568b38ff370SJamie Gritton } 2569b38ff370SJamie Gritton } 2570b38ff370SJamie Gritton 2571b38ff370SJamie Gritton /* Write the fetched parameters back to userspace. */ 2572b38ff370SJamie Gritton error = 0; 2573b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2574b38ff370SJamie Gritton if (opt->pos >= 0 && opt->pos != errmsg_pos) { 2575b38ff370SJamie Gritton pos = 2 * opt->pos + 1; 2576b38ff370SJamie Gritton optuio->uio_iov[pos].iov_len = opt->len; 2577b38ff370SJamie Gritton if (opt->value != NULL) { 2578b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) { 2579b38ff370SJamie Gritton bcopy(opt->value, 2580b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2581b38ff370SJamie Gritton opt->len); 2582b38ff370SJamie Gritton } else { 2583b38ff370SJamie Gritton error = copyout(opt->value, 2584b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2585b38ff370SJamie Gritton opt->len); 2586b38ff370SJamie Gritton if (error) 2587b38ff370SJamie Gritton break; 2588b38ff370SJamie Gritton } 2589b38ff370SJamie Gritton } 2590b38ff370SJamie Gritton } 2591b38ff370SJamie Gritton } 2592b38ff370SJamie Gritton 25932a4b2251SJamie Gritton done: 25942a4b2251SJamie Gritton /* Release any temporary prison holds and/or locks. */ 25952a4b2251SJamie Gritton if (pr != NULL) 25962a4b2251SJamie Gritton prison_deref(pr, drflags); 25972a4b2251SJamie Gritton else if (drflags & PD_LIST_SLOCKED) 2598b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2599b38ff370SJamie Gritton if (error && errmsg_pos >= 0) { 26002a4b2251SJamie Gritton /* Write the error message back to userspace. */ 2601b38ff370SJamie Gritton vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 2602b38ff370SJamie Gritton errmsg_pos = 2 * errmsg_pos + 1; 2603b38ff370SJamie Gritton if (errmsg_len > 0) { 2604b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 2605b38ff370SJamie Gritton bcopy(errmsg, 2606b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2607b38ff370SJamie Gritton errmsg_len); 2608b38ff370SJamie Gritton else 2609abbc260fSMark Johnston (void)copyout(errmsg, 2610b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2611b38ff370SJamie Gritton errmsg_len); 2612b38ff370SJamie Gritton } 2613b38ff370SJamie Gritton } 2614b38ff370SJamie Gritton vfs_freeopts(opts); 2615b38ff370SJamie Gritton return (error); 2616b38ff370SJamie Gritton } 2617b38ff370SJamie Gritton 2618b38ff370SJamie Gritton /* 2619b38ff370SJamie Gritton * struct jail_remove_args { 2620b38ff370SJamie Gritton * int jid; 2621b38ff370SJamie Gritton * }; 2622b38ff370SJamie Gritton */ 2623b38ff370SJamie Gritton int 26248451d0ddSKip Macy sys_jail_remove(struct thread *td, struct jail_remove_args *uap) 2625b38ff370SJamie Gritton { 2626c861373bSJamie Gritton struct prison *pr; 2627c861373bSJamie Gritton int error; 2628b38ff370SJamie Gritton 2629b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_REMOVE); 2630b38ff370SJamie Gritton if (error) 2631b38ff370SJamie Gritton return (error); 2632b38ff370SJamie Gritton 2633b38ff370SJamie Gritton sx_xlock(&allprison_lock); 26340304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2635b38ff370SJamie Gritton if (pr == NULL) { 2636b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2637b38ff370SJamie Gritton return (EINVAL); 2638b38ff370SJamie Gritton } 2639c861373bSJamie Gritton if (!prison_isalive(pr)) { 2640c861373bSJamie Gritton /* Silently ignore already-dying prisons. */ 26410304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 2642c861373bSJamie Gritton sx_xunlock(&allprison_lock); 26430304c731SJamie Gritton return (0); 26440304c731SJamie Gritton } 2645c861373bSJamie Gritton prison_deref(pr, PD_KILL | PD_LOCKED | PD_LIST_XLOCKED); 2646c861373bSJamie Gritton return (0); 264775c13541SPoul-Henning Kamp } 264875c13541SPoul-Henning Kamp 2649fd7a8150SMike Barcroft /* 26509ddb7954SMike Barcroft * struct jail_attach_args { 26519ddb7954SMike Barcroft * int jid; 26529ddb7954SMike Barcroft * }; 2653fd7a8150SMike Barcroft */ 2654fd7a8150SMike Barcroft int 26558451d0ddSKip Macy sys_jail_attach(struct thread *td, struct jail_attach_args *uap) 2656fd7a8150SMike Barcroft { 2657b38ff370SJamie Gritton struct prison *pr; 2658b38ff370SJamie Gritton int error; 2659b38ff370SJamie Gritton 2660b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 2661b38ff370SJamie Gritton if (error) 2662b38ff370SJamie Gritton return (error); 2663b38ff370SJamie Gritton 2664f7496dcaSJamie Gritton sx_slock(&allprison_lock); 26650304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2666b38ff370SJamie Gritton if (pr == NULL) { 2667b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2668b38ff370SJamie Gritton return (EINVAL); 2669b38ff370SJamie Gritton } 2670b38ff370SJamie Gritton 267176ad42abSJamie Gritton /* Do not allow a process to attach to a prison that is not alive. */ 267276ad42abSJamie Gritton if (!prison_isalive(pr)) { 2673b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2674b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2675b38ff370SJamie Gritton return (EINVAL); 2676b38ff370SJamie Gritton } 2677b38ff370SJamie Gritton 2678f7496dcaSJamie Gritton return (do_jail_attach(td, pr, PD_LOCKED | PD_LIST_SLOCKED)); 2679b38ff370SJamie Gritton } 2680b38ff370SJamie Gritton 2681b38ff370SJamie Gritton static int 2682f7496dcaSJamie Gritton do_jail_attach(struct thread *td, struct prison *pr, int drflags) 2683b38ff370SJamie Gritton { 2684fd7a8150SMike Barcroft struct proc *p; 2685fd7a8150SMike Barcroft struct ucred *newcred, *oldcred; 26865050aa86SKonstantin Belousov int error; 2687fd7a8150SMike Barcroft 2688f7496dcaSJamie Gritton mtx_assert(&pr->pr_mtx, MA_OWNED); 2689f7496dcaSJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 2690589e4c1dSJamie Gritton drflags &= PD_LOCK_FLAGS; 269157f22bd4SJacques Vidrine /* 269257f22bd4SJacques Vidrine * XXX: Note that there is a slight race here if two threads 269357f22bd4SJacques Vidrine * in the same privileged process attempt to attach to two 269457f22bd4SJacques Vidrine * different jails at the same time. It is important for 269557f22bd4SJacques Vidrine * user processes not to do this, or they might end up with 269657f22bd4SJacques Vidrine * a process root from one prison, but attached to the jail 269757f22bd4SJacques Vidrine * of another. 269857f22bd4SJacques Vidrine */ 26991158508aSJamie Gritton prison_hold(pr); 27006754ae25SJamie Gritton refcount_acquire(&pr->pr_uref); 2701f7496dcaSJamie Gritton drflags |= PD_DEREF | PD_DEUREF; 2702fd7a8150SMike Barcroft mtx_unlock(&pr->pr_mtx); 2703f7496dcaSJamie Gritton drflags &= ~PD_LOCKED; 2704b38ff370SJamie Gritton 2705b38ff370SJamie Gritton /* Let modules do whatever they need to prepare for attaching. */ 2706b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_ATTACH, td); 2707b38ff370SJamie Gritton if (error) { 2708f7496dcaSJamie Gritton prison_deref(pr, drflags); 2709b38ff370SJamie Gritton return (error); 2710b38ff370SJamie Gritton } 2711f7496dcaSJamie Gritton sx_unlock(&allprison_lock); 2712f7496dcaSJamie Gritton drflags &= ~(PD_LIST_SLOCKED | PD_LIST_XLOCKED); 2713fd7a8150SMike Barcroft 2714413628a7SBjoern A. Zeeb /* 2715413628a7SBjoern A. Zeeb * Reparent the newly attached process to this jail. 2716413628a7SBjoern A. Zeeb */ 2717b38ff370SJamie Gritton p = td->td_proc; 2718413628a7SBjoern A. Zeeb error = cpuset_setproc_update_set(p, pr->pr_cpuset); 2719413628a7SBjoern A. Zeeb if (error) 2720b38ff370SJamie Gritton goto e_revert_osd; 2721413628a7SBjoern A. Zeeb 2722cb05b60aSAttilio Rao vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY); 2723fd7a8150SMike Barcroft if ((error = change_dir(pr->pr_root, td)) != 0) 2724fd7a8150SMike Barcroft goto e_unlock; 2725fd7a8150SMike Barcroft #ifdef MAC 272630d239bcSRobert Watson if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root))) 2727fd7a8150SMike Barcroft goto e_unlock; 2728fd7a8150SMike Barcroft #endif 2729b249ce48SMateusz Guzik VOP_UNLOCK(pr->pr_root); 2730d4380c0cSJamie Gritton if ((error = pwd_chroot_chdir(td, pr->pr_root))) 27315050aa86SKonstantin Belousov goto e_revert_osd; 2732fd7a8150SMike Barcroft 2733fd7a8150SMike Barcroft newcred = crget(); 2734fd7a8150SMike Barcroft PROC_LOCK(p); 27351fb6767dSJamie Gritton oldcred = crcopysafe(p, newcred); 273669c4ee54SJohn Baldwin newcred->cr_prison = pr; 2737daf63fd2SMateusz Guzik proc_set_cred(p, newcred); 27381fb6767dSJamie Gritton setsugid(p); 2739097055e2SEdward Tomasz Napierala #ifdef RACCT 2740097055e2SEdward Tomasz Napierala racct_proc_ucred_changed(p, oldcred, newcred); 2741f87beb93SAndriy Gapon crhold(newcred); 2742f87beb93SAndriy Gapon #endif 2743f87beb93SAndriy Gapon PROC_UNLOCK(p); 2744f87beb93SAndriy Gapon #ifdef RCTL 2745f87beb93SAndriy Gapon rctl_proc_ucred_changed(p, newcred); 2746f87beb93SAndriy Gapon crfree(newcred); 2747097055e2SEdward Tomasz Napierala #endif 27485ecb5444SMateusz Guzik prison_proc_relink(oldcred->cr_prison, pr, p); 2749f7496dcaSJamie Gritton prison_deref(oldcred->cr_prison, drflags); 2750fd7a8150SMike Barcroft crfree(oldcred); 2751cc7b7306SJamie Gritton 2752cc7b7306SJamie Gritton /* 2753cc7b7306SJamie Gritton * If the prison was killed while changing credentials, die along 2754cc7b7306SJamie Gritton * with it. 2755cc7b7306SJamie Gritton */ 2756cc7b7306SJamie Gritton if (!prison_isalive(pr)) { 2757cc7b7306SJamie Gritton PROC_LOCK(p); 2758cc7b7306SJamie Gritton kern_psignal(p, SIGKILL); 2759cc7b7306SJamie Gritton PROC_UNLOCK(p); 2760cc7b7306SJamie Gritton } 2761cc7b7306SJamie Gritton 2762fd7a8150SMike Barcroft return (0); 27631fb6767dSJamie Gritton 2764fd7a8150SMike Barcroft e_unlock: 2765b249ce48SMateusz Guzik VOP_UNLOCK(pr->pr_root); 2766b38ff370SJamie Gritton e_revert_osd: 2767b38ff370SJamie Gritton /* Tell modules this thread is still in its old jail after all. */ 27687f4e7248SJamie Gritton sx_slock(&allprison_lock); 2769f7496dcaSJamie Gritton drflags |= PD_LIST_SLOCKED; 27701fb6767dSJamie Gritton (void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td); 2771f7496dcaSJamie Gritton prison_deref(pr, drflags); 2772fd7a8150SMike Barcroft return (error); 2773fd7a8150SMike Barcroft } 2774fd7a8150SMike Barcroft 2775fd7a8150SMike Barcroft /* 2776fd7a8150SMike Barcroft * Returns a locked prison instance, or NULL on failure. 2777fd7a8150SMike Barcroft */ 277854b369c1SPawel Jakub Dawidek struct prison * 2779fd7a8150SMike Barcroft prison_find(int prid) 2780fd7a8150SMike Barcroft { 2781fd7a8150SMike Barcroft struct prison *pr; 2782fd7a8150SMike Barcroft 2783dc68a633SPawel Jakub Dawidek sx_assert(&allprison_lock, SX_LOCKED); 2784b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 2785f7496dcaSJamie Gritton if (pr->pr_id < prid) 2786f7496dcaSJamie Gritton continue; 27877de883c8SJamie Gritton if (pr->pr_id > prid) 27887de883c8SJamie Gritton break; 2789f7496dcaSJamie Gritton KASSERT(prison_isvalid(pr), ("Found invalid prison %p", pr)); 2790f7496dcaSJamie Gritton mtx_lock(&pr->pr_mtx); 2791f7496dcaSJamie Gritton return (pr); 2792fd7a8150SMike Barcroft } 2793fd7a8150SMike Barcroft return (NULL); 2794fd7a8150SMike Barcroft } 2795fd7a8150SMike Barcroft 2796b38ff370SJamie Gritton /* 27970304c731SJamie Gritton * Find a prison that is a descendant of mypr. Returns a locked prison or NULL. 2798b38ff370SJamie Gritton */ 2799b38ff370SJamie Gritton struct prison * 28000304c731SJamie Gritton prison_find_child(struct prison *mypr, int prid) 2801b38ff370SJamie Gritton { 28020304c731SJamie Gritton struct prison *pr; 28030304c731SJamie Gritton int descend; 2804b38ff370SJamie Gritton 2805b38ff370SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 28060304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 28070304c731SJamie Gritton if (pr->pr_id == prid) { 2808f7496dcaSJamie Gritton KASSERT(prison_isvalid(pr), 2809f7496dcaSJamie Gritton ("Found invalid prison %p", pr)); 28100304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 28110304c731SJamie Gritton return (pr); 28120304c731SJamie Gritton } 28130304c731SJamie Gritton } 28140304c731SJamie Gritton return (NULL); 28150304c731SJamie Gritton } 28160304c731SJamie Gritton 28170304c731SJamie Gritton /* 28180304c731SJamie Gritton * Look for the name relative to mypr. Returns a locked prison or NULL. 28190304c731SJamie Gritton */ 28200304c731SJamie Gritton struct prison * 28210304c731SJamie Gritton prison_find_name(struct prison *mypr, const char *name) 28220304c731SJamie Gritton { 28230304c731SJamie Gritton struct prison *pr, *deadpr; 28240304c731SJamie Gritton size_t mylen; 28250304c731SJamie Gritton int descend; 28260304c731SJamie Gritton 28270304c731SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 28280304c731SJamie Gritton mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1; 2829b38ff370SJamie Gritton deadpr = NULL; 28300304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 28310304c731SJamie Gritton if (!strcmp(pr->pr_name + mylen, name)) { 2832f7496dcaSJamie Gritton KASSERT(prison_isvalid(pr), 2833f7496dcaSJamie Gritton ("Found invalid prison %p", pr)); 2834f7496dcaSJamie Gritton if (prison_isalive(pr)) { 2835b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 2836b38ff370SJamie Gritton return (pr); 2837f7496dcaSJamie Gritton } 2838b38ff370SJamie Gritton deadpr = pr; 2839b38ff370SJamie Gritton } 2840b38ff370SJamie Gritton } 28410304c731SJamie Gritton /* There was no valid prison - perhaps there was a dying one. */ 2842f7496dcaSJamie Gritton if (deadpr != NULL) 2843b38ff370SJamie Gritton mtx_lock(&deadpr->pr_mtx); 2844b38ff370SJamie Gritton return (deadpr); 2845b38ff370SJamie Gritton } 2846b38ff370SJamie Gritton 2847b38ff370SJamie Gritton /* 28480fe74ae6SJamie Gritton * See if a prison has the specific flag set. The prison should be locked, 28490fe74ae6SJamie Gritton * unless checking for flags that are only set at jail creation (such as 28500fe74ae6SJamie Gritton * PR_IP4 and PR_IP6), or only the single bit is examined, without regard 28510fe74ae6SJamie Gritton * to any other prison data. 28520304c731SJamie Gritton */ 28530b0ae2e4SMina Galić bool 28540304c731SJamie Gritton prison_flag(struct ucred *cred, unsigned flag) 28550304c731SJamie Gritton { 28560304c731SJamie Gritton 28570b0ae2e4SMina Galić return ((cred->cr_prison->pr_flags & flag) != 0); 28580304c731SJamie Gritton } 28590304c731SJamie Gritton 28600b0ae2e4SMina Galić /* 28610b0ae2e4SMina Galić * See if a prison has the specific allow flag set. 28620b0ae2e4SMina Galić * The prison *should* be locked, or only a single bit is examined, without 28630b0ae2e4SMina Galić * regard to any other prison data. 28640b0ae2e4SMina Galić */ 28650b0ae2e4SMina Galić bool 28660304c731SJamie Gritton prison_allow(struct ucred *cred, unsigned flag) 28670304c731SJamie Gritton { 28680304c731SJamie Gritton 28690fe74ae6SJamie Gritton return ((cred->cr_prison->pr_allow & flag) != 0); 28700304c731SJamie Gritton } 28710304c731SJamie Gritton 28720304c731SJamie Gritton /* 2873effad35eSJamie Gritton * Hold a prison reference, by incrementing pr_ref. It is generally 2874effad35eSJamie Gritton * an error to hold a prison that does not already have a reference. 2875effad35eSJamie Gritton * A prison record will remain valid as long as it has at least one 2876effad35eSJamie Gritton * reference, and will not be removed as long as either the prison 2877effad35eSJamie Gritton * mutex or the allprison lock is held (allprison_lock may be shared). 2878effad35eSJamie Gritton */ 2879effad35eSJamie Gritton void 2880effad35eSJamie Gritton prison_hold_locked(struct prison *pr) 2881effad35eSJamie Gritton { 2882effad35eSJamie Gritton 28836754ae25SJamie Gritton /* Locking is no longer required. */ 28846754ae25SJamie Gritton prison_hold(pr); 2885effad35eSJamie Gritton } 2886effad35eSJamie Gritton 2887effad35eSJamie Gritton void 2888effad35eSJamie Gritton prison_hold(struct prison *pr) 2889effad35eSJamie Gritton { 28906754ae25SJamie Gritton #ifdef INVARIANTS 28916754ae25SJamie Gritton int was_valid = refcount_acquire_if_not_zero(&pr->pr_ref); 2892effad35eSJamie Gritton 28936754ae25SJamie Gritton KASSERT(was_valid, 28946754ae25SJamie Gritton ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id)); 28956754ae25SJamie Gritton #else 28966754ae25SJamie Gritton refcount_acquire(&pr->pr_ref); 28976754ae25SJamie Gritton #endif 2898effad35eSJamie Gritton } 2899effad35eSJamie Gritton 2900effad35eSJamie Gritton /* 2901effad35eSJamie Gritton * Remove a prison reference. If that was the last reference, the 2902f7496dcaSJamie Gritton * prison will be removed (at a later time). 2903b38ff370SJamie Gritton */ 290491421ba2SRobert Watson void 29051ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr) 290691421ba2SRobert Watson { 290791421ba2SRobert Watson 29081ba4a712SPawel Jakub Dawidek mtx_assert(&pr->pr_mtx, MA_OWNED); 2909effad35eSJamie Gritton /* 2910f7496dcaSJamie Gritton * Locking is no longer required, but unlock because the caller 2911f7496dcaSJamie Gritton * expects it. 2912effad35eSJamie Gritton */ 2913f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 2914f7496dcaSJamie Gritton prison_free(pr); 2915effad35eSJamie Gritton } 2916c2cda609SPawel Jakub Dawidek 29171ba4a712SPawel Jakub Dawidek void 29181ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr) 29191ba4a712SPawel Jakub Dawidek { 29201ba4a712SPawel Jakub Dawidek 29216754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 29226754ae25SJamie Gritton ("Trying to free dead prison %p (jid=%d).", 29236754ae25SJamie Gritton pr, pr->pr_id)); 2924f7496dcaSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_ref)) { 2925f7496dcaSJamie Gritton /* 2926f7496dcaSJamie Gritton * Don't remove the last reference in this context, 2927f7496dcaSJamie Gritton * in case there are locks held. 2928f7496dcaSJamie Gritton */ 29298cf955f3SMark Johnston taskqueue_enqueue(taskqueue_jail_remove, &pr->pr_task); 2930f7496dcaSJamie Gritton } 2931f7496dcaSJamie Gritton } 2932f7496dcaSJamie Gritton 2933f7496dcaSJamie Gritton static void 2934f7496dcaSJamie Gritton prison_free_not_last(struct prison *pr) 2935f7496dcaSJamie Gritton { 2936f7496dcaSJamie Gritton #ifdef INVARIANTS 2937f7496dcaSJamie Gritton int lastref; 2938f7496dcaSJamie Gritton 2939f7496dcaSJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 2940f7496dcaSJamie Gritton ("Trying to free dead prison %p (jid=%d).", 2941f7496dcaSJamie Gritton pr, pr->pr_id)); 2942f7496dcaSJamie Gritton lastref = refcount_release(&pr->pr_ref); 2943f7496dcaSJamie Gritton KASSERT(!lastref, 2944f7496dcaSJamie Gritton ("prison_free_not_last freed last ref on prison %p (jid=%d).", 2945f7496dcaSJamie Gritton pr, pr->pr_id)); 2946f7496dcaSJamie Gritton #else 2947ee9b37aeSMateusz Guzik refcount_release(&pr->pr_ref); 2948f7496dcaSJamie Gritton #endif 29491ba4a712SPawel Jakub Dawidek } 29501ba4a712SPawel Jakub Dawidek 295173d9e52dSJamie Gritton /* 2952f171938cSGordon Bergling * Hold a prison for user visibility, by incrementing pr_uref. 2953effad35eSJamie Gritton * It is generally an error to hold a prison that isn't already 295449a033d8SGordon Bergling * user-visible, except through the jail system calls. It is also 2955effad35eSJamie Gritton * an error to hold an invalid prison. A prison record will remain 2956effad35eSJamie Gritton * alive as long as it has at least one user reference, and will not 2957f7496dcaSJamie Gritton * be set to the dying state until the prison mutex and allprison_lock 2958f7496dcaSJamie Gritton * are both freed. 2959effad35eSJamie Gritton */ 2960effad35eSJamie Gritton void 2961effad35eSJamie Gritton prison_proc_hold(struct prison *pr) 2962effad35eSJamie Gritton { 29636754ae25SJamie Gritton #ifdef INVARIANTS 29646754ae25SJamie Gritton int was_alive = refcount_acquire_if_not_zero(&pr->pr_uref); 2965effad35eSJamie Gritton 29666754ae25SJamie Gritton KASSERT(was_alive, 2967effad35eSJamie Gritton ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id)); 29686754ae25SJamie Gritton #else 29696754ae25SJamie Gritton refcount_acquire(&pr->pr_uref); 29706754ae25SJamie Gritton #endif 2971effad35eSJamie Gritton } 2972effad35eSJamie Gritton 2973effad35eSJamie Gritton /* 2974effad35eSJamie Gritton * Remove a prison user reference. If it was the last reference, the 2975effad35eSJamie Gritton * prison will be considered "dying", and may be removed once all of 2976effad35eSJamie Gritton * its references are dropped. 2977effad35eSJamie Gritton */ 2978effad35eSJamie Gritton void 2979effad35eSJamie Gritton prison_proc_free(struct prison *pr) 2980effad35eSJamie Gritton { 2981effad35eSJamie Gritton 29826754ae25SJamie Gritton /* 29836754ae25SJamie Gritton * Locking is only required when releasing the last reference. 29846754ae25SJamie Gritton * This allows assurance that a locked prison will remain alive 29856754ae25SJamie Gritton * until it is unlocked. 29866754ae25SJamie Gritton */ 29876754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_uref) > 0, 2988effad35eSJamie Gritton ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id)); 2989195cd6aeSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_uref)) { 2990effad35eSJamie Gritton /* 2991effad35eSJamie Gritton * Don't remove the last user reference in this context, 2992effad35eSJamie Gritton * which is expected to be a process that is not only locked, 2993effad35eSJamie Gritton * but also half dead. Add a reference so any calls to 2994effad35eSJamie Gritton * prison_free() won't re-submit the task. 2995effad35eSJamie Gritton */ 2996f7496dcaSJamie Gritton prison_hold(pr); 29971158508aSJamie Gritton mtx_lock(&pr->pr_mtx); 29981158508aSJamie Gritton KASSERT(!(pr->pr_flags & PR_COMPLETE_PROC), 29991158508aSJamie Gritton ("Redundant last reference in prison_proc_free (jid=%d)", 30001158508aSJamie Gritton pr->pr_id)); 30011158508aSJamie Gritton pr->pr_flags |= PR_COMPLETE_PROC; 30021158508aSJamie Gritton mtx_unlock(&pr->pr_mtx); 30038cf955f3SMark Johnston taskqueue_enqueue(taskqueue_jail_remove, &pr->pr_task); 3004effad35eSJamie Gritton } 3005effad35eSJamie Gritton } 3006effad35eSJamie Gritton 3007c861373bSJamie Gritton static void 3008c861373bSJamie Gritton prison_proc_free_not_last(struct prison *pr) 3009c861373bSJamie Gritton { 3010c861373bSJamie Gritton #ifdef INVARIANTS 3011c861373bSJamie Gritton int lastref; 3012c861373bSJamie Gritton 3013c861373bSJamie Gritton KASSERT(refcount_load(&pr->pr_uref) > 0, 3014c861373bSJamie Gritton ("Trying to free dead prison %p (jid=%d).", 3015c861373bSJamie Gritton pr, pr->pr_id)); 3016c861373bSJamie Gritton lastref = refcount_release(&pr->pr_uref); 3017c861373bSJamie Gritton KASSERT(!lastref, 3018c861373bSJamie Gritton ("prison_proc_free_not_last freed last uref on prison %p (jid=%d).", 3019c861373bSJamie Gritton pr, pr->pr_id)); 3020c861373bSJamie Gritton #else 3021c861373bSJamie Gritton refcount_release(&pr->pr_uref); 3022c861373bSJamie Gritton #endif 3023c861373bSJamie Gritton } 3024c861373bSJamie Gritton 30255ecb5444SMateusz Guzik void 30265ecb5444SMateusz Guzik prison_proc_link(struct prison *pr, struct proc *p) 30275ecb5444SMateusz Guzik { 30285ecb5444SMateusz Guzik 30295ecb5444SMateusz Guzik sx_assert(&allproc_lock, SA_XLOCKED); 30305ecb5444SMateusz Guzik LIST_INSERT_HEAD(&pr->pr_proclist, p, p_jaillist); 30315ecb5444SMateusz Guzik } 30325ecb5444SMateusz Guzik 30335ecb5444SMateusz Guzik void 30345ecb5444SMateusz Guzik prison_proc_unlink(struct prison *pr, struct proc *p) 30355ecb5444SMateusz Guzik { 30365ecb5444SMateusz Guzik 30375ecb5444SMateusz Guzik sx_assert(&allproc_lock, SA_XLOCKED); 30385ecb5444SMateusz Guzik LIST_REMOVE(p, p_jaillist); 30395ecb5444SMateusz Guzik } 30405ecb5444SMateusz Guzik 30415ecb5444SMateusz Guzik static void 30425ecb5444SMateusz Guzik prison_proc_relink(struct prison *opr, struct prison *npr, struct proc *p) 30435ecb5444SMateusz Guzik { 30445ecb5444SMateusz Guzik 30455ecb5444SMateusz Guzik sx_xlock(&allproc_lock); 30465ecb5444SMateusz Guzik prison_proc_unlink(opr, p); 30475ecb5444SMateusz Guzik prison_proc_link(npr, p); 30485ecb5444SMateusz Guzik sx_xunlock(&allproc_lock); 30495ecb5444SMateusz Guzik } 30505ecb5444SMateusz Guzik 3051effad35eSJamie Gritton /* 305273d9e52dSJamie Gritton * Complete a call to either prison_free or prison_proc_free. 305373d9e52dSJamie Gritton */ 3054c2cda609SPawel Jakub Dawidek static void 3055c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending) 3056c2cda609SPawel Jakub Dawidek { 305773d9e52dSJamie Gritton struct prison *pr = context; 3058f7496dcaSJamie Gritton int drflags; 3059b38ff370SJamie Gritton 3060effad35eSJamie Gritton /* 30611158508aSJamie Gritton * This could be called to release the last reference, or the last 30621158508aSJamie Gritton * user reference (plus the reference held in prison_proc_free). 3063effad35eSJamie Gritton */ 3064f7496dcaSJamie Gritton drflags = prison_lock_xlock(pr, PD_DEREF); 30651158508aSJamie Gritton if (pr->pr_flags & PR_COMPLETE_PROC) { 30661158508aSJamie Gritton pr->pr_flags &= ~PR_COMPLETE_PROC; 3067f7496dcaSJamie Gritton drflags |= PD_DEUREF; 30681158508aSJamie Gritton } 3069f7496dcaSJamie Gritton prison_deref(pr, drflags); 3070b38ff370SJamie Gritton } 3071b38ff370SJamie Gritton 30725ecb5444SMateusz Guzik static void 30735ecb5444SMateusz Guzik prison_kill_processes_cb(struct proc *p, void *arg __unused) 30745ecb5444SMateusz Guzik { 30755ecb5444SMateusz Guzik 30765ecb5444SMateusz Guzik kern_psignal(p, SIGKILL); 30775ecb5444SMateusz Guzik } 30785ecb5444SMateusz Guzik 30795ecb5444SMateusz Guzik /* 30805ecb5444SMateusz Guzik * Note the iteration does not guarantee acting on all processes. 30815ecb5444SMateusz Guzik * Most notably there may be fork or jail_attach in progress. 30825ecb5444SMateusz Guzik */ 30835ecb5444SMateusz Guzik void 30845ecb5444SMateusz Guzik prison_proc_iterate(struct prison *pr, void (*cb)(struct proc *, void *), 30855ecb5444SMateusz Guzik void *cbarg) 30865ecb5444SMateusz Guzik { 30875ecb5444SMateusz Guzik struct prison *ppr; 30885ecb5444SMateusz Guzik struct proc *p; 30895ecb5444SMateusz Guzik 30905ecb5444SMateusz Guzik if (atomic_load_int(&pr->pr_childcount) == 0) { 30915ecb5444SMateusz Guzik sx_slock(&allproc_lock); 30925ecb5444SMateusz Guzik LIST_FOREACH(p, &pr->pr_proclist, p_jaillist) { 30935ecb5444SMateusz Guzik if (p->p_state == PRS_NEW) 30945ecb5444SMateusz Guzik continue; 30955ecb5444SMateusz Guzik PROC_LOCK(p); 30965ecb5444SMateusz Guzik cb(p, cbarg); 30975ecb5444SMateusz Guzik PROC_UNLOCK(p); 30985ecb5444SMateusz Guzik } 30995ecb5444SMateusz Guzik sx_sunlock(&allproc_lock); 31005ecb5444SMateusz Guzik if (atomic_load_int(&pr->pr_childcount) == 0) 31015ecb5444SMateusz Guzik return; 31025ecb5444SMateusz Guzik /* 31035ecb5444SMateusz Guzik * Some jails popped up during the iteration, fall through to a 31045ecb5444SMateusz Guzik * system-wide search. 31055ecb5444SMateusz Guzik */ 31065ecb5444SMateusz Guzik } 31075ecb5444SMateusz Guzik 31085ecb5444SMateusz Guzik sx_slock(&allproc_lock); 31095ecb5444SMateusz Guzik FOREACH_PROC_IN_SYSTEM(p) { 31105ecb5444SMateusz Guzik PROC_LOCK(p); 31115ecb5444SMateusz Guzik if (p->p_state != PRS_NEW && p->p_ucred != NULL) { 3112831531a8SKonstantin Belousov for (ppr = p->p_ucred->cr_prison; ppr != NULL; 31135ecb5444SMateusz Guzik ppr = ppr->pr_parent) { 31145ecb5444SMateusz Guzik if (ppr == pr) { 31155ecb5444SMateusz Guzik cb(p, cbarg); 31165ecb5444SMateusz Guzik break; 31175ecb5444SMateusz Guzik } 31185ecb5444SMateusz Guzik } 31195ecb5444SMateusz Guzik } 31205ecb5444SMateusz Guzik PROC_UNLOCK(p); 31215ecb5444SMateusz Guzik } 31225ecb5444SMateusz Guzik sx_sunlock(&allproc_lock); 31235ecb5444SMateusz Guzik } 31245ecb5444SMateusz Guzik 3125b38ff370SJamie Gritton /* 3126effad35eSJamie Gritton * Remove a prison reference and/or user reference (usually). 3127effad35eSJamie Gritton * This assumes context that allows sleeping (for allprison_lock), 3128effad35eSJamie Gritton * with no non-sleeping locks held, except perhaps the prison itself. 3129effad35eSJamie Gritton * If there are no more references, release and delist the prison. 3130effad35eSJamie Gritton * On completion, the prison lock and the allprison lock are both 3131effad35eSJamie Gritton * unlocked. 3132b38ff370SJamie Gritton */ 3133b38ff370SJamie Gritton static void 3134b38ff370SJamie Gritton prison_deref(struct prison *pr, int flags) 3135b38ff370SJamie Gritton { 31366e1d1bfcSJamie Gritton struct prisonlist freeprison; 3137c861373bSJamie Gritton struct prison *killpr, *rpr, *ppr, *tpr; 3138c2cda609SPawel Jakub Dawidek 3139c861373bSJamie Gritton killpr = NULL; 31406e1d1bfcSJamie Gritton TAILQ_INIT(&freeprison); 31416e1d1bfcSJamie Gritton /* 31426e1d1bfcSJamie Gritton * Release this prison as requested, which may cause its parent 31436e1d1bfcSJamie Gritton * to be released, and then maybe its grandparent, etc. 31446e1d1bfcSJamie Gritton */ 31450304c731SJamie Gritton for (;;) { 3146c861373bSJamie Gritton if (flags & PD_KILL) { 3147c861373bSJamie Gritton /* Kill the prison and its descendents. */ 3148589e4c1dSJamie Gritton KASSERT(pr != &prison0, 3149589e4c1dSJamie Gritton ("prison_deref trying to kill prison0")); 3150c861373bSJamie Gritton if (!(flags & PD_DEREF)) { 3151c861373bSJamie Gritton prison_hold(pr); 3152c861373bSJamie Gritton flags |= PD_DEREF; 3153c861373bSJamie Gritton } 3154c861373bSJamie Gritton flags = prison_lock_xlock(pr, flags); 3155c861373bSJamie Gritton prison_deref_kill(pr, &freeprison); 3156c861373bSJamie Gritton } 3157e6d5cb63SJamie Gritton if (flags & PD_DEUREF) { 3158f7496dcaSJamie Gritton /* Drop a user reference. */ 31596754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_uref) > 0, 316073d9e52dSJamie Gritton ("prison_deref PD_DEUREF on a dead prison (jid=%d)", 316173d9e52dSJamie Gritton pr->pr_id)); 3162f7496dcaSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_uref)) { 3163f7496dcaSJamie Gritton if (!(flags & PD_DEREF)) { 3164f7496dcaSJamie Gritton prison_hold(pr); 3165f7496dcaSJamie Gritton flags |= PD_DEREF; 3166f7496dcaSJamie Gritton } 3167f7496dcaSJamie Gritton flags = prison_lock_xlock(pr, flags); 31681158508aSJamie Gritton if (refcount_release(&pr->pr_uref) && 31691158508aSJamie Gritton pr->pr_state == PRISON_STATE_ALIVE) { 3170f7496dcaSJamie Gritton /* 3171f7496dcaSJamie Gritton * When the last user references goes, 3172f7496dcaSJamie Gritton * this becomes a dying prison. 3173f7496dcaSJamie Gritton */ 3174f7496dcaSJamie Gritton KASSERT( 3175f7496dcaSJamie Gritton refcount_load(&prison0.pr_uref) > 0, 31766754ae25SJamie Gritton ("prison0 pr_uref=0")); 31771158508aSJamie Gritton pr->pr_state = PRISON_STATE_DYING; 3178f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 3179f7496dcaSJamie Gritton flags &= ~PD_LOCKED; 3180a9f7455cSJamie Gritton prison_cleanup(pr); 3181f7496dcaSJamie Gritton } 3182f7496dcaSJamie Gritton } 3183f7496dcaSJamie Gritton } 3184c861373bSJamie Gritton if (flags & PD_KILL) { 3185c861373bSJamie Gritton /* 3186c861373bSJamie Gritton * Any remaining user references are probably processes 3187c861373bSJamie Gritton * that need to be killed, either in this prison or its 3188c861373bSJamie Gritton * descendants. 3189c861373bSJamie Gritton */ 3190c861373bSJamie Gritton if (refcount_load(&pr->pr_uref) > 0) 3191c861373bSJamie Gritton killpr = pr; 3192589e4c1dSJamie Gritton /* Make sure the parent prison doesn't get killed. */ 3193589e4c1dSJamie Gritton flags &= ~PD_KILL; 3194c861373bSJamie Gritton } 319573d9e52dSJamie Gritton if (flags & PD_DEREF) { 3196f7496dcaSJamie Gritton /* Drop a reference. */ 31976754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 319873d9e52dSJamie Gritton ("prison_deref PD_DEREF on a dead prison (jid=%d)", 319973d9e52dSJamie Gritton pr->pr_id)); 3200f7496dcaSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_ref)) { 3201f7496dcaSJamie Gritton flags = prison_lock_xlock(pr, flags); 3202f7496dcaSJamie Gritton if (refcount_release(&pr->pr_ref)) { 3203cc5fd8c7SJamie Gritton /* 3204f7496dcaSJamie Gritton * When the last reference goes, 3205f7496dcaSJamie Gritton * unlink the prison and set it aside. 3206cc5fd8c7SJamie Gritton */ 3207f7496dcaSJamie Gritton KASSERT( 3208f7496dcaSJamie Gritton refcount_load(&pr->pr_uref) == 0, 3209f7496dcaSJamie Gritton ("prison_deref: last ref, " 3210f7496dcaSJamie Gritton "but still has %d urefs (jid=%d)", 3211f7496dcaSJamie Gritton pr->pr_uref, pr->pr_id)); 3212f7496dcaSJamie Gritton KASSERT( 3213f7496dcaSJamie Gritton refcount_load(&prison0.pr_ref) != 0, 3214f7496dcaSJamie Gritton ("prison0 pr_ref=0")); 32151158508aSJamie Gritton pr->pr_state = PRISON_STATE_INVALID; 3216b38ff370SJamie Gritton TAILQ_REMOVE(&allprison, pr, pr_list); 32170304c731SJamie Gritton LIST_REMOVE(pr, pr_sibling); 3218f7496dcaSJamie Gritton TAILQ_INSERT_TAIL(&freeprison, pr, 3219f7496dcaSJamie Gritton pr_list); 3220f7496dcaSJamie Gritton for (ppr = pr->pr_parent; 3221f7496dcaSJamie Gritton ppr != NULL; 3222f7496dcaSJamie Gritton ppr = ppr->pr_parent) 3223f7496dcaSJamie Gritton ppr->pr_childcount--; 3224f7496dcaSJamie Gritton /* 3225f7496dcaSJamie Gritton * Removing a prison frees references 3226f7496dcaSJamie Gritton * from its parent. 3227f7496dcaSJamie Gritton */ 32288c75c15dSMark Johnston ppr = pr->pr_parent; 32298c75c15dSMark Johnston pr->pr_parent = NULL; 3230f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 32318c75c15dSMark Johnston 32328c75c15dSMark Johnston pr = ppr; 3233f7496dcaSJamie Gritton flags &= ~PD_LOCKED; 32346e1d1bfcSJamie Gritton flags |= PD_DEREF | PD_DEUREF; 3235f7496dcaSJamie Gritton continue; 3236f7496dcaSJamie Gritton } 3237f7496dcaSJamie Gritton } 3238f7496dcaSJamie Gritton } 3239f7496dcaSJamie Gritton break; 32406e1d1bfcSJamie Gritton } 32416e1d1bfcSJamie Gritton 32426e1d1bfcSJamie Gritton /* Release all the prison locks. */ 3243f7496dcaSJamie Gritton if (flags & PD_LOCKED) 3244f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 32456e1d1bfcSJamie Gritton if (flags & PD_LIST_SLOCKED) 32466e1d1bfcSJamie Gritton sx_sunlock(&allprison_lock); 32476e1d1bfcSJamie Gritton else if (flags & PD_LIST_XLOCKED) 32486e1d1bfcSJamie Gritton sx_xunlock(&allprison_lock); 32496e1d1bfcSJamie Gritton 3250c861373bSJamie Gritton /* Kill any processes attached to a killed prison. */ 32515ecb5444SMateusz Guzik if (killpr != NULL) 32525ecb5444SMateusz Guzik prison_proc_iterate(killpr, prison_kill_processes_cb, NULL); 3253c861373bSJamie Gritton 32546e1d1bfcSJamie Gritton /* 32556e1d1bfcSJamie Gritton * Finish removing any unreferenced prisons, which couldn't happen 32566e1d1bfcSJamie Gritton * while allprison_lock was held (to avoid a LOR on vrele). 32576e1d1bfcSJamie Gritton */ 32586e1d1bfcSJamie Gritton TAILQ_FOREACH_SAFE(rpr, &freeprison, pr_list, tpr) { 32596e1d1bfcSJamie Gritton #ifdef VIMAGE 32608c75c15dSMark Johnston if (rpr->pr_flags & PR_VNET) 32616e1d1bfcSJamie Gritton vnet_destroy(rpr->pr_vnet); 32626e1d1bfcSJamie Gritton #endif 32636e1d1bfcSJamie Gritton if (rpr->pr_root != NULL) 32646e1d1bfcSJamie Gritton vrele(rpr->pr_root); 32656e1d1bfcSJamie Gritton mtx_destroy(&rpr->pr_mtx); 32666e1d1bfcSJamie Gritton #ifdef INET 3267eb8dcdeaSGleb Smirnoff prison_ip_free(rpr->pr_addrs[PR_INET]); 32686e1d1bfcSJamie Gritton #endif 32696e1d1bfcSJamie Gritton #ifdef INET6 3270eb8dcdeaSGleb Smirnoff prison_ip_free(rpr->pr_addrs[PR_INET6]); 32716e1d1bfcSJamie Gritton #endif 32726e1d1bfcSJamie Gritton if (rpr->pr_cpuset != NULL) 32736e1d1bfcSJamie Gritton cpuset_rel(rpr->pr_cpuset); 32746e1d1bfcSJamie Gritton osd_jail_exit(rpr); 32756e1d1bfcSJamie Gritton #ifdef RACCT 32766e1d1bfcSJamie Gritton if (racct_enable) 32776e1d1bfcSJamie Gritton prison_racct_detach(rpr); 32786e1d1bfcSJamie Gritton #endif 3279f7496dcaSJamie Gritton TAILQ_REMOVE(&freeprison, rpr, pr_list); 32806e1d1bfcSJamie Gritton free(rpr, M_PRISON); 32810304c731SJamie Gritton } 3282b3059e09SRobert Watson } 3283b3059e09SRobert Watson 3284413628a7SBjoern A. Zeeb /* 3285c861373bSJamie Gritton * Kill the prison and its descendants. Mark them as dying, clear the 3286c861373bSJamie Gritton * persist flag, and call module remove methods. 3287c861373bSJamie Gritton */ 3288c861373bSJamie Gritton static void 3289c861373bSJamie Gritton prison_deref_kill(struct prison *pr, struct prisonlist *freeprison) 3290c861373bSJamie Gritton { 3291c861373bSJamie Gritton struct prison *cpr, *ppr, *rpr; 3292c861373bSJamie Gritton bool descend; 3293c861373bSJamie Gritton 3294c861373bSJamie Gritton /* 3295c861373bSJamie Gritton * Unlike the descendants, the target prison can be killed 3296c861373bSJamie Gritton * even if it is currently dying. This is useful for failed 3297c861373bSJamie Gritton * creation in jail_set(2). 3298c861373bSJamie Gritton */ 3299c861373bSJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 3300c861373bSJamie Gritton ("Trying to kill dead prison %p (jid=%d).", 3301c861373bSJamie Gritton pr, pr->pr_id)); 3302c861373bSJamie Gritton refcount_acquire(&pr->pr_uref); 3303c861373bSJamie Gritton pr->pr_state = PRISON_STATE_DYING; 3304c861373bSJamie Gritton mtx_unlock(&pr->pr_mtx); 3305c861373bSJamie Gritton 3306c861373bSJamie Gritton rpr = NULL; 3307c861373bSJamie Gritton FOREACH_PRISON_DESCENDANT_PRE_POST(pr, cpr, descend) { 3308c861373bSJamie Gritton if (descend) { 3309c861373bSJamie Gritton if (!prison_isalive(cpr)) { 3310c861373bSJamie Gritton descend = false; 3311c861373bSJamie Gritton continue; 3312c861373bSJamie Gritton } 3313c861373bSJamie Gritton prison_hold(cpr); 3314c861373bSJamie Gritton prison_proc_hold(cpr); 3315c861373bSJamie Gritton mtx_lock(&cpr->pr_mtx); 3316c861373bSJamie Gritton cpr->pr_state = PRISON_STATE_DYING; 3317c861373bSJamie Gritton cpr->pr_flags |= PR_REMOVE; 3318c861373bSJamie Gritton mtx_unlock(&cpr->pr_mtx); 3319c861373bSJamie Gritton continue; 3320c861373bSJamie Gritton } 3321c861373bSJamie Gritton if (!(cpr->pr_flags & PR_REMOVE)) 3322c861373bSJamie Gritton continue; 3323a9f7455cSJamie Gritton prison_cleanup(cpr); 3324c861373bSJamie Gritton mtx_lock(&cpr->pr_mtx); 3325c861373bSJamie Gritton cpr->pr_flags &= ~PR_REMOVE; 3326c861373bSJamie Gritton if (cpr->pr_flags & PR_PERSIST) { 3327c861373bSJamie Gritton cpr->pr_flags &= ~PR_PERSIST; 3328c861373bSJamie Gritton prison_proc_free_not_last(cpr); 3329c861373bSJamie Gritton prison_free_not_last(cpr); 3330c861373bSJamie Gritton } 3331c861373bSJamie Gritton (void)refcount_release(&cpr->pr_uref); 3332c861373bSJamie Gritton if (refcount_release(&cpr->pr_ref)) { 3333c861373bSJamie Gritton /* 3334c861373bSJamie Gritton * When the last reference goes, unlink the prison 3335c861373bSJamie Gritton * and set it aside for prison_deref() to handle. 3336c861373bSJamie Gritton * Delay unlinking the sibling list to keep the loop 3337c861373bSJamie Gritton * safe. 3338c861373bSJamie Gritton */ 3339c861373bSJamie Gritton if (rpr != NULL) 3340c861373bSJamie Gritton LIST_REMOVE(rpr, pr_sibling); 3341c861373bSJamie Gritton rpr = cpr; 3342c861373bSJamie Gritton rpr->pr_state = PRISON_STATE_INVALID; 3343c861373bSJamie Gritton TAILQ_REMOVE(&allprison, rpr, pr_list); 3344c861373bSJamie Gritton TAILQ_INSERT_TAIL(freeprison, rpr, pr_list); 3345c861373bSJamie Gritton /* 3346c861373bSJamie Gritton * Removing a prison frees references from its parent. 3347c861373bSJamie Gritton */ 3348c861373bSJamie Gritton ppr = rpr->pr_parent; 3349c861373bSJamie Gritton prison_proc_free_not_last(ppr); 3350c861373bSJamie Gritton prison_free_not_last(ppr); 3351c861373bSJamie Gritton for (; ppr != NULL; ppr = ppr->pr_parent) 3352c861373bSJamie Gritton ppr->pr_childcount--; 3353c861373bSJamie Gritton } 3354c861373bSJamie Gritton mtx_unlock(&cpr->pr_mtx); 3355c861373bSJamie Gritton } 3356c861373bSJamie Gritton if (rpr != NULL) 3357c861373bSJamie Gritton LIST_REMOVE(rpr, pr_sibling); 3358c861373bSJamie Gritton 3359a9f7455cSJamie Gritton prison_cleanup(pr); 3360c861373bSJamie Gritton mtx_lock(&pr->pr_mtx); 3361c861373bSJamie Gritton if (pr->pr_flags & PR_PERSIST) { 3362c861373bSJamie Gritton pr->pr_flags &= ~PR_PERSIST; 3363c861373bSJamie Gritton prison_proc_free_not_last(pr); 3364c861373bSJamie Gritton prison_free_not_last(pr); 3365c861373bSJamie Gritton } 3366c861373bSJamie Gritton (void)refcount_release(&pr->pr_uref); 3367c861373bSJamie Gritton } 3368c861373bSJamie Gritton 3369c861373bSJamie Gritton /* 3370f7496dcaSJamie Gritton * Given the current locking state in the flags, make sure allprison_lock 3371f7496dcaSJamie Gritton * is held exclusive, and the prison is locked. Return flags indicating 3372f7496dcaSJamie Gritton * the new state. 3373f7496dcaSJamie Gritton */ 3374f7496dcaSJamie Gritton static int 3375f7496dcaSJamie Gritton prison_lock_xlock(struct prison *pr, int flags) 3376f7496dcaSJamie Gritton { 3377f7496dcaSJamie Gritton 3378f7496dcaSJamie Gritton if (!(flags & PD_LIST_XLOCKED)) { 3379f7496dcaSJamie Gritton /* 3380f7496dcaSJamie Gritton * Get allprison_lock, which may be an upgrade, 3381f7496dcaSJamie Gritton * and may require unlocking the prison. 3382f7496dcaSJamie Gritton */ 3383f7496dcaSJamie Gritton if (flags & PD_LOCKED) { 3384f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 3385f7496dcaSJamie Gritton flags &= ~PD_LOCKED; 3386f7496dcaSJamie Gritton } 3387f7496dcaSJamie Gritton if (flags & PD_LIST_SLOCKED) { 3388f7496dcaSJamie Gritton if (!sx_try_upgrade(&allprison_lock)) { 3389f7496dcaSJamie Gritton sx_sunlock(&allprison_lock); 3390f7496dcaSJamie Gritton sx_xlock(&allprison_lock); 3391f7496dcaSJamie Gritton } 3392f7496dcaSJamie Gritton flags &= ~PD_LIST_SLOCKED; 3393f7496dcaSJamie Gritton } else 3394f7496dcaSJamie Gritton sx_xlock(&allprison_lock); 3395f7496dcaSJamie Gritton flags |= PD_LIST_XLOCKED; 3396f7496dcaSJamie Gritton } 3397f7496dcaSJamie Gritton if (!(flags & PD_LOCKED)) { 3398f7496dcaSJamie Gritton /* Lock the prison mutex. */ 3399f7496dcaSJamie Gritton mtx_lock(&pr->pr_mtx); 3400f7496dcaSJamie Gritton flags |= PD_LOCKED; 3401f7496dcaSJamie Gritton } 3402f7496dcaSJamie Gritton return flags; 3403f7496dcaSJamie Gritton } 3404f7496dcaSJamie Gritton 3405f7496dcaSJamie Gritton /* 3406a9f7455cSJamie Gritton * Release a prison's resources when it starts dying (when the last user 3407a9f7455cSJamie Gritton * reference is dropped, or when it is killed). 3408a9f7455cSJamie Gritton */ 3409a9f7455cSJamie Gritton static void 3410a9f7455cSJamie Gritton prison_cleanup(struct prison *pr) 3411a9f7455cSJamie Gritton { 3412a9f7455cSJamie Gritton sx_assert(&allprison_lock, SA_XLOCKED); 3413a9f7455cSJamie Gritton mtx_assert(&pr->pr_mtx, MA_NOTOWNED); 341488175af8SRick Macklem vfs_exjail_delete(pr); 34157060da62SJamie Gritton shm_remove_prison(pr); 3416a9f7455cSJamie Gritton (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL); 3417a9f7455cSJamie Gritton } 3418a9f7455cSJamie Gritton 3419a9f7455cSJamie Gritton /* 34200fe74ae6SJamie Gritton * Set or clear a permission bit in the pr_allow field, passing restrictions 34210fe74ae6SJamie Gritton * (cleared permission) down to child jails. 34220fe74ae6SJamie Gritton */ 34230fe74ae6SJamie Gritton void 34240fe74ae6SJamie Gritton prison_set_allow(struct ucred *cred, unsigned flag, int enable) 34250fe74ae6SJamie Gritton { 34260fe74ae6SJamie Gritton struct prison *pr; 34270fe74ae6SJamie Gritton 34280fe74ae6SJamie Gritton pr = cred->cr_prison; 34290fe74ae6SJamie Gritton sx_slock(&allprison_lock); 34300fe74ae6SJamie Gritton mtx_lock(&pr->pr_mtx); 34310fe74ae6SJamie Gritton prison_set_allow_locked(pr, flag, enable); 34320fe74ae6SJamie Gritton mtx_unlock(&pr->pr_mtx); 34330fe74ae6SJamie Gritton sx_sunlock(&allprison_lock); 34340fe74ae6SJamie Gritton } 34350fe74ae6SJamie Gritton 34360fe74ae6SJamie Gritton static void 34370fe74ae6SJamie Gritton prison_set_allow_locked(struct prison *pr, unsigned flag, int enable) 34380fe74ae6SJamie Gritton { 34390fe74ae6SJamie Gritton struct prison *cpr; 34400fe74ae6SJamie Gritton int descend; 34410fe74ae6SJamie Gritton 34420fe74ae6SJamie Gritton if (enable != 0) 34430fe74ae6SJamie Gritton pr->pr_allow |= flag; 34440fe74ae6SJamie Gritton else { 34450fe74ae6SJamie Gritton pr->pr_allow &= ~flag; 34460fe74ae6SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, cpr, descend) 34470fe74ae6SJamie Gritton cpr->pr_allow &= ~flag; 34480fe74ae6SJamie Gritton } 34490fe74ae6SJamie Gritton } 34500fe74ae6SJamie Gritton 34510fe74ae6SJamie Gritton /* 3452ca04ba64SJamie Gritton * Check if a jail supports the given address family. 3453ca04ba64SJamie Gritton * 3454ca04ba64SJamie Gritton * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT 3455ca04ba64SJamie Gritton * if not. 3456ca04ba64SJamie Gritton */ 3457ca04ba64SJamie Gritton int 3458ca04ba64SJamie Gritton prison_check_af(struct ucred *cred, int af) 3459ca04ba64SJamie Gritton { 34600304c731SJamie Gritton struct prison *pr; 3461ca04ba64SJamie Gritton int error; 3462ca04ba64SJamie Gritton 3463ca04ba64SJamie Gritton KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3464ca04ba64SJamie Gritton 34650304c731SJamie Gritton pr = cred->cr_prison; 3466499650a0SJamie Gritton #ifdef VIMAGE 34676bb79563SJamie Gritton /* Prisons with their own network stack are not limited. */ 3468de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 34696bb79563SJamie Gritton return (0); 3470499650a0SJamie Gritton #endif 34716bb79563SJamie Gritton 3472ca04ba64SJamie Gritton error = 0; 3473ca04ba64SJamie Gritton switch (af) 3474ca04ba64SJamie Gritton { 3475ca04ba64SJamie Gritton #ifdef INET 3476ca04ba64SJamie Gritton case AF_INET: 34770304c731SJamie Gritton if (pr->pr_flags & PR_IP4) 34780304c731SJamie Gritton { 34790304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 3480eb8dcdeaSGleb Smirnoff if ((pr->pr_flags & PR_IP4) && 3481eb8dcdeaSGleb Smirnoff pr->pr_addrs[PR_INET] == NULL) 3482ca04ba64SJamie Gritton error = EAFNOSUPPORT; 34830304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 34840304c731SJamie Gritton } 3485ca04ba64SJamie Gritton break; 3486ca04ba64SJamie Gritton #endif 3487ca04ba64SJamie Gritton #ifdef INET6 3488ca04ba64SJamie Gritton case AF_INET6: 34890304c731SJamie Gritton if (pr->pr_flags & PR_IP6) 34900304c731SJamie Gritton { 34910304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 3492eb8dcdeaSGleb Smirnoff if ((pr->pr_flags & PR_IP6) && 3493eb8dcdeaSGleb Smirnoff pr->pr_addrs[PR_INET6] == NULL) 3494ca04ba64SJamie Gritton error = EAFNOSUPPORT; 34950304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 34960304c731SJamie Gritton } 3497ca04ba64SJamie Gritton break; 3498ca04ba64SJamie Gritton #endif 3499ca04ba64SJamie Gritton case AF_LOCAL: 3500ca04ba64SJamie Gritton case AF_ROUTE: 350104f75b98SAlexander V. Chernikov case AF_NETLINK: 3502ca04ba64SJamie Gritton break; 3503ca04ba64SJamie Gritton default: 35040304c731SJamie Gritton if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF)) 3505ca04ba64SJamie Gritton error = EAFNOSUPPORT; 3506ca04ba64SJamie Gritton } 3507ca04ba64SJamie Gritton return (error); 3508ca04ba64SJamie Gritton } 3509ca04ba64SJamie Gritton 3510ca04ba64SJamie Gritton /* 3511413628a7SBjoern A. Zeeb * Check if given address belongs to the jail referenced by cred (wrapper to 3512413628a7SBjoern A. Zeeb * prison_check_ip[46]). 3513413628a7SBjoern A. Zeeb * 35140304c731SJamie Gritton * Returns 0 if jail doesn't restrict the address family or if address belongs 35150304c731SJamie Gritton * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if 35160304c731SJamie Gritton * the jail doesn't allow the address family. IPv4 Address passed in in NBO. 3517413628a7SBjoern A. Zeeb */ 3518413628a7SBjoern A. Zeeb int 3519c83dda36SAlexander V. Chernikov prison_if(struct ucred *cred, const struct sockaddr *sa) 352075c13541SPoul-Henning Kamp { 3521413628a7SBjoern A. Zeeb #ifdef INET 3522c83dda36SAlexander V. Chernikov const struct sockaddr_in *sai; 3523413628a7SBjoern A. Zeeb #endif 3524413628a7SBjoern A. Zeeb #ifdef INET6 3525c83dda36SAlexander V. Chernikov const struct sockaddr_in6 *sai6; 3526413628a7SBjoern A. Zeeb #endif 3527b89e82ddSJamie Gritton int error; 352875c13541SPoul-Henning Kamp 3529413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3530413628a7SBjoern A. Zeeb KASSERT(sa != NULL, ("%s: sa is NULL", __func__)); 3531413628a7SBjoern A. Zeeb 3532de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE 3533de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 3534de0bd6f7SBjoern A. Zeeb return (0); 3535de0bd6f7SBjoern A. Zeeb #endif 3536de0bd6f7SBjoern A. Zeeb 3537b89e82ddSJamie Gritton error = 0; 3538413628a7SBjoern A. Zeeb switch (sa->sa_family) 3539413628a7SBjoern A. Zeeb { 3540413628a7SBjoern A. Zeeb #ifdef INET 3541413628a7SBjoern A. Zeeb case AF_INET: 3542c83dda36SAlexander V. Chernikov sai = (const struct sockaddr_in *)sa; 3543b89e82ddSJamie Gritton error = prison_check_ip4(cred, &sai->sin_addr); 3544413628a7SBjoern A. Zeeb break; 3545413628a7SBjoern A. Zeeb #endif 3546413628a7SBjoern A. Zeeb #ifdef INET6 3547413628a7SBjoern A. Zeeb case AF_INET6: 3548c83dda36SAlexander V. Chernikov sai6 = (const struct sockaddr_in6 *)sa; 3549b89e82ddSJamie Gritton error = prison_check_ip6(cred, &sai6->sin6_addr); 3550413628a7SBjoern A. Zeeb break; 3551413628a7SBjoern A. Zeeb #endif 3552413628a7SBjoern A. Zeeb default: 35530304c731SJamie Gritton if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF)) 3554b89e82ddSJamie Gritton error = EAFNOSUPPORT; 3555413628a7SBjoern A. Zeeb } 3556b89e82ddSJamie Gritton return (error); 355775c13541SPoul-Henning Kamp } 355891421ba2SRobert Watson 355991421ba2SRobert Watson /* 356091421ba2SRobert Watson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH. 356191421ba2SRobert Watson */ 356291421ba2SRobert Watson int 35639ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2) 356491421ba2SRobert Watson { 356591421ba2SRobert Watson 35660304c731SJamie Gritton return ((cred1->cr_prison == cred2->cr_prison || 35670304c731SJamie Gritton prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH); 35680304c731SJamie Gritton } 356991421ba2SRobert Watson 35700304c731SJamie Gritton /* 3571bba7a2e8SRick Macklem * For mountd/nfsd to run within a prison, it must be: 3572bba7a2e8SRick Macklem * - A vnet prison. 3573bba7a2e8SRick Macklem * - PR_ALLOW_NFSD must be set on it. 3574bba7a2e8SRick Macklem * - The root directory (pr_root) of the prison must be 3575bba7a2e8SRick Macklem * a file system mount point, so the mountd can hang 3576bba7a2e8SRick Macklem * export information on it. 357799187c3aSRick Macklem * - The prison's enforce_statfs cannot be 0, so that 357899187c3aSRick Macklem * mountd(8) can do exports. 3579bba7a2e8SRick Macklem */ 3580bba7a2e8SRick Macklem bool 3581bba7a2e8SRick Macklem prison_check_nfsd(struct ucred *cred) 3582bba7a2e8SRick Macklem { 3583bba7a2e8SRick Macklem 3584bba7a2e8SRick Macklem if (jailed_without_vnet(cred)) 3585bba7a2e8SRick Macklem return (false); 3586bba7a2e8SRick Macklem if (!prison_allow(cred, PR_ALLOW_NFSD)) 3587bba7a2e8SRick Macklem return (false); 3588bba7a2e8SRick Macklem if ((cred->cr_prison->pr_root->v_vflag & VV_ROOT) == 0) 3589bba7a2e8SRick Macklem return (false); 359099187c3aSRick Macklem if (cred->cr_prison->pr_enforce_statfs == 0) 359199187c3aSRick Macklem return (false); 3592bba7a2e8SRick Macklem return (true); 3593bba7a2e8SRick Macklem } 3594bba7a2e8SRick Macklem 3595bba7a2e8SRick Macklem /* 35960b0ae2e4SMina Galić * Return true if p2 is a child of p1, otherwise false. 35970304c731SJamie Gritton */ 35980b0ae2e4SMina Galić bool 35990304c731SJamie Gritton prison_ischild(struct prison *pr1, struct prison *pr2) 36000304c731SJamie Gritton { 36010304c731SJamie Gritton 36020304c731SJamie Gritton for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent) 36030304c731SJamie Gritton if (pr1 == pr2) 36040b0ae2e4SMina Galić return (true); 36050b0ae2e4SMina Galić return (false); 360691421ba2SRobert Watson } 360791421ba2SRobert Watson 360891421ba2SRobert Watson /* 3609f7496dcaSJamie Gritton * Return true if the prison is currently alive. A prison is alive if it 3610f7496dcaSJamie Gritton * holds user references and it isn't being removed. 361176ad42abSJamie Gritton */ 361276ad42abSJamie Gritton bool 3613eb8dcdeaSGleb Smirnoff prison_isalive(const struct prison *pr) 361476ad42abSJamie Gritton { 361576ad42abSJamie Gritton 36161158508aSJamie Gritton if (__predict_false(pr->pr_state != PRISON_STATE_ALIVE)) 3617cc7b7306SJamie Gritton return (false); 361876ad42abSJamie Gritton return (true); 361976ad42abSJamie Gritton } 362076ad42abSJamie Gritton 362176ad42abSJamie Gritton /* 362276ad42abSJamie Gritton * Return true if the prison is currently valid. A prison is valid if it has 362376ad42abSJamie Gritton * been fully created, and is not being destroyed. Note that dying prisons 3624f7496dcaSJamie Gritton * are still considered valid. Invalid prisons won't be found under normal 3625f7496dcaSJamie Gritton * circumstances, as they're only put in that state by functions that have 3626f7496dcaSJamie Gritton * an exclusive hold on allprison_lock. 362776ad42abSJamie Gritton */ 362876ad42abSJamie Gritton bool 362976ad42abSJamie Gritton prison_isvalid(struct prison *pr) 363076ad42abSJamie Gritton { 363176ad42abSJamie Gritton 36321158508aSJamie Gritton if (__predict_false(pr->pr_state == PRISON_STATE_INVALID)) 36331158508aSJamie Gritton return (false); 36346754ae25SJamie Gritton if (__predict_false(refcount_load(&pr->pr_ref) == 0)) 363576ad42abSJamie Gritton return (false); 363676ad42abSJamie Gritton return (true); 363776ad42abSJamie Gritton } 363876ad42abSJamie Gritton 363976ad42abSJamie Gritton /* 36400b0ae2e4SMina Galić * Return true if the passed credential is in a jail and that jail does not 36410b0ae2e4SMina Galić * have its own virtual network stack, otherwise false. 3642de0bd6f7SBjoern A. Zeeb */ 36430b0ae2e4SMina Galić bool 3644de0bd6f7SBjoern A. Zeeb jailed_without_vnet(struct ucred *cred) 3645de0bd6f7SBjoern A. Zeeb { 3646de0bd6f7SBjoern A. Zeeb 3647de0bd6f7SBjoern A. Zeeb if (!jailed(cred)) 36480b0ae2e4SMina Galić return (false); 3649de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE 3650de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 36510b0ae2e4SMina Galić return (false); 3652de0bd6f7SBjoern A. Zeeb #endif 3653de0bd6f7SBjoern A. Zeeb 36540b0ae2e4SMina Galić return (true); 3655de0bd6f7SBjoern A. Zeeb } 3656de0bd6f7SBjoern A. Zeeb 3657de0bd6f7SBjoern A. Zeeb /* 36587455b100SJamie Gritton * Return the correct hostname (domainname, et al) for the passed credential. 36599484d0c0SRobert Drehmel */ 3660ad1ff099SRobert Drehmel void 36619ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size) 36629484d0c0SRobert Drehmel { 366376ca6f88SJamie Gritton struct prison *pr; 36649484d0c0SRobert Drehmel 36657455b100SJamie Gritton /* 36667455b100SJamie Gritton * A NULL credential can be used to shortcut to the physical 36677455b100SJamie Gritton * system's hostname. 36687455b100SJamie Gritton */ 366976ca6f88SJamie Gritton pr = (cred != NULL) ? cred->cr_prison : &prison0; 367076ca6f88SJamie Gritton mtx_lock(&pr->pr_mtx); 3671c1f19219SJamie Gritton strlcpy(buf, pr->pr_hostname, size); 367276ca6f88SJamie Gritton mtx_unlock(&pr->pr_mtx); 36739484d0c0SRobert Drehmel } 3674fd7a8150SMike Barcroft 36757455b100SJamie Gritton void 36767455b100SJamie Gritton getcreddomainname(struct ucred *cred, char *buf, size_t size) 36777455b100SJamie Gritton { 36787455b100SJamie Gritton 36797455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 3680c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_domainname, size); 36817455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 36827455b100SJamie Gritton } 36837455b100SJamie Gritton 36847455b100SJamie Gritton void 36857455b100SJamie Gritton getcredhostuuid(struct ucred *cred, char *buf, size_t size) 36867455b100SJamie Gritton { 36877455b100SJamie Gritton 36887455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 3689c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_hostuuid, size); 36907455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 36917455b100SJamie Gritton } 36927455b100SJamie Gritton 36937455b100SJamie Gritton void 36947455b100SJamie Gritton getcredhostid(struct ucred *cred, unsigned long *hostid) 36957455b100SJamie Gritton { 36967455b100SJamie Gritton 36977455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 36987455b100SJamie Gritton *hostid = cred->cr_prison->pr_hostid; 36997455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 37007455b100SJamie Gritton } 37017455b100SJamie Gritton 37023f8bc99cSKristof Provost void 37033f8bc99cSKristof Provost getjailname(struct ucred *cred, char *name, size_t len) 37043f8bc99cSKristof Provost { 37053f8bc99cSKristof Provost 37063f8bc99cSKristof Provost mtx_lock(&cred->cr_prison->pr_mtx); 37073f8bc99cSKristof Provost strlcpy(name, cred->cr_prison->pr_name, len); 37083f8bc99cSKristof Provost mtx_unlock(&cred->cr_prison->pr_mtx); 37093f8bc99cSKristof Provost } 37103f8bc99cSKristof Provost 3711eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE 3712eb79e1c7SBjoern A. Zeeb /* 3713eb79e1c7SBjoern A. Zeeb * Determine whether the prison represented by cred owns 3714eb79e1c7SBjoern A. Zeeb * its vnet rather than having it inherited. 3715eb79e1c7SBjoern A. Zeeb * 37160b0ae2e4SMina Galić * Returns true in case the prison owns the vnet, false otherwise. 3717eb79e1c7SBjoern A. Zeeb */ 37180b0ae2e4SMina Galić bool 3719eb79e1c7SBjoern A. Zeeb prison_owns_vnet(struct ucred *cred) 3720eb79e1c7SBjoern A. Zeeb { 3721eb79e1c7SBjoern A. Zeeb 3722eb79e1c7SBjoern A. Zeeb /* 3723eb79e1c7SBjoern A. Zeeb * vnets cannot be added/removed after jail creation, 3724eb79e1c7SBjoern A. Zeeb * so no need to lock here. 3725eb79e1c7SBjoern A. Zeeb */ 37260b0ae2e4SMina Galić return ((cred->cr_prison->pr_flags & PR_VNET) != 0); 3727eb79e1c7SBjoern A. Zeeb } 3728eb79e1c7SBjoern A. Zeeb #endif 3729eb79e1c7SBjoern A. Zeeb 3730f08df373SRobert Watson /* 3731820a0de9SPawel Jakub Dawidek * Determine whether the subject represented by cred can "see" 3732820a0de9SPawel Jakub Dawidek * status of a mount point. 3733820a0de9SPawel Jakub Dawidek * Returns: 0 for permitted, ENOENT otherwise. 3734820a0de9SPawel Jakub Dawidek * XXX: This function should be called cr_canseemount() and should be 3735820a0de9SPawel Jakub Dawidek * placed in kern_prot.c. 3736f08df373SRobert Watson */ 3737f08df373SRobert Watson int 3738820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp) 3739f08df373SRobert Watson { 3740820a0de9SPawel Jakub Dawidek struct prison *pr; 3741820a0de9SPawel Jakub Dawidek struct statfs *sp; 3742820a0de9SPawel Jakub Dawidek size_t len; 3743f08df373SRobert Watson 3744820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 37450304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 37460304c731SJamie Gritton return (0); 3747820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) 3748820a0de9SPawel Jakub Dawidek return (0); 37490304c731SJamie Gritton if (pr->pr_enforce_statfs == 2) 3750820a0de9SPawel Jakub Dawidek return (ENOENT); 3751820a0de9SPawel Jakub Dawidek /* 3752820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 3753820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 3754820a0de9SPawel Jakub Dawidek * This is ugly check, but this is the only situation when jail's 3755820a0de9SPawel Jakub Dawidek * directory ends with '/'. 3756820a0de9SPawel Jakub Dawidek */ 3757820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 3758820a0de9SPawel Jakub Dawidek return (0); 3759820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 3760820a0de9SPawel Jakub Dawidek sp = &mp->mnt_stat; 3761820a0de9SPawel Jakub Dawidek if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0) 3762820a0de9SPawel Jakub Dawidek return (ENOENT); 3763820a0de9SPawel Jakub Dawidek /* 3764820a0de9SPawel Jakub Dawidek * Be sure that we don't have situation where jail's root directory 3765820a0de9SPawel Jakub Dawidek * is "/some/path" and mount point is "/some/pathpath". 3766820a0de9SPawel Jakub Dawidek */ 3767820a0de9SPawel Jakub Dawidek if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/') 3768820a0de9SPawel Jakub Dawidek return (ENOENT); 3769f08df373SRobert Watson return (0); 3770f08df373SRobert Watson } 3771820a0de9SPawel Jakub Dawidek 3772820a0de9SPawel Jakub Dawidek void 3773820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp) 3774820a0de9SPawel Jakub Dawidek { 3775820a0de9SPawel Jakub Dawidek char jpath[MAXPATHLEN]; 3776820a0de9SPawel Jakub Dawidek struct prison *pr; 3777820a0de9SPawel Jakub Dawidek size_t len; 3778820a0de9SPawel Jakub Dawidek 3779820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 37800304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 37810304c731SJamie Gritton return; 3782820a0de9SPawel Jakub Dawidek if (prison_canseemount(cred, mp) != 0) { 3783820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3784820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, "[restricted]", 3785820a0de9SPawel Jakub Dawidek sizeof(sp->f_mntonname)); 3786820a0de9SPawel Jakub Dawidek return; 3787820a0de9SPawel Jakub Dawidek } 3788820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) { 3789820a0de9SPawel Jakub Dawidek /* 3790820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3791820a0de9SPawel Jakub Dawidek * the valid path left there. 3792820a0de9SPawel Jakub Dawidek */ 3793820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3794820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3795820a0de9SPawel Jakub Dawidek return; 3796820a0de9SPawel Jakub Dawidek } 3797820a0de9SPawel Jakub Dawidek /* 3798820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 3799820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 3800820a0de9SPawel Jakub Dawidek */ 3801820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 3802820a0de9SPawel Jakub Dawidek return; 3803820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 3804820a0de9SPawel Jakub Dawidek strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath)); 3805820a0de9SPawel Jakub Dawidek /* 3806820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3807820a0de9SPawel Jakub Dawidek * the valid path left there. 3808820a0de9SPawel Jakub Dawidek */ 3809820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3810820a0de9SPawel Jakub Dawidek if (*jpath == '\0') { 3811820a0de9SPawel Jakub Dawidek /* Should never happen. */ 3812820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3813820a0de9SPawel Jakub Dawidek } else { 3814820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname)); 3815820a0de9SPawel Jakub Dawidek } 3816f08df373SRobert Watson } 3817f08df373SRobert Watson 3818800c9408SRobert Watson /* 3819800c9408SRobert Watson * Check with permission for a specific privilege is granted within jail. We 3820800c9408SRobert Watson * have a specific list of accepted privileges; the rest are denied. 3821800c9408SRobert Watson */ 3822800c9408SRobert Watson int 3823800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv) 3824800c9408SRobert Watson { 38250fe74ae6SJamie Gritton struct prison *pr; 38260fe74ae6SJamie Gritton int error; 3827800c9408SRobert Watson 38287b2ff0dcSMateusz Guzik /* 38297b2ff0dcSMateusz Guzik * Some policies have custom handlers. This routine should not be 38307b2ff0dcSMateusz Guzik * called for them. See priv_check_cred(). 38317b2ff0dcSMateusz Guzik */ 38327b2ff0dcSMateusz Guzik switch (priv) { 3833a459a6cfSMateusz Guzik case PRIV_VFS_LOOKUP: 38347b2ff0dcSMateusz Guzik case PRIV_VFS_GENERATION: 38357b2ff0dcSMateusz Guzik KASSERT(0, ("prison_priv_check instead of a custom handler " 38367b2ff0dcSMateusz Guzik "called for %d\n", priv)); 38377b2ff0dcSMateusz Guzik } 38387b2ff0dcSMateusz Guzik 3839800c9408SRobert Watson if (!jailed(cred)) 3840800c9408SRobert Watson return (0); 3841800c9408SRobert Watson 38426bb79563SJamie Gritton #ifdef VIMAGE 38436bb79563SJamie Gritton /* 38446bb79563SJamie Gritton * Privileges specific to prisons with a virtual network stack. 38456bb79563SJamie Gritton * There might be a duplicate entry here in case the privilege 38466bb79563SJamie Gritton * is only granted conditionally in the legacy jail case. 38476bb79563SJamie Gritton */ 38486bb79563SJamie Gritton switch (priv) { 38496bb79563SJamie Gritton /* 38506bb79563SJamie Gritton * NFS-specific privileges. 38516bb79563SJamie Gritton */ 38526bb79563SJamie Gritton case PRIV_NFS_DAEMON: 3853bba7a2e8SRick Macklem case PRIV_VFS_GETFH: 3854bba7a2e8SRick Macklem case PRIV_VFS_MOUNT_EXPORTED: 3855bba7a2e8SRick Macklem if (!prison_check_nfsd(cred)) 3856bba7a2e8SRick Macklem return (EPERM); 3857bba7a2e8SRick Macklem #ifdef notyet 38586bb79563SJamie Gritton case PRIV_NFS_LOCKD: 38596bb79563SJamie Gritton #endif 38606bb79563SJamie Gritton /* 38616bb79563SJamie Gritton * Network stack privileges. 38626bb79563SJamie Gritton */ 38636bb79563SJamie Gritton case PRIV_NET_BRIDGE: 38646bb79563SJamie Gritton case PRIV_NET_GRE: 38656bb79563SJamie Gritton case PRIV_NET_BPF: 38666bb79563SJamie Gritton case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */ 38676bb79563SJamie Gritton case PRIV_NET_ROUTE: 38686bb79563SJamie Gritton case PRIV_NET_TAP: 38696bb79563SJamie Gritton case PRIV_NET_SETIFMTU: 38706bb79563SJamie Gritton case PRIV_NET_SETIFFLAGS: 38716bb79563SJamie Gritton case PRIV_NET_SETIFCAP: 3872215940b3SXin LI case PRIV_NET_SETIFDESCR: 38736bb79563SJamie Gritton case PRIV_NET_SETIFNAME : 38746bb79563SJamie Gritton case PRIV_NET_SETIFMETRIC: 38756bb79563SJamie Gritton case PRIV_NET_SETIFPHYS: 38766bb79563SJamie Gritton case PRIV_NET_SETIFMAC: 3877389474c1SKonstantin Belousov case PRIV_NET_SETLANPCP: 38786bb79563SJamie Gritton case PRIV_NET_ADDMULTI: 38796bb79563SJamie Gritton case PRIV_NET_DELMULTI: 38806bb79563SJamie Gritton case PRIV_NET_HWIOCTL: 38816bb79563SJamie Gritton case PRIV_NET_SETLLADDR: 38826bb79563SJamie Gritton case PRIV_NET_ADDIFGROUP: 38836bb79563SJamie Gritton case PRIV_NET_DELIFGROUP: 38846bb79563SJamie Gritton case PRIV_NET_IFCREATE: 38856bb79563SJamie Gritton case PRIV_NET_IFDESTROY: 38866bb79563SJamie Gritton case PRIV_NET_ADDIFADDR: 38876bb79563SJamie Gritton case PRIV_NET_DELIFADDR: 38886bb79563SJamie Gritton case PRIV_NET_LAGG: 38896bb79563SJamie Gritton case PRIV_NET_GIF: 38906bb79563SJamie Gritton case PRIV_NET_SETIFVNET: 389135fd7bc0SBjoern A. Zeeb case PRIV_NET_SETIFFIB: 3892ab91feabSKristof Provost case PRIV_NET_OVPN: 389343f8c763SZhenlei Huang case PRIV_NET_ME: 3894744bfb21SJohn Baldwin case PRIV_NET_WG: 38956bb79563SJamie Gritton 38966bb79563SJamie Gritton /* 38976bb79563SJamie Gritton * 802.11-related privileges. 38986bb79563SJamie Gritton */ 3899f7d38a13SAdrian Chadd case PRIV_NET80211_VAP_GETKEY: 3900f7d38a13SAdrian Chadd case PRIV_NET80211_VAP_MANAGE: 39016bb79563SJamie Gritton 39026bb79563SJamie Gritton #ifdef notyet 39036bb79563SJamie Gritton /* 39046bb79563SJamie Gritton * ATM privileges. 39056bb79563SJamie Gritton */ 39066bb79563SJamie Gritton case PRIV_NETATM_CFG: 39076bb79563SJamie Gritton case PRIV_NETATM_ADD: 39086bb79563SJamie Gritton case PRIV_NETATM_DEL: 39096bb79563SJamie Gritton case PRIV_NETATM_SET: 39106bb79563SJamie Gritton 39116bb79563SJamie Gritton /* 39126bb79563SJamie Gritton * Bluetooth privileges. 39136bb79563SJamie Gritton */ 39146bb79563SJamie Gritton case PRIV_NETBLUETOOTH_RAW: 39156bb79563SJamie Gritton #endif 39166bb79563SJamie Gritton 39176bb79563SJamie Gritton /* 39186bb79563SJamie Gritton * Netgraph and netgraph module privileges. 39196bb79563SJamie Gritton */ 39206bb79563SJamie Gritton case PRIV_NETGRAPH_CONTROL: 39216bb79563SJamie Gritton #ifdef notyet 39226bb79563SJamie Gritton case PRIV_NETGRAPH_TTY: 39236bb79563SJamie Gritton #endif 39246bb79563SJamie Gritton 39256bb79563SJamie Gritton /* 39266bb79563SJamie Gritton * IPv4 and IPv6 privileges. 39276bb79563SJamie Gritton */ 39286bb79563SJamie Gritton case PRIV_NETINET_IPFW: 39296bb79563SJamie Gritton case PRIV_NETINET_DIVERT: 39306bb79563SJamie Gritton case PRIV_NETINET_PF: 39316bb79563SJamie Gritton case PRIV_NETINET_DUMMYNET: 39326bb79563SJamie Gritton case PRIV_NETINET_CARP: 39336bb79563SJamie Gritton case PRIV_NETINET_MROUTE: 39346bb79563SJamie Gritton case PRIV_NETINET_RAW: 39356bb79563SJamie Gritton case PRIV_NETINET_ADDRCTRL6: 39366bb79563SJamie Gritton case PRIV_NETINET_ND6: 39376bb79563SJamie Gritton case PRIV_NETINET_SCOPE6: 39386bb79563SJamie Gritton case PRIV_NETINET_ALIFETIME6: 39396bb79563SJamie Gritton case PRIV_NETINET_IPSEC: 39406bb79563SJamie Gritton case PRIV_NETINET_BINDANY: 39416bb79563SJamie Gritton 39426bb79563SJamie Gritton #ifdef notyet 39436bb79563SJamie Gritton /* 39446bb79563SJamie Gritton * NCP privileges. 39456bb79563SJamie Gritton */ 39466bb79563SJamie Gritton case PRIV_NETNCP: 39476bb79563SJamie Gritton 39486bb79563SJamie Gritton /* 39496bb79563SJamie Gritton * SMB privileges. 39506bb79563SJamie Gritton */ 39516bb79563SJamie Gritton case PRIV_NETSMB: 39526bb79563SJamie Gritton #endif 39536bb79563SJamie Gritton 39546bb79563SJamie Gritton /* 39556bb79563SJamie Gritton * No default: or deny here. 39566bb79563SJamie Gritton * In case of no permit fall through to next switch(). 39576bb79563SJamie Gritton */ 39586bb79563SJamie Gritton if (cred->cr_prison->pr_flags & PR_VNET) 39596bb79563SJamie Gritton return (0); 39606bb79563SJamie Gritton } 39616bb79563SJamie Gritton #endif /* VIMAGE */ 39626bb79563SJamie Gritton 3963800c9408SRobert Watson switch (priv) { 3964800c9408SRobert Watson /* 3965800c9408SRobert Watson * Allow ktrace privileges for root in jail. 3966800c9408SRobert Watson */ 3967800c9408SRobert Watson case PRIV_KTRACE: 3968800c9408SRobert Watson 3969c3c1b5e6SRobert Watson #if 0 3970800c9408SRobert Watson /* 3971800c9408SRobert Watson * Allow jailed processes to configure audit identity and 3972800c9408SRobert Watson * submit audit records (login, etc). In the future we may 3973800c9408SRobert Watson * want to further refine the relationship between audit and 3974800c9408SRobert Watson * jail. 3975800c9408SRobert Watson */ 3976800c9408SRobert Watson case PRIV_AUDIT_GETAUDIT: 3977800c9408SRobert Watson case PRIV_AUDIT_SETAUDIT: 3978800c9408SRobert Watson case PRIV_AUDIT_SUBMIT: 3979c3c1b5e6SRobert Watson #endif 3980800c9408SRobert Watson 3981800c9408SRobert Watson /* 3982800c9408SRobert Watson * Allow jailed processes to manipulate process UNIX 3983800c9408SRobert Watson * credentials in any way they see fit. 3984800c9408SRobert Watson */ 3985ddb3eb4eSOlivier Certner case PRIV_CRED_SETCRED: 3986800c9408SRobert Watson case PRIV_CRED_SETUID: 3987800c9408SRobert Watson case PRIV_CRED_SETEUID: 3988800c9408SRobert Watson case PRIV_CRED_SETGID: 3989800c9408SRobert Watson case PRIV_CRED_SETEGID: 3990800c9408SRobert Watson case PRIV_CRED_SETGROUPS: 3991800c9408SRobert Watson case PRIV_CRED_SETREUID: 3992800c9408SRobert Watson case PRIV_CRED_SETREGID: 3993800c9408SRobert Watson case PRIV_CRED_SETRESUID: 3994800c9408SRobert Watson case PRIV_CRED_SETRESGID: 3995800c9408SRobert Watson 3996800c9408SRobert Watson /* 3997800c9408SRobert Watson * Jail implements visibility constraints already, so allow 3998800c9408SRobert Watson * jailed root to override uid/gid-based constraints. 3999800c9408SRobert Watson */ 4000800c9408SRobert Watson case PRIV_SEEOTHERGIDS: 4001800c9408SRobert Watson case PRIV_SEEOTHERUIDS: 40027974ca1cSOlivier Certner case PRIV_SEEJAILPROC: 4003800c9408SRobert Watson 4004800c9408SRobert Watson /* 4005800c9408SRobert Watson * Jail implements inter-process debugging limits already, so 4006800c9408SRobert Watson * allow jailed root various debugging privileges. 4007800c9408SRobert Watson */ 4008800c9408SRobert Watson case PRIV_DEBUG_DIFFCRED: 4009800c9408SRobert Watson case PRIV_DEBUG_SUGID: 4010800c9408SRobert Watson case PRIV_DEBUG_UNPRIV: 4011800c9408SRobert Watson 4012800c9408SRobert Watson /* 4013800c9408SRobert Watson * Allow jail to set various resource limits and login 4014800c9408SRobert Watson * properties, and for now, exceed process resource limits. 4015800c9408SRobert Watson */ 4016800c9408SRobert Watson case PRIV_PROC_LIMIT: 4017800c9408SRobert Watson case PRIV_PROC_SETLOGIN: 4018800c9408SRobert Watson case PRIV_PROC_SETRLIMIT: 4019800c9408SRobert Watson 4020800c9408SRobert Watson /* 4021800c9408SRobert Watson * System V and POSIX IPC privileges are granted in jail. 4022800c9408SRobert Watson */ 4023800c9408SRobert Watson case PRIV_IPC_READ: 4024800c9408SRobert Watson case PRIV_IPC_WRITE: 4025800c9408SRobert Watson case PRIV_IPC_ADMIN: 4026800c9408SRobert Watson case PRIV_IPC_MSGSIZE: 4027800c9408SRobert Watson case PRIV_MQ_ADMIN: 4028800c9408SRobert Watson 4029800c9408SRobert Watson /* 40300304c731SJamie Gritton * Jail operations within a jail work on child jails. 40310304c731SJamie Gritton */ 40320304c731SJamie Gritton case PRIV_JAIL_ATTACH: 40330304c731SJamie Gritton case PRIV_JAIL_SET: 40340304c731SJamie Gritton case PRIV_JAIL_REMOVE: 40350304c731SJamie Gritton 40360304c731SJamie Gritton /* 4037800c9408SRobert Watson * Jail implements its own inter-process limits, so allow 4038800c9408SRobert Watson * root processes in jail to change scheduling on other 4039800c9408SRobert Watson * processes in the same jail. Likewise for signalling. 4040800c9408SRobert Watson */ 4041800c9408SRobert Watson case PRIV_SCHED_DIFFCRED: 4042413628a7SBjoern A. Zeeb case PRIV_SCHED_CPUSET: 4043800c9408SRobert Watson case PRIV_SIGNAL_DIFFCRED: 4044800c9408SRobert Watson case PRIV_SIGNAL_SUGID: 4045800c9408SRobert Watson 4046800c9408SRobert Watson /* 4047800c9408SRobert Watson * Allow jailed processes to write to sysctls marked as jail 4048800c9408SRobert Watson * writable. 4049800c9408SRobert Watson */ 4050800c9408SRobert Watson case PRIV_SYSCTL_WRITEJAIL: 4051800c9408SRobert Watson 4052800c9408SRobert Watson /* 4053800c9408SRobert Watson * Allow root in jail to manage a variety of quota 4054e82d0201SRobert Watson * properties. These should likely be conditional on a 4055e82d0201SRobert Watson * configuration option. 4056800c9408SRobert Watson */ 405795b091d2SRobert Watson case PRIV_VFS_GETQUOTA: 405895b091d2SRobert Watson case PRIV_VFS_SETQUOTA: 4059800c9408SRobert Watson 4060800c9408SRobert Watson /* 4061800c9408SRobert Watson * Since Jail relies on chroot() to implement file system 4062800c9408SRobert Watson * protections, grant many VFS privileges to root in jail. 4063800c9408SRobert Watson * Be careful to exclude mount-related and NFS-related 4064800c9408SRobert Watson * privileges. 4065800c9408SRobert Watson */ 4066800c9408SRobert Watson case PRIV_VFS_READ: 4067800c9408SRobert Watson case PRIV_VFS_WRITE: 4068800c9408SRobert Watson case PRIV_VFS_ADMIN: 4069800c9408SRobert Watson case PRIV_VFS_EXEC: 4070800c9408SRobert Watson case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */ 4071800c9408SRobert Watson case PRIV_VFS_CHFLAGS_DEV: 4072800c9408SRobert Watson case PRIV_VFS_CHOWN: 4073800c9408SRobert Watson case PRIV_VFS_CHROOT: 4074bb531912SPawel Jakub Dawidek case PRIV_VFS_RETAINSUGID: 4075800c9408SRobert Watson case PRIV_VFS_FCHROOT: 4076800c9408SRobert Watson case PRIV_VFS_LINK: 4077800c9408SRobert Watson case PRIV_VFS_SETGID: 4078e41966dcSRobert Watson case PRIV_VFS_STAT: 4079800c9408SRobert Watson case PRIV_VFS_STICKYFILE: 4080bb56d716SJamie Gritton 4081bb56d716SJamie Gritton /* 4082bb56d716SJamie Gritton * As in the non-jail case, non-root users are expected to be 408370de1003SGordon Bergling * able to read kernel/physical memory (provided /dev/[k]mem 4084bb56d716SJamie Gritton * exists in the jail and they have permission to access it). 4085bb56d716SJamie Gritton */ 4086bb56d716SJamie Gritton case PRIV_KMEM_READ: 4087800c9408SRobert Watson return (0); 4088800c9408SRobert Watson 4089800c9408SRobert Watson /* 4090800c9408SRobert Watson * Depending on the global setting, allow privilege of 4091800c9408SRobert Watson * setting system flags. 4092800c9408SRobert Watson */ 4093800c9408SRobert Watson case PRIV_VFS_SYSFLAGS: 40940304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS) 4095800c9408SRobert Watson return (0); 4096800c9408SRobert Watson else 4097800c9408SRobert Watson return (EPERM); 4098800c9408SRobert Watson 4099800c9408SRobert Watson /* 4100f3a8d2f9SPawel Jakub Dawidek * Depending on the global setting, allow privilege of 4101f3a8d2f9SPawel Jakub Dawidek * mounting/unmounting file systems. 4102f3a8d2f9SPawel Jakub Dawidek */ 4103f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT: 4104f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_UNMOUNT: 4105f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT_NONUSER: 410624b0502eSPawel Jakub Dawidek case PRIV_VFS_MOUNT_OWNER: 41070fe74ae6SJamie Gritton pr = cred->cr_prison; 41080fe74ae6SJamie Gritton prison_lock(pr); 41090fe74ae6SJamie Gritton if (pr->pr_allow & PR_ALLOW_MOUNT && pr->pr_enforce_statfs < 2) 41100fe74ae6SJamie Gritton error = 0; 4111f3a8d2f9SPawel Jakub Dawidek else 41120fe74ae6SJamie Gritton error = EPERM; 41130fe74ae6SJamie Gritton prison_unlock(pr); 41140fe74ae6SJamie Gritton return (error); 4115f3a8d2f9SPawel Jakub Dawidek 4116f3a8d2f9SPawel Jakub Dawidek /* 411763619b6dSKyle Evans * Jails should hold no disposition on the PRIV_VFS_READ_DIR 411863619b6dSKyle Evans * policy. priv_check_cred will not specifically allow it, and 411963619b6dSKyle Evans * we may want a MAC policy to allow it. 412063619b6dSKyle Evans */ 412163619b6dSKyle Evans case PRIV_VFS_READ_DIR: 412263619b6dSKyle Evans return (0); 412363619b6dSKyle Evans 412463619b6dSKyle Evans /* 4125cb48780dSShawn Webb * Conditionally allow privileged process in the jail to 4126cb48780dSShawn Webb * manipulate filesystem extended attributes in the system 4127cb48780dSShawn Webb * namespace. 4128cb48780dSShawn Webb */ 4129cb48780dSShawn Webb case PRIV_VFS_EXTATTR_SYSTEM: 4130cb48780dSShawn Webb if ((cred->cr_prison->pr_allow & PR_ALLOW_EXTATTR) != 0) 4131cb48780dSShawn Webb return (0); 4132cb48780dSShawn Webb else 4133cb48780dSShawn Webb return (EPERM); 4134cb48780dSShawn Webb 4135cb48780dSShawn Webb /* 4136ccd6ac9fSAntoine Brodin * Conditionnaly allow locking (unlocking) physical pages 4137ccd6ac9fSAntoine Brodin * in memory. 4138ccd6ac9fSAntoine Brodin */ 4139ccd6ac9fSAntoine Brodin case PRIV_VM_MLOCK: 4140ccd6ac9fSAntoine Brodin case PRIV_VM_MUNLOCK: 4141ccd6ac9fSAntoine Brodin if (cred->cr_prison->pr_allow & PR_ALLOW_MLOCK) 4142ccd6ac9fSAntoine Brodin return (0); 4143ccd6ac9fSAntoine Brodin else 4144ccd6ac9fSAntoine Brodin return (EPERM); 4145ccd6ac9fSAntoine Brodin 4146ccd6ac9fSAntoine Brodin /* 4147e28f9b7dSAllan Jude * Conditionally allow jailed root to bind reserved ports. 4148800c9408SRobert Watson */ 4149800c9408SRobert Watson case PRIV_NETINET_RESERVEDPORT: 4150e28f9b7dSAllan Jude if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS) 4151e28f9b7dSAllan Jude return (0); 4152e28f9b7dSAllan Jude else 4153e28f9b7dSAllan Jude return (EPERM); 4154e28f9b7dSAllan Jude 4155e28f9b7dSAllan Jude /* 4156e28f9b7dSAllan Jude * Allow jailed root to reuse in-use ports. 4157e28f9b7dSAllan Jude */ 41584b084056SRobert Watson case PRIV_NETINET_REUSEPORT: 4159800c9408SRobert Watson return (0); 4160800c9408SRobert Watson 4161800c9408SRobert Watson /* 4162e3043798SPedro F. Giffuni * Allow jailed root to set certain IPv4/6 (option) headers. 416379ba3952SBjoern A. Zeeb */ 416479ba3952SBjoern A. Zeeb case PRIV_NETINET_SETHDROPTS: 416579ba3952SBjoern A. Zeeb return (0); 416679ba3952SBjoern A. Zeeb 416779ba3952SBjoern A. Zeeb /* 4168800c9408SRobert Watson * Conditionally allow creating raw sockets in jail. 4169800c9408SRobert Watson */ 4170800c9408SRobert Watson case PRIV_NETINET_RAW: 41710304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS) 4172800c9408SRobert Watson return (0); 4173800c9408SRobert Watson else 4174800c9408SRobert Watson return (EPERM); 4175800c9408SRobert Watson 4176800c9408SRobert Watson /* 4177800c9408SRobert Watson * Since jail implements its own visibility limits on netstat 4178800c9408SRobert Watson * sysctls, allow getcred. This allows identd to work in 4179800c9408SRobert Watson * jail. 4180800c9408SRobert Watson */ 4181800c9408SRobert Watson case PRIV_NETINET_GETCRED: 4182800c9408SRobert Watson return (0); 4183800c9408SRobert Watson 41842bfc50bcSEdward Tomasz Napierala /* 41852bfc50bcSEdward Tomasz Napierala * Allow jailed root to set loginclass. 41862bfc50bcSEdward Tomasz Napierala */ 41872bfc50bcSEdward Tomasz Napierala case PRIV_PROC_SETLOGINCLASS: 41882bfc50bcSEdward Tomasz Napierala return (0); 41892bfc50bcSEdward Tomasz Napierala 4190b19d66fdSJamie Gritton /* 41914520f617SJamie Gritton * Do not allow a process inside a jail to read the kernel 4192b19d66fdSJamie Gritton * message buffer unless explicitly permitted. 4193b19d66fdSJamie Gritton */ 4194b19d66fdSJamie Gritton case PRIV_MSGBUF: 4195b19d66fdSJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_READ_MSGBUF) 4196b19d66fdSJamie Gritton return (0); 4197b19d66fdSJamie Gritton return (EPERM); 4198b19d66fdSJamie Gritton 4199d3bb35d4SMariusz Zaborski /* 4200d3bb35d4SMariusz Zaborski * Conditionally allow privileged process in the jail adjust 4201d3bb35d4SMariusz Zaborski * machine time. 4202d3bb35d4SMariusz Zaborski */ 4203d3bb35d4SMariusz Zaborski case PRIV_ADJTIME: 4204d3bb35d4SMariusz Zaborski case PRIV_NTP_ADJTIME: 4205d3bb35d4SMariusz Zaborski if (cred->cr_prison->pr_allow & 4206d3bb35d4SMariusz Zaborski (PR_ALLOW_ADJTIME | PR_ALLOW_SETTIME)) { 4207d3bb35d4SMariusz Zaborski return (0); 4208d3bb35d4SMariusz Zaborski } 4209d3bb35d4SMariusz Zaborski return (EPERM); 4210d3bb35d4SMariusz Zaborski 4211d3bb35d4SMariusz Zaborski /* 4212d3bb35d4SMariusz Zaborski * Conditionally allow privileged process in the jail set 4213d3bb35d4SMariusz Zaborski * machine time. 4214d3bb35d4SMariusz Zaborski */ 4215d3bb35d4SMariusz Zaborski case PRIV_CLOCK_SETTIME: 4216d3bb35d4SMariusz Zaborski if (cred->cr_prison->pr_allow & PR_ALLOW_SETTIME) 4217d3bb35d4SMariusz Zaborski return (0); 4218d3bb35d4SMariusz Zaborski else 4219d3bb35d4SMariusz Zaborski return (EPERM); 4220d3bb35d4SMariusz Zaborski 4221800c9408SRobert Watson default: 4222800c9408SRobert Watson /* 4223800c9408SRobert Watson * In all remaining cases, deny the privilege request. This 4224800c9408SRobert Watson * includes almost all network privileges, many system 4225800c9408SRobert Watson * configuration privileges. 4226800c9408SRobert Watson */ 4227800c9408SRobert Watson return (EPERM); 4228800c9408SRobert Watson } 4229800c9408SRobert Watson } 4230800c9408SRobert Watson 42310304c731SJamie Gritton /* 42320304c731SJamie Gritton * Return the part of pr2's name that is relative to pr1, or the whole name 42330304c731SJamie Gritton * if it does not directly follow. 42340304c731SJamie Gritton */ 42350304c731SJamie Gritton 42360304c731SJamie Gritton char * 42370304c731SJamie Gritton prison_name(struct prison *pr1, struct prison *pr2) 42380304c731SJamie Gritton { 42390304c731SJamie Gritton char *name; 42400304c731SJamie Gritton 42410304c731SJamie Gritton /* Jails see themselves as "0" (if they see themselves at all). */ 42420304c731SJamie Gritton if (pr1 == pr2) 42430304c731SJamie Gritton return "0"; 42440304c731SJamie Gritton name = pr2->pr_name; 42450304c731SJamie Gritton if (prison_ischild(pr1, pr2)) { 42460304c731SJamie Gritton /* 42470304c731SJamie Gritton * pr1 isn't locked (and allprison_lock may not be either) 42480304c731SJamie Gritton * so its length can't be counted on. But the number of dots 42490304c731SJamie Gritton * can be counted on - and counted. 42500304c731SJamie Gritton */ 42510304c731SJamie Gritton for (; pr1 != &prison0; pr1 = pr1->pr_parent) 42520304c731SJamie Gritton name = strchr(name, '.') + 1; 42530304c731SJamie Gritton } 42540304c731SJamie Gritton return (name); 42550304c731SJamie Gritton } 42560304c731SJamie Gritton 42570304c731SJamie Gritton /* 42580304c731SJamie Gritton * Return the part of pr2's path that is relative to pr1, or the whole path 42590304c731SJamie Gritton * if it does not directly follow. 42600304c731SJamie Gritton */ 42610304c731SJamie Gritton static char * 42620304c731SJamie Gritton prison_path(struct prison *pr1, struct prison *pr2) 42630304c731SJamie Gritton { 42640304c731SJamie Gritton char *path1, *path2; 42650304c731SJamie Gritton int len1; 42660304c731SJamie Gritton 42670304c731SJamie Gritton path1 = pr1->pr_path; 42680304c731SJamie Gritton path2 = pr2->pr_path; 42690304c731SJamie Gritton if (!strcmp(path1, "/")) 42700304c731SJamie Gritton return (path2); 42710304c731SJamie Gritton len1 = strlen(path1); 42720304c731SJamie Gritton if (strncmp(path1, path2, len1)) 42730304c731SJamie Gritton return (path2); 42740304c731SJamie Gritton if (path2[len1] == '\0') 42750304c731SJamie Gritton return "/"; 42760304c731SJamie Gritton if (path2[len1] == '/') 42770304c731SJamie Gritton return (path2 + len1); 42780304c731SJamie Gritton return (path2); 42790304c731SJamie Gritton } 42800304c731SJamie Gritton 42810304c731SJamie Gritton /* 42820304c731SJamie Gritton * Jail-related sysctls. 42830304c731SJamie Gritton */ 4284*30e6e008SIgor Ostapenko SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 42850304c731SJamie Gritton "Jails"); 42860304c731SJamie Gritton 4287eb8dcdeaSGleb Smirnoff #if defined(INET) || defined(INET6) 4288eb8dcdeaSGleb Smirnoff /* 4289eb8dcdeaSGleb Smirnoff * Copy address array to memory that would be then SYSCTL_OUT-ed. 4290eb8dcdeaSGleb Smirnoff * sysctl_jail_list() helper. 4291eb8dcdeaSGleb Smirnoff */ 4292eb8dcdeaSGleb Smirnoff static void 4293eb8dcdeaSGleb Smirnoff prison_ip_copyout(struct prison *pr, const pr_family_t af, void **out, int *len) 4294eb8dcdeaSGleb Smirnoff { 42952c33b456SZhenlei Huang const struct prison_ip *pip; 4296eb8dcdeaSGleb Smirnoff const size_t size = pr_families[af].size; 4297eb8dcdeaSGleb Smirnoff 4298eb8dcdeaSGleb Smirnoff again: 4299eb8dcdeaSGleb Smirnoff mtx_assert(&pr->pr_mtx, MA_OWNED); 43002c33b456SZhenlei Huang if ((pip = pr->pr_addrs[af]) != NULL) { 43012c33b456SZhenlei Huang if (*len < pip->ips) { 43022c33b456SZhenlei Huang *len = pip->ips; 4303eb8dcdeaSGleb Smirnoff mtx_unlock(&pr->pr_mtx); 4304eb8dcdeaSGleb Smirnoff *out = realloc(*out, *len * size, M_TEMP, M_WAITOK); 4305eb8dcdeaSGleb Smirnoff mtx_lock(&pr->pr_mtx); 4306eb8dcdeaSGleb Smirnoff goto again; 4307eb8dcdeaSGleb Smirnoff } 43082c33b456SZhenlei Huang bcopy(pip->pr_ip, *out, pip->ips * size); 4309eb8dcdeaSGleb Smirnoff } 4310eb8dcdeaSGleb Smirnoff } 4311eb8dcdeaSGleb Smirnoff #endif 4312eb8dcdeaSGleb Smirnoff 4313fd7a8150SMike Barcroft static int 4314fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS) 4315fd7a8150SMike Barcroft { 4316b38ff370SJamie Gritton struct xprison *xp; 43170304c731SJamie Gritton struct prison *pr, *cpr; 4318b38ff370SJamie Gritton #ifdef INET 4319b38ff370SJamie Gritton struct in_addr *ip4 = NULL; 4320b38ff370SJamie Gritton int ip4s = 0; 4321b38ff370SJamie Gritton #endif 4322b38ff370SJamie Gritton #ifdef INET6 43233beefaedSColin Percival struct in6_addr *ip6 = NULL; 4324b38ff370SJamie Gritton int ip6s = 0; 4325b38ff370SJamie Gritton #endif 43260304c731SJamie Gritton int descend, error; 4327fd7a8150SMike Barcroft 4328b38ff370SJamie Gritton xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK); 43290304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 4330b38ff370SJamie Gritton error = 0; 4331dc68a633SPawel Jakub Dawidek sx_slock(&allprison_lock); 43320304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 43330304c731SJamie Gritton mtx_lock(&cpr->pr_mtx); 4334413628a7SBjoern A. Zeeb #ifdef INET 4335eb8dcdeaSGleb Smirnoff prison_ip_copyout(cpr, PR_INET, (void **)&ip4, &ip4s); 4336413628a7SBjoern A. Zeeb #endif 4337413628a7SBjoern A. Zeeb #ifdef INET6 4338eb8dcdeaSGleb Smirnoff prison_ip_copyout(cpr, PR_INET6, (void **)&ip6, &ip6s); 4339b38ff370SJamie Gritton #endif 4340b38ff370SJamie Gritton bzero(xp, sizeof(*xp)); 4341fd7a8150SMike Barcroft xp->pr_version = XPRISON_VERSION; 43420304c731SJamie Gritton xp->pr_id = cpr->pr_id; 43431158508aSJamie Gritton xp->pr_state = cpr->pr_state; 43440304c731SJamie Gritton strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path)); 4345c1f19219SJamie Gritton strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host)); 43460304c731SJamie Gritton strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name)); 4347413628a7SBjoern A. Zeeb #ifdef INET 4348eb8dcdeaSGleb Smirnoff xp->pr_ip4s = ip4s; 4349413628a7SBjoern A. Zeeb #endif 4350413628a7SBjoern A. Zeeb #ifdef INET6 4351eb8dcdeaSGleb Smirnoff xp->pr_ip6s = ip6s; 4352413628a7SBjoern A. Zeeb #endif 43530304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 4354b38ff370SJamie Gritton error = SYSCTL_OUT(req, xp, sizeof(*xp)); 4355b38ff370SJamie Gritton if (error) 4356b38ff370SJamie Gritton break; 4357413628a7SBjoern A. Zeeb #ifdef INET 4358b38ff370SJamie Gritton if (xp->pr_ip4s > 0) { 4359b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip4, 4360b38ff370SJamie Gritton xp->pr_ip4s * sizeof(struct in_addr)); 4361b38ff370SJamie Gritton if (error) 4362b38ff370SJamie Gritton break; 4363413628a7SBjoern A. Zeeb } 4364413628a7SBjoern A. Zeeb #endif 4365413628a7SBjoern A. Zeeb #ifdef INET6 4366b38ff370SJamie Gritton if (xp->pr_ip6s > 0) { 4367b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip6, 4368b38ff370SJamie Gritton xp->pr_ip6s * sizeof(struct in6_addr)); 4369b38ff370SJamie Gritton if (error) 4370b38ff370SJamie Gritton break; 4371413628a7SBjoern A. Zeeb } 4372413628a7SBjoern A. Zeeb #endif 4373fd7a8150SMike Barcroft } 4374dc68a633SPawel Jakub Dawidek sx_sunlock(&allprison_lock); 4375b38ff370SJamie Gritton free(xp, M_TEMP); 4376b38ff370SJamie Gritton #ifdef INET 4377b38ff370SJamie Gritton free(ip4, M_TEMP); 4378b38ff370SJamie Gritton #endif 4379b38ff370SJamie Gritton #ifdef INET6 4380b38ff370SJamie Gritton free(ip6, M_TEMP); 4381b38ff370SJamie Gritton #endif 4382fd7a8150SMike Barcroft return (error); 4383fd7a8150SMike Barcroft } 4384fd7a8150SMike Barcroft 4385f3b86a5fSEd Schouten SYSCTL_OID(_security_jail, OID_AUTO, list, 4386f3b86a5fSEd Schouten CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4387f3b86a5fSEd Schouten sysctl_jail_list, "S", "List of active jails"); 4388461167c2SPawel Jakub Dawidek 4389461167c2SPawel Jakub Dawidek static int 4390461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS) 4391461167c2SPawel Jakub Dawidek { 4392461167c2SPawel Jakub Dawidek int error, injail; 4393461167c2SPawel Jakub Dawidek 4394461167c2SPawel Jakub Dawidek injail = jailed(req->td->td_ucred); 4395461167c2SPawel Jakub Dawidek error = SYSCTL_OUT(req, &injail, sizeof(injail)); 4396461167c2SPawel Jakub Dawidek 4397461167c2SPawel Jakub Dawidek return (error); 4398461167c2SPawel Jakub Dawidek } 43990304c731SJamie Gritton 4400f3b86a5fSEd Schouten SYSCTL_PROC(_security_jail, OID_AUTO, jailed, 4401f3b86a5fSEd Schouten CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4402f3b86a5fSEd Schouten sysctl_jail_jailed, "I", "Process in jail?"); 4403413628a7SBjoern A. Zeeb 4404761d2bb5SJamie Gritton static int 4405761d2bb5SJamie Gritton sysctl_jail_vnet(SYSCTL_HANDLER_ARGS) 4406761d2bb5SJamie Gritton { 4407761d2bb5SJamie Gritton int error, havevnet; 4408761d2bb5SJamie Gritton #ifdef VIMAGE 4409761d2bb5SJamie Gritton struct ucred *cred = req->td->td_ucred; 4410761d2bb5SJamie Gritton 4411761d2bb5SJamie Gritton havevnet = jailed(cred) && prison_owns_vnet(cred); 4412761d2bb5SJamie Gritton #else 4413761d2bb5SJamie Gritton havevnet = 0; 4414761d2bb5SJamie Gritton #endif 4415761d2bb5SJamie Gritton error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet)); 4416761d2bb5SJamie Gritton 4417761d2bb5SJamie Gritton return (error); 4418761d2bb5SJamie Gritton } 4419761d2bb5SJamie Gritton 4420761d2bb5SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, vnet, 4421761d2bb5SJamie Gritton CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4422bb8f1623SBjoern A. Zeeb sysctl_jail_vnet, "I", "Jail owns vnet?"); 4423761d2bb5SJamie Gritton 44240304c731SJamie Gritton #if defined(INET) || defined(INET6) 4425e92e0574SJamie Gritton SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW, 44260304c731SJamie Gritton &jail_max_af_ips, 0, 4427c542c43eSJamie Gritton "Number of IP addresses a jail may have at most per address family (deprecated)"); 44280304c731SJamie Gritton #endif 44290304c731SJamie Gritton 44300304c731SJamie Gritton /* 4431c542c43eSJamie Gritton * Default parameters for jail(2) compatibility. For historical reasons, 4432c542c43eSJamie Gritton * the sysctl names have varying similarity to the parameter names. Prisons 4433c542c43eSJamie Gritton * just see their own parameters, and can't change them. 44340304c731SJamie Gritton */ 44350304c731SJamie Gritton static int 44360304c731SJamie Gritton sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS) 44370304c731SJamie Gritton { 44380fe74ae6SJamie Gritton int error, i; 44390304c731SJamie Gritton 44400304c731SJamie Gritton /* Get the current flag value, and convert it to a boolean. */ 44410fe74ae6SJamie Gritton if (req->td->td_ucred->cr_prison == &prison0) { 44420fe74ae6SJamie Gritton mtx_lock(&prison0.pr_mtx); 44430fe74ae6SJamie Gritton i = (jail_default_allow & arg2) != 0; 44440fe74ae6SJamie Gritton mtx_unlock(&prison0.pr_mtx); 44450fe74ae6SJamie Gritton } else 44460fe74ae6SJamie Gritton i = prison_allow(req->td->td_ucred, arg2); 44470fe74ae6SJamie Gritton 44480304c731SJamie Gritton if (arg1 != NULL) 44490304c731SJamie Gritton i = !i; 44500304c731SJamie Gritton error = sysctl_handle_int(oidp, &i, 0, req); 4451c542c43eSJamie Gritton if (error || !req->newptr) 44520304c731SJamie Gritton return (error); 44530304c731SJamie Gritton i = i ? arg2 : 0; 44540304c731SJamie Gritton if (arg1 != NULL) 44550304c731SJamie Gritton i ^= arg2; 44560304c731SJamie Gritton /* 44570304c731SJamie Gritton * The sysctls don't have CTLFLAGS_PRISON, so assume prison0 44580304c731SJamie Gritton * for writing. 44590304c731SJamie Gritton */ 44600304c731SJamie Gritton mtx_lock(&prison0.pr_mtx); 44610304c731SJamie Gritton jail_default_allow = (jail_default_allow & ~arg2) | i; 44620304c731SJamie Gritton mtx_unlock(&prison0.pr_mtx); 44630304c731SJamie Gritton return (0); 44640304c731SJamie Gritton } 44650304c731SJamie Gritton 44660304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed, 4467c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44680304c731SJamie Gritton NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I", 4469c542c43eSJamie Gritton "Processes in jail can set their hostnames (deprecated)"); 44700304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only, 4471c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44720304c731SJamie Gritton (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I", 4473c542c43eSJamie Gritton "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)"); 44740304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed, 4475c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44760304c731SJamie Gritton NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I", 4477c542c43eSJamie Gritton "Processes in jail can use System V IPC primitives (deprecated)"); 44780304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets, 4479c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44800304c731SJamie Gritton NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I", 4481c542c43eSJamie Gritton "Prison root can create raw sockets (deprecated)"); 44820304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed, 4483c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44840304c731SJamie Gritton NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I", 4485c542c43eSJamie Gritton "Processes in jail can alter system file flags (deprecated)"); 44860304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed, 4487c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44880304c731SJamie Gritton NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I", 4489c542c43eSJamie Gritton "Processes in jail can mount/unmount jail-friendly file systems (deprecated)"); 44909fd97868SBaptiste Daroussin SYSCTL_PROC(_security_jail, OID_AUTO, mlock_allowed, 44919fd97868SBaptiste Daroussin CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44929fd97868SBaptiste Daroussin NULL, PR_ALLOW_MLOCK, sysctl_jail_default_allow, "I", 44939fd97868SBaptiste Daroussin "Processes in jail can lock/unlock physical pages in memory"); 44940304c731SJamie Gritton 44950304c731SJamie Gritton static int 44960304c731SJamie Gritton sysctl_jail_default_level(SYSCTL_HANDLER_ARGS) 44970304c731SJamie Gritton { 44980304c731SJamie Gritton struct prison *pr; 44990304c731SJamie Gritton int level, error; 45000304c731SJamie Gritton 45010304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 45020304c731SJamie Gritton level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2); 45030304c731SJamie Gritton error = sysctl_handle_int(oidp, &level, 0, req); 4504c542c43eSJamie Gritton if (error || !req->newptr) 45050304c731SJamie Gritton return (error); 45060304c731SJamie Gritton *(int *)arg1 = level; 45070304c731SJamie Gritton return (0); 45080304c731SJamie Gritton } 45090304c731SJamie Gritton 45100304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs, 4511c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4512c542c43eSJamie Gritton &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs), 45130304c731SJamie Gritton sysctl_jail_default_level, "I", 4514c542c43eSJamie Gritton "Processes in jail cannot see all mounted file systems (deprecated)"); 4515c542c43eSJamie Gritton 45160cc207a6SMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset, 4517c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 4518c542c43eSJamie Gritton &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum), 45190cc207a6SMartin Matuska sysctl_jail_default_level, "I", 4520c542c43eSJamie Gritton "Ruleset for the devfs filesystem in jail (deprecated)"); 45210cc207a6SMartin Matuska 4522ab0841bdSJamie Gritton SYSCTL_NODE(_security_jail, OID_AUTO, children, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 4523ab0841bdSJamie Gritton "Limits and stats of child jails"); 4524ab0841bdSJamie Gritton 4525ab0841bdSJamie Gritton static int 4526ab0841bdSJamie Gritton sysctl_jail_children(SYSCTL_HANDLER_ARGS) 4527ab0841bdSJamie Gritton { 4528ab0841bdSJamie Gritton struct prison *pr; 4529ab0841bdSJamie Gritton int i; 4530ab0841bdSJamie Gritton 4531ab0841bdSJamie Gritton pr = req->td->td_ucred->cr_prison; 4532ab0841bdSJamie Gritton 4533ab0841bdSJamie Gritton switch (oidp->oid_kind & CTLTYPE) { 4534ab0841bdSJamie Gritton case CTLTYPE_INT: 4535ab0841bdSJamie Gritton i = *(int *)((char *)pr + arg2); 4536ab0841bdSJamie Gritton return (SYSCTL_OUT(req, &i, sizeof(i))); 4537ab0841bdSJamie Gritton } 4538ab0841bdSJamie Gritton 4539ab0841bdSJamie Gritton return (0); 4540ab0841bdSJamie Gritton } 4541ab0841bdSJamie Gritton 4542ab0841bdSJamie Gritton SYSCTL_PROC(_security_jail_children, OID_AUTO, max, 4543ab0841bdSJamie Gritton CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 4544ab0841bdSJamie Gritton NULL, offsetof(struct prison, pr_childmax), sysctl_jail_children, 4545ab0841bdSJamie Gritton "I", "Maximum number of child jails"); 4546ab0841bdSJamie Gritton SYSCTL_PROC(_security_jail_children, OID_AUTO, cur, 4547ab0841bdSJamie Gritton CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 4548ab0841bdSJamie Gritton NULL, offsetof(struct prison, pr_childcount), sysctl_jail_children, 4549ab0841bdSJamie Gritton "I", "Current number of child jails"); 4550ab0841bdSJamie Gritton 45510304c731SJamie Gritton /* 45520304c731SJamie Gritton * Nodes to describe jail parameters. Maximum length of string parameters 45530304c731SJamie Gritton * is returned in the string itself, and the other parameters exist merely 45540304c731SJamie Gritton * to make themselves and their types known. 45550304c731SJamie Gritton */ 45567029da5cSPawel Biernacki SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 45570304c731SJamie Gritton "Jail parameters"); 45580304c731SJamie Gritton 45590304c731SJamie Gritton int 45600304c731SJamie Gritton sysctl_jail_param(SYSCTL_HANDLER_ARGS) 45610304c731SJamie Gritton { 45620304c731SJamie Gritton int i; 45630304c731SJamie Gritton long l; 45640304c731SJamie Gritton size_t s; 45650304c731SJamie Gritton char numbuf[12]; 45660304c731SJamie Gritton 45670304c731SJamie Gritton switch (oidp->oid_kind & CTLTYPE) 45680304c731SJamie Gritton { 45690304c731SJamie Gritton case CTLTYPE_LONG: 45700304c731SJamie Gritton case CTLTYPE_ULONG: 45710304c731SJamie Gritton l = 0; 45720304c731SJamie Gritton #ifdef SCTL_MASK32 45730304c731SJamie Gritton if (!(req->flags & SCTL_MASK32)) 45740304c731SJamie Gritton #endif 45750304c731SJamie Gritton return (SYSCTL_OUT(req, &l, sizeof(l))); 45760304c731SJamie Gritton case CTLTYPE_INT: 45770304c731SJamie Gritton case CTLTYPE_UINT: 45780304c731SJamie Gritton i = 0; 45790304c731SJamie Gritton return (SYSCTL_OUT(req, &i, sizeof(i))); 45800304c731SJamie Gritton case CTLTYPE_STRING: 4581e4cd31ddSJeff Roberson snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2); 45820304c731SJamie Gritton return 45830304c731SJamie Gritton (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req)); 45840304c731SJamie Gritton case CTLTYPE_STRUCT: 45850304c731SJamie Gritton s = (size_t)arg2; 45860304c731SJamie Gritton return (SYSCTL_OUT(req, &s, sizeof(s))); 45870304c731SJamie Gritton } 45880304c731SJamie Gritton return (0); 45890304c731SJamie Gritton } 45900304c731SJamie Gritton 4591b96bd95bSIan Lepore /* 4592b96bd95bSIan Lepore * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at 4593b96bd95bSIan Lepore * jail creation time but cannot be changed in an existing jail. 4594b96bd95bSIan Lepore */ 45950304c731SJamie Gritton SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID"); 45960304c731SJamie Gritton SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID"); 45970304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name"); 45980304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path"); 45990304c731SJamie Gritton SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW, 46000304c731SJamie Gritton "I", "Jail secure level"); 4601b96bd95bSIan Lepore SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I", 4602b96bd95bSIan Lepore "Jail value for kern.osreldate and uname -K"); 4603b96bd95bSIan Lepore SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN, 4604b96bd95bSIan Lepore "Jail value for kern.osrelease and uname -r"); 46050304c731SJamie Gritton SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW, 46060304c731SJamie Gritton "I", "Jail cannot see all mounted file systems"); 46070cc207a6SMartin Matuska SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW, 46080cc207a6SMartin Matuska "I", "Ruleset for in-jail devfs mounts"); 46090304c731SJamie Gritton SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW, 46100304c731SJamie Gritton "B", "Jail persistence"); 4611679e1390SJamie Gritton #ifdef VIMAGE 4612679e1390SJamie Gritton SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN, 46137cbf7213SJamie Gritton "E,jailsys", "Virtual network stack"); 4614679e1390SJamie Gritton #endif 46150304c731SJamie Gritton SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD, 46160304c731SJamie Gritton "B", "Jail is in the process of shutting down"); 46170304c731SJamie Gritton 4618b97457e2SJamie Gritton SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails"); 4619b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD, 4620b97457e2SJamie Gritton "I", "Current number of child jails"); 4621b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW, 4622b97457e2SJamie Gritton "I", "Maximum number of child jails"); 4623b97457e2SJamie Gritton 46247cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info"); 46250304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN, 46260304c731SJamie Gritton "Jail hostname"); 462776ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN, 462876ca6f88SJamie Gritton "Jail NIS domainname"); 462976ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN, 463076ca6f88SJamie Gritton "Jail host UUID"); 463176ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW, 463276ca6f88SJamie Gritton "LU", "Jail host ID"); 46330304c731SJamie Gritton 46340304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset"); 46350304c731SJamie Gritton SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID"); 46360304c731SJamie Gritton 46370304c731SJamie Gritton #ifdef INET 46382b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN, 46392b0d6f81SJamie Gritton "Jail IPv4 address virtualization"); 46400304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr), 46410304c731SJamie Gritton "S,in_addr,a", "Jail IPv4 addresses"); 4642592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4643592bcae8SBjoern A. Zeeb "B", "Do (not) use IPv4 source address selection rather than the " 4644592bcae8SBjoern A. Zeeb "primary jail IPv4 address."); 46450304c731SJamie Gritton #endif 46460304c731SJamie Gritton #ifdef INET6 46472b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN, 46482b0d6f81SJamie Gritton "Jail IPv6 address virtualization"); 46490304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr), 46500304c731SJamie Gritton "S,in6_addr,a", "Jail IPv6 addresses"); 4651592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4652592bcae8SBjoern A. Zeeb "B", "Do (not) use IPv6 source address selection rather than the " 4653592bcae8SBjoern A. Zeeb "primary jail IPv6 address."); 46540304c731SJamie Gritton #endif 46550304c731SJamie Gritton 46560304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags"); 46570304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW, 46580304c731SJamie Gritton "B", "Jail may set hostname"); 46590304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW, 46600304c731SJamie Gritton "B", "Jail may use SYSV IPC"); 46610304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW, 46620304c731SJamie Gritton "B", "Jail may create raw sockets"); 46630304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW, 46640304c731SJamie Gritton "B", "Jail may alter system file flags"); 46650304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW, 46660304c731SJamie Gritton "B", "Jail may set file quotas"); 46670304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW, 46680304c731SJamie Gritton "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route"); 4669ccd6ac9fSAntoine Brodin SYSCTL_JAIL_PARAM(_allow, mlock, CTLTYPE_INT | CTLFLAG_RW, 4670ccd6ac9fSAntoine Brodin "B", "Jail may lock (unlock) physical pages in memory"); 4671e28f9b7dSAllan Jude SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW, 4672e28f9b7dSAllan Jude "B", "Jail may bind sockets to reserved ports"); 4673b19d66fdSJamie Gritton SYSCTL_JAIL_PARAM(_allow, read_msgbuf, CTLTYPE_INT | CTLFLAG_RW, 4674b19d66fdSJamie Gritton "B", "Jail may read the kernel message buffer"); 4675b3079544SJamie Gritton SYSCTL_JAIL_PARAM(_allow, unprivileged_proc_debug, CTLTYPE_INT | CTLFLAG_RW, 4676b3079544SJamie Gritton "B", "Unprivileged processes may use process debugging facilities"); 467705e1e482SMariusz Zaborski SYSCTL_JAIL_PARAM(_allow, suser, CTLTYPE_INT | CTLFLAG_RW, 467805e1e482SMariusz Zaborski "B", "Processes in jail with uid 0 have privilege"); 4679cbbb2203SRick Macklem #ifdef VIMAGE 4680bba7a2e8SRick Macklem SYSCTL_JAIL_PARAM(_allow, nfsd, CTLTYPE_INT | CTLFLAG_RW, 4681bba7a2e8SRick Macklem "B", "Mountd/nfsd may run in the jail"); 4682bba7a2e8SRick Macklem #endif 4683cb48780dSShawn Webb SYSCTL_JAIL_PARAM(_allow, extattr, CTLTYPE_INT | CTLFLAG_RW, 4684cb48780dSShawn Webb "B", "Jail may set system-level filesystem extended attributes"); 4685d3bb35d4SMariusz Zaborski SYSCTL_JAIL_PARAM(_allow, adjtime, CTLTYPE_INT | CTLFLAG_RW, 4686d3bb35d4SMariusz Zaborski "B", "Jail may adjust system time"); 4687d3bb35d4SMariusz Zaborski SYSCTL_JAIL_PARAM(_allow, settime, CTLTYPE_INT | CTLFLAG_RW, 4688d3bb35d4SMariusz Zaborski "B", "Jail may set system time"); 46890304c731SJamie Gritton 4690bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags"); 4691bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW, 4692bf3db8aaSMartin Matuska "B", "Jail may mount/unmount jail-friendly file systems in general"); 46930e5c6bd4SJamie Gritton 46940e5c6bd4SJamie Gritton /* 46950a172404SJamie Gritton * Add a dynamic parameter allow.<name>, or allow.<prefix>.<name>. Return 46960a172404SJamie Gritton * its associated bit in the pr_allow bitmask, or zero if the parameter was 46970a172404SJamie Gritton * not created. 46980e5c6bd4SJamie Gritton */ 46990a172404SJamie Gritton unsigned 47000a172404SJamie Gritton prison_add_allow(const char *prefix, const char *name, const char *prefix_descr, 47010a172404SJamie Gritton const char *descr) 47020e5c6bd4SJamie Gritton { 47030e5c6bd4SJamie Gritton struct bool_flags *bf; 47040a172404SJamie Gritton struct sysctl_oid *parent; 47050a172404SJamie Gritton char *allow_name, *allow_noname, *allowed; 4706c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR 4707c542c43eSJamie Gritton char *descr_deprecated; 4708c542c43eSJamie Gritton #endif 47095d58f959SJamie Gritton u_int allow_flag; 47100e5c6bd4SJamie Gritton 47110a172404SJamie Gritton if (prefix 47120a172404SJamie Gritton ? asprintf(&allow_name, M_PRISON, "allow.%s.%s", prefix, name) 47130a172404SJamie Gritton < 0 || 47140a172404SJamie Gritton asprintf(&allow_noname, M_PRISON, "allow.%s.no%s", prefix, name) 47150a172404SJamie Gritton < 0 47160a172404SJamie Gritton : asprintf(&allow_name, M_PRISON, "allow.%s", name) < 0 || 47170a172404SJamie Gritton asprintf(&allow_noname, M_PRISON, "allow.no%s", name) < 0) { 47180e5c6bd4SJamie Gritton free(allow_name, M_PRISON); 47190a172404SJamie Gritton return 0; 47200e5c6bd4SJamie Gritton } 47210e5c6bd4SJamie Gritton 47220e5c6bd4SJamie Gritton /* 47230a172404SJamie Gritton * See if this parameter has already beed added, i.e. a module was 47240a172404SJamie Gritton * previously loaded/unloaded. 47250e5c6bd4SJamie Gritton */ 47260e5c6bd4SJamie Gritton mtx_lock(&prison0.pr_mtx); 47270e5c6bd4SJamie Gritton for (bf = pr_flag_allow; 47285d58f959SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 47295d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 47300e5c6bd4SJamie Gritton bf++) { 47310e5c6bd4SJamie Gritton if (strcmp(bf->name, allow_name) == 0) { 47320a172404SJamie Gritton allow_flag = bf->flag; 47330e5c6bd4SJamie Gritton goto no_add; 47340e5c6bd4SJamie Gritton } 47350e5c6bd4SJamie Gritton } 47360e5c6bd4SJamie Gritton 47370e5c6bd4SJamie Gritton /* 47385d58f959SJamie Gritton * Find a free bit in pr_allow_all, failing if there are none 47390e5c6bd4SJamie Gritton * (which shouldn't happen as long as we keep track of how many 47400a172404SJamie Gritton * potential dynamic flags exist). 47410e5c6bd4SJamie Gritton */ 47420e5c6bd4SJamie Gritton for (allow_flag = 1;; allow_flag <<= 1) { 47430e5c6bd4SJamie Gritton if (allow_flag == 0) 47440e5c6bd4SJamie Gritton goto no_add; 47455d58f959SJamie Gritton if ((pr_allow_all & allow_flag) == 0) 47460e5c6bd4SJamie Gritton break; 47470e5c6bd4SJamie Gritton } 47480e5c6bd4SJamie Gritton 47495d58f959SJamie Gritton /* Note the parameter in the next open slot in pr_flag_allow. */ 47505d58f959SJamie Gritton for (bf = pr_flag_allow; ; bf++) { 47510e5c6bd4SJamie Gritton if (bf == pr_flag_allow + nitems(pr_flag_allow)) { 47520e5c6bd4SJamie Gritton /* This should never happen, but is not fatal. */ 47530a172404SJamie Gritton allow_flag = 0; 47540e5c6bd4SJamie Gritton goto no_add; 47550e5c6bd4SJamie Gritton } 47565d58f959SJamie Gritton if (atomic_load_int(&bf->flag) == 0) 47575d58f959SJamie Gritton break; 47585d58f959SJamie Gritton } 47590e5c6bd4SJamie Gritton bf->name = allow_name; 47600e5c6bd4SJamie Gritton bf->noname = allow_noname; 47615d58f959SJamie Gritton pr_allow_all |= allow_flag; 47625d58f959SJamie Gritton /* 47635d58f959SJamie Gritton * prison0 always has permission for the new parameter. 47645d58f959SJamie Gritton * Other jails must have it granted to them. 47655d58f959SJamie Gritton */ 47665d58f959SJamie Gritton prison0.pr_allow |= allow_flag; 47675d58f959SJamie Gritton /* The flag indicates a valid entry, so make sure it is set last. */ 47685d58f959SJamie Gritton atomic_store_rel_int(&bf->flag, allow_flag); 47690e5c6bd4SJamie Gritton mtx_unlock(&prison0.pr_mtx); 47700e5c6bd4SJamie Gritton 4771c542c43eSJamie Gritton /* 47724771011bSGordon Bergling * Create sysctls for the parameter, and the back-compat global 4773c542c43eSJamie Gritton * permission. 4774c542c43eSJamie Gritton */ 47750a172404SJamie Gritton parent = prefix 47760a172404SJamie Gritton ? SYSCTL_ADD_NODE(NULL, 47770a172404SJamie Gritton SYSCTL_CHILDREN(&sysctl___security_jail_param_allow), 47787029da5cSPawel Biernacki OID_AUTO, prefix, CTLFLAG_MPSAFE, 0, prefix_descr) 47790a172404SJamie Gritton : &sysctl___security_jail_param_allow; 47800a172404SJamie Gritton (void)SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(parent), OID_AUTO, 47810a172404SJamie Gritton name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 47820e5c6bd4SJamie Gritton NULL, 0, sysctl_jail_param, "B", descr); 47830a172404SJamie Gritton if ((prefix 47840a172404SJamie Gritton ? asprintf(&allowed, M_TEMP, "%s_%s_allowed", prefix, name) 47850a172404SJamie Gritton : asprintf(&allowed, M_TEMP, "%s_allowed", name)) >= 0) { 4786c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR 4787c542c43eSJamie Gritton (void)asprintf(&descr_deprecated, M_TEMP, "%s (deprecated)", 4788c542c43eSJamie Gritton descr); 4789c542c43eSJamie Gritton #endif 47900e5c6bd4SJamie Gritton (void)SYSCTL_ADD_PROC(NULL, 47910a172404SJamie Gritton SYSCTL_CHILDREN(&sysctl___security_jail), OID_AUTO, allowed, 4792c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, allow_flag, 4793c542c43eSJamie Gritton sysctl_jail_default_allow, "I", descr_deprecated); 4794c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR 4795c542c43eSJamie Gritton free(descr_deprecated, M_TEMP); 4796c542c43eSJamie Gritton #endif 47970a172404SJamie Gritton free(allowed, M_TEMP); 47980e5c6bd4SJamie Gritton } 47990a172404SJamie Gritton return allow_flag; 48000e5c6bd4SJamie Gritton 48010e5c6bd4SJamie Gritton no_add: 48020e5c6bd4SJamie Gritton mtx_unlock(&prison0.pr_mtx); 48030e5c6bd4SJamie Gritton free(allow_name, M_PRISON); 48040e5c6bd4SJamie Gritton free(allow_noname, M_PRISON); 48050a172404SJamie Gritton return allow_flag; 48060a172404SJamie Gritton } 48070a172404SJamie Gritton 48080a172404SJamie Gritton /* 48090a172404SJamie Gritton * The VFS system will register jail-aware filesystems here. They each get 48100a172404SJamie Gritton * a parameter allow.mount.xxxfs and a flag to check when a jailed user 48110a172404SJamie Gritton * attempts to mount. 48120a172404SJamie Gritton */ 48130a172404SJamie Gritton void 48140a172404SJamie Gritton prison_add_vfs(struct vfsconf *vfsp) 48150a172404SJamie Gritton { 48160a172404SJamie Gritton #ifdef NO_SYSCTL_DESCR 48170a172404SJamie Gritton 48180a172404SJamie Gritton vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name, 48190a172404SJamie Gritton NULL, NULL); 48200a172404SJamie Gritton #else 48210a172404SJamie Gritton char *descr; 48220a172404SJamie Gritton 48230a172404SJamie Gritton (void)asprintf(&descr, M_TEMP, "Jail may mount the %s file system", 48240a172404SJamie Gritton vfsp->vfc_name); 48250a172404SJamie Gritton vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name, 48260a172404SJamie Gritton NULL, descr); 48270a172404SJamie Gritton free(descr, M_TEMP); 48280a172404SJamie Gritton #endif 48290e5c6bd4SJamie Gritton } 4830bf3db8aaSMartin Matuska 48314b5c9cf6SEdward Tomasz Napierala #ifdef RACCT 4832097055e2SEdward Tomasz Napierala void 4833097055e2SEdward Tomasz Napierala prison_racct_foreach(void (*callback)(struct racct *racct, 483415db3c07SEdward Tomasz Napierala void *arg2, void *arg3), void (*pre)(void), void (*post)(void), 483515db3c07SEdward Tomasz Napierala void *arg2, void *arg3) 4836097055e2SEdward Tomasz Napierala { 4837a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4838097055e2SEdward Tomasz Napierala 48394b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 48404b5c9cf6SEdward Tomasz Napierala 4841097055e2SEdward Tomasz Napierala sx_slock(&allprison_lock); 484215db3c07SEdward Tomasz Napierala if (pre != NULL) 484315db3c07SEdward Tomasz Napierala (pre)(); 4844a7ad07bfSEdward Tomasz Napierala LIST_FOREACH(prr, &allprison_racct, prr_next) 4845a7ad07bfSEdward Tomasz Napierala (callback)(prr->prr_racct, arg2, arg3); 484615db3c07SEdward Tomasz Napierala if (post != NULL) 484715db3c07SEdward Tomasz Napierala (post)(); 4848097055e2SEdward Tomasz Napierala sx_sunlock(&allprison_lock); 4849097055e2SEdward Tomasz Napierala } 48500304c731SJamie Gritton 4851a7ad07bfSEdward Tomasz Napierala static struct prison_racct * 4852a7ad07bfSEdward Tomasz Napierala prison_racct_find_locked(const char *name) 4853a7ad07bfSEdward Tomasz Napierala { 4854a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4855a7ad07bfSEdward Tomasz Napierala 48564b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4857a7ad07bfSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4858a7ad07bfSEdward Tomasz Napierala 4859a7ad07bfSEdward Tomasz Napierala if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN) 4860a7ad07bfSEdward Tomasz Napierala return (NULL); 4861a7ad07bfSEdward Tomasz Napierala 4862a7ad07bfSEdward Tomasz Napierala LIST_FOREACH(prr, &allprison_racct, prr_next) { 4863a7ad07bfSEdward Tomasz Napierala if (strcmp(name, prr->prr_name) != 0) 4864a7ad07bfSEdward Tomasz Napierala continue; 4865a7ad07bfSEdward Tomasz Napierala 4866a7ad07bfSEdward Tomasz Napierala /* Found prison_racct with a matching name? */ 4867a7ad07bfSEdward Tomasz Napierala prison_racct_hold(prr); 4868a7ad07bfSEdward Tomasz Napierala return (prr); 4869a7ad07bfSEdward Tomasz Napierala } 4870a7ad07bfSEdward Tomasz Napierala 4871a7ad07bfSEdward Tomasz Napierala /* Add new prison_racct. */ 4872a7ad07bfSEdward Tomasz Napierala prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK); 4873a7ad07bfSEdward Tomasz Napierala racct_create(&prr->prr_racct); 4874a7ad07bfSEdward Tomasz Napierala 4875a7ad07bfSEdward Tomasz Napierala strcpy(prr->prr_name, name); 4876a7ad07bfSEdward Tomasz Napierala refcount_init(&prr->prr_refcount, 1); 4877a7ad07bfSEdward Tomasz Napierala LIST_INSERT_HEAD(&allprison_racct, prr, prr_next); 4878a7ad07bfSEdward Tomasz Napierala 4879a7ad07bfSEdward Tomasz Napierala return (prr); 4880a7ad07bfSEdward Tomasz Napierala } 4881a7ad07bfSEdward Tomasz Napierala 4882a7ad07bfSEdward Tomasz Napierala struct prison_racct * 4883a7ad07bfSEdward Tomasz Napierala prison_racct_find(const char *name) 4884a7ad07bfSEdward Tomasz Napierala { 4885a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4886a7ad07bfSEdward Tomasz Napierala 48874b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 48884b5c9cf6SEdward Tomasz Napierala 4889a7ad07bfSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4890a7ad07bfSEdward Tomasz Napierala prr = prison_racct_find_locked(name); 4891a7ad07bfSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4892a7ad07bfSEdward Tomasz Napierala return (prr); 4893a7ad07bfSEdward Tomasz Napierala } 4894a7ad07bfSEdward Tomasz Napierala 4895a7ad07bfSEdward Tomasz Napierala void 4896a7ad07bfSEdward Tomasz Napierala prison_racct_hold(struct prison_racct *prr) 4897a7ad07bfSEdward Tomasz Napierala { 4898a7ad07bfSEdward Tomasz Napierala 48994b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 49004b5c9cf6SEdward Tomasz Napierala 4901a7ad07bfSEdward Tomasz Napierala refcount_acquire(&prr->prr_refcount); 4902a7ad07bfSEdward Tomasz Napierala } 4903a7ad07bfSEdward Tomasz Napierala 4904c34bbd2aSEdward Tomasz Napierala static void 4905c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(struct prison_racct *prr) 4906c34bbd2aSEdward Tomasz Napierala { 4907c34bbd2aSEdward Tomasz Napierala 49084b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4909c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4910c34bbd2aSEdward Tomasz Napierala 4911c34bbd2aSEdward Tomasz Napierala if (refcount_release(&prr->prr_refcount)) { 4912c34bbd2aSEdward Tomasz Napierala racct_destroy(&prr->prr_racct); 4913c34bbd2aSEdward Tomasz Napierala LIST_REMOVE(prr, prr_next); 4914c34bbd2aSEdward Tomasz Napierala free(prr, M_PRISON_RACCT); 4915c34bbd2aSEdward Tomasz Napierala } 4916c34bbd2aSEdward Tomasz Napierala } 4917c34bbd2aSEdward Tomasz Napierala 4918a7ad07bfSEdward Tomasz Napierala void 4919a7ad07bfSEdward Tomasz Napierala prison_racct_free(struct prison_racct *prr) 4920a7ad07bfSEdward Tomasz Napierala { 4921a7ad07bfSEdward Tomasz Napierala 49224b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4923c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_UNLOCKED); 4924c34bbd2aSEdward Tomasz Napierala 49256ff4688bSMateusz Guzik if (refcount_release_if_not_last(&prr->prr_refcount)) 4926a7ad07bfSEdward Tomasz Napierala return; 4927a7ad07bfSEdward Tomasz Napierala 4928a7ad07bfSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4929c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(prr); 4930a7ad07bfSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4931a7ad07bfSEdward Tomasz Napierala } 4932a7ad07bfSEdward Tomasz Napierala 4933a7ad07bfSEdward Tomasz Napierala static void 4934a7ad07bfSEdward Tomasz Napierala prison_racct_attach(struct prison *pr) 4935a7ad07bfSEdward Tomasz Napierala { 4936a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4937a7ad07bfSEdward Tomasz Napierala 49384b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4939c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4940c34bbd2aSEdward Tomasz Napierala 4941a7ad07bfSEdward Tomasz Napierala prr = prison_racct_find_locked(pr->pr_name); 4942a7ad07bfSEdward Tomasz Napierala KASSERT(prr != NULL, ("cannot find prison_racct")); 4943a7ad07bfSEdward Tomasz Napierala 4944a7ad07bfSEdward Tomasz Napierala pr->pr_prison_racct = prr; 4945a7ad07bfSEdward Tomasz Napierala } 4946a7ad07bfSEdward Tomasz Napierala 4947c34bbd2aSEdward Tomasz Napierala /* 4948c34bbd2aSEdward Tomasz Napierala * Handle jail renaming. From the racct point of view, renaming means 4949c34bbd2aSEdward Tomasz Napierala * moving from one prison_racct to another. 4950c34bbd2aSEdward Tomasz Napierala */ 4951c34bbd2aSEdward Tomasz Napierala static void 4952c34bbd2aSEdward Tomasz Napierala prison_racct_modify(struct prison *pr) 4953c34bbd2aSEdward Tomasz Napierala { 4954dbadb015SKonstantin Belousov #ifdef RCTL 4955c34bbd2aSEdward Tomasz Napierala struct proc *p; 4956c34bbd2aSEdward Tomasz Napierala struct ucred *cred; 4957dbadb015SKonstantin Belousov #endif 4958c34bbd2aSEdward Tomasz Napierala struct prison_racct *oldprr; 4959c34bbd2aSEdward Tomasz Napierala 49604b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 49614b5c9cf6SEdward Tomasz Napierala 4962c34bbd2aSEdward Tomasz Napierala sx_slock(&allproc_lock); 4963c34bbd2aSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4964c34bbd2aSEdward Tomasz Napierala 4965e30345e7SEdward Tomasz Napierala if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) { 4966e30345e7SEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4967e30345e7SEdward Tomasz Napierala sx_sunlock(&allproc_lock); 4968c34bbd2aSEdward Tomasz Napierala return; 4969e30345e7SEdward Tomasz Napierala } 4970c34bbd2aSEdward Tomasz Napierala 4971c34bbd2aSEdward Tomasz Napierala oldprr = pr->pr_prison_racct; 4972c34bbd2aSEdward Tomasz Napierala pr->pr_prison_racct = NULL; 4973c34bbd2aSEdward Tomasz Napierala 4974c34bbd2aSEdward Tomasz Napierala prison_racct_attach(pr); 4975c34bbd2aSEdward Tomasz Napierala 4976c34bbd2aSEdward Tomasz Napierala /* 4977c34bbd2aSEdward Tomasz Napierala * Move resource utilisation records. 4978c34bbd2aSEdward Tomasz Napierala */ 4979c34bbd2aSEdward Tomasz Napierala racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct); 4980c34bbd2aSEdward Tomasz Napierala 4981f87beb93SAndriy Gapon #ifdef RCTL 4982c34bbd2aSEdward Tomasz Napierala /* 4983c34bbd2aSEdward Tomasz Napierala * Force rctl to reattach rules to processes. 4984c34bbd2aSEdward Tomasz Napierala */ 4985c34bbd2aSEdward Tomasz Napierala FOREACH_PROC_IN_SYSTEM(p) { 4986c34bbd2aSEdward Tomasz Napierala PROC_LOCK(p); 4987c34bbd2aSEdward Tomasz Napierala cred = crhold(p->p_ucred); 4988c34bbd2aSEdward Tomasz Napierala PROC_UNLOCK(p); 4989f87beb93SAndriy Gapon rctl_proc_ucred_changed(p, cred); 4990c34bbd2aSEdward Tomasz Napierala crfree(cred); 4991c34bbd2aSEdward Tomasz Napierala } 4992f87beb93SAndriy Gapon #endif 4993c34bbd2aSEdward Tomasz Napierala 4994c34bbd2aSEdward Tomasz Napierala sx_sunlock(&allproc_lock); 4995c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(oldprr); 4996c34bbd2aSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4997c34bbd2aSEdward Tomasz Napierala } 4998c34bbd2aSEdward Tomasz Napierala 4999a7ad07bfSEdward Tomasz Napierala static void 5000a7ad07bfSEdward Tomasz Napierala prison_racct_detach(struct prison *pr) 5001a7ad07bfSEdward Tomasz Napierala { 5002c34bbd2aSEdward Tomasz Napierala 50034b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 5004c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_UNLOCKED); 5005c34bbd2aSEdward Tomasz Napierala 5006af3c786cSMateusz Guzik if (pr->pr_prison_racct == NULL) 5007af3c786cSMateusz Guzik return; 5008a7ad07bfSEdward Tomasz Napierala prison_racct_free(pr->pr_prison_racct); 5009a7ad07bfSEdward Tomasz Napierala pr->pr_prison_racct = NULL; 5010a7ad07bfSEdward Tomasz Napierala } 5011a7ad07bfSEdward Tomasz Napierala #endif /* RACCT */ 5012a7ad07bfSEdward Tomasz Napierala 5013413628a7SBjoern A. Zeeb #ifdef DDB 5014b38ff370SJamie Gritton 5015b38ff370SJamie Gritton static void 5016b38ff370SJamie Gritton db_show_prison(struct prison *pr) 5017413628a7SBjoern A. Zeeb { 5018672756aaSJamie Gritton struct bool_flags *bf; 5019672756aaSJamie Gritton struct jailsys_flags *jsf; 5020b38ff370SJamie Gritton #if defined(INET) || defined(INET6) 5021b38ff370SJamie Gritton int ii; 5022500f82d6SZhenlei Huang struct prison_ip *pip; 5023413628a7SBjoern A. Zeeb #endif 5024672756aaSJamie Gritton unsigned f; 50258144690aSEric van Gyzen #ifdef INET 50268144690aSEric van Gyzen char ip4buf[INET_ADDRSTRLEN]; 50278144690aSEric van Gyzen #endif 5028413628a7SBjoern A. Zeeb #ifdef INET6 5029413628a7SBjoern A. Zeeb char ip6buf[INET6_ADDRSTRLEN]; 5030413628a7SBjoern A. Zeeb #endif 5031413628a7SBjoern A. Zeeb 5032b38ff370SJamie Gritton db_printf("prison %p:\n", pr); 5033b38ff370SJamie Gritton db_printf(" jid = %d\n", pr->pr_id); 5034b38ff370SJamie Gritton db_printf(" name = %s\n", pr->pr_name); 50350304c731SJamie Gritton db_printf(" parent = %p\n", pr->pr_parent); 5036b38ff370SJamie Gritton db_printf(" ref = %d\n", pr->pr_ref); 5037b38ff370SJamie Gritton db_printf(" uref = %d\n", pr->pr_uref); 50381158508aSJamie Gritton db_printf(" state = %s\n", 50391158508aSJamie Gritton pr->pr_state == PRISON_STATE_ALIVE ? "alive" : 50401158508aSJamie Gritton pr->pr_state == PRISON_STATE_DYING ? "dying" : 50411158508aSJamie Gritton "invalid"); 5042b38ff370SJamie Gritton db_printf(" path = %s\n", pr->pr_path); 5043b38ff370SJamie Gritton db_printf(" cpuset = %d\n", pr->pr_cpuset 5044b38ff370SJamie Gritton ? pr->pr_cpuset->cs_id : -1); 5045679e1390SJamie Gritton #ifdef VIMAGE 5046679e1390SJamie Gritton db_printf(" vnet = %p\n", pr->pr_vnet); 5047679e1390SJamie Gritton #endif 5048b38ff370SJamie Gritton db_printf(" root = %p\n", pr->pr_root); 5049b38ff370SJamie Gritton db_printf(" securelevel = %d\n", pr->pr_securelevel); 50500cc207a6SMartin Matuska db_printf(" devfs_rsnum = %d\n", pr->pr_devfs_rsnum); 5051fe0518e9SBjoern A. Zeeb db_printf(" children.max = %d\n", pr->pr_childmax); 5052fe0518e9SBjoern A. Zeeb db_printf(" children.cur = %d\n", pr->pr_childcount); 50530304c731SJamie Gritton db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children)); 50540304c731SJamie Gritton db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling)); 5055fe0518e9SBjoern A. Zeeb db_printf(" flags = 0x%x", pr->pr_flags); 5056672756aaSJamie Gritton for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++) 5057672756aaSJamie Gritton if (pr->pr_flags & bf->flag) 5058672756aaSJamie Gritton db_printf(" %s", bf->name); 5059672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 5060672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 5061672756aaSJamie Gritton jsf++) { 5062672756aaSJamie Gritton f = pr->pr_flags & (jsf->disable | jsf->new); 5063672756aaSJamie Gritton db_printf(" %-16s= %s\n", jsf->name, 5064672756aaSJamie Gritton (f != 0 && f == jsf->disable) ? "disable" 5065672756aaSJamie Gritton : (f == jsf->new) ? "new" 50667cbf7213SJamie Gritton : "inherit"); 50677cbf7213SJamie Gritton } 5068fe0518e9SBjoern A. Zeeb db_printf(" allow = 0x%x", pr->pr_allow); 5069672756aaSJamie Gritton for (bf = pr_flag_allow; 50705d58f959SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 50715d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 5072672756aaSJamie Gritton bf++) 5073672756aaSJamie Gritton if (pr->pr_allow & bf->flag) 5074672756aaSJamie Gritton db_printf(" %s", bf->name); 5075b38ff370SJamie Gritton db_printf("\n"); 50760304c731SJamie Gritton db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs); 5077c1f19219SJamie Gritton db_printf(" host.hostname = %s\n", pr->pr_hostname); 5078c1f19219SJamie Gritton db_printf(" host.domainname = %s\n", pr->pr_domainname); 5079c1f19219SJamie Gritton db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid); 508076ca6f88SJamie Gritton db_printf(" host.hostid = %lu\n", pr->pr_hostid); 5081413628a7SBjoern A. Zeeb #ifdef INET 5082500f82d6SZhenlei Huang if ((pip = pr->pr_addrs[PR_INET]) != NULL) { 5083500f82d6SZhenlei Huang db_printf(" ip4s = %d\n", pip->ips); 5084500f82d6SZhenlei Huang for (ii = 0; ii < pip->ips; ii++) 5085b38ff370SJamie Gritton db_printf(" %s %s\n", 5086fe0518e9SBjoern A. Zeeb ii == 0 ? "ip4.addr =" : " ", 5087eb8dcdeaSGleb Smirnoff inet_ntoa_r( 5088500f82d6SZhenlei Huang *(const struct in_addr *)PR_IP(pip, PR_INET, ii), 5089eb8dcdeaSGleb Smirnoff ip4buf)); 5090eb8dcdeaSGleb Smirnoff } 5091413628a7SBjoern A. Zeeb #endif 5092413628a7SBjoern A. Zeeb #ifdef INET6 5093500f82d6SZhenlei Huang if ((pip = pr->pr_addrs[PR_INET6]) != NULL) { 5094500f82d6SZhenlei Huang db_printf(" ip6s = %d\n", pip->ips); 5095500f82d6SZhenlei Huang for (ii = 0; ii < pip->ips; ii++) 5096b38ff370SJamie Gritton db_printf(" %s %s\n", 5097fe0518e9SBjoern A. Zeeb ii == 0 ? "ip6.addr =" : " ", 5098eb8dcdeaSGleb Smirnoff ip6_sprintf(ip6buf, 5099500f82d6SZhenlei Huang (const struct in6_addr *)PR_IP(pip, PR_INET6, ii))); 5100eb8dcdeaSGleb Smirnoff } 5101b38ff370SJamie Gritton #endif 5102b38ff370SJamie Gritton } 5103b38ff370SJamie Gritton 5104b38ff370SJamie Gritton DB_SHOW_COMMAND(prison, db_show_prison_command) 5105b38ff370SJamie Gritton { 5106b38ff370SJamie Gritton struct prison *pr; 5107b38ff370SJamie Gritton 5108b38ff370SJamie Gritton if (!have_addr) { 51090304c731SJamie Gritton /* 51100304c731SJamie Gritton * Show all prisons in the list, and prison0 which is not 51110304c731SJamie Gritton * listed. 51120304c731SJamie Gritton */ 51130304c731SJamie Gritton db_show_prison(&prison0); 51140304c731SJamie Gritton if (!db_pager_quit) { 5115b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 5116b38ff370SJamie Gritton db_show_prison(pr); 5117413628a7SBjoern A. Zeeb if (db_pager_quit) 5118413628a7SBjoern A. Zeeb break; 5119413628a7SBjoern A. Zeeb } 51200304c731SJamie Gritton } 5121b38ff370SJamie Gritton return; 5122413628a7SBjoern A. Zeeb } 5123b38ff370SJamie Gritton 51240304c731SJamie Gritton if (addr == 0) 51250304c731SJamie Gritton pr = &prison0; 51260304c731SJamie Gritton else { 5127b38ff370SJamie Gritton /* Look for a prison with the ID and with references. */ 5128b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 5129b38ff370SJamie Gritton if (pr->pr_id == addr && pr->pr_ref > 0) 5130b38ff370SJamie Gritton break; 5131b38ff370SJamie Gritton if (pr == NULL) 5132b38ff370SJamie Gritton /* Look again, without requiring a reference. */ 5133b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 5134b38ff370SJamie Gritton if (pr->pr_id == addr) 5135b38ff370SJamie Gritton break; 5136b38ff370SJamie Gritton if (pr == NULL) 5137b38ff370SJamie Gritton /* Assume address points to a valid prison. */ 5138b38ff370SJamie Gritton pr = (struct prison *)addr; 51390304c731SJamie Gritton } 5140b38ff370SJamie Gritton db_show_prison(pr); 5141b38ff370SJamie Gritton } 5142b38ff370SJamie Gritton 5143413628a7SBjoern A. Zeeb #endif /* DDB */ 5144