19454b2d8SWarner Losh /*- 28a36da99SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 38a36da99SPedro F. Giffuni * 4413628a7SBjoern A. Zeeb * Copyright (c) 1999 Poul-Henning Kamp. 5413628a7SBjoern A. Zeeb * Copyright (c) 2008 Bjoern A. Zeeb. 6b38ff370SJamie Gritton * Copyright (c) 2009 James Gritton. 7413628a7SBjoern A. Zeeb * All rights reserved. 8b9f0b66cSBjoern A. Zeeb * 9b9f0b66cSBjoern A. Zeeb * Redistribution and use in source and binary forms, with or without 10b9f0b66cSBjoern A. Zeeb * modification, are permitted provided that the following conditions 11b9f0b66cSBjoern A. Zeeb * are met: 12b9f0b66cSBjoern A. Zeeb * 1. Redistributions of source code must retain the above copyright 13b9f0b66cSBjoern A. Zeeb * notice, this list of conditions and the following disclaimer. 14b9f0b66cSBjoern A. Zeeb * 2. Redistributions in binary form must reproduce the above copyright 15b9f0b66cSBjoern A. Zeeb * notice, this list of conditions and the following disclaimer in the 16b9f0b66cSBjoern A. Zeeb * documentation and/or other materials provided with the distribution. 17b9f0b66cSBjoern A. Zeeb * 18b9f0b66cSBjoern A. Zeeb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19b9f0b66cSBjoern A. Zeeb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20b9f0b66cSBjoern A. Zeeb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21b9f0b66cSBjoern A. Zeeb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22b9f0b66cSBjoern A. Zeeb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23b9f0b66cSBjoern A. Zeeb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24b9f0b66cSBjoern A. Zeeb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25b9f0b66cSBjoern A. Zeeb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26b9f0b66cSBjoern A. Zeeb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27b9f0b66cSBjoern A. Zeeb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28b9f0b66cSBjoern A. Zeeb * SUCH DAMAGE. 2907901f22SPoul-Henning Kamp */ 3075c13541SPoul-Henning Kamp 31677b542eSDavid E. O'Brien #include <sys/cdefs.h> 32677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 33677b542eSDavid E. O'Brien 34413628a7SBjoern A. Zeeb #include "opt_ddb.h" 35413628a7SBjoern A. Zeeb #include "opt_inet.h" 36413628a7SBjoern A. Zeeb #include "opt_inet6.h" 3746e3b1cbSPawel Jakub Dawidek 3875c13541SPoul-Henning Kamp #include <sys/param.h> 3975c13541SPoul-Henning Kamp #include <sys/types.h> 4075c13541SPoul-Henning Kamp #include <sys/kernel.h> 4175c13541SPoul-Henning Kamp #include <sys/systm.h> 4275c13541SPoul-Henning Kamp #include <sys/errno.h> 4375c13541SPoul-Henning Kamp #include <sys/sysproto.h> 4475c13541SPoul-Henning Kamp #include <sys/malloc.h> 450304c731SJamie Gritton #include <sys/osd.h> 46800c9408SRobert Watson #include <sys/priv.h> 4775c13541SPoul-Henning Kamp #include <sys/proc.h> 48eb8dcdeaSGleb Smirnoff #include <sys/epoch.h> 49b3059e09SRobert Watson #include <sys/taskqueue.h> 5057b4252eSKonstantin Belousov #include <sys/fcntl.h> 5175c13541SPoul-Henning Kamp #include <sys/jail.h> 52c3188289SKyle Evans #include <sys/linker.h> 5301137630SRobert Watson #include <sys/lock.h> 5401137630SRobert Watson #include <sys/mutex.h> 55097055e2SEdward Tomasz Napierala #include <sys/racct.h> 56f87beb93SAndriy Gapon #include <sys/rctl.h> 57a7ad07bfSEdward Tomasz Napierala #include <sys/refcount.h> 58dc68a633SPawel Jakub Dawidek #include <sys/sx.h> 5976ca6f88SJamie Gritton #include <sys/sysent.h> 60fd7a8150SMike Barcroft #include <sys/namei.h> 61820a0de9SPawel Jakub Dawidek #include <sys/mount.h> 62fd7a8150SMike Barcroft #include <sys/queue.h> 6375c13541SPoul-Henning Kamp #include <sys/socket.h> 64fd7a8150SMike Barcroft #include <sys/syscallsubr.h> 6583f1e257SRobert Watson #include <sys/sysctl.h> 66c3188289SKyle Evans #include <sys/uuid.h> 67fd7a8150SMike Barcroft #include <sys/vnode.h> 68530c0060SRobert Watson 6975c13541SPoul-Henning Kamp #include <net/if.h> 70530c0060SRobert Watson #include <net/vnet.h> 71530c0060SRobert Watson 7275c13541SPoul-Henning Kamp #include <netinet/in.h> 73530c0060SRobert Watson 74413628a7SBjoern A. Zeeb #ifdef DDB 75413628a7SBjoern A. Zeeb #include <ddb/ddb.h> 76413628a7SBjoern A. Zeeb #endif /* DDB */ 7775c13541SPoul-Henning Kamp 78aed55708SRobert Watson #include <security/mac/mac_framework.h> 79aed55708SRobert Watson 80c3188289SKyle Evans #define PRISON0_HOSTUUID_MODULE "hostuuid" 818986e3a0SJamie Gritton 8275c13541SPoul-Henning Kamp MALLOC_DEFINE(M_PRISON, "prison", "Prison structures"); 83d745c852SEd Schouten static MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures"); 8475c13541SPoul-Henning Kamp 85592bcae8SBjoern A. Zeeb /* Keep struct prison prison0 and some code in kern_jail_set() readable. */ 86592bcae8SBjoern A. Zeeb #ifdef INET 87592bcae8SBjoern A. Zeeb #ifdef INET6 88592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL|PR_IP6_SADDRSEL 89592bcae8SBjoern A. Zeeb #else 90592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL 91592bcae8SBjoern A. Zeeb #endif 92592bcae8SBjoern A. Zeeb #else /* !INET */ 93592bcae8SBjoern A. Zeeb #ifdef INET6 94592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL PR_IP6_SADDRSEL 95592bcae8SBjoern A. Zeeb #else 96592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL 0 97592bcae8SBjoern A. Zeeb #endif 98592bcae8SBjoern A. Zeeb #endif 99592bcae8SBjoern A. Zeeb 1000304c731SJamie Gritton /* prison0 describes what is "real" about the system. */ 1010304c731SJamie Gritton struct prison prison0 = { 1020304c731SJamie Gritton .pr_id = 0, 1030304c731SJamie Gritton .pr_name = "0", 1040304c731SJamie Gritton .pr_ref = 1, 1050304c731SJamie Gritton .pr_uref = 1, 1060304c731SJamie Gritton .pr_path = "/", 1070304c731SJamie Gritton .pr_securelevel = -1, 1080cc207a6SMartin Matuska .pr_devfs_rsnum = 0, 1091158508aSJamie Gritton .pr_state = PRISON_STATE_ALIVE, 110b97457e2SJamie Gritton .pr_childmax = JAIL_MAX, 1118986e3a0SJamie Gritton .pr_hostuuid = DEFAULT_HOSTUUID, 11213e403fdSAntoine Brodin .pr_children = LIST_HEAD_INITIALIZER(prison0.pr_children), 113eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE 114592bcae8SBjoern A. Zeeb .pr_flags = PR_HOST|PR_VNET|_PR_IP_SADDRSEL, 115eb79e1c7SBjoern A. Zeeb #else 116592bcae8SBjoern A. Zeeb .pr_flags = PR_HOST|_PR_IP_SADDRSEL, 117eb79e1c7SBjoern A. Zeeb #endif 1180e5c6bd4SJamie Gritton .pr_allow = PR_ALLOW_ALL_STATIC, 1190304c731SJamie Gritton }; 1200304c731SJamie Gritton MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF); 12183f1e257SRobert Watson 122672756aaSJamie Gritton struct bool_flags { 123672756aaSJamie Gritton const char *name; 124672756aaSJamie Gritton const char *noname; 1255d58f959SJamie Gritton volatile u_int flag; 126672756aaSJamie Gritton }; 127672756aaSJamie Gritton struct jailsys_flags { 128672756aaSJamie Gritton const char *name; 129672756aaSJamie Gritton unsigned disable; 130672756aaSJamie Gritton unsigned new; 131672756aaSJamie Gritton }; 132672756aaSJamie Gritton 133a7ad07bfSEdward Tomasz Napierala /* allprison, allprison_racct and lastprid are protected by allprison_lock. */ 134dc68a633SPawel Jakub Dawidek struct sx allprison_lock; 135b38ff370SJamie Gritton SX_SYSINIT(allprison_lock, &allprison_lock, "allprison"); 136b38ff370SJamie Gritton struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison); 137a7ad07bfSEdward Tomasz Napierala LIST_HEAD(, prison_racct) allprison_racct; 1382110d913SXin LI int lastprid = 0; 139fd7a8150SMike Barcroft 1407de883c8SJamie Gritton static int get_next_prid(struct prison **insprp); 141f7496dcaSJamie Gritton static int do_jail_attach(struct thread *td, struct prison *pr, int drflags); 142b3059e09SRobert Watson static void prison_complete(void *context, int pending); 143b38ff370SJamie Gritton static void prison_deref(struct prison *pr, int flags); 144c861373bSJamie Gritton static void prison_deref_kill(struct prison *pr, struct prisonlist *freeprison); 145f7496dcaSJamie Gritton static int prison_lock_xlock(struct prison *pr, int flags); 146*a9f7455cSJamie Gritton static void prison_cleanup(struct prison *pr); 147f7496dcaSJamie Gritton static void prison_free_not_last(struct prison *pr); 148c861373bSJamie Gritton static void prison_proc_free_not_last(struct prison *pr); 1490fe74ae6SJamie Gritton static void prison_set_allow_locked(struct prison *pr, unsigned flag, 1500fe74ae6SJamie Gritton int enable); 1510304c731SJamie Gritton static char *prison_path(struct prison *pr1, struct prison *pr2); 152a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 153a7ad07bfSEdward Tomasz Napierala static void prison_racct_attach(struct prison *pr); 154c34bbd2aSEdward Tomasz Napierala static void prison_racct_modify(struct prison *pr); 155a7ad07bfSEdward Tomasz Napierala static void prison_racct_detach(struct prison *pr); 156a7ad07bfSEdward Tomasz Napierala #endif 157fd7a8150SMike Barcroft 158b38ff370SJamie Gritton /* Flags for prison_deref */ 1592a4b2251SJamie Gritton #define PD_DEREF 0x01 /* Decrement pr_ref */ 1602a4b2251SJamie Gritton #define PD_DEUREF 0x02 /* Decrement pr_uref */ 161c861373bSJamie Gritton #define PD_KILL 0x04 /* Remove jail, kill processes, etc */ 162c861373bSJamie Gritton #define PD_LOCKED 0x10 /* pr_mtx is held */ 163c861373bSJamie Gritton #define PD_LIST_SLOCKED 0x20 /* allprison_lock is held shared */ 164c861373bSJamie Gritton #define PD_LIST_XLOCKED 0x40 /* allprison_lock is held exclusive */ 165589e4c1dSJamie Gritton #define PD_OP_FLAGS 0x07 /* Operation flags */ 166589e4c1dSJamie Gritton #define PD_LOCK_FLAGS 0x70 /* Lock status flags */ 167fd7a8150SMike Barcroft 1680304c731SJamie Gritton /* 1695cc70397SBjoern A. Zeeb * Parameter names corresponding to PR_* flag values. Size values are for kvm 1705cc70397SBjoern A. Zeeb * as we cannot figure out the size of a sparse array, or an array without a 1715cc70397SBjoern A. Zeeb * terminating entry. 1720304c731SJamie Gritton */ 173672756aaSJamie Gritton static struct bool_flags pr_flag_bool[] = { 174672756aaSJamie Gritton {"persist", "nopersist", PR_PERSIST}, 175592bcae8SBjoern A. Zeeb #ifdef INET 176672756aaSJamie Gritton {"ip4.saddrsel", "ip4.nosaddrsel", PR_IP4_SADDRSEL}, 177592bcae8SBjoern A. Zeeb #endif 178592bcae8SBjoern A. Zeeb #ifdef INET6 179672756aaSJamie Gritton {"ip6.saddrsel", "ip6.nosaddrsel", PR_IP6_SADDRSEL}, 180592bcae8SBjoern A. Zeeb #endif 1810304c731SJamie Gritton }; 182672756aaSJamie Gritton const size_t pr_flag_bool_size = sizeof(pr_flag_bool); 1830304c731SJamie Gritton 184672756aaSJamie Gritton static struct jailsys_flags pr_flag_jailsys[] = { 1857cbf7213SJamie Gritton {"host", 0, PR_HOST}, 1867cbf7213SJamie Gritton #ifdef VIMAGE 1877cbf7213SJamie Gritton {"vnet", 0, PR_VNET}, 1887cbf7213SJamie Gritton #endif 1890304c731SJamie Gritton #ifdef INET 1906a3f2779SJamie Gritton {"ip4", PR_IP4_USER, PR_IP4_USER}, 1910304c731SJamie Gritton #endif 1920304c731SJamie Gritton #ifdef INET6 1936a3f2779SJamie Gritton {"ip6", PR_IP6_USER, PR_IP6_USER}, 194679e1390SJamie Gritton #endif 1950304c731SJamie Gritton }; 1965cc70397SBjoern A. Zeeb const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys); 1970304c731SJamie Gritton 1985d58f959SJamie Gritton /* 1995d58f959SJamie Gritton * Make this array full-size so dynamic parameters can be added. 2005d58f959SJamie Gritton * It is protected by prison0.mtx, but lockless reading is allowed 2015d58f959SJamie Gritton * with an atomic check of the flag values. 2025d58f959SJamie Gritton */ 2030e5c6bd4SJamie Gritton static struct bool_flags pr_flag_allow[NBBY * NBPW] = { 204672756aaSJamie Gritton {"allow.set_hostname", "allow.noset_hostname", PR_ALLOW_SET_HOSTNAME}, 205672756aaSJamie Gritton {"allow.sysvipc", "allow.nosysvipc", PR_ALLOW_SYSVIPC}, 206672756aaSJamie Gritton {"allow.raw_sockets", "allow.noraw_sockets", PR_ALLOW_RAW_SOCKETS}, 207672756aaSJamie Gritton {"allow.chflags", "allow.nochflags", PR_ALLOW_CHFLAGS}, 208672756aaSJamie Gritton {"allow.mount", "allow.nomount", PR_ALLOW_MOUNT}, 209672756aaSJamie Gritton {"allow.quotas", "allow.noquotas", PR_ALLOW_QUOTAS}, 210672756aaSJamie Gritton {"allow.socket_af", "allow.nosocket_af", PR_ALLOW_SOCKET_AF}, 211ccd6ac9fSAntoine Brodin {"allow.mlock", "allow.nomlock", PR_ALLOW_MLOCK}, 212672756aaSJamie Gritton {"allow.reserved_ports", "allow.noreserved_ports", 213672756aaSJamie Gritton PR_ALLOW_RESERVED_PORTS}, 214b19d66fdSJamie Gritton {"allow.read_msgbuf", "allow.noread_msgbuf", PR_ALLOW_READ_MSGBUF}, 215b3079544SJamie Gritton {"allow.unprivileged_proc_debug", "allow.nounprivileged_proc_debug", 216b3079544SJamie Gritton PR_ALLOW_UNPRIV_DEBUG}, 21705e1e482SMariusz Zaborski {"allow.suser", "allow.nosuser", PR_ALLOW_SUSER}, 2180304c731SJamie Gritton }; 2195d58f959SJamie Gritton static unsigned pr_allow_all = PR_ALLOW_ALL_STATIC; 220672756aaSJamie Gritton const size_t pr_flag_allow_size = sizeof(pr_flag_allow); 2210304c731SJamie Gritton 222b3079544SJamie Gritton #define JAIL_DEFAULT_ALLOW (PR_ALLOW_SET_HOSTNAME | \ 223b3079544SJamie Gritton PR_ALLOW_RESERVED_PORTS | \ 22405e1e482SMariusz Zaborski PR_ALLOW_UNPRIV_DEBUG | \ 22505e1e482SMariusz Zaborski PR_ALLOW_SUSER) 22642bebd82SJamie Gritton #define JAIL_DEFAULT_ENFORCE_STATFS 2 227bf3db8aaSMartin Matuska #define JAIL_DEFAULT_DEVFS_RSNUM 0 2280304c731SJamie Gritton static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW; 22942bebd82SJamie Gritton static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 2300cc207a6SMartin Matuska static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM; 2310304c731SJamie Gritton #if defined(INET) || defined(INET6) 232e92e0574SJamie Gritton static unsigned jail_max_af_ips = 255; 2330304c731SJamie Gritton #endif 2340304c731SJamie Gritton 235b96bd95bSIan Lepore /* 236b96bd95bSIan Lepore * Initialize the parts of prison0 that can't be static-initialized with 237b96bd95bSIan Lepore * constants. This is called from proc0_init() after creating thread0 cpuset. 238b96bd95bSIan Lepore */ 239b96bd95bSIan Lepore void 240b96bd95bSIan Lepore prison0_init(void) 241b96bd95bSIan Lepore { 242c3188289SKyle Evans uint8_t *file, *data; 243c3188289SKyle Evans size_t size; 244d2ef3774SJessica Clarke char buf[sizeof(prison0.pr_hostuuid)]; 245d2ef3774SJessica Clarke bool valid; 246b96bd95bSIan Lepore 247b96bd95bSIan Lepore prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset); 248b96bd95bSIan Lepore prison0.pr_osreldate = osreldate; 249b96bd95bSIan Lepore strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease)); 250c3188289SKyle Evans 251c3188289SKyle Evans /* If we have a preloaded hostuuid, use it. */ 252c3188289SKyle Evans file = preload_search_by_type(PRISON0_HOSTUUID_MODULE); 253c3188289SKyle Evans if (file != NULL) { 254c3188289SKyle Evans data = preload_fetch_addr(file); 255c3188289SKyle Evans size = preload_fetch_size(file); 256c3188289SKyle Evans if (data != NULL) { 257c3188289SKyle Evans /* 258c3188289SKyle Evans * The preloaded data may include trailing whitespace, almost 259c3188289SKyle Evans * certainly a newline; skip over any whitespace or 260c3188289SKyle Evans * non-printable characters to be safe. 261c3188289SKyle Evans */ 262c3188289SKyle Evans while (size > 0 && data[size - 1] <= 0x20) { 263b6be9566SColin Percival size--; 264c3188289SKyle Evans } 265d2ef3774SJessica Clarke 266d2ef3774SJessica Clarke valid = false; 267d2ef3774SJessica Clarke 268d2ef3774SJessica Clarke /* 269d2ef3774SJessica Clarke * Not NUL-terminated when passed from loader, but 270d2ef3774SJessica Clarke * validate_uuid requires that due to using sscanf (as 271d2ef3774SJessica Clarke * does the subsequent strlcpy, since it still reads 272d2ef3774SJessica Clarke * past the given size to return the true length); 273d2ef3774SJessica Clarke * bounce to a temporary buffer to fix. 274d2ef3774SJessica Clarke */ 275d2ef3774SJessica Clarke if (size >= sizeof(buf)) 276d2ef3774SJessica Clarke goto done; 277d2ef3774SJessica Clarke 278d2ef3774SJessica Clarke memcpy(buf, data, size); 279d2ef3774SJessica Clarke buf[size] = '\0'; 280d2ef3774SJessica Clarke 281d2ef3774SJessica Clarke if (validate_uuid(buf, size, NULL, 0) != 0) 282d2ef3774SJessica Clarke goto done; 283d2ef3774SJessica Clarke 284d2ef3774SJessica Clarke valid = true; 285d2ef3774SJessica Clarke (void)strlcpy(prison0.pr_hostuuid, buf, 286d2ef3774SJessica Clarke sizeof(prison0.pr_hostuuid)); 287d2ef3774SJessica Clarke 288d2ef3774SJessica Clarke done: 289d2ef3774SJessica Clarke if (bootverbose && !valid) { 290330f110bSColin Percival printf("hostuuid: preload data malformed: '%.*s'\n", 291330f110bSColin Percival (int)size, data); 292c3188289SKyle Evans } 293c3188289SKyle Evans } 294c3188289SKyle Evans } 295c3188289SKyle Evans if (bootverbose) 296c3188289SKyle Evans printf("hostuuid: using %s\n", prison0.pr_hostuuid); 297b96bd95bSIan Lepore } 298b96bd95bSIan Lepore 299116734c4SMatthew Dillon /* 3009ddb7954SMike Barcroft * struct jail_args { 3019ddb7954SMike Barcroft * struct jail *jail; 3029ddb7954SMike Barcroft * }; 303116734c4SMatthew Dillon */ 30475c13541SPoul-Henning Kamp int 305c542c43eSJamie Gritton sys_jail(struct thread *td, struct jail_args *uap) 30675c13541SPoul-Henning Kamp { 307413628a7SBjoern A. Zeeb uint32_t version; 308413628a7SBjoern A. Zeeb int error; 3090304c731SJamie Gritton struct jail j; 310413628a7SBjoern A. Zeeb 311413628a7SBjoern A. Zeeb error = copyin(uap->jail, &version, sizeof(uint32_t)); 312413628a7SBjoern A. Zeeb if (error) 313413628a7SBjoern A. Zeeb return (error); 314413628a7SBjoern A. Zeeb 315413628a7SBjoern A. Zeeb switch (version) { 316413628a7SBjoern A. Zeeb case 0: 317413628a7SBjoern A. Zeeb { 318413628a7SBjoern A. Zeeb struct jail_v0 j0; 319413628a7SBjoern A. Zeeb 3200304c731SJamie Gritton /* FreeBSD single IPv4 jails. */ 3210304c731SJamie Gritton bzero(&j, sizeof(struct jail)); 322413628a7SBjoern A. Zeeb error = copyin(uap->jail, &j0, sizeof(struct jail_v0)); 323413628a7SBjoern A. Zeeb if (error) 324413628a7SBjoern A. Zeeb return (error); 3250304c731SJamie Gritton j.version = j0.version; 3260304c731SJamie Gritton j.path = j0.path; 3270304c731SJamie Gritton j.hostname = j0.hostname; 328b5019bc4SPeter Wemm j.ip4s = htonl(j0.ip_number); /* jail_v0 is host order */ 329413628a7SBjoern A. Zeeb break; 330413628a7SBjoern A. Zeeb } 331413628a7SBjoern A. Zeeb 332413628a7SBjoern A. Zeeb case 1: 333413628a7SBjoern A. Zeeb /* 334413628a7SBjoern A. Zeeb * Version 1 was used by multi-IPv4 jail implementations 335413628a7SBjoern A. Zeeb * that never made it into the official kernel. 336413628a7SBjoern A. Zeeb */ 337413628a7SBjoern A. Zeeb return (EINVAL); 338413628a7SBjoern A. Zeeb 339413628a7SBjoern A. Zeeb case 2: /* JAIL_API_VERSION */ 340413628a7SBjoern A. Zeeb /* FreeBSD multi-IPv4/IPv6,noIP jails. */ 341413628a7SBjoern A. Zeeb error = copyin(uap->jail, &j, sizeof(struct jail)); 342413628a7SBjoern A. Zeeb if (error) 343413628a7SBjoern A. Zeeb return (error); 3440304c731SJamie Gritton break; 3450304c731SJamie Gritton 3460304c731SJamie Gritton default: 3470304c731SJamie Gritton /* Sci-Fi jails are not supported, sorry. */ 3480304c731SJamie Gritton return (EINVAL); 3490304c731SJamie Gritton } 350c542c43eSJamie Gritton return (kern_jail(td, &j)); 3510304c731SJamie Gritton } 3520304c731SJamie Gritton 3530304c731SJamie Gritton int 354c542c43eSJamie Gritton kern_jail(struct thread *td, struct jail *j) 3550304c731SJamie Gritton { 356c542c43eSJamie Gritton struct iovec optiov[2 * (4 + nitems(pr_flag_allow) 357e92e0574SJamie Gritton #ifdef INET 358e92e0574SJamie Gritton + 1 359e92e0574SJamie Gritton #endif 360e92e0574SJamie Gritton #ifdef INET6 361e92e0574SJamie Gritton + 1 362e92e0574SJamie Gritton #endif 363e92e0574SJamie Gritton )]; 3640304c731SJamie Gritton struct uio opt; 3650304c731SJamie Gritton char *u_path, *u_hostname, *u_name; 366672756aaSJamie Gritton struct bool_flags *bf; 3670304c731SJamie Gritton #ifdef INET 368e92e0574SJamie Gritton uint32_t ip4s; 3690304c731SJamie Gritton struct in_addr *u_ip4; 3700304c731SJamie Gritton #endif 3710304c731SJamie Gritton #ifdef INET6 3720304c731SJamie Gritton struct in6_addr *u_ip6; 3730304c731SJamie Gritton #endif 3740304c731SJamie Gritton size_t tmplen; 375c542c43eSJamie Gritton int error, enforce_statfs; 3760304c731SJamie Gritton 3770304c731SJamie Gritton bzero(&optiov, sizeof(optiov)); 3780304c731SJamie Gritton opt.uio_iov = optiov; 3790304c731SJamie Gritton opt.uio_iovcnt = 0; 3800304c731SJamie Gritton opt.uio_offset = -1; 3810304c731SJamie Gritton opt.uio_resid = -1; 3820304c731SJamie Gritton opt.uio_segflg = UIO_SYSSPACE; 3830304c731SJamie Gritton opt.uio_rw = UIO_READ; 3840304c731SJamie Gritton opt.uio_td = td; 3850304c731SJamie Gritton 3860304c731SJamie Gritton /* Set permissions for top-level jails from sysctls. */ 3870304c731SJamie Gritton if (!jailed(td->td_ucred)) { 388672756aaSJamie Gritton for (bf = pr_flag_allow; 3890e5c6bd4SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 3905d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 391672756aaSJamie Gritton bf++) { 392672756aaSJamie Gritton optiov[opt.uio_iovcnt].iov_base = __DECONST(char *, 393672756aaSJamie Gritton (jail_default_allow & bf->flag) 394672756aaSJamie Gritton ? bf->name : bf->noname); 3950304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = 3960304c731SJamie Gritton strlen(optiov[opt.uio_iovcnt].iov_base) + 1; 3970304c731SJamie Gritton opt.uio_iovcnt += 2; 3980304c731SJamie Gritton } 3990304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "enforce_statfs"; 4000304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs"); 4010304c731SJamie Gritton opt.uio_iovcnt++; 4020304c731SJamie Gritton enforce_statfs = jail_default_enforce_statfs; 4030304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = &enforce_statfs; 4040304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs); 4050304c731SJamie Gritton opt.uio_iovcnt++; 4060304c731SJamie Gritton } 4070304c731SJamie Gritton 408b38ff370SJamie Gritton tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN; 409b38ff370SJamie Gritton #ifdef INET 4100304c731SJamie Gritton ip4s = (j->version == 0) ? 1 : j->ip4s; 4110304c731SJamie Gritton if (ip4s > jail_max_af_ips) 412b38ff370SJamie Gritton return (EINVAL); 4130304c731SJamie Gritton tmplen += ip4s * sizeof(struct in_addr); 414b38ff370SJamie Gritton #else 4150304c731SJamie Gritton if (j->ip4s > 0) 416b38ff370SJamie Gritton return (EINVAL); 417b38ff370SJamie Gritton #endif 418b38ff370SJamie Gritton #ifdef INET6 4190304c731SJamie Gritton if (j->ip6s > jail_max_af_ips) 420b38ff370SJamie Gritton return (EINVAL); 4210304c731SJamie Gritton tmplen += j->ip6s * sizeof(struct in6_addr); 422b38ff370SJamie Gritton #else 4230304c731SJamie Gritton if (j->ip6s > 0) 424b38ff370SJamie Gritton return (EINVAL); 425b38ff370SJamie Gritton #endif 426b38ff370SJamie Gritton u_path = malloc(tmplen, M_TEMP, M_WAITOK); 427b38ff370SJamie Gritton u_hostname = u_path + MAXPATHLEN; 428b38ff370SJamie Gritton u_name = u_hostname + MAXHOSTNAMELEN; 429b38ff370SJamie Gritton #ifdef INET 430b38ff370SJamie Gritton u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN); 431b38ff370SJamie Gritton #endif 432b38ff370SJamie Gritton #ifdef INET6 433b38ff370SJamie Gritton #ifdef INET 4340304c731SJamie Gritton u_ip6 = (struct in6_addr *)(u_ip4 + ip4s); 435b38ff370SJamie Gritton #else 436b38ff370SJamie Gritton u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN); 437b38ff370SJamie Gritton #endif 438b38ff370SJamie Gritton #endif 4390304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "path"; 4400304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("path"); 4410304c731SJamie Gritton opt.uio_iovcnt++; 4420304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_path; 4430304c731SJamie Gritton error = copyinstr(j->path, u_path, MAXPATHLEN, 4440304c731SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 445b38ff370SJamie Gritton if (error) { 446b38ff370SJamie Gritton free(u_path, M_TEMP); 447b38ff370SJamie Gritton return (error); 448b38ff370SJamie Gritton } 4490304c731SJamie Gritton opt.uio_iovcnt++; 4500304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "host.hostname"; 4510304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname"); 4520304c731SJamie Gritton opt.uio_iovcnt++; 4530304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_hostname; 4540304c731SJamie Gritton error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN, 4550304c731SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 456b38ff370SJamie Gritton if (error) { 457b38ff370SJamie Gritton free(u_path, M_TEMP); 458b38ff370SJamie Gritton return (error); 459b38ff370SJamie Gritton } 4600304c731SJamie Gritton opt.uio_iovcnt++; 4610304c731SJamie Gritton if (j->jailname != NULL) { 462b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "name"; 463b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("name"); 464b38ff370SJamie Gritton opt.uio_iovcnt++; 465b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_name; 4660304c731SJamie Gritton error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN, 467b38ff370SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 468b38ff370SJamie Gritton if (error) { 469b38ff370SJamie Gritton free(u_path, M_TEMP); 470b38ff370SJamie Gritton return (error); 471b38ff370SJamie Gritton } 472b38ff370SJamie Gritton opt.uio_iovcnt++; 473b38ff370SJamie Gritton } 474b38ff370SJamie Gritton #ifdef INET 475b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "ip4.addr"; 476b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr"); 477b38ff370SJamie Gritton opt.uio_iovcnt++; 478b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_ip4; 4790304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr); 4800304c731SJamie Gritton if (j->version == 0) 4810304c731SJamie Gritton u_ip4->s_addr = j->ip4s; 4820304c731SJamie Gritton else { 4830304c731SJamie Gritton error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len); 484b38ff370SJamie Gritton if (error) { 485b38ff370SJamie Gritton free(u_path, M_TEMP); 486b38ff370SJamie Gritton return (error); 487b38ff370SJamie Gritton } 4880304c731SJamie Gritton } 489b38ff370SJamie Gritton opt.uio_iovcnt++; 490b38ff370SJamie Gritton #endif 491b38ff370SJamie Gritton #ifdef INET6 492b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "ip6.addr"; 493b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr"); 494b38ff370SJamie Gritton opt.uio_iovcnt++; 495b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_ip6; 4960304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr); 4970304c731SJamie Gritton error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len); 498b38ff370SJamie Gritton if (error) { 499b38ff370SJamie Gritton free(u_path, M_TEMP); 500b38ff370SJamie Gritton return (error); 501b38ff370SJamie Gritton } 502b38ff370SJamie Gritton opt.uio_iovcnt++; 503b38ff370SJamie Gritton #endif 50402abd400SPedro F. Giffuni KASSERT(opt.uio_iovcnt <= nitems(optiov), 5050304c731SJamie Gritton ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt)); 506b38ff370SJamie Gritton error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH); 507b38ff370SJamie Gritton free(u_path, M_TEMP); 508b38ff370SJamie Gritton return (error); 509b38ff370SJamie Gritton } 510b38ff370SJamie Gritton 511b38ff370SJamie Gritton /* 512b38ff370SJamie Gritton * struct jail_set_args { 513b38ff370SJamie Gritton * struct iovec *iovp; 514b38ff370SJamie Gritton * unsigned int iovcnt; 515b38ff370SJamie Gritton * int flags; 516b38ff370SJamie Gritton * }; 517b38ff370SJamie Gritton */ 518b38ff370SJamie Gritton int 5198451d0ddSKip Macy sys_jail_set(struct thread *td, struct jail_set_args *uap) 520b38ff370SJamie Gritton { 521b38ff370SJamie Gritton struct uio *auio; 522b38ff370SJamie Gritton int error; 523b38ff370SJamie Gritton 524b38ff370SJamie Gritton /* Check that we have an even number of iovecs. */ 525b38ff370SJamie Gritton if (uap->iovcnt & 1) 526b38ff370SJamie Gritton return (EINVAL); 527b38ff370SJamie Gritton 528b38ff370SJamie Gritton error = copyinuio(uap->iovp, uap->iovcnt, &auio); 529b38ff370SJamie Gritton if (error) 530b38ff370SJamie Gritton return (error); 531b38ff370SJamie Gritton error = kern_jail_set(td, auio, uap->flags); 532b38ff370SJamie Gritton free(auio, M_IOV); 533b38ff370SJamie Gritton return (error); 534413628a7SBjoern A. Zeeb } 535413628a7SBjoern A. Zeeb 536eb8dcdeaSGleb Smirnoff #if defined(INET) || defined(INET6) 537eb8dcdeaSGleb Smirnoff typedef int prison_addr_cmp_t(const void *, const void *); 538eb8dcdeaSGleb Smirnoff typedef bool prison_addr_valid_t(const void *); 539eb8dcdeaSGleb Smirnoff static const struct pr_family { 540eb8dcdeaSGleb Smirnoff size_t size; 541eb8dcdeaSGleb Smirnoff prison_addr_cmp_t *cmp; 542eb8dcdeaSGleb Smirnoff prison_addr_valid_t *valid; 543eb8dcdeaSGleb Smirnoff int ip_flag; 544eb8dcdeaSGleb Smirnoff } pr_families[PR_FAMILY_MAX] = { 545eb8dcdeaSGleb Smirnoff #ifdef INET 546eb8dcdeaSGleb Smirnoff [PR_INET] = { 547eb8dcdeaSGleb Smirnoff .size = sizeof(struct in_addr), 548eb8dcdeaSGleb Smirnoff .cmp = prison_qcmp_v4, 549eb8dcdeaSGleb Smirnoff .valid = prison_valid_v4, 550eb8dcdeaSGleb Smirnoff .ip_flag = PR_IP4_USER, 551eb8dcdeaSGleb Smirnoff }, 552eb8dcdeaSGleb Smirnoff #endif 553eb8dcdeaSGleb Smirnoff #ifdef INET6 554eb8dcdeaSGleb Smirnoff [PR_INET6] = { 555eb8dcdeaSGleb Smirnoff .size = sizeof(struct in6_addr), 556eb8dcdeaSGleb Smirnoff .cmp = prison_qcmp_v6, 557eb8dcdeaSGleb Smirnoff .valid = prison_valid_v6, 558eb8dcdeaSGleb Smirnoff .ip_flag = PR_IP6_USER, 559eb8dcdeaSGleb Smirnoff }, 560eb8dcdeaSGleb Smirnoff #endif 561eb8dcdeaSGleb Smirnoff }; 562eb8dcdeaSGleb Smirnoff 563eb8dcdeaSGleb Smirnoff /* 564eb8dcdeaSGleb Smirnoff * Network address lists (pr_addrs) allocation for jails. The addresses 565eb8dcdeaSGleb Smirnoff * are accessed locklessly by the network stack, thus need to be protected by 566eb8dcdeaSGleb Smirnoff * the network epoch. 567eb8dcdeaSGleb Smirnoff */ 568eb8dcdeaSGleb Smirnoff struct prison_ip { 569eb8dcdeaSGleb Smirnoff struct epoch_context ctx; 570eb8dcdeaSGleb Smirnoff uint32_t ips; 571eb8dcdeaSGleb Smirnoff #ifdef FUTURE_C 572eb8dcdeaSGleb Smirnoff union { 573eb8dcdeaSGleb Smirnoff struct in_addr pr_ip4[]; 574eb8dcdeaSGleb Smirnoff struct in6_addr pr_ip6[]; 575eb8dcdeaSGleb Smirnoff }; 576eb8dcdeaSGleb Smirnoff #else /* No future C :( */ 577eb8dcdeaSGleb Smirnoff #define PR_IP(pip, i) ((const char *)((pip) + 1) + pr_families[af].size * (i)) 578eb8dcdeaSGleb Smirnoff #define PR_IPD(pip, i) ((char *)((pip) + 1) + pr_families[af].size * (i)) 579eb8dcdeaSGleb Smirnoff #endif 580eb8dcdeaSGleb Smirnoff }; 581eb8dcdeaSGleb Smirnoff 582eb8dcdeaSGleb Smirnoff static struct prison_ip * 583eb8dcdeaSGleb Smirnoff prison_ip_alloc(const pr_family_t af, uint32_t cnt, int flags) 584eb8dcdeaSGleb Smirnoff { 585eb8dcdeaSGleb Smirnoff struct prison_ip *pip; 586eb8dcdeaSGleb Smirnoff 587eb8dcdeaSGleb Smirnoff pip = malloc(sizeof(struct prison_ip) + cnt * pr_families[af].size, 588eb8dcdeaSGleb Smirnoff M_PRISON, flags); 589eb8dcdeaSGleb Smirnoff if (pip != NULL) 590eb8dcdeaSGleb Smirnoff pip->ips = cnt; 591eb8dcdeaSGleb Smirnoff return (pip); 592eb8dcdeaSGleb Smirnoff } 593eb8dcdeaSGleb Smirnoff 594eb8dcdeaSGleb Smirnoff /* 595eb8dcdeaSGleb Smirnoff * Allocate and copyin user supplied address list, sorting and validating. 596eb8dcdeaSGleb Smirnoff * kern_jail_set() helper. 597eb8dcdeaSGleb Smirnoff */ 598eb8dcdeaSGleb Smirnoff static struct prison_ip * 599eb8dcdeaSGleb Smirnoff prison_ip_copyin(const pr_family_t af, void *op, uint32_t cnt) 600eb8dcdeaSGleb Smirnoff { 601eb8dcdeaSGleb Smirnoff prison_addr_cmp_t *const cmp = pr_families[af].cmp; 602eb8dcdeaSGleb Smirnoff const size_t size = pr_families[af].size; 603eb8dcdeaSGleb Smirnoff struct prison_ip *pip; 604eb8dcdeaSGleb Smirnoff 605eb8dcdeaSGleb Smirnoff pip = prison_ip_alloc(af, cnt, M_WAITOK); 606eb8dcdeaSGleb Smirnoff bcopy(op, pip + 1, cnt * size); 607eb8dcdeaSGleb Smirnoff /* 608eb8dcdeaSGleb Smirnoff * IP addresses are all sorted but ip[0] to preserve 609eb8dcdeaSGleb Smirnoff * the primary IP address as given from userland. 610eb8dcdeaSGleb Smirnoff * This special IP is used for unbound outgoing 611eb8dcdeaSGleb Smirnoff * connections as well for "loopback" traffic in case 612eb8dcdeaSGleb Smirnoff * source address selection cannot find any more fitting 613eb8dcdeaSGleb Smirnoff * address to connect from. 614eb8dcdeaSGleb Smirnoff */ 615eb8dcdeaSGleb Smirnoff if (cnt > 1) 616eb8dcdeaSGleb Smirnoff qsort((char *)(pip + 1) + size, cnt - 1, size, 617eb8dcdeaSGleb Smirnoff pr_families[af].cmp); 618eb8dcdeaSGleb Smirnoff /* 619eb8dcdeaSGleb Smirnoff * Check for duplicate addresses and do some simple 620eb8dcdeaSGleb Smirnoff * zero and broadcast checks. If users give other bogus 621eb8dcdeaSGleb Smirnoff * addresses it is their problem. 622eb8dcdeaSGleb Smirnoff */ 623eb8dcdeaSGleb Smirnoff for (int i = 0; i < cnt; i++) { 624eb8dcdeaSGleb Smirnoff if (!pr_families[af].valid(PR_IP(pip, i))) { 625eb8dcdeaSGleb Smirnoff free(pip, M_PRISON); 626eb8dcdeaSGleb Smirnoff return (NULL); 627eb8dcdeaSGleb Smirnoff } 628eb8dcdeaSGleb Smirnoff if (i + 1 < cnt && 629eb8dcdeaSGleb Smirnoff (cmp(PR_IP(pip, 0), PR_IP(pip, i + 1)) == 0 || 630eb8dcdeaSGleb Smirnoff cmp(PR_IP(pip, i), PR_IP(pip, i + 1)) == 0)) { 631eb8dcdeaSGleb Smirnoff free(pip, M_PRISON); 632eb8dcdeaSGleb Smirnoff return (NULL); 633eb8dcdeaSGleb Smirnoff } 634eb8dcdeaSGleb Smirnoff } 635eb8dcdeaSGleb Smirnoff 636eb8dcdeaSGleb Smirnoff return (pip); 637eb8dcdeaSGleb Smirnoff } 638eb8dcdeaSGleb Smirnoff 639eb8dcdeaSGleb Smirnoff /* 640eb8dcdeaSGleb Smirnoff * Allocate and dup parent prison address list. 641eb8dcdeaSGleb Smirnoff * kern_jail_set() helper. 642eb8dcdeaSGleb Smirnoff */ 643eb8dcdeaSGleb Smirnoff static void 644eb8dcdeaSGleb Smirnoff prison_ip_dup(struct prison *ppr, struct prison *pr, const pr_family_t af) 645eb8dcdeaSGleb Smirnoff { 646eb8dcdeaSGleb Smirnoff 647eb8dcdeaSGleb Smirnoff if (ppr->pr_addrs[af] != NULL) { 648eb8dcdeaSGleb Smirnoff pr->pr_addrs[af] = prison_ip_alloc(af, 649eb8dcdeaSGleb Smirnoff ppr->pr_addrs[af]->ips, M_WAITOK); 650eb8dcdeaSGleb Smirnoff bcopy(ppr->pr_addrs[af], pr->pr_addrs[af], 651eb8dcdeaSGleb Smirnoff pr->pr_addrs[af]->ips * pr_families[af].size); 652eb8dcdeaSGleb Smirnoff } 653eb8dcdeaSGleb Smirnoff } 654eb8dcdeaSGleb Smirnoff 655eb8dcdeaSGleb Smirnoff /* 656eb8dcdeaSGleb Smirnoff * Make sure the new set of IP addresses is a subset of the parent's list. 657eb8dcdeaSGleb Smirnoff * Don't worry about the parent being unlocked, as any setting is done with 658eb8dcdeaSGleb Smirnoff * allprison_lock held. 659eb8dcdeaSGleb Smirnoff * kern_jail_set() helper. 660eb8dcdeaSGleb Smirnoff */ 661eb8dcdeaSGleb Smirnoff static bool 662eb8dcdeaSGleb Smirnoff prison_ip_parent_match(const struct prison_ip *ppip, 663eb8dcdeaSGleb Smirnoff const struct prison_ip *pip, const pr_family_t af) 664eb8dcdeaSGleb Smirnoff { 665eb8dcdeaSGleb Smirnoff prison_addr_cmp_t *const cmp = pr_families[af].cmp; 666eb8dcdeaSGleb Smirnoff int i, j; 667eb8dcdeaSGleb Smirnoff 668eb8dcdeaSGleb Smirnoff if (ppip == NULL) 669eb8dcdeaSGleb Smirnoff return (false); 670eb8dcdeaSGleb Smirnoff 671eb8dcdeaSGleb Smirnoff for (i = 0; i < ppip->ips; i++) 672eb8dcdeaSGleb Smirnoff if (cmp(PR_IP(pip, 0), PR_IP(ppip, i)) == 0) 673eb8dcdeaSGleb Smirnoff break; 674eb8dcdeaSGleb Smirnoff 675eb8dcdeaSGleb Smirnoff if (i == ppip->ips) 676eb8dcdeaSGleb Smirnoff /* Main address not present in parent. */ 677eb8dcdeaSGleb Smirnoff return (false); 678eb8dcdeaSGleb Smirnoff 679eb8dcdeaSGleb Smirnoff if (pip->ips > 1) { 680eb8dcdeaSGleb Smirnoff for (i = j = 1; i < pip->ips; i++) { 681eb8dcdeaSGleb Smirnoff if (cmp(PR_IP(pip, i), PR_IP(ppip, 0)) == 0) 682eb8dcdeaSGleb Smirnoff /* Equals to parent primary address. */ 683eb8dcdeaSGleb Smirnoff continue; 684eb8dcdeaSGleb Smirnoff for (; j < ppip->ips; j++) 685eb8dcdeaSGleb Smirnoff if (cmp(PR_IP(pip, i), PR_IP(ppip, j)) == 0) 686eb8dcdeaSGleb Smirnoff break; 687eb8dcdeaSGleb Smirnoff if (j == ppip->ips) 688eb8dcdeaSGleb Smirnoff break; 689eb8dcdeaSGleb Smirnoff } 690eb8dcdeaSGleb Smirnoff if (j == ppip->ips) 691eb8dcdeaSGleb Smirnoff /* Address not present in parent. */ 692eb8dcdeaSGleb Smirnoff return (false); 693eb8dcdeaSGleb Smirnoff } 694eb8dcdeaSGleb Smirnoff return (true); 695eb8dcdeaSGleb Smirnoff } 696eb8dcdeaSGleb Smirnoff 697eb8dcdeaSGleb Smirnoff /* 698eb8dcdeaSGleb Smirnoff * Check for conflicting IP addresses. We permit them if there is no more 699eb8dcdeaSGleb Smirnoff * than one IP on each jail. If there is a duplicate on a jail with more 700eb8dcdeaSGleb Smirnoff * than one IP stop checking and return error. 701eb8dcdeaSGleb Smirnoff * kern_jail_set() helper. 702eb8dcdeaSGleb Smirnoff */ 703eb8dcdeaSGleb Smirnoff static bool 704eb8dcdeaSGleb Smirnoff prison_ip_conflict_check(const struct prison *ppr, const struct prison *pr, 705eb8dcdeaSGleb Smirnoff const struct prison_ip *pip, pr_family_t af) 706eb8dcdeaSGleb Smirnoff { 707eb8dcdeaSGleb Smirnoff const struct prison *tppr, *tpr; 708eb8dcdeaSGleb Smirnoff int descend; 709eb8dcdeaSGleb Smirnoff 710eb8dcdeaSGleb Smirnoff #ifdef VIMAGE 711eb8dcdeaSGleb Smirnoff for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent) 712eb8dcdeaSGleb Smirnoff if (tppr->pr_flags & PR_VNET) 713eb8dcdeaSGleb Smirnoff break; 714eb8dcdeaSGleb Smirnoff #else 715eb8dcdeaSGleb Smirnoff tppr = &prison0; 716eb8dcdeaSGleb Smirnoff #endif 717eb8dcdeaSGleb Smirnoff FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 718eb8dcdeaSGleb Smirnoff if (tpr == pr || 719eb8dcdeaSGleb Smirnoff #ifdef VIMAGE 720eb8dcdeaSGleb Smirnoff (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 721eb8dcdeaSGleb Smirnoff #endif 722eb8dcdeaSGleb Smirnoff !prison_isalive(tpr)) { 723eb8dcdeaSGleb Smirnoff descend = 0; 724eb8dcdeaSGleb Smirnoff continue; 725eb8dcdeaSGleb Smirnoff } 726eb8dcdeaSGleb Smirnoff if (!(tpr->pr_flags & pr_families[af].ip_flag)) 727eb8dcdeaSGleb Smirnoff continue; 728eb8dcdeaSGleb Smirnoff descend = 0; 729eb8dcdeaSGleb Smirnoff if (tpr->pr_addrs[af] == NULL || 730eb8dcdeaSGleb Smirnoff (pip->ips == 1 && tpr->pr_addrs[af]->ips == 1)) 731eb8dcdeaSGleb Smirnoff continue; 732eb8dcdeaSGleb Smirnoff for (int i = 0; i < pip->ips; i++) 733eb8dcdeaSGleb Smirnoff if (prison_ip_check(tpr, af, PR_IP(pip, i)) == 0) 734eb8dcdeaSGleb Smirnoff return (false); 735eb8dcdeaSGleb Smirnoff } 736eb8dcdeaSGleb Smirnoff 737eb8dcdeaSGleb Smirnoff return (true); 738eb8dcdeaSGleb Smirnoff } 739eb8dcdeaSGleb Smirnoff 740eb8dcdeaSGleb Smirnoff _Static_assert(offsetof(struct prison_ip, ctx) == 0, 741eb8dcdeaSGleb Smirnoff "prison must start with epoch context"); 742eb8dcdeaSGleb Smirnoff static void 743eb8dcdeaSGleb Smirnoff prison_ip_free_deferred(epoch_context_t ctx) 744eb8dcdeaSGleb Smirnoff { 745eb8dcdeaSGleb Smirnoff 746eb8dcdeaSGleb Smirnoff free(ctx, M_PRISON); 747eb8dcdeaSGleb Smirnoff } 748eb8dcdeaSGleb Smirnoff 749eb8dcdeaSGleb Smirnoff static void 750eb8dcdeaSGleb Smirnoff prison_ip_free(struct prison_ip *pip) 751eb8dcdeaSGleb Smirnoff { 752eb8dcdeaSGleb Smirnoff 753eb8dcdeaSGleb Smirnoff if (pip != NULL) 754eb8dcdeaSGleb Smirnoff NET_EPOCH_CALL(prison_ip_free_deferred, &pip->ctx); 755eb8dcdeaSGleb Smirnoff } 756eb8dcdeaSGleb Smirnoff 757eb8dcdeaSGleb Smirnoff static void 758eb8dcdeaSGleb Smirnoff prison_ip_set(struct prison *pr, const pr_family_t af, struct prison_ip *new) 759eb8dcdeaSGleb Smirnoff { 760eb8dcdeaSGleb Smirnoff struct prison_ip **mem, *old; 761eb8dcdeaSGleb Smirnoff 762eb8dcdeaSGleb Smirnoff mtx_assert(&pr->pr_mtx, MA_OWNED); 763eb8dcdeaSGleb Smirnoff 764eb8dcdeaSGleb Smirnoff mem = &pr->pr_addrs[af]; 765eb8dcdeaSGleb Smirnoff 766eb8dcdeaSGleb Smirnoff old = *mem; 767eb8dcdeaSGleb Smirnoff ck_pr_store_ptr(mem, new); 768eb8dcdeaSGleb Smirnoff prison_ip_free(old); 769eb8dcdeaSGleb Smirnoff } 770eb8dcdeaSGleb Smirnoff 771eb8dcdeaSGleb Smirnoff /* 772eb8dcdeaSGleb Smirnoff * Restrict a prison's IP address list with its parent's, possibly replacing 773eb8dcdeaSGleb Smirnoff * it. Return true if the replacement buffer was used (or would have been). 774eb8dcdeaSGleb Smirnoff * kern_jail_set() helper. 775eb8dcdeaSGleb Smirnoff */ 776eb8dcdeaSGleb Smirnoff static bool 777eb8dcdeaSGleb Smirnoff prison_ip_restrict(struct prison *pr, const pr_family_t af, 778eb8dcdeaSGleb Smirnoff struct prison_ip *new) 779eb8dcdeaSGleb Smirnoff { 780eb8dcdeaSGleb Smirnoff const struct prison_ip *ppip = pr->pr_parent->pr_addrs[af]; 781eb8dcdeaSGleb Smirnoff const struct prison_ip *pip = pr->pr_addrs[af]; 782eb8dcdeaSGleb Smirnoff int (*const cmp)(const void *, const void *) = pr_families[af].cmp; 783eb8dcdeaSGleb Smirnoff const size_t size = pr_families[af].size; 784eb8dcdeaSGleb Smirnoff uint32_t ips; 785eb8dcdeaSGleb Smirnoff bool alloced; 786eb8dcdeaSGleb Smirnoff 787eb8dcdeaSGleb Smirnoff mtx_assert(&pr->pr_mtx, MA_OWNED); 788eb8dcdeaSGleb Smirnoff 789eb8dcdeaSGleb Smirnoff /* 790eb8dcdeaSGleb Smirnoff * Due to epoch-synchronized access to the IP address lists we always 791eb8dcdeaSGleb Smirnoff * allocate a new list even if the old one has enough space. We could 792eb8dcdeaSGleb Smirnoff * atomically update an IPv4 address inside a list, but that would 793eb8dcdeaSGleb Smirnoff * screw up sorting, and in case of IPv6 we can't even atomically write 794eb8dcdeaSGleb Smirnoff * one. 795eb8dcdeaSGleb Smirnoff */ 796eb8dcdeaSGleb Smirnoff ips = (pr->pr_flags & pr_families[af].ip_flag) ? pip->ips : ppip->ips; 797eb8dcdeaSGleb Smirnoff if (ips == 0) { 798eb8dcdeaSGleb Smirnoff prison_ip_set(pr, af, NULL); 799eb8dcdeaSGleb Smirnoff return (false); 800eb8dcdeaSGleb Smirnoff } 801eb8dcdeaSGleb Smirnoff if (new == NULL) { 802eb8dcdeaSGleb Smirnoff new = prison_ip_alloc(af, ips, M_NOWAIT); 803eb8dcdeaSGleb Smirnoff if (new == NULL) 804eb8dcdeaSGleb Smirnoff return (true); 805eb8dcdeaSGleb Smirnoff alloced = true; 806eb8dcdeaSGleb Smirnoff } else 807eb8dcdeaSGleb Smirnoff alloced = false; 808eb8dcdeaSGleb Smirnoff if (!(pr->pr_flags & pr_families[af].ip_flag)) { 809eb8dcdeaSGleb Smirnoff /* This has no user settings, so just copy the parent's list. */ 810eb8dcdeaSGleb Smirnoff bcopy(ppip, new, ips * size); 811eb8dcdeaSGleb Smirnoff } else { 812eb8dcdeaSGleb Smirnoff /* Remove addresses that aren't in the parent. */ 813eb8dcdeaSGleb Smirnoff int i; 814eb8dcdeaSGleb Smirnoff 815eb8dcdeaSGleb Smirnoff i = 0; /* index in pip */ 816eb8dcdeaSGleb Smirnoff ips = 0; /* index in new */ 817eb8dcdeaSGleb Smirnoff 818eb8dcdeaSGleb Smirnoff for (int pi = 0; pi < ppip->ips; pi++) 819eb8dcdeaSGleb Smirnoff if (cmp(PR_IP(pip, 0), PR_IP(ppip, pi)) == 0) { 820eb8dcdeaSGleb Smirnoff /* Found our primary address in parent. */ 821eb8dcdeaSGleb Smirnoff bcopy(PR_IP(pip, i), PR_IPD(new, ips), size); 822eb8dcdeaSGleb Smirnoff i++; 823eb8dcdeaSGleb Smirnoff ips++; 824eb8dcdeaSGleb Smirnoff break; 825eb8dcdeaSGleb Smirnoff } 826eb8dcdeaSGleb Smirnoff for (int pi = 1; i < pip->ips; ) { 827eb8dcdeaSGleb Smirnoff /* Check against primary, which is unsorted. */ 828eb8dcdeaSGleb Smirnoff if (cmp(PR_IP(pip, i), PR_IP(ppip, 0)) == 0) { 829eb8dcdeaSGleb Smirnoff /* Matches parent's primary address. */ 830eb8dcdeaSGleb Smirnoff bcopy(PR_IP(pip, i), PR_IPD(new, ips), size); 831eb8dcdeaSGleb Smirnoff i++; 832eb8dcdeaSGleb Smirnoff ips++; 833eb8dcdeaSGleb Smirnoff continue; 834eb8dcdeaSGleb Smirnoff } 835eb8dcdeaSGleb Smirnoff /* The rest are sorted. */ 836eb8dcdeaSGleb Smirnoff switch (pi >= ppip->ips ? -1 : 837eb8dcdeaSGleb Smirnoff cmp(PR_IP(pip, i), PR_IP(ppip, pi))) { 838eb8dcdeaSGleb Smirnoff case -1: 839eb8dcdeaSGleb Smirnoff i++; 840eb8dcdeaSGleb Smirnoff break; 841eb8dcdeaSGleb Smirnoff case 0: 842eb8dcdeaSGleb Smirnoff bcopy(PR_IP(pr, i), PR_IPD(new, ips), size); 843eb8dcdeaSGleb Smirnoff i++; 844eb8dcdeaSGleb Smirnoff pi++; 845eb8dcdeaSGleb Smirnoff ips++; 846eb8dcdeaSGleb Smirnoff break; 847eb8dcdeaSGleb Smirnoff case 1: 848eb8dcdeaSGleb Smirnoff pi++; 849eb8dcdeaSGleb Smirnoff break; 850eb8dcdeaSGleb Smirnoff } 851eb8dcdeaSGleb Smirnoff } 852eb8dcdeaSGleb Smirnoff if (ips == 0) { 853eb8dcdeaSGleb Smirnoff if (alloced) 854eb8dcdeaSGleb Smirnoff prison_ip_free(new); 855eb8dcdeaSGleb Smirnoff new = NULL; 856eb8dcdeaSGleb Smirnoff } 857eb8dcdeaSGleb Smirnoff } 858eb8dcdeaSGleb Smirnoff prison_ip_set(pr, af, new); 859eb8dcdeaSGleb Smirnoff return (new != NULL ? true : false); 860eb8dcdeaSGleb Smirnoff } 861eb8dcdeaSGleb Smirnoff 862eb8dcdeaSGleb Smirnoff /* 863eb8dcdeaSGleb Smirnoff * Fast-path check if an address belongs to a prison. 864eb8dcdeaSGleb Smirnoff */ 865eb8dcdeaSGleb Smirnoff int 866eb8dcdeaSGleb Smirnoff prison_ip_check(const struct prison *pr, const pr_family_t af, 867eb8dcdeaSGleb Smirnoff const void *addr) 868eb8dcdeaSGleb Smirnoff { 869eb8dcdeaSGleb Smirnoff int (*const cmp)(const void *, const void *) = pr_families[af].cmp; 870eb8dcdeaSGleb Smirnoff const struct prison_ip *pip; 871eb8dcdeaSGleb Smirnoff int i, a, z, d; 872eb8dcdeaSGleb Smirnoff 873eb8dcdeaSGleb Smirnoff MPASS(mtx_owned(&pr->pr_mtx) || 874eb8dcdeaSGleb Smirnoff in_epoch(net_epoch_preempt) || 875eb8dcdeaSGleb Smirnoff sx_xlocked(&allprison_lock)); 876eb8dcdeaSGleb Smirnoff 877eb8dcdeaSGleb Smirnoff pip = ck_pr_load_ptr(&pr->pr_addrs[af]); 878eb8dcdeaSGleb Smirnoff if (__predict_false(pip == NULL)) 879eb8dcdeaSGleb Smirnoff return (EAFNOSUPPORT); 880eb8dcdeaSGleb Smirnoff 881eb8dcdeaSGleb Smirnoff /* Check the primary IP. */ 882eb8dcdeaSGleb Smirnoff if (cmp(PR_IP(pip, 0), addr) == 0) 883eb8dcdeaSGleb Smirnoff return (0); 884eb8dcdeaSGleb Smirnoff 885eb8dcdeaSGleb Smirnoff /* 886eb8dcdeaSGleb Smirnoff * All the other IPs are sorted so we can do a binary search. 887eb8dcdeaSGleb Smirnoff */ 888eb8dcdeaSGleb Smirnoff a = 0; 889eb8dcdeaSGleb Smirnoff z = pip->ips - 2; 890eb8dcdeaSGleb Smirnoff while (a <= z) { 891eb8dcdeaSGleb Smirnoff i = (a + z) / 2; 892eb8dcdeaSGleb Smirnoff d = cmp(PR_IP(pip, i + 1), addr); 893eb8dcdeaSGleb Smirnoff if (d > 0) 894eb8dcdeaSGleb Smirnoff z = i - 1; 895eb8dcdeaSGleb Smirnoff else if (d < 0) 896eb8dcdeaSGleb Smirnoff a = i + 1; 897eb8dcdeaSGleb Smirnoff else 898eb8dcdeaSGleb Smirnoff return (0); 899eb8dcdeaSGleb Smirnoff } 900eb8dcdeaSGleb Smirnoff 901eb8dcdeaSGleb Smirnoff return (EADDRNOTAVAIL); 902eb8dcdeaSGleb Smirnoff } 903eb8dcdeaSGleb Smirnoff 904eb8dcdeaSGleb Smirnoff /* 905eb8dcdeaSGleb Smirnoff * Grab primary IP. Historically required mutex, but nothing prevents 906eb8dcdeaSGleb Smirnoff * us to support epoch-protected access. Is it used in fast path? 907eb8dcdeaSGleb Smirnoff * in{6}_jail.c helper 908eb8dcdeaSGleb Smirnoff */ 909eb8dcdeaSGleb Smirnoff const void * 910eb8dcdeaSGleb Smirnoff prison_ip_get0(const struct prison *pr, const pr_family_t af) 911eb8dcdeaSGleb Smirnoff { 912eb8dcdeaSGleb Smirnoff const struct prison_ip *pip = pr->pr_addrs[af]; 913eb8dcdeaSGleb Smirnoff 914eb8dcdeaSGleb Smirnoff mtx_assert(&pr->pr_mtx, MA_OWNED); 915eb8dcdeaSGleb Smirnoff MPASS(pip); 916eb8dcdeaSGleb Smirnoff 917eb8dcdeaSGleb Smirnoff return (pip + 1); 918eb8dcdeaSGleb Smirnoff } 919eb8dcdeaSGleb Smirnoff 920eb8dcdeaSGleb Smirnoff u_int 921eb8dcdeaSGleb Smirnoff prison_ip_cnt(const struct prison *pr, const pr_family_t af) 922eb8dcdeaSGleb Smirnoff { 923eb8dcdeaSGleb Smirnoff 924eb8dcdeaSGleb Smirnoff return (pr->pr_addrs[af]->ips); 925eb8dcdeaSGleb Smirnoff } 926eb8dcdeaSGleb Smirnoff #endif /* defined(INET) || defined(INET6) */ 927eb8dcdeaSGleb Smirnoff 928413628a7SBjoern A. Zeeb int 929b38ff370SJamie Gritton kern_jail_set(struct thread *td, struct uio *optuio, int flags) 930413628a7SBjoern A. Zeeb { 931fd7a8150SMike Barcroft struct nameidata nd; 932b38ff370SJamie Gritton #ifdef INET 933eb8dcdeaSGleb Smirnoff struct prison_ip *ip4; 934413628a7SBjoern A. Zeeb #endif 935b38ff370SJamie Gritton #ifdef INET6 936eb8dcdeaSGleb Smirnoff struct prison_ip *ip6; 937b38ff370SJamie Gritton #endif 938b38ff370SJamie Gritton struct vfsopt *opt; 939b38ff370SJamie Gritton struct vfsoptlist *opts; 9407de883c8SJamie Gritton struct prison *pr, *deadpr, *inspr, *mypr, *ppr, *tpr; 941b38ff370SJamie Gritton struct vnode *root; 942babbbb9cSJamie Gritton char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid; 943b96bd95bSIan Lepore char *g_path, *osrelstr; 944672756aaSJamie Gritton struct bool_flags *bf; 945672756aaSJamie Gritton struct jailsys_flags *jsf; 9460304c731SJamie Gritton #if defined(INET) || defined(INET6) 947b38ff370SJamie Gritton void *op; 9480304c731SJamie Gritton #endif 94976ca6f88SJamie Gritton unsigned long hid; 950b6f47c23SJamie Gritton size_t namelen, onamelen, pnamelen; 9512a4b2251SJamie Gritton int born, created, cuflags, descend, drflags, enforce; 952cc5fd8c7SJamie Gritton int error, errmsg_len, errmsg_pos; 9530cc207a6SMartin Matuska int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel; 954672756aaSJamie Gritton int jid, jsys, len, level; 955b96bd95bSIan Lepore int childmax, osreldt, rsnum, slevel; 956413628a7SBjoern A. Zeeb #ifdef INET 9572b0d6f81SJamie Gritton int ip4s, redo_ip4; 958413628a7SBjoern A. Zeeb #endif 959b38ff370SJamie Gritton #ifdef INET6 9602b0d6f81SJamie Gritton int ip6s, redo_ip6; 961b38ff370SJamie Gritton #endif 9626beb3bb4SKirk McKusick uint64_t pr_allow, ch_allow, pr_flags, ch_flags; 963b3079544SJamie Gritton uint64_t pr_allow_diff; 9646beb3bb4SKirk McKusick unsigned tallow; 965b38ff370SJamie Gritton char numbuf[12]; 966b38ff370SJamie Gritton 967b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_SET); 968b38ff370SJamie Gritton if (!error && (flags & JAIL_ATTACH)) 969b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 970b38ff370SJamie Gritton if (error) 97175c13541SPoul-Henning Kamp return (error); 972b6f47c23SJamie Gritton mypr = td->td_ucred->cr_prison; 973b97457e2SJamie Gritton if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0) 9740304c731SJamie Gritton return (EPERM); 975b38ff370SJamie Gritton if (flags & ~JAIL_SET_MASK) 976b38ff370SJamie Gritton return (EINVAL); 977b38ff370SJamie Gritton 978b38ff370SJamie Gritton /* 979b38ff370SJamie Gritton * Check all the parameters before committing to anything. Not all 980b38ff370SJamie Gritton * errors can be caught early, but we may as well try. Also, this 981b38ff370SJamie Gritton * takes care of some expensive stuff (path lookup) before getting 982b38ff370SJamie Gritton * the allprison lock. 983b38ff370SJamie Gritton * 984b38ff370SJamie Gritton * XXX Jails are not filesystems, and jail parameters are not mount 985b38ff370SJamie Gritton * options. But it makes more sense to re-use the vfsopt code 986b38ff370SJamie Gritton * than duplicate it under a different name. 987b38ff370SJamie Gritton */ 988b38ff370SJamie Gritton error = vfs_buildopts(optuio, &opts); 989b38ff370SJamie Gritton if (error) 990b38ff370SJamie Gritton return (error); 991b38ff370SJamie Gritton #ifdef INET 992b38ff370SJamie Gritton ip4 = NULL; 993b38ff370SJamie Gritton #endif 994b38ff370SJamie Gritton #ifdef INET6 995b38ff370SJamie Gritton ip6 = NULL; 996b38ff370SJamie Gritton #endif 9976dfe0a3dSMartin Matuska g_path = NULL; 998b38ff370SJamie Gritton 999b6f47c23SJamie Gritton cuflags = flags & (JAIL_CREATE | JAIL_UPDATE); 1000b6f47c23SJamie Gritton if (!cuflags) { 1001b6f47c23SJamie Gritton error = EINVAL; 1002b6f47c23SJamie Gritton vfs_opterror(opts, "no valid operation (create or update)"); 1003b6f47c23SJamie Gritton goto done_errmsg; 1004b6f47c23SJamie Gritton } 1005b6f47c23SJamie Gritton 1006b38ff370SJamie Gritton error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 1007b38ff370SJamie Gritton if (error == ENOENT) 1008b38ff370SJamie Gritton jid = 0; 1009b38ff370SJamie Gritton else if (error != 0) 1010b38ff370SJamie Gritton goto done_free; 1011b38ff370SJamie Gritton 1012b38ff370SJamie Gritton error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel)); 1013b38ff370SJamie Gritton if (error == ENOENT) 1014b38ff370SJamie Gritton gotslevel = 0; 1015b38ff370SJamie Gritton else if (error != 0) 1016b38ff370SJamie Gritton goto done_free; 1017b38ff370SJamie Gritton else 1018b38ff370SJamie Gritton gotslevel = 1; 1019b38ff370SJamie Gritton 1020b97457e2SJamie Gritton error = 1021b97457e2SJamie Gritton vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax)); 1022b97457e2SJamie Gritton if (error == ENOENT) 1023b97457e2SJamie Gritton gotchildmax = 0; 1024b97457e2SJamie Gritton else if (error != 0) 1025b97457e2SJamie Gritton goto done_free; 1026b97457e2SJamie Gritton else 1027b97457e2SJamie Gritton gotchildmax = 1; 1028b97457e2SJamie Gritton 10290304c731SJamie Gritton error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce)); 1030f337198dSJamie Gritton if (error == ENOENT) 1031f337198dSJamie Gritton gotenforce = 0; 1032f337198dSJamie Gritton else if (error != 0) 10330304c731SJamie Gritton goto done_free; 1034f337198dSJamie Gritton else if (enforce < 0 || enforce > 2) { 1035f337198dSJamie Gritton error = EINVAL; 1036f337198dSJamie Gritton goto done_free; 1037f337198dSJamie Gritton } else 1038f337198dSJamie Gritton gotenforce = 1; 10390304c731SJamie Gritton 10400cc207a6SMartin Matuska error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum)); 10410cc207a6SMartin Matuska if (error == ENOENT) 10420cc207a6SMartin Matuska gotrsnum = 0; 10430cc207a6SMartin Matuska else if (error != 0) 10440cc207a6SMartin Matuska goto done_free; 10450cc207a6SMartin Matuska else 10460cc207a6SMartin Matuska gotrsnum = 1; 10470cc207a6SMartin Matuska 1048b38ff370SJamie Gritton pr_flags = ch_flags = 0; 1049672756aaSJamie Gritton for (bf = pr_flag_bool; 1050672756aaSJamie Gritton bf < pr_flag_bool + nitems(pr_flag_bool); 1051672756aaSJamie Gritton bf++) { 1052672756aaSJamie Gritton vfs_flagopt(opts, bf->name, &pr_flags, bf->flag); 1053672756aaSJamie Gritton vfs_flagopt(opts, bf->noname, &ch_flags, bf->flag); 10540304c731SJamie Gritton } 1055b38ff370SJamie Gritton ch_flags |= pr_flags; 1056672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 1057672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 1058672756aaSJamie Gritton jsf++) { 1059672756aaSJamie Gritton error = vfs_copyopt(opts, jsf->name, &jsys, sizeof(jsys)); 10607cbf7213SJamie Gritton if (error == ENOENT) 10617cbf7213SJamie Gritton continue; 10627cbf7213SJamie Gritton if (error != 0) 10637cbf7213SJamie Gritton goto done_free; 10647cbf7213SJamie Gritton switch (jsys) { 10657cbf7213SJamie Gritton case JAIL_SYS_DISABLE: 1066672756aaSJamie Gritton if (!jsf->disable) { 10677cbf7213SJamie Gritton error = EINVAL; 10687cbf7213SJamie Gritton goto done_free; 10697cbf7213SJamie Gritton } 1070672756aaSJamie Gritton pr_flags |= jsf->disable; 10717cbf7213SJamie Gritton break; 10727cbf7213SJamie Gritton case JAIL_SYS_NEW: 1073672756aaSJamie Gritton pr_flags |= jsf->new; 10747cbf7213SJamie Gritton break; 10757cbf7213SJamie Gritton case JAIL_SYS_INHERIT: 10767cbf7213SJamie Gritton break; 10777cbf7213SJamie Gritton default: 10787cbf7213SJamie Gritton error = EINVAL; 10797cbf7213SJamie Gritton goto done_free; 10807cbf7213SJamie Gritton } 1081672756aaSJamie Gritton ch_flags |= jsf->new | jsf->disable; 10827cbf7213SJamie Gritton } 10831158508aSJamie Gritton if ((flags & (JAIL_CREATE | JAIL_ATTACH)) == JAIL_CREATE 10844affa14cSJamie Gritton && !(pr_flags & PR_PERSIST)) { 10854affa14cSJamie Gritton error = EINVAL; 10864affa14cSJamie Gritton vfs_opterror(opts, "new jail must persist or attach"); 10874affa14cSJamie Gritton goto done_errmsg; 10884affa14cSJamie Gritton } 1089679e1390SJamie Gritton #ifdef VIMAGE 1090679e1390SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) { 1091679e1390SJamie Gritton error = EINVAL; 1092679e1390SJamie Gritton vfs_opterror(opts, "vnet cannot be changed after creation"); 1093679e1390SJamie Gritton goto done_errmsg; 1094679e1390SJamie Gritton } 1095679e1390SJamie Gritton #endif 10962b0d6f81SJamie Gritton #ifdef INET 10972b0d6f81SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) { 10982b0d6f81SJamie Gritton error = EINVAL; 10992b0d6f81SJamie Gritton vfs_opterror(opts, "ip4 cannot be changed after creation"); 11002b0d6f81SJamie Gritton goto done_errmsg; 11012b0d6f81SJamie Gritton } 11022b0d6f81SJamie Gritton #endif 11032b0d6f81SJamie Gritton #ifdef INET6 11042b0d6f81SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) { 11052b0d6f81SJamie Gritton error = EINVAL; 11062b0d6f81SJamie Gritton vfs_opterror(opts, "ip6 cannot be changed after creation"); 11072b0d6f81SJamie Gritton goto done_errmsg; 11082b0d6f81SJamie Gritton } 11092b0d6f81SJamie Gritton #endif 1110b38ff370SJamie Gritton 11110304c731SJamie Gritton pr_allow = ch_allow = 0; 1112672756aaSJamie Gritton for (bf = pr_flag_allow; 11135d58f959SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 11145d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 1115672756aaSJamie Gritton bf++) { 1116672756aaSJamie Gritton vfs_flagopt(opts, bf->name, &pr_allow, bf->flag); 1117672756aaSJamie Gritton vfs_flagopt(opts, bf->noname, &ch_allow, bf->flag); 11180304c731SJamie Gritton } 11190304c731SJamie Gritton ch_allow |= pr_allow; 11200304c731SJamie Gritton 1121b38ff370SJamie Gritton error = vfs_getopt(opts, "name", (void **)&name, &len); 1122b38ff370SJamie Gritton if (error == ENOENT) 1123b38ff370SJamie Gritton name = NULL; 1124b38ff370SJamie Gritton else if (error != 0) 1125b38ff370SJamie Gritton goto done_free; 1126b38ff370SJamie Gritton else { 1127b38ff370SJamie Gritton if (len == 0 || name[len - 1] != '\0') { 1128b38ff370SJamie Gritton error = EINVAL; 1129b38ff370SJamie Gritton goto done_free; 1130b38ff370SJamie Gritton } 1131b38ff370SJamie Gritton if (len > MAXHOSTNAMELEN) { 1132b38ff370SJamie Gritton error = ENAMETOOLONG; 1133b38ff370SJamie Gritton goto done_free; 1134b38ff370SJamie Gritton } 1135b38ff370SJamie Gritton } 1136b38ff370SJamie Gritton 1137b38ff370SJamie Gritton error = vfs_getopt(opts, "host.hostname", (void **)&host, &len); 1138b38ff370SJamie Gritton if (error == ENOENT) 1139b38ff370SJamie Gritton host = NULL; 1140b38ff370SJamie Gritton else if (error != 0) 1141b38ff370SJamie Gritton goto done_free; 1142b38ff370SJamie Gritton else { 114376ca6f88SJamie Gritton ch_flags |= PR_HOST; 114476ca6f88SJamie Gritton pr_flags |= PR_HOST; 1145b38ff370SJamie Gritton if (len == 0 || host[len - 1] != '\0') { 1146b38ff370SJamie Gritton error = EINVAL; 1147b38ff370SJamie Gritton goto done_free; 1148b38ff370SJamie Gritton } 1149b38ff370SJamie Gritton if (len > MAXHOSTNAMELEN) { 1150b38ff370SJamie Gritton error = ENAMETOOLONG; 1151b38ff370SJamie Gritton goto done_free; 1152b38ff370SJamie Gritton } 1153b38ff370SJamie Gritton } 1154b38ff370SJamie Gritton 115576ca6f88SJamie Gritton error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len); 115676ca6f88SJamie Gritton if (error == ENOENT) 115776ca6f88SJamie Gritton domain = NULL; 115876ca6f88SJamie Gritton else if (error != 0) 115976ca6f88SJamie Gritton goto done_free; 116076ca6f88SJamie Gritton else { 116176ca6f88SJamie Gritton ch_flags |= PR_HOST; 116276ca6f88SJamie Gritton pr_flags |= PR_HOST; 116376ca6f88SJamie Gritton if (len == 0 || domain[len - 1] != '\0') { 116476ca6f88SJamie Gritton error = EINVAL; 116576ca6f88SJamie Gritton goto done_free; 116676ca6f88SJamie Gritton } 116776ca6f88SJamie Gritton if (len > MAXHOSTNAMELEN) { 116876ca6f88SJamie Gritton error = ENAMETOOLONG; 116976ca6f88SJamie Gritton goto done_free; 117076ca6f88SJamie Gritton } 117176ca6f88SJamie Gritton } 117276ca6f88SJamie Gritton 117376ca6f88SJamie Gritton error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len); 117476ca6f88SJamie Gritton if (error == ENOENT) 117576ca6f88SJamie Gritton uuid = NULL; 117676ca6f88SJamie Gritton else if (error != 0) 117776ca6f88SJamie Gritton goto done_free; 117876ca6f88SJamie Gritton else { 117976ca6f88SJamie Gritton ch_flags |= PR_HOST; 118076ca6f88SJamie Gritton pr_flags |= PR_HOST; 118176ca6f88SJamie Gritton if (len == 0 || uuid[len - 1] != '\0') { 118276ca6f88SJamie Gritton error = EINVAL; 118376ca6f88SJamie Gritton goto done_free; 118476ca6f88SJamie Gritton } 118576ca6f88SJamie Gritton if (len > HOSTUUIDLEN) { 118676ca6f88SJamie Gritton error = ENAMETOOLONG; 118776ca6f88SJamie Gritton goto done_free; 118876ca6f88SJamie Gritton } 118976ca6f88SJamie Gritton } 119076ca6f88SJamie Gritton 1191841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32 1192a5c1afadSDmitry Chagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 119376ca6f88SJamie Gritton uint32_t hid32; 119476ca6f88SJamie Gritton 119576ca6f88SJamie Gritton error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32)); 119676ca6f88SJamie Gritton hid = hid32; 119776ca6f88SJamie Gritton } else 119876ca6f88SJamie Gritton #endif 119976ca6f88SJamie Gritton error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid)); 120076ca6f88SJamie Gritton if (error == ENOENT) 120176ca6f88SJamie Gritton gothid = 0; 120276ca6f88SJamie Gritton else if (error != 0) 120376ca6f88SJamie Gritton goto done_free; 120476ca6f88SJamie Gritton else { 120576ca6f88SJamie Gritton gothid = 1; 120676ca6f88SJamie Gritton ch_flags |= PR_HOST; 120776ca6f88SJamie Gritton pr_flags |= PR_HOST; 120876ca6f88SJamie Gritton } 120976ca6f88SJamie Gritton 1210b38ff370SJamie Gritton #ifdef INET 1211b38ff370SJamie Gritton error = vfs_getopt(opts, "ip4.addr", &op, &ip4s); 1212b38ff370SJamie Gritton if (error == ENOENT) 12130e5e396eSJamie Gritton ip4s = 0; 1214b38ff370SJamie Gritton else if (error != 0) 1215b38ff370SJamie Gritton goto done_free; 1216eb8dcdeaSGleb Smirnoff else if (ip4s & (sizeof(struct in_addr) - 1)) { 1217b38ff370SJamie Gritton error = EINVAL; 1218b38ff370SJamie Gritton goto done_free; 12190304c731SJamie Gritton } else { 12206a3f2779SJamie Gritton ch_flags |= PR_IP4_USER; 12216a3f2779SJamie Gritton pr_flags |= PR_IP4_USER; 12226a3f2779SJamie Gritton if (ip4s > 0) { 1223eb8dcdeaSGleb Smirnoff ip4s /= sizeof(struct in_addr); 1224b38ff370SJamie Gritton if (ip4s > jail_max_af_ips) { 1225b38ff370SJamie Gritton error = EINVAL; 1226b38ff370SJamie Gritton vfs_opterror(opts, "too many IPv4 addresses"); 1227b38ff370SJamie Gritton goto done_errmsg; 1228b38ff370SJamie Gritton } 1229eb8dcdeaSGleb Smirnoff ip4 = prison_ip_copyin(PR_INET, op, ip4s); 1230eb8dcdeaSGleb Smirnoff if (ip4 == NULL) { 1231b38ff370SJamie Gritton error = EINVAL; 1232b38ff370SJamie Gritton goto done_free; 1233b38ff370SJamie Gritton } 1234b38ff370SJamie Gritton } 12350304c731SJamie Gritton } 1236b38ff370SJamie Gritton #endif 1237b38ff370SJamie Gritton 1238b38ff370SJamie Gritton #ifdef INET6 1239b38ff370SJamie Gritton error = vfs_getopt(opts, "ip6.addr", &op, &ip6s); 1240b38ff370SJamie Gritton if (error == ENOENT) 12410e5e396eSJamie Gritton ip6s = 0; 1242b38ff370SJamie Gritton else if (error != 0) 1243b38ff370SJamie Gritton goto done_free; 1244eb8dcdeaSGleb Smirnoff else if (ip6s & (sizeof(struct in6_addr) - 1)) { 1245b38ff370SJamie Gritton error = EINVAL; 1246b38ff370SJamie Gritton goto done_free; 12470304c731SJamie Gritton } else { 12486a3f2779SJamie Gritton ch_flags |= PR_IP6_USER; 12496a3f2779SJamie Gritton pr_flags |= PR_IP6_USER; 12506a3f2779SJamie Gritton if (ip6s > 0) { 1251eb8dcdeaSGleb Smirnoff ip6s /= sizeof(struct in6_addr); 1252b38ff370SJamie Gritton if (ip6s > jail_max_af_ips) { 1253b38ff370SJamie Gritton error = EINVAL; 1254b38ff370SJamie Gritton vfs_opterror(opts, "too many IPv6 addresses"); 1255b38ff370SJamie Gritton goto done_errmsg; 1256b38ff370SJamie Gritton } 1257eb8dcdeaSGleb Smirnoff ip6 = prison_ip_copyin(PR_INET6, op, ip6s); 1258eb8dcdeaSGleb Smirnoff if (ip6 == NULL) { 1259b38ff370SJamie Gritton error = EINVAL; 1260b38ff370SJamie Gritton goto done_free; 1261b38ff370SJamie Gritton } 1262b38ff370SJamie Gritton } 12630304c731SJamie Gritton } 1264b38ff370SJamie Gritton #endif 1265b38ff370SJamie Gritton 1266bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6)) 1267bdfc8cc4SJamie Gritton if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 1268bdfc8cc4SJamie Gritton error = EINVAL; 1269bdfc8cc4SJamie Gritton vfs_opterror(opts, 1270bdfc8cc4SJamie Gritton "vnet jails cannot have IP address restrictions"); 1271bdfc8cc4SJamie Gritton goto done_errmsg; 1272bdfc8cc4SJamie Gritton } 1273bdfc8cc4SJamie Gritton #endif 1274bdfc8cc4SJamie Gritton 1275cf0313c6SJamie Gritton error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len); 1276cf0313c6SJamie Gritton if (error == ENOENT) 1277cf0313c6SJamie Gritton osrelstr = NULL; 1278cf0313c6SJamie Gritton else if (error != 0) 1279cf0313c6SJamie Gritton goto done_free; 1280cf0313c6SJamie Gritton else { 1281cf0313c6SJamie Gritton if (flags & JAIL_UPDATE) { 1282cf0313c6SJamie Gritton error = EINVAL; 1283cf0313c6SJamie Gritton vfs_opterror(opts, 1284cf0313c6SJamie Gritton "osrelease cannot be changed after creation"); 1285cf0313c6SJamie Gritton goto done_errmsg; 1286cf0313c6SJamie Gritton } 12871b786d01SBjoern A. Zeeb if (len == 0 || osrelstr[len - 1] != '\0') { 1288cf0313c6SJamie Gritton error = EINVAL; 12891b786d01SBjoern A. Zeeb goto done_free; 12901b786d01SBjoern A. Zeeb } 12911b786d01SBjoern A. Zeeb if (len >= OSRELEASELEN) { 12921b786d01SBjoern A. Zeeb error = ENAMETOOLONG; 1293cf0313c6SJamie Gritton vfs_opterror(opts, 1294cf0313c6SJamie Gritton "osrelease string must be 1-%d bytes long", 1295cf0313c6SJamie Gritton OSRELEASELEN - 1); 1296cf0313c6SJamie Gritton goto done_errmsg; 1297cf0313c6SJamie Gritton } 1298cf0313c6SJamie Gritton } 1299cf0313c6SJamie Gritton 1300cf0313c6SJamie Gritton error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt)); 1301cf0313c6SJamie Gritton if (error == ENOENT) 1302cf0313c6SJamie Gritton osreldt = 0; 1303cf0313c6SJamie Gritton else if (error != 0) 1304cf0313c6SJamie Gritton goto done_free; 1305cf0313c6SJamie Gritton else { 1306cf0313c6SJamie Gritton if (flags & JAIL_UPDATE) { 1307cf0313c6SJamie Gritton error = EINVAL; 1308cf0313c6SJamie Gritton vfs_opterror(opts, 1309cf0313c6SJamie Gritton "osreldate cannot be changed after creation"); 1310cf0313c6SJamie Gritton goto done_errmsg; 1311cf0313c6SJamie Gritton } 1312cf0313c6SJamie Gritton if (osreldt == 0) { 1313cf0313c6SJamie Gritton error = EINVAL; 1314cf0313c6SJamie Gritton vfs_opterror(opts, "osreldate cannot be 0"); 1315cf0313c6SJamie Gritton goto done_errmsg; 1316cf0313c6SJamie Gritton } 1317cf0313c6SJamie Gritton } 1318cf0313c6SJamie Gritton 1319b38ff370SJamie Gritton root = NULL; 1320b38ff370SJamie Gritton error = vfs_getopt(opts, "path", (void **)&path, &len); 1321b38ff370SJamie Gritton if (error == ENOENT) 1322b38ff370SJamie Gritton path = NULL; 1323b38ff370SJamie Gritton else if (error != 0) 1324b38ff370SJamie Gritton goto done_free; 1325b38ff370SJamie Gritton else { 1326b38ff370SJamie Gritton if (flags & JAIL_UPDATE) { 1327b38ff370SJamie Gritton error = EINVAL; 1328b38ff370SJamie Gritton vfs_opterror(opts, 1329b38ff370SJamie Gritton "path cannot be changed after creation"); 1330b38ff370SJamie Gritton goto done_errmsg; 1331b38ff370SJamie Gritton } 1332b38ff370SJamie Gritton if (len == 0 || path[len - 1] != '\0') { 1333b38ff370SJamie Gritton error = EINVAL; 1334b38ff370SJamie Gritton goto done_free; 1335b38ff370SJamie Gritton } 13367e1d3eefSMateusz Guzik NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path); 1337b38ff370SJamie Gritton error = namei(&nd); 1338b38ff370SJamie Gritton if (error) 1339b38ff370SJamie Gritton goto done_free; 1340b38ff370SJamie Gritton root = nd.ni_vp; 1341bb92cd7bSMateusz Guzik NDFREE_PNBUF(&nd); 13426dfe0a3dSMartin Matuska g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 13436dfe0a3dSMartin Matuska strlcpy(g_path, path, MAXPATHLEN); 13446dfe0a3dSMartin Matuska error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN); 13453eb6b656SMateusz Guzik if (error == 0) { 13466dfe0a3dSMartin Matuska path = g_path; 13476dfe0a3dSMartin Matuska } else { 1348f6e633a9SMartin Matuska /* exit on other errors */ 1349b38ff370SJamie Gritton goto done_free; 1350b38ff370SJamie Gritton } 1351f6e633a9SMartin Matuska if (root->v_type != VDIR) { 1352f6e633a9SMartin Matuska error = ENOTDIR; 1353f6e633a9SMartin Matuska vput(root); 1354f6e633a9SMartin Matuska goto done_free; 1355f6e633a9SMartin Matuska } 1356b249ce48SMateusz Guzik VOP_UNLOCK(root); 1357b38ff370SJamie Gritton } 1358b38ff370SJamie Gritton 1359b38ff370SJamie Gritton /* 1360b6f47c23SJamie Gritton * Find the specified jail, or at least its parent. 1361b38ff370SJamie Gritton * This abuses the file error codes ENOENT and EEXIST. 1362b38ff370SJamie Gritton */ 1363b38ff370SJamie Gritton pr = NULL; 13647de883c8SJamie Gritton inspr = NULL; 1365babbbb9cSJamie Gritton if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) { 1366babbbb9cSJamie Gritton namelc = strrchr(name, '.'); 1367babbbb9cSJamie Gritton jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10); 1368babbbb9cSJamie Gritton if (*p != '\0') 1369babbbb9cSJamie Gritton jid = 0; 1370babbbb9cSJamie Gritton } 1371b6f47c23SJamie Gritton sx_xlock(&allprison_lock); 1372108a9384SJamie Gritton drflags = PD_LIST_XLOCKED; 13730a2a96f3SJamie Gritton ppr = mypr; 13740a2a96f3SJamie Gritton if (!prison_isalive(ppr)) { 13750a2a96f3SJamie Gritton /* This jail is dying. This process will surely follow. */ 13760a2a96f3SJamie Gritton error = EAGAIN; 13770a2a96f3SJamie Gritton goto done_deref; 13780a2a96f3SJamie Gritton } 1379b38ff370SJamie Gritton if (jid != 0) { 1380b38ff370SJamie Gritton if (jid < 0) { 1381b38ff370SJamie Gritton error = EINVAL; 1382b38ff370SJamie Gritton vfs_opterror(opts, "negative jid"); 13832a4b2251SJamie Gritton goto done_deref; 1384b38ff370SJamie Gritton } 13857de883c8SJamie Gritton /* 13867de883c8SJamie Gritton * See if a requested jid already exists. Keep track of 13877de883c8SJamie Gritton * where it can be inserted later. 13887de883c8SJamie Gritton */ 13897de883c8SJamie Gritton TAILQ_FOREACH(inspr, &allprison, pr_list) { 1390f7496dcaSJamie Gritton if (inspr->pr_id < jid) 1391f7496dcaSJamie Gritton continue; 1392f7496dcaSJamie Gritton if (inspr->pr_id > jid) 1393f7496dcaSJamie Gritton break; 13947de883c8SJamie Gritton pr = inspr; 1395f7496dcaSJamie Gritton mtx_lock(&pr->pr_mtx); 13962a4b2251SJamie Gritton drflags |= PD_LOCKED; 13977de883c8SJamie Gritton inspr = NULL; 13987de883c8SJamie Gritton break; 13997de883c8SJamie Gritton } 1400b38ff370SJamie Gritton if (pr != NULL) { 1401b38ff370SJamie Gritton /* Create: jid must not exist. */ 1402b38ff370SJamie Gritton if (cuflags == JAIL_CREATE) { 14037de883c8SJamie Gritton /* 14047de883c8SJamie Gritton * Even creators that cannot see the jail will 14057de883c8SJamie Gritton * get EEXIST. 14067de883c8SJamie Gritton */ 1407b38ff370SJamie Gritton error = EEXIST; 1408b38ff370SJamie Gritton vfs_opterror(opts, "jail %d already exists", 1409b38ff370SJamie Gritton jid); 14102a4b2251SJamie Gritton goto done_deref; 1411b38ff370SJamie Gritton } 14120304c731SJamie Gritton if (!prison_ischild(mypr, pr)) { 14137de883c8SJamie Gritton /* 14147de883c8SJamie Gritton * Updaters get ENOENT if they cannot see the 14157de883c8SJamie Gritton * jail. This is true even for CREATE | UPDATE, 14167de883c8SJamie Gritton * which normally cannot give this error. 14177de883c8SJamie Gritton */ 14182a4b2251SJamie Gritton error = ENOENT; 14192a4b2251SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 14202a4b2251SJamie Gritton goto done_deref; 1421f7496dcaSJamie Gritton } 14220a2a96f3SJamie Gritton ppr = pr->pr_parent; 14230a2a96f3SJamie Gritton if (!prison_isalive(ppr)) { 14240a2a96f3SJamie Gritton error = ENOENT; 14250a2a96f3SJamie Gritton vfs_opterror(opts, "jail %d is dying", 14260a2a96f3SJamie Gritton ppr->pr_id); 14270a2a96f3SJamie Gritton goto done_deref; 14280a2a96f3SJamie Gritton } 1429f7496dcaSJamie Gritton if (!prison_isalive(pr)) { 1430b38ff370SJamie Gritton if (!(flags & JAIL_DYING)) { 1431b38ff370SJamie Gritton error = ENOENT; 1432b38ff370SJamie Gritton vfs_opterror(opts, "jail %d is dying", 1433b38ff370SJamie Gritton jid); 14342a4b2251SJamie Gritton goto done_deref; 1435f7496dcaSJamie Gritton } 1436f7496dcaSJamie Gritton if ((flags & JAIL_ATTACH) || 1437b38ff370SJamie Gritton (pr_flags & PR_PERSIST)) { 1438b38ff370SJamie Gritton /* 1439b38ff370SJamie Gritton * A dying jail might be resurrected 1440b38ff370SJamie Gritton * (via attach or persist), but first 1441b38ff370SJamie Gritton * it must determine if another jail 1442b38ff370SJamie Gritton * has claimed its name. Accomplish 1443b38ff370SJamie Gritton * this by implicitly re-setting the 1444b38ff370SJamie Gritton * name. 1445b38ff370SJamie Gritton */ 1446b38ff370SJamie Gritton if (name == NULL) 14470304c731SJamie Gritton name = prison_name(mypr, pr); 1448b38ff370SJamie Gritton } 1449b38ff370SJamie Gritton } 14502a4b2251SJamie Gritton } else { 1451b38ff370SJamie Gritton /* Update: jid must exist. */ 1452b38ff370SJamie Gritton if (cuflags == JAIL_UPDATE) { 1453b38ff370SJamie Gritton error = ENOENT; 1454b38ff370SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 14552a4b2251SJamie Gritton goto done_deref; 1456b38ff370SJamie Gritton } 1457b38ff370SJamie Gritton } 1458b38ff370SJamie Gritton } 1459b38ff370SJamie Gritton /* 1460b38ff370SJamie Gritton * If the caller provided a name, look for a jail by that name. 1461b38ff370SJamie Gritton * This has different semantics for creates and updates keyed by jid 1462b38ff370SJamie Gritton * (where the name must not already exist in a different jail), 1463b38ff370SJamie Gritton * and updates keyed by the name itself (where the name must exist 1464b38ff370SJamie Gritton * because that is the jail being updated). 1465b38ff370SJamie Gritton */ 1466b6f47c23SJamie Gritton namelc = NULL; 1467b38ff370SJamie Gritton if (name != NULL) { 1468babbbb9cSJamie Gritton namelc = strrchr(name, '.'); 1469babbbb9cSJamie Gritton if (namelc == NULL) 1470babbbb9cSJamie Gritton namelc = name; 1471babbbb9cSJamie Gritton else { 14720304c731SJamie Gritton /* 14730304c731SJamie Gritton * This is a hierarchical name. Split it into the 14740304c731SJamie Gritton * parent and child names, and make sure the parent 14750304c731SJamie Gritton * exists or matches an already found jail. 14760304c731SJamie Gritton */ 14770304c731SJamie Gritton if (pr != NULL) { 1478babbbb9cSJamie Gritton if (strncmp(name, ppr->pr_name, namelc - name) 1479babbbb9cSJamie Gritton || ppr->pr_name[namelc - name] != '\0') { 14800304c731SJamie Gritton error = EINVAL; 14810304c731SJamie Gritton vfs_opterror(opts, 14820304c731SJamie Gritton "cannot change jail's parent"); 14832a4b2251SJamie Gritton goto done_deref; 14840304c731SJamie Gritton } 14850304c731SJamie Gritton } else { 1486b6f47c23SJamie Gritton *namelc = '\0'; 14870304c731SJamie Gritton ppr = prison_find_name(mypr, name); 14880304c731SJamie Gritton if (ppr == NULL) { 14890304c731SJamie Gritton error = ENOENT; 14900304c731SJamie Gritton vfs_opterror(opts, 14910304c731SJamie Gritton "jail \"%s\" not found", name); 14922a4b2251SJamie Gritton goto done_deref; 14930304c731SJamie Gritton } 1494c050ea80SJamie Gritton mtx_unlock(&ppr->pr_mtx); 14950a2a96f3SJamie Gritton if (!prison_isalive(ppr)) { 1496c050ea80SJamie Gritton error = ENOENT; 1497c050ea80SJamie Gritton vfs_opterror(opts, 1498c050ea80SJamie Gritton "jail \"%s\" is dying", name); 1499c050ea80SJamie Gritton goto done_deref; 1500c050ea80SJamie Gritton } 1501b6f47c23SJamie Gritton *namelc = '.'; 15020304c731SJamie Gritton } 1503b6f47c23SJamie Gritton namelc++; 15040304c731SJamie Gritton } 1505b6f47c23SJamie Gritton if (namelc[0] != '\0') { 1506b6f47c23SJamie Gritton pnamelen = 15070304c731SJamie Gritton (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 15080304c731SJamie Gritton deadpr = NULL; 15090304c731SJamie Gritton FOREACH_PRISON_CHILD(ppr, tpr) { 15106754ae25SJamie Gritton if (tpr != pr && 1511b6f47c23SJamie Gritton !strcmp(tpr->pr_name + pnamelen, namelc)) { 151276ad42abSJamie Gritton if (prison_isalive(tpr)) { 1513b38ff370SJamie Gritton if (pr == NULL && 1514b38ff370SJamie Gritton cuflags != JAIL_CREATE) { 1515b38ff370SJamie Gritton /* 1516b38ff370SJamie Gritton * Use this jail 1517b38ff370SJamie Gritton * for updates. 1518b38ff370SJamie Gritton */ 1519b38ff370SJamie Gritton pr = tpr; 1520f7496dcaSJamie Gritton mtx_lock(&pr->pr_mtx); 152183bc72a0SJamie Gritton drflags |= PD_LOCKED; 1522b38ff370SJamie Gritton break; 1523b38ff370SJamie Gritton } 1524b38ff370SJamie Gritton /* 1525b38ff370SJamie Gritton * Create, or update(jid): 1526b38ff370SJamie Gritton * name must not exist in an 15270304c731SJamie Gritton * active sibling jail. 1528b38ff370SJamie Gritton */ 1529b38ff370SJamie Gritton error = EEXIST; 1530b38ff370SJamie Gritton vfs_opterror(opts, 1531b38ff370SJamie Gritton "jail \"%s\" already exists", 1532b38ff370SJamie Gritton name); 15332a4b2251SJamie Gritton goto done_deref; 1534b38ff370SJamie Gritton } 153576ad42abSJamie Gritton if (pr == NULL && 1536f7496dcaSJamie Gritton cuflags != JAIL_CREATE) { 153776ad42abSJamie Gritton deadpr = tpr; 1538f7496dcaSJamie Gritton } 1539b38ff370SJamie Gritton } 1540b38ff370SJamie Gritton } 1541b38ff370SJamie Gritton /* If no active jail is found, use a dying one. */ 1542b38ff370SJamie Gritton if (deadpr != NULL && pr == NULL) { 1543b38ff370SJamie Gritton if (flags & JAIL_DYING) { 1544b38ff370SJamie Gritton pr = deadpr; 1545f7496dcaSJamie Gritton mtx_lock(&pr->pr_mtx); 15462a4b2251SJamie Gritton drflags |= PD_LOCKED; 1547b38ff370SJamie Gritton } else if (cuflags == JAIL_UPDATE) { 1548b38ff370SJamie Gritton error = ENOENT; 1549b38ff370SJamie Gritton vfs_opterror(opts, 1550b38ff370SJamie Gritton "jail \"%s\" is dying", name); 15512a4b2251SJamie Gritton goto done_deref; 1552b38ff370SJamie Gritton } 1553b38ff370SJamie Gritton } 1554b38ff370SJamie Gritton /* Update: name must exist if no jid. */ 1555b38ff370SJamie Gritton else if (cuflags == JAIL_UPDATE && pr == NULL) { 1556b38ff370SJamie Gritton error = ENOENT; 1557b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", 1558b38ff370SJamie Gritton name); 15592a4b2251SJamie Gritton goto done_deref; 1560b38ff370SJamie Gritton } 1561b38ff370SJamie Gritton } 1562b38ff370SJamie Gritton } 1563b38ff370SJamie Gritton /* Update: must provide a jid or name. */ 1564b38ff370SJamie Gritton else if (cuflags == JAIL_UPDATE && pr == NULL) { 1565b38ff370SJamie Gritton error = ENOENT; 1566b38ff370SJamie Gritton vfs_opterror(opts, "update specified no jail"); 15672a4b2251SJamie Gritton goto done_deref; 1568b38ff370SJamie Gritton } 1569b38ff370SJamie Gritton 1570b38ff370SJamie Gritton /* If there's no prison to update, create a new one and link it in. */ 15712a4b2251SJamie Gritton created = pr == NULL; 15722a4b2251SJamie Gritton if (created) { 1573b97457e2SJamie Gritton for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent) 1574b97457e2SJamie Gritton if (tpr->pr_childcount >= tpr->pr_childmax) { 1575b97457e2SJamie Gritton error = EPERM; 1576b97457e2SJamie Gritton vfs_opterror(opts, "prison limit exceeded"); 15772a4b2251SJamie Gritton goto done_deref; 1578b97457e2SJamie Gritton } 15797de883c8SJamie Gritton if (jid == 0 && (jid = get_next_prid(&inspr)) == 0) { 1580b38ff370SJamie Gritton error = EAGAIN; 15817de883c8SJamie Gritton vfs_opterror(opts, "no available jail IDs"); 15822a4b2251SJamie Gritton goto done_deref; 1583b38ff370SJamie Gritton } 15842a4b2251SJamie Gritton 15852a4b2251SJamie Gritton pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO); 15861158508aSJamie Gritton pr->pr_state = PRISON_STATE_INVALID; 15871158508aSJamie Gritton refcount_init(&pr->pr_ref, 1); 1588f7496dcaSJamie Gritton refcount_init(&pr->pr_uref, 0); 15891158508aSJamie Gritton drflags |= PD_DEREF; 15902a4b2251SJamie Gritton LIST_INIT(&pr->pr_children); 15912a4b2251SJamie Gritton mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK); 15922a4b2251SJamie Gritton TASK_INIT(&pr->pr_task, 0, prison_complete, pr); 15932a4b2251SJamie Gritton 15947de883c8SJamie Gritton pr->pr_id = jid; 15957de883c8SJamie Gritton if (inspr != NULL) 15967de883c8SJamie Gritton TAILQ_INSERT_BEFORE(inspr, pr, pr_list); 15977de883c8SJamie Gritton else 1598b38ff370SJamie Gritton TAILQ_INSERT_TAIL(&allprison, pr, pr_list); 15997de883c8SJamie Gritton 16007de883c8SJamie Gritton pr->pr_parent = ppr; 16010a2a96f3SJamie Gritton prison_hold(ppr); 16020a2a96f3SJamie Gritton prison_proc_hold(ppr); 16030304c731SJamie Gritton LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling); 16040304c731SJamie Gritton for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 1605b97457e2SJamie Gritton tpr->pr_childcount++; 1606b38ff370SJamie Gritton 16070304c731SJamie Gritton /* Set some default values, and inherit some from the parent. */ 1608b6f47c23SJamie Gritton if (namelc == NULL) 1609b6f47c23SJamie Gritton namelc = ""; 1610b38ff370SJamie Gritton if (path == NULL) { 1611b38ff370SJamie Gritton path = "/"; 16120304c731SJamie Gritton root = mypr->pr_root; 1613b38ff370SJamie Gritton vref(root); 1614b38ff370SJamie Gritton } 16158986e3a0SJamie Gritton strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN); 16168986e3a0SJamie Gritton pr->pr_flags |= PR_HOST; 1617bdfc8cc4SJamie Gritton #if defined(INET) || defined(INET6) 1618bdfc8cc4SJamie Gritton #ifdef VIMAGE 1619bdfc8cc4SJamie Gritton if (!(pr_flags & PR_VNET)) 1620bdfc8cc4SJamie Gritton #endif 1621bdfc8cc4SJamie Gritton { 16220304c731SJamie Gritton #ifdef INET 16232b0d6f81SJamie Gritton if (!(ch_flags & PR_IP4_USER)) 16246a3f2779SJamie Gritton pr->pr_flags |= PR_IP4 | PR_IP4_USER; 16252b0d6f81SJamie Gritton else if (!(pr_flags & PR_IP4_USER)) { 16262b0d6f81SJamie Gritton pr->pr_flags |= ppr->pr_flags & PR_IP4; 1627eb8dcdeaSGleb Smirnoff prison_ip_dup(ppr, pr, PR_INET); 16282b0d6f81SJamie Gritton } 16290304c731SJamie Gritton #endif 16300304c731SJamie Gritton #ifdef INET6 16312b0d6f81SJamie Gritton if (!(ch_flags & PR_IP6_USER)) 16326a3f2779SJamie Gritton pr->pr_flags |= PR_IP6 | PR_IP6_USER; 16332b0d6f81SJamie Gritton else if (!(pr_flags & PR_IP6_USER)) { 16342b0d6f81SJamie Gritton pr->pr_flags |= ppr->pr_flags & PR_IP6; 1635eb8dcdeaSGleb Smirnoff prison_ip_dup(ppr, pr, PR_INET6); 16362b0d6f81SJamie Gritton } 16370304c731SJamie Gritton #endif 1638bdfc8cc4SJamie Gritton } 1639bdfc8cc4SJamie Gritton #endif 1640592bcae8SBjoern A. Zeeb /* Source address selection is always on by default. */ 1641592bcae8SBjoern A. Zeeb pr->pr_flags |= _PR_IP_SADDRSEL; 1642592bcae8SBjoern A. Zeeb 16430304c731SJamie Gritton pr->pr_securelevel = ppr->pr_securelevel; 16440304c731SJamie Gritton pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow; 1645af10bf05SBjoern A. Zeeb pr->pr_enforce_statfs = jail_default_enforce_statfs; 1646bf3db8aaSMartin Matuska pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum; 1647b38ff370SJamie Gritton 1648b96bd95bSIan Lepore pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate; 1649b96bd95bSIan Lepore if (osrelstr == NULL) 16501b786d01SBjoern A. Zeeb strlcpy(pr->pr_osrelease, ppr->pr_osrelease, 16511b786d01SBjoern A. Zeeb sizeof(pr->pr_osrelease)); 1652b96bd95bSIan Lepore else 16531b786d01SBjoern A. Zeeb strlcpy(pr->pr_osrelease, osrelstr, 16541b786d01SBjoern A. Zeeb sizeof(pr->pr_osrelease)); 1655b96bd95bSIan Lepore 1656679e1390SJamie Gritton #ifdef VIMAGE 1657679e1390SJamie Gritton /* Allocate a new vnet if specified. */ 1658679e1390SJamie Gritton pr->pr_vnet = (pr_flags & PR_VNET) 1659679e1390SJamie Gritton ? vnet_alloc() : ppr->pr_vnet; 1660679e1390SJamie Gritton #endif 1661b38ff370SJamie Gritton /* 1662b38ff370SJamie Gritton * Allocate a dedicated cpuset for each jail. 16638771ff75SGordon Bergling * Unlike other initial settings, this may return an error. 1664b38ff370SJamie Gritton */ 16650304c731SJamie Gritton error = cpuset_create_root(ppr, &pr->pr_cpuset); 16662a4b2251SJamie Gritton if (error) 16672a4b2251SJamie Gritton goto done_deref; 1668b38ff370SJamie Gritton 1669b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 16702a4b2251SJamie Gritton drflags |= PD_LOCKED; 1671b38ff370SJamie Gritton } else { 16722b0d6f81SJamie Gritton /* 16732b0d6f81SJamie Gritton * Grab a reference for existing prisons, to ensure they 16742b0d6f81SJamie Gritton * continue to exist for the duration of the call. 16752b0d6f81SJamie Gritton */ 16766754ae25SJamie Gritton prison_hold(pr); 16772a4b2251SJamie Gritton drflags |= PD_DEREF; 1678bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6)) 1679bdfc8cc4SJamie Gritton if ((pr->pr_flags & PR_VNET) && 1680bdfc8cc4SJamie Gritton (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 1681bdfc8cc4SJamie Gritton error = EINVAL; 1682bdfc8cc4SJamie Gritton vfs_opterror(opts, 1683bdfc8cc4SJamie Gritton "vnet jails cannot have IP address restrictions"); 16842a4b2251SJamie Gritton goto done_deref; 1685bdfc8cc4SJamie Gritton } 1686bdfc8cc4SJamie Gritton #endif 16872b0d6f81SJamie Gritton #ifdef INET 16882b0d6f81SJamie Gritton if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 16892b0d6f81SJamie Gritton error = EINVAL; 16902b0d6f81SJamie Gritton vfs_opterror(opts, 16912b0d6f81SJamie Gritton "ip4 cannot be changed after creation"); 16922a4b2251SJamie Gritton goto done_deref; 16932b0d6f81SJamie Gritton } 16942b0d6f81SJamie Gritton #endif 16952b0d6f81SJamie Gritton #ifdef INET6 16962b0d6f81SJamie Gritton if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 16972b0d6f81SJamie Gritton error = EINVAL; 16982b0d6f81SJamie Gritton vfs_opterror(opts, 16992b0d6f81SJamie Gritton "ip6 cannot be changed after creation"); 17002a4b2251SJamie Gritton goto done_deref; 17012b0d6f81SJamie Gritton } 17022b0d6f81SJamie Gritton #endif 1703b38ff370SJamie Gritton } 1704b38ff370SJamie Gritton 1705b38ff370SJamie Gritton /* Do final error checking before setting anything. */ 17060304c731SJamie Gritton if (gotslevel) { 17070304c731SJamie Gritton if (slevel < ppr->pr_securelevel) { 17080304c731SJamie Gritton error = EPERM; 17092a4b2251SJamie Gritton goto done_deref; 17100304c731SJamie Gritton } 17110304c731SJamie Gritton } 1712b97457e2SJamie Gritton if (gotchildmax) { 1713b97457e2SJamie Gritton if (childmax >= ppr->pr_childmax) { 1714b97457e2SJamie Gritton error = EPERM; 17152a4b2251SJamie Gritton goto done_deref; 1716b97457e2SJamie Gritton } 1717b97457e2SJamie Gritton } 17180304c731SJamie Gritton if (gotenforce) { 17190304c731SJamie Gritton if (enforce < ppr->pr_enforce_statfs) { 17200304c731SJamie Gritton error = EPERM; 17212a4b2251SJamie Gritton goto done_deref; 17220304c731SJamie Gritton } 17230304c731SJamie Gritton } 17240cc207a6SMartin Matuska if (gotrsnum) { 17250cc207a6SMartin Matuska /* 17260cc207a6SMartin Matuska * devfs_rsnum is a uint16_t 17270cc207a6SMartin Matuska */ 1728bf3db8aaSMartin Matuska if (rsnum < 0 || rsnum > 65535) { 17290cc207a6SMartin Matuska error = EINVAL; 17302a4b2251SJamie Gritton goto done_deref; 17310cc207a6SMartin Matuska } 17320cc207a6SMartin Matuska /* 1733bf3db8aaSMartin Matuska * Nested jails always inherit parent's devfs ruleset 17340cc207a6SMartin Matuska */ 17350cc207a6SMartin Matuska if (jailed(td->td_ucred)) { 17360cc207a6SMartin Matuska if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) { 17370cc207a6SMartin Matuska error = EPERM; 17382a4b2251SJamie Gritton goto done_deref; 1739bf3db8aaSMartin Matuska } else 17400cc207a6SMartin Matuska rsnum = ppr->pr_devfs_rsnum; 17410cc207a6SMartin Matuska } 17420cc207a6SMartin Matuska } 1743b38ff370SJamie Gritton #ifdef INET 17442b0d6f81SJamie Gritton if (ip4s > 0) { 1745eb8dcdeaSGleb Smirnoff if ((ppr->pr_flags & PR_IP4) && 1746eb8dcdeaSGleb Smirnoff !prison_ip_parent_match(ppr->pr_addrs[PR_INET], ip4, 1747eb8dcdeaSGleb Smirnoff PR_INET)) { 17480304c731SJamie Gritton error = EPERM; 17492a4b2251SJamie Gritton goto done_deref; 17500304c731SJamie Gritton } 1751eb8dcdeaSGleb Smirnoff if (!prison_ip_conflict_check(ppr, pr, ip4, PR_INET)) { 17520304c731SJamie Gritton error = EADDRINUSE; 1753eb8dcdeaSGleb Smirnoff vfs_opterror(opts, "IPv4 addresses clash"); 17542a4b2251SJamie Gritton goto done_deref; 1755b38ff370SJamie Gritton } 17560304c731SJamie Gritton } 1757b38ff370SJamie Gritton #endif 1758b38ff370SJamie Gritton #ifdef INET6 17592b0d6f81SJamie Gritton if (ip6s > 0) { 1760eb8dcdeaSGleb Smirnoff if ((ppr->pr_flags & PR_IP6) && 1761eb8dcdeaSGleb Smirnoff !prison_ip_parent_match(ppr->pr_addrs[PR_INET6], ip6, 1762eb8dcdeaSGleb Smirnoff PR_INET6)) { 17630304c731SJamie Gritton error = EPERM; 17642a4b2251SJamie Gritton goto done_deref; 17650304c731SJamie Gritton } 1766eb8dcdeaSGleb Smirnoff if (!prison_ip_conflict_check(ppr, pr, ip6, PR_INET6)) { 17670304c731SJamie Gritton error = EADDRINUSE; 1768eb8dcdeaSGleb Smirnoff vfs_opterror(opts, "IPv6 addresses clash"); 17692a4b2251SJamie Gritton goto done_deref; 1770b38ff370SJamie Gritton } 17710304c731SJamie Gritton } 1772b38ff370SJamie Gritton #endif 17730304c731SJamie Gritton onamelen = namelen = 0; 1774b6f47c23SJamie Gritton if (namelc != NULL) { 17756ab6058eSJamie Gritton /* Give a default name of the jid. Also allow the name to be 17766ab6058eSJamie Gritton * explicitly the jid - but not any other number, and only in 17776ab6058eSJamie Gritton * normal form (no leading zero/etc). 17786ab6058eSJamie Gritton */ 1779b6f47c23SJamie Gritton if (namelc[0] == '\0') 1780b6f47c23SJamie Gritton snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid); 17816ab6058eSJamie Gritton else if ((strtoul(namelc, &p, 10) != jid || 17826ab6058eSJamie Gritton namelc[0] < '1' || namelc[0] > '9') && *p == '\0') { 1783b38ff370SJamie Gritton error = EINVAL; 1784babbbb9cSJamie Gritton vfs_opterror(opts, 1785babbbb9cSJamie Gritton "name cannot be numeric (unless it is the jid)"); 17862a4b2251SJamie Gritton goto done_deref; 1787b38ff370SJamie Gritton } 1788b38ff370SJamie Gritton /* 17890304c731SJamie Gritton * Make sure the name isn't too long for the prison or its 17900304c731SJamie Gritton * children. 1791b38ff370SJamie Gritton */ 1792b6f47c23SJamie Gritton pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 1793b6f47c23SJamie Gritton onamelen = strlen(pr->pr_name + pnamelen); 1794b6f47c23SJamie Gritton namelen = strlen(namelc); 1795b6f47c23SJamie Gritton if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) { 17960304c731SJamie Gritton error = ENAMETOOLONG; 17972a4b2251SJamie Gritton goto done_deref; 17980304c731SJamie Gritton } 17990304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 18000304c731SJamie Gritton if (strlen(tpr->pr_name) + (namelen - onamelen) >= 18010304c731SJamie Gritton sizeof(pr->pr_name)) { 18020304c731SJamie Gritton error = ENAMETOOLONG; 18032a4b2251SJamie Gritton goto done_deref; 18040304c731SJamie Gritton } 18050304c731SJamie Gritton } 18060304c731SJamie Gritton } 1807b3079544SJamie Gritton pr_allow_diff = pr_allow & ~ppr->pr_allow; 1808b3079544SJamie Gritton if (pr_allow_diff & ~PR_ALLOW_DIFFERENCES) { 18090304c731SJamie Gritton error = EPERM; 18102a4b2251SJamie Gritton goto done_deref; 1811b38ff370SJamie Gritton } 1812b38ff370SJamie Gritton 1813b6f47c23SJamie Gritton /* 1814b6f47c23SJamie Gritton * Let modules check their parameters. This requires unlocking and 1815b6f47c23SJamie Gritton * then re-locking the prison, but this is still a valid state as long 1816b6f47c23SJamie Gritton * as allprison_lock remains xlocked. 1817b6f47c23SJamie Gritton */ 1818b6f47c23SJamie Gritton mtx_unlock(&pr->pr_mtx); 18192a4b2251SJamie Gritton drflags &= ~PD_LOCKED; 1820b6f47c23SJamie Gritton error = osd_jail_call(pr, PR_METHOD_CHECK, opts); 18212a4b2251SJamie Gritton if (error != 0) 18222a4b2251SJamie Gritton goto done_deref; 1823b6f47c23SJamie Gritton mtx_lock(&pr->pr_mtx); 18242a4b2251SJamie Gritton drflags |= PD_LOCKED; 1825b6f47c23SJamie Gritton 1826b6f47c23SJamie Gritton /* At this point, all valid parameters should have been noted. */ 1827b6f47c23SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 1828b6f47c23SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 1829b6f47c23SJamie Gritton error = EINVAL; 1830b6f47c23SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 18312a4b2251SJamie Gritton goto done_deref; 1832b6f47c23SJamie Gritton } 1833b6f47c23SJamie Gritton } 1834b6f47c23SJamie Gritton 1835b38ff370SJamie Gritton /* Set the parameters of the prison. */ 1836b38ff370SJamie Gritton #ifdef INET 18370304c731SJamie Gritton redo_ip4 = 0; 18380304c731SJamie Gritton if (pr_flags & PR_IP4_USER) { 18390304c731SJamie Gritton pr->pr_flags |= PR_IP4; 1840eb8dcdeaSGleb Smirnoff prison_ip_set(pr, PR_INET, ip4); 1841b38ff370SJamie Gritton ip4 = NULL; 18420304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1843bdfc8cc4SJamie Gritton #ifdef VIMAGE 1844bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1845bdfc8cc4SJamie Gritton descend = 0; 1846bdfc8cc4SJamie Gritton continue; 1847bdfc8cc4SJamie Gritton } 1848bdfc8cc4SJamie Gritton #endif 1849eb8dcdeaSGleb Smirnoff if (prison_ip_restrict(tpr, PR_INET, NULL)) { 18500304c731SJamie Gritton redo_ip4 = 1; 18510304c731SJamie Gritton descend = 0; 18520304c731SJamie Gritton } 18530304c731SJamie Gritton } 18540304c731SJamie Gritton } 1855b38ff370SJamie Gritton #endif 1856b38ff370SJamie Gritton #ifdef INET6 18570304c731SJamie Gritton redo_ip6 = 0; 18580304c731SJamie Gritton if (pr_flags & PR_IP6_USER) { 18590304c731SJamie Gritton pr->pr_flags |= PR_IP6; 1860eb8dcdeaSGleb Smirnoff prison_ip_set(pr, PR_INET6, ip6); 1861b38ff370SJamie Gritton ip6 = NULL; 18620304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1863bdfc8cc4SJamie Gritton #ifdef VIMAGE 1864bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1865bdfc8cc4SJamie Gritton descend = 0; 1866bdfc8cc4SJamie Gritton continue; 1867bdfc8cc4SJamie Gritton } 1868bdfc8cc4SJamie Gritton #endif 1869eb8dcdeaSGleb Smirnoff if (prison_ip_restrict(tpr, PR_INET6, NULL)) { 18700304c731SJamie Gritton redo_ip6 = 1; 18710304c731SJamie Gritton descend = 0; 18720304c731SJamie Gritton } 18730304c731SJamie Gritton } 18740304c731SJamie Gritton } 1875b38ff370SJamie Gritton #endif 18760304c731SJamie Gritton if (gotslevel) { 1877b38ff370SJamie Gritton pr->pr_securelevel = slevel; 18780304c731SJamie Gritton /* Set all child jails to be at least this level. */ 18790304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 18800304c731SJamie Gritton if (tpr->pr_securelevel < slevel) 18810304c731SJamie Gritton tpr->pr_securelevel = slevel; 18820304c731SJamie Gritton } 1883b97457e2SJamie Gritton if (gotchildmax) { 1884b97457e2SJamie Gritton pr->pr_childmax = childmax; 1885b97457e2SJamie Gritton /* Set all child jails to under this limit. */ 1886b97457e2SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level) 1887b97457e2SJamie Gritton if (tpr->pr_childmax > childmax - level) 1888b97457e2SJamie Gritton tpr->pr_childmax = childmax > level 1889b97457e2SJamie Gritton ? childmax - level : 0; 1890b97457e2SJamie Gritton } 18910304c731SJamie Gritton if (gotenforce) { 18920304c731SJamie Gritton pr->pr_enforce_statfs = enforce; 18930304c731SJamie Gritton /* Pass this restriction on to the children. */ 18940304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 18950304c731SJamie Gritton if (tpr->pr_enforce_statfs < enforce) 18960304c731SJamie Gritton tpr->pr_enforce_statfs = enforce; 18970304c731SJamie Gritton } 18980cc207a6SMartin Matuska if (gotrsnum) { 18990cc207a6SMartin Matuska pr->pr_devfs_rsnum = rsnum; 19000cc207a6SMartin Matuska /* Pass this restriction on to the children. */ 19010cc207a6SMartin Matuska FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 19020cc207a6SMartin Matuska tpr->pr_devfs_rsnum = rsnum; 19030cc207a6SMartin Matuska } 1904b6f47c23SJamie Gritton if (namelc != NULL) { 19050304c731SJamie Gritton if (ppr == &prison0) 1906b6f47c23SJamie Gritton strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name)); 19070304c731SJamie Gritton else 19080304c731SJamie Gritton snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s", 1909b6f47c23SJamie Gritton ppr->pr_name, namelc); 19100304c731SJamie Gritton /* Change this component of child names. */ 19110304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 19120304c731SJamie Gritton bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen, 19130304c731SJamie Gritton strlen(tpr->pr_name + onamelen) + 1); 19140304c731SJamie Gritton bcopy(pr->pr_name, tpr->pr_name, namelen); 19150304c731SJamie Gritton } 19160304c731SJamie Gritton } 1917b38ff370SJamie Gritton if (path != NULL) { 19180304c731SJamie Gritton /* Try to keep a real-rooted full pathname. */ 1919b38ff370SJamie Gritton strlcpy(pr->pr_path, path, sizeof(pr->pr_path)); 1920b38ff370SJamie Gritton pr->pr_root = root; 19212a4b2251SJamie Gritton root = NULL; 1922b38ff370SJamie Gritton } 192376ca6f88SJamie Gritton if (PR_HOST & ch_flags & ~pr_flags) { 192476ca6f88SJamie Gritton if (pr->pr_flags & PR_HOST) { 192576ca6f88SJamie Gritton /* 192676ca6f88SJamie Gritton * Copy the parent's host info. As with pr_ip4 above, 192776ca6f88SJamie Gritton * the lack of a lock on the parent is not a problem; 192876ca6f88SJamie Gritton * it is always set with allprison_lock at least 192976ca6f88SJamie Gritton * shared, and is held exclusively here. 193076ca6f88SJamie Gritton */ 1931c1f19219SJamie Gritton strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname, 1932c1f19219SJamie Gritton sizeof(pr->pr_hostname)); 1933c1f19219SJamie Gritton strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname, 1934c1f19219SJamie Gritton sizeof(pr->pr_domainname)); 1935c1f19219SJamie Gritton strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid, 1936c1f19219SJamie Gritton sizeof(pr->pr_hostuuid)); 193776ca6f88SJamie Gritton pr->pr_hostid = pr->pr_parent->pr_hostid; 193876ca6f88SJamie Gritton } 193976ca6f88SJamie Gritton } else if (host != NULL || domain != NULL || uuid != NULL || gothid) { 194076ca6f88SJamie Gritton /* Set this prison, and any descendants without PR_HOST. */ 1941b38ff370SJamie Gritton if (host != NULL) 1942c1f19219SJamie Gritton strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname)); 194376ca6f88SJamie Gritton if (domain != NULL) 1944c1f19219SJamie Gritton strlcpy(pr->pr_domainname, domain, 1945c1f19219SJamie Gritton sizeof(pr->pr_domainname)); 194676ca6f88SJamie Gritton if (uuid != NULL) 1947c1f19219SJamie Gritton strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid)); 194876ca6f88SJamie Gritton if (gothid) 194976ca6f88SJamie Gritton pr->pr_hostid = hid; 195076ca6f88SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 195176ca6f88SJamie Gritton if (tpr->pr_flags & PR_HOST) 195276ca6f88SJamie Gritton descend = 0; 195376ca6f88SJamie Gritton else { 195476ca6f88SJamie Gritton if (host != NULL) 1955c1f19219SJamie Gritton strlcpy(tpr->pr_hostname, 1956c1f19219SJamie Gritton pr->pr_hostname, 1957c1f19219SJamie Gritton sizeof(tpr->pr_hostname)); 195876ca6f88SJamie Gritton if (domain != NULL) 1959c1f19219SJamie Gritton strlcpy(tpr->pr_domainname, 1960c1f19219SJamie Gritton pr->pr_domainname, 1961c1f19219SJamie Gritton sizeof(tpr->pr_domainname)); 196276ca6f88SJamie Gritton if (uuid != NULL) 1963c1f19219SJamie Gritton strlcpy(tpr->pr_hostuuid, 1964c1f19219SJamie Gritton pr->pr_hostuuid, 1965c1f19219SJamie Gritton sizeof(tpr->pr_hostuuid)); 196676ca6f88SJamie Gritton if (gothid) 196776ca6f88SJamie Gritton tpr->pr_hostid = hid; 196876ca6f88SJamie Gritton } 196976ca6f88SJamie Gritton } 197076ca6f88SJamie Gritton } 19710304c731SJamie Gritton pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow; 19720fe74ae6SJamie Gritton if ((tallow = ch_allow & ~pr_allow)) 19730fe74ae6SJamie Gritton prison_set_allow_locked(pr, tallow, 0); 1974b38ff370SJamie Gritton /* 1975b38ff370SJamie Gritton * Persistent prisons get an extra reference, and prisons losing their 19761158508aSJamie Gritton * persist flag lose that reference. 1977b38ff370SJamie Gritton */ 197876ad42abSJamie Gritton born = !prison_isalive(pr); 19791158508aSJamie Gritton if (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags)) { 1980b38ff370SJamie Gritton if (pr_flags & PR_PERSIST) { 19816754ae25SJamie Gritton prison_hold(pr); 19821158508aSJamie Gritton /* 19831158508aSJamie Gritton * This may make a dead prison alive again, but wait 19841158508aSJamie Gritton * to label it as such until after OSD calls have had 19851158508aSJamie Gritton * a chance to run (and perhaps to fail). 19861158508aSJamie Gritton */ 19876754ae25SJamie Gritton refcount_acquire(&pr->pr_uref); 1988b38ff370SJamie Gritton } else { 198939c8ef90SJamie Gritton drflags |= PD_DEUREF; 1990f7496dcaSJamie Gritton prison_free_not_last(pr); 1991b38ff370SJamie Gritton } 1992b38ff370SJamie Gritton } 1993b38ff370SJamie Gritton pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags; 1994b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 19952a4b2251SJamie Gritton drflags &= ~PD_LOCKED; 1996c861373bSJamie Gritton /* 1997c861373bSJamie Gritton * Any errors past this point will need to de-persist newly created 1998c861373bSJamie Gritton * prisons, as well as call remove methods. 1999c861373bSJamie Gritton */ 2000c861373bSJamie Gritton if (born) 2001c861373bSJamie Gritton drflags |= PD_KILL; 2002b38ff370SJamie Gritton 2003a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 20044b5c9cf6SEdward Tomasz Napierala if (racct_enable && created) 2005a7ad07bfSEdward Tomasz Napierala prison_racct_attach(pr); 2006a7ad07bfSEdward Tomasz Napierala #endif 2007a7ad07bfSEdward Tomasz Napierala 20080304c731SJamie Gritton /* Locks may have prevented a complete restriction of child IP 20090304c731SJamie Gritton * addresses. If so, allocate some more memory and try again. 20100304c731SJamie Gritton */ 20110304c731SJamie Gritton #ifdef INET 20120304c731SJamie Gritton while (redo_ip4) { 2013eb8dcdeaSGleb Smirnoff ip4s = pr->pr_addrs[PR_INET]->ips; 2014eb8dcdeaSGleb Smirnoff ip4 = prison_ip_alloc(PR_INET, ip4s, M_WAITOK); 20150304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 20160304c731SJamie Gritton redo_ip4 = 0; 20170304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 2018bdfc8cc4SJamie Gritton #ifdef VIMAGE 2019bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 2020bdfc8cc4SJamie Gritton descend = 0; 2021bdfc8cc4SJamie Gritton continue; 2022bdfc8cc4SJamie Gritton } 2023bdfc8cc4SJamie Gritton #endif 2024eb8dcdeaSGleb Smirnoff if (prison_ip_restrict(tpr, PR_INET, ip4)) { 20250304c731SJamie Gritton if (ip4 != NULL) 20260304c731SJamie Gritton ip4 = NULL; 20270304c731SJamie Gritton else 20280304c731SJamie Gritton redo_ip4 = 1; 20290304c731SJamie Gritton } 20300304c731SJamie Gritton } 20310304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 20320304c731SJamie Gritton } 20330304c731SJamie Gritton #endif 20340304c731SJamie Gritton #ifdef INET6 20350304c731SJamie Gritton while (redo_ip6) { 2036eb8dcdeaSGleb Smirnoff ip6s = pr->pr_addrs[PR_INET6]->ips; 2037eb8dcdeaSGleb Smirnoff ip6 = prison_ip_alloc(PR_INET6, ip6s, M_WAITOK); 20380304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 20390304c731SJamie Gritton redo_ip6 = 0; 20400304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 2041bdfc8cc4SJamie Gritton #ifdef VIMAGE 2042bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 2043bdfc8cc4SJamie Gritton descend = 0; 2044bdfc8cc4SJamie Gritton continue; 2045bdfc8cc4SJamie Gritton } 2046bdfc8cc4SJamie Gritton #endif 2047eb8dcdeaSGleb Smirnoff if (prison_ip_restrict(tpr, PR_INET6, ip6)) { 20480304c731SJamie Gritton if (ip6 != NULL) 20490304c731SJamie Gritton ip6 = NULL; 20500304c731SJamie Gritton else 20510304c731SJamie Gritton redo_ip6 = 1; 20520304c731SJamie Gritton } 20530304c731SJamie Gritton } 20540304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 20550304c731SJamie Gritton } 20560304c731SJamie Gritton #endif 20570304c731SJamie Gritton 2058b38ff370SJamie Gritton /* Let the modules do their work. */ 2059cc5fd8c7SJamie Gritton if (born) { 2060b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_CREATE, opts); 2061c861373bSJamie Gritton if (error) 20622a4b2251SJamie Gritton goto done_deref; 2063b38ff370SJamie Gritton } 2064b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_SET, opts); 2065c861373bSJamie Gritton if (error) 20662a4b2251SJamie Gritton goto done_deref; 2067b38ff370SJamie Gritton 20681158508aSJamie Gritton /* 20691158508aSJamie Gritton * A new prison is now ready to be seen; either it has gained a user 20701158508aSJamie Gritton * reference via persistence, or is about to gain one via attachment. 20711158508aSJamie Gritton */ 20721158508aSJamie Gritton if (born) { 20731158508aSJamie Gritton drflags = prison_lock_xlock(pr, drflags); 20741158508aSJamie Gritton pr->pr_state = PRISON_STATE_ALIVE; 20751158508aSJamie Gritton } 20761158508aSJamie Gritton 2077b38ff370SJamie Gritton /* Attach this process to the prison if requested. */ 2078b38ff370SJamie Gritton if (flags & JAIL_ATTACH) { 2079c861373bSJamie Gritton error = do_jail_attach(td, pr, 2080589e4c1dSJamie Gritton prison_lock_xlock(pr, drflags & PD_LOCK_FLAGS)); 2081f7496dcaSJamie Gritton drflags &= ~(PD_LOCKED | PD_LIST_XLOCKED); 2082b38ff370SJamie Gritton if (error) { 2083b38ff370SJamie Gritton vfs_opterror(opts, "attach failed"); 20842a4b2251SJamie Gritton goto done_deref; 2085b38ff370SJamie Gritton } 2086b38ff370SJamie Gritton } 2087b38ff370SJamie Gritton 20881fb24974SEdward Tomasz Napierala #ifdef RACCT 20894b5c9cf6SEdward Tomasz Napierala if (racct_enable && !created) { 2090701d6b50SJamie Gritton if (drflags & PD_LOCKED) { 2091701d6b50SJamie Gritton mtx_unlock(&pr->pr_mtx); 2092701d6b50SJamie Gritton drflags &= ~PD_LOCKED; 2093701d6b50SJamie Gritton } 2094f7496dcaSJamie Gritton if (drflags & PD_LIST_XLOCKED) { 2095f7496dcaSJamie Gritton sx_xunlock(&allprison_lock); 2096f7496dcaSJamie Gritton drflags &= ~PD_LIST_XLOCKED; 2097b4e87a63SJamie Gritton } 20981fb24974SEdward Tomasz Napierala prison_racct_modify(pr); 20991fb24974SEdward Tomasz Napierala } 21001fb24974SEdward Tomasz Napierala #endif 21011fb24974SEdward Tomasz Napierala 2102c861373bSJamie Gritton drflags &= ~PD_KILL; 21031fb24974SEdward Tomasz Napierala td->td_retval[0] = pr->pr_id; 21041fb24974SEdward Tomasz Napierala 21052a4b2251SJamie Gritton done_deref: 21062a4b2251SJamie Gritton /* Release any temporary prison holds and/or locks. */ 21072a4b2251SJamie Gritton if (pr != NULL) 21082a4b2251SJamie Gritton prison_deref(pr, drflags); 21092a4b2251SJamie Gritton else if (drflags & PD_LIST_SLOCKED) 2110b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 21112a4b2251SJamie Gritton else if (drflags & PD_LIST_XLOCKED) 2112b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 21135050aa86SKonstantin Belousov if (root != NULL) 2114b38ff370SJamie Gritton vrele(root); 2115b38ff370SJamie Gritton done_errmsg: 2116b38ff370SJamie Gritton if (error) { 21172a4b2251SJamie Gritton /* Write the error message back to userspace. */ 2118176ff3a0SJamie Gritton if (vfs_getopt(opts, "errmsg", (void **)&errmsg, 2119176ff3a0SJamie Gritton &errmsg_len) == 0 && errmsg_len > 0) { 2120b38ff370SJamie Gritton errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1; 2121b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 2122b38ff370SJamie Gritton bcopy(errmsg, 2123b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2124b38ff370SJamie Gritton errmsg_len); 2125b38ff370SJamie Gritton else 2126b38ff370SJamie Gritton copyout(errmsg, 2127b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2128b38ff370SJamie Gritton errmsg_len); 2129b38ff370SJamie Gritton } 2130b38ff370SJamie Gritton } 2131b38ff370SJamie Gritton done_free: 2132b38ff370SJamie Gritton #ifdef INET 2133eb8dcdeaSGleb Smirnoff prison_ip_free(ip4); 2134b38ff370SJamie Gritton #endif 2135b38ff370SJamie Gritton #ifdef INET6 2136eb8dcdeaSGleb Smirnoff prison_ip_free(ip6); 2137b38ff370SJamie Gritton #endif 21386dfe0a3dSMartin Matuska if (g_path != NULL) 21396dfe0a3dSMartin Matuska free(g_path, M_TEMP); 2140b38ff370SJamie Gritton vfs_freeopts(opts); 2141b38ff370SJamie Gritton return (error); 2142b38ff370SJamie Gritton } 2143b38ff370SJamie Gritton 2144b38ff370SJamie Gritton /* 21457de883c8SJamie Gritton * Find the next available prison ID. Return the ID on success, or zero 21467de883c8SJamie Gritton * on failure. Also set a pointer to the allprison list entry the prison 21477de883c8SJamie Gritton * should be inserted before. 21487de883c8SJamie Gritton */ 21497de883c8SJamie Gritton static int 21507de883c8SJamie Gritton get_next_prid(struct prison **insprp) 21517de883c8SJamie Gritton { 21527de883c8SJamie Gritton struct prison *inspr; 21537de883c8SJamie Gritton int jid, maxid; 21547de883c8SJamie Gritton 21557de883c8SJamie Gritton jid = lastprid % JAIL_MAX + 1; 21567de883c8SJamie Gritton if (TAILQ_EMPTY(&allprison) || 21577de883c8SJamie Gritton TAILQ_LAST(&allprison, prisonlist)->pr_id < jid) { 21587de883c8SJamie Gritton /* 21597de883c8SJamie Gritton * A common case is for all jails to be implicitly numbered, 21607de883c8SJamie Gritton * which means they'll go on the end of the list, at least 21617de883c8SJamie Gritton * for the first JAIL_MAX times. 21627de883c8SJamie Gritton */ 21637de883c8SJamie Gritton inspr = NULL; 21647de883c8SJamie Gritton } else { 21657de883c8SJamie Gritton /* 21667de883c8SJamie Gritton * Take two passes through the allprison list: first starting 21677de883c8SJamie Gritton * with the proposed jid, then ending with it. 21687de883c8SJamie Gritton */ 21697de883c8SJamie Gritton for (maxid = JAIL_MAX; maxid != 0; ) { 21707de883c8SJamie Gritton TAILQ_FOREACH(inspr, &allprison, pr_list) { 21717de883c8SJamie Gritton if (inspr->pr_id < jid) 21727de883c8SJamie Gritton continue; 2173f7496dcaSJamie Gritton if (inspr->pr_id > jid) { 2174f7496dcaSJamie Gritton /* Found an opening. */ 21757de883c8SJamie Gritton maxid = 0; 21767de883c8SJamie Gritton break; 21777de883c8SJamie Gritton } 21787de883c8SJamie Gritton if (++jid > maxid) { 21797de883c8SJamie Gritton if (lastprid == maxid || lastprid == 0) 21807de883c8SJamie Gritton { 21817de883c8SJamie Gritton /* 21827de883c8SJamie Gritton * The entire legal range 21837de883c8SJamie Gritton * has been traversed 21847de883c8SJamie Gritton */ 21857de883c8SJamie Gritton return 0; 21867de883c8SJamie Gritton } 21877de883c8SJamie Gritton /* Try again from the start. */ 21887de883c8SJamie Gritton jid = 1; 21897de883c8SJamie Gritton maxid = lastprid; 21907de883c8SJamie Gritton break; 21917de883c8SJamie Gritton } 21927de883c8SJamie Gritton } 21937de883c8SJamie Gritton if (inspr == NULL) { 21947de883c8SJamie Gritton /* Found room at the end of the list. */ 21957de883c8SJamie Gritton break; 21967de883c8SJamie Gritton } 21977de883c8SJamie Gritton } 21987de883c8SJamie Gritton } 21997de883c8SJamie Gritton *insprp = inspr; 22007de883c8SJamie Gritton lastprid = jid; 22017de883c8SJamie Gritton return (jid); 22027de883c8SJamie Gritton } 22037de883c8SJamie Gritton 22047de883c8SJamie Gritton /* 2205b38ff370SJamie Gritton * struct jail_get_args { 2206b38ff370SJamie Gritton * struct iovec *iovp; 2207b38ff370SJamie Gritton * unsigned int iovcnt; 2208b38ff370SJamie Gritton * int flags; 2209b38ff370SJamie Gritton * }; 2210b38ff370SJamie Gritton */ 2211b38ff370SJamie Gritton int 22128451d0ddSKip Macy sys_jail_get(struct thread *td, struct jail_get_args *uap) 2213b38ff370SJamie Gritton { 2214b38ff370SJamie Gritton struct uio *auio; 2215b38ff370SJamie Gritton int error; 2216b38ff370SJamie Gritton 2217b38ff370SJamie Gritton /* Check that we have an even number of iovecs. */ 2218b38ff370SJamie Gritton if (uap->iovcnt & 1) 2219b38ff370SJamie Gritton return (EINVAL); 2220b38ff370SJamie Gritton 2221b38ff370SJamie Gritton error = copyinuio(uap->iovp, uap->iovcnt, &auio); 2222b38ff370SJamie Gritton if (error) 2223b38ff370SJamie Gritton return (error); 2224b38ff370SJamie Gritton error = kern_jail_get(td, auio, uap->flags); 2225b38ff370SJamie Gritton if (error == 0) 2226b38ff370SJamie Gritton error = copyout(auio->uio_iov, uap->iovp, 2227b38ff370SJamie Gritton uap->iovcnt * sizeof (struct iovec)); 2228b38ff370SJamie Gritton free(auio, M_IOV); 2229b38ff370SJamie Gritton return (error); 2230b38ff370SJamie Gritton } 2231b38ff370SJamie Gritton 2232b38ff370SJamie Gritton int 2233b38ff370SJamie Gritton kern_jail_get(struct thread *td, struct uio *optuio, int flags) 2234b38ff370SJamie Gritton { 2235672756aaSJamie Gritton struct bool_flags *bf; 2236672756aaSJamie Gritton struct jailsys_flags *jsf; 22370304c731SJamie Gritton struct prison *pr, *mypr; 2238b38ff370SJamie Gritton struct vfsopt *opt; 2239b38ff370SJamie Gritton struct vfsoptlist *opts; 2240b38ff370SJamie Gritton char *errmsg, *name; 22412a4b2251SJamie Gritton int drflags, error, errmsg_len, errmsg_pos, i, jid, len, pos; 2242672756aaSJamie Gritton unsigned f; 2243b38ff370SJamie Gritton 2244b38ff370SJamie Gritton if (flags & ~JAIL_GET_MASK) 2245b38ff370SJamie Gritton return (EINVAL); 2246b38ff370SJamie Gritton 2247b38ff370SJamie Gritton /* Get the parameter list. */ 2248b38ff370SJamie Gritton error = vfs_buildopts(optuio, &opts); 2249b38ff370SJamie Gritton if (error) 2250b38ff370SJamie Gritton return (error); 2251b38ff370SJamie Gritton errmsg_pos = vfs_getopt_pos(opts, "errmsg"); 22520304c731SJamie Gritton mypr = td->td_ucred->cr_prison; 22532a4b2251SJamie Gritton pr = NULL; 22541e2a13e6SJamie Gritton 2255b38ff370SJamie Gritton /* 2256b38ff370SJamie Gritton * Find the prison specified by one of: lastjid, jid, name. 2257b38ff370SJamie Gritton */ 2258b38ff370SJamie Gritton sx_slock(&allprison_lock); 22592a4b2251SJamie Gritton drflags = PD_LIST_SLOCKED; 2260b38ff370SJamie Gritton error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid)); 2261b38ff370SJamie Gritton if (error == 0) { 2262b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 2263f7496dcaSJamie Gritton if (pr->pr_id > jid && 2264f7496dcaSJamie Gritton ((flags & JAIL_DYING) || prison_isalive(pr)) && 2265f7496dcaSJamie Gritton prison_ischild(mypr, pr)) { 2266b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 22672a4b2251SJamie Gritton drflags |= PD_LOCKED; 2268b38ff370SJamie Gritton goto found_prison; 22692a4b2251SJamie Gritton } 2270f7496dcaSJamie Gritton } 2271b38ff370SJamie Gritton error = ENOENT; 2272b38ff370SJamie Gritton vfs_opterror(opts, "no jail after %d", jid); 22732a4b2251SJamie Gritton goto done; 2274b38ff370SJamie Gritton } else if (error != ENOENT) 22752a4b2251SJamie Gritton goto done; 2276b38ff370SJamie Gritton 2277b38ff370SJamie Gritton error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 2278b38ff370SJamie Gritton if (error == 0) { 2279b38ff370SJamie Gritton if (jid != 0) { 22800304c731SJamie Gritton pr = prison_find_child(mypr, jid); 2281b38ff370SJamie Gritton if (pr != NULL) { 22822a4b2251SJamie Gritton drflags |= PD_LOCKED; 228376ad42abSJamie Gritton if (!(prison_isalive(pr) || 228476ad42abSJamie Gritton (flags & JAIL_DYING))) { 2285b38ff370SJamie Gritton error = ENOENT; 2286b38ff370SJamie Gritton vfs_opterror(opts, "jail %d is dying", 2287b38ff370SJamie Gritton jid); 22882a4b2251SJamie Gritton goto done; 2289b38ff370SJamie Gritton } 2290b38ff370SJamie Gritton goto found_prison; 2291b38ff370SJamie Gritton } 2292b38ff370SJamie Gritton error = ENOENT; 2293b38ff370SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 22942a4b2251SJamie Gritton goto done; 2295b38ff370SJamie Gritton } 2296b38ff370SJamie Gritton } else if (error != ENOENT) 22972a4b2251SJamie Gritton goto done; 2298b38ff370SJamie Gritton 2299b38ff370SJamie Gritton error = vfs_getopt(opts, "name", (void **)&name, &len); 2300b38ff370SJamie Gritton if (error == 0) { 2301b38ff370SJamie Gritton if (len == 0 || name[len - 1] != '\0') { 2302b38ff370SJamie Gritton error = EINVAL; 23032a4b2251SJamie Gritton goto done; 2304b38ff370SJamie Gritton } 23050304c731SJamie Gritton pr = prison_find_name(mypr, name); 2306b38ff370SJamie Gritton if (pr != NULL) { 23072a4b2251SJamie Gritton drflags |= PD_LOCKED; 230876ad42abSJamie Gritton if (!(prison_isalive(pr) || (flags & JAIL_DYING))) { 2309b38ff370SJamie Gritton error = ENOENT; 2310b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" is dying", 2311b38ff370SJamie Gritton name); 23122a4b2251SJamie Gritton goto done; 2313b38ff370SJamie Gritton } 2314b38ff370SJamie Gritton goto found_prison; 2315b38ff370SJamie Gritton } 2316b38ff370SJamie Gritton error = ENOENT; 2317b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", name); 23182a4b2251SJamie Gritton goto done; 2319b38ff370SJamie Gritton } else if (error != ENOENT) 23202a4b2251SJamie Gritton goto done; 2321b38ff370SJamie Gritton 2322b38ff370SJamie Gritton vfs_opterror(opts, "no jail specified"); 2323b38ff370SJamie Gritton error = ENOENT; 23242a4b2251SJamie Gritton goto done; 2325b38ff370SJamie Gritton 2326b38ff370SJamie Gritton found_prison: 2327b38ff370SJamie Gritton /* Get the parameters of the prison. */ 23286754ae25SJamie Gritton prison_hold(pr); 23292a4b2251SJamie Gritton drflags |= PD_DEREF; 2330b38ff370SJamie Gritton td->td_retval[0] = pr->pr_id; 2331b38ff370SJamie Gritton error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id)); 2332b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 23332a4b2251SJamie Gritton goto done; 23340304c731SJamie Gritton i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id; 23350304c731SJamie Gritton error = vfs_setopt(opts, "parent", &i, sizeof(i)); 2336b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 23372a4b2251SJamie Gritton goto done; 23380304c731SJamie Gritton error = vfs_setopts(opts, "name", prison_name(mypr, pr)); 23390304c731SJamie Gritton if (error != 0 && error != ENOENT) 23402a4b2251SJamie Gritton goto done; 23410304c731SJamie Gritton error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id, 2342b38ff370SJamie Gritton sizeof(pr->pr_cpuset->cs_id)); 2343b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 23442a4b2251SJamie Gritton goto done; 23450304c731SJamie Gritton error = vfs_setopts(opts, "path", prison_path(mypr, pr)); 2346b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 23472a4b2251SJamie Gritton goto done; 2348b38ff370SJamie Gritton #ifdef INET 2349eb8dcdeaSGleb Smirnoff error = vfs_setopt_part(opts, "ip4.addr", pr->pr_addrs[PR_INET] + 1, 2350eb8dcdeaSGleb Smirnoff pr->pr_addrs[PR_INET] ? pr->pr_addrs[PR_INET]->ips * 2351eb8dcdeaSGleb Smirnoff pr_families[PR_INET].size : 0 ); 2352b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 23532a4b2251SJamie Gritton goto done; 2354b38ff370SJamie Gritton #endif 2355b38ff370SJamie Gritton #ifdef INET6 2356eb8dcdeaSGleb Smirnoff error = vfs_setopt_part(opts, "ip6.addr", pr->pr_addrs[PR_INET6] + 1, 2357eb8dcdeaSGleb Smirnoff pr->pr_addrs[PR_INET6] ? pr->pr_addrs[PR_INET6]->ips * 2358eb8dcdeaSGleb Smirnoff pr_families[PR_INET6].size : 0 ); 2359b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 23602a4b2251SJamie Gritton goto done; 2361b38ff370SJamie Gritton #endif 2362b38ff370SJamie Gritton error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel, 2363b38ff370SJamie Gritton sizeof(pr->pr_securelevel)); 2364b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 23652a4b2251SJamie Gritton goto done; 2366b97457e2SJamie Gritton error = vfs_setopt(opts, "children.cur", &pr->pr_childcount, 2367b97457e2SJamie Gritton sizeof(pr->pr_childcount)); 2368b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 23692a4b2251SJamie Gritton goto done; 2370b97457e2SJamie Gritton error = vfs_setopt(opts, "children.max", &pr->pr_childmax, 2371b97457e2SJamie Gritton sizeof(pr->pr_childmax)); 2372b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 23732a4b2251SJamie Gritton goto done; 2374c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostname", pr->pr_hostname); 2375b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 23762a4b2251SJamie Gritton goto done; 2377c1f19219SJamie Gritton error = vfs_setopts(opts, "host.domainname", pr->pr_domainname); 237876ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 23792a4b2251SJamie Gritton goto done; 2380c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid); 238176ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 23822a4b2251SJamie Gritton goto done; 2383841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32 2384a5c1afadSDmitry Chagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 238576ca6f88SJamie Gritton uint32_t hid32 = pr->pr_hostid; 238676ca6f88SJamie Gritton 238776ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32)); 238876ca6f88SJamie Gritton } else 238976ca6f88SJamie Gritton #endif 239076ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid, 239176ca6f88SJamie Gritton sizeof(pr->pr_hostid)); 239276ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 23932a4b2251SJamie Gritton goto done; 23940304c731SJamie Gritton error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs, 23950304c731SJamie Gritton sizeof(pr->pr_enforce_statfs)); 23960304c731SJamie Gritton if (error != 0 && error != ENOENT) 23972a4b2251SJamie Gritton goto done; 23980cc207a6SMartin Matuska error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum, 23990cc207a6SMartin Matuska sizeof(pr->pr_devfs_rsnum)); 24000cc207a6SMartin Matuska if (error != 0 && error != ENOENT) 24012a4b2251SJamie Gritton goto done; 2402672756aaSJamie Gritton for (bf = pr_flag_bool; 2403672756aaSJamie Gritton bf < pr_flag_bool + nitems(pr_flag_bool); 2404672756aaSJamie Gritton bf++) { 2405672756aaSJamie Gritton i = (pr->pr_flags & bf->flag) ? 1 : 0; 2406672756aaSJamie Gritton error = vfs_setopt(opts, bf->name, &i, sizeof(i)); 2407b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24082a4b2251SJamie Gritton goto done; 2409b38ff370SJamie Gritton i = !i; 2410672756aaSJamie Gritton error = vfs_setopt(opts, bf->noname, &i, sizeof(i)); 2411b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24122a4b2251SJamie Gritton goto done; 24130304c731SJamie Gritton } 2414672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 2415672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 2416672756aaSJamie Gritton jsf++) { 2417672756aaSJamie Gritton f = pr->pr_flags & (jsf->disable | jsf->new); 2418672756aaSJamie Gritton i = (f != 0 && f == jsf->disable) ? JAIL_SYS_DISABLE 2419672756aaSJamie Gritton : (f == jsf->new) ? JAIL_SYS_NEW 24207cbf7213SJamie Gritton : JAIL_SYS_INHERIT; 2421672756aaSJamie Gritton error = vfs_setopt(opts, jsf->name, &i, sizeof(i)); 24227cbf7213SJamie Gritton if (error != 0 && error != ENOENT) 24232a4b2251SJamie Gritton goto done; 24247cbf7213SJamie Gritton } 2425672756aaSJamie Gritton for (bf = pr_flag_allow; 24265d58f959SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 24275d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 2428672756aaSJamie Gritton bf++) { 2429672756aaSJamie Gritton i = (pr->pr_allow & bf->flag) ? 1 : 0; 2430672756aaSJamie Gritton error = vfs_setopt(opts, bf->name, &i, sizeof(i)); 24310304c731SJamie Gritton if (error != 0 && error != ENOENT) 24322a4b2251SJamie Gritton goto done; 24330304c731SJamie Gritton i = !i; 2434672756aaSJamie Gritton error = vfs_setopt(opts, bf->noname, &i, sizeof(i)); 24350304c731SJamie Gritton if (error != 0 && error != ENOENT) 24362a4b2251SJamie Gritton goto done; 24370304c731SJamie Gritton } 243876ad42abSJamie Gritton i = !prison_isalive(pr); 2439b38ff370SJamie Gritton error = vfs_setopt(opts, "dying", &i, sizeof(i)); 2440b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24412a4b2251SJamie Gritton goto done; 2442b38ff370SJamie Gritton i = !i; 2443b38ff370SJamie Gritton error = vfs_setopt(opts, "nodying", &i, sizeof(i)); 2444b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24452a4b2251SJamie Gritton goto done; 2446bd96bd15SIan Lepore error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate, 2447bd96bd15SIan Lepore sizeof(pr->pr_osreldate)); 2448a1a4c1b0SIan Lepore if (error != 0 && error != ENOENT) 24492a4b2251SJamie Gritton goto done; 2450a1a4c1b0SIan Lepore error = vfs_setopts(opts, "osrelease", pr->pr_osrelease); 2451a1a4c1b0SIan Lepore if (error != 0 && error != ENOENT) 24522a4b2251SJamie Gritton goto done; 2453b38ff370SJamie Gritton 2454b38ff370SJamie Gritton /* Get the module parameters. */ 2455b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 24562a4b2251SJamie Gritton drflags &= ~PD_LOCKED; 2457b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_GET, opts); 2458b38ff370SJamie Gritton if (error) 24592a4b2251SJamie Gritton goto done; 24602a4b2251SJamie Gritton prison_deref(pr, drflags); 24612a4b2251SJamie Gritton pr = NULL; 24622a4b2251SJamie Gritton drflags = 0; 2463b38ff370SJamie Gritton 2464b38ff370SJamie Gritton /* By now, all parameters should have been noted. */ 2465b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2466b38ff370SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 2467b38ff370SJamie Gritton error = EINVAL; 2468b38ff370SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 24692a4b2251SJamie Gritton goto done; 2470b38ff370SJamie Gritton } 2471b38ff370SJamie Gritton } 2472b38ff370SJamie Gritton 2473b38ff370SJamie Gritton /* Write the fetched parameters back to userspace. */ 2474b38ff370SJamie Gritton error = 0; 2475b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2476b38ff370SJamie Gritton if (opt->pos >= 0 && opt->pos != errmsg_pos) { 2477b38ff370SJamie Gritton pos = 2 * opt->pos + 1; 2478b38ff370SJamie Gritton optuio->uio_iov[pos].iov_len = opt->len; 2479b38ff370SJamie Gritton if (opt->value != NULL) { 2480b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) { 2481b38ff370SJamie Gritton bcopy(opt->value, 2482b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2483b38ff370SJamie Gritton opt->len); 2484b38ff370SJamie Gritton } else { 2485b38ff370SJamie Gritton error = copyout(opt->value, 2486b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2487b38ff370SJamie Gritton opt->len); 2488b38ff370SJamie Gritton if (error) 2489b38ff370SJamie Gritton break; 2490b38ff370SJamie Gritton } 2491b38ff370SJamie Gritton } 2492b38ff370SJamie Gritton } 2493b38ff370SJamie Gritton } 2494b38ff370SJamie Gritton 24952a4b2251SJamie Gritton done: 24962a4b2251SJamie Gritton /* Release any temporary prison holds and/or locks. */ 24972a4b2251SJamie Gritton if (pr != NULL) 24982a4b2251SJamie Gritton prison_deref(pr, drflags); 24992a4b2251SJamie Gritton else if (drflags & PD_LIST_SLOCKED) 2500b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2501b38ff370SJamie Gritton if (error && errmsg_pos >= 0) { 25022a4b2251SJamie Gritton /* Write the error message back to userspace. */ 2503b38ff370SJamie Gritton vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 2504b38ff370SJamie Gritton errmsg_pos = 2 * errmsg_pos + 1; 2505b38ff370SJamie Gritton if (errmsg_len > 0) { 2506b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 2507b38ff370SJamie Gritton bcopy(errmsg, 2508b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2509b38ff370SJamie Gritton errmsg_len); 2510b38ff370SJamie Gritton else 2511b38ff370SJamie Gritton copyout(errmsg, 2512b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2513b38ff370SJamie Gritton errmsg_len); 2514b38ff370SJamie Gritton } 2515b38ff370SJamie Gritton } 2516b38ff370SJamie Gritton vfs_freeopts(opts); 2517b38ff370SJamie Gritton return (error); 2518b38ff370SJamie Gritton } 2519b38ff370SJamie Gritton 2520b38ff370SJamie Gritton /* 2521b38ff370SJamie Gritton * struct jail_remove_args { 2522b38ff370SJamie Gritton * int jid; 2523b38ff370SJamie Gritton * }; 2524b38ff370SJamie Gritton */ 2525b38ff370SJamie Gritton int 25268451d0ddSKip Macy sys_jail_remove(struct thread *td, struct jail_remove_args *uap) 2527b38ff370SJamie Gritton { 2528c861373bSJamie Gritton struct prison *pr; 2529c861373bSJamie Gritton int error; 2530b38ff370SJamie Gritton 2531b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_REMOVE); 2532b38ff370SJamie Gritton if (error) 2533b38ff370SJamie Gritton return (error); 2534b38ff370SJamie Gritton 2535b38ff370SJamie Gritton sx_xlock(&allprison_lock); 25360304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2537b38ff370SJamie Gritton if (pr == NULL) { 2538b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2539b38ff370SJamie Gritton return (EINVAL); 2540b38ff370SJamie Gritton } 2541c861373bSJamie Gritton if (!prison_isalive(pr)) { 2542c861373bSJamie Gritton /* Silently ignore already-dying prisons. */ 25430304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 2544c861373bSJamie Gritton sx_xunlock(&allprison_lock); 25450304c731SJamie Gritton return (0); 25460304c731SJamie Gritton } 2547c861373bSJamie Gritton prison_deref(pr, PD_KILL | PD_LOCKED | PD_LIST_XLOCKED); 2548c861373bSJamie Gritton return (0); 254975c13541SPoul-Henning Kamp } 255075c13541SPoul-Henning Kamp 2551fd7a8150SMike Barcroft /* 25529ddb7954SMike Barcroft * struct jail_attach_args { 25539ddb7954SMike Barcroft * int jid; 25549ddb7954SMike Barcroft * }; 2555fd7a8150SMike Barcroft */ 2556fd7a8150SMike Barcroft int 25578451d0ddSKip Macy sys_jail_attach(struct thread *td, struct jail_attach_args *uap) 2558fd7a8150SMike Barcroft { 2559b38ff370SJamie Gritton struct prison *pr; 2560b38ff370SJamie Gritton int error; 2561b38ff370SJamie Gritton 2562b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 2563b38ff370SJamie Gritton if (error) 2564b38ff370SJamie Gritton return (error); 2565b38ff370SJamie Gritton 2566f7496dcaSJamie Gritton sx_slock(&allprison_lock); 25670304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2568b38ff370SJamie Gritton if (pr == NULL) { 2569b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2570b38ff370SJamie Gritton return (EINVAL); 2571b38ff370SJamie Gritton } 2572b38ff370SJamie Gritton 257376ad42abSJamie Gritton /* Do not allow a process to attach to a prison that is not alive. */ 257476ad42abSJamie Gritton if (!prison_isalive(pr)) { 2575b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2576b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2577b38ff370SJamie Gritton return (EINVAL); 2578b38ff370SJamie Gritton } 2579b38ff370SJamie Gritton 2580f7496dcaSJamie Gritton return (do_jail_attach(td, pr, PD_LOCKED | PD_LIST_SLOCKED)); 2581b38ff370SJamie Gritton } 2582b38ff370SJamie Gritton 2583b38ff370SJamie Gritton static int 2584f7496dcaSJamie Gritton do_jail_attach(struct thread *td, struct prison *pr, int drflags) 2585b38ff370SJamie Gritton { 2586fd7a8150SMike Barcroft struct proc *p; 2587fd7a8150SMike Barcroft struct ucred *newcred, *oldcred; 25885050aa86SKonstantin Belousov int error; 2589fd7a8150SMike Barcroft 2590f7496dcaSJamie Gritton mtx_assert(&pr->pr_mtx, MA_OWNED); 2591f7496dcaSJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 2592589e4c1dSJamie Gritton drflags &= PD_LOCK_FLAGS; 259357f22bd4SJacques Vidrine /* 259457f22bd4SJacques Vidrine * XXX: Note that there is a slight race here if two threads 259557f22bd4SJacques Vidrine * in the same privileged process attempt to attach to two 259657f22bd4SJacques Vidrine * different jails at the same time. It is important for 259757f22bd4SJacques Vidrine * user processes not to do this, or they might end up with 259857f22bd4SJacques Vidrine * a process root from one prison, but attached to the jail 259957f22bd4SJacques Vidrine * of another. 260057f22bd4SJacques Vidrine */ 26011158508aSJamie Gritton prison_hold(pr); 26026754ae25SJamie Gritton refcount_acquire(&pr->pr_uref); 2603f7496dcaSJamie Gritton drflags |= PD_DEREF | PD_DEUREF; 2604fd7a8150SMike Barcroft mtx_unlock(&pr->pr_mtx); 2605f7496dcaSJamie Gritton drflags &= ~PD_LOCKED; 2606b38ff370SJamie Gritton 2607b38ff370SJamie Gritton /* Let modules do whatever they need to prepare for attaching. */ 2608b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_ATTACH, td); 2609b38ff370SJamie Gritton if (error) { 2610f7496dcaSJamie Gritton prison_deref(pr, drflags); 2611b38ff370SJamie Gritton return (error); 2612b38ff370SJamie Gritton } 2613f7496dcaSJamie Gritton sx_unlock(&allprison_lock); 2614f7496dcaSJamie Gritton drflags &= ~(PD_LIST_SLOCKED | PD_LIST_XLOCKED); 2615fd7a8150SMike Barcroft 2616413628a7SBjoern A. Zeeb /* 2617413628a7SBjoern A. Zeeb * Reparent the newly attached process to this jail. 2618413628a7SBjoern A. Zeeb */ 2619b38ff370SJamie Gritton p = td->td_proc; 2620413628a7SBjoern A. Zeeb error = cpuset_setproc_update_set(p, pr->pr_cpuset); 2621413628a7SBjoern A. Zeeb if (error) 2622b38ff370SJamie Gritton goto e_revert_osd; 2623413628a7SBjoern A. Zeeb 2624cb05b60aSAttilio Rao vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY); 2625fd7a8150SMike Barcroft if ((error = change_dir(pr->pr_root, td)) != 0) 2626fd7a8150SMike Barcroft goto e_unlock; 2627fd7a8150SMike Barcroft #ifdef MAC 262830d239bcSRobert Watson if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root))) 2629fd7a8150SMike Barcroft goto e_unlock; 2630fd7a8150SMike Barcroft #endif 2631b249ce48SMateusz Guzik VOP_UNLOCK(pr->pr_root); 2632d4380c0cSJamie Gritton if ((error = pwd_chroot_chdir(td, pr->pr_root))) 26335050aa86SKonstantin Belousov goto e_revert_osd; 2634fd7a8150SMike Barcroft 2635fd7a8150SMike Barcroft newcred = crget(); 2636fd7a8150SMike Barcroft PROC_LOCK(p); 26371fb6767dSJamie Gritton oldcred = crcopysafe(p, newcred); 263869c4ee54SJohn Baldwin newcred->cr_prison = pr; 2639daf63fd2SMateusz Guzik proc_set_cred(p, newcred); 26401fb6767dSJamie Gritton setsugid(p); 2641097055e2SEdward Tomasz Napierala #ifdef RACCT 2642097055e2SEdward Tomasz Napierala racct_proc_ucred_changed(p, oldcred, newcred); 2643f87beb93SAndriy Gapon crhold(newcred); 2644f87beb93SAndriy Gapon #endif 2645f87beb93SAndriy Gapon PROC_UNLOCK(p); 2646f87beb93SAndriy Gapon #ifdef RCTL 2647f87beb93SAndriy Gapon rctl_proc_ucred_changed(p, newcred); 2648f87beb93SAndriy Gapon crfree(newcred); 2649097055e2SEdward Tomasz Napierala #endif 2650f7496dcaSJamie Gritton prison_deref(oldcred->cr_prison, drflags); 2651fd7a8150SMike Barcroft crfree(oldcred); 2652cc7b7306SJamie Gritton 2653cc7b7306SJamie Gritton /* 2654cc7b7306SJamie Gritton * If the prison was killed while changing credentials, die along 2655cc7b7306SJamie Gritton * with it. 2656cc7b7306SJamie Gritton */ 2657cc7b7306SJamie Gritton if (!prison_isalive(pr)) { 2658cc7b7306SJamie Gritton PROC_LOCK(p); 2659cc7b7306SJamie Gritton kern_psignal(p, SIGKILL); 2660cc7b7306SJamie Gritton PROC_UNLOCK(p); 2661cc7b7306SJamie Gritton } 2662cc7b7306SJamie Gritton 2663fd7a8150SMike Barcroft return (0); 26641fb6767dSJamie Gritton 2665fd7a8150SMike Barcroft e_unlock: 2666b249ce48SMateusz Guzik VOP_UNLOCK(pr->pr_root); 2667b38ff370SJamie Gritton e_revert_osd: 2668b38ff370SJamie Gritton /* Tell modules this thread is still in its old jail after all. */ 26697f4e7248SJamie Gritton sx_slock(&allprison_lock); 2670f7496dcaSJamie Gritton drflags |= PD_LIST_SLOCKED; 26711fb6767dSJamie Gritton (void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td); 2672f7496dcaSJamie Gritton prison_deref(pr, drflags); 2673fd7a8150SMike Barcroft return (error); 2674fd7a8150SMike Barcroft } 2675fd7a8150SMike Barcroft 2676fd7a8150SMike Barcroft /* 2677fd7a8150SMike Barcroft * Returns a locked prison instance, or NULL on failure. 2678fd7a8150SMike Barcroft */ 267954b369c1SPawel Jakub Dawidek struct prison * 2680fd7a8150SMike Barcroft prison_find(int prid) 2681fd7a8150SMike Barcroft { 2682fd7a8150SMike Barcroft struct prison *pr; 2683fd7a8150SMike Barcroft 2684dc68a633SPawel Jakub Dawidek sx_assert(&allprison_lock, SX_LOCKED); 2685b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 2686f7496dcaSJamie Gritton if (pr->pr_id < prid) 2687f7496dcaSJamie Gritton continue; 26887de883c8SJamie Gritton if (pr->pr_id > prid) 26897de883c8SJamie Gritton break; 2690f7496dcaSJamie Gritton KASSERT(prison_isvalid(pr), ("Found invalid prison %p", pr)); 2691f7496dcaSJamie Gritton mtx_lock(&pr->pr_mtx); 2692f7496dcaSJamie Gritton return (pr); 2693fd7a8150SMike Barcroft } 2694fd7a8150SMike Barcroft return (NULL); 2695fd7a8150SMike Barcroft } 2696fd7a8150SMike Barcroft 2697b38ff370SJamie Gritton /* 26980304c731SJamie Gritton * Find a prison that is a descendant of mypr. Returns a locked prison or NULL. 2699b38ff370SJamie Gritton */ 2700b38ff370SJamie Gritton struct prison * 27010304c731SJamie Gritton prison_find_child(struct prison *mypr, int prid) 2702b38ff370SJamie Gritton { 27030304c731SJamie Gritton struct prison *pr; 27040304c731SJamie Gritton int descend; 2705b38ff370SJamie Gritton 2706b38ff370SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 27070304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 27080304c731SJamie Gritton if (pr->pr_id == prid) { 2709f7496dcaSJamie Gritton KASSERT(prison_isvalid(pr), 2710f7496dcaSJamie Gritton ("Found invalid prison %p", pr)); 27110304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 27120304c731SJamie Gritton return (pr); 27130304c731SJamie Gritton } 27140304c731SJamie Gritton } 27150304c731SJamie Gritton return (NULL); 27160304c731SJamie Gritton } 27170304c731SJamie Gritton 27180304c731SJamie Gritton /* 27190304c731SJamie Gritton * Look for the name relative to mypr. Returns a locked prison or NULL. 27200304c731SJamie Gritton */ 27210304c731SJamie Gritton struct prison * 27220304c731SJamie Gritton prison_find_name(struct prison *mypr, const char *name) 27230304c731SJamie Gritton { 27240304c731SJamie Gritton struct prison *pr, *deadpr; 27250304c731SJamie Gritton size_t mylen; 27260304c731SJamie Gritton int descend; 27270304c731SJamie Gritton 27280304c731SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 27290304c731SJamie Gritton mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1; 2730b38ff370SJamie Gritton deadpr = NULL; 27310304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 27320304c731SJamie Gritton if (!strcmp(pr->pr_name + mylen, name)) { 2733f7496dcaSJamie Gritton KASSERT(prison_isvalid(pr), 2734f7496dcaSJamie Gritton ("Found invalid prison %p", pr)); 2735f7496dcaSJamie Gritton if (prison_isalive(pr)) { 2736b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 2737b38ff370SJamie Gritton return (pr); 2738f7496dcaSJamie Gritton } 2739b38ff370SJamie Gritton deadpr = pr; 2740b38ff370SJamie Gritton } 2741b38ff370SJamie Gritton } 27420304c731SJamie Gritton /* There was no valid prison - perhaps there was a dying one. */ 2743f7496dcaSJamie Gritton if (deadpr != NULL) 2744b38ff370SJamie Gritton mtx_lock(&deadpr->pr_mtx); 2745b38ff370SJamie Gritton return (deadpr); 2746b38ff370SJamie Gritton } 2747b38ff370SJamie Gritton 2748b38ff370SJamie Gritton /* 27490fe74ae6SJamie Gritton * See if a prison has the specific flag set. The prison should be locked, 27500fe74ae6SJamie Gritton * unless checking for flags that are only set at jail creation (such as 27510fe74ae6SJamie Gritton * PR_IP4 and PR_IP6), or only the single bit is examined, without regard 27520fe74ae6SJamie Gritton * to any other prison data. 27530304c731SJamie Gritton */ 27540304c731SJamie Gritton int 27550304c731SJamie Gritton prison_flag(struct ucred *cred, unsigned flag) 27560304c731SJamie Gritton { 27570304c731SJamie Gritton 27580304c731SJamie Gritton return (cred->cr_prison->pr_flags & flag); 27590304c731SJamie Gritton } 27600304c731SJamie Gritton 27610304c731SJamie Gritton int 27620304c731SJamie Gritton prison_allow(struct ucred *cred, unsigned flag) 27630304c731SJamie Gritton { 27640304c731SJamie Gritton 27650fe74ae6SJamie Gritton return ((cred->cr_prison->pr_allow & flag) != 0); 27660304c731SJamie Gritton } 27670304c731SJamie Gritton 27680304c731SJamie Gritton /* 2769effad35eSJamie Gritton * Hold a prison reference, by incrementing pr_ref. It is generally 2770effad35eSJamie Gritton * an error to hold a prison that does not already have a reference. 2771effad35eSJamie Gritton * A prison record will remain valid as long as it has at least one 2772effad35eSJamie Gritton * reference, and will not be removed as long as either the prison 2773effad35eSJamie Gritton * mutex or the allprison lock is held (allprison_lock may be shared). 2774effad35eSJamie Gritton */ 2775effad35eSJamie Gritton void 2776effad35eSJamie Gritton prison_hold_locked(struct prison *pr) 2777effad35eSJamie Gritton { 2778effad35eSJamie Gritton 27796754ae25SJamie Gritton /* Locking is no longer required. */ 27806754ae25SJamie Gritton prison_hold(pr); 2781effad35eSJamie Gritton } 2782effad35eSJamie Gritton 2783effad35eSJamie Gritton void 2784effad35eSJamie Gritton prison_hold(struct prison *pr) 2785effad35eSJamie Gritton { 27866754ae25SJamie Gritton #ifdef INVARIANTS 27876754ae25SJamie Gritton int was_valid = refcount_acquire_if_not_zero(&pr->pr_ref); 2788effad35eSJamie Gritton 27896754ae25SJamie Gritton KASSERT(was_valid, 27906754ae25SJamie Gritton ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id)); 27916754ae25SJamie Gritton #else 27926754ae25SJamie Gritton refcount_acquire(&pr->pr_ref); 27936754ae25SJamie Gritton #endif 2794effad35eSJamie Gritton } 2795effad35eSJamie Gritton 2796effad35eSJamie Gritton /* 2797effad35eSJamie Gritton * Remove a prison reference. If that was the last reference, the 2798f7496dcaSJamie Gritton * prison will be removed (at a later time). 2799b38ff370SJamie Gritton */ 280091421ba2SRobert Watson void 28011ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr) 280291421ba2SRobert Watson { 280391421ba2SRobert Watson 28041ba4a712SPawel Jakub Dawidek mtx_assert(&pr->pr_mtx, MA_OWNED); 2805effad35eSJamie Gritton /* 2806f7496dcaSJamie Gritton * Locking is no longer required, but unlock because the caller 2807f7496dcaSJamie Gritton * expects it. 2808effad35eSJamie Gritton */ 2809f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 2810f7496dcaSJamie Gritton prison_free(pr); 2811effad35eSJamie Gritton } 2812c2cda609SPawel Jakub Dawidek 28131ba4a712SPawel Jakub Dawidek void 28141ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr) 28151ba4a712SPawel Jakub Dawidek { 28161ba4a712SPawel Jakub Dawidek 28176754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 28186754ae25SJamie Gritton ("Trying to free dead prison %p (jid=%d).", 28196754ae25SJamie Gritton pr, pr->pr_id)); 2820f7496dcaSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_ref)) { 2821f7496dcaSJamie Gritton /* 2822f7496dcaSJamie Gritton * Don't remove the last reference in this context, 2823f7496dcaSJamie Gritton * in case there are locks held. 2824f7496dcaSJamie Gritton */ 2825f7496dcaSJamie Gritton taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 2826f7496dcaSJamie Gritton } 2827f7496dcaSJamie Gritton } 2828f7496dcaSJamie Gritton 2829f7496dcaSJamie Gritton static void 2830f7496dcaSJamie Gritton prison_free_not_last(struct prison *pr) 2831f7496dcaSJamie Gritton { 2832f7496dcaSJamie Gritton #ifdef INVARIANTS 2833f7496dcaSJamie Gritton int lastref; 2834f7496dcaSJamie Gritton 2835f7496dcaSJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 2836f7496dcaSJamie Gritton ("Trying to free dead prison %p (jid=%d).", 2837f7496dcaSJamie Gritton pr, pr->pr_id)); 2838f7496dcaSJamie Gritton lastref = refcount_release(&pr->pr_ref); 2839f7496dcaSJamie Gritton KASSERT(!lastref, 2840f7496dcaSJamie Gritton ("prison_free_not_last freed last ref on prison %p (jid=%d).", 2841f7496dcaSJamie Gritton pr, pr->pr_id)); 2842f7496dcaSJamie Gritton #else 2843ee9b37aeSMateusz Guzik refcount_release(&pr->pr_ref); 2844f7496dcaSJamie Gritton #endif 28451ba4a712SPawel Jakub Dawidek } 28461ba4a712SPawel Jakub Dawidek 284773d9e52dSJamie Gritton /* 2848f171938cSGordon Bergling * Hold a prison for user visibility, by incrementing pr_uref. 2849effad35eSJamie Gritton * It is generally an error to hold a prison that isn't already 2850effad35eSJamie Gritton * user-visible, except through the the jail system calls. It is also 2851effad35eSJamie Gritton * an error to hold an invalid prison. A prison record will remain 2852effad35eSJamie Gritton * alive as long as it has at least one user reference, and will not 2853f7496dcaSJamie Gritton * be set to the dying state until the prison mutex and allprison_lock 2854f7496dcaSJamie Gritton * are both freed. 2855effad35eSJamie Gritton */ 2856effad35eSJamie Gritton void 2857effad35eSJamie Gritton prison_proc_hold(struct prison *pr) 2858effad35eSJamie Gritton { 28596754ae25SJamie Gritton #ifdef INVARIANTS 28606754ae25SJamie Gritton int was_alive = refcount_acquire_if_not_zero(&pr->pr_uref); 2861effad35eSJamie Gritton 28626754ae25SJamie Gritton KASSERT(was_alive, 2863effad35eSJamie Gritton ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id)); 28646754ae25SJamie Gritton #else 28656754ae25SJamie Gritton refcount_acquire(&pr->pr_uref); 28666754ae25SJamie Gritton #endif 2867effad35eSJamie Gritton } 2868effad35eSJamie Gritton 2869effad35eSJamie Gritton /* 2870effad35eSJamie Gritton * Remove a prison user reference. If it was the last reference, the 2871effad35eSJamie Gritton * prison will be considered "dying", and may be removed once all of 2872effad35eSJamie Gritton * its references are dropped. 2873effad35eSJamie Gritton */ 2874effad35eSJamie Gritton void 2875effad35eSJamie Gritton prison_proc_free(struct prison *pr) 2876effad35eSJamie Gritton { 2877effad35eSJamie Gritton 28786754ae25SJamie Gritton /* 28796754ae25SJamie Gritton * Locking is only required when releasing the last reference. 28806754ae25SJamie Gritton * This allows assurance that a locked prison will remain alive 28816754ae25SJamie Gritton * until it is unlocked. 28826754ae25SJamie Gritton */ 28836754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_uref) > 0, 2884effad35eSJamie Gritton ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id)); 2885195cd6aeSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_uref)) { 2886effad35eSJamie Gritton /* 2887effad35eSJamie Gritton * Don't remove the last user reference in this context, 2888effad35eSJamie Gritton * which is expected to be a process that is not only locked, 2889effad35eSJamie Gritton * but also half dead. Add a reference so any calls to 2890effad35eSJamie Gritton * prison_free() won't re-submit the task. 2891effad35eSJamie Gritton */ 2892f7496dcaSJamie Gritton prison_hold(pr); 28931158508aSJamie Gritton mtx_lock(&pr->pr_mtx); 28941158508aSJamie Gritton KASSERT(!(pr->pr_flags & PR_COMPLETE_PROC), 28951158508aSJamie Gritton ("Redundant last reference in prison_proc_free (jid=%d)", 28961158508aSJamie Gritton pr->pr_id)); 28971158508aSJamie Gritton pr->pr_flags |= PR_COMPLETE_PROC; 28981158508aSJamie Gritton mtx_unlock(&pr->pr_mtx); 2899effad35eSJamie Gritton taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 2900effad35eSJamie Gritton } 2901effad35eSJamie Gritton } 2902effad35eSJamie Gritton 2903c861373bSJamie Gritton static void 2904c861373bSJamie Gritton prison_proc_free_not_last(struct prison *pr) 2905c861373bSJamie Gritton { 2906c861373bSJamie Gritton #ifdef INVARIANTS 2907c861373bSJamie Gritton int lastref; 2908c861373bSJamie Gritton 2909c861373bSJamie Gritton KASSERT(refcount_load(&pr->pr_uref) > 0, 2910c861373bSJamie Gritton ("Trying to free dead prison %p (jid=%d).", 2911c861373bSJamie Gritton pr, pr->pr_id)); 2912c861373bSJamie Gritton lastref = refcount_release(&pr->pr_uref); 2913c861373bSJamie Gritton KASSERT(!lastref, 2914c861373bSJamie Gritton ("prison_proc_free_not_last freed last uref on prison %p (jid=%d).", 2915c861373bSJamie Gritton pr, pr->pr_id)); 2916c861373bSJamie Gritton #else 2917c861373bSJamie Gritton refcount_release(&pr->pr_uref); 2918c861373bSJamie Gritton #endif 2919c861373bSJamie Gritton } 2920c861373bSJamie Gritton 2921effad35eSJamie Gritton /* 292273d9e52dSJamie Gritton * Complete a call to either prison_free or prison_proc_free. 292373d9e52dSJamie Gritton */ 2924c2cda609SPawel Jakub Dawidek static void 2925c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending) 2926c2cda609SPawel Jakub Dawidek { 292773d9e52dSJamie Gritton struct prison *pr = context; 2928f7496dcaSJamie Gritton int drflags; 2929b38ff370SJamie Gritton 2930effad35eSJamie Gritton /* 29311158508aSJamie Gritton * This could be called to release the last reference, or the last 29321158508aSJamie Gritton * user reference (plus the reference held in prison_proc_free). 2933effad35eSJamie Gritton */ 2934f7496dcaSJamie Gritton drflags = prison_lock_xlock(pr, PD_DEREF); 29351158508aSJamie Gritton if (pr->pr_flags & PR_COMPLETE_PROC) { 29361158508aSJamie Gritton pr->pr_flags &= ~PR_COMPLETE_PROC; 2937f7496dcaSJamie Gritton drflags |= PD_DEUREF; 29381158508aSJamie Gritton } 2939f7496dcaSJamie Gritton prison_deref(pr, drflags); 2940b38ff370SJamie Gritton } 2941b38ff370SJamie Gritton 2942b38ff370SJamie Gritton /* 2943effad35eSJamie Gritton * Remove a prison reference and/or user reference (usually). 2944effad35eSJamie Gritton * This assumes context that allows sleeping (for allprison_lock), 2945effad35eSJamie Gritton * with no non-sleeping locks held, except perhaps the prison itself. 2946effad35eSJamie Gritton * If there are no more references, release and delist the prison. 2947effad35eSJamie Gritton * On completion, the prison lock and the allprison lock are both 2948effad35eSJamie Gritton * unlocked. 2949b38ff370SJamie Gritton */ 2950b38ff370SJamie Gritton static void 2951b38ff370SJamie Gritton prison_deref(struct prison *pr, int flags) 2952b38ff370SJamie Gritton { 29536e1d1bfcSJamie Gritton struct prisonlist freeprison; 2954c861373bSJamie Gritton struct prison *killpr, *rpr, *ppr, *tpr; 2955c861373bSJamie Gritton struct proc *p; 2956c2cda609SPawel Jakub Dawidek 2957c861373bSJamie Gritton killpr = NULL; 29586e1d1bfcSJamie Gritton TAILQ_INIT(&freeprison); 29596e1d1bfcSJamie Gritton /* 29606e1d1bfcSJamie Gritton * Release this prison as requested, which may cause its parent 29616e1d1bfcSJamie Gritton * to be released, and then maybe its grandparent, etc. 29626e1d1bfcSJamie Gritton */ 29630304c731SJamie Gritton for (;;) { 2964c861373bSJamie Gritton if (flags & PD_KILL) { 2965c861373bSJamie Gritton /* Kill the prison and its descendents. */ 2966589e4c1dSJamie Gritton KASSERT(pr != &prison0, 2967589e4c1dSJamie Gritton ("prison_deref trying to kill prison0")); 2968c861373bSJamie Gritton if (!(flags & PD_DEREF)) { 2969c861373bSJamie Gritton prison_hold(pr); 2970c861373bSJamie Gritton flags |= PD_DEREF; 2971c861373bSJamie Gritton } 2972c861373bSJamie Gritton flags = prison_lock_xlock(pr, flags); 2973c861373bSJamie Gritton prison_deref_kill(pr, &freeprison); 2974c861373bSJamie Gritton } 2975e6d5cb63SJamie Gritton if (flags & PD_DEUREF) { 2976f7496dcaSJamie Gritton /* Drop a user reference. */ 29776754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_uref) > 0, 297873d9e52dSJamie Gritton ("prison_deref PD_DEUREF on a dead prison (jid=%d)", 297973d9e52dSJamie Gritton pr->pr_id)); 2980f7496dcaSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_uref)) { 2981f7496dcaSJamie Gritton if (!(flags & PD_DEREF)) { 2982f7496dcaSJamie Gritton prison_hold(pr); 2983f7496dcaSJamie Gritton flags |= PD_DEREF; 2984f7496dcaSJamie Gritton } 2985f7496dcaSJamie Gritton flags = prison_lock_xlock(pr, flags); 29861158508aSJamie Gritton if (refcount_release(&pr->pr_uref) && 29871158508aSJamie Gritton pr->pr_state == PRISON_STATE_ALIVE) { 2988f7496dcaSJamie Gritton /* 2989f7496dcaSJamie Gritton * When the last user references goes, 2990f7496dcaSJamie Gritton * this becomes a dying prison. 2991f7496dcaSJamie Gritton */ 2992f7496dcaSJamie Gritton KASSERT( 2993f7496dcaSJamie Gritton refcount_load(&prison0.pr_uref) > 0, 29946754ae25SJamie Gritton ("prison0 pr_uref=0")); 29951158508aSJamie Gritton pr->pr_state = PRISON_STATE_DYING; 2996f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 2997f7496dcaSJamie Gritton flags &= ~PD_LOCKED; 2998*a9f7455cSJamie Gritton prison_cleanup(pr); 2999f7496dcaSJamie Gritton } 3000f7496dcaSJamie Gritton } 3001f7496dcaSJamie Gritton } 3002c861373bSJamie Gritton if (flags & PD_KILL) { 3003c861373bSJamie Gritton /* 3004c861373bSJamie Gritton * Any remaining user references are probably processes 3005c861373bSJamie Gritton * that need to be killed, either in this prison or its 3006c861373bSJamie Gritton * descendants. 3007c861373bSJamie Gritton */ 3008c861373bSJamie Gritton if (refcount_load(&pr->pr_uref) > 0) 3009c861373bSJamie Gritton killpr = pr; 3010589e4c1dSJamie Gritton /* Make sure the parent prison doesn't get killed. */ 3011589e4c1dSJamie Gritton flags &= ~PD_KILL; 3012c861373bSJamie Gritton } 301373d9e52dSJamie Gritton if (flags & PD_DEREF) { 3014f7496dcaSJamie Gritton /* Drop a reference. */ 30156754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 301673d9e52dSJamie Gritton ("prison_deref PD_DEREF on a dead prison (jid=%d)", 301773d9e52dSJamie Gritton pr->pr_id)); 3018f7496dcaSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_ref)) { 3019f7496dcaSJamie Gritton flags = prison_lock_xlock(pr, flags); 3020f7496dcaSJamie Gritton if (refcount_release(&pr->pr_ref)) { 3021cc5fd8c7SJamie Gritton /* 3022f7496dcaSJamie Gritton * When the last reference goes, 3023f7496dcaSJamie Gritton * unlink the prison and set it aside. 3024cc5fd8c7SJamie Gritton */ 3025f7496dcaSJamie Gritton KASSERT( 3026f7496dcaSJamie Gritton refcount_load(&pr->pr_uref) == 0, 3027f7496dcaSJamie Gritton ("prison_deref: last ref, " 3028f7496dcaSJamie Gritton "but still has %d urefs (jid=%d)", 3029f7496dcaSJamie Gritton pr->pr_uref, pr->pr_id)); 3030f7496dcaSJamie Gritton KASSERT( 3031f7496dcaSJamie Gritton refcount_load(&prison0.pr_ref) != 0, 3032f7496dcaSJamie Gritton ("prison0 pr_ref=0")); 30331158508aSJamie Gritton pr->pr_state = PRISON_STATE_INVALID; 3034b38ff370SJamie Gritton TAILQ_REMOVE(&allprison, pr, pr_list); 30350304c731SJamie Gritton LIST_REMOVE(pr, pr_sibling); 3036f7496dcaSJamie Gritton TAILQ_INSERT_TAIL(&freeprison, pr, 3037f7496dcaSJamie Gritton pr_list); 3038f7496dcaSJamie Gritton for (ppr = pr->pr_parent; 3039f7496dcaSJamie Gritton ppr != NULL; 3040f7496dcaSJamie Gritton ppr = ppr->pr_parent) 3041f7496dcaSJamie Gritton ppr->pr_childcount--; 3042f7496dcaSJamie Gritton /* 3043f7496dcaSJamie Gritton * Removing a prison frees references 3044f7496dcaSJamie Gritton * from its parent. 3045f7496dcaSJamie Gritton */ 3046f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 3047f7496dcaSJamie Gritton flags &= ~PD_LOCKED; 30486e1d1bfcSJamie Gritton pr = pr->pr_parent; 30496e1d1bfcSJamie Gritton flags |= PD_DEREF | PD_DEUREF; 3050f7496dcaSJamie Gritton continue; 3051f7496dcaSJamie Gritton } 3052f7496dcaSJamie Gritton } 3053f7496dcaSJamie Gritton } 3054f7496dcaSJamie Gritton break; 30556e1d1bfcSJamie Gritton } 30566e1d1bfcSJamie Gritton 30576e1d1bfcSJamie Gritton /* Release all the prison locks. */ 3058f7496dcaSJamie Gritton if (flags & PD_LOCKED) 3059f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 30606e1d1bfcSJamie Gritton if (flags & PD_LIST_SLOCKED) 30616e1d1bfcSJamie Gritton sx_sunlock(&allprison_lock); 30626e1d1bfcSJamie Gritton else if (flags & PD_LIST_XLOCKED) 30636e1d1bfcSJamie Gritton sx_xunlock(&allprison_lock); 30646e1d1bfcSJamie Gritton 3065c861373bSJamie Gritton /* Kill any processes attached to a killed prison. */ 3066c861373bSJamie Gritton if (killpr != NULL) { 3067c861373bSJamie Gritton sx_slock(&allproc_lock); 3068c861373bSJamie Gritton FOREACH_PROC_IN_SYSTEM(p) { 3069c861373bSJamie Gritton PROC_LOCK(p); 3070c861373bSJamie Gritton if (p->p_state != PRS_NEW && p->p_ucred != NULL) { 3071c861373bSJamie Gritton for (ppr = p->p_ucred->cr_prison; 3072c861373bSJamie Gritton ppr != &prison0; 3073c861373bSJamie Gritton ppr = ppr->pr_parent) 3074c861373bSJamie Gritton if (ppr == killpr) { 3075c861373bSJamie Gritton kern_psignal(p, SIGKILL); 3076c861373bSJamie Gritton break; 3077c861373bSJamie Gritton } 3078c861373bSJamie Gritton } 3079c861373bSJamie Gritton PROC_UNLOCK(p); 3080c861373bSJamie Gritton } 3081c861373bSJamie Gritton sx_sunlock(&allproc_lock); 3082c861373bSJamie Gritton } 3083c861373bSJamie Gritton 30846e1d1bfcSJamie Gritton /* 30856e1d1bfcSJamie Gritton * Finish removing any unreferenced prisons, which couldn't happen 30866e1d1bfcSJamie Gritton * while allprison_lock was held (to avoid a LOR on vrele). 30876e1d1bfcSJamie Gritton */ 30886e1d1bfcSJamie Gritton TAILQ_FOREACH_SAFE(rpr, &freeprison, pr_list, tpr) { 30896e1d1bfcSJamie Gritton #ifdef VIMAGE 30906e1d1bfcSJamie Gritton if (rpr->pr_vnet != rpr->pr_parent->pr_vnet) 30916e1d1bfcSJamie Gritton vnet_destroy(rpr->pr_vnet); 30926e1d1bfcSJamie Gritton #endif 30936e1d1bfcSJamie Gritton if (rpr->pr_root != NULL) 30946e1d1bfcSJamie Gritton vrele(rpr->pr_root); 30956e1d1bfcSJamie Gritton mtx_destroy(&rpr->pr_mtx); 30966e1d1bfcSJamie Gritton #ifdef INET 3097eb8dcdeaSGleb Smirnoff prison_ip_free(rpr->pr_addrs[PR_INET]); 30986e1d1bfcSJamie Gritton #endif 30996e1d1bfcSJamie Gritton #ifdef INET6 3100eb8dcdeaSGleb Smirnoff prison_ip_free(rpr->pr_addrs[PR_INET6]); 31016e1d1bfcSJamie Gritton #endif 31026e1d1bfcSJamie Gritton if (rpr->pr_cpuset != NULL) 31036e1d1bfcSJamie Gritton cpuset_rel(rpr->pr_cpuset); 31046e1d1bfcSJamie Gritton osd_jail_exit(rpr); 31056e1d1bfcSJamie Gritton #ifdef RACCT 31066e1d1bfcSJamie Gritton if (racct_enable) 31076e1d1bfcSJamie Gritton prison_racct_detach(rpr); 31086e1d1bfcSJamie Gritton #endif 3109f7496dcaSJamie Gritton TAILQ_REMOVE(&freeprison, rpr, pr_list); 31106e1d1bfcSJamie Gritton free(rpr, M_PRISON); 31110304c731SJamie Gritton } 3112b3059e09SRobert Watson } 3113b3059e09SRobert Watson 3114413628a7SBjoern A. Zeeb /* 3115c861373bSJamie Gritton * Kill the prison and its descendants. Mark them as dying, clear the 3116c861373bSJamie Gritton * persist flag, and call module remove methods. 3117c861373bSJamie Gritton */ 3118c861373bSJamie Gritton static void 3119c861373bSJamie Gritton prison_deref_kill(struct prison *pr, struct prisonlist *freeprison) 3120c861373bSJamie Gritton { 3121c861373bSJamie Gritton struct prison *cpr, *ppr, *rpr; 3122c861373bSJamie Gritton bool descend; 3123c861373bSJamie Gritton 3124c861373bSJamie Gritton /* 3125c861373bSJamie Gritton * Unlike the descendants, the target prison can be killed 3126c861373bSJamie Gritton * even if it is currently dying. This is useful for failed 3127c861373bSJamie Gritton * creation in jail_set(2). 3128c861373bSJamie Gritton */ 3129c861373bSJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 3130c861373bSJamie Gritton ("Trying to kill dead prison %p (jid=%d).", 3131c861373bSJamie Gritton pr, pr->pr_id)); 3132c861373bSJamie Gritton refcount_acquire(&pr->pr_uref); 3133c861373bSJamie Gritton pr->pr_state = PRISON_STATE_DYING; 3134c861373bSJamie Gritton mtx_unlock(&pr->pr_mtx); 3135c861373bSJamie Gritton 3136c861373bSJamie Gritton rpr = NULL; 3137c861373bSJamie Gritton FOREACH_PRISON_DESCENDANT_PRE_POST(pr, cpr, descend) { 3138c861373bSJamie Gritton if (descend) { 3139c861373bSJamie Gritton if (!prison_isalive(cpr)) { 3140c861373bSJamie Gritton descend = false; 3141c861373bSJamie Gritton continue; 3142c861373bSJamie Gritton } 3143c861373bSJamie Gritton prison_hold(cpr); 3144c861373bSJamie Gritton prison_proc_hold(cpr); 3145c861373bSJamie Gritton mtx_lock(&cpr->pr_mtx); 3146c861373bSJamie Gritton cpr->pr_state = PRISON_STATE_DYING; 3147c861373bSJamie Gritton cpr->pr_flags |= PR_REMOVE; 3148c861373bSJamie Gritton mtx_unlock(&cpr->pr_mtx); 3149c861373bSJamie Gritton continue; 3150c861373bSJamie Gritton } 3151c861373bSJamie Gritton if (!(cpr->pr_flags & PR_REMOVE)) 3152c861373bSJamie Gritton continue; 3153*a9f7455cSJamie Gritton prison_cleanup(cpr); 3154c861373bSJamie Gritton mtx_lock(&cpr->pr_mtx); 3155c861373bSJamie Gritton cpr->pr_flags &= ~PR_REMOVE; 3156c861373bSJamie Gritton if (cpr->pr_flags & PR_PERSIST) { 3157c861373bSJamie Gritton cpr->pr_flags &= ~PR_PERSIST; 3158c861373bSJamie Gritton prison_proc_free_not_last(cpr); 3159c861373bSJamie Gritton prison_free_not_last(cpr); 3160c861373bSJamie Gritton } 3161c861373bSJamie Gritton (void)refcount_release(&cpr->pr_uref); 3162c861373bSJamie Gritton if (refcount_release(&cpr->pr_ref)) { 3163c861373bSJamie Gritton /* 3164c861373bSJamie Gritton * When the last reference goes, unlink the prison 3165c861373bSJamie Gritton * and set it aside for prison_deref() to handle. 3166c861373bSJamie Gritton * Delay unlinking the sibling list to keep the loop 3167c861373bSJamie Gritton * safe. 3168c861373bSJamie Gritton */ 3169c861373bSJamie Gritton if (rpr != NULL) 3170c861373bSJamie Gritton LIST_REMOVE(rpr, pr_sibling); 3171c861373bSJamie Gritton rpr = cpr; 3172c861373bSJamie Gritton rpr->pr_state = PRISON_STATE_INVALID; 3173c861373bSJamie Gritton TAILQ_REMOVE(&allprison, rpr, pr_list); 3174c861373bSJamie Gritton TAILQ_INSERT_TAIL(freeprison, rpr, pr_list); 3175c861373bSJamie Gritton /* 3176c861373bSJamie Gritton * Removing a prison frees references from its parent. 3177c861373bSJamie Gritton */ 3178c861373bSJamie Gritton ppr = rpr->pr_parent; 3179c861373bSJamie Gritton prison_proc_free_not_last(ppr); 3180c861373bSJamie Gritton prison_free_not_last(ppr); 3181c861373bSJamie Gritton for (; ppr != NULL; ppr = ppr->pr_parent) 3182c861373bSJamie Gritton ppr->pr_childcount--; 3183c861373bSJamie Gritton } 3184c861373bSJamie Gritton mtx_unlock(&cpr->pr_mtx); 3185c861373bSJamie Gritton } 3186c861373bSJamie Gritton if (rpr != NULL) 3187c861373bSJamie Gritton LIST_REMOVE(rpr, pr_sibling); 3188c861373bSJamie Gritton 3189*a9f7455cSJamie Gritton prison_cleanup(pr); 3190c861373bSJamie Gritton mtx_lock(&pr->pr_mtx); 3191c861373bSJamie Gritton if (pr->pr_flags & PR_PERSIST) { 3192c861373bSJamie Gritton pr->pr_flags &= ~PR_PERSIST; 3193c861373bSJamie Gritton prison_proc_free_not_last(pr); 3194c861373bSJamie Gritton prison_free_not_last(pr); 3195c861373bSJamie Gritton } 3196c861373bSJamie Gritton (void)refcount_release(&pr->pr_uref); 3197c861373bSJamie Gritton } 3198c861373bSJamie Gritton 3199c861373bSJamie Gritton /* 3200f7496dcaSJamie Gritton * Given the current locking state in the flags, make sure allprison_lock 3201f7496dcaSJamie Gritton * is held exclusive, and the prison is locked. Return flags indicating 3202f7496dcaSJamie Gritton * the new state. 3203f7496dcaSJamie Gritton */ 3204f7496dcaSJamie Gritton static int 3205f7496dcaSJamie Gritton prison_lock_xlock(struct prison *pr, int flags) 3206f7496dcaSJamie Gritton { 3207f7496dcaSJamie Gritton 3208f7496dcaSJamie Gritton if (!(flags & PD_LIST_XLOCKED)) { 3209f7496dcaSJamie Gritton /* 3210f7496dcaSJamie Gritton * Get allprison_lock, which may be an upgrade, 3211f7496dcaSJamie Gritton * and may require unlocking the prison. 3212f7496dcaSJamie Gritton */ 3213f7496dcaSJamie Gritton if (flags & PD_LOCKED) { 3214f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 3215f7496dcaSJamie Gritton flags &= ~PD_LOCKED; 3216f7496dcaSJamie Gritton } 3217f7496dcaSJamie Gritton if (flags & PD_LIST_SLOCKED) { 3218f7496dcaSJamie Gritton if (!sx_try_upgrade(&allprison_lock)) { 3219f7496dcaSJamie Gritton sx_sunlock(&allprison_lock); 3220f7496dcaSJamie Gritton sx_xlock(&allprison_lock); 3221f7496dcaSJamie Gritton } 3222f7496dcaSJamie Gritton flags &= ~PD_LIST_SLOCKED; 3223f7496dcaSJamie Gritton } else 3224f7496dcaSJamie Gritton sx_xlock(&allprison_lock); 3225f7496dcaSJamie Gritton flags |= PD_LIST_XLOCKED; 3226f7496dcaSJamie Gritton } 3227f7496dcaSJamie Gritton if (!(flags & PD_LOCKED)) { 3228f7496dcaSJamie Gritton /* Lock the prison mutex. */ 3229f7496dcaSJamie Gritton mtx_lock(&pr->pr_mtx); 3230f7496dcaSJamie Gritton flags |= PD_LOCKED; 3231f7496dcaSJamie Gritton } 3232f7496dcaSJamie Gritton return flags; 3233f7496dcaSJamie Gritton } 3234f7496dcaSJamie Gritton 3235f7496dcaSJamie Gritton /* 3236*a9f7455cSJamie Gritton * Release a prison's resources when it starts dying (when the last user 3237*a9f7455cSJamie Gritton * reference is dropped, or when it is killed). 3238*a9f7455cSJamie Gritton */ 3239*a9f7455cSJamie Gritton static void 3240*a9f7455cSJamie Gritton prison_cleanup(struct prison *pr) 3241*a9f7455cSJamie Gritton { 3242*a9f7455cSJamie Gritton sx_assert(&allprison_lock, SA_XLOCKED); 3243*a9f7455cSJamie Gritton mtx_assert(&pr->pr_mtx, MA_NOTOWNED); 3244*a9f7455cSJamie Gritton (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL); 3245*a9f7455cSJamie Gritton } 3246*a9f7455cSJamie Gritton 3247*a9f7455cSJamie Gritton /* 32480fe74ae6SJamie Gritton * Set or clear a permission bit in the pr_allow field, passing restrictions 32490fe74ae6SJamie Gritton * (cleared permission) down to child jails. 32500fe74ae6SJamie Gritton */ 32510fe74ae6SJamie Gritton void 32520fe74ae6SJamie Gritton prison_set_allow(struct ucred *cred, unsigned flag, int enable) 32530fe74ae6SJamie Gritton { 32540fe74ae6SJamie Gritton struct prison *pr; 32550fe74ae6SJamie Gritton 32560fe74ae6SJamie Gritton pr = cred->cr_prison; 32570fe74ae6SJamie Gritton sx_slock(&allprison_lock); 32580fe74ae6SJamie Gritton mtx_lock(&pr->pr_mtx); 32590fe74ae6SJamie Gritton prison_set_allow_locked(pr, flag, enable); 32600fe74ae6SJamie Gritton mtx_unlock(&pr->pr_mtx); 32610fe74ae6SJamie Gritton sx_sunlock(&allprison_lock); 32620fe74ae6SJamie Gritton } 32630fe74ae6SJamie Gritton 32640fe74ae6SJamie Gritton static void 32650fe74ae6SJamie Gritton prison_set_allow_locked(struct prison *pr, unsigned flag, int enable) 32660fe74ae6SJamie Gritton { 32670fe74ae6SJamie Gritton struct prison *cpr; 32680fe74ae6SJamie Gritton int descend; 32690fe74ae6SJamie Gritton 32700fe74ae6SJamie Gritton if (enable != 0) 32710fe74ae6SJamie Gritton pr->pr_allow |= flag; 32720fe74ae6SJamie Gritton else { 32730fe74ae6SJamie Gritton pr->pr_allow &= ~flag; 32740fe74ae6SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, cpr, descend) 32750fe74ae6SJamie Gritton cpr->pr_allow &= ~flag; 32760fe74ae6SJamie Gritton } 32770fe74ae6SJamie Gritton } 32780fe74ae6SJamie Gritton 32790fe74ae6SJamie Gritton /* 3280ca04ba64SJamie Gritton * Check if a jail supports the given address family. 3281ca04ba64SJamie Gritton * 3282ca04ba64SJamie Gritton * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT 3283ca04ba64SJamie Gritton * if not. 3284ca04ba64SJamie Gritton */ 3285ca04ba64SJamie Gritton int 3286ca04ba64SJamie Gritton prison_check_af(struct ucred *cred, int af) 3287ca04ba64SJamie Gritton { 32880304c731SJamie Gritton struct prison *pr; 3289ca04ba64SJamie Gritton int error; 3290ca04ba64SJamie Gritton 3291ca04ba64SJamie Gritton KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3292ca04ba64SJamie Gritton 32930304c731SJamie Gritton pr = cred->cr_prison; 3294499650a0SJamie Gritton #ifdef VIMAGE 32956bb79563SJamie Gritton /* Prisons with their own network stack are not limited. */ 3296de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 32976bb79563SJamie Gritton return (0); 3298499650a0SJamie Gritton #endif 32996bb79563SJamie Gritton 3300ca04ba64SJamie Gritton error = 0; 3301ca04ba64SJamie Gritton switch (af) 3302ca04ba64SJamie Gritton { 3303ca04ba64SJamie Gritton #ifdef INET 3304ca04ba64SJamie Gritton case AF_INET: 33050304c731SJamie Gritton if (pr->pr_flags & PR_IP4) 33060304c731SJamie Gritton { 33070304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 3308eb8dcdeaSGleb Smirnoff if ((pr->pr_flags & PR_IP4) && 3309eb8dcdeaSGleb Smirnoff pr->pr_addrs[PR_INET] == NULL) 3310ca04ba64SJamie Gritton error = EAFNOSUPPORT; 33110304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 33120304c731SJamie Gritton } 3313ca04ba64SJamie Gritton break; 3314ca04ba64SJamie Gritton #endif 3315ca04ba64SJamie Gritton #ifdef INET6 3316ca04ba64SJamie Gritton case AF_INET6: 33170304c731SJamie Gritton if (pr->pr_flags & PR_IP6) 33180304c731SJamie Gritton { 33190304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 3320eb8dcdeaSGleb Smirnoff if ((pr->pr_flags & PR_IP6) && 3321eb8dcdeaSGleb Smirnoff pr->pr_addrs[PR_INET6] == NULL) 3322ca04ba64SJamie Gritton error = EAFNOSUPPORT; 33230304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 33240304c731SJamie Gritton } 3325ca04ba64SJamie Gritton break; 3326ca04ba64SJamie Gritton #endif 3327ca04ba64SJamie Gritton case AF_LOCAL: 3328ca04ba64SJamie Gritton case AF_ROUTE: 3329ca04ba64SJamie Gritton break; 3330ca04ba64SJamie Gritton default: 33310304c731SJamie Gritton if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF)) 3332ca04ba64SJamie Gritton error = EAFNOSUPPORT; 3333ca04ba64SJamie Gritton } 3334ca04ba64SJamie Gritton return (error); 3335ca04ba64SJamie Gritton } 3336ca04ba64SJamie Gritton 3337ca04ba64SJamie Gritton /* 3338413628a7SBjoern A. Zeeb * Check if given address belongs to the jail referenced by cred (wrapper to 3339413628a7SBjoern A. Zeeb * prison_check_ip[46]). 3340413628a7SBjoern A. Zeeb * 33410304c731SJamie Gritton * Returns 0 if jail doesn't restrict the address family or if address belongs 33420304c731SJamie Gritton * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if 33430304c731SJamie Gritton * the jail doesn't allow the address family. IPv4 Address passed in in NBO. 3344413628a7SBjoern A. Zeeb */ 3345413628a7SBjoern A. Zeeb int 3346c83dda36SAlexander V. Chernikov prison_if(struct ucred *cred, const struct sockaddr *sa) 334775c13541SPoul-Henning Kamp { 3348413628a7SBjoern A. Zeeb #ifdef INET 3349c83dda36SAlexander V. Chernikov const struct sockaddr_in *sai; 3350413628a7SBjoern A. Zeeb #endif 3351413628a7SBjoern A. Zeeb #ifdef INET6 3352c83dda36SAlexander V. Chernikov const struct sockaddr_in6 *sai6; 3353413628a7SBjoern A. Zeeb #endif 3354b89e82ddSJamie Gritton int error; 335575c13541SPoul-Henning Kamp 3356413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3357413628a7SBjoern A. Zeeb KASSERT(sa != NULL, ("%s: sa is NULL", __func__)); 3358413628a7SBjoern A. Zeeb 3359de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE 3360de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 3361de0bd6f7SBjoern A. Zeeb return (0); 3362de0bd6f7SBjoern A. Zeeb #endif 3363de0bd6f7SBjoern A. Zeeb 3364b89e82ddSJamie Gritton error = 0; 3365413628a7SBjoern A. Zeeb switch (sa->sa_family) 3366413628a7SBjoern A. Zeeb { 3367413628a7SBjoern A. Zeeb #ifdef INET 3368413628a7SBjoern A. Zeeb case AF_INET: 3369c83dda36SAlexander V. Chernikov sai = (const struct sockaddr_in *)sa; 3370b89e82ddSJamie Gritton error = prison_check_ip4(cred, &sai->sin_addr); 3371413628a7SBjoern A. Zeeb break; 3372413628a7SBjoern A. Zeeb #endif 3373413628a7SBjoern A. Zeeb #ifdef INET6 3374413628a7SBjoern A. Zeeb case AF_INET6: 3375c83dda36SAlexander V. Chernikov sai6 = (const struct sockaddr_in6 *)sa; 3376b89e82ddSJamie Gritton error = prison_check_ip6(cred, &sai6->sin6_addr); 3377413628a7SBjoern A. Zeeb break; 3378413628a7SBjoern A. Zeeb #endif 3379413628a7SBjoern A. Zeeb default: 33800304c731SJamie Gritton if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF)) 3381b89e82ddSJamie Gritton error = EAFNOSUPPORT; 3382413628a7SBjoern A. Zeeb } 3383b89e82ddSJamie Gritton return (error); 338475c13541SPoul-Henning Kamp } 338591421ba2SRobert Watson 338691421ba2SRobert Watson /* 338791421ba2SRobert Watson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH. 338891421ba2SRobert Watson */ 338991421ba2SRobert Watson int 33909ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2) 339191421ba2SRobert Watson { 339291421ba2SRobert Watson 33930304c731SJamie Gritton return ((cred1->cr_prison == cred2->cr_prison || 33940304c731SJamie Gritton prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH); 33950304c731SJamie Gritton } 339691421ba2SRobert Watson 33970304c731SJamie Gritton /* 33980304c731SJamie Gritton * Return 1 if p2 is a child of p1, otherwise 0. 33990304c731SJamie Gritton */ 34000304c731SJamie Gritton int 34010304c731SJamie Gritton prison_ischild(struct prison *pr1, struct prison *pr2) 34020304c731SJamie Gritton { 34030304c731SJamie Gritton 34040304c731SJamie Gritton for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent) 34050304c731SJamie Gritton if (pr1 == pr2) 34060304c731SJamie Gritton return (1); 340791421ba2SRobert Watson return (0); 340891421ba2SRobert Watson } 340991421ba2SRobert Watson 341091421ba2SRobert Watson /* 3411f7496dcaSJamie Gritton * Return true if the prison is currently alive. A prison is alive if it 3412f7496dcaSJamie Gritton * holds user references and it isn't being removed. 341376ad42abSJamie Gritton */ 341476ad42abSJamie Gritton bool 3415eb8dcdeaSGleb Smirnoff prison_isalive(const struct prison *pr) 341676ad42abSJamie Gritton { 341776ad42abSJamie Gritton 34181158508aSJamie Gritton if (__predict_false(pr->pr_state != PRISON_STATE_ALIVE)) 3419cc7b7306SJamie Gritton return (false); 342076ad42abSJamie Gritton return (true); 342176ad42abSJamie Gritton } 342276ad42abSJamie Gritton 342376ad42abSJamie Gritton /* 342476ad42abSJamie Gritton * Return true if the prison is currently valid. A prison is valid if it has 342576ad42abSJamie Gritton * been fully created, and is not being destroyed. Note that dying prisons 3426f7496dcaSJamie Gritton * are still considered valid. Invalid prisons won't be found under normal 3427f7496dcaSJamie Gritton * circumstances, as they're only put in that state by functions that have 3428f7496dcaSJamie Gritton * an exclusive hold on allprison_lock. 342976ad42abSJamie Gritton */ 343076ad42abSJamie Gritton bool 343176ad42abSJamie Gritton prison_isvalid(struct prison *pr) 343276ad42abSJamie Gritton { 343376ad42abSJamie Gritton 34341158508aSJamie Gritton if (__predict_false(pr->pr_state == PRISON_STATE_INVALID)) 34351158508aSJamie Gritton return (false); 34366754ae25SJamie Gritton if (__predict_false(refcount_load(&pr->pr_ref) == 0)) 343776ad42abSJamie Gritton return (false); 343876ad42abSJamie Gritton return (true); 343976ad42abSJamie Gritton } 344076ad42abSJamie Gritton 344176ad42abSJamie Gritton /* 3442de0bd6f7SBjoern A. Zeeb * Return 1 if the passed credential is in a jail and that jail does not 3443de0bd6f7SBjoern A. Zeeb * have its own virtual network stack, otherwise 0. 3444de0bd6f7SBjoern A. Zeeb */ 3445de0bd6f7SBjoern A. Zeeb int 3446de0bd6f7SBjoern A. Zeeb jailed_without_vnet(struct ucred *cred) 3447de0bd6f7SBjoern A. Zeeb { 3448de0bd6f7SBjoern A. Zeeb 3449de0bd6f7SBjoern A. Zeeb if (!jailed(cred)) 3450de0bd6f7SBjoern A. Zeeb return (0); 3451de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE 3452de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 3453de0bd6f7SBjoern A. Zeeb return (0); 3454de0bd6f7SBjoern A. Zeeb #endif 3455de0bd6f7SBjoern A. Zeeb 3456de0bd6f7SBjoern A. Zeeb return (1); 3457de0bd6f7SBjoern A. Zeeb } 3458de0bd6f7SBjoern A. Zeeb 3459de0bd6f7SBjoern A. Zeeb /* 34607455b100SJamie Gritton * Return the correct hostname (domainname, et al) for the passed credential. 34619484d0c0SRobert Drehmel */ 3462ad1ff099SRobert Drehmel void 34639ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size) 34649484d0c0SRobert Drehmel { 346576ca6f88SJamie Gritton struct prison *pr; 34669484d0c0SRobert Drehmel 34677455b100SJamie Gritton /* 34687455b100SJamie Gritton * A NULL credential can be used to shortcut to the physical 34697455b100SJamie Gritton * system's hostname. 34707455b100SJamie Gritton */ 347176ca6f88SJamie Gritton pr = (cred != NULL) ? cred->cr_prison : &prison0; 347276ca6f88SJamie Gritton mtx_lock(&pr->pr_mtx); 3473c1f19219SJamie Gritton strlcpy(buf, pr->pr_hostname, size); 347476ca6f88SJamie Gritton mtx_unlock(&pr->pr_mtx); 34759484d0c0SRobert Drehmel } 3476fd7a8150SMike Barcroft 34777455b100SJamie Gritton void 34787455b100SJamie Gritton getcreddomainname(struct ucred *cred, char *buf, size_t size) 34797455b100SJamie Gritton { 34807455b100SJamie Gritton 34817455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 3482c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_domainname, size); 34837455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 34847455b100SJamie Gritton } 34857455b100SJamie Gritton 34867455b100SJamie Gritton void 34877455b100SJamie Gritton getcredhostuuid(struct ucred *cred, char *buf, size_t size) 34887455b100SJamie Gritton { 34897455b100SJamie Gritton 34907455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 3491c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_hostuuid, size); 34927455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 34937455b100SJamie Gritton } 34947455b100SJamie Gritton 34957455b100SJamie Gritton void 34967455b100SJamie Gritton getcredhostid(struct ucred *cred, unsigned long *hostid) 34977455b100SJamie Gritton { 34987455b100SJamie Gritton 34997455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 35007455b100SJamie Gritton *hostid = cred->cr_prison->pr_hostid; 35017455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 35027455b100SJamie Gritton } 35037455b100SJamie Gritton 35043f8bc99cSKristof Provost void 35053f8bc99cSKristof Provost getjailname(struct ucred *cred, char *name, size_t len) 35063f8bc99cSKristof Provost { 35073f8bc99cSKristof Provost 35083f8bc99cSKristof Provost mtx_lock(&cred->cr_prison->pr_mtx); 35093f8bc99cSKristof Provost strlcpy(name, cred->cr_prison->pr_name, len); 35103f8bc99cSKristof Provost mtx_unlock(&cred->cr_prison->pr_mtx); 35113f8bc99cSKristof Provost } 35123f8bc99cSKristof Provost 3513eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE 3514eb79e1c7SBjoern A. Zeeb /* 3515eb79e1c7SBjoern A. Zeeb * Determine whether the prison represented by cred owns 3516eb79e1c7SBjoern A. Zeeb * its vnet rather than having it inherited. 3517eb79e1c7SBjoern A. Zeeb * 3518eb79e1c7SBjoern A. Zeeb * Returns 1 in case the prison owns the vnet, 0 otherwise. 3519eb79e1c7SBjoern A. Zeeb */ 3520eb79e1c7SBjoern A. Zeeb int 3521eb79e1c7SBjoern A. Zeeb prison_owns_vnet(struct ucred *cred) 3522eb79e1c7SBjoern A. Zeeb { 3523eb79e1c7SBjoern A. Zeeb 3524eb79e1c7SBjoern A. Zeeb /* 3525eb79e1c7SBjoern A. Zeeb * vnets cannot be added/removed after jail creation, 3526eb79e1c7SBjoern A. Zeeb * so no need to lock here. 3527eb79e1c7SBjoern A. Zeeb */ 3528eb79e1c7SBjoern A. Zeeb return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0); 3529eb79e1c7SBjoern A. Zeeb } 3530eb79e1c7SBjoern A. Zeeb #endif 3531eb79e1c7SBjoern A. Zeeb 3532f08df373SRobert Watson /* 3533820a0de9SPawel Jakub Dawidek * Determine whether the subject represented by cred can "see" 3534820a0de9SPawel Jakub Dawidek * status of a mount point. 3535820a0de9SPawel Jakub Dawidek * Returns: 0 for permitted, ENOENT otherwise. 3536820a0de9SPawel Jakub Dawidek * XXX: This function should be called cr_canseemount() and should be 3537820a0de9SPawel Jakub Dawidek * placed in kern_prot.c. 3538f08df373SRobert Watson */ 3539f08df373SRobert Watson int 3540820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp) 3541f08df373SRobert Watson { 3542820a0de9SPawel Jakub Dawidek struct prison *pr; 3543820a0de9SPawel Jakub Dawidek struct statfs *sp; 3544820a0de9SPawel Jakub Dawidek size_t len; 3545f08df373SRobert Watson 3546820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 35470304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 35480304c731SJamie Gritton return (0); 3549820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) 3550820a0de9SPawel Jakub Dawidek return (0); 35510304c731SJamie Gritton if (pr->pr_enforce_statfs == 2) 3552820a0de9SPawel Jakub Dawidek return (ENOENT); 3553820a0de9SPawel Jakub Dawidek /* 3554820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 3555820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 3556820a0de9SPawel Jakub Dawidek * This is ugly check, but this is the only situation when jail's 3557820a0de9SPawel Jakub Dawidek * directory ends with '/'. 3558820a0de9SPawel Jakub Dawidek */ 3559820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 3560820a0de9SPawel Jakub Dawidek return (0); 3561820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 3562820a0de9SPawel Jakub Dawidek sp = &mp->mnt_stat; 3563820a0de9SPawel Jakub Dawidek if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0) 3564820a0de9SPawel Jakub Dawidek return (ENOENT); 3565820a0de9SPawel Jakub Dawidek /* 3566820a0de9SPawel Jakub Dawidek * Be sure that we don't have situation where jail's root directory 3567820a0de9SPawel Jakub Dawidek * is "/some/path" and mount point is "/some/pathpath". 3568820a0de9SPawel Jakub Dawidek */ 3569820a0de9SPawel Jakub Dawidek if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/') 3570820a0de9SPawel Jakub Dawidek return (ENOENT); 3571f08df373SRobert Watson return (0); 3572f08df373SRobert Watson } 3573820a0de9SPawel Jakub Dawidek 3574820a0de9SPawel Jakub Dawidek void 3575820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp) 3576820a0de9SPawel Jakub Dawidek { 3577820a0de9SPawel Jakub Dawidek char jpath[MAXPATHLEN]; 3578820a0de9SPawel Jakub Dawidek struct prison *pr; 3579820a0de9SPawel Jakub Dawidek size_t len; 3580820a0de9SPawel Jakub Dawidek 3581820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 35820304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 35830304c731SJamie Gritton return; 3584820a0de9SPawel Jakub Dawidek if (prison_canseemount(cred, mp) != 0) { 3585820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3586820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, "[restricted]", 3587820a0de9SPawel Jakub Dawidek sizeof(sp->f_mntonname)); 3588820a0de9SPawel Jakub Dawidek return; 3589820a0de9SPawel Jakub Dawidek } 3590820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) { 3591820a0de9SPawel Jakub Dawidek /* 3592820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3593820a0de9SPawel Jakub Dawidek * the valid path left there. 3594820a0de9SPawel Jakub Dawidek */ 3595820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3596820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3597820a0de9SPawel Jakub Dawidek return; 3598820a0de9SPawel Jakub Dawidek } 3599820a0de9SPawel Jakub Dawidek /* 3600820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 3601820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 3602820a0de9SPawel Jakub Dawidek */ 3603820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 3604820a0de9SPawel Jakub Dawidek return; 3605820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 3606820a0de9SPawel Jakub Dawidek strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath)); 3607820a0de9SPawel Jakub Dawidek /* 3608820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3609820a0de9SPawel Jakub Dawidek * the valid path left there. 3610820a0de9SPawel Jakub Dawidek */ 3611820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3612820a0de9SPawel Jakub Dawidek if (*jpath == '\0') { 3613820a0de9SPawel Jakub Dawidek /* Should never happen. */ 3614820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3615820a0de9SPawel Jakub Dawidek } else { 3616820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname)); 3617820a0de9SPawel Jakub Dawidek } 3618f08df373SRobert Watson } 3619f08df373SRobert Watson 3620800c9408SRobert Watson /* 3621800c9408SRobert Watson * Check with permission for a specific privilege is granted within jail. We 3622800c9408SRobert Watson * have a specific list of accepted privileges; the rest are denied. 3623800c9408SRobert Watson */ 3624800c9408SRobert Watson int 3625800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv) 3626800c9408SRobert Watson { 36270fe74ae6SJamie Gritton struct prison *pr; 36280fe74ae6SJamie Gritton int error; 3629800c9408SRobert Watson 36307b2ff0dcSMateusz Guzik /* 36317b2ff0dcSMateusz Guzik * Some policies have custom handlers. This routine should not be 36327b2ff0dcSMateusz Guzik * called for them. See priv_check_cred(). 36337b2ff0dcSMateusz Guzik */ 36347b2ff0dcSMateusz Guzik switch (priv) { 3635a459a6cfSMateusz Guzik case PRIV_VFS_LOOKUP: 36367b2ff0dcSMateusz Guzik case PRIV_VFS_GENERATION: 36377b2ff0dcSMateusz Guzik KASSERT(0, ("prison_priv_check instead of a custom handler " 36387b2ff0dcSMateusz Guzik "called for %d\n", priv)); 36397b2ff0dcSMateusz Guzik } 36407b2ff0dcSMateusz Guzik 3641800c9408SRobert Watson if (!jailed(cred)) 3642800c9408SRobert Watson return (0); 3643800c9408SRobert Watson 36446bb79563SJamie Gritton #ifdef VIMAGE 36456bb79563SJamie Gritton /* 36466bb79563SJamie Gritton * Privileges specific to prisons with a virtual network stack. 36476bb79563SJamie Gritton * There might be a duplicate entry here in case the privilege 36486bb79563SJamie Gritton * is only granted conditionally in the legacy jail case. 36496bb79563SJamie Gritton */ 36506bb79563SJamie Gritton switch (priv) { 36516bb79563SJamie Gritton #ifdef notyet 36526bb79563SJamie Gritton /* 36536bb79563SJamie Gritton * NFS-specific privileges. 36546bb79563SJamie Gritton */ 36556bb79563SJamie Gritton case PRIV_NFS_DAEMON: 36566bb79563SJamie Gritton case PRIV_NFS_LOCKD: 36576bb79563SJamie Gritton #endif 36586bb79563SJamie Gritton /* 36596bb79563SJamie Gritton * Network stack privileges. 36606bb79563SJamie Gritton */ 36616bb79563SJamie Gritton case PRIV_NET_BRIDGE: 36626bb79563SJamie Gritton case PRIV_NET_GRE: 36636bb79563SJamie Gritton case PRIV_NET_BPF: 36646bb79563SJamie Gritton case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */ 36656bb79563SJamie Gritton case PRIV_NET_ROUTE: 36666bb79563SJamie Gritton case PRIV_NET_TAP: 36676bb79563SJamie Gritton case PRIV_NET_SETIFMTU: 36686bb79563SJamie Gritton case PRIV_NET_SETIFFLAGS: 36696bb79563SJamie Gritton case PRIV_NET_SETIFCAP: 3670215940b3SXin LI case PRIV_NET_SETIFDESCR: 36716bb79563SJamie Gritton case PRIV_NET_SETIFNAME : 36726bb79563SJamie Gritton case PRIV_NET_SETIFMETRIC: 36736bb79563SJamie Gritton case PRIV_NET_SETIFPHYS: 36746bb79563SJamie Gritton case PRIV_NET_SETIFMAC: 3675389474c1SKonstantin Belousov case PRIV_NET_SETLANPCP: 36766bb79563SJamie Gritton case PRIV_NET_ADDMULTI: 36776bb79563SJamie Gritton case PRIV_NET_DELMULTI: 36786bb79563SJamie Gritton case PRIV_NET_HWIOCTL: 36796bb79563SJamie Gritton case PRIV_NET_SETLLADDR: 36806bb79563SJamie Gritton case PRIV_NET_ADDIFGROUP: 36816bb79563SJamie Gritton case PRIV_NET_DELIFGROUP: 36826bb79563SJamie Gritton case PRIV_NET_IFCREATE: 36836bb79563SJamie Gritton case PRIV_NET_IFDESTROY: 36846bb79563SJamie Gritton case PRIV_NET_ADDIFADDR: 36856bb79563SJamie Gritton case PRIV_NET_DELIFADDR: 36866bb79563SJamie Gritton case PRIV_NET_LAGG: 36876bb79563SJamie Gritton case PRIV_NET_GIF: 36886bb79563SJamie Gritton case PRIV_NET_SETIFVNET: 368935fd7bc0SBjoern A. Zeeb case PRIV_NET_SETIFFIB: 3690ab91feabSKristof Provost case PRIV_NET_OVPN: 36916bb79563SJamie Gritton 36926bb79563SJamie Gritton /* 36936bb79563SJamie Gritton * 802.11-related privileges. 36946bb79563SJamie Gritton */ 3695f7d38a13SAdrian Chadd case PRIV_NET80211_VAP_GETKEY: 3696f7d38a13SAdrian Chadd case PRIV_NET80211_VAP_MANAGE: 36976bb79563SJamie Gritton 36986bb79563SJamie Gritton #ifdef notyet 36996bb79563SJamie Gritton /* 37006bb79563SJamie Gritton * ATM privileges. 37016bb79563SJamie Gritton */ 37026bb79563SJamie Gritton case PRIV_NETATM_CFG: 37036bb79563SJamie Gritton case PRIV_NETATM_ADD: 37046bb79563SJamie Gritton case PRIV_NETATM_DEL: 37056bb79563SJamie Gritton case PRIV_NETATM_SET: 37066bb79563SJamie Gritton 37076bb79563SJamie Gritton /* 37086bb79563SJamie Gritton * Bluetooth privileges. 37096bb79563SJamie Gritton */ 37106bb79563SJamie Gritton case PRIV_NETBLUETOOTH_RAW: 37116bb79563SJamie Gritton #endif 37126bb79563SJamie Gritton 37136bb79563SJamie Gritton /* 37146bb79563SJamie Gritton * Netgraph and netgraph module privileges. 37156bb79563SJamie Gritton */ 37166bb79563SJamie Gritton case PRIV_NETGRAPH_CONTROL: 37176bb79563SJamie Gritton #ifdef notyet 37186bb79563SJamie Gritton case PRIV_NETGRAPH_TTY: 37196bb79563SJamie Gritton #endif 37206bb79563SJamie Gritton 37216bb79563SJamie Gritton /* 37226bb79563SJamie Gritton * IPv4 and IPv6 privileges. 37236bb79563SJamie Gritton */ 37246bb79563SJamie Gritton case PRIV_NETINET_IPFW: 37256bb79563SJamie Gritton case PRIV_NETINET_DIVERT: 37266bb79563SJamie Gritton case PRIV_NETINET_PF: 37276bb79563SJamie Gritton case PRIV_NETINET_DUMMYNET: 37286bb79563SJamie Gritton case PRIV_NETINET_CARP: 37296bb79563SJamie Gritton case PRIV_NETINET_MROUTE: 37306bb79563SJamie Gritton case PRIV_NETINET_RAW: 37316bb79563SJamie Gritton case PRIV_NETINET_ADDRCTRL6: 37326bb79563SJamie Gritton case PRIV_NETINET_ND6: 37336bb79563SJamie Gritton case PRIV_NETINET_SCOPE6: 37346bb79563SJamie Gritton case PRIV_NETINET_ALIFETIME6: 37356bb79563SJamie Gritton case PRIV_NETINET_IPSEC: 37366bb79563SJamie Gritton case PRIV_NETINET_BINDANY: 37376bb79563SJamie Gritton 37386bb79563SJamie Gritton #ifdef notyet 37396bb79563SJamie Gritton /* 37406bb79563SJamie Gritton * NCP privileges. 37416bb79563SJamie Gritton */ 37426bb79563SJamie Gritton case PRIV_NETNCP: 37436bb79563SJamie Gritton 37446bb79563SJamie Gritton /* 37456bb79563SJamie Gritton * SMB privileges. 37466bb79563SJamie Gritton */ 37476bb79563SJamie Gritton case PRIV_NETSMB: 37486bb79563SJamie Gritton #endif 37496bb79563SJamie Gritton 37506bb79563SJamie Gritton /* 37516bb79563SJamie Gritton * No default: or deny here. 37526bb79563SJamie Gritton * In case of no permit fall through to next switch(). 37536bb79563SJamie Gritton */ 37546bb79563SJamie Gritton if (cred->cr_prison->pr_flags & PR_VNET) 37556bb79563SJamie Gritton return (0); 37566bb79563SJamie Gritton } 37576bb79563SJamie Gritton #endif /* VIMAGE */ 37586bb79563SJamie Gritton 3759800c9408SRobert Watson switch (priv) { 3760800c9408SRobert Watson /* 3761800c9408SRobert Watson * Allow ktrace privileges for root in jail. 3762800c9408SRobert Watson */ 3763800c9408SRobert Watson case PRIV_KTRACE: 3764800c9408SRobert Watson 3765c3c1b5e6SRobert Watson #if 0 3766800c9408SRobert Watson /* 3767800c9408SRobert Watson * Allow jailed processes to configure audit identity and 3768800c9408SRobert Watson * submit audit records (login, etc). In the future we may 3769800c9408SRobert Watson * want to further refine the relationship between audit and 3770800c9408SRobert Watson * jail. 3771800c9408SRobert Watson */ 3772800c9408SRobert Watson case PRIV_AUDIT_GETAUDIT: 3773800c9408SRobert Watson case PRIV_AUDIT_SETAUDIT: 3774800c9408SRobert Watson case PRIV_AUDIT_SUBMIT: 3775c3c1b5e6SRobert Watson #endif 3776800c9408SRobert Watson 3777800c9408SRobert Watson /* 3778800c9408SRobert Watson * Allow jailed processes to manipulate process UNIX 3779800c9408SRobert Watson * credentials in any way they see fit. 3780800c9408SRobert Watson */ 3781800c9408SRobert Watson case PRIV_CRED_SETUID: 3782800c9408SRobert Watson case PRIV_CRED_SETEUID: 3783800c9408SRobert Watson case PRIV_CRED_SETGID: 3784800c9408SRobert Watson case PRIV_CRED_SETEGID: 3785800c9408SRobert Watson case PRIV_CRED_SETGROUPS: 3786800c9408SRobert Watson case PRIV_CRED_SETREUID: 3787800c9408SRobert Watson case PRIV_CRED_SETREGID: 3788800c9408SRobert Watson case PRIV_CRED_SETRESUID: 3789800c9408SRobert Watson case PRIV_CRED_SETRESGID: 3790800c9408SRobert Watson 3791800c9408SRobert Watson /* 3792800c9408SRobert Watson * Jail implements visibility constraints already, so allow 3793800c9408SRobert Watson * jailed root to override uid/gid-based constraints. 3794800c9408SRobert Watson */ 3795800c9408SRobert Watson case PRIV_SEEOTHERGIDS: 3796800c9408SRobert Watson case PRIV_SEEOTHERUIDS: 3797800c9408SRobert Watson 3798800c9408SRobert Watson /* 3799800c9408SRobert Watson * Jail implements inter-process debugging limits already, so 3800800c9408SRobert Watson * allow jailed root various debugging privileges. 3801800c9408SRobert Watson */ 3802800c9408SRobert Watson case PRIV_DEBUG_DIFFCRED: 3803800c9408SRobert Watson case PRIV_DEBUG_SUGID: 3804800c9408SRobert Watson case PRIV_DEBUG_UNPRIV: 3805800c9408SRobert Watson 3806800c9408SRobert Watson /* 3807800c9408SRobert Watson * Allow jail to set various resource limits and login 3808800c9408SRobert Watson * properties, and for now, exceed process resource limits. 3809800c9408SRobert Watson */ 3810800c9408SRobert Watson case PRIV_PROC_LIMIT: 3811800c9408SRobert Watson case PRIV_PROC_SETLOGIN: 3812800c9408SRobert Watson case PRIV_PROC_SETRLIMIT: 3813800c9408SRobert Watson 3814800c9408SRobert Watson /* 3815800c9408SRobert Watson * System V and POSIX IPC privileges are granted in jail. 3816800c9408SRobert Watson */ 3817800c9408SRobert Watson case PRIV_IPC_READ: 3818800c9408SRobert Watson case PRIV_IPC_WRITE: 3819800c9408SRobert Watson case PRIV_IPC_ADMIN: 3820800c9408SRobert Watson case PRIV_IPC_MSGSIZE: 3821800c9408SRobert Watson case PRIV_MQ_ADMIN: 3822800c9408SRobert Watson 3823800c9408SRobert Watson /* 38240304c731SJamie Gritton * Jail operations within a jail work on child jails. 38250304c731SJamie Gritton */ 38260304c731SJamie Gritton case PRIV_JAIL_ATTACH: 38270304c731SJamie Gritton case PRIV_JAIL_SET: 38280304c731SJamie Gritton case PRIV_JAIL_REMOVE: 38290304c731SJamie Gritton 38300304c731SJamie Gritton /* 3831800c9408SRobert Watson * Jail implements its own inter-process limits, so allow 3832800c9408SRobert Watson * root processes in jail to change scheduling on other 3833800c9408SRobert Watson * processes in the same jail. Likewise for signalling. 3834800c9408SRobert Watson */ 3835800c9408SRobert Watson case PRIV_SCHED_DIFFCRED: 3836413628a7SBjoern A. Zeeb case PRIV_SCHED_CPUSET: 3837800c9408SRobert Watson case PRIV_SIGNAL_DIFFCRED: 3838800c9408SRobert Watson case PRIV_SIGNAL_SUGID: 3839800c9408SRobert Watson 3840800c9408SRobert Watson /* 3841800c9408SRobert Watson * Allow jailed processes to write to sysctls marked as jail 3842800c9408SRobert Watson * writable. 3843800c9408SRobert Watson */ 3844800c9408SRobert Watson case PRIV_SYSCTL_WRITEJAIL: 3845800c9408SRobert Watson 3846800c9408SRobert Watson /* 3847800c9408SRobert Watson * Allow root in jail to manage a variety of quota 3848e82d0201SRobert Watson * properties. These should likely be conditional on a 3849e82d0201SRobert Watson * configuration option. 3850800c9408SRobert Watson */ 385195b091d2SRobert Watson case PRIV_VFS_GETQUOTA: 385295b091d2SRobert Watson case PRIV_VFS_SETQUOTA: 3853800c9408SRobert Watson 3854800c9408SRobert Watson /* 3855800c9408SRobert Watson * Since Jail relies on chroot() to implement file system 3856800c9408SRobert Watson * protections, grant many VFS privileges to root in jail. 3857800c9408SRobert Watson * Be careful to exclude mount-related and NFS-related 3858800c9408SRobert Watson * privileges. 3859800c9408SRobert Watson */ 3860800c9408SRobert Watson case PRIV_VFS_READ: 3861800c9408SRobert Watson case PRIV_VFS_WRITE: 3862800c9408SRobert Watson case PRIV_VFS_ADMIN: 3863800c9408SRobert Watson case PRIV_VFS_EXEC: 3864800c9408SRobert Watson case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */ 3865800c9408SRobert Watson case PRIV_VFS_CHFLAGS_DEV: 3866800c9408SRobert Watson case PRIV_VFS_CHOWN: 3867800c9408SRobert Watson case PRIV_VFS_CHROOT: 3868bb531912SPawel Jakub Dawidek case PRIV_VFS_RETAINSUGID: 3869800c9408SRobert Watson case PRIV_VFS_FCHROOT: 3870800c9408SRobert Watson case PRIV_VFS_LINK: 3871800c9408SRobert Watson case PRIV_VFS_SETGID: 3872e41966dcSRobert Watson case PRIV_VFS_STAT: 3873800c9408SRobert Watson case PRIV_VFS_STICKYFILE: 3874bb56d716SJamie Gritton 3875bb56d716SJamie Gritton /* 3876bb56d716SJamie Gritton * As in the non-jail case, non-root users are expected to be 387770de1003SGordon Bergling * able to read kernel/physical memory (provided /dev/[k]mem 3878bb56d716SJamie Gritton * exists in the jail and they have permission to access it). 3879bb56d716SJamie Gritton */ 3880bb56d716SJamie Gritton case PRIV_KMEM_READ: 3881800c9408SRobert Watson return (0); 3882800c9408SRobert Watson 3883800c9408SRobert Watson /* 3884800c9408SRobert Watson * Depending on the global setting, allow privilege of 3885800c9408SRobert Watson * setting system flags. 3886800c9408SRobert Watson */ 3887800c9408SRobert Watson case PRIV_VFS_SYSFLAGS: 38880304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS) 3889800c9408SRobert Watson return (0); 3890800c9408SRobert Watson else 3891800c9408SRobert Watson return (EPERM); 3892800c9408SRobert Watson 3893800c9408SRobert Watson /* 3894f3a8d2f9SPawel Jakub Dawidek * Depending on the global setting, allow privilege of 3895f3a8d2f9SPawel Jakub Dawidek * mounting/unmounting file systems. 3896f3a8d2f9SPawel Jakub Dawidek */ 3897f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT: 3898f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_UNMOUNT: 3899f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT_NONUSER: 390024b0502eSPawel Jakub Dawidek case PRIV_VFS_MOUNT_OWNER: 39010fe74ae6SJamie Gritton pr = cred->cr_prison; 39020fe74ae6SJamie Gritton prison_lock(pr); 39030fe74ae6SJamie Gritton if (pr->pr_allow & PR_ALLOW_MOUNT && pr->pr_enforce_statfs < 2) 39040fe74ae6SJamie Gritton error = 0; 3905f3a8d2f9SPawel Jakub Dawidek else 39060fe74ae6SJamie Gritton error = EPERM; 39070fe74ae6SJamie Gritton prison_unlock(pr); 39080fe74ae6SJamie Gritton return (error); 3909f3a8d2f9SPawel Jakub Dawidek 3910f3a8d2f9SPawel Jakub Dawidek /* 391163619b6dSKyle Evans * Jails should hold no disposition on the PRIV_VFS_READ_DIR 391263619b6dSKyle Evans * policy. priv_check_cred will not specifically allow it, and 391363619b6dSKyle Evans * we may want a MAC policy to allow it. 391463619b6dSKyle Evans */ 391563619b6dSKyle Evans case PRIV_VFS_READ_DIR: 391663619b6dSKyle Evans return (0); 391763619b6dSKyle Evans 391863619b6dSKyle Evans /* 3919ccd6ac9fSAntoine Brodin * Conditionnaly allow locking (unlocking) physical pages 3920ccd6ac9fSAntoine Brodin * in memory. 3921ccd6ac9fSAntoine Brodin */ 3922ccd6ac9fSAntoine Brodin case PRIV_VM_MLOCK: 3923ccd6ac9fSAntoine Brodin case PRIV_VM_MUNLOCK: 3924ccd6ac9fSAntoine Brodin if (cred->cr_prison->pr_allow & PR_ALLOW_MLOCK) 3925ccd6ac9fSAntoine Brodin return (0); 3926ccd6ac9fSAntoine Brodin else 3927ccd6ac9fSAntoine Brodin return (EPERM); 3928ccd6ac9fSAntoine Brodin 3929ccd6ac9fSAntoine Brodin /* 3930e28f9b7dSAllan Jude * Conditionally allow jailed root to bind reserved ports. 3931800c9408SRobert Watson */ 3932800c9408SRobert Watson case PRIV_NETINET_RESERVEDPORT: 3933e28f9b7dSAllan Jude if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS) 3934e28f9b7dSAllan Jude return (0); 3935e28f9b7dSAllan Jude else 3936e28f9b7dSAllan Jude return (EPERM); 3937e28f9b7dSAllan Jude 3938e28f9b7dSAllan Jude /* 3939e28f9b7dSAllan Jude * Allow jailed root to reuse in-use ports. 3940e28f9b7dSAllan Jude */ 39414b084056SRobert Watson case PRIV_NETINET_REUSEPORT: 3942800c9408SRobert Watson return (0); 3943800c9408SRobert Watson 3944800c9408SRobert Watson /* 3945e3043798SPedro F. Giffuni * Allow jailed root to set certain IPv4/6 (option) headers. 394679ba3952SBjoern A. Zeeb */ 394779ba3952SBjoern A. Zeeb case PRIV_NETINET_SETHDROPTS: 394879ba3952SBjoern A. Zeeb return (0); 394979ba3952SBjoern A. Zeeb 395079ba3952SBjoern A. Zeeb /* 3951800c9408SRobert Watson * Conditionally allow creating raw sockets in jail. 3952800c9408SRobert Watson */ 3953800c9408SRobert Watson case PRIV_NETINET_RAW: 39540304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS) 3955800c9408SRobert Watson return (0); 3956800c9408SRobert Watson else 3957800c9408SRobert Watson return (EPERM); 3958800c9408SRobert Watson 3959800c9408SRobert Watson /* 3960800c9408SRobert Watson * Since jail implements its own visibility limits on netstat 3961800c9408SRobert Watson * sysctls, allow getcred. This allows identd to work in 3962800c9408SRobert Watson * jail. 3963800c9408SRobert Watson */ 3964800c9408SRobert Watson case PRIV_NETINET_GETCRED: 3965800c9408SRobert Watson return (0); 3966800c9408SRobert Watson 39672bfc50bcSEdward Tomasz Napierala /* 39682bfc50bcSEdward Tomasz Napierala * Allow jailed root to set loginclass. 39692bfc50bcSEdward Tomasz Napierala */ 39702bfc50bcSEdward Tomasz Napierala case PRIV_PROC_SETLOGINCLASS: 39712bfc50bcSEdward Tomasz Napierala return (0); 39722bfc50bcSEdward Tomasz Napierala 3973b19d66fdSJamie Gritton /* 39744520f617SJamie Gritton * Do not allow a process inside a jail to read the kernel 3975b19d66fdSJamie Gritton * message buffer unless explicitly permitted. 3976b19d66fdSJamie Gritton */ 3977b19d66fdSJamie Gritton case PRIV_MSGBUF: 3978b19d66fdSJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_READ_MSGBUF) 3979b19d66fdSJamie Gritton return (0); 3980b19d66fdSJamie Gritton return (EPERM); 3981b19d66fdSJamie Gritton 3982800c9408SRobert Watson default: 3983800c9408SRobert Watson /* 3984800c9408SRobert Watson * In all remaining cases, deny the privilege request. This 3985800c9408SRobert Watson * includes almost all network privileges, many system 3986800c9408SRobert Watson * configuration privileges. 3987800c9408SRobert Watson */ 3988800c9408SRobert Watson return (EPERM); 3989800c9408SRobert Watson } 3990800c9408SRobert Watson } 3991800c9408SRobert Watson 39920304c731SJamie Gritton /* 39930304c731SJamie Gritton * Return the part of pr2's name that is relative to pr1, or the whole name 39940304c731SJamie Gritton * if it does not directly follow. 39950304c731SJamie Gritton */ 39960304c731SJamie Gritton 39970304c731SJamie Gritton char * 39980304c731SJamie Gritton prison_name(struct prison *pr1, struct prison *pr2) 39990304c731SJamie Gritton { 40000304c731SJamie Gritton char *name; 40010304c731SJamie Gritton 40020304c731SJamie Gritton /* Jails see themselves as "0" (if they see themselves at all). */ 40030304c731SJamie Gritton if (pr1 == pr2) 40040304c731SJamie Gritton return "0"; 40050304c731SJamie Gritton name = pr2->pr_name; 40060304c731SJamie Gritton if (prison_ischild(pr1, pr2)) { 40070304c731SJamie Gritton /* 40080304c731SJamie Gritton * pr1 isn't locked (and allprison_lock may not be either) 40090304c731SJamie Gritton * so its length can't be counted on. But the number of dots 40100304c731SJamie Gritton * can be counted on - and counted. 40110304c731SJamie Gritton */ 40120304c731SJamie Gritton for (; pr1 != &prison0; pr1 = pr1->pr_parent) 40130304c731SJamie Gritton name = strchr(name, '.') + 1; 40140304c731SJamie Gritton } 40150304c731SJamie Gritton return (name); 40160304c731SJamie Gritton } 40170304c731SJamie Gritton 40180304c731SJamie Gritton /* 40190304c731SJamie Gritton * Return the part of pr2's path that is relative to pr1, or the whole path 40200304c731SJamie Gritton * if it does not directly follow. 40210304c731SJamie Gritton */ 40220304c731SJamie Gritton static char * 40230304c731SJamie Gritton prison_path(struct prison *pr1, struct prison *pr2) 40240304c731SJamie Gritton { 40250304c731SJamie Gritton char *path1, *path2; 40260304c731SJamie Gritton int len1; 40270304c731SJamie Gritton 40280304c731SJamie Gritton path1 = pr1->pr_path; 40290304c731SJamie Gritton path2 = pr2->pr_path; 40300304c731SJamie Gritton if (!strcmp(path1, "/")) 40310304c731SJamie Gritton return (path2); 40320304c731SJamie Gritton len1 = strlen(path1); 40330304c731SJamie Gritton if (strncmp(path1, path2, len1)) 40340304c731SJamie Gritton return (path2); 40350304c731SJamie Gritton if (path2[len1] == '\0') 40360304c731SJamie Gritton return "/"; 40370304c731SJamie Gritton if (path2[len1] == '/') 40380304c731SJamie Gritton return (path2 + len1); 40390304c731SJamie Gritton return (path2); 40400304c731SJamie Gritton } 40410304c731SJamie Gritton 40420304c731SJamie Gritton /* 40430304c731SJamie Gritton * Jail-related sysctls. 40440304c731SJamie Gritton */ 40457029da5cSPawel Biernacki static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 40460304c731SJamie Gritton "Jails"); 40470304c731SJamie Gritton 4048eb8dcdeaSGleb Smirnoff #if defined(INET) || defined(INET6) 4049eb8dcdeaSGleb Smirnoff /* 4050eb8dcdeaSGleb Smirnoff * Copy address array to memory that would be then SYSCTL_OUT-ed. 4051eb8dcdeaSGleb Smirnoff * sysctl_jail_list() helper. 4052eb8dcdeaSGleb Smirnoff */ 4053eb8dcdeaSGleb Smirnoff static void 4054eb8dcdeaSGleb Smirnoff prison_ip_copyout(struct prison *pr, const pr_family_t af, void **out, int *len) 4055eb8dcdeaSGleb Smirnoff { 4056eb8dcdeaSGleb Smirnoff const size_t size = pr_families[af].size; 4057eb8dcdeaSGleb Smirnoff 4058eb8dcdeaSGleb Smirnoff again: 4059eb8dcdeaSGleb Smirnoff mtx_assert(&pr->pr_mtx, MA_OWNED); 4060eb8dcdeaSGleb Smirnoff if (pr->pr_addrs[af] != NULL) { 4061eb8dcdeaSGleb Smirnoff if (*len < pr->pr_addrs[af]->ips) { 4062eb8dcdeaSGleb Smirnoff *len = pr->pr_addrs[af]->ips; 4063eb8dcdeaSGleb Smirnoff mtx_unlock(&pr->pr_mtx); 4064eb8dcdeaSGleb Smirnoff *out = realloc(*out, *len * size, M_TEMP, M_WAITOK); 4065eb8dcdeaSGleb Smirnoff mtx_lock(&pr->pr_mtx); 4066eb8dcdeaSGleb Smirnoff goto again; 4067eb8dcdeaSGleb Smirnoff } 4068eb8dcdeaSGleb Smirnoff bcopy(pr->pr_addrs[af] + 1, *out, pr->pr_addrs[af]->ips * size); 4069eb8dcdeaSGleb Smirnoff } 4070eb8dcdeaSGleb Smirnoff } 4071eb8dcdeaSGleb Smirnoff #endif 4072eb8dcdeaSGleb Smirnoff 4073fd7a8150SMike Barcroft static int 4074fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS) 4075fd7a8150SMike Barcroft { 4076b38ff370SJamie Gritton struct xprison *xp; 40770304c731SJamie Gritton struct prison *pr, *cpr; 4078b38ff370SJamie Gritton #ifdef INET 4079b38ff370SJamie Gritton struct in_addr *ip4 = NULL; 4080b38ff370SJamie Gritton int ip4s = 0; 4081b38ff370SJamie Gritton #endif 4082b38ff370SJamie Gritton #ifdef INET6 40833beefaedSColin Percival struct in6_addr *ip6 = NULL; 4084b38ff370SJamie Gritton int ip6s = 0; 4085b38ff370SJamie Gritton #endif 40860304c731SJamie Gritton int descend, error; 4087fd7a8150SMike Barcroft 4088b38ff370SJamie Gritton xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK); 40890304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 4090b38ff370SJamie Gritton error = 0; 4091dc68a633SPawel Jakub Dawidek sx_slock(&allprison_lock); 40920304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 40930304c731SJamie Gritton mtx_lock(&cpr->pr_mtx); 4094413628a7SBjoern A. Zeeb #ifdef INET 4095eb8dcdeaSGleb Smirnoff prison_ip_copyout(cpr, PR_INET, (void **)&ip4, &ip4s); 4096413628a7SBjoern A. Zeeb #endif 4097413628a7SBjoern A. Zeeb #ifdef INET6 4098eb8dcdeaSGleb Smirnoff prison_ip_copyout(cpr, PR_INET6, (void **)&ip6, &ip6s); 4099b38ff370SJamie Gritton #endif 4100b38ff370SJamie Gritton bzero(xp, sizeof(*xp)); 4101fd7a8150SMike Barcroft xp->pr_version = XPRISON_VERSION; 41020304c731SJamie Gritton xp->pr_id = cpr->pr_id; 41031158508aSJamie Gritton xp->pr_state = cpr->pr_state; 41040304c731SJamie Gritton strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path)); 4105c1f19219SJamie Gritton strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host)); 41060304c731SJamie Gritton strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name)); 4107413628a7SBjoern A. Zeeb #ifdef INET 4108eb8dcdeaSGleb Smirnoff xp->pr_ip4s = ip4s; 4109413628a7SBjoern A. Zeeb #endif 4110413628a7SBjoern A. Zeeb #ifdef INET6 4111eb8dcdeaSGleb Smirnoff xp->pr_ip6s = ip6s; 4112413628a7SBjoern A. Zeeb #endif 41130304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 4114b38ff370SJamie Gritton error = SYSCTL_OUT(req, xp, sizeof(*xp)); 4115b38ff370SJamie Gritton if (error) 4116b38ff370SJamie Gritton break; 4117413628a7SBjoern A. Zeeb #ifdef INET 4118b38ff370SJamie Gritton if (xp->pr_ip4s > 0) { 4119b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip4, 4120b38ff370SJamie Gritton xp->pr_ip4s * sizeof(struct in_addr)); 4121b38ff370SJamie Gritton if (error) 4122b38ff370SJamie Gritton break; 4123413628a7SBjoern A. Zeeb } 4124413628a7SBjoern A. Zeeb #endif 4125413628a7SBjoern A. Zeeb #ifdef INET6 4126b38ff370SJamie Gritton if (xp->pr_ip6s > 0) { 4127b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip6, 4128b38ff370SJamie Gritton xp->pr_ip6s * sizeof(struct in6_addr)); 4129b38ff370SJamie Gritton if (error) 4130b38ff370SJamie Gritton break; 4131413628a7SBjoern A. Zeeb } 4132413628a7SBjoern A. Zeeb #endif 4133fd7a8150SMike Barcroft } 4134dc68a633SPawel Jakub Dawidek sx_sunlock(&allprison_lock); 4135b38ff370SJamie Gritton free(xp, M_TEMP); 4136b38ff370SJamie Gritton #ifdef INET 4137b38ff370SJamie Gritton free(ip4, M_TEMP); 4138b38ff370SJamie Gritton #endif 4139b38ff370SJamie Gritton #ifdef INET6 4140b38ff370SJamie Gritton free(ip6, M_TEMP); 4141b38ff370SJamie Gritton #endif 4142fd7a8150SMike Barcroft return (error); 4143fd7a8150SMike Barcroft } 4144fd7a8150SMike Barcroft 4145f3b86a5fSEd Schouten SYSCTL_OID(_security_jail, OID_AUTO, list, 4146f3b86a5fSEd Schouten CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4147f3b86a5fSEd Schouten sysctl_jail_list, "S", "List of active jails"); 4148461167c2SPawel Jakub Dawidek 4149461167c2SPawel Jakub Dawidek static int 4150461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS) 4151461167c2SPawel Jakub Dawidek { 4152461167c2SPawel Jakub Dawidek int error, injail; 4153461167c2SPawel Jakub Dawidek 4154461167c2SPawel Jakub Dawidek injail = jailed(req->td->td_ucred); 4155461167c2SPawel Jakub Dawidek error = SYSCTL_OUT(req, &injail, sizeof(injail)); 4156461167c2SPawel Jakub Dawidek 4157461167c2SPawel Jakub Dawidek return (error); 4158461167c2SPawel Jakub Dawidek } 41590304c731SJamie Gritton 4160f3b86a5fSEd Schouten SYSCTL_PROC(_security_jail, OID_AUTO, jailed, 4161f3b86a5fSEd Schouten CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4162f3b86a5fSEd Schouten sysctl_jail_jailed, "I", "Process in jail?"); 4163413628a7SBjoern A. Zeeb 4164761d2bb5SJamie Gritton static int 4165761d2bb5SJamie Gritton sysctl_jail_vnet(SYSCTL_HANDLER_ARGS) 4166761d2bb5SJamie Gritton { 4167761d2bb5SJamie Gritton int error, havevnet; 4168761d2bb5SJamie Gritton #ifdef VIMAGE 4169761d2bb5SJamie Gritton struct ucred *cred = req->td->td_ucred; 4170761d2bb5SJamie Gritton 4171761d2bb5SJamie Gritton havevnet = jailed(cred) && prison_owns_vnet(cred); 4172761d2bb5SJamie Gritton #else 4173761d2bb5SJamie Gritton havevnet = 0; 4174761d2bb5SJamie Gritton #endif 4175761d2bb5SJamie Gritton error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet)); 4176761d2bb5SJamie Gritton 4177761d2bb5SJamie Gritton return (error); 4178761d2bb5SJamie Gritton } 4179761d2bb5SJamie Gritton 4180761d2bb5SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, vnet, 4181761d2bb5SJamie Gritton CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4182bb8f1623SBjoern A. Zeeb sysctl_jail_vnet, "I", "Jail owns vnet?"); 4183761d2bb5SJamie Gritton 41840304c731SJamie Gritton #if defined(INET) || defined(INET6) 4185e92e0574SJamie Gritton SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW, 41860304c731SJamie Gritton &jail_max_af_ips, 0, 4187c542c43eSJamie Gritton "Number of IP addresses a jail may have at most per address family (deprecated)"); 41880304c731SJamie Gritton #endif 41890304c731SJamie Gritton 41900304c731SJamie Gritton /* 4191c542c43eSJamie Gritton * Default parameters for jail(2) compatibility. For historical reasons, 4192c542c43eSJamie Gritton * the sysctl names have varying similarity to the parameter names. Prisons 4193c542c43eSJamie Gritton * just see their own parameters, and can't change them. 41940304c731SJamie Gritton */ 41950304c731SJamie Gritton static int 41960304c731SJamie Gritton sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS) 41970304c731SJamie Gritton { 41980fe74ae6SJamie Gritton int error, i; 41990304c731SJamie Gritton 42000304c731SJamie Gritton /* Get the current flag value, and convert it to a boolean. */ 42010fe74ae6SJamie Gritton if (req->td->td_ucred->cr_prison == &prison0) { 42020fe74ae6SJamie Gritton mtx_lock(&prison0.pr_mtx); 42030fe74ae6SJamie Gritton i = (jail_default_allow & arg2) != 0; 42040fe74ae6SJamie Gritton mtx_unlock(&prison0.pr_mtx); 42050fe74ae6SJamie Gritton } else 42060fe74ae6SJamie Gritton i = prison_allow(req->td->td_ucred, arg2); 42070fe74ae6SJamie Gritton 42080304c731SJamie Gritton if (arg1 != NULL) 42090304c731SJamie Gritton i = !i; 42100304c731SJamie Gritton error = sysctl_handle_int(oidp, &i, 0, req); 4211c542c43eSJamie Gritton if (error || !req->newptr) 42120304c731SJamie Gritton return (error); 42130304c731SJamie Gritton i = i ? arg2 : 0; 42140304c731SJamie Gritton if (arg1 != NULL) 42150304c731SJamie Gritton i ^= arg2; 42160304c731SJamie Gritton /* 42170304c731SJamie Gritton * The sysctls don't have CTLFLAGS_PRISON, so assume prison0 42180304c731SJamie Gritton * for writing. 42190304c731SJamie Gritton */ 42200304c731SJamie Gritton mtx_lock(&prison0.pr_mtx); 42210304c731SJamie Gritton jail_default_allow = (jail_default_allow & ~arg2) | i; 42220304c731SJamie Gritton mtx_unlock(&prison0.pr_mtx); 42230304c731SJamie Gritton return (0); 42240304c731SJamie Gritton } 42250304c731SJamie Gritton 42260304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed, 4227c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 42280304c731SJamie Gritton NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I", 4229c542c43eSJamie Gritton "Processes in jail can set their hostnames (deprecated)"); 42300304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only, 4231c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 42320304c731SJamie Gritton (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I", 4233c542c43eSJamie Gritton "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)"); 42340304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed, 4235c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 42360304c731SJamie Gritton NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I", 4237c542c43eSJamie Gritton "Processes in jail can use System V IPC primitives (deprecated)"); 42380304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets, 4239c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 42400304c731SJamie Gritton NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I", 4241c542c43eSJamie Gritton "Prison root can create raw sockets (deprecated)"); 42420304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed, 4243c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 42440304c731SJamie Gritton NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I", 4245c542c43eSJamie Gritton "Processes in jail can alter system file flags (deprecated)"); 42460304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed, 4247c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 42480304c731SJamie Gritton NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I", 4249c542c43eSJamie Gritton "Processes in jail can mount/unmount jail-friendly file systems (deprecated)"); 42500304c731SJamie Gritton 42510304c731SJamie Gritton static int 42520304c731SJamie Gritton sysctl_jail_default_level(SYSCTL_HANDLER_ARGS) 42530304c731SJamie Gritton { 42540304c731SJamie Gritton struct prison *pr; 42550304c731SJamie Gritton int level, error; 42560304c731SJamie Gritton 42570304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 42580304c731SJamie Gritton level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2); 42590304c731SJamie Gritton error = sysctl_handle_int(oidp, &level, 0, req); 4260c542c43eSJamie Gritton if (error || !req->newptr) 42610304c731SJamie Gritton return (error); 42620304c731SJamie Gritton *(int *)arg1 = level; 42630304c731SJamie Gritton return (0); 42640304c731SJamie Gritton } 42650304c731SJamie Gritton 42660304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs, 4267c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4268c542c43eSJamie Gritton &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs), 42690304c731SJamie Gritton sysctl_jail_default_level, "I", 4270c542c43eSJamie Gritton "Processes in jail cannot see all mounted file systems (deprecated)"); 4271c542c43eSJamie Gritton 42720cc207a6SMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset, 4273c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 4274c542c43eSJamie Gritton &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum), 42750cc207a6SMartin Matuska sysctl_jail_default_level, "I", 4276c542c43eSJamie Gritton "Ruleset for the devfs filesystem in jail (deprecated)"); 42770cc207a6SMartin Matuska 42780304c731SJamie Gritton /* 42790304c731SJamie Gritton * Nodes to describe jail parameters. Maximum length of string parameters 42800304c731SJamie Gritton * is returned in the string itself, and the other parameters exist merely 42810304c731SJamie Gritton * to make themselves and their types known. 42820304c731SJamie Gritton */ 42837029da5cSPawel Biernacki SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 42840304c731SJamie Gritton "Jail parameters"); 42850304c731SJamie Gritton 42860304c731SJamie Gritton int 42870304c731SJamie Gritton sysctl_jail_param(SYSCTL_HANDLER_ARGS) 42880304c731SJamie Gritton { 42890304c731SJamie Gritton int i; 42900304c731SJamie Gritton long l; 42910304c731SJamie Gritton size_t s; 42920304c731SJamie Gritton char numbuf[12]; 42930304c731SJamie Gritton 42940304c731SJamie Gritton switch (oidp->oid_kind & CTLTYPE) 42950304c731SJamie Gritton { 42960304c731SJamie Gritton case CTLTYPE_LONG: 42970304c731SJamie Gritton case CTLTYPE_ULONG: 42980304c731SJamie Gritton l = 0; 42990304c731SJamie Gritton #ifdef SCTL_MASK32 43000304c731SJamie Gritton if (!(req->flags & SCTL_MASK32)) 43010304c731SJamie Gritton #endif 43020304c731SJamie Gritton return (SYSCTL_OUT(req, &l, sizeof(l))); 43030304c731SJamie Gritton case CTLTYPE_INT: 43040304c731SJamie Gritton case CTLTYPE_UINT: 43050304c731SJamie Gritton i = 0; 43060304c731SJamie Gritton return (SYSCTL_OUT(req, &i, sizeof(i))); 43070304c731SJamie Gritton case CTLTYPE_STRING: 4308e4cd31ddSJeff Roberson snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2); 43090304c731SJamie Gritton return 43100304c731SJamie Gritton (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req)); 43110304c731SJamie Gritton case CTLTYPE_STRUCT: 43120304c731SJamie Gritton s = (size_t)arg2; 43130304c731SJamie Gritton return (SYSCTL_OUT(req, &s, sizeof(s))); 43140304c731SJamie Gritton } 43150304c731SJamie Gritton return (0); 43160304c731SJamie Gritton } 43170304c731SJamie Gritton 4318b96bd95bSIan Lepore /* 4319b96bd95bSIan Lepore * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at 4320b96bd95bSIan Lepore * jail creation time but cannot be changed in an existing jail. 4321b96bd95bSIan Lepore */ 43220304c731SJamie Gritton SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID"); 43230304c731SJamie Gritton SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID"); 43240304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name"); 43250304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path"); 43260304c731SJamie Gritton SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW, 43270304c731SJamie Gritton "I", "Jail secure level"); 4328b96bd95bSIan Lepore SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I", 4329b96bd95bSIan Lepore "Jail value for kern.osreldate and uname -K"); 4330b96bd95bSIan Lepore SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN, 4331b96bd95bSIan Lepore "Jail value for kern.osrelease and uname -r"); 43320304c731SJamie Gritton SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW, 43330304c731SJamie Gritton "I", "Jail cannot see all mounted file systems"); 43340cc207a6SMartin Matuska SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW, 43350cc207a6SMartin Matuska "I", "Ruleset for in-jail devfs mounts"); 43360304c731SJamie Gritton SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW, 43370304c731SJamie Gritton "B", "Jail persistence"); 4338679e1390SJamie Gritton #ifdef VIMAGE 4339679e1390SJamie Gritton SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN, 43407cbf7213SJamie Gritton "E,jailsys", "Virtual network stack"); 4341679e1390SJamie Gritton #endif 43420304c731SJamie Gritton SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD, 43430304c731SJamie Gritton "B", "Jail is in the process of shutting down"); 43440304c731SJamie Gritton 4345b97457e2SJamie Gritton SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails"); 4346b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD, 4347b97457e2SJamie Gritton "I", "Current number of child jails"); 4348b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW, 4349b97457e2SJamie Gritton "I", "Maximum number of child jails"); 4350b97457e2SJamie Gritton 43517cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info"); 43520304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN, 43530304c731SJamie Gritton "Jail hostname"); 435476ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN, 435576ca6f88SJamie Gritton "Jail NIS domainname"); 435676ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN, 435776ca6f88SJamie Gritton "Jail host UUID"); 435876ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW, 435976ca6f88SJamie Gritton "LU", "Jail host ID"); 43600304c731SJamie Gritton 43610304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset"); 43620304c731SJamie Gritton SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID"); 43630304c731SJamie Gritton 43640304c731SJamie Gritton #ifdef INET 43652b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN, 43662b0d6f81SJamie Gritton "Jail IPv4 address virtualization"); 43670304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr), 43680304c731SJamie Gritton "S,in_addr,a", "Jail IPv4 addresses"); 4369592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4370592bcae8SBjoern A. Zeeb "B", "Do (not) use IPv4 source address selection rather than the " 4371592bcae8SBjoern A. Zeeb "primary jail IPv4 address."); 43720304c731SJamie Gritton #endif 43730304c731SJamie Gritton #ifdef INET6 43742b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN, 43752b0d6f81SJamie Gritton "Jail IPv6 address virtualization"); 43760304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr), 43770304c731SJamie Gritton "S,in6_addr,a", "Jail IPv6 addresses"); 4378592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4379592bcae8SBjoern A. Zeeb "B", "Do (not) use IPv6 source address selection rather than the " 4380592bcae8SBjoern A. Zeeb "primary jail IPv6 address."); 43810304c731SJamie Gritton #endif 43820304c731SJamie Gritton 43830304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags"); 43840304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW, 43850304c731SJamie Gritton "B", "Jail may set hostname"); 43860304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW, 43870304c731SJamie Gritton "B", "Jail may use SYSV IPC"); 43880304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW, 43890304c731SJamie Gritton "B", "Jail may create raw sockets"); 43900304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW, 43910304c731SJamie Gritton "B", "Jail may alter system file flags"); 43920304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW, 43930304c731SJamie Gritton "B", "Jail may set file quotas"); 43940304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW, 43950304c731SJamie Gritton "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route"); 4396ccd6ac9fSAntoine Brodin SYSCTL_JAIL_PARAM(_allow, mlock, CTLTYPE_INT | CTLFLAG_RW, 4397ccd6ac9fSAntoine Brodin "B", "Jail may lock (unlock) physical pages in memory"); 4398e28f9b7dSAllan Jude SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW, 4399e28f9b7dSAllan Jude "B", "Jail may bind sockets to reserved ports"); 4400b19d66fdSJamie Gritton SYSCTL_JAIL_PARAM(_allow, read_msgbuf, CTLTYPE_INT | CTLFLAG_RW, 4401b19d66fdSJamie Gritton "B", "Jail may read the kernel message buffer"); 4402b3079544SJamie Gritton SYSCTL_JAIL_PARAM(_allow, unprivileged_proc_debug, CTLTYPE_INT | CTLFLAG_RW, 4403b3079544SJamie Gritton "B", "Unprivileged processes may use process debugging facilities"); 440405e1e482SMariusz Zaborski SYSCTL_JAIL_PARAM(_allow, suser, CTLTYPE_INT | CTLFLAG_RW, 440505e1e482SMariusz Zaborski "B", "Processes in jail with uid 0 have privilege"); 44060304c731SJamie Gritton 4407bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags"); 4408bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW, 4409bf3db8aaSMartin Matuska "B", "Jail may mount/unmount jail-friendly file systems in general"); 44100e5c6bd4SJamie Gritton 44110e5c6bd4SJamie Gritton /* 44120a172404SJamie Gritton * Add a dynamic parameter allow.<name>, or allow.<prefix>.<name>. Return 44130a172404SJamie Gritton * its associated bit in the pr_allow bitmask, or zero if the parameter was 44140a172404SJamie Gritton * not created. 44150e5c6bd4SJamie Gritton */ 44160a172404SJamie Gritton unsigned 44170a172404SJamie Gritton prison_add_allow(const char *prefix, const char *name, const char *prefix_descr, 44180a172404SJamie Gritton const char *descr) 44190e5c6bd4SJamie Gritton { 44200e5c6bd4SJamie Gritton struct bool_flags *bf; 44210a172404SJamie Gritton struct sysctl_oid *parent; 44220a172404SJamie Gritton char *allow_name, *allow_noname, *allowed; 4423c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR 4424c542c43eSJamie Gritton char *descr_deprecated; 4425c542c43eSJamie Gritton #endif 44265d58f959SJamie Gritton u_int allow_flag; 44270e5c6bd4SJamie Gritton 44280a172404SJamie Gritton if (prefix 44290a172404SJamie Gritton ? asprintf(&allow_name, M_PRISON, "allow.%s.%s", prefix, name) 44300a172404SJamie Gritton < 0 || 44310a172404SJamie Gritton asprintf(&allow_noname, M_PRISON, "allow.%s.no%s", prefix, name) 44320a172404SJamie Gritton < 0 44330a172404SJamie Gritton : asprintf(&allow_name, M_PRISON, "allow.%s", name) < 0 || 44340a172404SJamie Gritton asprintf(&allow_noname, M_PRISON, "allow.no%s", name) < 0) { 44350e5c6bd4SJamie Gritton free(allow_name, M_PRISON); 44360a172404SJamie Gritton return 0; 44370e5c6bd4SJamie Gritton } 44380e5c6bd4SJamie Gritton 44390e5c6bd4SJamie Gritton /* 44400a172404SJamie Gritton * See if this parameter has already beed added, i.e. a module was 44410a172404SJamie Gritton * previously loaded/unloaded. 44420e5c6bd4SJamie Gritton */ 44430e5c6bd4SJamie Gritton mtx_lock(&prison0.pr_mtx); 44440e5c6bd4SJamie Gritton for (bf = pr_flag_allow; 44455d58f959SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 44465d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 44470e5c6bd4SJamie Gritton bf++) { 44480e5c6bd4SJamie Gritton if (strcmp(bf->name, allow_name) == 0) { 44490a172404SJamie Gritton allow_flag = bf->flag; 44500e5c6bd4SJamie Gritton goto no_add; 44510e5c6bd4SJamie Gritton } 44520e5c6bd4SJamie Gritton } 44530e5c6bd4SJamie Gritton 44540e5c6bd4SJamie Gritton /* 44555d58f959SJamie Gritton * Find a free bit in pr_allow_all, failing if there are none 44560e5c6bd4SJamie Gritton * (which shouldn't happen as long as we keep track of how many 44570a172404SJamie Gritton * potential dynamic flags exist). 44580e5c6bd4SJamie Gritton */ 44590e5c6bd4SJamie Gritton for (allow_flag = 1;; allow_flag <<= 1) { 44600e5c6bd4SJamie Gritton if (allow_flag == 0) 44610e5c6bd4SJamie Gritton goto no_add; 44625d58f959SJamie Gritton if ((pr_allow_all & allow_flag) == 0) 44630e5c6bd4SJamie Gritton break; 44640e5c6bd4SJamie Gritton } 44650e5c6bd4SJamie Gritton 44665d58f959SJamie Gritton /* Note the parameter in the next open slot in pr_flag_allow. */ 44675d58f959SJamie Gritton for (bf = pr_flag_allow; ; bf++) { 44680e5c6bd4SJamie Gritton if (bf == pr_flag_allow + nitems(pr_flag_allow)) { 44690e5c6bd4SJamie Gritton /* This should never happen, but is not fatal. */ 44700a172404SJamie Gritton allow_flag = 0; 44710e5c6bd4SJamie Gritton goto no_add; 44720e5c6bd4SJamie Gritton } 44735d58f959SJamie Gritton if (atomic_load_int(&bf->flag) == 0) 44745d58f959SJamie Gritton break; 44755d58f959SJamie Gritton } 44760e5c6bd4SJamie Gritton bf->name = allow_name; 44770e5c6bd4SJamie Gritton bf->noname = allow_noname; 44785d58f959SJamie Gritton pr_allow_all |= allow_flag; 44795d58f959SJamie Gritton /* 44805d58f959SJamie Gritton * prison0 always has permission for the new parameter. 44815d58f959SJamie Gritton * Other jails must have it granted to them. 44825d58f959SJamie Gritton */ 44835d58f959SJamie Gritton prison0.pr_allow |= allow_flag; 44845d58f959SJamie Gritton /* The flag indicates a valid entry, so make sure it is set last. */ 44855d58f959SJamie Gritton atomic_store_rel_int(&bf->flag, allow_flag); 44860e5c6bd4SJamie Gritton mtx_unlock(&prison0.pr_mtx); 44870e5c6bd4SJamie Gritton 4488c542c43eSJamie Gritton /* 4489c542c43eSJamie Gritton * Create sysctls for the paramter, and the back-compat global 4490c542c43eSJamie Gritton * permission. 4491c542c43eSJamie Gritton */ 44920a172404SJamie Gritton parent = prefix 44930a172404SJamie Gritton ? SYSCTL_ADD_NODE(NULL, 44940a172404SJamie Gritton SYSCTL_CHILDREN(&sysctl___security_jail_param_allow), 44957029da5cSPawel Biernacki OID_AUTO, prefix, CTLFLAG_MPSAFE, 0, prefix_descr) 44960a172404SJamie Gritton : &sysctl___security_jail_param_allow; 44970a172404SJamie Gritton (void)SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(parent), OID_AUTO, 44980a172404SJamie Gritton name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44990e5c6bd4SJamie Gritton NULL, 0, sysctl_jail_param, "B", descr); 45000a172404SJamie Gritton if ((prefix 45010a172404SJamie Gritton ? asprintf(&allowed, M_TEMP, "%s_%s_allowed", prefix, name) 45020a172404SJamie Gritton : asprintf(&allowed, M_TEMP, "%s_allowed", name)) >= 0) { 4503c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR 4504c542c43eSJamie Gritton (void)asprintf(&descr_deprecated, M_TEMP, "%s (deprecated)", 4505c542c43eSJamie Gritton descr); 4506c542c43eSJamie Gritton #endif 45070e5c6bd4SJamie Gritton (void)SYSCTL_ADD_PROC(NULL, 45080a172404SJamie Gritton SYSCTL_CHILDREN(&sysctl___security_jail), OID_AUTO, allowed, 4509c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, allow_flag, 4510c542c43eSJamie Gritton sysctl_jail_default_allow, "I", descr_deprecated); 4511c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR 4512c542c43eSJamie Gritton free(descr_deprecated, M_TEMP); 4513c542c43eSJamie Gritton #endif 45140a172404SJamie Gritton free(allowed, M_TEMP); 45150e5c6bd4SJamie Gritton } 45160a172404SJamie Gritton return allow_flag; 45170e5c6bd4SJamie Gritton 45180e5c6bd4SJamie Gritton no_add: 45190e5c6bd4SJamie Gritton mtx_unlock(&prison0.pr_mtx); 45200e5c6bd4SJamie Gritton free(allow_name, M_PRISON); 45210e5c6bd4SJamie Gritton free(allow_noname, M_PRISON); 45220a172404SJamie Gritton return allow_flag; 45230a172404SJamie Gritton } 45240a172404SJamie Gritton 45250a172404SJamie Gritton /* 45260a172404SJamie Gritton * The VFS system will register jail-aware filesystems here. They each get 45270a172404SJamie Gritton * a parameter allow.mount.xxxfs and a flag to check when a jailed user 45280a172404SJamie Gritton * attempts to mount. 45290a172404SJamie Gritton */ 45300a172404SJamie Gritton void 45310a172404SJamie Gritton prison_add_vfs(struct vfsconf *vfsp) 45320a172404SJamie Gritton { 45330a172404SJamie Gritton #ifdef NO_SYSCTL_DESCR 45340a172404SJamie Gritton 45350a172404SJamie Gritton vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name, 45360a172404SJamie Gritton NULL, NULL); 45370a172404SJamie Gritton #else 45380a172404SJamie Gritton char *descr; 45390a172404SJamie Gritton 45400a172404SJamie Gritton (void)asprintf(&descr, M_TEMP, "Jail may mount the %s file system", 45410a172404SJamie Gritton vfsp->vfc_name); 45420a172404SJamie Gritton vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name, 45430a172404SJamie Gritton NULL, descr); 45440a172404SJamie Gritton free(descr, M_TEMP); 45450a172404SJamie Gritton #endif 45460e5c6bd4SJamie Gritton } 4547bf3db8aaSMartin Matuska 45484b5c9cf6SEdward Tomasz Napierala #ifdef RACCT 4549097055e2SEdward Tomasz Napierala void 4550097055e2SEdward Tomasz Napierala prison_racct_foreach(void (*callback)(struct racct *racct, 455115db3c07SEdward Tomasz Napierala void *arg2, void *arg3), void (*pre)(void), void (*post)(void), 455215db3c07SEdward Tomasz Napierala void *arg2, void *arg3) 4553097055e2SEdward Tomasz Napierala { 4554a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4555097055e2SEdward Tomasz Napierala 45564b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 45574b5c9cf6SEdward Tomasz Napierala 4558097055e2SEdward Tomasz Napierala sx_slock(&allprison_lock); 455915db3c07SEdward Tomasz Napierala if (pre != NULL) 456015db3c07SEdward Tomasz Napierala (pre)(); 4561a7ad07bfSEdward Tomasz Napierala LIST_FOREACH(prr, &allprison_racct, prr_next) 4562a7ad07bfSEdward Tomasz Napierala (callback)(prr->prr_racct, arg2, arg3); 456315db3c07SEdward Tomasz Napierala if (post != NULL) 456415db3c07SEdward Tomasz Napierala (post)(); 4565097055e2SEdward Tomasz Napierala sx_sunlock(&allprison_lock); 4566097055e2SEdward Tomasz Napierala } 45670304c731SJamie Gritton 4568a7ad07bfSEdward Tomasz Napierala static struct prison_racct * 4569a7ad07bfSEdward Tomasz Napierala prison_racct_find_locked(const char *name) 4570a7ad07bfSEdward Tomasz Napierala { 4571a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4572a7ad07bfSEdward Tomasz Napierala 45734b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4574a7ad07bfSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4575a7ad07bfSEdward Tomasz Napierala 4576a7ad07bfSEdward Tomasz Napierala if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN) 4577a7ad07bfSEdward Tomasz Napierala return (NULL); 4578a7ad07bfSEdward Tomasz Napierala 4579a7ad07bfSEdward Tomasz Napierala LIST_FOREACH(prr, &allprison_racct, prr_next) { 4580a7ad07bfSEdward Tomasz Napierala if (strcmp(name, prr->prr_name) != 0) 4581a7ad07bfSEdward Tomasz Napierala continue; 4582a7ad07bfSEdward Tomasz Napierala 4583a7ad07bfSEdward Tomasz Napierala /* Found prison_racct with a matching name? */ 4584a7ad07bfSEdward Tomasz Napierala prison_racct_hold(prr); 4585a7ad07bfSEdward Tomasz Napierala return (prr); 4586a7ad07bfSEdward Tomasz Napierala } 4587a7ad07bfSEdward Tomasz Napierala 4588a7ad07bfSEdward Tomasz Napierala /* Add new prison_racct. */ 4589a7ad07bfSEdward Tomasz Napierala prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK); 4590a7ad07bfSEdward Tomasz Napierala racct_create(&prr->prr_racct); 4591a7ad07bfSEdward Tomasz Napierala 4592a7ad07bfSEdward Tomasz Napierala strcpy(prr->prr_name, name); 4593a7ad07bfSEdward Tomasz Napierala refcount_init(&prr->prr_refcount, 1); 4594a7ad07bfSEdward Tomasz Napierala LIST_INSERT_HEAD(&allprison_racct, prr, prr_next); 4595a7ad07bfSEdward Tomasz Napierala 4596a7ad07bfSEdward Tomasz Napierala return (prr); 4597a7ad07bfSEdward Tomasz Napierala } 4598a7ad07bfSEdward Tomasz Napierala 4599a7ad07bfSEdward Tomasz Napierala struct prison_racct * 4600a7ad07bfSEdward Tomasz Napierala prison_racct_find(const char *name) 4601a7ad07bfSEdward Tomasz Napierala { 4602a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4603a7ad07bfSEdward Tomasz Napierala 46044b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 46054b5c9cf6SEdward Tomasz Napierala 4606a7ad07bfSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4607a7ad07bfSEdward Tomasz Napierala prr = prison_racct_find_locked(name); 4608a7ad07bfSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4609a7ad07bfSEdward Tomasz Napierala return (prr); 4610a7ad07bfSEdward Tomasz Napierala } 4611a7ad07bfSEdward Tomasz Napierala 4612a7ad07bfSEdward Tomasz Napierala void 4613a7ad07bfSEdward Tomasz Napierala prison_racct_hold(struct prison_racct *prr) 4614a7ad07bfSEdward Tomasz Napierala { 4615a7ad07bfSEdward Tomasz Napierala 46164b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 46174b5c9cf6SEdward Tomasz Napierala 4618a7ad07bfSEdward Tomasz Napierala refcount_acquire(&prr->prr_refcount); 4619a7ad07bfSEdward Tomasz Napierala } 4620a7ad07bfSEdward Tomasz Napierala 4621c34bbd2aSEdward Tomasz Napierala static void 4622c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(struct prison_racct *prr) 4623c34bbd2aSEdward Tomasz Napierala { 4624c34bbd2aSEdward Tomasz Napierala 46254b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4626c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4627c34bbd2aSEdward Tomasz Napierala 4628c34bbd2aSEdward Tomasz Napierala if (refcount_release(&prr->prr_refcount)) { 4629c34bbd2aSEdward Tomasz Napierala racct_destroy(&prr->prr_racct); 4630c34bbd2aSEdward Tomasz Napierala LIST_REMOVE(prr, prr_next); 4631c34bbd2aSEdward Tomasz Napierala free(prr, M_PRISON_RACCT); 4632c34bbd2aSEdward Tomasz Napierala } 4633c34bbd2aSEdward Tomasz Napierala } 4634c34bbd2aSEdward Tomasz Napierala 4635a7ad07bfSEdward Tomasz Napierala void 4636a7ad07bfSEdward Tomasz Napierala prison_racct_free(struct prison_racct *prr) 4637a7ad07bfSEdward Tomasz Napierala { 4638a7ad07bfSEdward Tomasz Napierala 46394b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4640c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_UNLOCKED); 4641c34bbd2aSEdward Tomasz Napierala 46426ff4688bSMateusz Guzik if (refcount_release_if_not_last(&prr->prr_refcount)) 4643a7ad07bfSEdward Tomasz Napierala return; 4644a7ad07bfSEdward Tomasz Napierala 4645a7ad07bfSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4646c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(prr); 4647a7ad07bfSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4648a7ad07bfSEdward Tomasz Napierala } 4649a7ad07bfSEdward Tomasz Napierala 4650a7ad07bfSEdward Tomasz Napierala static void 4651a7ad07bfSEdward Tomasz Napierala prison_racct_attach(struct prison *pr) 4652a7ad07bfSEdward Tomasz Napierala { 4653a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4654a7ad07bfSEdward Tomasz Napierala 46554b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4656c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4657c34bbd2aSEdward Tomasz Napierala 4658a7ad07bfSEdward Tomasz Napierala prr = prison_racct_find_locked(pr->pr_name); 4659a7ad07bfSEdward Tomasz Napierala KASSERT(prr != NULL, ("cannot find prison_racct")); 4660a7ad07bfSEdward Tomasz Napierala 4661a7ad07bfSEdward Tomasz Napierala pr->pr_prison_racct = prr; 4662a7ad07bfSEdward Tomasz Napierala } 4663a7ad07bfSEdward Tomasz Napierala 4664c34bbd2aSEdward Tomasz Napierala /* 4665c34bbd2aSEdward Tomasz Napierala * Handle jail renaming. From the racct point of view, renaming means 4666c34bbd2aSEdward Tomasz Napierala * moving from one prison_racct to another. 4667c34bbd2aSEdward Tomasz Napierala */ 4668c34bbd2aSEdward Tomasz Napierala static void 4669c34bbd2aSEdward Tomasz Napierala prison_racct_modify(struct prison *pr) 4670c34bbd2aSEdward Tomasz Napierala { 4671dbadb015SKonstantin Belousov #ifdef RCTL 4672c34bbd2aSEdward Tomasz Napierala struct proc *p; 4673c34bbd2aSEdward Tomasz Napierala struct ucred *cred; 4674dbadb015SKonstantin Belousov #endif 4675c34bbd2aSEdward Tomasz Napierala struct prison_racct *oldprr; 4676c34bbd2aSEdward Tomasz Napierala 46774b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 46784b5c9cf6SEdward Tomasz Napierala 4679c34bbd2aSEdward Tomasz Napierala sx_slock(&allproc_lock); 4680c34bbd2aSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4681c34bbd2aSEdward Tomasz Napierala 4682e30345e7SEdward Tomasz Napierala if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) { 4683e30345e7SEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4684e30345e7SEdward Tomasz Napierala sx_sunlock(&allproc_lock); 4685c34bbd2aSEdward Tomasz Napierala return; 4686e30345e7SEdward Tomasz Napierala } 4687c34bbd2aSEdward Tomasz Napierala 4688c34bbd2aSEdward Tomasz Napierala oldprr = pr->pr_prison_racct; 4689c34bbd2aSEdward Tomasz Napierala pr->pr_prison_racct = NULL; 4690c34bbd2aSEdward Tomasz Napierala 4691c34bbd2aSEdward Tomasz Napierala prison_racct_attach(pr); 4692c34bbd2aSEdward Tomasz Napierala 4693c34bbd2aSEdward Tomasz Napierala /* 4694c34bbd2aSEdward Tomasz Napierala * Move resource utilisation records. 4695c34bbd2aSEdward Tomasz Napierala */ 4696c34bbd2aSEdward Tomasz Napierala racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct); 4697c34bbd2aSEdward Tomasz Napierala 4698f87beb93SAndriy Gapon #ifdef RCTL 4699c34bbd2aSEdward Tomasz Napierala /* 4700c34bbd2aSEdward Tomasz Napierala * Force rctl to reattach rules to processes. 4701c34bbd2aSEdward Tomasz Napierala */ 4702c34bbd2aSEdward Tomasz Napierala FOREACH_PROC_IN_SYSTEM(p) { 4703c34bbd2aSEdward Tomasz Napierala PROC_LOCK(p); 4704c34bbd2aSEdward Tomasz Napierala cred = crhold(p->p_ucred); 4705c34bbd2aSEdward Tomasz Napierala PROC_UNLOCK(p); 4706f87beb93SAndriy Gapon rctl_proc_ucred_changed(p, cred); 4707c34bbd2aSEdward Tomasz Napierala crfree(cred); 4708c34bbd2aSEdward Tomasz Napierala } 4709f87beb93SAndriy Gapon #endif 4710c34bbd2aSEdward Tomasz Napierala 4711c34bbd2aSEdward Tomasz Napierala sx_sunlock(&allproc_lock); 4712c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(oldprr); 4713c34bbd2aSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4714c34bbd2aSEdward Tomasz Napierala } 4715c34bbd2aSEdward Tomasz Napierala 4716a7ad07bfSEdward Tomasz Napierala static void 4717a7ad07bfSEdward Tomasz Napierala prison_racct_detach(struct prison *pr) 4718a7ad07bfSEdward Tomasz Napierala { 4719c34bbd2aSEdward Tomasz Napierala 47204b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4721c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_UNLOCKED); 4722c34bbd2aSEdward Tomasz Napierala 4723af3c786cSMateusz Guzik if (pr->pr_prison_racct == NULL) 4724af3c786cSMateusz Guzik return; 4725a7ad07bfSEdward Tomasz Napierala prison_racct_free(pr->pr_prison_racct); 4726a7ad07bfSEdward Tomasz Napierala pr->pr_prison_racct = NULL; 4727a7ad07bfSEdward Tomasz Napierala } 4728a7ad07bfSEdward Tomasz Napierala #endif /* RACCT */ 4729a7ad07bfSEdward Tomasz Napierala 4730413628a7SBjoern A. Zeeb #ifdef DDB 4731b38ff370SJamie Gritton 4732b38ff370SJamie Gritton static void 4733b38ff370SJamie Gritton db_show_prison(struct prison *pr) 4734413628a7SBjoern A. Zeeb { 4735672756aaSJamie Gritton struct bool_flags *bf; 4736672756aaSJamie Gritton struct jailsys_flags *jsf; 4737b38ff370SJamie Gritton #if defined(INET) || defined(INET6) 4738b38ff370SJamie Gritton int ii; 4739413628a7SBjoern A. Zeeb #endif 4740672756aaSJamie Gritton unsigned f; 47418144690aSEric van Gyzen #ifdef INET 47428144690aSEric van Gyzen char ip4buf[INET_ADDRSTRLEN]; 47438144690aSEric van Gyzen #endif 4744413628a7SBjoern A. Zeeb #ifdef INET6 4745413628a7SBjoern A. Zeeb char ip6buf[INET6_ADDRSTRLEN]; 4746413628a7SBjoern A. Zeeb #endif 4747413628a7SBjoern A. Zeeb 4748b38ff370SJamie Gritton db_printf("prison %p:\n", pr); 4749b38ff370SJamie Gritton db_printf(" jid = %d\n", pr->pr_id); 4750b38ff370SJamie Gritton db_printf(" name = %s\n", pr->pr_name); 47510304c731SJamie Gritton db_printf(" parent = %p\n", pr->pr_parent); 4752b38ff370SJamie Gritton db_printf(" ref = %d\n", pr->pr_ref); 4753b38ff370SJamie Gritton db_printf(" uref = %d\n", pr->pr_uref); 47541158508aSJamie Gritton db_printf(" state = %s\n", 47551158508aSJamie Gritton pr->pr_state == PRISON_STATE_ALIVE ? "alive" : 47561158508aSJamie Gritton pr->pr_state == PRISON_STATE_DYING ? "dying" : 47571158508aSJamie Gritton "invalid"); 4758b38ff370SJamie Gritton db_printf(" path = %s\n", pr->pr_path); 4759b38ff370SJamie Gritton db_printf(" cpuset = %d\n", pr->pr_cpuset 4760b38ff370SJamie Gritton ? pr->pr_cpuset->cs_id : -1); 4761679e1390SJamie Gritton #ifdef VIMAGE 4762679e1390SJamie Gritton db_printf(" vnet = %p\n", pr->pr_vnet); 4763679e1390SJamie Gritton #endif 4764b38ff370SJamie Gritton db_printf(" root = %p\n", pr->pr_root); 4765b38ff370SJamie Gritton db_printf(" securelevel = %d\n", pr->pr_securelevel); 47660cc207a6SMartin Matuska db_printf(" devfs_rsnum = %d\n", pr->pr_devfs_rsnum); 4767fe0518e9SBjoern A. Zeeb db_printf(" children.max = %d\n", pr->pr_childmax); 4768fe0518e9SBjoern A. Zeeb db_printf(" children.cur = %d\n", pr->pr_childcount); 47690304c731SJamie Gritton db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children)); 47700304c731SJamie Gritton db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling)); 4771fe0518e9SBjoern A. Zeeb db_printf(" flags = 0x%x", pr->pr_flags); 4772672756aaSJamie Gritton for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++) 4773672756aaSJamie Gritton if (pr->pr_flags & bf->flag) 4774672756aaSJamie Gritton db_printf(" %s", bf->name); 4775672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 4776672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 4777672756aaSJamie Gritton jsf++) { 4778672756aaSJamie Gritton f = pr->pr_flags & (jsf->disable | jsf->new); 4779672756aaSJamie Gritton db_printf(" %-16s= %s\n", jsf->name, 4780672756aaSJamie Gritton (f != 0 && f == jsf->disable) ? "disable" 4781672756aaSJamie Gritton : (f == jsf->new) ? "new" 47827cbf7213SJamie Gritton : "inherit"); 47837cbf7213SJamie Gritton } 4784fe0518e9SBjoern A. Zeeb db_printf(" allow = 0x%x", pr->pr_allow); 4785672756aaSJamie Gritton for (bf = pr_flag_allow; 47865d58f959SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 47875d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 4788672756aaSJamie Gritton bf++) 4789672756aaSJamie Gritton if (pr->pr_allow & bf->flag) 4790672756aaSJamie Gritton db_printf(" %s", bf->name); 4791b38ff370SJamie Gritton db_printf("\n"); 47920304c731SJamie Gritton db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs); 4793c1f19219SJamie Gritton db_printf(" host.hostname = %s\n", pr->pr_hostname); 4794c1f19219SJamie Gritton db_printf(" host.domainname = %s\n", pr->pr_domainname); 4795c1f19219SJamie Gritton db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid); 479676ca6f88SJamie Gritton db_printf(" host.hostid = %lu\n", pr->pr_hostid); 4797413628a7SBjoern A. Zeeb #ifdef INET 4798eb8dcdeaSGleb Smirnoff if (pr->pr_addrs[PR_INET] != NULL) { 4799eb8dcdeaSGleb Smirnoff pr_family_t af = PR_INET; 4800eb8dcdeaSGleb Smirnoff 4801eb8dcdeaSGleb Smirnoff db_printf(" ip4s = %d\n", pr->pr_addrs[af]->ips); 4802eb8dcdeaSGleb Smirnoff for (ii = 0; ii < pr->pr_addrs[af]->ips; ii++) 4803b38ff370SJamie Gritton db_printf(" %s %s\n", 4804fe0518e9SBjoern A. Zeeb ii == 0 ? "ip4.addr =" : " ", 4805eb8dcdeaSGleb Smirnoff inet_ntoa_r( 4806eb8dcdeaSGleb Smirnoff *(const struct in_addr *)PR_IP(pr, ii), 4807eb8dcdeaSGleb Smirnoff ip4buf)); 4808eb8dcdeaSGleb Smirnoff } 4809413628a7SBjoern A. Zeeb #endif 4810413628a7SBjoern A. Zeeb #ifdef INET6 4811eb8dcdeaSGleb Smirnoff if (pr->pr_addrs[PR_INET6] != NULL) { 4812eb8dcdeaSGleb Smirnoff pr_family_t af = PR_INET6; 4813eb8dcdeaSGleb Smirnoff 4814eb8dcdeaSGleb Smirnoff db_printf(" ip6s = %d\n", pr->pr_addrs[af]->ips); 4815eb8dcdeaSGleb Smirnoff for (ii = 0; ii < pr->pr_addrs[af]->ips; ii++) 4816b38ff370SJamie Gritton db_printf(" %s %s\n", 4817fe0518e9SBjoern A. Zeeb ii == 0 ? "ip6.addr =" : " ", 4818eb8dcdeaSGleb Smirnoff ip6_sprintf(ip6buf, 4819eb8dcdeaSGleb Smirnoff (const struct in6_addr *)PR_IP(pr, ii))); 4820eb8dcdeaSGleb Smirnoff } 4821b38ff370SJamie Gritton #endif 4822b38ff370SJamie Gritton } 4823b38ff370SJamie Gritton 4824b38ff370SJamie Gritton DB_SHOW_COMMAND(prison, db_show_prison_command) 4825b38ff370SJamie Gritton { 4826b38ff370SJamie Gritton struct prison *pr; 4827b38ff370SJamie Gritton 4828b38ff370SJamie Gritton if (!have_addr) { 48290304c731SJamie Gritton /* 48300304c731SJamie Gritton * Show all prisons in the list, and prison0 which is not 48310304c731SJamie Gritton * listed. 48320304c731SJamie Gritton */ 48330304c731SJamie Gritton db_show_prison(&prison0); 48340304c731SJamie Gritton if (!db_pager_quit) { 4835b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 4836b38ff370SJamie Gritton db_show_prison(pr); 4837413628a7SBjoern A. Zeeb if (db_pager_quit) 4838413628a7SBjoern A. Zeeb break; 4839413628a7SBjoern A. Zeeb } 48400304c731SJamie Gritton } 4841b38ff370SJamie Gritton return; 4842413628a7SBjoern A. Zeeb } 4843b38ff370SJamie Gritton 48440304c731SJamie Gritton if (addr == 0) 48450304c731SJamie Gritton pr = &prison0; 48460304c731SJamie Gritton else { 4847b38ff370SJamie Gritton /* Look for a prison with the ID and with references. */ 4848b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 4849b38ff370SJamie Gritton if (pr->pr_id == addr && pr->pr_ref > 0) 4850b38ff370SJamie Gritton break; 4851b38ff370SJamie Gritton if (pr == NULL) 4852b38ff370SJamie Gritton /* Look again, without requiring a reference. */ 4853b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 4854b38ff370SJamie Gritton if (pr->pr_id == addr) 4855b38ff370SJamie Gritton break; 4856b38ff370SJamie Gritton if (pr == NULL) 4857b38ff370SJamie Gritton /* Assume address points to a valid prison. */ 4858b38ff370SJamie Gritton pr = (struct prison *)addr; 48590304c731SJamie Gritton } 4860b38ff370SJamie Gritton db_show_prison(pr); 4861b38ff370SJamie Gritton } 4862b38ff370SJamie Gritton 4863413628a7SBjoern A. Zeeb #endif /* DDB */ 4864