kern_jail.c revision 277279
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 277279 2015-01-17 01:16:03Z jamie $"); 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", 210192895Sjamie}; 211216861Sbzconst size_t pr_allow_names_size = sizeof(pr_allow_names); 212192895Sjamie 213192895Sjamiestatic char *pr_allow_nonames[] = { 214192895Sjamie "allow.noset_hostname", 215192895Sjamie "allow.nosysvipc", 216192895Sjamie "allow.noraw_sockets", 217192895Sjamie "allow.nochflags", 218192895Sjamie "allow.nomount", 219192895Sjamie "allow.noquotas", 220192895Sjamie "allow.nosocket_af", 221232059Smm "allow.mount.nodevfs", 222232059Smm "allow.mount.nonullfs", 223232186Smm "allow.mount.nozfs", 224232278Smm "allow.mount.noprocfs", 225254741Sdelphij "allow.mount.notmpfs", 226192895Sjamie}; 227216861Sbzconst size_t pr_allow_nonames_size = sizeof(pr_allow_nonames); 228192895Sjamie 229196002Sjamie#define JAIL_DEFAULT_ALLOW PR_ALLOW_SET_HOSTNAME 230196002Sjamie#define JAIL_DEFAULT_ENFORCE_STATFS 2 231232059Smm#define JAIL_DEFAULT_DEVFS_RSNUM 0 232192895Sjamiestatic unsigned jail_default_allow = JAIL_DEFAULT_ALLOW; 233196002Sjamiestatic int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 234231267Smmstatic int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM; 235192895Sjamie#if defined(INET) || defined(INET6) 236193865Sjamiestatic unsigned jail_max_af_ips = 255; 237192895Sjamie#endif 238192895Sjamie 239192895Sjamie#ifdef INET 240185435Sbzstatic int 241185435Sbzqcmp_v4(const void *ip1, const void *ip2) 242185435Sbz{ 243185435Sbz in_addr_t iaa, iab; 244185435Sbz 245185435Sbz /* 246185435Sbz * We need to compare in HBO here to get the list sorted as expected 247185435Sbz * by the result of the code. Sorting NBO addresses gives you 248185435Sbz * interesting results. If you do not understand, do not try. 249185435Sbz */ 250185435Sbz iaa = ntohl(((const struct in_addr *)ip1)->s_addr); 251185435Sbz iab = ntohl(((const struct in_addr *)ip2)->s_addr); 252185435Sbz 253185435Sbz /* 254185435Sbz * Do not simply return the difference of the two numbers, the int is 255185435Sbz * not wide enough. 256185435Sbz */ 257185435Sbz if (iaa > iab) 258185435Sbz return (1); 259185435Sbz else if (iaa < iab) 260185435Sbz return (-1); 261185435Sbz else 262185435Sbz return (0); 263185435Sbz} 264185435Sbz#endif 265185435Sbz 266185435Sbz#ifdef INET6 267185435Sbzstatic int 268185435Sbzqcmp_v6(const void *ip1, const void *ip2) 269185435Sbz{ 270185435Sbz const struct in6_addr *ia6a, *ia6b; 271185435Sbz int i, rc; 272185435Sbz 273185435Sbz ia6a = (const struct in6_addr *)ip1; 274185435Sbz ia6b = (const struct in6_addr *)ip2; 275185435Sbz 276185435Sbz rc = 0; 277190466Sjamie for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) { 278185435Sbz if (ia6a->s6_addr[i] > ia6b->s6_addr[i]) 279185435Sbz rc = 1; 280185435Sbz else if (ia6a->s6_addr[i] < ia6b->s6_addr[i]) 281185435Sbz rc = -1; 282185435Sbz } 283185435Sbz return (rc); 284185435Sbz} 285185435Sbz#endif 286185435Sbz 287191673Sjamie/* 288191673Sjamie * struct jail_args { 289191673Sjamie * struct jail *jail; 290191673Sjamie * }; 291191673Sjamie */ 292191673Sjamieint 293225617Skmacysys_jail(struct thread *td, struct jail_args *uap) 294185435Sbz{ 295191673Sjamie uint32_t version; 296191673Sjamie int error; 297192895Sjamie struct jail j; 298185435Sbz 299191673Sjamie error = copyin(uap->jail, &version, sizeof(uint32_t)); 300191673Sjamie if (error) 301191673Sjamie return (error); 302185435Sbz 303191673Sjamie switch (version) { 304191673Sjamie case 0: 305191673Sjamie { 306191673Sjamie struct jail_v0 j0; 307185435Sbz 308192895Sjamie /* FreeBSD single IPv4 jails. */ 309192895Sjamie bzero(&j, sizeof(struct jail)); 310191673Sjamie error = copyin(uap->jail, &j0, sizeof(struct jail_v0)); 311191673Sjamie if (error) 312191673Sjamie return (error); 313192895Sjamie j.version = j0.version; 314192895Sjamie j.path = j0.path; 315192895Sjamie j.hostname = j0.hostname; 316258929Speter j.ip4s = htonl(j0.ip_number); /* jail_v0 is host order */ 317191673Sjamie break; 318191673Sjamie } 319191673Sjamie 320191673Sjamie case 1: 321185435Sbz /* 322191673Sjamie * Version 1 was used by multi-IPv4 jail implementations 323191673Sjamie * that never made it into the official kernel. 324185435Sbz */ 325191673Sjamie return (EINVAL); 326185435Sbz 327191673Sjamie case 2: /* JAIL_API_VERSION */ 328191673Sjamie /* FreeBSD multi-IPv4/IPv6,noIP jails. */ 329191673Sjamie error = copyin(uap->jail, &j, sizeof(struct jail)); 330191673Sjamie if (error) 331191673Sjamie return (error); 332192895Sjamie break; 333192895Sjamie 334192895Sjamie default: 335192895Sjamie /* Sci-Fi jails are not supported, sorry. */ 336192895Sjamie return (EINVAL); 337192895Sjamie } 338192895Sjamie return (kern_jail(td, &j)); 339192895Sjamie} 340192895Sjamie 341192895Sjamieint 342192895Sjamiekern_jail(struct thread *td, struct jail *j) 343192895Sjamie{ 344193865Sjamie struct iovec optiov[2 * (4 345193865Sjamie + sizeof(pr_allow_names) / sizeof(pr_allow_names[0]) 346193865Sjamie#ifdef INET 347193865Sjamie + 1 348193865Sjamie#endif 349193865Sjamie#ifdef INET6 350193865Sjamie + 1 351193865Sjamie#endif 352193865Sjamie )]; 353192895Sjamie struct uio opt; 354192895Sjamie char *u_path, *u_hostname, *u_name; 355185435Sbz#ifdef INET 356193865Sjamie uint32_t ip4s; 357192895Sjamie struct in_addr *u_ip4; 358192895Sjamie#endif 359192895Sjamie#ifdef INET6 360192895Sjamie struct in6_addr *u_ip6; 361192895Sjamie#endif 362192895Sjamie size_t tmplen; 363192895Sjamie int error, enforce_statfs, fi; 364192895Sjamie 365192895Sjamie bzero(&optiov, sizeof(optiov)); 366192895Sjamie opt.uio_iov = optiov; 367192895Sjamie opt.uio_iovcnt = 0; 368192895Sjamie opt.uio_offset = -1; 369192895Sjamie opt.uio_resid = -1; 370192895Sjamie opt.uio_segflg = UIO_SYSSPACE; 371192895Sjamie opt.uio_rw = UIO_READ; 372192895Sjamie opt.uio_td = td; 373192895Sjamie 374192895Sjamie /* Set permissions for top-level jails from sysctls. */ 375192895Sjamie if (!jailed(td->td_ucred)) { 376192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / 377192895Sjamie sizeof(pr_allow_names[0]); fi++) { 378192895Sjamie optiov[opt.uio_iovcnt].iov_base = 379192895Sjamie (jail_default_allow & (1 << fi)) 380192895Sjamie ? pr_allow_names[fi] : pr_allow_nonames[fi]; 381192895Sjamie optiov[opt.uio_iovcnt].iov_len = 382192895Sjamie strlen(optiov[opt.uio_iovcnt].iov_base) + 1; 383192895Sjamie opt.uio_iovcnt += 2; 384192895Sjamie } 385192895Sjamie optiov[opt.uio_iovcnt].iov_base = "enforce_statfs"; 386192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs"); 387192895Sjamie opt.uio_iovcnt++; 388192895Sjamie enforce_statfs = jail_default_enforce_statfs; 389192895Sjamie optiov[opt.uio_iovcnt].iov_base = &enforce_statfs; 390192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs); 391192895Sjamie opt.uio_iovcnt++; 392192895Sjamie } 393192895Sjamie 394192895Sjamie tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN; 395192895Sjamie#ifdef INET 396192895Sjamie ip4s = (j->version == 0) ? 1 : j->ip4s; 397192895Sjamie if (ip4s > jail_max_af_ips) 398192895Sjamie return (EINVAL); 399192895Sjamie tmplen += ip4s * sizeof(struct in_addr); 400191673Sjamie#else 401192895Sjamie if (j->ip4s > 0) 402192895Sjamie return (EINVAL); 403191673Sjamie#endif 404191673Sjamie#ifdef INET6 405192895Sjamie if (j->ip6s > jail_max_af_ips) 406192895Sjamie return (EINVAL); 407192895Sjamie tmplen += j->ip6s * sizeof(struct in6_addr); 408191673Sjamie#else 409192895Sjamie if (j->ip6s > 0) 410192895Sjamie return (EINVAL); 411191673Sjamie#endif 412192895Sjamie u_path = malloc(tmplen, M_TEMP, M_WAITOK); 413192895Sjamie u_hostname = u_path + MAXPATHLEN; 414192895Sjamie u_name = u_hostname + MAXHOSTNAMELEN; 415191673Sjamie#ifdef INET 416192895Sjamie u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN); 417191673Sjamie#endif 418191673Sjamie#ifdef INET6 419191673Sjamie#ifdef INET 420192895Sjamie u_ip6 = (struct in6_addr *)(u_ip4 + ip4s); 421191673Sjamie#else 422192895Sjamie u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN); 423191673Sjamie#endif 424191673Sjamie#endif 425192895Sjamie optiov[opt.uio_iovcnt].iov_base = "path"; 426192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("path"); 427192895Sjamie opt.uio_iovcnt++; 428192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_path; 429192895Sjamie error = copyinstr(j->path, u_path, MAXPATHLEN, 430192895Sjamie &optiov[opt.uio_iovcnt].iov_len); 431192895Sjamie if (error) { 432192895Sjamie free(u_path, M_TEMP); 433192895Sjamie return (error); 434192895Sjamie } 435192895Sjamie opt.uio_iovcnt++; 436192895Sjamie optiov[opt.uio_iovcnt].iov_base = "host.hostname"; 437192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname"); 438192895Sjamie opt.uio_iovcnt++; 439192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_hostname; 440192895Sjamie error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN, 441192895Sjamie &optiov[opt.uio_iovcnt].iov_len); 442192895Sjamie if (error) { 443192895Sjamie free(u_path, M_TEMP); 444192895Sjamie return (error); 445192895Sjamie } 446192895Sjamie opt.uio_iovcnt++; 447192895Sjamie if (j->jailname != NULL) { 448192895Sjamie optiov[opt.uio_iovcnt].iov_base = "name"; 449192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("name"); 450192895Sjamie opt.uio_iovcnt++; 451192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_name; 452192895Sjamie error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN, 453192895Sjamie &optiov[opt.uio_iovcnt].iov_len); 454191673Sjamie if (error) { 455191673Sjamie free(u_path, M_TEMP); 456191673Sjamie return (error); 457191673Sjamie } 458192895Sjamie opt.uio_iovcnt++; 459192895Sjamie } 460191673Sjamie#ifdef INET 461192895Sjamie optiov[opt.uio_iovcnt].iov_base = "ip4.addr"; 462192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr"); 463192895Sjamie opt.uio_iovcnt++; 464192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_ip4; 465192895Sjamie optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr); 466192895Sjamie if (j->version == 0) 467192895Sjamie u_ip4->s_addr = j->ip4s; 468192895Sjamie else { 469192895Sjamie error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len); 470191673Sjamie if (error) { 471191673Sjamie free(u_path, M_TEMP); 472191673Sjamie return (error); 473191673Sjamie } 474192895Sjamie } 475192895Sjamie opt.uio_iovcnt++; 476185435Sbz#endif 477185435Sbz#ifdef INET6 478192895Sjamie optiov[opt.uio_iovcnt].iov_base = "ip6.addr"; 479192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr"); 480192895Sjamie opt.uio_iovcnt++; 481192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_ip6; 482192895Sjamie optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr); 483192895Sjamie error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len); 484192895Sjamie if (error) { 485192895Sjamie free(u_path, M_TEMP); 486192895Sjamie return (error); 487192895Sjamie } 488192895Sjamie opt.uio_iovcnt++; 489185435Sbz#endif 490192895Sjamie KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]), 491192895Sjamie ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt)); 492191673Sjamie error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH); 493191673Sjamie free(u_path, M_TEMP); 494191673Sjamie return (error); 495185435Sbz} 496185435Sbz 497192895Sjamie 498191673Sjamie/* 499191673Sjamie * struct jail_set_args { 500191673Sjamie * struct iovec *iovp; 501191673Sjamie * unsigned int iovcnt; 502191673Sjamie * int flags; 503191673Sjamie * }; 504191673Sjamie */ 505191673Sjamieint 506225617Skmacysys_jail_set(struct thread *td, struct jail_set_args *uap) 507185435Sbz{ 508191673Sjamie struct uio *auio; 509191673Sjamie int error; 510191673Sjamie 511191673Sjamie /* Check that we have an even number of iovecs. */ 512191673Sjamie if (uap->iovcnt & 1) 513191673Sjamie return (EINVAL); 514191673Sjamie 515191673Sjamie error = copyinuio(uap->iovp, uap->iovcnt, &auio); 516191673Sjamie if (error) 517191673Sjamie return (error); 518191673Sjamie error = kern_jail_set(td, auio, uap->flags); 519191673Sjamie free(auio, M_IOV); 520191673Sjamie return (error); 521191673Sjamie} 522191673Sjamie 523191673Sjamieint 524191673Sjamiekern_jail_set(struct thread *td, struct uio *optuio, int flags) 525191673Sjamie{ 526191673Sjamie struct nameidata nd; 527185435Sbz#ifdef INET 528190466Sjamie struct in_addr *ip4; 529185435Sbz#endif 530185435Sbz#ifdef INET6 531185435Sbz struct in6_addr *ip6; 532185435Sbz#endif 533191673Sjamie struct vfsopt *opt; 534191673Sjamie struct vfsoptlist *opts; 535196135Sbz struct prison *pr, *deadpr, *mypr, *ppr, *tpr; 536191673Sjamie struct vnode *root; 537196835Sjamie char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid; 538230407Smm char *g_path; 539192895Sjamie#if defined(INET) || defined(INET6) 540196135Sbz struct prison *tppr; 541191673Sjamie void *op; 542192895Sjamie#endif 543193066Sjamie unsigned long hid; 544192895Sjamie size_t namelen, onamelen; 545192895Sjamie int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos; 546231267Smm int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel; 547195870Sjamie int fi, jid, jsys, len, level; 548241896Skib int childmax, rsnum, slevel; 549230129Smm int fullpath_disabled; 550191673Sjamie#if defined(INET) || defined(INET6) 551192895Sjamie int ii, ij; 552191673Sjamie#endif 553191673Sjamie#ifdef INET 554195974Sjamie int ip4s, redo_ip4; 555191673Sjamie#endif 556191673Sjamie#ifdef INET6 557195974Sjamie int ip6s, redo_ip6; 558191673Sjamie#endif 559224290Smckusick uint64_t pr_allow, ch_allow, pr_flags, ch_flags; 560224290Smckusick unsigned tallow; 561191673Sjamie char numbuf[12]; 562185435Sbz 563191673Sjamie error = priv_check(td, PRIV_JAIL_SET); 564191673Sjamie if (!error && (flags & JAIL_ATTACH)) 565191673Sjamie error = priv_check(td, PRIV_JAIL_ATTACH); 566191673Sjamie if (error) 567191673Sjamie return (error); 568192895Sjamie mypr = ppr = td->td_ucred->cr_prison; 569194762Sjamie if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0) 570192895Sjamie return (EPERM); 571191673Sjamie if (flags & ~JAIL_SET_MASK) 572191673Sjamie return (EINVAL); 573191673Sjamie 574185435Sbz /* 575191673Sjamie * Check all the parameters before committing to anything. Not all 576191673Sjamie * errors can be caught early, but we may as well try. Also, this 577191673Sjamie * takes care of some expensive stuff (path lookup) before getting 578191673Sjamie * the allprison lock. 579185435Sbz * 580191673Sjamie * XXX Jails are not filesystems, and jail parameters are not mount 581191673Sjamie * options. But it makes more sense to re-use the vfsopt code 582191673Sjamie * than duplicate it under a different name. 583185435Sbz */ 584191673Sjamie error = vfs_buildopts(optuio, &opts); 585191673Sjamie if (error) 586191673Sjamie return (error); 587185435Sbz#ifdef INET 588185435Sbz ip4 = NULL; 589185435Sbz#endif 590185435Sbz#ifdef INET6 591185435Sbz ip6 = NULL; 592185435Sbz#endif 593230407Smm g_path = NULL; 594191673Sjamie 595191673Sjamie error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 596191673Sjamie if (error == ENOENT) 597191673Sjamie jid = 0; 598191673Sjamie else if (error != 0) 599191673Sjamie goto done_free; 600191673Sjamie 601191673Sjamie error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel)); 602191673Sjamie if (error == ENOENT) 603191673Sjamie gotslevel = 0; 604191673Sjamie else if (error != 0) 605191673Sjamie goto done_free; 606191673Sjamie else 607191673Sjamie gotslevel = 1; 608191673Sjamie 609194762Sjamie error = 610194762Sjamie vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax)); 611194762Sjamie if (error == ENOENT) 612194762Sjamie gotchildmax = 0; 613194762Sjamie else if (error != 0) 614194762Sjamie goto done_free; 615194762Sjamie else 616194762Sjamie gotchildmax = 1; 617194762Sjamie 618192895Sjamie error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce)); 619212436Sjamie if (error == ENOENT) 620212436Sjamie gotenforce = 0; 621212436Sjamie else if (error != 0) 622192895Sjamie goto done_free; 623212436Sjamie else if (enforce < 0 || enforce > 2) { 624212436Sjamie error = EINVAL; 625212436Sjamie goto done_free; 626212436Sjamie } else 627212436Sjamie gotenforce = 1; 628192895Sjamie 629231267Smm error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum)); 630231267Smm if (error == ENOENT) 631231267Smm gotrsnum = 0; 632231267Smm else if (error != 0) 633231267Smm goto done_free; 634231267Smm else 635231267Smm gotrsnum = 1; 636231267Smm 637191673Sjamie pr_flags = ch_flags = 0; 638192895Sjamie for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 639192895Sjamie fi++) { 640192895Sjamie if (pr_flag_names[fi] == NULL) 641192895Sjamie continue; 642192895Sjamie vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi); 643192895Sjamie vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi); 644192895Sjamie } 645191673Sjamie ch_flags |= pr_flags; 646195870Sjamie for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 647195870Sjamie fi++) { 648195870Sjamie error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys, 649195870Sjamie sizeof(jsys)); 650195870Sjamie if (error == ENOENT) 651195870Sjamie continue; 652195870Sjamie if (error != 0) 653195870Sjamie goto done_free; 654195870Sjamie switch (jsys) { 655195870Sjamie case JAIL_SYS_DISABLE: 656195870Sjamie if (!pr_flag_jailsys[fi].disable) { 657195870Sjamie error = EINVAL; 658195870Sjamie goto done_free; 659195870Sjamie } 660195870Sjamie pr_flags |= pr_flag_jailsys[fi].disable; 661195870Sjamie break; 662195870Sjamie case JAIL_SYS_NEW: 663195870Sjamie pr_flags |= pr_flag_jailsys[fi].new; 664195870Sjamie break; 665195870Sjamie case JAIL_SYS_INHERIT: 666195870Sjamie break; 667195870Sjamie default: 668195870Sjamie error = EINVAL; 669195870Sjamie goto done_free; 670195870Sjamie } 671195870Sjamie ch_flags |= 672195870Sjamie pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable; 673195870Sjamie } 674211085Sjamie if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE 675211085Sjamie && !(pr_flags & PR_PERSIST)) { 676211085Sjamie error = EINVAL; 677211085Sjamie vfs_opterror(opts, "new jail must persist or attach"); 678211085Sjamie goto done_errmsg; 679211085Sjamie } 680194251Sjamie#ifdef VIMAGE 681194251Sjamie if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) { 682194251Sjamie error = EINVAL; 683194251Sjamie vfs_opterror(opts, "vnet cannot be changed after creation"); 684194251Sjamie goto done_errmsg; 685194251Sjamie } 686194251Sjamie#endif 687195974Sjamie#ifdef INET 688195974Sjamie if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) { 689195974Sjamie error = EINVAL; 690195974Sjamie vfs_opterror(opts, "ip4 cannot be changed after creation"); 691195974Sjamie goto done_errmsg; 692195974Sjamie } 693195974Sjamie#endif 694195974Sjamie#ifdef INET6 695195974Sjamie if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) { 696195974Sjamie error = EINVAL; 697195974Sjamie vfs_opterror(opts, "ip6 cannot be changed after creation"); 698195974Sjamie goto done_errmsg; 699195974Sjamie } 700195974Sjamie#endif 701191673Sjamie 702192895Sjamie pr_allow = ch_allow = 0; 703192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 704192895Sjamie fi++) { 705192895Sjamie vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi); 706192895Sjamie vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi); 707192895Sjamie } 708192895Sjamie ch_allow |= pr_allow; 709192895Sjamie 710191673Sjamie error = vfs_getopt(opts, "name", (void **)&name, &len); 711191673Sjamie if (error == ENOENT) 712191673Sjamie name = NULL; 713191673Sjamie else if (error != 0) 714191673Sjamie goto done_free; 715191673Sjamie else { 716191673Sjamie if (len == 0 || name[len - 1] != '\0') { 717191673Sjamie error = EINVAL; 718191673Sjamie goto done_free; 719191673Sjamie } 720191673Sjamie if (len > MAXHOSTNAMELEN) { 721191673Sjamie error = ENAMETOOLONG; 722191673Sjamie goto done_free; 723191673Sjamie } 724191673Sjamie } 725191673Sjamie 726191673Sjamie error = vfs_getopt(opts, "host.hostname", (void **)&host, &len); 727191673Sjamie if (error == ENOENT) 728191673Sjamie host = NULL; 729191673Sjamie else if (error != 0) 730191673Sjamie goto done_free; 731191673Sjamie else { 732193066Sjamie ch_flags |= PR_HOST; 733193066Sjamie pr_flags |= PR_HOST; 734191673Sjamie if (len == 0 || host[len - 1] != '\0') { 735191673Sjamie error = EINVAL; 736191673Sjamie goto done_free; 737191673Sjamie } 738191673Sjamie if (len > MAXHOSTNAMELEN) { 739191673Sjamie error = ENAMETOOLONG; 740191673Sjamie goto done_free; 741191673Sjamie } 742191673Sjamie } 743191673Sjamie 744193066Sjamie error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len); 745193066Sjamie if (error == ENOENT) 746193066Sjamie domain = NULL; 747193066Sjamie else if (error != 0) 748193066Sjamie goto done_free; 749193066Sjamie else { 750193066Sjamie ch_flags |= PR_HOST; 751193066Sjamie pr_flags |= PR_HOST; 752193066Sjamie if (len == 0 || domain[len - 1] != '\0') { 753193066Sjamie error = EINVAL; 754193066Sjamie goto done_free; 755193066Sjamie } 756193066Sjamie if (len > MAXHOSTNAMELEN) { 757193066Sjamie error = ENAMETOOLONG; 758193066Sjamie goto done_free; 759193066Sjamie } 760193066Sjamie } 761193066Sjamie 762193066Sjamie error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len); 763193066Sjamie if (error == ENOENT) 764193066Sjamie uuid = NULL; 765193066Sjamie else if (error != 0) 766193066Sjamie goto done_free; 767193066Sjamie else { 768193066Sjamie ch_flags |= PR_HOST; 769193066Sjamie pr_flags |= PR_HOST; 770193066Sjamie if (len == 0 || uuid[len - 1] != '\0') { 771193066Sjamie error = EINVAL; 772193066Sjamie goto done_free; 773193066Sjamie } 774193066Sjamie if (len > HOSTUUIDLEN) { 775193066Sjamie error = ENAMETOOLONG; 776193066Sjamie goto done_free; 777193066Sjamie } 778193066Sjamie } 779193066Sjamie 780205014Snwhitehorn#ifdef COMPAT_FREEBSD32 781217896Sdchagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 782193066Sjamie uint32_t hid32; 783193066Sjamie 784193066Sjamie error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32)); 785193066Sjamie hid = hid32; 786193066Sjamie } else 787193066Sjamie#endif 788193066Sjamie error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid)); 789193066Sjamie if (error == ENOENT) 790193066Sjamie gothid = 0; 791193066Sjamie else if (error != 0) 792193066Sjamie goto done_free; 793193066Sjamie else { 794193066Sjamie gothid = 1; 795193066Sjamie ch_flags |= PR_HOST; 796193066Sjamie pr_flags |= PR_HOST; 797193066Sjamie } 798193066Sjamie 799185435Sbz#ifdef INET 800191673Sjamie error = vfs_getopt(opts, "ip4.addr", &op, &ip4s); 801191673Sjamie if (error == ENOENT) 802277279Sjamie ip4s = 0; 803191673Sjamie else if (error != 0) 804191673Sjamie goto done_free; 805191673Sjamie else if (ip4s & (sizeof(*ip4) - 1)) { 806191673Sjamie error = EINVAL; 807191673Sjamie goto done_free; 808192895Sjamie } else { 809195870Sjamie ch_flags |= PR_IP4_USER | PR_IP4_DISABLE; 810195870Sjamie if (ip4s == 0) 811195870Sjamie pr_flags |= PR_IP4_USER | PR_IP4_DISABLE; 812195870Sjamie else { 813195870Sjamie pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER; 814192895Sjamie ip4s /= sizeof(*ip4); 815192895Sjamie if (ip4s > jail_max_af_ips) { 816185435Sbz error = EINVAL; 817192895Sjamie vfs_opterror(opts, "too many IPv4 addresses"); 818192895Sjamie goto done_errmsg; 819185435Sbz } 820195974Sjamie ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 821192895Sjamie bcopy(op, ip4, ip4s * sizeof(*ip4)); 822192895Sjamie /* 823192895Sjamie * IP addresses are all sorted but ip[0] to preserve 824192895Sjamie * the primary IP address as given from userland. 825192895Sjamie * This special IP is used for unbound outgoing 826202116Sbz * connections as well for "loopback" traffic in case 827202116Sbz * source address selection cannot find any more fitting 828202116Sbz * address to connect from. 829192895Sjamie */ 830192895Sjamie if (ip4s > 1) 831192895Sjamie qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4); 832192895Sjamie /* 833192895Sjamie * Check for duplicate addresses and do some simple 834192895Sjamie * zero and broadcast checks. If users give other bogus 835192895Sjamie * addresses it is their problem. 836192895Sjamie * 837192895Sjamie * We do not have to care about byte order for these 838192895Sjamie * checks so we will do them in NBO. 839192895Sjamie */ 840192895Sjamie for (ii = 0; ii < ip4s; ii++) { 841192895Sjamie if (ip4[ii].s_addr == INADDR_ANY || 842192895Sjamie ip4[ii].s_addr == INADDR_BROADCAST) { 843192895Sjamie error = EINVAL; 844192895Sjamie goto done_free; 845192895Sjamie } 846192895Sjamie if ((ii+1) < ip4s && 847192895Sjamie (ip4[0].s_addr == ip4[ii+1].s_addr || 848192895Sjamie ip4[ii].s_addr == ip4[ii+1].s_addr)) { 849192895Sjamie error = EINVAL; 850192895Sjamie goto done_free; 851192895Sjamie } 852192895Sjamie } 853185435Sbz } 854191673Sjamie } 855191673Sjamie#endif 856185435Sbz 857185435Sbz#ifdef INET6 858191673Sjamie error = vfs_getopt(opts, "ip6.addr", &op, &ip6s); 859191673Sjamie if (error == ENOENT) 860277279Sjamie ip6s = 0; 861191673Sjamie else if (error != 0) 862191673Sjamie goto done_free; 863191673Sjamie else if (ip6s & (sizeof(*ip6) - 1)) { 864191673Sjamie error = EINVAL; 865191673Sjamie goto done_free; 866192895Sjamie } else { 867195870Sjamie ch_flags |= PR_IP6_USER | PR_IP6_DISABLE; 868195870Sjamie if (ip6s == 0) 869195870Sjamie pr_flags |= PR_IP6_USER | PR_IP6_DISABLE; 870195870Sjamie else { 871195870Sjamie pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER; 872192895Sjamie ip6s /= sizeof(*ip6); 873192895Sjamie if (ip6s > jail_max_af_ips) { 874185435Sbz error = EINVAL; 875192895Sjamie vfs_opterror(opts, "too many IPv6 addresses"); 876192895Sjamie goto done_errmsg; 877185435Sbz } 878195974Sjamie ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 879192895Sjamie bcopy(op, ip6, ip6s * sizeof(*ip6)); 880192895Sjamie if (ip6s > 1) 881192895Sjamie qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6); 882192895Sjamie for (ii = 0; ii < ip6s; ii++) { 883192895Sjamie if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) { 884192895Sjamie error = EINVAL; 885192895Sjamie goto done_free; 886192895Sjamie } 887192895Sjamie if ((ii+1) < ip6s && 888192895Sjamie (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) || 889192895Sjamie IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1]))) 890192895Sjamie { 891192895Sjamie error = EINVAL; 892192895Sjamie goto done_free; 893192895Sjamie } 894192895Sjamie } 895185435Sbz } 896191673Sjamie } 897185435Sbz#endif 898185435Sbz 899195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6)) 900195945Sjamie if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 901195945Sjamie error = EINVAL; 902195945Sjamie vfs_opterror(opts, 903195945Sjamie "vnet jails cannot have IP address restrictions"); 904195945Sjamie goto done_errmsg; 905195945Sjamie } 906195945Sjamie#endif 907195945Sjamie 908230143Smm fullpath_disabled = 0; 909191673Sjamie root = NULL; 910191673Sjamie error = vfs_getopt(opts, "path", (void **)&path, &len); 911191673Sjamie if (error == ENOENT) 912191673Sjamie path = NULL; 913191673Sjamie else if (error != 0) 914191673Sjamie goto done_free; 915191673Sjamie else { 916191673Sjamie if (flags & JAIL_UPDATE) { 917191673Sjamie error = EINVAL; 918191673Sjamie vfs_opterror(opts, 919191673Sjamie "path cannot be changed after creation"); 920191673Sjamie goto done_errmsg; 921191673Sjamie } 922191673Sjamie if (len == 0 || path[len - 1] != '\0') { 923191673Sjamie error = EINVAL; 924191673Sjamie goto done_free; 925191673Sjamie } 926241896Skib NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, 927230129Smm path, td); 928230129Smm error = namei(&nd); 929230129Smm if (error) 930230129Smm goto done_free; 931230129Smm root = nd.ni_vp; 932230129Smm NDFREE(&nd, NDF_ONLY_PNBUF); 933230407Smm g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 934230407Smm strlcpy(g_path, path, MAXPATHLEN); 935230407Smm error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN); 936230407Smm if (error == 0) 937230407Smm path = g_path; 938230407Smm else if (error == ENODEV) { 939230129Smm /* proceed if sysctl debug.disablefullpath == 1 */ 940230129Smm fullpath_disabled = 1; 941230129Smm if (len < 2 || (len == 2 && path[0] == '/')) 942230129Smm path = NULL; 943230407Smm } else { 944230129Smm /* exit on other errors */ 945230129Smm goto done_free; 946230129Smm } 947230129Smm if (root->v_type != VDIR) { 948230129Smm error = ENOTDIR; 949230129Smm vput(root); 950230129Smm goto done_free; 951230129Smm } 952230129Smm VOP_UNLOCK(root, 0); 953230129Smm if (fullpath_disabled) { 954192895Sjamie /* Leave room for a real-root full pathname. */ 955192895Sjamie if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/") 956192895Sjamie ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) { 957192895Sjamie error = ENAMETOOLONG; 958192895Sjamie goto done_free; 959192895Sjamie } 960191673Sjamie } 961191673Sjamie } 962185435Sbz 963191673Sjamie /* 964191673Sjamie * Grab the allprison lock before letting modules check their 965191673Sjamie * parameters. Once we have it, do not let go so we'll have a 966191673Sjamie * consistent view of the OSD list. 967191673Sjamie */ 968191673Sjamie sx_xlock(&allprison_lock); 969191673Sjamie error = osd_jail_call(NULL, PR_METHOD_CHECK, opts); 970191673Sjamie if (error) 971191673Sjamie goto done_unlock_list; 972185435Sbz 973191673Sjamie /* By now, all parameters should have been noted. */ 974191673Sjamie TAILQ_FOREACH(opt, opts, link) { 975191673Sjamie if (!opt->seen && strcmp(opt->name, "errmsg")) { 976191673Sjamie error = EINVAL; 977191673Sjamie vfs_opterror(opts, "unknown parameter: %s", opt->name); 978191673Sjamie goto done_unlock_list; 979191673Sjamie } 980191673Sjamie } 981191673Sjamie 982185435Sbz /* 983191673Sjamie * See if we are creating a new record or updating an existing one. 984191673Sjamie * This abuses the file error codes ENOENT and EEXIST. 985185435Sbz */ 986191673Sjamie cuflags = flags & (JAIL_CREATE | JAIL_UPDATE); 987191673Sjamie if (!cuflags) { 988191673Sjamie error = EINVAL; 989191673Sjamie vfs_opterror(opts, "no valid operation (create or update)"); 990191673Sjamie goto done_unlock_list; 991191673Sjamie } 992191673Sjamie pr = NULL; 993196835Sjamie namelc = NULL; 994196835Sjamie if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) { 995196835Sjamie namelc = strrchr(name, '.'); 996196835Sjamie jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10); 997196835Sjamie if (*p != '\0') 998196835Sjamie jid = 0; 999196835Sjamie } 1000191673Sjamie if (jid != 0) { 1001192895Sjamie /* 1002192895Sjamie * See if a requested jid already exists. There is an 1003192895Sjamie * information leak here if the jid exists but is not within 1004192895Sjamie * the caller's jail hierarchy. Jail creators will get EEXIST 1005192895Sjamie * even though they cannot see the jail, and CREATE | UPDATE 1006192895Sjamie * will return ENOENT which is not normally a valid error. 1007192895Sjamie */ 1008191673Sjamie if (jid < 0) { 1009191673Sjamie error = EINVAL; 1010191673Sjamie vfs_opterror(opts, "negative jid"); 1011191673Sjamie goto done_unlock_list; 1012191673Sjamie } 1013191673Sjamie pr = prison_find(jid); 1014191673Sjamie if (pr != NULL) { 1015192895Sjamie ppr = pr->pr_parent; 1016191673Sjamie /* Create: jid must not exist. */ 1017191673Sjamie if (cuflags == JAIL_CREATE) { 1018191673Sjamie mtx_unlock(&pr->pr_mtx); 1019191673Sjamie error = EEXIST; 1020191673Sjamie vfs_opterror(opts, "jail %d already exists", 1021191673Sjamie jid); 1022191673Sjamie goto done_unlock_list; 1023191673Sjamie } 1024192895Sjamie if (!prison_ischild(mypr, pr)) { 1025192895Sjamie mtx_unlock(&pr->pr_mtx); 1026192895Sjamie pr = NULL; 1027192895Sjamie } else if (pr->pr_uref == 0) { 1028191673Sjamie if (!(flags & JAIL_DYING)) { 1029191673Sjamie mtx_unlock(&pr->pr_mtx); 1030191673Sjamie error = ENOENT; 1031191673Sjamie vfs_opterror(opts, "jail %d is dying", 1032191673Sjamie jid); 1033191673Sjamie goto done_unlock_list; 1034191673Sjamie } else if ((flags & JAIL_ATTACH) || 1035191673Sjamie (pr_flags & PR_PERSIST)) { 1036191673Sjamie /* 1037191673Sjamie * A dying jail might be resurrected 1038191673Sjamie * (via attach or persist), but first 1039191673Sjamie * it must determine if another jail 1040191673Sjamie * has claimed its name. Accomplish 1041191673Sjamie * this by implicitly re-setting the 1042191673Sjamie * name. 1043191673Sjamie */ 1044191673Sjamie if (name == NULL) 1045192895Sjamie name = prison_name(mypr, pr); 1046191673Sjamie } 1047191673Sjamie } 1048191673Sjamie } 1049191673Sjamie if (pr == NULL) { 1050191673Sjamie /* Update: jid must exist. */ 1051191673Sjamie if (cuflags == JAIL_UPDATE) { 1052191673Sjamie error = ENOENT; 1053191673Sjamie vfs_opterror(opts, "jail %d not found", jid); 1054191673Sjamie goto done_unlock_list; 1055191673Sjamie } 1056191673Sjamie } 1057191673Sjamie } 1058191673Sjamie /* 1059191673Sjamie * If the caller provided a name, look for a jail by that name. 1060191673Sjamie * This has different semantics for creates and updates keyed by jid 1061191673Sjamie * (where the name must not already exist in a different jail), 1062191673Sjamie * and updates keyed by the name itself (where the name must exist 1063191673Sjamie * because that is the jail being updated). 1064191673Sjamie */ 1065191673Sjamie if (name != NULL) { 1066196835Sjamie namelc = strrchr(name, '.'); 1067196835Sjamie if (namelc == NULL) 1068196835Sjamie namelc = name; 1069196835Sjamie else { 1070192895Sjamie /* 1071192895Sjamie * This is a hierarchical name. Split it into the 1072192895Sjamie * parent and child names, and make sure the parent 1073192895Sjamie * exists or matches an already found jail. 1074192895Sjamie */ 1075196835Sjamie *namelc = '\0'; 1076192895Sjamie if (pr != NULL) { 1077196835Sjamie if (strncmp(name, ppr->pr_name, namelc - name) 1078196835Sjamie || ppr->pr_name[namelc - name] != '\0') { 1079192895Sjamie mtx_unlock(&pr->pr_mtx); 1080192895Sjamie error = EINVAL; 1081192895Sjamie vfs_opterror(opts, 1082192895Sjamie "cannot change jail's parent"); 1083192895Sjamie goto done_unlock_list; 1084192895Sjamie } 1085192895Sjamie } else { 1086192895Sjamie ppr = prison_find_name(mypr, name); 1087192895Sjamie if (ppr == NULL) { 1088192895Sjamie error = ENOENT; 1089192895Sjamie vfs_opterror(opts, 1090192895Sjamie "jail \"%s\" not found", name); 1091192895Sjamie goto done_unlock_list; 1092192895Sjamie } 1093192895Sjamie mtx_unlock(&ppr->pr_mtx); 1094192895Sjamie } 1095196835Sjamie name = ++namelc; 1096192895Sjamie } 1097191673Sjamie if (name[0] != '\0') { 1098192895Sjamie namelen = 1099192895Sjamie (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 1100192895Sjamie name_again: 1101191673Sjamie deadpr = NULL; 1102192895Sjamie FOREACH_PRISON_CHILD(ppr, tpr) { 1103191673Sjamie if (tpr != pr && tpr->pr_ref > 0 && 1104192895Sjamie !strcmp(tpr->pr_name + namelen, name)) { 1105191673Sjamie if (pr == NULL && 1106191673Sjamie cuflags != JAIL_CREATE) { 1107191673Sjamie mtx_lock(&tpr->pr_mtx); 1108191673Sjamie if (tpr->pr_ref > 0) { 1109191673Sjamie /* 1110191673Sjamie * Use this jail 1111191673Sjamie * for updates. 1112191673Sjamie */ 1113191673Sjamie if (tpr->pr_uref > 0) { 1114191673Sjamie pr = tpr; 1115191673Sjamie break; 1116191673Sjamie } 1117191673Sjamie deadpr = tpr; 1118191673Sjamie } 1119191673Sjamie mtx_unlock(&tpr->pr_mtx); 1120191673Sjamie } else if (tpr->pr_uref > 0) { 1121191673Sjamie /* 1122191673Sjamie * Create, or update(jid): 1123191673Sjamie * name must not exist in an 1124192895Sjamie * active sibling jail. 1125191673Sjamie */ 1126191673Sjamie error = EEXIST; 1127191673Sjamie if (pr != NULL) 1128191673Sjamie mtx_unlock(&pr->pr_mtx); 1129191673Sjamie vfs_opterror(opts, 1130191673Sjamie "jail \"%s\" already exists", 1131191673Sjamie name); 1132191673Sjamie goto done_unlock_list; 1133191673Sjamie } 1134191673Sjamie } 1135191673Sjamie } 1136191673Sjamie /* If no active jail is found, use a dying one. */ 1137191673Sjamie if (deadpr != NULL && pr == NULL) { 1138191673Sjamie if (flags & JAIL_DYING) { 1139191673Sjamie mtx_lock(&deadpr->pr_mtx); 1140191673Sjamie if (deadpr->pr_ref == 0) { 1141191673Sjamie mtx_unlock(&deadpr->pr_mtx); 1142191673Sjamie goto name_again; 1143191673Sjamie } 1144191673Sjamie pr = deadpr; 1145191673Sjamie } else if (cuflags == JAIL_UPDATE) { 1146191673Sjamie error = ENOENT; 1147191673Sjamie vfs_opterror(opts, 1148191673Sjamie "jail \"%s\" is dying", name); 1149191673Sjamie goto done_unlock_list; 1150191673Sjamie } 1151191673Sjamie } 1152191673Sjamie /* Update: name must exist if no jid. */ 1153191673Sjamie else if (cuflags == JAIL_UPDATE && pr == NULL) { 1154191673Sjamie error = ENOENT; 1155191673Sjamie vfs_opterror(opts, "jail \"%s\" not found", 1156191673Sjamie name); 1157191673Sjamie goto done_unlock_list; 1158191673Sjamie } 1159191673Sjamie } 1160191673Sjamie } 1161191673Sjamie /* Update: must provide a jid or name. */ 1162191673Sjamie else if (cuflags == JAIL_UPDATE && pr == NULL) { 1163191673Sjamie error = ENOENT; 1164191673Sjamie vfs_opterror(opts, "update specified no jail"); 1165191673Sjamie goto done_unlock_list; 1166191673Sjamie } 1167185435Sbz 1168191673Sjamie /* If there's no prison to update, create a new one and link it in. */ 1169191673Sjamie if (pr == NULL) { 1170194762Sjamie for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent) 1171194762Sjamie if (tpr->pr_childcount >= tpr->pr_childmax) { 1172194762Sjamie error = EPERM; 1173194762Sjamie vfs_opterror(opts, "prison limit exceeded"); 1174194762Sjamie goto done_unlock_list; 1175194762Sjamie } 1176191673Sjamie created = 1; 1177192895Sjamie mtx_lock(&ppr->pr_mtx); 1178192895Sjamie if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) { 1179192895Sjamie mtx_unlock(&ppr->pr_mtx); 1180192895Sjamie error = ENOENT; 1181192895Sjamie vfs_opterror(opts, "parent jail went away!"); 1182192895Sjamie goto done_unlock_list; 1183192895Sjamie } 1184192895Sjamie ppr->pr_ref++; 1185192895Sjamie ppr->pr_uref++; 1186192895Sjamie mtx_unlock(&ppr->pr_mtx); 1187191673Sjamie pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO); 1188191673Sjamie if (jid == 0) { 1189191673Sjamie /* Find the next free jid. */ 1190191673Sjamie jid = lastprid + 1; 1191191673Sjamie findnext: 1192191673Sjamie if (jid == JAIL_MAX) 1193191673Sjamie jid = 1; 1194191673Sjamie TAILQ_FOREACH(tpr, &allprison, pr_list) { 1195191673Sjamie if (tpr->pr_id < jid) 1196191673Sjamie continue; 1197191673Sjamie if (tpr->pr_id > jid || tpr->pr_ref == 0) { 1198191673Sjamie TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1199191673Sjamie break; 1200191673Sjamie } 1201191673Sjamie if (jid == lastprid) { 1202191673Sjamie error = EAGAIN; 1203191673Sjamie vfs_opterror(opts, 1204191673Sjamie "no available jail IDs"); 1205191673Sjamie free(pr, M_PRISON); 1206192895Sjamie prison_deref(ppr, PD_DEREF | 1207192895Sjamie PD_DEUREF | PD_LIST_XLOCKED); 1208192895Sjamie goto done_releroot; 1209191673Sjamie } 1210191673Sjamie jid++; 1211191673Sjamie goto findnext; 1212191673Sjamie } 1213191673Sjamie lastprid = jid; 1214191673Sjamie } else { 1215191673Sjamie /* 1216191673Sjamie * The jail already has a jid (that did not yet exist), 1217191673Sjamie * so just find where to insert it. 1218191673Sjamie */ 1219191673Sjamie TAILQ_FOREACH(tpr, &allprison, pr_list) 1220191673Sjamie if (tpr->pr_id >= jid) { 1221191673Sjamie TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1222191673Sjamie break; 1223191673Sjamie } 1224191673Sjamie } 1225191673Sjamie if (tpr == NULL) 1226191673Sjamie TAILQ_INSERT_TAIL(&allprison, pr, pr_list); 1227192895Sjamie LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling); 1228192895Sjamie for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 1229194762Sjamie tpr->pr_childcount++; 1230185435Sbz 1231192895Sjamie pr->pr_parent = ppr; 1232191673Sjamie pr->pr_id = jid; 1233192895Sjamie 1234192895Sjamie /* Set some default values, and inherit some from the parent. */ 1235191673Sjamie if (name == NULL) 1236191673Sjamie name = ""; 1237191673Sjamie if (path == NULL) { 1238191673Sjamie path = "/"; 1239192895Sjamie root = mypr->pr_root; 1240191673Sjamie vref(root); 1241191673Sjamie } 1242195944Sjamie strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN); 1243195944Sjamie pr->pr_flags |= PR_HOST; 1244195945Sjamie#if defined(INET) || defined(INET6) 1245195945Sjamie#ifdef VIMAGE 1246195945Sjamie if (!(pr_flags & PR_VNET)) 1247195945Sjamie#endif 1248195945Sjamie { 1249192895Sjamie#ifdef INET 1250195974Sjamie if (!(ch_flags & PR_IP4_USER)) 1251195974Sjamie pr->pr_flags |= 1252195974Sjamie PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE; 1253195974Sjamie else if (!(pr_flags & PR_IP4_USER)) { 1254195974Sjamie pr->pr_flags |= ppr->pr_flags & PR_IP4; 1255195974Sjamie if (ppr->pr_ip4 != NULL) { 1256195974Sjamie pr->pr_ip4s = ppr->pr_ip4s; 1257195974Sjamie pr->pr_ip4 = malloc(pr->pr_ip4s * 1258195974Sjamie sizeof(struct in_addr), M_PRISON, 1259195974Sjamie M_WAITOK); 1260195974Sjamie bcopy(ppr->pr_ip4, pr->pr_ip4, 1261195974Sjamie pr->pr_ip4s * sizeof(*pr->pr_ip4)); 1262195974Sjamie } 1263195974Sjamie } 1264192895Sjamie#endif 1265192895Sjamie#ifdef INET6 1266195974Sjamie if (!(ch_flags & PR_IP6_USER)) 1267195974Sjamie pr->pr_flags |= 1268195974Sjamie PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE; 1269195974Sjamie else if (!(pr_flags & PR_IP6_USER)) { 1270195974Sjamie pr->pr_flags |= ppr->pr_flags & PR_IP6; 1271195974Sjamie if (ppr->pr_ip6 != NULL) { 1272195974Sjamie pr->pr_ip6s = ppr->pr_ip6s; 1273195974Sjamie pr->pr_ip6 = malloc(pr->pr_ip6s * 1274195974Sjamie sizeof(struct in6_addr), M_PRISON, 1275195974Sjamie M_WAITOK); 1276195974Sjamie bcopy(ppr->pr_ip6, pr->pr_ip6, 1277195974Sjamie pr->pr_ip6s * sizeof(*pr->pr_ip6)); 1278195974Sjamie } 1279195974Sjamie } 1280192895Sjamie#endif 1281195945Sjamie } 1282195945Sjamie#endif 1283202468Sbz /* Source address selection is always on by default. */ 1284202468Sbz pr->pr_flags |= _PR_IP_SADDRSEL; 1285202468Sbz 1286192895Sjamie pr->pr_securelevel = ppr->pr_securelevel; 1287192895Sjamie pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow; 1288196002Sjamie pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 1289232059Smm pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum; 1290191673Sjamie 1291192895Sjamie LIST_INIT(&pr->pr_children); 1292192895Sjamie mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK); 1293191673Sjamie 1294194251Sjamie#ifdef VIMAGE 1295194251Sjamie /* Allocate a new vnet if specified. */ 1296194251Sjamie pr->pr_vnet = (pr_flags & PR_VNET) 1297194251Sjamie ? vnet_alloc() : ppr->pr_vnet; 1298194251Sjamie#endif 1299185435Sbz /* 1300191673Sjamie * Allocate a dedicated cpuset for each jail. 1301191673Sjamie * Unlike other initial settings, this may return an erorr. 1302185435Sbz */ 1303192895Sjamie error = cpuset_create_root(ppr, &pr->pr_cpuset); 1304191673Sjamie if (error) { 1305191673Sjamie prison_deref(pr, PD_LIST_XLOCKED); 1306191673Sjamie goto done_releroot; 1307191673Sjamie } 1308185435Sbz 1309191673Sjamie mtx_lock(&pr->pr_mtx); 1310185435Sbz /* 1311191673Sjamie * New prisons do not yet have a reference, because we do not 1312191673Sjamie * want other to see the incomplete prison once the 1313191673Sjamie * allprison_lock is downgraded. 1314185435Sbz */ 1315191673Sjamie } else { 1316191673Sjamie created = 0; 1317195974Sjamie /* 1318195974Sjamie * Grab a reference for existing prisons, to ensure they 1319195974Sjamie * continue to exist for the duration of the call. 1320195974Sjamie */ 1321195974Sjamie pr->pr_ref++; 1322195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6)) 1323195945Sjamie if ((pr->pr_flags & PR_VNET) && 1324195945Sjamie (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 1325195945Sjamie error = EINVAL; 1326195945Sjamie vfs_opterror(opts, 1327195945Sjamie "vnet jails cannot have IP address restrictions"); 1328195945Sjamie goto done_deref_locked; 1329195945Sjamie } 1330195945Sjamie#endif 1331195974Sjamie#ifdef INET 1332195974Sjamie if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 1333195974Sjamie error = EINVAL; 1334195974Sjamie vfs_opterror(opts, 1335195974Sjamie "ip4 cannot be changed after creation"); 1336195974Sjamie goto done_deref_locked; 1337195974Sjamie } 1338195974Sjamie#endif 1339195974Sjamie#ifdef INET6 1340195974Sjamie if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 1341195974Sjamie error = EINVAL; 1342195974Sjamie vfs_opterror(opts, 1343195974Sjamie "ip6 cannot be changed after creation"); 1344195974Sjamie goto done_deref_locked; 1345195974Sjamie } 1346195974Sjamie#endif 1347191673Sjamie } 1348185435Sbz 1349191673Sjamie /* Do final error checking before setting anything. */ 1350192895Sjamie if (gotslevel) { 1351192895Sjamie if (slevel < ppr->pr_securelevel) { 1352192895Sjamie error = EPERM; 1353192895Sjamie goto done_deref_locked; 1354192895Sjamie } 1355192895Sjamie } 1356194762Sjamie if (gotchildmax) { 1357194762Sjamie if (childmax >= ppr->pr_childmax) { 1358194762Sjamie error = EPERM; 1359194762Sjamie goto done_deref_locked; 1360194762Sjamie } 1361194762Sjamie } 1362192895Sjamie if (gotenforce) { 1363192895Sjamie if (enforce < ppr->pr_enforce_statfs) { 1364192895Sjamie error = EPERM; 1365192895Sjamie goto done_deref_locked; 1366192895Sjamie } 1367192895Sjamie } 1368231267Smm if (gotrsnum) { 1369231267Smm /* 1370231267Smm * devfs_rsnum is a uint16_t 1371231267Smm */ 1372232059Smm if (rsnum < 0 || rsnum > 65535) { 1373231267Smm error = EINVAL; 1374231267Smm goto done_deref_locked; 1375231267Smm } 1376231267Smm /* 1377232059Smm * Nested jails always inherit parent's devfs ruleset 1378231267Smm */ 1379231267Smm if (jailed(td->td_ucred)) { 1380231267Smm if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) { 1381231267Smm error = EPERM; 1382231267Smm goto done_deref_locked; 1383232059Smm } else 1384231267Smm rsnum = ppr->pr_devfs_rsnum; 1385231267Smm } 1386231267Smm } 1387185435Sbz#ifdef INET 1388195974Sjamie if (ip4s > 0) { 1389192895Sjamie if (ppr->pr_flags & PR_IP4) { 1390195974Sjamie /* 1391195974Sjamie * Make sure the new set of IP addresses is a 1392195974Sjamie * subset of the parent's list. Don't worry 1393195974Sjamie * about the parent being unlocked, as any 1394195974Sjamie * setting is done with allprison_lock held. 1395195974Sjamie */ 1396195974Sjamie for (ij = 0; ij < ppr->pr_ip4s; ij++) 1397195974Sjamie if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 1398195974Sjamie break; 1399195974Sjamie if (ij == ppr->pr_ip4s) { 1400195974Sjamie error = EPERM; 1401195974Sjamie goto done_deref_locked; 1402195974Sjamie } 1403195974Sjamie if (ip4s > 1) { 1404195974Sjamie for (ii = ij = 1; ii < ip4s; ii++) { 1405195974Sjamie if (ip4[ii].s_addr == 1406195974Sjamie ppr->pr_ip4[0].s_addr) 1407195974Sjamie continue; 1408195974Sjamie for (; ij < ppr->pr_ip4s; ij++) 1409195974Sjamie if (ip4[ii].s_addr == 1410195974Sjamie ppr->pr_ip4[ij].s_addr) 1411195974Sjamie break; 1412195974Sjamie if (ij == ppr->pr_ip4s) 1413195974Sjamie break; 1414192895Sjamie } 1415192895Sjamie if (ij == ppr->pr_ip4s) { 1416192895Sjamie error = EPERM; 1417192895Sjamie goto done_deref_locked; 1418192895Sjamie } 1419192895Sjamie } 1420192895Sjamie } 1421195974Sjamie /* 1422195974Sjamie * Check for conflicting IP addresses. We permit them 1423195974Sjamie * if there is no more than one IP on each jail. If 1424195974Sjamie * there is a duplicate on a jail with more than one 1425195974Sjamie * IP stop checking and return error. 1426195974Sjamie */ 1427195974Sjamie tppr = ppr; 1428195945Sjamie#ifdef VIMAGE 1429195974Sjamie for (; tppr != &prison0; tppr = tppr->pr_parent) 1430195974Sjamie if (tppr->pr_flags & PR_VNET) 1431195974Sjamie break; 1432195945Sjamie#endif 1433195974Sjamie FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1434195974Sjamie if (tpr == pr || 1435195945Sjamie#ifdef VIMAGE 1436195974Sjamie (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1437195945Sjamie#endif 1438195974Sjamie tpr->pr_uref == 0) { 1439192895Sjamie descend = 0; 1440195974Sjamie continue; 1441195974Sjamie } 1442195974Sjamie if (!(tpr->pr_flags & PR_IP4_USER)) 1443195974Sjamie continue; 1444195974Sjamie descend = 0; 1445195974Sjamie if (tpr->pr_ip4 == NULL || 1446195974Sjamie (ip4s == 1 && tpr->pr_ip4s == 1)) 1447195974Sjamie continue; 1448195974Sjamie for (ii = 0; ii < ip4s; ii++) { 1449195974Sjamie if (_prison_check_ip4(tpr, &ip4[ii]) == 0) { 1450195974Sjamie error = EADDRINUSE; 1451195974Sjamie vfs_opterror(opts, 1452195974Sjamie "IPv4 addresses clash"); 1453195974Sjamie goto done_deref_locked; 1454192895Sjamie } 1455192895Sjamie } 1456192895Sjamie } 1457192895Sjamie } 1458185435Sbz#endif 1459191673Sjamie#ifdef INET6 1460195974Sjamie if (ip6s > 0) { 1461192895Sjamie if (ppr->pr_flags & PR_IP6) { 1462195974Sjamie /* 1463195974Sjamie * Make sure the new set of IP addresses is a 1464195974Sjamie * subset of the parent's list. 1465195974Sjamie */ 1466195974Sjamie for (ij = 0; ij < ppr->pr_ip6s; ij++) 1467195974Sjamie if (IN6_ARE_ADDR_EQUAL(&ip6[0], 1468195974Sjamie &ppr->pr_ip6[ij])) 1469195974Sjamie break; 1470195974Sjamie if (ij == ppr->pr_ip6s) { 1471195974Sjamie error = EPERM; 1472195974Sjamie goto done_deref_locked; 1473195974Sjamie } 1474195974Sjamie if (ip6s > 1) { 1475195974Sjamie for (ii = ij = 1; ii < ip6s; ii++) { 1476195974Sjamie if (IN6_ARE_ADDR_EQUAL(&ip6[ii], 1477195974Sjamie &ppr->pr_ip6[0])) 1478195974Sjamie continue; 1479195974Sjamie for (; ij < ppr->pr_ip6s; ij++) 1480195974Sjamie if (IN6_ARE_ADDR_EQUAL( 1481195974Sjamie &ip6[ii], &ppr->pr_ip6[ij])) 1482195974Sjamie break; 1483195974Sjamie if (ij == ppr->pr_ip6s) 1484195974Sjamie break; 1485192895Sjamie } 1486192895Sjamie if (ij == ppr->pr_ip6s) { 1487192895Sjamie error = EPERM; 1488192895Sjamie goto done_deref_locked; 1489192895Sjamie } 1490192895Sjamie } 1491192895Sjamie } 1492195974Sjamie /* Check for conflicting IP addresses. */ 1493195974Sjamie tppr = ppr; 1494195945Sjamie#ifdef VIMAGE 1495195974Sjamie for (; tppr != &prison0; tppr = tppr->pr_parent) 1496195974Sjamie if (tppr->pr_flags & PR_VNET) 1497195974Sjamie break; 1498195945Sjamie#endif 1499195974Sjamie FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1500195974Sjamie if (tpr == pr || 1501195945Sjamie#ifdef VIMAGE 1502195974Sjamie (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1503195945Sjamie#endif 1504195974Sjamie tpr->pr_uref == 0) { 1505192895Sjamie descend = 0; 1506195974Sjamie continue; 1507195974Sjamie } 1508195974Sjamie if (!(tpr->pr_flags & PR_IP6_USER)) 1509195974Sjamie continue; 1510195974Sjamie descend = 0; 1511195974Sjamie if (tpr->pr_ip6 == NULL || 1512195974Sjamie (ip6s == 1 && tpr->pr_ip6s == 1)) 1513195974Sjamie continue; 1514195974Sjamie for (ii = 0; ii < ip6s; ii++) { 1515195974Sjamie if (_prison_check_ip6(tpr, &ip6[ii]) == 0) { 1516195974Sjamie error = EADDRINUSE; 1517195974Sjamie vfs_opterror(opts, 1518195974Sjamie "IPv6 addresses clash"); 1519195974Sjamie goto done_deref_locked; 1520192895Sjamie } 1521192895Sjamie } 1522191673Sjamie } 1523192895Sjamie } 1524191673Sjamie#endif 1525192895Sjamie onamelen = namelen = 0; 1526192895Sjamie if (name != NULL) { 1527191673Sjamie /* Give a default name of the jid. */ 1528191673Sjamie if (name[0] == '\0') 1529191673Sjamie snprintf(name = numbuf, sizeof(numbuf), "%d", jid); 1530196835Sjamie else if (*namelc == '0' || (strtoul(namelc, &p, 10) != jid && 1531196835Sjamie *p == '\0')) { 1532191673Sjamie error = EINVAL; 1533196835Sjamie vfs_opterror(opts, 1534196835Sjamie "name cannot be numeric (unless it is the jid)"); 1535192895Sjamie goto done_deref_locked; 1536191673Sjamie } 1537191673Sjamie /* 1538192895Sjamie * Make sure the name isn't too long for the prison or its 1539192895Sjamie * children. 1540191673Sjamie */ 1541192895Sjamie onamelen = strlen(pr->pr_name); 1542192895Sjamie namelen = strlen(name); 1543192895Sjamie if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) { 1544192895Sjamie error = ENAMETOOLONG; 1545192895Sjamie goto done_deref_locked; 1546192895Sjamie } 1547192895Sjamie FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 1548192895Sjamie if (strlen(tpr->pr_name) + (namelen - onamelen) >= 1549192895Sjamie sizeof(pr->pr_name)) { 1550192895Sjamie error = ENAMETOOLONG; 1551192895Sjamie goto done_deref_locked; 1552192895Sjamie } 1553192895Sjamie } 1554191673Sjamie } 1555192895Sjamie if (pr_allow & ~ppr->pr_allow) { 1556192895Sjamie error = EPERM; 1557192895Sjamie goto done_deref_locked; 1558192895Sjamie } 1559185435Sbz 1560191673Sjamie /* Set the parameters of the prison. */ 1561191673Sjamie#ifdef INET 1562192895Sjamie redo_ip4 = 0; 1563195974Sjamie if (pr_flags & PR_IP4_USER) { 1564195974Sjamie pr->pr_flags |= PR_IP4; 1565195974Sjamie free(pr->pr_ip4, M_PRISON); 1566195974Sjamie pr->pr_ip4s = ip4s; 1567195974Sjamie pr->pr_ip4 = ip4; 1568195974Sjamie ip4 = NULL; 1569192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1570195945Sjamie#ifdef VIMAGE 1571195945Sjamie if (tpr->pr_flags & PR_VNET) { 1572195945Sjamie descend = 0; 1573195945Sjamie continue; 1574195945Sjamie } 1575195945Sjamie#endif 1576192895Sjamie if (prison_restrict_ip4(tpr, NULL)) { 1577192895Sjamie redo_ip4 = 1; 1578192895Sjamie descend = 0; 1579192895Sjamie } 1580192895Sjamie } 1581185435Sbz } 1582191673Sjamie#endif 1583191673Sjamie#ifdef INET6 1584192895Sjamie redo_ip6 = 0; 1585195974Sjamie if (pr_flags & PR_IP6_USER) { 1586195974Sjamie pr->pr_flags |= PR_IP6; 1587195974Sjamie free(pr->pr_ip6, M_PRISON); 1588195974Sjamie pr->pr_ip6s = ip6s; 1589195974Sjamie pr->pr_ip6 = ip6; 1590195974Sjamie ip6 = NULL; 1591192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1592195945Sjamie#ifdef VIMAGE 1593195945Sjamie if (tpr->pr_flags & PR_VNET) { 1594195945Sjamie descend = 0; 1595195945Sjamie continue; 1596195945Sjamie } 1597195945Sjamie#endif 1598192895Sjamie if (prison_restrict_ip6(tpr, NULL)) { 1599192895Sjamie redo_ip6 = 1; 1600192895Sjamie descend = 0; 1601192895Sjamie } 1602192895Sjamie } 1603191673Sjamie } 1604191673Sjamie#endif 1605192895Sjamie if (gotslevel) { 1606191673Sjamie pr->pr_securelevel = slevel; 1607192895Sjamie /* Set all child jails to be at least this level. */ 1608192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1609192895Sjamie if (tpr->pr_securelevel < slevel) 1610192895Sjamie tpr->pr_securelevel = slevel; 1611192895Sjamie } 1612194762Sjamie if (gotchildmax) { 1613194762Sjamie pr->pr_childmax = childmax; 1614194762Sjamie /* Set all child jails to under this limit. */ 1615194762Sjamie FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level) 1616194762Sjamie if (tpr->pr_childmax > childmax - level) 1617194762Sjamie tpr->pr_childmax = childmax > level 1618194762Sjamie ? childmax - level : 0; 1619194762Sjamie } 1620192895Sjamie if (gotenforce) { 1621192895Sjamie pr->pr_enforce_statfs = enforce; 1622192895Sjamie /* Pass this restriction on to the children. */ 1623192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1624192895Sjamie if (tpr->pr_enforce_statfs < enforce) 1625192895Sjamie tpr->pr_enforce_statfs = enforce; 1626192895Sjamie } 1627231267Smm if (gotrsnum) { 1628231267Smm pr->pr_devfs_rsnum = rsnum; 1629231267Smm /* Pass this restriction on to the children. */ 1630231267Smm FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1631232059Smm tpr->pr_devfs_rsnum = rsnum; 1632231267Smm } 1633192895Sjamie if (name != NULL) { 1634192895Sjamie if (ppr == &prison0) 1635192895Sjamie strlcpy(pr->pr_name, name, sizeof(pr->pr_name)); 1636192895Sjamie else 1637192895Sjamie snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s", 1638192895Sjamie ppr->pr_name, name); 1639192895Sjamie /* Change this component of child names. */ 1640192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1641192895Sjamie bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen, 1642192895Sjamie strlen(tpr->pr_name + onamelen) + 1); 1643192895Sjamie bcopy(pr->pr_name, tpr->pr_name, namelen); 1644192895Sjamie } 1645192895Sjamie } 1646191673Sjamie if (path != NULL) { 1647192895Sjamie /* Try to keep a real-rooted full pathname. */ 1648230129Smm if (fullpath_disabled && path[0] == '/' && 1649230129Smm strcmp(mypr->pr_path, "/")) 1650192895Sjamie snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s", 1651192895Sjamie mypr->pr_path, path); 1652192895Sjamie else 1653192895Sjamie strlcpy(pr->pr_path, path, sizeof(pr->pr_path)); 1654191673Sjamie pr->pr_root = root; 1655191673Sjamie } 1656193066Sjamie if (PR_HOST & ch_flags & ~pr_flags) { 1657193066Sjamie if (pr->pr_flags & PR_HOST) { 1658193066Sjamie /* 1659193066Sjamie * Copy the parent's host info. As with pr_ip4 above, 1660193066Sjamie * the lack of a lock on the parent is not a problem; 1661193066Sjamie * it is always set with allprison_lock at least 1662193066Sjamie * shared, and is held exclusively here. 1663193066Sjamie */ 1664194118Sjamie strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname, 1665194118Sjamie sizeof(pr->pr_hostname)); 1666194118Sjamie strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname, 1667194118Sjamie sizeof(pr->pr_domainname)); 1668194118Sjamie strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid, 1669194118Sjamie sizeof(pr->pr_hostuuid)); 1670193066Sjamie pr->pr_hostid = pr->pr_parent->pr_hostid; 1671193066Sjamie } 1672193066Sjamie } else if (host != NULL || domain != NULL || uuid != NULL || gothid) { 1673193066Sjamie /* Set this prison, and any descendants without PR_HOST. */ 1674193066Sjamie if (host != NULL) 1675194118Sjamie strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname)); 1676193066Sjamie if (domain != NULL) 1677194118Sjamie strlcpy(pr->pr_domainname, domain, 1678194118Sjamie sizeof(pr->pr_domainname)); 1679193066Sjamie if (uuid != NULL) 1680194118Sjamie strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid)); 1681193066Sjamie if (gothid) 1682193066Sjamie pr->pr_hostid = hid; 1683193066Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1684193066Sjamie if (tpr->pr_flags & PR_HOST) 1685193066Sjamie descend = 0; 1686193066Sjamie else { 1687193066Sjamie if (host != NULL) 1688194118Sjamie strlcpy(tpr->pr_hostname, 1689194118Sjamie pr->pr_hostname, 1690194118Sjamie sizeof(tpr->pr_hostname)); 1691193066Sjamie if (domain != NULL) 1692194118Sjamie strlcpy(tpr->pr_domainname, 1693194118Sjamie pr->pr_domainname, 1694194118Sjamie sizeof(tpr->pr_domainname)); 1695193066Sjamie if (uuid != NULL) 1696194118Sjamie strlcpy(tpr->pr_hostuuid, 1697194118Sjamie pr->pr_hostuuid, 1698194118Sjamie sizeof(tpr->pr_hostuuid)); 1699193066Sjamie if (gothid) 1700193066Sjamie tpr->pr_hostid = hid; 1701193066Sjamie } 1702193066Sjamie } 1703193066Sjamie } 1704192895Sjamie if ((tallow = ch_allow & ~pr_allow)) { 1705192895Sjamie /* Clear allow bits in all children. */ 1706192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1707192895Sjamie tpr->pr_allow &= ~tallow; 1708192895Sjamie } 1709192895Sjamie pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow; 1710191673Sjamie /* 1711191673Sjamie * Persistent prisons get an extra reference, and prisons losing their 1712191673Sjamie * persist flag lose that reference. Only do this for existing prisons 1713191673Sjamie * for now, so new ones will remain unseen until after the module 1714191673Sjamie * handlers have completed. 1715191673Sjamie */ 1716191673Sjamie if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) { 1717191673Sjamie if (pr_flags & PR_PERSIST) { 1718191673Sjamie pr->pr_ref++; 1719191673Sjamie pr->pr_uref++; 1720191673Sjamie } else { 1721191673Sjamie pr->pr_ref--; 1722191673Sjamie pr->pr_uref--; 1723191673Sjamie } 1724191673Sjamie } 1725191673Sjamie pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags; 1726191673Sjamie mtx_unlock(&pr->pr_mtx); 1727185435Sbz 1728221362Strasz#ifdef RACCT 1729221362Strasz if (created) 1730221362Strasz prison_racct_attach(pr); 1731221362Strasz#endif 1732221362Strasz 1733192895Sjamie /* Locks may have prevented a complete restriction of child IP 1734192895Sjamie * addresses. If so, allocate some more memory and try again. 1735192895Sjamie */ 1736192895Sjamie#ifdef INET 1737192895Sjamie while (redo_ip4) { 1738192895Sjamie ip4s = pr->pr_ip4s; 1739192895Sjamie ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 1740192895Sjamie mtx_lock(&pr->pr_mtx); 1741192895Sjamie redo_ip4 = 0; 1742192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1743195945Sjamie#ifdef VIMAGE 1744195945Sjamie if (tpr->pr_flags & PR_VNET) { 1745195945Sjamie descend = 0; 1746195945Sjamie continue; 1747195945Sjamie } 1748195945Sjamie#endif 1749192895Sjamie if (prison_restrict_ip4(tpr, ip4)) { 1750192895Sjamie if (ip4 != NULL) 1751192895Sjamie ip4 = NULL; 1752192895Sjamie else 1753192895Sjamie redo_ip4 = 1; 1754192895Sjamie } 1755192895Sjamie } 1756192895Sjamie mtx_unlock(&pr->pr_mtx); 1757192895Sjamie } 1758192895Sjamie#endif 1759192895Sjamie#ifdef INET6 1760192895Sjamie while (redo_ip6) { 1761192895Sjamie ip6s = pr->pr_ip6s; 1762192895Sjamie ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 1763192895Sjamie mtx_lock(&pr->pr_mtx); 1764192895Sjamie redo_ip6 = 0; 1765192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1766195945Sjamie#ifdef VIMAGE 1767195945Sjamie if (tpr->pr_flags & PR_VNET) { 1768195945Sjamie descend = 0; 1769195945Sjamie continue; 1770195945Sjamie } 1771195945Sjamie#endif 1772192895Sjamie if (prison_restrict_ip6(tpr, ip6)) { 1773192895Sjamie if (ip6 != NULL) 1774192895Sjamie ip6 = NULL; 1775192895Sjamie else 1776192895Sjamie redo_ip6 = 1; 1777192895Sjamie } 1778192895Sjamie } 1779192895Sjamie mtx_unlock(&pr->pr_mtx); 1780192895Sjamie } 1781192895Sjamie#endif 1782192895Sjamie 1783191673Sjamie /* Let the modules do their work. */ 1784191673Sjamie sx_downgrade(&allprison_lock); 1785191673Sjamie if (created) { 1786191673Sjamie error = osd_jail_call(pr, PR_METHOD_CREATE, opts); 1787191673Sjamie if (error) { 1788191673Sjamie prison_deref(pr, PD_LIST_SLOCKED); 1789191673Sjamie goto done_errmsg; 1790191673Sjamie } 1791191673Sjamie } 1792191673Sjamie error = osd_jail_call(pr, PR_METHOD_SET, opts); 1793191673Sjamie if (error) { 1794191673Sjamie prison_deref(pr, created 1795191673Sjamie ? PD_LIST_SLOCKED 1796191673Sjamie : PD_DEREF | PD_LIST_SLOCKED); 1797191673Sjamie goto done_errmsg; 1798191673Sjamie } 1799191673Sjamie 1800191673Sjamie /* Attach this process to the prison if requested. */ 1801191673Sjamie if (flags & JAIL_ATTACH) { 1802191673Sjamie mtx_lock(&pr->pr_mtx); 1803191673Sjamie error = do_jail_attach(td, pr); 1804191673Sjamie if (error) { 1805191673Sjamie vfs_opterror(opts, "attach failed"); 1806191673Sjamie if (!created) 1807191673Sjamie prison_deref(pr, PD_DEREF); 1808191673Sjamie goto done_errmsg; 1809191673Sjamie } 1810191673Sjamie } 1811191673Sjamie 1812235803Strasz#ifdef RACCT 1813235803Strasz if (!created) { 1814271622Strasz if (!(flags & JAIL_ATTACH)) 1815271622Strasz sx_sunlock(&allprison_lock); 1816235803Strasz prison_racct_modify(pr); 1817271622Strasz if (!(flags & JAIL_ATTACH)) 1818271622Strasz sx_slock(&allprison_lock); 1819235803Strasz } 1820235803Strasz#endif 1821235803Strasz 1822235803Strasz td->td_retval[0] = pr->pr_id; 1823235803Strasz 1824191673Sjamie /* 1825191673Sjamie * Now that it is all there, drop the temporary reference from existing 1826191673Sjamie * prisons. Or add a reference to newly created persistent prisons 1827191673Sjamie * (which was not done earlier so that the prison would not be publicly 1828191673Sjamie * visible). 1829191673Sjamie */ 1830191673Sjamie if (!created) { 1831191673Sjamie prison_deref(pr, (flags & JAIL_ATTACH) 1832191673Sjamie ? PD_DEREF 1833191673Sjamie : PD_DEREF | PD_LIST_SLOCKED); 1834191673Sjamie } else { 1835191673Sjamie if (pr_flags & PR_PERSIST) { 1836191673Sjamie mtx_lock(&pr->pr_mtx); 1837191673Sjamie pr->pr_ref++; 1838191673Sjamie pr->pr_uref++; 1839191673Sjamie mtx_unlock(&pr->pr_mtx); 1840191673Sjamie } 1841191673Sjamie if (!(flags & JAIL_ATTACH)) 1842191673Sjamie sx_sunlock(&allprison_lock); 1843191673Sjamie } 1844232598Strasz 1845191673Sjamie goto done_errmsg; 1846191673Sjamie 1847192895Sjamie done_deref_locked: 1848192895Sjamie prison_deref(pr, created 1849192895Sjamie ? PD_LOCKED | PD_LIST_XLOCKED 1850192895Sjamie : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 1851192895Sjamie goto done_releroot; 1852191673Sjamie done_unlock_list: 1853191673Sjamie sx_xunlock(&allprison_lock); 1854191673Sjamie done_releroot: 1855241896Skib if (root != NULL) 1856191673Sjamie vrele(root); 1857191673Sjamie done_errmsg: 1858191673Sjamie if (error) { 1859191673Sjamie vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 1860191673Sjamie if (errmsg_len > 0) { 1861191673Sjamie errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1; 1862191673Sjamie if (errmsg_pos > 0) { 1863191673Sjamie if (optuio->uio_segflg == UIO_SYSSPACE) 1864191673Sjamie bcopy(errmsg, 1865191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 1866191673Sjamie errmsg_len); 1867191673Sjamie else 1868191673Sjamie copyout(errmsg, 1869191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 1870191673Sjamie errmsg_len); 1871191673Sjamie } 1872191673Sjamie } 1873191673Sjamie } 1874191673Sjamie done_free: 1875191673Sjamie#ifdef INET 1876191673Sjamie free(ip4, M_PRISON); 1877191673Sjamie#endif 1878191673Sjamie#ifdef INET6 1879191673Sjamie free(ip6, M_PRISON); 1880191673Sjamie#endif 1881230407Smm if (g_path != NULL) 1882230407Smm free(g_path, M_TEMP); 1883191673Sjamie vfs_freeopts(opts); 1884191673Sjamie return (error); 1885191673Sjamie} 1886191673Sjamie 1887191673Sjamie 188882710Sdillon/* 1889191673Sjamie * struct jail_get_args { 1890191673Sjamie * struct iovec *iovp; 1891191673Sjamie * unsigned int iovcnt; 1892191673Sjamie * int flags; 1893114168Smike * }; 189482710Sdillon */ 189546155Sphkint 1896225617Skmacysys_jail_get(struct thread *td, struct jail_get_args *uap) 189746155Sphk{ 1898191673Sjamie struct uio *auio; 1899185435Sbz int error; 1900185435Sbz 1901191673Sjamie /* Check that we have an even number of iovecs. */ 1902191673Sjamie if (uap->iovcnt & 1) 1903191673Sjamie return (EINVAL); 1904191673Sjamie 1905191673Sjamie error = copyinuio(uap->iovp, uap->iovcnt, &auio); 1906185435Sbz if (error) 1907185435Sbz return (error); 1908191673Sjamie error = kern_jail_get(td, auio, uap->flags); 1909191673Sjamie if (error == 0) 1910191673Sjamie error = copyout(auio->uio_iov, uap->iovp, 1911191673Sjamie uap->iovcnt * sizeof (struct iovec)); 1912191673Sjamie free(auio, M_IOV); 1913191673Sjamie return (error); 1914191673Sjamie} 1915185435Sbz 1916191673Sjamieint 1917191673Sjamiekern_jail_get(struct thread *td, struct uio *optuio, int flags) 1918191673Sjamie{ 1919192895Sjamie struct prison *pr, *mypr; 1920191673Sjamie struct vfsopt *opt; 1921191673Sjamie struct vfsoptlist *opts; 1922191673Sjamie char *errmsg, *name; 1923192895Sjamie int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos; 1924185435Sbz 1925191673Sjamie if (flags & ~JAIL_GET_MASK) 1926191673Sjamie return (EINVAL); 1927185435Sbz 1928191673Sjamie /* Get the parameter list. */ 1929191673Sjamie error = vfs_buildopts(optuio, &opts); 1930191673Sjamie if (error) 1931191673Sjamie return (error); 1932191673Sjamie errmsg_pos = vfs_getopt_pos(opts, "errmsg"); 1933192895Sjamie mypr = td->td_ucred->cr_prison; 1934185435Sbz 1935191673Sjamie /* 1936191673Sjamie * Find the prison specified by one of: lastjid, jid, name. 1937191673Sjamie */ 1938191673Sjamie sx_slock(&allprison_lock); 1939191673Sjamie error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid)); 1940191673Sjamie if (error == 0) { 1941191673Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) { 1942192895Sjamie if (pr->pr_id > jid && prison_ischild(mypr, pr)) { 1943191673Sjamie mtx_lock(&pr->pr_mtx); 1944191673Sjamie if (pr->pr_ref > 0 && 1945191673Sjamie (pr->pr_uref > 0 || (flags & JAIL_DYING))) 1946191673Sjamie break; 1947191673Sjamie mtx_unlock(&pr->pr_mtx); 1948191673Sjamie } 1949191673Sjamie } 1950191673Sjamie if (pr != NULL) 1951191673Sjamie goto found_prison; 1952191673Sjamie error = ENOENT; 1953191673Sjamie vfs_opterror(opts, "no jail after %d", jid); 1954191673Sjamie goto done_unlock_list; 1955191673Sjamie } else if (error != ENOENT) 1956191673Sjamie goto done_unlock_list; 1957185435Sbz 1958191673Sjamie error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 1959191673Sjamie if (error == 0) { 1960191673Sjamie if (jid != 0) { 1961192895Sjamie pr = prison_find_child(mypr, jid); 1962191673Sjamie if (pr != NULL) { 1963191673Sjamie if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 1964191673Sjamie mtx_unlock(&pr->pr_mtx); 1965191673Sjamie error = ENOENT; 1966191673Sjamie vfs_opterror(opts, "jail %d is dying", 1967191673Sjamie jid); 1968191673Sjamie goto done_unlock_list; 1969191673Sjamie } 1970191673Sjamie goto found_prison; 1971191673Sjamie } 1972191673Sjamie error = ENOENT; 1973191673Sjamie vfs_opterror(opts, "jail %d not found", jid); 1974191673Sjamie goto done_unlock_list; 1975191673Sjamie } 1976191673Sjamie } else if (error != ENOENT) 1977191673Sjamie goto done_unlock_list; 197846155Sphk 1979191673Sjamie error = vfs_getopt(opts, "name", (void **)&name, &len); 1980191673Sjamie if (error == 0) { 1981191673Sjamie if (len == 0 || name[len - 1] != '\0') { 1982191673Sjamie error = EINVAL; 1983191673Sjamie goto done_unlock_list; 1984191673Sjamie } 1985192895Sjamie pr = prison_find_name(mypr, name); 1986191673Sjamie if (pr != NULL) { 1987191673Sjamie if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 1988191673Sjamie mtx_unlock(&pr->pr_mtx); 1989191673Sjamie error = ENOENT; 1990191673Sjamie vfs_opterror(opts, "jail \"%s\" is dying", 1991191673Sjamie name); 1992191673Sjamie goto done_unlock_list; 1993191673Sjamie } 1994191673Sjamie goto found_prison; 1995191673Sjamie } 1996191673Sjamie error = ENOENT; 1997191673Sjamie vfs_opterror(opts, "jail \"%s\" not found", name); 1998191673Sjamie goto done_unlock_list; 1999191673Sjamie } else if (error != ENOENT) 2000191673Sjamie goto done_unlock_list; 2001185435Sbz 2002191673Sjamie vfs_opterror(opts, "no jail specified"); 2003191673Sjamie error = ENOENT; 2004191673Sjamie goto done_unlock_list; 2005191673Sjamie 2006191673Sjamie found_prison: 2007191673Sjamie /* Get the parameters of the prison. */ 2008191673Sjamie pr->pr_ref++; 2009191673Sjamie locked = PD_LOCKED; 2010191673Sjamie td->td_retval[0] = pr->pr_id; 2011191673Sjamie error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id)); 2012191673Sjamie if (error != 0 && error != ENOENT) 2013191673Sjamie goto done_deref; 2014192895Sjamie i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id; 2015192895Sjamie error = vfs_setopt(opts, "parent", &i, sizeof(i)); 2016191673Sjamie if (error != 0 && error != ENOENT) 2017191673Sjamie goto done_deref; 2018192895Sjamie error = vfs_setopts(opts, "name", prison_name(mypr, pr)); 2019192895Sjamie if (error != 0 && error != ENOENT) 2020192895Sjamie goto done_deref; 2021192895Sjamie error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id, 2022191673Sjamie sizeof(pr->pr_cpuset->cs_id)); 2023191673Sjamie if (error != 0 && error != ENOENT) 2024191673Sjamie goto done_deref; 2025192895Sjamie error = vfs_setopts(opts, "path", prison_path(mypr, pr)); 2026191673Sjamie if (error != 0 && error != ENOENT) 2027191673Sjamie goto done_deref; 2028191673Sjamie#ifdef INET 2029191673Sjamie error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4, 2030191673Sjamie pr->pr_ip4s * sizeof(*pr->pr_ip4)); 2031191673Sjamie if (error != 0 && error != ENOENT) 2032191673Sjamie goto done_deref; 2033191673Sjamie#endif 2034191673Sjamie#ifdef INET6 2035191673Sjamie error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6, 2036191673Sjamie pr->pr_ip6s * sizeof(*pr->pr_ip6)); 2037191673Sjamie if (error != 0 && error != ENOENT) 2038191673Sjamie goto done_deref; 2039191673Sjamie#endif 2040191673Sjamie error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel, 2041191673Sjamie sizeof(pr->pr_securelevel)); 2042191673Sjamie if (error != 0 && error != ENOENT) 2043191673Sjamie goto done_deref; 2044194762Sjamie error = vfs_setopt(opts, "children.cur", &pr->pr_childcount, 2045194762Sjamie sizeof(pr->pr_childcount)); 2046194762Sjamie if (error != 0 && error != ENOENT) 2047194762Sjamie goto done_deref; 2048194762Sjamie error = vfs_setopt(opts, "children.max", &pr->pr_childmax, 2049194762Sjamie sizeof(pr->pr_childmax)); 2050194762Sjamie if (error != 0 && error != ENOENT) 2051194762Sjamie goto done_deref; 2052194118Sjamie error = vfs_setopts(opts, "host.hostname", pr->pr_hostname); 2053191673Sjamie if (error != 0 && error != ENOENT) 2054191673Sjamie goto done_deref; 2055194118Sjamie error = vfs_setopts(opts, "host.domainname", pr->pr_domainname); 2056193066Sjamie if (error != 0 && error != ENOENT) 2057193066Sjamie goto done_deref; 2058194118Sjamie error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid); 2059193066Sjamie if (error != 0 && error != ENOENT) 2060193066Sjamie goto done_deref; 2061205014Snwhitehorn#ifdef COMPAT_FREEBSD32 2062217896Sdchagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 2063193066Sjamie uint32_t hid32 = pr->pr_hostid; 2064193066Sjamie 2065193066Sjamie error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32)); 2066193066Sjamie } else 2067193066Sjamie#endif 2068193066Sjamie error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid, 2069193066Sjamie sizeof(pr->pr_hostid)); 2070193066Sjamie if (error != 0 && error != ENOENT) 2071193066Sjamie goto done_deref; 2072192895Sjamie error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs, 2073192895Sjamie sizeof(pr->pr_enforce_statfs)); 2074191673Sjamie if (error != 0 && error != ENOENT) 2075191673Sjamie goto done_deref; 2076231267Smm error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum, 2077231267Smm sizeof(pr->pr_devfs_rsnum)); 2078231267Smm if (error != 0 && error != ENOENT) 2079231267Smm goto done_deref; 2080192895Sjamie for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 2081192895Sjamie fi++) { 2082192895Sjamie if (pr_flag_names[fi] == NULL) 2083192895Sjamie continue; 2084192895Sjamie i = (pr->pr_flags & (1 << fi)) ? 1 : 0; 2085192895Sjamie error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i)); 2086192895Sjamie if (error != 0 && error != ENOENT) 2087192895Sjamie goto done_deref; 2088192895Sjamie i = !i; 2089192895Sjamie error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i)); 2090192895Sjamie if (error != 0 && error != ENOENT) 2091192895Sjamie goto done_deref; 2092192895Sjamie } 2093195870Sjamie for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 2094195870Sjamie fi++) { 2095195870Sjamie i = pr->pr_flags & 2096195870Sjamie (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new); 2097195870Sjamie i = pr_flag_jailsys[fi].disable && 2098195870Sjamie (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE 2099195870Sjamie : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW 2100195870Sjamie : JAIL_SYS_INHERIT; 2101195870Sjamie error = 2102195870Sjamie vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i)); 2103195870Sjamie if (error != 0 && error != ENOENT) 2104195870Sjamie goto done_deref; 2105195870Sjamie } 2106192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 2107192895Sjamie fi++) { 2108192895Sjamie if (pr_allow_names[fi] == NULL) 2109192895Sjamie continue; 2110192895Sjamie i = (pr->pr_allow & (1 << fi)) ? 1 : 0; 2111192895Sjamie error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i)); 2112192895Sjamie if (error != 0 && error != ENOENT) 2113192895Sjamie goto done_deref; 2114192895Sjamie i = !i; 2115192895Sjamie error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i)); 2116192895Sjamie if (error != 0 && error != ENOENT) 2117192895Sjamie goto done_deref; 2118192895Sjamie } 2119191673Sjamie i = (pr->pr_uref == 0); 2120191673Sjamie error = vfs_setopt(opts, "dying", &i, sizeof(i)); 2121191673Sjamie if (error != 0 && error != ENOENT) 2122191673Sjamie goto done_deref; 2123191673Sjamie i = !i; 2124191673Sjamie error = vfs_setopt(opts, "nodying", &i, sizeof(i)); 2125191673Sjamie if (error != 0 && error != ENOENT) 2126191673Sjamie goto done_deref; 2127191673Sjamie 2128191673Sjamie /* Get the module parameters. */ 2129191673Sjamie mtx_unlock(&pr->pr_mtx); 2130191673Sjamie locked = 0; 2131191673Sjamie error = osd_jail_call(pr, PR_METHOD_GET, opts); 213246155Sphk if (error) 2133191673Sjamie goto done_deref; 2134191673Sjamie prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED); 213584828Sjhb 2136191673Sjamie /* By now, all parameters should have been noted. */ 2137191673Sjamie TAILQ_FOREACH(opt, opts, link) { 2138191673Sjamie if (!opt->seen && strcmp(opt->name, "errmsg")) { 2139191673Sjamie error = EINVAL; 2140191673Sjamie vfs_opterror(opts, "unknown parameter: %s", opt->name); 2141191673Sjamie goto done_errmsg; 2142191673Sjamie } 2143185435Sbz } 2144191673Sjamie 2145191673Sjamie /* Write the fetched parameters back to userspace. */ 2146191673Sjamie error = 0; 2147191673Sjamie TAILQ_FOREACH(opt, opts, link) { 2148191673Sjamie if (opt->pos >= 0 && opt->pos != errmsg_pos) { 2149191673Sjamie pos = 2 * opt->pos + 1; 2150191673Sjamie optuio->uio_iov[pos].iov_len = opt->len; 2151191673Sjamie if (opt->value != NULL) { 2152191673Sjamie if (optuio->uio_segflg == UIO_SYSSPACE) { 2153191673Sjamie bcopy(opt->value, 2154191673Sjamie optuio->uio_iov[pos].iov_base, 2155191673Sjamie opt->len); 2156191673Sjamie } else { 2157191673Sjamie error = copyout(opt->value, 2158191673Sjamie optuio->uio_iov[pos].iov_base, 2159191673Sjamie opt->len); 2160191673Sjamie if (error) 2161191673Sjamie break; 2162191673Sjamie } 2163191673Sjamie } 2164191673Sjamie } 2165185435Sbz } 2166191673Sjamie goto done_errmsg; 2167191673Sjamie 2168191673Sjamie done_deref: 2169191673Sjamie prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED); 2170191673Sjamie goto done_errmsg; 2171191673Sjamie 2172191673Sjamie done_unlock_list: 2173191673Sjamie sx_sunlock(&allprison_lock); 2174191673Sjamie done_errmsg: 2175191673Sjamie if (error && errmsg_pos >= 0) { 2176191673Sjamie vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 2177191673Sjamie errmsg_pos = 2 * errmsg_pos + 1; 2178191673Sjamie if (errmsg_len > 0) { 2179191673Sjamie if (optuio->uio_segflg == UIO_SYSSPACE) 2180191673Sjamie bcopy(errmsg, 2181191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 2182191673Sjamie errmsg_len); 2183191673Sjamie else 2184191673Sjamie copyout(errmsg, 2185191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 2186191673Sjamie errmsg_len); 2187191673Sjamie } 2188185435Sbz } 2189191673Sjamie vfs_freeopts(opts); 2190191673Sjamie return (error); 2191191673Sjamie} 2192113275Smike 2193192895Sjamie 2194191673Sjamie/* 2195191673Sjamie * struct jail_remove_args { 2196191673Sjamie * int jid; 2197191673Sjamie * }; 2198191673Sjamie */ 2199191673Sjamieint 2200225617Skmacysys_jail_remove(struct thread *td, struct jail_remove_args *uap) 2201191673Sjamie{ 2202192895Sjamie struct prison *pr, *cpr, *lpr, *tpr; 2203192895Sjamie int descend, error; 2204185435Sbz 2205191673Sjamie error = priv_check(td, PRIV_JAIL_REMOVE); 2206185435Sbz if (error) 2207191673Sjamie return (error); 2208185435Sbz 2209185435Sbz sx_xlock(&allprison_lock); 2210192895Sjamie pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2211191673Sjamie if (pr == NULL) { 2212185435Sbz sx_xunlock(&allprison_lock); 2213191673Sjamie return (EINVAL); 2214185435Sbz } 2215185435Sbz 2216192895Sjamie /* Remove all descendants of this prison, then remove this prison. */ 2217192895Sjamie pr->pr_ref++; 2218192895Sjamie pr->pr_flags |= PR_REMOVE; 2219192895Sjamie if (!LIST_EMPTY(&pr->pr_children)) { 2220192895Sjamie mtx_unlock(&pr->pr_mtx); 2221192895Sjamie lpr = NULL; 2222192895Sjamie FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 2223192895Sjamie mtx_lock(&cpr->pr_mtx); 2224192895Sjamie if (cpr->pr_ref > 0) { 2225192895Sjamie tpr = cpr; 2226192895Sjamie cpr->pr_ref++; 2227192895Sjamie cpr->pr_flags |= PR_REMOVE; 2228192895Sjamie } else { 2229192895Sjamie /* Already removed - do not do it again. */ 2230192895Sjamie tpr = NULL; 2231192895Sjamie } 2232192895Sjamie mtx_unlock(&cpr->pr_mtx); 2233192895Sjamie if (lpr != NULL) { 2234192895Sjamie mtx_lock(&lpr->pr_mtx); 2235192895Sjamie prison_remove_one(lpr); 2236192895Sjamie sx_xlock(&allprison_lock); 2237192895Sjamie } 2238192895Sjamie lpr = tpr; 2239192895Sjamie } 2240192895Sjamie if (lpr != NULL) { 2241192895Sjamie mtx_lock(&lpr->pr_mtx); 2242192895Sjamie prison_remove_one(lpr); 2243192895Sjamie sx_xlock(&allprison_lock); 2244192895Sjamie } 2245192895Sjamie mtx_lock(&pr->pr_mtx); 2246192895Sjamie } 2247192895Sjamie prison_remove_one(pr); 2248192895Sjamie return (0); 2249192895Sjamie} 2250192895Sjamie 2251192895Sjamiestatic void 2252192895Sjamieprison_remove_one(struct prison *pr) 2253192895Sjamie{ 2254192895Sjamie struct proc *p; 2255192895Sjamie int deuref; 2256192895Sjamie 2257191673Sjamie /* If the prison was persistent, it is not anymore. */ 2258191673Sjamie deuref = 0; 2259191673Sjamie if (pr->pr_flags & PR_PERSIST) { 2260191673Sjamie pr->pr_ref--; 2261191673Sjamie deuref = PD_DEUREF; 2262191673Sjamie pr->pr_flags &= ~PR_PERSIST; 2263179881Sdelphij } 2264113275Smike 2265192895Sjamie /* 2266192895Sjamie * jail_remove added a reference. If that's the only one, remove 2267192895Sjamie * the prison now. 2268192895Sjamie */ 2269192895Sjamie KASSERT(pr->pr_ref > 0, 2270192895Sjamie ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id)); 2271192895Sjamie if (pr->pr_ref == 1) { 2272191673Sjamie prison_deref(pr, 2273191673Sjamie deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 2274192895Sjamie return; 2275191673Sjamie } 2276191673Sjamie 2277113275Smike mtx_unlock(&pr->pr_mtx); 2278191673Sjamie sx_xunlock(&allprison_lock); 2279191673Sjamie /* 2280191673Sjamie * Kill all processes unfortunate enough to be attached to this prison. 2281191673Sjamie */ 2282191673Sjamie sx_slock(&allproc_lock); 2283191673Sjamie LIST_FOREACH(p, &allproc, p_list) { 2284191673Sjamie PROC_LOCK(p); 2285191673Sjamie if (p->p_state != PRS_NEW && p->p_ucred && 2286191673Sjamie p->p_ucred->cr_prison == pr) 2287225617Skmacy kern_psignal(p, SIGKILL); 2288191673Sjamie PROC_UNLOCK(p); 2289191673Sjamie } 2290191673Sjamie sx_sunlock(&allproc_lock); 2291192895Sjamie /* Remove the temporary reference added by jail_remove. */ 2292191673Sjamie prison_deref(pr, deuref | PD_DEREF); 2293113275Smike} 2294113275Smike 2295190466Sjamie 2296113275Smike/* 2297114168Smike * struct jail_attach_args { 2298114168Smike * int jid; 2299114168Smike * }; 2300113275Smike */ 2301113275Smikeint 2302225617Skmacysys_jail_attach(struct thread *td, struct jail_attach_args *uap) 2303113275Smike{ 2304113275Smike struct prison *pr; 2305191673Sjamie int error; 2306167309Spjd 2307164032Srwatson error = priv_check(td, PRIV_JAIL_ATTACH); 2308126023Snectar if (error) 2309126023Snectar return (error); 2310126023Snectar 2311168401Spjd sx_slock(&allprison_lock); 2312192895Sjamie pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2313113275Smike if (pr == NULL) { 2314168401Spjd sx_sunlock(&allprison_lock); 2315113275Smike return (EINVAL); 2316113275Smike } 2317185435Sbz 2318185435Sbz /* 2319185435Sbz * Do not allow a process to attach to a prison that is not 2320191673Sjamie * considered to be "alive". 2321185435Sbz */ 2322191673Sjamie if (pr->pr_uref == 0) { 2323185435Sbz mtx_unlock(&pr->pr_mtx); 2324185435Sbz sx_sunlock(&allprison_lock); 2325185435Sbz return (EINVAL); 2326185435Sbz } 2327191673Sjamie 2328191673Sjamie return (do_jail_attach(td, pr)); 2329191673Sjamie} 2330191673Sjamie 2331191673Sjamiestatic int 2332191673Sjamiedo_jail_attach(struct thread *td, struct prison *pr) 2333191673Sjamie{ 2334192895Sjamie struct prison *ppr; 2335191673Sjamie struct proc *p; 2336191673Sjamie struct ucred *newcred, *oldcred; 2337241896Skib int error; 2338191673Sjamie 2339191673Sjamie /* 2340191673Sjamie * XXX: Note that there is a slight race here if two threads 2341191673Sjamie * in the same privileged process attempt to attach to two 2342191673Sjamie * different jails at the same time. It is important for 2343191673Sjamie * user processes not to do this, or they might end up with 2344191673Sjamie * a process root from one prison, but attached to the jail 2345191673Sjamie * of another. 2346191673Sjamie */ 2347113275Smike pr->pr_ref++; 2348191673Sjamie pr->pr_uref++; 2349113275Smike mtx_unlock(&pr->pr_mtx); 2350191673Sjamie 2351191673Sjamie /* Let modules do whatever they need to prepare for attaching. */ 2352191673Sjamie error = osd_jail_call(pr, PR_METHOD_ATTACH, td); 2353191673Sjamie if (error) { 2354191673Sjamie prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED); 2355191673Sjamie return (error); 2356191673Sjamie } 2357168401Spjd sx_sunlock(&allprison_lock); 2358113275Smike 2359185435Sbz /* 2360185435Sbz * Reparent the newly attached process to this jail. 2361185435Sbz */ 2362192895Sjamie ppr = td->td_ucred->cr_prison; 2363191673Sjamie p = td->td_proc; 2364185435Sbz error = cpuset_setproc_update_set(p, pr->pr_cpuset); 2365185435Sbz if (error) 2366191673Sjamie goto e_revert_osd; 2367185435Sbz 2368175202Sattilio vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY); 2369113275Smike if ((error = change_dir(pr->pr_root, td)) != 0) 2370113275Smike goto e_unlock; 2371113275Smike#ifdef MAC 2372172930Srwatson if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root))) 2373113275Smike goto e_unlock; 2374113275Smike#endif 2375175294Sattilio VOP_UNLOCK(pr->pr_root, 0); 2376191673Sjamie if ((error = change_root(pr->pr_root, td))) 2377241896Skib goto e_revert_osd; 2378113275Smike 237984828Sjhb newcred = crget(); 238084828Sjhb PROC_LOCK(p); 238184828Sjhb oldcred = p->p_ucred; 2382113275Smike setsugid(p); 238384828Sjhb crcopy(newcred, oldcred); 2384113630Sjhb newcred->cr_prison = pr; 238584828Sjhb p->p_ucred = newcred; 238684828Sjhb PROC_UNLOCK(p); 2387220137Strasz#ifdef RACCT 2388220137Strasz racct_proc_ucred_changed(p, oldcred, newcred); 2389220137Strasz#endif 239084828Sjhb crfree(oldcred); 2391192895Sjamie prison_deref(ppr, PD_DEREF | PD_DEUREF); 239246155Sphk return (0); 2393191673Sjamie e_unlock: 2394175294Sattilio VOP_UNLOCK(pr->pr_root, 0); 2395191673Sjamie e_revert_osd: 2396191673Sjamie /* Tell modules this thread is still in its old jail after all. */ 2397192895Sjamie (void)osd_jail_call(ppr, PR_METHOD_ATTACH, td); 2398191673Sjamie prison_deref(pr, PD_DEREF | PD_DEUREF); 239946155Sphk return (error); 240046155Sphk} 240146155Sphk 2402192895Sjamie 2403113275Smike/* 2404113275Smike * Returns a locked prison instance, or NULL on failure. 2405113275Smike */ 2406168399Spjdstruct prison * 2407113275Smikeprison_find(int prid) 2408113275Smike{ 2409113275Smike struct prison *pr; 2410113275Smike 2411168401Spjd sx_assert(&allprison_lock, SX_LOCKED); 2412191673Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) { 2413113275Smike if (pr->pr_id == prid) { 2414113275Smike mtx_lock(&pr->pr_mtx); 2415191673Sjamie if (pr->pr_ref > 0) 2416191673Sjamie return (pr); 2417191673Sjamie mtx_unlock(&pr->pr_mtx); 2418113275Smike } 2419113275Smike } 2420113275Smike return (NULL); 2421113275Smike} 2422113275Smike 2423191673Sjamie/* 2424192895Sjamie * Find a prison that is a descendant of mypr. Returns a locked prison or NULL. 2425191673Sjamie */ 2426191673Sjamiestruct prison * 2427192895Sjamieprison_find_child(struct prison *mypr, int prid) 2428191673Sjamie{ 2429192895Sjamie struct prison *pr; 2430192895Sjamie int descend; 2431192895Sjamie 2432192895Sjamie sx_assert(&allprison_lock, SX_LOCKED); 2433192895Sjamie FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 2434192895Sjamie if (pr->pr_id == prid) { 2435192895Sjamie mtx_lock(&pr->pr_mtx); 2436192895Sjamie if (pr->pr_ref > 0) 2437192895Sjamie return (pr); 2438192895Sjamie mtx_unlock(&pr->pr_mtx); 2439192895Sjamie } 2440192895Sjamie } 2441192895Sjamie return (NULL); 2442192895Sjamie} 2443192895Sjamie 2444192895Sjamie/* 2445192895Sjamie * Look for the name relative to mypr. Returns a locked prison or NULL. 2446192895Sjamie */ 2447192895Sjamiestruct prison * 2448192895Sjamieprison_find_name(struct prison *mypr, const char *name) 2449192895Sjamie{ 2450191673Sjamie struct prison *pr, *deadpr; 2451192895Sjamie size_t mylen; 2452192895Sjamie int descend; 2453191673Sjamie 2454191673Sjamie sx_assert(&allprison_lock, SX_LOCKED); 2455192895Sjamie mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1; 2456191673Sjamie again: 2457191673Sjamie deadpr = NULL; 2458192895Sjamie FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 2459192895Sjamie if (!strcmp(pr->pr_name + mylen, name)) { 2460191673Sjamie mtx_lock(&pr->pr_mtx); 2461191673Sjamie if (pr->pr_ref > 0) { 2462191673Sjamie if (pr->pr_uref > 0) 2463191673Sjamie return (pr); 2464191673Sjamie deadpr = pr; 2465191673Sjamie } 2466191673Sjamie mtx_unlock(&pr->pr_mtx); 2467191673Sjamie } 2468191673Sjamie } 2469192895Sjamie /* There was no valid prison - perhaps there was a dying one. */ 2470191673Sjamie if (deadpr != NULL) { 2471191673Sjamie mtx_lock(&deadpr->pr_mtx); 2472191673Sjamie if (deadpr->pr_ref == 0) { 2473191673Sjamie mtx_unlock(&deadpr->pr_mtx); 2474191673Sjamie goto again; 2475191673Sjamie } 2476191673Sjamie } 2477191673Sjamie return (deadpr); 2478191673Sjamie} 2479191673Sjamie 2480191673Sjamie/* 2481192895Sjamie * See if a prison has the specific flag set. 2482192895Sjamie */ 2483192895Sjamieint 2484192895Sjamieprison_flag(struct ucred *cred, unsigned flag) 2485192895Sjamie{ 2486192895Sjamie 2487192895Sjamie /* This is an atomic read, so no locking is necessary. */ 2488192895Sjamie return (cred->cr_prison->pr_flags & flag); 2489192895Sjamie} 2490192895Sjamie 2491192895Sjamieint 2492192895Sjamieprison_allow(struct ucred *cred, unsigned flag) 2493192895Sjamie{ 2494192895Sjamie 2495192895Sjamie /* This is an atomic read, so no locking is necessary. */ 2496192895Sjamie return (cred->cr_prison->pr_allow & flag); 2497192895Sjamie} 2498192895Sjamie 2499192895Sjamie/* 2500191673Sjamie * Remove a prison reference. If that was the last reference, remove the 2501191673Sjamie * prison itself - but not in this context in case there are locks held. 2502191673Sjamie */ 250372786Srwatsonvoid 2504185029Spjdprison_free_locked(struct prison *pr) 250572786Srwatson{ 250672786Srwatson 2507185029Spjd mtx_assert(&pr->pr_mtx, MA_OWNED); 250872786Srwatson pr->pr_ref--; 250972786Srwatson if (pr->pr_ref == 0) { 2510168483Spjd mtx_unlock(&pr->pr_mtx); 2511124882Srwatson TASK_INIT(&pr->pr_task, 0, prison_complete, pr); 2512144660Sjeff taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 251387275Srwatson return; 251472786Srwatson } 251587275Srwatson mtx_unlock(&pr->pr_mtx); 251672786Srwatson} 251772786Srwatson 2518185029Spjdvoid 2519185029Spjdprison_free(struct prison *pr) 2520185029Spjd{ 2521185029Spjd 2522185029Spjd mtx_lock(&pr->pr_mtx); 2523185029Spjd prison_free_locked(pr); 2524185029Spjd} 2525185029Spjd 2526124882Srwatsonstatic void 2527124882Srwatsonprison_complete(void *context, int pending) 2528124882Srwatson{ 2529191673Sjamie 2530191673Sjamie prison_deref((struct prison *)context, 0); 2531191673Sjamie} 2532191673Sjamie 2533191673Sjamie/* 2534191673Sjamie * Remove a prison reference (usually). This internal version assumes no 2535191673Sjamie * mutexes are held, except perhaps the prison itself. If there are no more 2536191673Sjamie * references, release and delist the prison. On completion, the prison lock 2537191673Sjamie * and the allprison lock are both unlocked. 2538191673Sjamie */ 2539191673Sjamiestatic void 2540191673Sjamieprison_deref(struct prison *pr, int flags) 2541191673Sjamie{ 2542192895Sjamie struct prison *ppr, *tpr; 2543124882Srwatson 2544191673Sjamie if (!(flags & PD_LOCKED)) 2545191673Sjamie mtx_lock(&pr->pr_mtx); 2546225191Sjamie for (;;) { 2547225191Sjamie if (flags & PD_DEUREF) { 2548225191Sjamie pr->pr_uref--; 2549225191Sjamie KASSERT(prison0.pr_uref != 0, ("prison0 pr_uref=0")); 2550192895Sjamie } 2551192895Sjamie if (flags & PD_DEREF) 2552192895Sjamie pr->pr_ref--; 2553192895Sjamie /* If the prison still has references, nothing else to do. */ 2554192895Sjamie if (pr->pr_ref > 0) { 2555192895Sjamie mtx_unlock(&pr->pr_mtx); 2556192895Sjamie if (flags & PD_LIST_SLOCKED) 2557192895Sjamie sx_sunlock(&allprison_lock); 2558192895Sjamie else if (flags & PD_LIST_XLOCKED) 2559192895Sjamie sx_xunlock(&allprison_lock); 2560192895Sjamie return; 2561191673Sjamie } 2562191673Sjamie 2563192895Sjamie mtx_unlock(&pr->pr_mtx); 2564192895Sjamie if (flags & PD_LIST_SLOCKED) { 2565192895Sjamie if (!sx_try_upgrade(&allprison_lock)) { 2566192895Sjamie sx_sunlock(&allprison_lock); 2567192895Sjamie sx_xlock(&allprison_lock); 2568192895Sjamie } 2569192895Sjamie } else if (!(flags & PD_LIST_XLOCKED)) 2570192895Sjamie sx_xlock(&allprison_lock); 2571168489Spjd 2572192895Sjamie TAILQ_REMOVE(&allprison, pr, pr_list); 2573192895Sjamie LIST_REMOVE(pr, pr_sibling); 2574192895Sjamie ppr = pr->pr_parent; 2575192895Sjamie for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 2576194762Sjamie tpr->pr_childcount--; 2577196592Sjamie sx_xunlock(&allprison_lock); 2578192895Sjamie 2579194251Sjamie#ifdef VIMAGE 2580196505Szec if (pr->pr_vnet != ppr->pr_vnet) 2581194251Sjamie vnet_destroy(pr->pr_vnet); 2582194251Sjamie#endif 2583241896Skib if (pr->pr_root != NULL) 2584192895Sjamie vrele(pr->pr_root); 2585192895Sjamie mtx_destroy(&pr->pr_mtx); 2586191673Sjamie#ifdef INET 2587192895Sjamie free(pr->pr_ip4, M_PRISON); 2588191673Sjamie#endif 2589185435Sbz#ifdef INET6 2590192895Sjamie free(pr->pr_ip6, M_PRISON); 2591185435Sbz#endif 2592192895Sjamie if (pr->pr_cpuset != NULL) 2593192895Sjamie cpuset_rel(pr->pr_cpuset); 2594192895Sjamie osd_jail_exit(pr); 2595221362Strasz#ifdef RACCT 2596221362Strasz prison_racct_detach(pr); 2597220163Strasz#endif 2598192895Sjamie free(pr, M_PRISON); 2599192895Sjamie 2600192895Sjamie /* Removing a prison frees a reference on its parent. */ 2601192895Sjamie pr = ppr; 2602192895Sjamie mtx_lock(&pr->pr_mtx); 2603225191Sjamie flags = PD_DEREF | PD_DEUREF; 2604192895Sjamie } 2605124882Srwatson} 2606124882Srwatson 260772786Srwatsonvoid 2608185029Spjdprison_hold_locked(struct prison *pr) 260972786Srwatson{ 261072786Srwatson 2611185029Spjd mtx_assert(&pr->pr_mtx, MA_OWNED); 2612168489Spjd KASSERT(pr->pr_ref > 0, 2613191671Sjamie ("Trying to hold dead prison (jid=%d).", pr->pr_id)); 261472786Srwatson pr->pr_ref++; 2615185029Spjd} 2616185029Spjd 2617185029Spjdvoid 2618185029Spjdprison_hold(struct prison *pr) 2619185029Spjd{ 2620185029Spjd 2621185029Spjd mtx_lock(&pr->pr_mtx); 2622185029Spjd prison_hold_locked(pr); 262387275Srwatson mtx_unlock(&pr->pr_mtx); 262472786Srwatson} 262572786Srwatson 2626185435Sbzvoid 2627185435Sbzprison_proc_hold(struct prison *pr) 262887275Srwatson{ 262987275Srwatson 2630185435Sbz mtx_lock(&pr->pr_mtx); 2631191673Sjamie KASSERT(pr->pr_uref > 0, 2632191673Sjamie ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id)); 2633191673Sjamie pr->pr_uref++; 2634185435Sbz mtx_unlock(&pr->pr_mtx); 263587275Srwatson} 263687275Srwatson 2637185435Sbzvoid 2638185435Sbzprison_proc_free(struct prison *pr) 2639185435Sbz{ 2640185435Sbz 2641185435Sbz mtx_lock(&pr->pr_mtx); 2642191673Sjamie KASSERT(pr->pr_uref > 0, 2643191673Sjamie ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id)); 2644191673Sjamie prison_deref(pr, PD_DEUREF | PD_LOCKED); 2645185435Sbz} 2646185435Sbz 2647185435Sbz 2648185435Sbz#ifdef INET 2649185435Sbz/* 2650192895Sjamie * Restrict a prison's IP address list with its parent's, possibly replacing 2651192895Sjamie * it. Return true if the replacement buffer was used (or would have been). 2652192895Sjamie */ 2653192895Sjamiestatic int 2654192895Sjamieprison_restrict_ip4(struct prison *pr, struct in_addr *newip4) 2655192895Sjamie{ 2656192895Sjamie int ii, ij, used; 2657192895Sjamie struct prison *ppr; 2658192895Sjamie 2659192895Sjamie ppr = pr->pr_parent; 2660192895Sjamie if (!(pr->pr_flags & PR_IP4_USER)) { 2661192895Sjamie /* This has no user settings, so just copy the parent's list. */ 2662192895Sjamie if (pr->pr_ip4s < ppr->pr_ip4s) { 2663192895Sjamie /* 2664192895Sjamie * There's no room for the parent's list. Use the 2665192895Sjamie * new list buffer, which is assumed to be big enough 2666192895Sjamie * (if it was passed). If there's no buffer, try to 2667192895Sjamie * allocate one. 2668192895Sjamie */ 2669192895Sjamie used = 1; 2670192895Sjamie if (newip4 == NULL) { 2671192895Sjamie newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4), 2672192895Sjamie M_PRISON, M_NOWAIT); 2673192895Sjamie if (newip4 != NULL) 2674192895Sjamie used = 0; 2675192895Sjamie } 2676192895Sjamie if (newip4 != NULL) { 2677192895Sjamie bcopy(ppr->pr_ip4, newip4, 2678192895Sjamie ppr->pr_ip4s * sizeof(*newip4)); 2679192895Sjamie free(pr->pr_ip4, M_PRISON); 2680192895Sjamie pr->pr_ip4 = newip4; 2681192895Sjamie pr->pr_ip4s = ppr->pr_ip4s; 2682192895Sjamie } 2683192895Sjamie return (used); 2684192895Sjamie } 2685192895Sjamie pr->pr_ip4s = ppr->pr_ip4s; 2686192895Sjamie if (pr->pr_ip4s > 0) 2687192895Sjamie bcopy(ppr->pr_ip4, pr->pr_ip4, 2688192895Sjamie pr->pr_ip4s * sizeof(*newip4)); 2689192895Sjamie else if (pr->pr_ip4 != NULL) { 2690192895Sjamie free(pr->pr_ip4, M_PRISON); 2691192895Sjamie pr->pr_ip4 = NULL; 2692192895Sjamie } 2693195974Sjamie } else if (pr->pr_ip4s > 0) { 2694192895Sjamie /* Remove addresses that aren't in the parent. */ 2695192895Sjamie for (ij = 0; ij < ppr->pr_ip4s; ij++) 2696192895Sjamie if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 2697192895Sjamie break; 2698192895Sjamie if (ij < ppr->pr_ip4s) 2699192895Sjamie ii = 1; 2700192895Sjamie else { 2701192895Sjamie bcopy(pr->pr_ip4 + 1, pr->pr_ip4, 2702192895Sjamie --pr->pr_ip4s * sizeof(*pr->pr_ip4)); 2703192895Sjamie ii = 0; 2704192895Sjamie } 2705192895Sjamie for (ij = 1; ii < pr->pr_ip4s; ) { 2706192895Sjamie if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) { 2707192895Sjamie ii++; 2708192895Sjamie continue; 2709192895Sjamie } 2710192895Sjamie switch (ij >= ppr->pr_ip4s ? -1 : 2711192895Sjamie qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) { 2712192895Sjamie case -1: 2713192895Sjamie bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii, 2714192895Sjamie (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4)); 2715192895Sjamie break; 2716192895Sjamie case 0: 2717192895Sjamie ii++; 2718192895Sjamie ij++; 2719192895Sjamie break; 2720192895Sjamie case 1: 2721192895Sjamie ij++; 2722192895Sjamie break; 2723192895Sjamie } 2724192895Sjamie } 2725192895Sjamie if (pr->pr_ip4s == 0) { 2726195870Sjamie pr->pr_flags |= PR_IP4_DISABLE; 2727192895Sjamie free(pr->pr_ip4, M_PRISON); 2728192895Sjamie pr->pr_ip4 = NULL; 2729192895Sjamie } 2730192895Sjamie } 2731192895Sjamie return (0); 2732192895Sjamie} 2733192895Sjamie 2734192895Sjamie/* 2735185435Sbz * Pass back primary IPv4 address of this jail. 2736185435Sbz * 2737192895Sjamie * If not restricted return success but do not alter the address. Caller has 2738192895Sjamie * to make sure to initialize it correctly (e.g. INADDR_ANY). 2739185435Sbz * 2740188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4. 2741188144Sjamie * Address returned in NBO. 2742185435Sbz */ 274346155Sphkint 2744187684Sbzprison_get_ip4(struct ucred *cred, struct in_addr *ia) 274546155Sphk{ 2746191673Sjamie struct prison *pr; 274746155Sphk 2748185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2749185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2750185435Sbz 2751192895Sjamie pr = cred->cr_prison; 2752192895Sjamie if (!(pr->pr_flags & PR_IP4)) 275346155Sphk return (0); 2754191673Sjamie mtx_lock(&pr->pr_mtx); 2755192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2756192895Sjamie mtx_unlock(&pr->pr_mtx); 2757192895Sjamie return (0); 2758192895Sjamie } 2759191673Sjamie if (pr->pr_ip4 == NULL) { 2760191673Sjamie mtx_unlock(&pr->pr_mtx); 2761188144Sjamie return (EAFNOSUPPORT); 2762191673Sjamie } 2763185435Sbz 2764191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2765191673Sjamie mtx_unlock(&pr->pr_mtx); 2766185435Sbz return (0); 2767185435Sbz} 2768185435Sbz 2769185435Sbz/* 2770202468Sbz * Return 1 if we should do proper source address selection or are not jailed. 2771202468Sbz * We will return 0 if we should bypass source address selection in favour 2772202468Sbz * of the primary jail IPv4 address. Only in this case *ia will be updated and 2773202468Sbz * returned in NBO. 2774202468Sbz * Return EAFNOSUPPORT, in case this jail does not allow IPv4. 2775202468Sbz */ 2776202468Sbzint 2777202468Sbzprison_saddrsel_ip4(struct ucred *cred, struct in_addr *ia) 2778202468Sbz{ 2779202468Sbz struct prison *pr; 2780202468Sbz struct in_addr lia; 2781202468Sbz int error; 2782202468Sbz 2783202468Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2784202468Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2785202468Sbz 2786202468Sbz if (!jailed(cred)) 2787202468Sbz return (1); 2788202468Sbz 2789202468Sbz pr = cred->cr_prison; 2790202468Sbz if (pr->pr_flags & PR_IP4_SADDRSEL) 2791202468Sbz return (1); 2792202468Sbz 2793202468Sbz lia.s_addr = INADDR_ANY; 2794202468Sbz error = prison_get_ip4(cred, &lia); 2795202468Sbz if (error) 2796202468Sbz return (error); 2797202468Sbz if (lia.s_addr == INADDR_ANY) 2798202468Sbz return (1); 2799202468Sbz 2800202468Sbz ia->s_addr = lia.s_addr; 2801202468Sbz return (0); 2802202468Sbz} 2803202468Sbz 2804202468Sbz/* 2805192895Sjamie * Return true if pr1 and pr2 have the same IPv4 address restrictions. 2806192895Sjamie */ 2807192895Sjamieint 2808192895Sjamieprison_equal_ip4(struct prison *pr1, struct prison *pr2) 2809192895Sjamie{ 2810192895Sjamie 2811192895Sjamie if (pr1 == pr2) 2812192895Sjamie return (1); 2813192895Sjamie 2814192895Sjamie /* 2815195974Sjamie * No need to lock since the PR_IP4_USER flag can't be altered for 2816195974Sjamie * existing prisons. 2817192895Sjamie */ 2818195945Sjamie while (pr1 != &prison0 && 2819195945Sjamie#ifdef VIMAGE 2820195945Sjamie !(pr1->pr_flags & PR_VNET) && 2821195945Sjamie#endif 2822195945Sjamie !(pr1->pr_flags & PR_IP4_USER)) 2823192895Sjamie pr1 = pr1->pr_parent; 2824195945Sjamie while (pr2 != &prison0 && 2825195945Sjamie#ifdef VIMAGE 2826195945Sjamie !(pr2->pr_flags & PR_VNET) && 2827195945Sjamie#endif 2828195945Sjamie !(pr2->pr_flags & PR_IP4_USER)) 2829192895Sjamie pr2 = pr2->pr_parent; 2830192895Sjamie return (pr1 == pr2); 2831192895Sjamie} 2832192895Sjamie 2833192895Sjamie/* 2834185435Sbz * Make sure our (source) address is set to something meaningful to this 2835185435Sbz * jail. 2836185435Sbz * 2837192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail, 2838192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 2839192895Sjamie * doesn't allow IPv4. Address passed in in NBO and returned in NBO. 2840185435Sbz */ 2841185435Sbzint 2842185435Sbzprison_local_ip4(struct ucred *cred, struct in_addr *ia) 2843185435Sbz{ 2844191673Sjamie struct prison *pr; 2845185435Sbz struct in_addr ia0; 2846191673Sjamie int error; 2847185435Sbz 2848185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2849185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2850185435Sbz 2851192895Sjamie pr = cred->cr_prison; 2852192895Sjamie if (!(pr->pr_flags & PR_IP4)) 285346155Sphk return (0); 2854191673Sjamie mtx_lock(&pr->pr_mtx); 2855192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2856192895Sjamie mtx_unlock(&pr->pr_mtx); 2857192895Sjamie return (0); 2858192895Sjamie } 2859191673Sjamie if (pr->pr_ip4 == NULL) { 2860191673Sjamie mtx_unlock(&pr->pr_mtx); 2861188144Sjamie return (EAFNOSUPPORT); 2862191673Sjamie } 2863185435Sbz 2864185435Sbz ia0.s_addr = ntohl(ia->s_addr); 2865185435Sbz if (ia0.s_addr == INADDR_LOOPBACK) { 2866191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2867191673Sjamie mtx_unlock(&pr->pr_mtx); 2868185435Sbz return (0); 286946155Sphk } 2870185435Sbz 2871188144Sjamie if (ia0.s_addr == INADDR_ANY) { 2872188144Sjamie /* 2873188144Sjamie * In case there is only 1 IPv4 address, bind directly. 2874188144Sjamie */ 2875191673Sjamie if (pr->pr_ip4s == 1) 2876191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2877191673Sjamie mtx_unlock(&pr->pr_mtx); 2878185435Sbz return (0); 2879185435Sbz } 2880185435Sbz 2881191673Sjamie error = _prison_check_ip4(pr, ia); 2882191673Sjamie mtx_unlock(&pr->pr_mtx); 2883191673Sjamie return (error); 2884185435Sbz} 2885185435Sbz 2886185435Sbz/* 2887185435Sbz * Rewrite destination address in case we will connect to loopback address. 2888185435Sbz * 2889188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4. 2890188144Sjamie * Address passed in in NBO and returned in NBO. 2891185435Sbz */ 2892185435Sbzint 2893185435Sbzprison_remote_ip4(struct ucred *cred, struct in_addr *ia) 2894185435Sbz{ 2895191673Sjamie struct prison *pr; 2896185435Sbz 2897185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2898185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2899185435Sbz 2900192895Sjamie pr = cred->cr_prison; 2901192895Sjamie if (!(pr->pr_flags & PR_IP4)) 2902185435Sbz return (0); 2903191673Sjamie mtx_lock(&pr->pr_mtx); 2904192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2905192895Sjamie mtx_unlock(&pr->pr_mtx); 2906192895Sjamie return (0); 2907192895Sjamie } 2908191673Sjamie if (pr->pr_ip4 == NULL) { 2909191673Sjamie mtx_unlock(&pr->pr_mtx); 2910188144Sjamie return (EAFNOSUPPORT); 2911191673Sjamie } 2912188144Sjamie 2913185435Sbz if (ntohl(ia->s_addr) == INADDR_LOOPBACK) { 2914191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2915191673Sjamie mtx_unlock(&pr->pr_mtx); 2916185435Sbz return (0); 2917185435Sbz } 2918185435Sbz 2919185435Sbz /* 2920185435Sbz * Return success because nothing had to be changed. 2921185435Sbz */ 2922191673Sjamie mtx_unlock(&pr->pr_mtx); 2923185435Sbz return (0); 2924185435Sbz} 2925185435Sbz 2926185435Sbz/* 2927188144Sjamie * Check if given address belongs to the jail referenced by cred/prison. 2928185435Sbz * 2929192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail, 2930192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 2931192895Sjamie * doesn't allow IPv4. Address passed in in NBO. 2932185435Sbz */ 2933185435Sbzstatic int 2934185435Sbz_prison_check_ip4(struct prison *pr, struct in_addr *ia) 2935185435Sbz{ 2936185435Sbz int i, a, z, d; 2937185435Sbz 2938185435Sbz /* 2939185435Sbz * Check the primary IP. 2940185435Sbz */ 2941185435Sbz if (pr->pr_ip4[0].s_addr == ia->s_addr) 2942188144Sjamie return (0); 2943185435Sbz 2944185435Sbz /* 2945185435Sbz * All the other IPs are sorted so we can do a binary search. 2946185435Sbz */ 2947185435Sbz a = 0; 2948185435Sbz z = pr->pr_ip4s - 2; 2949185435Sbz while (a <= z) { 2950185435Sbz i = (a + z) / 2; 2951185435Sbz d = qcmp_v4(&pr->pr_ip4[i+1], ia); 2952185435Sbz if (d > 0) 2953185435Sbz z = i - 1; 2954185435Sbz else if (d < 0) 2955185435Sbz a = i + 1; 295681114Srwatson else 2957188144Sjamie return (0); 2958185435Sbz } 2959188144Sjamie 2960188144Sjamie return (EADDRNOTAVAIL); 2961185435Sbz} 2962185435Sbz 2963185435Sbzint 2964185435Sbzprison_check_ip4(struct ucred *cred, struct in_addr *ia) 2965185435Sbz{ 2966191673Sjamie struct prison *pr; 2967191673Sjamie int error; 2968185435Sbz 2969185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2970185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2971185435Sbz 2972192895Sjamie pr = cred->cr_prison; 2973192895Sjamie if (!(pr->pr_flags & PR_IP4)) 2974188144Sjamie return (0); 2975191673Sjamie mtx_lock(&pr->pr_mtx); 2976192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2977192895Sjamie mtx_unlock(&pr->pr_mtx); 2978192895Sjamie return (0); 2979192895Sjamie } 2980191673Sjamie if (pr->pr_ip4 == NULL) { 2981191673Sjamie mtx_unlock(&pr->pr_mtx); 2982188144Sjamie return (EAFNOSUPPORT); 2983191673Sjamie } 2984185435Sbz 2985191673Sjamie error = _prison_check_ip4(pr, ia); 2986191673Sjamie mtx_unlock(&pr->pr_mtx); 2987191673Sjamie return (error); 2988185435Sbz} 2989185435Sbz#endif 2990185435Sbz 2991185435Sbz#ifdef INET6 2992192895Sjamiestatic int 2993192895Sjamieprison_restrict_ip6(struct prison *pr, struct in6_addr *newip6) 2994192895Sjamie{ 2995192895Sjamie int ii, ij, used; 2996192895Sjamie struct prison *ppr; 2997192895Sjamie 2998192895Sjamie ppr = pr->pr_parent; 2999192895Sjamie if (!(pr->pr_flags & PR_IP6_USER)) { 3000192895Sjamie /* This has no user settings, so just copy the parent's list. */ 3001192895Sjamie if (pr->pr_ip6s < ppr->pr_ip6s) { 3002192895Sjamie /* 3003192895Sjamie * There's no room for the parent's list. Use the 3004192895Sjamie * new list buffer, which is assumed to be big enough 3005192895Sjamie * (if it was passed). If there's no buffer, try to 3006192895Sjamie * allocate one. 3007192895Sjamie */ 3008192895Sjamie used = 1; 3009192895Sjamie if (newip6 == NULL) { 3010192895Sjamie newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6), 3011192895Sjamie M_PRISON, M_NOWAIT); 3012192895Sjamie if (newip6 != NULL) 3013192895Sjamie used = 0; 3014192895Sjamie } 3015192895Sjamie if (newip6 != NULL) { 3016192895Sjamie bcopy(ppr->pr_ip6, newip6, 3017192895Sjamie ppr->pr_ip6s * sizeof(*newip6)); 3018192895Sjamie free(pr->pr_ip6, M_PRISON); 3019192895Sjamie pr->pr_ip6 = newip6; 3020192895Sjamie pr->pr_ip6s = ppr->pr_ip6s; 3021192895Sjamie } 3022192895Sjamie return (used); 3023192895Sjamie } 3024192895Sjamie pr->pr_ip6s = ppr->pr_ip6s; 3025192895Sjamie if (pr->pr_ip6s > 0) 3026192895Sjamie bcopy(ppr->pr_ip6, pr->pr_ip6, 3027192895Sjamie pr->pr_ip6s * sizeof(*newip6)); 3028192895Sjamie else if (pr->pr_ip6 != NULL) { 3029192895Sjamie free(pr->pr_ip6, M_PRISON); 3030192895Sjamie pr->pr_ip6 = NULL; 3031192895Sjamie } 3032195974Sjamie } else if (pr->pr_ip6s > 0) { 3033192895Sjamie /* Remove addresses that aren't in the parent. */ 3034192895Sjamie for (ij = 0; ij < ppr->pr_ip6s; ij++) 3035192895Sjamie if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], 3036192895Sjamie &ppr->pr_ip6[ij])) 3037192895Sjamie break; 3038192895Sjamie if (ij < ppr->pr_ip6s) 3039192895Sjamie ii = 1; 3040192895Sjamie else { 3041192895Sjamie bcopy(pr->pr_ip6 + 1, pr->pr_ip6, 3042192895Sjamie --pr->pr_ip6s * sizeof(*pr->pr_ip6)); 3043192895Sjamie ii = 0; 3044192895Sjamie } 3045192895Sjamie for (ij = 1; ii < pr->pr_ip6s; ) { 3046192895Sjamie if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii], 3047192895Sjamie &ppr->pr_ip6[0])) { 3048192895Sjamie ii++; 3049192895Sjamie continue; 3050192895Sjamie } 3051259847Sae switch (ij >= ppr->pr_ip6s ? -1 : 3052192895Sjamie qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) { 3053192895Sjamie case -1: 3054192895Sjamie bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii, 3055192895Sjamie (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6)); 3056192895Sjamie break; 3057192895Sjamie case 0: 3058192895Sjamie ii++; 3059192895Sjamie ij++; 3060192895Sjamie break; 3061192895Sjamie case 1: 3062192895Sjamie ij++; 3063192895Sjamie break; 3064192895Sjamie } 3065192895Sjamie } 3066192895Sjamie if (pr->pr_ip6s == 0) { 3067195870Sjamie pr->pr_flags |= PR_IP6_DISABLE; 3068192895Sjamie free(pr->pr_ip6, M_PRISON); 3069192895Sjamie pr->pr_ip6 = NULL; 3070192895Sjamie } 3071192895Sjamie } 3072192895Sjamie return 0; 3073192895Sjamie} 3074192895Sjamie 3075185435Sbz/* 3076185435Sbz * Pass back primary IPv6 address for this jail. 3077185435Sbz * 3078192895Sjamie * If not restricted return success but do not alter the address. Caller has 3079192895Sjamie * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT). 3080185435Sbz * 3081188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6. 3082185435Sbz */ 3083185435Sbzint 3084187684Sbzprison_get_ip6(struct ucred *cred, struct in6_addr *ia6) 3085185435Sbz{ 3086191673Sjamie struct prison *pr; 3087185435Sbz 3088185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3089185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3090185435Sbz 3091192895Sjamie pr = cred->cr_prison; 3092192895Sjamie if (!(pr->pr_flags & PR_IP6)) 309381114Srwatson return (0); 3094191673Sjamie mtx_lock(&pr->pr_mtx); 3095192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3096192895Sjamie mtx_unlock(&pr->pr_mtx); 3097192895Sjamie return (0); 3098192895Sjamie } 3099191673Sjamie if (pr->pr_ip6 == NULL) { 3100191673Sjamie mtx_unlock(&pr->pr_mtx); 3101188144Sjamie return (EAFNOSUPPORT); 3102191673Sjamie } 3103188144Sjamie 3104191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3105191673Sjamie mtx_unlock(&pr->pr_mtx); 3106185435Sbz return (0); 3107185435Sbz} 3108185435Sbz 3109185435Sbz/* 3110202468Sbz * Return 1 if we should do proper source address selection or are not jailed. 3111202468Sbz * We will return 0 if we should bypass source address selection in favour 3112202468Sbz * of the primary jail IPv6 address. Only in this case *ia will be updated and 3113202468Sbz * returned in NBO. 3114202468Sbz * Return EAFNOSUPPORT, in case this jail does not allow IPv6. 3115202468Sbz */ 3116202468Sbzint 3117202468Sbzprison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia6) 3118202468Sbz{ 3119202468Sbz struct prison *pr; 3120202468Sbz struct in6_addr lia6; 3121202468Sbz int error; 3122202468Sbz 3123202468Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3124202468Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3125202468Sbz 3126202468Sbz if (!jailed(cred)) 3127202468Sbz return (1); 3128202468Sbz 3129202468Sbz pr = cred->cr_prison; 3130202468Sbz if (pr->pr_flags & PR_IP6_SADDRSEL) 3131202468Sbz return (1); 3132202468Sbz 3133202468Sbz lia6 = in6addr_any; 3134202468Sbz error = prison_get_ip6(cred, &lia6); 3135202468Sbz if (error) 3136202468Sbz return (error); 3137202468Sbz if (IN6_IS_ADDR_UNSPECIFIED(&lia6)) 3138202468Sbz return (1); 3139202468Sbz 3140202468Sbz bcopy(&lia6, ia6, sizeof(struct in6_addr)); 3141202468Sbz return (0); 3142202468Sbz} 3143202468Sbz 3144202468Sbz/* 3145192895Sjamie * Return true if pr1 and pr2 have the same IPv6 address restrictions. 3146192895Sjamie */ 3147192895Sjamieint 3148192895Sjamieprison_equal_ip6(struct prison *pr1, struct prison *pr2) 3149192895Sjamie{ 3150192895Sjamie 3151192895Sjamie if (pr1 == pr2) 3152192895Sjamie return (1); 3153192895Sjamie 3154195945Sjamie while (pr1 != &prison0 && 3155195945Sjamie#ifdef VIMAGE 3156195945Sjamie !(pr1->pr_flags & PR_VNET) && 3157195945Sjamie#endif 3158195945Sjamie !(pr1->pr_flags & PR_IP6_USER)) 3159192895Sjamie pr1 = pr1->pr_parent; 3160195945Sjamie while (pr2 != &prison0 && 3161195945Sjamie#ifdef VIMAGE 3162195945Sjamie !(pr2->pr_flags & PR_VNET) && 3163195945Sjamie#endif 3164195945Sjamie !(pr2->pr_flags & PR_IP6_USER)) 3165192895Sjamie pr2 = pr2->pr_parent; 3166192895Sjamie return (pr1 == pr2); 3167192895Sjamie} 3168192895Sjamie 3169192895Sjamie/* 3170185435Sbz * Make sure our (source) address is set to something meaningful to this jail. 3171185435Sbz * 3172185435Sbz * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0) 3173185435Sbz * when needed while binding. 3174185435Sbz * 3175192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail, 3176192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 3177192895Sjamie * doesn't allow IPv6. 3178185435Sbz */ 3179185435Sbzint 3180185435Sbzprison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only) 3181185435Sbz{ 3182191673Sjamie struct prison *pr; 3183191673Sjamie int error; 3184185435Sbz 3185185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3186185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3187185435Sbz 3188192895Sjamie pr = cred->cr_prison; 3189192895Sjamie if (!(pr->pr_flags & PR_IP6)) 3190185435Sbz return (0); 3191191673Sjamie mtx_lock(&pr->pr_mtx); 3192192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3193192895Sjamie mtx_unlock(&pr->pr_mtx); 3194192895Sjamie return (0); 3195192895Sjamie } 3196191673Sjamie if (pr->pr_ip6 == NULL) { 3197191673Sjamie mtx_unlock(&pr->pr_mtx); 3198188144Sjamie return (EAFNOSUPPORT); 3199191673Sjamie } 3200188144Sjamie 3201185435Sbz if (IN6_IS_ADDR_LOOPBACK(ia6)) { 3202191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3203191673Sjamie mtx_unlock(&pr->pr_mtx); 3204185435Sbz return (0); 320581114Srwatson } 3206185435Sbz 3207188144Sjamie if (IN6_IS_ADDR_UNSPECIFIED(ia6)) { 3208188144Sjamie /* 3209188144Sjamie * In case there is only 1 IPv6 address, and v6only is true, 3210188144Sjamie * then bind directly. 3211188144Sjamie */ 3212191673Sjamie if (v6only != 0 && pr->pr_ip6s == 1) 3213191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3214191673Sjamie mtx_unlock(&pr->pr_mtx); 3215185435Sbz return (0); 3216185435Sbz } 3217188144Sjamie 3218191673Sjamie error = _prison_check_ip6(pr, ia6); 3219191673Sjamie mtx_unlock(&pr->pr_mtx); 3220191673Sjamie return (error); 3221185435Sbz} 3222185435Sbz 3223185435Sbz/* 3224185435Sbz * Rewrite destination address in case we will connect to loopback address. 3225185435Sbz * 3226188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6. 3227185435Sbz */ 3228185435Sbzint 3229185435Sbzprison_remote_ip6(struct ucred *cred, struct in6_addr *ia6) 3230185435Sbz{ 3231191673Sjamie struct prison *pr; 3232185435Sbz 3233185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3234185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3235185435Sbz 3236192895Sjamie pr = cred->cr_prison; 3237192895Sjamie if (!(pr->pr_flags & PR_IP6)) 3238185435Sbz return (0); 3239191673Sjamie mtx_lock(&pr->pr_mtx); 3240192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3241192895Sjamie mtx_unlock(&pr->pr_mtx); 3242192895Sjamie return (0); 3243192895Sjamie } 3244191673Sjamie if (pr->pr_ip6 == NULL) { 3245191673Sjamie mtx_unlock(&pr->pr_mtx); 3246188144Sjamie return (EAFNOSUPPORT); 3247191673Sjamie } 3248188144Sjamie 3249185435Sbz if (IN6_IS_ADDR_LOOPBACK(ia6)) { 3250191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3251191673Sjamie mtx_unlock(&pr->pr_mtx); 3252185435Sbz return (0); 3253185435Sbz } 3254185435Sbz 3255185435Sbz /* 3256185435Sbz * Return success because nothing had to be changed. 3257185435Sbz */ 3258191673Sjamie mtx_unlock(&pr->pr_mtx); 325946155Sphk return (0); 326046155Sphk} 326146155Sphk 3262185435Sbz/* 3263188144Sjamie * Check if given address belongs to the jail referenced by cred/prison. 3264185435Sbz * 3265192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail, 3266192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 3267192895Sjamie * doesn't allow IPv6. 3268185435Sbz */ 3269185435Sbzstatic int 3270185435Sbz_prison_check_ip6(struct prison *pr, struct in6_addr *ia6) 327146155Sphk{ 3272185435Sbz int i, a, z, d; 327346155Sphk 3274185435Sbz /* 3275185435Sbz * Check the primary IP. 3276185435Sbz */ 3277185435Sbz if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6)) 3278188144Sjamie return (0); 3279185435Sbz 3280185435Sbz /* 3281185435Sbz * All the other IPs are sorted so we can do a binary search. 3282185435Sbz */ 3283185435Sbz a = 0; 3284185435Sbz z = pr->pr_ip6s - 2; 3285185435Sbz while (a <= z) { 3286185435Sbz i = (a + z) / 2; 3287185435Sbz d = qcmp_v6(&pr->pr_ip6[i+1], ia6); 3288185435Sbz if (d > 0) 3289185435Sbz z = i - 1; 3290185435Sbz else if (d < 0) 3291185435Sbz a = i + 1; 329246155Sphk else 3293188144Sjamie return (0); 329446155Sphk } 3295188144Sjamie 3296188144Sjamie return (EADDRNOTAVAIL); 329746155Sphk} 329846155Sphk 329946155Sphkint 3300185435Sbzprison_check_ip6(struct ucred *cred, struct in6_addr *ia6) 3301185435Sbz{ 3302191673Sjamie struct prison *pr; 3303191673Sjamie int error; 3304185435Sbz 3305185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3306185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3307185435Sbz 3308192895Sjamie pr = cred->cr_prison; 3309192895Sjamie if (!(pr->pr_flags & PR_IP6)) 3310188144Sjamie return (0); 3311191673Sjamie mtx_lock(&pr->pr_mtx); 3312192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3313192895Sjamie mtx_unlock(&pr->pr_mtx); 3314192895Sjamie return (0); 3315192895Sjamie } 3316191673Sjamie if (pr->pr_ip6 == NULL) { 3317191673Sjamie mtx_unlock(&pr->pr_mtx); 3318188144Sjamie return (EAFNOSUPPORT); 3319191673Sjamie } 3320185435Sbz 3321191673Sjamie error = _prison_check_ip6(pr, ia6); 3322191673Sjamie mtx_unlock(&pr->pr_mtx); 3323191673Sjamie return (error); 3324185435Sbz} 3325185435Sbz#endif 3326185435Sbz 3327185435Sbz/* 3328188146Sjamie * Check if a jail supports the given address family. 3329188146Sjamie * 3330188146Sjamie * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT 3331188146Sjamie * if not. 3332188146Sjamie */ 3333188146Sjamieint 3334188146Sjamieprison_check_af(struct ucred *cred, int af) 3335188146Sjamie{ 3336192895Sjamie struct prison *pr; 3337188146Sjamie int error; 3338188146Sjamie 3339188146Sjamie KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3340188146Sjamie 3341192895Sjamie pr = cred->cr_prison; 3342194923Sjamie#ifdef VIMAGE 3343194915Sjamie /* Prisons with their own network stack are not limited. */ 3344200473Sbz if (prison_owns_vnet(cred)) 3345194915Sjamie return (0); 3346194923Sjamie#endif 3347194915Sjamie 3348188146Sjamie error = 0; 3349188146Sjamie switch (af) 3350188146Sjamie { 3351188146Sjamie#ifdef INET 3352188146Sjamie case AF_INET: 3353192895Sjamie if (pr->pr_flags & PR_IP4) 3354192895Sjamie { 3355192895Sjamie mtx_lock(&pr->pr_mtx); 3356192895Sjamie if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL) 3357192895Sjamie error = EAFNOSUPPORT; 3358192895Sjamie mtx_unlock(&pr->pr_mtx); 3359192895Sjamie } 3360188146Sjamie break; 3361188146Sjamie#endif 3362188146Sjamie#ifdef INET6 3363188146Sjamie case AF_INET6: 3364192895Sjamie if (pr->pr_flags & PR_IP6) 3365192895Sjamie { 3366192895Sjamie mtx_lock(&pr->pr_mtx); 3367192895Sjamie if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL) 3368192895Sjamie error = EAFNOSUPPORT; 3369192895Sjamie mtx_unlock(&pr->pr_mtx); 3370192895Sjamie } 3371188146Sjamie break; 3372188146Sjamie#endif 3373188146Sjamie case AF_LOCAL: 3374188146Sjamie case AF_ROUTE: 3375188146Sjamie break; 3376188146Sjamie default: 3377192895Sjamie if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF)) 3378188146Sjamie error = EAFNOSUPPORT; 3379188146Sjamie } 3380188146Sjamie return (error); 3381188146Sjamie} 3382188146Sjamie 3383188146Sjamie/* 3384185435Sbz * Check if given address belongs to the jail referenced by cred (wrapper to 3385185435Sbz * prison_check_ip[46]). 3386185435Sbz * 3387192895Sjamie * Returns 0 if jail doesn't restrict the address family or if address belongs 3388192895Sjamie * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if 3389192895Sjamie * the jail doesn't allow the address family. IPv4 Address passed in in NBO. 3390185435Sbz */ 3391185435Sbzint 339272786Srwatsonprison_if(struct ucred *cred, struct sockaddr *sa) 339346155Sphk{ 3394185435Sbz#ifdef INET 3395114168Smike struct sockaddr_in *sai; 3396185435Sbz#endif 3397185435Sbz#ifdef INET6 3398185435Sbz struct sockaddr_in6 *sai6; 3399185435Sbz#endif 3400188144Sjamie int error; 340146155Sphk 3402185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3403185435Sbz KASSERT(sa != NULL, ("%s: sa is NULL", __func__)); 3404185435Sbz 3405200473Sbz#ifdef VIMAGE 3406200473Sbz if (prison_owns_vnet(cred)) 3407200473Sbz return (0); 3408200473Sbz#endif 3409200473Sbz 3410188144Sjamie error = 0; 3411188144Sjamie switch (sa->sa_family) 3412185435Sbz { 3413185435Sbz#ifdef INET 3414185435Sbz case AF_INET: 3415185435Sbz sai = (struct sockaddr_in *)sa; 3416188144Sjamie error = prison_check_ip4(cred, &sai->sin_addr); 3417185435Sbz break; 3418185435Sbz#endif 3419185435Sbz#ifdef INET6 3420185435Sbz case AF_INET6: 3421185435Sbz sai6 = (struct sockaddr_in6 *)sa; 3422188144Sjamie error = prison_check_ip6(cred, &sai6->sin6_addr); 3423185435Sbz break; 3424185435Sbz#endif 3425185435Sbz default: 3426192895Sjamie if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF)) 3427188144Sjamie error = EAFNOSUPPORT; 3428185435Sbz } 3429188144Sjamie return (error); 343046155Sphk} 343172786Srwatson 343272786Srwatson/* 343372786Srwatson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH. 343472786Srwatson */ 343572786Srwatsonint 3436114168Smikeprison_check(struct ucred *cred1, struct ucred *cred2) 343772786Srwatson{ 343872786Srwatson 3439192895Sjamie return ((cred1->cr_prison == cred2->cr_prison || 3440192895Sjamie prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH); 3441192895Sjamie} 344272786Srwatson 3443192895Sjamie/* 3444192895Sjamie * Return 1 if p2 is a child of p1, otherwise 0. 3445192895Sjamie */ 3446192895Sjamieint 3447192895Sjamieprison_ischild(struct prison *pr1, struct prison *pr2) 3448192895Sjamie{ 3449192895Sjamie 3450192895Sjamie for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent) 3451192895Sjamie if (pr1 == pr2) 3452192895Sjamie return (1); 345372786Srwatson return (0); 345472786Srwatson} 345572786Srwatson 345672786Srwatson/* 345772786Srwatson * Return 1 if the passed credential is in a jail, otherwise 0. 345872786Srwatson */ 345972786Srwatsonint 3460114168Smikejailed(struct ucred *cred) 346172786Srwatson{ 346272786Srwatson 3463192895Sjamie return (cred->cr_prison != &prison0); 346472786Srwatson} 346591384Srobert 346691384Srobert/* 3467200473Sbz * Return 1 if the passed credential is in a jail and that jail does not 3468200473Sbz * have its own virtual network stack, otherwise 0. 3469200473Sbz */ 3470200473Sbzint 3471200473Sbzjailed_without_vnet(struct ucred *cred) 3472200473Sbz{ 3473200473Sbz 3474200473Sbz if (!jailed(cred)) 3475200473Sbz return (0); 3476200473Sbz#ifdef VIMAGE 3477200473Sbz if (prison_owns_vnet(cred)) 3478200473Sbz return (0); 3479200473Sbz#endif 3480200473Sbz 3481200473Sbz return (1); 3482200473Sbz} 3483200473Sbz 3484200473Sbz/* 3485194090Sjamie * Return the correct hostname (domainname, et al) for the passed credential. 348691384Srobert */ 348791391Srobertvoid 3488114168Smikegetcredhostname(struct ucred *cred, char *buf, size_t size) 348991384Srobert{ 3490193066Sjamie struct prison *pr; 349191384Srobert 3492194090Sjamie /* 3493194090Sjamie * A NULL credential can be used to shortcut to the physical 3494194090Sjamie * system's hostname. 3495194090Sjamie */ 3496193066Sjamie pr = (cred != NULL) ? cred->cr_prison : &prison0; 3497193066Sjamie mtx_lock(&pr->pr_mtx); 3498194118Sjamie strlcpy(buf, pr->pr_hostname, size); 3499193066Sjamie mtx_unlock(&pr->pr_mtx); 350091384Srobert} 3501113275Smike 3502194090Sjamievoid 3503194090Sjamiegetcreddomainname(struct ucred *cred, char *buf, size_t size) 3504194090Sjamie{ 3505194090Sjamie 3506194090Sjamie mtx_lock(&cred->cr_prison->pr_mtx); 3507194118Sjamie strlcpy(buf, cred->cr_prison->pr_domainname, size); 3508194090Sjamie mtx_unlock(&cred->cr_prison->pr_mtx); 3509194090Sjamie} 3510194090Sjamie 3511194090Sjamievoid 3512194090Sjamiegetcredhostuuid(struct ucred *cred, char *buf, size_t size) 3513194090Sjamie{ 3514194090Sjamie 3515194090Sjamie mtx_lock(&cred->cr_prison->pr_mtx); 3516194118Sjamie strlcpy(buf, cred->cr_prison->pr_hostuuid, size); 3517194090Sjamie mtx_unlock(&cred->cr_prison->pr_mtx); 3518194090Sjamie} 3519194090Sjamie 3520194090Sjamievoid 3521194090Sjamiegetcredhostid(struct ucred *cred, unsigned long *hostid) 3522194090Sjamie{ 3523194090Sjamie 3524194090Sjamie mtx_lock(&cred->cr_prison->pr_mtx); 3525194090Sjamie *hostid = cred->cr_prison->pr_hostid; 3526194090Sjamie mtx_unlock(&cred->cr_prison->pr_mtx); 3527194090Sjamie} 3528194090Sjamie 3529196176Sbz#ifdef VIMAGE 3530125804Srwatson/* 3531196176Sbz * Determine whether the prison represented by cred owns 3532196176Sbz * its vnet rather than having it inherited. 3533196176Sbz * 3534196176Sbz * Returns 1 in case the prison owns the vnet, 0 otherwise. 3535196176Sbz */ 3536196176Sbzint 3537196176Sbzprison_owns_vnet(struct ucred *cred) 3538196176Sbz{ 3539196176Sbz 3540196176Sbz /* 3541196176Sbz * vnets cannot be added/removed after jail creation, 3542196176Sbz * so no need to lock here. 3543196176Sbz */ 3544196176Sbz return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0); 3545196176Sbz} 3546196176Sbz#endif 3547196176Sbz 3548196176Sbz/* 3549147185Spjd * Determine whether the subject represented by cred can "see" 3550147185Spjd * status of a mount point. 3551147185Spjd * Returns: 0 for permitted, ENOENT otherwise. 3552147185Spjd * XXX: This function should be called cr_canseemount() and should be 3553147185Spjd * placed in kern_prot.c. 3554125804Srwatson */ 3555125804Srwatsonint 3556147185Spjdprison_canseemount(struct ucred *cred, struct mount *mp) 3557125804Srwatson{ 3558147185Spjd struct prison *pr; 3559147185Spjd struct statfs *sp; 3560147185Spjd size_t len; 3561125804Srwatson 3562192895Sjamie pr = cred->cr_prison; 3563192895Sjamie if (pr->pr_enforce_statfs == 0) 3564147185Spjd return (0); 3565147185Spjd if (pr->pr_root->v_mount == mp) 3566147185Spjd return (0); 3567192895Sjamie if (pr->pr_enforce_statfs == 2) 3568147185Spjd return (ENOENT); 3569147185Spjd /* 3570147185Spjd * If jail's chroot directory is set to "/" we should be able to see 3571147185Spjd * all mount-points from inside a jail. 3572147185Spjd * This is ugly check, but this is the only situation when jail's 3573147185Spjd * directory ends with '/'. 3574147185Spjd */ 3575147185Spjd if (strcmp(pr->pr_path, "/") == 0) 3576147185Spjd return (0); 3577147185Spjd len = strlen(pr->pr_path); 3578147185Spjd sp = &mp->mnt_stat; 3579147185Spjd if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0) 3580147185Spjd return (ENOENT); 3581147185Spjd /* 3582147185Spjd * Be sure that we don't have situation where jail's root directory 3583147185Spjd * is "/some/path" and mount point is "/some/pathpath". 3584147185Spjd */ 3585147185Spjd if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/') 3586147185Spjd return (ENOENT); 3587147185Spjd return (0); 3588147185Spjd} 3589147185Spjd 3590147185Spjdvoid 3591147185Spjdprison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp) 3592147185Spjd{ 3593147185Spjd char jpath[MAXPATHLEN]; 3594147185Spjd struct prison *pr; 3595147185Spjd size_t len; 3596147185Spjd 3597192895Sjamie pr = cred->cr_prison; 3598192895Sjamie if (pr->pr_enforce_statfs == 0) 3599147185Spjd return; 3600147185Spjd if (prison_canseemount(cred, mp) != 0) { 3601147185Spjd bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3602147185Spjd strlcpy(sp->f_mntonname, "[restricted]", 3603147185Spjd sizeof(sp->f_mntonname)); 3604147185Spjd return; 3605125804Srwatson } 3606147185Spjd if (pr->pr_root->v_mount == mp) { 3607147185Spjd /* 3608147185Spjd * Clear current buffer data, so we are sure nothing from 3609147185Spjd * the valid path left there. 3610147185Spjd */ 3611147185Spjd bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3612147185Spjd *sp->f_mntonname = '/'; 3613147185Spjd return; 3614147185Spjd } 3615147185Spjd /* 3616147185Spjd * If jail's chroot directory is set to "/" we should be able to see 3617147185Spjd * all mount-points from inside a jail. 3618147185Spjd */ 3619147185Spjd if (strcmp(pr->pr_path, "/") == 0) 3620147185Spjd return; 3621147185Spjd len = strlen(pr->pr_path); 3622147185Spjd strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath)); 3623147185Spjd /* 3624147185Spjd * Clear current buffer data, so we are sure nothing from 3625147185Spjd * the valid path left there. 3626147185Spjd */ 3627147185Spjd bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3628147185Spjd if (*jpath == '\0') { 3629147185Spjd /* Should never happen. */ 3630147185Spjd *sp->f_mntonname = '/'; 3631147185Spjd } else { 3632147185Spjd strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname)); 3633147185Spjd } 3634125804Srwatson} 3635125804Srwatson 3636164032Srwatson/* 3637164032Srwatson * Check with permission for a specific privilege is granted within jail. We 3638164032Srwatson * have a specific list of accepted privileges; the rest are denied. 3639164032Srwatson */ 3640164032Srwatsonint 3641164032Srwatsonprison_priv_check(struct ucred *cred, int priv) 3642164032Srwatson{ 3643164032Srwatson 3644164032Srwatson if (!jailed(cred)) 3645164032Srwatson return (0); 3646164032Srwatson 3647194915Sjamie#ifdef VIMAGE 3648194915Sjamie /* 3649194915Sjamie * Privileges specific to prisons with a virtual network stack. 3650194915Sjamie * There might be a duplicate entry here in case the privilege 3651194915Sjamie * is only granted conditionally in the legacy jail case. 3652194915Sjamie */ 3653164032Srwatson switch (priv) { 3654194915Sjamie#ifdef notyet 3655194915Sjamie /* 3656194915Sjamie * NFS-specific privileges. 3657194915Sjamie */ 3658194915Sjamie case PRIV_NFS_DAEMON: 3659194915Sjamie case PRIV_NFS_LOCKD: 3660194915Sjamie#endif 3661194915Sjamie /* 3662194915Sjamie * Network stack privileges. 3663194915Sjamie */ 3664194915Sjamie case PRIV_NET_BRIDGE: 3665194915Sjamie case PRIV_NET_GRE: 3666194915Sjamie case PRIV_NET_BPF: 3667194915Sjamie case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */ 3668194915Sjamie case PRIV_NET_ROUTE: 3669194915Sjamie case PRIV_NET_TAP: 3670194915Sjamie case PRIV_NET_SETIFMTU: 3671194915Sjamie case PRIV_NET_SETIFFLAGS: 3672194915Sjamie case PRIV_NET_SETIFCAP: 3673203052Sdelphij case PRIV_NET_SETIFDESCR: 3674194915Sjamie case PRIV_NET_SETIFNAME : 3675194915Sjamie case PRIV_NET_SETIFMETRIC: 3676194915Sjamie case PRIV_NET_SETIFPHYS: 3677194915Sjamie case PRIV_NET_SETIFMAC: 3678194915Sjamie case PRIV_NET_ADDMULTI: 3679194915Sjamie case PRIV_NET_DELMULTI: 3680194915Sjamie case PRIV_NET_HWIOCTL: 3681194915Sjamie case PRIV_NET_SETLLADDR: 3682194915Sjamie case PRIV_NET_ADDIFGROUP: 3683194915Sjamie case PRIV_NET_DELIFGROUP: 3684194915Sjamie case PRIV_NET_IFCREATE: 3685194915Sjamie case PRIV_NET_IFDESTROY: 3686194915Sjamie case PRIV_NET_ADDIFADDR: 3687194915Sjamie case PRIV_NET_DELIFADDR: 3688194915Sjamie case PRIV_NET_LAGG: 3689194915Sjamie case PRIV_NET_GIF: 3690194915Sjamie case PRIV_NET_SETIFVNET: 3691223735Sbz case PRIV_NET_SETIFFIB: 3692164032Srwatson 3693164032Srwatson /* 3694194915Sjamie * 802.11-related privileges. 3695194915Sjamie */ 3696194915Sjamie case PRIV_NET80211_GETKEY: 3697194915Sjamie#ifdef notyet 3698194915Sjamie case PRIV_NET80211_MANAGE: /* XXX-BZ discuss with sam@ */ 3699194915Sjamie#endif 3700194915Sjamie 3701194915Sjamie#ifdef notyet 3702194915Sjamie /* 3703194915Sjamie * AppleTalk privileges. 3704194915Sjamie */ 3705194915Sjamie case PRIV_NETATALK_RESERVEDPORT: 3706194915Sjamie 3707194915Sjamie /* 3708194915Sjamie * ATM privileges. 3709194915Sjamie */ 3710194915Sjamie case PRIV_NETATM_CFG: 3711194915Sjamie case PRIV_NETATM_ADD: 3712194915Sjamie case PRIV_NETATM_DEL: 3713194915Sjamie case PRIV_NETATM_SET: 3714194915Sjamie 3715194915Sjamie /* 3716194915Sjamie * Bluetooth privileges. 3717194915Sjamie */ 3718194915Sjamie case PRIV_NETBLUETOOTH_RAW: 3719194915Sjamie#endif 3720194915Sjamie 3721194915Sjamie /* 3722194915Sjamie * Netgraph and netgraph module privileges. 3723194915Sjamie */ 3724194915Sjamie case PRIV_NETGRAPH_CONTROL: 3725194915Sjamie#ifdef notyet 3726194915Sjamie case PRIV_NETGRAPH_TTY: 3727194915Sjamie#endif 3728194915Sjamie 3729194915Sjamie /* 3730194915Sjamie * IPv4 and IPv6 privileges. 3731194915Sjamie */ 3732194915Sjamie case PRIV_NETINET_IPFW: 3733194915Sjamie case PRIV_NETINET_DIVERT: 3734194915Sjamie case PRIV_NETINET_PF: 3735194915Sjamie case PRIV_NETINET_DUMMYNET: 3736194915Sjamie case PRIV_NETINET_CARP: 3737194915Sjamie case PRIV_NETINET_MROUTE: 3738194915Sjamie case PRIV_NETINET_RAW: 3739194915Sjamie case PRIV_NETINET_ADDRCTRL6: 3740194915Sjamie case PRIV_NETINET_ND6: 3741194915Sjamie case PRIV_NETINET_SCOPE6: 3742194915Sjamie case PRIV_NETINET_ALIFETIME6: 3743194915Sjamie case PRIV_NETINET_IPSEC: 3744194915Sjamie case PRIV_NETINET_BINDANY: 3745194915Sjamie 3746194915Sjamie#ifdef notyet 3747194915Sjamie /* 3748194915Sjamie * IPX/SPX privileges. 3749194915Sjamie */ 3750194915Sjamie case PRIV_NETIPX_RESERVEDPORT: 3751194915Sjamie case PRIV_NETIPX_RAW: 3752194915Sjamie 3753194915Sjamie /* 3754194915Sjamie * NCP privileges. 3755194915Sjamie */ 3756194915Sjamie case PRIV_NETNCP: 3757194915Sjamie 3758194915Sjamie /* 3759194915Sjamie * SMB privileges. 3760194915Sjamie */ 3761194915Sjamie case PRIV_NETSMB: 3762194915Sjamie#endif 3763194915Sjamie 3764194915Sjamie /* 3765194915Sjamie * No default: or deny here. 3766194915Sjamie * In case of no permit fall through to next switch(). 3767194915Sjamie */ 3768194915Sjamie if (cred->cr_prison->pr_flags & PR_VNET) 3769194915Sjamie return (0); 3770194915Sjamie } 3771194915Sjamie#endif /* VIMAGE */ 3772194915Sjamie 3773194915Sjamie switch (priv) { 3774194915Sjamie 3775194915Sjamie /* 3776164032Srwatson * Allow ktrace privileges for root in jail. 3777164032Srwatson */ 3778164032Srwatson case PRIV_KTRACE: 3779164032Srwatson 3780166827Srwatson#if 0 3781164032Srwatson /* 3782164032Srwatson * Allow jailed processes to configure audit identity and 3783164032Srwatson * submit audit records (login, etc). In the future we may 3784164032Srwatson * want to further refine the relationship between audit and 3785164032Srwatson * jail. 3786164032Srwatson */ 3787164032Srwatson case PRIV_AUDIT_GETAUDIT: 3788164032Srwatson case PRIV_AUDIT_SETAUDIT: 3789164032Srwatson case PRIV_AUDIT_SUBMIT: 3790166827Srwatson#endif 3791164032Srwatson 3792164032Srwatson /* 3793164032Srwatson * Allow jailed processes to manipulate process UNIX 3794164032Srwatson * credentials in any way they see fit. 3795164032Srwatson */ 3796164032Srwatson case PRIV_CRED_SETUID: 3797164032Srwatson case PRIV_CRED_SETEUID: 3798164032Srwatson case PRIV_CRED_SETGID: 3799164032Srwatson case PRIV_CRED_SETEGID: 3800164032Srwatson case PRIV_CRED_SETGROUPS: 3801164032Srwatson case PRIV_CRED_SETREUID: 3802164032Srwatson case PRIV_CRED_SETREGID: 3803164032Srwatson case PRIV_CRED_SETRESUID: 3804164032Srwatson case PRIV_CRED_SETRESGID: 3805164032Srwatson 3806164032Srwatson /* 3807164032Srwatson * Jail implements visibility constraints already, so allow 3808164032Srwatson * jailed root to override uid/gid-based constraints. 3809164032Srwatson */ 3810164032Srwatson case PRIV_SEEOTHERGIDS: 3811164032Srwatson case PRIV_SEEOTHERUIDS: 3812164032Srwatson 3813164032Srwatson /* 3814164032Srwatson * Jail implements inter-process debugging limits already, so 3815164032Srwatson * allow jailed root various debugging privileges. 3816164032Srwatson */ 3817164032Srwatson case PRIV_DEBUG_DIFFCRED: 3818164032Srwatson case PRIV_DEBUG_SUGID: 3819164032Srwatson case PRIV_DEBUG_UNPRIV: 3820164032Srwatson 3821164032Srwatson /* 3822164032Srwatson * Allow jail to set various resource limits and login 3823164032Srwatson * properties, and for now, exceed process resource limits. 3824164032Srwatson */ 3825164032Srwatson case PRIV_PROC_LIMIT: 3826164032Srwatson case PRIV_PROC_SETLOGIN: 3827164032Srwatson case PRIV_PROC_SETRLIMIT: 3828164032Srwatson 3829164032Srwatson /* 3830164032Srwatson * System V and POSIX IPC privileges are granted in jail. 3831164032Srwatson */ 3832164032Srwatson case PRIV_IPC_READ: 3833164032Srwatson case PRIV_IPC_WRITE: 3834164032Srwatson case PRIV_IPC_ADMIN: 3835164032Srwatson case PRIV_IPC_MSGSIZE: 3836164032Srwatson case PRIV_MQ_ADMIN: 3837164032Srwatson 3838164032Srwatson /* 3839192895Sjamie * Jail operations within a jail work on child jails. 3840192895Sjamie */ 3841192895Sjamie case PRIV_JAIL_ATTACH: 3842192895Sjamie case PRIV_JAIL_SET: 3843192895Sjamie case PRIV_JAIL_REMOVE: 3844192895Sjamie 3845192895Sjamie /* 3846164032Srwatson * Jail implements its own inter-process limits, so allow 3847164032Srwatson * root processes in jail to change scheduling on other 3848164032Srwatson * processes in the same jail. Likewise for signalling. 3849164032Srwatson */ 3850164032Srwatson case PRIV_SCHED_DIFFCRED: 3851185435Sbz case PRIV_SCHED_CPUSET: 3852164032Srwatson case PRIV_SIGNAL_DIFFCRED: 3853164032Srwatson case PRIV_SIGNAL_SUGID: 3854164032Srwatson 3855164032Srwatson /* 3856164032Srwatson * Allow jailed processes to write to sysctls marked as jail 3857164032Srwatson * writable. 3858164032Srwatson */ 3859164032Srwatson case PRIV_SYSCTL_WRITEJAIL: 3860164032Srwatson 3861164032Srwatson /* 3862164032Srwatson * Allow root in jail to manage a variety of quota 3863166831Srwatson * properties. These should likely be conditional on a 3864166831Srwatson * configuration option. 3865164032Srwatson */ 3866166832Srwatson case PRIV_VFS_GETQUOTA: 3867166832Srwatson case PRIV_VFS_SETQUOTA: 3868164032Srwatson 3869164032Srwatson /* 3870164032Srwatson * Since Jail relies on chroot() to implement file system 3871164032Srwatson * protections, grant many VFS privileges to root in jail. 3872164032Srwatson * Be careful to exclude mount-related and NFS-related 3873164032Srwatson * privileges. 3874164032Srwatson */ 3875164032Srwatson case PRIV_VFS_READ: 3876164032Srwatson case PRIV_VFS_WRITE: 3877164032Srwatson case PRIV_VFS_ADMIN: 3878164032Srwatson case PRIV_VFS_EXEC: 3879164032Srwatson case PRIV_VFS_LOOKUP: 3880164032Srwatson case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */ 3881164032Srwatson case PRIV_VFS_CHFLAGS_DEV: 3882164032Srwatson case PRIV_VFS_CHOWN: 3883164032Srwatson case PRIV_VFS_CHROOT: 3884167152Spjd case PRIV_VFS_RETAINSUGID: 3885164032Srwatson case PRIV_VFS_FCHROOT: 3886164032Srwatson case PRIV_VFS_LINK: 3887164032Srwatson case PRIV_VFS_SETGID: 3888172860Srwatson case PRIV_VFS_STAT: 3889164032Srwatson case PRIV_VFS_STICKYFILE: 3890255316Sjamie 3891255316Sjamie /* 3892255316Sjamie * As in the non-jail case, non-root users are expected to be 3893255316Sjamie * able to read kernel/phyiscal memory (provided /dev/[k]mem 3894255316Sjamie * exists in the jail and they have permission to access it). 3895255316Sjamie */ 3896255316Sjamie case PRIV_KMEM_READ: 3897164032Srwatson return (0); 3898164032Srwatson 3899164032Srwatson /* 3900164032Srwatson * Depending on the global setting, allow privilege of 3901164032Srwatson * setting system flags. 3902164032Srwatson */ 3903164032Srwatson case PRIV_VFS_SYSFLAGS: 3904192895Sjamie if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS) 3905164032Srwatson return (0); 3906164032Srwatson else 3907164032Srwatson return (EPERM); 3908164032Srwatson 3909164032Srwatson /* 3910168396Spjd * Depending on the global setting, allow privilege of 3911168396Spjd * mounting/unmounting file systems. 3912168396Spjd */ 3913168396Spjd case PRIV_VFS_MOUNT: 3914168396Spjd case PRIV_VFS_UNMOUNT: 3915168396Spjd case PRIV_VFS_MOUNT_NONUSER: 3916168699Spjd case PRIV_VFS_MOUNT_OWNER: 3917224615Smm if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT && 3918224615Smm cred->cr_prison->pr_enforce_statfs < 2) 3919168396Spjd return (0); 3920168396Spjd else 3921168396Spjd return (EPERM); 3922168396Spjd 3923168396Spjd /* 3924168591Srwatson * Allow jailed root to bind reserved ports and reuse in-use 3925168591Srwatson * ports. 3926164032Srwatson */ 3927164032Srwatson case PRIV_NETINET_RESERVEDPORT: 3928168591Srwatson case PRIV_NETINET_REUSEPORT: 3929164032Srwatson return (0); 3930164032Srwatson 3931164032Srwatson /* 3932175630Sbz * Allow jailed root to set certian IPv4/6 (option) headers. 3933175630Sbz */ 3934175630Sbz case PRIV_NETINET_SETHDROPTS: 3935175630Sbz return (0); 3936175630Sbz 3937175630Sbz /* 3938164032Srwatson * Conditionally allow creating raw sockets in jail. 3939164032Srwatson */ 3940164032Srwatson case PRIV_NETINET_RAW: 3941192895Sjamie if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS) 3942164032Srwatson return (0); 3943164032Srwatson else 3944164032Srwatson return (EPERM); 3945164032Srwatson 3946164032Srwatson /* 3947164032Srwatson * Since jail implements its own visibility limits on netstat 3948164032Srwatson * sysctls, allow getcred. This allows identd to work in 3949164032Srwatson * jail. 3950164032Srwatson */ 3951164032Srwatson case PRIV_NETINET_GETCRED: 3952164032Srwatson return (0); 3953164032Srwatson 3954219304Strasz /* 3955219304Strasz * Allow jailed root to set loginclass. 3956219304Strasz */ 3957219304Strasz case PRIV_PROC_SETLOGINCLASS: 3958219304Strasz return (0); 3959219304Strasz 3960164032Srwatson default: 3961164032Srwatson /* 3962164032Srwatson * In all remaining cases, deny the privilege request. This 3963164032Srwatson * includes almost all network privileges, many system 3964164032Srwatson * configuration privileges. 3965164032Srwatson */ 3966164032Srwatson return (EPERM); 3967164032Srwatson } 3968164032Srwatson} 3969164032Srwatson 3970192895Sjamie/* 3971192895Sjamie * Return the part of pr2's name that is relative to pr1, or the whole name 3972192895Sjamie * if it does not directly follow. 3973192895Sjamie */ 3974192895Sjamie 3975192895Sjamiechar * 3976192895Sjamieprison_name(struct prison *pr1, struct prison *pr2) 3977192895Sjamie{ 3978192895Sjamie char *name; 3979192895Sjamie 3980192895Sjamie /* Jails see themselves as "0" (if they see themselves at all). */ 3981192895Sjamie if (pr1 == pr2) 3982192895Sjamie return "0"; 3983192895Sjamie name = pr2->pr_name; 3984192895Sjamie if (prison_ischild(pr1, pr2)) { 3985192895Sjamie /* 3986192895Sjamie * pr1 isn't locked (and allprison_lock may not be either) 3987192895Sjamie * so its length can't be counted on. But the number of dots 3988192895Sjamie * can be counted on - and counted. 3989192895Sjamie */ 3990192895Sjamie for (; pr1 != &prison0; pr1 = pr1->pr_parent) 3991192895Sjamie name = strchr(name, '.') + 1; 3992192895Sjamie } 3993192895Sjamie return (name); 3994192895Sjamie} 3995192895Sjamie 3996192895Sjamie/* 3997192895Sjamie * Return the part of pr2's path that is relative to pr1, or the whole path 3998192895Sjamie * if it does not directly follow. 3999192895Sjamie */ 4000192895Sjamiestatic char * 4001192895Sjamieprison_path(struct prison *pr1, struct prison *pr2) 4002192895Sjamie{ 4003192895Sjamie char *path1, *path2; 4004192895Sjamie int len1; 4005192895Sjamie 4006192895Sjamie path1 = pr1->pr_path; 4007192895Sjamie path2 = pr2->pr_path; 4008192895Sjamie if (!strcmp(path1, "/")) 4009192895Sjamie return (path2); 4010192895Sjamie len1 = strlen(path1); 4011192895Sjamie if (strncmp(path1, path2, len1)) 4012192895Sjamie return (path2); 4013192895Sjamie if (path2[len1] == '\0') 4014192895Sjamie return "/"; 4015192895Sjamie if (path2[len1] == '/') 4016192895Sjamie return (path2 + len1); 4017192895Sjamie return (path2); 4018192895Sjamie} 4019192895Sjamie 4020192895Sjamie 4021192895Sjamie/* 4022192895Sjamie * Jail-related sysctls. 4023192895Sjamie */ 4024227309Sedstatic SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0, 4025192895Sjamie "Jails"); 4026192895Sjamie 4027113275Smikestatic int 4028113275Smikesysctl_jail_list(SYSCTL_HANDLER_ARGS) 4029113275Smike{ 4030191673Sjamie struct xprison *xp; 4031192895Sjamie struct prison *pr, *cpr; 4032191673Sjamie#ifdef INET 4033191673Sjamie struct in_addr *ip4 = NULL; 4034191673Sjamie int ip4s = 0; 4035191673Sjamie#endif 4036191673Sjamie#ifdef INET6 4037208803Scperciva struct in6_addr *ip6 = NULL; 4038191673Sjamie int ip6s = 0; 4039191673Sjamie#endif 4040192895Sjamie int descend, error; 4041113275Smike 4042191673Sjamie xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK); 4043192895Sjamie pr = req->td->td_ucred->cr_prison; 4044191673Sjamie error = 0; 4045168401Spjd sx_slock(&allprison_lock); 4046192895Sjamie FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 4047192895Sjamie#if defined(INET) || defined(INET6) 4048191673Sjamie again: 4049192895Sjamie#endif 4050192895Sjamie mtx_lock(&cpr->pr_mtx); 4051185435Sbz#ifdef INET 4052192895Sjamie if (cpr->pr_ip4s > 0) { 4053192895Sjamie if (ip4s < cpr->pr_ip4s) { 4054192895Sjamie ip4s = cpr->pr_ip4s; 4055192895Sjamie mtx_unlock(&cpr->pr_mtx); 4056191673Sjamie ip4 = realloc(ip4, ip4s * 4057191673Sjamie sizeof(struct in_addr), M_TEMP, M_WAITOK); 4058191673Sjamie goto again; 4059191673Sjamie } 4060192895Sjamie bcopy(cpr->pr_ip4, ip4, 4061192895Sjamie cpr->pr_ip4s * sizeof(struct in_addr)); 4062191673Sjamie } 4063185435Sbz#endif 4064185435Sbz#ifdef INET6 4065192895Sjamie if (cpr->pr_ip6s > 0) { 4066192895Sjamie if (ip6s < cpr->pr_ip6s) { 4067192895Sjamie ip6s = cpr->pr_ip6s; 4068192895Sjamie mtx_unlock(&cpr->pr_mtx); 4069191673Sjamie ip6 = realloc(ip6, ip6s * 4070191673Sjamie sizeof(struct in6_addr), M_TEMP, M_WAITOK); 4071191673Sjamie goto again; 4072191673Sjamie } 4073192895Sjamie bcopy(cpr->pr_ip6, ip6, 4074192895Sjamie cpr->pr_ip6s * sizeof(struct in6_addr)); 4075191673Sjamie } 4076185435Sbz#endif 4077192895Sjamie if (cpr->pr_ref == 0) { 4078192895Sjamie mtx_unlock(&cpr->pr_mtx); 4079191673Sjamie continue; 4080191673Sjamie } 4081191673Sjamie bzero(xp, sizeof(*xp)); 4082113275Smike xp->pr_version = XPRISON_VERSION; 4083192895Sjamie xp->pr_id = cpr->pr_id; 4084192895Sjamie xp->pr_state = cpr->pr_uref > 0 4085191673Sjamie ? PRISON_STATE_ALIVE : PRISON_STATE_DYING; 4086192895Sjamie strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path)); 4087194118Sjamie strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host)); 4088192895Sjamie strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name)); 4089185435Sbz#ifdef INET 4090192895Sjamie xp->pr_ip4s = cpr->pr_ip4s; 4091185435Sbz#endif 4092185435Sbz#ifdef INET6 4093192895Sjamie xp->pr_ip6s = cpr->pr_ip6s; 4094185435Sbz#endif 4095192895Sjamie mtx_unlock(&cpr->pr_mtx); 4096191673Sjamie error = SYSCTL_OUT(req, xp, sizeof(*xp)); 4097191673Sjamie if (error) 4098191673Sjamie break; 4099185435Sbz#ifdef INET 4100191673Sjamie if (xp->pr_ip4s > 0) { 4101191673Sjamie error = SYSCTL_OUT(req, ip4, 4102191673Sjamie xp->pr_ip4s * sizeof(struct in_addr)); 4103191673Sjamie if (error) 4104191673Sjamie break; 4105185435Sbz } 4106185435Sbz#endif 4107185435Sbz#ifdef INET6 4108191673Sjamie if (xp->pr_ip6s > 0) { 4109191673Sjamie error = SYSCTL_OUT(req, ip6, 4110191673Sjamie xp->pr_ip6s * sizeof(struct in6_addr)); 4111191673Sjamie if (error) 4112191673Sjamie break; 4113185435Sbz } 4114185435Sbz#endif 4115113275Smike } 4116168401Spjd sx_sunlock(&allprison_lock); 4117191673Sjamie free(xp, M_TEMP); 4118191673Sjamie#ifdef INET 4119191673Sjamie free(ip4, M_TEMP); 4120191673Sjamie#endif 4121191673Sjamie#ifdef INET6 4122191673Sjamie free(ip6, M_TEMP); 4123191673Sjamie#endif 4124167354Spjd return (error); 4125113275Smike} 4126113275Smike 4127187864SedSYSCTL_OID(_security_jail, OID_AUTO, list, 4128187864Sed CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4129187864Sed sysctl_jail_list, "S", "List of active jails"); 4130126004Spjd 4131126004Spjdstatic int 4132126004Spjdsysctl_jail_jailed(SYSCTL_HANDLER_ARGS) 4133126004Spjd{ 4134126004Spjd int error, injail; 4135126004Spjd 4136126004Spjd injail = jailed(req->td->td_ucred); 4137126004Spjd error = SYSCTL_OUT(req, &injail, sizeof(injail)); 4138126004Spjd 4139126004Spjd return (error); 4140126004Spjd} 4141192895Sjamie 4142187864SedSYSCTL_PROC(_security_jail, OID_AUTO, jailed, 4143187864Sed CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4144187864Sed sysctl_jail_jailed, "I", "Process in jail?"); 4145185435Sbz 4146250804Sjamiestatic int 4147250804Sjamiesysctl_jail_vnet(SYSCTL_HANDLER_ARGS) 4148250804Sjamie{ 4149250804Sjamie int error, havevnet; 4150250804Sjamie#ifdef VIMAGE 4151250804Sjamie struct ucred *cred = req->td->td_ucred; 4152250804Sjamie 4153250804Sjamie havevnet = jailed(cred) && prison_owns_vnet(cred); 4154250804Sjamie#else 4155250804Sjamie havevnet = 0; 4156250804Sjamie#endif 4157250804Sjamie error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet)); 4158250804Sjamie 4159250804Sjamie return (error); 4160250804Sjamie} 4161250804Sjamie 4162250804SjamieSYSCTL_PROC(_security_jail, OID_AUTO, vnet, 4163250804Sjamie CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4164250804Sjamie sysctl_jail_vnet, "I", "Jail owns VNET?"); 4165250804Sjamie 4166192895Sjamie#if defined(INET) || defined(INET6) 4167193865SjamieSYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW, 4168192895Sjamie &jail_max_af_ips, 0, 4169192895Sjamie "Number of IP addresses a jail may have at most per address family"); 4170192895Sjamie#endif 4171192895Sjamie 4172192895Sjamie/* 4173192895Sjamie * Default parameters for jail(2) compatability. For historical reasons, 4174192895Sjamie * the sysctl names have varying similarity to the parameter names. Prisons 4175192895Sjamie * just see their own parameters, and can't change them. 4176192895Sjamie */ 4177192895Sjamiestatic int 4178192895Sjamiesysctl_jail_default_allow(SYSCTL_HANDLER_ARGS) 4179192895Sjamie{ 4180192895Sjamie struct prison *pr; 4181192895Sjamie int allow, error, i; 4182192895Sjamie 4183192895Sjamie pr = req->td->td_ucred->cr_prison; 4184192895Sjamie allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow; 4185192895Sjamie 4186192895Sjamie /* Get the current flag value, and convert it to a boolean. */ 4187192895Sjamie i = (allow & arg2) ? 1 : 0; 4188192895Sjamie if (arg1 != NULL) 4189192895Sjamie i = !i; 4190192895Sjamie error = sysctl_handle_int(oidp, &i, 0, req); 4191192895Sjamie if (error || !req->newptr) 4192192895Sjamie return (error); 4193192895Sjamie i = i ? arg2 : 0; 4194192895Sjamie if (arg1 != NULL) 4195192895Sjamie i ^= arg2; 4196192895Sjamie /* 4197192895Sjamie * The sysctls don't have CTLFLAGS_PRISON, so assume prison0 4198192895Sjamie * for writing. 4199192895Sjamie */ 4200192895Sjamie mtx_lock(&prison0.pr_mtx); 4201192895Sjamie jail_default_allow = (jail_default_allow & ~arg2) | i; 4202192895Sjamie mtx_unlock(&prison0.pr_mtx); 4203192895Sjamie return (0); 4204192895Sjamie} 4205192895Sjamie 4206192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed, 4207192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4208192895Sjamie NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I", 4209192895Sjamie "Processes in jail can set their hostnames"); 4210192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only, 4211192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4212192895Sjamie (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I", 4213192895Sjamie "Processes in jail are limited to creating UNIX/IP/route sockets only"); 4214192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed, 4215192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4216192895Sjamie NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I", 4217192895Sjamie "Processes in jail can use System V IPC primitives"); 4218192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets, 4219192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4220192895Sjamie NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I", 4221192895Sjamie "Prison root can create raw sockets"); 4222192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed, 4223192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4224192895Sjamie NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I", 4225192895Sjamie "Processes in jail can alter system file flags"); 4226192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed, 4227192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4228192895Sjamie NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I", 4229192895Sjamie "Processes in jail can mount/unmount jail-friendly file systems"); 4230232059SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_devfs_allowed, 4231232059Smm CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4232232059Smm NULL, PR_ALLOW_MOUNT_DEVFS, sysctl_jail_default_allow, "I", 4233232186Smm "Processes in jail can mount the devfs file system"); 4234232059SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_nullfs_allowed, 4235232059Smm CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4236232059Smm NULL, PR_ALLOW_MOUNT_NULLFS, sysctl_jail_default_allow, "I", 4237232186Smm "Processes in jail can mount the nullfs file system"); 4238232278SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_procfs_allowed, 4239232278Smm CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4240232278Smm NULL, PR_ALLOW_MOUNT_PROCFS, sysctl_jail_default_allow, "I", 4241232278Smm "Processes in jail can mount the procfs file system"); 4242254741SdelphijSYSCTL_PROC(_security_jail, OID_AUTO, mount_tmpfs_allowed, 4243254741Sdelphij CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4244254741Sdelphij NULL, PR_ALLOW_MOUNT_TMPFS, sysctl_jail_default_allow, "I", 4245254741Sdelphij "Processes in jail can mount the tmpfs file system"); 4246232186SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_zfs_allowed, 4247232186Smm CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4248232186Smm NULL, PR_ALLOW_MOUNT_ZFS, sysctl_jail_default_allow, "I", 4249232186Smm "Processes in jail can mount the zfs file system"); 4250192895Sjamie 4251192895Sjamiestatic int 4252192895Sjamiesysctl_jail_default_level(SYSCTL_HANDLER_ARGS) 4253192895Sjamie{ 4254192895Sjamie struct prison *pr; 4255192895Sjamie int level, error; 4256192895Sjamie 4257192895Sjamie pr = req->td->td_ucred->cr_prison; 4258192895Sjamie level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2); 4259192895Sjamie error = sysctl_handle_int(oidp, &level, 0, req); 4260192895Sjamie if (error || !req->newptr) 4261192895Sjamie return (error); 4262192895Sjamie *(int *)arg1 = level; 4263192895Sjamie return (0); 4264192895Sjamie} 4265192895Sjamie 4266192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs, 4267192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4268192895Sjamie &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs), 4269192895Sjamie sysctl_jail_default_level, "I", 4270192895Sjamie "Processes in jail cannot see all mounted file systems"); 4271192895Sjamie 4272231267SmmSYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset, 4273231267Smm CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 4274231267Smm &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum), 4275231267Smm sysctl_jail_default_level, "I", 4276231267Smm "Ruleset for the devfs filesystem in jail"); 4277231267Smm 4278192895Sjamie/* 4279192895Sjamie * Nodes to describe jail parameters. Maximum length of string parameters 4280192895Sjamie * is returned in the string itself, and the other parameters exist merely 4281192895Sjamie * to make themselves and their types known. 4282192895Sjamie */ 4283192895SjamieSYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0, 4284192895Sjamie "Jail parameters"); 4285192895Sjamie 4286192895Sjamieint 4287192895Sjamiesysctl_jail_param(SYSCTL_HANDLER_ARGS) 4288192895Sjamie{ 4289192895Sjamie int i; 4290192895Sjamie long l; 4291192895Sjamie size_t s; 4292192895Sjamie char numbuf[12]; 4293192895Sjamie 4294192895Sjamie switch (oidp->oid_kind & CTLTYPE) 4295192895Sjamie { 4296192895Sjamie case CTLTYPE_LONG: 4297192895Sjamie case CTLTYPE_ULONG: 4298192895Sjamie l = 0; 4299192895Sjamie#ifdef SCTL_MASK32 4300192895Sjamie if (!(req->flags & SCTL_MASK32)) 4301192895Sjamie#endif 4302192895Sjamie return (SYSCTL_OUT(req, &l, sizeof(l))); 4303192895Sjamie case CTLTYPE_INT: 4304192895Sjamie case CTLTYPE_UINT: 4305192895Sjamie i = 0; 4306192895Sjamie return (SYSCTL_OUT(req, &i, sizeof(i))); 4307192895Sjamie case CTLTYPE_STRING: 4308219819Sjeff snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2); 4309192895Sjamie return 4310192895Sjamie (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req)); 4311192895Sjamie case CTLTYPE_STRUCT: 4312192895Sjamie s = (size_t)arg2; 4313192895Sjamie return (SYSCTL_OUT(req, &s, sizeof(s))); 4314192895Sjamie } 4315192895Sjamie return (0); 4316192895Sjamie} 4317192895Sjamie 4318192895SjamieSYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID"); 4319192895SjamieSYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID"); 4320192895SjamieSYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name"); 4321192895SjamieSYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path"); 4322192895SjamieSYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW, 4323192895Sjamie "I", "Jail secure level"); 4324192895SjamieSYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW, 4325192895Sjamie "I", "Jail cannot see all mounted file systems"); 4326231267SmmSYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW, 4327231267Smm "I", "Ruleset for in-jail devfs mounts"); 4328192895SjamieSYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW, 4329192895Sjamie "B", "Jail persistence"); 4330194251Sjamie#ifdef VIMAGE 4331194251SjamieSYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN, 4332195870Sjamie "E,jailsys", "Virtual network stack"); 4333194251Sjamie#endif 4334192895SjamieSYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD, 4335192895Sjamie "B", "Jail is in the process of shutting down"); 4336192895Sjamie 4337194762SjamieSYSCTL_JAIL_PARAM_NODE(children, "Number of child jails"); 4338194762SjamieSYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD, 4339194762Sjamie "I", "Current number of child jails"); 4340194762SjamieSYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW, 4341194762Sjamie "I", "Maximum number of child jails"); 4342194762Sjamie 4343195870SjamieSYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info"); 4344192895SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN, 4345192895Sjamie "Jail hostname"); 4346193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN, 4347193066Sjamie "Jail NIS domainname"); 4348193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN, 4349193066Sjamie "Jail host UUID"); 4350193066SjamieSYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW, 4351193066Sjamie "LU", "Jail host ID"); 4352192895Sjamie 4353192895SjamieSYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset"); 4354192895SjamieSYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID"); 4355192895Sjamie 4356192895Sjamie#ifdef INET 4357195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN, 4358195974Sjamie "Jail IPv4 address virtualization"); 4359192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr), 4360192895Sjamie "S,in_addr,a", "Jail IPv4 addresses"); 4361202468SbzSYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4362202468Sbz "B", "Do (not) use IPv4 source address selection rather than the " 4363202468Sbz "primary jail IPv4 address."); 4364192895Sjamie#endif 4365192895Sjamie#ifdef INET6 4366195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN, 4367195974Sjamie "Jail IPv6 address virtualization"); 4368192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr), 4369192895Sjamie "S,in6_addr,a", "Jail IPv6 addresses"); 4370202468SbzSYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4371202468Sbz "B", "Do (not) use IPv6 source address selection rather than the " 4372202468Sbz "primary jail IPv6 address."); 4373192895Sjamie#endif 4374192895Sjamie 4375192895SjamieSYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags"); 4376192895SjamieSYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW, 4377192895Sjamie "B", "Jail may set hostname"); 4378192895SjamieSYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW, 4379192895Sjamie "B", "Jail may use SYSV IPC"); 4380192895SjamieSYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW, 4381192895Sjamie "B", "Jail may create raw sockets"); 4382192895SjamieSYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW, 4383192895Sjamie "B", "Jail may alter system file flags"); 4384192895SjamieSYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW, 4385192895Sjamie "B", "Jail may set file quotas"); 4386192895SjamieSYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW, 4387192895Sjamie "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route"); 4388192895Sjamie 4389232059SmmSYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags"); 4390232059SmmSYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW, 4391232059Smm "B", "Jail may mount/unmount jail-friendly file systems in general"); 4392232059SmmSYSCTL_JAIL_PARAM(_allow_mount, devfs, CTLTYPE_INT | CTLFLAG_RW, 4393232186Smm "B", "Jail may mount the devfs file system"); 4394232059SmmSYSCTL_JAIL_PARAM(_allow_mount, nullfs, CTLTYPE_INT | CTLFLAG_RW, 4395232186Smm "B", "Jail may mount the nullfs file system"); 4396232278SmmSYSCTL_JAIL_PARAM(_allow_mount, procfs, CTLTYPE_INT | CTLFLAG_RW, 4397232278Smm "B", "Jail may mount the procfs file system"); 4398254741SdelphijSYSCTL_JAIL_PARAM(_allow_mount, tmpfs, CTLTYPE_INT | CTLFLAG_RW, 4399254741Sdelphij "B", "Jail may mount the tmpfs file system"); 4400232186SmmSYSCTL_JAIL_PARAM(_allow_mount, zfs, CTLTYPE_INT | CTLFLAG_RW, 4401232186Smm "B", "Jail may mount the zfs file system"); 4402232059Smm 4403220137Straszvoid 4404220137Straszprison_racct_foreach(void (*callback)(struct racct *racct, 4405220137Strasz void *arg2, void *arg3), void *arg2, void *arg3) 4406220137Strasz{ 4407221362Strasz struct prison_racct *prr; 4408192895Sjamie 4409220137Strasz sx_slock(&allprison_lock); 4410221362Strasz LIST_FOREACH(prr, &allprison_racct, prr_next) 4411221362Strasz (callback)(prr->prr_racct, arg2, arg3); 4412220137Strasz sx_sunlock(&allprison_lock); 4413220137Strasz} 4414220137Strasz 4415221362Straszstatic struct prison_racct * 4416221362Straszprison_racct_find_locked(const char *name) 4417221362Strasz{ 4418221362Strasz struct prison_racct *prr; 4419221362Strasz 4420221362Strasz sx_assert(&allprison_lock, SA_XLOCKED); 4421221362Strasz 4422221362Strasz if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN) 4423221362Strasz return (NULL); 4424221362Strasz 4425221362Strasz LIST_FOREACH(prr, &allprison_racct, prr_next) { 4426221362Strasz if (strcmp(name, prr->prr_name) != 0) 4427221362Strasz continue; 4428221362Strasz 4429221362Strasz /* Found prison_racct with a matching name? */ 4430221362Strasz prison_racct_hold(prr); 4431221362Strasz return (prr); 4432221362Strasz } 4433221362Strasz 4434221362Strasz /* Add new prison_racct. */ 4435221362Strasz prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK); 4436221362Strasz racct_create(&prr->prr_racct); 4437221362Strasz 4438221362Strasz strcpy(prr->prr_name, name); 4439221362Strasz refcount_init(&prr->prr_refcount, 1); 4440221362Strasz LIST_INSERT_HEAD(&allprison_racct, prr, prr_next); 4441221362Strasz 4442221362Strasz return (prr); 4443221362Strasz} 4444221362Strasz 4445221362Straszstruct prison_racct * 4446221362Straszprison_racct_find(const char *name) 4447221362Strasz{ 4448221362Strasz struct prison_racct *prr; 4449221362Strasz 4450221362Strasz sx_xlock(&allprison_lock); 4451221362Strasz prr = prison_racct_find_locked(name); 4452221362Strasz sx_xunlock(&allprison_lock); 4453221362Strasz return (prr); 4454221362Strasz} 4455221362Strasz 4456221362Straszvoid 4457221362Straszprison_racct_hold(struct prison_racct *prr) 4458221362Strasz{ 4459221362Strasz 4460221362Strasz refcount_acquire(&prr->prr_refcount); 4461221362Strasz} 4462221362Strasz 4463232598Straszstatic void 4464232598Straszprison_racct_free_locked(struct prison_racct *prr) 4465232598Strasz{ 4466232598Strasz 4467232598Strasz sx_assert(&allprison_lock, SA_XLOCKED); 4468232598Strasz 4469232598Strasz if (refcount_release(&prr->prr_refcount)) { 4470232598Strasz racct_destroy(&prr->prr_racct); 4471232598Strasz LIST_REMOVE(prr, prr_next); 4472232598Strasz free(prr, M_PRISON_RACCT); 4473232598Strasz } 4474232598Strasz} 4475232598Strasz 4476221362Straszvoid 4477221362Straszprison_racct_free(struct prison_racct *prr) 4478221362Strasz{ 4479221362Strasz int old; 4480221362Strasz 4481232598Strasz sx_assert(&allprison_lock, SA_UNLOCKED); 4482232598Strasz 4483221362Strasz old = prr->prr_refcount; 4484221362Strasz if (old > 1 && atomic_cmpset_int(&prr->prr_refcount, old, old - 1)) 4485221362Strasz return; 4486221362Strasz 4487221362Strasz sx_xlock(&allprison_lock); 4488232598Strasz prison_racct_free_locked(prr); 4489221362Strasz sx_xunlock(&allprison_lock); 4490221362Strasz} 4491221362Strasz 4492221362Strasz#ifdef RACCT 4493221362Straszstatic void 4494221362Straszprison_racct_attach(struct prison *pr) 4495221362Strasz{ 4496221362Strasz struct prison_racct *prr; 4497221362Strasz 4498232598Strasz sx_assert(&allprison_lock, SA_XLOCKED); 4499232598Strasz 4500221362Strasz prr = prison_racct_find_locked(pr->pr_name); 4501221362Strasz KASSERT(prr != NULL, ("cannot find prison_racct")); 4502221362Strasz 4503221362Strasz pr->pr_prison_racct = prr; 4504221362Strasz} 4505221362Strasz 4506232598Strasz/* 4507232598Strasz * Handle jail renaming. From the racct point of view, renaming means 4508232598Strasz * moving from one prison_racct to another. 4509232598Strasz */ 4510221362Straszstatic void 4511232598Straszprison_racct_modify(struct prison *pr) 4512232598Strasz{ 4513232598Strasz struct proc *p; 4514232598Strasz struct ucred *cred; 4515232598Strasz struct prison_racct *oldprr; 4516232598Strasz 4517232598Strasz sx_slock(&allproc_lock); 4518232598Strasz sx_xlock(&allprison_lock); 4519232598Strasz 4520235795Strasz if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) { 4521235795Strasz sx_xunlock(&allprison_lock); 4522235795Strasz sx_sunlock(&allproc_lock); 4523232598Strasz return; 4524235795Strasz } 4525232598Strasz 4526232598Strasz oldprr = pr->pr_prison_racct; 4527232598Strasz pr->pr_prison_racct = NULL; 4528232598Strasz 4529232598Strasz prison_racct_attach(pr); 4530232598Strasz 4531232598Strasz /* 4532232598Strasz * Move resource utilisation records. 4533232598Strasz */ 4534232598Strasz racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct); 4535232598Strasz 4536232598Strasz /* 4537232598Strasz * Force rctl to reattach rules to processes. 4538232598Strasz */ 4539232598Strasz FOREACH_PROC_IN_SYSTEM(p) { 4540232598Strasz PROC_LOCK(p); 4541232598Strasz cred = crhold(p->p_ucred); 4542232598Strasz PROC_UNLOCK(p); 4543232598Strasz racct_proc_ucred_changed(p, cred, cred); 4544232598Strasz crfree(cred); 4545232598Strasz } 4546232598Strasz 4547232598Strasz sx_sunlock(&allproc_lock); 4548232598Strasz prison_racct_free_locked(oldprr); 4549232598Strasz sx_xunlock(&allprison_lock); 4550232598Strasz} 4551232598Strasz 4552232598Straszstatic void 4553221362Straszprison_racct_detach(struct prison *pr) 4554221362Strasz{ 4555232598Strasz 4556232598Strasz sx_assert(&allprison_lock, SA_UNLOCKED); 4557232598Strasz 4558244404Smjg if (pr->pr_prison_racct == NULL) 4559244404Smjg return; 4560221362Strasz prison_racct_free(pr->pr_prison_racct); 4561221362Strasz pr->pr_prison_racct = NULL; 4562221362Strasz} 4563221362Strasz#endif /* RACCT */ 4564221362Strasz 4565185435Sbz#ifdef DDB 4566191673Sjamie 4567191673Sjamiestatic void 4568191673Sjamiedb_show_prison(struct prison *pr) 4569185435Sbz{ 4570192895Sjamie int fi; 4571191673Sjamie#if defined(INET) || defined(INET6) 4572191673Sjamie int ii; 4573185435Sbz#endif 4574195870Sjamie unsigned jsf; 4575185435Sbz#ifdef INET6 4576185435Sbz char ip6buf[INET6_ADDRSTRLEN]; 4577185435Sbz#endif 4578185435Sbz 4579191673Sjamie db_printf("prison %p:\n", pr); 4580191673Sjamie db_printf(" jid = %d\n", pr->pr_id); 4581191673Sjamie db_printf(" name = %s\n", pr->pr_name); 4582192895Sjamie db_printf(" parent = %p\n", pr->pr_parent); 4583191673Sjamie db_printf(" ref = %d\n", pr->pr_ref); 4584191673Sjamie db_printf(" uref = %d\n", pr->pr_uref); 4585191673Sjamie db_printf(" path = %s\n", pr->pr_path); 4586191673Sjamie db_printf(" cpuset = %d\n", pr->pr_cpuset 4587191673Sjamie ? pr->pr_cpuset->cs_id : -1); 4588194251Sjamie#ifdef VIMAGE 4589194251Sjamie db_printf(" vnet = %p\n", pr->pr_vnet); 4590194251Sjamie#endif 4591191673Sjamie db_printf(" root = %p\n", pr->pr_root); 4592191673Sjamie db_printf(" securelevel = %d\n", pr->pr_securelevel); 4593231267Smm db_printf(" devfs_rsnum = %d\n", pr->pr_devfs_rsnum); 4594202123Sbz db_printf(" children.max = %d\n", pr->pr_childmax); 4595202123Sbz db_printf(" children.cur = %d\n", pr->pr_childcount); 4596192895Sjamie db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children)); 4597192895Sjamie db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling)); 4598202123Sbz db_printf(" flags = 0x%x", pr->pr_flags); 4599192895Sjamie for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 4600192895Sjamie fi++) 4601192895Sjamie if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi))) 4602192895Sjamie db_printf(" %s", pr_flag_names[fi]); 4603195870Sjamie for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 4604195870Sjamie fi++) { 4605195870Sjamie jsf = pr->pr_flags & 4606195870Sjamie (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new); 4607195870Sjamie db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name, 4608195870Sjamie pr_flag_jailsys[fi].disable && 4609195870Sjamie (jsf == pr_flag_jailsys[fi].disable) ? "disable" 4610195870Sjamie : (jsf == pr_flag_jailsys[fi].new) ? "new" 4611195870Sjamie : "inherit"); 4612195870Sjamie } 4613202123Sbz db_printf(" allow = 0x%x", pr->pr_allow); 4614192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 4615192895Sjamie fi++) 4616192895Sjamie if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi))) 4617192895Sjamie db_printf(" %s", pr_allow_names[fi]); 4618191673Sjamie db_printf("\n"); 4619192895Sjamie db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs); 4620194118Sjamie db_printf(" host.hostname = %s\n", pr->pr_hostname); 4621194118Sjamie db_printf(" host.domainname = %s\n", pr->pr_domainname); 4622194118Sjamie db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid); 4623193066Sjamie db_printf(" host.hostid = %lu\n", pr->pr_hostid); 4624185435Sbz#ifdef INET 4625191673Sjamie db_printf(" ip4s = %d\n", pr->pr_ip4s); 4626191673Sjamie for (ii = 0; ii < pr->pr_ip4s; ii++) 4627191673Sjamie db_printf(" %s %s\n", 4628202123Sbz ii == 0 ? "ip4.addr =" : " ", 4629191673Sjamie inet_ntoa(pr->pr_ip4[ii])); 4630185435Sbz#endif 4631185435Sbz#ifdef INET6 4632191673Sjamie db_printf(" ip6s = %d\n", pr->pr_ip6s); 4633191673Sjamie for (ii = 0; ii < pr->pr_ip6s; ii++) 4634191673Sjamie db_printf(" %s %s\n", 4635202123Sbz ii == 0 ? "ip6.addr =" : " ", 4636191673Sjamie ip6_sprintf(ip6buf, &pr->pr_ip6[ii])); 4637191673Sjamie#endif 4638191673Sjamie} 4639191673Sjamie 4640191673SjamieDB_SHOW_COMMAND(prison, db_show_prison_command) 4641191673Sjamie{ 4642191673Sjamie struct prison *pr; 4643191673Sjamie 4644191673Sjamie if (!have_addr) { 4645192895Sjamie /* 4646192895Sjamie * Show all prisons in the list, and prison0 which is not 4647192895Sjamie * listed. 4648192895Sjamie */ 4649192895Sjamie db_show_prison(&prison0); 4650192895Sjamie if (!db_pager_quit) { 4651192895Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) { 4652192895Sjamie db_show_prison(pr); 4653192895Sjamie if (db_pager_quit) 4654192895Sjamie break; 4655192895Sjamie } 4656191673Sjamie } 4657191673Sjamie return; 4658191673Sjamie } 4659191673Sjamie 4660192895Sjamie if (addr == 0) 4661192895Sjamie pr = &prison0; 4662192895Sjamie else { 4663192895Sjamie /* Look for a prison with the ID and with references. */ 4664191673Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) 4665192895Sjamie if (pr->pr_id == addr && pr->pr_ref > 0) 4666191673Sjamie break; 4667192895Sjamie if (pr == NULL) 4668192895Sjamie /* Look again, without requiring a reference. */ 4669192895Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) 4670192895Sjamie if (pr->pr_id == addr) 4671192895Sjamie break; 4672192895Sjamie if (pr == NULL) 4673192895Sjamie /* Assume address points to a valid prison. */ 4674192895Sjamie pr = (struct prison *)addr; 4675192895Sjamie } 4676191673Sjamie db_show_prison(pr); 4677185435Sbz} 4678191673Sjamie 4679185435Sbz#endif /* DDB */ 4680