kern_jail.c revision 241896
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: head/sys/kern/kern_jail.c 241896 2012-10-22 17:50:54Z kib $"); 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", 209192895Sjamie}; 210216861Sbzconst size_t pr_allow_names_size = sizeof(pr_allow_names); 211192895Sjamie 212192895Sjamiestatic char *pr_allow_nonames[] = { 213192895Sjamie "allow.noset_hostname", 214192895Sjamie "allow.nosysvipc", 215192895Sjamie "allow.noraw_sockets", 216192895Sjamie "allow.nochflags", 217192895Sjamie "allow.nomount", 218192895Sjamie "allow.noquotas", 219192895Sjamie "allow.nosocket_af", 220232059Smm "allow.mount.nodevfs", 221232059Smm "allow.mount.nonullfs", 222232186Smm "allow.mount.nozfs", 223232278Smm "allow.mount.noprocfs", 224192895Sjamie}; 225216861Sbzconst size_t pr_allow_nonames_size = sizeof(pr_allow_nonames); 226192895Sjamie 227196002Sjamie#define JAIL_DEFAULT_ALLOW PR_ALLOW_SET_HOSTNAME 228196002Sjamie#define JAIL_DEFAULT_ENFORCE_STATFS 2 229232059Smm#define JAIL_DEFAULT_DEVFS_RSNUM 0 230192895Sjamiestatic unsigned jail_default_allow = JAIL_DEFAULT_ALLOW; 231196002Sjamiestatic int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 232231267Smmstatic int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM; 233192895Sjamie#if defined(INET) || defined(INET6) 234193865Sjamiestatic unsigned jail_max_af_ips = 255; 235192895Sjamie#endif 236192895Sjamie 237192895Sjamie#ifdef INET 238185435Sbzstatic int 239185435Sbzqcmp_v4(const void *ip1, const void *ip2) 240185435Sbz{ 241185435Sbz in_addr_t iaa, iab; 242185435Sbz 243185435Sbz /* 244185435Sbz * We need to compare in HBO here to get the list sorted as expected 245185435Sbz * by the result of the code. Sorting NBO addresses gives you 246185435Sbz * interesting results. If you do not understand, do not try. 247185435Sbz */ 248185435Sbz iaa = ntohl(((const struct in_addr *)ip1)->s_addr); 249185435Sbz iab = ntohl(((const struct in_addr *)ip2)->s_addr); 250185435Sbz 251185435Sbz /* 252185435Sbz * Do not simply return the difference of the two numbers, the int is 253185435Sbz * not wide enough. 254185435Sbz */ 255185435Sbz if (iaa > iab) 256185435Sbz return (1); 257185435Sbz else if (iaa < iab) 258185435Sbz return (-1); 259185435Sbz else 260185435Sbz return (0); 261185435Sbz} 262185435Sbz#endif 263185435Sbz 264185435Sbz#ifdef INET6 265185435Sbzstatic int 266185435Sbzqcmp_v6(const void *ip1, const void *ip2) 267185435Sbz{ 268185435Sbz const struct in6_addr *ia6a, *ia6b; 269185435Sbz int i, rc; 270185435Sbz 271185435Sbz ia6a = (const struct in6_addr *)ip1; 272185435Sbz ia6b = (const struct in6_addr *)ip2; 273185435Sbz 274185435Sbz rc = 0; 275190466Sjamie for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) { 276185435Sbz if (ia6a->s6_addr[i] > ia6b->s6_addr[i]) 277185435Sbz rc = 1; 278185435Sbz else if (ia6a->s6_addr[i] < ia6b->s6_addr[i]) 279185435Sbz rc = -1; 280185435Sbz } 281185435Sbz return (rc); 282185435Sbz} 283185435Sbz#endif 284185435Sbz 285191673Sjamie/* 286191673Sjamie * struct jail_args { 287191673Sjamie * struct jail *jail; 288191673Sjamie * }; 289191673Sjamie */ 290191673Sjamieint 291225617Skmacysys_jail(struct thread *td, struct jail_args *uap) 292185435Sbz{ 293191673Sjamie uint32_t version; 294191673Sjamie int error; 295192895Sjamie struct jail j; 296185435Sbz 297191673Sjamie error = copyin(uap->jail, &version, sizeof(uint32_t)); 298191673Sjamie if (error) 299191673Sjamie return (error); 300185435Sbz 301191673Sjamie switch (version) { 302191673Sjamie case 0: 303191673Sjamie { 304191673Sjamie struct jail_v0 j0; 305185435Sbz 306192895Sjamie /* FreeBSD single IPv4 jails. */ 307192895Sjamie bzero(&j, sizeof(struct jail)); 308191673Sjamie error = copyin(uap->jail, &j0, sizeof(struct jail_v0)); 309191673Sjamie if (error) 310191673Sjamie return (error); 311192895Sjamie j.version = j0.version; 312192895Sjamie j.path = j0.path; 313192895Sjamie j.hostname = j0.hostname; 314192895Sjamie j.ip4s = j0.ip_number; 315191673Sjamie break; 316191673Sjamie } 317191673Sjamie 318191673Sjamie case 1: 319185435Sbz /* 320191673Sjamie * Version 1 was used by multi-IPv4 jail implementations 321191673Sjamie * that never made it into the official kernel. 322185435Sbz */ 323191673Sjamie return (EINVAL); 324185435Sbz 325191673Sjamie case 2: /* JAIL_API_VERSION */ 326191673Sjamie /* FreeBSD multi-IPv4/IPv6,noIP jails. */ 327191673Sjamie error = copyin(uap->jail, &j, sizeof(struct jail)); 328191673Sjamie if (error) 329191673Sjamie return (error); 330192895Sjamie break; 331192895Sjamie 332192895Sjamie default: 333192895Sjamie /* Sci-Fi jails are not supported, sorry. */ 334192895Sjamie return (EINVAL); 335192895Sjamie } 336192895Sjamie return (kern_jail(td, &j)); 337192895Sjamie} 338192895Sjamie 339192895Sjamieint 340192895Sjamiekern_jail(struct thread *td, struct jail *j) 341192895Sjamie{ 342193865Sjamie struct iovec optiov[2 * (4 343193865Sjamie + sizeof(pr_allow_names) / sizeof(pr_allow_names[0]) 344193865Sjamie#ifdef INET 345193865Sjamie + 1 346193865Sjamie#endif 347193865Sjamie#ifdef INET6 348193865Sjamie + 1 349193865Sjamie#endif 350193865Sjamie )]; 351192895Sjamie struct uio opt; 352192895Sjamie char *u_path, *u_hostname, *u_name; 353185435Sbz#ifdef INET 354193865Sjamie uint32_t ip4s; 355192895Sjamie struct in_addr *u_ip4; 356192895Sjamie#endif 357192895Sjamie#ifdef INET6 358192895Sjamie struct in6_addr *u_ip6; 359192895Sjamie#endif 360192895Sjamie size_t tmplen; 361192895Sjamie int error, enforce_statfs, fi; 362192895Sjamie 363192895Sjamie bzero(&optiov, sizeof(optiov)); 364192895Sjamie opt.uio_iov = optiov; 365192895Sjamie opt.uio_iovcnt = 0; 366192895Sjamie opt.uio_offset = -1; 367192895Sjamie opt.uio_resid = -1; 368192895Sjamie opt.uio_segflg = UIO_SYSSPACE; 369192895Sjamie opt.uio_rw = UIO_READ; 370192895Sjamie opt.uio_td = td; 371192895Sjamie 372192895Sjamie /* Set permissions for top-level jails from sysctls. */ 373192895Sjamie if (!jailed(td->td_ucred)) { 374192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / 375192895Sjamie sizeof(pr_allow_names[0]); fi++) { 376192895Sjamie optiov[opt.uio_iovcnt].iov_base = 377192895Sjamie (jail_default_allow & (1 << fi)) 378192895Sjamie ? pr_allow_names[fi] : pr_allow_nonames[fi]; 379192895Sjamie optiov[opt.uio_iovcnt].iov_len = 380192895Sjamie strlen(optiov[opt.uio_iovcnt].iov_base) + 1; 381192895Sjamie opt.uio_iovcnt += 2; 382192895Sjamie } 383192895Sjamie optiov[opt.uio_iovcnt].iov_base = "enforce_statfs"; 384192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs"); 385192895Sjamie opt.uio_iovcnt++; 386192895Sjamie enforce_statfs = jail_default_enforce_statfs; 387192895Sjamie optiov[opt.uio_iovcnt].iov_base = &enforce_statfs; 388192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs); 389192895Sjamie opt.uio_iovcnt++; 390192895Sjamie } 391192895Sjamie 392192895Sjamie tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN; 393192895Sjamie#ifdef INET 394192895Sjamie ip4s = (j->version == 0) ? 1 : j->ip4s; 395192895Sjamie if (ip4s > jail_max_af_ips) 396192895Sjamie return (EINVAL); 397192895Sjamie tmplen += ip4s * sizeof(struct in_addr); 398191673Sjamie#else 399192895Sjamie if (j->ip4s > 0) 400192895Sjamie return (EINVAL); 401191673Sjamie#endif 402191673Sjamie#ifdef INET6 403192895Sjamie if (j->ip6s > jail_max_af_ips) 404192895Sjamie return (EINVAL); 405192895Sjamie tmplen += j->ip6s * sizeof(struct in6_addr); 406191673Sjamie#else 407192895Sjamie if (j->ip6s > 0) 408192895Sjamie return (EINVAL); 409191673Sjamie#endif 410192895Sjamie u_path = malloc(tmplen, M_TEMP, M_WAITOK); 411192895Sjamie u_hostname = u_path + MAXPATHLEN; 412192895Sjamie u_name = u_hostname + MAXHOSTNAMELEN; 413191673Sjamie#ifdef INET 414192895Sjamie u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN); 415191673Sjamie#endif 416191673Sjamie#ifdef INET6 417191673Sjamie#ifdef INET 418192895Sjamie u_ip6 = (struct in6_addr *)(u_ip4 + ip4s); 419191673Sjamie#else 420192895Sjamie u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN); 421191673Sjamie#endif 422191673Sjamie#endif 423192895Sjamie optiov[opt.uio_iovcnt].iov_base = "path"; 424192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("path"); 425192895Sjamie opt.uio_iovcnt++; 426192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_path; 427192895Sjamie error = copyinstr(j->path, u_path, MAXPATHLEN, 428192895Sjamie &optiov[opt.uio_iovcnt].iov_len); 429192895Sjamie if (error) { 430192895Sjamie free(u_path, M_TEMP); 431192895Sjamie return (error); 432192895Sjamie } 433192895Sjamie opt.uio_iovcnt++; 434192895Sjamie optiov[opt.uio_iovcnt].iov_base = "host.hostname"; 435192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname"); 436192895Sjamie opt.uio_iovcnt++; 437192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_hostname; 438192895Sjamie error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN, 439192895Sjamie &optiov[opt.uio_iovcnt].iov_len); 440192895Sjamie if (error) { 441192895Sjamie free(u_path, M_TEMP); 442192895Sjamie return (error); 443192895Sjamie } 444192895Sjamie opt.uio_iovcnt++; 445192895Sjamie if (j->jailname != NULL) { 446192895Sjamie optiov[opt.uio_iovcnt].iov_base = "name"; 447192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("name"); 448192895Sjamie opt.uio_iovcnt++; 449192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_name; 450192895Sjamie error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN, 451192895Sjamie &optiov[opt.uio_iovcnt].iov_len); 452191673Sjamie if (error) { 453191673Sjamie free(u_path, M_TEMP); 454191673Sjamie return (error); 455191673Sjamie } 456192895Sjamie opt.uio_iovcnt++; 457192895Sjamie } 458191673Sjamie#ifdef INET 459192895Sjamie optiov[opt.uio_iovcnt].iov_base = "ip4.addr"; 460192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr"); 461192895Sjamie opt.uio_iovcnt++; 462192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_ip4; 463192895Sjamie optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr); 464192895Sjamie if (j->version == 0) 465192895Sjamie u_ip4->s_addr = j->ip4s; 466192895Sjamie else { 467192895Sjamie error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len); 468191673Sjamie if (error) { 469191673Sjamie free(u_path, M_TEMP); 470191673Sjamie return (error); 471191673Sjamie } 472192895Sjamie } 473192895Sjamie opt.uio_iovcnt++; 474185435Sbz#endif 475185435Sbz#ifdef INET6 476192895Sjamie optiov[opt.uio_iovcnt].iov_base = "ip6.addr"; 477192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr"); 478192895Sjamie opt.uio_iovcnt++; 479192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_ip6; 480192895Sjamie optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr); 481192895Sjamie error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len); 482192895Sjamie if (error) { 483192895Sjamie free(u_path, M_TEMP); 484192895Sjamie return (error); 485192895Sjamie } 486192895Sjamie opt.uio_iovcnt++; 487185435Sbz#endif 488192895Sjamie KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]), 489192895Sjamie ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt)); 490191673Sjamie error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH); 491191673Sjamie free(u_path, M_TEMP); 492191673Sjamie return (error); 493185435Sbz} 494185435Sbz 495192895Sjamie 496191673Sjamie/* 497191673Sjamie * struct jail_set_args { 498191673Sjamie * struct iovec *iovp; 499191673Sjamie * unsigned int iovcnt; 500191673Sjamie * int flags; 501191673Sjamie * }; 502191673Sjamie */ 503191673Sjamieint 504225617Skmacysys_jail_set(struct thread *td, struct jail_set_args *uap) 505185435Sbz{ 506191673Sjamie struct uio *auio; 507191673Sjamie int error; 508191673Sjamie 509191673Sjamie /* Check that we have an even number of iovecs. */ 510191673Sjamie if (uap->iovcnt & 1) 511191673Sjamie return (EINVAL); 512191673Sjamie 513191673Sjamie error = copyinuio(uap->iovp, uap->iovcnt, &auio); 514191673Sjamie if (error) 515191673Sjamie return (error); 516191673Sjamie error = kern_jail_set(td, auio, uap->flags); 517191673Sjamie free(auio, M_IOV); 518191673Sjamie return (error); 519191673Sjamie} 520191673Sjamie 521191673Sjamieint 522191673Sjamiekern_jail_set(struct thread *td, struct uio *optuio, int flags) 523191673Sjamie{ 524191673Sjamie struct nameidata nd; 525185435Sbz#ifdef INET 526190466Sjamie struct in_addr *ip4; 527185435Sbz#endif 528185435Sbz#ifdef INET6 529185435Sbz struct in6_addr *ip6; 530185435Sbz#endif 531191673Sjamie struct vfsopt *opt; 532191673Sjamie struct vfsoptlist *opts; 533196135Sbz struct prison *pr, *deadpr, *mypr, *ppr, *tpr; 534191673Sjamie struct vnode *root; 535196835Sjamie char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid; 536230407Smm char *g_path; 537192895Sjamie#if defined(INET) || defined(INET6) 538196135Sbz struct prison *tppr; 539191673Sjamie void *op; 540192895Sjamie#endif 541193066Sjamie unsigned long hid; 542192895Sjamie size_t namelen, onamelen; 543192895Sjamie int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos; 544231267Smm int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel; 545195870Sjamie int fi, jid, jsys, len, level; 546241896Skib int childmax, rsnum, slevel; 547230129Smm int fullpath_disabled; 548191673Sjamie#if defined(INET) || defined(INET6) 549192895Sjamie int ii, ij; 550191673Sjamie#endif 551191673Sjamie#ifdef INET 552195974Sjamie int ip4s, redo_ip4; 553191673Sjamie#endif 554191673Sjamie#ifdef INET6 555195974Sjamie int ip6s, redo_ip6; 556191673Sjamie#endif 557224290Smckusick uint64_t pr_allow, ch_allow, pr_flags, ch_flags; 558224290Smckusick unsigned tallow; 559191673Sjamie char numbuf[12]; 560185435Sbz 561191673Sjamie error = priv_check(td, PRIV_JAIL_SET); 562191673Sjamie if (!error && (flags & JAIL_ATTACH)) 563191673Sjamie error = priv_check(td, PRIV_JAIL_ATTACH); 564191673Sjamie if (error) 565191673Sjamie return (error); 566192895Sjamie mypr = ppr = td->td_ucred->cr_prison; 567194762Sjamie if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0) 568192895Sjamie return (EPERM); 569191673Sjamie if (flags & ~JAIL_SET_MASK) 570191673Sjamie return (EINVAL); 571191673Sjamie 572185435Sbz /* 573191673Sjamie * Check all the parameters before committing to anything. Not all 574191673Sjamie * errors can be caught early, but we may as well try. Also, this 575191673Sjamie * takes care of some expensive stuff (path lookup) before getting 576191673Sjamie * the allprison lock. 577185435Sbz * 578191673Sjamie * XXX Jails are not filesystems, and jail parameters are not mount 579191673Sjamie * options. But it makes more sense to re-use the vfsopt code 580191673Sjamie * than duplicate it under a different name. 581185435Sbz */ 582191673Sjamie error = vfs_buildopts(optuio, &opts); 583191673Sjamie if (error) 584191673Sjamie return (error); 585185435Sbz#ifdef INET 586185435Sbz ip4 = NULL; 587185435Sbz#endif 588185435Sbz#ifdef INET6 589185435Sbz ip6 = NULL; 590185435Sbz#endif 591230407Smm g_path = NULL; 592191673Sjamie 593191673Sjamie error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 594191673Sjamie if (error == ENOENT) 595191673Sjamie jid = 0; 596191673Sjamie else if (error != 0) 597191673Sjamie goto done_free; 598191673Sjamie 599191673Sjamie error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel)); 600191673Sjamie if (error == ENOENT) 601191673Sjamie gotslevel = 0; 602191673Sjamie else if (error != 0) 603191673Sjamie goto done_free; 604191673Sjamie else 605191673Sjamie gotslevel = 1; 606191673Sjamie 607194762Sjamie error = 608194762Sjamie vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax)); 609194762Sjamie if (error == ENOENT) 610194762Sjamie gotchildmax = 0; 611194762Sjamie else if (error != 0) 612194762Sjamie goto done_free; 613194762Sjamie else 614194762Sjamie gotchildmax = 1; 615194762Sjamie 616192895Sjamie error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce)); 617212436Sjamie if (error == ENOENT) 618212436Sjamie gotenforce = 0; 619212436Sjamie else if (error != 0) 620192895Sjamie goto done_free; 621212436Sjamie else if (enforce < 0 || enforce > 2) { 622212436Sjamie error = EINVAL; 623212436Sjamie goto done_free; 624212436Sjamie } else 625212436Sjamie gotenforce = 1; 626192895Sjamie 627231267Smm error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum)); 628231267Smm if (error == ENOENT) 629231267Smm gotrsnum = 0; 630231267Smm else if (error != 0) 631231267Smm goto done_free; 632231267Smm else 633231267Smm gotrsnum = 1; 634231267Smm 635191673Sjamie pr_flags = ch_flags = 0; 636192895Sjamie for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 637192895Sjamie fi++) { 638192895Sjamie if (pr_flag_names[fi] == NULL) 639192895Sjamie continue; 640192895Sjamie vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi); 641192895Sjamie vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi); 642192895Sjamie } 643191673Sjamie ch_flags |= pr_flags; 644195870Sjamie for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 645195870Sjamie fi++) { 646195870Sjamie error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys, 647195870Sjamie sizeof(jsys)); 648195870Sjamie if (error == ENOENT) 649195870Sjamie continue; 650195870Sjamie if (error != 0) 651195870Sjamie goto done_free; 652195870Sjamie switch (jsys) { 653195870Sjamie case JAIL_SYS_DISABLE: 654195870Sjamie if (!pr_flag_jailsys[fi].disable) { 655195870Sjamie error = EINVAL; 656195870Sjamie goto done_free; 657195870Sjamie } 658195870Sjamie pr_flags |= pr_flag_jailsys[fi].disable; 659195870Sjamie break; 660195870Sjamie case JAIL_SYS_NEW: 661195870Sjamie pr_flags |= pr_flag_jailsys[fi].new; 662195870Sjamie break; 663195870Sjamie case JAIL_SYS_INHERIT: 664195870Sjamie break; 665195870Sjamie default: 666195870Sjamie error = EINVAL; 667195870Sjamie goto done_free; 668195870Sjamie } 669195870Sjamie ch_flags |= 670195870Sjamie pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable; 671195870Sjamie } 672211085Sjamie if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE 673211085Sjamie && !(pr_flags & PR_PERSIST)) { 674211085Sjamie error = EINVAL; 675211085Sjamie vfs_opterror(opts, "new jail must persist or attach"); 676211085Sjamie goto done_errmsg; 677211085Sjamie } 678194251Sjamie#ifdef VIMAGE 679194251Sjamie if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) { 680194251Sjamie error = EINVAL; 681194251Sjamie vfs_opterror(opts, "vnet cannot be changed after creation"); 682194251Sjamie goto done_errmsg; 683194251Sjamie } 684194251Sjamie#endif 685195974Sjamie#ifdef INET 686195974Sjamie if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) { 687195974Sjamie error = EINVAL; 688195974Sjamie vfs_opterror(opts, "ip4 cannot be changed after creation"); 689195974Sjamie goto done_errmsg; 690195974Sjamie } 691195974Sjamie#endif 692195974Sjamie#ifdef INET6 693195974Sjamie if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) { 694195974Sjamie error = EINVAL; 695195974Sjamie vfs_opterror(opts, "ip6 cannot be changed after creation"); 696195974Sjamie goto done_errmsg; 697195974Sjamie } 698195974Sjamie#endif 699191673Sjamie 700192895Sjamie pr_allow = ch_allow = 0; 701192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 702192895Sjamie fi++) { 703192895Sjamie vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi); 704192895Sjamie vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi); 705192895Sjamie } 706192895Sjamie ch_allow |= pr_allow; 707192895Sjamie 708191673Sjamie error = vfs_getopt(opts, "name", (void **)&name, &len); 709191673Sjamie if (error == ENOENT) 710191673Sjamie name = NULL; 711191673Sjamie else if (error != 0) 712191673Sjamie goto done_free; 713191673Sjamie else { 714191673Sjamie if (len == 0 || name[len - 1] != '\0') { 715191673Sjamie error = EINVAL; 716191673Sjamie goto done_free; 717191673Sjamie } 718191673Sjamie if (len > MAXHOSTNAMELEN) { 719191673Sjamie error = ENAMETOOLONG; 720191673Sjamie goto done_free; 721191673Sjamie } 722191673Sjamie } 723191673Sjamie 724191673Sjamie error = vfs_getopt(opts, "host.hostname", (void **)&host, &len); 725191673Sjamie if (error == ENOENT) 726191673Sjamie host = NULL; 727191673Sjamie else if (error != 0) 728191673Sjamie goto done_free; 729191673Sjamie else { 730193066Sjamie ch_flags |= PR_HOST; 731193066Sjamie pr_flags |= PR_HOST; 732191673Sjamie if (len == 0 || host[len - 1] != '\0') { 733191673Sjamie error = EINVAL; 734191673Sjamie goto done_free; 735191673Sjamie } 736191673Sjamie if (len > MAXHOSTNAMELEN) { 737191673Sjamie error = ENAMETOOLONG; 738191673Sjamie goto done_free; 739191673Sjamie } 740191673Sjamie } 741191673Sjamie 742193066Sjamie error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len); 743193066Sjamie if (error == ENOENT) 744193066Sjamie domain = NULL; 745193066Sjamie else if (error != 0) 746193066Sjamie goto done_free; 747193066Sjamie else { 748193066Sjamie ch_flags |= PR_HOST; 749193066Sjamie pr_flags |= PR_HOST; 750193066Sjamie if (len == 0 || domain[len - 1] != '\0') { 751193066Sjamie error = EINVAL; 752193066Sjamie goto done_free; 753193066Sjamie } 754193066Sjamie if (len > MAXHOSTNAMELEN) { 755193066Sjamie error = ENAMETOOLONG; 756193066Sjamie goto done_free; 757193066Sjamie } 758193066Sjamie } 759193066Sjamie 760193066Sjamie error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len); 761193066Sjamie if (error == ENOENT) 762193066Sjamie uuid = NULL; 763193066Sjamie else if (error != 0) 764193066Sjamie goto done_free; 765193066Sjamie else { 766193066Sjamie ch_flags |= PR_HOST; 767193066Sjamie pr_flags |= PR_HOST; 768193066Sjamie if (len == 0 || uuid[len - 1] != '\0') { 769193066Sjamie error = EINVAL; 770193066Sjamie goto done_free; 771193066Sjamie } 772193066Sjamie if (len > HOSTUUIDLEN) { 773193066Sjamie error = ENAMETOOLONG; 774193066Sjamie goto done_free; 775193066Sjamie } 776193066Sjamie } 777193066Sjamie 778205014Snwhitehorn#ifdef COMPAT_FREEBSD32 779217896Sdchagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 780193066Sjamie uint32_t hid32; 781193066Sjamie 782193066Sjamie error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32)); 783193066Sjamie hid = hid32; 784193066Sjamie } else 785193066Sjamie#endif 786193066Sjamie error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid)); 787193066Sjamie if (error == ENOENT) 788193066Sjamie gothid = 0; 789193066Sjamie else if (error != 0) 790193066Sjamie goto done_free; 791193066Sjamie else { 792193066Sjamie gothid = 1; 793193066Sjamie ch_flags |= PR_HOST; 794193066Sjamie pr_flags |= PR_HOST; 795193066Sjamie } 796193066Sjamie 797185435Sbz#ifdef INET 798191673Sjamie error = vfs_getopt(opts, "ip4.addr", &op, &ip4s); 799191673Sjamie if (error == ENOENT) 800195870Sjamie ip4s = (pr_flags & PR_IP4_DISABLE) ? 0 : -1; 801191673Sjamie else if (error != 0) 802191673Sjamie goto done_free; 803191673Sjamie else if (ip4s & (sizeof(*ip4) - 1)) { 804191673Sjamie error = EINVAL; 805191673Sjamie goto done_free; 806192895Sjamie } else { 807195870Sjamie ch_flags |= PR_IP4_USER | PR_IP4_DISABLE; 808195870Sjamie if (ip4s == 0) 809195870Sjamie pr_flags |= PR_IP4_USER | PR_IP4_DISABLE; 810195870Sjamie else { 811195870Sjamie pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER; 812192895Sjamie ip4s /= sizeof(*ip4); 813192895Sjamie if (ip4s > jail_max_af_ips) { 814185435Sbz error = EINVAL; 815192895Sjamie vfs_opterror(opts, "too many IPv4 addresses"); 816192895Sjamie goto done_errmsg; 817185435Sbz } 818195974Sjamie ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 819192895Sjamie bcopy(op, ip4, ip4s * sizeof(*ip4)); 820192895Sjamie /* 821192895Sjamie * IP addresses are all sorted but ip[0] to preserve 822192895Sjamie * the primary IP address as given from userland. 823192895Sjamie * This special IP is used for unbound outgoing 824202116Sbz * connections as well for "loopback" traffic in case 825202116Sbz * source address selection cannot find any more fitting 826202116Sbz * address to connect from. 827192895Sjamie */ 828192895Sjamie if (ip4s > 1) 829192895Sjamie qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4); 830192895Sjamie /* 831192895Sjamie * Check for duplicate addresses and do some simple 832192895Sjamie * zero and broadcast checks. If users give other bogus 833192895Sjamie * addresses it is their problem. 834192895Sjamie * 835192895Sjamie * We do not have to care about byte order for these 836192895Sjamie * checks so we will do them in NBO. 837192895Sjamie */ 838192895Sjamie for (ii = 0; ii < ip4s; ii++) { 839192895Sjamie if (ip4[ii].s_addr == INADDR_ANY || 840192895Sjamie ip4[ii].s_addr == INADDR_BROADCAST) { 841192895Sjamie error = EINVAL; 842192895Sjamie goto done_free; 843192895Sjamie } 844192895Sjamie if ((ii+1) < ip4s && 845192895Sjamie (ip4[0].s_addr == ip4[ii+1].s_addr || 846192895Sjamie ip4[ii].s_addr == ip4[ii+1].s_addr)) { 847192895Sjamie error = EINVAL; 848192895Sjamie goto done_free; 849192895Sjamie } 850192895Sjamie } 851185435Sbz } 852191673Sjamie } 853191673Sjamie#endif 854185435Sbz 855185435Sbz#ifdef INET6 856191673Sjamie error = vfs_getopt(opts, "ip6.addr", &op, &ip6s); 857191673Sjamie if (error == ENOENT) 858195870Sjamie ip6s = (pr_flags & PR_IP6_DISABLE) ? 0 : -1; 859191673Sjamie else if (error != 0) 860191673Sjamie goto done_free; 861191673Sjamie else if (ip6s & (sizeof(*ip6) - 1)) { 862191673Sjamie error = EINVAL; 863191673Sjamie goto done_free; 864192895Sjamie } else { 865195870Sjamie ch_flags |= PR_IP6_USER | PR_IP6_DISABLE; 866195870Sjamie if (ip6s == 0) 867195870Sjamie pr_flags |= PR_IP6_USER | PR_IP6_DISABLE; 868195870Sjamie else { 869195870Sjamie pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER; 870192895Sjamie ip6s /= sizeof(*ip6); 871192895Sjamie if (ip6s > jail_max_af_ips) { 872185435Sbz error = EINVAL; 873192895Sjamie vfs_opterror(opts, "too many IPv6 addresses"); 874192895Sjamie goto done_errmsg; 875185435Sbz } 876195974Sjamie ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 877192895Sjamie bcopy(op, ip6, ip6s * sizeof(*ip6)); 878192895Sjamie if (ip6s > 1) 879192895Sjamie qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6); 880192895Sjamie for (ii = 0; ii < ip6s; ii++) { 881192895Sjamie if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) { 882192895Sjamie error = EINVAL; 883192895Sjamie goto done_free; 884192895Sjamie } 885192895Sjamie if ((ii+1) < ip6s && 886192895Sjamie (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) || 887192895Sjamie IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1]))) 888192895Sjamie { 889192895Sjamie error = EINVAL; 890192895Sjamie goto done_free; 891192895Sjamie } 892192895Sjamie } 893185435Sbz } 894191673Sjamie } 895185435Sbz#endif 896185435Sbz 897195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6)) 898195945Sjamie if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 899195945Sjamie error = EINVAL; 900195945Sjamie vfs_opterror(opts, 901195945Sjamie "vnet jails cannot have IP address restrictions"); 902195945Sjamie goto done_errmsg; 903195945Sjamie } 904195945Sjamie#endif 905195945Sjamie 906230143Smm fullpath_disabled = 0; 907191673Sjamie root = NULL; 908191673Sjamie error = vfs_getopt(opts, "path", (void **)&path, &len); 909191673Sjamie if (error == ENOENT) 910191673Sjamie path = NULL; 911191673Sjamie else if (error != 0) 912191673Sjamie goto done_free; 913191673Sjamie else { 914191673Sjamie if (flags & JAIL_UPDATE) { 915191673Sjamie error = EINVAL; 916191673Sjamie vfs_opterror(opts, 917191673Sjamie "path cannot be changed after creation"); 918191673Sjamie goto done_errmsg; 919191673Sjamie } 920191673Sjamie if (len == 0 || path[len - 1] != '\0') { 921191673Sjamie error = EINVAL; 922191673Sjamie goto done_free; 923191673Sjamie } 924241896Skib NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, 925230129Smm path, td); 926230129Smm error = namei(&nd); 927230129Smm if (error) 928230129Smm goto done_free; 929230129Smm root = nd.ni_vp; 930230129Smm NDFREE(&nd, NDF_ONLY_PNBUF); 931230407Smm g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 932230407Smm strlcpy(g_path, path, MAXPATHLEN); 933230407Smm error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN); 934230407Smm if (error == 0) 935230407Smm path = g_path; 936230407Smm else if (error == ENODEV) { 937230129Smm /* proceed if sysctl debug.disablefullpath == 1 */ 938230129Smm fullpath_disabled = 1; 939230129Smm if (len < 2 || (len == 2 && path[0] == '/')) 940230129Smm path = NULL; 941230407Smm } else { 942230129Smm /* exit on other errors */ 943230129Smm goto done_free; 944230129Smm } 945230129Smm if (root->v_type != VDIR) { 946230129Smm error = ENOTDIR; 947230129Smm vput(root); 948230129Smm goto done_free; 949230129Smm } 950230129Smm VOP_UNLOCK(root, 0); 951230129Smm if (fullpath_disabled) { 952192895Sjamie /* Leave room for a real-root full pathname. */ 953192895Sjamie if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/") 954192895Sjamie ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) { 955192895Sjamie error = ENAMETOOLONG; 956192895Sjamie goto done_free; 957192895Sjamie } 958191673Sjamie } 959191673Sjamie } 960185435Sbz 961191673Sjamie /* 962191673Sjamie * Grab the allprison lock before letting modules check their 963191673Sjamie * parameters. Once we have it, do not let go so we'll have a 964191673Sjamie * consistent view of the OSD list. 965191673Sjamie */ 966191673Sjamie sx_xlock(&allprison_lock); 967191673Sjamie error = osd_jail_call(NULL, PR_METHOD_CHECK, opts); 968191673Sjamie if (error) 969191673Sjamie goto done_unlock_list; 970185435Sbz 971191673Sjamie /* By now, all parameters should have been noted. */ 972191673Sjamie TAILQ_FOREACH(opt, opts, link) { 973191673Sjamie if (!opt->seen && strcmp(opt->name, "errmsg")) { 974191673Sjamie error = EINVAL; 975191673Sjamie vfs_opterror(opts, "unknown parameter: %s", opt->name); 976191673Sjamie goto done_unlock_list; 977191673Sjamie } 978191673Sjamie } 979191673Sjamie 980185435Sbz /* 981191673Sjamie * See if we are creating a new record or updating an existing one. 982191673Sjamie * This abuses the file error codes ENOENT and EEXIST. 983185435Sbz */ 984191673Sjamie cuflags = flags & (JAIL_CREATE | JAIL_UPDATE); 985191673Sjamie if (!cuflags) { 986191673Sjamie error = EINVAL; 987191673Sjamie vfs_opterror(opts, "no valid operation (create or update)"); 988191673Sjamie goto done_unlock_list; 989191673Sjamie } 990191673Sjamie pr = NULL; 991196835Sjamie namelc = NULL; 992196835Sjamie if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) { 993196835Sjamie namelc = strrchr(name, '.'); 994196835Sjamie jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10); 995196835Sjamie if (*p != '\0') 996196835Sjamie jid = 0; 997196835Sjamie } 998191673Sjamie if (jid != 0) { 999192895Sjamie /* 1000192895Sjamie * See if a requested jid already exists. There is an 1001192895Sjamie * information leak here if the jid exists but is not within 1002192895Sjamie * the caller's jail hierarchy. Jail creators will get EEXIST 1003192895Sjamie * even though they cannot see the jail, and CREATE | UPDATE 1004192895Sjamie * will return ENOENT which is not normally a valid error. 1005192895Sjamie */ 1006191673Sjamie if (jid < 0) { 1007191673Sjamie error = EINVAL; 1008191673Sjamie vfs_opterror(opts, "negative jid"); 1009191673Sjamie goto done_unlock_list; 1010191673Sjamie } 1011191673Sjamie pr = prison_find(jid); 1012191673Sjamie if (pr != NULL) { 1013192895Sjamie ppr = pr->pr_parent; 1014191673Sjamie /* Create: jid must not exist. */ 1015191673Sjamie if (cuflags == JAIL_CREATE) { 1016191673Sjamie mtx_unlock(&pr->pr_mtx); 1017191673Sjamie error = EEXIST; 1018191673Sjamie vfs_opterror(opts, "jail %d already exists", 1019191673Sjamie jid); 1020191673Sjamie goto done_unlock_list; 1021191673Sjamie } 1022192895Sjamie if (!prison_ischild(mypr, pr)) { 1023192895Sjamie mtx_unlock(&pr->pr_mtx); 1024192895Sjamie pr = NULL; 1025192895Sjamie } else if (pr->pr_uref == 0) { 1026191673Sjamie if (!(flags & JAIL_DYING)) { 1027191673Sjamie mtx_unlock(&pr->pr_mtx); 1028191673Sjamie error = ENOENT; 1029191673Sjamie vfs_opterror(opts, "jail %d is dying", 1030191673Sjamie jid); 1031191673Sjamie goto done_unlock_list; 1032191673Sjamie } else if ((flags & JAIL_ATTACH) || 1033191673Sjamie (pr_flags & PR_PERSIST)) { 1034191673Sjamie /* 1035191673Sjamie * A dying jail might be resurrected 1036191673Sjamie * (via attach or persist), but first 1037191673Sjamie * it must determine if another jail 1038191673Sjamie * has claimed its name. Accomplish 1039191673Sjamie * this by implicitly re-setting the 1040191673Sjamie * name. 1041191673Sjamie */ 1042191673Sjamie if (name == NULL) 1043192895Sjamie name = prison_name(mypr, pr); 1044191673Sjamie } 1045191673Sjamie } 1046191673Sjamie } 1047191673Sjamie if (pr == NULL) { 1048191673Sjamie /* Update: jid must exist. */ 1049191673Sjamie if (cuflags == JAIL_UPDATE) { 1050191673Sjamie error = ENOENT; 1051191673Sjamie vfs_opterror(opts, "jail %d not found", jid); 1052191673Sjamie goto done_unlock_list; 1053191673Sjamie } 1054191673Sjamie } 1055191673Sjamie } 1056191673Sjamie /* 1057191673Sjamie * If the caller provided a name, look for a jail by that name. 1058191673Sjamie * This has different semantics for creates and updates keyed by jid 1059191673Sjamie * (where the name must not already exist in a different jail), 1060191673Sjamie * and updates keyed by the name itself (where the name must exist 1061191673Sjamie * because that is the jail being updated). 1062191673Sjamie */ 1063191673Sjamie if (name != NULL) { 1064196835Sjamie namelc = strrchr(name, '.'); 1065196835Sjamie if (namelc == NULL) 1066196835Sjamie namelc = name; 1067196835Sjamie else { 1068192895Sjamie /* 1069192895Sjamie * This is a hierarchical name. Split it into the 1070192895Sjamie * parent and child names, and make sure the parent 1071192895Sjamie * exists or matches an already found jail. 1072192895Sjamie */ 1073196835Sjamie *namelc = '\0'; 1074192895Sjamie if (pr != NULL) { 1075196835Sjamie if (strncmp(name, ppr->pr_name, namelc - name) 1076196835Sjamie || ppr->pr_name[namelc - name] != '\0') { 1077192895Sjamie mtx_unlock(&pr->pr_mtx); 1078192895Sjamie error = EINVAL; 1079192895Sjamie vfs_opterror(opts, 1080192895Sjamie "cannot change jail's parent"); 1081192895Sjamie goto done_unlock_list; 1082192895Sjamie } 1083192895Sjamie } else { 1084192895Sjamie ppr = prison_find_name(mypr, name); 1085192895Sjamie if (ppr == NULL) { 1086192895Sjamie error = ENOENT; 1087192895Sjamie vfs_opterror(opts, 1088192895Sjamie "jail \"%s\" not found", name); 1089192895Sjamie goto done_unlock_list; 1090192895Sjamie } 1091192895Sjamie mtx_unlock(&ppr->pr_mtx); 1092192895Sjamie } 1093196835Sjamie name = ++namelc; 1094192895Sjamie } 1095191673Sjamie if (name[0] != '\0') { 1096192895Sjamie namelen = 1097192895Sjamie (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 1098192895Sjamie name_again: 1099191673Sjamie deadpr = NULL; 1100192895Sjamie FOREACH_PRISON_CHILD(ppr, tpr) { 1101191673Sjamie if (tpr != pr && tpr->pr_ref > 0 && 1102192895Sjamie !strcmp(tpr->pr_name + namelen, name)) { 1103191673Sjamie if (pr == NULL && 1104191673Sjamie cuflags != JAIL_CREATE) { 1105191673Sjamie mtx_lock(&tpr->pr_mtx); 1106191673Sjamie if (tpr->pr_ref > 0) { 1107191673Sjamie /* 1108191673Sjamie * Use this jail 1109191673Sjamie * for updates. 1110191673Sjamie */ 1111191673Sjamie if (tpr->pr_uref > 0) { 1112191673Sjamie pr = tpr; 1113191673Sjamie break; 1114191673Sjamie } 1115191673Sjamie deadpr = tpr; 1116191673Sjamie } 1117191673Sjamie mtx_unlock(&tpr->pr_mtx); 1118191673Sjamie } else if (tpr->pr_uref > 0) { 1119191673Sjamie /* 1120191673Sjamie * Create, or update(jid): 1121191673Sjamie * name must not exist in an 1122192895Sjamie * active sibling jail. 1123191673Sjamie */ 1124191673Sjamie error = EEXIST; 1125191673Sjamie if (pr != NULL) 1126191673Sjamie mtx_unlock(&pr->pr_mtx); 1127191673Sjamie vfs_opterror(opts, 1128191673Sjamie "jail \"%s\" already exists", 1129191673Sjamie name); 1130191673Sjamie goto done_unlock_list; 1131191673Sjamie } 1132191673Sjamie } 1133191673Sjamie } 1134191673Sjamie /* If no active jail is found, use a dying one. */ 1135191673Sjamie if (deadpr != NULL && pr == NULL) { 1136191673Sjamie if (flags & JAIL_DYING) { 1137191673Sjamie mtx_lock(&deadpr->pr_mtx); 1138191673Sjamie if (deadpr->pr_ref == 0) { 1139191673Sjamie mtx_unlock(&deadpr->pr_mtx); 1140191673Sjamie goto name_again; 1141191673Sjamie } 1142191673Sjamie pr = deadpr; 1143191673Sjamie } else if (cuflags == JAIL_UPDATE) { 1144191673Sjamie error = ENOENT; 1145191673Sjamie vfs_opterror(opts, 1146191673Sjamie "jail \"%s\" is dying", name); 1147191673Sjamie goto done_unlock_list; 1148191673Sjamie } 1149191673Sjamie } 1150191673Sjamie /* Update: name must exist if no jid. */ 1151191673Sjamie else if (cuflags == JAIL_UPDATE && pr == NULL) { 1152191673Sjamie error = ENOENT; 1153191673Sjamie vfs_opterror(opts, "jail \"%s\" not found", 1154191673Sjamie name); 1155191673Sjamie goto done_unlock_list; 1156191673Sjamie } 1157191673Sjamie } 1158191673Sjamie } 1159191673Sjamie /* Update: must provide a jid or name. */ 1160191673Sjamie else if (cuflags == JAIL_UPDATE && pr == NULL) { 1161191673Sjamie error = ENOENT; 1162191673Sjamie vfs_opterror(opts, "update specified no jail"); 1163191673Sjamie goto done_unlock_list; 1164191673Sjamie } 1165185435Sbz 1166191673Sjamie /* If there's no prison to update, create a new one and link it in. */ 1167191673Sjamie if (pr == NULL) { 1168194762Sjamie for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent) 1169194762Sjamie if (tpr->pr_childcount >= tpr->pr_childmax) { 1170194762Sjamie error = EPERM; 1171194762Sjamie vfs_opterror(opts, "prison limit exceeded"); 1172194762Sjamie goto done_unlock_list; 1173194762Sjamie } 1174191673Sjamie created = 1; 1175192895Sjamie mtx_lock(&ppr->pr_mtx); 1176192895Sjamie if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) { 1177192895Sjamie mtx_unlock(&ppr->pr_mtx); 1178192895Sjamie error = ENOENT; 1179192895Sjamie vfs_opterror(opts, "parent jail went away!"); 1180192895Sjamie goto done_unlock_list; 1181192895Sjamie } 1182192895Sjamie ppr->pr_ref++; 1183192895Sjamie ppr->pr_uref++; 1184192895Sjamie mtx_unlock(&ppr->pr_mtx); 1185191673Sjamie pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO); 1186191673Sjamie if (jid == 0) { 1187191673Sjamie /* Find the next free jid. */ 1188191673Sjamie jid = lastprid + 1; 1189191673Sjamie findnext: 1190191673Sjamie if (jid == JAIL_MAX) 1191191673Sjamie jid = 1; 1192191673Sjamie TAILQ_FOREACH(tpr, &allprison, pr_list) { 1193191673Sjamie if (tpr->pr_id < jid) 1194191673Sjamie continue; 1195191673Sjamie if (tpr->pr_id > jid || tpr->pr_ref == 0) { 1196191673Sjamie TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1197191673Sjamie break; 1198191673Sjamie } 1199191673Sjamie if (jid == lastprid) { 1200191673Sjamie error = EAGAIN; 1201191673Sjamie vfs_opterror(opts, 1202191673Sjamie "no available jail IDs"); 1203191673Sjamie free(pr, M_PRISON); 1204192895Sjamie prison_deref(ppr, PD_DEREF | 1205192895Sjamie PD_DEUREF | PD_LIST_XLOCKED); 1206192895Sjamie goto done_releroot; 1207191673Sjamie } 1208191673Sjamie jid++; 1209191673Sjamie goto findnext; 1210191673Sjamie } 1211191673Sjamie lastprid = jid; 1212191673Sjamie } else { 1213191673Sjamie /* 1214191673Sjamie * The jail already has a jid (that did not yet exist), 1215191673Sjamie * so just find where to insert it. 1216191673Sjamie */ 1217191673Sjamie TAILQ_FOREACH(tpr, &allprison, pr_list) 1218191673Sjamie if (tpr->pr_id >= jid) { 1219191673Sjamie TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1220191673Sjamie break; 1221191673Sjamie } 1222191673Sjamie } 1223191673Sjamie if (tpr == NULL) 1224191673Sjamie TAILQ_INSERT_TAIL(&allprison, pr, pr_list); 1225192895Sjamie LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling); 1226192895Sjamie for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 1227194762Sjamie tpr->pr_childcount++; 1228185435Sbz 1229192895Sjamie pr->pr_parent = ppr; 1230191673Sjamie pr->pr_id = jid; 1231192895Sjamie 1232192895Sjamie /* Set some default values, and inherit some from the parent. */ 1233191673Sjamie if (name == NULL) 1234191673Sjamie name = ""; 1235191673Sjamie if (path == NULL) { 1236191673Sjamie path = "/"; 1237192895Sjamie root = mypr->pr_root; 1238191673Sjamie vref(root); 1239191673Sjamie } 1240195944Sjamie strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN); 1241195944Sjamie pr->pr_flags |= PR_HOST; 1242195945Sjamie#if defined(INET) || defined(INET6) 1243195945Sjamie#ifdef VIMAGE 1244195945Sjamie if (!(pr_flags & PR_VNET)) 1245195945Sjamie#endif 1246195945Sjamie { 1247192895Sjamie#ifdef INET 1248195974Sjamie if (!(ch_flags & PR_IP4_USER)) 1249195974Sjamie pr->pr_flags |= 1250195974Sjamie PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE; 1251195974Sjamie else if (!(pr_flags & PR_IP4_USER)) { 1252195974Sjamie pr->pr_flags |= ppr->pr_flags & PR_IP4; 1253195974Sjamie if (ppr->pr_ip4 != NULL) { 1254195974Sjamie pr->pr_ip4s = ppr->pr_ip4s; 1255195974Sjamie pr->pr_ip4 = malloc(pr->pr_ip4s * 1256195974Sjamie sizeof(struct in_addr), M_PRISON, 1257195974Sjamie M_WAITOK); 1258195974Sjamie bcopy(ppr->pr_ip4, pr->pr_ip4, 1259195974Sjamie pr->pr_ip4s * sizeof(*pr->pr_ip4)); 1260195974Sjamie } 1261195974Sjamie } 1262192895Sjamie#endif 1263192895Sjamie#ifdef INET6 1264195974Sjamie if (!(ch_flags & PR_IP6_USER)) 1265195974Sjamie pr->pr_flags |= 1266195974Sjamie PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE; 1267195974Sjamie else if (!(pr_flags & PR_IP6_USER)) { 1268195974Sjamie pr->pr_flags |= ppr->pr_flags & PR_IP6; 1269195974Sjamie if (ppr->pr_ip6 != NULL) { 1270195974Sjamie pr->pr_ip6s = ppr->pr_ip6s; 1271195974Sjamie pr->pr_ip6 = malloc(pr->pr_ip6s * 1272195974Sjamie sizeof(struct in6_addr), M_PRISON, 1273195974Sjamie M_WAITOK); 1274195974Sjamie bcopy(ppr->pr_ip6, pr->pr_ip6, 1275195974Sjamie pr->pr_ip6s * sizeof(*pr->pr_ip6)); 1276195974Sjamie } 1277195974Sjamie } 1278192895Sjamie#endif 1279195945Sjamie } 1280195945Sjamie#endif 1281202468Sbz /* Source address selection is always on by default. */ 1282202468Sbz pr->pr_flags |= _PR_IP_SADDRSEL; 1283202468Sbz 1284192895Sjamie pr->pr_securelevel = ppr->pr_securelevel; 1285192895Sjamie pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow; 1286196002Sjamie pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 1287232059Smm pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum; 1288191673Sjamie 1289192895Sjamie LIST_INIT(&pr->pr_children); 1290192895Sjamie mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK); 1291191673Sjamie 1292194251Sjamie#ifdef VIMAGE 1293194251Sjamie /* Allocate a new vnet if specified. */ 1294194251Sjamie pr->pr_vnet = (pr_flags & PR_VNET) 1295194251Sjamie ? vnet_alloc() : ppr->pr_vnet; 1296194251Sjamie#endif 1297185435Sbz /* 1298191673Sjamie * Allocate a dedicated cpuset for each jail. 1299191673Sjamie * Unlike other initial settings, this may return an erorr. 1300185435Sbz */ 1301192895Sjamie error = cpuset_create_root(ppr, &pr->pr_cpuset); 1302191673Sjamie if (error) { 1303191673Sjamie prison_deref(pr, PD_LIST_XLOCKED); 1304191673Sjamie goto done_releroot; 1305191673Sjamie } 1306185435Sbz 1307191673Sjamie mtx_lock(&pr->pr_mtx); 1308185435Sbz /* 1309191673Sjamie * New prisons do not yet have a reference, because we do not 1310191673Sjamie * want other to see the incomplete prison once the 1311191673Sjamie * allprison_lock is downgraded. 1312185435Sbz */ 1313191673Sjamie } else { 1314191673Sjamie created = 0; 1315195974Sjamie /* 1316195974Sjamie * Grab a reference for existing prisons, to ensure they 1317195974Sjamie * continue to exist for the duration of the call. 1318195974Sjamie */ 1319195974Sjamie pr->pr_ref++; 1320195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6)) 1321195945Sjamie if ((pr->pr_flags & PR_VNET) && 1322195945Sjamie (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 1323195945Sjamie error = EINVAL; 1324195945Sjamie vfs_opterror(opts, 1325195945Sjamie "vnet jails cannot have IP address restrictions"); 1326195945Sjamie goto done_deref_locked; 1327195945Sjamie } 1328195945Sjamie#endif 1329195974Sjamie#ifdef INET 1330195974Sjamie if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 1331195974Sjamie error = EINVAL; 1332195974Sjamie vfs_opterror(opts, 1333195974Sjamie "ip4 cannot be changed after creation"); 1334195974Sjamie goto done_deref_locked; 1335195974Sjamie } 1336195974Sjamie#endif 1337195974Sjamie#ifdef INET6 1338195974Sjamie if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 1339195974Sjamie error = EINVAL; 1340195974Sjamie vfs_opterror(opts, 1341195974Sjamie "ip6 cannot be changed after creation"); 1342195974Sjamie goto done_deref_locked; 1343195974Sjamie } 1344195974Sjamie#endif 1345191673Sjamie } 1346185435Sbz 1347191673Sjamie /* Do final error checking before setting anything. */ 1348192895Sjamie if (gotslevel) { 1349192895Sjamie if (slevel < ppr->pr_securelevel) { 1350192895Sjamie error = EPERM; 1351192895Sjamie goto done_deref_locked; 1352192895Sjamie } 1353192895Sjamie } 1354194762Sjamie if (gotchildmax) { 1355194762Sjamie if (childmax >= ppr->pr_childmax) { 1356194762Sjamie error = EPERM; 1357194762Sjamie goto done_deref_locked; 1358194762Sjamie } 1359194762Sjamie } 1360192895Sjamie if (gotenforce) { 1361192895Sjamie if (enforce < ppr->pr_enforce_statfs) { 1362192895Sjamie error = EPERM; 1363192895Sjamie goto done_deref_locked; 1364192895Sjamie } 1365192895Sjamie } 1366231267Smm if (gotrsnum) { 1367231267Smm /* 1368231267Smm * devfs_rsnum is a uint16_t 1369231267Smm */ 1370232059Smm if (rsnum < 0 || rsnum > 65535) { 1371231267Smm error = EINVAL; 1372231267Smm goto done_deref_locked; 1373231267Smm } 1374231267Smm /* 1375232059Smm * Nested jails always inherit parent's devfs ruleset 1376231267Smm */ 1377231267Smm if (jailed(td->td_ucred)) { 1378231267Smm if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) { 1379231267Smm error = EPERM; 1380231267Smm goto done_deref_locked; 1381232059Smm } else 1382231267Smm rsnum = ppr->pr_devfs_rsnum; 1383231267Smm } 1384231267Smm } 1385185435Sbz#ifdef INET 1386195974Sjamie if (ip4s > 0) { 1387192895Sjamie if (ppr->pr_flags & PR_IP4) { 1388195974Sjamie /* 1389195974Sjamie * Make sure the new set of IP addresses is a 1390195974Sjamie * subset of the parent's list. Don't worry 1391195974Sjamie * about the parent being unlocked, as any 1392195974Sjamie * setting is done with allprison_lock held. 1393195974Sjamie */ 1394195974Sjamie for (ij = 0; ij < ppr->pr_ip4s; ij++) 1395195974Sjamie if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 1396195974Sjamie break; 1397195974Sjamie if (ij == ppr->pr_ip4s) { 1398195974Sjamie error = EPERM; 1399195974Sjamie goto done_deref_locked; 1400195974Sjamie } 1401195974Sjamie if (ip4s > 1) { 1402195974Sjamie for (ii = ij = 1; ii < ip4s; ii++) { 1403195974Sjamie if (ip4[ii].s_addr == 1404195974Sjamie ppr->pr_ip4[0].s_addr) 1405195974Sjamie continue; 1406195974Sjamie for (; ij < ppr->pr_ip4s; ij++) 1407195974Sjamie if (ip4[ii].s_addr == 1408195974Sjamie ppr->pr_ip4[ij].s_addr) 1409195974Sjamie break; 1410195974Sjamie if (ij == ppr->pr_ip4s) 1411195974Sjamie break; 1412192895Sjamie } 1413192895Sjamie if (ij == ppr->pr_ip4s) { 1414192895Sjamie error = EPERM; 1415192895Sjamie goto done_deref_locked; 1416192895Sjamie } 1417192895Sjamie } 1418192895Sjamie } 1419195974Sjamie /* 1420195974Sjamie * Check for conflicting IP addresses. We permit them 1421195974Sjamie * if there is no more than one IP on each jail. If 1422195974Sjamie * there is a duplicate on a jail with more than one 1423195974Sjamie * IP stop checking and return error. 1424195974Sjamie */ 1425195974Sjamie tppr = ppr; 1426195945Sjamie#ifdef VIMAGE 1427195974Sjamie for (; tppr != &prison0; tppr = tppr->pr_parent) 1428195974Sjamie if (tppr->pr_flags & PR_VNET) 1429195974Sjamie break; 1430195945Sjamie#endif 1431195974Sjamie FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1432195974Sjamie if (tpr == pr || 1433195945Sjamie#ifdef VIMAGE 1434195974Sjamie (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1435195945Sjamie#endif 1436195974Sjamie tpr->pr_uref == 0) { 1437192895Sjamie descend = 0; 1438195974Sjamie continue; 1439195974Sjamie } 1440195974Sjamie if (!(tpr->pr_flags & PR_IP4_USER)) 1441195974Sjamie continue; 1442195974Sjamie descend = 0; 1443195974Sjamie if (tpr->pr_ip4 == NULL || 1444195974Sjamie (ip4s == 1 && tpr->pr_ip4s == 1)) 1445195974Sjamie continue; 1446195974Sjamie for (ii = 0; ii < ip4s; ii++) { 1447195974Sjamie if (_prison_check_ip4(tpr, &ip4[ii]) == 0) { 1448195974Sjamie error = EADDRINUSE; 1449195974Sjamie vfs_opterror(opts, 1450195974Sjamie "IPv4 addresses clash"); 1451195974Sjamie goto done_deref_locked; 1452192895Sjamie } 1453192895Sjamie } 1454192895Sjamie } 1455192895Sjamie } 1456185435Sbz#endif 1457191673Sjamie#ifdef INET6 1458195974Sjamie if (ip6s > 0) { 1459192895Sjamie if (ppr->pr_flags & PR_IP6) { 1460195974Sjamie /* 1461195974Sjamie * Make sure the new set of IP addresses is a 1462195974Sjamie * subset of the parent's list. 1463195974Sjamie */ 1464195974Sjamie for (ij = 0; ij < ppr->pr_ip6s; ij++) 1465195974Sjamie if (IN6_ARE_ADDR_EQUAL(&ip6[0], 1466195974Sjamie &ppr->pr_ip6[ij])) 1467195974Sjamie break; 1468195974Sjamie if (ij == ppr->pr_ip6s) { 1469195974Sjamie error = EPERM; 1470195974Sjamie goto done_deref_locked; 1471195974Sjamie } 1472195974Sjamie if (ip6s > 1) { 1473195974Sjamie for (ii = ij = 1; ii < ip6s; ii++) { 1474195974Sjamie if (IN6_ARE_ADDR_EQUAL(&ip6[ii], 1475195974Sjamie &ppr->pr_ip6[0])) 1476195974Sjamie continue; 1477195974Sjamie for (; ij < ppr->pr_ip6s; ij++) 1478195974Sjamie if (IN6_ARE_ADDR_EQUAL( 1479195974Sjamie &ip6[ii], &ppr->pr_ip6[ij])) 1480195974Sjamie break; 1481195974Sjamie if (ij == ppr->pr_ip6s) 1482195974Sjamie break; 1483192895Sjamie } 1484192895Sjamie if (ij == ppr->pr_ip6s) { 1485192895Sjamie error = EPERM; 1486192895Sjamie goto done_deref_locked; 1487192895Sjamie } 1488192895Sjamie } 1489192895Sjamie } 1490195974Sjamie /* Check for conflicting IP addresses. */ 1491195974Sjamie tppr = ppr; 1492195945Sjamie#ifdef VIMAGE 1493195974Sjamie for (; tppr != &prison0; tppr = tppr->pr_parent) 1494195974Sjamie if (tppr->pr_flags & PR_VNET) 1495195974Sjamie break; 1496195945Sjamie#endif 1497195974Sjamie FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1498195974Sjamie if (tpr == pr || 1499195945Sjamie#ifdef VIMAGE 1500195974Sjamie (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1501195945Sjamie#endif 1502195974Sjamie tpr->pr_uref == 0) { 1503192895Sjamie descend = 0; 1504195974Sjamie continue; 1505195974Sjamie } 1506195974Sjamie if (!(tpr->pr_flags & PR_IP6_USER)) 1507195974Sjamie continue; 1508195974Sjamie descend = 0; 1509195974Sjamie if (tpr->pr_ip6 == NULL || 1510195974Sjamie (ip6s == 1 && tpr->pr_ip6s == 1)) 1511195974Sjamie continue; 1512195974Sjamie for (ii = 0; ii < ip6s; ii++) { 1513195974Sjamie if (_prison_check_ip6(tpr, &ip6[ii]) == 0) { 1514195974Sjamie error = EADDRINUSE; 1515195974Sjamie vfs_opterror(opts, 1516195974Sjamie "IPv6 addresses clash"); 1517195974Sjamie goto done_deref_locked; 1518192895Sjamie } 1519192895Sjamie } 1520191673Sjamie } 1521192895Sjamie } 1522191673Sjamie#endif 1523192895Sjamie onamelen = namelen = 0; 1524192895Sjamie if (name != NULL) { 1525191673Sjamie /* Give a default name of the jid. */ 1526191673Sjamie if (name[0] == '\0') 1527191673Sjamie snprintf(name = numbuf, sizeof(numbuf), "%d", jid); 1528196835Sjamie else if (*namelc == '0' || (strtoul(namelc, &p, 10) != jid && 1529196835Sjamie *p == '\0')) { 1530191673Sjamie error = EINVAL; 1531196835Sjamie vfs_opterror(opts, 1532196835Sjamie "name cannot be numeric (unless it is the jid)"); 1533192895Sjamie goto done_deref_locked; 1534191673Sjamie } 1535191673Sjamie /* 1536192895Sjamie * Make sure the name isn't too long for the prison or its 1537192895Sjamie * children. 1538191673Sjamie */ 1539192895Sjamie onamelen = strlen(pr->pr_name); 1540192895Sjamie namelen = strlen(name); 1541192895Sjamie if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) { 1542192895Sjamie error = ENAMETOOLONG; 1543192895Sjamie goto done_deref_locked; 1544192895Sjamie } 1545192895Sjamie FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 1546192895Sjamie if (strlen(tpr->pr_name) + (namelen - onamelen) >= 1547192895Sjamie sizeof(pr->pr_name)) { 1548192895Sjamie error = ENAMETOOLONG; 1549192895Sjamie goto done_deref_locked; 1550192895Sjamie } 1551192895Sjamie } 1552191673Sjamie } 1553192895Sjamie if (pr_allow & ~ppr->pr_allow) { 1554192895Sjamie error = EPERM; 1555192895Sjamie goto done_deref_locked; 1556192895Sjamie } 1557185435Sbz 1558191673Sjamie /* Set the parameters of the prison. */ 1559191673Sjamie#ifdef INET 1560192895Sjamie redo_ip4 = 0; 1561195974Sjamie if (pr_flags & PR_IP4_USER) { 1562195974Sjamie pr->pr_flags |= PR_IP4; 1563195974Sjamie free(pr->pr_ip4, M_PRISON); 1564195974Sjamie pr->pr_ip4s = ip4s; 1565195974Sjamie pr->pr_ip4 = ip4; 1566195974Sjamie ip4 = NULL; 1567192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1568195945Sjamie#ifdef VIMAGE 1569195945Sjamie if (tpr->pr_flags & PR_VNET) { 1570195945Sjamie descend = 0; 1571195945Sjamie continue; 1572195945Sjamie } 1573195945Sjamie#endif 1574192895Sjamie if (prison_restrict_ip4(tpr, NULL)) { 1575192895Sjamie redo_ip4 = 1; 1576192895Sjamie descend = 0; 1577192895Sjamie } 1578192895Sjamie } 1579185435Sbz } 1580191673Sjamie#endif 1581191673Sjamie#ifdef INET6 1582192895Sjamie redo_ip6 = 0; 1583195974Sjamie if (pr_flags & PR_IP6_USER) { 1584195974Sjamie pr->pr_flags |= PR_IP6; 1585195974Sjamie free(pr->pr_ip6, M_PRISON); 1586195974Sjamie pr->pr_ip6s = ip6s; 1587195974Sjamie pr->pr_ip6 = ip6; 1588195974Sjamie ip6 = NULL; 1589192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1590195945Sjamie#ifdef VIMAGE 1591195945Sjamie if (tpr->pr_flags & PR_VNET) { 1592195945Sjamie descend = 0; 1593195945Sjamie continue; 1594195945Sjamie } 1595195945Sjamie#endif 1596192895Sjamie if (prison_restrict_ip6(tpr, NULL)) { 1597192895Sjamie redo_ip6 = 1; 1598192895Sjamie descend = 0; 1599192895Sjamie } 1600192895Sjamie } 1601191673Sjamie } 1602191673Sjamie#endif 1603192895Sjamie if (gotslevel) { 1604191673Sjamie pr->pr_securelevel = slevel; 1605192895Sjamie /* Set all child jails to be at least this level. */ 1606192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1607192895Sjamie if (tpr->pr_securelevel < slevel) 1608192895Sjamie tpr->pr_securelevel = slevel; 1609192895Sjamie } 1610194762Sjamie if (gotchildmax) { 1611194762Sjamie pr->pr_childmax = childmax; 1612194762Sjamie /* Set all child jails to under this limit. */ 1613194762Sjamie FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level) 1614194762Sjamie if (tpr->pr_childmax > childmax - level) 1615194762Sjamie tpr->pr_childmax = childmax > level 1616194762Sjamie ? childmax - level : 0; 1617194762Sjamie } 1618192895Sjamie if (gotenforce) { 1619192895Sjamie pr->pr_enforce_statfs = enforce; 1620192895Sjamie /* Pass this restriction on to the children. */ 1621192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1622192895Sjamie if (tpr->pr_enforce_statfs < enforce) 1623192895Sjamie tpr->pr_enforce_statfs = enforce; 1624192895Sjamie } 1625231267Smm if (gotrsnum) { 1626231267Smm pr->pr_devfs_rsnum = rsnum; 1627231267Smm /* Pass this restriction on to the children. */ 1628231267Smm FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1629232059Smm tpr->pr_devfs_rsnum = rsnum; 1630231267Smm } 1631192895Sjamie if (name != NULL) { 1632192895Sjamie if (ppr == &prison0) 1633192895Sjamie strlcpy(pr->pr_name, name, sizeof(pr->pr_name)); 1634192895Sjamie else 1635192895Sjamie snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s", 1636192895Sjamie ppr->pr_name, name); 1637192895Sjamie /* Change this component of child names. */ 1638192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1639192895Sjamie bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen, 1640192895Sjamie strlen(tpr->pr_name + onamelen) + 1); 1641192895Sjamie bcopy(pr->pr_name, tpr->pr_name, namelen); 1642192895Sjamie } 1643192895Sjamie } 1644191673Sjamie if (path != NULL) { 1645192895Sjamie /* Try to keep a real-rooted full pathname. */ 1646230129Smm if (fullpath_disabled && path[0] == '/' && 1647230129Smm strcmp(mypr->pr_path, "/")) 1648192895Sjamie snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s", 1649192895Sjamie mypr->pr_path, path); 1650192895Sjamie else 1651192895Sjamie strlcpy(pr->pr_path, path, sizeof(pr->pr_path)); 1652191673Sjamie pr->pr_root = root; 1653191673Sjamie } 1654193066Sjamie if (PR_HOST & ch_flags & ~pr_flags) { 1655193066Sjamie if (pr->pr_flags & PR_HOST) { 1656193066Sjamie /* 1657193066Sjamie * Copy the parent's host info. As with pr_ip4 above, 1658193066Sjamie * the lack of a lock on the parent is not a problem; 1659193066Sjamie * it is always set with allprison_lock at least 1660193066Sjamie * shared, and is held exclusively here. 1661193066Sjamie */ 1662194118Sjamie strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname, 1663194118Sjamie sizeof(pr->pr_hostname)); 1664194118Sjamie strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname, 1665194118Sjamie sizeof(pr->pr_domainname)); 1666194118Sjamie strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid, 1667194118Sjamie sizeof(pr->pr_hostuuid)); 1668193066Sjamie pr->pr_hostid = pr->pr_parent->pr_hostid; 1669193066Sjamie } 1670193066Sjamie } else if (host != NULL || domain != NULL || uuid != NULL || gothid) { 1671193066Sjamie /* Set this prison, and any descendants without PR_HOST. */ 1672193066Sjamie if (host != NULL) 1673194118Sjamie strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname)); 1674193066Sjamie if (domain != NULL) 1675194118Sjamie strlcpy(pr->pr_domainname, domain, 1676194118Sjamie sizeof(pr->pr_domainname)); 1677193066Sjamie if (uuid != NULL) 1678194118Sjamie strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid)); 1679193066Sjamie if (gothid) 1680193066Sjamie pr->pr_hostid = hid; 1681193066Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1682193066Sjamie if (tpr->pr_flags & PR_HOST) 1683193066Sjamie descend = 0; 1684193066Sjamie else { 1685193066Sjamie if (host != NULL) 1686194118Sjamie strlcpy(tpr->pr_hostname, 1687194118Sjamie pr->pr_hostname, 1688194118Sjamie sizeof(tpr->pr_hostname)); 1689193066Sjamie if (domain != NULL) 1690194118Sjamie strlcpy(tpr->pr_domainname, 1691194118Sjamie pr->pr_domainname, 1692194118Sjamie sizeof(tpr->pr_domainname)); 1693193066Sjamie if (uuid != NULL) 1694194118Sjamie strlcpy(tpr->pr_hostuuid, 1695194118Sjamie pr->pr_hostuuid, 1696194118Sjamie sizeof(tpr->pr_hostuuid)); 1697193066Sjamie if (gothid) 1698193066Sjamie tpr->pr_hostid = hid; 1699193066Sjamie } 1700193066Sjamie } 1701193066Sjamie } 1702192895Sjamie if ((tallow = ch_allow & ~pr_allow)) { 1703192895Sjamie /* Clear allow bits in all children. */ 1704192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1705192895Sjamie tpr->pr_allow &= ~tallow; 1706192895Sjamie } 1707192895Sjamie pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow; 1708191673Sjamie /* 1709191673Sjamie * Persistent prisons get an extra reference, and prisons losing their 1710191673Sjamie * persist flag lose that reference. Only do this for existing prisons 1711191673Sjamie * for now, so new ones will remain unseen until after the module 1712191673Sjamie * handlers have completed. 1713191673Sjamie */ 1714191673Sjamie if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) { 1715191673Sjamie if (pr_flags & PR_PERSIST) { 1716191673Sjamie pr->pr_ref++; 1717191673Sjamie pr->pr_uref++; 1718191673Sjamie } else { 1719191673Sjamie pr->pr_ref--; 1720191673Sjamie pr->pr_uref--; 1721191673Sjamie } 1722191673Sjamie } 1723191673Sjamie pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags; 1724191673Sjamie mtx_unlock(&pr->pr_mtx); 1725185435Sbz 1726221362Strasz#ifdef RACCT 1727221362Strasz if (created) 1728221362Strasz prison_racct_attach(pr); 1729221362Strasz#endif 1730221362Strasz 1731192895Sjamie /* Locks may have prevented a complete restriction of child IP 1732192895Sjamie * addresses. If so, allocate some more memory and try again. 1733192895Sjamie */ 1734192895Sjamie#ifdef INET 1735192895Sjamie while (redo_ip4) { 1736192895Sjamie ip4s = pr->pr_ip4s; 1737192895Sjamie ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 1738192895Sjamie mtx_lock(&pr->pr_mtx); 1739192895Sjamie redo_ip4 = 0; 1740192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1741195945Sjamie#ifdef VIMAGE 1742195945Sjamie if (tpr->pr_flags & PR_VNET) { 1743195945Sjamie descend = 0; 1744195945Sjamie continue; 1745195945Sjamie } 1746195945Sjamie#endif 1747192895Sjamie if (prison_restrict_ip4(tpr, ip4)) { 1748192895Sjamie if (ip4 != NULL) 1749192895Sjamie ip4 = NULL; 1750192895Sjamie else 1751192895Sjamie redo_ip4 = 1; 1752192895Sjamie } 1753192895Sjamie } 1754192895Sjamie mtx_unlock(&pr->pr_mtx); 1755192895Sjamie } 1756192895Sjamie#endif 1757192895Sjamie#ifdef INET6 1758192895Sjamie while (redo_ip6) { 1759192895Sjamie ip6s = pr->pr_ip6s; 1760192895Sjamie ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 1761192895Sjamie mtx_lock(&pr->pr_mtx); 1762192895Sjamie redo_ip6 = 0; 1763192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1764195945Sjamie#ifdef VIMAGE 1765195945Sjamie if (tpr->pr_flags & PR_VNET) { 1766195945Sjamie descend = 0; 1767195945Sjamie continue; 1768195945Sjamie } 1769195945Sjamie#endif 1770192895Sjamie if (prison_restrict_ip6(tpr, ip6)) { 1771192895Sjamie if (ip6 != NULL) 1772192895Sjamie ip6 = NULL; 1773192895Sjamie else 1774192895Sjamie redo_ip6 = 1; 1775192895Sjamie } 1776192895Sjamie } 1777192895Sjamie mtx_unlock(&pr->pr_mtx); 1778192895Sjamie } 1779192895Sjamie#endif 1780192895Sjamie 1781191673Sjamie /* Let the modules do their work. */ 1782191673Sjamie sx_downgrade(&allprison_lock); 1783191673Sjamie if (created) { 1784191673Sjamie error = osd_jail_call(pr, PR_METHOD_CREATE, opts); 1785191673Sjamie if (error) { 1786191673Sjamie prison_deref(pr, PD_LIST_SLOCKED); 1787191673Sjamie goto done_errmsg; 1788191673Sjamie } 1789191673Sjamie } 1790191673Sjamie error = osd_jail_call(pr, PR_METHOD_SET, opts); 1791191673Sjamie if (error) { 1792191673Sjamie prison_deref(pr, created 1793191673Sjamie ? PD_LIST_SLOCKED 1794191673Sjamie : PD_DEREF | PD_LIST_SLOCKED); 1795191673Sjamie goto done_errmsg; 1796191673Sjamie } 1797191673Sjamie 1798191673Sjamie /* Attach this process to the prison if requested. */ 1799191673Sjamie if (flags & JAIL_ATTACH) { 1800191673Sjamie mtx_lock(&pr->pr_mtx); 1801191673Sjamie error = do_jail_attach(td, pr); 1802191673Sjamie if (error) { 1803191673Sjamie vfs_opterror(opts, "attach failed"); 1804191673Sjamie if (!created) 1805191673Sjamie prison_deref(pr, PD_DEREF); 1806191673Sjamie goto done_errmsg; 1807191673Sjamie } 1808191673Sjamie } 1809191673Sjamie 1810235803Strasz#ifdef RACCT 1811235803Strasz if (!created) { 1812235803Strasz sx_sunlock(&allprison_lock); 1813235803Strasz prison_racct_modify(pr); 1814235803Strasz sx_slock(&allprison_lock); 1815235803Strasz } 1816235803Strasz#endif 1817235803Strasz 1818235803Strasz td->td_retval[0] = pr->pr_id; 1819235803Strasz 1820191673Sjamie /* 1821191673Sjamie * Now that it is all there, drop the temporary reference from existing 1822191673Sjamie * prisons. Or add a reference to newly created persistent prisons 1823191673Sjamie * (which was not done earlier so that the prison would not be publicly 1824191673Sjamie * visible). 1825191673Sjamie */ 1826191673Sjamie if (!created) { 1827191673Sjamie prison_deref(pr, (flags & JAIL_ATTACH) 1828191673Sjamie ? PD_DEREF 1829191673Sjamie : PD_DEREF | PD_LIST_SLOCKED); 1830191673Sjamie } else { 1831191673Sjamie if (pr_flags & PR_PERSIST) { 1832191673Sjamie mtx_lock(&pr->pr_mtx); 1833191673Sjamie pr->pr_ref++; 1834191673Sjamie pr->pr_uref++; 1835191673Sjamie mtx_unlock(&pr->pr_mtx); 1836191673Sjamie } 1837191673Sjamie if (!(flags & JAIL_ATTACH)) 1838191673Sjamie sx_sunlock(&allprison_lock); 1839191673Sjamie } 1840232598Strasz 1841191673Sjamie goto done_errmsg; 1842191673Sjamie 1843192895Sjamie done_deref_locked: 1844192895Sjamie prison_deref(pr, created 1845192895Sjamie ? PD_LOCKED | PD_LIST_XLOCKED 1846192895Sjamie : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 1847192895Sjamie goto done_releroot; 1848191673Sjamie done_unlock_list: 1849191673Sjamie sx_xunlock(&allprison_lock); 1850191673Sjamie done_releroot: 1851241896Skib if (root != NULL) 1852191673Sjamie vrele(root); 1853191673Sjamie done_errmsg: 1854191673Sjamie if (error) { 1855191673Sjamie vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 1856191673Sjamie if (errmsg_len > 0) { 1857191673Sjamie errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1; 1858191673Sjamie if (errmsg_pos > 0) { 1859191673Sjamie if (optuio->uio_segflg == UIO_SYSSPACE) 1860191673Sjamie bcopy(errmsg, 1861191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 1862191673Sjamie errmsg_len); 1863191673Sjamie else 1864191673Sjamie copyout(errmsg, 1865191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 1866191673Sjamie errmsg_len); 1867191673Sjamie } 1868191673Sjamie } 1869191673Sjamie } 1870191673Sjamie done_free: 1871191673Sjamie#ifdef INET 1872191673Sjamie free(ip4, M_PRISON); 1873191673Sjamie#endif 1874191673Sjamie#ifdef INET6 1875191673Sjamie free(ip6, M_PRISON); 1876191673Sjamie#endif 1877230407Smm if (g_path != NULL) 1878230407Smm free(g_path, M_TEMP); 1879191673Sjamie vfs_freeopts(opts); 1880191673Sjamie return (error); 1881191673Sjamie} 1882191673Sjamie 1883191673Sjamie 188482710Sdillon/* 1885191673Sjamie * struct jail_get_args { 1886191673Sjamie * struct iovec *iovp; 1887191673Sjamie * unsigned int iovcnt; 1888191673Sjamie * int flags; 1889114168Smike * }; 189082710Sdillon */ 189146155Sphkint 1892225617Skmacysys_jail_get(struct thread *td, struct jail_get_args *uap) 189346155Sphk{ 1894191673Sjamie struct uio *auio; 1895185435Sbz int error; 1896185435Sbz 1897191673Sjamie /* Check that we have an even number of iovecs. */ 1898191673Sjamie if (uap->iovcnt & 1) 1899191673Sjamie return (EINVAL); 1900191673Sjamie 1901191673Sjamie error = copyinuio(uap->iovp, uap->iovcnt, &auio); 1902185435Sbz if (error) 1903185435Sbz return (error); 1904191673Sjamie error = kern_jail_get(td, auio, uap->flags); 1905191673Sjamie if (error == 0) 1906191673Sjamie error = copyout(auio->uio_iov, uap->iovp, 1907191673Sjamie uap->iovcnt * sizeof (struct iovec)); 1908191673Sjamie free(auio, M_IOV); 1909191673Sjamie return (error); 1910191673Sjamie} 1911185435Sbz 1912191673Sjamieint 1913191673Sjamiekern_jail_get(struct thread *td, struct uio *optuio, int flags) 1914191673Sjamie{ 1915192895Sjamie struct prison *pr, *mypr; 1916191673Sjamie struct vfsopt *opt; 1917191673Sjamie struct vfsoptlist *opts; 1918191673Sjamie char *errmsg, *name; 1919192895Sjamie int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos; 1920185435Sbz 1921191673Sjamie if (flags & ~JAIL_GET_MASK) 1922191673Sjamie return (EINVAL); 1923185435Sbz 1924191673Sjamie /* Get the parameter list. */ 1925191673Sjamie error = vfs_buildopts(optuio, &opts); 1926191673Sjamie if (error) 1927191673Sjamie return (error); 1928191673Sjamie errmsg_pos = vfs_getopt_pos(opts, "errmsg"); 1929192895Sjamie mypr = td->td_ucred->cr_prison; 1930185435Sbz 1931191673Sjamie /* 1932191673Sjamie * Find the prison specified by one of: lastjid, jid, name. 1933191673Sjamie */ 1934191673Sjamie sx_slock(&allprison_lock); 1935191673Sjamie error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid)); 1936191673Sjamie if (error == 0) { 1937191673Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) { 1938192895Sjamie if (pr->pr_id > jid && prison_ischild(mypr, pr)) { 1939191673Sjamie mtx_lock(&pr->pr_mtx); 1940191673Sjamie if (pr->pr_ref > 0 && 1941191673Sjamie (pr->pr_uref > 0 || (flags & JAIL_DYING))) 1942191673Sjamie break; 1943191673Sjamie mtx_unlock(&pr->pr_mtx); 1944191673Sjamie } 1945191673Sjamie } 1946191673Sjamie if (pr != NULL) 1947191673Sjamie goto found_prison; 1948191673Sjamie error = ENOENT; 1949191673Sjamie vfs_opterror(opts, "no jail after %d", jid); 1950191673Sjamie goto done_unlock_list; 1951191673Sjamie } else if (error != ENOENT) 1952191673Sjamie goto done_unlock_list; 1953185435Sbz 1954191673Sjamie error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 1955191673Sjamie if (error == 0) { 1956191673Sjamie if (jid != 0) { 1957192895Sjamie pr = prison_find_child(mypr, jid); 1958191673Sjamie if (pr != NULL) { 1959191673Sjamie if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 1960191673Sjamie mtx_unlock(&pr->pr_mtx); 1961191673Sjamie error = ENOENT; 1962191673Sjamie vfs_opterror(opts, "jail %d is dying", 1963191673Sjamie jid); 1964191673Sjamie goto done_unlock_list; 1965191673Sjamie } 1966191673Sjamie goto found_prison; 1967191673Sjamie } 1968191673Sjamie error = ENOENT; 1969191673Sjamie vfs_opterror(opts, "jail %d not found", jid); 1970191673Sjamie goto done_unlock_list; 1971191673Sjamie } 1972191673Sjamie } else if (error != ENOENT) 1973191673Sjamie goto done_unlock_list; 197446155Sphk 1975191673Sjamie error = vfs_getopt(opts, "name", (void **)&name, &len); 1976191673Sjamie if (error == 0) { 1977191673Sjamie if (len == 0 || name[len - 1] != '\0') { 1978191673Sjamie error = EINVAL; 1979191673Sjamie goto done_unlock_list; 1980191673Sjamie } 1981192895Sjamie pr = prison_find_name(mypr, name); 1982191673Sjamie if (pr != NULL) { 1983191673Sjamie if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 1984191673Sjamie mtx_unlock(&pr->pr_mtx); 1985191673Sjamie error = ENOENT; 1986191673Sjamie vfs_opterror(opts, "jail \"%s\" is dying", 1987191673Sjamie name); 1988191673Sjamie goto done_unlock_list; 1989191673Sjamie } 1990191673Sjamie goto found_prison; 1991191673Sjamie } 1992191673Sjamie error = ENOENT; 1993191673Sjamie vfs_opterror(opts, "jail \"%s\" not found", name); 1994191673Sjamie goto done_unlock_list; 1995191673Sjamie } else if (error != ENOENT) 1996191673Sjamie goto done_unlock_list; 1997185435Sbz 1998191673Sjamie vfs_opterror(opts, "no jail specified"); 1999191673Sjamie error = ENOENT; 2000191673Sjamie goto done_unlock_list; 2001191673Sjamie 2002191673Sjamie found_prison: 2003191673Sjamie /* Get the parameters of the prison. */ 2004191673Sjamie pr->pr_ref++; 2005191673Sjamie locked = PD_LOCKED; 2006191673Sjamie td->td_retval[0] = pr->pr_id; 2007191673Sjamie error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id)); 2008191673Sjamie if (error != 0 && error != ENOENT) 2009191673Sjamie goto done_deref; 2010192895Sjamie i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id; 2011192895Sjamie error = vfs_setopt(opts, "parent", &i, sizeof(i)); 2012191673Sjamie if (error != 0 && error != ENOENT) 2013191673Sjamie goto done_deref; 2014192895Sjamie error = vfs_setopts(opts, "name", prison_name(mypr, pr)); 2015192895Sjamie if (error != 0 && error != ENOENT) 2016192895Sjamie goto done_deref; 2017192895Sjamie error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id, 2018191673Sjamie sizeof(pr->pr_cpuset->cs_id)); 2019191673Sjamie if (error != 0 && error != ENOENT) 2020191673Sjamie goto done_deref; 2021192895Sjamie error = vfs_setopts(opts, "path", prison_path(mypr, pr)); 2022191673Sjamie if (error != 0 && error != ENOENT) 2023191673Sjamie goto done_deref; 2024191673Sjamie#ifdef INET 2025191673Sjamie error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4, 2026191673Sjamie pr->pr_ip4s * sizeof(*pr->pr_ip4)); 2027191673Sjamie if (error != 0 && error != ENOENT) 2028191673Sjamie goto done_deref; 2029191673Sjamie#endif 2030191673Sjamie#ifdef INET6 2031191673Sjamie error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6, 2032191673Sjamie pr->pr_ip6s * sizeof(*pr->pr_ip6)); 2033191673Sjamie if (error != 0 && error != ENOENT) 2034191673Sjamie goto done_deref; 2035191673Sjamie#endif 2036191673Sjamie error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel, 2037191673Sjamie sizeof(pr->pr_securelevel)); 2038191673Sjamie if (error != 0 && error != ENOENT) 2039191673Sjamie goto done_deref; 2040194762Sjamie error = vfs_setopt(opts, "children.cur", &pr->pr_childcount, 2041194762Sjamie sizeof(pr->pr_childcount)); 2042194762Sjamie if (error != 0 && error != ENOENT) 2043194762Sjamie goto done_deref; 2044194762Sjamie error = vfs_setopt(opts, "children.max", &pr->pr_childmax, 2045194762Sjamie sizeof(pr->pr_childmax)); 2046194762Sjamie if (error != 0 && error != ENOENT) 2047194762Sjamie goto done_deref; 2048194118Sjamie error = vfs_setopts(opts, "host.hostname", pr->pr_hostname); 2049191673Sjamie if (error != 0 && error != ENOENT) 2050191673Sjamie goto done_deref; 2051194118Sjamie error = vfs_setopts(opts, "host.domainname", pr->pr_domainname); 2052193066Sjamie if (error != 0 && error != ENOENT) 2053193066Sjamie goto done_deref; 2054194118Sjamie error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid); 2055193066Sjamie if (error != 0 && error != ENOENT) 2056193066Sjamie goto done_deref; 2057205014Snwhitehorn#ifdef COMPAT_FREEBSD32 2058217896Sdchagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 2059193066Sjamie uint32_t hid32 = pr->pr_hostid; 2060193066Sjamie 2061193066Sjamie error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32)); 2062193066Sjamie } else 2063193066Sjamie#endif 2064193066Sjamie error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid, 2065193066Sjamie sizeof(pr->pr_hostid)); 2066193066Sjamie if (error != 0 && error != ENOENT) 2067193066Sjamie goto done_deref; 2068192895Sjamie error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs, 2069192895Sjamie sizeof(pr->pr_enforce_statfs)); 2070191673Sjamie if (error != 0 && error != ENOENT) 2071191673Sjamie goto done_deref; 2072231267Smm error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum, 2073231267Smm sizeof(pr->pr_devfs_rsnum)); 2074231267Smm if (error != 0 && error != ENOENT) 2075231267Smm goto done_deref; 2076192895Sjamie for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 2077192895Sjamie fi++) { 2078192895Sjamie if (pr_flag_names[fi] == NULL) 2079192895Sjamie continue; 2080192895Sjamie i = (pr->pr_flags & (1 << fi)) ? 1 : 0; 2081192895Sjamie error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i)); 2082192895Sjamie if (error != 0 && error != ENOENT) 2083192895Sjamie goto done_deref; 2084192895Sjamie i = !i; 2085192895Sjamie error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i)); 2086192895Sjamie if (error != 0 && error != ENOENT) 2087192895Sjamie goto done_deref; 2088192895Sjamie } 2089195870Sjamie for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 2090195870Sjamie fi++) { 2091195870Sjamie i = pr->pr_flags & 2092195870Sjamie (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new); 2093195870Sjamie i = pr_flag_jailsys[fi].disable && 2094195870Sjamie (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE 2095195870Sjamie : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW 2096195870Sjamie : JAIL_SYS_INHERIT; 2097195870Sjamie error = 2098195870Sjamie vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i)); 2099195870Sjamie if (error != 0 && error != ENOENT) 2100195870Sjamie goto done_deref; 2101195870Sjamie } 2102192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 2103192895Sjamie fi++) { 2104192895Sjamie if (pr_allow_names[fi] == NULL) 2105192895Sjamie continue; 2106192895Sjamie i = (pr->pr_allow & (1 << fi)) ? 1 : 0; 2107192895Sjamie error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i)); 2108192895Sjamie if (error != 0 && error != ENOENT) 2109192895Sjamie goto done_deref; 2110192895Sjamie i = !i; 2111192895Sjamie error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i)); 2112192895Sjamie if (error != 0 && error != ENOENT) 2113192895Sjamie goto done_deref; 2114192895Sjamie } 2115191673Sjamie i = (pr->pr_uref == 0); 2116191673Sjamie error = vfs_setopt(opts, "dying", &i, sizeof(i)); 2117191673Sjamie if (error != 0 && error != ENOENT) 2118191673Sjamie goto done_deref; 2119191673Sjamie i = !i; 2120191673Sjamie error = vfs_setopt(opts, "nodying", &i, sizeof(i)); 2121191673Sjamie if (error != 0 && error != ENOENT) 2122191673Sjamie goto done_deref; 2123191673Sjamie 2124191673Sjamie /* Get the module parameters. */ 2125191673Sjamie mtx_unlock(&pr->pr_mtx); 2126191673Sjamie locked = 0; 2127191673Sjamie error = osd_jail_call(pr, PR_METHOD_GET, opts); 212846155Sphk if (error) 2129191673Sjamie goto done_deref; 2130191673Sjamie prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED); 213184828Sjhb 2132191673Sjamie /* By now, all parameters should have been noted. */ 2133191673Sjamie TAILQ_FOREACH(opt, opts, link) { 2134191673Sjamie if (!opt->seen && strcmp(opt->name, "errmsg")) { 2135191673Sjamie error = EINVAL; 2136191673Sjamie vfs_opterror(opts, "unknown parameter: %s", opt->name); 2137191673Sjamie goto done_errmsg; 2138191673Sjamie } 2139185435Sbz } 2140191673Sjamie 2141191673Sjamie /* Write the fetched parameters back to userspace. */ 2142191673Sjamie error = 0; 2143191673Sjamie TAILQ_FOREACH(opt, opts, link) { 2144191673Sjamie if (opt->pos >= 0 && opt->pos != errmsg_pos) { 2145191673Sjamie pos = 2 * opt->pos + 1; 2146191673Sjamie optuio->uio_iov[pos].iov_len = opt->len; 2147191673Sjamie if (opt->value != NULL) { 2148191673Sjamie if (optuio->uio_segflg == UIO_SYSSPACE) { 2149191673Sjamie bcopy(opt->value, 2150191673Sjamie optuio->uio_iov[pos].iov_base, 2151191673Sjamie opt->len); 2152191673Sjamie } else { 2153191673Sjamie error = copyout(opt->value, 2154191673Sjamie optuio->uio_iov[pos].iov_base, 2155191673Sjamie opt->len); 2156191673Sjamie if (error) 2157191673Sjamie break; 2158191673Sjamie } 2159191673Sjamie } 2160191673Sjamie } 2161185435Sbz } 2162191673Sjamie goto done_errmsg; 2163191673Sjamie 2164191673Sjamie done_deref: 2165191673Sjamie prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED); 2166191673Sjamie goto done_errmsg; 2167191673Sjamie 2168191673Sjamie done_unlock_list: 2169191673Sjamie sx_sunlock(&allprison_lock); 2170191673Sjamie done_errmsg: 2171191673Sjamie if (error && errmsg_pos >= 0) { 2172191673Sjamie vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 2173191673Sjamie errmsg_pos = 2 * errmsg_pos + 1; 2174191673Sjamie if (errmsg_len > 0) { 2175191673Sjamie if (optuio->uio_segflg == UIO_SYSSPACE) 2176191673Sjamie bcopy(errmsg, 2177191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 2178191673Sjamie errmsg_len); 2179191673Sjamie else 2180191673Sjamie copyout(errmsg, 2181191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 2182191673Sjamie errmsg_len); 2183191673Sjamie } 2184185435Sbz } 2185191673Sjamie vfs_freeopts(opts); 2186191673Sjamie return (error); 2187191673Sjamie} 2188113275Smike 2189192895Sjamie 2190191673Sjamie/* 2191191673Sjamie * struct jail_remove_args { 2192191673Sjamie * int jid; 2193191673Sjamie * }; 2194191673Sjamie */ 2195191673Sjamieint 2196225617Skmacysys_jail_remove(struct thread *td, struct jail_remove_args *uap) 2197191673Sjamie{ 2198192895Sjamie struct prison *pr, *cpr, *lpr, *tpr; 2199192895Sjamie int descend, error; 2200185435Sbz 2201191673Sjamie error = priv_check(td, PRIV_JAIL_REMOVE); 2202185435Sbz if (error) 2203191673Sjamie return (error); 2204185435Sbz 2205185435Sbz sx_xlock(&allprison_lock); 2206192895Sjamie pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2207191673Sjamie if (pr == NULL) { 2208185435Sbz sx_xunlock(&allprison_lock); 2209191673Sjamie return (EINVAL); 2210185435Sbz } 2211185435Sbz 2212192895Sjamie /* Remove all descendants of this prison, then remove this prison. */ 2213192895Sjamie pr->pr_ref++; 2214192895Sjamie pr->pr_flags |= PR_REMOVE; 2215192895Sjamie if (!LIST_EMPTY(&pr->pr_children)) { 2216192895Sjamie mtx_unlock(&pr->pr_mtx); 2217192895Sjamie lpr = NULL; 2218192895Sjamie FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 2219192895Sjamie mtx_lock(&cpr->pr_mtx); 2220192895Sjamie if (cpr->pr_ref > 0) { 2221192895Sjamie tpr = cpr; 2222192895Sjamie cpr->pr_ref++; 2223192895Sjamie cpr->pr_flags |= PR_REMOVE; 2224192895Sjamie } else { 2225192895Sjamie /* Already removed - do not do it again. */ 2226192895Sjamie tpr = NULL; 2227192895Sjamie } 2228192895Sjamie mtx_unlock(&cpr->pr_mtx); 2229192895Sjamie if (lpr != NULL) { 2230192895Sjamie mtx_lock(&lpr->pr_mtx); 2231192895Sjamie prison_remove_one(lpr); 2232192895Sjamie sx_xlock(&allprison_lock); 2233192895Sjamie } 2234192895Sjamie lpr = tpr; 2235192895Sjamie } 2236192895Sjamie if (lpr != NULL) { 2237192895Sjamie mtx_lock(&lpr->pr_mtx); 2238192895Sjamie prison_remove_one(lpr); 2239192895Sjamie sx_xlock(&allprison_lock); 2240192895Sjamie } 2241192895Sjamie mtx_lock(&pr->pr_mtx); 2242192895Sjamie } 2243192895Sjamie prison_remove_one(pr); 2244192895Sjamie return (0); 2245192895Sjamie} 2246192895Sjamie 2247192895Sjamiestatic void 2248192895Sjamieprison_remove_one(struct prison *pr) 2249192895Sjamie{ 2250192895Sjamie struct proc *p; 2251192895Sjamie int deuref; 2252192895Sjamie 2253191673Sjamie /* If the prison was persistent, it is not anymore. */ 2254191673Sjamie deuref = 0; 2255191673Sjamie if (pr->pr_flags & PR_PERSIST) { 2256191673Sjamie pr->pr_ref--; 2257191673Sjamie deuref = PD_DEUREF; 2258191673Sjamie pr->pr_flags &= ~PR_PERSIST; 2259179881Sdelphij } 2260113275Smike 2261192895Sjamie /* 2262192895Sjamie * jail_remove added a reference. If that's the only one, remove 2263192895Sjamie * the prison now. 2264192895Sjamie */ 2265192895Sjamie KASSERT(pr->pr_ref > 0, 2266192895Sjamie ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id)); 2267192895Sjamie if (pr->pr_ref == 1) { 2268191673Sjamie prison_deref(pr, 2269191673Sjamie deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 2270192895Sjamie return; 2271191673Sjamie } 2272191673Sjamie 2273113275Smike mtx_unlock(&pr->pr_mtx); 2274191673Sjamie sx_xunlock(&allprison_lock); 2275191673Sjamie /* 2276191673Sjamie * Kill all processes unfortunate enough to be attached to this prison. 2277191673Sjamie */ 2278191673Sjamie sx_slock(&allproc_lock); 2279191673Sjamie LIST_FOREACH(p, &allproc, p_list) { 2280191673Sjamie PROC_LOCK(p); 2281191673Sjamie if (p->p_state != PRS_NEW && p->p_ucred && 2282191673Sjamie p->p_ucred->cr_prison == pr) 2283225617Skmacy kern_psignal(p, SIGKILL); 2284191673Sjamie PROC_UNLOCK(p); 2285191673Sjamie } 2286191673Sjamie sx_sunlock(&allproc_lock); 2287192895Sjamie /* Remove the temporary reference added by jail_remove. */ 2288191673Sjamie prison_deref(pr, deuref | PD_DEREF); 2289113275Smike} 2290113275Smike 2291190466Sjamie 2292113275Smike/* 2293114168Smike * struct jail_attach_args { 2294114168Smike * int jid; 2295114168Smike * }; 2296113275Smike */ 2297113275Smikeint 2298225617Skmacysys_jail_attach(struct thread *td, struct jail_attach_args *uap) 2299113275Smike{ 2300113275Smike struct prison *pr; 2301191673Sjamie int error; 2302167309Spjd 2303164032Srwatson error = priv_check(td, PRIV_JAIL_ATTACH); 2304126023Snectar if (error) 2305126023Snectar return (error); 2306126023Snectar 2307168401Spjd sx_slock(&allprison_lock); 2308192895Sjamie pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2309113275Smike if (pr == NULL) { 2310168401Spjd sx_sunlock(&allprison_lock); 2311113275Smike return (EINVAL); 2312113275Smike } 2313185435Sbz 2314185435Sbz /* 2315185435Sbz * Do not allow a process to attach to a prison that is not 2316191673Sjamie * considered to be "alive". 2317185435Sbz */ 2318191673Sjamie if (pr->pr_uref == 0) { 2319185435Sbz mtx_unlock(&pr->pr_mtx); 2320185435Sbz sx_sunlock(&allprison_lock); 2321185435Sbz return (EINVAL); 2322185435Sbz } 2323191673Sjamie 2324191673Sjamie return (do_jail_attach(td, pr)); 2325191673Sjamie} 2326191673Sjamie 2327191673Sjamiestatic int 2328191673Sjamiedo_jail_attach(struct thread *td, struct prison *pr) 2329191673Sjamie{ 2330192895Sjamie struct prison *ppr; 2331191673Sjamie struct proc *p; 2332191673Sjamie struct ucred *newcred, *oldcred; 2333241896Skib int error; 2334191673Sjamie 2335191673Sjamie /* 2336191673Sjamie * XXX: Note that there is a slight race here if two threads 2337191673Sjamie * in the same privileged process attempt to attach to two 2338191673Sjamie * different jails at the same time. It is important for 2339191673Sjamie * user processes not to do this, or they might end up with 2340191673Sjamie * a process root from one prison, but attached to the jail 2341191673Sjamie * of another. 2342191673Sjamie */ 2343113275Smike pr->pr_ref++; 2344191673Sjamie pr->pr_uref++; 2345113275Smike mtx_unlock(&pr->pr_mtx); 2346191673Sjamie 2347191673Sjamie /* Let modules do whatever they need to prepare for attaching. */ 2348191673Sjamie error = osd_jail_call(pr, PR_METHOD_ATTACH, td); 2349191673Sjamie if (error) { 2350191673Sjamie prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED); 2351191673Sjamie return (error); 2352191673Sjamie } 2353168401Spjd sx_sunlock(&allprison_lock); 2354113275Smike 2355185435Sbz /* 2356185435Sbz * Reparent the newly attached process to this jail. 2357185435Sbz */ 2358192895Sjamie ppr = td->td_ucred->cr_prison; 2359191673Sjamie p = td->td_proc; 2360185435Sbz error = cpuset_setproc_update_set(p, pr->pr_cpuset); 2361185435Sbz if (error) 2362191673Sjamie goto e_revert_osd; 2363185435Sbz 2364175202Sattilio vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY); 2365113275Smike if ((error = change_dir(pr->pr_root, td)) != 0) 2366113275Smike goto e_unlock; 2367113275Smike#ifdef MAC 2368172930Srwatson if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root))) 2369113275Smike goto e_unlock; 2370113275Smike#endif 2371175294Sattilio VOP_UNLOCK(pr->pr_root, 0); 2372191673Sjamie if ((error = change_root(pr->pr_root, td))) 2373241896Skib goto e_revert_osd; 2374113275Smike 237584828Sjhb newcred = crget(); 237684828Sjhb PROC_LOCK(p); 237784828Sjhb oldcred = p->p_ucred; 2378113275Smike setsugid(p); 237984828Sjhb crcopy(newcred, oldcred); 2380113630Sjhb newcred->cr_prison = pr; 238184828Sjhb p->p_ucred = newcred; 238284828Sjhb PROC_UNLOCK(p); 2383220137Strasz#ifdef RACCT 2384220137Strasz racct_proc_ucred_changed(p, oldcred, newcred); 2385220137Strasz#endif 238684828Sjhb crfree(oldcred); 2387192895Sjamie prison_deref(ppr, PD_DEREF | PD_DEUREF); 238846155Sphk return (0); 2389191673Sjamie e_unlock: 2390175294Sattilio VOP_UNLOCK(pr->pr_root, 0); 2391191673Sjamie e_revert_osd: 2392191673Sjamie /* Tell modules this thread is still in its old jail after all. */ 2393192895Sjamie (void)osd_jail_call(ppr, PR_METHOD_ATTACH, td); 2394191673Sjamie prison_deref(pr, PD_DEREF | PD_DEUREF); 239546155Sphk return (error); 239646155Sphk} 239746155Sphk 2398192895Sjamie 2399113275Smike/* 2400113275Smike * Returns a locked prison instance, or NULL on failure. 2401113275Smike */ 2402168399Spjdstruct prison * 2403113275Smikeprison_find(int prid) 2404113275Smike{ 2405113275Smike struct prison *pr; 2406113275Smike 2407168401Spjd sx_assert(&allprison_lock, SX_LOCKED); 2408191673Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) { 2409113275Smike if (pr->pr_id == prid) { 2410113275Smike mtx_lock(&pr->pr_mtx); 2411191673Sjamie if (pr->pr_ref > 0) 2412191673Sjamie return (pr); 2413191673Sjamie mtx_unlock(&pr->pr_mtx); 2414113275Smike } 2415113275Smike } 2416113275Smike return (NULL); 2417113275Smike} 2418113275Smike 2419191673Sjamie/* 2420192895Sjamie * Find a prison that is a descendant of mypr. Returns a locked prison or NULL. 2421191673Sjamie */ 2422191673Sjamiestruct prison * 2423192895Sjamieprison_find_child(struct prison *mypr, int prid) 2424191673Sjamie{ 2425192895Sjamie struct prison *pr; 2426192895Sjamie int descend; 2427192895Sjamie 2428192895Sjamie sx_assert(&allprison_lock, SX_LOCKED); 2429192895Sjamie FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 2430192895Sjamie if (pr->pr_id == prid) { 2431192895Sjamie mtx_lock(&pr->pr_mtx); 2432192895Sjamie if (pr->pr_ref > 0) 2433192895Sjamie return (pr); 2434192895Sjamie mtx_unlock(&pr->pr_mtx); 2435192895Sjamie } 2436192895Sjamie } 2437192895Sjamie return (NULL); 2438192895Sjamie} 2439192895Sjamie 2440192895Sjamie/* 2441192895Sjamie * Look for the name relative to mypr. Returns a locked prison or NULL. 2442192895Sjamie */ 2443192895Sjamiestruct prison * 2444192895Sjamieprison_find_name(struct prison *mypr, const char *name) 2445192895Sjamie{ 2446191673Sjamie struct prison *pr, *deadpr; 2447192895Sjamie size_t mylen; 2448192895Sjamie int descend; 2449191673Sjamie 2450191673Sjamie sx_assert(&allprison_lock, SX_LOCKED); 2451192895Sjamie mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1; 2452191673Sjamie again: 2453191673Sjamie deadpr = NULL; 2454192895Sjamie FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 2455192895Sjamie if (!strcmp(pr->pr_name + mylen, name)) { 2456191673Sjamie mtx_lock(&pr->pr_mtx); 2457191673Sjamie if (pr->pr_ref > 0) { 2458191673Sjamie if (pr->pr_uref > 0) 2459191673Sjamie return (pr); 2460191673Sjamie deadpr = pr; 2461191673Sjamie } 2462191673Sjamie mtx_unlock(&pr->pr_mtx); 2463191673Sjamie } 2464191673Sjamie } 2465192895Sjamie /* There was no valid prison - perhaps there was a dying one. */ 2466191673Sjamie if (deadpr != NULL) { 2467191673Sjamie mtx_lock(&deadpr->pr_mtx); 2468191673Sjamie if (deadpr->pr_ref == 0) { 2469191673Sjamie mtx_unlock(&deadpr->pr_mtx); 2470191673Sjamie goto again; 2471191673Sjamie } 2472191673Sjamie } 2473191673Sjamie return (deadpr); 2474191673Sjamie} 2475191673Sjamie 2476191673Sjamie/* 2477192895Sjamie * See if a prison has the specific flag set. 2478192895Sjamie */ 2479192895Sjamieint 2480192895Sjamieprison_flag(struct ucred *cred, unsigned flag) 2481192895Sjamie{ 2482192895Sjamie 2483192895Sjamie /* This is an atomic read, so no locking is necessary. */ 2484192895Sjamie return (cred->cr_prison->pr_flags & flag); 2485192895Sjamie} 2486192895Sjamie 2487192895Sjamieint 2488192895Sjamieprison_allow(struct ucred *cred, unsigned flag) 2489192895Sjamie{ 2490192895Sjamie 2491192895Sjamie /* This is an atomic read, so no locking is necessary. */ 2492192895Sjamie return (cred->cr_prison->pr_allow & flag); 2493192895Sjamie} 2494192895Sjamie 2495192895Sjamie/* 2496191673Sjamie * Remove a prison reference. If that was the last reference, remove the 2497191673Sjamie * prison itself - but not in this context in case there are locks held. 2498191673Sjamie */ 249972786Srwatsonvoid 2500185029Spjdprison_free_locked(struct prison *pr) 250172786Srwatson{ 250272786Srwatson 2503185029Spjd mtx_assert(&pr->pr_mtx, MA_OWNED); 250472786Srwatson pr->pr_ref--; 250572786Srwatson if (pr->pr_ref == 0) { 2506168483Spjd mtx_unlock(&pr->pr_mtx); 2507124882Srwatson TASK_INIT(&pr->pr_task, 0, prison_complete, pr); 2508144660Sjeff taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 250987275Srwatson return; 251072786Srwatson } 251187275Srwatson mtx_unlock(&pr->pr_mtx); 251272786Srwatson} 251372786Srwatson 2514185029Spjdvoid 2515185029Spjdprison_free(struct prison *pr) 2516185029Spjd{ 2517185029Spjd 2518185029Spjd mtx_lock(&pr->pr_mtx); 2519185029Spjd prison_free_locked(pr); 2520185029Spjd} 2521185029Spjd 2522124882Srwatsonstatic void 2523124882Srwatsonprison_complete(void *context, int pending) 2524124882Srwatson{ 2525191673Sjamie 2526191673Sjamie prison_deref((struct prison *)context, 0); 2527191673Sjamie} 2528191673Sjamie 2529191673Sjamie/* 2530191673Sjamie * Remove a prison reference (usually). This internal version assumes no 2531191673Sjamie * mutexes are held, except perhaps the prison itself. If there are no more 2532191673Sjamie * references, release and delist the prison. On completion, the prison lock 2533191673Sjamie * and the allprison lock are both unlocked. 2534191673Sjamie */ 2535191673Sjamiestatic void 2536191673Sjamieprison_deref(struct prison *pr, int flags) 2537191673Sjamie{ 2538192895Sjamie struct prison *ppr, *tpr; 2539124882Srwatson 2540191673Sjamie if (!(flags & PD_LOCKED)) 2541191673Sjamie mtx_lock(&pr->pr_mtx); 2542225191Sjamie for (;;) { 2543225191Sjamie if (flags & PD_DEUREF) { 2544225191Sjamie pr->pr_uref--; 2545225191Sjamie KASSERT(prison0.pr_uref != 0, ("prison0 pr_uref=0")); 2546192895Sjamie } 2547192895Sjamie if (flags & PD_DEREF) 2548192895Sjamie pr->pr_ref--; 2549192895Sjamie /* If the prison still has references, nothing else to do. */ 2550192895Sjamie if (pr->pr_ref > 0) { 2551192895Sjamie mtx_unlock(&pr->pr_mtx); 2552192895Sjamie if (flags & PD_LIST_SLOCKED) 2553192895Sjamie sx_sunlock(&allprison_lock); 2554192895Sjamie else if (flags & PD_LIST_XLOCKED) 2555192895Sjamie sx_xunlock(&allprison_lock); 2556192895Sjamie return; 2557191673Sjamie } 2558191673Sjamie 2559192895Sjamie mtx_unlock(&pr->pr_mtx); 2560192895Sjamie if (flags & PD_LIST_SLOCKED) { 2561192895Sjamie if (!sx_try_upgrade(&allprison_lock)) { 2562192895Sjamie sx_sunlock(&allprison_lock); 2563192895Sjamie sx_xlock(&allprison_lock); 2564192895Sjamie } 2565192895Sjamie } else if (!(flags & PD_LIST_XLOCKED)) 2566192895Sjamie sx_xlock(&allprison_lock); 2567168489Spjd 2568192895Sjamie TAILQ_REMOVE(&allprison, pr, pr_list); 2569192895Sjamie LIST_REMOVE(pr, pr_sibling); 2570192895Sjamie ppr = pr->pr_parent; 2571192895Sjamie for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 2572194762Sjamie tpr->pr_childcount--; 2573196592Sjamie sx_xunlock(&allprison_lock); 2574192895Sjamie 2575194251Sjamie#ifdef VIMAGE 2576196505Szec if (pr->pr_vnet != ppr->pr_vnet) 2577194251Sjamie vnet_destroy(pr->pr_vnet); 2578194251Sjamie#endif 2579241896Skib if (pr->pr_root != NULL) 2580192895Sjamie vrele(pr->pr_root); 2581192895Sjamie mtx_destroy(&pr->pr_mtx); 2582191673Sjamie#ifdef INET 2583192895Sjamie free(pr->pr_ip4, M_PRISON); 2584191673Sjamie#endif 2585185435Sbz#ifdef INET6 2586192895Sjamie free(pr->pr_ip6, M_PRISON); 2587185435Sbz#endif 2588192895Sjamie if (pr->pr_cpuset != NULL) 2589192895Sjamie cpuset_rel(pr->pr_cpuset); 2590192895Sjamie osd_jail_exit(pr); 2591221362Strasz#ifdef RACCT 2592221362Strasz prison_racct_detach(pr); 2593220163Strasz#endif 2594192895Sjamie free(pr, M_PRISON); 2595192895Sjamie 2596192895Sjamie /* Removing a prison frees a reference on its parent. */ 2597192895Sjamie pr = ppr; 2598192895Sjamie mtx_lock(&pr->pr_mtx); 2599225191Sjamie flags = PD_DEREF | PD_DEUREF; 2600192895Sjamie } 2601124882Srwatson} 2602124882Srwatson 260372786Srwatsonvoid 2604185029Spjdprison_hold_locked(struct prison *pr) 260572786Srwatson{ 260672786Srwatson 2607185029Spjd mtx_assert(&pr->pr_mtx, MA_OWNED); 2608168489Spjd KASSERT(pr->pr_ref > 0, 2609191671Sjamie ("Trying to hold dead prison (jid=%d).", pr->pr_id)); 261072786Srwatson pr->pr_ref++; 2611185029Spjd} 2612185029Spjd 2613185029Spjdvoid 2614185029Spjdprison_hold(struct prison *pr) 2615185029Spjd{ 2616185029Spjd 2617185029Spjd mtx_lock(&pr->pr_mtx); 2618185029Spjd prison_hold_locked(pr); 261987275Srwatson mtx_unlock(&pr->pr_mtx); 262072786Srwatson} 262172786Srwatson 2622185435Sbzvoid 2623185435Sbzprison_proc_hold(struct prison *pr) 262487275Srwatson{ 262587275Srwatson 2626185435Sbz mtx_lock(&pr->pr_mtx); 2627191673Sjamie KASSERT(pr->pr_uref > 0, 2628191673Sjamie ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id)); 2629191673Sjamie pr->pr_uref++; 2630185435Sbz mtx_unlock(&pr->pr_mtx); 263187275Srwatson} 263287275Srwatson 2633185435Sbzvoid 2634185435Sbzprison_proc_free(struct prison *pr) 2635185435Sbz{ 2636185435Sbz 2637185435Sbz mtx_lock(&pr->pr_mtx); 2638191673Sjamie KASSERT(pr->pr_uref > 0, 2639191673Sjamie ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id)); 2640191673Sjamie prison_deref(pr, PD_DEUREF | PD_LOCKED); 2641185435Sbz} 2642185435Sbz 2643185435Sbz 2644185435Sbz#ifdef INET 2645185435Sbz/* 2646192895Sjamie * Restrict a prison's IP address list with its parent's, possibly replacing 2647192895Sjamie * it. Return true if the replacement buffer was used (or would have been). 2648192895Sjamie */ 2649192895Sjamiestatic int 2650192895Sjamieprison_restrict_ip4(struct prison *pr, struct in_addr *newip4) 2651192895Sjamie{ 2652192895Sjamie int ii, ij, used; 2653192895Sjamie struct prison *ppr; 2654192895Sjamie 2655192895Sjamie ppr = pr->pr_parent; 2656192895Sjamie if (!(pr->pr_flags & PR_IP4_USER)) { 2657192895Sjamie /* This has no user settings, so just copy the parent's list. */ 2658192895Sjamie if (pr->pr_ip4s < ppr->pr_ip4s) { 2659192895Sjamie /* 2660192895Sjamie * There's no room for the parent's list. Use the 2661192895Sjamie * new list buffer, which is assumed to be big enough 2662192895Sjamie * (if it was passed). If there's no buffer, try to 2663192895Sjamie * allocate one. 2664192895Sjamie */ 2665192895Sjamie used = 1; 2666192895Sjamie if (newip4 == NULL) { 2667192895Sjamie newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4), 2668192895Sjamie M_PRISON, M_NOWAIT); 2669192895Sjamie if (newip4 != NULL) 2670192895Sjamie used = 0; 2671192895Sjamie } 2672192895Sjamie if (newip4 != NULL) { 2673192895Sjamie bcopy(ppr->pr_ip4, newip4, 2674192895Sjamie ppr->pr_ip4s * sizeof(*newip4)); 2675192895Sjamie free(pr->pr_ip4, M_PRISON); 2676192895Sjamie pr->pr_ip4 = newip4; 2677192895Sjamie pr->pr_ip4s = ppr->pr_ip4s; 2678192895Sjamie } 2679192895Sjamie return (used); 2680192895Sjamie } 2681192895Sjamie pr->pr_ip4s = ppr->pr_ip4s; 2682192895Sjamie if (pr->pr_ip4s > 0) 2683192895Sjamie bcopy(ppr->pr_ip4, pr->pr_ip4, 2684192895Sjamie pr->pr_ip4s * sizeof(*newip4)); 2685192895Sjamie else if (pr->pr_ip4 != NULL) { 2686192895Sjamie free(pr->pr_ip4, M_PRISON); 2687192895Sjamie pr->pr_ip4 = NULL; 2688192895Sjamie } 2689195974Sjamie } else if (pr->pr_ip4s > 0) { 2690192895Sjamie /* Remove addresses that aren't in the parent. */ 2691192895Sjamie for (ij = 0; ij < ppr->pr_ip4s; ij++) 2692192895Sjamie if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 2693192895Sjamie break; 2694192895Sjamie if (ij < ppr->pr_ip4s) 2695192895Sjamie ii = 1; 2696192895Sjamie else { 2697192895Sjamie bcopy(pr->pr_ip4 + 1, pr->pr_ip4, 2698192895Sjamie --pr->pr_ip4s * sizeof(*pr->pr_ip4)); 2699192895Sjamie ii = 0; 2700192895Sjamie } 2701192895Sjamie for (ij = 1; ii < pr->pr_ip4s; ) { 2702192895Sjamie if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) { 2703192895Sjamie ii++; 2704192895Sjamie continue; 2705192895Sjamie } 2706192895Sjamie switch (ij >= ppr->pr_ip4s ? -1 : 2707192895Sjamie qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) { 2708192895Sjamie case -1: 2709192895Sjamie bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii, 2710192895Sjamie (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4)); 2711192895Sjamie break; 2712192895Sjamie case 0: 2713192895Sjamie ii++; 2714192895Sjamie ij++; 2715192895Sjamie break; 2716192895Sjamie case 1: 2717192895Sjamie ij++; 2718192895Sjamie break; 2719192895Sjamie } 2720192895Sjamie } 2721192895Sjamie if (pr->pr_ip4s == 0) { 2722195870Sjamie pr->pr_flags |= PR_IP4_DISABLE; 2723192895Sjamie free(pr->pr_ip4, M_PRISON); 2724192895Sjamie pr->pr_ip4 = NULL; 2725192895Sjamie } 2726192895Sjamie } 2727192895Sjamie return (0); 2728192895Sjamie} 2729192895Sjamie 2730192895Sjamie/* 2731185435Sbz * Pass back primary IPv4 address of this jail. 2732185435Sbz * 2733192895Sjamie * If not restricted return success but do not alter the address. Caller has 2734192895Sjamie * to make sure to initialize it correctly (e.g. INADDR_ANY). 2735185435Sbz * 2736188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4. 2737188144Sjamie * Address returned in NBO. 2738185435Sbz */ 273946155Sphkint 2740187684Sbzprison_get_ip4(struct ucred *cred, struct in_addr *ia) 274146155Sphk{ 2742191673Sjamie struct prison *pr; 274346155Sphk 2744185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2745185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2746185435Sbz 2747192895Sjamie pr = cred->cr_prison; 2748192895Sjamie if (!(pr->pr_flags & PR_IP4)) 274946155Sphk return (0); 2750191673Sjamie mtx_lock(&pr->pr_mtx); 2751192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2752192895Sjamie mtx_unlock(&pr->pr_mtx); 2753192895Sjamie return (0); 2754192895Sjamie } 2755191673Sjamie if (pr->pr_ip4 == NULL) { 2756191673Sjamie mtx_unlock(&pr->pr_mtx); 2757188144Sjamie return (EAFNOSUPPORT); 2758191673Sjamie } 2759185435Sbz 2760191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2761191673Sjamie mtx_unlock(&pr->pr_mtx); 2762185435Sbz return (0); 2763185435Sbz} 2764185435Sbz 2765185435Sbz/* 2766202468Sbz * Return 1 if we should do proper source address selection or are not jailed. 2767202468Sbz * We will return 0 if we should bypass source address selection in favour 2768202468Sbz * of the primary jail IPv4 address. Only in this case *ia will be updated and 2769202468Sbz * returned in NBO. 2770202468Sbz * Return EAFNOSUPPORT, in case this jail does not allow IPv4. 2771202468Sbz */ 2772202468Sbzint 2773202468Sbzprison_saddrsel_ip4(struct ucred *cred, struct in_addr *ia) 2774202468Sbz{ 2775202468Sbz struct prison *pr; 2776202468Sbz struct in_addr lia; 2777202468Sbz int error; 2778202468Sbz 2779202468Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2780202468Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2781202468Sbz 2782202468Sbz if (!jailed(cred)) 2783202468Sbz return (1); 2784202468Sbz 2785202468Sbz pr = cred->cr_prison; 2786202468Sbz if (pr->pr_flags & PR_IP4_SADDRSEL) 2787202468Sbz return (1); 2788202468Sbz 2789202468Sbz lia.s_addr = INADDR_ANY; 2790202468Sbz error = prison_get_ip4(cred, &lia); 2791202468Sbz if (error) 2792202468Sbz return (error); 2793202468Sbz if (lia.s_addr == INADDR_ANY) 2794202468Sbz return (1); 2795202468Sbz 2796202468Sbz ia->s_addr = lia.s_addr; 2797202468Sbz return (0); 2798202468Sbz} 2799202468Sbz 2800202468Sbz/* 2801192895Sjamie * Return true if pr1 and pr2 have the same IPv4 address restrictions. 2802192895Sjamie */ 2803192895Sjamieint 2804192895Sjamieprison_equal_ip4(struct prison *pr1, struct prison *pr2) 2805192895Sjamie{ 2806192895Sjamie 2807192895Sjamie if (pr1 == pr2) 2808192895Sjamie return (1); 2809192895Sjamie 2810192895Sjamie /* 2811195974Sjamie * No need to lock since the PR_IP4_USER flag can't be altered for 2812195974Sjamie * existing prisons. 2813192895Sjamie */ 2814195945Sjamie while (pr1 != &prison0 && 2815195945Sjamie#ifdef VIMAGE 2816195945Sjamie !(pr1->pr_flags & PR_VNET) && 2817195945Sjamie#endif 2818195945Sjamie !(pr1->pr_flags & PR_IP4_USER)) 2819192895Sjamie pr1 = pr1->pr_parent; 2820195945Sjamie while (pr2 != &prison0 && 2821195945Sjamie#ifdef VIMAGE 2822195945Sjamie !(pr2->pr_flags & PR_VNET) && 2823195945Sjamie#endif 2824195945Sjamie !(pr2->pr_flags & PR_IP4_USER)) 2825192895Sjamie pr2 = pr2->pr_parent; 2826192895Sjamie return (pr1 == pr2); 2827192895Sjamie} 2828192895Sjamie 2829192895Sjamie/* 2830185435Sbz * Make sure our (source) address is set to something meaningful to this 2831185435Sbz * jail. 2832185435Sbz * 2833192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail, 2834192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 2835192895Sjamie * doesn't allow IPv4. Address passed in in NBO and returned in NBO. 2836185435Sbz */ 2837185435Sbzint 2838185435Sbzprison_local_ip4(struct ucred *cred, struct in_addr *ia) 2839185435Sbz{ 2840191673Sjamie struct prison *pr; 2841185435Sbz struct in_addr ia0; 2842191673Sjamie int error; 2843185435Sbz 2844185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2845185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2846185435Sbz 2847192895Sjamie pr = cred->cr_prison; 2848192895Sjamie if (!(pr->pr_flags & PR_IP4)) 284946155Sphk return (0); 2850191673Sjamie mtx_lock(&pr->pr_mtx); 2851192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2852192895Sjamie mtx_unlock(&pr->pr_mtx); 2853192895Sjamie return (0); 2854192895Sjamie } 2855191673Sjamie if (pr->pr_ip4 == NULL) { 2856191673Sjamie mtx_unlock(&pr->pr_mtx); 2857188144Sjamie return (EAFNOSUPPORT); 2858191673Sjamie } 2859185435Sbz 2860185435Sbz ia0.s_addr = ntohl(ia->s_addr); 2861185435Sbz if (ia0.s_addr == INADDR_LOOPBACK) { 2862191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2863191673Sjamie mtx_unlock(&pr->pr_mtx); 2864185435Sbz return (0); 286546155Sphk } 2866185435Sbz 2867188144Sjamie if (ia0.s_addr == INADDR_ANY) { 2868188144Sjamie /* 2869188144Sjamie * In case there is only 1 IPv4 address, bind directly. 2870188144Sjamie */ 2871191673Sjamie if (pr->pr_ip4s == 1) 2872191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2873191673Sjamie mtx_unlock(&pr->pr_mtx); 2874185435Sbz return (0); 2875185435Sbz } 2876185435Sbz 2877191673Sjamie error = _prison_check_ip4(pr, ia); 2878191673Sjamie mtx_unlock(&pr->pr_mtx); 2879191673Sjamie return (error); 2880185435Sbz} 2881185435Sbz 2882185435Sbz/* 2883185435Sbz * Rewrite destination address in case we will connect to loopback address. 2884185435Sbz * 2885188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4. 2886188144Sjamie * Address passed in in NBO and returned in NBO. 2887185435Sbz */ 2888185435Sbzint 2889185435Sbzprison_remote_ip4(struct ucred *cred, struct in_addr *ia) 2890185435Sbz{ 2891191673Sjamie struct prison *pr; 2892185435Sbz 2893185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2894185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2895185435Sbz 2896192895Sjamie pr = cred->cr_prison; 2897192895Sjamie if (!(pr->pr_flags & PR_IP4)) 2898185435Sbz return (0); 2899191673Sjamie mtx_lock(&pr->pr_mtx); 2900192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2901192895Sjamie mtx_unlock(&pr->pr_mtx); 2902192895Sjamie return (0); 2903192895Sjamie } 2904191673Sjamie if (pr->pr_ip4 == NULL) { 2905191673Sjamie mtx_unlock(&pr->pr_mtx); 2906188144Sjamie return (EAFNOSUPPORT); 2907191673Sjamie } 2908188144Sjamie 2909185435Sbz if (ntohl(ia->s_addr) == INADDR_LOOPBACK) { 2910191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2911191673Sjamie mtx_unlock(&pr->pr_mtx); 2912185435Sbz return (0); 2913185435Sbz } 2914185435Sbz 2915185435Sbz /* 2916185435Sbz * Return success because nothing had to be changed. 2917185435Sbz */ 2918191673Sjamie mtx_unlock(&pr->pr_mtx); 2919185435Sbz return (0); 2920185435Sbz} 2921185435Sbz 2922185435Sbz/* 2923188144Sjamie * Check if given address belongs to the jail referenced by cred/prison. 2924185435Sbz * 2925192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail, 2926192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 2927192895Sjamie * doesn't allow IPv4. Address passed in in NBO. 2928185435Sbz */ 2929185435Sbzstatic int 2930185435Sbz_prison_check_ip4(struct prison *pr, struct in_addr *ia) 2931185435Sbz{ 2932185435Sbz int i, a, z, d; 2933185435Sbz 2934185435Sbz /* 2935185435Sbz * Check the primary IP. 2936185435Sbz */ 2937185435Sbz if (pr->pr_ip4[0].s_addr == ia->s_addr) 2938188144Sjamie return (0); 2939185435Sbz 2940185435Sbz /* 2941185435Sbz * All the other IPs are sorted so we can do a binary search. 2942185435Sbz */ 2943185435Sbz a = 0; 2944185435Sbz z = pr->pr_ip4s - 2; 2945185435Sbz while (a <= z) { 2946185435Sbz i = (a + z) / 2; 2947185435Sbz d = qcmp_v4(&pr->pr_ip4[i+1], ia); 2948185435Sbz if (d > 0) 2949185435Sbz z = i - 1; 2950185435Sbz else if (d < 0) 2951185435Sbz a = i + 1; 295281114Srwatson else 2953188144Sjamie return (0); 2954185435Sbz } 2955188144Sjamie 2956188144Sjamie return (EADDRNOTAVAIL); 2957185435Sbz} 2958185435Sbz 2959185435Sbzint 2960185435Sbzprison_check_ip4(struct ucred *cred, struct in_addr *ia) 2961185435Sbz{ 2962191673Sjamie struct prison *pr; 2963191673Sjamie int error; 2964185435Sbz 2965185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2966185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2967185435Sbz 2968192895Sjamie pr = cred->cr_prison; 2969192895Sjamie if (!(pr->pr_flags & PR_IP4)) 2970188144Sjamie return (0); 2971191673Sjamie mtx_lock(&pr->pr_mtx); 2972192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2973192895Sjamie mtx_unlock(&pr->pr_mtx); 2974192895Sjamie return (0); 2975192895Sjamie } 2976191673Sjamie if (pr->pr_ip4 == NULL) { 2977191673Sjamie mtx_unlock(&pr->pr_mtx); 2978188144Sjamie return (EAFNOSUPPORT); 2979191673Sjamie } 2980185435Sbz 2981191673Sjamie error = _prison_check_ip4(pr, ia); 2982191673Sjamie mtx_unlock(&pr->pr_mtx); 2983191673Sjamie return (error); 2984185435Sbz} 2985185435Sbz#endif 2986185435Sbz 2987185435Sbz#ifdef INET6 2988192895Sjamiestatic int 2989192895Sjamieprison_restrict_ip6(struct prison *pr, struct in6_addr *newip6) 2990192895Sjamie{ 2991192895Sjamie int ii, ij, used; 2992192895Sjamie struct prison *ppr; 2993192895Sjamie 2994192895Sjamie ppr = pr->pr_parent; 2995192895Sjamie if (!(pr->pr_flags & PR_IP6_USER)) { 2996192895Sjamie /* This has no user settings, so just copy the parent's list. */ 2997192895Sjamie if (pr->pr_ip6s < ppr->pr_ip6s) { 2998192895Sjamie /* 2999192895Sjamie * There's no room for the parent's list. Use the 3000192895Sjamie * new list buffer, which is assumed to be big enough 3001192895Sjamie * (if it was passed). If there's no buffer, try to 3002192895Sjamie * allocate one. 3003192895Sjamie */ 3004192895Sjamie used = 1; 3005192895Sjamie if (newip6 == NULL) { 3006192895Sjamie newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6), 3007192895Sjamie M_PRISON, M_NOWAIT); 3008192895Sjamie if (newip6 != NULL) 3009192895Sjamie used = 0; 3010192895Sjamie } 3011192895Sjamie if (newip6 != NULL) { 3012192895Sjamie bcopy(ppr->pr_ip6, newip6, 3013192895Sjamie ppr->pr_ip6s * sizeof(*newip6)); 3014192895Sjamie free(pr->pr_ip6, M_PRISON); 3015192895Sjamie pr->pr_ip6 = newip6; 3016192895Sjamie pr->pr_ip6s = ppr->pr_ip6s; 3017192895Sjamie } 3018192895Sjamie return (used); 3019192895Sjamie } 3020192895Sjamie pr->pr_ip6s = ppr->pr_ip6s; 3021192895Sjamie if (pr->pr_ip6s > 0) 3022192895Sjamie bcopy(ppr->pr_ip6, pr->pr_ip6, 3023192895Sjamie pr->pr_ip6s * sizeof(*newip6)); 3024192895Sjamie else if (pr->pr_ip6 != NULL) { 3025192895Sjamie free(pr->pr_ip6, M_PRISON); 3026192895Sjamie pr->pr_ip6 = NULL; 3027192895Sjamie } 3028195974Sjamie } else if (pr->pr_ip6s > 0) { 3029192895Sjamie /* Remove addresses that aren't in the parent. */ 3030192895Sjamie for (ij = 0; ij < ppr->pr_ip6s; ij++) 3031192895Sjamie if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], 3032192895Sjamie &ppr->pr_ip6[ij])) 3033192895Sjamie break; 3034192895Sjamie if (ij < ppr->pr_ip6s) 3035192895Sjamie ii = 1; 3036192895Sjamie else { 3037192895Sjamie bcopy(pr->pr_ip6 + 1, pr->pr_ip6, 3038192895Sjamie --pr->pr_ip6s * sizeof(*pr->pr_ip6)); 3039192895Sjamie ii = 0; 3040192895Sjamie } 3041192895Sjamie for (ij = 1; ii < pr->pr_ip6s; ) { 3042192895Sjamie if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii], 3043192895Sjamie &ppr->pr_ip6[0])) { 3044192895Sjamie ii++; 3045192895Sjamie continue; 3046192895Sjamie } 3047192895Sjamie switch (ij >= ppr->pr_ip4s ? -1 : 3048192895Sjamie qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) { 3049192895Sjamie case -1: 3050192895Sjamie bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii, 3051192895Sjamie (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6)); 3052192895Sjamie break; 3053192895Sjamie case 0: 3054192895Sjamie ii++; 3055192895Sjamie ij++; 3056192895Sjamie break; 3057192895Sjamie case 1: 3058192895Sjamie ij++; 3059192895Sjamie break; 3060192895Sjamie } 3061192895Sjamie } 3062192895Sjamie if (pr->pr_ip6s == 0) { 3063195870Sjamie pr->pr_flags |= PR_IP6_DISABLE; 3064192895Sjamie free(pr->pr_ip6, M_PRISON); 3065192895Sjamie pr->pr_ip6 = NULL; 3066192895Sjamie } 3067192895Sjamie } 3068192895Sjamie return 0; 3069192895Sjamie} 3070192895Sjamie 3071185435Sbz/* 3072185435Sbz * Pass back primary IPv6 address for this jail. 3073185435Sbz * 3074192895Sjamie * If not restricted return success but do not alter the address. Caller has 3075192895Sjamie * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT). 3076185435Sbz * 3077188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6. 3078185435Sbz */ 3079185435Sbzint 3080187684Sbzprison_get_ip6(struct ucred *cred, struct in6_addr *ia6) 3081185435Sbz{ 3082191673Sjamie struct prison *pr; 3083185435Sbz 3084185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3085185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3086185435Sbz 3087192895Sjamie pr = cred->cr_prison; 3088192895Sjamie if (!(pr->pr_flags & PR_IP6)) 308981114Srwatson return (0); 3090191673Sjamie mtx_lock(&pr->pr_mtx); 3091192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3092192895Sjamie mtx_unlock(&pr->pr_mtx); 3093192895Sjamie return (0); 3094192895Sjamie } 3095191673Sjamie if (pr->pr_ip6 == NULL) { 3096191673Sjamie mtx_unlock(&pr->pr_mtx); 3097188144Sjamie return (EAFNOSUPPORT); 3098191673Sjamie } 3099188144Sjamie 3100191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3101191673Sjamie mtx_unlock(&pr->pr_mtx); 3102185435Sbz return (0); 3103185435Sbz} 3104185435Sbz 3105185435Sbz/* 3106202468Sbz * Return 1 if we should do proper source address selection or are not jailed. 3107202468Sbz * We will return 0 if we should bypass source address selection in favour 3108202468Sbz * of the primary jail IPv6 address. Only in this case *ia will be updated and 3109202468Sbz * returned in NBO. 3110202468Sbz * Return EAFNOSUPPORT, in case this jail does not allow IPv6. 3111202468Sbz */ 3112202468Sbzint 3113202468Sbzprison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia6) 3114202468Sbz{ 3115202468Sbz struct prison *pr; 3116202468Sbz struct in6_addr lia6; 3117202468Sbz int error; 3118202468Sbz 3119202468Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3120202468Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3121202468Sbz 3122202468Sbz if (!jailed(cred)) 3123202468Sbz return (1); 3124202468Sbz 3125202468Sbz pr = cred->cr_prison; 3126202468Sbz if (pr->pr_flags & PR_IP6_SADDRSEL) 3127202468Sbz return (1); 3128202468Sbz 3129202468Sbz lia6 = in6addr_any; 3130202468Sbz error = prison_get_ip6(cred, &lia6); 3131202468Sbz if (error) 3132202468Sbz return (error); 3133202468Sbz if (IN6_IS_ADDR_UNSPECIFIED(&lia6)) 3134202468Sbz return (1); 3135202468Sbz 3136202468Sbz bcopy(&lia6, ia6, sizeof(struct in6_addr)); 3137202468Sbz return (0); 3138202468Sbz} 3139202468Sbz 3140202468Sbz/* 3141192895Sjamie * Return true if pr1 and pr2 have the same IPv6 address restrictions. 3142192895Sjamie */ 3143192895Sjamieint 3144192895Sjamieprison_equal_ip6(struct prison *pr1, struct prison *pr2) 3145192895Sjamie{ 3146192895Sjamie 3147192895Sjamie if (pr1 == pr2) 3148192895Sjamie return (1); 3149192895Sjamie 3150195945Sjamie while (pr1 != &prison0 && 3151195945Sjamie#ifdef VIMAGE 3152195945Sjamie !(pr1->pr_flags & PR_VNET) && 3153195945Sjamie#endif 3154195945Sjamie !(pr1->pr_flags & PR_IP6_USER)) 3155192895Sjamie pr1 = pr1->pr_parent; 3156195945Sjamie while (pr2 != &prison0 && 3157195945Sjamie#ifdef VIMAGE 3158195945Sjamie !(pr2->pr_flags & PR_VNET) && 3159195945Sjamie#endif 3160195945Sjamie !(pr2->pr_flags & PR_IP6_USER)) 3161192895Sjamie pr2 = pr2->pr_parent; 3162192895Sjamie return (pr1 == pr2); 3163192895Sjamie} 3164192895Sjamie 3165192895Sjamie/* 3166185435Sbz * Make sure our (source) address is set to something meaningful to this jail. 3167185435Sbz * 3168185435Sbz * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0) 3169185435Sbz * when needed while binding. 3170185435Sbz * 3171192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail, 3172192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 3173192895Sjamie * doesn't allow IPv6. 3174185435Sbz */ 3175185435Sbzint 3176185435Sbzprison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only) 3177185435Sbz{ 3178191673Sjamie struct prison *pr; 3179191673Sjamie int error; 3180185435Sbz 3181185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3182185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3183185435Sbz 3184192895Sjamie pr = cred->cr_prison; 3185192895Sjamie if (!(pr->pr_flags & PR_IP6)) 3186185435Sbz return (0); 3187191673Sjamie mtx_lock(&pr->pr_mtx); 3188192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3189192895Sjamie mtx_unlock(&pr->pr_mtx); 3190192895Sjamie return (0); 3191192895Sjamie } 3192191673Sjamie if (pr->pr_ip6 == NULL) { 3193191673Sjamie mtx_unlock(&pr->pr_mtx); 3194188144Sjamie return (EAFNOSUPPORT); 3195191673Sjamie } 3196188144Sjamie 3197185435Sbz if (IN6_IS_ADDR_LOOPBACK(ia6)) { 3198191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3199191673Sjamie mtx_unlock(&pr->pr_mtx); 3200185435Sbz return (0); 320181114Srwatson } 3202185435Sbz 3203188144Sjamie if (IN6_IS_ADDR_UNSPECIFIED(ia6)) { 3204188144Sjamie /* 3205188144Sjamie * In case there is only 1 IPv6 address, and v6only is true, 3206188144Sjamie * then bind directly. 3207188144Sjamie */ 3208191673Sjamie if (v6only != 0 && pr->pr_ip6s == 1) 3209191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3210191673Sjamie mtx_unlock(&pr->pr_mtx); 3211185435Sbz return (0); 3212185435Sbz } 3213188144Sjamie 3214191673Sjamie error = _prison_check_ip6(pr, ia6); 3215191673Sjamie mtx_unlock(&pr->pr_mtx); 3216191673Sjamie return (error); 3217185435Sbz} 3218185435Sbz 3219185435Sbz/* 3220185435Sbz * Rewrite destination address in case we will connect to loopback address. 3221185435Sbz * 3222188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6. 3223185435Sbz */ 3224185435Sbzint 3225185435Sbzprison_remote_ip6(struct ucred *cred, struct in6_addr *ia6) 3226185435Sbz{ 3227191673Sjamie struct prison *pr; 3228185435Sbz 3229185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3230185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3231185435Sbz 3232192895Sjamie pr = cred->cr_prison; 3233192895Sjamie if (!(pr->pr_flags & PR_IP6)) 3234185435Sbz return (0); 3235191673Sjamie mtx_lock(&pr->pr_mtx); 3236192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3237192895Sjamie mtx_unlock(&pr->pr_mtx); 3238192895Sjamie return (0); 3239192895Sjamie } 3240191673Sjamie if (pr->pr_ip6 == NULL) { 3241191673Sjamie mtx_unlock(&pr->pr_mtx); 3242188144Sjamie return (EAFNOSUPPORT); 3243191673Sjamie } 3244188144Sjamie 3245185435Sbz if (IN6_IS_ADDR_LOOPBACK(ia6)) { 3246191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3247191673Sjamie mtx_unlock(&pr->pr_mtx); 3248185435Sbz return (0); 3249185435Sbz } 3250185435Sbz 3251185435Sbz /* 3252185435Sbz * Return success because nothing had to be changed. 3253185435Sbz */ 3254191673Sjamie mtx_unlock(&pr->pr_mtx); 325546155Sphk return (0); 325646155Sphk} 325746155Sphk 3258185435Sbz/* 3259188144Sjamie * Check if given address belongs to the jail referenced by cred/prison. 3260185435Sbz * 3261192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail, 3262192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 3263192895Sjamie * doesn't allow IPv6. 3264185435Sbz */ 3265185435Sbzstatic int 3266185435Sbz_prison_check_ip6(struct prison *pr, struct in6_addr *ia6) 326746155Sphk{ 3268185435Sbz int i, a, z, d; 326946155Sphk 3270185435Sbz /* 3271185435Sbz * Check the primary IP. 3272185435Sbz */ 3273185435Sbz if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6)) 3274188144Sjamie return (0); 3275185435Sbz 3276185435Sbz /* 3277185435Sbz * All the other IPs are sorted so we can do a binary search. 3278185435Sbz */ 3279185435Sbz a = 0; 3280185435Sbz z = pr->pr_ip6s - 2; 3281185435Sbz while (a <= z) { 3282185435Sbz i = (a + z) / 2; 3283185435Sbz d = qcmp_v6(&pr->pr_ip6[i+1], ia6); 3284185435Sbz if (d > 0) 3285185435Sbz z = i - 1; 3286185435Sbz else if (d < 0) 3287185435Sbz a = i + 1; 328846155Sphk else 3289188144Sjamie return (0); 329046155Sphk } 3291188144Sjamie 3292188144Sjamie return (EADDRNOTAVAIL); 329346155Sphk} 329446155Sphk 329546155Sphkint 3296185435Sbzprison_check_ip6(struct ucred *cred, struct in6_addr *ia6) 3297185435Sbz{ 3298191673Sjamie struct prison *pr; 3299191673Sjamie int error; 3300185435Sbz 3301185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3302185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3303185435Sbz 3304192895Sjamie pr = cred->cr_prison; 3305192895Sjamie if (!(pr->pr_flags & PR_IP6)) 3306188144Sjamie return (0); 3307191673Sjamie mtx_lock(&pr->pr_mtx); 3308192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3309192895Sjamie mtx_unlock(&pr->pr_mtx); 3310192895Sjamie return (0); 3311192895Sjamie } 3312191673Sjamie if (pr->pr_ip6 == NULL) { 3313191673Sjamie mtx_unlock(&pr->pr_mtx); 3314188144Sjamie return (EAFNOSUPPORT); 3315191673Sjamie } 3316185435Sbz 3317191673Sjamie error = _prison_check_ip6(pr, ia6); 3318191673Sjamie mtx_unlock(&pr->pr_mtx); 3319191673Sjamie return (error); 3320185435Sbz} 3321185435Sbz#endif 3322185435Sbz 3323185435Sbz/* 3324188146Sjamie * Check if a jail supports the given address family. 3325188146Sjamie * 3326188146Sjamie * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT 3327188146Sjamie * if not. 3328188146Sjamie */ 3329188146Sjamieint 3330188146Sjamieprison_check_af(struct ucred *cred, int af) 3331188146Sjamie{ 3332192895Sjamie struct prison *pr; 3333188146Sjamie int error; 3334188146Sjamie 3335188146Sjamie KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3336188146Sjamie 3337192895Sjamie pr = cred->cr_prison; 3338194923Sjamie#ifdef VIMAGE 3339194915Sjamie /* Prisons with their own network stack are not limited. */ 3340200473Sbz if (prison_owns_vnet(cred)) 3341194915Sjamie return (0); 3342194923Sjamie#endif 3343194915Sjamie 3344188146Sjamie error = 0; 3345188146Sjamie switch (af) 3346188146Sjamie { 3347188146Sjamie#ifdef INET 3348188146Sjamie case AF_INET: 3349192895Sjamie if (pr->pr_flags & PR_IP4) 3350192895Sjamie { 3351192895Sjamie mtx_lock(&pr->pr_mtx); 3352192895Sjamie if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL) 3353192895Sjamie error = EAFNOSUPPORT; 3354192895Sjamie mtx_unlock(&pr->pr_mtx); 3355192895Sjamie } 3356188146Sjamie break; 3357188146Sjamie#endif 3358188146Sjamie#ifdef INET6 3359188146Sjamie case AF_INET6: 3360192895Sjamie if (pr->pr_flags & PR_IP6) 3361192895Sjamie { 3362192895Sjamie mtx_lock(&pr->pr_mtx); 3363192895Sjamie if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL) 3364192895Sjamie error = EAFNOSUPPORT; 3365192895Sjamie mtx_unlock(&pr->pr_mtx); 3366192895Sjamie } 3367188146Sjamie break; 3368188146Sjamie#endif 3369188146Sjamie case AF_LOCAL: 3370188146Sjamie case AF_ROUTE: 3371188146Sjamie break; 3372188146Sjamie default: 3373192895Sjamie if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF)) 3374188146Sjamie error = EAFNOSUPPORT; 3375188146Sjamie } 3376188146Sjamie return (error); 3377188146Sjamie} 3378188146Sjamie 3379188146Sjamie/* 3380185435Sbz * Check if given address belongs to the jail referenced by cred (wrapper to 3381185435Sbz * prison_check_ip[46]). 3382185435Sbz * 3383192895Sjamie * Returns 0 if jail doesn't restrict the address family or if address belongs 3384192895Sjamie * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if 3385192895Sjamie * the jail doesn't allow the address family. IPv4 Address passed in in NBO. 3386185435Sbz */ 3387185435Sbzint 338872786Srwatsonprison_if(struct ucred *cred, struct sockaddr *sa) 338946155Sphk{ 3390185435Sbz#ifdef INET 3391114168Smike struct sockaddr_in *sai; 3392185435Sbz#endif 3393185435Sbz#ifdef INET6 3394185435Sbz struct sockaddr_in6 *sai6; 3395185435Sbz#endif 3396188144Sjamie int error; 339746155Sphk 3398185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3399185435Sbz KASSERT(sa != NULL, ("%s: sa is NULL", __func__)); 3400185435Sbz 3401200473Sbz#ifdef VIMAGE 3402200473Sbz if (prison_owns_vnet(cred)) 3403200473Sbz return (0); 3404200473Sbz#endif 3405200473Sbz 3406188144Sjamie error = 0; 3407188144Sjamie switch (sa->sa_family) 3408185435Sbz { 3409185435Sbz#ifdef INET 3410185435Sbz case AF_INET: 3411185435Sbz sai = (struct sockaddr_in *)sa; 3412188144Sjamie error = prison_check_ip4(cred, &sai->sin_addr); 3413185435Sbz break; 3414185435Sbz#endif 3415185435Sbz#ifdef INET6 3416185435Sbz case AF_INET6: 3417185435Sbz sai6 = (struct sockaddr_in6 *)sa; 3418188144Sjamie error = prison_check_ip6(cred, &sai6->sin6_addr); 3419185435Sbz break; 3420185435Sbz#endif 3421185435Sbz default: 3422192895Sjamie if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF)) 3423188144Sjamie error = EAFNOSUPPORT; 3424185435Sbz } 3425188144Sjamie return (error); 342646155Sphk} 342772786Srwatson 342872786Srwatson/* 342972786Srwatson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH. 343072786Srwatson */ 343172786Srwatsonint 3432114168Smikeprison_check(struct ucred *cred1, struct ucred *cred2) 343372786Srwatson{ 343472786Srwatson 3435192895Sjamie return ((cred1->cr_prison == cred2->cr_prison || 3436192895Sjamie prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH); 3437192895Sjamie} 343872786Srwatson 3439192895Sjamie/* 3440192895Sjamie * Return 1 if p2 is a child of p1, otherwise 0. 3441192895Sjamie */ 3442192895Sjamieint 3443192895Sjamieprison_ischild(struct prison *pr1, struct prison *pr2) 3444192895Sjamie{ 3445192895Sjamie 3446192895Sjamie for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent) 3447192895Sjamie if (pr1 == pr2) 3448192895Sjamie return (1); 344972786Srwatson return (0); 345072786Srwatson} 345172786Srwatson 345272786Srwatson/* 345372786Srwatson * Return 1 if the passed credential is in a jail, otherwise 0. 345472786Srwatson */ 345572786Srwatsonint 3456114168Smikejailed(struct ucred *cred) 345772786Srwatson{ 345872786Srwatson 3459192895Sjamie return (cred->cr_prison != &prison0); 346072786Srwatson} 346191384Srobert 346291384Srobert/* 3463200473Sbz * Return 1 if the passed credential is in a jail and that jail does not 3464200473Sbz * have its own virtual network stack, otherwise 0. 3465200473Sbz */ 3466200473Sbzint 3467200473Sbzjailed_without_vnet(struct ucred *cred) 3468200473Sbz{ 3469200473Sbz 3470200473Sbz if (!jailed(cred)) 3471200473Sbz return (0); 3472200473Sbz#ifdef VIMAGE 3473200473Sbz if (prison_owns_vnet(cred)) 3474200473Sbz return (0); 3475200473Sbz#endif 3476200473Sbz 3477200473Sbz return (1); 3478200473Sbz} 3479200473Sbz 3480200473Sbz/* 3481194090Sjamie * Return the correct hostname (domainname, et al) for the passed credential. 348291384Srobert */ 348391391Srobertvoid 3484114168Smikegetcredhostname(struct ucred *cred, char *buf, size_t size) 348591384Srobert{ 3486193066Sjamie struct prison *pr; 348791384Srobert 3488194090Sjamie /* 3489194090Sjamie * A NULL credential can be used to shortcut to the physical 3490194090Sjamie * system's hostname. 3491194090Sjamie */ 3492193066Sjamie pr = (cred != NULL) ? cred->cr_prison : &prison0; 3493193066Sjamie mtx_lock(&pr->pr_mtx); 3494194118Sjamie strlcpy(buf, pr->pr_hostname, size); 3495193066Sjamie mtx_unlock(&pr->pr_mtx); 349691384Srobert} 3497113275Smike 3498194090Sjamievoid 3499194090Sjamiegetcreddomainname(struct ucred *cred, char *buf, size_t size) 3500194090Sjamie{ 3501194090Sjamie 3502194090Sjamie mtx_lock(&cred->cr_prison->pr_mtx); 3503194118Sjamie strlcpy(buf, cred->cr_prison->pr_domainname, size); 3504194090Sjamie mtx_unlock(&cred->cr_prison->pr_mtx); 3505194090Sjamie} 3506194090Sjamie 3507194090Sjamievoid 3508194090Sjamiegetcredhostuuid(struct ucred *cred, char *buf, size_t size) 3509194090Sjamie{ 3510194090Sjamie 3511194090Sjamie mtx_lock(&cred->cr_prison->pr_mtx); 3512194118Sjamie strlcpy(buf, cred->cr_prison->pr_hostuuid, size); 3513194090Sjamie mtx_unlock(&cred->cr_prison->pr_mtx); 3514194090Sjamie} 3515194090Sjamie 3516194090Sjamievoid 3517194090Sjamiegetcredhostid(struct ucred *cred, unsigned long *hostid) 3518194090Sjamie{ 3519194090Sjamie 3520194090Sjamie mtx_lock(&cred->cr_prison->pr_mtx); 3521194090Sjamie *hostid = cred->cr_prison->pr_hostid; 3522194090Sjamie mtx_unlock(&cred->cr_prison->pr_mtx); 3523194090Sjamie} 3524194090Sjamie 3525196176Sbz#ifdef VIMAGE 3526125804Srwatson/* 3527196176Sbz * Determine whether the prison represented by cred owns 3528196176Sbz * its vnet rather than having it inherited. 3529196176Sbz * 3530196176Sbz * Returns 1 in case the prison owns the vnet, 0 otherwise. 3531196176Sbz */ 3532196176Sbzint 3533196176Sbzprison_owns_vnet(struct ucred *cred) 3534196176Sbz{ 3535196176Sbz 3536196176Sbz /* 3537196176Sbz * vnets cannot be added/removed after jail creation, 3538196176Sbz * so no need to lock here. 3539196176Sbz */ 3540196176Sbz return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0); 3541196176Sbz} 3542196176Sbz#endif 3543196176Sbz 3544196176Sbz/* 3545147185Spjd * Determine whether the subject represented by cred can "see" 3546147185Spjd * status of a mount point. 3547147185Spjd * Returns: 0 for permitted, ENOENT otherwise. 3548147185Spjd * XXX: This function should be called cr_canseemount() and should be 3549147185Spjd * placed in kern_prot.c. 3550125804Srwatson */ 3551125804Srwatsonint 3552147185Spjdprison_canseemount(struct ucred *cred, struct mount *mp) 3553125804Srwatson{ 3554147185Spjd struct prison *pr; 3555147185Spjd struct statfs *sp; 3556147185Spjd size_t len; 3557125804Srwatson 3558192895Sjamie pr = cred->cr_prison; 3559192895Sjamie if (pr->pr_enforce_statfs == 0) 3560147185Spjd return (0); 3561147185Spjd if (pr->pr_root->v_mount == mp) 3562147185Spjd return (0); 3563192895Sjamie if (pr->pr_enforce_statfs == 2) 3564147185Spjd return (ENOENT); 3565147185Spjd /* 3566147185Spjd * If jail's chroot directory is set to "/" we should be able to see 3567147185Spjd * all mount-points from inside a jail. 3568147185Spjd * This is ugly check, but this is the only situation when jail's 3569147185Spjd * directory ends with '/'. 3570147185Spjd */ 3571147185Spjd if (strcmp(pr->pr_path, "/") == 0) 3572147185Spjd return (0); 3573147185Spjd len = strlen(pr->pr_path); 3574147185Spjd sp = &mp->mnt_stat; 3575147185Spjd if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0) 3576147185Spjd return (ENOENT); 3577147185Spjd /* 3578147185Spjd * Be sure that we don't have situation where jail's root directory 3579147185Spjd * is "/some/path" and mount point is "/some/pathpath". 3580147185Spjd */ 3581147185Spjd if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/') 3582147185Spjd return (ENOENT); 3583147185Spjd return (0); 3584147185Spjd} 3585147185Spjd 3586147185Spjdvoid 3587147185Spjdprison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp) 3588147185Spjd{ 3589147185Spjd char jpath[MAXPATHLEN]; 3590147185Spjd struct prison *pr; 3591147185Spjd size_t len; 3592147185Spjd 3593192895Sjamie pr = cred->cr_prison; 3594192895Sjamie if (pr->pr_enforce_statfs == 0) 3595147185Spjd return; 3596147185Spjd if (prison_canseemount(cred, mp) != 0) { 3597147185Spjd bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3598147185Spjd strlcpy(sp->f_mntonname, "[restricted]", 3599147185Spjd sizeof(sp->f_mntonname)); 3600147185Spjd return; 3601125804Srwatson } 3602147185Spjd if (pr->pr_root->v_mount == mp) { 3603147185Spjd /* 3604147185Spjd * Clear current buffer data, so we are sure nothing from 3605147185Spjd * the valid path left there. 3606147185Spjd */ 3607147185Spjd bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3608147185Spjd *sp->f_mntonname = '/'; 3609147185Spjd return; 3610147185Spjd } 3611147185Spjd /* 3612147185Spjd * If jail's chroot directory is set to "/" we should be able to see 3613147185Spjd * all mount-points from inside a jail. 3614147185Spjd */ 3615147185Spjd if (strcmp(pr->pr_path, "/") == 0) 3616147185Spjd return; 3617147185Spjd len = strlen(pr->pr_path); 3618147185Spjd strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath)); 3619147185Spjd /* 3620147185Spjd * Clear current buffer data, so we are sure nothing from 3621147185Spjd * the valid path left there. 3622147185Spjd */ 3623147185Spjd bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3624147185Spjd if (*jpath == '\0') { 3625147185Spjd /* Should never happen. */ 3626147185Spjd *sp->f_mntonname = '/'; 3627147185Spjd } else { 3628147185Spjd strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname)); 3629147185Spjd } 3630125804Srwatson} 3631125804Srwatson 3632164032Srwatson/* 3633164032Srwatson * Check with permission for a specific privilege is granted within jail. We 3634164032Srwatson * have a specific list of accepted privileges; the rest are denied. 3635164032Srwatson */ 3636164032Srwatsonint 3637164032Srwatsonprison_priv_check(struct ucred *cred, int priv) 3638164032Srwatson{ 3639164032Srwatson 3640164032Srwatson if (!jailed(cred)) 3641164032Srwatson return (0); 3642164032Srwatson 3643194915Sjamie#ifdef VIMAGE 3644194915Sjamie /* 3645194915Sjamie * Privileges specific to prisons with a virtual network stack. 3646194915Sjamie * There might be a duplicate entry here in case the privilege 3647194915Sjamie * is only granted conditionally in the legacy jail case. 3648194915Sjamie */ 3649164032Srwatson switch (priv) { 3650194915Sjamie#ifdef notyet 3651194915Sjamie /* 3652194915Sjamie * NFS-specific privileges. 3653194915Sjamie */ 3654194915Sjamie case PRIV_NFS_DAEMON: 3655194915Sjamie case PRIV_NFS_LOCKD: 3656194915Sjamie#endif 3657194915Sjamie /* 3658194915Sjamie * Network stack privileges. 3659194915Sjamie */ 3660194915Sjamie case PRIV_NET_BRIDGE: 3661194915Sjamie case PRIV_NET_GRE: 3662194915Sjamie case PRIV_NET_BPF: 3663194915Sjamie case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */ 3664194915Sjamie case PRIV_NET_ROUTE: 3665194915Sjamie case PRIV_NET_TAP: 3666194915Sjamie case PRIV_NET_SETIFMTU: 3667194915Sjamie case PRIV_NET_SETIFFLAGS: 3668194915Sjamie case PRIV_NET_SETIFCAP: 3669203052Sdelphij case PRIV_NET_SETIFDESCR: 3670194915Sjamie case PRIV_NET_SETIFNAME : 3671194915Sjamie case PRIV_NET_SETIFMETRIC: 3672194915Sjamie case PRIV_NET_SETIFPHYS: 3673194915Sjamie case PRIV_NET_SETIFMAC: 3674194915Sjamie case PRIV_NET_ADDMULTI: 3675194915Sjamie case PRIV_NET_DELMULTI: 3676194915Sjamie case PRIV_NET_HWIOCTL: 3677194915Sjamie case PRIV_NET_SETLLADDR: 3678194915Sjamie case PRIV_NET_ADDIFGROUP: 3679194915Sjamie case PRIV_NET_DELIFGROUP: 3680194915Sjamie case PRIV_NET_IFCREATE: 3681194915Sjamie case PRIV_NET_IFDESTROY: 3682194915Sjamie case PRIV_NET_ADDIFADDR: 3683194915Sjamie case PRIV_NET_DELIFADDR: 3684194915Sjamie case PRIV_NET_LAGG: 3685194915Sjamie case PRIV_NET_GIF: 3686194915Sjamie case PRIV_NET_SETIFVNET: 3687223735Sbz case PRIV_NET_SETIFFIB: 3688164032Srwatson 3689164032Srwatson /* 3690194915Sjamie * 802.11-related privileges. 3691194915Sjamie */ 3692194915Sjamie case PRIV_NET80211_GETKEY: 3693194915Sjamie#ifdef notyet 3694194915Sjamie case PRIV_NET80211_MANAGE: /* XXX-BZ discuss with sam@ */ 3695194915Sjamie#endif 3696194915Sjamie 3697194915Sjamie#ifdef notyet 3698194915Sjamie /* 3699194915Sjamie * AppleTalk privileges. 3700194915Sjamie */ 3701194915Sjamie case PRIV_NETATALK_RESERVEDPORT: 3702194915Sjamie 3703194915Sjamie /* 3704194915Sjamie * ATM privileges. 3705194915Sjamie */ 3706194915Sjamie case PRIV_NETATM_CFG: 3707194915Sjamie case PRIV_NETATM_ADD: 3708194915Sjamie case PRIV_NETATM_DEL: 3709194915Sjamie case PRIV_NETATM_SET: 3710194915Sjamie 3711194915Sjamie /* 3712194915Sjamie * Bluetooth privileges. 3713194915Sjamie */ 3714194915Sjamie case PRIV_NETBLUETOOTH_RAW: 3715194915Sjamie#endif 3716194915Sjamie 3717194915Sjamie /* 3718194915Sjamie * Netgraph and netgraph module privileges. 3719194915Sjamie */ 3720194915Sjamie case PRIV_NETGRAPH_CONTROL: 3721194915Sjamie#ifdef notyet 3722194915Sjamie case PRIV_NETGRAPH_TTY: 3723194915Sjamie#endif 3724194915Sjamie 3725194915Sjamie /* 3726194915Sjamie * IPv4 and IPv6 privileges. 3727194915Sjamie */ 3728194915Sjamie case PRIV_NETINET_IPFW: 3729194915Sjamie case PRIV_NETINET_DIVERT: 3730194915Sjamie case PRIV_NETINET_PF: 3731194915Sjamie case PRIV_NETINET_DUMMYNET: 3732194915Sjamie case PRIV_NETINET_CARP: 3733194915Sjamie case PRIV_NETINET_MROUTE: 3734194915Sjamie case PRIV_NETINET_RAW: 3735194915Sjamie case PRIV_NETINET_ADDRCTRL6: 3736194915Sjamie case PRIV_NETINET_ND6: 3737194915Sjamie case PRIV_NETINET_SCOPE6: 3738194915Sjamie case PRIV_NETINET_ALIFETIME6: 3739194915Sjamie case PRIV_NETINET_IPSEC: 3740194915Sjamie case PRIV_NETINET_BINDANY: 3741194915Sjamie 3742194915Sjamie#ifdef notyet 3743194915Sjamie /* 3744194915Sjamie * IPX/SPX privileges. 3745194915Sjamie */ 3746194915Sjamie case PRIV_NETIPX_RESERVEDPORT: 3747194915Sjamie case PRIV_NETIPX_RAW: 3748194915Sjamie 3749194915Sjamie /* 3750194915Sjamie * NCP privileges. 3751194915Sjamie */ 3752194915Sjamie case PRIV_NETNCP: 3753194915Sjamie 3754194915Sjamie /* 3755194915Sjamie * SMB privileges. 3756194915Sjamie */ 3757194915Sjamie case PRIV_NETSMB: 3758194915Sjamie#endif 3759194915Sjamie 3760194915Sjamie /* 3761194915Sjamie * No default: or deny here. 3762194915Sjamie * In case of no permit fall through to next switch(). 3763194915Sjamie */ 3764194915Sjamie if (cred->cr_prison->pr_flags & PR_VNET) 3765194915Sjamie return (0); 3766194915Sjamie } 3767194915Sjamie#endif /* VIMAGE */ 3768194915Sjamie 3769194915Sjamie switch (priv) { 3770194915Sjamie 3771194915Sjamie /* 3772164032Srwatson * Allow ktrace privileges for root in jail. 3773164032Srwatson */ 3774164032Srwatson case PRIV_KTRACE: 3775164032Srwatson 3776166827Srwatson#if 0 3777164032Srwatson /* 3778164032Srwatson * Allow jailed processes to configure audit identity and 3779164032Srwatson * submit audit records (login, etc). In the future we may 3780164032Srwatson * want to further refine the relationship between audit and 3781164032Srwatson * jail. 3782164032Srwatson */ 3783164032Srwatson case PRIV_AUDIT_GETAUDIT: 3784164032Srwatson case PRIV_AUDIT_SETAUDIT: 3785164032Srwatson case PRIV_AUDIT_SUBMIT: 3786166827Srwatson#endif 3787164032Srwatson 3788164032Srwatson /* 3789164032Srwatson * Allow jailed processes to manipulate process UNIX 3790164032Srwatson * credentials in any way they see fit. 3791164032Srwatson */ 3792164032Srwatson case PRIV_CRED_SETUID: 3793164032Srwatson case PRIV_CRED_SETEUID: 3794164032Srwatson case PRIV_CRED_SETGID: 3795164032Srwatson case PRIV_CRED_SETEGID: 3796164032Srwatson case PRIV_CRED_SETGROUPS: 3797164032Srwatson case PRIV_CRED_SETREUID: 3798164032Srwatson case PRIV_CRED_SETREGID: 3799164032Srwatson case PRIV_CRED_SETRESUID: 3800164032Srwatson case PRIV_CRED_SETRESGID: 3801164032Srwatson 3802164032Srwatson /* 3803164032Srwatson * Jail implements visibility constraints already, so allow 3804164032Srwatson * jailed root to override uid/gid-based constraints. 3805164032Srwatson */ 3806164032Srwatson case PRIV_SEEOTHERGIDS: 3807164032Srwatson case PRIV_SEEOTHERUIDS: 3808164032Srwatson 3809164032Srwatson /* 3810164032Srwatson * Jail implements inter-process debugging limits already, so 3811164032Srwatson * allow jailed root various debugging privileges. 3812164032Srwatson */ 3813164032Srwatson case PRIV_DEBUG_DIFFCRED: 3814164032Srwatson case PRIV_DEBUG_SUGID: 3815164032Srwatson case PRIV_DEBUG_UNPRIV: 3816164032Srwatson 3817164032Srwatson /* 3818164032Srwatson * Allow jail to set various resource limits and login 3819164032Srwatson * properties, and for now, exceed process resource limits. 3820164032Srwatson */ 3821164032Srwatson case PRIV_PROC_LIMIT: 3822164032Srwatson case PRIV_PROC_SETLOGIN: 3823164032Srwatson case PRIV_PROC_SETRLIMIT: 3824164032Srwatson 3825164032Srwatson /* 3826164032Srwatson * System V and POSIX IPC privileges are granted in jail. 3827164032Srwatson */ 3828164032Srwatson case PRIV_IPC_READ: 3829164032Srwatson case PRIV_IPC_WRITE: 3830164032Srwatson case PRIV_IPC_ADMIN: 3831164032Srwatson case PRIV_IPC_MSGSIZE: 3832164032Srwatson case PRIV_MQ_ADMIN: 3833164032Srwatson 3834164032Srwatson /* 3835192895Sjamie * Jail operations within a jail work on child jails. 3836192895Sjamie */ 3837192895Sjamie case PRIV_JAIL_ATTACH: 3838192895Sjamie case PRIV_JAIL_SET: 3839192895Sjamie case PRIV_JAIL_REMOVE: 3840192895Sjamie 3841192895Sjamie /* 3842164032Srwatson * Jail implements its own inter-process limits, so allow 3843164032Srwatson * root processes in jail to change scheduling on other 3844164032Srwatson * processes in the same jail. Likewise for signalling. 3845164032Srwatson */ 3846164032Srwatson case PRIV_SCHED_DIFFCRED: 3847185435Sbz case PRIV_SCHED_CPUSET: 3848164032Srwatson case PRIV_SIGNAL_DIFFCRED: 3849164032Srwatson case PRIV_SIGNAL_SUGID: 3850164032Srwatson 3851164032Srwatson /* 3852164032Srwatson * Allow jailed processes to write to sysctls marked as jail 3853164032Srwatson * writable. 3854164032Srwatson */ 3855164032Srwatson case PRIV_SYSCTL_WRITEJAIL: 3856164032Srwatson 3857164032Srwatson /* 3858164032Srwatson * Allow root in jail to manage a variety of quota 3859166831Srwatson * properties. These should likely be conditional on a 3860166831Srwatson * configuration option. 3861164032Srwatson */ 3862166832Srwatson case PRIV_VFS_GETQUOTA: 3863166832Srwatson case PRIV_VFS_SETQUOTA: 3864164032Srwatson 3865164032Srwatson /* 3866164032Srwatson * Since Jail relies on chroot() to implement file system 3867164032Srwatson * protections, grant many VFS privileges to root in jail. 3868164032Srwatson * Be careful to exclude mount-related and NFS-related 3869164032Srwatson * privileges. 3870164032Srwatson */ 3871164032Srwatson case PRIV_VFS_READ: 3872164032Srwatson case PRIV_VFS_WRITE: 3873164032Srwatson case PRIV_VFS_ADMIN: 3874164032Srwatson case PRIV_VFS_EXEC: 3875164032Srwatson case PRIV_VFS_LOOKUP: 3876164032Srwatson case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */ 3877164032Srwatson case PRIV_VFS_CHFLAGS_DEV: 3878164032Srwatson case PRIV_VFS_CHOWN: 3879164032Srwatson case PRIV_VFS_CHROOT: 3880167152Spjd case PRIV_VFS_RETAINSUGID: 3881164032Srwatson case PRIV_VFS_FCHROOT: 3882164032Srwatson case PRIV_VFS_LINK: 3883164032Srwatson case PRIV_VFS_SETGID: 3884172860Srwatson case PRIV_VFS_STAT: 3885164032Srwatson case PRIV_VFS_STICKYFILE: 3886164032Srwatson return (0); 3887164032Srwatson 3888164032Srwatson /* 3889164032Srwatson * Depending on the global setting, allow privilege of 3890164032Srwatson * setting system flags. 3891164032Srwatson */ 3892164032Srwatson case PRIV_VFS_SYSFLAGS: 3893192895Sjamie if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS) 3894164032Srwatson return (0); 3895164032Srwatson else 3896164032Srwatson return (EPERM); 3897164032Srwatson 3898164032Srwatson /* 3899168396Spjd * Depending on the global setting, allow privilege of 3900168396Spjd * mounting/unmounting file systems. 3901168396Spjd */ 3902168396Spjd case PRIV_VFS_MOUNT: 3903168396Spjd case PRIV_VFS_UNMOUNT: 3904168396Spjd case PRIV_VFS_MOUNT_NONUSER: 3905168699Spjd case PRIV_VFS_MOUNT_OWNER: 3906224615Smm if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT && 3907224615Smm cred->cr_prison->pr_enforce_statfs < 2) 3908168396Spjd return (0); 3909168396Spjd else 3910168396Spjd return (EPERM); 3911168396Spjd 3912168396Spjd /* 3913168591Srwatson * Allow jailed root to bind reserved ports and reuse in-use 3914168591Srwatson * ports. 3915164032Srwatson */ 3916164032Srwatson case PRIV_NETINET_RESERVEDPORT: 3917168591Srwatson case PRIV_NETINET_REUSEPORT: 3918164032Srwatson return (0); 3919164032Srwatson 3920164032Srwatson /* 3921175630Sbz * Allow jailed root to set certian IPv4/6 (option) headers. 3922175630Sbz */ 3923175630Sbz case PRIV_NETINET_SETHDROPTS: 3924175630Sbz return (0); 3925175630Sbz 3926175630Sbz /* 3927164032Srwatson * Conditionally allow creating raw sockets in jail. 3928164032Srwatson */ 3929164032Srwatson case PRIV_NETINET_RAW: 3930192895Sjamie if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS) 3931164032Srwatson return (0); 3932164032Srwatson else 3933164032Srwatson return (EPERM); 3934164032Srwatson 3935164032Srwatson /* 3936164032Srwatson * Since jail implements its own visibility limits on netstat 3937164032Srwatson * sysctls, allow getcred. This allows identd to work in 3938164032Srwatson * jail. 3939164032Srwatson */ 3940164032Srwatson case PRIV_NETINET_GETCRED: 3941164032Srwatson return (0); 3942164032Srwatson 3943219304Strasz /* 3944219304Strasz * Allow jailed root to set loginclass. 3945219304Strasz */ 3946219304Strasz case PRIV_PROC_SETLOGINCLASS: 3947219304Strasz return (0); 3948219304Strasz 3949164032Srwatson default: 3950164032Srwatson /* 3951164032Srwatson * In all remaining cases, deny the privilege request. This 3952164032Srwatson * includes almost all network privileges, many system 3953164032Srwatson * configuration privileges. 3954164032Srwatson */ 3955164032Srwatson return (EPERM); 3956164032Srwatson } 3957164032Srwatson} 3958164032Srwatson 3959192895Sjamie/* 3960192895Sjamie * Return the part of pr2's name that is relative to pr1, or the whole name 3961192895Sjamie * if it does not directly follow. 3962192895Sjamie */ 3963192895Sjamie 3964192895Sjamiechar * 3965192895Sjamieprison_name(struct prison *pr1, struct prison *pr2) 3966192895Sjamie{ 3967192895Sjamie char *name; 3968192895Sjamie 3969192895Sjamie /* Jails see themselves as "0" (if they see themselves at all). */ 3970192895Sjamie if (pr1 == pr2) 3971192895Sjamie return "0"; 3972192895Sjamie name = pr2->pr_name; 3973192895Sjamie if (prison_ischild(pr1, pr2)) { 3974192895Sjamie /* 3975192895Sjamie * pr1 isn't locked (and allprison_lock may not be either) 3976192895Sjamie * so its length can't be counted on. But the number of dots 3977192895Sjamie * can be counted on - and counted. 3978192895Sjamie */ 3979192895Sjamie for (; pr1 != &prison0; pr1 = pr1->pr_parent) 3980192895Sjamie name = strchr(name, '.') + 1; 3981192895Sjamie } 3982192895Sjamie return (name); 3983192895Sjamie} 3984192895Sjamie 3985192895Sjamie/* 3986192895Sjamie * Return the part of pr2's path that is relative to pr1, or the whole path 3987192895Sjamie * if it does not directly follow. 3988192895Sjamie */ 3989192895Sjamiestatic char * 3990192895Sjamieprison_path(struct prison *pr1, struct prison *pr2) 3991192895Sjamie{ 3992192895Sjamie char *path1, *path2; 3993192895Sjamie int len1; 3994192895Sjamie 3995192895Sjamie path1 = pr1->pr_path; 3996192895Sjamie path2 = pr2->pr_path; 3997192895Sjamie if (!strcmp(path1, "/")) 3998192895Sjamie return (path2); 3999192895Sjamie len1 = strlen(path1); 4000192895Sjamie if (strncmp(path1, path2, len1)) 4001192895Sjamie return (path2); 4002192895Sjamie if (path2[len1] == '\0') 4003192895Sjamie return "/"; 4004192895Sjamie if (path2[len1] == '/') 4005192895Sjamie return (path2 + len1); 4006192895Sjamie return (path2); 4007192895Sjamie} 4008192895Sjamie 4009192895Sjamie 4010192895Sjamie/* 4011192895Sjamie * Jail-related sysctls. 4012192895Sjamie */ 4013227309Sedstatic SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0, 4014192895Sjamie "Jails"); 4015192895Sjamie 4016113275Smikestatic int 4017113275Smikesysctl_jail_list(SYSCTL_HANDLER_ARGS) 4018113275Smike{ 4019191673Sjamie struct xprison *xp; 4020192895Sjamie struct prison *pr, *cpr; 4021191673Sjamie#ifdef INET 4022191673Sjamie struct in_addr *ip4 = NULL; 4023191673Sjamie int ip4s = 0; 4024191673Sjamie#endif 4025191673Sjamie#ifdef INET6 4026208803Scperciva struct in6_addr *ip6 = NULL; 4027191673Sjamie int ip6s = 0; 4028191673Sjamie#endif 4029192895Sjamie int descend, error; 4030113275Smike 4031191673Sjamie xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK); 4032192895Sjamie pr = req->td->td_ucred->cr_prison; 4033191673Sjamie error = 0; 4034168401Spjd sx_slock(&allprison_lock); 4035192895Sjamie FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 4036192895Sjamie#if defined(INET) || defined(INET6) 4037191673Sjamie again: 4038192895Sjamie#endif 4039192895Sjamie mtx_lock(&cpr->pr_mtx); 4040185435Sbz#ifdef INET 4041192895Sjamie if (cpr->pr_ip4s > 0) { 4042192895Sjamie if (ip4s < cpr->pr_ip4s) { 4043192895Sjamie ip4s = cpr->pr_ip4s; 4044192895Sjamie mtx_unlock(&cpr->pr_mtx); 4045191673Sjamie ip4 = realloc(ip4, ip4s * 4046191673Sjamie sizeof(struct in_addr), M_TEMP, M_WAITOK); 4047191673Sjamie goto again; 4048191673Sjamie } 4049192895Sjamie bcopy(cpr->pr_ip4, ip4, 4050192895Sjamie cpr->pr_ip4s * sizeof(struct in_addr)); 4051191673Sjamie } 4052185435Sbz#endif 4053185435Sbz#ifdef INET6 4054192895Sjamie if (cpr->pr_ip6s > 0) { 4055192895Sjamie if (ip6s < cpr->pr_ip6s) { 4056192895Sjamie ip6s = cpr->pr_ip6s; 4057192895Sjamie mtx_unlock(&cpr->pr_mtx); 4058191673Sjamie ip6 = realloc(ip6, ip6s * 4059191673Sjamie sizeof(struct in6_addr), M_TEMP, M_WAITOK); 4060191673Sjamie goto again; 4061191673Sjamie } 4062192895Sjamie bcopy(cpr->pr_ip6, ip6, 4063192895Sjamie cpr->pr_ip6s * sizeof(struct in6_addr)); 4064191673Sjamie } 4065185435Sbz#endif 4066192895Sjamie if (cpr->pr_ref == 0) { 4067192895Sjamie mtx_unlock(&cpr->pr_mtx); 4068191673Sjamie continue; 4069191673Sjamie } 4070191673Sjamie bzero(xp, sizeof(*xp)); 4071113275Smike xp->pr_version = XPRISON_VERSION; 4072192895Sjamie xp->pr_id = cpr->pr_id; 4073192895Sjamie xp->pr_state = cpr->pr_uref > 0 4074191673Sjamie ? PRISON_STATE_ALIVE : PRISON_STATE_DYING; 4075192895Sjamie strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path)); 4076194118Sjamie strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host)); 4077192895Sjamie strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name)); 4078185435Sbz#ifdef INET 4079192895Sjamie xp->pr_ip4s = cpr->pr_ip4s; 4080185435Sbz#endif 4081185435Sbz#ifdef INET6 4082192895Sjamie xp->pr_ip6s = cpr->pr_ip6s; 4083185435Sbz#endif 4084192895Sjamie mtx_unlock(&cpr->pr_mtx); 4085191673Sjamie error = SYSCTL_OUT(req, xp, sizeof(*xp)); 4086191673Sjamie if (error) 4087191673Sjamie break; 4088185435Sbz#ifdef INET 4089191673Sjamie if (xp->pr_ip4s > 0) { 4090191673Sjamie error = SYSCTL_OUT(req, ip4, 4091191673Sjamie xp->pr_ip4s * sizeof(struct in_addr)); 4092191673Sjamie if (error) 4093191673Sjamie break; 4094185435Sbz } 4095185435Sbz#endif 4096185435Sbz#ifdef INET6 4097191673Sjamie if (xp->pr_ip6s > 0) { 4098191673Sjamie error = SYSCTL_OUT(req, ip6, 4099191673Sjamie xp->pr_ip6s * sizeof(struct in6_addr)); 4100191673Sjamie if (error) 4101191673Sjamie break; 4102185435Sbz } 4103185435Sbz#endif 4104113275Smike } 4105168401Spjd sx_sunlock(&allprison_lock); 4106191673Sjamie free(xp, M_TEMP); 4107191673Sjamie#ifdef INET 4108191673Sjamie free(ip4, M_TEMP); 4109191673Sjamie#endif 4110191673Sjamie#ifdef INET6 4111191673Sjamie free(ip6, M_TEMP); 4112191673Sjamie#endif 4113167354Spjd return (error); 4114113275Smike} 4115113275Smike 4116187864SedSYSCTL_OID(_security_jail, OID_AUTO, list, 4117187864Sed CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4118187864Sed sysctl_jail_list, "S", "List of active jails"); 4119126004Spjd 4120126004Spjdstatic int 4121126004Spjdsysctl_jail_jailed(SYSCTL_HANDLER_ARGS) 4122126004Spjd{ 4123126004Spjd int error, injail; 4124126004Spjd 4125126004Spjd injail = jailed(req->td->td_ucred); 4126126004Spjd error = SYSCTL_OUT(req, &injail, sizeof(injail)); 4127126004Spjd 4128126004Spjd return (error); 4129126004Spjd} 4130192895Sjamie 4131187864SedSYSCTL_PROC(_security_jail, OID_AUTO, jailed, 4132187864Sed CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4133187864Sed sysctl_jail_jailed, "I", "Process in jail?"); 4134185435Sbz 4135192895Sjamie#if defined(INET) || defined(INET6) 4136193865SjamieSYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW, 4137192895Sjamie &jail_max_af_ips, 0, 4138192895Sjamie "Number of IP addresses a jail may have at most per address family"); 4139192895Sjamie#endif 4140192895Sjamie 4141192895Sjamie/* 4142192895Sjamie * Default parameters for jail(2) compatability. For historical reasons, 4143192895Sjamie * the sysctl names have varying similarity to the parameter names. Prisons 4144192895Sjamie * just see their own parameters, and can't change them. 4145192895Sjamie */ 4146192895Sjamiestatic int 4147192895Sjamiesysctl_jail_default_allow(SYSCTL_HANDLER_ARGS) 4148192895Sjamie{ 4149192895Sjamie struct prison *pr; 4150192895Sjamie int allow, error, i; 4151192895Sjamie 4152192895Sjamie pr = req->td->td_ucred->cr_prison; 4153192895Sjamie allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow; 4154192895Sjamie 4155192895Sjamie /* Get the current flag value, and convert it to a boolean. */ 4156192895Sjamie i = (allow & arg2) ? 1 : 0; 4157192895Sjamie if (arg1 != NULL) 4158192895Sjamie i = !i; 4159192895Sjamie error = sysctl_handle_int(oidp, &i, 0, req); 4160192895Sjamie if (error || !req->newptr) 4161192895Sjamie return (error); 4162192895Sjamie i = i ? arg2 : 0; 4163192895Sjamie if (arg1 != NULL) 4164192895Sjamie i ^= arg2; 4165192895Sjamie /* 4166192895Sjamie * The sysctls don't have CTLFLAGS_PRISON, so assume prison0 4167192895Sjamie * for writing. 4168192895Sjamie */ 4169192895Sjamie mtx_lock(&prison0.pr_mtx); 4170192895Sjamie jail_default_allow = (jail_default_allow & ~arg2) | i; 4171192895Sjamie mtx_unlock(&prison0.pr_mtx); 4172192895Sjamie return (0); 4173192895Sjamie} 4174192895Sjamie 4175192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed, 4176192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4177192895Sjamie NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I", 4178192895Sjamie "Processes in jail can set their hostnames"); 4179192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only, 4180192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4181192895Sjamie (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I", 4182192895Sjamie "Processes in jail are limited to creating UNIX/IP/route sockets only"); 4183192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed, 4184192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4185192895Sjamie NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I", 4186192895Sjamie "Processes in jail can use System V IPC primitives"); 4187192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets, 4188192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4189192895Sjamie NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I", 4190192895Sjamie "Prison root can create raw sockets"); 4191192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed, 4192192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4193192895Sjamie NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I", 4194192895Sjamie "Processes in jail can alter system file flags"); 4195192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed, 4196192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4197192895Sjamie NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I", 4198192895Sjamie "Processes in jail can mount/unmount jail-friendly file systems"); 4199232059SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_devfs_allowed, 4200232059Smm CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4201232059Smm NULL, PR_ALLOW_MOUNT_DEVFS, sysctl_jail_default_allow, "I", 4202232186Smm "Processes in jail can mount the devfs file system"); 4203232059SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_nullfs_allowed, 4204232059Smm CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4205232059Smm NULL, PR_ALLOW_MOUNT_NULLFS, sysctl_jail_default_allow, "I", 4206232186Smm "Processes in jail can mount the nullfs file system"); 4207232278SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_procfs_allowed, 4208232278Smm CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4209232278Smm NULL, PR_ALLOW_MOUNT_PROCFS, sysctl_jail_default_allow, "I", 4210232278Smm "Processes in jail can mount the procfs file system"); 4211232186SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_zfs_allowed, 4212232186Smm CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4213232186Smm NULL, PR_ALLOW_MOUNT_ZFS, sysctl_jail_default_allow, "I", 4214232186Smm "Processes in jail can mount the zfs file system"); 4215192895Sjamie 4216192895Sjamiestatic int 4217192895Sjamiesysctl_jail_default_level(SYSCTL_HANDLER_ARGS) 4218192895Sjamie{ 4219192895Sjamie struct prison *pr; 4220192895Sjamie int level, error; 4221192895Sjamie 4222192895Sjamie pr = req->td->td_ucred->cr_prison; 4223192895Sjamie level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2); 4224192895Sjamie error = sysctl_handle_int(oidp, &level, 0, req); 4225192895Sjamie if (error || !req->newptr) 4226192895Sjamie return (error); 4227192895Sjamie *(int *)arg1 = level; 4228192895Sjamie return (0); 4229192895Sjamie} 4230192895Sjamie 4231192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs, 4232192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4233192895Sjamie &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs), 4234192895Sjamie sysctl_jail_default_level, "I", 4235192895Sjamie "Processes in jail cannot see all mounted file systems"); 4236192895Sjamie 4237231267SmmSYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset, 4238231267Smm CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 4239231267Smm &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum), 4240231267Smm sysctl_jail_default_level, "I", 4241231267Smm "Ruleset for the devfs filesystem in jail"); 4242231267Smm 4243192895Sjamie/* 4244192895Sjamie * Nodes to describe jail parameters. Maximum length of string parameters 4245192895Sjamie * is returned in the string itself, and the other parameters exist merely 4246192895Sjamie * to make themselves and their types known. 4247192895Sjamie */ 4248192895SjamieSYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0, 4249192895Sjamie "Jail parameters"); 4250192895Sjamie 4251192895Sjamieint 4252192895Sjamiesysctl_jail_param(SYSCTL_HANDLER_ARGS) 4253192895Sjamie{ 4254192895Sjamie int i; 4255192895Sjamie long l; 4256192895Sjamie size_t s; 4257192895Sjamie char numbuf[12]; 4258192895Sjamie 4259192895Sjamie switch (oidp->oid_kind & CTLTYPE) 4260192895Sjamie { 4261192895Sjamie case CTLTYPE_LONG: 4262192895Sjamie case CTLTYPE_ULONG: 4263192895Sjamie l = 0; 4264192895Sjamie#ifdef SCTL_MASK32 4265192895Sjamie if (!(req->flags & SCTL_MASK32)) 4266192895Sjamie#endif 4267192895Sjamie return (SYSCTL_OUT(req, &l, sizeof(l))); 4268192895Sjamie case CTLTYPE_INT: 4269192895Sjamie case CTLTYPE_UINT: 4270192895Sjamie i = 0; 4271192895Sjamie return (SYSCTL_OUT(req, &i, sizeof(i))); 4272192895Sjamie case CTLTYPE_STRING: 4273219819Sjeff snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2); 4274192895Sjamie return 4275192895Sjamie (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req)); 4276192895Sjamie case CTLTYPE_STRUCT: 4277192895Sjamie s = (size_t)arg2; 4278192895Sjamie return (SYSCTL_OUT(req, &s, sizeof(s))); 4279192895Sjamie } 4280192895Sjamie return (0); 4281192895Sjamie} 4282192895Sjamie 4283192895SjamieSYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID"); 4284192895SjamieSYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID"); 4285192895SjamieSYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name"); 4286192895SjamieSYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path"); 4287192895SjamieSYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW, 4288192895Sjamie "I", "Jail secure level"); 4289192895SjamieSYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW, 4290192895Sjamie "I", "Jail cannot see all mounted file systems"); 4291231267SmmSYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW, 4292231267Smm "I", "Ruleset for in-jail devfs mounts"); 4293192895SjamieSYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW, 4294192895Sjamie "B", "Jail persistence"); 4295194251Sjamie#ifdef VIMAGE 4296194251SjamieSYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN, 4297195870Sjamie "E,jailsys", "Virtual network stack"); 4298194251Sjamie#endif 4299192895SjamieSYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD, 4300192895Sjamie "B", "Jail is in the process of shutting down"); 4301192895Sjamie 4302194762SjamieSYSCTL_JAIL_PARAM_NODE(children, "Number of child jails"); 4303194762SjamieSYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD, 4304194762Sjamie "I", "Current number of child jails"); 4305194762SjamieSYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW, 4306194762Sjamie "I", "Maximum number of child jails"); 4307194762Sjamie 4308195870SjamieSYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info"); 4309192895SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN, 4310192895Sjamie "Jail hostname"); 4311193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN, 4312193066Sjamie "Jail NIS domainname"); 4313193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN, 4314193066Sjamie "Jail host UUID"); 4315193066SjamieSYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW, 4316193066Sjamie "LU", "Jail host ID"); 4317192895Sjamie 4318192895SjamieSYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset"); 4319192895SjamieSYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID"); 4320192895Sjamie 4321192895Sjamie#ifdef INET 4322195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN, 4323195974Sjamie "Jail IPv4 address virtualization"); 4324192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr), 4325192895Sjamie "S,in_addr,a", "Jail IPv4 addresses"); 4326202468SbzSYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4327202468Sbz "B", "Do (not) use IPv4 source address selection rather than the " 4328202468Sbz "primary jail IPv4 address."); 4329192895Sjamie#endif 4330192895Sjamie#ifdef INET6 4331195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN, 4332195974Sjamie "Jail IPv6 address virtualization"); 4333192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr), 4334192895Sjamie "S,in6_addr,a", "Jail IPv6 addresses"); 4335202468SbzSYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4336202468Sbz "B", "Do (not) use IPv6 source address selection rather than the " 4337202468Sbz "primary jail IPv6 address."); 4338192895Sjamie#endif 4339192895Sjamie 4340192895SjamieSYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags"); 4341192895SjamieSYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW, 4342192895Sjamie "B", "Jail may set hostname"); 4343192895SjamieSYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW, 4344192895Sjamie "B", "Jail may use SYSV IPC"); 4345192895SjamieSYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW, 4346192895Sjamie "B", "Jail may create raw sockets"); 4347192895SjamieSYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW, 4348192895Sjamie "B", "Jail may alter system file flags"); 4349192895SjamieSYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW, 4350192895Sjamie "B", "Jail may set file quotas"); 4351192895SjamieSYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW, 4352192895Sjamie "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route"); 4353192895Sjamie 4354232059SmmSYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags"); 4355232059SmmSYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW, 4356232059Smm "B", "Jail may mount/unmount jail-friendly file systems in general"); 4357232059SmmSYSCTL_JAIL_PARAM(_allow_mount, devfs, CTLTYPE_INT | CTLFLAG_RW, 4358232186Smm "B", "Jail may mount the devfs file system"); 4359232059SmmSYSCTL_JAIL_PARAM(_allow_mount, nullfs, CTLTYPE_INT | CTLFLAG_RW, 4360232186Smm "B", "Jail may mount the nullfs file system"); 4361232278SmmSYSCTL_JAIL_PARAM(_allow_mount, procfs, CTLTYPE_INT | CTLFLAG_RW, 4362232278Smm "B", "Jail may mount the procfs file system"); 4363232186SmmSYSCTL_JAIL_PARAM(_allow_mount, zfs, CTLTYPE_INT | CTLFLAG_RW, 4364232186Smm "B", "Jail may mount the zfs file system"); 4365232059Smm 4366220137Straszvoid 4367220137Straszprison_racct_foreach(void (*callback)(struct racct *racct, 4368220137Strasz void *arg2, void *arg3), void *arg2, void *arg3) 4369220137Strasz{ 4370221362Strasz struct prison_racct *prr; 4371192895Sjamie 4372220137Strasz sx_slock(&allprison_lock); 4373221362Strasz LIST_FOREACH(prr, &allprison_racct, prr_next) 4374221362Strasz (callback)(prr->prr_racct, arg2, arg3); 4375220137Strasz sx_sunlock(&allprison_lock); 4376220137Strasz} 4377220137Strasz 4378221362Straszstatic struct prison_racct * 4379221362Straszprison_racct_find_locked(const char *name) 4380221362Strasz{ 4381221362Strasz struct prison_racct *prr; 4382221362Strasz 4383221362Strasz sx_assert(&allprison_lock, SA_XLOCKED); 4384221362Strasz 4385221362Strasz if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN) 4386221362Strasz return (NULL); 4387221362Strasz 4388221362Strasz LIST_FOREACH(prr, &allprison_racct, prr_next) { 4389221362Strasz if (strcmp(name, prr->prr_name) != 0) 4390221362Strasz continue; 4391221362Strasz 4392221362Strasz /* Found prison_racct with a matching name? */ 4393221362Strasz prison_racct_hold(prr); 4394221362Strasz return (prr); 4395221362Strasz } 4396221362Strasz 4397221362Strasz /* Add new prison_racct. */ 4398221362Strasz prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK); 4399221362Strasz racct_create(&prr->prr_racct); 4400221362Strasz 4401221362Strasz strcpy(prr->prr_name, name); 4402221362Strasz refcount_init(&prr->prr_refcount, 1); 4403221362Strasz LIST_INSERT_HEAD(&allprison_racct, prr, prr_next); 4404221362Strasz 4405221362Strasz return (prr); 4406221362Strasz} 4407221362Strasz 4408221362Straszstruct prison_racct * 4409221362Straszprison_racct_find(const char *name) 4410221362Strasz{ 4411221362Strasz struct prison_racct *prr; 4412221362Strasz 4413221362Strasz sx_xlock(&allprison_lock); 4414221362Strasz prr = prison_racct_find_locked(name); 4415221362Strasz sx_xunlock(&allprison_lock); 4416221362Strasz return (prr); 4417221362Strasz} 4418221362Strasz 4419221362Straszvoid 4420221362Straszprison_racct_hold(struct prison_racct *prr) 4421221362Strasz{ 4422221362Strasz 4423221362Strasz refcount_acquire(&prr->prr_refcount); 4424221362Strasz} 4425221362Strasz 4426232598Straszstatic void 4427232598Straszprison_racct_free_locked(struct prison_racct *prr) 4428232598Strasz{ 4429232598Strasz 4430232598Strasz sx_assert(&allprison_lock, SA_XLOCKED); 4431232598Strasz 4432232598Strasz if (refcount_release(&prr->prr_refcount)) { 4433232598Strasz racct_destroy(&prr->prr_racct); 4434232598Strasz LIST_REMOVE(prr, prr_next); 4435232598Strasz free(prr, M_PRISON_RACCT); 4436232598Strasz } 4437232598Strasz} 4438232598Strasz 4439221362Straszvoid 4440221362Straszprison_racct_free(struct prison_racct *prr) 4441221362Strasz{ 4442221362Strasz int old; 4443221362Strasz 4444232598Strasz sx_assert(&allprison_lock, SA_UNLOCKED); 4445232598Strasz 4446221362Strasz old = prr->prr_refcount; 4447221362Strasz if (old > 1 && atomic_cmpset_int(&prr->prr_refcount, old, old - 1)) 4448221362Strasz return; 4449221362Strasz 4450221362Strasz sx_xlock(&allprison_lock); 4451232598Strasz prison_racct_free_locked(prr); 4452221362Strasz sx_xunlock(&allprison_lock); 4453221362Strasz} 4454221362Strasz 4455221362Strasz#ifdef RACCT 4456221362Straszstatic void 4457221362Straszprison_racct_attach(struct prison *pr) 4458221362Strasz{ 4459221362Strasz struct prison_racct *prr; 4460221362Strasz 4461232598Strasz sx_assert(&allprison_lock, SA_XLOCKED); 4462232598Strasz 4463221362Strasz prr = prison_racct_find_locked(pr->pr_name); 4464221362Strasz KASSERT(prr != NULL, ("cannot find prison_racct")); 4465221362Strasz 4466221362Strasz pr->pr_prison_racct = prr; 4467221362Strasz} 4468221362Strasz 4469232598Strasz/* 4470232598Strasz * Handle jail renaming. From the racct point of view, renaming means 4471232598Strasz * moving from one prison_racct to another. 4472232598Strasz */ 4473221362Straszstatic void 4474232598Straszprison_racct_modify(struct prison *pr) 4475232598Strasz{ 4476232598Strasz struct proc *p; 4477232598Strasz struct ucred *cred; 4478232598Strasz struct prison_racct *oldprr; 4479232598Strasz 4480232598Strasz sx_slock(&allproc_lock); 4481232598Strasz sx_xlock(&allprison_lock); 4482232598Strasz 4483235795Strasz if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) { 4484235795Strasz sx_xunlock(&allprison_lock); 4485235795Strasz sx_sunlock(&allproc_lock); 4486232598Strasz return; 4487235795Strasz } 4488232598Strasz 4489232598Strasz oldprr = pr->pr_prison_racct; 4490232598Strasz pr->pr_prison_racct = NULL; 4491232598Strasz 4492232598Strasz prison_racct_attach(pr); 4493232598Strasz 4494232598Strasz /* 4495232598Strasz * Move resource utilisation records. 4496232598Strasz */ 4497232598Strasz racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct); 4498232598Strasz 4499232598Strasz /* 4500232598Strasz * Force rctl to reattach rules to processes. 4501232598Strasz */ 4502232598Strasz FOREACH_PROC_IN_SYSTEM(p) { 4503232598Strasz PROC_LOCK(p); 4504232598Strasz cred = crhold(p->p_ucred); 4505232598Strasz PROC_UNLOCK(p); 4506232598Strasz racct_proc_ucred_changed(p, cred, cred); 4507232598Strasz crfree(cred); 4508232598Strasz } 4509232598Strasz 4510232598Strasz sx_sunlock(&allproc_lock); 4511232598Strasz prison_racct_free_locked(oldprr); 4512232598Strasz sx_xunlock(&allprison_lock); 4513232598Strasz} 4514232598Strasz 4515232598Straszstatic void 4516221362Straszprison_racct_detach(struct prison *pr) 4517221362Strasz{ 4518232598Strasz 4519232598Strasz sx_assert(&allprison_lock, SA_UNLOCKED); 4520232598Strasz 4521221362Strasz prison_racct_free(pr->pr_prison_racct); 4522221362Strasz pr->pr_prison_racct = NULL; 4523221362Strasz} 4524221362Strasz#endif /* RACCT */ 4525221362Strasz 4526185435Sbz#ifdef DDB 4527191673Sjamie 4528191673Sjamiestatic void 4529191673Sjamiedb_show_prison(struct prison *pr) 4530185435Sbz{ 4531192895Sjamie int fi; 4532191673Sjamie#if defined(INET) || defined(INET6) 4533191673Sjamie int ii; 4534185435Sbz#endif 4535195870Sjamie unsigned jsf; 4536185435Sbz#ifdef INET6 4537185435Sbz char ip6buf[INET6_ADDRSTRLEN]; 4538185435Sbz#endif 4539185435Sbz 4540191673Sjamie db_printf("prison %p:\n", pr); 4541191673Sjamie db_printf(" jid = %d\n", pr->pr_id); 4542191673Sjamie db_printf(" name = %s\n", pr->pr_name); 4543192895Sjamie db_printf(" parent = %p\n", pr->pr_parent); 4544191673Sjamie db_printf(" ref = %d\n", pr->pr_ref); 4545191673Sjamie db_printf(" uref = %d\n", pr->pr_uref); 4546191673Sjamie db_printf(" path = %s\n", pr->pr_path); 4547191673Sjamie db_printf(" cpuset = %d\n", pr->pr_cpuset 4548191673Sjamie ? pr->pr_cpuset->cs_id : -1); 4549194251Sjamie#ifdef VIMAGE 4550194251Sjamie db_printf(" vnet = %p\n", pr->pr_vnet); 4551194251Sjamie#endif 4552191673Sjamie db_printf(" root = %p\n", pr->pr_root); 4553191673Sjamie db_printf(" securelevel = %d\n", pr->pr_securelevel); 4554231267Smm db_printf(" devfs_rsnum = %d\n", pr->pr_devfs_rsnum); 4555202123Sbz db_printf(" children.max = %d\n", pr->pr_childmax); 4556202123Sbz db_printf(" children.cur = %d\n", pr->pr_childcount); 4557192895Sjamie db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children)); 4558192895Sjamie db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling)); 4559202123Sbz db_printf(" flags = 0x%x", pr->pr_flags); 4560192895Sjamie for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 4561192895Sjamie fi++) 4562192895Sjamie if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi))) 4563192895Sjamie db_printf(" %s", pr_flag_names[fi]); 4564195870Sjamie for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 4565195870Sjamie fi++) { 4566195870Sjamie jsf = pr->pr_flags & 4567195870Sjamie (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new); 4568195870Sjamie db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name, 4569195870Sjamie pr_flag_jailsys[fi].disable && 4570195870Sjamie (jsf == pr_flag_jailsys[fi].disable) ? "disable" 4571195870Sjamie : (jsf == pr_flag_jailsys[fi].new) ? "new" 4572195870Sjamie : "inherit"); 4573195870Sjamie } 4574202123Sbz db_printf(" allow = 0x%x", pr->pr_allow); 4575192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 4576192895Sjamie fi++) 4577192895Sjamie if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi))) 4578192895Sjamie db_printf(" %s", pr_allow_names[fi]); 4579191673Sjamie db_printf("\n"); 4580192895Sjamie db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs); 4581194118Sjamie db_printf(" host.hostname = %s\n", pr->pr_hostname); 4582194118Sjamie db_printf(" host.domainname = %s\n", pr->pr_domainname); 4583194118Sjamie db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid); 4584193066Sjamie db_printf(" host.hostid = %lu\n", pr->pr_hostid); 4585185435Sbz#ifdef INET 4586191673Sjamie db_printf(" ip4s = %d\n", pr->pr_ip4s); 4587191673Sjamie for (ii = 0; ii < pr->pr_ip4s; ii++) 4588191673Sjamie db_printf(" %s %s\n", 4589202123Sbz ii == 0 ? "ip4.addr =" : " ", 4590191673Sjamie inet_ntoa(pr->pr_ip4[ii])); 4591185435Sbz#endif 4592185435Sbz#ifdef INET6 4593191673Sjamie db_printf(" ip6s = %d\n", pr->pr_ip6s); 4594191673Sjamie for (ii = 0; ii < pr->pr_ip6s; ii++) 4595191673Sjamie db_printf(" %s %s\n", 4596202123Sbz ii == 0 ? "ip6.addr =" : " ", 4597191673Sjamie ip6_sprintf(ip6buf, &pr->pr_ip6[ii])); 4598191673Sjamie#endif 4599191673Sjamie} 4600191673Sjamie 4601191673SjamieDB_SHOW_COMMAND(prison, db_show_prison_command) 4602191673Sjamie{ 4603191673Sjamie struct prison *pr; 4604191673Sjamie 4605191673Sjamie if (!have_addr) { 4606192895Sjamie /* 4607192895Sjamie * Show all prisons in the list, and prison0 which is not 4608192895Sjamie * listed. 4609192895Sjamie */ 4610192895Sjamie db_show_prison(&prison0); 4611192895Sjamie if (!db_pager_quit) { 4612192895Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) { 4613192895Sjamie db_show_prison(pr); 4614192895Sjamie if (db_pager_quit) 4615192895Sjamie break; 4616192895Sjamie } 4617191673Sjamie } 4618191673Sjamie return; 4619191673Sjamie } 4620191673Sjamie 4621192895Sjamie if (addr == 0) 4622192895Sjamie pr = &prison0; 4623192895Sjamie else { 4624192895Sjamie /* Look for a prison with the ID and with references. */ 4625191673Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) 4626192895Sjamie if (pr->pr_id == addr && pr->pr_ref > 0) 4627191673Sjamie break; 4628192895Sjamie if (pr == NULL) 4629192895Sjamie /* Look again, without requiring a reference. */ 4630192895Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) 4631192895Sjamie if (pr->pr_id == addr) 4632192895Sjamie break; 4633192895Sjamie if (pr == NULL) 4634192895Sjamie /* Assume address points to a valid prison. */ 4635192895Sjamie pr = (struct prison *)addr; 4636192895Sjamie } 4637191673Sjamie db_show_prison(pr); 4638185435Sbz} 4639191673Sjamie 4640185435Sbz#endif /* DDB */ 4641