kern_jail.c revision 212436
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 212436 2010-09-10 21:45:42Z jamie $"); 31116182Sobrien 32193066Sjamie#include "opt_compat.h" 33185435Sbz#include "opt_ddb.h" 34185435Sbz#include "opt_inet.h" 35185435Sbz#include "opt_inet6.h" 36131177Spjd 3746155Sphk#include <sys/param.h> 3846155Sphk#include <sys/types.h> 3946155Sphk#include <sys/kernel.h> 4046155Sphk#include <sys/systm.h> 4146155Sphk#include <sys/errno.h> 4246155Sphk#include <sys/sysproto.h> 4346155Sphk#include <sys/malloc.h> 44192895Sjamie#include <sys/osd.h> 45164032Srwatson#include <sys/priv.h> 4646155Sphk#include <sys/proc.h> 47124882Srwatson#include <sys/taskqueue.h> 48177785Skib#include <sys/fcntl.h> 4946155Sphk#include <sys/jail.h> 5087275Srwatson#include <sys/lock.h> 5187275Srwatson#include <sys/mutex.h> 52168401Spjd#include <sys/sx.h> 53193066Sjamie#include <sys/sysent.h> 54113275Smike#include <sys/namei.h> 55147185Spjd#include <sys/mount.h> 56113275Smike#include <sys/queue.h> 5746155Sphk#include <sys/socket.h> 58113275Smike#include <sys/syscallsubr.h> 5957163Srwatson#include <sys/sysctl.h> 60113275Smike#include <sys/vnode.h> 61196019Srwatson 6246155Sphk#include <net/if.h> 63196019Srwatson#include <net/vnet.h> 64196019Srwatson 6546155Sphk#include <netinet/in.h> 66196019Srwatson 67185435Sbz#ifdef DDB 68185435Sbz#include <ddb/ddb.h> 69185435Sbz#ifdef INET6 70185435Sbz#include <netinet6/in6_var.h> 71185435Sbz#endif /* INET6 */ 72185435Sbz#endif /* DDB */ 7346155Sphk 74163606Srwatson#include <security/mac/mac_framework.h> 75163606Srwatson 76195944Sjamie#define DEFAULT_HOSTUUID "00000000-0000-0000-0000-000000000000" 77195944Sjamie 7846155SphkMALLOC_DEFINE(M_PRISON, "prison", "Prison structures"); 7946155Sphk 80202468Sbz/* Keep struct prison prison0 and some code in kern_jail_set() readable. */ 81202468Sbz#ifdef INET 82202468Sbz#ifdef INET6 83202468Sbz#define _PR_IP_SADDRSEL PR_IP4_SADDRSEL|PR_IP6_SADDRSEL 84202468Sbz#else 85202468Sbz#define _PR_IP_SADDRSEL PR_IP4_SADDRSEL 86202468Sbz#endif 87202468Sbz#else /* !INET */ 88202468Sbz#ifdef INET6 89202468Sbz#define _PR_IP_SADDRSEL PR_IP6_SADDRSEL 90202468Sbz#else 91202468Sbz#define _PR_IP_SADDRSEL 0 92202468Sbz#endif 93202468Sbz#endif 94202468Sbz 95192895Sjamie/* prison0 describes what is "real" about the system. */ 96192895Sjamiestruct prison prison0 = { 97192895Sjamie .pr_id = 0, 98192895Sjamie .pr_name = "0", 99192895Sjamie .pr_ref = 1, 100192895Sjamie .pr_uref = 1, 101192895Sjamie .pr_path = "/", 102192895Sjamie .pr_securelevel = -1, 103194762Sjamie .pr_childmax = JAIL_MAX, 104195944Sjamie .pr_hostuuid = DEFAULT_HOSTUUID, 105201145Santoine .pr_children = LIST_HEAD_INITIALIZER(prison0.pr_children), 106196176Sbz#ifdef VIMAGE 107202468Sbz .pr_flags = PR_HOST|PR_VNET|_PR_IP_SADDRSEL, 108196176Sbz#else 109202468Sbz .pr_flags = PR_HOST|_PR_IP_SADDRSEL, 110196176Sbz#endif 111192895Sjamie .pr_allow = PR_ALLOW_ALL, 112192895Sjamie}; 113192895SjamieMTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF); 11457163Srwatson 115192895Sjamie/* allprison and lastprid are protected by allprison_lock. */ 116168401Spjdstruct sx allprison_lock; 117191673SjamieSX_SYSINIT(allprison_lock, &allprison_lock, "allprison"); 118191673Sjamiestruct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison); 119179881Sdelphijint lastprid = 0; 120113275Smike 121191673Sjamiestatic int do_jail_attach(struct thread *td, struct prison *pr); 122190466Sjamiestatic void prison_complete(void *context, int pending); 123191673Sjamiestatic void prison_deref(struct prison *pr, int flags); 124192895Sjamiestatic char *prison_path(struct prison *pr1, struct prison *pr2); 125192895Sjamiestatic void prison_remove_one(struct prison *pr); 126185435Sbz#ifdef INET 127190466Sjamiestatic int _prison_check_ip4(struct prison *pr, struct in_addr *ia); 128192895Sjamiestatic int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4); 129185435Sbz#endif 130185435Sbz#ifdef INET6 131190466Sjamiestatic int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6); 132192895Sjamiestatic int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6); 133185435Sbz#endif 134113275Smike 135191673Sjamie/* Flags for prison_deref */ 136191673Sjamie#define PD_DEREF 0x01 137191673Sjamie#define PD_DEUREF 0x02 138191673Sjamie#define PD_LOCKED 0x04 139191673Sjamie#define PD_LIST_SLOCKED 0x08 140191673Sjamie#define PD_LIST_XLOCKED 0x10 141113275Smike 142192895Sjamie/* 143192895Sjamie * Parameter names corresponding to PR_* flag values 144192895Sjamie */ 145192895Sjamiestatic char *pr_flag_names[] = { 146192895Sjamie [0] = "persist", 147202468Sbz#ifdef INET 148202468Sbz [7] = "ip4.saddrsel", 149202468Sbz#endif 150202468Sbz#ifdef INET6 151202468Sbz [8] = "ip6.saddrsel", 152202468Sbz#endif 153192895Sjamie}; 154192895Sjamie 155192895Sjamiestatic char *pr_flag_nonames[] = { 156192895Sjamie [0] = "nopersist", 157202468Sbz#ifdef INET 158202468Sbz [7] = "ip4.nosaddrsel", 159202468Sbz#endif 160202468Sbz#ifdef INET6 161202468Sbz [8] = "ip6.nosaddrsel", 162202468Sbz#endif 163195870Sjamie}; 164195870Sjamie 165195870Sjamiestruct jailsys_flags { 166195870Sjamie const char *name; 167195870Sjamie unsigned disable; 168195870Sjamie unsigned new; 169195870Sjamie} pr_flag_jailsys[] = { 170195870Sjamie { "host", 0, PR_HOST }, 171195870Sjamie#ifdef VIMAGE 172195870Sjamie { "vnet", 0, PR_VNET }, 173195870Sjamie#endif 174192895Sjamie#ifdef INET 175195870Sjamie { "ip4", PR_IP4_USER | PR_IP4_DISABLE, PR_IP4_USER }, 176192895Sjamie#endif 177192895Sjamie#ifdef INET6 178195870Sjamie { "ip6", PR_IP6_USER | PR_IP6_DISABLE, PR_IP6_USER }, 179192895Sjamie#endif 180192895Sjamie}; 181192895Sjamie 182192895Sjamiestatic char *pr_allow_names[] = { 183192895Sjamie "allow.set_hostname", 184192895Sjamie "allow.sysvipc", 185192895Sjamie "allow.raw_sockets", 186192895Sjamie "allow.chflags", 187192895Sjamie "allow.mount", 188192895Sjamie "allow.quotas", 189192895Sjamie "allow.socket_af", 190192895Sjamie}; 191192895Sjamie 192192895Sjamiestatic char *pr_allow_nonames[] = { 193192895Sjamie "allow.noset_hostname", 194192895Sjamie "allow.nosysvipc", 195192895Sjamie "allow.noraw_sockets", 196192895Sjamie "allow.nochflags", 197192895Sjamie "allow.nomount", 198192895Sjamie "allow.noquotas", 199192895Sjamie "allow.nosocket_af", 200192895Sjamie}; 201192895Sjamie 202196002Sjamie#define JAIL_DEFAULT_ALLOW PR_ALLOW_SET_HOSTNAME 203196002Sjamie#define JAIL_DEFAULT_ENFORCE_STATFS 2 204192895Sjamiestatic unsigned jail_default_allow = JAIL_DEFAULT_ALLOW; 205196002Sjamiestatic int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 206192895Sjamie#if defined(INET) || defined(INET6) 207193865Sjamiestatic unsigned jail_max_af_ips = 255; 208192895Sjamie#endif 209192895Sjamie 210192895Sjamie#ifdef INET 211185435Sbzstatic int 212185435Sbzqcmp_v4(const void *ip1, const void *ip2) 213185435Sbz{ 214185435Sbz in_addr_t iaa, iab; 215185435Sbz 216185435Sbz /* 217185435Sbz * We need to compare in HBO here to get the list sorted as expected 218185435Sbz * by the result of the code. Sorting NBO addresses gives you 219185435Sbz * interesting results. If you do not understand, do not try. 220185435Sbz */ 221185435Sbz iaa = ntohl(((const struct in_addr *)ip1)->s_addr); 222185435Sbz iab = ntohl(((const struct in_addr *)ip2)->s_addr); 223185435Sbz 224185435Sbz /* 225185435Sbz * Do not simply return the difference of the two numbers, the int is 226185435Sbz * not wide enough. 227185435Sbz */ 228185435Sbz if (iaa > iab) 229185435Sbz return (1); 230185435Sbz else if (iaa < iab) 231185435Sbz return (-1); 232185435Sbz else 233185435Sbz return (0); 234185435Sbz} 235185435Sbz#endif 236185435Sbz 237185435Sbz#ifdef INET6 238185435Sbzstatic int 239185435Sbzqcmp_v6(const void *ip1, const void *ip2) 240185435Sbz{ 241185435Sbz const struct in6_addr *ia6a, *ia6b; 242185435Sbz int i, rc; 243185435Sbz 244185435Sbz ia6a = (const struct in6_addr *)ip1; 245185435Sbz ia6b = (const struct in6_addr *)ip2; 246185435Sbz 247185435Sbz rc = 0; 248190466Sjamie for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) { 249185435Sbz if (ia6a->s6_addr[i] > ia6b->s6_addr[i]) 250185435Sbz rc = 1; 251185435Sbz else if (ia6a->s6_addr[i] < ia6b->s6_addr[i]) 252185435Sbz rc = -1; 253185435Sbz } 254185435Sbz return (rc); 255185435Sbz} 256185435Sbz#endif 257185435Sbz 258191673Sjamie/* 259191673Sjamie * struct jail_args { 260191673Sjamie * struct jail *jail; 261191673Sjamie * }; 262191673Sjamie */ 263191673Sjamieint 264191673Sjamiejail(struct thread *td, struct jail_args *uap) 265185435Sbz{ 266191673Sjamie uint32_t version; 267191673Sjamie int error; 268192895Sjamie struct jail j; 269185435Sbz 270191673Sjamie error = copyin(uap->jail, &version, sizeof(uint32_t)); 271191673Sjamie if (error) 272191673Sjamie return (error); 273185435Sbz 274191673Sjamie switch (version) { 275191673Sjamie case 0: 276191673Sjamie { 277191673Sjamie struct jail_v0 j0; 278185435Sbz 279192895Sjamie /* FreeBSD single IPv4 jails. */ 280192895Sjamie bzero(&j, sizeof(struct jail)); 281191673Sjamie error = copyin(uap->jail, &j0, sizeof(struct jail_v0)); 282191673Sjamie if (error) 283191673Sjamie return (error); 284192895Sjamie j.version = j0.version; 285192895Sjamie j.path = j0.path; 286192895Sjamie j.hostname = j0.hostname; 287192895Sjamie j.ip4s = j0.ip_number; 288191673Sjamie break; 289191673Sjamie } 290191673Sjamie 291191673Sjamie case 1: 292185435Sbz /* 293191673Sjamie * Version 1 was used by multi-IPv4 jail implementations 294191673Sjamie * that never made it into the official kernel. 295185435Sbz */ 296191673Sjamie return (EINVAL); 297185435Sbz 298191673Sjamie case 2: /* JAIL_API_VERSION */ 299191673Sjamie /* FreeBSD multi-IPv4/IPv6,noIP jails. */ 300191673Sjamie error = copyin(uap->jail, &j, sizeof(struct jail)); 301191673Sjamie if (error) 302191673Sjamie return (error); 303192895Sjamie break; 304192895Sjamie 305192895Sjamie default: 306192895Sjamie /* Sci-Fi jails are not supported, sorry. */ 307192895Sjamie return (EINVAL); 308192895Sjamie } 309192895Sjamie return (kern_jail(td, &j)); 310192895Sjamie} 311192895Sjamie 312192895Sjamieint 313192895Sjamiekern_jail(struct thread *td, struct jail *j) 314192895Sjamie{ 315193865Sjamie struct iovec optiov[2 * (4 316193865Sjamie + sizeof(pr_allow_names) / sizeof(pr_allow_names[0]) 317193865Sjamie#ifdef INET 318193865Sjamie + 1 319193865Sjamie#endif 320193865Sjamie#ifdef INET6 321193865Sjamie + 1 322193865Sjamie#endif 323193865Sjamie )]; 324192895Sjamie struct uio opt; 325192895Sjamie char *u_path, *u_hostname, *u_name; 326185435Sbz#ifdef INET 327193865Sjamie uint32_t ip4s; 328192895Sjamie struct in_addr *u_ip4; 329192895Sjamie#endif 330192895Sjamie#ifdef INET6 331192895Sjamie struct in6_addr *u_ip6; 332192895Sjamie#endif 333192895Sjamie size_t tmplen; 334192895Sjamie int error, enforce_statfs, fi; 335192895Sjamie 336192895Sjamie bzero(&optiov, sizeof(optiov)); 337192895Sjamie opt.uio_iov = optiov; 338192895Sjamie opt.uio_iovcnt = 0; 339192895Sjamie opt.uio_offset = -1; 340192895Sjamie opt.uio_resid = -1; 341192895Sjamie opt.uio_segflg = UIO_SYSSPACE; 342192895Sjamie opt.uio_rw = UIO_READ; 343192895Sjamie opt.uio_td = td; 344192895Sjamie 345192895Sjamie /* Set permissions for top-level jails from sysctls. */ 346192895Sjamie if (!jailed(td->td_ucred)) { 347192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / 348192895Sjamie sizeof(pr_allow_names[0]); fi++) { 349192895Sjamie optiov[opt.uio_iovcnt].iov_base = 350192895Sjamie (jail_default_allow & (1 << fi)) 351192895Sjamie ? pr_allow_names[fi] : pr_allow_nonames[fi]; 352192895Sjamie optiov[opt.uio_iovcnt].iov_len = 353192895Sjamie strlen(optiov[opt.uio_iovcnt].iov_base) + 1; 354192895Sjamie opt.uio_iovcnt += 2; 355192895Sjamie } 356192895Sjamie optiov[opt.uio_iovcnt].iov_base = "enforce_statfs"; 357192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs"); 358192895Sjamie opt.uio_iovcnt++; 359192895Sjamie enforce_statfs = jail_default_enforce_statfs; 360192895Sjamie optiov[opt.uio_iovcnt].iov_base = &enforce_statfs; 361192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs); 362192895Sjamie opt.uio_iovcnt++; 363192895Sjamie } 364192895Sjamie 365192895Sjamie tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN; 366192895Sjamie#ifdef INET 367192895Sjamie ip4s = (j->version == 0) ? 1 : j->ip4s; 368192895Sjamie if (ip4s > jail_max_af_ips) 369192895Sjamie return (EINVAL); 370192895Sjamie tmplen += ip4s * sizeof(struct in_addr); 371191673Sjamie#else 372192895Sjamie if (j->ip4s > 0) 373192895Sjamie return (EINVAL); 374191673Sjamie#endif 375191673Sjamie#ifdef INET6 376192895Sjamie if (j->ip6s > jail_max_af_ips) 377192895Sjamie return (EINVAL); 378192895Sjamie tmplen += j->ip6s * sizeof(struct in6_addr); 379191673Sjamie#else 380192895Sjamie if (j->ip6s > 0) 381192895Sjamie return (EINVAL); 382191673Sjamie#endif 383192895Sjamie u_path = malloc(tmplen, M_TEMP, M_WAITOK); 384192895Sjamie u_hostname = u_path + MAXPATHLEN; 385192895Sjamie u_name = u_hostname + MAXHOSTNAMELEN; 386191673Sjamie#ifdef INET 387192895Sjamie u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN); 388191673Sjamie#endif 389191673Sjamie#ifdef INET6 390191673Sjamie#ifdef INET 391192895Sjamie u_ip6 = (struct in6_addr *)(u_ip4 + ip4s); 392191673Sjamie#else 393192895Sjamie u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN); 394191673Sjamie#endif 395191673Sjamie#endif 396192895Sjamie optiov[opt.uio_iovcnt].iov_base = "path"; 397192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("path"); 398192895Sjamie opt.uio_iovcnt++; 399192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_path; 400192895Sjamie error = copyinstr(j->path, u_path, MAXPATHLEN, 401192895Sjamie &optiov[opt.uio_iovcnt].iov_len); 402192895Sjamie if (error) { 403192895Sjamie free(u_path, M_TEMP); 404192895Sjamie return (error); 405192895Sjamie } 406192895Sjamie opt.uio_iovcnt++; 407192895Sjamie optiov[opt.uio_iovcnt].iov_base = "host.hostname"; 408192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname"); 409192895Sjamie opt.uio_iovcnt++; 410192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_hostname; 411192895Sjamie error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN, 412192895Sjamie &optiov[opt.uio_iovcnt].iov_len); 413192895Sjamie if (error) { 414192895Sjamie free(u_path, M_TEMP); 415192895Sjamie return (error); 416192895Sjamie } 417192895Sjamie opt.uio_iovcnt++; 418192895Sjamie if (j->jailname != NULL) { 419192895Sjamie optiov[opt.uio_iovcnt].iov_base = "name"; 420192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("name"); 421192895Sjamie opt.uio_iovcnt++; 422192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_name; 423192895Sjamie error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN, 424192895Sjamie &optiov[opt.uio_iovcnt].iov_len); 425191673Sjamie if (error) { 426191673Sjamie free(u_path, M_TEMP); 427191673Sjamie return (error); 428191673Sjamie } 429192895Sjamie opt.uio_iovcnt++; 430192895Sjamie } 431191673Sjamie#ifdef INET 432192895Sjamie optiov[opt.uio_iovcnt].iov_base = "ip4.addr"; 433192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr"); 434192895Sjamie opt.uio_iovcnt++; 435192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_ip4; 436192895Sjamie optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr); 437192895Sjamie if (j->version == 0) 438192895Sjamie u_ip4->s_addr = j->ip4s; 439192895Sjamie else { 440192895Sjamie error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len); 441191673Sjamie if (error) { 442191673Sjamie free(u_path, M_TEMP); 443191673Sjamie return (error); 444191673Sjamie } 445192895Sjamie } 446192895Sjamie opt.uio_iovcnt++; 447185435Sbz#endif 448185435Sbz#ifdef INET6 449192895Sjamie optiov[opt.uio_iovcnt].iov_base = "ip6.addr"; 450192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr"); 451192895Sjamie opt.uio_iovcnt++; 452192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_ip6; 453192895Sjamie optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr); 454192895Sjamie error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len); 455192895Sjamie if (error) { 456192895Sjamie free(u_path, M_TEMP); 457192895Sjamie return (error); 458192895Sjamie } 459192895Sjamie opt.uio_iovcnt++; 460185435Sbz#endif 461192895Sjamie KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]), 462192895Sjamie ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt)); 463191673Sjamie error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH); 464191673Sjamie free(u_path, M_TEMP); 465191673Sjamie return (error); 466185435Sbz} 467185435Sbz 468192895Sjamie 469191673Sjamie/* 470191673Sjamie * struct jail_set_args { 471191673Sjamie * struct iovec *iovp; 472191673Sjamie * unsigned int iovcnt; 473191673Sjamie * int flags; 474191673Sjamie * }; 475191673Sjamie */ 476191673Sjamieint 477191673Sjamiejail_set(struct thread *td, struct jail_set_args *uap) 478185435Sbz{ 479191673Sjamie struct uio *auio; 480191673Sjamie int error; 481191673Sjamie 482191673Sjamie /* Check that we have an even number of iovecs. */ 483191673Sjamie if (uap->iovcnt & 1) 484191673Sjamie return (EINVAL); 485191673Sjamie 486191673Sjamie error = copyinuio(uap->iovp, uap->iovcnt, &auio); 487191673Sjamie if (error) 488191673Sjamie return (error); 489191673Sjamie error = kern_jail_set(td, auio, uap->flags); 490191673Sjamie free(auio, M_IOV); 491191673Sjamie return (error); 492191673Sjamie} 493191673Sjamie 494191673Sjamieint 495191673Sjamiekern_jail_set(struct thread *td, struct uio *optuio, int flags) 496191673Sjamie{ 497191673Sjamie struct nameidata nd; 498185435Sbz#ifdef INET 499190466Sjamie struct in_addr *ip4; 500185435Sbz#endif 501185435Sbz#ifdef INET6 502185435Sbz struct in6_addr *ip6; 503185435Sbz#endif 504191673Sjamie struct vfsopt *opt; 505191673Sjamie struct vfsoptlist *opts; 506196135Sbz struct prison *pr, *deadpr, *mypr, *ppr, *tpr; 507191673Sjamie struct vnode *root; 508196835Sjamie char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid; 509192895Sjamie#if defined(INET) || defined(INET6) 510196135Sbz struct prison *tppr; 511191673Sjamie void *op; 512192895Sjamie#endif 513193066Sjamie unsigned long hid; 514192895Sjamie size_t namelen, onamelen; 515192895Sjamie int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos; 516195870Sjamie int gotchildmax, gotenforce, gothid, gotslevel; 517195870Sjamie int fi, jid, jsys, len, level; 518194762Sjamie int childmax, slevel, vfslocked; 519191673Sjamie#if defined(INET) || defined(INET6) 520192895Sjamie int ii, ij; 521191673Sjamie#endif 522191673Sjamie#ifdef INET 523195974Sjamie int ip4s, redo_ip4; 524191673Sjamie#endif 525191673Sjamie#ifdef INET6 526195974Sjamie int ip6s, redo_ip6; 527191673Sjamie#endif 528191673Sjamie unsigned pr_flags, ch_flags; 529192895Sjamie unsigned pr_allow, ch_allow, tallow; 530191673Sjamie char numbuf[12]; 531185435Sbz 532191673Sjamie error = priv_check(td, PRIV_JAIL_SET); 533191673Sjamie if (!error && (flags & JAIL_ATTACH)) 534191673Sjamie error = priv_check(td, PRIV_JAIL_ATTACH); 535191673Sjamie if (error) 536191673Sjamie return (error); 537192895Sjamie mypr = ppr = td->td_ucred->cr_prison; 538194762Sjamie if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0) 539192895Sjamie return (EPERM); 540191673Sjamie if (flags & ~JAIL_SET_MASK) 541191673Sjamie return (EINVAL); 542191673Sjamie 543185435Sbz /* 544191673Sjamie * Check all the parameters before committing to anything. Not all 545191673Sjamie * errors can be caught early, but we may as well try. Also, this 546191673Sjamie * takes care of some expensive stuff (path lookup) before getting 547191673Sjamie * the allprison lock. 548185435Sbz * 549191673Sjamie * XXX Jails are not filesystems, and jail parameters are not mount 550191673Sjamie * options. But it makes more sense to re-use the vfsopt code 551191673Sjamie * than duplicate it under a different name. 552185435Sbz */ 553191673Sjamie error = vfs_buildopts(optuio, &opts); 554191673Sjamie if (error) 555191673Sjamie return (error); 556185435Sbz#ifdef INET 557185435Sbz ip4 = NULL; 558185435Sbz#endif 559185435Sbz#ifdef INET6 560185435Sbz ip6 = NULL; 561185435Sbz#endif 562191673Sjamie 563191673Sjamie error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 564191673Sjamie if (error == ENOENT) 565191673Sjamie jid = 0; 566191673Sjamie else if (error != 0) 567191673Sjamie goto done_free; 568191673Sjamie 569191673Sjamie error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel)); 570191673Sjamie if (error == ENOENT) 571191673Sjamie gotslevel = 0; 572191673Sjamie else if (error != 0) 573191673Sjamie goto done_free; 574191673Sjamie else 575191673Sjamie gotslevel = 1; 576191673Sjamie 577194762Sjamie error = 578194762Sjamie vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax)); 579194762Sjamie if (error == ENOENT) 580194762Sjamie gotchildmax = 0; 581194762Sjamie else if (error != 0) 582194762Sjamie goto done_free; 583194762Sjamie else 584194762Sjamie gotchildmax = 1; 585194762Sjamie 586192895Sjamie error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce)); 587212436Sjamie if (error == ENOENT) 588212436Sjamie gotenforce = 0; 589212436Sjamie else if (error != 0) 590192895Sjamie goto done_free; 591212436Sjamie else if (enforce < 0 || enforce > 2) { 592212436Sjamie error = EINVAL; 593212436Sjamie goto done_free; 594212436Sjamie } else 595212436Sjamie gotenforce = 1; 596192895Sjamie 597191673Sjamie pr_flags = ch_flags = 0; 598192895Sjamie for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 599192895Sjamie fi++) { 600192895Sjamie if (pr_flag_names[fi] == NULL) 601192895Sjamie continue; 602192895Sjamie vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi); 603192895Sjamie vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi); 604192895Sjamie } 605191673Sjamie ch_flags |= pr_flags; 606195870Sjamie for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 607195870Sjamie fi++) { 608195870Sjamie error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys, 609195870Sjamie sizeof(jsys)); 610195870Sjamie if (error == ENOENT) 611195870Sjamie continue; 612195870Sjamie if (error != 0) 613195870Sjamie goto done_free; 614195870Sjamie switch (jsys) { 615195870Sjamie case JAIL_SYS_DISABLE: 616195870Sjamie if (!pr_flag_jailsys[fi].disable) { 617195870Sjamie error = EINVAL; 618195870Sjamie goto done_free; 619195870Sjamie } 620195870Sjamie pr_flags |= pr_flag_jailsys[fi].disable; 621195870Sjamie break; 622195870Sjamie case JAIL_SYS_NEW: 623195870Sjamie pr_flags |= pr_flag_jailsys[fi].new; 624195870Sjamie break; 625195870Sjamie case JAIL_SYS_INHERIT: 626195870Sjamie break; 627195870Sjamie default: 628195870Sjamie error = EINVAL; 629195870Sjamie goto done_free; 630195870Sjamie } 631195870Sjamie ch_flags |= 632195870Sjamie pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable; 633195870Sjamie } 634211085Sjamie if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE 635211085Sjamie && !(pr_flags & PR_PERSIST)) { 636211085Sjamie error = EINVAL; 637211085Sjamie vfs_opterror(opts, "new jail must persist or attach"); 638211085Sjamie goto done_errmsg; 639211085Sjamie } 640194251Sjamie#ifdef VIMAGE 641194251Sjamie if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) { 642194251Sjamie error = EINVAL; 643194251Sjamie vfs_opterror(opts, "vnet cannot be changed after creation"); 644194251Sjamie goto done_errmsg; 645194251Sjamie } 646194251Sjamie#endif 647195974Sjamie#ifdef INET 648195974Sjamie if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) { 649195974Sjamie error = EINVAL; 650195974Sjamie vfs_opterror(opts, "ip4 cannot be changed after creation"); 651195974Sjamie goto done_errmsg; 652195974Sjamie } 653195974Sjamie#endif 654195974Sjamie#ifdef INET6 655195974Sjamie if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) { 656195974Sjamie error = EINVAL; 657195974Sjamie vfs_opterror(opts, "ip6 cannot be changed after creation"); 658195974Sjamie goto done_errmsg; 659195974Sjamie } 660195974Sjamie#endif 661191673Sjamie 662192895Sjamie pr_allow = ch_allow = 0; 663192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 664192895Sjamie fi++) { 665192895Sjamie vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi); 666192895Sjamie vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi); 667192895Sjamie } 668192895Sjamie ch_allow |= pr_allow; 669192895Sjamie 670191673Sjamie error = vfs_getopt(opts, "name", (void **)&name, &len); 671191673Sjamie if (error == ENOENT) 672191673Sjamie name = NULL; 673191673Sjamie else if (error != 0) 674191673Sjamie goto done_free; 675191673Sjamie else { 676191673Sjamie if (len == 0 || name[len - 1] != '\0') { 677191673Sjamie error = EINVAL; 678191673Sjamie goto done_free; 679191673Sjamie } 680191673Sjamie if (len > MAXHOSTNAMELEN) { 681191673Sjamie error = ENAMETOOLONG; 682191673Sjamie goto done_free; 683191673Sjamie } 684191673Sjamie } 685191673Sjamie 686191673Sjamie error = vfs_getopt(opts, "host.hostname", (void **)&host, &len); 687191673Sjamie if (error == ENOENT) 688191673Sjamie host = NULL; 689191673Sjamie else if (error != 0) 690191673Sjamie goto done_free; 691191673Sjamie else { 692193066Sjamie ch_flags |= PR_HOST; 693193066Sjamie pr_flags |= PR_HOST; 694191673Sjamie if (len == 0 || host[len - 1] != '\0') { 695191673Sjamie error = EINVAL; 696191673Sjamie goto done_free; 697191673Sjamie } 698191673Sjamie if (len > MAXHOSTNAMELEN) { 699191673Sjamie error = ENAMETOOLONG; 700191673Sjamie goto done_free; 701191673Sjamie } 702191673Sjamie } 703191673Sjamie 704193066Sjamie error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len); 705193066Sjamie if (error == ENOENT) 706193066Sjamie domain = NULL; 707193066Sjamie else if (error != 0) 708193066Sjamie goto done_free; 709193066Sjamie else { 710193066Sjamie ch_flags |= PR_HOST; 711193066Sjamie pr_flags |= PR_HOST; 712193066Sjamie if (len == 0 || domain[len - 1] != '\0') { 713193066Sjamie error = EINVAL; 714193066Sjamie goto done_free; 715193066Sjamie } 716193066Sjamie if (len > MAXHOSTNAMELEN) { 717193066Sjamie error = ENAMETOOLONG; 718193066Sjamie goto done_free; 719193066Sjamie } 720193066Sjamie } 721193066Sjamie 722193066Sjamie error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len); 723193066Sjamie if (error == ENOENT) 724193066Sjamie uuid = NULL; 725193066Sjamie else if (error != 0) 726193066Sjamie goto done_free; 727193066Sjamie else { 728193066Sjamie ch_flags |= PR_HOST; 729193066Sjamie pr_flags |= PR_HOST; 730193066Sjamie if (len == 0 || uuid[len - 1] != '\0') { 731193066Sjamie error = EINVAL; 732193066Sjamie goto done_free; 733193066Sjamie } 734193066Sjamie if (len > HOSTUUIDLEN) { 735193066Sjamie error = ENAMETOOLONG; 736193066Sjamie goto done_free; 737193066Sjamie } 738193066Sjamie } 739193066Sjamie 740205014Snwhitehorn#ifdef COMPAT_FREEBSD32 741205014Snwhitehorn if (td->td_proc->p_sysent->sv_flags & SV_ILP32) { 742193066Sjamie uint32_t hid32; 743193066Sjamie 744193066Sjamie error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32)); 745193066Sjamie hid = hid32; 746193066Sjamie } else 747193066Sjamie#endif 748193066Sjamie error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid)); 749193066Sjamie if (error == ENOENT) 750193066Sjamie gothid = 0; 751193066Sjamie else if (error != 0) 752193066Sjamie goto done_free; 753193066Sjamie else { 754193066Sjamie gothid = 1; 755193066Sjamie ch_flags |= PR_HOST; 756193066Sjamie pr_flags |= PR_HOST; 757193066Sjamie } 758193066Sjamie 759185435Sbz#ifdef INET 760191673Sjamie error = vfs_getopt(opts, "ip4.addr", &op, &ip4s); 761191673Sjamie if (error == ENOENT) 762195870Sjamie ip4s = (pr_flags & PR_IP4_DISABLE) ? 0 : -1; 763191673Sjamie else if (error != 0) 764191673Sjamie goto done_free; 765191673Sjamie else if (ip4s & (sizeof(*ip4) - 1)) { 766191673Sjamie error = EINVAL; 767191673Sjamie goto done_free; 768192895Sjamie } else { 769195870Sjamie ch_flags |= PR_IP4_USER | PR_IP4_DISABLE; 770195870Sjamie if (ip4s == 0) 771195870Sjamie pr_flags |= PR_IP4_USER | PR_IP4_DISABLE; 772195870Sjamie else { 773195870Sjamie pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER; 774192895Sjamie ip4s /= sizeof(*ip4); 775192895Sjamie if (ip4s > jail_max_af_ips) { 776185435Sbz error = EINVAL; 777192895Sjamie vfs_opterror(opts, "too many IPv4 addresses"); 778192895Sjamie goto done_errmsg; 779185435Sbz } 780195974Sjamie ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 781192895Sjamie bcopy(op, ip4, ip4s * sizeof(*ip4)); 782192895Sjamie /* 783192895Sjamie * IP addresses are all sorted but ip[0] to preserve 784192895Sjamie * the primary IP address as given from userland. 785192895Sjamie * This special IP is used for unbound outgoing 786202116Sbz * connections as well for "loopback" traffic in case 787202116Sbz * source address selection cannot find any more fitting 788202116Sbz * address to connect from. 789192895Sjamie */ 790192895Sjamie if (ip4s > 1) 791192895Sjamie qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4); 792192895Sjamie /* 793192895Sjamie * Check for duplicate addresses and do some simple 794192895Sjamie * zero and broadcast checks. If users give other bogus 795192895Sjamie * addresses it is their problem. 796192895Sjamie * 797192895Sjamie * We do not have to care about byte order for these 798192895Sjamie * checks so we will do them in NBO. 799192895Sjamie */ 800192895Sjamie for (ii = 0; ii < ip4s; ii++) { 801192895Sjamie if (ip4[ii].s_addr == INADDR_ANY || 802192895Sjamie ip4[ii].s_addr == INADDR_BROADCAST) { 803192895Sjamie error = EINVAL; 804192895Sjamie goto done_free; 805192895Sjamie } 806192895Sjamie if ((ii+1) < ip4s && 807192895Sjamie (ip4[0].s_addr == ip4[ii+1].s_addr || 808192895Sjamie ip4[ii].s_addr == ip4[ii+1].s_addr)) { 809192895Sjamie error = EINVAL; 810192895Sjamie goto done_free; 811192895Sjamie } 812192895Sjamie } 813185435Sbz } 814191673Sjamie } 815191673Sjamie#endif 816185435Sbz 817185435Sbz#ifdef INET6 818191673Sjamie error = vfs_getopt(opts, "ip6.addr", &op, &ip6s); 819191673Sjamie if (error == ENOENT) 820195870Sjamie ip6s = (pr_flags & PR_IP6_DISABLE) ? 0 : -1; 821191673Sjamie else if (error != 0) 822191673Sjamie goto done_free; 823191673Sjamie else if (ip6s & (sizeof(*ip6) - 1)) { 824191673Sjamie error = EINVAL; 825191673Sjamie goto done_free; 826192895Sjamie } else { 827195870Sjamie ch_flags |= PR_IP6_USER | PR_IP6_DISABLE; 828195870Sjamie if (ip6s == 0) 829195870Sjamie pr_flags |= PR_IP6_USER | PR_IP6_DISABLE; 830195870Sjamie else { 831195870Sjamie pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER; 832192895Sjamie ip6s /= sizeof(*ip6); 833192895Sjamie if (ip6s > jail_max_af_ips) { 834185435Sbz error = EINVAL; 835192895Sjamie vfs_opterror(opts, "too many IPv6 addresses"); 836192895Sjamie goto done_errmsg; 837185435Sbz } 838195974Sjamie ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 839192895Sjamie bcopy(op, ip6, ip6s * sizeof(*ip6)); 840192895Sjamie if (ip6s > 1) 841192895Sjamie qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6); 842192895Sjamie for (ii = 0; ii < ip6s; ii++) { 843192895Sjamie if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) { 844192895Sjamie error = EINVAL; 845192895Sjamie goto done_free; 846192895Sjamie } 847192895Sjamie if ((ii+1) < ip6s && 848192895Sjamie (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) || 849192895Sjamie IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1]))) 850192895Sjamie { 851192895Sjamie error = EINVAL; 852192895Sjamie goto done_free; 853192895Sjamie } 854192895Sjamie } 855185435Sbz } 856191673Sjamie } 857185435Sbz#endif 858185435Sbz 859195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6)) 860195945Sjamie if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 861195945Sjamie error = EINVAL; 862195945Sjamie vfs_opterror(opts, 863195945Sjamie "vnet jails cannot have IP address restrictions"); 864195945Sjamie goto done_errmsg; 865195945Sjamie } 866195945Sjamie#endif 867195945Sjamie 868191673Sjamie root = NULL; 869191673Sjamie error = vfs_getopt(opts, "path", (void **)&path, &len); 870191673Sjamie if (error == ENOENT) 871191673Sjamie path = NULL; 872191673Sjamie else if (error != 0) 873191673Sjamie goto done_free; 874191673Sjamie else { 875191673Sjamie if (flags & JAIL_UPDATE) { 876191673Sjamie error = EINVAL; 877191673Sjamie vfs_opterror(opts, 878191673Sjamie "path cannot be changed after creation"); 879191673Sjamie goto done_errmsg; 880191673Sjamie } 881191673Sjamie if (len == 0 || path[len - 1] != '\0') { 882191673Sjamie error = EINVAL; 883191673Sjamie goto done_free; 884191673Sjamie } 885191673Sjamie if (len < 2 || (len == 2 && path[0] == '/')) 886191673Sjamie path = NULL; 887191673Sjamie else { 888192895Sjamie /* Leave room for a real-root full pathname. */ 889192895Sjamie if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/") 890192895Sjamie ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) { 891192895Sjamie error = ENAMETOOLONG; 892192895Sjamie goto done_free; 893192895Sjamie } 894191673Sjamie NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_SYSSPACE, 895191673Sjamie path, td); 896191673Sjamie error = namei(&nd); 897191673Sjamie if (error) 898191673Sjamie goto done_free; 899191673Sjamie vfslocked = NDHASGIANT(&nd); 900191673Sjamie root = nd.ni_vp; 901191673Sjamie NDFREE(&nd, NDF_ONLY_PNBUF); 902191673Sjamie if (root->v_type != VDIR) { 903191673Sjamie error = ENOTDIR; 904191673Sjamie vrele(root); 905191673Sjamie VFS_UNLOCK_GIANT(vfslocked); 906191673Sjamie goto done_free; 907191673Sjamie } 908191673Sjamie VFS_UNLOCK_GIANT(vfslocked); 909191673Sjamie } 910191673Sjamie } 911185435Sbz 912191673Sjamie /* 913191673Sjamie * Grab the allprison lock before letting modules check their 914191673Sjamie * parameters. Once we have it, do not let go so we'll have a 915191673Sjamie * consistent view of the OSD list. 916191673Sjamie */ 917191673Sjamie sx_xlock(&allprison_lock); 918191673Sjamie error = osd_jail_call(NULL, PR_METHOD_CHECK, opts); 919191673Sjamie if (error) 920191673Sjamie goto done_unlock_list; 921185435Sbz 922191673Sjamie /* By now, all parameters should have been noted. */ 923191673Sjamie TAILQ_FOREACH(opt, opts, link) { 924191673Sjamie if (!opt->seen && strcmp(opt->name, "errmsg")) { 925191673Sjamie error = EINVAL; 926191673Sjamie vfs_opterror(opts, "unknown parameter: %s", opt->name); 927191673Sjamie goto done_unlock_list; 928191673Sjamie } 929191673Sjamie } 930191673Sjamie 931185435Sbz /* 932191673Sjamie * See if we are creating a new record or updating an existing one. 933191673Sjamie * This abuses the file error codes ENOENT and EEXIST. 934185435Sbz */ 935191673Sjamie cuflags = flags & (JAIL_CREATE | JAIL_UPDATE); 936191673Sjamie if (!cuflags) { 937191673Sjamie error = EINVAL; 938191673Sjamie vfs_opterror(opts, "no valid operation (create or update)"); 939191673Sjamie goto done_unlock_list; 940191673Sjamie } 941191673Sjamie pr = NULL; 942196835Sjamie namelc = NULL; 943196835Sjamie if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) { 944196835Sjamie namelc = strrchr(name, '.'); 945196835Sjamie jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10); 946196835Sjamie if (*p != '\0') 947196835Sjamie jid = 0; 948196835Sjamie } 949191673Sjamie if (jid != 0) { 950192895Sjamie /* 951192895Sjamie * See if a requested jid already exists. There is an 952192895Sjamie * information leak here if the jid exists but is not within 953192895Sjamie * the caller's jail hierarchy. Jail creators will get EEXIST 954192895Sjamie * even though they cannot see the jail, and CREATE | UPDATE 955192895Sjamie * will return ENOENT which is not normally a valid error. 956192895Sjamie */ 957191673Sjamie if (jid < 0) { 958191673Sjamie error = EINVAL; 959191673Sjamie vfs_opterror(opts, "negative jid"); 960191673Sjamie goto done_unlock_list; 961191673Sjamie } 962191673Sjamie pr = prison_find(jid); 963191673Sjamie if (pr != NULL) { 964192895Sjamie ppr = pr->pr_parent; 965191673Sjamie /* Create: jid must not exist. */ 966191673Sjamie if (cuflags == JAIL_CREATE) { 967191673Sjamie mtx_unlock(&pr->pr_mtx); 968191673Sjamie error = EEXIST; 969191673Sjamie vfs_opterror(opts, "jail %d already exists", 970191673Sjamie jid); 971191673Sjamie goto done_unlock_list; 972191673Sjamie } 973192895Sjamie if (!prison_ischild(mypr, pr)) { 974192895Sjamie mtx_unlock(&pr->pr_mtx); 975192895Sjamie pr = NULL; 976192895Sjamie } else if (pr->pr_uref == 0) { 977191673Sjamie if (!(flags & JAIL_DYING)) { 978191673Sjamie mtx_unlock(&pr->pr_mtx); 979191673Sjamie error = ENOENT; 980191673Sjamie vfs_opterror(opts, "jail %d is dying", 981191673Sjamie jid); 982191673Sjamie goto done_unlock_list; 983191673Sjamie } else if ((flags & JAIL_ATTACH) || 984191673Sjamie (pr_flags & PR_PERSIST)) { 985191673Sjamie /* 986191673Sjamie * A dying jail might be resurrected 987191673Sjamie * (via attach or persist), but first 988191673Sjamie * it must determine if another jail 989191673Sjamie * has claimed its name. Accomplish 990191673Sjamie * this by implicitly re-setting the 991191673Sjamie * name. 992191673Sjamie */ 993191673Sjamie if (name == NULL) 994192895Sjamie name = prison_name(mypr, pr); 995191673Sjamie } 996191673Sjamie } 997191673Sjamie } 998191673Sjamie if (pr == NULL) { 999191673Sjamie /* Update: jid must exist. */ 1000191673Sjamie if (cuflags == JAIL_UPDATE) { 1001191673Sjamie error = ENOENT; 1002191673Sjamie vfs_opterror(opts, "jail %d not found", jid); 1003191673Sjamie goto done_unlock_list; 1004191673Sjamie } 1005191673Sjamie } 1006191673Sjamie } 1007191673Sjamie /* 1008191673Sjamie * If the caller provided a name, look for a jail by that name. 1009191673Sjamie * This has different semantics for creates and updates keyed by jid 1010191673Sjamie * (where the name must not already exist in a different jail), 1011191673Sjamie * and updates keyed by the name itself (where the name must exist 1012191673Sjamie * because that is the jail being updated). 1013191673Sjamie */ 1014191673Sjamie if (name != NULL) { 1015196835Sjamie namelc = strrchr(name, '.'); 1016196835Sjamie if (namelc == NULL) 1017196835Sjamie namelc = name; 1018196835Sjamie else { 1019192895Sjamie /* 1020192895Sjamie * This is a hierarchical name. Split it into the 1021192895Sjamie * parent and child names, and make sure the parent 1022192895Sjamie * exists or matches an already found jail. 1023192895Sjamie */ 1024196835Sjamie *namelc = '\0'; 1025192895Sjamie if (pr != NULL) { 1026196835Sjamie if (strncmp(name, ppr->pr_name, namelc - name) 1027196835Sjamie || ppr->pr_name[namelc - name] != '\0') { 1028192895Sjamie mtx_unlock(&pr->pr_mtx); 1029192895Sjamie error = EINVAL; 1030192895Sjamie vfs_opterror(opts, 1031192895Sjamie "cannot change jail's parent"); 1032192895Sjamie goto done_unlock_list; 1033192895Sjamie } 1034192895Sjamie } else { 1035192895Sjamie ppr = prison_find_name(mypr, name); 1036192895Sjamie if (ppr == NULL) { 1037192895Sjamie error = ENOENT; 1038192895Sjamie vfs_opterror(opts, 1039192895Sjamie "jail \"%s\" not found", name); 1040192895Sjamie goto done_unlock_list; 1041192895Sjamie } 1042192895Sjamie mtx_unlock(&ppr->pr_mtx); 1043192895Sjamie } 1044196835Sjamie name = ++namelc; 1045192895Sjamie } 1046191673Sjamie if (name[0] != '\0') { 1047192895Sjamie namelen = 1048192895Sjamie (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 1049192895Sjamie name_again: 1050191673Sjamie deadpr = NULL; 1051192895Sjamie FOREACH_PRISON_CHILD(ppr, tpr) { 1052191673Sjamie if (tpr != pr && tpr->pr_ref > 0 && 1053192895Sjamie !strcmp(tpr->pr_name + namelen, name)) { 1054191673Sjamie if (pr == NULL && 1055191673Sjamie cuflags != JAIL_CREATE) { 1056191673Sjamie mtx_lock(&tpr->pr_mtx); 1057191673Sjamie if (tpr->pr_ref > 0) { 1058191673Sjamie /* 1059191673Sjamie * Use this jail 1060191673Sjamie * for updates. 1061191673Sjamie */ 1062191673Sjamie if (tpr->pr_uref > 0) { 1063191673Sjamie pr = tpr; 1064191673Sjamie break; 1065191673Sjamie } 1066191673Sjamie deadpr = tpr; 1067191673Sjamie } 1068191673Sjamie mtx_unlock(&tpr->pr_mtx); 1069191673Sjamie } else if (tpr->pr_uref > 0) { 1070191673Sjamie /* 1071191673Sjamie * Create, or update(jid): 1072191673Sjamie * name must not exist in an 1073192895Sjamie * active sibling jail. 1074191673Sjamie */ 1075191673Sjamie error = EEXIST; 1076191673Sjamie if (pr != NULL) 1077191673Sjamie mtx_unlock(&pr->pr_mtx); 1078191673Sjamie vfs_opterror(opts, 1079191673Sjamie "jail \"%s\" already exists", 1080191673Sjamie name); 1081191673Sjamie goto done_unlock_list; 1082191673Sjamie } 1083191673Sjamie } 1084191673Sjamie } 1085191673Sjamie /* If no active jail is found, use a dying one. */ 1086191673Sjamie if (deadpr != NULL && pr == NULL) { 1087191673Sjamie if (flags & JAIL_DYING) { 1088191673Sjamie mtx_lock(&deadpr->pr_mtx); 1089191673Sjamie if (deadpr->pr_ref == 0) { 1090191673Sjamie mtx_unlock(&deadpr->pr_mtx); 1091191673Sjamie goto name_again; 1092191673Sjamie } 1093191673Sjamie pr = deadpr; 1094191673Sjamie } else if (cuflags == JAIL_UPDATE) { 1095191673Sjamie error = ENOENT; 1096191673Sjamie vfs_opterror(opts, 1097191673Sjamie "jail \"%s\" is dying", name); 1098191673Sjamie goto done_unlock_list; 1099191673Sjamie } 1100191673Sjamie } 1101191673Sjamie /* Update: name must exist if no jid. */ 1102191673Sjamie else if (cuflags == JAIL_UPDATE && pr == NULL) { 1103191673Sjamie error = ENOENT; 1104191673Sjamie vfs_opterror(opts, "jail \"%s\" not found", 1105191673Sjamie name); 1106191673Sjamie goto done_unlock_list; 1107191673Sjamie } 1108191673Sjamie } 1109191673Sjamie } 1110191673Sjamie /* Update: must provide a jid or name. */ 1111191673Sjamie else if (cuflags == JAIL_UPDATE && pr == NULL) { 1112191673Sjamie error = ENOENT; 1113191673Sjamie vfs_opterror(opts, "update specified no jail"); 1114191673Sjamie goto done_unlock_list; 1115191673Sjamie } 1116185435Sbz 1117191673Sjamie /* If there's no prison to update, create a new one and link it in. */ 1118191673Sjamie if (pr == NULL) { 1119194762Sjamie for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent) 1120194762Sjamie if (tpr->pr_childcount >= tpr->pr_childmax) { 1121194762Sjamie error = EPERM; 1122194762Sjamie vfs_opterror(opts, "prison limit exceeded"); 1123194762Sjamie goto done_unlock_list; 1124194762Sjamie } 1125191673Sjamie created = 1; 1126192895Sjamie mtx_lock(&ppr->pr_mtx); 1127192895Sjamie if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) { 1128192895Sjamie mtx_unlock(&ppr->pr_mtx); 1129192895Sjamie error = ENOENT; 1130192895Sjamie vfs_opterror(opts, "parent jail went away!"); 1131192895Sjamie goto done_unlock_list; 1132192895Sjamie } 1133192895Sjamie ppr->pr_ref++; 1134192895Sjamie ppr->pr_uref++; 1135192895Sjamie mtx_unlock(&ppr->pr_mtx); 1136191673Sjamie pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO); 1137191673Sjamie if (jid == 0) { 1138191673Sjamie /* Find the next free jid. */ 1139191673Sjamie jid = lastprid + 1; 1140191673Sjamie findnext: 1141191673Sjamie if (jid == JAIL_MAX) 1142191673Sjamie jid = 1; 1143191673Sjamie TAILQ_FOREACH(tpr, &allprison, pr_list) { 1144191673Sjamie if (tpr->pr_id < jid) 1145191673Sjamie continue; 1146191673Sjamie if (tpr->pr_id > jid || tpr->pr_ref == 0) { 1147191673Sjamie TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1148191673Sjamie break; 1149191673Sjamie } 1150191673Sjamie if (jid == lastprid) { 1151191673Sjamie error = EAGAIN; 1152191673Sjamie vfs_opterror(opts, 1153191673Sjamie "no available jail IDs"); 1154191673Sjamie free(pr, M_PRISON); 1155192895Sjamie prison_deref(ppr, PD_DEREF | 1156192895Sjamie PD_DEUREF | PD_LIST_XLOCKED); 1157192895Sjamie goto done_releroot; 1158191673Sjamie } 1159191673Sjamie jid++; 1160191673Sjamie goto findnext; 1161191673Sjamie } 1162191673Sjamie lastprid = jid; 1163191673Sjamie } else { 1164191673Sjamie /* 1165191673Sjamie * The jail already has a jid (that did not yet exist), 1166191673Sjamie * so just find where to insert it. 1167191673Sjamie */ 1168191673Sjamie TAILQ_FOREACH(tpr, &allprison, pr_list) 1169191673Sjamie if (tpr->pr_id >= jid) { 1170191673Sjamie TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1171191673Sjamie break; 1172191673Sjamie } 1173191673Sjamie } 1174191673Sjamie if (tpr == NULL) 1175191673Sjamie TAILQ_INSERT_TAIL(&allprison, pr, pr_list); 1176192895Sjamie LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling); 1177192895Sjamie for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 1178194762Sjamie tpr->pr_childcount++; 1179185435Sbz 1180192895Sjamie pr->pr_parent = ppr; 1181191673Sjamie pr->pr_id = jid; 1182192895Sjamie 1183192895Sjamie /* Set some default values, and inherit some from the parent. */ 1184191673Sjamie if (name == NULL) 1185191673Sjamie name = ""; 1186191673Sjamie if (path == NULL) { 1187191673Sjamie path = "/"; 1188192895Sjamie root = mypr->pr_root; 1189191673Sjamie vref(root); 1190191673Sjamie } 1191195944Sjamie strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN); 1192195944Sjamie pr->pr_flags |= PR_HOST; 1193195945Sjamie#if defined(INET) || defined(INET6) 1194195945Sjamie#ifdef VIMAGE 1195195945Sjamie if (!(pr_flags & PR_VNET)) 1196195945Sjamie#endif 1197195945Sjamie { 1198192895Sjamie#ifdef INET 1199195974Sjamie if (!(ch_flags & PR_IP4_USER)) 1200195974Sjamie pr->pr_flags |= 1201195974Sjamie PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE; 1202195974Sjamie else if (!(pr_flags & PR_IP4_USER)) { 1203195974Sjamie pr->pr_flags |= ppr->pr_flags & PR_IP4; 1204195974Sjamie if (ppr->pr_ip4 != NULL) { 1205195974Sjamie pr->pr_ip4s = ppr->pr_ip4s; 1206195974Sjamie pr->pr_ip4 = malloc(pr->pr_ip4s * 1207195974Sjamie sizeof(struct in_addr), M_PRISON, 1208195974Sjamie M_WAITOK); 1209195974Sjamie bcopy(ppr->pr_ip4, pr->pr_ip4, 1210195974Sjamie pr->pr_ip4s * sizeof(*pr->pr_ip4)); 1211195974Sjamie } 1212195974Sjamie } 1213192895Sjamie#endif 1214192895Sjamie#ifdef INET6 1215195974Sjamie if (!(ch_flags & PR_IP6_USER)) 1216195974Sjamie pr->pr_flags |= 1217195974Sjamie PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE; 1218195974Sjamie else if (!(pr_flags & PR_IP6_USER)) { 1219195974Sjamie pr->pr_flags |= ppr->pr_flags & PR_IP6; 1220195974Sjamie if (ppr->pr_ip6 != NULL) { 1221195974Sjamie pr->pr_ip6s = ppr->pr_ip6s; 1222195974Sjamie pr->pr_ip6 = malloc(pr->pr_ip6s * 1223195974Sjamie sizeof(struct in6_addr), M_PRISON, 1224195974Sjamie M_WAITOK); 1225195974Sjamie bcopy(ppr->pr_ip6, pr->pr_ip6, 1226195974Sjamie pr->pr_ip6s * sizeof(*pr->pr_ip6)); 1227195974Sjamie } 1228195974Sjamie } 1229192895Sjamie#endif 1230195945Sjamie } 1231195945Sjamie#endif 1232202468Sbz /* Source address selection is always on by default. */ 1233202468Sbz pr->pr_flags |= _PR_IP_SADDRSEL; 1234202468Sbz 1235192895Sjamie pr->pr_securelevel = ppr->pr_securelevel; 1236192895Sjamie pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow; 1237196002Sjamie pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 1238191673Sjamie 1239192895Sjamie LIST_INIT(&pr->pr_children); 1240192895Sjamie mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK); 1241191673Sjamie 1242194251Sjamie#ifdef VIMAGE 1243194251Sjamie /* Allocate a new vnet if specified. */ 1244194251Sjamie pr->pr_vnet = (pr_flags & PR_VNET) 1245194251Sjamie ? vnet_alloc() : ppr->pr_vnet; 1246194251Sjamie#endif 1247185435Sbz /* 1248191673Sjamie * Allocate a dedicated cpuset for each jail. 1249191673Sjamie * Unlike other initial settings, this may return an erorr. 1250185435Sbz */ 1251192895Sjamie error = cpuset_create_root(ppr, &pr->pr_cpuset); 1252191673Sjamie if (error) { 1253191673Sjamie prison_deref(pr, PD_LIST_XLOCKED); 1254191673Sjamie goto done_releroot; 1255191673Sjamie } 1256185435Sbz 1257191673Sjamie mtx_lock(&pr->pr_mtx); 1258185435Sbz /* 1259191673Sjamie * New prisons do not yet have a reference, because we do not 1260191673Sjamie * want other to see the incomplete prison once the 1261191673Sjamie * allprison_lock is downgraded. 1262185435Sbz */ 1263191673Sjamie } else { 1264191673Sjamie created = 0; 1265195974Sjamie /* 1266195974Sjamie * Grab a reference for existing prisons, to ensure they 1267195974Sjamie * continue to exist for the duration of the call. 1268195974Sjamie */ 1269195974Sjamie pr->pr_ref++; 1270195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6)) 1271195945Sjamie if ((pr->pr_flags & PR_VNET) && 1272195945Sjamie (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 1273195945Sjamie error = EINVAL; 1274195945Sjamie vfs_opterror(opts, 1275195945Sjamie "vnet jails cannot have IP address restrictions"); 1276195945Sjamie goto done_deref_locked; 1277195945Sjamie } 1278195945Sjamie#endif 1279195974Sjamie#ifdef INET 1280195974Sjamie if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 1281195974Sjamie error = EINVAL; 1282195974Sjamie vfs_opterror(opts, 1283195974Sjamie "ip4 cannot be changed after creation"); 1284195974Sjamie goto done_deref_locked; 1285195974Sjamie } 1286195974Sjamie#endif 1287195974Sjamie#ifdef INET6 1288195974Sjamie if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 1289195974Sjamie error = EINVAL; 1290195974Sjamie vfs_opterror(opts, 1291195974Sjamie "ip6 cannot be changed after creation"); 1292195974Sjamie goto done_deref_locked; 1293195974Sjamie } 1294195974Sjamie#endif 1295191673Sjamie } 1296185435Sbz 1297191673Sjamie /* Do final error checking before setting anything. */ 1298192895Sjamie if (gotslevel) { 1299192895Sjamie if (slevel < ppr->pr_securelevel) { 1300192895Sjamie error = EPERM; 1301192895Sjamie goto done_deref_locked; 1302192895Sjamie } 1303192895Sjamie } 1304194762Sjamie if (gotchildmax) { 1305194762Sjamie if (childmax >= ppr->pr_childmax) { 1306194762Sjamie error = EPERM; 1307194762Sjamie goto done_deref_locked; 1308194762Sjamie } 1309194762Sjamie } 1310192895Sjamie if (gotenforce) { 1311192895Sjamie if (enforce < ppr->pr_enforce_statfs) { 1312192895Sjamie error = EPERM; 1313192895Sjamie goto done_deref_locked; 1314192895Sjamie } 1315192895Sjamie } 1316185435Sbz#ifdef INET 1317195974Sjamie if (ip4s > 0) { 1318192895Sjamie if (ppr->pr_flags & PR_IP4) { 1319195974Sjamie /* 1320195974Sjamie * Make sure the new set of IP addresses is a 1321195974Sjamie * subset of the parent's list. Don't worry 1322195974Sjamie * about the parent being unlocked, as any 1323195974Sjamie * setting is done with allprison_lock held. 1324195974Sjamie */ 1325195974Sjamie for (ij = 0; ij < ppr->pr_ip4s; ij++) 1326195974Sjamie if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 1327195974Sjamie break; 1328195974Sjamie if (ij == ppr->pr_ip4s) { 1329195974Sjamie error = EPERM; 1330195974Sjamie goto done_deref_locked; 1331195974Sjamie } 1332195974Sjamie if (ip4s > 1) { 1333195974Sjamie for (ii = ij = 1; ii < ip4s; ii++) { 1334195974Sjamie if (ip4[ii].s_addr == 1335195974Sjamie ppr->pr_ip4[0].s_addr) 1336195974Sjamie continue; 1337195974Sjamie for (; ij < ppr->pr_ip4s; ij++) 1338195974Sjamie if (ip4[ii].s_addr == 1339195974Sjamie ppr->pr_ip4[ij].s_addr) 1340195974Sjamie break; 1341195974Sjamie if (ij == ppr->pr_ip4s) 1342195974Sjamie break; 1343192895Sjamie } 1344192895Sjamie if (ij == ppr->pr_ip4s) { 1345192895Sjamie error = EPERM; 1346192895Sjamie goto done_deref_locked; 1347192895Sjamie } 1348192895Sjamie } 1349192895Sjamie } 1350195974Sjamie /* 1351195974Sjamie * Check for conflicting IP addresses. We permit them 1352195974Sjamie * if there is no more than one IP on each jail. If 1353195974Sjamie * there is a duplicate on a jail with more than one 1354195974Sjamie * IP stop checking and return error. 1355195974Sjamie */ 1356195974Sjamie tppr = ppr; 1357195945Sjamie#ifdef VIMAGE 1358195974Sjamie for (; tppr != &prison0; tppr = tppr->pr_parent) 1359195974Sjamie if (tppr->pr_flags & PR_VNET) 1360195974Sjamie break; 1361195945Sjamie#endif 1362195974Sjamie FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1363195974Sjamie if (tpr == pr || 1364195945Sjamie#ifdef VIMAGE 1365195974Sjamie (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1366195945Sjamie#endif 1367195974Sjamie tpr->pr_uref == 0) { 1368192895Sjamie descend = 0; 1369195974Sjamie continue; 1370195974Sjamie } 1371195974Sjamie if (!(tpr->pr_flags & PR_IP4_USER)) 1372195974Sjamie continue; 1373195974Sjamie descend = 0; 1374195974Sjamie if (tpr->pr_ip4 == NULL || 1375195974Sjamie (ip4s == 1 && tpr->pr_ip4s == 1)) 1376195974Sjamie continue; 1377195974Sjamie for (ii = 0; ii < ip4s; ii++) { 1378195974Sjamie if (_prison_check_ip4(tpr, &ip4[ii]) == 0) { 1379195974Sjamie error = EADDRINUSE; 1380195974Sjamie vfs_opterror(opts, 1381195974Sjamie "IPv4 addresses clash"); 1382195974Sjamie goto done_deref_locked; 1383192895Sjamie } 1384192895Sjamie } 1385192895Sjamie } 1386192895Sjamie } 1387185435Sbz#endif 1388191673Sjamie#ifdef INET6 1389195974Sjamie if (ip6s > 0) { 1390192895Sjamie if (ppr->pr_flags & PR_IP6) { 1391195974Sjamie /* 1392195974Sjamie * Make sure the new set of IP addresses is a 1393195974Sjamie * subset of the parent's list. 1394195974Sjamie */ 1395195974Sjamie for (ij = 0; ij < ppr->pr_ip6s; ij++) 1396195974Sjamie if (IN6_ARE_ADDR_EQUAL(&ip6[0], 1397195974Sjamie &ppr->pr_ip6[ij])) 1398195974Sjamie break; 1399195974Sjamie if (ij == ppr->pr_ip6s) { 1400195974Sjamie error = EPERM; 1401195974Sjamie goto done_deref_locked; 1402195974Sjamie } 1403195974Sjamie if (ip6s > 1) { 1404195974Sjamie for (ii = ij = 1; ii < ip6s; ii++) { 1405195974Sjamie if (IN6_ARE_ADDR_EQUAL(&ip6[ii], 1406195974Sjamie &ppr->pr_ip6[0])) 1407195974Sjamie continue; 1408195974Sjamie for (; ij < ppr->pr_ip6s; ij++) 1409195974Sjamie if (IN6_ARE_ADDR_EQUAL( 1410195974Sjamie &ip6[ii], &ppr->pr_ip6[ij])) 1411195974Sjamie break; 1412195974Sjamie if (ij == ppr->pr_ip6s) 1413195974Sjamie break; 1414192895Sjamie } 1415192895Sjamie if (ij == ppr->pr_ip6s) { 1416192895Sjamie error = EPERM; 1417192895Sjamie goto done_deref_locked; 1418192895Sjamie } 1419192895Sjamie } 1420192895Sjamie } 1421195974Sjamie /* Check for conflicting IP addresses. */ 1422195974Sjamie tppr = ppr; 1423195945Sjamie#ifdef VIMAGE 1424195974Sjamie for (; tppr != &prison0; tppr = tppr->pr_parent) 1425195974Sjamie if (tppr->pr_flags & PR_VNET) 1426195974Sjamie break; 1427195945Sjamie#endif 1428195974Sjamie FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1429195974Sjamie if (tpr == pr || 1430195945Sjamie#ifdef VIMAGE 1431195974Sjamie (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1432195945Sjamie#endif 1433195974Sjamie tpr->pr_uref == 0) { 1434192895Sjamie descend = 0; 1435195974Sjamie continue; 1436195974Sjamie } 1437195974Sjamie if (!(tpr->pr_flags & PR_IP6_USER)) 1438195974Sjamie continue; 1439195974Sjamie descend = 0; 1440195974Sjamie if (tpr->pr_ip6 == NULL || 1441195974Sjamie (ip6s == 1 && tpr->pr_ip6s == 1)) 1442195974Sjamie continue; 1443195974Sjamie for (ii = 0; ii < ip6s; ii++) { 1444195974Sjamie if (_prison_check_ip6(tpr, &ip6[ii]) == 0) { 1445195974Sjamie error = EADDRINUSE; 1446195974Sjamie vfs_opterror(opts, 1447195974Sjamie "IPv6 addresses clash"); 1448195974Sjamie goto done_deref_locked; 1449192895Sjamie } 1450192895Sjamie } 1451191673Sjamie } 1452192895Sjamie } 1453191673Sjamie#endif 1454192895Sjamie onamelen = namelen = 0; 1455192895Sjamie if (name != NULL) { 1456191673Sjamie /* Give a default name of the jid. */ 1457191673Sjamie if (name[0] == '\0') 1458191673Sjamie snprintf(name = numbuf, sizeof(numbuf), "%d", jid); 1459196835Sjamie else if (*namelc == '0' || (strtoul(namelc, &p, 10) != jid && 1460196835Sjamie *p == '\0')) { 1461191673Sjamie error = EINVAL; 1462196835Sjamie vfs_opterror(opts, 1463196835Sjamie "name cannot be numeric (unless it is the jid)"); 1464192895Sjamie goto done_deref_locked; 1465191673Sjamie } 1466191673Sjamie /* 1467192895Sjamie * Make sure the name isn't too long for the prison or its 1468192895Sjamie * children. 1469191673Sjamie */ 1470192895Sjamie onamelen = strlen(pr->pr_name); 1471192895Sjamie namelen = strlen(name); 1472192895Sjamie if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) { 1473192895Sjamie error = ENAMETOOLONG; 1474192895Sjamie goto done_deref_locked; 1475192895Sjamie } 1476192895Sjamie FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 1477192895Sjamie if (strlen(tpr->pr_name) + (namelen - onamelen) >= 1478192895Sjamie sizeof(pr->pr_name)) { 1479192895Sjamie error = ENAMETOOLONG; 1480192895Sjamie goto done_deref_locked; 1481192895Sjamie } 1482192895Sjamie } 1483191673Sjamie } 1484192895Sjamie if (pr_allow & ~ppr->pr_allow) { 1485192895Sjamie error = EPERM; 1486192895Sjamie goto done_deref_locked; 1487192895Sjamie } 1488185435Sbz 1489191673Sjamie /* Set the parameters of the prison. */ 1490191673Sjamie#ifdef INET 1491192895Sjamie redo_ip4 = 0; 1492195974Sjamie if (pr_flags & PR_IP4_USER) { 1493195974Sjamie pr->pr_flags |= PR_IP4; 1494195974Sjamie free(pr->pr_ip4, M_PRISON); 1495195974Sjamie pr->pr_ip4s = ip4s; 1496195974Sjamie pr->pr_ip4 = ip4; 1497195974Sjamie ip4 = NULL; 1498192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1499195945Sjamie#ifdef VIMAGE 1500195945Sjamie if (tpr->pr_flags & PR_VNET) { 1501195945Sjamie descend = 0; 1502195945Sjamie continue; 1503195945Sjamie } 1504195945Sjamie#endif 1505192895Sjamie if (prison_restrict_ip4(tpr, NULL)) { 1506192895Sjamie redo_ip4 = 1; 1507192895Sjamie descend = 0; 1508192895Sjamie } 1509192895Sjamie } 1510185435Sbz } 1511191673Sjamie#endif 1512191673Sjamie#ifdef INET6 1513192895Sjamie redo_ip6 = 0; 1514195974Sjamie if (pr_flags & PR_IP6_USER) { 1515195974Sjamie pr->pr_flags |= PR_IP6; 1516195974Sjamie free(pr->pr_ip6, M_PRISON); 1517195974Sjamie pr->pr_ip6s = ip6s; 1518195974Sjamie pr->pr_ip6 = ip6; 1519195974Sjamie ip6 = NULL; 1520192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1521195945Sjamie#ifdef VIMAGE 1522195945Sjamie if (tpr->pr_flags & PR_VNET) { 1523195945Sjamie descend = 0; 1524195945Sjamie continue; 1525195945Sjamie } 1526195945Sjamie#endif 1527192895Sjamie if (prison_restrict_ip6(tpr, NULL)) { 1528192895Sjamie redo_ip6 = 1; 1529192895Sjamie descend = 0; 1530192895Sjamie } 1531192895Sjamie } 1532191673Sjamie } 1533191673Sjamie#endif 1534192895Sjamie if (gotslevel) { 1535191673Sjamie pr->pr_securelevel = slevel; 1536192895Sjamie /* Set all child jails to be at least this level. */ 1537192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1538192895Sjamie if (tpr->pr_securelevel < slevel) 1539192895Sjamie tpr->pr_securelevel = slevel; 1540192895Sjamie } 1541194762Sjamie if (gotchildmax) { 1542194762Sjamie pr->pr_childmax = childmax; 1543194762Sjamie /* Set all child jails to under this limit. */ 1544194762Sjamie FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level) 1545194762Sjamie if (tpr->pr_childmax > childmax - level) 1546194762Sjamie tpr->pr_childmax = childmax > level 1547194762Sjamie ? childmax - level : 0; 1548194762Sjamie } 1549192895Sjamie if (gotenforce) { 1550192895Sjamie pr->pr_enforce_statfs = enforce; 1551192895Sjamie /* Pass this restriction on to the children. */ 1552192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1553192895Sjamie if (tpr->pr_enforce_statfs < enforce) 1554192895Sjamie tpr->pr_enforce_statfs = enforce; 1555192895Sjamie } 1556192895Sjamie if (name != NULL) { 1557192895Sjamie if (ppr == &prison0) 1558192895Sjamie strlcpy(pr->pr_name, name, sizeof(pr->pr_name)); 1559192895Sjamie else 1560192895Sjamie snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s", 1561192895Sjamie ppr->pr_name, name); 1562192895Sjamie /* Change this component of child names. */ 1563192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1564192895Sjamie bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen, 1565192895Sjamie strlen(tpr->pr_name + onamelen) + 1); 1566192895Sjamie bcopy(pr->pr_name, tpr->pr_name, namelen); 1567192895Sjamie } 1568192895Sjamie } 1569191673Sjamie if (path != NULL) { 1570192895Sjamie /* Try to keep a real-rooted full pathname. */ 1571192895Sjamie if (path[0] == '/' && strcmp(mypr->pr_path, "/")) 1572192895Sjamie snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s", 1573192895Sjamie mypr->pr_path, path); 1574192895Sjamie else 1575192895Sjamie strlcpy(pr->pr_path, path, sizeof(pr->pr_path)); 1576191673Sjamie pr->pr_root = root; 1577191673Sjamie } 1578193066Sjamie if (PR_HOST & ch_flags & ~pr_flags) { 1579193066Sjamie if (pr->pr_flags & PR_HOST) { 1580193066Sjamie /* 1581193066Sjamie * Copy the parent's host info. As with pr_ip4 above, 1582193066Sjamie * the lack of a lock on the parent is not a problem; 1583193066Sjamie * it is always set with allprison_lock at least 1584193066Sjamie * shared, and is held exclusively here. 1585193066Sjamie */ 1586194118Sjamie strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname, 1587194118Sjamie sizeof(pr->pr_hostname)); 1588194118Sjamie strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname, 1589194118Sjamie sizeof(pr->pr_domainname)); 1590194118Sjamie strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid, 1591194118Sjamie sizeof(pr->pr_hostuuid)); 1592193066Sjamie pr->pr_hostid = pr->pr_parent->pr_hostid; 1593193066Sjamie } 1594193066Sjamie } else if (host != NULL || domain != NULL || uuid != NULL || gothid) { 1595193066Sjamie /* Set this prison, and any descendants without PR_HOST. */ 1596193066Sjamie if (host != NULL) 1597194118Sjamie strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname)); 1598193066Sjamie if (domain != NULL) 1599194118Sjamie strlcpy(pr->pr_domainname, domain, 1600194118Sjamie sizeof(pr->pr_domainname)); 1601193066Sjamie if (uuid != NULL) 1602194118Sjamie strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid)); 1603193066Sjamie if (gothid) 1604193066Sjamie pr->pr_hostid = hid; 1605193066Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1606193066Sjamie if (tpr->pr_flags & PR_HOST) 1607193066Sjamie descend = 0; 1608193066Sjamie else { 1609193066Sjamie if (host != NULL) 1610194118Sjamie strlcpy(tpr->pr_hostname, 1611194118Sjamie pr->pr_hostname, 1612194118Sjamie sizeof(tpr->pr_hostname)); 1613193066Sjamie if (domain != NULL) 1614194118Sjamie strlcpy(tpr->pr_domainname, 1615194118Sjamie pr->pr_domainname, 1616194118Sjamie sizeof(tpr->pr_domainname)); 1617193066Sjamie if (uuid != NULL) 1618194118Sjamie strlcpy(tpr->pr_hostuuid, 1619194118Sjamie pr->pr_hostuuid, 1620194118Sjamie sizeof(tpr->pr_hostuuid)); 1621193066Sjamie if (gothid) 1622193066Sjamie tpr->pr_hostid = hid; 1623193066Sjamie } 1624193066Sjamie } 1625193066Sjamie } 1626192895Sjamie if ((tallow = ch_allow & ~pr_allow)) { 1627192895Sjamie /* Clear allow bits in all children. */ 1628192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1629192895Sjamie tpr->pr_allow &= ~tallow; 1630192895Sjamie } 1631192895Sjamie pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow; 1632191673Sjamie /* 1633191673Sjamie * Persistent prisons get an extra reference, and prisons losing their 1634191673Sjamie * persist flag lose that reference. Only do this for existing prisons 1635191673Sjamie * for now, so new ones will remain unseen until after the module 1636191673Sjamie * handlers have completed. 1637191673Sjamie */ 1638191673Sjamie if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) { 1639191673Sjamie if (pr_flags & PR_PERSIST) { 1640191673Sjamie pr->pr_ref++; 1641191673Sjamie pr->pr_uref++; 1642191673Sjamie } else { 1643191673Sjamie pr->pr_ref--; 1644191673Sjamie pr->pr_uref--; 1645191673Sjamie } 1646191673Sjamie } 1647191673Sjamie pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags; 1648191673Sjamie mtx_unlock(&pr->pr_mtx); 1649185435Sbz 1650192895Sjamie /* Locks may have prevented a complete restriction of child IP 1651192895Sjamie * addresses. If so, allocate some more memory and try again. 1652192895Sjamie */ 1653192895Sjamie#ifdef INET 1654192895Sjamie while (redo_ip4) { 1655192895Sjamie ip4s = pr->pr_ip4s; 1656192895Sjamie ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 1657192895Sjamie mtx_lock(&pr->pr_mtx); 1658192895Sjamie redo_ip4 = 0; 1659192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1660195945Sjamie#ifdef VIMAGE 1661195945Sjamie if (tpr->pr_flags & PR_VNET) { 1662195945Sjamie descend = 0; 1663195945Sjamie continue; 1664195945Sjamie } 1665195945Sjamie#endif 1666192895Sjamie if (prison_restrict_ip4(tpr, ip4)) { 1667192895Sjamie if (ip4 != NULL) 1668192895Sjamie ip4 = NULL; 1669192895Sjamie else 1670192895Sjamie redo_ip4 = 1; 1671192895Sjamie } 1672192895Sjamie } 1673192895Sjamie mtx_unlock(&pr->pr_mtx); 1674192895Sjamie } 1675192895Sjamie#endif 1676192895Sjamie#ifdef INET6 1677192895Sjamie while (redo_ip6) { 1678192895Sjamie ip6s = pr->pr_ip6s; 1679192895Sjamie ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 1680192895Sjamie mtx_lock(&pr->pr_mtx); 1681192895Sjamie redo_ip6 = 0; 1682192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1683195945Sjamie#ifdef VIMAGE 1684195945Sjamie if (tpr->pr_flags & PR_VNET) { 1685195945Sjamie descend = 0; 1686195945Sjamie continue; 1687195945Sjamie } 1688195945Sjamie#endif 1689192895Sjamie if (prison_restrict_ip6(tpr, ip6)) { 1690192895Sjamie if (ip6 != NULL) 1691192895Sjamie ip6 = NULL; 1692192895Sjamie else 1693192895Sjamie redo_ip6 = 1; 1694192895Sjamie } 1695192895Sjamie } 1696192895Sjamie mtx_unlock(&pr->pr_mtx); 1697192895Sjamie } 1698192895Sjamie#endif 1699192895Sjamie 1700191673Sjamie /* Let the modules do their work. */ 1701191673Sjamie sx_downgrade(&allprison_lock); 1702191673Sjamie if (created) { 1703191673Sjamie error = osd_jail_call(pr, PR_METHOD_CREATE, opts); 1704191673Sjamie if (error) { 1705191673Sjamie prison_deref(pr, PD_LIST_SLOCKED); 1706191673Sjamie goto done_errmsg; 1707191673Sjamie } 1708191673Sjamie } 1709191673Sjamie error = osd_jail_call(pr, PR_METHOD_SET, opts); 1710191673Sjamie if (error) { 1711191673Sjamie prison_deref(pr, created 1712191673Sjamie ? PD_LIST_SLOCKED 1713191673Sjamie : PD_DEREF | PD_LIST_SLOCKED); 1714191673Sjamie goto done_errmsg; 1715191673Sjamie } 1716191673Sjamie 1717191673Sjamie /* Attach this process to the prison if requested. */ 1718191673Sjamie if (flags & JAIL_ATTACH) { 1719191673Sjamie mtx_lock(&pr->pr_mtx); 1720191673Sjamie error = do_jail_attach(td, pr); 1721191673Sjamie if (error) { 1722191673Sjamie vfs_opterror(opts, "attach failed"); 1723191673Sjamie if (!created) 1724191673Sjamie prison_deref(pr, PD_DEREF); 1725191673Sjamie goto done_errmsg; 1726191673Sjamie } 1727191673Sjamie } 1728191673Sjamie 1729191673Sjamie /* 1730191673Sjamie * Now that it is all there, drop the temporary reference from existing 1731191673Sjamie * prisons. Or add a reference to newly created persistent prisons 1732191673Sjamie * (which was not done earlier so that the prison would not be publicly 1733191673Sjamie * visible). 1734191673Sjamie */ 1735191673Sjamie if (!created) { 1736191673Sjamie prison_deref(pr, (flags & JAIL_ATTACH) 1737191673Sjamie ? PD_DEREF 1738191673Sjamie : PD_DEREF | PD_LIST_SLOCKED); 1739191673Sjamie } else { 1740191673Sjamie if (pr_flags & PR_PERSIST) { 1741191673Sjamie mtx_lock(&pr->pr_mtx); 1742191673Sjamie pr->pr_ref++; 1743191673Sjamie pr->pr_uref++; 1744191673Sjamie mtx_unlock(&pr->pr_mtx); 1745191673Sjamie } 1746191673Sjamie if (!(flags & JAIL_ATTACH)) 1747191673Sjamie sx_sunlock(&allprison_lock); 1748191673Sjamie } 1749191673Sjamie td->td_retval[0] = pr->pr_id; 1750191673Sjamie goto done_errmsg; 1751191673Sjamie 1752192895Sjamie done_deref_locked: 1753192895Sjamie prison_deref(pr, created 1754192895Sjamie ? PD_LOCKED | PD_LIST_XLOCKED 1755192895Sjamie : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 1756192895Sjamie goto done_releroot; 1757191673Sjamie done_unlock_list: 1758191673Sjamie sx_xunlock(&allprison_lock); 1759191673Sjamie done_releroot: 1760191673Sjamie if (root != NULL) { 1761191673Sjamie vfslocked = VFS_LOCK_GIANT(root->v_mount); 1762191673Sjamie vrele(root); 1763191673Sjamie VFS_UNLOCK_GIANT(vfslocked); 1764191673Sjamie } 1765191673Sjamie done_errmsg: 1766191673Sjamie if (error) { 1767191673Sjamie vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 1768191673Sjamie if (errmsg_len > 0) { 1769191673Sjamie errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1; 1770191673Sjamie if (errmsg_pos > 0) { 1771191673Sjamie if (optuio->uio_segflg == UIO_SYSSPACE) 1772191673Sjamie bcopy(errmsg, 1773191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 1774191673Sjamie errmsg_len); 1775191673Sjamie else 1776191673Sjamie copyout(errmsg, 1777191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 1778191673Sjamie errmsg_len); 1779191673Sjamie } 1780191673Sjamie } 1781191673Sjamie } 1782191673Sjamie done_free: 1783191673Sjamie#ifdef INET 1784191673Sjamie free(ip4, M_PRISON); 1785191673Sjamie#endif 1786191673Sjamie#ifdef INET6 1787191673Sjamie free(ip6, M_PRISON); 1788191673Sjamie#endif 1789191673Sjamie vfs_freeopts(opts); 1790191673Sjamie return (error); 1791191673Sjamie} 1792191673Sjamie 1793191673Sjamie 179482710Sdillon/* 1795191673Sjamie * struct jail_get_args { 1796191673Sjamie * struct iovec *iovp; 1797191673Sjamie * unsigned int iovcnt; 1798191673Sjamie * int flags; 1799114168Smike * }; 180082710Sdillon */ 180146155Sphkint 1802191673Sjamiejail_get(struct thread *td, struct jail_get_args *uap) 180346155Sphk{ 1804191673Sjamie struct uio *auio; 1805185435Sbz int error; 1806185435Sbz 1807191673Sjamie /* Check that we have an even number of iovecs. */ 1808191673Sjamie if (uap->iovcnt & 1) 1809191673Sjamie return (EINVAL); 1810191673Sjamie 1811191673Sjamie error = copyinuio(uap->iovp, uap->iovcnt, &auio); 1812185435Sbz if (error) 1813185435Sbz return (error); 1814191673Sjamie error = kern_jail_get(td, auio, uap->flags); 1815191673Sjamie if (error == 0) 1816191673Sjamie error = copyout(auio->uio_iov, uap->iovp, 1817191673Sjamie uap->iovcnt * sizeof (struct iovec)); 1818191673Sjamie free(auio, M_IOV); 1819191673Sjamie return (error); 1820191673Sjamie} 1821185435Sbz 1822191673Sjamieint 1823191673Sjamiekern_jail_get(struct thread *td, struct uio *optuio, int flags) 1824191673Sjamie{ 1825192895Sjamie struct prison *pr, *mypr; 1826191673Sjamie struct vfsopt *opt; 1827191673Sjamie struct vfsoptlist *opts; 1828191673Sjamie char *errmsg, *name; 1829192895Sjamie int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos; 1830185435Sbz 1831191673Sjamie if (flags & ~JAIL_GET_MASK) 1832191673Sjamie return (EINVAL); 1833185435Sbz 1834191673Sjamie /* Get the parameter list. */ 1835191673Sjamie error = vfs_buildopts(optuio, &opts); 1836191673Sjamie if (error) 1837191673Sjamie return (error); 1838191673Sjamie errmsg_pos = vfs_getopt_pos(opts, "errmsg"); 1839192895Sjamie mypr = td->td_ucred->cr_prison; 1840185435Sbz 1841191673Sjamie /* 1842191673Sjamie * Find the prison specified by one of: lastjid, jid, name. 1843191673Sjamie */ 1844191673Sjamie sx_slock(&allprison_lock); 1845191673Sjamie error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid)); 1846191673Sjamie if (error == 0) { 1847191673Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) { 1848192895Sjamie if (pr->pr_id > jid && prison_ischild(mypr, pr)) { 1849191673Sjamie mtx_lock(&pr->pr_mtx); 1850191673Sjamie if (pr->pr_ref > 0 && 1851191673Sjamie (pr->pr_uref > 0 || (flags & JAIL_DYING))) 1852191673Sjamie break; 1853191673Sjamie mtx_unlock(&pr->pr_mtx); 1854191673Sjamie } 1855191673Sjamie } 1856191673Sjamie if (pr != NULL) 1857191673Sjamie goto found_prison; 1858191673Sjamie error = ENOENT; 1859191673Sjamie vfs_opterror(opts, "no jail after %d", jid); 1860191673Sjamie goto done_unlock_list; 1861191673Sjamie } else if (error != ENOENT) 1862191673Sjamie goto done_unlock_list; 1863185435Sbz 1864191673Sjamie error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 1865191673Sjamie if (error == 0) { 1866191673Sjamie if (jid != 0) { 1867192895Sjamie pr = prison_find_child(mypr, jid); 1868191673Sjamie if (pr != NULL) { 1869191673Sjamie if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 1870191673Sjamie mtx_unlock(&pr->pr_mtx); 1871191673Sjamie error = ENOENT; 1872191673Sjamie vfs_opterror(opts, "jail %d is dying", 1873191673Sjamie jid); 1874191673Sjamie goto done_unlock_list; 1875191673Sjamie } 1876191673Sjamie goto found_prison; 1877191673Sjamie } 1878191673Sjamie error = ENOENT; 1879191673Sjamie vfs_opterror(opts, "jail %d not found", jid); 1880191673Sjamie goto done_unlock_list; 1881191673Sjamie } 1882191673Sjamie } else if (error != ENOENT) 1883191673Sjamie goto done_unlock_list; 188446155Sphk 1885191673Sjamie error = vfs_getopt(opts, "name", (void **)&name, &len); 1886191673Sjamie if (error == 0) { 1887191673Sjamie if (len == 0 || name[len - 1] != '\0') { 1888191673Sjamie error = EINVAL; 1889191673Sjamie goto done_unlock_list; 1890191673Sjamie } 1891192895Sjamie pr = prison_find_name(mypr, name); 1892191673Sjamie if (pr != NULL) { 1893191673Sjamie if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 1894191673Sjamie mtx_unlock(&pr->pr_mtx); 1895191673Sjamie error = ENOENT; 1896191673Sjamie vfs_opterror(opts, "jail \"%s\" is dying", 1897191673Sjamie name); 1898191673Sjamie goto done_unlock_list; 1899191673Sjamie } 1900191673Sjamie goto found_prison; 1901191673Sjamie } 1902191673Sjamie error = ENOENT; 1903191673Sjamie vfs_opterror(opts, "jail \"%s\" not found", name); 1904191673Sjamie goto done_unlock_list; 1905191673Sjamie } else if (error != ENOENT) 1906191673Sjamie goto done_unlock_list; 1907185435Sbz 1908191673Sjamie vfs_opterror(opts, "no jail specified"); 1909191673Sjamie error = ENOENT; 1910191673Sjamie goto done_unlock_list; 1911191673Sjamie 1912191673Sjamie found_prison: 1913191673Sjamie /* Get the parameters of the prison. */ 1914191673Sjamie pr->pr_ref++; 1915191673Sjamie locked = PD_LOCKED; 1916191673Sjamie td->td_retval[0] = pr->pr_id; 1917191673Sjamie error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id)); 1918191673Sjamie if (error != 0 && error != ENOENT) 1919191673Sjamie goto done_deref; 1920192895Sjamie i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id; 1921192895Sjamie error = vfs_setopt(opts, "parent", &i, sizeof(i)); 1922191673Sjamie if (error != 0 && error != ENOENT) 1923191673Sjamie goto done_deref; 1924192895Sjamie error = vfs_setopts(opts, "name", prison_name(mypr, pr)); 1925192895Sjamie if (error != 0 && error != ENOENT) 1926192895Sjamie goto done_deref; 1927192895Sjamie error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id, 1928191673Sjamie sizeof(pr->pr_cpuset->cs_id)); 1929191673Sjamie if (error != 0 && error != ENOENT) 1930191673Sjamie goto done_deref; 1931192895Sjamie error = vfs_setopts(opts, "path", prison_path(mypr, pr)); 1932191673Sjamie if (error != 0 && error != ENOENT) 1933191673Sjamie goto done_deref; 1934191673Sjamie#ifdef INET 1935191673Sjamie error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4, 1936191673Sjamie pr->pr_ip4s * sizeof(*pr->pr_ip4)); 1937191673Sjamie if (error != 0 && error != ENOENT) 1938191673Sjamie goto done_deref; 1939191673Sjamie#endif 1940191673Sjamie#ifdef INET6 1941191673Sjamie error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6, 1942191673Sjamie pr->pr_ip6s * sizeof(*pr->pr_ip6)); 1943191673Sjamie if (error != 0 && error != ENOENT) 1944191673Sjamie goto done_deref; 1945191673Sjamie#endif 1946191673Sjamie error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel, 1947191673Sjamie sizeof(pr->pr_securelevel)); 1948191673Sjamie if (error != 0 && error != ENOENT) 1949191673Sjamie goto done_deref; 1950194762Sjamie error = vfs_setopt(opts, "children.cur", &pr->pr_childcount, 1951194762Sjamie sizeof(pr->pr_childcount)); 1952194762Sjamie if (error != 0 && error != ENOENT) 1953194762Sjamie goto done_deref; 1954194762Sjamie error = vfs_setopt(opts, "children.max", &pr->pr_childmax, 1955194762Sjamie sizeof(pr->pr_childmax)); 1956194762Sjamie if (error != 0 && error != ENOENT) 1957194762Sjamie goto done_deref; 1958194118Sjamie error = vfs_setopts(opts, "host.hostname", pr->pr_hostname); 1959191673Sjamie if (error != 0 && error != ENOENT) 1960191673Sjamie goto done_deref; 1961194118Sjamie error = vfs_setopts(opts, "host.domainname", pr->pr_domainname); 1962193066Sjamie if (error != 0 && error != ENOENT) 1963193066Sjamie goto done_deref; 1964194118Sjamie error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid); 1965193066Sjamie if (error != 0 && error != ENOENT) 1966193066Sjamie goto done_deref; 1967205014Snwhitehorn#ifdef COMPAT_FREEBSD32 1968205014Snwhitehorn if (td->td_proc->p_sysent->sv_flags & SV_ILP32) { 1969193066Sjamie uint32_t hid32 = pr->pr_hostid; 1970193066Sjamie 1971193066Sjamie error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32)); 1972193066Sjamie } else 1973193066Sjamie#endif 1974193066Sjamie error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid, 1975193066Sjamie sizeof(pr->pr_hostid)); 1976193066Sjamie if (error != 0 && error != ENOENT) 1977193066Sjamie goto done_deref; 1978192895Sjamie error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs, 1979192895Sjamie sizeof(pr->pr_enforce_statfs)); 1980191673Sjamie if (error != 0 && error != ENOENT) 1981191673Sjamie goto done_deref; 1982192895Sjamie for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 1983192895Sjamie fi++) { 1984192895Sjamie if (pr_flag_names[fi] == NULL) 1985192895Sjamie continue; 1986192895Sjamie i = (pr->pr_flags & (1 << fi)) ? 1 : 0; 1987192895Sjamie error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i)); 1988192895Sjamie if (error != 0 && error != ENOENT) 1989192895Sjamie goto done_deref; 1990192895Sjamie i = !i; 1991192895Sjamie error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i)); 1992192895Sjamie if (error != 0 && error != ENOENT) 1993192895Sjamie goto done_deref; 1994192895Sjamie } 1995195870Sjamie for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 1996195870Sjamie fi++) { 1997195870Sjamie i = pr->pr_flags & 1998195870Sjamie (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new); 1999195870Sjamie i = pr_flag_jailsys[fi].disable && 2000195870Sjamie (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE 2001195870Sjamie : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW 2002195870Sjamie : JAIL_SYS_INHERIT; 2003195870Sjamie error = 2004195870Sjamie vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i)); 2005195870Sjamie if (error != 0 && error != ENOENT) 2006195870Sjamie goto done_deref; 2007195870Sjamie } 2008192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 2009192895Sjamie fi++) { 2010192895Sjamie if (pr_allow_names[fi] == NULL) 2011192895Sjamie continue; 2012192895Sjamie i = (pr->pr_allow & (1 << fi)) ? 1 : 0; 2013192895Sjamie error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i)); 2014192895Sjamie if (error != 0 && error != ENOENT) 2015192895Sjamie goto done_deref; 2016192895Sjamie i = !i; 2017192895Sjamie error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i)); 2018192895Sjamie if (error != 0 && error != ENOENT) 2019192895Sjamie goto done_deref; 2020192895Sjamie } 2021191673Sjamie i = (pr->pr_uref == 0); 2022191673Sjamie error = vfs_setopt(opts, "dying", &i, sizeof(i)); 2023191673Sjamie if (error != 0 && error != ENOENT) 2024191673Sjamie goto done_deref; 2025191673Sjamie i = !i; 2026191673Sjamie error = vfs_setopt(opts, "nodying", &i, sizeof(i)); 2027191673Sjamie if (error != 0 && error != ENOENT) 2028191673Sjamie goto done_deref; 2029191673Sjamie 2030191673Sjamie /* Get the module parameters. */ 2031191673Sjamie mtx_unlock(&pr->pr_mtx); 2032191673Sjamie locked = 0; 2033191673Sjamie error = osd_jail_call(pr, PR_METHOD_GET, opts); 203446155Sphk if (error) 2035191673Sjamie goto done_deref; 2036191673Sjamie prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED); 203784828Sjhb 2038191673Sjamie /* By now, all parameters should have been noted. */ 2039191673Sjamie TAILQ_FOREACH(opt, opts, link) { 2040191673Sjamie if (!opt->seen && strcmp(opt->name, "errmsg")) { 2041191673Sjamie error = EINVAL; 2042191673Sjamie vfs_opterror(opts, "unknown parameter: %s", opt->name); 2043191673Sjamie goto done_errmsg; 2044191673Sjamie } 2045185435Sbz } 2046191673Sjamie 2047191673Sjamie /* Write the fetched parameters back to userspace. */ 2048191673Sjamie error = 0; 2049191673Sjamie TAILQ_FOREACH(opt, opts, link) { 2050191673Sjamie if (opt->pos >= 0 && opt->pos != errmsg_pos) { 2051191673Sjamie pos = 2 * opt->pos + 1; 2052191673Sjamie optuio->uio_iov[pos].iov_len = opt->len; 2053191673Sjamie if (opt->value != NULL) { 2054191673Sjamie if (optuio->uio_segflg == UIO_SYSSPACE) { 2055191673Sjamie bcopy(opt->value, 2056191673Sjamie optuio->uio_iov[pos].iov_base, 2057191673Sjamie opt->len); 2058191673Sjamie } else { 2059191673Sjamie error = copyout(opt->value, 2060191673Sjamie optuio->uio_iov[pos].iov_base, 2061191673Sjamie opt->len); 2062191673Sjamie if (error) 2063191673Sjamie break; 2064191673Sjamie } 2065191673Sjamie } 2066191673Sjamie } 2067185435Sbz } 2068191673Sjamie goto done_errmsg; 2069191673Sjamie 2070191673Sjamie done_deref: 2071191673Sjamie prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED); 2072191673Sjamie goto done_errmsg; 2073191673Sjamie 2074191673Sjamie done_unlock_list: 2075191673Sjamie sx_sunlock(&allprison_lock); 2076191673Sjamie done_errmsg: 2077191673Sjamie if (error && errmsg_pos >= 0) { 2078191673Sjamie vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 2079191673Sjamie errmsg_pos = 2 * errmsg_pos + 1; 2080191673Sjamie if (errmsg_len > 0) { 2081191673Sjamie if (optuio->uio_segflg == UIO_SYSSPACE) 2082191673Sjamie bcopy(errmsg, 2083191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 2084191673Sjamie errmsg_len); 2085191673Sjamie else 2086191673Sjamie copyout(errmsg, 2087191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 2088191673Sjamie errmsg_len); 2089191673Sjamie } 2090185435Sbz } 2091191673Sjamie vfs_freeopts(opts); 2092191673Sjamie return (error); 2093191673Sjamie} 2094113275Smike 2095192895Sjamie 2096191673Sjamie/* 2097191673Sjamie * struct jail_remove_args { 2098191673Sjamie * int jid; 2099191673Sjamie * }; 2100191673Sjamie */ 2101191673Sjamieint 2102191673Sjamiejail_remove(struct thread *td, struct jail_remove_args *uap) 2103191673Sjamie{ 2104192895Sjamie struct prison *pr, *cpr, *lpr, *tpr; 2105192895Sjamie int descend, error; 2106185435Sbz 2107191673Sjamie error = priv_check(td, PRIV_JAIL_REMOVE); 2108185435Sbz if (error) 2109191673Sjamie return (error); 2110185435Sbz 2111185435Sbz sx_xlock(&allprison_lock); 2112192895Sjamie pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2113191673Sjamie if (pr == NULL) { 2114185435Sbz sx_xunlock(&allprison_lock); 2115191673Sjamie return (EINVAL); 2116185435Sbz } 2117185435Sbz 2118192895Sjamie /* Remove all descendants of this prison, then remove this prison. */ 2119192895Sjamie pr->pr_ref++; 2120192895Sjamie pr->pr_flags |= PR_REMOVE; 2121192895Sjamie if (!LIST_EMPTY(&pr->pr_children)) { 2122192895Sjamie mtx_unlock(&pr->pr_mtx); 2123192895Sjamie lpr = NULL; 2124192895Sjamie FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 2125192895Sjamie mtx_lock(&cpr->pr_mtx); 2126192895Sjamie if (cpr->pr_ref > 0) { 2127192895Sjamie tpr = cpr; 2128192895Sjamie cpr->pr_ref++; 2129192895Sjamie cpr->pr_flags |= PR_REMOVE; 2130192895Sjamie } else { 2131192895Sjamie /* Already removed - do not do it again. */ 2132192895Sjamie tpr = NULL; 2133192895Sjamie } 2134192895Sjamie mtx_unlock(&cpr->pr_mtx); 2135192895Sjamie if (lpr != NULL) { 2136192895Sjamie mtx_lock(&lpr->pr_mtx); 2137192895Sjamie prison_remove_one(lpr); 2138192895Sjamie sx_xlock(&allprison_lock); 2139192895Sjamie } 2140192895Sjamie lpr = tpr; 2141192895Sjamie } 2142192895Sjamie if (lpr != NULL) { 2143192895Sjamie mtx_lock(&lpr->pr_mtx); 2144192895Sjamie prison_remove_one(lpr); 2145192895Sjamie sx_xlock(&allprison_lock); 2146192895Sjamie } 2147192895Sjamie mtx_lock(&pr->pr_mtx); 2148192895Sjamie } 2149192895Sjamie prison_remove_one(pr); 2150192895Sjamie return (0); 2151192895Sjamie} 2152192895Sjamie 2153192895Sjamiestatic void 2154192895Sjamieprison_remove_one(struct prison *pr) 2155192895Sjamie{ 2156192895Sjamie struct proc *p; 2157192895Sjamie int deuref; 2158192895Sjamie 2159191673Sjamie /* If the prison was persistent, it is not anymore. */ 2160191673Sjamie deuref = 0; 2161191673Sjamie if (pr->pr_flags & PR_PERSIST) { 2162191673Sjamie pr->pr_ref--; 2163191673Sjamie deuref = PD_DEUREF; 2164191673Sjamie pr->pr_flags &= ~PR_PERSIST; 2165179881Sdelphij } 2166113275Smike 2167192895Sjamie /* 2168192895Sjamie * jail_remove added a reference. If that's the only one, remove 2169192895Sjamie * the prison now. 2170192895Sjamie */ 2171192895Sjamie KASSERT(pr->pr_ref > 0, 2172192895Sjamie ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id)); 2173192895Sjamie if (pr->pr_ref == 1) { 2174191673Sjamie prison_deref(pr, 2175191673Sjamie deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 2176192895Sjamie return; 2177191673Sjamie } 2178191673Sjamie 2179113275Smike mtx_unlock(&pr->pr_mtx); 2180191673Sjamie sx_xunlock(&allprison_lock); 2181191673Sjamie /* 2182191673Sjamie * Kill all processes unfortunate enough to be attached to this prison. 2183191673Sjamie */ 2184191673Sjamie sx_slock(&allproc_lock); 2185191673Sjamie LIST_FOREACH(p, &allproc, p_list) { 2186191673Sjamie PROC_LOCK(p); 2187191673Sjamie if (p->p_state != PRS_NEW && p->p_ucred && 2188191673Sjamie p->p_ucred->cr_prison == pr) 2189191673Sjamie psignal(p, SIGKILL); 2190191673Sjamie PROC_UNLOCK(p); 2191191673Sjamie } 2192191673Sjamie sx_sunlock(&allproc_lock); 2193192895Sjamie /* Remove the temporary reference added by jail_remove. */ 2194191673Sjamie prison_deref(pr, deuref | PD_DEREF); 2195113275Smike} 2196113275Smike 2197190466Sjamie 2198113275Smike/* 2199114168Smike * struct jail_attach_args { 2200114168Smike * int jid; 2201114168Smike * }; 2202113275Smike */ 2203113275Smikeint 2204114168Smikejail_attach(struct thread *td, struct jail_attach_args *uap) 2205113275Smike{ 2206113275Smike struct prison *pr; 2207191673Sjamie int error; 2208167309Spjd 2209164032Srwatson error = priv_check(td, PRIV_JAIL_ATTACH); 2210126023Snectar if (error) 2211126023Snectar return (error); 2212126023Snectar 2213168401Spjd sx_slock(&allprison_lock); 2214192895Sjamie pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2215113275Smike if (pr == NULL) { 2216168401Spjd sx_sunlock(&allprison_lock); 2217113275Smike return (EINVAL); 2218113275Smike } 2219185435Sbz 2220185435Sbz /* 2221185435Sbz * Do not allow a process to attach to a prison that is not 2222191673Sjamie * considered to be "alive". 2223185435Sbz */ 2224191673Sjamie if (pr->pr_uref == 0) { 2225185435Sbz mtx_unlock(&pr->pr_mtx); 2226185435Sbz sx_sunlock(&allprison_lock); 2227185435Sbz return (EINVAL); 2228185435Sbz } 2229191673Sjamie 2230191673Sjamie return (do_jail_attach(td, pr)); 2231191673Sjamie} 2232191673Sjamie 2233191673Sjamiestatic int 2234191673Sjamiedo_jail_attach(struct thread *td, struct prison *pr) 2235191673Sjamie{ 2236192895Sjamie struct prison *ppr; 2237191673Sjamie struct proc *p; 2238191673Sjamie struct ucred *newcred, *oldcred; 2239191673Sjamie int vfslocked, error; 2240191673Sjamie 2241191673Sjamie /* 2242191673Sjamie * XXX: Note that there is a slight race here if two threads 2243191673Sjamie * in the same privileged process attempt to attach to two 2244191673Sjamie * different jails at the same time. It is important for 2245191673Sjamie * user processes not to do this, or they might end up with 2246191673Sjamie * a process root from one prison, but attached to the jail 2247191673Sjamie * of another. 2248191673Sjamie */ 2249113275Smike pr->pr_ref++; 2250191673Sjamie pr->pr_uref++; 2251113275Smike mtx_unlock(&pr->pr_mtx); 2252191673Sjamie 2253191673Sjamie /* Let modules do whatever they need to prepare for attaching. */ 2254191673Sjamie error = osd_jail_call(pr, PR_METHOD_ATTACH, td); 2255191673Sjamie if (error) { 2256191673Sjamie prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED); 2257191673Sjamie return (error); 2258191673Sjamie } 2259168401Spjd sx_sunlock(&allprison_lock); 2260113275Smike 2261185435Sbz /* 2262185435Sbz * Reparent the newly attached process to this jail. 2263185435Sbz */ 2264192895Sjamie ppr = td->td_ucred->cr_prison; 2265191673Sjamie p = td->td_proc; 2266185435Sbz error = cpuset_setproc_update_set(p, pr->pr_cpuset); 2267185435Sbz if (error) 2268191673Sjamie goto e_revert_osd; 2269185435Sbz 2270150652Scsjp vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount); 2271175202Sattilio vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY); 2272113275Smike if ((error = change_dir(pr->pr_root, td)) != 0) 2273113275Smike goto e_unlock; 2274113275Smike#ifdef MAC 2275172930Srwatson if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root))) 2276113275Smike goto e_unlock; 2277113275Smike#endif 2278175294Sattilio VOP_UNLOCK(pr->pr_root, 0); 2279191673Sjamie if ((error = change_root(pr->pr_root, td))) 2280191673Sjamie goto e_unlock_giant; 2281150652Scsjp VFS_UNLOCK_GIANT(vfslocked); 2282113275Smike 228384828Sjhb newcred = crget(); 228484828Sjhb PROC_LOCK(p); 228584828Sjhb oldcred = p->p_ucred; 2286113275Smike setsugid(p); 228784828Sjhb crcopy(newcred, oldcred); 2288113630Sjhb newcred->cr_prison = pr; 228984828Sjhb p->p_ucred = newcred; 229084828Sjhb PROC_UNLOCK(p); 229184828Sjhb crfree(oldcred); 2292192895Sjamie prison_deref(ppr, PD_DEREF | PD_DEUREF); 229346155Sphk return (0); 2294191673Sjamie e_unlock: 2295175294Sattilio VOP_UNLOCK(pr->pr_root, 0); 2296191673Sjamie e_unlock_giant: 2297150652Scsjp VFS_UNLOCK_GIANT(vfslocked); 2298191673Sjamie e_revert_osd: 2299191673Sjamie /* Tell modules this thread is still in its old jail after all. */ 2300192895Sjamie (void)osd_jail_call(ppr, PR_METHOD_ATTACH, td); 2301191673Sjamie prison_deref(pr, PD_DEREF | PD_DEUREF); 230246155Sphk return (error); 230346155Sphk} 230446155Sphk 2305192895Sjamie 2306113275Smike/* 2307113275Smike * Returns a locked prison instance, or NULL on failure. 2308113275Smike */ 2309168399Spjdstruct prison * 2310113275Smikeprison_find(int prid) 2311113275Smike{ 2312113275Smike struct prison *pr; 2313113275Smike 2314168401Spjd sx_assert(&allprison_lock, SX_LOCKED); 2315191673Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) { 2316113275Smike if (pr->pr_id == prid) { 2317113275Smike mtx_lock(&pr->pr_mtx); 2318191673Sjamie if (pr->pr_ref > 0) 2319191673Sjamie return (pr); 2320191673Sjamie mtx_unlock(&pr->pr_mtx); 2321113275Smike } 2322113275Smike } 2323113275Smike return (NULL); 2324113275Smike} 2325113275Smike 2326191673Sjamie/* 2327192895Sjamie * Find a prison that is a descendant of mypr. Returns a locked prison or NULL. 2328191673Sjamie */ 2329191673Sjamiestruct prison * 2330192895Sjamieprison_find_child(struct prison *mypr, int prid) 2331191673Sjamie{ 2332192895Sjamie struct prison *pr; 2333192895Sjamie int descend; 2334192895Sjamie 2335192895Sjamie sx_assert(&allprison_lock, SX_LOCKED); 2336192895Sjamie FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 2337192895Sjamie if (pr->pr_id == prid) { 2338192895Sjamie mtx_lock(&pr->pr_mtx); 2339192895Sjamie if (pr->pr_ref > 0) 2340192895Sjamie return (pr); 2341192895Sjamie mtx_unlock(&pr->pr_mtx); 2342192895Sjamie } 2343192895Sjamie } 2344192895Sjamie return (NULL); 2345192895Sjamie} 2346192895Sjamie 2347192895Sjamie/* 2348192895Sjamie * Look for the name relative to mypr. Returns a locked prison or NULL. 2349192895Sjamie */ 2350192895Sjamiestruct prison * 2351192895Sjamieprison_find_name(struct prison *mypr, const char *name) 2352192895Sjamie{ 2353191673Sjamie struct prison *pr, *deadpr; 2354192895Sjamie size_t mylen; 2355192895Sjamie int descend; 2356191673Sjamie 2357191673Sjamie sx_assert(&allprison_lock, SX_LOCKED); 2358192895Sjamie mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1; 2359191673Sjamie again: 2360191673Sjamie deadpr = NULL; 2361192895Sjamie FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 2362192895Sjamie if (!strcmp(pr->pr_name + mylen, name)) { 2363191673Sjamie mtx_lock(&pr->pr_mtx); 2364191673Sjamie if (pr->pr_ref > 0) { 2365191673Sjamie if (pr->pr_uref > 0) 2366191673Sjamie return (pr); 2367191673Sjamie deadpr = pr; 2368191673Sjamie } 2369191673Sjamie mtx_unlock(&pr->pr_mtx); 2370191673Sjamie } 2371191673Sjamie } 2372192895Sjamie /* There was no valid prison - perhaps there was a dying one. */ 2373191673Sjamie if (deadpr != NULL) { 2374191673Sjamie mtx_lock(&deadpr->pr_mtx); 2375191673Sjamie if (deadpr->pr_ref == 0) { 2376191673Sjamie mtx_unlock(&deadpr->pr_mtx); 2377191673Sjamie goto again; 2378191673Sjamie } 2379191673Sjamie } 2380191673Sjamie return (deadpr); 2381191673Sjamie} 2382191673Sjamie 2383191673Sjamie/* 2384192895Sjamie * See if a prison has the specific flag set. 2385192895Sjamie */ 2386192895Sjamieint 2387192895Sjamieprison_flag(struct ucred *cred, unsigned flag) 2388192895Sjamie{ 2389192895Sjamie 2390192895Sjamie /* This is an atomic read, so no locking is necessary. */ 2391192895Sjamie return (cred->cr_prison->pr_flags & flag); 2392192895Sjamie} 2393192895Sjamie 2394192895Sjamieint 2395192895Sjamieprison_allow(struct ucred *cred, unsigned flag) 2396192895Sjamie{ 2397192895Sjamie 2398192895Sjamie /* This is an atomic read, so no locking is necessary. */ 2399192895Sjamie return (cred->cr_prison->pr_allow & flag); 2400192895Sjamie} 2401192895Sjamie 2402192895Sjamie/* 2403191673Sjamie * Remove a prison reference. If that was the last reference, remove the 2404191673Sjamie * prison itself - but not in this context in case there are locks held. 2405191673Sjamie */ 240672786Srwatsonvoid 2407185029Spjdprison_free_locked(struct prison *pr) 240872786Srwatson{ 240972786Srwatson 2410185029Spjd mtx_assert(&pr->pr_mtx, MA_OWNED); 241172786Srwatson pr->pr_ref--; 241272786Srwatson if (pr->pr_ref == 0) { 2413168483Spjd mtx_unlock(&pr->pr_mtx); 2414124882Srwatson TASK_INIT(&pr->pr_task, 0, prison_complete, pr); 2415144660Sjeff taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 241687275Srwatson return; 241772786Srwatson } 241887275Srwatson mtx_unlock(&pr->pr_mtx); 241972786Srwatson} 242072786Srwatson 2421185029Spjdvoid 2422185029Spjdprison_free(struct prison *pr) 2423185029Spjd{ 2424185029Spjd 2425185029Spjd mtx_lock(&pr->pr_mtx); 2426185029Spjd prison_free_locked(pr); 2427185029Spjd} 2428185029Spjd 2429124882Srwatsonstatic void 2430124882Srwatsonprison_complete(void *context, int pending) 2431124882Srwatson{ 2432191673Sjamie 2433191673Sjamie prison_deref((struct prison *)context, 0); 2434191673Sjamie} 2435191673Sjamie 2436191673Sjamie/* 2437191673Sjamie * Remove a prison reference (usually). This internal version assumes no 2438191673Sjamie * mutexes are held, except perhaps the prison itself. If there are no more 2439191673Sjamie * references, release and delist the prison. On completion, the prison lock 2440191673Sjamie * and the allprison lock are both unlocked. 2441191673Sjamie */ 2442191673Sjamiestatic void 2443191673Sjamieprison_deref(struct prison *pr, int flags) 2444191673Sjamie{ 2445192895Sjamie struct prison *ppr, *tpr; 2446150652Scsjp int vfslocked; 2447124882Srwatson 2448191673Sjamie if (!(flags & PD_LOCKED)) 2449191673Sjamie mtx_lock(&pr->pr_mtx); 2450192895Sjamie /* Decrement the user references in a separate loop. */ 2451191673Sjamie if (flags & PD_DEUREF) { 2452192895Sjamie for (tpr = pr;; tpr = tpr->pr_parent) { 2453192895Sjamie if (tpr != pr) 2454192895Sjamie mtx_lock(&tpr->pr_mtx); 2455192895Sjamie if (--tpr->pr_uref > 0) 2456192895Sjamie break; 2457192895Sjamie KASSERT(tpr != &prison0, ("prison0 pr_uref=0")); 2458192895Sjamie mtx_unlock(&tpr->pr_mtx); 2459192895Sjamie } 2460191673Sjamie /* Done if there were only user references to remove. */ 2461191673Sjamie if (!(flags & PD_DEREF)) { 2462192895Sjamie mtx_unlock(&tpr->pr_mtx); 2463191673Sjamie if (flags & PD_LIST_SLOCKED) 2464191673Sjamie sx_sunlock(&allprison_lock); 2465191673Sjamie else if (flags & PD_LIST_XLOCKED) 2466191673Sjamie sx_xunlock(&allprison_lock); 2467191673Sjamie return; 2468191673Sjamie } 2469192895Sjamie if (tpr != pr) { 2470192895Sjamie mtx_unlock(&tpr->pr_mtx); 2471192895Sjamie mtx_lock(&pr->pr_mtx); 2472192895Sjamie } 2473191673Sjamie } 2474124882Srwatson 2475192895Sjamie for (;;) { 2476192895Sjamie if (flags & PD_DEREF) 2477192895Sjamie pr->pr_ref--; 2478192895Sjamie /* If the prison still has references, nothing else to do. */ 2479192895Sjamie if (pr->pr_ref > 0) { 2480192895Sjamie mtx_unlock(&pr->pr_mtx); 2481192895Sjamie if (flags & PD_LIST_SLOCKED) 2482192895Sjamie sx_sunlock(&allprison_lock); 2483192895Sjamie else if (flags & PD_LIST_XLOCKED) 2484192895Sjamie sx_xunlock(&allprison_lock); 2485192895Sjamie return; 2486191673Sjamie } 2487191673Sjamie 2488192895Sjamie mtx_unlock(&pr->pr_mtx); 2489192895Sjamie if (flags & PD_LIST_SLOCKED) { 2490192895Sjamie if (!sx_try_upgrade(&allprison_lock)) { 2491192895Sjamie sx_sunlock(&allprison_lock); 2492192895Sjamie sx_xlock(&allprison_lock); 2493192895Sjamie } 2494192895Sjamie } else if (!(flags & PD_LIST_XLOCKED)) 2495192895Sjamie sx_xlock(&allprison_lock); 2496168489Spjd 2497192895Sjamie TAILQ_REMOVE(&allprison, pr, pr_list); 2498192895Sjamie LIST_REMOVE(pr, pr_sibling); 2499192895Sjamie ppr = pr->pr_parent; 2500192895Sjamie for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 2501194762Sjamie tpr->pr_childcount--; 2502196592Sjamie sx_xunlock(&allprison_lock); 2503192895Sjamie 2504194251Sjamie#ifdef VIMAGE 2505196505Szec if (pr->pr_vnet != ppr->pr_vnet) 2506194251Sjamie vnet_destroy(pr->pr_vnet); 2507194251Sjamie#endif 2508192895Sjamie if (pr->pr_root != NULL) { 2509192895Sjamie vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount); 2510192895Sjamie vrele(pr->pr_root); 2511192895Sjamie VFS_UNLOCK_GIANT(vfslocked); 2512192895Sjamie } 2513192895Sjamie mtx_destroy(&pr->pr_mtx); 2514191673Sjamie#ifdef INET 2515192895Sjamie free(pr->pr_ip4, M_PRISON); 2516191673Sjamie#endif 2517185435Sbz#ifdef INET6 2518192895Sjamie free(pr->pr_ip6, M_PRISON); 2519185435Sbz#endif 2520192895Sjamie if (pr->pr_cpuset != NULL) 2521192895Sjamie cpuset_rel(pr->pr_cpuset); 2522192895Sjamie osd_jail_exit(pr); 2523192895Sjamie free(pr, M_PRISON); 2524192895Sjamie 2525192895Sjamie /* Removing a prison frees a reference on its parent. */ 2526192895Sjamie pr = ppr; 2527192895Sjamie mtx_lock(&pr->pr_mtx); 2528196592Sjamie flags = PD_DEREF; 2529192895Sjamie } 2530124882Srwatson} 2531124882Srwatson 253272786Srwatsonvoid 2533185029Spjdprison_hold_locked(struct prison *pr) 253472786Srwatson{ 253572786Srwatson 2536185029Spjd mtx_assert(&pr->pr_mtx, MA_OWNED); 2537168489Spjd KASSERT(pr->pr_ref > 0, 2538191671Sjamie ("Trying to hold dead prison (jid=%d).", pr->pr_id)); 253972786Srwatson pr->pr_ref++; 2540185029Spjd} 2541185029Spjd 2542185029Spjdvoid 2543185029Spjdprison_hold(struct prison *pr) 2544185029Spjd{ 2545185029Spjd 2546185029Spjd mtx_lock(&pr->pr_mtx); 2547185029Spjd prison_hold_locked(pr); 254887275Srwatson mtx_unlock(&pr->pr_mtx); 254972786Srwatson} 255072786Srwatson 2551185435Sbzvoid 2552185435Sbzprison_proc_hold(struct prison *pr) 255387275Srwatson{ 255487275Srwatson 2555185435Sbz mtx_lock(&pr->pr_mtx); 2556191673Sjamie KASSERT(pr->pr_uref > 0, 2557191673Sjamie ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id)); 2558191673Sjamie pr->pr_uref++; 2559185435Sbz mtx_unlock(&pr->pr_mtx); 256087275Srwatson} 256187275Srwatson 2562185435Sbzvoid 2563185435Sbzprison_proc_free(struct prison *pr) 2564185435Sbz{ 2565185435Sbz 2566185435Sbz mtx_lock(&pr->pr_mtx); 2567191673Sjamie KASSERT(pr->pr_uref > 0, 2568191673Sjamie ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id)); 2569191673Sjamie prison_deref(pr, PD_DEUREF | PD_LOCKED); 2570185435Sbz} 2571185435Sbz 2572185435Sbz 2573185435Sbz#ifdef INET 2574185435Sbz/* 2575192895Sjamie * Restrict a prison's IP address list with its parent's, possibly replacing 2576192895Sjamie * it. Return true if the replacement buffer was used (or would have been). 2577192895Sjamie */ 2578192895Sjamiestatic int 2579192895Sjamieprison_restrict_ip4(struct prison *pr, struct in_addr *newip4) 2580192895Sjamie{ 2581192895Sjamie int ii, ij, used; 2582192895Sjamie struct prison *ppr; 2583192895Sjamie 2584192895Sjamie ppr = pr->pr_parent; 2585192895Sjamie if (!(pr->pr_flags & PR_IP4_USER)) { 2586192895Sjamie /* This has no user settings, so just copy the parent's list. */ 2587192895Sjamie if (pr->pr_ip4s < ppr->pr_ip4s) { 2588192895Sjamie /* 2589192895Sjamie * There's no room for the parent's list. Use the 2590192895Sjamie * new list buffer, which is assumed to be big enough 2591192895Sjamie * (if it was passed). If there's no buffer, try to 2592192895Sjamie * allocate one. 2593192895Sjamie */ 2594192895Sjamie used = 1; 2595192895Sjamie if (newip4 == NULL) { 2596192895Sjamie newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4), 2597192895Sjamie M_PRISON, M_NOWAIT); 2598192895Sjamie if (newip4 != NULL) 2599192895Sjamie used = 0; 2600192895Sjamie } 2601192895Sjamie if (newip4 != NULL) { 2602192895Sjamie bcopy(ppr->pr_ip4, newip4, 2603192895Sjamie ppr->pr_ip4s * sizeof(*newip4)); 2604192895Sjamie free(pr->pr_ip4, M_PRISON); 2605192895Sjamie pr->pr_ip4 = newip4; 2606192895Sjamie pr->pr_ip4s = ppr->pr_ip4s; 2607192895Sjamie } 2608192895Sjamie return (used); 2609192895Sjamie } 2610192895Sjamie pr->pr_ip4s = ppr->pr_ip4s; 2611192895Sjamie if (pr->pr_ip4s > 0) 2612192895Sjamie bcopy(ppr->pr_ip4, pr->pr_ip4, 2613192895Sjamie pr->pr_ip4s * sizeof(*newip4)); 2614192895Sjamie else if (pr->pr_ip4 != NULL) { 2615192895Sjamie free(pr->pr_ip4, M_PRISON); 2616192895Sjamie pr->pr_ip4 = NULL; 2617192895Sjamie } 2618195974Sjamie } else if (pr->pr_ip4s > 0) { 2619192895Sjamie /* Remove addresses that aren't in the parent. */ 2620192895Sjamie for (ij = 0; ij < ppr->pr_ip4s; ij++) 2621192895Sjamie if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 2622192895Sjamie break; 2623192895Sjamie if (ij < ppr->pr_ip4s) 2624192895Sjamie ii = 1; 2625192895Sjamie else { 2626192895Sjamie bcopy(pr->pr_ip4 + 1, pr->pr_ip4, 2627192895Sjamie --pr->pr_ip4s * sizeof(*pr->pr_ip4)); 2628192895Sjamie ii = 0; 2629192895Sjamie } 2630192895Sjamie for (ij = 1; ii < pr->pr_ip4s; ) { 2631192895Sjamie if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) { 2632192895Sjamie ii++; 2633192895Sjamie continue; 2634192895Sjamie } 2635192895Sjamie switch (ij >= ppr->pr_ip4s ? -1 : 2636192895Sjamie qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) { 2637192895Sjamie case -1: 2638192895Sjamie bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii, 2639192895Sjamie (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4)); 2640192895Sjamie break; 2641192895Sjamie case 0: 2642192895Sjamie ii++; 2643192895Sjamie ij++; 2644192895Sjamie break; 2645192895Sjamie case 1: 2646192895Sjamie ij++; 2647192895Sjamie break; 2648192895Sjamie } 2649192895Sjamie } 2650192895Sjamie if (pr->pr_ip4s == 0) { 2651195870Sjamie pr->pr_flags |= PR_IP4_DISABLE; 2652192895Sjamie free(pr->pr_ip4, M_PRISON); 2653192895Sjamie pr->pr_ip4 = NULL; 2654192895Sjamie } 2655192895Sjamie } 2656192895Sjamie return (0); 2657192895Sjamie} 2658192895Sjamie 2659192895Sjamie/* 2660185435Sbz * Pass back primary IPv4 address of this jail. 2661185435Sbz * 2662192895Sjamie * If not restricted return success but do not alter the address. Caller has 2663192895Sjamie * to make sure to initialize it correctly (e.g. INADDR_ANY). 2664185435Sbz * 2665188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4. 2666188144Sjamie * Address returned in NBO. 2667185435Sbz */ 266846155Sphkint 2669187684Sbzprison_get_ip4(struct ucred *cred, struct in_addr *ia) 267046155Sphk{ 2671191673Sjamie struct prison *pr; 267246155Sphk 2673185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2674185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2675185435Sbz 2676192895Sjamie pr = cred->cr_prison; 2677192895Sjamie if (!(pr->pr_flags & PR_IP4)) 267846155Sphk return (0); 2679191673Sjamie mtx_lock(&pr->pr_mtx); 2680192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2681192895Sjamie mtx_unlock(&pr->pr_mtx); 2682192895Sjamie return (0); 2683192895Sjamie } 2684191673Sjamie if (pr->pr_ip4 == NULL) { 2685191673Sjamie mtx_unlock(&pr->pr_mtx); 2686188144Sjamie return (EAFNOSUPPORT); 2687191673Sjamie } 2688185435Sbz 2689191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2690191673Sjamie mtx_unlock(&pr->pr_mtx); 2691185435Sbz return (0); 2692185435Sbz} 2693185435Sbz 2694185435Sbz/* 2695202468Sbz * Return 1 if we should do proper source address selection or are not jailed. 2696202468Sbz * We will return 0 if we should bypass source address selection in favour 2697202468Sbz * of the primary jail IPv4 address. Only in this case *ia will be updated and 2698202468Sbz * returned in NBO. 2699202468Sbz * Return EAFNOSUPPORT, in case this jail does not allow IPv4. 2700202468Sbz */ 2701202468Sbzint 2702202468Sbzprison_saddrsel_ip4(struct ucred *cred, struct in_addr *ia) 2703202468Sbz{ 2704202468Sbz struct prison *pr; 2705202468Sbz struct in_addr lia; 2706202468Sbz int error; 2707202468Sbz 2708202468Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2709202468Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2710202468Sbz 2711202468Sbz if (!jailed(cred)) 2712202468Sbz return (1); 2713202468Sbz 2714202468Sbz pr = cred->cr_prison; 2715202468Sbz if (pr->pr_flags & PR_IP4_SADDRSEL) 2716202468Sbz return (1); 2717202468Sbz 2718202468Sbz lia.s_addr = INADDR_ANY; 2719202468Sbz error = prison_get_ip4(cred, &lia); 2720202468Sbz if (error) 2721202468Sbz return (error); 2722202468Sbz if (lia.s_addr == INADDR_ANY) 2723202468Sbz return (1); 2724202468Sbz 2725202468Sbz ia->s_addr = lia.s_addr; 2726202468Sbz return (0); 2727202468Sbz} 2728202468Sbz 2729202468Sbz/* 2730192895Sjamie * Return true if pr1 and pr2 have the same IPv4 address restrictions. 2731192895Sjamie */ 2732192895Sjamieint 2733192895Sjamieprison_equal_ip4(struct prison *pr1, struct prison *pr2) 2734192895Sjamie{ 2735192895Sjamie 2736192895Sjamie if (pr1 == pr2) 2737192895Sjamie return (1); 2738192895Sjamie 2739192895Sjamie /* 2740195974Sjamie * No need to lock since the PR_IP4_USER flag can't be altered for 2741195974Sjamie * existing prisons. 2742192895Sjamie */ 2743195945Sjamie while (pr1 != &prison0 && 2744195945Sjamie#ifdef VIMAGE 2745195945Sjamie !(pr1->pr_flags & PR_VNET) && 2746195945Sjamie#endif 2747195945Sjamie !(pr1->pr_flags & PR_IP4_USER)) 2748192895Sjamie pr1 = pr1->pr_parent; 2749195945Sjamie while (pr2 != &prison0 && 2750195945Sjamie#ifdef VIMAGE 2751195945Sjamie !(pr2->pr_flags & PR_VNET) && 2752195945Sjamie#endif 2753195945Sjamie !(pr2->pr_flags & PR_IP4_USER)) 2754192895Sjamie pr2 = pr2->pr_parent; 2755192895Sjamie return (pr1 == pr2); 2756192895Sjamie} 2757192895Sjamie 2758192895Sjamie/* 2759185435Sbz * Make sure our (source) address is set to something meaningful to this 2760185435Sbz * jail. 2761185435Sbz * 2762192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail, 2763192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 2764192895Sjamie * doesn't allow IPv4. Address passed in in NBO and returned in NBO. 2765185435Sbz */ 2766185435Sbzint 2767185435Sbzprison_local_ip4(struct ucred *cred, struct in_addr *ia) 2768185435Sbz{ 2769191673Sjamie struct prison *pr; 2770185435Sbz struct in_addr ia0; 2771191673Sjamie int error; 2772185435Sbz 2773185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2774185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2775185435Sbz 2776192895Sjamie pr = cred->cr_prison; 2777192895Sjamie if (!(pr->pr_flags & PR_IP4)) 277846155Sphk return (0); 2779191673Sjamie mtx_lock(&pr->pr_mtx); 2780192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2781192895Sjamie mtx_unlock(&pr->pr_mtx); 2782192895Sjamie return (0); 2783192895Sjamie } 2784191673Sjamie if (pr->pr_ip4 == NULL) { 2785191673Sjamie mtx_unlock(&pr->pr_mtx); 2786188144Sjamie return (EAFNOSUPPORT); 2787191673Sjamie } 2788185435Sbz 2789185435Sbz ia0.s_addr = ntohl(ia->s_addr); 2790185435Sbz if (ia0.s_addr == INADDR_LOOPBACK) { 2791191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2792191673Sjamie mtx_unlock(&pr->pr_mtx); 2793185435Sbz return (0); 279446155Sphk } 2795185435Sbz 2796188144Sjamie if (ia0.s_addr == INADDR_ANY) { 2797188144Sjamie /* 2798188144Sjamie * In case there is only 1 IPv4 address, bind directly. 2799188144Sjamie */ 2800191673Sjamie if (pr->pr_ip4s == 1) 2801191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2802191673Sjamie mtx_unlock(&pr->pr_mtx); 2803185435Sbz return (0); 2804185435Sbz } 2805185435Sbz 2806191673Sjamie error = _prison_check_ip4(pr, ia); 2807191673Sjamie mtx_unlock(&pr->pr_mtx); 2808191673Sjamie return (error); 2809185435Sbz} 2810185435Sbz 2811185435Sbz/* 2812185435Sbz * Rewrite destination address in case we will connect to loopback address. 2813185435Sbz * 2814188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4. 2815188144Sjamie * Address passed in in NBO and returned in NBO. 2816185435Sbz */ 2817185435Sbzint 2818185435Sbzprison_remote_ip4(struct ucred *cred, struct in_addr *ia) 2819185435Sbz{ 2820191673Sjamie struct prison *pr; 2821185435Sbz 2822185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2823185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2824185435Sbz 2825192895Sjamie pr = cred->cr_prison; 2826192895Sjamie if (!(pr->pr_flags & PR_IP4)) 2827185435Sbz return (0); 2828191673Sjamie mtx_lock(&pr->pr_mtx); 2829192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2830192895Sjamie mtx_unlock(&pr->pr_mtx); 2831192895Sjamie return (0); 2832192895Sjamie } 2833191673Sjamie if (pr->pr_ip4 == NULL) { 2834191673Sjamie mtx_unlock(&pr->pr_mtx); 2835188144Sjamie return (EAFNOSUPPORT); 2836191673Sjamie } 2837188144Sjamie 2838185435Sbz if (ntohl(ia->s_addr) == INADDR_LOOPBACK) { 2839191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2840191673Sjamie mtx_unlock(&pr->pr_mtx); 2841185435Sbz return (0); 2842185435Sbz } 2843185435Sbz 2844185435Sbz /* 2845185435Sbz * Return success because nothing had to be changed. 2846185435Sbz */ 2847191673Sjamie mtx_unlock(&pr->pr_mtx); 2848185435Sbz return (0); 2849185435Sbz} 2850185435Sbz 2851185435Sbz/* 2852188144Sjamie * Check if given address belongs to the jail referenced by cred/prison. 2853185435Sbz * 2854192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail, 2855192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 2856192895Sjamie * doesn't allow IPv4. Address passed in in NBO. 2857185435Sbz */ 2858185435Sbzstatic int 2859185435Sbz_prison_check_ip4(struct prison *pr, struct in_addr *ia) 2860185435Sbz{ 2861185435Sbz int i, a, z, d; 2862185435Sbz 2863185435Sbz /* 2864185435Sbz * Check the primary IP. 2865185435Sbz */ 2866185435Sbz if (pr->pr_ip4[0].s_addr == ia->s_addr) 2867188144Sjamie return (0); 2868185435Sbz 2869185435Sbz /* 2870185435Sbz * All the other IPs are sorted so we can do a binary search. 2871185435Sbz */ 2872185435Sbz a = 0; 2873185435Sbz z = pr->pr_ip4s - 2; 2874185435Sbz while (a <= z) { 2875185435Sbz i = (a + z) / 2; 2876185435Sbz d = qcmp_v4(&pr->pr_ip4[i+1], ia); 2877185435Sbz if (d > 0) 2878185435Sbz z = i - 1; 2879185435Sbz else if (d < 0) 2880185435Sbz a = i + 1; 288181114Srwatson else 2882188144Sjamie return (0); 2883185435Sbz } 2884188144Sjamie 2885188144Sjamie return (EADDRNOTAVAIL); 2886185435Sbz} 2887185435Sbz 2888185435Sbzint 2889185435Sbzprison_check_ip4(struct ucred *cred, struct in_addr *ia) 2890185435Sbz{ 2891191673Sjamie struct prison *pr; 2892191673Sjamie int error; 2893185435Sbz 2894185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2895185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2896185435Sbz 2897192895Sjamie pr = cred->cr_prison; 2898192895Sjamie if (!(pr->pr_flags & PR_IP4)) 2899188144Sjamie return (0); 2900191673Sjamie mtx_lock(&pr->pr_mtx); 2901192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2902192895Sjamie mtx_unlock(&pr->pr_mtx); 2903192895Sjamie return (0); 2904192895Sjamie } 2905191673Sjamie if (pr->pr_ip4 == NULL) { 2906191673Sjamie mtx_unlock(&pr->pr_mtx); 2907188144Sjamie return (EAFNOSUPPORT); 2908191673Sjamie } 2909185435Sbz 2910191673Sjamie error = _prison_check_ip4(pr, ia); 2911191673Sjamie mtx_unlock(&pr->pr_mtx); 2912191673Sjamie return (error); 2913185435Sbz} 2914185435Sbz#endif 2915185435Sbz 2916185435Sbz#ifdef INET6 2917192895Sjamiestatic int 2918192895Sjamieprison_restrict_ip6(struct prison *pr, struct in6_addr *newip6) 2919192895Sjamie{ 2920192895Sjamie int ii, ij, used; 2921192895Sjamie struct prison *ppr; 2922192895Sjamie 2923192895Sjamie ppr = pr->pr_parent; 2924192895Sjamie if (!(pr->pr_flags & PR_IP6_USER)) { 2925192895Sjamie /* This has no user settings, so just copy the parent's list. */ 2926192895Sjamie if (pr->pr_ip6s < ppr->pr_ip6s) { 2927192895Sjamie /* 2928192895Sjamie * There's no room for the parent's list. Use the 2929192895Sjamie * new list buffer, which is assumed to be big enough 2930192895Sjamie * (if it was passed). If there's no buffer, try to 2931192895Sjamie * allocate one. 2932192895Sjamie */ 2933192895Sjamie used = 1; 2934192895Sjamie if (newip6 == NULL) { 2935192895Sjamie newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6), 2936192895Sjamie M_PRISON, M_NOWAIT); 2937192895Sjamie if (newip6 != NULL) 2938192895Sjamie used = 0; 2939192895Sjamie } 2940192895Sjamie if (newip6 != NULL) { 2941192895Sjamie bcopy(ppr->pr_ip6, newip6, 2942192895Sjamie ppr->pr_ip6s * sizeof(*newip6)); 2943192895Sjamie free(pr->pr_ip6, M_PRISON); 2944192895Sjamie pr->pr_ip6 = newip6; 2945192895Sjamie pr->pr_ip6s = ppr->pr_ip6s; 2946192895Sjamie } 2947192895Sjamie return (used); 2948192895Sjamie } 2949192895Sjamie pr->pr_ip6s = ppr->pr_ip6s; 2950192895Sjamie if (pr->pr_ip6s > 0) 2951192895Sjamie bcopy(ppr->pr_ip6, pr->pr_ip6, 2952192895Sjamie pr->pr_ip6s * sizeof(*newip6)); 2953192895Sjamie else if (pr->pr_ip6 != NULL) { 2954192895Sjamie free(pr->pr_ip6, M_PRISON); 2955192895Sjamie pr->pr_ip6 = NULL; 2956192895Sjamie } 2957195974Sjamie } else if (pr->pr_ip6s > 0) { 2958192895Sjamie /* Remove addresses that aren't in the parent. */ 2959192895Sjamie for (ij = 0; ij < ppr->pr_ip6s; ij++) 2960192895Sjamie if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], 2961192895Sjamie &ppr->pr_ip6[ij])) 2962192895Sjamie break; 2963192895Sjamie if (ij < ppr->pr_ip6s) 2964192895Sjamie ii = 1; 2965192895Sjamie else { 2966192895Sjamie bcopy(pr->pr_ip6 + 1, pr->pr_ip6, 2967192895Sjamie --pr->pr_ip6s * sizeof(*pr->pr_ip6)); 2968192895Sjamie ii = 0; 2969192895Sjamie } 2970192895Sjamie for (ij = 1; ii < pr->pr_ip6s; ) { 2971192895Sjamie if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii], 2972192895Sjamie &ppr->pr_ip6[0])) { 2973192895Sjamie ii++; 2974192895Sjamie continue; 2975192895Sjamie } 2976192895Sjamie switch (ij >= ppr->pr_ip4s ? -1 : 2977192895Sjamie qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) { 2978192895Sjamie case -1: 2979192895Sjamie bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii, 2980192895Sjamie (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6)); 2981192895Sjamie break; 2982192895Sjamie case 0: 2983192895Sjamie ii++; 2984192895Sjamie ij++; 2985192895Sjamie break; 2986192895Sjamie case 1: 2987192895Sjamie ij++; 2988192895Sjamie break; 2989192895Sjamie } 2990192895Sjamie } 2991192895Sjamie if (pr->pr_ip6s == 0) { 2992195870Sjamie pr->pr_flags |= PR_IP6_DISABLE; 2993192895Sjamie free(pr->pr_ip6, M_PRISON); 2994192895Sjamie pr->pr_ip6 = NULL; 2995192895Sjamie } 2996192895Sjamie } 2997192895Sjamie return 0; 2998192895Sjamie} 2999192895Sjamie 3000185435Sbz/* 3001185435Sbz * Pass back primary IPv6 address for this jail. 3002185435Sbz * 3003192895Sjamie * If not restricted return success but do not alter the address. Caller has 3004192895Sjamie * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT). 3005185435Sbz * 3006188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6. 3007185435Sbz */ 3008185435Sbzint 3009187684Sbzprison_get_ip6(struct ucred *cred, struct in6_addr *ia6) 3010185435Sbz{ 3011191673Sjamie struct prison *pr; 3012185435Sbz 3013185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3014185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3015185435Sbz 3016192895Sjamie pr = cred->cr_prison; 3017192895Sjamie if (!(pr->pr_flags & PR_IP6)) 301881114Srwatson return (0); 3019191673Sjamie mtx_lock(&pr->pr_mtx); 3020192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3021192895Sjamie mtx_unlock(&pr->pr_mtx); 3022192895Sjamie return (0); 3023192895Sjamie } 3024191673Sjamie if (pr->pr_ip6 == NULL) { 3025191673Sjamie mtx_unlock(&pr->pr_mtx); 3026188144Sjamie return (EAFNOSUPPORT); 3027191673Sjamie } 3028188144Sjamie 3029191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3030191673Sjamie mtx_unlock(&pr->pr_mtx); 3031185435Sbz return (0); 3032185435Sbz} 3033185435Sbz 3034185435Sbz/* 3035202468Sbz * Return 1 if we should do proper source address selection or are not jailed. 3036202468Sbz * We will return 0 if we should bypass source address selection in favour 3037202468Sbz * of the primary jail IPv6 address. Only in this case *ia will be updated and 3038202468Sbz * returned in NBO. 3039202468Sbz * Return EAFNOSUPPORT, in case this jail does not allow IPv6. 3040202468Sbz */ 3041202468Sbzint 3042202468Sbzprison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia6) 3043202468Sbz{ 3044202468Sbz struct prison *pr; 3045202468Sbz struct in6_addr lia6; 3046202468Sbz int error; 3047202468Sbz 3048202468Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3049202468Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3050202468Sbz 3051202468Sbz if (!jailed(cred)) 3052202468Sbz return (1); 3053202468Sbz 3054202468Sbz pr = cred->cr_prison; 3055202468Sbz if (pr->pr_flags & PR_IP6_SADDRSEL) 3056202468Sbz return (1); 3057202468Sbz 3058202468Sbz lia6 = in6addr_any; 3059202468Sbz error = prison_get_ip6(cred, &lia6); 3060202468Sbz if (error) 3061202468Sbz return (error); 3062202468Sbz if (IN6_IS_ADDR_UNSPECIFIED(&lia6)) 3063202468Sbz return (1); 3064202468Sbz 3065202468Sbz bcopy(&lia6, ia6, sizeof(struct in6_addr)); 3066202468Sbz return (0); 3067202468Sbz} 3068202468Sbz 3069202468Sbz/* 3070192895Sjamie * Return true if pr1 and pr2 have the same IPv6 address restrictions. 3071192895Sjamie */ 3072192895Sjamieint 3073192895Sjamieprison_equal_ip6(struct prison *pr1, struct prison *pr2) 3074192895Sjamie{ 3075192895Sjamie 3076192895Sjamie if (pr1 == pr2) 3077192895Sjamie return (1); 3078192895Sjamie 3079195945Sjamie while (pr1 != &prison0 && 3080195945Sjamie#ifdef VIMAGE 3081195945Sjamie !(pr1->pr_flags & PR_VNET) && 3082195945Sjamie#endif 3083195945Sjamie !(pr1->pr_flags & PR_IP6_USER)) 3084192895Sjamie pr1 = pr1->pr_parent; 3085195945Sjamie while (pr2 != &prison0 && 3086195945Sjamie#ifdef VIMAGE 3087195945Sjamie !(pr2->pr_flags & PR_VNET) && 3088195945Sjamie#endif 3089195945Sjamie !(pr2->pr_flags & PR_IP6_USER)) 3090192895Sjamie pr2 = pr2->pr_parent; 3091192895Sjamie return (pr1 == pr2); 3092192895Sjamie} 3093192895Sjamie 3094192895Sjamie/* 3095185435Sbz * Make sure our (source) address is set to something meaningful to this jail. 3096185435Sbz * 3097185435Sbz * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0) 3098185435Sbz * when needed while binding. 3099185435Sbz * 3100192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail, 3101192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 3102192895Sjamie * doesn't allow IPv6. 3103185435Sbz */ 3104185435Sbzint 3105185435Sbzprison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only) 3106185435Sbz{ 3107191673Sjamie struct prison *pr; 3108191673Sjamie int error; 3109185435Sbz 3110185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3111185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3112185435Sbz 3113192895Sjamie pr = cred->cr_prison; 3114192895Sjamie if (!(pr->pr_flags & PR_IP6)) 3115185435Sbz return (0); 3116191673Sjamie mtx_lock(&pr->pr_mtx); 3117192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3118192895Sjamie mtx_unlock(&pr->pr_mtx); 3119192895Sjamie return (0); 3120192895Sjamie } 3121191673Sjamie if (pr->pr_ip6 == NULL) { 3122191673Sjamie mtx_unlock(&pr->pr_mtx); 3123188144Sjamie return (EAFNOSUPPORT); 3124191673Sjamie } 3125188144Sjamie 3126185435Sbz if (IN6_IS_ADDR_LOOPBACK(ia6)) { 3127191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3128191673Sjamie mtx_unlock(&pr->pr_mtx); 3129185435Sbz return (0); 313081114Srwatson } 3131185435Sbz 3132188144Sjamie if (IN6_IS_ADDR_UNSPECIFIED(ia6)) { 3133188144Sjamie /* 3134188144Sjamie * In case there is only 1 IPv6 address, and v6only is true, 3135188144Sjamie * then bind directly. 3136188144Sjamie */ 3137191673Sjamie if (v6only != 0 && pr->pr_ip6s == 1) 3138191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3139191673Sjamie mtx_unlock(&pr->pr_mtx); 3140185435Sbz return (0); 3141185435Sbz } 3142188144Sjamie 3143191673Sjamie error = _prison_check_ip6(pr, ia6); 3144191673Sjamie mtx_unlock(&pr->pr_mtx); 3145191673Sjamie return (error); 3146185435Sbz} 3147185435Sbz 3148185435Sbz/* 3149185435Sbz * Rewrite destination address in case we will connect to loopback address. 3150185435Sbz * 3151188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6. 3152185435Sbz */ 3153185435Sbzint 3154185435Sbzprison_remote_ip6(struct ucred *cred, struct in6_addr *ia6) 3155185435Sbz{ 3156191673Sjamie struct prison *pr; 3157185435Sbz 3158185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3159185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3160185435Sbz 3161192895Sjamie pr = cred->cr_prison; 3162192895Sjamie if (!(pr->pr_flags & PR_IP6)) 3163185435Sbz return (0); 3164191673Sjamie mtx_lock(&pr->pr_mtx); 3165192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3166192895Sjamie mtx_unlock(&pr->pr_mtx); 3167192895Sjamie return (0); 3168192895Sjamie } 3169191673Sjamie if (pr->pr_ip6 == NULL) { 3170191673Sjamie mtx_unlock(&pr->pr_mtx); 3171188144Sjamie return (EAFNOSUPPORT); 3172191673Sjamie } 3173188144Sjamie 3174185435Sbz if (IN6_IS_ADDR_LOOPBACK(ia6)) { 3175191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3176191673Sjamie mtx_unlock(&pr->pr_mtx); 3177185435Sbz return (0); 3178185435Sbz } 3179185435Sbz 3180185435Sbz /* 3181185435Sbz * Return success because nothing had to be changed. 3182185435Sbz */ 3183191673Sjamie mtx_unlock(&pr->pr_mtx); 318446155Sphk return (0); 318546155Sphk} 318646155Sphk 3187185435Sbz/* 3188188144Sjamie * Check if given address belongs to the jail referenced by cred/prison. 3189185435Sbz * 3190192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail, 3191192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 3192192895Sjamie * doesn't allow IPv6. 3193185435Sbz */ 3194185435Sbzstatic int 3195185435Sbz_prison_check_ip6(struct prison *pr, struct in6_addr *ia6) 319646155Sphk{ 3197185435Sbz int i, a, z, d; 319846155Sphk 3199185435Sbz /* 3200185435Sbz * Check the primary IP. 3201185435Sbz */ 3202185435Sbz if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6)) 3203188144Sjamie return (0); 3204185435Sbz 3205185435Sbz /* 3206185435Sbz * All the other IPs are sorted so we can do a binary search. 3207185435Sbz */ 3208185435Sbz a = 0; 3209185435Sbz z = pr->pr_ip6s - 2; 3210185435Sbz while (a <= z) { 3211185435Sbz i = (a + z) / 2; 3212185435Sbz d = qcmp_v6(&pr->pr_ip6[i+1], ia6); 3213185435Sbz if (d > 0) 3214185435Sbz z = i - 1; 3215185435Sbz else if (d < 0) 3216185435Sbz a = i + 1; 321746155Sphk else 3218188144Sjamie return (0); 321946155Sphk } 3220188144Sjamie 3221188144Sjamie return (EADDRNOTAVAIL); 322246155Sphk} 322346155Sphk 322446155Sphkint 3225185435Sbzprison_check_ip6(struct ucred *cred, struct in6_addr *ia6) 3226185435Sbz{ 3227191673Sjamie struct prison *pr; 3228191673Sjamie int error; 3229185435Sbz 3230185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3231185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3232185435Sbz 3233192895Sjamie pr = cred->cr_prison; 3234192895Sjamie if (!(pr->pr_flags & PR_IP6)) 3235188144Sjamie return (0); 3236191673Sjamie mtx_lock(&pr->pr_mtx); 3237192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3238192895Sjamie mtx_unlock(&pr->pr_mtx); 3239192895Sjamie return (0); 3240192895Sjamie } 3241191673Sjamie if (pr->pr_ip6 == NULL) { 3242191673Sjamie mtx_unlock(&pr->pr_mtx); 3243188144Sjamie return (EAFNOSUPPORT); 3244191673Sjamie } 3245185435Sbz 3246191673Sjamie error = _prison_check_ip6(pr, ia6); 3247191673Sjamie mtx_unlock(&pr->pr_mtx); 3248191673Sjamie return (error); 3249185435Sbz} 3250185435Sbz#endif 3251185435Sbz 3252185435Sbz/* 3253188146Sjamie * Check if a jail supports the given address family. 3254188146Sjamie * 3255188146Sjamie * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT 3256188146Sjamie * if not. 3257188146Sjamie */ 3258188146Sjamieint 3259188146Sjamieprison_check_af(struct ucred *cred, int af) 3260188146Sjamie{ 3261192895Sjamie struct prison *pr; 3262188146Sjamie int error; 3263188146Sjamie 3264188146Sjamie KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3265188146Sjamie 3266192895Sjamie pr = cred->cr_prison; 3267194923Sjamie#ifdef VIMAGE 3268194915Sjamie /* Prisons with their own network stack are not limited. */ 3269200473Sbz if (prison_owns_vnet(cred)) 3270194915Sjamie return (0); 3271194923Sjamie#endif 3272194915Sjamie 3273188146Sjamie error = 0; 3274188146Sjamie switch (af) 3275188146Sjamie { 3276188146Sjamie#ifdef INET 3277188146Sjamie case AF_INET: 3278192895Sjamie if (pr->pr_flags & PR_IP4) 3279192895Sjamie { 3280192895Sjamie mtx_lock(&pr->pr_mtx); 3281192895Sjamie if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL) 3282192895Sjamie error = EAFNOSUPPORT; 3283192895Sjamie mtx_unlock(&pr->pr_mtx); 3284192895Sjamie } 3285188146Sjamie break; 3286188146Sjamie#endif 3287188146Sjamie#ifdef INET6 3288188146Sjamie case AF_INET6: 3289192895Sjamie if (pr->pr_flags & PR_IP6) 3290192895Sjamie { 3291192895Sjamie mtx_lock(&pr->pr_mtx); 3292192895Sjamie if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL) 3293192895Sjamie error = EAFNOSUPPORT; 3294192895Sjamie mtx_unlock(&pr->pr_mtx); 3295192895Sjamie } 3296188146Sjamie break; 3297188146Sjamie#endif 3298188146Sjamie case AF_LOCAL: 3299188146Sjamie case AF_ROUTE: 3300188146Sjamie break; 3301188146Sjamie default: 3302192895Sjamie if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF)) 3303188146Sjamie error = EAFNOSUPPORT; 3304188146Sjamie } 3305188146Sjamie return (error); 3306188146Sjamie} 3307188146Sjamie 3308188146Sjamie/* 3309185435Sbz * Check if given address belongs to the jail referenced by cred (wrapper to 3310185435Sbz * prison_check_ip[46]). 3311185435Sbz * 3312192895Sjamie * Returns 0 if jail doesn't restrict the address family or if address belongs 3313192895Sjamie * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if 3314192895Sjamie * the jail doesn't allow the address family. IPv4 Address passed in in NBO. 3315185435Sbz */ 3316185435Sbzint 331772786Srwatsonprison_if(struct ucred *cred, struct sockaddr *sa) 331846155Sphk{ 3319185435Sbz#ifdef INET 3320114168Smike struct sockaddr_in *sai; 3321185435Sbz#endif 3322185435Sbz#ifdef INET6 3323185435Sbz struct sockaddr_in6 *sai6; 3324185435Sbz#endif 3325188144Sjamie int error; 332646155Sphk 3327185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3328185435Sbz KASSERT(sa != NULL, ("%s: sa is NULL", __func__)); 3329185435Sbz 3330200473Sbz#ifdef VIMAGE 3331200473Sbz if (prison_owns_vnet(cred)) 3332200473Sbz return (0); 3333200473Sbz#endif 3334200473Sbz 3335188144Sjamie error = 0; 3336188144Sjamie switch (sa->sa_family) 3337185435Sbz { 3338185435Sbz#ifdef INET 3339185435Sbz case AF_INET: 3340185435Sbz sai = (struct sockaddr_in *)sa; 3341188144Sjamie error = prison_check_ip4(cred, &sai->sin_addr); 3342185435Sbz break; 3343185435Sbz#endif 3344185435Sbz#ifdef INET6 3345185435Sbz case AF_INET6: 3346185435Sbz sai6 = (struct sockaddr_in6 *)sa; 3347188144Sjamie error = prison_check_ip6(cred, &sai6->sin6_addr); 3348185435Sbz break; 3349185435Sbz#endif 3350185435Sbz default: 3351192895Sjamie if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF)) 3352188144Sjamie error = EAFNOSUPPORT; 3353185435Sbz } 3354188144Sjamie return (error); 335546155Sphk} 335672786Srwatson 335772786Srwatson/* 335872786Srwatson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH. 335972786Srwatson */ 336072786Srwatsonint 3361114168Smikeprison_check(struct ucred *cred1, struct ucred *cred2) 336272786Srwatson{ 336372786Srwatson 3364192895Sjamie return ((cred1->cr_prison == cred2->cr_prison || 3365192895Sjamie prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH); 3366192895Sjamie} 336772786Srwatson 3368192895Sjamie/* 3369192895Sjamie * Return 1 if p2 is a child of p1, otherwise 0. 3370192895Sjamie */ 3371192895Sjamieint 3372192895Sjamieprison_ischild(struct prison *pr1, struct prison *pr2) 3373192895Sjamie{ 3374192895Sjamie 3375192895Sjamie for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent) 3376192895Sjamie if (pr1 == pr2) 3377192895Sjamie return (1); 337872786Srwatson return (0); 337972786Srwatson} 338072786Srwatson 338172786Srwatson/* 338272786Srwatson * Return 1 if the passed credential is in a jail, otherwise 0. 338372786Srwatson */ 338472786Srwatsonint 3385114168Smikejailed(struct ucred *cred) 338672786Srwatson{ 338772786Srwatson 3388192895Sjamie return (cred->cr_prison != &prison0); 338972786Srwatson} 339091384Srobert 339191384Srobert/* 3392200473Sbz * Return 1 if the passed credential is in a jail and that jail does not 3393200473Sbz * have its own virtual network stack, otherwise 0. 3394200473Sbz */ 3395200473Sbzint 3396200473Sbzjailed_without_vnet(struct ucred *cred) 3397200473Sbz{ 3398200473Sbz 3399200473Sbz if (!jailed(cred)) 3400200473Sbz return (0); 3401200473Sbz#ifdef VIMAGE 3402200473Sbz if (prison_owns_vnet(cred)) 3403200473Sbz return (0); 3404200473Sbz#endif 3405200473Sbz 3406200473Sbz return (1); 3407200473Sbz} 3408200473Sbz 3409200473Sbz/* 3410194090Sjamie * Return the correct hostname (domainname, et al) for the passed credential. 341191384Srobert */ 341291391Srobertvoid 3413114168Smikegetcredhostname(struct ucred *cred, char *buf, size_t size) 341491384Srobert{ 3415193066Sjamie struct prison *pr; 341691384Srobert 3417194090Sjamie /* 3418194090Sjamie * A NULL credential can be used to shortcut to the physical 3419194090Sjamie * system's hostname. 3420194090Sjamie */ 3421193066Sjamie pr = (cred != NULL) ? cred->cr_prison : &prison0; 3422193066Sjamie mtx_lock(&pr->pr_mtx); 3423194118Sjamie strlcpy(buf, pr->pr_hostname, size); 3424193066Sjamie mtx_unlock(&pr->pr_mtx); 342591384Srobert} 3426113275Smike 3427194090Sjamievoid 3428194090Sjamiegetcreddomainname(struct ucred *cred, char *buf, size_t size) 3429194090Sjamie{ 3430194090Sjamie 3431194090Sjamie mtx_lock(&cred->cr_prison->pr_mtx); 3432194118Sjamie strlcpy(buf, cred->cr_prison->pr_domainname, size); 3433194090Sjamie mtx_unlock(&cred->cr_prison->pr_mtx); 3434194090Sjamie} 3435194090Sjamie 3436194090Sjamievoid 3437194090Sjamiegetcredhostuuid(struct ucred *cred, char *buf, size_t size) 3438194090Sjamie{ 3439194090Sjamie 3440194090Sjamie mtx_lock(&cred->cr_prison->pr_mtx); 3441194118Sjamie strlcpy(buf, cred->cr_prison->pr_hostuuid, size); 3442194090Sjamie mtx_unlock(&cred->cr_prison->pr_mtx); 3443194090Sjamie} 3444194090Sjamie 3445194090Sjamievoid 3446194090Sjamiegetcredhostid(struct ucred *cred, unsigned long *hostid) 3447194090Sjamie{ 3448194090Sjamie 3449194090Sjamie mtx_lock(&cred->cr_prison->pr_mtx); 3450194090Sjamie *hostid = cred->cr_prison->pr_hostid; 3451194090Sjamie mtx_unlock(&cred->cr_prison->pr_mtx); 3452194090Sjamie} 3453194090Sjamie 3454196176Sbz#ifdef VIMAGE 3455125804Srwatson/* 3456196176Sbz * Determine whether the prison represented by cred owns 3457196176Sbz * its vnet rather than having it inherited. 3458196176Sbz * 3459196176Sbz * Returns 1 in case the prison owns the vnet, 0 otherwise. 3460196176Sbz */ 3461196176Sbzint 3462196176Sbzprison_owns_vnet(struct ucred *cred) 3463196176Sbz{ 3464196176Sbz 3465196176Sbz /* 3466196176Sbz * vnets cannot be added/removed after jail creation, 3467196176Sbz * so no need to lock here. 3468196176Sbz */ 3469196176Sbz return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0); 3470196176Sbz} 3471196176Sbz#endif 3472196176Sbz 3473196176Sbz/* 3474147185Spjd * Determine whether the subject represented by cred can "see" 3475147185Spjd * status of a mount point. 3476147185Spjd * Returns: 0 for permitted, ENOENT otherwise. 3477147185Spjd * XXX: This function should be called cr_canseemount() and should be 3478147185Spjd * placed in kern_prot.c. 3479125804Srwatson */ 3480125804Srwatsonint 3481147185Spjdprison_canseemount(struct ucred *cred, struct mount *mp) 3482125804Srwatson{ 3483147185Spjd struct prison *pr; 3484147185Spjd struct statfs *sp; 3485147185Spjd size_t len; 3486125804Srwatson 3487192895Sjamie pr = cred->cr_prison; 3488192895Sjamie if (pr->pr_enforce_statfs == 0) 3489147185Spjd return (0); 3490147185Spjd if (pr->pr_root->v_mount == mp) 3491147185Spjd return (0); 3492192895Sjamie if (pr->pr_enforce_statfs == 2) 3493147185Spjd return (ENOENT); 3494147185Spjd /* 3495147185Spjd * If jail's chroot directory is set to "/" we should be able to see 3496147185Spjd * all mount-points from inside a jail. 3497147185Spjd * This is ugly check, but this is the only situation when jail's 3498147185Spjd * directory ends with '/'. 3499147185Spjd */ 3500147185Spjd if (strcmp(pr->pr_path, "/") == 0) 3501147185Spjd return (0); 3502147185Spjd len = strlen(pr->pr_path); 3503147185Spjd sp = &mp->mnt_stat; 3504147185Spjd if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0) 3505147185Spjd return (ENOENT); 3506147185Spjd /* 3507147185Spjd * Be sure that we don't have situation where jail's root directory 3508147185Spjd * is "/some/path" and mount point is "/some/pathpath". 3509147185Spjd */ 3510147185Spjd if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/') 3511147185Spjd return (ENOENT); 3512147185Spjd return (0); 3513147185Spjd} 3514147185Spjd 3515147185Spjdvoid 3516147185Spjdprison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp) 3517147185Spjd{ 3518147185Spjd char jpath[MAXPATHLEN]; 3519147185Spjd struct prison *pr; 3520147185Spjd size_t len; 3521147185Spjd 3522192895Sjamie pr = cred->cr_prison; 3523192895Sjamie if (pr->pr_enforce_statfs == 0) 3524147185Spjd return; 3525147185Spjd if (prison_canseemount(cred, mp) != 0) { 3526147185Spjd bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3527147185Spjd strlcpy(sp->f_mntonname, "[restricted]", 3528147185Spjd sizeof(sp->f_mntonname)); 3529147185Spjd return; 3530125804Srwatson } 3531147185Spjd if (pr->pr_root->v_mount == mp) { 3532147185Spjd /* 3533147185Spjd * Clear current buffer data, so we are sure nothing from 3534147185Spjd * the valid path left there. 3535147185Spjd */ 3536147185Spjd bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3537147185Spjd *sp->f_mntonname = '/'; 3538147185Spjd return; 3539147185Spjd } 3540147185Spjd /* 3541147185Spjd * If jail's chroot directory is set to "/" we should be able to see 3542147185Spjd * all mount-points from inside a jail. 3543147185Spjd */ 3544147185Spjd if (strcmp(pr->pr_path, "/") == 0) 3545147185Spjd return; 3546147185Spjd len = strlen(pr->pr_path); 3547147185Spjd strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath)); 3548147185Spjd /* 3549147185Spjd * Clear current buffer data, so we are sure nothing from 3550147185Spjd * the valid path left there. 3551147185Spjd */ 3552147185Spjd bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3553147185Spjd if (*jpath == '\0') { 3554147185Spjd /* Should never happen. */ 3555147185Spjd *sp->f_mntonname = '/'; 3556147185Spjd } else { 3557147185Spjd strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname)); 3558147185Spjd } 3559125804Srwatson} 3560125804Srwatson 3561164032Srwatson/* 3562164032Srwatson * Check with permission for a specific privilege is granted within jail. We 3563164032Srwatson * have a specific list of accepted privileges; the rest are denied. 3564164032Srwatson */ 3565164032Srwatsonint 3566164032Srwatsonprison_priv_check(struct ucred *cred, int priv) 3567164032Srwatson{ 3568164032Srwatson 3569164032Srwatson if (!jailed(cred)) 3570164032Srwatson return (0); 3571164032Srwatson 3572194915Sjamie#ifdef VIMAGE 3573194915Sjamie /* 3574194915Sjamie * Privileges specific to prisons with a virtual network stack. 3575194915Sjamie * There might be a duplicate entry here in case the privilege 3576194915Sjamie * is only granted conditionally in the legacy jail case. 3577194915Sjamie */ 3578164032Srwatson switch (priv) { 3579194915Sjamie#ifdef notyet 3580194915Sjamie /* 3581194915Sjamie * NFS-specific privileges. 3582194915Sjamie */ 3583194915Sjamie case PRIV_NFS_DAEMON: 3584194915Sjamie case PRIV_NFS_LOCKD: 3585194915Sjamie#endif 3586194915Sjamie /* 3587194915Sjamie * Network stack privileges. 3588194915Sjamie */ 3589194915Sjamie case PRIV_NET_BRIDGE: 3590194915Sjamie case PRIV_NET_GRE: 3591194915Sjamie case PRIV_NET_BPF: 3592194915Sjamie case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */ 3593194915Sjamie case PRIV_NET_ROUTE: 3594194915Sjamie case PRIV_NET_TAP: 3595194915Sjamie case PRIV_NET_SETIFMTU: 3596194915Sjamie case PRIV_NET_SETIFFLAGS: 3597194915Sjamie case PRIV_NET_SETIFCAP: 3598203052Sdelphij case PRIV_NET_SETIFDESCR: 3599194915Sjamie case PRIV_NET_SETIFNAME : 3600194915Sjamie case PRIV_NET_SETIFMETRIC: 3601194915Sjamie case PRIV_NET_SETIFPHYS: 3602194915Sjamie case PRIV_NET_SETIFMAC: 3603194915Sjamie case PRIV_NET_ADDMULTI: 3604194915Sjamie case PRIV_NET_DELMULTI: 3605194915Sjamie case PRIV_NET_HWIOCTL: 3606194915Sjamie case PRIV_NET_SETLLADDR: 3607194915Sjamie case PRIV_NET_ADDIFGROUP: 3608194915Sjamie case PRIV_NET_DELIFGROUP: 3609194915Sjamie case PRIV_NET_IFCREATE: 3610194915Sjamie case PRIV_NET_IFDESTROY: 3611194915Sjamie case PRIV_NET_ADDIFADDR: 3612194915Sjamie case PRIV_NET_DELIFADDR: 3613194915Sjamie case PRIV_NET_LAGG: 3614194915Sjamie case PRIV_NET_GIF: 3615194915Sjamie case PRIV_NET_SETIFVNET: 3616164032Srwatson 3617164032Srwatson /* 3618194915Sjamie * 802.11-related privileges. 3619194915Sjamie */ 3620194915Sjamie case PRIV_NET80211_GETKEY: 3621194915Sjamie#ifdef notyet 3622194915Sjamie case PRIV_NET80211_MANAGE: /* XXX-BZ discuss with sam@ */ 3623194915Sjamie#endif 3624194915Sjamie 3625194915Sjamie#ifdef notyet 3626194915Sjamie /* 3627194915Sjamie * AppleTalk privileges. 3628194915Sjamie */ 3629194915Sjamie case PRIV_NETATALK_RESERVEDPORT: 3630194915Sjamie 3631194915Sjamie /* 3632194915Sjamie * ATM privileges. 3633194915Sjamie */ 3634194915Sjamie case PRIV_NETATM_CFG: 3635194915Sjamie case PRIV_NETATM_ADD: 3636194915Sjamie case PRIV_NETATM_DEL: 3637194915Sjamie case PRIV_NETATM_SET: 3638194915Sjamie 3639194915Sjamie /* 3640194915Sjamie * Bluetooth privileges. 3641194915Sjamie */ 3642194915Sjamie case PRIV_NETBLUETOOTH_RAW: 3643194915Sjamie#endif 3644194915Sjamie 3645194915Sjamie /* 3646194915Sjamie * Netgraph and netgraph module privileges. 3647194915Sjamie */ 3648194915Sjamie case PRIV_NETGRAPH_CONTROL: 3649194915Sjamie#ifdef notyet 3650194915Sjamie case PRIV_NETGRAPH_TTY: 3651194915Sjamie#endif 3652194915Sjamie 3653194915Sjamie /* 3654194915Sjamie * IPv4 and IPv6 privileges. 3655194915Sjamie */ 3656194915Sjamie case PRIV_NETINET_IPFW: 3657194915Sjamie case PRIV_NETINET_DIVERT: 3658194915Sjamie case PRIV_NETINET_PF: 3659194915Sjamie case PRIV_NETINET_DUMMYNET: 3660194915Sjamie case PRIV_NETINET_CARP: 3661194915Sjamie case PRIV_NETINET_MROUTE: 3662194915Sjamie case PRIV_NETINET_RAW: 3663194915Sjamie case PRIV_NETINET_ADDRCTRL6: 3664194915Sjamie case PRIV_NETINET_ND6: 3665194915Sjamie case PRIV_NETINET_SCOPE6: 3666194915Sjamie case PRIV_NETINET_ALIFETIME6: 3667194915Sjamie case PRIV_NETINET_IPSEC: 3668194915Sjamie case PRIV_NETINET_BINDANY: 3669194915Sjamie 3670194915Sjamie#ifdef notyet 3671194915Sjamie /* 3672194915Sjamie * IPX/SPX privileges. 3673194915Sjamie */ 3674194915Sjamie case PRIV_NETIPX_RESERVEDPORT: 3675194915Sjamie case PRIV_NETIPX_RAW: 3676194915Sjamie 3677194915Sjamie /* 3678194915Sjamie * NCP privileges. 3679194915Sjamie */ 3680194915Sjamie case PRIV_NETNCP: 3681194915Sjamie 3682194915Sjamie /* 3683194915Sjamie * SMB privileges. 3684194915Sjamie */ 3685194915Sjamie case PRIV_NETSMB: 3686194915Sjamie#endif 3687194915Sjamie 3688194915Sjamie /* 3689194915Sjamie * No default: or deny here. 3690194915Sjamie * In case of no permit fall through to next switch(). 3691194915Sjamie */ 3692194915Sjamie if (cred->cr_prison->pr_flags & PR_VNET) 3693194915Sjamie return (0); 3694194915Sjamie } 3695194915Sjamie#endif /* VIMAGE */ 3696194915Sjamie 3697194915Sjamie switch (priv) { 3698194915Sjamie 3699194915Sjamie /* 3700164032Srwatson * Allow ktrace privileges for root in jail. 3701164032Srwatson */ 3702164032Srwatson case PRIV_KTRACE: 3703164032Srwatson 3704166827Srwatson#if 0 3705164032Srwatson /* 3706164032Srwatson * Allow jailed processes to configure audit identity and 3707164032Srwatson * submit audit records (login, etc). In the future we may 3708164032Srwatson * want to further refine the relationship between audit and 3709164032Srwatson * jail. 3710164032Srwatson */ 3711164032Srwatson case PRIV_AUDIT_GETAUDIT: 3712164032Srwatson case PRIV_AUDIT_SETAUDIT: 3713164032Srwatson case PRIV_AUDIT_SUBMIT: 3714166827Srwatson#endif 3715164032Srwatson 3716164032Srwatson /* 3717164032Srwatson * Allow jailed processes to manipulate process UNIX 3718164032Srwatson * credentials in any way they see fit. 3719164032Srwatson */ 3720164032Srwatson case PRIV_CRED_SETUID: 3721164032Srwatson case PRIV_CRED_SETEUID: 3722164032Srwatson case PRIV_CRED_SETGID: 3723164032Srwatson case PRIV_CRED_SETEGID: 3724164032Srwatson case PRIV_CRED_SETGROUPS: 3725164032Srwatson case PRIV_CRED_SETREUID: 3726164032Srwatson case PRIV_CRED_SETREGID: 3727164032Srwatson case PRIV_CRED_SETRESUID: 3728164032Srwatson case PRIV_CRED_SETRESGID: 3729164032Srwatson 3730164032Srwatson /* 3731164032Srwatson * Jail implements visibility constraints already, so allow 3732164032Srwatson * jailed root to override uid/gid-based constraints. 3733164032Srwatson */ 3734164032Srwatson case PRIV_SEEOTHERGIDS: 3735164032Srwatson case PRIV_SEEOTHERUIDS: 3736164032Srwatson 3737164032Srwatson /* 3738164032Srwatson * Jail implements inter-process debugging limits already, so 3739164032Srwatson * allow jailed root various debugging privileges. 3740164032Srwatson */ 3741164032Srwatson case PRIV_DEBUG_DIFFCRED: 3742164032Srwatson case PRIV_DEBUG_SUGID: 3743164032Srwatson case PRIV_DEBUG_UNPRIV: 3744164032Srwatson 3745164032Srwatson /* 3746164032Srwatson * Allow jail to set various resource limits and login 3747164032Srwatson * properties, and for now, exceed process resource limits. 3748164032Srwatson */ 3749164032Srwatson case PRIV_PROC_LIMIT: 3750164032Srwatson case PRIV_PROC_SETLOGIN: 3751164032Srwatson case PRIV_PROC_SETRLIMIT: 3752164032Srwatson 3753164032Srwatson /* 3754164032Srwatson * System V and POSIX IPC privileges are granted in jail. 3755164032Srwatson */ 3756164032Srwatson case PRIV_IPC_READ: 3757164032Srwatson case PRIV_IPC_WRITE: 3758164032Srwatson case PRIV_IPC_ADMIN: 3759164032Srwatson case PRIV_IPC_MSGSIZE: 3760164032Srwatson case PRIV_MQ_ADMIN: 3761164032Srwatson 3762164032Srwatson /* 3763192895Sjamie * Jail operations within a jail work on child jails. 3764192895Sjamie */ 3765192895Sjamie case PRIV_JAIL_ATTACH: 3766192895Sjamie case PRIV_JAIL_SET: 3767192895Sjamie case PRIV_JAIL_REMOVE: 3768192895Sjamie 3769192895Sjamie /* 3770164032Srwatson * Jail implements its own inter-process limits, so allow 3771164032Srwatson * root processes in jail to change scheduling on other 3772164032Srwatson * processes in the same jail. Likewise for signalling. 3773164032Srwatson */ 3774164032Srwatson case PRIV_SCHED_DIFFCRED: 3775185435Sbz case PRIV_SCHED_CPUSET: 3776164032Srwatson case PRIV_SIGNAL_DIFFCRED: 3777164032Srwatson case PRIV_SIGNAL_SUGID: 3778164032Srwatson 3779164032Srwatson /* 3780164032Srwatson * Allow jailed processes to write to sysctls marked as jail 3781164032Srwatson * writable. 3782164032Srwatson */ 3783164032Srwatson case PRIV_SYSCTL_WRITEJAIL: 3784164032Srwatson 3785164032Srwatson /* 3786164032Srwatson * Allow root in jail to manage a variety of quota 3787166831Srwatson * properties. These should likely be conditional on a 3788166831Srwatson * configuration option. 3789164032Srwatson */ 3790166832Srwatson case PRIV_VFS_GETQUOTA: 3791166832Srwatson case PRIV_VFS_SETQUOTA: 3792164032Srwatson 3793164032Srwatson /* 3794164032Srwatson * Since Jail relies on chroot() to implement file system 3795164032Srwatson * protections, grant many VFS privileges to root in jail. 3796164032Srwatson * Be careful to exclude mount-related and NFS-related 3797164032Srwatson * privileges. 3798164032Srwatson */ 3799164032Srwatson case PRIV_VFS_READ: 3800164032Srwatson case PRIV_VFS_WRITE: 3801164032Srwatson case PRIV_VFS_ADMIN: 3802164032Srwatson case PRIV_VFS_EXEC: 3803164032Srwatson case PRIV_VFS_LOOKUP: 3804164032Srwatson case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */ 3805164032Srwatson case PRIV_VFS_CHFLAGS_DEV: 3806164032Srwatson case PRIV_VFS_CHOWN: 3807164032Srwatson case PRIV_VFS_CHROOT: 3808167152Spjd case PRIV_VFS_RETAINSUGID: 3809164032Srwatson case PRIV_VFS_FCHROOT: 3810164032Srwatson case PRIV_VFS_LINK: 3811164032Srwatson case PRIV_VFS_SETGID: 3812172860Srwatson case PRIV_VFS_STAT: 3813164032Srwatson case PRIV_VFS_STICKYFILE: 3814164032Srwatson return (0); 3815164032Srwatson 3816164032Srwatson /* 3817164032Srwatson * Depending on the global setting, allow privilege of 3818164032Srwatson * setting system flags. 3819164032Srwatson */ 3820164032Srwatson case PRIV_VFS_SYSFLAGS: 3821192895Sjamie if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS) 3822164032Srwatson return (0); 3823164032Srwatson else 3824164032Srwatson return (EPERM); 3825164032Srwatson 3826164032Srwatson /* 3827168396Spjd * Depending on the global setting, allow privilege of 3828168396Spjd * mounting/unmounting file systems. 3829168396Spjd */ 3830168396Spjd case PRIV_VFS_MOUNT: 3831168396Spjd case PRIV_VFS_UNMOUNT: 3832168396Spjd case PRIV_VFS_MOUNT_NONUSER: 3833168699Spjd case PRIV_VFS_MOUNT_OWNER: 3834192895Sjamie if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT) 3835168396Spjd return (0); 3836168396Spjd else 3837168396Spjd return (EPERM); 3838168396Spjd 3839168396Spjd /* 3840168591Srwatson * Allow jailed root to bind reserved ports and reuse in-use 3841168591Srwatson * ports. 3842164032Srwatson */ 3843164032Srwatson case PRIV_NETINET_RESERVEDPORT: 3844168591Srwatson case PRIV_NETINET_REUSEPORT: 3845164032Srwatson return (0); 3846164032Srwatson 3847164032Srwatson /* 3848175630Sbz * Allow jailed root to set certian IPv4/6 (option) headers. 3849175630Sbz */ 3850175630Sbz case PRIV_NETINET_SETHDROPTS: 3851175630Sbz return (0); 3852175630Sbz 3853175630Sbz /* 3854164032Srwatson * Conditionally allow creating raw sockets in jail. 3855164032Srwatson */ 3856164032Srwatson case PRIV_NETINET_RAW: 3857192895Sjamie if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS) 3858164032Srwatson return (0); 3859164032Srwatson else 3860164032Srwatson return (EPERM); 3861164032Srwatson 3862164032Srwatson /* 3863164032Srwatson * Since jail implements its own visibility limits on netstat 3864164032Srwatson * sysctls, allow getcred. This allows identd to work in 3865164032Srwatson * jail. 3866164032Srwatson */ 3867164032Srwatson case PRIV_NETINET_GETCRED: 3868164032Srwatson return (0); 3869164032Srwatson 3870164032Srwatson default: 3871164032Srwatson /* 3872164032Srwatson * In all remaining cases, deny the privilege request. This 3873164032Srwatson * includes almost all network privileges, many system 3874164032Srwatson * configuration privileges. 3875164032Srwatson */ 3876164032Srwatson return (EPERM); 3877164032Srwatson } 3878164032Srwatson} 3879164032Srwatson 3880192895Sjamie/* 3881192895Sjamie * Return the part of pr2's name that is relative to pr1, or the whole name 3882192895Sjamie * if it does not directly follow. 3883192895Sjamie */ 3884192895Sjamie 3885192895Sjamiechar * 3886192895Sjamieprison_name(struct prison *pr1, struct prison *pr2) 3887192895Sjamie{ 3888192895Sjamie char *name; 3889192895Sjamie 3890192895Sjamie /* Jails see themselves as "0" (if they see themselves at all). */ 3891192895Sjamie if (pr1 == pr2) 3892192895Sjamie return "0"; 3893192895Sjamie name = pr2->pr_name; 3894192895Sjamie if (prison_ischild(pr1, pr2)) { 3895192895Sjamie /* 3896192895Sjamie * pr1 isn't locked (and allprison_lock may not be either) 3897192895Sjamie * so its length can't be counted on. But the number of dots 3898192895Sjamie * can be counted on - and counted. 3899192895Sjamie */ 3900192895Sjamie for (; pr1 != &prison0; pr1 = pr1->pr_parent) 3901192895Sjamie name = strchr(name, '.') + 1; 3902192895Sjamie } 3903192895Sjamie return (name); 3904192895Sjamie} 3905192895Sjamie 3906192895Sjamie/* 3907192895Sjamie * Return the part of pr2's path that is relative to pr1, or the whole path 3908192895Sjamie * if it does not directly follow. 3909192895Sjamie */ 3910192895Sjamiestatic char * 3911192895Sjamieprison_path(struct prison *pr1, struct prison *pr2) 3912192895Sjamie{ 3913192895Sjamie char *path1, *path2; 3914192895Sjamie int len1; 3915192895Sjamie 3916192895Sjamie path1 = pr1->pr_path; 3917192895Sjamie path2 = pr2->pr_path; 3918192895Sjamie if (!strcmp(path1, "/")) 3919192895Sjamie return (path2); 3920192895Sjamie len1 = strlen(path1); 3921192895Sjamie if (strncmp(path1, path2, len1)) 3922192895Sjamie return (path2); 3923192895Sjamie if (path2[len1] == '\0') 3924192895Sjamie return "/"; 3925192895Sjamie if (path2[len1] == '/') 3926192895Sjamie return (path2 + len1); 3927192895Sjamie return (path2); 3928192895Sjamie} 3929192895Sjamie 3930192895Sjamie 3931192895Sjamie/* 3932192895Sjamie * Jail-related sysctls. 3933192895Sjamie */ 3934192895SjamieSYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0, 3935192895Sjamie "Jails"); 3936192895Sjamie 3937113275Smikestatic int 3938113275Smikesysctl_jail_list(SYSCTL_HANDLER_ARGS) 3939113275Smike{ 3940191673Sjamie struct xprison *xp; 3941192895Sjamie struct prison *pr, *cpr; 3942191673Sjamie#ifdef INET 3943191673Sjamie struct in_addr *ip4 = NULL; 3944191673Sjamie int ip4s = 0; 3945191673Sjamie#endif 3946191673Sjamie#ifdef INET6 3947208803Scperciva struct in6_addr *ip6 = NULL; 3948191673Sjamie int ip6s = 0; 3949191673Sjamie#endif 3950192895Sjamie int descend, error; 3951113275Smike 3952191673Sjamie xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK); 3953192895Sjamie pr = req->td->td_ucred->cr_prison; 3954191673Sjamie error = 0; 3955168401Spjd sx_slock(&allprison_lock); 3956192895Sjamie FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 3957192895Sjamie#if defined(INET) || defined(INET6) 3958191673Sjamie again: 3959192895Sjamie#endif 3960192895Sjamie mtx_lock(&cpr->pr_mtx); 3961185435Sbz#ifdef INET 3962192895Sjamie if (cpr->pr_ip4s > 0) { 3963192895Sjamie if (ip4s < cpr->pr_ip4s) { 3964192895Sjamie ip4s = cpr->pr_ip4s; 3965192895Sjamie mtx_unlock(&cpr->pr_mtx); 3966191673Sjamie ip4 = realloc(ip4, ip4s * 3967191673Sjamie sizeof(struct in_addr), M_TEMP, M_WAITOK); 3968191673Sjamie goto again; 3969191673Sjamie } 3970192895Sjamie bcopy(cpr->pr_ip4, ip4, 3971192895Sjamie cpr->pr_ip4s * sizeof(struct in_addr)); 3972191673Sjamie } 3973185435Sbz#endif 3974185435Sbz#ifdef INET6 3975192895Sjamie if (cpr->pr_ip6s > 0) { 3976192895Sjamie if (ip6s < cpr->pr_ip6s) { 3977192895Sjamie ip6s = cpr->pr_ip6s; 3978192895Sjamie mtx_unlock(&cpr->pr_mtx); 3979191673Sjamie ip6 = realloc(ip6, ip6s * 3980191673Sjamie sizeof(struct in6_addr), M_TEMP, M_WAITOK); 3981191673Sjamie goto again; 3982191673Sjamie } 3983192895Sjamie bcopy(cpr->pr_ip6, ip6, 3984192895Sjamie cpr->pr_ip6s * sizeof(struct in6_addr)); 3985191673Sjamie } 3986185435Sbz#endif 3987192895Sjamie if (cpr->pr_ref == 0) { 3988192895Sjamie mtx_unlock(&cpr->pr_mtx); 3989191673Sjamie continue; 3990191673Sjamie } 3991191673Sjamie bzero(xp, sizeof(*xp)); 3992113275Smike xp->pr_version = XPRISON_VERSION; 3993192895Sjamie xp->pr_id = cpr->pr_id; 3994192895Sjamie xp->pr_state = cpr->pr_uref > 0 3995191673Sjamie ? PRISON_STATE_ALIVE : PRISON_STATE_DYING; 3996192895Sjamie strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path)); 3997194118Sjamie strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host)); 3998192895Sjamie strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name)); 3999185435Sbz#ifdef INET 4000192895Sjamie xp->pr_ip4s = cpr->pr_ip4s; 4001185435Sbz#endif 4002185435Sbz#ifdef INET6 4003192895Sjamie xp->pr_ip6s = cpr->pr_ip6s; 4004185435Sbz#endif 4005192895Sjamie mtx_unlock(&cpr->pr_mtx); 4006191673Sjamie error = SYSCTL_OUT(req, xp, sizeof(*xp)); 4007191673Sjamie if (error) 4008191673Sjamie break; 4009185435Sbz#ifdef INET 4010191673Sjamie if (xp->pr_ip4s > 0) { 4011191673Sjamie error = SYSCTL_OUT(req, ip4, 4012191673Sjamie xp->pr_ip4s * sizeof(struct in_addr)); 4013191673Sjamie if (error) 4014191673Sjamie break; 4015185435Sbz } 4016185435Sbz#endif 4017185435Sbz#ifdef INET6 4018191673Sjamie if (xp->pr_ip6s > 0) { 4019191673Sjamie error = SYSCTL_OUT(req, ip6, 4020191673Sjamie xp->pr_ip6s * sizeof(struct in6_addr)); 4021191673Sjamie if (error) 4022191673Sjamie break; 4023185435Sbz } 4024185435Sbz#endif 4025113275Smike } 4026168401Spjd sx_sunlock(&allprison_lock); 4027191673Sjamie free(xp, M_TEMP); 4028191673Sjamie#ifdef INET 4029191673Sjamie free(ip4, M_TEMP); 4030191673Sjamie#endif 4031191673Sjamie#ifdef INET6 4032191673Sjamie free(ip6, M_TEMP); 4033191673Sjamie#endif 4034167354Spjd return (error); 4035113275Smike} 4036113275Smike 4037187864SedSYSCTL_OID(_security_jail, OID_AUTO, list, 4038187864Sed CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4039187864Sed sysctl_jail_list, "S", "List of active jails"); 4040126004Spjd 4041126004Spjdstatic int 4042126004Spjdsysctl_jail_jailed(SYSCTL_HANDLER_ARGS) 4043126004Spjd{ 4044126004Spjd int error, injail; 4045126004Spjd 4046126004Spjd injail = jailed(req->td->td_ucred); 4047126004Spjd error = SYSCTL_OUT(req, &injail, sizeof(injail)); 4048126004Spjd 4049126004Spjd return (error); 4050126004Spjd} 4051192895Sjamie 4052187864SedSYSCTL_PROC(_security_jail, OID_AUTO, jailed, 4053187864Sed CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4054187864Sed sysctl_jail_jailed, "I", "Process in jail?"); 4055185435Sbz 4056192895Sjamie#if defined(INET) || defined(INET6) 4057193865SjamieSYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW, 4058192895Sjamie &jail_max_af_ips, 0, 4059192895Sjamie "Number of IP addresses a jail may have at most per address family"); 4060192895Sjamie#endif 4061192895Sjamie 4062192895Sjamie/* 4063192895Sjamie * Default parameters for jail(2) compatability. For historical reasons, 4064192895Sjamie * the sysctl names have varying similarity to the parameter names. Prisons 4065192895Sjamie * just see their own parameters, and can't change them. 4066192895Sjamie */ 4067192895Sjamiestatic int 4068192895Sjamiesysctl_jail_default_allow(SYSCTL_HANDLER_ARGS) 4069192895Sjamie{ 4070192895Sjamie struct prison *pr; 4071192895Sjamie int allow, error, i; 4072192895Sjamie 4073192895Sjamie pr = req->td->td_ucred->cr_prison; 4074192895Sjamie allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow; 4075192895Sjamie 4076192895Sjamie /* Get the current flag value, and convert it to a boolean. */ 4077192895Sjamie i = (allow & arg2) ? 1 : 0; 4078192895Sjamie if (arg1 != NULL) 4079192895Sjamie i = !i; 4080192895Sjamie error = sysctl_handle_int(oidp, &i, 0, req); 4081192895Sjamie if (error || !req->newptr) 4082192895Sjamie return (error); 4083192895Sjamie i = i ? arg2 : 0; 4084192895Sjamie if (arg1 != NULL) 4085192895Sjamie i ^= arg2; 4086192895Sjamie /* 4087192895Sjamie * The sysctls don't have CTLFLAGS_PRISON, so assume prison0 4088192895Sjamie * for writing. 4089192895Sjamie */ 4090192895Sjamie mtx_lock(&prison0.pr_mtx); 4091192895Sjamie jail_default_allow = (jail_default_allow & ~arg2) | i; 4092192895Sjamie mtx_unlock(&prison0.pr_mtx); 4093192895Sjamie return (0); 4094192895Sjamie} 4095192895Sjamie 4096192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed, 4097192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4098192895Sjamie NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I", 4099192895Sjamie "Processes in jail can set their hostnames"); 4100192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only, 4101192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4102192895Sjamie (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I", 4103192895Sjamie "Processes in jail are limited to creating UNIX/IP/route sockets only"); 4104192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed, 4105192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4106192895Sjamie NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I", 4107192895Sjamie "Processes in jail can use System V IPC primitives"); 4108192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets, 4109192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4110192895Sjamie NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I", 4111192895Sjamie "Prison root can create raw sockets"); 4112192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed, 4113192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4114192895Sjamie NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I", 4115192895Sjamie "Processes in jail can alter system file flags"); 4116192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed, 4117192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4118192895Sjamie NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I", 4119192895Sjamie "Processes in jail can mount/unmount jail-friendly file systems"); 4120192895Sjamie 4121192895Sjamiestatic int 4122192895Sjamiesysctl_jail_default_level(SYSCTL_HANDLER_ARGS) 4123192895Sjamie{ 4124192895Sjamie struct prison *pr; 4125192895Sjamie int level, error; 4126192895Sjamie 4127192895Sjamie pr = req->td->td_ucred->cr_prison; 4128192895Sjamie level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2); 4129192895Sjamie error = sysctl_handle_int(oidp, &level, 0, req); 4130192895Sjamie if (error || !req->newptr) 4131192895Sjamie return (error); 4132192895Sjamie *(int *)arg1 = level; 4133192895Sjamie return (0); 4134192895Sjamie} 4135192895Sjamie 4136192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs, 4137192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4138192895Sjamie &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs), 4139192895Sjamie sysctl_jail_default_level, "I", 4140192895Sjamie "Processes in jail cannot see all mounted file systems"); 4141192895Sjamie 4142192895Sjamie/* 4143192895Sjamie * Nodes to describe jail parameters. Maximum length of string parameters 4144192895Sjamie * is returned in the string itself, and the other parameters exist merely 4145192895Sjamie * to make themselves and their types known. 4146192895Sjamie */ 4147192895SjamieSYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0, 4148192895Sjamie "Jail parameters"); 4149192895Sjamie 4150192895Sjamieint 4151192895Sjamiesysctl_jail_param(SYSCTL_HANDLER_ARGS) 4152192895Sjamie{ 4153192895Sjamie int i; 4154192895Sjamie long l; 4155192895Sjamie size_t s; 4156192895Sjamie char numbuf[12]; 4157192895Sjamie 4158192895Sjamie switch (oidp->oid_kind & CTLTYPE) 4159192895Sjamie { 4160192895Sjamie case CTLTYPE_LONG: 4161192895Sjamie case CTLTYPE_ULONG: 4162192895Sjamie l = 0; 4163192895Sjamie#ifdef SCTL_MASK32 4164192895Sjamie if (!(req->flags & SCTL_MASK32)) 4165192895Sjamie#endif 4166192895Sjamie return (SYSCTL_OUT(req, &l, sizeof(l))); 4167192895Sjamie case CTLTYPE_INT: 4168192895Sjamie case CTLTYPE_UINT: 4169192895Sjamie i = 0; 4170192895Sjamie return (SYSCTL_OUT(req, &i, sizeof(i))); 4171192895Sjamie case CTLTYPE_STRING: 4172192895Sjamie snprintf(numbuf, sizeof(numbuf), "%d", arg2); 4173192895Sjamie return 4174192895Sjamie (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req)); 4175192895Sjamie case CTLTYPE_STRUCT: 4176192895Sjamie s = (size_t)arg2; 4177192895Sjamie return (SYSCTL_OUT(req, &s, sizeof(s))); 4178192895Sjamie } 4179192895Sjamie return (0); 4180192895Sjamie} 4181192895Sjamie 4182192895SjamieSYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID"); 4183192895SjamieSYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID"); 4184192895SjamieSYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name"); 4185192895SjamieSYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path"); 4186192895SjamieSYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW, 4187192895Sjamie "I", "Jail secure level"); 4188192895SjamieSYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW, 4189192895Sjamie "I", "Jail cannot see all mounted file systems"); 4190192895SjamieSYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW, 4191192895Sjamie "B", "Jail persistence"); 4192194251Sjamie#ifdef VIMAGE 4193194251SjamieSYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN, 4194195870Sjamie "E,jailsys", "Virtual network stack"); 4195194251Sjamie#endif 4196192895SjamieSYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD, 4197192895Sjamie "B", "Jail is in the process of shutting down"); 4198192895Sjamie 4199194762SjamieSYSCTL_JAIL_PARAM_NODE(children, "Number of child jails"); 4200194762SjamieSYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD, 4201194762Sjamie "I", "Current number of child jails"); 4202194762SjamieSYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW, 4203194762Sjamie "I", "Maximum number of child jails"); 4204194762Sjamie 4205195870SjamieSYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info"); 4206192895SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN, 4207192895Sjamie "Jail hostname"); 4208193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN, 4209193066Sjamie "Jail NIS domainname"); 4210193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN, 4211193066Sjamie "Jail host UUID"); 4212193066SjamieSYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW, 4213193066Sjamie "LU", "Jail host ID"); 4214192895Sjamie 4215192895SjamieSYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset"); 4216192895SjamieSYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID"); 4217192895Sjamie 4218192895Sjamie#ifdef INET 4219195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN, 4220195974Sjamie "Jail IPv4 address virtualization"); 4221192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr), 4222192895Sjamie "S,in_addr,a", "Jail IPv4 addresses"); 4223202468SbzSYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4224202468Sbz "B", "Do (not) use IPv4 source address selection rather than the " 4225202468Sbz "primary jail IPv4 address."); 4226192895Sjamie#endif 4227192895Sjamie#ifdef INET6 4228195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN, 4229195974Sjamie "Jail IPv6 address virtualization"); 4230192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr), 4231192895Sjamie "S,in6_addr,a", "Jail IPv6 addresses"); 4232202468SbzSYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4233202468Sbz "B", "Do (not) use IPv6 source address selection rather than the " 4234202468Sbz "primary jail IPv6 address."); 4235192895Sjamie#endif 4236192895Sjamie 4237192895SjamieSYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags"); 4238192895SjamieSYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW, 4239192895Sjamie "B", "Jail may set hostname"); 4240192895SjamieSYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW, 4241192895Sjamie "B", "Jail may use SYSV IPC"); 4242192895SjamieSYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW, 4243192895Sjamie "B", "Jail may create raw sockets"); 4244192895SjamieSYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW, 4245192895Sjamie "B", "Jail may alter system file flags"); 4246192895SjamieSYSCTL_JAIL_PARAM(_allow, mount, CTLTYPE_INT | CTLFLAG_RW, 4247192895Sjamie "B", "Jail may mount/unmount jail-friendly file systems"); 4248192895SjamieSYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW, 4249192895Sjamie "B", "Jail may set file quotas"); 4250192895SjamieSYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW, 4251192895Sjamie "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route"); 4252192895Sjamie 4253192895Sjamie 4254185435Sbz#ifdef DDB 4255191673Sjamie 4256191673Sjamiestatic void 4257191673Sjamiedb_show_prison(struct prison *pr) 4258185435Sbz{ 4259192895Sjamie int fi; 4260191673Sjamie#if defined(INET) || defined(INET6) 4261191673Sjamie int ii; 4262185435Sbz#endif 4263195870Sjamie unsigned jsf; 4264185435Sbz#ifdef INET6 4265185435Sbz char ip6buf[INET6_ADDRSTRLEN]; 4266185435Sbz#endif 4267185435Sbz 4268191673Sjamie db_printf("prison %p:\n", pr); 4269191673Sjamie db_printf(" jid = %d\n", pr->pr_id); 4270191673Sjamie db_printf(" name = %s\n", pr->pr_name); 4271192895Sjamie db_printf(" parent = %p\n", pr->pr_parent); 4272191673Sjamie db_printf(" ref = %d\n", pr->pr_ref); 4273191673Sjamie db_printf(" uref = %d\n", pr->pr_uref); 4274191673Sjamie db_printf(" path = %s\n", pr->pr_path); 4275191673Sjamie db_printf(" cpuset = %d\n", pr->pr_cpuset 4276191673Sjamie ? pr->pr_cpuset->cs_id : -1); 4277194251Sjamie#ifdef VIMAGE 4278194251Sjamie db_printf(" vnet = %p\n", pr->pr_vnet); 4279194251Sjamie#endif 4280191673Sjamie db_printf(" root = %p\n", pr->pr_root); 4281191673Sjamie db_printf(" securelevel = %d\n", pr->pr_securelevel); 4282202123Sbz db_printf(" children.max = %d\n", pr->pr_childmax); 4283202123Sbz db_printf(" children.cur = %d\n", pr->pr_childcount); 4284192895Sjamie db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children)); 4285192895Sjamie db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling)); 4286202123Sbz db_printf(" flags = 0x%x", pr->pr_flags); 4287192895Sjamie for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 4288192895Sjamie fi++) 4289192895Sjamie if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi))) 4290192895Sjamie db_printf(" %s", pr_flag_names[fi]); 4291195870Sjamie for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 4292195870Sjamie fi++) { 4293195870Sjamie jsf = pr->pr_flags & 4294195870Sjamie (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new); 4295195870Sjamie db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name, 4296195870Sjamie pr_flag_jailsys[fi].disable && 4297195870Sjamie (jsf == pr_flag_jailsys[fi].disable) ? "disable" 4298195870Sjamie : (jsf == pr_flag_jailsys[fi].new) ? "new" 4299195870Sjamie : "inherit"); 4300195870Sjamie } 4301202123Sbz db_printf(" allow = 0x%x", pr->pr_allow); 4302192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 4303192895Sjamie fi++) 4304192895Sjamie if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi))) 4305192895Sjamie db_printf(" %s", pr_allow_names[fi]); 4306191673Sjamie db_printf("\n"); 4307192895Sjamie db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs); 4308194118Sjamie db_printf(" host.hostname = %s\n", pr->pr_hostname); 4309194118Sjamie db_printf(" host.domainname = %s\n", pr->pr_domainname); 4310194118Sjamie db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid); 4311193066Sjamie db_printf(" host.hostid = %lu\n", pr->pr_hostid); 4312185435Sbz#ifdef INET 4313191673Sjamie db_printf(" ip4s = %d\n", pr->pr_ip4s); 4314191673Sjamie for (ii = 0; ii < pr->pr_ip4s; ii++) 4315191673Sjamie db_printf(" %s %s\n", 4316202123Sbz ii == 0 ? "ip4.addr =" : " ", 4317191673Sjamie inet_ntoa(pr->pr_ip4[ii])); 4318185435Sbz#endif 4319185435Sbz#ifdef INET6 4320191673Sjamie db_printf(" ip6s = %d\n", pr->pr_ip6s); 4321191673Sjamie for (ii = 0; ii < pr->pr_ip6s; ii++) 4322191673Sjamie db_printf(" %s %s\n", 4323202123Sbz ii == 0 ? "ip6.addr =" : " ", 4324191673Sjamie ip6_sprintf(ip6buf, &pr->pr_ip6[ii])); 4325191673Sjamie#endif 4326191673Sjamie} 4327191673Sjamie 4328191673SjamieDB_SHOW_COMMAND(prison, db_show_prison_command) 4329191673Sjamie{ 4330191673Sjamie struct prison *pr; 4331191673Sjamie 4332191673Sjamie if (!have_addr) { 4333192895Sjamie /* 4334192895Sjamie * Show all prisons in the list, and prison0 which is not 4335192895Sjamie * listed. 4336192895Sjamie */ 4337192895Sjamie db_show_prison(&prison0); 4338192895Sjamie if (!db_pager_quit) { 4339192895Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) { 4340192895Sjamie db_show_prison(pr); 4341192895Sjamie if (db_pager_quit) 4342192895Sjamie break; 4343192895Sjamie } 4344191673Sjamie } 4345191673Sjamie return; 4346191673Sjamie } 4347191673Sjamie 4348192895Sjamie if (addr == 0) 4349192895Sjamie pr = &prison0; 4350192895Sjamie else { 4351192895Sjamie /* Look for a prison with the ID and with references. */ 4352191673Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) 4353192895Sjamie if (pr->pr_id == addr && pr->pr_ref > 0) 4354191673Sjamie break; 4355192895Sjamie if (pr == NULL) 4356192895Sjamie /* Look again, without requiring a reference. */ 4357192895Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) 4358192895Sjamie if (pr->pr_id == addr) 4359192895Sjamie break; 4360192895Sjamie if (pr == NULL) 4361192895Sjamie /* Assume address points to a valid prison. */ 4362192895Sjamie pr = (struct prison *)addr; 4363192895Sjamie } 4364191673Sjamie db_show_prison(pr); 4365185435Sbz} 4366191673Sjamie 4367185435Sbz#endif /* DDB */ 4368