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 139*7de883c8SJamie Gritton static int get_next_prid(struct prison **insprp); 140b38ff370SJamie Gritton static int do_jail_attach(struct thread *td, struct prison *pr); 141b3059e09SRobert Watson static void prison_complete(void *context, int pending); 142b38ff370SJamie Gritton static void prison_deref(struct prison *pr, int flags); 1430304c731SJamie Gritton static char *prison_path(struct prison *pr1, struct prison *pr2); 1440304c731SJamie Gritton static void prison_remove_one(struct prison *pr); 145a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 146a7ad07bfSEdward Tomasz Napierala static void prison_racct_attach(struct prison *pr); 147c34bbd2aSEdward Tomasz Napierala static void prison_racct_modify(struct prison *pr); 148a7ad07bfSEdward Tomasz Napierala static void prison_racct_detach(struct prison *pr); 149a7ad07bfSEdward Tomasz Napierala #endif 150fd7a8150SMike Barcroft 151b38ff370SJamie Gritton /* Flags for prison_deref */ 152b38ff370SJamie Gritton #define PD_DEREF 0x01 153b38ff370SJamie Gritton #define PD_DEUREF 0x02 154b38ff370SJamie Gritton #define PD_LOCKED 0x04 155b38ff370SJamie Gritton #define PD_LIST_SLOCKED 0x08 156b38ff370SJamie Gritton #define PD_LIST_XLOCKED 0x10 157fd7a8150SMike Barcroft 1580304c731SJamie Gritton /* 1595cc70397SBjoern A. Zeeb * Parameter names corresponding to PR_* flag values. Size values are for kvm 1605cc70397SBjoern A. Zeeb * as we cannot figure out the size of a sparse array, or an array without a 1615cc70397SBjoern A. Zeeb * terminating entry. 1620304c731SJamie Gritton */ 163672756aaSJamie Gritton static struct bool_flags pr_flag_bool[] = { 164672756aaSJamie Gritton {"persist", "nopersist", PR_PERSIST}, 165592bcae8SBjoern A. Zeeb #ifdef INET 166672756aaSJamie Gritton {"ip4.saddrsel", "ip4.nosaddrsel", PR_IP4_SADDRSEL}, 167592bcae8SBjoern A. Zeeb #endif 168592bcae8SBjoern A. Zeeb #ifdef INET6 169672756aaSJamie Gritton {"ip6.saddrsel", "ip6.nosaddrsel", PR_IP6_SADDRSEL}, 170592bcae8SBjoern A. Zeeb #endif 1710304c731SJamie Gritton }; 172672756aaSJamie Gritton const size_t pr_flag_bool_size = sizeof(pr_flag_bool); 1730304c731SJamie Gritton 174672756aaSJamie Gritton static struct jailsys_flags pr_flag_jailsys[] = { 1757cbf7213SJamie Gritton {"host", 0, PR_HOST}, 1767cbf7213SJamie Gritton #ifdef VIMAGE 1777cbf7213SJamie Gritton {"vnet", 0, PR_VNET}, 1787cbf7213SJamie Gritton #endif 1790304c731SJamie Gritton #ifdef INET 1806a3f2779SJamie Gritton {"ip4", PR_IP4_USER, PR_IP4_USER}, 1810304c731SJamie Gritton #endif 1820304c731SJamie Gritton #ifdef INET6 1836a3f2779SJamie Gritton {"ip6", PR_IP6_USER, PR_IP6_USER}, 184679e1390SJamie Gritton #endif 1850304c731SJamie Gritton }; 1865cc70397SBjoern A. Zeeb const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys); 1870304c731SJamie Gritton 1880e5c6bd4SJamie Gritton /* Make this array full-size so dynamic parameters can be added. */ 1890e5c6bd4SJamie Gritton static struct bool_flags pr_flag_allow[NBBY * NBPW] = { 190672756aaSJamie Gritton {"allow.set_hostname", "allow.noset_hostname", PR_ALLOW_SET_HOSTNAME}, 191672756aaSJamie Gritton {"allow.sysvipc", "allow.nosysvipc", PR_ALLOW_SYSVIPC}, 192672756aaSJamie Gritton {"allow.raw_sockets", "allow.noraw_sockets", PR_ALLOW_RAW_SOCKETS}, 193672756aaSJamie Gritton {"allow.chflags", "allow.nochflags", PR_ALLOW_CHFLAGS}, 194672756aaSJamie Gritton {"allow.mount", "allow.nomount", PR_ALLOW_MOUNT}, 195672756aaSJamie Gritton {"allow.quotas", "allow.noquotas", PR_ALLOW_QUOTAS}, 196672756aaSJamie Gritton {"allow.socket_af", "allow.nosocket_af", PR_ALLOW_SOCKET_AF}, 197ccd6ac9fSAntoine Brodin {"allow.mlock", "allow.nomlock", PR_ALLOW_MLOCK}, 198672756aaSJamie Gritton {"allow.reserved_ports", "allow.noreserved_ports", 199672756aaSJamie Gritton PR_ALLOW_RESERVED_PORTS}, 200b19d66fdSJamie Gritton {"allow.read_msgbuf", "allow.noread_msgbuf", PR_ALLOW_READ_MSGBUF}, 201b3079544SJamie Gritton {"allow.unprivileged_proc_debug", "allow.nounprivileged_proc_debug", 202b3079544SJamie Gritton PR_ALLOW_UNPRIV_DEBUG}, 20305e1e482SMariusz Zaborski {"allow.suser", "allow.nosuser", PR_ALLOW_SUSER}, 2040304c731SJamie Gritton }; 205672756aaSJamie Gritton const size_t pr_flag_allow_size = sizeof(pr_flag_allow); 2060304c731SJamie Gritton 207b3079544SJamie Gritton #define JAIL_DEFAULT_ALLOW (PR_ALLOW_SET_HOSTNAME | \ 208b3079544SJamie Gritton PR_ALLOW_RESERVED_PORTS | \ 20905e1e482SMariusz Zaborski PR_ALLOW_UNPRIV_DEBUG | \ 21005e1e482SMariusz Zaborski PR_ALLOW_SUSER) 21142bebd82SJamie Gritton #define JAIL_DEFAULT_ENFORCE_STATFS 2 212bf3db8aaSMartin Matuska #define JAIL_DEFAULT_DEVFS_RSNUM 0 2130304c731SJamie Gritton static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW; 21442bebd82SJamie Gritton static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 2150cc207a6SMartin Matuska static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM; 2160304c731SJamie Gritton #if defined(INET) || defined(INET6) 217e92e0574SJamie Gritton static unsigned jail_max_af_ips = 255; 2180304c731SJamie Gritton #endif 2190304c731SJamie Gritton 220b96bd95bSIan Lepore /* 221b96bd95bSIan Lepore * Initialize the parts of prison0 that can't be static-initialized with 222b96bd95bSIan Lepore * constants. This is called from proc0_init() after creating thread0 cpuset. 223b96bd95bSIan Lepore */ 224b96bd95bSIan Lepore void 225b96bd95bSIan Lepore prison0_init(void) 226b96bd95bSIan Lepore { 227c3188289SKyle Evans uint8_t *file, *data; 228c3188289SKyle Evans size_t size; 229b96bd95bSIan Lepore 230b96bd95bSIan Lepore prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset); 231b96bd95bSIan Lepore prison0.pr_osreldate = osreldate; 232b96bd95bSIan Lepore strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease)); 233c3188289SKyle Evans 234c3188289SKyle Evans /* If we have a preloaded hostuuid, use it. */ 235c3188289SKyle Evans file = preload_search_by_type(PRISON0_HOSTUUID_MODULE); 236c3188289SKyle Evans if (file != NULL) { 237c3188289SKyle Evans data = preload_fetch_addr(file); 238c3188289SKyle Evans size = preload_fetch_size(file); 239c3188289SKyle Evans if (data != NULL) { 240c3188289SKyle Evans /* 241c3188289SKyle Evans * The preloaded data may include trailing whitespace, almost 242c3188289SKyle Evans * certainly a newline; skip over any whitespace or 243c3188289SKyle Evans * non-printable characters to be safe. 244c3188289SKyle Evans */ 245c3188289SKyle Evans while (size > 0 && data[size - 1] <= 0x20) { 246c3188289SKyle Evans data[size--] = '\0'; 247c3188289SKyle Evans } 248c3188289SKyle Evans if (validate_uuid(data, size, NULL, 0) == 0) { 249c3188289SKyle Evans (void)strlcpy(prison0.pr_hostuuid, data, 250c3188289SKyle Evans size + 1); 251c3188289SKyle Evans } else if (bootverbose) { 252c3188289SKyle Evans printf("hostuuid: preload data malformed: '%s'", 253c3188289SKyle Evans data); 254c3188289SKyle Evans } 255c3188289SKyle Evans } 256c3188289SKyle Evans } 257c3188289SKyle Evans if (bootverbose) 258c3188289SKyle Evans printf("hostuuid: using %s\n", prison0.pr_hostuuid); 259b96bd95bSIan Lepore } 260b96bd95bSIan Lepore 261116734c4SMatthew Dillon /* 2629ddb7954SMike Barcroft * struct jail_args { 2639ddb7954SMike Barcroft * struct jail *jail; 2649ddb7954SMike Barcroft * }; 265116734c4SMatthew Dillon */ 26675c13541SPoul-Henning Kamp int 267c542c43eSJamie Gritton sys_jail(struct thread *td, struct jail_args *uap) 26875c13541SPoul-Henning Kamp { 269413628a7SBjoern A. Zeeb uint32_t version; 270413628a7SBjoern A. Zeeb int error; 2710304c731SJamie Gritton struct jail j; 272413628a7SBjoern A. Zeeb 273413628a7SBjoern A. Zeeb error = copyin(uap->jail, &version, sizeof(uint32_t)); 274413628a7SBjoern A. Zeeb if (error) 275413628a7SBjoern A. Zeeb return (error); 276413628a7SBjoern A. Zeeb 277413628a7SBjoern A. Zeeb switch (version) { 278413628a7SBjoern A. Zeeb case 0: 279413628a7SBjoern A. Zeeb { 280413628a7SBjoern A. Zeeb struct jail_v0 j0; 281413628a7SBjoern A. Zeeb 2820304c731SJamie Gritton /* FreeBSD single IPv4 jails. */ 2830304c731SJamie Gritton bzero(&j, sizeof(struct jail)); 284413628a7SBjoern A. Zeeb error = copyin(uap->jail, &j0, sizeof(struct jail_v0)); 285413628a7SBjoern A. Zeeb if (error) 286413628a7SBjoern A. Zeeb return (error); 2870304c731SJamie Gritton j.version = j0.version; 2880304c731SJamie Gritton j.path = j0.path; 2890304c731SJamie Gritton j.hostname = j0.hostname; 290b5019bc4SPeter Wemm j.ip4s = htonl(j0.ip_number); /* jail_v0 is host order */ 291413628a7SBjoern A. Zeeb break; 292413628a7SBjoern A. Zeeb } 293413628a7SBjoern A. Zeeb 294413628a7SBjoern A. Zeeb case 1: 295413628a7SBjoern A. Zeeb /* 296413628a7SBjoern A. Zeeb * Version 1 was used by multi-IPv4 jail implementations 297413628a7SBjoern A. Zeeb * that never made it into the official kernel. 298413628a7SBjoern A. Zeeb */ 299413628a7SBjoern A. Zeeb return (EINVAL); 300413628a7SBjoern A. Zeeb 301413628a7SBjoern A. Zeeb case 2: /* JAIL_API_VERSION */ 302413628a7SBjoern A. Zeeb /* FreeBSD multi-IPv4/IPv6,noIP jails. */ 303413628a7SBjoern A. Zeeb error = copyin(uap->jail, &j, sizeof(struct jail)); 304413628a7SBjoern A. Zeeb if (error) 305413628a7SBjoern A. Zeeb return (error); 3060304c731SJamie Gritton break; 3070304c731SJamie Gritton 3080304c731SJamie Gritton default: 3090304c731SJamie Gritton /* Sci-Fi jails are not supported, sorry. */ 3100304c731SJamie Gritton return (EINVAL); 3110304c731SJamie Gritton } 312c542c43eSJamie Gritton return (kern_jail(td, &j)); 3130304c731SJamie Gritton } 3140304c731SJamie Gritton 3150304c731SJamie Gritton int 316c542c43eSJamie Gritton kern_jail(struct thread *td, struct jail *j) 3170304c731SJamie Gritton { 318c542c43eSJamie Gritton struct iovec optiov[2 * (4 + nitems(pr_flag_allow) 319e92e0574SJamie Gritton #ifdef INET 320e92e0574SJamie Gritton + 1 321e92e0574SJamie Gritton #endif 322e92e0574SJamie Gritton #ifdef INET6 323e92e0574SJamie Gritton + 1 324e92e0574SJamie Gritton #endif 325e92e0574SJamie Gritton )]; 3260304c731SJamie Gritton struct uio opt; 3270304c731SJamie Gritton char *u_path, *u_hostname, *u_name; 328672756aaSJamie Gritton struct bool_flags *bf; 3290304c731SJamie Gritton #ifdef INET 330e92e0574SJamie Gritton uint32_t ip4s; 3310304c731SJamie Gritton struct in_addr *u_ip4; 3320304c731SJamie Gritton #endif 3330304c731SJamie Gritton #ifdef INET6 3340304c731SJamie Gritton struct in6_addr *u_ip6; 3350304c731SJamie Gritton #endif 3360304c731SJamie Gritton size_t tmplen; 337c542c43eSJamie Gritton int error, enforce_statfs; 3380304c731SJamie Gritton 3390304c731SJamie Gritton bzero(&optiov, sizeof(optiov)); 3400304c731SJamie Gritton opt.uio_iov = optiov; 3410304c731SJamie Gritton opt.uio_iovcnt = 0; 3420304c731SJamie Gritton opt.uio_offset = -1; 3430304c731SJamie Gritton opt.uio_resid = -1; 3440304c731SJamie Gritton opt.uio_segflg = UIO_SYSSPACE; 3450304c731SJamie Gritton opt.uio_rw = UIO_READ; 3460304c731SJamie Gritton opt.uio_td = td; 3470304c731SJamie Gritton 3480304c731SJamie Gritton /* Set permissions for top-level jails from sysctls. */ 3490304c731SJamie Gritton if (!jailed(td->td_ucred)) { 350672756aaSJamie Gritton for (bf = pr_flag_allow; 3510e5c6bd4SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 3520e5c6bd4SJamie Gritton bf->flag != 0; 353672756aaSJamie Gritton bf++) { 354672756aaSJamie Gritton optiov[opt.uio_iovcnt].iov_base = __DECONST(char *, 355672756aaSJamie Gritton (jail_default_allow & bf->flag) 356672756aaSJamie Gritton ? bf->name : bf->noname); 3570304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = 3580304c731SJamie Gritton strlen(optiov[opt.uio_iovcnt].iov_base) + 1; 3590304c731SJamie Gritton opt.uio_iovcnt += 2; 3600304c731SJamie Gritton } 3610304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "enforce_statfs"; 3620304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs"); 3630304c731SJamie Gritton opt.uio_iovcnt++; 3640304c731SJamie Gritton enforce_statfs = jail_default_enforce_statfs; 3650304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = &enforce_statfs; 3660304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs); 3670304c731SJamie Gritton opt.uio_iovcnt++; 3680304c731SJamie Gritton } 3690304c731SJamie Gritton 370b38ff370SJamie Gritton tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN; 371b38ff370SJamie Gritton #ifdef INET 3720304c731SJamie Gritton ip4s = (j->version == 0) ? 1 : j->ip4s; 3730304c731SJamie Gritton if (ip4s > jail_max_af_ips) 374b38ff370SJamie Gritton return (EINVAL); 3750304c731SJamie Gritton tmplen += ip4s * sizeof(struct in_addr); 376b38ff370SJamie Gritton #else 3770304c731SJamie Gritton if (j->ip4s > 0) 378b38ff370SJamie Gritton return (EINVAL); 379b38ff370SJamie Gritton #endif 380b38ff370SJamie Gritton #ifdef INET6 3810304c731SJamie Gritton if (j->ip6s > jail_max_af_ips) 382b38ff370SJamie Gritton return (EINVAL); 3830304c731SJamie Gritton tmplen += j->ip6s * sizeof(struct in6_addr); 384b38ff370SJamie Gritton #else 3850304c731SJamie Gritton if (j->ip6s > 0) 386b38ff370SJamie Gritton return (EINVAL); 387b38ff370SJamie Gritton #endif 388b38ff370SJamie Gritton u_path = malloc(tmplen, M_TEMP, M_WAITOK); 389b38ff370SJamie Gritton u_hostname = u_path + MAXPATHLEN; 390b38ff370SJamie Gritton u_name = u_hostname + MAXHOSTNAMELEN; 391b38ff370SJamie Gritton #ifdef INET 392b38ff370SJamie Gritton u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN); 393b38ff370SJamie Gritton #endif 394b38ff370SJamie Gritton #ifdef INET6 395b38ff370SJamie Gritton #ifdef INET 3960304c731SJamie Gritton u_ip6 = (struct in6_addr *)(u_ip4 + ip4s); 397b38ff370SJamie Gritton #else 398b38ff370SJamie Gritton u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN); 399b38ff370SJamie Gritton #endif 400b38ff370SJamie Gritton #endif 4010304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "path"; 4020304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("path"); 4030304c731SJamie Gritton opt.uio_iovcnt++; 4040304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_path; 4050304c731SJamie Gritton error = copyinstr(j->path, u_path, MAXPATHLEN, 4060304c731SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 407b38ff370SJamie Gritton if (error) { 408b38ff370SJamie Gritton free(u_path, M_TEMP); 409b38ff370SJamie Gritton return (error); 410b38ff370SJamie Gritton } 4110304c731SJamie Gritton opt.uio_iovcnt++; 4120304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "host.hostname"; 4130304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname"); 4140304c731SJamie Gritton opt.uio_iovcnt++; 4150304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_hostname; 4160304c731SJamie Gritton error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN, 4170304c731SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 418b38ff370SJamie Gritton if (error) { 419b38ff370SJamie Gritton free(u_path, M_TEMP); 420b38ff370SJamie Gritton return (error); 421b38ff370SJamie Gritton } 4220304c731SJamie Gritton opt.uio_iovcnt++; 4230304c731SJamie Gritton if (j->jailname != NULL) { 424b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "name"; 425b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("name"); 426b38ff370SJamie Gritton opt.uio_iovcnt++; 427b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_name; 4280304c731SJamie Gritton error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN, 429b38ff370SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 430b38ff370SJamie Gritton if (error) { 431b38ff370SJamie Gritton free(u_path, M_TEMP); 432b38ff370SJamie Gritton return (error); 433b38ff370SJamie Gritton } 434b38ff370SJamie Gritton opt.uio_iovcnt++; 435b38ff370SJamie Gritton } 436b38ff370SJamie Gritton #ifdef INET 437b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "ip4.addr"; 438b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr"); 439b38ff370SJamie Gritton opt.uio_iovcnt++; 440b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_ip4; 4410304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr); 4420304c731SJamie Gritton if (j->version == 0) 4430304c731SJamie Gritton u_ip4->s_addr = j->ip4s; 4440304c731SJamie Gritton else { 4450304c731SJamie Gritton error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len); 446b38ff370SJamie Gritton if (error) { 447b38ff370SJamie Gritton free(u_path, M_TEMP); 448b38ff370SJamie Gritton return (error); 449b38ff370SJamie Gritton } 4500304c731SJamie Gritton } 451b38ff370SJamie Gritton opt.uio_iovcnt++; 452b38ff370SJamie Gritton #endif 453b38ff370SJamie Gritton #ifdef INET6 454b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "ip6.addr"; 455b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr"); 456b38ff370SJamie Gritton opt.uio_iovcnt++; 457b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_ip6; 4580304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr); 4590304c731SJamie Gritton error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len); 460b38ff370SJamie Gritton if (error) { 461b38ff370SJamie Gritton free(u_path, M_TEMP); 462b38ff370SJamie Gritton return (error); 463b38ff370SJamie Gritton } 464b38ff370SJamie Gritton opt.uio_iovcnt++; 465b38ff370SJamie Gritton #endif 46602abd400SPedro F. Giffuni KASSERT(opt.uio_iovcnt <= nitems(optiov), 4670304c731SJamie Gritton ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt)); 468b38ff370SJamie Gritton error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH); 469b38ff370SJamie Gritton free(u_path, M_TEMP); 470b38ff370SJamie Gritton return (error); 471b38ff370SJamie Gritton } 472b38ff370SJamie Gritton 473b38ff370SJamie Gritton /* 474b38ff370SJamie Gritton * struct jail_set_args { 475b38ff370SJamie Gritton * struct iovec *iovp; 476b38ff370SJamie Gritton * unsigned int iovcnt; 477b38ff370SJamie Gritton * int flags; 478b38ff370SJamie Gritton * }; 479b38ff370SJamie Gritton */ 480b38ff370SJamie Gritton int 4818451d0ddSKip Macy sys_jail_set(struct thread *td, struct jail_set_args *uap) 482b38ff370SJamie Gritton { 483b38ff370SJamie Gritton struct uio *auio; 484b38ff370SJamie Gritton int error; 485b38ff370SJamie Gritton 486b38ff370SJamie Gritton /* Check that we have an even number of iovecs. */ 487b38ff370SJamie Gritton if (uap->iovcnt & 1) 488b38ff370SJamie Gritton return (EINVAL); 489b38ff370SJamie Gritton 490b38ff370SJamie Gritton error = copyinuio(uap->iovp, uap->iovcnt, &auio); 491b38ff370SJamie Gritton if (error) 492b38ff370SJamie Gritton return (error); 493b38ff370SJamie Gritton error = kern_jail_set(td, auio, uap->flags); 494b38ff370SJamie Gritton free(auio, M_IOV); 495b38ff370SJamie Gritton return (error); 496413628a7SBjoern A. Zeeb } 497413628a7SBjoern A. Zeeb 498413628a7SBjoern A. Zeeb int 499b38ff370SJamie Gritton kern_jail_set(struct thread *td, struct uio *optuio, int flags) 500413628a7SBjoern A. Zeeb { 501fd7a8150SMike Barcroft struct nameidata nd; 502b38ff370SJamie Gritton #ifdef INET 503b38ff370SJamie Gritton struct in_addr *ip4; 504413628a7SBjoern A. Zeeb #endif 505b38ff370SJamie Gritton #ifdef INET6 506b38ff370SJamie Gritton struct in6_addr *ip6; 507b38ff370SJamie Gritton #endif 508b38ff370SJamie Gritton struct vfsopt *opt; 509b38ff370SJamie Gritton struct vfsoptlist *opts; 510*7de883c8SJamie Gritton struct prison *pr, *deadpr, *inspr, *mypr, *ppr, *tpr; 511b38ff370SJamie Gritton struct vnode *root; 512babbbb9cSJamie Gritton char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid; 513b96bd95bSIan Lepore char *g_path, *osrelstr; 514672756aaSJamie Gritton struct bool_flags *bf; 515672756aaSJamie Gritton struct jailsys_flags *jsf; 5160304c731SJamie Gritton #if defined(INET) || defined(INET6) 51757aea6dfSBjoern A. Zeeb struct prison *tppr; 518b38ff370SJamie Gritton void *op; 5190304c731SJamie Gritton #endif 52076ca6f88SJamie Gritton unsigned long hid; 521b6f47c23SJamie Gritton size_t namelen, onamelen, pnamelen; 522cc5fd8c7SJamie Gritton int born, created, cuflags, descend, enforce; 523cc5fd8c7SJamie Gritton int error, errmsg_len, errmsg_pos; 5240cc207a6SMartin Matuska int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel; 525672756aaSJamie Gritton int jid, jsys, len, level; 526b96bd95bSIan Lepore int childmax, osreldt, rsnum, slevel; 527d465a41dSBjoern A. Zeeb #if defined(INET) || defined(INET6) 5280304c731SJamie Gritton int ii, ij; 529413628a7SBjoern A. Zeeb #endif 530413628a7SBjoern A. Zeeb #ifdef INET 5312b0d6f81SJamie Gritton int ip4s, redo_ip4; 532413628a7SBjoern A. Zeeb #endif 533b38ff370SJamie Gritton #ifdef INET6 5342b0d6f81SJamie Gritton int ip6s, redo_ip6; 535b38ff370SJamie Gritton #endif 5366beb3bb4SKirk McKusick uint64_t pr_allow, ch_allow, pr_flags, ch_flags; 537b3079544SJamie Gritton uint64_t pr_allow_diff; 5386beb3bb4SKirk McKusick unsigned tallow; 539b38ff370SJamie Gritton char numbuf[12]; 540b38ff370SJamie Gritton 541b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_SET); 542b38ff370SJamie Gritton if (!error && (flags & JAIL_ATTACH)) 543b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 544b38ff370SJamie Gritton if (error) 54575c13541SPoul-Henning Kamp return (error); 546b6f47c23SJamie Gritton mypr = td->td_ucred->cr_prison; 547b97457e2SJamie Gritton if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0) 5480304c731SJamie Gritton return (EPERM); 549b38ff370SJamie Gritton if (flags & ~JAIL_SET_MASK) 550b38ff370SJamie Gritton return (EINVAL); 551b38ff370SJamie Gritton 552b38ff370SJamie Gritton /* 553b38ff370SJamie Gritton * Check all the parameters before committing to anything. Not all 554b38ff370SJamie Gritton * errors can be caught early, but we may as well try. Also, this 555b38ff370SJamie Gritton * takes care of some expensive stuff (path lookup) before getting 556b38ff370SJamie Gritton * the allprison lock. 557b38ff370SJamie Gritton * 558b38ff370SJamie Gritton * XXX Jails are not filesystems, and jail parameters are not mount 559b38ff370SJamie Gritton * options. But it makes more sense to re-use the vfsopt code 560b38ff370SJamie Gritton * than duplicate it under a different name. 561b38ff370SJamie Gritton */ 562b38ff370SJamie Gritton error = vfs_buildopts(optuio, &opts); 563b38ff370SJamie Gritton if (error) 564b38ff370SJamie Gritton return (error); 565b38ff370SJamie Gritton #ifdef INET 566b38ff370SJamie Gritton ip4 = NULL; 567b38ff370SJamie Gritton #endif 568b38ff370SJamie Gritton #ifdef INET6 569b38ff370SJamie Gritton ip6 = NULL; 570b38ff370SJamie Gritton #endif 5716dfe0a3dSMartin Matuska g_path = NULL; 572b38ff370SJamie Gritton 573b6f47c23SJamie Gritton cuflags = flags & (JAIL_CREATE | JAIL_UPDATE); 574b6f47c23SJamie Gritton if (!cuflags) { 575b6f47c23SJamie Gritton error = EINVAL; 576b6f47c23SJamie Gritton vfs_opterror(opts, "no valid operation (create or update)"); 577b6f47c23SJamie Gritton goto done_errmsg; 578b6f47c23SJamie Gritton } 579b6f47c23SJamie Gritton 580b38ff370SJamie Gritton error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 581b38ff370SJamie Gritton if (error == ENOENT) 582b38ff370SJamie Gritton jid = 0; 583b38ff370SJamie Gritton else if (error != 0) 584b38ff370SJamie Gritton goto done_free; 585b38ff370SJamie Gritton 586b38ff370SJamie Gritton error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel)); 587b38ff370SJamie Gritton if (error == ENOENT) 588b38ff370SJamie Gritton gotslevel = 0; 589b38ff370SJamie Gritton else if (error != 0) 590b38ff370SJamie Gritton goto done_free; 591b38ff370SJamie Gritton else 592b38ff370SJamie Gritton gotslevel = 1; 593b38ff370SJamie Gritton 594b97457e2SJamie Gritton error = 595b97457e2SJamie Gritton vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax)); 596b97457e2SJamie Gritton if (error == ENOENT) 597b97457e2SJamie Gritton gotchildmax = 0; 598b97457e2SJamie Gritton else if (error != 0) 599b97457e2SJamie Gritton goto done_free; 600b97457e2SJamie Gritton else 601b97457e2SJamie Gritton gotchildmax = 1; 602b97457e2SJamie Gritton 6030304c731SJamie Gritton error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce)); 604f337198dSJamie Gritton if (error == ENOENT) 605f337198dSJamie Gritton gotenforce = 0; 606f337198dSJamie Gritton else if (error != 0) 6070304c731SJamie Gritton goto done_free; 608f337198dSJamie Gritton else if (enforce < 0 || enforce > 2) { 609f337198dSJamie Gritton error = EINVAL; 610f337198dSJamie Gritton goto done_free; 611f337198dSJamie Gritton } else 612f337198dSJamie Gritton gotenforce = 1; 6130304c731SJamie Gritton 6140cc207a6SMartin Matuska error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum)); 6150cc207a6SMartin Matuska if (error == ENOENT) 6160cc207a6SMartin Matuska gotrsnum = 0; 6170cc207a6SMartin Matuska else if (error != 0) 6180cc207a6SMartin Matuska goto done_free; 6190cc207a6SMartin Matuska else 6200cc207a6SMartin Matuska gotrsnum = 1; 6210cc207a6SMartin Matuska 622b38ff370SJamie Gritton pr_flags = ch_flags = 0; 623672756aaSJamie Gritton for (bf = pr_flag_bool; 624672756aaSJamie Gritton bf < pr_flag_bool + nitems(pr_flag_bool); 625672756aaSJamie Gritton bf++) { 626672756aaSJamie Gritton vfs_flagopt(opts, bf->name, &pr_flags, bf->flag); 627672756aaSJamie Gritton vfs_flagopt(opts, bf->noname, &ch_flags, bf->flag); 6280304c731SJamie Gritton } 629b38ff370SJamie Gritton ch_flags |= pr_flags; 630672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 631672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 632672756aaSJamie Gritton jsf++) { 633672756aaSJamie Gritton error = vfs_copyopt(opts, jsf->name, &jsys, sizeof(jsys)); 6347cbf7213SJamie Gritton if (error == ENOENT) 6357cbf7213SJamie Gritton continue; 6367cbf7213SJamie Gritton if (error != 0) 6377cbf7213SJamie Gritton goto done_free; 6387cbf7213SJamie Gritton switch (jsys) { 6397cbf7213SJamie Gritton case JAIL_SYS_DISABLE: 640672756aaSJamie Gritton if (!jsf->disable) { 6417cbf7213SJamie Gritton error = EINVAL; 6427cbf7213SJamie Gritton goto done_free; 6437cbf7213SJamie Gritton } 644672756aaSJamie Gritton pr_flags |= jsf->disable; 6457cbf7213SJamie Gritton break; 6467cbf7213SJamie Gritton case JAIL_SYS_NEW: 647672756aaSJamie Gritton pr_flags |= jsf->new; 6487cbf7213SJamie Gritton break; 6497cbf7213SJamie Gritton case JAIL_SYS_INHERIT: 6507cbf7213SJamie Gritton break; 6517cbf7213SJamie Gritton default: 6527cbf7213SJamie Gritton error = EINVAL; 6537cbf7213SJamie Gritton goto done_free; 6547cbf7213SJamie Gritton } 655672756aaSJamie Gritton ch_flags |= jsf->new | jsf->disable; 6567cbf7213SJamie Gritton } 6574affa14cSJamie Gritton if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE 6584affa14cSJamie Gritton && !(pr_flags & PR_PERSIST)) { 6594affa14cSJamie Gritton error = EINVAL; 6604affa14cSJamie Gritton vfs_opterror(opts, "new jail must persist or attach"); 6614affa14cSJamie Gritton goto done_errmsg; 6624affa14cSJamie Gritton } 663679e1390SJamie Gritton #ifdef VIMAGE 664679e1390SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) { 665679e1390SJamie Gritton error = EINVAL; 666679e1390SJamie Gritton vfs_opterror(opts, "vnet cannot be changed after creation"); 667679e1390SJamie Gritton goto done_errmsg; 668679e1390SJamie Gritton } 669679e1390SJamie Gritton #endif 6702b0d6f81SJamie Gritton #ifdef INET 6712b0d6f81SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) { 6722b0d6f81SJamie Gritton error = EINVAL; 6732b0d6f81SJamie Gritton vfs_opterror(opts, "ip4 cannot be changed after creation"); 6742b0d6f81SJamie Gritton goto done_errmsg; 6752b0d6f81SJamie Gritton } 6762b0d6f81SJamie Gritton #endif 6772b0d6f81SJamie Gritton #ifdef INET6 6782b0d6f81SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) { 6792b0d6f81SJamie Gritton error = EINVAL; 6802b0d6f81SJamie Gritton vfs_opterror(opts, "ip6 cannot be changed after creation"); 6812b0d6f81SJamie Gritton goto done_errmsg; 6822b0d6f81SJamie Gritton } 6832b0d6f81SJamie Gritton #endif 684b38ff370SJamie Gritton 6850304c731SJamie Gritton pr_allow = ch_allow = 0; 686672756aaSJamie Gritton for (bf = pr_flag_allow; 6870e5c6bd4SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && bf->flag != 0; 688672756aaSJamie Gritton bf++) { 689672756aaSJamie Gritton vfs_flagopt(opts, bf->name, &pr_allow, bf->flag); 690672756aaSJamie Gritton vfs_flagopt(opts, bf->noname, &ch_allow, bf->flag); 6910304c731SJamie Gritton } 6920304c731SJamie Gritton ch_allow |= pr_allow; 6930304c731SJamie Gritton 694b38ff370SJamie Gritton error = vfs_getopt(opts, "name", (void **)&name, &len); 695b38ff370SJamie Gritton if (error == ENOENT) 696b38ff370SJamie Gritton name = NULL; 697b38ff370SJamie Gritton else if (error != 0) 698b38ff370SJamie Gritton goto done_free; 699b38ff370SJamie Gritton else { 700b38ff370SJamie Gritton if (len == 0 || name[len - 1] != '\0') { 701b38ff370SJamie Gritton error = EINVAL; 702b38ff370SJamie Gritton goto done_free; 703b38ff370SJamie Gritton } 704b38ff370SJamie Gritton if (len > MAXHOSTNAMELEN) { 705b38ff370SJamie Gritton error = ENAMETOOLONG; 706b38ff370SJamie Gritton goto done_free; 707b38ff370SJamie Gritton } 708b38ff370SJamie Gritton } 709b38ff370SJamie Gritton 710b38ff370SJamie Gritton error = vfs_getopt(opts, "host.hostname", (void **)&host, &len); 711b38ff370SJamie Gritton if (error == ENOENT) 712b38ff370SJamie Gritton host = NULL; 713b38ff370SJamie Gritton else if (error != 0) 714b38ff370SJamie Gritton goto done_free; 715b38ff370SJamie Gritton else { 71676ca6f88SJamie Gritton ch_flags |= PR_HOST; 71776ca6f88SJamie Gritton pr_flags |= PR_HOST; 718b38ff370SJamie Gritton if (len == 0 || host[len - 1] != '\0') { 719b38ff370SJamie Gritton error = EINVAL; 720b38ff370SJamie Gritton goto done_free; 721b38ff370SJamie Gritton } 722b38ff370SJamie Gritton if (len > MAXHOSTNAMELEN) { 723b38ff370SJamie Gritton error = ENAMETOOLONG; 724b38ff370SJamie Gritton goto done_free; 725b38ff370SJamie Gritton } 726b38ff370SJamie Gritton } 727b38ff370SJamie Gritton 72876ca6f88SJamie Gritton error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len); 72976ca6f88SJamie Gritton if (error == ENOENT) 73076ca6f88SJamie Gritton domain = NULL; 73176ca6f88SJamie Gritton else if (error != 0) 73276ca6f88SJamie Gritton goto done_free; 73376ca6f88SJamie Gritton else { 73476ca6f88SJamie Gritton ch_flags |= PR_HOST; 73576ca6f88SJamie Gritton pr_flags |= PR_HOST; 73676ca6f88SJamie Gritton if (len == 0 || domain[len - 1] != '\0') { 73776ca6f88SJamie Gritton error = EINVAL; 73876ca6f88SJamie Gritton goto done_free; 73976ca6f88SJamie Gritton } 74076ca6f88SJamie Gritton if (len > MAXHOSTNAMELEN) { 74176ca6f88SJamie Gritton error = ENAMETOOLONG; 74276ca6f88SJamie Gritton goto done_free; 74376ca6f88SJamie Gritton } 74476ca6f88SJamie Gritton } 74576ca6f88SJamie Gritton 74676ca6f88SJamie Gritton error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len); 74776ca6f88SJamie Gritton if (error == ENOENT) 74876ca6f88SJamie Gritton uuid = NULL; 74976ca6f88SJamie Gritton else if (error != 0) 75076ca6f88SJamie Gritton goto done_free; 75176ca6f88SJamie Gritton else { 75276ca6f88SJamie Gritton ch_flags |= PR_HOST; 75376ca6f88SJamie Gritton pr_flags |= PR_HOST; 75476ca6f88SJamie Gritton if (len == 0 || uuid[len - 1] != '\0') { 75576ca6f88SJamie Gritton error = EINVAL; 75676ca6f88SJamie Gritton goto done_free; 75776ca6f88SJamie Gritton } 75876ca6f88SJamie Gritton if (len > HOSTUUIDLEN) { 75976ca6f88SJamie Gritton error = ENAMETOOLONG; 76076ca6f88SJamie Gritton goto done_free; 76176ca6f88SJamie Gritton } 76276ca6f88SJamie Gritton } 76376ca6f88SJamie Gritton 764841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32 765a5c1afadSDmitry Chagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 76676ca6f88SJamie Gritton uint32_t hid32; 76776ca6f88SJamie Gritton 76876ca6f88SJamie Gritton error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32)); 76976ca6f88SJamie Gritton hid = hid32; 77076ca6f88SJamie Gritton } else 77176ca6f88SJamie Gritton #endif 77276ca6f88SJamie Gritton error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid)); 77376ca6f88SJamie Gritton if (error == ENOENT) 77476ca6f88SJamie Gritton gothid = 0; 77576ca6f88SJamie Gritton else if (error != 0) 77676ca6f88SJamie Gritton goto done_free; 77776ca6f88SJamie Gritton else { 77876ca6f88SJamie Gritton gothid = 1; 77976ca6f88SJamie Gritton ch_flags |= PR_HOST; 78076ca6f88SJamie Gritton pr_flags |= PR_HOST; 78176ca6f88SJamie Gritton } 78276ca6f88SJamie Gritton 783b38ff370SJamie Gritton #ifdef INET 784b38ff370SJamie Gritton error = vfs_getopt(opts, "ip4.addr", &op, &ip4s); 785b38ff370SJamie Gritton if (error == ENOENT) 7860e5e396eSJamie Gritton ip4s = 0; 787b38ff370SJamie Gritton else if (error != 0) 788b38ff370SJamie Gritton goto done_free; 789b38ff370SJamie Gritton else if (ip4s & (sizeof(*ip4) - 1)) { 790b38ff370SJamie Gritton error = EINVAL; 791b38ff370SJamie Gritton goto done_free; 7920304c731SJamie Gritton } else { 7936a3f2779SJamie Gritton ch_flags |= PR_IP4_USER; 7946a3f2779SJamie Gritton pr_flags |= PR_IP4_USER; 7956a3f2779SJamie Gritton if (ip4s > 0) { 796b38ff370SJamie Gritton ip4s /= sizeof(*ip4); 797b38ff370SJamie Gritton if (ip4s > jail_max_af_ips) { 798b38ff370SJamie Gritton error = EINVAL; 799b38ff370SJamie Gritton vfs_opterror(opts, "too many IPv4 addresses"); 800b38ff370SJamie Gritton goto done_errmsg; 801b38ff370SJamie Gritton } 8022b0d6f81SJamie Gritton ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 803b38ff370SJamie Gritton bcopy(op, ip4, ip4s * sizeof(*ip4)); 804b38ff370SJamie Gritton /* 8050304c731SJamie Gritton * IP addresses are all sorted but ip[0] to preserve 8060304c731SJamie Gritton * the primary IP address as given from userland. 8070304c731SJamie Gritton * This special IP is used for unbound outgoing 808bef916f9SBjoern A. Zeeb * connections as well for "loopback" traffic in case 809bef916f9SBjoern A. Zeeb * source address selection cannot find any more fitting 810bef916f9SBjoern A. Zeeb * address to connect from. 811b38ff370SJamie Gritton */ 812b38ff370SJamie Gritton if (ip4s > 1) 8130ce1624dSStephen J. Kiernan qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), 8140ce1624dSStephen J. Kiernan prison_qcmp_v4); 815b38ff370SJamie Gritton /* 8160304c731SJamie Gritton * Check for duplicate addresses and do some simple 8170304c731SJamie Gritton * zero and broadcast checks. If users give other bogus 8180304c731SJamie Gritton * addresses it is their problem. 819b38ff370SJamie Gritton * 8200304c731SJamie Gritton * We do not have to care about byte order for these 8210304c731SJamie Gritton * checks so we will do them in NBO. 822b38ff370SJamie Gritton */ 823b38ff370SJamie Gritton for (ii = 0; ii < ip4s; ii++) { 824b38ff370SJamie Gritton if (ip4[ii].s_addr == INADDR_ANY || 825b38ff370SJamie Gritton ip4[ii].s_addr == INADDR_BROADCAST) { 826b38ff370SJamie Gritton error = EINVAL; 827b38ff370SJamie Gritton goto done_free; 828b38ff370SJamie Gritton } 829b38ff370SJamie Gritton if ((ii+1) < ip4s && 830b38ff370SJamie Gritton (ip4[0].s_addr == ip4[ii+1].s_addr || 831b38ff370SJamie Gritton ip4[ii].s_addr == ip4[ii+1].s_addr)) { 832b38ff370SJamie Gritton error = EINVAL; 833b38ff370SJamie Gritton goto done_free; 834b38ff370SJamie Gritton } 835b38ff370SJamie Gritton } 836b38ff370SJamie Gritton } 8370304c731SJamie Gritton } 838b38ff370SJamie Gritton #endif 839b38ff370SJamie Gritton 840b38ff370SJamie Gritton #ifdef INET6 841b38ff370SJamie Gritton error = vfs_getopt(opts, "ip6.addr", &op, &ip6s); 842b38ff370SJamie Gritton if (error == ENOENT) 8430e5e396eSJamie Gritton ip6s = 0; 844b38ff370SJamie Gritton else if (error != 0) 845b38ff370SJamie Gritton goto done_free; 846b38ff370SJamie Gritton else if (ip6s & (sizeof(*ip6) - 1)) { 847b38ff370SJamie Gritton error = EINVAL; 848b38ff370SJamie Gritton goto done_free; 8490304c731SJamie Gritton } else { 8506a3f2779SJamie Gritton ch_flags |= PR_IP6_USER; 8516a3f2779SJamie Gritton pr_flags |= PR_IP6_USER; 8526a3f2779SJamie Gritton if (ip6s > 0) { 853b38ff370SJamie Gritton ip6s /= sizeof(*ip6); 854b38ff370SJamie Gritton if (ip6s > jail_max_af_ips) { 855b38ff370SJamie Gritton error = EINVAL; 856b38ff370SJamie Gritton vfs_opterror(opts, "too many IPv6 addresses"); 857b38ff370SJamie Gritton goto done_errmsg; 858b38ff370SJamie Gritton } 8592b0d6f81SJamie Gritton ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 860b38ff370SJamie Gritton bcopy(op, ip6, ip6s * sizeof(*ip6)); 861b38ff370SJamie Gritton if (ip6s > 1) 8620ce1624dSStephen J. Kiernan qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), 8630ce1624dSStephen J. Kiernan prison_qcmp_v6); 864b38ff370SJamie Gritton for (ii = 0; ii < ip6s; ii++) { 8650304c731SJamie Gritton if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) { 866b38ff370SJamie Gritton error = EINVAL; 867b38ff370SJamie Gritton goto done_free; 868b38ff370SJamie Gritton } 869b38ff370SJamie Gritton if ((ii+1) < ip6s && 870b38ff370SJamie Gritton (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) || 871b38ff370SJamie Gritton IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1]))) 872b38ff370SJamie Gritton { 873b38ff370SJamie Gritton error = EINVAL; 874b38ff370SJamie Gritton goto done_free; 875b38ff370SJamie Gritton } 876b38ff370SJamie Gritton } 877b38ff370SJamie Gritton } 8780304c731SJamie Gritton } 879b38ff370SJamie Gritton #endif 880b38ff370SJamie Gritton 881bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6)) 882bdfc8cc4SJamie Gritton if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 883bdfc8cc4SJamie Gritton error = EINVAL; 884bdfc8cc4SJamie Gritton vfs_opterror(opts, 885bdfc8cc4SJamie Gritton "vnet jails cannot have IP address restrictions"); 886bdfc8cc4SJamie Gritton goto done_errmsg; 887bdfc8cc4SJamie Gritton } 888bdfc8cc4SJamie Gritton #endif 889bdfc8cc4SJamie Gritton 890cf0313c6SJamie Gritton error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len); 891cf0313c6SJamie Gritton if (error == ENOENT) 892cf0313c6SJamie Gritton osrelstr = NULL; 893cf0313c6SJamie Gritton else if (error != 0) 894cf0313c6SJamie Gritton goto done_free; 895cf0313c6SJamie Gritton else { 896cf0313c6SJamie Gritton if (flags & JAIL_UPDATE) { 897cf0313c6SJamie Gritton error = EINVAL; 898cf0313c6SJamie Gritton vfs_opterror(opts, 899cf0313c6SJamie Gritton "osrelease cannot be changed after creation"); 900cf0313c6SJamie Gritton goto done_errmsg; 901cf0313c6SJamie Gritton } 9021b786d01SBjoern A. Zeeb if (len == 0 || osrelstr[len - 1] != '\0') { 903cf0313c6SJamie Gritton error = EINVAL; 9041b786d01SBjoern A. Zeeb goto done_free; 9051b786d01SBjoern A. Zeeb } 9061b786d01SBjoern A. Zeeb if (len >= OSRELEASELEN) { 9071b786d01SBjoern A. Zeeb error = ENAMETOOLONG; 908cf0313c6SJamie Gritton vfs_opterror(opts, 909cf0313c6SJamie Gritton "osrelease string must be 1-%d bytes long", 910cf0313c6SJamie Gritton OSRELEASELEN - 1); 911cf0313c6SJamie Gritton goto done_errmsg; 912cf0313c6SJamie Gritton } 913cf0313c6SJamie Gritton } 914cf0313c6SJamie Gritton 915cf0313c6SJamie Gritton error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt)); 916cf0313c6SJamie Gritton if (error == ENOENT) 917cf0313c6SJamie Gritton osreldt = 0; 918cf0313c6SJamie Gritton else if (error != 0) 919cf0313c6SJamie Gritton goto done_free; 920cf0313c6SJamie Gritton else { 921cf0313c6SJamie Gritton if (flags & JAIL_UPDATE) { 922cf0313c6SJamie Gritton error = EINVAL; 923cf0313c6SJamie Gritton vfs_opterror(opts, 924cf0313c6SJamie Gritton "osreldate cannot be changed after creation"); 925cf0313c6SJamie Gritton goto done_errmsg; 926cf0313c6SJamie Gritton } 927cf0313c6SJamie Gritton if (osreldt == 0) { 928cf0313c6SJamie Gritton error = EINVAL; 929cf0313c6SJamie Gritton vfs_opterror(opts, "osreldate cannot be 0"); 930cf0313c6SJamie Gritton goto done_errmsg; 931cf0313c6SJamie Gritton } 932cf0313c6SJamie Gritton } 933cf0313c6SJamie Gritton 934b38ff370SJamie Gritton root = NULL; 935b38ff370SJamie Gritton error = vfs_getopt(opts, "path", (void **)&path, &len); 936b38ff370SJamie Gritton if (error == ENOENT) 937b38ff370SJamie Gritton path = NULL; 938b38ff370SJamie Gritton else if (error != 0) 939b38ff370SJamie Gritton goto done_free; 940b38ff370SJamie Gritton else { 941b38ff370SJamie Gritton if (flags & JAIL_UPDATE) { 942b38ff370SJamie Gritton error = EINVAL; 943b38ff370SJamie Gritton vfs_opterror(opts, 944b38ff370SJamie Gritton "path cannot be changed after creation"); 945b38ff370SJamie Gritton goto done_errmsg; 946b38ff370SJamie Gritton } 947b38ff370SJamie Gritton if (len == 0 || path[len - 1] != '\0') { 948b38ff370SJamie Gritton error = EINVAL; 949b38ff370SJamie Gritton goto done_free; 950b38ff370SJamie Gritton } 9515050aa86SKonstantin Belousov NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, 952b38ff370SJamie Gritton path, td); 953b38ff370SJamie Gritton error = namei(&nd); 954b38ff370SJamie Gritton if (error) 955b38ff370SJamie Gritton goto done_free; 956b38ff370SJamie Gritton root = nd.ni_vp; 957b38ff370SJamie Gritton NDFREE(&nd, NDF_ONLY_PNBUF); 9586dfe0a3dSMartin Matuska g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 9596dfe0a3dSMartin Matuska strlcpy(g_path, path, MAXPATHLEN); 9606dfe0a3dSMartin Matuska error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN); 9613eb6b656SMateusz Guzik if (error == 0) { 9626dfe0a3dSMartin Matuska path = g_path; 9636dfe0a3dSMartin Matuska } else { 964f6e633a9SMartin Matuska /* exit on other errors */ 965b38ff370SJamie Gritton goto done_free; 966b38ff370SJamie Gritton } 967f6e633a9SMartin Matuska if (root->v_type != VDIR) { 968f6e633a9SMartin Matuska error = ENOTDIR; 969f6e633a9SMartin Matuska vput(root); 970f6e633a9SMartin Matuska goto done_free; 971f6e633a9SMartin Matuska } 972b249ce48SMateusz Guzik VOP_UNLOCK(root); 973b38ff370SJamie Gritton } 974b38ff370SJamie Gritton 975b38ff370SJamie Gritton /* 976b6f47c23SJamie Gritton * Find the specified jail, or at least its parent. 977b38ff370SJamie Gritton * This abuses the file error codes ENOENT and EEXIST. 978b38ff370SJamie Gritton */ 979b38ff370SJamie Gritton pr = NULL; 980b6f47c23SJamie Gritton ppr = mypr; 981*7de883c8SJamie Gritton inspr = NULL; 982babbbb9cSJamie Gritton if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) { 983babbbb9cSJamie Gritton namelc = strrchr(name, '.'); 984babbbb9cSJamie Gritton jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10); 985babbbb9cSJamie Gritton if (*p != '\0') 986babbbb9cSJamie Gritton jid = 0; 987babbbb9cSJamie Gritton } 988b6f47c23SJamie Gritton sx_xlock(&allprison_lock); 989b38ff370SJamie Gritton if (jid != 0) { 990b38ff370SJamie Gritton if (jid < 0) { 991b38ff370SJamie Gritton error = EINVAL; 992b38ff370SJamie Gritton vfs_opterror(opts, "negative jid"); 993b38ff370SJamie Gritton goto done_unlock_list; 994b38ff370SJamie Gritton } 995*7de883c8SJamie Gritton /* 996*7de883c8SJamie Gritton * See if a requested jid already exists. Keep track of 997*7de883c8SJamie Gritton * where it can be inserted later. 998*7de883c8SJamie Gritton */ 999*7de883c8SJamie Gritton TAILQ_FOREACH(inspr, &allprison, pr_list) { 1000*7de883c8SJamie Gritton if (inspr->pr_id == jid) { 1001*7de883c8SJamie Gritton mtx_lock(&inspr->pr_mtx); 1002*7de883c8SJamie Gritton if (inspr->pr_ref > 0) { 1003*7de883c8SJamie Gritton pr = inspr; 1004*7de883c8SJamie Gritton inspr = NULL; 1005*7de883c8SJamie Gritton } else 1006*7de883c8SJamie Gritton mtx_unlock(&inspr->pr_mtx); 1007*7de883c8SJamie Gritton break; 1008*7de883c8SJamie Gritton } 1009*7de883c8SJamie Gritton if (inspr->pr_id > jid) 1010*7de883c8SJamie Gritton break; 1011*7de883c8SJamie Gritton } 1012b38ff370SJamie Gritton if (pr != NULL) { 10130304c731SJamie Gritton ppr = pr->pr_parent; 1014b38ff370SJamie Gritton /* Create: jid must not exist. */ 1015b38ff370SJamie Gritton if (cuflags == JAIL_CREATE) { 1016*7de883c8SJamie Gritton /* 1017*7de883c8SJamie Gritton * Even creators that cannot see the jail will 1018*7de883c8SJamie Gritton * get EEXIST. 1019*7de883c8SJamie Gritton */ 1020b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1021b38ff370SJamie Gritton error = EEXIST; 1022b38ff370SJamie Gritton vfs_opterror(opts, "jail %d already exists", 1023b38ff370SJamie Gritton jid); 1024b38ff370SJamie Gritton goto done_unlock_list; 1025b38ff370SJamie Gritton } 10260304c731SJamie Gritton if (!prison_ischild(mypr, pr)) { 1027*7de883c8SJamie Gritton /* 1028*7de883c8SJamie Gritton * Updaters get ENOENT if they cannot see the 1029*7de883c8SJamie Gritton * jail. This is true even for CREATE | UPDATE, 1030*7de883c8SJamie Gritton * which normally cannot give this error. 1031*7de883c8SJamie Gritton */ 10320304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 10330304c731SJamie Gritton pr = NULL; 10340304c731SJamie Gritton } else if (pr->pr_uref == 0) { 1035b38ff370SJamie Gritton if (!(flags & JAIL_DYING)) { 1036b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1037b38ff370SJamie Gritton error = ENOENT; 1038b38ff370SJamie Gritton vfs_opterror(opts, "jail %d is dying", 1039b38ff370SJamie Gritton jid); 1040b38ff370SJamie Gritton goto done_unlock_list; 1041b38ff370SJamie Gritton } else if ((flags & JAIL_ATTACH) || 1042b38ff370SJamie Gritton (pr_flags & PR_PERSIST)) { 1043b38ff370SJamie Gritton /* 1044b38ff370SJamie Gritton * A dying jail might be resurrected 1045b38ff370SJamie Gritton * (via attach or persist), but first 1046b38ff370SJamie Gritton * it must determine if another jail 1047b38ff370SJamie Gritton * has claimed its name. Accomplish 1048b38ff370SJamie Gritton * this by implicitly re-setting the 1049b38ff370SJamie Gritton * name. 1050b38ff370SJamie Gritton */ 1051b38ff370SJamie Gritton if (name == NULL) 10520304c731SJamie Gritton name = prison_name(mypr, pr); 1053b38ff370SJamie Gritton } 1054b38ff370SJamie Gritton } 1055b38ff370SJamie Gritton } 1056b38ff370SJamie Gritton if (pr == NULL) { 1057b38ff370SJamie Gritton /* Update: jid must exist. */ 1058b38ff370SJamie Gritton if (cuflags == JAIL_UPDATE) { 1059b38ff370SJamie Gritton error = ENOENT; 1060b38ff370SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 1061b38ff370SJamie Gritton goto done_unlock_list; 1062b38ff370SJamie Gritton } 1063b38ff370SJamie Gritton } 1064b38ff370SJamie Gritton } 1065b38ff370SJamie Gritton /* 1066b38ff370SJamie Gritton * If the caller provided a name, look for a jail by that name. 1067b38ff370SJamie Gritton * This has different semantics for creates and updates keyed by jid 1068b38ff370SJamie Gritton * (where the name must not already exist in a different jail), 1069b38ff370SJamie Gritton * and updates keyed by the name itself (where the name must exist 1070b38ff370SJamie Gritton * because that is the jail being updated). 1071b38ff370SJamie Gritton */ 1072b6f47c23SJamie Gritton namelc = NULL; 1073b38ff370SJamie Gritton if (name != NULL) { 1074babbbb9cSJamie Gritton namelc = strrchr(name, '.'); 1075babbbb9cSJamie Gritton if (namelc == NULL) 1076babbbb9cSJamie Gritton namelc = name; 1077babbbb9cSJamie Gritton else { 10780304c731SJamie Gritton /* 10790304c731SJamie Gritton * This is a hierarchical name. Split it into the 10800304c731SJamie Gritton * parent and child names, and make sure the parent 10810304c731SJamie Gritton * exists or matches an already found jail. 10820304c731SJamie Gritton */ 10830304c731SJamie Gritton if (pr != NULL) { 1084babbbb9cSJamie Gritton if (strncmp(name, ppr->pr_name, namelc - name) 1085babbbb9cSJamie Gritton || ppr->pr_name[namelc - name] != '\0') { 10860304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 10870304c731SJamie Gritton error = EINVAL; 10880304c731SJamie Gritton vfs_opterror(opts, 10890304c731SJamie Gritton "cannot change jail's parent"); 10900304c731SJamie Gritton goto done_unlock_list; 10910304c731SJamie Gritton } 10920304c731SJamie Gritton } else { 1093b6f47c23SJamie Gritton *namelc = '\0'; 10940304c731SJamie Gritton ppr = prison_find_name(mypr, name); 10950304c731SJamie Gritton if (ppr == NULL) { 10960304c731SJamie Gritton error = ENOENT; 10970304c731SJamie Gritton vfs_opterror(opts, 10980304c731SJamie Gritton "jail \"%s\" not found", name); 10990304c731SJamie Gritton goto done_unlock_list; 11000304c731SJamie Gritton } 11010304c731SJamie Gritton mtx_unlock(&ppr->pr_mtx); 1102b6f47c23SJamie Gritton *namelc = '.'; 11030304c731SJamie Gritton } 1104b6f47c23SJamie Gritton namelc++; 11050304c731SJamie Gritton } 1106b6f47c23SJamie Gritton if (namelc[0] != '\0') { 1107b6f47c23SJamie Gritton pnamelen = 11080304c731SJamie Gritton (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 1109b38ff370SJamie Gritton name_again: 11100304c731SJamie Gritton deadpr = NULL; 11110304c731SJamie Gritton FOREACH_PRISON_CHILD(ppr, tpr) { 1112b38ff370SJamie Gritton if (tpr != pr && tpr->pr_ref > 0 && 1113b6f47c23SJamie Gritton !strcmp(tpr->pr_name + pnamelen, namelc)) { 1114b38ff370SJamie Gritton if (pr == NULL && 1115b38ff370SJamie Gritton cuflags != JAIL_CREATE) { 1116b38ff370SJamie Gritton mtx_lock(&tpr->pr_mtx); 1117b38ff370SJamie Gritton if (tpr->pr_ref > 0) { 1118b38ff370SJamie Gritton /* 1119b38ff370SJamie Gritton * Use this jail 1120b38ff370SJamie Gritton * for updates. 1121b38ff370SJamie Gritton */ 1122b38ff370SJamie Gritton if (tpr->pr_uref > 0) { 1123b38ff370SJamie Gritton pr = tpr; 1124b38ff370SJamie Gritton break; 1125b38ff370SJamie Gritton } 1126b38ff370SJamie Gritton deadpr = tpr; 1127b38ff370SJamie Gritton } 1128b38ff370SJamie Gritton mtx_unlock(&tpr->pr_mtx); 1129b38ff370SJamie Gritton } else if (tpr->pr_uref > 0) { 1130b38ff370SJamie Gritton /* 1131b38ff370SJamie Gritton * Create, or update(jid): 1132b38ff370SJamie Gritton * name must not exist in an 11330304c731SJamie Gritton * active sibling jail. 1134b38ff370SJamie Gritton */ 1135b38ff370SJamie Gritton error = EEXIST; 1136b38ff370SJamie Gritton if (pr != NULL) 1137b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1138b38ff370SJamie Gritton vfs_opterror(opts, 1139b38ff370SJamie Gritton "jail \"%s\" already exists", 1140b38ff370SJamie Gritton name); 1141b38ff370SJamie Gritton goto done_unlock_list; 1142b38ff370SJamie Gritton } 1143b38ff370SJamie Gritton } 1144b38ff370SJamie Gritton } 1145b38ff370SJamie Gritton /* If no active jail is found, use a dying one. */ 1146b38ff370SJamie Gritton if (deadpr != NULL && pr == NULL) { 1147b38ff370SJamie Gritton if (flags & JAIL_DYING) { 1148b38ff370SJamie Gritton mtx_lock(&deadpr->pr_mtx); 1149b38ff370SJamie Gritton if (deadpr->pr_ref == 0) { 1150b38ff370SJamie Gritton mtx_unlock(&deadpr->pr_mtx); 1151b38ff370SJamie Gritton goto name_again; 1152b38ff370SJamie Gritton } 1153b38ff370SJamie Gritton pr = deadpr; 1154b38ff370SJamie Gritton } else if (cuflags == JAIL_UPDATE) { 1155b38ff370SJamie Gritton error = ENOENT; 1156b38ff370SJamie Gritton vfs_opterror(opts, 1157b38ff370SJamie Gritton "jail \"%s\" is dying", name); 1158b38ff370SJamie Gritton goto done_unlock_list; 1159b38ff370SJamie Gritton } 1160b38ff370SJamie Gritton } 1161b38ff370SJamie Gritton /* Update: name must exist if no jid. */ 1162b38ff370SJamie Gritton else if (cuflags == JAIL_UPDATE && pr == NULL) { 1163b38ff370SJamie Gritton error = ENOENT; 1164b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", 1165b38ff370SJamie Gritton name); 1166b38ff370SJamie Gritton goto done_unlock_list; 1167b38ff370SJamie Gritton } 1168b38ff370SJamie Gritton } 1169b38ff370SJamie Gritton } 1170b38ff370SJamie Gritton /* Update: must provide a jid or name. */ 1171b38ff370SJamie Gritton else if (cuflags == JAIL_UPDATE && pr == NULL) { 1172b38ff370SJamie Gritton error = ENOENT; 1173b38ff370SJamie Gritton vfs_opterror(opts, "update specified no jail"); 1174b38ff370SJamie Gritton goto done_unlock_list; 1175b38ff370SJamie Gritton } 1176b38ff370SJamie Gritton 1177b38ff370SJamie Gritton /* If there's no prison to update, create a new one and link it in. */ 1178b38ff370SJamie Gritton if (pr == NULL) { 1179b97457e2SJamie Gritton for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent) 1180b97457e2SJamie Gritton if (tpr->pr_childcount >= tpr->pr_childmax) { 1181b97457e2SJamie Gritton error = EPERM; 1182b97457e2SJamie Gritton vfs_opterror(opts, "prison limit exceeded"); 1183b97457e2SJamie Gritton goto done_unlock_list; 1184b97457e2SJamie Gritton } 1185b38ff370SJamie Gritton created = 1; 11860304c731SJamie Gritton mtx_lock(&ppr->pr_mtx); 11872a549507SJamie Gritton if (ppr->pr_ref == 0) { 11880304c731SJamie Gritton mtx_unlock(&ppr->pr_mtx); 11890304c731SJamie Gritton error = ENOENT; 1190b6f47c23SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", 1191b6f47c23SJamie Gritton prison_name(mypr, ppr)); 11920304c731SJamie Gritton goto done_unlock_list; 11930304c731SJamie Gritton } 11940304c731SJamie Gritton ppr->pr_ref++; 11950304c731SJamie Gritton ppr->pr_uref++; 11960304c731SJamie Gritton mtx_unlock(&ppr->pr_mtx); 1197b38ff370SJamie Gritton pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO); 1198*7de883c8SJamie Gritton 1199*7de883c8SJamie Gritton if (jid == 0 && (jid = get_next_prid(&inspr)) == 0) { 1200b38ff370SJamie Gritton error = EAGAIN; 1201*7de883c8SJamie Gritton vfs_opterror(opts, "no available jail IDs"); 1202b38ff370SJamie Gritton free(pr, M_PRISON); 1203*7de883c8SJamie Gritton prison_deref(ppr, 1204*7de883c8SJamie Gritton PD_DEREF | PD_DEUREF | PD_LIST_XLOCKED); 12050304c731SJamie Gritton goto done_releroot; 1206b38ff370SJamie Gritton } 1207*7de883c8SJamie Gritton pr->pr_id = jid; 1208*7de883c8SJamie Gritton if (inspr != NULL) 1209*7de883c8SJamie Gritton TAILQ_INSERT_BEFORE(inspr, pr, pr_list); 1210*7de883c8SJamie Gritton else 1211b38ff370SJamie Gritton TAILQ_INSERT_TAIL(&allprison, pr, pr_list); 1212*7de883c8SJamie Gritton 1213*7de883c8SJamie Gritton pr->pr_parent = ppr; 12140304c731SJamie Gritton LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling); 12150304c731SJamie Gritton for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 1216b97457e2SJamie Gritton tpr->pr_childcount++; 1217b38ff370SJamie Gritton 12180304c731SJamie Gritton /* Set some default values, and inherit some from the parent. */ 1219b6f47c23SJamie Gritton if (namelc == NULL) 1220b6f47c23SJamie Gritton namelc = ""; 1221b38ff370SJamie Gritton if (path == NULL) { 1222b38ff370SJamie Gritton path = "/"; 12230304c731SJamie Gritton root = mypr->pr_root; 1224b38ff370SJamie Gritton vref(root); 1225b38ff370SJamie Gritton } 12268986e3a0SJamie Gritton strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN); 12278986e3a0SJamie Gritton pr->pr_flags |= PR_HOST; 1228bdfc8cc4SJamie Gritton #if defined(INET) || defined(INET6) 1229bdfc8cc4SJamie Gritton #ifdef VIMAGE 1230bdfc8cc4SJamie Gritton if (!(pr_flags & PR_VNET)) 1231bdfc8cc4SJamie Gritton #endif 1232bdfc8cc4SJamie Gritton { 12330304c731SJamie Gritton #ifdef INET 12342b0d6f81SJamie Gritton if (!(ch_flags & PR_IP4_USER)) 12356a3f2779SJamie Gritton pr->pr_flags |= PR_IP4 | PR_IP4_USER; 12362b0d6f81SJamie Gritton else if (!(pr_flags & PR_IP4_USER)) { 12372b0d6f81SJamie Gritton pr->pr_flags |= ppr->pr_flags & PR_IP4; 12382b0d6f81SJamie Gritton if (ppr->pr_ip4 != NULL) { 12392b0d6f81SJamie Gritton pr->pr_ip4s = ppr->pr_ip4s; 12402b0d6f81SJamie Gritton pr->pr_ip4 = malloc(pr->pr_ip4s * 12412b0d6f81SJamie Gritton sizeof(struct in_addr), M_PRISON, 12422b0d6f81SJamie Gritton M_WAITOK); 12432b0d6f81SJamie Gritton bcopy(ppr->pr_ip4, pr->pr_ip4, 12442b0d6f81SJamie Gritton pr->pr_ip4s * sizeof(*pr->pr_ip4)); 12452b0d6f81SJamie Gritton } 12462b0d6f81SJamie Gritton } 12470304c731SJamie Gritton #endif 12480304c731SJamie Gritton #ifdef INET6 12492b0d6f81SJamie Gritton if (!(ch_flags & PR_IP6_USER)) 12506a3f2779SJamie Gritton pr->pr_flags |= PR_IP6 | PR_IP6_USER; 12512b0d6f81SJamie Gritton else if (!(pr_flags & PR_IP6_USER)) { 12522b0d6f81SJamie Gritton pr->pr_flags |= ppr->pr_flags & PR_IP6; 12532b0d6f81SJamie Gritton if (ppr->pr_ip6 != NULL) { 12542b0d6f81SJamie Gritton pr->pr_ip6s = ppr->pr_ip6s; 12552b0d6f81SJamie Gritton pr->pr_ip6 = malloc(pr->pr_ip6s * 12562b0d6f81SJamie Gritton sizeof(struct in6_addr), M_PRISON, 12572b0d6f81SJamie Gritton M_WAITOK); 12582b0d6f81SJamie Gritton bcopy(ppr->pr_ip6, pr->pr_ip6, 12592b0d6f81SJamie Gritton pr->pr_ip6s * sizeof(*pr->pr_ip6)); 12602b0d6f81SJamie Gritton } 12612b0d6f81SJamie Gritton } 12620304c731SJamie Gritton #endif 1263bdfc8cc4SJamie Gritton } 1264bdfc8cc4SJamie Gritton #endif 1265592bcae8SBjoern A. Zeeb /* Source address selection is always on by default. */ 1266592bcae8SBjoern A. Zeeb pr->pr_flags |= _PR_IP_SADDRSEL; 1267592bcae8SBjoern A. Zeeb 12680304c731SJamie Gritton pr->pr_securelevel = ppr->pr_securelevel; 12690304c731SJamie Gritton pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow; 1270af10bf05SBjoern A. Zeeb pr->pr_enforce_statfs = jail_default_enforce_statfs; 1271bf3db8aaSMartin Matuska pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum; 1272b38ff370SJamie Gritton 1273b96bd95bSIan Lepore pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate; 1274b96bd95bSIan Lepore if (osrelstr == NULL) 12751b786d01SBjoern A. Zeeb strlcpy(pr->pr_osrelease, ppr->pr_osrelease, 12761b786d01SBjoern A. Zeeb sizeof(pr->pr_osrelease)); 1277b96bd95bSIan Lepore else 12781b786d01SBjoern A. Zeeb strlcpy(pr->pr_osrelease, osrelstr, 12791b786d01SBjoern A. Zeeb sizeof(pr->pr_osrelease)); 1280b96bd95bSIan Lepore 12810304c731SJamie Gritton LIST_INIT(&pr->pr_children); 12820304c731SJamie Gritton mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK); 128373d9e52dSJamie Gritton TASK_INIT(&pr->pr_task, 0, prison_complete, pr); 1284b38ff370SJamie Gritton 1285679e1390SJamie Gritton #ifdef VIMAGE 1286679e1390SJamie Gritton /* Allocate a new vnet if specified. */ 1287679e1390SJamie Gritton pr->pr_vnet = (pr_flags & PR_VNET) 1288679e1390SJamie Gritton ? vnet_alloc() : ppr->pr_vnet; 1289679e1390SJamie Gritton #endif 1290b38ff370SJamie Gritton /* 1291b38ff370SJamie Gritton * Allocate a dedicated cpuset for each jail. 1292b38ff370SJamie Gritton * Unlike other initial settings, this may return an erorr. 1293b38ff370SJamie Gritton */ 12940304c731SJamie Gritton error = cpuset_create_root(ppr, &pr->pr_cpuset); 1295b38ff370SJamie Gritton if (error) { 1296b38ff370SJamie Gritton prison_deref(pr, PD_LIST_XLOCKED); 1297b38ff370SJamie Gritton goto done_releroot; 1298b38ff370SJamie Gritton } 1299b38ff370SJamie Gritton 1300b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1301b38ff370SJamie Gritton /* 1302b38ff370SJamie Gritton * New prisons do not yet have a reference, because we do not 1303b6f47c23SJamie Gritton * want others to see the incomplete prison once the 1304b38ff370SJamie Gritton * allprison_lock is downgraded. 1305b38ff370SJamie Gritton */ 1306b38ff370SJamie Gritton } else { 1307b38ff370SJamie Gritton created = 0; 13082b0d6f81SJamie Gritton /* 13092b0d6f81SJamie Gritton * Grab a reference for existing prisons, to ensure they 13102b0d6f81SJamie Gritton * continue to exist for the duration of the call. 13112b0d6f81SJamie Gritton */ 13122b0d6f81SJamie Gritton pr->pr_ref++; 1313bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6)) 1314bdfc8cc4SJamie Gritton if ((pr->pr_flags & PR_VNET) && 1315bdfc8cc4SJamie Gritton (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 1316bdfc8cc4SJamie Gritton error = EINVAL; 1317bdfc8cc4SJamie Gritton vfs_opterror(opts, 1318bdfc8cc4SJamie Gritton "vnet jails cannot have IP address restrictions"); 1319bdfc8cc4SJamie Gritton goto done_deref_locked; 1320bdfc8cc4SJamie Gritton } 1321bdfc8cc4SJamie Gritton #endif 13222b0d6f81SJamie Gritton #ifdef INET 13232b0d6f81SJamie Gritton if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 13242b0d6f81SJamie Gritton error = EINVAL; 13252b0d6f81SJamie Gritton vfs_opterror(opts, 13262b0d6f81SJamie Gritton "ip4 cannot be changed after creation"); 13272b0d6f81SJamie Gritton goto done_deref_locked; 13282b0d6f81SJamie Gritton } 13292b0d6f81SJamie Gritton #endif 13302b0d6f81SJamie Gritton #ifdef INET6 13312b0d6f81SJamie Gritton if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 13322b0d6f81SJamie Gritton error = EINVAL; 13332b0d6f81SJamie Gritton vfs_opterror(opts, 13342b0d6f81SJamie Gritton "ip6 cannot be changed after creation"); 13352b0d6f81SJamie Gritton goto done_deref_locked; 13362b0d6f81SJamie Gritton } 13372b0d6f81SJamie Gritton #endif 1338b38ff370SJamie Gritton } 1339b38ff370SJamie Gritton 1340b38ff370SJamie Gritton /* Do final error checking before setting anything. */ 13410304c731SJamie Gritton if (gotslevel) { 13420304c731SJamie Gritton if (slevel < ppr->pr_securelevel) { 13430304c731SJamie Gritton error = EPERM; 13440304c731SJamie Gritton goto done_deref_locked; 13450304c731SJamie Gritton } 13460304c731SJamie Gritton } 1347b97457e2SJamie Gritton if (gotchildmax) { 1348b97457e2SJamie Gritton if (childmax >= ppr->pr_childmax) { 1349b97457e2SJamie Gritton error = EPERM; 1350b97457e2SJamie Gritton goto done_deref_locked; 1351b97457e2SJamie Gritton } 1352b97457e2SJamie Gritton } 13530304c731SJamie Gritton if (gotenforce) { 13540304c731SJamie Gritton if (enforce < ppr->pr_enforce_statfs) { 13550304c731SJamie Gritton error = EPERM; 13560304c731SJamie Gritton goto done_deref_locked; 13570304c731SJamie Gritton } 13580304c731SJamie Gritton } 13590cc207a6SMartin Matuska if (gotrsnum) { 13600cc207a6SMartin Matuska /* 13610cc207a6SMartin Matuska * devfs_rsnum is a uint16_t 13620cc207a6SMartin Matuska */ 1363bf3db8aaSMartin Matuska if (rsnum < 0 || rsnum > 65535) { 13640cc207a6SMartin Matuska error = EINVAL; 13650cc207a6SMartin Matuska goto done_deref_locked; 13660cc207a6SMartin Matuska } 13670cc207a6SMartin Matuska /* 1368bf3db8aaSMartin Matuska * Nested jails always inherit parent's devfs ruleset 13690cc207a6SMartin Matuska */ 13700cc207a6SMartin Matuska if (jailed(td->td_ucred)) { 13710cc207a6SMartin Matuska if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) { 13720cc207a6SMartin Matuska error = EPERM; 13730cc207a6SMartin Matuska goto done_deref_locked; 1374bf3db8aaSMartin Matuska } else 13750cc207a6SMartin Matuska rsnum = ppr->pr_devfs_rsnum; 13760cc207a6SMartin Matuska } 13770cc207a6SMartin Matuska } 1378b38ff370SJamie Gritton #ifdef INET 13792b0d6f81SJamie Gritton if (ip4s > 0) { 13800304c731SJamie Gritton if (ppr->pr_flags & PR_IP4) { 13810304c731SJamie Gritton /* 13820304c731SJamie Gritton * Make sure the new set of IP addresses is a 13830304c731SJamie Gritton * subset of the parent's list. Don't worry 13840304c731SJamie Gritton * about the parent being unlocked, as any 13850304c731SJamie Gritton * setting is done with allprison_lock held. 13860304c731SJamie Gritton */ 13870304c731SJamie Gritton for (ij = 0; ij < ppr->pr_ip4s; ij++) 13882b0d6f81SJamie Gritton if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 13890304c731SJamie Gritton break; 13900304c731SJamie Gritton if (ij == ppr->pr_ip4s) { 13910304c731SJamie Gritton error = EPERM; 13920304c731SJamie Gritton goto done_deref_locked; 13930304c731SJamie Gritton } 13940304c731SJamie Gritton if (ip4s > 1) { 13950304c731SJamie Gritton for (ii = ij = 1; ii < ip4s; ii++) { 13960304c731SJamie Gritton if (ip4[ii].s_addr == 13970304c731SJamie Gritton ppr->pr_ip4[0].s_addr) 1398b38ff370SJamie Gritton continue; 13990304c731SJamie Gritton for (; ij < ppr->pr_ip4s; ij++) 14000304c731SJamie Gritton if (ip4[ii].s_addr == 14010304c731SJamie Gritton ppr->pr_ip4[ij].s_addr) 14020304c731SJamie Gritton break; 14030304c731SJamie Gritton if (ij == ppr->pr_ip4s) 14040304c731SJamie Gritton break; 14050304c731SJamie Gritton } 14060304c731SJamie Gritton if (ij == ppr->pr_ip4s) { 14070304c731SJamie Gritton error = EPERM; 14080304c731SJamie Gritton goto done_deref_locked; 14090304c731SJamie Gritton } 14100304c731SJamie Gritton } 14110304c731SJamie Gritton } 14120304c731SJamie Gritton /* 14130304c731SJamie Gritton * Check for conflicting IP addresses. We permit them 14140304c731SJamie Gritton * if there is no more than one IP on each jail. If 14150304c731SJamie Gritton * there is a duplicate on a jail with more than one 14160304c731SJamie Gritton * IP stop checking and return error. 14170304c731SJamie Gritton */ 1418bdfc8cc4SJamie Gritton #ifdef VIMAGE 141908b43333SJamie Gritton for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent) 1420bdfc8cc4SJamie Gritton if (tppr->pr_flags & PR_VNET) 1421bdfc8cc4SJamie Gritton break; 142208b43333SJamie Gritton #else 142308b43333SJamie Gritton tppr = &prison0; 1424bdfc8cc4SJamie Gritton #endif 1425bdfc8cc4SJamie Gritton FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1426bdfc8cc4SJamie Gritton if (tpr == pr || 1427bdfc8cc4SJamie Gritton #ifdef VIMAGE 14282b0d6f81SJamie Gritton (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1429bdfc8cc4SJamie Gritton #endif 1430bdfc8cc4SJamie Gritton tpr->pr_uref == 0) { 14310304c731SJamie Gritton descend = 0; 14320304c731SJamie Gritton continue; 14330304c731SJamie Gritton } 14340304c731SJamie Gritton if (!(tpr->pr_flags & PR_IP4_USER)) 14350304c731SJamie Gritton continue; 14360304c731SJamie Gritton descend = 0; 14370304c731SJamie Gritton if (tpr->pr_ip4 == NULL || 14380304c731SJamie Gritton (ip4s == 1 && tpr->pr_ip4s == 1)) 14390304c731SJamie Gritton continue; 14400304c731SJamie Gritton for (ii = 0; ii < ip4s; ii++) { 14410ce1624dSStephen J. Kiernan if (prison_check_ip4_locked(tpr, &ip4[ii]) == 14420ce1624dSStephen J. Kiernan 0) { 14430304c731SJamie Gritton error = EADDRINUSE; 1444b38ff370SJamie Gritton vfs_opterror(opts, 1445b38ff370SJamie Gritton "IPv4 addresses clash"); 1446b38ff370SJamie Gritton goto done_deref_locked; 1447b38ff370SJamie Gritton } 14480304c731SJamie Gritton } 14490304c731SJamie Gritton } 14500304c731SJamie Gritton } 1451b38ff370SJamie Gritton #endif 1452b38ff370SJamie Gritton #ifdef INET6 14532b0d6f81SJamie Gritton if (ip6s > 0) { 14540304c731SJamie Gritton if (ppr->pr_flags & PR_IP6) { 14550304c731SJamie Gritton /* 14560304c731SJamie Gritton * Make sure the new set of IP addresses is a 14570304c731SJamie Gritton * subset of the parent's list. 14580304c731SJamie Gritton */ 14590304c731SJamie Gritton for (ij = 0; ij < ppr->pr_ip6s; ij++) 14600304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL(&ip6[0], 14610304c731SJamie Gritton &ppr->pr_ip6[ij])) 14620304c731SJamie Gritton break; 14630304c731SJamie Gritton if (ij == ppr->pr_ip6s) { 14640304c731SJamie Gritton error = EPERM; 14650304c731SJamie Gritton goto done_deref_locked; 14660304c731SJamie Gritton } 14670304c731SJamie Gritton if (ip6s > 1) { 14680304c731SJamie Gritton for (ii = ij = 1; ii < ip6s; ii++) { 14690304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL(&ip6[ii], 14700304c731SJamie Gritton &ppr->pr_ip6[0])) 14710304c731SJamie Gritton continue; 14720304c731SJamie Gritton for (; ij < ppr->pr_ip6s; ij++) 14730304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL( 14742b0d6f81SJamie Gritton &ip6[ii], &ppr->pr_ip6[ij])) 14750304c731SJamie Gritton break; 14760304c731SJamie Gritton if (ij == ppr->pr_ip6s) 14770304c731SJamie Gritton break; 14780304c731SJamie Gritton } 14790304c731SJamie Gritton if (ij == ppr->pr_ip6s) { 14800304c731SJamie Gritton error = EPERM; 14810304c731SJamie Gritton goto done_deref_locked; 14820304c731SJamie Gritton } 14830304c731SJamie Gritton } 14840304c731SJamie Gritton } 14850304c731SJamie Gritton /* Check for conflicting IP addresses. */ 1486bdfc8cc4SJamie Gritton #ifdef VIMAGE 148708b43333SJamie Gritton for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent) 1488bdfc8cc4SJamie Gritton if (tppr->pr_flags & PR_VNET) 1489bdfc8cc4SJamie Gritton break; 149008b43333SJamie Gritton #else 149108b43333SJamie Gritton tppr = &prison0; 1492bdfc8cc4SJamie Gritton #endif 1493bdfc8cc4SJamie Gritton FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1494bdfc8cc4SJamie Gritton if (tpr == pr || 1495bdfc8cc4SJamie Gritton #ifdef VIMAGE 14962b0d6f81SJamie Gritton (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1497bdfc8cc4SJamie Gritton #endif 1498bdfc8cc4SJamie Gritton tpr->pr_uref == 0) { 14990304c731SJamie Gritton descend = 0; 15000304c731SJamie Gritton continue; 15010304c731SJamie Gritton } 15020304c731SJamie Gritton if (!(tpr->pr_flags & PR_IP6_USER)) 15030304c731SJamie Gritton continue; 15040304c731SJamie Gritton descend = 0; 15050304c731SJamie Gritton if (tpr->pr_ip6 == NULL || 15060304c731SJamie Gritton (ip6s == 1 && tpr->pr_ip6s == 1)) 15070304c731SJamie Gritton continue; 15080304c731SJamie Gritton for (ii = 0; ii < ip6s; ii++) { 15090ce1624dSStephen J. Kiernan if (prison_check_ip6_locked(tpr, &ip6[ii]) == 15100ce1624dSStephen J. Kiernan 0) { 15110304c731SJamie Gritton error = EADDRINUSE; 1512b38ff370SJamie Gritton vfs_opterror(opts, 1513b38ff370SJamie Gritton "IPv6 addresses clash"); 1514b38ff370SJamie Gritton goto done_deref_locked; 1515b38ff370SJamie Gritton } 15160304c731SJamie Gritton } 15170304c731SJamie Gritton } 15180304c731SJamie Gritton } 1519b38ff370SJamie Gritton #endif 15200304c731SJamie Gritton onamelen = namelen = 0; 1521b6f47c23SJamie Gritton if (namelc != NULL) { 15226ab6058eSJamie Gritton /* Give a default name of the jid. Also allow the name to be 15236ab6058eSJamie Gritton * explicitly the jid - but not any other number, and only in 15246ab6058eSJamie Gritton * normal form (no leading zero/etc). 15256ab6058eSJamie Gritton */ 1526b6f47c23SJamie Gritton if (namelc[0] == '\0') 1527b6f47c23SJamie Gritton snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid); 15286ab6058eSJamie Gritton else if ((strtoul(namelc, &p, 10) != jid || 15296ab6058eSJamie Gritton namelc[0] < '1' || namelc[0] > '9') && *p == '\0') { 1530b38ff370SJamie Gritton error = EINVAL; 1531babbbb9cSJamie Gritton vfs_opterror(opts, 1532babbbb9cSJamie Gritton "name cannot be numeric (unless it is the jid)"); 15330304c731SJamie Gritton goto done_deref_locked; 1534b38ff370SJamie Gritton } 1535b38ff370SJamie Gritton /* 15360304c731SJamie Gritton * Make sure the name isn't too long for the prison or its 15370304c731SJamie Gritton * children. 1538b38ff370SJamie Gritton */ 1539b6f47c23SJamie Gritton pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 1540b6f47c23SJamie Gritton onamelen = strlen(pr->pr_name + pnamelen); 1541b6f47c23SJamie Gritton namelen = strlen(namelc); 1542b6f47c23SJamie Gritton if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) { 15430304c731SJamie Gritton error = ENAMETOOLONG; 15440304c731SJamie Gritton goto done_deref_locked; 15450304c731SJamie Gritton } 15460304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 15470304c731SJamie Gritton if (strlen(tpr->pr_name) + (namelen - onamelen) >= 15480304c731SJamie Gritton sizeof(pr->pr_name)) { 15490304c731SJamie Gritton error = ENAMETOOLONG; 15500304c731SJamie Gritton goto done_deref_locked; 15510304c731SJamie Gritton } 15520304c731SJamie Gritton } 15530304c731SJamie Gritton } 1554b3079544SJamie Gritton pr_allow_diff = pr_allow & ~ppr->pr_allow; 1555b3079544SJamie Gritton if (pr_allow_diff & ~PR_ALLOW_DIFFERENCES) { 15560304c731SJamie Gritton error = EPERM; 15570304c731SJamie Gritton goto done_deref_locked; 1558b38ff370SJamie Gritton } 1559b38ff370SJamie Gritton 1560b6f47c23SJamie Gritton /* 1561b6f47c23SJamie Gritton * Let modules check their parameters. This requires unlocking and 1562b6f47c23SJamie Gritton * then re-locking the prison, but this is still a valid state as long 1563b6f47c23SJamie Gritton * as allprison_lock remains xlocked. 1564b6f47c23SJamie Gritton */ 1565b6f47c23SJamie Gritton mtx_unlock(&pr->pr_mtx); 1566b6f47c23SJamie Gritton error = osd_jail_call(pr, PR_METHOD_CHECK, opts); 1567b6f47c23SJamie Gritton if (error != 0) { 1568b6f47c23SJamie Gritton prison_deref(pr, created 1569b6f47c23SJamie Gritton ? PD_LIST_XLOCKED 1570b6f47c23SJamie Gritton : PD_DEREF | PD_LIST_XLOCKED); 1571b6f47c23SJamie Gritton goto done_releroot; 1572b6f47c23SJamie Gritton } 1573b6f47c23SJamie Gritton mtx_lock(&pr->pr_mtx); 1574b6f47c23SJamie Gritton 1575b6f47c23SJamie Gritton /* At this point, all valid parameters should have been noted. */ 1576b6f47c23SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 1577b6f47c23SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 1578b6f47c23SJamie Gritton error = EINVAL; 1579b6f47c23SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 1580b6f47c23SJamie Gritton goto done_deref_locked; 1581b6f47c23SJamie Gritton } 1582b6f47c23SJamie Gritton } 1583b6f47c23SJamie Gritton 1584b38ff370SJamie Gritton /* Set the parameters of the prison. */ 1585b38ff370SJamie Gritton #ifdef INET 15860304c731SJamie Gritton redo_ip4 = 0; 15870304c731SJamie Gritton if (pr_flags & PR_IP4_USER) { 15880304c731SJamie Gritton pr->pr_flags |= PR_IP4; 1589b38ff370SJamie Gritton free(pr->pr_ip4, M_PRISON); 15900304c731SJamie Gritton pr->pr_ip4s = ip4s; 1591b38ff370SJamie Gritton pr->pr_ip4 = ip4; 1592b38ff370SJamie Gritton ip4 = NULL; 15930304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1594bdfc8cc4SJamie Gritton #ifdef VIMAGE 1595bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1596bdfc8cc4SJamie Gritton descend = 0; 1597bdfc8cc4SJamie Gritton continue; 1598bdfc8cc4SJamie Gritton } 1599bdfc8cc4SJamie Gritton #endif 16000304c731SJamie Gritton if (prison_restrict_ip4(tpr, NULL)) { 16010304c731SJamie Gritton redo_ip4 = 1; 16020304c731SJamie Gritton descend = 0; 16030304c731SJamie Gritton } 16040304c731SJamie Gritton } 16050304c731SJamie Gritton } 1606b38ff370SJamie Gritton #endif 1607b38ff370SJamie Gritton #ifdef INET6 16080304c731SJamie Gritton redo_ip6 = 0; 16090304c731SJamie Gritton if (pr_flags & PR_IP6_USER) { 16100304c731SJamie Gritton pr->pr_flags |= PR_IP6; 1611b38ff370SJamie Gritton free(pr->pr_ip6, M_PRISON); 16120304c731SJamie Gritton pr->pr_ip6s = ip6s; 1613b38ff370SJamie Gritton pr->pr_ip6 = ip6; 1614b38ff370SJamie Gritton ip6 = NULL; 16150304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1616bdfc8cc4SJamie Gritton #ifdef VIMAGE 1617bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1618bdfc8cc4SJamie Gritton descend = 0; 1619bdfc8cc4SJamie Gritton continue; 1620bdfc8cc4SJamie Gritton } 1621bdfc8cc4SJamie Gritton #endif 16220304c731SJamie Gritton if (prison_restrict_ip6(tpr, NULL)) { 16230304c731SJamie Gritton redo_ip6 = 1; 16240304c731SJamie Gritton descend = 0; 16250304c731SJamie Gritton } 16260304c731SJamie Gritton } 16270304c731SJamie Gritton } 1628b38ff370SJamie Gritton #endif 16290304c731SJamie Gritton if (gotslevel) { 1630b38ff370SJamie Gritton pr->pr_securelevel = slevel; 16310304c731SJamie Gritton /* Set all child jails to be at least this level. */ 16320304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 16330304c731SJamie Gritton if (tpr->pr_securelevel < slevel) 16340304c731SJamie Gritton tpr->pr_securelevel = slevel; 16350304c731SJamie Gritton } 1636b97457e2SJamie Gritton if (gotchildmax) { 1637b97457e2SJamie Gritton pr->pr_childmax = childmax; 1638b97457e2SJamie Gritton /* Set all child jails to under this limit. */ 1639b97457e2SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level) 1640b97457e2SJamie Gritton if (tpr->pr_childmax > childmax - level) 1641b97457e2SJamie Gritton tpr->pr_childmax = childmax > level 1642b97457e2SJamie Gritton ? childmax - level : 0; 1643b97457e2SJamie Gritton } 16440304c731SJamie Gritton if (gotenforce) { 16450304c731SJamie Gritton pr->pr_enforce_statfs = enforce; 16460304c731SJamie Gritton /* Pass this restriction on to the children. */ 16470304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 16480304c731SJamie Gritton if (tpr->pr_enforce_statfs < enforce) 16490304c731SJamie Gritton tpr->pr_enforce_statfs = enforce; 16500304c731SJamie Gritton } 16510cc207a6SMartin Matuska if (gotrsnum) { 16520cc207a6SMartin Matuska pr->pr_devfs_rsnum = rsnum; 16530cc207a6SMartin Matuska /* Pass this restriction on to the children. */ 16540cc207a6SMartin Matuska FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 16550cc207a6SMartin Matuska tpr->pr_devfs_rsnum = rsnum; 16560cc207a6SMartin Matuska } 1657b6f47c23SJamie Gritton if (namelc != NULL) { 16580304c731SJamie Gritton if (ppr == &prison0) 1659b6f47c23SJamie Gritton strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name)); 16600304c731SJamie Gritton else 16610304c731SJamie Gritton snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s", 1662b6f47c23SJamie Gritton ppr->pr_name, namelc); 16630304c731SJamie Gritton /* Change this component of child names. */ 16640304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 16650304c731SJamie Gritton bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen, 16660304c731SJamie Gritton strlen(tpr->pr_name + onamelen) + 1); 16670304c731SJamie Gritton bcopy(pr->pr_name, tpr->pr_name, namelen); 16680304c731SJamie Gritton } 16690304c731SJamie Gritton } 1670b38ff370SJamie Gritton if (path != NULL) { 16710304c731SJamie Gritton /* Try to keep a real-rooted full pathname. */ 1672b38ff370SJamie Gritton strlcpy(pr->pr_path, path, sizeof(pr->pr_path)); 1673b38ff370SJamie Gritton pr->pr_root = root; 1674b38ff370SJamie Gritton } 167576ca6f88SJamie Gritton if (PR_HOST & ch_flags & ~pr_flags) { 167676ca6f88SJamie Gritton if (pr->pr_flags & PR_HOST) { 167776ca6f88SJamie Gritton /* 167876ca6f88SJamie Gritton * Copy the parent's host info. As with pr_ip4 above, 167976ca6f88SJamie Gritton * the lack of a lock on the parent is not a problem; 168076ca6f88SJamie Gritton * it is always set with allprison_lock at least 168176ca6f88SJamie Gritton * shared, and is held exclusively here. 168276ca6f88SJamie Gritton */ 1683c1f19219SJamie Gritton strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname, 1684c1f19219SJamie Gritton sizeof(pr->pr_hostname)); 1685c1f19219SJamie Gritton strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname, 1686c1f19219SJamie Gritton sizeof(pr->pr_domainname)); 1687c1f19219SJamie Gritton strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid, 1688c1f19219SJamie Gritton sizeof(pr->pr_hostuuid)); 168976ca6f88SJamie Gritton pr->pr_hostid = pr->pr_parent->pr_hostid; 169076ca6f88SJamie Gritton } 169176ca6f88SJamie Gritton } else if (host != NULL || domain != NULL || uuid != NULL || gothid) { 169276ca6f88SJamie Gritton /* Set this prison, and any descendants without PR_HOST. */ 1693b38ff370SJamie Gritton if (host != NULL) 1694c1f19219SJamie Gritton strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname)); 169576ca6f88SJamie Gritton if (domain != NULL) 1696c1f19219SJamie Gritton strlcpy(pr->pr_domainname, domain, 1697c1f19219SJamie Gritton sizeof(pr->pr_domainname)); 169876ca6f88SJamie Gritton if (uuid != NULL) 1699c1f19219SJamie Gritton strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid)); 170076ca6f88SJamie Gritton if (gothid) 170176ca6f88SJamie Gritton pr->pr_hostid = hid; 170276ca6f88SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 170376ca6f88SJamie Gritton if (tpr->pr_flags & PR_HOST) 170476ca6f88SJamie Gritton descend = 0; 170576ca6f88SJamie Gritton else { 170676ca6f88SJamie Gritton if (host != NULL) 1707c1f19219SJamie Gritton strlcpy(tpr->pr_hostname, 1708c1f19219SJamie Gritton pr->pr_hostname, 1709c1f19219SJamie Gritton sizeof(tpr->pr_hostname)); 171076ca6f88SJamie Gritton if (domain != NULL) 1711c1f19219SJamie Gritton strlcpy(tpr->pr_domainname, 1712c1f19219SJamie Gritton pr->pr_domainname, 1713c1f19219SJamie Gritton sizeof(tpr->pr_domainname)); 171476ca6f88SJamie Gritton if (uuid != NULL) 1715c1f19219SJamie Gritton strlcpy(tpr->pr_hostuuid, 1716c1f19219SJamie Gritton pr->pr_hostuuid, 1717c1f19219SJamie Gritton sizeof(tpr->pr_hostuuid)); 171876ca6f88SJamie Gritton if (gothid) 171976ca6f88SJamie Gritton tpr->pr_hostid = hid; 172076ca6f88SJamie Gritton } 172176ca6f88SJamie Gritton } 172276ca6f88SJamie Gritton } 17230304c731SJamie Gritton if ((tallow = ch_allow & ~pr_allow)) { 17240304c731SJamie Gritton /* Clear allow bits in all children. */ 17250304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 17260304c731SJamie Gritton tpr->pr_allow &= ~tallow; 17270304c731SJamie Gritton } 17280304c731SJamie Gritton pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow; 1729b38ff370SJamie Gritton /* 1730b38ff370SJamie Gritton * Persistent prisons get an extra reference, and prisons losing their 1731b38ff370SJamie Gritton * persist flag lose that reference. Only do this for existing prisons 1732b38ff370SJamie Gritton * for now, so new ones will remain unseen until after the module 1733b38ff370SJamie Gritton * handlers have completed. 1734b38ff370SJamie Gritton */ 1735cc5fd8c7SJamie Gritton born = pr->pr_uref == 0; 1736b38ff370SJamie Gritton if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) { 1737b38ff370SJamie Gritton if (pr_flags & PR_PERSIST) { 1738b38ff370SJamie Gritton pr->pr_ref++; 1739b38ff370SJamie Gritton pr->pr_uref++; 1740b38ff370SJamie Gritton } else { 1741b38ff370SJamie Gritton pr->pr_ref--; 1742b38ff370SJamie Gritton pr->pr_uref--; 1743b38ff370SJamie Gritton } 1744b38ff370SJamie Gritton } 1745b38ff370SJamie Gritton pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags; 1746b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1747b38ff370SJamie Gritton 1748a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 17494b5c9cf6SEdward Tomasz Napierala if (racct_enable && created) 1750a7ad07bfSEdward Tomasz Napierala prison_racct_attach(pr); 1751a7ad07bfSEdward Tomasz Napierala #endif 1752a7ad07bfSEdward Tomasz Napierala 17530304c731SJamie Gritton /* Locks may have prevented a complete restriction of child IP 17540304c731SJamie Gritton * addresses. If so, allocate some more memory and try again. 17550304c731SJamie Gritton */ 17560304c731SJamie Gritton #ifdef INET 17570304c731SJamie Gritton while (redo_ip4) { 17580304c731SJamie Gritton ip4s = pr->pr_ip4s; 17590304c731SJamie Gritton ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 17600304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 17610304c731SJamie Gritton redo_ip4 = 0; 17620304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1763bdfc8cc4SJamie Gritton #ifdef VIMAGE 1764bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1765bdfc8cc4SJamie Gritton descend = 0; 1766bdfc8cc4SJamie Gritton continue; 1767bdfc8cc4SJamie Gritton } 1768bdfc8cc4SJamie Gritton #endif 17690304c731SJamie Gritton if (prison_restrict_ip4(tpr, ip4)) { 17700304c731SJamie Gritton if (ip4 != NULL) 17710304c731SJamie Gritton ip4 = NULL; 17720304c731SJamie Gritton else 17730304c731SJamie Gritton redo_ip4 = 1; 17740304c731SJamie Gritton } 17750304c731SJamie Gritton } 17760304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 17770304c731SJamie Gritton } 17780304c731SJamie Gritton #endif 17790304c731SJamie Gritton #ifdef INET6 17800304c731SJamie Gritton while (redo_ip6) { 17810304c731SJamie Gritton ip6s = pr->pr_ip6s; 17820304c731SJamie Gritton ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 17830304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 17840304c731SJamie Gritton redo_ip6 = 0; 17850304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1786bdfc8cc4SJamie Gritton #ifdef VIMAGE 1787bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1788bdfc8cc4SJamie Gritton descend = 0; 1789bdfc8cc4SJamie Gritton continue; 1790bdfc8cc4SJamie Gritton } 1791bdfc8cc4SJamie Gritton #endif 17920304c731SJamie Gritton if (prison_restrict_ip6(tpr, ip6)) { 17930304c731SJamie Gritton if (ip6 != NULL) 17940304c731SJamie Gritton ip6 = NULL; 17950304c731SJamie Gritton else 17960304c731SJamie Gritton redo_ip6 = 1; 17970304c731SJamie Gritton } 17980304c731SJamie Gritton } 17990304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 18000304c731SJamie Gritton } 18010304c731SJamie Gritton #endif 18020304c731SJamie Gritton 1803b38ff370SJamie Gritton /* Let the modules do their work. */ 1804b38ff370SJamie Gritton sx_downgrade(&allprison_lock); 1805cc5fd8c7SJamie Gritton if (born) { 1806b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_CREATE, opts); 1807b38ff370SJamie Gritton if (error) { 1808cc5fd8c7SJamie Gritton (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL); 1809cc5fd8c7SJamie Gritton prison_deref(pr, created 1810cc5fd8c7SJamie Gritton ? PD_LIST_SLOCKED 1811cc5fd8c7SJamie Gritton : PD_DEREF | PD_LIST_SLOCKED); 1812b38ff370SJamie Gritton goto done_errmsg; 1813b38ff370SJamie Gritton } 1814b38ff370SJamie Gritton } 1815b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_SET, opts); 1816b38ff370SJamie Gritton if (error) { 1817cc5fd8c7SJamie Gritton if (born) 1818cc5fd8c7SJamie Gritton (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL); 1819b38ff370SJamie Gritton prison_deref(pr, created 1820b38ff370SJamie Gritton ? PD_LIST_SLOCKED 1821b38ff370SJamie Gritton : PD_DEREF | PD_LIST_SLOCKED); 1822b38ff370SJamie Gritton goto done_errmsg; 1823b38ff370SJamie Gritton } 1824b38ff370SJamie Gritton 1825b38ff370SJamie Gritton /* Attach this process to the prison if requested. */ 1826b38ff370SJamie Gritton if (flags & JAIL_ATTACH) { 1827b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1828b38ff370SJamie Gritton error = do_jail_attach(td, pr); 1829b38ff370SJamie Gritton if (error) { 1830b38ff370SJamie Gritton vfs_opterror(opts, "attach failed"); 1831b38ff370SJamie Gritton if (!created) 1832b38ff370SJamie Gritton prison_deref(pr, PD_DEREF); 1833b38ff370SJamie Gritton goto done_errmsg; 1834b38ff370SJamie Gritton } 1835b38ff370SJamie Gritton } 1836b38ff370SJamie Gritton 18371fb24974SEdward Tomasz Napierala #ifdef RACCT 18384b5c9cf6SEdward Tomasz Napierala if (racct_enable && !created) { 1839f514b97bSEdward Tomasz Napierala if (!(flags & JAIL_ATTACH)) 18401fb24974SEdward Tomasz Napierala sx_sunlock(&allprison_lock); 18411fb24974SEdward Tomasz Napierala prison_racct_modify(pr); 1842f514b97bSEdward Tomasz Napierala if (!(flags & JAIL_ATTACH)) 18431fb24974SEdward Tomasz Napierala sx_slock(&allprison_lock); 18441fb24974SEdward Tomasz Napierala } 18451fb24974SEdward Tomasz Napierala #endif 18461fb24974SEdward Tomasz Napierala 18471fb24974SEdward Tomasz Napierala td->td_retval[0] = pr->pr_id; 18481fb24974SEdward Tomasz Napierala 1849b38ff370SJamie Gritton /* 1850b38ff370SJamie Gritton * Now that it is all there, drop the temporary reference from existing 1851b38ff370SJamie Gritton * prisons. Or add a reference to newly created persistent prisons 1852b38ff370SJamie Gritton * (which was not done earlier so that the prison would not be publicly 1853b38ff370SJamie Gritton * visible). 1854b38ff370SJamie Gritton */ 1855b38ff370SJamie Gritton if (!created) { 1856b38ff370SJamie Gritton prison_deref(pr, (flags & JAIL_ATTACH) 1857b38ff370SJamie Gritton ? PD_DEREF 1858b38ff370SJamie Gritton : PD_DEREF | PD_LIST_SLOCKED); 1859b38ff370SJamie Gritton } else { 1860b38ff370SJamie Gritton if (pr_flags & PR_PERSIST) { 1861b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1862b38ff370SJamie Gritton pr->pr_ref++; 1863b38ff370SJamie Gritton pr->pr_uref++; 1864b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1865b38ff370SJamie Gritton } 1866b38ff370SJamie Gritton if (!(flags & JAIL_ATTACH)) 1867b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 1868b38ff370SJamie Gritton } 1869c34bbd2aSEdward Tomasz Napierala 1870cc5fd8c7SJamie Gritton goto done_free; 1871b38ff370SJamie Gritton 18720304c731SJamie Gritton done_deref_locked: 18730304c731SJamie Gritton prison_deref(pr, created 18740304c731SJamie Gritton ? PD_LOCKED | PD_LIST_XLOCKED 18750304c731SJamie Gritton : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 18760304c731SJamie Gritton goto done_releroot; 1877b38ff370SJamie Gritton done_unlock_list: 1878b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 1879b38ff370SJamie Gritton done_releroot: 18805050aa86SKonstantin Belousov if (root != NULL) 1881b38ff370SJamie Gritton vrele(root); 1882b38ff370SJamie Gritton done_errmsg: 1883b38ff370SJamie Gritton if (error) { 1884176ff3a0SJamie Gritton if (vfs_getopt(opts, "errmsg", (void **)&errmsg, 1885176ff3a0SJamie Gritton &errmsg_len) == 0 && errmsg_len > 0) { 1886b38ff370SJamie Gritton errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1; 1887b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 1888b38ff370SJamie Gritton bcopy(errmsg, 1889b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 1890b38ff370SJamie Gritton errmsg_len); 1891b38ff370SJamie Gritton else 1892b38ff370SJamie Gritton copyout(errmsg, 1893b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 1894b38ff370SJamie Gritton errmsg_len); 1895b38ff370SJamie Gritton } 1896b38ff370SJamie Gritton } 1897b38ff370SJamie Gritton done_free: 1898b38ff370SJamie Gritton #ifdef INET 1899b38ff370SJamie Gritton free(ip4, M_PRISON); 1900b38ff370SJamie Gritton #endif 1901b38ff370SJamie Gritton #ifdef INET6 1902b38ff370SJamie Gritton free(ip6, M_PRISON); 1903b38ff370SJamie Gritton #endif 19046dfe0a3dSMartin Matuska if (g_path != NULL) 19056dfe0a3dSMartin Matuska free(g_path, M_TEMP); 1906b38ff370SJamie Gritton vfs_freeopts(opts); 1907b38ff370SJamie Gritton return (error); 1908b38ff370SJamie Gritton } 1909b38ff370SJamie Gritton 1910b38ff370SJamie Gritton /* 1911*7de883c8SJamie Gritton * Find the next available prison ID. Return the ID on success, or zero 1912*7de883c8SJamie Gritton * on failure. Also set a pointer to the allprison list entry the prison 1913*7de883c8SJamie Gritton * should be inserted before. 1914*7de883c8SJamie Gritton */ 1915*7de883c8SJamie Gritton static int 1916*7de883c8SJamie Gritton get_next_prid(struct prison **insprp) 1917*7de883c8SJamie Gritton { 1918*7de883c8SJamie Gritton struct prison *inspr; 1919*7de883c8SJamie Gritton int jid, maxid; 1920*7de883c8SJamie Gritton 1921*7de883c8SJamie Gritton jid = lastprid % JAIL_MAX + 1; 1922*7de883c8SJamie Gritton if (TAILQ_EMPTY(&allprison) || 1923*7de883c8SJamie Gritton TAILQ_LAST(&allprison, prisonlist)->pr_id < jid) { 1924*7de883c8SJamie Gritton /* 1925*7de883c8SJamie Gritton * A common case is for all jails to be implicitly numbered, 1926*7de883c8SJamie Gritton * which means they'll go on the end of the list, at least 1927*7de883c8SJamie Gritton * for the first JAIL_MAX times. 1928*7de883c8SJamie Gritton */ 1929*7de883c8SJamie Gritton inspr = NULL; 1930*7de883c8SJamie Gritton } else { 1931*7de883c8SJamie Gritton /* 1932*7de883c8SJamie Gritton * Take two passes through the allprison list: first starting 1933*7de883c8SJamie Gritton * with the proposed jid, then ending with it. 1934*7de883c8SJamie Gritton */ 1935*7de883c8SJamie Gritton for (maxid = JAIL_MAX; maxid != 0; ) { 1936*7de883c8SJamie Gritton TAILQ_FOREACH(inspr, &allprison, pr_list) { 1937*7de883c8SJamie Gritton if (inspr->pr_id < jid) 1938*7de883c8SJamie Gritton continue; 1939*7de883c8SJamie Gritton if (inspr->pr_id > jid || inspr->pr_ref == 0) { 1940*7de883c8SJamie Gritton /* 1941*7de883c8SJamie Gritton * Found an opening. This may be a gap 1942*7de883c8SJamie Gritton * in the list, or a dead jail with the 1943*7de883c8SJamie Gritton * same ID. 1944*7de883c8SJamie Gritton */ 1945*7de883c8SJamie Gritton maxid = 0; 1946*7de883c8SJamie Gritton break; 1947*7de883c8SJamie Gritton } 1948*7de883c8SJamie Gritton if (++jid > maxid) { 1949*7de883c8SJamie Gritton if (lastprid == maxid || lastprid == 0) 1950*7de883c8SJamie Gritton { 1951*7de883c8SJamie Gritton /* 1952*7de883c8SJamie Gritton * The entire legal range 1953*7de883c8SJamie Gritton * has been traversed 1954*7de883c8SJamie Gritton */ 1955*7de883c8SJamie Gritton return 0; 1956*7de883c8SJamie Gritton } 1957*7de883c8SJamie Gritton /* Try again from the start. */ 1958*7de883c8SJamie Gritton jid = 1; 1959*7de883c8SJamie Gritton maxid = lastprid; 1960*7de883c8SJamie Gritton break; 1961*7de883c8SJamie Gritton } 1962*7de883c8SJamie Gritton } 1963*7de883c8SJamie Gritton if (inspr == NULL) { 1964*7de883c8SJamie Gritton /* Found room at the end of the list. */ 1965*7de883c8SJamie Gritton break; 1966*7de883c8SJamie Gritton } 1967*7de883c8SJamie Gritton } 1968*7de883c8SJamie Gritton } 1969*7de883c8SJamie Gritton *insprp = inspr; 1970*7de883c8SJamie Gritton lastprid = jid; 1971*7de883c8SJamie Gritton return (jid); 1972*7de883c8SJamie Gritton } 1973*7de883c8SJamie Gritton 1974*7de883c8SJamie Gritton /* 1975b38ff370SJamie Gritton * struct jail_get_args { 1976b38ff370SJamie Gritton * struct iovec *iovp; 1977b38ff370SJamie Gritton * unsigned int iovcnt; 1978b38ff370SJamie Gritton * int flags; 1979b38ff370SJamie Gritton * }; 1980b38ff370SJamie Gritton */ 1981b38ff370SJamie Gritton int 19828451d0ddSKip Macy sys_jail_get(struct thread *td, struct jail_get_args *uap) 1983b38ff370SJamie Gritton { 1984b38ff370SJamie Gritton struct uio *auio; 1985b38ff370SJamie Gritton int error; 1986b38ff370SJamie Gritton 1987b38ff370SJamie Gritton /* Check that we have an even number of iovecs. */ 1988b38ff370SJamie Gritton if (uap->iovcnt & 1) 1989b38ff370SJamie Gritton return (EINVAL); 1990b38ff370SJamie Gritton 1991b38ff370SJamie Gritton error = copyinuio(uap->iovp, uap->iovcnt, &auio); 1992b38ff370SJamie Gritton if (error) 1993b38ff370SJamie Gritton return (error); 1994b38ff370SJamie Gritton error = kern_jail_get(td, auio, uap->flags); 1995b38ff370SJamie Gritton if (error == 0) 1996b38ff370SJamie Gritton error = copyout(auio->uio_iov, uap->iovp, 1997b38ff370SJamie Gritton uap->iovcnt * sizeof (struct iovec)); 1998b38ff370SJamie Gritton free(auio, M_IOV); 1999b38ff370SJamie Gritton return (error); 2000b38ff370SJamie Gritton } 2001b38ff370SJamie Gritton 2002b38ff370SJamie Gritton int 2003b38ff370SJamie Gritton kern_jail_get(struct thread *td, struct uio *optuio, int flags) 2004b38ff370SJamie Gritton { 2005672756aaSJamie Gritton struct bool_flags *bf; 2006672756aaSJamie Gritton struct jailsys_flags *jsf; 20070304c731SJamie Gritton struct prison *pr, *mypr; 2008b38ff370SJamie Gritton struct vfsopt *opt; 2009b38ff370SJamie Gritton struct vfsoptlist *opts; 2010b38ff370SJamie Gritton char *errmsg, *name; 2011672756aaSJamie Gritton int error, errmsg_len, errmsg_pos, i, jid, len, locked, pos; 2012672756aaSJamie Gritton unsigned f; 2013b38ff370SJamie Gritton 2014b38ff370SJamie Gritton if (flags & ~JAIL_GET_MASK) 2015b38ff370SJamie Gritton return (EINVAL); 2016b38ff370SJamie Gritton 2017b38ff370SJamie Gritton /* Get the parameter list. */ 2018b38ff370SJamie Gritton error = vfs_buildopts(optuio, &opts); 2019b38ff370SJamie Gritton if (error) 2020b38ff370SJamie Gritton return (error); 2021b38ff370SJamie Gritton errmsg_pos = vfs_getopt_pos(opts, "errmsg"); 20220304c731SJamie Gritton mypr = td->td_ucred->cr_prison; 20231e2a13e6SJamie Gritton 2024b38ff370SJamie Gritton /* 2025b38ff370SJamie Gritton * Find the prison specified by one of: lastjid, jid, name. 2026b38ff370SJamie Gritton */ 2027b38ff370SJamie Gritton sx_slock(&allprison_lock); 2028b38ff370SJamie Gritton error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid)); 2029b38ff370SJamie Gritton if (error == 0) { 2030b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 20310304c731SJamie Gritton if (pr->pr_id > jid && prison_ischild(mypr, pr)) { 2032b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 2033b38ff370SJamie Gritton if (pr->pr_ref > 0 && 2034b38ff370SJamie Gritton (pr->pr_uref > 0 || (flags & JAIL_DYING))) 2035b38ff370SJamie Gritton break; 2036b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2037b38ff370SJamie Gritton } 2038b38ff370SJamie Gritton } 2039b38ff370SJamie Gritton if (pr != NULL) 2040b38ff370SJamie Gritton goto found_prison; 2041b38ff370SJamie Gritton error = ENOENT; 2042b38ff370SJamie Gritton vfs_opterror(opts, "no jail after %d", jid); 2043b38ff370SJamie Gritton goto done_unlock_list; 2044b38ff370SJamie Gritton } else if (error != ENOENT) 2045b38ff370SJamie Gritton goto done_unlock_list; 2046b38ff370SJamie Gritton 2047b38ff370SJamie Gritton error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 2048b38ff370SJamie Gritton if (error == 0) { 2049b38ff370SJamie Gritton if (jid != 0) { 20500304c731SJamie Gritton pr = prison_find_child(mypr, jid); 2051b38ff370SJamie Gritton if (pr != NULL) { 2052b38ff370SJamie Gritton if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 2053b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2054b38ff370SJamie Gritton error = ENOENT; 2055b38ff370SJamie Gritton vfs_opterror(opts, "jail %d is dying", 2056b38ff370SJamie Gritton jid); 2057b38ff370SJamie Gritton goto done_unlock_list; 2058b38ff370SJamie Gritton } 2059b38ff370SJamie Gritton goto found_prison; 2060b38ff370SJamie Gritton } 2061b38ff370SJamie Gritton error = ENOENT; 2062b38ff370SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 2063b38ff370SJamie Gritton goto done_unlock_list; 2064b38ff370SJamie Gritton } 2065b38ff370SJamie Gritton } else if (error != ENOENT) 2066b38ff370SJamie Gritton goto done_unlock_list; 2067b38ff370SJamie Gritton 2068b38ff370SJamie Gritton error = vfs_getopt(opts, "name", (void **)&name, &len); 2069b38ff370SJamie Gritton if (error == 0) { 2070b38ff370SJamie Gritton if (len == 0 || name[len - 1] != '\0') { 2071b38ff370SJamie Gritton error = EINVAL; 2072b38ff370SJamie Gritton goto done_unlock_list; 2073b38ff370SJamie Gritton } 20740304c731SJamie Gritton pr = prison_find_name(mypr, name); 2075b38ff370SJamie Gritton if (pr != NULL) { 2076b38ff370SJamie Gritton if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 2077b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2078b38ff370SJamie Gritton error = ENOENT; 2079b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" is dying", 2080b38ff370SJamie Gritton name); 2081b38ff370SJamie Gritton goto done_unlock_list; 2082b38ff370SJamie Gritton } 2083b38ff370SJamie Gritton goto found_prison; 2084b38ff370SJamie Gritton } 2085b38ff370SJamie Gritton error = ENOENT; 2086b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", name); 2087b38ff370SJamie Gritton goto done_unlock_list; 2088b38ff370SJamie Gritton } else if (error != ENOENT) 2089b38ff370SJamie Gritton goto done_unlock_list; 2090b38ff370SJamie Gritton 2091b38ff370SJamie Gritton vfs_opterror(opts, "no jail specified"); 2092b38ff370SJamie Gritton error = ENOENT; 2093b38ff370SJamie Gritton goto done_unlock_list; 2094b38ff370SJamie Gritton 2095b38ff370SJamie Gritton found_prison: 2096b38ff370SJamie Gritton /* Get the parameters of the prison. */ 2097b38ff370SJamie Gritton pr->pr_ref++; 2098b38ff370SJamie Gritton locked = PD_LOCKED; 2099b38ff370SJamie Gritton td->td_retval[0] = pr->pr_id; 2100b38ff370SJamie Gritton error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id)); 2101b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2102b38ff370SJamie Gritton goto done_deref; 21030304c731SJamie Gritton i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id; 21040304c731SJamie Gritton error = vfs_setopt(opts, "parent", &i, sizeof(i)); 2105b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2106b38ff370SJamie Gritton goto done_deref; 21070304c731SJamie Gritton error = vfs_setopts(opts, "name", prison_name(mypr, pr)); 21080304c731SJamie Gritton if (error != 0 && error != ENOENT) 21090304c731SJamie Gritton goto done_deref; 21100304c731SJamie Gritton error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id, 2111b38ff370SJamie Gritton sizeof(pr->pr_cpuset->cs_id)); 2112b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2113b38ff370SJamie Gritton goto done_deref; 21140304c731SJamie Gritton error = vfs_setopts(opts, "path", prison_path(mypr, pr)); 2115b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2116b38ff370SJamie Gritton goto done_deref; 2117b38ff370SJamie Gritton #ifdef INET 2118b38ff370SJamie Gritton error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4, 2119b38ff370SJamie Gritton pr->pr_ip4s * sizeof(*pr->pr_ip4)); 2120b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2121b38ff370SJamie Gritton goto done_deref; 2122b38ff370SJamie Gritton #endif 2123b38ff370SJamie Gritton #ifdef INET6 2124b38ff370SJamie Gritton error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6, 2125b38ff370SJamie Gritton pr->pr_ip6s * sizeof(*pr->pr_ip6)); 2126b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2127b38ff370SJamie Gritton goto done_deref; 2128b38ff370SJamie Gritton #endif 2129b38ff370SJamie Gritton error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel, 2130b38ff370SJamie Gritton sizeof(pr->pr_securelevel)); 2131b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2132b38ff370SJamie Gritton goto done_deref; 2133b97457e2SJamie Gritton error = vfs_setopt(opts, "children.cur", &pr->pr_childcount, 2134b97457e2SJamie Gritton sizeof(pr->pr_childcount)); 2135b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 2136b97457e2SJamie Gritton goto done_deref; 2137b97457e2SJamie Gritton error = vfs_setopt(opts, "children.max", &pr->pr_childmax, 2138b97457e2SJamie Gritton sizeof(pr->pr_childmax)); 2139b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 2140b97457e2SJamie Gritton goto done_deref; 2141c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostname", pr->pr_hostname); 2142b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2143b38ff370SJamie Gritton goto done_deref; 2144c1f19219SJamie Gritton error = vfs_setopts(opts, "host.domainname", pr->pr_domainname); 214576ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 214676ca6f88SJamie Gritton goto done_deref; 2147c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid); 214876ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 214976ca6f88SJamie Gritton goto done_deref; 2150841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32 2151a5c1afadSDmitry Chagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 215276ca6f88SJamie Gritton uint32_t hid32 = pr->pr_hostid; 215376ca6f88SJamie Gritton 215476ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32)); 215576ca6f88SJamie Gritton } else 215676ca6f88SJamie Gritton #endif 215776ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid, 215876ca6f88SJamie Gritton sizeof(pr->pr_hostid)); 215976ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 216076ca6f88SJamie Gritton goto done_deref; 21610304c731SJamie Gritton error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs, 21620304c731SJamie Gritton sizeof(pr->pr_enforce_statfs)); 21630304c731SJamie Gritton if (error != 0 && error != ENOENT) 21640304c731SJamie Gritton goto done_deref; 21650cc207a6SMartin Matuska error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum, 21660cc207a6SMartin Matuska sizeof(pr->pr_devfs_rsnum)); 21670cc207a6SMartin Matuska if (error != 0 && error != ENOENT) 21680cc207a6SMartin Matuska goto done_deref; 2169672756aaSJamie Gritton for (bf = pr_flag_bool; 2170672756aaSJamie Gritton bf < pr_flag_bool + nitems(pr_flag_bool); 2171672756aaSJamie Gritton bf++) { 2172672756aaSJamie Gritton i = (pr->pr_flags & bf->flag) ? 1 : 0; 2173672756aaSJamie Gritton error = vfs_setopt(opts, bf->name, &i, sizeof(i)); 2174b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2175b38ff370SJamie Gritton goto done_deref; 2176b38ff370SJamie Gritton i = !i; 2177672756aaSJamie Gritton error = vfs_setopt(opts, bf->noname, &i, sizeof(i)); 2178b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2179b38ff370SJamie Gritton goto done_deref; 21800304c731SJamie Gritton } 2181672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 2182672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 2183672756aaSJamie Gritton jsf++) { 2184672756aaSJamie Gritton f = pr->pr_flags & (jsf->disable | jsf->new); 2185672756aaSJamie Gritton i = (f != 0 && f == jsf->disable) ? JAIL_SYS_DISABLE 2186672756aaSJamie Gritton : (f == jsf->new) ? JAIL_SYS_NEW 21877cbf7213SJamie Gritton : JAIL_SYS_INHERIT; 2188672756aaSJamie Gritton error = vfs_setopt(opts, jsf->name, &i, sizeof(i)); 21897cbf7213SJamie Gritton if (error != 0 && error != ENOENT) 21907cbf7213SJamie Gritton goto done_deref; 21917cbf7213SJamie Gritton } 2192672756aaSJamie Gritton for (bf = pr_flag_allow; 21930e5c6bd4SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && bf->flag != 0; 2194672756aaSJamie Gritton bf++) { 2195672756aaSJamie Gritton i = (pr->pr_allow & bf->flag) ? 1 : 0; 2196672756aaSJamie Gritton error = vfs_setopt(opts, bf->name, &i, sizeof(i)); 21970304c731SJamie Gritton if (error != 0 && error != ENOENT) 21980304c731SJamie Gritton goto done_deref; 21990304c731SJamie Gritton i = !i; 2200672756aaSJamie Gritton error = vfs_setopt(opts, bf->noname, &i, sizeof(i)); 22010304c731SJamie Gritton if (error != 0 && error != ENOENT) 22020304c731SJamie Gritton goto done_deref; 22030304c731SJamie Gritton } 2204b38ff370SJamie Gritton i = (pr->pr_uref == 0); 2205b38ff370SJamie Gritton error = vfs_setopt(opts, "dying", &i, sizeof(i)); 2206b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2207b38ff370SJamie Gritton goto done_deref; 2208b38ff370SJamie Gritton i = !i; 2209b38ff370SJamie Gritton error = vfs_setopt(opts, "nodying", &i, sizeof(i)); 2210b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2211b38ff370SJamie Gritton goto done_deref; 2212bd96bd15SIan Lepore error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate, 2213bd96bd15SIan Lepore sizeof(pr->pr_osreldate)); 2214a1a4c1b0SIan Lepore if (error != 0 && error != ENOENT) 2215a1a4c1b0SIan Lepore goto done_deref; 2216a1a4c1b0SIan Lepore error = vfs_setopts(opts, "osrelease", pr->pr_osrelease); 2217a1a4c1b0SIan Lepore if (error != 0 && error != ENOENT) 2218a1a4c1b0SIan Lepore goto done_deref; 2219b38ff370SJamie Gritton 2220b38ff370SJamie Gritton /* Get the module parameters. */ 2221b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2222b38ff370SJamie Gritton locked = 0; 2223b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_GET, opts); 2224b38ff370SJamie Gritton if (error) 2225b38ff370SJamie Gritton goto done_deref; 2226b38ff370SJamie Gritton prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED); 2227b38ff370SJamie Gritton 2228b38ff370SJamie Gritton /* By now, all parameters should have been noted. */ 2229b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2230b38ff370SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 2231b38ff370SJamie Gritton error = EINVAL; 2232b38ff370SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 2233b38ff370SJamie Gritton goto done_errmsg; 2234b38ff370SJamie Gritton } 2235b38ff370SJamie Gritton } 2236b38ff370SJamie Gritton 2237b38ff370SJamie Gritton /* Write the fetched parameters back to userspace. */ 2238b38ff370SJamie Gritton error = 0; 2239b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2240b38ff370SJamie Gritton if (opt->pos >= 0 && opt->pos != errmsg_pos) { 2241b38ff370SJamie Gritton pos = 2 * opt->pos + 1; 2242b38ff370SJamie Gritton optuio->uio_iov[pos].iov_len = opt->len; 2243b38ff370SJamie Gritton if (opt->value != NULL) { 2244b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) { 2245b38ff370SJamie Gritton bcopy(opt->value, 2246b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2247b38ff370SJamie Gritton opt->len); 2248b38ff370SJamie Gritton } else { 2249b38ff370SJamie Gritton error = copyout(opt->value, 2250b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2251b38ff370SJamie Gritton opt->len); 2252b38ff370SJamie Gritton if (error) 2253b38ff370SJamie Gritton break; 2254b38ff370SJamie Gritton } 2255b38ff370SJamie Gritton } 2256b38ff370SJamie Gritton } 2257b38ff370SJamie Gritton } 2258b38ff370SJamie Gritton goto done_errmsg; 2259b38ff370SJamie Gritton 2260b38ff370SJamie Gritton done_deref: 2261b38ff370SJamie Gritton prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED); 2262b38ff370SJamie Gritton goto done_errmsg; 2263b38ff370SJamie Gritton 2264b38ff370SJamie Gritton done_unlock_list: 2265b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2266b38ff370SJamie Gritton done_errmsg: 2267b38ff370SJamie Gritton if (error && errmsg_pos >= 0) { 2268b38ff370SJamie Gritton vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 2269b38ff370SJamie Gritton errmsg_pos = 2 * errmsg_pos + 1; 2270b38ff370SJamie Gritton if (errmsg_len > 0) { 2271b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 2272b38ff370SJamie Gritton bcopy(errmsg, 2273b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2274b38ff370SJamie Gritton errmsg_len); 2275b38ff370SJamie Gritton else 2276b38ff370SJamie Gritton copyout(errmsg, 2277b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2278b38ff370SJamie Gritton errmsg_len); 2279b38ff370SJamie Gritton } 2280b38ff370SJamie Gritton } 2281b38ff370SJamie Gritton vfs_freeopts(opts); 2282b38ff370SJamie Gritton return (error); 2283b38ff370SJamie Gritton } 2284b38ff370SJamie Gritton 2285b38ff370SJamie Gritton /* 2286b38ff370SJamie Gritton * struct jail_remove_args { 2287b38ff370SJamie Gritton * int jid; 2288b38ff370SJamie Gritton * }; 2289b38ff370SJamie Gritton */ 2290b38ff370SJamie Gritton int 22918451d0ddSKip Macy sys_jail_remove(struct thread *td, struct jail_remove_args *uap) 2292b38ff370SJamie Gritton { 22930304c731SJamie Gritton struct prison *pr, *cpr, *lpr, *tpr; 22940304c731SJamie Gritton int descend, error; 2295b38ff370SJamie Gritton 2296b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_REMOVE); 2297b38ff370SJamie Gritton if (error) 2298b38ff370SJamie Gritton return (error); 2299b38ff370SJamie Gritton 2300b38ff370SJamie Gritton sx_xlock(&allprison_lock); 23010304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2302b38ff370SJamie Gritton if (pr == NULL) { 2303b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2304b38ff370SJamie Gritton return (EINVAL); 2305b38ff370SJamie Gritton } 2306b38ff370SJamie Gritton 23070304c731SJamie Gritton /* Remove all descendants of this prison, then remove this prison. */ 23080304c731SJamie Gritton pr->pr_ref++; 23090304c731SJamie Gritton if (!LIST_EMPTY(&pr->pr_children)) { 23100304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 23110304c731SJamie Gritton lpr = NULL; 23120304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 23130304c731SJamie Gritton mtx_lock(&cpr->pr_mtx); 23140304c731SJamie Gritton if (cpr->pr_ref > 0) { 23150304c731SJamie Gritton tpr = cpr; 23160304c731SJamie Gritton cpr->pr_ref++; 23170304c731SJamie Gritton } else { 23180304c731SJamie Gritton /* Already removed - do not do it again. */ 23190304c731SJamie Gritton tpr = NULL; 23200304c731SJamie Gritton } 23210304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 23220304c731SJamie Gritton if (lpr != NULL) { 23230304c731SJamie Gritton mtx_lock(&lpr->pr_mtx); 23240304c731SJamie Gritton prison_remove_one(lpr); 23250304c731SJamie Gritton sx_xlock(&allprison_lock); 23260304c731SJamie Gritton } 23270304c731SJamie Gritton lpr = tpr; 23280304c731SJamie Gritton } 23290304c731SJamie Gritton if (lpr != NULL) { 23300304c731SJamie Gritton mtx_lock(&lpr->pr_mtx); 23310304c731SJamie Gritton prison_remove_one(lpr); 23320304c731SJamie Gritton sx_xlock(&allprison_lock); 23330304c731SJamie Gritton } 23340304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 23350304c731SJamie Gritton } 23360304c731SJamie Gritton prison_remove_one(pr); 23370304c731SJamie Gritton return (0); 23380304c731SJamie Gritton } 23390304c731SJamie Gritton 23400304c731SJamie Gritton static void 23410304c731SJamie Gritton prison_remove_one(struct prison *pr) 23420304c731SJamie Gritton { 23430304c731SJamie Gritton struct proc *p; 23440304c731SJamie Gritton int deuref; 23450304c731SJamie Gritton 2346b38ff370SJamie Gritton /* If the prison was persistent, it is not anymore. */ 2347b38ff370SJamie Gritton deuref = 0; 2348b38ff370SJamie Gritton if (pr->pr_flags & PR_PERSIST) { 2349b38ff370SJamie Gritton pr->pr_ref--; 2350b38ff370SJamie Gritton deuref = PD_DEUREF; 2351b38ff370SJamie Gritton pr->pr_flags &= ~PR_PERSIST; 2352b38ff370SJamie Gritton } 2353b38ff370SJamie Gritton 23540304c731SJamie Gritton /* 23550304c731SJamie Gritton * jail_remove added a reference. If that's the only one, remove 23560304c731SJamie Gritton * the prison now. 23570304c731SJamie Gritton */ 23580304c731SJamie Gritton KASSERT(pr->pr_ref > 0, 23590304c731SJamie Gritton ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id)); 23600304c731SJamie Gritton if (pr->pr_ref == 1) { 2361b38ff370SJamie Gritton prison_deref(pr, 2362b38ff370SJamie Gritton deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 23630304c731SJamie Gritton return; 2364b38ff370SJamie Gritton } 2365b38ff370SJamie Gritton 2366b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2367b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2368b38ff370SJamie Gritton /* 2369b38ff370SJamie Gritton * Kill all processes unfortunate enough to be attached to this prison. 2370b38ff370SJamie Gritton */ 2371b38ff370SJamie Gritton sx_slock(&allproc_lock); 23727938a442SBjoern A. Zeeb FOREACH_PROC_IN_SYSTEM(p) { 2373b38ff370SJamie Gritton PROC_LOCK(p); 2374b38ff370SJamie Gritton if (p->p_state != PRS_NEW && p->p_ucred && 2375b38ff370SJamie Gritton p->p_ucred->cr_prison == pr) 23768451d0ddSKip Macy kern_psignal(p, SIGKILL); 2377b38ff370SJamie Gritton PROC_UNLOCK(p); 2378b38ff370SJamie Gritton } 2379b38ff370SJamie Gritton sx_sunlock(&allproc_lock); 23800304c731SJamie Gritton /* Remove the temporary reference added by jail_remove. */ 2381b38ff370SJamie Gritton prison_deref(pr, deuref | PD_DEREF); 238275c13541SPoul-Henning Kamp } 238375c13541SPoul-Henning Kamp 2384fd7a8150SMike Barcroft /* 23859ddb7954SMike Barcroft * struct jail_attach_args { 23869ddb7954SMike Barcroft * int jid; 23879ddb7954SMike Barcroft * }; 2388fd7a8150SMike Barcroft */ 2389fd7a8150SMike Barcroft int 23908451d0ddSKip Macy sys_jail_attach(struct thread *td, struct jail_attach_args *uap) 2391fd7a8150SMike Barcroft { 2392b38ff370SJamie Gritton struct prison *pr; 2393b38ff370SJamie Gritton int error; 2394b38ff370SJamie Gritton 2395b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 2396b38ff370SJamie Gritton if (error) 2397b38ff370SJamie Gritton return (error); 2398b38ff370SJamie Gritton 2399ef0ddea3SJamie Gritton /* 2400ef0ddea3SJamie Gritton * Start with exclusive hold on allprison_lock to ensure that a possible 2401ef0ddea3SJamie Gritton * PR_METHOD_REMOVE call isn't concurrent with jail_set or jail_remove. 2402ef0ddea3SJamie Gritton * But then immediately downgrade it since we don't need to stop 2403ef0ddea3SJamie Gritton * readers. 2404ef0ddea3SJamie Gritton */ 2405ef0ddea3SJamie Gritton sx_xlock(&allprison_lock); 2406ef0ddea3SJamie Gritton sx_downgrade(&allprison_lock); 24070304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2408b38ff370SJamie Gritton if (pr == NULL) { 2409b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2410b38ff370SJamie Gritton return (EINVAL); 2411b38ff370SJamie Gritton } 2412b38ff370SJamie Gritton 2413b38ff370SJamie Gritton /* 2414b38ff370SJamie Gritton * Do not allow a process to attach to a prison that is not 2415b38ff370SJamie Gritton * considered to be "alive". 2416b38ff370SJamie Gritton */ 2417b38ff370SJamie Gritton if (pr->pr_uref == 0) { 2418b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2419b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2420b38ff370SJamie Gritton return (EINVAL); 2421b38ff370SJamie Gritton } 2422b38ff370SJamie Gritton 2423b38ff370SJamie Gritton return (do_jail_attach(td, pr)); 2424b38ff370SJamie Gritton } 2425b38ff370SJamie Gritton 2426b38ff370SJamie Gritton static int 2427b38ff370SJamie Gritton do_jail_attach(struct thread *td, struct prison *pr) 2428b38ff370SJamie Gritton { 2429fd7a8150SMike Barcroft struct proc *p; 2430fd7a8150SMike Barcroft struct ucred *newcred, *oldcred; 24315050aa86SKonstantin Belousov int error; 2432fd7a8150SMike Barcroft 243357f22bd4SJacques Vidrine /* 243457f22bd4SJacques Vidrine * XXX: Note that there is a slight race here if two threads 243557f22bd4SJacques Vidrine * in the same privileged process attempt to attach to two 243657f22bd4SJacques Vidrine * different jails at the same time. It is important for 243757f22bd4SJacques Vidrine * user processes not to do this, or they might end up with 243857f22bd4SJacques Vidrine * a process root from one prison, but attached to the jail 243957f22bd4SJacques Vidrine * of another. 244057f22bd4SJacques Vidrine */ 2441fd7a8150SMike Barcroft pr->pr_ref++; 2442b38ff370SJamie Gritton pr->pr_uref++; 2443fd7a8150SMike Barcroft mtx_unlock(&pr->pr_mtx); 2444b38ff370SJamie Gritton 2445b38ff370SJamie Gritton /* Let modules do whatever they need to prepare for attaching. */ 2446b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_ATTACH, td); 2447b38ff370SJamie Gritton if (error) { 2448b38ff370SJamie Gritton prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED); 2449b38ff370SJamie Gritton return (error); 2450b38ff370SJamie Gritton } 2451dc68a633SPawel Jakub Dawidek sx_sunlock(&allprison_lock); 2452fd7a8150SMike Barcroft 2453413628a7SBjoern A. Zeeb /* 2454413628a7SBjoern A. Zeeb * Reparent the newly attached process to this jail. 2455413628a7SBjoern A. Zeeb */ 2456b38ff370SJamie Gritton p = td->td_proc; 2457413628a7SBjoern A. Zeeb error = cpuset_setproc_update_set(p, pr->pr_cpuset); 2458413628a7SBjoern A. Zeeb if (error) 2459b38ff370SJamie Gritton goto e_revert_osd; 2460413628a7SBjoern A. Zeeb 2461cb05b60aSAttilio Rao vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY); 2462fd7a8150SMike Barcroft if ((error = change_dir(pr->pr_root, td)) != 0) 2463fd7a8150SMike Barcroft goto e_unlock; 2464fd7a8150SMike Barcroft #ifdef MAC 246530d239bcSRobert Watson if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root))) 2466fd7a8150SMike Barcroft goto e_unlock; 2467fd7a8150SMike Barcroft #endif 2468b249ce48SMateusz Guzik VOP_UNLOCK(pr->pr_root); 2469f0725a8eSMateusz Guzik if ((error = pwd_chroot(td, pr->pr_root))) 24705050aa86SKonstantin Belousov goto e_revert_osd; 2471fd7a8150SMike Barcroft 2472fd7a8150SMike Barcroft newcred = crget(); 2473fd7a8150SMike Barcroft PROC_LOCK(p); 24741fb6767dSJamie Gritton oldcred = crcopysafe(p, newcred); 247569c4ee54SJohn Baldwin newcred->cr_prison = pr; 2476daf63fd2SMateusz Guzik proc_set_cred(p, newcred); 24771fb6767dSJamie Gritton setsugid(p); 2478097055e2SEdward Tomasz Napierala #ifdef RACCT 2479097055e2SEdward Tomasz Napierala racct_proc_ucred_changed(p, oldcred, newcred); 2480f87beb93SAndriy Gapon crhold(newcred); 2481f87beb93SAndriy Gapon #endif 2482f87beb93SAndriy Gapon PROC_UNLOCK(p); 2483f87beb93SAndriy Gapon #ifdef RCTL 2484f87beb93SAndriy Gapon rctl_proc_ucred_changed(p, newcred); 2485f87beb93SAndriy Gapon crfree(newcred); 2486097055e2SEdward Tomasz Napierala #endif 24871fb6767dSJamie Gritton prison_deref(oldcred->cr_prison, PD_DEREF | PD_DEUREF); 2488fd7a8150SMike Barcroft crfree(oldcred); 2489fd7a8150SMike Barcroft return (0); 24901fb6767dSJamie Gritton 2491fd7a8150SMike Barcroft e_unlock: 2492b249ce48SMateusz Guzik VOP_UNLOCK(pr->pr_root); 2493b38ff370SJamie Gritton e_revert_osd: 2494b38ff370SJamie Gritton /* Tell modules this thread is still in its old jail after all. */ 24951fb6767dSJamie Gritton (void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td); 2496b38ff370SJamie Gritton prison_deref(pr, PD_DEREF | PD_DEUREF); 2497fd7a8150SMike Barcroft return (error); 2498fd7a8150SMike Barcroft } 2499fd7a8150SMike Barcroft 2500fd7a8150SMike Barcroft /* 2501fd7a8150SMike Barcroft * Returns a locked prison instance, or NULL on failure. 2502fd7a8150SMike Barcroft */ 250354b369c1SPawel Jakub Dawidek struct prison * 2504fd7a8150SMike Barcroft prison_find(int prid) 2505fd7a8150SMike Barcroft { 2506fd7a8150SMike Barcroft struct prison *pr; 2507fd7a8150SMike Barcroft 2508dc68a633SPawel Jakub Dawidek sx_assert(&allprison_lock, SX_LOCKED); 2509b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 2510fd7a8150SMike Barcroft if (pr->pr_id == prid) { 2511fd7a8150SMike Barcroft mtx_lock(&pr->pr_mtx); 2512b38ff370SJamie Gritton if (pr->pr_ref > 0) 2513fd7a8150SMike Barcroft return (pr); 2514*7de883c8SJamie Gritton /* 2515*7de883c8SJamie Gritton * Any active prison with the same ID would have 2516*7de883c8SJamie Gritton * been inserted before a dead one. 2517*7de883c8SJamie Gritton */ 2518b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2519*7de883c8SJamie Gritton break; 2520fd7a8150SMike Barcroft } 2521*7de883c8SJamie Gritton if (pr->pr_id > prid) 2522*7de883c8SJamie Gritton break; 2523fd7a8150SMike Barcroft } 2524fd7a8150SMike Barcroft return (NULL); 2525fd7a8150SMike Barcroft } 2526fd7a8150SMike Barcroft 2527b38ff370SJamie Gritton /* 25280304c731SJamie Gritton * Find a prison that is a descendant of mypr. Returns a locked prison or NULL. 2529b38ff370SJamie Gritton */ 2530b38ff370SJamie Gritton struct prison * 25310304c731SJamie Gritton prison_find_child(struct prison *mypr, int prid) 2532b38ff370SJamie Gritton { 25330304c731SJamie Gritton struct prison *pr; 25340304c731SJamie Gritton int descend; 2535b38ff370SJamie Gritton 2536b38ff370SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 25370304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 25380304c731SJamie Gritton if (pr->pr_id == prid) { 25390304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 25400304c731SJamie Gritton if (pr->pr_ref > 0) 25410304c731SJamie Gritton return (pr); 25420304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 25430304c731SJamie Gritton } 25440304c731SJamie Gritton } 25450304c731SJamie Gritton return (NULL); 25460304c731SJamie Gritton } 25470304c731SJamie Gritton 25480304c731SJamie Gritton /* 25490304c731SJamie Gritton * Look for the name relative to mypr. Returns a locked prison or NULL. 25500304c731SJamie Gritton */ 25510304c731SJamie Gritton struct prison * 25520304c731SJamie Gritton prison_find_name(struct prison *mypr, const char *name) 25530304c731SJamie Gritton { 25540304c731SJamie Gritton struct prison *pr, *deadpr; 25550304c731SJamie Gritton size_t mylen; 25560304c731SJamie Gritton int descend; 25570304c731SJamie Gritton 25580304c731SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 25590304c731SJamie Gritton mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1; 2560b38ff370SJamie Gritton again: 2561b38ff370SJamie Gritton deadpr = NULL; 25620304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 25630304c731SJamie Gritton if (!strcmp(pr->pr_name + mylen, name)) { 2564b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 2565b38ff370SJamie Gritton if (pr->pr_ref > 0) { 2566b38ff370SJamie Gritton if (pr->pr_uref > 0) 2567b38ff370SJamie Gritton return (pr); 2568b38ff370SJamie Gritton deadpr = pr; 2569b38ff370SJamie Gritton } 2570b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2571b38ff370SJamie Gritton } 2572b38ff370SJamie Gritton } 25730304c731SJamie Gritton /* There was no valid prison - perhaps there was a dying one. */ 2574b38ff370SJamie Gritton if (deadpr != NULL) { 2575b38ff370SJamie Gritton mtx_lock(&deadpr->pr_mtx); 2576b38ff370SJamie Gritton if (deadpr->pr_ref == 0) { 2577b38ff370SJamie Gritton mtx_unlock(&deadpr->pr_mtx); 2578b38ff370SJamie Gritton goto again; 2579b38ff370SJamie Gritton } 2580b38ff370SJamie Gritton } 2581b38ff370SJamie Gritton return (deadpr); 2582b38ff370SJamie Gritton } 2583b38ff370SJamie Gritton 2584b38ff370SJamie Gritton /* 25850304c731SJamie Gritton * See if a prison has the specific flag set. 25860304c731SJamie Gritton */ 25870304c731SJamie Gritton int 25880304c731SJamie Gritton prison_flag(struct ucred *cred, unsigned flag) 25890304c731SJamie Gritton { 25900304c731SJamie Gritton 25910304c731SJamie Gritton /* This is an atomic read, so no locking is necessary. */ 25920304c731SJamie Gritton return (cred->cr_prison->pr_flags & flag); 25930304c731SJamie Gritton } 25940304c731SJamie Gritton 25950304c731SJamie Gritton int 25960304c731SJamie Gritton prison_allow(struct ucred *cred, unsigned flag) 25970304c731SJamie Gritton { 25980304c731SJamie Gritton 25990304c731SJamie Gritton /* This is an atomic read, so no locking is necessary. */ 26000304c731SJamie Gritton return (cred->cr_prison->pr_allow & flag); 26010304c731SJamie Gritton } 26020304c731SJamie Gritton 26030304c731SJamie Gritton /* 2604b38ff370SJamie Gritton * Remove a prison reference. If that was the last reference, remove the 2605b38ff370SJamie Gritton * prison itself - but not in this context in case there are locks held. 2606b38ff370SJamie Gritton */ 260791421ba2SRobert Watson void 26081ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr) 260991421ba2SRobert Watson { 261073d9e52dSJamie Gritton int ref; 261191421ba2SRobert Watson 26121ba4a712SPawel Jakub Dawidek mtx_assert(&pr->pr_mtx, MA_OWNED); 261373d9e52dSJamie Gritton ref = --pr->pr_ref; 261401137630SRobert Watson mtx_unlock(&pr->pr_mtx); 261573d9e52dSJamie Gritton if (ref == 0) 2616c2cda609SPawel Jakub Dawidek taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 2617c2cda609SPawel Jakub Dawidek } 2618c2cda609SPawel Jakub Dawidek 26191ba4a712SPawel Jakub Dawidek void 26201ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr) 26211ba4a712SPawel Jakub Dawidek { 26221ba4a712SPawel Jakub Dawidek 26231ba4a712SPawel Jakub Dawidek mtx_lock(&pr->pr_mtx); 26241ba4a712SPawel Jakub Dawidek prison_free_locked(pr); 26251ba4a712SPawel Jakub Dawidek } 26261ba4a712SPawel Jakub Dawidek 262773d9e52dSJamie Gritton /* 262873d9e52dSJamie Gritton * Complete a call to either prison_free or prison_proc_free. 262973d9e52dSJamie Gritton */ 2630c2cda609SPawel Jakub Dawidek static void 2631c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending) 2632c2cda609SPawel Jakub Dawidek { 263373d9e52dSJamie Gritton struct prison *pr = context; 2634b38ff370SJamie Gritton 2635ef0ddea3SJamie Gritton sx_xlock(&allprison_lock); 263673d9e52dSJamie Gritton mtx_lock(&pr->pr_mtx); 263773d9e52dSJamie Gritton prison_deref(pr, pr->pr_uref 2638ef0ddea3SJamie Gritton ? PD_DEREF | PD_DEUREF | PD_LOCKED | PD_LIST_XLOCKED 2639ef0ddea3SJamie Gritton : PD_LOCKED | PD_LIST_XLOCKED); 2640b38ff370SJamie Gritton } 2641b38ff370SJamie Gritton 2642b38ff370SJamie Gritton /* 2643b38ff370SJamie Gritton * Remove a prison reference (usually). This internal version assumes no 2644b38ff370SJamie Gritton * mutexes are held, except perhaps the prison itself. If there are no more 2645b38ff370SJamie Gritton * references, release and delist the prison. On completion, the prison lock 2646b38ff370SJamie Gritton * and the allprison lock are both unlocked. 2647b38ff370SJamie Gritton */ 2648b38ff370SJamie Gritton static void 2649b38ff370SJamie Gritton prison_deref(struct prison *pr, int flags) 2650b38ff370SJamie Gritton { 26510304c731SJamie Gritton struct prison *ppr, *tpr; 2652cc5fd8c7SJamie Gritton int ref, lasturef; 2653c2cda609SPawel Jakub Dawidek 2654b38ff370SJamie Gritton if (!(flags & PD_LOCKED)) 2655b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 26560304c731SJamie Gritton for (;;) { 2657e6d5cb63SJamie Gritton if (flags & PD_DEUREF) { 265873d9e52dSJamie Gritton KASSERT(pr->pr_uref > 0, 265973d9e52dSJamie Gritton ("prison_deref PD_DEUREF on a dead prison (jid=%d)", 266073d9e52dSJamie Gritton pr->pr_id)); 2661e6d5cb63SJamie Gritton pr->pr_uref--; 2662cc5fd8c7SJamie Gritton lasturef = pr->pr_uref == 0; 2663cc5fd8c7SJamie Gritton if (lasturef) 2664cc5fd8c7SJamie Gritton pr->pr_ref++; 2665e6d5cb63SJamie Gritton KASSERT(prison0.pr_uref != 0, ("prison0 pr_uref=0")); 2666cc5fd8c7SJamie Gritton } else 2667cc5fd8c7SJamie Gritton lasturef = 0; 266873d9e52dSJamie Gritton if (flags & PD_DEREF) { 266973d9e52dSJamie Gritton KASSERT(pr->pr_ref > 0, 267073d9e52dSJamie Gritton ("prison_deref PD_DEREF on a dead prison (jid=%d)", 267173d9e52dSJamie Gritton pr->pr_id)); 2672b38ff370SJamie Gritton pr->pr_ref--; 267373d9e52dSJamie Gritton } 2674cc5fd8c7SJamie Gritton ref = pr->pr_ref; 2675b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2676cc5fd8c7SJamie Gritton 2677cc5fd8c7SJamie Gritton /* 2678cc5fd8c7SJamie Gritton * Tell the modules if the last user reference was removed 2679cc5fd8c7SJamie Gritton * (even it sticks around in dying state). 2680cc5fd8c7SJamie Gritton */ 2681cc5fd8c7SJamie Gritton if (lasturef) { 2682cc5fd8c7SJamie Gritton if (!(flags & (PD_LIST_SLOCKED | PD_LIST_XLOCKED))) { 2683cc5fd8c7SJamie Gritton sx_xlock(&allprison_lock); 2684cc5fd8c7SJamie Gritton flags |= PD_LIST_XLOCKED; 2685cc5fd8c7SJamie Gritton } 2686cc5fd8c7SJamie Gritton (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL); 2687cc5fd8c7SJamie Gritton mtx_lock(&pr->pr_mtx); 2688cc5fd8c7SJamie Gritton ref = --pr->pr_ref; 2689cc5fd8c7SJamie Gritton mtx_unlock(&pr->pr_mtx); 2690cc5fd8c7SJamie Gritton } 2691cc5fd8c7SJamie Gritton 2692cc5fd8c7SJamie Gritton /* If the prison still has references, nothing else to do. */ 2693cc5fd8c7SJamie Gritton if (ref > 0) { 2694b38ff370SJamie Gritton if (flags & PD_LIST_SLOCKED) 2695b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2696b38ff370SJamie Gritton else if (flags & PD_LIST_XLOCKED) 2697b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2698b38ff370SJamie Gritton return; 2699b38ff370SJamie Gritton } 2700c2cda609SPawel Jakub Dawidek 2701b38ff370SJamie Gritton if (flags & PD_LIST_SLOCKED) { 2702b38ff370SJamie Gritton if (!sx_try_upgrade(&allprison_lock)) { 2703b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2704c2cda609SPawel Jakub Dawidek sx_xlock(&allprison_lock); 2705b38ff370SJamie Gritton } 2706b38ff370SJamie Gritton } else if (!(flags & PD_LIST_XLOCKED)) 2707b38ff370SJamie Gritton sx_xlock(&allprison_lock); 2708b38ff370SJamie Gritton 2709b38ff370SJamie Gritton TAILQ_REMOVE(&allprison, pr, pr_list); 27100304c731SJamie Gritton LIST_REMOVE(pr, pr_sibling); 27110304c731SJamie Gritton ppr = pr->pr_parent; 27120304c731SJamie Gritton for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 2713b97457e2SJamie Gritton tpr->pr_childcount--; 2714c4884ffaSJamie Gritton sx_xunlock(&allprison_lock); 27151ba4a712SPawel Jakub Dawidek 2716679e1390SJamie Gritton #ifdef VIMAGE 27170cb8b6a9SMarko Zec if (pr->pr_vnet != ppr->pr_vnet) 2718679e1390SJamie Gritton vnet_destroy(pr->pr_vnet); 2719679e1390SJamie Gritton #endif 27205050aa86SKonstantin Belousov if (pr->pr_root != NULL) 2721b3059e09SRobert Watson vrele(pr->pr_root); 2722b3059e09SRobert Watson mtx_destroy(&pr->pr_mtx); 2723413628a7SBjoern A. Zeeb #ifdef INET 2724413628a7SBjoern A. Zeeb free(pr->pr_ip4, M_PRISON); 2725413628a7SBjoern A. Zeeb #endif 2726b38ff370SJamie Gritton #ifdef INET6 2727b38ff370SJamie Gritton free(pr->pr_ip6, M_PRISON); 2728b38ff370SJamie Gritton #endif 2729b38ff370SJamie Gritton if (pr->pr_cpuset != NULL) 2730b38ff370SJamie Gritton cpuset_rel(pr->pr_cpuset); 2731b38ff370SJamie Gritton osd_jail_exit(pr); 2732a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 27334b5c9cf6SEdward Tomasz Napierala if (racct_enable) 2734a7ad07bfSEdward Tomasz Napierala prison_racct_detach(pr); 2735ec125fbbSEdward Tomasz Napierala #endif 27361ede983cSDag-Erling Smørgrav free(pr, M_PRISON); 27370304c731SJamie Gritton 27380304c731SJamie Gritton /* Removing a prison frees a reference on its parent. */ 27390304c731SJamie Gritton pr = ppr; 27400304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 2741e6d5cb63SJamie Gritton flags = PD_DEREF | PD_DEUREF; 27420304c731SJamie Gritton } 2743b3059e09SRobert Watson } 2744b3059e09SRobert Watson 274591421ba2SRobert Watson void 27461ba4a712SPawel Jakub Dawidek prison_hold_locked(struct prison *pr) 27471ba4a712SPawel Jakub Dawidek { 27481ba4a712SPawel Jakub Dawidek 27491ba4a712SPawel Jakub Dawidek mtx_assert(&pr->pr_mtx, MA_OWNED); 27501ba4a712SPawel Jakub Dawidek KASSERT(pr->pr_ref > 0, 275136b41cc3SBjoern A. Zeeb ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id)); 27521ba4a712SPawel Jakub Dawidek pr->pr_ref++; 27531ba4a712SPawel Jakub Dawidek } 27541ba4a712SPawel Jakub Dawidek 27551ba4a712SPawel Jakub Dawidek void 275691421ba2SRobert Watson prison_hold(struct prison *pr) 275791421ba2SRobert Watson { 275891421ba2SRobert Watson 275901137630SRobert Watson mtx_lock(&pr->pr_mtx); 27601ba4a712SPawel Jakub Dawidek prison_hold_locked(pr); 276101137630SRobert Watson mtx_unlock(&pr->pr_mtx); 276201137630SRobert Watson } 276301137630SRobert Watson 2764413628a7SBjoern A. Zeeb void 2765413628a7SBjoern A. Zeeb prison_proc_hold(struct prison *pr) 276601137630SRobert Watson { 276701137630SRobert Watson 2768413628a7SBjoern A. Zeeb mtx_lock(&pr->pr_mtx); 2769b38ff370SJamie Gritton KASSERT(pr->pr_uref > 0, 2770b38ff370SJamie Gritton ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id)); 2771b38ff370SJamie Gritton pr->pr_uref++; 2772413628a7SBjoern A. Zeeb mtx_unlock(&pr->pr_mtx); 277375c13541SPoul-Henning Kamp } 277475c13541SPoul-Henning Kamp 277575c13541SPoul-Henning Kamp void 2776413628a7SBjoern A. Zeeb prison_proc_free(struct prison *pr) 277775c13541SPoul-Henning Kamp { 2778413628a7SBjoern A. Zeeb 2779413628a7SBjoern A. Zeeb mtx_lock(&pr->pr_mtx); 2780b38ff370SJamie Gritton KASSERT(pr->pr_uref > 0, 2781b38ff370SJamie Gritton ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id)); 278273d9e52dSJamie Gritton if (pr->pr_uref > 1) 278373d9e52dSJamie Gritton pr->pr_uref--; 278473d9e52dSJamie Gritton else { 278573d9e52dSJamie Gritton /* 278673d9e52dSJamie Gritton * Don't remove the last user reference in this context, which 278773d9e52dSJamie Gritton * is expected to be a process that is not only locked, but 278873d9e52dSJamie Gritton * also half dead. 278973d9e52dSJamie Gritton */ 279073d9e52dSJamie Gritton pr->pr_ref++; 279173d9e52dSJamie Gritton mtx_unlock(&pr->pr_mtx); 279273d9e52dSJamie Gritton taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 279373d9e52dSJamie Gritton return; 279473d9e52dSJamie Gritton } 279573d9e52dSJamie Gritton mtx_unlock(&pr->pr_mtx); 2796413628a7SBjoern A. Zeeb } 2797413628a7SBjoern A. Zeeb 2798413628a7SBjoern A. Zeeb /* 2799ca04ba64SJamie Gritton * Check if a jail supports the given address family. 2800ca04ba64SJamie Gritton * 2801ca04ba64SJamie Gritton * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT 2802ca04ba64SJamie Gritton * if not. 2803ca04ba64SJamie Gritton */ 2804ca04ba64SJamie Gritton int 2805ca04ba64SJamie Gritton prison_check_af(struct ucred *cred, int af) 2806ca04ba64SJamie Gritton { 28070304c731SJamie Gritton struct prison *pr; 2808ca04ba64SJamie Gritton int error; 2809ca04ba64SJamie Gritton 2810ca04ba64SJamie Gritton KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2811ca04ba64SJamie Gritton 28120304c731SJamie Gritton pr = cred->cr_prison; 2813499650a0SJamie Gritton #ifdef VIMAGE 28146bb79563SJamie Gritton /* Prisons with their own network stack are not limited. */ 2815de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 28166bb79563SJamie Gritton return (0); 2817499650a0SJamie Gritton #endif 28186bb79563SJamie Gritton 2819ca04ba64SJamie Gritton error = 0; 2820ca04ba64SJamie Gritton switch (af) 2821ca04ba64SJamie Gritton { 2822ca04ba64SJamie Gritton #ifdef INET 2823ca04ba64SJamie Gritton case AF_INET: 28240304c731SJamie Gritton if (pr->pr_flags & PR_IP4) 28250304c731SJamie Gritton { 28260304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 28270304c731SJamie Gritton if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL) 2828ca04ba64SJamie Gritton error = EAFNOSUPPORT; 28290304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 28300304c731SJamie Gritton } 2831ca04ba64SJamie Gritton break; 2832ca04ba64SJamie Gritton #endif 2833ca04ba64SJamie Gritton #ifdef INET6 2834ca04ba64SJamie Gritton case AF_INET6: 28350304c731SJamie Gritton if (pr->pr_flags & PR_IP6) 28360304c731SJamie Gritton { 28370304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 28380304c731SJamie Gritton if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL) 2839ca04ba64SJamie Gritton error = EAFNOSUPPORT; 28400304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 28410304c731SJamie Gritton } 2842ca04ba64SJamie Gritton break; 2843ca04ba64SJamie Gritton #endif 2844ca04ba64SJamie Gritton case AF_LOCAL: 2845ca04ba64SJamie Gritton case AF_ROUTE: 2846ca04ba64SJamie Gritton break; 2847ca04ba64SJamie Gritton default: 28480304c731SJamie Gritton if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF)) 2849ca04ba64SJamie Gritton error = EAFNOSUPPORT; 2850ca04ba64SJamie Gritton } 2851ca04ba64SJamie Gritton return (error); 2852ca04ba64SJamie Gritton } 2853ca04ba64SJamie Gritton 2854ca04ba64SJamie Gritton /* 2855413628a7SBjoern A. Zeeb * Check if given address belongs to the jail referenced by cred (wrapper to 2856413628a7SBjoern A. Zeeb * prison_check_ip[46]). 2857413628a7SBjoern A. Zeeb * 28580304c731SJamie Gritton * Returns 0 if jail doesn't restrict the address family or if address belongs 28590304c731SJamie Gritton * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if 28600304c731SJamie Gritton * the jail doesn't allow the address family. IPv4 Address passed in in NBO. 2861413628a7SBjoern A. Zeeb */ 2862413628a7SBjoern A. Zeeb int 2863c83dda36SAlexander V. Chernikov prison_if(struct ucred *cred, const struct sockaddr *sa) 286475c13541SPoul-Henning Kamp { 2865413628a7SBjoern A. Zeeb #ifdef INET 2866c83dda36SAlexander V. Chernikov const struct sockaddr_in *sai; 2867413628a7SBjoern A. Zeeb #endif 2868413628a7SBjoern A. Zeeb #ifdef INET6 2869c83dda36SAlexander V. Chernikov const struct sockaddr_in6 *sai6; 2870413628a7SBjoern A. Zeeb #endif 2871b89e82ddSJamie Gritton int error; 287275c13541SPoul-Henning Kamp 2873413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2874413628a7SBjoern A. Zeeb KASSERT(sa != NULL, ("%s: sa is NULL", __func__)); 2875413628a7SBjoern A. Zeeb 2876de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE 2877de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 2878de0bd6f7SBjoern A. Zeeb return (0); 2879de0bd6f7SBjoern A. Zeeb #endif 2880de0bd6f7SBjoern A. Zeeb 2881b89e82ddSJamie Gritton error = 0; 2882413628a7SBjoern A. Zeeb switch (sa->sa_family) 2883413628a7SBjoern A. Zeeb { 2884413628a7SBjoern A. Zeeb #ifdef INET 2885413628a7SBjoern A. Zeeb case AF_INET: 2886c83dda36SAlexander V. Chernikov sai = (const struct sockaddr_in *)sa; 2887b89e82ddSJamie Gritton error = prison_check_ip4(cred, &sai->sin_addr); 2888413628a7SBjoern A. Zeeb break; 2889413628a7SBjoern A. Zeeb #endif 2890413628a7SBjoern A. Zeeb #ifdef INET6 2891413628a7SBjoern A. Zeeb case AF_INET6: 2892c83dda36SAlexander V. Chernikov sai6 = (const struct sockaddr_in6 *)sa; 2893b89e82ddSJamie Gritton error = prison_check_ip6(cred, &sai6->sin6_addr); 2894413628a7SBjoern A. Zeeb break; 2895413628a7SBjoern A. Zeeb #endif 2896413628a7SBjoern A. Zeeb default: 28970304c731SJamie Gritton if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF)) 2898b89e82ddSJamie Gritton error = EAFNOSUPPORT; 2899413628a7SBjoern A. Zeeb } 2900b89e82ddSJamie Gritton return (error); 290175c13541SPoul-Henning Kamp } 290291421ba2SRobert Watson 290391421ba2SRobert Watson /* 290491421ba2SRobert Watson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH. 290591421ba2SRobert Watson */ 290691421ba2SRobert Watson int 29079ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2) 290891421ba2SRobert Watson { 290991421ba2SRobert Watson 29100304c731SJamie Gritton return ((cred1->cr_prison == cred2->cr_prison || 29110304c731SJamie Gritton prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH); 29120304c731SJamie Gritton } 291391421ba2SRobert Watson 29140304c731SJamie Gritton /* 29150304c731SJamie Gritton * Return 1 if p2 is a child of p1, otherwise 0. 29160304c731SJamie Gritton */ 29170304c731SJamie Gritton int 29180304c731SJamie Gritton prison_ischild(struct prison *pr1, struct prison *pr2) 29190304c731SJamie Gritton { 29200304c731SJamie Gritton 29210304c731SJamie Gritton for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent) 29220304c731SJamie Gritton if (pr1 == pr2) 29230304c731SJamie Gritton return (1); 292491421ba2SRobert Watson return (0); 292591421ba2SRobert Watson } 292691421ba2SRobert Watson 292791421ba2SRobert Watson /* 2928de0bd6f7SBjoern A. Zeeb * Return 1 if the passed credential is in a jail and that jail does not 2929de0bd6f7SBjoern A. Zeeb * have its own virtual network stack, otherwise 0. 2930de0bd6f7SBjoern A. Zeeb */ 2931de0bd6f7SBjoern A. Zeeb int 2932de0bd6f7SBjoern A. Zeeb jailed_without_vnet(struct ucred *cred) 2933de0bd6f7SBjoern A. Zeeb { 2934de0bd6f7SBjoern A. Zeeb 2935de0bd6f7SBjoern A. Zeeb if (!jailed(cred)) 2936de0bd6f7SBjoern A. Zeeb return (0); 2937de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE 2938de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 2939de0bd6f7SBjoern A. Zeeb return (0); 2940de0bd6f7SBjoern A. Zeeb #endif 2941de0bd6f7SBjoern A. Zeeb 2942de0bd6f7SBjoern A. Zeeb return (1); 2943de0bd6f7SBjoern A. Zeeb } 2944de0bd6f7SBjoern A. Zeeb 2945de0bd6f7SBjoern A. Zeeb /* 29467455b100SJamie Gritton * Return the correct hostname (domainname, et al) for the passed credential. 29479484d0c0SRobert Drehmel */ 2948ad1ff099SRobert Drehmel void 29499ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size) 29509484d0c0SRobert Drehmel { 295176ca6f88SJamie Gritton struct prison *pr; 29529484d0c0SRobert Drehmel 29537455b100SJamie Gritton /* 29547455b100SJamie Gritton * A NULL credential can be used to shortcut to the physical 29557455b100SJamie Gritton * system's hostname. 29567455b100SJamie Gritton */ 295776ca6f88SJamie Gritton pr = (cred != NULL) ? cred->cr_prison : &prison0; 295876ca6f88SJamie Gritton mtx_lock(&pr->pr_mtx); 2959c1f19219SJamie Gritton strlcpy(buf, pr->pr_hostname, size); 296076ca6f88SJamie Gritton mtx_unlock(&pr->pr_mtx); 29619484d0c0SRobert Drehmel } 2962fd7a8150SMike Barcroft 29637455b100SJamie Gritton void 29647455b100SJamie Gritton getcreddomainname(struct ucred *cred, char *buf, size_t size) 29657455b100SJamie Gritton { 29667455b100SJamie Gritton 29677455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 2968c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_domainname, size); 29697455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 29707455b100SJamie Gritton } 29717455b100SJamie Gritton 29727455b100SJamie Gritton void 29737455b100SJamie Gritton getcredhostuuid(struct ucred *cred, char *buf, size_t size) 29747455b100SJamie Gritton { 29757455b100SJamie Gritton 29767455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 2977c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_hostuuid, size); 29787455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 29797455b100SJamie Gritton } 29807455b100SJamie Gritton 29817455b100SJamie Gritton void 29827455b100SJamie Gritton getcredhostid(struct ucred *cred, unsigned long *hostid) 29837455b100SJamie Gritton { 29847455b100SJamie Gritton 29857455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 29867455b100SJamie Gritton *hostid = cred->cr_prison->pr_hostid; 29877455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 29887455b100SJamie Gritton } 29897455b100SJamie Gritton 29903f8bc99cSKristof Provost void 29913f8bc99cSKristof Provost getjailname(struct ucred *cred, char *name, size_t len) 29923f8bc99cSKristof Provost { 29933f8bc99cSKristof Provost 29943f8bc99cSKristof Provost mtx_lock(&cred->cr_prison->pr_mtx); 29953f8bc99cSKristof Provost strlcpy(name, cred->cr_prison->pr_name, len); 29963f8bc99cSKristof Provost mtx_unlock(&cred->cr_prison->pr_mtx); 29973f8bc99cSKristof Provost } 29983f8bc99cSKristof Provost 2999eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE 3000eb79e1c7SBjoern A. Zeeb /* 3001eb79e1c7SBjoern A. Zeeb * Determine whether the prison represented by cred owns 3002eb79e1c7SBjoern A. Zeeb * its vnet rather than having it inherited. 3003eb79e1c7SBjoern A. Zeeb * 3004eb79e1c7SBjoern A. Zeeb * Returns 1 in case the prison owns the vnet, 0 otherwise. 3005eb79e1c7SBjoern A. Zeeb */ 3006eb79e1c7SBjoern A. Zeeb int 3007eb79e1c7SBjoern A. Zeeb prison_owns_vnet(struct ucred *cred) 3008eb79e1c7SBjoern A. Zeeb { 3009eb79e1c7SBjoern A. Zeeb 3010eb79e1c7SBjoern A. Zeeb /* 3011eb79e1c7SBjoern A. Zeeb * vnets cannot be added/removed after jail creation, 3012eb79e1c7SBjoern A. Zeeb * so no need to lock here. 3013eb79e1c7SBjoern A. Zeeb */ 3014eb79e1c7SBjoern A. Zeeb return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0); 3015eb79e1c7SBjoern A. Zeeb } 3016eb79e1c7SBjoern A. Zeeb #endif 3017eb79e1c7SBjoern A. Zeeb 3018f08df373SRobert Watson /* 3019820a0de9SPawel Jakub Dawidek * Determine whether the subject represented by cred can "see" 3020820a0de9SPawel Jakub Dawidek * status of a mount point. 3021820a0de9SPawel Jakub Dawidek * Returns: 0 for permitted, ENOENT otherwise. 3022820a0de9SPawel Jakub Dawidek * XXX: This function should be called cr_canseemount() and should be 3023820a0de9SPawel Jakub Dawidek * placed in kern_prot.c. 3024f08df373SRobert Watson */ 3025f08df373SRobert Watson int 3026820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp) 3027f08df373SRobert Watson { 3028820a0de9SPawel Jakub Dawidek struct prison *pr; 3029820a0de9SPawel Jakub Dawidek struct statfs *sp; 3030820a0de9SPawel Jakub Dawidek size_t len; 3031f08df373SRobert Watson 3032820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 30330304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 30340304c731SJamie Gritton return (0); 3035820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) 3036820a0de9SPawel Jakub Dawidek return (0); 30370304c731SJamie Gritton if (pr->pr_enforce_statfs == 2) 3038820a0de9SPawel Jakub Dawidek return (ENOENT); 3039820a0de9SPawel Jakub Dawidek /* 3040820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 3041820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 3042820a0de9SPawel Jakub Dawidek * This is ugly check, but this is the only situation when jail's 3043820a0de9SPawel Jakub Dawidek * directory ends with '/'. 3044820a0de9SPawel Jakub Dawidek */ 3045820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 3046820a0de9SPawel Jakub Dawidek return (0); 3047820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 3048820a0de9SPawel Jakub Dawidek sp = &mp->mnt_stat; 3049820a0de9SPawel Jakub Dawidek if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0) 3050820a0de9SPawel Jakub Dawidek return (ENOENT); 3051820a0de9SPawel Jakub Dawidek /* 3052820a0de9SPawel Jakub Dawidek * Be sure that we don't have situation where jail's root directory 3053820a0de9SPawel Jakub Dawidek * is "/some/path" and mount point is "/some/pathpath". 3054820a0de9SPawel Jakub Dawidek */ 3055820a0de9SPawel Jakub Dawidek if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/') 3056820a0de9SPawel Jakub Dawidek return (ENOENT); 3057f08df373SRobert Watson return (0); 3058f08df373SRobert Watson } 3059820a0de9SPawel Jakub Dawidek 3060820a0de9SPawel Jakub Dawidek void 3061820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp) 3062820a0de9SPawel Jakub Dawidek { 3063820a0de9SPawel Jakub Dawidek char jpath[MAXPATHLEN]; 3064820a0de9SPawel Jakub Dawidek struct prison *pr; 3065820a0de9SPawel Jakub Dawidek size_t len; 3066820a0de9SPawel Jakub Dawidek 3067820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 30680304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 30690304c731SJamie Gritton return; 3070820a0de9SPawel Jakub Dawidek if (prison_canseemount(cred, mp) != 0) { 3071820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3072820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, "[restricted]", 3073820a0de9SPawel Jakub Dawidek sizeof(sp->f_mntonname)); 3074820a0de9SPawel Jakub Dawidek return; 3075820a0de9SPawel Jakub Dawidek } 3076820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) { 3077820a0de9SPawel Jakub Dawidek /* 3078820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3079820a0de9SPawel Jakub Dawidek * the valid path left there. 3080820a0de9SPawel Jakub Dawidek */ 3081820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3082820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3083820a0de9SPawel Jakub Dawidek return; 3084820a0de9SPawel Jakub Dawidek } 3085820a0de9SPawel Jakub Dawidek /* 3086820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 3087820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 3088820a0de9SPawel Jakub Dawidek */ 3089820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 3090820a0de9SPawel Jakub Dawidek return; 3091820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 3092820a0de9SPawel Jakub Dawidek strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath)); 3093820a0de9SPawel Jakub Dawidek /* 3094820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3095820a0de9SPawel Jakub Dawidek * the valid path left there. 3096820a0de9SPawel Jakub Dawidek */ 3097820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3098820a0de9SPawel Jakub Dawidek if (*jpath == '\0') { 3099820a0de9SPawel Jakub Dawidek /* Should never happen. */ 3100820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3101820a0de9SPawel Jakub Dawidek } else { 3102820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname)); 3103820a0de9SPawel Jakub Dawidek } 3104f08df373SRobert Watson } 3105f08df373SRobert Watson 3106800c9408SRobert Watson /* 3107800c9408SRobert Watson * Check with permission for a specific privilege is granted within jail. We 3108800c9408SRobert Watson * have a specific list of accepted privileges; the rest are denied. 3109800c9408SRobert Watson */ 3110800c9408SRobert Watson int 3111800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv) 3112800c9408SRobert Watson { 3113800c9408SRobert Watson 31147b2ff0dcSMateusz Guzik /* 31157b2ff0dcSMateusz Guzik * Some policies have custom handlers. This routine should not be 31167b2ff0dcSMateusz Guzik * called for them. See priv_check_cred(). 31177b2ff0dcSMateusz Guzik */ 31187b2ff0dcSMateusz Guzik switch (priv) { 3119a459a6cfSMateusz Guzik case PRIV_VFS_LOOKUP: 31207b2ff0dcSMateusz Guzik case PRIV_VFS_GENERATION: 31217b2ff0dcSMateusz Guzik KASSERT(0, ("prison_priv_check instead of a custom handler " 31227b2ff0dcSMateusz Guzik "called for %d\n", priv)); 31237b2ff0dcSMateusz Guzik } 31247b2ff0dcSMateusz Guzik 3125800c9408SRobert Watson if (!jailed(cred)) 3126800c9408SRobert Watson return (0); 3127800c9408SRobert Watson 31286bb79563SJamie Gritton #ifdef VIMAGE 31296bb79563SJamie Gritton /* 31306bb79563SJamie Gritton * Privileges specific to prisons with a virtual network stack. 31316bb79563SJamie Gritton * There might be a duplicate entry here in case the privilege 31326bb79563SJamie Gritton * is only granted conditionally in the legacy jail case. 31336bb79563SJamie Gritton */ 31346bb79563SJamie Gritton switch (priv) { 31356bb79563SJamie Gritton #ifdef notyet 31366bb79563SJamie Gritton /* 31376bb79563SJamie Gritton * NFS-specific privileges. 31386bb79563SJamie Gritton */ 31396bb79563SJamie Gritton case PRIV_NFS_DAEMON: 31406bb79563SJamie Gritton case PRIV_NFS_LOCKD: 31416bb79563SJamie Gritton #endif 31426bb79563SJamie Gritton /* 31436bb79563SJamie Gritton * Network stack privileges. 31446bb79563SJamie Gritton */ 31456bb79563SJamie Gritton case PRIV_NET_BRIDGE: 31466bb79563SJamie Gritton case PRIV_NET_GRE: 31476bb79563SJamie Gritton case PRIV_NET_BPF: 31486bb79563SJamie Gritton case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */ 31496bb79563SJamie Gritton case PRIV_NET_ROUTE: 31506bb79563SJamie Gritton case PRIV_NET_TAP: 31516bb79563SJamie Gritton case PRIV_NET_SETIFMTU: 31526bb79563SJamie Gritton case PRIV_NET_SETIFFLAGS: 31536bb79563SJamie Gritton case PRIV_NET_SETIFCAP: 3154215940b3SXin LI case PRIV_NET_SETIFDESCR: 31556bb79563SJamie Gritton case PRIV_NET_SETIFNAME : 31566bb79563SJamie Gritton case PRIV_NET_SETIFMETRIC: 31576bb79563SJamie Gritton case PRIV_NET_SETIFPHYS: 31586bb79563SJamie Gritton case PRIV_NET_SETIFMAC: 3159389474c1SKonstantin Belousov case PRIV_NET_SETLANPCP: 31606bb79563SJamie Gritton case PRIV_NET_ADDMULTI: 31616bb79563SJamie Gritton case PRIV_NET_DELMULTI: 31626bb79563SJamie Gritton case PRIV_NET_HWIOCTL: 31636bb79563SJamie Gritton case PRIV_NET_SETLLADDR: 31646bb79563SJamie Gritton case PRIV_NET_ADDIFGROUP: 31656bb79563SJamie Gritton case PRIV_NET_DELIFGROUP: 31666bb79563SJamie Gritton case PRIV_NET_IFCREATE: 31676bb79563SJamie Gritton case PRIV_NET_IFDESTROY: 31686bb79563SJamie Gritton case PRIV_NET_ADDIFADDR: 31696bb79563SJamie Gritton case PRIV_NET_DELIFADDR: 31706bb79563SJamie Gritton case PRIV_NET_LAGG: 31716bb79563SJamie Gritton case PRIV_NET_GIF: 31726bb79563SJamie Gritton case PRIV_NET_SETIFVNET: 317335fd7bc0SBjoern A. Zeeb case PRIV_NET_SETIFFIB: 31746bb79563SJamie Gritton 31756bb79563SJamie Gritton /* 31766bb79563SJamie Gritton * 802.11-related privileges. 31776bb79563SJamie Gritton */ 3178f7d38a13SAdrian Chadd case PRIV_NET80211_VAP_GETKEY: 3179f7d38a13SAdrian Chadd case PRIV_NET80211_VAP_MANAGE: 31806bb79563SJamie Gritton 31816bb79563SJamie Gritton #ifdef notyet 31826bb79563SJamie Gritton /* 31836bb79563SJamie Gritton * ATM privileges. 31846bb79563SJamie Gritton */ 31856bb79563SJamie Gritton case PRIV_NETATM_CFG: 31866bb79563SJamie Gritton case PRIV_NETATM_ADD: 31876bb79563SJamie Gritton case PRIV_NETATM_DEL: 31886bb79563SJamie Gritton case PRIV_NETATM_SET: 31896bb79563SJamie Gritton 31906bb79563SJamie Gritton /* 31916bb79563SJamie Gritton * Bluetooth privileges. 31926bb79563SJamie Gritton */ 31936bb79563SJamie Gritton case PRIV_NETBLUETOOTH_RAW: 31946bb79563SJamie Gritton #endif 31956bb79563SJamie Gritton 31966bb79563SJamie Gritton /* 31976bb79563SJamie Gritton * Netgraph and netgraph module privileges. 31986bb79563SJamie Gritton */ 31996bb79563SJamie Gritton case PRIV_NETGRAPH_CONTROL: 32006bb79563SJamie Gritton #ifdef notyet 32016bb79563SJamie Gritton case PRIV_NETGRAPH_TTY: 32026bb79563SJamie Gritton #endif 32036bb79563SJamie Gritton 32046bb79563SJamie Gritton /* 32056bb79563SJamie Gritton * IPv4 and IPv6 privileges. 32066bb79563SJamie Gritton */ 32076bb79563SJamie Gritton case PRIV_NETINET_IPFW: 32086bb79563SJamie Gritton case PRIV_NETINET_DIVERT: 32096bb79563SJamie Gritton case PRIV_NETINET_PF: 32106bb79563SJamie Gritton case PRIV_NETINET_DUMMYNET: 32116bb79563SJamie Gritton case PRIV_NETINET_CARP: 32126bb79563SJamie Gritton case PRIV_NETINET_MROUTE: 32136bb79563SJamie Gritton case PRIV_NETINET_RAW: 32146bb79563SJamie Gritton case PRIV_NETINET_ADDRCTRL6: 32156bb79563SJamie Gritton case PRIV_NETINET_ND6: 32166bb79563SJamie Gritton case PRIV_NETINET_SCOPE6: 32176bb79563SJamie Gritton case PRIV_NETINET_ALIFETIME6: 32186bb79563SJamie Gritton case PRIV_NETINET_IPSEC: 32196bb79563SJamie Gritton case PRIV_NETINET_BINDANY: 32206bb79563SJamie Gritton 32216bb79563SJamie Gritton #ifdef notyet 32226bb79563SJamie Gritton /* 32236bb79563SJamie Gritton * NCP privileges. 32246bb79563SJamie Gritton */ 32256bb79563SJamie Gritton case PRIV_NETNCP: 32266bb79563SJamie Gritton 32276bb79563SJamie Gritton /* 32286bb79563SJamie Gritton * SMB privileges. 32296bb79563SJamie Gritton */ 32306bb79563SJamie Gritton case PRIV_NETSMB: 32316bb79563SJamie Gritton #endif 32326bb79563SJamie Gritton 32336bb79563SJamie Gritton /* 32346bb79563SJamie Gritton * No default: or deny here. 32356bb79563SJamie Gritton * In case of no permit fall through to next switch(). 32366bb79563SJamie Gritton */ 32376bb79563SJamie Gritton if (cred->cr_prison->pr_flags & PR_VNET) 32386bb79563SJamie Gritton return (0); 32396bb79563SJamie Gritton } 32406bb79563SJamie Gritton #endif /* VIMAGE */ 32416bb79563SJamie Gritton 3242800c9408SRobert Watson switch (priv) { 3243800c9408SRobert Watson /* 3244800c9408SRobert Watson * Allow ktrace privileges for root in jail. 3245800c9408SRobert Watson */ 3246800c9408SRobert Watson case PRIV_KTRACE: 3247800c9408SRobert Watson 3248c3c1b5e6SRobert Watson #if 0 3249800c9408SRobert Watson /* 3250800c9408SRobert Watson * Allow jailed processes to configure audit identity and 3251800c9408SRobert Watson * submit audit records (login, etc). In the future we may 3252800c9408SRobert Watson * want to further refine the relationship between audit and 3253800c9408SRobert Watson * jail. 3254800c9408SRobert Watson */ 3255800c9408SRobert Watson case PRIV_AUDIT_GETAUDIT: 3256800c9408SRobert Watson case PRIV_AUDIT_SETAUDIT: 3257800c9408SRobert Watson case PRIV_AUDIT_SUBMIT: 3258c3c1b5e6SRobert Watson #endif 3259800c9408SRobert Watson 3260800c9408SRobert Watson /* 3261800c9408SRobert Watson * Allow jailed processes to manipulate process UNIX 3262800c9408SRobert Watson * credentials in any way they see fit. 3263800c9408SRobert Watson */ 3264800c9408SRobert Watson case PRIV_CRED_SETUID: 3265800c9408SRobert Watson case PRIV_CRED_SETEUID: 3266800c9408SRobert Watson case PRIV_CRED_SETGID: 3267800c9408SRobert Watson case PRIV_CRED_SETEGID: 3268800c9408SRobert Watson case PRIV_CRED_SETGROUPS: 3269800c9408SRobert Watson case PRIV_CRED_SETREUID: 3270800c9408SRobert Watson case PRIV_CRED_SETREGID: 3271800c9408SRobert Watson case PRIV_CRED_SETRESUID: 3272800c9408SRobert Watson case PRIV_CRED_SETRESGID: 3273800c9408SRobert Watson 3274800c9408SRobert Watson /* 3275800c9408SRobert Watson * Jail implements visibility constraints already, so allow 3276800c9408SRobert Watson * jailed root to override uid/gid-based constraints. 3277800c9408SRobert Watson */ 3278800c9408SRobert Watson case PRIV_SEEOTHERGIDS: 3279800c9408SRobert Watson case PRIV_SEEOTHERUIDS: 3280800c9408SRobert Watson 3281800c9408SRobert Watson /* 3282800c9408SRobert Watson * Jail implements inter-process debugging limits already, so 3283800c9408SRobert Watson * allow jailed root various debugging privileges. 3284800c9408SRobert Watson */ 3285800c9408SRobert Watson case PRIV_DEBUG_DIFFCRED: 3286800c9408SRobert Watson case PRIV_DEBUG_SUGID: 3287800c9408SRobert Watson case PRIV_DEBUG_UNPRIV: 3288800c9408SRobert Watson 3289800c9408SRobert Watson /* 3290800c9408SRobert Watson * Allow jail to set various resource limits and login 3291800c9408SRobert Watson * properties, and for now, exceed process resource limits. 3292800c9408SRobert Watson */ 3293800c9408SRobert Watson case PRIV_PROC_LIMIT: 3294800c9408SRobert Watson case PRIV_PROC_SETLOGIN: 3295800c9408SRobert Watson case PRIV_PROC_SETRLIMIT: 3296800c9408SRobert Watson 3297800c9408SRobert Watson /* 3298800c9408SRobert Watson * System V and POSIX IPC privileges are granted in jail. 3299800c9408SRobert Watson */ 3300800c9408SRobert Watson case PRIV_IPC_READ: 3301800c9408SRobert Watson case PRIV_IPC_WRITE: 3302800c9408SRobert Watson case PRIV_IPC_ADMIN: 3303800c9408SRobert Watson case PRIV_IPC_MSGSIZE: 3304800c9408SRobert Watson case PRIV_MQ_ADMIN: 3305800c9408SRobert Watson 3306800c9408SRobert Watson /* 33070304c731SJamie Gritton * Jail operations within a jail work on child jails. 33080304c731SJamie Gritton */ 33090304c731SJamie Gritton case PRIV_JAIL_ATTACH: 33100304c731SJamie Gritton case PRIV_JAIL_SET: 33110304c731SJamie Gritton case PRIV_JAIL_REMOVE: 33120304c731SJamie Gritton 33130304c731SJamie Gritton /* 3314800c9408SRobert Watson * Jail implements its own inter-process limits, so allow 3315800c9408SRobert Watson * root processes in jail to change scheduling on other 3316800c9408SRobert Watson * processes in the same jail. Likewise for signalling. 3317800c9408SRobert Watson */ 3318800c9408SRobert Watson case PRIV_SCHED_DIFFCRED: 3319413628a7SBjoern A. Zeeb case PRIV_SCHED_CPUSET: 3320800c9408SRobert Watson case PRIV_SIGNAL_DIFFCRED: 3321800c9408SRobert Watson case PRIV_SIGNAL_SUGID: 3322800c9408SRobert Watson 3323800c9408SRobert Watson /* 3324800c9408SRobert Watson * Allow jailed processes to write to sysctls marked as jail 3325800c9408SRobert Watson * writable. 3326800c9408SRobert Watson */ 3327800c9408SRobert Watson case PRIV_SYSCTL_WRITEJAIL: 3328800c9408SRobert Watson 3329800c9408SRobert Watson /* 3330800c9408SRobert Watson * Allow root in jail to manage a variety of quota 3331e82d0201SRobert Watson * properties. These should likely be conditional on a 3332e82d0201SRobert Watson * configuration option. 3333800c9408SRobert Watson */ 333495b091d2SRobert Watson case PRIV_VFS_GETQUOTA: 333595b091d2SRobert Watson case PRIV_VFS_SETQUOTA: 3336800c9408SRobert Watson 3337800c9408SRobert Watson /* 3338800c9408SRobert Watson * Since Jail relies on chroot() to implement file system 3339800c9408SRobert Watson * protections, grant many VFS privileges to root in jail. 3340800c9408SRobert Watson * Be careful to exclude mount-related and NFS-related 3341800c9408SRobert Watson * privileges. 3342800c9408SRobert Watson */ 3343800c9408SRobert Watson case PRIV_VFS_READ: 3344800c9408SRobert Watson case PRIV_VFS_WRITE: 3345800c9408SRobert Watson case PRIV_VFS_ADMIN: 3346800c9408SRobert Watson case PRIV_VFS_EXEC: 3347800c9408SRobert Watson case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */ 3348800c9408SRobert Watson case PRIV_VFS_CHFLAGS_DEV: 3349800c9408SRobert Watson case PRIV_VFS_CHOWN: 3350800c9408SRobert Watson case PRIV_VFS_CHROOT: 3351bb531912SPawel Jakub Dawidek case PRIV_VFS_RETAINSUGID: 3352800c9408SRobert Watson case PRIV_VFS_FCHROOT: 3353800c9408SRobert Watson case PRIV_VFS_LINK: 3354800c9408SRobert Watson case PRIV_VFS_SETGID: 3355e41966dcSRobert Watson case PRIV_VFS_STAT: 3356800c9408SRobert Watson case PRIV_VFS_STICKYFILE: 3357bb56d716SJamie Gritton 3358bb56d716SJamie Gritton /* 3359bb56d716SJamie Gritton * As in the non-jail case, non-root users are expected to be 3360bb56d716SJamie Gritton * able to read kernel/phyiscal memory (provided /dev/[k]mem 3361bb56d716SJamie Gritton * exists in the jail and they have permission to access it). 3362bb56d716SJamie Gritton */ 3363bb56d716SJamie Gritton case PRIV_KMEM_READ: 3364800c9408SRobert Watson return (0); 3365800c9408SRobert Watson 3366800c9408SRobert Watson /* 3367800c9408SRobert Watson * Depending on the global setting, allow privilege of 3368800c9408SRobert Watson * setting system flags. 3369800c9408SRobert Watson */ 3370800c9408SRobert Watson case PRIV_VFS_SYSFLAGS: 33710304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS) 3372800c9408SRobert Watson return (0); 3373800c9408SRobert Watson else 3374800c9408SRobert Watson return (EPERM); 3375800c9408SRobert Watson 3376800c9408SRobert Watson /* 3377f3a8d2f9SPawel Jakub Dawidek * Depending on the global setting, allow privilege of 3378f3a8d2f9SPawel Jakub Dawidek * mounting/unmounting file systems. 3379f3a8d2f9SPawel Jakub Dawidek */ 3380f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT: 3381f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_UNMOUNT: 3382f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT_NONUSER: 338324b0502eSPawel Jakub Dawidek case PRIV_VFS_MOUNT_OWNER: 3384435d4667SMartin Matuska if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT && 3385435d4667SMartin Matuska cred->cr_prison->pr_enforce_statfs < 2) 3386f3a8d2f9SPawel Jakub Dawidek return (0); 3387f3a8d2f9SPawel Jakub Dawidek else 3388f3a8d2f9SPawel Jakub Dawidek return (EPERM); 3389f3a8d2f9SPawel Jakub Dawidek 3390f3a8d2f9SPawel Jakub Dawidek /* 339163619b6dSKyle Evans * Jails should hold no disposition on the PRIV_VFS_READ_DIR 339263619b6dSKyle Evans * policy. priv_check_cred will not specifically allow it, and 339363619b6dSKyle Evans * we may want a MAC policy to allow it. 339463619b6dSKyle Evans */ 339563619b6dSKyle Evans case PRIV_VFS_READ_DIR: 339663619b6dSKyle Evans return (0); 339763619b6dSKyle Evans 339863619b6dSKyle Evans /* 3399ccd6ac9fSAntoine Brodin * Conditionnaly allow locking (unlocking) physical pages 3400ccd6ac9fSAntoine Brodin * in memory. 3401ccd6ac9fSAntoine Brodin */ 3402ccd6ac9fSAntoine Brodin case PRIV_VM_MLOCK: 3403ccd6ac9fSAntoine Brodin case PRIV_VM_MUNLOCK: 3404ccd6ac9fSAntoine Brodin if (cred->cr_prison->pr_allow & PR_ALLOW_MLOCK) 3405ccd6ac9fSAntoine Brodin return (0); 3406ccd6ac9fSAntoine Brodin else 3407ccd6ac9fSAntoine Brodin return (EPERM); 3408ccd6ac9fSAntoine Brodin 3409ccd6ac9fSAntoine Brodin /* 3410e28f9b7dSAllan Jude * Conditionally allow jailed root to bind reserved ports. 3411800c9408SRobert Watson */ 3412800c9408SRobert Watson case PRIV_NETINET_RESERVEDPORT: 3413e28f9b7dSAllan Jude if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS) 3414e28f9b7dSAllan Jude return (0); 3415e28f9b7dSAllan Jude else 3416e28f9b7dSAllan Jude return (EPERM); 3417e28f9b7dSAllan Jude 3418e28f9b7dSAllan Jude /* 3419e28f9b7dSAllan Jude * Allow jailed root to reuse in-use ports. 3420e28f9b7dSAllan Jude */ 34214b084056SRobert Watson case PRIV_NETINET_REUSEPORT: 3422800c9408SRobert Watson return (0); 3423800c9408SRobert Watson 3424800c9408SRobert Watson /* 3425e3043798SPedro F. Giffuni * Allow jailed root to set certain IPv4/6 (option) headers. 342679ba3952SBjoern A. Zeeb */ 342779ba3952SBjoern A. Zeeb case PRIV_NETINET_SETHDROPTS: 342879ba3952SBjoern A. Zeeb return (0); 342979ba3952SBjoern A. Zeeb 343079ba3952SBjoern A. Zeeb /* 3431800c9408SRobert Watson * Conditionally allow creating raw sockets in jail. 3432800c9408SRobert Watson */ 3433800c9408SRobert Watson case PRIV_NETINET_RAW: 34340304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS) 3435800c9408SRobert Watson return (0); 3436800c9408SRobert Watson else 3437800c9408SRobert Watson return (EPERM); 3438800c9408SRobert Watson 3439800c9408SRobert Watson /* 3440800c9408SRobert Watson * Since jail implements its own visibility limits on netstat 3441800c9408SRobert Watson * sysctls, allow getcred. This allows identd to work in 3442800c9408SRobert Watson * jail. 3443800c9408SRobert Watson */ 3444800c9408SRobert Watson case PRIV_NETINET_GETCRED: 3445800c9408SRobert Watson return (0); 3446800c9408SRobert Watson 34472bfc50bcSEdward Tomasz Napierala /* 34482bfc50bcSEdward Tomasz Napierala * Allow jailed root to set loginclass. 34492bfc50bcSEdward Tomasz Napierala */ 34502bfc50bcSEdward Tomasz Napierala case PRIV_PROC_SETLOGINCLASS: 34512bfc50bcSEdward Tomasz Napierala return (0); 34522bfc50bcSEdward Tomasz Napierala 3453b19d66fdSJamie Gritton /* 34544520f617SJamie Gritton * Do not allow a process inside a jail to read the kernel 3455b19d66fdSJamie Gritton * message buffer unless explicitly permitted. 3456b19d66fdSJamie Gritton */ 3457b19d66fdSJamie Gritton case PRIV_MSGBUF: 3458b19d66fdSJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_READ_MSGBUF) 3459b19d66fdSJamie Gritton return (0); 3460b19d66fdSJamie Gritton return (EPERM); 3461b19d66fdSJamie Gritton 3462800c9408SRobert Watson default: 3463800c9408SRobert Watson /* 3464800c9408SRobert Watson * In all remaining cases, deny the privilege request. This 3465800c9408SRobert Watson * includes almost all network privileges, many system 3466800c9408SRobert Watson * configuration privileges. 3467800c9408SRobert Watson */ 3468800c9408SRobert Watson return (EPERM); 3469800c9408SRobert Watson } 3470800c9408SRobert Watson } 3471800c9408SRobert Watson 34720304c731SJamie Gritton /* 34730304c731SJamie Gritton * Return the part of pr2's name that is relative to pr1, or the whole name 34740304c731SJamie Gritton * if it does not directly follow. 34750304c731SJamie Gritton */ 34760304c731SJamie Gritton 34770304c731SJamie Gritton char * 34780304c731SJamie Gritton prison_name(struct prison *pr1, struct prison *pr2) 34790304c731SJamie Gritton { 34800304c731SJamie Gritton char *name; 34810304c731SJamie Gritton 34820304c731SJamie Gritton /* Jails see themselves as "0" (if they see themselves at all). */ 34830304c731SJamie Gritton if (pr1 == pr2) 34840304c731SJamie Gritton return "0"; 34850304c731SJamie Gritton name = pr2->pr_name; 34860304c731SJamie Gritton if (prison_ischild(pr1, pr2)) { 34870304c731SJamie Gritton /* 34880304c731SJamie Gritton * pr1 isn't locked (and allprison_lock may not be either) 34890304c731SJamie Gritton * so its length can't be counted on. But the number of dots 34900304c731SJamie Gritton * can be counted on - and counted. 34910304c731SJamie Gritton */ 34920304c731SJamie Gritton for (; pr1 != &prison0; pr1 = pr1->pr_parent) 34930304c731SJamie Gritton name = strchr(name, '.') + 1; 34940304c731SJamie Gritton } 34950304c731SJamie Gritton return (name); 34960304c731SJamie Gritton } 34970304c731SJamie Gritton 34980304c731SJamie Gritton /* 34990304c731SJamie Gritton * Return the part of pr2's path that is relative to pr1, or the whole path 35000304c731SJamie Gritton * if it does not directly follow. 35010304c731SJamie Gritton */ 35020304c731SJamie Gritton static char * 35030304c731SJamie Gritton prison_path(struct prison *pr1, struct prison *pr2) 35040304c731SJamie Gritton { 35050304c731SJamie Gritton char *path1, *path2; 35060304c731SJamie Gritton int len1; 35070304c731SJamie Gritton 35080304c731SJamie Gritton path1 = pr1->pr_path; 35090304c731SJamie Gritton path2 = pr2->pr_path; 35100304c731SJamie Gritton if (!strcmp(path1, "/")) 35110304c731SJamie Gritton return (path2); 35120304c731SJamie Gritton len1 = strlen(path1); 35130304c731SJamie Gritton if (strncmp(path1, path2, len1)) 35140304c731SJamie Gritton return (path2); 35150304c731SJamie Gritton if (path2[len1] == '\0') 35160304c731SJamie Gritton return "/"; 35170304c731SJamie Gritton if (path2[len1] == '/') 35180304c731SJamie Gritton return (path2 + len1); 35190304c731SJamie Gritton return (path2); 35200304c731SJamie Gritton } 35210304c731SJamie Gritton 35220304c731SJamie Gritton /* 35230304c731SJamie Gritton * Jail-related sysctls. 35240304c731SJamie Gritton */ 35257029da5cSPawel Biernacki static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 35260304c731SJamie Gritton "Jails"); 35270304c731SJamie Gritton 3528fd7a8150SMike Barcroft static int 3529fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS) 3530fd7a8150SMike Barcroft { 3531b38ff370SJamie Gritton struct xprison *xp; 35320304c731SJamie Gritton struct prison *pr, *cpr; 3533b38ff370SJamie Gritton #ifdef INET 3534b38ff370SJamie Gritton struct in_addr *ip4 = NULL; 3535b38ff370SJamie Gritton int ip4s = 0; 3536b38ff370SJamie Gritton #endif 3537b38ff370SJamie Gritton #ifdef INET6 35383beefaedSColin Percival struct in6_addr *ip6 = NULL; 3539b38ff370SJamie Gritton int ip6s = 0; 3540b38ff370SJamie Gritton #endif 35410304c731SJamie Gritton int descend, error; 3542fd7a8150SMike Barcroft 3543b38ff370SJamie Gritton xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK); 35440304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 3545b38ff370SJamie Gritton error = 0; 3546dc68a633SPawel Jakub Dawidek sx_slock(&allprison_lock); 35470304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 35480304c731SJamie Gritton #if defined(INET) || defined(INET6) 3549b38ff370SJamie Gritton again: 35500304c731SJamie Gritton #endif 35510304c731SJamie Gritton mtx_lock(&cpr->pr_mtx); 3552413628a7SBjoern A. Zeeb #ifdef INET 35530304c731SJamie Gritton if (cpr->pr_ip4s > 0) { 35540304c731SJamie Gritton if (ip4s < cpr->pr_ip4s) { 35550304c731SJamie Gritton ip4s = cpr->pr_ip4s; 35560304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 3557b38ff370SJamie Gritton ip4 = realloc(ip4, ip4s * 3558b38ff370SJamie Gritton sizeof(struct in_addr), M_TEMP, M_WAITOK); 3559b38ff370SJamie Gritton goto again; 3560b38ff370SJamie Gritton } 35610304c731SJamie Gritton bcopy(cpr->pr_ip4, ip4, 35620304c731SJamie Gritton cpr->pr_ip4s * sizeof(struct in_addr)); 3563b38ff370SJamie Gritton } 3564413628a7SBjoern A. Zeeb #endif 3565413628a7SBjoern A. Zeeb #ifdef INET6 35660304c731SJamie Gritton if (cpr->pr_ip6s > 0) { 35670304c731SJamie Gritton if (ip6s < cpr->pr_ip6s) { 35680304c731SJamie Gritton ip6s = cpr->pr_ip6s; 35690304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 3570b38ff370SJamie Gritton ip6 = realloc(ip6, ip6s * 3571b38ff370SJamie Gritton sizeof(struct in6_addr), M_TEMP, M_WAITOK); 3572b38ff370SJamie Gritton goto again; 3573413628a7SBjoern A. Zeeb } 35740304c731SJamie Gritton bcopy(cpr->pr_ip6, ip6, 35750304c731SJamie Gritton cpr->pr_ip6s * sizeof(struct in6_addr)); 3576b38ff370SJamie Gritton } 3577b38ff370SJamie Gritton #endif 35780304c731SJamie Gritton if (cpr->pr_ref == 0) { 35790304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 3580b38ff370SJamie Gritton continue; 3581b38ff370SJamie Gritton } 3582b38ff370SJamie Gritton bzero(xp, sizeof(*xp)); 3583fd7a8150SMike Barcroft xp->pr_version = XPRISON_VERSION; 35840304c731SJamie Gritton xp->pr_id = cpr->pr_id; 35850304c731SJamie Gritton xp->pr_state = cpr->pr_uref > 0 3586b38ff370SJamie Gritton ? PRISON_STATE_ALIVE : PRISON_STATE_DYING; 35870304c731SJamie Gritton strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path)); 3588c1f19219SJamie Gritton strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host)); 35890304c731SJamie Gritton strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name)); 3590413628a7SBjoern A. Zeeb #ifdef INET 35910304c731SJamie Gritton xp->pr_ip4s = cpr->pr_ip4s; 3592413628a7SBjoern A. Zeeb #endif 3593413628a7SBjoern A. Zeeb #ifdef INET6 35940304c731SJamie Gritton xp->pr_ip6s = cpr->pr_ip6s; 3595413628a7SBjoern A. Zeeb #endif 35960304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 3597b38ff370SJamie Gritton error = SYSCTL_OUT(req, xp, sizeof(*xp)); 3598b38ff370SJamie Gritton if (error) 3599b38ff370SJamie Gritton break; 3600413628a7SBjoern A. Zeeb #ifdef INET 3601b38ff370SJamie Gritton if (xp->pr_ip4s > 0) { 3602b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip4, 3603b38ff370SJamie Gritton xp->pr_ip4s * sizeof(struct in_addr)); 3604b38ff370SJamie Gritton if (error) 3605b38ff370SJamie Gritton break; 3606413628a7SBjoern A. Zeeb } 3607413628a7SBjoern A. Zeeb #endif 3608413628a7SBjoern A. Zeeb #ifdef INET6 3609b38ff370SJamie Gritton if (xp->pr_ip6s > 0) { 3610b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip6, 3611b38ff370SJamie Gritton xp->pr_ip6s * sizeof(struct in6_addr)); 3612b38ff370SJamie Gritton if (error) 3613b38ff370SJamie Gritton break; 3614413628a7SBjoern A. Zeeb } 3615413628a7SBjoern A. Zeeb #endif 3616fd7a8150SMike Barcroft } 3617dc68a633SPawel Jakub Dawidek sx_sunlock(&allprison_lock); 3618b38ff370SJamie Gritton free(xp, M_TEMP); 3619b38ff370SJamie Gritton #ifdef INET 3620b38ff370SJamie Gritton free(ip4, M_TEMP); 3621b38ff370SJamie Gritton #endif 3622b38ff370SJamie Gritton #ifdef INET6 3623b38ff370SJamie Gritton free(ip6, M_TEMP); 3624b38ff370SJamie Gritton #endif 3625fd7a8150SMike Barcroft return (error); 3626fd7a8150SMike Barcroft } 3627fd7a8150SMike Barcroft 3628f3b86a5fSEd Schouten SYSCTL_OID(_security_jail, OID_AUTO, list, 3629f3b86a5fSEd Schouten CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 3630f3b86a5fSEd Schouten sysctl_jail_list, "S", "List of active jails"); 3631461167c2SPawel Jakub Dawidek 3632461167c2SPawel Jakub Dawidek static int 3633461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS) 3634461167c2SPawel Jakub Dawidek { 3635461167c2SPawel Jakub Dawidek int error, injail; 3636461167c2SPawel Jakub Dawidek 3637461167c2SPawel Jakub Dawidek injail = jailed(req->td->td_ucred); 3638461167c2SPawel Jakub Dawidek error = SYSCTL_OUT(req, &injail, sizeof(injail)); 3639461167c2SPawel Jakub Dawidek 3640461167c2SPawel Jakub Dawidek return (error); 3641461167c2SPawel Jakub Dawidek } 36420304c731SJamie Gritton 3643f3b86a5fSEd Schouten SYSCTL_PROC(_security_jail, OID_AUTO, jailed, 3644f3b86a5fSEd Schouten CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 3645f3b86a5fSEd Schouten sysctl_jail_jailed, "I", "Process in jail?"); 3646413628a7SBjoern A. Zeeb 3647761d2bb5SJamie Gritton static int 3648761d2bb5SJamie Gritton sysctl_jail_vnet(SYSCTL_HANDLER_ARGS) 3649761d2bb5SJamie Gritton { 3650761d2bb5SJamie Gritton int error, havevnet; 3651761d2bb5SJamie Gritton #ifdef VIMAGE 3652761d2bb5SJamie Gritton struct ucred *cred = req->td->td_ucred; 3653761d2bb5SJamie Gritton 3654761d2bb5SJamie Gritton havevnet = jailed(cred) && prison_owns_vnet(cred); 3655761d2bb5SJamie Gritton #else 3656761d2bb5SJamie Gritton havevnet = 0; 3657761d2bb5SJamie Gritton #endif 3658761d2bb5SJamie Gritton error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet)); 3659761d2bb5SJamie Gritton 3660761d2bb5SJamie Gritton return (error); 3661761d2bb5SJamie Gritton } 3662761d2bb5SJamie Gritton 3663761d2bb5SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, vnet, 3664761d2bb5SJamie Gritton CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 3665bb8f1623SBjoern A. Zeeb sysctl_jail_vnet, "I", "Jail owns vnet?"); 3666761d2bb5SJamie Gritton 36670304c731SJamie Gritton #if defined(INET) || defined(INET6) 3668e92e0574SJamie Gritton SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW, 36690304c731SJamie Gritton &jail_max_af_ips, 0, 3670c542c43eSJamie Gritton "Number of IP addresses a jail may have at most per address family (deprecated)"); 36710304c731SJamie Gritton #endif 36720304c731SJamie Gritton 36730304c731SJamie Gritton /* 3674c542c43eSJamie Gritton * Default parameters for jail(2) compatibility. For historical reasons, 3675c542c43eSJamie Gritton * the sysctl names have varying similarity to the parameter names. Prisons 3676c542c43eSJamie Gritton * just see their own parameters, and can't change them. 36770304c731SJamie Gritton */ 36780304c731SJamie Gritton static int 36790304c731SJamie Gritton sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS) 36800304c731SJamie Gritton { 36810304c731SJamie Gritton struct prison *pr; 36820304c731SJamie Gritton int allow, error, i; 36830304c731SJamie Gritton 36840304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 36850304c731SJamie Gritton allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow; 36860304c731SJamie Gritton 36870304c731SJamie Gritton /* Get the current flag value, and convert it to a boolean. */ 36880304c731SJamie Gritton i = (allow & arg2) ? 1 : 0; 36890304c731SJamie Gritton if (arg1 != NULL) 36900304c731SJamie Gritton i = !i; 36910304c731SJamie Gritton error = sysctl_handle_int(oidp, &i, 0, req); 3692c542c43eSJamie Gritton if (error || !req->newptr) 36930304c731SJamie Gritton return (error); 36940304c731SJamie Gritton i = i ? arg2 : 0; 36950304c731SJamie Gritton if (arg1 != NULL) 36960304c731SJamie Gritton i ^= arg2; 36970304c731SJamie Gritton /* 36980304c731SJamie Gritton * The sysctls don't have CTLFLAGS_PRISON, so assume prison0 36990304c731SJamie Gritton * for writing. 37000304c731SJamie Gritton */ 37010304c731SJamie Gritton mtx_lock(&prison0.pr_mtx); 37020304c731SJamie Gritton jail_default_allow = (jail_default_allow & ~arg2) | i; 37030304c731SJamie Gritton mtx_unlock(&prison0.pr_mtx); 37040304c731SJamie Gritton return (0); 37050304c731SJamie Gritton } 37060304c731SJamie Gritton 37070304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed, 3708c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 37090304c731SJamie Gritton NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I", 3710c542c43eSJamie Gritton "Processes in jail can set their hostnames (deprecated)"); 37110304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only, 3712c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 37130304c731SJamie Gritton (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I", 3714c542c43eSJamie Gritton "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)"); 37150304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed, 3716c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 37170304c731SJamie Gritton NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I", 3718c542c43eSJamie Gritton "Processes in jail can use System V IPC primitives (deprecated)"); 37190304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets, 3720c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 37210304c731SJamie Gritton NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I", 3722c542c43eSJamie Gritton "Prison root can create raw sockets (deprecated)"); 37230304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed, 3724c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 37250304c731SJamie Gritton NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I", 3726c542c43eSJamie Gritton "Processes in jail can alter system file flags (deprecated)"); 37270304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed, 3728c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 37290304c731SJamie Gritton NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I", 3730c542c43eSJamie Gritton "Processes in jail can mount/unmount jail-friendly file systems (deprecated)"); 37310304c731SJamie Gritton 37320304c731SJamie Gritton static int 37330304c731SJamie Gritton sysctl_jail_default_level(SYSCTL_HANDLER_ARGS) 37340304c731SJamie Gritton { 37350304c731SJamie Gritton struct prison *pr; 37360304c731SJamie Gritton int level, error; 37370304c731SJamie Gritton 37380304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 37390304c731SJamie Gritton level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2); 37400304c731SJamie Gritton error = sysctl_handle_int(oidp, &level, 0, req); 3741c542c43eSJamie Gritton if (error || !req->newptr) 37420304c731SJamie Gritton return (error); 37430304c731SJamie Gritton *(int *)arg1 = level; 37440304c731SJamie Gritton return (0); 37450304c731SJamie Gritton } 37460304c731SJamie Gritton 37470304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs, 3748c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 3749c542c43eSJamie Gritton &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs), 37500304c731SJamie Gritton sysctl_jail_default_level, "I", 3751c542c43eSJamie Gritton "Processes in jail cannot see all mounted file systems (deprecated)"); 3752c542c43eSJamie Gritton 37530cc207a6SMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset, 3754c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 3755c542c43eSJamie Gritton &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum), 37560cc207a6SMartin Matuska sysctl_jail_default_level, "I", 3757c542c43eSJamie Gritton "Ruleset for the devfs filesystem in jail (deprecated)"); 37580cc207a6SMartin Matuska 37590304c731SJamie Gritton /* 37600304c731SJamie Gritton * Nodes to describe jail parameters. Maximum length of string parameters 37610304c731SJamie Gritton * is returned in the string itself, and the other parameters exist merely 37620304c731SJamie Gritton * to make themselves and their types known. 37630304c731SJamie Gritton */ 37647029da5cSPawel Biernacki SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 37650304c731SJamie Gritton "Jail parameters"); 37660304c731SJamie Gritton 37670304c731SJamie Gritton int 37680304c731SJamie Gritton sysctl_jail_param(SYSCTL_HANDLER_ARGS) 37690304c731SJamie Gritton { 37700304c731SJamie Gritton int i; 37710304c731SJamie Gritton long l; 37720304c731SJamie Gritton size_t s; 37730304c731SJamie Gritton char numbuf[12]; 37740304c731SJamie Gritton 37750304c731SJamie Gritton switch (oidp->oid_kind & CTLTYPE) 37760304c731SJamie Gritton { 37770304c731SJamie Gritton case CTLTYPE_LONG: 37780304c731SJamie Gritton case CTLTYPE_ULONG: 37790304c731SJamie Gritton l = 0; 37800304c731SJamie Gritton #ifdef SCTL_MASK32 37810304c731SJamie Gritton if (!(req->flags & SCTL_MASK32)) 37820304c731SJamie Gritton #endif 37830304c731SJamie Gritton return (SYSCTL_OUT(req, &l, sizeof(l))); 37840304c731SJamie Gritton case CTLTYPE_INT: 37850304c731SJamie Gritton case CTLTYPE_UINT: 37860304c731SJamie Gritton i = 0; 37870304c731SJamie Gritton return (SYSCTL_OUT(req, &i, sizeof(i))); 37880304c731SJamie Gritton case CTLTYPE_STRING: 3789e4cd31ddSJeff Roberson snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2); 37900304c731SJamie Gritton return 37910304c731SJamie Gritton (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req)); 37920304c731SJamie Gritton case CTLTYPE_STRUCT: 37930304c731SJamie Gritton s = (size_t)arg2; 37940304c731SJamie Gritton return (SYSCTL_OUT(req, &s, sizeof(s))); 37950304c731SJamie Gritton } 37960304c731SJamie Gritton return (0); 37970304c731SJamie Gritton } 37980304c731SJamie Gritton 3799b96bd95bSIan Lepore /* 3800b96bd95bSIan Lepore * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at 3801b96bd95bSIan Lepore * jail creation time but cannot be changed in an existing jail. 3802b96bd95bSIan Lepore */ 38030304c731SJamie Gritton SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID"); 38040304c731SJamie Gritton SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID"); 38050304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name"); 38060304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path"); 38070304c731SJamie Gritton SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW, 38080304c731SJamie Gritton "I", "Jail secure level"); 3809b96bd95bSIan Lepore SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I", 3810b96bd95bSIan Lepore "Jail value for kern.osreldate and uname -K"); 3811b96bd95bSIan Lepore SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN, 3812b96bd95bSIan Lepore "Jail value for kern.osrelease and uname -r"); 38130304c731SJamie Gritton SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW, 38140304c731SJamie Gritton "I", "Jail cannot see all mounted file systems"); 38150cc207a6SMartin Matuska SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW, 38160cc207a6SMartin Matuska "I", "Ruleset for in-jail devfs mounts"); 38170304c731SJamie Gritton SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW, 38180304c731SJamie Gritton "B", "Jail persistence"); 3819679e1390SJamie Gritton #ifdef VIMAGE 3820679e1390SJamie Gritton SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN, 38217cbf7213SJamie Gritton "E,jailsys", "Virtual network stack"); 3822679e1390SJamie Gritton #endif 38230304c731SJamie Gritton SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD, 38240304c731SJamie Gritton "B", "Jail is in the process of shutting down"); 38250304c731SJamie Gritton 3826b97457e2SJamie Gritton SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails"); 3827b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD, 3828b97457e2SJamie Gritton "I", "Current number of child jails"); 3829b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW, 3830b97457e2SJamie Gritton "I", "Maximum number of child jails"); 3831b97457e2SJamie Gritton 38327cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info"); 38330304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN, 38340304c731SJamie Gritton "Jail hostname"); 383576ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN, 383676ca6f88SJamie Gritton "Jail NIS domainname"); 383776ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN, 383876ca6f88SJamie Gritton "Jail host UUID"); 383976ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW, 384076ca6f88SJamie Gritton "LU", "Jail host ID"); 38410304c731SJamie Gritton 38420304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset"); 38430304c731SJamie Gritton SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID"); 38440304c731SJamie Gritton 38450304c731SJamie Gritton #ifdef INET 38462b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN, 38472b0d6f81SJamie Gritton "Jail IPv4 address virtualization"); 38480304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr), 38490304c731SJamie Gritton "S,in_addr,a", "Jail IPv4 addresses"); 3850592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 3851592bcae8SBjoern A. Zeeb "B", "Do (not) use IPv4 source address selection rather than the " 3852592bcae8SBjoern A. Zeeb "primary jail IPv4 address."); 38530304c731SJamie Gritton #endif 38540304c731SJamie Gritton #ifdef INET6 38552b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN, 38562b0d6f81SJamie Gritton "Jail IPv6 address virtualization"); 38570304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr), 38580304c731SJamie Gritton "S,in6_addr,a", "Jail IPv6 addresses"); 3859592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 3860592bcae8SBjoern A. Zeeb "B", "Do (not) use IPv6 source address selection rather than the " 3861592bcae8SBjoern A. Zeeb "primary jail IPv6 address."); 38620304c731SJamie Gritton #endif 38630304c731SJamie Gritton 38640304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags"); 38650304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW, 38660304c731SJamie Gritton "B", "Jail may set hostname"); 38670304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW, 38680304c731SJamie Gritton "B", "Jail may use SYSV IPC"); 38690304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW, 38700304c731SJamie Gritton "B", "Jail may create raw sockets"); 38710304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW, 38720304c731SJamie Gritton "B", "Jail may alter system file flags"); 38730304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW, 38740304c731SJamie Gritton "B", "Jail may set file quotas"); 38750304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW, 38760304c731SJamie Gritton "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route"); 3877ccd6ac9fSAntoine Brodin SYSCTL_JAIL_PARAM(_allow, mlock, CTLTYPE_INT | CTLFLAG_RW, 3878ccd6ac9fSAntoine Brodin "B", "Jail may lock (unlock) physical pages in memory"); 3879e28f9b7dSAllan Jude SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW, 3880e28f9b7dSAllan Jude "B", "Jail may bind sockets to reserved ports"); 3881b19d66fdSJamie Gritton SYSCTL_JAIL_PARAM(_allow, read_msgbuf, CTLTYPE_INT | CTLFLAG_RW, 3882b19d66fdSJamie Gritton "B", "Jail may read the kernel message buffer"); 3883b3079544SJamie Gritton SYSCTL_JAIL_PARAM(_allow, unprivileged_proc_debug, CTLTYPE_INT | CTLFLAG_RW, 3884b3079544SJamie Gritton "B", "Unprivileged processes may use process debugging facilities"); 388505e1e482SMariusz Zaborski SYSCTL_JAIL_PARAM(_allow, suser, CTLTYPE_INT | CTLFLAG_RW, 388605e1e482SMariusz Zaborski "B", "Processes in jail with uid 0 have privilege"); 38870304c731SJamie Gritton 3888bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags"); 3889bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW, 3890bf3db8aaSMartin Matuska "B", "Jail may mount/unmount jail-friendly file systems in general"); 38910e5c6bd4SJamie Gritton 38920e5c6bd4SJamie Gritton /* 38930a172404SJamie Gritton * Add a dynamic parameter allow.<name>, or allow.<prefix>.<name>. Return 38940a172404SJamie Gritton * its associated bit in the pr_allow bitmask, or zero if the parameter was 38950a172404SJamie Gritton * not created. 38960e5c6bd4SJamie Gritton */ 38970a172404SJamie Gritton unsigned 38980a172404SJamie Gritton prison_add_allow(const char *prefix, const char *name, const char *prefix_descr, 38990a172404SJamie Gritton const char *descr) 39000e5c6bd4SJamie Gritton { 39010e5c6bd4SJamie Gritton struct bool_flags *bf; 39020a172404SJamie Gritton struct sysctl_oid *parent; 39030a172404SJamie Gritton char *allow_name, *allow_noname, *allowed; 3904c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR 3905c542c43eSJamie Gritton char *descr_deprecated; 3906c542c43eSJamie Gritton #endif 39070e5c6bd4SJamie Gritton unsigned allow_flag; 39080e5c6bd4SJamie Gritton 39090a172404SJamie Gritton if (prefix 39100a172404SJamie Gritton ? asprintf(&allow_name, M_PRISON, "allow.%s.%s", prefix, name) 39110a172404SJamie Gritton < 0 || 39120a172404SJamie Gritton asprintf(&allow_noname, M_PRISON, "allow.%s.no%s", prefix, name) 39130a172404SJamie Gritton < 0 39140a172404SJamie Gritton : asprintf(&allow_name, M_PRISON, "allow.%s", name) < 0 || 39150a172404SJamie Gritton asprintf(&allow_noname, M_PRISON, "allow.no%s", name) < 0) { 39160e5c6bd4SJamie Gritton free(allow_name, M_PRISON); 39170a172404SJamie Gritton return 0; 39180e5c6bd4SJamie Gritton } 39190e5c6bd4SJamie Gritton 39200e5c6bd4SJamie Gritton /* 39210a172404SJamie Gritton * See if this parameter has already beed added, i.e. a module was 39220a172404SJamie Gritton * previously loaded/unloaded. 39230e5c6bd4SJamie Gritton */ 39240e5c6bd4SJamie Gritton mtx_lock(&prison0.pr_mtx); 39250e5c6bd4SJamie Gritton for (bf = pr_flag_allow; 39260e5c6bd4SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && bf->flag != 0; 39270e5c6bd4SJamie Gritton bf++) { 39280e5c6bd4SJamie Gritton if (strcmp(bf->name, allow_name) == 0) { 39290a172404SJamie Gritton allow_flag = bf->flag; 39300e5c6bd4SJamie Gritton goto no_add; 39310e5c6bd4SJamie Gritton } 39320e5c6bd4SJamie Gritton } 39330e5c6bd4SJamie Gritton 39340e5c6bd4SJamie Gritton /* 39350e5c6bd4SJamie Gritton * Find a free bit in prison0's pr_allow, failing if there are none 39360e5c6bd4SJamie Gritton * (which shouldn't happen as long as we keep track of how many 39370a172404SJamie Gritton * potential dynamic flags exist). 3938b3079544SJamie Gritton * 3939b3079544SJamie Gritton * Due to per-jail unprivileged process debugging support 3940b3079544SJamie Gritton * using pr_allow, also verify against PR_ALLOW_ALL_STATIC. 3941b3079544SJamie Gritton * prison0 may have unprivileged process debugging unset. 39420e5c6bd4SJamie Gritton */ 39430e5c6bd4SJamie Gritton for (allow_flag = 1;; allow_flag <<= 1) { 39440e5c6bd4SJamie Gritton if (allow_flag == 0) 39450e5c6bd4SJamie Gritton goto no_add; 3946b3079544SJamie Gritton if (allow_flag & PR_ALLOW_ALL_STATIC) 3947b3079544SJamie Gritton continue; 39480e5c6bd4SJamie Gritton if ((prison0.pr_allow & allow_flag) == 0) 39490e5c6bd4SJamie Gritton break; 39500e5c6bd4SJamie Gritton } 39510e5c6bd4SJamie Gritton 39520e5c6bd4SJamie Gritton /* 39530e5c6bd4SJamie Gritton * Note the parameter in the next open slot in pr_flag_allow. 39540e5c6bd4SJamie Gritton * Set the flag last so code that checks pr_flag_allow can do so 39550e5c6bd4SJamie Gritton * without locking. 39560e5c6bd4SJamie Gritton */ 39570e5c6bd4SJamie Gritton for (bf = pr_flag_allow; bf->flag != 0; bf++) 39580e5c6bd4SJamie Gritton if (bf == pr_flag_allow + nitems(pr_flag_allow)) { 39590e5c6bd4SJamie Gritton /* This should never happen, but is not fatal. */ 39600a172404SJamie Gritton allow_flag = 0; 39610e5c6bd4SJamie Gritton goto no_add; 39620e5c6bd4SJamie Gritton } 39630e5c6bd4SJamie Gritton prison0.pr_allow |= allow_flag; 39640e5c6bd4SJamie Gritton bf->name = allow_name; 39650e5c6bd4SJamie Gritton bf->noname = allow_noname; 39660e5c6bd4SJamie Gritton bf->flag = allow_flag; 39670e5c6bd4SJamie Gritton mtx_unlock(&prison0.pr_mtx); 39680e5c6bd4SJamie Gritton 3969c542c43eSJamie Gritton /* 3970c542c43eSJamie Gritton * Create sysctls for the paramter, and the back-compat global 3971c542c43eSJamie Gritton * permission. 3972c542c43eSJamie Gritton */ 39730a172404SJamie Gritton parent = prefix 39740a172404SJamie Gritton ? SYSCTL_ADD_NODE(NULL, 39750a172404SJamie Gritton SYSCTL_CHILDREN(&sysctl___security_jail_param_allow), 39767029da5cSPawel Biernacki OID_AUTO, prefix, CTLFLAG_MPSAFE, 0, prefix_descr) 39770a172404SJamie Gritton : &sysctl___security_jail_param_allow; 39780a172404SJamie Gritton (void)SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(parent), OID_AUTO, 39790a172404SJamie Gritton name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 39800e5c6bd4SJamie Gritton NULL, 0, sysctl_jail_param, "B", descr); 39810a172404SJamie Gritton if ((prefix 39820a172404SJamie Gritton ? asprintf(&allowed, M_TEMP, "%s_%s_allowed", prefix, name) 39830a172404SJamie Gritton : asprintf(&allowed, M_TEMP, "%s_allowed", name)) >= 0) { 3984c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR 3985c542c43eSJamie Gritton (void)asprintf(&descr_deprecated, M_TEMP, "%s (deprecated)", 3986c542c43eSJamie Gritton descr); 3987c542c43eSJamie Gritton #endif 39880e5c6bd4SJamie Gritton (void)SYSCTL_ADD_PROC(NULL, 39890a172404SJamie Gritton SYSCTL_CHILDREN(&sysctl___security_jail), OID_AUTO, allowed, 3990c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, allow_flag, 3991c542c43eSJamie Gritton sysctl_jail_default_allow, "I", descr_deprecated); 3992c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR 3993c542c43eSJamie Gritton free(descr_deprecated, M_TEMP); 3994c542c43eSJamie Gritton #endif 39950a172404SJamie Gritton free(allowed, M_TEMP); 39960e5c6bd4SJamie Gritton } 39970a172404SJamie Gritton return allow_flag; 39980e5c6bd4SJamie Gritton 39990e5c6bd4SJamie Gritton no_add: 40000e5c6bd4SJamie Gritton mtx_unlock(&prison0.pr_mtx); 40010e5c6bd4SJamie Gritton free(allow_name, M_PRISON); 40020e5c6bd4SJamie Gritton free(allow_noname, M_PRISON); 40030a172404SJamie Gritton return allow_flag; 40040a172404SJamie Gritton } 40050a172404SJamie Gritton 40060a172404SJamie Gritton /* 40070a172404SJamie Gritton * The VFS system will register jail-aware filesystems here. They each get 40080a172404SJamie Gritton * a parameter allow.mount.xxxfs and a flag to check when a jailed user 40090a172404SJamie Gritton * attempts to mount. 40100a172404SJamie Gritton */ 40110a172404SJamie Gritton void 40120a172404SJamie Gritton prison_add_vfs(struct vfsconf *vfsp) 40130a172404SJamie Gritton { 40140a172404SJamie Gritton #ifdef NO_SYSCTL_DESCR 40150a172404SJamie Gritton 40160a172404SJamie Gritton vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name, 40170a172404SJamie Gritton NULL, NULL); 40180a172404SJamie Gritton #else 40190a172404SJamie Gritton char *descr; 40200a172404SJamie Gritton 40210a172404SJamie Gritton (void)asprintf(&descr, M_TEMP, "Jail may mount the %s file system", 40220a172404SJamie Gritton vfsp->vfc_name); 40230a172404SJamie Gritton vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name, 40240a172404SJamie Gritton NULL, descr); 40250a172404SJamie Gritton free(descr, M_TEMP); 40260a172404SJamie Gritton #endif 40270e5c6bd4SJamie Gritton } 4028bf3db8aaSMartin Matuska 40294b5c9cf6SEdward Tomasz Napierala #ifdef RACCT 4030097055e2SEdward Tomasz Napierala void 4031097055e2SEdward Tomasz Napierala prison_racct_foreach(void (*callback)(struct racct *racct, 403215db3c07SEdward Tomasz Napierala void *arg2, void *arg3), void (*pre)(void), void (*post)(void), 403315db3c07SEdward Tomasz Napierala void *arg2, void *arg3) 4034097055e2SEdward Tomasz Napierala { 4035a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4036097055e2SEdward Tomasz Napierala 40374b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 40384b5c9cf6SEdward Tomasz Napierala 4039097055e2SEdward Tomasz Napierala sx_slock(&allprison_lock); 404015db3c07SEdward Tomasz Napierala if (pre != NULL) 404115db3c07SEdward Tomasz Napierala (pre)(); 4042a7ad07bfSEdward Tomasz Napierala LIST_FOREACH(prr, &allprison_racct, prr_next) 4043a7ad07bfSEdward Tomasz Napierala (callback)(prr->prr_racct, arg2, arg3); 404415db3c07SEdward Tomasz Napierala if (post != NULL) 404515db3c07SEdward Tomasz Napierala (post)(); 4046097055e2SEdward Tomasz Napierala sx_sunlock(&allprison_lock); 4047097055e2SEdward Tomasz Napierala } 40480304c731SJamie Gritton 4049a7ad07bfSEdward Tomasz Napierala static struct prison_racct * 4050a7ad07bfSEdward Tomasz Napierala prison_racct_find_locked(const char *name) 4051a7ad07bfSEdward Tomasz Napierala { 4052a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4053a7ad07bfSEdward Tomasz Napierala 40544b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4055a7ad07bfSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4056a7ad07bfSEdward Tomasz Napierala 4057a7ad07bfSEdward Tomasz Napierala if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN) 4058a7ad07bfSEdward Tomasz Napierala return (NULL); 4059a7ad07bfSEdward Tomasz Napierala 4060a7ad07bfSEdward Tomasz Napierala LIST_FOREACH(prr, &allprison_racct, prr_next) { 4061a7ad07bfSEdward Tomasz Napierala if (strcmp(name, prr->prr_name) != 0) 4062a7ad07bfSEdward Tomasz Napierala continue; 4063a7ad07bfSEdward Tomasz Napierala 4064a7ad07bfSEdward Tomasz Napierala /* Found prison_racct with a matching name? */ 4065a7ad07bfSEdward Tomasz Napierala prison_racct_hold(prr); 4066a7ad07bfSEdward Tomasz Napierala return (prr); 4067a7ad07bfSEdward Tomasz Napierala } 4068a7ad07bfSEdward Tomasz Napierala 4069a7ad07bfSEdward Tomasz Napierala /* Add new prison_racct. */ 4070a7ad07bfSEdward Tomasz Napierala prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK); 4071a7ad07bfSEdward Tomasz Napierala racct_create(&prr->prr_racct); 4072a7ad07bfSEdward Tomasz Napierala 4073a7ad07bfSEdward Tomasz Napierala strcpy(prr->prr_name, name); 4074a7ad07bfSEdward Tomasz Napierala refcount_init(&prr->prr_refcount, 1); 4075a7ad07bfSEdward Tomasz Napierala LIST_INSERT_HEAD(&allprison_racct, prr, prr_next); 4076a7ad07bfSEdward Tomasz Napierala 4077a7ad07bfSEdward Tomasz Napierala return (prr); 4078a7ad07bfSEdward Tomasz Napierala } 4079a7ad07bfSEdward Tomasz Napierala 4080a7ad07bfSEdward Tomasz Napierala struct prison_racct * 4081a7ad07bfSEdward Tomasz Napierala prison_racct_find(const char *name) 4082a7ad07bfSEdward Tomasz Napierala { 4083a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4084a7ad07bfSEdward Tomasz Napierala 40854b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 40864b5c9cf6SEdward Tomasz Napierala 4087a7ad07bfSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4088a7ad07bfSEdward Tomasz Napierala prr = prison_racct_find_locked(name); 4089a7ad07bfSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4090a7ad07bfSEdward Tomasz Napierala return (prr); 4091a7ad07bfSEdward Tomasz Napierala } 4092a7ad07bfSEdward Tomasz Napierala 4093a7ad07bfSEdward Tomasz Napierala void 4094a7ad07bfSEdward Tomasz Napierala prison_racct_hold(struct prison_racct *prr) 4095a7ad07bfSEdward Tomasz Napierala { 4096a7ad07bfSEdward Tomasz Napierala 40974b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 40984b5c9cf6SEdward Tomasz Napierala 4099a7ad07bfSEdward Tomasz Napierala refcount_acquire(&prr->prr_refcount); 4100a7ad07bfSEdward Tomasz Napierala } 4101a7ad07bfSEdward Tomasz Napierala 4102c34bbd2aSEdward Tomasz Napierala static void 4103c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(struct prison_racct *prr) 4104c34bbd2aSEdward Tomasz Napierala { 4105c34bbd2aSEdward Tomasz Napierala 41064b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4107c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4108c34bbd2aSEdward Tomasz Napierala 4109c34bbd2aSEdward Tomasz Napierala if (refcount_release(&prr->prr_refcount)) { 4110c34bbd2aSEdward Tomasz Napierala racct_destroy(&prr->prr_racct); 4111c34bbd2aSEdward Tomasz Napierala LIST_REMOVE(prr, prr_next); 4112c34bbd2aSEdward Tomasz Napierala free(prr, M_PRISON_RACCT); 4113c34bbd2aSEdward Tomasz Napierala } 4114c34bbd2aSEdward Tomasz Napierala } 4115c34bbd2aSEdward Tomasz Napierala 4116a7ad07bfSEdward Tomasz Napierala void 4117a7ad07bfSEdward Tomasz Napierala prison_racct_free(struct prison_racct *prr) 4118a7ad07bfSEdward Tomasz Napierala { 4119a7ad07bfSEdward Tomasz Napierala 41204b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4121c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_UNLOCKED); 4122c34bbd2aSEdward Tomasz Napierala 41236ff4688bSMateusz Guzik if (refcount_release_if_not_last(&prr->prr_refcount)) 4124a7ad07bfSEdward Tomasz Napierala return; 4125a7ad07bfSEdward Tomasz Napierala 4126a7ad07bfSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4127c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(prr); 4128a7ad07bfSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4129a7ad07bfSEdward Tomasz Napierala } 4130a7ad07bfSEdward Tomasz Napierala 4131a7ad07bfSEdward Tomasz Napierala static void 4132a7ad07bfSEdward Tomasz Napierala prison_racct_attach(struct prison *pr) 4133a7ad07bfSEdward Tomasz Napierala { 4134a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4135a7ad07bfSEdward Tomasz Napierala 41364b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4137c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4138c34bbd2aSEdward Tomasz Napierala 4139a7ad07bfSEdward Tomasz Napierala prr = prison_racct_find_locked(pr->pr_name); 4140a7ad07bfSEdward Tomasz Napierala KASSERT(prr != NULL, ("cannot find prison_racct")); 4141a7ad07bfSEdward Tomasz Napierala 4142a7ad07bfSEdward Tomasz Napierala pr->pr_prison_racct = prr; 4143a7ad07bfSEdward Tomasz Napierala } 4144a7ad07bfSEdward Tomasz Napierala 4145c34bbd2aSEdward Tomasz Napierala /* 4146c34bbd2aSEdward Tomasz Napierala * Handle jail renaming. From the racct point of view, renaming means 4147c34bbd2aSEdward Tomasz Napierala * moving from one prison_racct to another. 4148c34bbd2aSEdward Tomasz Napierala */ 4149c34bbd2aSEdward Tomasz Napierala static void 4150c34bbd2aSEdward Tomasz Napierala prison_racct_modify(struct prison *pr) 4151c34bbd2aSEdward Tomasz Napierala { 4152dbadb015SKonstantin Belousov #ifdef RCTL 4153c34bbd2aSEdward Tomasz Napierala struct proc *p; 4154c34bbd2aSEdward Tomasz Napierala struct ucred *cred; 4155dbadb015SKonstantin Belousov #endif 4156c34bbd2aSEdward Tomasz Napierala struct prison_racct *oldprr; 4157c34bbd2aSEdward Tomasz Napierala 41584b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 41594b5c9cf6SEdward Tomasz Napierala 4160c34bbd2aSEdward Tomasz Napierala sx_slock(&allproc_lock); 4161c34bbd2aSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4162c34bbd2aSEdward Tomasz Napierala 4163e30345e7SEdward Tomasz Napierala if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) { 4164e30345e7SEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4165e30345e7SEdward Tomasz Napierala sx_sunlock(&allproc_lock); 4166c34bbd2aSEdward Tomasz Napierala return; 4167e30345e7SEdward Tomasz Napierala } 4168c34bbd2aSEdward Tomasz Napierala 4169c34bbd2aSEdward Tomasz Napierala oldprr = pr->pr_prison_racct; 4170c34bbd2aSEdward Tomasz Napierala pr->pr_prison_racct = NULL; 4171c34bbd2aSEdward Tomasz Napierala 4172c34bbd2aSEdward Tomasz Napierala prison_racct_attach(pr); 4173c34bbd2aSEdward Tomasz Napierala 4174c34bbd2aSEdward Tomasz Napierala /* 4175c34bbd2aSEdward Tomasz Napierala * Move resource utilisation records. 4176c34bbd2aSEdward Tomasz Napierala */ 4177c34bbd2aSEdward Tomasz Napierala racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct); 4178c34bbd2aSEdward Tomasz Napierala 4179f87beb93SAndriy Gapon #ifdef RCTL 4180c34bbd2aSEdward Tomasz Napierala /* 4181c34bbd2aSEdward Tomasz Napierala * Force rctl to reattach rules to processes. 4182c34bbd2aSEdward Tomasz Napierala */ 4183c34bbd2aSEdward Tomasz Napierala FOREACH_PROC_IN_SYSTEM(p) { 4184c34bbd2aSEdward Tomasz Napierala PROC_LOCK(p); 4185c34bbd2aSEdward Tomasz Napierala cred = crhold(p->p_ucred); 4186c34bbd2aSEdward Tomasz Napierala PROC_UNLOCK(p); 4187f87beb93SAndriy Gapon rctl_proc_ucred_changed(p, cred); 4188c34bbd2aSEdward Tomasz Napierala crfree(cred); 4189c34bbd2aSEdward Tomasz Napierala } 4190f87beb93SAndriy Gapon #endif 4191c34bbd2aSEdward Tomasz Napierala 4192c34bbd2aSEdward Tomasz Napierala sx_sunlock(&allproc_lock); 4193c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(oldprr); 4194c34bbd2aSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4195c34bbd2aSEdward Tomasz Napierala } 4196c34bbd2aSEdward Tomasz Napierala 4197a7ad07bfSEdward Tomasz Napierala static void 4198a7ad07bfSEdward Tomasz Napierala prison_racct_detach(struct prison *pr) 4199a7ad07bfSEdward Tomasz Napierala { 4200c34bbd2aSEdward Tomasz Napierala 42014b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4202c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_UNLOCKED); 4203c34bbd2aSEdward Tomasz Napierala 4204af3c786cSMateusz Guzik if (pr->pr_prison_racct == NULL) 4205af3c786cSMateusz Guzik return; 4206a7ad07bfSEdward Tomasz Napierala prison_racct_free(pr->pr_prison_racct); 4207a7ad07bfSEdward Tomasz Napierala pr->pr_prison_racct = NULL; 4208a7ad07bfSEdward Tomasz Napierala } 4209a7ad07bfSEdward Tomasz Napierala #endif /* RACCT */ 4210a7ad07bfSEdward Tomasz Napierala 4211413628a7SBjoern A. Zeeb #ifdef DDB 4212b38ff370SJamie Gritton 4213b38ff370SJamie Gritton static void 4214b38ff370SJamie Gritton db_show_prison(struct prison *pr) 4215413628a7SBjoern A. Zeeb { 4216672756aaSJamie Gritton struct bool_flags *bf; 4217672756aaSJamie Gritton struct jailsys_flags *jsf; 4218b38ff370SJamie Gritton #if defined(INET) || defined(INET6) 4219b38ff370SJamie Gritton int ii; 4220413628a7SBjoern A. Zeeb #endif 4221672756aaSJamie Gritton unsigned f; 42228144690aSEric van Gyzen #ifdef INET 42238144690aSEric van Gyzen char ip4buf[INET_ADDRSTRLEN]; 42248144690aSEric van Gyzen #endif 4225413628a7SBjoern A. Zeeb #ifdef INET6 4226413628a7SBjoern A. Zeeb char ip6buf[INET6_ADDRSTRLEN]; 4227413628a7SBjoern A. Zeeb #endif 4228413628a7SBjoern A. Zeeb 4229b38ff370SJamie Gritton db_printf("prison %p:\n", pr); 4230b38ff370SJamie Gritton db_printf(" jid = %d\n", pr->pr_id); 4231b38ff370SJamie Gritton db_printf(" name = %s\n", pr->pr_name); 42320304c731SJamie Gritton db_printf(" parent = %p\n", pr->pr_parent); 4233b38ff370SJamie Gritton db_printf(" ref = %d\n", pr->pr_ref); 4234b38ff370SJamie Gritton db_printf(" uref = %d\n", pr->pr_uref); 4235b38ff370SJamie Gritton db_printf(" path = %s\n", pr->pr_path); 4236b38ff370SJamie Gritton db_printf(" cpuset = %d\n", pr->pr_cpuset 4237b38ff370SJamie Gritton ? pr->pr_cpuset->cs_id : -1); 4238679e1390SJamie Gritton #ifdef VIMAGE 4239679e1390SJamie Gritton db_printf(" vnet = %p\n", pr->pr_vnet); 4240679e1390SJamie Gritton #endif 4241b38ff370SJamie Gritton db_printf(" root = %p\n", pr->pr_root); 4242b38ff370SJamie Gritton db_printf(" securelevel = %d\n", pr->pr_securelevel); 42430cc207a6SMartin Matuska db_printf(" devfs_rsnum = %d\n", pr->pr_devfs_rsnum); 4244fe0518e9SBjoern A. Zeeb db_printf(" children.max = %d\n", pr->pr_childmax); 4245fe0518e9SBjoern A. Zeeb db_printf(" children.cur = %d\n", pr->pr_childcount); 42460304c731SJamie Gritton db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children)); 42470304c731SJamie Gritton db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling)); 4248fe0518e9SBjoern A. Zeeb db_printf(" flags = 0x%x", pr->pr_flags); 4249672756aaSJamie Gritton for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++) 4250672756aaSJamie Gritton if (pr->pr_flags & bf->flag) 4251672756aaSJamie Gritton db_printf(" %s", bf->name); 4252672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 4253672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 4254672756aaSJamie Gritton jsf++) { 4255672756aaSJamie Gritton f = pr->pr_flags & (jsf->disable | jsf->new); 4256672756aaSJamie Gritton db_printf(" %-16s= %s\n", jsf->name, 4257672756aaSJamie Gritton (f != 0 && f == jsf->disable) ? "disable" 4258672756aaSJamie Gritton : (f == jsf->new) ? "new" 42597cbf7213SJamie Gritton : "inherit"); 42607cbf7213SJamie Gritton } 4261fe0518e9SBjoern A. Zeeb db_printf(" allow = 0x%x", pr->pr_allow); 4262672756aaSJamie Gritton for (bf = pr_flag_allow; 42630e5c6bd4SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && bf->flag != 0; 4264672756aaSJamie Gritton bf++) 4265672756aaSJamie Gritton if (pr->pr_allow & bf->flag) 4266672756aaSJamie Gritton db_printf(" %s", bf->name); 4267b38ff370SJamie Gritton db_printf("\n"); 42680304c731SJamie Gritton db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs); 4269c1f19219SJamie Gritton db_printf(" host.hostname = %s\n", pr->pr_hostname); 4270c1f19219SJamie Gritton db_printf(" host.domainname = %s\n", pr->pr_domainname); 4271c1f19219SJamie Gritton db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid); 427276ca6f88SJamie Gritton db_printf(" host.hostid = %lu\n", pr->pr_hostid); 4273413628a7SBjoern A. Zeeb #ifdef INET 4274b38ff370SJamie Gritton db_printf(" ip4s = %d\n", pr->pr_ip4s); 4275b38ff370SJamie Gritton for (ii = 0; ii < pr->pr_ip4s; ii++) 4276b38ff370SJamie Gritton db_printf(" %s %s\n", 4277fe0518e9SBjoern A. Zeeb ii == 0 ? "ip4.addr =" : " ", 42788144690aSEric van Gyzen inet_ntoa_r(pr->pr_ip4[ii], ip4buf)); 4279413628a7SBjoern A. Zeeb #endif 4280413628a7SBjoern A. Zeeb #ifdef INET6 4281b38ff370SJamie Gritton db_printf(" ip6s = %d\n", pr->pr_ip6s); 4282b38ff370SJamie Gritton for (ii = 0; ii < pr->pr_ip6s; ii++) 4283b38ff370SJamie Gritton db_printf(" %s %s\n", 4284fe0518e9SBjoern A. Zeeb ii == 0 ? "ip6.addr =" : " ", 4285b38ff370SJamie Gritton ip6_sprintf(ip6buf, &pr->pr_ip6[ii])); 4286b38ff370SJamie Gritton #endif 4287b38ff370SJamie Gritton } 4288b38ff370SJamie Gritton 4289b38ff370SJamie Gritton DB_SHOW_COMMAND(prison, db_show_prison_command) 4290b38ff370SJamie Gritton { 4291b38ff370SJamie Gritton struct prison *pr; 4292b38ff370SJamie Gritton 4293b38ff370SJamie Gritton if (!have_addr) { 42940304c731SJamie Gritton /* 42950304c731SJamie Gritton * Show all prisons in the list, and prison0 which is not 42960304c731SJamie Gritton * listed. 42970304c731SJamie Gritton */ 42980304c731SJamie Gritton db_show_prison(&prison0); 42990304c731SJamie Gritton if (!db_pager_quit) { 4300b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 4301b38ff370SJamie Gritton db_show_prison(pr); 4302413628a7SBjoern A. Zeeb if (db_pager_quit) 4303413628a7SBjoern A. Zeeb break; 4304413628a7SBjoern A. Zeeb } 43050304c731SJamie Gritton } 4306b38ff370SJamie Gritton return; 4307413628a7SBjoern A. Zeeb } 4308b38ff370SJamie Gritton 43090304c731SJamie Gritton if (addr == 0) 43100304c731SJamie Gritton pr = &prison0; 43110304c731SJamie Gritton else { 4312b38ff370SJamie Gritton /* Look for a prison with the ID and with references. */ 4313b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 4314b38ff370SJamie Gritton if (pr->pr_id == addr && pr->pr_ref > 0) 4315b38ff370SJamie Gritton break; 4316b38ff370SJamie Gritton if (pr == NULL) 4317b38ff370SJamie Gritton /* Look again, without requiring a reference. */ 4318b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 4319b38ff370SJamie Gritton if (pr->pr_id == addr) 4320b38ff370SJamie Gritton break; 4321b38ff370SJamie Gritton if (pr == NULL) 4322b38ff370SJamie Gritton /* Assume address points to a valid prison. */ 4323b38ff370SJamie Gritton pr = (struct prison *)addr; 43240304c731SJamie Gritton } 4325b38ff370SJamie Gritton db_show_prison(pr); 4326b38ff370SJamie Gritton } 4327b38ff370SJamie Gritton 4328413628a7SBjoern A. Zeeb #endif /* DDB */ 4329