kern_jail.c revision 280632
1139804Simp/*- 2185435Sbz * Copyright (c) 1999 Poul-Henning Kamp. 3185435Sbz * Copyright (c) 2008 Bjoern A. Zeeb. 4191673Sjamie * Copyright (c) 2009 James Gritton. 5185435Sbz * All rights reserved. 6190466Sjamie * 7185404Sbz * Redistribution and use in source and binary forms, with or without 8185404Sbz * modification, are permitted provided that the following conditions 9185404Sbz * are met: 10185404Sbz * 1. Redistributions of source code must retain the above copyright 11185404Sbz * notice, this list of conditions and the following disclaimer. 12185404Sbz * 2. Redistributions in binary form must reproduce the above copyright 13185404Sbz * notice, this list of conditions and the following disclaimer in the 14185404Sbz * documentation and/or other materials provided with the distribution. 15185404Sbz * 16185404Sbz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17185404Sbz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18185404Sbz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19185404Sbz * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20185404Sbz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21185404Sbz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22185404Sbz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23185404Sbz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24185404Sbz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25185404Sbz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26185404Sbz * SUCH DAMAGE. 2746197Sphk */ 2846155Sphk 29116182Sobrien#include <sys/cdefs.h> 30116182Sobrien__FBSDID("$FreeBSD: stable/10/sys/kern/kern_jail.c 280632 2015-03-25 20:57:54Z ian $"); 31116182Sobrien 32193066Sjamie#include "opt_compat.h" 33185435Sbz#include "opt_ddb.h" 34185435Sbz#include "opt_inet.h" 35185435Sbz#include "opt_inet6.h" 36131177Spjd 3746155Sphk#include <sys/param.h> 3846155Sphk#include <sys/types.h> 3946155Sphk#include <sys/kernel.h> 4046155Sphk#include <sys/systm.h> 4146155Sphk#include <sys/errno.h> 4246155Sphk#include <sys/sysproto.h> 4346155Sphk#include <sys/malloc.h> 44192895Sjamie#include <sys/osd.h> 45164032Srwatson#include <sys/priv.h> 4646155Sphk#include <sys/proc.h> 47124882Srwatson#include <sys/taskqueue.h> 48177785Skib#include <sys/fcntl.h> 4946155Sphk#include <sys/jail.h> 5087275Srwatson#include <sys/lock.h> 5187275Srwatson#include <sys/mutex.h> 52220137Strasz#include <sys/racct.h> 53221362Strasz#include <sys/refcount.h> 54168401Spjd#include <sys/sx.h> 55193066Sjamie#include <sys/sysent.h> 56113275Smike#include <sys/namei.h> 57147185Spjd#include <sys/mount.h> 58113275Smike#include <sys/queue.h> 5946155Sphk#include <sys/socket.h> 60113275Smike#include <sys/syscallsubr.h> 6157163Srwatson#include <sys/sysctl.h> 62113275Smike#include <sys/vnode.h> 63196019Srwatson 6446155Sphk#include <net/if.h> 65196019Srwatson#include <net/vnet.h> 66196019Srwatson 6746155Sphk#include <netinet/in.h> 68196019Srwatson 69185435Sbz#ifdef DDB 70185435Sbz#include <ddb/ddb.h> 71185435Sbz#ifdef INET6 72185435Sbz#include <netinet6/in6_var.h> 73185435Sbz#endif /* INET6 */ 74185435Sbz#endif /* DDB */ 7546155Sphk 76163606Srwatson#include <security/mac/mac_framework.h> 77163606Srwatson 78195944Sjamie#define DEFAULT_HOSTUUID "00000000-0000-0000-0000-000000000000" 79195944Sjamie 8046155SphkMALLOC_DEFINE(M_PRISON, "prison", "Prison structures"); 81227293Sedstatic MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures"); 8246155Sphk 83202468Sbz/* Keep struct prison prison0 and some code in kern_jail_set() readable. */ 84202468Sbz#ifdef INET 85202468Sbz#ifdef INET6 86202468Sbz#define _PR_IP_SADDRSEL PR_IP4_SADDRSEL|PR_IP6_SADDRSEL 87202468Sbz#else 88202468Sbz#define _PR_IP_SADDRSEL PR_IP4_SADDRSEL 89202468Sbz#endif 90202468Sbz#else /* !INET */ 91202468Sbz#ifdef INET6 92202468Sbz#define _PR_IP_SADDRSEL PR_IP6_SADDRSEL 93202468Sbz#else 94202468Sbz#define _PR_IP_SADDRSEL 0 95202468Sbz#endif 96202468Sbz#endif 97202468Sbz 98192895Sjamie/* prison0 describes what is "real" about the system. */ 99192895Sjamiestruct prison prison0 = { 100192895Sjamie .pr_id = 0, 101192895Sjamie .pr_name = "0", 102192895Sjamie .pr_ref = 1, 103192895Sjamie .pr_uref = 1, 104192895Sjamie .pr_path = "/", 105192895Sjamie .pr_securelevel = -1, 106231267Smm .pr_devfs_rsnum = 0, 107194762Sjamie .pr_childmax = JAIL_MAX, 108195944Sjamie .pr_hostuuid = DEFAULT_HOSTUUID, 109201145Santoine .pr_children = LIST_HEAD_INITIALIZER(prison0.pr_children), 110196176Sbz#ifdef VIMAGE 111202468Sbz .pr_flags = PR_HOST|PR_VNET|_PR_IP_SADDRSEL, 112196176Sbz#else 113202468Sbz .pr_flags = PR_HOST|_PR_IP_SADDRSEL, 114196176Sbz#endif 115192895Sjamie .pr_allow = PR_ALLOW_ALL, 116192895Sjamie}; 117192895SjamieMTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF); 11857163Srwatson 119221362Strasz/* allprison, allprison_racct and lastprid are protected by allprison_lock. */ 120168401Spjdstruct sx allprison_lock; 121191673SjamieSX_SYSINIT(allprison_lock, &allprison_lock, "allprison"); 122191673Sjamiestruct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison); 123221362StraszLIST_HEAD(, prison_racct) allprison_racct; 124179881Sdelphijint lastprid = 0; 125113275Smike 126191673Sjamiestatic int do_jail_attach(struct thread *td, struct prison *pr); 127190466Sjamiestatic void prison_complete(void *context, int pending); 128191673Sjamiestatic void prison_deref(struct prison *pr, int flags); 129192895Sjamiestatic char *prison_path(struct prison *pr1, struct prison *pr2); 130192895Sjamiestatic void prison_remove_one(struct prison *pr); 131221362Strasz#ifdef RACCT 132221362Straszstatic void prison_racct_attach(struct prison *pr); 133232598Straszstatic void prison_racct_modify(struct prison *pr); 134221362Straszstatic void prison_racct_detach(struct prison *pr); 135221362Strasz#endif 136185435Sbz#ifdef INET 137190466Sjamiestatic int _prison_check_ip4(struct prison *pr, struct in_addr *ia); 138192895Sjamiestatic int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4); 139185435Sbz#endif 140185435Sbz#ifdef INET6 141190466Sjamiestatic int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6); 142192895Sjamiestatic int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6); 143185435Sbz#endif 144113275Smike 145191673Sjamie/* Flags for prison_deref */ 146191673Sjamie#define PD_DEREF 0x01 147191673Sjamie#define PD_DEUREF 0x02 148191673Sjamie#define PD_LOCKED 0x04 149191673Sjamie#define PD_LIST_SLOCKED 0x08 150191673Sjamie#define PD_LIST_XLOCKED 0x10 151113275Smike 152192895Sjamie/* 153216861Sbz * Parameter names corresponding to PR_* flag values. Size values are for kvm 154216861Sbz * as we cannot figure out the size of a sparse array, or an array without a 155216861Sbz * terminating entry. 156192895Sjamie */ 157192895Sjamiestatic char *pr_flag_names[] = { 158192895Sjamie [0] = "persist", 159202468Sbz#ifdef INET 160202468Sbz [7] = "ip4.saddrsel", 161202468Sbz#endif 162202468Sbz#ifdef INET6 163202468Sbz [8] = "ip6.saddrsel", 164202468Sbz#endif 165192895Sjamie}; 166216861Sbzconst size_t pr_flag_names_size = sizeof(pr_flag_names); 167192895Sjamie 168192895Sjamiestatic char *pr_flag_nonames[] = { 169192895Sjamie [0] = "nopersist", 170202468Sbz#ifdef INET 171202468Sbz [7] = "ip4.nosaddrsel", 172202468Sbz#endif 173202468Sbz#ifdef INET6 174202468Sbz [8] = "ip6.nosaddrsel", 175202468Sbz#endif 176195870Sjamie}; 177216861Sbzconst size_t pr_flag_nonames_size = sizeof(pr_flag_nonames); 178195870Sjamie 179195870Sjamiestruct jailsys_flags { 180195870Sjamie const char *name; 181195870Sjamie unsigned disable; 182195870Sjamie unsigned new; 183195870Sjamie} pr_flag_jailsys[] = { 184195870Sjamie { "host", 0, PR_HOST }, 185195870Sjamie#ifdef VIMAGE 186195870Sjamie { "vnet", 0, PR_VNET }, 187195870Sjamie#endif 188192895Sjamie#ifdef INET 189195870Sjamie { "ip4", PR_IP4_USER | PR_IP4_DISABLE, PR_IP4_USER }, 190192895Sjamie#endif 191192895Sjamie#ifdef INET6 192195870Sjamie { "ip6", PR_IP6_USER | PR_IP6_DISABLE, PR_IP6_USER }, 193192895Sjamie#endif 194192895Sjamie}; 195216861Sbzconst size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys); 196192895Sjamie 197192895Sjamiestatic char *pr_allow_names[] = { 198192895Sjamie "allow.set_hostname", 199192895Sjamie "allow.sysvipc", 200192895Sjamie "allow.raw_sockets", 201192895Sjamie "allow.chflags", 202192895Sjamie "allow.mount", 203192895Sjamie "allow.quotas", 204192895Sjamie "allow.socket_af", 205232059Smm "allow.mount.devfs", 206232059Smm "allow.mount.nullfs", 207232186Smm "allow.mount.zfs", 208232278Smm "allow.mount.procfs", 209254741Sdelphij "allow.mount.tmpfs", 210277985Sjamie "allow.mount.fdescfs", 211192895Sjamie}; 212216861Sbzconst size_t pr_allow_names_size = sizeof(pr_allow_names); 213192895Sjamie 214192895Sjamiestatic char *pr_allow_nonames[] = { 215192895Sjamie "allow.noset_hostname", 216192895Sjamie "allow.nosysvipc", 217192895Sjamie "allow.noraw_sockets", 218192895Sjamie "allow.nochflags", 219192895Sjamie "allow.nomount", 220192895Sjamie "allow.noquotas", 221192895Sjamie "allow.nosocket_af", 222232059Smm "allow.mount.nodevfs", 223232059Smm "allow.mount.nonullfs", 224232186Smm "allow.mount.nozfs", 225232278Smm "allow.mount.noprocfs", 226254741Sdelphij "allow.mount.notmpfs", 227277985Sjamie "allow.mount.nofdescfs", 228192895Sjamie}; 229216861Sbzconst size_t pr_allow_nonames_size = sizeof(pr_allow_nonames); 230192895Sjamie 231196002Sjamie#define JAIL_DEFAULT_ALLOW PR_ALLOW_SET_HOSTNAME 232196002Sjamie#define JAIL_DEFAULT_ENFORCE_STATFS 2 233232059Smm#define JAIL_DEFAULT_DEVFS_RSNUM 0 234192895Sjamiestatic unsigned jail_default_allow = JAIL_DEFAULT_ALLOW; 235196002Sjamiestatic int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 236231267Smmstatic int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM; 237192895Sjamie#if defined(INET) || defined(INET6) 238193865Sjamiestatic unsigned jail_max_af_ips = 255; 239192895Sjamie#endif 240192895Sjamie 241280632Sian/* 242280632Sian * Initialize the parts of prison0 that can't be static-initialized with 243280632Sian * constants. This is called from proc0_init() after creating thread0 cpuset. 244280632Sian */ 245280632Sianvoid 246280632Sianprison0_init(void) 247280632Sian{ 248280632Sian 249280632Sian prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset); 250280632Sian prison0.pr_osreldate = osreldate; 251280632Sian strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease)); 252280632Sian} 253280632Sian 254192895Sjamie#ifdef INET 255185435Sbzstatic int 256185435Sbzqcmp_v4(const void *ip1, const void *ip2) 257185435Sbz{ 258185435Sbz in_addr_t iaa, iab; 259185435Sbz 260185435Sbz /* 261185435Sbz * We need to compare in HBO here to get the list sorted as expected 262185435Sbz * by the result of the code. Sorting NBO addresses gives you 263185435Sbz * interesting results. If you do not understand, do not try. 264185435Sbz */ 265185435Sbz iaa = ntohl(((const struct in_addr *)ip1)->s_addr); 266185435Sbz iab = ntohl(((const struct in_addr *)ip2)->s_addr); 267185435Sbz 268185435Sbz /* 269185435Sbz * Do not simply return the difference of the two numbers, the int is 270185435Sbz * not wide enough. 271185435Sbz */ 272185435Sbz if (iaa > iab) 273185435Sbz return (1); 274185435Sbz else if (iaa < iab) 275185435Sbz return (-1); 276185435Sbz else 277185435Sbz return (0); 278185435Sbz} 279185435Sbz#endif 280185435Sbz 281185435Sbz#ifdef INET6 282185435Sbzstatic int 283185435Sbzqcmp_v6(const void *ip1, const void *ip2) 284185435Sbz{ 285185435Sbz const struct in6_addr *ia6a, *ia6b; 286185435Sbz int i, rc; 287185435Sbz 288185435Sbz ia6a = (const struct in6_addr *)ip1; 289185435Sbz ia6b = (const struct in6_addr *)ip2; 290185435Sbz 291185435Sbz rc = 0; 292190466Sjamie for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) { 293185435Sbz if (ia6a->s6_addr[i] > ia6b->s6_addr[i]) 294185435Sbz rc = 1; 295185435Sbz else if (ia6a->s6_addr[i] < ia6b->s6_addr[i]) 296185435Sbz rc = -1; 297185435Sbz } 298185435Sbz return (rc); 299185435Sbz} 300185435Sbz#endif 301185435Sbz 302191673Sjamie/* 303191673Sjamie * struct jail_args { 304191673Sjamie * struct jail *jail; 305191673Sjamie * }; 306191673Sjamie */ 307191673Sjamieint 308225617Skmacysys_jail(struct thread *td, struct jail_args *uap) 309185435Sbz{ 310191673Sjamie uint32_t version; 311191673Sjamie int error; 312192895Sjamie struct jail j; 313185435Sbz 314191673Sjamie error = copyin(uap->jail, &version, sizeof(uint32_t)); 315191673Sjamie if (error) 316191673Sjamie return (error); 317185435Sbz 318191673Sjamie switch (version) { 319191673Sjamie case 0: 320191673Sjamie { 321191673Sjamie struct jail_v0 j0; 322185435Sbz 323192895Sjamie /* FreeBSD single IPv4 jails. */ 324192895Sjamie bzero(&j, sizeof(struct jail)); 325191673Sjamie error = copyin(uap->jail, &j0, sizeof(struct jail_v0)); 326191673Sjamie if (error) 327191673Sjamie return (error); 328192895Sjamie j.version = j0.version; 329192895Sjamie j.path = j0.path; 330192895Sjamie j.hostname = j0.hostname; 331258929Speter j.ip4s = htonl(j0.ip_number); /* jail_v0 is host order */ 332191673Sjamie break; 333191673Sjamie } 334191673Sjamie 335191673Sjamie case 1: 336185435Sbz /* 337191673Sjamie * Version 1 was used by multi-IPv4 jail implementations 338191673Sjamie * that never made it into the official kernel. 339185435Sbz */ 340191673Sjamie return (EINVAL); 341185435Sbz 342191673Sjamie case 2: /* JAIL_API_VERSION */ 343191673Sjamie /* FreeBSD multi-IPv4/IPv6,noIP jails. */ 344191673Sjamie error = copyin(uap->jail, &j, sizeof(struct jail)); 345191673Sjamie if (error) 346191673Sjamie return (error); 347192895Sjamie break; 348192895Sjamie 349192895Sjamie default: 350192895Sjamie /* Sci-Fi jails are not supported, sorry. */ 351192895Sjamie return (EINVAL); 352192895Sjamie } 353192895Sjamie return (kern_jail(td, &j)); 354192895Sjamie} 355192895Sjamie 356192895Sjamieint 357192895Sjamiekern_jail(struct thread *td, struct jail *j) 358192895Sjamie{ 359193865Sjamie struct iovec optiov[2 * (4 360193865Sjamie + sizeof(pr_allow_names) / sizeof(pr_allow_names[0]) 361193865Sjamie#ifdef INET 362193865Sjamie + 1 363193865Sjamie#endif 364193865Sjamie#ifdef INET6 365193865Sjamie + 1 366193865Sjamie#endif 367193865Sjamie )]; 368192895Sjamie struct uio opt; 369192895Sjamie char *u_path, *u_hostname, *u_name; 370185435Sbz#ifdef INET 371193865Sjamie uint32_t ip4s; 372192895Sjamie struct in_addr *u_ip4; 373192895Sjamie#endif 374192895Sjamie#ifdef INET6 375192895Sjamie struct in6_addr *u_ip6; 376192895Sjamie#endif 377192895Sjamie size_t tmplen; 378192895Sjamie int error, enforce_statfs, fi; 379192895Sjamie 380192895Sjamie bzero(&optiov, sizeof(optiov)); 381192895Sjamie opt.uio_iov = optiov; 382192895Sjamie opt.uio_iovcnt = 0; 383192895Sjamie opt.uio_offset = -1; 384192895Sjamie opt.uio_resid = -1; 385192895Sjamie opt.uio_segflg = UIO_SYSSPACE; 386192895Sjamie opt.uio_rw = UIO_READ; 387192895Sjamie opt.uio_td = td; 388192895Sjamie 389192895Sjamie /* Set permissions for top-level jails from sysctls. */ 390192895Sjamie if (!jailed(td->td_ucred)) { 391192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / 392192895Sjamie sizeof(pr_allow_names[0]); fi++) { 393192895Sjamie optiov[opt.uio_iovcnt].iov_base = 394192895Sjamie (jail_default_allow & (1 << fi)) 395192895Sjamie ? pr_allow_names[fi] : pr_allow_nonames[fi]; 396192895Sjamie optiov[opt.uio_iovcnt].iov_len = 397192895Sjamie strlen(optiov[opt.uio_iovcnt].iov_base) + 1; 398192895Sjamie opt.uio_iovcnt += 2; 399192895Sjamie } 400192895Sjamie optiov[opt.uio_iovcnt].iov_base = "enforce_statfs"; 401192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs"); 402192895Sjamie opt.uio_iovcnt++; 403192895Sjamie enforce_statfs = jail_default_enforce_statfs; 404192895Sjamie optiov[opt.uio_iovcnt].iov_base = &enforce_statfs; 405192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs); 406192895Sjamie opt.uio_iovcnt++; 407192895Sjamie } 408192895Sjamie 409192895Sjamie tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN; 410192895Sjamie#ifdef INET 411192895Sjamie ip4s = (j->version == 0) ? 1 : j->ip4s; 412192895Sjamie if (ip4s > jail_max_af_ips) 413192895Sjamie return (EINVAL); 414192895Sjamie tmplen += ip4s * sizeof(struct in_addr); 415191673Sjamie#else 416192895Sjamie if (j->ip4s > 0) 417192895Sjamie return (EINVAL); 418191673Sjamie#endif 419191673Sjamie#ifdef INET6 420192895Sjamie if (j->ip6s > jail_max_af_ips) 421192895Sjamie return (EINVAL); 422192895Sjamie tmplen += j->ip6s * sizeof(struct in6_addr); 423191673Sjamie#else 424192895Sjamie if (j->ip6s > 0) 425192895Sjamie return (EINVAL); 426191673Sjamie#endif 427192895Sjamie u_path = malloc(tmplen, M_TEMP, M_WAITOK); 428192895Sjamie u_hostname = u_path + MAXPATHLEN; 429192895Sjamie u_name = u_hostname + MAXHOSTNAMELEN; 430191673Sjamie#ifdef INET 431192895Sjamie u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN); 432191673Sjamie#endif 433191673Sjamie#ifdef INET6 434191673Sjamie#ifdef INET 435192895Sjamie u_ip6 = (struct in6_addr *)(u_ip4 + ip4s); 436191673Sjamie#else 437192895Sjamie u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN); 438191673Sjamie#endif 439191673Sjamie#endif 440192895Sjamie optiov[opt.uio_iovcnt].iov_base = "path"; 441192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("path"); 442192895Sjamie opt.uio_iovcnt++; 443192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_path; 444192895Sjamie error = copyinstr(j->path, u_path, MAXPATHLEN, 445192895Sjamie &optiov[opt.uio_iovcnt].iov_len); 446192895Sjamie if (error) { 447192895Sjamie free(u_path, M_TEMP); 448192895Sjamie return (error); 449192895Sjamie } 450192895Sjamie opt.uio_iovcnt++; 451192895Sjamie optiov[opt.uio_iovcnt].iov_base = "host.hostname"; 452192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname"); 453192895Sjamie opt.uio_iovcnt++; 454192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_hostname; 455192895Sjamie error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN, 456192895Sjamie &optiov[opt.uio_iovcnt].iov_len); 457192895Sjamie if (error) { 458192895Sjamie free(u_path, M_TEMP); 459192895Sjamie return (error); 460192895Sjamie } 461192895Sjamie opt.uio_iovcnt++; 462192895Sjamie if (j->jailname != NULL) { 463192895Sjamie optiov[opt.uio_iovcnt].iov_base = "name"; 464192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("name"); 465192895Sjamie opt.uio_iovcnt++; 466192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_name; 467192895Sjamie error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN, 468192895Sjamie &optiov[opt.uio_iovcnt].iov_len); 469191673Sjamie if (error) { 470191673Sjamie free(u_path, M_TEMP); 471191673Sjamie return (error); 472191673Sjamie } 473192895Sjamie opt.uio_iovcnt++; 474192895Sjamie } 475191673Sjamie#ifdef INET 476192895Sjamie optiov[opt.uio_iovcnt].iov_base = "ip4.addr"; 477192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr"); 478192895Sjamie opt.uio_iovcnt++; 479192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_ip4; 480192895Sjamie optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr); 481192895Sjamie if (j->version == 0) 482192895Sjamie u_ip4->s_addr = j->ip4s; 483192895Sjamie else { 484192895Sjamie error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len); 485191673Sjamie if (error) { 486191673Sjamie free(u_path, M_TEMP); 487191673Sjamie return (error); 488191673Sjamie } 489192895Sjamie } 490192895Sjamie opt.uio_iovcnt++; 491185435Sbz#endif 492185435Sbz#ifdef INET6 493192895Sjamie optiov[opt.uio_iovcnt].iov_base = "ip6.addr"; 494192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr"); 495192895Sjamie opt.uio_iovcnt++; 496192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_ip6; 497192895Sjamie optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr); 498192895Sjamie error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len); 499192895Sjamie if (error) { 500192895Sjamie free(u_path, M_TEMP); 501192895Sjamie return (error); 502192895Sjamie } 503192895Sjamie opt.uio_iovcnt++; 504185435Sbz#endif 505192895Sjamie KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]), 506192895Sjamie ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt)); 507191673Sjamie error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH); 508191673Sjamie free(u_path, M_TEMP); 509191673Sjamie return (error); 510185435Sbz} 511185435Sbz 512192895Sjamie 513191673Sjamie/* 514191673Sjamie * struct jail_set_args { 515191673Sjamie * struct iovec *iovp; 516191673Sjamie * unsigned int iovcnt; 517191673Sjamie * int flags; 518191673Sjamie * }; 519191673Sjamie */ 520191673Sjamieint 521225617Skmacysys_jail_set(struct thread *td, struct jail_set_args *uap) 522185435Sbz{ 523191673Sjamie struct uio *auio; 524191673Sjamie int error; 525191673Sjamie 526191673Sjamie /* Check that we have an even number of iovecs. */ 527191673Sjamie if (uap->iovcnt & 1) 528191673Sjamie return (EINVAL); 529191673Sjamie 530191673Sjamie error = copyinuio(uap->iovp, uap->iovcnt, &auio); 531191673Sjamie if (error) 532191673Sjamie return (error); 533191673Sjamie error = kern_jail_set(td, auio, uap->flags); 534191673Sjamie free(auio, M_IOV); 535191673Sjamie return (error); 536191673Sjamie} 537191673Sjamie 538191673Sjamieint 539191673Sjamiekern_jail_set(struct thread *td, struct uio *optuio, int flags) 540191673Sjamie{ 541191673Sjamie struct nameidata nd; 542185435Sbz#ifdef INET 543190466Sjamie struct in_addr *ip4; 544185435Sbz#endif 545185435Sbz#ifdef INET6 546185435Sbz struct in6_addr *ip6; 547185435Sbz#endif 548191673Sjamie struct vfsopt *opt; 549191673Sjamie struct vfsoptlist *opts; 550196135Sbz struct prison *pr, *deadpr, *mypr, *ppr, *tpr; 551191673Sjamie struct vnode *root; 552196835Sjamie char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid; 553280632Sian char *g_path, *osrelstr; 554192895Sjamie#if defined(INET) || defined(INET6) 555196135Sbz struct prison *tppr; 556191673Sjamie void *op; 557192895Sjamie#endif 558193066Sjamie unsigned long hid; 559192895Sjamie size_t namelen, onamelen; 560192895Sjamie int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos; 561231267Smm int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel; 562195870Sjamie int fi, jid, jsys, len, level; 563280632Sian int childmax, osreldt, rsnum, slevel; 564230129Smm int fullpath_disabled; 565191673Sjamie#if defined(INET) || defined(INET6) 566192895Sjamie int ii, ij; 567191673Sjamie#endif 568191673Sjamie#ifdef INET 569195974Sjamie int ip4s, redo_ip4; 570191673Sjamie#endif 571191673Sjamie#ifdef INET6 572195974Sjamie int ip6s, redo_ip6; 573191673Sjamie#endif 574224290Smckusick uint64_t pr_allow, ch_allow, pr_flags, ch_flags; 575224290Smckusick unsigned tallow; 576191673Sjamie char numbuf[12]; 577185435Sbz 578191673Sjamie error = priv_check(td, PRIV_JAIL_SET); 579191673Sjamie if (!error && (flags & JAIL_ATTACH)) 580191673Sjamie error = priv_check(td, PRIV_JAIL_ATTACH); 581191673Sjamie if (error) 582191673Sjamie return (error); 583192895Sjamie mypr = ppr = td->td_ucred->cr_prison; 584194762Sjamie if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0) 585192895Sjamie return (EPERM); 586191673Sjamie if (flags & ~JAIL_SET_MASK) 587191673Sjamie return (EINVAL); 588191673Sjamie 589185435Sbz /* 590191673Sjamie * Check all the parameters before committing to anything. Not all 591191673Sjamie * errors can be caught early, but we may as well try. Also, this 592191673Sjamie * takes care of some expensive stuff (path lookup) before getting 593191673Sjamie * the allprison lock. 594185435Sbz * 595191673Sjamie * XXX Jails are not filesystems, and jail parameters are not mount 596191673Sjamie * options. But it makes more sense to re-use the vfsopt code 597191673Sjamie * than duplicate it under a different name. 598185435Sbz */ 599191673Sjamie error = vfs_buildopts(optuio, &opts); 600191673Sjamie if (error) 601191673Sjamie return (error); 602185435Sbz#ifdef INET 603185435Sbz ip4 = NULL; 604185435Sbz#endif 605185435Sbz#ifdef INET6 606185435Sbz ip6 = NULL; 607185435Sbz#endif 608230407Smm g_path = NULL; 609191673Sjamie 610191673Sjamie error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 611191673Sjamie if (error == ENOENT) 612191673Sjamie jid = 0; 613191673Sjamie else if (error != 0) 614191673Sjamie goto done_free; 615191673Sjamie 616191673Sjamie error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel)); 617191673Sjamie if (error == ENOENT) 618191673Sjamie gotslevel = 0; 619191673Sjamie else if (error != 0) 620191673Sjamie goto done_free; 621191673Sjamie else 622191673Sjamie gotslevel = 1; 623191673Sjamie 624194762Sjamie error = 625194762Sjamie vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax)); 626194762Sjamie if (error == ENOENT) 627194762Sjamie gotchildmax = 0; 628194762Sjamie else if (error != 0) 629194762Sjamie goto done_free; 630194762Sjamie else 631194762Sjamie gotchildmax = 1; 632194762Sjamie 633192895Sjamie error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce)); 634212436Sjamie if (error == ENOENT) 635212436Sjamie gotenforce = 0; 636212436Sjamie else if (error != 0) 637192895Sjamie goto done_free; 638212436Sjamie else if (enforce < 0 || enforce > 2) { 639212436Sjamie error = EINVAL; 640212436Sjamie goto done_free; 641212436Sjamie } else 642212436Sjamie gotenforce = 1; 643192895Sjamie 644231267Smm error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum)); 645231267Smm if (error == ENOENT) 646231267Smm gotrsnum = 0; 647231267Smm else if (error != 0) 648231267Smm goto done_free; 649231267Smm else 650231267Smm gotrsnum = 1; 651231267Smm 652191673Sjamie pr_flags = ch_flags = 0; 653192895Sjamie for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 654192895Sjamie fi++) { 655192895Sjamie if (pr_flag_names[fi] == NULL) 656192895Sjamie continue; 657192895Sjamie vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi); 658192895Sjamie vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi); 659192895Sjamie } 660191673Sjamie ch_flags |= pr_flags; 661195870Sjamie for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 662195870Sjamie fi++) { 663195870Sjamie error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys, 664195870Sjamie sizeof(jsys)); 665195870Sjamie if (error == ENOENT) 666195870Sjamie continue; 667195870Sjamie if (error != 0) 668195870Sjamie goto done_free; 669195870Sjamie switch (jsys) { 670195870Sjamie case JAIL_SYS_DISABLE: 671195870Sjamie if (!pr_flag_jailsys[fi].disable) { 672195870Sjamie error = EINVAL; 673195870Sjamie goto done_free; 674195870Sjamie } 675195870Sjamie pr_flags |= pr_flag_jailsys[fi].disable; 676195870Sjamie break; 677195870Sjamie case JAIL_SYS_NEW: 678195870Sjamie pr_flags |= pr_flag_jailsys[fi].new; 679195870Sjamie break; 680195870Sjamie case JAIL_SYS_INHERIT: 681195870Sjamie break; 682195870Sjamie default: 683195870Sjamie error = EINVAL; 684195870Sjamie goto done_free; 685195870Sjamie } 686195870Sjamie ch_flags |= 687195870Sjamie pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable; 688195870Sjamie } 689211085Sjamie if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE 690211085Sjamie && !(pr_flags & PR_PERSIST)) { 691211085Sjamie error = EINVAL; 692211085Sjamie vfs_opterror(opts, "new jail must persist or attach"); 693211085Sjamie goto done_errmsg; 694211085Sjamie } 695194251Sjamie#ifdef VIMAGE 696194251Sjamie if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) { 697194251Sjamie error = EINVAL; 698194251Sjamie vfs_opterror(opts, "vnet cannot be changed after creation"); 699194251Sjamie goto done_errmsg; 700194251Sjamie } 701194251Sjamie#endif 702195974Sjamie#ifdef INET 703195974Sjamie if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) { 704195974Sjamie error = EINVAL; 705195974Sjamie vfs_opterror(opts, "ip4 cannot be changed after creation"); 706195974Sjamie goto done_errmsg; 707195974Sjamie } 708195974Sjamie#endif 709195974Sjamie#ifdef INET6 710195974Sjamie if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) { 711195974Sjamie error = EINVAL; 712195974Sjamie vfs_opterror(opts, "ip6 cannot be changed after creation"); 713195974Sjamie goto done_errmsg; 714195974Sjamie } 715195974Sjamie#endif 716191673Sjamie 717192895Sjamie pr_allow = ch_allow = 0; 718192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 719192895Sjamie fi++) { 720192895Sjamie vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi); 721192895Sjamie vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi); 722192895Sjamie } 723192895Sjamie ch_allow |= pr_allow; 724192895Sjamie 725191673Sjamie error = vfs_getopt(opts, "name", (void **)&name, &len); 726191673Sjamie if (error == ENOENT) 727191673Sjamie name = NULL; 728191673Sjamie else if (error != 0) 729191673Sjamie goto done_free; 730191673Sjamie else { 731191673Sjamie if (len == 0 || name[len - 1] != '\0') { 732191673Sjamie error = EINVAL; 733191673Sjamie goto done_free; 734191673Sjamie } 735191673Sjamie if (len > MAXHOSTNAMELEN) { 736191673Sjamie error = ENAMETOOLONG; 737191673Sjamie goto done_free; 738191673Sjamie } 739191673Sjamie } 740191673Sjamie 741191673Sjamie error = vfs_getopt(opts, "host.hostname", (void **)&host, &len); 742191673Sjamie if (error == ENOENT) 743191673Sjamie host = NULL; 744191673Sjamie else if (error != 0) 745191673Sjamie goto done_free; 746191673Sjamie else { 747193066Sjamie ch_flags |= PR_HOST; 748193066Sjamie pr_flags |= PR_HOST; 749191673Sjamie if (len == 0 || host[len - 1] != '\0') { 750191673Sjamie error = EINVAL; 751191673Sjamie goto done_free; 752191673Sjamie } 753191673Sjamie if (len > MAXHOSTNAMELEN) { 754191673Sjamie error = ENAMETOOLONG; 755191673Sjamie goto done_free; 756191673Sjamie } 757191673Sjamie } 758191673Sjamie 759193066Sjamie error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len); 760193066Sjamie if (error == ENOENT) 761193066Sjamie domain = NULL; 762193066Sjamie else if (error != 0) 763193066Sjamie goto done_free; 764193066Sjamie else { 765193066Sjamie ch_flags |= PR_HOST; 766193066Sjamie pr_flags |= PR_HOST; 767193066Sjamie if (len == 0 || domain[len - 1] != '\0') { 768193066Sjamie error = EINVAL; 769193066Sjamie goto done_free; 770193066Sjamie } 771193066Sjamie if (len > MAXHOSTNAMELEN) { 772193066Sjamie error = ENAMETOOLONG; 773193066Sjamie goto done_free; 774193066Sjamie } 775193066Sjamie } 776193066Sjamie 777193066Sjamie error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len); 778193066Sjamie if (error == ENOENT) 779193066Sjamie uuid = NULL; 780193066Sjamie else if (error != 0) 781193066Sjamie goto done_free; 782193066Sjamie else { 783193066Sjamie ch_flags |= PR_HOST; 784193066Sjamie pr_flags |= PR_HOST; 785193066Sjamie if (len == 0 || uuid[len - 1] != '\0') { 786193066Sjamie error = EINVAL; 787193066Sjamie goto done_free; 788193066Sjamie } 789193066Sjamie if (len > HOSTUUIDLEN) { 790193066Sjamie error = ENAMETOOLONG; 791193066Sjamie goto done_free; 792193066Sjamie } 793193066Sjamie } 794193066Sjamie 795205014Snwhitehorn#ifdef COMPAT_FREEBSD32 796217896Sdchagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 797193066Sjamie uint32_t hid32; 798193066Sjamie 799193066Sjamie error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32)); 800193066Sjamie hid = hid32; 801193066Sjamie } else 802193066Sjamie#endif 803193066Sjamie error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid)); 804193066Sjamie if (error == ENOENT) 805193066Sjamie gothid = 0; 806193066Sjamie else if (error != 0) 807193066Sjamie goto done_free; 808193066Sjamie else { 809193066Sjamie gothid = 1; 810193066Sjamie ch_flags |= PR_HOST; 811193066Sjamie pr_flags |= PR_HOST; 812193066Sjamie } 813193066Sjamie 814185435Sbz#ifdef INET 815191673Sjamie error = vfs_getopt(opts, "ip4.addr", &op, &ip4s); 816191673Sjamie if (error == ENOENT) 817277279Sjamie ip4s = 0; 818191673Sjamie else if (error != 0) 819191673Sjamie goto done_free; 820191673Sjamie else if (ip4s & (sizeof(*ip4) - 1)) { 821191673Sjamie error = EINVAL; 822191673Sjamie goto done_free; 823192895Sjamie } else { 824195870Sjamie ch_flags |= PR_IP4_USER | PR_IP4_DISABLE; 825195870Sjamie if (ip4s == 0) 826195870Sjamie pr_flags |= PR_IP4_USER | PR_IP4_DISABLE; 827195870Sjamie else { 828195870Sjamie pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER; 829192895Sjamie ip4s /= sizeof(*ip4); 830192895Sjamie if (ip4s > jail_max_af_ips) { 831185435Sbz error = EINVAL; 832192895Sjamie vfs_opterror(opts, "too many IPv4 addresses"); 833192895Sjamie goto done_errmsg; 834185435Sbz } 835195974Sjamie ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 836192895Sjamie bcopy(op, ip4, ip4s * sizeof(*ip4)); 837192895Sjamie /* 838192895Sjamie * IP addresses are all sorted but ip[0] to preserve 839192895Sjamie * the primary IP address as given from userland. 840192895Sjamie * This special IP is used for unbound outgoing 841202116Sbz * connections as well for "loopback" traffic in case 842202116Sbz * source address selection cannot find any more fitting 843202116Sbz * address to connect from. 844192895Sjamie */ 845192895Sjamie if (ip4s > 1) 846192895Sjamie qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4); 847192895Sjamie /* 848192895Sjamie * Check for duplicate addresses and do some simple 849192895Sjamie * zero and broadcast checks. If users give other bogus 850192895Sjamie * addresses it is their problem. 851192895Sjamie * 852192895Sjamie * We do not have to care about byte order for these 853192895Sjamie * checks so we will do them in NBO. 854192895Sjamie */ 855192895Sjamie for (ii = 0; ii < ip4s; ii++) { 856192895Sjamie if (ip4[ii].s_addr == INADDR_ANY || 857192895Sjamie ip4[ii].s_addr == INADDR_BROADCAST) { 858192895Sjamie error = EINVAL; 859192895Sjamie goto done_free; 860192895Sjamie } 861192895Sjamie if ((ii+1) < ip4s && 862192895Sjamie (ip4[0].s_addr == ip4[ii+1].s_addr || 863192895Sjamie ip4[ii].s_addr == ip4[ii+1].s_addr)) { 864192895Sjamie error = EINVAL; 865192895Sjamie goto done_free; 866192895Sjamie } 867192895Sjamie } 868185435Sbz } 869191673Sjamie } 870191673Sjamie#endif 871185435Sbz 872185435Sbz#ifdef INET6 873191673Sjamie error = vfs_getopt(opts, "ip6.addr", &op, &ip6s); 874191673Sjamie if (error == ENOENT) 875277279Sjamie ip6s = 0; 876191673Sjamie else if (error != 0) 877191673Sjamie goto done_free; 878191673Sjamie else if (ip6s & (sizeof(*ip6) - 1)) { 879191673Sjamie error = EINVAL; 880191673Sjamie goto done_free; 881192895Sjamie } else { 882195870Sjamie ch_flags |= PR_IP6_USER | PR_IP6_DISABLE; 883195870Sjamie if (ip6s == 0) 884195870Sjamie pr_flags |= PR_IP6_USER | PR_IP6_DISABLE; 885195870Sjamie else { 886195870Sjamie pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER; 887192895Sjamie ip6s /= sizeof(*ip6); 888192895Sjamie if (ip6s > jail_max_af_ips) { 889185435Sbz error = EINVAL; 890192895Sjamie vfs_opterror(opts, "too many IPv6 addresses"); 891192895Sjamie goto done_errmsg; 892185435Sbz } 893195974Sjamie ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 894192895Sjamie bcopy(op, ip6, ip6s * sizeof(*ip6)); 895192895Sjamie if (ip6s > 1) 896192895Sjamie qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6); 897192895Sjamie for (ii = 0; ii < ip6s; ii++) { 898192895Sjamie if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) { 899192895Sjamie error = EINVAL; 900192895Sjamie goto done_free; 901192895Sjamie } 902192895Sjamie if ((ii+1) < ip6s && 903192895Sjamie (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) || 904192895Sjamie IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1]))) 905192895Sjamie { 906192895Sjamie error = EINVAL; 907192895Sjamie goto done_free; 908192895Sjamie } 909192895Sjamie } 910185435Sbz } 911191673Sjamie } 912185435Sbz#endif 913185435Sbz 914195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6)) 915195945Sjamie if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 916195945Sjamie error = EINVAL; 917195945Sjamie vfs_opterror(opts, 918195945Sjamie "vnet jails cannot have IP address restrictions"); 919195945Sjamie goto done_errmsg; 920195945Sjamie } 921195945Sjamie#endif 922195945Sjamie 923230143Smm fullpath_disabled = 0; 924191673Sjamie root = NULL; 925191673Sjamie error = vfs_getopt(opts, "path", (void **)&path, &len); 926191673Sjamie if (error == ENOENT) 927191673Sjamie path = NULL; 928191673Sjamie else if (error != 0) 929191673Sjamie goto done_free; 930191673Sjamie else { 931191673Sjamie if (flags & JAIL_UPDATE) { 932191673Sjamie error = EINVAL; 933191673Sjamie vfs_opterror(opts, 934191673Sjamie "path cannot be changed after creation"); 935191673Sjamie goto done_errmsg; 936191673Sjamie } 937191673Sjamie if (len == 0 || path[len - 1] != '\0') { 938191673Sjamie error = EINVAL; 939191673Sjamie goto done_free; 940191673Sjamie } 941241896Skib NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, 942230129Smm path, td); 943230129Smm error = namei(&nd); 944230129Smm if (error) 945230129Smm goto done_free; 946230129Smm root = nd.ni_vp; 947230129Smm NDFREE(&nd, NDF_ONLY_PNBUF); 948230407Smm g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 949230407Smm strlcpy(g_path, path, MAXPATHLEN); 950230407Smm error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN); 951230407Smm if (error == 0) 952230407Smm path = g_path; 953230407Smm else if (error == ENODEV) { 954230129Smm /* proceed if sysctl debug.disablefullpath == 1 */ 955230129Smm fullpath_disabled = 1; 956230129Smm if (len < 2 || (len == 2 && path[0] == '/')) 957230129Smm path = NULL; 958230407Smm } else { 959230129Smm /* exit on other errors */ 960230129Smm goto done_free; 961230129Smm } 962230129Smm if (root->v_type != VDIR) { 963230129Smm error = ENOTDIR; 964230129Smm vput(root); 965230129Smm goto done_free; 966230129Smm } 967230129Smm VOP_UNLOCK(root, 0); 968230129Smm if (fullpath_disabled) { 969192895Sjamie /* Leave room for a real-root full pathname. */ 970192895Sjamie if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/") 971192895Sjamie ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) { 972192895Sjamie error = ENAMETOOLONG; 973192895Sjamie goto done_free; 974192895Sjamie } 975191673Sjamie } 976191673Sjamie } 977185435Sbz 978280632Sian error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len); 979280632Sian if (error == ENOENT) 980280632Sian osrelstr = NULL; 981280632Sian else if (error != 0) 982280632Sian goto done_free; 983280632Sian else { 984280632Sian if (flags & JAIL_UPDATE) { 985280632Sian error = EINVAL; 986280632Sian vfs_opterror(opts, 987280632Sian "osrelease cannot be changed after creation"); 988280632Sian goto done_errmsg; 989280632Sian } 990280632Sian if (len == 0 || len >= OSRELEASELEN) { 991280632Sian error = EINVAL; 992280632Sian vfs_opterror(opts, 993280632Sian "osrelease string must be 1-%d bytes long", 994280632Sian OSRELEASELEN - 1); 995280632Sian goto done_errmsg; 996280632Sian } 997280632Sian } 998280632Sian 999280632Sian error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt)); 1000280632Sian if (error == ENOENT) 1001280632Sian osreldt = 0; 1002280632Sian else if (error != 0) 1003280632Sian goto done_free; 1004280632Sian else { 1005280632Sian if (flags & JAIL_UPDATE) { 1006280632Sian error = EINVAL; 1007280632Sian vfs_opterror(opts, 1008280632Sian "osreldate cannot be changed after creation"); 1009280632Sian goto done_errmsg; 1010280632Sian } 1011280632Sian if (osreldt == 0) { 1012280632Sian error = EINVAL; 1013280632Sian vfs_opterror(opts, "osreldate cannot be 0"); 1014280632Sian goto done_errmsg; 1015280632Sian } 1016280632Sian } 1017280632Sian 1018191673Sjamie /* 1019191673Sjamie * Grab the allprison lock before letting modules check their 1020191673Sjamie * parameters. Once we have it, do not let go so we'll have a 1021191673Sjamie * consistent view of the OSD list. 1022191673Sjamie */ 1023191673Sjamie sx_xlock(&allprison_lock); 1024191673Sjamie error = osd_jail_call(NULL, PR_METHOD_CHECK, opts); 1025191673Sjamie if (error) 1026191673Sjamie goto done_unlock_list; 1027185435Sbz 1028191673Sjamie /* By now, all parameters should have been noted. */ 1029191673Sjamie TAILQ_FOREACH(opt, opts, link) { 1030191673Sjamie if (!opt->seen && strcmp(opt->name, "errmsg")) { 1031191673Sjamie error = EINVAL; 1032191673Sjamie vfs_opterror(opts, "unknown parameter: %s", opt->name); 1033191673Sjamie goto done_unlock_list; 1034191673Sjamie } 1035191673Sjamie } 1036191673Sjamie 1037185435Sbz /* 1038191673Sjamie * See if we are creating a new record or updating an existing one. 1039191673Sjamie * This abuses the file error codes ENOENT and EEXIST. 1040185435Sbz */ 1041191673Sjamie cuflags = flags & (JAIL_CREATE | JAIL_UPDATE); 1042191673Sjamie if (!cuflags) { 1043191673Sjamie error = EINVAL; 1044191673Sjamie vfs_opterror(opts, "no valid operation (create or update)"); 1045191673Sjamie goto done_unlock_list; 1046191673Sjamie } 1047191673Sjamie pr = NULL; 1048196835Sjamie namelc = NULL; 1049196835Sjamie if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) { 1050196835Sjamie namelc = strrchr(name, '.'); 1051196835Sjamie jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10); 1052196835Sjamie if (*p != '\0') 1053196835Sjamie jid = 0; 1054196835Sjamie } 1055191673Sjamie if (jid != 0) { 1056192895Sjamie /* 1057192895Sjamie * See if a requested jid already exists. There is an 1058192895Sjamie * information leak here if the jid exists but is not within 1059192895Sjamie * the caller's jail hierarchy. Jail creators will get EEXIST 1060192895Sjamie * even though they cannot see the jail, and CREATE | UPDATE 1061192895Sjamie * will return ENOENT which is not normally a valid error. 1062192895Sjamie */ 1063191673Sjamie if (jid < 0) { 1064191673Sjamie error = EINVAL; 1065191673Sjamie vfs_opterror(opts, "negative jid"); 1066191673Sjamie goto done_unlock_list; 1067191673Sjamie } 1068191673Sjamie pr = prison_find(jid); 1069191673Sjamie if (pr != NULL) { 1070192895Sjamie ppr = pr->pr_parent; 1071191673Sjamie /* Create: jid must not exist. */ 1072191673Sjamie if (cuflags == JAIL_CREATE) { 1073191673Sjamie mtx_unlock(&pr->pr_mtx); 1074191673Sjamie error = EEXIST; 1075191673Sjamie vfs_opterror(opts, "jail %d already exists", 1076191673Sjamie jid); 1077191673Sjamie goto done_unlock_list; 1078191673Sjamie } 1079192895Sjamie if (!prison_ischild(mypr, pr)) { 1080192895Sjamie mtx_unlock(&pr->pr_mtx); 1081192895Sjamie pr = NULL; 1082192895Sjamie } else if (pr->pr_uref == 0) { 1083191673Sjamie if (!(flags & JAIL_DYING)) { 1084191673Sjamie mtx_unlock(&pr->pr_mtx); 1085191673Sjamie error = ENOENT; 1086191673Sjamie vfs_opterror(opts, "jail %d is dying", 1087191673Sjamie jid); 1088191673Sjamie goto done_unlock_list; 1089191673Sjamie } else if ((flags & JAIL_ATTACH) || 1090191673Sjamie (pr_flags & PR_PERSIST)) { 1091191673Sjamie /* 1092191673Sjamie * A dying jail might be resurrected 1093191673Sjamie * (via attach or persist), but first 1094191673Sjamie * it must determine if another jail 1095191673Sjamie * has claimed its name. Accomplish 1096191673Sjamie * this by implicitly re-setting the 1097191673Sjamie * name. 1098191673Sjamie */ 1099191673Sjamie if (name == NULL) 1100192895Sjamie name = prison_name(mypr, pr); 1101191673Sjamie } 1102191673Sjamie } 1103191673Sjamie } 1104191673Sjamie if (pr == NULL) { 1105191673Sjamie /* Update: jid must exist. */ 1106191673Sjamie if (cuflags == JAIL_UPDATE) { 1107191673Sjamie error = ENOENT; 1108191673Sjamie vfs_opterror(opts, "jail %d not found", jid); 1109191673Sjamie goto done_unlock_list; 1110191673Sjamie } 1111191673Sjamie } 1112191673Sjamie } 1113191673Sjamie /* 1114191673Sjamie * If the caller provided a name, look for a jail by that name. 1115191673Sjamie * This has different semantics for creates and updates keyed by jid 1116191673Sjamie * (where the name must not already exist in a different jail), 1117191673Sjamie * and updates keyed by the name itself (where the name must exist 1118191673Sjamie * because that is the jail being updated). 1119191673Sjamie */ 1120191673Sjamie if (name != NULL) { 1121196835Sjamie namelc = strrchr(name, '.'); 1122196835Sjamie if (namelc == NULL) 1123196835Sjamie namelc = name; 1124196835Sjamie else { 1125192895Sjamie /* 1126192895Sjamie * This is a hierarchical name. Split it into the 1127192895Sjamie * parent and child names, and make sure the parent 1128192895Sjamie * exists or matches an already found jail. 1129192895Sjamie */ 1130196835Sjamie *namelc = '\0'; 1131192895Sjamie if (pr != NULL) { 1132196835Sjamie if (strncmp(name, ppr->pr_name, namelc - name) 1133196835Sjamie || ppr->pr_name[namelc - name] != '\0') { 1134192895Sjamie mtx_unlock(&pr->pr_mtx); 1135192895Sjamie error = EINVAL; 1136192895Sjamie vfs_opterror(opts, 1137192895Sjamie "cannot change jail's parent"); 1138192895Sjamie goto done_unlock_list; 1139192895Sjamie } 1140192895Sjamie } else { 1141192895Sjamie ppr = prison_find_name(mypr, name); 1142192895Sjamie if (ppr == NULL) { 1143192895Sjamie error = ENOENT; 1144192895Sjamie vfs_opterror(opts, 1145192895Sjamie "jail \"%s\" not found", name); 1146192895Sjamie goto done_unlock_list; 1147192895Sjamie } 1148192895Sjamie mtx_unlock(&ppr->pr_mtx); 1149192895Sjamie } 1150196835Sjamie name = ++namelc; 1151192895Sjamie } 1152191673Sjamie if (name[0] != '\0') { 1153192895Sjamie namelen = 1154192895Sjamie (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 1155192895Sjamie name_again: 1156191673Sjamie deadpr = NULL; 1157192895Sjamie FOREACH_PRISON_CHILD(ppr, tpr) { 1158191673Sjamie if (tpr != pr && tpr->pr_ref > 0 && 1159192895Sjamie !strcmp(tpr->pr_name + namelen, name)) { 1160191673Sjamie if (pr == NULL && 1161191673Sjamie cuflags != JAIL_CREATE) { 1162191673Sjamie mtx_lock(&tpr->pr_mtx); 1163191673Sjamie if (tpr->pr_ref > 0) { 1164191673Sjamie /* 1165191673Sjamie * Use this jail 1166191673Sjamie * for updates. 1167191673Sjamie */ 1168191673Sjamie if (tpr->pr_uref > 0) { 1169191673Sjamie pr = tpr; 1170191673Sjamie break; 1171191673Sjamie } 1172191673Sjamie deadpr = tpr; 1173191673Sjamie } 1174191673Sjamie mtx_unlock(&tpr->pr_mtx); 1175191673Sjamie } else if (tpr->pr_uref > 0) { 1176191673Sjamie /* 1177191673Sjamie * Create, or update(jid): 1178191673Sjamie * name must not exist in an 1179192895Sjamie * active sibling jail. 1180191673Sjamie */ 1181191673Sjamie error = EEXIST; 1182191673Sjamie if (pr != NULL) 1183191673Sjamie mtx_unlock(&pr->pr_mtx); 1184191673Sjamie vfs_opterror(opts, 1185191673Sjamie "jail \"%s\" already exists", 1186191673Sjamie name); 1187191673Sjamie goto done_unlock_list; 1188191673Sjamie } 1189191673Sjamie } 1190191673Sjamie } 1191191673Sjamie /* If no active jail is found, use a dying one. */ 1192191673Sjamie if (deadpr != NULL && pr == NULL) { 1193191673Sjamie if (flags & JAIL_DYING) { 1194191673Sjamie mtx_lock(&deadpr->pr_mtx); 1195191673Sjamie if (deadpr->pr_ref == 0) { 1196191673Sjamie mtx_unlock(&deadpr->pr_mtx); 1197191673Sjamie goto name_again; 1198191673Sjamie } 1199191673Sjamie pr = deadpr; 1200191673Sjamie } else if (cuflags == JAIL_UPDATE) { 1201191673Sjamie error = ENOENT; 1202191673Sjamie vfs_opterror(opts, 1203191673Sjamie "jail \"%s\" is dying", name); 1204191673Sjamie goto done_unlock_list; 1205191673Sjamie } 1206191673Sjamie } 1207191673Sjamie /* Update: name must exist if no jid. */ 1208191673Sjamie else if (cuflags == JAIL_UPDATE && pr == NULL) { 1209191673Sjamie error = ENOENT; 1210191673Sjamie vfs_opterror(opts, "jail \"%s\" not found", 1211191673Sjamie name); 1212191673Sjamie goto done_unlock_list; 1213191673Sjamie } 1214191673Sjamie } 1215191673Sjamie } 1216191673Sjamie /* Update: must provide a jid or name. */ 1217191673Sjamie else if (cuflags == JAIL_UPDATE && pr == NULL) { 1218191673Sjamie error = ENOENT; 1219191673Sjamie vfs_opterror(opts, "update specified no jail"); 1220191673Sjamie goto done_unlock_list; 1221191673Sjamie } 1222185435Sbz 1223191673Sjamie /* If there's no prison to update, create a new one and link it in. */ 1224191673Sjamie if (pr == NULL) { 1225194762Sjamie for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent) 1226194762Sjamie if (tpr->pr_childcount >= tpr->pr_childmax) { 1227194762Sjamie error = EPERM; 1228194762Sjamie vfs_opterror(opts, "prison limit exceeded"); 1229194762Sjamie goto done_unlock_list; 1230194762Sjamie } 1231191673Sjamie created = 1; 1232192895Sjamie mtx_lock(&ppr->pr_mtx); 1233192895Sjamie if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) { 1234192895Sjamie mtx_unlock(&ppr->pr_mtx); 1235192895Sjamie error = ENOENT; 1236192895Sjamie vfs_opterror(opts, "parent jail went away!"); 1237192895Sjamie goto done_unlock_list; 1238192895Sjamie } 1239192895Sjamie ppr->pr_ref++; 1240192895Sjamie ppr->pr_uref++; 1241192895Sjamie mtx_unlock(&ppr->pr_mtx); 1242191673Sjamie pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO); 1243191673Sjamie if (jid == 0) { 1244191673Sjamie /* Find the next free jid. */ 1245191673Sjamie jid = lastprid + 1; 1246191673Sjamie findnext: 1247191673Sjamie if (jid == JAIL_MAX) 1248191673Sjamie jid = 1; 1249191673Sjamie TAILQ_FOREACH(tpr, &allprison, pr_list) { 1250191673Sjamie if (tpr->pr_id < jid) 1251191673Sjamie continue; 1252191673Sjamie if (tpr->pr_id > jid || tpr->pr_ref == 0) { 1253191673Sjamie TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1254191673Sjamie break; 1255191673Sjamie } 1256191673Sjamie if (jid == lastprid) { 1257191673Sjamie error = EAGAIN; 1258191673Sjamie vfs_opterror(opts, 1259191673Sjamie "no available jail IDs"); 1260191673Sjamie free(pr, M_PRISON); 1261192895Sjamie prison_deref(ppr, PD_DEREF | 1262192895Sjamie PD_DEUREF | PD_LIST_XLOCKED); 1263192895Sjamie goto done_releroot; 1264191673Sjamie } 1265191673Sjamie jid++; 1266191673Sjamie goto findnext; 1267191673Sjamie } 1268191673Sjamie lastprid = jid; 1269191673Sjamie } else { 1270191673Sjamie /* 1271191673Sjamie * The jail already has a jid (that did not yet exist), 1272191673Sjamie * so just find where to insert it. 1273191673Sjamie */ 1274191673Sjamie TAILQ_FOREACH(tpr, &allprison, pr_list) 1275191673Sjamie if (tpr->pr_id >= jid) { 1276191673Sjamie TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1277191673Sjamie break; 1278191673Sjamie } 1279191673Sjamie } 1280191673Sjamie if (tpr == NULL) 1281191673Sjamie TAILQ_INSERT_TAIL(&allprison, pr, pr_list); 1282192895Sjamie LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling); 1283192895Sjamie for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 1284194762Sjamie tpr->pr_childcount++; 1285185435Sbz 1286192895Sjamie pr->pr_parent = ppr; 1287191673Sjamie pr->pr_id = jid; 1288192895Sjamie 1289192895Sjamie /* Set some default values, and inherit some from the parent. */ 1290191673Sjamie if (name == NULL) 1291191673Sjamie name = ""; 1292191673Sjamie if (path == NULL) { 1293191673Sjamie path = "/"; 1294192895Sjamie root = mypr->pr_root; 1295191673Sjamie vref(root); 1296191673Sjamie } 1297195944Sjamie strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN); 1298195944Sjamie pr->pr_flags |= PR_HOST; 1299195945Sjamie#if defined(INET) || defined(INET6) 1300195945Sjamie#ifdef VIMAGE 1301195945Sjamie if (!(pr_flags & PR_VNET)) 1302195945Sjamie#endif 1303195945Sjamie { 1304192895Sjamie#ifdef INET 1305195974Sjamie if (!(ch_flags & PR_IP4_USER)) 1306195974Sjamie pr->pr_flags |= 1307195974Sjamie PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE; 1308195974Sjamie else if (!(pr_flags & PR_IP4_USER)) { 1309195974Sjamie pr->pr_flags |= ppr->pr_flags & PR_IP4; 1310195974Sjamie if (ppr->pr_ip4 != NULL) { 1311195974Sjamie pr->pr_ip4s = ppr->pr_ip4s; 1312195974Sjamie pr->pr_ip4 = malloc(pr->pr_ip4s * 1313195974Sjamie sizeof(struct in_addr), M_PRISON, 1314195974Sjamie M_WAITOK); 1315195974Sjamie bcopy(ppr->pr_ip4, pr->pr_ip4, 1316195974Sjamie pr->pr_ip4s * sizeof(*pr->pr_ip4)); 1317195974Sjamie } 1318195974Sjamie } 1319192895Sjamie#endif 1320192895Sjamie#ifdef INET6 1321195974Sjamie if (!(ch_flags & PR_IP6_USER)) 1322195974Sjamie pr->pr_flags |= 1323195974Sjamie PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE; 1324195974Sjamie else if (!(pr_flags & PR_IP6_USER)) { 1325195974Sjamie pr->pr_flags |= ppr->pr_flags & PR_IP6; 1326195974Sjamie if (ppr->pr_ip6 != NULL) { 1327195974Sjamie pr->pr_ip6s = ppr->pr_ip6s; 1328195974Sjamie pr->pr_ip6 = malloc(pr->pr_ip6s * 1329195974Sjamie sizeof(struct in6_addr), M_PRISON, 1330195974Sjamie M_WAITOK); 1331195974Sjamie bcopy(ppr->pr_ip6, pr->pr_ip6, 1332195974Sjamie pr->pr_ip6s * sizeof(*pr->pr_ip6)); 1333195974Sjamie } 1334195974Sjamie } 1335192895Sjamie#endif 1336195945Sjamie } 1337195945Sjamie#endif 1338202468Sbz /* Source address selection is always on by default. */ 1339202468Sbz pr->pr_flags |= _PR_IP_SADDRSEL; 1340202468Sbz 1341192895Sjamie pr->pr_securelevel = ppr->pr_securelevel; 1342192895Sjamie pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow; 1343196002Sjamie pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 1344232059Smm pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum; 1345191673Sjamie 1346280632Sian pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate; 1347280632Sian if (osrelstr == NULL) 1348280632Sian strcpy(pr->pr_osrelease, ppr->pr_osrelease); 1349280632Sian else 1350280632Sian strcpy(pr->pr_osrelease, osrelstr); 1351280632Sian 1352192895Sjamie LIST_INIT(&pr->pr_children); 1353192895Sjamie mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK); 1354191673Sjamie 1355194251Sjamie#ifdef VIMAGE 1356194251Sjamie /* Allocate a new vnet if specified. */ 1357194251Sjamie pr->pr_vnet = (pr_flags & PR_VNET) 1358194251Sjamie ? vnet_alloc() : ppr->pr_vnet; 1359194251Sjamie#endif 1360185435Sbz /* 1361191673Sjamie * Allocate a dedicated cpuset for each jail. 1362191673Sjamie * Unlike other initial settings, this may return an erorr. 1363185435Sbz */ 1364192895Sjamie error = cpuset_create_root(ppr, &pr->pr_cpuset); 1365191673Sjamie if (error) { 1366191673Sjamie prison_deref(pr, PD_LIST_XLOCKED); 1367191673Sjamie goto done_releroot; 1368191673Sjamie } 1369185435Sbz 1370191673Sjamie mtx_lock(&pr->pr_mtx); 1371185435Sbz /* 1372191673Sjamie * New prisons do not yet have a reference, because we do not 1373191673Sjamie * want other to see the incomplete prison once the 1374191673Sjamie * allprison_lock is downgraded. 1375185435Sbz */ 1376191673Sjamie } else { 1377191673Sjamie created = 0; 1378195974Sjamie /* 1379195974Sjamie * Grab a reference for existing prisons, to ensure they 1380195974Sjamie * continue to exist for the duration of the call. 1381195974Sjamie */ 1382195974Sjamie pr->pr_ref++; 1383195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6)) 1384195945Sjamie if ((pr->pr_flags & PR_VNET) && 1385195945Sjamie (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 1386195945Sjamie error = EINVAL; 1387195945Sjamie vfs_opterror(opts, 1388195945Sjamie "vnet jails cannot have IP address restrictions"); 1389195945Sjamie goto done_deref_locked; 1390195945Sjamie } 1391195945Sjamie#endif 1392195974Sjamie#ifdef INET 1393195974Sjamie if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 1394195974Sjamie error = EINVAL; 1395195974Sjamie vfs_opterror(opts, 1396195974Sjamie "ip4 cannot be changed after creation"); 1397195974Sjamie goto done_deref_locked; 1398195974Sjamie } 1399195974Sjamie#endif 1400195974Sjamie#ifdef INET6 1401195974Sjamie if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 1402195974Sjamie error = EINVAL; 1403195974Sjamie vfs_opterror(opts, 1404195974Sjamie "ip6 cannot be changed after creation"); 1405195974Sjamie goto done_deref_locked; 1406195974Sjamie } 1407195974Sjamie#endif 1408191673Sjamie } 1409185435Sbz 1410191673Sjamie /* Do final error checking before setting anything. */ 1411192895Sjamie if (gotslevel) { 1412192895Sjamie if (slevel < ppr->pr_securelevel) { 1413192895Sjamie error = EPERM; 1414192895Sjamie goto done_deref_locked; 1415192895Sjamie } 1416192895Sjamie } 1417194762Sjamie if (gotchildmax) { 1418194762Sjamie if (childmax >= ppr->pr_childmax) { 1419194762Sjamie error = EPERM; 1420194762Sjamie goto done_deref_locked; 1421194762Sjamie } 1422194762Sjamie } 1423192895Sjamie if (gotenforce) { 1424192895Sjamie if (enforce < ppr->pr_enforce_statfs) { 1425192895Sjamie error = EPERM; 1426192895Sjamie goto done_deref_locked; 1427192895Sjamie } 1428192895Sjamie } 1429231267Smm if (gotrsnum) { 1430231267Smm /* 1431231267Smm * devfs_rsnum is a uint16_t 1432231267Smm */ 1433232059Smm if (rsnum < 0 || rsnum > 65535) { 1434231267Smm error = EINVAL; 1435231267Smm goto done_deref_locked; 1436231267Smm } 1437231267Smm /* 1438232059Smm * Nested jails always inherit parent's devfs ruleset 1439231267Smm */ 1440231267Smm if (jailed(td->td_ucred)) { 1441231267Smm if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) { 1442231267Smm error = EPERM; 1443231267Smm goto done_deref_locked; 1444232059Smm } else 1445231267Smm rsnum = ppr->pr_devfs_rsnum; 1446231267Smm } 1447231267Smm } 1448185435Sbz#ifdef INET 1449195974Sjamie if (ip4s > 0) { 1450192895Sjamie if (ppr->pr_flags & PR_IP4) { 1451195974Sjamie /* 1452195974Sjamie * Make sure the new set of IP addresses is a 1453195974Sjamie * subset of the parent's list. Don't worry 1454195974Sjamie * about the parent being unlocked, as any 1455195974Sjamie * setting is done with allprison_lock held. 1456195974Sjamie */ 1457195974Sjamie for (ij = 0; ij < ppr->pr_ip4s; ij++) 1458195974Sjamie if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 1459195974Sjamie break; 1460195974Sjamie if (ij == ppr->pr_ip4s) { 1461195974Sjamie error = EPERM; 1462195974Sjamie goto done_deref_locked; 1463195974Sjamie } 1464195974Sjamie if (ip4s > 1) { 1465195974Sjamie for (ii = ij = 1; ii < ip4s; ii++) { 1466195974Sjamie if (ip4[ii].s_addr == 1467195974Sjamie ppr->pr_ip4[0].s_addr) 1468195974Sjamie continue; 1469195974Sjamie for (; ij < ppr->pr_ip4s; ij++) 1470195974Sjamie if (ip4[ii].s_addr == 1471195974Sjamie ppr->pr_ip4[ij].s_addr) 1472195974Sjamie break; 1473195974Sjamie if (ij == ppr->pr_ip4s) 1474195974Sjamie break; 1475192895Sjamie } 1476192895Sjamie if (ij == ppr->pr_ip4s) { 1477192895Sjamie error = EPERM; 1478192895Sjamie goto done_deref_locked; 1479192895Sjamie } 1480192895Sjamie } 1481192895Sjamie } 1482195974Sjamie /* 1483195974Sjamie * Check for conflicting IP addresses. We permit them 1484195974Sjamie * if there is no more than one IP on each jail. If 1485195974Sjamie * there is a duplicate on a jail with more than one 1486195974Sjamie * IP stop checking and return error. 1487195974Sjamie */ 1488195974Sjamie tppr = ppr; 1489195945Sjamie#ifdef VIMAGE 1490195974Sjamie for (; tppr != &prison0; tppr = tppr->pr_parent) 1491195974Sjamie if (tppr->pr_flags & PR_VNET) 1492195974Sjamie break; 1493195945Sjamie#endif 1494195974Sjamie FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1495195974Sjamie if (tpr == pr || 1496195945Sjamie#ifdef VIMAGE 1497195974Sjamie (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1498195945Sjamie#endif 1499195974Sjamie tpr->pr_uref == 0) { 1500192895Sjamie descend = 0; 1501195974Sjamie continue; 1502195974Sjamie } 1503195974Sjamie if (!(tpr->pr_flags & PR_IP4_USER)) 1504195974Sjamie continue; 1505195974Sjamie descend = 0; 1506195974Sjamie if (tpr->pr_ip4 == NULL || 1507195974Sjamie (ip4s == 1 && tpr->pr_ip4s == 1)) 1508195974Sjamie continue; 1509195974Sjamie for (ii = 0; ii < ip4s; ii++) { 1510195974Sjamie if (_prison_check_ip4(tpr, &ip4[ii]) == 0) { 1511195974Sjamie error = EADDRINUSE; 1512195974Sjamie vfs_opterror(opts, 1513195974Sjamie "IPv4 addresses clash"); 1514195974Sjamie goto done_deref_locked; 1515192895Sjamie } 1516192895Sjamie } 1517192895Sjamie } 1518192895Sjamie } 1519185435Sbz#endif 1520191673Sjamie#ifdef INET6 1521195974Sjamie if (ip6s > 0) { 1522192895Sjamie if (ppr->pr_flags & PR_IP6) { 1523195974Sjamie /* 1524195974Sjamie * Make sure the new set of IP addresses is a 1525195974Sjamie * subset of the parent's list. 1526195974Sjamie */ 1527195974Sjamie for (ij = 0; ij < ppr->pr_ip6s; ij++) 1528195974Sjamie if (IN6_ARE_ADDR_EQUAL(&ip6[0], 1529195974Sjamie &ppr->pr_ip6[ij])) 1530195974Sjamie break; 1531195974Sjamie if (ij == ppr->pr_ip6s) { 1532195974Sjamie error = EPERM; 1533195974Sjamie goto done_deref_locked; 1534195974Sjamie } 1535195974Sjamie if (ip6s > 1) { 1536195974Sjamie for (ii = ij = 1; ii < ip6s; ii++) { 1537195974Sjamie if (IN6_ARE_ADDR_EQUAL(&ip6[ii], 1538195974Sjamie &ppr->pr_ip6[0])) 1539195974Sjamie continue; 1540195974Sjamie for (; ij < ppr->pr_ip6s; ij++) 1541195974Sjamie if (IN6_ARE_ADDR_EQUAL( 1542195974Sjamie &ip6[ii], &ppr->pr_ip6[ij])) 1543195974Sjamie break; 1544195974Sjamie if (ij == ppr->pr_ip6s) 1545195974Sjamie break; 1546192895Sjamie } 1547192895Sjamie if (ij == ppr->pr_ip6s) { 1548192895Sjamie error = EPERM; 1549192895Sjamie goto done_deref_locked; 1550192895Sjamie } 1551192895Sjamie } 1552192895Sjamie } 1553195974Sjamie /* Check for conflicting IP addresses. */ 1554195974Sjamie tppr = ppr; 1555195945Sjamie#ifdef VIMAGE 1556195974Sjamie for (; tppr != &prison0; tppr = tppr->pr_parent) 1557195974Sjamie if (tppr->pr_flags & PR_VNET) 1558195974Sjamie break; 1559195945Sjamie#endif 1560195974Sjamie FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1561195974Sjamie if (tpr == pr || 1562195945Sjamie#ifdef VIMAGE 1563195974Sjamie (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1564195945Sjamie#endif 1565195974Sjamie tpr->pr_uref == 0) { 1566192895Sjamie descend = 0; 1567195974Sjamie continue; 1568195974Sjamie } 1569195974Sjamie if (!(tpr->pr_flags & PR_IP6_USER)) 1570195974Sjamie continue; 1571195974Sjamie descend = 0; 1572195974Sjamie if (tpr->pr_ip6 == NULL || 1573195974Sjamie (ip6s == 1 && tpr->pr_ip6s == 1)) 1574195974Sjamie continue; 1575195974Sjamie for (ii = 0; ii < ip6s; ii++) { 1576195974Sjamie if (_prison_check_ip6(tpr, &ip6[ii]) == 0) { 1577195974Sjamie error = EADDRINUSE; 1578195974Sjamie vfs_opterror(opts, 1579195974Sjamie "IPv6 addresses clash"); 1580195974Sjamie goto done_deref_locked; 1581192895Sjamie } 1582192895Sjamie } 1583191673Sjamie } 1584192895Sjamie } 1585191673Sjamie#endif 1586192895Sjamie onamelen = namelen = 0; 1587192895Sjamie if (name != NULL) { 1588191673Sjamie /* Give a default name of the jid. */ 1589191673Sjamie if (name[0] == '\0') 1590191673Sjamie snprintf(name = numbuf, sizeof(numbuf), "%d", jid); 1591196835Sjamie else if (*namelc == '0' || (strtoul(namelc, &p, 10) != jid && 1592196835Sjamie *p == '\0')) { 1593191673Sjamie error = EINVAL; 1594196835Sjamie vfs_opterror(opts, 1595196835Sjamie "name cannot be numeric (unless it is the jid)"); 1596192895Sjamie goto done_deref_locked; 1597191673Sjamie } 1598191673Sjamie /* 1599192895Sjamie * Make sure the name isn't too long for the prison or its 1600192895Sjamie * children. 1601191673Sjamie */ 1602192895Sjamie onamelen = strlen(pr->pr_name); 1603192895Sjamie namelen = strlen(name); 1604192895Sjamie if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) { 1605192895Sjamie error = ENAMETOOLONG; 1606192895Sjamie goto done_deref_locked; 1607192895Sjamie } 1608192895Sjamie FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 1609192895Sjamie if (strlen(tpr->pr_name) + (namelen - onamelen) >= 1610192895Sjamie sizeof(pr->pr_name)) { 1611192895Sjamie error = ENAMETOOLONG; 1612192895Sjamie goto done_deref_locked; 1613192895Sjamie } 1614192895Sjamie } 1615191673Sjamie } 1616192895Sjamie if (pr_allow & ~ppr->pr_allow) { 1617192895Sjamie error = EPERM; 1618192895Sjamie goto done_deref_locked; 1619192895Sjamie } 1620185435Sbz 1621191673Sjamie /* Set the parameters of the prison. */ 1622191673Sjamie#ifdef INET 1623192895Sjamie redo_ip4 = 0; 1624195974Sjamie if (pr_flags & PR_IP4_USER) { 1625195974Sjamie pr->pr_flags |= PR_IP4; 1626195974Sjamie free(pr->pr_ip4, M_PRISON); 1627195974Sjamie pr->pr_ip4s = ip4s; 1628195974Sjamie pr->pr_ip4 = ip4; 1629195974Sjamie ip4 = NULL; 1630192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1631195945Sjamie#ifdef VIMAGE 1632195945Sjamie if (tpr->pr_flags & PR_VNET) { 1633195945Sjamie descend = 0; 1634195945Sjamie continue; 1635195945Sjamie } 1636195945Sjamie#endif 1637192895Sjamie if (prison_restrict_ip4(tpr, NULL)) { 1638192895Sjamie redo_ip4 = 1; 1639192895Sjamie descend = 0; 1640192895Sjamie } 1641192895Sjamie } 1642185435Sbz } 1643191673Sjamie#endif 1644191673Sjamie#ifdef INET6 1645192895Sjamie redo_ip6 = 0; 1646195974Sjamie if (pr_flags & PR_IP6_USER) { 1647195974Sjamie pr->pr_flags |= PR_IP6; 1648195974Sjamie free(pr->pr_ip6, M_PRISON); 1649195974Sjamie pr->pr_ip6s = ip6s; 1650195974Sjamie pr->pr_ip6 = ip6; 1651195974Sjamie ip6 = NULL; 1652192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1653195945Sjamie#ifdef VIMAGE 1654195945Sjamie if (tpr->pr_flags & PR_VNET) { 1655195945Sjamie descend = 0; 1656195945Sjamie continue; 1657195945Sjamie } 1658195945Sjamie#endif 1659192895Sjamie if (prison_restrict_ip6(tpr, NULL)) { 1660192895Sjamie redo_ip6 = 1; 1661192895Sjamie descend = 0; 1662192895Sjamie } 1663192895Sjamie } 1664191673Sjamie } 1665191673Sjamie#endif 1666192895Sjamie if (gotslevel) { 1667191673Sjamie pr->pr_securelevel = slevel; 1668192895Sjamie /* Set all child jails to be at least this level. */ 1669192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1670192895Sjamie if (tpr->pr_securelevel < slevel) 1671192895Sjamie tpr->pr_securelevel = slevel; 1672192895Sjamie } 1673194762Sjamie if (gotchildmax) { 1674194762Sjamie pr->pr_childmax = childmax; 1675194762Sjamie /* Set all child jails to under this limit. */ 1676194762Sjamie FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level) 1677194762Sjamie if (tpr->pr_childmax > childmax - level) 1678194762Sjamie tpr->pr_childmax = childmax > level 1679194762Sjamie ? childmax - level : 0; 1680194762Sjamie } 1681192895Sjamie if (gotenforce) { 1682192895Sjamie pr->pr_enforce_statfs = enforce; 1683192895Sjamie /* Pass this restriction on to the children. */ 1684192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1685192895Sjamie if (tpr->pr_enforce_statfs < enforce) 1686192895Sjamie tpr->pr_enforce_statfs = enforce; 1687192895Sjamie } 1688231267Smm if (gotrsnum) { 1689231267Smm pr->pr_devfs_rsnum = rsnum; 1690231267Smm /* Pass this restriction on to the children. */ 1691231267Smm FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1692232059Smm tpr->pr_devfs_rsnum = rsnum; 1693231267Smm } 1694192895Sjamie if (name != NULL) { 1695192895Sjamie if (ppr == &prison0) 1696192895Sjamie strlcpy(pr->pr_name, name, sizeof(pr->pr_name)); 1697192895Sjamie else 1698192895Sjamie snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s", 1699192895Sjamie ppr->pr_name, name); 1700192895Sjamie /* Change this component of child names. */ 1701192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1702192895Sjamie bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen, 1703192895Sjamie strlen(tpr->pr_name + onamelen) + 1); 1704192895Sjamie bcopy(pr->pr_name, tpr->pr_name, namelen); 1705192895Sjamie } 1706192895Sjamie } 1707191673Sjamie if (path != NULL) { 1708192895Sjamie /* Try to keep a real-rooted full pathname. */ 1709230129Smm if (fullpath_disabled && path[0] == '/' && 1710230129Smm strcmp(mypr->pr_path, "/")) 1711192895Sjamie snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s", 1712192895Sjamie mypr->pr_path, path); 1713192895Sjamie else 1714192895Sjamie strlcpy(pr->pr_path, path, sizeof(pr->pr_path)); 1715191673Sjamie pr->pr_root = root; 1716191673Sjamie } 1717193066Sjamie if (PR_HOST & ch_flags & ~pr_flags) { 1718193066Sjamie if (pr->pr_flags & PR_HOST) { 1719193066Sjamie /* 1720193066Sjamie * Copy the parent's host info. As with pr_ip4 above, 1721193066Sjamie * the lack of a lock on the parent is not a problem; 1722193066Sjamie * it is always set with allprison_lock at least 1723193066Sjamie * shared, and is held exclusively here. 1724193066Sjamie */ 1725194118Sjamie strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname, 1726194118Sjamie sizeof(pr->pr_hostname)); 1727194118Sjamie strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname, 1728194118Sjamie sizeof(pr->pr_domainname)); 1729194118Sjamie strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid, 1730194118Sjamie sizeof(pr->pr_hostuuid)); 1731193066Sjamie pr->pr_hostid = pr->pr_parent->pr_hostid; 1732193066Sjamie } 1733193066Sjamie } else if (host != NULL || domain != NULL || uuid != NULL || gothid) { 1734193066Sjamie /* Set this prison, and any descendants without PR_HOST. */ 1735193066Sjamie if (host != NULL) 1736194118Sjamie strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname)); 1737193066Sjamie if (domain != NULL) 1738194118Sjamie strlcpy(pr->pr_domainname, domain, 1739194118Sjamie sizeof(pr->pr_domainname)); 1740193066Sjamie if (uuid != NULL) 1741194118Sjamie strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid)); 1742193066Sjamie if (gothid) 1743193066Sjamie pr->pr_hostid = hid; 1744193066Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1745193066Sjamie if (tpr->pr_flags & PR_HOST) 1746193066Sjamie descend = 0; 1747193066Sjamie else { 1748193066Sjamie if (host != NULL) 1749194118Sjamie strlcpy(tpr->pr_hostname, 1750194118Sjamie pr->pr_hostname, 1751194118Sjamie sizeof(tpr->pr_hostname)); 1752193066Sjamie if (domain != NULL) 1753194118Sjamie strlcpy(tpr->pr_domainname, 1754194118Sjamie pr->pr_domainname, 1755194118Sjamie sizeof(tpr->pr_domainname)); 1756193066Sjamie if (uuid != NULL) 1757194118Sjamie strlcpy(tpr->pr_hostuuid, 1758194118Sjamie pr->pr_hostuuid, 1759194118Sjamie sizeof(tpr->pr_hostuuid)); 1760193066Sjamie if (gothid) 1761193066Sjamie tpr->pr_hostid = hid; 1762193066Sjamie } 1763193066Sjamie } 1764193066Sjamie } 1765192895Sjamie if ((tallow = ch_allow & ~pr_allow)) { 1766192895Sjamie /* Clear allow bits in all children. */ 1767192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1768192895Sjamie tpr->pr_allow &= ~tallow; 1769192895Sjamie } 1770192895Sjamie pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow; 1771191673Sjamie /* 1772191673Sjamie * Persistent prisons get an extra reference, and prisons losing their 1773191673Sjamie * persist flag lose that reference. Only do this for existing prisons 1774191673Sjamie * for now, so new ones will remain unseen until after the module 1775191673Sjamie * handlers have completed. 1776191673Sjamie */ 1777191673Sjamie if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) { 1778191673Sjamie if (pr_flags & PR_PERSIST) { 1779191673Sjamie pr->pr_ref++; 1780191673Sjamie pr->pr_uref++; 1781191673Sjamie } else { 1782191673Sjamie pr->pr_ref--; 1783191673Sjamie pr->pr_uref--; 1784191673Sjamie } 1785191673Sjamie } 1786191673Sjamie pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags; 1787191673Sjamie mtx_unlock(&pr->pr_mtx); 1788185435Sbz 1789221362Strasz#ifdef RACCT 1790221362Strasz if (created) 1791221362Strasz prison_racct_attach(pr); 1792221362Strasz#endif 1793221362Strasz 1794192895Sjamie /* Locks may have prevented a complete restriction of child IP 1795192895Sjamie * addresses. If so, allocate some more memory and try again. 1796192895Sjamie */ 1797192895Sjamie#ifdef INET 1798192895Sjamie while (redo_ip4) { 1799192895Sjamie ip4s = pr->pr_ip4s; 1800192895Sjamie ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 1801192895Sjamie mtx_lock(&pr->pr_mtx); 1802192895Sjamie redo_ip4 = 0; 1803192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1804195945Sjamie#ifdef VIMAGE 1805195945Sjamie if (tpr->pr_flags & PR_VNET) { 1806195945Sjamie descend = 0; 1807195945Sjamie continue; 1808195945Sjamie } 1809195945Sjamie#endif 1810192895Sjamie if (prison_restrict_ip4(tpr, ip4)) { 1811192895Sjamie if (ip4 != NULL) 1812192895Sjamie ip4 = NULL; 1813192895Sjamie else 1814192895Sjamie redo_ip4 = 1; 1815192895Sjamie } 1816192895Sjamie } 1817192895Sjamie mtx_unlock(&pr->pr_mtx); 1818192895Sjamie } 1819192895Sjamie#endif 1820192895Sjamie#ifdef INET6 1821192895Sjamie while (redo_ip6) { 1822192895Sjamie ip6s = pr->pr_ip6s; 1823192895Sjamie ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 1824192895Sjamie mtx_lock(&pr->pr_mtx); 1825192895Sjamie redo_ip6 = 0; 1826192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1827195945Sjamie#ifdef VIMAGE 1828195945Sjamie if (tpr->pr_flags & PR_VNET) { 1829195945Sjamie descend = 0; 1830195945Sjamie continue; 1831195945Sjamie } 1832195945Sjamie#endif 1833192895Sjamie if (prison_restrict_ip6(tpr, ip6)) { 1834192895Sjamie if (ip6 != NULL) 1835192895Sjamie ip6 = NULL; 1836192895Sjamie else 1837192895Sjamie redo_ip6 = 1; 1838192895Sjamie } 1839192895Sjamie } 1840192895Sjamie mtx_unlock(&pr->pr_mtx); 1841192895Sjamie } 1842192895Sjamie#endif 1843192895Sjamie 1844191673Sjamie /* Let the modules do their work. */ 1845191673Sjamie sx_downgrade(&allprison_lock); 1846191673Sjamie if (created) { 1847191673Sjamie error = osd_jail_call(pr, PR_METHOD_CREATE, opts); 1848191673Sjamie if (error) { 1849191673Sjamie prison_deref(pr, PD_LIST_SLOCKED); 1850191673Sjamie goto done_errmsg; 1851191673Sjamie } 1852191673Sjamie } 1853191673Sjamie error = osd_jail_call(pr, PR_METHOD_SET, opts); 1854191673Sjamie if (error) { 1855191673Sjamie prison_deref(pr, created 1856191673Sjamie ? PD_LIST_SLOCKED 1857191673Sjamie : PD_DEREF | PD_LIST_SLOCKED); 1858191673Sjamie goto done_errmsg; 1859191673Sjamie } 1860191673Sjamie 1861191673Sjamie /* Attach this process to the prison if requested. */ 1862191673Sjamie if (flags & JAIL_ATTACH) { 1863191673Sjamie mtx_lock(&pr->pr_mtx); 1864191673Sjamie error = do_jail_attach(td, pr); 1865191673Sjamie if (error) { 1866191673Sjamie vfs_opterror(opts, "attach failed"); 1867191673Sjamie if (!created) 1868191673Sjamie prison_deref(pr, PD_DEREF); 1869191673Sjamie goto done_errmsg; 1870191673Sjamie } 1871191673Sjamie } 1872191673Sjamie 1873235803Strasz#ifdef RACCT 1874235803Strasz if (!created) { 1875271622Strasz if (!(flags & JAIL_ATTACH)) 1876271622Strasz sx_sunlock(&allprison_lock); 1877235803Strasz prison_racct_modify(pr); 1878271622Strasz if (!(flags & JAIL_ATTACH)) 1879271622Strasz sx_slock(&allprison_lock); 1880235803Strasz } 1881235803Strasz#endif 1882235803Strasz 1883235803Strasz td->td_retval[0] = pr->pr_id; 1884235803Strasz 1885191673Sjamie /* 1886191673Sjamie * Now that it is all there, drop the temporary reference from existing 1887191673Sjamie * prisons. Or add a reference to newly created persistent prisons 1888191673Sjamie * (which was not done earlier so that the prison would not be publicly 1889191673Sjamie * visible). 1890191673Sjamie */ 1891191673Sjamie if (!created) { 1892191673Sjamie prison_deref(pr, (flags & JAIL_ATTACH) 1893191673Sjamie ? PD_DEREF 1894191673Sjamie : PD_DEREF | PD_LIST_SLOCKED); 1895191673Sjamie } else { 1896191673Sjamie if (pr_flags & PR_PERSIST) { 1897191673Sjamie mtx_lock(&pr->pr_mtx); 1898191673Sjamie pr->pr_ref++; 1899191673Sjamie pr->pr_uref++; 1900191673Sjamie mtx_unlock(&pr->pr_mtx); 1901191673Sjamie } 1902191673Sjamie if (!(flags & JAIL_ATTACH)) 1903191673Sjamie sx_sunlock(&allprison_lock); 1904191673Sjamie } 1905232598Strasz 1906191673Sjamie goto done_errmsg; 1907191673Sjamie 1908192895Sjamie done_deref_locked: 1909192895Sjamie prison_deref(pr, created 1910192895Sjamie ? PD_LOCKED | PD_LIST_XLOCKED 1911192895Sjamie : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 1912192895Sjamie goto done_releroot; 1913191673Sjamie done_unlock_list: 1914191673Sjamie sx_xunlock(&allprison_lock); 1915191673Sjamie done_releroot: 1916241896Skib if (root != NULL) 1917191673Sjamie vrele(root); 1918191673Sjamie done_errmsg: 1919191673Sjamie if (error) { 1920191673Sjamie vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 1921191673Sjamie if (errmsg_len > 0) { 1922191673Sjamie errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1; 1923191673Sjamie if (errmsg_pos > 0) { 1924191673Sjamie if (optuio->uio_segflg == UIO_SYSSPACE) 1925191673Sjamie bcopy(errmsg, 1926191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 1927191673Sjamie errmsg_len); 1928191673Sjamie else 1929191673Sjamie copyout(errmsg, 1930191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 1931191673Sjamie errmsg_len); 1932191673Sjamie } 1933191673Sjamie } 1934191673Sjamie } 1935191673Sjamie done_free: 1936191673Sjamie#ifdef INET 1937191673Sjamie free(ip4, M_PRISON); 1938191673Sjamie#endif 1939191673Sjamie#ifdef INET6 1940191673Sjamie free(ip6, M_PRISON); 1941191673Sjamie#endif 1942230407Smm if (g_path != NULL) 1943230407Smm free(g_path, M_TEMP); 1944191673Sjamie vfs_freeopts(opts); 1945191673Sjamie return (error); 1946191673Sjamie} 1947191673Sjamie 1948191673Sjamie 194982710Sdillon/* 1950191673Sjamie * struct jail_get_args { 1951191673Sjamie * struct iovec *iovp; 1952191673Sjamie * unsigned int iovcnt; 1953191673Sjamie * int flags; 1954114168Smike * }; 195582710Sdillon */ 195646155Sphkint 1957225617Skmacysys_jail_get(struct thread *td, struct jail_get_args *uap) 195846155Sphk{ 1959191673Sjamie struct uio *auio; 1960185435Sbz int error; 1961185435Sbz 1962191673Sjamie /* Check that we have an even number of iovecs. */ 1963191673Sjamie if (uap->iovcnt & 1) 1964191673Sjamie return (EINVAL); 1965191673Sjamie 1966191673Sjamie error = copyinuio(uap->iovp, uap->iovcnt, &auio); 1967185435Sbz if (error) 1968185435Sbz return (error); 1969191673Sjamie error = kern_jail_get(td, auio, uap->flags); 1970191673Sjamie if (error == 0) 1971191673Sjamie error = copyout(auio->uio_iov, uap->iovp, 1972191673Sjamie uap->iovcnt * sizeof (struct iovec)); 1973191673Sjamie free(auio, M_IOV); 1974191673Sjamie return (error); 1975191673Sjamie} 1976185435Sbz 1977191673Sjamieint 1978191673Sjamiekern_jail_get(struct thread *td, struct uio *optuio, int flags) 1979191673Sjamie{ 1980192895Sjamie struct prison *pr, *mypr; 1981191673Sjamie struct vfsopt *opt; 1982191673Sjamie struct vfsoptlist *opts; 1983191673Sjamie char *errmsg, *name; 1984192895Sjamie int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos; 1985185435Sbz 1986191673Sjamie if (flags & ~JAIL_GET_MASK) 1987191673Sjamie return (EINVAL); 1988185435Sbz 1989191673Sjamie /* Get the parameter list. */ 1990191673Sjamie error = vfs_buildopts(optuio, &opts); 1991191673Sjamie if (error) 1992191673Sjamie return (error); 1993191673Sjamie errmsg_pos = vfs_getopt_pos(opts, "errmsg"); 1994192895Sjamie mypr = td->td_ucred->cr_prison; 1995185435Sbz 1996191673Sjamie /* 1997191673Sjamie * Find the prison specified by one of: lastjid, jid, name. 1998191673Sjamie */ 1999191673Sjamie sx_slock(&allprison_lock); 2000191673Sjamie error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid)); 2001191673Sjamie if (error == 0) { 2002191673Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) { 2003192895Sjamie if (pr->pr_id > jid && prison_ischild(mypr, pr)) { 2004191673Sjamie mtx_lock(&pr->pr_mtx); 2005191673Sjamie if (pr->pr_ref > 0 && 2006191673Sjamie (pr->pr_uref > 0 || (flags & JAIL_DYING))) 2007191673Sjamie break; 2008191673Sjamie mtx_unlock(&pr->pr_mtx); 2009191673Sjamie } 2010191673Sjamie } 2011191673Sjamie if (pr != NULL) 2012191673Sjamie goto found_prison; 2013191673Sjamie error = ENOENT; 2014191673Sjamie vfs_opterror(opts, "no jail after %d", jid); 2015191673Sjamie goto done_unlock_list; 2016191673Sjamie } else if (error != ENOENT) 2017191673Sjamie goto done_unlock_list; 2018185435Sbz 2019191673Sjamie error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 2020191673Sjamie if (error == 0) { 2021191673Sjamie if (jid != 0) { 2022192895Sjamie pr = prison_find_child(mypr, jid); 2023191673Sjamie if (pr != NULL) { 2024191673Sjamie if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 2025191673Sjamie mtx_unlock(&pr->pr_mtx); 2026191673Sjamie error = ENOENT; 2027191673Sjamie vfs_opterror(opts, "jail %d is dying", 2028191673Sjamie jid); 2029191673Sjamie goto done_unlock_list; 2030191673Sjamie } 2031191673Sjamie goto found_prison; 2032191673Sjamie } 2033191673Sjamie error = ENOENT; 2034191673Sjamie vfs_opterror(opts, "jail %d not found", jid); 2035191673Sjamie goto done_unlock_list; 2036191673Sjamie } 2037191673Sjamie } else if (error != ENOENT) 2038191673Sjamie goto done_unlock_list; 203946155Sphk 2040191673Sjamie error = vfs_getopt(opts, "name", (void **)&name, &len); 2041191673Sjamie if (error == 0) { 2042191673Sjamie if (len == 0 || name[len - 1] != '\0') { 2043191673Sjamie error = EINVAL; 2044191673Sjamie goto done_unlock_list; 2045191673Sjamie } 2046192895Sjamie pr = prison_find_name(mypr, name); 2047191673Sjamie if (pr != NULL) { 2048191673Sjamie if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 2049191673Sjamie mtx_unlock(&pr->pr_mtx); 2050191673Sjamie error = ENOENT; 2051191673Sjamie vfs_opterror(opts, "jail \"%s\" is dying", 2052191673Sjamie name); 2053191673Sjamie goto done_unlock_list; 2054191673Sjamie } 2055191673Sjamie goto found_prison; 2056191673Sjamie } 2057191673Sjamie error = ENOENT; 2058191673Sjamie vfs_opterror(opts, "jail \"%s\" not found", name); 2059191673Sjamie goto done_unlock_list; 2060191673Sjamie } else if (error != ENOENT) 2061191673Sjamie goto done_unlock_list; 2062185435Sbz 2063191673Sjamie vfs_opterror(opts, "no jail specified"); 2064191673Sjamie error = ENOENT; 2065191673Sjamie goto done_unlock_list; 2066191673Sjamie 2067191673Sjamie found_prison: 2068191673Sjamie /* Get the parameters of the prison. */ 2069191673Sjamie pr->pr_ref++; 2070191673Sjamie locked = PD_LOCKED; 2071191673Sjamie td->td_retval[0] = pr->pr_id; 2072191673Sjamie error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id)); 2073191673Sjamie if (error != 0 && error != ENOENT) 2074191673Sjamie goto done_deref; 2075192895Sjamie i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id; 2076192895Sjamie error = vfs_setopt(opts, "parent", &i, sizeof(i)); 2077191673Sjamie if (error != 0 && error != ENOENT) 2078191673Sjamie goto done_deref; 2079192895Sjamie error = vfs_setopts(opts, "name", prison_name(mypr, pr)); 2080192895Sjamie if (error != 0 && error != ENOENT) 2081192895Sjamie goto done_deref; 2082192895Sjamie error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id, 2083191673Sjamie sizeof(pr->pr_cpuset->cs_id)); 2084191673Sjamie if (error != 0 && error != ENOENT) 2085191673Sjamie goto done_deref; 2086192895Sjamie error = vfs_setopts(opts, "path", prison_path(mypr, pr)); 2087191673Sjamie if (error != 0 && error != ENOENT) 2088191673Sjamie goto done_deref; 2089191673Sjamie#ifdef INET 2090191673Sjamie error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4, 2091191673Sjamie pr->pr_ip4s * sizeof(*pr->pr_ip4)); 2092191673Sjamie if (error != 0 && error != ENOENT) 2093191673Sjamie goto done_deref; 2094191673Sjamie#endif 2095191673Sjamie#ifdef INET6 2096191673Sjamie error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6, 2097191673Sjamie pr->pr_ip6s * sizeof(*pr->pr_ip6)); 2098191673Sjamie if (error != 0 && error != ENOENT) 2099191673Sjamie goto done_deref; 2100191673Sjamie#endif 2101191673Sjamie error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel, 2102191673Sjamie sizeof(pr->pr_securelevel)); 2103191673Sjamie if (error != 0 && error != ENOENT) 2104191673Sjamie goto done_deref; 2105194762Sjamie error = vfs_setopt(opts, "children.cur", &pr->pr_childcount, 2106194762Sjamie sizeof(pr->pr_childcount)); 2107194762Sjamie if (error != 0 && error != ENOENT) 2108194762Sjamie goto done_deref; 2109194762Sjamie error = vfs_setopt(opts, "children.max", &pr->pr_childmax, 2110194762Sjamie sizeof(pr->pr_childmax)); 2111194762Sjamie if (error != 0 && error != ENOENT) 2112194762Sjamie goto done_deref; 2113194118Sjamie error = vfs_setopts(opts, "host.hostname", pr->pr_hostname); 2114191673Sjamie if (error != 0 && error != ENOENT) 2115191673Sjamie goto done_deref; 2116194118Sjamie error = vfs_setopts(opts, "host.domainname", pr->pr_domainname); 2117193066Sjamie if (error != 0 && error != ENOENT) 2118193066Sjamie goto done_deref; 2119194118Sjamie error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid); 2120193066Sjamie if (error != 0 && error != ENOENT) 2121193066Sjamie goto done_deref; 2122205014Snwhitehorn#ifdef COMPAT_FREEBSD32 2123217896Sdchagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 2124193066Sjamie uint32_t hid32 = pr->pr_hostid; 2125193066Sjamie 2126193066Sjamie error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32)); 2127193066Sjamie } else 2128193066Sjamie#endif 2129193066Sjamie error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid, 2130193066Sjamie sizeof(pr->pr_hostid)); 2131193066Sjamie if (error != 0 && error != ENOENT) 2132193066Sjamie goto done_deref; 2133192895Sjamie error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs, 2134192895Sjamie sizeof(pr->pr_enforce_statfs)); 2135191673Sjamie if (error != 0 && error != ENOENT) 2136191673Sjamie goto done_deref; 2137231267Smm error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum, 2138231267Smm sizeof(pr->pr_devfs_rsnum)); 2139231267Smm if (error != 0 && error != ENOENT) 2140231267Smm goto done_deref; 2141192895Sjamie for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 2142192895Sjamie fi++) { 2143192895Sjamie if (pr_flag_names[fi] == NULL) 2144192895Sjamie continue; 2145192895Sjamie i = (pr->pr_flags & (1 << fi)) ? 1 : 0; 2146192895Sjamie error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i)); 2147192895Sjamie if (error != 0 && error != ENOENT) 2148192895Sjamie goto done_deref; 2149192895Sjamie i = !i; 2150192895Sjamie error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i)); 2151192895Sjamie if (error != 0 && error != ENOENT) 2152192895Sjamie goto done_deref; 2153192895Sjamie } 2154195870Sjamie for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 2155195870Sjamie fi++) { 2156195870Sjamie i = pr->pr_flags & 2157195870Sjamie (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new); 2158195870Sjamie i = pr_flag_jailsys[fi].disable && 2159195870Sjamie (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE 2160195870Sjamie : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW 2161195870Sjamie : JAIL_SYS_INHERIT; 2162195870Sjamie error = 2163195870Sjamie vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i)); 2164195870Sjamie if (error != 0 && error != ENOENT) 2165195870Sjamie goto done_deref; 2166195870Sjamie } 2167192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 2168192895Sjamie fi++) { 2169192895Sjamie if (pr_allow_names[fi] == NULL) 2170192895Sjamie continue; 2171192895Sjamie i = (pr->pr_allow & (1 << fi)) ? 1 : 0; 2172192895Sjamie error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i)); 2173192895Sjamie if (error != 0 && error != ENOENT) 2174192895Sjamie goto done_deref; 2175192895Sjamie i = !i; 2176192895Sjamie error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i)); 2177192895Sjamie if (error != 0 && error != ENOENT) 2178192895Sjamie goto done_deref; 2179192895Sjamie } 2180191673Sjamie i = (pr->pr_uref == 0); 2181191673Sjamie error = vfs_setopt(opts, "dying", &i, sizeof(i)); 2182191673Sjamie if (error != 0 && error != ENOENT) 2183191673Sjamie goto done_deref; 2184191673Sjamie i = !i; 2185191673Sjamie error = vfs_setopt(opts, "nodying", &i, sizeof(i)); 2186191673Sjamie if (error != 0 && error != ENOENT) 2187191673Sjamie goto done_deref; 2188280632Sian error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate, 2189280632Sian sizeof(pr->pr_osreldate)); 2190280632Sian if (error != 0 && error != ENOENT) 2191280632Sian goto done_deref; 2192280632Sian error = vfs_setopts(opts, "osrelease", pr->pr_osrelease); 2193280632Sian if (error != 0 && error != ENOENT) 2194280632Sian goto done_deref; 2195191673Sjamie 2196191673Sjamie /* Get the module parameters. */ 2197191673Sjamie mtx_unlock(&pr->pr_mtx); 2198191673Sjamie locked = 0; 2199191673Sjamie error = osd_jail_call(pr, PR_METHOD_GET, opts); 220046155Sphk if (error) 2201191673Sjamie goto done_deref; 2202191673Sjamie prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED); 220384828Sjhb 2204191673Sjamie /* By now, all parameters should have been noted. */ 2205191673Sjamie TAILQ_FOREACH(opt, opts, link) { 2206191673Sjamie if (!opt->seen && strcmp(opt->name, "errmsg")) { 2207191673Sjamie error = EINVAL; 2208191673Sjamie vfs_opterror(opts, "unknown parameter: %s", opt->name); 2209191673Sjamie goto done_errmsg; 2210191673Sjamie } 2211185435Sbz } 2212191673Sjamie 2213191673Sjamie /* Write the fetched parameters back to userspace. */ 2214191673Sjamie error = 0; 2215191673Sjamie TAILQ_FOREACH(opt, opts, link) { 2216191673Sjamie if (opt->pos >= 0 && opt->pos != errmsg_pos) { 2217191673Sjamie pos = 2 * opt->pos + 1; 2218191673Sjamie optuio->uio_iov[pos].iov_len = opt->len; 2219191673Sjamie if (opt->value != NULL) { 2220191673Sjamie if (optuio->uio_segflg == UIO_SYSSPACE) { 2221191673Sjamie bcopy(opt->value, 2222191673Sjamie optuio->uio_iov[pos].iov_base, 2223191673Sjamie opt->len); 2224191673Sjamie } else { 2225191673Sjamie error = copyout(opt->value, 2226191673Sjamie optuio->uio_iov[pos].iov_base, 2227191673Sjamie opt->len); 2228191673Sjamie if (error) 2229191673Sjamie break; 2230191673Sjamie } 2231191673Sjamie } 2232191673Sjamie } 2233185435Sbz } 2234191673Sjamie goto done_errmsg; 2235191673Sjamie 2236191673Sjamie done_deref: 2237191673Sjamie prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED); 2238191673Sjamie goto done_errmsg; 2239191673Sjamie 2240191673Sjamie done_unlock_list: 2241191673Sjamie sx_sunlock(&allprison_lock); 2242191673Sjamie done_errmsg: 2243191673Sjamie if (error && errmsg_pos >= 0) { 2244191673Sjamie vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 2245191673Sjamie errmsg_pos = 2 * errmsg_pos + 1; 2246191673Sjamie if (errmsg_len > 0) { 2247191673Sjamie if (optuio->uio_segflg == UIO_SYSSPACE) 2248191673Sjamie bcopy(errmsg, 2249191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 2250191673Sjamie errmsg_len); 2251191673Sjamie else 2252191673Sjamie copyout(errmsg, 2253191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 2254191673Sjamie errmsg_len); 2255191673Sjamie } 2256185435Sbz } 2257191673Sjamie vfs_freeopts(opts); 2258191673Sjamie return (error); 2259191673Sjamie} 2260113275Smike 2261192895Sjamie 2262191673Sjamie/* 2263191673Sjamie * struct jail_remove_args { 2264191673Sjamie * int jid; 2265191673Sjamie * }; 2266191673Sjamie */ 2267191673Sjamieint 2268225617Skmacysys_jail_remove(struct thread *td, struct jail_remove_args *uap) 2269191673Sjamie{ 2270192895Sjamie struct prison *pr, *cpr, *lpr, *tpr; 2271192895Sjamie int descend, error; 2272185435Sbz 2273191673Sjamie error = priv_check(td, PRIV_JAIL_REMOVE); 2274185435Sbz if (error) 2275191673Sjamie return (error); 2276185435Sbz 2277185435Sbz sx_xlock(&allprison_lock); 2278192895Sjamie pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2279191673Sjamie if (pr == NULL) { 2280185435Sbz sx_xunlock(&allprison_lock); 2281191673Sjamie return (EINVAL); 2282185435Sbz } 2283185435Sbz 2284192895Sjamie /* Remove all descendants of this prison, then remove this prison. */ 2285192895Sjamie pr->pr_ref++; 2286192895Sjamie pr->pr_flags |= PR_REMOVE; 2287192895Sjamie if (!LIST_EMPTY(&pr->pr_children)) { 2288192895Sjamie mtx_unlock(&pr->pr_mtx); 2289192895Sjamie lpr = NULL; 2290192895Sjamie FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 2291192895Sjamie mtx_lock(&cpr->pr_mtx); 2292192895Sjamie if (cpr->pr_ref > 0) { 2293192895Sjamie tpr = cpr; 2294192895Sjamie cpr->pr_ref++; 2295192895Sjamie cpr->pr_flags |= PR_REMOVE; 2296192895Sjamie } else { 2297192895Sjamie /* Already removed - do not do it again. */ 2298192895Sjamie tpr = NULL; 2299192895Sjamie } 2300192895Sjamie mtx_unlock(&cpr->pr_mtx); 2301192895Sjamie if (lpr != NULL) { 2302192895Sjamie mtx_lock(&lpr->pr_mtx); 2303192895Sjamie prison_remove_one(lpr); 2304192895Sjamie sx_xlock(&allprison_lock); 2305192895Sjamie } 2306192895Sjamie lpr = tpr; 2307192895Sjamie } 2308192895Sjamie if (lpr != NULL) { 2309192895Sjamie mtx_lock(&lpr->pr_mtx); 2310192895Sjamie prison_remove_one(lpr); 2311192895Sjamie sx_xlock(&allprison_lock); 2312192895Sjamie } 2313192895Sjamie mtx_lock(&pr->pr_mtx); 2314192895Sjamie } 2315192895Sjamie prison_remove_one(pr); 2316192895Sjamie return (0); 2317192895Sjamie} 2318192895Sjamie 2319192895Sjamiestatic void 2320192895Sjamieprison_remove_one(struct prison *pr) 2321192895Sjamie{ 2322192895Sjamie struct proc *p; 2323192895Sjamie int deuref; 2324192895Sjamie 2325191673Sjamie /* If the prison was persistent, it is not anymore. */ 2326191673Sjamie deuref = 0; 2327191673Sjamie if (pr->pr_flags & PR_PERSIST) { 2328191673Sjamie pr->pr_ref--; 2329191673Sjamie deuref = PD_DEUREF; 2330191673Sjamie pr->pr_flags &= ~PR_PERSIST; 2331179881Sdelphij } 2332113275Smike 2333192895Sjamie /* 2334192895Sjamie * jail_remove added a reference. If that's the only one, remove 2335192895Sjamie * the prison now. 2336192895Sjamie */ 2337192895Sjamie KASSERT(pr->pr_ref > 0, 2338192895Sjamie ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id)); 2339192895Sjamie if (pr->pr_ref == 1) { 2340191673Sjamie prison_deref(pr, 2341191673Sjamie deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 2342192895Sjamie return; 2343191673Sjamie } 2344191673Sjamie 2345113275Smike mtx_unlock(&pr->pr_mtx); 2346191673Sjamie sx_xunlock(&allprison_lock); 2347191673Sjamie /* 2348191673Sjamie * Kill all processes unfortunate enough to be attached to this prison. 2349191673Sjamie */ 2350191673Sjamie sx_slock(&allproc_lock); 2351191673Sjamie LIST_FOREACH(p, &allproc, p_list) { 2352191673Sjamie PROC_LOCK(p); 2353191673Sjamie if (p->p_state != PRS_NEW && p->p_ucred && 2354191673Sjamie p->p_ucred->cr_prison == pr) 2355225617Skmacy kern_psignal(p, SIGKILL); 2356191673Sjamie PROC_UNLOCK(p); 2357191673Sjamie } 2358191673Sjamie sx_sunlock(&allproc_lock); 2359192895Sjamie /* Remove the temporary reference added by jail_remove. */ 2360191673Sjamie prison_deref(pr, deuref | PD_DEREF); 2361113275Smike} 2362113275Smike 2363190466Sjamie 2364113275Smike/* 2365114168Smike * struct jail_attach_args { 2366114168Smike * int jid; 2367114168Smike * }; 2368113275Smike */ 2369113275Smikeint 2370225617Skmacysys_jail_attach(struct thread *td, struct jail_attach_args *uap) 2371113275Smike{ 2372113275Smike struct prison *pr; 2373191673Sjamie int error; 2374167309Spjd 2375164032Srwatson error = priv_check(td, PRIV_JAIL_ATTACH); 2376126023Snectar if (error) 2377126023Snectar return (error); 2378126023Snectar 2379168401Spjd sx_slock(&allprison_lock); 2380192895Sjamie pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2381113275Smike if (pr == NULL) { 2382168401Spjd sx_sunlock(&allprison_lock); 2383113275Smike return (EINVAL); 2384113275Smike } 2385185435Sbz 2386185435Sbz /* 2387185435Sbz * Do not allow a process to attach to a prison that is not 2388191673Sjamie * considered to be "alive". 2389185435Sbz */ 2390191673Sjamie if (pr->pr_uref == 0) { 2391185435Sbz mtx_unlock(&pr->pr_mtx); 2392185435Sbz sx_sunlock(&allprison_lock); 2393185435Sbz return (EINVAL); 2394185435Sbz } 2395191673Sjamie 2396191673Sjamie return (do_jail_attach(td, pr)); 2397191673Sjamie} 2398191673Sjamie 2399191673Sjamiestatic int 2400191673Sjamiedo_jail_attach(struct thread *td, struct prison *pr) 2401191673Sjamie{ 2402192895Sjamie struct prison *ppr; 2403191673Sjamie struct proc *p; 2404191673Sjamie struct ucred *newcred, *oldcred; 2405241896Skib int error; 2406191673Sjamie 2407191673Sjamie /* 2408191673Sjamie * XXX: Note that there is a slight race here if two threads 2409191673Sjamie * in the same privileged process attempt to attach to two 2410191673Sjamie * different jails at the same time. It is important for 2411191673Sjamie * user processes not to do this, or they might end up with 2412191673Sjamie * a process root from one prison, but attached to the jail 2413191673Sjamie * of another. 2414191673Sjamie */ 2415113275Smike pr->pr_ref++; 2416191673Sjamie pr->pr_uref++; 2417113275Smike mtx_unlock(&pr->pr_mtx); 2418191673Sjamie 2419191673Sjamie /* Let modules do whatever they need to prepare for attaching. */ 2420191673Sjamie error = osd_jail_call(pr, PR_METHOD_ATTACH, td); 2421191673Sjamie if (error) { 2422191673Sjamie prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED); 2423191673Sjamie return (error); 2424191673Sjamie } 2425168401Spjd sx_sunlock(&allprison_lock); 2426113275Smike 2427185435Sbz /* 2428185435Sbz * Reparent the newly attached process to this jail. 2429185435Sbz */ 2430192895Sjamie ppr = td->td_ucred->cr_prison; 2431191673Sjamie p = td->td_proc; 2432185435Sbz error = cpuset_setproc_update_set(p, pr->pr_cpuset); 2433185435Sbz if (error) 2434191673Sjamie goto e_revert_osd; 2435185435Sbz 2436175202Sattilio vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY); 2437113275Smike if ((error = change_dir(pr->pr_root, td)) != 0) 2438113275Smike goto e_unlock; 2439113275Smike#ifdef MAC 2440172930Srwatson if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root))) 2441113275Smike goto e_unlock; 2442113275Smike#endif 2443175294Sattilio VOP_UNLOCK(pr->pr_root, 0); 2444191673Sjamie if ((error = change_root(pr->pr_root, td))) 2445241896Skib goto e_revert_osd; 2446113275Smike 244784828Sjhb newcred = crget(); 244884828Sjhb PROC_LOCK(p); 244984828Sjhb oldcred = p->p_ucred; 2450113275Smike setsugid(p); 245184828Sjhb crcopy(newcred, oldcred); 2452113630Sjhb newcred->cr_prison = pr; 245384828Sjhb p->p_ucred = newcred; 245484828Sjhb PROC_UNLOCK(p); 2455220137Strasz#ifdef RACCT 2456220137Strasz racct_proc_ucred_changed(p, oldcred, newcred); 2457220137Strasz#endif 245884828Sjhb crfree(oldcred); 2459192895Sjamie prison_deref(ppr, PD_DEREF | PD_DEUREF); 246046155Sphk return (0); 2461191673Sjamie e_unlock: 2462175294Sattilio VOP_UNLOCK(pr->pr_root, 0); 2463191673Sjamie e_revert_osd: 2464191673Sjamie /* Tell modules this thread is still in its old jail after all. */ 2465192895Sjamie (void)osd_jail_call(ppr, PR_METHOD_ATTACH, td); 2466191673Sjamie prison_deref(pr, PD_DEREF | PD_DEUREF); 246746155Sphk return (error); 246846155Sphk} 246946155Sphk 2470192895Sjamie 2471113275Smike/* 2472113275Smike * Returns a locked prison instance, or NULL on failure. 2473113275Smike */ 2474168399Spjdstruct prison * 2475113275Smikeprison_find(int prid) 2476113275Smike{ 2477113275Smike struct prison *pr; 2478113275Smike 2479168401Spjd sx_assert(&allprison_lock, SX_LOCKED); 2480191673Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) { 2481113275Smike if (pr->pr_id == prid) { 2482113275Smike mtx_lock(&pr->pr_mtx); 2483191673Sjamie if (pr->pr_ref > 0) 2484191673Sjamie return (pr); 2485191673Sjamie mtx_unlock(&pr->pr_mtx); 2486113275Smike } 2487113275Smike } 2488113275Smike return (NULL); 2489113275Smike} 2490113275Smike 2491191673Sjamie/* 2492192895Sjamie * Find a prison that is a descendant of mypr. Returns a locked prison or NULL. 2493191673Sjamie */ 2494191673Sjamiestruct prison * 2495192895Sjamieprison_find_child(struct prison *mypr, int prid) 2496191673Sjamie{ 2497192895Sjamie struct prison *pr; 2498192895Sjamie int descend; 2499192895Sjamie 2500192895Sjamie sx_assert(&allprison_lock, SX_LOCKED); 2501192895Sjamie FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 2502192895Sjamie if (pr->pr_id == prid) { 2503192895Sjamie mtx_lock(&pr->pr_mtx); 2504192895Sjamie if (pr->pr_ref > 0) 2505192895Sjamie return (pr); 2506192895Sjamie mtx_unlock(&pr->pr_mtx); 2507192895Sjamie } 2508192895Sjamie } 2509192895Sjamie return (NULL); 2510192895Sjamie} 2511192895Sjamie 2512192895Sjamie/* 2513192895Sjamie * Look for the name relative to mypr. Returns a locked prison or NULL. 2514192895Sjamie */ 2515192895Sjamiestruct prison * 2516192895Sjamieprison_find_name(struct prison *mypr, const char *name) 2517192895Sjamie{ 2518191673Sjamie struct prison *pr, *deadpr; 2519192895Sjamie size_t mylen; 2520192895Sjamie int descend; 2521191673Sjamie 2522191673Sjamie sx_assert(&allprison_lock, SX_LOCKED); 2523192895Sjamie mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1; 2524191673Sjamie again: 2525191673Sjamie deadpr = NULL; 2526192895Sjamie FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 2527192895Sjamie if (!strcmp(pr->pr_name + mylen, name)) { 2528191673Sjamie mtx_lock(&pr->pr_mtx); 2529191673Sjamie if (pr->pr_ref > 0) { 2530191673Sjamie if (pr->pr_uref > 0) 2531191673Sjamie return (pr); 2532191673Sjamie deadpr = pr; 2533191673Sjamie } 2534191673Sjamie mtx_unlock(&pr->pr_mtx); 2535191673Sjamie } 2536191673Sjamie } 2537192895Sjamie /* There was no valid prison - perhaps there was a dying one. */ 2538191673Sjamie if (deadpr != NULL) { 2539191673Sjamie mtx_lock(&deadpr->pr_mtx); 2540191673Sjamie if (deadpr->pr_ref == 0) { 2541191673Sjamie mtx_unlock(&deadpr->pr_mtx); 2542191673Sjamie goto again; 2543191673Sjamie } 2544191673Sjamie } 2545191673Sjamie return (deadpr); 2546191673Sjamie} 2547191673Sjamie 2548191673Sjamie/* 2549192895Sjamie * See if a prison has the specific flag set. 2550192895Sjamie */ 2551192895Sjamieint 2552192895Sjamieprison_flag(struct ucred *cred, unsigned flag) 2553192895Sjamie{ 2554192895Sjamie 2555192895Sjamie /* This is an atomic read, so no locking is necessary. */ 2556192895Sjamie return (cred->cr_prison->pr_flags & flag); 2557192895Sjamie} 2558192895Sjamie 2559192895Sjamieint 2560192895Sjamieprison_allow(struct ucred *cred, unsigned flag) 2561192895Sjamie{ 2562192895Sjamie 2563192895Sjamie /* This is an atomic read, so no locking is necessary. */ 2564192895Sjamie return (cred->cr_prison->pr_allow & flag); 2565192895Sjamie} 2566192895Sjamie 2567192895Sjamie/* 2568191673Sjamie * Remove a prison reference. If that was the last reference, remove the 2569191673Sjamie * prison itself - but not in this context in case there are locks held. 2570191673Sjamie */ 257172786Srwatsonvoid 2572185029Spjdprison_free_locked(struct prison *pr) 257372786Srwatson{ 257472786Srwatson 2575185029Spjd mtx_assert(&pr->pr_mtx, MA_OWNED); 257672786Srwatson pr->pr_ref--; 257772786Srwatson if (pr->pr_ref == 0) { 2578168483Spjd mtx_unlock(&pr->pr_mtx); 2579124882Srwatson TASK_INIT(&pr->pr_task, 0, prison_complete, pr); 2580144660Sjeff taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 258187275Srwatson return; 258272786Srwatson } 258387275Srwatson mtx_unlock(&pr->pr_mtx); 258472786Srwatson} 258572786Srwatson 2586185029Spjdvoid 2587185029Spjdprison_free(struct prison *pr) 2588185029Spjd{ 2589185029Spjd 2590185029Spjd mtx_lock(&pr->pr_mtx); 2591185029Spjd prison_free_locked(pr); 2592185029Spjd} 2593185029Spjd 2594124882Srwatsonstatic void 2595124882Srwatsonprison_complete(void *context, int pending) 2596124882Srwatson{ 2597191673Sjamie 2598191673Sjamie prison_deref((struct prison *)context, 0); 2599191673Sjamie} 2600191673Sjamie 2601191673Sjamie/* 2602191673Sjamie * Remove a prison reference (usually). This internal version assumes no 2603191673Sjamie * mutexes are held, except perhaps the prison itself. If there are no more 2604191673Sjamie * references, release and delist the prison. On completion, the prison lock 2605191673Sjamie * and the allprison lock are both unlocked. 2606191673Sjamie */ 2607191673Sjamiestatic void 2608191673Sjamieprison_deref(struct prison *pr, int flags) 2609191673Sjamie{ 2610192895Sjamie struct prison *ppr, *tpr; 2611124882Srwatson 2612191673Sjamie if (!(flags & PD_LOCKED)) 2613191673Sjamie mtx_lock(&pr->pr_mtx); 2614225191Sjamie for (;;) { 2615225191Sjamie if (flags & PD_DEUREF) { 2616225191Sjamie pr->pr_uref--; 2617225191Sjamie KASSERT(prison0.pr_uref != 0, ("prison0 pr_uref=0")); 2618192895Sjamie } 2619192895Sjamie if (flags & PD_DEREF) 2620192895Sjamie pr->pr_ref--; 2621192895Sjamie /* If the prison still has references, nothing else to do. */ 2622192895Sjamie if (pr->pr_ref > 0) { 2623192895Sjamie mtx_unlock(&pr->pr_mtx); 2624192895Sjamie if (flags & PD_LIST_SLOCKED) 2625192895Sjamie sx_sunlock(&allprison_lock); 2626192895Sjamie else if (flags & PD_LIST_XLOCKED) 2627192895Sjamie sx_xunlock(&allprison_lock); 2628192895Sjamie return; 2629191673Sjamie } 2630191673Sjamie 2631192895Sjamie mtx_unlock(&pr->pr_mtx); 2632192895Sjamie if (flags & PD_LIST_SLOCKED) { 2633192895Sjamie if (!sx_try_upgrade(&allprison_lock)) { 2634192895Sjamie sx_sunlock(&allprison_lock); 2635192895Sjamie sx_xlock(&allprison_lock); 2636192895Sjamie } 2637192895Sjamie } else if (!(flags & PD_LIST_XLOCKED)) 2638192895Sjamie sx_xlock(&allprison_lock); 2639168489Spjd 2640192895Sjamie TAILQ_REMOVE(&allprison, pr, pr_list); 2641192895Sjamie LIST_REMOVE(pr, pr_sibling); 2642192895Sjamie ppr = pr->pr_parent; 2643192895Sjamie for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 2644194762Sjamie tpr->pr_childcount--; 2645196592Sjamie sx_xunlock(&allprison_lock); 2646192895Sjamie 2647194251Sjamie#ifdef VIMAGE 2648196505Szec if (pr->pr_vnet != ppr->pr_vnet) 2649194251Sjamie vnet_destroy(pr->pr_vnet); 2650194251Sjamie#endif 2651241896Skib if (pr->pr_root != NULL) 2652192895Sjamie vrele(pr->pr_root); 2653192895Sjamie mtx_destroy(&pr->pr_mtx); 2654191673Sjamie#ifdef INET 2655192895Sjamie free(pr->pr_ip4, M_PRISON); 2656191673Sjamie#endif 2657185435Sbz#ifdef INET6 2658192895Sjamie free(pr->pr_ip6, M_PRISON); 2659185435Sbz#endif 2660192895Sjamie if (pr->pr_cpuset != NULL) 2661192895Sjamie cpuset_rel(pr->pr_cpuset); 2662192895Sjamie osd_jail_exit(pr); 2663221362Strasz#ifdef RACCT 2664221362Strasz prison_racct_detach(pr); 2665220163Strasz#endif 2666192895Sjamie free(pr, M_PRISON); 2667192895Sjamie 2668192895Sjamie /* Removing a prison frees a reference on its parent. */ 2669192895Sjamie pr = ppr; 2670192895Sjamie mtx_lock(&pr->pr_mtx); 2671225191Sjamie flags = PD_DEREF | PD_DEUREF; 2672192895Sjamie } 2673124882Srwatson} 2674124882Srwatson 267572786Srwatsonvoid 2676185029Spjdprison_hold_locked(struct prison *pr) 267772786Srwatson{ 267872786Srwatson 2679185029Spjd mtx_assert(&pr->pr_mtx, MA_OWNED); 2680168489Spjd KASSERT(pr->pr_ref > 0, 2681191671Sjamie ("Trying to hold dead prison (jid=%d).", pr->pr_id)); 268272786Srwatson pr->pr_ref++; 2683185029Spjd} 2684185029Spjd 2685185029Spjdvoid 2686185029Spjdprison_hold(struct prison *pr) 2687185029Spjd{ 2688185029Spjd 2689185029Spjd mtx_lock(&pr->pr_mtx); 2690185029Spjd prison_hold_locked(pr); 269187275Srwatson mtx_unlock(&pr->pr_mtx); 269272786Srwatson} 269372786Srwatson 2694185435Sbzvoid 2695185435Sbzprison_proc_hold(struct prison *pr) 269687275Srwatson{ 269787275Srwatson 2698185435Sbz mtx_lock(&pr->pr_mtx); 2699191673Sjamie KASSERT(pr->pr_uref > 0, 2700191673Sjamie ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id)); 2701191673Sjamie pr->pr_uref++; 2702185435Sbz mtx_unlock(&pr->pr_mtx); 270387275Srwatson} 270487275Srwatson 2705185435Sbzvoid 2706185435Sbzprison_proc_free(struct prison *pr) 2707185435Sbz{ 2708185435Sbz 2709185435Sbz mtx_lock(&pr->pr_mtx); 2710191673Sjamie KASSERT(pr->pr_uref > 0, 2711191673Sjamie ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id)); 2712191673Sjamie prison_deref(pr, PD_DEUREF | PD_LOCKED); 2713185435Sbz} 2714185435Sbz 2715185435Sbz 2716185435Sbz#ifdef INET 2717185435Sbz/* 2718192895Sjamie * Restrict a prison's IP address list with its parent's, possibly replacing 2719192895Sjamie * it. Return true if the replacement buffer was used (or would have been). 2720192895Sjamie */ 2721192895Sjamiestatic int 2722192895Sjamieprison_restrict_ip4(struct prison *pr, struct in_addr *newip4) 2723192895Sjamie{ 2724192895Sjamie int ii, ij, used; 2725192895Sjamie struct prison *ppr; 2726192895Sjamie 2727192895Sjamie ppr = pr->pr_parent; 2728192895Sjamie if (!(pr->pr_flags & PR_IP4_USER)) { 2729192895Sjamie /* This has no user settings, so just copy the parent's list. */ 2730192895Sjamie if (pr->pr_ip4s < ppr->pr_ip4s) { 2731192895Sjamie /* 2732192895Sjamie * There's no room for the parent's list. Use the 2733192895Sjamie * new list buffer, which is assumed to be big enough 2734192895Sjamie * (if it was passed). If there's no buffer, try to 2735192895Sjamie * allocate one. 2736192895Sjamie */ 2737192895Sjamie used = 1; 2738192895Sjamie if (newip4 == NULL) { 2739192895Sjamie newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4), 2740192895Sjamie M_PRISON, M_NOWAIT); 2741192895Sjamie if (newip4 != NULL) 2742192895Sjamie used = 0; 2743192895Sjamie } 2744192895Sjamie if (newip4 != NULL) { 2745192895Sjamie bcopy(ppr->pr_ip4, newip4, 2746192895Sjamie ppr->pr_ip4s * sizeof(*newip4)); 2747192895Sjamie free(pr->pr_ip4, M_PRISON); 2748192895Sjamie pr->pr_ip4 = newip4; 2749192895Sjamie pr->pr_ip4s = ppr->pr_ip4s; 2750192895Sjamie } 2751192895Sjamie return (used); 2752192895Sjamie } 2753192895Sjamie pr->pr_ip4s = ppr->pr_ip4s; 2754192895Sjamie if (pr->pr_ip4s > 0) 2755192895Sjamie bcopy(ppr->pr_ip4, pr->pr_ip4, 2756192895Sjamie pr->pr_ip4s * sizeof(*newip4)); 2757192895Sjamie else if (pr->pr_ip4 != NULL) { 2758192895Sjamie free(pr->pr_ip4, M_PRISON); 2759192895Sjamie pr->pr_ip4 = NULL; 2760192895Sjamie } 2761195974Sjamie } else if (pr->pr_ip4s > 0) { 2762192895Sjamie /* Remove addresses that aren't in the parent. */ 2763192895Sjamie for (ij = 0; ij < ppr->pr_ip4s; ij++) 2764192895Sjamie if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 2765192895Sjamie break; 2766192895Sjamie if (ij < ppr->pr_ip4s) 2767192895Sjamie ii = 1; 2768192895Sjamie else { 2769192895Sjamie bcopy(pr->pr_ip4 + 1, pr->pr_ip4, 2770192895Sjamie --pr->pr_ip4s * sizeof(*pr->pr_ip4)); 2771192895Sjamie ii = 0; 2772192895Sjamie } 2773192895Sjamie for (ij = 1; ii < pr->pr_ip4s; ) { 2774192895Sjamie if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) { 2775192895Sjamie ii++; 2776192895Sjamie continue; 2777192895Sjamie } 2778192895Sjamie switch (ij >= ppr->pr_ip4s ? -1 : 2779192895Sjamie qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) { 2780192895Sjamie case -1: 2781192895Sjamie bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii, 2782192895Sjamie (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4)); 2783192895Sjamie break; 2784192895Sjamie case 0: 2785192895Sjamie ii++; 2786192895Sjamie ij++; 2787192895Sjamie break; 2788192895Sjamie case 1: 2789192895Sjamie ij++; 2790192895Sjamie break; 2791192895Sjamie } 2792192895Sjamie } 2793192895Sjamie if (pr->pr_ip4s == 0) { 2794195870Sjamie pr->pr_flags |= PR_IP4_DISABLE; 2795192895Sjamie free(pr->pr_ip4, M_PRISON); 2796192895Sjamie pr->pr_ip4 = NULL; 2797192895Sjamie } 2798192895Sjamie } 2799192895Sjamie return (0); 2800192895Sjamie} 2801192895Sjamie 2802192895Sjamie/* 2803185435Sbz * Pass back primary IPv4 address of this jail. 2804185435Sbz * 2805192895Sjamie * If not restricted return success but do not alter the address. Caller has 2806192895Sjamie * to make sure to initialize it correctly (e.g. INADDR_ANY). 2807185435Sbz * 2808188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4. 2809188144Sjamie * Address returned in NBO. 2810185435Sbz */ 281146155Sphkint 2812187684Sbzprison_get_ip4(struct ucred *cred, struct in_addr *ia) 281346155Sphk{ 2814191673Sjamie struct prison *pr; 281546155Sphk 2816185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2817185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2818185435Sbz 2819192895Sjamie pr = cred->cr_prison; 2820192895Sjamie if (!(pr->pr_flags & PR_IP4)) 282146155Sphk return (0); 2822191673Sjamie mtx_lock(&pr->pr_mtx); 2823192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2824192895Sjamie mtx_unlock(&pr->pr_mtx); 2825192895Sjamie return (0); 2826192895Sjamie } 2827191673Sjamie if (pr->pr_ip4 == NULL) { 2828191673Sjamie mtx_unlock(&pr->pr_mtx); 2829188144Sjamie return (EAFNOSUPPORT); 2830191673Sjamie } 2831185435Sbz 2832191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2833191673Sjamie mtx_unlock(&pr->pr_mtx); 2834185435Sbz return (0); 2835185435Sbz} 2836185435Sbz 2837185435Sbz/* 2838202468Sbz * Return 1 if we should do proper source address selection or are not jailed. 2839202468Sbz * We will return 0 if we should bypass source address selection in favour 2840202468Sbz * of the primary jail IPv4 address. Only in this case *ia will be updated and 2841202468Sbz * returned in NBO. 2842202468Sbz * Return EAFNOSUPPORT, in case this jail does not allow IPv4. 2843202468Sbz */ 2844202468Sbzint 2845202468Sbzprison_saddrsel_ip4(struct ucred *cred, struct in_addr *ia) 2846202468Sbz{ 2847202468Sbz struct prison *pr; 2848202468Sbz struct in_addr lia; 2849202468Sbz int error; 2850202468Sbz 2851202468Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2852202468Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2853202468Sbz 2854202468Sbz if (!jailed(cred)) 2855202468Sbz return (1); 2856202468Sbz 2857202468Sbz pr = cred->cr_prison; 2858202468Sbz if (pr->pr_flags & PR_IP4_SADDRSEL) 2859202468Sbz return (1); 2860202468Sbz 2861202468Sbz lia.s_addr = INADDR_ANY; 2862202468Sbz error = prison_get_ip4(cred, &lia); 2863202468Sbz if (error) 2864202468Sbz return (error); 2865202468Sbz if (lia.s_addr == INADDR_ANY) 2866202468Sbz return (1); 2867202468Sbz 2868202468Sbz ia->s_addr = lia.s_addr; 2869202468Sbz return (0); 2870202468Sbz} 2871202468Sbz 2872202468Sbz/* 2873192895Sjamie * Return true if pr1 and pr2 have the same IPv4 address restrictions. 2874192895Sjamie */ 2875192895Sjamieint 2876192895Sjamieprison_equal_ip4(struct prison *pr1, struct prison *pr2) 2877192895Sjamie{ 2878192895Sjamie 2879192895Sjamie if (pr1 == pr2) 2880192895Sjamie return (1); 2881192895Sjamie 2882192895Sjamie /* 2883195974Sjamie * No need to lock since the PR_IP4_USER flag can't be altered for 2884195974Sjamie * existing prisons. 2885192895Sjamie */ 2886195945Sjamie while (pr1 != &prison0 && 2887195945Sjamie#ifdef VIMAGE 2888195945Sjamie !(pr1->pr_flags & PR_VNET) && 2889195945Sjamie#endif 2890195945Sjamie !(pr1->pr_flags & PR_IP4_USER)) 2891192895Sjamie pr1 = pr1->pr_parent; 2892195945Sjamie while (pr2 != &prison0 && 2893195945Sjamie#ifdef VIMAGE 2894195945Sjamie !(pr2->pr_flags & PR_VNET) && 2895195945Sjamie#endif 2896195945Sjamie !(pr2->pr_flags & PR_IP4_USER)) 2897192895Sjamie pr2 = pr2->pr_parent; 2898192895Sjamie return (pr1 == pr2); 2899192895Sjamie} 2900192895Sjamie 2901192895Sjamie/* 2902185435Sbz * Make sure our (source) address is set to something meaningful to this 2903185435Sbz * jail. 2904185435Sbz * 2905192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail, 2906192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 2907192895Sjamie * doesn't allow IPv4. Address passed in in NBO and returned in NBO. 2908185435Sbz */ 2909185435Sbzint 2910185435Sbzprison_local_ip4(struct ucred *cred, struct in_addr *ia) 2911185435Sbz{ 2912191673Sjamie struct prison *pr; 2913185435Sbz struct in_addr ia0; 2914191673Sjamie int error; 2915185435Sbz 2916185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2917185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2918185435Sbz 2919192895Sjamie pr = cred->cr_prison; 2920192895Sjamie if (!(pr->pr_flags & PR_IP4)) 292146155Sphk return (0); 2922191673Sjamie mtx_lock(&pr->pr_mtx); 2923192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2924192895Sjamie mtx_unlock(&pr->pr_mtx); 2925192895Sjamie return (0); 2926192895Sjamie } 2927191673Sjamie if (pr->pr_ip4 == NULL) { 2928191673Sjamie mtx_unlock(&pr->pr_mtx); 2929188144Sjamie return (EAFNOSUPPORT); 2930191673Sjamie } 2931185435Sbz 2932185435Sbz ia0.s_addr = ntohl(ia->s_addr); 2933185435Sbz if (ia0.s_addr == INADDR_LOOPBACK) { 2934191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2935191673Sjamie mtx_unlock(&pr->pr_mtx); 2936185435Sbz return (0); 293746155Sphk } 2938185435Sbz 2939188144Sjamie if (ia0.s_addr == INADDR_ANY) { 2940188144Sjamie /* 2941188144Sjamie * In case there is only 1 IPv4 address, bind directly. 2942188144Sjamie */ 2943191673Sjamie if (pr->pr_ip4s == 1) 2944191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2945191673Sjamie mtx_unlock(&pr->pr_mtx); 2946185435Sbz return (0); 2947185435Sbz } 2948185435Sbz 2949191673Sjamie error = _prison_check_ip4(pr, ia); 2950191673Sjamie mtx_unlock(&pr->pr_mtx); 2951191673Sjamie return (error); 2952185435Sbz} 2953185435Sbz 2954185435Sbz/* 2955185435Sbz * Rewrite destination address in case we will connect to loopback address. 2956185435Sbz * 2957188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4. 2958188144Sjamie * Address passed in in NBO and returned in NBO. 2959185435Sbz */ 2960185435Sbzint 2961185435Sbzprison_remote_ip4(struct ucred *cred, struct in_addr *ia) 2962185435Sbz{ 2963191673Sjamie struct prison *pr; 2964185435Sbz 2965185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2966185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2967185435Sbz 2968192895Sjamie pr = cred->cr_prison; 2969192895Sjamie if (!(pr->pr_flags & PR_IP4)) 2970185435Sbz return (0); 2971191673Sjamie mtx_lock(&pr->pr_mtx); 2972192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2973192895Sjamie mtx_unlock(&pr->pr_mtx); 2974192895Sjamie return (0); 2975192895Sjamie } 2976191673Sjamie if (pr->pr_ip4 == NULL) { 2977191673Sjamie mtx_unlock(&pr->pr_mtx); 2978188144Sjamie return (EAFNOSUPPORT); 2979191673Sjamie } 2980188144Sjamie 2981185435Sbz if (ntohl(ia->s_addr) == INADDR_LOOPBACK) { 2982191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2983191673Sjamie mtx_unlock(&pr->pr_mtx); 2984185435Sbz return (0); 2985185435Sbz } 2986185435Sbz 2987185435Sbz /* 2988185435Sbz * Return success because nothing had to be changed. 2989185435Sbz */ 2990191673Sjamie mtx_unlock(&pr->pr_mtx); 2991185435Sbz return (0); 2992185435Sbz} 2993185435Sbz 2994185435Sbz/* 2995188144Sjamie * Check if given address belongs to the jail referenced by cred/prison. 2996185435Sbz * 2997192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail, 2998192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 2999192895Sjamie * doesn't allow IPv4. Address passed in in NBO. 3000185435Sbz */ 3001185435Sbzstatic int 3002185435Sbz_prison_check_ip4(struct prison *pr, struct in_addr *ia) 3003185435Sbz{ 3004185435Sbz int i, a, z, d; 3005185435Sbz 3006185435Sbz /* 3007185435Sbz * Check the primary IP. 3008185435Sbz */ 3009185435Sbz if (pr->pr_ip4[0].s_addr == ia->s_addr) 3010188144Sjamie return (0); 3011185435Sbz 3012185435Sbz /* 3013185435Sbz * All the other IPs are sorted so we can do a binary search. 3014185435Sbz */ 3015185435Sbz a = 0; 3016185435Sbz z = pr->pr_ip4s - 2; 3017185435Sbz while (a <= z) { 3018185435Sbz i = (a + z) / 2; 3019185435Sbz d = qcmp_v4(&pr->pr_ip4[i+1], ia); 3020185435Sbz if (d > 0) 3021185435Sbz z = i - 1; 3022185435Sbz else if (d < 0) 3023185435Sbz a = i + 1; 302481114Srwatson else 3025188144Sjamie return (0); 3026185435Sbz } 3027188144Sjamie 3028188144Sjamie return (EADDRNOTAVAIL); 3029185435Sbz} 3030185435Sbz 3031185435Sbzint 3032185435Sbzprison_check_ip4(struct ucred *cred, struct in_addr *ia) 3033185435Sbz{ 3034191673Sjamie struct prison *pr; 3035191673Sjamie int error; 3036185435Sbz 3037185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3038185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 3039185435Sbz 3040192895Sjamie pr = cred->cr_prison; 3041192895Sjamie if (!(pr->pr_flags & PR_IP4)) 3042188144Sjamie return (0); 3043191673Sjamie mtx_lock(&pr->pr_mtx); 3044192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 3045192895Sjamie mtx_unlock(&pr->pr_mtx); 3046192895Sjamie return (0); 3047192895Sjamie } 3048191673Sjamie if (pr->pr_ip4 == NULL) { 3049191673Sjamie mtx_unlock(&pr->pr_mtx); 3050188144Sjamie return (EAFNOSUPPORT); 3051191673Sjamie } 3052185435Sbz 3053191673Sjamie error = _prison_check_ip4(pr, ia); 3054191673Sjamie mtx_unlock(&pr->pr_mtx); 3055191673Sjamie return (error); 3056185435Sbz} 3057185435Sbz#endif 3058185435Sbz 3059185435Sbz#ifdef INET6 3060192895Sjamiestatic int 3061192895Sjamieprison_restrict_ip6(struct prison *pr, struct in6_addr *newip6) 3062192895Sjamie{ 3063192895Sjamie int ii, ij, used; 3064192895Sjamie struct prison *ppr; 3065192895Sjamie 3066192895Sjamie ppr = pr->pr_parent; 3067192895Sjamie if (!(pr->pr_flags & PR_IP6_USER)) { 3068192895Sjamie /* This has no user settings, so just copy the parent's list. */ 3069192895Sjamie if (pr->pr_ip6s < ppr->pr_ip6s) { 3070192895Sjamie /* 3071192895Sjamie * There's no room for the parent's list. Use the 3072192895Sjamie * new list buffer, which is assumed to be big enough 3073192895Sjamie * (if it was passed). If there's no buffer, try to 3074192895Sjamie * allocate one. 3075192895Sjamie */ 3076192895Sjamie used = 1; 3077192895Sjamie if (newip6 == NULL) { 3078192895Sjamie newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6), 3079192895Sjamie M_PRISON, M_NOWAIT); 3080192895Sjamie if (newip6 != NULL) 3081192895Sjamie used = 0; 3082192895Sjamie } 3083192895Sjamie if (newip6 != NULL) { 3084192895Sjamie bcopy(ppr->pr_ip6, newip6, 3085192895Sjamie ppr->pr_ip6s * sizeof(*newip6)); 3086192895Sjamie free(pr->pr_ip6, M_PRISON); 3087192895Sjamie pr->pr_ip6 = newip6; 3088192895Sjamie pr->pr_ip6s = ppr->pr_ip6s; 3089192895Sjamie } 3090192895Sjamie return (used); 3091192895Sjamie } 3092192895Sjamie pr->pr_ip6s = ppr->pr_ip6s; 3093192895Sjamie if (pr->pr_ip6s > 0) 3094192895Sjamie bcopy(ppr->pr_ip6, pr->pr_ip6, 3095192895Sjamie pr->pr_ip6s * sizeof(*newip6)); 3096192895Sjamie else if (pr->pr_ip6 != NULL) { 3097192895Sjamie free(pr->pr_ip6, M_PRISON); 3098192895Sjamie pr->pr_ip6 = NULL; 3099192895Sjamie } 3100195974Sjamie } else if (pr->pr_ip6s > 0) { 3101192895Sjamie /* Remove addresses that aren't in the parent. */ 3102192895Sjamie for (ij = 0; ij < ppr->pr_ip6s; ij++) 3103192895Sjamie if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], 3104192895Sjamie &ppr->pr_ip6[ij])) 3105192895Sjamie break; 3106192895Sjamie if (ij < ppr->pr_ip6s) 3107192895Sjamie ii = 1; 3108192895Sjamie else { 3109192895Sjamie bcopy(pr->pr_ip6 + 1, pr->pr_ip6, 3110192895Sjamie --pr->pr_ip6s * sizeof(*pr->pr_ip6)); 3111192895Sjamie ii = 0; 3112192895Sjamie } 3113192895Sjamie for (ij = 1; ii < pr->pr_ip6s; ) { 3114192895Sjamie if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii], 3115192895Sjamie &ppr->pr_ip6[0])) { 3116192895Sjamie ii++; 3117192895Sjamie continue; 3118192895Sjamie } 3119259847Sae switch (ij >= ppr->pr_ip6s ? -1 : 3120192895Sjamie qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) { 3121192895Sjamie case -1: 3122192895Sjamie bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii, 3123192895Sjamie (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6)); 3124192895Sjamie break; 3125192895Sjamie case 0: 3126192895Sjamie ii++; 3127192895Sjamie ij++; 3128192895Sjamie break; 3129192895Sjamie case 1: 3130192895Sjamie ij++; 3131192895Sjamie break; 3132192895Sjamie } 3133192895Sjamie } 3134192895Sjamie if (pr->pr_ip6s == 0) { 3135195870Sjamie pr->pr_flags |= PR_IP6_DISABLE; 3136192895Sjamie free(pr->pr_ip6, M_PRISON); 3137192895Sjamie pr->pr_ip6 = NULL; 3138192895Sjamie } 3139192895Sjamie } 3140192895Sjamie return 0; 3141192895Sjamie} 3142192895Sjamie 3143185435Sbz/* 3144185435Sbz * Pass back primary IPv6 address for this jail. 3145185435Sbz * 3146192895Sjamie * If not restricted return success but do not alter the address. Caller has 3147192895Sjamie * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT). 3148185435Sbz * 3149188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6. 3150185435Sbz */ 3151185435Sbzint 3152187684Sbzprison_get_ip6(struct ucred *cred, struct in6_addr *ia6) 3153185435Sbz{ 3154191673Sjamie struct prison *pr; 3155185435Sbz 3156185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3157185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3158185435Sbz 3159192895Sjamie pr = cred->cr_prison; 3160192895Sjamie if (!(pr->pr_flags & PR_IP6)) 316181114Srwatson return (0); 3162191673Sjamie mtx_lock(&pr->pr_mtx); 3163192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3164192895Sjamie mtx_unlock(&pr->pr_mtx); 3165192895Sjamie return (0); 3166192895Sjamie } 3167191673Sjamie if (pr->pr_ip6 == NULL) { 3168191673Sjamie mtx_unlock(&pr->pr_mtx); 3169188144Sjamie return (EAFNOSUPPORT); 3170191673Sjamie } 3171188144Sjamie 3172191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3173191673Sjamie mtx_unlock(&pr->pr_mtx); 3174185435Sbz return (0); 3175185435Sbz} 3176185435Sbz 3177185435Sbz/* 3178202468Sbz * Return 1 if we should do proper source address selection or are not jailed. 3179202468Sbz * We will return 0 if we should bypass source address selection in favour 3180202468Sbz * of the primary jail IPv6 address. Only in this case *ia will be updated and 3181202468Sbz * returned in NBO. 3182202468Sbz * Return EAFNOSUPPORT, in case this jail does not allow IPv6. 3183202468Sbz */ 3184202468Sbzint 3185202468Sbzprison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia6) 3186202468Sbz{ 3187202468Sbz struct prison *pr; 3188202468Sbz struct in6_addr lia6; 3189202468Sbz int error; 3190202468Sbz 3191202468Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3192202468Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3193202468Sbz 3194202468Sbz if (!jailed(cred)) 3195202468Sbz return (1); 3196202468Sbz 3197202468Sbz pr = cred->cr_prison; 3198202468Sbz if (pr->pr_flags & PR_IP6_SADDRSEL) 3199202468Sbz return (1); 3200202468Sbz 3201202468Sbz lia6 = in6addr_any; 3202202468Sbz error = prison_get_ip6(cred, &lia6); 3203202468Sbz if (error) 3204202468Sbz return (error); 3205202468Sbz if (IN6_IS_ADDR_UNSPECIFIED(&lia6)) 3206202468Sbz return (1); 3207202468Sbz 3208202468Sbz bcopy(&lia6, ia6, sizeof(struct in6_addr)); 3209202468Sbz return (0); 3210202468Sbz} 3211202468Sbz 3212202468Sbz/* 3213192895Sjamie * Return true if pr1 and pr2 have the same IPv6 address restrictions. 3214192895Sjamie */ 3215192895Sjamieint 3216192895Sjamieprison_equal_ip6(struct prison *pr1, struct prison *pr2) 3217192895Sjamie{ 3218192895Sjamie 3219192895Sjamie if (pr1 == pr2) 3220192895Sjamie return (1); 3221192895Sjamie 3222195945Sjamie while (pr1 != &prison0 && 3223195945Sjamie#ifdef VIMAGE 3224195945Sjamie !(pr1->pr_flags & PR_VNET) && 3225195945Sjamie#endif 3226195945Sjamie !(pr1->pr_flags & PR_IP6_USER)) 3227192895Sjamie pr1 = pr1->pr_parent; 3228195945Sjamie while (pr2 != &prison0 && 3229195945Sjamie#ifdef VIMAGE 3230195945Sjamie !(pr2->pr_flags & PR_VNET) && 3231195945Sjamie#endif 3232195945Sjamie !(pr2->pr_flags & PR_IP6_USER)) 3233192895Sjamie pr2 = pr2->pr_parent; 3234192895Sjamie return (pr1 == pr2); 3235192895Sjamie} 3236192895Sjamie 3237192895Sjamie/* 3238185435Sbz * Make sure our (source) address is set to something meaningful to this jail. 3239185435Sbz * 3240185435Sbz * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0) 3241185435Sbz * when needed while binding. 3242185435Sbz * 3243192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail, 3244192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 3245192895Sjamie * doesn't allow IPv6. 3246185435Sbz */ 3247185435Sbzint 3248185435Sbzprison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only) 3249185435Sbz{ 3250191673Sjamie struct prison *pr; 3251191673Sjamie int error; 3252185435Sbz 3253185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3254185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3255185435Sbz 3256192895Sjamie pr = cred->cr_prison; 3257192895Sjamie if (!(pr->pr_flags & PR_IP6)) 3258185435Sbz return (0); 3259191673Sjamie mtx_lock(&pr->pr_mtx); 3260192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3261192895Sjamie mtx_unlock(&pr->pr_mtx); 3262192895Sjamie return (0); 3263192895Sjamie } 3264191673Sjamie if (pr->pr_ip6 == NULL) { 3265191673Sjamie mtx_unlock(&pr->pr_mtx); 3266188144Sjamie return (EAFNOSUPPORT); 3267191673Sjamie } 3268188144Sjamie 3269185435Sbz if (IN6_IS_ADDR_LOOPBACK(ia6)) { 3270191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3271191673Sjamie mtx_unlock(&pr->pr_mtx); 3272185435Sbz return (0); 327381114Srwatson } 3274185435Sbz 3275188144Sjamie if (IN6_IS_ADDR_UNSPECIFIED(ia6)) { 3276188144Sjamie /* 3277188144Sjamie * In case there is only 1 IPv6 address, and v6only is true, 3278188144Sjamie * then bind directly. 3279188144Sjamie */ 3280191673Sjamie if (v6only != 0 && pr->pr_ip6s == 1) 3281191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3282191673Sjamie mtx_unlock(&pr->pr_mtx); 3283185435Sbz return (0); 3284185435Sbz } 3285188144Sjamie 3286191673Sjamie error = _prison_check_ip6(pr, ia6); 3287191673Sjamie mtx_unlock(&pr->pr_mtx); 3288191673Sjamie return (error); 3289185435Sbz} 3290185435Sbz 3291185435Sbz/* 3292185435Sbz * Rewrite destination address in case we will connect to loopback address. 3293185435Sbz * 3294188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6. 3295185435Sbz */ 3296185435Sbzint 3297185435Sbzprison_remote_ip6(struct ucred *cred, struct in6_addr *ia6) 3298185435Sbz{ 3299191673Sjamie struct prison *pr; 3300185435Sbz 3301185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3302185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3303185435Sbz 3304192895Sjamie pr = cred->cr_prison; 3305192895Sjamie if (!(pr->pr_flags & PR_IP6)) 3306185435Sbz return (0); 3307191673Sjamie mtx_lock(&pr->pr_mtx); 3308192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3309192895Sjamie mtx_unlock(&pr->pr_mtx); 3310192895Sjamie return (0); 3311192895Sjamie } 3312191673Sjamie if (pr->pr_ip6 == NULL) { 3313191673Sjamie mtx_unlock(&pr->pr_mtx); 3314188144Sjamie return (EAFNOSUPPORT); 3315191673Sjamie } 3316188144Sjamie 3317185435Sbz if (IN6_IS_ADDR_LOOPBACK(ia6)) { 3318191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3319191673Sjamie mtx_unlock(&pr->pr_mtx); 3320185435Sbz return (0); 3321185435Sbz } 3322185435Sbz 3323185435Sbz /* 3324185435Sbz * Return success because nothing had to be changed. 3325185435Sbz */ 3326191673Sjamie mtx_unlock(&pr->pr_mtx); 332746155Sphk return (0); 332846155Sphk} 332946155Sphk 3330185435Sbz/* 3331188144Sjamie * Check if given address belongs to the jail referenced by cred/prison. 3332185435Sbz * 3333192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail, 3334192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 3335192895Sjamie * doesn't allow IPv6. 3336185435Sbz */ 3337185435Sbzstatic int 3338185435Sbz_prison_check_ip6(struct prison *pr, struct in6_addr *ia6) 333946155Sphk{ 3340185435Sbz int i, a, z, d; 334146155Sphk 3342185435Sbz /* 3343185435Sbz * Check the primary IP. 3344185435Sbz */ 3345185435Sbz if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6)) 3346188144Sjamie return (0); 3347185435Sbz 3348185435Sbz /* 3349185435Sbz * All the other IPs are sorted so we can do a binary search. 3350185435Sbz */ 3351185435Sbz a = 0; 3352185435Sbz z = pr->pr_ip6s - 2; 3353185435Sbz while (a <= z) { 3354185435Sbz i = (a + z) / 2; 3355185435Sbz d = qcmp_v6(&pr->pr_ip6[i+1], ia6); 3356185435Sbz if (d > 0) 3357185435Sbz z = i - 1; 3358185435Sbz else if (d < 0) 3359185435Sbz a = i + 1; 336046155Sphk else 3361188144Sjamie return (0); 336246155Sphk } 3363188144Sjamie 3364188144Sjamie return (EADDRNOTAVAIL); 336546155Sphk} 336646155Sphk 336746155Sphkint 3368185435Sbzprison_check_ip6(struct ucred *cred, struct in6_addr *ia6) 3369185435Sbz{ 3370191673Sjamie struct prison *pr; 3371191673Sjamie int error; 3372185435Sbz 3373185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3374185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3375185435Sbz 3376192895Sjamie pr = cred->cr_prison; 3377192895Sjamie if (!(pr->pr_flags & PR_IP6)) 3378188144Sjamie return (0); 3379191673Sjamie mtx_lock(&pr->pr_mtx); 3380192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3381192895Sjamie mtx_unlock(&pr->pr_mtx); 3382192895Sjamie return (0); 3383192895Sjamie } 3384191673Sjamie if (pr->pr_ip6 == NULL) { 3385191673Sjamie mtx_unlock(&pr->pr_mtx); 3386188144Sjamie return (EAFNOSUPPORT); 3387191673Sjamie } 3388185435Sbz 3389191673Sjamie error = _prison_check_ip6(pr, ia6); 3390191673Sjamie mtx_unlock(&pr->pr_mtx); 3391191673Sjamie return (error); 3392185435Sbz} 3393185435Sbz#endif 3394185435Sbz 3395185435Sbz/* 3396188146Sjamie * Check if a jail supports the given address family. 3397188146Sjamie * 3398188146Sjamie * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT 3399188146Sjamie * if not. 3400188146Sjamie */ 3401188146Sjamieint 3402188146Sjamieprison_check_af(struct ucred *cred, int af) 3403188146Sjamie{ 3404192895Sjamie struct prison *pr; 3405188146Sjamie int error; 3406188146Sjamie 3407188146Sjamie KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3408188146Sjamie 3409192895Sjamie pr = cred->cr_prison; 3410194923Sjamie#ifdef VIMAGE 3411194915Sjamie /* Prisons with their own network stack are not limited. */ 3412200473Sbz if (prison_owns_vnet(cred)) 3413194915Sjamie return (0); 3414194923Sjamie#endif 3415194915Sjamie 3416188146Sjamie error = 0; 3417188146Sjamie switch (af) 3418188146Sjamie { 3419188146Sjamie#ifdef INET 3420188146Sjamie case AF_INET: 3421192895Sjamie if (pr->pr_flags & PR_IP4) 3422192895Sjamie { 3423192895Sjamie mtx_lock(&pr->pr_mtx); 3424192895Sjamie if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL) 3425192895Sjamie error = EAFNOSUPPORT; 3426192895Sjamie mtx_unlock(&pr->pr_mtx); 3427192895Sjamie } 3428188146Sjamie break; 3429188146Sjamie#endif 3430188146Sjamie#ifdef INET6 3431188146Sjamie case AF_INET6: 3432192895Sjamie if (pr->pr_flags & PR_IP6) 3433192895Sjamie { 3434192895Sjamie mtx_lock(&pr->pr_mtx); 3435192895Sjamie if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL) 3436192895Sjamie error = EAFNOSUPPORT; 3437192895Sjamie mtx_unlock(&pr->pr_mtx); 3438192895Sjamie } 3439188146Sjamie break; 3440188146Sjamie#endif 3441188146Sjamie case AF_LOCAL: 3442188146Sjamie case AF_ROUTE: 3443188146Sjamie break; 3444188146Sjamie default: 3445192895Sjamie if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF)) 3446188146Sjamie error = EAFNOSUPPORT; 3447188146Sjamie } 3448188146Sjamie return (error); 3449188146Sjamie} 3450188146Sjamie 3451188146Sjamie/* 3452185435Sbz * Check if given address belongs to the jail referenced by cred (wrapper to 3453185435Sbz * prison_check_ip[46]). 3454185435Sbz * 3455192895Sjamie * Returns 0 if jail doesn't restrict the address family or if address belongs 3456192895Sjamie * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if 3457192895Sjamie * the jail doesn't allow the address family. IPv4 Address passed in in NBO. 3458185435Sbz */ 3459185435Sbzint 346072786Srwatsonprison_if(struct ucred *cred, struct sockaddr *sa) 346146155Sphk{ 3462185435Sbz#ifdef INET 3463114168Smike struct sockaddr_in *sai; 3464185435Sbz#endif 3465185435Sbz#ifdef INET6 3466185435Sbz struct sockaddr_in6 *sai6; 3467185435Sbz#endif 3468188144Sjamie int error; 346946155Sphk 3470185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3471185435Sbz KASSERT(sa != NULL, ("%s: sa is NULL", __func__)); 3472185435Sbz 3473200473Sbz#ifdef VIMAGE 3474200473Sbz if (prison_owns_vnet(cred)) 3475200473Sbz return (0); 3476200473Sbz#endif 3477200473Sbz 3478188144Sjamie error = 0; 3479188144Sjamie switch (sa->sa_family) 3480185435Sbz { 3481185435Sbz#ifdef INET 3482185435Sbz case AF_INET: 3483185435Sbz sai = (struct sockaddr_in *)sa; 3484188144Sjamie error = prison_check_ip4(cred, &sai->sin_addr); 3485185435Sbz break; 3486185435Sbz#endif 3487185435Sbz#ifdef INET6 3488185435Sbz case AF_INET6: 3489185435Sbz sai6 = (struct sockaddr_in6 *)sa; 3490188144Sjamie error = prison_check_ip6(cred, &sai6->sin6_addr); 3491185435Sbz break; 3492185435Sbz#endif 3493185435Sbz default: 3494192895Sjamie if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF)) 3495188144Sjamie error = EAFNOSUPPORT; 3496185435Sbz } 3497188144Sjamie return (error); 349846155Sphk} 349972786Srwatson 350072786Srwatson/* 350172786Srwatson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH. 350272786Srwatson */ 350372786Srwatsonint 3504114168Smikeprison_check(struct ucred *cred1, struct ucred *cred2) 350572786Srwatson{ 350672786Srwatson 3507192895Sjamie return ((cred1->cr_prison == cred2->cr_prison || 3508192895Sjamie prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH); 3509192895Sjamie} 351072786Srwatson 3511192895Sjamie/* 3512192895Sjamie * Return 1 if p2 is a child of p1, otherwise 0. 3513192895Sjamie */ 3514192895Sjamieint 3515192895Sjamieprison_ischild(struct prison *pr1, struct prison *pr2) 3516192895Sjamie{ 3517192895Sjamie 3518192895Sjamie for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent) 3519192895Sjamie if (pr1 == pr2) 3520192895Sjamie return (1); 352172786Srwatson return (0); 352272786Srwatson} 352372786Srwatson 352472786Srwatson/* 352572786Srwatson * Return 1 if the passed credential is in a jail, otherwise 0. 352672786Srwatson */ 352772786Srwatsonint 3528114168Smikejailed(struct ucred *cred) 352972786Srwatson{ 353072786Srwatson 3531192895Sjamie return (cred->cr_prison != &prison0); 353272786Srwatson} 353391384Srobert 353491384Srobert/* 3535200473Sbz * Return 1 if the passed credential is in a jail and that jail does not 3536200473Sbz * have its own virtual network stack, otherwise 0. 3537200473Sbz */ 3538200473Sbzint 3539200473Sbzjailed_without_vnet(struct ucred *cred) 3540200473Sbz{ 3541200473Sbz 3542200473Sbz if (!jailed(cred)) 3543200473Sbz return (0); 3544200473Sbz#ifdef VIMAGE 3545200473Sbz if (prison_owns_vnet(cred)) 3546200473Sbz return (0); 3547200473Sbz#endif 3548200473Sbz 3549200473Sbz return (1); 3550200473Sbz} 3551200473Sbz 3552200473Sbz/* 3553194090Sjamie * Return the correct hostname (domainname, et al) for the passed credential. 355491384Srobert */ 355591391Srobertvoid 3556114168Smikegetcredhostname(struct ucred *cred, char *buf, size_t size) 355791384Srobert{ 3558193066Sjamie struct prison *pr; 355991384Srobert 3560194090Sjamie /* 3561194090Sjamie * A NULL credential can be used to shortcut to the physical 3562194090Sjamie * system's hostname. 3563194090Sjamie */ 3564193066Sjamie pr = (cred != NULL) ? cred->cr_prison : &prison0; 3565193066Sjamie mtx_lock(&pr->pr_mtx); 3566194118Sjamie strlcpy(buf, pr->pr_hostname, size); 3567193066Sjamie mtx_unlock(&pr->pr_mtx); 356891384Srobert} 3569113275Smike 3570194090Sjamievoid 3571194090Sjamiegetcreddomainname(struct ucred *cred, char *buf, size_t size) 3572194090Sjamie{ 3573194090Sjamie 3574194090Sjamie mtx_lock(&cred->cr_prison->pr_mtx); 3575194118Sjamie strlcpy(buf, cred->cr_prison->pr_domainname, size); 3576194090Sjamie mtx_unlock(&cred->cr_prison->pr_mtx); 3577194090Sjamie} 3578194090Sjamie 3579194090Sjamievoid 3580194090Sjamiegetcredhostuuid(struct ucred *cred, char *buf, size_t size) 3581194090Sjamie{ 3582194090Sjamie 3583194090Sjamie mtx_lock(&cred->cr_prison->pr_mtx); 3584194118Sjamie strlcpy(buf, cred->cr_prison->pr_hostuuid, size); 3585194090Sjamie mtx_unlock(&cred->cr_prison->pr_mtx); 3586194090Sjamie} 3587194090Sjamie 3588194090Sjamievoid 3589194090Sjamiegetcredhostid(struct ucred *cred, unsigned long *hostid) 3590194090Sjamie{ 3591194090Sjamie 3592194090Sjamie mtx_lock(&cred->cr_prison->pr_mtx); 3593194090Sjamie *hostid = cred->cr_prison->pr_hostid; 3594194090Sjamie mtx_unlock(&cred->cr_prison->pr_mtx); 3595194090Sjamie} 3596194090Sjamie 3597196176Sbz#ifdef VIMAGE 3598125804Srwatson/* 3599196176Sbz * Determine whether the prison represented by cred owns 3600196176Sbz * its vnet rather than having it inherited. 3601196176Sbz * 3602196176Sbz * Returns 1 in case the prison owns the vnet, 0 otherwise. 3603196176Sbz */ 3604196176Sbzint 3605196176Sbzprison_owns_vnet(struct ucred *cred) 3606196176Sbz{ 3607196176Sbz 3608196176Sbz /* 3609196176Sbz * vnets cannot be added/removed after jail creation, 3610196176Sbz * so no need to lock here. 3611196176Sbz */ 3612196176Sbz return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0); 3613196176Sbz} 3614196176Sbz#endif 3615196176Sbz 3616196176Sbz/* 3617147185Spjd * Determine whether the subject represented by cred can "see" 3618147185Spjd * status of a mount point. 3619147185Spjd * Returns: 0 for permitted, ENOENT otherwise. 3620147185Spjd * XXX: This function should be called cr_canseemount() and should be 3621147185Spjd * placed in kern_prot.c. 3622125804Srwatson */ 3623125804Srwatsonint 3624147185Spjdprison_canseemount(struct ucred *cred, struct mount *mp) 3625125804Srwatson{ 3626147185Spjd struct prison *pr; 3627147185Spjd struct statfs *sp; 3628147185Spjd size_t len; 3629125804Srwatson 3630192895Sjamie pr = cred->cr_prison; 3631192895Sjamie if (pr->pr_enforce_statfs == 0) 3632147185Spjd return (0); 3633147185Spjd if (pr->pr_root->v_mount == mp) 3634147185Spjd return (0); 3635192895Sjamie if (pr->pr_enforce_statfs == 2) 3636147185Spjd return (ENOENT); 3637147185Spjd /* 3638147185Spjd * If jail's chroot directory is set to "/" we should be able to see 3639147185Spjd * all mount-points from inside a jail. 3640147185Spjd * This is ugly check, but this is the only situation when jail's 3641147185Spjd * directory ends with '/'. 3642147185Spjd */ 3643147185Spjd if (strcmp(pr->pr_path, "/") == 0) 3644147185Spjd return (0); 3645147185Spjd len = strlen(pr->pr_path); 3646147185Spjd sp = &mp->mnt_stat; 3647147185Spjd if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0) 3648147185Spjd return (ENOENT); 3649147185Spjd /* 3650147185Spjd * Be sure that we don't have situation where jail's root directory 3651147185Spjd * is "/some/path" and mount point is "/some/pathpath". 3652147185Spjd */ 3653147185Spjd if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/') 3654147185Spjd return (ENOENT); 3655147185Spjd return (0); 3656147185Spjd} 3657147185Spjd 3658147185Spjdvoid 3659147185Spjdprison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp) 3660147185Spjd{ 3661147185Spjd char jpath[MAXPATHLEN]; 3662147185Spjd struct prison *pr; 3663147185Spjd size_t len; 3664147185Spjd 3665192895Sjamie pr = cred->cr_prison; 3666192895Sjamie if (pr->pr_enforce_statfs == 0) 3667147185Spjd return; 3668147185Spjd if (prison_canseemount(cred, mp) != 0) { 3669147185Spjd bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3670147185Spjd strlcpy(sp->f_mntonname, "[restricted]", 3671147185Spjd sizeof(sp->f_mntonname)); 3672147185Spjd return; 3673125804Srwatson } 3674147185Spjd if (pr->pr_root->v_mount == mp) { 3675147185Spjd /* 3676147185Spjd * Clear current buffer data, so we are sure nothing from 3677147185Spjd * the valid path left there. 3678147185Spjd */ 3679147185Spjd bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3680147185Spjd *sp->f_mntonname = '/'; 3681147185Spjd return; 3682147185Spjd } 3683147185Spjd /* 3684147185Spjd * If jail's chroot directory is set to "/" we should be able to see 3685147185Spjd * all mount-points from inside a jail. 3686147185Spjd */ 3687147185Spjd if (strcmp(pr->pr_path, "/") == 0) 3688147185Spjd return; 3689147185Spjd len = strlen(pr->pr_path); 3690147185Spjd strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath)); 3691147185Spjd /* 3692147185Spjd * Clear current buffer data, so we are sure nothing from 3693147185Spjd * the valid path left there. 3694147185Spjd */ 3695147185Spjd bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3696147185Spjd if (*jpath == '\0') { 3697147185Spjd /* Should never happen. */ 3698147185Spjd *sp->f_mntonname = '/'; 3699147185Spjd } else { 3700147185Spjd strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname)); 3701147185Spjd } 3702125804Srwatson} 3703125804Srwatson 3704164032Srwatson/* 3705164032Srwatson * Check with permission for a specific privilege is granted within jail. We 3706164032Srwatson * have a specific list of accepted privileges; the rest are denied. 3707164032Srwatson */ 3708164032Srwatsonint 3709164032Srwatsonprison_priv_check(struct ucred *cred, int priv) 3710164032Srwatson{ 3711164032Srwatson 3712164032Srwatson if (!jailed(cred)) 3713164032Srwatson return (0); 3714164032Srwatson 3715194915Sjamie#ifdef VIMAGE 3716194915Sjamie /* 3717194915Sjamie * Privileges specific to prisons with a virtual network stack. 3718194915Sjamie * There might be a duplicate entry here in case the privilege 3719194915Sjamie * is only granted conditionally in the legacy jail case. 3720194915Sjamie */ 3721164032Srwatson switch (priv) { 3722194915Sjamie#ifdef notyet 3723194915Sjamie /* 3724194915Sjamie * NFS-specific privileges. 3725194915Sjamie */ 3726194915Sjamie case PRIV_NFS_DAEMON: 3727194915Sjamie case PRIV_NFS_LOCKD: 3728194915Sjamie#endif 3729194915Sjamie /* 3730194915Sjamie * Network stack privileges. 3731194915Sjamie */ 3732194915Sjamie case PRIV_NET_BRIDGE: 3733194915Sjamie case PRIV_NET_GRE: 3734194915Sjamie case PRIV_NET_BPF: 3735194915Sjamie case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */ 3736194915Sjamie case PRIV_NET_ROUTE: 3737194915Sjamie case PRIV_NET_TAP: 3738194915Sjamie case PRIV_NET_SETIFMTU: 3739194915Sjamie case PRIV_NET_SETIFFLAGS: 3740194915Sjamie case PRIV_NET_SETIFCAP: 3741203052Sdelphij case PRIV_NET_SETIFDESCR: 3742194915Sjamie case PRIV_NET_SETIFNAME : 3743194915Sjamie case PRIV_NET_SETIFMETRIC: 3744194915Sjamie case PRIV_NET_SETIFPHYS: 3745194915Sjamie case PRIV_NET_SETIFMAC: 3746194915Sjamie case PRIV_NET_ADDMULTI: 3747194915Sjamie case PRIV_NET_DELMULTI: 3748194915Sjamie case PRIV_NET_HWIOCTL: 3749194915Sjamie case PRIV_NET_SETLLADDR: 3750194915Sjamie case PRIV_NET_ADDIFGROUP: 3751194915Sjamie case PRIV_NET_DELIFGROUP: 3752194915Sjamie case PRIV_NET_IFCREATE: 3753194915Sjamie case PRIV_NET_IFDESTROY: 3754194915Sjamie case PRIV_NET_ADDIFADDR: 3755194915Sjamie case PRIV_NET_DELIFADDR: 3756194915Sjamie case PRIV_NET_LAGG: 3757194915Sjamie case PRIV_NET_GIF: 3758194915Sjamie case PRIV_NET_SETIFVNET: 3759223735Sbz case PRIV_NET_SETIFFIB: 3760164032Srwatson 3761164032Srwatson /* 3762194915Sjamie * 802.11-related privileges. 3763194915Sjamie */ 3764194915Sjamie case PRIV_NET80211_GETKEY: 3765194915Sjamie#ifdef notyet 3766194915Sjamie case PRIV_NET80211_MANAGE: /* XXX-BZ discuss with sam@ */ 3767194915Sjamie#endif 3768194915Sjamie 3769194915Sjamie#ifdef notyet 3770194915Sjamie /* 3771194915Sjamie * AppleTalk privileges. 3772194915Sjamie */ 3773194915Sjamie case PRIV_NETATALK_RESERVEDPORT: 3774194915Sjamie 3775194915Sjamie /* 3776194915Sjamie * ATM privileges. 3777194915Sjamie */ 3778194915Sjamie case PRIV_NETATM_CFG: 3779194915Sjamie case PRIV_NETATM_ADD: 3780194915Sjamie case PRIV_NETATM_DEL: 3781194915Sjamie case PRIV_NETATM_SET: 3782194915Sjamie 3783194915Sjamie /* 3784194915Sjamie * Bluetooth privileges. 3785194915Sjamie */ 3786194915Sjamie case PRIV_NETBLUETOOTH_RAW: 3787194915Sjamie#endif 3788194915Sjamie 3789194915Sjamie /* 3790194915Sjamie * Netgraph and netgraph module privileges. 3791194915Sjamie */ 3792194915Sjamie case PRIV_NETGRAPH_CONTROL: 3793194915Sjamie#ifdef notyet 3794194915Sjamie case PRIV_NETGRAPH_TTY: 3795194915Sjamie#endif 3796194915Sjamie 3797194915Sjamie /* 3798194915Sjamie * IPv4 and IPv6 privileges. 3799194915Sjamie */ 3800194915Sjamie case PRIV_NETINET_IPFW: 3801194915Sjamie case PRIV_NETINET_DIVERT: 3802194915Sjamie case PRIV_NETINET_PF: 3803194915Sjamie case PRIV_NETINET_DUMMYNET: 3804194915Sjamie case PRIV_NETINET_CARP: 3805194915Sjamie case PRIV_NETINET_MROUTE: 3806194915Sjamie case PRIV_NETINET_RAW: 3807194915Sjamie case PRIV_NETINET_ADDRCTRL6: 3808194915Sjamie case PRIV_NETINET_ND6: 3809194915Sjamie case PRIV_NETINET_SCOPE6: 3810194915Sjamie case PRIV_NETINET_ALIFETIME6: 3811194915Sjamie case PRIV_NETINET_IPSEC: 3812194915Sjamie case PRIV_NETINET_BINDANY: 3813194915Sjamie 3814194915Sjamie#ifdef notyet 3815194915Sjamie /* 3816194915Sjamie * IPX/SPX privileges. 3817194915Sjamie */ 3818194915Sjamie case PRIV_NETIPX_RESERVEDPORT: 3819194915Sjamie case PRIV_NETIPX_RAW: 3820194915Sjamie 3821194915Sjamie /* 3822194915Sjamie * NCP privileges. 3823194915Sjamie */ 3824194915Sjamie case PRIV_NETNCP: 3825194915Sjamie 3826194915Sjamie /* 3827194915Sjamie * SMB privileges. 3828194915Sjamie */ 3829194915Sjamie case PRIV_NETSMB: 3830194915Sjamie#endif 3831194915Sjamie 3832194915Sjamie /* 3833194915Sjamie * No default: or deny here. 3834194915Sjamie * In case of no permit fall through to next switch(). 3835194915Sjamie */ 3836194915Sjamie if (cred->cr_prison->pr_flags & PR_VNET) 3837194915Sjamie return (0); 3838194915Sjamie } 3839194915Sjamie#endif /* VIMAGE */ 3840194915Sjamie 3841194915Sjamie switch (priv) { 3842194915Sjamie 3843194915Sjamie /* 3844164032Srwatson * Allow ktrace privileges for root in jail. 3845164032Srwatson */ 3846164032Srwatson case PRIV_KTRACE: 3847164032Srwatson 3848166827Srwatson#if 0 3849164032Srwatson /* 3850164032Srwatson * Allow jailed processes to configure audit identity and 3851164032Srwatson * submit audit records (login, etc). In the future we may 3852164032Srwatson * want to further refine the relationship between audit and 3853164032Srwatson * jail. 3854164032Srwatson */ 3855164032Srwatson case PRIV_AUDIT_GETAUDIT: 3856164032Srwatson case PRIV_AUDIT_SETAUDIT: 3857164032Srwatson case PRIV_AUDIT_SUBMIT: 3858166827Srwatson#endif 3859164032Srwatson 3860164032Srwatson /* 3861164032Srwatson * Allow jailed processes to manipulate process UNIX 3862164032Srwatson * credentials in any way they see fit. 3863164032Srwatson */ 3864164032Srwatson case PRIV_CRED_SETUID: 3865164032Srwatson case PRIV_CRED_SETEUID: 3866164032Srwatson case PRIV_CRED_SETGID: 3867164032Srwatson case PRIV_CRED_SETEGID: 3868164032Srwatson case PRIV_CRED_SETGROUPS: 3869164032Srwatson case PRIV_CRED_SETREUID: 3870164032Srwatson case PRIV_CRED_SETREGID: 3871164032Srwatson case PRIV_CRED_SETRESUID: 3872164032Srwatson case PRIV_CRED_SETRESGID: 3873164032Srwatson 3874164032Srwatson /* 3875164032Srwatson * Jail implements visibility constraints already, so allow 3876164032Srwatson * jailed root to override uid/gid-based constraints. 3877164032Srwatson */ 3878164032Srwatson case PRIV_SEEOTHERGIDS: 3879164032Srwatson case PRIV_SEEOTHERUIDS: 3880164032Srwatson 3881164032Srwatson /* 3882164032Srwatson * Jail implements inter-process debugging limits already, so 3883164032Srwatson * allow jailed root various debugging privileges. 3884164032Srwatson */ 3885164032Srwatson case PRIV_DEBUG_DIFFCRED: 3886164032Srwatson case PRIV_DEBUG_SUGID: 3887164032Srwatson case PRIV_DEBUG_UNPRIV: 3888164032Srwatson 3889164032Srwatson /* 3890164032Srwatson * Allow jail to set various resource limits and login 3891164032Srwatson * properties, and for now, exceed process resource limits. 3892164032Srwatson */ 3893164032Srwatson case PRIV_PROC_LIMIT: 3894164032Srwatson case PRIV_PROC_SETLOGIN: 3895164032Srwatson case PRIV_PROC_SETRLIMIT: 3896164032Srwatson 3897164032Srwatson /* 3898164032Srwatson * System V and POSIX IPC privileges are granted in jail. 3899164032Srwatson */ 3900164032Srwatson case PRIV_IPC_READ: 3901164032Srwatson case PRIV_IPC_WRITE: 3902164032Srwatson case PRIV_IPC_ADMIN: 3903164032Srwatson case PRIV_IPC_MSGSIZE: 3904164032Srwatson case PRIV_MQ_ADMIN: 3905164032Srwatson 3906164032Srwatson /* 3907192895Sjamie * Jail operations within a jail work on child jails. 3908192895Sjamie */ 3909192895Sjamie case PRIV_JAIL_ATTACH: 3910192895Sjamie case PRIV_JAIL_SET: 3911192895Sjamie case PRIV_JAIL_REMOVE: 3912192895Sjamie 3913192895Sjamie /* 3914164032Srwatson * Jail implements its own inter-process limits, so allow 3915164032Srwatson * root processes in jail to change scheduling on other 3916164032Srwatson * processes in the same jail. Likewise for signalling. 3917164032Srwatson */ 3918164032Srwatson case PRIV_SCHED_DIFFCRED: 3919185435Sbz case PRIV_SCHED_CPUSET: 3920164032Srwatson case PRIV_SIGNAL_DIFFCRED: 3921164032Srwatson case PRIV_SIGNAL_SUGID: 3922164032Srwatson 3923164032Srwatson /* 3924164032Srwatson * Allow jailed processes to write to sysctls marked as jail 3925164032Srwatson * writable. 3926164032Srwatson */ 3927164032Srwatson case PRIV_SYSCTL_WRITEJAIL: 3928164032Srwatson 3929164032Srwatson /* 3930164032Srwatson * Allow root in jail to manage a variety of quota 3931166831Srwatson * properties. These should likely be conditional on a 3932166831Srwatson * configuration option. 3933164032Srwatson */ 3934166832Srwatson case PRIV_VFS_GETQUOTA: 3935166832Srwatson case PRIV_VFS_SETQUOTA: 3936164032Srwatson 3937164032Srwatson /* 3938164032Srwatson * Since Jail relies on chroot() to implement file system 3939164032Srwatson * protections, grant many VFS privileges to root in jail. 3940164032Srwatson * Be careful to exclude mount-related and NFS-related 3941164032Srwatson * privileges. 3942164032Srwatson */ 3943164032Srwatson case PRIV_VFS_READ: 3944164032Srwatson case PRIV_VFS_WRITE: 3945164032Srwatson case PRIV_VFS_ADMIN: 3946164032Srwatson case PRIV_VFS_EXEC: 3947164032Srwatson case PRIV_VFS_LOOKUP: 3948164032Srwatson case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */ 3949164032Srwatson case PRIV_VFS_CHFLAGS_DEV: 3950164032Srwatson case PRIV_VFS_CHOWN: 3951164032Srwatson case PRIV_VFS_CHROOT: 3952167152Spjd case PRIV_VFS_RETAINSUGID: 3953164032Srwatson case PRIV_VFS_FCHROOT: 3954164032Srwatson case PRIV_VFS_LINK: 3955164032Srwatson case PRIV_VFS_SETGID: 3956172860Srwatson case PRIV_VFS_STAT: 3957164032Srwatson case PRIV_VFS_STICKYFILE: 3958255316Sjamie 3959255316Sjamie /* 3960255316Sjamie * As in the non-jail case, non-root users are expected to be 3961255316Sjamie * able to read kernel/phyiscal memory (provided /dev/[k]mem 3962255316Sjamie * exists in the jail and they have permission to access it). 3963255316Sjamie */ 3964255316Sjamie case PRIV_KMEM_READ: 3965164032Srwatson return (0); 3966164032Srwatson 3967164032Srwatson /* 3968164032Srwatson * Depending on the global setting, allow privilege of 3969164032Srwatson * setting system flags. 3970164032Srwatson */ 3971164032Srwatson case PRIV_VFS_SYSFLAGS: 3972192895Sjamie if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS) 3973164032Srwatson return (0); 3974164032Srwatson else 3975164032Srwatson return (EPERM); 3976164032Srwatson 3977164032Srwatson /* 3978168396Spjd * Depending on the global setting, allow privilege of 3979168396Spjd * mounting/unmounting file systems. 3980168396Spjd */ 3981168396Spjd case PRIV_VFS_MOUNT: 3982168396Spjd case PRIV_VFS_UNMOUNT: 3983168396Spjd case PRIV_VFS_MOUNT_NONUSER: 3984168699Spjd case PRIV_VFS_MOUNT_OWNER: 3985224615Smm if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT && 3986224615Smm cred->cr_prison->pr_enforce_statfs < 2) 3987168396Spjd return (0); 3988168396Spjd else 3989168396Spjd return (EPERM); 3990168396Spjd 3991168396Spjd /* 3992168591Srwatson * Allow jailed root to bind reserved ports and reuse in-use 3993168591Srwatson * ports. 3994164032Srwatson */ 3995164032Srwatson case PRIV_NETINET_RESERVEDPORT: 3996168591Srwatson case PRIV_NETINET_REUSEPORT: 3997164032Srwatson return (0); 3998164032Srwatson 3999164032Srwatson /* 4000175630Sbz * Allow jailed root to set certian IPv4/6 (option) headers. 4001175630Sbz */ 4002175630Sbz case PRIV_NETINET_SETHDROPTS: 4003175630Sbz return (0); 4004175630Sbz 4005175630Sbz /* 4006164032Srwatson * Conditionally allow creating raw sockets in jail. 4007164032Srwatson */ 4008164032Srwatson case PRIV_NETINET_RAW: 4009192895Sjamie if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS) 4010164032Srwatson return (0); 4011164032Srwatson else 4012164032Srwatson return (EPERM); 4013164032Srwatson 4014164032Srwatson /* 4015164032Srwatson * Since jail implements its own visibility limits on netstat 4016164032Srwatson * sysctls, allow getcred. This allows identd to work in 4017164032Srwatson * jail. 4018164032Srwatson */ 4019164032Srwatson case PRIV_NETINET_GETCRED: 4020164032Srwatson return (0); 4021164032Srwatson 4022219304Strasz /* 4023219304Strasz * Allow jailed root to set loginclass. 4024219304Strasz */ 4025219304Strasz case PRIV_PROC_SETLOGINCLASS: 4026219304Strasz return (0); 4027219304Strasz 4028164032Srwatson default: 4029164032Srwatson /* 4030164032Srwatson * In all remaining cases, deny the privilege request. This 4031164032Srwatson * includes almost all network privileges, many system 4032164032Srwatson * configuration privileges. 4033164032Srwatson */ 4034164032Srwatson return (EPERM); 4035164032Srwatson } 4036164032Srwatson} 4037164032Srwatson 4038192895Sjamie/* 4039192895Sjamie * Return the part of pr2's name that is relative to pr1, or the whole name 4040192895Sjamie * if it does not directly follow. 4041192895Sjamie */ 4042192895Sjamie 4043192895Sjamiechar * 4044192895Sjamieprison_name(struct prison *pr1, struct prison *pr2) 4045192895Sjamie{ 4046192895Sjamie char *name; 4047192895Sjamie 4048192895Sjamie /* Jails see themselves as "0" (if they see themselves at all). */ 4049192895Sjamie if (pr1 == pr2) 4050192895Sjamie return "0"; 4051192895Sjamie name = pr2->pr_name; 4052192895Sjamie if (prison_ischild(pr1, pr2)) { 4053192895Sjamie /* 4054192895Sjamie * pr1 isn't locked (and allprison_lock may not be either) 4055192895Sjamie * so its length can't be counted on. But the number of dots 4056192895Sjamie * can be counted on - and counted. 4057192895Sjamie */ 4058192895Sjamie for (; pr1 != &prison0; pr1 = pr1->pr_parent) 4059192895Sjamie name = strchr(name, '.') + 1; 4060192895Sjamie } 4061192895Sjamie return (name); 4062192895Sjamie} 4063192895Sjamie 4064192895Sjamie/* 4065192895Sjamie * Return the part of pr2's path that is relative to pr1, or the whole path 4066192895Sjamie * if it does not directly follow. 4067192895Sjamie */ 4068192895Sjamiestatic char * 4069192895Sjamieprison_path(struct prison *pr1, struct prison *pr2) 4070192895Sjamie{ 4071192895Sjamie char *path1, *path2; 4072192895Sjamie int len1; 4073192895Sjamie 4074192895Sjamie path1 = pr1->pr_path; 4075192895Sjamie path2 = pr2->pr_path; 4076192895Sjamie if (!strcmp(path1, "/")) 4077192895Sjamie return (path2); 4078192895Sjamie len1 = strlen(path1); 4079192895Sjamie if (strncmp(path1, path2, len1)) 4080192895Sjamie return (path2); 4081192895Sjamie if (path2[len1] == '\0') 4082192895Sjamie return "/"; 4083192895Sjamie if (path2[len1] == '/') 4084192895Sjamie return (path2 + len1); 4085192895Sjamie return (path2); 4086192895Sjamie} 4087192895Sjamie 4088192895Sjamie 4089192895Sjamie/* 4090192895Sjamie * Jail-related sysctls. 4091192895Sjamie */ 4092227309Sedstatic SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0, 4093192895Sjamie "Jails"); 4094192895Sjamie 4095113275Smikestatic int 4096113275Smikesysctl_jail_list(SYSCTL_HANDLER_ARGS) 4097113275Smike{ 4098191673Sjamie struct xprison *xp; 4099192895Sjamie struct prison *pr, *cpr; 4100191673Sjamie#ifdef INET 4101191673Sjamie struct in_addr *ip4 = NULL; 4102191673Sjamie int ip4s = 0; 4103191673Sjamie#endif 4104191673Sjamie#ifdef INET6 4105208803Scperciva struct in6_addr *ip6 = NULL; 4106191673Sjamie int ip6s = 0; 4107191673Sjamie#endif 4108192895Sjamie int descend, error; 4109113275Smike 4110191673Sjamie xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK); 4111192895Sjamie pr = req->td->td_ucred->cr_prison; 4112191673Sjamie error = 0; 4113168401Spjd sx_slock(&allprison_lock); 4114192895Sjamie FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 4115192895Sjamie#if defined(INET) || defined(INET6) 4116191673Sjamie again: 4117192895Sjamie#endif 4118192895Sjamie mtx_lock(&cpr->pr_mtx); 4119185435Sbz#ifdef INET 4120192895Sjamie if (cpr->pr_ip4s > 0) { 4121192895Sjamie if (ip4s < cpr->pr_ip4s) { 4122192895Sjamie ip4s = cpr->pr_ip4s; 4123192895Sjamie mtx_unlock(&cpr->pr_mtx); 4124191673Sjamie ip4 = realloc(ip4, ip4s * 4125191673Sjamie sizeof(struct in_addr), M_TEMP, M_WAITOK); 4126191673Sjamie goto again; 4127191673Sjamie } 4128192895Sjamie bcopy(cpr->pr_ip4, ip4, 4129192895Sjamie cpr->pr_ip4s * sizeof(struct in_addr)); 4130191673Sjamie } 4131185435Sbz#endif 4132185435Sbz#ifdef INET6 4133192895Sjamie if (cpr->pr_ip6s > 0) { 4134192895Sjamie if (ip6s < cpr->pr_ip6s) { 4135192895Sjamie ip6s = cpr->pr_ip6s; 4136192895Sjamie mtx_unlock(&cpr->pr_mtx); 4137191673Sjamie ip6 = realloc(ip6, ip6s * 4138191673Sjamie sizeof(struct in6_addr), M_TEMP, M_WAITOK); 4139191673Sjamie goto again; 4140191673Sjamie } 4141192895Sjamie bcopy(cpr->pr_ip6, ip6, 4142192895Sjamie cpr->pr_ip6s * sizeof(struct in6_addr)); 4143191673Sjamie } 4144185435Sbz#endif 4145192895Sjamie if (cpr->pr_ref == 0) { 4146192895Sjamie mtx_unlock(&cpr->pr_mtx); 4147191673Sjamie continue; 4148191673Sjamie } 4149191673Sjamie bzero(xp, sizeof(*xp)); 4150113275Smike xp->pr_version = XPRISON_VERSION; 4151192895Sjamie xp->pr_id = cpr->pr_id; 4152192895Sjamie xp->pr_state = cpr->pr_uref > 0 4153191673Sjamie ? PRISON_STATE_ALIVE : PRISON_STATE_DYING; 4154192895Sjamie strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path)); 4155194118Sjamie strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host)); 4156192895Sjamie strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name)); 4157185435Sbz#ifdef INET 4158192895Sjamie xp->pr_ip4s = cpr->pr_ip4s; 4159185435Sbz#endif 4160185435Sbz#ifdef INET6 4161192895Sjamie xp->pr_ip6s = cpr->pr_ip6s; 4162185435Sbz#endif 4163192895Sjamie mtx_unlock(&cpr->pr_mtx); 4164191673Sjamie error = SYSCTL_OUT(req, xp, sizeof(*xp)); 4165191673Sjamie if (error) 4166191673Sjamie break; 4167185435Sbz#ifdef INET 4168191673Sjamie if (xp->pr_ip4s > 0) { 4169191673Sjamie error = SYSCTL_OUT(req, ip4, 4170191673Sjamie xp->pr_ip4s * sizeof(struct in_addr)); 4171191673Sjamie if (error) 4172191673Sjamie break; 4173185435Sbz } 4174185435Sbz#endif 4175185435Sbz#ifdef INET6 4176191673Sjamie if (xp->pr_ip6s > 0) { 4177191673Sjamie error = SYSCTL_OUT(req, ip6, 4178191673Sjamie xp->pr_ip6s * sizeof(struct in6_addr)); 4179191673Sjamie if (error) 4180191673Sjamie break; 4181185435Sbz } 4182185435Sbz#endif 4183113275Smike } 4184168401Spjd sx_sunlock(&allprison_lock); 4185191673Sjamie free(xp, M_TEMP); 4186191673Sjamie#ifdef INET 4187191673Sjamie free(ip4, M_TEMP); 4188191673Sjamie#endif 4189191673Sjamie#ifdef INET6 4190191673Sjamie free(ip6, M_TEMP); 4191191673Sjamie#endif 4192167354Spjd return (error); 4193113275Smike} 4194113275Smike 4195187864SedSYSCTL_OID(_security_jail, OID_AUTO, list, 4196187864Sed CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4197187864Sed sysctl_jail_list, "S", "List of active jails"); 4198126004Spjd 4199126004Spjdstatic int 4200126004Spjdsysctl_jail_jailed(SYSCTL_HANDLER_ARGS) 4201126004Spjd{ 4202126004Spjd int error, injail; 4203126004Spjd 4204126004Spjd injail = jailed(req->td->td_ucred); 4205126004Spjd error = SYSCTL_OUT(req, &injail, sizeof(injail)); 4206126004Spjd 4207126004Spjd return (error); 4208126004Spjd} 4209192895Sjamie 4210187864SedSYSCTL_PROC(_security_jail, OID_AUTO, jailed, 4211187864Sed CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4212187864Sed sysctl_jail_jailed, "I", "Process in jail?"); 4213185435Sbz 4214250804Sjamiestatic int 4215250804Sjamiesysctl_jail_vnet(SYSCTL_HANDLER_ARGS) 4216250804Sjamie{ 4217250804Sjamie int error, havevnet; 4218250804Sjamie#ifdef VIMAGE 4219250804Sjamie struct ucred *cred = req->td->td_ucred; 4220250804Sjamie 4221250804Sjamie havevnet = jailed(cred) && prison_owns_vnet(cred); 4222250804Sjamie#else 4223250804Sjamie havevnet = 0; 4224250804Sjamie#endif 4225250804Sjamie error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet)); 4226250804Sjamie 4227250804Sjamie return (error); 4228250804Sjamie} 4229250804Sjamie 4230250804SjamieSYSCTL_PROC(_security_jail, OID_AUTO, vnet, 4231250804Sjamie CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4232250804Sjamie sysctl_jail_vnet, "I", "Jail owns VNET?"); 4233250804Sjamie 4234192895Sjamie#if defined(INET) || defined(INET6) 4235193865SjamieSYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW, 4236192895Sjamie &jail_max_af_ips, 0, 4237192895Sjamie "Number of IP addresses a jail may have at most per address family"); 4238192895Sjamie#endif 4239192895Sjamie 4240192895Sjamie/* 4241192895Sjamie * Default parameters for jail(2) compatability. For historical reasons, 4242192895Sjamie * the sysctl names have varying similarity to the parameter names. Prisons 4243192895Sjamie * just see their own parameters, and can't change them. 4244192895Sjamie */ 4245192895Sjamiestatic int 4246192895Sjamiesysctl_jail_default_allow(SYSCTL_HANDLER_ARGS) 4247192895Sjamie{ 4248192895Sjamie struct prison *pr; 4249192895Sjamie int allow, error, i; 4250192895Sjamie 4251192895Sjamie pr = req->td->td_ucred->cr_prison; 4252192895Sjamie allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow; 4253192895Sjamie 4254192895Sjamie /* Get the current flag value, and convert it to a boolean. */ 4255192895Sjamie i = (allow & arg2) ? 1 : 0; 4256192895Sjamie if (arg1 != NULL) 4257192895Sjamie i = !i; 4258192895Sjamie error = sysctl_handle_int(oidp, &i, 0, req); 4259192895Sjamie if (error || !req->newptr) 4260192895Sjamie return (error); 4261192895Sjamie i = i ? arg2 : 0; 4262192895Sjamie if (arg1 != NULL) 4263192895Sjamie i ^= arg2; 4264192895Sjamie /* 4265192895Sjamie * The sysctls don't have CTLFLAGS_PRISON, so assume prison0 4266192895Sjamie * for writing. 4267192895Sjamie */ 4268192895Sjamie mtx_lock(&prison0.pr_mtx); 4269192895Sjamie jail_default_allow = (jail_default_allow & ~arg2) | i; 4270192895Sjamie mtx_unlock(&prison0.pr_mtx); 4271192895Sjamie return (0); 4272192895Sjamie} 4273192895Sjamie 4274192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed, 4275192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4276192895Sjamie NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I", 4277192895Sjamie "Processes in jail can set their hostnames"); 4278192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only, 4279192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4280192895Sjamie (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I", 4281192895Sjamie "Processes in jail are limited to creating UNIX/IP/route sockets only"); 4282192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed, 4283192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4284192895Sjamie NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I", 4285192895Sjamie "Processes in jail can use System V IPC primitives"); 4286192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets, 4287192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4288192895Sjamie NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I", 4289192895Sjamie "Prison root can create raw sockets"); 4290192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed, 4291192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4292192895Sjamie NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I", 4293192895Sjamie "Processes in jail can alter system file flags"); 4294192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed, 4295192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4296192895Sjamie NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I", 4297192895Sjamie "Processes in jail can mount/unmount jail-friendly file systems"); 4298232059SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_devfs_allowed, 4299232059Smm CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4300232059Smm NULL, PR_ALLOW_MOUNT_DEVFS, sysctl_jail_default_allow, "I", 4301232186Smm "Processes in jail can mount the devfs file system"); 4302277985SjamieSYSCTL_PROC(_security_jail, OID_AUTO, mount_fdescfs_allowed, 4303277985Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4304277985Sjamie NULL, PR_ALLOW_MOUNT_FDESCFS, sysctl_jail_default_allow, "I", 4305277985Sjamie "Processes in jail can mount the fdescfs file system"); 4306232059SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_nullfs_allowed, 4307232059Smm CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4308232059Smm NULL, PR_ALLOW_MOUNT_NULLFS, sysctl_jail_default_allow, "I", 4309232186Smm "Processes in jail can mount the nullfs file system"); 4310232278SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_procfs_allowed, 4311232278Smm CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4312232278Smm NULL, PR_ALLOW_MOUNT_PROCFS, sysctl_jail_default_allow, "I", 4313232278Smm "Processes in jail can mount the procfs file system"); 4314254741SdelphijSYSCTL_PROC(_security_jail, OID_AUTO, mount_tmpfs_allowed, 4315254741Sdelphij CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4316254741Sdelphij NULL, PR_ALLOW_MOUNT_TMPFS, sysctl_jail_default_allow, "I", 4317254741Sdelphij "Processes in jail can mount the tmpfs file system"); 4318232186SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_zfs_allowed, 4319232186Smm CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4320232186Smm NULL, PR_ALLOW_MOUNT_ZFS, sysctl_jail_default_allow, "I", 4321232186Smm "Processes in jail can mount the zfs file system"); 4322192895Sjamie 4323192895Sjamiestatic int 4324192895Sjamiesysctl_jail_default_level(SYSCTL_HANDLER_ARGS) 4325192895Sjamie{ 4326192895Sjamie struct prison *pr; 4327192895Sjamie int level, error; 4328192895Sjamie 4329192895Sjamie pr = req->td->td_ucred->cr_prison; 4330192895Sjamie level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2); 4331192895Sjamie error = sysctl_handle_int(oidp, &level, 0, req); 4332192895Sjamie if (error || !req->newptr) 4333192895Sjamie return (error); 4334192895Sjamie *(int *)arg1 = level; 4335192895Sjamie return (0); 4336192895Sjamie} 4337192895Sjamie 4338192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs, 4339192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4340192895Sjamie &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs), 4341192895Sjamie sysctl_jail_default_level, "I", 4342192895Sjamie "Processes in jail cannot see all mounted file systems"); 4343192895Sjamie 4344231267SmmSYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset, 4345231267Smm CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 4346231267Smm &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum), 4347231267Smm sysctl_jail_default_level, "I", 4348231267Smm "Ruleset for the devfs filesystem in jail"); 4349231267Smm 4350192895Sjamie/* 4351192895Sjamie * Nodes to describe jail parameters. Maximum length of string parameters 4352192895Sjamie * is returned in the string itself, and the other parameters exist merely 4353192895Sjamie * to make themselves and their types known. 4354192895Sjamie */ 4355192895SjamieSYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0, 4356192895Sjamie "Jail parameters"); 4357192895Sjamie 4358192895Sjamieint 4359192895Sjamiesysctl_jail_param(SYSCTL_HANDLER_ARGS) 4360192895Sjamie{ 4361192895Sjamie int i; 4362192895Sjamie long l; 4363192895Sjamie size_t s; 4364192895Sjamie char numbuf[12]; 4365192895Sjamie 4366192895Sjamie switch (oidp->oid_kind & CTLTYPE) 4367192895Sjamie { 4368192895Sjamie case CTLTYPE_LONG: 4369192895Sjamie case CTLTYPE_ULONG: 4370192895Sjamie l = 0; 4371192895Sjamie#ifdef SCTL_MASK32 4372192895Sjamie if (!(req->flags & SCTL_MASK32)) 4373192895Sjamie#endif 4374192895Sjamie return (SYSCTL_OUT(req, &l, sizeof(l))); 4375192895Sjamie case CTLTYPE_INT: 4376192895Sjamie case CTLTYPE_UINT: 4377192895Sjamie i = 0; 4378192895Sjamie return (SYSCTL_OUT(req, &i, sizeof(i))); 4379192895Sjamie case CTLTYPE_STRING: 4380219819Sjeff snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2); 4381192895Sjamie return 4382192895Sjamie (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req)); 4383192895Sjamie case CTLTYPE_STRUCT: 4384192895Sjamie s = (size_t)arg2; 4385192895Sjamie return (SYSCTL_OUT(req, &s, sizeof(s))); 4386192895Sjamie } 4387192895Sjamie return (0); 4388192895Sjamie} 4389192895Sjamie 4390280632Sian/* 4391280632Sian * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at 4392280632Sian * jail creation time but cannot be changed in an existing jail. 4393280632Sian */ 4394192895SjamieSYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID"); 4395192895SjamieSYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID"); 4396192895SjamieSYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name"); 4397192895SjamieSYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path"); 4398192895SjamieSYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW, 4399192895Sjamie "I", "Jail secure level"); 4400280632SianSYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I", 4401280632Sian "Jail value for kern.osreldate and uname -K"); 4402280632SianSYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN, 4403280632Sian "Jail value for kern.osrelease and uname -r"); 4404192895SjamieSYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW, 4405192895Sjamie "I", "Jail cannot see all mounted file systems"); 4406231267SmmSYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW, 4407231267Smm "I", "Ruleset for in-jail devfs mounts"); 4408192895SjamieSYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW, 4409192895Sjamie "B", "Jail persistence"); 4410194251Sjamie#ifdef VIMAGE 4411194251SjamieSYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN, 4412195870Sjamie "E,jailsys", "Virtual network stack"); 4413194251Sjamie#endif 4414192895SjamieSYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD, 4415192895Sjamie "B", "Jail is in the process of shutting down"); 4416192895Sjamie 4417194762SjamieSYSCTL_JAIL_PARAM_NODE(children, "Number of child jails"); 4418194762SjamieSYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD, 4419194762Sjamie "I", "Current number of child jails"); 4420194762SjamieSYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW, 4421194762Sjamie "I", "Maximum number of child jails"); 4422194762Sjamie 4423195870SjamieSYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info"); 4424192895SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN, 4425192895Sjamie "Jail hostname"); 4426193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN, 4427193066Sjamie "Jail NIS domainname"); 4428193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN, 4429193066Sjamie "Jail host UUID"); 4430193066SjamieSYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW, 4431193066Sjamie "LU", "Jail host ID"); 4432192895Sjamie 4433192895SjamieSYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset"); 4434192895SjamieSYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID"); 4435192895Sjamie 4436192895Sjamie#ifdef INET 4437195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN, 4438195974Sjamie "Jail IPv4 address virtualization"); 4439192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr), 4440192895Sjamie "S,in_addr,a", "Jail IPv4 addresses"); 4441202468SbzSYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4442202468Sbz "B", "Do (not) use IPv4 source address selection rather than the " 4443202468Sbz "primary jail IPv4 address."); 4444192895Sjamie#endif 4445192895Sjamie#ifdef INET6 4446195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN, 4447195974Sjamie "Jail IPv6 address virtualization"); 4448192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr), 4449192895Sjamie "S,in6_addr,a", "Jail IPv6 addresses"); 4450202468SbzSYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4451202468Sbz "B", "Do (not) use IPv6 source address selection rather than the " 4452202468Sbz "primary jail IPv6 address."); 4453192895Sjamie#endif 4454192895Sjamie 4455192895SjamieSYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags"); 4456192895SjamieSYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW, 4457192895Sjamie "B", "Jail may set hostname"); 4458192895SjamieSYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW, 4459192895Sjamie "B", "Jail may use SYSV IPC"); 4460192895SjamieSYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW, 4461192895Sjamie "B", "Jail may create raw sockets"); 4462192895SjamieSYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW, 4463192895Sjamie "B", "Jail may alter system file flags"); 4464192895SjamieSYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW, 4465192895Sjamie "B", "Jail may set file quotas"); 4466192895SjamieSYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW, 4467192895Sjamie "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route"); 4468192895Sjamie 4469232059SmmSYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags"); 4470232059SmmSYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW, 4471232059Smm "B", "Jail may mount/unmount jail-friendly file systems in general"); 4472232059SmmSYSCTL_JAIL_PARAM(_allow_mount, devfs, CTLTYPE_INT | CTLFLAG_RW, 4473232186Smm "B", "Jail may mount the devfs file system"); 4474277985SjamieSYSCTL_JAIL_PARAM(_allow_mount, fdescfs, CTLTYPE_INT | CTLFLAG_RW, 4475277985Sjamie "B", "Jail may mount the fdescfs file system"); 4476232059SmmSYSCTL_JAIL_PARAM(_allow_mount, nullfs, CTLTYPE_INT | CTLFLAG_RW, 4477232186Smm "B", "Jail may mount the nullfs file system"); 4478232278SmmSYSCTL_JAIL_PARAM(_allow_mount, procfs, CTLTYPE_INT | CTLFLAG_RW, 4479232278Smm "B", "Jail may mount the procfs file system"); 4480254741SdelphijSYSCTL_JAIL_PARAM(_allow_mount, tmpfs, CTLTYPE_INT | CTLFLAG_RW, 4481254741Sdelphij "B", "Jail may mount the tmpfs file system"); 4482232186SmmSYSCTL_JAIL_PARAM(_allow_mount, zfs, CTLTYPE_INT | CTLFLAG_RW, 4483232186Smm "B", "Jail may mount the zfs file system"); 4484232059Smm 4485220137Straszvoid 4486220137Straszprison_racct_foreach(void (*callback)(struct racct *racct, 4487220137Strasz void *arg2, void *arg3), void *arg2, void *arg3) 4488220137Strasz{ 4489221362Strasz struct prison_racct *prr; 4490192895Sjamie 4491220137Strasz sx_slock(&allprison_lock); 4492221362Strasz LIST_FOREACH(prr, &allprison_racct, prr_next) 4493221362Strasz (callback)(prr->prr_racct, arg2, arg3); 4494220137Strasz sx_sunlock(&allprison_lock); 4495220137Strasz} 4496220137Strasz 4497221362Straszstatic struct prison_racct * 4498221362Straszprison_racct_find_locked(const char *name) 4499221362Strasz{ 4500221362Strasz struct prison_racct *prr; 4501221362Strasz 4502221362Strasz sx_assert(&allprison_lock, SA_XLOCKED); 4503221362Strasz 4504221362Strasz if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN) 4505221362Strasz return (NULL); 4506221362Strasz 4507221362Strasz LIST_FOREACH(prr, &allprison_racct, prr_next) { 4508221362Strasz if (strcmp(name, prr->prr_name) != 0) 4509221362Strasz continue; 4510221362Strasz 4511221362Strasz /* Found prison_racct with a matching name? */ 4512221362Strasz prison_racct_hold(prr); 4513221362Strasz return (prr); 4514221362Strasz } 4515221362Strasz 4516221362Strasz /* Add new prison_racct. */ 4517221362Strasz prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK); 4518221362Strasz racct_create(&prr->prr_racct); 4519221362Strasz 4520221362Strasz strcpy(prr->prr_name, name); 4521221362Strasz refcount_init(&prr->prr_refcount, 1); 4522221362Strasz LIST_INSERT_HEAD(&allprison_racct, prr, prr_next); 4523221362Strasz 4524221362Strasz return (prr); 4525221362Strasz} 4526221362Strasz 4527221362Straszstruct prison_racct * 4528221362Straszprison_racct_find(const char *name) 4529221362Strasz{ 4530221362Strasz struct prison_racct *prr; 4531221362Strasz 4532221362Strasz sx_xlock(&allprison_lock); 4533221362Strasz prr = prison_racct_find_locked(name); 4534221362Strasz sx_xunlock(&allprison_lock); 4535221362Strasz return (prr); 4536221362Strasz} 4537221362Strasz 4538221362Straszvoid 4539221362Straszprison_racct_hold(struct prison_racct *prr) 4540221362Strasz{ 4541221362Strasz 4542221362Strasz refcount_acquire(&prr->prr_refcount); 4543221362Strasz} 4544221362Strasz 4545232598Straszstatic void 4546232598Straszprison_racct_free_locked(struct prison_racct *prr) 4547232598Strasz{ 4548232598Strasz 4549232598Strasz sx_assert(&allprison_lock, SA_XLOCKED); 4550232598Strasz 4551232598Strasz if (refcount_release(&prr->prr_refcount)) { 4552232598Strasz racct_destroy(&prr->prr_racct); 4553232598Strasz LIST_REMOVE(prr, prr_next); 4554232598Strasz free(prr, M_PRISON_RACCT); 4555232598Strasz } 4556232598Strasz} 4557232598Strasz 4558221362Straszvoid 4559221362Straszprison_racct_free(struct prison_racct *prr) 4560221362Strasz{ 4561221362Strasz int old; 4562221362Strasz 4563232598Strasz sx_assert(&allprison_lock, SA_UNLOCKED); 4564232598Strasz 4565221362Strasz old = prr->prr_refcount; 4566221362Strasz if (old > 1 && atomic_cmpset_int(&prr->prr_refcount, old, old - 1)) 4567221362Strasz return; 4568221362Strasz 4569221362Strasz sx_xlock(&allprison_lock); 4570232598Strasz prison_racct_free_locked(prr); 4571221362Strasz sx_xunlock(&allprison_lock); 4572221362Strasz} 4573221362Strasz 4574221362Strasz#ifdef RACCT 4575221362Straszstatic void 4576221362Straszprison_racct_attach(struct prison *pr) 4577221362Strasz{ 4578221362Strasz struct prison_racct *prr; 4579221362Strasz 4580232598Strasz sx_assert(&allprison_lock, SA_XLOCKED); 4581232598Strasz 4582221362Strasz prr = prison_racct_find_locked(pr->pr_name); 4583221362Strasz KASSERT(prr != NULL, ("cannot find prison_racct")); 4584221362Strasz 4585221362Strasz pr->pr_prison_racct = prr; 4586221362Strasz} 4587221362Strasz 4588232598Strasz/* 4589232598Strasz * Handle jail renaming. From the racct point of view, renaming means 4590232598Strasz * moving from one prison_racct to another. 4591232598Strasz */ 4592221362Straszstatic void 4593232598Straszprison_racct_modify(struct prison *pr) 4594232598Strasz{ 4595232598Strasz struct proc *p; 4596232598Strasz struct ucred *cred; 4597232598Strasz struct prison_racct *oldprr; 4598232598Strasz 4599232598Strasz sx_slock(&allproc_lock); 4600232598Strasz sx_xlock(&allprison_lock); 4601232598Strasz 4602235795Strasz if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) { 4603235795Strasz sx_xunlock(&allprison_lock); 4604235795Strasz sx_sunlock(&allproc_lock); 4605232598Strasz return; 4606235795Strasz } 4607232598Strasz 4608232598Strasz oldprr = pr->pr_prison_racct; 4609232598Strasz pr->pr_prison_racct = NULL; 4610232598Strasz 4611232598Strasz prison_racct_attach(pr); 4612232598Strasz 4613232598Strasz /* 4614232598Strasz * Move resource utilisation records. 4615232598Strasz */ 4616232598Strasz racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct); 4617232598Strasz 4618232598Strasz /* 4619232598Strasz * Force rctl to reattach rules to processes. 4620232598Strasz */ 4621232598Strasz FOREACH_PROC_IN_SYSTEM(p) { 4622232598Strasz PROC_LOCK(p); 4623232598Strasz cred = crhold(p->p_ucred); 4624232598Strasz PROC_UNLOCK(p); 4625232598Strasz racct_proc_ucred_changed(p, cred, cred); 4626232598Strasz crfree(cred); 4627232598Strasz } 4628232598Strasz 4629232598Strasz sx_sunlock(&allproc_lock); 4630232598Strasz prison_racct_free_locked(oldprr); 4631232598Strasz sx_xunlock(&allprison_lock); 4632232598Strasz} 4633232598Strasz 4634232598Straszstatic void 4635221362Straszprison_racct_detach(struct prison *pr) 4636221362Strasz{ 4637232598Strasz 4638232598Strasz sx_assert(&allprison_lock, SA_UNLOCKED); 4639232598Strasz 4640244404Smjg if (pr->pr_prison_racct == NULL) 4641244404Smjg return; 4642221362Strasz prison_racct_free(pr->pr_prison_racct); 4643221362Strasz pr->pr_prison_racct = NULL; 4644221362Strasz} 4645221362Strasz#endif /* RACCT */ 4646221362Strasz 4647185435Sbz#ifdef DDB 4648191673Sjamie 4649191673Sjamiestatic void 4650191673Sjamiedb_show_prison(struct prison *pr) 4651185435Sbz{ 4652192895Sjamie int fi; 4653191673Sjamie#if defined(INET) || defined(INET6) 4654191673Sjamie int ii; 4655185435Sbz#endif 4656195870Sjamie unsigned jsf; 4657185435Sbz#ifdef INET6 4658185435Sbz char ip6buf[INET6_ADDRSTRLEN]; 4659185435Sbz#endif 4660185435Sbz 4661191673Sjamie db_printf("prison %p:\n", pr); 4662191673Sjamie db_printf(" jid = %d\n", pr->pr_id); 4663191673Sjamie db_printf(" name = %s\n", pr->pr_name); 4664192895Sjamie db_printf(" parent = %p\n", pr->pr_parent); 4665191673Sjamie db_printf(" ref = %d\n", pr->pr_ref); 4666191673Sjamie db_printf(" uref = %d\n", pr->pr_uref); 4667191673Sjamie db_printf(" path = %s\n", pr->pr_path); 4668191673Sjamie db_printf(" cpuset = %d\n", pr->pr_cpuset 4669191673Sjamie ? pr->pr_cpuset->cs_id : -1); 4670194251Sjamie#ifdef VIMAGE 4671194251Sjamie db_printf(" vnet = %p\n", pr->pr_vnet); 4672194251Sjamie#endif 4673191673Sjamie db_printf(" root = %p\n", pr->pr_root); 4674191673Sjamie db_printf(" securelevel = %d\n", pr->pr_securelevel); 4675231267Smm db_printf(" devfs_rsnum = %d\n", pr->pr_devfs_rsnum); 4676202123Sbz db_printf(" children.max = %d\n", pr->pr_childmax); 4677202123Sbz db_printf(" children.cur = %d\n", pr->pr_childcount); 4678192895Sjamie db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children)); 4679192895Sjamie db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling)); 4680202123Sbz db_printf(" flags = 0x%x", pr->pr_flags); 4681192895Sjamie for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 4682192895Sjamie fi++) 4683192895Sjamie if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi))) 4684192895Sjamie db_printf(" %s", pr_flag_names[fi]); 4685195870Sjamie for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 4686195870Sjamie fi++) { 4687195870Sjamie jsf = pr->pr_flags & 4688195870Sjamie (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new); 4689195870Sjamie db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name, 4690195870Sjamie pr_flag_jailsys[fi].disable && 4691195870Sjamie (jsf == pr_flag_jailsys[fi].disable) ? "disable" 4692195870Sjamie : (jsf == pr_flag_jailsys[fi].new) ? "new" 4693195870Sjamie : "inherit"); 4694195870Sjamie } 4695202123Sbz db_printf(" allow = 0x%x", pr->pr_allow); 4696192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 4697192895Sjamie fi++) 4698192895Sjamie if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi))) 4699192895Sjamie db_printf(" %s", pr_allow_names[fi]); 4700191673Sjamie db_printf("\n"); 4701192895Sjamie db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs); 4702194118Sjamie db_printf(" host.hostname = %s\n", pr->pr_hostname); 4703194118Sjamie db_printf(" host.domainname = %s\n", pr->pr_domainname); 4704194118Sjamie db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid); 4705193066Sjamie db_printf(" host.hostid = %lu\n", pr->pr_hostid); 4706185435Sbz#ifdef INET 4707191673Sjamie db_printf(" ip4s = %d\n", pr->pr_ip4s); 4708191673Sjamie for (ii = 0; ii < pr->pr_ip4s; ii++) 4709191673Sjamie db_printf(" %s %s\n", 4710202123Sbz ii == 0 ? "ip4.addr =" : " ", 4711191673Sjamie inet_ntoa(pr->pr_ip4[ii])); 4712185435Sbz#endif 4713185435Sbz#ifdef INET6 4714191673Sjamie db_printf(" ip6s = %d\n", pr->pr_ip6s); 4715191673Sjamie for (ii = 0; ii < pr->pr_ip6s; ii++) 4716191673Sjamie db_printf(" %s %s\n", 4717202123Sbz ii == 0 ? "ip6.addr =" : " ", 4718191673Sjamie ip6_sprintf(ip6buf, &pr->pr_ip6[ii])); 4719191673Sjamie#endif 4720191673Sjamie} 4721191673Sjamie 4722191673SjamieDB_SHOW_COMMAND(prison, db_show_prison_command) 4723191673Sjamie{ 4724191673Sjamie struct prison *pr; 4725191673Sjamie 4726191673Sjamie if (!have_addr) { 4727192895Sjamie /* 4728192895Sjamie * Show all prisons in the list, and prison0 which is not 4729192895Sjamie * listed. 4730192895Sjamie */ 4731192895Sjamie db_show_prison(&prison0); 4732192895Sjamie if (!db_pager_quit) { 4733192895Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) { 4734192895Sjamie db_show_prison(pr); 4735192895Sjamie if (db_pager_quit) 4736192895Sjamie break; 4737192895Sjamie } 4738191673Sjamie } 4739191673Sjamie return; 4740191673Sjamie } 4741191673Sjamie 4742192895Sjamie if (addr == 0) 4743192895Sjamie pr = &prison0; 4744192895Sjamie else { 4745192895Sjamie /* Look for a prison with the ID and with references. */ 4746191673Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) 4747192895Sjamie if (pr->pr_id == addr && pr->pr_ref > 0) 4748191673Sjamie break; 4749192895Sjamie if (pr == NULL) 4750192895Sjamie /* Look again, without requiring a reference. */ 4751192895Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) 4752192895Sjamie if (pr->pr_id == addr) 4753192895Sjamie break; 4754192895Sjamie if (pr == NULL) 4755192895Sjamie /* Assume address points to a valid prison. */ 4756192895Sjamie pr = (struct prison *)addr; 4757192895Sjamie } 4758191673Sjamie db_show_prison(pr); 4759185435Sbz} 4760191673Sjamie 4761185435Sbz#endif /* DDB */ 4762