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> 5101137630SRobert Watson #include <sys/lock.h> 5201137630SRobert Watson #include <sys/mutex.h> 53097055e2SEdward Tomasz Napierala #include <sys/racct.h> 54f87beb93SAndriy Gapon #include <sys/rctl.h> 55a7ad07bfSEdward Tomasz Napierala #include <sys/refcount.h> 56dc68a633SPawel Jakub Dawidek #include <sys/sx.h> 5776ca6f88SJamie Gritton #include <sys/sysent.h> 58fd7a8150SMike Barcroft #include <sys/namei.h> 59820a0de9SPawel Jakub Dawidek #include <sys/mount.h> 60fd7a8150SMike Barcroft #include <sys/queue.h> 6175c13541SPoul-Henning Kamp #include <sys/socket.h> 62fd7a8150SMike Barcroft #include <sys/syscallsubr.h> 6383f1e257SRobert Watson #include <sys/sysctl.h> 64fd7a8150SMike Barcroft #include <sys/vnode.h> 65530c0060SRobert Watson 6675c13541SPoul-Henning Kamp #include <net/if.h> 67530c0060SRobert Watson #include <net/vnet.h> 68530c0060SRobert Watson 6975c13541SPoul-Henning Kamp #include <netinet/in.h> 70530c0060SRobert Watson 71413628a7SBjoern A. Zeeb #ifdef DDB 72413628a7SBjoern A. Zeeb #include <ddb/ddb.h> 73413628a7SBjoern A. Zeeb #endif /* DDB */ 7475c13541SPoul-Henning Kamp 75aed55708SRobert Watson #include <security/mac/mac_framework.h> 76aed55708SRobert Watson 778986e3a0SJamie Gritton #define DEFAULT_HOSTUUID "00000000-0000-0000-0000-000000000000" 788986e3a0SJamie Gritton 7975c13541SPoul-Henning Kamp MALLOC_DEFINE(M_PRISON, "prison", "Prison structures"); 80d745c852SEd Schouten static MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures"); 8175c13541SPoul-Henning Kamp 82592bcae8SBjoern A. Zeeb /* Keep struct prison prison0 and some code in kern_jail_set() readable. */ 83592bcae8SBjoern A. Zeeb #ifdef INET 84592bcae8SBjoern A. Zeeb #ifdef INET6 85592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL|PR_IP6_SADDRSEL 86592bcae8SBjoern A. Zeeb #else 87592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL 88592bcae8SBjoern A. Zeeb #endif 89592bcae8SBjoern A. Zeeb #else /* !INET */ 90592bcae8SBjoern A. Zeeb #ifdef INET6 91592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL PR_IP6_SADDRSEL 92592bcae8SBjoern A. Zeeb #else 93592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL 0 94592bcae8SBjoern A. Zeeb #endif 95592bcae8SBjoern A. Zeeb #endif 96592bcae8SBjoern A. Zeeb 970304c731SJamie Gritton /* prison0 describes what is "real" about the system. */ 980304c731SJamie Gritton struct prison prison0 = { 990304c731SJamie Gritton .pr_id = 0, 1000304c731SJamie Gritton .pr_name = "0", 1010304c731SJamie Gritton .pr_ref = 1, 1020304c731SJamie Gritton .pr_uref = 1, 1030304c731SJamie Gritton .pr_path = "/", 1040304c731SJamie Gritton .pr_securelevel = -1, 1050cc207a6SMartin Matuska .pr_devfs_rsnum = 0, 106b97457e2SJamie Gritton .pr_childmax = JAIL_MAX, 1078986e3a0SJamie Gritton .pr_hostuuid = DEFAULT_HOSTUUID, 10813e403fdSAntoine Brodin .pr_children = LIST_HEAD_INITIALIZER(prison0.pr_children), 109eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE 110592bcae8SBjoern A. Zeeb .pr_flags = PR_HOST|PR_VNET|_PR_IP_SADDRSEL, 111eb79e1c7SBjoern A. Zeeb #else 112592bcae8SBjoern A. Zeeb .pr_flags = PR_HOST|_PR_IP_SADDRSEL, 113eb79e1c7SBjoern A. Zeeb #endif 1140e5c6bd4SJamie Gritton .pr_allow = PR_ALLOW_ALL_STATIC, 1150304c731SJamie Gritton }; 1160304c731SJamie Gritton MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF); 11783f1e257SRobert Watson 118672756aaSJamie Gritton struct bool_flags { 119672756aaSJamie Gritton const char *name; 120672756aaSJamie Gritton const char *noname; 121672756aaSJamie Gritton unsigned flag; 122672756aaSJamie Gritton }; 123672756aaSJamie Gritton struct jailsys_flags { 124672756aaSJamie Gritton const char *name; 125672756aaSJamie Gritton unsigned disable; 126672756aaSJamie Gritton unsigned new; 127672756aaSJamie Gritton }; 128672756aaSJamie Gritton 129a7ad07bfSEdward Tomasz Napierala /* allprison, allprison_racct and lastprid are protected by allprison_lock. */ 130dc68a633SPawel Jakub Dawidek struct sx allprison_lock; 131b38ff370SJamie Gritton SX_SYSINIT(allprison_lock, &allprison_lock, "allprison"); 132b38ff370SJamie Gritton struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison); 133a7ad07bfSEdward Tomasz Napierala LIST_HEAD(, prison_racct) allprison_racct; 1342110d913SXin LI int lastprid = 0; 135fd7a8150SMike Barcroft 136b38ff370SJamie Gritton static int do_jail_attach(struct thread *td, struct prison *pr); 137b3059e09SRobert Watson static void prison_complete(void *context, int pending); 138b38ff370SJamie Gritton static void prison_deref(struct prison *pr, int flags); 1390304c731SJamie Gritton static char *prison_path(struct prison *pr1, struct prison *pr2); 1400304c731SJamie Gritton static void prison_remove_one(struct prison *pr); 141a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 142a7ad07bfSEdward Tomasz Napierala static void prison_racct_attach(struct prison *pr); 143c34bbd2aSEdward Tomasz Napierala static void prison_racct_modify(struct prison *pr); 144a7ad07bfSEdward Tomasz Napierala static void prison_racct_detach(struct prison *pr); 145a7ad07bfSEdward Tomasz Napierala #endif 146fd7a8150SMike Barcroft 147b38ff370SJamie Gritton /* Flags for prison_deref */ 148b38ff370SJamie Gritton #define PD_DEREF 0x01 149b38ff370SJamie Gritton #define PD_DEUREF 0x02 150b38ff370SJamie Gritton #define PD_LOCKED 0x04 151b38ff370SJamie Gritton #define PD_LIST_SLOCKED 0x08 152b38ff370SJamie Gritton #define PD_LIST_XLOCKED 0x10 153fd7a8150SMike Barcroft 1540304c731SJamie Gritton /* 1555cc70397SBjoern A. Zeeb * Parameter names corresponding to PR_* flag values. Size values are for kvm 1565cc70397SBjoern A. Zeeb * as we cannot figure out the size of a sparse array, or an array without a 1575cc70397SBjoern A. Zeeb * terminating entry. 1580304c731SJamie Gritton */ 159672756aaSJamie Gritton static struct bool_flags pr_flag_bool[] = { 160672756aaSJamie Gritton {"persist", "nopersist", PR_PERSIST}, 161592bcae8SBjoern A. Zeeb #ifdef INET 162672756aaSJamie Gritton {"ip4.saddrsel", "ip4.nosaddrsel", PR_IP4_SADDRSEL}, 163592bcae8SBjoern A. Zeeb #endif 164592bcae8SBjoern A. Zeeb #ifdef INET6 165672756aaSJamie Gritton {"ip6.saddrsel", "ip6.nosaddrsel", PR_IP6_SADDRSEL}, 166592bcae8SBjoern A. Zeeb #endif 1670304c731SJamie Gritton }; 168672756aaSJamie Gritton const size_t pr_flag_bool_size = sizeof(pr_flag_bool); 1690304c731SJamie Gritton 170672756aaSJamie Gritton static struct jailsys_flags pr_flag_jailsys[] = { 1717cbf7213SJamie Gritton {"host", 0, PR_HOST}, 1727cbf7213SJamie Gritton #ifdef VIMAGE 1737cbf7213SJamie Gritton {"vnet", 0, PR_VNET}, 1747cbf7213SJamie Gritton #endif 1750304c731SJamie Gritton #ifdef INET 1766a3f2779SJamie Gritton {"ip4", PR_IP4_USER, PR_IP4_USER}, 1770304c731SJamie Gritton #endif 1780304c731SJamie Gritton #ifdef INET6 1796a3f2779SJamie Gritton {"ip6", PR_IP6_USER, PR_IP6_USER}, 180679e1390SJamie Gritton #endif 1810304c731SJamie Gritton }; 1825cc70397SBjoern A. Zeeb const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys); 1830304c731SJamie Gritton 1840e5c6bd4SJamie Gritton /* Make this array full-size so dynamic parameters can be added. */ 1850e5c6bd4SJamie Gritton static struct bool_flags pr_flag_allow[NBBY * NBPW] = { 186672756aaSJamie Gritton {"allow.set_hostname", "allow.noset_hostname", PR_ALLOW_SET_HOSTNAME}, 187672756aaSJamie Gritton {"allow.sysvipc", "allow.nosysvipc", PR_ALLOW_SYSVIPC}, 188672756aaSJamie Gritton {"allow.raw_sockets", "allow.noraw_sockets", PR_ALLOW_RAW_SOCKETS}, 189672756aaSJamie Gritton {"allow.chflags", "allow.nochflags", PR_ALLOW_CHFLAGS}, 190672756aaSJamie Gritton {"allow.mount", "allow.nomount", PR_ALLOW_MOUNT}, 191672756aaSJamie Gritton {"allow.quotas", "allow.noquotas", PR_ALLOW_QUOTAS}, 192672756aaSJamie Gritton {"allow.socket_af", "allow.nosocket_af", PR_ALLOW_SOCKET_AF}, 193672756aaSJamie Gritton {"allow.reserved_ports", "allow.noreserved_ports", 194672756aaSJamie Gritton PR_ALLOW_RESERVED_PORTS}, 1950304c731SJamie Gritton }; 196672756aaSJamie Gritton const size_t pr_flag_allow_size = sizeof(pr_flag_allow); 1970304c731SJamie Gritton 198e28f9b7dSAllan Jude #define JAIL_DEFAULT_ALLOW (PR_ALLOW_SET_HOSTNAME | PR_ALLOW_RESERVED_PORTS) 19942bebd82SJamie Gritton #define JAIL_DEFAULT_ENFORCE_STATFS 2 200bf3db8aaSMartin Matuska #define JAIL_DEFAULT_DEVFS_RSNUM 0 2010304c731SJamie Gritton static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW; 20242bebd82SJamie Gritton static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 2030cc207a6SMartin Matuska static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM; 2040304c731SJamie Gritton #if defined(INET) || defined(INET6) 205e92e0574SJamie Gritton static unsigned jail_max_af_ips = 255; 2060304c731SJamie Gritton #endif 2070304c731SJamie Gritton 208b96bd95bSIan Lepore /* 209b96bd95bSIan Lepore * Initialize the parts of prison0 that can't be static-initialized with 210b96bd95bSIan Lepore * constants. This is called from proc0_init() after creating thread0 cpuset. 211b96bd95bSIan Lepore */ 212b96bd95bSIan Lepore void 213b96bd95bSIan Lepore prison0_init(void) 214b96bd95bSIan Lepore { 215b96bd95bSIan Lepore 216b96bd95bSIan Lepore prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset); 217b96bd95bSIan Lepore prison0.pr_osreldate = osreldate; 218b96bd95bSIan Lepore strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease)); 219b96bd95bSIan Lepore } 220b96bd95bSIan Lepore 221116734c4SMatthew Dillon /* 2229ddb7954SMike Barcroft * struct jail_args { 2239ddb7954SMike Barcroft * struct jail *jail; 2249ddb7954SMike Barcroft * }; 225116734c4SMatthew Dillon */ 22675c13541SPoul-Henning Kamp int 2278451d0ddSKip Macy sys_jail(struct thread *td, struct jail_args *uap) 22875c13541SPoul-Henning Kamp { 229413628a7SBjoern A. Zeeb uint32_t version; 230413628a7SBjoern A. Zeeb int error; 2310304c731SJamie Gritton struct jail j; 232413628a7SBjoern A. Zeeb 233413628a7SBjoern A. Zeeb error = copyin(uap->jail, &version, sizeof(uint32_t)); 234413628a7SBjoern A. Zeeb if (error) 235413628a7SBjoern A. Zeeb return (error); 236413628a7SBjoern A. Zeeb 237413628a7SBjoern A. Zeeb switch (version) { 238413628a7SBjoern A. Zeeb case 0: 239413628a7SBjoern A. Zeeb { 240413628a7SBjoern A. Zeeb struct jail_v0 j0; 241413628a7SBjoern A. Zeeb 2420304c731SJamie Gritton /* FreeBSD single IPv4 jails. */ 2430304c731SJamie Gritton bzero(&j, sizeof(struct jail)); 244413628a7SBjoern A. Zeeb error = copyin(uap->jail, &j0, sizeof(struct jail_v0)); 245413628a7SBjoern A. Zeeb if (error) 246413628a7SBjoern A. Zeeb return (error); 2470304c731SJamie Gritton j.version = j0.version; 2480304c731SJamie Gritton j.path = j0.path; 2490304c731SJamie Gritton j.hostname = j0.hostname; 250b5019bc4SPeter Wemm j.ip4s = htonl(j0.ip_number); /* jail_v0 is host order */ 251413628a7SBjoern A. Zeeb break; 252413628a7SBjoern A. Zeeb } 253413628a7SBjoern A. Zeeb 254413628a7SBjoern A. Zeeb case 1: 255413628a7SBjoern A. Zeeb /* 256413628a7SBjoern A. Zeeb * Version 1 was used by multi-IPv4 jail implementations 257413628a7SBjoern A. Zeeb * that never made it into the official kernel. 258413628a7SBjoern A. Zeeb */ 259413628a7SBjoern A. Zeeb return (EINVAL); 260413628a7SBjoern A. Zeeb 261413628a7SBjoern A. Zeeb case 2: /* JAIL_API_VERSION */ 262413628a7SBjoern A. Zeeb /* FreeBSD multi-IPv4/IPv6,noIP jails. */ 263413628a7SBjoern A. Zeeb error = copyin(uap->jail, &j, sizeof(struct jail)); 264413628a7SBjoern A. Zeeb if (error) 265413628a7SBjoern A. Zeeb return (error); 2660304c731SJamie Gritton break; 2670304c731SJamie Gritton 2680304c731SJamie Gritton default: 2690304c731SJamie Gritton /* Sci-Fi jails are not supported, sorry. */ 2700304c731SJamie Gritton return (EINVAL); 2710304c731SJamie Gritton } 2720304c731SJamie Gritton return (kern_jail(td, &j)); 2730304c731SJamie Gritton } 2740304c731SJamie Gritton 2750304c731SJamie Gritton int 2760304c731SJamie Gritton kern_jail(struct thread *td, struct jail *j) 2770304c731SJamie Gritton { 278672756aaSJamie Gritton struct iovec optiov[2 * (4 + nitems(pr_flag_allow) 279e92e0574SJamie Gritton #ifdef INET 280e92e0574SJamie Gritton + 1 281e92e0574SJamie Gritton #endif 282e92e0574SJamie Gritton #ifdef INET6 283e92e0574SJamie Gritton + 1 284e92e0574SJamie Gritton #endif 285e92e0574SJamie Gritton )]; 2860304c731SJamie Gritton struct uio opt; 2870304c731SJamie Gritton char *u_path, *u_hostname, *u_name; 288672756aaSJamie Gritton struct bool_flags *bf; 2890304c731SJamie Gritton #ifdef INET 290e92e0574SJamie Gritton uint32_t ip4s; 2910304c731SJamie Gritton struct in_addr *u_ip4; 2920304c731SJamie Gritton #endif 2930304c731SJamie Gritton #ifdef INET6 2940304c731SJamie Gritton struct in6_addr *u_ip6; 2950304c731SJamie Gritton #endif 2960304c731SJamie Gritton size_t tmplen; 297672756aaSJamie Gritton int error, enforce_statfs; 2980304c731SJamie Gritton 2990304c731SJamie Gritton bzero(&optiov, sizeof(optiov)); 3000304c731SJamie Gritton opt.uio_iov = optiov; 3010304c731SJamie Gritton opt.uio_iovcnt = 0; 3020304c731SJamie Gritton opt.uio_offset = -1; 3030304c731SJamie Gritton opt.uio_resid = -1; 3040304c731SJamie Gritton opt.uio_segflg = UIO_SYSSPACE; 3050304c731SJamie Gritton opt.uio_rw = UIO_READ; 3060304c731SJamie Gritton opt.uio_td = td; 3070304c731SJamie Gritton 3080304c731SJamie Gritton /* Set permissions for top-level jails from sysctls. */ 3090304c731SJamie Gritton if (!jailed(td->td_ucred)) { 310672756aaSJamie Gritton for (bf = pr_flag_allow; 3110e5c6bd4SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 3120e5c6bd4SJamie Gritton bf->flag != 0; 313672756aaSJamie Gritton bf++) { 314672756aaSJamie Gritton optiov[opt.uio_iovcnt].iov_base = __DECONST(char *, 315672756aaSJamie Gritton (jail_default_allow & bf->flag) 316672756aaSJamie Gritton ? bf->name : bf->noname); 3170304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = 3180304c731SJamie Gritton strlen(optiov[opt.uio_iovcnt].iov_base) + 1; 3190304c731SJamie Gritton opt.uio_iovcnt += 2; 3200304c731SJamie Gritton } 3210304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "enforce_statfs"; 3220304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs"); 3230304c731SJamie Gritton opt.uio_iovcnt++; 3240304c731SJamie Gritton enforce_statfs = jail_default_enforce_statfs; 3250304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = &enforce_statfs; 3260304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs); 3270304c731SJamie Gritton opt.uio_iovcnt++; 3280304c731SJamie Gritton } 3290304c731SJamie Gritton 330b38ff370SJamie Gritton tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN; 331b38ff370SJamie Gritton #ifdef INET 3320304c731SJamie Gritton ip4s = (j->version == 0) ? 1 : j->ip4s; 3330304c731SJamie Gritton if (ip4s > jail_max_af_ips) 334b38ff370SJamie Gritton return (EINVAL); 3350304c731SJamie Gritton tmplen += ip4s * sizeof(struct in_addr); 336b38ff370SJamie Gritton #else 3370304c731SJamie Gritton if (j->ip4s > 0) 338b38ff370SJamie Gritton return (EINVAL); 339b38ff370SJamie Gritton #endif 340b38ff370SJamie Gritton #ifdef INET6 3410304c731SJamie Gritton if (j->ip6s > jail_max_af_ips) 342b38ff370SJamie Gritton return (EINVAL); 3430304c731SJamie Gritton tmplen += j->ip6s * sizeof(struct in6_addr); 344b38ff370SJamie Gritton #else 3450304c731SJamie Gritton if (j->ip6s > 0) 346b38ff370SJamie Gritton return (EINVAL); 347b38ff370SJamie Gritton #endif 348b38ff370SJamie Gritton u_path = malloc(tmplen, M_TEMP, M_WAITOK); 349b38ff370SJamie Gritton u_hostname = u_path + MAXPATHLEN; 350b38ff370SJamie Gritton u_name = u_hostname + MAXHOSTNAMELEN; 351b38ff370SJamie Gritton #ifdef INET 352b38ff370SJamie Gritton u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN); 353b38ff370SJamie Gritton #endif 354b38ff370SJamie Gritton #ifdef INET6 355b38ff370SJamie Gritton #ifdef INET 3560304c731SJamie Gritton u_ip6 = (struct in6_addr *)(u_ip4 + ip4s); 357b38ff370SJamie Gritton #else 358b38ff370SJamie Gritton u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN); 359b38ff370SJamie Gritton #endif 360b38ff370SJamie Gritton #endif 3610304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "path"; 3620304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("path"); 3630304c731SJamie Gritton opt.uio_iovcnt++; 3640304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_path; 3650304c731SJamie Gritton error = copyinstr(j->path, u_path, MAXPATHLEN, 3660304c731SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 367b38ff370SJamie Gritton if (error) { 368b38ff370SJamie Gritton free(u_path, M_TEMP); 369b38ff370SJamie Gritton return (error); 370b38ff370SJamie Gritton } 3710304c731SJamie Gritton opt.uio_iovcnt++; 3720304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "host.hostname"; 3730304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname"); 3740304c731SJamie Gritton opt.uio_iovcnt++; 3750304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_hostname; 3760304c731SJamie Gritton error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN, 3770304c731SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 378b38ff370SJamie Gritton if (error) { 379b38ff370SJamie Gritton free(u_path, M_TEMP); 380b38ff370SJamie Gritton return (error); 381b38ff370SJamie Gritton } 3820304c731SJamie Gritton opt.uio_iovcnt++; 3830304c731SJamie Gritton if (j->jailname != NULL) { 384b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "name"; 385b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("name"); 386b38ff370SJamie Gritton opt.uio_iovcnt++; 387b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_name; 3880304c731SJamie Gritton error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN, 389b38ff370SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 390b38ff370SJamie Gritton if (error) { 391b38ff370SJamie Gritton free(u_path, M_TEMP); 392b38ff370SJamie Gritton return (error); 393b38ff370SJamie Gritton } 394b38ff370SJamie Gritton opt.uio_iovcnt++; 395b38ff370SJamie Gritton } 396b38ff370SJamie Gritton #ifdef INET 397b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "ip4.addr"; 398b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr"); 399b38ff370SJamie Gritton opt.uio_iovcnt++; 400b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_ip4; 4010304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr); 4020304c731SJamie Gritton if (j->version == 0) 4030304c731SJamie Gritton u_ip4->s_addr = j->ip4s; 4040304c731SJamie Gritton else { 4050304c731SJamie Gritton error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len); 406b38ff370SJamie Gritton if (error) { 407b38ff370SJamie Gritton free(u_path, M_TEMP); 408b38ff370SJamie Gritton return (error); 409b38ff370SJamie Gritton } 4100304c731SJamie Gritton } 411b38ff370SJamie Gritton opt.uio_iovcnt++; 412b38ff370SJamie Gritton #endif 413b38ff370SJamie Gritton #ifdef INET6 414b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "ip6.addr"; 415b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr"); 416b38ff370SJamie Gritton opt.uio_iovcnt++; 417b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_ip6; 4180304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr); 4190304c731SJamie Gritton error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len); 420b38ff370SJamie Gritton if (error) { 421b38ff370SJamie Gritton free(u_path, M_TEMP); 422b38ff370SJamie Gritton return (error); 423b38ff370SJamie Gritton } 424b38ff370SJamie Gritton opt.uio_iovcnt++; 425b38ff370SJamie Gritton #endif 42602abd400SPedro F. Giffuni KASSERT(opt.uio_iovcnt <= nitems(optiov), 4270304c731SJamie Gritton ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt)); 428b38ff370SJamie Gritton error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH); 429b38ff370SJamie Gritton free(u_path, M_TEMP); 430b38ff370SJamie Gritton return (error); 431b38ff370SJamie Gritton } 432b38ff370SJamie Gritton 4330304c731SJamie Gritton 434b38ff370SJamie Gritton /* 435b38ff370SJamie Gritton * struct jail_set_args { 436b38ff370SJamie Gritton * struct iovec *iovp; 437b38ff370SJamie Gritton * unsigned int iovcnt; 438b38ff370SJamie Gritton * int flags; 439b38ff370SJamie Gritton * }; 440b38ff370SJamie Gritton */ 441b38ff370SJamie Gritton int 4428451d0ddSKip Macy sys_jail_set(struct thread *td, struct jail_set_args *uap) 443b38ff370SJamie Gritton { 444b38ff370SJamie Gritton struct uio *auio; 445b38ff370SJamie Gritton int error; 446b38ff370SJamie Gritton 447b38ff370SJamie Gritton /* Check that we have an even number of iovecs. */ 448b38ff370SJamie Gritton if (uap->iovcnt & 1) 449b38ff370SJamie Gritton return (EINVAL); 450b38ff370SJamie Gritton 451b38ff370SJamie Gritton error = copyinuio(uap->iovp, uap->iovcnt, &auio); 452b38ff370SJamie Gritton if (error) 453b38ff370SJamie Gritton return (error); 454b38ff370SJamie Gritton error = kern_jail_set(td, auio, uap->flags); 455b38ff370SJamie Gritton free(auio, M_IOV); 456b38ff370SJamie Gritton return (error); 457413628a7SBjoern A. Zeeb } 458413628a7SBjoern A. Zeeb 459413628a7SBjoern A. Zeeb int 460b38ff370SJamie Gritton kern_jail_set(struct thread *td, struct uio *optuio, int flags) 461413628a7SBjoern A. Zeeb { 462fd7a8150SMike Barcroft struct nameidata nd; 463b38ff370SJamie Gritton #ifdef INET 464b38ff370SJamie Gritton struct in_addr *ip4; 465413628a7SBjoern A. Zeeb #endif 466b38ff370SJamie Gritton #ifdef INET6 467b38ff370SJamie Gritton struct in6_addr *ip6; 468b38ff370SJamie Gritton #endif 469b38ff370SJamie Gritton struct vfsopt *opt; 470b38ff370SJamie Gritton struct vfsoptlist *opts; 47157aea6dfSBjoern A. Zeeb struct prison *pr, *deadpr, *mypr, *ppr, *tpr; 472b38ff370SJamie Gritton struct vnode *root; 473babbbb9cSJamie Gritton char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid; 474b96bd95bSIan Lepore char *g_path, *osrelstr; 475672756aaSJamie Gritton struct bool_flags *bf; 476672756aaSJamie Gritton struct jailsys_flags *jsf; 4770304c731SJamie Gritton #if defined(INET) || defined(INET6) 47857aea6dfSBjoern A. Zeeb struct prison *tppr; 479b38ff370SJamie Gritton void *op; 4800304c731SJamie Gritton #endif 48176ca6f88SJamie Gritton unsigned long hid; 482b6f47c23SJamie Gritton size_t namelen, onamelen, pnamelen; 483cc5fd8c7SJamie Gritton int born, created, cuflags, descend, enforce; 484cc5fd8c7SJamie Gritton int error, errmsg_len, errmsg_pos; 4850cc207a6SMartin Matuska int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel; 486672756aaSJamie Gritton int jid, jsys, len, level; 487b96bd95bSIan Lepore int childmax, osreldt, rsnum, slevel; 488f6e633a9SMartin Matuska int fullpath_disabled; 489d465a41dSBjoern A. Zeeb #if defined(INET) || defined(INET6) 4900304c731SJamie Gritton int ii, ij; 491413628a7SBjoern A. Zeeb #endif 492413628a7SBjoern A. Zeeb #ifdef INET 4932b0d6f81SJamie Gritton int ip4s, redo_ip4; 494413628a7SBjoern A. Zeeb #endif 495b38ff370SJamie Gritton #ifdef INET6 4962b0d6f81SJamie Gritton int ip6s, redo_ip6; 497b38ff370SJamie Gritton #endif 4986beb3bb4SKirk McKusick uint64_t pr_allow, ch_allow, pr_flags, ch_flags; 4996beb3bb4SKirk McKusick unsigned tallow; 500b38ff370SJamie Gritton char numbuf[12]; 501b38ff370SJamie Gritton 502b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_SET); 503b38ff370SJamie Gritton if (!error && (flags & JAIL_ATTACH)) 504b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 505b38ff370SJamie Gritton if (error) 50675c13541SPoul-Henning Kamp return (error); 507b6f47c23SJamie Gritton mypr = td->td_ucred->cr_prison; 508b97457e2SJamie Gritton if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0) 5090304c731SJamie Gritton return (EPERM); 510b38ff370SJamie Gritton if (flags & ~JAIL_SET_MASK) 511b38ff370SJamie Gritton return (EINVAL); 512b38ff370SJamie Gritton 513b38ff370SJamie Gritton /* 514b38ff370SJamie Gritton * Check all the parameters before committing to anything. Not all 515b38ff370SJamie Gritton * errors can be caught early, but we may as well try. Also, this 516b38ff370SJamie Gritton * takes care of some expensive stuff (path lookup) before getting 517b38ff370SJamie Gritton * the allprison lock. 518b38ff370SJamie Gritton * 519b38ff370SJamie Gritton * XXX Jails are not filesystems, and jail parameters are not mount 520b38ff370SJamie Gritton * options. But it makes more sense to re-use the vfsopt code 521b38ff370SJamie Gritton * than duplicate it under a different name. 522b38ff370SJamie Gritton */ 523b38ff370SJamie Gritton error = vfs_buildopts(optuio, &opts); 524b38ff370SJamie Gritton if (error) 525b38ff370SJamie Gritton return (error); 526b38ff370SJamie Gritton #ifdef INET 527b38ff370SJamie Gritton ip4 = NULL; 528b38ff370SJamie Gritton #endif 529b38ff370SJamie Gritton #ifdef INET6 530b38ff370SJamie Gritton ip6 = NULL; 531b38ff370SJamie Gritton #endif 5326dfe0a3dSMartin Matuska g_path = NULL; 533b38ff370SJamie Gritton 534b6f47c23SJamie Gritton cuflags = flags & (JAIL_CREATE | JAIL_UPDATE); 535b6f47c23SJamie Gritton if (!cuflags) { 536b6f47c23SJamie Gritton error = EINVAL; 537b6f47c23SJamie Gritton vfs_opterror(opts, "no valid operation (create or update)"); 538b6f47c23SJamie Gritton goto done_errmsg; 539b6f47c23SJamie Gritton } 540b6f47c23SJamie Gritton 541b38ff370SJamie Gritton error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 542b38ff370SJamie Gritton if (error == ENOENT) 543b38ff370SJamie Gritton jid = 0; 544b38ff370SJamie Gritton else if (error != 0) 545b38ff370SJamie Gritton goto done_free; 546b38ff370SJamie Gritton 547b38ff370SJamie Gritton error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel)); 548b38ff370SJamie Gritton if (error == ENOENT) 549b38ff370SJamie Gritton gotslevel = 0; 550b38ff370SJamie Gritton else if (error != 0) 551b38ff370SJamie Gritton goto done_free; 552b38ff370SJamie Gritton else 553b38ff370SJamie Gritton gotslevel = 1; 554b38ff370SJamie Gritton 555b97457e2SJamie Gritton error = 556b97457e2SJamie Gritton vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax)); 557b97457e2SJamie Gritton if (error == ENOENT) 558b97457e2SJamie Gritton gotchildmax = 0; 559b97457e2SJamie Gritton else if (error != 0) 560b97457e2SJamie Gritton goto done_free; 561b97457e2SJamie Gritton else 562b97457e2SJamie Gritton gotchildmax = 1; 563b97457e2SJamie Gritton 5640304c731SJamie Gritton error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce)); 565f337198dSJamie Gritton if (error == ENOENT) 566f337198dSJamie Gritton gotenforce = 0; 567f337198dSJamie Gritton else if (error != 0) 5680304c731SJamie Gritton goto done_free; 569f337198dSJamie Gritton else if (enforce < 0 || enforce > 2) { 570f337198dSJamie Gritton error = EINVAL; 571f337198dSJamie Gritton goto done_free; 572f337198dSJamie Gritton } else 573f337198dSJamie Gritton gotenforce = 1; 5740304c731SJamie Gritton 5750cc207a6SMartin Matuska error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum)); 5760cc207a6SMartin Matuska if (error == ENOENT) 5770cc207a6SMartin Matuska gotrsnum = 0; 5780cc207a6SMartin Matuska else if (error != 0) 5790cc207a6SMartin Matuska goto done_free; 5800cc207a6SMartin Matuska else 5810cc207a6SMartin Matuska gotrsnum = 1; 5820cc207a6SMartin Matuska 583b38ff370SJamie Gritton pr_flags = ch_flags = 0; 584672756aaSJamie Gritton for (bf = pr_flag_bool; 585672756aaSJamie Gritton bf < pr_flag_bool + nitems(pr_flag_bool); 586672756aaSJamie Gritton bf++) { 587672756aaSJamie Gritton vfs_flagopt(opts, bf->name, &pr_flags, bf->flag); 588672756aaSJamie Gritton vfs_flagopt(opts, bf->noname, &ch_flags, bf->flag); 5890304c731SJamie Gritton } 590b38ff370SJamie Gritton ch_flags |= pr_flags; 591672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 592672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 593672756aaSJamie Gritton jsf++) { 594672756aaSJamie Gritton error = vfs_copyopt(opts, jsf->name, &jsys, sizeof(jsys)); 5957cbf7213SJamie Gritton if (error == ENOENT) 5967cbf7213SJamie Gritton continue; 5977cbf7213SJamie Gritton if (error != 0) 5987cbf7213SJamie Gritton goto done_free; 5997cbf7213SJamie Gritton switch (jsys) { 6007cbf7213SJamie Gritton case JAIL_SYS_DISABLE: 601672756aaSJamie Gritton if (!jsf->disable) { 6027cbf7213SJamie Gritton error = EINVAL; 6037cbf7213SJamie Gritton goto done_free; 6047cbf7213SJamie Gritton } 605672756aaSJamie Gritton pr_flags |= jsf->disable; 6067cbf7213SJamie Gritton break; 6077cbf7213SJamie Gritton case JAIL_SYS_NEW: 608672756aaSJamie Gritton pr_flags |= jsf->new; 6097cbf7213SJamie Gritton break; 6107cbf7213SJamie Gritton case JAIL_SYS_INHERIT: 6117cbf7213SJamie Gritton break; 6127cbf7213SJamie Gritton default: 6137cbf7213SJamie Gritton error = EINVAL; 6147cbf7213SJamie Gritton goto done_free; 6157cbf7213SJamie Gritton } 616672756aaSJamie Gritton ch_flags |= jsf->new | jsf->disable; 6177cbf7213SJamie Gritton } 6184affa14cSJamie Gritton if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE 6194affa14cSJamie Gritton && !(pr_flags & PR_PERSIST)) { 6204affa14cSJamie Gritton error = EINVAL; 6214affa14cSJamie Gritton vfs_opterror(opts, "new jail must persist or attach"); 6224affa14cSJamie Gritton goto done_errmsg; 6234affa14cSJamie Gritton } 624679e1390SJamie Gritton #ifdef VIMAGE 625679e1390SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) { 626679e1390SJamie Gritton error = EINVAL; 627679e1390SJamie Gritton vfs_opterror(opts, "vnet cannot be changed after creation"); 628679e1390SJamie Gritton goto done_errmsg; 629679e1390SJamie Gritton } 630679e1390SJamie Gritton #endif 6312b0d6f81SJamie Gritton #ifdef INET 6322b0d6f81SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) { 6332b0d6f81SJamie Gritton error = EINVAL; 6342b0d6f81SJamie Gritton vfs_opterror(opts, "ip4 cannot be changed after creation"); 6352b0d6f81SJamie Gritton goto done_errmsg; 6362b0d6f81SJamie Gritton } 6372b0d6f81SJamie Gritton #endif 6382b0d6f81SJamie Gritton #ifdef INET6 6392b0d6f81SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) { 6402b0d6f81SJamie Gritton error = EINVAL; 6412b0d6f81SJamie Gritton vfs_opterror(opts, "ip6 cannot be changed after creation"); 6422b0d6f81SJamie Gritton goto done_errmsg; 6432b0d6f81SJamie Gritton } 6442b0d6f81SJamie Gritton #endif 645b38ff370SJamie Gritton 6460304c731SJamie Gritton pr_allow = ch_allow = 0; 647672756aaSJamie Gritton for (bf = pr_flag_allow; 6480e5c6bd4SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && bf->flag != 0; 649672756aaSJamie Gritton bf++) { 650672756aaSJamie Gritton vfs_flagopt(opts, bf->name, &pr_allow, bf->flag); 651672756aaSJamie Gritton vfs_flagopt(opts, bf->noname, &ch_allow, bf->flag); 6520304c731SJamie Gritton } 6530304c731SJamie Gritton ch_allow |= pr_allow; 6540304c731SJamie Gritton 655b38ff370SJamie Gritton error = vfs_getopt(opts, "name", (void **)&name, &len); 656b38ff370SJamie Gritton if (error == ENOENT) 657b38ff370SJamie Gritton name = NULL; 658b38ff370SJamie Gritton else if (error != 0) 659b38ff370SJamie Gritton goto done_free; 660b38ff370SJamie Gritton else { 661b38ff370SJamie Gritton if (len == 0 || name[len - 1] != '\0') { 662b38ff370SJamie Gritton error = EINVAL; 663b38ff370SJamie Gritton goto done_free; 664b38ff370SJamie Gritton } 665b38ff370SJamie Gritton if (len > MAXHOSTNAMELEN) { 666b38ff370SJamie Gritton error = ENAMETOOLONG; 667b38ff370SJamie Gritton goto done_free; 668b38ff370SJamie Gritton } 669b38ff370SJamie Gritton } 670b38ff370SJamie Gritton 671b38ff370SJamie Gritton error = vfs_getopt(opts, "host.hostname", (void **)&host, &len); 672b38ff370SJamie Gritton if (error == ENOENT) 673b38ff370SJamie Gritton host = NULL; 674b38ff370SJamie Gritton else if (error != 0) 675b38ff370SJamie Gritton goto done_free; 676b38ff370SJamie Gritton else { 67776ca6f88SJamie Gritton ch_flags |= PR_HOST; 67876ca6f88SJamie Gritton pr_flags |= PR_HOST; 679b38ff370SJamie Gritton if (len == 0 || host[len - 1] != '\0') { 680b38ff370SJamie Gritton error = EINVAL; 681b38ff370SJamie Gritton goto done_free; 682b38ff370SJamie Gritton } 683b38ff370SJamie Gritton if (len > MAXHOSTNAMELEN) { 684b38ff370SJamie Gritton error = ENAMETOOLONG; 685b38ff370SJamie Gritton goto done_free; 686b38ff370SJamie Gritton } 687b38ff370SJamie Gritton } 688b38ff370SJamie Gritton 68976ca6f88SJamie Gritton error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len); 69076ca6f88SJamie Gritton if (error == ENOENT) 69176ca6f88SJamie Gritton domain = NULL; 69276ca6f88SJamie Gritton else if (error != 0) 69376ca6f88SJamie Gritton goto done_free; 69476ca6f88SJamie Gritton else { 69576ca6f88SJamie Gritton ch_flags |= PR_HOST; 69676ca6f88SJamie Gritton pr_flags |= PR_HOST; 69776ca6f88SJamie Gritton if (len == 0 || domain[len - 1] != '\0') { 69876ca6f88SJamie Gritton error = EINVAL; 69976ca6f88SJamie Gritton goto done_free; 70076ca6f88SJamie Gritton } 70176ca6f88SJamie Gritton if (len > MAXHOSTNAMELEN) { 70276ca6f88SJamie Gritton error = ENAMETOOLONG; 70376ca6f88SJamie Gritton goto done_free; 70476ca6f88SJamie Gritton } 70576ca6f88SJamie Gritton } 70676ca6f88SJamie Gritton 70776ca6f88SJamie Gritton error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len); 70876ca6f88SJamie Gritton if (error == ENOENT) 70976ca6f88SJamie Gritton uuid = NULL; 71076ca6f88SJamie Gritton else if (error != 0) 71176ca6f88SJamie Gritton goto done_free; 71276ca6f88SJamie Gritton else { 71376ca6f88SJamie Gritton ch_flags |= PR_HOST; 71476ca6f88SJamie Gritton pr_flags |= PR_HOST; 71576ca6f88SJamie Gritton if (len == 0 || uuid[len - 1] != '\0') { 71676ca6f88SJamie Gritton error = EINVAL; 71776ca6f88SJamie Gritton goto done_free; 71876ca6f88SJamie Gritton } 71976ca6f88SJamie Gritton if (len > HOSTUUIDLEN) { 72076ca6f88SJamie Gritton error = ENAMETOOLONG; 72176ca6f88SJamie Gritton goto done_free; 72276ca6f88SJamie Gritton } 72376ca6f88SJamie Gritton } 72476ca6f88SJamie Gritton 725841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32 726a5c1afadSDmitry Chagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 72776ca6f88SJamie Gritton uint32_t hid32; 72876ca6f88SJamie Gritton 72976ca6f88SJamie Gritton error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32)); 73076ca6f88SJamie Gritton hid = hid32; 73176ca6f88SJamie Gritton } else 73276ca6f88SJamie Gritton #endif 73376ca6f88SJamie Gritton error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid)); 73476ca6f88SJamie Gritton if (error == ENOENT) 73576ca6f88SJamie Gritton gothid = 0; 73676ca6f88SJamie Gritton else if (error != 0) 73776ca6f88SJamie Gritton goto done_free; 73876ca6f88SJamie Gritton else { 73976ca6f88SJamie Gritton gothid = 1; 74076ca6f88SJamie Gritton ch_flags |= PR_HOST; 74176ca6f88SJamie Gritton pr_flags |= PR_HOST; 74276ca6f88SJamie Gritton } 74376ca6f88SJamie Gritton 744b38ff370SJamie Gritton #ifdef INET 745b38ff370SJamie Gritton error = vfs_getopt(opts, "ip4.addr", &op, &ip4s); 746b38ff370SJamie Gritton if (error == ENOENT) 7470e5e396eSJamie Gritton ip4s = 0; 748b38ff370SJamie Gritton else if (error != 0) 749b38ff370SJamie Gritton goto done_free; 750b38ff370SJamie Gritton else if (ip4s & (sizeof(*ip4) - 1)) { 751b38ff370SJamie Gritton error = EINVAL; 752b38ff370SJamie Gritton goto done_free; 7530304c731SJamie Gritton } else { 7546a3f2779SJamie Gritton ch_flags |= PR_IP4_USER; 7556a3f2779SJamie Gritton pr_flags |= PR_IP4_USER; 7566a3f2779SJamie Gritton if (ip4s > 0) { 757b38ff370SJamie Gritton ip4s /= sizeof(*ip4); 758b38ff370SJamie Gritton if (ip4s > jail_max_af_ips) { 759b38ff370SJamie Gritton error = EINVAL; 760b38ff370SJamie Gritton vfs_opterror(opts, "too many IPv4 addresses"); 761b38ff370SJamie Gritton goto done_errmsg; 762b38ff370SJamie Gritton } 7632b0d6f81SJamie Gritton ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 764b38ff370SJamie Gritton bcopy(op, ip4, ip4s * sizeof(*ip4)); 765b38ff370SJamie Gritton /* 7660304c731SJamie Gritton * IP addresses are all sorted but ip[0] to preserve 7670304c731SJamie Gritton * the primary IP address as given from userland. 7680304c731SJamie Gritton * This special IP is used for unbound outgoing 769bef916f9SBjoern A. Zeeb * connections as well for "loopback" traffic in case 770bef916f9SBjoern A. Zeeb * source address selection cannot find any more fitting 771bef916f9SBjoern A. Zeeb * address to connect from. 772b38ff370SJamie Gritton */ 773b38ff370SJamie Gritton if (ip4s > 1) 7740ce1624dSStephen J. Kiernan qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), 7750ce1624dSStephen J. Kiernan prison_qcmp_v4); 776b38ff370SJamie Gritton /* 7770304c731SJamie Gritton * Check for duplicate addresses and do some simple 7780304c731SJamie Gritton * zero and broadcast checks. If users give other bogus 7790304c731SJamie Gritton * addresses it is their problem. 780b38ff370SJamie Gritton * 7810304c731SJamie Gritton * We do not have to care about byte order for these 7820304c731SJamie Gritton * checks so we will do them in NBO. 783b38ff370SJamie Gritton */ 784b38ff370SJamie Gritton for (ii = 0; ii < ip4s; ii++) { 785b38ff370SJamie Gritton if (ip4[ii].s_addr == INADDR_ANY || 786b38ff370SJamie Gritton ip4[ii].s_addr == INADDR_BROADCAST) { 787b38ff370SJamie Gritton error = EINVAL; 788b38ff370SJamie Gritton goto done_free; 789b38ff370SJamie Gritton } 790b38ff370SJamie Gritton if ((ii+1) < ip4s && 791b38ff370SJamie Gritton (ip4[0].s_addr == ip4[ii+1].s_addr || 792b38ff370SJamie Gritton ip4[ii].s_addr == ip4[ii+1].s_addr)) { 793b38ff370SJamie Gritton error = EINVAL; 794b38ff370SJamie Gritton goto done_free; 795b38ff370SJamie Gritton } 796b38ff370SJamie Gritton } 797b38ff370SJamie Gritton } 7980304c731SJamie Gritton } 799b38ff370SJamie Gritton #endif 800b38ff370SJamie Gritton 801b38ff370SJamie Gritton #ifdef INET6 802b38ff370SJamie Gritton error = vfs_getopt(opts, "ip6.addr", &op, &ip6s); 803b38ff370SJamie Gritton if (error == ENOENT) 8040e5e396eSJamie Gritton ip6s = 0; 805b38ff370SJamie Gritton else if (error != 0) 806b38ff370SJamie Gritton goto done_free; 807b38ff370SJamie Gritton else if (ip6s & (sizeof(*ip6) - 1)) { 808b38ff370SJamie Gritton error = EINVAL; 809b38ff370SJamie Gritton goto done_free; 8100304c731SJamie Gritton } else { 8116a3f2779SJamie Gritton ch_flags |= PR_IP6_USER; 8126a3f2779SJamie Gritton pr_flags |= PR_IP6_USER; 8136a3f2779SJamie Gritton if (ip6s > 0) { 814b38ff370SJamie Gritton ip6s /= sizeof(*ip6); 815b38ff370SJamie Gritton if (ip6s > jail_max_af_ips) { 816b38ff370SJamie Gritton error = EINVAL; 817b38ff370SJamie Gritton vfs_opterror(opts, "too many IPv6 addresses"); 818b38ff370SJamie Gritton goto done_errmsg; 819b38ff370SJamie Gritton } 8202b0d6f81SJamie Gritton ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 821b38ff370SJamie Gritton bcopy(op, ip6, ip6s * sizeof(*ip6)); 822b38ff370SJamie Gritton if (ip6s > 1) 8230ce1624dSStephen J. Kiernan qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), 8240ce1624dSStephen J. Kiernan prison_qcmp_v6); 825b38ff370SJamie Gritton for (ii = 0; ii < ip6s; ii++) { 8260304c731SJamie Gritton if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) { 827b38ff370SJamie Gritton error = EINVAL; 828b38ff370SJamie Gritton goto done_free; 829b38ff370SJamie Gritton } 830b38ff370SJamie Gritton if ((ii+1) < ip6s && 831b38ff370SJamie Gritton (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) || 832b38ff370SJamie Gritton IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1]))) 833b38ff370SJamie Gritton { 834b38ff370SJamie Gritton error = EINVAL; 835b38ff370SJamie Gritton goto done_free; 836b38ff370SJamie Gritton } 837b38ff370SJamie Gritton } 838b38ff370SJamie Gritton } 8390304c731SJamie Gritton } 840b38ff370SJamie Gritton #endif 841b38ff370SJamie Gritton 842bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6)) 843bdfc8cc4SJamie Gritton if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 844bdfc8cc4SJamie Gritton error = EINVAL; 845bdfc8cc4SJamie Gritton vfs_opterror(opts, 846bdfc8cc4SJamie Gritton "vnet jails cannot have IP address restrictions"); 847bdfc8cc4SJamie Gritton goto done_errmsg; 848bdfc8cc4SJamie Gritton } 849bdfc8cc4SJamie Gritton #endif 850bdfc8cc4SJamie Gritton 851cf0313c6SJamie Gritton error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len); 852cf0313c6SJamie Gritton if (error == ENOENT) 853cf0313c6SJamie Gritton osrelstr = NULL; 854cf0313c6SJamie Gritton else if (error != 0) 855cf0313c6SJamie Gritton goto done_free; 856cf0313c6SJamie Gritton else { 857cf0313c6SJamie Gritton if (flags & JAIL_UPDATE) { 858cf0313c6SJamie Gritton error = EINVAL; 859cf0313c6SJamie Gritton vfs_opterror(opts, 860cf0313c6SJamie Gritton "osrelease cannot be changed after creation"); 861cf0313c6SJamie Gritton goto done_errmsg; 862cf0313c6SJamie Gritton } 863cf0313c6SJamie Gritton if (len == 0 || len >= OSRELEASELEN) { 864cf0313c6SJamie Gritton error = EINVAL; 865cf0313c6SJamie Gritton vfs_opterror(opts, 866cf0313c6SJamie Gritton "osrelease string must be 1-%d bytes long", 867cf0313c6SJamie Gritton OSRELEASELEN - 1); 868cf0313c6SJamie Gritton goto done_errmsg; 869cf0313c6SJamie Gritton } 870cf0313c6SJamie Gritton } 871cf0313c6SJamie Gritton 872cf0313c6SJamie Gritton error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt)); 873cf0313c6SJamie Gritton if (error == ENOENT) 874cf0313c6SJamie Gritton osreldt = 0; 875cf0313c6SJamie Gritton else if (error != 0) 876cf0313c6SJamie Gritton goto done_free; 877cf0313c6SJamie Gritton else { 878cf0313c6SJamie Gritton if (flags & JAIL_UPDATE) { 879cf0313c6SJamie Gritton error = EINVAL; 880cf0313c6SJamie Gritton vfs_opterror(opts, 881cf0313c6SJamie Gritton "osreldate cannot be changed after creation"); 882cf0313c6SJamie Gritton goto done_errmsg; 883cf0313c6SJamie Gritton } 884cf0313c6SJamie Gritton if (osreldt == 0) { 885cf0313c6SJamie Gritton error = EINVAL; 886cf0313c6SJamie Gritton vfs_opterror(opts, "osreldate cannot be 0"); 887cf0313c6SJamie Gritton goto done_errmsg; 888cf0313c6SJamie Gritton } 889cf0313c6SJamie Gritton } 890cf0313c6SJamie Gritton 8919cbe30e1SMartin Matuska fullpath_disabled = 0; 892b38ff370SJamie Gritton root = NULL; 893b38ff370SJamie Gritton error = vfs_getopt(opts, "path", (void **)&path, &len); 894b38ff370SJamie Gritton if (error == ENOENT) 895b38ff370SJamie Gritton path = NULL; 896b38ff370SJamie Gritton else if (error != 0) 897b38ff370SJamie Gritton goto done_free; 898b38ff370SJamie Gritton else { 899b38ff370SJamie Gritton if (flags & JAIL_UPDATE) { 900b38ff370SJamie Gritton error = EINVAL; 901b38ff370SJamie Gritton vfs_opterror(opts, 902b38ff370SJamie Gritton "path cannot be changed after creation"); 903b38ff370SJamie Gritton goto done_errmsg; 904b38ff370SJamie Gritton } 905b38ff370SJamie Gritton if (len == 0 || path[len - 1] != '\0') { 906b38ff370SJamie Gritton error = EINVAL; 907b38ff370SJamie Gritton goto done_free; 908b38ff370SJamie Gritton } 9095050aa86SKonstantin Belousov NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, 910b38ff370SJamie Gritton path, td); 911b38ff370SJamie Gritton error = namei(&nd); 912b38ff370SJamie Gritton if (error) 913b38ff370SJamie Gritton goto done_free; 914b38ff370SJamie Gritton root = nd.ni_vp; 915b38ff370SJamie Gritton NDFREE(&nd, NDF_ONLY_PNBUF); 9166dfe0a3dSMartin Matuska g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 9176dfe0a3dSMartin Matuska strlcpy(g_path, path, MAXPATHLEN); 9186dfe0a3dSMartin Matuska error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN); 9196dfe0a3dSMartin Matuska if (error == 0) 9206dfe0a3dSMartin Matuska path = g_path; 9216dfe0a3dSMartin Matuska else if (error == ENODEV) { 922f6e633a9SMartin Matuska /* proceed if sysctl debug.disablefullpath == 1 */ 923f6e633a9SMartin Matuska fullpath_disabled = 1; 924f6e633a9SMartin Matuska if (len < 2 || (len == 2 && path[0] == '/')) 925f6e633a9SMartin Matuska path = NULL; 9266dfe0a3dSMartin Matuska } else { 927f6e633a9SMartin Matuska /* exit on other errors */ 928b38ff370SJamie Gritton goto done_free; 929b38ff370SJamie Gritton } 930f6e633a9SMartin Matuska if (root->v_type != VDIR) { 931f6e633a9SMartin Matuska error = ENOTDIR; 932f6e633a9SMartin Matuska vput(root); 933f6e633a9SMartin Matuska goto done_free; 934f6e633a9SMartin Matuska } 935f6e633a9SMartin Matuska VOP_UNLOCK(root, 0); 936f6e633a9SMartin Matuska if (fullpath_disabled) { 937f6e633a9SMartin Matuska /* Leave room for a real-root full pathname. */ 938f6e633a9SMartin Matuska if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/") 939f6e633a9SMartin Matuska ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) { 940f6e633a9SMartin Matuska error = ENAMETOOLONG; 941932a6e43SJamie Gritton vrele(root); 942f6e633a9SMartin Matuska goto done_free; 943f6e633a9SMartin Matuska } 944b38ff370SJamie Gritton } 945b38ff370SJamie Gritton } 946b38ff370SJamie Gritton 947b38ff370SJamie Gritton /* 948b6f47c23SJamie Gritton * Find the specified jail, or at least its parent. 949b38ff370SJamie Gritton * This abuses the file error codes ENOENT and EEXIST. 950b38ff370SJamie Gritton */ 951b38ff370SJamie Gritton pr = NULL; 952b6f47c23SJamie Gritton ppr = mypr; 953babbbb9cSJamie Gritton if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) { 954babbbb9cSJamie Gritton namelc = strrchr(name, '.'); 955babbbb9cSJamie Gritton jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10); 956babbbb9cSJamie Gritton if (*p != '\0') 957babbbb9cSJamie Gritton jid = 0; 958babbbb9cSJamie Gritton } 959b6f47c23SJamie Gritton sx_xlock(&allprison_lock); 960b38ff370SJamie Gritton if (jid != 0) { 9610304c731SJamie Gritton /* 9620304c731SJamie Gritton * See if a requested jid already exists. There is an 9630304c731SJamie Gritton * information leak here if the jid exists but is not within 9640304c731SJamie Gritton * the caller's jail hierarchy. Jail creators will get EEXIST 9650304c731SJamie Gritton * even though they cannot see the jail, and CREATE | UPDATE 9660304c731SJamie Gritton * will return ENOENT which is not normally a valid error. 9670304c731SJamie Gritton */ 968b38ff370SJamie Gritton if (jid < 0) { 969b38ff370SJamie Gritton error = EINVAL; 970b38ff370SJamie Gritton vfs_opterror(opts, "negative jid"); 971b38ff370SJamie Gritton goto done_unlock_list; 972b38ff370SJamie Gritton } 973b38ff370SJamie Gritton pr = prison_find(jid); 974b38ff370SJamie Gritton if (pr != NULL) { 9750304c731SJamie Gritton ppr = pr->pr_parent; 976b38ff370SJamie Gritton /* Create: jid must not exist. */ 977b38ff370SJamie Gritton if (cuflags == JAIL_CREATE) { 978b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 979b38ff370SJamie Gritton error = EEXIST; 980b38ff370SJamie Gritton vfs_opterror(opts, "jail %d already exists", 981b38ff370SJamie Gritton jid); 982b38ff370SJamie Gritton goto done_unlock_list; 983b38ff370SJamie Gritton } 9840304c731SJamie Gritton if (!prison_ischild(mypr, pr)) { 9850304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 9860304c731SJamie Gritton pr = NULL; 9870304c731SJamie Gritton } else if (pr->pr_uref == 0) { 988b38ff370SJamie Gritton if (!(flags & JAIL_DYING)) { 989b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 990b38ff370SJamie Gritton error = ENOENT; 991b38ff370SJamie Gritton vfs_opterror(opts, "jail %d is dying", 992b38ff370SJamie Gritton jid); 993b38ff370SJamie Gritton goto done_unlock_list; 994b38ff370SJamie Gritton } else if ((flags & JAIL_ATTACH) || 995b38ff370SJamie Gritton (pr_flags & PR_PERSIST)) { 996b38ff370SJamie Gritton /* 997b38ff370SJamie Gritton * A dying jail might be resurrected 998b38ff370SJamie Gritton * (via attach or persist), but first 999b38ff370SJamie Gritton * it must determine if another jail 1000b38ff370SJamie Gritton * has claimed its name. Accomplish 1001b38ff370SJamie Gritton * this by implicitly re-setting the 1002b38ff370SJamie Gritton * name. 1003b38ff370SJamie Gritton */ 1004b38ff370SJamie Gritton if (name == NULL) 10050304c731SJamie Gritton name = prison_name(mypr, pr); 1006b38ff370SJamie Gritton } 1007b38ff370SJamie Gritton } 1008b38ff370SJamie Gritton } 1009b38ff370SJamie Gritton if (pr == NULL) { 1010b38ff370SJamie Gritton /* Update: jid must exist. */ 1011b38ff370SJamie Gritton if (cuflags == JAIL_UPDATE) { 1012b38ff370SJamie Gritton error = ENOENT; 1013b38ff370SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 1014b38ff370SJamie Gritton goto done_unlock_list; 1015b38ff370SJamie Gritton } 1016b38ff370SJamie Gritton } 1017b38ff370SJamie Gritton } 1018b38ff370SJamie Gritton /* 1019b38ff370SJamie Gritton * If the caller provided a name, look for a jail by that name. 1020b38ff370SJamie Gritton * This has different semantics for creates and updates keyed by jid 1021b38ff370SJamie Gritton * (where the name must not already exist in a different jail), 1022b38ff370SJamie Gritton * and updates keyed by the name itself (where the name must exist 1023b38ff370SJamie Gritton * because that is the jail being updated). 1024b38ff370SJamie Gritton */ 1025b6f47c23SJamie Gritton namelc = NULL; 1026b38ff370SJamie Gritton if (name != NULL) { 1027babbbb9cSJamie Gritton namelc = strrchr(name, '.'); 1028babbbb9cSJamie Gritton if (namelc == NULL) 1029babbbb9cSJamie Gritton namelc = name; 1030babbbb9cSJamie Gritton else { 10310304c731SJamie Gritton /* 10320304c731SJamie Gritton * This is a hierarchical name. Split it into the 10330304c731SJamie Gritton * parent and child names, and make sure the parent 10340304c731SJamie Gritton * exists or matches an already found jail. 10350304c731SJamie Gritton */ 10360304c731SJamie Gritton if (pr != NULL) { 1037babbbb9cSJamie Gritton if (strncmp(name, ppr->pr_name, namelc - name) 1038babbbb9cSJamie Gritton || ppr->pr_name[namelc - name] != '\0') { 10390304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 10400304c731SJamie Gritton error = EINVAL; 10410304c731SJamie Gritton vfs_opterror(opts, 10420304c731SJamie Gritton "cannot change jail's parent"); 10430304c731SJamie Gritton goto done_unlock_list; 10440304c731SJamie Gritton } 10450304c731SJamie Gritton } else { 1046b6f47c23SJamie Gritton *namelc = '\0'; 10470304c731SJamie Gritton ppr = prison_find_name(mypr, name); 10480304c731SJamie Gritton if (ppr == NULL) { 10490304c731SJamie Gritton error = ENOENT; 10500304c731SJamie Gritton vfs_opterror(opts, 10510304c731SJamie Gritton "jail \"%s\" not found", name); 10520304c731SJamie Gritton goto done_unlock_list; 10530304c731SJamie Gritton } 10540304c731SJamie Gritton mtx_unlock(&ppr->pr_mtx); 1055b6f47c23SJamie Gritton *namelc = '.'; 10560304c731SJamie Gritton } 1057b6f47c23SJamie Gritton namelc++; 10580304c731SJamie Gritton } 1059b6f47c23SJamie Gritton if (namelc[0] != '\0') { 1060b6f47c23SJamie Gritton pnamelen = 10610304c731SJamie Gritton (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 1062b38ff370SJamie Gritton name_again: 10630304c731SJamie Gritton deadpr = NULL; 10640304c731SJamie Gritton FOREACH_PRISON_CHILD(ppr, tpr) { 1065b38ff370SJamie Gritton if (tpr != pr && tpr->pr_ref > 0 && 1066b6f47c23SJamie Gritton !strcmp(tpr->pr_name + pnamelen, namelc)) { 1067b38ff370SJamie Gritton if (pr == NULL && 1068b38ff370SJamie Gritton cuflags != JAIL_CREATE) { 1069b38ff370SJamie Gritton mtx_lock(&tpr->pr_mtx); 1070b38ff370SJamie Gritton if (tpr->pr_ref > 0) { 1071b38ff370SJamie Gritton /* 1072b38ff370SJamie Gritton * Use this jail 1073b38ff370SJamie Gritton * for updates. 1074b38ff370SJamie Gritton */ 1075b38ff370SJamie Gritton if (tpr->pr_uref > 0) { 1076b38ff370SJamie Gritton pr = tpr; 1077b38ff370SJamie Gritton break; 1078b38ff370SJamie Gritton } 1079b38ff370SJamie Gritton deadpr = tpr; 1080b38ff370SJamie Gritton } 1081b38ff370SJamie Gritton mtx_unlock(&tpr->pr_mtx); 1082b38ff370SJamie Gritton } else if (tpr->pr_uref > 0) { 1083b38ff370SJamie Gritton /* 1084b38ff370SJamie Gritton * Create, or update(jid): 1085b38ff370SJamie Gritton * name must not exist in an 10860304c731SJamie Gritton * active sibling jail. 1087b38ff370SJamie Gritton */ 1088b38ff370SJamie Gritton error = EEXIST; 1089b38ff370SJamie Gritton if (pr != NULL) 1090b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1091b38ff370SJamie Gritton vfs_opterror(opts, 1092b38ff370SJamie Gritton "jail \"%s\" already exists", 1093b38ff370SJamie Gritton name); 1094b38ff370SJamie Gritton goto done_unlock_list; 1095b38ff370SJamie Gritton } 1096b38ff370SJamie Gritton } 1097b38ff370SJamie Gritton } 1098b38ff370SJamie Gritton /* If no active jail is found, use a dying one. */ 1099b38ff370SJamie Gritton if (deadpr != NULL && pr == NULL) { 1100b38ff370SJamie Gritton if (flags & JAIL_DYING) { 1101b38ff370SJamie Gritton mtx_lock(&deadpr->pr_mtx); 1102b38ff370SJamie Gritton if (deadpr->pr_ref == 0) { 1103b38ff370SJamie Gritton mtx_unlock(&deadpr->pr_mtx); 1104b38ff370SJamie Gritton goto name_again; 1105b38ff370SJamie Gritton } 1106b38ff370SJamie Gritton pr = deadpr; 1107b38ff370SJamie Gritton } else if (cuflags == JAIL_UPDATE) { 1108b38ff370SJamie Gritton error = ENOENT; 1109b38ff370SJamie Gritton vfs_opterror(opts, 1110b38ff370SJamie Gritton "jail \"%s\" is dying", name); 1111b38ff370SJamie Gritton goto done_unlock_list; 1112b38ff370SJamie Gritton } 1113b38ff370SJamie Gritton } 1114b38ff370SJamie Gritton /* Update: name must exist if no jid. */ 1115b38ff370SJamie Gritton else if (cuflags == JAIL_UPDATE && pr == NULL) { 1116b38ff370SJamie Gritton error = ENOENT; 1117b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", 1118b38ff370SJamie Gritton name); 1119b38ff370SJamie Gritton goto done_unlock_list; 1120b38ff370SJamie Gritton } 1121b38ff370SJamie Gritton } 1122b38ff370SJamie Gritton } 1123b38ff370SJamie Gritton /* Update: must provide a jid or name. */ 1124b38ff370SJamie Gritton else if (cuflags == JAIL_UPDATE && pr == NULL) { 1125b38ff370SJamie Gritton error = ENOENT; 1126b38ff370SJamie Gritton vfs_opterror(opts, "update specified no jail"); 1127b38ff370SJamie Gritton goto done_unlock_list; 1128b38ff370SJamie Gritton } 1129b38ff370SJamie Gritton 1130b38ff370SJamie Gritton /* If there's no prison to update, create a new one and link it in. */ 1131b38ff370SJamie Gritton if (pr == NULL) { 1132b97457e2SJamie Gritton for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent) 1133b97457e2SJamie Gritton if (tpr->pr_childcount >= tpr->pr_childmax) { 1134b97457e2SJamie Gritton error = EPERM; 1135b97457e2SJamie Gritton vfs_opterror(opts, "prison limit exceeded"); 1136b97457e2SJamie Gritton goto done_unlock_list; 1137b97457e2SJamie Gritton } 1138b38ff370SJamie Gritton created = 1; 11390304c731SJamie Gritton mtx_lock(&ppr->pr_mtx); 11402a549507SJamie Gritton if (ppr->pr_ref == 0) { 11410304c731SJamie Gritton mtx_unlock(&ppr->pr_mtx); 11420304c731SJamie Gritton error = ENOENT; 1143b6f47c23SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", 1144b6f47c23SJamie Gritton prison_name(mypr, ppr)); 11450304c731SJamie Gritton goto done_unlock_list; 11460304c731SJamie Gritton } 11470304c731SJamie Gritton ppr->pr_ref++; 11480304c731SJamie Gritton ppr->pr_uref++; 11490304c731SJamie Gritton mtx_unlock(&ppr->pr_mtx); 1150b38ff370SJamie Gritton pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO); 1151b38ff370SJamie Gritton if (jid == 0) { 1152b38ff370SJamie Gritton /* Find the next free jid. */ 1153b38ff370SJamie Gritton jid = lastprid + 1; 1154b38ff370SJamie Gritton findnext: 1155b38ff370SJamie Gritton if (jid == JAIL_MAX) 1156b38ff370SJamie Gritton jid = 1; 1157b38ff370SJamie Gritton TAILQ_FOREACH(tpr, &allprison, pr_list) { 1158b38ff370SJamie Gritton if (tpr->pr_id < jid) 1159b38ff370SJamie Gritton continue; 1160b38ff370SJamie Gritton if (tpr->pr_id > jid || tpr->pr_ref == 0) { 1161b38ff370SJamie Gritton TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1162b38ff370SJamie Gritton break; 1163b38ff370SJamie Gritton } 1164b38ff370SJamie Gritton if (jid == lastprid) { 1165b38ff370SJamie Gritton error = EAGAIN; 1166b38ff370SJamie Gritton vfs_opterror(opts, 1167b38ff370SJamie Gritton "no available jail IDs"); 1168b38ff370SJamie Gritton free(pr, M_PRISON); 11690304c731SJamie Gritton prison_deref(ppr, PD_DEREF | 11700304c731SJamie Gritton PD_DEUREF | PD_LIST_XLOCKED); 11710304c731SJamie Gritton goto done_releroot; 1172b38ff370SJamie Gritton } 1173b38ff370SJamie Gritton jid++; 1174b38ff370SJamie Gritton goto findnext; 1175b38ff370SJamie Gritton } 1176b38ff370SJamie Gritton lastprid = jid; 1177b38ff370SJamie Gritton } else { 1178b38ff370SJamie Gritton /* 1179b38ff370SJamie Gritton * The jail already has a jid (that did not yet exist), 1180b38ff370SJamie Gritton * so just find where to insert it. 1181b38ff370SJamie Gritton */ 1182b38ff370SJamie Gritton TAILQ_FOREACH(tpr, &allprison, pr_list) 1183b38ff370SJamie Gritton if (tpr->pr_id >= jid) { 1184b38ff370SJamie Gritton TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1185b38ff370SJamie Gritton break; 1186b38ff370SJamie Gritton } 1187b38ff370SJamie Gritton } 1188b38ff370SJamie Gritton if (tpr == NULL) 1189b38ff370SJamie Gritton TAILQ_INSERT_TAIL(&allprison, pr, pr_list); 11900304c731SJamie Gritton LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling); 11910304c731SJamie Gritton for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 1192b97457e2SJamie Gritton tpr->pr_childcount++; 1193b38ff370SJamie Gritton 11940304c731SJamie Gritton pr->pr_parent = ppr; 1195b38ff370SJamie Gritton pr->pr_id = jid; 11960304c731SJamie Gritton 11970304c731SJamie Gritton /* Set some default values, and inherit some from the parent. */ 1198b6f47c23SJamie Gritton if (namelc == NULL) 1199b6f47c23SJamie Gritton namelc = ""; 1200b38ff370SJamie Gritton if (path == NULL) { 1201b38ff370SJamie Gritton path = "/"; 12020304c731SJamie Gritton root = mypr->pr_root; 1203b38ff370SJamie Gritton vref(root); 1204b38ff370SJamie Gritton } 12058986e3a0SJamie Gritton strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN); 12068986e3a0SJamie Gritton pr->pr_flags |= PR_HOST; 1207bdfc8cc4SJamie Gritton #if defined(INET) || defined(INET6) 1208bdfc8cc4SJamie Gritton #ifdef VIMAGE 1209bdfc8cc4SJamie Gritton if (!(pr_flags & PR_VNET)) 1210bdfc8cc4SJamie Gritton #endif 1211bdfc8cc4SJamie Gritton { 12120304c731SJamie Gritton #ifdef INET 12132b0d6f81SJamie Gritton if (!(ch_flags & PR_IP4_USER)) 12146a3f2779SJamie Gritton pr->pr_flags |= PR_IP4 | PR_IP4_USER; 12152b0d6f81SJamie Gritton else if (!(pr_flags & PR_IP4_USER)) { 12162b0d6f81SJamie Gritton pr->pr_flags |= ppr->pr_flags & PR_IP4; 12172b0d6f81SJamie Gritton if (ppr->pr_ip4 != NULL) { 12182b0d6f81SJamie Gritton pr->pr_ip4s = ppr->pr_ip4s; 12192b0d6f81SJamie Gritton pr->pr_ip4 = malloc(pr->pr_ip4s * 12202b0d6f81SJamie Gritton sizeof(struct in_addr), M_PRISON, 12212b0d6f81SJamie Gritton M_WAITOK); 12222b0d6f81SJamie Gritton bcopy(ppr->pr_ip4, pr->pr_ip4, 12232b0d6f81SJamie Gritton pr->pr_ip4s * sizeof(*pr->pr_ip4)); 12242b0d6f81SJamie Gritton } 12252b0d6f81SJamie Gritton } 12260304c731SJamie Gritton #endif 12270304c731SJamie Gritton #ifdef INET6 12282b0d6f81SJamie Gritton if (!(ch_flags & PR_IP6_USER)) 12296a3f2779SJamie Gritton pr->pr_flags |= PR_IP6 | PR_IP6_USER; 12302b0d6f81SJamie Gritton else if (!(pr_flags & PR_IP6_USER)) { 12312b0d6f81SJamie Gritton pr->pr_flags |= ppr->pr_flags & PR_IP6; 12322b0d6f81SJamie Gritton if (ppr->pr_ip6 != NULL) { 12332b0d6f81SJamie Gritton pr->pr_ip6s = ppr->pr_ip6s; 12342b0d6f81SJamie Gritton pr->pr_ip6 = malloc(pr->pr_ip6s * 12352b0d6f81SJamie Gritton sizeof(struct in6_addr), M_PRISON, 12362b0d6f81SJamie Gritton M_WAITOK); 12372b0d6f81SJamie Gritton bcopy(ppr->pr_ip6, pr->pr_ip6, 12382b0d6f81SJamie Gritton pr->pr_ip6s * sizeof(*pr->pr_ip6)); 12392b0d6f81SJamie Gritton } 12402b0d6f81SJamie Gritton } 12410304c731SJamie Gritton #endif 1242bdfc8cc4SJamie Gritton } 1243bdfc8cc4SJamie Gritton #endif 1244592bcae8SBjoern A. Zeeb /* Source address selection is always on by default. */ 1245592bcae8SBjoern A. Zeeb pr->pr_flags |= _PR_IP_SADDRSEL; 1246592bcae8SBjoern A. Zeeb 12470304c731SJamie Gritton pr->pr_securelevel = ppr->pr_securelevel; 12480304c731SJamie Gritton pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow; 1249af10bf05SBjoern A. Zeeb pr->pr_enforce_statfs = jail_default_enforce_statfs; 1250bf3db8aaSMartin Matuska pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum; 1251b38ff370SJamie Gritton 1252b96bd95bSIan Lepore pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate; 1253b96bd95bSIan Lepore if (osrelstr == NULL) 1254b96bd95bSIan Lepore strcpy(pr->pr_osrelease, ppr->pr_osrelease); 1255b96bd95bSIan Lepore else 1256b96bd95bSIan Lepore strcpy(pr->pr_osrelease, osrelstr); 1257b96bd95bSIan Lepore 12580304c731SJamie Gritton LIST_INIT(&pr->pr_children); 12590304c731SJamie Gritton mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK); 126073d9e52dSJamie Gritton TASK_INIT(&pr->pr_task, 0, prison_complete, pr); 1261b38ff370SJamie Gritton 1262679e1390SJamie Gritton #ifdef VIMAGE 1263679e1390SJamie Gritton /* Allocate a new vnet if specified. */ 1264679e1390SJamie Gritton pr->pr_vnet = (pr_flags & PR_VNET) 1265679e1390SJamie Gritton ? vnet_alloc() : ppr->pr_vnet; 1266679e1390SJamie Gritton #endif 1267b38ff370SJamie Gritton /* 1268b38ff370SJamie Gritton * Allocate a dedicated cpuset for each jail. 1269b38ff370SJamie Gritton * Unlike other initial settings, this may return an erorr. 1270b38ff370SJamie Gritton */ 12710304c731SJamie Gritton error = cpuset_create_root(ppr, &pr->pr_cpuset); 1272b38ff370SJamie Gritton if (error) { 1273b38ff370SJamie Gritton prison_deref(pr, PD_LIST_XLOCKED); 1274b38ff370SJamie Gritton goto done_releroot; 1275b38ff370SJamie Gritton } 1276b38ff370SJamie Gritton 1277b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1278b38ff370SJamie Gritton /* 1279b38ff370SJamie Gritton * New prisons do not yet have a reference, because we do not 1280b6f47c23SJamie Gritton * want others to see the incomplete prison once the 1281b38ff370SJamie Gritton * allprison_lock is downgraded. 1282b38ff370SJamie Gritton */ 1283b38ff370SJamie Gritton } else { 1284b38ff370SJamie Gritton created = 0; 12852b0d6f81SJamie Gritton /* 12862b0d6f81SJamie Gritton * Grab a reference for existing prisons, to ensure they 12872b0d6f81SJamie Gritton * continue to exist for the duration of the call. 12882b0d6f81SJamie Gritton */ 12892b0d6f81SJamie Gritton pr->pr_ref++; 1290bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6)) 1291bdfc8cc4SJamie Gritton if ((pr->pr_flags & PR_VNET) && 1292bdfc8cc4SJamie Gritton (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 1293bdfc8cc4SJamie Gritton error = EINVAL; 1294bdfc8cc4SJamie Gritton vfs_opterror(opts, 1295bdfc8cc4SJamie Gritton "vnet jails cannot have IP address restrictions"); 1296bdfc8cc4SJamie Gritton goto done_deref_locked; 1297bdfc8cc4SJamie Gritton } 1298bdfc8cc4SJamie Gritton #endif 12992b0d6f81SJamie Gritton #ifdef INET 13002b0d6f81SJamie Gritton if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 13012b0d6f81SJamie Gritton error = EINVAL; 13022b0d6f81SJamie Gritton vfs_opterror(opts, 13032b0d6f81SJamie Gritton "ip4 cannot be changed after creation"); 13042b0d6f81SJamie Gritton goto done_deref_locked; 13052b0d6f81SJamie Gritton } 13062b0d6f81SJamie Gritton #endif 13072b0d6f81SJamie Gritton #ifdef INET6 13082b0d6f81SJamie Gritton if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 13092b0d6f81SJamie Gritton error = EINVAL; 13102b0d6f81SJamie Gritton vfs_opterror(opts, 13112b0d6f81SJamie Gritton "ip6 cannot be changed after creation"); 13122b0d6f81SJamie Gritton goto done_deref_locked; 13132b0d6f81SJamie Gritton } 13142b0d6f81SJamie Gritton #endif 1315b38ff370SJamie Gritton } 1316b38ff370SJamie Gritton 1317b38ff370SJamie Gritton /* Do final error checking before setting anything. */ 13180304c731SJamie Gritton if (gotslevel) { 13190304c731SJamie Gritton if (slevel < ppr->pr_securelevel) { 13200304c731SJamie Gritton error = EPERM; 13210304c731SJamie Gritton goto done_deref_locked; 13220304c731SJamie Gritton } 13230304c731SJamie Gritton } 1324b97457e2SJamie Gritton if (gotchildmax) { 1325b97457e2SJamie Gritton if (childmax >= ppr->pr_childmax) { 1326b97457e2SJamie Gritton error = EPERM; 1327b97457e2SJamie Gritton goto done_deref_locked; 1328b97457e2SJamie Gritton } 1329b97457e2SJamie Gritton } 13300304c731SJamie Gritton if (gotenforce) { 13310304c731SJamie Gritton if (enforce < ppr->pr_enforce_statfs) { 13320304c731SJamie Gritton error = EPERM; 13330304c731SJamie Gritton goto done_deref_locked; 13340304c731SJamie Gritton } 13350304c731SJamie Gritton } 13360cc207a6SMartin Matuska if (gotrsnum) { 13370cc207a6SMartin Matuska /* 13380cc207a6SMartin Matuska * devfs_rsnum is a uint16_t 13390cc207a6SMartin Matuska */ 1340bf3db8aaSMartin Matuska if (rsnum < 0 || rsnum > 65535) { 13410cc207a6SMartin Matuska error = EINVAL; 13420cc207a6SMartin Matuska goto done_deref_locked; 13430cc207a6SMartin Matuska } 13440cc207a6SMartin Matuska /* 1345bf3db8aaSMartin Matuska * Nested jails always inherit parent's devfs ruleset 13460cc207a6SMartin Matuska */ 13470cc207a6SMartin Matuska if (jailed(td->td_ucred)) { 13480cc207a6SMartin Matuska if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) { 13490cc207a6SMartin Matuska error = EPERM; 13500cc207a6SMartin Matuska goto done_deref_locked; 1351bf3db8aaSMartin Matuska } else 13520cc207a6SMartin Matuska rsnum = ppr->pr_devfs_rsnum; 13530cc207a6SMartin Matuska } 13540cc207a6SMartin Matuska } 1355b38ff370SJamie Gritton #ifdef INET 13562b0d6f81SJamie Gritton if (ip4s > 0) { 13570304c731SJamie Gritton if (ppr->pr_flags & PR_IP4) { 13580304c731SJamie Gritton /* 13590304c731SJamie Gritton * Make sure the new set of IP addresses is a 13600304c731SJamie Gritton * subset of the parent's list. Don't worry 13610304c731SJamie Gritton * about the parent being unlocked, as any 13620304c731SJamie Gritton * setting is done with allprison_lock held. 13630304c731SJamie Gritton */ 13640304c731SJamie Gritton for (ij = 0; ij < ppr->pr_ip4s; ij++) 13652b0d6f81SJamie Gritton if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 13660304c731SJamie Gritton break; 13670304c731SJamie Gritton if (ij == ppr->pr_ip4s) { 13680304c731SJamie Gritton error = EPERM; 13690304c731SJamie Gritton goto done_deref_locked; 13700304c731SJamie Gritton } 13710304c731SJamie Gritton if (ip4s > 1) { 13720304c731SJamie Gritton for (ii = ij = 1; ii < ip4s; ii++) { 13730304c731SJamie Gritton if (ip4[ii].s_addr == 13740304c731SJamie Gritton ppr->pr_ip4[0].s_addr) 1375b38ff370SJamie Gritton continue; 13760304c731SJamie Gritton for (; ij < ppr->pr_ip4s; ij++) 13770304c731SJamie Gritton if (ip4[ii].s_addr == 13780304c731SJamie Gritton ppr->pr_ip4[ij].s_addr) 13790304c731SJamie Gritton break; 13800304c731SJamie Gritton if (ij == ppr->pr_ip4s) 13810304c731SJamie Gritton break; 13820304c731SJamie Gritton } 13830304c731SJamie Gritton if (ij == ppr->pr_ip4s) { 13840304c731SJamie Gritton error = EPERM; 13850304c731SJamie Gritton goto done_deref_locked; 13860304c731SJamie Gritton } 13870304c731SJamie Gritton } 13880304c731SJamie Gritton } 13890304c731SJamie Gritton /* 13900304c731SJamie Gritton * Check for conflicting IP addresses. We permit them 13910304c731SJamie Gritton * if there is no more than one IP on each jail. If 13920304c731SJamie Gritton * there is a duplicate on a jail with more than one 13930304c731SJamie Gritton * IP stop checking and return error. 13940304c731SJamie Gritton */ 1395bdfc8cc4SJamie Gritton tppr = ppr; 1396bdfc8cc4SJamie Gritton #ifdef VIMAGE 1397bdfc8cc4SJamie Gritton for (; tppr != &prison0; tppr = tppr->pr_parent) 1398bdfc8cc4SJamie Gritton if (tppr->pr_flags & PR_VNET) 1399bdfc8cc4SJamie Gritton break; 1400bdfc8cc4SJamie Gritton #endif 1401bdfc8cc4SJamie Gritton FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1402bdfc8cc4SJamie Gritton if (tpr == pr || 1403bdfc8cc4SJamie Gritton #ifdef VIMAGE 14042b0d6f81SJamie Gritton (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1405bdfc8cc4SJamie Gritton #endif 1406bdfc8cc4SJamie Gritton tpr->pr_uref == 0) { 14070304c731SJamie Gritton descend = 0; 14080304c731SJamie Gritton continue; 14090304c731SJamie Gritton } 14100304c731SJamie Gritton if (!(tpr->pr_flags & PR_IP4_USER)) 14110304c731SJamie Gritton continue; 14120304c731SJamie Gritton descend = 0; 14130304c731SJamie Gritton if (tpr->pr_ip4 == NULL || 14140304c731SJamie Gritton (ip4s == 1 && tpr->pr_ip4s == 1)) 14150304c731SJamie Gritton continue; 14160304c731SJamie Gritton for (ii = 0; ii < ip4s; ii++) { 14170ce1624dSStephen J. Kiernan if (prison_check_ip4_locked(tpr, &ip4[ii]) == 14180ce1624dSStephen J. Kiernan 0) { 14190304c731SJamie Gritton error = EADDRINUSE; 1420b38ff370SJamie Gritton vfs_opterror(opts, 1421b38ff370SJamie Gritton "IPv4 addresses clash"); 1422b38ff370SJamie Gritton goto done_deref_locked; 1423b38ff370SJamie Gritton } 14240304c731SJamie Gritton } 14250304c731SJamie Gritton } 14260304c731SJamie Gritton } 1427b38ff370SJamie Gritton #endif 1428b38ff370SJamie Gritton #ifdef INET6 14292b0d6f81SJamie Gritton if (ip6s > 0) { 14300304c731SJamie Gritton if (ppr->pr_flags & PR_IP6) { 14310304c731SJamie Gritton /* 14320304c731SJamie Gritton * Make sure the new set of IP addresses is a 14330304c731SJamie Gritton * subset of the parent's list. 14340304c731SJamie Gritton */ 14350304c731SJamie Gritton for (ij = 0; ij < ppr->pr_ip6s; ij++) 14360304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL(&ip6[0], 14370304c731SJamie Gritton &ppr->pr_ip6[ij])) 14380304c731SJamie Gritton break; 14390304c731SJamie Gritton if (ij == ppr->pr_ip6s) { 14400304c731SJamie Gritton error = EPERM; 14410304c731SJamie Gritton goto done_deref_locked; 14420304c731SJamie Gritton } 14430304c731SJamie Gritton if (ip6s > 1) { 14440304c731SJamie Gritton for (ii = ij = 1; ii < ip6s; ii++) { 14450304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL(&ip6[ii], 14460304c731SJamie Gritton &ppr->pr_ip6[0])) 14470304c731SJamie Gritton continue; 14480304c731SJamie Gritton for (; ij < ppr->pr_ip6s; ij++) 14490304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL( 14502b0d6f81SJamie Gritton &ip6[ii], &ppr->pr_ip6[ij])) 14510304c731SJamie Gritton break; 14520304c731SJamie Gritton if (ij == ppr->pr_ip6s) 14530304c731SJamie Gritton break; 14540304c731SJamie Gritton } 14550304c731SJamie Gritton if (ij == ppr->pr_ip6s) { 14560304c731SJamie Gritton error = EPERM; 14570304c731SJamie Gritton goto done_deref_locked; 14580304c731SJamie Gritton } 14590304c731SJamie Gritton } 14600304c731SJamie Gritton } 14610304c731SJamie Gritton /* Check for conflicting IP addresses. */ 1462bdfc8cc4SJamie Gritton tppr = ppr; 1463bdfc8cc4SJamie Gritton #ifdef VIMAGE 1464bdfc8cc4SJamie Gritton for (; tppr != &prison0; tppr = tppr->pr_parent) 1465bdfc8cc4SJamie Gritton if (tppr->pr_flags & PR_VNET) 1466bdfc8cc4SJamie Gritton break; 1467bdfc8cc4SJamie Gritton #endif 1468bdfc8cc4SJamie Gritton FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1469bdfc8cc4SJamie Gritton if (tpr == pr || 1470bdfc8cc4SJamie Gritton #ifdef VIMAGE 14712b0d6f81SJamie Gritton (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1472bdfc8cc4SJamie Gritton #endif 1473bdfc8cc4SJamie Gritton tpr->pr_uref == 0) { 14740304c731SJamie Gritton descend = 0; 14750304c731SJamie Gritton continue; 14760304c731SJamie Gritton } 14770304c731SJamie Gritton if (!(tpr->pr_flags & PR_IP6_USER)) 14780304c731SJamie Gritton continue; 14790304c731SJamie Gritton descend = 0; 14800304c731SJamie Gritton if (tpr->pr_ip6 == NULL || 14810304c731SJamie Gritton (ip6s == 1 && tpr->pr_ip6s == 1)) 14820304c731SJamie Gritton continue; 14830304c731SJamie Gritton for (ii = 0; ii < ip6s; ii++) { 14840ce1624dSStephen J. Kiernan if (prison_check_ip6_locked(tpr, &ip6[ii]) == 14850ce1624dSStephen J. Kiernan 0) { 14860304c731SJamie Gritton error = EADDRINUSE; 1487b38ff370SJamie Gritton vfs_opterror(opts, 1488b38ff370SJamie Gritton "IPv6 addresses clash"); 1489b38ff370SJamie Gritton goto done_deref_locked; 1490b38ff370SJamie Gritton } 14910304c731SJamie Gritton } 14920304c731SJamie Gritton } 14930304c731SJamie Gritton } 1494b38ff370SJamie Gritton #endif 14950304c731SJamie Gritton onamelen = namelen = 0; 1496b6f47c23SJamie Gritton if (namelc != NULL) { 14976ab6058eSJamie Gritton /* Give a default name of the jid. Also allow the name to be 14986ab6058eSJamie Gritton * explicitly the jid - but not any other number, and only in 14996ab6058eSJamie Gritton * normal form (no leading zero/etc). 15006ab6058eSJamie Gritton */ 1501b6f47c23SJamie Gritton if (namelc[0] == '\0') 1502b6f47c23SJamie Gritton snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid); 15036ab6058eSJamie Gritton else if ((strtoul(namelc, &p, 10) != jid || 15046ab6058eSJamie Gritton namelc[0] < '1' || namelc[0] > '9') && *p == '\0') { 1505b38ff370SJamie Gritton error = EINVAL; 1506babbbb9cSJamie Gritton vfs_opterror(opts, 1507babbbb9cSJamie Gritton "name cannot be numeric (unless it is the jid)"); 15080304c731SJamie Gritton goto done_deref_locked; 1509b38ff370SJamie Gritton } 1510b38ff370SJamie Gritton /* 15110304c731SJamie Gritton * Make sure the name isn't too long for the prison or its 15120304c731SJamie Gritton * children. 1513b38ff370SJamie Gritton */ 1514b6f47c23SJamie Gritton pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 1515b6f47c23SJamie Gritton onamelen = strlen(pr->pr_name + pnamelen); 1516b6f47c23SJamie Gritton namelen = strlen(namelc); 1517b6f47c23SJamie Gritton if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) { 15180304c731SJamie Gritton error = ENAMETOOLONG; 15190304c731SJamie Gritton goto done_deref_locked; 15200304c731SJamie Gritton } 15210304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 15220304c731SJamie Gritton if (strlen(tpr->pr_name) + (namelen - onamelen) >= 15230304c731SJamie Gritton sizeof(pr->pr_name)) { 15240304c731SJamie Gritton error = ENAMETOOLONG; 15250304c731SJamie Gritton goto done_deref_locked; 15260304c731SJamie Gritton } 15270304c731SJamie Gritton } 15280304c731SJamie Gritton } 15290304c731SJamie Gritton if (pr_allow & ~ppr->pr_allow) { 15300304c731SJamie Gritton error = EPERM; 15310304c731SJamie Gritton goto done_deref_locked; 1532b38ff370SJamie Gritton } 1533b38ff370SJamie Gritton 1534b6f47c23SJamie Gritton /* 1535b6f47c23SJamie Gritton * Let modules check their parameters. This requires unlocking and 1536b6f47c23SJamie Gritton * then re-locking the prison, but this is still a valid state as long 1537b6f47c23SJamie Gritton * as allprison_lock remains xlocked. 1538b6f47c23SJamie Gritton */ 1539b6f47c23SJamie Gritton mtx_unlock(&pr->pr_mtx); 1540b6f47c23SJamie Gritton error = osd_jail_call(pr, PR_METHOD_CHECK, opts); 1541b6f47c23SJamie Gritton if (error != 0) { 1542b6f47c23SJamie Gritton prison_deref(pr, created 1543b6f47c23SJamie Gritton ? PD_LIST_XLOCKED 1544b6f47c23SJamie Gritton : PD_DEREF | PD_LIST_XLOCKED); 1545b6f47c23SJamie Gritton goto done_releroot; 1546b6f47c23SJamie Gritton } 1547b6f47c23SJamie Gritton mtx_lock(&pr->pr_mtx); 1548b6f47c23SJamie Gritton 1549b6f47c23SJamie Gritton /* At this point, all valid parameters should have been noted. */ 1550b6f47c23SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 1551b6f47c23SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 1552b6f47c23SJamie Gritton error = EINVAL; 1553b6f47c23SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 1554b6f47c23SJamie Gritton goto done_deref_locked; 1555b6f47c23SJamie Gritton } 1556b6f47c23SJamie Gritton } 1557b6f47c23SJamie Gritton 1558b38ff370SJamie Gritton /* Set the parameters of the prison. */ 1559b38ff370SJamie Gritton #ifdef INET 15600304c731SJamie Gritton redo_ip4 = 0; 15610304c731SJamie Gritton if (pr_flags & PR_IP4_USER) { 15620304c731SJamie Gritton pr->pr_flags |= PR_IP4; 1563b38ff370SJamie Gritton free(pr->pr_ip4, M_PRISON); 15640304c731SJamie Gritton pr->pr_ip4s = ip4s; 1565b38ff370SJamie Gritton pr->pr_ip4 = ip4; 1566b38ff370SJamie Gritton ip4 = NULL; 15670304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1568bdfc8cc4SJamie Gritton #ifdef VIMAGE 1569bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1570bdfc8cc4SJamie Gritton descend = 0; 1571bdfc8cc4SJamie Gritton continue; 1572bdfc8cc4SJamie Gritton } 1573bdfc8cc4SJamie Gritton #endif 15740304c731SJamie Gritton if (prison_restrict_ip4(tpr, NULL)) { 15750304c731SJamie Gritton redo_ip4 = 1; 15760304c731SJamie Gritton descend = 0; 15770304c731SJamie Gritton } 15780304c731SJamie Gritton } 15790304c731SJamie Gritton } 1580b38ff370SJamie Gritton #endif 1581b38ff370SJamie Gritton #ifdef INET6 15820304c731SJamie Gritton redo_ip6 = 0; 15830304c731SJamie Gritton if (pr_flags & PR_IP6_USER) { 15840304c731SJamie Gritton pr->pr_flags |= PR_IP6; 1585b38ff370SJamie Gritton free(pr->pr_ip6, M_PRISON); 15860304c731SJamie Gritton pr->pr_ip6s = ip6s; 1587b38ff370SJamie Gritton pr->pr_ip6 = ip6; 1588b38ff370SJamie Gritton ip6 = NULL; 15890304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1590bdfc8cc4SJamie Gritton #ifdef VIMAGE 1591bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1592bdfc8cc4SJamie Gritton descend = 0; 1593bdfc8cc4SJamie Gritton continue; 1594bdfc8cc4SJamie Gritton } 1595bdfc8cc4SJamie Gritton #endif 15960304c731SJamie Gritton if (prison_restrict_ip6(tpr, NULL)) { 15970304c731SJamie Gritton redo_ip6 = 1; 15980304c731SJamie Gritton descend = 0; 15990304c731SJamie Gritton } 16000304c731SJamie Gritton } 16010304c731SJamie Gritton } 1602b38ff370SJamie Gritton #endif 16030304c731SJamie Gritton if (gotslevel) { 1604b38ff370SJamie Gritton pr->pr_securelevel = slevel; 16050304c731SJamie Gritton /* Set all child jails to be at least this level. */ 16060304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 16070304c731SJamie Gritton if (tpr->pr_securelevel < slevel) 16080304c731SJamie Gritton tpr->pr_securelevel = slevel; 16090304c731SJamie Gritton } 1610b97457e2SJamie Gritton if (gotchildmax) { 1611b97457e2SJamie Gritton pr->pr_childmax = childmax; 1612b97457e2SJamie Gritton /* Set all child jails to under this limit. */ 1613b97457e2SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level) 1614b97457e2SJamie Gritton if (tpr->pr_childmax > childmax - level) 1615b97457e2SJamie Gritton tpr->pr_childmax = childmax > level 1616b97457e2SJamie Gritton ? childmax - level : 0; 1617b97457e2SJamie Gritton } 16180304c731SJamie Gritton if (gotenforce) { 16190304c731SJamie Gritton pr->pr_enforce_statfs = enforce; 16200304c731SJamie Gritton /* Pass this restriction on to the children. */ 16210304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 16220304c731SJamie Gritton if (tpr->pr_enforce_statfs < enforce) 16230304c731SJamie Gritton tpr->pr_enforce_statfs = enforce; 16240304c731SJamie Gritton } 16250cc207a6SMartin Matuska if (gotrsnum) { 16260cc207a6SMartin Matuska pr->pr_devfs_rsnum = rsnum; 16270cc207a6SMartin Matuska /* Pass this restriction on to the children. */ 16280cc207a6SMartin Matuska FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 16290cc207a6SMartin Matuska tpr->pr_devfs_rsnum = rsnum; 16300cc207a6SMartin Matuska } 1631b6f47c23SJamie Gritton if (namelc != NULL) { 16320304c731SJamie Gritton if (ppr == &prison0) 1633b6f47c23SJamie Gritton strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name)); 16340304c731SJamie Gritton else 16350304c731SJamie Gritton snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s", 1636b6f47c23SJamie Gritton ppr->pr_name, namelc); 16370304c731SJamie Gritton /* Change this component of child names. */ 16380304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 16390304c731SJamie Gritton bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen, 16400304c731SJamie Gritton strlen(tpr->pr_name + onamelen) + 1); 16410304c731SJamie Gritton bcopy(pr->pr_name, tpr->pr_name, namelen); 16420304c731SJamie Gritton } 16430304c731SJamie Gritton } 1644b38ff370SJamie Gritton if (path != NULL) { 16450304c731SJamie Gritton /* Try to keep a real-rooted full pathname. */ 1646f6e633a9SMartin Matuska if (fullpath_disabled && path[0] == '/' && 1647f6e633a9SMartin Matuska strcmp(mypr->pr_path, "/")) 16480304c731SJamie Gritton snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s", 16490304c731SJamie Gritton mypr->pr_path, path); 16500304c731SJamie Gritton else 1651b38ff370SJamie Gritton strlcpy(pr->pr_path, path, sizeof(pr->pr_path)); 1652b38ff370SJamie Gritton pr->pr_root = root; 1653b38ff370SJamie Gritton } 165476ca6f88SJamie Gritton if (PR_HOST & ch_flags & ~pr_flags) { 165576ca6f88SJamie Gritton if (pr->pr_flags & PR_HOST) { 165676ca6f88SJamie Gritton /* 165776ca6f88SJamie Gritton * Copy the parent's host info. As with pr_ip4 above, 165876ca6f88SJamie Gritton * the lack of a lock on the parent is not a problem; 165976ca6f88SJamie Gritton * it is always set with allprison_lock at least 166076ca6f88SJamie Gritton * shared, and is held exclusively here. 166176ca6f88SJamie Gritton */ 1662c1f19219SJamie Gritton strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname, 1663c1f19219SJamie Gritton sizeof(pr->pr_hostname)); 1664c1f19219SJamie Gritton strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname, 1665c1f19219SJamie Gritton sizeof(pr->pr_domainname)); 1666c1f19219SJamie Gritton strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid, 1667c1f19219SJamie Gritton sizeof(pr->pr_hostuuid)); 166876ca6f88SJamie Gritton pr->pr_hostid = pr->pr_parent->pr_hostid; 166976ca6f88SJamie Gritton } 167076ca6f88SJamie Gritton } else if (host != NULL || domain != NULL || uuid != NULL || gothid) { 167176ca6f88SJamie Gritton /* Set this prison, and any descendants without PR_HOST. */ 1672b38ff370SJamie Gritton if (host != NULL) 1673c1f19219SJamie Gritton strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname)); 167476ca6f88SJamie Gritton if (domain != NULL) 1675c1f19219SJamie Gritton strlcpy(pr->pr_domainname, domain, 1676c1f19219SJamie Gritton sizeof(pr->pr_domainname)); 167776ca6f88SJamie Gritton if (uuid != NULL) 1678c1f19219SJamie Gritton strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid)); 167976ca6f88SJamie Gritton if (gothid) 168076ca6f88SJamie Gritton pr->pr_hostid = hid; 168176ca6f88SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 168276ca6f88SJamie Gritton if (tpr->pr_flags & PR_HOST) 168376ca6f88SJamie Gritton descend = 0; 168476ca6f88SJamie Gritton else { 168576ca6f88SJamie Gritton if (host != NULL) 1686c1f19219SJamie Gritton strlcpy(tpr->pr_hostname, 1687c1f19219SJamie Gritton pr->pr_hostname, 1688c1f19219SJamie Gritton sizeof(tpr->pr_hostname)); 168976ca6f88SJamie Gritton if (domain != NULL) 1690c1f19219SJamie Gritton strlcpy(tpr->pr_domainname, 1691c1f19219SJamie Gritton pr->pr_domainname, 1692c1f19219SJamie Gritton sizeof(tpr->pr_domainname)); 169376ca6f88SJamie Gritton if (uuid != NULL) 1694c1f19219SJamie Gritton strlcpy(tpr->pr_hostuuid, 1695c1f19219SJamie Gritton pr->pr_hostuuid, 1696c1f19219SJamie Gritton sizeof(tpr->pr_hostuuid)); 169776ca6f88SJamie Gritton if (gothid) 169876ca6f88SJamie Gritton tpr->pr_hostid = hid; 169976ca6f88SJamie Gritton } 170076ca6f88SJamie Gritton } 170176ca6f88SJamie Gritton } 17020304c731SJamie Gritton if ((tallow = ch_allow & ~pr_allow)) { 17030304c731SJamie Gritton /* Clear allow bits in all children. */ 17040304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 17050304c731SJamie Gritton tpr->pr_allow &= ~tallow; 17060304c731SJamie Gritton } 17070304c731SJamie Gritton pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow; 1708b38ff370SJamie Gritton /* 1709b38ff370SJamie Gritton * Persistent prisons get an extra reference, and prisons losing their 1710b38ff370SJamie Gritton * persist flag lose that reference. Only do this for existing prisons 1711b38ff370SJamie Gritton * for now, so new ones will remain unseen until after the module 1712b38ff370SJamie Gritton * handlers have completed. 1713b38ff370SJamie Gritton */ 1714cc5fd8c7SJamie Gritton born = pr->pr_uref == 0; 1715b38ff370SJamie Gritton if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) { 1716b38ff370SJamie Gritton if (pr_flags & PR_PERSIST) { 1717b38ff370SJamie Gritton pr->pr_ref++; 1718b38ff370SJamie Gritton pr->pr_uref++; 1719b38ff370SJamie Gritton } else { 1720b38ff370SJamie Gritton pr->pr_ref--; 1721b38ff370SJamie Gritton pr->pr_uref--; 1722b38ff370SJamie Gritton } 1723b38ff370SJamie Gritton } 1724b38ff370SJamie Gritton pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags; 1725b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1726b38ff370SJamie Gritton 1727a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 17284b5c9cf6SEdward Tomasz Napierala if (racct_enable && created) 1729a7ad07bfSEdward Tomasz Napierala prison_racct_attach(pr); 1730a7ad07bfSEdward Tomasz Napierala #endif 1731a7ad07bfSEdward Tomasz Napierala 17320304c731SJamie Gritton /* Locks may have prevented a complete restriction of child IP 17330304c731SJamie Gritton * addresses. If so, allocate some more memory and try again. 17340304c731SJamie Gritton */ 17350304c731SJamie Gritton #ifdef INET 17360304c731SJamie Gritton while (redo_ip4) { 17370304c731SJamie Gritton ip4s = pr->pr_ip4s; 17380304c731SJamie Gritton ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 17390304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 17400304c731SJamie Gritton redo_ip4 = 0; 17410304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1742bdfc8cc4SJamie Gritton #ifdef VIMAGE 1743bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1744bdfc8cc4SJamie Gritton descend = 0; 1745bdfc8cc4SJamie Gritton continue; 1746bdfc8cc4SJamie Gritton } 1747bdfc8cc4SJamie Gritton #endif 17480304c731SJamie Gritton if (prison_restrict_ip4(tpr, ip4)) { 17490304c731SJamie Gritton if (ip4 != NULL) 17500304c731SJamie Gritton ip4 = NULL; 17510304c731SJamie Gritton else 17520304c731SJamie Gritton redo_ip4 = 1; 17530304c731SJamie Gritton } 17540304c731SJamie Gritton } 17550304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 17560304c731SJamie Gritton } 17570304c731SJamie Gritton #endif 17580304c731SJamie Gritton #ifdef INET6 17590304c731SJamie Gritton while (redo_ip6) { 17600304c731SJamie Gritton ip6s = pr->pr_ip6s; 17610304c731SJamie Gritton ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 17620304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 17630304c731SJamie Gritton redo_ip6 = 0; 17640304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1765bdfc8cc4SJamie Gritton #ifdef VIMAGE 1766bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1767bdfc8cc4SJamie Gritton descend = 0; 1768bdfc8cc4SJamie Gritton continue; 1769bdfc8cc4SJamie Gritton } 1770bdfc8cc4SJamie Gritton #endif 17710304c731SJamie Gritton if (prison_restrict_ip6(tpr, ip6)) { 17720304c731SJamie Gritton if (ip6 != NULL) 17730304c731SJamie Gritton ip6 = NULL; 17740304c731SJamie Gritton else 17750304c731SJamie Gritton redo_ip6 = 1; 17760304c731SJamie Gritton } 17770304c731SJamie Gritton } 17780304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 17790304c731SJamie Gritton } 17800304c731SJamie Gritton #endif 17810304c731SJamie Gritton 1782b38ff370SJamie Gritton /* Let the modules do their work. */ 1783b38ff370SJamie Gritton sx_downgrade(&allprison_lock); 1784cc5fd8c7SJamie Gritton if (born) { 1785b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_CREATE, opts); 1786b38ff370SJamie Gritton if (error) { 1787cc5fd8c7SJamie Gritton (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL); 1788cc5fd8c7SJamie Gritton prison_deref(pr, created 1789cc5fd8c7SJamie Gritton ? PD_LIST_SLOCKED 1790cc5fd8c7SJamie Gritton : PD_DEREF | PD_LIST_SLOCKED); 1791b38ff370SJamie Gritton goto done_errmsg; 1792b38ff370SJamie Gritton } 1793b38ff370SJamie Gritton } 1794b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_SET, opts); 1795b38ff370SJamie Gritton if (error) { 1796cc5fd8c7SJamie Gritton if (born) 1797cc5fd8c7SJamie Gritton (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL); 1798b38ff370SJamie Gritton prison_deref(pr, created 1799b38ff370SJamie Gritton ? PD_LIST_SLOCKED 1800b38ff370SJamie Gritton : PD_DEREF | PD_LIST_SLOCKED); 1801b38ff370SJamie Gritton goto done_errmsg; 1802b38ff370SJamie Gritton } 1803b38ff370SJamie Gritton 1804b38ff370SJamie Gritton /* Attach this process to the prison if requested. */ 1805b38ff370SJamie Gritton if (flags & JAIL_ATTACH) { 1806b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1807b38ff370SJamie Gritton error = do_jail_attach(td, pr); 1808b38ff370SJamie Gritton if (error) { 1809b38ff370SJamie Gritton vfs_opterror(opts, "attach failed"); 1810b38ff370SJamie Gritton if (!created) 1811b38ff370SJamie Gritton prison_deref(pr, PD_DEREF); 1812b38ff370SJamie Gritton goto done_errmsg; 1813b38ff370SJamie Gritton } 1814b38ff370SJamie Gritton } 1815b38ff370SJamie Gritton 18161fb24974SEdward Tomasz Napierala #ifdef RACCT 18174b5c9cf6SEdward Tomasz Napierala if (racct_enable && !created) { 1818f514b97bSEdward Tomasz Napierala if (!(flags & JAIL_ATTACH)) 18191fb24974SEdward Tomasz Napierala sx_sunlock(&allprison_lock); 18201fb24974SEdward Tomasz Napierala prison_racct_modify(pr); 1821f514b97bSEdward Tomasz Napierala if (!(flags & JAIL_ATTACH)) 18221fb24974SEdward Tomasz Napierala sx_slock(&allprison_lock); 18231fb24974SEdward Tomasz Napierala } 18241fb24974SEdward Tomasz Napierala #endif 18251fb24974SEdward Tomasz Napierala 18261fb24974SEdward Tomasz Napierala td->td_retval[0] = pr->pr_id; 18271fb24974SEdward Tomasz Napierala 1828b38ff370SJamie Gritton /* 1829b38ff370SJamie Gritton * Now that it is all there, drop the temporary reference from existing 1830b38ff370SJamie Gritton * prisons. Or add a reference to newly created persistent prisons 1831b38ff370SJamie Gritton * (which was not done earlier so that the prison would not be publicly 1832b38ff370SJamie Gritton * visible). 1833b38ff370SJamie Gritton */ 1834b38ff370SJamie Gritton if (!created) { 1835b38ff370SJamie Gritton prison_deref(pr, (flags & JAIL_ATTACH) 1836b38ff370SJamie Gritton ? PD_DEREF 1837b38ff370SJamie Gritton : PD_DEREF | PD_LIST_SLOCKED); 1838b38ff370SJamie Gritton } else { 1839b38ff370SJamie Gritton if (pr_flags & PR_PERSIST) { 1840b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1841b38ff370SJamie Gritton pr->pr_ref++; 1842b38ff370SJamie Gritton pr->pr_uref++; 1843b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1844b38ff370SJamie Gritton } 1845b38ff370SJamie Gritton if (!(flags & JAIL_ATTACH)) 1846b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 1847b38ff370SJamie Gritton } 1848c34bbd2aSEdward Tomasz Napierala 1849cc5fd8c7SJamie Gritton goto done_free; 1850b38ff370SJamie Gritton 18510304c731SJamie Gritton done_deref_locked: 18520304c731SJamie Gritton prison_deref(pr, created 18530304c731SJamie Gritton ? PD_LOCKED | PD_LIST_XLOCKED 18540304c731SJamie Gritton : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 18550304c731SJamie Gritton goto done_releroot; 1856b38ff370SJamie Gritton done_unlock_list: 1857b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 1858b38ff370SJamie Gritton done_releroot: 18595050aa86SKonstantin Belousov if (root != NULL) 1860b38ff370SJamie Gritton vrele(root); 1861b38ff370SJamie Gritton done_errmsg: 1862b38ff370SJamie Gritton if (error) { 1863176ff3a0SJamie Gritton if (vfs_getopt(opts, "errmsg", (void **)&errmsg, 1864176ff3a0SJamie Gritton &errmsg_len) == 0 && errmsg_len > 0) { 1865b38ff370SJamie Gritton errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1; 1866b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 1867b38ff370SJamie Gritton bcopy(errmsg, 1868b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 1869b38ff370SJamie Gritton errmsg_len); 1870b38ff370SJamie Gritton else 1871b38ff370SJamie Gritton copyout(errmsg, 1872b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 1873b38ff370SJamie Gritton errmsg_len); 1874b38ff370SJamie Gritton } 1875b38ff370SJamie Gritton } 1876b38ff370SJamie Gritton done_free: 1877b38ff370SJamie Gritton #ifdef INET 1878b38ff370SJamie Gritton free(ip4, M_PRISON); 1879b38ff370SJamie Gritton #endif 1880b38ff370SJamie Gritton #ifdef INET6 1881b38ff370SJamie Gritton free(ip6, M_PRISON); 1882b38ff370SJamie Gritton #endif 18836dfe0a3dSMartin Matuska if (g_path != NULL) 18846dfe0a3dSMartin Matuska free(g_path, M_TEMP); 1885b38ff370SJamie Gritton vfs_freeopts(opts); 1886b38ff370SJamie Gritton return (error); 1887b38ff370SJamie Gritton } 1888b38ff370SJamie Gritton 1889b38ff370SJamie Gritton 1890b38ff370SJamie Gritton /* 1891b38ff370SJamie Gritton * struct jail_get_args { 1892b38ff370SJamie Gritton * struct iovec *iovp; 1893b38ff370SJamie Gritton * unsigned int iovcnt; 1894b38ff370SJamie Gritton * int flags; 1895b38ff370SJamie Gritton * }; 1896b38ff370SJamie Gritton */ 1897b38ff370SJamie Gritton int 18988451d0ddSKip Macy sys_jail_get(struct thread *td, struct jail_get_args *uap) 1899b38ff370SJamie Gritton { 1900b38ff370SJamie Gritton struct uio *auio; 1901b38ff370SJamie Gritton int error; 1902b38ff370SJamie Gritton 1903b38ff370SJamie Gritton /* Check that we have an even number of iovecs. */ 1904b38ff370SJamie Gritton if (uap->iovcnt & 1) 1905b38ff370SJamie Gritton return (EINVAL); 1906b38ff370SJamie Gritton 1907b38ff370SJamie Gritton error = copyinuio(uap->iovp, uap->iovcnt, &auio); 1908b38ff370SJamie Gritton if (error) 1909b38ff370SJamie Gritton return (error); 1910b38ff370SJamie Gritton error = kern_jail_get(td, auio, uap->flags); 1911b38ff370SJamie Gritton if (error == 0) 1912b38ff370SJamie Gritton error = copyout(auio->uio_iov, uap->iovp, 1913b38ff370SJamie Gritton uap->iovcnt * sizeof (struct iovec)); 1914b38ff370SJamie Gritton free(auio, M_IOV); 1915b38ff370SJamie Gritton return (error); 1916b38ff370SJamie Gritton } 1917b38ff370SJamie Gritton 1918b38ff370SJamie Gritton int 1919b38ff370SJamie Gritton kern_jail_get(struct thread *td, struct uio *optuio, int flags) 1920b38ff370SJamie Gritton { 1921672756aaSJamie Gritton struct bool_flags *bf; 1922672756aaSJamie Gritton struct jailsys_flags *jsf; 19230304c731SJamie Gritton struct prison *pr, *mypr; 1924b38ff370SJamie Gritton struct vfsopt *opt; 1925b38ff370SJamie Gritton struct vfsoptlist *opts; 1926b38ff370SJamie Gritton char *errmsg, *name; 1927672756aaSJamie Gritton int error, errmsg_len, errmsg_pos, i, jid, len, locked, pos; 1928672756aaSJamie Gritton unsigned f; 1929b38ff370SJamie Gritton 1930b38ff370SJamie Gritton if (flags & ~JAIL_GET_MASK) 1931b38ff370SJamie Gritton return (EINVAL); 1932b38ff370SJamie Gritton 1933b38ff370SJamie Gritton /* Get the parameter list. */ 1934b38ff370SJamie Gritton error = vfs_buildopts(optuio, &opts); 1935b38ff370SJamie Gritton if (error) 1936b38ff370SJamie Gritton return (error); 1937b38ff370SJamie Gritton errmsg_pos = vfs_getopt_pos(opts, "errmsg"); 19380304c731SJamie Gritton mypr = td->td_ucred->cr_prison; 19391e2a13e6SJamie Gritton 1940b38ff370SJamie Gritton /* 1941b38ff370SJamie Gritton * Find the prison specified by one of: lastjid, jid, name. 1942b38ff370SJamie Gritton */ 1943b38ff370SJamie Gritton sx_slock(&allprison_lock); 1944b38ff370SJamie Gritton error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid)); 1945b38ff370SJamie Gritton if (error == 0) { 1946b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 19470304c731SJamie Gritton if (pr->pr_id > jid && prison_ischild(mypr, pr)) { 1948b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1949b38ff370SJamie Gritton if (pr->pr_ref > 0 && 1950b38ff370SJamie Gritton (pr->pr_uref > 0 || (flags & JAIL_DYING))) 1951b38ff370SJamie Gritton break; 1952b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1953b38ff370SJamie Gritton } 1954b38ff370SJamie Gritton } 1955b38ff370SJamie Gritton if (pr != NULL) 1956b38ff370SJamie Gritton goto found_prison; 1957b38ff370SJamie Gritton error = ENOENT; 1958b38ff370SJamie Gritton vfs_opterror(opts, "no jail after %d", jid); 1959b38ff370SJamie Gritton goto done_unlock_list; 1960b38ff370SJamie Gritton } else if (error != ENOENT) 1961b38ff370SJamie Gritton goto done_unlock_list; 1962b38ff370SJamie Gritton 1963b38ff370SJamie Gritton error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 1964b38ff370SJamie Gritton if (error == 0) { 1965b38ff370SJamie Gritton if (jid != 0) { 19660304c731SJamie Gritton pr = prison_find_child(mypr, jid); 1967b38ff370SJamie Gritton if (pr != NULL) { 1968b38ff370SJamie Gritton if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 1969b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1970b38ff370SJamie Gritton error = ENOENT; 1971b38ff370SJamie Gritton vfs_opterror(opts, "jail %d is dying", 1972b38ff370SJamie Gritton jid); 1973b38ff370SJamie Gritton goto done_unlock_list; 1974b38ff370SJamie Gritton } 1975b38ff370SJamie Gritton goto found_prison; 1976b38ff370SJamie Gritton } 1977b38ff370SJamie Gritton error = ENOENT; 1978b38ff370SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 1979b38ff370SJamie Gritton goto done_unlock_list; 1980b38ff370SJamie Gritton } 1981b38ff370SJamie Gritton } else if (error != ENOENT) 1982b38ff370SJamie Gritton goto done_unlock_list; 1983b38ff370SJamie Gritton 1984b38ff370SJamie Gritton error = vfs_getopt(opts, "name", (void **)&name, &len); 1985b38ff370SJamie Gritton if (error == 0) { 1986b38ff370SJamie Gritton if (len == 0 || name[len - 1] != '\0') { 1987b38ff370SJamie Gritton error = EINVAL; 1988b38ff370SJamie Gritton goto done_unlock_list; 1989b38ff370SJamie Gritton } 19900304c731SJamie Gritton pr = prison_find_name(mypr, name); 1991b38ff370SJamie Gritton if (pr != NULL) { 1992b38ff370SJamie Gritton if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 1993b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1994b38ff370SJamie Gritton error = ENOENT; 1995b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" is dying", 1996b38ff370SJamie Gritton name); 1997b38ff370SJamie Gritton goto done_unlock_list; 1998b38ff370SJamie Gritton } 1999b38ff370SJamie Gritton goto found_prison; 2000b38ff370SJamie Gritton } 2001b38ff370SJamie Gritton error = ENOENT; 2002b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", name); 2003b38ff370SJamie Gritton goto done_unlock_list; 2004b38ff370SJamie Gritton } else if (error != ENOENT) 2005b38ff370SJamie Gritton goto done_unlock_list; 2006b38ff370SJamie Gritton 2007b38ff370SJamie Gritton vfs_opterror(opts, "no jail specified"); 2008b38ff370SJamie Gritton error = ENOENT; 2009b38ff370SJamie Gritton goto done_unlock_list; 2010b38ff370SJamie Gritton 2011b38ff370SJamie Gritton found_prison: 2012b38ff370SJamie Gritton /* Get the parameters of the prison. */ 2013b38ff370SJamie Gritton pr->pr_ref++; 2014b38ff370SJamie Gritton locked = PD_LOCKED; 2015b38ff370SJamie Gritton td->td_retval[0] = pr->pr_id; 2016b38ff370SJamie Gritton error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id)); 2017b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2018b38ff370SJamie Gritton goto done_deref; 20190304c731SJamie Gritton i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id; 20200304c731SJamie Gritton error = vfs_setopt(opts, "parent", &i, sizeof(i)); 2021b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2022b38ff370SJamie Gritton goto done_deref; 20230304c731SJamie Gritton error = vfs_setopts(opts, "name", prison_name(mypr, pr)); 20240304c731SJamie Gritton if (error != 0 && error != ENOENT) 20250304c731SJamie Gritton goto done_deref; 20260304c731SJamie Gritton error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id, 2027b38ff370SJamie Gritton sizeof(pr->pr_cpuset->cs_id)); 2028b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2029b38ff370SJamie Gritton goto done_deref; 20300304c731SJamie Gritton error = vfs_setopts(opts, "path", prison_path(mypr, pr)); 2031b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2032b38ff370SJamie Gritton goto done_deref; 2033b38ff370SJamie Gritton #ifdef INET 2034b38ff370SJamie Gritton error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4, 2035b38ff370SJamie Gritton pr->pr_ip4s * sizeof(*pr->pr_ip4)); 2036b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2037b38ff370SJamie Gritton goto done_deref; 2038b38ff370SJamie Gritton #endif 2039b38ff370SJamie Gritton #ifdef INET6 2040b38ff370SJamie Gritton error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6, 2041b38ff370SJamie Gritton pr->pr_ip6s * sizeof(*pr->pr_ip6)); 2042b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2043b38ff370SJamie Gritton goto done_deref; 2044b38ff370SJamie Gritton #endif 2045b38ff370SJamie Gritton error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel, 2046b38ff370SJamie Gritton sizeof(pr->pr_securelevel)); 2047b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2048b38ff370SJamie Gritton goto done_deref; 2049b97457e2SJamie Gritton error = vfs_setopt(opts, "children.cur", &pr->pr_childcount, 2050b97457e2SJamie Gritton sizeof(pr->pr_childcount)); 2051b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 2052b97457e2SJamie Gritton goto done_deref; 2053b97457e2SJamie Gritton error = vfs_setopt(opts, "children.max", &pr->pr_childmax, 2054b97457e2SJamie Gritton sizeof(pr->pr_childmax)); 2055b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 2056b97457e2SJamie Gritton goto done_deref; 2057c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostname", pr->pr_hostname); 2058b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2059b38ff370SJamie Gritton goto done_deref; 2060c1f19219SJamie Gritton error = vfs_setopts(opts, "host.domainname", pr->pr_domainname); 206176ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 206276ca6f88SJamie Gritton goto done_deref; 2063c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid); 206476ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 206576ca6f88SJamie Gritton goto done_deref; 2066841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32 2067a5c1afadSDmitry Chagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 206876ca6f88SJamie Gritton uint32_t hid32 = pr->pr_hostid; 206976ca6f88SJamie Gritton 207076ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32)); 207176ca6f88SJamie Gritton } else 207276ca6f88SJamie Gritton #endif 207376ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid, 207476ca6f88SJamie Gritton sizeof(pr->pr_hostid)); 207576ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 207676ca6f88SJamie Gritton goto done_deref; 20770304c731SJamie Gritton error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs, 20780304c731SJamie Gritton sizeof(pr->pr_enforce_statfs)); 20790304c731SJamie Gritton if (error != 0 && error != ENOENT) 20800304c731SJamie Gritton goto done_deref; 20810cc207a6SMartin Matuska error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum, 20820cc207a6SMartin Matuska sizeof(pr->pr_devfs_rsnum)); 20830cc207a6SMartin Matuska if (error != 0 && error != ENOENT) 20840cc207a6SMartin Matuska goto done_deref; 2085672756aaSJamie Gritton for (bf = pr_flag_bool; 2086672756aaSJamie Gritton bf < pr_flag_bool + nitems(pr_flag_bool); 2087672756aaSJamie Gritton bf++) { 2088672756aaSJamie Gritton i = (pr->pr_flags & bf->flag) ? 1 : 0; 2089672756aaSJamie Gritton error = vfs_setopt(opts, bf->name, &i, sizeof(i)); 2090b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2091b38ff370SJamie Gritton goto done_deref; 2092b38ff370SJamie Gritton i = !i; 2093672756aaSJamie Gritton error = vfs_setopt(opts, bf->noname, &i, sizeof(i)); 2094b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2095b38ff370SJamie Gritton goto done_deref; 20960304c731SJamie Gritton } 2097672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 2098672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 2099672756aaSJamie Gritton jsf++) { 2100672756aaSJamie Gritton f = pr->pr_flags & (jsf->disable | jsf->new); 2101672756aaSJamie Gritton i = (f != 0 && f == jsf->disable) ? JAIL_SYS_DISABLE 2102672756aaSJamie Gritton : (f == jsf->new) ? JAIL_SYS_NEW 21037cbf7213SJamie Gritton : JAIL_SYS_INHERIT; 2104672756aaSJamie Gritton error = vfs_setopt(opts, jsf->name, &i, sizeof(i)); 21057cbf7213SJamie Gritton if (error != 0 && error != ENOENT) 21067cbf7213SJamie Gritton goto done_deref; 21077cbf7213SJamie Gritton } 2108672756aaSJamie Gritton for (bf = pr_flag_allow; 21090e5c6bd4SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && bf->flag != 0; 2110672756aaSJamie Gritton bf++) { 2111672756aaSJamie Gritton i = (pr->pr_allow & bf->flag) ? 1 : 0; 2112672756aaSJamie Gritton error = vfs_setopt(opts, bf->name, &i, sizeof(i)); 21130304c731SJamie Gritton if (error != 0 && error != ENOENT) 21140304c731SJamie Gritton goto done_deref; 21150304c731SJamie Gritton i = !i; 2116672756aaSJamie Gritton error = vfs_setopt(opts, bf->noname, &i, sizeof(i)); 21170304c731SJamie Gritton if (error != 0 && error != ENOENT) 21180304c731SJamie Gritton goto done_deref; 21190304c731SJamie Gritton } 2120b38ff370SJamie Gritton i = (pr->pr_uref == 0); 2121b38ff370SJamie Gritton error = vfs_setopt(opts, "dying", &i, sizeof(i)); 2122b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2123b38ff370SJamie Gritton goto done_deref; 2124b38ff370SJamie Gritton i = !i; 2125b38ff370SJamie Gritton error = vfs_setopt(opts, "nodying", &i, sizeof(i)); 2126b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2127b38ff370SJamie Gritton goto done_deref; 2128bd96bd15SIan Lepore error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate, 2129bd96bd15SIan Lepore sizeof(pr->pr_osreldate)); 2130a1a4c1b0SIan Lepore if (error != 0 && error != ENOENT) 2131a1a4c1b0SIan Lepore goto done_deref; 2132a1a4c1b0SIan Lepore error = vfs_setopts(opts, "osrelease", pr->pr_osrelease); 2133a1a4c1b0SIan Lepore if (error != 0 && error != ENOENT) 2134a1a4c1b0SIan Lepore goto done_deref; 2135b38ff370SJamie Gritton 2136b38ff370SJamie Gritton /* Get the module parameters. */ 2137b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2138b38ff370SJamie Gritton locked = 0; 2139b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_GET, opts); 2140b38ff370SJamie Gritton if (error) 2141b38ff370SJamie Gritton goto done_deref; 2142b38ff370SJamie Gritton prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED); 2143b38ff370SJamie Gritton 2144b38ff370SJamie Gritton /* By now, all parameters should have been noted. */ 2145b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2146b38ff370SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 2147b38ff370SJamie Gritton error = EINVAL; 2148b38ff370SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 2149b38ff370SJamie Gritton goto done_errmsg; 2150b38ff370SJamie Gritton } 2151b38ff370SJamie Gritton } 2152b38ff370SJamie Gritton 2153b38ff370SJamie Gritton /* Write the fetched parameters back to userspace. */ 2154b38ff370SJamie Gritton error = 0; 2155b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2156b38ff370SJamie Gritton if (opt->pos >= 0 && opt->pos != errmsg_pos) { 2157b38ff370SJamie Gritton pos = 2 * opt->pos + 1; 2158b38ff370SJamie Gritton optuio->uio_iov[pos].iov_len = opt->len; 2159b38ff370SJamie Gritton if (opt->value != NULL) { 2160b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) { 2161b38ff370SJamie Gritton bcopy(opt->value, 2162b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2163b38ff370SJamie Gritton opt->len); 2164b38ff370SJamie Gritton } else { 2165b38ff370SJamie Gritton error = copyout(opt->value, 2166b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2167b38ff370SJamie Gritton opt->len); 2168b38ff370SJamie Gritton if (error) 2169b38ff370SJamie Gritton break; 2170b38ff370SJamie Gritton } 2171b38ff370SJamie Gritton } 2172b38ff370SJamie Gritton } 2173b38ff370SJamie Gritton } 2174b38ff370SJamie Gritton goto done_errmsg; 2175b38ff370SJamie Gritton 2176b38ff370SJamie Gritton done_deref: 2177b38ff370SJamie Gritton prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED); 2178b38ff370SJamie Gritton goto done_errmsg; 2179b38ff370SJamie Gritton 2180b38ff370SJamie Gritton done_unlock_list: 2181b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2182b38ff370SJamie Gritton done_errmsg: 2183b38ff370SJamie Gritton if (error && errmsg_pos >= 0) { 2184b38ff370SJamie Gritton vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 2185b38ff370SJamie Gritton errmsg_pos = 2 * errmsg_pos + 1; 2186b38ff370SJamie Gritton if (errmsg_len > 0) { 2187b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 2188b38ff370SJamie Gritton bcopy(errmsg, 2189b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2190b38ff370SJamie Gritton errmsg_len); 2191b38ff370SJamie Gritton else 2192b38ff370SJamie Gritton copyout(errmsg, 2193b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2194b38ff370SJamie Gritton errmsg_len); 2195b38ff370SJamie Gritton } 2196b38ff370SJamie Gritton } 2197b38ff370SJamie Gritton vfs_freeopts(opts); 2198b38ff370SJamie Gritton return (error); 2199b38ff370SJamie Gritton } 2200b38ff370SJamie Gritton 22010304c731SJamie Gritton 2202b38ff370SJamie Gritton /* 2203b38ff370SJamie Gritton * struct jail_remove_args { 2204b38ff370SJamie Gritton * int jid; 2205b38ff370SJamie Gritton * }; 2206b38ff370SJamie Gritton */ 2207b38ff370SJamie Gritton int 22088451d0ddSKip Macy sys_jail_remove(struct thread *td, struct jail_remove_args *uap) 2209b38ff370SJamie Gritton { 22100304c731SJamie Gritton struct prison *pr, *cpr, *lpr, *tpr; 22110304c731SJamie Gritton int descend, error; 2212b38ff370SJamie Gritton 2213b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_REMOVE); 2214b38ff370SJamie Gritton if (error) 2215b38ff370SJamie Gritton return (error); 2216b38ff370SJamie Gritton 2217b38ff370SJamie Gritton sx_xlock(&allprison_lock); 22180304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2219b38ff370SJamie Gritton if (pr == NULL) { 2220b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2221b38ff370SJamie Gritton return (EINVAL); 2222b38ff370SJamie Gritton } 2223b38ff370SJamie Gritton 22240304c731SJamie Gritton /* Remove all descendants of this prison, then remove this prison. */ 22250304c731SJamie Gritton pr->pr_ref++; 22260304c731SJamie Gritton if (!LIST_EMPTY(&pr->pr_children)) { 22270304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 22280304c731SJamie Gritton lpr = NULL; 22290304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 22300304c731SJamie Gritton mtx_lock(&cpr->pr_mtx); 22310304c731SJamie Gritton if (cpr->pr_ref > 0) { 22320304c731SJamie Gritton tpr = cpr; 22330304c731SJamie Gritton cpr->pr_ref++; 22340304c731SJamie Gritton } else { 22350304c731SJamie Gritton /* Already removed - do not do it again. */ 22360304c731SJamie Gritton tpr = NULL; 22370304c731SJamie Gritton } 22380304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 22390304c731SJamie Gritton if (lpr != NULL) { 22400304c731SJamie Gritton mtx_lock(&lpr->pr_mtx); 22410304c731SJamie Gritton prison_remove_one(lpr); 22420304c731SJamie Gritton sx_xlock(&allprison_lock); 22430304c731SJamie Gritton } 22440304c731SJamie Gritton lpr = tpr; 22450304c731SJamie Gritton } 22460304c731SJamie Gritton if (lpr != NULL) { 22470304c731SJamie Gritton mtx_lock(&lpr->pr_mtx); 22480304c731SJamie Gritton prison_remove_one(lpr); 22490304c731SJamie Gritton sx_xlock(&allprison_lock); 22500304c731SJamie Gritton } 22510304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 22520304c731SJamie Gritton } 22530304c731SJamie Gritton prison_remove_one(pr); 22540304c731SJamie Gritton return (0); 22550304c731SJamie Gritton } 22560304c731SJamie Gritton 22570304c731SJamie Gritton static void 22580304c731SJamie Gritton prison_remove_one(struct prison *pr) 22590304c731SJamie Gritton { 22600304c731SJamie Gritton struct proc *p; 22610304c731SJamie Gritton int deuref; 22620304c731SJamie Gritton 2263b38ff370SJamie Gritton /* If the prison was persistent, it is not anymore. */ 2264b38ff370SJamie Gritton deuref = 0; 2265b38ff370SJamie Gritton if (pr->pr_flags & PR_PERSIST) { 2266b38ff370SJamie Gritton pr->pr_ref--; 2267b38ff370SJamie Gritton deuref = PD_DEUREF; 2268b38ff370SJamie Gritton pr->pr_flags &= ~PR_PERSIST; 2269b38ff370SJamie Gritton } 2270b38ff370SJamie Gritton 22710304c731SJamie Gritton /* 22720304c731SJamie Gritton * jail_remove added a reference. If that's the only one, remove 22730304c731SJamie Gritton * the prison now. 22740304c731SJamie Gritton */ 22750304c731SJamie Gritton KASSERT(pr->pr_ref > 0, 22760304c731SJamie Gritton ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id)); 22770304c731SJamie Gritton if (pr->pr_ref == 1) { 2278b38ff370SJamie Gritton prison_deref(pr, 2279b38ff370SJamie Gritton deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 22800304c731SJamie Gritton return; 2281b38ff370SJamie Gritton } 2282b38ff370SJamie Gritton 2283b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2284b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2285b38ff370SJamie Gritton /* 2286b38ff370SJamie Gritton * Kill all processes unfortunate enough to be attached to this prison. 2287b38ff370SJamie Gritton */ 2288b38ff370SJamie Gritton sx_slock(&allproc_lock); 22897938a442SBjoern A. Zeeb FOREACH_PROC_IN_SYSTEM(p) { 2290b38ff370SJamie Gritton PROC_LOCK(p); 2291b38ff370SJamie Gritton if (p->p_state != PRS_NEW && p->p_ucred && 2292b38ff370SJamie Gritton p->p_ucred->cr_prison == pr) 22938451d0ddSKip Macy kern_psignal(p, SIGKILL); 2294b38ff370SJamie Gritton PROC_UNLOCK(p); 2295b38ff370SJamie Gritton } 2296b38ff370SJamie Gritton sx_sunlock(&allproc_lock); 22970304c731SJamie Gritton /* Remove the temporary reference added by jail_remove. */ 2298b38ff370SJamie Gritton prison_deref(pr, deuref | PD_DEREF); 229975c13541SPoul-Henning Kamp } 230075c13541SPoul-Henning Kamp 23018571af59SJamie Gritton 2302fd7a8150SMike Barcroft /* 23039ddb7954SMike Barcroft * struct jail_attach_args { 23049ddb7954SMike Barcroft * int jid; 23059ddb7954SMike Barcroft * }; 2306fd7a8150SMike Barcroft */ 2307fd7a8150SMike Barcroft int 23088451d0ddSKip Macy sys_jail_attach(struct thread *td, struct jail_attach_args *uap) 2309fd7a8150SMike Barcroft { 2310b38ff370SJamie Gritton struct prison *pr; 2311b38ff370SJamie Gritton int error; 2312b38ff370SJamie Gritton 2313b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 2314b38ff370SJamie Gritton if (error) 2315b38ff370SJamie Gritton return (error); 2316b38ff370SJamie Gritton 2317ef0ddea3SJamie Gritton /* 2318ef0ddea3SJamie Gritton * Start with exclusive hold on allprison_lock to ensure that a possible 2319ef0ddea3SJamie Gritton * PR_METHOD_REMOVE call isn't concurrent with jail_set or jail_remove. 2320ef0ddea3SJamie Gritton * But then immediately downgrade it since we don't need to stop 2321ef0ddea3SJamie Gritton * readers. 2322ef0ddea3SJamie Gritton */ 2323ef0ddea3SJamie Gritton sx_xlock(&allprison_lock); 2324ef0ddea3SJamie Gritton sx_downgrade(&allprison_lock); 23250304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2326b38ff370SJamie Gritton if (pr == NULL) { 2327b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2328b38ff370SJamie Gritton return (EINVAL); 2329b38ff370SJamie Gritton } 2330b38ff370SJamie Gritton 2331b38ff370SJamie Gritton /* 2332b38ff370SJamie Gritton * Do not allow a process to attach to a prison that is not 2333b38ff370SJamie Gritton * considered to be "alive". 2334b38ff370SJamie Gritton */ 2335b38ff370SJamie Gritton if (pr->pr_uref == 0) { 2336b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2337b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2338b38ff370SJamie Gritton return (EINVAL); 2339b38ff370SJamie Gritton } 2340b38ff370SJamie Gritton 2341b38ff370SJamie Gritton return (do_jail_attach(td, pr)); 2342b38ff370SJamie Gritton } 2343b38ff370SJamie Gritton 2344b38ff370SJamie Gritton static int 2345b38ff370SJamie Gritton do_jail_attach(struct thread *td, struct prison *pr) 2346b38ff370SJamie Gritton { 2347fd7a8150SMike Barcroft struct proc *p; 2348fd7a8150SMike Barcroft struct ucred *newcred, *oldcred; 23495050aa86SKonstantin Belousov int error; 2350fd7a8150SMike Barcroft 235157f22bd4SJacques Vidrine /* 235257f22bd4SJacques Vidrine * XXX: Note that there is a slight race here if two threads 235357f22bd4SJacques Vidrine * in the same privileged process attempt to attach to two 235457f22bd4SJacques Vidrine * different jails at the same time. It is important for 235557f22bd4SJacques Vidrine * user processes not to do this, or they might end up with 235657f22bd4SJacques Vidrine * a process root from one prison, but attached to the jail 235757f22bd4SJacques Vidrine * of another. 235857f22bd4SJacques Vidrine */ 2359fd7a8150SMike Barcroft pr->pr_ref++; 2360b38ff370SJamie Gritton pr->pr_uref++; 2361fd7a8150SMike Barcroft mtx_unlock(&pr->pr_mtx); 2362b38ff370SJamie Gritton 2363b38ff370SJamie Gritton /* Let modules do whatever they need to prepare for attaching. */ 2364b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_ATTACH, td); 2365b38ff370SJamie Gritton if (error) { 2366b38ff370SJamie Gritton prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED); 2367b38ff370SJamie Gritton return (error); 2368b38ff370SJamie Gritton } 2369dc68a633SPawel Jakub Dawidek sx_sunlock(&allprison_lock); 2370fd7a8150SMike Barcroft 2371413628a7SBjoern A. Zeeb /* 2372413628a7SBjoern A. Zeeb * Reparent the newly attached process to this jail. 2373413628a7SBjoern A. Zeeb */ 2374b38ff370SJamie Gritton p = td->td_proc; 2375413628a7SBjoern A. Zeeb error = cpuset_setproc_update_set(p, pr->pr_cpuset); 2376413628a7SBjoern A. Zeeb if (error) 2377b38ff370SJamie Gritton goto e_revert_osd; 2378413628a7SBjoern A. Zeeb 2379cb05b60aSAttilio Rao vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY); 2380fd7a8150SMike Barcroft if ((error = change_dir(pr->pr_root, td)) != 0) 2381fd7a8150SMike Barcroft goto e_unlock; 2382fd7a8150SMike Barcroft #ifdef MAC 238330d239bcSRobert Watson if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root))) 2384fd7a8150SMike Barcroft goto e_unlock; 2385fd7a8150SMike Barcroft #endif 238622db15c0SAttilio Rao VOP_UNLOCK(pr->pr_root, 0); 2387f0725a8eSMateusz Guzik if ((error = pwd_chroot(td, pr->pr_root))) 23885050aa86SKonstantin Belousov goto e_revert_osd; 2389fd7a8150SMike Barcroft 2390fd7a8150SMike Barcroft newcred = crget(); 2391fd7a8150SMike Barcroft PROC_LOCK(p); 23921fb6767dSJamie Gritton oldcred = crcopysafe(p, newcred); 239369c4ee54SJohn Baldwin newcred->cr_prison = pr; 2394daf63fd2SMateusz Guzik proc_set_cred(p, newcred); 23951fb6767dSJamie Gritton setsugid(p); 2396097055e2SEdward Tomasz Napierala #ifdef RACCT 2397097055e2SEdward Tomasz Napierala racct_proc_ucred_changed(p, oldcred, newcred); 2398f87beb93SAndriy Gapon crhold(newcred); 2399f87beb93SAndriy Gapon #endif 2400f87beb93SAndriy Gapon PROC_UNLOCK(p); 2401f87beb93SAndriy Gapon #ifdef RCTL 2402f87beb93SAndriy Gapon rctl_proc_ucred_changed(p, newcred); 2403f87beb93SAndriy Gapon crfree(newcred); 2404097055e2SEdward Tomasz Napierala #endif 24051fb6767dSJamie Gritton prison_deref(oldcred->cr_prison, PD_DEREF | PD_DEUREF); 2406fd7a8150SMike Barcroft crfree(oldcred); 2407fd7a8150SMike Barcroft return (0); 24081fb6767dSJamie Gritton 2409fd7a8150SMike Barcroft e_unlock: 241022db15c0SAttilio Rao VOP_UNLOCK(pr->pr_root, 0); 2411b38ff370SJamie Gritton e_revert_osd: 2412b38ff370SJamie Gritton /* Tell modules this thread is still in its old jail after all. */ 24131fb6767dSJamie Gritton (void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td); 2414b38ff370SJamie Gritton prison_deref(pr, PD_DEREF | PD_DEUREF); 2415fd7a8150SMike Barcroft return (error); 2416fd7a8150SMike Barcroft } 2417fd7a8150SMike Barcroft 24180304c731SJamie Gritton 2419fd7a8150SMike Barcroft /* 2420fd7a8150SMike Barcroft * Returns a locked prison instance, or NULL on failure. 2421fd7a8150SMike Barcroft */ 242254b369c1SPawel Jakub Dawidek struct prison * 2423fd7a8150SMike Barcroft prison_find(int prid) 2424fd7a8150SMike Barcroft { 2425fd7a8150SMike Barcroft struct prison *pr; 2426fd7a8150SMike Barcroft 2427dc68a633SPawel Jakub Dawidek sx_assert(&allprison_lock, SX_LOCKED); 2428b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 2429fd7a8150SMike Barcroft if (pr->pr_id == prid) { 2430fd7a8150SMike Barcroft mtx_lock(&pr->pr_mtx); 2431b38ff370SJamie Gritton if (pr->pr_ref > 0) 2432fd7a8150SMike Barcroft return (pr); 2433b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2434fd7a8150SMike Barcroft } 2435fd7a8150SMike Barcroft } 2436fd7a8150SMike Barcroft return (NULL); 2437fd7a8150SMike Barcroft } 2438fd7a8150SMike Barcroft 2439b38ff370SJamie Gritton /* 24400304c731SJamie Gritton * Find a prison that is a descendant of mypr. Returns a locked prison or NULL. 2441b38ff370SJamie Gritton */ 2442b38ff370SJamie Gritton struct prison * 24430304c731SJamie Gritton prison_find_child(struct prison *mypr, int prid) 2444b38ff370SJamie Gritton { 24450304c731SJamie Gritton struct prison *pr; 24460304c731SJamie Gritton int descend; 2447b38ff370SJamie Gritton 2448b38ff370SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 24490304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 24500304c731SJamie Gritton if (pr->pr_id == prid) { 24510304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 24520304c731SJamie Gritton if (pr->pr_ref > 0) 24530304c731SJamie Gritton return (pr); 24540304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 24550304c731SJamie Gritton } 24560304c731SJamie Gritton } 24570304c731SJamie Gritton return (NULL); 24580304c731SJamie Gritton } 24590304c731SJamie Gritton 24600304c731SJamie Gritton /* 24610304c731SJamie Gritton * Look for the name relative to mypr. Returns a locked prison or NULL. 24620304c731SJamie Gritton */ 24630304c731SJamie Gritton struct prison * 24640304c731SJamie Gritton prison_find_name(struct prison *mypr, const char *name) 24650304c731SJamie Gritton { 24660304c731SJamie Gritton struct prison *pr, *deadpr; 24670304c731SJamie Gritton size_t mylen; 24680304c731SJamie Gritton int descend; 24690304c731SJamie Gritton 24700304c731SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 24710304c731SJamie Gritton mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1; 2472b38ff370SJamie Gritton again: 2473b38ff370SJamie Gritton deadpr = NULL; 24740304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 24750304c731SJamie Gritton if (!strcmp(pr->pr_name + mylen, name)) { 2476b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 2477b38ff370SJamie Gritton if (pr->pr_ref > 0) { 2478b38ff370SJamie Gritton if (pr->pr_uref > 0) 2479b38ff370SJamie Gritton return (pr); 2480b38ff370SJamie Gritton deadpr = pr; 2481b38ff370SJamie Gritton } 2482b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2483b38ff370SJamie Gritton } 2484b38ff370SJamie Gritton } 24850304c731SJamie Gritton /* There was no valid prison - perhaps there was a dying one. */ 2486b38ff370SJamie Gritton if (deadpr != NULL) { 2487b38ff370SJamie Gritton mtx_lock(&deadpr->pr_mtx); 2488b38ff370SJamie Gritton if (deadpr->pr_ref == 0) { 2489b38ff370SJamie Gritton mtx_unlock(&deadpr->pr_mtx); 2490b38ff370SJamie Gritton goto again; 2491b38ff370SJamie Gritton } 2492b38ff370SJamie Gritton } 2493b38ff370SJamie Gritton return (deadpr); 2494b38ff370SJamie Gritton } 2495b38ff370SJamie Gritton 2496b38ff370SJamie Gritton /* 24970304c731SJamie Gritton * See if a prison has the specific flag set. 24980304c731SJamie Gritton */ 24990304c731SJamie Gritton int 25000304c731SJamie Gritton prison_flag(struct ucred *cred, unsigned flag) 25010304c731SJamie Gritton { 25020304c731SJamie Gritton 25030304c731SJamie Gritton /* This is an atomic read, so no locking is necessary. */ 25040304c731SJamie Gritton return (cred->cr_prison->pr_flags & flag); 25050304c731SJamie Gritton } 25060304c731SJamie Gritton 25070304c731SJamie Gritton int 25080304c731SJamie Gritton prison_allow(struct ucred *cred, unsigned flag) 25090304c731SJamie Gritton { 25100304c731SJamie Gritton 25110304c731SJamie Gritton /* This is an atomic read, so no locking is necessary. */ 25120304c731SJamie Gritton return (cred->cr_prison->pr_allow & flag); 25130304c731SJamie Gritton } 25140304c731SJamie Gritton 25150304c731SJamie Gritton /* 2516b38ff370SJamie Gritton * Remove a prison reference. If that was the last reference, remove the 2517b38ff370SJamie Gritton * prison itself - but not in this context in case there are locks held. 2518b38ff370SJamie Gritton */ 251991421ba2SRobert Watson void 25201ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr) 252191421ba2SRobert Watson { 252273d9e52dSJamie Gritton int ref; 252391421ba2SRobert Watson 25241ba4a712SPawel Jakub Dawidek mtx_assert(&pr->pr_mtx, MA_OWNED); 252573d9e52dSJamie Gritton ref = --pr->pr_ref; 252601137630SRobert Watson mtx_unlock(&pr->pr_mtx); 252773d9e52dSJamie Gritton if (ref == 0) 2528c2cda609SPawel Jakub Dawidek taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 2529c2cda609SPawel Jakub Dawidek } 2530c2cda609SPawel Jakub Dawidek 25311ba4a712SPawel Jakub Dawidek void 25321ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr) 25331ba4a712SPawel Jakub Dawidek { 25341ba4a712SPawel Jakub Dawidek 25351ba4a712SPawel Jakub Dawidek mtx_lock(&pr->pr_mtx); 25361ba4a712SPawel Jakub Dawidek prison_free_locked(pr); 25371ba4a712SPawel Jakub Dawidek } 25381ba4a712SPawel Jakub Dawidek 253973d9e52dSJamie Gritton /* 254073d9e52dSJamie Gritton * Complete a call to either prison_free or prison_proc_free. 254173d9e52dSJamie Gritton */ 2542c2cda609SPawel Jakub Dawidek static void 2543c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending) 2544c2cda609SPawel Jakub Dawidek { 254573d9e52dSJamie Gritton struct prison *pr = context; 2546b38ff370SJamie Gritton 2547ef0ddea3SJamie Gritton sx_xlock(&allprison_lock); 254873d9e52dSJamie Gritton mtx_lock(&pr->pr_mtx); 254973d9e52dSJamie Gritton prison_deref(pr, pr->pr_uref 2550ef0ddea3SJamie Gritton ? PD_DEREF | PD_DEUREF | PD_LOCKED | PD_LIST_XLOCKED 2551ef0ddea3SJamie Gritton : PD_LOCKED | PD_LIST_XLOCKED); 2552b38ff370SJamie Gritton } 2553b38ff370SJamie Gritton 2554b38ff370SJamie Gritton /* 2555b38ff370SJamie Gritton * Remove a prison reference (usually). This internal version assumes no 2556b38ff370SJamie Gritton * mutexes are held, except perhaps the prison itself. If there are no more 2557b38ff370SJamie Gritton * references, release and delist the prison. On completion, the prison lock 2558b38ff370SJamie Gritton * and the allprison lock are both unlocked. 2559b38ff370SJamie Gritton */ 2560b38ff370SJamie Gritton static void 2561b38ff370SJamie Gritton prison_deref(struct prison *pr, int flags) 2562b38ff370SJamie Gritton { 25630304c731SJamie Gritton struct prison *ppr, *tpr; 2564cc5fd8c7SJamie Gritton int ref, lasturef; 2565c2cda609SPawel Jakub Dawidek 2566b38ff370SJamie Gritton if (!(flags & PD_LOCKED)) 2567b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 25680304c731SJamie Gritton for (;;) { 2569e6d5cb63SJamie Gritton if (flags & PD_DEUREF) { 257073d9e52dSJamie Gritton KASSERT(pr->pr_uref > 0, 257173d9e52dSJamie Gritton ("prison_deref PD_DEUREF on a dead prison (jid=%d)", 257273d9e52dSJamie Gritton pr->pr_id)); 2573e6d5cb63SJamie Gritton pr->pr_uref--; 2574cc5fd8c7SJamie Gritton lasturef = pr->pr_uref == 0; 2575cc5fd8c7SJamie Gritton if (lasturef) 2576cc5fd8c7SJamie Gritton pr->pr_ref++; 2577e6d5cb63SJamie Gritton KASSERT(prison0.pr_uref != 0, ("prison0 pr_uref=0")); 2578cc5fd8c7SJamie Gritton } else 2579cc5fd8c7SJamie Gritton lasturef = 0; 258073d9e52dSJamie Gritton if (flags & PD_DEREF) { 258173d9e52dSJamie Gritton KASSERT(pr->pr_ref > 0, 258273d9e52dSJamie Gritton ("prison_deref PD_DEREF on a dead prison (jid=%d)", 258373d9e52dSJamie Gritton pr->pr_id)); 2584b38ff370SJamie Gritton pr->pr_ref--; 258573d9e52dSJamie Gritton } 2586cc5fd8c7SJamie Gritton ref = pr->pr_ref; 2587b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2588cc5fd8c7SJamie Gritton 2589cc5fd8c7SJamie Gritton /* 2590cc5fd8c7SJamie Gritton * Tell the modules if the last user reference was removed 2591cc5fd8c7SJamie Gritton * (even it sticks around in dying state). 2592cc5fd8c7SJamie Gritton */ 2593cc5fd8c7SJamie Gritton if (lasturef) { 2594cc5fd8c7SJamie Gritton if (!(flags & (PD_LIST_SLOCKED | PD_LIST_XLOCKED))) { 2595cc5fd8c7SJamie Gritton sx_xlock(&allprison_lock); 2596cc5fd8c7SJamie Gritton flags |= PD_LIST_XLOCKED; 2597cc5fd8c7SJamie Gritton } 2598cc5fd8c7SJamie Gritton (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL); 2599cc5fd8c7SJamie Gritton mtx_lock(&pr->pr_mtx); 2600cc5fd8c7SJamie Gritton ref = --pr->pr_ref; 2601cc5fd8c7SJamie Gritton mtx_unlock(&pr->pr_mtx); 2602cc5fd8c7SJamie Gritton } 2603cc5fd8c7SJamie Gritton 2604cc5fd8c7SJamie Gritton /* If the prison still has references, nothing else to do. */ 2605cc5fd8c7SJamie Gritton if (ref > 0) { 2606b38ff370SJamie Gritton if (flags & PD_LIST_SLOCKED) 2607b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2608b38ff370SJamie Gritton else if (flags & PD_LIST_XLOCKED) 2609b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2610b38ff370SJamie Gritton return; 2611b38ff370SJamie Gritton } 2612c2cda609SPawel Jakub Dawidek 2613b38ff370SJamie Gritton if (flags & PD_LIST_SLOCKED) { 2614b38ff370SJamie Gritton if (!sx_try_upgrade(&allprison_lock)) { 2615b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2616c2cda609SPawel Jakub Dawidek sx_xlock(&allprison_lock); 2617b38ff370SJamie Gritton } 2618b38ff370SJamie Gritton } else if (!(flags & PD_LIST_XLOCKED)) 2619b38ff370SJamie Gritton sx_xlock(&allprison_lock); 2620b38ff370SJamie Gritton 2621b38ff370SJamie Gritton TAILQ_REMOVE(&allprison, pr, pr_list); 26220304c731SJamie Gritton LIST_REMOVE(pr, pr_sibling); 26230304c731SJamie Gritton ppr = pr->pr_parent; 26240304c731SJamie Gritton for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 2625b97457e2SJamie Gritton tpr->pr_childcount--; 2626c4884ffaSJamie Gritton sx_xunlock(&allprison_lock); 26271ba4a712SPawel Jakub Dawidek 2628679e1390SJamie Gritton #ifdef VIMAGE 26290cb8b6a9SMarko Zec if (pr->pr_vnet != ppr->pr_vnet) 2630679e1390SJamie Gritton vnet_destroy(pr->pr_vnet); 2631679e1390SJamie Gritton #endif 26325050aa86SKonstantin Belousov if (pr->pr_root != NULL) 2633b3059e09SRobert Watson vrele(pr->pr_root); 2634b3059e09SRobert Watson mtx_destroy(&pr->pr_mtx); 2635413628a7SBjoern A. Zeeb #ifdef INET 2636413628a7SBjoern A. Zeeb free(pr->pr_ip4, M_PRISON); 2637413628a7SBjoern A. Zeeb #endif 2638b38ff370SJamie Gritton #ifdef INET6 2639b38ff370SJamie Gritton free(pr->pr_ip6, M_PRISON); 2640b38ff370SJamie Gritton #endif 2641b38ff370SJamie Gritton if (pr->pr_cpuset != NULL) 2642b38ff370SJamie Gritton cpuset_rel(pr->pr_cpuset); 2643b38ff370SJamie Gritton osd_jail_exit(pr); 2644a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 26454b5c9cf6SEdward Tomasz Napierala if (racct_enable) 2646a7ad07bfSEdward Tomasz Napierala prison_racct_detach(pr); 2647ec125fbbSEdward Tomasz Napierala #endif 26481ede983cSDag-Erling Smørgrav free(pr, M_PRISON); 26490304c731SJamie Gritton 26500304c731SJamie Gritton /* Removing a prison frees a reference on its parent. */ 26510304c731SJamie Gritton pr = ppr; 26520304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 2653e6d5cb63SJamie Gritton flags = PD_DEREF | PD_DEUREF; 26540304c731SJamie Gritton } 2655b3059e09SRobert Watson } 2656b3059e09SRobert Watson 265791421ba2SRobert Watson void 26581ba4a712SPawel Jakub Dawidek prison_hold_locked(struct prison *pr) 26591ba4a712SPawel Jakub Dawidek { 26601ba4a712SPawel Jakub Dawidek 26611ba4a712SPawel Jakub Dawidek mtx_assert(&pr->pr_mtx, MA_OWNED); 26621ba4a712SPawel Jakub Dawidek KASSERT(pr->pr_ref > 0, 266336b41cc3SBjoern A. Zeeb ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id)); 26641ba4a712SPawel Jakub Dawidek pr->pr_ref++; 26651ba4a712SPawel Jakub Dawidek } 26661ba4a712SPawel Jakub Dawidek 26671ba4a712SPawel Jakub Dawidek void 266891421ba2SRobert Watson prison_hold(struct prison *pr) 266991421ba2SRobert Watson { 267091421ba2SRobert Watson 267101137630SRobert Watson mtx_lock(&pr->pr_mtx); 26721ba4a712SPawel Jakub Dawidek prison_hold_locked(pr); 267301137630SRobert Watson mtx_unlock(&pr->pr_mtx); 267401137630SRobert Watson } 267501137630SRobert Watson 2676413628a7SBjoern A. Zeeb void 2677413628a7SBjoern A. Zeeb prison_proc_hold(struct prison *pr) 267801137630SRobert Watson { 267901137630SRobert Watson 2680413628a7SBjoern A. Zeeb mtx_lock(&pr->pr_mtx); 2681b38ff370SJamie Gritton KASSERT(pr->pr_uref > 0, 2682b38ff370SJamie Gritton ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id)); 2683b38ff370SJamie Gritton pr->pr_uref++; 2684413628a7SBjoern A. Zeeb mtx_unlock(&pr->pr_mtx); 268575c13541SPoul-Henning Kamp } 268675c13541SPoul-Henning Kamp 268775c13541SPoul-Henning Kamp void 2688413628a7SBjoern A. Zeeb prison_proc_free(struct prison *pr) 268975c13541SPoul-Henning Kamp { 2690413628a7SBjoern A. Zeeb 2691413628a7SBjoern A. Zeeb mtx_lock(&pr->pr_mtx); 2692b38ff370SJamie Gritton KASSERT(pr->pr_uref > 0, 2693b38ff370SJamie Gritton ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id)); 269473d9e52dSJamie Gritton if (pr->pr_uref > 1) 269573d9e52dSJamie Gritton pr->pr_uref--; 269673d9e52dSJamie Gritton else { 269773d9e52dSJamie Gritton /* 269873d9e52dSJamie Gritton * Don't remove the last user reference in this context, which 269973d9e52dSJamie Gritton * is expected to be a process that is not only locked, but 270073d9e52dSJamie Gritton * also half dead. 270173d9e52dSJamie Gritton */ 270273d9e52dSJamie Gritton pr->pr_ref++; 270373d9e52dSJamie Gritton mtx_unlock(&pr->pr_mtx); 270473d9e52dSJamie Gritton taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 270573d9e52dSJamie Gritton return; 270673d9e52dSJamie Gritton } 270773d9e52dSJamie Gritton mtx_unlock(&pr->pr_mtx); 2708413628a7SBjoern A. Zeeb } 2709413628a7SBjoern A. Zeeb 2710413628a7SBjoern A. Zeeb /* 2711ca04ba64SJamie Gritton * Check if a jail supports the given address family. 2712ca04ba64SJamie Gritton * 2713ca04ba64SJamie Gritton * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT 2714ca04ba64SJamie Gritton * if not. 2715ca04ba64SJamie Gritton */ 2716ca04ba64SJamie Gritton int 2717ca04ba64SJamie Gritton prison_check_af(struct ucred *cred, int af) 2718ca04ba64SJamie Gritton { 27190304c731SJamie Gritton struct prison *pr; 2720ca04ba64SJamie Gritton int error; 2721ca04ba64SJamie Gritton 2722ca04ba64SJamie Gritton KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2723ca04ba64SJamie Gritton 27240304c731SJamie Gritton pr = cred->cr_prison; 2725499650a0SJamie Gritton #ifdef VIMAGE 27266bb79563SJamie Gritton /* Prisons with their own network stack are not limited. */ 2727de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 27286bb79563SJamie Gritton return (0); 2729499650a0SJamie Gritton #endif 27306bb79563SJamie Gritton 2731ca04ba64SJamie Gritton error = 0; 2732ca04ba64SJamie Gritton switch (af) 2733ca04ba64SJamie Gritton { 2734ca04ba64SJamie Gritton #ifdef INET 2735ca04ba64SJamie Gritton case AF_INET: 27360304c731SJamie Gritton if (pr->pr_flags & PR_IP4) 27370304c731SJamie Gritton { 27380304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 27390304c731SJamie Gritton if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL) 2740ca04ba64SJamie Gritton error = EAFNOSUPPORT; 27410304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 27420304c731SJamie Gritton } 2743ca04ba64SJamie Gritton break; 2744ca04ba64SJamie Gritton #endif 2745ca04ba64SJamie Gritton #ifdef INET6 2746ca04ba64SJamie Gritton case AF_INET6: 27470304c731SJamie Gritton if (pr->pr_flags & PR_IP6) 27480304c731SJamie Gritton { 27490304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 27500304c731SJamie Gritton if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL) 2751ca04ba64SJamie Gritton error = EAFNOSUPPORT; 27520304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 27530304c731SJamie Gritton } 2754ca04ba64SJamie Gritton break; 2755ca04ba64SJamie Gritton #endif 2756ca04ba64SJamie Gritton case AF_LOCAL: 2757ca04ba64SJamie Gritton case AF_ROUTE: 2758ca04ba64SJamie Gritton break; 2759ca04ba64SJamie Gritton default: 27600304c731SJamie Gritton if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF)) 2761ca04ba64SJamie Gritton error = EAFNOSUPPORT; 2762ca04ba64SJamie Gritton } 2763ca04ba64SJamie Gritton return (error); 2764ca04ba64SJamie Gritton } 2765ca04ba64SJamie Gritton 2766ca04ba64SJamie Gritton /* 2767413628a7SBjoern A. Zeeb * Check if given address belongs to the jail referenced by cred (wrapper to 2768413628a7SBjoern A. Zeeb * prison_check_ip[46]). 2769413628a7SBjoern A. Zeeb * 27700304c731SJamie Gritton * Returns 0 if jail doesn't restrict the address family or if address belongs 27710304c731SJamie Gritton * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if 27720304c731SJamie Gritton * the jail doesn't allow the address family. IPv4 Address passed in in NBO. 2773413628a7SBjoern A. Zeeb */ 2774413628a7SBjoern A. Zeeb int 277591421ba2SRobert Watson prison_if(struct ucred *cred, struct sockaddr *sa) 277675c13541SPoul-Henning Kamp { 2777413628a7SBjoern A. Zeeb #ifdef INET 27789ddb7954SMike Barcroft struct sockaddr_in *sai; 2779413628a7SBjoern A. Zeeb #endif 2780413628a7SBjoern A. Zeeb #ifdef INET6 2781413628a7SBjoern A. Zeeb struct sockaddr_in6 *sai6; 2782413628a7SBjoern A. Zeeb #endif 2783b89e82ddSJamie Gritton int error; 278475c13541SPoul-Henning Kamp 2785413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2786413628a7SBjoern A. Zeeb KASSERT(sa != NULL, ("%s: sa is NULL", __func__)); 2787413628a7SBjoern A. Zeeb 2788de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE 2789de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 2790de0bd6f7SBjoern A. Zeeb return (0); 2791de0bd6f7SBjoern A. Zeeb #endif 2792de0bd6f7SBjoern A. Zeeb 2793b89e82ddSJamie Gritton error = 0; 2794413628a7SBjoern A. Zeeb switch (sa->sa_family) 2795413628a7SBjoern A. Zeeb { 2796413628a7SBjoern A. Zeeb #ifdef INET 2797413628a7SBjoern A. Zeeb case AF_INET: 27989ddb7954SMike Barcroft sai = (struct sockaddr_in *)sa; 2799b89e82ddSJamie Gritton error = prison_check_ip4(cred, &sai->sin_addr); 2800413628a7SBjoern A. Zeeb break; 2801413628a7SBjoern A. Zeeb #endif 2802413628a7SBjoern A. Zeeb #ifdef INET6 2803413628a7SBjoern A. Zeeb case AF_INET6: 2804413628a7SBjoern A. Zeeb sai6 = (struct sockaddr_in6 *)sa; 2805b89e82ddSJamie Gritton error = prison_check_ip6(cred, &sai6->sin6_addr); 2806413628a7SBjoern A. Zeeb break; 2807413628a7SBjoern A. Zeeb #endif 2808413628a7SBjoern A. Zeeb default: 28090304c731SJamie Gritton if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF)) 2810b89e82ddSJamie Gritton error = EAFNOSUPPORT; 2811413628a7SBjoern A. Zeeb } 2812b89e82ddSJamie Gritton return (error); 281375c13541SPoul-Henning Kamp } 281491421ba2SRobert Watson 281591421ba2SRobert Watson /* 281691421ba2SRobert Watson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH. 281791421ba2SRobert Watson */ 281891421ba2SRobert Watson int 28199ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2) 282091421ba2SRobert Watson { 282191421ba2SRobert Watson 28220304c731SJamie Gritton return ((cred1->cr_prison == cred2->cr_prison || 28230304c731SJamie Gritton prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH); 28240304c731SJamie Gritton } 282591421ba2SRobert Watson 28260304c731SJamie Gritton /* 28270304c731SJamie Gritton * Return 1 if p2 is a child of p1, otherwise 0. 28280304c731SJamie Gritton */ 28290304c731SJamie Gritton int 28300304c731SJamie Gritton prison_ischild(struct prison *pr1, struct prison *pr2) 28310304c731SJamie Gritton { 28320304c731SJamie Gritton 28330304c731SJamie Gritton for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent) 28340304c731SJamie Gritton if (pr1 == pr2) 28350304c731SJamie Gritton return (1); 283691421ba2SRobert Watson return (0); 283791421ba2SRobert Watson } 283891421ba2SRobert Watson 283991421ba2SRobert Watson /* 284091421ba2SRobert Watson * Return 1 if the passed credential is in a jail, otherwise 0. 284191421ba2SRobert Watson */ 284291421ba2SRobert Watson int 28439ddb7954SMike Barcroft jailed(struct ucred *cred) 284491421ba2SRobert Watson { 284591421ba2SRobert Watson 28460304c731SJamie Gritton return (cred->cr_prison != &prison0); 284791421ba2SRobert Watson } 28489484d0c0SRobert Drehmel 28499484d0c0SRobert Drehmel /* 2850de0bd6f7SBjoern A. Zeeb * Return 1 if the passed credential is in a jail and that jail does not 2851de0bd6f7SBjoern A. Zeeb * have its own virtual network stack, otherwise 0. 2852de0bd6f7SBjoern A. Zeeb */ 2853de0bd6f7SBjoern A. Zeeb int 2854de0bd6f7SBjoern A. Zeeb jailed_without_vnet(struct ucred *cred) 2855de0bd6f7SBjoern A. Zeeb { 2856de0bd6f7SBjoern A. Zeeb 2857de0bd6f7SBjoern A. Zeeb if (!jailed(cred)) 2858de0bd6f7SBjoern A. Zeeb return (0); 2859de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE 2860de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 2861de0bd6f7SBjoern A. Zeeb return (0); 2862de0bd6f7SBjoern A. Zeeb #endif 2863de0bd6f7SBjoern A. Zeeb 2864de0bd6f7SBjoern A. Zeeb return (1); 2865de0bd6f7SBjoern A. Zeeb } 2866de0bd6f7SBjoern A. Zeeb 2867de0bd6f7SBjoern A. Zeeb /* 28687455b100SJamie Gritton * Return the correct hostname (domainname, et al) for the passed credential. 28699484d0c0SRobert Drehmel */ 2870ad1ff099SRobert Drehmel void 28719ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size) 28729484d0c0SRobert Drehmel { 287376ca6f88SJamie Gritton struct prison *pr; 28749484d0c0SRobert Drehmel 28757455b100SJamie Gritton /* 28767455b100SJamie Gritton * A NULL credential can be used to shortcut to the physical 28777455b100SJamie Gritton * system's hostname. 28787455b100SJamie Gritton */ 287976ca6f88SJamie Gritton pr = (cred != NULL) ? cred->cr_prison : &prison0; 288076ca6f88SJamie Gritton mtx_lock(&pr->pr_mtx); 2881c1f19219SJamie Gritton strlcpy(buf, pr->pr_hostname, size); 288276ca6f88SJamie Gritton mtx_unlock(&pr->pr_mtx); 28839484d0c0SRobert Drehmel } 2884fd7a8150SMike Barcroft 28857455b100SJamie Gritton void 28867455b100SJamie Gritton getcreddomainname(struct ucred *cred, char *buf, size_t size) 28877455b100SJamie Gritton { 28887455b100SJamie Gritton 28897455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 2890c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_domainname, size); 28917455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 28927455b100SJamie Gritton } 28937455b100SJamie Gritton 28947455b100SJamie Gritton void 28957455b100SJamie Gritton getcredhostuuid(struct ucred *cred, char *buf, size_t size) 28967455b100SJamie Gritton { 28977455b100SJamie Gritton 28987455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 2899c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_hostuuid, size); 29007455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 29017455b100SJamie Gritton } 29027455b100SJamie Gritton 29037455b100SJamie Gritton void 29047455b100SJamie Gritton getcredhostid(struct ucred *cred, unsigned long *hostid) 29057455b100SJamie Gritton { 29067455b100SJamie Gritton 29077455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 29087455b100SJamie Gritton *hostid = cred->cr_prison->pr_hostid; 29097455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 29107455b100SJamie Gritton } 29117455b100SJamie Gritton 2912eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE 2913eb79e1c7SBjoern A. Zeeb /* 2914eb79e1c7SBjoern A. Zeeb * Determine whether the prison represented by cred owns 2915eb79e1c7SBjoern A. Zeeb * its vnet rather than having it inherited. 2916eb79e1c7SBjoern A. Zeeb * 2917eb79e1c7SBjoern A. Zeeb * Returns 1 in case the prison owns the vnet, 0 otherwise. 2918eb79e1c7SBjoern A. Zeeb */ 2919eb79e1c7SBjoern A. Zeeb int 2920eb79e1c7SBjoern A. Zeeb prison_owns_vnet(struct ucred *cred) 2921eb79e1c7SBjoern A. Zeeb { 2922eb79e1c7SBjoern A. Zeeb 2923eb79e1c7SBjoern A. Zeeb /* 2924eb79e1c7SBjoern A. Zeeb * vnets cannot be added/removed after jail creation, 2925eb79e1c7SBjoern A. Zeeb * so no need to lock here. 2926eb79e1c7SBjoern A. Zeeb */ 2927eb79e1c7SBjoern A. Zeeb return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0); 2928eb79e1c7SBjoern A. Zeeb } 2929eb79e1c7SBjoern A. Zeeb #endif 2930eb79e1c7SBjoern A. Zeeb 2931f08df373SRobert Watson /* 2932820a0de9SPawel Jakub Dawidek * Determine whether the subject represented by cred can "see" 2933820a0de9SPawel Jakub Dawidek * status of a mount point. 2934820a0de9SPawel Jakub Dawidek * Returns: 0 for permitted, ENOENT otherwise. 2935820a0de9SPawel Jakub Dawidek * XXX: This function should be called cr_canseemount() and should be 2936820a0de9SPawel Jakub Dawidek * placed in kern_prot.c. 2937f08df373SRobert Watson */ 2938f08df373SRobert Watson int 2939820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp) 2940f08df373SRobert Watson { 2941820a0de9SPawel Jakub Dawidek struct prison *pr; 2942820a0de9SPawel Jakub Dawidek struct statfs *sp; 2943820a0de9SPawel Jakub Dawidek size_t len; 2944f08df373SRobert Watson 2945820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 29460304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 29470304c731SJamie Gritton return (0); 2948820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) 2949820a0de9SPawel Jakub Dawidek return (0); 29500304c731SJamie Gritton if (pr->pr_enforce_statfs == 2) 2951820a0de9SPawel Jakub Dawidek return (ENOENT); 2952820a0de9SPawel Jakub Dawidek /* 2953820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 2954820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 2955820a0de9SPawel Jakub Dawidek * This is ugly check, but this is the only situation when jail's 2956820a0de9SPawel Jakub Dawidek * directory ends with '/'. 2957820a0de9SPawel Jakub Dawidek */ 2958820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 2959820a0de9SPawel Jakub Dawidek return (0); 2960820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 2961820a0de9SPawel Jakub Dawidek sp = &mp->mnt_stat; 2962820a0de9SPawel Jakub Dawidek if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0) 2963820a0de9SPawel Jakub Dawidek return (ENOENT); 2964820a0de9SPawel Jakub Dawidek /* 2965820a0de9SPawel Jakub Dawidek * Be sure that we don't have situation where jail's root directory 2966820a0de9SPawel Jakub Dawidek * is "/some/path" and mount point is "/some/pathpath". 2967820a0de9SPawel Jakub Dawidek */ 2968820a0de9SPawel Jakub Dawidek if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/') 2969820a0de9SPawel Jakub Dawidek return (ENOENT); 2970f08df373SRobert Watson return (0); 2971f08df373SRobert Watson } 2972820a0de9SPawel Jakub Dawidek 2973820a0de9SPawel Jakub Dawidek void 2974820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp) 2975820a0de9SPawel Jakub Dawidek { 2976820a0de9SPawel Jakub Dawidek char jpath[MAXPATHLEN]; 2977820a0de9SPawel Jakub Dawidek struct prison *pr; 2978820a0de9SPawel Jakub Dawidek size_t len; 2979820a0de9SPawel Jakub Dawidek 2980820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 29810304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 29820304c731SJamie Gritton return; 2983820a0de9SPawel Jakub Dawidek if (prison_canseemount(cred, mp) != 0) { 2984820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 2985820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, "[restricted]", 2986820a0de9SPawel Jakub Dawidek sizeof(sp->f_mntonname)); 2987820a0de9SPawel Jakub Dawidek return; 2988820a0de9SPawel Jakub Dawidek } 2989820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) { 2990820a0de9SPawel Jakub Dawidek /* 2991820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 2992820a0de9SPawel Jakub Dawidek * the valid path left there. 2993820a0de9SPawel Jakub Dawidek */ 2994820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 2995820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 2996820a0de9SPawel Jakub Dawidek return; 2997820a0de9SPawel Jakub Dawidek } 2998820a0de9SPawel Jakub Dawidek /* 2999820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 3000820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 3001820a0de9SPawel Jakub Dawidek */ 3002820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 3003820a0de9SPawel Jakub Dawidek return; 3004820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 3005820a0de9SPawel Jakub Dawidek strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath)); 3006820a0de9SPawel Jakub Dawidek /* 3007820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3008820a0de9SPawel Jakub Dawidek * the valid path left there. 3009820a0de9SPawel Jakub Dawidek */ 3010820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3011820a0de9SPawel Jakub Dawidek if (*jpath == '\0') { 3012820a0de9SPawel Jakub Dawidek /* Should never happen. */ 3013820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3014820a0de9SPawel Jakub Dawidek } else { 3015820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname)); 3016820a0de9SPawel Jakub Dawidek } 3017f08df373SRobert Watson } 3018f08df373SRobert Watson 3019800c9408SRobert Watson /* 3020800c9408SRobert Watson * Check with permission for a specific privilege is granted within jail. We 3021800c9408SRobert Watson * have a specific list of accepted privileges; the rest are denied. 3022800c9408SRobert Watson */ 3023800c9408SRobert Watson int 3024800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv) 3025800c9408SRobert Watson { 3026800c9408SRobert Watson 3027800c9408SRobert Watson if (!jailed(cred)) 3028800c9408SRobert Watson return (0); 3029800c9408SRobert Watson 30306bb79563SJamie Gritton #ifdef VIMAGE 30316bb79563SJamie Gritton /* 30326bb79563SJamie Gritton * Privileges specific to prisons with a virtual network stack. 30336bb79563SJamie Gritton * There might be a duplicate entry here in case the privilege 30346bb79563SJamie Gritton * is only granted conditionally in the legacy jail case. 30356bb79563SJamie Gritton */ 30366bb79563SJamie Gritton switch (priv) { 30376bb79563SJamie Gritton #ifdef notyet 30386bb79563SJamie Gritton /* 30396bb79563SJamie Gritton * NFS-specific privileges. 30406bb79563SJamie Gritton */ 30416bb79563SJamie Gritton case PRIV_NFS_DAEMON: 30426bb79563SJamie Gritton case PRIV_NFS_LOCKD: 30436bb79563SJamie Gritton #endif 30446bb79563SJamie Gritton /* 30456bb79563SJamie Gritton * Network stack privileges. 30466bb79563SJamie Gritton */ 30476bb79563SJamie Gritton case PRIV_NET_BRIDGE: 30486bb79563SJamie Gritton case PRIV_NET_GRE: 30496bb79563SJamie Gritton case PRIV_NET_BPF: 30506bb79563SJamie Gritton case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */ 30516bb79563SJamie Gritton case PRIV_NET_ROUTE: 30526bb79563SJamie Gritton case PRIV_NET_TAP: 30536bb79563SJamie Gritton case PRIV_NET_SETIFMTU: 30546bb79563SJamie Gritton case PRIV_NET_SETIFFLAGS: 30556bb79563SJamie Gritton case PRIV_NET_SETIFCAP: 3056215940b3SXin LI case PRIV_NET_SETIFDESCR: 30576bb79563SJamie Gritton case PRIV_NET_SETIFNAME : 30586bb79563SJamie Gritton case PRIV_NET_SETIFMETRIC: 30596bb79563SJamie Gritton case PRIV_NET_SETIFPHYS: 30606bb79563SJamie Gritton case PRIV_NET_SETIFMAC: 30616bb79563SJamie Gritton case PRIV_NET_ADDMULTI: 30626bb79563SJamie Gritton case PRIV_NET_DELMULTI: 30636bb79563SJamie Gritton case PRIV_NET_HWIOCTL: 30646bb79563SJamie Gritton case PRIV_NET_SETLLADDR: 30656bb79563SJamie Gritton case PRIV_NET_ADDIFGROUP: 30666bb79563SJamie Gritton case PRIV_NET_DELIFGROUP: 30676bb79563SJamie Gritton case PRIV_NET_IFCREATE: 30686bb79563SJamie Gritton case PRIV_NET_IFDESTROY: 30696bb79563SJamie Gritton case PRIV_NET_ADDIFADDR: 30706bb79563SJamie Gritton case PRIV_NET_DELIFADDR: 30716bb79563SJamie Gritton case PRIV_NET_LAGG: 30726bb79563SJamie Gritton case PRIV_NET_GIF: 30736bb79563SJamie Gritton case PRIV_NET_SETIFVNET: 307435fd7bc0SBjoern A. Zeeb case PRIV_NET_SETIFFIB: 30756bb79563SJamie Gritton 30766bb79563SJamie Gritton /* 30776bb79563SJamie Gritton * 802.11-related privileges. 30786bb79563SJamie Gritton */ 30796bb79563SJamie Gritton case PRIV_NET80211_GETKEY: 30806bb79563SJamie Gritton #ifdef notyet 30816bb79563SJamie Gritton case PRIV_NET80211_MANAGE: /* XXX-BZ discuss with sam@ */ 30826bb79563SJamie Gritton #endif 30836bb79563SJamie Gritton 30846bb79563SJamie Gritton #ifdef notyet 30856bb79563SJamie Gritton /* 30866bb79563SJamie Gritton * ATM privileges. 30876bb79563SJamie Gritton */ 30886bb79563SJamie Gritton case PRIV_NETATM_CFG: 30896bb79563SJamie Gritton case PRIV_NETATM_ADD: 30906bb79563SJamie Gritton case PRIV_NETATM_DEL: 30916bb79563SJamie Gritton case PRIV_NETATM_SET: 30926bb79563SJamie Gritton 30936bb79563SJamie Gritton /* 30946bb79563SJamie Gritton * Bluetooth privileges. 30956bb79563SJamie Gritton */ 30966bb79563SJamie Gritton case PRIV_NETBLUETOOTH_RAW: 30976bb79563SJamie Gritton #endif 30986bb79563SJamie Gritton 30996bb79563SJamie Gritton /* 31006bb79563SJamie Gritton * Netgraph and netgraph module privileges. 31016bb79563SJamie Gritton */ 31026bb79563SJamie Gritton case PRIV_NETGRAPH_CONTROL: 31036bb79563SJamie Gritton #ifdef notyet 31046bb79563SJamie Gritton case PRIV_NETGRAPH_TTY: 31056bb79563SJamie Gritton #endif 31066bb79563SJamie Gritton 31076bb79563SJamie Gritton /* 31086bb79563SJamie Gritton * IPv4 and IPv6 privileges. 31096bb79563SJamie Gritton */ 31106bb79563SJamie Gritton case PRIV_NETINET_IPFW: 31116bb79563SJamie Gritton case PRIV_NETINET_DIVERT: 31126bb79563SJamie Gritton case PRIV_NETINET_PF: 31136bb79563SJamie Gritton case PRIV_NETINET_DUMMYNET: 31146bb79563SJamie Gritton case PRIV_NETINET_CARP: 31156bb79563SJamie Gritton case PRIV_NETINET_MROUTE: 31166bb79563SJamie Gritton case PRIV_NETINET_RAW: 31176bb79563SJamie Gritton case PRIV_NETINET_ADDRCTRL6: 31186bb79563SJamie Gritton case PRIV_NETINET_ND6: 31196bb79563SJamie Gritton case PRIV_NETINET_SCOPE6: 31206bb79563SJamie Gritton case PRIV_NETINET_ALIFETIME6: 31216bb79563SJamie Gritton case PRIV_NETINET_IPSEC: 31226bb79563SJamie Gritton case PRIV_NETINET_BINDANY: 31236bb79563SJamie Gritton 31246bb79563SJamie Gritton #ifdef notyet 31256bb79563SJamie Gritton /* 31266bb79563SJamie Gritton * NCP privileges. 31276bb79563SJamie Gritton */ 31286bb79563SJamie Gritton case PRIV_NETNCP: 31296bb79563SJamie Gritton 31306bb79563SJamie Gritton /* 31316bb79563SJamie Gritton * SMB privileges. 31326bb79563SJamie Gritton */ 31336bb79563SJamie Gritton case PRIV_NETSMB: 31346bb79563SJamie Gritton #endif 31356bb79563SJamie Gritton 31366bb79563SJamie Gritton /* 31376bb79563SJamie Gritton * No default: or deny here. 31386bb79563SJamie Gritton * In case of no permit fall through to next switch(). 31396bb79563SJamie Gritton */ 31406bb79563SJamie Gritton if (cred->cr_prison->pr_flags & PR_VNET) 31416bb79563SJamie Gritton return (0); 31426bb79563SJamie Gritton } 31436bb79563SJamie Gritton #endif /* VIMAGE */ 31446bb79563SJamie Gritton 3145800c9408SRobert Watson switch (priv) { 3146800c9408SRobert Watson 3147800c9408SRobert Watson /* 3148800c9408SRobert Watson * Allow ktrace privileges for root in jail. 3149800c9408SRobert Watson */ 3150800c9408SRobert Watson case PRIV_KTRACE: 3151800c9408SRobert Watson 3152c3c1b5e6SRobert Watson #if 0 3153800c9408SRobert Watson /* 3154800c9408SRobert Watson * Allow jailed processes to configure audit identity and 3155800c9408SRobert Watson * submit audit records (login, etc). In the future we may 3156800c9408SRobert Watson * want to further refine the relationship between audit and 3157800c9408SRobert Watson * jail. 3158800c9408SRobert Watson */ 3159800c9408SRobert Watson case PRIV_AUDIT_GETAUDIT: 3160800c9408SRobert Watson case PRIV_AUDIT_SETAUDIT: 3161800c9408SRobert Watson case PRIV_AUDIT_SUBMIT: 3162c3c1b5e6SRobert Watson #endif 3163800c9408SRobert Watson 3164800c9408SRobert Watson /* 3165800c9408SRobert Watson * Allow jailed processes to manipulate process UNIX 3166800c9408SRobert Watson * credentials in any way they see fit. 3167800c9408SRobert Watson */ 3168800c9408SRobert Watson case PRIV_CRED_SETUID: 3169800c9408SRobert Watson case PRIV_CRED_SETEUID: 3170800c9408SRobert Watson case PRIV_CRED_SETGID: 3171800c9408SRobert Watson case PRIV_CRED_SETEGID: 3172800c9408SRobert Watson case PRIV_CRED_SETGROUPS: 3173800c9408SRobert Watson case PRIV_CRED_SETREUID: 3174800c9408SRobert Watson case PRIV_CRED_SETREGID: 3175800c9408SRobert Watson case PRIV_CRED_SETRESUID: 3176800c9408SRobert Watson case PRIV_CRED_SETRESGID: 3177800c9408SRobert Watson 3178800c9408SRobert Watson /* 3179800c9408SRobert Watson * Jail implements visibility constraints already, so allow 3180800c9408SRobert Watson * jailed root to override uid/gid-based constraints. 3181800c9408SRobert Watson */ 3182800c9408SRobert Watson case PRIV_SEEOTHERGIDS: 3183800c9408SRobert Watson case PRIV_SEEOTHERUIDS: 3184800c9408SRobert Watson 3185800c9408SRobert Watson /* 3186800c9408SRobert Watson * Jail implements inter-process debugging limits already, so 3187800c9408SRobert Watson * allow jailed root various debugging privileges. 3188800c9408SRobert Watson */ 3189800c9408SRobert Watson case PRIV_DEBUG_DIFFCRED: 3190800c9408SRobert Watson case PRIV_DEBUG_SUGID: 3191800c9408SRobert Watson case PRIV_DEBUG_UNPRIV: 3192800c9408SRobert Watson 3193800c9408SRobert Watson /* 3194800c9408SRobert Watson * Allow jail to set various resource limits and login 3195800c9408SRobert Watson * properties, and for now, exceed process resource limits. 3196800c9408SRobert Watson */ 3197800c9408SRobert Watson case PRIV_PROC_LIMIT: 3198800c9408SRobert Watson case PRIV_PROC_SETLOGIN: 3199800c9408SRobert Watson case PRIV_PROC_SETRLIMIT: 3200800c9408SRobert Watson 3201800c9408SRobert Watson /* 3202800c9408SRobert Watson * System V and POSIX IPC privileges are granted in jail. 3203800c9408SRobert Watson */ 3204800c9408SRobert Watson case PRIV_IPC_READ: 3205800c9408SRobert Watson case PRIV_IPC_WRITE: 3206800c9408SRobert Watson case PRIV_IPC_ADMIN: 3207800c9408SRobert Watson case PRIV_IPC_MSGSIZE: 3208800c9408SRobert Watson case PRIV_MQ_ADMIN: 3209800c9408SRobert Watson 3210800c9408SRobert Watson /* 32110304c731SJamie Gritton * Jail operations within a jail work on child jails. 32120304c731SJamie Gritton */ 32130304c731SJamie Gritton case PRIV_JAIL_ATTACH: 32140304c731SJamie Gritton case PRIV_JAIL_SET: 32150304c731SJamie Gritton case PRIV_JAIL_REMOVE: 32160304c731SJamie Gritton 32170304c731SJamie Gritton /* 3218800c9408SRobert Watson * Jail implements its own inter-process limits, so allow 3219800c9408SRobert Watson * root processes in jail to change scheduling on other 3220800c9408SRobert Watson * processes in the same jail. Likewise for signalling. 3221800c9408SRobert Watson */ 3222800c9408SRobert Watson case PRIV_SCHED_DIFFCRED: 3223413628a7SBjoern A. Zeeb case PRIV_SCHED_CPUSET: 3224800c9408SRobert Watson case PRIV_SIGNAL_DIFFCRED: 3225800c9408SRobert Watson case PRIV_SIGNAL_SUGID: 3226800c9408SRobert Watson 3227800c9408SRobert Watson /* 3228800c9408SRobert Watson * Allow jailed processes to write to sysctls marked as jail 3229800c9408SRobert Watson * writable. 3230800c9408SRobert Watson */ 3231800c9408SRobert Watson case PRIV_SYSCTL_WRITEJAIL: 3232800c9408SRobert Watson 3233800c9408SRobert Watson /* 3234800c9408SRobert Watson * Allow root in jail to manage a variety of quota 3235e82d0201SRobert Watson * properties. These should likely be conditional on a 3236e82d0201SRobert Watson * configuration option. 3237800c9408SRobert Watson */ 323895b091d2SRobert Watson case PRIV_VFS_GETQUOTA: 323995b091d2SRobert Watson case PRIV_VFS_SETQUOTA: 3240800c9408SRobert Watson 3241800c9408SRobert Watson /* 3242800c9408SRobert Watson * Since Jail relies on chroot() to implement file system 3243800c9408SRobert Watson * protections, grant many VFS privileges to root in jail. 3244800c9408SRobert Watson * Be careful to exclude mount-related and NFS-related 3245800c9408SRobert Watson * privileges. 3246800c9408SRobert Watson */ 3247800c9408SRobert Watson case PRIV_VFS_READ: 3248800c9408SRobert Watson case PRIV_VFS_WRITE: 3249800c9408SRobert Watson case PRIV_VFS_ADMIN: 3250800c9408SRobert Watson case PRIV_VFS_EXEC: 3251800c9408SRobert Watson case PRIV_VFS_LOOKUP: 3252800c9408SRobert Watson case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */ 3253800c9408SRobert Watson case PRIV_VFS_CHFLAGS_DEV: 3254800c9408SRobert Watson case PRIV_VFS_CHOWN: 3255800c9408SRobert Watson case PRIV_VFS_CHROOT: 3256bb531912SPawel Jakub Dawidek case PRIV_VFS_RETAINSUGID: 3257800c9408SRobert Watson case PRIV_VFS_FCHROOT: 3258800c9408SRobert Watson case PRIV_VFS_LINK: 3259800c9408SRobert Watson case PRIV_VFS_SETGID: 3260e41966dcSRobert Watson case PRIV_VFS_STAT: 3261800c9408SRobert Watson case PRIV_VFS_STICKYFILE: 3262bb56d716SJamie Gritton 3263bb56d716SJamie Gritton /* 3264bb56d716SJamie Gritton * As in the non-jail case, non-root users are expected to be 3265bb56d716SJamie Gritton * able to read kernel/phyiscal memory (provided /dev/[k]mem 3266bb56d716SJamie Gritton * exists in the jail and they have permission to access it). 3267bb56d716SJamie Gritton */ 3268bb56d716SJamie Gritton case PRIV_KMEM_READ: 3269800c9408SRobert Watson return (0); 3270800c9408SRobert Watson 3271800c9408SRobert Watson /* 3272800c9408SRobert Watson * Depending on the global setting, allow privilege of 3273800c9408SRobert Watson * setting system flags. 3274800c9408SRobert Watson */ 3275800c9408SRobert Watson case PRIV_VFS_SYSFLAGS: 32760304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS) 3277800c9408SRobert Watson return (0); 3278800c9408SRobert Watson else 3279800c9408SRobert Watson return (EPERM); 3280800c9408SRobert Watson 3281800c9408SRobert Watson /* 3282f3a8d2f9SPawel Jakub Dawidek * Depending on the global setting, allow privilege of 3283f3a8d2f9SPawel Jakub Dawidek * mounting/unmounting file systems. 3284f3a8d2f9SPawel Jakub Dawidek */ 3285f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT: 3286f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_UNMOUNT: 3287f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT_NONUSER: 328824b0502eSPawel Jakub Dawidek case PRIV_VFS_MOUNT_OWNER: 3289435d4667SMartin Matuska if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT && 3290435d4667SMartin Matuska cred->cr_prison->pr_enforce_statfs < 2) 3291f3a8d2f9SPawel Jakub Dawidek return (0); 3292f3a8d2f9SPawel Jakub Dawidek else 3293f3a8d2f9SPawel Jakub Dawidek return (EPERM); 3294f3a8d2f9SPawel Jakub Dawidek 3295f3a8d2f9SPawel Jakub Dawidek /* 3296e28f9b7dSAllan Jude * Conditionally allow jailed root to bind reserved ports. 3297800c9408SRobert Watson */ 3298800c9408SRobert Watson case PRIV_NETINET_RESERVEDPORT: 3299e28f9b7dSAllan Jude if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS) 3300e28f9b7dSAllan Jude return (0); 3301e28f9b7dSAllan Jude else 3302e28f9b7dSAllan Jude return (EPERM); 3303e28f9b7dSAllan Jude 3304e28f9b7dSAllan Jude /* 3305e28f9b7dSAllan Jude * Allow jailed root to reuse in-use ports. 3306e28f9b7dSAllan Jude */ 33074b084056SRobert Watson case PRIV_NETINET_REUSEPORT: 3308800c9408SRobert Watson return (0); 3309800c9408SRobert Watson 3310800c9408SRobert Watson /* 3311e3043798SPedro F. Giffuni * Allow jailed root to set certain IPv4/6 (option) headers. 331279ba3952SBjoern A. Zeeb */ 331379ba3952SBjoern A. Zeeb case PRIV_NETINET_SETHDROPTS: 331479ba3952SBjoern A. Zeeb return (0); 331579ba3952SBjoern A. Zeeb 331679ba3952SBjoern A. Zeeb /* 3317800c9408SRobert Watson * Conditionally allow creating raw sockets in jail. 3318800c9408SRobert Watson */ 3319800c9408SRobert Watson case PRIV_NETINET_RAW: 33200304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS) 3321800c9408SRobert Watson return (0); 3322800c9408SRobert Watson else 3323800c9408SRobert Watson return (EPERM); 3324800c9408SRobert Watson 3325800c9408SRobert Watson /* 3326800c9408SRobert Watson * Since jail implements its own visibility limits on netstat 3327800c9408SRobert Watson * sysctls, allow getcred. This allows identd to work in 3328800c9408SRobert Watson * jail. 3329800c9408SRobert Watson */ 3330800c9408SRobert Watson case PRIV_NETINET_GETCRED: 3331800c9408SRobert Watson return (0); 3332800c9408SRobert Watson 33332bfc50bcSEdward Tomasz Napierala /* 33342bfc50bcSEdward Tomasz Napierala * Allow jailed root to set loginclass. 33352bfc50bcSEdward Tomasz Napierala */ 33362bfc50bcSEdward Tomasz Napierala case PRIV_PROC_SETLOGINCLASS: 33372bfc50bcSEdward Tomasz Napierala return (0); 33382bfc50bcSEdward Tomasz Napierala 3339800c9408SRobert Watson default: 3340800c9408SRobert Watson /* 3341800c9408SRobert Watson * In all remaining cases, deny the privilege request. This 3342800c9408SRobert Watson * includes almost all network privileges, many system 3343800c9408SRobert Watson * configuration privileges. 3344800c9408SRobert Watson */ 3345800c9408SRobert Watson return (EPERM); 3346800c9408SRobert Watson } 3347800c9408SRobert Watson } 3348800c9408SRobert Watson 33490304c731SJamie Gritton /* 33500304c731SJamie Gritton * Return the part of pr2's name that is relative to pr1, or the whole name 33510304c731SJamie Gritton * if it does not directly follow. 33520304c731SJamie Gritton */ 33530304c731SJamie Gritton 33540304c731SJamie Gritton char * 33550304c731SJamie Gritton prison_name(struct prison *pr1, struct prison *pr2) 33560304c731SJamie Gritton { 33570304c731SJamie Gritton char *name; 33580304c731SJamie Gritton 33590304c731SJamie Gritton /* Jails see themselves as "0" (if they see themselves at all). */ 33600304c731SJamie Gritton if (pr1 == pr2) 33610304c731SJamie Gritton return "0"; 33620304c731SJamie Gritton name = pr2->pr_name; 33630304c731SJamie Gritton if (prison_ischild(pr1, pr2)) { 33640304c731SJamie Gritton /* 33650304c731SJamie Gritton * pr1 isn't locked (and allprison_lock may not be either) 33660304c731SJamie Gritton * so its length can't be counted on. But the number of dots 33670304c731SJamie Gritton * can be counted on - and counted. 33680304c731SJamie Gritton */ 33690304c731SJamie Gritton for (; pr1 != &prison0; pr1 = pr1->pr_parent) 33700304c731SJamie Gritton name = strchr(name, '.') + 1; 33710304c731SJamie Gritton } 33720304c731SJamie Gritton return (name); 33730304c731SJamie Gritton } 33740304c731SJamie Gritton 33750304c731SJamie Gritton /* 33760304c731SJamie Gritton * Return the part of pr2's path that is relative to pr1, or the whole path 33770304c731SJamie Gritton * if it does not directly follow. 33780304c731SJamie Gritton */ 33790304c731SJamie Gritton static char * 33800304c731SJamie Gritton prison_path(struct prison *pr1, struct prison *pr2) 33810304c731SJamie Gritton { 33820304c731SJamie Gritton char *path1, *path2; 33830304c731SJamie Gritton int len1; 33840304c731SJamie Gritton 33850304c731SJamie Gritton path1 = pr1->pr_path; 33860304c731SJamie Gritton path2 = pr2->pr_path; 33870304c731SJamie Gritton if (!strcmp(path1, "/")) 33880304c731SJamie Gritton return (path2); 33890304c731SJamie Gritton len1 = strlen(path1); 33900304c731SJamie Gritton if (strncmp(path1, path2, len1)) 33910304c731SJamie Gritton return (path2); 33920304c731SJamie Gritton if (path2[len1] == '\0') 33930304c731SJamie Gritton return "/"; 33940304c731SJamie Gritton if (path2[len1] == '/') 33950304c731SJamie Gritton return (path2 + len1); 33960304c731SJamie Gritton return (path2); 33970304c731SJamie Gritton } 33980304c731SJamie Gritton 33990304c731SJamie Gritton 34000304c731SJamie Gritton /* 34010304c731SJamie Gritton * Jail-related sysctls. 34020304c731SJamie Gritton */ 34036472ac3dSEd Schouten static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0, 34040304c731SJamie Gritton "Jails"); 34050304c731SJamie Gritton 3406fd7a8150SMike Barcroft static int 3407fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS) 3408fd7a8150SMike Barcroft { 3409b38ff370SJamie Gritton struct xprison *xp; 34100304c731SJamie Gritton struct prison *pr, *cpr; 3411b38ff370SJamie Gritton #ifdef INET 3412b38ff370SJamie Gritton struct in_addr *ip4 = NULL; 3413b38ff370SJamie Gritton int ip4s = 0; 3414b38ff370SJamie Gritton #endif 3415b38ff370SJamie Gritton #ifdef INET6 34163beefaedSColin Percival struct in6_addr *ip6 = NULL; 3417b38ff370SJamie Gritton int ip6s = 0; 3418b38ff370SJamie Gritton #endif 34190304c731SJamie Gritton int descend, error; 3420fd7a8150SMike Barcroft 3421b38ff370SJamie Gritton xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK); 34220304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 3423b38ff370SJamie Gritton error = 0; 3424dc68a633SPawel Jakub Dawidek sx_slock(&allprison_lock); 34250304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 34260304c731SJamie Gritton #if defined(INET) || defined(INET6) 3427b38ff370SJamie Gritton again: 34280304c731SJamie Gritton #endif 34290304c731SJamie Gritton mtx_lock(&cpr->pr_mtx); 3430413628a7SBjoern A. Zeeb #ifdef INET 34310304c731SJamie Gritton if (cpr->pr_ip4s > 0) { 34320304c731SJamie Gritton if (ip4s < cpr->pr_ip4s) { 34330304c731SJamie Gritton ip4s = cpr->pr_ip4s; 34340304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 3435b38ff370SJamie Gritton ip4 = realloc(ip4, ip4s * 3436b38ff370SJamie Gritton sizeof(struct in_addr), M_TEMP, M_WAITOK); 3437b38ff370SJamie Gritton goto again; 3438b38ff370SJamie Gritton } 34390304c731SJamie Gritton bcopy(cpr->pr_ip4, ip4, 34400304c731SJamie Gritton cpr->pr_ip4s * sizeof(struct in_addr)); 3441b38ff370SJamie Gritton } 3442413628a7SBjoern A. Zeeb #endif 3443413628a7SBjoern A. Zeeb #ifdef INET6 34440304c731SJamie Gritton if (cpr->pr_ip6s > 0) { 34450304c731SJamie Gritton if (ip6s < cpr->pr_ip6s) { 34460304c731SJamie Gritton ip6s = cpr->pr_ip6s; 34470304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 3448b38ff370SJamie Gritton ip6 = realloc(ip6, ip6s * 3449b38ff370SJamie Gritton sizeof(struct in6_addr), M_TEMP, M_WAITOK); 3450b38ff370SJamie Gritton goto again; 3451413628a7SBjoern A. Zeeb } 34520304c731SJamie Gritton bcopy(cpr->pr_ip6, ip6, 34530304c731SJamie Gritton cpr->pr_ip6s * sizeof(struct in6_addr)); 3454b38ff370SJamie Gritton } 3455b38ff370SJamie Gritton #endif 34560304c731SJamie Gritton if (cpr->pr_ref == 0) { 34570304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 3458b38ff370SJamie Gritton continue; 3459b38ff370SJamie Gritton } 3460b38ff370SJamie Gritton bzero(xp, sizeof(*xp)); 3461fd7a8150SMike Barcroft xp->pr_version = XPRISON_VERSION; 34620304c731SJamie Gritton xp->pr_id = cpr->pr_id; 34630304c731SJamie Gritton xp->pr_state = cpr->pr_uref > 0 3464b38ff370SJamie Gritton ? PRISON_STATE_ALIVE : PRISON_STATE_DYING; 34650304c731SJamie Gritton strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path)); 3466c1f19219SJamie Gritton strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host)); 34670304c731SJamie Gritton strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name)); 3468413628a7SBjoern A. Zeeb #ifdef INET 34690304c731SJamie Gritton xp->pr_ip4s = cpr->pr_ip4s; 3470413628a7SBjoern A. Zeeb #endif 3471413628a7SBjoern A. Zeeb #ifdef INET6 34720304c731SJamie Gritton xp->pr_ip6s = cpr->pr_ip6s; 3473413628a7SBjoern A. Zeeb #endif 34740304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 3475b38ff370SJamie Gritton error = SYSCTL_OUT(req, xp, sizeof(*xp)); 3476b38ff370SJamie Gritton if (error) 3477b38ff370SJamie Gritton break; 3478413628a7SBjoern A. Zeeb #ifdef INET 3479b38ff370SJamie Gritton if (xp->pr_ip4s > 0) { 3480b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip4, 3481b38ff370SJamie Gritton xp->pr_ip4s * sizeof(struct in_addr)); 3482b38ff370SJamie Gritton if (error) 3483b38ff370SJamie Gritton break; 3484413628a7SBjoern A. Zeeb } 3485413628a7SBjoern A. Zeeb #endif 3486413628a7SBjoern A. Zeeb #ifdef INET6 3487b38ff370SJamie Gritton if (xp->pr_ip6s > 0) { 3488b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip6, 3489b38ff370SJamie Gritton xp->pr_ip6s * sizeof(struct in6_addr)); 3490b38ff370SJamie Gritton if (error) 3491b38ff370SJamie Gritton break; 3492413628a7SBjoern A. Zeeb } 3493413628a7SBjoern A. Zeeb #endif 3494fd7a8150SMike Barcroft } 3495dc68a633SPawel Jakub Dawidek sx_sunlock(&allprison_lock); 3496b38ff370SJamie Gritton free(xp, M_TEMP); 3497b38ff370SJamie Gritton #ifdef INET 3498b38ff370SJamie Gritton free(ip4, M_TEMP); 3499b38ff370SJamie Gritton #endif 3500b38ff370SJamie Gritton #ifdef INET6 3501b38ff370SJamie Gritton free(ip6, M_TEMP); 3502b38ff370SJamie Gritton #endif 3503fd7a8150SMike Barcroft return (error); 3504fd7a8150SMike Barcroft } 3505fd7a8150SMike Barcroft 3506f3b86a5fSEd Schouten SYSCTL_OID(_security_jail, OID_AUTO, list, 3507f3b86a5fSEd Schouten CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 3508f3b86a5fSEd Schouten sysctl_jail_list, "S", "List of active jails"); 3509461167c2SPawel Jakub Dawidek 3510461167c2SPawel Jakub Dawidek static int 3511461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS) 3512461167c2SPawel Jakub Dawidek { 3513461167c2SPawel Jakub Dawidek int error, injail; 3514461167c2SPawel Jakub Dawidek 3515461167c2SPawel Jakub Dawidek injail = jailed(req->td->td_ucred); 3516461167c2SPawel Jakub Dawidek error = SYSCTL_OUT(req, &injail, sizeof(injail)); 3517461167c2SPawel Jakub Dawidek 3518461167c2SPawel Jakub Dawidek return (error); 3519461167c2SPawel Jakub Dawidek } 35200304c731SJamie Gritton 3521f3b86a5fSEd Schouten SYSCTL_PROC(_security_jail, OID_AUTO, jailed, 3522f3b86a5fSEd Schouten CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 3523f3b86a5fSEd Schouten sysctl_jail_jailed, "I", "Process in jail?"); 3524413628a7SBjoern A. Zeeb 3525761d2bb5SJamie Gritton static int 3526761d2bb5SJamie Gritton sysctl_jail_vnet(SYSCTL_HANDLER_ARGS) 3527761d2bb5SJamie Gritton { 3528761d2bb5SJamie Gritton int error, havevnet; 3529761d2bb5SJamie Gritton #ifdef VIMAGE 3530761d2bb5SJamie Gritton struct ucred *cred = req->td->td_ucred; 3531761d2bb5SJamie Gritton 3532761d2bb5SJamie Gritton havevnet = jailed(cred) && prison_owns_vnet(cred); 3533761d2bb5SJamie Gritton #else 3534761d2bb5SJamie Gritton havevnet = 0; 3535761d2bb5SJamie Gritton #endif 3536761d2bb5SJamie Gritton error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet)); 3537761d2bb5SJamie Gritton 3538761d2bb5SJamie Gritton return (error); 3539761d2bb5SJamie Gritton } 3540761d2bb5SJamie Gritton 3541761d2bb5SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, vnet, 3542761d2bb5SJamie Gritton CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 3543bb8f1623SBjoern A. Zeeb sysctl_jail_vnet, "I", "Jail owns vnet?"); 3544761d2bb5SJamie Gritton 35450304c731SJamie Gritton #if defined(INET) || defined(INET6) 3546e92e0574SJamie Gritton SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW, 35470304c731SJamie Gritton &jail_max_af_ips, 0, 3548ee8d6bd3SJamie Gritton "Number of IP addresses a jail may have at most per address family (deprecated)"); 35490304c731SJamie Gritton #endif 35500304c731SJamie Gritton 35510304c731SJamie Gritton /* 3552e3043798SPedro F. Giffuni * Default parameters for jail(2) compatibility. For historical reasons, 35530304c731SJamie Gritton * the sysctl names have varying similarity to the parameter names. Prisons 35540304c731SJamie Gritton * just see their own parameters, and can't change them. 35550304c731SJamie Gritton */ 35560304c731SJamie Gritton static int 35570304c731SJamie Gritton sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS) 35580304c731SJamie Gritton { 35590304c731SJamie Gritton struct prison *pr; 35600304c731SJamie Gritton int allow, error, i; 35610304c731SJamie Gritton 35620304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 35630304c731SJamie Gritton allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow; 35640304c731SJamie Gritton 35650304c731SJamie Gritton /* Get the current flag value, and convert it to a boolean. */ 35660304c731SJamie Gritton i = (allow & arg2) ? 1 : 0; 35670304c731SJamie Gritton if (arg1 != NULL) 35680304c731SJamie Gritton i = !i; 35690304c731SJamie Gritton error = sysctl_handle_int(oidp, &i, 0, req); 35700304c731SJamie Gritton if (error || !req->newptr) 35710304c731SJamie Gritton return (error); 35720304c731SJamie Gritton i = i ? arg2 : 0; 35730304c731SJamie Gritton if (arg1 != NULL) 35740304c731SJamie Gritton i ^= arg2; 35750304c731SJamie Gritton /* 35760304c731SJamie Gritton * The sysctls don't have CTLFLAGS_PRISON, so assume prison0 35770304c731SJamie Gritton * for writing. 35780304c731SJamie Gritton */ 35790304c731SJamie Gritton mtx_lock(&prison0.pr_mtx); 35800304c731SJamie Gritton jail_default_allow = (jail_default_allow & ~arg2) | i; 35810304c731SJamie Gritton mtx_unlock(&prison0.pr_mtx); 35820304c731SJamie Gritton return (0); 35830304c731SJamie Gritton } 35840304c731SJamie Gritton 35850304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed, 35860304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 35870304c731SJamie Gritton NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I", 3588ee8d6bd3SJamie Gritton "Processes in jail can set their hostnames (deprecated)"); 35890304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only, 35900304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 35910304c731SJamie Gritton (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I", 3592ee8d6bd3SJamie Gritton "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)"); 35930304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed, 35940304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 35950304c731SJamie Gritton NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I", 3596ee8d6bd3SJamie Gritton "Processes in jail can use System V IPC primitives (deprecated)"); 35970304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets, 35980304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 35990304c731SJamie Gritton NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I", 3600ee8d6bd3SJamie Gritton "Prison root can create raw sockets (deprecated)"); 36010304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed, 36020304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 36030304c731SJamie Gritton NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I", 3604ee8d6bd3SJamie Gritton "Processes in jail can alter system file flags (deprecated)"); 36050304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed, 36060304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 36070304c731SJamie Gritton NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I", 3608ee8d6bd3SJamie Gritton "Processes in jail can mount/unmount jail-friendly file systems (deprecated)"); 36090304c731SJamie Gritton 36100304c731SJamie Gritton static int 36110304c731SJamie Gritton sysctl_jail_default_level(SYSCTL_HANDLER_ARGS) 36120304c731SJamie Gritton { 36130304c731SJamie Gritton struct prison *pr; 36140304c731SJamie Gritton int level, error; 36150304c731SJamie Gritton 36160304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 36170304c731SJamie Gritton level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2); 36180304c731SJamie Gritton error = sysctl_handle_int(oidp, &level, 0, req); 36190304c731SJamie Gritton if (error || !req->newptr) 36200304c731SJamie Gritton return (error); 36210304c731SJamie Gritton *(int *)arg1 = level; 36220304c731SJamie Gritton return (0); 36230304c731SJamie Gritton } 36240304c731SJamie Gritton 36250304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs, 36260304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 36270304c731SJamie Gritton &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs), 36280304c731SJamie Gritton sysctl_jail_default_level, "I", 3629ee8d6bd3SJamie Gritton "Processes in jail cannot see all mounted file systems (deprecated)"); 36300304c731SJamie Gritton 36310cc207a6SMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset, 36320cc207a6SMartin Matuska CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 36330cc207a6SMartin Matuska &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum), 36340cc207a6SMartin Matuska sysctl_jail_default_level, "I", 3635ee8d6bd3SJamie Gritton "Ruleset for the devfs filesystem in jail (deprecated)"); 36360cc207a6SMartin Matuska 36370304c731SJamie Gritton /* 36380304c731SJamie Gritton * Nodes to describe jail parameters. Maximum length of string parameters 36390304c731SJamie Gritton * is returned in the string itself, and the other parameters exist merely 36400304c731SJamie Gritton * to make themselves and their types known. 36410304c731SJamie Gritton */ 36420304c731SJamie Gritton SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0, 36430304c731SJamie Gritton "Jail parameters"); 36440304c731SJamie Gritton 36450304c731SJamie Gritton int 36460304c731SJamie Gritton sysctl_jail_param(SYSCTL_HANDLER_ARGS) 36470304c731SJamie Gritton { 36480304c731SJamie Gritton int i; 36490304c731SJamie Gritton long l; 36500304c731SJamie Gritton size_t s; 36510304c731SJamie Gritton char numbuf[12]; 36520304c731SJamie Gritton 36530304c731SJamie Gritton switch (oidp->oid_kind & CTLTYPE) 36540304c731SJamie Gritton { 36550304c731SJamie Gritton case CTLTYPE_LONG: 36560304c731SJamie Gritton case CTLTYPE_ULONG: 36570304c731SJamie Gritton l = 0; 36580304c731SJamie Gritton #ifdef SCTL_MASK32 36590304c731SJamie Gritton if (!(req->flags & SCTL_MASK32)) 36600304c731SJamie Gritton #endif 36610304c731SJamie Gritton return (SYSCTL_OUT(req, &l, sizeof(l))); 36620304c731SJamie Gritton case CTLTYPE_INT: 36630304c731SJamie Gritton case CTLTYPE_UINT: 36640304c731SJamie Gritton i = 0; 36650304c731SJamie Gritton return (SYSCTL_OUT(req, &i, sizeof(i))); 36660304c731SJamie Gritton case CTLTYPE_STRING: 3667e4cd31ddSJeff Roberson snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2); 36680304c731SJamie Gritton return 36690304c731SJamie Gritton (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req)); 36700304c731SJamie Gritton case CTLTYPE_STRUCT: 36710304c731SJamie Gritton s = (size_t)arg2; 36720304c731SJamie Gritton return (SYSCTL_OUT(req, &s, sizeof(s))); 36730304c731SJamie Gritton } 36740304c731SJamie Gritton return (0); 36750304c731SJamie Gritton } 36760304c731SJamie Gritton 3677b96bd95bSIan Lepore /* 3678b96bd95bSIan Lepore * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at 3679b96bd95bSIan Lepore * jail creation time but cannot be changed in an existing jail. 3680b96bd95bSIan Lepore */ 36810304c731SJamie Gritton SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID"); 36820304c731SJamie Gritton SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID"); 36830304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name"); 36840304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path"); 36850304c731SJamie Gritton SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW, 36860304c731SJamie Gritton "I", "Jail secure level"); 3687b96bd95bSIan Lepore SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I", 3688b96bd95bSIan Lepore "Jail value for kern.osreldate and uname -K"); 3689b96bd95bSIan Lepore SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN, 3690b96bd95bSIan Lepore "Jail value for kern.osrelease and uname -r"); 36910304c731SJamie Gritton SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW, 36920304c731SJamie Gritton "I", "Jail cannot see all mounted file systems"); 36930cc207a6SMartin Matuska SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW, 36940cc207a6SMartin Matuska "I", "Ruleset for in-jail devfs mounts"); 36950304c731SJamie Gritton SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW, 36960304c731SJamie Gritton "B", "Jail persistence"); 3697679e1390SJamie Gritton #ifdef VIMAGE 3698679e1390SJamie Gritton SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN, 36997cbf7213SJamie Gritton "E,jailsys", "Virtual network stack"); 3700679e1390SJamie Gritton #endif 37010304c731SJamie Gritton SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD, 37020304c731SJamie Gritton "B", "Jail is in the process of shutting down"); 37030304c731SJamie Gritton 3704b97457e2SJamie Gritton SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails"); 3705b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD, 3706b97457e2SJamie Gritton "I", "Current number of child jails"); 3707b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW, 3708b97457e2SJamie Gritton "I", "Maximum number of child jails"); 3709b97457e2SJamie Gritton 37107cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info"); 37110304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN, 37120304c731SJamie Gritton "Jail hostname"); 371376ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN, 371476ca6f88SJamie Gritton "Jail NIS domainname"); 371576ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN, 371676ca6f88SJamie Gritton "Jail host UUID"); 371776ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW, 371876ca6f88SJamie Gritton "LU", "Jail host ID"); 37190304c731SJamie Gritton 37200304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset"); 37210304c731SJamie Gritton SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID"); 37220304c731SJamie Gritton 37230304c731SJamie Gritton #ifdef INET 37242b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN, 37252b0d6f81SJamie Gritton "Jail IPv4 address virtualization"); 37260304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr), 37270304c731SJamie Gritton "S,in_addr,a", "Jail IPv4 addresses"); 3728592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 3729592bcae8SBjoern A. Zeeb "B", "Do (not) use IPv4 source address selection rather than the " 3730592bcae8SBjoern A. Zeeb "primary jail IPv4 address."); 37310304c731SJamie Gritton #endif 37320304c731SJamie Gritton #ifdef INET6 37332b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN, 37342b0d6f81SJamie Gritton "Jail IPv6 address virtualization"); 37350304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr), 37360304c731SJamie Gritton "S,in6_addr,a", "Jail IPv6 addresses"); 3737592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 3738592bcae8SBjoern A. Zeeb "B", "Do (not) use IPv6 source address selection rather than the " 3739592bcae8SBjoern A. Zeeb "primary jail IPv6 address."); 37400304c731SJamie Gritton #endif 37410304c731SJamie Gritton 37420304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags"); 37430304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW, 37440304c731SJamie Gritton "B", "Jail may set hostname"); 37450304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW, 37460304c731SJamie Gritton "B", "Jail may use SYSV IPC"); 37470304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW, 37480304c731SJamie Gritton "B", "Jail may create raw sockets"); 37490304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW, 37500304c731SJamie Gritton "B", "Jail may alter system file flags"); 37510304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW, 37520304c731SJamie Gritton "B", "Jail may set file quotas"); 37530304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW, 37540304c731SJamie Gritton "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route"); 3755e28f9b7dSAllan Jude SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW, 3756e28f9b7dSAllan Jude "B", "Jail may bind sockets to reserved ports"); 37570304c731SJamie Gritton 3758bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags"); 3759bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW, 3760bf3db8aaSMartin Matuska "B", "Jail may mount/unmount jail-friendly file systems in general"); 37610e5c6bd4SJamie Gritton 37620e5c6bd4SJamie Gritton /* 37630e5c6bd4SJamie Gritton * The VFS system will register jail-aware filesystems here. They each get 37640e5c6bd4SJamie Gritton * a parameter allow.mount.xxxfs and a flag to check when a jailed user 37650e5c6bd4SJamie Gritton * attempts to mount. 37660e5c6bd4SJamie Gritton */ 37670e5c6bd4SJamie Gritton void 37680e5c6bd4SJamie Gritton prison_add_vfs(struct vfsconf *vfsp) 37690e5c6bd4SJamie Gritton { 37700e5c6bd4SJamie Gritton char *allow_name, *allow_noname, *mount_allowed; 37710e5c6bd4SJamie Gritton struct bool_flags *bf; 37720e5c6bd4SJamie Gritton #ifndef NO_SYSCTL_DESCR 37730e5c6bd4SJamie Gritton char *descr; 37740e5c6bd4SJamie Gritton #endif 37750e5c6bd4SJamie Gritton unsigned allow_flag; 37760e5c6bd4SJamie Gritton 37770e5c6bd4SJamie Gritton if (asprintf(&allow_name, M_PRISON, "allow.mount.%s", vfsp->vfc_name) < 37780e5c6bd4SJamie Gritton 0 || asprintf(&allow_noname, M_PRISON, "allow.mount.no%s", 37790e5c6bd4SJamie Gritton vfsp->vfc_name) < 0) { 37800e5c6bd4SJamie Gritton free(allow_name, M_PRISON); 37810e5c6bd4SJamie Gritton return; 37820e5c6bd4SJamie Gritton } 37830e5c6bd4SJamie Gritton 37840e5c6bd4SJamie Gritton /* 37850e5c6bd4SJamie Gritton * See if this parameter has already beed added, i.e. if the filesystem 37860e5c6bd4SJamie Gritton * was previously loaded/unloaded. 37870e5c6bd4SJamie Gritton */ 37880e5c6bd4SJamie Gritton mtx_lock(&prison0.pr_mtx); 37890e5c6bd4SJamie Gritton for (bf = pr_flag_allow; 37900e5c6bd4SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && bf->flag != 0; 37910e5c6bd4SJamie Gritton bf++) { 37920e5c6bd4SJamie Gritton if (strcmp(bf->name, allow_name) == 0) { 37930e5c6bd4SJamie Gritton vfsp->vfc_prison_flag = bf->flag; 37940e5c6bd4SJamie Gritton goto no_add; 37950e5c6bd4SJamie Gritton } 37960e5c6bd4SJamie Gritton } 37970e5c6bd4SJamie Gritton 37980e5c6bd4SJamie Gritton /* 37990e5c6bd4SJamie Gritton * Find a free bit in prison0's pr_allow, failing if there are none 38000e5c6bd4SJamie Gritton * (which shouldn't happen as long as we keep track of how many 38010e5c6bd4SJamie Gritton * filesystems are jail-aware). 38020e5c6bd4SJamie Gritton */ 38030e5c6bd4SJamie Gritton for (allow_flag = 1;; allow_flag <<= 1) { 38040e5c6bd4SJamie Gritton if (allow_flag == 0) 38050e5c6bd4SJamie Gritton goto no_add; 38060e5c6bd4SJamie Gritton if ((prison0.pr_allow & allow_flag) == 0) 38070e5c6bd4SJamie Gritton break; 38080e5c6bd4SJamie Gritton } 38090e5c6bd4SJamie Gritton 38100e5c6bd4SJamie Gritton /* 38110e5c6bd4SJamie Gritton * Note the parameter in the next open slot in pr_flag_allow. 38120e5c6bd4SJamie Gritton * Set the flag last so code that checks pr_flag_allow can do so 38130e5c6bd4SJamie Gritton * without locking. 38140e5c6bd4SJamie Gritton */ 38150e5c6bd4SJamie Gritton for (bf = pr_flag_allow; bf->flag != 0; bf++) 38160e5c6bd4SJamie Gritton if (bf == pr_flag_allow + nitems(pr_flag_allow)) { 38170e5c6bd4SJamie Gritton /* This should never happen, but is not fatal. */ 38180e5c6bd4SJamie Gritton goto no_add; 38190e5c6bd4SJamie Gritton } 38200e5c6bd4SJamie Gritton prison0.pr_allow |= allow_flag; 38210e5c6bd4SJamie Gritton bf->name = allow_name; 38220e5c6bd4SJamie Gritton bf->noname = allow_noname; 38230e5c6bd4SJamie Gritton bf->flag = allow_flag; 38240e5c6bd4SJamie Gritton vfsp->vfc_prison_flag = allow_flag; 38250e5c6bd4SJamie Gritton mtx_unlock(&prison0.pr_mtx); 38260e5c6bd4SJamie Gritton 38270e5c6bd4SJamie Gritton /* 38280e5c6bd4SJamie Gritton * Create sysctls for the paramter, and the back-compat global 38290e5c6bd4SJamie Gritton * permission. 38300e5c6bd4SJamie Gritton */ 38310e5c6bd4SJamie Gritton #ifndef NO_SYSCTL_DESCR 38320e5c6bd4SJamie Gritton (void)asprintf(&descr, M_TEMP, "Jail may mount the %s file system", 38330e5c6bd4SJamie Gritton vfsp->vfc_name); 38340e5c6bd4SJamie Gritton #endif 38350e5c6bd4SJamie Gritton (void)SYSCTL_ADD_PROC(NULL, 38360e5c6bd4SJamie Gritton SYSCTL_CHILDREN(&sysctl___security_jail_param_allow_mount), 38370e5c6bd4SJamie Gritton OID_AUTO, vfsp->vfc_name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 38380e5c6bd4SJamie Gritton NULL, 0, sysctl_jail_param, "B", descr); 38390e5c6bd4SJamie Gritton #ifndef NO_SYSCTL_DESCR 38400e5c6bd4SJamie Gritton free(descr, M_TEMP); 38410e5c6bd4SJamie Gritton #endif 38420e5c6bd4SJamie Gritton if (asprintf(&mount_allowed, M_TEMP, "mount_%s_allowed", 38430e5c6bd4SJamie Gritton vfsp->vfc_name) >= 0) { 38440e5c6bd4SJamie Gritton #ifndef NO_SYSCTL_DESCR 38450e5c6bd4SJamie Gritton (void)asprintf(&descr, M_TEMP, 38460e5c6bd4SJamie Gritton "Processes in jail can mount the %s file system (deprecated)", 38470e5c6bd4SJamie Gritton vfsp->vfc_name); 38480e5c6bd4SJamie Gritton #endif 38490e5c6bd4SJamie Gritton (void)SYSCTL_ADD_PROC(NULL, 38500e5c6bd4SJamie Gritton SYSCTL_CHILDREN(&sysctl___security_jail), OID_AUTO, 38510e5c6bd4SJamie Gritton mount_allowed, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 38520e5c6bd4SJamie Gritton NULL, allow_flag, sysctl_jail_default_allow, "I", descr); 38530e5c6bd4SJamie Gritton #ifndef NO_SYSCTL_DESCR 38540e5c6bd4SJamie Gritton free(descr, M_TEMP); 38550e5c6bd4SJamie Gritton #endif 38560e5c6bd4SJamie Gritton free(mount_allowed, M_TEMP); 38570e5c6bd4SJamie Gritton } 38580e5c6bd4SJamie Gritton return; 38590e5c6bd4SJamie Gritton 38600e5c6bd4SJamie Gritton no_add: 38610e5c6bd4SJamie Gritton mtx_unlock(&prison0.pr_mtx); 38620e5c6bd4SJamie Gritton free(allow_name, M_PRISON); 38630e5c6bd4SJamie Gritton free(allow_noname, M_PRISON); 38640e5c6bd4SJamie Gritton } 3865bf3db8aaSMartin Matuska 38664b5c9cf6SEdward Tomasz Napierala #ifdef RACCT 3867097055e2SEdward Tomasz Napierala void 3868097055e2SEdward Tomasz Napierala prison_racct_foreach(void (*callback)(struct racct *racct, 386915db3c07SEdward Tomasz Napierala void *arg2, void *arg3), void (*pre)(void), void (*post)(void), 387015db3c07SEdward Tomasz Napierala void *arg2, void *arg3) 3871097055e2SEdward Tomasz Napierala { 3872a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 3873097055e2SEdward Tomasz Napierala 38744b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 38754b5c9cf6SEdward Tomasz Napierala 3876097055e2SEdward Tomasz Napierala sx_slock(&allprison_lock); 387715db3c07SEdward Tomasz Napierala if (pre != NULL) 387815db3c07SEdward Tomasz Napierala (pre)(); 3879a7ad07bfSEdward Tomasz Napierala LIST_FOREACH(prr, &allprison_racct, prr_next) 3880a7ad07bfSEdward Tomasz Napierala (callback)(prr->prr_racct, arg2, arg3); 388115db3c07SEdward Tomasz Napierala if (post != NULL) 388215db3c07SEdward Tomasz Napierala (post)(); 3883097055e2SEdward Tomasz Napierala sx_sunlock(&allprison_lock); 3884097055e2SEdward Tomasz Napierala } 38850304c731SJamie Gritton 3886a7ad07bfSEdward Tomasz Napierala static struct prison_racct * 3887a7ad07bfSEdward Tomasz Napierala prison_racct_find_locked(const char *name) 3888a7ad07bfSEdward Tomasz Napierala { 3889a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 3890a7ad07bfSEdward Tomasz Napierala 38914b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 3892a7ad07bfSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 3893a7ad07bfSEdward Tomasz Napierala 3894a7ad07bfSEdward Tomasz Napierala if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN) 3895a7ad07bfSEdward Tomasz Napierala return (NULL); 3896a7ad07bfSEdward Tomasz Napierala 3897a7ad07bfSEdward Tomasz Napierala LIST_FOREACH(prr, &allprison_racct, prr_next) { 3898a7ad07bfSEdward Tomasz Napierala if (strcmp(name, prr->prr_name) != 0) 3899a7ad07bfSEdward Tomasz Napierala continue; 3900a7ad07bfSEdward Tomasz Napierala 3901a7ad07bfSEdward Tomasz Napierala /* Found prison_racct with a matching name? */ 3902a7ad07bfSEdward Tomasz Napierala prison_racct_hold(prr); 3903a7ad07bfSEdward Tomasz Napierala return (prr); 3904a7ad07bfSEdward Tomasz Napierala } 3905a7ad07bfSEdward Tomasz Napierala 3906a7ad07bfSEdward Tomasz Napierala /* Add new prison_racct. */ 3907a7ad07bfSEdward Tomasz Napierala prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK); 3908a7ad07bfSEdward Tomasz Napierala racct_create(&prr->prr_racct); 3909a7ad07bfSEdward Tomasz Napierala 3910a7ad07bfSEdward Tomasz Napierala strcpy(prr->prr_name, name); 3911a7ad07bfSEdward Tomasz Napierala refcount_init(&prr->prr_refcount, 1); 3912a7ad07bfSEdward Tomasz Napierala LIST_INSERT_HEAD(&allprison_racct, prr, prr_next); 3913a7ad07bfSEdward Tomasz Napierala 3914a7ad07bfSEdward Tomasz Napierala return (prr); 3915a7ad07bfSEdward Tomasz Napierala } 3916a7ad07bfSEdward Tomasz Napierala 3917a7ad07bfSEdward Tomasz Napierala struct prison_racct * 3918a7ad07bfSEdward Tomasz Napierala prison_racct_find(const char *name) 3919a7ad07bfSEdward Tomasz Napierala { 3920a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 3921a7ad07bfSEdward Tomasz Napierala 39224b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 39234b5c9cf6SEdward Tomasz Napierala 3924a7ad07bfSEdward Tomasz Napierala sx_xlock(&allprison_lock); 3925a7ad07bfSEdward Tomasz Napierala prr = prison_racct_find_locked(name); 3926a7ad07bfSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 3927a7ad07bfSEdward Tomasz Napierala return (prr); 3928a7ad07bfSEdward Tomasz Napierala } 3929a7ad07bfSEdward Tomasz Napierala 3930a7ad07bfSEdward Tomasz Napierala void 3931a7ad07bfSEdward Tomasz Napierala prison_racct_hold(struct prison_racct *prr) 3932a7ad07bfSEdward Tomasz Napierala { 3933a7ad07bfSEdward Tomasz Napierala 39344b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 39354b5c9cf6SEdward Tomasz Napierala 3936a7ad07bfSEdward Tomasz Napierala refcount_acquire(&prr->prr_refcount); 3937a7ad07bfSEdward Tomasz Napierala } 3938a7ad07bfSEdward Tomasz Napierala 3939c34bbd2aSEdward Tomasz Napierala static void 3940c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(struct prison_racct *prr) 3941c34bbd2aSEdward Tomasz Napierala { 3942c34bbd2aSEdward Tomasz Napierala 39434b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 3944c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 3945c34bbd2aSEdward Tomasz Napierala 3946c34bbd2aSEdward Tomasz Napierala if (refcount_release(&prr->prr_refcount)) { 3947c34bbd2aSEdward Tomasz Napierala racct_destroy(&prr->prr_racct); 3948c34bbd2aSEdward Tomasz Napierala LIST_REMOVE(prr, prr_next); 3949c34bbd2aSEdward Tomasz Napierala free(prr, M_PRISON_RACCT); 3950c34bbd2aSEdward Tomasz Napierala } 3951c34bbd2aSEdward Tomasz Napierala } 3952c34bbd2aSEdward Tomasz Napierala 3953a7ad07bfSEdward Tomasz Napierala void 3954a7ad07bfSEdward Tomasz Napierala prison_racct_free(struct prison_racct *prr) 3955a7ad07bfSEdward Tomasz Napierala { 3956a7ad07bfSEdward Tomasz Napierala int old; 3957a7ad07bfSEdward Tomasz Napierala 39584b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 3959c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_UNLOCKED); 3960c34bbd2aSEdward Tomasz Napierala 3961a7ad07bfSEdward Tomasz Napierala old = prr->prr_refcount; 3962a7ad07bfSEdward Tomasz Napierala if (old > 1 && atomic_cmpset_int(&prr->prr_refcount, old, old - 1)) 3963a7ad07bfSEdward Tomasz Napierala return; 3964a7ad07bfSEdward Tomasz Napierala 3965a7ad07bfSEdward Tomasz Napierala sx_xlock(&allprison_lock); 3966c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(prr); 3967a7ad07bfSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 3968a7ad07bfSEdward Tomasz Napierala } 3969a7ad07bfSEdward Tomasz Napierala 3970a7ad07bfSEdward Tomasz Napierala static void 3971a7ad07bfSEdward Tomasz Napierala prison_racct_attach(struct prison *pr) 3972a7ad07bfSEdward Tomasz Napierala { 3973a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 3974a7ad07bfSEdward Tomasz Napierala 39754b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 3976c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 3977c34bbd2aSEdward Tomasz Napierala 3978a7ad07bfSEdward Tomasz Napierala prr = prison_racct_find_locked(pr->pr_name); 3979a7ad07bfSEdward Tomasz Napierala KASSERT(prr != NULL, ("cannot find prison_racct")); 3980a7ad07bfSEdward Tomasz Napierala 3981a7ad07bfSEdward Tomasz Napierala pr->pr_prison_racct = prr; 3982a7ad07bfSEdward Tomasz Napierala } 3983a7ad07bfSEdward Tomasz Napierala 3984c34bbd2aSEdward Tomasz Napierala /* 3985c34bbd2aSEdward Tomasz Napierala * Handle jail renaming. From the racct point of view, renaming means 3986c34bbd2aSEdward Tomasz Napierala * moving from one prison_racct to another. 3987c34bbd2aSEdward Tomasz Napierala */ 3988c34bbd2aSEdward Tomasz Napierala static void 3989c34bbd2aSEdward Tomasz Napierala prison_racct_modify(struct prison *pr) 3990c34bbd2aSEdward Tomasz Napierala { 3991*dbadb015SKonstantin Belousov #ifdef RCTL 3992c34bbd2aSEdward Tomasz Napierala struct proc *p; 3993c34bbd2aSEdward Tomasz Napierala struct ucred *cred; 3994*dbadb015SKonstantin Belousov #endif 3995c34bbd2aSEdward Tomasz Napierala struct prison_racct *oldprr; 3996c34bbd2aSEdward Tomasz Napierala 39974b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 39984b5c9cf6SEdward Tomasz Napierala 3999c34bbd2aSEdward Tomasz Napierala sx_slock(&allproc_lock); 4000c34bbd2aSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4001c34bbd2aSEdward Tomasz Napierala 4002e30345e7SEdward Tomasz Napierala if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) { 4003e30345e7SEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4004e30345e7SEdward Tomasz Napierala sx_sunlock(&allproc_lock); 4005c34bbd2aSEdward Tomasz Napierala return; 4006e30345e7SEdward Tomasz Napierala } 4007c34bbd2aSEdward Tomasz Napierala 4008c34bbd2aSEdward Tomasz Napierala oldprr = pr->pr_prison_racct; 4009c34bbd2aSEdward Tomasz Napierala pr->pr_prison_racct = NULL; 4010c34bbd2aSEdward Tomasz Napierala 4011c34bbd2aSEdward Tomasz Napierala prison_racct_attach(pr); 4012c34bbd2aSEdward Tomasz Napierala 4013c34bbd2aSEdward Tomasz Napierala /* 4014c34bbd2aSEdward Tomasz Napierala * Move resource utilisation records. 4015c34bbd2aSEdward Tomasz Napierala */ 4016c34bbd2aSEdward Tomasz Napierala racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct); 4017c34bbd2aSEdward Tomasz Napierala 4018f87beb93SAndriy Gapon #ifdef RCTL 4019c34bbd2aSEdward Tomasz Napierala /* 4020c34bbd2aSEdward Tomasz Napierala * Force rctl to reattach rules to processes. 4021c34bbd2aSEdward Tomasz Napierala */ 4022c34bbd2aSEdward Tomasz Napierala FOREACH_PROC_IN_SYSTEM(p) { 4023c34bbd2aSEdward Tomasz Napierala PROC_LOCK(p); 4024c34bbd2aSEdward Tomasz Napierala cred = crhold(p->p_ucred); 4025c34bbd2aSEdward Tomasz Napierala PROC_UNLOCK(p); 4026f87beb93SAndriy Gapon rctl_proc_ucred_changed(p, cred); 4027c34bbd2aSEdward Tomasz Napierala crfree(cred); 4028c34bbd2aSEdward Tomasz Napierala } 4029f87beb93SAndriy Gapon #endif 4030c34bbd2aSEdward Tomasz Napierala 4031c34bbd2aSEdward Tomasz Napierala sx_sunlock(&allproc_lock); 4032c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(oldprr); 4033c34bbd2aSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4034c34bbd2aSEdward Tomasz Napierala } 4035c34bbd2aSEdward Tomasz Napierala 4036a7ad07bfSEdward Tomasz Napierala static void 4037a7ad07bfSEdward Tomasz Napierala prison_racct_detach(struct prison *pr) 4038a7ad07bfSEdward Tomasz Napierala { 4039c34bbd2aSEdward Tomasz Napierala 40404b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4041c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_UNLOCKED); 4042c34bbd2aSEdward Tomasz Napierala 4043af3c786cSMateusz Guzik if (pr->pr_prison_racct == NULL) 4044af3c786cSMateusz Guzik return; 4045a7ad07bfSEdward Tomasz Napierala prison_racct_free(pr->pr_prison_racct); 4046a7ad07bfSEdward Tomasz Napierala pr->pr_prison_racct = NULL; 4047a7ad07bfSEdward Tomasz Napierala } 4048a7ad07bfSEdward Tomasz Napierala #endif /* RACCT */ 4049a7ad07bfSEdward Tomasz Napierala 4050413628a7SBjoern A. Zeeb #ifdef DDB 4051b38ff370SJamie Gritton 4052b38ff370SJamie Gritton static void 4053b38ff370SJamie Gritton db_show_prison(struct prison *pr) 4054413628a7SBjoern A. Zeeb { 4055672756aaSJamie Gritton struct bool_flags *bf; 4056672756aaSJamie Gritton struct jailsys_flags *jsf; 4057b38ff370SJamie Gritton #if defined(INET) || defined(INET6) 4058b38ff370SJamie Gritton int ii; 4059413628a7SBjoern A. Zeeb #endif 4060672756aaSJamie Gritton unsigned f; 40618144690aSEric van Gyzen #ifdef INET 40628144690aSEric van Gyzen char ip4buf[INET_ADDRSTRLEN]; 40638144690aSEric van Gyzen #endif 4064413628a7SBjoern A. Zeeb #ifdef INET6 4065413628a7SBjoern A. Zeeb char ip6buf[INET6_ADDRSTRLEN]; 4066413628a7SBjoern A. Zeeb #endif 4067413628a7SBjoern A. Zeeb 4068b38ff370SJamie Gritton db_printf("prison %p:\n", pr); 4069b38ff370SJamie Gritton db_printf(" jid = %d\n", pr->pr_id); 4070b38ff370SJamie Gritton db_printf(" name = %s\n", pr->pr_name); 40710304c731SJamie Gritton db_printf(" parent = %p\n", pr->pr_parent); 4072b38ff370SJamie Gritton db_printf(" ref = %d\n", pr->pr_ref); 4073b38ff370SJamie Gritton db_printf(" uref = %d\n", pr->pr_uref); 4074b38ff370SJamie Gritton db_printf(" path = %s\n", pr->pr_path); 4075b38ff370SJamie Gritton db_printf(" cpuset = %d\n", pr->pr_cpuset 4076b38ff370SJamie Gritton ? pr->pr_cpuset->cs_id : -1); 4077679e1390SJamie Gritton #ifdef VIMAGE 4078679e1390SJamie Gritton db_printf(" vnet = %p\n", pr->pr_vnet); 4079679e1390SJamie Gritton #endif 4080b38ff370SJamie Gritton db_printf(" root = %p\n", pr->pr_root); 4081b38ff370SJamie Gritton db_printf(" securelevel = %d\n", pr->pr_securelevel); 40820cc207a6SMartin Matuska db_printf(" devfs_rsnum = %d\n", pr->pr_devfs_rsnum); 4083fe0518e9SBjoern A. Zeeb db_printf(" children.max = %d\n", pr->pr_childmax); 4084fe0518e9SBjoern A. Zeeb db_printf(" children.cur = %d\n", pr->pr_childcount); 40850304c731SJamie Gritton db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children)); 40860304c731SJamie Gritton db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling)); 4087fe0518e9SBjoern A. Zeeb db_printf(" flags = 0x%x", pr->pr_flags); 4088672756aaSJamie Gritton for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++) 4089672756aaSJamie Gritton if (pr->pr_flags & bf->flag) 4090672756aaSJamie Gritton db_printf(" %s", bf->name); 4091672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 4092672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 4093672756aaSJamie Gritton jsf++) { 4094672756aaSJamie Gritton f = pr->pr_flags & (jsf->disable | jsf->new); 4095672756aaSJamie Gritton db_printf(" %-16s= %s\n", jsf->name, 4096672756aaSJamie Gritton (f != 0 && f == jsf->disable) ? "disable" 4097672756aaSJamie Gritton : (f == jsf->new) ? "new" 40987cbf7213SJamie Gritton : "inherit"); 40997cbf7213SJamie Gritton } 4100fe0518e9SBjoern A. Zeeb db_printf(" allow = 0x%x", pr->pr_allow); 4101672756aaSJamie Gritton for (bf = pr_flag_allow; 41020e5c6bd4SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && bf->flag != 0; 4103672756aaSJamie Gritton bf++) 4104672756aaSJamie Gritton if (pr->pr_allow & bf->flag) 4105672756aaSJamie Gritton db_printf(" %s", bf->name); 4106b38ff370SJamie Gritton db_printf("\n"); 41070304c731SJamie Gritton db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs); 4108c1f19219SJamie Gritton db_printf(" host.hostname = %s\n", pr->pr_hostname); 4109c1f19219SJamie Gritton db_printf(" host.domainname = %s\n", pr->pr_domainname); 4110c1f19219SJamie Gritton db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid); 411176ca6f88SJamie Gritton db_printf(" host.hostid = %lu\n", pr->pr_hostid); 4112413628a7SBjoern A. Zeeb #ifdef INET 4113b38ff370SJamie Gritton db_printf(" ip4s = %d\n", pr->pr_ip4s); 4114b38ff370SJamie Gritton for (ii = 0; ii < pr->pr_ip4s; ii++) 4115b38ff370SJamie Gritton db_printf(" %s %s\n", 4116fe0518e9SBjoern A. Zeeb ii == 0 ? "ip4.addr =" : " ", 41178144690aSEric van Gyzen inet_ntoa_r(pr->pr_ip4[ii], ip4buf)); 4118413628a7SBjoern A. Zeeb #endif 4119413628a7SBjoern A. Zeeb #ifdef INET6 4120b38ff370SJamie Gritton db_printf(" ip6s = %d\n", pr->pr_ip6s); 4121b38ff370SJamie Gritton for (ii = 0; ii < pr->pr_ip6s; ii++) 4122b38ff370SJamie Gritton db_printf(" %s %s\n", 4123fe0518e9SBjoern A. Zeeb ii == 0 ? "ip6.addr =" : " ", 4124b38ff370SJamie Gritton ip6_sprintf(ip6buf, &pr->pr_ip6[ii])); 4125b38ff370SJamie Gritton #endif 4126b38ff370SJamie Gritton } 4127b38ff370SJamie Gritton 4128b38ff370SJamie Gritton DB_SHOW_COMMAND(prison, db_show_prison_command) 4129b38ff370SJamie Gritton { 4130b38ff370SJamie Gritton struct prison *pr; 4131b38ff370SJamie Gritton 4132b38ff370SJamie Gritton if (!have_addr) { 41330304c731SJamie Gritton /* 41340304c731SJamie Gritton * Show all prisons in the list, and prison0 which is not 41350304c731SJamie Gritton * listed. 41360304c731SJamie Gritton */ 41370304c731SJamie Gritton db_show_prison(&prison0); 41380304c731SJamie Gritton if (!db_pager_quit) { 4139b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 4140b38ff370SJamie Gritton db_show_prison(pr); 4141413628a7SBjoern A. Zeeb if (db_pager_quit) 4142413628a7SBjoern A. Zeeb break; 4143413628a7SBjoern A. Zeeb } 41440304c731SJamie Gritton } 4145b38ff370SJamie Gritton return; 4146413628a7SBjoern A. Zeeb } 4147b38ff370SJamie Gritton 41480304c731SJamie Gritton if (addr == 0) 41490304c731SJamie Gritton pr = &prison0; 41500304c731SJamie Gritton else { 4151b38ff370SJamie Gritton /* Look for a prison with the ID and with references. */ 4152b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 4153b38ff370SJamie Gritton if (pr->pr_id == addr && pr->pr_ref > 0) 4154b38ff370SJamie Gritton break; 4155b38ff370SJamie Gritton if (pr == NULL) 4156b38ff370SJamie Gritton /* Look again, without requiring a reference. */ 4157b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 4158b38ff370SJamie Gritton if (pr->pr_id == addr) 4159b38ff370SJamie Gritton break; 4160b38ff370SJamie Gritton if (pr == NULL) 4161b38ff370SJamie Gritton /* Assume address points to a valid prison. */ 4162b38ff370SJamie Gritton pr = (struct prison *)addr; 41630304c731SJamie Gritton } 4164b38ff370SJamie Gritton db_show_prison(pr); 4165b38ff370SJamie Gritton } 4166b38ff370SJamie Gritton 4167413628a7SBjoern A. Zeeb #endif /* DDB */ 4168