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> 48b3059e09SRobert Watson #include <sys/taskqueue.h> 4957b4252eSKonstantin Belousov #include <sys/fcntl.h> 5075c13541SPoul-Henning Kamp #include <sys/jail.h> 51c3188289SKyle Evans #include <sys/linker.h> 5201137630SRobert Watson #include <sys/lock.h> 5301137630SRobert Watson #include <sys/mutex.h> 54097055e2SEdward Tomasz Napierala #include <sys/racct.h> 55f87beb93SAndriy Gapon #include <sys/rctl.h> 56a7ad07bfSEdward Tomasz Napierala #include <sys/refcount.h> 57dc68a633SPawel Jakub Dawidek #include <sys/sx.h> 5876ca6f88SJamie Gritton #include <sys/sysent.h> 59fd7a8150SMike Barcroft #include <sys/namei.h> 60820a0de9SPawel Jakub Dawidek #include <sys/mount.h> 61fd7a8150SMike Barcroft #include <sys/queue.h> 6275c13541SPoul-Henning Kamp #include <sys/socket.h> 63fd7a8150SMike Barcroft #include <sys/syscallsubr.h> 6483f1e257SRobert Watson #include <sys/sysctl.h> 65c3188289SKyle Evans #include <sys/uuid.h> 66fd7a8150SMike Barcroft #include <sys/vnode.h> 67530c0060SRobert Watson 6875c13541SPoul-Henning Kamp #include <net/if.h> 69530c0060SRobert Watson #include <net/vnet.h> 70530c0060SRobert Watson 7175c13541SPoul-Henning Kamp #include <netinet/in.h> 72530c0060SRobert Watson 73413628a7SBjoern A. Zeeb #ifdef DDB 74413628a7SBjoern A. Zeeb #include <ddb/ddb.h> 75413628a7SBjoern A. Zeeb #endif /* DDB */ 7675c13541SPoul-Henning Kamp 77aed55708SRobert Watson #include <security/mac/mac_framework.h> 78aed55708SRobert Watson 798986e3a0SJamie Gritton #define DEFAULT_HOSTUUID "00000000-0000-0000-0000-000000000000" 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, 109b97457e2SJamie Gritton .pr_childmax = JAIL_MAX, 1108986e3a0SJamie Gritton .pr_hostuuid = DEFAULT_HOSTUUID, 11113e403fdSAntoine Brodin .pr_children = LIST_HEAD_INITIALIZER(prison0.pr_children), 112eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE 113592bcae8SBjoern A. Zeeb .pr_flags = PR_HOST|PR_VNET|_PR_IP_SADDRSEL, 114eb79e1c7SBjoern A. Zeeb #else 115592bcae8SBjoern A. Zeeb .pr_flags = PR_HOST|_PR_IP_SADDRSEL, 116eb79e1c7SBjoern A. Zeeb #endif 1170e5c6bd4SJamie Gritton .pr_allow = PR_ALLOW_ALL_STATIC, 1180304c731SJamie Gritton }; 1190304c731SJamie Gritton MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF); 12083f1e257SRobert Watson 121672756aaSJamie Gritton struct bool_flags { 122672756aaSJamie Gritton const char *name; 123672756aaSJamie Gritton const char *noname; 124672756aaSJamie Gritton unsigned flag; 125672756aaSJamie Gritton }; 126672756aaSJamie Gritton struct jailsys_flags { 127672756aaSJamie Gritton const char *name; 128672756aaSJamie Gritton unsigned disable; 129672756aaSJamie Gritton unsigned new; 130672756aaSJamie Gritton }; 131672756aaSJamie Gritton 132a7ad07bfSEdward Tomasz Napierala /* allprison, allprison_racct and lastprid are protected by allprison_lock. */ 133dc68a633SPawel Jakub Dawidek struct sx allprison_lock; 134b38ff370SJamie Gritton SX_SYSINIT(allprison_lock, &allprison_lock, "allprison"); 135b38ff370SJamie Gritton struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison); 136a7ad07bfSEdward Tomasz Napierala LIST_HEAD(, prison_racct) allprison_racct; 1372110d913SXin LI int lastprid = 0; 138fd7a8150SMike Barcroft 139b38ff370SJamie Gritton static int do_jail_attach(struct thread *td, struct prison *pr); 140b3059e09SRobert Watson static void prison_complete(void *context, int pending); 141b38ff370SJamie Gritton static void prison_deref(struct prison *pr, int flags); 1420304c731SJamie Gritton static char *prison_path(struct prison *pr1, struct prison *pr2); 1430304c731SJamie Gritton static void prison_remove_one(struct prison *pr); 144a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 145a7ad07bfSEdward Tomasz Napierala static void prison_racct_attach(struct prison *pr); 146c34bbd2aSEdward Tomasz Napierala static void prison_racct_modify(struct prison *pr); 147a7ad07bfSEdward Tomasz Napierala static void prison_racct_detach(struct prison *pr); 148a7ad07bfSEdward Tomasz Napierala #endif 149fd7a8150SMike Barcroft 150b38ff370SJamie Gritton /* Flags for prison_deref */ 151b38ff370SJamie Gritton #define PD_DEREF 0x01 152b38ff370SJamie Gritton #define PD_DEUREF 0x02 153b38ff370SJamie Gritton #define PD_LOCKED 0x04 154b38ff370SJamie Gritton #define PD_LIST_SLOCKED 0x08 155b38ff370SJamie Gritton #define PD_LIST_XLOCKED 0x10 156fd7a8150SMike Barcroft 1570304c731SJamie Gritton /* 1585cc70397SBjoern A. Zeeb * Parameter names corresponding to PR_* flag values. Size values are for kvm 1595cc70397SBjoern A. Zeeb * as we cannot figure out the size of a sparse array, or an array without a 1605cc70397SBjoern A. Zeeb * terminating entry. 1610304c731SJamie Gritton */ 162672756aaSJamie Gritton static struct bool_flags pr_flag_bool[] = { 163672756aaSJamie Gritton {"persist", "nopersist", PR_PERSIST}, 164592bcae8SBjoern A. Zeeb #ifdef INET 165672756aaSJamie Gritton {"ip4.saddrsel", "ip4.nosaddrsel", PR_IP4_SADDRSEL}, 166592bcae8SBjoern A. Zeeb #endif 167592bcae8SBjoern A. Zeeb #ifdef INET6 168672756aaSJamie Gritton {"ip6.saddrsel", "ip6.nosaddrsel", PR_IP6_SADDRSEL}, 169592bcae8SBjoern A. Zeeb #endif 1700304c731SJamie Gritton }; 171672756aaSJamie Gritton const size_t pr_flag_bool_size = sizeof(pr_flag_bool); 1720304c731SJamie Gritton 173672756aaSJamie Gritton static struct jailsys_flags pr_flag_jailsys[] = { 1747cbf7213SJamie Gritton {"host", 0, PR_HOST}, 1757cbf7213SJamie Gritton #ifdef VIMAGE 1767cbf7213SJamie Gritton {"vnet", 0, PR_VNET}, 1777cbf7213SJamie Gritton #endif 1780304c731SJamie Gritton #ifdef INET 1796a3f2779SJamie Gritton {"ip4", PR_IP4_USER, PR_IP4_USER}, 1800304c731SJamie Gritton #endif 1810304c731SJamie Gritton #ifdef INET6 1826a3f2779SJamie Gritton {"ip6", PR_IP6_USER, PR_IP6_USER}, 183679e1390SJamie Gritton #endif 1840304c731SJamie Gritton }; 1855cc70397SBjoern A. Zeeb const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys); 1860304c731SJamie Gritton 1870e5c6bd4SJamie Gritton /* Make this array full-size so dynamic parameters can be added. */ 1880e5c6bd4SJamie Gritton static struct bool_flags pr_flag_allow[NBBY * NBPW] = { 189672756aaSJamie Gritton {"allow.set_hostname", "allow.noset_hostname", PR_ALLOW_SET_HOSTNAME}, 190672756aaSJamie Gritton {"allow.sysvipc", "allow.nosysvipc", PR_ALLOW_SYSVIPC}, 191672756aaSJamie Gritton {"allow.raw_sockets", "allow.noraw_sockets", PR_ALLOW_RAW_SOCKETS}, 192672756aaSJamie Gritton {"allow.chflags", "allow.nochflags", PR_ALLOW_CHFLAGS}, 193672756aaSJamie Gritton {"allow.mount", "allow.nomount", PR_ALLOW_MOUNT}, 194672756aaSJamie Gritton {"allow.quotas", "allow.noquotas", PR_ALLOW_QUOTAS}, 195672756aaSJamie Gritton {"allow.socket_af", "allow.nosocket_af", PR_ALLOW_SOCKET_AF}, 196ccd6ac9fSAntoine Brodin {"allow.mlock", "allow.nomlock", PR_ALLOW_MLOCK}, 197672756aaSJamie Gritton {"allow.reserved_ports", "allow.noreserved_ports", 198672756aaSJamie Gritton PR_ALLOW_RESERVED_PORTS}, 199b19d66fdSJamie Gritton {"allow.read_msgbuf", "allow.noread_msgbuf", PR_ALLOW_READ_MSGBUF}, 200b3079544SJamie Gritton {"allow.unprivileged_proc_debug", "allow.nounprivileged_proc_debug", 201b3079544SJamie Gritton PR_ALLOW_UNPRIV_DEBUG}, 2020304c731SJamie Gritton }; 203672756aaSJamie Gritton const size_t pr_flag_allow_size = sizeof(pr_flag_allow); 2040304c731SJamie Gritton 205b3079544SJamie Gritton #define JAIL_DEFAULT_ALLOW (PR_ALLOW_SET_HOSTNAME | \ 206b3079544SJamie Gritton PR_ALLOW_RESERVED_PORTS | \ 207b3079544SJamie Gritton PR_ALLOW_UNPRIV_DEBUG) 20842bebd82SJamie Gritton #define JAIL_DEFAULT_ENFORCE_STATFS 2 209bf3db8aaSMartin Matuska #define JAIL_DEFAULT_DEVFS_RSNUM 0 2100304c731SJamie Gritton static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW; 21142bebd82SJamie Gritton static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 2120cc207a6SMartin Matuska static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM; 2130304c731SJamie Gritton #if defined(INET) || defined(INET6) 214e92e0574SJamie Gritton static unsigned jail_max_af_ips = 255; 2150304c731SJamie Gritton #endif 2160304c731SJamie Gritton 217b96bd95bSIan Lepore /* 218b96bd95bSIan Lepore * Initialize the parts of prison0 that can't be static-initialized with 219b96bd95bSIan Lepore * constants. This is called from proc0_init() after creating thread0 cpuset. 220b96bd95bSIan Lepore */ 221b96bd95bSIan Lepore void 222b96bd95bSIan Lepore prison0_init(void) 223b96bd95bSIan Lepore { 224c3188289SKyle Evans uint8_t *file, *data; 225c3188289SKyle Evans size_t size; 226b96bd95bSIan Lepore 227b96bd95bSIan Lepore prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset); 228b96bd95bSIan Lepore prison0.pr_osreldate = osreldate; 229b96bd95bSIan Lepore strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease)); 230c3188289SKyle Evans 231c3188289SKyle Evans /* If we have a preloaded hostuuid, use it. */ 232c3188289SKyle Evans file = preload_search_by_type(PRISON0_HOSTUUID_MODULE); 233c3188289SKyle Evans if (file != NULL) { 234c3188289SKyle Evans data = preload_fetch_addr(file); 235c3188289SKyle Evans size = preload_fetch_size(file); 236c3188289SKyle Evans if (data != NULL) { 237c3188289SKyle Evans /* 238c3188289SKyle Evans * The preloaded data may include trailing whitespace, almost 239c3188289SKyle Evans * certainly a newline; skip over any whitespace or 240c3188289SKyle Evans * non-printable characters to be safe. 241c3188289SKyle Evans */ 242c3188289SKyle Evans while (size > 0 && data[size - 1] <= 0x20) { 243c3188289SKyle Evans data[size--] = '\0'; 244c3188289SKyle Evans } 245c3188289SKyle Evans if (validate_uuid(data, size, NULL, 0) == 0) { 246c3188289SKyle Evans (void)strlcpy(prison0.pr_hostuuid, data, 247c3188289SKyle Evans size + 1); 248c3188289SKyle Evans } else if (bootverbose) { 249c3188289SKyle Evans printf("hostuuid: preload data malformed: '%s'", 250c3188289SKyle Evans data); 251c3188289SKyle Evans } 252c3188289SKyle Evans } 253c3188289SKyle Evans } 254c3188289SKyle Evans if (bootverbose) 255c3188289SKyle Evans printf("hostuuid: using %s\n", prison0.pr_hostuuid); 256b96bd95bSIan Lepore } 257b96bd95bSIan Lepore 258116734c4SMatthew Dillon /* 2599ddb7954SMike Barcroft * struct jail_args { 2609ddb7954SMike Barcroft * struct jail *jail; 2619ddb7954SMike Barcroft * }; 262116734c4SMatthew Dillon */ 26375c13541SPoul-Henning Kamp int 264c542c43eSJamie Gritton sys_jail(struct thread *td, struct jail_args *uap) 26575c13541SPoul-Henning Kamp { 266413628a7SBjoern A. Zeeb uint32_t version; 267413628a7SBjoern A. Zeeb int error; 2680304c731SJamie Gritton struct jail j; 269413628a7SBjoern A. Zeeb 270413628a7SBjoern A. Zeeb error = copyin(uap->jail, &version, sizeof(uint32_t)); 271413628a7SBjoern A. Zeeb if (error) 272413628a7SBjoern A. Zeeb return (error); 273413628a7SBjoern A. Zeeb 274413628a7SBjoern A. Zeeb switch (version) { 275413628a7SBjoern A. Zeeb case 0: 276413628a7SBjoern A. Zeeb { 277413628a7SBjoern A. Zeeb struct jail_v0 j0; 278413628a7SBjoern A. Zeeb 2790304c731SJamie Gritton /* FreeBSD single IPv4 jails. */ 2800304c731SJamie Gritton bzero(&j, sizeof(struct jail)); 281413628a7SBjoern A. Zeeb error = copyin(uap->jail, &j0, sizeof(struct jail_v0)); 282413628a7SBjoern A. Zeeb if (error) 283413628a7SBjoern A. Zeeb return (error); 2840304c731SJamie Gritton j.version = j0.version; 2850304c731SJamie Gritton j.path = j0.path; 2860304c731SJamie Gritton j.hostname = j0.hostname; 287b5019bc4SPeter Wemm j.ip4s = htonl(j0.ip_number); /* jail_v0 is host order */ 288413628a7SBjoern A. Zeeb break; 289413628a7SBjoern A. Zeeb } 290413628a7SBjoern A. Zeeb 291413628a7SBjoern A. Zeeb case 1: 292413628a7SBjoern A. Zeeb /* 293413628a7SBjoern A. Zeeb * Version 1 was used by multi-IPv4 jail implementations 294413628a7SBjoern A. Zeeb * that never made it into the official kernel. 295413628a7SBjoern A. Zeeb */ 296413628a7SBjoern A. Zeeb return (EINVAL); 297413628a7SBjoern A. Zeeb 298413628a7SBjoern A. Zeeb case 2: /* JAIL_API_VERSION */ 299413628a7SBjoern A. Zeeb /* FreeBSD multi-IPv4/IPv6,noIP jails. */ 300413628a7SBjoern A. Zeeb error = copyin(uap->jail, &j, sizeof(struct jail)); 301413628a7SBjoern A. Zeeb if (error) 302413628a7SBjoern A. Zeeb return (error); 3030304c731SJamie Gritton break; 3040304c731SJamie Gritton 3050304c731SJamie Gritton default: 3060304c731SJamie Gritton /* Sci-Fi jails are not supported, sorry. */ 3070304c731SJamie Gritton return (EINVAL); 3080304c731SJamie Gritton } 309c542c43eSJamie Gritton return (kern_jail(td, &j)); 3100304c731SJamie Gritton } 3110304c731SJamie Gritton 3120304c731SJamie Gritton int 313c542c43eSJamie Gritton kern_jail(struct thread *td, struct jail *j) 3140304c731SJamie Gritton { 315c542c43eSJamie Gritton struct iovec optiov[2 * (4 + nitems(pr_flag_allow) 316e92e0574SJamie Gritton #ifdef INET 317e92e0574SJamie Gritton + 1 318e92e0574SJamie Gritton #endif 319e92e0574SJamie Gritton #ifdef INET6 320e92e0574SJamie Gritton + 1 321e92e0574SJamie Gritton #endif 322e92e0574SJamie Gritton )]; 3230304c731SJamie Gritton struct uio opt; 3240304c731SJamie Gritton char *u_path, *u_hostname, *u_name; 325672756aaSJamie Gritton struct bool_flags *bf; 3260304c731SJamie Gritton #ifdef INET 327e92e0574SJamie Gritton uint32_t ip4s; 3280304c731SJamie Gritton struct in_addr *u_ip4; 3290304c731SJamie Gritton #endif 3300304c731SJamie Gritton #ifdef INET6 3310304c731SJamie Gritton struct in6_addr *u_ip6; 3320304c731SJamie Gritton #endif 3330304c731SJamie Gritton size_t tmplen; 334c542c43eSJamie Gritton int error, enforce_statfs; 3350304c731SJamie Gritton 3360304c731SJamie Gritton bzero(&optiov, sizeof(optiov)); 3370304c731SJamie Gritton opt.uio_iov = optiov; 3380304c731SJamie Gritton opt.uio_iovcnt = 0; 3390304c731SJamie Gritton opt.uio_offset = -1; 3400304c731SJamie Gritton opt.uio_resid = -1; 3410304c731SJamie Gritton opt.uio_segflg = UIO_SYSSPACE; 3420304c731SJamie Gritton opt.uio_rw = UIO_READ; 3430304c731SJamie Gritton opt.uio_td = td; 3440304c731SJamie Gritton 3450304c731SJamie Gritton /* Set permissions for top-level jails from sysctls. */ 3460304c731SJamie Gritton if (!jailed(td->td_ucred)) { 347672756aaSJamie Gritton for (bf = pr_flag_allow; 3480e5c6bd4SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 3490e5c6bd4SJamie Gritton bf->flag != 0; 350672756aaSJamie Gritton bf++) { 351672756aaSJamie Gritton optiov[opt.uio_iovcnt].iov_base = __DECONST(char *, 352672756aaSJamie Gritton (jail_default_allow & bf->flag) 353672756aaSJamie Gritton ? bf->name : bf->noname); 3540304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = 3550304c731SJamie Gritton strlen(optiov[opt.uio_iovcnt].iov_base) + 1; 3560304c731SJamie Gritton opt.uio_iovcnt += 2; 3570304c731SJamie Gritton } 3580304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "enforce_statfs"; 3590304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs"); 3600304c731SJamie Gritton opt.uio_iovcnt++; 3610304c731SJamie Gritton enforce_statfs = jail_default_enforce_statfs; 3620304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = &enforce_statfs; 3630304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs); 3640304c731SJamie Gritton opt.uio_iovcnt++; 3650304c731SJamie Gritton } 3660304c731SJamie Gritton 367b38ff370SJamie Gritton tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN; 368b38ff370SJamie Gritton #ifdef INET 3690304c731SJamie Gritton ip4s = (j->version == 0) ? 1 : j->ip4s; 3700304c731SJamie Gritton if (ip4s > jail_max_af_ips) 371b38ff370SJamie Gritton return (EINVAL); 3720304c731SJamie Gritton tmplen += ip4s * sizeof(struct in_addr); 373b38ff370SJamie Gritton #else 3740304c731SJamie Gritton if (j->ip4s > 0) 375b38ff370SJamie Gritton return (EINVAL); 376b38ff370SJamie Gritton #endif 377b38ff370SJamie Gritton #ifdef INET6 3780304c731SJamie Gritton if (j->ip6s > jail_max_af_ips) 379b38ff370SJamie Gritton return (EINVAL); 3800304c731SJamie Gritton tmplen += j->ip6s * sizeof(struct in6_addr); 381b38ff370SJamie Gritton #else 3820304c731SJamie Gritton if (j->ip6s > 0) 383b38ff370SJamie Gritton return (EINVAL); 384b38ff370SJamie Gritton #endif 385b38ff370SJamie Gritton u_path = malloc(tmplen, M_TEMP, M_WAITOK); 386b38ff370SJamie Gritton u_hostname = u_path + MAXPATHLEN; 387b38ff370SJamie Gritton u_name = u_hostname + MAXHOSTNAMELEN; 388b38ff370SJamie Gritton #ifdef INET 389b38ff370SJamie Gritton u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN); 390b38ff370SJamie Gritton #endif 391b38ff370SJamie Gritton #ifdef INET6 392b38ff370SJamie Gritton #ifdef INET 3930304c731SJamie Gritton u_ip6 = (struct in6_addr *)(u_ip4 + ip4s); 394b38ff370SJamie Gritton #else 395b38ff370SJamie Gritton u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN); 396b38ff370SJamie Gritton #endif 397b38ff370SJamie Gritton #endif 3980304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "path"; 3990304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("path"); 4000304c731SJamie Gritton opt.uio_iovcnt++; 4010304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_path; 4020304c731SJamie Gritton error = copyinstr(j->path, u_path, MAXPATHLEN, 4030304c731SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 404b38ff370SJamie Gritton if (error) { 405b38ff370SJamie Gritton free(u_path, M_TEMP); 406b38ff370SJamie Gritton return (error); 407b38ff370SJamie Gritton } 4080304c731SJamie Gritton opt.uio_iovcnt++; 4090304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "host.hostname"; 4100304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname"); 4110304c731SJamie Gritton opt.uio_iovcnt++; 4120304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_hostname; 4130304c731SJamie Gritton error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN, 4140304c731SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 415b38ff370SJamie Gritton if (error) { 416b38ff370SJamie Gritton free(u_path, M_TEMP); 417b38ff370SJamie Gritton return (error); 418b38ff370SJamie Gritton } 4190304c731SJamie Gritton opt.uio_iovcnt++; 4200304c731SJamie Gritton if (j->jailname != NULL) { 421b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "name"; 422b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("name"); 423b38ff370SJamie Gritton opt.uio_iovcnt++; 424b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_name; 4250304c731SJamie Gritton error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN, 426b38ff370SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 427b38ff370SJamie Gritton if (error) { 428b38ff370SJamie Gritton free(u_path, M_TEMP); 429b38ff370SJamie Gritton return (error); 430b38ff370SJamie Gritton } 431b38ff370SJamie Gritton opt.uio_iovcnt++; 432b38ff370SJamie Gritton } 433b38ff370SJamie Gritton #ifdef INET 434b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "ip4.addr"; 435b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr"); 436b38ff370SJamie Gritton opt.uio_iovcnt++; 437b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_ip4; 4380304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr); 4390304c731SJamie Gritton if (j->version == 0) 4400304c731SJamie Gritton u_ip4->s_addr = j->ip4s; 4410304c731SJamie Gritton else { 4420304c731SJamie Gritton error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len); 443b38ff370SJamie Gritton if (error) { 444b38ff370SJamie Gritton free(u_path, M_TEMP); 445b38ff370SJamie Gritton return (error); 446b38ff370SJamie Gritton } 4470304c731SJamie Gritton } 448b38ff370SJamie Gritton opt.uio_iovcnt++; 449b38ff370SJamie Gritton #endif 450b38ff370SJamie Gritton #ifdef INET6 451b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "ip6.addr"; 452b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr"); 453b38ff370SJamie Gritton opt.uio_iovcnt++; 454b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_ip6; 4550304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr); 4560304c731SJamie Gritton error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len); 457b38ff370SJamie Gritton if (error) { 458b38ff370SJamie Gritton free(u_path, M_TEMP); 459b38ff370SJamie Gritton return (error); 460b38ff370SJamie Gritton } 461b38ff370SJamie Gritton opt.uio_iovcnt++; 462b38ff370SJamie Gritton #endif 46302abd400SPedro F. Giffuni KASSERT(opt.uio_iovcnt <= nitems(optiov), 4640304c731SJamie Gritton ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt)); 465b38ff370SJamie Gritton error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH); 466b38ff370SJamie Gritton free(u_path, M_TEMP); 467b38ff370SJamie Gritton return (error); 468b38ff370SJamie Gritton } 469b38ff370SJamie Gritton 470b38ff370SJamie Gritton /* 471b38ff370SJamie Gritton * struct jail_set_args { 472b38ff370SJamie Gritton * struct iovec *iovp; 473b38ff370SJamie Gritton * unsigned int iovcnt; 474b38ff370SJamie Gritton * int flags; 475b38ff370SJamie Gritton * }; 476b38ff370SJamie Gritton */ 477b38ff370SJamie Gritton int 4788451d0ddSKip Macy sys_jail_set(struct thread *td, struct jail_set_args *uap) 479b38ff370SJamie Gritton { 480b38ff370SJamie Gritton struct uio *auio; 481b38ff370SJamie Gritton int error; 482b38ff370SJamie Gritton 483b38ff370SJamie Gritton /* Check that we have an even number of iovecs. */ 484b38ff370SJamie Gritton if (uap->iovcnt & 1) 485b38ff370SJamie Gritton return (EINVAL); 486b38ff370SJamie Gritton 487b38ff370SJamie Gritton error = copyinuio(uap->iovp, uap->iovcnt, &auio); 488b38ff370SJamie Gritton if (error) 489b38ff370SJamie Gritton return (error); 490b38ff370SJamie Gritton error = kern_jail_set(td, auio, uap->flags); 491b38ff370SJamie Gritton free(auio, M_IOV); 492b38ff370SJamie Gritton return (error); 493413628a7SBjoern A. Zeeb } 494413628a7SBjoern A. Zeeb 495413628a7SBjoern A. Zeeb int 496b38ff370SJamie Gritton kern_jail_set(struct thread *td, struct uio *optuio, int flags) 497413628a7SBjoern A. Zeeb { 498fd7a8150SMike Barcroft struct nameidata nd; 499b38ff370SJamie Gritton #ifdef INET 500b38ff370SJamie Gritton struct in_addr *ip4; 501413628a7SBjoern A. Zeeb #endif 502b38ff370SJamie Gritton #ifdef INET6 503b38ff370SJamie Gritton struct in6_addr *ip6; 504b38ff370SJamie Gritton #endif 505b38ff370SJamie Gritton struct vfsopt *opt; 506b38ff370SJamie Gritton struct vfsoptlist *opts; 50757aea6dfSBjoern A. Zeeb struct prison *pr, *deadpr, *mypr, *ppr, *tpr; 508b38ff370SJamie Gritton struct vnode *root; 509babbbb9cSJamie Gritton char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid; 510b96bd95bSIan Lepore char *g_path, *osrelstr; 511672756aaSJamie Gritton struct bool_flags *bf; 512672756aaSJamie Gritton struct jailsys_flags *jsf; 5130304c731SJamie Gritton #if defined(INET) || defined(INET6) 51457aea6dfSBjoern A. Zeeb struct prison *tppr; 515b38ff370SJamie Gritton void *op; 5160304c731SJamie Gritton #endif 51776ca6f88SJamie Gritton unsigned long hid; 518b6f47c23SJamie Gritton size_t namelen, onamelen, pnamelen; 519cc5fd8c7SJamie Gritton int born, created, cuflags, descend, enforce; 520cc5fd8c7SJamie Gritton int error, errmsg_len, errmsg_pos; 5210cc207a6SMartin Matuska int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel; 522672756aaSJamie Gritton int jid, jsys, len, level; 523b96bd95bSIan Lepore int childmax, osreldt, rsnum, slevel; 524d465a41dSBjoern A. Zeeb #if defined(INET) || defined(INET6) 5250304c731SJamie Gritton int ii, ij; 526413628a7SBjoern A. Zeeb #endif 527413628a7SBjoern A. Zeeb #ifdef INET 5282b0d6f81SJamie Gritton int ip4s, redo_ip4; 529413628a7SBjoern A. Zeeb #endif 530b38ff370SJamie Gritton #ifdef INET6 5312b0d6f81SJamie Gritton int ip6s, redo_ip6; 532b38ff370SJamie Gritton #endif 5336beb3bb4SKirk McKusick uint64_t pr_allow, ch_allow, pr_flags, ch_flags; 534b3079544SJamie Gritton uint64_t pr_allow_diff; 5356beb3bb4SKirk McKusick unsigned tallow; 536b38ff370SJamie Gritton char numbuf[12]; 537b38ff370SJamie Gritton 538b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_SET); 539b38ff370SJamie Gritton if (!error && (flags & JAIL_ATTACH)) 540b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 541b38ff370SJamie Gritton if (error) 54275c13541SPoul-Henning Kamp return (error); 543b6f47c23SJamie Gritton mypr = td->td_ucred->cr_prison; 544b97457e2SJamie Gritton if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0) 5450304c731SJamie Gritton return (EPERM); 546b38ff370SJamie Gritton if (flags & ~JAIL_SET_MASK) 547b38ff370SJamie Gritton return (EINVAL); 548b38ff370SJamie Gritton 549b38ff370SJamie Gritton /* 550b38ff370SJamie Gritton * Check all the parameters before committing to anything. Not all 551b38ff370SJamie Gritton * errors can be caught early, but we may as well try. Also, this 552b38ff370SJamie Gritton * takes care of some expensive stuff (path lookup) before getting 553b38ff370SJamie Gritton * the allprison lock. 554b38ff370SJamie Gritton * 555b38ff370SJamie Gritton * XXX Jails are not filesystems, and jail parameters are not mount 556b38ff370SJamie Gritton * options. But it makes more sense to re-use the vfsopt code 557b38ff370SJamie Gritton * than duplicate it under a different name. 558b38ff370SJamie Gritton */ 559b38ff370SJamie Gritton error = vfs_buildopts(optuio, &opts); 560b38ff370SJamie Gritton if (error) 561b38ff370SJamie Gritton return (error); 562b38ff370SJamie Gritton #ifdef INET 563b38ff370SJamie Gritton ip4 = NULL; 564b38ff370SJamie Gritton #endif 565b38ff370SJamie Gritton #ifdef INET6 566b38ff370SJamie Gritton ip6 = NULL; 567b38ff370SJamie Gritton #endif 5686dfe0a3dSMartin Matuska g_path = NULL; 569b38ff370SJamie Gritton 570b6f47c23SJamie Gritton cuflags = flags & (JAIL_CREATE | JAIL_UPDATE); 571b6f47c23SJamie Gritton if (!cuflags) { 572b6f47c23SJamie Gritton error = EINVAL; 573b6f47c23SJamie Gritton vfs_opterror(opts, "no valid operation (create or update)"); 574b6f47c23SJamie Gritton goto done_errmsg; 575b6f47c23SJamie Gritton } 576b6f47c23SJamie Gritton 577b38ff370SJamie Gritton error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 578b38ff370SJamie Gritton if (error == ENOENT) 579b38ff370SJamie Gritton jid = 0; 580b38ff370SJamie Gritton else if (error != 0) 581b38ff370SJamie Gritton goto done_free; 582b38ff370SJamie Gritton 583b38ff370SJamie Gritton error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel)); 584b38ff370SJamie Gritton if (error == ENOENT) 585b38ff370SJamie Gritton gotslevel = 0; 586b38ff370SJamie Gritton else if (error != 0) 587b38ff370SJamie Gritton goto done_free; 588b38ff370SJamie Gritton else 589b38ff370SJamie Gritton gotslevel = 1; 590b38ff370SJamie Gritton 591b97457e2SJamie Gritton error = 592b97457e2SJamie Gritton vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax)); 593b97457e2SJamie Gritton if (error == ENOENT) 594b97457e2SJamie Gritton gotchildmax = 0; 595b97457e2SJamie Gritton else if (error != 0) 596b97457e2SJamie Gritton goto done_free; 597b97457e2SJamie Gritton else 598b97457e2SJamie Gritton gotchildmax = 1; 599b97457e2SJamie Gritton 6000304c731SJamie Gritton error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce)); 601f337198dSJamie Gritton if (error == ENOENT) 602f337198dSJamie Gritton gotenforce = 0; 603f337198dSJamie Gritton else if (error != 0) 6040304c731SJamie Gritton goto done_free; 605f337198dSJamie Gritton else if (enforce < 0 || enforce > 2) { 606f337198dSJamie Gritton error = EINVAL; 607f337198dSJamie Gritton goto done_free; 608f337198dSJamie Gritton } else 609f337198dSJamie Gritton gotenforce = 1; 6100304c731SJamie Gritton 6110cc207a6SMartin Matuska error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum)); 6120cc207a6SMartin Matuska if (error == ENOENT) 6130cc207a6SMartin Matuska gotrsnum = 0; 6140cc207a6SMartin Matuska else if (error != 0) 6150cc207a6SMartin Matuska goto done_free; 6160cc207a6SMartin Matuska else 6170cc207a6SMartin Matuska gotrsnum = 1; 6180cc207a6SMartin Matuska 619b38ff370SJamie Gritton pr_flags = ch_flags = 0; 620672756aaSJamie Gritton for (bf = pr_flag_bool; 621672756aaSJamie Gritton bf < pr_flag_bool + nitems(pr_flag_bool); 622672756aaSJamie Gritton bf++) { 623672756aaSJamie Gritton vfs_flagopt(opts, bf->name, &pr_flags, bf->flag); 624672756aaSJamie Gritton vfs_flagopt(opts, bf->noname, &ch_flags, bf->flag); 6250304c731SJamie Gritton } 626b38ff370SJamie Gritton ch_flags |= pr_flags; 627672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 628672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 629672756aaSJamie Gritton jsf++) { 630672756aaSJamie Gritton error = vfs_copyopt(opts, jsf->name, &jsys, sizeof(jsys)); 6317cbf7213SJamie Gritton if (error == ENOENT) 6327cbf7213SJamie Gritton continue; 6337cbf7213SJamie Gritton if (error != 0) 6347cbf7213SJamie Gritton goto done_free; 6357cbf7213SJamie Gritton switch (jsys) { 6367cbf7213SJamie Gritton case JAIL_SYS_DISABLE: 637672756aaSJamie Gritton if (!jsf->disable) { 6387cbf7213SJamie Gritton error = EINVAL; 6397cbf7213SJamie Gritton goto done_free; 6407cbf7213SJamie Gritton } 641672756aaSJamie Gritton pr_flags |= jsf->disable; 6427cbf7213SJamie Gritton break; 6437cbf7213SJamie Gritton case JAIL_SYS_NEW: 644672756aaSJamie Gritton pr_flags |= jsf->new; 6457cbf7213SJamie Gritton break; 6467cbf7213SJamie Gritton case JAIL_SYS_INHERIT: 6477cbf7213SJamie Gritton break; 6487cbf7213SJamie Gritton default: 6497cbf7213SJamie Gritton error = EINVAL; 6507cbf7213SJamie Gritton goto done_free; 6517cbf7213SJamie Gritton } 652672756aaSJamie Gritton ch_flags |= jsf->new | jsf->disable; 6537cbf7213SJamie Gritton } 6544affa14cSJamie Gritton if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE 6554affa14cSJamie Gritton && !(pr_flags & PR_PERSIST)) { 6564affa14cSJamie Gritton error = EINVAL; 6574affa14cSJamie Gritton vfs_opterror(opts, "new jail must persist or attach"); 6584affa14cSJamie Gritton goto done_errmsg; 6594affa14cSJamie Gritton } 660679e1390SJamie Gritton #ifdef VIMAGE 661679e1390SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) { 662679e1390SJamie Gritton error = EINVAL; 663679e1390SJamie Gritton vfs_opterror(opts, "vnet cannot be changed after creation"); 664679e1390SJamie Gritton goto done_errmsg; 665679e1390SJamie Gritton } 666679e1390SJamie Gritton #endif 6672b0d6f81SJamie Gritton #ifdef INET 6682b0d6f81SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) { 6692b0d6f81SJamie Gritton error = EINVAL; 6702b0d6f81SJamie Gritton vfs_opterror(opts, "ip4 cannot be changed after creation"); 6712b0d6f81SJamie Gritton goto done_errmsg; 6722b0d6f81SJamie Gritton } 6732b0d6f81SJamie Gritton #endif 6742b0d6f81SJamie Gritton #ifdef INET6 6752b0d6f81SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) { 6762b0d6f81SJamie Gritton error = EINVAL; 6772b0d6f81SJamie Gritton vfs_opterror(opts, "ip6 cannot be changed after creation"); 6782b0d6f81SJamie Gritton goto done_errmsg; 6792b0d6f81SJamie Gritton } 6802b0d6f81SJamie Gritton #endif 681b38ff370SJamie Gritton 6820304c731SJamie Gritton pr_allow = ch_allow = 0; 683672756aaSJamie Gritton for (bf = pr_flag_allow; 6840e5c6bd4SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && bf->flag != 0; 685672756aaSJamie Gritton bf++) { 686672756aaSJamie Gritton vfs_flagopt(opts, bf->name, &pr_allow, bf->flag); 687672756aaSJamie Gritton vfs_flagopt(opts, bf->noname, &ch_allow, bf->flag); 6880304c731SJamie Gritton } 6890304c731SJamie Gritton ch_allow |= pr_allow; 6900304c731SJamie Gritton 691b38ff370SJamie Gritton error = vfs_getopt(opts, "name", (void **)&name, &len); 692b38ff370SJamie Gritton if (error == ENOENT) 693b38ff370SJamie Gritton name = NULL; 694b38ff370SJamie Gritton else if (error != 0) 695b38ff370SJamie Gritton goto done_free; 696b38ff370SJamie Gritton else { 697b38ff370SJamie Gritton if (len == 0 || name[len - 1] != '\0') { 698b38ff370SJamie Gritton error = EINVAL; 699b38ff370SJamie Gritton goto done_free; 700b38ff370SJamie Gritton } 701b38ff370SJamie Gritton if (len > MAXHOSTNAMELEN) { 702b38ff370SJamie Gritton error = ENAMETOOLONG; 703b38ff370SJamie Gritton goto done_free; 704b38ff370SJamie Gritton } 705b38ff370SJamie Gritton } 706b38ff370SJamie Gritton 707b38ff370SJamie Gritton error = vfs_getopt(opts, "host.hostname", (void **)&host, &len); 708b38ff370SJamie Gritton if (error == ENOENT) 709b38ff370SJamie Gritton host = NULL; 710b38ff370SJamie Gritton else if (error != 0) 711b38ff370SJamie Gritton goto done_free; 712b38ff370SJamie Gritton else { 71376ca6f88SJamie Gritton ch_flags |= PR_HOST; 71476ca6f88SJamie Gritton pr_flags |= PR_HOST; 715b38ff370SJamie Gritton if (len == 0 || host[len - 1] != '\0') { 716b38ff370SJamie Gritton error = EINVAL; 717b38ff370SJamie Gritton goto done_free; 718b38ff370SJamie Gritton } 719b38ff370SJamie Gritton if (len > MAXHOSTNAMELEN) { 720b38ff370SJamie Gritton error = ENAMETOOLONG; 721b38ff370SJamie Gritton goto done_free; 722b38ff370SJamie Gritton } 723b38ff370SJamie Gritton } 724b38ff370SJamie Gritton 72576ca6f88SJamie Gritton error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len); 72676ca6f88SJamie Gritton if (error == ENOENT) 72776ca6f88SJamie Gritton domain = NULL; 72876ca6f88SJamie Gritton else if (error != 0) 72976ca6f88SJamie Gritton goto done_free; 73076ca6f88SJamie Gritton else { 73176ca6f88SJamie Gritton ch_flags |= PR_HOST; 73276ca6f88SJamie Gritton pr_flags |= PR_HOST; 73376ca6f88SJamie Gritton if (len == 0 || domain[len - 1] != '\0') { 73476ca6f88SJamie Gritton error = EINVAL; 73576ca6f88SJamie Gritton goto done_free; 73676ca6f88SJamie Gritton } 73776ca6f88SJamie Gritton if (len > MAXHOSTNAMELEN) { 73876ca6f88SJamie Gritton error = ENAMETOOLONG; 73976ca6f88SJamie Gritton goto done_free; 74076ca6f88SJamie Gritton } 74176ca6f88SJamie Gritton } 74276ca6f88SJamie Gritton 74376ca6f88SJamie Gritton error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len); 74476ca6f88SJamie Gritton if (error == ENOENT) 74576ca6f88SJamie Gritton uuid = NULL; 74676ca6f88SJamie Gritton else if (error != 0) 74776ca6f88SJamie Gritton goto done_free; 74876ca6f88SJamie Gritton else { 74976ca6f88SJamie Gritton ch_flags |= PR_HOST; 75076ca6f88SJamie Gritton pr_flags |= PR_HOST; 75176ca6f88SJamie Gritton if (len == 0 || uuid[len - 1] != '\0') { 75276ca6f88SJamie Gritton error = EINVAL; 75376ca6f88SJamie Gritton goto done_free; 75476ca6f88SJamie Gritton } 75576ca6f88SJamie Gritton if (len > HOSTUUIDLEN) { 75676ca6f88SJamie Gritton error = ENAMETOOLONG; 75776ca6f88SJamie Gritton goto done_free; 75876ca6f88SJamie Gritton } 75976ca6f88SJamie Gritton } 76076ca6f88SJamie Gritton 761841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32 762a5c1afadSDmitry Chagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 76376ca6f88SJamie Gritton uint32_t hid32; 76476ca6f88SJamie Gritton 76576ca6f88SJamie Gritton error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32)); 76676ca6f88SJamie Gritton hid = hid32; 76776ca6f88SJamie Gritton } else 76876ca6f88SJamie Gritton #endif 76976ca6f88SJamie Gritton error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid)); 77076ca6f88SJamie Gritton if (error == ENOENT) 77176ca6f88SJamie Gritton gothid = 0; 77276ca6f88SJamie Gritton else if (error != 0) 77376ca6f88SJamie Gritton goto done_free; 77476ca6f88SJamie Gritton else { 77576ca6f88SJamie Gritton gothid = 1; 77676ca6f88SJamie Gritton ch_flags |= PR_HOST; 77776ca6f88SJamie Gritton pr_flags |= PR_HOST; 77876ca6f88SJamie Gritton } 77976ca6f88SJamie Gritton 780b38ff370SJamie Gritton #ifdef INET 781b38ff370SJamie Gritton error = vfs_getopt(opts, "ip4.addr", &op, &ip4s); 782b38ff370SJamie Gritton if (error == ENOENT) 7830e5e396eSJamie Gritton ip4s = 0; 784b38ff370SJamie Gritton else if (error != 0) 785b38ff370SJamie Gritton goto done_free; 786b38ff370SJamie Gritton else if (ip4s & (sizeof(*ip4) - 1)) { 787b38ff370SJamie Gritton error = EINVAL; 788b38ff370SJamie Gritton goto done_free; 7890304c731SJamie Gritton } else { 7906a3f2779SJamie Gritton ch_flags |= PR_IP4_USER; 7916a3f2779SJamie Gritton pr_flags |= PR_IP4_USER; 7926a3f2779SJamie Gritton if (ip4s > 0) { 793b38ff370SJamie Gritton ip4s /= sizeof(*ip4); 794b38ff370SJamie Gritton if (ip4s > jail_max_af_ips) { 795b38ff370SJamie Gritton error = EINVAL; 796b38ff370SJamie Gritton vfs_opterror(opts, "too many IPv4 addresses"); 797b38ff370SJamie Gritton goto done_errmsg; 798b38ff370SJamie Gritton } 7992b0d6f81SJamie Gritton ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 800b38ff370SJamie Gritton bcopy(op, ip4, ip4s * sizeof(*ip4)); 801b38ff370SJamie Gritton /* 8020304c731SJamie Gritton * IP addresses are all sorted but ip[0] to preserve 8030304c731SJamie Gritton * the primary IP address as given from userland. 8040304c731SJamie Gritton * This special IP is used for unbound outgoing 805bef916f9SBjoern A. Zeeb * connections as well for "loopback" traffic in case 806bef916f9SBjoern A. Zeeb * source address selection cannot find any more fitting 807bef916f9SBjoern A. Zeeb * address to connect from. 808b38ff370SJamie Gritton */ 809b38ff370SJamie Gritton if (ip4s > 1) 8100ce1624dSStephen J. Kiernan qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), 8110ce1624dSStephen J. Kiernan prison_qcmp_v4); 812b38ff370SJamie Gritton /* 8130304c731SJamie Gritton * Check for duplicate addresses and do some simple 8140304c731SJamie Gritton * zero and broadcast checks. If users give other bogus 8150304c731SJamie Gritton * addresses it is their problem. 816b38ff370SJamie Gritton * 8170304c731SJamie Gritton * We do not have to care about byte order for these 8180304c731SJamie Gritton * checks so we will do them in NBO. 819b38ff370SJamie Gritton */ 820b38ff370SJamie Gritton for (ii = 0; ii < ip4s; ii++) { 821b38ff370SJamie Gritton if (ip4[ii].s_addr == INADDR_ANY || 822b38ff370SJamie Gritton ip4[ii].s_addr == INADDR_BROADCAST) { 823b38ff370SJamie Gritton error = EINVAL; 824b38ff370SJamie Gritton goto done_free; 825b38ff370SJamie Gritton } 826b38ff370SJamie Gritton if ((ii+1) < ip4s && 827b38ff370SJamie Gritton (ip4[0].s_addr == ip4[ii+1].s_addr || 828b38ff370SJamie Gritton ip4[ii].s_addr == ip4[ii+1].s_addr)) { 829b38ff370SJamie Gritton error = EINVAL; 830b38ff370SJamie Gritton goto done_free; 831b38ff370SJamie Gritton } 832b38ff370SJamie Gritton } 833b38ff370SJamie Gritton } 8340304c731SJamie Gritton } 835b38ff370SJamie Gritton #endif 836b38ff370SJamie Gritton 837b38ff370SJamie Gritton #ifdef INET6 838b38ff370SJamie Gritton error = vfs_getopt(opts, "ip6.addr", &op, &ip6s); 839b38ff370SJamie Gritton if (error == ENOENT) 8400e5e396eSJamie Gritton ip6s = 0; 841b38ff370SJamie Gritton else if (error != 0) 842b38ff370SJamie Gritton goto done_free; 843b38ff370SJamie Gritton else if (ip6s & (sizeof(*ip6) - 1)) { 844b38ff370SJamie Gritton error = EINVAL; 845b38ff370SJamie Gritton goto done_free; 8460304c731SJamie Gritton } else { 8476a3f2779SJamie Gritton ch_flags |= PR_IP6_USER; 8486a3f2779SJamie Gritton pr_flags |= PR_IP6_USER; 8496a3f2779SJamie Gritton if (ip6s > 0) { 850b38ff370SJamie Gritton ip6s /= sizeof(*ip6); 851b38ff370SJamie Gritton if (ip6s > jail_max_af_ips) { 852b38ff370SJamie Gritton error = EINVAL; 853b38ff370SJamie Gritton vfs_opterror(opts, "too many IPv6 addresses"); 854b38ff370SJamie Gritton goto done_errmsg; 855b38ff370SJamie Gritton } 8562b0d6f81SJamie Gritton ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 857b38ff370SJamie Gritton bcopy(op, ip6, ip6s * sizeof(*ip6)); 858b38ff370SJamie Gritton if (ip6s > 1) 8590ce1624dSStephen J. Kiernan qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), 8600ce1624dSStephen J. Kiernan prison_qcmp_v6); 861b38ff370SJamie Gritton for (ii = 0; ii < ip6s; ii++) { 8620304c731SJamie Gritton if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) { 863b38ff370SJamie Gritton error = EINVAL; 864b38ff370SJamie Gritton goto done_free; 865b38ff370SJamie Gritton } 866b38ff370SJamie Gritton if ((ii+1) < ip6s && 867b38ff370SJamie Gritton (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) || 868b38ff370SJamie Gritton IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1]))) 869b38ff370SJamie Gritton { 870b38ff370SJamie Gritton error = EINVAL; 871b38ff370SJamie Gritton goto done_free; 872b38ff370SJamie Gritton } 873b38ff370SJamie Gritton } 874b38ff370SJamie Gritton } 8750304c731SJamie Gritton } 876b38ff370SJamie Gritton #endif 877b38ff370SJamie Gritton 878bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6)) 879bdfc8cc4SJamie Gritton if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 880bdfc8cc4SJamie Gritton error = EINVAL; 881bdfc8cc4SJamie Gritton vfs_opterror(opts, 882bdfc8cc4SJamie Gritton "vnet jails cannot have IP address restrictions"); 883bdfc8cc4SJamie Gritton goto done_errmsg; 884bdfc8cc4SJamie Gritton } 885bdfc8cc4SJamie Gritton #endif 886bdfc8cc4SJamie Gritton 887cf0313c6SJamie Gritton error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len); 888cf0313c6SJamie Gritton if (error == ENOENT) 889cf0313c6SJamie Gritton osrelstr = NULL; 890cf0313c6SJamie Gritton else if (error != 0) 891cf0313c6SJamie Gritton goto done_free; 892cf0313c6SJamie Gritton else { 893cf0313c6SJamie Gritton if (flags & JAIL_UPDATE) { 894cf0313c6SJamie Gritton error = EINVAL; 895cf0313c6SJamie Gritton vfs_opterror(opts, 896cf0313c6SJamie Gritton "osrelease cannot be changed after creation"); 897cf0313c6SJamie Gritton goto done_errmsg; 898cf0313c6SJamie Gritton } 8991b786d01SBjoern A. Zeeb if (len == 0 || osrelstr[len - 1] != '\0') { 900cf0313c6SJamie Gritton error = EINVAL; 9011b786d01SBjoern A. Zeeb goto done_free; 9021b786d01SBjoern A. Zeeb } 9031b786d01SBjoern A. Zeeb if (len >= OSRELEASELEN) { 9041b786d01SBjoern A. Zeeb error = ENAMETOOLONG; 905cf0313c6SJamie Gritton vfs_opterror(opts, 906cf0313c6SJamie Gritton "osrelease string must be 1-%d bytes long", 907cf0313c6SJamie Gritton OSRELEASELEN - 1); 908cf0313c6SJamie Gritton goto done_errmsg; 909cf0313c6SJamie Gritton } 910cf0313c6SJamie Gritton } 911cf0313c6SJamie Gritton 912cf0313c6SJamie Gritton error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt)); 913cf0313c6SJamie Gritton if (error == ENOENT) 914cf0313c6SJamie Gritton osreldt = 0; 915cf0313c6SJamie Gritton else if (error != 0) 916cf0313c6SJamie Gritton goto done_free; 917cf0313c6SJamie Gritton else { 918cf0313c6SJamie Gritton if (flags & JAIL_UPDATE) { 919cf0313c6SJamie Gritton error = EINVAL; 920cf0313c6SJamie Gritton vfs_opterror(opts, 921cf0313c6SJamie Gritton "osreldate cannot be changed after creation"); 922cf0313c6SJamie Gritton goto done_errmsg; 923cf0313c6SJamie Gritton } 924cf0313c6SJamie Gritton if (osreldt == 0) { 925cf0313c6SJamie Gritton error = EINVAL; 926cf0313c6SJamie Gritton vfs_opterror(opts, "osreldate cannot be 0"); 927cf0313c6SJamie Gritton goto done_errmsg; 928cf0313c6SJamie Gritton } 929cf0313c6SJamie Gritton } 930cf0313c6SJamie Gritton 931b38ff370SJamie Gritton root = NULL; 932b38ff370SJamie Gritton error = vfs_getopt(opts, "path", (void **)&path, &len); 933b38ff370SJamie Gritton if (error == ENOENT) 934b38ff370SJamie Gritton path = NULL; 935b38ff370SJamie Gritton else if (error != 0) 936b38ff370SJamie Gritton goto done_free; 937b38ff370SJamie Gritton else { 938b38ff370SJamie Gritton if (flags & JAIL_UPDATE) { 939b38ff370SJamie Gritton error = EINVAL; 940b38ff370SJamie Gritton vfs_opterror(opts, 941b38ff370SJamie Gritton "path cannot be changed after creation"); 942b38ff370SJamie Gritton goto done_errmsg; 943b38ff370SJamie Gritton } 944b38ff370SJamie Gritton if (len == 0 || path[len - 1] != '\0') { 945b38ff370SJamie Gritton error = EINVAL; 946b38ff370SJamie Gritton goto done_free; 947b38ff370SJamie Gritton } 9485050aa86SKonstantin Belousov NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, 949b38ff370SJamie Gritton path, td); 950b38ff370SJamie Gritton error = namei(&nd); 951b38ff370SJamie Gritton if (error) 952b38ff370SJamie Gritton goto done_free; 953b38ff370SJamie Gritton root = nd.ni_vp; 954b38ff370SJamie Gritton NDFREE(&nd, NDF_ONLY_PNBUF); 9556dfe0a3dSMartin Matuska g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 9566dfe0a3dSMartin Matuska strlcpy(g_path, path, MAXPATHLEN); 9576dfe0a3dSMartin Matuska error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN); 9583eb6b656SMateusz Guzik if (error == 0) { 9596dfe0a3dSMartin Matuska path = g_path; 9606dfe0a3dSMartin Matuska } else { 961f6e633a9SMartin Matuska /* exit on other errors */ 962b38ff370SJamie Gritton goto done_free; 963b38ff370SJamie Gritton } 964f6e633a9SMartin Matuska if (root->v_type != VDIR) { 965f6e633a9SMartin Matuska error = ENOTDIR; 966f6e633a9SMartin Matuska vput(root); 967f6e633a9SMartin Matuska goto done_free; 968f6e633a9SMartin Matuska } 969b249ce48SMateusz Guzik VOP_UNLOCK(root); 970b38ff370SJamie Gritton } 971b38ff370SJamie Gritton 972b38ff370SJamie Gritton /* 973b6f47c23SJamie Gritton * Find the specified jail, or at least its parent. 974b38ff370SJamie Gritton * This abuses the file error codes ENOENT and EEXIST. 975b38ff370SJamie Gritton */ 976b38ff370SJamie Gritton pr = NULL; 977b6f47c23SJamie Gritton ppr = mypr; 978babbbb9cSJamie Gritton if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) { 979babbbb9cSJamie Gritton namelc = strrchr(name, '.'); 980babbbb9cSJamie Gritton jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10); 981babbbb9cSJamie Gritton if (*p != '\0') 982babbbb9cSJamie Gritton jid = 0; 983babbbb9cSJamie Gritton } 984b6f47c23SJamie Gritton sx_xlock(&allprison_lock); 985b38ff370SJamie Gritton if (jid != 0) { 9860304c731SJamie Gritton /* 9870304c731SJamie Gritton * See if a requested jid already exists. There is an 9880304c731SJamie Gritton * information leak here if the jid exists but is not within 9890304c731SJamie Gritton * the caller's jail hierarchy. Jail creators will get EEXIST 9900304c731SJamie Gritton * even though they cannot see the jail, and CREATE | UPDATE 9910304c731SJamie Gritton * will return ENOENT which is not normally a valid error. 9920304c731SJamie Gritton */ 993b38ff370SJamie Gritton if (jid < 0) { 994b38ff370SJamie Gritton error = EINVAL; 995b38ff370SJamie Gritton vfs_opterror(opts, "negative jid"); 996b38ff370SJamie Gritton goto done_unlock_list; 997b38ff370SJamie Gritton } 998b38ff370SJamie Gritton pr = prison_find(jid); 999b38ff370SJamie Gritton if (pr != NULL) { 10000304c731SJamie Gritton ppr = pr->pr_parent; 1001b38ff370SJamie Gritton /* Create: jid must not exist. */ 1002b38ff370SJamie Gritton if (cuflags == JAIL_CREATE) { 1003b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1004b38ff370SJamie Gritton error = EEXIST; 1005b38ff370SJamie Gritton vfs_opterror(opts, "jail %d already exists", 1006b38ff370SJamie Gritton jid); 1007b38ff370SJamie Gritton goto done_unlock_list; 1008b38ff370SJamie Gritton } 10090304c731SJamie Gritton if (!prison_ischild(mypr, pr)) { 10100304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 10110304c731SJamie Gritton pr = NULL; 10120304c731SJamie Gritton } else if (pr->pr_uref == 0) { 1013b38ff370SJamie Gritton if (!(flags & JAIL_DYING)) { 1014b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1015b38ff370SJamie Gritton error = ENOENT; 1016b38ff370SJamie Gritton vfs_opterror(opts, "jail %d is dying", 1017b38ff370SJamie Gritton jid); 1018b38ff370SJamie Gritton goto done_unlock_list; 1019b38ff370SJamie Gritton } else if ((flags & JAIL_ATTACH) || 1020b38ff370SJamie Gritton (pr_flags & PR_PERSIST)) { 1021b38ff370SJamie Gritton /* 1022b38ff370SJamie Gritton * A dying jail might be resurrected 1023b38ff370SJamie Gritton * (via attach or persist), but first 1024b38ff370SJamie Gritton * it must determine if another jail 1025b38ff370SJamie Gritton * has claimed its name. Accomplish 1026b38ff370SJamie Gritton * this by implicitly re-setting the 1027b38ff370SJamie Gritton * name. 1028b38ff370SJamie Gritton */ 1029b38ff370SJamie Gritton if (name == NULL) 10300304c731SJamie Gritton name = prison_name(mypr, pr); 1031b38ff370SJamie Gritton } 1032b38ff370SJamie Gritton } 1033b38ff370SJamie Gritton } 1034b38ff370SJamie Gritton if (pr == NULL) { 1035b38ff370SJamie Gritton /* Update: jid must exist. */ 1036b38ff370SJamie Gritton if (cuflags == JAIL_UPDATE) { 1037b38ff370SJamie Gritton error = ENOENT; 1038b38ff370SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 1039b38ff370SJamie Gritton goto done_unlock_list; 1040b38ff370SJamie Gritton } 1041b38ff370SJamie Gritton } 1042b38ff370SJamie Gritton } 1043b38ff370SJamie Gritton /* 1044b38ff370SJamie Gritton * If the caller provided a name, look for a jail by that name. 1045b38ff370SJamie Gritton * This has different semantics for creates and updates keyed by jid 1046b38ff370SJamie Gritton * (where the name must not already exist in a different jail), 1047b38ff370SJamie Gritton * and updates keyed by the name itself (where the name must exist 1048b38ff370SJamie Gritton * because that is the jail being updated). 1049b38ff370SJamie Gritton */ 1050b6f47c23SJamie Gritton namelc = NULL; 1051b38ff370SJamie Gritton if (name != NULL) { 1052babbbb9cSJamie Gritton namelc = strrchr(name, '.'); 1053babbbb9cSJamie Gritton if (namelc == NULL) 1054babbbb9cSJamie Gritton namelc = name; 1055babbbb9cSJamie Gritton else { 10560304c731SJamie Gritton /* 10570304c731SJamie Gritton * This is a hierarchical name. Split it into the 10580304c731SJamie Gritton * parent and child names, and make sure the parent 10590304c731SJamie Gritton * exists or matches an already found jail. 10600304c731SJamie Gritton */ 10610304c731SJamie Gritton if (pr != NULL) { 1062babbbb9cSJamie Gritton if (strncmp(name, ppr->pr_name, namelc - name) 1063babbbb9cSJamie Gritton || ppr->pr_name[namelc - name] != '\0') { 10640304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 10650304c731SJamie Gritton error = EINVAL; 10660304c731SJamie Gritton vfs_opterror(opts, 10670304c731SJamie Gritton "cannot change jail's parent"); 10680304c731SJamie Gritton goto done_unlock_list; 10690304c731SJamie Gritton } 10700304c731SJamie Gritton } else { 1071b6f47c23SJamie Gritton *namelc = '\0'; 10720304c731SJamie Gritton ppr = prison_find_name(mypr, name); 10730304c731SJamie Gritton if (ppr == NULL) { 10740304c731SJamie Gritton error = ENOENT; 10750304c731SJamie Gritton vfs_opterror(opts, 10760304c731SJamie Gritton "jail \"%s\" not found", name); 10770304c731SJamie Gritton goto done_unlock_list; 10780304c731SJamie Gritton } 10790304c731SJamie Gritton mtx_unlock(&ppr->pr_mtx); 1080b6f47c23SJamie Gritton *namelc = '.'; 10810304c731SJamie Gritton } 1082b6f47c23SJamie Gritton namelc++; 10830304c731SJamie Gritton } 1084b6f47c23SJamie Gritton if (namelc[0] != '\0') { 1085b6f47c23SJamie Gritton pnamelen = 10860304c731SJamie Gritton (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 1087b38ff370SJamie Gritton name_again: 10880304c731SJamie Gritton deadpr = NULL; 10890304c731SJamie Gritton FOREACH_PRISON_CHILD(ppr, tpr) { 1090b38ff370SJamie Gritton if (tpr != pr && tpr->pr_ref > 0 && 1091b6f47c23SJamie Gritton !strcmp(tpr->pr_name + pnamelen, namelc)) { 1092b38ff370SJamie Gritton if (pr == NULL && 1093b38ff370SJamie Gritton cuflags != JAIL_CREATE) { 1094b38ff370SJamie Gritton mtx_lock(&tpr->pr_mtx); 1095b38ff370SJamie Gritton if (tpr->pr_ref > 0) { 1096b38ff370SJamie Gritton /* 1097b38ff370SJamie Gritton * Use this jail 1098b38ff370SJamie Gritton * for updates. 1099b38ff370SJamie Gritton */ 1100b38ff370SJamie Gritton if (tpr->pr_uref > 0) { 1101b38ff370SJamie Gritton pr = tpr; 1102b38ff370SJamie Gritton break; 1103b38ff370SJamie Gritton } 1104b38ff370SJamie Gritton deadpr = tpr; 1105b38ff370SJamie Gritton } 1106b38ff370SJamie Gritton mtx_unlock(&tpr->pr_mtx); 1107b38ff370SJamie Gritton } else if (tpr->pr_uref > 0) { 1108b38ff370SJamie Gritton /* 1109b38ff370SJamie Gritton * Create, or update(jid): 1110b38ff370SJamie Gritton * name must not exist in an 11110304c731SJamie Gritton * active sibling jail. 1112b38ff370SJamie Gritton */ 1113b38ff370SJamie Gritton error = EEXIST; 1114b38ff370SJamie Gritton if (pr != NULL) 1115b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1116b38ff370SJamie Gritton vfs_opterror(opts, 1117b38ff370SJamie Gritton "jail \"%s\" already exists", 1118b38ff370SJamie Gritton name); 1119b38ff370SJamie Gritton goto done_unlock_list; 1120b38ff370SJamie Gritton } 1121b38ff370SJamie Gritton } 1122b38ff370SJamie Gritton } 1123b38ff370SJamie Gritton /* If no active jail is found, use a dying one. */ 1124b38ff370SJamie Gritton if (deadpr != NULL && pr == NULL) { 1125b38ff370SJamie Gritton if (flags & JAIL_DYING) { 1126b38ff370SJamie Gritton mtx_lock(&deadpr->pr_mtx); 1127b38ff370SJamie Gritton if (deadpr->pr_ref == 0) { 1128b38ff370SJamie Gritton mtx_unlock(&deadpr->pr_mtx); 1129b38ff370SJamie Gritton goto name_again; 1130b38ff370SJamie Gritton } 1131b38ff370SJamie Gritton pr = deadpr; 1132b38ff370SJamie Gritton } else if (cuflags == JAIL_UPDATE) { 1133b38ff370SJamie Gritton error = ENOENT; 1134b38ff370SJamie Gritton vfs_opterror(opts, 1135b38ff370SJamie Gritton "jail \"%s\" is dying", name); 1136b38ff370SJamie Gritton goto done_unlock_list; 1137b38ff370SJamie Gritton } 1138b38ff370SJamie Gritton } 1139b38ff370SJamie Gritton /* Update: name must exist if no jid. */ 1140b38ff370SJamie Gritton else if (cuflags == JAIL_UPDATE && pr == NULL) { 1141b38ff370SJamie Gritton error = ENOENT; 1142b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", 1143b38ff370SJamie Gritton name); 1144b38ff370SJamie Gritton goto done_unlock_list; 1145b38ff370SJamie Gritton } 1146b38ff370SJamie Gritton } 1147b38ff370SJamie Gritton } 1148b38ff370SJamie Gritton /* Update: must provide a jid or name. */ 1149b38ff370SJamie Gritton else if (cuflags == JAIL_UPDATE && pr == NULL) { 1150b38ff370SJamie Gritton error = ENOENT; 1151b38ff370SJamie Gritton vfs_opterror(opts, "update specified no jail"); 1152b38ff370SJamie Gritton goto done_unlock_list; 1153b38ff370SJamie Gritton } 1154b38ff370SJamie Gritton 1155b38ff370SJamie Gritton /* If there's no prison to update, create a new one and link it in. */ 1156b38ff370SJamie Gritton if (pr == NULL) { 1157b97457e2SJamie Gritton for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent) 1158b97457e2SJamie Gritton if (tpr->pr_childcount >= tpr->pr_childmax) { 1159b97457e2SJamie Gritton error = EPERM; 1160b97457e2SJamie Gritton vfs_opterror(opts, "prison limit exceeded"); 1161b97457e2SJamie Gritton goto done_unlock_list; 1162b97457e2SJamie Gritton } 1163b38ff370SJamie Gritton created = 1; 11640304c731SJamie Gritton mtx_lock(&ppr->pr_mtx); 11652a549507SJamie Gritton if (ppr->pr_ref == 0) { 11660304c731SJamie Gritton mtx_unlock(&ppr->pr_mtx); 11670304c731SJamie Gritton error = ENOENT; 1168b6f47c23SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", 1169b6f47c23SJamie Gritton prison_name(mypr, ppr)); 11700304c731SJamie Gritton goto done_unlock_list; 11710304c731SJamie Gritton } 11720304c731SJamie Gritton ppr->pr_ref++; 11730304c731SJamie Gritton ppr->pr_uref++; 11740304c731SJamie Gritton mtx_unlock(&ppr->pr_mtx); 1175b38ff370SJamie Gritton pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO); 1176b38ff370SJamie Gritton if (jid == 0) { 1177b38ff370SJamie Gritton /* Find the next free jid. */ 1178b38ff370SJamie Gritton jid = lastprid + 1; 1179b38ff370SJamie Gritton findnext: 1180b38ff370SJamie Gritton if (jid == JAIL_MAX) 1181b38ff370SJamie Gritton jid = 1; 1182b38ff370SJamie Gritton TAILQ_FOREACH(tpr, &allprison, pr_list) { 1183b38ff370SJamie Gritton if (tpr->pr_id < jid) 1184b38ff370SJamie Gritton continue; 1185b38ff370SJamie Gritton if (tpr->pr_id > jid || tpr->pr_ref == 0) { 1186b38ff370SJamie Gritton TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1187b38ff370SJamie Gritton break; 1188b38ff370SJamie Gritton } 1189b38ff370SJamie Gritton if (jid == lastprid) { 1190b38ff370SJamie Gritton error = EAGAIN; 1191b38ff370SJamie Gritton vfs_opterror(opts, 1192b38ff370SJamie Gritton "no available jail IDs"); 1193b38ff370SJamie Gritton free(pr, M_PRISON); 11940304c731SJamie Gritton prison_deref(ppr, PD_DEREF | 11950304c731SJamie Gritton PD_DEUREF | PD_LIST_XLOCKED); 11960304c731SJamie Gritton goto done_releroot; 1197b38ff370SJamie Gritton } 1198b38ff370SJamie Gritton jid++; 1199b38ff370SJamie Gritton goto findnext; 1200b38ff370SJamie Gritton } 1201b38ff370SJamie Gritton lastprid = jid; 1202b38ff370SJamie Gritton } else { 1203b38ff370SJamie Gritton /* 1204b38ff370SJamie Gritton * The jail already has a jid (that did not yet exist), 1205b38ff370SJamie Gritton * so just find where to insert it. 1206b38ff370SJamie Gritton */ 1207b38ff370SJamie Gritton TAILQ_FOREACH(tpr, &allprison, pr_list) 1208b38ff370SJamie Gritton if (tpr->pr_id >= jid) { 1209b38ff370SJamie Gritton TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1210b38ff370SJamie Gritton break; 1211b38ff370SJamie Gritton } 1212b38ff370SJamie Gritton } 1213b38ff370SJamie Gritton if (tpr == NULL) 1214b38ff370SJamie Gritton TAILQ_INSERT_TAIL(&allprison, pr, pr_list); 12150304c731SJamie Gritton LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling); 12160304c731SJamie Gritton for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 1217b97457e2SJamie Gritton tpr->pr_childcount++; 1218b38ff370SJamie Gritton 12190304c731SJamie Gritton pr->pr_parent = ppr; 1220b38ff370SJamie Gritton pr->pr_id = jid; 12210304c731SJamie Gritton 12220304c731SJamie Gritton /* Set some default values, and inherit some from the parent. */ 1223b6f47c23SJamie Gritton if (namelc == NULL) 1224b6f47c23SJamie Gritton namelc = ""; 1225b38ff370SJamie Gritton if (path == NULL) { 1226b38ff370SJamie Gritton path = "/"; 12270304c731SJamie Gritton root = mypr->pr_root; 1228b38ff370SJamie Gritton vref(root); 1229b38ff370SJamie Gritton } 12308986e3a0SJamie Gritton strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN); 12318986e3a0SJamie Gritton pr->pr_flags |= PR_HOST; 1232bdfc8cc4SJamie Gritton #if defined(INET) || defined(INET6) 1233bdfc8cc4SJamie Gritton #ifdef VIMAGE 1234bdfc8cc4SJamie Gritton if (!(pr_flags & PR_VNET)) 1235bdfc8cc4SJamie Gritton #endif 1236bdfc8cc4SJamie Gritton { 12370304c731SJamie Gritton #ifdef INET 12382b0d6f81SJamie Gritton if (!(ch_flags & PR_IP4_USER)) 12396a3f2779SJamie Gritton pr->pr_flags |= PR_IP4 | PR_IP4_USER; 12402b0d6f81SJamie Gritton else if (!(pr_flags & PR_IP4_USER)) { 12412b0d6f81SJamie Gritton pr->pr_flags |= ppr->pr_flags & PR_IP4; 12422b0d6f81SJamie Gritton if (ppr->pr_ip4 != NULL) { 12432b0d6f81SJamie Gritton pr->pr_ip4s = ppr->pr_ip4s; 12442b0d6f81SJamie Gritton pr->pr_ip4 = malloc(pr->pr_ip4s * 12452b0d6f81SJamie Gritton sizeof(struct in_addr), M_PRISON, 12462b0d6f81SJamie Gritton M_WAITOK); 12472b0d6f81SJamie Gritton bcopy(ppr->pr_ip4, pr->pr_ip4, 12482b0d6f81SJamie Gritton pr->pr_ip4s * sizeof(*pr->pr_ip4)); 12492b0d6f81SJamie Gritton } 12502b0d6f81SJamie Gritton } 12510304c731SJamie Gritton #endif 12520304c731SJamie Gritton #ifdef INET6 12532b0d6f81SJamie Gritton if (!(ch_flags & PR_IP6_USER)) 12546a3f2779SJamie Gritton pr->pr_flags |= PR_IP6 | PR_IP6_USER; 12552b0d6f81SJamie Gritton else if (!(pr_flags & PR_IP6_USER)) { 12562b0d6f81SJamie Gritton pr->pr_flags |= ppr->pr_flags & PR_IP6; 12572b0d6f81SJamie Gritton if (ppr->pr_ip6 != NULL) { 12582b0d6f81SJamie Gritton pr->pr_ip6s = ppr->pr_ip6s; 12592b0d6f81SJamie Gritton pr->pr_ip6 = malloc(pr->pr_ip6s * 12602b0d6f81SJamie Gritton sizeof(struct in6_addr), M_PRISON, 12612b0d6f81SJamie Gritton M_WAITOK); 12622b0d6f81SJamie Gritton bcopy(ppr->pr_ip6, pr->pr_ip6, 12632b0d6f81SJamie Gritton pr->pr_ip6s * sizeof(*pr->pr_ip6)); 12642b0d6f81SJamie Gritton } 12652b0d6f81SJamie Gritton } 12660304c731SJamie Gritton #endif 1267bdfc8cc4SJamie Gritton } 1268bdfc8cc4SJamie Gritton #endif 1269592bcae8SBjoern A. Zeeb /* Source address selection is always on by default. */ 1270592bcae8SBjoern A. Zeeb pr->pr_flags |= _PR_IP_SADDRSEL; 1271592bcae8SBjoern A. Zeeb 12720304c731SJamie Gritton pr->pr_securelevel = ppr->pr_securelevel; 12730304c731SJamie Gritton pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow; 1274af10bf05SBjoern A. Zeeb pr->pr_enforce_statfs = jail_default_enforce_statfs; 1275bf3db8aaSMartin Matuska pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum; 1276b38ff370SJamie Gritton 1277b96bd95bSIan Lepore pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate; 1278b96bd95bSIan Lepore if (osrelstr == NULL) 12791b786d01SBjoern A. Zeeb strlcpy(pr->pr_osrelease, ppr->pr_osrelease, 12801b786d01SBjoern A. Zeeb sizeof(pr->pr_osrelease)); 1281b96bd95bSIan Lepore else 12821b786d01SBjoern A. Zeeb strlcpy(pr->pr_osrelease, osrelstr, 12831b786d01SBjoern A. Zeeb sizeof(pr->pr_osrelease)); 1284b96bd95bSIan Lepore 12850304c731SJamie Gritton LIST_INIT(&pr->pr_children); 12860304c731SJamie Gritton mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK); 128773d9e52dSJamie Gritton TASK_INIT(&pr->pr_task, 0, prison_complete, pr); 1288b38ff370SJamie Gritton 1289679e1390SJamie Gritton #ifdef VIMAGE 1290679e1390SJamie Gritton /* Allocate a new vnet if specified. */ 1291679e1390SJamie Gritton pr->pr_vnet = (pr_flags & PR_VNET) 1292679e1390SJamie Gritton ? vnet_alloc() : ppr->pr_vnet; 1293679e1390SJamie Gritton #endif 1294b38ff370SJamie Gritton /* 1295b38ff370SJamie Gritton * Allocate a dedicated cpuset for each jail. 1296b38ff370SJamie Gritton * Unlike other initial settings, this may return an erorr. 1297b38ff370SJamie Gritton */ 12980304c731SJamie Gritton error = cpuset_create_root(ppr, &pr->pr_cpuset); 1299b38ff370SJamie Gritton if (error) { 1300b38ff370SJamie Gritton prison_deref(pr, PD_LIST_XLOCKED); 1301b38ff370SJamie Gritton goto done_releroot; 1302b38ff370SJamie Gritton } 1303b38ff370SJamie Gritton 1304b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1305b38ff370SJamie Gritton /* 1306b38ff370SJamie Gritton * New prisons do not yet have a reference, because we do not 1307b6f47c23SJamie Gritton * want others to see the incomplete prison once the 1308b38ff370SJamie Gritton * allprison_lock is downgraded. 1309b38ff370SJamie Gritton */ 1310b38ff370SJamie Gritton } else { 1311b38ff370SJamie Gritton created = 0; 13122b0d6f81SJamie Gritton /* 13132b0d6f81SJamie Gritton * Grab a reference for existing prisons, to ensure they 13142b0d6f81SJamie Gritton * continue to exist for the duration of the call. 13152b0d6f81SJamie Gritton */ 13162b0d6f81SJamie Gritton pr->pr_ref++; 1317bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6)) 1318bdfc8cc4SJamie Gritton if ((pr->pr_flags & PR_VNET) && 1319bdfc8cc4SJamie Gritton (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 1320bdfc8cc4SJamie Gritton error = EINVAL; 1321bdfc8cc4SJamie Gritton vfs_opterror(opts, 1322bdfc8cc4SJamie Gritton "vnet jails cannot have IP address restrictions"); 1323bdfc8cc4SJamie Gritton goto done_deref_locked; 1324bdfc8cc4SJamie Gritton } 1325bdfc8cc4SJamie Gritton #endif 13262b0d6f81SJamie Gritton #ifdef INET 13272b0d6f81SJamie Gritton if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 13282b0d6f81SJamie Gritton error = EINVAL; 13292b0d6f81SJamie Gritton vfs_opterror(opts, 13302b0d6f81SJamie Gritton "ip4 cannot be changed after creation"); 13312b0d6f81SJamie Gritton goto done_deref_locked; 13322b0d6f81SJamie Gritton } 13332b0d6f81SJamie Gritton #endif 13342b0d6f81SJamie Gritton #ifdef INET6 13352b0d6f81SJamie Gritton if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 13362b0d6f81SJamie Gritton error = EINVAL; 13372b0d6f81SJamie Gritton vfs_opterror(opts, 13382b0d6f81SJamie Gritton "ip6 cannot be changed after creation"); 13392b0d6f81SJamie Gritton goto done_deref_locked; 13402b0d6f81SJamie Gritton } 13412b0d6f81SJamie Gritton #endif 1342b38ff370SJamie Gritton } 1343b38ff370SJamie Gritton 1344b38ff370SJamie Gritton /* Do final error checking before setting anything. */ 13450304c731SJamie Gritton if (gotslevel) { 13460304c731SJamie Gritton if (slevel < ppr->pr_securelevel) { 13470304c731SJamie Gritton error = EPERM; 13480304c731SJamie Gritton goto done_deref_locked; 13490304c731SJamie Gritton } 13500304c731SJamie Gritton } 1351b97457e2SJamie Gritton if (gotchildmax) { 1352b97457e2SJamie Gritton if (childmax >= ppr->pr_childmax) { 1353b97457e2SJamie Gritton error = EPERM; 1354b97457e2SJamie Gritton goto done_deref_locked; 1355b97457e2SJamie Gritton } 1356b97457e2SJamie Gritton } 13570304c731SJamie Gritton if (gotenforce) { 13580304c731SJamie Gritton if (enforce < ppr->pr_enforce_statfs) { 13590304c731SJamie Gritton error = EPERM; 13600304c731SJamie Gritton goto done_deref_locked; 13610304c731SJamie Gritton } 13620304c731SJamie Gritton } 13630cc207a6SMartin Matuska if (gotrsnum) { 13640cc207a6SMartin Matuska /* 13650cc207a6SMartin Matuska * devfs_rsnum is a uint16_t 13660cc207a6SMartin Matuska */ 1367bf3db8aaSMartin Matuska if (rsnum < 0 || rsnum > 65535) { 13680cc207a6SMartin Matuska error = EINVAL; 13690cc207a6SMartin Matuska goto done_deref_locked; 13700cc207a6SMartin Matuska } 13710cc207a6SMartin Matuska /* 1372bf3db8aaSMartin Matuska * Nested jails always inherit parent's devfs ruleset 13730cc207a6SMartin Matuska */ 13740cc207a6SMartin Matuska if (jailed(td->td_ucred)) { 13750cc207a6SMartin Matuska if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) { 13760cc207a6SMartin Matuska error = EPERM; 13770cc207a6SMartin Matuska goto done_deref_locked; 1378bf3db8aaSMartin Matuska } else 13790cc207a6SMartin Matuska rsnum = ppr->pr_devfs_rsnum; 13800cc207a6SMartin Matuska } 13810cc207a6SMartin Matuska } 1382b38ff370SJamie Gritton #ifdef INET 13832b0d6f81SJamie Gritton if (ip4s > 0) { 13840304c731SJamie Gritton if (ppr->pr_flags & PR_IP4) { 13850304c731SJamie Gritton /* 13860304c731SJamie Gritton * Make sure the new set of IP addresses is a 13870304c731SJamie Gritton * subset of the parent's list. Don't worry 13880304c731SJamie Gritton * about the parent being unlocked, as any 13890304c731SJamie Gritton * setting is done with allprison_lock held. 13900304c731SJamie Gritton */ 13910304c731SJamie Gritton for (ij = 0; ij < ppr->pr_ip4s; ij++) 13922b0d6f81SJamie Gritton if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 13930304c731SJamie Gritton break; 13940304c731SJamie Gritton if (ij == ppr->pr_ip4s) { 13950304c731SJamie Gritton error = EPERM; 13960304c731SJamie Gritton goto done_deref_locked; 13970304c731SJamie Gritton } 13980304c731SJamie Gritton if (ip4s > 1) { 13990304c731SJamie Gritton for (ii = ij = 1; ii < ip4s; ii++) { 14000304c731SJamie Gritton if (ip4[ii].s_addr == 14010304c731SJamie Gritton ppr->pr_ip4[0].s_addr) 1402b38ff370SJamie Gritton continue; 14030304c731SJamie Gritton for (; ij < ppr->pr_ip4s; ij++) 14040304c731SJamie Gritton if (ip4[ii].s_addr == 14050304c731SJamie Gritton ppr->pr_ip4[ij].s_addr) 14060304c731SJamie Gritton break; 14070304c731SJamie Gritton if (ij == ppr->pr_ip4s) 14080304c731SJamie Gritton break; 14090304c731SJamie Gritton } 14100304c731SJamie Gritton if (ij == ppr->pr_ip4s) { 14110304c731SJamie Gritton error = EPERM; 14120304c731SJamie Gritton goto done_deref_locked; 14130304c731SJamie Gritton } 14140304c731SJamie Gritton } 14150304c731SJamie Gritton } 14160304c731SJamie Gritton /* 14170304c731SJamie Gritton * Check for conflicting IP addresses. We permit them 14180304c731SJamie Gritton * if there is no more than one IP on each jail. If 14190304c731SJamie Gritton * there is a duplicate on a jail with more than one 14200304c731SJamie Gritton * IP stop checking and return error. 14210304c731SJamie Gritton */ 1422bdfc8cc4SJamie Gritton #ifdef VIMAGE 142308b43333SJamie Gritton for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent) 1424bdfc8cc4SJamie Gritton if (tppr->pr_flags & PR_VNET) 1425bdfc8cc4SJamie Gritton break; 142608b43333SJamie Gritton #else 142708b43333SJamie Gritton tppr = &prison0; 1428bdfc8cc4SJamie Gritton #endif 1429bdfc8cc4SJamie Gritton FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1430bdfc8cc4SJamie Gritton if (tpr == pr || 1431bdfc8cc4SJamie Gritton #ifdef VIMAGE 14322b0d6f81SJamie Gritton (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1433bdfc8cc4SJamie Gritton #endif 1434bdfc8cc4SJamie Gritton tpr->pr_uref == 0) { 14350304c731SJamie Gritton descend = 0; 14360304c731SJamie Gritton continue; 14370304c731SJamie Gritton } 14380304c731SJamie Gritton if (!(tpr->pr_flags & PR_IP4_USER)) 14390304c731SJamie Gritton continue; 14400304c731SJamie Gritton descend = 0; 14410304c731SJamie Gritton if (tpr->pr_ip4 == NULL || 14420304c731SJamie Gritton (ip4s == 1 && tpr->pr_ip4s == 1)) 14430304c731SJamie Gritton continue; 14440304c731SJamie Gritton for (ii = 0; ii < ip4s; ii++) { 14450ce1624dSStephen J. Kiernan if (prison_check_ip4_locked(tpr, &ip4[ii]) == 14460ce1624dSStephen J. Kiernan 0) { 14470304c731SJamie Gritton error = EADDRINUSE; 1448b38ff370SJamie Gritton vfs_opterror(opts, 1449b38ff370SJamie Gritton "IPv4 addresses clash"); 1450b38ff370SJamie Gritton goto done_deref_locked; 1451b38ff370SJamie Gritton } 14520304c731SJamie Gritton } 14530304c731SJamie Gritton } 14540304c731SJamie Gritton } 1455b38ff370SJamie Gritton #endif 1456b38ff370SJamie Gritton #ifdef INET6 14572b0d6f81SJamie Gritton if (ip6s > 0) { 14580304c731SJamie Gritton if (ppr->pr_flags & PR_IP6) { 14590304c731SJamie Gritton /* 14600304c731SJamie Gritton * Make sure the new set of IP addresses is a 14610304c731SJamie Gritton * subset of the parent's list. 14620304c731SJamie Gritton */ 14630304c731SJamie Gritton for (ij = 0; ij < ppr->pr_ip6s; ij++) 14640304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL(&ip6[0], 14650304c731SJamie Gritton &ppr->pr_ip6[ij])) 14660304c731SJamie Gritton break; 14670304c731SJamie Gritton if (ij == ppr->pr_ip6s) { 14680304c731SJamie Gritton error = EPERM; 14690304c731SJamie Gritton goto done_deref_locked; 14700304c731SJamie Gritton } 14710304c731SJamie Gritton if (ip6s > 1) { 14720304c731SJamie Gritton for (ii = ij = 1; ii < ip6s; ii++) { 14730304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL(&ip6[ii], 14740304c731SJamie Gritton &ppr->pr_ip6[0])) 14750304c731SJamie Gritton continue; 14760304c731SJamie Gritton for (; ij < ppr->pr_ip6s; ij++) 14770304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL( 14782b0d6f81SJamie Gritton &ip6[ii], &ppr->pr_ip6[ij])) 14790304c731SJamie Gritton break; 14800304c731SJamie Gritton if (ij == ppr->pr_ip6s) 14810304c731SJamie Gritton break; 14820304c731SJamie Gritton } 14830304c731SJamie Gritton if (ij == ppr->pr_ip6s) { 14840304c731SJamie Gritton error = EPERM; 14850304c731SJamie Gritton goto done_deref_locked; 14860304c731SJamie Gritton } 14870304c731SJamie Gritton } 14880304c731SJamie Gritton } 14890304c731SJamie Gritton /* Check for conflicting IP addresses. */ 1490bdfc8cc4SJamie Gritton #ifdef VIMAGE 149108b43333SJamie Gritton for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent) 1492bdfc8cc4SJamie Gritton if (tppr->pr_flags & PR_VNET) 1493bdfc8cc4SJamie Gritton break; 149408b43333SJamie Gritton #else 149508b43333SJamie Gritton tppr = &prison0; 1496bdfc8cc4SJamie Gritton #endif 1497bdfc8cc4SJamie Gritton FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1498bdfc8cc4SJamie Gritton if (tpr == pr || 1499bdfc8cc4SJamie Gritton #ifdef VIMAGE 15002b0d6f81SJamie Gritton (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1501bdfc8cc4SJamie Gritton #endif 1502bdfc8cc4SJamie Gritton tpr->pr_uref == 0) { 15030304c731SJamie Gritton descend = 0; 15040304c731SJamie Gritton continue; 15050304c731SJamie Gritton } 15060304c731SJamie Gritton if (!(tpr->pr_flags & PR_IP6_USER)) 15070304c731SJamie Gritton continue; 15080304c731SJamie Gritton descend = 0; 15090304c731SJamie Gritton if (tpr->pr_ip6 == NULL || 15100304c731SJamie Gritton (ip6s == 1 && tpr->pr_ip6s == 1)) 15110304c731SJamie Gritton continue; 15120304c731SJamie Gritton for (ii = 0; ii < ip6s; ii++) { 15130ce1624dSStephen J. Kiernan if (prison_check_ip6_locked(tpr, &ip6[ii]) == 15140ce1624dSStephen J. Kiernan 0) { 15150304c731SJamie Gritton error = EADDRINUSE; 1516b38ff370SJamie Gritton vfs_opterror(opts, 1517b38ff370SJamie Gritton "IPv6 addresses clash"); 1518b38ff370SJamie Gritton goto done_deref_locked; 1519b38ff370SJamie Gritton } 15200304c731SJamie Gritton } 15210304c731SJamie Gritton } 15220304c731SJamie Gritton } 1523b38ff370SJamie Gritton #endif 15240304c731SJamie Gritton onamelen = namelen = 0; 1525b6f47c23SJamie Gritton if (namelc != NULL) { 15266ab6058eSJamie Gritton /* Give a default name of the jid. Also allow the name to be 15276ab6058eSJamie Gritton * explicitly the jid - but not any other number, and only in 15286ab6058eSJamie Gritton * normal form (no leading zero/etc). 15296ab6058eSJamie Gritton */ 1530b6f47c23SJamie Gritton if (namelc[0] == '\0') 1531b6f47c23SJamie Gritton snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid); 15326ab6058eSJamie Gritton else if ((strtoul(namelc, &p, 10) != jid || 15336ab6058eSJamie Gritton namelc[0] < '1' || namelc[0] > '9') && *p == '\0') { 1534b38ff370SJamie Gritton error = EINVAL; 1535babbbb9cSJamie Gritton vfs_opterror(opts, 1536babbbb9cSJamie Gritton "name cannot be numeric (unless it is the jid)"); 15370304c731SJamie Gritton goto done_deref_locked; 1538b38ff370SJamie Gritton } 1539b38ff370SJamie Gritton /* 15400304c731SJamie Gritton * Make sure the name isn't too long for the prison or its 15410304c731SJamie Gritton * children. 1542b38ff370SJamie Gritton */ 1543b6f47c23SJamie Gritton pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 1544b6f47c23SJamie Gritton onamelen = strlen(pr->pr_name + pnamelen); 1545b6f47c23SJamie Gritton namelen = strlen(namelc); 1546b6f47c23SJamie Gritton if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) { 15470304c731SJamie Gritton error = ENAMETOOLONG; 15480304c731SJamie Gritton goto done_deref_locked; 15490304c731SJamie Gritton } 15500304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 15510304c731SJamie Gritton if (strlen(tpr->pr_name) + (namelen - onamelen) >= 15520304c731SJamie Gritton sizeof(pr->pr_name)) { 15530304c731SJamie Gritton error = ENAMETOOLONG; 15540304c731SJamie Gritton goto done_deref_locked; 15550304c731SJamie Gritton } 15560304c731SJamie Gritton } 15570304c731SJamie Gritton } 1558b3079544SJamie Gritton pr_allow_diff = pr_allow & ~ppr->pr_allow; 1559b3079544SJamie Gritton if (pr_allow_diff & ~PR_ALLOW_DIFFERENCES) { 15600304c731SJamie Gritton error = EPERM; 15610304c731SJamie Gritton goto done_deref_locked; 1562b38ff370SJamie Gritton } 1563b38ff370SJamie Gritton 1564b6f47c23SJamie Gritton /* 1565b6f47c23SJamie Gritton * Let modules check their parameters. This requires unlocking and 1566b6f47c23SJamie Gritton * then re-locking the prison, but this is still a valid state as long 1567b6f47c23SJamie Gritton * as allprison_lock remains xlocked. 1568b6f47c23SJamie Gritton */ 1569b6f47c23SJamie Gritton mtx_unlock(&pr->pr_mtx); 1570b6f47c23SJamie Gritton error = osd_jail_call(pr, PR_METHOD_CHECK, opts); 1571b6f47c23SJamie Gritton if (error != 0) { 1572b6f47c23SJamie Gritton prison_deref(pr, created 1573b6f47c23SJamie Gritton ? PD_LIST_XLOCKED 1574b6f47c23SJamie Gritton : PD_DEREF | PD_LIST_XLOCKED); 1575b6f47c23SJamie Gritton goto done_releroot; 1576b6f47c23SJamie Gritton } 1577b6f47c23SJamie Gritton mtx_lock(&pr->pr_mtx); 1578b6f47c23SJamie Gritton 1579b6f47c23SJamie Gritton /* At this point, all valid parameters should have been noted. */ 1580b6f47c23SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 1581b6f47c23SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 1582b6f47c23SJamie Gritton error = EINVAL; 1583b6f47c23SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 1584b6f47c23SJamie Gritton goto done_deref_locked; 1585b6f47c23SJamie Gritton } 1586b6f47c23SJamie Gritton } 1587b6f47c23SJamie Gritton 1588b38ff370SJamie Gritton /* Set the parameters of the prison. */ 1589b38ff370SJamie Gritton #ifdef INET 15900304c731SJamie Gritton redo_ip4 = 0; 15910304c731SJamie Gritton if (pr_flags & PR_IP4_USER) { 15920304c731SJamie Gritton pr->pr_flags |= PR_IP4; 1593b38ff370SJamie Gritton free(pr->pr_ip4, M_PRISON); 15940304c731SJamie Gritton pr->pr_ip4s = ip4s; 1595b38ff370SJamie Gritton pr->pr_ip4 = ip4; 1596b38ff370SJamie Gritton ip4 = NULL; 15970304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1598bdfc8cc4SJamie Gritton #ifdef VIMAGE 1599bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1600bdfc8cc4SJamie Gritton descend = 0; 1601bdfc8cc4SJamie Gritton continue; 1602bdfc8cc4SJamie Gritton } 1603bdfc8cc4SJamie Gritton #endif 16040304c731SJamie Gritton if (prison_restrict_ip4(tpr, NULL)) { 16050304c731SJamie Gritton redo_ip4 = 1; 16060304c731SJamie Gritton descend = 0; 16070304c731SJamie Gritton } 16080304c731SJamie Gritton } 16090304c731SJamie Gritton } 1610b38ff370SJamie Gritton #endif 1611b38ff370SJamie Gritton #ifdef INET6 16120304c731SJamie Gritton redo_ip6 = 0; 16130304c731SJamie Gritton if (pr_flags & PR_IP6_USER) { 16140304c731SJamie Gritton pr->pr_flags |= PR_IP6; 1615b38ff370SJamie Gritton free(pr->pr_ip6, M_PRISON); 16160304c731SJamie Gritton pr->pr_ip6s = ip6s; 1617b38ff370SJamie Gritton pr->pr_ip6 = ip6; 1618b38ff370SJamie Gritton ip6 = NULL; 16190304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1620bdfc8cc4SJamie Gritton #ifdef VIMAGE 1621bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1622bdfc8cc4SJamie Gritton descend = 0; 1623bdfc8cc4SJamie Gritton continue; 1624bdfc8cc4SJamie Gritton } 1625bdfc8cc4SJamie Gritton #endif 16260304c731SJamie Gritton if (prison_restrict_ip6(tpr, NULL)) { 16270304c731SJamie Gritton redo_ip6 = 1; 16280304c731SJamie Gritton descend = 0; 16290304c731SJamie Gritton } 16300304c731SJamie Gritton } 16310304c731SJamie Gritton } 1632b38ff370SJamie Gritton #endif 16330304c731SJamie Gritton if (gotslevel) { 1634b38ff370SJamie Gritton pr->pr_securelevel = slevel; 16350304c731SJamie Gritton /* Set all child jails to be at least this level. */ 16360304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 16370304c731SJamie Gritton if (tpr->pr_securelevel < slevel) 16380304c731SJamie Gritton tpr->pr_securelevel = slevel; 16390304c731SJamie Gritton } 1640b97457e2SJamie Gritton if (gotchildmax) { 1641b97457e2SJamie Gritton pr->pr_childmax = childmax; 1642b97457e2SJamie Gritton /* Set all child jails to under this limit. */ 1643b97457e2SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level) 1644b97457e2SJamie Gritton if (tpr->pr_childmax > childmax - level) 1645b97457e2SJamie Gritton tpr->pr_childmax = childmax > level 1646b97457e2SJamie Gritton ? childmax - level : 0; 1647b97457e2SJamie Gritton } 16480304c731SJamie Gritton if (gotenforce) { 16490304c731SJamie Gritton pr->pr_enforce_statfs = enforce; 16500304c731SJamie Gritton /* Pass this restriction on to the children. */ 16510304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 16520304c731SJamie Gritton if (tpr->pr_enforce_statfs < enforce) 16530304c731SJamie Gritton tpr->pr_enforce_statfs = enforce; 16540304c731SJamie Gritton } 16550cc207a6SMartin Matuska if (gotrsnum) { 16560cc207a6SMartin Matuska pr->pr_devfs_rsnum = rsnum; 16570cc207a6SMartin Matuska /* Pass this restriction on to the children. */ 16580cc207a6SMartin Matuska FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 16590cc207a6SMartin Matuska tpr->pr_devfs_rsnum = rsnum; 16600cc207a6SMartin Matuska } 1661b6f47c23SJamie Gritton if (namelc != NULL) { 16620304c731SJamie Gritton if (ppr == &prison0) 1663b6f47c23SJamie Gritton strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name)); 16640304c731SJamie Gritton else 16650304c731SJamie Gritton snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s", 1666b6f47c23SJamie Gritton ppr->pr_name, namelc); 16670304c731SJamie Gritton /* Change this component of child names. */ 16680304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 16690304c731SJamie Gritton bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen, 16700304c731SJamie Gritton strlen(tpr->pr_name + onamelen) + 1); 16710304c731SJamie Gritton bcopy(pr->pr_name, tpr->pr_name, namelen); 16720304c731SJamie Gritton } 16730304c731SJamie Gritton } 1674b38ff370SJamie Gritton if (path != NULL) { 16750304c731SJamie Gritton /* Try to keep a real-rooted full pathname. */ 1676b38ff370SJamie Gritton strlcpy(pr->pr_path, path, sizeof(pr->pr_path)); 1677b38ff370SJamie Gritton pr->pr_root = root; 1678b38ff370SJamie Gritton } 167976ca6f88SJamie Gritton if (PR_HOST & ch_flags & ~pr_flags) { 168076ca6f88SJamie Gritton if (pr->pr_flags & PR_HOST) { 168176ca6f88SJamie Gritton /* 168276ca6f88SJamie Gritton * Copy the parent's host info. As with pr_ip4 above, 168376ca6f88SJamie Gritton * the lack of a lock on the parent is not a problem; 168476ca6f88SJamie Gritton * it is always set with allprison_lock at least 168576ca6f88SJamie Gritton * shared, and is held exclusively here. 168676ca6f88SJamie Gritton */ 1687c1f19219SJamie Gritton strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname, 1688c1f19219SJamie Gritton sizeof(pr->pr_hostname)); 1689c1f19219SJamie Gritton strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname, 1690c1f19219SJamie Gritton sizeof(pr->pr_domainname)); 1691c1f19219SJamie Gritton strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid, 1692c1f19219SJamie Gritton sizeof(pr->pr_hostuuid)); 169376ca6f88SJamie Gritton pr->pr_hostid = pr->pr_parent->pr_hostid; 169476ca6f88SJamie Gritton } 169576ca6f88SJamie Gritton } else if (host != NULL || domain != NULL || uuid != NULL || gothid) { 169676ca6f88SJamie Gritton /* Set this prison, and any descendants without PR_HOST. */ 1697b38ff370SJamie Gritton if (host != NULL) 1698c1f19219SJamie Gritton strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname)); 169976ca6f88SJamie Gritton if (domain != NULL) 1700c1f19219SJamie Gritton strlcpy(pr->pr_domainname, domain, 1701c1f19219SJamie Gritton sizeof(pr->pr_domainname)); 170276ca6f88SJamie Gritton if (uuid != NULL) 1703c1f19219SJamie Gritton strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid)); 170476ca6f88SJamie Gritton if (gothid) 170576ca6f88SJamie Gritton pr->pr_hostid = hid; 170676ca6f88SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 170776ca6f88SJamie Gritton if (tpr->pr_flags & PR_HOST) 170876ca6f88SJamie Gritton descend = 0; 170976ca6f88SJamie Gritton else { 171076ca6f88SJamie Gritton if (host != NULL) 1711c1f19219SJamie Gritton strlcpy(tpr->pr_hostname, 1712c1f19219SJamie Gritton pr->pr_hostname, 1713c1f19219SJamie Gritton sizeof(tpr->pr_hostname)); 171476ca6f88SJamie Gritton if (domain != NULL) 1715c1f19219SJamie Gritton strlcpy(tpr->pr_domainname, 1716c1f19219SJamie Gritton pr->pr_domainname, 1717c1f19219SJamie Gritton sizeof(tpr->pr_domainname)); 171876ca6f88SJamie Gritton if (uuid != NULL) 1719c1f19219SJamie Gritton strlcpy(tpr->pr_hostuuid, 1720c1f19219SJamie Gritton pr->pr_hostuuid, 1721c1f19219SJamie Gritton sizeof(tpr->pr_hostuuid)); 172276ca6f88SJamie Gritton if (gothid) 172376ca6f88SJamie Gritton tpr->pr_hostid = hid; 172476ca6f88SJamie Gritton } 172576ca6f88SJamie Gritton } 172676ca6f88SJamie Gritton } 17270304c731SJamie Gritton if ((tallow = ch_allow & ~pr_allow)) { 17280304c731SJamie Gritton /* Clear allow bits in all children. */ 17290304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 17300304c731SJamie Gritton tpr->pr_allow &= ~tallow; 17310304c731SJamie Gritton } 17320304c731SJamie Gritton pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow; 1733b38ff370SJamie Gritton /* 1734b38ff370SJamie Gritton * Persistent prisons get an extra reference, and prisons losing their 1735b38ff370SJamie Gritton * persist flag lose that reference. Only do this for existing prisons 1736b38ff370SJamie Gritton * for now, so new ones will remain unseen until after the module 1737b38ff370SJamie Gritton * handlers have completed. 1738b38ff370SJamie Gritton */ 1739cc5fd8c7SJamie Gritton born = pr->pr_uref == 0; 1740b38ff370SJamie Gritton if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) { 1741b38ff370SJamie Gritton if (pr_flags & PR_PERSIST) { 1742b38ff370SJamie Gritton pr->pr_ref++; 1743b38ff370SJamie Gritton pr->pr_uref++; 1744b38ff370SJamie Gritton } else { 1745b38ff370SJamie Gritton pr->pr_ref--; 1746b38ff370SJamie Gritton pr->pr_uref--; 1747b38ff370SJamie Gritton } 1748b38ff370SJamie Gritton } 1749b38ff370SJamie Gritton pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags; 1750b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1751b38ff370SJamie Gritton 1752a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 17534b5c9cf6SEdward Tomasz Napierala if (racct_enable && created) 1754a7ad07bfSEdward Tomasz Napierala prison_racct_attach(pr); 1755a7ad07bfSEdward Tomasz Napierala #endif 1756a7ad07bfSEdward Tomasz Napierala 17570304c731SJamie Gritton /* Locks may have prevented a complete restriction of child IP 17580304c731SJamie Gritton * addresses. If so, allocate some more memory and try again. 17590304c731SJamie Gritton */ 17600304c731SJamie Gritton #ifdef INET 17610304c731SJamie Gritton while (redo_ip4) { 17620304c731SJamie Gritton ip4s = pr->pr_ip4s; 17630304c731SJamie Gritton ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 17640304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 17650304c731SJamie Gritton redo_ip4 = 0; 17660304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1767bdfc8cc4SJamie Gritton #ifdef VIMAGE 1768bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1769bdfc8cc4SJamie Gritton descend = 0; 1770bdfc8cc4SJamie Gritton continue; 1771bdfc8cc4SJamie Gritton } 1772bdfc8cc4SJamie Gritton #endif 17730304c731SJamie Gritton if (prison_restrict_ip4(tpr, ip4)) { 17740304c731SJamie Gritton if (ip4 != NULL) 17750304c731SJamie Gritton ip4 = NULL; 17760304c731SJamie Gritton else 17770304c731SJamie Gritton redo_ip4 = 1; 17780304c731SJamie Gritton } 17790304c731SJamie Gritton } 17800304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 17810304c731SJamie Gritton } 17820304c731SJamie Gritton #endif 17830304c731SJamie Gritton #ifdef INET6 17840304c731SJamie Gritton while (redo_ip6) { 17850304c731SJamie Gritton ip6s = pr->pr_ip6s; 17860304c731SJamie Gritton ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 17870304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 17880304c731SJamie Gritton redo_ip6 = 0; 17890304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1790bdfc8cc4SJamie Gritton #ifdef VIMAGE 1791bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1792bdfc8cc4SJamie Gritton descend = 0; 1793bdfc8cc4SJamie Gritton continue; 1794bdfc8cc4SJamie Gritton } 1795bdfc8cc4SJamie Gritton #endif 17960304c731SJamie Gritton if (prison_restrict_ip6(tpr, ip6)) { 17970304c731SJamie Gritton if (ip6 != NULL) 17980304c731SJamie Gritton ip6 = NULL; 17990304c731SJamie Gritton else 18000304c731SJamie Gritton redo_ip6 = 1; 18010304c731SJamie Gritton } 18020304c731SJamie Gritton } 18030304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 18040304c731SJamie Gritton } 18050304c731SJamie Gritton #endif 18060304c731SJamie Gritton 1807b38ff370SJamie Gritton /* Let the modules do their work. */ 1808b38ff370SJamie Gritton sx_downgrade(&allprison_lock); 1809cc5fd8c7SJamie Gritton if (born) { 1810b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_CREATE, opts); 1811b38ff370SJamie Gritton if (error) { 1812cc5fd8c7SJamie Gritton (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL); 1813cc5fd8c7SJamie Gritton prison_deref(pr, created 1814cc5fd8c7SJamie Gritton ? PD_LIST_SLOCKED 1815cc5fd8c7SJamie Gritton : PD_DEREF | PD_LIST_SLOCKED); 1816b38ff370SJamie Gritton goto done_errmsg; 1817b38ff370SJamie Gritton } 1818b38ff370SJamie Gritton } 1819b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_SET, opts); 1820b38ff370SJamie Gritton if (error) { 1821cc5fd8c7SJamie Gritton if (born) 1822cc5fd8c7SJamie Gritton (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL); 1823b38ff370SJamie Gritton prison_deref(pr, created 1824b38ff370SJamie Gritton ? PD_LIST_SLOCKED 1825b38ff370SJamie Gritton : PD_DEREF | PD_LIST_SLOCKED); 1826b38ff370SJamie Gritton goto done_errmsg; 1827b38ff370SJamie Gritton } 1828b38ff370SJamie Gritton 1829b38ff370SJamie Gritton /* Attach this process to the prison if requested. */ 1830b38ff370SJamie Gritton if (flags & JAIL_ATTACH) { 1831b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1832b38ff370SJamie Gritton error = do_jail_attach(td, pr); 1833b38ff370SJamie Gritton if (error) { 1834b38ff370SJamie Gritton vfs_opterror(opts, "attach failed"); 1835b38ff370SJamie Gritton if (!created) 1836b38ff370SJamie Gritton prison_deref(pr, PD_DEREF); 1837b38ff370SJamie Gritton goto done_errmsg; 1838b38ff370SJamie Gritton } 1839b38ff370SJamie Gritton } 1840b38ff370SJamie Gritton 18411fb24974SEdward Tomasz Napierala #ifdef RACCT 18424b5c9cf6SEdward Tomasz Napierala if (racct_enable && !created) { 1843f514b97bSEdward Tomasz Napierala if (!(flags & JAIL_ATTACH)) 18441fb24974SEdward Tomasz Napierala sx_sunlock(&allprison_lock); 18451fb24974SEdward Tomasz Napierala prison_racct_modify(pr); 1846f514b97bSEdward Tomasz Napierala if (!(flags & JAIL_ATTACH)) 18471fb24974SEdward Tomasz Napierala sx_slock(&allprison_lock); 18481fb24974SEdward Tomasz Napierala } 18491fb24974SEdward Tomasz Napierala #endif 18501fb24974SEdward Tomasz Napierala 18511fb24974SEdward Tomasz Napierala td->td_retval[0] = pr->pr_id; 18521fb24974SEdward Tomasz Napierala 1853b38ff370SJamie Gritton /* 1854b38ff370SJamie Gritton * Now that it is all there, drop the temporary reference from existing 1855b38ff370SJamie Gritton * prisons. Or add a reference to newly created persistent prisons 1856b38ff370SJamie Gritton * (which was not done earlier so that the prison would not be publicly 1857b38ff370SJamie Gritton * visible). 1858b38ff370SJamie Gritton */ 1859b38ff370SJamie Gritton if (!created) { 1860b38ff370SJamie Gritton prison_deref(pr, (flags & JAIL_ATTACH) 1861b38ff370SJamie Gritton ? PD_DEREF 1862b38ff370SJamie Gritton : PD_DEREF | PD_LIST_SLOCKED); 1863b38ff370SJamie Gritton } else { 1864b38ff370SJamie Gritton if (pr_flags & PR_PERSIST) { 1865b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1866b38ff370SJamie Gritton pr->pr_ref++; 1867b38ff370SJamie Gritton pr->pr_uref++; 1868b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1869b38ff370SJamie Gritton } 1870b38ff370SJamie Gritton if (!(flags & JAIL_ATTACH)) 1871b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 1872b38ff370SJamie Gritton } 1873c34bbd2aSEdward Tomasz Napierala 1874cc5fd8c7SJamie Gritton goto done_free; 1875b38ff370SJamie Gritton 18760304c731SJamie Gritton done_deref_locked: 18770304c731SJamie Gritton prison_deref(pr, created 18780304c731SJamie Gritton ? PD_LOCKED | PD_LIST_XLOCKED 18790304c731SJamie Gritton : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 18800304c731SJamie Gritton goto done_releroot; 1881b38ff370SJamie Gritton done_unlock_list: 1882b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 1883b38ff370SJamie Gritton done_releroot: 18845050aa86SKonstantin Belousov if (root != NULL) 1885b38ff370SJamie Gritton vrele(root); 1886b38ff370SJamie Gritton done_errmsg: 1887b38ff370SJamie Gritton if (error) { 1888176ff3a0SJamie Gritton if (vfs_getopt(opts, "errmsg", (void **)&errmsg, 1889176ff3a0SJamie Gritton &errmsg_len) == 0 && errmsg_len > 0) { 1890b38ff370SJamie Gritton errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1; 1891b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 1892b38ff370SJamie Gritton bcopy(errmsg, 1893b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 1894b38ff370SJamie Gritton errmsg_len); 1895b38ff370SJamie Gritton else 1896b38ff370SJamie Gritton copyout(errmsg, 1897b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 1898b38ff370SJamie Gritton errmsg_len); 1899b38ff370SJamie Gritton } 1900b38ff370SJamie Gritton } 1901b38ff370SJamie Gritton done_free: 1902b38ff370SJamie Gritton #ifdef INET 1903b38ff370SJamie Gritton free(ip4, M_PRISON); 1904b38ff370SJamie Gritton #endif 1905b38ff370SJamie Gritton #ifdef INET6 1906b38ff370SJamie Gritton free(ip6, M_PRISON); 1907b38ff370SJamie Gritton #endif 19086dfe0a3dSMartin Matuska if (g_path != NULL) 19096dfe0a3dSMartin Matuska free(g_path, M_TEMP); 1910b38ff370SJamie Gritton vfs_freeopts(opts); 1911b38ff370SJamie Gritton return (error); 1912b38ff370SJamie Gritton } 1913b38ff370SJamie Gritton 1914b38ff370SJamie Gritton /* 1915b38ff370SJamie Gritton * struct jail_get_args { 1916b38ff370SJamie Gritton * struct iovec *iovp; 1917b38ff370SJamie Gritton * unsigned int iovcnt; 1918b38ff370SJamie Gritton * int flags; 1919b38ff370SJamie Gritton * }; 1920b38ff370SJamie Gritton */ 1921b38ff370SJamie Gritton int 19228451d0ddSKip Macy sys_jail_get(struct thread *td, struct jail_get_args *uap) 1923b38ff370SJamie Gritton { 1924b38ff370SJamie Gritton struct uio *auio; 1925b38ff370SJamie Gritton int error; 1926b38ff370SJamie Gritton 1927b38ff370SJamie Gritton /* Check that we have an even number of iovecs. */ 1928b38ff370SJamie Gritton if (uap->iovcnt & 1) 1929b38ff370SJamie Gritton return (EINVAL); 1930b38ff370SJamie Gritton 1931b38ff370SJamie Gritton error = copyinuio(uap->iovp, uap->iovcnt, &auio); 1932b38ff370SJamie Gritton if (error) 1933b38ff370SJamie Gritton return (error); 1934b38ff370SJamie Gritton error = kern_jail_get(td, auio, uap->flags); 1935b38ff370SJamie Gritton if (error == 0) 1936b38ff370SJamie Gritton error = copyout(auio->uio_iov, uap->iovp, 1937b38ff370SJamie Gritton uap->iovcnt * sizeof (struct iovec)); 1938b38ff370SJamie Gritton free(auio, M_IOV); 1939b38ff370SJamie Gritton return (error); 1940b38ff370SJamie Gritton } 1941b38ff370SJamie Gritton 1942b38ff370SJamie Gritton int 1943b38ff370SJamie Gritton kern_jail_get(struct thread *td, struct uio *optuio, int flags) 1944b38ff370SJamie Gritton { 1945672756aaSJamie Gritton struct bool_flags *bf; 1946672756aaSJamie Gritton struct jailsys_flags *jsf; 19470304c731SJamie Gritton struct prison *pr, *mypr; 1948b38ff370SJamie Gritton struct vfsopt *opt; 1949b38ff370SJamie Gritton struct vfsoptlist *opts; 1950b38ff370SJamie Gritton char *errmsg, *name; 1951672756aaSJamie Gritton int error, errmsg_len, errmsg_pos, i, jid, len, locked, pos; 1952672756aaSJamie Gritton unsigned f; 1953b38ff370SJamie Gritton 1954b38ff370SJamie Gritton if (flags & ~JAIL_GET_MASK) 1955b38ff370SJamie Gritton return (EINVAL); 1956b38ff370SJamie Gritton 1957b38ff370SJamie Gritton /* Get the parameter list. */ 1958b38ff370SJamie Gritton error = vfs_buildopts(optuio, &opts); 1959b38ff370SJamie Gritton if (error) 1960b38ff370SJamie Gritton return (error); 1961b38ff370SJamie Gritton errmsg_pos = vfs_getopt_pos(opts, "errmsg"); 19620304c731SJamie Gritton mypr = td->td_ucred->cr_prison; 19631e2a13e6SJamie Gritton 1964b38ff370SJamie Gritton /* 1965b38ff370SJamie Gritton * Find the prison specified by one of: lastjid, jid, name. 1966b38ff370SJamie Gritton */ 1967b38ff370SJamie Gritton sx_slock(&allprison_lock); 1968b38ff370SJamie Gritton error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid)); 1969b38ff370SJamie Gritton if (error == 0) { 1970b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 19710304c731SJamie Gritton if (pr->pr_id > jid && prison_ischild(mypr, pr)) { 1972b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1973b38ff370SJamie Gritton if (pr->pr_ref > 0 && 1974b38ff370SJamie Gritton (pr->pr_uref > 0 || (flags & JAIL_DYING))) 1975b38ff370SJamie Gritton break; 1976b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1977b38ff370SJamie Gritton } 1978b38ff370SJamie Gritton } 1979b38ff370SJamie Gritton if (pr != NULL) 1980b38ff370SJamie Gritton goto found_prison; 1981b38ff370SJamie Gritton error = ENOENT; 1982b38ff370SJamie Gritton vfs_opterror(opts, "no jail after %d", jid); 1983b38ff370SJamie Gritton goto done_unlock_list; 1984b38ff370SJamie Gritton } else if (error != ENOENT) 1985b38ff370SJamie Gritton goto done_unlock_list; 1986b38ff370SJamie Gritton 1987b38ff370SJamie Gritton error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 1988b38ff370SJamie Gritton if (error == 0) { 1989b38ff370SJamie Gritton if (jid != 0) { 19900304c731SJamie Gritton pr = prison_find_child(mypr, jid); 1991b38ff370SJamie Gritton if (pr != NULL) { 1992b38ff370SJamie Gritton if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 1993b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1994b38ff370SJamie Gritton error = ENOENT; 1995b38ff370SJamie Gritton vfs_opterror(opts, "jail %d is dying", 1996b38ff370SJamie Gritton jid); 1997b38ff370SJamie Gritton goto done_unlock_list; 1998b38ff370SJamie Gritton } 1999b38ff370SJamie Gritton goto found_prison; 2000b38ff370SJamie Gritton } 2001b38ff370SJamie Gritton error = ENOENT; 2002b38ff370SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 2003b38ff370SJamie Gritton goto done_unlock_list; 2004b38ff370SJamie Gritton } 2005b38ff370SJamie Gritton } else if (error != ENOENT) 2006b38ff370SJamie Gritton goto done_unlock_list; 2007b38ff370SJamie Gritton 2008b38ff370SJamie Gritton error = vfs_getopt(opts, "name", (void **)&name, &len); 2009b38ff370SJamie Gritton if (error == 0) { 2010b38ff370SJamie Gritton if (len == 0 || name[len - 1] != '\0') { 2011b38ff370SJamie Gritton error = EINVAL; 2012b38ff370SJamie Gritton goto done_unlock_list; 2013b38ff370SJamie Gritton } 20140304c731SJamie Gritton pr = prison_find_name(mypr, name); 2015b38ff370SJamie Gritton if (pr != NULL) { 2016b38ff370SJamie Gritton if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 2017b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2018b38ff370SJamie Gritton error = ENOENT; 2019b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" is dying", 2020b38ff370SJamie Gritton name); 2021b38ff370SJamie Gritton goto done_unlock_list; 2022b38ff370SJamie Gritton } 2023b38ff370SJamie Gritton goto found_prison; 2024b38ff370SJamie Gritton } 2025b38ff370SJamie Gritton error = ENOENT; 2026b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", name); 2027b38ff370SJamie Gritton goto done_unlock_list; 2028b38ff370SJamie Gritton } else if (error != ENOENT) 2029b38ff370SJamie Gritton goto done_unlock_list; 2030b38ff370SJamie Gritton 2031b38ff370SJamie Gritton vfs_opterror(opts, "no jail specified"); 2032b38ff370SJamie Gritton error = ENOENT; 2033b38ff370SJamie Gritton goto done_unlock_list; 2034b38ff370SJamie Gritton 2035b38ff370SJamie Gritton found_prison: 2036b38ff370SJamie Gritton /* Get the parameters of the prison. */ 2037b38ff370SJamie Gritton pr->pr_ref++; 2038b38ff370SJamie Gritton locked = PD_LOCKED; 2039b38ff370SJamie Gritton td->td_retval[0] = pr->pr_id; 2040b38ff370SJamie Gritton error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id)); 2041b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2042b38ff370SJamie Gritton goto done_deref; 20430304c731SJamie Gritton i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id; 20440304c731SJamie Gritton error = vfs_setopt(opts, "parent", &i, sizeof(i)); 2045b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2046b38ff370SJamie Gritton goto done_deref; 20470304c731SJamie Gritton error = vfs_setopts(opts, "name", prison_name(mypr, pr)); 20480304c731SJamie Gritton if (error != 0 && error != ENOENT) 20490304c731SJamie Gritton goto done_deref; 20500304c731SJamie Gritton error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id, 2051b38ff370SJamie Gritton sizeof(pr->pr_cpuset->cs_id)); 2052b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2053b38ff370SJamie Gritton goto done_deref; 20540304c731SJamie Gritton error = vfs_setopts(opts, "path", prison_path(mypr, pr)); 2055b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2056b38ff370SJamie Gritton goto done_deref; 2057b38ff370SJamie Gritton #ifdef INET 2058b38ff370SJamie Gritton error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4, 2059b38ff370SJamie Gritton pr->pr_ip4s * sizeof(*pr->pr_ip4)); 2060b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2061b38ff370SJamie Gritton goto done_deref; 2062b38ff370SJamie Gritton #endif 2063b38ff370SJamie Gritton #ifdef INET6 2064b38ff370SJamie Gritton error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6, 2065b38ff370SJamie Gritton pr->pr_ip6s * sizeof(*pr->pr_ip6)); 2066b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2067b38ff370SJamie Gritton goto done_deref; 2068b38ff370SJamie Gritton #endif 2069b38ff370SJamie Gritton error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel, 2070b38ff370SJamie Gritton sizeof(pr->pr_securelevel)); 2071b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2072b38ff370SJamie Gritton goto done_deref; 2073b97457e2SJamie Gritton error = vfs_setopt(opts, "children.cur", &pr->pr_childcount, 2074b97457e2SJamie Gritton sizeof(pr->pr_childcount)); 2075b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 2076b97457e2SJamie Gritton goto done_deref; 2077b97457e2SJamie Gritton error = vfs_setopt(opts, "children.max", &pr->pr_childmax, 2078b97457e2SJamie Gritton sizeof(pr->pr_childmax)); 2079b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 2080b97457e2SJamie Gritton goto done_deref; 2081c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostname", pr->pr_hostname); 2082b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2083b38ff370SJamie Gritton goto done_deref; 2084c1f19219SJamie Gritton error = vfs_setopts(opts, "host.domainname", pr->pr_domainname); 208576ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 208676ca6f88SJamie Gritton goto done_deref; 2087c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid); 208876ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 208976ca6f88SJamie Gritton goto done_deref; 2090841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32 2091a5c1afadSDmitry Chagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 209276ca6f88SJamie Gritton uint32_t hid32 = pr->pr_hostid; 209376ca6f88SJamie Gritton 209476ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32)); 209576ca6f88SJamie Gritton } else 209676ca6f88SJamie Gritton #endif 209776ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid, 209876ca6f88SJamie Gritton sizeof(pr->pr_hostid)); 209976ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 210076ca6f88SJamie Gritton goto done_deref; 21010304c731SJamie Gritton error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs, 21020304c731SJamie Gritton sizeof(pr->pr_enforce_statfs)); 21030304c731SJamie Gritton if (error != 0 && error != ENOENT) 21040304c731SJamie Gritton goto done_deref; 21050cc207a6SMartin Matuska error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum, 21060cc207a6SMartin Matuska sizeof(pr->pr_devfs_rsnum)); 21070cc207a6SMartin Matuska if (error != 0 && error != ENOENT) 21080cc207a6SMartin Matuska goto done_deref; 2109672756aaSJamie Gritton for (bf = pr_flag_bool; 2110672756aaSJamie Gritton bf < pr_flag_bool + nitems(pr_flag_bool); 2111672756aaSJamie Gritton bf++) { 2112672756aaSJamie Gritton i = (pr->pr_flags & bf->flag) ? 1 : 0; 2113672756aaSJamie Gritton error = vfs_setopt(opts, bf->name, &i, sizeof(i)); 2114b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2115b38ff370SJamie Gritton goto done_deref; 2116b38ff370SJamie Gritton i = !i; 2117672756aaSJamie Gritton error = vfs_setopt(opts, bf->noname, &i, sizeof(i)); 2118b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2119b38ff370SJamie Gritton goto done_deref; 21200304c731SJamie Gritton } 2121672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 2122672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 2123672756aaSJamie Gritton jsf++) { 2124672756aaSJamie Gritton f = pr->pr_flags & (jsf->disable | jsf->new); 2125672756aaSJamie Gritton i = (f != 0 && f == jsf->disable) ? JAIL_SYS_DISABLE 2126672756aaSJamie Gritton : (f == jsf->new) ? JAIL_SYS_NEW 21277cbf7213SJamie Gritton : JAIL_SYS_INHERIT; 2128672756aaSJamie Gritton error = vfs_setopt(opts, jsf->name, &i, sizeof(i)); 21297cbf7213SJamie Gritton if (error != 0 && error != ENOENT) 21307cbf7213SJamie Gritton goto done_deref; 21317cbf7213SJamie Gritton } 2132672756aaSJamie Gritton for (bf = pr_flag_allow; 21330e5c6bd4SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && bf->flag != 0; 2134672756aaSJamie Gritton bf++) { 2135672756aaSJamie Gritton i = (pr->pr_allow & bf->flag) ? 1 : 0; 2136672756aaSJamie Gritton error = vfs_setopt(opts, bf->name, &i, sizeof(i)); 21370304c731SJamie Gritton if (error != 0 && error != ENOENT) 21380304c731SJamie Gritton goto done_deref; 21390304c731SJamie Gritton i = !i; 2140672756aaSJamie Gritton error = vfs_setopt(opts, bf->noname, &i, sizeof(i)); 21410304c731SJamie Gritton if (error != 0 && error != ENOENT) 21420304c731SJamie Gritton goto done_deref; 21430304c731SJamie Gritton } 2144b38ff370SJamie Gritton i = (pr->pr_uref == 0); 2145b38ff370SJamie Gritton error = vfs_setopt(opts, "dying", &i, sizeof(i)); 2146b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2147b38ff370SJamie Gritton goto done_deref; 2148b38ff370SJamie Gritton i = !i; 2149b38ff370SJamie Gritton error = vfs_setopt(opts, "nodying", &i, sizeof(i)); 2150b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2151b38ff370SJamie Gritton goto done_deref; 2152bd96bd15SIan Lepore error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate, 2153bd96bd15SIan Lepore sizeof(pr->pr_osreldate)); 2154a1a4c1b0SIan Lepore if (error != 0 && error != ENOENT) 2155a1a4c1b0SIan Lepore goto done_deref; 2156a1a4c1b0SIan Lepore error = vfs_setopts(opts, "osrelease", pr->pr_osrelease); 2157a1a4c1b0SIan Lepore if (error != 0 && error != ENOENT) 2158a1a4c1b0SIan Lepore goto done_deref; 2159b38ff370SJamie Gritton 2160b38ff370SJamie Gritton /* Get the module parameters. */ 2161b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2162b38ff370SJamie Gritton locked = 0; 2163b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_GET, opts); 2164b38ff370SJamie Gritton if (error) 2165b38ff370SJamie Gritton goto done_deref; 2166b38ff370SJamie Gritton prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED); 2167b38ff370SJamie Gritton 2168b38ff370SJamie Gritton /* By now, all parameters should have been noted. */ 2169b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2170b38ff370SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 2171b38ff370SJamie Gritton error = EINVAL; 2172b38ff370SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 2173b38ff370SJamie Gritton goto done_errmsg; 2174b38ff370SJamie Gritton } 2175b38ff370SJamie Gritton } 2176b38ff370SJamie Gritton 2177b38ff370SJamie Gritton /* Write the fetched parameters back to userspace. */ 2178b38ff370SJamie Gritton error = 0; 2179b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2180b38ff370SJamie Gritton if (opt->pos >= 0 && opt->pos != errmsg_pos) { 2181b38ff370SJamie Gritton pos = 2 * opt->pos + 1; 2182b38ff370SJamie Gritton optuio->uio_iov[pos].iov_len = opt->len; 2183b38ff370SJamie Gritton if (opt->value != NULL) { 2184b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) { 2185b38ff370SJamie Gritton bcopy(opt->value, 2186b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2187b38ff370SJamie Gritton opt->len); 2188b38ff370SJamie Gritton } else { 2189b38ff370SJamie Gritton error = copyout(opt->value, 2190b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2191b38ff370SJamie Gritton opt->len); 2192b38ff370SJamie Gritton if (error) 2193b38ff370SJamie Gritton break; 2194b38ff370SJamie Gritton } 2195b38ff370SJamie Gritton } 2196b38ff370SJamie Gritton } 2197b38ff370SJamie Gritton } 2198b38ff370SJamie Gritton goto done_errmsg; 2199b38ff370SJamie Gritton 2200b38ff370SJamie Gritton done_deref: 2201b38ff370SJamie Gritton prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED); 2202b38ff370SJamie Gritton goto done_errmsg; 2203b38ff370SJamie Gritton 2204b38ff370SJamie Gritton done_unlock_list: 2205b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2206b38ff370SJamie Gritton done_errmsg: 2207b38ff370SJamie Gritton if (error && errmsg_pos >= 0) { 2208b38ff370SJamie Gritton vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 2209b38ff370SJamie Gritton errmsg_pos = 2 * errmsg_pos + 1; 2210b38ff370SJamie Gritton if (errmsg_len > 0) { 2211b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 2212b38ff370SJamie Gritton bcopy(errmsg, 2213b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2214b38ff370SJamie Gritton errmsg_len); 2215b38ff370SJamie Gritton else 2216b38ff370SJamie Gritton copyout(errmsg, 2217b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2218b38ff370SJamie Gritton errmsg_len); 2219b38ff370SJamie Gritton } 2220b38ff370SJamie Gritton } 2221b38ff370SJamie Gritton vfs_freeopts(opts); 2222b38ff370SJamie Gritton return (error); 2223b38ff370SJamie Gritton } 2224b38ff370SJamie Gritton 2225b38ff370SJamie Gritton /* 2226b38ff370SJamie Gritton * struct jail_remove_args { 2227b38ff370SJamie Gritton * int jid; 2228b38ff370SJamie Gritton * }; 2229b38ff370SJamie Gritton */ 2230b38ff370SJamie Gritton int 22318451d0ddSKip Macy sys_jail_remove(struct thread *td, struct jail_remove_args *uap) 2232b38ff370SJamie Gritton { 22330304c731SJamie Gritton struct prison *pr, *cpr, *lpr, *tpr; 22340304c731SJamie Gritton int descend, error; 2235b38ff370SJamie Gritton 2236b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_REMOVE); 2237b38ff370SJamie Gritton if (error) 2238b38ff370SJamie Gritton return (error); 2239b38ff370SJamie Gritton 2240b38ff370SJamie Gritton sx_xlock(&allprison_lock); 22410304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2242b38ff370SJamie Gritton if (pr == NULL) { 2243b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2244b38ff370SJamie Gritton return (EINVAL); 2245b38ff370SJamie Gritton } 2246b38ff370SJamie Gritton 22470304c731SJamie Gritton /* Remove all descendants of this prison, then remove this prison. */ 22480304c731SJamie Gritton pr->pr_ref++; 22490304c731SJamie Gritton if (!LIST_EMPTY(&pr->pr_children)) { 22500304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 22510304c731SJamie Gritton lpr = NULL; 22520304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 22530304c731SJamie Gritton mtx_lock(&cpr->pr_mtx); 22540304c731SJamie Gritton if (cpr->pr_ref > 0) { 22550304c731SJamie Gritton tpr = cpr; 22560304c731SJamie Gritton cpr->pr_ref++; 22570304c731SJamie Gritton } else { 22580304c731SJamie Gritton /* Already removed - do not do it again. */ 22590304c731SJamie Gritton tpr = NULL; 22600304c731SJamie Gritton } 22610304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 22620304c731SJamie Gritton if (lpr != NULL) { 22630304c731SJamie Gritton mtx_lock(&lpr->pr_mtx); 22640304c731SJamie Gritton prison_remove_one(lpr); 22650304c731SJamie Gritton sx_xlock(&allprison_lock); 22660304c731SJamie Gritton } 22670304c731SJamie Gritton lpr = tpr; 22680304c731SJamie Gritton } 22690304c731SJamie Gritton if (lpr != NULL) { 22700304c731SJamie Gritton mtx_lock(&lpr->pr_mtx); 22710304c731SJamie Gritton prison_remove_one(lpr); 22720304c731SJamie Gritton sx_xlock(&allprison_lock); 22730304c731SJamie Gritton } 22740304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 22750304c731SJamie Gritton } 22760304c731SJamie Gritton prison_remove_one(pr); 22770304c731SJamie Gritton return (0); 22780304c731SJamie Gritton } 22790304c731SJamie Gritton 22800304c731SJamie Gritton static void 22810304c731SJamie Gritton prison_remove_one(struct prison *pr) 22820304c731SJamie Gritton { 22830304c731SJamie Gritton struct proc *p; 22840304c731SJamie Gritton int deuref; 22850304c731SJamie Gritton 2286b38ff370SJamie Gritton /* If the prison was persistent, it is not anymore. */ 2287b38ff370SJamie Gritton deuref = 0; 2288b38ff370SJamie Gritton if (pr->pr_flags & PR_PERSIST) { 2289b38ff370SJamie Gritton pr->pr_ref--; 2290b38ff370SJamie Gritton deuref = PD_DEUREF; 2291b38ff370SJamie Gritton pr->pr_flags &= ~PR_PERSIST; 2292b38ff370SJamie Gritton } 2293b38ff370SJamie Gritton 22940304c731SJamie Gritton /* 22950304c731SJamie Gritton * jail_remove added a reference. If that's the only one, remove 22960304c731SJamie Gritton * the prison now. 22970304c731SJamie Gritton */ 22980304c731SJamie Gritton KASSERT(pr->pr_ref > 0, 22990304c731SJamie Gritton ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id)); 23000304c731SJamie Gritton if (pr->pr_ref == 1) { 2301b38ff370SJamie Gritton prison_deref(pr, 2302b38ff370SJamie Gritton deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 23030304c731SJamie Gritton return; 2304b38ff370SJamie Gritton } 2305b38ff370SJamie Gritton 2306b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2307b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2308b38ff370SJamie Gritton /* 2309b38ff370SJamie Gritton * Kill all processes unfortunate enough to be attached to this prison. 2310b38ff370SJamie Gritton */ 2311b38ff370SJamie Gritton sx_slock(&allproc_lock); 23127938a442SBjoern A. Zeeb FOREACH_PROC_IN_SYSTEM(p) { 2313b38ff370SJamie Gritton PROC_LOCK(p); 2314b38ff370SJamie Gritton if (p->p_state != PRS_NEW && p->p_ucred && 2315b38ff370SJamie Gritton p->p_ucred->cr_prison == pr) 23168451d0ddSKip Macy kern_psignal(p, SIGKILL); 2317b38ff370SJamie Gritton PROC_UNLOCK(p); 2318b38ff370SJamie Gritton } 2319b38ff370SJamie Gritton sx_sunlock(&allproc_lock); 23200304c731SJamie Gritton /* Remove the temporary reference added by jail_remove. */ 2321b38ff370SJamie Gritton prison_deref(pr, deuref | PD_DEREF); 232275c13541SPoul-Henning Kamp } 232375c13541SPoul-Henning Kamp 2324fd7a8150SMike Barcroft /* 23259ddb7954SMike Barcroft * struct jail_attach_args { 23269ddb7954SMike Barcroft * int jid; 23279ddb7954SMike Barcroft * }; 2328fd7a8150SMike Barcroft */ 2329fd7a8150SMike Barcroft int 23308451d0ddSKip Macy sys_jail_attach(struct thread *td, struct jail_attach_args *uap) 2331fd7a8150SMike Barcroft { 2332b38ff370SJamie Gritton struct prison *pr; 2333b38ff370SJamie Gritton int error; 2334b38ff370SJamie Gritton 2335b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 2336b38ff370SJamie Gritton if (error) 2337b38ff370SJamie Gritton return (error); 2338b38ff370SJamie Gritton 2339ef0ddea3SJamie Gritton /* 2340ef0ddea3SJamie Gritton * Start with exclusive hold on allprison_lock to ensure that a possible 2341ef0ddea3SJamie Gritton * PR_METHOD_REMOVE call isn't concurrent with jail_set or jail_remove. 2342ef0ddea3SJamie Gritton * But then immediately downgrade it since we don't need to stop 2343ef0ddea3SJamie Gritton * readers. 2344ef0ddea3SJamie Gritton */ 2345ef0ddea3SJamie Gritton sx_xlock(&allprison_lock); 2346ef0ddea3SJamie Gritton sx_downgrade(&allprison_lock); 23470304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2348b38ff370SJamie Gritton if (pr == NULL) { 2349b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2350b38ff370SJamie Gritton return (EINVAL); 2351b38ff370SJamie Gritton } 2352b38ff370SJamie Gritton 2353b38ff370SJamie Gritton /* 2354b38ff370SJamie Gritton * Do not allow a process to attach to a prison that is not 2355b38ff370SJamie Gritton * considered to be "alive". 2356b38ff370SJamie Gritton */ 2357b38ff370SJamie Gritton if (pr->pr_uref == 0) { 2358b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2359b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2360b38ff370SJamie Gritton return (EINVAL); 2361b38ff370SJamie Gritton } 2362b38ff370SJamie Gritton 2363b38ff370SJamie Gritton return (do_jail_attach(td, pr)); 2364b38ff370SJamie Gritton } 2365b38ff370SJamie Gritton 2366b38ff370SJamie Gritton static int 2367b38ff370SJamie Gritton do_jail_attach(struct thread *td, struct prison *pr) 2368b38ff370SJamie Gritton { 2369fd7a8150SMike Barcroft struct proc *p; 2370fd7a8150SMike Barcroft struct ucred *newcred, *oldcred; 23715050aa86SKonstantin Belousov int error; 2372fd7a8150SMike Barcroft 237357f22bd4SJacques Vidrine /* 237457f22bd4SJacques Vidrine * XXX: Note that there is a slight race here if two threads 237557f22bd4SJacques Vidrine * in the same privileged process attempt to attach to two 237657f22bd4SJacques Vidrine * different jails at the same time. It is important for 237757f22bd4SJacques Vidrine * user processes not to do this, or they might end up with 237857f22bd4SJacques Vidrine * a process root from one prison, but attached to the jail 237957f22bd4SJacques Vidrine * of another. 238057f22bd4SJacques Vidrine */ 2381fd7a8150SMike Barcroft pr->pr_ref++; 2382b38ff370SJamie Gritton pr->pr_uref++; 2383fd7a8150SMike Barcroft mtx_unlock(&pr->pr_mtx); 2384b38ff370SJamie Gritton 2385b38ff370SJamie Gritton /* Let modules do whatever they need to prepare for attaching. */ 2386b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_ATTACH, td); 2387b38ff370SJamie Gritton if (error) { 2388b38ff370SJamie Gritton prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED); 2389b38ff370SJamie Gritton return (error); 2390b38ff370SJamie Gritton } 2391dc68a633SPawel Jakub Dawidek sx_sunlock(&allprison_lock); 2392fd7a8150SMike Barcroft 2393413628a7SBjoern A. Zeeb /* 2394413628a7SBjoern A. Zeeb * Reparent the newly attached process to this jail. 2395413628a7SBjoern A. Zeeb */ 2396b38ff370SJamie Gritton p = td->td_proc; 2397413628a7SBjoern A. Zeeb error = cpuset_setproc_update_set(p, pr->pr_cpuset); 2398413628a7SBjoern A. Zeeb if (error) 2399b38ff370SJamie Gritton goto e_revert_osd; 2400413628a7SBjoern A. Zeeb 2401cb05b60aSAttilio Rao vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY); 2402fd7a8150SMike Barcroft if ((error = change_dir(pr->pr_root, td)) != 0) 2403fd7a8150SMike Barcroft goto e_unlock; 2404fd7a8150SMike Barcroft #ifdef MAC 240530d239bcSRobert Watson if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root))) 2406fd7a8150SMike Barcroft goto e_unlock; 2407fd7a8150SMike Barcroft #endif 2408b249ce48SMateusz Guzik VOP_UNLOCK(pr->pr_root); 2409f0725a8eSMateusz Guzik if ((error = pwd_chroot(td, pr->pr_root))) 24105050aa86SKonstantin Belousov goto e_revert_osd; 2411fd7a8150SMike Barcroft 2412fd7a8150SMike Barcroft newcred = crget(); 2413fd7a8150SMike Barcroft PROC_LOCK(p); 24141fb6767dSJamie Gritton oldcred = crcopysafe(p, newcred); 241569c4ee54SJohn Baldwin newcred->cr_prison = pr; 2416daf63fd2SMateusz Guzik proc_set_cred(p, newcred); 24171fb6767dSJamie Gritton setsugid(p); 2418097055e2SEdward Tomasz Napierala #ifdef RACCT 2419097055e2SEdward Tomasz Napierala racct_proc_ucred_changed(p, oldcred, newcred); 2420f87beb93SAndriy Gapon crhold(newcred); 2421f87beb93SAndriy Gapon #endif 2422f87beb93SAndriy Gapon PROC_UNLOCK(p); 2423f87beb93SAndriy Gapon #ifdef RCTL 2424f87beb93SAndriy Gapon rctl_proc_ucred_changed(p, newcred); 2425f87beb93SAndriy Gapon crfree(newcred); 2426097055e2SEdward Tomasz Napierala #endif 24271fb6767dSJamie Gritton prison_deref(oldcred->cr_prison, PD_DEREF | PD_DEUREF); 2428fd7a8150SMike Barcroft crfree(oldcred); 2429fd7a8150SMike Barcroft return (0); 24301fb6767dSJamie Gritton 2431fd7a8150SMike Barcroft e_unlock: 2432b249ce48SMateusz Guzik VOP_UNLOCK(pr->pr_root); 2433b38ff370SJamie Gritton e_revert_osd: 2434b38ff370SJamie Gritton /* Tell modules this thread is still in its old jail after all. */ 24351fb6767dSJamie Gritton (void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td); 2436b38ff370SJamie Gritton prison_deref(pr, PD_DEREF | PD_DEUREF); 2437fd7a8150SMike Barcroft return (error); 2438fd7a8150SMike Barcroft } 2439fd7a8150SMike Barcroft 2440fd7a8150SMike Barcroft /* 2441fd7a8150SMike Barcroft * Returns a locked prison instance, or NULL on failure. 2442fd7a8150SMike Barcroft */ 244354b369c1SPawel Jakub Dawidek struct prison * 2444fd7a8150SMike Barcroft prison_find(int prid) 2445fd7a8150SMike Barcroft { 2446fd7a8150SMike Barcroft struct prison *pr; 2447fd7a8150SMike Barcroft 2448dc68a633SPawel Jakub Dawidek sx_assert(&allprison_lock, SX_LOCKED); 2449b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 2450fd7a8150SMike Barcroft if (pr->pr_id == prid) { 2451fd7a8150SMike Barcroft mtx_lock(&pr->pr_mtx); 2452b38ff370SJamie Gritton if (pr->pr_ref > 0) 2453fd7a8150SMike Barcroft return (pr); 2454b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2455fd7a8150SMike Barcroft } 2456fd7a8150SMike Barcroft } 2457fd7a8150SMike Barcroft return (NULL); 2458fd7a8150SMike Barcroft } 2459fd7a8150SMike Barcroft 2460b38ff370SJamie Gritton /* 24610304c731SJamie Gritton * Find a prison that is a descendant of mypr. Returns a locked prison or NULL. 2462b38ff370SJamie Gritton */ 2463b38ff370SJamie Gritton struct prison * 24640304c731SJamie Gritton prison_find_child(struct prison *mypr, int prid) 2465b38ff370SJamie Gritton { 24660304c731SJamie Gritton struct prison *pr; 24670304c731SJamie Gritton int descend; 2468b38ff370SJamie Gritton 2469b38ff370SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 24700304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 24710304c731SJamie Gritton if (pr->pr_id == prid) { 24720304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 24730304c731SJamie Gritton if (pr->pr_ref > 0) 24740304c731SJamie Gritton return (pr); 24750304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 24760304c731SJamie Gritton } 24770304c731SJamie Gritton } 24780304c731SJamie Gritton return (NULL); 24790304c731SJamie Gritton } 24800304c731SJamie Gritton 24810304c731SJamie Gritton /* 24820304c731SJamie Gritton * Look for the name relative to mypr. Returns a locked prison or NULL. 24830304c731SJamie Gritton */ 24840304c731SJamie Gritton struct prison * 24850304c731SJamie Gritton prison_find_name(struct prison *mypr, const char *name) 24860304c731SJamie Gritton { 24870304c731SJamie Gritton struct prison *pr, *deadpr; 24880304c731SJamie Gritton size_t mylen; 24890304c731SJamie Gritton int descend; 24900304c731SJamie Gritton 24910304c731SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 24920304c731SJamie Gritton mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1; 2493b38ff370SJamie Gritton again: 2494b38ff370SJamie Gritton deadpr = NULL; 24950304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 24960304c731SJamie Gritton if (!strcmp(pr->pr_name + mylen, name)) { 2497b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 2498b38ff370SJamie Gritton if (pr->pr_ref > 0) { 2499b38ff370SJamie Gritton if (pr->pr_uref > 0) 2500b38ff370SJamie Gritton return (pr); 2501b38ff370SJamie Gritton deadpr = pr; 2502b38ff370SJamie Gritton } 2503b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2504b38ff370SJamie Gritton } 2505b38ff370SJamie Gritton } 25060304c731SJamie Gritton /* There was no valid prison - perhaps there was a dying one. */ 2507b38ff370SJamie Gritton if (deadpr != NULL) { 2508b38ff370SJamie Gritton mtx_lock(&deadpr->pr_mtx); 2509b38ff370SJamie Gritton if (deadpr->pr_ref == 0) { 2510b38ff370SJamie Gritton mtx_unlock(&deadpr->pr_mtx); 2511b38ff370SJamie Gritton goto again; 2512b38ff370SJamie Gritton } 2513b38ff370SJamie Gritton } 2514b38ff370SJamie Gritton return (deadpr); 2515b38ff370SJamie Gritton } 2516b38ff370SJamie Gritton 2517b38ff370SJamie Gritton /* 25180304c731SJamie Gritton * See if a prison has the specific flag set. 25190304c731SJamie Gritton */ 25200304c731SJamie Gritton int 25210304c731SJamie Gritton prison_flag(struct ucred *cred, unsigned flag) 25220304c731SJamie Gritton { 25230304c731SJamie Gritton 25240304c731SJamie Gritton /* This is an atomic read, so no locking is necessary. */ 25250304c731SJamie Gritton return (cred->cr_prison->pr_flags & flag); 25260304c731SJamie Gritton } 25270304c731SJamie Gritton 25280304c731SJamie Gritton int 25290304c731SJamie Gritton prison_allow(struct ucred *cred, unsigned flag) 25300304c731SJamie Gritton { 25310304c731SJamie Gritton 25320304c731SJamie Gritton /* This is an atomic read, so no locking is necessary. */ 25330304c731SJamie Gritton return (cred->cr_prison->pr_allow & flag); 25340304c731SJamie Gritton } 25350304c731SJamie Gritton 25360304c731SJamie Gritton /* 2537b38ff370SJamie Gritton * Remove a prison reference. If that was the last reference, remove the 2538b38ff370SJamie Gritton * prison itself - but not in this context in case there are locks held. 2539b38ff370SJamie Gritton */ 254091421ba2SRobert Watson void 25411ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr) 254291421ba2SRobert Watson { 254373d9e52dSJamie Gritton int ref; 254491421ba2SRobert Watson 25451ba4a712SPawel Jakub Dawidek mtx_assert(&pr->pr_mtx, MA_OWNED); 254673d9e52dSJamie Gritton ref = --pr->pr_ref; 254701137630SRobert Watson mtx_unlock(&pr->pr_mtx); 254873d9e52dSJamie Gritton if (ref == 0) 2549c2cda609SPawel Jakub Dawidek taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 2550c2cda609SPawel Jakub Dawidek } 2551c2cda609SPawel Jakub Dawidek 25521ba4a712SPawel Jakub Dawidek void 25531ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr) 25541ba4a712SPawel Jakub Dawidek { 25551ba4a712SPawel Jakub Dawidek 25561ba4a712SPawel Jakub Dawidek mtx_lock(&pr->pr_mtx); 25571ba4a712SPawel Jakub Dawidek prison_free_locked(pr); 25581ba4a712SPawel Jakub Dawidek } 25591ba4a712SPawel Jakub Dawidek 256073d9e52dSJamie Gritton /* 256173d9e52dSJamie Gritton * Complete a call to either prison_free or prison_proc_free. 256273d9e52dSJamie Gritton */ 2563c2cda609SPawel Jakub Dawidek static void 2564c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending) 2565c2cda609SPawel Jakub Dawidek { 256673d9e52dSJamie Gritton struct prison *pr = context; 2567b38ff370SJamie Gritton 2568ef0ddea3SJamie Gritton sx_xlock(&allprison_lock); 256973d9e52dSJamie Gritton mtx_lock(&pr->pr_mtx); 257073d9e52dSJamie Gritton prison_deref(pr, pr->pr_uref 2571ef0ddea3SJamie Gritton ? PD_DEREF | PD_DEUREF | PD_LOCKED | PD_LIST_XLOCKED 2572ef0ddea3SJamie Gritton : PD_LOCKED | PD_LIST_XLOCKED); 2573b38ff370SJamie Gritton } 2574b38ff370SJamie Gritton 2575b38ff370SJamie Gritton /* 2576b38ff370SJamie Gritton * Remove a prison reference (usually). This internal version assumes no 2577b38ff370SJamie Gritton * mutexes are held, except perhaps the prison itself. If there are no more 2578b38ff370SJamie Gritton * references, release and delist the prison. On completion, the prison lock 2579b38ff370SJamie Gritton * and the allprison lock are both unlocked. 2580b38ff370SJamie Gritton */ 2581b38ff370SJamie Gritton static void 2582b38ff370SJamie Gritton prison_deref(struct prison *pr, int flags) 2583b38ff370SJamie Gritton { 25840304c731SJamie Gritton struct prison *ppr, *tpr; 2585cc5fd8c7SJamie Gritton int ref, lasturef; 2586c2cda609SPawel Jakub Dawidek 2587b38ff370SJamie Gritton if (!(flags & PD_LOCKED)) 2588b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 25890304c731SJamie Gritton for (;;) { 2590e6d5cb63SJamie Gritton if (flags & PD_DEUREF) { 259173d9e52dSJamie Gritton KASSERT(pr->pr_uref > 0, 259273d9e52dSJamie Gritton ("prison_deref PD_DEUREF on a dead prison (jid=%d)", 259373d9e52dSJamie Gritton pr->pr_id)); 2594e6d5cb63SJamie Gritton pr->pr_uref--; 2595cc5fd8c7SJamie Gritton lasturef = pr->pr_uref == 0; 2596cc5fd8c7SJamie Gritton if (lasturef) 2597cc5fd8c7SJamie Gritton pr->pr_ref++; 2598e6d5cb63SJamie Gritton KASSERT(prison0.pr_uref != 0, ("prison0 pr_uref=0")); 2599cc5fd8c7SJamie Gritton } else 2600cc5fd8c7SJamie Gritton lasturef = 0; 260173d9e52dSJamie Gritton if (flags & PD_DEREF) { 260273d9e52dSJamie Gritton KASSERT(pr->pr_ref > 0, 260373d9e52dSJamie Gritton ("prison_deref PD_DEREF on a dead prison (jid=%d)", 260473d9e52dSJamie Gritton pr->pr_id)); 2605b38ff370SJamie Gritton pr->pr_ref--; 260673d9e52dSJamie Gritton } 2607cc5fd8c7SJamie Gritton ref = pr->pr_ref; 2608b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2609cc5fd8c7SJamie Gritton 2610cc5fd8c7SJamie Gritton /* 2611cc5fd8c7SJamie Gritton * Tell the modules if the last user reference was removed 2612cc5fd8c7SJamie Gritton * (even it sticks around in dying state). 2613cc5fd8c7SJamie Gritton */ 2614cc5fd8c7SJamie Gritton if (lasturef) { 2615cc5fd8c7SJamie Gritton if (!(flags & (PD_LIST_SLOCKED | PD_LIST_XLOCKED))) { 2616cc5fd8c7SJamie Gritton sx_xlock(&allprison_lock); 2617cc5fd8c7SJamie Gritton flags |= PD_LIST_XLOCKED; 2618cc5fd8c7SJamie Gritton } 2619cc5fd8c7SJamie Gritton (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL); 2620cc5fd8c7SJamie Gritton mtx_lock(&pr->pr_mtx); 2621cc5fd8c7SJamie Gritton ref = --pr->pr_ref; 2622cc5fd8c7SJamie Gritton mtx_unlock(&pr->pr_mtx); 2623cc5fd8c7SJamie Gritton } 2624cc5fd8c7SJamie Gritton 2625cc5fd8c7SJamie Gritton /* If the prison still has references, nothing else to do. */ 2626cc5fd8c7SJamie Gritton if (ref > 0) { 2627b38ff370SJamie Gritton if (flags & PD_LIST_SLOCKED) 2628b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2629b38ff370SJamie Gritton else if (flags & PD_LIST_XLOCKED) 2630b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2631b38ff370SJamie Gritton return; 2632b38ff370SJamie Gritton } 2633c2cda609SPawel Jakub Dawidek 2634b38ff370SJamie Gritton if (flags & PD_LIST_SLOCKED) { 2635b38ff370SJamie Gritton if (!sx_try_upgrade(&allprison_lock)) { 2636b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2637c2cda609SPawel Jakub Dawidek sx_xlock(&allprison_lock); 2638b38ff370SJamie Gritton } 2639b38ff370SJamie Gritton } else if (!(flags & PD_LIST_XLOCKED)) 2640b38ff370SJamie Gritton sx_xlock(&allprison_lock); 2641b38ff370SJamie Gritton 2642b38ff370SJamie Gritton TAILQ_REMOVE(&allprison, pr, pr_list); 26430304c731SJamie Gritton LIST_REMOVE(pr, pr_sibling); 26440304c731SJamie Gritton ppr = pr->pr_parent; 26450304c731SJamie Gritton for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 2646b97457e2SJamie Gritton tpr->pr_childcount--; 2647c4884ffaSJamie Gritton sx_xunlock(&allprison_lock); 26481ba4a712SPawel Jakub Dawidek 2649679e1390SJamie Gritton #ifdef VIMAGE 26500cb8b6a9SMarko Zec if (pr->pr_vnet != ppr->pr_vnet) 2651679e1390SJamie Gritton vnet_destroy(pr->pr_vnet); 2652679e1390SJamie Gritton #endif 26535050aa86SKonstantin Belousov if (pr->pr_root != NULL) 2654b3059e09SRobert Watson vrele(pr->pr_root); 2655b3059e09SRobert Watson mtx_destroy(&pr->pr_mtx); 2656413628a7SBjoern A. Zeeb #ifdef INET 2657413628a7SBjoern A. Zeeb free(pr->pr_ip4, M_PRISON); 2658413628a7SBjoern A. Zeeb #endif 2659b38ff370SJamie Gritton #ifdef INET6 2660b38ff370SJamie Gritton free(pr->pr_ip6, M_PRISON); 2661b38ff370SJamie Gritton #endif 2662b38ff370SJamie Gritton if (pr->pr_cpuset != NULL) 2663b38ff370SJamie Gritton cpuset_rel(pr->pr_cpuset); 2664b38ff370SJamie Gritton osd_jail_exit(pr); 2665a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 26664b5c9cf6SEdward Tomasz Napierala if (racct_enable) 2667a7ad07bfSEdward Tomasz Napierala prison_racct_detach(pr); 2668ec125fbbSEdward Tomasz Napierala #endif 26691ede983cSDag-Erling Smørgrav free(pr, M_PRISON); 26700304c731SJamie Gritton 26710304c731SJamie Gritton /* Removing a prison frees a reference on its parent. */ 26720304c731SJamie Gritton pr = ppr; 26730304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 2674e6d5cb63SJamie Gritton flags = PD_DEREF | PD_DEUREF; 26750304c731SJamie Gritton } 2676b3059e09SRobert Watson } 2677b3059e09SRobert Watson 267891421ba2SRobert Watson void 26791ba4a712SPawel Jakub Dawidek prison_hold_locked(struct prison *pr) 26801ba4a712SPawel Jakub Dawidek { 26811ba4a712SPawel Jakub Dawidek 26821ba4a712SPawel Jakub Dawidek mtx_assert(&pr->pr_mtx, MA_OWNED); 26831ba4a712SPawel Jakub Dawidek KASSERT(pr->pr_ref > 0, 268436b41cc3SBjoern A. Zeeb ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id)); 26851ba4a712SPawel Jakub Dawidek pr->pr_ref++; 26861ba4a712SPawel Jakub Dawidek } 26871ba4a712SPawel Jakub Dawidek 26881ba4a712SPawel Jakub Dawidek void 268991421ba2SRobert Watson prison_hold(struct prison *pr) 269091421ba2SRobert Watson { 269191421ba2SRobert Watson 269201137630SRobert Watson mtx_lock(&pr->pr_mtx); 26931ba4a712SPawel Jakub Dawidek prison_hold_locked(pr); 269401137630SRobert Watson mtx_unlock(&pr->pr_mtx); 269501137630SRobert Watson } 269601137630SRobert Watson 2697413628a7SBjoern A. Zeeb void 2698413628a7SBjoern A. Zeeb prison_proc_hold(struct prison *pr) 269901137630SRobert Watson { 270001137630SRobert Watson 2701413628a7SBjoern A. Zeeb mtx_lock(&pr->pr_mtx); 2702b38ff370SJamie Gritton KASSERT(pr->pr_uref > 0, 2703b38ff370SJamie Gritton ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id)); 2704b38ff370SJamie Gritton pr->pr_uref++; 2705413628a7SBjoern A. Zeeb mtx_unlock(&pr->pr_mtx); 270675c13541SPoul-Henning Kamp } 270775c13541SPoul-Henning Kamp 270875c13541SPoul-Henning Kamp void 2709413628a7SBjoern A. Zeeb prison_proc_free(struct prison *pr) 271075c13541SPoul-Henning Kamp { 2711413628a7SBjoern A. Zeeb 2712413628a7SBjoern A. Zeeb mtx_lock(&pr->pr_mtx); 2713b38ff370SJamie Gritton KASSERT(pr->pr_uref > 0, 2714b38ff370SJamie Gritton ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id)); 271573d9e52dSJamie Gritton if (pr->pr_uref > 1) 271673d9e52dSJamie Gritton pr->pr_uref--; 271773d9e52dSJamie Gritton else { 271873d9e52dSJamie Gritton /* 271973d9e52dSJamie Gritton * Don't remove the last user reference in this context, which 272073d9e52dSJamie Gritton * is expected to be a process that is not only locked, but 272173d9e52dSJamie Gritton * also half dead. 272273d9e52dSJamie Gritton */ 272373d9e52dSJamie Gritton pr->pr_ref++; 272473d9e52dSJamie Gritton mtx_unlock(&pr->pr_mtx); 272573d9e52dSJamie Gritton taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 272673d9e52dSJamie Gritton return; 272773d9e52dSJamie Gritton } 272873d9e52dSJamie Gritton mtx_unlock(&pr->pr_mtx); 2729413628a7SBjoern A. Zeeb } 2730413628a7SBjoern A. Zeeb 2731413628a7SBjoern A. Zeeb /* 2732ca04ba64SJamie Gritton * Check if a jail supports the given address family. 2733ca04ba64SJamie Gritton * 2734ca04ba64SJamie Gritton * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT 2735ca04ba64SJamie Gritton * if not. 2736ca04ba64SJamie Gritton */ 2737ca04ba64SJamie Gritton int 2738ca04ba64SJamie Gritton prison_check_af(struct ucred *cred, int af) 2739ca04ba64SJamie Gritton { 27400304c731SJamie Gritton struct prison *pr; 2741ca04ba64SJamie Gritton int error; 2742ca04ba64SJamie Gritton 2743ca04ba64SJamie Gritton KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2744ca04ba64SJamie Gritton 27450304c731SJamie Gritton pr = cred->cr_prison; 2746499650a0SJamie Gritton #ifdef VIMAGE 27476bb79563SJamie Gritton /* Prisons with their own network stack are not limited. */ 2748de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 27496bb79563SJamie Gritton return (0); 2750499650a0SJamie Gritton #endif 27516bb79563SJamie Gritton 2752ca04ba64SJamie Gritton error = 0; 2753ca04ba64SJamie Gritton switch (af) 2754ca04ba64SJamie Gritton { 2755ca04ba64SJamie Gritton #ifdef INET 2756ca04ba64SJamie Gritton case AF_INET: 27570304c731SJamie Gritton if (pr->pr_flags & PR_IP4) 27580304c731SJamie Gritton { 27590304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 27600304c731SJamie Gritton if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL) 2761ca04ba64SJamie Gritton error = EAFNOSUPPORT; 27620304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 27630304c731SJamie Gritton } 2764ca04ba64SJamie Gritton break; 2765ca04ba64SJamie Gritton #endif 2766ca04ba64SJamie Gritton #ifdef INET6 2767ca04ba64SJamie Gritton case AF_INET6: 27680304c731SJamie Gritton if (pr->pr_flags & PR_IP6) 27690304c731SJamie Gritton { 27700304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 27710304c731SJamie Gritton if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL) 2772ca04ba64SJamie Gritton error = EAFNOSUPPORT; 27730304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 27740304c731SJamie Gritton } 2775ca04ba64SJamie Gritton break; 2776ca04ba64SJamie Gritton #endif 2777ca04ba64SJamie Gritton case AF_LOCAL: 2778ca04ba64SJamie Gritton case AF_ROUTE: 2779ca04ba64SJamie Gritton break; 2780ca04ba64SJamie Gritton default: 27810304c731SJamie Gritton if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF)) 2782ca04ba64SJamie Gritton error = EAFNOSUPPORT; 2783ca04ba64SJamie Gritton } 2784ca04ba64SJamie Gritton return (error); 2785ca04ba64SJamie Gritton } 2786ca04ba64SJamie Gritton 2787ca04ba64SJamie Gritton /* 2788413628a7SBjoern A. Zeeb * Check if given address belongs to the jail referenced by cred (wrapper to 2789413628a7SBjoern A. Zeeb * prison_check_ip[46]). 2790413628a7SBjoern A. Zeeb * 27910304c731SJamie Gritton * Returns 0 if jail doesn't restrict the address family or if address belongs 27920304c731SJamie Gritton * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if 27930304c731SJamie Gritton * the jail doesn't allow the address family. IPv4 Address passed in in NBO. 2794413628a7SBjoern A. Zeeb */ 2795413628a7SBjoern A. Zeeb int 2796c83dda36SAlexander V. Chernikov prison_if(struct ucred *cred, const struct sockaddr *sa) 279775c13541SPoul-Henning Kamp { 2798413628a7SBjoern A. Zeeb #ifdef INET 2799c83dda36SAlexander V. Chernikov const struct sockaddr_in *sai; 2800413628a7SBjoern A. Zeeb #endif 2801413628a7SBjoern A. Zeeb #ifdef INET6 2802c83dda36SAlexander V. Chernikov const struct sockaddr_in6 *sai6; 2803413628a7SBjoern A. Zeeb #endif 2804b89e82ddSJamie Gritton int error; 280575c13541SPoul-Henning Kamp 2806413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2807413628a7SBjoern A. Zeeb KASSERT(sa != NULL, ("%s: sa is NULL", __func__)); 2808413628a7SBjoern A. Zeeb 2809de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE 2810de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 2811de0bd6f7SBjoern A. Zeeb return (0); 2812de0bd6f7SBjoern A. Zeeb #endif 2813de0bd6f7SBjoern A. Zeeb 2814b89e82ddSJamie Gritton error = 0; 2815413628a7SBjoern A. Zeeb switch (sa->sa_family) 2816413628a7SBjoern A. Zeeb { 2817413628a7SBjoern A. Zeeb #ifdef INET 2818413628a7SBjoern A. Zeeb case AF_INET: 2819c83dda36SAlexander V. Chernikov sai = (const struct sockaddr_in *)sa; 2820b89e82ddSJamie Gritton error = prison_check_ip4(cred, &sai->sin_addr); 2821413628a7SBjoern A. Zeeb break; 2822413628a7SBjoern A. Zeeb #endif 2823413628a7SBjoern A. Zeeb #ifdef INET6 2824413628a7SBjoern A. Zeeb case AF_INET6: 2825c83dda36SAlexander V. Chernikov sai6 = (const struct sockaddr_in6 *)sa; 2826b89e82ddSJamie Gritton error = prison_check_ip6(cred, &sai6->sin6_addr); 2827413628a7SBjoern A. Zeeb break; 2828413628a7SBjoern A. Zeeb #endif 2829413628a7SBjoern A. Zeeb default: 28300304c731SJamie Gritton if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF)) 2831b89e82ddSJamie Gritton error = EAFNOSUPPORT; 2832413628a7SBjoern A. Zeeb } 2833b89e82ddSJamie Gritton return (error); 283475c13541SPoul-Henning Kamp } 283591421ba2SRobert Watson 283691421ba2SRobert Watson /* 283791421ba2SRobert Watson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH. 283891421ba2SRobert Watson */ 283991421ba2SRobert Watson int 28409ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2) 284191421ba2SRobert Watson { 284291421ba2SRobert Watson 28430304c731SJamie Gritton return ((cred1->cr_prison == cred2->cr_prison || 28440304c731SJamie Gritton prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH); 28450304c731SJamie Gritton } 284691421ba2SRobert Watson 28470304c731SJamie Gritton /* 28480304c731SJamie Gritton * Return 1 if p2 is a child of p1, otherwise 0. 28490304c731SJamie Gritton */ 28500304c731SJamie Gritton int 28510304c731SJamie Gritton prison_ischild(struct prison *pr1, struct prison *pr2) 28520304c731SJamie Gritton { 28530304c731SJamie Gritton 28540304c731SJamie Gritton for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent) 28550304c731SJamie Gritton if (pr1 == pr2) 28560304c731SJamie Gritton return (1); 285791421ba2SRobert Watson return (0); 285891421ba2SRobert Watson } 285991421ba2SRobert Watson 286091421ba2SRobert Watson /* 2861de0bd6f7SBjoern A. Zeeb * Return 1 if the passed credential is in a jail and that jail does not 2862de0bd6f7SBjoern A. Zeeb * have its own virtual network stack, otherwise 0. 2863de0bd6f7SBjoern A. Zeeb */ 2864de0bd6f7SBjoern A. Zeeb int 2865de0bd6f7SBjoern A. Zeeb jailed_without_vnet(struct ucred *cred) 2866de0bd6f7SBjoern A. Zeeb { 2867de0bd6f7SBjoern A. Zeeb 2868de0bd6f7SBjoern A. Zeeb if (!jailed(cred)) 2869de0bd6f7SBjoern A. Zeeb return (0); 2870de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE 2871de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 2872de0bd6f7SBjoern A. Zeeb return (0); 2873de0bd6f7SBjoern A. Zeeb #endif 2874de0bd6f7SBjoern A. Zeeb 2875de0bd6f7SBjoern A. Zeeb return (1); 2876de0bd6f7SBjoern A. Zeeb } 2877de0bd6f7SBjoern A. Zeeb 2878de0bd6f7SBjoern A. Zeeb /* 28797455b100SJamie Gritton * Return the correct hostname (domainname, et al) for the passed credential. 28809484d0c0SRobert Drehmel */ 2881ad1ff099SRobert Drehmel void 28829ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size) 28839484d0c0SRobert Drehmel { 288476ca6f88SJamie Gritton struct prison *pr; 28859484d0c0SRobert Drehmel 28867455b100SJamie Gritton /* 28877455b100SJamie Gritton * A NULL credential can be used to shortcut to the physical 28887455b100SJamie Gritton * system's hostname. 28897455b100SJamie Gritton */ 289076ca6f88SJamie Gritton pr = (cred != NULL) ? cred->cr_prison : &prison0; 289176ca6f88SJamie Gritton mtx_lock(&pr->pr_mtx); 2892c1f19219SJamie Gritton strlcpy(buf, pr->pr_hostname, size); 289376ca6f88SJamie Gritton mtx_unlock(&pr->pr_mtx); 28949484d0c0SRobert Drehmel } 2895fd7a8150SMike Barcroft 28967455b100SJamie Gritton void 28977455b100SJamie Gritton getcreddomainname(struct ucred *cred, char *buf, size_t size) 28987455b100SJamie Gritton { 28997455b100SJamie Gritton 29007455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 2901c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_domainname, size); 29027455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 29037455b100SJamie Gritton } 29047455b100SJamie Gritton 29057455b100SJamie Gritton void 29067455b100SJamie Gritton getcredhostuuid(struct ucred *cred, char *buf, size_t size) 29077455b100SJamie Gritton { 29087455b100SJamie Gritton 29097455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 2910c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_hostuuid, size); 29117455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 29127455b100SJamie Gritton } 29137455b100SJamie Gritton 29147455b100SJamie Gritton void 29157455b100SJamie Gritton getcredhostid(struct ucred *cred, unsigned long *hostid) 29167455b100SJamie Gritton { 29177455b100SJamie Gritton 29187455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 29197455b100SJamie Gritton *hostid = cred->cr_prison->pr_hostid; 29207455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 29217455b100SJamie Gritton } 29227455b100SJamie Gritton 29233f8bc99cSKristof Provost void 29243f8bc99cSKristof Provost getjailname(struct ucred *cred, char *name, size_t len) 29253f8bc99cSKristof Provost { 29263f8bc99cSKristof Provost 29273f8bc99cSKristof Provost mtx_lock(&cred->cr_prison->pr_mtx); 29283f8bc99cSKristof Provost strlcpy(name, cred->cr_prison->pr_name, len); 29293f8bc99cSKristof Provost mtx_unlock(&cred->cr_prison->pr_mtx); 29303f8bc99cSKristof Provost } 29313f8bc99cSKristof Provost 2932eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE 2933eb79e1c7SBjoern A. Zeeb /* 2934eb79e1c7SBjoern A. Zeeb * Determine whether the prison represented by cred owns 2935eb79e1c7SBjoern A. Zeeb * its vnet rather than having it inherited. 2936eb79e1c7SBjoern A. Zeeb * 2937eb79e1c7SBjoern A. Zeeb * Returns 1 in case the prison owns the vnet, 0 otherwise. 2938eb79e1c7SBjoern A. Zeeb */ 2939eb79e1c7SBjoern A. Zeeb int 2940eb79e1c7SBjoern A. Zeeb prison_owns_vnet(struct ucred *cred) 2941eb79e1c7SBjoern A. Zeeb { 2942eb79e1c7SBjoern A. Zeeb 2943eb79e1c7SBjoern A. Zeeb /* 2944eb79e1c7SBjoern A. Zeeb * vnets cannot be added/removed after jail creation, 2945eb79e1c7SBjoern A. Zeeb * so no need to lock here. 2946eb79e1c7SBjoern A. Zeeb */ 2947eb79e1c7SBjoern A. Zeeb return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0); 2948eb79e1c7SBjoern A. Zeeb } 2949eb79e1c7SBjoern A. Zeeb #endif 2950eb79e1c7SBjoern A. Zeeb 2951f08df373SRobert Watson /* 2952820a0de9SPawel Jakub Dawidek * Determine whether the subject represented by cred can "see" 2953820a0de9SPawel Jakub Dawidek * status of a mount point. 2954820a0de9SPawel Jakub Dawidek * Returns: 0 for permitted, ENOENT otherwise. 2955820a0de9SPawel Jakub Dawidek * XXX: This function should be called cr_canseemount() and should be 2956820a0de9SPawel Jakub Dawidek * placed in kern_prot.c. 2957f08df373SRobert Watson */ 2958f08df373SRobert Watson int 2959820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp) 2960f08df373SRobert Watson { 2961820a0de9SPawel Jakub Dawidek struct prison *pr; 2962820a0de9SPawel Jakub Dawidek struct statfs *sp; 2963820a0de9SPawel Jakub Dawidek size_t len; 2964f08df373SRobert Watson 2965820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 29660304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 29670304c731SJamie Gritton return (0); 2968820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) 2969820a0de9SPawel Jakub Dawidek return (0); 29700304c731SJamie Gritton if (pr->pr_enforce_statfs == 2) 2971820a0de9SPawel Jakub Dawidek return (ENOENT); 2972820a0de9SPawel Jakub Dawidek /* 2973820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 2974820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 2975820a0de9SPawel Jakub Dawidek * This is ugly check, but this is the only situation when jail's 2976820a0de9SPawel Jakub Dawidek * directory ends with '/'. 2977820a0de9SPawel Jakub Dawidek */ 2978820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 2979820a0de9SPawel Jakub Dawidek return (0); 2980820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 2981820a0de9SPawel Jakub Dawidek sp = &mp->mnt_stat; 2982820a0de9SPawel Jakub Dawidek if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0) 2983820a0de9SPawel Jakub Dawidek return (ENOENT); 2984820a0de9SPawel Jakub Dawidek /* 2985820a0de9SPawel Jakub Dawidek * Be sure that we don't have situation where jail's root directory 2986820a0de9SPawel Jakub Dawidek * is "/some/path" and mount point is "/some/pathpath". 2987820a0de9SPawel Jakub Dawidek */ 2988820a0de9SPawel Jakub Dawidek if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/') 2989820a0de9SPawel Jakub Dawidek return (ENOENT); 2990f08df373SRobert Watson return (0); 2991f08df373SRobert Watson } 2992820a0de9SPawel Jakub Dawidek 2993820a0de9SPawel Jakub Dawidek void 2994820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp) 2995820a0de9SPawel Jakub Dawidek { 2996820a0de9SPawel Jakub Dawidek char jpath[MAXPATHLEN]; 2997820a0de9SPawel Jakub Dawidek struct prison *pr; 2998820a0de9SPawel Jakub Dawidek size_t len; 2999820a0de9SPawel Jakub Dawidek 3000820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 30010304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 30020304c731SJamie Gritton return; 3003820a0de9SPawel Jakub Dawidek if (prison_canseemount(cred, mp) != 0) { 3004820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3005820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, "[restricted]", 3006820a0de9SPawel Jakub Dawidek sizeof(sp->f_mntonname)); 3007820a0de9SPawel Jakub Dawidek return; 3008820a0de9SPawel Jakub Dawidek } 3009820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) { 3010820a0de9SPawel Jakub Dawidek /* 3011820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3012820a0de9SPawel Jakub Dawidek * the valid path left there. 3013820a0de9SPawel Jakub Dawidek */ 3014820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3015820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3016820a0de9SPawel Jakub Dawidek return; 3017820a0de9SPawel Jakub Dawidek } 3018820a0de9SPawel Jakub Dawidek /* 3019820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 3020820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 3021820a0de9SPawel Jakub Dawidek */ 3022820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 3023820a0de9SPawel Jakub Dawidek return; 3024820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 3025820a0de9SPawel Jakub Dawidek strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath)); 3026820a0de9SPawel Jakub Dawidek /* 3027820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3028820a0de9SPawel Jakub Dawidek * the valid path left there. 3029820a0de9SPawel Jakub Dawidek */ 3030820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3031820a0de9SPawel Jakub Dawidek if (*jpath == '\0') { 3032820a0de9SPawel Jakub Dawidek /* Should never happen. */ 3033820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3034820a0de9SPawel Jakub Dawidek } else { 3035820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname)); 3036820a0de9SPawel Jakub Dawidek } 3037f08df373SRobert Watson } 3038f08df373SRobert Watson 3039800c9408SRobert Watson /* 3040800c9408SRobert Watson * Check with permission for a specific privilege is granted within jail. We 3041800c9408SRobert Watson * have a specific list of accepted privileges; the rest are denied. 3042800c9408SRobert Watson */ 3043800c9408SRobert Watson int 3044800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv) 3045800c9408SRobert Watson { 3046800c9408SRobert Watson 30477b2ff0dcSMateusz Guzik /* 30487b2ff0dcSMateusz Guzik * Some policies have custom handlers. This routine should not be 30497b2ff0dcSMateusz Guzik * called for them. See priv_check_cred(). 30507b2ff0dcSMateusz Guzik */ 30517b2ff0dcSMateusz Guzik switch (priv) { 30527b2ff0dcSMateusz Guzik case PRIV_VFS_GENERATION: 30537b2ff0dcSMateusz Guzik KASSERT(0, ("prison_priv_check instead of a custom handler " 30547b2ff0dcSMateusz Guzik "called for %d\n", priv)); 30557b2ff0dcSMateusz Guzik } 30567b2ff0dcSMateusz Guzik 3057800c9408SRobert Watson if (!jailed(cred)) 3058800c9408SRobert Watson return (0); 3059800c9408SRobert Watson 30606bb79563SJamie Gritton #ifdef VIMAGE 30616bb79563SJamie Gritton /* 30626bb79563SJamie Gritton * Privileges specific to prisons with a virtual network stack. 30636bb79563SJamie Gritton * There might be a duplicate entry here in case the privilege 30646bb79563SJamie Gritton * is only granted conditionally in the legacy jail case. 30656bb79563SJamie Gritton */ 30666bb79563SJamie Gritton switch (priv) { 30676bb79563SJamie Gritton #ifdef notyet 30686bb79563SJamie Gritton /* 30696bb79563SJamie Gritton * NFS-specific privileges. 30706bb79563SJamie Gritton */ 30716bb79563SJamie Gritton case PRIV_NFS_DAEMON: 30726bb79563SJamie Gritton case PRIV_NFS_LOCKD: 30736bb79563SJamie Gritton #endif 30746bb79563SJamie Gritton /* 30756bb79563SJamie Gritton * Network stack privileges. 30766bb79563SJamie Gritton */ 30776bb79563SJamie Gritton case PRIV_NET_BRIDGE: 30786bb79563SJamie Gritton case PRIV_NET_GRE: 30796bb79563SJamie Gritton case PRIV_NET_BPF: 30806bb79563SJamie Gritton case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */ 30816bb79563SJamie Gritton case PRIV_NET_ROUTE: 30826bb79563SJamie Gritton case PRIV_NET_TAP: 30836bb79563SJamie Gritton case PRIV_NET_SETIFMTU: 30846bb79563SJamie Gritton case PRIV_NET_SETIFFLAGS: 30856bb79563SJamie Gritton case PRIV_NET_SETIFCAP: 3086215940b3SXin LI case PRIV_NET_SETIFDESCR: 30876bb79563SJamie Gritton case PRIV_NET_SETIFNAME : 30886bb79563SJamie Gritton case PRIV_NET_SETIFMETRIC: 30896bb79563SJamie Gritton case PRIV_NET_SETIFPHYS: 30906bb79563SJamie Gritton case PRIV_NET_SETIFMAC: 3091389474c1SKonstantin Belousov case PRIV_NET_SETLANPCP: 30926bb79563SJamie Gritton case PRIV_NET_ADDMULTI: 30936bb79563SJamie Gritton case PRIV_NET_DELMULTI: 30946bb79563SJamie Gritton case PRIV_NET_HWIOCTL: 30956bb79563SJamie Gritton case PRIV_NET_SETLLADDR: 30966bb79563SJamie Gritton case PRIV_NET_ADDIFGROUP: 30976bb79563SJamie Gritton case PRIV_NET_DELIFGROUP: 30986bb79563SJamie Gritton case PRIV_NET_IFCREATE: 30996bb79563SJamie Gritton case PRIV_NET_IFDESTROY: 31006bb79563SJamie Gritton case PRIV_NET_ADDIFADDR: 31016bb79563SJamie Gritton case PRIV_NET_DELIFADDR: 31026bb79563SJamie Gritton case PRIV_NET_LAGG: 31036bb79563SJamie Gritton case PRIV_NET_GIF: 31046bb79563SJamie Gritton case PRIV_NET_SETIFVNET: 310535fd7bc0SBjoern A. Zeeb case PRIV_NET_SETIFFIB: 31066bb79563SJamie Gritton 31076bb79563SJamie Gritton /* 31086bb79563SJamie Gritton * 802.11-related privileges. 31096bb79563SJamie Gritton */ 3110*f7d38a13SAdrian Chadd case PRIV_NET80211_VAP_GETKEY: 3111*f7d38a13SAdrian Chadd case PRIV_NET80211_VAP_MANAGE: 31126bb79563SJamie Gritton 31136bb79563SJamie Gritton #ifdef notyet 31146bb79563SJamie Gritton /* 31156bb79563SJamie Gritton * ATM privileges. 31166bb79563SJamie Gritton */ 31176bb79563SJamie Gritton case PRIV_NETATM_CFG: 31186bb79563SJamie Gritton case PRIV_NETATM_ADD: 31196bb79563SJamie Gritton case PRIV_NETATM_DEL: 31206bb79563SJamie Gritton case PRIV_NETATM_SET: 31216bb79563SJamie Gritton 31226bb79563SJamie Gritton /* 31236bb79563SJamie Gritton * Bluetooth privileges. 31246bb79563SJamie Gritton */ 31256bb79563SJamie Gritton case PRIV_NETBLUETOOTH_RAW: 31266bb79563SJamie Gritton #endif 31276bb79563SJamie Gritton 31286bb79563SJamie Gritton /* 31296bb79563SJamie Gritton * Netgraph and netgraph module privileges. 31306bb79563SJamie Gritton */ 31316bb79563SJamie Gritton case PRIV_NETGRAPH_CONTROL: 31326bb79563SJamie Gritton #ifdef notyet 31336bb79563SJamie Gritton case PRIV_NETGRAPH_TTY: 31346bb79563SJamie Gritton #endif 31356bb79563SJamie Gritton 31366bb79563SJamie Gritton /* 31376bb79563SJamie Gritton * IPv4 and IPv6 privileges. 31386bb79563SJamie Gritton */ 31396bb79563SJamie Gritton case PRIV_NETINET_IPFW: 31406bb79563SJamie Gritton case PRIV_NETINET_DIVERT: 31416bb79563SJamie Gritton case PRIV_NETINET_PF: 31426bb79563SJamie Gritton case PRIV_NETINET_DUMMYNET: 31436bb79563SJamie Gritton case PRIV_NETINET_CARP: 31446bb79563SJamie Gritton case PRIV_NETINET_MROUTE: 31456bb79563SJamie Gritton case PRIV_NETINET_RAW: 31466bb79563SJamie Gritton case PRIV_NETINET_ADDRCTRL6: 31476bb79563SJamie Gritton case PRIV_NETINET_ND6: 31486bb79563SJamie Gritton case PRIV_NETINET_SCOPE6: 31496bb79563SJamie Gritton case PRIV_NETINET_ALIFETIME6: 31506bb79563SJamie Gritton case PRIV_NETINET_IPSEC: 31516bb79563SJamie Gritton case PRIV_NETINET_BINDANY: 31526bb79563SJamie Gritton 31536bb79563SJamie Gritton #ifdef notyet 31546bb79563SJamie Gritton /* 31556bb79563SJamie Gritton * NCP privileges. 31566bb79563SJamie Gritton */ 31576bb79563SJamie Gritton case PRIV_NETNCP: 31586bb79563SJamie Gritton 31596bb79563SJamie Gritton /* 31606bb79563SJamie Gritton * SMB privileges. 31616bb79563SJamie Gritton */ 31626bb79563SJamie Gritton case PRIV_NETSMB: 31636bb79563SJamie Gritton #endif 31646bb79563SJamie Gritton 31656bb79563SJamie Gritton /* 31666bb79563SJamie Gritton * No default: or deny here. 31676bb79563SJamie Gritton * In case of no permit fall through to next switch(). 31686bb79563SJamie Gritton */ 31696bb79563SJamie Gritton if (cred->cr_prison->pr_flags & PR_VNET) 31706bb79563SJamie Gritton return (0); 31716bb79563SJamie Gritton } 31726bb79563SJamie Gritton #endif /* VIMAGE */ 31736bb79563SJamie Gritton 3174800c9408SRobert Watson switch (priv) { 3175800c9408SRobert Watson 3176800c9408SRobert Watson /* 3177800c9408SRobert Watson * Allow ktrace privileges for root in jail. 3178800c9408SRobert Watson */ 3179800c9408SRobert Watson case PRIV_KTRACE: 3180800c9408SRobert Watson 3181c3c1b5e6SRobert Watson #if 0 3182800c9408SRobert Watson /* 3183800c9408SRobert Watson * Allow jailed processes to configure audit identity and 3184800c9408SRobert Watson * submit audit records (login, etc). In the future we may 3185800c9408SRobert Watson * want to further refine the relationship between audit and 3186800c9408SRobert Watson * jail. 3187800c9408SRobert Watson */ 3188800c9408SRobert Watson case PRIV_AUDIT_GETAUDIT: 3189800c9408SRobert Watson case PRIV_AUDIT_SETAUDIT: 3190800c9408SRobert Watson case PRIV_AUDIT_SUBMIT: 3191c3c1b5e6SRobert Watson #endif 3192800c9408SRobert Watson 3193800c9408SRobert Watson /* 3194800c9408SRobert Watson * Allow jailed processes to manipulate process UNIX 3195800c9408SRobert Watson * credentials in any way they see fit. 3196800c9408SRobert Watson */ 3197800c9408SRobert Watson case PRIV_CRED_SETUID: 3198800c9408SRobert Watson case PRIV_CRED_SETEUID: 3199800c9408SRobert Watson case PRIV_CRED_SETGID: 3200800c9408SRobert Watson case PRIV_CRED_SETEGID: 3201800c9408SRobert Watson case PRIV_CRED_SETGROUPS: 3202800c9408SRobert Watson case PRIV_CRED_SETREUID: 3203800c9408SRobert Watson case PRIV_CRED_SETREGID: 3204800c9408SRobert Watson case PRIV_CRED_SETRESUID: 3205800c9408SRobert Watson case PRIV_CRED_SETRESGID: 3206800c9408SRobert Watson 3207800c9408SRobert Watson /* 3208800c9408SRobert Watson * Jail implements visibility constraints already, so allow 3209800c9408SRobert Watson * jailed root to override uid/gid-based constraints. 3210800c9408SRobert Watson */ 3211800c9408SRobert Watson case PRIV_SEEOTHERGIDS: 3212800c9408SRobert Watson case PRIV_SEEOTHERUIDS: 3213800c9408SRobert Watson 3214800c9408SRobert Watson /* 3215800c9408SRobert Watson * Jail implements inter-process debugging limits already, so 3216800c9408SRobert Watson * allow jailed root various debugging privileges. 3217800c9408SRobert Watson */ 3218800c9408SRobert Watson case PRIV_DEBUG_DIFFCRED: 3219800c9408SRobert Watson case PRIV_DEBUG_SUGID: 3220800c9408SRobert Watson case PRIV_DEBUG_UNPRIV: 3221800c9408SRobert Watson 3222800c9408SRobert Watson /* 3223800c9408SRobert Watson * Allow jail to set various resource limits and login 3224800c9408SRobert Watson * properties, and for now, exceed process resource limits. 3225800c9408SRobert Watson */ 3226800c9408SRobert Watson case PRIV_PROC_LIMIT: 3227800c9408SRobert Watson case PRIV_PROC_SETLOGIN: 3228800c9408SRobert Watson case PRIV_PROC_SETRLIMIT: 3229800c9408SRobert Watson 3230800c9408SRobert Watson /* 3231800c9408SRobert Watson * System V and POSIX IPC privileges are granted in jail. 3232800c9408SRobert Watson */ 3233800c9408SRobert Watson case PRIV_IPC_READ: 3234800c9408SRobert Watson case PRIV_IPC_WRITE: 3235800c9408SRobert Watson case PRIV_IPC_ADMIN: 3236800c9408SRobert Watson case PRIV_IPC_MSGSIZE: 3237800c9408SRobert Watson case PRIV_MQ_ADMIN: 3238800c9408SRobert Watson 3239800c9408SRobert Watson /* 32400304c731SJamie Gritton * Jail operations within a jail work on child jails. 32410304c731SJamie Gritton */ 32420304c731SJamie Gritton case PRIV_JAIL_ATTACH: 32430304c731SJamie Gritton case PRIV_JAIL_SET: 32440304c731SJamie Gritton case PRIV_JAIL_REMOVE: 32450304c731SJamie Gritton 32460304c731SJamie Gritton /* 3247800c9408SRobert Watson * Jail implements its own inter-process limits, so allow 3248800c9408SRobert Watson * root processes in jail to change scheduling on other 3249800c9408SRobert Watson * processes in the same jail. Likewise for signalling. 3250800c9408SRobert Watson */ 3251800c9408SRobert Watson case PRIV_SCHED_DIFFCRED: 3252413628a7SBjoern A. Zeeb case PRIV_SCHED_CPUSET: 3253800c9408SRobert Watson case PRIV_SIGNAL_DIFFCRED: 3254800c9408SRobert Watson case PRIV_SIGNAL_SUGID: 3255800c9408SRobert Watson 3256800c9408SRobert Watson /* 3257800c9408SRobert Watson * Allow jailed processes to write to sysctls marked as jail 3258800c9408SRobert Watson * writable. 3259800c9408SRobert Watson */ 3260800c9408SRobert Watson case PRIV_SYSCTL_WRITEJAIL: 3261800c9408SRobert Watson 3262800c9408SRobert Watson /* 3263800c9408SRobert Watson * Allow root in jail to manage a variety of quota 3264e82d0201SRobert Watson * properties. These should likely be conditional on a 3265e82d0201SRobert Watson * configuration option. 3266800c9408SRobert Watson */ 326795b091d2SRobert Watson case PRIV_VFS_GETQUOTA: 326895b091d2SRobert Watson case PRIV_VFS_SETQUOTA: 3269800c9408SRobert Watson 3270800c9408SRobert Watson /* 3271800c9408SRobert Watson * Since Jail relies on chroot() to implement file system 3272800c9408SRobert Watson * protections, grant many VFS privileges to root in jail. 3273800c9408SRobert Watson * Be careful to exclude mount-related and NFS-related 3274800c9408SRobert Watson * privileges. 3275800c9408SRobert Watson */ 3276800c9408SRobert Watson case PRIV_VFS_READ: 3277800c9408SRobert Watson case PRIV_VFS_WRITE: 3278800c9408SRobert Watson case PRIV_VFS_ADMIN: 3279800c9408SRobert Watson case PRIV_VFS_EXEC: 3280800c9408SRobert Watson case PRIV_VFS_LOOKUP: 3281800c9408SRobert Watson case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */ 3282800c9408SRobert Watson case PRIV_VFS_CHFLAGS_DEV: 3283800c9408SRobert Watson case PRIV_VFS_CHOWN: 3284800c9408SRobert Watson case PRIV_VFS_CHROOT: 3285bb531912SPawel Jakub Dawidek case PRIV_VFS_RETAINSUGID: 3286800c9408SRobert Watson case PRIV_VFS_FCHROOT: 3287800c9408SRobert Watson case PRIV_VFS_LINK: 3288800c9408SRobert Watson case PRIV_VFS_SETGID: 3289e41966dcSRobert Watson case PRIV_VFS_STAT: 3290800c9408SRobert Watson case PRIV_VFS_STICKYFILE: 3291bb56d716SJamie Gritton 3292bb56d716SJamie Gritton /* 3293bb56d716SJamie Gritton * As in the non-jail case, non-root users are expected to be 3294bb56d716SJamie Gritton * able to read kernel/phyiscal memory (provided /dev/[k]mem 3295bb56d716SJamie Gritton * exists in the jail and they have permission to access it). 3296bb56d716SJamie Gritton */ 3297bb56d716SJamie Gritton case PRIV_KMEM_READ: 3298800c9408SRobert Watson return (0); 3299800c9408SRobert Watson 3300800c9408SRobert Watson /* 3301800c9408SRobert Watson * Depending on the global setting, allow privilege of 3302800c9408SRobert Watson * setting system flags. 3303800c9408SRobert Watson */ 3304800c9408SRobert Watson case PRIV_VFS_SYSFLAGS: 33050304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS) 3306800c9408SRobert Watson return (0); 3307800c9408SRobert Watson else 3308800c9408SRobert Watson return (EPERM); 3309800c9408SRobert Watson 3310800c9408SRobert Watson /* 3311f3a8d2f9SPawel Jakub Dawidek * Depending on the global setting, allow privilege of 3312f3a8d2f9SPawel Jakub Dawidek * mounting/unmounting file systems. 3313f3a8d2f9SPawel Jakub Dawidek */ 3314f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT: 3315f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_UNMOUNT: 3316f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT_NONUSER: 331724b0502eSPawel Jakub Dawidek case PRIV_VFS_MOUNT_OWNER: 3318435d4667SMartin Matuska if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT && 3319435d4667SMartin Matuska cred->cr_prison->pr_enforce_statfs < 2) 3320f3a8d2f9SPawel Jakub Dawidek return (0); 3321f3a8d2f9SPawel Jakub Dawidek else 3322f3a8d2f9SPawel Jakub Dawidek return (EPERM); 3323f3a8d2f9SPawel Jakub Dawidek 3324f3a8d2f9SPawel Jakub Dawidek /* 332563619b6dSKyle Evans * Jails should hold no disposition on the PRIV_VFS_READ_DIR 332663619b6dSKyle Evans * policy. priv_check_cred will not specifically allow it, and 332763619b6dSKyle Evans * we may want a MAC policy to allow it. 332863619b6dSKyle Evans */ 332963619b6dSKyle Evans case PRIV_VFS_READ_DIR: 333063619b6dSKyle Evans return (0); 333163619b6dSKyle Evans 333263619b6dSKyle Evans /* 3333ccd6ac9fSAntoine Brodin * Conditionnaly allow locking (unlocking) physical pages 3334ccd6ac9fSAntoine Brodin * in memory. 3335ccd6ac9fSAntoine Brodin */ 3336ccd6ac9fSAntoine Brodin case PRIV_VM_MLOCK: 3337ccd6ac9fSAntoine Brodin case PRIV_VM_MUNLOCK: 3338ccd6ac9fSAntoine Brodin if (cred->cr_prison->pr_allow & PR_ALLOW_MLOCK) 3339ccd6ac9fSAntoine Brodin return (0); 3340ccd6ac9fSAntoine Brodin else 3341ccd6ac9fSAntoine Brodin return (EPERM); 3342ccd6ac9fSAntoine Brodin 3343ccd6ac9fSAntoine Brodin /* 3344e28f9b7dSAllan Jude * Conditionally allow jailed root to bind reserved ports. 3345800c9408SRobert Watson */ 3346800c9408SRobert Watson case PRIV_NETINET_RESERVEDPORT: 3347e28f9b7dSAllan Jude if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS) 3348e28f9b7dSAllan Jude return (0); 3349e28f9b7dSAllan Jude else 3350e28f9b7dSAllan Jude return (EPERM); 3351e28f9b7dSAllan Jude 3352e28f9b7dSAllan Jude /* 3353e28f9b7dSAllan Jude * Allow jailed root to reuse in-use ports. 3354e28f9b7dSAllan Jude */ 33554b084056SRobert Watson case PRIV_NETINET_REUSEPORT: 3356800c9408SRobert Watson return (0); 3357800c9408SRobert Watson 3358800c9408SRobert Watson /* 3359e3043798SPedro F. Giffuni * Allow jailed root to set certain IPv4/6 (option) headers. 336079ba3952SBjoern A. Zeeb */ 336179ba3952SBjoern A. Zeeb case PRIV_NETINET_SETHDROPTS: 336279ba3952SBjoern A. Zeeb return (0); 336379ba3952SBjoern A. Zeeb 336479ba3952SBjoern A. Zeeb /* 3365800c9408SRobert Watson * Conditionally allow creating raw sockets in jail. 3366800c9408SRobert Watson */ 3367800c9408SRobert Watson case PRIV_NETINET_RAW: 33680304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS) 3369800c9408SRobert Watson return (0); 3370800c9408SRobert Watson else 3371800c9408SRobert Watson return (EPERM); 3372800c9408SRobert Watson 3373800c9408SRobert Watson /* 3374800c9408SRobert Watson * Since jail implements its own visibility limits on netstat 3375800c9408SRobert Watson * sysctls, allow getcred. This allows identd to work in 3376800c9408SRobert Watson * jail. 3377800c9408SRobert Watson */ 3378800c9408SRobert Watson case PRIV_NETINET_GETCRED: 3379800c9408SRobert Watson return (0); 3380800c9408SRobert Watson 33812bfc50bcSEdward Tomasz Napierala /* 33822bfc50bcSEdward Tomasz Napierala * Allow jailed root to set loginclass. 33832bfc50bcSEdward Tomasz Napierala */ 33842bfc50bcSEdward Tomasz Napierala case PRIV_PROC_SETLOGINCLASS: 33852bfc50bcSEdward Tomasz Napierala return (0); 33862bfc50bcSEdward Tomasz Napierala 3387b19d66fdSJamie Gritton /* 33884520f617SJamie Gritton * Do not allow a process inside a jail to read the kernel 3389b19d66fdSJamie Gritton * message buffer unless explicitly permitted. 3390b19d66fdSJamie Gritton */ 3391b19d66fdSJamie Gritton case PRIV_MSGBUF: 3392b19d66fdSJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_READ_MSGBUF) 3393b19d66fdSJamie Gritton return (0); 3394b19d66fdSJamie Gritton return (EPERM); 3395b19d66fdSJamie Gritton 3396800c9408SRobert Watson default: 3397800c9408SRobert Watson /* 3398800c9408SRobert Watson * In all remaining cases, deny the privilege request. This 3399800c9408SRobert Watson * includes almost all network privileges, many system 3400800c9408SRobert Watson * configuration privileges. 3401800c9408SRobert Watson */ 3402800c9408SRobert Watson return (EPERM); 3403800c9408SRobert Watson } 3404800c9408SRobert Watson } 3405800c9408SRobert Watson 34060304c731SJamie Gritton /* 34070304c731SJamie Gritton * Return the part of pr2's name that is relative to pr1, or the whole name 34080304c731SJamie Gritton * if it does not directly follow. 34090304c731SJamie Gritton */ 34100304c731SJamie Gritton 34110304c731SJamie Gritton char * 34120304c731SJamie Gritton prison_name(struct prison *pr1, struct prison *pr2) 34130304c731SJamie Gritton { 34140304c731SJamie Gritton char *name; 34150304c731SJamie Gritton 34160304c731SJamie Gritton /* Jails see themselves as "0" (if they see themselves at all). */ 34170304c731SJamie Gritton if (pr1 == pr2) 34180304c731SJamie Gritton return "0"; 34190304c731SJamie Gritton name = pr2->pr_name; 34200304c731SJamie Gritton if (prison_ischild(pr1, pr2)) { 34210304c731SJamie Gritton /* 34220304c731SJamie Gritton * pr1 isn't locked (and allprison_lock may not be either) 34230304c731SJamie Gritton * so its length can't be counted on. But the number of dots 34240304c731SJamie Gritton * can be counted on - and counted. 34250304c731SJamie Gritton */ 34260304c731SJamie Gritton for (; pr1 != &prison0; pr1 = pr1->pr_parent) 34270304c731SJamie Gritton name = strchr(name, '.') + 1; 34280304c731SJamie Gritton } 34290304c731SJamie Gritton return (name); 34300304c731SJamie Gritton } 34310304c731SJamie Gritton 34320304c731SJamie Gritton /* 34330304c731SJamie Gritton * Return the part of pr2's path that is relative to pr1, or the whole path 34340304c731SJamie Gritton * if it does not directly follow. 34350304c731SJamie Gritton */ 34360304c731SJamie Gritton static char * 34370304c731SJamie Gritton prison_path(struct prison *pr1, struct prison *pr2) 34380304c731SJamie Gritton { 34390304c731SJamie Gritton char *path1, *path2; 34400304c731SJamie Gritton int len1; 34410304c731SJamie Gritton 34420304c731SJamie Gritton path1 = pr1->pr_path; 34430304c731SJamie Gritton path2 = pr2->pr_path; 34440304c731SJamie Gritton if (!strcmp(path1, "/")) 34450304c731SJamie Gritton return (path2); 34460304c731SJamie Gritton len1 = strlen(path1); 34470304c731SJamie Gritton if (strncmp(path1, path2, len1)) 34480304c731SJamie Gritton return (path2); 34490304c731SJamie Gritton if (path2[len1] == '\0') 34500304c731SJamie Gritton return "/"; 34510304c731SJamie Gritton if (path2[len1] == '/') 34520304c731SJamie Gritton return (path2 + len1); 34530304c731SJamie Gritton return (path2); 34540304c731SJamie Gritton } 34550304c731SJamie Gritton 34560304c731SJamie Gritton /* 34570304c731SJamie Gritton * Jail-related sysctls. 34580304c731SJamie Gritton */ 34597029da5cSPawel Biernacki static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 34600304c731SJamie Gritton "Jails"); 34610304c731SJamie Gritton 3462fd7a8150SMike Barcroft static int 3463fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS) 3464fd7a8150SMike Barcroft { 3465b38ff370SJamie Gritton struct xprison *xp; 34660304c731SJamie Gritton struct prison *pr, *cpr; 3467b38ff370SJamie Gritton #ifdef INET 3468b38ff370SJamie Gritton struct in_addr *ip4 = NULL; 3469b38ff370SJamie Gritton int ip4s = 0; 3470b38ff370SJamie Gritton #endif 3471b38ff370SJamie Gritton #ifdef INET6 34723beefaedSColin Percival struct in6_addr *ip6 = NULL; 3473b38ff370SJamie Gritton int ip6s = 0; 3474b38ff370SJamie Gritton #endif 34750304c731SJamie Gritton int descend, error; 3476fd7a8150SMike Barcroft 3477b38ff370SJamie Gritton xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK); 34780304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 3479b38ff370SJamie Gritton error = 0; 3480dc68a633SPawel Jakub Dawidek sx_slock(&allprison_lock); 34810304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 34820304c731SJamie Gritton #if defined(INET) || defined(INET6) 3483b38ff370SJamie Gritton again: 34840304c731SJamie Gritton #endif 34850304c731SJamie Gritton mtx_lock(&cpr->pr_mtx); 3486413628a7SBjoern A. Zeeb #ifdef INET 34870304c731SJamie Gritton if (cpr->pr_ip4s > 0) { 34880304c731SJamie Gritton if (ip4s < cpr->pr_ip4s) { 34890304c731SJamie Gritton ip4s = cpr->pr_ip4s; 34900304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 3491b38ff370SJamie Gritton ip4 = realloc(ip4, ip4s * 3492b38ff370SJamie Gritton sizeof(struct in_addr), M_TEMP, M_WAITOK); 3493b38ff370SJamie Gritton goto again; 3494b38ff370SJamie Gritton } 34950304c731SJamie Gritton bcopy(cpr->pr_ip4, ip4, 34960304c731SJamie Gritton cpr->pr_ip4s * sizeof(struct in_addr)); 3497b38ff370SJamie Gritton } 3498413628a7SBjoern A. Zeeb #endif 3499413628a7SBjoern A. Zeeb #ifdef INET6 35000304c731SJamie Gritton if (cpr->pr_ip6s > 0) { 35010304c731SJamie Gritton if (ip6s < cpr->pr_ip6s) { 35020304c731SJamie Gritton ip6s = cpr->pr_ip6s; 35030304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 3504b38ff370SJamie Gritton ip6 = realloc(ip6, ip6s * 3505b38ff370SJamie Gritton sizeof(struct in6_addr), M_TEMP, M_WAITOK); 3506b38ff370SJamie Gritton goto again; 3507413628a7SBjoern A. Zeeb } 35080304c731SJamie Gritton bcopy(cpr->pr_ip6, ip6, 35090304c731SJamie Gritton cpr->pr_ip6s * sizeof(struct in6_addr)); 3510b38ff370SJamie Gritton } 3511b38ff370SJamie Gritton #endif 35120304c731SJamie Gritton if (cpr->pr_ref == 0) { 35130304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 3514b38ff370SJamie Gritton continue; 3515b38ff370SJamie Gritton } 3516b38ff370SJamie Gritton bzero(xp, sizeof(*xp)); 3517fd7a8150SMike Barcroft xp->pr_version = XPRISON_VERSION; 35180304c731SJamie Gritton xp->pr_id = cpr->pr_id; 35190304c731SJamie Gritton xp->pr_state = cpr->pr_uref > 0 3520b38ff370SJamie Gritton ? PRISON_STATE_ALIVE : PRISON_STATE_DYING; 35210304c731SJamie Gritton strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path)); 3522c1f19219SJamie Gritton strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host)); 35230304c731SJamie Gritton strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name)); 3524413628a7SBjoern A. Zeeb #ifdef INET 35250304c731SJamie Gritton xp->pr_ip4s = cpr->pr_ip4s; 3526413628a7SBjoern A. Zeeb #endif 3527413628a7SBjoern A. Zeeb #ifdef INET6 35280304c731SJamie Gritton xp->pr_ip6s = cpr->pr_ip6s; 3529413628a7SBjoern A. Zeeb #endif 35300304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 3531b38ff370SJamie Gritton error = SYSCTL_OUT(req, xp, sizeof(*xp)); 3532b38ff370SJamie Gritton if (error) 3533b38ff370SJamie Gritton break; 3534413628a7SBjoern A. Zeeb #ifdef INET 3535b38ff370SJamie Gritton if (xp->pr_ip4s > 0) { 3536b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip4, 3537b38ff370SJamie Gritton xp->pr_ip4s * sizeof(struct in_addr)); 3538b38ff370SJamie Gritton if (error) 3539b38ff370SJamie Gritton break; 3540413628a7SBjoern A. Zeeb } 3541413628a7SBjoern A. Zeeb #endif 3542413628a7SBjoern A. Zeeb #ifdef INET6 3543b38ff370SJamie Gritton if (xp->pr_ip6s > 0) { 3544b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip6, 3545b38ff370SJamie Gritton xp->pr_ip6s * sizeof(struct in6_addr)); 3546b38ff370SJamie Gritton if (error) 3547b38ff370SJamie Gritton break; 3548413628a7SBjoern A. Zeeb } 3549413628a7SBjoern A. Zeeb #endif 3550fd7a8150SMike Barcroft } 3551dc68a633SPawel Jakub Dawidek sx_sunlock(&allprison_lock); 3552b38ff370SJamie Gritton free(xp, M_TEMP); 3553b38ff370SJamie Gritton #ifdef INET 3554b38ff370SJamie Gritton free(ip4, M_TEMP); 3555b38ff370SJamie Gritton #endif 3556b38ff370SJamie Gritton #ifdef INET6 3557b38ff370SJamie Gritton free(ip6, M_TEMP); 3558b38ff370SJamie Gritton #endif 3559fd7a8150SMike Barcroft return (error); 3560fd7a8150SMike Barcroft } 3561fd7a8150SMike Barcroft 3562f3b86a5fSEd Schouten SYSCTL_OID(_security_jail, OID_AUTO, list, 3563f3b86a5fSEd Schouten CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 3564f3b86a5fSEd Schouten sysctl_jail_list, "S", "List of active jails"); 3565461167c2SPawel Jakub Dawidek 3566461167c2SPawel Jakub Dawidek static int 3567461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS) 3568461167c2SPawel Jakub Dawidek { 3569461167c2SPawel Jakub Dawidek int error, injail; 3570461167c2SPawel Jakub Dawidek 3571461167c2SPawel Jakub Dawidek injail = jailed(req->td->td_ucred); 3572461167c2SPawel Jakub Dawidek error = SYSCTL_OUT(req, &injail, sizeof(injail)); 3573461167c2SPawel Jakub Dawidek 3574461167c2SPawel Jakub Dawidek return (error); 3575461167c2SPawel Jakub Dawidek } 35760304c731SJamie Gritton 3577f3b86a5fSEd Schouten SYSCTL_PROC(_security_jail, OID_AUTO, jailed, 3578f3b86a5fSEd Schouten CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 3579f3b86a5fSEd Schouten sysctl_jail_jailed, "I", "Process in jail?"); 3580413628a7SBjoern A. Zeeb 3581761d2bb5SJamie Gritton static int 3582761d2bb5SJamie Gritton sysctl_jail_vnet(SYSCTL_HANDLER_ARGS) 3583761d2bb5SJamie Gritton { 3584761d2bb5SJamie Gritton int error, havevnet; 3585761d2bb5SJamie Gritton #ifdef VIMAGE 3586761d2bb5SJamie Gritton struct ucred *cred = req->td->td_ucred; 3587761d2bb5SJamie Gritton 3588761d2bb5SJamie Gritton havevnet = jailed(cred) && prison_owns_vnet(cred); 3589761d2bb5SJamie Gritton #else 3590761d2bb5SJamie Gritton havevnet = 0; 3591761d2bb5SJamie Gritton #endif 3592761d2bb5SJamie Gritton error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet)); 3593761d2bb5SJamie Gritton 3594761d2bb5SJamie Gritton return (error); 3595761d2bb5SJamie Gritton } 3596761d2bb5SJamie Gritton 3597761d2bb5SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, vnet, 3598761d2bb5SJamie Gritton CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 3599bb8f1623SBjoern A. Zeeb sysctl_jail_vnet, "I", "Jail owns vnet?"); 3600761d2bb5SJamie Gritton 36010304c731SJamie Gritton #if defined(INET) || defined(INET6) 3602e92e0574SJamie Gritton SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW, 36030304c731SJamie Gritton &jail_max_af_ips, 0, 3604c542c43eSJamie Gritton "Number of IP addresses a jail may have at most per address family (deprecated)"); 36050304c731SJamie Gritton #endif 36060304c731SJamie Gritton 36070304c731SJamie Gritton /* 3608c542c43eSJamie Gritton * Default parameters for jail(2) compatibility. For historical reasons, 3609c542c43eSJamie Gritton * the sysctl names have varying similarity to the parameter names. Prisons 3610c542c43eSJamie Gritton * just see their own parameters, and can't change them. 36110304c731SJamie Gritton */ 36120304c731SJamie Gritton static int 36130304c731SJamie Gritton sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS) 36140304c731SJamie Gritton { 36150304c731SJamie Gritton struct prison *pr; 36160304c731SJamie Gritton int allow, error, i; 36170304c731SJamie Gritton 36180304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 36190304c731SJamie Gritton allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow; 36200304c731SJamie Gritton 36210304c731SJamie Gritton /* Get the current flag value, and convert it to a boolean. */ 36220304c731SJamie Gritton i = (allow & arg2) ? 1 : 0; 36230304c731SJamie Gritton if (arg1 != NULL) 36240304c731SJamie Gritton i = !i; 36250304c731SJamie Gritton error = sysctl_handle_int(oidp, &i, 0, req); 3626c542c43eSJamie Gritton if (error || !req->newptr) 36270304c731SJamie Gritton return (error); 36280304c731SJamie Gritton i = i ? arg2 : 0; 36290304c731SJamie Gritton if (arg1 != NULL) 36300304c731SJamie Gritton i ^= arg2; 36310304c731SJamie Gritton /* 36320304c731SJamie Gritton * The sysctls don't have CTLFLAGS_PRISON, so assume prison0 36330304c731SJamie Gritton * for writing. 36340304c731SJamie Gritton */ 36350304c731SJamie Gritton mtx_lock(&prison0.pr_mtx); 36360304c731SJamie Gritton jail_default_allow = (jail_default_allow & ~arg2) | i; 36370304c731SJamie Gritton mtx_unlock(&prison0.pr_mtx); 36380304c731SJamie Gritton return (0); 36390304c731SJamie Gritton } 36400304c731SJamie Gritton 36410304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed, 3642c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 36430304c731SJamie Gritton NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I", 3644c542c43eSJamie Gritton "Processes in jail can set their hostnames (deprecated)"); 36450304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only, 3646c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 36470304c731SJamie Gritton (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I", 3648c542c43eSJamie Gritton "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)"); 36490304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed, 3650c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 36510304c731SJamie Gritton NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I", 3652c542c43eSJamie Gritton "Processes in jail can use System V IPC primitives (deprecated)"); 36530304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets, 3654c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 36550304c731SJamie Gritton NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I", 3656c542c43eSJamie Gritton "Prison root can create raw sockets (deprecated)"); 36570304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed, 3658c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 36590304c731SJamie Gritton NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I", 3660c542c43eSJamie Gritton "Processes in jail can alter system file flags (deprecated)"); 36610304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed, 3662c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 36630304c731SJamie Gritton NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I", 3664c542c43eSJamie Gritton "Processes in jail can mount/unmount jail-friendly file systems (deprecated)"); 36650304c731SJamie Gritton 36660304c731SJamie Gritton static int 36670304c731SJamie Gritton sysctl_jail_default_level(SYSCTL_HANDLER_ARGS) 36680304c731SJamie Gritton { 36690304c731SJamie Gritton struct prison *pr; 36700304c731SJamie Gritton int level, error; 36710304c731SJamie Gritton 36720304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 36730304c731SJamie Gritton level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2); 36740304c731SJamie Gritton error = sysctl_handle_int(oidp, &level, 0, req); 3675c542c43eSJamie Gritton if (error || !req->newptr) 36760304c731SJamie Gritton return (error); 36770304c731SJamie Gritton *(int *)arg1 = level; 36780304c731SJamie Gritton return (0); 36790304c731SJamie Gritton } 36800304c731SJamie Gritton 36810304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs, 3682c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 3683c542c43eSJamie Gritton &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs), 36840304c731SJamie Gritton sysctl_jail_default_level, "I", 3685c542c43eSJamie Gritton "Processes in jail cannot see all mounted file systems (deprecated)"); 3686c542c43eSJamie Gritton 36870cc207a6SMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset, 3688c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 3689c542c43eSJamie Gritton &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum), 36900cc207a6SMartin Matuska sysctl_jail_default_level, "I", 3691c542c43eSJamie Gritton "Ruleset for the devfs filesystem in jail (deprecated)"); 36920cc207a6SMartin Matuska 36930304c731SJamie Gritton /* 36940304c731SJamie Gritton * Nodes to describe jail parameters. Maximum length of string parameters 36950304c731SJamie Gritton * is returned in the string itself, and the other parameters exist merely 36960304c731SJamie Gritton * to make themselves and their types known. 36970304c731SJamie Gritton */ 36987029da5cSPawel Biernacki SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 36990304c731SJamie Gritton "Jail parameters"); 37000304c731SJamie Gritton 37010304c731SJamie Gritton int 37020304c731SJamie Gritton sysctl_jail_param(SYSCTL_HANDLER_ARGS) 37030304c731SJamie Gritton { 37040304c731SJamie Gritton int i; 37050304c731SJamie Gritton long l; 37060304c731SJamie Gritton size_t s; 37070304c731SJamie Gritton char numbuf[12]; 37080304c731SJamie Gritton 37090304c731SJamie Gritton switch (oidp->oid_kind & CTLTYPE) 37100304c731SJamie Gritton { 37110304c731SJamie Gritton case CTLTYPE_LONG: 37120304c731SJamie Gritton case CTLTYPE_ULONG: 37130304c731SJamie Gritton l = 0; 37140304c731SJamie Gritton #ifdef SCTL_MASK32 37150304c731SJamie Gritton if (!(req->flags & SCTL_MASK32)) 37160304c731SJamie Gritton #endif 37170304c731SJamie Gritton return (SYSCTL_OUT(req, &l, sizeof(l))); 37180304c731SJamie Gritton case CTLTYPE_INT: 37190304c731SJamie Gritton case CTLTYPE_UINT: 37200304c731SJamie Gritton i = 0; 37210304c731SJamie Gritton return (SYSCTL_OUT(req, &i, sizeof(i))); 37220304c731SJamie Gritton case CTLTYPE_STRING: 3723e4cd31ddSJeff Roberson snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2); 37240304c731SJamie Gritton return 37250304c731SJamie Gritton (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req)); 37260304c731SJamie Gritton case CTLTYPE_STRUCT: 37270304c731SJamie Gritton s = (size_t)arg2; 37280304c731SJamie Gritton return (SYSCTL_OUT(req, &s, sizeof(s))); 37290304c731SJamie Gritton } 37300304c731SJamie Gritton return (0); 37310304c731SJamie Gritton } 37320304c731SJamie Gritton 3733b96bd95bSIan Lepore /* 3734b96bd95bSIan Lepore * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at 3735b96bd95bSIan Lepore * jail creation time but cannot be changed in an existing jail. 3736b96bd95bSIan Lepore */ 37370304c731SJamie Gritton SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID"); 37380304c731SJamie Gritton SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID"); 37390304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name"); 37400304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path"); 37410304c731SJamie Gritton SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW, 37420304c731SJamie Gritton "I", "Jail secure level"); 3743b96bd95bSIan Lepore SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I", 3744b96bd95bSIan Lepore "Jail value for kern.osreldate and uname -K"); 3745b96bd95bSIan Lepore SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN, 3746b96bd95bSIan Lepore "Jail value for kern.osrelease and uname -r"); 37470304c731SJamie Gritton SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW, 37480304c731SJamie Gritton "I", "Jail cannot see all mounted file systems"); 37490cc207a6SMartin Matuska SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW, 37500cc207a6SMartin Matuska "I", "Ruleset for in-jail devfs mounts"); 37510304c731SJamie Gritton SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW, 37520304c731SJamie Gritton "B", "Jail persistence"); 3753679e1390SJamie Gritton #ifdef VIMAGE 3754679e1390SJamie Gritton SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN, 37557cbf7213SJamie Gritton "E,jailsys", "Virtual network stack"); 3756679e1390SJamie Gritton #endif 37570304c731SJamie Gritton SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD, 37580304c731SJamie Gritton "B", "Jail is in the process of shutting down"); 37590304c731SJamie Gritton 3760b97457e2SJamie Gritton SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails"); 3761b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD, 3762b97457e2SJamie Gritton "I", "Current number of child jails"); 3763b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW, 3764b97457e2SJamie Gritton "I", "Maximum number of child jails"); 3765b97457e2SJamie Gritton 37667cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info"); 37670304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN, 37680304c731SJamie Gritton "Jail hostname"); 376976ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN, 377076ca6f88SJamie Gritton "Jail NIS domainname"); 377176ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN, 377276ca6f88SJamie Gritton "Jail host UUID"); 377376ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW, 377476ca6f88SJamie Gritton "LU", "Jail host ID"); 37750304c731SJamie Gritton 37760304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset"); 37770304c731SJamie Gritton SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID"); 37780304c731SJamie Gritton 37790304c731SJamie Gritton #ifdef INET 37802b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN, 37812b0d6f81SJamie Gritton "Jail IPv4 address virtualization"); 37820304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr), 37830304c731SJamie Gritton "S,in_addr,a", "Jail IPv4 addresses"); 3784592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 3785592bcae8SBjoern A. Zeeb "B", "Do (not) use IPv4 source address selection rather than the " 3786592bcae8SBjoern A. Zeeb "primary jail IPv4 address."); 37870304c731SJamie Gritton #endif 37880304c731SJamie Gritton #ifdef INET6 37892b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN, 37902b0d6f81SJamie Gritton "Jail IPv6 address virtualization"); 37910304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr), 37920304c731SJamie Gritton "S,in6_addr,a", "Jail IPv6 addresses"); 3793592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 3794592bcae8SBjoern A. Zeeb "B", "Do (not) use IPv6 source address selection rather than the " 3795592bcae8SBjoern A. Zeeb "primary jail IPv6 address."); 37960304c731SJamie Gritton #endif 37970304c731SJamie Gritton 37980304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags"); 37990304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW, 38000304c731SJamie Gritton "B", "Jail may set hostname"); 38010304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW, 38020304c731SJamie Gritton "B", "Jail may use SYSV IPC"); 38030304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW, 38040304c731SJamie Gritton "B", "Jail may create raw sockets"); 38050304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW, 38060304c731SJamie Gritton "B", "Jail may alter system file flags"); 38070304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW, 38080304c731SJamie Gritton "B", "Jail may set file quotas"); 38090304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW, 38100304c731SJamie Gritton "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route"); 3811ccd6ac9fSAntoine Brodin SYSCTL_JAIL_PARAM(_allow, mlock, CTLTYPE_INT | CTLFLAG_RW, 3812ccd6ac9fSAntoine Brodin "B", "Jail may lock (unlock) physical pages in memory"); 3813e28f9b7dSAllan Jude SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW, 3814e28f9b7dSAllan Jude "B", "Jail may bind sockets to reserved ports"); 3815b19d66fdSJamie Gritton SYSCTL_JAIL_PARAM(_allow, read_msgbuf, CTLTYPE_INT | CTLFLAG_RW, 3816b19d66fdSJamie Gritton "B", "Jail may read the kernel message buffer"); 3817b3079544SJamie Gritton SYSCTL_JAIL_PARAM(_allow, unprivileged_proc_debug, CTLTYPE_INT | CTLFLAG_RW, 3818b3079544SJamie Gritton "B", "Unprivileged processes may use process debugging facilities"); 38190304c731SJamie Gritton 3820bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags"); 3821bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW, 3822bf3db8aaSMartin Matuska "B", "Jail may mount/unmount jail-friendly file systems in general"); 38230e5c6bd4SJamie Gritton 38240e5c6bd4SJamie Gritton /* 38250a172404SJamie Gritton * Add a dynamic parameter allow.<name>, or allow.<prefix>.<name>. Return 38260a172404SJamie Gritton * its associated bit in the pr_allow bitmask, or zero if the parameter was 38270a172404SJamie Gritton * not created. 38280e5c6bd4SJamie Gritton */ 38290a172404SJamie Gritton unsigned 38300a172404SJamie Gritton prison_add_allow(const char *prefix, const char *name, const char *prefix_descr, 38310a172404SJamie Gritton const char *descr) 38320e5c6bd4SJamie Gritton { 38330e5c6bd4SJamie Gritton struct bool_flags *bf; 38340a172404SJamie Gritton struct sysctl_oid *parent; 38350a172404SJamie Gritton char *allow_name, *allow_noname, *allowed; 3836c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR 3837c542c43eSJamie Gritton char *descr_deprecated; 3838c542c43eSJamie Gritton #endif 38390e5c6bd4SJamie Gritton unsigned allow_flag; 38400e5c6bd4SJamie Gritton 38410a172404SJamie Gritton if (prefix 38420a172404SJamie Gritton ? asprintf(&allow_name, M_PRISON, "allow.%s.%s", prefix, name) 38430a172404SJamie Gritton < 0 || 38440a172404SJamie Gritton asprintf(&allow_noname, M_PRISON, "allow.%s.no%s", prefix, name) 38450a172404SJamie Gritton < 0 38460a172404SJamie Gritton : asprintf(&allow_name, M_PRISON, "allow.%s", name) < 0 || 38470a172404SJamie Gritton asprintf(&allow_noname, M_PRISON, "allow.no%s", name) < 0) { 38480e5c6bd4SJamie Gritton free(allow_name, M_PRISON); 38490a172404SJamie Gritton return 0; 38500e5c6bd4SJamie Gritton } 38510e5c6bd4SJamie Gritton 38520e5c6bd4SJamie Gritton /* 38530a172404SJamie Gritton * See if this parameter has already beed added, i.e. a module was 38540a172404SJamie Gritton * previously loaded/unloaded. 38550e5c6bd4SJamie Gritton */ 38560e5c6bd4SJamie Gritton mtx_lock(&prison0.pr_mtx); 38570e5c6bd4SJamie Gritton for (bf = pr_flag_allow; 38580e5c6bd4SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && bf->flag != 0; 38590e5c6bd4SJamie Gritton bf++) { 38600e5c6bd4SJamie Gritton if (strcmp(bf->name, allow_name) == 0) { 38610a172404SJamie Gritton allow_flag = bf->flag; 38620e5c6bd4SJamie Gritton goto no_add; 38630e5c6bd4SJamie Gritton } 38640e5c6bd4SJamie Gritton } 38650e5c6bd4SJamie Gritton 38660e5c6bd4SJamie Gritton /* 38670e5c6bd4SJamie Gritton * Find a free bit in prison0's pr_allow, failing if there are none 38680e5c6bd4SJamie Gritton * (which shouldn't happen as long as we keep track of how many 38690a172404SJamie Gritton * potential dynamic flags exist). 3870b3079544SJamie Gritton * 3871b3079544SJamie Gritton * Due to per-jail unprivileged process debugging support 3872b3079544SJamie Gritton * using pr_allow, also verify against PR_ALLOW_ALL_STATIC. 3873b3079544SJamie Gritton * prison0 may have unprivileged process debugging unset. 38740e5c6bd4SJamie Gritton */ 38750e5c6bd4SJamie Gritton for (allow_flag = 1;; allow_flag <<= 1) { 38760e5c6bd4SJamie Gritton if (allow_flag == 0) 38770e5c6bd4SJamie Gritton goto no_add; 3878b3079544SJamie Gritton if (allow_flag & PR_ALLOW_ALL_STATIC) 3879b3079544SJamie Gritton continue; 38800e5c6bd4SJamie Gritton if ((prison0.pr_allow & allow_flag) == 0) 38810e5c6bd4SJamie Gritton break; 38820e5c6bd4SJamie Gritton } 38830e5c6bd4SJamie Gritton 38840e5c6bd4SJamie Gritton /* 38850e5c6bd4SJamie Gritton * Note the parameter in the next open slot in pr_flag_allow. 38860e5c6bd4SJamie Gritton * Set the flag last so code that checks pr_flag_allow can do so 38870e5c6bd4SJamie Gritton * without locking. 38880e5c6bd4SJamie Gritton */ 38890e5c6bd4SJamie Gritton for (bf = pr_flag_allow; bf->flag != 0; bf++) 38900e5c6bd4SJamie Gritton if (bf == pr_flag_allow + nitems(pr_flag_allow)) { 38910e5c6bd4SJamie Gritton /* This should never happen, but is not fatal. */ 38920a172404SJamie Gritton allow_flag = 0; 38930e5c6bd4SJamie Gritton goto no_add; 38940e5c6bd4SJamie Gritton } 38950e5c6bd4SJamie Gritton prison0.pr_allow |= allow_flag; 38960e5c6bd4SJamie Gritton bf->name = allow_name; 38970e5c6bd4SJamie Gritton bf->noname = allow_noname; 38980e5c6bd4SJamie Gritton bf->flag = allow_flag; 38990e5c6bd4SJamie Gritton mtx_unlock(&prison0.pr_mtx); 39000e5c6bd4SJamie Gritton 3901c542c43eSJamie Gritton /* 3902c542c43eSJamie Gritton * Create sysctls for the paramter, and the back-compat global 3903c542c43eSJamie Gritton * permission. 3904c542c43eSJamie Gritton */ 39050a172404SJamie Gritton parent = prefix 39060a172404SJamie Gritton ? SYSCTL_ADD_NODE(NULL, 39070a172404SJamie Gritton SYSCTL_CHILDREN(&sysctl___security_jail_param_allow), 39087029da5cSPawel Biernacki OID_AUTO, prefix, CTLFLAG_MPSAFE, 0, prefix_descr) 39090a172404SJamie Gritton : &sysctl___security_jail_param_allow; 39100a172404SJamie Gritton (void)SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(parent), OID_AUTO, 39110a172404SJamie Gritton name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 39120e5c6bd4SJamie Gritton NULL, 0, sysctl_jail_param, "B", descr); 39130a172404SJamie Gritton if ((prefix 39140a172404SJamie Gritton ? asprintf(&allowed, M_TEMP, "%s_%s_allowed", prefix, name) 39150a172404SJamie Gritton : asprintf(&allowed, M_TEMP, "%s_allowed", name)) >= 0) { 3916c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR 3917c542c43eSJamie Gritton (void)asprintf(&descr_deprecated, M_TEMP, "%s (deprecated)", 3918c542c43eSJamie Gritton descr); 3919c542c43eSJamie Gritton #endif 39200e5c6bd4SJamie Gritton (void)SYSCTL_ADD_PROC(NULL, 39210a172404SJamie Gritton SYSCTL_CHILDREN(&sysctl___security_jail), OID_AUTO, allowed, 3922c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, allow_flag, 3923c542c43eSJamie Gritton sysctl_jail_default_allow, "I", descr_deprecated); 3924c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR 3925c542c43eSJamie Gritton free(descr_deprecated, M_TEMP); 3926c542c43eSJamie Gritton #endif 39270a172404SJamie Gritton free(allowed, M_TEMP); 39280e5c6bd4SJamie Gritton } 39290a172404SJamie Gritton return allow_flag; 39300e5c6bd4SJamie Gritton 39310e5c6bd4SJamie Gritton no_add: 39320e5c6bd4SJamie Gritton mtx_unlock(&prison0.pr_mtx); 39330e5c6bd4SJamie Gritton free(allow_name, M_PRISON); 39340e5c6bd4SJamie Gritton free(allow_noname, M_PRISON); 39350a172404SJamie Gritton return allow_flag; 39360a172404SJamie Gritton } 39370a172404SJamie Gritton 39380a172404SJamie Gritton /* 39390a172404SJamie Gritton * The VFS system will register jail-aware filesystems here. They each get 39400a172404SJamie Gritton * a parameter allow.mount.xxxfs and a flag to check when a jailed user 39410a172404SJamie Gritton * attempts to mount. 39420a172404SJamie Gritton */ 39430a172404SJamie Gritton void 39440a172404SJamie Gritton prison_add_vfs(struct vfsconf *vfsp) 39450a172404SJamie Gritton { 39460a172404SJamie Gritton #ifdef NO_SYSCTL_DESCR 39470a172404SJamie Gritton 39480a172404SJamie Gritton vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name, 39490a172404SJamie Gritton NULL, NULL); 39500a172404SJamie Gritton #else 39510a172404SJamie Gritton char *descr; 39520a172404SJamie Gritton 39530a172404SJamie Gritton (void)asprintf(&descr, M_TEMP, "Jail may mount the %s file system", 39540a172404SJamie Gritton vfsp->vfc_name); 39550a172404SJamie Gritton vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name, 39560a172404SJamie Gritton NULL, descr); 39570a172404SJamie Gritton free(descr, M_TEMP); 39580a172404SJamie Gritton #endif 39590e5c6bd4SJamie Gritton } 3960bf3db8aaSMartin Matuska 39614b5c9cf6SEdward Tomasz Napierala #ifdef RACCT 3962097055e2SEdward Tomasz Napierala void 3963097055e2SEdward Tomasz Napierala prison_racct_foreach(void (*callback)(struct racct *racct, 396415db3c07SEdward Tomasz Napierala void *arg2, void *arg3), void (*pre)(void), void (*post)(void), 396515db3c07SEdward Tomasz Napierala void *arg2, void *arg3) 3966097055e2SEdward Tomasz Napierala { 3967a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 3968097055e2SEdward Tomasz Napierala 39694b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 39704b5c9cf6SEdward Tomasz Napierala 3971097055e2SEdward Tomasz Napierala sx_slock(&allprison_lock); 397215db3c07SEdward Tomasz Napierala if (pre != NULL) 397315db3c07SEdward Tomasz Napierala (pre)(); 3974a7ad07bfSEdward Tomasz Napierala LIST_FOREACH(prr, &allprison_racct, prr_next) 3975a7ad07bfSEdward Tomasz Napierala (callback)(prr->prr_racct, arg2, arg3); 397615db3c07SEdward Tomasz Napierala if (post != NULL) 397715db3c07SEdward Tomasz Napierala (post)(); 3978097055e2SEdward Tomasz Napierala sx_sunlock(&allprison_lock); 3979097055e2SEdward Tomasz Napierala } 39800304c731SJamie Gritton 3981a7ad07bfSEdward Tomasz Napierala static struct prison_racct * 3982a7ad07bfSEdward Tomasz Napierala prison_racct_find_locked(const char *name) 3983a7ad07bfSEdward Tomasz Napierala { 3984a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 3985a7ad07bfSEdward Tomasz Napierala 39864b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 3987a7ad07bfSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 3988a7ad07bfSEdward Tomasz Napierala 3989a7ad07bfSEdward Tomasz Napierala if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN) 3990a7ad07bfSEdward Tomasz Napierala return (NULL); 3991a7ad07bfSEdward Tomasz Napierala 3992a7ad07bfSEdward Tomasz Napierala LIST_FOREACH(prr, &allprison_racct, prr_next) { 3993a7ad07bfSEdward Tomasz Napierala if (strcmp(name, prr->prr_name) != 0) 3994a7ad07bfSEdward Tomasz Napierala continue; 3995a7ad07bfSEdward Tomasz Napierala 3996a7ad07bfSEdward Tomasz Napierala /* Found prison_racct with a matching name? */ 3997a7ad07bfSEdward Tomasz Napierala prison_racct_hold(prr); 3998a7ad07bfSEdward Tomasz Napierala return (prr); 3999a7ad07bfSEdward Tomasz Napierala } 4000a7ad07bfSEdward Tomasz Napierala 4001a7ad07bfSEdward Tomasz Napierala /* Add new prison_racct. */ 4002a7ad07bfSEdward Tomasz Napierala prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK); 4003a7ad07bfSEdward Tomasz Napierala racct_create(&prr->prr_racct); 4004a7ad07bfSEdward Tomasz Napierala 4005a7ad07bfSEdward Tomasz Napierala strcpy(prr->prr_name, name); 4006a7ad07bfSEdward Tomasz Napierala refcount_init(&prr->prr_refcount, 1); 4007a7ad07bfSEdward Tomasz Napierala LIST_INSERT_HEAD(&allprison_racct, prr, prr_next); 4008a7ad07bfSEdward Tomasz Napierala 4009a7ad07bfSEdward Tomasz Napierala return (prr); 4010a7ad07bfSEdward Tomasz Napierala } 4011a7ad07bfSEdward Tomasz Napierala 4012a7ad07bfSEdward Tomasz Napierala struct prison_racct * 4013a7ad07bfSEdward Tomasz Napierala prison_racct_find(const char *name) 4014a7ad07bfSEdward Tomasz Napierala { 4015a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4016a7ad07bfSEdward Tomasz Napierala 40174b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 40184b5c9cf6SEdward Tomasz Napierala 4019a7ad07bfSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4020a7ad07bfSEdward Tomasz Napierala prr = prison_racct_find_locked(name); 4021a7ad07bfSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4022a7ad07bfSEdward Tomasz Napierala return (prr); 4023a7ad07bfSEdward Tomasz Napierala } 4024a7ad07bfSEdward Tomasz Napierala 4025a7ad07bfSEdward Tomasz Napierala void 4026a7ad07bfSEdward Tomasz Napierala prison_racct_hold(struct prison_racct *prr) 4027a7ad07bfSEdward Tomasz Napierala { 4028a7ad07bfSEdward Tomasz Napierala 40294b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 40304b5c9cf6SEdward Tomasz Napierala 4031a7ad07bfSEdward Tomasz Napierala refcount_acquire(&prr->prr_refcount); 4032a7ad07bfSEdward Tomasz Napierala } 4033a7ad07bfSEdward Tomasz Napierala 4034c34bbd2aSEdward Tomasz Napierala static void 4035c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(struct prison_racct *prr) 4036c34bbd2aSEdward Tomasz Napierala { 4037c34bbd2aSEdward Tomasz Napierala 40384b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4039c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4040c34bbd2aSEdward Tomasz Napierala 4041c34bbd2aSEdward Tomasz Napierala if (refcount_release(&prr->prr_refcount)) { 4042c34bbd2aSEdward Tomasz Napierala racct_destroy(&prr->prr_racct); 4043c34bbd2aSEdward Tomasz Napierala LIST_REMOVE(prr, prr_next); 4044c34bbd2aSEdward Tomasz Napierala free(prr, M_PRISON_RACCT); 4045c34bbd2aSEdward Tomasz Napierala } 4046c34bbd2aSEdward Tomasz Napierala } 4047c34bbd2aSEdward Tomasz Napierala 4048a7ad07bfSEdward Tomasz Napierala void 4049a7ad07bfSEdward Tomasz Napierala prison_racct_free(struct prison_racct *prr) 4050a7ad07bfSEdward Tomasz Napierala { 4051a7ad07bfSEdward Tomasz Napierala 40524b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4053c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_UNLOCKED); 4054c34bbd2aSEdward Tomasz Napierala 40556ff4688bSMateusz Guzik if (refcount_release_if_not_last(&prr->prr_refcount)) 4056a7ad07bfSEdward Tomasz Napierala return; 4057a7ad07bfSEdward Tomasz Napierala 4058a7ad07bfSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4059c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(prr); 4060a7ad07bfSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4061a7ad07bfSEdward Tomasz Napierala } 4062a7ad07bfSEdward Tomasz Napierala 4063a7ad07bfSEdward Tomasz Napierala static void 4064a7ad07bfSEdward Tomasz Napierala prison_racct_attach(struct prison *pr) 4065a7ad07bfSEdward Tomasz Napierala { 4066a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4067a7ad07bfSEdward Tomasz Napierala 40684b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4069c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4070c34bbd2aSEdward Tomasz Napierala 4071a7ad07bfSEdward Tomasz Napierala prr = prison_racct_find_locked(pr->pr_name); 4072a7ad07bfSEdward Tomasz Napierala KASSERT(prr != NULL, ("cannot find prison_racct")); 4073a7ad07bfSEdward Tomasz Napierala 4074a7ad07bfSEdward Tomasz Napierala pr->pr_prison_racct = prr; 4075a7ad07bfSEdward Tomasz Napierala } 4076a7ad07bfSEdward Tomasz Napierala 4077c34bbd2aSEdward Tomasz Napierala /* 4078c34bbd2aSEdward Tomasz Napierala * Handle jail renaming. From the racct point of view, renaming means 4079c34bbd2aSEdward Tomasz Napierala * moving from one prison_racct to another. 4080c34bbd2aSEdward Tomasz Napierala */ 4081c34bbd2aSEdward Tomasz Napierala static void 4082c34bbd2aSEdward Tomasz Napierala prison_racct_modify(struct prison *pr) 4083c34bbd2aSEdward Tomasz Napierala { 4084dbadb015SKonstantin Belousov #ifdef RCTL 4085c34bbd2aSEdward Tomasz Napierala struct proc *p; 4086c34bbd2aSEdward Tomasz Napierala struct ucred *cred; 4087dbadb015SKonstantin Belousov #endif 4088c34bbd2aSEdward Tomasz Napierala struct prison_racct *oldprr; 4089c34bbd2aSEdward Tomasz Napierala 40904b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 40914b5c9cf6SEdward Tomasz Napierala 4092c34bbd2aSEdward Tomasz Napierala sx_slock(&allproc_lock); 4093c34bbd2aSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4094c34bbd2aSEdward Tomasz Napierala 4095e30345e7SEdward Tomasz Napierala if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) { 4096e30345e7SEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4097e30345e7SEdward Tomasz Napierala sx_sunlock(&allproc_lock); 4098c34bbd2aSEdward Tomasz Napierala return; 4099e30345e7SEdward Tomasz Napierala } 4100c34bbd2aSEdward Tomasz Napierala 4101c34bbd2aSEdward Tomasz Napierala oldprr = pr->pr_prison_racct; 4102c34bbd2aSEdward Tomasz Napierala pr->pr_prison_racct = NULL; 4103c34bbd2aSEdward Tomasz Napierala 4104c34bbd2aSEdward Tomasz Napierala prison_racct_attach(pr); 4105c34bbd2aSEdward Tomasz Napierala 4106c34bbd2aSEdward Tomasz Napierala /* 4107c34bbd2aSEdward Tomasz Napierala * Move resource utilisation records. 4108c34bbd2aSEdward Tomasz Napierala */ 4109c34bbd2aSEdward Tomasz Napierala racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct); 4110c34bbd2aSEdward Tomasz Napierala 4111f87beb93SAndriy Gapon #ifdef RCTL 4112c34bbd2aSEdward Tomasz Napierala /* 4113c34bbd2aSEdward Tomasz Napierala * Force rctl to reattach rules to processes. 4114c34bbd2aSEdward Tomasz Napierala */ 4115c34bbd2aSEdward Tomasz Napierala FOREACH_PROC_IN_SYSTEM(p) { 4116c34bbd2aSEdward Tomasz Napierala PROC_LOCK(p); 4117c34bbd2aSEdward Tomasz Napierala cred = crhold(p->p_ucred); 4118c34bbd2aSEdward Tomasz Napierala PROC_UNLOCK(p); 4119f87beb93SAndriy Gapon rctl_proc_ucred_changed(p, cred); 4120c34bbd2aSEdward Tomasz Napierala crfree(cred); 4121c34bbd2aSEdward Tomasz Napierala } 4122f87beb93SAndriy Gapon #endif 4123c34bbd2aSEdward Tomasz Napierala 4124c34bbd2aSEdward Tomasz Napierala sx_sunlock(&allproc_lock); 4125c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(oldprr); 4126c34bbd2aSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4127c34bbd2aSEdward Tomasz Napierala } 4128c34bbd2aSEdward Tomasz Napierala 4129a7ad07bfSEdward Tomasz Napierala static void 4130a7ad07bfSEdward Tomasz Napierala prison_racct_detach(struct prison *pr) 4131a7ad07bfSEdward Tomasz Napierala { 4132c34bbd2aSEdward Tomasz Napierala 41334b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4134c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_UNLOCKED); 4135c34bbd2aSEdward Tomasz Napierala 4136af3c786cSMateusz Guzik if (pr->pr_prison_racct == NULL) 4137af3c786cSMateusz Guzik return; 4138a7ad07bfSEdward Tomasz Napierala prison_racct_free(pr->pr_prison_racct); 4139a7ad07bfSEdward Tomasz Napierala pr->pr_prison_racct = NULL; 4140a7ad07bfSEdward Tomasz Napierala } 4141a7ad07bfSEdward Tomasz Napierala #endif /* RACCT */ 4142a7ad07bfSEdward Tomasz Napierala 4143413628a7SBjoern A. Zeeb #ifdef DDB 4144b38ff370SJamie Gritton 4145b38ff370SJamie Gritton static void 4146b38ff370SJamie Gritton db_show_prison(struct prison *pr) 4147413628a7SBjoern A. Zeeb { 4148672756aaSJamie Gritton struct bool_flags *bf; 4149672756aaSJamie Gritton struct jailsys_flags *jsf; 4150b38ff370SJamie Gritton #if defined(INET) || defined(INET6) 4151b38ff370SJamie Gritton int ii; 4152413628a7SBjoern A. Zeeb #endif 4153672756aaSJamie Gritton unsigned f; 41548144690aSEric van Gyzen #ifdef INET 41558144690aSEric van Gyzen char ip4buf[INET_ADDRSTRLEN]; 41568144690aSEric van Gyzen #endif 4157413628a7SBjoern A. Zeeb #ifdef INET6 4158413628a7SBjoern A. Zeeb char ip6buf[INET6_ADDRSTRLEN]; 4159413628a7SBjoern A. Zeeb #endif 4160413628a7SBjoern A. Zeeb 4161b38ff370SJamie Gritton db_printf("prison %p:\n", pr); 4162b38ff370SJamie Gritton db_printf(" jid = %d\n", pr->pr_id); 4163b38ff370SJamie Gritton db_printf(" name = %s\n", pr->pr_name); 41640304c731SJamie Gritton db_printf(" parent = %p\n", pr->pr_parent); 4165b38ff370SJamie Gritton db_printf(" ref = %d\n", pr->pr_ref); 4166b38ff370SJamie Gritton db_printf(" uref = %d\n", pr->pr_uref); 4167b38ff370SJamie Gritton db_printf(" path = %s\n", pr->pr_path); 4168b38ff370SJamie Gritton db_printf(" cpuset = %d\n", pr->pr_cpuset 4169b38ff370SJamie Gritton ? pr->pr_cpuset->cs_id : -1); 4170679e1390SJamie Gritton #ifdef VIMAGE 4171679e1390SJamie Gritton db_printf(" vnet = %p\n", pr->pr_vnet); 4172679e1390SJamie Gritton #endif 4173b38ff370SJamie Gritton db_printf(" root = %p\n", pr->pr_root); 4174b38ff370SJamie Gritton db_printf(" securelevel = %d\n", pr->pr_securelevel); 41750cc207a6SMartin Matuska db_printf(" devfs_rsnum = %d\n", pr->pr_devfs_rsnum); 4176fe0518e9SBjoern A. Zeeb db_printf(" children.max = %d\n", pr->pr_childmax); 4177fe0518e9SBjoern A. Zeeb db_printf(" children.cur = %d\n", pr->pr_childcount); 41780304c731SJamie Gritton db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children)); 41790304c731SJamie Gritton db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling)); 4180fe0518e9SBjoern A. Zeeb db_printf(" flags = 0x%x", pr->pr_flags); 4181672756aaSJamie Gritton for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++) 4182672756aaSJamie Gritton if (pr->pr_flags & bf->flag) 4183672756aaSJamie Gritton db_printf(" %s", bf->name); 4184672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 4185672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 4186672756aaSJamie Gritton jsf++) { 4187672756aaSJamie Gritton f = pr->pr_flags & (jsf->disable | jsf->new); 4188672756aaSJamie Gritton db_printf(" %-16s= %s\n", jsf->name, 4189672756aaSJamie Gritton (f != 0 && f == jsf->disable) ? "disable" 4190672756aaSJamie Gritton : (f == jsf->new) ? "new" 41917cbf7213SJamie Gritton : "inherit"); 41927cbf7213SJamie Gritton } 4193fe0518e9SBjoern A. Zeeb db_printf(" allow = 0x%x", pr->pr_allow); 4194672756aaSJamie Gritton for (bf = pr_flag_allow; 41950e5c6bd4SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && bf->flag != 0; 4196672756aaSJamie Gritton bf++) 4197672756aaSJamie Gritton if (pr->pr_allow & bf->flag) 4198672756aaSJamie Gritton db_printf(" %s", bf->name); 4199b38ff370SJamie Gritton db_printf("\n"); 42000304c731SJamie Gritton db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs); 4201c1f19219SJamie Gritton db_printf(" host.hostname = %s\n", pr->pr_hostname); 4202c1f19219SJamie Gritton db_printf(" host.domainname = %s\n", pr->pr_domainname); 4203c1f19219SJamie Gritton db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid); 420476ca6f88SJamie Gritton db_printf(" host.hostid = %lu\n", pr->pr_hostid); 4205413628a7SBjoern A. Zeeb #ifdef INET 4206b38ff370SJamie Gritton db_printf(" ip4s = %d\n", pr->pr_ip4s); 4207b38ff370SJamie Gritton for (ii = 0; ii < pr->pr_ip4s; ii++) 4208b38ff370SJamie Gritton db_printf(" %s %s\n", 4209fe0518e9SBjoern A. Zeeb ii == 0 ? "ip4.addr =" : " ", 42108144690aSEric van Gyzen inet_ntoa_r(pr->pr_ip4[ii], ip4buf)); 4211413628a7SBjoern A. Zeeb #endif 4212413628a7SBjoern A. Zeeb #ifdef INET6 4213b38ff370SJamie Gritton db_printf(" ip6s = %d\n", pr->pr_ip6s); 4214b38ff370SJamie Gritton for (ii = 0; ii < pr->pr_ip6s; ii++) 4215b38ff370SJamie Gritton db_printf(" %s %s\n", 4216fe0518e9SBjoern A. Zeeb ii == 0 ? "ip6.addr =" : " ", 4217b38ff370SJamie Gritton ip6_sprintf(ip6buf, &pr->pr_ip6[ii])); 4218b38ff370SJamie Gritton #endif 4219b38ff370SJamie Gritton } 4220b38ff370SJamie Gritton 4221b38ff370SJamie Gritton DB_SHOW_COMMAND(prison, db_show_prison_command) 4222b38ff370SJamie Gritton { 4223b38ff370SJamie Gritton struct prison *pr; 4224b38ff370SJamie Gritton 4225b38ff370SJamie Gritton if (!have_addr) { 42260304c731SJamie Gritton /* 42270304c731SJamie Gritton * Show all prisons in the list, and prison0 which is not 42280304c731SJamie Gritton * listed. 42290304c731SJamie Gritton */ 42300304c731SJamie Gritton db_show_prison(&prison0); 42310304c731SJamie Gritton if (!db_pager_quit) { 4232b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 4233b38ff370SJamie Gritton db_show_prison(pr); 4234413628a7SBjoern A. Zeeb if (db_pager_quit) 4235413628a7SBjoern A. Zeeb break; 4236413628a7SBjoern A. Zeeb } 42370304c731SJamie Gritton } 4238b38ff370SJamie Gritton return; 4239413628a7SBjoern A. Zeeb } 4240b38ff370SJamie Gritton 42410304c731SJamie Gritton if (addr == 0) 42420304c731SJamie Gritton pr = &prison0; 42430304c731SJamie Gritton else { 4244b38ff370SJamie Gritton /* Look for a prison with the ID and with references. */ 4245b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 4246b38ff370SJamie Gritton if (pr->pr_id == addr && pr->pr_ref > 0) 4247b38ff370SJamie Gritton break; 4248b38ff370SJamie Gritton if (pr == NULL) 4249b38ff370SJamie Gritton /* Look again, without requiring a reference. */ 4250b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 4251b38ff370SJamie Gritton if (pr->pr_id == addr) 4252b38ff370SJamie Gritton break; 4253b38ff370SJamie Gritton if (pr == NULL) 4254b38ff370SJamie Gritton /* Assume address points to a valid prison. */ 4255b38ff370SJamie Gritton pr = (struct prison *)addr; 42560304c731SJamie Gritton } 4257b38ff370SJamie Gritton db_show_prison(pr); 4258b38ff370SJamie Gritton } 4259b38ff370SJamie Gritton 4260413628a7SBjoern A. Zeeb #endif /* DDB */ 4261