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 1690*8c75c15dSMark Johnston /* 1691*8c75c15dSMark Johnston * Allocate a new vnet if specified. 1692*8c75c15dSMark Johnston * 1693*8c75c15dSMark Johnston * Set PR_VNET now if so, so that the vnet is disposed of 1694*8c75c15dSMark Johnston * properly when the jail is destroyed. 1695*8c75c15dSMark Johnston */ 1696*8c75c15dSMark Johnston if (pr_flags & PR_VNET) { 1697*8c75c15dSMark Johnston pr->pr_flags |= PR_VNET; 1698*8c75c15dSMark Johnston pr->pr_vnet = vnet_alloc(); 1699*8c75c15dSMark Johnston } else { 1700*8c75c15dSMark Johnston pr->pr_vnet = ppr->pr_vnet; 1701*8c75c15dSMark 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) { 2555b38ff370SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 2556b38ff370SJamie Gritton error = EINVAL; 2557b38ff370SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 25582a4b2251SJamie Gritton goto done; 2559b38ff370SJamie Gritton } 2560b38ff370SJamie Gritton } 2561b38ff370SJamie Gritton 2562b38ff370SJamie Gritton /* Write the fetched parameters back to userspace. */ 2563b38ff370SJamie Gritton error = 0; 2564b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2565b38ff370SJamie Gritton if (opt->pos >= 0 && opt->pos != errmsg_pos) { 2566b38ff370SJamie Gritton pos = 2 * opt->pos + 1; 2567b38ff370SJamie Gritton optuio->uio_iov[pos].iov_len = opt->len; 2568b38ff370SJamie Gritton if (opt->value != NULL) { 2569b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) { 2570b38ff370SJamie Gritton bcopy(opt->value, 2571b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2572b38ff370SJamie Gritton opt->len); 2573b38ff370SJamie Gritton } else { 2574b38ff370SJamie Gritton error = copyout(opt->value, 2575b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2576b38ff370SJamie Gritton opt->len); 2577b38ff370SJamie Gritton if (error) 2578b38ff370SJamie Gritton break; 2579b38ff370SJamie Gritton } 2580b38ff370SJamie Gritton } 2581b38ff370SJamie Gritton } 2582b38ff370SJamie Gritton } 2583b38ff370SJamie Gritton 25842a4b2251SJamie Gritton done: 25852a4b2251SJamie Gritton /* Release any temporary prison holds and/or locks. */ 25862a4b2251SJamie Gritton if (pr != NULL) 25872a4b2251SJamie Gritton prison_deref(pr, drflags); 25882a4b2251SJamie Gritton else if (drflags & PD_LIST_SLOCKED) 2589b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2590b38ff370SJamie Gritton if (error && errmsg_pos >= 0) { 25912a4b2251SJamie Gritton /* Write the error message back to userspace. */ 2592b38ff370SJamie Gritton vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 2593b38ff370SJamie Gritton errmsg_pos = 2 * errmsg_pos + 1; 2594b38ff370SJamie Gritton if (errmsg_len > 0) { 2595b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 2596b38ff370SJamie Gritton bcopy(errmsg, 2597b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2598b38ff370SJamie Gritton errmsg_len); 2599b38ff370SJamie Gritton else 2600abbc260fSMark Johnston (void)copyout(errmsg, 2601b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2602b38ff370SJamie Gritton errmsg_len); 2603b38ff370SJamie Gritton } 2604b38ff370SJamie Gritton } 2605b38ff370SJamie Gritton vfs_freeopts(opts); 2606b38ff370SJamie Gritton return (error); 2607b38ff370SJamie Gritton } 2608b38ff370SJamie Gritton 2609b38ff370SJamie Gritton /* 2610b38ff370SJamie Gritton * struct jail_remove_args { 2611b38ff370SJamie Gritton * int jid; 2612b38ff370SJamie Gritton * }; 2613b38ff370SJamie Gritton */ 2614b38ff370SJamie Gritton int 26158451d0ddSKip Macy sys_jail_remove(struct thread *td, struct jail_remove_args *uap) 2616b38ff370SJamie Gritton { 2617c861373bSJamie Gritton struct prison *pr; 2618c861373bSJamie Gritton int error; 2619b38ff370SJamie Gritton 2620b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_REMOVE); 2621b38ff370SJamie Gritton if (error) 2622b38ff370SJamie Gritton return (error); 2623b38ff370SJamie Gritton 2624b38ff370SJamie Gritton sx_xlock(&allprison_lock); 26250304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2626b38ff370SJamie Gritton if (pr == NULL) { 2627b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2628b38ff370SJamie Gritton return (EINVAL); 2629b38ff370SJamie Gritton } 2630c861373bSJamie Gritton if (!prison_isalive(pr)) { 2631c861373bSJamie Gritton /* Silently ignore already-dying prisons. */ 26320304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 2633c861373bSJamie Gritton sx_xunlock(&allprison_lock); 26340304c731SJamie Gritton return (0); 26350304c731SJamie Gritton } 2636c861373bSJamie Gritton prison_deref(pr, PD_KILL | PD_LOCKED | PD_LIST_XLOCKED); 2637c861373bSJamie Gritton return (0); 263875c13541SPoul-Henning Kamp } 263975c13541SPoul-Henning Kamp 2640fd7a8150SMike Barcroft /* 26419ddb7954SMike Barcroft * struct jail_attach_args { 26429ddb7954SMike Barcroft * int jid; 26439ddb7954SMike Barcroft * }; 2644fd7a8150SMike Barcroft */ 2645fd7a8150SMike Barcroft int 26468451d0ddSKip Macy sys_jail_attach(struct thread *td, struct jail_attach_args *uap) 2647fd7a8150SMike Barcroft { 2648b38ff370SJamie Gritton struct prison *pr; 2649b38ff370SJamie Gritton int error; 2650b38ff370SJamie Gritton 2651b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 2652b38ff370SJamie Gritton if (error) 2653b38ff370SJamie Gritton return (error); 2654b38ff370SJamie Gritton 2655f7496dcaSJamie Gritton sx_slock(&allprison_lock); 26560304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2657b38ff370SJamie Gritton if (pr == NULL) { 2658b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2659b38ff370SJamie Gritton return (EINVAL); 2660b38ff370SJamie Gritton } 2661b38ff370SJamie Gritton 266276ad42abSJamie Gritton /* Do not allow a process to attach to a prison that is not alive. */ 266376ad42abSJamie Gritton if (!prison_isalive(pr)) { 2664b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2665b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2666b38ff370SJamie Gritton return (EINVAL); 2667b38ff370SJamie Gritton } 2668b38ff370SJamie Gritton 2669f7496dcaSJamie Gritton return (do_jail_attach(td, pr, PD_LOCKED | PD_LIST_SLOCKED)); 2670b38ff370SJamie Gritton } 2671b38ff370SJamie Gritton 2672b38ff370SJamie Gritton static int 2673f7496dcaSJamie Gritton do_jail_attach(struct thread *td, struct prison *pr, int drflags) 2674b38ff370SJamie Gritton { 2675fd7a8150SMike Barcroft struct proc *p; 2676fd7a8150SMike Barcroft struct ucred *newcred, *oldcred; 26775050aa86SKonstantin Belousov int error; 2678fd7a8150SMike Barcroft 2679f7496dcaSJamie Gritton mtx_assert(&pr->pr_mtx, MA_OWNED); 2680f7496dcaSJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 2681589e4c1dSJamie Gritton drflags &= PD_LOCK_FLAGS; 268257f22bd4SJacques Vidrine /* 268357f22bd4SJacques Vidrine * XXX: Note that there is a slight race here if two threads 268457f22bd4SJacques Vidrine * in the same privileged process attempt to attach to two 268557f22bd4SJacques Vidrine * different jails at the same time. It is important for 268657f22bd4SJacques Vidrine * user processes not to do this, or they might end up with 268757f22bd4SJacques Vidrine * a process root from one prison, but attached to the jail 268857f22bd4SJacques Vidrine * of another. 268957f22bd4SJacques Vidrine */ 26901158508aSJamie Gritton prison_hold(pr); 26916754ae25SJamie Gritton refcount_acquire(&pr->pr_uref); 2692f7496dcaSJamie Gritton drflags |= PD_DEREF | PD_DEUREF; 2693fd7a8150SMike Barcroft mtx_unlock(&pr->pr_mtx); 2694f7496dcaSJamie Gritton drflags &= ~PD_LOCKED; 2695b38ff370SJamie Gritton 2696b38ff370SJamie Gritton /* Let modules do whatever they need to prepare for attaching. */ 2697b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_ATTACH, td); 2698b38ff370SJamie Gritton if (error) { 2699f7496dcaSJamie Gritton prison_deref(pr, drflags); 2700b38ff370SJamie Gritton return (error); 2701b38ff370SJamie Gritton } 2702f7496dcaSJamie Gritton sx_unlock(&allprison_lock); 2703f7496dcaSJamie Gritton drflags &= ~(PD_LIST_SLOCKED | PD_LIST_XLOCKED); 2704fd7a8150SMike Barcroft 2705413628a7SBjoern A. Zeeb /* 2706413628a7SBjoern A. Zeeb * Reparent the newly attached process to this jail. 2707413628a7SBjoern A. Zeeb */ 2708b38ff370SJamie Gritton p = td->td_proc; 2709413628a7SBjoern A. Zeeb error = cpuset_setproc_update_set(p, pr->pr_cpuset); 2710413628a7SBjoern A. Zeeb if (error) 2711b38ff370SJamie Gritton goto e_revert_osd; 2712413628a7SBjoern A. Zeeb 2713cb05b60aSAttilio Rao vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY); 2714fd7a8150SMike Barcroft if ((error = change_dir(pr->pr_root, td)) != 0) 2715fd7a8150SMike Barcroft goto e_unlock; 2716fd7a8150SMike Barcroft #ifdef MAC 271730d239bcSRobert Watson if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root))) 2718fd7a8150SMike Barcroft goto e_unlock; 2719fd7a8150SMike Barcroft #endif 2720b249ce48SMateusz Guzik VOP_UNLOCK(pr->pr_root); 2721d4380c0cSJamie Gritton if ((error = pwd_chroot_chdir(td, pr->pr_root))) 27225050aa86SKonstantin Belousov goto e_revert_osd; 2723fd7a8150SMike Barcroft 2724fd7a8150SMike Barcroft newcred = crget(); 2725fd7a8150SMike Barcroft PROC_LOCK(p); 27261fb6767dSJamie Gritton oldcred = crcopysafe(p, newcred); 272769c4ee54SJohn Baldwin newcred->cr_prison = pr; 2728daf63fd2SMateusz Guzik proc_set_cred(p, newcred); 27291fb6767dSJamie Gritton setsugid(p); 2730097055e2SEdward Tomasz Napierala #ifdef RACCT 2731097055e2SEdward Tomasz Napierala racct_proc_ucred_changed(p, oldcred, newcred); 2732f87beb93SAndriy Gapon crhold(newcred); 2733f87beb93SAndriy Gapon #endif 2734f87beb93SAndriy Gapon PROC_UNLOCK(p); 2735f87beb93SAndriy Gapon #ifdef RCTL 2736f87beb93SAndriy Gapon rctl_proc_ucred_changed(p, newcred); 2737f87beb93SAndriy Gapon crfree(newcred); 2738097055e2SEdward Tomasz Napierala #endif 27395ecb5444SMateusz Guzik prison_proc_relink(oldcred->cr_prison, pr, p); 2740f7496dcaSJamie Gritton prison_deref(oldcred->cr_prison, drflags); 2741fd7a8150SMike Barcroft crfree(oldcred); 2742cc7b7306SJamie Gritton 2743cc7b7306SJamie Gritton /* 2744cc7b7306SJamie Gritton * If the prison was killed while changing credentials, die along 2745cc7b7306SJamie Gritton * with it. 2746cc7b7306SJamie Gritton */ 2747cc7b7306SJamie Gritton if (!prison_isalive(pr)) { 2748cc7b7306SJamie Gritton PROC_LOCK(p); 2749cc7b7306SJamie Gritton kern_psignal(p, SIGKILL); 2750cc7b7306SJamie Gritton PROC_UNLOCK(p); 2751cc7b7306SJamie Gritton } 2752cc7b7306SJamie Gritton 2753fd7a8150SMike Barcroft return (0); 27541fb6767dSJamie Gritton 2755fd7a8150SMike Barcroft e_unlock: 2756b249ce48SMateusz Guzik VOP_UNLOCK(pr->pr_root); 2757b38ff370SJamie Gritton e_revert_osd: 2758b38ff370SJamie Gritton /* Tell modules this thread is still in its old jail after all. */ 27597f4e7248SJamie Gritton sx_slock(&allprison_lock); 2760f7496dcaSJamie Gritton drflags |= PD_LIST_SLOCKED; 27611fb6767dSJamie Gritton (void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td); 2762f7496dcaSJamie Gritton prison_deref(pr, drflags); 2763fd7a8150SMike Barcroft return (error); 2764fd7a8150SMike Barcroft } 2765fd7a8150SMike Barcroft 2766fd7a8150SMike Barcroft /* 2767fd7a8150SMike Barcroft * Returns a locked prison instance, or NULL on failure. 2768fd7a8150SMike Barcroft */ 276954b369c1SPawel Jakub Dawidek struct prison * 2770fd7a8150SMike Barcroft prison_find(int prid) 2771fd7a8150SMike Barcroft { 2772fd7a8150SMike Barcroft struct prison *pr; 2773fd7a8150SMike Barcroft 2774dc68a633SPawel Jakub Dawidek sx_assert(&allprison_lock, SX_LOCKED); 2775b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 2776f7496dcaSJamie Gritton if (pr->pr_id < prid) 2777f7496dcaSJamie Gritton continue; 27787de883c8SJamie Gritton if (pr->pr_id > prid) 27797de883c8SJamie Gritton break; 2780f7496dcaSJamie Gritton KASSERT(prison_isvalid(pr), ("Found invalid prison %p", pr)); 2781f7496dcaSJamie Gritton mtx_lock(&pr->pr_mtx); 2782f7496dcaSJamie Gritton return (pr); 2783fd7a8150SMike Barcroft } 2784fd7a8150SMike Barcroft return (NULL); 2785fd7a8150SMike Barcroft } 2786fd7a8150SMike Barcroft 2787b38ff370SJamie Gritton /* 27880304c731SJamie Gritton * Find a prison that is a descendant of mypr. Returns a locked prison or NULL. 2789b38ff370SJamie Gritton */ 2790b38ff370SJamie Gritton struct prison * 27910304c731SJamie Gritton prison_find_child(struct prison *mypr, int prid) 2792b38ff370SJamie Gritton { 27930304c731SJamie Gritton struct prison *pr; 27940304c731SJamie Gritton int descend; 2795b38ff370SJamie Gritton 2796b38ff370SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 27970304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 27980304c731SJamie Gritton if (pr->pr_id == prid) { 2799f7496dcaSJamie Gritton KASSERT(prison_isvalid(pr), 2800f7496dcaSJamie Gritton ("Found invalid prison %p", pr)); 28010304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 28020304c731SJamie Gritton return (pr); 28030304c731SJamie Gritton } 28040304c731SJamie Gritton } 28050304c731SJamie Gritton return (NULL); 28060304c731SJamie Gritton } 28070304c731SJamie Gritton 28080304c731SJamie Gritton /* 28090304c731SJamie Gritton * Look for the name relative to mypr. Returns a locked prison or NULL. 28100304c731SJamie Gritton */ 28110304c731SJamie Gritton struct prison * 28120304c731SJamie Gritton prison_find_name(struct prison *mypr, const char *name) 28130304c731SJamie Gritton { 28140304c731SJamie Gritton struct prison *pr, *deadpr; 28150304c731SJamie Gritton size_t mylen; 28160304c731SJamie Gritton int descend; 28170304c731SJamie Gritton 28180304c731SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 28190304c731SJamie Gritton mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1; 2820b38ff370SJamie Gritton deadpr = NULL; 28210304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 28220304c731SJamie Gritton if (!strcmp(pr->pr_name + mylen, name)) { 2823f7496dcaSJamie Gritton KASSERT(prison_isvalid(pr), 2824f7496dcaSJamie Gritton ("Found invalid prison %p", pr)); 2825f7496dcaSJamie Gritton if (prison_isalive(pr)) { 2826b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 2827b38ff370SJamie Gritton return (pr); 2828f7496dcaSJamie Gritton } 2829b38ff370SJamie Gritton deadpr = pr; 2830b38ff370SJamie Gritton } 2831b38ff370SJamie Gritton } 28320304c731SJamie Gritton /* There was no valid prison - perhaps there was a dying one. */ 2833f7496dcaSJamie Gritton if (deadpr != NULL) 2834b38ff370SJamie Gritton mtx_lock(&deadpr->pr_mtx); 2835b38ff370SJamie Gritton return (deadpr); 2836b38ff370SJamie Gritton } 2837b38ff370SJamie Gritton 2838b38ff370SJamie Gritton /* 28390fe74ae6SJamie Gritton * See if a prison has the specific flag set. The prison should be locked, 28400fe74ae6SJamie Gritton * unless checking for flags that are only set at jail creation (such as 28410fe74ae6SJamie Gritton * PR_IP4 and PR_IP6), or only the single bit is examined, without regard 28420fe74ae6SJamie Gritton * to any other prison data. 28430304c731SJamie Gritton */ 28440b0ae2e4SMina Galić bool 28450304c731SJamie Gritton prison_flag(struct ucred *cred, unsigned flag) 28460304c731SJamie Gritton { 28470304c731SJamie Gritton 28480b0ae2e4SMina Galić return ((cred->cr_prison->pr_flags & flag) != 0); 28490304c731SJamie Gritton } 28500304c731SJamie Gritton 28510b0ae2e4SMina Galić /* 28520b0ae2e4SMina Galić * See if a prison has the specific allow flag set. 28530b0ae2e4SMina Galić * The prison *should* be locked, or only a single bit is examined, without 28540b0ae2e4SMina Galić * regard to any other prison data. 28550b0ae2e4SMina Galić */ 28560b0ae2e4SMina Galić bool 28570304c731SJamie Gritton prison_allow(struct ucred *cred, unsigned flag) 28580304c731SJamie Gritton { 28590304c731SJamie Gritton 28600fe74ae6SJamie Gritton return ((cred->cr_prison->pr_allow & flag) != 0); 28610304c731SJamie Gritton } 28620304c731SJamie Gritton 28630304c731SJamie Gritton /* 2864effad35eSJamie Gritton * Hold a prison reference, by incrementing pr_ref. It is generally 2865effad35eSJamie Gritton * an error to hold a prison that does not already have a reference. 2866effad35eSJamie Gritton * A prison record will remain valid as long as it has at least one 2867effad35eSJamie Gritton * reference, and will not be removed as long as either the prison 2868effad35eSJamie Gritton * mutex or the allprison lock is held (allprison_lock may be shared). 2869effad35eSJamie Gritton */ 2870effad35eSJamie Gritton void 2871effad35eSJamie Gritton prison_hold_locked(struct prison *pr) 2872effad35eSJamie Gritton { 2873effad35eSJamie Gritton 28746754ae25SJamie Gritton /* Locking is no longer required. */ 28756754ae25SJamie Gritton prison_hold(pr); 2876effad35eSJamie Gritton } 2877effad35eSJamie Gritton 2878effad35eSJamie Gritton void 2879effad35eSJamie Gritton prison_hold(struct prison *pr) 2880effad35eSJamie Gritton { 28816754ae25SJamie Gritton #ifdef INVARIANTS 28826754ae25SJamie Gritton int was_valid = refcount_acquire_if_not_zero(&pr->pr_ref); 2883effad35eSJamie Gritton 28846754ae25SJamie Gritton KASSERT(was_valid, 28856754ae25SJamie Gritton ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id)); 28866754ae25SJamie Gritton #else 28876754ae25SJamie Gritton refcount_acquire(&pr->pr_ref); 28886754ae25SJamie Gritton #endif 2889effad35eSJamie Gritton } 2890effad35eSJamie Gritton 2891effad35eSJamie Gritton /* 2892effad35eSJamie Gritton * Remove a prison reference. If that was the last reference, the 2893f7496dcaSJamie Gritton * prison will be removed (at a later time). 2894b38ff370SJamie Gritton */ 289591421ba2SRobert Watson void 28961ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr) 289791421ba2SRobert Watson { 289891421ba2SRobert Watson 28991ba4a712SPawel Jakub Dawidek mtx_assert(&pr->pr_mtx, MA_OWNED); 2900effad35eSJamie Gritton /* 2901f7496dcaSJamie Gritton * Locking is no longer required, but unlock because the caller 2902f7496dcaSJamie Gritton * expects it. 2903effad35eSJamie Gritton */ 2904f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 2905f7496dcaSJamie Gritton prison_free(pr); 2906effad35eSJamie Gritton } 2907c2cda609SPawel Jakub Dawidek 29081ba4a712SPawel Jakub Dawidek void 29091ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr) 29101ba4a712SPawel Jakub Dawidek { 29111ba4a712SPawel Jakub Dawidek 29126754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 29136754ae25SJamie Gritton ("Trying to free dead prison %p (jid=%d).", 29146754ae25SJamie Gritton pr, pr->pr_id)); 2915f7496dcaSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_ref)) { 2916f7496dcaSJamie Gritton /* 2917f7496dcaSJamie Gritton * Don't remove the last reference in this context, 2918f7496dcaSJamie Gritton * in case there are locks held. 2919f7496dcaSJamie Gritton */ 29208cf955f3SMark Johnston taskqueue_enqueue(taskqueue_jail_remove, &pr->pr_task); 2921f7496dcaSJamie Gritton } 2922f7496dcaSJamie Gritton } 2923f7496dcaSJamie Gritton 2924f7496dcaSJamie Gritton static void 2925f7496dcaSJamie Gritton prison_free_not_last(struct prison *pr) 2926f7496dcaSJamie Gritton { 2927f7496dcaSJamie Gritton #ifdef INVARIANTS 2928f7496dcaSJamie Gritton int lastref; 2929f7496dcaSJamie Gritton 2930f7496dcaSJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 2931f7496dcaSJamie Gritton ("Trying to free dead prison %p (jid=%d).", 2932f7496dcaSJamie Gritton pr, pr->pr_id)); 2933f7496dcaSJamie Gritton lastref = refcount_release(&pr->pr_ref); 2934f7496dcaSJamie Gritton KASSERT(!lastref, 2935f7496dcaSJamie Gritton ("prison_free_not_last freed last ref on prison %p (jid=%d).", 2936f7496dcaSJamie Gritton pr, pr->pr_id)); 2937f7496dcaSJamie Gritton #else 2938ee9b37aeSMateusz Guzik refcount_release(&pr->pr_ref); 2939f7496dcaSJamie Gritton #endif 29401ba4a712SPawel Jakub Dawidek } 29411ba4a712SPawel Jakub Dawidek 294273d9e52dSJamie Gritton /* 2943f171938cSGordon Bergling * Hold a prison for user visibility, by incrementing pr_uref. 2944effad35eSJamie Gritton * It is generally an error to hold a prison that isn't already 294549a033d8SGordon Bergling * user-visible, except through the jail system calls. It is also 2946effad35eSJamie Gritton * an error to hold an invalid prison. A prison record will remain 2947effad35eSJamie Gritton * alive as long as it has at least one user reference, and will not 2948f7496dcaSJamie Gritton * be set to the dying state until the prison mutex and allprison_lock 2949f7496dcaSJamie Gritton * are both freed. 2950effad35eSJamie Gritton */ 2951effad35eSJamie Gritton void 2952effad35eSJamie Gritton prison_proc_hold(struct prison *pr) 2953effad35eSJamie Gritton { 29546754ae25SJamie Gritton #ifdef INVARIANTS 29556754ae25SJamie Gritton int was_alive = refcount_acquire_if_not_zero(&pr->pr_uref); 2956effad35eSJamie Gritton 29576754ae25SJamie Gritton KASSERT(was_alive, 2958effad35eSJamie Gritton ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id)); 29596754ae25SJamie Gritton #else 29606754ae25SJamie Gritton refcount_acquire(&pr->pr_uref); 29616754ae25SJamie Gritton #endif 2962effad35eSJamie Gritton } 2963effad35eSJamie Gritton 2964effad35eSJamie Gritton /* 2965effad35eSJamie Gritton * Remove a prison user reference. If it was the last reference, the 2966effad35eSJamie Gritton * prison will be considered "dying", and may be removed once all of 2967effad35eSJamie Gritton * its references are dropped. 2968effad35eSJamie Gritton */ 2969effad35eSJamie Gritton void 2970effad35eSJamie Gritton prison_proc_free(struct prison *pr) 2971effad35eSJamie Gritton { 2972effad35eSJamie Gritton 29736754ae25SJamie Gritton /* 29746754ae25SJamie Gritton * Locking is only required when releasing the last reference. 29756754ae25SJamie Gritton * This allows assurance that a locked prison will remain alive 29766754ae25SJamie Gritton * until it is unlocked. 29776754ae25SJamie Gritton */ 29786754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_uref) > 0, 2979effad35eSJamie Gritton ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id)); 2980195cd6aeSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_uref)) { 2981effad35eSJamie Gritton /* 2982effad35eSJamie Gritton * Don't remove the last user reference in this context, 2983effad35eSJamie Gritton * which is expected to be a process that is not only locked, 2984effad35eSJamie Gritton * but also half dead. Add a reference so any calls to 2985effad35eSJamie Gritton * prison_free() won't re-submit the task. 2986effad35eSJamie Gritton */ 2987f7496dcaSJamie Gritton prison_hold(pr); 29881158508aSJamie Gritton mtx_lock(&pr->pr_mtx); 29891158508aSJamie Gritton KASSERT(!(pr->pr_flags & PR_COMPLETE_PROC), 29901158508aSJamie Gritton ("Redundant last reference in prison_proc_free (jid=%d)", 29911158508aSJamie Gritton pr->pr_id)); 29921158508aSJamie Gritton pr->pr_flags |= PR_COMPLETE_PROC; 29931158508aSJamie Gritton mtx_unlock(&pr->pr_mtx); 29948cf955f3SMark Johnston taskqueue_enqueue(taskqueue_jail_remove, &pr->pr_task); 2995effad35eSJamie Gritton } 2996effad35eSJamie Gritton } 2997effad35eSJamie Gritton 2998c861373bSJamie Gritton static void 2999c861373bSJamie Gritton prison_proc_free_not_last(struct prison *pr) 3000c861373bSJamie Gritton { 3001c861373bSJamie Gritton #ifdef INVARIANTS 3002c861373bSJamie Gritton int lastref; 3003c861373bSJamie Gritton 3004c861373bSJamie Gritton KASSERT(refcount_load(&pr->pr_uref) > 0, 3005c861373bSJamie Gritton ("Trying to free dead prison %p (jid=%d).", 3006c861373bSJamie Gritton pr, pr->pr_id)); 3007c861373bSJamie Gritton lastref = refcount_release(&pr->pr_uref); 3008c861373bSJamie Gritton KASSERT(!lastref, 3009c861373bSJamie Gritton ("prison_proc_free_not_last freed last uref on prison %p (jid=%d).", 3010c861373bSJamie Gritton pr, pr->pr_id)); 3011c861373bSJamie Gritton #else 3012c861373bSJamie Gritton refcount_release(&pr->pr_uref); 3013c861373bSJamie Gritton #endif 3014c861373bSJamie Gritton } 3015c861373bSJamie Gritton 30165ecb5444SMateusz Guzik void 30175ecb5444SMateusz Guzik prison_proc_link(struct prison *pr, struct proc *p) 30185ecb5444SMateusz Guzik { 30195ecb5444SMateusz Guzik 30205ecb5444SMateusz Guzik sx_assert(&allproc_lock, SA_XLOCKED); 30215ecb5444SMateusz Guzik LIST_INSERT_HEAD(&pr->pr_proclist, p, p_jaillist); 30225ecb5444SMateusz Guzik } 30235ecb5444SMateusz Guzik 30245ecb5444SMateusz Guzik void 30255ecb5444SMateusz Guzik prison_proc_unlink(struct prison *pr, struct proc *p) 30265ecb5444SMateusz Guzik { 30275ecb5444SMateusz Guzik 30285ecb5444SMateusz Guzik sx_assert(&allproc_lock, SA_XLOCKED); 30295ecb5444SMateusz Guzik LIST_REMOVE(p, p_jaillist); 30305ecb5444SMateusz Guzik } 30315ecb5444SMateusz Guzik 30325ecb5444SMateusz Guzik static void 30335ecb5444SMateusz Guzik prison_proc_relink(struct prison *opr, struct prison *npr, struct proc *p) 30345ecb5444SMateusz Guzik { 30355ecb5444SMateusz Guzik 30365ecb5444SMateusz Guzik sx_xlock(&allproc_lock); 30375ecb5444SMateusz Guzik prison_proc_unlink(opr, p); 30385ecb5444SMateusz Guzik prison_proc_link(npr, p); 30395ecb5444SMateusz Guzik sx_xunlock(&allproc_lock); 30405ecb5444SMateusz Guzik } 30415ecb5444SMateusz Guzik 3042effad35eSJamie Gritton /* 304373d9e52dSJamie Gritton * Complete a call to either prison_free or prison_proc_free. 304473d9e52dSJamie Gritton */ 3045c2cda609SPawel Jakub Dawidek static void 3046c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending) 3047c2cda609SPawel Jakub Dawidek { 304873d9e52dSJamie Gritton struct prison *pr = context; 3049f7496dcaSJamie Gritton int drflags; 3050b38ff370SJamie Gritton 3051effad35eSJamie Gritton /* 30521158508aSJamie Gritton * This could be called to release the last reference, or the last 30531158508aSJamie Gritton * user reference (plus the reference held in prison_proc_free). 3054effad35eSJamie Gritton */ 3055f7496dcaSJamie Gritton drflags = prison_lock_xlock(pr, PD_DEREF); 30561158508aSJamie Gritton if (pr->pr_flags & PR_COMPLETE_PROC) { 30571158508aSJamie Gritton pr->pr_flags &= ~PR_COMPLETE_PROC; 3058f7496dcaSJamie Gritton drflags |= PD_DEUREF; 30591158508aSJamie Gritton } 3060f7496dcaSJamie Gritton prison_deref(pr, drflags); 3061b38ff370SJamie Gritton } 3062b38ff370SJamie Gritton 30635ecb5444SMateusz Guzik static void 30645ecb5444SMateusz Guzik prison_kill_processes_cb(struct proc *p, void *arg __unused) 30655ecb5444SMateusz Guzik { 30665ecb5444SMateusz Guzik 30675ecb5444SMateusz Guzik kern_psignal(p, SIGKILL); 30685ecb5444SMateusz Guzik } 30695ecb5444SMateusz Guzik 30705ecb5444SMateusz Guzik /* 30715ecb5444SMateusz Guzik * Note the iteration does not guarantee acting on all processes. 30725ecb5444SMateusz Guzik * Most notably there may be fork or jail_attach in progress. 30735ecb5444SMateusz Guzik */ 30745ecb5444SMateusz Guzik void 30755ecb5444SMateusz Guzik prison_proc_iterate(struct prison *pr, void (*cb)(struct proc *, void *), 30765ecb5444SMateusz Guzik void *cbarg) 30775ecb5444SMateusz Guzik { 30785ecb5444SMateusz Guzik struct prison *ppr; 30795ecb5444SMateusz Guzik struct proc *p; 30805ecb5444SMateusz Guzik 30815ecb5444SMateusz Guzik if (atomic_load_int(&pr->pr_childcount) == 0) { 30825ecb5444SMateusz Guzik sx_slock(&allproc_lock); 30835ecb5444SMateusz Guzik LIST_FOREACH(p, &pr->pr_proclist, p_jaillist) { 30845ecb5444SMateusz Guzik if (p->p_state == PRS_NEW) 30855ecb5444SMateusz Guzik continue; 30865ecb5444SMateusz Guzik PROC_LOCK(p); 30875ecb5444SMateusz Guzik cb(p, cbarg); 30885ecb5444SMateusz Guzik PROC_UNLOCK(p); 30895ecb5444SMateusz Guzik } 30905ecb5444SMateusz Guzik sx_sunlock(&allproc_lock); 30915ecb5444SMateusz Guzik if (atomic_load_int(&pr->pr_childcount) == 0) 30925ecb5444SMateusz Guzik return; 30935ecb5444SMateusz Guzik /* 30945ecb5444SMateusz Guzik * Some jails popped up during the iteration, fall through to a 30955ecb5444SMateusz Guzik * system-wide search. 30965ecb5444SMateusz Guzik */ 30975ecb5444SMateusz Guzik } 30985ecb5444SMateusz Guzik 30995ecb5444SMateusz Guzik sx_slock(&allproc_lock); 31005ecb5444SMateusz Guzik FOREACH_PROC_IN_SYSTEM(p) { 31015ecb5444SMateusz Guzik PROC_LOCK(p); 31025ecb5444SMateusz Guzik if (p->p_state != PRS_NEW && p->p_ucred != NULL) { 3103831531a8SKonstantin Belousov for (ppr = p->p_ucred->cr_prison; ppr != NULL; 31045ecb5444SMateusz Guzik ppr = ppr->pr_parent) { 31055ecb5444SMateusz Guzik if (ppr == pr) { 31065ecb5444SMateusz Guzik cb(p, cbarg); 31075ecb5444SMateusz Guzik break; 31085ecb5444SMateusz Guzik } 31095ecb5444SMateusz Guzik } 31105ecb5444SMateusz Guzik } 31115ecb5444SMateusz Guzik PROC_UNLOCK(p); 31125ecb5444SMateusz Guzik } 31135ecb5444SMateusz Guzik sx_sunlock(&allproc_lock); 31145ecb5444SMateusz Guzik } 31155ecb5444SMateusz Guzik 3116b38ff370SJamie Gritton /* 3117effad35eSJamie Gritton * Remove a prison reference and/or user reference (usually). 3118effad35eSJamie Gritton * This assumes context that allows sleeping (for allprison_lock), 3119effad35eSJamie Gritton * with no non-sleeping locks held, except perhaps the prison itself. 3120effad35eSJamie Gritton * If there are no more references, release and delist the prison. 3121effad35eSJamie Gritton * On completion, the prison lock and the allprison lock are both 3122effad35eSJamie Gritton * unlocked. 3123b38ff370SJamie Gritton */ 3124b38ff370SJamie Gritton static void 3125b38ff370SJamie Gritton prison_deref(struct prison *pr, int flags) 3126b38ff370SJamie Gritton { 31276e1d1bfcSJamie Gritton struct prisonlist freeprison; 3128c861373bSJamie Gritton struct prison *killpr, *rpr, *ppr, *tpr; 3129c2cda609SPawel Jakub Dawidek 3130c861373bSJamie Gritton killpr = NULL; 31316e1d1bfcSJamie Gritton TAILQ_INIT(&freeprison); 31326e1d1bfcSJamie Gritton /* 31336e1d1bfcSJamie Gritton * Release this prison as requested, which may cause its parent 31346e1d1bfcSJamie Gritton * to be released, and then maybe its grandparent, etc. 31356e1d1bfcSJamie Gritton */ 31360304c731SJamie Gritton for (;;) { 3137c861373bSJamie Gritton if (flags & PD_KILL) { 3138c861373bSJamie Gritton /* Kill the prison and its descendents. */ 3139589e4c1dSJamie Gritton KASSERT(pr != &prison0, 3140589e4c1dSJamie Gritton ("prison_deref trying to kill prison0")); 3141c861373bSJamie Gritton if (!(flags & PD_DEREF)) { 3142c861373bSJamie Gritton prison_hold(pr); 3143c861373bSJamie Gritton flags |= PD_DEREF; 3144c861373bSJamie Gritton } 3145c861373bSJamie Gritton flags = prison_lock_xlock(pr, flags); 3146c861373bSJamie Gritton prison_deref_kill(pr, &freeprison); 3147c861373bSJamie Gritton } 3148e6d5cb63SJamie Gritton if (flags & PD_DEUREF) { 3149f7496dcaSJamie Gritton /* Drop a user reference. */ 31506754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_uref) > 0, 315173d9e52dSJamie Gritton ("prison_deref PD_DEUREF on a dead prison (jid=%d)", 315273d9e52dSJamie Gritton pr->pr_id)); 3153f7496dcaSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_uref)) { 3154f7496dcaSJamie Gritton if (!(flags & PD_DEREF)) { 3155f7496dcaSJamie Gritton prison_hold(pr); 3156f7496dcaSJamie Gritton flags |= PD_DEREF; 3157f7496dcaSJamie Gritton } 3158f7496dcaSJamie Gritton flags = prison_lock_xlock(pr, flags); 31591158508aSJamie Gritton if (refcount_release(&pr->pr_uref) && 31601158508aSJamie Gritton pr->pr_state == PRISON_STATE_ALIVE) { 3161f7496dcaSJamie Gritton /* 3162f7496dcaSJamie Gritton * When the last user references goes, 3163f7496dcaSJamie Gritton * this becomes a dying prison. 3164f7496dcaSJamie Gritton */ 3165f7496dcaSJamie Gritton KASSERT( 3166f7496dcaSJamie Gritton refcount_load(&prison0.pr_uref) > 0, 31676754ae25SJamie Gritton ("prison0 pr_uref=0")); 31681158508aSJamie Gritton pr->pr_state = PRISON_STATE_DYING; 3169f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 3170f7496dcaSJamie Gritton flags &= ~PD_LOCKED; 3171a9f7455cSJamie Gritton prison_cleanup(pr); 3172f7496dcaSJamie Gritton } 3173f7496dcaSJamie Gritton } 3174f7496dcaSJamie Gritton } 3175c861373bSJamie Gritton if (flags & PD_KILL) { 3176c861373bSJamie Gritton /* 3177c861373bSJamie Gritton * Any remaining user references are probably processes 3178c861373bSJamie Gritton * that need to be killed, either in this prison or its 3179c861373bSJamie Gritton * descendants. 3180c861373bSJamie Gritton */ 3181c861373bSJamie Gritton if (refcount_load(&pr->pr_uref) > 0) 3182c861373bSJamie Gritton killpr = pr; 3183589e4c1dSJamie Gritton /* Make sure the parent prison doesn't get killed. */ 3184589e4c1dSJamie Gritton flags &= ~PD_KILL; 3185c861373bSJamie Gritton } 318673d9e52dSJamie Gritton if (flags & PD_DEREF) { 3187f7496dcaSJamie Gritton /* Drop a reference. */ 31886754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 318973d9e52dSJamie Gritton ("prison_deref PD_DEREF on a dead prison (jid=%d)", 319073d9e52dSJamie Gritton pr->pr_id)); 3191f7496dcaSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_ref)) { 3192f7496dcaSJamie Gritton flags = prison_lock_xlock(pr, flags); 3193f7496dcaSJamie Gritton if (refcount_release(&pr->pr_ref)) { 3194cc5fd8c7SJamie Gritton /* 3195f7496dcaSJamie Gritton * When the last reference goes, 3196f7496dcaSJamie Gritton * unlink the prison and set it aside. 3197cc5fd8c7SJamie Gritton */ 3198f7496dcaSJamie Gritton KASSERT( 3199f7496dcaSJamie Gritton refcount_load(&pr->pr_uref) == 0, 3200f7496dcaSJamie Gritton ("prison_deref: last ref, " 3201f7496dcaSJamie Gritton "but still has %d urefs (jid=%d)", 3202f7496dcaSJamie Gritton pr->pr_uref, pr->pr_id)); 3203f7496dcaSJamie Gritton KASSERT( 3204f7496dcaSJamie Gritton refcount_load(&prison0.pr_ref) != 0, 3205f7496dcaSJamie Gritton ("prison0 pr_ref=0")); 32061158508aSJamie Gritton pr->pr_state = PRISON_STATE_INVALID; 3207b38ff370SJamie Gritton TAILQ_REMOVE(&allprison, pr, pr_list); 32080304c731SJamie Gritton LIST_REMOVE(pr, pr_sibling); 3209f7496dcaSJamie Gritton TAILQ_INSERT_TAIL(&freeprison, pr, 3210f7496dcaSJamie Gritton pr_list); 3211f7496dcaSJamie Gritton for (ppr = pr->pr_parent; 3212f7496dcaSJamie Gritton ppr != NULL; 3213f7496dcaSJamie Gritton ppr = ppr->pr_parent) 3214f7496dcaSJamie Gritton ppr->pr_childcount--; 3215f7496dcaSJamie Gritton /* 3216f7496dcaSJamie Gritton * Removing a prison frees references 3217f7496dcaSJamie Gritton * from its parent. 3218f7496dcaSJamie Gritton */ 3219*8c75c15dSMark Johnston ppr = pr->pr_parent; 3220*8c75c15dSMark Johnston pr->pr_parent = NULL; 3221f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 3222*8c75c15dSMark Johnston 3223*8c75c15dSMark Johnston pr = ppr; 3224f7496dcaSJamie Gritton flags &= ~PD_LOCKED; 32256e1d1bfcSJamie Gritton flags |= PD_DEREF | PD_DEUREF; 3226f7496dcaSJamie Gritton continue; 3227f7496dcaSJamie Gritton } 3228f7496dcaSJamie Gritton } 3229f7496dcaSJamie Gritton } 3230f7496dcaSJamie Gritton break; 32316e1d1bfcSJamie Gritton } 32326e1d1bfcSJamie Gritton 32336e1d1bfcSJamie Gritton /* Release all the prison locks. */ 3234f7496dcaSJamie Gritton if (flags & PD_LOCKED) 3235f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 32366e1d1bfcSJamie Gritton if (flags & PD_LIST_SLOCKED) 32376e1d1bfcSJamie Gritton sx_sunlock(&allprison_lock); 32386e1d1bfcSJamie Gritton else if (flags & PD_LIST_XLOCKED) 32396e1d1bfcSJamie Gritton sx_xunlock(&allprison_lock); 32406e1d1bfcSJamie Gritton 3241c861373bSJamie Gritton /* Kill any processes attached to a killed prison. */ 32425ecb5444SMateusz Guzik if (killpr != NULL) 32435ecb5444SMateusz Guzik prison_proc_iterate(killpr, prison_kill_processes_cb, NULL); 3244c861373bSJamie Gritton 32456e1d1bfcSJamie Gritton /* 32466e1d1bfcSJamie Gritton * Finish removing any unreferenced prisons, which couldn't happen 32476e1d1bfcSJamie Gritton * while allprison_lock was held (to avoid a LOR on vrele). 32486e1d1bfcSJamie Gritton */ 32496e1d1bfcSJamie Gritton TAILQ_FOREACH_SAFE(rpr, &freeprison, pr_list, tpr) { 32506e1d1bfcSJamie Gritton #ifdef VIMAGE 3251*8c75c15dSMark Johnston if (rpr->pr_flags & PR_VNET) 32526e1d1bfcSJamie Gritton vnet_destroy(rpr->pr_vnet); 32536e1d1bfcSJamie Gritton #endif 32546e1d1bfcSJamie Gritton if (rpr->pr_root != NULL) 32556e1d1bfcSJamie Gritton vrele(rpr->pr_root); 32566e1d1bfcSJamie Gritton mtx_destroy(&rpr->pr_mtx); 32576e1d1bfcSJamie Gritton #ifdef INET 3258eb8dcdeaSGleb Smirnoff prison_ip_free(rpr->pr_addrs[PR_INET]); 32596e1d1bfcSJamie Gritton #endif 32606e1d1bfcSJamie Gritton #ifdef INET6 3261eb8dcdeaSGleb Smirnoff prison_ip_free(rpr->pr_addrs[PR_INET6]); 32626e1d1bfcSJamie Gritton #endif 32636e1d1bfcSJamie Gritton if (rpr->pr_cpuset != NULL) 32646e1d1bfcSJamie Gritton cpuset_rel(rpr->pr_cpuset); 32656e1d1bfcSJamie Gritton osd_jail_exit(rpr); 32666e1d1bfcSJamie Gritton #ifdef RACCT 32676e1d1bfcSJamie Gritton if (racct_enable) 32686e1d1bfcSJamie Gritton prison_racct_detach(rpr); 32696e1d1bfcSJamie Gritton #endif 3270f7496dcaSJamie Gritton TAILQ_REMOVE(&freeprison, rpr, pr_list); 32716e1d1bfcSJamie Gritton free(rpr, M_PRISON); 32720304c731SJamie Gritton } 3273b3059e09SRobert Watson } 3274b3059e09SRobert Watson 3275413628a7SBjoern A. Zeeb /* 3276c861373bSJamie Gritton * Kill the prison and its descendants. Mark them as dying, clear the 3277c861373bSJamie Gritton * persist flag, and call module remove methods. 3278c861373bSJamie Gritton */ 3279c861373bSJamie Gritton static void 3280c861373bSJamie Gritton prison_deref_kill(struct prison *pr, struct prisonlist *freeprison) 3281c861373bSJamie Gritton { 3282c861373bSJamie Gritton struct prison *cpr, *ppr, *rpr; 3283c861373bSJamie Gritton bool descend; 3284c861373bSJamie Gritton 3285c861373bSJamie Gritton /* 3286c861373bSJamie Gritton * Unlike the descendants, the target prison can be killed 3287c861373bSJamie Gritton * even if it is currently dying. This is useful for failed 3288c861373bSJamie Gritton * creation in jail_set(2). 3289c861373bSJamie Gritton */ 3290c861373bSJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 3291c861373bSJamie Gritton ("Trying to kill dead prison %p (jid=%d).", 3292c861373bSJamie Gritton pr, pr->pr_id)); 3293c861373bSJamie Gritton refcount_acquire(&pr->pr_uref); 3294c861373bSJamie Gritton pr->pr_state = PRISON_STATE_DYING; 3295c861373bSJamie Gritton mtx_unlock(&pr->pr_mtx); 3296c861373bSJamie Gritton 3297c861373bSJamie Gritton rpr = NULL; 3298c861373bSJamie Gritton FOREACH_PRISON_DESCENDANT_PRE_POST(pr, cpr, descend) { 3299c861373bSJamie Gritton if (descend) { 3300c861373bSJamie Gritton if (!prison_isalive(cpr)) { 3301c861373bSJamie Gritton descend = false; 3302c861373bSJamie Gritton continue; 3303c861373bSJamie Gritton } 3304c861373bSJamie Gritton prison_hold(cpr); 3305c861373bSJamie Gritton prison_proc_hold(cpr); 3306c861373bSJamie Gritton mtx_lock(&cpr->pr_mtx); 3307c861373bSJamie Gritton cpr->pr_state = PRISON_STATE_DYING; 3308c861373bSJamie Gritton cpr->pr_flags |= PR_REMOVE; 3309c861373bSJamie Gritton mtx_unlock(&cpr->pr_mtx); 3310c861373bSJamie Gritton continue; 3311c861373bSJamie Gritton } 3312c861373bSJamie Gritton if (!(cpr->pr_flags & PR_REMOVE)) 3313c861373bSJamie Gritton continue; 3314a9f7455cSJamie Gritton prison_cleanup(cpr); 3315c861373bSJamie Gritton mtx_lock(&cpr->pr_mtx); 3316c861373bSJamie Gritton cpr->pr_flags &= ~PR_REMOVE; 3317c861373bSJamie Gritton if (cpr->pr_flags & PR_PERSIST) { 3318c861373bSJamie Gritton cpr->pr_flags &= ~PR_PERSIST; 3319c861373bSJamie Gritton prison_proc_free_not_last(cpr); 3320c861373bSJamie Gritton prison_free_not_last(cpr); 3321c861373bSJamie Gritton } 3322c861373bSJamie Gritton (void)refcount_release(&cpr->pr_uref); 3323c861373bSJamie Gritton if (refcount_release(&cpr->pr_ref)) { 3324c861373bSJamie Gritton /* 3325c861373bSJamie Gritton * When the last reference goes, unlink the prison 3326c861373bSJamie Gritton * and set it aside for prison_deref() to handle. 3327c861373bSJamie Gritton * Delay unlinking the sibling list to keep the loop 3328c861373bSJamie Gritton * safe. 3329c861373bSJamie Gritton */ 3330c861373bSJamie Gritton if (rpr != NULL) 3331c861373bSJamie Gritton LIST_REMOVE(rpr, pr_sibling); 3332c861373bSJamie Gritton rpr = cpr; 3333c861373bSJamie Gritton rpr->pr_state = PRISON_STATE_INVALID; 3334c861373bSJamie Gritton TAILQ_REMOVE(&allprison, rpr, pr_list); 3335c861373bSJamie Gritton TAILQ_INSERT_TAIL(freeprison, rpr, pr_list); 3336c861373bSJamie Gritton /* 3337c861373bSJamie Gritton * Removing a prison frees references from its parent. 3338c861373bSJamie Gritton */ 3339c861373bSJamie Gritton ppr = rpr->pr_parent; 3340c861373bSJamie Gritton prison_proc_free_not_last(ppr); 3341c861373bSJamie Gritton prison_free_not_last(ppr); 3342c861373bSJamie Gritton for (; ppr != NULL; ppr = ppr->pr_parent) 3343c861373bSJamie Gritton ppr->pr_childcount--; 3344c861373bSJamie Gritton } 3345c861373bSJamie Gritton mtx_unlock(&cpr->pr_mtx); 3346c861373bSJamie Gritton } 3347c861373bSJamie Gritton if (rpr != NULL) 3348c861373bSJamie Gritton LIST_REMOVE(rpr, pr_sibling); 3349c861373bSJamie Gritton 3350a9f7455cSJamie Gritton prison_cleanup(pr); 3351c861373bSJamie Gritton mtx_lock(&pr->pr_mtx); 3352c861373bSJamie Gritton if (pr->pr_flags & PR_PERSIST) { 3353c861373bSJamie Gritton pr->pr_flags &= ~PR_PERSIST; 3354c861373bSJamie Gritton prison_proc_free_not_last(pr); 3355c861373bSJamie Gritton prison_free_not_last(pr); 3356c861373bSJamie Gritton } 3357c861373bSJamie Gritton (void)refcount_release(&pr->pr_uref); 3358c861373bSJamie Gritton } 3359c861373bSJamie Gritton 3360c861373bSJamie Gritton /* 3361f7496dcaSJamie Gritton * Given the current locking state in the flags, make sure allprison_lock 3362f7496dcaSJamie Gritton * is held exclusive, and the prison is locked. Return flags indicating 3363f7496dcaSJamie Gritton * the new state. 3364f7496dcaSJamie Gritton */ 3365f7496dcaSJamie Gritton static int 3366f7496dcaSJamie Gritton prison_lock_xlock(struct prison *pr, int flags) 3367f7496dcaSJamie Gritton { 3368f7496dcaSJamie Gritton 3369f7496dcaSJamie Gritton if (!(flags & PD_LIST_XLOCKED)) { 3370f7496dcaSJamie Gritton /* 3371f7496dcaSJamie Gritton * Get allprison_lock, which may be an upgrade, 3372f7496dcaSJamie Gritton * and may require unlocking the prison. 3373f7496dcaSJamie Gritton */ 3374f7496dcaSJamie Gritton if (flags & PD_LOCKED) { 3375f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 3376f7496dcaSJamie Gritton flags &= ~PD_LOCKED; 3377f7496dcaSJamie Gritton } 3378f7496dcaSJamie Gritton if (flags & PD_LIST_SLOCKED) { 3379f7496dcaSJamie Gritton if (!sx_try_upgrade(&allprison_lock)) { 3380f7496dcaSJamie Gritton sx_sunlock(&allprison_lock); 3381f7496dcaSJamie Gritton sx_xlock(&allprison_lock); 3382f7496dcaSJamie Gritton } 3383f7496dcaSJamie Gritton flags &= ~PD_LIST_SLOCKED; 3384f7496dcaSJamie Gritton } else 3385f7496dcaSJamie Gritton sx_xlock(&allprison_lock); 3386f7496dcaSJamie Gritton flags |= PD_LIST_XLOCKED; 3387f7496dcaSJamie Gritton } 3388f7496dcaSJamie Gritton if (!(flags & PD_LOCKED)) { 3389f7496dcaSJamie Gritton /* Lock the prison mutex. */ 3390f7496dcaSJamie Gritton mtx_lock(&pr->pr_mtx); 3391f7496dcaSJamie Gritton flags |= PD_LOCKED; 3392f7496dcaSJamie Gritton } 3393f7496dcaSJamie Gritton return flags; 3394f7496dcaSJamie Gritton } 3395f7496dcaSJamie Gritton 3396f7496dcaSJamie Gritton /* 3397a9f7455cSJamie Gritton * Release a prison's resources when it starts dying (when the last user 3398a9f7455cSJamie Gritton * reference is dropped, or when it is killed). 3399a9f7455cSJamie Gritton */ 3400a9f7455cSJamie Gritton static void 3401a9f7455cSJamie Gritton prison_cleanup(struct prison *pr) 3402a9f7455cSJamie Gritton { 3403a9f7455cSJamie Gritton sx_assert(&allprison_lock, SA_XLOCKED); 3404a9f7455cSJamie Gritton mtx_assert(&pr->pr_mtx, MA_NOTOWNED); 340588175af8SRick Macklem vfs_exjail_delete(pr); 34067060da62SJamie Gritton shm_remove_prison(pr); 3407a9f7455cSJamie Gritton (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL); 3408a9f7455cSJamie Gritton } 3409a9f7455cSJamie Gritton 3410a9f7455cSJamie Gritton /* 34110fe74ae6SJamie Gritton * Set or clear a permission bit in the pr_allow field, passing restrictions 34120fe74ae6SJamie Gritton * (cleared permission) down to child jails. 34130fe74ae6SJamie Gritton */ 34140fe74ae6SJamie Gritton void 34150fe74ae6SJamie Gritton prison_set_allow(struct ucred *cred, unsigned flag, int enable) 34160fe74ae6SJamie Gritton { 34170fe74ae6SJamie Gritton struct prison *pr; 34180fe74ae6SJamie Gritton 34190fe74ae6SJamie Gritton pr = cred->cr_prison; 34200fe74ae6SJamie Gritton sx_slock(&allprison_lock); 34210fe74ae6SJamie Gritton mtx_lock(&pr->pr_mtx); 34220fe74ae6SJamie Gritton prison_set_allow_locked(pr, flag, enable); 34230fe74ae6SJamie Gritton mtx_unlock(&pr->pr_mtx); 34240fe74ae6SJamie Gritton sx_sunlock(&allprison_lock); 34250fe74ae6SJamie Gritton } 34260fe74ae6SJamie Gritton 34270fe74ae6SJamie Gritton static void 34280fe74ae6SJamie Gritton prison_set_allow_locked(struct prison *pr, unsigned flag, int enable) 34290fe74ae6SJamie Gritton { 34300fe74ae6SJamie Gritton struct prison *cpr; 34310fe74ae6SJamie Gritton int descend; 34320fe74ae6SJamie Gritton 34330fe74ae6SJamie Gritton if (enable != 0) 34340fe74ae6SJamie Gritton pr->pr_allow |= flag; 34350fe74ae6SJamie Gritton else { 34360fe74ae6SJamie Gritton pr->pr_allow &= ~flag; 34370fe74ae6SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, cpr, descend) 34380fe74ae6SJamie Gritton cpr->pr_allow &= ~flag; 34390fe74ae6SJamie Gritton } 34400fe74ae6SJamie Gritton } 34410fe74ae6SJamie Gritton 34420fe74ae6SJamie Gritton /* 3443ca04ba64SJamie Gritton * Check if a jail supports the given address family. 3444ca04ba64SJamie Gritton * 3445ca04ba64SJamie Gritton * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT 3446ca04ba64SJamie Gritton * if not. 3447ca04ba64SJamie Gritton */ 3448ca04ba64SJamie Gritton int 3449ca04ba64SJamie Gritton prison_check_af(struct ucred *cred, int af) 3450ca04ba64SJamie Gritton { 34510304c731SJamie Gritton struct prison *pr; 3452ca04ba64SJamie Gritton int error; 3453ca04ba64SJamie Gritton 3454ca04ba64SJamie Gritton KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3455ca04ba64SJamie Gritton 34560304c731SJamie Gritton pr = cred->cr_prison; 3457499650a0SJamie Gritton #ifdef VIMAGE 34586bb79563SJamie Gritton /* Prisons with their own network stack are not limited. */ 3459de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 34606bb79563SJamie Gritton return (0); 3461499650a0SJamie Gritton #endif 34626bb79563SJamie Gritton 3463ca04ba64SJamie Gritton error = 0; 3464ca04ba64SJamie Gritton switch (af) 3465ca04ba64SJamie Gritton { 3466ca04ba64SJamie Gritton #ifdef INET 3467ca04ba64SJamie Gritton case AF_INET: 34680304c731SJamie Gritton if (pr->pr_flags & PR_IP4) 34690304c731SJamie Gritton { 34700304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 3471eb8dcdeaSGleb Smirnoff if ((pr->pr_flags & PR_IP4) && 3472eb8dcdeaSGleb Smirnoff pr->pr_addrs[PR_INET] == NULL) 3473ca04ba64SJamie Gritton error = EAFNOSUPPORT; 34740304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 34750304c731SJamie Gritton } 3476ca04ba64SJamie Gritton break; 3477ca04ba64SJamie Gritton #endif 3478ca04ba64SJamie Gritton #ifdef INET6 3479ca04ba64SJamie Gritton case AF_INET6: 34800304c731SJamie Gritton if (pr->pr_flags & PR_IP6) 34810304c731SJamie Gritton { 34820304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 3483eb8dcdeaSGleb Smirnoff if ((pr->pr_flags & PR_IP6) && 3484eb8dcdeaSGleb Smirnoff pr->pr_addrs[PR_INET6] == NULL) 3485ca04ba64SJamie Gritton error = EAFNOSUPPORT; 34860304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 34870304c731SJamie Gritton } 3488ca04ba64SJamie Gritton break; 3489ca04ba64SJamie Gritton #endif 3490ca04ba64SJamie Gritton case AF_LOCAL: 3491ca04ba64SJamie Gritton case AF_ROUTE: 349204f75b98SAlexander V. Chernikov case AF_NETLINK: 3493ca04ba64SJamie Gritton break; 3494ca04ba64SJamie Gritton default: 34950304c731SJamie Gritton if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF)) 3496ca04ba64SJamie Gritton error = EAFNOSUPPORT; 3497ca04ba64SJamie Gritton } 3498ca04ba64SJamie Gritton return (error); 3499ca04ba64SJamie Gritton } 3500ca04ba64SJamie Gritton 3501ca04ba64SJamie Gritton /* 3502413628a7SBjoern A. Zeeb * Check if given address belongs to the jail referenced by cred (wrapper to 3503413628a7SBjoern A. Zeeb * prison_check_ip[46]). 3504413628a7SBjoern A. Zeeb * 35050304c731SJamie Gritton * Returns 0 if jail doesn't restrict the address family or if address belongs 35060304c731SJamie Gritton * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if 35070304c731SJamie Gritton * the jail doesn't allow the address family. IPv4 Address passed in in NBO. 3508413628a7SBjoern A. Zeeb */ 3509413628a7SBjoern A. Zeeb int 3510c83dda36SAlexander V. Chernikov prison_if(struct ucred *cred, const struct sockaddr *sa) 351175c13541SPoul-Henning Kamp { 3512413628a7SBjoern A. Zeeb #ifdef INET 3513c83dda36SAlexander V. Chernikov const struct sockaddr_in *sai; 3514413628a7SBjoern A. Zeeb #endif 3515413628a7SBjoern A. Zeeb #ifdef INET6 3516c83dda36SAlexander V. Chernikov const struct sockaddr_in6 *sai6; 3517413628a7SBjoern A. Zeeb #endif 3518b89e82ddSJamie Gritton int error; 351975c13541SPoul-Henning Kamp 3520413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3521413628a7SBjoern A. Zeeb KASSERT(sa != NULL, ("%s: sa is NULL", __func__)); 3522413628a7SBjoern A. Zeeb 3523de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE 3524de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 3525de0bd6f7SBjoern A. Zeeb return (0); 3526de0bd6f7SBjoern A. Zeeb #endif 3527de0bd6f7SBjoern A. Zeeb 3528b89e82ddSJamie Gritton error = 0; 3529413628a7SBjoern A. Zeeb switch (sa->sa_family) 3530413628a7SBjoern A. Zeeb { 3531413628a7SBjoern A. Zeeb #ifdef INET 3532413628a7SBjoern A. Zeeb case AF_INET: 3533c83dda36SAlexander V. Chernikov sai = (const struct sockaddr_in *)sa; 3534b89e82ddSJamie Gritton error = prison_check_ip4(cred, &sai->sin_addr); 3535413628a7SBjoern A. Zeeb break; 3536413628a7SBjoern A. Zeeb #endif 3537413628a7SBjoern A. Zeeb #ifdef INET6 3538413628a7SBjoern A. Zeeb case AF_INET6: 3539c83dda36SAlexander V. Chernikov sai6 = (const struct sockaddr_in6 *)sa; 3540b89e82ddSJamie Gritton error = prison_check_ip6(cred, &sai6->sin6_addr); 3541413628a7SBjoern A. Zeeb break; 3542413628a7SBjoern A. Zeeb #endif 3543413628a7SBjoern A. Zeeb default: 35440304c731SJamie Gritton if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF)) 3545b89e82ddSJamie Gritton error = EAFNOSUPPORT; 3546413628a7SBjoern A. Zeeb } 3547b89e82ddSJamie Gritton return (error); 354875c13541SPoul-Henning Kamp } 354991421ba2SRobert Watson 355091421ba2SRobert Watson /* 355191421ba2SRobert Watson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH. 355291421ba2SRobert Watson */ 355391421ba2SRobert Watson int 35549ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2) 355591421ba2SRobert Watson { 355691421ba2SRobert Watson 35570304c731SJamie Gritton return ((cred1->cr_prison == cred2->cr_prison || 35580304c731SJamie Gritton prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH); 35590304c731SJamie Gritton } 356091421ba2SRobert Watson 35610304c731SJamie Gritton /* 3562bba7a2e8SRick Macklem * For mountd/nfsd to run within a prison, it must be: 3563bba7a2e8SRick Macklem * - A vnet prison. 3564bba7a2e8SRick Macklem * - PR_ALLOW_NFSD must be set on it. 3565bba7a2e8SRick Macklem * - The root directory (pr_root) of the prison must be 3566bba7a2e8SRick Macklem * a file system mount point, so the mountd can hang 3567bba7a2e8SRick Macklem * export information on it. 356899187c3aSRick Macklem * - The prison's enforce_statfs cannot be 0, so that 356999187c3aSRick Macklem * mountd(8) can do exports. 3570bba7a2e8SRick Macklem */ 3571bba7a2e8SRick Macklem bool 3572bba7a2e8SRick Macklem prison_check_nfsd(struct ucred *cred) 3573bba7a2e8SRick Macklem { 3574bba7a2e8SRick Macklem 3575bba7a2e8SRick Macklem if (jailed_without_vnet(cred)) 3576bba7a2e8SRick Macklem return (false); 3577bba7a2e8SRick Macklem if (!prison_allow(cred, PR_ALLOW_NFSD)) 3578bba7a2e8SRick Macklem return (false); 3579bba7a2e8SRick Macklem if ((cred->cr_prison->pr_root->v_vflag & VV_ROOT) == 0) 3580bba7a2e8SRick Macklem return (false); 358199187c3aSRick Macklem if (cred->cr_prison->pr_enforce_statfs == 0) 358299187c3aSRick Macklem return (false); 3583bba7a2e8SRick Macklem return (true); 3584bba7a2e8SRick Macklem } 3585bba7a2e8SRick Macklem 3586bba7a2e8SRick Macklem /* 35870b0ae2e4SMina Galić * Return true if p2 is a child of p1, otherwise false. 35880304c731SJamie Gritton */ 35890b0ae2e4SMina Galić bool 35900304c731SJamie Gritton prison_ischild(struct prison *pr1, struct prison *pr2) 35910304c731SJamie Gritton { 35920304c731SJamie Gritton 35930304c731SJamie Gritton for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent) 35940304c731SJamie Gritton if (pr1 == pr2) 35950b0ae2e4SMina Galić return (true); 35960b0ae2e4SMina Galić return (false); 359791421ba2SRobert Watson } 359891421ba2SRobert Watson 359991421ba2SRobert Watson /* 3600f7496dcaSJamie Gritton * Return true if the prison is currently alive. A prison is alive if it 3601f7496dcaSJamie Gritton * holds user references and it isn't being removed. 360276ad42abSJamie Gritton */ 360376ad42abSJamie Gritton bool 3604eb8dcdeaSGleb Smirnoff prison_isalive(const struct prison *pr) 360576ad42abSJamie Gritton { 360676ad42abSJamie Gritton 36071158508aSJamie Gritton if (__predict_false(pr->pr_state != PRISON_STATE_ALIVE)) 3608cc7b7306SJamie Gritton return (false); 360976ad42abSJamie Gritton return (true); 361076ad42abSJamie Gritton } 361176ad42abSJamie Gritton 361276ad42abSJamie Gritton /* 361376ad42abSJamie Gritton * Return true if the prison is currently valid. A prison is valid if it has 361476ad42abSJamie Gritton * been fully created, and is not being destroyed. Note that dying prisons 3615f7496dcaSJamie Gritton * are still considered valid. Invalid prisons won't be found under normal 3616f7496dcaSJamie Gritton * circumstances, as they're only put in that state by functions that have 3617f7496dcaSJamie Gritton * an exclusive hold on allprison_lock. 361876ad42abSJamie Gritton */ 361976ad42abSJamie Gritton bool 362076ad42abSJamie Gritton prison_isvalid(struct prison *pr) 362176ad42abSJamie Gritton { 362276ad42abSJamie Gritton 36231158508aSJamie Gritton if (__predict_false(pr->pr_state == PRISON_STATE_INVALID)) 36241158508aSJamie Gritton return (false); 36256754ae25SJamie Gritton if (__predict_false(refcount_load(&pr->pr_ref) == 0)) 362676ad42abSJamie Gritton return (false); 362776ad42abSJamie Gritton return (true); 362876ad42abSJamie Gritton } 362976ad42abSJamie Gritton 363076ad42abSJamie Gritton /* 36310b0ae2e4SMina Galić * Return true if the passed credential is in a jail and that jail does not 36320b0ae2e4SMina Galić * have its own virtual network stack, otherwise false. 3633de0bd6f7SBjoern A. Zeeb */ 36340b0ae2e4SMina Galić bool 3635de0bd6f7SBjoern A. Zeeb jailed_without_vnet(struct ucred *cred) 3636de0bd6f7SBjoern A. Zeeb { 3637de0bd6f7SBjoern A. Zeeb 3638de0bd6f7SBjoern A. Zeeb if (!jailed(cred)) 36390b0ae2e4SMina Galić return (false); 3640de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE 3641de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 36420b0ae2e4SMina Galić return (false); 3643de0bd6f7SBjoern A. Zeeb #endif 3644de0bd6f7SBjoern A. Zeeb 36450b0ae2e4SMina Galić return (true); 3646de0bd6f7SBjoern A. Zeeb } 3647de0bd6f7SBjoern A. Zeeb 3648de0bd6f7SBjoern A. Zeeb /* 36497455b100SJamie Gritton * Return the correct hostname (domainname, et al) for the passed credential. 36509484d0c0SRobert Drehmel */ 3651ad1ff099SRobert Drehmel void 36529ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size) 36539484d0c0SRobert Drehmel { 365476ca6f88SJamie Gritton struct prison *pr; 36559484d0c0SRobert Drehmel 36567455b100SJamie Gritton /* 36577455b100SJamie Gritton * A NULL credential can be used to shortcut to the physical 36587455b100SJamie Gritton * system's hostname. 36597455b100SJamie Gritton */ 366076ca6f88SJamie Gritton pr = (cred != NULL) ? cred->cr_prison : &prison0; 366176ca6f88SJamie Gritton mtx_lock(&pr->pr_mtx); 3662c1f19219SJamie Gritton strlcpy(buf, pr->pr_hostname, size); 366376ca6f88SJamie Gritton mtx_unlock(&pr->pr_mtx); 36649484d0c0SRobert Drehmel } 3665fd7a8150SMike Barcroft 36667455b100SJamie Gritton void 36677455b100SJamie Gritton getcreddomainname(struct ucred *cred, char *buf, size_t size) 36687455b100SJamie Gritton { 36697455b100SJamie Gritton 36707455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 3671c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_domainname, size); 36727455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 36737455b100SJamie Gritton } 36747455b100SJamie Gritton 36757455b100SJamie Gritton void 36767455b100SJamie Gritton getcredhostuuid(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_hostuuid, size); 36817455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 36827455b100SJamie Gritton } 36837455b100SJamie Gritton 36847455b100SJamie Gritton void 36857455b100SJamie Gritton getcredhostid(struct ucred *cred, unsigned long *hostid) 36867455b100SJamie Gritton { 36877455b100SJamie Gritton 36887455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 36897455b100SJamie Gritton *hostid = cred->cr_prison->pr_hostid; 36907455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 36917455b100SJamie Gritton } 36927455b100SJamie Gritton 36933f8bc99cSKristof Provost void 36943f8bc99cSKristof Provost getjailname(struct ucred *cred, char *name, size_t len) 36953f8bc99cSKristof Provost { 36963f8bc99cSKristof Provost 36973f8bc99cSKristof Provost mtx_lock(&cred->cr_prison->pr_mtx); 36983f8bc99cSKristof Provost strlcpy(name, cred->cr_prison->pr_name, len); 36993f8bc99cSKristof Provost mtx_unlock(&cred->cr_prison->pr_mtx); 37003f8bc99cSKristof Provost } 37013f8bc99cSKristof Provost 3702eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE 3703eb79e1c7SBjoern A. Zeeb /* 3704eb79e1c7SBjoern A. Zeeb * Determine whether the prison represented by cred owns 3705eb79e1c7SBjoern A. Zeeb * its vnet rather than having it inherited. 3706eb79e1c7SBjoern A. Zeeb * 37070b0ae2e4SMina Galić * Returns true in case the prison owns the vnet, false otherwise. 3708eb79e1c7SBjoern A. Zeeb */ 37090b0ae2e4SMina Galić bool 3710eb79e1c7SBjoern A. Zeeb prison_owns_vnet(struct ucred *cred) 3711eb79e1c7SBjoern A. Zeeb { 3712eb79e1c7SBjoern A. Zeeb 3713eb79e1c7SBjoern A. Zeeb /* 3714eb79e1c7SBjoern A. Zeeb * vnets cannot be added/removed after jail creation, 3715eb79e1c7SBjoern A. Zeeb * so no need to lock here. 3716eb79e1c7SBjoern A. Zeeb */ 37170b0ae2e4SMina Galić return ((cred->cr_prison->pr_flags & PR_VNET) != 0); 3718eb79e1c7SBjoern A. Zeeb } 3719eb79e1c7SBjoern A. Zeeb #endif 3720eb79e1c7SBjoern A. Zeeb 3721f08df373SRobert Watson /* 3722820a0de9SPawel Jakub Dawidek * Determine whether the subject represented by cred can "see" 3723820a0de9SPawel Jakub Dawidek * status of a mount point. 3724820a0de9SPawel Jakub Dawidek * Returns: 0 for permitted, ENOENT otherwise. 3725820a0de9SPawel Jakub Dawidek * XXX: This function should be called cr_canseemount() and should be 3726820a0de9SPawel Jakub Dawidek * placed in kern_prot.c. 3727f08df373SRobert Watson */ 3728f08df373SRobert Watson int 3729820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp) 3730f08df373SRobert Watson { 3731820a0de9SPawel Jakub Dawidek struct prison *pr; 3732820a0de9SPawel Jakub Dawidek struct statfs *sp; 3733820a0de9SPawel Jakub Dawidek size_t len; 3734f08df373SRobert Watson 3735820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 37360304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 37370304c731SJamie Gritton return (0); 3738820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) 3739820a0de9SPawel Jakub Dawidek return (0); 37400304c731SJamie Gritton if (pr->pr_enforce_statfs == 2) 3741820a0de9SPawel Jakub Dawidek return (ENOENT); 3742820a0de9SPawel Jakub Dawidek /* 3743820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 3744820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 3745820a0de9SPawel Jakub Dawidek * This is ugly check, but this is the only situation when jail's 3746820a0de9SPawel Jakub Dawidek * directory ends with '/'. 3747820a0de9SPawel Jakub Dawidek */ 3748820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 3749820a0de9SPawel Jakub Dawidek return (0); 3750820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 3751820a0de9SPawel Jakub Dawidek sp = &mp->mnt_stat; 3752820a0de9SPawel Jakub Dawidek if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0) 3753820a0de9SPawel Jakub Dawidek return (ENOENT); 3754820a0de9SPawel Jakub Dawidek /* 3755820a0de9SPawel Jakub Dawidek * Be sure that we don't have situation where jail's root directory 3756820a0de9SPawel Jakub Dawidek * is "/some/path" and mount point is "/some/pathpath". 3757820a0de9SPawel Jakub Dawidek */ 3758820a0de9SPawel Jakub Dawidek if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/') 3759820a0de9SPawel Jakub Dawidek return (ENOENT); 3760f08df373SRobert Watson return (0); 3761f08df373SRobert Watson } 3762820a0de9SPawel Jakub Dawidek 3763820a0de9SPawel Jakub Dawidek void 3764820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp) 3765820a0de9SPawel Jakub Dawidek { 3766820a0de9SPawel Jakub Dawidek char jpath[MAXPATHLEN]; 3767820a0de9SPawel Jakub Dawidek struct prison *pr; 3768820a0de9SPawel Jakub Dawidek size_t len; 3769820a0de9SPawel Jakub Dawidek 3770820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 37710304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 37720304c731SJamie Gritton return; 3773820a0de9SPawel Jakub Dawidek if (prison_canseemount(cred, mp) != 0) { 3774820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3775820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, "[restricted]", 3776820a0de9SPawel Jakub Dawidek sizeof(sp->f_mntonname)); 3777820a0de9SPawel Jakub Dawidek return; 3778820a0de9SPawel Jakub Dawidek } 3779820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) { 3780820a0de9SPawel Jakub Dawidek /* 3781820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3782820a0de9SPawel Jakub Dawidek * the valid path left there. 3783820a0de9SPawel Jakub Dawidek */ 3784820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3785820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3786820a0de9SPawel Jakub Dawidek return; 3787820a0de9SPawel Jakub Dawidek } 3788820a0de9SPawel Jakub Dawidek /* 3789820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 3790820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 3791820a0de9SPawel Jakub Dawidek */ 3792820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 3793820a0de9SPawel Jakub Dawidek return; 3794820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 3795820a0de9SPawel Jakub Dawidek strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath)); 3796820a0de9SPawel Jakub Dawidek /* 3797820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3798820a0de9SPawel Jakub Dawidek * the valid path left there. 3799820a0de9SPawel Jakub Dawidek */ 3800820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3801820a0de9SPawel Jakub Dawidek if (*jpath == '\0') { 3802820a0de9SPawel Jakub Dawidek /* Should never happen. */ 3803820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3804820a0de9SPawel Jakub Dawidek } else { 3805820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname)); 3806820a0de9SPawel Jakub Dawidek } 3807f08df373SRobert Watson } 3808f08df373SRobert Watson 3809800c9408SRobert Watson /* 3810800c9408SRobert Watson * Check with permission for a specific privilege is granted within jail. We 3811800c9408SRobert Watson * have a specific list of accepted privileges; the rest are denied. 3812800c9408SRobert Watson */ 3813800c9408SRobert Watson int 3814800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv) 3815800c9408SRobert Watson { 38160fe74ae6SJamie Gritton struct prison *pr; 38170fe74ae6SJamie Gritton int error; 3818800c9408SRobert Watson 38197b2ff0dcSMateusz Guzik /* 38207b2ff0dcSMateusz Guzik * Some policies have custom handlers. This routine should not be 38217b2ff0dcSMateusz Guzik * called for them. See priv_check_cred(). 38227b2ff0dcSMateusz Guzik */ 38237b2ff0dcSMateusz Guzik switch (priv) { 3824a459a6cfSMateusz Guzik case PRIV_VFS_LOOKUP: 38257b2ff0dcSMateusz Guzik case PRIV_VFS_GENERATION: 38267b2ff0dcSMateusz Guzik KASSERT(0, ("prison_priv_check instead of a custom handler " 38277b2ff0dcSMateusz Guzik "called for %d\n", priv)); 38287b2ff0dcSMateusz Guzik } 38297b2ff0dcSMateusz Guzik 3830800c9408SRobert Watson if (!jailed(cred)) 3831800c9408SRobert Watson return (0); 3832800c9408SRobert Watson 38336bb79563SJamie Gritton #ifdef VIMAGE 38346bb79563SJamie Gritton /* 38356bb79563SJamie Gritton * Privileges specific to prisons with a virtual network stack. 38366bb79563SJamie Gritton * There might be a duplicate entry here in case the privilege 38376bb79563SJamie Gritton * is only granted conditionally in the legacy jail case. 38386bb79563SJamie Gritton */ 38396bb79563SJamie Gritton switch (priv) { 38406bb79563SJamie Gritton /* 38416bb79563SJamie Gritton * NFS-specific privileges. 38426bb79563SJamie Gritton */ 38436bb79563SJamie Gritton case PRIV_NFS_DAEMON: 3844bba7a2e8SRick Macklem case PRIV_VFS_GETFH: 3845bba7a2e8SRick Macklem case PRIV_VFS_MOUNT_EXPORTED: 3846bba7a2e8SRick Macklem if (!prison_check_nfsd(cred)) 3847bba7a2e8SRick Macklem return (EPERM); 3848bba7a2e8SRick Macklem #ifdef notyet 38496bb79563SJamie Gritton case PRIV_NFS_LOCKD: 38506bb79563SJamie Gritton #endif 38516bb79563SJamie Gritton /* 38526bb79563SJamie Gritton * Network stack privileges. 38536bb79563SJamie Gritton */ 38546bb79563SJamie Gritton case PRIV_NET_BRIDGE: 38556bb79563SJamie Gritton case PRIV_NET_GRE: 38566bb79563SJamie Gritton case PRIV_NET_BPF: 38576bb79563SJamie Gritton case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */ 38586bb79563SJamie Gritton case PRIV_NET_ROUTE: 38596bb79563SJamie Gritton case PRIV_NET_TAP: 38606bb79563SJamie Gritton case PRIV_NET_SETIFMTU: 38616bb79563SJamie Gritton case PRIV_NET_SETIFFLAGS: 38626bb79563SJamie Gritton case PRIV_NET_SETIFCAP: 3863215940b3SXin LI case PRIV_NET_SETIFDESCR: 38646bb79563SJamie Gritton case PRIV_NET_SETIFNAME : 38656bb79563SJamie Gritton case PRIV_NET_SETIFMETRIC: 38666bb79563SJamie Gritton case PRIV_NET_SETIFPHYS: 38676bb79563SJamie Gritton case PRIV_NET_SETIFMAC: 3868389474c1SKonstantin Belousov case PRIV_NET_SETLANPCP: 38696bb79563SJamie Gritton case PRIV_NET_ADDMULTI: 38706bb79563SJamie Gritton case PRIV_NET_DELMULTI: 38716bb79563SJamie Gritton case PRIV_NET_HWIOCTL: 38726bb79563SJamie Gritton case PRIV_NET_SETLLADDR: 38736bb79563SJamie Gritton case PRIV_NET_ADDIFGROUP: 38746bb79563SJamie Gritton case PRIV_NET_DELIFGROUP: 38756bb79563SJamie Gritton case PRIV_NET_IFCREATE: 38766bb79563SJamie Gritton case PRIV_NET_IFDESTROY: 38776bb79563SJamie Gritton case PRIV_NET_ADDIFADDR: 38786bb79563SJamie Gritton case PRIV_NET_DELIFADDR: 38796bb79563SJamie Gritton case PRIV_NET_LAGG: 38806bb79563SJamie Gritton case PRIV_NET_GIF: 38816bb79563SJamie Gritton case PRIV_NET_SETIFVNET: 388235fd7bc0SBjoern A. Zeeb case PRIV_NET_SETIFFIB: 3883ab91feabSKristof Provost case PRIV_NET_OVPN: 388443f8c763SZhenlei Huang case PRIV_NET_ME: 3885744bfb21SJohn Baldwin case PRIV_NET_WG: 38866bb79563SJamie Gritton 38876bb79563SJamie Gritton /* 38886bb79563SJamie Gritton * 802.11-related privileges. 38896bb79563SJamie Gritton */ 3890f7d38a13SAdrian Chadd case PRIV_NET80211_VAP_GETKEY: 3891f7d38a13SAdrian Chadd case PRIV_NET80211_VAP_MANAGE: 38926bb79563SJamie Gritton 38936bb79563SJamie Gritton #ifdef notyet 38946bb79563SJamie Gritton /* 38956bb79563SJamie Gritton * ATM privileges. 38966bb79563SJamie Gritton */ 38976bb79563SJamie Gritton case PRIV_NETATM_CFG: 38986bb79563SJamie Gritton case PRIV_NETATM_ADD: 38996bb79563SJamie Gritton case PRIV_NETATM_DEL: 39006bb79563SJamie Gritton case PRIV_NETATM_SET: 39016bb79563SJamie Gritton 39026bb79563SJamie Gritton /* 39036bb79563SJamie Gritton * Bluetooth privileges. 39046bb79563SJamie Gritton */ 39056bb79563SJamie Gritton case PRIV_NETBLUETOOTH_RAW: 39066bb79563SJamie Gritton #endif 39076bb79563SJamie Gritton 39086bb79563SJamie Gritton /* 39096bb79563SJamie Gritton * Netgraph and netgraph module privileges. 39106bb79563SJamie Gritton */ 39116bb79563SJamie Gritton case PRIV_NETGRAPH_CONTROL: 39126bb79563SJamie Gritton #ifdef notyet 39136bb79563SJamie Gritton case PRIV_NETGRAPH_TTY: 39146bb79563SJamie Gritton #endif 39156bb79563SJamie Gritton 39166bb79563SJamie Gritton /* 39176bb79563SJamie Gritton * IPv4 and IPv6 privileges. 39186bb79563SJamie Gritton */ 39196bb79563SJamie Gritton case PRIV_NETINET_IPFW: 39206bb79563SJamie Gritton case PRIV_NETINET_DIVERT: 39216bb79563SJamie Gritton case PRIV_NETINET_PF: 39226bb79563SJamie Gritton case PRIV_NETINET_DUMMYNET: 39236bb79563SJamie Gritton case PRIV_NETINET_CARP: 39246bb79563SJamie Gritton case PRIV_NETINET_MROUTE: 39256bb79563SJamie Gritton case PRIV_NETINET_RAW: 39266bb79563SJamie Gritton case PRIV_NETINET_ADDRCTRL6: 39276bb79563SJamie Gritton case PRIV_NETINET_ND6: 39286bb79563SJamie Gritton case PRIV_NETINET_SCOPE6: 39296bb79563SJamie Gritton case PRIV_NETINET_ALIFETIME6: 39306bb79563SJamie Gritton case PRIV_NETINET_IPSEC: 39316bb79563SJamie Gritton case PRIV_NETINET_BINDANY: 39326bb79563SJamie Gritton 39336bb79563SJamie Gritton #ifdef notyet 39346bb79563SJamie Gritton /* 39356bb79563SJamie Gritton * NCP privileges. 39366bb79563SJamie Gritton */ 39376bb79563SJamie Gritton case PRIV_NETNCP: 39386bb79563SJamie Gritton 39396bb79563SJamie Gritton /* 39406bb79563SJamie Gritton * SMB privileges. 39416bb79563SJamie Gritton */ 39426bb79563SJamie Gritton case PRIV_NETSMB: 39436bb79563SJamie Gritton #endif 39446bb79563SJamie Gritton 39456bb79563SJamie Gritton /* 39466bb79563SJamie Gritton * No default: or deny here. 39476bb79563SJamie Gritton * In case of no permit fall through to next switch(). 39486bb79563SJamie Gritton */ 39496bb79563SJamie Gritton if (cred->cr_prison->pr_flags & PR_VNET) 39506bb79563SJamie Gritton return (0); 39516bb79563SJamie Gritton } 39526bb79563SJamie Gritton #endif /* VIMAGE */ 39536bb79563SJamie Gritton 3954800c9408SRobert Watson switch (priv) { 3955800c9408SRobert Watson /* 3956800c9408SRobert Watson * Allow ktrace privileges for root in jail. 3957800c9408SRobert Watson */ 3958800c9408SRobert Watson case PRIV_KTRACE: 3959800c9408SRobert Watson 3960c3c1b5e6SRobert Watson #if 0 3961800c9408SRobert Watson /* 3962800c9408SRobert Watson * Allow jailed processes to configure audit identity and 3963800c9408SRobert Watson * submit audit records (login, etc). In the future we may 3964800c9408SRobert Watson * want to further refine the relationship between audit and 3965800c9408SRobert Watson * jail. 3966800c9408SRobert Watson */ 3967800c9408SRobert Watson case PRIV_AUDIT_GETAUDIT: 3968800c9408SRobert Watson case PRIV_AUDIT_SETAUDIT: 3969800c9408SRobert Watson case PRIV_AUDIT_SUBMIT: 3970c3c1b5e6SRobert Watson #endif 3971800c9408SRobert Watson 3972800c9408SRobert Watson /* 3973800c9408SRobert Watson * Allow jailed processes to manipulate process UNIX 3974800c9408SRobert Watson * credentials in any way they see fit. 3975800c9408SRobert Watson */ 3976ddb3eb4eSOlivier Certner case PRIV_CRED_SETCRED: 3977800c9408SRobert Watson case PRIV_CRED_SETUID: 3978800c9408SRobert Watson case PRIV_CRED_SETEUID: 3979800c9408SRobert Watson case PRIV_CRED_SETGID: 3980800c9408SRobert Watson case PRIV_CRED_SETEGID: 3981800c9408SRobert Watson case PRIV_CRED_SETGROUPS: 3982800c9408SRobert Watson case PRIV_CRED_SETREUID: 3983800c9408SRobert Watson case PRIV_CRED_SETREGID: 3984800c9408SRobert Watson case PRIV_CRED_SETRESUID: 3985800c9408SRobert Watson case PRIV_CRED_SETRESGID: 3986800c9408SRobert Watson 3987800c9408SRobert Watson /* 3988800c9408SRobert Watson * Jail implements visibility constraints already, so allow 3989800c9408SRobert Watson * jailed root to override uid/gid-based constraints. 3990800c9408SRobert Watson */ 3991800c9408SRobert Watson case PRIV_SEEOTHERGIDS: 3992800c9408SRobert Watson case PRIV_SEEOTHERUIDS: 39937974ca1cSOlivier Certner case PRIV_SEEJAILPROC: 3994800c9408SRobert Watson 3995800c9408SRobert Watson /* 3996800c9408SRobert Watson * Jail implements inter-process debugging limits already, so 3997800c9408SRobert Watson * allow jailed root various debugging privileges. 3998800c9408SRobert Watson */ 3999800c9408SRobert Watson case PRIV_DEBUG_DIFFCRED: 4000800c9408SRobert Watson case PRIV_DEBUG_SUGID: 4001800c9408SRobert Watson case PRIV_DEBUG_UNPRIV: 4002800c9408SRobert Watson 4003800c9408SRobert Watson /* 4004800c9408SRobert Watson * Allow jail to set various resource limits and login 4005800c9408SRobert Watson * properties, and for now, exceed process resource limits. 4006800c9408SRobert Watson */ 4007800c9408SRobert Watson case PRIV_PROC_LIMIT: 4008800c9408SRobert Watson case PRIV_PROC_SETLOGIN: 4009800c9408SRobert Watson case PRIV_PROC_SETRLIMIT: 4010800c9408SRobert Watson 4011800c9408SRobert Watson /* 4012800c9408SRobert Watson * System V and POSIX IPC privileges are granted in jail. 4013800c9408SRobert Watson */ 4014800c9408SRobert Watson case PRIV_IPC_READ: 4015800c9408SRobert Watson case PRIV_IPC_WRITE: 4016800c9408SRobert Watson case PRIV_IPC_ADMIN: 4017800c9408SRobert Watson case PRIV_IPC_MSGSIZE: 4018800c9408SRobert Watson case PRIV_MQ_ADMIN: 4019800c9408SRobert Watson 4020800c9408SRobert Watson /* 40210304c731SJamie Gritton * Jail operations within a jail work on child jails. 40220304c731SJamie Gritton */ 40230304c731SJamie Gritton case PRIV_JAIL_ATTACH: 40240304c731SJamie Gritton case PRIV_JAIL_SET: 40250304c731SJamie Gritton case PRIV_JAIL_REMOVE: 40260304c731SJamie Gritton 40270304c731SJamie Gritton /* 4028800c9408SRobert Watson * Jail implements its own inter-process limits, so allow 4029800c9408SRobert Watson * root processes in jail to change scheduling on other 4030800c9408SRobert Watson * processes in the same jail. Likewise for signalling. 4031800c9408SRobert Watson */ 4032800c9408SRobert Watson case PRIV_SCHED_DIFFCRED: 4033413628a7SBjoern A. Zeeb case PRIV_SCHED_CPUSET: 4034800c9408SRobert Watson case PRIV_SIGNAL_DIFFCRED: 4035800c9408SRobert Watson case PRIV_SIGNAL_SUGID: 4036800c9408SRobert Watson 4037800c9408SRobert Watson /* 4038800c9408SRobert Watson * Allow jailed processes to write to sysctls marked as jail 4039800c9408SRobert Watson * writable. 4040800c9408SRobert Watson */ 4041800c9408SRobert Watson case PRIV_SYSCTL_WRITEJAIL: 4042800c9408SRobert Watson 4043800c9408SRobert Watson /* 4044800c9408SRobert Watson * Allow root in jail to manage a variety of quota 4045e82d0201SRobert Watson * properties. These should likely be conditional on a 4046e82d0201SRobert Watson * configuration option. 4047800c9408SRobert Watson */ 404895b091d2SRobert Watson case PRIV_VFS_GETQUOTA: 404995b091d2SRobert Watson case PRIV_VFS_SETQUOTA: 4050800c9408SRobert Watson 4051800c9408SRobert Watson /* 4052800c9408SRobert Watson * Since Jail relies on chroot() to implement file system 4053800c9408SRobert Watson * protections, grant many VFS privileges to root in jail. 4054800c9408SRobert Watson * Be careful to exclude mount-related and NFS-related 4055800c9408SRobert Watson * privileges. 4056800c9408SRobert Watson */ 4057800c9408SRobert Watson case PRIV_VFS_READ: 4058800c9408SRobert Watson case PRIV_VFS_WRITE: 4059800c9408SRobert Watson case PRIV_VFS_ADMIN: 4060800c9408SRobert Watson case PRIV_VFS_EXEC: 4061800c9408SRobert Watson case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */ 4062800c9408SRobert Watson case PRIV_VFS_CHFLAGS_DEV: 4063800c9408SRobert Watson case PRIV_VFS_CHOWN: 4064800c9408SRobert Watson case PRIV_VFS_CHROOT: 4065bb531912SPawel Jakub Dawidek case PRIV_VFS_RETAINSUGID: 4066800c9408SRobert Watson case PRIV_VFS_FCHROOT: 4067800c9408SRobert Watson case PRIV_VFS_LINK: 4068800c9408SRobert Watson case PRIV_VFS_SETGID: 4069e41966dcSRobert Watson case PRIV_VFS_STAT: 4070800c9408SRobert Watson case PRIV_VFS_STICKYFILE: 4071bb56d716SJamie Gritton 4072bb56d716SJamie Gritton /* 4073bb56d716SJamie Gritton * As in the non-jail case, non-root users are expected to be 407470de1003SGordon Bergling * able to read kernel/physical memory (provided /dev/[k]mem 4075bb56d716SJamie Gritton * exists in the jail and they have permission to access it). 4076bb56d716SJamie Gritton */ 4077bb56d716SJamie Gritton case PRIV_KMEM_READ: 4078800c9408SRobert Watson return (0); 4079800c9408SRobert Watson 4080800c9408SRobert Watson /* 4081800c9408SRobert Watson * Depending on the global setting, allow privilege of 4082800c9408SRobert Watson * setting system flags. 4083800c9408SRobert Watson */ 4084800c9408SRobert Watson case PRIV_VFS_SYSFLAGS: 40850304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS) 4086800c9408SRobert Watson return (0); 4087800c9408SRobert Watson else 4088800c9408SRobert Watson return (EPERM); 4089800c9408SRobert Watson 4090800c9408SRobert Watson /* 4091f3a8d2f9SPawel Jakub Dawidek * Depending on the global setting, allow privilege of 4092f3a8d2f9SPawel Jakub Dawidek * mounting/unmounting file systems. 4093f3a8d2f9SPawel Jakub Dawidek */ 4094f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT: 4095f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_UNMOUNT: 4096f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT_NONUSER: 409724b0502eSPawel Jakub Dawidek case PRIV_VFS_MOUNT_OWNER: 40980fe74ae6SJamie Gritton pr = cred->cr_prison; 40990fe74ae6SJamie Gritton prison_lock(pr); 41000fe74ae6SJamie Gritton if (pr->pr_allow & PR_ALLOW_MOUNT && pr->pr_enforce_statfs < 2) 41010fe74ae6SJamie Gritton error = 0; 4102f3a8d2f9SPawel Jakub Dawidek else 41030fe74ae6SJamie Gritton error = EPERM; 41040fe74ae6SJamie Gritton prison_unlock(pr); 41050fe74ae6SJamie Gritton return (error); 4106f3a8d2f9SPawel Jakub Dawidek 4107f3a8d2f9SPawel Jakub Dawidek /* 410863619b6dSKyle Evans * Jails should hold no disposition on the PRIV_VFS_READ_DIR 410963619b6dSKyle Evans * policy. priv_check_cred will not specifically allow it, and 411063619b6dSKyle Evans * we may want a MAC policy to allow it. 411163619b6dSKyle Evans */ 411263619b6dSKyle Evans case PRIV_VFS_READ_DIR: 411363619b6dSKyle Evans return (0); 411463619b6dSKyle Evans 411563619b6dSKyle Evans /* 4116cb48780dSShawn Webb * Conditionally allow privileged process in the jail to 4117cb48780dSShawn Webb * manipulate filesystem extended attributes in the system 4118cb48780dSShawn Webb * namespace. 4119cb48780dSShawn Webb */ 4120cb48780dSShawn Webb case PRIV_VFS_EXTATTR_SYSTEM: 4121cb48780dSShawn Webb if ((cred->cr_prison->pr_allow & PR_ALLOW_EXTATTR) != 0) 4122cb48780dSShawn Webb return (0); 4123cb48780dSShawn Webb else 4124cb48780dSShawn Webb return (EPERM); 4125cb48780dSShawn Webb 4126cb48780dSShawn Webb /* 4127ccd6ac9fSAntoine Brodin * Conditionnaly allow locking (unlocking) physical pages 4128ccd6ac9fSAntoine Brodin * in memory. 4129ccd6ac9fSAntoine Brodin */ 4130ccd6ac9fSAntoine Brodin case PRIV_VM_MLOCK: 4131ccd6ac9fSAntoine Brodin case PRIV_VM_MUNLOCK: 4132ccd6ac9fSAntoine Brodin if (cred->cr_prison->pr_allow & PR_ALLOW_MLOCK) 4133ccd6ac9fSAntoine Brodin return (0); 4134ccd6ac9fSAntoine Brodin else 4135ccd6ac9fSAntoine Brodin return (EPERM); 4136ccd6ac9fSAntoine Brodin 4137ccd6ac9fSAntoine Brodin /* 4138e28f9b7dSAllan Jude * Conditionally allow jailed root to bind reserved ports. 4139800c9408SRobert Watson */ 4140800c9408SRobert Watson case PRIV_NETINET_RESERVEDPORT: 4141e28f9b7dSAllan Jude if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS) 4142e28f9b7dSAllan Jude return (0); 4143e28f9b7dSAllan Jude else 4144e28f9b7dSAllan Jude return (EPERM); 4145e28f9b7dSAllan Jude 4146e28f9b7dSAllan Jude /* 4147e28f9b7dSAllan Jude * Allow jailed root to reuse in-use ports. 4148e28f9b7dSAllan Jude */ 41494b084056SRobert Watson case PRIV_NETINET_REUSEPORT: 4150800c9408SRobert Watson return (0); 4151800c9408SRobert Watson 4152800c9408SRobert Watson /* 4153e3043798SPedro F. Giffuni * Allow jailed root to set certain IPv4/6 (option) headers. 415479ba3952SBjoern A. Zeeb */ 415579ba3952SBjoern A. Zeeb case PRIV_NETINET_SETHDROPTS: 415679ba3952SBjoern A. Zeeb return (0); 415779ba3952SBjoern A. Zeeb 415879ba3952SBjoern A. Zeeb /* 4159800c9408SRobert Watson * Conditionally allow creating raw sockets in jail. 4160800c9408SRobert Watson */ 4161800c9408SRobert Watson case PRIV_NETINET_RAW: 41620304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS) 4163800c9408SRobert Watson return (0); 4164800c9408SRobert Watson else 4165800c9408SRobert Watson return (EPERM); 4166800c9408SRobert Watson 4167800c9408SRobert Watson /* 4168800c9408SRobert Watson * Since jail implements its own visibility limits on netstat 4169800c9408SRobert Watson * sysctls, allow getcred. This allows identd to work in 4170800c9408SRobert Watson * jail. 4171800c9408SRobert Watson */ 4172800c9408SRobert Watson case PRIV_NETINET_GETCRED: 4173800c9408SRobert Watson return (0); 4174800c9408SRobert Watson 41752bfc50bcSEdward Tomasz Napierala /* 41762bfc50bcSEdward Tomasz Napierala * Allow jailed root to set loginclass. 41772bfc50bcSEdward Tomasz Napierala */ 41782bfc50bcSEdward Tomasz Napierala case PRIV_PROC_SETLOGINCLASS: 41792bfc50bcSEdward Tomasz Napierala return (0); 41802bfc50bcSEdward Tomasz Napierala 4181b19d66fdSJamie Gritton /* 41824520f617SJamie Gritton * Do not allow a process inside a jail to read the kernel 4183b19d66fdSJamie Gritton * message buffer unless explicitly permitted. 4184b19d66fdSJamie Gritton */ 4185b19d66fdSJamie Gritton case PRIV_MSGBUF: 4186b19d66fdSJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_READ_MSGBUF) 4187b19d66fdSJamie Gritton return (0); 4188b19d66fdSJamie Gritton return (EPERM); 4189b19d66fdSJamie Gritton 4190d3bb35d4SMariusz Zaborski /* 4191d3bb35d4SMariusz Zaborski * Conditionally allow privileged process in the jail adjust 4192d3bb35d4SMariusz Zaborski * machine time. 4193d3bb35d4SMariusz Zaborski */ 4194d3bb35d4SMariusz Zaborski case PRIV_ADJTIME: 4195d3bb35d4SMariusz Zaborski case PRIV_NTP_ADJTIME: 4196d3bb35d4SMariusz Zaborski if (cred->cr_prison->pr_allow & 4197d3bb35d4SMariusz Zaborski (PR_ALLOW_ADJTIME | PR_ALLOW_SETTIME)) { 4198d3bb35d4SMariusz Zaborski return (0); 4199d3bb35d4SMariusz Zaborski } 4200d3bb35d4SMariusz Zaborski return (EPERM); 4201d3bb35d4SMariusz Zaborski 4202d3bb35d4SMariusz Zaborski /* 4203d3bb35d4SMariusz Zaborski * Conditionally allow privileged process in the jail set 4204d3bb35d4SMariusz Zaborski * machine time. 4205d3bb35d4SMariusz Zaborski */ 4206d3bb35d4SMariusz Zaborski case PRIV_CLOCK_SETTIME: 4207d3bb35d4SMariusz Zaborski if (cred->cr_prison->pr_allow & PR_ALLOW_SETTIME) 4208d3bb35d4SMariusz Zaborski return (0); 4209d3bb35d4SMariusz Zaborski else 4210d3bb35d4SMariusz Zaborski return (EPERM); 4211d3bb35d4SMariusz Zaborski 4212800c9408SRobert Watson default: 4213800c9408SRobert Watson /* 4214800c9408SRobert Watson * In all remaining cases, deny the privilege request. This 4215800c9408SRobert Watson * includes almost all network privileges, many system 4216800c9408SRobert Watson * configuration privileges. 4217800c9408SRobert Watson */ 4218800c9408SRobert Watson return (EPERM); 4219800c9408SRobert Watson } 4220800c9408SRobert Watson } 4221800c9408SRobert Watson 42220304c731SJamie Gritton /* 42230304c731SJamie Gritton * Return the part of pr2's name that is relative to pr1, or the whole name 42240304c731SJamie Gritton * if it does not directly follow. 42250304c731SJamie Gritton */ 42260304c731SJamie Gritton 42270304c731SJamie Gritton char * 42280304c731SJamie Gritton prison_name(struct prison *pr1, struct prison *pr2) 42290304c731SJamie Gritton { 42300304c731SJamie Gritton char *name; 42310304c731SJamie Gritton 42320304c731SJamie Gritton /* Jails see themselves as "0" (if they see themselves at all). */ 42330304c731SJamie Gritton if (pr1 == pr2) 42340304c731SJamie Gritton return "0"; 42350304c731SJamie Gritton name = pr2->pr_name; 42360304c731SJamie Gritton if (prison_ischild(pr1, pr2)) { 42370304c731SJamie Gritton /* 42380304c731SJamie Gritton * pr1 isn't locked (and allprison_lock may not be either) 42390304c731SJamie Gritton * so its length can't be counted on. But the number of dots 42400304c731SJamie Gritton * can be counted on - and counted. 42410304c731SJamie Gritton */ 42420304c731SJamie Gritton for (; pr1 != &prison0; pr1 = pr1->pr_parent) 42430304c731SJamie Gritton name = strchr(name, '.') + 1; 42440304c731SJamie Gritton } 42450304c731SJamie Gritton return (name); 42460304c731SJamie Gritton } 42470304c731SJamie Gritton 42480304c731SJamie Gritton /* 42490304c731SJamie Gritton * Return the part of pr2's path that is relative to pr1, or the whole path 42500304c731SJamie Gritton * if it does not directly follow. 42510304c731SJamie Gritton */ 42520304c731SJamie Gritton static char * 42530304c731SJamie Gritton prison_path(struct prison *pr1, struct prison *pr2) 42540304c731SJamie Gritton { 42550304c731SJamie Gritton char *path1, *path2; 42560304c731SJamie Gritton int len1; 42570304c731SJamie Gritton 42580304c731SJamie Gritton path1 = pr1->pr_path; 42590304c731SJamie Gritton path2 = pr2->pr_path; 42600304c731SJamie Gritton if (!strcmp(path1, "/")) 42610304c731SJamie Gritton return (path2); 42620304c731SJamie Gritton len1 = strlen(path1); 42630304c731SJamie Gritton if (strncmp(path1, path2, len1)) 42640304c731SJamie Gritton return (path2); 42650304c731SJamie Gritton if (path2[len1] == '\0') 42660304c731SJamie Gritton return "/"; 42670304c731SJamie Gritton if (path2[len1] == '/') 42680304c731SJamie Gritton return (path2 + len1); 42690304c731SJamie Gritton return (path2); 42700304c731SJamie Gritton } 42710304c731SJamie Gritton 42720304c731SJamie Gritton /* 42730304c731SJamie Gritton * Jail-related sysctls. 42740304c731SJamie Gritton */ 42757029da5cSPawel Biernacki static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 42760304c731SJamie Gritton "Jails"); 42770304c731SJamie Gritton 4278eb8dcdeaSGleb Smirnoff #if defined(INET) || defined(INET6) 4279eb8dcdeaSGleb Smirnoff /* 4280eb8dcdeaSGleb Smirnoff * Copy address array to memory that would be then SYSCTL_OUT-ed. 4281eb8dcdeaSGleb Smirnoff * sysctl_jail_list() helper. 4282eb8dcdeaSGleb Smirnoff */ 4283eb8dcdeaSGleb Smirnoff static void 4284eb8dcdeaSGleb Smirnoff prison_ip_copyout(struct prison *pr, const pr_family_t af, void **out, int *len) 4285eb8dcdeaSGleb Smirnoff { 42862c33b456SZhenlei Huang const struct prison_ip *pip; 4287eb8dcdeaSGleb Smirnoff const size_t size = pr_families[af].size; 4288eb8dcdeaSGleb Smirnoff 4289eb8dcdeaSGleb Smirnoff again: 4290eb8dcdeaSGleb Smirnoff mtx_assert(&pr->pr_mtx, MA_OWNED); 42912c33b456SZhenlei Huang if ((pip = pr->pr_addrs[af]) != NULL) { 42922c33b456SZhenlei Huang if (*len < pip->ips) { 42932c33b456SZhenlei Huang *len = pip->ips; 4294eb8dcdeaSGleb Smirnoff mtx_unlock(&pr->pr_mtx); 4295eb8dcdeaSGleb Smirnoff *out = realloc(*out, *len * size, M_TEMP, M_WAITOK); 4296eb8dcdeaSGleb Smirnoff mtx_lock(&pr->pr_mtx); 4297eb8dcdeaSGleb Smirnoff goto again; 4298eb8dcdeaSGleb Smirnoff } 42992c33b456SZhenlei Huang bcopy(pip->pr_ip, *out, pip->ips * size); 4300eb8dcdeaSGleb Smirnoff } 4301eb8dcdeaSGleb Smirnoff } 4302eb8dcdeaSGleb Smirnoff #endif 4303eb8dcdeaSGleb Smirnoff 4304fd7a8150SMike Barcroft static int 4305fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS) 4306fd7a8150SMike Barcroft { 4307b38ff370SJamie Gritton struct xprison *xp; 43080304c731SJamie Gritton struct prison *pr, *cpr; 4309b38ff370SJamie Gritton #ifdef INET 4310b38ff370SJamie Gritton struct in_addr *ip4 = NULL; 4311b38ff370SJamie Gritton int ip4s = 0; 4312b38ff370SJamie Gritton #endif 4313b38ff370SJamie Gritton #ifdef INET6 43143beefaedSColin Percival struct in6_addr *ip6 = NULL; 4315b38ff370SJamie Gritton int ip6s = 0; 4316b38ff370SJamie Gritton #endif 43170304c731SJamie Gritton int descend, error; 4318fd7a8150SMike Barcroft 4319b38ff370SJamie Gritton xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK); 43200304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 4321b38ff370SJamie Gritton error = 0; 4322dc68a633SPawel Jakub Dawidek sx_slock(&allprison_lock); 43230304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 43240304c731SJamie Gritton mtx_lock(&cpr->pr_mtx); 4325413628a7SBjoern A. Zeeb #ifdef INET 4326eb8dcdeaSGleb Smirnoff prison_ip_copyout(cpr, PR_INET, (void **)&ip4, &ip4s); 4327413628a7SBjoern A. Zeeb #endif 4328413628a7SBjoern A. Zeeb #ifdef INET6 4329eb8dcdeaSGleb Smirnoff prison_ip_copyout(cpr, PR_INET6, (void **)&ip6, &ip6s); 4330b38ff370SJamie Gritton #endif 4331b38ff370SJamie Gritton bzero(xp, sizeof(*xp)); 4332fd7a8150SMike Barcroft xp->pr_version = XPRISON_VERSION; 43330304c731SJamie Gritton xp->pr_id = cpr->pr_id; 43341158508aSJamie Gritton xp->pr_state = cpr->pr_state; 43350304c731SJamie Gritton strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path)); 4336c1f19219SJamie Gritton strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host)); 43370304c731SJamie Gritton strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name)); 4338413628a7SBjoern A. Zeeb #ifdef INET 4339eb8dcdeaSGleb Smirnoff xp->pr_ip4s = ip4s; 4340413628a7SBjoern A. Zeeb #endif 4341413628a7SBjoern A. Zeeb #ifdef INET6 4342eb8dcdeaSGleb Smirnoff xp->pr_ip6s = ip6s; 4343413628a7SBjoern A. Zeeb #endif 43440304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 4345b38ff370SJamie Gritton error = SYSCTL_OUT(req, xp, sizeof(*xp)); 4346b38ff370SJamie Gritton if (error) 4347b38ff370SJamie Gritton break; 4348413628a7SBjoern A. Zeeb #ifdef INET 4349b38ff370SJamie Gritton if (xp->pr_ip4s > 0) { 4350b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip4, 4351b38ff370SJamie Gritton xp->pr_ip4s * sizeof(struct in_addr)); 4352b38ff370SJamie Gritton if (error) 4353b38ff370SJamie Gritton break; 4354413628a7SBjoern A. Zeeb } 4355413628a7SBjoern A. Zeeb #endif 4356413628a7SBjoern A. Zeeb #ifdef INET6 4357b38ff370SJamie Gritton if (xp->pr_ip6s > 0) { 4358b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip6, 4359b38ff370SJamie Gritton xp->pr_ip6s * sizeof(struct in6_addr)); 4360b38ff370SJamie Gritton if (error) 4361b38ff370SJamie Gritton break; 4362413628a7SBjoern A. Zeeb } 4363413628a7SBjoern A. Zeeb #endif 4364fd7a8150SMike Barcroft } 4365dc68a633SPawel Jakub Dawidek sx_sunlock(&allprison_lock); 4366b38ff370SJamie Gritton free(xp, M_TEMP); 4367b38ff370SJamie Gritton #ifdef INET 4368b38ff370SJamie Gritton free(ip4, M_TEMP); 4369b38ff370SJamie Gritton #endif 4370b38ff370SJamie Gritton #ifdef INET6 4371b38ff370SJamie Gritton free(ip6, M_TEMP); 4372b38ff370SJamie Gritton #endif 4373fd7a8150SMike Barcroft return (error); 4374fd7a8150SMike Barcroft } 4375fd7a8150SMike Barcroft 4376f3b86a5fSEd Schouten SYSCTL_OID(_security_jail, OID_AUTO, list, 4377f3b86a5fSEd Schouten CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4378f3b86a5fSEd Schouten sysctl_jail_list, "S", "List of active jails"); 4379461167c2SPawel Jakub Dawidek 4380461167c2SPawel Jakub Dawidek static int 4381461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS) 4382461167c2SPawel Jakub Dawidek { 4383461167c2SPawel Jakub Dawidek int error, injail; 4384461167c2SPawel Jakub Dawidek 4385461167c2SPawel Jakub Dawidek injail = jailed(req->td->td_ucred); 4386461167c2SPawel Jakub Dawidek error = SYSCTL_OUT(req, &injail, sizeof(injail)); 4387461167c2SPawel Jakub Dawidek 4388461167c2SPawel Jakub Dawidek return (error); 4389461167c2SPawel Jakub Dawidek } 43900304c731SJamie Gritton 4391f3b86a5fSEd Schouten SYSCTL_PROC(_security_jail, OID_AUTO, jailed, 4392f3b86a5fSEd Schouten CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4393f3b86a5fSEd Schouten sysctl_jail_jailed, "I", "Process in jail?"); 4394413628a7SBjoern A. Zeeb 4395761d2bb5SJamie Gritton static int 4396761d2bb5SJamie Gritton sysctl_jail_vnet(SYSCTL_HANDLER_ARGS) 4397761d2bb5SJamie Gritton { 4398761d2bb5SJamie Gritton int error, havevnet; 4399761d2bb5SJamie Gritton #ifdef VIMAGE 4400761d2bb5SJamie Gritton struct ucred *cred = req->td->td_ucred; 4401761d2bb5SJamie Gritton 4402761d2bb5SJamie Gritton havevnet = jailed(cred) && prison_owns_vnet(cred); 4403761d2bb5SJamie Gritton #else 4404761d2bb5SJamie Gritton havevnet = 0; 4405761d2bb5SJamie Gritton #endif 4406761d2bb5SJamie Gritton error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet)); 4407761d2bb5SJamie Gritton 4408761d2bb5SJamie Gritton return (error); 4409761d2bb5SJamie Gritton } 4410761d2bb5SJamie Gritton 4411761d2bb5SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, vnet, 4412761d2bb5SJamie Gritton CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4413bb8f1623SBjoern A. Zeeb sysctl_jail_vnet, "I", "Jail owns vnet?"); 4414761d2bb5SJamie Gritton 44150304c731SJamie Gritton #if defined(INET) || defined(INET6) 4416e92e0574SJamie Gritton SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW, 44170304c731SJamie Gritton &jail_max_af_ips, 0, 4418c542c43eSJamie Gritton "Number of IP addresses a jail may have at most per address family (deprecated)"); 44190304c731SJamie Gritton #endif 44200304c731SJamie Gritton 44210304c731SJamie Gritton /* 4422c542c43eSJamie Gritton * Default parameters for jail(2) compatibility. For historical reasons, 4423c542c43eSJamie Gritton * the sysctl names have varying similarity to the parameter names. Prisons 4424c542c43eSJamie Gritton * just see their own parameters, and can't change them. 44250304c731SJamie Gritton */ 44260304c731SJamie Gritton static int 44270304c731SJamie Gritton sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS) 44280304c731SJamie Gritton { 44290fe74ae6SJamie Gritton int error, i; 44300304c731SJamie Gritton 44310304c731SJamie Gritton /* Get the current flag value, and convert it to a boolean. */ 44320fe74ae6SJamie Gritton if (req->td->td_ucred->cr_prison == &prison0) { 44330fe74ae6SJamie Gritton mtx_lock(&prison0.pr_mtx); 44340fe74ae6SJamie Gritton i = (jail_default_allow & arg2) != 0; 44350fe74ae6SJamie Gritton mtx_unlock(&prison0.pr_mtx); 44360fe74ae6SJamie Gritton } else 44370fe74ae6SJamie Gritton i = prison_allow(req->td->td_ucred, arg2); 44380fe74ae6SJamie Gritton 44390304c731SJamie Gritton if (arg1 != NULL) 44400304c731SJamie Gritton i = !i; 44410304c731SJamie Gritton error = sysctl_handle_int(oidp, &i, 0, req); 4442c542c43eSJamie Gritton if (error || !req->newptr) 44430304c731SJamie Gritton return (error); 44440304c731SJamie Gritton i = i ? arg2 : 0; 44450304c731SJamie Gritton if (arg1 != NULL) 44460304c731SJamie Gritton i ^= arg2; 44470304c731SJamie Gritton /* 44480304c731SJamie Gritton * The sysctls don't have CTLFLAGS_PRISON, so assume prison0 44490304c731SJamie Gritton * for writing. 44500304c731SJamie Gritton */ 44510304c731SJamie Gritton mtx_lock(&prison0.pr_mtx); 44520304c731SJamie Gritton jail_default_allow = (jail_default_allow & ~arg2) | i; 44530304c731SJamie Gritton mtx_unlock(&prison0.pr_mtx); 44540304c731SJamie Gritton return (0); 44550304c731SJamie Gritton } 44560304c731SJamie Gritton 44570304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed, 4458c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44590304c731SJamie Gritton NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I", 4460c542c43eSJamie Gritton "Processes in jail can set their hostnames (deprecated)"); 44610304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only, 4462c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44630304c731SJamie Gritton (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I", 4464c542c43eSJamie Gritton "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)"); 44650304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed, 4466c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44670304c731SJamie Gritton NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I", 4468c542c43eSJamie Gritton "Processes in jail can use System V IPC primitives (deprecated)"); 44690304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets, 4470c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44710304c731SJamie Gritton NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I", 4472c542c43eSJamie Gritton "Prison root can create raw sockets (deprecated)"); 44730304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed, 4474c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44750304c731SJamie Gritton NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I", 4476c542c43eSJamie Gritton "Processes in jail can alter system file flags (deprecated)"); 44770304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed, 4478c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44790304c731SJamie Gritton NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I", 4480c542c43eSJamie Gritton "Processes in jail can mount/unmount jail-friendly file systems (deprecated)"); 44819fd97868SBaptiste Daroussin SYSCTL_PROC(_security_jail, OID_AUTO, mlock_allowed, 44829fd97868SBaptiste Daroussin CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44839fd97868SBaptiste Daroussin NULL, PR_ALLOW_MLOCK, sysctl_jail_default_allow, "I", 44849fd97868SBaptiste Daroussin "Processes in jail can lock/unlock physical pages in memory"); 44850304c731SJamie Gritton 44860304c731SJamie Gritton static int 44870304c731SJamie Gritton sysctl_jail_default_level(SYSCTL_HANDLER_ARGS) 44880304c731SJamie Gritton { 44890304c731SJamie Gritton struct prison *pr; 44900304c731SJamie Gritton int level, error; 44910304c731SJamie Gritton 44920304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 44930304c731SJamie Gritton level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2); 44940304c731SJamie Gritton error = sysctl_handle_int(oidp, &level, 0, req); 4495c542c43eSJamie Gritton if (error || !req->newptr) 44960304c731SJamie Gritton return (error); 44970304c731SJamie Gritton *(int *)arg1 = level; 44980304c731SJamie Gritton return (0); 44990304c731SJamie Gritton } 45000304c731SJamie Gritton 45010304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs, 4502c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4503c542c43eSJamie Gritton &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs), 45040304c731SJamie Gritton sysctl_jail_default_level, "I", 4505c542c43eSJamie Gritton "Processes in jail cannot see all mounted file systems (deprecated)"); 4506c542c43eSJamie Gritton 45070cc207a6SMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset, 4508c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 4509c542c43eSJamie Gritton &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum), 45100cc207a6SMartin Matuska sysctl_jail_default_level, "I", 4511c542c43eSJamie Gritton "Ruleset for the devfs filesystem in jail (deprecated)"); 45120cc207a6SMartin Matuska 4513ab0841bdSJamie Gritton SYSCTL_NODE(_security_jail, OID_AUTO, children, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 4514ab0841bdSJamie Gritton "Limits and stats of child jails"); 4515ab0841bdSJamie Gritton 4516ab0841bdSJamie Gritton static int 4517ab0841bdSJamie Gritton sysctl_jail_children(SYSCTL_HANDLER_ARGS) 4518ab0841bdSJamie Gritton { 4519ab0841bdSJamie Gritton struct prison *pr; 4520ab0841bdSJamie Gritton int i; 4521ab0841bdSJamie Gritton 4522ab0841bdSJamie Gritton pr = req->td->td_ucred->cr_prison; 4523ab0841bdSJamie Gritton 4524ab0841bdSJamie Gritton switch (oidp->oid_kind & CTLTYPE) { 4525ab0841bdSJamie Gritton case CTLTYPE_INT: 4526ab0841bdSJamie Gritton i = *(int *)((char *)pr + arg2); 4527ab0841bdSJamie Gritton return (SYSCTL_OUT(req, &i, sizeof(i))); 4528ab0841bdSJamie Gritton } 4529ab0841bdSJamie Gritton 4530ab0841bdSJamie Gritton return (0); 4531ab0841bdSJamie Gritton } 4532ab0841bdSJamie Gritton 4533ab0841bdSJamie Gritton SYSCTL_PROC(_security_jail_children, OID_AUTO, max, 4534ab0841bdSJamie Gritton CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 4535ab0841bdSJamie Gritton NULL, offsetof(struct prison, pr_childmax), sysctl_jail_children, 4536ab0841bdSJamie Gritton "I", "Maximum number of child jails"); 4537ab0841bdSJamie Gritton SYSCTL_PROC(_security_jail_children, OID_AUTO, cur, 4538ab0841bdSJamie Gritton CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 4539ab0841bdSJamie Gritton NULL, offsetof(struct prison, pr_childcount), sysctl_jail_children, 4540ab0841bdSJamie Gritton "I", "Current number of child jails"); 4541ab0841bdSJamie Gritton 45420304c731SJamie Gritton /* 45430304c731SJamie Gritton * Nodes to describe jail parameters. Maximum length of string parameters 45440304c731SJamie Gritton * is returned in the string itself, and the other parameters exist merely 45450304c731SJamie Gritton * to make themselves and their types known. 45460304c731SJamie Gritton */ 45477029da5cSPawel Biernacki SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 45480304c731SJamie Gritton "Jail parameters"); 45490304c731SJamie Gritton 45500304c731SJamie Gritton int 45510304c731SJamie Gritton sysctl_jail_param(SYSCTL_HANDLER_ARGS) 45520304c731SJamie Gritton { 45530304c731SJamie Gritton int i; 45540304c731SJamie Gritton long l; 45550304c731SJamie Gritton size_t s; 45560304c731SJamie Gritton char numbuf[12]; 45570304c731SJamie Gritton 45580304c731SJamie Gritton switch (oidp->oid_kind & CTLTYPE) 45590304c731SJamie Gritton { 45600304c731SJamie Gritton case CTLTYPE_LONG: 45610304c731SJamie Gritton case CTLTYPE_ULONG: 45620304c731SJamie Gritton l = 0; 45630304c731SJamie Gritton #ifdef SCTL_MASK32 45640304c731SJamie Gritton if (!(req->flags & SCTL_MASK32)) 45650304c731SJamie Gritton #endif 45660304c731SJamie Gritton return (SYSCTL_OUT(req, &l, sizeof(l))); 45670304c731SJamie Gritton case CTLTYPE_INT: 45680304c731SJamie Gritton case CTLTYPE_UINT: 45690304c731SJamie Gritton i = 0; 45700304c731SJamie Gritton return (SYSCTL_OUT(req, &i, sizeof(i))); 45710304c731SJamie Gritton case CTLTYPE_STRING: 4572e4cd31ddSJeff Roberson snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2); 45730304c731SJamie Gritton return 45740304c731SJamie Gritton (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req)); 45750304c731SJamie Gritton case CTLTYPE_STRUCT: 45760304c731SJamie Gritton s = (size_t)arg2; 45770304c731SJamie Gritton return (SYSCTL_OUT(req, &s, sizeof(s))); 45780304c731SJamie Gritton } 45790304c731SJamie Gritton return (0); 45800304c731SJamie Gritton } 45810304c731SJamie Gritton 4582b96bd95bSIan Lepore /* 4583b96bd95bSIan Lepore * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at 4584b96bd95bSIan Lepore * jail creation time but cannot be changed in an existing jail. 4585b96bd95bSIan Lepore */ 45860304c731SJamie Gritton SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID"); 45870304c731SJamie Gritton SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID"); 45880304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name"); 45890304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path"); 45900304c731SJamie Gritton SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW, 45910304c731SJamie Gritton "I", "Jail secure level"); 4592b96bd95bSIan Lepore SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I", 4593b96bd95bSIan Lepore "Jail value for kern.osreldate and uname -K"); 4594b96bd95bSIan Lepore SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN, 4595b96bd95bSIan Lepore "Jail value for kern.osrelease and uname -r"); 45960304c731SJamie Gritton SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW, 45970304c731SJamie Gritton "I", "Jail cannot see all mounted file systems"); 45980cc207a6SMartin Matuska SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW, 45990cc207a6SMartin Matuska "I", "Ruleset for in-jail devfs mounts"); 46000304c731SJamie Gritton SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW, 46010304c731SJamie Gritton "B", "Jail persistence"); 4602679e1390SJamie Gritton #ifdef VIMAGE 4603679e1390SJamie Gritton SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN, 46047cbf7213SJamie Gritton "E,jailsys", "Virtual network stack"); 4605679e1390SJamie Gritton #endif 46060304c731SJamie Gritton SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD, 46070304c731SJamie Gritton "B", "Jail is in the process of shutting down"); 46080304c731SJamie Gritton 4609b97457e2SJamie Gritton SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails"); 4610b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD, 4611b97457e2SJamie Gritton "I", "Current number of child jails"); 4612b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW, 4613b97457e2SJamie Gritton "I", "Maximum number of child jails"); 4614b97457e2SJamie Gritton 46157cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info"); 46160304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN, 46170304c731SJamie Gritton "Jail hostname"); 461876ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN, 461976ca6f88SJamie Gritton "Jail NIS domainname"); 462076ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN, 462176ca6f88SJamie Gritton "Jail host UUID"); 462276ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW, 462376ca6f88SJamie Gritton "LU", "Jail host ID"); 46240304c731SJamie Gritton 46250304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset"); 46260304c731SJamie Gritton SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID"); 46270304c731SJamie Gritton 46280304c731SJamie Gritton #ifdef INET 46292b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN, 46302b0d6f81SJamie Gritton "Jail IPv4 address virtualization"); 46310304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr), 46320304c731SJamie Gritton "S,in_addr,a", "Jail IPv4 addresses"); 4633592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4634592bcae8SBjoern A. Zeeb "B", "Do (not) use IPv4 source address selection rather than the " 4635592bcae8SBjoern A. Zeeb "primary jail IPv4 address."); 46360304c731SJamie Gritton #endif 46370304c731SJamie Gritton #ifdef INET6 46382b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN, 46392b0d6f81SJamie Gritton "Jail IPv6 address virtualization"); 46400304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr), 46410304c731SJamie Gritton "S,in6_addr,a", "Jail IPv6 addresses"); 4642592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4643592bcae8SBjoern A. Zeeb "B", "Do (not) use IPv6 source address selection rather than the " 4644592bcae8SBjoern A. Zeeb "primary jail IPv6 address."); 46450304c731SJamie Gritton #endif 46460304c731SJamie Gritton 46470304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags"); 46480304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW, 46490304c731SJamie Gritton "B", "Jail may set hostname"); 46500304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW, 46510304c731SJamie Gritton "B", "Jail may use SYSV IPC"); 46520304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW, 46530304c731SJamie Gritton "B", "Jail may create raw sockets"); 46540304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW, 46550304c731SJamie Gritton "B", "Jail may alter system file flags"); 46560304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW, 46570304c731SJamie Gritton "B", "Jail may set file quotas"); 46580304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW, 46590304c731SJamie Gritton "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route"); 4660ccd6ac9fSAntoine Brodin SYSCTL_JAIL_PARAM(_allow, mlock, CTLTYPE_INT | CTLFLAG_RW, 4661ccd6ac9fSAntoine Brodin "B", "Jail may lock (unlock) physical pages in memory"); 4662e28f9b7dSAllan Jude SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW, 4663e28f9b7dSAllan Jude "B", "Jail may bind sockets to reserved ports"); 4664b19d66fdSJamie Gritton SYSCTL_JAIL_PARAM(_allow, read_msgbuf, CTLTYPE_INT | CTLFLAG_RW, 4665b19d66fdSJamie Gritton "B", "Jail may read the kernel message buffer"); 4666b3079544SJamie Gritton SYSCTL_JAIL_PARAM(_allow, unprivileged_proc_debug, CTLTYPE_INT | CTLFLAG_RW, 4667b3079544SJamie Gritton "B", "Unprivileged processes may use process debugging facilities"); 466805e1e482SMariusz Zaborski SYSCTL_JAIL_PARAM(_allow, suser, CTLTYPE_INT | CTLFLAG_RW, 466905e1e482SMariusz Zaborski "B", "Processes in jail with uid 0 have privilege"); 4670cbbb2203SRick Macklem #ifdef VIMAGE 4671bba7a2e8SRick Macklem SYSCTL_JAIL_PARAM(_allow, nfsd, CTLTYPE_INT | CTLFLAG_RW, 4672bba7a2e8SRick Macklem "B", "Mountd/nfsd may run in the jail"); 4673bba7a2e8SRick Macklem #endif 4674cb48780dSShawn Webb SYSCTL_JAIL_PARAM(_allow, extattr, CTLTYPE_INT | CTLFLAG_RW, 4675cb48780dSShawn Webb "B", "Jail may set system-level filesystem extended attributes"); 4676d3bb35d4SMariusz Zaborski SYSCTL_JAIL_PARAM(_allow, adjtime, CTLTYPE_INT | CTLFLAG_RW, 4677d3bb35d4SMariusz Zaborski "B", "Jail may adjust system time"); 4678d3bb35d4SMariusz Zaborski SYSCTL_JAIL_PARAM(_allow, settime, CTLTYPE_INT | CTLFLAG_RW, 4679d3bb35d4SMariusz Zaborski "B", "Jail may set system time"); 46800304c731SJamie Gritton 4681bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags"); 4682bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW, 4683bf3db8aaSMartin Matuska "B", "Jail may mount/unmount jail-friendly file systems in general"); 46840e5c6bd4SJamie Gritton 46850e5c6bd4SJamie Gritton /* 46860a172404SJamie Gritton * Add a dynamic parameter allow.<name>, or allow.<prefix>.<name>. Return 46870a172404SJamie Gritton * its associated bit in the pr_allow bitmask, or zero if the parameter was 46880a172404SJamie Gritton * not created. 46890e5c6bd4SJamie Gritton */ 46900a172404SJamie Gritton unsigned 46910a172404SJamie Gritton prison_add_allow(const char *prefix, const char *name, const char *prefix_descr, 46920a172404SJamie Gritton const char *descr) 46930e5c6bd4SJamie Gritton { 46940e5c6bd4SJamie Gritton struct bool_flags *bf; 46950a172404SJamie Gritton struct sysctl_oid *parent; 46960a172404SJamie Gritton char *allow_name, *allow_noname, *allowed; 4697c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR 4698c542c43eSJamie Gritton char *descr_deprecated; 4699c542c43eSJamie Gritton #endif 47005d58f959SJamie Gritton u_int allow_flag; 47010e5c6bd4SJamie Gritton 47020a172404SJamie Gritton if (prefix 47030a172404SJamie Gritton ? asprintf(&allow_name, M_PRISON, "allow.%s.%s", prefix, name) 47040a172404SJamie Gritton < 0 || 47050a172404SJamie Gritton asprintf(&allow_noname, M_PRISON, "allow.%s.no%s", prefix, name) 47060a172404SJamie Gritton < 0 47070a172404SJamie Gritton : asprintf(&allow_name, M_PRISON, "allow.%s", name) < 0 || 47080a172404SJamie Gritton asprintf(&allow_noname, M_PRISON, "allow.no%s", name) < 0) { 47090e5c6bd4SJamie Gritton free(allow_name, M_PRISON); 47100a172404SJamie Gritton return 0; 47110e5c6bd4SJamie Gritton } 47120e5c6bd4SJamie Gritton 47130e5c6bd4SJamie Gritton /* 47140a172404SJamie Gritton * See if this parameter has already beed added, i.e. a module was 47150a172404SJamie Gritton * previously loaded/unloaded. 47160e5c6bd4SJamie Gritton */ 47170e5c6bd4SJamie Gritton mtx_lock(&prison0.pr_mtx); 47180e5c6bd4SJamie Gritton for (bf = pr_flag_allow; 47195d58f959SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 47205d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 47210e5c6bd4SJamie Gritton bf++) { 47220e5c6bd4SJamie Gritton if (strcmp(bf->name, allow_name) == 0) { 47230a172404SJamie Gritton allow_flag = bf->flag; 47240e5c6bd4SJamie Gritton goto no_add; 47250e5c6bd4SJamie Gritton } 47260e5c6bd4SJamie Gritton } 47270e5c6bd4SJamie Gritton 47280e5c6bd4SJamie Gritton /* 47295d58f959SJamie Gritton * Find a free bit in pr_allow_all, failing if there are none 47300e5c6bd4SJamie Gritton * (which shouldn't happen as long as we keep track of how many 47310a172404SJamie Gritton * potential dynamic flags exist). 47320e5c6bd4SJamie Gritton */ 47330e5c6bd4SJamie Gritton for (allow_flag = 1;; allow_flag <<= 1) { 47340e5c6bd4SJamie Gritton if (allow_flag == 0) 47350e5c6bd4SJamie Gritton goto no_add; 47365d58f959SJamie Gritton if ((pr_allow_all & allow_flag) == 0) 47370e5c6bd4SJamie Gritton break; 47380e5c6bd4SJamie Gritton } 47390e5c6bd4SJamie Gritton 47405d58f959SJamie Gritton /* Note the parameter in the next open slot in pr_flag_allow. */ 47415d58f959SJamie Gritton for (bf = pr_flag_allow; ; bf++) { 47420e5c6bd4SJamie Gritton if (bf == pr_flag_allow + nitems(pr_flag_allow)) { 47430e5c6bd4SJamie Gritton /* This should never happen, but is not fatal. */ 47440a172404SJamie Gritton allow_flag = 0; 47450e5c6bd4SJamie Gritton goto no_add; 47460e5c6bd4SJamie Gritton } 47475d58f959SJamie Gritton if (atomic_load_int(&bf->flag) == 0) 47485d58f959SJamie Gritton break; 47495d58f959SJamie Gritton } 47500e5c6bd4SJamie Gritton bf->name = allow_name; 47510e5c6bd4SJamie Gritton bf->noname = allow_noname; 47525d58f959SJamie Gritton pr_allow_all |= allow_flag; 47535d58f959SJamie Gritton /* 47545d58f959SJamie Gritton * prison0 always has permission for the new parameter. 47555d58f959SJamie Gritton * Other jails must have it granted to them. 47565d58f959SJamie Gritton */ 47575d58f959SJamie Gritton prison0.pr_allow |= allow_flag; 47585d58f959SJamie Gritton /* The flag indicates a valid entry, so make sure it is set last. */ 47595d58f959SJamie Gritton atomic_store_rel_int(&bf->flag, allow_flag); 47600e5c6bd4SJamie Gritton mtx_unlock(&prison0.pr_mtx); 47610e5c6bd4SJamie Gritton 4762c542c43eSJamie Gritton /* 47634771011bSGordon Bergling * Create sysctls for the parameter, and the back-compat global 4764c542c43eSJamie Gritton * permission. 4765c542c43eSJamie Gritton */ 47660a172404SJamie Gritton parent = prefix 47670a172404SJamie Gritton ? SYSCTL_ADD_NODE(NULL, 47680a172404SJamie Gritton SYSCTL_CHILDREN(&sysctl___security_jail_param_allow), 47697029da5cSPawel Biernacki OID_AUTO, prefix, CTLFLAG_MPSAFE, 0, prefix_descr) 47700a172404SJamie Gritton : &sysctl___security_jail_param_allow; 47710a172404SJamie Gritton (void)SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(parent), OID_AUTO, 47720a172404SJamie Gritton name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 47730e5c6bd4SJamie Gritton NULL, 0, sysctl_jail_param, "B", descr); 47740a172404SJamie Gritton if ((prefix 47750a172404SJamie Gritton ? asprintf(&allowed, M_TEMP, "%s_%s_allowed", prefix, name) 47760a172404SJamie Gritton : asprintf(&allowed, M_TEMP, "%s_allowed", name)) >= 0) { 4777c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR 4778c542c43eSJamie Gritton (void)asprintf(&descr_deprecated, M_TEMP, "%s (deprecated)", 4779c542c43eSJamie Gritton descr); 4780c542c43eSJamie Gritton #endif 47810e5c6bd4SJamie Gritton (void)SYSCTL_ADD_PROC(NULL, 47820a172404SJamie Gritton SYSCTL_CHILDREN(&sysctl___security_jail), OID_AUTO, allowed, 4783c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, allow_flag, 4784c542c43eSJamie Gritton sysctl_jail_default_allow, "I", descr_deprecated); 4785c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR 4786c542c43eSJamie Gritton free(descr_deprecated, M_TEMP); 4787c542c43eSJamie Gritton #endif 47880a172404SJamie Gritton free(allowed, M_TEMP); 47890e5c6bd4SJamie Gritton } 47900a172404SJamie Gritton return allow_flag; 47910e5c6bd4SJamie Gritton 47920e5c6bd4SJamie Gritton no_add: 47930e5c6bd4SJamie Gritton mtx_unlock(&prison0.pr_mtx); 47940e5c6bd4SJamie Gritton free(allow_name, M_PRISON); 47950e5c6bd4SJamie Gritton free(allow_noname, M_PRISON); 47960a172404SJamie Gritton return allow_flag; 47970a172404SJamie Gritton } 47980a172404SJamie Gritton 47990a172404SJamie Gritton /* 48000a172404SJamie Gritton * The VFS system will register jail-aware filesystems here. They each get 48010a172404SJamie Gritton * a parameter allow.mount.xxxfs and a flag to check when a jailed user 48020a172404SJamie Gritton * attempts to mount. 48030a172404SJamie Gritton */ 48040a172404SJamie Gritton void 48050a172404SJamie Gritton prison_add_vfs(struct vfsconf *vfsp) 48060a172404SJamie Gritton { 48070a172404SJamie Gritton #ifdef NO_SYSCTL_DESCR 48080a172404SJamie Gritton 48090a172404SJamie Gritton vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name, 48100a172404SJamie Gritton NULL, NULL); 48110a172404SJamie Gritton #else 48120a172404SJamie Gritton char *descr; 48130a172404SJamie Gritton 48140a172404SJamie Gritton (void)asprintf(&descr, M_TEMP, "Jail may mount the %s file system", 48150a172404SJamie Gritton vfsp->vfc_name); 48160a172404SJamie Gritton vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name, 48170a172404SJamie Gritton NULL, descr); 48180a172404SJamie Gritton free(descr, M_TEMP); 48190a172404SJamie Gritton #endif 48200e5c6bd4SJamie Gritton } 4821bf3db8aaSMartin Matuska 48224b5c9cf6SEdward Tomasz Napierala #ifdef RACCT 4823097055e2SEdward Tomasz Napierala void 4824097055e2SEdward Tomasz Napierala prison_racct_foreach(void (*callback)(struct racct *racct, 482515db3c07SEdward Tomasz Napierala void *arg2, void *arg3), void (*pre)(void), void (*post)(void), 482615db3c07SEdward Tomasz Napierala void *arg2, void *arg3) 4827097055e2SEdward Tomasz Napierala { 4828a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4829097055e2SEdward Tomasz Napierala 48304b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 48314b5c9cf6SEdward Tomasz Napierala 4832097055e2SEdward Tomasz Napierala sx_slock(&allprison_lock); 483315db3c07SEdward Tomasz Napierala if (pre != NULL) 483415db3c07SEdward Tomasz Napierala (pre)(); 4835a7ad07bfSEdward Tomasz Napierala LIST_FOREACH(prr, &allprison_racct, prr_next) 4836a7ad07bfSEdward Tomasz Napierala (callback)(prr->prr_racct, arg2, arg3); 483715db3c07SEdward Tomasz Napierala if (post != NULL) 483815db3c07SEdward Tomasz Napierala (post)(); 4839097055e2SEdward Tomasz Napierala sx_sunlock(&allprison_lock); 4840097055e2SEdward Tomasz Napierala } 48410304c731SJamie Gritton 4842a7ad07bfSEdward Tomasz Napierala static struct prison_racct * 4843a7ad07bfSEdward Tomasz Napierala prison_racct_find_locked(const char *name) 4844a7ad07bfSEdward Tomasz Napierala { 4845a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4846a7ad07bfSEdward Tomasz Napierala 48474b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4848a7ad07bfSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4849a7ad07bfSEdward Tomasz Napierala 4850a7ad07bfSEdward Tomasz Napierala if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN) 4851a7ad07bfSEdward Tomasz Napierala return (NULL); 4852a7ad07bfSEdward Tomasz Napierala 4853a7ad07bfSEdward Tomasz Napierala LIST_FOREACH(prr, &allprison_racct, prr_next) { 4854a7ad07bfSEdward Tomasz Napierala if (strcmp(name, prr->prr_name) != 0) 4855a7ad07bfSEdward Tomasz Napierala continue; 4856a7ad07bfSEdward Tomasz Napierala 4857a7ad07bfSEdward Tomasz Napierala /* Found prison_racct with a matching name? */ 4858a7ad07bfSEdward Tomasz Napierala prison_racct_hold(prr); 4859a7ad07bfSEdward Tomasz Napierala return (prr); 4860a7ad07bfSEdward Tomasz Napierala } 4861a7ad07bfSEdward Tomasz Napierala 4862a7ad07bfSEdward Tomasz Napierala /* Add new prison_racct. */ 4863a7ad07bfSEdward Tomasz Napierala prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK); 4864a7ad07bfSEdward Tomasz Napierala racct_create(&prr->prr_racct); 4865a7ad07bfSEdward Tomasz Napierala 4866a7ad07bfSEdward Tomasz Napierala strcpy(prr->prr_name, name); 4867a7ad07bfSEdward Tomasz Napierala refcount_init(&prr->prr_refcount, 1); 4868a7ad07bfSEdward Tomasz Napierala LIST_INSERT_HEAD(&allprison_racct, prr, prr_next); 4869a7ad07bfSEdward Tomasz Napierala 4870a7ad07bfSEdward Tomasz Napierala return (prr); 4871a7ad07bfSEdward Tomasz Napierala } 4872a7ad07bfSEdward Tomasz Napierala 4873a7ad07bfSEdward Tomasz Napierala struct prison_racct * 4874a7ad07bfSEdward Tomasz Napierala prison_racct_find(const char *name) 4875a7ad07bfSEdward Tomasz Napierala { 4876a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4877a7ad07bfSEdward Tomasz Napierala 48784b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 48794b5c9cf6SEdward Tomasz Napierala 4880a7ad07bfSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4881a7ad07bfSEdward Tomasz Napierala prr = prison_racct_find_locked(name); 4882a7ad07bfSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4883a7ad07bfSEdward Tomasz Napierala return (prr); 4884a7ad07bfSEdward Tomasz Napierala } 4885a7ad07bfSEdward Tomasz Napierala 4886a7ad07bfSEdward Tomasz Napierala void 4887a7ad07bfSEdward Tomasz Napierala prison_racct_hold(struct prison_racct *prr) 4888a7ad07bfSEdward Tomasz Napierala { 4889a7ad07bfSEdward Tomasz Napierala 48904b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 48914b5c9cf6SEdward Tomasz Napierala 4892a7ad07bfSEdward Tomasz Napierala refcount_acquire(&prr->prr_refcount); 4893a7ad07bfSEdward Tomasz Napierala } 4894a7ad07bfSEdward Tomasz Napierala 4895c34bbd2aSEdward Tomasz Napierala static void 4896c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(struct prison_racct *prr) 4897c34bbd2aSEdward Tomasz Napierala { 4898c34bbd2aSEdward Tomasz Napierala 48994b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4900c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4901c34bbd2aSEdward Tomasz Napierala 4902c34bbd2aSEdward Tomasz Napierala if (refcount_release(&prr->prr_refcount)) { 4903c34bbd2aSEdward Tomasz Napierala racct_destroy(&prr->prr_racct); 4904c34bbd2aSEdward Tomasz Napierala LIST_REMOVE(prr, prr_next); 4905c34bbd2aSEdward Tomasz Napierala free(prr, M_PRISON_RACCT); 4906c34bbd2aSEdward Tomasz Napierala } 4907c34bbd2aSEdward Tomasz Napierala } 4908c34bbd2aSEdward Tomasz Napierala 4909a7ad07bfSEdward Tomasz Napierala void 4910a7ad07bfSEdward Tomasz Napierala prison_racct_free(struct prison_racct *prr) 4911a7ad07bfSEdward Tomasz Napierala { 4912a7ad07bfSEdward Tomasz Napierala 49134b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4914c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_UNLOCKED); 4915c34bbd2aSEdward Tomasz Napierala 49166ff4688bSMateusz Guzik if (refcount_release_if_not_last(&prr->prr_refcount)) 4917a7ad07bfSEdward Tomasz Napierala return; 4918a7ad07bfSEdward Tomasz Napierala 4919a7ad07bfSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4920c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(prr); 4921a7ad07bfSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4922a7ad07bfSEdward Tomasz Napierala } 4923a7ad07bfSEdward Tomasz Napierala 4924a7ad07bfSEdward Tomasz Napierala static void 4925a7ad07bfSEdward Tomasz Napierala prison_racct_attach(struct prison *pr) 4926a7ad07bfSEdward Tomasz Napierala { 4927a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4928a7ad07bfSEdward Tomasz Napierala 49294b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4930c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4931c34bbd2aSEdward Tomasz Napierala 4932a7ad07bfSEdward Tomasz Napierala prr = prison_racct_find_locked(pr->pr_name); 4933a7ad07bfSEdward Tomasz Napierala KASSERT(prr != NULL, ("cannot find prison_racct")); 4934a7ad07bfSEdward Tomasz Napierala 4935a7ad07bfSEdward Tomasz Napierala pr->pr_prison_racct = prr; 4936a7ad07bfSEdward Tomasz Napierala } 4937a7ad07bfSEdward Tomasz Napierala 4938c34bbd2aSEdward Tomasz Napierala /* 4939c34bbd2aSEdward Tomasz Napierala * Handle jail renaming. From the racct point of view, renaming means 4940c34bbd2aSEdward Tomasz Napierala * moving from one prison_racct to another. 4941c34bbd2aSEdward Tomasz Napierala */ 4942c34bbd2aSEdward Tomasz Napierala static void 4943c34bbd2aSEdward Tomasz Napierala prison_racct_modify(struct prison *pr) 4944c34bbd2aSEdward Tomasz Napierala { 4945dbadb015SKonstantin Belousov #ifdef RCTL 4946c34bbd2aSEdward Tomasz Napierala struct proc *p; 4947c34bbd2aSEdward Tomasz Napierala struct ucred *cred; 4948dbadb015SKonstantin Belousov #endif 4949c34bbd2aSEdward Tomasz Napierala struct prison_racct *oldprr; 4950c34bbd2aSEdward Tomasz Napierala 49514b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 49524b5c9cf6SEdward Tomasz Napierala 4953c34bbd2aSEdward Tomasz Napierala sx_slock(&allproc_lock); 4954c34bbd2aSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4955c34bbd2aSEdward Tomasz Napierala 4956e30345e7SEdward Tomasz Napierala if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) { 4957e30345e7SEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4958e30345e7SEdward Tomasz Napierala sx_sunlock(&allproc_lock); 4959c34bbd2aSEdward Tomasz Napierala return; 4960e30345e7SEdward Tomasz Napierala } 4961c34bbd2aSEdward Tomasz Napierala 4962c34bbd2aSEdward Tomasz Napierala oldprr = pr->pr_prison_racct; 4963c34bbd2aSEdward Tomasz Napierala pr->pr_prison_racct = NULL; 4964c34bbd2aSEdward Tomasz Napierala 4965c34bbd2aSEdward Tomasz Napierala prison_racct_attach(pr); 4966c34bbd2aSEdward Tomasz Napierala 4967c34bbd2aSEdward Tomasz Napierala /* 4968c34bbd2aSEdward Tomasz Napierala * Move resource utilisation records. 4969c34bbd2aSEdward Tomasz Napierala */ 4970c34bbd2aSEdward Tomasz Napierala racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct); 4971c34bbd2aSEdward Tomasz Napierala 4972f87beb93SAndriy Gapon #ifdef RCTL 4973c34bbd2aSEdward Tomasz Napierala /* 4974c34bbd2aSEdward Tomasz Napierala * Force rctl to reattach rules to processes. 4975c34bbd2aSEdward Tomasz Napierala */ 4976c34bbd2aSEdward Tomasz Napierala FOREACH_PROC_IN_SYSTEM(p) { 4977c34bbd2aSEdward Tomasz Napierala PROC_LOCK(p); 4978c34bbd2aSEdward Tomasz Napierala cred = crhold(p->p_ucred); 4979c34bbd2aSEdward Tomasz Napierala PROC_UNLOCK(p); 4980f87beb93SAndriy Gapon rctl_proc_ucred_changed(p, cred); 4981c34bbd2aSEdward Tomasz Napierala crfree(cred); 4982c34bbd2aSEdward Tomasz Napierala } 4983f87beb93SAndriy Gapon #endif 4984c34bbd2aSEdward Tomasz Napierala 4985c34bbd2aSEdward Tomasz Napierala sx_sunlock(&allproc_lock); 4986c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(oldprr); 4987c34bbd2aSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4988c34bbd2aSEdward Tomasz Napierala } 4989c34bbd2aSEdward Tomasz Napierala 4990a7ad07bfSEdward Tomasz Napierala static void 4991a7ad07bfSEdward Tomasz Napierala prison_racct_detach(struct prison *pr) 4992a7ad07bfSEdward Tomasz Napierala { 4993c34bbd2aSEdward Tomasz Napierala 49944b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4995c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_UNLOCKED); 4996c34bbd2aSEdward Tomasz Napierala 4997af3c786cSMateusz Guzik if (pr->pr_prison_racct == NULL) 4998af3c786cSMateusz Guzik return; 4999a7ad07bfSEdward Tomasz Napierala prison_racct_free(pr->pr_prison_racct); 5000a7ad07bfSEdward Tomasz Napierala pr->pr_prison_racct = NULL; 5001a7ad07bfSEdward Tomasz Napierala } 5002a7ad07bfSEdward Tomasz Napierala #endif /* RACCT */ 5003a7ad07bfSEdward Tomasz Napierala 5004413628a7SBjoern A. Zeeb #ifdef DDB 5005b38ff370SJamie Gritton 5006b38ff370SJamie Gritton static void 5007b38ff370SJamie Gritton db_show_prison(struct prison *pr) 5008413628a7SBjoern A. Zeeb { 5009672756aaSJamie Gritton struct bool_flags *bf; 5010672756aaSJamie Gritton struct jailsys_flags *jsf; 5011b38ff370SJamie Gritton #if defined(INET) || defined(INET6) 5012b38ff370SJamie Gritton int ii; 5013500f82d6SZhenlei Huang struct prison_ip *pip; 5014413628a7SBjoern A. Zeeb #endif 5015672756aaSJamie Gritton unsigned f; 50168144690aSEric van Gyzen #ifdef INET 50178144690aSEric van Gyzen char ip4buf[INET_ADDRSTRLEN]; 50188144690aSEric van Gyzen #endif 5019413628a7SBjoern A. Zeeb #ifdef INET6 5020413628a7SBjoern A. Zeeb char ip6buf[INET6_ADDRSTRLEN]; 5021413628a7SBjoern A. Zeeb #endif 5022413628a7SBjoern A. Zeeb 5023b38ff370SJamie Gritton db_printf("prison %p:\n", pr); 5024b38ff370SJamie Gritton db_printf(" jid = %d\n", pr->pr_id); 5025b38ff370SJamie Gritton db_printf(" name = %s\n", pr->pr_name); 50260304c731SJamie Gritton db_printf(" parent = %p\n", pr->pr_parent); 5027b38ff370SJamie Gritton db_printf(" ref = %d\n", pr->pr_ref); 5028b38ff370SJamie Gritton db_printf(" uref = %d\n", pr->pr_uref); 50291158508aSJamie Gritton db_printf(" state = %s\n", 50301158508aSJamie Gritton pr->pr_state == PRISON_STATE_ALIVE ? "alive" : 50311158508aSJamie Gritton pr->pr_state == PRISON_STATE_DYING ? "dying" : 50321158508aSJamie Gritton "invalid"); 5033b38ff370SJamie Gritton db_printf(" path = %s\n", pr->pr_path); 5034b38ff370SJamie Gritton db_printf(" cpuset = %d\n", pr->pr_cpuset 5035b38ff370SJamie Gritton ? pr->pr_cpuset->cs_id : -1); 5036679e1390SJamie Gritton #ifdef VIMAGE 5037679e1390SJamie Gritton db_printf(" vnet = %p\n", pr->pr_vnet); 5038679e1390SJamie Gritton #endif 5039b38ff370SJamie Gritton db_printf(" root = %p\n", pr->pr_root); 5040b38ff370SJamie Gritton db_printf(" securelevel = %d\n", pr->pr_securelevel); 50410cc207a6SMartin Matuska db_printf(" devfs_rsnum = %d\n", pr->pr_devfs_rsnum); 5042fe0518e9SBjoern A. Zeeb db_printf(" children.max = %d\n", pr->pr_childmax); 5043fe0518e9SBjoern A. Zeeb db_printf(" children.cur = %d\n", pr->pr_childcount); 50440304c731SJamie Gritton db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children)); 50450304c731SJamie Gritton db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling)); 5046fe0518e9SBjoern A. Zeeb db_printf(" flags = 0x%x", pr->pr_flags); 5047672756aaSJamie Gritton for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++) 5048672756aaSJamie Gritton if (pr->pr_flags & bf->flag) 5049672756aaSJamie Gritton db_printf(" %s", bf->name); 5050672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 5051672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 5052672756aaSJamie Gritton jsf++) { 5053672756aaSJamie Gritton f = pr->pr_flags & (jsf->disable | jsf->new); 5054672756aaSJamie Gritton db_printf(" %-16s= %s\n", jsf->name, 5055672756aaSJamie Gritton (f != 0 && f == jsf->disable) ? "disable" 5056672756aaSJamie Gritton : (f == jsf->new) ? "new" 50577cbf7213SJamie Gritton : "inherit"); 50587cbf7213SJamie Gritton } 5059fe0518e9SBjoern A. Zeeb db_printf(" allow = 0x%x", pr->pr_allow); 5060672756aaSJamie Gritton for (bf = pr_flag_allow; 50615d58f959SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 50625d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 5063672756aaSJamie Gritton bf++) 5064672756aaSJamie Gritton if (pr->pr_allow & bf->flag) 5065672756aaSJamie Gritton db_printf(" %s", bf->name); 5066b38ff370SJamie Gritton db_printf("\n"); 50670304c731SJamie Gritton db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs); 5068c1f19219SJamie Gritton db_printf(" host.hostname = %s\n", pr->pr_hostname); 5069c1f19219SJamie Gritton db_printf(" host.domainname = %s\n", pr->pr_domainname); 5070c1f19219SJamie Gritton db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid); 507176ca6f88SJamie Gritton db_printf(" host.hostid = %lu\n", pr->pr_hostid); 5072413628a7SBjoern A. Zeeb #ifdef INET 5073500f82d6SZhenlei Huang if ((pip = pr->pr_addrs[PR_INET]) != NULL) { 5074500f82d6SZhenlei Huang db_printf(" ip4s = %d\n", pip->ips); 5075500f82d6SZhenlei Huang for (ii = 0; ii < pip->ips; ii++) 5076b38ff370SJamie Gritton db_printf(" %s %s\n", 5077fe0518e9SBjoern A. Zeeb ii == 0 ? "ip4.addr =" : " ", 5078eb8dcdeaSGleb Smirnoff inet_ntoa_r( 5079500f82d6SZhenlei Huang *(const struct in_addr *)PR_IP(pip, PR_INET, ii), 5080eb8dcdeaSGleb Smirnoff ip4buf)); 5081eb8dcdeaSGleb Smirnoff } 5082413628a7SBjoern A. Zeeb #endif 5083413628a7SBjoern A. Zeeb #ifdef INET6 5084500f82d6SZhenlei Huang if ((pip = pr->pr_addrs[PR_INET6]) != NULL) { 5085500f82d6SZhenlei Huang db_printf(" ip6s = %d\n", pip->ips); 5086500f82d6SZhenlei Huang for (ii = 0; ii < pip->ips; ii++) 5087b38ff370SJamie Gritton db_printf(" %s %s\n", 5088fe0518e9SBjoern A. Zeeb ii == 0 ? "ip6.addr =" : " ", 5089eb8dcdeaSGleb Smirnoff ip6_sprintf(ip6buf, 5090500f82d6SZhenlei Huang (const struct in6_addr *)PR_IP(pip, PR_INET6, ii))); 5091eb8dcdeaSGleb Smirnoff } 5092b38ff370SJamie Gritton #endif 5093b38ff370SJamie Gritton } 5094b38ff370SJamie Gritton 5095b38ff370SJamie Gritton DB_SHOW_COMMAND(prison, db_show_prison_command) 5096b38ff370SJamie Gritton { 5097b38ff370SJamie Gritton struct prison *pr; 5098b38ff370SJamie Gritton 5099b38ff370SJamie Gritton if (!have_addr) { 51000304c731SJamie Gritton /* 51010304c731SJamie Gritton * Show all prisons in the list, and prison0 which is not 51020304c731SJamie Gritton * listed. 51030304c731SJamie Gritton */ 51040304c731SJamie Gritton db_show_prison(&prison0); 51050304c731SJamie Gritton if (!db_pager_quit) { 5106b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 5107b38ff370SJamie Gritton db_show_prison(pr); 5108413628a7SBjoern A. Zeeb if (db_pager_quit) 5109413628a7SBjoern A. Zeeb break; 5110413628a7SBjoern A. Zeeb } 51110304c731SJamie Gritton } 5112b38ff370SJamie Gritton return; 5113413628a7SBjoern A. Zeeb } 5114b38ff370SJamie Gritton 51150304c731SJamie Gritton if (addr == 0) 51160304c731SJamie Gritton pr = &prison0; 51170304c731SJamie Gritton else { 5118b38ff370SJamie Gritton /* Look for a prison with the ID and with references. */ 5119b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 5120b38ff370SJamie Gritton if (pr->pr_id == addr && pr->pr_ref > 0) 5121b38ff370SJamie Gritton break; 5122b38ff370SJamie Gritton if (pr == NULL) 5123b38ff370SJamie Gritton /* Look again, without requiring a reference. */ 5124b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 5125b38ff370SJamie Gritton if (pr->pr_id == addr) 5126b38ff370SJamie Gritton break; 5127b38ff370SJamie Gritton if (pr == NULL) 5128b38ff370SJamie Gritton /* Assume address points to a valid prison. */ 5129b38ff370SJamie Gritton pr = (struct prison *)addr; 51300304c731SJamie Gritton } 5131b38ff370SJamie Gritton db_show_prison(pr); 5132b38ff370SJamie Gritton } 5133b38ff370SJamie Gritton 5134413628a7SBjoern A. Zeeb #endif /* DDB */ 5135