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> 54*f87beb93SAndriy 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 1140304c731SJamie Gritton .pr_allow = PR_ALLOW_ALL, 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 184672756aaSJamie Gritton static struct bool_flags pr_flag_allow[] = { 185672756aaSJamie Gritton {"allow.set_hostname", "allow.noset_hostname", PR_ALLOW_SET_HOSTNAME}, 186672756aaSJamie Gritton {"allow.sysvipc", "allow.nosysvipc", PR_ALLOW_SYSVIPC}, 187672756aaSJamie Gritton {"allow.raw_sockets", "allow.noraw_sockets", PR_ALLOW_RAW_SOCKETS}, 188672756aaSJamie Gritton {"allow.chflags", "allow.nochflags", PR_ALLOW_CHFLAGS}, 189672756aaSJamie Gritton {"allow.mount", "allow.nomount", PR_ALLOW_MOUNT}, 190672756aaSJamie Gritton {"allow.quotas", "allow.noquotas", PR_ALLOW_QUOTAS}, 191672756aaSJamie Gritton {"allow.socket_af", "allow.nosocket_af", PR_ALLOW_SOCKET_AF}, 192672756aaSJamie Gritton {"allow.mount.devfs", "allow.mount.nodevfs", PR_ALLOW_MOUNT_DEVFS}, 193672756aaSJamie Gritton {"allow.mount.nullfs", "allow.mount.nonullfs", PR_ALLOW_MOUNT_NULLFS}, 194672756aaSJamie Gritton {"allow.mount.zfs", "allow.mount.nozfs", PR_ALLOW_MOUNT_ZFS}, 195672756aaSJamie Gritton {"allow.mount.procfs", "allow.mount.noprocfs", PR_ALLOW_MOUNT_PROCFS}, 196672756aaSJamie Gritton {"allow.mount.tmpfs", "allow.mount.notmpfs", PR_ALLOW_MOUNT_TMPFS}, 197672756aaSJamie Gritton {"allow.mount.fdescfs", "allow.mount.nofdescfs", 198672756aaSJamie Gritton PR_ALLOW_MOUNT_FDESCFS}, 199672756aaSJamie Gritton {"allow.mount.linprocfs", "allow.mount.nolinprocfs", 200672756aaSJamie Gritton PR_ALLOW_MOUNT_LINPROCFS}, 201672756aaSJamie Gritton {"allow.mount.linsysfs", "allow.mount.nolinsysfs", 202672756aaSJamie Gritton PR_ALLOW_MOUNT_LINSYSFS}, 203672756aaSJamie Gritton {"allow.reserved_ports", "allow.noreserved_ports", 204672756aaSJamie Gritton PR_ALLOW_RESERVED_PORTS}, 2050304c731SJamie Gritton }; 206672756aaSJamie Gritton const size_t pr_flag_allow_size = sizeof(pr_flag_allow); 2070304c731SJamie Gritton 208e28f9b7dSAllan Jude #define JAIL_DEFAULT_ALLOW (PR_ALLOW_SET_HOSTNAME | PR_ALLOW_RESERVED_PORTS) 20942bebd82SJamie Gritton #define JAIL_DEFAULT_ENFORCE_STATFS 2 210bf3db8aaSMartin Matuska #define JAIL_DEFAULT_DEVFS_RSNUM 0 2110304c731SJamie Gritton static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW; 21242bebd82SJamie Gritton static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 2130cc207a6SMartin Matuska static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM; 2140304c731SJamie Gritton #if defined(INET) || defined(INET6) 215e92e0574SJamie Gritton static unsigned jail_max_af_ips = 255; 2160304c731SJamie Gritton #endif 2170304c731SJamie Gritton 218b96bd95bSIan Lepore /* 219b96bd95bSIan Lepore * Initialize the parts of prison0 that can't be static-initialized with 220b96bd95bSIan Lepore * constants. This is called from proc0_init() after creating thread0 cpuset. 221b96bd95bSIan Lepore */ 222b96bd95bSIan Lepore void 223b96bd95bSIan Lepore prison0_init(void) 224b96bd95bSIan Lepore { 225b96bd95bSIan Lepore 226b96bd95bSIan Lepore prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset); 227b96bd95bSIan Lepore prison0.pr_osreldate = osreldate; 228b96bd95bSIan Lepore strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease)); 229b96bd95bSIan Lepore } 230b96bd95bSIan Lepore 231116734c4SMatthew Dillon /* 2329ddb7954SMike Barcroft * struct jail_args { 2339ddb7954SMike Barcroft * struct jail *jail; 2349ddb7954SMike Barcroft * }; 235116734c4SMatthew Dillon */ 23675c13541SPoul-Henning Kamp int 2378451d0ddSKip Macy sys_jail(struct thread *td, struct jail_args *uap) 23875c13541SPoul-Henning Kamp { 239413628a7SBjoern A. Zeeb uint32_t version; 240413628a7SBjoern A. Zeeb int error; 2410304c731SJamie Gritton struct jail j; 242413628a7SBjoern A. Zeeb 243413628a7SBjoern A. Zeeb error = copyin(uap->jail, &version, sizeof(uint32_t)); 244413628a7SBjoern A. Zeeb if (error) 245413628a7SBjoern A. Zeeb return (error); 246413628a7SBjoern A. Zeeb 247413628a7SBjoern A. Zeeb switch (version) { 248413628a7SBjoern A. Zeeb case 0: 249413628a7SBjoern A. Zeeb { 250413628a7SBjoern A. Zeeb struct jail_v0 j0; 251413628a7SBjoern A. Zeeb 2520304c731SJamie Gritton /* FreeBSD single IPv4 jails. */ 2530304c731SJamie Gritton bzero(&j, sizeof(struct jail)); 254413628a7SBjoern A. Zeeb error = copyin(uap->jail, &j0, sizeof(struct jail_v0)); 255413628a7SBjoern A. Zeeb if (error) 256413628a7SBjoern A. Zeeb return (error); 2570304c731SJamie Gritton j.version = j0.version; 2580304c731SJamie Gritton j.path = j0.path; 2590304c731SJamie Gritton j.hostname = j0.hostname; 260b5019bc4SPeter Wemm j.ip4s = htonl(j0.ip_number); /* jail_v0 is host order */ 261413628a7SBjoern A. Zeeb break; 262413628a7SBjoern A. Zeeb } 263413628a7SBjoern A. Zeeb 264413628a7SBjoern A. Zeeb case 1: 265413628a7SBjoern A. Zeeb /* 266413628a7SBjoern A. Zeeb * Version 1 was used by multi-IPv4 jail implementations 267413628a7SBjoern A. Zeeb * that never made it into the official kernel. 268413628a7SBjoern A. Zeeb */ 269413628a7SBjoern A. Zeeb return (EINVAL); 270413628a7SBjoern A. Zeeb 271413628a7SBjoern A. Zeeb case 2: /* JAIL_API_VERSION */ 272413628a7SBjoern A. Zeeb /* FreeBSD multi-IPv4/IPv6,noIP jails. */ 273413628a7SBjoern A. Zeeb error = copyin(uap->jail, &j, sizeof(struct jail)); 274413628a7SBjoern A. Zeeb if (error) 275413628a7SBjoern A. Zeeb return (error); 2760304c731SJamie Gritton break; 2770304c731SJamie Gritton 2780304c731SJamie Gritton default: 2790304c731SJamie Gritton /* Sci-Fi jails are not supported, sorry. */ 2800304c731SJamie Gritton return (EINVAL); 2810304c731SJamie Gritton } 2820304c731SJamie Gritton return (kern_jail(td, &j)); 2830304c731SJamie Gritton } 2840304c731SJamie Gritton 2850304c731SJamie Gritton int 2860304c731SJamie Gritton kern_jail(struct thread *td, struct jail *j) 2870304c731SJamie Gritton { 288672756aaSJamie Gritton struct iovec optiov[2 * (4 + nitems(pr_flag_allow) 289e92e0574SJamie Gritton #ifdef INET 290e92e0574SJamie Gritton + 1 291e92e0574SJamie Gritton #endif 292e92e0574SJamie Gritton #ifdef INET6 293e92e0574SJamie Gritton + 1 294e92e0574SJamie Gritton #endif 295e92e0574SJamie Gritton )]; 2960304c731SJamie Gritton struct uio opt; 2970304c731SJamie Gritton char *u_path, *u_hostname, *u_name; 298672756aaSJamie Gritton struct bool_flags *bf; 2990304c731SJamie Gritton #ifdef INET 300e92e0574SJamie Gritton uint32_t ip4s; 3010304c731SJamie Gritton struct in_addr *u_ip4; 3020304c731SJamie Gritton #endif 3030304c731SJamie Gritton #ifdef INET6 3040304c731SJamie Gritton struct in6_addr *u_ip6; 3050304c731SJamie Gritton #endif 3060304c731SJamie Gritton size_t tmplen; 307672756aaSJamie Gritton int error, enforce_statfs; 3080304c731SJamie Gritton 3090304c731SJamie Gritton bzero(&optiov, sizeof(optiov)); 3100304c731SJamie Gritton opt.uio_iov = optiov; 3110304c731SJamie Gritton opt.uio_iovcnt = 0; 3120304c731SJamie Gritton opt.uio_offset = -1; 3130304c731SJamie Gritton opt.uio_resid = -1; 3140304c731SJamie Gritton opt.uio_segflg = UIO_SYSSPACE; 3150304c731SJamie Gritton opt.uio_rw = UIO_READ; 3160304c731SJamie Gritton opt.uio_td = td; 3170304c731SJamie Gritton 3180304c731SJamie Gritton /* Set permissions for top-level jails from sysctls. */ 3190304c731SJamie Gritton if (!jailed(td->td_ucred)) { 320672756aaSJamie Gritton for (bf = pr_flag_allow; 321672756aaSJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow); 322672756aaSJamie Gritton bf++) { 323672756aaSJamie Gritton optiov[opt.uio_iovcnt].iov_base = __DECONST(char *, 324672756aaSJamie Gritton (jail_default_allow & bf->flag) 325672756aaSJamie Gritton ? bf->name : bf->noname); 3260304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = 3270304c731SJamie Gritton strlen(optiov[opt.uio_iovcnt].iov_base) + 1; 3280304c731SJamie Gritton opt.uio_iovcnt += 2; 3290304c731SJamie Gritton } 3300304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "enforce_statfs"; 3310304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs"); 3320304c731SJamie Gritton opt.uio_iovcnt++; 3330304c731SJamie Gritton enforce_statfs = jail_default_enforce_statfs; 3340304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = &enforce_statfs; 3350304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs); 3360304c731SJamie Gritton opt.uio_iovcnt++; 3370304c731SJamie Gritton } 3380304c731SJamie Gritton 339b38ff370SJamie Gritton tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN; 340b38ff370SJamie Gritton #ifdef INET 3410304c731SJamie Gritton ip4s = (j->version == 0) ? 1 : j->ip4s; 3420304c731SJamie Gritton if (ip4s > jail_max_af_ips) 343b38ff370SJamie Gritton return (EINVAL); 3440304c731SJamie Gritton tmplen += ip4s * sizeof(struct in_addr); 345b38ff370SJamie Gritton #else 3460304c731SJamie Gritton if (j->ip4s > 0) 347b38ff370SJamie Gritton return (EINVAL); 348b38ff370SJamie Gritton #endif 349b38ff370SJamie Gritton #ifdef INET6 3500304c731SJamie Gritton if (j->ip6s > jail_max_af_ips) 351b38ff370SJamie Gritton return (EINVAL); 3520304c731SJamie Gritton tmplen += j->ip6s * sizeof(struct in6_addr); 353b38ff370SJamie Gritton #else 3540304c731SJamie Gritton if (j->ip6s > 0) 355b38ff370SJamie Gritton return (EINVAL); 356b38ff370SJamie Gritton #endif 357b38ff370SJamie Gritton u_path = malloc(tmplen, M_TEMP, M_WAITOK); 358b38ff370SJamie Gritton u_hostname = u_path + MAXPATHLEN; 359b38ff370SJamie Gritton u_name = u_hostname + MAXHOSTNAMELEN; 360b38ff370SJamie Gritton #ifdef INET 361b38ff370SJamie Gritton u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN); 362b38ff370SJamie Gritton #endif 363b38ff370SJamie Gritton #ifdef INET6 364b38ff370SJamie Gritton #ifdef INET 3650304c731SJamie Gritton u_ip6 = (struct in6_addr *)(u_ip4 + ip4s); 366b38ff370SJamie Gritton #else 367b38ff370SJamie Gritton u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN); 368b38ff370SJamie Gritton #endif 369b38ff370SJamie Gritton #endif 3700304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "path"; 3710304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("path"); 3720304c731SJamie Gritton opt.uio_iovcnt++; 3730304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_path; 3740304c731SJamie Gritton error = copyinstr(j->path, u_path, MAXPATHLEN, 3750304c731SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 376b38ff370SJamie Gritton if (error) { 377b38ff370SJamie Gritton free(u_path, M_TEMP); 378b38ff370SJamie Gritton return (error); 379b38ff370SJamie Gritton } 3800304c731SJamie Gritton opt.uio_iovcnt++; 3810304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "host.hostname"; 3820304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname"); 3830304c731SJamie Gritton opt.uio_iovcnt++; 3840304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_hostname; 3850304c731SJamie Gritton error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN, 3860304c731SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 387b38ff370SJamie Gritton if (error) { 388b38ff370SJamie Gritton free(u_path, M_TEMP); 389b38ff370SJamie Gritton return (error); 390b38ff370SJamie Gritton } 3910304c731SJamie Gritton opt.uio_iovcnt++; 3920304c731SJamie Gritton if (j->jailname != NULL) { 393b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "name"; 394b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("name"); 395b38ff370SJamie Gritton opt.uio_iovcnt++; 396b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_name; 3970304c731SJamie Gritton error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN, 398b38ff370SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 399b38ff370SJamie Gritton if (error) { 400b38ff370SJamie Gritton free(u_path, M_TEMP); 401b38ff370SJamie Gritton return (error); 402b38ff370SJamie Gritton } 403b38ff370SJamie Gritton opt.uio_iovcnt++; 404b38ff370SJamie Gritton } 405b38ff370SJamie Gritton #ifdef INET 406b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "ip4.addr"; 407b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr"); 408b38ff370SJamie Gritton opt.uio_iovcnt++; 409b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_ip4; 4100304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr); 4110304c731SJamie Gritton if (j->version == 0) 4120304c731SJamie Gritton u_ip4->s_addr = j->ip4s; 4130304c731SJamie Gritton else { 4140304c731SJamie Gritton error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len); 415b38ff370SJamie Gritton if (error) { 416b38ff370SJamie Gritton free(u_path, M_TEMP); 417b38ff370SJamie Gritton return (error); 418b38ff370SJamie Gritton } 4190304c731SJamie Gritton } 420b38ff370SJamie Gritton opt.uio_iovcnt++; 421b38ff370SJamie Gritton #endif 422b38ff370SJamie Gritton #ifdef INET6 423b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "ip6.addr"; 424b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr"); 425b38ff370SJamie Gritton opt.uio_iovcnt++; 426b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_ip6; 4270304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr); 4280304c731SJamie Gritton error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len); 429b38ff370SJamie Gritton if (error) { 430b38ff370SJamie Gritton free(u_path, M_TEMP); 431b38ff370SJamie Gritton return (error); 432b38ff370SJamie Gritton } 433b38ff370SJamie Gritton opt.uio_iovcnt++; 434b38ff370SJamie Gritton #endif 43502abd400SPedro F. Giffuni KASSERT(opt.uio_iovcnt <= nitems(optiov), 4360304c731SJamie Gritton ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt)); 437b38ff370SJamie Gritton error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH); 438b38ff370SJamie Gritton free(u_path, M_TEMP); 439b38ff370SJamie Gritton return (error); 440b38ff370SJamie Gritton } 441b38ff370SJamie Gritton 4420304c731SJamie Gritton 443b38ff370SJamie Gritton /* 444b38ff370SJamie Gritton * struct jail_set_args { 445b38ff370SJamie Gritton * struct iovec *iovp; 446b38ff370SJamie Gritton * unsigned int iovcnt; 447b38ff370SJamie Gritton * int flags; 448b38ff370SJamie Gritton * }; 449b38ff370SJamie Gritton */ 450b38ff370SJamie Gritton int 4518451d0ddSKip Macy sys_jail_set(struct thread *td, struct jail_set_args *uap) 452b38ff370SJamie Gritton { 453b38ff370SJamie Gritton struct uio *auio; 454b38ff370SJamie Gritton int error; 455b38ff370SJamie Gritton 456b38ff370SJamie Gritton /* Check that we have an even number of iovecs. */ 457b38ff370SJamie Gritton if (uap->iovcnt & 1) 458b38ff370SJamie Gritton return (EINVAL); 459b38ff370SJamie Gritton 460b38ff370SJamie Gritton error = copyinuio(uap->iovp, uap->iovcnt, &auio); 461b38ff370SJamie Gritton if (error) 462b38ff370SJamie Gritton return (error); 463b38ff370SJamie Gritton error = kern_jail_set(td, auio, uap->flags); 464b38ff370SJamie Gritton free(auio, M_IOV); 465b38ff370SJamie Gritton return (error); 466413628a7SBjoern A. Zeeb } 467413628a7SBjoern A. Zeeb 468413628a7SBjoern A. Zeeb int 469b38ff370SJamie Gritton kern_jail_set(struct thread *td, struct uio *optuio, int flags) 470413628a7SBjoern A. Zeeb { 471fd7a8150SMike Barcroft struct nameidata nd; 472b38ff370SJamie Gritton #ifdef INET 473b38ff370SJamie Gritton struct in_addr *ip4; 474413628a7SBjoern A. Zeeb #endif 475b38ff370SJamie Gritton #ifdef INET6 476b38ff370SJamie Gritton struct in6_addr *ip6; 477b38ff370SJamie Gritton #endif 478b38ff370SJamie Gritton struct vfsopt *opt; 479b38ff370SJamie Gritton struct vfsoptlist *opts; 48057aea6dfSBjoern A. Zeeb struct prison *pr, *deadpr, *mypr, *ppr, *tpr; 481b38ff370SJamie Gritton struct vnode *root; 482babbbb9cSJamie Gritton char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid; 483b96bd95bSIan Lepore char *g_path, *osrelstr; 484672756aaSJamie Gritton struct bool_flags *bf; 485672756aaSJamie Gritton struct jailsys_flags *jsf; 4860304c731SJamie Gritton #if defined(INET) || defined(INET6) 48757aea6dfSBjoern A. Zeeb struct prison *tppr; 488b38ff370SJamie Gritton void *op; 4890304c731SJamie Gritton #endif 49076ca6f88SJamie Gritton unsigned long hid; 491b6f47c23SJamie Gritton size_t namelen, onamelen, pnamelen; 492cc5fd8c7SJamie Gritton int born, created, cuflags, descend, enforce; 493cc5fd8c7SJamie Gritton int error, errmsg_len, errmsg_pos; 4940cc207a6SMartin Matuska int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel; 495672756aaSJamie Gritton int jid, jsys, len, level; 496b96bd95bSIan Lepore int childmax, osreldt, rsnum, slevel; 497f6e633a9SMartin Matuska int fullpath_disabled; 498d465a41dSBjoern A. Zeeb #if defined(INET) || defined(INET6) 4990304c731SJamie Gritton int ii, ij; 500413628a7SBjoern A. Zeeb #endif 501413628a7SBjoern A. Zeeb #ifdef INET 5022b0d6f81SJamie Gritton int ip4s, redo_ip4; 503413628a7SBjoern A. Zeeb #endif 504b38ff370SJamie Gritton #ifdef INET6 5052b0d6f81SJamie Gritton int ip6s, redo_ip6; 506b38ff370SJamie Gritton #endif 5076beb3bb4SKirk McKusick uint64_t pr_allow, ch_allow, pr_flags, ch_flags; 5086beb3bb4SKirk McKusick unsigned tallow; 509b38ff370SJamie Gritton char numbuf[12]; 510b38ff370SJamie Gritton 511b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_SET); 512b38ff370SJamie Gritton if (!error && (flags & JAIL_ATTACH)) 513b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 514b38ff370SJamie Gritton if (error) 51575c13541SPoul-Henning Kamp return (error); 516b6f47c23SJamie Gritton mypr = td->td_ucred->cr_prison; 517b97457e2SJamie Gritton if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0) 5180304c731SJamie Gritton return (EPERM); 519b38ff370SJamie Gritton if (flags & ~JAIL_SET_MASK) 520b38ff370SJamie Gritton return (EINVAL); 521b38ff370SJamie Gritton 522b38ff370SJamie Gritton /* 523b38ff370SJamie Gritton * Check all the parameters before committing to anything. Not all 524b38ff370SJamie Gritton * errors can be caught early, but we may as well try. Also, this 525b38ff370SJamie Gritton * takes care of some expensive stuff (path lookup) before getting 526b38ff370SJamie Gritton * the allprison lock. 527b38ff370SJamie Gritton * 528b38ff370SJamie Gritton * XXX Jails are not filesystems, and jail parameters are not mount 529b38ff370SJamie Gritton * options. But it makes more sense to re-use the vfsopt code 530b38ff370SJamie Gritton * than duplicate it under a different name. 531b38ff370SJamie Gritton */ 532b38ff370SJamie Gritton error = vfs_buildopts(optuio, &opts); 533b38ff370SJamie Gritton if (error) 534b38ff370SJamie Gritton return (error); 535b38ff370SJamie Gritton #ifdef INET 536b38ff370SJamie Gritton ip4 = NULL; 537b38ff370SJamie Gritton #endif 538b38ff370SJamie Gritton #ifdef INET6 539b38ff370SJamie Gritton ip6 = NULL; 540b38ff370SJamie Gritton #endif 5416dfe0a3dSMartin Matuska g_path = NULL; 542b38ff370SJamie Gritton 543b6f47c23SJamie Gritton cuflags = flags & (JAIL_CREATE | JAIL_UPDATE); 544b6f47c23SJamie Gritton if (!cuflags) { 545b6f47c23SJamie Gritton error = EINVAL; 546b6f47c23SJamie Gritton vfs_opterror(opts, "no valid operation (create or update)"); 547b6f47c23SJamie Gritton goto done_errmsg; 548b6f47c23SJamie Gritton } 549b6f47c23SJamie Gritton 550b38ff370SJamie Gritton error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 551b38ff370SJamie Gritton if (error == ENOENT) 552b38ff370SJamie Gritton jid = 0; 553b38ff370SJamie Gritton else if (error != 0) 554b38ff370SJamie Gritton goto done_free; 555b38ff370SJamie Gritton 556b38ff370SJamie Gritton error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel)); 557b38ff370SJamie Gritton if (error == ENOENT) 558b38ff370SJamie Gritton gotslevel = 0; 559b38ff370SJamie Gritton else if (error != 0) 560b38ff370SJamie Gritton goto done_free; 561b38ff370SJamie Gritton else 562b38ff370SJamie Gritton gotslevel = 1; 563b38ff370SJamie Gritton 564b97457e2SJamie Gritton error = 565b97457e2SJamie Gritton vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax)); 566b97457e2SJamie Gritton if (error == ENOENT) 567b97457e2SJamie Gritton gotchildmax = 0; 568b97457e2SJamie Gritton else if (error != 0) 569b97457e2SJamie Gritton goto done_free; 570b97457e2SJamie Gritton else 571b97457e2SJamie Gritton gotchildmax = 1; 572b97457e2SJamie Gritton 5730304c731SJamie Gritton error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce)); 574f337198dSJamie Gritton if (error == ENOENT) 575f337198dSJamie Gritton gotenforce = 0; 576f337198dSJamie Gritton else if (error != 0) 5770304c731SJamie Gritton goto done_free; 578f337198dSJamie Gritton else if (enforce < 0 || enforce > 2) { 579f337198dSJamie Gritton error = EINVAL; 580f337198dSJamie Gritton goto done_free; 581f337198dSJamie Gritton } else 582f337198dSJamie Gritton gotenforce = 1; 5830304c731SJamie Gritton 5840cc207a6SMartin Matuska error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum)); 5850cc207a6SMartin Matuska if (error == ENOENT) 5860cc207a6SMartin Matuska gotrsnum = 0; 5870cc207a6SMartin Matuska else if (error != 0) 5880cc207a6SMartin Matuska goto done_free; 5890cc207a6SMartin Matuska else 5900cc207a6SMartin Matuska gotrsnum = 1; 5910cc207a6SMartin Matuska 592b38ff370SJamie Gritton pr_flags = ch_flags = 0; 593672756aaSJamie Gritton for (bf = pr_flag_bool; 594672756aaSJamie Gritton bf < pr_flag_bool + nitems(pr_flag_bool); 595672756aaSJamie Gritton bf++) { 596672756aaSJamie Gritton vfs_flagopt(opts, bf->name, &pr_flags, bf->flag); 597672756aaSJamie Gritton vfs_flagopt(opts, bf->noname, &ch_flags, bf->flag); 5980304c731SJamie Gritton } 599b38ff370SJamie Gritton ch_flags |= pr_flags; 600672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 601672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 602672756aaSJamie Gritton jsf++) { 603672756aaSJamie Gritton error = vfs_copyopt(opts, jsf->name, &jsys, sizeof(jsys)); 6047cbf7213SJamie Gritton if (error == ENOENT) 6057cbf7213SJamie Gritton continue; 6067cbf7213SJamie Gritton if (error != 0) 6077cbf7213SJamie Gritton goto done_free; 6087cbf7213SJamie Gritton switch (jsys) { 6097cbf7213SJamie Gritton case JAIL_SYS_DISABLE: 610672756aaSJamie Gritton if (!jsf->disable) { 6117cbf7213SJamie Gritton error = EINVAL; 6127cbf7213SJamie Gritton goto done_free; 6137cbf7213SJamie Gritton } 614672756aaSJamie Gritton pr_flags |= jsf->disable; 6157cbf7213SJamie Gritton break; 6167cbf7213SJamie Gritton case JAIL_SYS_NEW: 617672756aaSJamie Gritton pr_flags |= jsf->new; 6187cbf7213SJamie Gritton break; 6197cbf7213SJamie Gritton case JAIL_SYS_INHERIT: 6207cbf7213SJamie Gritton break; 6217cbf7213SJamie Gritton default: 6227cbf7213SJamie Gritton error = EINVAL; 6237cbf7213SJamie Gritton goto done_free; 6247cbf7213SJamie Gritton } 625672756aaSJamie Gritton ch_flags |= jsf->new | jsf->disable; 6267cbf7213SJamie Gritton } 6274affa14cSJamie Gritton if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE 6284affa14cSJamie Gritton && !(pr_flags & PR_PERSIST)) { 6294affa14cSJamie Gritton error = EINVAL; 6304affa14cSJamie Gritton vfs_opterror(opts, "new jail must persist or attach"); 6314affa14cSJamie Gritton goto done_errmsg; 6324affa14cSJamie Gritton } 633679e1390SJamie Gritton #ifdef VIMAGE 634679e1390SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) { 635679e1390SJamie Gritton error = EINVAL; 636679e1390SJamie Gritton vfs_opterror(opts, "vnet cannot be changed after creation"); 637679e1390SJamie Gritton goto done_errmsg; 638679e1390SJamie Gritton } 639679e1390SJamie Gritton #endif 6402b0d6f81SJamie Gritton #ifdef INET 6412b0d6f81SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) { 6422b0d6f81SJamie Gritton error = EINVAL; 6432b0d6f81SJamie Gritton vfs_opterror(opts, "ip4 cannot be changed after creation"); 6442b0d6f81SJamie Gritton goto done_errmsg; 6452b0d6f81SJamie Gritton } 6462b0d6f81SJamie Gritton #endif 6472b0d6f81SJamie Gritton #ifdef INET6 6482b0d6f81SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) { 6492b0d6f81SJamie Gritton error = EINVAL; 6502b0d6f81SJamie Gritton vfs_opterror(opts, "ip6 cannot be changed after creation"); 6512b0d6f81SJamie Gritton goto done_errmsg; 6522b0d6f81SJamie Gritton } 6532b0d6f81SJamie Gritton #endif 654b38ff370SJamie Gritton 6550304c731SJamie Gritton pr_allow = ch_allow = 0; 656672756aaSJamie Gritton for (bf = pr_flag_allow; 657672756aaSJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow); 658672756aaSJamie Gritton bf++) { 659672756aaSJamie Gritton vfs_flagopt(opts, bf->name, &pr_allow, bf->flag); 660672756aaSJamie Gritton vfs_flagopt(opts, bf->noname, &ch_allow, bf->flag); 6610304c731SJamie Gritton } 6620304c731SJamie Gritton ch_allow |= pr_allow; 6630304c731SJamie Gritton 664b38ff370SJamie Gritton error = vfs_getopt(opts, "name", (void **)&name, &len); 665b38ff370SJamie Gritton if (error == ENOENT) 666b38ff370SJamie Gritton name = NULL; 667b38ff370SJamie Gritton else if (error != 0) 668b38ff370SJamie Gritton goto done_free; 669b38ff370SJamie Gritton else { 670b38ff370SJamie Gritton if (len == 0 || name[len - 1] != '\0') { 671b38ff370SJamie Gritton error = EINVAL; 672b38ff370SJamie Gritton goto done_free; 673b38ff370SJamie Gritton } 674b38ff370SJamie Gritton if (len > MAXHOSTNAMELEN) { 675b38ff370SJamie Gritton error = ENAMETOOLONG; 676b38ff370SJamie Gritton goto done_free; 677b38ff370SJamie Gritton } 678b38ff370SJamie Gritton } 679b38ff370SJamie Gritton 680b38ff370SJamie Gritton error = vfs_getopt(opts, "host.hostname", (void **)&host, &len); 681b38ff370SJamie Gritton if (error == ENOENT) 682b38ff370SJamie Gritton host = NULL; 683b38ff370SJamie Gritton else if (error != 0) 684b38ff370SJamie Gritton goto done_free; 685b38ff370SJamie Gritton else { 68676ca6f88SJamie Gritton ch_flags |= PR_HOST; 68776ca6f88SJamie Gritton pr_flags |= PR_HOST; 688b38ff370SJamie Gritton if (len == 0 || host[len - 1] != '\0') { 689b38ff370SJamie Gritton error = EINVAL; 690b38ff370SJamie Gritton goto done_free; 691b38ff370SJamie Gritton } 692b38ff370SJamie Gritton if (len > MAXHOSTNAMELEN) { 693b38ff370SJamie Gritton error = ENAMETOOLONG; 694b38ff370SJamie Gritton goto done_free; 695b38ff370SJamie Gritton } 696b38ff370SJamie Gritton } 697b38ff370SJamie Gritton 69876ca6f88SJamie Gritton error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len); 69976ca6f88SJamie Gritton if (error == ENOENT) 70076ca6f88SJamie Gritton domain = NULL; 70176ca6f88SJamie Gritton else if (error != 0) 70276ca6f88SJamie Gritton goto done_free; 70376ca6f88SJamie Gritton else { 70476ca6f88SJamie Gritton ch_flags |= PR_HOST; 70576ca6f88SJamie Gritton pr_flags |= PR_HOST; 70676ca6f88SJamie Gritton if (len == 0 || domain[len - 1] != '\0') { 70776ca6f88SJamie Gritton error = EINVAL; 70876ca6f88SJamie Gritton goto done_free; 70976ca6f88SJamie Gritton } 71076ca6f88SJamie Gritton if (len > MAXHOSTNAMELEN) { 71176ca6f88SJamie Gritton error = ENAMETOOLONG; 71276ca6f88SJamie Gritton goto done_free; 71376ca6f88SJamie Gritton } 71476ca6f88SJamie Gritton } 71576ca6f88SJamie Gritton 71676ca6f88SJamie Gritton error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len); 71776ca6f88SJamie Gritton if (error == ENOENT) 71876ca6f88SJamie Gritton uuid = NULL; 71976ca6f88SJamie Gritton else if (error != 0) 72076ca6f88SJamie Gritton goto done_free; 72176ca6f88SJamie Gritton else { 72276ca6f88SJamie Gritton ch_flags |= PR_HOST; 72376ca6f88SJamie Gritton pr_flags |= PR_HOST; 72476ca6f88SJamie Gritton if (len == 0 || uuid[len - 1] != '\0') { 72576ca6f88SJamie Gritton error = EINVAL; 72676ca6f88SJamie Gritton goto done_free; 72776ca6f88SJamie Gritton } 72876ca6f88SJamie Gritton if (len > HOSTUUIDLEN) { 72976ca6f88SJamie Gritton error = ENAMETOOLONG; 73076ca6f88SJamie Gritton goto done_free; 73176ca6f88SJamie Gritton } 73276ca6f88SJamie Gritton } 73376ca6f88SJamie Gritton 734841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32 735a5c1afadSDmitry Chagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 73676ca6f88SJamie Gritton uint32_t hid32; 73776ca6f88SJamie Gritton 73876ca6f88SJamie Gritton error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32)); 73976ca6f88SJamie Gritton hid = hid32; 74076ca6f88SJamie Gritton } else 74176ca6f88SJamie Gritton #endif 74276ca6f88SJamie Gritton error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid)); 74376ca6f88SJamie Gritton if (error == ENOENT) 74476ca6f88SJamie Gritton gothid = 0; 74576ca6f88SJamie Gritton else if (error != 0) 74676ca6f88SJamie Gritton goto done_free; 74776ca6f88SJamie Gritton else { 74876ca6f88SJamie Gritton gothid = 1; 74976ca6f88SJamie Gritton ch_flags |= PR_HOST; 75076ca6f88SJamie Gritton pr_flags |= PR_HOST; 75176ca6f88SJamie Gritton } 75276ca6f88SJamie Gritton 753b38ff370SJamie Gritton #ifdef INET 754b38ff370SJamie Gritton error = vfs_getopt(opts, "ip4.addr", &op, &ip4s); 755b38ff370SJamie Gritton if (error == ENOENT) 7560e5e396eSJamie Gritton ip4s = 0; 757b38ff370SJamie Gritton else if (error != 0) 758b38ff370SJamie Gritton goto done_free; 759b38ff370SJamie Gritton else if (ip4s & (sizeof(*ip4) - 1)) { 760b38ff370SJamie Gritton error = EINVAL; 761b38ff370SJamie Gritton goto done_free; 7620304c731SJamie Gritton } else { 7636a3f2779SJamie Gritton ch_flags |= PR_IP4_USER; 7646a3f2779SJamie Gritton pr_flags |= PR_IP4_USER; 7656a3f2779SJamie Gritton if (ip4s > 0) { 766b38ff370SJamie Gritton ip4s /= sizeof(*ip4); 767b38ff370SJamie Gritton if (ip4s > jail_max_af_ips) { 768b38ff370SJamie Gritton error = EINVAL; 769b38ff370SJamie Gritton vfs_opterror(opts, "too many IPv4 addresses"); 770b38ff370SJamie Gritton goto done_errmsg; 771b38ff370SJamie Gritton } 7722b0d6f81SJamie Gritton ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 773b38ff370SJamie Gritton bcopy(op, ip4, ip4s * sizeof(*ip4)); 774b38ff370SJamie Gritton /* 7750304c731SJamie Gritton * IP addresses are all sorted but ip[0] to preserve 7760304c731SJamie Gritton * the primary IP address as given from userland. 7770304c731SJamie Gritton * This special IP is used for unbound outgoing 778bef916f9SBjoern A. Zeeb * connections as well for "loopback" traffic in case 779bef916f9SBjoern A. Zeeb * source address selection cannot find any more fitting 780bef916f9SBjoern A. Zeeb * address to connect from. 781b38ff370SJamie Gritton */ 782b38ff370SJamie Gritton if (ip4s > 1) 7830ce1624dSStephen J. Kiernan qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), 7840ce1624dSStephen J. Kiernan prison_qcmp_v4); 785b38ff370SJamie Gritton /* 7860304c731SJamie Gritton * Check for duplicate addresses and do some simple 7870304c731SJamie Gritton * zero and broadcast checks. If users give other bogus 7880304c731SJamie Gritton * addresses it is their problem. 789b38ff370SJamie Gritton * 7900304c731SJamie Gritton * We do not have to care about byte order for these 7910304c731SJamie Gritton * checks so we will do them in NBO. 792b38ff370SJamie Gritton */ 793b38ff370SJamie Gritton for (ii = 0; ii < ip4s; ii++) { 794b38ff370SJamie Gritton if (ip4[ii].s_addr == INADDR_ANY || 795b38ff370SJamie Gritton ip4[ii].s_addr == INADDR_BROADCAST) { 796b38ff370SJamie Gritton error = EINVAL; 797b38ff370SJamie Gritton goto done_free; 798b38ff370SJamie Gritton } 799b38ff370SJamie Gritton if ((ii+1) < ip4s && 800b38ff370SJamie Gritton (ip4[0].s_addr == ip4[ii+1].s_addr || 801b38ff370SJamie Gritton ip4[ii].s_addr == ip4[ii+1].s_addr)) { 802b38ff370SJamie Gritton error = EINVAL; 803b38ff370SJamie Gritton goto done_free; 804b38ff370SJamie Gritton } 805b38ff370SJamie Gritton } 806b38ff370SJamie Gritton } 8070304c731SJamie Gritton } 808b38ff370SJamie Gritton #endif 809b38ff370SJamie Gritton 810b38ff370SJamie Gritton #ifdef INET6 811b38ff370SJamie Gritton error = vfs_getopt(opts, "ip6.addr", &op, &ip6s); 812b38ff370SJamie Gritton if (error == ENOENT) 8130e5e396eSJamie Gritton ip6s = 0; 814b38ff370SJamie Gritton else if (error != 0) 815b38ff370SJamie Gritton goto done_free; 816b38ff370SJamie Gritton else if (ip6s & (sizeof(*ip6) - 1)) { 817b38ff370SJamie Gritton error = EINVAL; 818b38ff370SJamie Gritton goto done_free; 8190304c731SJamie Gritton } else { 8206a3f2779SJamie Gritton ch_flags |= PR_IP6_USER; 8216a3f2779SJamie Gritton pr_flags |= PR_IP6_USER; 8226a3f2779SJamie Gritton if (ip6s > 0) { 823b38ff370SJamie Gritton ip6s /= sizeof(*ip6); 824b38ff370SJamie Gritton if (ip6s > jail_max_af_ips) { 825b38ff370SJamie Gritton error = EINVAL; 826b38ff370SJamie Gritton vfs_opterror(opts, "too many IPv6 addresses"); 827b38ff370SJamie Gritton goto done_errmsg; 828b38ff370SJamie Gritton } 8292b0d6f81SJamie Gritton ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 830b38ff370SJamie Gritton bcopy(op, ip6, ip6s * sizeof(*ip6)); 831b38ff370SJamie Gritton if (ip6s > 1) 8320ce1624dSStephen J. Kiernan qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), 8330ce1624dSStephen J. Kiernan prison_qcmp_v6); 834b38ff370SJamie Gritton for (ii = 0; ii < ip6s; ii++) { 8350304c731SJamie Gritton if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) { 836b38ff370SJamie Gritton error = EINVAL; 837b38ff370SJamie Gritton goto done_free; 838b38ff370SJamie Gritton } 839b38ff370SJamie Gritton if ((ii+1) < ip6s && 840b38ff370SJamie Gritton (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) || 841b38ff370SJamie Gritton IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1]))) 842b38ff370SJamie Gritton { 843b38ff370SJamie Gritton error = EINVAL; 844b38ff370SJamie Gritton goto done_free; 845b38ff370SJamie Gritton } 846b38ff370SJamie Gritton } 847b38ff370SJamie Gritton } 8480304c731SJamie Gritton } 849b38ff370SJamie Gritton #endif 850b38ff370SJamie Gritton 851bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6)) 852bdfc8cc4SJamie Gritton if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 853bdfc8cc4SJamie Gritton error = EINVAL; 854bdfc8cc4SJamie Gritton vfs_opterror(opts, 855bdfc8cc4SJamie Gritton "vnet jails cannot have IP address restrictions"); 856bdfc8cc4SJamie Gritton goto done_errmsg; 857bdfc8cc4SJamie Gritton } 858bdfc8cc4SJamie Gritton #endif 859bdfc8cc4SJamie Gritton 860cf0313c6SJamie Gritton error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len); 861cf0313c6SJamie Gritton if (error == ENOENT) 862cf0313c6SJamie Gritton osrelstr = NULL; 863cf0313c6SJamie Gritton else if (error != 0) 864cf0313c6SJamie Gritton goto done_free; 865cf0313c6SJamie Gritton else { 866cf0313c6SJamie Gritton if (flags & JAIL_UPDATE) { 867cf0313c6SJamie Gritton error = EINVAL; 868cf0313c6SJamie Gritton vfs_opterror(opts, 869cf0313c6SJamie Gritton "osrelease cannot be changed after creation"); 870cf0313c6SJamie Gritton goto done_errmsg; 871cf0313c6SJamie Gritton } 872cf0313c6SJamie Gritton if (len == 0 || len >= OSRELEASELEN) { 873cf0313c6SJamie Gritton error = EINVAL; 874cf0313c6SJamie Gritton vfs_opterror(opts, 875cf0313c6SJamie Gritton "osrelease string must be 1-%d bytes long", 876cf0313c6SJamie Gritton OSRELEASELEN - 1); 877cf0313c6SJamie Gritton goto done_errmsg; 878cf0313c6SJamie Gritton } 879cf0313c6SJamie Gritton } 880cf0313c6SJamie Gritton 881cf0313c6SJamie Gritton error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt)); 882cf0313c6SJamie Gritton if (error == ENOENT) 883cf0313c6SJamie Gritton osreldt = 0; 884cf0313c6SJamie Gritton else if (error != 0) 885cf0313c6SJamie Gritton goto done_free; 886cf0313c6SJamie Gritton else { 887cf0313c6SJamie Gritton if (flags & JAIL_UPDATE) { 888cf0313c6SJamie Gritton error = EINVAL; 889cf0313c6SJamie Gritton vfs_opterror(opts, 890cf0313c6SJamie Gritton "osreldate cannot be changed after creation"); 891cf0313c6SJamie Gritton goto done_errmsg; 892cf0313c6SJamie Gritton } 893cf0313c6SJamie Gritton if (osreldt == 0) { 894cf0313c6SJamie Gritton error = EINVAL; 895cf0313c6SJamie Gritton vfs_opterror(opts, "osreldate cannot be 0"); 896cf0313c6SJamie Gritton goto done_errmsg; 897cf0313c6SJamie Gritton } 898cf0313c6SJamie Gritton } 899cf0313c6SJamie Gritton 9009cbe30e1SMartin Matuska fullpath_disabled = 0; 901b38ff370SJamie Gritton root = NULL; 902b38ff370SJamie Gritton error = vfs_getopt(opts, "path", (void **)&path, &len); 903b38ff370SJamie Gritton if (error == ENOENT) 904b38ff370SJamie Gritton path = NULL; 905b38ff370SJamie Gritton else if (error != 0) 906b38ff370SJamie Gritton goto done_free; 907b38ff370SJamie Gritton else { 908b38ff370SJamie Gritton if (flags & JAIL_UPDATE) { 909b38ff370SJamie Gritton error = EINVAL; 910b38ff370SJamie Gritton vfs_opterror(opts, 911b38ff370SJamie Gritton "path cannot be changed after creation"); 912b38ff370SJamie Gritton goto done_errmsg; 913b38ff370SJamie Gritton } 914b38ff370SJamie Gritton if (len == 0 || path[len - 1] != '\0') { 915b38ff370SJamie Gritton error = EINVAL; 916b38ff370SJamie Gritton goto done_free; 917b38ff370SJamie Gritton } 9185050aa86SKonstantin Belousov NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, 919b38ff370SJamie Gritton path, td); 920b38ff370SJamie Gritton error = namei(&nd); 921b38ff370SJamie Gritton if (error) 922b38ff370SJamie Gritton goto done_free; 923b38ff370SJamie Gritton root = nd.ni_vp; 924b38ff370SJamie Gritton NDFREE(&nd, NDF_ONLY_PNBUF); 9256dfe0a3dSMartin Matuska g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 9266dfe0a3dSMartin Matuska strlcpy(g_path, path, MAXPATHLEN); 9276dfe0a3dSMartin Matuska error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN); 9286dfe0a3dSMartin Matuska if (error == 0) 9296dfe0a3dSMartin Matuska path = g_path; 9306dfe0a3dSMartin Matuska else if (error == ENODEV) { 931f6e633a9SMartin Matuska /* proceed if sysctl debug.disablefullpath == 1 */ 932f6e633a9SMartin Matuska fullpath_disabled = 1; 933f6e633a9SMartin Matuska if (len < 2 || (len == 2 && path[0] == '/')) 934f6e633a9SMartin Matuska path = NULL; 9356dfe0a3dSMartin Matuska } else { 936f6e633a9SMartin Matuska /* exit on other errors */ 937b38ff370SJamie Gritton goto done_free; 938b38ff370SJamie Gritton } 939f6e633a9SMartin Matuska if (root->v_type != VDIR) { 940f6e633a9SMartin Matuska error = ENOTDIR; 941f6e633a9SMartin Matuska vput(root); 942f6e633a9SMartin Matuska goto done_free; 943f6e633a9SMartin Matuska } 944f6e633a9SMartin Matuska VOP_UNLOCK(root, 0); 945f6e633a9SMartin Matuska if (fullpath_disabled) { 946f6e633a9SMartin Matuska /* Leave room for a real-root full pathname. */ 947f6e633a9SMartin Matuska if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/") 948f6e633a9SMartin Matuska ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) { 949f6e633a9SMartin Matuska error = ENAMETOOLONG; 950932a6e43SJamie Gritton vrele(root); 951f6e633a9SMartin Matuska goto done_free; 952f6e633a9SMartin Matuska } 953b38ff370SJamie Gritton } 954b38ff370SJamie Gritton } 955b38ff370SJamie Gritton 956b38ff370SJamie Gritton /* 957b6f47c23SJamie Gritton * Find the specified jail, or at least its parent. 958b38ff370SJamie Gritton * This abuses the file error codes ENOENT and EEXIST. 959b38ff370SJamie Gritton */ 960b38ff370SJamie Gritton pr = NULL; 961b6f47c23SJamie Gritton ppr = mypr; 962babbbb9cSJamie Gritton if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) { 963babbbb9cSJamie Gritton namelc = strrchr(name, '.'); 964babbbb9cSJamie Gritton jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10); 965babbbb9cSJamie Gritton if (*p != '\0') 966babbbb9cSJamie Gritton jid = 0; 967babbbb9cSJamie Gritton } 968b6f47c23SJamie Gritton sx_xlock(&allprison_lock); 969b38ff370SJamie Gritton if (jid != 0) { 9700304c731SJamie Gritton /* 9710304c731SJamie Gritton * See if a requested jid already exists. There is an 9720304c731SJamie Gritton * information leak here if the jid exists but is not within 9730304c731SJamie Gritton * the caller's jail hierarchy. Jail creators will get EEXIST 9740304c731SJamie Gritton * even though they cannot see the jail, and CREATE | UPDATE 9750304c731SJamie Gritton * will return ENOENT which is not normally a valid error. 9760304c731SJamie Gritton */ 977b38ff370SJamie Gritton if (jid < 0) { 978b38ff370SJamie Gritton error = EINVAL; 979b38ff370SJamie Gritton vfs_opterror(opts, "negative jid"); 980b38ff370SJamie Gritton goto done_unlock_list; 981b38ff370SJamie Gritton } 982b38ff370SJamie Gritton pr = prison_find(jid); 983b38ff370SJamie Gritton if (pr != NULL) { 9840304c731SJamie Gritton ppr = pr->pr_parent; 985b38ff370SJamie Gritton /* Create: jid must not exist. */ 986b38ff370SJamie Gritton if (cuflags == JAIL_CREATE) { 987b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 988b38ff370SJamie Gritton error = EEXIST; 989b38ff370SJamie Gritton vfs_opterror(opts, "jail %d already exists", 990b38ff370SJamie Gritton jid); 991b38ff370SJamie Gritton goto done_unlock_list; 992b38ff370SJamie Gritton } 9930304c731SJamie Gritton if (!prison_ischild(mypr, pr)) { 9940304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 9950304c731SJamie Gritton pr = NULL; 9960304c731SJamie Gritton } else if (pr->pr_uref == 0) { 997b38ff370SJamie Gritton if (!(flags & JAIL_DYING)) { 998b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 999b38ff370SJamie Gritton error = ENOENT; 1000b38ff370SJamie Gritton vfs_opterror(opts, "jail %d is dying", 1001b38ff370SJamie Gritton jid); 1002b38ff370SJamie Gritton goto done_unlock_list; 1003b38ff370SJamie Gritton } else if ((flags & JAIL_ATTACH) || 1004b38ff370SJamie Gritton (pr_flags & PR_PERSIST)) { 1005b38ff370SJamie Gritton /* 1006b38ff370SJamie Gritton * A dying jail might be resurrected 1007b38ff370SJamie Gritton * (via attach or persist), but first 1008b38ff370SJamie Gritton * it must determine if another jail 1009b38ff370SJamie Gritton * has claimed its name. Accomplish 1010b38ff370SJamie Gritton * this by implicitly re-setting the 1011b38ff370SJamie Gritton * name. 1012b38ff370SJamie Gritton */ 1013b38ff370SJamie Gritton if (name == NULL) 10140304c731SJamie Gritton name = prison_name(mypr, pr); 1015b38ff370SJamie Gritton } 1016b38ff370SJamie Gritton } 1017b38ff370SJamie Gritton } 1018b38ff370SJamie Gritton if (pr == NULL) { 1019b38ff370SJamie Gritton /* Update: jid must exist. */ 1020b38ff370SJamie Gritton if (cuflags == JAIL_UPDATE) { 1021b38ff370SJamie Gritton error = ENOENT; 1022b38ff370SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 1023b38ff370SJamie Gritton goto done_unlock_list; 1024b38ff370SJamie Gritton } 1025b38ff370SJamie Gritton } 1026b38ff370SJamie Gritton } 1027b38ff370SJamie Gritton /* 1028b38ff370SJamie Gritton * If the caller provided a name, look for a jail by that name. 1029b38ff370SJamie Gritton * This has different semantics for creates and updates keyed by jid 1030b38ff370SJamie Gritton * (where the name must not already exist in a different jail), 1031b38ff370SJamie Gritton * and updates keyed by the name itself (where the name must exist 1032b38ff370SJamie Gritton * because that is the jail being updated). 1033b38ff370SJamie Gritton */ 1034b6f47c23SJamie Gritton namelc = NULL; 1035b38ff370SJamie Gritton if (name != NULL) { 1036babbbb9cSJamie Gritton namelc = strrchr(name, '.'); 1037babbbb9cSJamie Gritton if (namelc == NULL) 1038babbbb9cSJamie Gritton namelc = name; 1039babbbb9cSJamie Gritton else { 10400304c731SJamie Gritton /* 10410304c731SJamie Gritton * This is a hierarchical name. Split it into the 10420304c731SJamie Gritton * parent and child names, and make sure the parent 10430304c731SJamie Gritton * exists or matches an already found jail. 10440304c731SJamie Gritton */ 10450304c731SJamie Gritton if (pr != NULL) { 1046babbbb9cSJamie Gritton if (strncmp(name, ppr->pr_name, namelc - name) 1047babbbb9cSJamie Gritton || ppr->pr_name[namelc - name] != '\0') { 10480304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 10490304c731SJamie Gritton error = EINVAL; 10500304c731SJamie Gritton vfs_opterror(opts, 10510304c731SJamie Gritton "cannot change jail's parent"); 10520304c731SJamie Gritton goto done_unlock_list; 10530304c731SJamie Gritton } 10540304c731SJamie Gritton } else { 1055b6f47c23SJamie Gritton *namelc = '\0'; 10560304c731SJamie Gritton ppr = prison_find_name(mypr, name); 10570304c731SJamie Gritton if (ppr == NULL) { 10580304c731SJamie Gritton error = ENOENT; 10590304c731SJamie Gritton vfs_opterror(opts, 10600304c731SJamie Gritton "jail \"%s\" not found", name); 10610304c731SJamie Gritton goto done_unlock_list; 10620304c731SJamie Gritton } 10630304c731SJamie Gritton mtx_unlock(&ppr->pr_mtx); 1064b6f47c23SJamie Gritton *namelc = '.'; 10650304c731SJamie Gritton } 1066b6f47c23SJamie Gritton namelc++; 10670304c731SJamie Gritton } 1068b6f47c23SJamie Gritton if (namelc[0] != '\0') { 1069b6f47c23SJamie Gritton pnamelen = 10700304c731SJamie Gritton (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 1071b38ff370SJamie Gritton name_again: 10720304c731SJamie Gritton deadpr = NULL; 10730304c731SJamie Gritton FOREACH_PRISON_CHILD(ppr, tpr) { 1074b38ff370SJamie Gritton if (tpr != pr && tpr->pr_ref > 0 && 1075b6f47c23SJamie Gritton !strcmp(tpr->pr_name + pnamelen, namelc)) { 1076b38ff370SJamie Gritton if (pr == NULL && 1077b38ff370SJamie Gritton cuflags != JAIL_CREATE) { 1078b38ff370SJamie Gritton mtx_lock(&tpr->pr_mtx); 1079b38ff370SJamie Gritton if (tpr->pr_ref > 0) { 1080b38ff370SJamie Gritton /* 1081b38ff370SJamie Gritton * Use this jail 1082b38ff370SJamie Gritton * for updates. 1083b38ff370SJamie Gritton */ 1084b38ff370SJamie Gritton if (tpr->pr_uref > 0) { 1085b38ff370SJamie Gritton pr = tpr; 1086b38ff370SJamie Gritton break; 1087b38ff370SJamie Gritton } 1088b38ff370SJamie Gritton deadpr = tpr; 1089b38ff370SJamie Gritton } 1090b38ff370SJamie Gritton mtx_unlock(&tpr->pr_mtx); 1091b38ff370SJamie Gritton } else if (tpr->pr_uref > 0) { 1092b38ff370SJamie Gritton /* 1093b38ff370SJamie Gritton * Create, or update(jid): 1094b38ff370SJamie Gritton * name must not exist in an 10950304c731SJamie Gritton * active sibling jail. 1096b38ff370SJamie Gritton */ 1097b38ff370SJamie Gritton error = EEXIST; 1098b38ff370SJamie Gritton if (pr != NULL) 1099b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1100b38ff370SJamie Gritton vfs_opterror(opts, 1101b38ff370SJamie Gritton "jail \"%s\" already exists", 1102b38ff370SJamie Gritton name); 1103b38ff370SJamie Gritton goto done_unlock_list; 1104b38ff370SJamie Gritton } 1105b38ff370SJamie Gritton } 1106b38ff370SJamie Gritton } 1107b38ff370SJamie Gritton /* If no active jail is found, use a dying one. */ 1108b38ff370SJamie Gritton if (deadpr != NULL && pr == NULL) { 1109b38ff370SJamie Gritton if (flags & JAIL_DYING) { 1110b38ff370SJamie Gritton mtx_lock(&deadpr->pr_mtx); 1111b38ff370SJamie Gritton if (deadpr->pr_ref == 0) { 1112b38ff370SJamie Gritton mtx_unlock(&deadpr->pr_mtx); 1113b38ff370SJamie Gritton goto name_again; 1114b38ff370SJamie Gritton } 1115b38ff370SJamie Gritton pr = deadpr; 1116b38ff370SJamie Gritton } else if (cuflags == JAIL_UPDATE) { 1117b38ff370SJamie Gritton error = ENOENT; 1118b38ff370SJamie Gritton vfs_opterror(opts, 1119b38ff370SJamie Gritton "jail \"%s\" is dying", name); 1120b38ff370SJamie Gritton goto done_unlock_list; 1121b38ff370SJamie Gritton } 1122b38ff370SJamie Gritton } 1123b38ff370SJamie Gritton /* Update: name must exist if no jid. */ 1124b38ff370SJamie Gritton else if (cuflags == JAIL_UPDATE && pr == NULL) { 1125b38ff370SJamie Gritton error = ENOENT; 1126b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", 1127b38ff370SJamie Gritton name); 1128b38ff370SJamie Gritton goto done_unlock_list; 1129b38ff370SJamie Gritton } 1130b38ff370SJamie Gritton } 1131b38ff370SJamie Gritton } 1132b38ff370SJamie Gritton /* Update: must provide a jid or name. */ 1133b38ff370SJamie Gritton else if (cuflags == JAIL_UPDATE && pr == NULL) { 1134b38ff370SJamie Gritton error = ENOENT; 1135b38ff370SJamie Gritton vfs_opterror(opts, "update specified no jail"); 1136b38ff370SJamie Gritton goto done_unlock_list; 1137b38ff370SJamie Gritton } 1138b38ff370SJamie Gritton 1139b38ff370SJamie Gritton /* If there's no prison to update, create a new one and link it in. */ 1140b38ff370SJamie Gritton if (pr == NULL) { 1141b97457e2SJamie Gritton for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent) 1142b97457e2SJamie Gritton if (tpr->pr_childcount >= tpr->pr_childmax) { 1143b97457e2SJamie Gritton error = EPERM; 1144b97457e2SJamie Gritton vfs_opterror(opts, "prison limit exceeded"); 1145b97457e2SJamie Gritton goto done_unlock_list; 1146b97457e2SJamie Gritton } 1147b38ff370SJamie Gritton created = 1; 11480304c731SJamie Gritton mtx_lock(&ppr->pr_mtx); 11492a549507SJamie Gritton if (ppr->pr_ref == 0) { 11500304c731SJamie Gritton mtx_unlock(&ppr->pr_mtx); 11510304c731SJamie Gritton error = ENOENT; 1152b6f47c23SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", 1153b6f47c23SJamie Gritton prison_name(mypr, ppr)); 11540304c731SJamie Gritton goto done_unlock_list; 11550304c731SJamie Gritton } 11560304c731SJamie Gritton ppr->pr_ref++; 11570304c731SJamie Gritton ppr->pr_uref++; 11580304c731SJamie Gritton mtx_unlock(&ppr->pr_mtx); 1159b38ff370SJamie Gritton pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO); 1160b38ff370SJamie Gritton if (jid == 0) { 1161b38ff370SJamie Gritton /* Find the next free jid. */ 1162b38ff370SJamie Gritton jid = lastprid + 1; 1163b38ff370SJamie Gritton findnext: 1164b38ff370SJamie Gritton if (jid == JAIL_MAX) 1165b38ff370SJamie Gritton jid = 1; 1166b38ff370SJamie Gritton TAILQ_FOREACH(tpr, &allprison, pr_list) { 1167b38ff370SJamie Gritton if (tpr->pr_id < jid) 1168b38ff370SJamie Gritton continue; 1169b38ff370SJamie Gritton if (tpr->pr_id > jid || tpr->pr_ref == 0) { 1170b38ff370SJamie Gritton TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1171b38ff370SJamie Gritton break; 1172b38ff370SJamie Gritton } 1173b38ff370SJamie Gritton if (jid == lastprid) { 1174b38ff370SJamie Gritton error = EAGAIN; 1175b38ff370SJamie Gritton vfs_opterror(opts, 1176b38ff370SJamie Gritton "no available jail IDs"); 1177b38ff370SJamie Gritton free(pr, M_PRISON); 11780304c731SJamie Gritton prison_deref(ppr, PD_DEREF | 11790304c731SJamie Gritton PD_DEUREF | PD_LIST_XLOCKED); 11800304c731SJamie Gritton goto done_releroot; 1181b38ff370SJamie Gritton } 1182b38ff370SJamie Gritton jid++; 1183b38ff370SJamie Gritton goto findnext; 1184b38ff370SJamie Gritton } 1185b38ff370SJamie Gritton lastprid = jid; 1186b38ff370SJamie Gritton } else { 1187b38ff370SJamie Gritton /* 1188b38ff370SJamie Gritton * The jail already has a jid (that did not yet exist), 1189b38ff370SJamie Gritton * so just find where to insert it. 1190b38ff370SJamie Gritton */ 1191b38ff370SJamie Gritton TAILQ_FOREACH(tpr, &allprison, pr_list) 1192b38ff370SJamie Gritton if (tpr->pr_id >= jid) { 1193b38ff370SJamie Gritton TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1194b38ff370SJamie Gritton break; 1195b38ff370SJamie Gritton } 1196b38ff370SJamie Gritton } 1197b38ff370SJamie Gritton if (tpr == NULL) 1198b38ff370SJamie Gritton TAILQ_INSERT_TAIL(&allprison, pr, pr_list); 11990304c731SJamie Gritton LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling); 12000304c731SJamie Gritton for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 1201b97457e2SJamie Gritton tpr->pr_childcount++; 1202b38ff370SJamie Gritton 12030304c731SJamie Gritton pr->pr_parent = ppr; 1204b38ff370SJamie Gritton pr->pr_id = jid; 12050304c731SJamie Gritton 12060304c731SJamie Gritton /* Set some default values, and inherit some from the parent. */ 1207b6f47c23SJamie Gritton if (namelc == NULL) 1208b6f47c23SJamie Gritton namelc = ""; 1209b38ff370SJamie Gritton if (path == NULL) { 1210b38ff370SJamie Gritton path = "/"; 12110304c731SJamie Gritton root = mypr->pr_root; 1212b38ff370SJamie Gritton vref(root); 1213b38ff370SJamie Gritton } 12148986e3a0SJamie Gritton strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN); 12158986e3a0SJamie Gritton pr->pr_flags |= PR_HOST; 1216bdfc8cc4SJamie Gritton #if defined(INET) || defined(INET6) 1217bdfc8cc4SJamie Gritton #ifdef VIMAGE 1218bdfc8cc4SJamie Gritton if (!(pr_flags & PR_VNET)) 1219bdfc8cc4SJamie Gritton #endif 1220bdfc8cc4SJamie Gritton { 12210304c731SJamie Gritton #ifdef INET 12222b0d6f81SJamie Gritton if (!(ch_flags & PR_IP4_USER)) 12236a3f2779SJamie Gritton pr->pr_flags |= PR_IP4 | PR_IP4_USER; 12242b0d6f81SJamie Gritton else if (!(pr_flags & PR_IP4_USER)) { 12252b0d6f81SJamie Gritton pr->pr_flags |= ppr->pr_flags & PR_IP4; 12262b0d6f81SJamie Gritton if (ppr->pr_ip4 != NULL) { 12272b0d6f81SJamie Gritton pr->pr_ip4s = ppr->pr_ip4s; 12282b0d6f81SJamie Gritton pr->pr_ip4 = malloc(pr->pr_ip4s * 12292b0d6f81SJamie Gritton sizeof(struct in_addr), M_PRISON, 12302b0d6f81SJamie Gritton M_WAITOK); 12312b0d6f81SJamie Gritton bcopy(ppr->pr_ip4, pr->pr_ip4, 12322b0d6f81SJamie Gritton pr->pr_ip4s * sizeof(*pr->pr_ip4)); 12332b0d6f81SJamie Gritton } 12342b0d6f81SJamie Gritton } 12350304c731SJamie Gritton #endif 12360304c731SJamie Gritton #ifdef INET6 12372b0d6f81SJamie Gritton if (!(ch_flags & PR_IP6_USER)) 12386a3f2779SJamie Gritton pr->pr_flags |= PR_IP6 | PR_IP6_USER; 12392b0d6f81SJamie Gritton else if (!(pr_flags & PR_IP6_USER)) { 12402b0d6f81SJamie Gritton pr->pr_flags |= ppr->pr_flags & PR_IP6; 12412b0d6f81SJamie Gritton if (ppr->pr_ip6 != NULL) { 12422b0d6f81SJamie Gritton pr->pr_ip6s = ppr->pr_ip6s; 12432b0d6f81SJamie Gritton pr->pr_ip6 = malloc(pr->pr_ip6s * 12442b0d6f81SJamie Gritton sizeof(struct in6_addr), M_PRISON, 12452b0d6f81SJamie Gritton M_WAITOK); 12462b0d6f81SJamie Gritton bcopy(ppr->pr_ip6, pr->pr_ip6, 12472b0d6f81SJamie Gritton pr->pr_ip6s * sizeof(*pr->pr_ip6)); 12482b0d6f81SJamie Gritton } 12492b0d6f81SJamie Gritton } 12500304c731SJamie Gritton #endif 1251bdfc8cc4SJamie Gritton } 1252bdfc8cc4SJamie Gritton #endif 1253592bcae8SBjoern A. Zeeb /* Source address selection is always on by default. */ 1254592bcae8SBjoern A. Zeeb pr->pr_flags |= _PR_IP_SADDRSEL; 1255592bcae8SBjoern A. Zeeb 12560304c731SJamie Gritton pr->pr_securelevel = ppr->pr_securelevel; 12570304c731SJamie Gritton pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow; 1258af10bf05SBjoern A. Zeeb pr->pr_enforce_statfs = jail_default_enforce_statfs; 1259bf3db8aaSMartin Matuska pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum; 1260b38ff370SJamie Gritton 1261b96bd95bSIan Lepore pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate; 1262b96bd95bSIan Lepore if (osrelstr == NULL) 1263b96bd95bSIan Lepore strcpy(pr->pr_osrelease, ppr->pr_osrelease); 1264b96bd95bSIan Lepore else 1265b96bd95bSIan Lepore strcpy(pr->pr_osrelease, osrelstr); 1266b96bd95bSIan Lepore 12670304c731SJamie Gritton LIST_INIT(&pr->pr_children); 12680304c731SJamie Gritton mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK); 126973d9e52dSJamie Gritton TASK_INIT(&pr->pr_task, 0, prison_complete, pr); 1270b38ff370SJamie Gritton 1271679e1390SJamie Gritton #ifdef VIMAGE 1272679e1390SJamie Gritton /* Allocate a new vnet if specified. */ 1273679e1390SJamie Gritton pr->pr_vnet = (pr_flags & PR_VNET) 1274679e1390SJamie Gritton ? vnet_alloc() : ppr->pr_vnet; 1275679e1390SJamie Gritton #endif 1276b38ff370SJamie Gritton /* 1277b38ff370SJamie Gritton * Allocate a dedicated cpuset for each jail. 1278b38ff370SJamie Gritton * Unlike other initial settings, this may return an erorr. 1279b38ff370SJamie Gritton */ 12800304c731SJamie Gritton error = cpuset_create_root(ppr, &pr->pr_cpuset); 1281b38ff370SJamie Gritton if (error) { 1282b38ff370SJamie Gritton prison_deref(pr, PD_LIST_XLOCKED); 1283b38ff370SJamie Gritton goto done_releroot; 1284b38ff370SJamie Gritton } 1285b38ff370SJamie Gritton 1286b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1287b38ff370SJamie Gritton /* 1288b38ff370SJamie Gritton * New prisons do not yet have a reference, because we do not 1289b6f47c23SJamie Gritton * want others to see the incomplete prison once the 1290b38ff370SJamie Gritton * allprison_lock is downgraded. 1291b38ff370SJamie Gritton */ 1292b38ff370SJamie Gritton } else { 1293b38ff370SJamie Gritton created = 0; 12942b0d6f81SJamie Gritton /* 12952b0d6f81SJamie Gritton * Grab a reference for existing prisons, to ensure they 12962b0d6f81SJamie Gritton * continue to exist for the duration of the call. 12972b0d6f81SJamie Gritton */ 12982b0d6f81SJamie Gritton pr->pr_ref++; 1299bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6)) 1300bdfc8cc4SJamie Gritton if ((pr->pr_flags & PR_VNET) && 1301bdfc8cc4SJamie Gritton (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 1302bdfc8cc4SJamie Gritton error = EINVAL; 1303bdfc8cc4SJamie Gritton vfs_opterror(opts, 1304bdfc8cc4SJamie Gritton "vnet jails cannot have IP address restrictions"); 1305bdfc8cc4SJamie Gritton goto done_deref_locked; 1306bdfc8cc4SJamie Gritton } 1307bdfc8cc4SJamie Gritton #endif 13082b0d6f81SJamie Gritton #ifdef INET 13092b0d6f81SJamie Gritton if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 13102b0d6f81SJamie Gritton error = EINVAL; 13112b0d6f81SJamie Gritton vfs_opterror(opts, 13122b0d6f81SJamie Gritton "ip4 cannot be changed after creation"); 13132b0d6f81SJamie Gritton goto done_deref_locked; 13142b0d6f81SJamie Gritton } 13152b0d6f81SJamie Gritton #endif 13162b0d6f81SJamie Gritton #ifdef INET6 13172b0d6f81SJamie Gritton if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 13182b0d6f81SJamie Gritton error = EINVAL; 13192b0d6f81SJamie Gritton vfs_opterror(opts, 13202b0d6f81SJamie Gritton "ip6 cannot be changed after creation"); 13212b0d6f81SJamie Gritton goto done_deref_locked; 13222b0d6f81SJamie Gritton } 13232b0d6f81SJamie Gritton #endif 1324b38ff370SJamie Gritton } 1325b38ff370SJamie Gritton 1326b38ff370SJamie Gritton /* Do final error checking before setting anything. */ 13270304c731SJamie Gritton if (gotslevel) { 13280304c731SJamie Gritton if (slevel < ppr->pr_securelevel) { 13290304c731SJamie Gritton error = EPERM; 13300304c731SJamie Gritton goto done_deref_locked; 13310304c731SJamie Gritton } 13320304c731SJamie Gritton } 1333b97457e2SJamie Gritton if (gotchildmax) { 1334b97457e2SJamie Gritton if (childmax >= ppr->pr_childmax) { 1335b97457e2SJamie Gritton error = EPERM; 1336b97457e2SJamie Gritton goto done_deref_locked; 1337b97457e2SJamie Gritton } 1338b97457e2SJamie Gritton } 13390304c731SJamie Gritton if (gotenforce) { 13400304c731SJamie Gritton if (enforce < ppr->pr_enforce_statfs) { 13410304c731SJamie Gritton error = EPERM; 13420304c731SJamie Gritton goto done_deref_locked; 13430304c731SJamie Gritton } 13440304c731SJamie Gritton } 13450cc207a6SMartin Matuska if (gotrsnum) { 13460cc207a6SMartin Matuska /* 13470cc207a6SMartin Matuska * devfs_rsnum is a uint16_t 13480cc207a6SMartin Matuska */ 1349bf3db8aaSMartin Matuska if (rsnum < 0 || rsnum > 65535) { 13500cc207a6SMartin Matuska error = EINVAL; 13510cc207a6SMartin Matuska goto done_deref_locked; 13520cc207a6SMartin Matuska } 13530cc207a6SMartin Matuska /* 1354bf3db8aaSMartin Matuska * Nested jails always inherit parent's devfs ruleset 13550cc207a6SMartin Matuska */ 13560cc207a6SMartin Matuska if (jailed(td->td_ucred)) { 13570cc207a6SMartin Matuska if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) { 13580cc207a6SMartin Matuska error = EPERM; 13590cc207a6SMartin Matuska goto done_deref_locked; 1360bf3db8aaSMartin Matuska } else 13610cc207a6SMartin Matuska rsnum = ppr->pr_devfs_rsnum; 13620cc207a6SMartin Matuska } 13630cc207a6SMartin Matuska } 1364b38ff370SJamie Gritton #ifdef INET 13652b0d6f81SJamie Gritton if (ip4s > 0) { 13660304c731SJamie Gritton if (ppr->pr_flags & PR_IP4) { 13670304c731SJamie Gritton /* 13680304c731SJamie Gritton * Make sure the new set of IP addresses is a 13690304c731SJamie Gritton * subset of the parent's list. Don't worry 13700304c731SJamie Gritton * about the parent being unlocked, as any 13710304c731SJamie Gritton * setting is done with allprison_lock held. 13720304c731SJamie Gritton */ 13730304c731SJamie Gritton for (ij = 0; ij < ppr->pr_ip4s; ij++) 13742b0d6f81SJamie Gritton if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 13750304c731SJamie Gritton break; 13760304c731SJamie Gritton if (ij == ppr->pr_ip4s) { 13770304c731SJamie Gritton error = EPERM; 13780304c731SJamie Gritton goto done_deref_locked; 13790304c731SJamie Gritton } 13800304c731SJamie Gritton if (ip4s > 1) { 13810304c731SJamie Gritton for (ii = ij = 1; ii < ip4s; ii++) { 13820304c731SJamie Gritton if (ip4[ii].s_addr == 13830304c731SJamie Gritton ppr->pr_ip4[0].s_addr) 1384b38ff370SJamie Gritton continue; 13850304c731SJamie Gritton for (; ij < ppr->pr_ip4s; ij++) 13860304c731SJamie Gritton if (ip4[ii].s_addr == 13870304c731SJamie Gritton ppr->pr_ip4[ij].s_addr) 13880304c731SJamie Gritton break; 13890304c731SJamie Gritton if (ij == ppr->pr_ip4s) 13900304c731SJamie Gritton break; 13910304c731SJamie Gritton } 13920304c731SJamie Gritton if (ij == ppr->pr_ip4s) { 13930304c731SJamie Gritton error = EPERM; 13940304c731SJamie Gritton goto done_deref_locked; 13950304c731SJamie Gritton } 13960304c731SJamie Gritton } 13970304c731SJamie Gritton } 13980304c731SJamie Gritton /* 13990304c731SJamie Gritton * Check for conflicting IP addresses. We permit them 14000304c731SJamie Gritton * if there is no more than one IP on each jail. If 14010304c731SJamie Gritton * there is a duplicate on a jail with more than one 14020304c731SJamie Gritton * IP stop checking and return error. 14030304c731SJamie Gritton */ 1404bdfc8cc4SJamie Gritton tppr = ppr; 1405bdfc8cc4SJamie Gritton #ifdef VIMAGE 1406bdfc8cc4SJamie Gritton for (; tppr != &prison0; tppr = tppr->pr_parent) 1407bdfc8cc4SJamie Gritton if (tppr->pr_flags & PR_VNET) 1408bdfc8cc4SJamie Gritton break; 1409bdfc8cc4SJamie Gritton #endif 1410bdfc8cc4SJamie Gritton FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1411bdfc8cc4SJamie Gritton if (tpr == pr || 1412bdfc8cc4SJamie Gritton #ifdef VIMAGE 14132b0d6f81SJamie Gritton (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1414bdfc8cc4SJamie Gritton #endif 1415bdfc8cc4SJamie Gritton tpr->pr_uref == 0) { 14160304c731SJamie Gritton descend = 0; 14170304c731SJamie Gritton continue; 14180304c731SJamie Gritton } 14190304c731SJamie Gritton if (!(tpr->pr_flags & PR_IP4_USER)) 14200304c731SJamie Gritton continue; 14210304c731SJamie Gritton descend = 0; 14220304c731SJamie Gritton if (tpr->pr_ip4 == NULL || 14230304c731SJamie Gritton (ip4s == 1 && tpr->pr_ip4s == 1)) 14240304c731SJamie Gritton continue; 14250304c731SJamie Gritton for (ii = 0; ii < ip4s; ii++) { 14260ce1624dSStephen J. Kiernan if (prison_check_ip4_locked(tpr, &ip4[ii]) == 14270ce1624dSStephen J. Kiernan 0) { 14280304c731SJamie Gritton error = EADDRINUSE; 1429b38ff370SJamie Gritton vfs_opterror(opts, 1430b38ff370SJamie Gritton "IPv4 addresses clash"); 1431b38ff370SJamie Gritton goto done_deref_locked; 1432b38ff370SJamie Gritton } 14330304c731SJamie Gritton } 14340304c731SJamie Gritton } 14350304c731SJamie Gritton } 1436b38ff370SJamie Gritton #endif 1437b38ff370SJamie Gritton #ifdef INET6 14382b0d6f81SJamie Gritton if (ip6s > 0) { 14390304c731SJamie Gritton if (ppr->pr_flags & PR_IP6) { 14400304c731SJamie Gritton /* 14410304c731SJamie Gritton * Make sure the new set of IP addresses is a 14420304c731SJamie Gritton * subset of the parent's list. 14430304c731SJamie Gritton */ 14440304c731SJamie Gritton for (ij = 0; ij < ppr->pr_ip6s; ij++) 14450304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL(&ip6[0], 14460304c731SJamie Gritton &ppr->pr_ip6[ij])) 14470304c731SJamie Gritton break; 14480304c731SJamie Gritton if (ij == ppr->pr_ip6s) { 14490304c731SJamie Gritton error = EPERM; 14500304c731SJamie Gritton goto done_deref_locked; 14510304c731SJamie Gritton } 14520304c731SJamie Gritton if (ip6s > 1) { 14530304c731SJamie Gritton for (ii = ij = 1; ii < ip6s; ii++) { 14540304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL(&ip6[ii], 14550304c731SJamie Gritton &ppr->pr_ip6[0])) 14560304c731SJamie Gritton continue; 14570304c731SJamie Gritton for (; ij < ppr->pr_ip6s; ij++) 14580304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL( 14592b0d6f81SJamie Gritton &ip6[ii], &ppr->pr_ip6[ij])) 14600304c731SJamie Gritton break; 14610304c731SJamie Gritton if (ij == ppr->pr_ip6s) 14620304c731SJamie Gritton break; 14630304c731SJamie Gritton } 14640304c731SJamie Gritton if (ij == ppr->pr_ip6s) { 14650304c731SJamie Gritton error = EPERM; 14660304c731SJamie Gritton goto done_deref_locked; 14670304c731SJamie Gritton } 14680304c731SJamie Gritton } 14690304c731SJamie Gritton } 14700304c731SJamie Gritton /* Check for conflicting IP addresses. */ 1471bdfc8cc4SJamie Gritton tppr = ppr; 1472bdfc8cc4SJamie Gritton #ifdef VIMAGE 1473bdfc8cc4SJamie Gritton for (; tppr != &prison0; tppr = tppr->pr_parent) 1474bdfc8cc4SJamie Gritton if (tppr->pr_flags & PR_VNET) 1475bdfc8cc4SJamie Gritton break; 1476bdfc8cc4SJamie Gritton #endif 1477bdfc8cc4SJamie Gritton FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1478bdfc8cc4SJamie Gritton if (tpr == pr || 1479bdfc8cc4SJamie Gritton #ifdef VIMAGE 14802b0d6f81SJamie Gritton (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1481bdfc8cc4SJamie Gritton #endif 1482bdfc8cc4SJamie Gritton tpr->pr_uref == 0) { 14830304c731SJamie Gritton descend = 0; 14840304c731SJamie Gritton continue; 14850304c731SJamie Gritton } 14860304c731SJamie Gritton if (!(tpr->pr_flags & PR_IP6_USER)) 14870304c731SJamie Gritton continue; 14880304c731SJamie Gritton descend = 0; 14890304c731SJamie Gritton if (tpr->pr_ip6 == NULL || 14900304c731SJamie Gritton (ip6s == 1 && tpr->pr_ip6s == 1)) 14910304c731SJamie Gritton continue; 14920304c731SJamie Gritton for (ii = 0; ii < ip6s; ii++) { 14930ce1624dSStephen J. Kiernan if (prison_check_ip6_locked(tpr, &ip6[ii]) == 14940ce1624dSStephen J. Kiernan 0) { 14950304c731SJamie Gritton error = EADDRINUSE; 1496b38ff370SJamie Gritton vfs_opterror(opts, 1497b38ff370SJamie Gritton "IPv6 addresses clash"); 1498b38ff370SJamie Gritton goto done_deref_locked; 1499b38ff370SJamie Gritton } 15000304c731SJamie Gritton } 15010304c731SJamie Gritton } 15020304c731SJamie Gritton } 1503b38ff370SJamie Gritton #endif 15040304c731SJamie Gritton onamelen = namelen = 0; 1505b6f47c23SJamie Gritton if (namelc != NULL) { 15066ab6058eSJamie Gritton /* Give a default name of the jid. Also allow the name to be 15076ab6058eSJamie Gritton * explicitly the jid - but not any other number, and only in 15086ab6058eSJamie Gritton * normal form (no leading zero/etc). 15096ab6058eSJamie Gritton */ 1510b6f47c23SJamie Gritton if (namelc[0] == '\0') 1511b6f47c23SJamie Gritton snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid); 15126ab6058eSJamie Gritton else if ((strtoul(namelc, &p, 10) != jid || 15136ab6058eSJamie Gritton namelc[0] < '1' || namelc[0] > '9') && *p == '\0') { 1514b38ff370SJamie Gritton error = EINVAL; 1515babbbb9cSJamie Gritton vfs_opterror(opts, 1516babbbb9cSJamie Gritton "name cannot be numeric (unless it is the jid)"); 15170304c731SJamie Gritton goto done_deref_locked; 1518b38ff370SJamie Gritton } 1519b38ff370SJamie Gritton /* 15200304c731SJamie Gritton * Make sure the name isn't too long for the prison or its 15210304c731SJamie Gritton * children. 1522b38ff370SJamie Gritton */ 1523b6f47c23SJamie Gritton pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 1524b6f47c23SJamie Gritton onamelen = strlen(pr->pr_name + pnamelen); 1525b6f47c23SJamie Gritton namelen = strlen(namelc); 1526b6f47c23SJamie Gritton if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) { 15270304c731SJamie Gritton error = ENAMETOOLONG; 15280304c731SJamie Gritton goto done_deref_locked; 15290304c731SJamie Gritton } 15300304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 15310304c731SJamie Gritton if (strlen(tpr->pr_name) + (namelen - onamelen) >= 15320304c731SJamie Gritton sizeof(pr->pr_name)) { 15330304c731SJamie Gritton error = ENAMETOOLONG; 15340304c731SJamie Gritton goto done_deref_locked; 15350304c731SJamie Gritton } 15360304c731SJamie Gritton } 15370304c731SJamie Gritton } 15380304c731SJamie Gritton if (pr_allow & ~ppr->pr_allow) { 15390304c731SJamie Gritton error = EPERM; 15400304c731SJamie Gritton goto done_deref_locked; 1541b38ff370SJamie Gritton } 1542b38ff370SJamie Gritton 1543b6f47c23SJamie Gritton /* 1544b6f47c23SJamie Gritton * Let modules check their parameters. This requires unlocking and 1545b6f47c23SJamie Gritton * then re-locking the prison, but this is still a valid state as long 1546b6f47c23SJamie Gritton * as allprison_lock remains xlocked. 1547b6f47c23SJamie Gritton */ 1548b6f47c23SJamie Gritton mtx_unlock(&pr->pr_mtx); 1549b6f47c23SJamie Gritton error = osd_jail_call(pr, PR_METHOD_CHECK, opts); 1550b6f47c23SJamie Gritton if (error != 0) { 1551b6f47c23SJamie Gritton prison_deref(pr, created 1552b6f47c23SJamie Gritton ? PD_LIST_XLOCKED 1553b6f47c23SJamie Gritton : PD_DEREF | PD_LIST_XLOCKED); 1554b6f47c23SJamie Gritton goto done_releroot; 1555b6f47c23SJamie Gritton } 1556b6f47c23SJamie Gritton mtx_lock(&pr->pr_mtx); 1557b6f47c23SJamie Gritton 1558b6f47c23SJamie Gritton /* At this point, all valid parameters should have been noted. */ 1559b6f47c23SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 1560b6f47c23SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 1561b6f47c23SJamie Gritton error = EINVAL; 1562b6f47c23SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 1563b6f47c23SJamie Gritton goto done_deref_locked; 1564b6f47c23SJamie Gritton } 1565b6f47c23SJamie Gritton } 1566b6f47c23SJamie Gritton 1567b38ff370SJamie Gritton /* Set the parameters of the prison. */ 1568b38ff370SJamie Gritton #ifdef INET 15690304c731SJamie Gritton redo_ip4 = 0; 15700304c731SJamie Gritton if (pr_flags & PR_IP4_USER) { 15710304c731SJamie Gritton pr->pr_flags |= PR_IP4; 1572b38ff370SJamie Gritton free(pr->pr_ip4, M_PRISON); 15730304c731SJamie Gritton pr->pr_ip4s = ip4s; 1574b38ff370SJamie Gritton pr->pr_ip4 = ip4; 1575b38ff370SJamie Gritton ip4 = NULL; 15760304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1577bdfc8cc4SJamie Gritton #ifdef VIMAGE 1578bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1579bdfc8cc4SJamie Gritton descend = 0; 1580bdfc8cc4SJamie Gritton continue; 1581bdfc8cc4SJamie Gritton } 1582bdfc8cc4SJamie Gritton #endif 15830304c731SJamie Gritton if (prison_restrict_ip4(tpr, NULL)) { 15840304c731SJamie Gritton redo_ip4 = 1; 15850304c731SJamie Gritton descend = 0; 15860304c731SJamie Gritton } 15870304c731SJamie Gritton } 15880304c731SJamie Gritton } 1589b38ff370SJamie Gritton #endif 1590b38ff370SJamie Gritton #ifdef INET6 15910304c731SJamie Gritton redo_ip6 = 0; 15920304c731SJamie Gritton if (pr_flags & PR_IP6_USER) { 15930304c731SJamie Gritton pr->pr_flags |= PR_IP6; 1594b38ff370SJamie Gritton free(pr->pr_ip6, M_PRISON); 15950304c731SJamie Gritton pr->pr_ip6s = ip6s; 1596b38ff370SJamie Gritton pr->pr_ip6 = ip6; 1597b38ff370SJamie Gritton ip6 = NULL; 15980304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1599bdfc8cc4SJamie Gritton #ifdef VIMAGE 1600bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1601bdfc8cc4SJamie Gritton descend = 0; 1602bdfc8cc4SJamie Gritton continue; 1603bdfc8cc4SJamie Gritton } 1604bdfc8cc4SJamie Gritton #endif 16050304c731SJamie Gritton if (prison_restrict_ip6(tpr, NULL)) { 16060304c731SJamie Gritton redo_ip6 = 1; 16070304c731SJamie Gritton descend = 0; 16080304c731SJamie Gritton } 16090304c731SJamie Gritton } 16100304c731SJamie Gritton } 1611b38ff370SJamie Gritton #endif 16120304c731SJamie Gritton if (gotslevel) { 1613b38ff370SJamie Gritton pr->pr_securelevel = slevel; 16140304c731SJamie Gritton /* Set all child jails to be at least this level. */ 16150304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 16160304c731SJamie Gritton if (tpr->pr_securelevel < slevel) 16170304c731SJamie Gritton tpr->pr_securelevel = slevel; 16180304c731SJamie Gritton } 1619b97457e2SJamie Gritton if (gotchildmax) { 1620b97457e2SJamie Gritton pr->pr_childmax = childmax; 1621b97457e2SJamie Gritton /* Set all child jails to under this limit. */ 1622b97457e2SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level) 1623b97457e2SJamie Gritton if (tpr->pr_childmax > childmax - level) 1624b97457e2SJamie Gritton tpr->pr_childmax = childmax > level 1625b97457e2SJamie Gritton ? childmax - level : 0; 1626b97457e2SJamie Gritton } 16270304c731SJamie Gritton if (gotenforce) { 16280304c731SJamie Gritton pr->pr_enforce_statfs = enforce; 16290304c731SJamie Gritton /* Pass this restriction on to the children. */ 16300304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 16310304c731SJamie Gritton if (tpr->pr_enforce_statfs < enforce) 16320304c731SJamie Gritton tpr->pr_enforce_statfs = enforce; 16330304c731SJamie Gritton } 16340cc207a6SMartin Matuska if (gotrsnum) { 16350cc207a6SMartin Matuska pr->pr_devfs_rsnum = rsnum; 16360cc207a6SMartin Matuska /* Pass this restriction on to the children. */ 16370cc207a6SMartin Matuska FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 16380cc207a6SMartin Matuska tpr->pr_devfs_rsnum = rsnum; 16390cc207a6SMartin Matuska } 1640b6f47c23SJamie Gritton if (namelc != NULL) { 16410304c731SJamie Gritton if (ppr == &prison0) 1642b6f47c23SJamie Gritton strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name)); 16430304c731SJamie Gritton else 16440304c731SJamie Gritton snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s", 1645b6f47c23SJamie Gritton ppr->pr_name, namelc); 16460304c731SJamie Gritton /* Change this component of child names. */ 16470304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 16480304c731SJamie Gritton bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen, 16490304c731SJamie Gritton strlen(tpr->pr_name + onamelen) + 1); 16500304c731SJamie Gritton bcopy(pr->pr_name, tpr->pr_name, namelen); 16510304c731SJamie Gritton } 16520304c731SJamie Gritton } 1653b38ff370SJamie Gritton if (path != NULL) { 16540304c731SJamie Gritton /* Try to keep a real-rooted full pathname. */ 1655f6e633a9SMartin Matuska if (fullpath_disabled && path[0] == '/' && 1656f6e633a9SMartin Matuska strcmp(mypr->pr_path, "/")) 16570304c731SJamie Gritton snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s", 16580304c731SJamie Gritton mypr->pr_path, path); 16590304c731SJamie Gritton else 1660b38ff370SJamie Gritton strlcpy(pr->pr_path, path, sizeof(pr->pr_path)); 1661b38ff370SJamie Gritton pr->pr_root = root; 1662b38ff370SJamie Gritton } 166376ca6f88SJamie Gritton if (PR_HOST & ch_flags & ~pr_flags) { 166476ca6f88SJamie Gritton if (pr->pr_flags & PR_HOST) { 166576ca6f88SJamie Gritton /* 166676ca6f88SJamie Gritton * Copy the parent's host info. As with pr_ip4 above, 166776ca6f88SJamie Gritton * the lack of a lock on the parent is not a problem; 166876ca6f88SJamie Gritton * it is always set with allprison_lock at least 166976ca6f88SJamie Gritton * shared, and is held exclusively here. 167076ca6f88SJamie Gritton */ 1671c1f19219SJamie Gritton strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname, 1672c1f19219SJamie Gritton sizeof(pr->pr_hostname)); 1673c1f19219SJamie Gritton strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname, 1674c1f19219SJamie Gritton sizeof(pr->pr_domainname)); 1675c1f19219SJamie Gritton strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid, 1676c1f19219SJamie Gritton sizeof(pr->pr_hostuuid)); 167776ca6f88SJamie Gritton pr->pr_hostid = pr->pr_parent->pr_hostid; 167876ca6f88SJamie Gritton } 167976ca6f88SJamie Gritton } else if (host != NULL || domain != NULL || uuid != NULL || gothid) { 168076ca6f88SJamie Gritton /* Set this prison, and any descendants without PR_HOST. */ 1681b38ff370SJamie Gritton if (host != NULL) 1682c1f19219SJamie Gritton strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname)); 168376ca6f88SJamie Gritton if (domain != NULL) 1684c1f19219SJamie Gritton strlcpy(pr->pr_domainname, domain, 1685c1f19219SJamie Gritton sizeof(pr->pr_domainname)); 168676ca6f88SJamie Gritton if (uuid != NULL) 1687c1f19219SJamie Gritton strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid)); 168876ca6f88SJamie Gritton if (gothid) 168976ca6f88SJamie Gritton pr->pr_hostid = hid; 169076ca6f88SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 169176ca6f88SJamie Gritton if (tpr->pr_flags & PR_HOST) 169276ca6f88SJamie Gritton descend = 0; 169376ca6f88SJamie Gritton else { 169476ca6f88SJamie Gritton if (host != NULL) 1695c1f19219SJamie Gritton strlcpy(tpr->pr_hostname, 1696c1f19219SJamie Gritton pr->pr_hostname, 1697c1f19219SJamie Gritton sizeof(tpr->pr_hostname)); 169876ca6f88SJamie Gritton if (domain != NULL) 1699c1f19219SJamie Gritton strlcpy(tpr->pr_domainname, 1700c1f19219SJamie Gritton pr->pr_domainname, 1701c1f19219SJamie Gritton sizeof(tpr->pr_domainname)); 170276ca6f88SJamie Gritton if (uuid != NULL) 1703c1f19219SJamie Gritton strlcpy(tpr->pr_hostuuid, 1704c1f19219SJamie Gritton pr->pr_hostuuid, 1705c1f19219SJamie Gritton sizeof(tpr->pr_hostuuid)); 170676ca6f88SJamie Gritton if (gothid) 170776ca6f88SJamie Gritton tpr->pr_hostid = hid; 170876ca6f88SJamie Gritton } 170976ca6f88SJamie Gritton } 171076ca6f88SJamie Gritton } 17110304c731SJamie Gritton if ((tallow = ch_allow & ~pr_allow)) { 17120304c731SJamie Gritton /* Clear allow bits in all children. */ 17130304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 17140304c731SJamie Gritton tpr->pr_allow &= ~tallow; 17150304c731SJamie Gritton } 17160304c731SJamie Gritton pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow; 1717b38ff370SJamie Gritton /* 1718b38ff370SJamie Gritton * Persistent prisons get an extra reference, and prisons losing their 1719b38ff370SJamie Gritton * persist flag lose that reference. Only do this for existing prisons 1720b38ff370SJamie Gritton * for now, so new ones will remain unseen until after the module 1721b38ff370SJamie Gritton * handlers have completed. 1722b38ff370SJamie Gritton */ 1723cc5fd8c7SJamie Gritton born = pr->pr_uref == 0; 1724b38ff370SJamie Gritton if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) { 1725b38ff370SJamie Gritton if (pr_flags & PR_PERSIST) { 1726b38ff370SJamie Gritton pr->pr_ref++; 1727b38ff370SJamie Gritton pr->pr_uref++; 1728b38ff370SJamie Gritton } else { 1729b38ff370SJamie Gritton pr->pr_ref--; 1730b38ff370SJamie Gritton pr->pr_uref--; 1731b38ff370SJamie Gritton } 1732b38ff370SJamie Gritton } 1733b38ff370SJamie Gritton pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags; 1734b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1735b38ff370SJamie Gritton 1736a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 17374b5c9cf6SEdward Tomasz Napierala if (racct_enable && created) 1738a7ad07bfSEdward Tomasz Napierala prison_racct_attach(pr); 1739a7ad07bfSEdward Tomasz Napierala #endif 1740a7ad07bfSEdward Tomasz Napierala 17410304c731SJamie Gritton /* Locks may have prevented a complete restriction of child IP 17420304c731SJamie Gritton * addresses. If so, allocate some more memory and try again. 17430304c731SJamie Gritton */ 17440304c731SJamie Gritton #ifdef INET 17450304c731SJamie Gritton while (redo_ip4) { 17460304c731SJamie Gritton ip4s = pr->pr_ip4s; 17470304c731SJamie Gritton ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 17480304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 17490304c731SJamie Gritton redo_ip4 = 0; 17500304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1751bdfc8cc4SJamie Gritton #ifdef VIMAGE 1752bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1753bdfc8cc4SJamie Gritton descend = 0; 1754bdfc8cc4SJamie Gritton continue; 1755bdfc8cc4SJamie Gritton } 1756bdfc8cc4SJamie Gritton #endif 17570304c731SJamie Gritton if (prison_restrict_ip4(tpr, ip4)) { 17580304c731SJamie Gritton if (ip4 != NULL) 17590304c731SJamie Gritton ip4 = NULL; 17600304c731SJamie Gritton else 17610304c731SJamie Gritton redo_ip4 = 1; 17620304c731SJamie Gritton } 17630304c731SJamie Gritton } 17640304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 17650304c731SJamie Gritton } 17660304c731SJamie Gritton #endif 17670304c731SJamie Gritton #ifdef INET6 17680304c731SJamie Gritton while (redo_ip6) { 17690304c731SJamie Gritton ip6s = pr->pr_ip6s; 17700304c731SJamie Gritton ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 17710304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 17720304c731SJamie Gritton redo_ip6 = 0; 17730304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1774bdfc8cc4SJamie Gritton #ifdef VIMAGE 1775bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1776bdfc8cc4SJamie Gritton descend = 0; 1777bdfc8cc4SJamie Gritton continue; 1778bdfc8cc4SJamie Gritton } 1779bdfc8cc4SJamie Gritton #endif 17800304c731SJamie Gritton if (prison_restrict_ip6(tpr, ip6)) { 17810304c731SJamie Gritton if (ip6 != NULL) 17820304c731SJamie Gritton ip6 = NULL; 17830304c731SJamie Gritton else 17840304c731SJamie Gritton redo_ip6 = 1; 17850304c731SJamie Gritton } 17860304c731SJamie Gritton } 17870304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 17880304c731SJamie Gritton } 17890304c731SJamie Gritton #endif 17900304c731SJamie Gritton 1791b38ff370SJamie Gritton /* Let the modules do their work. */ 1792b38ff370SJamie Gritton sx_downgrade(&allprison_lock); 1793cc5fd8c7SJamie Gritton if (born) { 1794b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_CREATE, opts); 1795b38ff370SJamie Gritton if (error) { 1796cc5fd8c7SJamie Gritton (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL); 1797cc5fd8c7SJamie Gritton prison_deref(pr, created 1798cc5fd8c7SJamie Gritton ? PD_LIST_SLOCKED 1799cc5fd8c7SJamie Gritton : PD_DEREF | PD_LIST_SLOCKED); 1800b38ff370SJamie Gritton goto done_errmsg; 1801b38ff370SJamie Gritton } 1802b38ff370SJamie Gritton } 1803b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_SET, opts); 1804b38ff370SJamie Gritton if (error) { 1805cc5fd8c7SJamie Gritton if (born) 1806cc5fd8c7SJamie Gritton (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL); 1807b38ff370SJamie Gritton prison_deref(pr, created 1808b38ff370SJamie Gritton ? PD_LIST_SLOCKED 1809b38ff370SJamie Gritton : PD_DEREF | PD_LIST_SLOCKED); 1810b38ff370SJamie Gritton goto done_errmsg; 1811b38ff370SJamie Gritton } 1812b38ff370SJamie Gritton 1813b38ff370SJamie Gritton /* Attach this process to the prison if requested. */ 1814b38ff370SJamie Gritton if (flags & JAIL_ATTACH) { 1815b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1816b38ff370SJamie Gritton error = do_jail_attach(td, pr); 1817b38ff370SJamie Gritton if (error) { 1818b38ff370SJamie Gritton vfs_opterror(opts, "attach failed"); 1819b38ff370SJamie Gritton if (!created) 1820b38ff370SJamie Gritton prison_deref(pr, PD_DEREF); 1821b38ff370SJamie Gritton goto done_errmsg; 1822b38ff370SJamie Gritton } 1823b38ff370SJamie Gritton } 1824b38ff370SJamie Gritton 18251fb24974SEdward Tomasz Napierala #ifdef RACCT 18264b5c9cf6SEdward Tomasz Napierala if (racct_enable && !created) { 1827f514b97bSEdward Tomasz Napierala if (!(flags & JAIL_ATTACH)) 18281fb24974SEdward Tomasz Napierala sx_sunlock(&allprison_lock); 18291fb24974SEdward Tomasz Napierala prison_racct_modify(pr); 1830f514b97bSEdward Tomasz Napierala if (!(flags & JAIL_ATTACH)) 18311fb24974SEdward Tomasz Napierala sx_slock(&allprison_lock); 18321fb24974SEdward Tomasz Napierala } 18331fb24974SEdward Tomasz Napierala #endif 18341fb24974SEdward Tomasz Napierala 18351fb24974SEdward Tomasz Napierala td->td_retval[0] = pr->pr_id; 18361fb24974SEdward Tomasz Napierala 1837b38ff370SJamie Gritton /* 1838b38ff370SJamie Gritton * Now that it is all there, drop the temporary reference from existing 1839b38ff370SJamie Gritton * prisons. Or add a reference to newly created persistent prisons 1840b38ff370SJamie Gritton * (which was not done earlier so that the prison would not be publicly 1841b38ff370SJamie Gritton * visible). 1842b38ff370SJamie Gritton */ 1843b38ff370SJamie Gritton if (!created) { 1844b38ff370SJamie Gritton prison_deref(pr, (flags & JAIL_ATTACH) 1845b38ff370SJamie Gritton ? PD_DEREF 1846b38ff370SJamie Gritton : PD_DEREF | PD_LIST_SLOCKED); 1847b38ff370SJamie Gritton } else { 1848b38ff370SJamie Gritton if (pr_flags & PR_PERSIST) { 1849b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1850b38ff370SJamie Gritton pr->pr_ref++; 1851b38ff370SJamie Gritton pr->pr_uref++; 1852b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1853b38ff370SJamie Gritton } 1854b38ff370SJamie Gritton if (!(flags & JAIL_ATTACH)) 1855b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 1856b38ff370SJamie Gritton } 1857c34bbd2aSEdward Tomasz Napierala 1858cc5fd8c7SJamie Gritton goto done_free; 1859b38ff370SJamie Gritton 18600304c731SJamie Gritton done_deref_locked: 18610304c731SJamie Gritton prison_deref(pr, created 18620304c731SJamie Gritton ? PD_LOCKED | PD_LIST_XLOCKED 18630304c731SJamie Gritton : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 18640304c731SJamie Gritton goto done_releroot; 1865b38ff370SJamie Gritton done_unlock_list: 1866b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 1867b38ff370SJamie Gritton done_releroot: 18685050aa86SKonstantin Belousov if (root != NULL) 1869b38ff370SJamie Gritton vrele(root); 1870b38ff370SJamie Gritton done_errmsg: 1871b38ff370SJamie Gritton if (error) { 1872176ff3a0SJamie Gritton if (vfs_getopt(opts, "errmsg", (void **)&errmsg, 1873176ff3a0SJamie Gritton &errmsg_len) == 0 && errmsg_len > 0) { 1874b38ff370SJamie Gritton errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1; 1875b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 1876b38ff370SJamie Gritton bcopy(errmsg, 1877b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 1878b38ff370SJamie Gritton errmsg_len); 1879b38ff370SJamie Gritton else 1880b38ff370SJamie Gritton copyout(errmsg, 1881b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 1882b38ff370SJamie Gritton errmsg_len); 1883b38ff370SJamie Gritton } 1884b38ff370SJamie Gritton } 1885b38ff370SJamie Gritton done_free: 1886b38ff370SJamie Gritton #ifdef INET 1887b38ff370SJamie Gritton free(ip4, M_PRISON); 1888b38ff370SJamie Gritton #endif 1889b38ff370SJamie Gritton #ifdef INET6 1890b38ff370SJamie Gritton free(ip6, M_PRISON); 1891b38ff370SJamie Gritton #endif 18926dfe0a3dSMartin Matuska if (g_path != NULL) 18936dfe0a3dSMartin Matuska free(g_path, M_TEMP); 1894b38ff370SJamie Gritton vfs_freeopts(opts); 1895b38ff370SJamie Gritton return (error); 1896b38ff370SJamie Gritton } 1897b38ff370SJamie Gritton 1898b38ff370SJamie Gritton 1899b38ff370SJamie Gritton /* 1900b38ff370SJamie Gritton * struct jail_get_args { 1901b38ff370SJamie Gritton * struct iovec *iovp; 1902b38ff370SJamie Gritton * unsigned int iovcnt; 1903b38ff370SJamie Gritton * int flags; 1904b38ff370SJamie Gritton * }; 1905b38ff370SJamie Gritton */ 1906b38ff370SJamie Gritton int 19078451d0ddSKip Macy sys_jail_get(struct thread *td, struct jail_get_args *uap) 1908b38ff370SJamie Gritton { 1909b38ff370SJamie Gritton struct uio *auio; 1910b38ff370SJamie Gritton int error; 1911b38ff370SJamie Gritton 1912b38ff370SJamie Gritton /* Check that we have an even number of iovecs. */ 1913b38ff370SJamie Gritton if (uap->iovcnt & 1) 1914b38ff370SJamie Gritton return (EINVAL); 1915b38ff370SJamie Gritton 1916b38ff370SJamie Gritton error = copyinuio(uap->iovp, uap->iovcnt, &auio); 1917b38ff370SJamie Gritton if (error) 1918b38ff370SJamie Gritton return (error); 1919b38ff370SJamie Gritton error = kern_jail_get(td, auio, uap->flags); 1920b38ff370SJamie Gritton if (error == 0) 1921b38ff370SJamie Gritton error = copyout(auio->uio_iov, uap->iovp, 1922b38ff370SJamie Gritton uap->iovcnt * sizeof (struct iovec)); 1923b38ff370SJamie Gritton free(auio, M_IOV); 1924b38ff370SJamie Gritton return (error); 1925b38ff370SJamie Gritton } 1926b38ff370SJamie Gritton 1927b38ff370SJamie Gritton int 1928b38ff370SJamie Gritton kern_jail_get(struct thread *td, struct uio *optuio, int flags) 1929b38ff370SJamie Gritton { 1930672756aaSJamie Gritton struct bool_flags *bf; 1931672756aaSJamie Gritton struct jailsys_flags *jsf; 19320304c731SJamie Gritton struct prison *pr, *mypr; 1933b38ff370SJamie Gritton struct vfsopt *opt; 1934b38ff370SJamie Gritton struct vfsoptlist *opts; 1935b38ff370SJamie Gritton char *errmsg, *name; 1936672756aaSJamie Gritton int error, errmsg_len, errmsg_pos, i, jid, len, locked, pos; 1937672756aaSJamie Gritton unsigned f; 1938b38ff370SJamie Gritton 1939b38ff370SJamie Gritton if (flags & ~JAIL_GET_MASK) 1940b38ff370SJamie Gritton return (EINVAL); 1941b38ff370SJamie Gritton 1942b38ff370SJamie Gritton /* Get the parameter list. */ 1943b38ff370SJamie Gritton error = vfs_buildopts(optuio, &opts); 1944b38ff370SJamie Gritton if (error) 1945b38ff370SJamie Gritton return (error); 1946b38ff370SJamie Gritton errmsg_pos = vfs_getopt_pos(opts, "errmsg"); 19470304c731SJamie Gritton mypr = td->td_ucred->cr_prison; 19481e2a13e6SJamie Gritton 1949b38ff370SJamie Gritton /* 1950b38ff370SJamie Gritton * Find the prison specified by one of: lastjid, jid, name. 1951b38ff370SJamie Gritton */ 1952b38ff370SJamie Gritton sx_slock(&allprison_lock); 1953b38ff370SJamie Gritton error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid)); 1954b38ff370SJamie Gritton if (error == 0) { 1955b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 19560304c731SJamie Gritton if (pr->pr_id > jid && prison_ischild(mypr, pr)) { 1957b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1958b38ff370SJamie Gritton if (pr->pr_ref > 0 && 1959b38ff370SJamie Gritton (pr->pr_uref > 0 || (flags & JAIL_DYING))) 1960b38ff370SJamie Gritton break; 1961b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1962b38ff370SJamie Gritton } 1963b38ff370SJamie Gritton } 1964b38ff370SJamie Gritton if (pr != NULL) 1965b38ff370SJamie Gritton goto found_prison; 1966b38ff370SJamie Gritton error = ENOENT; 1967b38ff370SJamie Gritton vfs_opterror(opts, "no jail after %d", jid); 1968b38ff370SJamie Gritton goto done_unlock_list; 1969b38ff370SJamie Gritton } else if (error != ENOENT) 1970b38ff370SJamie Gritton goto done_unlock_list; 1971b38ff370SJamie Gritton 1972b38ff370SJamie Gritton error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 1973b38ff370SJamie Gritton if (error == 0) { 1974b38ff370SJamie Gritton if (jid != 0) { 19750304c731SJamie Gritton pr = prison_find_child(mypr, jid); 1976b38ff370SJamie Gritton if (pr != NULL) { 1977b38ff370SJamie Gritton if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 1978b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1979b38ff370SJamie Gritton error = ENOENT; 1980b38ff370SJamie Gritton vfs_opterror(opts, "jail %d is dying", 1981b38ff370SJamie Gritton jid); 1982b38ff370SJamie Gritton goto done_unlock_list; 1983b38ff370SJamie Gritton } 1984b38ff370SJamie Gritton goto found_prison; 1985b38ff370SJamie Gritton } 1986b38ff370SJamie Gritton error = ENOENT; 1987b38ff370SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 1988b38ff370SJamie Gritton goto done_unlock_list; 1989b38ff370SJamie Gritton } 1990b38ff370SJamie Gritton } else if (error != ENOENT) 1991b38ff370SJamie Gritton goto done_unlock_list; 1992b38ff370SJamie Gritton 1993b38ff370SJamie Gritton error = vfs_getopt(opts, "name", (void **)&name, &len); 1994b38ff370SJamie Gritton if (error == 0) { 1995b38ff370SJamie Gritton if (len == 0 || name[len - 1] != '\0') { 1996b38ff370SJamie Gritton error = EINVAL; 1997b38ff370SJamie Gritton goto done_unlock_list; 1998b38ff370SJamie Gritton } 19990304c731SJamie Gritton pr = prison_find_name(mypr, name); 2000b38ff370SJamie Gritton if (pr != NULL) { 2001b38ff370SJamie Gritton if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 2002b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2003b38ff370SJamie Gritton error = ENOENT; 2004b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" is dying", 2005b38ff370SJamie Gritton name); 2006b38ff370SJamie Gritton goto done_unlock_list; 2007b38ff370SJamie Gritton } 2008b38ff370SJamie Gritton goto found_prison; 2009b38ff370SJamie Gritton } 2010b38ff370SJamie Gritton error = ENOENT; 2011b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", name); 2012b38ff370SJamie Gritton goto done_unlock_list; 2013b38ff370SJamie Gritton } else if (error != ENOENT) 2014b38ff370SJamie Gritton goto done_unlock_list; 2015b38ff370SJamie Gritton 2016b38ff370SJamie Gritton vfs_opterror(opts, "no jail specified"); 2017b38ff370SJamie Gritton error = ENOENT; 2018b38ff370SJamie Gritton goto done_unlock_list; 2019b38ff370SJamie Gritton 2020b38ff370SJamie Gritton found_prison: 2021b38ff370SJamie Gritton /* Get the parameters of the prison. */ 2022b38ff370SJamie Gritton pr->pr_ref++; 2023b38ff370SJamie Gritton locked = PD_LOCKED; 2024b38ff370SJamie Gritton td->td_retval[0] = pr->pr_id; 2025b38ff370SJamie Gritton error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id)); 2026b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2027b38ff370SJamie Gritton goto done_deref; 20280304c731SJamie Gritton i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id; 20290304c731SJamie Gritton error = vfs_setopt(opts, "parent", &i, sizeof(i)); 2030b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2031b38ff370SJamie Gritton goto done_deref; 20320304c731SJamie Gritton error = vfs_setopts(opts, "name", prison_name(mypr, pr)); 20330304c731SJamie Gritton if (error != 0 && error != ENOENT) 20340304c731SJamie Gritton goto done_deref; 20350304c731SJamie Gritton error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id, 2036b38ff370SJamie Gritton sizeof(pr->pr_cpuset->cs_id)); 2037b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2038b38ff370SJamie Gritton goto done_deref; 20390304c731SJamie Gritton error = vfs_setopts(opts, "path", prison_path(mypr, pr)); 2040b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2041b38ff370SJamie Gritton goto done_deref; 2042b38ff370SJamie Gritton #ifdef INET 2043b38ff370SJamie Gritton error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4, 2044b38ff370SJamie Gritton pr->pr_ip4s * sizeof(*pr->pr_ip4)); 2045b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2046b38ff370SJamie Gritton goto done_deref; 2047b38ff370SJamie Gritton #endif 2048b38ff370SJamie Gritton #ifdef INET6 2049b38ff370SJamie Gritton error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6, 2050b38ff370SJamie Gritton pr->pr_ip6s * sizeof(*pr->pr_ip6)); 2051b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2052b38ff370SJamie Gritton goto done_deref; 2053b38ff370SJamie Gritton #endif 2054b38ff370SJamie Gritton error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel, 2055b38ff370SJamie Gritton sizeof(pr->pr_securelevel)); 2056b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2057b38ff370SJamie Gritton goto done_deref; 2058b97457e2SJamie Gritton error = vfs_setopt(opts, "children.cur", &pr->pr_childcount, 2059b97457e2SJamie Gritton sizeof(pr->pr_childcount)); 2060b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 2061b97457e2SJamie Gritton goto done_deref; 2062b97457e2SJamie Gritton error = vfs_setopt(opts, "children.max", &pr->pr_childmax, 2063b97457e2SJamie Gritton sizeof(pr->pr_childmax)); 2064b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 2065b97457e2SJamie Gritton goto done_deref; 2066c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostname", pr->pr_hostname); 2067b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2068b38ff370SJamie Gritton goto done_deref; 2069c1f19219SJamie Gritton error = vfs_setopts(opts, "host.domainname", pr->pr_domainname); 207076ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 207176ca6f88SJamie Gritton goto done_deref; 2072c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid); 207376ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 207476ca6f88SJamie Gritton goto done_deref; 2075841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32 2076a5c1afadSDmitry Chagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 207776ca6f88SJamie Gritton uint32_t hid32 = pr->pr_hostid; 207876ca6f88SJamie Gritton 207976ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32)); 208076ca6f88SJamie Gritton } else 208176ca6f88SJamie Gritton #endif 208276ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid, 208376ca6f88SJamie Gritton sizeof(pr->pr_hostid)); 208476ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 208576ca6f88SJamie Gritton goto done_deref; 20860304c731SJamie Gritton error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs, 20870304c731SJamie Gritton sizeof(pr->pr_enforce_statfs)); 20880304c731SJamie Gritton if (error != 0 && error != ENOENT) 20890304c731SJamie Gritton goto done_deref; 20900cc207a6SMartin Matuska error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum, 20910cc207a6SMartin Matuska sizeof(pr->pr_devfs_rsnum)); 20920cc207a6SMartin Matuska if (error != 0 && error != ENOENT) 20930cc207a6SMartin Matuska goto done_deref; 2094672756aaSJamie Gritton for (bf = pr_flag_bool; 2095672756aaSJamie Gritton bf < pr_flag_bool + nitems(pr_flag_bool); 2096672756aaSJamie Gritton bf++) { 2097672756aaSJamie Gritton i = (pr->pr_flags & bf->flag) ? 1 : 0; 2098672756aaSJamie Gritton error = vfs_setopt(opts, bf->name, &i, sizeof(i)); 2099b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2100b38ff370SJamie Gritton goto done_deref; 2101b38ff370SJamie Gritton i = !i; 2102672756aaSJamie Gritton error = vfs_setopt(opts, bf->noname, &i, sizeof(i)); 2103b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2104b38ff370SJamie Gritton goto done_deref; 21050304c731SJamie Gritton } 2106672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 2107672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 2108672756aaSJamie Gritton jsf++) { 2109672756aaSJamie Gritton f = pr->pr_flags & (jsf->disable | jsf->new); 2110672756aaSJamie Gritton i = (f != 0 && f == jsf->disable) ? JAIL_SYS_DISABLE 2111672756aaSJamie Gritton : (f == jsf->new) ? JAIL_SYS_NEW 21127cbf7213SJamie Gritton : JAIL_SYS_INHERIT; 2113672756aaSJamie Gritton error = vfs_setopt(opts, jsf->name, &i, sizeof(i)); 21147cbf7213SJamie Gritton if (error != 0 && error != ENOENT) 21157cbf7213SJamie Gritton goto done_deref; 21167cbf7213SJamie Gritton } 2117672756aaSJamie Gritton for (bf = pr_flag_allow; 2118672756aaSJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow); 2119672756aaSJamie Gritton bf++) { 2120672756aaSJamie Gritton i = (pr->pr_allow & bf->flag) ? 1 : 0; 2121672756aaSJamie Gritton error = vfs_setopt(opts, bf->name, &i, sizeof(i)); 21220304c731SJamie Gritton if (error != 0 && error != ENOENT) 21230304c731SJamie Gritton goto done_deref; 21240304c731SJamie Gritton i = !i; 2125672756aaSJamie Gritton error = vfs_setopt(opts, bf->noname, &i, sizeof(i)); 21260304c731SJamie Gritton if (error != 0 && error != ENOENT) 21270304c731SJamie Gritton goto done_deref; 21280304c731SJamie Gritton } 2129b38ff370SJamie Gritton i = (pr->pr_uref == 0); 2130b38ff370SJamie Gritton error = vfs_setopt(opts, "dying", &i, sizeof(i)); 2131b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2132b38ff370SJamie Gritton goto done_deref; 2133b38ff370SJamie Gritton i = !i; 2134b38ff370SJamie Gritton error = vfs_setopt(opts, "nodying", &i, sizeof(i)); 2135b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2136b38ff370SJamie Gritton goto done_deref; 2137bd96bd15SIan Lepore error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate, 2138bd96bd15SIan Lepore sizeof(pr->pr_osreldate)); 2139a1a4c1b0SIan Lepore if (error != 0 && error != ENOENT) 2140a1a4c1b0SIan Lepore goto done_deref; 2141a1a4c1b0SIan Lepore error = vfs_setopts(opts, "osrelease", pr->pr_osrelease); 2142a1a4c1b0SIan Lepore if (error != 0 && error != ENOENT) 2143a1a4c1b0SIan Lepore goto done_deref; 2144b38ff370SJamie Gritton 2145b38ff370SJamie Gritton /* Get the module parameters. */ 2146b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2147b38ff370SJamie Gritton locked = 0; 2148b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_GET, opts); 2149b38ff370SJamie Gritton if (error) 2150b38ff370SJamie Gritton goto done_deref; 2151b38ff370SJamie Gritton prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED); 2152b38ff370SJamie Gritton 2153b38ff370SJamie Gritton /* By now, all parameters should have been noted. */ 2154b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2155b38ff370SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 2156b38ff370SJamie Gritton error = EINVAL; 2157b38ff370SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 2158b38ff370SJamie Gritton goto done_errmsg; 2159b38ff370SJamie Gritton } 2160b38ff370SJamie Gritton } 2161b38ff370SJamie Gritton 2162b38ff370SJamie Gritton /* Write the fetched parameters back to userspace. */ 2163b38ff370SJamie Gritton error = 0; 2164b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2165b38ff370SJamie Gritton if (opt->pos >= 0 && opt->pos != errmsg_pos) { 2166b38ff370SJamie Gritton pos = 2 * opt->pos + 1; 2167b38ff370SJamie Gritton optuio->uio_iov[pos].iov_len = opt->len; 2168b38ff370SJamie Gritton if (opt->value != NULL) { 2169b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) { 2170b38ff370SJamie Gritton bcopy(opt->value, 2171b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2172b38ff370SJamie Gritton opt->len); 2173b38ff370SJamie Gritton } else { 2174b38ff370SJamie Gritton error = copyout(opt->value, 2175b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2176b38ff370SJamie Gritton opt->len); 2177b38ff370SJamie Gritton if (error) 2178b38ff370SJamie Gritton break; 2179b38ff370SJamie Gritton } 2180b38ff370SJamie Gritton } 2181b38ff370SJamie Gritton } 2182b38ff370SJamie Gritton } 2183b38ff370SJamie Gritton goto done_errmsg; 2184b38ff370SJamie Gritton 2185b38ff370SJamie Gritton done_deref: 2186b38ff370SJamie Gritton prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED); 2187b38ff370SJamie Gritton goto done_errmsg; 2188b38ff370SJamie Gritton 2189b38ff370SJamie Gritton done_unlock_list: 2190b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2191b38ff370SJamie Gritton done_errmsg: 2192b38ff370SJamie Gritton if (error && errmsg_pos >= 0) { 2193b38ff370SJamie Gritton vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 2194b38ff370SJamie Gritton errmsg_pos = 2 * errmsg_pos + 1; 2195b38ff370SJamie Gritton if (errmsg_len > 0) { 2196b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 2197b38ff370SJamie Gritton bcopy(errmsg, 2198b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2199b38ff370SJamie Gritton errmsg_len); 2200b38ff370SJamie Gritton else 2201b38ff370SJamie Gritton copyout(errmsg, 2202b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2203b38ff370SJamie Gritton errmsg_len); 2204b38ff370SJamie Gritton } 2205b38ff370SJamie Gritton } 2206b38ff370SJamie Gritton vfs_freeopts(opts); 2207b38ff370SJamie Gritton return (error); 2208b38ff370SJamie Gritton } 2209b38ff370SJamie Gritton 22100304c731SJamie Gritton 2211b38ff370SJamie Gritton /* 2212b38ff370SJamie Gritton * struct jail_remove_args { 2213b38ff370SJamie Gritton * int jid; 2214b38ff370SJamie Gritton * }; 2215b38ff370SJamie Gritton */ 2216b38ff370SJamie Gritton int 22178451d0ddSKip Macy sys_jail_remove(struct thread *td, struct jail_remove_args *uap) 2218b38ff370SJamie Gritton { 22190304c731SJamie Gritton struct prison *pr, *cpr, *lpr, *tpr; 22200304c731SJamie Gritton int descend, error; 2221b38ff370SJamie Gritton 2222b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_REMOVE); 2223b38ff370SJamie Gritton if (error) 2224b38ff370SJamie Gritton return (error); 2225b38ff370SJamie Gritton 2226b38ff370SJamie Gritton sx_xlock(&allprison_lock); 22270304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2228b38ff370SJamie Gritton if (pr == NULL) { 2229b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2230b38ff370SJamie Gritton return (EINVAL); 2231b38ff370SJamie Gritton } 2232b38ff370SJamie Gritton 22330304c731SJamie Gritton /* Remove all descendants of this prison, then remove this prison. */ 22340304c731SJamie Gritton pr->pr_ref++; 22350304c731SJamie Gritton if (!LIST_EMPTY(&pr->pr_children)) { 22360304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 22370304c731SJamie Gritton lpr = NULL; 22380304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 22390304c731SJamie Gritton mtx_lock(&cpr->pr_mtx); 22400304c731SJamie Gritton if (cpr->pr_ref > 0) { 22410304c731SJamie Gritton tpr = cpr; 22420304c731SJamie Gritton cpr->pr_ref++; 22430304c731SJamie Gritton } else { 22440304c731SJamie Gritton /* Already removed - do not do it again. */ 22450304c731SJamie Gritton tpr = NULL; 22460304c731SJamie Gritton } 22470304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 22480304c731SJamie Gritton if (lpr != NULL) { 22490304c731SJamie Gritton mtx_lock(&lpr->pr_mtx); 22500304c731SJamie Gritton prison_remove_one(lpr); 22510304c731SJamie Gritton sx_xlock(&allprison_lock); 22520304c731SJamie Gritton } 22530304c731SJamie Gritton lpr = tpr; 22540304c731SJamie Gritton } 22550304c731SJamie Gritton if (lpr != NULL) { 22560304c731SJamie Gritton mtx_lock(&lpr->pr_mtx); 22570304c731SJamie Gritton prison_remove_one(lpr); 22580304c731SJamie Gritton sx_xlock(&allprison_lock); 22590304c731SJamie Gritton } 22600304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 22610304c731SJamie Gritton } 22620304c731SJamie Gritton prison_remove_one(pr); 22630304c731SJamie Gritton return (0); 22640304c731SJamie Gritton } 22650304c731SJamie Gritton 22660304c731SJamie Gritton static void 22670304c731SJamie Gritton prison_remove_one(struct prison *pr) 22680304c731SJamie Gritton { 22690304c731SJamie Gritton struct proc *p; 22700304c731SJamie Gritton int deuref; 22710304c731SJamie Gritton 2272b38ff370SJamie Gritton /* If the prison was persistent, it is not anymore. */ 2273b38ff370SJamie Gritton deuref = 0; 2274b38ff370SJamie Gritton if (pr->pr_flags & PR_PERSIST) { 2275b38ff370SJamie Gritton pr->pr_ref--; 2276b38ff370SJamie Gritton deuref = PD_DEUREF; 2277b38ff370SJamie Gritton pr->pr_flags &= ~PR_PERSIST; 2278b38ff370SJamie Gritton } 2279b38ff370SJamie Gritton 22800304c731SJamie Gritton /* 22810304c731SJamie Gritton * jail_remove added a reference. If that's the only one, remove 22820304c731SJamie Gritton * the prison now. 22830304c731SJamie Gritton */ 22840304c731SJamie Gritton KASSERT(pr->pr_ref > 0, 22850304c731SJamie Gritton ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id)); 22860304c731SJamie Gritton if (pr->pr_ref == 1) { 2287b38ff370SJamie Gritton prison_deref(pr, 2288b38ff370SJamie Gritton deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 22890304c731SJamie Gritton return; 2290b38ff370SJamie Gritton } 2291b38ff370SJamie Gritton 2292b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2293b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2294b38ff370SJamie Gritton /* 2295b38ff370SJamie Gritton * Kill all processes unfortunate enough to be attached to this prison. 2296b38ff370SJamie Gritton */ 2297b38ff370SJamie Gritton sx_slock(&allproc_lock); 2298b38ff370SJamie Gritton LIST_FOREACH(p, &allproc, p_list) { 2299b38ff370SJamie Gritton PROC_LOCK(p); 2300b38ff370SJamie Gritton if (p->p_state != PRS_NEW && p->p_ucred && 2301b38ff370SJamie Gritton p->p_ucred->cr_prison == pr) 23028451d0ddSKip Macy kern_psignal(p, SIGKILL); 2303b38ff370SJamie Gritton PROC_UNLOCK(p); 2304b38ff370SJamie Gritton } 2305b38ff370SJamie Gritton sx_sunlock(&allproc_lock); 23060304c731SJamie Gritton /* Remove the temporary reference added by jail_remove. */ 2307b38ff370SJamie Gritton prison_deref(pr, deuref | PD_DEREF); 230875c13541SPoul-Henning Kamp } 230975c13541SPoul-Henning Kamp 23108571af59SJamie Gritton 2311fd7a8150SMike Barcroft /* 23129ddb7954SMike Barcroft * struct jail_attach_args { 23139ddb7954SMike Barcroft * int jid; 23149ddb7954SMike Barcroft * }; 2315fd7a8150SMike Barcroft */ 2316fd7a8150SMike Barcroft int 23178451d0ddSKip Macy sys_jail_attach(struct thread *td, struct jail_attach_args *uap) 2318fd7a8150SMike Barcroft { 2319b38ff370SJamie Gritton struct prison *pr; 2320b38ff370SJamie Gritton int error; 2321b38ff370SJamie Gritton 2322b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 2323b38ff370SJamie Gritton if (error) 2324b38ff370SJamie Gritton return (error); 2325b38ff370SJamie Gritton 2326ef0ddea3SJamie Gritton /* 2327ef0ddea3SJamie Gritton * Start with exclusive hold on allprison_lock to ensure that a possible 2328ef0ddea3SJamie Gritton * PR_METHOD_REMOVE call isn't concurrent with jail_set or jail_remove. 2329ef0ddea3SJamie Gritton * But then immediately downgrade it since we don't need to stop 2330ef0ddea3SJamie Gritton * readers. 2331ef0ddea3SJamie Gritton */ 2332ef0ddea3SJamie Gritton sx_xlock(&allprison_lock); 2333ef0ddea3SJamie Gritton sx_downgrade(&allprison_lock); 23340304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2335b38ff370SJamie Gritton if (pr == NULL) { 2336b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2337b38ff370SJamie Gritton return (EINVAL); 2338b38ff370SJamie Gritton } 2339b38ff370SJamie Gritton 2340b38ff370SJamie Gritton /* 2341b38ff370SJamie Gritton * Do not allow a process to attach to a prison that is not 2342b38ff370SJamie Gritton * considered to be "alive". 2343b38ff370SJamie Gritton */ 2344b38ff370SJamie Gritton if (pr->pr_uref == 0) { 2345b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2346b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2347b38ff370SJamie Gritton return (EINVAL); 2348b38ff370SJamie Gritton } 2349b38ff370SJamie Gritton 2350b38ff370SJamie Gritton return (do_jail_attach(td, pr)); 2351b38ff370SJamie Gritton } 2352b38ff370SJamie Gritton 2353b38ff370SJamie Gritton static int 2354b38ff370SJamie Gritton do_jail_attach(struct thread *td, struct prison *pr) 2355b38ff370SJamie Gritton { 2356fd7a8150SMike Barcroft struct proc *p; 2357fd7a8150SMike Barcroft struct ucred *newcred, *oldcred; 23585050aa86SKonstantin Belousov int error; 2359fd7a8150SMike Barcroft 236057f22bd4SJacques Vidrine /* 236157f22bd4SJacques Vidrine * XXX: Note that there is a slight race here if two threads 236257f22bd4SJacques Vidrine * in the same privileged process attempt to attach to two 236357f22bd4SJacques Vidrine * different jails at the same time. It is important for 236457f22bd4SJacques Vidrine * user processes not to do this, or they might end up with 236557f22bd4SJacques Vidrine * a process root from one prison, but attached to the jail 236657f22bd4SJacques Vidrine * of another. 236757f22bd4SJacques Vidrine */ 2368fd7a8150SMike Barcroft pr->pr_ref++; 2369b38ff370SJamie Gritton pr->pr_uref++; 2370fd7a8150SMike Barcroft mtx_unlock(&pr->pr_mtx); 2371b38ff370SJamie Gritton 2372b38ff370SJamie Gritton /* Let modules do whatever they need to prepare for attaching. */ 2373b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_ATTACH, td); 2374b38ff370SJamie Gritton if (error) { 2375b38ff370SJamie Gritton prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED); 2376b38ff370SJamie Gritton return (error); 2377b38ff370SJamie Gritton } 2378dc68a633SPawel Jakub Dawidek sx_sunlock(&allprison_lock); 2379fd7a8150SMike Barcroft 2380413628a7SBjoern A. Zeeb /* 2381413628a7SBjoern A. Zeeb * Reparent the newly attached process to this jail. 2382413628a7SBjoern A. Zeeb */ 2383b38ff370SJamie Gritton p = td->td_proc; 2384413628a7SBjoern A. Zeeb error = cpuset_setproc_update_set(p, pr->pr_cpuset); 2385413628a7SBjoern A. Zeeb if (error) 2386b38ff370SJamie Gritton goto e_revert_osd; 2387413628a7SBjoern A. Zeeb 2388cb05b60aSAttilio Rao vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY); 2389fd7a8150SMike Barcroft if ((error = change_dir(pr->pr_root, td)) != 0) 2390fd7a8150SMike Barcroft goto e_unlock; 2391fd7a8150SMike Barcroft #ifdef MAC 239230d239bcSRobert Watson if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root))) 2393fd7a8150SMike Barcroft goto e_unlock; 2394fd7a8150SMike Barcroft #endif 239522db15c0SAttilio Rao VOP_UNLOCK(pr->pr_root, 0); 2396f0725a8eSMateusz Guzik if ((error = pwd_chroot(td, pr->pr_root))) 23975050aa86SKonstantin Belousov goto e_revert_osd; 2398fd7a8150SMike Barcroft 2399fd7a8150SMike Barcroft newcred = crget(); 2400fd7a8150SMike Barcroft PROC_LOCK(p); 24011fb6767dSJamie Gritton oldcred = crcopysafe(p, newcred); 240269c4ee54SJohn Baldwin newcred->cr_prison = pr; 2403daf63fd2SMateusz Guzik proc_set_cred(p, newcred); 24041fb6767dSJamie Gritton setsugid(p); 2405097055e2SEdward Tomasz Napierala #ifdef RACCT 2406097055e2SEdward Tomasz Napierala racct_proc_ucred_changed(p, oldcred, newcred); 2407*f87beb93SAndriy Gapon crhold(newcred); 2408*f87beb93SAndriy Gapon #endif 2409*f87beb93SAndriy Gapon PROC_UNLOCK(p); 2410*f87beb93SAndriy Gapon #ifdef RCTL 2411*f87beb93SAndriy Gapon rctl_proc_ucred_changed(p, newcred); 2412*f87beb93SAndriy Gapon crfree(newcred); 2413097055e2SEdward Tomasz Napierala #endif 24141fb6767dSJamie Gritton prison_deref(oldcred->cr_prison, PD_DEREF | PD_DEUREF); 2415fd7a8150SMike Barcroft crfree(oldcred); 2416fd7a8150SMike Barcroft return (0); 24171fb6767dSJamie Gritton 2418fd7a8150SMike Barcroft e_unlock: 241922db15c0SAttilio Rao VOP_UNLOCK(pr->pr_root, 0); 2420b38ff370SJamie Gritton e_revert_osd: 2421b38ff370SJamie Gritton /* Tell modules this thread is still in its old jail after all. */ 24221fb6767dSJamie Gritton (void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td); 2423b38ff370SJamie Gritton prison_deref(pr, PD_DEREF | PD_DEUREF); 2424fd7a8150SMike Barcroft return (error); 2425fd7a8150SMike Barcroft } 2426fd7a8150SMike Barcroft 24270304c731SJamie Gritton 2428fd7a8150SMike Barcroft /* 2429fd7a8150SMike Barcroft * Returns a locked prison instance, or NULL on failure. 2430fd7a8150SMike Barcroft */ 243154b369c1SPawel Jakub Dawidek struct prison * 2432fd7a8150SMike Barcroft prison_find(int prid) 2433fd7a8150SMike Barcroft { 2434fd7a8150SMike Barcroft struct prison *pr; 2435fd7a8150SMike Barcroft 2436dc68a633SPawel Jakub Dawidek sx_assert(&allprison_lock, SX_LOCKED); 2437b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 2438fd7a8150SMike Barcroft if (pr->pr_id == prid) { 2439fd7a8150SMike Barcroft mtx_lock(&pr->pr_mtx); 2440b38ff370SJamie Gritton if (pr->pr_ref > 0) 2441fd7a8150SMike Barcroft return (pr); 2442b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2443fd7a8150SMike Barcroft } 2444fd7a8150SMike Barcroft } 2445fd7a8150SMike Barcroft return (NULL); 2446fd7a8150SMike Barcroft } 2447fd7a8150SMike Barcroft 2448b38ff370SJamie Gritton /* 24490304c731SJamie Gritton * Find a prison that is a descendant of mypr. Returns a locked prison or NULL. 2450b38ff370SJamie Gritton */ 2451b38ff370SJamie Gritton struct prison * 24520304c731SJamie Gritton prison_find_child(struct prison *mypr, int prid) 2453b38ff370SJamie Gritton { 24540304c731SJamie Gritton struct prison *pr; 24550304c731SJamie Gritton int descend; 2456b38ff370SJamie Gritton 2457b38ff370SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 24580304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 24590304c731SJamie Gritton if (pr->pr_id == prid) { 24600304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 24610304c731SJamie Gritton if (pr->pr_ref > 0) 24620304c731SJamie Gritton return (pr); 24630304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 24640304c731SJamie Gritton } 24650304c731SJamie Gritton } 24660304c731SJamie Gritton return (NULL); 24670304c731SJamie Gritton } 24680304c731SJamie Gritton 24690304c731SJamie Gritton /* 24700304c731SJamie Gritton * Look for the name relative to mypr. Returns a locked prison or NULL. 24710304c731SJamie Gritton */ 24720304c731SJamie Gritton struct prison * 24730304c731SJamie Gritton prison_find_name(struct prison *mypr, const char *name) 24740304c731SJamie Gritton { 24750304c731SJamie Gritton struct prison *pr, *deadpr; 24760304c731SJamie Gritton size_t mylen; 24770304c731SJamie Gritton int descend; 24780304c731SJamie Gritton 24790304c731SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 24800304c731SJamie Gritton mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1; 2481b38ff370SJamie Gritton again: 2482b38ff370SJamie Gritton deadpr = NULL; 24830304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 24840304c731SJamie Gritton if (!strcmp(pr->pr_name + mylen, name)) { 2485b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 2486b38ff370SJamie Gritton if (pr->pr_ref > 0) { 2487b38ff370SJamie Gritton if (pr->pr_uref > 0) 2488b38ff370SJamie Gritton return (pr); 2489b38ff370SJamie Gritton deadpr = pr; 2490b38ff370SJamie Gritton } 2491b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2492b38ff370SJamie Gritton } 2493b38ff370SJamie Gritton } 24940304c731SJamie Gritton /* There was no valid prison - perhaps there was a dying one. */ 2495b38ff370SJamie Gritton if (deadpr != NULL) { 2496b38ff370SJamie Gritton mtx_lock(&deadpr->pr_mtx); 2497b38ff370SJamie Gritton if (deadpr->pr_ref == 0) { 2498b38ff370SJamie Gritton mtx_unlock(&deadpr->pr_mtx); 2499b38ff370SJamie Gritton goto again; 2500b38ff370SJamie Gritton } 2501b38ff370SJamie Gritton } 2502b38ff370SJamie Gritton return (deadpr); 2503b38ff370SJamie Gritton } 2504b38ff370SJamie Gritton 2505b38ff370SJamie Gritton /* 25060304c731SJamie Gritton * See if a prison has the specific flag set. 25070304c731SJamie Gritton */ 25080304c731SJamie Gritton int 25090304c731SJamie Gritton prison_flag(struct ucred *cred, unsigned flag) 25100304c731SJamie Gritton { 25110304c731SJamie Gritton 25120304c731SJamie Gritton /* This is an atomic read, so no locking is necessary. */ 25130304c731SJamie Gritton return (cred->cr_prison->pr_flags & flag); 25140304c731SJamie Gritton } 25150304c731SJamie Gritton 25160304c731SJamie Gritton int 25170304c731SJamie Gritton prison_allow(struct ucred *cred, unsigned flag) 25180304c731SJamie Gritton { 25190304c731SJamie Gritton 25200304c731SJamie Gritton /* This is an atomic read, so no locking is necessary. */ 25210304c731SJamie Gritton return (cred->cr_prison->pr_allow & flag); 25220304c731SJamie Gritton } 25230304c731SJamie Gritton 25240304c731SJamie Gritton /* 2525b38ff370SJamie Gritton * Remove a prison reference. If that was the last reference, remove the 2526b38ff370SJamie Gritton * prison itself - but not in this context in case there are locks held. 2527b38ff370SJamie Gritton */ 252891421ba2SRobert Watson void 25291ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr) 253091421ba2SRobert Watson { 253173d9e52dSJamie Gritton int ref; 253291421ba2SRobert Watson 25331ba4a712SPawel Jakub Dawidek mtx_assert(&pr->pr_mtx, MA_OWNED); 253473d9e52dSJamie Gritton ref = --pr->pr_ref; 253501137630SRobert Watson mtx_unlock(&pr->pr_mtx); 253673d9e52dSJamie Gritton if (ref == 0) 2537c2cda609SPawel Jakub Dawidek taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 2538c2cda609SPawel Jakub Dawidek } 2539c2cda609SPawel Jakub Dawidek 25401ba4a712SPawel Jakub Dawidek void 25411ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr) 25421ba4a712SPawel Jakub Dawidek { 25431ba4a712SPawel Jakub Dawidek 25441ba4a712SPawel Jakub Dawidek mtx_lock(&pr->pr_mtx); 25451ba4a712SPawel Jakub Dawidek prison_free_locked(pr); 25461ba4a712SPawel Jakub Dawidek } 25471ba4a712SPawel Jakub Dawidek 254873d9e52dSJamie Gritton /* 254973d9e52dSJamie Gritton * Complete a call to either prison_free or prison_proc_free. 255073d9e52dSJamie Gritton */ 2551c2cda609SPawel Jakub Dawidek static void 2552c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending) 2553c2cda609SPawel Jakub Dawidek { 255473d9e52dSJamie Gritton struct prison *pr = context; 2555b38ff370SJamie Gritton 2556ef0ddea3SJamie Gritton sx_xlock(&allprison_lock); 255773d9e52dSJamie Gritton mtx_lock(&pr->pr_mtx); 255873d9e52dSJamie Gritton prison_deref(pr, pr->pr_uref 2559ef0ddea3SJamie Gritton ? PD_DEREF | PD_DEUREF | PD_LOCKED | PD_LIST_XLOCKED 2560ef0ddea3SJamie Gritton : PD_LOCKED | PD_LIST_XLOCKED); 2561b38ff370SJamie Gritton } 2562b38ff370SJamie Gritton 2563b38ff370SJamie Gritton /* 2564b38ff370SJamie Gritton * Remove a prison reference (usually). This internal version assumes no 2565b38ff370SJamie Gritton * mutexes are held, except perhaps the prison itself. If there are no more 2566b38ff370SJamie Gritton * references, release and delist the prison. On completion, the prison lock 2567b38ff370SJamie Gritton * and the allprison lock are both unlocked. 2568b38ff370SJamie Gritton */ 2569b38ff370SJamie Gritton static void 2570b38ff370SJamie Gritton prison_deref(struct prison *pr, int flags) 2571b38ff370SJamie Gritton { 25720304c731SJamie Gritton struct prison *ppr, *tpr; 2573cc5fd8c7SJamie Gritton int ref, lasturef; 2574c2cda609SPawel Jakub Dawidek 2575b38ff370SJamie Gritton if (!(flags & PD_LOCKED)) 2576b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 25770304c731SJamie Gritton for (;;) { 2578e6d5cb63SJamie Gritton if (flags & PD_DEUREF) { 257973d9e52dSJamie Gritton KASSERT(pr->pr_uref > 0, 258073d9e52dSJamie Gritton ("prison_deref PD_DEUREF on a dead prison (jid=%d)", 258173d9e52dSJamie Gritton pr->pr_id)); 2582e6d5cb63SJamie Gritton pr->pr_uref--; 2583cc5fd8c7SJamie Gritton lasturef = pr->pr_uref == 0; 2584cc5fd8c7SJamie Gritton if (lasturef) 2585cc5fd8c7SJamie Gritton pr->pr_ref++; 2586e6d5cb63SJamie Gritton KASSERT(prison0.pr_uref != 0, ("prison0 pr_uref=0")); 2587cc5fd8c7SJamie Gritton } else 2588cc5fd8c7SJamie Gritton lasturef = 0; 258973d9e52dSJamie Gritton if (flags & PD_DEREF) { 259073d9e52dSJamie Gritton KASSERT(pr->pr_ref > 0, 259173d9e52dSJamie Gritton ("prison_deref PD_DEREF on a dead prison (jid=%d)", 259273d9e52dSJamie Gritton pr->pr_id)); 2593b38ff370SJamie Gritton pr->pr_ref--; 259473d9e52dSJamie Gritton } 2595cc5fd8c7SJamie Gritton ref = pr->pr_ref; 2596b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2597cc5fd8c7SJamie Gritton 2598cc5fd8c7SJamie Gritton /* 2599cc5fd8c7SJamie Gritton * Tell the modules if the last user reference was removed 2600cc5fd8c7SJamie Gritton * (even it sticks around in dying state). 2601cc5fd8c7SJamie Gritton */ 2602cc5fd8c7SJamie Gritton if (lasturef) { 2603cc5fd8c7SJamie Gritton if (!(flags & (PD_LIST_SLOCKED | PD_LIST_XLOCKED))) { 2604cc5fd8c7SJamie Gritton sx_xlock(&allprison_lock); 2605cc5fd8c7SJamie Gritton flags |= PD_LIST_XLOCKED; 2606cc5fd8c7SJamie Gritton } 2607cc5fd8c7SJamie Gritton (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL); 2608cc5fd8c7SJamie Gritton mtx_lock(&pr->pr_mtx); 2609cc5fd8c7SJamie Gritton ref = --pr->pr_ref; 2610cc5fd8c7SJamie Gritton mtx_unlock(&pr->pr_mtx); 2611cc5fd8c7SJamie Gritton } 2612cc5fd8c7SJamie Gritton 2613cc5fd8c7SJamie Gritton /* If the prison still has references, nothing else to do. */ 2614cc5fd8c7SJamie Gritton if (ref > 0) { 2615b38ff370SJamie Gritton if (flags & PD_LIST_SLOCKED) 2616b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2617b38ff370SJamie Gritton else if (flags & PD_LIST_XLOCKED) 2618b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2619b38ff370SJamie Gritton return; 2620b38ff370SJamie Gritton } 2621c2cda609SPawel Jakub Dawidek 2622b38ff370SJamie Gritton if (flags & PD_LIST_SLOCKED) { 2623b38ff370SJamie Gritton if (!sx_try_upgrade(&allprison_lock)) { 2624b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2625c2cda609SPawel Jakub Dawidek sx_xlock(&allprison_lock); 2626b38ff370SJamie Gritton } 2627b38ff370SJamie Gritton } else if (!(flags & PD_LIST_XLOCKED)) 2628b38ff370SJamie Gritton sx_xlock(&allprison_lock); 2629b38ff370SJamie Gritton 2630b38ff370SJamie Gritton TAILQ_REMOVE(&allprison, pr, pr_list); 26310304c731SJamie Gritton LIST_REMOVE(pr, pr_sibling); 26320304c731SJamie Gritton ppr = pr->pr_parent; 26330304c731SJamie Gritton for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 2634b97457e2SJamie Gritton tpr->pr_childcount--; 2635c4884ffaSJamie Gritton sx_xunlock(&allprison_lock); 26361ba4a712SPawel Jakub Dawidek 2637679e1390SJamie Gritton #ifdef VIMAGE 26380cb8b6a9SMarko Zec if (pr->pr_vnet != ppr->pr_vnet) 2639679e1390SJamie Gritton vnet_destroy(pr->pr_vnet); 2640679e1390SJamie Gritton #endif 26415050aa86SKonstantin Belousov if (pr->pr_root != NULL) 2642b3059e09SRobert Watson vrele(pr->pr_root); 2643b3059e09SRobert Watson mtx_destroy(&pr->pr_mtx); 2644413628a7SBjoern A. Zeeb #ifdef INET 2645413628a7SBjoern A. Zeeb free(pr->pr_ip4, M_PRISON); 2646413628a7SBjoern A. Zeeb #endif 2647b38ff370SJamie Gritton #ifdef INET6 2648b38ff370SJamie Gritton free(pr->pr_ip6, M_PRISON); 2649b38ff370SJamie Gritton #endif 2650b38ff370SJamie Gritton if (pr->pr_cpuset != NULL) 2651b38ff370SJamie Gritton cpuset_rel(pr->pr_cpuset); 2652b38ff370SJamie Gritton osd_jail_exit(pr); 2653a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 26544b5c9cf6SEdward Tomasz Napierala if (racct_enable) 2655a7ad07bfSEdward Tomasz Napierala prison_racct_detach(pr); 2656ec125fbbSEdward Tomasz Napierala #endif 26571ede983cSDag-Erling Smørgrav free(pr, M_PRISON); 26580304c731SJamie Gritton 26590304c731SJamie Gritton /* Removing a prison frees a reference on its parent. */ 26600304c731SJamie Gritton pr = ppr; 26610304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 2662e6d5cb63SJamie Gritton flags = PD_DEREF | PD_DEUREF; 26630304c731SJamie Gritton } 2664b3059e09SRobert Watson } 2665b3059e09SRobert Watson 266691421ba2SRobert Watson void 26671ba4a712SPawel Jakub Dawidek prison_hold_locked(struct prison *pr) 26681ba4a712SPawel Jakub Dawidek { 26691ba4a712SPawel Jakub Dawidek 26701ba4a712SPawel Jakub Dawidek mtx_assert(&pr->pr_mtx, MA_OWNED); 26711ba4a712SPawel Jakub Dawidek KASSERT(pr->pr_ref > 0, 2672af7bd9a4SJamie Gritton ("Trying to hold dead prison (jid=%d).", pr->pr_id)); 26731ba4a712SPawel Jakub Dawidek pr->pr_ref++; 26741ba4a712SPawel Jakub Dawidek } 26751ba4a712SPawel Jakub Dawidek 26761ba4a712SPawel Jakub Dawidek void 267791421ba2SRobert Watson prison_hold(struct prison *pr) 267891421ba2SRobert Watson { 267991421ba2SRobert Watson 268001137630SRobert Watson mtx_lock(&pr->pr_mtx); 26811ba4a712SPawel Jakub Dawidek prison_hold_locked(pr); 268201137630SRobert Watson mtx_unlock(&pr->pr_mtx); 268301137630SRobert Watson } 268401137630SRobert Watson 2685413628a7SBjoern A. Zeeb void 2686413628a7SBjoern A. Zeeb prison_proc_hold(struct prison *pr) 268701137630SRobert Watson { 268801137630SRobert Watson 2689413628a7SBjoern A. Zeeb mtx_lock(&pr->pr_mtx); 2690b38ff370SJamie Gritton KASSERT(pr->pr_uref > 0, 2691b38ff370SJamie Gritton ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id)); 2692b38ff370SJamie Gritton pr->pr_uref++; 2693413628a7SBjoern A. Zeeb mtx_unlock(&pr->pr_mtx); 269475c13541SPoul-Henning Kamp } 269575c13541SPoul-Henning Kamp 269675c13541SPoul-Henning Kamp void 2697413628a7SBjoern A. Zeeb prison_proc_free(struct prison *pr) 269875c13541SPoul-Henning Kamp { 2699413628a7SBjoern A. Zeeb 2700413628a7SBjoern A. Zeeb mtx_lock(&pr->pr_mtx); 2701b38ff370SJamie Gritton KASSERT(pr->pr_uref > 0, 2702b38ff370SJamie Gritton ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id)); 270373d9e52dSJamie Gritton if (pr->pr_uref > 1) 270473d9e52dSJamie Gritton pr->pr_uref--; 270573d9e52dSJamie Gritton else { 270673d9e52dSJamie Gritton /* 270773d9e52dSJamie Gritton * Don't remove the last user reference in this context, which 270873d9e52dSJamie Gritton * is expected to be a process that is not only locked, but 270973d9e52dSJamie Gritton * also half dead. 271073d9e52dSJamie Gritton */ 271173d9e52dSJamie Gritton pr->pr_ref++; 271273d9e52dSJamie Gritton mtx_unlock(&pr->pr_mtx); 271373d9e52dSJamie Gritton taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 271473d9e52dSJamie Gritton return; 271573d9e52dSJamie Gritton } 271673d9e52dSJamie Gritton mtx_unlock(&pr->pr_mtx); 2717413628a7SBjoern A. Zeeb } 2718413628a7SBjoern A. Zeeb 2719413628a7SBjoern A. Zeeb /* 2720ca04ba64SJamie Gritton * Check if a jail supports the given address family. 2721ca04ba64SJamie Gritton * 2722ca04ba64SJamie Gritton * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT 2723ca04ba64SJamie Gritton * if not. 2724ca04ba64SJamie Gritton */ 2725ca04ba64SJamie Gritton int 2726ca04ba64SJamie Gritton prison_check_af(struct ucred *cred, int af) 2727ca04ba64SJamie Gritton { 27280304c731SJamie Gritton struct prison *pr; 2729ca04ba64SJamie Gritton int error; 2730ca04ba64SJamie Gritton 2731ca04ba64SJamie Gritton KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2732ca04ba64SJamie Gritton 27330304c731SJamie Gritton pr = cred->cr_prison; 2734499650a0SJamie Gritton #ifdef VIMAGE 27356bb79563SJamie Gritton /* Prisons with their own network stack are not limited. */ 2736de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 27376bb79563SJamie Gritton return (0); 2738499650a0SJamie Gritton #endif 27396bb79563SJamie Gritton 2740ca04ba64SJamie Gritton error = 0; 2741ca04ba64SJamie Gritton switch (af) 2742ca04ba64SJamie Gritton { 2743ca04ba64SJamie Gritton #ifdef INET 2744ca04ba64SJamie Gritton case AF_INET: 27450304c731SJamie Gritton if (pr->pr_flags & PR_IP4) 27460304c731SJamie Gritton { 27470304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 27480304c731SJamie Gritton if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL) 2749ca04ba64SJamie Gritton error = EAFNOSUPPORT; 27500304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 27510304c731SJamie Gritton } 2752ca04ba64SJamie Gritton break; 2753ca04ba64SJamie Gritton #endif 2754ca04ba64SJamie Gritton #ifdef INET6 2755ca04ba64SJamie Gritton case AF_INET6: 27560304c731SJamie Gritton if (pr->pr_flags & PR_IP6) 27570304c731SJamie Gritton { 27580304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 27590304c731SJamie Gritton if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL) 2760ca04ba64SJamie Gritton error = EAFNOSUPPORT; 27610304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 27620304c731SJamie Gritton } 2763ca04ba64SJamie Gritton break; 2764ca04ba64SJamie Gritton #endif 2765ca04ba64SJamie Gritton case AF_LOCAL: 2766ca04ba64SJamie Gritton case AF_ROUTE: 2767ca04ba64SJamie Gritton break; 2768ca04ba64SJamie Gritton default: 27690304c731SJamie Gritton if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF)) 2770ca04ba64SJamie Gritton error = EAFNOSUPPORT; 2771ca04ba64SJamie Gritton } 2772ca04ba64SJamie Gritton return (error); 2773ca04ba64SJamie Gritton } 2774ca04ba64SJamie Gritton 2775ca04ba64SJamie Gritton /* 2776413628a7SBjoern A. Zeeb * Check if given address belongs to the jail referenced by cred (wrapper to 2777413628a7SBjoern A. Zeeb * prison_check_ip[46]). 2778413628a7SBjoern A. Zeeb * 27790304c731SJamie Gritton * Returns 0 if jail doesn't restrict the address family or if address belongs 27800304c731SJamie Gritton * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if 27810304c731SJamie Gritton * the jail doesn't allow the address family. IPv4 Address passed in in NBO. 2782413628a7SBjoern A. Zeeb */ 2783413628a7SBjoern A. Zeeb int 278491421ba2SRobert Watson prison_if(struct ucred *cred, struct sockaddr *sa) 278575c13541SPoul-Henning Kamp { 2786413628a7SBjoern A. Zeeb #ifdef INET 27879ddb7954SMike Barcroft struct sockaddr_in *sai; 2788413628a7SBjoern A. Zeeb #endif 2789413628a7SBjoern A. Zeeb #ifdef INET6 2790413628a7SBjoern A. Zeeb struct sockaddr_in6 *sai6; 2791413628a7SBjoern A. Zeeb #endif 2792b89e82ddSJamie Gritton int error; 279375c13541SPoul-Henning Kamp 2794413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2795413628a7SBjoern A. Zeeb KASSERT(sa != NULL, ("%s: sa is NULL", __func__)); 2796413628a7SBjoern A. Zeeb 2797de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE 2798de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 2799de0bd6f7SBjoern A. Zeeb return (0); 2800de0bd6f7SBjoern A. Zeeb #endif 2801de0bd6f7SBjoern A. Zeeb 2802b89e82ddSJamie Gritton error = 0; 2803413628a7SBjoern A. Zeeb switch (sa->sa_family) 2804413628a7SBjoern A. Zeeb { 2805413628a7SBjoern A. Zeeb #ifdef INET 2806413628a7SBjoern A. Zeeb case AF_INET: 28079ddb7954SMike Barcroft sai = (struct sockaddr_in *)sa; 2808b89e82ddSJamie Gritton error = prison_check_ip4(cred, &sai->sin_addr); 2809413628a7SBjoern A. Zeeb break; 2810413628a7SBjoern A. Zeeb #endif 2811413628a7SBjoern A. Zeeb #ifdef INET6 2812413628a7SBjoern A. Zeeb case AF_INET6: 2813413628a7SBjoern A. Zeeb sai6 = (struct sockaddr_in6 *)sa; 2814b89e82ddSJamie Gritton error = prison_check_ip6(cred, &sai6->sin6_addr); 2815413628a7SBjoern A. Zeeb break; 2816413628a7SBjoern A. Zeeb #endif 2817413628a7SBjoern A. Zeeb default: 28180304c731SJamie Gritton if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF)) 2819b89e82ddSJamie Gritton error = EAFNOSUPPORT; 2820413628a7SBjoern A. Zeeb } 2821b89e82ddSJamie Gritton return (error); 282275c13541SPoul-Henning Kamp } 282391421ba2SRobert Watson 282491421ba2SRobert Watson /* 282591421ba2SRobert Watson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH. 282691421ba2SRobert Watson */ 282791421ba2SRobert Watson int 28289ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2) 282991421ba2SRobert Watson { 283091421ba2SRobert Watson 28310304c731SJamie Gritton return ((cred1->cr_prison == cred2->cr_prison || 28320304c731SJamie Gritton prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH); 28330304c731SJamie Gritton } 283491421ba2SRobert Watson 28350304c731SJamie Gritton /* 28360304c731SJamie Gritton * Return 1 if p2 is a child of p1, otherwise 0. 28370304c731SJamie Gritton */ 28380304c731SJamie Gritton int 28390304c731SJamie Gritton prison_ischild(struct prison *pr1, struct prison *pr2) 28400304c731SJamie Gritton { 28410304c731SJamie Gritton 28420304c731SJamie Gritton for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent) 28430304c731SJamie Gritton if (pr1 == pr2) 28440304c731SJamie Gritton return (1); 284591421ba2SRobert Watson return (0); 284691421ba2SRobert Watson } 284791421ba2SRobert Watson 284891421ba2SRobert Watson /* 284991421ba2SRobert Watson * Return 1 if the passed credential is in a jail, otherwise 0. 285091421ba2SRobert Watson */ 285191421ba2SRobert Watson int 28529ddb7954SMike Barcroft jailed(struct ucred *cred) 285391421ba2SRobert Watson { 285491421ba2SRobert Watson 28550304c731SJamie Gritton return (cred->cr_prison != &prison0); 285691421ba2SRobert Watson } 28579484d0c0SRobert Drehmel 28589484d0c0SRobert Drehmel /* 2859de0bd6f7SBjoern A. Zeeb * Return 1 if the passed credential is in a jail and that jail does not 2860de0bd6f7SBjoern A. Zeeb * have its own virtual network stack, otherwise 0. 2861de0bd6f7SBjoern A. Zeeb */ 2862de0bd6f7SBjoern A. Zeeb int 2863de0bd6f7SBjoern A. Zeeb jailed_without_vnet(struct ucred *cred) 2864de0bd6f7SBjoern A. Zeeb { 2865de0bd6f7SBjoern A. Zeeb 2866de0bd6f7SBjoern A. Zeeb if (!jailed(cred)) 2867de0bd6f7SBjoern A. Zeeb return (0); 2868de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE 2869de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 2870de0bd6f7SBjoern A. Zeeb return (0); 2871de0bd6f7SBjoern A. Zeeb #endif 2872de0bd6f7SBjoern A. Zeeb 2873de0bd6f7SBjoern A. Zeeb return (1); 2874de0bd6f7SBjoern A. Zeeb } 2875de0bd6f7SBjoern A. Zeeb 2876de0bd6f7SBjoern A. Zeeb /* 28777455b100SJamie Gritton * Return the correct hostname (domainname, et al) for the passed credential. 28789484d0c0SRobert Drehmel */ 2879ad1ff099SRobert Drehmel void 28809ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size) 28819484d0c0SRobert Drehmel { 288276ca6f88SJamie Gritton struct prison *pr; 28839484d0c0SRobert Drehmel 28847455b100SJamie Gritton /* 28857455b100SJamie Gritton * A NULL credential can be used to shortcut to the physical 28867455b100SJamie Gritton * system's hostname. 28877455b100SJamie Gritton */ 288876ca6f88SJamie Gritton pr = (cred != NULL) ? cred->cr_prison : &prison0; 288976ca6f88SJamie Gritton mtx_lock(&pr->pr_mtx); 2890c1f19219SJamie Gritton strlcpy(buf, pr->pr_hostname, size); 289176ca6f88SJamie Gritton mtx_unlock(&pr->pr_mtx); 28929484d0c0SRobert Drehmel } 2893fd7a8150SMike Barcroft 28947455b100SJamie Gritton void 28957455b100SJamie Gritton getcreddomainname(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_domainname, size); 29007455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 29017455b100SJamie Gritton } 29027455b100SJamie Gritton 29037455b100SJamie Gritton void 29047455b100SJamie Gritton getcredhostuuid(struct ucred *cred, char *buf, size_t size) 29057455b100SJamie Gritton { 29067455b100SJamie Gritton 29077455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 2908c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_hostuuid, size); 29097455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 29107455b100SJamie Gritton } 29117455b100SJamie Gritton 29127455b100SJamie Gritton void 29137455b100SJamie Gritton getcredhostid(struct ucred *cred, unsigned long *hostid) 29147455b100SJamie Gritton { 29157455b100SJamie Gritton 29167455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 29177455b100SJamie Gritton *hostid = cred->cr_prison->pr_hostid; 29187455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 29197455b100SJamie Gritton } 29207455b100SJamie Gritton 2921eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE 2922eb79e1c7SBjoern A. Zeeb /* 2923eb79e1c7SBjoern A. Zeeb * Determine whether the prison represented by cred owns 2924eb79e1c7SBjoern A. Zeeb * its vnet rather than having it inherited. 2925eb79e1c7SBjoern A. Zeeb * 2926eb79e1c7SBjoern A. Zeeb * Returns 1 in case the prison owns the vnet, 0 otherwise. 2927eb79e1c7SBjoern A. Zeeb */ 2928eb79e1c7SBjoern A. Zeeb int 2929eb79e1c7SBjoern A. Zeeb prison_owns_vnet(struct ucred *cred) 2930eb79e1c7SBjoern A. Zeeb { 2931eb79e1c7SBjoern A. Zeeb 2932eb79e1c7SBjoern A. Zeeb /* 2933eb79e1c7SBjoern A. Zeeb * vnets cannot be added/removed after jail creation, 2934eb79e1c7SBjoern A. Zeeb * so no need to lock here. 2935eb79e1c7SBjoern A. Zeeb */ 2936eb79e1c7SBjoern A. Zeeb return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0); 2937eb79e1c7SBjoern A. Zeeb } 2938eb79e1c7SBjoern A. Zeeb #endif 2939eb79e1c7SBjoern A. Zeeb 2940f08df373SRobert Watson /* 2941820a0de9SPawel Jakub Dawidek * Determine whether the subject represented by cred can "see" 2942820a0de9SPawel Jakub Dawidek * status of a mount point. 2943820a0de9SPawel Jakub Dawidek * Returns: 0 for permitted, ENOENT otherwise. 2944820a0de9SPawel Jakub Dawidek * XXX: This function should be called cr_canseemount() and should be 2945820a0de9SPawel Jakub Dawidek * placed in kern_prot.c. 2946f08df373SRobert Watson */ 2947f08df373SRobert Watson int 2948820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp) 2949f08df373SRobert Watson { 2950820a0de9SPawel Jakub Dawidek struct prison *pr; 2951820a0de9SPawel Jakub Dawidek struct statfs *sp; 2952820a0de9SPawel Jakub Dawidek size_t len; 2953f08df373SRobert Watson 2954820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 29550304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 29560304c731SJamie Gritton return (0); 2957820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) 2958820a0de9SPawel Jakub Dawidek return (0); 29590304c731SJamie Gritton if (pr->pr_enforce_statfs == 2) 2960820a0de9SPawel Jakub Dawidek return (ENOENT); 2961820a0de9SPawel Jakub Dawidek /* 2962820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 2963820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 2964820a0de9SPawel Jakub Dawidek * This is ugly check, but this is the only situation when jail's 2965820a0de9SPawel Jakub Dawidek * directory ends with '/'. 2966820a0de9SPawel Jakub Dawidek */ 2967820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 2968820a0de9SPawel Jakub Dawidek return (0); 2969820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 2970820a0de9SPawel Jakub Dawidek sp = &mp->mnt_stat; 2971820a0de9SPawel Jakub Dawidek if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0) 2972820a0de9SPawel Jakub Dawidek return (ENOENT); 2973820a0de9SPawel Jakub Dawidek /* 2974820a0de9SPawel Jakub Dawidek * Be sure that we don't have situation where jail's root directory 2975820a0de9SPawel Jakub Dawidek * is "/some/path" and mount point is "/some/pathpath". 2976820a0de9SPawel Jakub Dawidek */ 2977820a0de9SPawel Jakub Dawidek if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/') 2978820a0de9SPawel Jakub Dawidek return (ENOENT); 2979f08df373SRobert Watson return (0); 2980f08df373SRobert Watson } 2981820a0de9SPawel Jakub Dawidek 2982820a0de9SPawel Jakub Dawidek void 2983820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp) 2984820a0de9SPawel Jakub Dawidek { 2985820a0de9SPawel Jakub Dawidek char jpath[MAXPATHLEN]; 2986820a0de9SPawel Jakub Dawidek struct prison *pr; 2987820a0de9SPawel Jakub Dawidek size_t len; 2988820a0de9SPawel Jakub Dawidek 2989820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 29900304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 29910304c731SJamie Gritton return; 2992820a0de9SPawel Jakub Dawidek if (prison_canseemount(cred, mp) != 0) { 2993820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 2994820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, "[restricted]", 2995820a0de9SPawel Jakub Dawidek sizeof(sp->f_mntonname)); 2996820a0de9SPawel Jakub Dawidek return; 2997820a0de9SPawel Jakub Dawidek } 2998820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) { 2999820a0de9SPawel Jakub Dawidek /* 3000820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3001820a0de9SPawel Jakub Dawidek * the valid path left there. 3002820a0de9SPawel Jakub Dawidek */ 3003820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3004820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3005820a0de9SPawel Jakub Dawidek return; 3006820a0de9SPawel Jakub Dawidek } 3007820a0de9SPawel Jakub Dawidek /* 3008820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 3009820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 3010820a0de9SPawel Jakub Dawidek */ 3011820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 3012820a0de9SPawel Jakub Dawidek return; 3013820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 3014820a0de9SPawel Jakub Dawidek strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath)); 3015820a0de9SPawel Jakub Dawidek /* 3016820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3017820a0de9SPawel Jakub Dawidek * the valid path left there. 3018820a0de9SPawel Jakub Dawidek */ 3019820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3020820a0de9SPawel Jakub Dawidek if (*jpath == '\0') { 3021820a0de9SPawel Jakub Dawidek /* Should never happen. */ 3022820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3023820a0de9SPawel Jakub Dawidek } else { 3024820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname)); 3025820a0de9SPawel Jakub Dawidek } 3026f08df373SRobert Watson } 3027f08df373SRobert Watson 3028800c9408SRobert Watson /* 3029800c9408SRobert Watson * Check with permission for a specific privilege is granted within jail. We 3030800c9408SRobert Watson * have a specific list of accepted privileges; the rest are denied. 3031800c9408SRobert Watson */ 3032800c9408SRobert Watson int 3033800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv) 3034800c9408SRobert Watson { 3035800c9408SRobert Watson 3036800c9408SRobert Watson if (!jailed(cred)) 3037800c9408SRobert Watson return (0); 3038800c9408SRobert Watson 30396bb79563SJamie Gritton #ifdef VIMAGE 30406bb79563SJamie Gritton /* 30416bb79563SJamie Gritton * Privileges specific to prisons with a virtual network stack. 30426bb79563SJamie Gritton * There might be a duplicate entry here in case the privilege 30436bb79563SJamie Gritton * is only granted conditionally in the legacy jail case. 30446bb79563SJamie Gritton */ 30456bb79563SJamie Gritton switch (priv) { 30466bb79563SJamie Gritton #ifdef notyet 30476bb79563SJamie Gritton /* 30486bb79563SJamie Gritton * NFS-specific privileges. 30496bb79563SJamie Gritton */ 30506bb79563SJamie Gritton case PRIV_NFS_DAEMON: 30516bb79563SJamie Gritton case PRIV_NFS_LOCKD: 30526bb79563SJamie Gritton #endif 30536bb79563SJamie Gritton /* 30546bb79563SJamie Gritton * Network stack privileges. 30556bb79563SJamie Gritton */ 30566bb79563SJamie Gritton case PRIV_NET_BRIDGE: 30576bb79563SJamie Gritton case PRIV_NET_GRE: 30586bb79563SJamie Gritton case PRIV_NET_BPF: 30596bb79563SJamie Gritton case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */ 30606bb79563SJamie Gritton case PRIV_NET_ROUTE: 30616bb79563SJamie Gritton case PRIV_NET_TAP: 30626bb79563SJamie Gritton case PRIV_NET_SETIFMTU: 30636bb79563SJamie Gritton case PRIV_NET_SETIFFLAGS: 30646bb79563SJamie Gritton case PRIV_NET_SETIFCAP: 3065215940b3SXin LI case PRIV_NET_SETIFDESCR: 30666bb79563SJamie Gritton case PRIV_NET_SETIFNAME : 30676bb79563SJamie Gritton case PRIV_NET_SETIFMETRIC: 30686bb79563SJamie Gritton case PRIV_NET_SETIFPHYS: 30696bb79563SJamie Gritton case PRIV_NET_SETIFMAC: 30706bb79563SJamie Gritton case PRIV_NET_ADDMULTI: 30716bb79563SJamie Gritton case PRIV_NET_DELMULTI: 30726bb79563SJamie Gritton case PRIV_NET_HWIOCTL: 30736bb79563SJamie Gritton case PRIV_NET_SETLLADDR: 30746bb79563SJamie Gritton case PRIV_NET_ADDIFGROUP: 30756bb79563SJamie Gritton case PRIV_NET_DELIFGROUP: 30766bb79563SJamie Gritton case PRIV_NET_IFCREATE: 30776bb79563SJamie Gritton case PRIV_NET_IFDESTROY: 30786bb79563SJamie Gritton case PRIV_NET_ADDIFADDR: 30796bb79563SJamie Gritton case PRIV_NET_DELIFADDR: 30806bb79563SJamie Gritton case PRIV_NET_LAGG: 30816bb79563SJamie Gritton case PRIV_NET_GIF: 30826bb79563SJamie Gritton case PRIV_NET_SETIFVNET: 308335fd7bc0SBjoern A. Zeeb case PRIV_NET_SETIFFIB: 30846bb79563SJamie Gritton 30856bb79563SJamie Gritton /* 30866bb79563SJamie Gritton * 802.11-related privileges. 30876bb79563SJamie Gritton */ 30886bb79563SJamie Gritton case PRIV_NET80211_GETKEY: 30896bb79563SJamie Gritton #ifdef notyet 30906bb79563SJamie Gritton case PRIV_NET80211_MANAGE: /* XXX-BZ discuss with sam@ */ 30916bb79563SJamie Gritton #endif 30926bb79563SJamie Gritton 30936bb79563SJamie Gritton #ifdef notyet 30946bb79563SJamie Gritton /* 30956bb79563SJamie Gritton * ATM privileges. 30966bb79563SJamie Gritton */ 30976bb79563SJamie Gritton case PRIV_NETATM_CFG: 30986bb79563SJamie Gritton case PRIV_NETATM_ADD: 30996bb79563SJamie Gritton case PRIV_NETATM_DEL: 31006bb79563SJamie Gritton case PRIV_NETATM_SET: 31016bb79563SJamie Gritton 31026bb79563SJamie Gritton /* 31036bb79563SJamie Gritton * Bluetooth privileges. 31046bb79563SJamie Gritton */ 31056bb79563SJamie Gritton case PRIV_NETBLUETOOTH_RAW: 31066bb79563SJamie Gritton #endif 31076bb79563SJamie Gritton 31086bb79563SJamie Gritton /* 31096bb79563SJamie Gritton * Netgraph and netgraph module privileges. 31106bb79563SJamie Gritton */ 31116bb79563SJamie Gritton case PRIV_NETGRAPH_CONTROL: 31126bb79563SJamie Gritton #ifdef notyet 31136bb79563SJamie Gritton case PRIV_NETGRAPH_TTY: 31146bb79563SJamie Gritton #endif 31156bb79563SJamie Gritton 31166bb79563SJamie Gritton /* 31176bb79563SJamie Gritton * IPv4 and IPv6 privileges. 31186bb79563SJamie Gritton */ 31196bb79563SJamie Gritton case PRIV_NETINET_IPFW: 31206bb79563SJamie Gritton case PRIV_NETINET_DIVERT: 31216bb79563SJamie Gritton case PRIV_NETINET_PF: 31226bb79563SJamie Gritton case PRIV_NETINET_DUMMYNET: 31236bb79563SJamie Gritton case PRIV_NETINET_CARP: 31246bb79563SJamie Gritton case PRIV_NETINET_MROUTE: 31256bb79563SJamie Gritton case PRIV_NETINET_RAW: 31266bb79563SJamie Gritton case PRIV_NETINET_ADDRCTRL6: 31276bb79563SJamie Gritton case PRIV_NETINET_ND6: 31286bb79563SJamie Gritton case PRIV_NETINET_SCOPE6: 31296bb79563SJamie Gritton case PRIV_NETINET_ALIFETIME6: 31306bb79563SJamie Gritton case PRIV_NETINET_IPSEC: 31316bb79563SJamie Gritton case PRIV_NETINET_BINDANY: 31326bb79563SJamie Gritton 31336bb79563SJamie Gritton #ifdef notyet 31346bb79563SJamie Gritton /* 31356bb79563SJamie Gritton * NCP privileges. 31366bb79563SJamie Gritton */ 31376bb79563SJamie Gritton case PRIV_NETNCP: 31386bb79563SJamie Gritton 31396bb79563SJamie Gritton /* 31406bb79563SJamie Gritton * SMB privileges. 31416bb79563SJamie Gritton */ 31426bb79563SJamie Gritton case PRIV_NETSMB: 31436bb79563SJamie Gritton #endif 31446bb79563SJamie Gritton 31456bb79563SJamie Gritton /* 31466bb79563SJamie Gritton * No default: or deny here. 31476bb79563SJamie Gritton * In case of no permit fall through to next switch(). 31486bb79563SJamie Gritton */ 31496bb79563SJamie Gritton if (cred->cr_prison->pr_flags & PR_VNET) 31506bb79563SJamie Gritton return (0); 31516bb79563SJamie Gritton } 31526bb79563SJamie Gritton #endif /* VIMAGE */ 31536bb79563SJamie Gritton 3154800c9408SRobert Watson switch (priv) { 3155800c9408SRobert Watson 3156800c9408SRobert Watson /* 3157800c9408SRobert Watson * Allow ktrace privileges for root in jail. 3158800c9408SRobert Watson */ 3159800c9408SRobert Watson case PRIV_KTRACE: 3160800c9408SRobert Watson 3161c3c1b5e6SRobert Watson #if 0 3162800c9408SRobert Watson /* 3163800c9408SRobert Watson * Allow jailed processes to configure audit identity and 3164800c9408SRobert Watson * submit audit records (login, etc). In the future we may 3165800c9408SRobert Watson * want to further refine the relationship between audit and 3166800c9408SRobert Watson * jail. 3167800c9408SRobert Watson */ 3168800c9408SRobert Watson case PRIV_AUDIT_GETAUDIT: 3169800c9408SRobert Watson case PRIV_AUDIT_SETAUDIT: 3170800c9408SRobert Watson case PRIV_AUDIT_SUBMIT: 3171c3c1b5e6SRobert Watson #endif 3172800c9408SRobert Watson 3173800c9408SRobert Watson /* 3174800c9408SRobert Watson * Allow jailed processes to manipulate process UNIX 3175800c9408SRobert Watson * credentials in any way they see fit. 3176800c9408SRobert Watson */ 3177800c9408SRobert Watson case PRIV_CRED_SETUID: 3178800c9408SRobert Watson case PRIV_CRED_SETEUID: 3179800c9408SRobert Watson case PRIV_CRED_SETGID: 3180800c9408SRobert Watson case PRIV_CRED_SETEGID: 3181800c9408SRobert Watson case PRIV_CRED_SETGROUPS: 3182800c9408SRobert Watson case PRIV_CRED_SETREUID: 3183800c9408SRobert Watson case PRIV_CRED_SETREGID: 3184800c9408SRobert Watson case PRIV_CRED_SETRESUID: 3185800c9408SRobert Watson case PRIV_CRED_SETRESGID: 3186800c9408SRobert Watson 3187800c9408SRobert Watson /* 3188800c9408SRobert Watson * Jail implements visibility constraints already, so allow 3189800c9408SRobert Watson * jailed root to override uid/gid-based constraints. 3190800c9408SRobert Watson */ 3191800c9408SRobert Watson case PRIV_SEEOTHERGIDS: 3192800c9408SRobert Watson case PRIV_SEEOTHERUIDS: 3193800c9408SRobert Watson 3194800c9408SRobert Watson /* 3195800c9408SRobert Watson * Jail implements inter-process debugging limits already, so 3196800c9408SRobert Watson * allow jailed root various debugging privileges. 3197800c9408SRobert Watson */ 3198800c9408SRobert Watson case PRIV_DEBUG_DIFFCRED: 3199800c9408SRobert Watson case PRIV_DEBUG_SUGID: 3200800c9408SRobert Watson case PRIV_DEBUG_UNPRIV: 3201800c9408SRobert Watson 3202800c9408SRobert Watson /* 3203800c9408SRobert Watson * Allow jail to set various resource limits and login 3204800c9408SRobert Watson * properties, and for now, exceed process resource limits. 3205800c9408SRobert Watson */ 3206800c9408SRobert Watson case PRIV_PROC_LIMIT: 3207800c9408SRobert Watson case PRIV_PROC_SETLOGIN: 3208800c9408SRobert Watson case PRIV_PROC_SETRLIMIT: 3209800c9408SRobert Watson 3210800c9408SRobert Watson /* 3211800c9408SRobert Watson * System V and POSIX IPC privileges are granted in jail. 3212800c9408SRobert Watson */ 3213800c9408SRobert Watson case PRIV_IPC_READ: 3214800c9408SRobert Watson case PRIV_IPC_WRITE: 3215800c9408SRobert Watson case PRIV_IPC_ADMIN: 3216800c9408SRobert Watson case PRIV_IPC_MSGSIZE: 3217800c9408SRobert Watson case PRIV_MQ_ADMIN: 3218800c9408SRobert Watson 3219800c9408SRobert Watson /* 32200304c731SJamie Gritton * Jail operations within a jail work on child jails. 32210304c731SJamie Gritton */ 32220304c731SJamie Gritton case PRIV_JAIL_ATTACH: 32230304c731SJamie Gritton case PRIV_JAIL_SET: 32240304c731SJamie Gritton case PRIV_JAIL_REMOVE: 32250304c731SJamie Gritton 32260304c731SJamie Gritton /* 3227800c9408SRobert Watson * Jail implements its own inter-process limits, so allow 3228800c9408SRobert Watson * root processes in jail to change scheduling on other 3229800c9408SRobert Watson * processes in the same jail. Likewise for signalling. 3230800c9408SRobert Watson */ 3231800c9408SRobert Watson case PRIV_SCHED_DIFFCRED: 3232413628a7SBjoern A. Zeeb case PRIV_SCHED_CPUSET: 3233800c9408SRobert Watson case PRIV_SIGNAL_DIFFCRED: 3234800c9408SRobert Watson case PRIV_SIGNAL_SUGID: 3235800c9408SRobert Watson 3236800c9408SRobert Watson /* 3237800c9408SRobert Watson * Allow jailed processes to write to sysctls marked as jail 3238800c9408SRobert Watson * writable. 3239800c9408SRobert Watson */ 3240800c9408SRobert Watson case PRIV_SYSCTL_WRITEJAIL: 3241800c9408SRobert Watson 3242800c9408SRobert Watson /* 3243800c9408SRobert Watson * Allow root in jail to manage a variety of quota 3244e82d0201SRobert Watson * properties. These should likely be conditional on a 3245e82d0201SRobert Watson * configuration option. 3246800c9408SRobert Watson */ 324795b091d2SRobert Watson case PRIV_VFS_GETQUOTA: 324895b091d2SRobert Watson case PRIV_VFS_SETQUOTA: 3249800c9408SRobert Watson 3250800c9408SRobert Watson /* 3251800c9408SRobert Watson * Since Jail relies on chroot() to implement file system 3252800c9408SRobert Watson * protections, grant many VFS privileges to root in jail. 3253800c9408SRobert Watson * Be careful to exclude mount-related and NFS-related 3254800c9408SRobert Watson * privileges. 3255800c9408SRobert Watson */ 3256800c9408SRobert Watson case PRIV_VFS_READ: 3257800c9408SRobert Watson case PRIV_VFS_WRITE: 3258800c9408SRobert Watson case PRIV_VFS_ADMIN: 3259800c9408SRobert Watson case PRIV_VFS_EXEC: 3260800c9408SRobert Watson case PRIV_VFS_LOOKUP: 3261800c9408SRobert Watson case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */ 3262800c9408SRobert Watson case PRIV_VFS_CHFLAGS_DEV: 3263800c9408SRobert Watson case PRIV_VFS_CHOWN: 3264800c9408SRobert Watson case PRIV_VFS_CHROOT: 3265bb531912SPawel Jakub Dawidek case PRIV_VFS_RETAINSUGID: 3266800c9408SRobert Watson case PRIV_VFS_FCHROOT: 3267800c9408SRobert Watson case PRIV_VFS_LINK: 3268800c9408SRobert Watson case PRIV_VFS_SETGID: 3269e41966dcSRobert Watson case PRIV_VFS_STAT: 3270800c9408SRobert Watson case PRIV_VFS_STICKYFILE: 3271bb56d716SJamie Gritton 3272bb56d716SJamie Gritton /* 3273bb56d716SJamie Gritton * As in the non-jail case, non-root users are expected to be 3274bb56d716SJamie Gritton * able to read kernel/phyiscal memory (provided /dev/[k]mem 3275bb56d716SJamie Gritton * exists in the jail and they have permission to access it). 3276bb56d716SJamie Gritton */ 3277bb56d716SJamie Gritton case PRIV_KMEM_READ: 3278800c9408SRobert Watson return (0); 3279800c9408SRobert Watson 3280800c9408SRobert Watson /* 3281800c9408SRobert Watson * Depending on the global setting, allow privilege of 3282800c9408SRobert Watson * setting system flags. 3283800c9408SRobert Watson */ 3284800c9408SRobert Watson case PRIV_VFS_SYSFLAGS: 32850304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS) 3286800c9408SRobert Watson return (0); 3287800c9408SRobert Watson else 3288800c9408SRobert Watson return (EPERM); 3289800c9408SRobert Watson 3290800c9408SRobert Watson /* 3291f3a8d2f9SPawel Jakub Dawidek * Depending on the global setting, allow privilege of 3292f3a8d2f9SPawel Jakub Dawidek * mounting/unmounting file systems. 3293f3a8d2f9SPawel Jakub Dawidek */ 3294f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT: 3295f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_UNMOUNT: 3296f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT_NONUSER: 329724b0502eSPawel Jakub Dawidek case PRIV_VFS_MOUNT_OWNER: 3298435d4667SMartin Matuska if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT && 3299435d4667SMartin Matuska cred->cr_prison->pr_enforce_statfs < 2) 3300f3a8d2f9SPawel Jakub Dawidek return (0); 3301f3a8d2f9SPawel Jakub Dawidek else 3302f3a8d2f9SPawel Jakub Dawidek return (EPERM); 3303f3a8d2f9SPawel Jakub Dawidek 3304f3a8d2f9SPawel Jakub Dawidek /* 3305e28f9b7dSAllan Jude * Conditionally allow jailed root to bind reserved ports. 3306800c9408SRobert Watson */ 3307800c9408SRobert Watson case PRIV_NETINET_RESERVEDPORT: 3308e28f9b7dSAllan Jude if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS) 3309e28f9b7dSAllan Jude return (0); 3310e28f9b7dSAllan Jude else 3311e28f9b7dSAllan Jude return (EPERM); 3312e28f9b7dSAllan Jude 3313e28f9b7dSAllan Jude /* 3314e28f9b7dSAllan Jude * Allow jailed root to reuse in-use ports. 3315e28f9b7dSAllan Jude */ 33164b084056SRobert Watson case PRIV_NETINET_REUSEPORT: 3317800c9408SRobert Watson return (0); 3318800c9408SRobert Watson 3319800c9408SRobert Watson /* 3320e3043798SPedro F. Giffuni * Allow jailed root to set certain IPv4/6 (option) headers. 332179ba3952SBjoern A. Zeeb */ 332279ba3952SBjoern A. Zeeb case PRIV_NETINET_SETHDROPTS: 332379ba3952SBjoern A. Zeeb return (0); 332479ba3952SBjoern A. Zeeb 332579ba3952SBjoern A. Zeeb /* 3326800c9408SRobert Watson * Conditionally allow creating raw sockets in jail. 3327800c9408SRobert Watson */ 3328800c9408SRobert Watson case PRIV_NETINET_RAW: 33290304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS) 3330800c9408SRobert Watson return (0); 3331800c9408SRobert Watson else 3332800c9408SRobert Watson return (EPERM); 3333800c9408SRobert Watson 3334800c9408SRobert Watson /* 3335800c9408SRobert Watson * Since jail implements its own visibility limits on netstat 3336800c9408SRobert Watson * sysctls, allow getcred. This allows identd to work in 3337800c9408SRobert Watson * jail. 3338800c9408SRobert Watson */ 3339800c9408SRobert Watson case PRIV_NETINET_GETCRED: 3340800c9408SRobert Watson return (0); 3341800c9408SRobert Watson 33422bfc50bcSEdward Tomasz Napierala /* 33432bfc50bcSEdward Tomasz Napierala * Allow jailed root to set loginclass. 33442bfc50bcSEdward Tomasz Napierala */ 33452bfc50bcSEdward Tomasz Napierala case PRIV_PROC_SETLOGINCLASS: 33462bfc50bcSEdward Tomasz Napierala return (0); 33472bfc50bcSEdward Tomasz Napierala 3348800c9408SRobert Watson default: 3349800c9408SRobert Watson /* 3350800c9408SRobert Watson * In all remaining cases, deny the privilege request. This 3351800c9408SRobert Watson * includes almost all network privileges, many system 3352800c9408SRobert Watson * configuration privileges. 3353800c9408SRobert Watson */ 3354800c9408SRobert Watson return (EPERM); 3355800c9408SRobert Watson } 3356800c9408SRobert Watson } 3357800c9408SRobert Watson 33580304c731SJamie Gritton /* 33590304c731SJamie Gritton * Return the part of pr2's name that is relative to pr1, or the whole name 33600304c731SJamie Gritton * if it does not directly follow. 33610304c731SJamie Gritton */ 33620304c731SJamie Gritton 33630304c731SJamie Gritton char * 33640304c731SJamie Gritton prison_name(struct prison *pr1, struct prison *pr2) 33650304c731SJamie Gritton { 33660304c731SJamie Gritton char *name; 33670304c731SJamie Gritton 33680304c731SJamie Gritton /* Jails see themselves as "0" (if they see themselves at all). */ 33690304c731SJamie Gritton if (pr1 == pr2) 33700304c731SJamie Gritton return "0"; 33710304c731SJamie Gritton name = pr2->pr_name; 33720304c731SJamie Gritton if (prison_ischild(pr1, pr2)) { 33730304c731SJamie Gritton /* 33740304c731SJamie Gritton * pr1 isn't locked (and allprison_lock may not be either) 33750304c731SJamie Gritton * so its length can't be counted on. But the number of dots 33760304c731SJamie Gritton * can be counted on - and counted. 33770304c731SJamie Gritton */ 33780304c731SJamie Gritton for (; pr1 != &prison0; pr1 = pr1->pr_parent) 33790304c731SJamie Gritton name = strchr(name, '.') + 1; 33800304c731SJamie Gritton } 33810304c731SJamie Gritton return (name); 33820304c731SJamie Gritton } 33830304c731SJamie Gritton 33840304c731SJamie Gritton /* 33850304c731SJamie Gritton * Return the part of pr2's path that is relative to pr1, or the whole path 33860304c731SJamie Gritton * if it does not directly follow. 33870304c731SJamie Gritton */ 33880304c731SJamie Gritton static char * 33890304c731SJamie Gritton prison_path(struct prison *pr1, struct prison *pr2) 33900304c731SJamie Gritton { 33910304c731SJamie Gritton char *path1, *path2; 33920304c731SJamie Gritton int len1; 33930304c731SJamie Gritton 33940304c731SJamie Gritton path1 = pr1->pr_path; 33950304c731SJamie Gritton path2 = pr2->pr_path; 33960304c731SJamie Gritton if (!strcmp(path1, "/")) 33970304c731SJamie Gritton return (path2); 33980304c731SJamie Gritton len1 = strlen(path1); 33990304c731SJamie Gritton if (strncmp(path1, path2, len1)) 34000304c731SJamie Gritton return (path2); 34010304c731SJamie Gritton if (path2[len1] == '\0') 34020304c731SJamie Gritton return "/"; 34030304c731SJamie Gritton if (path2[len1] == '/') 34040304c731SJamie Gritton return (path2 + len1); 34050304c731SJamie Gritton return (path2); 34060304c731SJamie Gritton } 34070304c731SJamie Gritton 34080304c731SJamie Gritton 34090304c731SJamie Gritton /* 34100304c731SJamie Gritton * Jail-related sysctls. 34110304c731SJamie Gritton */ 34126472ac3dSEd Schouten static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0, 34130304c731SJamie Gritton "Jails"); 34140304c731SJamie Gritton 3415fd7a8150SMike Barcroft static int 3416fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS) 3417fd7a8150SMike Barcroft { 3418b38ff370SJamie Gritton struct xprison *xp; 34190304c731SJamie Gritton struct prison *pr, *cpr; 3420b38ff370SJamie Gritton #ifdef INET 3421b38ff370SJamie Gritton struct in_addr *ip4 = NULL; 3422b38ff370SJamie Gritton int ip4s = 0; 3423b38ff370SJamie Gritton #endif 3424b38ff370SJamie Gritton #ifdef INET6 34253beefaedSColin Percival struct in6_addr *ip6 = NULL; 3426b38ff370SJamie Gritton int ip6s = 0; 3427b38ff370SJamie Gritton #endif 34280304c731SJamie Gritton int descend, error; 3429fd7a8150SMike Barcroft 3430b38ff370SJamie Gritton xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK); 34310304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 3432b38ff370SJamie Gritton error = 0; 3433dc68a633SPawel Jakub Dawidek sx_slock(&allprison_lock); 34340304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 34350304c731SJamie Gritton #if defined(INET) || defined(INET6) 3436b38ff370SJamie Gritton again: 34370304c731SJamie Gritton #endif 34380304c731SJamie Gritton mtx_lock(&cpr->pr_mtx); 3439413628a7SBjoern A. Zeeb #ifdef INET 34400304c731SJamie Gritton if (cpr->pr_ip4s > 0) { 34410304c731SJamie Gritton if (ip4s < cpr->pr_ip4s) { 34420304c731SJamie Gritton ip4s = cpr->pr_ip4s; 34430304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 3444b38ff370SJamie Gritton ip4 = realloc(ip4, ip4s * 3445b38ff370SJamie Gritton sizeof(struct in_addr), M_TEMP, M_WAITOK); 3446b38ff370SJamie Gritton goto again; 3447b38ff370SJamie Gritton } 34480304c731SJamie Gritton bcopy(cpr->pr_ip4, ip4, 34490304c731SJamie Gritton cpr->pr_ip4s * sizeof(struct in_addr)); 3450b38ff370SJamie Gritton } 3451413628a7SBjoern A. Zeeb #endif 3452413628a7SBjoern A. Zeeb #ifdef INET6 34530304c731SJamie Gritton if (cpr->pr_ip6s > 0) { 34540304c731SJamie Gritton if (ip6s < cpr->pr_ip6s) { 34550304c731SJamie Gritton ip6s = cpr->pr_ip6s; 34560304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 3457b38ff370SJamie Gritton ip6 = realloc(ip6, ip6s * 3458b38ff370SJamie Gritton sizeof(struct in6_addr), M_TEMP, M_WAITOK); 3459b38ff370SJamie Gritton goto again; 3460413628a7SBjoern A. Zeeb } 34610304c731SJamie Gritton bcopy(cpr->pr_ip6, ip6, 34620304c731SJamie Gritton cpr->pr_ip6s * sizeof(struct in6_addr)); 3463b38ff370SJamie Gritton } 3464b38ff370SJamie Gritton #endif 34650304c731SJamie Gritton if (cpr->pr_ref == 0) { 34660304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 3467b38ff370SJamie Gritton continue; 3468b38ff370SJamie Gritton } 3469b38ff370SJamie Gritton bzero(xp, sizeof(*xp)); 3470fd7a8150SMike Barcroft xp->pr_version = XPRISON_VERSION; 34710304c731SJamie Gritton xp->pr_id = cpr->pr_id; 34720304c731SJamie Gritton xp->pr_state = cpr->pr_uref > 0 3473b38ff370SJamie Gritton ? PRISON_STATE_ALIVE : PRISON_STATE_DYING; 34740304c731SJamie Gritton strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path)); 3475c1f19219SJamie Gritton strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host)); 34760304c731SJamie Gritton strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name)); 3477413628a7SBjoern A. Zeeb #ifdef INET 34780304c731SJamie Gritton xp->pr_ip4s = cpr->pr_ip4s; 3479413628a7SBjoern A. Zeeb #endif 3480413628a7SBjoern A. Zeeb #ifdef INET6 34810304c731SJamie Gritton xp->pr_ip6s = cpr->pr_ip6s; 3482413628a7SBjoern A. Zeeb #endif 34830304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 3484b38ff370SJamie Gritton error = SYSCTL_OUT(req, xp, sizeof(*xp)); 3485b38ff370SJamie Gritton if (error) 3486b38ff370SJamie Gritton break; 3487413628a7SBjoern A. Zeeb #ifdef INET 3488b38ff370SJamie Gritton if (xp->pr_ip4s > 0) { 3489b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip4, 3490b38ff370SJamie Gritton xp->pr_ip4s * sizeof(struct in_addr)); 3491b38ff370SJamie Gritton if (error) 3492b38ff370SJamie Gritton break; 3493413628a7SBjoern A. Zeeb } 3494413628a7SBjoern A. Zeeb #endif 3495413628a7SBjoern A. Zeeb #ifdef INET6 3496b38ff370SJamie Gritton if (xp->pr_ip6s > 0) { 3497b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip6, 3498b38ff370SJamie Gritton xp->pr_ip6s * sizeof(struct in6_addr)); 3499b38ff370SJamie Gritton if (error) 3500b38ff370SJamie Gritton break; 3501413628a7SBjoern A. Zeeb } 3502413628a7SBjoern A. Zeeb #endif 3503fd7a8150SMike Barcroft } 3504dc68a633SPawel Jakub Dawidek sx_sunlock(&allprison_lock); 3505b38ff370SJamie Gritton free(xp, M_TEMP); 3506b38ff370SJamie Gritton #ifdef INET 3507b38ff370SJamie Gritton free(ip4, M_TEMP); 3508b38ff370SJamie Gritton #endif 3509b38ff370SJamie Gritton #ifdef INET6 3510b38ff370SJamie Gritton free(ip6, M_TEMP); 3511b38ff370SJamie Gritton #endif 3512fd7a8150SMike Barcroft return (error); 3513fd7a8150SMike Barcroft } 3514fd7a8150SMike Barcroft 3515f3b86a5fSEd Schouten SYSCTL_OID(_security_jail, OID_AUTO, list, 3516f3b86a5fSEd Schouten CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 3517f3b86a5fSEd Schouten sysctl_jail_list, "S", "List of active jails"); 3518461167c2SPawel Jakub Dawidek 3519461167c2SPawel Jakub Dawidek static int 3520461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS) 3521461167c2SPawel Jakub Dawidek { 3522461167c2SPawel Jakub Dawidek int error, injail; 3523461167c2SPawel Jakub Dawidek 3524461167c2SPawel Jakub Dawidek injail = jailed(req->td->td_ucred); 3525461167c2SPawel Jakub Dawidek error = SYSCTL_OUT(req, &injail, sizeof(injail)); 3526461167c2SPawel Jakub Dawidek 3527461167c2SPawel Jakub Dawidek return (error); 3528461167c2SPawel Jakub Dawidek } 35290304c731SJamie Gritton 3530f3b86a5fSEd Schouten SYSCTL_PROC(_security_jail, OID_AUTO, jailed, 3531f3b86a5fSEd Schouten CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 3532f3b86a5fSEd Schouten sysctl_jail_jailed, "I", "Process in jail?"); 3533413628a7SBjoern A. Zeeb 3534761d2bb5SJamie Gritton static int 3535761d2bb5SJamie Gritton sysctl_jail_vnet(SYSCTL_HANDLER_ARGS) 3536761d2bb5SJamie Gritton { 3537761d2bb5SJamie Gritton int error, havevnet; 3538761d2bb5SJamie Gritton #ifdef VIMAGE 3539761d2bb5SJamie Gritton struct ucred *cred = req->td->td_ucred; 3540761d2bb5SJamie Gritton 3541761d2bb5SJamie Gritton havevnet = jailed(cred) && prison_owns_vnet(cred); 3542761d2bb5SJamie Gritton #else 3543761d2bb5SJamie Gritton havevnet = 0; 3544761d2bb5SJamie Gritton #endif 3545761d2bb5SJamie Gritton error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet)); 3546761d2bb5SJamie Gritton 3547761d2bb5SJamie Gritton return (error); 3548761d2bb5SJamie Gritton } 3549761d2bb5SJamie Gritton 3550761d2bb5SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, vnet, 3551761d2bb5SJamie Gritton CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 3552761d2bb5SJamie Gritton sysctl_jail_vnet, "I", "Jail owns VNET?"); 3553761d2bb5SJamie Gritton 35540304c731SJamie Gritton #if defined(INET) || defined(INET6) 3555e92e0574SJamie Gritton SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW, 35560304c731SJamie Gritton &jail_max_af_ips, 0, 3557ee8d6bd3SJamie Gritton "Number of IP addresses a jail may have at most per address family (deprecated)"); 35580304c731SJamie Gritton #endif 35590304c731SJamie Gritton 35600304c731SJamie Gritton /* 3561e3043798SPedro F. Giffuni * Default parameters for jail(2) compatibility. For historical reasons, 35620304c731SJamie Gritton * the sysctl names have varying similarity to the parameter names. Prisons 35630304c731SJamie Gritton * just see their own parameters, and can't change them. 35640304c731SJamie Gritton */ 35650304c731SJamie Gritton static int 35660304c731SJamie Gritton sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS) 35670304c731SJamie Gritton { 35680304c731SJamie Gritton struct prison *pr; 35690304c731SJamie Gritton int allow, error, i; 35700304c731SJamie Gritton 35710304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 35720304c731SJamie Gritton allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow; 35730304c731SJamie Gritton 35740304c731SJamie Gritton /* Get the current flag value, and convert it to a boolean. */ 35750304c731SJamie Gritton i = (allow & arg2) ? 1 : 0; 35760304c731SJamie Gritton if (arg1 != NULL) 35770304c731SJamie Gritton i = !i; 35780304c731SJamie Gritton error = sysctl_handle_int(oidp, &i, 0, req); 35790304c731SJamie Gritton if (error || !req->newptr) 35800304c731SJamie Gritton return (error); 35810304c731SJamie Gritton i = i ? arg2 : 0; 35820304c731SJamie Gritton if (arg1 != NULL) 35830304c731SJamie Gritton i ^= arg2; 35840304c731SJamie Gritton /* 35850304c731SJamie Gritton * The sysctls don't have CTLFLAGS_PRISON, so assume prison0 35860304c731SJamie Gritton * for writing. 35870304c731SJamie Gritton */ 35880304c731SJamie Gritton mtx_lock(&prison0.pr_mtx); 35890304c731SJamie Gritton jail_default_allow = (jail_default_allow & ~arg2) | i; 35900304c731SJamie Gritton mtx_unlock(&prison0.pr_mtx); 35910304c731SJamie Gritton return (0); 35920304c731SJamie Gritton } 35930304c731SJamie Gritton 35940304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed, 35950304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 35960304c731SJamie Gritton NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I", 3597ee8d6bd3SJamie Gritton "Processes in jail can set their hostnames (deprecated)"); 35980304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only, 35990304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 36000304c731SJamie Gritton (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I", 3601ee8d6bd3SJamie Gritton "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)"); 36020304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed, 36030304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 36040304c731SJamie Gritton NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I", 3605ee8d6bd3SJamie Gritton "Processes in jail can use System V IPC primitives (deprecated)"); 36060304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets, 36070304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 36080304c731SJamie Gritton NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I", 3609ee8d6bd3SJamie Gritton "Prison root can create raw sockets (deprecated)"); 36100304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed, 36110304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 36120304c731SJamie Gritton NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I", 3613ee8d6bd3SJamie Gritton "Processes in jail can alter system file flags (deprecated)"); 36140304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed, 36150304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 36160304c731SJamie Gritton NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I", 3617ee8d6bd3SJamie Gritton "Processes in jail can mount/unmount jail-friendly file systems (deprecated)"); 3618bf3db8aaSMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, mount_devfs_allowed, 3619bf3db8aaSMartin Matuska CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 3620bf3db8aaSMartin Matuska NULL, PR_ALLOW_MOUNT_DEVFS, sysctl_jail_default_allow, "I", 3621ee8d6bd3SJamie Gritton "Processes in jail can mount the devfs file system (deprecated)"); 3622464aad14SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_fdescfs_allowed, 3623464aad14SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 3624464aad14SJamie Gritton NULL, PR_ALLOW_MOUNT_FDESCFS, sysctl_jail_default_allow, "I", 3625ee8d6bd3SJamie Gritton "Processes in jail can mount the fdescfs file system (deprecated)"); 3626bf3db8aaSMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, mount_nullfs_allowed, 3627bf3db8aaSMartin Matuska CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 3628bf3db8aaSMartin Matuska NULL, PR_ALLOW_MOUNT_NULLFS, sysctl_jail_default_allow, "I", 3629ee8d6bd3SJamie Gritton "Processes in jail can mount the nullfs file system (deprecated)"); 363041c0675eSMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, mount_procfs_allowed, 363141c0675eSMartin Matuska CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 363241c0675eSMartin Matuska NULL, PR_ALLOW_MOUNT_PROCFS, sysctl_jail_default_allow, "I", 3633ee8d6bd3SJamie Gritton "Processes in jail can mount the procfs file system (deprecated)"); 3634f19e47d6SMarcelo Araujo SYSCTL_PROC(_security_jail, OID_AUTO, mount_linprocfs_allowed, 3635f19e47d6SMarcelo Araujo CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 3636f19e47d6SMarcelo Araujo NULL, PR_ALLOW_MOUNT_LINPROCFS, sysctl_jail_default_allow, "I", 3637ee8d6bd3SJamie Gritton "Processes in jail can mount the linprocfs file system (deprecated)"); 3638f19e47d6SMarcelo Araujo SYSCTL_PROC(_security_jail, OID_AUTO, mount_linsysfs_allowed, 3639f19e47d6SMarcelo Araujo CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 3640f19e47d6SMarcelo Araujo NULL, PR_ALLOW_MOUNT_LINSYSFS, sysctl_jail_default_allow, "I", 3641ee8d6bd3SJamie Gritton "Processes in jail can mount the linsysfs file system (deprecated)"); 36422454886eSXin LI SYSCTL_PROC(_security_jail, OID_AUTO, mount_tmpfs_allowed, 36432454886eSXin LI CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 36442454886eSXin LI NULL, PR_ALLOW_MOUNT_TMPFS, sysctl_jail_default_allow, "I", 3645ee8d6bd3SJamie Gritton "Processes in jail can mount the tmpfs file system (deprecated)"); 3646e7af90abSMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, mount_zfs_allowed, 3647e7af90abSMartin Matuska CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 3648e7af90abSMartin Matuska NULL, PR_ALLOW_MOUNT_ZFS, sysctl_jail_default_allow, "I", 3649ee8d6bd3SJamie Gritton "Processes in jail can mount the zfs file system (deprecated)"); 36500304c731SJamie Gritton 36510304c731SJamie Gritton static int 36520304c731SJamie Gritton sysctl_jail_default_level(SYSCTL_HANDLER_ARGS) 36530304c731SJamie Gritton { 36540304c731SJamie Gritton struct prison *pr; 36550304c731SJamie Gritton int level, error; 36560304c731SJamie Gritton 36570304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 36580304c731SJamie Gritton level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2); 36590304c731SJamie Gritton error = sysctl_handle_int(oidp, &level, 0, req); 36600304c731SJamie Gritton if (error || !req->newptr) 36610304c731SJamie Gritton return (error); 36620304c731SJamie Gritton *(int *)arg1 = level; 36630304c731SJamie Gritton return (0); 36640304c731SJamie Gritton } 36650304c731SJamie Gritton 36660304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs, 36670304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 36680304c731SJamie Gritton &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs), 36690304c731SJamie Gritton sysctl_jail_default_level, "I", 3670ee8d6bd3SJamie Gritton "Processes in jail cannot see all mounted file systems (deprecated)"); 36710304c731SJamie Gritton 36720cc207a6SMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset, 36730cc207a6SMartin Matuska CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 36740cc207a6SMartin Matuska &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum), 36750cc207a6SMartin Matuska sysctl_jail_default_level, "I", 3676ee8d6bd3SJamie Gritton "Ruleset for the devfs filesystem in jail (deprecated)"); 36770cc207a6SMartin Matuska 36780304c731SJamie Gritton /* 36790304c731SJamie Gritton * Nodes to describe jail parameters. Maximum length of string parameters 36800304c731SJamie Gritton * is returned in the string itself, and the other parameters exist merely 36810304c731SJamie Gritton * to make themselves and their types known. 36820304c731SJamie Gritton */ 36830304c731SJamie Gritton SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0, 36840304c731SJamie Gritton "Jail parameters"); 36850304c731SJamie Gritton 36860304c731SJamie Gritton int 36870304c731SJamie Gritton sysctl_jail_param(SYSCTL_HANDLER_ARGS) 36880304c731SJamie Gritton { 36890304c731SJamie Gritton int i; 36900304c731SJamie Gritton long l; 36910304c731SJamie Gritton size_t s; 36920304c731SJamie Gritton char numbuf[12]; 36930304c731SJamie Gritton 36940304c731SJamie Gritton switch (oidp->oid_kind & CTLTYPE) 36950304c731SJamie Gritton { 36960304c731SJamie Gritton case CTLTYPE_LONG: 36970304c731SJamie Gritton case CTLTYPE_ULONG: 36980304c731SJamie Gritton l = 0; 36990304c731SJamie Gritton #ifdef SCTL_MASK32 37000304c731SJamie Gritton if (!(req->flags & SCTL_MASK32)) 37010304c731SJamie Gritton #endif 37020304c731SJamie Gritton return (SYSCTL_OUT(req, &l, sizeof(l))); 37030304c731SJamie Gritton case CTLTYPE_INT: 37040304c731SJamie Gritton case CTLTYPE_UINT: 37050304c731SJamie Gritton i = 0; 37060304c731SJamie Gritton return (SYSCTL_OUT(req, &i, sizeof(i))); 37070304c731SJamie Gritton case CTLTYPE_STRING: 3708e4cd31ddSJeff Roberson snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2); 37090304c731SJamie Gritton return 37100304c731SJamie Gritton (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req)); 37110304c731SJamie Gritton case CTLTYPE_STRUCT: 37120304c731SJamie Gritton s = (size_t)arg2; 37130304c731SJamie Gritton return (SYSCTL_OUT(req, &s, sizeof(s))); 37140304c731SJamie Gritton } 37150304c731SJamie Gritton return (0); 37160304c731SJamie Gritton } 37170304c731SJamie Gritton 3718b96bd95bSIan Lepore /* 3719b96bd95bSIan Lepore * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at 3720b96bd95bSIan Lepore * jail creation time but cannot be changed in an existing jail. 3721b96bd95bSIan Lepore */ 37220304c731SJamie Gritton SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID"); 37230304c731SJamie Gritton SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID"); 37240304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name"); 37250304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path"); 37260304c731SJamie Gritton SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW, 37270304c731SJamie Gritton "I", "Jail secure level"); 3728b96bd95bSIan Lepore SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I", 3729b96bd95bSIan Lepore "Jail value for kern.osreldate and uname -K"); 3730b96bd95bSIan Lepore SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN, 3731b96bd95bSIan Lepore "Jail value for kern.osrelease and uname -r"); 37320304c731SJamie Gritton SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW, 37330304c731SJamie Gritton "I", "Jail cannot see all mounted file systems"); 37340cc207a6SMartin Matuska SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW, 37350cc207a6SMartin Matuska "I", "Ruleset for in-jail devfs mounts"); 37360304c731SJamie Gritton SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW, 37370304c731SJamie Gritton "B", "Jail persistence"); 3738679e1390SJamie Gritton #ifdef VIMAGE 3739679e1390SJamie Gritton SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN, 37407cbf7213SJamie Gritton "E,jailsys", "Virtual network stack"); 3741679e1390SJamie Gritton #endif 37420304c731SJamie Gritton SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD, 37430304c731SJamie Gritton "B", "Jail is in the process of shutting down"); 37440304c731SJamie Gritton 3745b97457e2SJamie Gritton SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails"); 3746b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD, 3747b97457e2SJamie Gritton "I", "Current number of child jails"); 3748b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW, 3749b97457e2SJamie Gritton "I", "Maximum number of child jails"); 3750b97457e2SJamie Gritton 37517cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info"); 37520304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN, 37530304c731SJamie Gritton "Jail hostname"); 375476ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN, 375576ca6f88SJamie Gritton "Jail NIS domainname"); 375676ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN, 375776ca6f88SJamie Gritton "Jail host UUID"); 375876ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW, 375976ca6f88SJamie Gritton "LU", "Jail host ID"); 37600304c731SJamie Gritton 37610304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset"); 37620304c731SJamie Gritton SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID"); 37630304c731SJamie Gritton 37640304c731SJamie Gritton #ifdef INET 37652b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN, 37662b0d6f81SJamie Gritton "Jail IPv4 address virtualization"); 37670304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr), 37680304c731SJamie Gritton "S,in_addr,a", "Jail IPv4 addresses"); 3769592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 3770592bcae8SBjoern A. Zeeb "B", "Do (not) use IPv4 source address selection rather than the " 3771592bcae8SBjoern A. Zeeb "primary jail IPv4 address."); 37720304c731SJamie Gritton #endif 37730304c731SJamie Gritton #ifdef INET6 37742b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN, 37752b0d6f81SJamie Gritton "Jail IPv6 address virtualization"); 37760304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr), 37770304c731SJamie Gritton "S,in6_addr,a", "Jail IPv6 addresses"); 3778592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 3779592bcae8SBjoern A. Zeeb "B", "Do (not) use IPv6 source address selection rather than the " 3780592bcae8SBjoern A. Zeeb "primary jail IPv6 address."); 37810304c731SJamie Gritton #endif 37820304c731SJamie Gritton 37830304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags"); 37840304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW, 37850304c731SJamie Gritton "B", "Jail may set hostname"); 37860304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW, 37870304c731SJamie Gritton "B", "Jail may use SYSV IPC"); 37880304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW, 37890304c731SJamie Gritton "B", "Jail may create raw sockets"); 37900304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW, 37910304c731SJamie Gritton "B", "Jail may alter system file flags"); 37920304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW, 37930304c731SJamie Gritton "B", "Jail may set file quotas"); 37940304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW, 37950304c731SJamie Gritton "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route"); 3796e28f9b7dSAllan Jude SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW, 3797e28f9b7dSAllan Jude "B", "Jail may bind sockets to reserved ports"); 37980304c731SJamie Gritton 3799bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags"); 3800bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW, 3801bf3db8aaSMartin Matuska "B", "Jail may mount/unmount jail-friendly file systems in general"); 3802bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, devfs, CTLTYPE_INT | CTLFLAG_RW, 3803e7af90abSMartin Matuska "B", "Jail may mount the devfs file system"); 3804464aad14SJamie Gritton SYSCTL_JAIL_PARAM(_allow_mount, fdescfs, CTLTYPE_INT | CTLFLAG_RW, 3805464aad14SJamie Gritton "B", "Jail may mount the fdescfs file system"); 3806bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, nullfs, CTLTYPE_INT | CTLFLAG_RW, 3807e7af90abSMartin Matuska "B", "Jail may mount the nullfs file system"); 380841c0675eSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, procfs, CTLTYPE_INT | CTLFLAG_RW, 380941c0675eSMartin Matuska "B", "Jail may mount the procfs file system"); 3810f19e47d6SMarcelo Araujo SYSCTL_JAIL_PARAM(_allow_mount, linprocfs, CTLTYPE_INT | CTLFLAG_RW, 3811f19e47d6SMarcelo Araujo "B", "Jail may mount the linprocfs file system"); 3812f19e47d6SMarcelo Araujo SYSCTL_JAIL_PARAM(_allow_mount, linsysfs, CTLTYPE_INT | CTLFLAG_RW, 3813f19e47d6SMarcelo Araujo "B", "Jail may mount the linsysfs file system"); 38142454886eSXin LI SYSCTL_JAIL_PARAM(_allow_mount, tmpfs, CTLTYPE_INT | CTLFLAG_RW, 38152454886eSXin LI "B", "Jail may mount the tmpfs file system"); 3816e7af90abSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, zfs, CTLTYPE_INT | CTLFLAG_RW, 3817e7af90abSMartin Matuska "B", "Jail may mount the zfs file system"); 3818bf3db8aaSMartin Matuska 38194b5c9cf6SEdward Tomasz Napierala #ifdef RACCT 3820097055e2SEdward Tomasz Napierala void 3821097055e2SEdward Tomasz Napierala prison_racct_foreach(void (*callback)(struct racct *racct, 382215db3c07SEdward Tomasz Napierala void *arg2, void *arg3), void (*pre)(void), void (*post)(void), 382315db3c07SEdward Tomasz Napierala void *arg2, void *arg3) 3824097055e2SEdward Tomasz Napierala { 3825a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 3826097055e2SEdward Tomasz Napierala 38274b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 38284b5c9cf6SEdward Tomasz Napierala 3829097055e2SEdward Tomasz Napierala sx_slock(&allprison_lock); 383015db3c07SEdward Tomasz Napierala if (pre != NULL) 383115db3c07SEdward Tomasz Napierala (pre)(); 3832a7ad07bfSEdward Tomasz Napierala LIST_FOREACH(prr, &allprison_racct, prr_next) 3833a7ad07bfSEdward Tomasz Napierala (callback)(prr->prr_racct, arg2, arg3); 383415db3c07SEdward Tomasz Napierala if (post != NULL) 383515db3c07SEdward Tomasz Napierala (post)(); 3836097055e2SEdward Tomasz Napierala sx_sunlock(&allprison_lock); 3837097055e2SEdward Tomasz Napierala } 38380304c731SJamie Gritton 3839a7ad07bfSEdward Tomasz Napierala static struct prison_racct * 3840a7ad07bfSEdward Tomasz Napierala prison_racct_find_locked(const char *name) 3841a7ad07bfSEdward Tomasz Napierala { 3842a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 3843a7ad07bfSEdward Tomasz Napierala 38444b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 3845a7ad07bfSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 3846a7ad07bfSEdward Tomasz Napierala 3847a7ad07bfSEdward Tomasz Napierala if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN) 3848a7ad07bfSEdward Tomasz Napierala return (NULL); 3849a7ad07bfSEdward Tomasz Napierala 3850a7ad07bfSEdward Tomasz Napierala LIST_FOREACH(prr, &allprison_racct, prr_next) { 3851a7ad07bfSEdward Tomasz Napierala if (strcmp(name, prr->prr_name) != 0) 3852a7ad07bfSEdward Tomasz Napierala continue; 3853a7ad07bfSEdward Tomasz Napierala 3854a7ad07bfSEdward Tomasz Napierala /* Found prison_racct with a matching name? */ 3855a7ad07bfSEdward Tomasz Napierala prison_racct_hold(prr); 3856a7ad07bfSEdward Tomasz Napierala return (prr); 3857a7ad07bfSEdward Tomasz Napierala } 3858a7ad07bfSEdward Tomasz Napierala 3859a7ad07bfSEdward Tomasz Napierala /* Add new prison_racct. */ 3860a7ad07bfSEdward Tomasz Napierala prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK); 3861a7ad07bfSEdward Tomasz Napierala racct_create(&prr->prr_racct); 3862a7ad07bfSEdward Tomasz Napierala 3863a7ad07bfSEdward Tomasz Napierala strcpy(prr->prr_name, name); 3864a7ad07bfSEdward Tomasz Napierala refcount_init(&prr->prr_refcount, 1); 3865a7ad07bfSEdward Tomasz Napierala LIST_INSERT_HEAD(&allprison_racct, prr, prr_next); 3866a7ad07bfSEdward Tomasz Napierala 3867a7ad07bfSEdward Tomasz Napierala return (prr); 3868a7ad07bfSEdward Tomasz Napierala } 3869a7ad07bfSEdward Tomasz Napierala 3870a7ad07bfSEdward Tomasz Napierala struct prison_racct * 3871a7ad07bfSEdward Tomasz Napierala prison_racct_find(const char *name) 3872a7ad07bfSEdward Tomasz Napierala { 3873a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 3874a7ad07bfSEdward Tomasz Napierala 38754b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 38764b5c9cf6SEdward Tomasz Napierala 3877a7ad07bfSEdward Tomasz Napierala sx_xlock(&allprison_lock); 3878a7ad07bfSEdward Tomasz Napierala prr = prison_racct_find_locked(name); 3879a7ad07bfSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 3880a7ad07bfSEdward Tomasz Napierala return (prr); 3881a7ad07bfSEdward Tomasz Napierala } 3882a7ad07bfSEdward Tomasz Napierala 3883a7ad07bfSEdward Tomasz Napierala void 3884a7ad07bfSEdward Tomasz Napierala prison_racct_hold(struct prison_racct *prr) 3885a7ad07bfSEdward Tomasz Napierala { 3886a7ad07bfSEdward Tomasz Napierala 38874b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 38884b5c9cf6SEdward Tomasz Napierala 3889a7ad07bfSEdward Tomasz Napierala refcount_acquire(&prr->prr_refcount); 3890a7ad07bfSEdward Tomasz Napierala } 3891a7ad07bfSEdward Tomasz Napierala 3892c34bbd2aSEdward Tomasz Napierala static void 3893c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(struct prison_racct *prr) 3894c34bbd2aSEdward Tomasz Napierala { 3895c34bbd2aSEdward Tomasz Napierala 38964b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 3897c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 3898c34bbd2aSEdward Tomasz Napierala 3899c34bbd2aSEdward Tomasz Napierala if (refcount_release(&prr->prr_refcount)) { 3900c34bbd2aSEdward Tomasz Napierala racct_destroy(&prr->prr_racct); 3901c34bbd2aSEdward Tomasz Napierala LIST_REMOVE(prr, prr_next); 3902c34bbd2aSEdward Tomasz Napierala free(prr, M_PRISON_RACCT); 3903c34bbd2aSEdward Tomasz Napierala } 3904c34bbd2aSEdward Tomasz Napierala } 3905c34bbd2aSEdward Tomasz Napierala 3906a7ad07bfSEdward Tomasz Napierala void 3907a7ad07bfSEdward Tomasz Napierala prison_racct_free(struct prison_racct *prr) 3908a7ad07bfSEdward Tomasz Napierala { 3909a7ad07bfSEdward Tomasz Napierala int old; 3910a7ad07bfSEdward Tomasz Napierala 39114b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 3912c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_UNLOCKED); 3913c34bbd2aSEdward Tomasz Napierala 3914a7ad07bfSEdward Tomasz Napierala old = prr->prr_refcount; 3915a7ad07bfSEdward Tomasz Napierala if (old > 1 && atomic_cmpset_int(&prr->prr_refcount, old, old - 1)) 3916a7ad07bfSEdward Tomasz Napierala return; 3917a7ad07bfSEdward Tomasz Napierala 3918a7ad07bfSEdward Tomasz Napierala sx_xlock(&allprison_lock); 3919c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(prr); 3920a7ad07bfSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 3921a7ad07bfSEdward Tomasz Napierala } 3922a7ad07bfSEdward Tomasz Napierala 3923a7ad07bfSEdward Tomasz Napierala static void 3924a7ad07bfSEdward Tomasz Napierala prison_racct_attach(struct prison *pr) 3925a7ad07bfSEdward Tomasz Napierala { 3926a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 3927a7ad07bfSEdward Tomasz Napierala 39284b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 3929c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 3930c34bbd2aSEdward Tomasz Napierala 3931a7ad07bfSEdward Tomasz Napierala prr = prison_racct_find_locked(pr->pr_name); 3932a7ad07bfSEdward Tomasz Napierala KASSERT(prr != NULL, ("cannot find prison_racct")); 3933a7ad07bfSEdward Tomasz Napierala 3934a7ad07bfSEdward Tomasz Napierala pr->pr_prison_racct = prr; 3935a7ad07bfSEdward Tomasz Napierala } 3936a7ad07bfSEdward Tomasz Napierala 3937c34bbd2aSEdward Tomasz Napierala /* 3938c34bbd2aSEdward Tomasz Napierala * Handle jail renaming. From the racct point of view, renaming means 3939c34bbd2aSEdward Tomasz Napierala * moving from one prison_racct to another. 3940c34bbd2aSEdward Tomasz Napierala */ 3941c34bbd2aSEdward Tomasz Napierala static void 3942c34bbd2aSEdward Tomasz Napierala prison_racct_modify(struct prison *pr) 3943c34bbd2aSEdward Tomasz Napierala { 3944c34bbd2aSEdward Tomasz Napierala struct proc *p; 3945c34bbd2aSEdward Tomasz Napierala struct ucred *cred; 3946c34bbd2aSEdward Tomasz Napierala struct prison_racct *oldprr; 3947c34bbd2aSEdward Tomasz Napierala 39484b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 39494b5c9cf6SEdward Tomasz Napierala 3950c34bbd2aSEdward Tomasz Napierala sx_slock(&allproc_lock); 3951c34bbd2aSEdward Tomasz Napierala sx_xlock(&allprison_lock); 3952c34bbd2aSEdward Tomasz Napierala 3953e30345e7SEdward Tomasz Napierala if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) { 3954e30345e7SEdward Tomasz Napierala sx_xunlock(&allprison_lock); 3955e30345e7SEdward Tomasz Napierala sx_sunlock(&allproc_lock); 3956c34bbd2aSEdward Tomasz Napierala return; 3957e30345e7SEdward Tomasz Napierala } 3958c34bbd2aSEdward Tomasz Napierala 3959c34bbd2aSEdward Tomasz Napierala oldprr = pr->pr_prison_racct; 3960c34bbd2aSEdward Tomasz Napierala pr->pr_prison_racct = NULL; 3961c34bbd2aSEdward Tomasz Napierala 3962c34bbd2aSEdward Tomasz Napierala prison_racct_attach(pr); 3963c34bbd2aSEdward Tomasz Napierala 3964c34bbd2aSEdward Tomasz Napierala /* 3965c34bbd2aSEdward Tomasz Napierala * Move resource utilisation records. 3966c34bbd2aSEdward Tomasz Napierala */ 3967c34bbd2aSEdward Tomasz Napierala racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct); 3968c34bbd2aSEdward Tomasz Napierala 3969*f87beb93SAndriy Gapon #ifdef RCTL 3970c34bbd2aSEdward Tomasz Napierala /* 3971c34bbd2aSEdward Tomasz Napierala * Force rctl to reattach rules to processes. 3972c34bbd2aSEdward Tomasz Napierala */ 3973c34bbd2aSEdward Tomasz Napierala FOREACH_PROC_IN_SYSTEM(p) { 3974c34bbd2aSEdward Tomasz Napierala PROC_LOCK(p); 3975c34bbd2aSEdward Tomasz Napierala cred = crhold(p->p_ucred); 3976c34bbd2aSEdward Tomasz Napierala PROC_UNLOCK(p); 3977*f87beb93SAndriy Gapon rctl_proc_ucred_changed(p, cred); 3978c34bbd2aSEdward Tomasz Napierala crfree(cred); 3979c34bbd2aSEdward Tomasz Napierala } 3980*f87beb93SAndriy Gapon #endif 3981c34bbd2aSEdward Tomasz Napierala 3982c34bbd2aSEdward Tomasz Napierala sx_sunlock(&allproc_lock); 3983c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(oldprr); 3984c34bbd2aSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 3985c34bbd2aSEdward Tomasz Napierala } 3986c34bbd2aSEdward Tomasz Napierala 3987a7ad07bfSEdward Tomasz Napierala static void 3988a7ad07bfSEdward Tomasz Napierala prison_racct_detach(struct prison *pr) 3989a7ad07bfSEdward Tomasz Napierala { 3990c34bbd2aSEdward Tomasz Napierala 39914b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 3992c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_UNLOCKED); 3993c34bbd2aSEdward Tomasz Napierala 3994af3c786cSMateusz Guzik if (pr->pr_prison_racct == NULL) 3995af3c786cSMateusz Guzik return; 3996a7ad07bfSEdward Tomasz Napierala prison_racct_free(pr->pr_prison_racct); 3997a7ad07bfSEdward Tomasz Napierala pr->pr_prison_racct = NULL; 3998a7ad07bfSEdward Tomasz Napierala } 3999a7ad07bfSEdward Tomasz Napierala #endif /* RACCT */ 4000a7ad07bfSEdward Tomasz Napierala 4001413628a7SBjoern A. Zeeb #ifdef DDB 4002b38ff370SJamie Gritton 4003b38ff370SJamie Gritton static void 4004b38ff370SJamie Gritton db_show_prison(struct prison *pr) 4005413628a7SBjoern A. Zeeb { 4006672756aaSJamie Gritton struct bool_flags *bf; 4007672756aaSJamie Gritton struct jailsys_flags *jsf; 4008b38ff370SJamie Gritton #if defined(INET) || defined(INET6) 4009b38ff370SJamie Gritton int ii; 4010413628a7SBjoern A. Zeeb #endif 4011672756aaSJamie Gritton unsigned f; 40128144690aSEric van Gyzen #ifdef INET 40138144690aSEric van Gyzen char ip4buf[INET_ADDRSTRLEN]; 40148144690aSEric van Gyzen #endif 4015413628a7SBjoern A. Zeeb #ifdef INET6 4016413628a7SBjoern A. Zeeb char ip6buf[INET6_ADDRSTRLEN]; 4017413628a7SBjoern A. Zeeb #endif 4018413628a7SBjoern A. Zeeb 4019b38ff370SJamie Gritton db_printf("prison %p:\n", pr); 4020b38ff370SJamie Gritton db_printf(" jid = %d\n", pr->pr_id); 4021b38ff370SJamie Gritton db_printf(" name = %s\n", pr->pr_name); 40220304c731SJamie Gritton db_printf(" parent = %p\n", pr->pr_parent); 4023b38ff370SJamie Gritton db_printf(" ref = %d\n", pr->pr_ref); 4024b38ff370SJamie Gritton db_printf(" uref = %d\n", pr->pr_uref); 4025b38ff370SJamie Gritton db_printf(" path = %s\n", pr->pr_path); 4026b38ff370SJamie Gritton db_printf(" cpuset = %d\n", pr->pr_cpuset 4027b38ff370SJamie Gritton ? pr->pr_cpuset->cs_id : -1); 4028679e1390SJamie Gritton #ifdef VIMAGE 4029679e1390SJamie Gritton db_printf(" vnet = %p\n", pr->pr_vnet); 4030679e1390SJamie Gritton #endif 4031b38ff370SJamie Gritton db_printf(" root = %p\n", pr->pr_root); 4032b38ff370SJamie Gritton db_printf(" securelevel = %d\n", pr->pr_securelevel); 40330cc207a6SMartin Matuska db_printf(" devfs_rsnum = %d\n", pr->pr_devfs_rsnum); 4034fe0518e9SBjoern A. Zeeb db_printf(" children.max = %d\n", pr->pr_childmax); 4035fe0518e9SBjoern A. Zeeb db_printf(" children.cur = %d\n", pr->pr_childcount); 40360304c731SJamie Gritton db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children)); 40370304c731SJamie Gritton db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling)); 4038fe0518e9SBjoern A. Zeeb db_printf(" flags = 0x%x", pr->pr_flags); 4039672756aaSJamie Gritton for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++) 4040672756aaSJamie Gritton if (pr->pr_flags & bf->flag) 4041672756aaSJamie Gritton db_printf(" %s", bf->name); 4042672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 4043672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 4044672756aaSJamie Gritton jsf++) { 4045672756aaSJamie Gritton f = pr->pr_flags & (jsf->disable | jsf->new); 4046672756aaSJamie Gritton db_printf(" %-16s= %s\n", jsf->name, 4047672756aaSJamie Gritton (f != 0 && f == jsf->disable) ? "disable" 4048672756aaSJamie Gritton : (f == jsf->new) ? "new" 40497cbf7213SJamie Gritton : "inherit"); 40507cbf7213SJamie Gritton } 4051fe0518e9SBjoern A. Zeeb db_printf(" allow = 0x%x", pr->pr_allow); 4052672756aaSJamie Gritton for (bf = pr_flag_allow; 4053672756aaSJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow); 4054672756aaSJamie Gritton bf++) 4055672756aaSJamie Gritton if (pr->pr_allow & bf->flag) 4056672756aaSJamie Gritton db_printf(" %s", bf->name); 4057b38ff370SJamie Gritton db_printf("\n"); 40580304c731SJamie Gritton db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs); 4059c1f19219SJamie Gritton db_printf(" host.hostname = %s\n", pr->pr_hostname); 4060c1f19219SJamie Gritton db_printf(" host.domainname = %s\n", pr->pr_domainname); 4061c1f19219SJamie Gritton db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid); 406276ca6f88SJamie Gritton db_printf(" host.hostid = %lu\n", pr->pr_hostid); 4063413628a7SBjoern A. Zeeb #ifdef INET 4064b38ff370SJamie Gritton db_printf(" ip4s = %d\n", pr->pr_ip4s); 4065b38ff370SJamie Gritton for (ii = 0; ii < pr->pr_ip4s; ii++) 4066b38ff370SJamie Gritton db_printf(" %s %s\n", 4067fe0518e9SBjoern A. Zeeb ii == 0 ? "ip4.addr =" : " ", 40688144690aSEric van Gyzen inet_ntoa_r(pr->pr_ip4[ii], ip4buf)); 4069413628a7SBjoern A. Zeeb #endif 4070413628a7SBjoern A. Zeeb #ifdef INET6 4071b38ff370SJamie Gritton db_printf(" ip6s = %d\n", pr->pr_ip6s); 4072b38ff370SJamie Gritton for (ii = 0; ii < pr->pr_ip6s; ii++) 4073b38ff370SJamie Gritton db_printf(" %s %s\n", 4074fe0518e9SBjoern A. Zeeb ii == 0 ? "ip6.addr =" : " ", 4075b38ff370SJamie Gritton ip6_sprintf(ip6buf, &pr->pr_ip6[ii])); 4076b38ff370SJamie Gritton #endif 4077b38ff370SJamie Gritton } 4078b38ff370SJamie Gritton 4079b38ff370SJamie Gritton DB_SHOW_COMMAND(prison, db_show_prison_command) 4080b38ff370SJamie Gritton { 4081b38ff370SJamie Gritton struct prison *pr; 4082b38ff370SJamie Gritton 4083b38ff370SJamie Gritton if (!have_addr) { 40840304c731SJamie Gritton /* 40850304c731SJamie Gritton * Show all prisons in the list, and prison0 which is not 40860304c731SJamie Gritton * listed. 40870304c731SJamie Gritton */ 40880304c731SJamie Gritton db_show_prison(&prison0); 40890304c731SJamie Gritton if (!db_pager_quit) { 4090b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 4091b38ff370SJamie Gritton db_show_prison(pr); 4092413628a7SBjoern A. Zeeb if (db_pager_quit) 4093413628a7SBjoern A. Zeeb break; 4094413628a7SBjoern A. Zeeb } 40950304c731SJamie Gritton } 4096b38ff370SJamie Gritton return; 4097413628a7SBjoern A. Zeeb } 4098b38ff370SJamie Gritton 40990304c731SJamie Gritton if (addr == 0) 41000304c731SJamie Gritton pr = &prison0; 41010304c731SJamie Gritton else { 4102b38ff370SJamie Gritton /* Look for a prison with the ID and with references. */ 4103b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 4104b38ff370SJamie Gritton if (pr->pr_id == addr && pr->pr_ref > 0) 4105b38ff370SJamie Gritton break; 4106b38ff370SJamie Gritton if (pr == NULL) 4107b38ff370SJamie Gritton /* Look again, without requiring a reference. */ 4108b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 4109b38ff370SJamie Gritton if (pr->pr_id == addr) 4110b38ff370SJamie Gritton break; 4111b38ff370SJamie Gritton if (pr == NULL) 4112b38ff370SJamie Gritton /* Assume address points to a valid prison. */ 4113b38ff370SJamie Gritton pr = (struct prison *)addr; 41140304c731SJamie Gritton } 4115b38ff370SJamie Gritton db_show_prison(pr); 4116b38ff370SJamie Gritton } 4117b38ff370SJamie Gritton 4118413628a7SBjoern A. Zeeb #endif /* DDB */ 4119