19454b2d8SWarner Losh /*- 2413628a7SBjoern A. Zeeb * Copyright (c) 1999 Poul-Henning Kamp. 3413628a7SBjoern A. Zeeb * Copyright (c) 2008 Bjoern A. Zeeb. 4b38ff370SJamie Gritton * Copyright (c) 2009 James Gritton. 5413628a7SBjoern A. Zeeb * All rights reserved. 6b9f0b66cSBjoern A. Zeeb * 7b9f0b66cSBjoern A. Zeeb * Redistribution and use in source and binary forms, with or without 8b9f0b66cSBjoern A. Zeeb * modification, are permitted provided that the following conditions 9b9f0b66cSBjoern A. Zeeb * are met: 10b9f0b66cSBjoern A. Zeeb * 1. Redistributions of source code must retain the above copyright 11b9f0b66cSBjoern A. Zeeb * notice, this list of conditions and the following disclaimer. 12b9f0b66cSBjoern A. Zeeb * 2. Redistributions in binary form must reproduce the above copyright 13b9f0b66cSBjoern A. Zeeb * notice, this list of conditions and the following disclaimer in the 14b9f0b66cSBjoern A. Zeeb * documentation and/or other materials provided with the distribution. 15b9f0b66cSBjoern A. Zeeb * 16b9f0b66cSBjoern A. Zeeb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17b9f0b66cSBjoern A. Zeeb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18b9f0b66cSBjoern A. Zeeb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19b9f0b66cSBjoern A. Zeeb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20b9f0b66cSBjoern A. Zeeb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21b9f0b66cSBjoern A. Zeeb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22b9f0b66cSBjoern A. Zeeb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23b9f0b66cSBjoern A. Zeeb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24b9f0b66cSBjoern A. Zeeb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25b9f0b66cSBjoern A. Zeeb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26b9f0b66cSBjoern A. Zeeb * SUCH DAMAGE. 2707901f22SPoul-Henning Kamp */ 2875c13541SPoul-Henning Kamp 29677b542eSDavid E. O'Brien #include <sys/cdefs.h> 30677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 31677b542eSDavid E. O'Brien 3276ca6f88SJamie Gritton #include "opt_compat.h" 33413628a7SBjoern A. Zeeb #include "opt_ddb.h" 34413628a7SBjoern A. Zeeb #include "opt_inet.h" 35413628a7SBjoern A. Zeeb #include "opt_inet6.h" 3646e3b1cbSPawel Jakub Dawidek 3775c13541SPoul-Henning Kamp #include <sys/param.h> 3875c13541SPoul-Henning Kamp #include <sys/types.h> 3975c13541SPoul-Henning Kamp #include <sys/kernel.h> 4075c13541SPoul-Henning Kamp #include <sys/systm.h> 4175c13541SPoul-Henning Kamp #include <sys/errno.h> 4275c13541SPoul-Henning Kamp #include <sys/sysproto.h> 4375c13541SPoul-Henning Kamp #include <sys/malloc.h> 440304c731SJamie Gritton #include <sys/osd.h> 45800c9408SRobert Watson #include <sys/priv.h> 4675c13541SPoul-Henning Kamp #include <sys/proc.h> 47b3059e09SRobert Watson #include <sys/taskqueue.h> 4857b4252eSKonstantin Belousov #include <sys/fcntl.h> 4975c13541SPoul-Henning Kamp #include <sys/jail.h> 5001137630SRobert Watson #include <sys/lock.h> 5101137630SRobert Watson #include <sys/mutex.h> 52dc68a633SPawel Jakub Dawidek #include <sys/sx.h> 5376ca6f88SJamie Gritton #include <sys/sysent.h> 54fd7a8150SMike Barcroft #include <sys/namei.h> 55820a0de9SPawel Jakub Dawidek #include <sys/mount.h> 56fd7a8150SMike Barcroft #include <sys/queue.h> 5775c13541SPoul-Henning Kamp #include <sys/socket.h> 58fd7a8150SMike Barcroft #include <sys/syscallsubr.h> 5983f1e257SRobert Watson #include <sys/sysctl.h> 60fd7a8150SMike Barcroft #include <sys/vnode.h> 61603724d3SBjoern A. Zeeb #include <sys/vimage.h> 6275c13541SPoul-Henning Kamp #include <net/if.h> 6375c13541SPoul-Henning Kamp #include <netinet/in.h> 64413628a7SBjoern A. Zeeb #ifdef DDB 65413628a7SBjoern A. Zeeb #include <ddb/ddb.h> 66413628a7SBjoern A. Zeeb #ifdef INET6 67413628a7SBjoern A. Zeeb #include <netinet6/in6_var.h> 68413628a7SBjoern A. Zeeb #endif /* INET6 */ 69413628a7SBjoern A. Zeeb #endif /* DDB */ 7075c13541SPoul-Henning Kamp 71aed55708SRobert Watson #include <security/mac/mac_framework.h> 72aed55708SRobert Watson 738986e3a0SJamie Gritton #define DEFAULT_HOSTUUID "00000000-0000-0000-0000-000000000000" 748986e3a0SJamie Gritton 7575c13541SPoul-Henning Kamp MALLOC_DEFINE(M_PRISON, "prison", "Prison structures"); 7675c13541SPoul-Henning Kamp 770304c731SJamie Gritton /* prison0 describes what is "real" about the system. */ 780304c731SJamie Gritton struct prison prison0 = { 790304c731SJamie Gritton .pr_id = 0, 800304c731SJamie Gritton .pr_name = "0", 810304c731SJamie Gritton .pr_ref = 1, 820304c731SJamie Gritton .pr_uref = 1, 830304c731SJamie Gritton .pr_path = "/", 840304c731SJamie Gritton .pr_securelevel = -1, 85b97457e2SJamie Gritton .pr_childmax = JAIL_MAX, 868986e3a0SJamie Gritton .pr_hostuuid = DEFAULT_HOSTUUID, 870304c731SJamie Gritton .pr_children = LIST_HEAD_INITIALIZER(&prison0.pr_children), 8876ca6f88SJamie Gritton .pr_flags = PR_HOST, 890304c731SJamie Gritton .pr_allow = PR_ALLOW_ALL, 900304c731SJamie Gritton }; 910304c731SJamie Gritton MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF); 9283f1e257SRobert Watson 930304c731SJamie Gritton /* allprison and lastprid are protected by allprison_lock. */ 94dc68a633SPawel Jakub Dawidek struct sx allprison_lock; 95b38ff370SJamie Gritton SX_SYSINIT(allprison_lock, &allprison_lock, "allprison"); 96b38ff370SJamie Gritton struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison); 972110d913SXin LI int lastprid = 0; 98fd7a8150SMike Barcroft 99b38ff370SJamie Gritton static int do_jail_attach(struct thread *td, struct prison *pr); 100b3059e09SRobert Watson static void prison_complete(void *context, int pending); 101b38ff370SJamie Gritton static void prison_deref(struct prison *pr, int flags); 1020304c731SJamie Gritton static char *prison_path(struct prison *pr1, struct prison *pr2); 1030304c731SJamie Gritton static void prison_remove_one(struct prison *pr); 104413628a7SBjoern A. Zeeb #ifdef INET 1058571af59SJamie Gritton static int _prison_check_ip4(struct prison *pr, struct in_addr *ia); 1060304c731SJamie Gritton static int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4); 107413628a7SBjoern A. Zeeb #endif 108413628a7SBjoern A. Zeeb #ifdef INET6 1098571af59SJamie Gritton static int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6); 1100304c731SJamie Gritton static int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6); 111413628a7SBjoern A. Zeeb #endif 112fd7a8150SMike Barcroft 113b38ff370SJamie Gritton /* Flags for prison_deref */ 114b38ff370SJamie Gritton #define PD_DEREF 0x01 115b38ff370SJamie Gritton #define PD_DEUREF 0x02 116b38ff370SJamie Gritton #define PD_LOCKED 0x04 117b38ff370SJamie Gritton #define PD_LIST_SLOCKED 0x08 118b38ff370SJamie Gritton #define PD_LIST_XLOCKED 0x10 119fd7a8150SMike Barcroft 1200304c731SJamie Gritton /* 1210304c731SJamie Gritton * Parameter names corresponding to PR_* flag values 1220304c731SJamie Gritton */ 1230304c731SJamie Gritton static char *pr_flag_names[] = { 1240304c731SJamie Gritton [0] = "persist", 1250304c731SJamie Gritton }; 1260304c731SJamie Gritton 1270304c731SJamie Gritton static char *pr_flag_nonames[] = { 1280304c731SJamie Gritton [0] = "nopersist", 1297cbf7213SJamie Gritton }; 1307cbf7213SJamie Gritton 1317cbf7213SJamie Gritton struct jailsys_flags { 1327cbf7213SJamie Gritton const char *name; 1337cbf7213SJamie Gritton unsigned disable; 1347cbf7213SJamie Gritton unsigned new; 1357cbf7213SJamie Gritton } pr_flag_jailsys[] = { 1367cbf7213SJamie Gritton { "host", 0, PR_HOST }, 1377cbf7213SJamie Gritton #ifdef VIMAGE 1387cbf7213SJamie Gritton { "vnet", 0, PR_VNET }, 1397cbf7213SJamie Gritton #endif 1400304c731SJamie Gritton #ifdef INET 1417cbf7213SJamie Gritton { "ip4", PR_IP4_USER | PR_IP4_DISABLE, PR_IP4_USER }, 1420304c731SJamie Gritton #endif 1430304c731SJamie Gritton #ifdef INET6 1447cbf7213SJamie Gritton { "ip6", PR_IP6_USER | PR_IP6_DISABLE, PR_IP6_USER }, 145679e1390SJamie Gritton #endif 1460304c731SJamie Gritton }; 1470304c731SJamie Gritton 1480304c731SJamie Gritton static char *pr_allow_names[] = { 1490304c731SJamie Gritton "allow.set_hostname", 1500304c731SJamie Gritton "allow.sysvipc", 1510304c731SJamie Gritton "allow.raw_sockets", 1520304c731SJamie Gritton "allow.chflags", 1530304c731SJamie Gritton "allow.mount", 1540304c731SJamie Gritton "allow.quotas", 1550304c731SJamie Gritton "allow.socket_af", 1560304c731SJamie Gritton }; 1570304c731SJamie Gritton 1580304c731SJamie Gritton static char *pr_allow_nonames[] = { 1590304c731SJamie Gritton "allow.noset_hostname", 1600304c731SJamie Gritton "allow.nosysvipc", 1610304c731SJamie Gritton "allow.noraw_sockets", 1620304c731SJamie Gritton "allow.nochflags", 1630304c731SJamie Gritton "allow.nomount", 1640304c731SJamie Gritton "allow.noquotas", 1650304c731SJamie Gritton "allow.nosocket_af", 1660304c731SJamie Gritton }; 1670304c731SJamie Gritton 1680304c731SJamie Gritton #define JAIL_DEFAULT_ALLOW PR_ALLOW_SET_HOSTNAME 1690304c731SJamie Gritton static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW; 1700304c731SJamie Gritton static int jail_default_enforce_statfs = 2; 1710304c731SJamie Gritton #if defined(INET) || defined(INET6) 172e92e0574SJamie Gritton static unsigned jail_max_af_ips = 255; 1730304c731SJamie Gritton #endif 1740304c731SJamie Gritton 175413628a7SBjoern A. Zeeb #ifdef INET 176413628a7SBjoern A. Zeeb static int 177413628a7SBjoern A. Zeeb qcmp_v4(const void *ip1, const void *ip2) 178413628a7SBjoern A. Zeeb { 179413628a7SBjoern A. Zeeb in_addr_t iaa, iab; 180413628a7SBjoern A. Zeeb 181413628a7SBjoern A. Zeeb /* 182413628a7SBjoern A. Zeeb * We need to compare in HBO here to get the list sorted as expected 183413628a7SBjoern A. Zeeb * by the result of the code. Sorting NBO addresses gives you 184413628a7SBjoern A. Zeeb * interesting results. If you do not understand, do not try. 185413628a7SBjoern A. Zeeb */ 186413628a7SBjoern A. Zeeb iaa = ntohl(((const struct in_addr *)ip1)->s_addr); 187413628a7SBjoern A. Zeeb iab = ntohl(((const struct in_addr *)ip2)->s_addr); 188413628a7SBjoern A. Zeeb 189413628a7SBjoern A. Zeeb /* 190413628a7SBjoern A. Zeeb * Do not simply return the difference of the two numbers, the int is 191413628a7SBjoern A. Zeeb * not wide enough. 192413628a7SBjoern A. Zeeb */ 193413628a7SBjoern A. Zeeb if (iaa > iab) 194413628a7SBjoern A. Zeeb return (1); 195413628a7SBjoern A. Zeeb else if (iaa < iab) 196413628a7SBjoern A. Zeeb return (-1); 197413628a7SBjoern A. Zeeb else 198413628a7SBjoern A. Zeeb return (0); 199413628a7SBjoern A. Zeeb } 200413628a7SBjoern A. Zeeb #endif 201413628a7SBjoern A. Zeeb 202413628a7SBjoern A. Zeeb #ifdef INET6 203413628a7SBjoern A. Zeeb static int 204413628a7SBjoern A. Zeeb qcmp_v6(const void *ip1, const void *ip2) 205413628a7SBjoern A. Zeeb { 206413628a7SBjoern A. Zeeb const struct in6_addr *ia6a, *ia6b; 207413628a7SBjoern A. Zeeb int i, rc; 208413628a7SBjoern A. Zeeb 209413628a7SBjoern A. Zeeb ia6a = (const struct in6_addr *)ip1; 210413628a7SBjoern A. Zeeb ia6b = (const struct in6_addr *)ip2; 211413628a7SBjoern A. Zeeb 212413628a7SBjoern A. Zeeb rc = 0; 213413628a7SBjoern A. Zeeb for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) { 214413628a7SBjoern A. Zeeb if (ia6a->s6_addr[i] > ia6b->s6_addr[i]) 215413628a7SBjoern A. Zeeb rc = 1; 216413628a7SBjoern A. Zeeb else if (ia6a->s6_addr[i] < ia6b->s6_addr[i]) 217413628a7SBjoern A. Zeeb rc = -1; 218413628a7SBjoern A. Zeeb } 219413628a7SBjoern A. Zeeb return (rc); 220413628a7SBjoern A. Zeeb } 221413628a7SBjoern A. Zeeb #endif 222413628a7SBjoern A. Zeeb 223116734c4SMatthew Dillon /* 2249ddb7954SMike Barcroft * struct jail_args { 2259ddb7954SMike Barcroft * struct jail *jail; 2269ddb7954SMike Barcroft * }; 227116734c4SMatthew Dillon */ 22875c13541SPoul-Henning Kamp int 2299ddb7954SMike Barcroft jail(struct thread *td, struct jail_args *uap) 23075c13541SPoul-Henning Kamp { 231413628a7SBjoern A. Zeeb uint32_t version; 232413628a7SBjoern A. Zeeb int error; 2330304c731SJamie Gritton struct jail j; 234413628a7SBjoern A. Zeeb 235413628a7SBjoern A. Zeeb error = copyin(uap->jail, &version, sizeof(uint32_t)); 236413628a7SBjoern A. Zeeb if (error) 237413628a7SBjoern A. Zeeb return (error); 238413628a7SBjoern A. Zeeb 239413628a7SBjoern A. Zeeb switch (version) { 240413628a7SBjoern A. Zeeb case 0: 241413628a7SBjoern A. Zeeb { 242413628a7SBjoern A. Zeeb struct jail_v0 j0; 243413628a7SBjoern A. Zeeb 2440304c731SJamie Gritton /* FreeBSD single IPv4 jails. */ 2450304c731SJamie Gritton bzero(&j, sizeof(struct jail)); 246413628a7SBjoern A. Zeeb error = copyin(uap->jail, &j0, sizeof(struct jail_v0)); 247413628a7SBjoern A. Zeeb if (error) 248413628a7SBjoern A. Zeeb return (error); 2490304c731SJamie Gritton j.version = j0.version; 2500304c731SJamie Gritton j.path = j0.path; 2510304c731SJamie Gritton j.hostname = j0.hostname; 2520304c731SJamie Gritton j.ip4s = j0.ip_number; 253413628a7SBjoern A. Zeeb break; 254413628a7SBjoern A. Zeeb } 255413628a7SBjoern A. Zeeb 256413628a7SBjoern A. Zeeb case 1: 257413628a7SBjoern A. Zeeb /* 258413628a7SBjoern A. Zeeb * Version 1 was used by multi-IPv4 jail implementations 259413628a7SBjoern A. Zeeb * that never made it into the official kernel. 260413628a7SBjoern A. Zeeb */ 261413628a7SBjoern A. Zeeb return (EINVAL); 262413628a7SBjoern A. Zeeb 263413628a7SBjoern A. Zeeb case 2: /* JAIL_API_VERSION */ 264413628a7SBjoern A. Zeeb /* FreeBSD multi-IPv4/IPv6,noIP jails. */ 265413628a7SBjoern A. Zeeb error = copyin(uap->jail, &j, sizeof(struct jail)); 266413628a7SBjoern A. Zeeb if (error) 267413628a7SBjoern A. Zeeb return (error); 2680304c731SJamie Gritton break; 2690304c731SJamie Gritton 2700304c731SJamie Gritton default: 2710304c731SJamie Gritton /* Sci-Fi jails are not supported, sorry. */ 2720304c731SJamie Gritton return (EINVAL); 2730304c731SJamie Gritton } 2740304c731SJamie Gritton return (kern_jail(td, &j)); 2750304c731SJamie Gritton } 2760304c731SJamie Gritton 2770304c731SJamie Gritton int 2780304c731SJamie Gritton kern_jail(struct thread *td, struct jail *j) 2790304c731SJamie Gritton { 280e92e0574SJamie Gritton struct iovec optiov[2 * (4 281e92e0574SJamie Gritton + sizeof(pr_allow_names) / sizeof(pr_allow_names[0]) 282e92e0574SJamie Gritton #ifdef INET 283e92e0574SJamie Gritton + 1 284e92e0574SJamie Gritton #endif 285e92e0574SJamie Gritton #ifdef INET6 286e92e0574SJamie Gritton + 1 287e92e0574SJamie Gritton #endif 288e92e0574SJamie Gritton )]; 2890304c731SJamie Gritton struct uio opt; 2900304c731SJamie Gritton char *u_path, *u_hostname, *u_name; 2910304c731SJamie Gritton #ifdef INET 292e92e0574SJamie Gritton uint32_t ip4s; 2930304c731SJamie Gritton struct in_addr *u_ip4; 2940304c731SJamie Gritton #endif 2950304c731SJamie Gritton #ifdef INET6 2960304c731SJamie Gritton struct in6_addr *u_ip6; 2970304c731SJamie Gritton #endif 2980304c731SJamie Gritton size_t tmplen; 2990304c731SJamie Gritton int error, enforce_statfs, fi; 3000304c731SJamie Gritton 3010304c731SJamie Gritton bzero(&optiov, sizeof(optiov)); 3020304c731SJamie Gritton opt.uio_iov = optiov; 3030304c731SJamie Gritton opt.uio_iovcnt = 0; 3040304c731SJamie Gritton opt.uio_offset = -1; 3050304c731SJamie Gritton opt.uio_resid = -1; 3060304c731SJamie Gritton opt.uio_segflg = UIO_SYSSPACE; 3070304c731SJamie Gritton opt.uio_rw = UIO_READ; 3080304c731SJamie Gritton opt.uio_td = td; 3090304c731SJamie Gritton 3100304c731SJamie Gritton /* Set permissions for top-level jails from sysctls. */ 3110304c731SJamie Gritton if (!jailed(td->td_ucred)) { 3120304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_allow_names) / 3130304c731SJamie Gritton sizeof(pr_allow_names[0]); fi++) { 3140304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = 3150304c731SJamie Gritton (jail_default_allow & (1 << fi)) 3160304c731SJamie Gritton ? pr_allow_names[fi] : pr_allow_nonames[fi]; 3170304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = 3180304c731SJamie Gritton strlen(optiov[opt.uio_iovcnt].iov_base) + 1; 3190304c731SJamie Gritton opt.uio_iovcnt += 2; 3200304c731SJamie Gritton } 3210304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "enforce_statfs"; 3220304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs"); 3230304c731SJamie Gritton opt.uio_iovcnt++; 3240304c731SJamie Gritton enforce_statfs = jail_default_enforce_statfs; 3250304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = &enforce_statfs; 3260304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs); 3270304c731SJamie Gritton opt.uio_iovcnt++; 3280304c731SJamie Gritton } 3290304c731SJamie Gritton 330b38ff370SJamie Gritton tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN; 331b38ff370SJamie Gritton #ifdef INET 3320304c731SJamie Gritton ip4s = (j->version == 0) ? 1 : j->ip4s; 3330304c731SJamie Gritton if (ip4s > jail_max_af_ips) 334b38ff370SJamie Gritton return (EINVAL); 3350304c731SJamie Gritton tmplen += ip4s * sizeof(struct in_addr); 336b38ff370SJamie Gritton #else 3370304c731SJamie Gritton if (j->ip4s > 0) 338b38ff370SJamie Gritton return (EINVAL); 339b38ff370SJamie Gritton #endif 340b38ff370SJamie Gritton #ifdef INET6 3410304c731SJamie Gritton if (j->ip6s > jail_max_af_ips) 342b38ff370SJamie Gritton return (EINVAL); 3430304c731SJamie Gritton tmplen += j->ip6s * sizeof(struct in6_addr); 344b38ff370SJamie Gritton #else 3450304c731SJamie Gritton if (j->ip6s > 0) 346b38ff370SJamie Gritton return (EINVAL); 347b38ff370SJamie Gritton #endif 348b38ff370SJamie Gritton u_path = malloc(tmplen, M_TEMP, M_WAITOK); 349b38ff370SJamie Gritton u_hostname = u_path + MAXPATHLEN; 350b38ff370SJamie Gritton u_name = u_hostname + MAXHOSTNAMELEN; 351b38ff370SJamie Gritton #ifdef INET 352b38ff370SJamie Gritton u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN); 353b38ff370SJamie Gritton #endif 354b38ff370SJamie Gritton #ifdef INET6 355b38ff370SJamie Gritton #ifdef INET 3560304c731SJamie Gritton u_ip6 = (struct in6_addr *)(u_ip4 + ip4s); 357b38ff370SJamie Gritton #else 358b38ff370SJamie Gritton u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN); 359b38ff370SJamie Gritton #endif 360b38ff370SJamie Gritton #endif 3610304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "path"; 3620304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("path"); 3630304c731SJamie Gritton opt.uio_iovcnt++; 3640304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_path; 3650304c731SJamie Gritton error = copyinstr(j->path, u_path, MAXPATHLEN, 3660304c731SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 367b38ff370SJamie Gritton if (error) { 368b38ff370SJamie Gritton free(u_path, M_TEMP); 369b38ff370SJamie Gritton return (error); 370b38ff370SJamie Gritton } 3710304c731SJamie Gritton opt.uio_iovcnt++; 3720304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "host.hostname"; 3730304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname"); 3740304c731SJamie Gritton opt.uio_iovcnt++; 3750304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_hostname; 3760304c731SJamie Gritton error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN, 3770304c731SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 378b38ff370SJamie Gritton if (error) { 379b38ff370SJamie Gritton free(u_path, M_TEMP); 380b38ff370SJamie Gritton return (error); 381b38ff370SJamie Gritton } 3820304c731SJamie Gritton opt.uio_iovcnt++; 3830304c731SJamie Gritton if (j->jailname != NULL) { 384b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "name"; 385b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("name"); 386b38ff370SJamie Gritton opt.uio_iovcnt++; 387b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_name; 3880304c731SJamie Gritton error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN, 389b38ff370SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 390b38ff370SJamie Gritton if (error) { 391b38ff370SJamie Gritton free(u_path, M_TEMP); 392b38ff370SJamie Gritton return (error); 393b38ff370SJamie Gritton } 394b38ff370SJamie Gritton opt.uio_iovcnt++; 395b38ff370SJamie Gritton } 396b38ff370SJamie Gritton #ifdef INET 397b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "ip4.addr"; 398b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr"); 399b38ff370SJamie Gritton opt.uio_iovcnt++; 400b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_ip4; 4010304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr); 4020304c731SJamie Gritton if (j->version == 0) 4030304c731SJamie Gritton u_ip4->s_addr = j->ip4s; 4040304c731SJamie Gritton else { 4050304c731SJamie Gritton error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len); 406b38ff370SJamie Gritton if (error) { 407b38ff370SJamie Gritton free(u_path, M_TEMP); 408b38ff370SJamie Gritton return (error); 409b38ff370SJamie Gritton } 4100304c731SJamie Gritton } 411b38ff370SJamie Gritton opt.uio_iovcnt++; 412b38ff370SJamie Gritton #endif 413b38ff370SJamie Gritton #ifdef INET6 414b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "ip6.addr"; 415b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr"); 416b38ff370SJamie Gritton opt.uio_iovcnt++; 417b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_ip6; 4180304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr); 4190304c731SJamie Gritton error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len); 420b38ff370SJamie Gritton if (error) { 421b38ff370SJamie Gritton free(u_path, M_TEMP); 422b38ff370SJamie Gritton return (error); 423b38ff370SJamie Gritton } 424b38ff370SJamie Gritton opt.uio_iovcnt++; 425b38ff370SJamie Gritton #endif 4260304c731SJamie Gritton KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]), 4270304c731SJamie Gritton ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt)); 428b38ff370SJamie Gritton error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH); 429b38ff370SJamie Gritton free(u_path, M_TEMP); 430b38ff370SJamie Gritton return (error); 431b38ff370SJamie Gritton } 432b38ff370SJamie Gritton 4330304c731SJamie Gritton 434b38ff370SJamie Gritton /* 435b38ff370SJamie Gritton * struct jail_set_args { 436b38ff370SJamie Gritton * struct iovec *iovp; 437b38ff370SJamie Gritton * unsigned int iovcnt; 438b38ff370SJamie Gritton * int flags; 439b38ff370SJamie Gritton * }; 440b38ff370SJamie Gritton */ 441b38ff370SJamie Gritton int 442b38ff370SJamie Gritton jail_set(struct thread *td, struct jail_set_args *uap) 443b38ff370SJamie Gritton { 444b38ff370SJamie Gritton struct uio *auio; 445b38ff370SJamie Gritton int error; 446b38ff370SJamie Gritton 447b38ff370SJamie Gritton /* Check that we have an even number of iovecs. */ 448b38ff370SJamie Gritton if (uap->iovcnt & 1) 449b38ff370SJamie Gritton return (EINVAL); 450b38ff370SJamie Gritton 451b38ff370SJamie Gritton error = copyinuio(uap->iovp, uap->iovcnt, &auio); 452b38ff370SJamie Gritton if (error) 453b38ff370SJamie Gritton return (error); 454b38ff370SJamie Gritton error = kern_jail_set(td, auio, uap->flags); 455b38ff370SJamie Gritton free(auio, M_IOV); 456b38ff370SJamie Gritton return (error); 457413628a7SBjoern A. Zeeb } 458413628a7SBjoern A. Zeeb 459413628a7SBjoern A. Zeeb int 460b38ff370SJamie Gritton kern_jail_set(struct thread *td, struct uio *optuio, int flags) 461413628a7SBjoern A. Zeeb { 462fd7a8150SMike Barcroft struct nameidata nd; 463b38ff370SJamie Gritton #ifdef INET 464b38ff370SJamie Gritton struct in_addr *ip4; 465413628a7SBjoern A. Zeeb #endif 466b38ff370SJamie Gritton #ifdef INET6 467b38ff370SJamie Gritton struct in6_addr *ip6; 468b38ff370SJamie Gritton #endif 469b38ff370SJamie Gritton struct vfsopt *opt; 470b38ff370SJamie Gritton struct vfsoptlist *opts; 471bdfc8cc4SJamie Gritton struct prison *pr, *deadpr, *mypr, *ppr, *tpr, *tppr; 472b38ff370SJamie Gritton struct vnode *root; 47376ca6f88SJamie Gritton char *domain, *errmsg, *host, *name, *p, *path, *uuid; 4740304c731SJamie Gritton #if defined(INET) || defined(INET6) 475b38ff370SJamie Gritton void *op; 4760304c731SJamie Gritton #endif 47776ca6f88SJamie Gritton unsigned long hid; 4780304c731SJamie Gritton size_t namelen, onamelen; 4790304c731SJamie Gritton int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos; 4807cbf7213SJamie Gritton int gotchildmax, gotenforce, gothid, gotslevel; 4817cbf7213SJamie Gritton int fi, jid, jsys, len, level; 482b97457e2SJamie Gritton int childmax, slevel, vfslocked; 483d465a41dSBjoern A. Zeeb #if defined(INET) || defined(INET6) 4840304c731SJamie Gritton int ii, ij; 485413628a7SBjoern A. Zeeb #endif 486413628a7SBjoern A. Zeeb #ifdef INET 4870304c731SJamie Gritton int ip4s, ip4a, redo_ip4; 488413628a7SBjoern A. Zeeb #endif 489b38ff370SJamie Gritton #ifdef INET6 4900304c731SJamie Gritton int ip6s, ip6a, redo_ip6; 491b38ff370SJamie Gritton #endif 492b38ff370SJamie Gritton unsigned pr_flags, ch_flags; 4930304c731SJamie Gritton unsigned pr_allow, ch_allow, tallow; 494b38ff370SJamie Gritton char numbuf[12]; 495b38ff370SJamie Gritton 496b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_SET); 497b38ff370SJamie Gritton if (!error && (flags & JAIL_ATTACH)) 498b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 499b38ff370SJamie Gritton if (error) 50075c13541SPoul-Henning Kamp return (error); 5010304c731SJamie Gritton mypr = ppr = td->td_ucred->cr_prison; 502b97457e2SJamie Gritton if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0) 5030304c731SJamie Gritton return (EPERM); 504b38ff370SJamie Gritton if (flags & ~JAIL_SET_MASK) 505b38ff370SJamie Gritton return (EINVAL); 506b38ff370SJamie Gritton 507b38ff370SJamie Gritton /* 508b38ff370SJamie Gritton * Check all the parameters before committing to anything. Not all 509b38ff370SJamie Gritton * errors can be caught early, but we may as well try. Also, this 510b38ff370SJamie Gritton * takes care of some expensive stuff (path lookup) before getting 511b38ff370SJamie Gritton * the allprison lock. 512b38ff370SJamie Gritton * 513b38ff370SJamie Gritton * XXX Jails are not filesystems, and jail parameters are not mount 514b38ff370SJamie Gritton * options. But it makes more sense to re-use the vfsopt code 515b38ff370SJamie Gritton * than duplicate it under a different name. 516b38ff370SJamie Gritton */ 517b38ff370SJamie Gritton error = vfs_buildopts(optuio, &opts); 518b38ff370SJamie Gritton if (error) 519b38ff370SJamie Gritton return (error); 520b38ff370SJamie Gritton #ifdef INET 5210304c731SJamie Gritton ip4a = 0; 522b38ff370SJamie Gritton ip4 = NULL; 523b38ff370SJamie Gritton #endif 524b38ff370SJamie Gritton #ifdef INET6 5250304c731SJamie Gritton ip6a = 0; 526b38ff370SJamie Gritton ip6 = NULL; 527b38ff370SJamie Gritton #endif 528b38ff370SJamie Gritton 5290304c731SJamie Gritton #if defined(INET) || defined(INET6) 5300304c731SJamie Gritton again: 5310304c731SJamie Gritton #endif 532b38ff370SJamie Gritton error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 533b38ff370SJamie Gritton if (error == ENOENT) 534b38ff370SJamie Gritton jid = 0; 535b38ff370SJamie Gritton else if (error != 0) 536b38ff370SJamie Gritton goto done_free; 537b38ff370SJamie Gritton 538b38ff370SJamie Gritton error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel)); 539b38ff370SJamie Gritton if (error == ENOENT) 540b38ff370SJamie Gritton gotslevel = 0; 541b38ff370SJamie Gritton else if (error != 0) 542b38ff370SJamie Gritton goto done_free; 543b38ff370SJamie Gritton else 544b38ff370SJamie Gritton gotslevel = 1; 545b38ff370SJamie Gritton 546b97457e2SJamie Gritton error = 547b97457e2SJamie Gritton vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax)); 548b97457e2SJamie Gritton if (error == ENOENT) 549b97457e2SJamie Gritton gotchildmax = 0; 550b97457e2SJamie Gritton else if (error != 0) 551b97457e2SJamie Gritton goto done_free; 552b97457e2SJamie Gritton else 553b97457e2SJamie Gritton gotchildmax = 1; 554b97457e2SJamie Gritton 5550304c731SJamie Gritton error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce)); 5560304c731SJamie Gritton gotenforce = (error == 0); 5570304c731SJamie Gritton if (gotenforce) { 5580304c731SJamie Gritton if (enforce < 0 || enforce > 2) 5590304c731SJamie Gritton return (EINVAL); 5600304c731SJamie Gritton } else if (error != ENOENT) 5610304c731SJamie Gritton goto done_free; 5620304c731SJamie Gritton 563b38ff370SJamie Gritton pr_flags = ch_flags = 0; 5640304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 5650304c731SJamie Gritton fi++) { 5660304c731SJamie Gritton if (pr_flag_names[fi] == NULL) 5670304c731SJamie Gritton continue; 5680304c731SJamie Gritton vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi); 5690304c731SJamie Gritton vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi); 5700304c731SJamie Gritton } 571b38ff370SJamie Gritton ch_flags |= pr_flags; 5727cbf7213SJamie Gritton for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 5737cbf7213SJamie Gritton fi++) { 5747cbf7213SJamie Gritton error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys, 5757cbf7213SJamie Gritton sizeof(jsys)); 5767cbf7213SJamie Gritton if (error == ENOENT) 5777cbf7213SJamie Gritton continue; 5787cbf7213SJamie Gritton if (error != 0) 5797cbf7213SJamie Gritton goto done_free; 5807cbf7213SJamie Gritton switch (jsys) { 5817cbf7213SJamie Gritton case JAIL_SYS_DISABLE: 5827cbf7213SJamie Gritton if (!pr_flag_jailsys[fi].disable) { 5837cbf7213SJamie Gritton error = EINVAL; 5847cbf7213SJamie Gritton goto done_free; 5857cbf7213SJamie Gritton } 5867cbf7213SJamie Gritton pr_flags |= pr_flag_jailsys[fi].disable; 5877cbf7213SJamie Gritton break; 5887cbf7213SJamie Gritton case JAIL_SYS_NEW: 5897cbf7213SJamie Gritton pr_flags |= pr_flag_jailsys[fi].new; 5907cbf7213SJamie Gritton break; 5917cbf7213SJamie Gritton case JAIL_SYS_INHERIT: 5927cbf7213SJamie Gritton break; 5937cbf7213SJamie Gritton default: 5947cbf7213SJamie Gritton error = EINVAL; 5957cbf7213SJamie Gritton goto done_free; 5967cbf7213SJamie Gritton } 5977cbf7213SJamie Gritton ch_flags |= 5987cbf7213SJamie Gritton pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable; 5997cbf7213SJamie Gritton } 600b38ff370SJamie Gritton if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE 601b38ff370SJamie Gritton && !(pr_flags & PR_PERSIST)) { 602b38ff370SJamie Gritton error = EINVAL; 603b38ff370SJamie Gritton vfs_opterror(opts, "new jail must persist or attach"); 604b38ff370SJamie Gritton goto done_errmsg; 605b38ff370SJamie Gritton } 606679e1390SJamie Gritton #ifdef VIMAGE 607679e1390SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) { 608679e1390SJamie Gritton error = EINVAL; 609679e1390SJamie Gritton vfs_opterror(opts, "vnet cannot be changed after creation"); 610679e1390SJamie Gritton goto done_errmsg; 611679e1390SJamie Gritton } 612679e1390SJamie Gritton #endif 613b38ff370SJamie Gritton 6140304c731SJamie Gritton pr_allow = ch_allow = 0; 6150304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 6160304c731SJamie Gritton fi++) { 6170304c731SJamie Gritton vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi); 6180304c731SJamie Gritton vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi); 6190304c731SJamie Gritton } 6200304c731SJamie Gritton ch_allow |= pr_allow; 6210304c731SJamie Gritton 622b38ff370SJamie Gritton error = vfs_getopt(opts, "name", (void **)&name, &len); 623b38ff370SJamie Gritton if (error == ENOENT) 624b38ff370SJamie Gritton name = NULL; 625b38ff370SJamie Gritton else if (error != 0) 626b38ff370SJamie Gritton goto done_free; 627b38ff370SJamie Gritton else { 628b38ff370SJamie Gritton if (len == 0 || name[len - 1] != '\0') { 629b38ff370SJamie Gritton error = EINVAL; 630b38ff370SJamie Gritton goto done_free; 631b38ff370SJamie Gritton } 632b38ff370SJamie Gritton if (len > MAXHOSTNAMELEN) { 633b38ff370SJamie Gritton error = ENAMETOOLONG; 634b38ff370SJamie Gritton goto done_free; 635b38ff370SJamie Gritton } 636b38ff370SJamie Gritton } 637b38ff370SJamie Gritton 638b38ff370SJamie Gritton error = vfs_getopt(opts, "host.hostname", (void **)&host, &len); 639b38ff370SJamie Gritton if (error == ENOENT) 640b38ff370SJamie Gritton host = NULL; 641b38ff370SJamie Gritton else if (error != 0) 642b38ff370SJamie Gritton goto done_free; 643b38ff370SJamie Gritton else { 64476ca6f88SJamie Gritton ch_flags |= PR_HOST; 64576ca6f88SJamie Gritton pr_flags |= PR_HOST; 646b38ff370SJamie Gritton if (len == 0 || host[len - 1] != '\0') { 647b38ff370SJamie Gritton error = EINVAL; 648b38ff370SJamie Gritton goto done_free; 649b38ff370SJamie Gritton } 650b38ff370SJamie Gritton if (len > MAXHOSTNAMELEN) { 651b38ff370SJamie Gritton error = ENAMETOOLONG; 652b38ff370SJamie Gritton goto done_free; 653b38ff370SJamie Gritton } 654b38ff370SJamie Gritton } 655b38ff370SJamie Gritton 65676ca6f88SJamie Gritton error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len); 65776ca6f88SJamie Gritton if (error == ENOENT) 65876ca6f88SJamie Gritton domain = NULL; 65976ca6f88SJamie Gritton else if (error != 0) 66076ca6f88SJamie Gritton goto done_free; 66176ca6f88SJamie Gritton else { 66276ca6f88SJamie Gritton ch_flags |= PR_HOST; 66376ca6f88SJamie Gritton pr_flags |= PR_HOST; 66476ca6f88SJamie Gritton if (len == 0 || domain[len - 1] != '\0') { 66576ca6f88SJamie Gritton error = EINVAL; 66676ca6f88SJamie Gritton goto done_free; 66776ca6f88SJamie Gritton } 66876ca6f88SJamie Gritton if (len > MAXHOSTNAMELEN) { 66976ca6f88SJamie Gritton error = ENAMETOOLONG; 67076ca6f88SJamie Gritton goto done_free; 67176ca6f88SJamie Gritton } 67276ca6f88SJamie Gritton } 67376ca6f88SJamie Gritton 67476ca6f88SJamie Gritton error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len); 67576ca6f88SJamie Gritton if (error == ENOENT) 67676ca6f88SJamie Gritton uuid = NULL; 67776ca6f88SJamie Gritton else if (error != 0) 67876ca6f88SJamie Gritton goto done_free; 67976ca6f88SJamie Gritton else { 68076ca6f88SJamie Gritton ch_flags |= PR_HOST; 68176ca6f88SJamie Gritton pr_flags |= PR_HOST; 68276ca6f88SJamie Gritton if (len == 0 || uuid[len - 1] != '\0') { 68376ca6f88SJamie Gritton error = EINVAL; 68476ca6f88SJamie Gritton goto done_free; 68576ca6f88SJamie Gritton } 68676ca6f88SJamie Gritton if (len > HOSTUUIDLEN) { 68776ca6f88SJamie Gritton error = ENAMETOOLONG; 68876ca6f88SJamie Gritton goto done_free; 68976ca6f88SJamie Gritton } 69076ca6f88SJamie Gritton } 69176ca6f88SJamie Gritton 69276ca6f88SJamie Gritton #ifdef COMPAT_IA32 69376ca6f88SJamie Gritton if (td->td_proc->p_sysent->sv_flags & SV_IA32) { 69476ca6f88SJamie Gritton uint32_t hid32; 69576ca6f88SJamie Gritton 69676ca6f88SJamie Gritton error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32)); 69776ca6f88SJamie Gritton hid = hid32; 69876ca6f88SJamie Gritton } else 69976ca6f88SJamie Gritton #endif 70076ca6f88SJamie Gritton error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid)); 70176ca6f88SJamie Gritton if (error == ENOENT) 70276ca6f88SJamie Gritton gothid = 0; 70376ca6f88SJamie Gritton else if (error != 0) 70476ca6f88SJamie Gritton goto done_free; 70576ca6f88SJamie Gritton else { 70676ca6f88SJamie Gritton gothid = 1; 70776ca6f88SJamie Gritton ch_flags |= PR_HOST; 70876ca6f88SJamie Gritton pr_flags |= PR_HOST; 70976ca6f88SJamie Gritton } 71076ca6f88SJamie Gritton 7110304c731SJamie Gritton /* This might be the second time around for this option. */ 712b38ff370SJamie Gritton #ifdef INET 713b38ff370SJamie Gritton error = vfs_getopt(opts, "ip4.addr", &op, &ip4s); 714b38ff370SJamie Gritton if (error == ENOENT) 7157cbf7213SJamie Gritton ip4s = (pr_flags & PR_IP4_DISABLE) ? 0 : -1; 716b38ff370SJamie Gritton else if (error != 0) 717b38ff370SJamie Gritton goto done_free; 718b38ff370SJamie Gritton else if (ip4s & (sizeof(*ip4) - 1)) { 719b38ff370SJamie Gritton error = EINVAL; 720b38ff370SJamie Gritton goto done_free; 7210304c731SJamie Gritton } else { 7227cbf7213SJamie Gritton ch_flags |= PR_IP4_USER | PR_IP4_DISABLE; 7237cbf7213SJamie Gritton if (ip4s == 0) 7247cbf7213SJamie Gritton pr_flags |= PR_IP4_USER | PR_IP4_DISABLE; 7257cbf7213SJamie Gritton else { 7267cbf7213SJamie Gritton pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER; 727b38ff370SJamie Gritton ip4s /= sizeof(*ip4); 728b38ff370SJamie Gritton if (ip4s > jail_max_af_ips) { 729b38ff370SJamie Gritton error = EINVAL; 730b38ff370SJamie Gritton vfs_opterror(opts, "too many IPv4 addresses"); 731b38ff370SJamie Gritton goto done_errmsg; 732b38ff370SJamie Gritton } 7330304c731SJamie Gritton if (ip4a < ip4s) { 7340304c731SJamie Gritton ip4a = ip4s; 7350304c731SJamie Gritton free(ip4, M_PRISON); 7360304c731SJamie Gritton ip4 = NULL; 7370304c731SJamie Gritton } 7380304c731SJamie Gritton if (ip4 == NULL) 7390304c731SJamie Gritton ip4 = malloc(ip4a * sizeof(*ip4), M_PRISON, 7400304c731SJamie Gritton M_WAITOK); 741b38ff370SJamie Gritton bcopy(op, ip4, ip4s * sizeof(*ip4)); 742b38ff370SJamie Gritton /* 7430304c731SJamie Gritton * IP addresses are all sorted but ip[0] to preserve 7440304c731SJamie Gritton * the primary IP address as given from userland. 7450304c731SJamie Gritton * This special IP is used for unbound outgoing 7460304c731SJamie Gritton * connections as well for "loopback" traffic. 747b38ff370SJamie Gritton */ 748b38ff370SJamie Gritton if (ip4s > 1) 749b38ff370SJamie Gritton qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4); 750b38ff370SJamie Gritton /* 7510304c731SJamie Gritton * Check for duplicate addresses and do some simple 7520304c731SJamie Gritton * zero and broadcast checks. If users give other bogus 7530304c731SJamie Gritton * addresses it is their problem. 754b38ff370SJamie Gritton * 7550304c731SJamie Gritton * We do not have to care about byte order for these 7560304c731SJamie Gritton * checks so we will do them in NBO. 757b38ff370SJamie Gritton */ 758b38ff370SJamie Gritton for (ii = 0; ii < ip4s; ii++) { 759b38ff370SJamie Gritton if (ip4[ii].s_addr == INADDR_ANY || 760b38ff370SJamie Gritton ip4[ii].s_addr == INADDR_BROADCAST) { 761b38ff370SJamie Gritton error = EINVAL; 762b38ff370SJamie Gritton goto done_free; 763b38ff370SJamie Gritton } 764b38ff370SJamie Gritton if ((ii+1) < ip4s && 765b38ff370SJamie Gritton (ip4[0].s_addr == ip4[ii+1].s_addr || 766b38ff370SJamie Gritton ip4[ii].s_addr == ip4[ii+1].s_addr)) { 767b38ff370SJamie Gritton error = EINVAL; 768b38ff370SJamie Gritton goto done_free; 769b38ff370SJamie Gritton } 770b38ff370SJamie Gritton } 771b38ff370SJamie Gritton } 7720304c731SJamie Gritton } 773b38ff370SJamie Gritton #endif 774b38ff370SJamie Gritton 775b38ff370SJamie Gritton #ifdef INET6 776b38ff370SJamie Gritton error = vfs_getopt(opts, "ip6.addr", &op, &ip6s); 777b38ff370SJamie Gritton if (error == ENOENT) 7787cbf7213SJamie Gritton ip6s = (pr_flags & PR_IP6_DISABLE) ? 0 : -1; 779b38ff370SJamie Gritton else if (error != 0) 780b38ff370SJamie Gritton goto done_free; 781b38ff370SJamie Gritton else if (ip6s & (sizeof(*ip6) - 1)) { 782b38ff370SJamie Gritton error = EINVAL; 783b38ff370SJamie Gritton goto done_free; 7840304c731SJamie Gritton } else { 7857cbf7213SJamie Gritton ch_flags |= PR_IP6_USER | PR_IP6_DISABLE; 7867cbf7213SJamie Gritton if (ip6s == 0) 7877cbf7213SJamie Gritton pr_flags |= PR_IP6_USER | PR_IP6_DISABLE; 7887cbf7213SJamie Gritton else { 7897cbf7213SJamie Gritton pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER; 790b38ff370SJamie Gritton ip6s /= sizeof(*ip6); 791b38ff370SJamie Gritton if (ip6s > jail_max_af_ips) { 792b38ff370SJamie Gritton error = EINVAL; 793b38ff370SJamie Gritton vfs_opterror(opts, "too many IPv6 addresses"); 794b38ff370SJamie Gritton goto done_errmsg; 795b38ff370SJamie Gritton } 7960304c731SJamie Gritton if (ip6a < ip6s) { 7970304c731SJamie Gritton ip6a = ip6s; 7980304c731SJamie Gritton free(ip6, M_PRISON); 7990304c731SJamie Gritton ip6 = NULL; 8000304c731SJamie Gritton } 8010304c731SJamie Gritton if (ip6 == NULL) 8020304c731SJamie Gritton ip6 = malloc(ip6a * sizeof(*ip6), M_PRISON, 8030304c731SJamie Gritton M_WAITOK); 804b38ff370SJamie Gritton bcopy(op, ip6, ip6s * sizeof(*ip6)); 805b38ff370SJamie Gritton if (ip6s > 1) 806b38ff370SJamie Gritton qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6); 807b38ff370SJamie Gritton for (ii = 0; ii < ip6s; ii++) { 8080304c731SJamie Gritton if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) { 809b38ff370SJamie Gritton error = EINVAL; 810b38ff370SJamie Gritton goto done_free; 811b38ff370SJamie Gritton } 812b38ff370SJamie Gritton if ((ii+1) < ip6s && 813b38ff370SJamie Gritton (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) || 814b38ff370SJamie Gritton IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1]))) 815b38ff370SJamie Gritton { 816b38ff370SJamie Gritton error = EINVAL; 817b38ff370SJamie Gritton goto done_free; 818b38ff370SJamie Gritton } 819b38ff370SJamie Gritton } 820b38ff370SJamie Gritton } 8210304c731SJamie Gritton } 822b38ff370SJamie Gritton #endif 823b38ff370SJamie Gritton 824bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6)) 825bdfc8cc4SJamie Gritton if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 826bdfc8cc4SJamie Gritton error = EINVAL; 827bdfc8cc4SJamie Gritton vfs_opterror(opts, 828bdfc8cc4SJamie Gritton "vnet jails cannot have IP address restrictions"); 829bdfc8cc4SJamie Gritton goto done_errmsg; 830bdfc8cc4SJamie Gritton } 831bdfc8cc4SJamie Gritton #endif 832bdfc8cc4SJamie Gritton 833b38ff370SJamie Gritton root = NULL; 834b38ff370SJamie Gritton error = vfs_getopt(opts, "path", (void **)&path, &len); 835b38ff370SJamie Gritton if (error == ENOENT) 836b38ff370SJamie Gritton path = NULL; 837b38ff370SJamie Gritton else if (error != 0) 838b38ff370SJamie Gritton goto done_free; 839b38ff370SJamie Gritton else { 840b38ff370SJamie Gritton if (flags & JAIL_UPDATE) { 841b38ff370SJamie Gritton error = EINVAL; 842b38ff370SJamie Gritton vfs_opterror(opts, 843b38ff370SJamie Gritton "path cannot be changed after creation"); 844b38ff370SJamie Gritton goto done_errmsg; 845b38ff370SJamie Gritton } 846b38ff370SJamie Gritton if (len == 0 || path[len - 1] != '\0') { 847b38ff370SJamie Gritton error = EINVAL; 848b38ff370SJamie Gritton goto done_free; 849b38ff370SJamie Gritton } 850b38ff370SJamie Gritton if (len < 2 || (len == 2 && path[0] == '/')) 851b38ff370SJamie Gritton path = NULL; 852b38ff370SJamie Gritton else { 8530304c731SJamie Gritton /* Leave room for a real-root full pathname. */ 8540304c731SJamie Gritton if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/") 8550304c731SJamie Gritton ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) { 8560304c731SJamie Gritton error = ENAMETOOLONG; 8570304c731SJamie Gritton goto done_free; 8580304c731SJamie Gritton } 859b38ff370SJamie Gritton NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_SYSSPACE, 860b38ff370SJamie Gritton path, td); 861b38ff370SJamie Gritton error = namei(&nd); 862b38ff370SJamie Gritton if (error) 863b38ff370SJamie Gritton goto done_free; 864b38ff370SJamie Gritton vfslocked = NDHASGIANT(&nd); 865b38ff370SJamie Gritton root = nd.ni_vp; 866b38ff370SJamie Gritton NDFREE(&nd, NDF_ONLY_PNBUF); 867b38ff370SJamie Gritton if (root->v_type != VDIR) { 868b38ff370SJamie Gritton error = ENOTDIR; 869b38ff370SJamie Gritton vrele(root); 870b38ff370SJamie Gritton VFS_UNLOCK_GIANT(vfslocked); 871b38ff370SJamie Gritton goto done_free; 872b38ff370SJamie Gritton } 873b38ff370SJamie Gritton VFS_UNLOCK_GIANT(vfslocked); 874b38ff370SJamie Gritton } 875b38ff370SJamie Gritton } 876b38ff370SJamie Gritton 877b38ff370SJamie Gritton /* 878b38ff370SJamie Gritton * Grab the allprison lock before letting modules check their 879b38ff370SJamie Gritton * parameters. Once we have it, do not let go so we'll have a 880b38ff370SJamie Gritton * consistent view of the OSD list. 881b38ff370SJamie Gritton */ 882b38ff370SJamie Gritton sx_xlock(&allprison_lock); 883b38ff370SJamie Gritton error = osd_jail_call(NULL, PR_METHOD_CHECK, opts); 884b38ff370SJamie Gritton if (error) 885b38ff370SJamie Gritton goto done_unlock_list; 886b38ff370SJamie Gritton 887b38ff370SJamie Gritton /* By now, all parameters should have been noted. */ 888b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 889b38ff370SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 890b38ff370SJamie Gritton error = EINVAL; 891b38ff370SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 892b38ff370SJamie Gritton goto done_unlock_list; 893b38ff370SJamie Gritton } 894b38ff370SJamie Gritton } 895b38ff370SJamie Gritton 896b38ff370SJamie Gritton /* 897b38ff370SJamie Gritton * See if we are creating a new record or updating an existing one. 898b38ff370SJamie Gritton * This abuses the file error codes ENOENT and EEXIST. 899b38ff370SJamie Gritton */ 900b38ff370SJamie Gritton cuflags = flags & (JAIL_CREATE | JAIL_UPDATE); 901b38ff370SJamie Gritton if (!cuflags) { 902b38ff370SJamie Gritton error = EINVAL; 903b38ff370SJamie Gritton vfs_opterror(opts, "no valid operation (create or update)"); 904b38ff370SJamie Gritton goto done_unlock_list; 905b38ff370SJamie Gritton } 906b38ff370SJamie Gritton pr = NULL; 907b38ff370SJamie Gritton if (jid != 0) { 9080304c731SJamie Gritton /* 9090304c731SJamie Gritton * See if a requested jid already exists. There is an 9100304c731SJamie Gritton * information leak here if the jid exists but is not within 9110304c731SJamie Gritton * the caller's jail hierarchy. Jail creators will get EEXIST 9120304c731SJamie Gritton * even though they cannot see the jail, and CREATE | UPDATE 9130304c731SJamie Gritton * will return ENOENT which is not normally a valid error. 9140304c731SJamie Gritton */ 915b38ff370SJamie Gritton if (jid < 0) { 916b38ff370SJamie Gritton error = EINVAL; 917b38ff370SJamie Gritton vfs_opterror(opts, "negative jid"); 918b38ff370SJamie Gritton goto done_unlock_list; 919b38ff370SJamie Gritton } 920b38ff370SJamie Gritton pr = prison_find(jid); 921b38ff370SJamie Gritton if (pr != NULL) { 9220304c731SJamie Gritton ppr = pr->pr_parent; 923b38ff370SJamie Gritton /* Create: jid must not exist. */ 924b38ff370SJamie Gritton if (cuflags == JAIL_CREATE) { 925b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 926b38ff370SJamie Gritton error = EEXIST; 927b38ff370SJamie Gritton vfs_opterror(opts, "jail %d already exists", 928b38ff370SJamie Gritton jid); 929b38ff370SJamie Gritton goto done_unlock_list; 930b38ff370SJamie Gritton } 9310304c731SJamie Gritton if (!prison_ischild(mypr, pr)) { 9320304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 9330304c731SJamie Gritton pr = NULL; 9340304c731SJamie Gritton } else if (pr->pr_uref == 0) { 935b38ff370SJamie Gritton if (!(flags & JAIL_DYING)) { 936b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 937b38ff370SJamie Gritton error = ENOENT; 938b38ff370SJamie Gritton vfs_opterror(opts, "jail %d is dying", 939b38ff370SJamie Gritton jid); 940b38ff370SJamie Gritton goto done_unlock_list; 941b38ff370SJamie Gritton } else if ((flags & JAIL_ATTACH) || 942b38ff370SJamie Gritton (pr_flags & PR_PERSIST)) { 943b38ff370SJamie Gritton /* 944b38ff370SJamie Gritton * A dying jail might be resurrected 945b38ff370SJamie Gritton * (via attach or persist), but first 946b38ff370SJamie Gritton * it must determine if another jail 947b38ff370SJamie Gritton * has claimed its name. Accomplish 948b38ff370SJamie Gritton * this by implicitly re-setting the 949b38ff370SJamie Gritton * name. 950b38ff370SJamie Gritton */ 951b38ff370SJamie Gritton if (name == NULL) 9520304c731SJamie Gritton name = prison_name(mypr, pr); 953b38ff370SJamie Gritton } 954b38ff370SJamie Gritton } 955b38ff370SJamie Gritton } 956b38ff370SJamie Gritton if (pr == NULL) { 957b38ff370SJamie Gritton /* Update: jid must exist. */ 958b38ff370SJamie Gritton if (cuflags == JAIL_UPDATE) { 959b38ff370SJamie Gritton error = ENOENT; 960b38ff370SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 961b38ff370SJamie Gritton goto done_unlock_list; 962b38ff370SJamie Gritton } 963b38ff370SJamie Gritton } 964b38ff370SJamie Gritton } 965b38ff370SJamie Gritton /* 966b38ff370SJamie Gritton * If the caller provided a name, look for a jail by that name. 967b38ff370SJamie Gritton * This has different semantics for creates and updates keyed by jid 968b38ff370SJamie Gritton * (where the name must not already exist in a different jail), 969b38ff370SJamie Gritton * and updates keyed by the name itself (where the name must exist 970b38ff370SJamie Gritton * because that is the jail being updated). 971b38ff370SJamie Gritton */ 972b38ff370SJamie Gritton if (name != NULL) { 9730304c731SJamie Gritton p = strrchr(name, '.'); 9740304c731SJamie Gritton if (p != NULL) { 9750304c731SJamie Gritton /* 9760304c731SJamie Gritton * This is a hierarchical name. Split it into the 9770304c731SJamie Gritton * parent and child names, and make sure the parent 9780304c731SJamie Gritton * exists or matches an already found jail. 9790304c731SJamie Gritton */ 9800304c731SJamie Gritton *p = '\0'; 9810304c731SJamie Gritton if (pr != NULL) { 9820304c731SJamie Gritton if (strncmp(name, ppr->pr_name, p - name) || 9830304c731SJamie Gritton ppr->pr_name[p - name] != '\0') { 9840304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 9850304c731SJamie Gritton error = EINVAL; 9860304c731SJamie Gritton vfs_opterror(opts, 9870304c731SJamie Gritton "cannot change jail's parent"); 9880304c731SJamie Gritton goto done_unlock_list; 9890304c731SJamie Gritton } 9900304c731SJamie Gritton } else { 9910304c731SJamie Gritton ppr = prison_find_name(mypr, name); 9920304c731SJamie Gritton if (ppr == NULL) { 9930304c731SJamie Gritton error = ENOENT; 9940304c731SJamie Gritton vfs_opterror(opts, 9950304c731SJamie Gritton "jail \"%s\" not found", name); 9960304c731SJamie Gritton goto done_unlock_list; 9970304c731SJamie Gritton } 9980304c731SJamie Gritton mtx_unlock(&ppr->pr_mtx); 9990304c731SJamie Gritton } 10000304c731SJamie Gritton name = p + 1; 10010304c731SJamie Gritton } 1002b38ff370SJamie Gritton if (name[0] != '\0') { 10030304c731SJamie Gritton namelen = 10040304c731SJamie Gritton (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 1005b38ff370SJamie Gritton name_again: 10060304c731SJamie Gritton deadpr = NULL; 10070304c731SJamie Gritton FOREACH_PRISON_CHILD(ppr, tpr) { 1008b38ff370SJamie Gritton if (tpr != pr && tpr->pr_ref > 0 && 10090304c731SJamie Gritton !strcmp(tpr->pr_name + namelen, name)) { 1010b38ff370SJamie Gritton if (pr == NULL && 1011b38ff370SJamie Gritton cuflags != JAIL_CREATE) { 1012b38ff370SJamie Gritton mtx_lock(&tpr->pr_mtx); 1013b38ff370SJamie Gritton if (tpr->pr_ref > 0) { 1014b38ff370SJamie Gritton /* 1015b38ff370SJamie Gritton * Use this jail 1016b38ff370SJamie Gritton * for updates. 1017b38ff370SJamie Gritton */ 1018b38ff370SJamie Gritton if (tpr->pr_uref > 0) { 1019b38ff370SJamie Gritton pr = tpr; 1020b38ff370SJamie Gritton break; 1021b38ff370SJamie Gritton } 1022b38ff370SJamie Gritton deadpr = tpr; 1023b38ff370SJamie Gritton } 1024b38ff370SJamie Gritton mtx_unlock(&tpr->pr_mtx); 1025b38ff370SJamie Gritton } else if (tpr->pr_uref > 0) { 1026b38ff370SJamie Gritton /* 1027b38ff370SJamie Gritton * Create, or update(jid): 1028b38ff370SJamie Gritton * name must not exist in an 10290304c731SJamie Gritton * active sibling jail. 1030b38ff370SJamie Gritton */ 1031b38ff370SJamie Gritton error = EEXIST; 1032b38ff370SJamie Gritton if (pr != NULL) 1033b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1034b38ff370SJamie Gritton vfs_opterror(opts, 1035b38ff370SJamie Gritton "jail \"%s\" already exists", 1036b38ff370SJamie Gritton name); 1037b38ff370SJamie Gritton goto done_unlock_list; 1038b38ff370SJamie Gritton } 1039b38ff370SJamie Gritton } 1040b38ff370SJamie Gritton } 1041b38ff370SJamie Gritton /* If no active jail is found, use a dying one. */ 1042b38ff370SJamie Gritton if (deadpr != NULL && pr == NULL) { 1043b38ff370SJamie Gritton if (flags & JAIL_DYING) { 1044b38ff370SJamie Gritton mtx_lock(&deadpr->pr_mtx); 1045b38ff370SJamie Gritton if (deadpr->pr_ref == 0) { 1046b38ff370SJamie Gritton mtx_unlock(&deadpr->pr_mtx); 1047b38ff370SJamie Gritton goto name_again; 1048b38ff370SJamie Gritton } 1049b38ff370SJamie Gritton pr = deadpr; 1050b38ff370SJamie Gritton } else if (cuflags == JAIL_UPDATE) { 1051b38ff370SJamie Gritton error = ENOENT; 1052b38ff370SJamie Gritton vfs_opterror(opts, 1053b38ff370SJamie Gritton "jail \"%s\" is dying", name); 1054b38ff370SJamie Gritton goto done_unlock_list; 1055b38ff370SJamie Gritton } 1056b38ff370SJamie Gritton } 1057b38ff370SJamie Gritton /* Update: name must exist if no jid. */ 1058b38ff370SJamie Gritton else if (cuflags == JAIL_UPDATE && pr == NULL) { 1059b38ff370SJamie Gritton error = ENOENT; 1060b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", 1061b38ff370SJamie Gritton name); 1062b38ff370SJamie Gritton goto done_unlock_list; 1063b38ff370SJamie Gritton } 1064b38ff370SJamie Gritton } 1065b38ff370SJamie Gritton } 1066b38ff370SJamie Gritton /* Update: must provide a jid or name. */ 1067b38ff370SJamie Gritton else if (cuflags == JAIL_UPDATE && pr == NULL) { 1068b38ff370SJamie Gritton error = ENOENT; 1069b38ff370SJamie Gritton vfs_opterror(opts, "update specified no jail"); 1070b38ff370SJamie Gritton goto done_unlock_list; 1071b38ff370SJamie Gritton } 1072b38ff370SJamie Gritton 1073b38ff370SJamie Gritton /* If there's no prison to update, create a new one and link it in. */ 1074b38ff370SJamie Gritton if (pr == NULL) { 1075b97457e2SJamie Gritton for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent) 1076b97457e2SJamie Gritton if (tpr->pr_childcount >= tpr->pr_childmax) { 1077b97457e2SJamie Gritton error = EPERM; 1078b97457e2SJamie Gritton vfs_opterror(opts, "prison limit exceeded"); 1079b97457e2SJamie Gritton goto done_unlock_list; 1080b97457e2SJamie Gritton } 1081b38ff370SJamie Gritton created = 1; 10820304c731SJamie Gritton mtx_lock(&ppr->pr_mtx); 10830304c731SJamie Gritton if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) { 10840304c731SJamie Gritton mtx_unlock(&ppr->pr_mtx); 10850304c731SJamie Gritton error = ENOENT; 10860304c731SJamie Gritton vfs_opterror(opts, "parent jail went away!"); 10870304c731SJamie Gritton goto done_unlock_list; 10880304c731SJamie Gritton } 10890304c731SJamie Gritton ppr->pr_ref++; 10900304c731SJamie Gritton ppr->pr_uref++; 10910304c731SJamie Gritton mtx_unlock(&ppr->pr_mtx); 1092b38ff370SJamie Gritton pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO); 1093b38ff370SJamie Gritton if (jid == 0) { 1094b38ff370SJamie Gritton /* Find the next free jid. */ 1095b38ff370SJamie Gritton jid = lastprid + 1; 1096b38ff370SJamie Gritton findnext: 1097b38ff370SJamie Gritton if (jid == JAIL_MAX) 1098b38ff370SJamie Gritton jid = 1; 1099b38ff370SJamie Gritton TAILQ_FOREACH(tpr, &allprison, pr_list) { 1100b38ff370SJamie Gritton if (tpr->pr_id < jid) 1101b38ff370SJamie Gritton continue; 1102b38ff370SJamie Gritton if (tpr->pr_id > jid || tpr->pr_ref == 0) { 1103b38ff370SJamie Gritton TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1104b38ff370SJamie Gritton break; 1105b38ff370SJamie Gritton } 1106b38ff370SJamie Gritton if (jid == lastprid) { 1107b38ff370SJamie Gritton error = EAGAIN; 1108b38ff370SJamie Gritton vfs_opterror(opts, 1109b38ff370SJamie Gritton "no available jail IDs"); 1110b38ff370SJamie Gritton free(pr, M_PRISON); 11110304c731SJamie Gritton prison_deref(ppr, PD_DEREF | 11120304c731SJamie Gritton PD_DEUREF | PD_LIST_XLOCKED); 11130304c731SJamie Gritton goto done_releroot; 1114b38ff370SJamie Gritton } 1115b38ff370SJamie Gritton jid++; 1116b38ff370SJamie Gritton goto findnext; 1117b38ff370SJamie Gritton } 1118b38ff370SJamie Gritton lastprid = jid; 1119b38ff370SJamie Gritton } else { 1120b38ff370SJamie Gritton /* 1121b38ff370SJamie Gritton * The jail already has a jid (that did not yet exist), 1122b38ff370SJamie Gritton * so just find where to insert it. 1123b38ff370SJamie Gritton */ 1124b38ff370SJamie Gritton TAILQ_FOREACH(tpr, &allprison, pr_list) 1125b38ff370SJamie Gritton if (tpr->pr_id >= jid) { 1126b38ff370SJamie Gritton TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1127b38ff370SJamie Gritton break; 1128b38ff370SJamie Gritton } 1129b38ff370SJamie Gritton } 1130b38ff370SJamie Gritton if (tpr == NULL) 1131b38ff370SJamie Gritton TAILQ_INSERT_TAIL(&allprison, pr, pr_list); 11320304c731SJamie Gritton LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling); 11330304c731SJamie Gritton for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 1134b97457e2SJamie Gritton tpr->pr_childcount++; 1135b38ff370SJamie Gritton 11360304c731SJamie Gritton pr->pr_parent = ppr; 1137b38ff370SJamie Gritton pr->pr_id = jid; 11380304c731SJamie Gritton 11390304c731SJamie Gritton /* Set some default values, and inherit some from the parent. */ 1140b38ff370SJamie Gritton if (name == NULL) 1141b38ff370SJamie Gritton name = ""; 1142b38ff370SJamie Gritton if (path == NULL) { 1143b38ff370SJamie Gritton path = "/"; 11440304c731SJamie Gritton root = mypr->pr_root; 1145b38ff370SJamie Gritton vref(root); 1146b38ff370SJamie Gritton } 11478986e3a0SJamie Gritton strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN); 11488986e3a0SJamie Gritton pr->pr_flags |= PR_HOST; 1149bdfc8cc4SJamie Gritton #if defined(INET) || defined(INET6) 1150bdfc8cc4SJamie Gritton #ifdef VIMAGE 1151bdfc8cc4SJamie Gritton if (!(pr_flags & PR_VNET)) 1152bdfc8cc4SJamie Gritton #endif 1153bdfc8cc4SJamie Gritton { 11540304c731SJamie Gritton #ifdef INET 11558986e3a0SJamie Gritton pr->pr_flags |= PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE; 11560304c731SJamie Gritton #endif 11570304c731SJamie Gritton #ifdef INET6 11588986e3a0SJamie Gritton pr->pr_flags |= PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE; 11590304c731SJamie Gritton #endif 1160bdfc8cc4SJamie Gritton } 1161bdfc8cc4SJamie Gritton #endif 11620304c731SJamie Gritton pr->pr_securelevel = ppr->pr_securelevel; 11630304c731SJamie Gritton pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow; 11640304c731SJamie Gritton pr->pr_enforce_statfs = ppr->pr_enforce_statfs; 1165b38ff370SJamie Gritton 11660304c731SJamie Gritton LIST_INIT(&pr->pr_children); 11670304c731SJamie Gritton mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK); 1168b38ff370SJamie Gritton 1169679e1390SJamie Gritton #ifdef VIMAGE 1170679e1390SJamie Gritton /* Allocate a new vnet if specified. */ 1171679e1390SJamie Gritton pr->pr_vnet = (pr_flags & PR_VNET) 1172679e1390SJamie Gritton ? vnet_alloc() : ppr->pr_vnet; 1173679e1390SJamie Gritton #endif 1174b38ff370SJamie Gritton /* 1175b38ff370SJamie Gritton * Allocate a dedicated cpuset for each jail. 1176b38ff370SJamie Gritton * Unlike other initial settings, this may return an erorr. 1177b38ff370SJamie Gritton */ 11780304c731SJamie Gritton error = cpuset_create_root(ppr, &pr->pr_cpuset); 1179b38ff370SJamie Gritton if (error) { 1180b38ff370SJamie Gritton prison_deref(pr, PD_LIST_XLOCKED); 1181b38ff370SJamie Gritton goto done_releroot; 1182b38ff370SJamie Gritton } 1183b38ff370SJamie Gritton 1184b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1185b38ff370SJamie Gritton /* 1186b38ff370SJamie Gritton * New prisons do not yet have a reference, because we do not 1187b38ff370SJamie Gritton * want other to see the incomplete prison once the 1188b38ff370SJamie Gritton * allprison_lock is downgraded. 1189b38ff370SJamie Gritton */ 1190b38ff370SJamie Gritton } else { 1191b38ff370SJamie Gritton created = 0; 1192bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6)) 1193bdfc8cc4SJamie Gritton if ((pr->pr_flags & PR_VNET) && 1194bdfc8cc4SJamie Gritton (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 1195bdfc8cc4SJamie Gritton error = EINVAL; 1196bdfc8cc4SJamie Gritton vfs_opterror(opts, 1197bdfc8cc4SJamie Gritton "vnet jails cannot have IP address restrictions"); 1198bdfc8cc4SJamie Gritton goto done_deref_locked; 1199bdfc8cc4SJamie Gritton } 1200bdfc8cc4SJamie Gritton #endif 1201b38ff370SJamie Gritton /* 1202b38ff370SJamie Gritton * Grab a reference for existing prisons, to ensure they 1203b38ff370SJamie Gritton * continue to exist for the duration of the call. 1204b38ff370SJamie Gritton */ 1205b38ff370SJamie Gritton pr->pr_ref++; 1206b38ff370SJamie Gritton } 1207b38ff370SJamie Gritton 1208b38ff370SJamie Gritton /* Do final error checking before setting anything. */ 12090304c731SJamie Gritton if (gotslevel) { 12100304c731SJamie Gritton if (slevel < ppr->pr_securelevel) { 12110304c731SJamie Gritton error = EPERM; 12120304c731SJamie Gritton goto done_deref_locked; 12130304c731SJamie Gritton } 12140304c731SJamie Gritton } 1215b97457e2SJamie Gritton if (gotchildmax) { 1216b97457e2SJamie Gritton if (childmax >= ppr->pr_childmax) { 1217b97457e2SJamie Gritton error = EPERM; 1218b97457e2SJamie Gritton goto done_deref_locked; 1219b97457e2SJamie Gritton } 1220b97457e2SJamie Gritton } 12210304c731SJamie Gritton if (gotenforce) { 12220304c731SJamie Gritton if (enforce < ppr->pr_enforce_statfs) { 12230304c731SJamie Gritton error = EPERM; 12240304c731SJamie Gritton goto done_deref_locked; 12250304c731SJamie Gritton } 12260304c731SJamie Gritton } 1227b38ff370SJamie Gritton #ifdef INET 12280304c731SJamie Gritton if (ch_flags & PR_IP4_USER) { 12290304c731SJamie Gritton if (ppr->pr_flags & PR_IP4) { 12300304c731SJamie Gritton if (!(pr_flags & PR_IP4_USER)) { 1231b38ff370SJamie Gritton /* 12320304c731SJamie Gritton * Silently ignore attempts to make the IP 12330304c731SJamie Gritton * addresses unrestricted when the parent is 12340304c731SJamie Gritton * restricted; in other words, interpret 12350304c731SJamie Gritton * "unrestricted" as "as unrestricted as 12360304c731SJamie Gritton * possible". 1237b38ff370SJamie Gritton */ 12380304c731SJamie Gritton ip4s = ppr->pr_ip4s; 12390304c731SJamie Gritton if (ip4s == 0) { 12400304c731SJamie Gritton free(ip4, M_PRISON); 12410304c731SJamie Gritton ip4 = NULL; 12420304c731SJamie Gritton } else if (ip4s <= ip4a) { 12430304c731SJamie Gritton /* Inherit the parent's address(es). */ 12440304c731SJamie Gritton bcopy(ppr->pr_ip4, ip4, 12450304c731SJamie Gritton ip4s * sizeof(*ip4)); 12460304c731SJamie Gritton } else { 12470304c731SJamie Gritton /* 12480304c731SJamie Gritton * There's no room for the parent's 12490304c731SJamie Gritton * address list. Allocate some more. 12500304c731SJamie Gritton */ 12510304c731SJamie Gritton ip4a = ip4s; 12520304c731SJamie Gritton free(ip4, M_PRISON); 12530304c731SJamie Gritton ip4 = malloc(ip4a * sizeof(*ip4), 12540304c731SJamie Gritton M_PRISON, M_NOWAIT); 12550304c731SJamie Gritton if (ip4 != NULL) 12560304c731SJamie Gritton bcopy(ppr->pr_ip4, ip4, 12570304c731SJamie Gritton ip4s * sizeof(*ip4)); 12580304c731SJamie Gritton else { 12590304c731SJamie Gritton /* Allocation failed without 12600304c731SJamie Gritton * sleeping. Unlocking the 12610304c731SJamie Gritton * prison now will invalidate 12620304c731SJamie Gritton * some checks and prematurely 12630304c731SJamie Gritton * show an unfinished new jail. 12640304c731SJamie Gritton * So let go of everything and 12650304c731SJamie Gritton * start over. 12660304c731SJamie Gritton */ 12670304c731SJamie Gritton prison_deref(pr, created 12680304c731SJamie Gritton ? PD_LOCKED | 12690304c731SJamie Gritton PD_LIST_XLOCKED 12700304c731SJamie Gritton : PD_DEREF | PD_LOCKED | 12710304c731SJamie Gritton PD_LIST_XLOCKED); 12720304c731SJamie Gritton if (root != NULL) { 12730304c731SJamie Gritton vfslocked = 12740304c731SJamie Gritton VFS_LOCK_GIANT( 12750304c731SJamie Gritton root->v_mount); 12760304c731SJamie Gritton vrele(root); 12770304c731SJamie Gritton VFS_UNLOCK_GIANT( 12780304c731SJamie Gritton vfslocked); 12790304c731SJamie Gritton } 12800304c731SJamie Gritton ip4 = malloc(ip4a * 12810304c731SJamie Gritton sizeof(*ip4), M_PRISON, 12820304c731SJamie Gritton M_WAITOK); 12830304c731SJamie Gritton goto again; 12840304c731SJamie Gritton } 12850304c731SJamie Gritton } 12860304c731SJamie Gritton } else if (ip4s > 0) { 12870304c731SJamie Gritton /* 12880304c731SJamie Gritton * Make sure the new set of IP addresses is a 12890304c731SJamie Gritton * subset of the parent's list. Don't worry 12900304c731SJamie Gritton * about the parent being unlocked, as any 12910304c731SJamie Gritton * setting is done with allprison_lock held. 12920304c731SJamie Gritton */ 12930304c731SJamie Gritton for (ij = 0; ij < ppr->pr_ip4s; ij++) 12940304c731SJamie Gritton if (ip4[0].s_addr == 12950304c731SJamie Gritton ppr->pr_ip4[ij].s_addr) 12960304c731SJamie Gritton break; 12970304c731SJamie Gritton if (ij == ppr->pr_ip4s) { 12980304c731SJamie Gritton error = EPERM; 12990304c731SJamie Gritton goto done_deref_locked; 13000304c731SJamie Gritton } 13010304c731SJamie Gritton if (ip4s > 1) { 13020304c731SJamie Gritton for (ii = ij = 1; ii < ip4s; ii++) { 13030304c731SJamie Gritton if (ip4[ii].s_addr == 13040304c731SJamie Gritton ppr->pr_ip4[0].s_addr) 1305b38ff370SJamie Gritton continue; 13060304c731SJamie Gritton for (; ij < ppr->pr_ip4s; ij++) 13070304c731SJamie Gritton if (ip4[ii].s_addr == 13080304c731SJamie Gritton ppr->pr_ip4[ij].s_addr) 13090304c731SJamie Gritton break; 13100304c731SJamie Gritton if (ij == ppr->pr_ip4s) 13110304c731SJamie Gritton break; 13120304c731SJamie Gritton } 13130304c731SJamie Gritton if (ij == ppr->pr_ip4s) { 13140304c731SJamie Gritton error = EPERM; 13150304c731SJamie Gritton goto done_deref_locked; 13160304c731SJamie Gritton } 13170304c731SJamie Gritton } 13180304c731SJamie Gritton } 13190304c731SJamie Gritton } 13200304c731SJamie Gritton if (ip4s > 0) { 13210304c731SJamie Gritton /* 13220304c731SJamie Gritton * Check for conflicting IP addresses. We permit them 13230304c731SJamie Gritton * if there is no more than one IP on each jail. If 13240304c731SJamie Gritton * there is a duplicate on a jail with more than one 13250304c731SJamie Gritton * IP stop checking and return error. 13260304c731SJamie Gritton */ 1327bdfc8cc4SJamie Gritton tppr = ppr; 1328bdfc8cc4SJamie Gritton #ifdef VIMAGE 1329bdfc8cc4SJamie Gritton for (; tppr != &prison0; tppr = tppr->pr_parent) 1330bdfc8cc4SJamie Gritton if (tppr->pr_flags & PR_VNET) 1331bdfc8cc4SJamie Gritton break; 1332bdfc8cc4SJamie Gritton #endif 1333bdfc8cc4SJamie Gritton FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1334bdfc8cc4SJamie Gritton if (tpr == pr || 1335bdfc8cc4SJamie Gritton #ifdef VIMAGE 1336bdfc8cc4SJamie Gritton (tpr != tppr && 1337bdfc8cc4SJamie Gritton (tpr->pr_flags & PR_VNET)) || 1338bdfc8cc4SJamie Gritton #endif 1339bdfc8cc4SJamie Gritton tpr->pr_uref == 0) { 13400304c731SJamie Gritton descend = 0; 13410304c731SJamie Gritton continue; 13420304c731SJamie Gritton } 13430304c731SJamie Gritton if (!(tpr->pr_flags & PR_IP4_USER)) 13440304c731SJamie Gritton continue; 13450304c731SJamie Gritton descend = 0; 13460304c731SJamie Gritton if (tpr->pr_ip4 == NULL || 13470304c731SJamie Gritton (ip4s == 1 && tpr->pr_ip4s == 1)) 13480304c731SJamie Gritton continue; 13490304c731SJamie Gritton for (ii = 0; ii < ip4s; ii++) { 1350b38ff370SJamie Gritton if (_prison_check_ip4(tpr, 1351b38ff370SJamie Gritton &ip4[ii]) == 0) { 13520304c731SJamie Gritton error = EADDRINUSE; 1353b38ff370SJamie Gritton vfs_opterror(opts, 1354b38ff370SJamie Gritton "IPv4 addresses clash"); 1355b38ff370SJamie Gritton goto done_deref_locked; 1356b38ff370SJamie Gritton } 13570304c731SJamie Gritton } 13580304c731SJamie Gritton } 13590304c731SJamie Gritton } 13600304c731SJamie Gritton } 1361b38ff370SJamie Gritton #endif 1362b38ff370SJamie Gritton #ifdef INET6 13630304c731SJamie Gritton if (ch_flags & PR_IP6_USER) { 13640304c731SJamie Gritton if (ppr->pr_flags & PR_IP6) { 13650304c731SJamie Gritton if (!(pr_flags & PR_IP6_USER)) { 13660304c731SJamie Gritton /* 13670304c731SJamie Gritton * Silently ignore attempts to make the IP 13680304c731SJamie Gritton * addresses unrestricted when the parent is 13690304c731SJamie Gritton * restricted. 13700304c731SJamie Gritton */ 13710304c731SJamie Gritton ip6s = ppr->pr_ip6s; 13720304c731SJamie Gritton if (ip6s == 0) { 13730304c731SJamie Gritton free(ip6, M_PRISON); 13740304c731SJamie Gritton ip6 = NULL; 13750304c731SJamie Gritton } else if (ip6s <= ip6a) { 13760304c731SJamie Gritton /* Inherit the parent's address(es). */ 13770304c731SJamie Gritton bcopy(ppr->pr_ip6, ip6, 13780304c731SJamie Gritton ip6s * sizeof(*ip6)); 13790304c731SJamie Gritton } else { 13800304c731SJamie Gritton /* 13810304c731SJamie Gritton * There's no room for the parent's 13820304c731SJamie Gritton * address list. 13830304c731SJamie Gritton */ 13840304c731SJamie Gritton ip6a = ip6s; 13850304c731SJamie Gritton free(ip6, M_PRISON); 13860304c731SJamie Gritton ip6 = malloc(ip6a * sizeof(*ip6), 13870304c731SJamie Gritton M_PRISON, M_NOWAIT); 13880304c731SJamie Gritton if (ip6 != NULL) 13890304c731SJamie Gritton bcopy(ppr->pr_ip6, ip6, 13900304c731SJamie Gritton ip6s * sizeof(*ip6)); 13910304c731SJamie Gritton else { 13920304c731SJamie Gritton prison_deref(pr, created 13930304c731SJamie Gritton ? PD_LOCKED | 13940304c731SJamie Gritton PD_LIST_XLOCKED 13950304c731SJamie Gritton : PD_DEREF | PD_LOCKED | 13960304c731SJamie Gritton PD_LIST_XLOCKED); 13970304c731SJamie Gritton if (root != NULL) { 13980304c731SJamie Gritton vfslocked = 13990304c731SJamie Gritton VFS_LOCK_GIANT( 14000304c731SJamie Gritton root->v_mount); 14010304c731SJamie Gritton vrele(root); 14020304c731SJamie Gritton VFS_UNLOCK_GIANT( 14030304c731SJamie Gritton vfslocked); 14040304c731SJamie Gritton } 14050304c731SJamie Gritton ip6 = malloc(ip6a * 14060304c731SJamie Gritton sizeof(*ip6), M_PRISON, 14070304c731SJamie Gritton M_WAITOK); 14080304c731SJamie Gritton goto again; 14090304c731SJamie Gritton } 14100304c731SJamie Gritton } 14110304c731SJamie Gritton } else if (ip6s > 0) { 14120304c731SJamie Gritton /* 14130304c731SJamie Gritton * Make sure the new set of IP addresses is a 14140304c731SJamie Gritton * subset of the parent's list. 14150304c731SJamie Gritton */ 14160304c731SJamie Gritton for (ij = 0; ij < ppr->pr_ip6s; ij++) 14170304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL(&ip6[0], 14180304c731SJamie Gritton &ppr->pr_ip6[ij])) 14190304c731SJamie Gritton break; 14200304c731SJamie Gritton if (ij == ppr->pr_ip6s) { 14210304c731SJamie Gritton error = EPERM; 14220304c731SJamie Gritton goto done_deref_locked; 14230304c731SJamie Gritton } 14240304c731SJamie Gritton if (ip6s > 1) { 14250304c731SJamie Gritton for (ii = ij = 1; ii < ip6s; ii++) { 14260304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL(&ip6[ii], 14270304c731SJamie Gritton &ppr->pr_ip6[0])) 14280304c731SJamie Gritton continue; 14290304c731SJamie Gritton for (; ij < ppr->pr_ip6s; ij++) 14300304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL( 14310304c731SJamie Gritton &ip6[ii], 14320304c731SJamie Gritton &ppr->pr_ip6[ij])) 14330304c731SJamie Gritton break; 14340304c731SJamie Gritton if (ij == ppr->pr_ip6s) 14350304c731SJamie Gritton break; 14360304c731SJamie Gritton } 14370304c731SJamie Gritton if (ij == ppr->pr_ip6s) { 14380304c731SJamie Gritton error = EPERM; 14390304c731SJamie Gritton goto done_deref_locked; 14400304c731SJamie Gritton } 14410304c731SJamie Gritton } 14420304c731SJamie Gritton } 14430304c731SJamie Gritton } 14440304c731SJamie Gritton if (ip6s > 0) { 14450304c731SJamie Gritton /* Check for conflicting IP addresses. */ 1446bdfc8cc4SJamie Gritton tppr = ppr; 1447bdfc8cc4SJamie Gritton #ifdef VIMAGE 1448bdfc8cc4SJamie Gritton for (; tppr != &prison0; tppr = tppr->pr_parent) 1449bdfc8cc4SJamie Gritton if (tppr->pr_flags & PR_VNET) 1450bdfc8cc4SJamie Gritton break; 1451bdfc8cc4SJamie Gritton #endif 1452bdfc8cc4SJamie Gritton FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1453bdfc8cc4SJamie Gritton if (tpr == pr || 1454bdfc8cc4SJamie Gritton #ifdef VIMAGE 1455bdfc8cc4SJamie Gritton (tpr != tppr && 1456bdfc8cc4SJamie Gritton (tpr->pr_flags & PR_VNET)) || 1457bdfc8cc4SJamie Gritton #endif 1458bdfc8cc4SJamie Gritton tpr->pr_uref == 0) { 14590304c731SJamie Gritton descend = 0; 14600304c731SJamie Gritton continue; 14610304c731SJamie Gritton } 14620304c731SJamie Gritton if (!(tpr->pr_flags & PR_IP6_USER)) 14630304c731SJamie Gritton continue; 14640304c731SJamie Gritton descend = 0; 14650304c731SJamie Gritton if (tpr->pr_ip6 == NULL || 14660304c731SJamie Gritton (ip6s == 1 && tpr->pr_ip6s == 1)) 14670304c731SJamie Gritton continue; 14680304c731SJamie Gritton for (ii = 0; ii < ip6s; ii++) { 1469b38ff370SJamie Gritton if (_prison_check_ip6(tpr, 1470b38ff370SJamie Gritton &ip6[ii]) == 0) { 14710304c731SJamie Gritton error = EADDRINUSE; 1472b38ff370SJamie Gritton vfs_opterror(opts, 1473b38ff370SJamie Gritton "IPv6 addresses clash"); 1474b38ff370SJamie Gritton goto done_deref_locked; 1475b38ff370SJamie Gritton } 14760304c731SJamie Gritton } 14770304c731SJamie Gritton } 14780304c731SJamie Gritton } 1479b38ff370SJamie Gritton } 1480b38ff370SJamie Gritton #endif 14810304c731SJamie Gritton onamelen = namelen = 0; 14820304c731SJamie Gritton if (name != NULL) { 1483b38ff370SJamie Gritton /* Give a default name of the jid. */ 1484b38ff370SJamie Gritton if (name[0] == '\0') 1485b38ff370SJamie Gritton snprintf(name = numbuf, sizeof(numbuf), "%d", jid); 1486b38ff370SJamie Gritton else if (strtoul(name, &p, 10) != jid && *p == '\0') { 1487b38ff370SJamie Gritton error = EINVAL; 1488b38ff370SJamie Gritton vfs_opterror(opts, "name cannot be numeric"); 14890304c731SJamie Gritton goto done_deref_locked; 1490b38ff370SJamie Gritton } 1491b38ff370SJamie Gritton /* 14920304c731SJamie Gritton * Make sure the name isn't too long for the prison or its 14930304c731SJamie Gritton * children. 1494b38ff370SJamie Gritton */ 14950304c731SJamie Gritton onamelen = strlen(pr->pr_name); 14960304c731SJamie Gritton namelen = strlen(name); 14970304c731SJamie Gritton if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) { 14980304c731SJamie Gritton error = ENAMETOOLONG; 14990304c731SJamie Gritton goto done_deref_locked; 15000304c731SJamie Gritton } 15010304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 15020304c731SJamie Gritton if (strlen(tpr->pr_name) + (namelen - onamelen) >= 15030304c731SJamie Gritton sizeof(pr->pr_name)) { 15040304c731SJamie Gritton error = ENAMETOOLONG; 15050304c731SJamie Gritton goto done_deref_locked; 15060304c731SJamie Gritton } 15070304c731SJamie Gritton } 15080304c731SJamie Gritton } 15090304c731SJamie Gritton if (pr_allow & ~ppr->pr_allow) { 15100304c731SJamie Gritton error = EPERM; 15110304c731SJamie Gritton goto done_deref_locked; 1512b38ff370SJamie Gritton } 1513b38ff370SJamie Gritton 1514b38ff370SJamie Gritton /* Set the parameters of the prison. */ 1515b38ff370SJamie Gritton #ifdef INET 15160304c731SJamie Gritton redo_ip4 = 0; 15170304c731SJamie Gritton if (ch_flags & PR_IP4_USER) { 15180304c731SJamie Gritton if (pr_flags & PR_IP4_USER) { 15190304c731SJamie Gritton /* Some restriction set. */ 15200304c731SJamie Gritton pr->pr_flags |= PR_IP4; 1521b38ff370SJamie Gritton if (ip4s >= 0) { 1522b38ff370SJamie Gritton free(pr->pr_ip4, M_PRISON); 15230304c731SJamie Gritton pr->pr_ip4s = ip4s; 1524b38ff370SJamie Gritton pr->pr_ip4 = ip4; 1525b38ff370SJamie Gritton ip4 = NULL; 1526b38ff370SJamie Gritton } 15270304c731SJamie Gritton } else if (ppr->pr_flags & PR_IP4) { 15280304c731SJamie Gritton /* This restriction cleared, but keep inherited. */ 15290304c731SJamie Gritton free(pr->pr_ip4, M_PRISON); 15300304c731SJamie Gritton pr->pr_ip4s = ip4s; 15310304c731SJamie Gritton pr->pr_ip4 = ip4; 15320304c731SJamie Gritton ip4 = NULL; 15330304c731SJamie Gritton } else { 15340304c731SJamie Gritton /* Restriction cleared, now unrestricted. */ 15350304c731SJamie Gritton pr->pr_flags &= ~PR_IP4; 15360304c731SJamie Gritton free(pr->pr_ip4, M_PRISON); 15370304c731SJamie Gritton pr->pr_ip4s = 0; 15380304c731SJamie Gritton } 15390304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1540bdfc8cc4SJamie Gritton #ifdef VIMAGE 1541bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1542bdfc8cc4SJamie Gritton descend = 0; 1543bdfc8cc4SJamie Gritton continue; 1544bdfc8cc4SJamie Gritton } 1545bdfc8cc4SJamie Gritton #endif 15460304c731SJamie Gritton if (prison_restrict_ip4(tpr, NULL)) { 15470304c731SJamie Gritton redo_ip4 = 1; 15480304c731SJamie Gritton descend = 0; 15490304c731SJamie Gritton } 15500304c731SJamie Gritton } 15510304c731SJamie Gritton } 1552b38ff370SJamie Gritton #endif 1553b38ff370SJamie Gritton #ifdef INET6 15540304c731SJamie Gritton redo_ip6 = 0; 15550304c731SJamie Gritton if (ch_flags & PR_IP6_USER) { 15560304c731SJamie Gritton if (pr_flags & PR_IP6_USER) { 15570304c731SJamie Gritton /* Some restriction set. */ 15580304c731SJamie Gritton pr->pr_flags |= PR_IP6; 1559b38ff370SJamie Gritton if (ip6s >= 0) { 1560b38ff370SJamie Gritton free(pr->pr_ip6, M_PRISON); 15610304c731SJamie Gritton pr->pr_ip6s = ip6s; 1562b38ff370SJamie Gritton pr->pr_ip6 = ip6; 1563b38ff370SJamie Gritton ip6 = NULL; 1564b38ff370SJamie Gritton } 15650304c731SJamie Gritton } else if (ppr->pr_flags & PR_IP6) { 15660304c731SJamie Gritton /* This restriction cleared, but keep inherited. */ 15670304c731SJamie Gritton free(pr->pr_ip6, M_PRISON); 15680304c731SJamie Gritton pr->pr_ip6s = ip6s; 15690304c731SJamie Gritton pr->pr_ip6 = ip6; 15700304c731SJamie Gritton ip6 = NULL; 15710304c731SJamie Gritton } else { 15720304c731SJamie Gritton /* Restriction cleared, now unrestricted. */ 15730304c731SJamie Gritton pr->pr_flags &= ~PR_IP6; 15740304c731SJamie Gritton free(pr->pr_ip6, M_PRISON); 15750304c731SJamie Gritton pr->pr_ip6s = 0; 15760304c731SJamie Gritton } 15770304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1578bdfc8cc4SJamie Gritton #ifdef VIMAGE 1579bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1580bdfc8cc4SJamie Gritton descend = 0; 1581bdfc8cc4SJamie Gritton continue; 1582bdfc8cc4SJamie Gritton } 1583bdfc8cc4SJamie Gritton #endif 15840304c731SJamie Gritton if (prison_restrict_ip6(tpr, NULL)) { 15850304c731SJamie Gritton redo_ip6 = 1; 15860304c731SJamie Gritton descend = 0; 15870304c731SJamie Gritton } 15880304c731SJamie Gritton } 15890304c731SJamie Gritton } 1590b38ff370SJamie Gritton #endif 15910304c731SJamie Gritton if (gotslevel) { 1592b38ff370SJamie Gritton pr->pr_securelevel = slevel; 15930304c731SJamie Gritton /* Set all child jails to be at least this level. */ 15940304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 15950304c731SJamie Gritton if (tpr->pr_securelevel < slevel) 15960304c731SJamie Gritton tpr->pr_securelevel = slevel; 15970304c731SJamie Gritton } 1598b97457e2SJamie Gritton if (gotchildmax) { 1599b97457e2SJamie Gritton pr->pr_childmax = childmax; 1600b97457e2SJamie Gritton /* Set all child jails to under this limit. */ 1601b97457e2SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level) 1602b97457e2SJamie Gritton if (tpr->pr_childmax > childmax - level) 1603b97457e2SJamie Gritton tpr->pr_childmax = childmax > level 1604b97457e2SJamie Gritton ? childmax - level : 0; 1605b97457e2SJamie Gritton } 16060304c731SJamie Gritton if (gotenforce) { 16070304c731SJamie Gritton pr->pr_enforce_statfs = enforce; 16080304c731SJamie Gritton /* Pass this restriction on to the children. */ 16090304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 16100304c731SJamie Gritton if (tpr->pr_enforce_statfs < enforce) 16110304c731SJamie Gritton tpr->pr_enforce_statfs = enforce; 16120304c731SJamie Gritton } 16130304c731SJamie Gritton if (name != NULL) { 16140304c731SJamie Gritton if (ppr == &prison0) 1615b38ff370SJamie Gritton strlcpy(pr->pr_name, name, sizeof(pr->pr_name)); 16160304c731SJamie Gritton else 16170304c731SJamie Gritton snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s", 16180304c731SJamie Gritton ppr->pr_name, name); 16190304c731SJamie Gritton /* Change this component of child names. */ 16200304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 16210304c731SJamie Gritton bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen, 16220304c731SJamie Gritton strlen(tpr->pr_name + onamelen) + 1); 16230304c731SJamie Gritton bcopy(pr->pr_name, tpr->pr_name, namelen); 16240304c731SJamie Gritton } 16250304c731SJamie Gritton } 1626b38ff370SJamie Gritton if (path != NULL) { 16270304c731SJamie Gritton /* Try to keep a real-rooted full pathname. */ 16280304c731SJamie Gritton if (path[0] == '/' && strcmp(mypr->pr_path, "/")) 16290304c731SJamie Gritton snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s", 16300304c731SJamie Gritton mypr->pr_path, path); 16310304c731SJamie Gritton else 1632b38ff370SJamie Gritton strlcpy(pr->pr_path, path, sizeof(pr->pr_path)); 1633b38ff370SJamie Gritton pr->pr_root = root; 1634b38ff370SJamie Gritton } 163576ca6f88SJamie Gritton if (PR_HOST & ch_flags & ~pr_flags) { 163676ca6f88SJamie Gritton if (pr->pr_flags & PR_HOST) { 163776ca6f88SJamie Gritton /* 163876ca6f88SJamie Gritton * Copy the parent's host info. As with pr_ip4 above, 163976ca6f88SJamie Gritton * the lack of a lock on the parent is not a problem; 164076ca6f88SJamie Gritton * it is always set with allprison_lock at least 164176ca6f88SJamie Gritton * shared, and is held exclusively here. 164276ca6f88SJamie Gritton */ 1643c1f19219SJamie Gritton strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname, 1644c1f19219SJamie Gritton sizeof(pr->pr_hostname)); 1645c1f19219SJamie Gritton strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname, 1646c1f19219SJamie Gritton sizeof(pr->pr_domainname)); 1647c1f19219SJamie Gritton strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid, 1648c1f19219SJamie Gritton sizeof(pr->pr_hostuuid)); 164976ca6f88SJamie Gritton pr->pr_hostid = pr->pr_parent->pr_hostid; 165076ca6f88SJamie Gritton } 165176ca6f88SJamie Gritton } else if (host != NULL || domain != NULL || uuid != NULL || gothid) { 165276ca6f88SJamie Gritton /* Set this prison, and any descendants without PR_HOST. */ 1653b38ff370SJamie Gritton if (host != NULL) 1654c1f19219SJamie Gritton strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname)); 165576ca6f88SJamie Gritton if (domain != NULL) 1656c1f19219SJamie Gritton strlcpy(pr->pr_domainname, domain, 1657c1f19219SJamie Gritton sizeof(pr->pr_domainname)); 165876ca6f88SJamie Gritton if (uuid != NULL) 1659c1f19219SJamie Gritton strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid)); 166076ca6f88SJamie Gritton if (gothid) 166176ca6f88SJamie Gritton pr->pr_hostid = hid; 166276ca6f88SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 166376ca6f88SJamie Gritton if (tpr->pr_flags & PR_HOST) 166476ca6f88SJamie Gritton descend = 0; 166576ca6f88SJamie Gritton else { 166676ca6f88SJamie Gritton if (host != NULL) 1667c1f19219SJamie Gritton strlcpy(tpr->pr_hostname, 1668c1f19219SJamie Gritton pr->pr_hostname, 1669c1f19219SJamie Gritton sizeof(tpr->pr_hostname)); 167076ca6f88SJamie Gritton if (domain != NULL) 1671c1f19219SJamie Gritton strlcpy(tpr->pr_domainname, 1672c1f19219SJamie Gritton pr->pr_domainname, 1673c1f19219SJamie Gritton sizeof(tpr->pr_domainname)); 167476ca6f88SJamie Gritton if (uuid != NULL) 1675c1f19219SJamie Gritton strlcpy(tpr->pr_hostuuid, 1676c1f19219SJamie Gritton pr->pr_hostuuid, 1677c1f19219SJamie Gritton sizeof(tpr->pr_hostuuid)); 167876ca6f88SJamie Gritton if (gothid) 167976ca6f88SJamie Gritton tpr->pr_hostid = hid; 168076ca6f88SJamie Gritton } 168176ca6f88SJamie Gritton } 168276ca6f88SJamie Gritton } 16830304c731SJamie Gritton if ((tallow = ch_allow & ~pr_allow)) { 16840304c731SJamie Gritton /* Clear allow bits in all children. */ 16850304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 16860304c731SJamie Gritton tpr->pr_allow &= ~tallow; 16870304c731SJamie Gritton } 16880304c731SJamie Gritton pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow; 1689b38ff370SJamie Gritton /* 1690b38ff370SJamie Gritton * Persistent prisons get an extra reference, and prisons losing their 1691b38ff370SJamie Gritton * persist flag lose that reference. Only do this for existing prisons 1692b38ff370SJamie Gritton * for now, so new ones will remain unseen until after the module 1693b38ff370SJamie Gritton * handlers have completed. 1694b38ff370SJamie Gritton */ 1695b38ff370SJamie Gritton if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) { 1696b38ff370SJamie Gritton if (pr_flags & PR_PERSIST) { 1697b38ff370SJamie Gritton pr->pr_ref++; 1698b38ff370SJamie Gritton pr->pr_uref++; 1699b38ff370SJamie Gritton } else { 1700b38ff370SJamie Gritton pr->pr_ref--; 1701b38ff370SJamie Gritton pr->pr_uref--; 1702b38ff370SJamie Gritton } 1703b38ff370SJamie Gritton } 1704b38ff370SJamie Gritton pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags; 1705b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1706b38ff370SJamie Gritton 17070304c731SJamie Gritton /* Locks may have prevented a complete restriction of child IP 17080304c731SJamie Gritton * addresses. If so, allocate some more memory and try again. 17090304c731SJamie Gritton */ 17100304c731SJamie Gritton #ifdef INET 17110304c731SJamie Gritton while (redo_ip4) { 17120304c731SJamie Gritton ip4s = pr->pr_ip4s; 17130304c731SJamie Gritton ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 17140304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 17150304c731SJamie Gritton redo_ip4 = 0; 17160304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1717bdfc8cc4SJamie Gritton #ifdef VIMAGE 1718bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1719bdfc8cc4SJamie Gritton descend = 0; 1720bdfc8cc4SJamie Gritton continue; 1721bdfc8cc4SJamie Gritton } 1722bdfc8cc4SJamie Gritton #endif 17230304c731SJamie Gritton if (prison_restrict_ip4(tpr, ip4)) { 17240304c731SJamie Gritton if (ip4 != NULL) 17250304c731SJamie Gritton ip4 = NULL; 17260304c731SJamie Gritton else 17270304c731SJamie Gritton redo_ip4 = 1; 17280304c731SJamie Gritton } 17290304c731SJamie Gritton } 17300304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 17310304c731SJamie Gritton } 17320304c731SJamie Gritton #endif 17330304c731SJamie Gritton #ifdef INET6 17340304c731SJamie Gritton while (redo_ip6) { 17350304c731SJamie Gritton ip6s = pr->pr_ip6s; 17360304c731SJamie Gritton ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 17370304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 17380304c731SJamie Gritton redo_ip6 = 0; 17390304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1740bdfc8cc4SJamie Gritton #ifdef VIMAGE 1741bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1742bdfc8cc4SJamie Gritton descend = 0; 1743bdfc8cc4SJamie Gritton continue; 1744bdfc8cc4SJamie Gritton } 1745bdfc8cc4SJamie Gritton #endif 17460304c731SJamie Gritton if (prison_restrict_ip6(tpr, ip6)) { 17470304c731SJamie Gritton if (ip6 != NULL) 17480304c731SJamie Gritton ip6 = NULL; 17490304c731SJamie Gritton else 17500304c731SJamie Gritton redo_ip6 = 1; 17510304c731SJamie Gritton } 17520304c731SJamie Gritton } 17530304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 17540304c731SJamie Gritton } 17550304c731SJamie Gritton #endif 17560304c731SJamie Gritton 1757b38ff370SJamie Gritton /* Let the modules do their work. */ 1758b38ff370SJamie Gritton sx_downgrade(&allprison_lock); 1759b38ff370SJamie Gritton if (created) { 1760b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_CREATE, opts); 1761b38ff370SJamie Gritton if (error) { 1762b38ff370SJamie Gritton prison_deref(pr, PD_LIST_SLOCKED); 1763b38ff370SJamie Gritton goto done_errmsg; 1764b38ff370SJamie Gritton } 1765b38ff370SJamie Gritton } 1766b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_SET, opts); 1767b38ff370SJamie Gritton if (error) { 1768b38ff370SJamie Gritton prison_deref(pr, created 1769b38ff370SJamie Gritton ? PD_LIST_SLOCKED 1770b38ff370SJamie Gritton : PD_DEREF | PD_LIST_SLOCKED); 1771b38ff370SJamie Gritton goto done_errmsg; 1772b38ff370SJamie Gritton } 1773b38ff370SJamie Gritton 1774b38ff370SJamie Gritton /* Attach this process to the prison if requested. */ 1775b38ff370SJamie Gritton if (flags & JAIL_ATTACH) { 1776b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1777b38ff370SJamie Gritton error = do_jail_attach(td, pr); 1778b38ff370SJamie Gritton if (error) { 1779b38ff370SJamie Gritton vfs_opterror(opts, "attach failed"); 1780b38ff370SJamie Gritton if (!created) 1781b38ff370SJamie Gritton prison_deref(pr, PD_DEREF); 1782b38ff370SJamie Gritton goto done_errmsg; 1783b38ff370SJamie Gritton } 1784b38ff370SJamie Gritton } 1785b38ff370SJamie Gritton 1786b38ff370SJamie Gritton /* 1787b38ff370SJamie Gritton * Now that it is all there, drop the temporary reference from existing 1788b38ff370SJamie Gritton * prisons. Or add a reference to newly created persistent prisons 1789b38ff370SJamie Gritton * (which was not done earlier so that the prison would not be publicly 1790b38ff370SJamie Gritton * visible). 1791b38ff370SJamie Gritton */ 1792b38ff370SJamie Gritton if (!created) { 1793b38ff370SJamie Gritton prison_deref(pr, (flags & JAIL_ATTACH) 1794b38ff370SJamie Gritton ? PD_DEREF 1795b38ff370SJamie Gritton : PD_DEREF | PD_LIST_SLOCKED); 1796b38ff370SJamie Gritton } else { 1797b38ff370SJamie Gritton if (pr_flags & PR_PERSIST) { 1798b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1799b38ff370SJamie Gritton pr->pr_ref++; 1800b38ff370SJamie Gritton pr->pr_uref++; 1801b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1802b38ff370SJamie Gritton } 1803b38ff370SJamie Gritton if (!(flags & JAIL_ATTACH)) 1804b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 1805b38ff370SJamie Gritton } 1806b38ff370SJamie Gritton td->td_retval[0] = pr->pr_id; 1807b38ff370SJamie Gritton goto done_errmsg; 1808b38ff370SJamie Gritton 18090304c731SJamie Gritton done_deref_locked: 18100304c731SJamie Gritton prison_deref(pr, created 18110304c731SJamie Gritton ? PD_LOCKED | PD_LIST_XLOCKED 18120304c731SJamie Gritton : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 18130304c731SJamie Gritton goto done_releroot; 1814b38ff370SJamie Gritton done_unlock_list: 1815b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 1816b38ff370SJamie Gritton done_releroot: 1817b38ff370SJamie Gritton if (root != NULL) { 1818b38ff370SJamie Gritton vfslocked = VFS_LOCK_GIANT(root->v_mount); 1819b38ff370SJamie Gritton vrele(root); 1820b38ff370SJamie Gritton VFS_UNLOCK_GIANT(vfslocked); 1821b38ff370SJamie Gritton } 1822b38ff370SJamie Gritton done_errmsg: 1823b38ff370SJamie Gritton if (error) { 1824b38ff370SJamie Gritton vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 1825b38ff370SJamie Gritton if (errmsg_len > 0) { 1826b38ff370SJamie Gritton errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1; 1827b38ff370SJamie Gritton if (errmsg_pos > 0) { 1828b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 1829b38ff370SJamie Gritton bcopy(errmsg, 1830b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 1831b38ff370SJamie Gritton errmsg_len); 1832b38ff370SJamie Gritton else 1833b38ff370SJamie Gritton copyout(errmsg, 1834b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 1835b38ff370SJamie Gritton errmsg_len); 1836b38ff370SJamie Gritton } 1837b38ff370SJamie Gritton } 1838b38ff370SJamie Gritton } 1839b38ff370SJamie Gritton done_free: 1840b38ff370SJamie Gritton #ifdef INET 1841b38ff370SJamie Gritton free(ip4, M_PRISON); 1842b38ff370SJamie Gritton #endif 1843b38ff370SJamie Gritton #ifdef INET6 1844b38ff370SJamie Gritton free(ip6, M_PRISON); 1845b38ff370SJamie Gritton #endif 1846b38ff370SJamie Gritton vfs_freeopts(opts); 1847b38ff370SJamie Gritton return (error); 1848b38ff370SJamie Gritton } 1849b38ff370SJamie Gritton 1850b38ff370SJamie Gritton 1851b38ff370SJamie Gritton /* 1852b38ff370SJamie Gritton * struct jail_get_args { 1853b38ff370SJamie Gritton * struct iovec *iovp; 1854b38ff370SJamie Gritton * unsigned int iovcnt; 1855b38ff370SJamie Gritton * int flags; 1856b38ff370SJamie Gritton * }; 1857b38ff370SJamie Gritton */ 1858b38ff370SJamie Gritton int 1859b38ff370SJamie Gritton jail_get(struct thread *td, struct jail_get_args *uap) 1860b38ff370SJamie Gritton { 1861b38ff370SJamie Gritton struct uio *auio; 1862b38ff370SJamie Gritton int error; 1863b38ff370SJamie Gritton 1864b38ff370SJamie Gritton /* Check that we have an even number of iovecs. */ 1865b38ff370SJamie Gritton if (uap->iovcnt & 1) 1866b38ff370SJamie Gritton return (EINVAL); 1867b38ff370SJamie Gritton 1868b38ff370SJamie Gritton error = copyinuio(uap->iovp, uap->iovcnt, &auio); 1869b38ff370SJamie Gritton if (error) 1870b38ff370SJamie Gritton return (error); 1871b38ff370SJamie Gritton error = kern_jail_get(td, auio, uap->flags); 1872b38ff370SJamie Gritton if (error == 0) 1873b38ff370SJamie Gritton error = copyout(auio->uio_iov, uap->iovp, 1874b38ff370SJamie Gritton uap->iovcnt * sizeof (struct iovec)); 1875b38ff370SJamie Gritton free(auio, M_IOV); 1876b38ff370SJamie Gritton return (error); 1877b38ff370SJamie Gritton } 1878b38ff370SJamie Gritton 1879b38ff370SJamie Gritton int 1880b38ff370SJamie Gritton kern_jail_get(struct thread *td, struct uio *optuio, int flags) 1881b38ff370SJamie Gritton { 18820304c731SJamie Gritton struct prison *pr, *mypr; 1883b38ff370SJamie Gritton struct vfsopt *opt; 1884b38ff370SJamie Gritton struct vfsoptlist *opts; 1885b38ff370SJamie Gritton char *errmsg, *name; 18860304c731SJamie Gritton int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos; 1887b38ff370SJamie Gritton 1888b38ff370SJamie Gritton if (flags & ~JAIL_GET_MASK) 1889b38ff370SJamie Gritton return (EINVAL); 1890b38ff370SJamie Gritton 1891b38ff370SJamie Gritton /* Get the parameter list. */ 1892b38ff370SJamie Gritton error = vfs_buildopts(optuio, &opts); 1893b38ff370SJamie Gritton if (error) 1894b38ff370SJamie Gritton return (error); 1895b38ff370SJamie Gritton errmsg_pos = vfs_getopt_pos(opts, "errmsg"); 18960304c731SJamie Gritton mypr = td->td_ucred->cr_prison; 18971e2a13e6SJamie Gritton 1898b38ff370SJamie Gritton /* 1899b38ff370SJamie Gritton * Find the prison specified by one of: lastjid, jid, name. 1900b38ff370SJamie Gritton */ 1901b38ff370SJamie Gritton sx_slock(&allprison_lock); 1902b38ff370SJamie Gritton error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid)); 1903b38ff370SJamie Gritton if (error == 0) { 1904b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 19050304c731SJamie Gritton if (pr->pr_id > jid && prison_ischild(mypr, pr)) { 1906b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1907b38ff370SJamie Gritton if (pr->pr_ref > 0 && 1908b38ff370SJamie Gritton (pr->pr_uref > 0 || (flags & JAIL_DYING))) 1909b38ff370SJamie Gritton break; 1910b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1911b38ff370SJamie Gritton } 1912b38ff370SJamie Gritton } 1913b38ff370SJamie Gritton if (pr != NULL) 1914b38ff370SJamie Gritton goto found_prison; 1915b38ff370SJamie Gritton error = ENOENT; 1916b38ff370SJamie Gritton vfs_opterror(opts, "no jail after %d", jid); 1917b38ff370SJamie Gritton goto done_unlock_list; 1918b38ff370SJamie Gritton } else if (error != ENOENT) 1919b38ff370SJamie Gritton goto done_unlock_list; 1920b38ff370SJamie Gritton 1921b38ff370SJamie Gritton error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 1922b38ff370SJamie Gritton if (error == 0) { 1923b38ff370SJamie Gritton if (jid != 0) { 19240304c731SJamie Gritton pr = prison_find_child(mypr, jid); 1925b38ff370SJamie Gritton if (pr != NULL) { 1926b38ff370SJamie Gritton if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 1927b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1928b38ff370SJamie Gritton error = ENOENT; 1929b38ff370SJamie Gritton vfs_opterror(opts, "jail %d is dying", 1930b38ff370SJamie Gritton jid); 1931b38ff370SJamie Gritton goto done_unlock_list; 1932b38ff370SJamie Gritton } 1933b38ff370SJamie Gritton goto found_prison; 1934b38ff370SJamie Gritton } 1935b38ff370SJamie Gritton error = ENOENT; 1936b38ff370SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 1937b38ff370SJamie Gritton goto done_unlock_list; 1938b38ff370SJamie Gritton } 1939b38ff370SJamie Gritton } else if (error != ENOENT) 1940b38ff370SJamie Gritton goto done_unlock_list; 1941b38ff370SJamie Gritton 1942b38ff370SJamie Gritton error = vfs_getopt(opts, "name", (void **)&name, &len); 1943b38ff370SJamie Gritton if (error == 0) { 1944b38ff370SJamie Gritton if (len == 0 || name[len - 1] != '\0') { 1945b38ff370SJamie Gritton error = EINVAL; 1946b38ff370SJamie Gritton goto done_unlock_list; 1947b38ff370SJamie Gritton } 19480304c731SJamie Gritton pr = prison_find_name(mypr, name); 1949b38ff370SJamie Gritton if (pr != NULL) { 1950b38ff370SJamie Gritton if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 1951b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1952b38ff370SJamie Gritton error = ENOENT; 1953b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" is dying", 1954b38ff370SJamie Gritton name); 1955b38ff370SJamie Gritton goto done_unlock_list; 1956b38ff370SJamie Gritton } 1957b38ff370SJamie Gritton goto found_prison; 1958b38ff370SJamie Gritton } 1959b38ff370SJamie Gritton error = ENOENT; 1960b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", name); 1961b38ff370SJamie Gritton goto done_unlock_list; 1962b38ff370SJamie Gritton } else if (error != ENOENT) 1963b38ff370SJamie Gritton goto done_unlock_list; 1964b38ff370SJamie Gritton 1965b38ff370SJamie Gritton vfs_opterror(opts, "no jail specified"); 1966b38ff370SJamie Gritton error = ENOENT; 1967b38ff370SJamie Gritton goto done_unlock_list; 1968b38ff370SJamie Gritton 1969b38ff370SJamie Gritton found_prison: 1970b38ff370SJamie Gritton /* Get the parameters of the prison. */ 1971b38ff370SJamie Gritton pr->pr_ref++; 1972b38ff370SJamie Gritton locked = PD_LOCKED; 1973b38ff370SJamie Gritton td->td_retval[0] = pr->pr_id; 1974b38ff370SJamie Gritton error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id)); 1975b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 1976b38ff370SJamie Gritton goto done_deref; 19770304c731SJamie Gritton i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id; 19780304c731SJamie Gritton error = vfs_setopt(opts, "parent", &i, sizeof(i)); 1979b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 1980b38ff370SJamie Gritton goto done_deref; 19810304c731SJamie Gritton error = vfs_setopts(opts, "name", prison_name(mypr, pr)); 19820304c731SJamie Gritton if (error != 0 && error != ENOENT) 19830304c731SJamie Gritton goto done_deref; 19840304c731SJamie Gritton error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id, 1985b38ff370SJamie Gritton sizeof(pr->pr_cpuset->cs_id)); 1986b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 1987b38ff370SJamie Gritton goto done_deref; 19880304c731SJamie Gritton error = vfs_setopts(opts, "path", prison_path(mypr, pr)); 1989b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 1990b38ff370SJamie Gritton goto done_deref; 1991b38ff370SJamie Gritton #ifdef INET 1992b38ff370SJamie Gritton error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4, 1993b38ff370SJamie Gritton pr->pr_ip4s * sizeof(*pr->pr_ip4)); 1994b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 1995b38ff370SJamie Gritton goto done_deref; 1996b38ff370SJamie Gritton #endif 1997b38ff370SJamie Gritton #ifdef INET6 1998b38ff370SJamie Gritton error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6, 1999b38ff370SJamie Gritton pr->pr_ip6s * sizeof(*pr->pr_ip6)); 2000b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2001b38ff370SJamie Gritton goto done_deref; 2002b38ff370SJamie Gritton #endif 2003b38ff370SJamie Gritton error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel, 2004b38ff370SJamie Gritton sizeof(pr->pr_securelevel)); 2005b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2006b38ff370SJamie Gritton goto done_deref; 2007b97457e2SJamie Gritton error = vfs_setopt(opts, "children.cur", &pr->pr_childcount, 2008b97457e2SJamie Gritton sizeof(pr->pr_childcount)); 2009b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 2010b97457e2SJamie Gritton goto done_deref; 2011b97457e2SJamie Gritton error = vfs_setopt(opts, "children.max", &pr->pr_childmax, 2012b97457e2SJamie Gritton sizeof(pr->pr_childmax)); 2013b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 2014b97457e2SJamie Gritton goto done_deref; 2015c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostname", pr->pr_hostname); 2016b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2017b38ff370SJamie Gritton goto done_deref; 2018c1f19219SJamie Gritton error = vfs_setopts(opts, "host.domainname", pr->pr_domainname); 201976ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 202076ca6f88SJamie Gritton goto done_deref; 2021c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid); 202276ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 202376ca6f88SJamie Gritton goto done_deref; 202476ca6f88SJamie Gritton #ifdef COMPAT_IA32 202576ca6f88SJamie Gritton if (td->td_proc->p_sysent->sv_flags & SV_IA32) { 202676ca6f88SJamie Gritton uint32_t hid32 = pr->pr_hostid; 202776ca6f88SJamie Gritton 202876ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32)); 202976ca6f88SJamie Gritton } else 203076ca6f88SJamie Gritton #endif 203176ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid, 203276ca6f88SJamie Gritton sizeof(pr->pr_hostid)); 203376ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 203476ca6f88SJamie Gritton goto done_deref; 20350304c731SJamie Gritton error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs, 20360304c731SJamie Gritton sizeof(pr->pr_enforce_statfs)); 20370304c731SJamie Gritton if (error != 0 && error != ENOENT) 20380304c731SJamie Gritton goto done_deref; 20390304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 20400304c731SJamie Gritton fi++) { 20410304c731SJamie Gritton if (pr_flag_names[fi] == NULL) 20420304c731SJamie Gritton continue; 20430304c731SJamie Gritton i = (pr->pr_flags & (1 << fi)) ? 1 : 0; 20440304c731SJamie Gritton error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i)); 2045b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2046b38ff370SJamie Gritton goto done_deref; 2047b38ff370SJamie Gritton i = !i; 20480304c731SJamie Gritton error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i)); 2049b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2050b38ff370SJamie Gritton goto done_deref; 20510304c731SJamie Gritton } 20527cbf7213SJamie Gritton for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 20537cbf7213SJamie Gritton fi++) { 20547cbf7213SJamie Gritton i = pr->pr_flags & 20557cbf7213SJamie Gritton (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new); 20567cbf7213SJamie Gritton i = pr_flag_jailsys[fi].disable && 20577cbf7213SJamie Gritton (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE 20587cbf7213SJamie Gritton : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW 20597cbf7213SJamie Gritton : JAIL_SYS_INHERIT; 20607cbf7213SJamie Gritton error = 20617cbf7213SJamie Gritton vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i)); 20627cbf7213SJamie Gritton if (error != 0 && error != ENOENT) 20637cbf7213SJamie Gritton goto done_deref; 20647cbf7213SJamie Gritton } 20650304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 20660304c731SJamie Gritton fi++) { 20670304c731SJamie Gritton if (pr_allow_names[fi] == NULL) 20680304c731SJamie Gritton continue; 20690304c731SJamie Gritton i = (pr->pr_allow & (1 << fi)) ? 1 : 0; 20700304c731SJamie Gritton error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i)); 20710304c731SJamie Gritton if (error != 0 && error != ENOENT) 20720304c731SJamie Gritton goto done_deref; 20730304c731SJamie Gritton i = !i; 20740304c731SJamie Gritton error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i)); 20750304c731SJamie Gritton if (error != 0 && error != ENOENT) 20760304c731SJamie Gritton goto done_deref; 20770304c731SJamie Gritton } 2078b38ff370SJamie Gritton i = (pr->pr_uref == 0); 2079b38ff370SJamie Gritton error = vfs_setopt(opts, "dying", &i, sizeof(i)); 2080b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2081b38ff370SJamie Gritton goto done_deref; 2082b38ff370SJamie Gritton i = !i; 2083b38ff370SJamie Gritton error = vfs_setopt(opts, "nodying", &i, sizeof(i)); 2084b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2085b38ff370SJamie Gritton goto done_deref; 2086b38ff370SJamie Gritton 2087b38ff370SJamie Gritton /* Get the module parameters. */ 2088b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2089b38ff370SJamie Gritton locked = 0; 2090b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_GET, opts); 2091b38ff370SJamie Gritton if (error) 2092b38ff370SJamie Gritton goto done_deref; 2093b38ff370SJamie Gritton prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED); 2094b38ff370SJamie Gritton 2095b38ff370SJamie Gritton /* By now, all parameters should have been noted. */ 2096b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2097b38ff370SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 2098b38ff370SJamie Gritton error = EINVAL; 2099b38ff370SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 2100b38ff370SJamie Gritton goto done_errmsg; 2101b38ff370SJamie Gritton } 2102b38ff370SJamie Gritton } 2103b38ff370SJamie Gritton 2104b38ff370SJamie Gritton /* Write the fetched parameters back to userspace. */ 2105b38ff370SJamie Gritton error = 0; 2106b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2107b38ff370SJamie Gritton if (opt->pos >= 0 && opt->pos != errmsg_pos) { 2108b38ff370SJamie Gritton pos = 2 * opt->pos + 1; 2109b38ff370SJamie Gritton optuio->uio_iov[pos].iov_len = opt->len; 2110b38ff370SJamie Gritton if (opt->value != NULL) { 2111b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) { 2112b38ff370SJamie Gritton bcopy(opt->value, 2113b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2114b38ff370SJamie Gritton opt->len); 2115b38ff370SJamie Gritton } else { 2116b38ff370SJamie Gritton error = copyout(opt->value, 2117b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2118b38ff370SJamie Gritton opt->len); 2119b38ff370SJamie Gritton if (error) 2120b38ff370SJamie Gritton break; 2121b38ff370SJamie Gritton } 2122b38ff370SJamie Gritton } 2123b38ff370SJamie Gritton } 2124b38ff370SJamie Gritton } 2125b38ff370SJamie Gritton goto done_errmsg; 2126b38ff370SJamie Gritton 2127b38ff370SJamie Gritton done_deref: 2128b38ff370SJamie Gritton prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED); 2129b38ff370SJamie Gritton goto done_errmsg; 2130b38ff370SJamie Gritton 2131b38ff370SJamie Gritton done_unlock_list: 2132b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2133b38ff370SJamie Gritton done_errmsg: 2134b38ff370SJamie Gritton if (error && errmsg_pos >= 0) { 2135b38ff370SJamie Gritton vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 2136b38ff370SJamie Gritton errmsg_pos = 2 * errmsg_pos + 1; 2137b38ff370SJamie Gritton if (errmsg_len > 0) { 2138b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 2139b38ff370SJamie Gritton bcopy(errmsg, 2140b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2141b38ff370SJamie Gritton errmsg_len); 2142b38ff370SJamie Gritton else 2143b38ff370SJamie Gritton copyout(errmsg, 2144b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2145b38ff370SJamie Gritton errmsg_len); 2146b38ff370SJamie Gritton } 2147b38ff370SJamie Gritton } 2148b38ff370SJamie Gritton vfs_freeopts(opts); 2149b38ff370SJamie Gritton return (error); 2150b38ff370SJamie Gritton } 2151b38ff370SJamie Gritton 21520304c731SJamie Gritton 2153b38ff370SJamie Gritton /* 2154b38ff370SJamie Gritton * struct jail_remove_args { 2155b38ff370SJamie Gritton * int jid; 2156b38ff370SJamie Gritton * }; 2157b38ff370SJamie Gritton */ 2158b38ff370SJamie Gritton int 2159b38ff370SJamie Gritton jail_remove(struct thread *td, struct jail_remove_args *uap) 2160b38ff370SJamie Gritton { 21610304c731SJamie Gritton struct prison *pr, *cpr, *lpr, *tpr; 21620304c731SJamie Gritton int descend, error; 2163b38ff370SJamie Gritton 2164b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_REMOVE); 2165b38ff370SJamie Gritton if (error) 2166b38ff370SJamie Gritton return (error); 2167b38ff370SJamie Gritton 2168b38ff370SJamie Gritton sx_xlock(&allprison_lock); 21690304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2170b38ff370SJamie Gritton if (pr == NULL) { 2171b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2172b38ff370SJamie Gritton return (EINVAL); 2173b38ff370SJamie Gritton } 2174b38ff370SJamie Gritton 21750304c731SJamie Gritton /* Remove all descendants of this prison, then remove this prison. */ 21760304c731SJamie Gritton pr->pr_ref++; 21770304c731SJamie Gritton pr->pr_flags |= PR_REMOVE; 21780304c731SJamie Gritton if (!LIST_EMPTY(&pr->pr_children)) { 21790304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 21800304c731SJamie Gritton lpr = NULL; 21810304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 21820304c731SJamie Gritton mtx_lock(&cpr->pr_mtx); 21830304c731SJamie Gritton if (cpr->pr_ref > 0) { 21840304c731SJamie Gritton tpr = cpr; 21850304c731SJamie Gritton cpr->pr_ref++; 21860304c731SJamie Gritton cpr->pr_flags |= PR_REMOVE; 21870304c731SJamie Gritton } else { 21880304c731SJamie Gritton /* Already removed - do not do it again. */ 21890304c731SJamie Gritton tpr = NULL; 21900304c731SJamie Gritton } 21910304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 21920304c731SJamie Gritton if (lpr != NULL) { 21930304c731SJamie Gritton mtx_lock(&lpr->pr_mtx); 21940304c731SJamie Gritton prison_remove_one(lpr); 21950304c731SJamie Gritton sx_xlock(&allprison_lock); 21960304c731SJamie Gritton } 21970304c731SJamie Gritton lpr = tpr; 21980304c731SJamie Gritton } 21990304c731SJamie Gritton if (lpr != NULL) { 22000304c731SJamie Gritton mtx_lock(&lpr->pr_mtx); 22010304c731SJamie Gritton prison_remove_one(lpr); 22020304c731SJamie Gritton sx_xlock(&allprison_lock); 22030304c731SJamie Gritton } 22040304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 22050304c731SJamie Gritton } 22060304c731SJamie Gritton prison_remove_one(pr); 22070304c731SJamie Gritton return (0); 22080304c731SJamie Gritton } 22090304c731SJamie Gritton 22100304c731SJamie Gritton static void 22110304c731SJamie Gritton prison_remove_one(struct prison *pr) 22120304c731SJamie Gritton { 22130304c731SJamie Gritton struct proc *p; 22140304c731SJamie Gritton int deuref; 22150304c731SJamie Gritton 2216b38ff370SJamie Gritton /* If the prison was persistent, it is not anymore. */ 2217b38ff370SJamie Gritton deuref = 0; 2218b38ff370SJamie Gritton if (pr->pr_flags & PR_PERSIST) { 2219b38ff370SJamie Gritton pr->pr_ref--; 2220b38ff370SJamie Gritton deuref = PD_DEUREF; 2221b38ff370SJamie Gritton pr->pr_flags &= ~PR_PERSIST; 2222b38ff370SJamie Gritton } 2223b38ff370SJamie Gritton 22240304c731SJamie Gritton /* 22250304c731SJamie Gritton * jail_remove added a reference. If that's the only one, remove 22260304c731SJamie Gritton * the prison now. 22270304c731SJamie Gritton */ 22280304c731SJamie Gritton KASSERT(pr->pr_ref > 0, 22290304c731SJamie Gritton ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id)); 22300304c731SJamie Gritton if (pr->pr_ref == 1) { 2231b38ff370SJamie Gritton prison_deref(pr, 2232b38ff370SJamie Gritton deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 22330304c731SJamie Gritton return; 2234b38ff370SJamie Gritton } 2235b38ff370SJamie Gritton 2236b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2237b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2238b38ff370SJamie Gritton /* 2239b38ff370SJamie Gritton * Kill all processes unfortunate enough to be attached to this prison. 2240b38ff370SJamie Gritton */ 2241b38ff370SJamie Gritton sx_slock(&allproc_lock); 2242b38ff370SJamie Gritton LIST_FOREACH(p, &allproc, p_list) { 2243b38ff370SJamie Gritton PROC_LOCK(p); 2244b38ff370SJamie Gritton if (p->p_state != PRS_NEW && p->p_ucred && 2245b38ff370SJamie Gritton p->p_ucred->cr_prison == pr) 2246b38ff370SJamie Gritton psignal(p, SIGKILL); 2247b38ff370SJamie Gritton PROC_UNLOCK(p); 2248b38ff370SJamie Gritton } 2249b38ff370SJamie Gritton sx_sunlock(&allproc_lock); 22500304c731SJamie Gritton /* Remove the temporary reference added by jail_remove. */ 2251b38ff370SJamie Gritton prison_deref(pr, deuref | PD_DEREF); 225275c13541SPoul-Henning Kamp } 225375c13541SPoul-Henning Kamp 22548571af59SJamie Gritton 2255fd7a8150SMike Barcroft /* 22569ddb7954SMike Barcroft * struct jail_attach_args { 22579ddb7954SMike Barcroft * int jid; 22589ddb7954SMike Barcroft * }; 2259fd7a8150SMike Barcroft */ 2260fd7a8150SMike Barcroft int 22619ddb7954SMike Barcroft jail_attach(struct thread *td, struct jail_attach_args *uap) 2262fd7a8150SMike Barcroft { 2263b38ff370SJamie Gritton struct prison *pr; 2264b38ff370SJamie Gritton int error; 2265b38ff370SJamie Gritton 2266b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 2267b38ff370SJamie Gritton if (error) 2268b38ff370SJamie Gritton return (error); 2269b38ff370SJamie Gritton 2270b38ff370SJamie Gritton sx_slock(&allprison_lock); 22710304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2272b38ff370SJamie Gritton if (pr == NULL) { 2273b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2274b38ff370SJamie Gritton return (EINVAL); 2275b38ff370SJamie Gritton } 2276b38ff370SJamie Gritton 2277b38ff370SJamie Gritton /* 2278b38ff370SJamie Gritton * Do not allow a process to attach to a prison that is not 2279b38ff370SJamie Gritton * considered to be "alive". 2280b38ff370SJamie Gritton */ 2281b38ff370SJamie Gritton if (pr->pr_uref == 0) { 2282b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2283b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2284b38ff370SJamie Gritton return (EINVAL); 2285b38ff370SJamie Gritton } 2286b38ff370SJamie Gritton 2287b38ff370SJamie Gritton return (do_jail_attach(td, pr)); 2288b38ff370SJamie Gritton } 2289b38ff370SJamie Gritton 2290b38ff370SJamie Gritton static int 2291b38ff370SJamie Gritton do_jail_attach(struct thread *td, struct prison *pr) 2292b38ff370SJamie Gritton { 22930304c731SJamie Gritton struct prison *ppr; 2294fd7a8150SMike Barcroft struct proc *p; 2295fd7a8150SMike Barcroft struct ucred *newcred, *oldcred; 2296453f7d53SChristian S.J. Peron int vfslocked, error; 2297fd7a8150SMike Barcroft 229857f22bd4SJacques Vidrine /* 229957f22bd4SJacques Vidrine * XXX: Note that there is a slight race here if two threads 230057f22bd4SJacques Vidrine * in the same privileged process attempt to attach to two 230157f22bd4SJacques Vidrine * different jails at the same time. It is important for 230257f22bd4SJacques Vidrine * user processes not to do this, or they might end up with 230357f22bd4SJacques Vidrine * a process root from one prison, but attached to the jail 230457f22bd4SJacques Vidrine * of another. 230557f22bd4SJacques Vidrine */ 2306fd7a8150SMike Barcroft pr->pr_ref++; 2307b38ff370SJamie Gritton pr->pr_uref++; 2308fd7a8150SMike Barcroft mtx_unlock(&pr->pr_mtx); 2309b38ff370SJamie Gritton 2310b38ff370SJamie Gritton /* Let modules do whatever they need to prepare for attaching. */ 2311b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_ATTACH, td); 2312b38ff370SJamie Gritton if (error) { 2313b38ff370SJamie Gritton prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED); 2314b38ff370SJamie Gritton return (error); 2315b38ff370SJamie Gritton } 2316dc68a633SPawel Jakub Dawidek sx_sunlock(&allprison_lock); 2317fd7a8150SMike Barcroft 2318413628a7SBjoern A. Zeeb /* 2319413628a7SBjoern A. Zeeb * Reparent the newly attached process to this jail. 2320413628a7SBjoern A. Zeeb */ 23210304c731SJamie Gritton ppr = td->td_ucred->cr_prison; 2322b38ff370SJamie Gritton p = td->td_proc; 2323413628a7SBjoern A. Zeeb error = cpuset_setproc_update_set(p, pr->pr_cpuset); 2324413628a7SBjoern A. Zeeb if (error) 2325b38ff370SJamie Gritton goto e_revert_osd; 2326413628a7SBjoern A. Zeeb 2327453f7d53SChristian S.J. Peron vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount); 2328cb05b60aSAttilio Rao vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY); 2329fd7a8150SMike Barcroft if ((error = change_dir(pr->pr_root, td)) != 0) 2330fd7a8150SMike Barcroft goto e_unlock; 2331fd7a8150SMike Barcroft #ifdef MAC 233230d239bcSRobert Watson if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root))) 2333fd7a8150SMike Barcroft goto e_unlock; 2334fd7a8150SMike Barcroft #endif 233522db15c0SAttilio Rao VOP_UNLOCK(pr->pr_root, 0); 2336b38ff370SJamie Gritton if ((error = change_root(pr->pr_root, td))) 2337b38ff370SJamie Gritton goto e_unlock_giant; 2338453f7d53SChristian S.J. Peron VFS_UNLOCK_GIANT(vfslocked); 2339fd7a8150SMike Barcroft 2340fd7a8150SMike Barcroft newcred = crget(); 2341fd7a8150SMike Barcroft PROC_LOCK(p); 2342fd7a8150SMike Barcroft oldcred = p->p_ucred; 2343fd7a8150SMike Barcroft setsugid(p); 2344fd7a8150SMike Barcroft crcopy(newcred, oldcred); 234569c4ee54SJohn Baldwin newcred->cr_prison = pr; 2346fd7a8150SMike Barcroft p->p_ucred = newcred; 2347fd7a8150SMike Barcroft PROC_UNLOCK(p); 2348fd7a8150SMike Barcroft crfree(oldcred); 23490304c731SJamie Gritton prison_deref(ppr, PD_DEREF | PD_DEUREF); 2350fd7a8150SMike Barcroft return (0); 2351fd7a8150SMike Barcroft e_unlock: 235222db15c0SAttilio Rao VOP_UNLOCK(pr->pr_root, 0); 2353b38ff370SJamie Gritton e_unlock_giant: 2354453f7d53SChristian S.J. Peron VFS_UNLOCK_GIANT(vfslocked); 2355b38ff370SJamie Gritton e_revert_osd: 2356b38ff370SJamie Gritton /* Tell modules this thread is still in its old jail after all. */ 23570304c731SJamie Gritton (void)osd_jail_call(ppr, PR_METHOD_ATTACH, td); 2358b38ff370SJamie Gritton prison_deref(pr, PD_DEREF | PD_DEUREF); 2359fd7a8150SMike Barcroft return (error); 2360fd7a8150SMike Barcroft } 2361fd7a8150SMike Barcroft 23620304c731SJamie Gritton 2363fd7a8150SMike Barcroft /* 2364fd7a8150SMike Barcroft * Returns a locked prison instance, or NULL on failure. 2365fd7a8150SMike Barcroft */ 236654b369c1SPawel Jakub Dawidek struct prison * 2367fd7a8150SMike Barcroft prison_find(int prid) 2368fd7a8150SMike Barcroft { 2369fd7a8150SMike Barcroft struct prison *pr; 2370fd7a8150SMike Barcroft 2371dc68a633SPawel Jakub Dawidek sx_assert(&allprison_lock, SX_LOCKED); 2372b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 2373fd7a8150SMike Barcroft if (pr->pr_id == prid) { 2374fd7a8150SMike Barcroft mtx_lock(&pr->pr_mtx); 2375b38ff370SJamie Gritton if (pr->pr_ref > 0) 2376fd7a8150SMike Barcroft return (pr); 2377b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2378fd7a8150SMike Barcroft } 2379fd7a8150SMike Barcroft } 2380fd7a8150SMike Barcroft return (NULL); 2381fd7a8150SMike Barcroft } 2382fd7a8150SMike Barcroft 2383b38ff370SJamie Gritton /* 23840304c731SJamie Gritton * Find a prison that is a descendant of mypr. Returns a locked prison or NULL. 2385b38ff370SJamie Gritton */ 2386b38ff370SJamie Gritton struct prison * 23870304c731SJamie Gritton prison_find_child(struct prison *mypr, int prid) 2388b38ff370SJamie Gritton { 23890304c731SJamie Gritton struct prison *pr; 23900304c731SJamie Gritton int descend; 2391b38ff370SJamie Gritton 2392b38ff370SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 23930304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 23940304c731SJamie Gritton if (pr->pr_id == prid) { 23950304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 23960304c731SJamie Gritton if (pr->pr_ref > 0) 23970304c731SJamie Gritton return (pr); 23980304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 23990304c731SJamie Gritton } 24000304c731SJamie Gritton } 24010304c731SJamie Gritton return (NULL); 24020304c731SJamie Gritton } 24030304c731SJamie Gritton 24040304c731SJamie Gritton /* 24050304c731SJamie Gritton * Look for the name relative to mypr. Returns a locked prison or NULL. 24060304c731SJamie Gritton */ 24070304c731SJamie Gritton struct prison * 24080304c731SJamie Gritton prison_find_name(struct prison *mypr, const char *name) 24090304c731SJamie Gritton { 24100304c731SJamie Gritton struct prison *pr, *deadpr; 24110304c731SJamie Gritton size_t mylen; 24120304c731SJamie Gritton int descend; 24130304c731SJamie Gritton 24140304c731SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 24150304c731SJamie Gritton mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1; 2416b38ff370SJamie Gritton again: 2417b38ff370SJamie Gritton deadpr = NULL; 24180304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 24190304c731SJamie Gritton if (!strcmp(pr->pr_name + mylen, name)) { 2420b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 2421b38ff370SJamie Gritton if (pr->pr_ref > 0) { 2422b38ff370SJamie Gritton if (pr->pr_uref > 0) 2423b38ff370SJamie Gritton return (pr); 2424b38ff370SJamie Gritton deadpr = pr; 2425b38ff370SJamie Gritton } 2426b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2427b38ff370SJamie Gritton } 2428b38ff370SJamie Gritton } 24290304c731SJamie Gritton /* There was no valid prison - perhaps there was a dying one. */ 2430b38ff370SJamie Gritton if (deadpr != NULL) { 2431b38ff370SJamie Gritton mtx_lock(&deadpr->pr_mtx); 2432b38ff370SJamie Gritton if (deadpr->pr_ref == 0) { 2433b38ff370SJamie Gritton mtx_unlock(&deadpr->pr_mtx); 2434b38ff370SJamie Gritton goto again; 2435b38ff370SJamie Gritton } 2436b38ff370SJamie Gritton } 2437b38ff370SJamie Gritton return (deadpr); 2438b38ff370SJamie Gritton } 2439b38ff370SJamie Gritton 2440b38ff370SJamie Gritton /* 24410304c731SJamie Gritton * See if a prison has the specific flag set. 24420304c731SJamie Gritton */ 24430304c731SJamie Gritton int 24440304c731SJamie Gritton prison_flag(struct ucred *cred, unsigned flag) 24450304c731SJamie Gritton { 24460304c731SJamie Gritton 24470304c731SJamie Gritton /* This is an atomic read, so no locking is necessary. */ 24480304c731SJamie Gritton return (cred->cr_prison->pr_flags & flag); 24490304c731SJamie Gritton } 24500304c731SJamie Gritton 24510304c731SJamie Gritton int 24520304c731SJamie Gritton prison_allow(struct ucred *cred, unsigned flag) 24530304c731SJamie Gritton { 24540304c731SJamie Gritton 24550304c731SJamie Gritton /* This is an atomic read, so no locking is necessary. */ 24560304c731SJamie Gritton return (cred->cr_prison->pr_allow & flag); 24570304c731SJamie Gritton } 24580304c731SJamie Gritton 24590304c731SJamie Gritton /* 2460b38ff370SJamie Gritton * Remove a prison reference. If that was the last reference, remove the 2461b38ff370SJamie Gritton * prison itself - but not in this context in case there are locks held. 2462b38ff370SJamie Gritton */ 246391421ba2SRobert Watson void 24641ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr) 246591421ba2SRobert Watson { 246691421ba2SRobert Watson 24671ba4a712SPawel Jakub Dawidek mtx_assert(&pr->pr_mtx, MA_OWNED); 246891421ba2SRobert Watson pr->pr_ref--; 246991421ba2SRobert Watson if (pr->pr_ref == 0) { 247001137630SRobert Watson mtx_unlock(&pr->pr_mtx); 2471c2cda609SPawel Jakub Dawidek TASK_INIT(&pr->pr_task, 0, prison_complete, pr); 2472c2cda609SPawel Jakub Dawidek taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 2473c2cda609SPawel Jakub Dawidek return; 2474c2cda609SPawel Jakub Dawidek } 2475c2cda609SPawel Jakub Dawidek mtx_unlock(&pr->pr_mtx); 2476c2cda609SPawel Jakub Dawidek } 2477c2cda609SPawel Jakub Dawidek 24781ba4a712SPawel Jakub Dawidek void 24791ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr) 24801ba4a712SPawel Jakub Dawidek { 24811ba4a712SPawel Jakub Dawidek 24821ba4a712SPawel Jakub Dawidek mtx_lock(&pr->pr_mtx); 24831ba4a712SPawel Jakub Dawidek prison_free_locked(pr); 24841ba4a712SPawel Jakub Dawidek } 24851ba4a712SPawel Jakub Dawidek 2486c2cda609SPawel Jakub Dawidek static void 2487c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending) 2488c2cda609SPawel Jakub Dawidek { 2489b38ff370SJamie Gritton 2490b38ff370SJamie Gritton prison_deref((struct prison *)context, 0); 2491b38ff370SJamie Gritton } 2492b38ff370SJamie Gritton 2493b38ff370SJamie Gritton /* 2494b38ff370SJamie Gritton * Remove a prison reference (usually). This internal version assumes no 2495b38ff370SJamie Gritton * mutexes are held, except perhaps the prison itself. If there are no more 2496b38ff370SJamie Gritton * references, release and delist the prison. On completion, the prison lock 2497b38ff370SJamie Gritton * and the allprison lock are both unlocked. 2498b38ff370SJamie Gritton */ 2499b38ff370SJamie Gritton static void 2500b38ff370SJamie Gritton prison_deref(struct prison *pr, int flags) 2501b38ff370SJamie Gritton { 25020304c731SJamie Gritton struct prison *ppr, *tpr; 2503c2cda609SPawel Jakub Dawidek int vfslocked; 2504c2cda609SPawel Jakub Dawidek 2505b38ff370SJamie Gritton if (!(flags & PD_LOCKED)) 2506b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 25070304c731SJamie Gritton /* Decrement the user references in a separate loop. */ 2508b38ff370SJamie Gritton if (flags & PD_DEUREF) { 25090304c731SJamie Gritton for (tpr = pr;; tpr = tpr->pr_parent) { 25100304c731SJamie Gritton if (tpr != pr) 25110304c731SJamie Gritton mtx_lock(&tpr->pr_mtx); 25120304c731SJamie Gritton if (--tpr->pr_uref > 0) 25130304c731SJamie Gritton break; 25140304c731SJamie Gritton KASSERT(tpr != &prison0, ("prison0 pr_uref=0")); 25150304c731SJamie Gritton mtx_unlock(&tpr->pr_mtx); 25160304c731SJamie Gritton } 2517b38ff370SJamie Gritton /* Done if there were only user references to remove. */ 2518b38ff370SJamie Gritton if (!(flags & PD_DEREF)) { 25190304c731SJamie Gritton mtx_unlock(&tpr->pr_mtx); 2520b38ff370SJamie Gritton if (flags & PD_LIST_SLOCKED) 2521b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2522b38ff370SJamie Gritton else if (flags & PD_LIST_XLOCKED) 2523b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2524b38ff370SJamie Gritton return; 2525b38ff370SJamie Gritton } 25260304c731SJamie Gritton if (tpr != pr) { 25270304c731SJamie Gritton mtx_unlock(&tpr->pr_mtx); 25280304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 2529b38ff370SJamie Gritton } 25300304c731SJamie Gritton } 25310304c731SJamie Gritton 25320304c731SJamie Gritton for (;;) { 2533b38ff370SJamie Gritton if (flags & PD_DEREF) 2534b38ff370SJamie Gritton pr->pr_ref--; 2535b38ff370SJamie Gritton /* If the prison still has references, nothing else to do. */ 2536b38ff370SJamie Gritton if (pr->pr_ref > 0) { 2537b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2538b38ff370SJamie Gritton if (flags & PD_LIST_SLOCKED) 2539b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2540b38ff370SJamie Gritton else if (flags & PD_LIST_XLOCKED) 2541b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2542b38ff370SJamie Gritton return; 2543b38ff370SJamie Gritton } 2544c2cda609SPawel Jakub Dawidek 2545b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2546b38ff370SJamie Gritton if (flags & PD_LIST_SLOCKED) { 2547b38ff370SJamie Gritton if (!sx_try_upgrade(&allprison_lock)) { 2548b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2549c2cda609SPawel Jakub Dawidek sx_xlock(&allprison_lock); 2550b38ff370SJamie Gritton } 2551b38ff370SJamie Gritton } else if (!(flags & PD_LIST_XLOCKED)) 2552b38ff370SJamie Gritton sx_xlock(&allprison_lock); 2553b38ff370SJamie Gritton 2554b38ff370SJamie Gritton TAILQ_REMOVE(&allprison, pr, pr_list); 25550304c731SJamie Gritton LIST_REMOVE(pr, pr_sibling); 25560304c731SJamie Gritton ppr = pr->pr_parent; 25570304c731SJamie Gritton for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 2558b97457e2SJamie Gritton tpr->pr_childcount--; 25590304c731SJamie Gritton sx_downgrade(&allprison_lock); 25601ba4a712SPawel Jakub Dawidek 2561679e1390SJamie Gritton #ifdef VIMAGE 2562679e1390SJamie Gritton if (pr->pr_flags & PR_VNET) 2563679e1390SJamie Gritton vnet_destroy(pr->pr_vnet); 2564679e1390SJamie Gritton #endif 2565b38ff370SJamie Gritton if (pr->pr_root != NULL) { 2566453f7d53SChristian S.J. Peron vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount); 2567b3059e09SRobert Watson vrele(pr->pr_root); 2568453f7d53SChristian S.J. Peron VFS_UNLOCK_GIANT(vfslocked); 2569b38ff370SJamie Gritton } 2570b3059e09SRobert Watson mtx_destroy(&pr->pr_mtx); 2571413628a7SBjoern A. Zeeb #ifdef INET 2572413628a7SBjoern A. Zeeb free(pr->pr_ip4, M_PRISON); 2573413628a7SBjoern A. Zeeb #endif 2574b38ff370SJamie Gritton #ifdef INET6 2575b38ff370SJamie Gritton free(pr->pr_ip6, M_PRISON); 2576b38ff370SJamie Gritton #endif 2577b38ff370SJamie Gritton if (pr->pr_cpuset != NULL) 2578b38ff370SJamie Gritton cpuset_rel(pr->pr_cpuset); 2579b38ff370SJamie Gritton osd_jail_exit(pr); 25801ede983cSDag-Erling Smørgrav free(pr, M_PRISON); 25810304c731SJamie Gritton 25820304c731SJamie Gritton /* Removing a prison frees a reference on its parent. */ 25830304c731SJamie Gritton pr = ppr; 25840304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 25850304c731SJamie Gritton flags = PD_DEREF | PD_LIST_SLOCKED; 25860304c731SJamie Gritton } 2587b3059e09SRobert Watson } 2588b3059e09SRobert Watson 258991421ba2SRobert Watson void 25901ba4a712SPawel Jakub Dawidek prison_hold_locked(struct prison *pr) 25911ba4a712SPawel Jakub Dawidek { 25921ba4a712SPawel Jakub Dawidek 25931ba4a712SPawel Jakub Dawidek mtx_assert(&pr->pr_mtx, MA_OWNED); 25941ba4a712SPawel Jakub Dawidek KASSERT(pr->pr_ref > 0, 2595af7bd9a4SJamie Gritton ("Trying to hold dead prison (jid=%d).", pr->pr_id)); 25961ba4a712SPawel Jakub Dawidek pr->pr_ref++; 25971ba4a712SPawel Jakub Dawidek } 25981ba4a712SPawel Jakub Dawidek 25991ba4a712SPawel Jakub Dawidek void 260091421ba2SRobert Watson prison_hold(struct prison *pr) 260191421ba2SRobert Watson { 260291421ba2SRobert Watson 260301137630SRobert Watson mtx_lock(&pr->pr_mtx); 26041ba4a712SPawel Jakub Dawidek prison_hold_locked(pr); 260501137630SRobert Watson mtx_unlock(&pr->pr_mtx); 260601137630SRobert Watson } 260701137630SRobert Watson 2608413628a7SBjoern A. Zeeb void 2609413628a7SBjoern A. Zeeb prison_proc_hold(struct prison *pr) 261001137630SRobert Watson { 261101137630SRobert Watson 2612413628a7SBjoern A. Zeeb mtx_lock(&pr->pr_mtx); 2613b38ff370SJamie Gritton KASSERT(pr->pr_uref > 0, 2614b38ff370SJamie Gritton ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id)); 2615b38ff370SJamie Gritton pr->pr_uref++; 2616413628a7SBjoern A. Zeeb mtx_unlock(&pr->pr_mtx); 261775c13541SPoul-Henning Kamp } 261875c13541SPoul-Henning Kamp 261975c13541SPoul-Henning Kamp void 2620413628a7SBjoern A. Zeeb prison_proc_free(struct prison *pr) 262175c13541SPoul-Henning Kamp { 2622413628a7SBjoern A. Zeeb 2623413628a7SBjoern A. Zeeb mtx_lock(&pr->pr_mtx); 2624b38ff370SJamie Gritton KASSERT(pr->pr_uref > 0, 2625b38ff370SJamie Gritton ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id)); 2626b38ff370SJamie Gritton prison_deref(pr, PD_DEUREF | PD_LOCKED); 2627413628a7SBjoern A. Zeeb } 2628413628a7SBjoern A. Zeeb 2629413628a7SBjoern A. Zeeb 2630413628a7SBjoern A. Zeeb #ifdef INET 2631413628a7SBjoern A. Zeeb /* 26320304c731SJamie Gritton * Restrict a prison's IP address list with its parent's, possibly replacing 26330304c731SJamie Gritton * it. Return true if the replacement buffer was used (or would have been). 26340304c731SJamie Gritton */ 26350304c731SJamie Gritton static int 26360304c731SJamie Gritton prison_restrict_ip4(struct prison *pr, struct in_addr *newip4) 26370304c731SJamie Gritton { 26380304c731SJamie Gritton int ii, ij, used; 26390304c731SJamie Gritton struct prison *ppr; 26400304c731SJamie Gritton 26410304c731SJamie Gritton ppr = pr->pr_parent; 26420304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4_USER)) { 26430304c731SJamie Gritton /* This has no user settings, so just copy the parent's list. */ 26440304c731SJamie Gritton if (pr->pr_ip4s < ppr->pr_ip4s) { 26450304c731SJamie Gritton /* 26460304c731SJamie Gritton * There's no room for the parent's list. Use the 26470304c731SJamie Gritton * new list buffer, which is assumed to be big enough 26480304c731SJamie Gritton * (if it was passed). If there's no buffer, try to 26490304c731SJamie Gritton * allocate one. 26500304c731SJamie Gritton */ 26510304c731SJamie Gritton used = 1; 26520304c731SJamie Gritton if (newip4 == NULL) { 26530304c731SJamie Gritton newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4), 26540304c731SJamie Gritton M_PRISON, M_NOWAIT); 26550304c731SJamie Gritton if (newip4 != NULL) 26560304c731SJamie Gritton used = 0; 26570304c731SJamie Gritton } 26580304c731SJamie Gritton if (newip4 != NULL) { 26590304c731SJamie Gritton bcopy(ppr->pr_ip4, newip4, 26600304c731SJamie Gritton ppr->pr_ip4s * sizeof(*newip4)); 26610304c731SJamie Gritton free(pr->pr_ip4, M_PRISON); 26620304c731SJamie Gritton pr->pr_ip4 = newip4; 26630304c731SJamie Gritton pr->pr_ip4s = ppr->pr_ip4s; 26640304c731SJamie Gritton pr->pr_flags |= PR_IP4; 26650304c731SJamie Gritton } 26660304c731SJamie Gritton return (used); 26670304c731SJamie Gritton } 26680304c731SJamie Gritton pr->pr_ip4s = ppr->pr_ip4s; 26690304c731SJamie Gritton if (pr->pr_ip4s > 0) 26700304c731SJamie Gritton bcopy(ppr->pr_ip4, pr->pr_ip4, 26710304c731SJamie Gritton pr->pr_ip4s * sizeof(*newip4)); 26720304c731SJamie Gritton else if (pr->pr_ip4 != NULL) { 26730304c731SJamie Gritton free(pr->pr_ip4, M_PRISON); 26740304c731SJamie Gritton pr->pr_ip4 = NULL; 26750304c731SJamie Gritton } 26760304c731SJamie Gritton pr->pr_flags = 26770304c731SJamie Gritton (pr->pr_flags & ~PR_IP4) | (ppr->pr_flags & PR_IP4); 26780304c731SJamie Gritton } else if (pr->pr_ip4s > 0 && (ppr->pr_flags & PR_IP4)) { 26790304c731SJamie Gritton /* Remove addresses that aren't in the parent. */ 26800304c731SJamie Gritton for (ij = 0; ij < ppr->pr_ip4s; ij++) 26810304c731SJamie Gritton if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 26820304c731SJamie Gritton break; 26830304c731SJamie Gritton if (ij < ppr->pr_ip4s) 26840304c731SJamie Gritton ii = 1; 26850304c731SJamie Gritton else { 26860304c731SJamie Gritton bcopy(pr->pr_ip4 + 1, pr->pr_ip4, 26870304c731SJamie Gritton --pr->pr_ip4s * sizeof(*pr->pr_ip4)); 26880304c731SJamie Gritton ii = 0; 26890304c731SJamie Gritton } 26900304c731SJamie Gritton for (ij = 1; ii < pr->pr_ip4s; ) { 26910304c731SJamie Gritton if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) { 26920304c731SJamie Gritton ii++; 26930304c731SJamie Gritton continue; 26940304c731SJamie Gritton } 26950304c731SJamie Gritton switch (ij >= ppr->pr_ip4s ? -1 : 26960304c731SJamie Gritton qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) { 26970304c731SJamie Gritton case -1: 26980304c731SJamie Gritton bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii, 26990304c731SJamie Gritton (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4)); 27000304c731SJamie Gritton break; 27010304c731SJamie Gritton case 0: 27020304c731SJamie Gritton ii++; 27030304c731SJamie Gritton ij++; 27040304c731SJamie Gritton break; 27050304c731SJamie Gritton case 1: 27060304c731SJamie Gritton ij++; 27070304c731SJamie Gritton break; 27080304c731SJamie Gritton } 27090304c731SJamie Gritton } 27100304c731SJamie Gritton if (pr->pr_ip4s == 0) { 27117cbf7213SJamie Gritton pr->pr_flags |= PR_IP4_DISABLE; 27120304c731SJamie Gritton free(pr->pr_ip4, M_PRISON); 27130304c731SJamie Gritton pr->pr_ip4 = NULL; 27140304c731SJamie Gritton } 27150304c731SJamie Gritton } 27160304c731SJamie Gritton return (0); 27170304c731SJamie Gritton } 27180304c731SJamie Gritton 27190304c731SJamie Gritton /* 2720413628a7SBjoern A. Zeeb * Pass back primary IPv4 address of this jail. 2721413628a7SBjoern A. Zeeb * 27220304c731SJamie Gritton * If not restricted return success but do not alter the address. Caller has 27230304c731SJamie Gritton * to make sure to initialize it correctly (e.g. INADDR_ANY). 2724413628a7SBjoern A. Zeeb * 2725b89e82ddSJamie Gritton * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4. 2726b89e82ddSJamie Gritton * Address returned in NBO. 2727413628a7SBjoern A. Zeeb */ 2728413628a7SBjoern A. Zeeb int 27291cecba0fSBjoern A. Zeeb prison_get_ip4(struct ucred *cred, struct in_addr *ia) 2730413628a7SBjoern A. Zeeb { 2731b38ff370SJamie Gritton struct prison *pr; 2732413628a7SBjoern A. Zeeb 2733413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2734413628a7SBjoern A. Zeeb KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 273575c13541SPoul-Henning Kamp 2736b38ff370SJamie Gritton pr = cred->cr_prison; 27370304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) 27380304c731SJamie Gritton return (0); 2739b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 27400304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) { 27410304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 27420304c731SJamie Gritton return (0); 27430304c731SJamie Gritton } 2744b38ff370SJamie Gritton if (pr->pr_ip4 == NULL) { 2745b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2746b89e82ddSJamie Gritton return (EAFNOSUPPORT); 2747b38ff370SJamie Gritton } 2748413628a7SBjoern A. Zeeb 2749b38ff370SJamie Gritton ia->s_addr = pr->pr_ip4[0].s_addr; 2750b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2751413628a7SBjoern A. Zeeb return (0); 275275c13541SPoul-Henning Kamp } 2753413628a7SBjoern A. Zeeb 2754413628a7SBjoern A. Zeeb /* 27550304c731SJamie Gritton * Return true if pr1 and pr2 have the same IPv4 address restrictions. 27560304c731SJamie Gritton */ 27570304c731SJamie Gritton int 27580304c731SJamie Gritton prison_equal_ip4(struct prison *pr1, struct prison *pr2) 27590304c731SJamie Gritton { 27600304c731SJamie Gritton 27610304c731SJamie Gritton if (pr1 == pr2) 27620304c731SJamie Gritton return (1); 27630304c731SJamie Gritton 27640304c731SJamie Gritton /* 27650304c731SJamie Gritton * jail_set maintains an exclusive hold on allprison_lock while it 27660304c731SJamie Gritton * changes the IP addresses, so only a shared hold is needed. This is 27670304c731SJamie Gritton * easier than locking the two prisons which would require finding the 27680304c731SJamie Gritton * proper locking order and end up needing allprison_lock anyway. 27690304c731SJamie Gritton */ 27700304c731SJamie Gritton sx_slock(&allprison_lock); 2771bdfc8cc4SJamie Gritton while (pr1 != &prison0 && 2772bdfc8cc4SJamie Gritton #ifdef VIMAGE 2773bdfc8cc4SJamie Gritton !(pr1->pr_flags & PR_VNET) && 2774bdfc8cc4SJamie Gritton #endif 2775bdfc8cc4SJamie Gritton !(pr1->pr_flags & PR_IP4_USER)) 27760304c731SJamie Gritton pr1 = pr1->pr_parent; 2777bdfc8cc4SJamie Gritton while (pr2 != &prison0 && 2778bdfc8cc4SJamie Gritton #ifdef VIMAGE 2779bdfc8cc4SJamie Gritton !(pr2->pr_flags & PR_VNET) && 2780bdfc8cc4SJamie Gritton #endif 2781bdfc8cc4SJamie Gritton !(pr2->pr_flags & PR_IP4_USER)) 27820304c731SJamie Gritton pr2 = pr2->pr_parent; 27830304c731SJamie Gritton sx_sunlock(&allprison_lock); 27840304c731SJamie Gritton return (pr1 == pr2); 27850304c731SJamie Gritton } 27860304c731SJamie Gritton 27870304c731SJamie Gritton /* 2788413628a7SBjoern A. Zeeb * Make sure our (source) address is set to something meaningful to this 2789413628a7SBjoern A. Zeeb * jail. 2790413628a7SBjoern A. Zeeb * 27910304c731SJamie Gritton * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail, 27920304c731SJamie Gritton * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 27930304c731SJamie Gritton * doesn't allow IPv4. Address passed in in NBO and returned in NBO. 2794413628a7SBjoern A. Zeeb */ 2795413628a7SBjoern A. Zeeb int 2796413628a7SBjoern A. Zeeb prison_local_ip4(struct ucred *cred, struct in_addr *ia) 2797413628a7SBjoern A. Zeeb { 2798b38ff370SJamie Gritton struct prison *pr; 2799413628a7SBjoern A. Zeeb struct in_addr ia0; 2800b38ff370SJamie Gritton int error; 2801413628a7SBjoern A. Zeeb 2802413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2803413628a7SBjoern A. Zeeb KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2804413628a7SBjoern A. Zeeb 2805b38ff370SJamie Gritton pr = cred->cr_prison; 28060304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) 28070304c731SJamie Gritton return (0); 2808b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 28090304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) { 28100304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 28110304c731SJamie Gritton return (0); 28120304c731SJamie Gritton } 2813b38ff370SJamie Gritton if (pr->pr_ip4 == NULL) { 2814b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2815b89e82ddSJamie Gritton return (EAFNOSUPPORT); 2816b38ff370SJamie Gritton } 2817413628a7SBjoern A. Zeeb 2818413628a7SBjoern A. Zeeb ia0.s_addr = ntohl(ia->s_addr); 2819413628a7SBjoern A. Zeeb if (ia0.s_addr == INADDR_LOOPBACK) { 2820b38ff370SJamie Gritton ia->s_addr = pr->pr_ip4[0].s_addr; 2821b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2822413628a7SBjoern A. Zeeb return (0); 2823413628a7SBjoern A. Zeeb } 2824413628a7SBjoern A. Zeeb 2825b89e82ddSJamie Gritton if (ia0.s_addr == INADDR_ANY) { 2826413628a7SBjoern A. Zeeb /* 2827413628a7SBjoern A. Zeeb * In case there is only 1 IPv4 address, bind directly. 2828413628a7SBjoern A. Zeeb */ 2829b38ff370SJamie Gritton if (pr->pr_ip4s == 1) 2830b38ff370SJamie Gritton ia->s_addr = pr->pr_ip4[0].s_addr; 2831b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2832413628a7SBjoern A. Zeeb return (0); 2833413628a7SBjoern A. Zeeb } 2834413628a7SBjoern A. Zeeb 2835b38ff370SJamie Gritton error = _prison_check_ip4(pr, ia); 2836b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2837b38ff370SJamie Gritton return (error); 2838413628a7SBjoern A. Zeeb } 2839413628a7SBjoern A. Zeeb 2840413628a7SBjoern A. Zeeb /* 2841413628a7SBjoern A. Zeeb * Rewrite destination address in case we will connect to loopback address. 2842413628a7SBjoern A. Zeeb * 2843b89e82ddSJamie Gritton * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4. 2844b89e82ddSJamie Gritton * Address passed in in NBO and returned in NBO. 2845413628a7SBjoern A. Zeeb */ 2846413628a7SBjoern A. Zeeb int 2847413628a7SBjoern A. Zeeb prison_remote_ip4(struct ucred *cred, struct in_addr *ia) 2848413628a7SBjoern A. Zeeb { 2849b38ff370SJamie Gritton struct prison *pr; 2850413628a7SBjoern A. Zeeb 2851413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2852413628a7SBjoern A. Zeeb KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2853413628a7SBjoern A. Zeeb 2854b38ff370SJamie Gritton pr = cred->cr_prison; 28550304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) 28560304c731SJamie Gritton return (0); 2857b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 28580304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) { 28590304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 28600304c731SJamie Gritton return (0); 28610304c731SJamie Gritton } 2862b38ff370SJamie Gritton if (pr->pr_ip4 == NULL) { 2863b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2864b89e82ddSJamie Gritton return (EAFNOSUPPORT); 2865b38ff370SJamie Gritton } 2866b89e82ddSJamie Gritton 2867413628a7SBjoern A. Zeeb if (ntohl(ia->s_addr) == INADDR_LOOPBACK) { 2868b38ff370SJamie Gritton ia->s_addr = pr->pr_ip4[0].s_addr; 2869b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2870413628a7SBjoern A. Zeeb return (0); 2871413628a7SBjoern A. Zeeb } 2872413628a7SBjoern A. Zeeb 2873413628a7SBjoern A. Zeeb /* 2874413628a7SBjoern A. Zeeb * Return success because nothing had to be changed. 2875413628a7SBjoern A. Zeeb */ 2876b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2877413628a7SBjoern A. Zeeb return (0); 2878413628a7SBjoern A. Zeeb } 2879413628a7SBjoern A. Zeeb 2880413628a7SBjoern A. Zeeb /* 2881b89e82ddSJamie Gritton * Check if given address belongs to the jail referenced by cred/prison. 2882413628a7SBjoern A. Zeeb * 28830304c731SJamie Gritton * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail, 28840304c731SJamie Gritton * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 28850304c731SJamie Gritton * doesn't allow IPv4. Address passed in in NBO. 2886413628a7SBjoern A. Zeeb */ 2887413628a7SBjoern A. Zeeb static int 2888413628a7SBjoern A. Zeeb _prison_check_ip4(struct prison *pr, struct in_addr *ia) 2889413628a7SBjoern A. Zeeb { 2890413628a7SBjoern A. Zeeb int i, a, z, d; 2891413628a7SBjoern A. Zeeb 2892413628a7SBjoern A. Zeeb /* 2893413628a7SBjoern A. Zeeb * Check the primary IP. 2894413628a7SBjoern A. Zeeb */ 2895413628a7SBjoern A. Zeeb if (pr->pr_ip4[0].s_addr == ia->s_addr) 2896b89e82ddSJamie Gritton return (0); 2897413628a7SBjoern A. Zeeb 2898413628a7SBjoern A. Zeeb /* 2899413628a7SBjoern A. Zeeb * All the other IPs are sorted so we can do a binary search. 2900413628a7SBjoern A. Zeeb */ 2901413628a7SBjoern A. Zeeb a = 0; 2902413628a7SBjoern A. Zeeb z = pr->pr_ip4s - 2; 2903413628a7SBjoern A. Zeeb while (a <= z) { 2904413628a7SBjoern A. Zeeb i = (a + z) / 2; 2905413628a7SBjoern A. Zeeb d = qcmp_v4(&pr->pr_ip4[i+1], ia); 2906413628a7SBjoern A. Zeeb if (d > 0) 2907413628a7SBjoern A. Zeeb z = i - 1; 2908413628a7SBjoern A. Zeeb else if (d < 0) 2909413628a7SBjoern A. Zeeb a = i + 1; 2910413628a7SBjoern A. Zeeb else 2911413628a7SBjoern A. Zeeb return (0); 291275c13541SPoul-Henning Kamp } 291375c13541SPoul-Henning Kamp 2914b89e82ddSJamie Gritton return (EADDRNOTAVAIL); 2915b89e82ddSJamie Gritton } 2916b89e82ddSJamie Gritton 291775c13541SPoul-Henning Kamp int 2918413628a7SBjoern A. Zeeb prison_check_ip4(struct ucred *cred, struct in_addr *ia) 2919413628a7SBjoern A. Zeeb { 2920b38ff370SJamie Gritton struct prison *pr; 2921b38ff370SJamie Gritton int error; 2922413628a7SBjoern A. Zeeb 2923413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2924413628a7SBjoern A. Zeeb KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2925413628a7SBjoern A. Zeeb 2926b38ff370SJamie Gritton pr = cred->cr_prison; 29270304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) 29280304c731SJamie Gritton return (0); 2929b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 29300304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) { 29310304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 29320304c731SJamie Gritton return (0); 29330304c731SJamie Gritton } 2934b38ff370SJamie Gritton if (pr->pr_ip4 == NULL) { 2935b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2936b89e82ddSJamie Gritton return (EAFNOSUPPORT); 2937b38ff370SJamie Gritton } 2938413628a7SBjoern A. Zeeb 2939b38ff370SJamie Gritton error = _prison_check_ip4(pr, ia); 2940b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2941b38ff370SJamie Gritton return (error); 2942413628a7SBjoern A. Zeeb } 2943413628a7SBjoern A. Zeeb #endif 2944413628a7SBjoern A. Zeeb 2945413628a7SBjoern A. Zeeb #ifdef INET6 29460304c731SJamie Gritton static int 29470304c731SJamie Gritton prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6) 29480304c731SJamie Gritton { 29490304c731SJamie Gritton int ii, ij, used; 29500304c731SJamie Gritton struct prison *ppr; 29510304c731SJamie Gritton 29520304c731SJamie Gritton ppr = pr->pr_parent; 29530304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6_USER)) { 29540304c731SJamie Gritton /* This has no user settings, so just copy the parent's list. */ 29550304c731SJamie Gritton if (pr->pr_ip6s < ppr->pr_ip6s) { 29560304c731SJamie Gritton /* 29570304c731SJamie Gritton * There's no room for the parent's list. Use the 29580304c731SJamie Gritton * new list buffer, which is assumed to be big enough 29590304c731SJamie Gritton * (if it was passed). If there's no buffer, try to 29600304c731SJamie Gritton * allocate one. 29610304c731SJamie Gritton */ 29620304c731SJamie Gritton used = 1; 29630304c731SJamie Gritton if (newip6 == NULL) { 29640304c731SJamie Gritton newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6), 29650304c731SJamie Gritton M_PRISON, M_NOWAIT); 29660304c731SJamie Gritton if (newip6 != NULL) 29670304c731SJamie Gritton used = 0; 29680304c731SJamie Gritton } 29690304c731SJamie Gritton if (newip6 != NULL) { 29700304c731SJamie Gritton bcopy(ppr->pr_ip6, newip6, 29710304c731SJamie Gritton ppr->pr_ip6s * sizeof(*newip6)); 29720304c731SJamie Gritton free(pr->pr_ip6, M_PRISON); 29730304c731SJamie Gritton pr->pr_ip6 = newip6; 29740304c731SJamie Gritton pr->pr_ip6s = ppr->pr_ip6s; 29750304c731SJamie Gritton pr->pr_flags |= PR_IP6; 29760304c731SJamie Gritton } 29770304c731SJamie Gritton return (used); 29780304c731SJamie Gritton } 29790304c731SJamie Gritton pr->pr_ip6s = ppr->pr_ip6s; 29800304c731SJamie Gritton if (pr->pr_ip6s > 0) 29810304c731SJamie Gritton bcopy(ppr->pr_ip6, pr->pr_ip6, 29820304c731SJamie Gritton pr->pr_ip6s * sizeof(*newip6)); 29830304c731SJamie Gritton else if (pr->pr_ip6 != NULL) { 29840304c731SJamie Gritton free(pr->pr_ip6, M_PRISON); 29850304c731SJamie Gritton pr->pr_ip6 = NULL; 29860304c731SJamie Gritton } 29870304c731SJamie Gritton pr->pr_flags = 29880304c731SJamie Gritton (pr->pr_flags & ~PR_IP6) | (ppr->pr_flags & PR_IP6); 29890304c731SJamie Gritton } else if (pr->pr_ip6s > 0 && (ppr->pr_flags & PR_IP6)) { 29900304c731SJamie Gritton /* Remove addresses that aren't in the parent. */ 29910304c731SJamie Gritton for (ij = 0; ij < ppr->pr_ip6s; ij++) 29920304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], 29930304c731SJamie Gritton &ppr->pr_ip6[ij])) 29940304c731SJamie Gritton break; 29950304c731SJamie Gritton if (ij < ppr->pr_ip6s) 29960304c731SJamie Gritton ii = 1; 29970304c731SJamie Gritton else { 29980304c731SJamie Gritton bcopy(pr->pr_ip6 + 1, pr->pr_ip6, 29990304c731SJamie Gritton --pr->pr_ip6s * sizeof(*pr->pr_ip6)); 30000304c731SJamie Gritton ii = 0; 30010304c731SJamie Gritton } 30020304c731SJamie Gritton for (ij = 1; ii < pr->pr_ip6s; ) { 30030304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii], 30040304c731SJamie Gritton &ppr->pr_ip6[0])) { 30050304c731SJamie Gritton ii++; 30060304c731SJamie Gritton continue; 30070304c731SJamie Gritton } 30080304c731SJamie Gritton switch (ij >= ppr->pr_ip4s ? -1 : 30090304c731SJamie Gritton qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) { 30100304c731SJamie Gritton case -1: 30110304c731SJamie Gritton bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii, 30120304c731SJamie Gritton (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6)); 30130304c731SJamie Gritton break; 30140304c731SJamie Gritton case 0: 30150304c731SJamie Gritton ii++; 30160304c731SJamie Gritton ij++; 30170304c731SJamie Gritton break; 30180304c731SJamie Gritton case 1: 30190304c731SJamie Gritton ij++; 30200304c731SJamie Gritton break; 30210304c731SJamie Gritton } 30220304c731SJamie Gritton } 30230304c731SJamie Gritton if (pr->pr_ip6s == 0) { 30247cbf7213SJamie Gritton pr->pr_flags |= PR_IP6_DISABLE; 30250304c731SJamie Gritton free(pr->pr_ip6, M_PRISON); 30260304c731SJamie Gritton pr->pr_ip6 = NULL; 30270304c731SJamie Gritton } 30280304c731SJamie Gritton } 30290304c731SJamie Gritton return 0; 30300304c731SJamie Gritton } 30310304c731SJamie Gritton 3032413628a7SBjoern A. Zeeb /* 3033413628a7SBjoern A. Zeeb * Pass back primary IPv6 address for this jail. 3034413628a7SBjoern A. Zeeb * 30350304c731SJamie Gritton * If not restricted return success but do not alter the address. Caller has 30360304c731SJamie Gritton * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT). 3037413628a7SBjoern A. Zeeb * 3038b89e82ddSJamie Gritton * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6. 3039413628a7SBjoern A. Zeeb */ 3040413628a7SBjoern A. Zeeb int 30411cecba0fSBjoern A. Zeeb prison_get_ip6(struct ucred *cred, struct in6_addr *ia6) 3042413628a7SBjoern A. Zeeb { 3043b38ff370SJamie Gritton struct prison *pr; 3044413628a7SBjoern A. Zeeb 3045413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3046413628a7SBjoern A. Zeeb KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3047413628a7SBjoern A. Zeeb 3048b38ff370SJamie Gritton pr = cred->cr_prison; 30490304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) 30500304c731SJamie Gritton return (0); 3051b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 30520304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) { 30530304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 30540304c731SJamie Gritton return (0); 30550304c731SJamie Gritton } 3056b38ff370SJamie Gritton if (pr->pr_ip6 == NULL) { 3057b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3058b89e82ddSJamie Gritton return (EAFNOSUPPORT); 3059b38ff370SJamie Gritton } 3060b89e82ddSJamie Gritton 3061b38ff370SJamie Gritton bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3062b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3063413628a7SBjoern A. Zeeb return (0); 3064413628a7SBjoern A. Zeeb } 3065413628a7SBjoern A. Zeeb 3066413628a7SBjoern A. Zeeb /* 30670304c731SJamie Gritton * Return true if pr1 and pr2 have the same IPv6 address restrictions. 30680304c731SJamie Gritton */ 30690304c731SJamie Gritton int 30700304c731SJamie Gritton prison_equal_ip6(struct prison *pr1, struct prison *pr2) 30710304c731SJamie Gritton { 30720304c731SJamie Gritton 30730304c731SJamie Gritton if (pr1 == pr2) 30740304c731SJamie Gritton return (1); 30750304c731SJamie Gritton 30760304c731SJamie Gritton sx_slock(&allprison_lock); 3077bdfc8cc4SJamie Gritton while (pr1 != &prison0 && 3078bdfc8cc4SJamie Gritton #ifdef VIMAGE 3079bdfc8cc4SJamie Gritton !(pr1->pr_flags & PR_VNET) && 3080bdfc8cc4SJamie Gritton #endif 3081bdfc8cc4SJamie Gritton !(pr1->pr_flags & PR_IP6_USER)) 30820304c731SJamie Gritton pr1 = pr1->pr_parent; 3083bdfc8cc4SJamie Gritton while (pr2 != &prison0 && 3084bdfc8cc4SJamie Gritton #ifdef VIMAGE 3085bdfc8cc4SJamie Gritton !(pr2->pr_flags & PR_VNET) && 3086bdfc8cc4SJamie Gritton #endif 3087bdfc8cc4SJamie Gritton !(pr2->pr_flags & PR_IP6_USER)) 30880304c731SJamie Gritton pr2 = pr2->pr_parent; 30890304c731SJamie Gritton sx_sunlock(&allprison_lock); 30900304c731SJamie Gritton return (pr1 == pr2); 30910304c731SJamie Gritton } 30920304c731SJamie Gritton 30930304c731SJamie Gritton /* 3094413628a7SBjoern A. Zeeb * Make sure our (source) address is set to something meaningful to this jail. 3095413628a7SBjoern A. Zeeb * 3096413628a7SBjoern A. Zeeb * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0) 3097413628a7SBjoern A. Zeeb * when needed while binding. 3098413628a7SBjoern A. Zeeb * 30990304c731SJamie Gritton * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail, 31000304c731SJamie Gritton * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 31010304c731SJamie Gritton * doesn't allow IPv6. 3102413628a7SBjoern A. Zeeb */ 3103413628a7SBjoern A. Zeeb int 3104413628a7SBjoern A. Zeeb prison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only) 3105413628a7SBjoern A. Zeeb { 3106b38ff370SJamie Gritton struct prison *pr; 3107b38ff370SJamie Gritton int error; 3108413628a7SBjoern A. Zeeb 3109413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3110413628a7SBjoern A. Zeeb KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3111413628a7SBjoern A. Zeeb 3112b38ff370SJamie Gritton pr = cred->cr_prison; 31130304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) 31140304c731SJamie Gritton return (0); 3115b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 31160304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) { 31170304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 31180304c731SJamie Gritton return (0); 31190304c731SJamie Gritton } 3120b38ff370SJamie Gritton if (pr->pr_ip6 == NULL) { 3121b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3122b89e82ddSJamie Gritton return (EAFNOSUPPORT); 3123b38ff370SJamie Gritton } 3124b89e82ddSJamie Gritton 3125413628a7SBjoern A. Zeeb if (IN6_IS_ADDR_LOOPBACK(ia6)) { 3126b38ff370SJamie Gritton bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3127b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3128413628a7SBjoern A. Zeeb return (0); 3129413628a7SBjoern A. Zeeb } 3130413628a7SBjoern A. Zeeb 3131b89e82ddSJamie Gritton if (IN6_IS_ADDR_UNSPECIFIED(ia6)) { 3132413628a7SBjoern A. Zeeb /* 3133b89e82ddSJamie Gritton * In case there is only 1 IPv6 address, and v6only is true, 3134b89e82ddSJamie Gritton * then bind directly. 3135413628a7SBjoern A. Zeeb */ 3136b38ff370SJamie Gritton if (v6only != 0 && pr->pr_ip6s == 1) 3137b38ff370SJamie Gritton bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3138b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3139413628a7SBjoern A. Zeeb return (0); 3140413628a7SBjoern A. Zeeb } 3141b89e82ddSJamie Gritton 3142b38ff370SJamie Gritton error = _prison_check_ip6(pr, ia6); 3143b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3144b38ff370SJamie Gritton return (error); 3145413628a7SBjoern A. Zeeb } 3146413628a7SBjoern A. Zeeb 3147413628a7SBjoern A. Zeeb /* 3148413628a7SBjoern A. Zeeb * Rewrite destination address in case we will connect to loopback address. 3149413628a7SBjoern A. Zeeb * 3150b89e82ddSJamie Gritton * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6. 3151413628a7SBjoern A. Zeeb */ 3152413628a7SBjoern A. Zeeb int 3153413628a7SBjoern A. Zeeb prison_remote_ip6(struct ucred *cred, struct in6_addr *ia6) 3154413628a7SBjoern A. Zeeb { 3155b38ff370SJamie Gritton struct prison *pr; 3156413628a7SBjoern A. Zeeb 3157413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3158413628a7SBjoern A. Zeeb KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3159413628a7SBjoern A. Zeeb 3160b38ff370SJamie Gritton pr = cred->cr_prison; 31610304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) 31620304c731SJamie Gritton return (0); 3163b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 31640304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) { 31650304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 31660304c731SJamie Gritton return (0); 31670304c731SJamie Gritton } 3168b38ff370SJamie Gritton if (pr->pr_ip6 == NULL) { 3169b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3170b89e82ddSJamie Gritton return (EAFNOSUPPORT); 3171b38ff370SJamie Gritton } 3172b89e82ddSJamie Gritton 3173413628a7SBjoern A. Zeeb if (IN6_IS_ADDR_LOOPBACK(ia6)) { 3174b38ff370SJamie Gritton bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3175b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3176413628a7SBjoern A. Zeeb return (0); 3177413628a7SBjoern A. Zeeb } 3178413628a7SBjoern A. Zeeb 3179413628a7SBjoern A. Zeeb /* 3180413628a7SBjoern A. Zeeb * Return success because nothing had to be changed. 3181413628a7SBjoern A. Zeeb */ 3182b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3183413628a7SBjoern A. Zeeb return (0); 3184413628a7SBjoern A. Zeeb } 3185413628a7SBjoern A. Zeeb 3186413628a7SBjoern A. Zeeb /* 3187b89e82ddSJamie Gritton * Check if given address belongs to the jail referenced by cred/prison. 3188413628a7SBjoern A. Zeeb * 31890304c731SJamie Gritton * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail, 31900304c731SJamie Gritton * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 31910304c731SJamie Gritton * doesn't allow IPv6. 3192413628a7SBjoern A. Zeeb */ 3193413628a7SBjoern A. Zeeb static int 3194413628a7SBjoern A. Zeeb _prison_check_ip6(struct prison *pr, struct in6_addr *ia6) 3195413628a7SBjoern A. Zeeb { 3196413628a7SBjoern A. Zeeb int i, a, z, d; 3197413628a7SBjoern A. Zeeb 3198413628a7SBjoern A. Zeeb /* 3199413628a7SBjoern A. Zeeb * Check the primary IP. 3200413628a7SBjoern A. Zeeb */ 3201413628a7SBjoern A. Zeeb if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6)) 3202b89e82ddSJamie Gritton return (0); 3203413628a7SBjoern A. Zeeb 3204413628a7SBjoern A. Zeeb /* 3205413628a7SBjoern A. Zeeb * All the other IPs are sorted so we can do a binary search. 3206413628a7SBjoern A. Zeeb */ 3207413628a7SBjoern A. Zeeb a = 0; 3208413628a7SBjoern A. Zeeb z = pr->pr_ip6s - 2; 3209413628a7SBjoern A. Zeeb while (a <= z) { 3210413628a7SBjoern A. Zeeb i = (a + z) / 2; 3211413628a7SBjoern A. Zeeb d = qcmp_v6(&pr->pr_ip6[i+1], ia6); 3212413628a7SBjoern A. Zeeb if (d > 0) 3213413628a7SBjoern A. Zeeb z = i - 1; 3214413628a7SBjoern A. Zeeb else if (d < 0) 3215413628a7SBjoern A. Zeeb a = i + 1; 3216413628a7SBjoern A. Zeeb else 3217413628a7SBjoern A. Zeeb return (0); 3218413628a7SBjoern A. Zeeb } 3219413628a7SBjoern A. Zeeb 3220b89e82ddSJamie Gritton return (EADDRNOTAVAIL); 3221b89e82ddSJamie Gritton } 3222b89e82ddSJamie Gritton 3223413628a7SBjoern A. Zeeb int 3224413628a7SBjoern A. Zeeb prison_check_ip6(struct ucred *cred, struct in6_addr *ia6) 3225413628a7SBjoern A. Zeeb { 3226b38ff370SJamie Gritton struct prison *pr; 3227b38ff370SJamie Gritton int error; 3228413628a7SBjoern A. Zeeb 3229413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3230413628a7SBjoern A. Zeeb KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3231413628a7SBjoern A. Zeeb 3232b38ff370SJamie Gritton pr = cred->cr_prison; 32330304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) 32340304c731SJamie Gritton return (0); 3235b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 32360304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) { 32370304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 32380304c731SJamie Gritton return (0); 32390304c731SJamie Gritton } 3240b38ff370SJamie Gritton if (pr->pr_ip6 == NULL) { 3241b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3242b89e82ddSJamie Gritton return (EAFNOSUPPORT); 3243b38ff370SJamie Gritton } 3244413628a7SBjoern A. Zeeb 3245b38ff370SJamie Gritton error = _prison_check_ip6(pr, ia6); 3246b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3247b38ff370SJamie Gritton return (error); 3248413628a7SBjoern A. Zeeb } 3249413628a7SBjoern A. Zeeb #endif 3250413628a7SBjoern A. Zeeb 3251413628a7SBjoern A. Zeeb /* 3252ca04ba64SJamie Gritton * Check if a jail supports the given address family. 3253ca04ba64SJamie Gritton * 3254ca04ba64SJamie Gritton * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT 3255ca04ba64SJamie Gritton * if not. 3256ca04ba64SJamie Gritton */ 3257ca04ba64SJamie Gritton int 3258ca04ba64SJamie Gritton prison_check_af(struct ucred *cred, int af) 3259ca04ba64SJamie Gritton { 32600304c731SJamie Gritton struct prison *pr; 3261ca04ba64SJamie Gritton int error; 3262ca04ba64SJamie Gritton 3263ca04ba64SJamie Gritton KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3264ca04ba64SJamie Gritton 32650304c731SJamie Gritton pr = cred->cr_prison; 3266499650a0SJamie Gritton #ifdef VIMAGE 32676bb79563SJamie Gritton /* Prisons with their own network stack are not limited. */ 32686bb79563SJamie Gritton if (pr->pr_flags & PR_VNET) 32696bb79563SJamie Gritton return (0); 3270499650a0SJamie Gritton #endif 32716bb79563SJamie Gritton 3272ca04ba64SJamie Gritton error = 0; 3273ca04ba64SJamie Gritton switch (af) 3274ca04ba64SJamie Gritton { 3275ca04ba64SJamie Gritton #ifdef INET 3276ca04ba64SJamie Gritton case AF_INET: 32770304c731SJamie Gritton if (pr->pr_flags & PR_IP4) 32780304c731SJamie Gritton { 32790304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 32800304c731SJamie Gritton if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL) 3281ca04ba64SJamie Gritton error = EAFNOSUPPORT; 32820304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 32830304c731SJamie Gritton } 3284ca04ba64SJamie Gritton break; 3285ca04ba64SJamie Gritton #endif 3286ca04ba64SJamie Gritton #ifdef INET6 3287ca04ba64SJamie Gritton case AF_INET6: 32880304c731SJamie Gritton if (pr->pr_flags & PR_IP6) 32890304c731SJamie Gritton { 32900304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 32910304c731SJamie Gritton if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL) 3292ca04ba64SJamie Gritton error = EAFNOSUPPORT; 32930304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 32940304c731SJamie Gritton } 3295ca04ba64SJamie Gritton break; 3296ca04ba64SJamie Gritton #endif 3297ca04ba64SJamie Gritton case AF_LOCAL: 3298ca04ba64SJamie Gritton case AF_ROUTE: 3299ca04ba64SJamie Gritton break; 3300ca04ba64SJamie Gritton default: 33010304c731SJamie Gritton if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF)) 3302ca04ba64SJamie Gritton error = EAFNOSUPPORT; 3303ca04ba64SJamie Gritton } 3304ca04ba64SJamie Gritton return (error); 3305ca04ba64SJamie Gritton } 3306ca04ba64SJamie Gritton 3307ca04ba64SJamie Gritton /* 3308413628a7SBjoern A. Zeeb * Check if given address belongs to the jail referenced by cred (wrapper to 3309413628a7SBjoern A. Zeeb * prison_check_ip[46]). 3310413628a7SBjoern A. Zeeb * 33110304c731SJamie Gritton * Returns 0 if jail doesn't restrict the address family or if address belongs 33120304c731SJamie Gritton * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if 33130304c731SJamie Gritton * the jail doesn't allow the address family. IPv4 Address passed in in NBO. 3314413628a7SBjoern A. Zeeb */ 3315413628a7SBjoern A. Zeeb int 331691421ba2SRobert Watson prison_if(struct ucred *cred, struct sockaddr *sa) 331775c13541SPoul-Henning Kamp { 3318413628a7SBjoern A. Zeeb #ifdef INET 33199ddb7954SMike Barcroft struct sockaddr_in *sai; 3320413628a7SBjoern A. Zeeb #endif 3321413628a7SBjoern A. Zeeb #ifdef INET6 3322413628a7SBjoern A. Zeeb struct sockaddr_in6 *sai6; 3323413628a7SBjoern A. Zeeb #endif 3324b89e82ddSJamie Gritton int error; 332575c13541SPoul-Henning Kamp 3326413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3327413628a7SBjoern A. Zeeb KASSERT(sa != NULL, ("%s: sa is NULL", __func__)); 3328413628a7SBjoern A. Zeeb 3329b89e82ddSJamie Gritton error = 0; 3330413628a7SBjoern A. Zeeb switch (sa->sa_family) 3331413628a7SBjoern A. Zeeb { 3332413628a7SBjoern A. Zeeb #ifdef INET 3333413628a7SBjoern A. Zeeb case AF_INET: 33349ddb7954SMike Barcroft sai = (struct sockaddr_in *)sa; 3335b89e82ddSJamie Gritton error = prison_check_ip4(cred, &sai->sin_addr); 3336413628a7SBjoern A. Zeeb break; 3337413628a7SBjoern A. Zeeb #endif 3338413628a7SBjoern A. Zeeb #ifdef INET6 3339413628a7SBjoern A. Zeeb case AF_INET6: 3340413628a7SBjoern A. Zeeb sai6 = (struct sockaddr_in6 *)sa; 3341b89e82ddSJamie Gritton error = prison_check_ip6(cred, &sai6->sin6_addr); 3342413628a7SBjoern A. Zeeb break; 3343413628a7SBjoern A. Zeeb #endif 3344413628a7SBjoern A. Zeeb default: 33450304c731SJamie Gritton if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF)) 3346b89e82ddSJamie Gritton error = EAFNOSUPPORT; 3347413628a7SBjoern A. Zeeb } 3348b89e82ddSJamie Gritton return (error); 334975c13541SPoul-Henning Kamp } 335091421ba2SRobert Watson 335191421ba2SRobert Watson /* 335291421ba2SRobert Watson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH. 335391421ba2SRobert Watson */ 335491421ba2SRobert Watson int 33559ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2) 335691421ba2SRobert Watson { 335791421ba2SRobert Watson 33580304c731SJamie Gritton return ((cred1->cr_prison == cred2->cr_prison || 33590304c731SJamie Gritton prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH); 33600304c731SJamie Gritton } 336191421ba2SRobert Watson 33620304c731SJamie Gritton /* 33630304c731SJamie Gritton * Return 1 if p2 is a child of p1, otherwise 0. 33640304c731SJamie Gritton */ 33650304c731SJamie Gritton int 33660304c731SJamie Gritton prison_ischild(struct prison *pr1, struct prison *pr2) 33670304c731SJamie Gritton { 33680304c731SJamie Gritton 33690304c731SJamie Gritton for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent) 33700304c731SJamie Gritton if (pr1 == pr2) 33710304c731SJamie Gritton return (1); 337291421ba2SRobert Watson return (0); 337391421ba2SRobert Watson } 337491421ba2SRobert Watson 337591421ba2SRobert Watson /* 337691421ba2SRobert Watson * Return 1 if the passed credential is in a jail, otherwise 0. 337791421ba2SRobert Watson */ 337891421ba2SRobert Watson int 33799ddb7954SMike Barcroft jailed(struct ucred *cred) 338091421ba2SRobert Watson { 338191421ba2SRobert Watson 33820304c731SJamie Gritton return (cred->cr_prison != &prison0); 338391421ba2SRobert Watson } 33849484d0c0SRobert Drehmel 33859484d0c0SRobert Drehmel /* 33867455b100SJamie Gritton * Return the correct hostname (domainname, et al) for the passed credential. 33879484d0c0SRobert Drehmel */ 3388ad1ff099SRobert Drehmel void 33899ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size) 33909484d0c0SRobert Drehmel { 339176ca6f88SJamie Gritton struct prison *pr; 33929484d0c0SRobert Drehmel 33937455b100SJamie Gritton /* 33947455b100SJamie Gritton * A NULL credential can be used to shortcut to the physical 33957455b100SJamie Gritton * system's hostname. 33967455b100SJamie Gritton */ 339776ca6f88SJamie Gritton pr = (cred != NULL) ? cred->cr_prison : &prison0; 339876ca6f88SJamie Gritton mtx_lock(&pr->pr_mtx); 3399c1f19219SJamie Gritton strlcpy(buf, pr->pr_hostname, size); 340076ca6f88SJamie Gritton mtx_unlock(&pr->pr_mtx); 34019484d0c0SRobert Drehmel } 3402fd7a8150SMike Barcroft 34037455b100SJamie Gritton void 34047455b100SJamie Gritton getcreddomainname(struct ucred *cred, char *buf, size_t size) 34057455b100SJamie Gritton { 34067455b100SJamie Gritton 34077455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 3408c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_domainname, size); 34097455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 34107455b100SJamie Gritton } 34117455b100SJamie Gritton 34127455b100SJamie Gritton void 34137455b100SJamie Gritton getcredhostuuid(struct ucred *cred, char *buf, size_t size) 34147455b100SJamie Gritton { 34157455b100SJamie Gritton 34167455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 3417c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_hostuuid, size); 34187455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 34197455b100SJamie Gritton } 34207455b100SJamie Gritton 34217455b100SJamie Gritton void 34227455b100SJamie Gritton getcredhostid(struct ucred *cred, unsigned long *hostid) 34237455b100SJamie Gritton { 34247455b100SJamie Gritton 34257455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 34267455b100SJamie Gritton *hostid = cred->cr_prison->pr_hostid; 34277455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 34287455b100SJamie Gritton } 34297455b100SJamie Gritton 3430f08df373SRobert Watson /* 3431820a0de9SPawel Jakub Dawidek * Determine whether the subject represented by cred can "see" 3432820a0de9SPawel Jakub Dawidek * status of a mount point. 3433820a0de9SPawel Jakub Dawidek * Returns: 0 for permitted, ENOENT otherwise. 3434820a0de9SPawel Jakub Dawidek * XXX: This function should be called cr_canseemount() and should be 3435820a0de9SPawel Jakub Dawidek * placed in kern_prot.c. 3436f08df373SRobert Watson */ 3437f08df373SRobert Watson int 3438820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp) 3439f08df373SRobert Watson { 3440820a0de9SPawel Jakub Dawidek struct prison *pr; 3441820a0de9SPawel Jakub Dawidek struct statfs *sp; 3442820a0de9SPawel Jakub Dawidek size_t len; 3443f08df373SRobert Watson 3444820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 34450304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 34460304c731SJamie Gritton return (0); 3447820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) 3448820a0de9SPawel Jakub Dawidek return (0); 34490304c731SJamie Gritton if (pr->pr_enforce_statfs == 2) 3450820a0de9SPawel Jakub Dawidek return (ENOENT); 3451820a0de9SPawel Jakub Dawidek /* 3452820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 3453820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 3454820a0de9SPawel Jakub Dawidek * This is ugly check, but this is the only situation when jail's 3455820a0de9SPawel Jakub Dawidek * directory ends with '/'. 3456820a0de9SPawel Jakub Dawidek */ 3457820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 3458820a0de9SPawel Jakub Dawidek return (0); 3459820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 3460820a0de9SPawel Jakub Dawidek sp = &mp->mnt_stat; 3461820a0de9SPawel Jakub Dawidek if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0) 3462820a0de9SPawel Jakub Dawidek return (ENOENT); 3463820a0de9SPawel Jakub Dawidek /* 3464820a0de9SPawel Jakub Dawidek * Be sure that we don't have situation where jail's root directory 3465820a0de9SPawel Jakub Dawidek * is "/some/path" and mount point is "/some/pathpath". 3466820a0de9SPawel Jakub Dawidek */ 3467820a0de9SPawel Jakub Dawidek if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/') 3468820a0de9SPawel Jakub Dawidek return (ENOENT); 3469f08df373SRobert Watson return (0); 3470f08df373SRobert Watson } 3471820a0de9SPawel Jakub Dawidek 3472820a0de9SPawel Jakub Dawidek void 3473820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp) 3474820a0de9SPawel Jakub Dawidek { 3475820a0de9SPawel Jakub Dawidek char jpath[MAXPATHLEN]; 3476820a0de9SPawel Jakub Dawidek struct prison *pr; 3477820a0de9SPawel Jakub Dawidek size_t len; 3478820a0de9SPawel Jakub Dawidek 3479820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 34800304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 34810304c731SJamie Gritton return; 3482820a0de9SPawel Jakub Dawidek if (prison_canseemount(cred, mp) != 0) { 3483820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3484820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, "[restricted]", 3485820a0de9SPawel Jakub Dawidek sizeof(sp->f_mntonname)); 3486820a0de9SPawel Jakub Dawidek return; 3487820a0de9SPawel Jakub Dawidek } 3488820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) { 3489820a0de9SPawel Jakub Dawidek /* 3490820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3491820a0de9SPawel Jakub Dawidek * the valid path left there. 3492820a0de9SPawel Jakub Dawidek */ 3493820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3494820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3495820a0de9SPawel Jakub Dawidek return; 3496820a0de9SPawel Jakub Dawidek } 3497820a0de9SPawel Jakub Dawidek /* 3498820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 3499820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 3500820a0de9SPawel Jakub Dawidek */ 3501820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 3502820a0de9SPawel Jakub Dawidek return; 3503820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 3504820a0de9SPawel Jakub Dawidek strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath)); 3505820a0de9SPawel Jakub Dawidek /* 3506820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3507820a0de9SPawel Jakub Dawidek * the valid path left there. 3508820a0de9SPawel Jakub Dawidek */ 3509820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3510820a0de9SPawel Jakub Dawidek if (*jpath == '\0') { 3511820a0de9SPawel Jakub Dawidek /* Should never happen. */ 3512820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3513820a0de9SPawel Jakub Dawidek } else { 3514820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname)); 3515820a0de9SPawel Jakub Dawidek } 3516f08df373SRobert Watson } 3517f08df373SRobert Watson 3518800c9408SRobert Watson /* 3519800c9408SRobert Watson * Check with permission for a specific privilege is granted within jail. We 3520800c9408SRobert Watson * have a specific list of accepted privileges; the rest are denied. 3521800c9408SRobert Watson */ 3522800c9408SRobert Watson int 3523800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv) 3524800c9408SRobert Watson { 3525800c9408SRobert Watson 3526800c9408SRobert Watson if (!jailed(cred)) 3527800c9408SRobert Watson return (0); 3528800c9408SRobert Watson 35296bb79563SJamie Gritton #ifdef VIMAGE 35306bb79563SJamie Gritton /* 35316bb79563SJamie Gritton * Privileges specific to prisons with a virtual network stack. 35326bb79563SJamie Gritton * There might be a duplicate entry here in case the privilege 35336bb79563SJamie Gritton * is only granted conditionally in the legacy jail case. 35346bb79563SJamie Gritton */ 35356bb79563SJamie Gritton switch (priv) { 35366bb79563SJamie Gritton #ifdef notyet 35376bb79563SJamie Gritton /* 35386bb79563SJamie Gritton * NFS-specific privileges. 35396bb79563SJamie Gritton */ 35406bb79563SJamie Gritton case PRIV_NFS_DAEMON: 35416bb79563SJamie Gritton case PRIV_NFS_LOCKD: 35426bb79563SJamie Gritton #endif 35436bb79563SJamie Gritton /* 35446bb79563SJamie Gritton * Network stack privileges. 35456bb79563SJamie Gritton */ 35466bb79563SJamie Gritton case PRIV_NET_BRIDGE: 35476bb79563SJamie Gritton case PRIV_NET_GRE: 35486bb79563SJamie Gritton case PRIV_NET_BPF: 35496bb79563SJamie Gritton case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */ 35506bb79563SJamie Gritton case PRIV_NET_ROUTE: 35516bb79563SJamie Gritton case PRIV_NET_TAP: 35526bb79563SJamie Gritton case PRIV_NET_SETIFMTU: 35536bb79563SJamie Gritton case PRIV_NET_SETIFFLAGS: 35546bb79563SJamie Gritton case PRIV_NET_SETIFCAP: 35556bb79563SJamie Gritton case PRIV_NET_SETIFNAME : 35566bb79563SJamie Gritton case PRIV_NET_SETIFMETRIC: 35576bb79563SJamie Gritton case PRIV_NET_SETIFPHYS: 35586bb79563SJamie Gritton case PRIV_NET_SETIFMAC: 35596bb79563SJamie Gritton case PRIV_NET_ADDMULTI: 35606bb79563SJamie Gritton case PRIV_NET_DELMULTI: 35616bb79563SJamie Gritton case PRIV_NET_HWIOCTL: 35626bb79563SJamie Gritton case PRIV_NET_SETLLADDR: 35636bb79563SJamie Gritton case PRIV_NET_ADDIFGROUP: 35646bb79563SJamie Gritton case PRIV_NET_DELIFGROUP: 35656bb79563SJamie Gritton case PRIV_NET_IFCREATE: 35666bb79563SJamie Gritton case PRIV_NET_IFDESTROY: 35676bb79563SJamie Gritton case PRIV_NET_ADDIFADDR: 35686bb79563SJamie Gritton case PRIV_NET_DELIFADDR: 35696bb79563SJamie Gritton case PRIV_NET_LAGG: 35706bb79563SJamie Gritton case PRIV_NET_GIF: 35716bb79563SJamie Gritton case PRIV_NET_SETIFVNET: 35726bb79563SJamie Gritton 35736bb79563SJamie Gritton /* 35746bb79563SJamie Gritton * 802.11-related privileges. 35756bb79563SJamie Gritton */ 35766bb79563SJamie Gritton case PRIV_NET80211_GETKEY: 35776bb79563SJamie Gritton #ifdef notyet 35786bb79563SJamie Gritton case PRIV_NET80211_MANAGE: /* XXX-BZ discuss with sam@ */ 35796bb79563SJamie Gritton #endif 35806bb79563SJamie Gritton 35816bb79563SJamie Gritton #ifdef notyet 35826bb79563SJamie Gritton /* 35836bb79563SJamie Gritton * AppleTalk privileges. 35846bb79563SJamie Gritton */ 35856bb79563SJamie Gritton case PRIV_NETATALK_RESERVEDPORT: 35866bb79563SJamie Gritton 35876bb79563SJamie Gritton /* 35886bb79563SJamie Gritton * ATM privileges. 35896bb79563SJamie Gritton */ 35906bb79563SJamie Gritton case PRIV_NETATM_CFG: 35916bb79563SJamie Gritton case PRIV_NETATM_ADD: 35926bb79563SJamie Gritton case PRIV_NETATM_DEL: 35936bb79563SJamie Gritton case PRIV_NETATM_SET: 35946bb79563SJamie Gritton 35956bb79563SJamie Gritton /* 35966bb79563SJamie Gritton * Bluetooth privileges. 35976bb79563SJamie Gritton */ 35986bb79563SJamie Gritton case PRIV_NETBLUETOOTH_RAW: 35996bb79563SJamie Gritton #endif 36006bb79563SJamie Gritton 36016bb79563SJamie Gritton /* 36026bb79563SJamie Gritton * Netgraph and netgraph module privileges. 36036bb79563SJamie Gritton */ 36046bb79563SJamie Gritton case PRIV_NETGRAPH_CONTROL: 36056bb79563SJamie Gritton #ifdef notyet 36066bb79563SJamie Gritton case PRIV_NETGRAPH_TTY: 36076bb79563SJamie Gritton #endif 36086bb79563SJamie Gritton 36096bb79563SJamie Gritton /* 36106bb79563SJamie Gritton * IPv4 and IPv6 privileges. 36116bb79563SJamie Gritton */ 36126bb79563SJamie Gritton case PRIV_NETINET_IPFW: 36136bb79563SJamie Gritton case PRIV_NETINET_DIVERT: 36146bb79563SJamie Gritton case PRIV_NETINET_PF: 36156bb79563SJamie Gritton case PRIV_NETINET_DUMMYNET: 36166bb79563SJamie Gritton case PRIV_NETINET_CARP: 36176bb79563SJamie Gritton case PRIV_NETINET_MROUTE: 36186bb79563SJamie Gritton case PRIV_NETINET_RAW: 36196bb79563SJamie Gritton case PRIV_NETINET_ADDRCTRL6: 36206bb79563SJamie Gritton case PRIV_NETINET_ND6: 36216bb79563SJamie Gritton case PRIV_NETINET_SCOPE6: 36226bb79563SJamie Gritton case PRIV_NETINET_ALIFETIME6: 36236bb79563SJamie Gritton case PRIV_NETINET_IPSEC: 36246bb79563SJamie Gritton case PRIV_NETINET_BINDANY: 36256bb79563SJamie Gritton 36266bb79563SJamie Gritton #ifdef notyet 36276bb79563SJamie Gritton /* 36286bb79563SJamie Gritton * IPX/SPX privileges. 36296bb79563SJamie Gritton */ 36306bb79563SJamie Gritton case PRIV_NETIPX_RESERVEDPORT: 36316bb79563SJamie Gritton case PRIV_NETIPX_RAW: 36326bb79563SJamie Gritton 36336bb79563SJamie Gritton /* 36346bb79563SJamie Gritton * NCP privileges. 36356bb79563SJamie Gritton */ 36366bb79563SJamie Gritton case PRIV_NETNCP: 36376bb79563SJamie Gritton 36386bb79563SJamie Gritton /* 36396bb79563SJamie Gritton * SMB privileges. 36406bb79563SJamie Gritton */ 36416bb79563SJamie Gritton case PRIV_NETSMB: 36426bb79563SJamie Gritton #endif 36436bb79563SJamie Gritton 36446bb79563SJamie Gritton /* 36456bb79563SJamie Gritton * No default: or deny here. 36466bb79563SJamie Gritton * In case of no permit fall through to next switch(). 36476bb79563SJamie Gritton */ 36486bb79563SJamie Gritton if (cred->cr_prison->pr_flags & PR_VNET) 36496bb79563SJamie Gritton return (0); 36506bb79563SJamie Gritton } 36516bb79563SJamie Gritton #endif /* VIMAGE */ 36526bb79563SJamie Gritton 3653800c9408SRobert Watson switch (priv) { 3654800c9408SRobert Watson 3655800c9408SRobert Watson /* 3656800c9408SRobert Watson * Allow ktrace privileges for root in jail. 3657800c9408SRobert Watson */ 3658800c9408SRobert Watson case PRIV_KTRACE: 3659800c9408SRobert Watson 3660c3c1b5e6SRobert Watson #if 0 3661800c9408SRobert Watson /* 3662800c9408SRobert Watson * Allow jailed processes to configure audit identity and 3663800c9408SRobert Watson * submit audit records (login, etc). In the future we may 3664800c9408SRobert Watson * want to further refine the relationship between audit and 3665800c9408SRobert Watson * jail. 3666800c9408SRobert Watson */ 3667800c9408SRobert Watson case PRIV_AUDIT_GETAUDIT: 3668800c9408SRobert Watson case PRIV_AUDIT_SETAUDIT: 3669800c9408SRobert Watson case PRIV_AUDIT_SUBMIT: 3670c3c1b5e6SRobert Watson #endif 3671800c9408SRobert Watson 3672800c9408SRobert Watson /* 3673800c9408SRobert Watson * Allow jailed processes to manipulate process UNIX 3674800c9408SRobert Watson * credentials in any way they see fit. 3675800c9408SRobert Watson */ 3676800c9408SRobert Watson case PRIV_CRED_SETUID: 3677800c9408SRobert Watson case PRIV_CRED_SETEUID: 3678800c9408SRobert Watson case PRIV_CRED_SETGID: 3679800c9408SRobert Watson case PRIV_CRED_SETEGID: 3680800c9408SRobert Watson case PRIV_CRED_SETGROUPS: 3681800c9408SRobert Watson case PRIV_CRED_SETREUID: 3682800c9408SRobert Watson case PRIV_CRED_SETREGID: 3683800c9408SRobert Watson case PRIV_CRED_SETRESUID: 3684800c9408SRobert Watson case PRIV_CRED_SETRESGID: 3685800c9408SRobert Watson 3686800c9408SRobert Watson /* 3687800c9408SRobert Watson * Jail implements visibility constraints already, so allow 3688800c9408SRobert Watson * jailed root to override uid/gid-based constraints. 3689800c9408SRobert Watson */ 3690800c9408SRobert Watson case PRIV_SEEOTHERGIDS: 3691800c9408SRobert Watson case PRIV_SEEOTHERUIDS: 3692800c9408SRobert Watson 3693800c9408SRobert Watson /* 3694800c9408SRobert Watson * Jail implements inter-process debugging limits already, so 3695800c9408SRobert Watson * allow jailed root various debugging privileges. 3696800c9408SRobert Watson */ 3697800c9408SRobert Watson case PRIV_DEBUG_DIFFCRED: 3698800c9408SRobert Watson case PRIV_DEBUG_SUGID: 3699800c9408SRobert Watson case PRIV_DEBUG_UNPRIV: 3700800c9408SRobert Watson 3701800c9408SRobert Watson /* 3702800c9408SRobert Watson * Allow jail to set various resource limits and login 3703800c9408SRobert Watson * properties, and for now, exceed process resource limits. 3704800c9408SRobert Watson */ 3705800c9408SRobert Watson case PRIV_PROC_LIMIT: 3706800c9408SRobert Watson case PRIV_PROC_SETLOGIN: 3707800c9408SRobert Watson case PRIV_PROC_SETRLIMIT: 3708800c9408SRobert Watson 3709800c9408SRobert Watson /* 3710800c9408SRobert Watson * System V and POSIX IPC privileges are granted in jail. 3711800c9408SRobert Watson */ 3712800c9408SRobert Watson case PRIV_IPC_READ: 3713800c9408SRobert Watson case PRIV_IPC_WRITE: 3714800c9408SRobert Watson case PRIV_IPC_ADMIN: 3715800c9408SRobert Watson case PRIV_IPC_MSGSIZE: 3716800c9408SRobert Watson case PRIV_MQ_ADMIN: 3717800c9408SRobert Watson 3718800c9408SRobert Watson /* 37190304c731SJamie Gritton * Jail operations within a jail work on child jails. 37200304c731SJamie Gritton */ 37210304c731SJamie Gritton case PRIV_JAIL_ATTACH: 37220304c731SJamie Gritton case PRIV_JAIL_SET: 37230304c731SJamie Gritton case PRIV_JAIL_REMOVE: 37240304c731SJamie Gritton 37250304c731SJamie Gritton /* 3726800c9408SRobert Watson * Jail implements its own inter-process limits, so allow 3727800c9408SRobert Watson * root processes in jail to change scheduling on other 3728800c9408SRobert Watson * processes in the same jail. Likewise for signalling. 3729800c9408SRobert Watson */ 3730800c9408SRobert Watson case PRIV_SCHED_DIFFCRED: 3731413628a7SBjoern A. Zeeb case PRIV_SCHED_CPUSET: 3732800c9408SRobert Watson case PRIV_SIGNAL_DIFFCRED: 3733800c9408SRobert Watson case PRIV_SIGNAL_SUGID: 3734800c9408SRobert Watson 3735800c9408SRobert Watson /* 3736800c9408SRobert Watson * Allow jailed processes to write to sysctls marked as jail 3737800c9408SRobert Watson * writable. 3738800c9408SRobert Watson */ 3739800c9408SRobert Watson case PRIV_SYSCTL_WRITEJAIL: 3740800c9408SRobert Watson 3741800c9408SRobert Watson /* 3742800c9408SRobert Watson * Allow root in jail to manage a variety of quota 3743e82d0201SRobert Watson * properties. These should likely be conditional on a 3744e82d0201SRobert Watson * configuration option. 3745800c9408SRobert Watson */ 374695b091d2SRobert Watson case PRIV_VFS_GETQUOTA: 374795b091d2SRobert Watson case PRIV_VFS_SETQUOTA: 3748800c9408SRobert Watson 3749800c9408SRobert Watson /* 3750800c9408SRobert Watson * Since Jail relies on chroot() to implement file system 3751800c9408SRobert Watson * protections, grant many VFS privileges to root in jail. 3752800c9408SRobert Watson * Be careful to exclude mount-related and NFS-related 3753800c9408SRobert Watson * privileges. 3754800c9408SRobert Watson */ 3755800c9408SRobert Watson case PRIV_VFS_READ: 3756800c9408SRobert Watson case PRIV_VFS_WRITE: 3757800c9408SRobert Watson case PRIV_VFS_ADMIN: 3758800c9408SRobert Watson case PRIV_VFS_EXEC: 3759800c9408SRobert Watson case PRIV_VFS_LOOKUP: 3760800c9408SRobert Watson case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */ 3761800c9408SRobert Watson case PRIV_VFS_CHFLAGS_DEV: 3762800c9408SRobert Watson case PRIV_VFS_CHOWN: 3763800c9408SRobert Watson case PRIV_VFS_CHROOT: 3764bb531912SPawel Jakub Dawidek case PRIV_VFS_RETAINSUGID: 3765800c9408SRobert Watson case PRIV_VFS_FCHROOT: 3766800c9408SRobert Watson case PRIV_VFS_LINK: 3767800c9408SRobert Watson case PRIV_VFS_SETGID: 3768e41966dcSRobert Watson case PRIV_VFS_STAT: 3769800c9408SRobert Watson case PRIV_VFS_STICKYFILE: 3770800c9408SRobert Watson return (0); 3771800c9408SRobert Watson 3772800c9408SRobert Watson /* 3773800c9408SRobert Watson * Depending on the global setting, allow privilege of 3774800c9408SRobert Watson * setting system flags. 3775800c9408SRobert Watson */ 3776800c9408SRobert Watson case PRIV_VFS_SYSFLAGS: 37770304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS) 3778800c9408SRobert Watson return (0); 3779800c9408SRobert Watson else 3780800c9408SRobert Watson return (EPERM); 3781800c9408SRobert Watson 3782800c9408SRobert Watson /* 3783f3a8d2f9SPawel Jakub Dawidek * Depending on the global setting, allow privilege of 3784f3a8d2f9SPawel Jakub Dawidek * mounting/unmounting file systems. 3785f3a8d2f9SPawel Jakub Dawidek */ 3786f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT: 3787f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_UNMOUNT: 3788f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT_NONUSER: 378924b0502eSPawel Jakub Dawidek case PRIV_VFS_MOUNT_OWNER: 37900304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT) 3791f3a8d2f9SPawel Jakub Dawidek return (0); 3792f3a8d2f9SPawel Jakub Dawidek else 3793f3a8d2f9SPawel Jakub Dawidek return (EPERM); 3794f3a8d2f9SPawel Jakub Dawidek 3795f3a8d2f9SPawel Jakub Dawidek /* 37964b084056SRobert Watson * Allow jailed root to bind reserved ports and reuse in-use 37974b084056SRobert Watson * ports. 3798800c9408SRobert Watson */ 3799800c9408SRobert Watson case PRIV_NETINET_RESERVEDPORT: 38004b084056SRobert Watson case PRIV_NETINET_REUSEPORT: 3801800c9408SRobert Watson return (0); 3802800c9408SRobert Watson 3803800c9408SRobert Watson /* 380479ba3952SBjoern A. Zeeb * Allow jailed root to set certian IPv4/6 (option) headers. 380579ba3952SBjoern A. Zeeb */ 380679ba3952SBjoern A. Zeeb case PRIV_NETINET_SETHDROPTS: 380779ba3952SBjoern A. Zeeb return (0); 380879ba3952SBjoern A. Zeeb 380979ba3952SBjoern A. Zeeb /* 3810800c9408SRobert Watson * Conditionally allow creating raw sockets in jail. 3811800c9408SRobert Watson */ 3812800c9408SRobert Watson case PRIV_NETINET_RAW: 38130304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS) 3814800c9408SRobert Watson return (0); 3815800c9408SRobert Watson else 3816800c9408SRobert Watson return (EPERM); 3817800c9408SRobert Watson 3818800c9408SRobert Watson /* 3819800c9408SRobert Watson * Since jail implements its own visibility limits on netstat 3820800c9408SRobert Watson * sysctls, allow getcred. This allows identd to work in 3821800c9408SRobert Watson * jail. 3822800c9408SRobert Watson */ 3823800c9408SRobert Watson case PRIV_NETINET_GETCRED: 3824800c9408SRobert Watson return (0); 3825800c9408SRobert Watson 3826800c9408SRobert Watson default: 3827800c9408SRobert Watson /* 3828800c9408SRobert Watson * In all remaining cases, deny the privilege request. This 3829800c9408SRobert Watson * includes almost all network privileges, many system 3830800c9408SRobert Watson * configuration privileges. 3831800c9408SRobert Watson */ 3832800c9408SRobert Watson return (EPERM); 3833800c9408SRobert Watson } 3834800c9408SRobert Watson } 3835800c9408SRobert Watson 38360304c731SJamie Gritton /* 38370304c731SJamie Gritton * Return the part of pr2's name that is relative to pr1, or the whole name 38380304c731SJamie Gritton * if it does not directly follow. 38390304c731SJamie Gritton */ 38400304c731SJamie Gritton 38410304c731SJamie Gritton char * 38420304c731SJamie Gritton prison_name(struct prison *pr1, struct prison *pr2) 38430304c731SJamie Gritton { 38440304c731SJamie Gritton char *name; 38450304c731SJamie Gritton 38460304c731SJamie Gritton /* Jails see themselves as "0" (if they see themselves at all). */ 38470304c731SJamie Gritton if (pr1 == pr2) 38480304c731SJamie Gritton return "0"; 38490304c731SJamie Gritton name = pr2->pr_name; 38500304c731SJamie Gritton if (prison_ischild(pr1, pr2)) { 38510304c731SJamie Gritton /* 38520304c731SJamie Gritton * pr1 isn't locked (and allprison_lock may not be either) 38530304c731SJamie Gritton * so its length can't be counted on. But the number of dots 38540304c731SJamie Gritton * can be counted on - and counted. 38550304c731SJamie Gritton */ 38560304c731SJamie Gritton for (; pr1 != &prison0; pr1 = pr1->pr_parent) 38570304c731SJamie Gritton name = strchr(name, '.') + 1; 38580304c731SJamie Gritton } 38590304c731SJamie Gritton return (name); 38600304c731SJamie Gritton } 38610304c731SJamie Gritton 38620304c731SJamie Gritton /* 38630304c731SJamie Gritton * Return the part of pr2's path that is relative to pr1, or the whole path 38640304c731SJamie Gritton * if it does not directly follow. 38650304c731SJamie Gritton */ 38660304c731SJamie Gritton static char * 38670304c731SJamie Gritton prison_path(struct prison *pr1, struct prison *pr2) 38680304c731SJamie Gritton { 38690304c731SJamie Gritton char *path1, *path2; 38700304c731SJamie Gritton int len1; 38710304c731SJamie Gritton 38720304c731SJamie Gritton path1 = pr1->pr_path; 38730304c731SJamie Gritton path2 = pr2->pr_path; 38740304c731SJamie Gritton if (!strcmp(path1, "/")) 38750304c731SJamie Gritton return (path2); 38760304c731SJamie Gritton len1 = strlen(path1); 38770304c731SJamie Gritton if (strncmp(path1, path2, len1)) 38780304c731SJamie Gritton return (path2); 38790304c731SJamie Gritton if (path2[len1] == '\0') 38800304c731SJamie Gritton return "/"; 38810304c731SJamie Gritton if (path2[len1] == '/') 38820304c731SJamie Gritton return (path2 + len1); 38830304c731SJamie Gritton return (path2); 38840304c731SJamie Gritton } 38850304c731SJamie Gritton 38860304c731SJamie Gritton 38870304c731SJamie Gritton /* 38880304c731SJamie Gritton * Jail-related sysctls. 38890304c731SJamie Gritton */ 38900304c731SJamie Gritton SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0, 38910304c731SJamie Gritton "Jails"); 38920304c731SJamie Gritton 3893fd7a8150SMike Barcroft static int 3894fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS) 3895fd7a8150SMike Barcroft { 3896b38ff370SJamie Gritton struct xprison *xp; 38970304c731SJamie Gritton struct prison *pr, *cpr; 3898b38ff370SJamie Gritton #ifdef INET 3899b38ff370SJamie Gritton struct in_addr *ip4 = NULL; 3900b38ff370SJamie Gritton int ip4s = 0; 3901b38ff370SJamie Gritton #endif 3902b38ff370SJamie Gritton #ifdef INET6 3903b38ff370SJamie Gritton struct in_addr *ip6 = NULL; 3904b38ff370SJamie Gritton int ip6s = 0; 3905b38ff370SJamie Gritton #endif 39060304c731SJamie Gritton int descend, error; 3907fd7a8150SMike Barcroft 3908b38ff370SJamie Gritton xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK); 39090304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 3910b38ff370SJamie Gritton error = 0; 3911dc68a633SPawel Jakub Dawidek sx_slock(&allprison_lock); 39120304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 39130304c731SJamie Gritton #if defined(INET) || defined(INET6) 3914b38ff370SJamie Gritton again: 39150304c731SJamie Gritton #endif 39160304c731SJamie Gritton mtx_lock(&cpr->pr_mtx); 3917413628a7SBjoern A. Zeeb #ifdef INET 39180304c731SJamie Gritton if (cpr->pr_ip4s > 0) { 39190304c731SJamie Gritton if (ip4s < cpr->pr_ip4s) { 39200304c731SJamie Gritton ip4s = cpr->pr_ip4s; 39210304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 3922b38ff370SJamie Gritton ip4 = realloc(ip4, ip4s * 3923b38ff370SJamie Gritton sizeof(struct in_addr), M_TEMP, M_WAITOK); 3924b38ff370SJamie Gritton goto again; 3925b38ff370SJamie Gritton } 39260304c731SJamie Gritton bcopy(cpr->pr_ip4, ip4, 39270304c731SJamie Gritton cpr->pr_ip4s * sizeof(struct in_addr)); 3928b38ff370SJamie Gritton } 3929413628a7SBjoern A. Zeeb #endif 3930413628a7SBjoern A. Zeeb #ifdef INET6 39310304c731SJamie Gritton if (cpr->pr_ip6s > 0) { 39320304c731SJamie Gritton if (ip6s < cpr->pr_ip6s) { 39330304c731SJamie Gritton ip6s = cpr->pr_ip6s; 39340304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 3935b38ff370SJamie Gritton ip6 = realloc(ip6, ip6s * 3936b38ff370SJamie Gritton sizeof(struct in6_addr), M_TEMP, M_WAITOK); 3937b38ff370SJamie Gritton goto again; 3938413628a7SBjoern A. Zeeb } 39390304c731SJamie Gritton bcopy(cpr->pr_ip6, ip6, 39400304c731SJamie Gritton cpr->pr_ip6s * sizeof(struct in6_addr)); 3941b38ff370SJamie Gritton } 3942b38ff370SJamie Gritton #endif 39430304c731SJamie Gritton if (cpr->pr_ref == 0) { 39440304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 3945b38ff370SJamie Gritton continue; 3946b38ff370SJamie Gritton } 3947b38ff370SJamie Gritton bzero(xp, sizeof(*xp)); 3948fd7a8150SMike Barcroft xp->pr_version = XPRISON_VERSION; 39490304c731SJamie Gritton xp->pr_id = cpr->pr_id; 39500304c731SJamie Gritton xp->pr_state = cpr->pr_uref > 0 3951b38ff370SJamie Gritton ? PRISON_STATE_ALIVE : PRISON_STATE_DYING; 39520304c731SJamie Gritton strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path)); 3953c1f19219SJamie Gritton strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host)); 39540304c731SJamie Gritton strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name)); 3955413628a7SBjoern A. Zeeb #ifdef INET 39560304c731SJamie Gritton xp->pr_ip4s = cpr->pr_ip4s; 3957413628a7SBjoern A. Zeeb #endif 3958413628a7SBjoern A. Zeeb #ifdef INET6 39590304c731SJamie Gritton xp->pr_ip6s = cpr->pr_ip6s; 3960413628a7SBjoern A. Zeeb #endif 39610304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 3962b38ff370SJamie Gritton error = SYSCTL_OUT(req, xp, sizeof(*xp)); 3963b38ff370SJamie Gritton if (error) 3964b38ff370SJamie Gritton break; 3965413628a7SBjoern A. Zeeb #ifdef INET 3966b38ff370SJamie Gritton if (xp->pr_ip4s > 0) { 3967b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip4, 3968b38ff370SJamie Gritton xp->pr_ip4s * sizeof(struct in_addr)); 3969b38ff370SJamie Gritton if (error) 3970b38ff370SJamie Gritton break; 3971413628a7SBjoern A. Zeeb } 3972413628a7SBjoern A. Zeeb #endif 3973413628a7SBjoern A. Zeeb #ifdef INET6 3974b38ff370SJamie Gritton if (xp->pr_ip6s > 0) { 3975b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip6, 3976b38ff370SJamie Gritton xp->pr_ip6s * sizeof(struct in6_addr)); 3977b38ff370SJamie Gritton if (error) 3978b38ff370SJamie Gritton break; 3979413628a7SBjoern A. Zeeb } 3980413628a7SBjoern A. Zeeb #endif 3981fd7a8150SMike Barcroft } 3982dc68a633SPawel Jakub Dawidek sx_sunlock(&allprison_lock); 3983b38ff370SJamie Gritton free(xp, M_TEMP); 3984b38ff370SJamie Gritton #ifdef INET 3985b38ff370SJamie Gritton free(ip4, M_TEMP); 3986b38ff370SJamie Gritton #endif 3987b38ff370SJamie Gritton #ifdef INET6 3988b38ff370SJamie Gritton free(ip6, M_TEMP); 3989b38ff370SJamie Gritton #endif 3990fd7a8150SMike Barcroft return (error); 3991fd7a8150SMike Barcroft } 3992fd7a8150SMike Barcroft 3993f3b86a5fSEd Schouten SYSCTL_OID(_security_jail, OID_AUTO, list, 3994f3b86a5fSEd Schouten CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 3995f3b86a5fSEd Schouten sysctl_jail_list, "S", "List of active jails"); 3996461167c2SPawel Jakub Dawidek 3997461167c2SPawel Jakub Dawidek static int 3998461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS) 3999461167c2SPawel Jakub Dawidek { 4000461167c2SPawel Jakub Dawidek int error, injail; 4001461167c2SPawel Jakub Dawidek 4002461167c2SPawel Jakub Dawidek injail = jailed(req->td->td_ucred); 4003461167c2SPawel Jakub Dawidek error = SYSCTL_OUT(req, &injail, sizeof(injail)); 4004461167c2SPawel Jakub Dawidek 4005461167c2SPawel Jakub Dawidek return (error); 4006461167c2SPawel Jakub Dawidek } 40070304c731SJamie Gritton 4008f3b86a5fSEd Schouten SYSCTL_PROC(_security_jail, OID_AUTO, jailed, 4009f3b86a5fSEd Schouten CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4010f3b86a5fSEd Schouten sysctl_jail_jailed, "I", "Process in jail?"); 4011413628a7SBjoern A. Zeeb 40120304c731SJamie Gritton #if defined(INET) || defined(INET6) 4013e92e0574SJamie Gritton SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW, 40140304c731SJamie Gritton &jail_max_af_ips, 0, 40150304c731SJamie Gritton "Number of IP addresses a jail may have at most per address family"); 40160304c731SJamie Gritton #endif 40170304c731SJamie Gritton 40180304c731SJamie Gritton /* 40190304c731SJamie Gritton * Default parameters for jail(2) compatability. For historical reasons, 40200304c731SJamie Gritton * the sysctl names have varying similarity to the parameter names. Prisons 40210304c731SJamie Gritton * just see their own parameters, and can't change them. 40220304c731SJamie Gritton */ 40230304c731SJamie Gritton static int 40240304c731SJamie Gritton sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS) 40250304c731SJamie Gritton { 40260304c731SJamie Gritton struct prison *pr; 40270304c731SJamie Gritton int allow, error, i; 40280304c731SJamie Gritton 40290304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 40300304c731SJamie Gritton allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow; 40310304c731SJamie Gritton 40320304c731SJamie Gritton /* Get the current flag value, and convert it to a boolean. */ 40330304c731SJamie Gritton i = (allow & arg2) ? 1 : 0; 40340304c731SJamie Gritton if (arg1 != NULL) 40350304c731SJamie Gritton i = !i; 40360304c731SJamie Gritton error = sysctl_handle_int(oidp, &i, 0, req); 40370304c731SJamie Gritton if (error || !req->newptr) 40380304c731SJamie Gritton return (error); 40390304c731SJamie Gritton i = i ? arg2 : 0; 40400304c731SJamie Gritton if (arg1 != NULL) 40410304c731SJamie Gritton i ^= arg2; 40420304c731SJamie Gritton /* 40430304c731SJamie Gritton * The sysctls don't have CTLFLAGS_PRISON, so assume prison0 40440304c731SJamie Gritton * for writing. 40450304c731SJamie Gritton */ 40460304c731SJamie Gritton mtx_lock(&prison0.pr_mtx); 40470304c731SJamie Gritton jail_default_allow = (jail_default_allow & ~arg2) | i; 40480304c731SJamie Gritton mtx_unlock(&prison0.pr_mtx); 40490304c731SJamie Gritton return (0); 40500304c731SJamie Gritton } 40510304c731SJamie Gritton 40520304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed, 40530304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 40540304c731SJamie Gritton NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I", 40550304c731SJamie Gritton "Processes in jail can set their hostnames"); 40560304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only, 40570304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 40580304c731SJamie Gritton (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I", 40590304c731SJamie Gritton "Processes in jail are limited to creating UNIX/IP/route sockets only"); 40600304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed, 40610304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 40620304c731SJamie Gritton NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I", 40630304c731SJamie Gritton "Processes in jail can use System V IPC primitives"); 40640304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets, 40650304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 40660304c731SJamie Gritton NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I", 40670304c731SJamie Gritton "Prison root can create raw sockets"); 40680304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed, 40690304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 40700304c731SJamie Gritton NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I", 40710304c731SJamie Gritton "Processes in jail can alter system file flags"); 40720304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed, 40730304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 40740304c731SJamie Gritton NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I", 40750304c731SJamie Gritton "Processes in jail can mount/unmount jail-friendly file systems"); 40760304c731SJamie Gritton 40770304c731SJamie Gritton static int 40780304c731SJamie Gritton sysctl_jail_default_level(SYSCTL_HANDLER_ARGS) 40790304c731SJamie Gritton { 40800304c731SJamie Gritton struct prison *pr; 40810304c731SJamie Gritton int level, error; 40820304c731SJamie Gritton 40830304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 40840304c731SJamie Gritton level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2); 40850304c731SJamie Gritton error = sysctl_handle_int(oidp, &level, 0, req); 40860304c731SJamie Gritton if (error || !req->newptr) 40870304c731SJamie Gritton return (error); 40880304c731SJamie Gritton *(int *)arg1 = level; 40890304c731SJamie Gritton return (0); 40900304c731SJamie Gritton } 40910304c731SJamie Gritton 40920304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs, 40930304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 40940304c731SJamie Gritton &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs), 40950304c731SJamie Gritton sysctl_jail_default_level, "I", 40960304c731SJamie Gritton "Processes in jail cannot see all mounted file systems"); 40970304c731SJamie Gritton 40980304c731SJamie Gritton /* 40990304c731SJamie Gritton * Nodes to describe jail parameters. Maximum length of string parameters 41000304c731SJamie Gritton * is returned in the string itself, and the other parameters exist merely 41010304c731SJamie Gritton * to make themselves and their types known. 41020304c731SJamie Gritton */ 41030304c731SJamie Gritton SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0, 41040304c731SJamie Gritton "Jail parameters"); 41050304c731SJamie Gritton 41060304c731SJamie Gritton int 41070304c731SJamie Gritton sysctl_jail_param(SYSCTL_HANDLER_ARGS) 41080304c731SJamie Gritton { 41090304c731SJamie Gritton int i; 41100304c731SJamie Gritton long l; 41110304c731SJamie Gritton size_t s; 41120304c731SJamie Gritton char numbuf[12]; 41130304c731SJamie Gritton 41140304c731SJamie Gritton switch (oidp->oid_kind & CTLTYPE) 41150304c731SJamie Gritton { 41160304c731SJamie Gritton case CTLTYPE_LONG: 41170304c731SJamie Gritton case CTLTYPE_ULONG: 41180304c731SJamie Gritton l = 0; 41190304c731SJamie Gritton #ifdef SCTL_MASK32 41200304c731SJamie Gritton if (!(req->flags & SCTL_MASK32)) 41210304c731SJamie Gritton #endif 41220304c731SJamie Gritton return (SYSCTL_OUT(req, &l, sizeof(l))); 41230304c731SJamie Gritton case CTLTYPE_INT: 41240304c731SJamie Gritton case CTLTYPE_UINT: 41250304c731SJamie Gritton i = 0; 41260304c731SJamie Gritton return (SYSCTL_OUT(req, &i, sizeof(i))); 41270304c731SJamie Gritton case CTLTYPE_STRING: 41280304c731SJamie Gritton snprintf(numbuf, sizeof(numbuf), "%d", arg2); 41290304c731SJamie Gritton return 41300304c731SJamie Gritton (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req)); 41310304c731SJamie Gritton case CTLTYPE_STRUCT: 41320304c731SJamie Gritton s = (size_t)arg2; 41330304c731SJamie Gritton return (SYSCTL_OUT(req, &s, sizeof(s))); 41340304c731SJamie Gritton } 41350304c731SJamie Gritton return (0); 41360304c731SJamie Gritton } 41370304c731SJamie Gritton 41380304c731SJamie Gritton SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID"); 41390304c731SJamie Gritton SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID"); 41400304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name"); 41410304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path"); 41420304c731SJamie Gritton SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW, 41430304c731SJamie Gritton "I", "Jail secure level"); 41440304c731SJamie Gritton SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW, 41450304c731SJamie Gritton "I", "Jail cannot see all mounted file systems"); 41460304c731SJamie Gritton SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW, 41470304c731SJamie Gritton "B", "Jail persistence"); 4148679e1390SJamie Gritton #ifdef VIMAGE 4149679e1390SJamie Gritton SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN, 41507cbf7213SJamie Gritton "E,jailsys", "Virtual network stack"); 4151679e1390SJamie Gritton #endif 41520304c731SJamie Gritton SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD, 41530304c731SJamie Gritton "B", "Jail is in the process of shutting down"); 41540304c731SJamie Gritton 4155b97457e2SJamie Gritton SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails"); 4156b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD, 4157b97457e2SJamie Gritton "I", "Current number of child jails"); 4158b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW, 4159b97457e2SJamie Gritton "I", "Maximum number of child jails"); 4160b97457e2SJamie Gritton 41617cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info"); 41620304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN, 41630304c731SJamie Gritton "Jail hostname"); 416476ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN, 416576ca6f88SJamie Gritton "Jail NIS domainname"); 416676ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN, 416776ca6f88SJamie Gritton "Jail host UUID"); 416876ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW, 416976ca6f88SJamie Gritton "LU", "Jail host ID"); 41700304c731SJamie Gritton 41710304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset"); 41720304c731SJamie Gritton SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID"); 41730304c731SJamie Gritton 41740304c731SJamie Gritton #ifdef INET 41757cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RW, "Jail IPv4 address virtualization"); 41760304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr), 41770304c731SJamie Gritton "S,in_addr,a", "Jail IPv4 addresses"); 41780304c731SJamie Gritton #endif 41790304c731SJamie Gritton #ifdef INET6 41807cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RW, "Jail IPv6 address virtualization"); 41810304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr), 41820304c731SJamie Gritton "S,in6_addr,a", "Jail IPv6 addresses"); 41830304c731SJamie Gritton #endif 41840304c731SJamie Gritton 41850304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags"); 41860304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW, 41870304c731SJamie Gritton "B", "Jail may set hostname"); 41880304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW, 41890304c731SJamie Gritton "B", "Jail may use SYSV IPC"); 41900304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW, 41910304c731SJamie Gritton "B", "Jail may create raw sockets"); 41920304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW, 41930304c731SJamie Gritton "B", "Jail may alter system file flags"); 41940304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, mount, CTLTYPE_INT | CTLFLAG_RW, 41950304c731SJamie Gritton "B", "Jail may mount/unmount jail-friendly file systems"); 41960304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW, 41970304c731SJamie Gritton "B", "Jail may set file quotas"); 41980304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW, 41990304c731SJamie Gritton "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route"); 42000304c731SJamie Gritton 42010304c731SJamie Gritton 4202413628a7SBjoern A. Zeeb #ifdef DDB 4203b38ff370SJamie Gritton 4204b38ff370SJamie Gritton static void 4205b38ff370SJamie Gritton db_show_prison(struct prison *pr) 4206413628a7SBjoern A. Zeeb { 42070304c731SJamie Gritton int fi; 4208b38ff370SJamie Gritton #if defined(INET) || defined(INET6) 4209b38ff370SJamie Gritton int ii; 4210413628a7SBjoern A. Zeeb #endif 42117cbf7213SJamie Gritton unsigned jsf; 4212413628a7SBjoern A. Zeeb #ifdef INET6 4213413628a7SBjoern A. Zeeb char ip6buf[INET6_ADDRSTRLEN]; 4214413628a7SBjoern A. Zeeb #endif 4215413628a7SBjoern A. Zeeb 4216b38ff370SJamie Gritton db_printf("prison %p:\n", pr); 4217b38ff370SJamie Gritton db_printf(" jid = %d\n", pr->pr_id); 4218b38ff370SJamie Gritton db_printf(" name = %s\n", pr->pr_name); 42190304c731SJamie Gritton db_printf(" parent = %p\n", pr->pr_parent); 4220b38ff370SJamie Gritton db_printf(" ref = %d\n", pr->pr_ref); 4221b38ff370SJamie Gritton db_printf(" uref = %d\n", pr->pr_uref); 4222b38ff370SJamie Gritton db_printf(" path = %s\n", pr->pr_path); 4223b38ff370SJamie Gritton db_printf(" cpuset = %d\n", pr->pr_cpuset 4224b38ff370SJamie Gritton ? pr->pr_cpuset->cs_id : -1); 4225679e1390SJamie Gritton #ifdef VIMAGE 4226679e1390SJamie Gritton db_printf(" vnet = %p\n", pr->pr_vnet); 4227679e1390SJamie Gritton #endif 4228b38ff370SJamie Gritton db_printf(" root = %p\n", pr->pr_root); 4229b38ff370SJamie Gritton db_printf(" securelevel = %d\n", pr->pr_securelevel); 4230b97457e2SJamie Gritton db_printf(" childcount = %d\n", pr->pr_childcount); 42310304c731SJamie Gritton db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children)); 42320304c731SJamie Gritton db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling)); 4233b38ff370SJamie Gritton db_printf(" flags = %x", pr->pr_flags); 42340304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 42350304c731SJamie Gritton fi++) 42360304c731SJamie Gritton if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi))) 42370304c731SJamie Gritton db_printf(" %s", pr_flag_names[fi]); 42387cbf7213SJamie Gritton for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 42397cbf7213SJamie Gritton fi++) { 42407cbf7213SJamie Gritton jsf = pr->pr_flags & 42417cbf7213SJamie Gritton (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new); 42427cbf7213SJamie Gritton db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name, 42437cbf7213SJamie Gritton pr_flag_jailsys[fi].disable && 42447cbf7213SJamie Gritton (jsf == pr_flag_jailsys[fi].disable) ? "disable" 42457cbf7213SJamie Gritton : (jsf == pr_flag_jailsys[fi].new) ? "new" 42467cbf7213SJamie Gritton : "inherit"); 42477cbf7213SJamie Gritton } 42480304c731SJamie Gritton db_printf(" allow = %x", pr->pr_allow); 42490304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 42500304c731SJamie Gritton fi++) 42510304c731SJamie Gritton if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi))) 42520304c731SJamie Gritton db_printf(" %s", pr_allow_names[fi]); 4253b38ff370SJamie Gritton db_printf("\n"); 42540304c731SJamie Gritton db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs); 4255c1f19219SJamie Gritton db_printf(" host.hostname = %s\n", pr->pr_hostname); 4256c1f19219SJamie Gritton db_printf(" host.domainname = %s\n", pr->pr_domainname); 4257c1f19219SJamie Gritton db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid); 425876ca6f88SJamie Gritton db_printf(" host.hostid = %lu\n", pr->pr_hostid); 4259413628a7SBjoern A. Zeeb #ifdef INET 4260b38ff370SJamie Gritton db_printf(" ip4s = %d\n", pr->pr_ip4s); 4261b38ff370SJamie Gritton for (ii = 0; ii < pr->pr_ip4s; ii++) 4262b38ff370SJamie Gritton db_printf(" %s %s\n", 4263b38ff370SJamie Gritton ii == 0 ? "ip4 =" : " ", 4264b38ff370SJamie Gritton inet_ntoa(pr->pr_ip4[ii])); 4265413628a7SBjoern A. Zeeb #endif 4266413628a7SBjoern A. Zeeb #ifdef INET6 4267b38ff370SJamie Gritton db_printf(" ip6s = %d\n", pr->pr_ip6s); 4268b38ff370SJamie Gritton for (ii = 0; ii < pr->pr_ip6s; ii++) 4269b38ff370SJamie Gritton db_printf(" %s %s\n", 4270b38ff370SJamie Gritton ii == 0 ? "ip6 =" : " ", 4271b38ff370SJamie Gritton ip6_sprintf(ip6buf, &pr->pr_ip6[ii])); 4272b38ff370SJamie Gritton #endif 4273b38ff370SJamie Gritton } 4274b38ff370SJamie Gritton 4275b38ff370SJamie Gritton DB_SHOW_COMMAND(prison, db_show_prison_command) 4276b38ff370SJamie Gritton { 4277b38ff370SJamie Gritton struct prison *pr; 4278b38ff370SJamie Gritton 4279b38ff370SJamie Gritton if (!have_addr) { 42800304c731SJamie Gritton /* 42810304c731SJamie Gritton * Show all prisons in the list, and prison0 which is not 42820304c731SJamie Gritton * listed. 42830304c731SJamie Gritton */ 42840304c731SJamie Gritton db_show_prison(&prison0); 42850304c731SJamie Gritton if (!db_pager_quit) { 4286b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 4287b38ff370SJamie Gritton db_show_prison(pr); 4288413628a7SBjoern A. Zeeb if (db_pager_quit) 4289413628a7SBjoern A. Zeeb break; 4290413628a7SBjoern A. Zeeb } 42910304c731SJamie Gritton } 4292b38ff370SJamie Gritton return; 4293413628a7SBjoern A. Zeeb } 4294b38ff370SJamie Gritton 42950304c731SJamie Gritton if (addr == 0) 42960304c731SJamie Gritton pr = &prison0; 42970304c731SJamie Gritton else { 4298b38ff370SJamie Gritton /* Look for a prison with the ID and with references. */ 4299b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 4300b38ff370SJamie Gritton if (pr->pr_id == addr && pr->pr_ref > 0) 4301b38ff370SJamie Gritton break; 4302b38ff370SJamie Gritton if (pr == NULL) 4303b38ff370SJamie Gritton /* Look again, without requiring a reference. */ 4304b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 4305b38ff370SJamie Gritton if (pr->pr_id == addr) 4306b38ff370SJamie Gritton break; 4307b38ff370SJamie Gritton if (pr == NULL) 4308b38ff370SJamie Gritton /* Assume address points to a valid prison. */ 4309b38ff370SJamie Gritton pr = (struct prison *)addr; 43100304c731SJamie Gritton } 4311b38ff370SJamie Gritton db_show_prison(pr); 4312b38ff370SJamie Gritton } 4313b38ff370SJamie Gritton 4314413628a7SBjoern A. Zeeb #endif /* DDB */ 4315