kern_jail.c revision 220137
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 220137 2011-03-29 17:47:25Z trasz $"); 31116182Sobrien 32193066Sjamie#include "opt_compat.h" 33185435Sbz#include "opt_ddb.h" 34185435Sbz#include "opt_inet.h" 35185435Sbz#include "opt_inet6.h" 36131177Spjd 3746155Sphk#include <sys/param.h> 3846155Sphk#include <sys/types.h> 3946155Sphk#include <sys/kernel.h> 4046155Sphk#include <sys/systm.h> 4146155Sphk#include <sys/errno.h> 4246155Sphk#include <sys/sysproto.h> 4346155Sphk#include <sys/malloc.h> 44192895Sjamie#include <sys/osd.h> 45164032Srwatson#include <sys/priv.h> 4646155Sphk#include <sys/proc.h> 47124882Srwatson#include <sys/taskqueue.h> 48177785Skib#include <sys/fcntl.h> 4946155Sphk#include <sys/jail.h> 5087275Srwatson#include <sys/lock.h> 5187275Srwatson#include <sys/mutex.h> 52220137Strasz#include <sys/racct.h> 53168401Spjd#include <sys/sx.h> 54193066Sjamie#include <sys/sysent.h> 55113275Smike#include <sys/namei.h> 56147185Spjd#include <sys/mount.h> 57113275Smike#include <sys/queue.h> 5846155Sphk#include <sys/socket.h> 59113275Smike#include <sys/syscallsubr.h> 6057163Srwatson#include <sys/sysctl.h> 61113275Smike#include <sys/vnode.h> 62196019Srwatson 6346155Sphk#include <net/if.h> 64196019Srwatson#include <net/vnet.h> 65196019Srwatson 6646155Sphk#include <netinet/in.h> 67196019Srwatson 68185435Sbz#ifdef DDB 69185435Sbz#include <ddb/ddb.h> 70185435Sbz#ifdef INET6 71185435Sbz#include <netinet6/in6_var.h> 72185435Sbz#endif /* INET6 */ 73185435Sbz#endif /* DDB */ 7446155Sphk 75163606Srwatson#include <security/mac/mac_framework.h> 76163606Srwatson 77195944Sjamie#define DEFAULT_HOSTUUID "00000000-0000-0000-0000-000000000000" 78195944Sjamie 7946155SphkMALLOC_DEFINE(M_PRISON, "prison", "Prison structures"); 8046155Sphk 81202468Sbz/* Keep struct prison prison0 and some code in kern_jail_set() readable. */ 82202468Sbz#ifdef INET 83202468Sbz#ifdef INET6 84202468Sbz#define _PR_IP_SADDRSEL PR_IP4_SADDRSEL|PR_IP6_SADDRSEL 85202468Sbz#else 86202468Sbz#define _PR_IP_SADDRSEL PR_IP4_SADDRSEL 87202468Sbz#endif 88202468Sbz#else /* !INET */ 89202468Sbz#ifdef INET6 90202468Sbz#define _PR_IP_SADDRSEL PR_IP6_SADDRSEL 91202468Sbz#else 92202468Sbz#define _PR_IP_SADDRSEL 0 93202468Sbz#endif 94202468Sbz#endif 95202468Sbz 96192895Sjamie/* prison0 describes what is "real" about the system. */ 97192895Sjamiestruct prison prison0 = { 98192895Sjamie .pr_id = 0, 99192895Sjamie .pr_name = "0", 100192895Sjamie .pr_ref = 1, 101192895Sjamie .pr_uref = 1, 102192895Sjamie .pr_path = "/", 103192895Sjamie .pr_securelevel = -1, 104194762Sjamie .pr_childmax = JAIL_MAX, 105195944Sjamie .pr_hostuuid = DEFAULT_HOSTUUID, 106201145Santoine .pr_children = LIST_HEAD_INITIALIZER(prison0.pr_children), 107196176Sbz#ifdef VIMAGE 108202468Sbz .pr_flags = PR_HOST|PR_VNET|_PR_IP_SADDRSEL, 109196176Sbz#else 110202468Sbz .pr_flags = PR_HOST|_PR_IP_SADDRSEL, 111196176Sbz#endif 112192895Sjamie .pr_allow = PR_ALLOW_ALL, 113192895Sjamie}; 114192895SjamieMTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF); 11557163Srwatson 116192895Sjamie/* allprison and lastprid are protected by allprison_lock. */ 117168401Spjdstruct sx allprison_lock; 118191673SjamieSX_SYSINIT(allprison_lock, &allprison_lock, "allprison"); 119191673Sjamiestruct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison); 120179881Sdelphijint lastprid = 0; 121113275Smike 122191673Sjamiestatic int do_jail_attach(struct thread *td, struct prison *pr); 123190466Sjamiestatic void prison_complete(void *context, int pending); 124191673Sjamiestatic void prison_deref(struct prison *pr, int flags); 125192895Sjamiestatic char *prison_path(struct prison *pr1, struct prison *pr2); 126192895Sjamiestatic void prison_remove_one(struct prison *pr); 127185435Sbz#ifdef INET 128190466Sjamiestatic int _prison_check_ip4(struct prison *pr, struct in_addr *ia); 129192895Sjamiestatic int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4); 130185435Sbz#endif 131185435Sbz#ifdef INET6 132190466Sjamiestatic int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6); 133192895Sjamiestatic int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6); 134185435Sbz#endif 135113275Smike 136191673Sjamie/* Flags for prison_deref */ 137191673Sjamie#define PD_DEREF 0x01 138191673Sjamie#define PD_DEUREF 0x02 139191673Sjamie#define PD_LOCKED 0x04 140191673Sjamie#define PD_LIST_SLOCKED 0x08 141191673Sjamie#define PD_LIST_XLOCKED 0x10 142113275Smike 143192895Sjamie/* 144216861Sbz * Parameter names corresponding to PR_* flag values. Size values are for kvm 145216861Sbz * as we cannot figure out the size of a sparse array, or an array without a 146216861Sbz * terminating entry. 147192895Sjamie */ 148192895Sjamiestatic char *pr_flag_names[] = { 149192895Sjamie [0] = "persist", 150202468Sbz#ifdef INET 151202468Sbz [7] = "ip4.saddrsel", 152202468Sbz#endif 153202468Sbz#ifdef INET6 154202468Sbz [8] = "ip6.saddrsel", 155202468Sbz#endif 156192895Sjamie}; 157216861Sbzconst size_t pr_flag_names_size = sizeof(pr_flag_names); 158192895Sjamie 159192895Sjamiestatic char *pr_flag_nonames[] = { 160192895Sjamie [0] = "nopersist", 161202468Sbz#ifdef INET 162202468Sbz [7] = "ip4.nosaddrsel", 163202468Sbz#endif 164202468Sbz#ifdef INET6 165202468Sbz [8] = "ip6.nosaddrsel", 166202468Sbz#endif 167195870Sjamie}; 168216861Sbzconst size_t pr_flag_nonames_size = sizeof(pr_flag_nonames); 169195870Sjamie 170195870Sjamiestruct jailsys_flags { 171195870Sjamie const char *name; 172195870Sjamie unsigned disable; 173195870Sjamie unsigned new; 174195870Sjamie} pr_flag_jailsys[] = { 175195870Sjamie { "host", 0, PR_HOST }, 176195870Sjamie#ifdef VIMAGE 177195870Sjamie { "vnet", 0, PR_VNET }, 178195870Sjamie#endif 179192895Sjamie#ifdef INET 180195870Sjamie { "ip4", PR_IP4_USER | PR_IP4_DISABLE, PR_IP4_USER }, 181192895Sjamie#endif 182192895Sjamie#ifdef INET6 183195870Sjamie { "ip6", PR_IP6_USER | PR_IP6_DISABLE, PR_IP6_USER }, 184192895Sjamie#endif 185192895Sjamie}; 186216861Sbzconst size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys); 187192895Sjamie 188192895Sjamiestatic char *pr_allow_names[] = { 189192895Sjamie "allow.set_hostname", 190192895Sjamie "allow.sysvipc", 191192895Sjamie "allow.raw_sockets", 192192895Sjamie "allow.chflags", 193192895Sjamie "allow.mount", 194192895Sjamie "allow.quotas", 195192895Sjamie "allow.socket_af", 196192895Sjamie}; 197216861Sbzconst size_t pr_allow_names_size = sizeof(pr_allow_names); 198192895Sjamie 199192895Sjamiestatic char *pr_allow_nonames[] = { 200192895Sjamie "allow.noset_hostname", 201192895Sjamie "allow.nosysvipc", 202192895Sjamie "allow.noraw_sockets", 203192895Sjamie "allow.nochflags", 204192895Sjamie "allow.nomount", 205192895Sjamie "allow.noquotas", 206192895Sjamie "allow.nosocket_af", 207192895Sjamie}; 208216861Sbzconst size_t pr_allow_nonames_size = sizeof(pr_allow_nonames); 209192895Sjamie 210196002Sjamie#define JAIL_DEFAULT_ALLOW PR_ALLOW_SET_HOSTNAME 211196002Sjamie#define JAIL_DEFAULT_ENFORCE_STATFS 2 212192895Sjamiestatic unsigned jail_default_allow = JAIL_DEFAULT_ALLOW; 213196002Sjamiestatic int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 214192895Sjamie#if defined(INET) || defined(INET6) 215193865Sjamiestatic unsigned jail_max_af_ips = 255; 216192895Sjamie#endif 217192895Sjamie 218192895Sjamie#ifdef INET 219185435Sbzstatic int 220185435Sbzqcmp_v4(const void *ip1, const void *ip2) 221185435Sbz{ 222185435Sbz in_addr_t iaa, iab; 223185435Sbz 224185435Sbz /* 225185435Sbz * We need to compare in HBO here to get the list sorted as expected 226185435Sbz * by the result of the code. Sorting NBO addresses gives you 227185435Sbz * interesting results. If you do not understand, do not try. 228185435Sbz */ 229185435Sbz iaa = ntohl(((const struct in_addr *)ip1)->s_addr); 230185435Sbz iab = ntohl(((const struct in_addr *)ip2)->s_addr); 231185435Sbz 232185435Sbz /* 233185435Sbz * Do not simply return the difference of the two numbers, the int is 234185435Sbz * not wide enough. 235185435Sbz */ 236185435Sbz if (iaa > iab) 237185435Sbz return (1); 238185435Sbz else if (iaa < iab) 239185435Sbz return (-1); 240185435Sbz else 241185435Sbz return (0); 242185435Sbz} 243185435Sbz#endif 244185435Sbz 245185435Sbz#ifdef INET6 246185435Sbzstatic int 247185435Sbzqcmp_v6(const void *ip1, const void *ip2) 248185435Sbz{ 249185435Sbz const struct in6_addr *ia6a, *ia6b; 250185435Sbz int i, rc; 251185435Sbz 252185435Sbz ia6a = (const struct in6_addr *)ip1; 253185435Sbz ia6b = (const struct in6_addr *)ip2; 254185435Sbz 255185435Sbz rc = 0; 256190466Sjamie for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) { 257185435Sbz if (ia6a->s6_addr[i] > ia6b->s6_addr[i]) 258185435Sbz rc = 1; 259185435Sbz else if (ia6a->s6_addr[i] < ia6b->s6_addr[i]) 260185435Sbz rc = -1; 261185435Sbz } 262185435Sbz return (rc); 263185435Sbz} 264185435Sbz#endif 265185435Sbz 266191673Sjamie/* 267191673Sjamie * struct jail_args { 268191673Sjamie * struct jail *jail; 269191673Sjamie * }; 270191673Sjamie */ 271191673Sjamieint 272191673Sjamiejail(struct thread *td, struct jail_args *uap) 273185435Sbz{ 274191673Sjamie uint32_t version; 275191673Sjamie int error; 276192895Sjamie struct jail j; 277185435Sbz 278191673Sjamie error = copyin(uap->jail, &version, sizeof(uint32_t)); 279191673Sjamie if (error) 280191673Sjamie return (error); 281185435Sbz 282191673Sjamie switch (version) { 283191673Sjamie case 0: 284191673Sjamie { 285191673Sjamie struct jail_v0 j0; 286185435Sbz 287192895Sjamie /* FreeBSD single IPv4 jails. */ 288192895Sjamie bzero(&j, sizeof(struct jail)); 289191673Sjamie error = copyin(uap->jail, &j0, sizeof(struct jail_v0)); 290191673Sjamie if (error) 291191673Sjamie return (error); 292192895Sjamie j.version = j0.version; 293192895Sjamie j.path = j0.path; 294192895Sjamie j.hostname = j0.hostname; 295192895Sjamie j.ip4s = j0.ip_number; 296191673Sjamie break; 297191673Sjamie } 298191673Sjamie 299191673Sjamie case 1: 300185435Sbz /* 301191673Sjamie * Version 1 was used by multi-IPv4 jail implementations 302191673Sjamie * that never made it into the official kernel. 303185435Sbz */ 304191673Sjamie return (EINVAL); 305185435Sbz 306191673Sjamie case 2: /* JAIL_API_VERSION */ 307191673Sjamie /* FreeBSD multi-IPv4/IPv6,noIP jails. */ 308191673Sjamie error = copyin(uap->jail, &j, sizeof(struct jail)); 309191673Sjamie if (error) 310191673Sjamie return (error); 311192895Sjamie break; 312192895Sjamie 313192895Sjamie default: 314192895Sjamie /* Sci-Fi jails are not supported, sorry. */ 315192895Sjamie return (EINVAL); 316192895Sjamie } 317192895Sjamie return (kern_jail(td, &j)); 318192895Sjamie} 319192895Sjamie 320192895Sjamieint 321192895Sjamiekern_jail(struct thread *td, struct jail *j) 322192895Sjamie{ 323193865Sjamie struct iovec optiov[2 * (4 324193865Sjamie + sizeof(pr_allow_names) / sizeof(pr_allow_names[0]) 325193865Sjamie#ifdef INET 326193865Sjamie + 1 327193865Sjamie#endif 328193865Sjamie#ifdef INET6 329193865Sjamie + 1 330193865Sjamie#endif 331193865Sjamie )]; 332192895Sjamie struct uio opt; 333192895Sjamie char *u_path, *u_hostname, *u_name; 334185435Sbz#ifdef INET 335193865Sjamie uint32_t ip4s; 336192895Sjamie struct in_addr *u_ip4; 337192895Sjamie#endif 338192895Sjamie#ifdef INET6 339192895Sjamie struct in6_addr *u_ip6; 340192895Sjamie#endif 341192895Sjamie size_t tmplen; 342192895Sjamie int error, enforce_statfs, fi; 343192895Sjamie 344192895Sjamie bzero(&optiov, sizeof(optiov)); 345192895Sjamie opt.uio_iov = optiov; 346192895Sjamie opt.uio_iovcnt = 0; 347192895Sjamie opt.uio_offset = -1; 348192895Sjamie opt.uio_resid = -1; 349192895Sjamie opt.uio_segflg = UIO_SYSSPACE; 350192895Sjamie opt.uio_rw = UIO_READ; 351192895Sjamie opt.uio_td = td; 352192895Sjamie 353192895Sjamie /* Set permissions for top-level jails from sysctls. */ 354192895Sjamie if (!jailed(td->td_ucred)) { 355192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / 356192895Sjamie sizeof(pr_allow_names[0]); fi++) { 357192895Sjamie optiov[opt.uio_iovcnt].iov_base = 358192895Sjamie (jail_default_allow & (1 << fi)) 359192895Sjamie ? pr_allow_names[fi] : pr_allow_nonames[fi]; 360192895Sjamie optiov[opt.uio_iovcnt].iov_len = 361192895Sjamie strlen(optiov[opt.uio_iovcnt].iov_base) + 1; 362192895Sjamie opt.uio_iovcnt += 2; 363192895Sjamie } 364192895Sjamie optiov[opt.uio_iovcnt].iov_base = "enforce_statfs"; 365192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs"); 366192895Sjamie opt.uio_iovcnt++; 367192895Sjamie enforce_statfs = jail_default_enforce_statfs; 368192895Sjamie optiov[opt.uio_iovcnt].iov_base = &enforce_statfs; 369192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs); 370192895Sjamie opt.uio_iovcnt++; 371192895Sjamie } 372192895Sjamie 373192895Sjamie tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN; 374192895Sjamie#ifdef INET 375192895Sjamie ip4s = (j->version == 0) ? 1 : j->ip4s; 376192895Sjamie if (ip4s > jail_max_af_ips) 377192895Sjamie return (EINVAL); 378192895Sjamie tmplen += ip4s * sizeof(struct in_addr); 379191673Sjamie#else 380192895Sjamie if (j->ip4s > 0) 381192895Sjamie return (EINVAL); 382191673Sjamie#endif 383191673Sjamie#ifdef INET6 384192895Sjamie if (j->ip6s > jail_max_af_ips) 385192895Sjamie return (EINVAL); 386192895Sjamie tmplen += j->ip6s * sizeof(struct in6_addr); 387191673Sjamie#else 388192895Sjamie if (j->ip6s > 0) 389192895Sjamie return (EINVAL); 390191673Sjamie#endif 391192895Sjamie u_path = malloc(tmplen, M_TEMP, M_WAITOK); 392192895Sjamie u_hostname = u_path + MAXPATHLEN; 393192895Sjamie u_name = u_hostname + MAXHOSTNAMELEN; 394191673Sjamie#ifdef INET 395192895Sjamie u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN); 396191673Sjamie#endif 397191673Sjamie#ifdef INET6 398191673Sjamie#ifdef INET 399192895Sjamie u_ip6 = (struct in6_addr *)(u_ip4 + ip4s); 400191673Sjamie#else 401192895Sjamie u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN); 402191673Sjamie#endif 403191673Sjamie#endif 404192895Sjamie optiov[opt.uio_iovcnt].iov_base = "path"; 405192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("path"); 406192895Sjamie opt.uio_iovcnt++; 407192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_path; 408192895Sjamie error = copyinstr(j->path, u_path, MAXPATHLEN, 409192895Sjamie &optiov[opt.uio_iovcnt].iov_len); 410192895Sjamie if (error) { 411192895Sjamie free(u_path, M_TEMP); 412192895Sjamie return (error); 413192895Sjamie } 414192895Sjamie opt.uio_iovcnt++; 415192895Sjamie optiov[opt.uio_iovcnt].iov_base = "host.hostname"; 416192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname"); 417192895Sjamie opt.uio_iovcnt++; 418192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_hostname; 419192895Sjamie error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN, 420192895Sjamie &optiov[opt.uio_iovcnt].iov_len); 421192895Sjamie if (error) { 422192895Sjamie free(u_path, M_TEMP); 423192895Sjamie return (error); 424192895Sjamie } 425192895Sjamie opt.uio_iovcnt++; 426192895Sjamie if (j->jailname != NULL) { 427192895Sjamie optiov[opt.uio_iovcnt].iov_base = "name"; 428192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("name"); 429192895Sjamie opt.uio_iovcnt++; 430192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_name; 431192895Sjamie error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN, 432192895Sjamie &optiov[opt.uio_iovcnt].iov_len); 433191673Sjamie if (error) { 434191673Sjamie free(u_path, M_TEMP); 435191673Sjamie return (error); 436191673Sjamie } 437192895Sjamie opt.uio_iovcnt++; 438192895Sjamie } 439191673Sjamie#ifdef INET 440192895Sjamie optiov[opt.uio_iovcnt].iov_base = "ip4.addr"; 441192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr"); 442192895Sjamie opt.uio_iovcnt++; 443192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_ip4; 444192895Sjamie optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr); 445192895Sjamie if (j->version == 0) 446192895Sjamie u_ip4->s_addr = j->ip4s; 447192895Sjamie else { 448192895Sjamie error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len); 449191673Sjamie if (error) { 450191673Sjamie free(u_path, M_TEMP); 451191673Sjamie return (error); 452191673Sjamie } 453192895Sjamie } 454192895Sjamie opt.uio_iovcnt++; 455185435Sbz#endif 456185435Sbz#ifdef INET6 457192895Sjamie optiov[opt.uio_iovcnt].iov_base = "ip6.addr"; 458192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr"); 459192895Sjamie opt.uio_iovcnt++; 460192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_ip6; 461192895Sjamie optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr); 462192895Sjamie error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len); 463192895Sjamie if (error) { 464192895Sjamie free(u_path, M_TEMP); 465192895Sjamie return (error); 466192895Sjamie } 467192895Sjamie opt.uio_iovcnt++; 468185435Sbz#endif 469192895Sjamie KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]), 470192895Sjamie ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt)); 471191673Sjamie error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH); 472191673Sjamie free(u_path, M_TEMP); 473191673Sjamie return (error); 474185435Sbz} 475185435Sbz 476192895Sjamie 477191673Sjamie/* 478191673Sjamie * struct jail_set_args { 479191673Sjamie * struct iovec *iovp; 480191673Sjamie * unsigned int iovcnt; 481191673Sjamie * int flags; 482191673Sjamie * }; 483191673Sjamie */ 484191673Sjamieint 485191673Sjamiejail_set(struct thread *td, struct jail_set_args *uap) 486185435Sbz{ 487191673Sjamie struct uio *auio; 488191673Sjamie int error; 489191673Sjamie 490191673Sjamie /* Check that we have an even number of iovecs. */ 491191673Sjamie if (uap->iovcnt & 1) 492191673Sjamie return (EINVAL); 493191673Sjamie 494191673Sjamie error = copyinuio(uap->iovp, uap->iovcnt, &auio); 495191673Sjamie if (error) 496191673Sjamie return (error); 497191673Sjamie error = kern_jail_set(td, auio, uap->flags); 498191673Sjamie free(auio, M_IOV); 499191673Sjamie return (error); 500191673Sjamie} 501191673Sjamie 502191673Sjamieint 503191673Sjamiekern_jail_set(struct thread *td, struct uio *optuio, int flags) 504191673Sjamie{ 505191673Sjamie struct nameidata nd; 506185435Sbz#ifdef INET 507190466Sjamie struct in_addr *ip4; 508185435Sbz#endif 509185435Sbz#ifdef INET6 510185435Sbz struct in6_addr *ip6; 511185435Sbz#endif 512191673Sjamie struct vfsopt *opt; 513191673Sjamie struct vfsoptlist *opts; 514196135Sbz struct prison *pr, *deadpr, *mypr, *ppr, *tpr; 515191673Sjamie struct vnode *root; 516196835Sjamie char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid; 517192895Sjamie#if defined(INET) || defined(INET6) 518196135Sbz struct prison *tppr; 519191673Sjamie void *op; 520192895Sjamie#endif 521193066Sjamie unsigned long hid; 522192895Sjamie size_t namelen, onamelen; 523192895Sjamie int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos; 524195870Sjamie int gotchildmax, gotenforce, gothid, gotslevel; 525195870Sjamie int fi, jid, jsys, len, level; 526194762Sjamie int childmax, slevel, vfslocked; 527191673Sjamie#if defined(INET) || defined(INET6) 528192895Sjamie int ii, ij; 529191673Sjamie#endif 530191673Sjamie#ifdef INET 531195974Sjamie int ip4s, redo_ip4; 532191673Sjamie#endif 533191673Sjamie#ifdef INET6 534195974Sjamie int ip6s, redo_ip6; 535191673Sjamie#endif 536191673Sjamie unsigned pr_flags, ch_flags; 537192895Sjamie unsigned pr_allow, ch_allow, tallow; 538191673Sjamie char numbuf[12]; 539185435Sbz 540191673Sjamie error = priv_check(td, PRIV_JAIL_SET); 541191673Sjamie if (!error && (flags & JAIL_ATTACH)) 542191673Sjamie error = priv_check(td, PRIV_JAIL_ATTACH); 543191673Sjamie if (error) 544191673Sjamie return (error); 545192895Sjamie mypr = ppr = td->td_ucred->cr_prison; 546194762Sjamie if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0) 547192895Sjamie return (EPERM); 548191673Sjamie if (flags & ~JAIL_SET_MASK) 549191673Sjamie return (EINVAL); 550191673Sjamie 551185435Sbz /* 552191673Sjamie * Check all the parameters before committing to anything. Not all 553191673Sjamie * errors can be caught early, but we may as well try. Also, this 554191673Sjamie * takes care of some expensive stuff (path lookup) before getting 555191673Sjamie * the allprison lock. 556185435Sbz * 557191673Sjamie * XXX Jails are not filesystems, and jail parameters are not mount 558191673Sjamie * options. But it makes more sense to re-use the vfsopt code 559191673Sjamie * than duplicate it under a different name. 560185435Sbz */ 561191673Sjamie error = vfs_buildopts(optuio, &opts); 562191673Sjamie if (error) 563191673Sjamie return (error); 564185435Sbz#ifdef INET 565185435Sbz ip4 = NULL; 566185435Sbz#endif 567185435Sbz#ifdef INET6 568185435Sbz ip6 = NULL; 569185435Sbz#endif 570191673Sjamie 571191673Sjamie error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 572191673Sjamie if (error == ENOENT) 573191673Sjamie jid = 0; 574191673Sjamie else if (error != 0) 575191673Sjamie goto done_free; 576191673Sjamie 577191673Sjamie error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel)); 578191673Sjamie if (error == ENOENT) 579191673Sjamie gotslevel = 0; 580191673Sjamie else if (error != 0) 581191673Sjamie goto done_free; 582191673Sjamie else 583191673Sjamie gotslevel = 1; 584191673Sjamie 585194762Sjamie error = 586194762Sjamie vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax)); 587194762Sjamie if (error == ENOENT) 588194762Sjamie gotchildmax = 0; 589194762Sjamie else if (error != 0) 590194762Sjamie goto done_free; 591194762Sjamie else 592194762Sjamie gotchildmax = 1; 593194762Sjamie 594192895Sjamie error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce)); 595212436Sjamie if (error == ENOENT) 596212436Sjamie gotenforce = 0; 597212436Sjamie else if (error != 0) 598192895Sjamie goto done_free; 599212436Sjamie else if (enforce < 0 || enforce > 2) { 600212436Sjamie error = EINVAL; 601212436Sjamie goto done_free; 602212436Sjamie } else 603212436Sjamie gotenforce = 1; 604192895Sjamie 605191673Sjamie pr_flags = ch_flags = 0; 606192895Sjamie for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 607192895Sjamie fi++) { 608192895Sjamie if (pr_flag_names[fi] == NULL) 609192895Sjamie continue; 610192895Sjamie vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi); 611192895Sjamie vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi); 612192895Sjamie } 613191673Sjamie ch_flags |= pr_flags; 614195870Sjamie for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 615195870Sjamie fi++) { 616195870Sjamie error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys, 617195870Sjamie sizeof(jsys)); 618195870Sjamie if (error == ENOENT) 619195870Sjamie continue; 620195870Sjamie if (error != 0) 621195870Sjamie goto done_free; 622195870Sjamie switch (jsys) { 623195870Sjamie case JAIL_SYS_DISABLE: 624195870Sjamie if (!pr_flag_jailsys[fi].disable) { 625195870Sjamie error = EINVAL; 626195870Sjamie goto done_free; 627195870Sjamie } 628195870Sjamie pr_flags |= pr_flag_jailsys[fi].disable; 629195870Sjamie break; 630195870Sjamie case JAIL_SYS_NEW: 631195870Sjamie pr_flags |= pr_flag_jailsys[fi].new; 632195870Sjamie break; 633195870Sjamie case JAIL_SYS_INHERIT: 634195870Sjamie break; 635195870Sjamie default: 636195870Sjamie error = EINVAL; 637195870Sjamie goto done_free; 638195870Sjamie } 639195870Sjamie ch_flags |= 640195870Sjamie pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable; 641195870Sjamie } 642211085Sjamie if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE 643211085Sjamie && !(pr_flags & PR_PERSIST)) { 644211085Sjamie error = EINVAL; 645211085Sjamie vfs_opterror(opts, "new jail must persist or attach"); 646211085Sjamie goto done_errmsg; 647211085Sjamie } 648194251Sjamie#ifdef VIMAGE 649194251Sjamie if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) { 650194251Sjamie error = EINVAL; 651194251Sjamie vfs_opterror(opts, "vnet cannot be changed after creation"); 652194251Sjamie goto done_errmsg; 653194251Sjamie } 654194251Sjamie#endif 655195974Sjamie#ifdef INET 656195974Sjamie if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) { 657195974Sjamie error = EINVAL; 658195974Sjamie vfs_opterror(opts, "ip4 cannot be changed after creation"); 659195974Sjamie goto done_errmsg; 660195974Sjamie } 661195974Sjamie#endif 662195974Sjamie#ifdef INET6 663195974Sjamie if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) { 664195974Sjamie error = EINVAL; 665195974Sjamie vfs_opterror(opts, "ip6 cannot be changed after creation"); 666195974Sjamie goto done_errmsg; 667195974Sjamie } 668195974Sjamie#endif 669191673Sjamie 670192895Sjamie pr_allow = ch_allow = 0; 671192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 672192895Sjamie fi++) { 673192895Sjamie vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi); 674192895Sjamie vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi); 675192895Sjamie } 676192895Sjamie ch_allow |= pr_allow; 677192895Sjamie 678191673Sjamie error = vfs_getopt(opts, "name", (void **)&name, &len); 679191673Sjamie if (error == ENOENT) 680191673Sjamie name = NULL; 681191673Sjamie else if (error != 0) 682191673Sjamie goto done_free; 683191673Sjamie else { 684191673Sjamie if (len == 0 || name[len - 1] != '\0') { 685191673Sjamie error = EINVAL; 686191673Sjamie goto done_free; 687191673Sjamie } 688191673Sjamie if (len > MAXHOSTNAMELEN) { 689191673Sjamie error = ENAMETOOLONG; 690191673Sjamie goto done_free; 691191673Sjamie } 692191673Sjamie } 693191673Sjamie 694191673Sjamie error = vfs_getopt(opts, "host.hostname", (void **)&host, &len); 695191673Sjamie if (error == ENOENT) 696191673Sjamie host = NULL; 697191673Sjamie else if (error != 0) 698191673Sjamie goto done_free; 699191673Sjamie else { 700193066Sjamie ch_flags |= PR_HOST; 701193066Sjamie pr_flags |= PR_HOST; 702191673Sjamie if (len == 0 || host[len - 1] != '\0') { 703191673Sjamie error = EINVAL; 704191673Sjamie goto done_free; 705191673Sjamie } 706191673Sjamie if (len > MAXHOSTNAMELEN) { 707191673Sjamie error = ENAMETOOLONG; 708191673Sjamie goto done_free; 709191673Sjamie } 710191673Sjamie } 711191673Sjamie 712193066Sjamie error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len); 713193066Sjamie if (error == ENOENT) 714193066Sjamie domain = NULL; 715193066Sjamie else if (error != 0) 716193066Sjamie goto done_free; 717193066Sjamie else { 718193066Sjamie ch_flags |= PR_HOST; 719193066Sjamie pr_flags |= PR_HOST; 720193066Sjamie if (len == 0 || domain[len - 1] != '\0') { 721193066Sjamie error = EINVAL; 722193066Sjamie goto done_free; 723193066Sjamie } 724193066Sjamie if (len > MAXHOSTNAMELEN) { 725193066Sjamie error = ENAMETOOLONG; 726193066Sjamie goto done_free; 727193066Sjamie } 728193066Sjamie } 729193066Sjamie 730193066Sjamie error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len); 731193066Sjamie if (error == ENOENT) 732193066Sjamie uuid = NULL; 733193066Sjamie else if (error != 0) 734193066Sjamie goto done_free; 735193066Sjamie else { 736193066Sjamie ch_flags |= PR_HOST; 737193066Sjamie pr_flags |= PR_HOST; 738193066Sjamie if (len == 0 || uuid[len - 1] != '\0') { 739193066Sjamie error = EINVAL; 740193066Sjamie goto done_free; 741193066Sjamie } 742193066Sjamie if (len > HOSTUUIDLEN) { 743193066Sjamie error = ENAMETOOLONG; 744193066Sjamie goto done_free; 745193066Sjamie } 746193066Sjamie } 747193066Sjamie 748205014Snwhitehorn#ifdef COMPAT_FREEBSD32 749217896Sdchagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 750193066Sjamie uint32_t hid32; 751193066Sjamie 752193066Sjamie error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32)); 753193066Sjamie hid = hid32; 754193066Sjamie } else 755193066Sjamie#endif 756193066Sjamie error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid)); 757193066Sjamie if (error == ENOENT) 758193066Sjamie gothid = 0; 759193066Sjamie else if (error != 0) 760193066Sjamie goto done_free; 761193066Sjamie else { 762193066Sjamie gothid = 1; 763193066Sjamie ch_flags |= PR_HOST; 764193066Sjamie pr_flags |= PR_HOST; 765193066Sjamie } 766193066Sjamie 767185435Sbz#ifdef INET 768191673Sjamie error = vfs_getopt(opts, "ip4.addr", &op, &ip4s); 769191673Sjamie if (error == ENOENT) 770195870Sjamie ip4s = (pr_flags & PR_IP4_DISABLE) ? 0 : -1; 771191673Sjamie else if (error != 0) 772191673Sjamie goto done_free; 773191673Sjamie else if (ip4s & (sizeof(*ip4) - 1)) { 774191673Sjamie error = EINVAL; 775191673Sjamie goto done_free; 776192895Sjamie } else { 777195870Sjamie ch_flags |= PR_IP4_USER | PR_IP4_DISABLE; 778195870Sjamie if (ip4s == 0) 779195870Sjamie pr_flags |= PR_IP4_USER | PR_IP4_DISABLE; 780195870Sjamie else { 781195870Sjamie pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER; 782192895Sjamie ip4s /= sizeof(*ip4); 783192895Sjamie if (ip4s > jail_max_af_ips) { 784185435Sbz error = EINVAL; 785192895Sjamie vfs_opterror(opts, "too many IPv4 addresses"); 786192895Sjamie goto done_errmsg; 787185435Sbz } 788195974Sjamie ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 789192895Sjamie bcopy(op, ip4, ip4s * sizeof(*ip4)); 790192895Sjamie /* 791192895Sjamie * IP addresses are all sorted but ip[0] to preserve 792192895Sjamie * the primary IP address as given from userland. 793192895Sjamie * This special IP is used for unbound outgoing 794202116Sbz * connections as well for "loopback" traffic in case 795202116Sbz * source address selection cannot find any more fitting 796202116Sbz * address to connect from. 797192895Sjamie */ 798192895Sjamie if (ip4s > 1) 799192895Sjamie qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4); 800192895Sjamie /* 801192895Sjamie * Check for duplicate addresses and do some simple 802192895Sjamie * zero and broadcast checks. If users give other bogus 803192895Sjamie * addresses it is their problem. 804192895Sjamie * 805192895Sjamie * We do not have to care about byte order for these 806192895Sjamie * checks so we will do them in NBO. 807192895Sjamie */ 808192895Sjamie for (ii = 0; ii < ip4s; ii++) { 809192895Sjamie if (ip4[ii].s_addr == INADDR_ANY || 810192895Sjamie ip4[ii].s_addr == INADDR_BROADCAST) { 811192895Sjamie error = EINVAL; 812192895Sjamie goto done_free; 813192895Sjamie } 814192895Sjamie if ((ii+1) < ip4s && 815192895Sjamie (ip4[0].s_addr == ip4[ii+1].s_addr || 816192895Sjamie ip4[ii].s_addr == ip4[ii+1].s_addr)) { 817192895Sjamie error = EINVAL; 818192895Sjamie goto done_free; 819192895Sjamie } 820192895Sjamie } 821185435Sbz } 822191673Sjamie } 823191673Sjamie#endif 824185435Sbz 825185435Sbz#ifdef INET6 826191673Sjamie error = vfs_getopt(opts, "ip6.addr", &op, &ip6s); 827191673Sjamie if (error == ENOENT) 828195870Sjamie ip6s = (pr_flags & PR_IP6_DISABLE) ? 0 : -1; 829191673Sjamie else if (error != 0) 830191673Sjamie goto done_free; 831191673Sjamie else if (ip6s & (sizeof(*ip6) - 1)) { 832191673Sjamie error = EINVAL; 833191673Sjamie goto done_free; 834192895Sjamie } else { 835195870Sjamie ch_flags |= PR_IP6_USER | PR_IP6_DISABLE; 836195870Sjamie if (ip6s == 0) 837195870Sjamie pr_flags |= PR_IP6_USER | PR_IP6_DISABLE; 838195870Sjamie else { 839195870Sjamie pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER; 840192895Sjamie ip6s /= sizeof(*ip6); 841192895Sjamie if (ip6s > jail_max_af_ips) { 842185435Sbz error = EINVAL; 843192895Sjamie vfs_opterror(opts, "too many IPv6 addresses"); 844192895Sjamie goto done_errmsg; 845185435Sbz } 846195974Sjamie ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 847192895Sjamie bcopy(op, ip6, ip6s * sizeof(*ip6)); 848192895Sjamie if (ip6s > 1) 849192895Sjamie qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6); 850192895Sjamie for (ii = 0; ii < ip6s; ii++) { 851192895Sjamie if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) { 852192895Sjamie error = EINVAL; 853192895Sjamie goto done_free; 854192895Sjamie } 855192895Sjamie if ((ii+1) < ip6s && 856192895Sjamie (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) || 857192895Sjamie IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1]))) 858192895Sjamie { 859192895Sjamie error = EINVAL; 860192895Sjamie goto done_free; 861192895Sjamie } 862192895Sjamie } 863185435Sbz } 864191673Sjamie } 865185435Sbz#endif 866185435Sbz 867195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6)) 868195945Sjamie if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 869195945Sjamie error = EINVAL; 870195945Sjamie vfs_opterror(opts, 871195945Sjamie "vnet jails cannot have IP address restrictions"); 872195945Sjamie goto done_errmsg; 873195945Sjamie } 874195945Sjamie#endif 875195945Sjamie 876191673Sjamie root = NULL; 877191673Sjamie error = vfs_getopt(opts, "path", (void **)&path, &len); 878191673Sjamie if (error == ENOENT) 879191673Sjamie path = NULL; 880191673Sjamie else if (error != 0) 881191673Sjamie goto done_free; 882191673Sjamie else { 883191673Sjamie if (flags & JAIL_UPDATE) { 884191673Sjamie error = EINVAL; 885191673Sjamie vfs_opterror(opts, 886191673Sjamie "path cannot be changed after creation"); 887191673Sjamie goto done_errmsg; 888191673Sjamie } 889191673Sjamie if (len == 0 || path[len - 1] != '\0') { 890191673Sjamie error = EINVAL; 891191673Sjamie goto done_free; 892191673Sjamie } 893191673Sjamie if (len < 2 || (len == 2 && path[0] == '/')) 894191673Sjamie path = NULL; 895191673Sjamie else { 896192895Sjamie /* Leave room for a real-root full pathname. */ 897192895Sjamie if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/") 898192895Sjamie ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) { 899192895Sjamie error = ENAMETOOLONG; 900192895Sjamie goto done_free; 901192895Sjamie } 902191673Sjamie NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_SYSSPACE, 903191673Sjamie path, td); 904191673Sjamie error = namei(&nd); 905191673Sjamie if (error) 906191673Sjamie goto done_free; 907191673Sjamie vfslocked = NDHASGIANT(&nd); 908191673Sjamie root = nd.ni_vp; 909191673Sjamie NDFREE(&nd, NDF_ONLY_PNBUF); 910191673Sjamie if (root->v_type != VDIR) { 911191673Sjamie error = ENOTDIR; 912191673Sjamie vrele(root); 913191673Sjamie VFS_UNLOCK_GIANT(vfslocked); 914191673Sjamie goto done_free; 915191673Sjamie } 916191673Sjamie VFS_UNLOCK_GIANT(vfslocked); 917191673Sjamie } 918191673Sjamie } 919185435Sbz 920191673Sjamie /* 921191673Sjamie * Grab the allprison lock before letting modules check their 922191673Sjamie * parameters. Once we have it, do not let go so we'll have a 923191673Sjamie * consistent view of the OSD list. 924191673Sjamie */ 925191673Sjamie sx_xlock(&allprison_lock); 926191673Sjamie error = osd_jail_call(NULL, PR_METHOD_CHECK, opts); 927191673Sjamie if (error) 928191673Sjamie goto done_unlock_list; 929185435Sbz 930191673Sjamie /* By now, all parameters should have been noted. */ 931191673Sjamie TAILQ_FOREACH(opt, opts, link) { 932191673Sjamie if (!opt->seen && strcmp(opt->name, "errmsg")) { 933191673Sjamie error = EINVAL; 934191673Sjamie vfs_opterror(opts, "unknown parameter: %s", opt->name); 935191673Sjamie goto done_unlock_list; 936191673Sjamie } 937191673Sjamie } 938191673Sjamie 939185435Sbz /* 940191673Sjamie * See if we are creating a new record or updating an existing one. 941191673Sjamie * This abuses the file error codes ENOENT and EEXIST. 942185435Sbz */ 943191673Sjamie cuflags = flags & (JAIL_CREATE | JAIL_UPDATE); 944191673Sjamie if (!cuflags) { 945191673Sjamie error = EINVAL; 946191673Sjamie vfs_opterror(opts, "no valid operation (create or update)"); 947191673Sjamie goto done_unlock_list; 948191673Sjamie } 949191673Sjamie pr = NULL; 950196835Sjamie namelc = NULL; 951196835Sjamie if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) { 952196835Sjamie namelc = strrchr(name, '.'); 953196835Sjamie jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10); 954196835Sjamie if (*p != '\0') 955196835Sjamie jid = 0; 956196835Sjamie } 957191673Sjamie if (jid != 0) { 958192895Sjamie /* 959192895Sjamie * See if a requested jid already exists. There is an 960192895Sjamie * information leak here if the jid exists but is not within 961192895Sjamie * the caller's jail hierarchy. Jail creators will get EEXIST 962192895Sjamie * even though they cannot see the jail, and CREATE | UPDATE 963192895Sjamie * will return ENOENT which is not normally a valid error. 964192895Sjamie */ 965191673Sjamie if (jid < 0) { 966191673Sjamie error = EINVAL; 967191673Sjamie vfs_opterror(opts, "negative jid"); 968191673Sjamie goto done_unlock_list; 969191673Sjamie } 970191673Sjamie pr = prison_find(jid); 971191673Sjamie if (pr != NULL) { 972192895Sjamie ppr = pr->pr_parent; 973191673Sjamie /* Create: jid must not exist. */ 974191673Sjamie if (cuflags == JAIL_CREATE) { 975191673Sjamie mtx_unlock(&pr->pr_mtx); 976191673Sjamie error = EEXIST; 977191673Sjamie vfs_opterror(opts, "jail %d already exists", 978191673Sjamie jid); 979191673Sjamie goto done_unlock_list; 980191673Sjamie } 981192895Sjamie if (!prison_ischild(mypr, pr)) { 982192895Sjamie mtx_unlock(&pr->pr_mtx); 983192895Sjamie pr = NULL; 984192895Sjamie } else if (pr->pr_uref == 0) { 985191673Sjamie if (!(flags & JAIL_DYING)) { 986191673Sjamie mtx_unlock(&pr->pr_mtx); 987191673Sjamie error = ENOENT; 988191673Sjamie vfs_opterror(opts, "jail %d is dying", 989191673Sjamie jid); 990191673Sjamie goto done_unlock_list; 991191673Sjamie } else if ((flags & JAIL_ATTACH) || 992191673Sjamie (pr_flags & PR_PERSIST)) { 993191673Sjamie /* 994191673Sjamie * A dying jail might be resurrected 995191673Sjamie * (via attach or persist), but first 996191673Sjamie * it must determine if another jail 997191673Sjamie * has claimed its name. Accomplish 998191673Sjamie * this by implicitly re-setting the 999191673Sjamie * name. 1000191673Sjamie */ 1001191673Sjamie if (name == NULL) 1002192895Sjamie name = prison_name(mypr, pr); 1003191673Sjamie } 1004191673Sjamie } 1005191673Sjamie } 1006191673Sjamie if (pr == NULL) { 1007191673Sjamie /* Update: jid must exist. */ 1008191673Sjamie if (cuflags == JAIL_UPDATE) { 1009191673Sjamie error = ENOENT; 1010191673Sjamie vfs_opterror(opts, "jail %d not found", jid); 1011191673Sjamie goto done_unlock_list; 1012191673Sjamie } 1013191673Sjamie } 1014191673Sjamie } 1015191673Sjamie /* 1016191673Sjamie * If the caller provided a name, look for a jail by that name. 1017191673Sjamie * This has different semantics for creates and updates keyed by jid 1018191673Sjamie * (where the name must not already exist in a different jail), 1019191673Sjamie * and updates keyed by the name itself (where the name must exist 1020191673Sjamie * because that is the jail being updated). 1021191673Sjamie */ 1022191673Sjamie if (name != NULL) { 1023196835Sjamie namelc = strrchr(name, '.'); 1024196835Sjamie if (namelc == NULL) 1025196835Sjamie namelc = name; 1026196835Sjamie else { 1027192895Sjamie /* 1028192895Sjamie * This is a hierarchical name. Split it into the 1029192895Sjamie * parent and child names, and make sure the parent 1030192895Sjamie * exists or matches an already found jail. 1031192895Sjamie */ 1032196835Sjamie *namelc = '\0'; 1033192895Sjamie if (pr != NULL) { 1034196835Sjamie if (strncmp(name, ppr->pr_name, namelc - name) 1035196835Sjamie || ppr->pr_name[namelc - name] != '\0') { 1036192895Sjamie mtx_unlock(&pr->pr_mtx); 1037192895Sjamie error = EINVAL; 1038192895Sjamie vfs_opterror(opts, 1039192895Sjamie "cannot change jail's parent"); 1040192895Sjamie goto done_unlock_list; 1041192895Sjamie } 1042192895Sjamie } else { 1043192895Sjamie ppr = prison_find_name(mypr, name); 1044192895Sjamie if (ppr == NULL) { 1045192895Sjamie error = ENOENT; 1046192895Sjamie vfs_opterror(opts, 1047192895Sjamie "jail \"%s\" not found", name); 1048192895Sjamie goto done_unlock_list; 1049192895Sjamie } 1050192895Sjamie mtx_unlock(&ppr->pr_mtx); 1051192895Sjamie } 1052196835Sjamie name = ++namelc; 1053192895Sjamie } 1054191673Sjamie if (name[0] != '\0') { 1055192895Sjamie namelen = 1056192895Sjamie (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 1057192895Sjamie name_again: 1058191673Sjamie deadpr = NULL; 1059192895Sjamie FOREACH_PRISON_CHILD(ppr, tpr) { 1060191673Sjamie if (tpr != pr && tpr->pr_ref > 0 && 1061192895Sjamie !strcmp(tpr->pr_name + namelen, name)) { 1062191673Sjamie if (pr == NULL && 1063191673Sjamie cuflags != JAIL_CREATE) { 1064191673Sjamie mtx_lock(&tpr->pr_mtx); 1065191673Sjamie if (tpr->pr_ref > 0) { 1066191673Sjamie /* 1067191673Sjamie * Use this jail 1068191673Sjamie * for updates. 1069191673Sjamie */ 1070191673Sjamie if (tpr->pr_uref > 0) { 1071191673Sjamie pr = tpr; 1072191673Sjamie break; 1073191673Sjamie } 1074191673Sjamie deadpr = tpr; 1075191673Sjamie } 1076191673Sjamie mtx_unlock(&tpr->pr_mtx); 1077191673Sjamie } else if (tpr->pr_uref > 0) { 1078191673Sjamie /* 1079191673Sjamie * Create, or update(jid): 1080191673Sjamie * name must not exist in an 1081192895Sjamie * active sibling jail. 1082191673Sjamie */ 1083191673Sjamie error = EEXIST; 1084191673Sjamie if (pr != NULL) 1085191673Sjamie mtx_unlock(&pr->pr_mtx); 1086191673Sjamie vfs_opterror(opts, 1087191673Sjamie "jail \"%s\" already exists", 1088191673Sjamie name); 1089191673Sjamie goto done_unlock_list; 1090191673Sjamie } 1091191673Sjamie } 1092191673Sjamie } 1093191673Sjamie /* If no active jail is found, use a dying one. */ 1094191673Sjamie if (deadpr != NULL && pr == NULL) { 1095191673Sjamie if (flags & JAIL_DYING) { 1096191673Sjamie mtx_lock(&deadpr->pr_mtx); 1097191673Sjamie if (deadpr->pr_ref == 0) { 1098191673Sjamie mtx_unlock(&deadpr->pr_mtx); 1099191673Sjamie goto name_again; 1100191673Sjamie } 1101191673Sjamie pr = deadpr; 1102191673Sjamie } else if (cuflags == JAIL_UPDATE) { 1103191673Sjamie error = ENOENT; 1104191673Sjamie vfs_opterror(opts, 1105191673Sjamie "jail \"%s\" is dying", name); 1106191673Sjamie goto done_unlock_list; 1107191673Sjamie } 1108191673Sjamie } 1109191673Sjamie /* Update: name must exist if no jid. */ 1110191673Sjamie else if (cuflags == JAIL_UPDATE && pr == NULL) { 1111191673Sjamie error = ENOENT; 1112191673Sjamie vfs_opterror(opts, "jail \"%s\" not found", 1113191673Sjamie name); 1114191673Sjamie goto done_unlock_list; 1115191673Sjamie } 1116191673Sjamie } 1117191673Sjamie } 1118191673Sjamie /* Update: must provide a jid or name. */ 1119191673Sjamie else if (cuflags == JAIL_UPDATE && pr == NULL) { 1120191673Sjamie error = ENOENT; 1121191673Sjamie vfs_opterror(opts, "update specified no jail"); 1122191673Sjamie goto done_unlock_list; 1123191673Sjamie } 1124185435Sbz 1125191673Sjamie /* If there's no prison to update, create a new one and link it in. */ 1126191673Sjamie if (pr == NULL) { 1127194762Sjamie for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent) 1128194762Sjamie if (tpr->pr_childcount >= tpr->pr_childmax) { 1129194762Sjamie error = EPERM; 1130194762Sjamie vfs_opterror(opts, "prison limit exceeded"); 1131194762Sjamie goto done_unlock_list; 1132194762Sjamie } 1133191673Sjamie created = 1; 1134192895Sjamie mtx_lock(&ppr->pr_mtx); 1135192895Sjamie if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) { 1136192895Sjamie mtx_unlock(&ppr->pr_mtx); 1137192895Sjamie error = ENOENT; 1138192895Sjamie vfs_opterror(opts, "parent jail went away!"); 1139192895Sjamie goto done_unlock_list; 1140192895Sjamie } 1141192895Sjamie ppr->pr_ref++; 1142192895Sjamie ppr->pr_uref++; 1143192895Sjamie mtx_unlock(&ppr->pr_mtx); 1144191673Sjamie pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO); 1145191673Sjamie if (jid == 0) { 1146191673Sjamie /* Find the next free jid. */ 1147191673Sjamie jid = lastprid + 1; 1148191673Sjamie findnext: 1149191673Sjamie if (jid == JAIL_MAX) 1150191673Sjamie jid = 1; 1151191673Sjamie TAILQ_FOREACH(tpr, &allprison, pr_list) { 1152191673Sjamie if (tpr->pr_id < jid) 1153191673Sjamie continue; 1154191673Sjamie if (tpr->pr_id > jid || tpr->pr_ref == 0) { 1155191673Sjamie TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1156191673Sjamie break; 1157191673Sjamie } 1158191673Sjamie if (jid == lastprid) { 1159191673Sjamie error = EAGAIN; 1160191673Sjamie vfs_opterror(opts, 1161191673Sjamie "no available jail IDs"); 1162191673Sjamie free(pr, M_PRISON); 1163192895Sjamie prison_deref(ppr, PD_DEREF | 1164192895Sjamie PD_DEUREF | PD_LIST_XLOCKED); 1165192895Sjamie goto done_releroot; 1166191673Sjamie } 1167191673Sjamie jid++; 1168191673Sjamie goto findnext; 1169191673Sjamie } 1170191673Sjamie lastprid = jid; 1171191673Sjamie } else { 1172191673Sjamie /* 1173191673Sjamie * The jail already has a jid (that did not yet exist), 1174191673Sjamie * so just find where to insert it. 1175191673Sjamie */ 1176191673Sjamie TAILQ_FOREACH(tpr, &allprison, pr_list) 1177191673Sjamie if (tpr->pr_id >= jid) { 1178191673Sjamie TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1179191673Sjamie break; 1180191673Sjamie } 1181191673Sjamie } 1182191673Sjamie if (tpr == NULL) 1183191673Sjamie TAILQ_INSERT_TAIL(&allprison, pr, pr_list); 1184192895Sjamie LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling); 1185192895Sjamie for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 1186194762Sjamie tpr->pr_childcount++; 1187185435Sbz 1188192895Sjamie pr->pr_parent = ppr; 1189191673Sjamie pr->pr_id = jid; 1190192895Sjamie 1191192895Sjamie /* Set some default values, and inherit some from the parent. */ 1192191673Sjamie if (name == NULL) 1193191673Sjamie name = ""; 1194191673Sjamie if (path == NULL) { 1195191673Sjamie path = "/"; 1196192895Sjamie root = mypr->pr_root; 1197191673Sjamie vref(root); 1198191673Sjamie } 1199220137Strasz racct_create(&pr->pr_racct); 1200195944Sjamie strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN); 1201195944Sjamie pr->pr_flags |= PR_HOST; 1202195945Sjamie#if defined(INET) || defined(INET6) 1203195945Sjamie#ifdef VIMAGE 1204195945Sjamie if (!(pr_flags & PR_VNET)) 1205195945Sjamie#endif 1206195945Sjamie { 1207192895Sjamie#ifdef INET 1208195974Sjamie if (!(ch_flags & PR_IP4_USER)) 1209195974Sjamie pr->pr_flags |= 1210195974Sjamie PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE; 1211195974Sjamie else if (!(pr_flags & PR_IP4_USER)) { 1212195974Sjamie pr->pr_flags |= ppr->pr_flags & PR_IP4; 1213195974Sjamie if (ppr->pr_ip4 != NULL) { 1214195974Sjamie pr->pr_ip4s = ppr->pr_ip4s; 1215195974Sjamie pr->pr_ip4 = malloc(pr->pr_ip4s * 1216195974Sjamie sizeof(struct in_addr), M_PRISON, 1217195974Sjamie M_WAITOK); 1218195974Sjamie bcopy(ppr->pr_ip4, pr->pr_ip4, 1219195974Sjamie pr->pr_ip4s * sizeof(*pr->pr_ip4)); 1220195974Sjamie } 1221195974Sjamie } 1222192895Sjamie#endif 1223192895Sjamie#ifdef INET6 1224195974Sjamie if (!(ch_flags & PR_IP6_USER)) 1225195974Sjamie pr->pr_flags |= 1226195974Sjamie PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE; 1227195974Sjamie else if (!(pr_flags & PR_IP6_USER)) { 1228195974Sjamie pr->pr_flags |= ppr->pr_flags & PR_IP6; 1229195974Sjamie if (ppr->pr_ip6 != NULL) { 1230195974Sjamie pr->pr_ip6s = ppr->pr_ip6s; 1231195974Sjamie pr->pr_ip6 = malloc(pr->pr_ip6s * 1232195974Sjamie sizeof(struct in6_addr), M_PRISON, 1233195974Sjamie M_WAITOK); 1234195974Sjamie bcopy(ppr->pr_ip6, pr->pr_ip6, 1235195974Sjamie pr->pr_ip6s * sizeof(*pr->pr_ip6)); 1236195974Sjamie } 1237195974Sjamie } 1238192895Sjamie#endif 1239195945Sjamie } 1240195945Sjamie#endif 1241202468Sbz /* Source address selection is always on by default. */ 1242202468Sbz pr->pr_flags |= _PR_IP_SADDRSEL; 1243202468Sbz 1244192895Sjamie pr->pr_securelevel = ppr->pr_securelevel; 1245192895Sjamie pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow; 1246196002Sjamie pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 1247191673Sjamie 1248192895Sjamie LIST_INIT(&pr->pr_children); 1249192895Sjamie mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK); 1250191673Sjamie 1251194251Sjamie#ifdef VIMAGE 1252194251Sjamie /* Allocate a new vnet if specified. */ 1253194251Sjamie pr->pr_vnet = (pr_flags & PR_VNET) 1254194251Sjamie ? vnet_alloc() : ppr->pr_vnet; 1255194251Sjamie#endif 1256185435Sbz /* 1257191673Sjamie * Allocate a dedicated cpuset for each jail. 1258191673Sjamie * Unlike other initial settings, this may return an erorr. 1259185435Sbz */ 1260192895Sjamie error = cpuset_create_root(ppr, &pr->pr_cpuset); 1261191673Sjamie if (error) { 1262191673Sjamie prison_deref(pr, PD_LIST_XLOCKED); 1263191673Sjamie goto done_releroot; 1264191673Sjamie } 1265185435Sbz 1266191673Sjamie mtx_lock(&pr->pr_mtx); 1267185435Sbz /* 1268191673Sjamie * New prisons do not yet have a reference, because we do not 1269191673Sjamie * want other to see the incomplete prison once the 1270191673Sjamie * allprison_lock is downgraded. 1271185435Sbz */ 1272191673Sjamie } else { 1273191673Sjamie created = 0; 1274195974Sjamie /* 1275195974Sjamie * Grab a reference for existing prisons, to ensure they 1276195974Sjamie * continue to exist for the duration of the call. 1277195974Sjamie */ 1278195974Sjamie pr->pr_ref++; 1279195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6)) 1280195945Sjamie if ((pr->pr_flags & PR_VNET) && 1281195945Sjamie (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 1282195945Sjamie error = EINVAL; 1283195945Sjamie vfs_opterror(opts, 1284195945Sjamie "vnet jails cannot have IP address restrictions"); 1285195945Sjamie goto done_deref_locked; 1286195945Sjamie } 1287195945Sjamie#endif 1288195974Sjamie#ifdef INET 1289195974Sjamie if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 1290195974Sjamie error = EINVAL; 1291195974Sjamie vfs_opterror(opts, 1292195974Sjamie "ip4 cannot be changed after creation"); 1293195974Sjamie goto done_deref_locked; 1294195974Sjamie } 1295195974Sjamie#endif 1296195974Sjamie#ifdef INET6 1297195974Sjamie if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 1298195974Sjamie error = EINVAL; 1299195974Sjamie vfs_opterror(opts, 1300195974Sjamie "ip6 cannot be changed after creation"); 1301195974Sjamie goto done_deref_locked; 1302195974Sjamie } 1303195974Sjamie#endif 1304191673Sjamie } 1305185435Sbz 1306191673Sjamie /* Do final error checking before setting anything. */ 1307192895Sjamie if (gotslevel) { 1308192895Sjamie if (slevel < ppr->pr_securelevel) { 1309192895Sjamie error = EPERM; 1310192895Sjamie goto done_deref_locked; 1311192895Sjamie } 1312192895Sjamie } 1313194762Sjamie if (gotchildmax) { 1314194762Sjamie if (childmax >= ppr->pr_childmax) { 1315194762Sjamie error = EPERM; 1316194762Sjamie goto done_deref_locked; 1317194762Sjamie } 1318194762Sjamie } 1319192895Sjamie if (gotenforce) { 1320192895Sjamie if (enforce < ppr->pr_enforce_statfs) { 1321192895Sjamie error = EPERM; 1322192895Sjamie goto done_deref_locked; 1323192895Sjamie } 1324192895Sjamie } 1325185435Sbz#ifdef INET 1326195974Sjamie if (ip4s > 0) { 1327192895Sjamie if (ppr->pr_flags & PR_IP4) { 1328195974Sjamie /* 1329195974Sjamie * Make sure the new set of IP addresses is a 1330195974Sjamie * subset of the parent's list. Don't worry 1331195974Sjamie * about the parent being unlocked, as any 1332195974Sjamie * setting is done with allprison_lock held. 1333195974Sjamie */ 1334195974Sjamie for (ij = 0; ij < ppr->pr_ip4s; ij++) 1335195974Sjamie if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 1336195974Sjamie break; 1337195974Sjamie if (ij == ppr->pr_ip4s) { 1338195974Sjamie error = EPERM; 1339195974Sjamie goto done_deref_locked; 1340195974Sjamie } 1341195974Sjamie if (ip4s > 1) { 1342195974Sjamie for (ii = ij = 1; ii < ip4s; ii++) { 1343195974Sjamie if (ip4[ii].s_addr == 1344195974Sjamie ppr->pr_ip4[0].s_addr) 1345195974Sjamie continue; 1346195974Sjamie for (; ij < ppr->pr_ip4s; ij++) 1347195974Sjamie if (ip4[ii].s_addr == 1348195974Sjamie ppr->pr_ip4[ij].s_addr) 1349195974Sjamie break; 1350195974Sjamie if (ij == ppr->pr_ip4s) 1351195974Sjamie break; 1352192895Sjamie } 1353192895Sjamie if (ij == ppr->pr_ip4s) { 1354192895Sjamie error = EPERM; 1355192895Sjamie goto done_deref_locked; 1356192895Sjamie } 1357192895Sjamie } 1358192895Sjamie } 1359195974Sjamie /* 1360195974Sjamie * Check for conflicting IP addresses. We permit them 1361195974Sjamie * if there is no more than one IP on each jail. If 1362195974Sjamie * there is a duplicate on a jail with more than one 1363195974Sjamie * IP stop checking and return error. 1364195974Sjamie */ 1365195974Sjamie tppr = ppr; 1366195945Sjamie#ifdef VIMAGE 1367195974Sjamie for (; tppr != &prison0; tppr = tppr->pr_parent) 1368195974Sjamie if (tppr->pr_flags & PR_VNET) 1369195974Sjamie break; 1370195945Sjamie#endif 1371195974Sjamie FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1372195974Sjamie if (tpr == pr || 1373195945Sjamie#ifdef VIMAGE 1374195974Sjamie (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1375195945Sjamie#endif 1376195974Sjamie tpr->pr_uref == 0) { 1377192895Sjamie descend = 0; 1378195974Sjamie continue; 1379195974Sjamie } 1380195974Sjamie if (!(tpr->pr_flags & PR_IP4_USER)) 1381195974Sjamie continue; 1382195974Sjamie descend = 0; 1383195974Sjamie if (tpr->pr_ip4 == NULL || 1384195974Sjamie (ip4s == 1 && tpr->pr_ip4s == 1)) 1385195974Sjamie continue; 1386195974Sjamie for (ii = 0; ii < ip4s; ii++) { 1387195974Sjamie if (_prison_check_ip4(tpr, &ip4[ii]) == 0) { 1388195974Sjamie error = EADDRINUSE; 1389195974Sjamie vfs_opterror(opts, 1390195974Sjamie "IPv4 addresses clash"); 1391195974Sjamie goto done_deref_locked; 1392192895Sjamie } 1393192895Sjamie } 1394192895Sjamie } 1395192895Sjamie } 1396185435Sbz#endif 1397191673Sjamie#ifdef INET6 1398195974Sjamie if (ip6s > 0) { 1399192895Sjamie if (ppr->pr_flags & PR_IP6) { 1400195974Sjamie /* 1401195974Sjamie * Make sure the new set of IP addresses is a 1402195974Sjamie * subset of the parent's list. 1403195974Sjamie */ 1404195974Sjamie for (ij = 0; ij < ppr->pr_ip6s; ij++) 1405195974Sjamie if (IN6_ARE_ADDR_EQUAL(&ip6[0], 1406195974Sjamie &ppr->pr_ip6[ij])) 1407195974Sjamie break; 1408195974Sjamie if (ij == ppr->pr_ip6s) { 1409195974Sjamie error = EPERM; 1410195974Sjamie goto done_deref_locked; 1411195974Sjamie } 1412195974Sjamie if (ip6s > 1) { 1413195974Sjamie for (ii = ij = 1; ii < ip6s; ii++) { 1414195974Sjamie if (IN6_ARE_ADDR_EQUAL(&ip6[ii], 1415195974Sjamie &ppr->pr_ip6[0])) 1416195974Sjamie continue; 1417195974Sjamie for (; ij < ppr->pr_ip6s; ij++) 1418195974Sjamie if (IN6_ARE_ADDR_EQUAL( 1419195974Sjamie &ip6[ii], &ppr->pr_ip6[ij])) 1420195974Sjamie break; 1421195974Sjamie if (ij == ppr->pr_ip6s) 1422195974Sjamie break; 1423192895Sjamie } 1424192895Sjamie if (ij == ppr->pr_ip6s) { 1425192895Sjamie error = EPERM; 1426192895Sjamie goto done_deref_locked; 1427192895Sjamie } 1428192895Sjamie } 1429192895Sjamie } 1430195974Sjamie /* Check for conflicting IP addresses. */ 1431195974Sjamie tppr = ppr; 1432195945Sjamie#ifdef VIMAGE 1433195974Sjamie for (; tppr != &prison0; tppr = tppr->pr_parent) 1434195974Sjamie if (tppr->pr_flags & PR_VNET) 1435195974Sjamie break; 1436195945Sjamie#endif 1437195974Sjamie FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1438195974Sjamie if (tpr == pr || 1439195945Sjamie#ifdef VIMAGE 1440195974Sjamie (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1441195945Sjamie#endif 1442195974Sjamie tpr->pr_uref == 0) { 1443192895Sjamie descend = 0; 1444195974Sjamie continue; 1445195974Sjamie } 1446195974Sjamie if (!(tpr->pr_flags & PR_IP6_USER)) 1447195974Sjamie continue; 1448195974Sjamie descend = 0; 1449195974Sjamie if (tpr->pr_ip6 == NULL || 1450195974Sjamie (ip6s == 1 && tpr->pr_ip6s == 1)) 1451195974Sjamie continue; 1452195974Sjamie for (ii = 0; ii < ip6s; ii++) { 1453195974Sjamie if (_prison_check_ip6(tpr, &ip6[ii]) == 0) { 1454195974Sjamie error = EADDRINUSE; 1455195974Sjamie vfs_opterror(opts, 1456195974Sjamie "IPv6 addresses clash"); 1457195974Sjamie goto done_deref_locked; 1458192895Sjamie } 1459192895Sjamie } 1460191673Sjamie } 1461192895Sjamie } 1462191673Sjamie#endif 1463192895Sjamie onamelen = namelen = 0; 1464192895Sjamie if (name != NULL) { 1465191673Sjamie /* Give a default name of the jid. */ 1466191673Sjamie if (name[0] == '\0') 1467191673Sjamie snprintf(name = numbuf, sizeof(numbuf), "%d", jid); 1468196835Sjamie else if (*namelc == '0' || (strtoul(namelc, &p, 10) != jid && 1469196835Sjamie *p == '\0')) { 1470191673Sjamie error = EINVAL; 1471196835Sjamie vfs_opterror(opts, 1472196835Sjamie "name cannot be numeric (unless it is the jid)"); 1473192895Sjamie goto done_deref_locked; 1474191673Sjamie } 1475191673Sjamie /* 1476192895Sjamie * Make sure the name isn't too long for the prison or its 1477192895Sjamie * children. 1478191673Sjamie */ 1479192895Sjamie onamelen = strlen(pr->pr_name); 1480192895Sjamie namelen = strlen(name); 1481192895Sjamie if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) { 1482192895Sjamie error = ENAMETOOLONG; 1483192895Sjamie goto done_deref_locked; 1484192895Sjamie } 1485192895Sjamie FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 1486192895Sjamie if (strlen(tpr->pr_name) + (namelen - onamelen) >= 1487192895Sjamie sizeof(pr->pr_name)) { 1488192895Sjamie error = ENAMETOOLONG; 1489192895Sjamie goto done_deref_locked; 1490192895Sjamie } 1491192895Sjamie } 1492191673Sjamie } 1493192895Sjamie if (pr_allow & ~ppr->pr_allow) { 1494192895Sjamie error = EPERM; 1495192895Sjamie goto done_deref_locked; 1496192895Sjamie } 1497185435Sbz 1498191673Sjamie /* Set the parameters of the prison. */ 1499191673Sjamie#ifdef INET 1500192895Sjamie redo_ip4 = 0; 1501195974Sjamie if (pr_flags & PR_IP4_USER) { 1502195974Sjamie pr->pr_flags |= PR_IP4; 1503195974Sjamie free(pr->pr_ip4, M_PRISON); 1504195974Sjamie pr->pr_ip4s = ip4s; 1505195974Sjamie pr->pr_ip4 = ip4; 1506195974Sjamie ip4 = NULL; 1507192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1508195945Sjamie#ifdef VIMAGE 1509195945Sjamie if (tpr->pr_flags & PR_VNET) { 1510195945Sjamie descend = 0; 1511195945Sjamie continue; 1512195945Sjamie } 1513195945Sjamie#endif 1514192895Sjamie if (prison_restrict_ip4(tpr, NULL)) { 1515192895Sjamie redo_ip4 = 1; 1516192895Sjamie descend = 0; 1517192895Sjamie } 1518192895Sjamie } 1519185435Sbz } 1520191673Sjamie#endif 1521191673Sjamie#ifdef INET6 1522192895Sjamie redo_ip6 = 0; 1523195974Sjamie if (pr_flags & PR_IP6_USER) { 1524195974Sjamie pr->pr_flags |= PR_IP6; 1525195974Sjamie free(pr->pr_ip6, M_PRISON); 1526195974Sjamie pr->pr_ip6s = ip6s; 1527195974Sjamie pr->pr_ip6 = ip6; 1528195974Sjamie ip6 = NULL; 1529192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1530195945Sjamie#ifdef VIMAGE 1531195945Sjamie if (tpr->pr_flags & PR_VNET) { 1532195945Sjamie descend = 0; 1533195945Sjamie continue; 1534195945Sjamie } 1535195945Sjamie#endif 1536192895Sjamie if (prison_restrict_ip6(tpr, NULL)) { 1537192895Sjamie redo_ip6 = 1; 1538192895Sjamie descend = 0; 1539192895Sjamie } 1540192895Sjamie } 1541191673Sjamie } 1542191673Sjamie#endif 1543192895Sjamie if (gotslevel) { 1544191673Sjamie pr->pr_securelevel = slevel; 1545192895Sjamie /* Set all child jails to be at least this level. */ 1546192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1547192895Sjamie if (tpr->pr_securelevel < slevel) 1548192895Sjamie tpr->pr_securelevel = slevel; 1549192895Sjamie } 1550194762Sjamie if (gotchildmax) { 1551194762Sjamie pr->pr_childmax = childmax; 1552194762Sjamie /* Set all child jails to under this limit. */ 1553194762Sjamie FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level) 1554194762Sjamie if (tpr->pr_childmax > childmax - level) 1555194762Sjamie tpr->pr_childmax = childmax > level 1556194762Sjamie ? childmax - level : 0; 1557194762Sjamie } 1558192895Sjamie if (gotenforce) { 1559192895Sjamie pr->pr_enforce_statfs = enforce; 1560192895Sjamie /* Pass this restriction on to the children. */ 1561192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1562192895Sjamie if (tpr->pr_enforce_statfs < enforce) 1563192895Sjamie tpr->pr_enforce_statfs = enforce; 1564192895Sjamie } 1565192895Sjamie if (name != NULL) { 1566192895Sjamie if (ppr == &prison0) 1567192895Sjamie strlcpy(pr->pr_name, name, sizeof(pr->pr_name)); 1568192895Sjamie else 1569192895Sjamie snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s", 1570192895Sjamie ppr->pr_name, name); 1571192895Sjamie /* Change this component of child names. */ 1572192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1573192895Sjamie bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen, 1574192895Sjamie strlen(tpr->pr_name + onamelen) + 1); 1575192895Sjamie bcopy(pr->pr_name, tpr->pr_name, namelen); 1576192895Sjamie } 1577192895Sjamie } 1578191673Sjamie if (path != NULL) { 1579192895Sjamie /* Try to keep a real-rooted full pathname. */ 1580192895Sjamie if (path[0] == '/' && strcmp(mypr->pr_path, "/")) 1581192895Sjamie snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s", 1582192895Sjamie mypr->pr_path, path); 1583192895Sjamie else 1584192895Sjamie strlcpy(pr->pr_path, path, sizeof(pr->pr_path)); 1585191673Sjamie pr->pr_root = root; 1586191673Sjamie } 1587193066Sjamie if (PR_HOST & ch_flags & ~pr_flags) { 1588193066Sjamie if (pr->pr_flags & PR_HOST) { 1589193066Sjamie /* 1590193066Sjamie * Copy the parent's host info. As with pr_ip4 above, 1591193066Sjamie * the lack of a lock on the parent is not a problem; 1592193066Sjamie * it is always set with allprison_lock at least 1593193066Sjamie * shared, and is held exclusively here. 1594193066Sjamie */ 1595194118Sjamie strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname, 1596194118Sjamie sizeof(pr->pr_hostname)); 1597194118Sjamie strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname, 1598194118Sjamie sizeof(pr->pr_domainname)); 1599194118Sjamie strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid, 1600194118Sjamie sizeof(pr->pr_hostuuid)); 1601193066Sjamie pr->pr_hostid = pr->pr_parent->pr_hostid; 1602193066Sjamie } 1603193066Sjamie } else if (host != NULL || domain != NULL || uuid != NULL || gothid) { 1604193066Sjamie /* Set this prison, and any descendants without PR_HOST. */ 1605193066Sjamie if (host != NULL) 1606194118Sjamie strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname)); 1607193066Sjamie if (domain != NULL) 1608194118Sjamie strlcpy(pr->pr_domainname, domain, 1609194118Sjamie sizeof(pr->pr_domainname)); 1610193066Sjamie if (uuid != NULL) 1611194118Sjamie strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid)); 1612193066Sjamie if (gothid) 1613193066Sjamie pr->pr_hostid = hid; 1614193066Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1615193066Sjamie if (tpr->pr_flags & PR_HOST) 1616193066Sjamie descend = 0; 1617193066Sjamie else { 1618193066Sjamie if (host != NULL) 1619194118Sjamie strlcpy(tpr->pr_hostname, 1620194118Sjamie pr->pr_hostname, 1621194118Sjamie sizeof(tpr->pr_hostname)); 1622193066Sjamie if (domain != NULL) 1623194118Sjamie strlcpy(tpr->pr_domainname, 1624194118Sjamie pr->pr_domainname, 1625194118Sjamie sizeof(tpr->pr_domainname)); 1626193066Sjamie if (uuid != NULL) 1627194118Sjamie strlcpy(tpr->pr_hostuuid, 1628194118Sjamie pr->pr_hostuuid, 1629194118Sjamie sizeof(tpr->pr_hostuuid)); 1630193066Sjamie if (gothid) 1631193066Sjamie tpr->pr_hostid = hid; 1632193066Sjamie } 1633193066Sjamie } 1634193066Sjamie } 1635192895Sjamie if ((tallow = ch_allow & ~pr_allow)) { 1636192895Sjamie /* Clear allow bits in all children. */ 1637192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1638192895Sjamie tpr->pr_allow &= ~tallow; 1639192895Sjamie } 1640192895Sjamie pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow; 1641191673Sjamie /* 1642191673Sjamie * Persistent prisons get an extra reference, and prisons losing their 1643191673Sjamie * persist flag lose that reference. Only do this for existing prisons 1644191673Sjamie * for now, so new ones will remain unseen until after the module 1645191673Sjamie * handlers have completed. 1646191673Sjamie */ 1647191673Sjamie if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) { 1648191673Sjamie if (pr_flags & PR_PERSIST) { 1649191673Sjamie pr->pr_ref++; 1650191673Sjamie pr->pr_uref++; 1651191673Sjamie } else { 1652191673Sjamie pr->pr_ref--; 1653191673Sjamie pr->pr_uref--; 1654191673Sjamie } 1655191673Sjamie } 1656191673Sjamie pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags; 1657191673Sjamie mtx_unlock(&pr->pr_mtx); 1658185435Sbz 1659192895Sjamie /* Locks may have prevented a complete restriction of child IP 1660192895Sjamie * addresses. If so, allocate some more memory and try again. 1661192895Sjamie */ 1662192895Sjamie#ifdef INET 1663192895Sjamie while (redo_ip4) { 1664192895Sjamie ip4s = pr->pr_ip4s; 1665192895Sjamie ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 1666192895Sjamie mtx_lock(&pr->pr_mtx); 1667192895Sjamie redo_ip4 = 0; 1668192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1669195945Sjamie#ifdef VIMAGE 1670195945Sjamie if (tpr->pr_flags & PR_VNET) { 1671195945Sjamie descend = 0; 1672195945Sjamie continue; 1673195945Sjamie } 1674195945Sjamie#endif 1675192895Sjamie if (prison_restrict_ip4(tpr, ip4)) { 1676192895Sjamie if (ip4 != NULL) 1677192895Sjamie ip4 = NULL; 1678192895Sjamie else 1679192895Sjamie redo_ip4 = 1; 1680192895Sjamie } 1681192895Sjamie } 1682192895Sjamie mtx_unlock(&pr->pr_mtx); 1683192895Sjamie } 1684192895Sjamie#endif 1685192895Sjamie#ifdef INET6 1686192895Sjamie while (redo_ip6) { 1687192895Sjamie ip6s = pr->pr_ip6s; 1688192895Sjamie ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 1689192895Sjamie mtx_lock(&pr->pr_mtx); 1690192895Sjamie redo_ip6 = 0; 1691192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1692195945Sjamie#ifdef VIMAGE 1693195945Sjamie if (tpr->pr_flags & PR_VNET) { 1694195945Sjamie descend = 0; 1695195945Sjamie continue; 1696195945Sjamie } 1697195945Sjamie#endif 1698192895Sjamie if (prison_restrict_ip6(tpr, ip6)) { 1699192895Sjamie if (ip6 != NULL) 1700192895Sjamie ip6 = NULL; 1701192895Sjamie else 1702192895Sjamie redo_ip6 = 1; 1703192895Sjamie } 1704192895Sjamie } 1705192895Sjamie mtx_unlock(&pr->pr_mtx); 1706192895Sjamie } 1707192895Sjamie#endif 1708192895Sjamie 1709191673Sjamie /* Let the modules do their work. */ 1710191673Sjamie sx_downgrade(&allprison_lock); 1711191673Sjamie if (created) { 1712191673Sjamie error = osd_jail_call(pr, PR_METHOD_CREATE, opts); 1713191673Sjamie if (error) { 1714191673Sjamie prison_deref(pr, PD_LIST_SLOCKED); 1715191673Sjamie goto done_errmsg; 1716191673Sjamie } 1717191673Sjamie } 1718191673Sjamie error = osd_jail_call(pr, PR_METHOD_SET, opts); 1719191673Sjamie if (error) { 1720191673Sjamie prison_deref(pr, created 1721191673Sjamie ? PD_LIST_SLOCKED 1722191673Sjamie : PD_DEREF | PD_LIST_SLOCKED); 1723191673Sjamie goto done_errmsg; 1724191673Sjamie } 1725191673Sjamie 1726191673Sjamie /* Attach this process to the prison if requested. */ 1727191673Sjamie if (flags & JAIL_ATTACH) { 1728191673Sjamie mtx_lock(&pr->pr_mtx); 1729191673Sjamie error = do_jail_attach(td, pr); 1730191673Sjamie if (error) { 1731191673Sjamie vfs_opterror(opts, "attach failed"); 1732191673Sjamie if (!created) 1733191673Sjamie prison_deref(pr, PD_DEREF); 1734191673Sjamie goto done_errmsg; 1735191673Sjamie } 1736191673Sjamie } 1737191673Sjamie 1738191673Sjamie /* 1739191673Sjamie * Now that it is all there, drop the temporary reference from existing 1740191673Sjamie * prisons. Or add a reference to newly created persistent prisons 1741191673Sjamie * (which was not done earlier so that the prison would not be publicly 1742191673Sjamie * visible). 1743191673Sjamie */ 1744191673Sjamie if (!created) { 1745191673Sjamie prison_deref(pr, (flags & JAIL_ATTACH) 1746191673Sjamie ? PD_DEREF 1747191673Sjamie : PD_DEREF | PD_LIST_SLOCKED); 1748191673Sjamie } else { 1749191673Sjamie if (pr_flags & PR_PERSIST) { 1750191673Sjamie mtx_lock(&pr->pr_mtx); 1751191673Sjamie pr->pr_ref++; 1752191673Sjamie pr->pr_uref++; 1753191673Sjamie mtx_unlock(&pr->pr_mtx); 1754191673Sjamie } 1755191673Sjamie if (!(flags & JAIL_ATTACH)) 1756191673Sjamie sx_sunlock(&allprison_lock); 1757191673Sjamie } 1758191673Sjamie td->td_retval[0] = pr->pr_id; 1759191673Sjamie goto done_errmsg; 1760191673Sjamie 1761192895Sjamie done_deref_locked: 1762192895Sjamie prison_deref(pr, created 1763192895Sjamie ? PD_LOCKED | PD_LIST_XLOCKED 1764192895Sjamie : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 1765192895Sjamie goto done_releroot; 1766191673Sjamie done_unlock_list: 1767191673Sjamie sx_xunlock(&allprison_lock); 1768191673Sjamie done_releroot: 1769191673Sjamie if (root != NULL) { 1770191673Sjamie vfslocked = VFS_LOCK_GIANT(root->v_mount); 1771191673Sjamie vrele(root); 1772191673Sjamie VFS_UNLOCK_GIANT(vfslocked); 1773191673Sjamie } 1774191673Sjamie done_errmsg: 1775191673Sjamie if (error) { 1776191673Sjamie vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 1777191673Sjamie if (errmsg_len > 0) { 1778191673Sjamie errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1; 1779191673Sjamie if (errmsg_pos > 0) { 1780191673Sjamie if (optuio->uio_segflg == UIO_SYSSPACE) 1781191673Sjamie bcopy(errmsg, 1782191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 1783191673Sjamie errmsg_len); 1784191673Sjamie else 1785191673Sjamie copyout(errmsg, 1786191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 1787191673Sjamie errmsg_len); 1788191673Sjamie } 1789191673Sjamie } 1790191673Sjamie } 1791191673Sjamie done_free: 1792191673Sjamie#ifdef INET 1793191673Sjamie free(ip4, M_PRISON); 1794191673Sjamie#endif 1795191673Sjamie#ifdef INET6 1796191673Sjamie free(ip6, M_PRISON); 1797191673Sjamie#endif 1798191673Sjamie vfs_freeopts(opts); 1799191673Sjamie return (error); 1800191673Sjamie} 1801191673Sjamie 1802191673Sjamie 180382710Sdillon/* 1804191673Sjamie * struct jail_get_args { 1805191673Sjamie * struct iovec *iovp; 1806191673Sjamie * unsigned int iovcnt; 1807191673Sjamie * int flags; 1808114168Smike * }; 180982710Sdillon */ 181046155Sphkint 1811191673Sjamiejail_get(struct thread *td, struct jail_get_args *uap) 181246155Sphk{ 1813191673Sjamie struct uio *auio; 1814185435Sbz int error; 1815185435Sbz 1816191673Sjamie /* Check that we have an even number of iovecs. */ 1817191673Sjamie if (uap->iovcnt & 1) 1818191673Sjamie return (EINVAL); 1819191673Sjamie 1820191673Sjamie error = copyinuio(uap->iovp, uap->iovcnt, &auio); 1821185435Sbz if (error) 1822185435Sbz return (error); 1823191673Sjamie error = kern_jail_get(td, auio, uap->flags); 1824191673Sjamie if (error == 0) 1825191673Sjamie error = copyout(auio->uio_iov, uap->iovp, 1826191673Sjamie uap->iovcnt * sizeof (struct iovec)); 1827191673Sjamie free(auio, M_IOV); 1828191673Sjamie return (error); 1829191673Sjamie} 1830185435Sbz 1831191673Sjamieint 1832191673Sjamiekern_jail_get(struct thread *td, struct uio *optuio, int flags) 1833191673Sjamie{ 1834192895Sjamie struct prison *pr, *mypr; 1835191673Sjamie struct vfsopt *opt; 1836191673Sjamie struct vfsoptlist *opts; 1837191673Sjamie char *errmsg, *name; 1838192895Sjamie int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos; 1839185435Sbz 1840191673Sjamie if (flags & ~JAIL_GET_MASK) 1841191673Sjamie return (EINVAL); 1842185435Sbz 1843191673Sjamie /* Get the parameter list. */ 1844191673Sjamie error = vfs_buildopts(optuio, &opts); 1845191673Sjamie if (error) 1846191673Sjamie return (error); 1847191673Sjamie errmsg_pos = vfs_getopt_pos(opts, "errmsg"); 1848192895Sjamie mypr = td->td_ucred->cr_prison; 1849185435Sbz 1850191673Sjamie /* 1851191673Sjamie * Find the prison specified by one of: lastjid, jid, name. 1852191673Sjamie */ 1853191673Sjamie sx_slock(&allprison_lock); 1854191673Sjamie error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid)); 1855191673Sjamie if (error == 0) { 1856191673Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) { 1857192895Sjamie if (pr->pr_id > jid && prison_ischild(mypr, pr)) { 1858191673Sjamie mtx_lock(&pr->pr_mtx); 1859191673Sjamie if (pr->pr_ref > 0 && 1860191673Sjamie (pr->pr_uref > 0 || (flags & JAIL_DYING))) 1861191673Sjamie break; 1862191673Sjamie mtx_unlock(&pr->pr_mtx); 1863191673Sjamie } 1864191673Sjamie } 1865191673Sjamie if (pr != NULL) 1866191673Sjamie goto found_prison; 1867191673Sjamie error = ENOENT; 1868191673Sjamie vfs_opterror(opts, "no jail after %d", jid); 1869191673Sjamie goto done_unlock_list; 1870191673Sjamie } else if (error != ENOENT) 1871191673Sjamie goto done_unlock_list; 1872185435Sbz 1873191673Sjamie error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 1874191673Sjamie if (error == 0) { 1875191673Sjamie if (jid != 0) { 1876192895Sjamie pr = prison_find_child(mypr, jid); 1877191673Sjamie if (pr != NULL) { 1878191673Sjamie if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 1879191673Sjamie mtx_unlock(&pr->pr_mtx); 1880191673Sjamie error = ENOENT; 1881191673Sjamie vfs_opterror(opts, "jail %d is dying", 1882191673Sjamie jid); 1883191673Sjamie goto done_unlock_list; 1884191673Sjamie } 1885191673Sjamie goto found_prison; 1886191673Sjamie } 1887191673Sjamie error = ENOENT; 1888191673Sjamie vfs_opterror(opts, "jail %d not found", jid); 1889191673Sjamie goto done_unlock_list; 1890191673Sjamie } 1891191673Sjamie } else if (error != ENOENT) 1892191673Sjamie goto done_unlock_list; 189346155Sphk 1894191673Sjamie error = vfs_getopt(opts, "name", (void **)&name, &len); 1895191673Sjamie if (error == 0) { 1896191673Sjamie if (len == 0 || name[len - 1] != '\0') { 1897191673Sjamie error = EINVAL; 1898191673Sjamie goto done_unlock_list; 1899191673Sjamie } 1900192895Sjamie pr = prison_find_name(mypr, name); 1901191673Sjamie if (pr != NULL) { 1902191673Sjamie if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 1903191673Sjamie mtx_unlock(&pr->pr_mtx); 1904191673Sjamie error = ENOENT; 1905191673Sjamie vfs_opterror(opts, "jail \"%s\" is dying", 1906191673Sjamie name); 1907191673Sjamie goto done_unlock_list; 1908191673Sjamie } 1909191673Sjamie goto found_prison; 1910191673Sjamie } 1911191673Sjamie error = ENOENT; 1912191673Sjamie vfs_opterror(opts, "jail \"%s\" not found", name); 1913191673Sjamie goto done_unlock_list; 1914191673Sjamie } else if (error != ENOENT) 1915191673Sjamie goto done_unlock_list; 1916185435Sbz 1917191673Sjamie vfs_opterror(opts, "no jail specified"); 1918191673Sjamie error = ENOENT; 1919191673Sjamie goto done_unlock_list; 1920191673Sjamie 1921191673Sjamie found_prison: 1922191673Sjamie /* Get the parameters of the prison. */ 1923191673Sjamie pr->pr_ref++; 1924191673Sjamie locked = PD_LOCKED; 1925191673Sjamie td->td_retval[0] = pr->pr_id; 1926191673Sjamie error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id)); 1927191673Sjamie if (error != 0 && error != ENOENT) 1928191673Sjamie goto done_deref; 1929192895Sjamie i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id; 1930192895Sjamie error = vfs_setopt(opts, "parent", &i, sizeof(i)); 1931191673Sjamie if (error != 0 && error != ENOENT) 1932191673Sjamie goto done_deref; 1933192895Sjamie error = vfs_setopts(opts, "name", prison_name(mypr, pr)); 1934192895Sjamie if (error != 0 && error != ENOENT) 1935192895Sjamie goto done_deref; 1936192895Sjamie error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id, 1937191673Sjamie sizeof(pr->pr_cpuset->cs_id)); 1938191673Sjamie if (error != 0 && error != ENOENT) 1939191673Sjamie goto done_deref; 1940192895Sjamie error = vfs_setopts(opts, "path", prison_path(mypr, pr)); 1941191673Sjamie if (error != 0 && error != ENOENT) 1942191673Sjamie goto done_deref; 1943191673Sjamie#ifdef INET 1944191673Sjamie error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4, 1945191673Sjamie pr->pr_ip4s * sizeof(*pr->pr_ip4)); 1946191673Sjamie if (error != 0 && error != ENOENT) 1947191673Sjamie goto done_deref; 1948191673Sjamie#endif 1949191673Sjamie#ifdef INET6 1950191673Sjamie error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6, 1951191673Sjamie pr->pr_ip6s * sizeof(*pr->pr_ip6)); 1952191673Sjamie if (error != 0 && error != ENOENT) 1953191673Sjamie goto done_deref; 1954191673Sjamie#endif 1955191673Sjamie error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel, 1956191673Sjamie sizeof(pr->pr_securelevel)); 1957191673Sjamie if (error != 0 && error != ENOENT) 1958191673Sjamie goto done_deref; 1959194762Sjamie error = vfs_setopt(opts, "children.cur", &pr->pr_childcount, 1960194762Sjamie sizeof(pr->pr_childcount)); 1961194762Sjamie if (error != 0 && error != ENOENT) 1962194762Sjamie goto done_deref; 1963194762Sjamie error = vfs_setopt(opts, "children.max", &pr->pr_childmax, 1964194762Sjamie sizeof(pr->pr_childmax)); 1965194762Sjamie if (error != 0 && error != ENOENT) 1966194762Sjamie goto done_deref; 1967194118Sjamie error = vfs_setopts(opts, "host.hostname", pr->pr_hostname); 1968191673Sjamie if (error != 0 && error != ENOENT) 1969191673Sjamie goto done_deref; 1970194118Sjamie error = vfs_setopts(opts, "host.domainname", pr->pr_domainname); 1971193066Sjamie if (error != 0 && error != ENOENT) 1972193066Sjamie goto done_deref; 1973194118Sjamie error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid); 1974193066Sjamie if (error != 0 && error != ENOENT) 1975193066Sjamie goto done_deref; 1976205014Snwhitehorn#ifdef COMPAT_FREEBSD32 1977217896Sdchagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 1978193066Sjamie uint32_t hid32 = pr->pr_hostid; 1979193066Sjamie 1980193066Sjamie error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32)); 1981193066Sjamie } else 1982193066Sjamie#endif 1983193066Sjamie error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid, 1984193066Sjamie sizeof(pr->pr_hostid)); 1985193066Sjamie if (error != 0 && error != ENOENT) 1986193066Sjamie goto done_deref; 1987192895Sjamie error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs, 1988192895Sjamie sizeof(pr->pr_enforce_statfs)); 1989191673Sjamie if (error != 0 && error != ENOENT) 1990191673Sjamie goto done_deref; 1991192895Sjamie for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 1992192895Sjamie fi++) { 1993192895Sjamie if (pr_flag_names[fi] == NULL) 1994192895Sjamie continue; 1995192895Sjamie i = (pr->pr_flags & (1 << fi)) ? 1 : 0; 1996192895Sjamie error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i)); 1997192895Sjamie if (error != 0 && error != ENOENT) 1998192895Sjamie goto done_deref; 1999192895Sjamie i = !i; 2000192895Sjamie error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i)); 2001192895Sjamie if (error != 0 && error != ENOENT) 2002192895Sjamie goto done_deref; 2003192895Sjamie } 2004195870Sjamie for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 2005195870Sjamie fi++) { 2006195870Sjamie i = pr->pr_flags & 2007195870Sjamie (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new); 2008195870Sjamie i = pr_flag_jailsys[fi].disable && 2009195870Sjamie (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE 2010195870Sjamie : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW 2011195870Sjamie : JAIL_SYS_INHERIT; 2012195870Sjamie error = 2013195870Sjamie vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i)); 2014195870Sjamie if (error != 0 && error != ENOENT) 2015195870Sjamie goto done_deref; 2016195870Sjamie } 2017192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 2018192895Sjamie fi++) { 2019192895Sjamie if (pr_allow_names[fi] == NULL) 2020192895Sjamie continue; 2021192895Sjamie i = (pr->pr_allow & (1 << fi)) ? 1 : 0; 2022192895Sjamie error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i)); 2023192895Sjamie if (error != 0 && error != ENOENT) 2024192895Sjamie goto done_deref; 2025192895Sjamie i = !i; 2026192895Sjamie error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i)); 2027192895Sjamie if (error != 0 && error != ENOENT) 2028192895Sjamie goto done_deref; 2029192895Sjamie } 2030191673Sjamie i = (pr->pr_uref == 0); 2031191673Sjamie error = vfs_setopt(opts, "dying", &i, sizeof(i)); 2032191673Sjamie if (error != 0 && error != ENOENT) 2033191673Sjamie goto done_deref; 2034191673Sjamie i = !i; 2035191673Sjamie error = vfs_setopt(opts, "nodying", &i, sizeof(i)); 2036191673Sjamie if (error != 0 && error != ENOENT) 2037191673Sjamie goto done_deref; 2038191673Sjamie 2039191673Sjamie /* Get the module parameters. */ 2040191673Sjamie mtx_unlock(&pr->pr_mtx); 2041191673Sjamie locked = 0; 2042191673Sjamie error = osd_jail_call(pr, PR_METHOD_GET, opts); 204346155Sphk if (error) 2044191673Sjamie goto done_deref; 2045191673Sjamie prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED); 204684828Sjhb 2047191673Sjamie /* By now, all parameters should have been noted. */ 2048191673Sjamie TAILQ_FOREACH(opt, opts, link) { 2049191673Sjamie if (!opt->seen && strcmp(opt->name, "errmsg")) { 2050191673Sjamie error = EINVAL; 2051191673Sjamie vfs_opterror(opts, "unknown parameter: %s", opt->name); 2052191673Sjamie goto done_errmsg; 2053191673Sjamie } 2054185435Sbz } 2055191673Sjamie 2056191673Sjamie /* Write the fetched parameters back to userspace. */ 2057191673Sjamie error = 0; 2058191673Sjamie TAILQ_FOREACH(opt, opts, link) { 2059191673Sjamie if (opt->pos >= 0 && opt->pos != errmsg_pos) { 2060191673Sjamie pos = 2 * opt->pos + 1; 2061191673Sjamie optuio->uio_iov[pos].iov_len = opt->len; 2062191673Sjamie if (opt->value != NULL) { 2063191673Sjamie if (optuio->uio_segflg == UIO_SYSSPACE) { 2064191673Sjamie bcopy(opt->value, 2065191673Sjamie optuio->uio_iov[pos].iov_base, 2066191673Sjamie opt->len); 2067191673Sjamie } else { 2068191673Sjamie error = copyout(opt->value, 2069191673Sjamie optuio->uio_iov[pos].iov_base, 2070191673Sjamie opt->len); 2071191673Sjamie if (error) 2072191673Sjamie break; 2073191673Sjamie } 2074191673Sjamie } 2075191673Sjamie } 2076185435Sbz } 2077191673Sjamie goto done_errmsg; 2078191673Sjamie 2079191673Sjamie done_deref: 2080191673Sjamie prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED); 2081191673Sjamie goto done_errmsg; 2082191673Sjamie 2083191673Sjamie done_unlock_list: 2084191673Sjamie sx_sunlock(&allprison_lock); 2085191673Sjamie done_errmsg: 2086191673Sjamie if (error && errmsg_pos >= 0) { 2087191673Sjamie vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 2088191673Sjamie errmsg_pos = 2 * errmsg_pos + 1; 2089191673Sjamie if (errmsg_len > 0) { 2090191673Sjamie if (optuio->uio_segflg == UIO_SYSSPACE) 2091191673Sjamie bcopy(errmsg, 2092191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 2093191673Sjamie errmsg_len); 2094191673Sjamie else 2095191673Sjamie copyout(errmsg, 2096191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 2097191673Sjamie errmsg_len); 2098191673Sjamie } 2099185435Sbz } 2100191673Sjamie vfs_freeopts(opts); 2101191673Sjamie return (error); 2102191673Sjamie} 2103113275Smike 2104192895Sjamie 2105191673Sjamie/* 2106191673Sjamie * struct jail_remove_args { 2107191673Sjamie * int jid; 2108191673Sjamie * }; 2109191673Sjamie */ 2110191673Sjamieint 2111191673Sjamiejail_remove(struct thread *td, struct jail_remove_args *uap) 2112191673Sjamie{ 2113192895Sjamie struct prison *pr, *cpr, *lpr, *tpr; 2114192895Sjamie int descend, error; 2115185435Sbz 2116191673Sjamie error = priv_check(td, PRIV_JAIL_REMOVE); 2117185435Sbz if (error) 2118191673Sjamie return (error); 2119185435Sbz 2120185435Sbz sx_xlock(&allprison_lock); 2121192895Sjamie pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2122191673Sjamie if (pr == NULL) { 2123185435Sbz sx_xunlock(&allprison_lock); 2124191673Sjamie return (EINVAL); 2125185435Sbz } 2126185435Sbz 2127192895Sjamie /* Remove all descendants of this prison, then remove this prison. */ 2128192895Sjamie pr->pr_ref++; 2129192895Sjamie pr->pr_flags |= PR_REMOVE; 2130192895Sjamie if (!LIST_EMPTY(&pr->pr_children)) { 2131192895Sjamie mtx_unlock(&pr->pr_mtx); 2132192895Sjamie lpr = NULL; 2133192895Sjamie FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 2134192895Sjamie mtx_lock(&cpr->pr_mtx); 2135192895Sjamie if (cpr->pr_ref > 0) { 2136192895Sjamie tpr = cpr; 2137192895Sjamie cpr->pr_ref++; 2138192895Sjamie cpr->pr_flags |= PR_REMOVE; 2139192895Sjamie } else { 2140192895Sjamie /* Already removed - do not do it again. */ 2141192895Sjamie tpr = NULL; 2142192895Sjamie } 2143192895Sjamie mtx_unlock(&cpr->pr_mtx); 2144192895Sjamie if (lpr != NULL) { 2145192895Sjamie mtx_lock(&lpr->pr_mtx); 2146192895Sjamie prison_remove_one(lpr); 2147192895Sjamie sx_xlock(&allprison_lock); 2148192895Sjamie } 2149192895Sjamie lpr = tpr; 2150192895Sjamie } 2151192895Sjamie if (lpr != NULL) { 2152192895Sjamie mtx_lock(&lpr->pr_mtx); 2153192895Sjamie prison_remove_one(lpr); 2154192895Sjamie sx_xlock(&allprison_lock); 2155192895Sjamie } 2156192895Sjamie mtx_lock(&pr->pr_mtx); 2157192895Sjamie } 2158192895Sjamie prison_remove_one(pr); 2159192895Sjamie return (0); 2160192895Sjamie} 2161192895Sjamie 2162192895Sjamiestatic void 2163192895Sjamieprison_remove_one(struct prison *pr) 2164192895Sjamie{ 2165192895Sjamie struct proc *p; 2166192895Sjamie int deuref; 2167192895Sjamie 2168191673Sjamie /* If the prison was persistent, it is not anymore. */ 2169191673Sjamie deuref = 0; 2170191673Sjamie if (pr->pr_flags & PR_PERSIST) { 2171191673Sjamie pr->pr_ref--; 2172191673Sjamie deuref = PD_DEUREF; 2173191673Sjamie pr->pr_flags &= ~PR_PERSIST; 2174179881Sdelphij } 2175113275Smike 2176192895Sjamie /* 2177192895Sjamie * jail_remove added a reference. If that's the only one, remove 2178192895Sjamie * the prison now. 2179192895Sjamie */ 2180192895Sjamie KASSERT(pr->pr_ref > 0, 2181192895Sjamie ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id)); 2182192895Sjamie if (pr->pr_ref == 1) { 2183191673Sjamie prison_deref(pr, 2184191673Sjamie deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 2185192895Sjamie return; 2186191673Sjamie } 2187191673Sjamie 2188113275Smike mtx_unlock(&pr->pr_mtx); 2189191673Sjamie sx_xunlock(&allprison_lock); 2190191673Sjamie /* 2191191673Sjamie * Kill all processes unfortunate enough to be attached to this prison. 2192191673Sjamie */ 2193191673Sjamie sx_slock(&allproc_lock); 2194191673Sjamie LIST_FOREACH(p, &allproc, p_list) { 2195191673Sjamie PROC_LOCK(p); 2196191673Sjamie if (p->p_state != PRS_NEW && p->p_ucred && 2197191673Sjamie p->p_ucred->cr_prison == pr) 2198191673Sjamie psignal(p, SIGKILL); 2199191673Sjamie PROC_UNLOCK(p); 2200191673Sjamie } 2201191673Sjamie sx_sunlock(&allproc_lock); 2202192895Sjamie /* Remove the temporary reference added by jail_remove. */ 2203191673Sjamie prison_deref(pr, deuref | PD_DEREF); 2204113275Smike} 2205113275Smike 2206190466Sjamie 2207113275Smike/* 2208114168Smike * struct jail_attach_args { 2209114168Smike * int jid; 2210114168Smike * }; 2211113275Smike */ 2212113275Smikeint 2213114168Smikejail_attach(struct thread *td, struct jail_attach_args *uap) 2214113275Smike{ 2215113275Smike struct prison *pr; 2216191673Sjamie int error; 2217167309Spjd 2218164032Srwatson error = priv_check(td, PRIV_JAIL_ATTACH); 2219126023Snectar if (error) 2220126023Snectar return (error); 2221126023Snectar 2222168401Spjd sx_slock(&allprison_lock); 2223192895Sjamie pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2224113275Smike if (pr == NULL) { 2225168401Spjd sx_sunlock(&allprison_lock); 2226113275Smike return (EINVAL); 2227113275Smike } 2228185435Sbz 2229185435Sbz /* 2230185435Sbz * Do not allow a process to attach to a prison that is not 2231191673Sjamie * considered to be "alive". 2232185435Sbz */ 2233191673Sjamie if (pr->pr_uref == 0) { 2234185435Sbz mtx_unlock(&pr->pr_mtx); 2235185435Sbz sx_sunlock(&allprison_lock); 2236185435Sbz return (EINVAL); 2237185435Sbz } 2238191673Sjamie 2239191673Sjamie return (do_jail_attach(td, pr)); 2240191673Sjamie} 2241191673Sjamie 2242191673Sjamiestatic int 2243191673Sjamiedo_jail_attach(struct thread *td, struct prison *pr) 2244191673Sjamie{ 2245192895Sjamie struct prison *ppr; 2246191673Sjamie struct proc *p; 2247191673Sjamie struct ucred *newcred, *oldcred; 2248191673Sjamie int vfslocked, error; 2249191673Sjamie 2250191673Sjamie /* 2251191673Sjamie * XXX: Note that there is a slight race here if two threads 2252191673Sjamie * in the same privileged process attempt to attach to two 2253191673Sjamie * different jails at the same time. It is important for 2254191673Sjamie * user processes not to do this, or they might end up with 2255191673Sjamie * a process root from one prison, but attached to the jail 2256191673Sjamie * of another. 2257191673Sjamie */ 2258113275Smike pr->pr_ref++; 2259191673Sjamie pr->pr_uref++; 2260113275Smike mtx_unlock(&pr->pr_mtx); 2261191673Sjamie 2262191673Sjamie /* Let modules do whatever they need to prepare for attaching. */ 2263191673Sjamie error = osd_jail_call(pr, PR_METHOD_ATTACH, td); 2264191673Sjamie if (error) { 2265191673Sjamie prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED); 2266191673Sjamie return (error); 2267191673Sjamie } 2268168401Spjd sx_sunlock(&allprison_lock); 2269113275Smike 2270185435Sbz /* 2271185435Sbz * Reparent the newly attached process to this jail. 2272185435Sbz */ 2273192895Sjamie ppr = td->td_ucred->cr_prison; 2274191673Sjamie p = td->td_proc; 2275185435Sbz error = cpuset_setproc_update_set(p, pr->pr_cpuset); 2276185435Sbz if (error) 2277191673Sjamie goto e_revert_osd; 2278185435Sbz 2279150652Scsjp vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount); 2280175202Sattilio vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY); 2281113275Smike if ((error = change_dir(pr->pr_root, td)) != 0) 2282113275Smike goto e_unlock; 2283113275Smike#ifdef MAC 2284172930Srwatson if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root))) 2285113275Smike goto e_unlock; 2286113275Smike#endif 2287175294Sattilio VOP_UNLOCK(pr->pr_root, 0); 2288191673Sjamie if ((error = change_root(pr->pr_root, td))) 2289191673Sjamie goto e_unlock_giant; 2290150652Scsjp VFS_UNLOCK_GIANT(vfslocked); 2291113275Smike 229284828Sjhb newcred = crget(); 229384828Sjhb PROC_LOCK(p); 229484828Sjhb oldcred = p->p_ucred; 2295113275Smike setsugid(p); 229684828Sjhb crcopy(newcred, oldcred); 2297113630Sjhb newcred->cr_prison = pr; 229884828Sjhb p->p_ucred = newcred; 229984828Sjhb PROC_UNLOCK(p); 2300220137Strasz#ifdef RACCT 2301220137Strasz racct_proc_ucred_changed(p, oldcred, newcred); 2302220137Strasz#endif 230384828Sjhb crfree(oldcred); 2304192895Sjamie prison_deref(ppr, PD_DEREF | PD_DEUREF); 230546155Sphk return (0); 2306191673Sjamie e_unlock: 2307175294Sattilio VOP_UNLOCK(pr->pr_root, 0); 2308191673Sjamie e_unlock_giant: 2309150652Scsjp VFS_UNLOCK_GIANT(vfslocked); 2310191673Sjamie e_revert_osd: 2311191673Sjamie /* Tell modules this thread is still in its old jail after all. */ 2312192895Sjamie (void)osd_jail_call(ppr, PR_METHOD_ATTACH, td); 2313191673Sjamie prison_deref(pr, PD_DEREF | PD_DEUREF); 231446155Sphk return (error); 231546155Sphk} 231646155Sphk 2317192895Sjamie 2318113275Smike/* 2319113275Smike * Returns a locked prison instance, or NULL on failure. 2320113275Smike */ 2321168399Spjdstruct prison * 2322113275Smikeprison_find(int prid) 2323113275Smike{ 2324113275Smike struct prison *pr; 2325113275Smike 2326168401Spjd sx_assert(&allprison_lock, SX_LOCKED); 2327191673Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) { 2328113275Smike if (pr->pr_id == prid) { 2329113275Smike mtx_lock(&pr->pr_mtx); 2330191673Sjamie if (pr->pr_ref > 0) 2331191673Sjamie return (pr); 2332191673Sjamie mtx_unlock(&pr->pr_mtx); 2333113275Smike } 2334113275Smike } 2335113275Smike return (NULL); 2336113275Smike} 2337113275Smike 2338191673Sjamie/* 2339192895Sjamie * Find a prison that is a descendant of mypr. Returns a locked prison or NULL. 2340191673Sjamie */ 2341191673Sjamiestruct prison * 2342192895Sjamieprison_find_child(struct prison *mypr, int prid) 2343191673Sjamie{ 2344192895Sjamie struct prison *pr; 2345192895Sjamie int descend; 2346192895Sjamie 2347192895Sjamie sx_assert(&allprison_lock, SX_LOCKED); 2348192895Sjamie FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 2349192895Sjamie if (pr->pr_id == prid) { 2350192895Sjamie mtx_lock(&pr->pr_mtx); 2351192895Sjamie if (pr->pr_ref > 0) 2352192895Sjamie return (pr); 2353192895Sjamie mtx_unlock(&pr->pr_mtx); 2354192895Sjamie } 2355192895Sjamie } 2356192895Sjamie return (NULL); 2357192895Sjamie} 2358192895Sjamie 2359192895Sjamie/* 2360192895Sjamie * Look for the name relative to mypr. Returns a locked prison or NULL. 2361192895Sjamie */ 2362192895Sjamiestruct prison * 2363192895Sjamieprison_find_name(struct prison *mypr, const char *name) 2364192895Sjamie{ 2365191673Sjamie struct prison *pr, *deadpr; 2366192895Sjamie size_t mylen; 2367192895Sjamie int descend; 2368191673Sjamie 2369191673Sjamie sx_assert(&allprison_lock, SX_LOCKED); 2370192895Sjamie mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1; 2371191673Sjamie again: 2372191673Sjamie deadpr = NULL; 2373192895Sjamie FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 2374192895Sjamie if (!strcmp(pr->pr_name + mylen, name)) { 2375191673Sjamie mtx_lock(&pr->pr_mtx); 2376191673Sjamie if (pr->pr_ref > 0) { 2377191673Sjamie if (pr->pr_uref > 0) 2378191673Sjamie return (pr); 2379191673Sjamie deadpr = pr; 2380191673Sjamie } 2381191673Sjamie mtx_unlock(&pr->pr_mtx); 2382191673Sjamie } 2383191673Sjamie } 2384192895Sjamie /* There was no valid prison - perhaps there was a dying one. */ 2385191673Sjamie if (deadpr != NULL) { 2386191673Sjamie mtx_lock(&deadpr->pr_mtx); 2387191673Sjamie if (deadpr->pr_ref == 0) { 2388191673Sjamie mtx_unlock(&deadpr->pr_mtx); 2389191673Sjamie goto again; 2390191673Sjamie } 2391191673Sjamie } 2392191673Sjamie return (deadpr); 2393191673Sjamie} 2394191673Sjamie 2395191673Sjamie/* 2396192895Sjamie * See if a prison has the specific flag set. 2397192895Sjamie */ 2398192895Sjamieint 2399192895Sjamieprison_flag(struct ucred *cred, unsigned flag) 2400192895Sjamie{ 2401192895Sjamie 2402192895Sjamie /* This is an atomic read, so no locking is necessary. */ 2403192895Sjamie return (cred->cr_prison->pr_flags & flag); 2404192895Sjamie} 2405192895Sjamie 2406192895Sjamieint 2407192895Sjamieprison_allow(struct ucred *cred, unsigned flag) 2408192895Sjamie{ 2409192895Sjamie 2410192895Sjamie /* This is an atomic read, so no locking is necessary. */ 2411192895Sjamie return (cred->cr_prison->pr_allow & flag); 2412192895Sjamie} 2413192895Sjamie 2414192895Sjamie/* 2415191673Sjamie * Remove a prison reference. If that was the last reference, remove the 2416191673Sjamie * prison itself - but not in this context in case there are locks held. 2417191673Sjamie */ 241872786Srwatsonvoid 2419185029Spjdprison_free_locked(struct prison *pr) 242072786Srwatson{ 242172786Srwatson 2422185029Spjd mtx_assert(&pr->pr_mtx, MA_OWNED); 242372786Srwatson pr->pr_ref--; 242472786Srwatson if (pr->pr_ref == 0) { 2425168483Spjd mtx_unlock(&pr->pr_mtx); 2426124882Srwatson TASK_INIT(&pr->pr_task, 0, prison_complete, pr); 2427144660Sjeff taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 242887275Srwatson return; 242972786Srwatson } 243087275Srwatson mtx_unlock(&pr->pr_mtx); 243172786Srwatson} 243272786Srwatson 2433185029Spjdvoid 2434185029Spjdprison_free(struct prison *pr) 2435185029Spjd{ 2436185029Spjd 2437185029Spjd mtx_lock(&pr->pr_mtx); 2438185029Spjd prison_free_locked(pr); 2439185029Spjd} 2440185029Spjd 2441124882Srwatsonstatic void 2442124882Srwatsonprison_complete(void *context, int pending) 2443124882Srwatson{ 2444191673Sjamie 2445191673Sjamie prison_deref((struct prison *)context, 0); 2446191673Sjamie} 2447191673Sjamie 2448191673Sjamie/* 2449191673Sjamie * Remove a prison reference (usually). This internal version assumes no 2450191673Sjamie * mutexes are held, except perhaps the prison itself. If there are no more 2451191673Sjamie * references, release and delist the prison. On completion, the prison lock 2452191673Sjamie * and the allprison lock are both unlocked. 2453191673Sjamie */ 2454191673Sjamiestatic void 2455191673Sjamieprison_deref(struct prison *pr, int flags) 2456191673Sjamie{ 2457192895Sjamie struct prison *ppr, *tpr; 2458150652Scsjp int vfslocked; 2459124882Srwatson 2460191673Sjamie if (!(flags & PD_LOCKED)) 2461191673Sjamie mtx_lock(&pr->pr_mtx); 2462192895Sjamie /* Decrement the user references in a separate loop. */ 2463191673Sjamie if (flags & PD_DEUREF) { 2464192895Sjamie for (tpr = pr;; tpr = tpr->pr_parent) { 2465192895Sjamie if (tpr != pr) 2466192895Sjamie mtx_lock(&tpr->pr_mtx); 2467192895Sjamie if (--tpr->pr_uref > 0) 2468192895Sjamie break; 2469192895Sjamie KASSERT(tpr != &prison0, ("prison0 pr_uref=0")); 2470192895Sjamie mtx_unlock(&tpr->pr_mtx); 2471192895Sjamie } 2472191673Sjamie /* Done if there were only user references to remove. */ 2473191673Sjamie if (!(flags & PD_DEREF)) { 2474192895Sjamie mtx_unlock(&tpr->pr_mtx); 2475191673Sjamie if (flags & PD_LIST_SLOCKED) 2476191673Sjamie sx_sunlock(&allprison_lock); 2477191673Sjamie else if (flags & PD_LIST_XLOCKED) 2478191673Sjamie sx_xunlock(&allprison_lock); 2479191673Sjamie return; 2480191673Sjamie } 2481192895Sjamie if (tpr != pr) { 2482192895Sjamie mtx_unlock(&tpr->pr_mtx); 2483192895Sjamie mtx_lock(&pr->pr_mtx); 2484192895Sjamie } 2485191673Sjamie } 2486124882Srwatson 2487192895Sjamie for (;;) { 2488192895Sjamie if (flags & PD_DEREF) 2489192895Sjamie pr->pr_ref--; 2490192895Sjamie /* If the prison still has references, nothing else to do. */ 2491192895Sjamie if (pr->pr_ref > 0) { 2492192895Sjamie mtx_unlock(&pr->pr_mtx); 2493192895Sjamie if (flags & PD_LIST_SLOCKED) 2494192895Sjamie sx_sunlock(&allprison_lock); 2495192895Sjamie else if (flags & PD_LIST_XLOCKED) 2496192895Sjamie sx_xunlock(&allprison_lock); 2497192895Sjamie return; 2498191673Sjamie } 2499191673Sjamie 2500192895Sjamie mtx_unlock(&pr->pr_mtx); 2501192895Sjamie if (flags & PD_LIST_SLOCKED) { 2502192895Sjamie if (!sx_try_upgrade(&allprison_lock)) { 2503192895Sjamie sx_sunlock(&allprison_lock); 2504192895Sjamie sx_xlock(&allprison_lock); 2505192895Sjamie } 2506192895Sjamie } else if (!(flags & PD_LIST_XLOCKED)) 2507192895Sjamie sx_xlock(&allprison_lock); 2508168489Spjd 2509192895Sjamie TAILQ_REMOVE(&allprison, pr, pr_list); 2510192895Sjamie LIST_REMOVE(pr, pr_sibling); 2511192895Sjamie ppr = pr->pr_parent; 2512192895Sjamie for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 2513194762Sjamie tpr->pr_childcount--; 2514196592Sjamie sx_xunlock(&allprison_lock); 2515192895Sjamie 2516194251Sjamie#ifdef VIMAGE 2517196505Szec if (pr->pr_vnet != ppr->pr_vnet) 2518194251Sjamie vnet_destroy(pr->pr_vnet); 2519194251Sjamie#endif 2520192895Sjamie if (pr->pr_root != NULL) { 2521192895Sjamie vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount); 2522192895Sjamie vrele(pr->pr_root); 2523192895Sjamie VFS_UNLOCK_GIANT(vfslocked); 2524192895Sjamie } 2525192895Sjamie mtx_destroy(&pr->pr_mtx); 2526191673Sjamie#ifdef INET 2527192895Sjamie free(pr->pr_ip4, M_PRISON); 2528191673Sjamie#endif 2529185435Sbz#ifdef INET6 2530192895Sjamie free(pr->pr_ip6, M_PRISON); 2531185435Sbz#endif 2532192895Sjamie if (pr->pr_cpuset != NULL) 2533192895Sjamie cpuset_rel(pr->pr_cpuset); 2534192895Sjamie osd_jail_exit(pr); 2535220137Strasz racct_destroy(&pr->pr_racct); 2536192895Sjamie free(pr, M_PRISON); 2537192895Sjamie 2538192895Sjamie /* Removing a prison frees a reference on its parent. */ 2539192895Sjamie pr = ppr; 2540192895Sjamie mtx_lock(&pr->pr_mtx); 2541196592Sjamie flags = PD_DEREF; 2542192895Sjamie } 2543124882Srwatson} 2544124882Srwatson 254572786Srwatsonvoid 2546185029Spjdprison_hold_locked(struct prison *pr) 254772786Srwatson{ 254872786Srwatson 2549185029Spjd mtx_assert(&pr->pr_mtx, MA_OWNED); 2550168489Spjd KASSERT(pr->pr_ref > 0, 2551191671Sjamie ("Trying to hold dead prison (jid=%d).", pr->pr_id)); 255272786Srwatson pr->pr_ref++; 2553185029Spjd} 2554185029Spjd 2555185029Spjdvoid 2556185029Spjdprison_hold(struct prison *pr) 2557185029Spjd{ 2558185029Spjd 2559185029Spjd mtx_lock(&pr->pr_mtx); 2560185029Spjd prison_hold_locked(pr); 256187275Srwatson mtx_unlock(&pr->pr_mtx); 256272786Srwatson} 256372786Srwatson 2564185435Sbzvoid 2565185435Sbzprison_proc_hold(struct prison *pr) 256687275Srwatson{ 256787275Srwatson 2568185435Sbz mtx_lock(&pr->pr_mtx); 2569191673Sjamie KASSERT(pr->pr_uref > 0, 2570191673Sjamie ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id)); 2571191673Sjamie pr->pr_uref++; 2572185435Sbz mtx_unlock(&pr->pr_mtx); 257387275Srwatson} 257487275Srwatson 2575185435Sbzvoid 2576185435Sbzprison_proc_free(struct prison *pr) 2577185435Sbz{ 2578185435Sbz 2579185435Sbz mtx_lock(&pr->pr_mtx); 2580191673Sjamie KASSERT(pr->pr_uref > 0, 2581191673Sjamie ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id)); 2582191673Sjamie prison_deref(pr, PD_DEUREF | PD_LOCKED); 2583185435Sbz} 2584185435Sbz 2585185435Sbz 2586185435Sbz#ifdef INET 2587185435Sbz/* 2588192895Sjamie * Restrict a prison's IP address list with its parent's, possibly replacing 2589192895Sjamie * it. Return true if the replacement buffer was used (or would have been). 2590192895Sjamie */ 2591192895Sjamiestatic int 2592192895Sjamieprison_restrict_ip4(struct prison *pr, struct in_addr *newip4) 2593192895Sjamie{ 2594192895Sjamie int ii, ij, used; 2595192895Sjamie struct prison *ppr; 2596192895Sjamie 2597192895Sjamie ppr = pr->pr_parent; 2598192895Sjamie if (!(pr->pr_flags & PR_IP4_USER)) { 2599192895Sjamie /* This has no user settings, so just copy the parent's list. */ 2600192895Sjamie if (pr->pr_ip4s < ppr->pr_ip4s) { 2601192895Sjamie /* 2602192895Sjamie * There's no room for the parent's list. Use the 2603192895Sjamie * new list buffer, which is assumed to be big enough 2604192895Sjamie * (if it was passed). If there's no buffer, try to 2605192895Sjamie * allocate one. 2606192895Sjamie */ 2607192895Sjamie used = 1; 2608192895Sjamie if (newip4 == NULL) { 2609192895Sjamie newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4), 2610192895Sjamie M_PRISON, M_NOWAIT); 2611192895Sjamie if (newip4 != NULL) 2612192895Sjamie used = 0; 2613192895Sjamie } 2614192895Sjamie if (newip4 != NULL) { 2615192895Sjamie bcopy(ppr->pr_ip4, newip4, 2616192895Sjamie ppr->pr_ip4s * sizeof(*newip4)); 2617192895Sjamie free(pr->pr_ip4, M_PRISON); 2618192895Sjamie pr->pr_ip4 = newip4; 2619192895Sjamie pr->pr_ip4s = ppr->pr_ip4s; 2620192895Sjamie } 2621192895Sjamie return (used); 2622192895Sjamie } 2623192895Sjamie pr->pr_ip4s = ppr->pr_ip4s; 2624192895Sjamie if (pr->pr_ip4s > 0) 2625192895Sjamie bcopy(ppr->pr_ip4, pr->pr_ip4, 2626192895Sjamie pr->pr_ip4s * sizeof(*newip4)); 2627192895Sjamie else if (pr->pr_ip4 != NULL) { 2628192895Sjamie free(pr->pr_ip4, M_PRISON); 2629192895Sjamie pr->pr_ip4 = NULL; 2630192895Sjamie } 2631195974Sjamie } else if (pr->pr_ip4s > 0) { 2632192895Sjamie /* Remove addresses that aren't in the parent. */ 2633192895Sjamie for (ij = 0; ij < ppr->pr_ip4s; ij++) 2634192895Sjamie if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 2635192895Sjamie break; 2636192895Sjamie if (ij < ppr->pr_ip4s) 2637192895Sjamie ii = 1; 2638192895Sjamie else { 2639192895Sjamie bcopy(pr->pr_ip4 + 1, pr->pr_ip4, 2640192895Sjamie --pr->pr_ip4s * sizeof(*pr->pr_ip4)); 2641192895Sjamie ii = 0; 2642192895Sjamie } 2643192895Sjamie for (ij = 1; ii < pr->pr_ip4s; ) { 2644192895Sjamie if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) { 2645192895Sjamie ii++; 2646192895Sjamie continue; 2647192895Sjamie } 2648192895Sjamie switch (ij >= ppr->pr_ip4s ? -1 : 2649192895Sjamie qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) { 2650192895Sjamie case -1: 2651192895Sjamie bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii, 2652192895Sjamie (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4)); 2653192895Sjamie break; 2654192895Sjamie case 0: 2655192895Sjamie ii++; 2656192895Sjamie ij++; 2657192895Sjamie break; 2658192895Sjamie case 1: 2659192895Sjamie ij++; 2660192895Sjamie break; 2661192895Sjamie } 2662192895Sjamie } 2663192895Sjamie if (pr->pr_ip4s == 0) { 2664195870Sjamie pr->pr_flags |= PR_IP4_DISABLE; 2665192895Sjamie free(pr->pr_ip4, M_PRISON); 2666192895Sjamie pr->pr_ip4 = NULL; 2667192895Sjamie } 2668192895Sjamie } 2669192895Sjamie return (0); 2670192895Sjamie} 2671192895Sjamie 2672192895Sjamie/* 2673185435Sbz * Pass back primary IPv4 address of this jail. 2674185435Sbz * 2675192895Sjamie * If not restricted return success but do not alter the address. Caller has 2676192895Sjamie * to make sure to initialize it correctly (e.g. INADDR_ANY). 2677185435Sbz * 2678188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4. 2679188144Sjamie * Address returned in NBO. 2680185435Sbz */ 268146155Sphkint 2682187684Sbzprison_get_ip4(struct ucred *cred, struct in_addr *ia) 268346155Sphk{ 2684191673Sjamie struct prison *pr; 268546155Sphk 2686185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2687185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2688185435Sbz 2689192895Sjamie pr = cred->cr_prison; 2690192895Sjamie if (!(pr->pr_flags & PR_IP4)) 269146155Sphk return (0); 2692191673Sjamie mtx_lock(&pr->pr_mtx); 2693192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2694192895Sjamie mtx_unlock(&pr->pr_mtx); 2695192895Sjamie return (0); 2696192895Sjamie } 2697191673Sjamie if (pr->pr_ip4 == NULL) { 2698191673Sjamie mtx_unlock(&pr->pr_mtx); 2699188144Sjamie return (EAFNOSUPPORT); 2700191673Sjamie } 2701185435Sbz 2702191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2703191673Sjamie mtx_unlock(&pr->pr_mtx); 2704185435Sbz return (0); 2705185435Sbz} 2706185435Sbz 2707185435Sbz/* 2708202468Sbz * Return 1 if we should do proper source address selection or are not jailed. 2709202468Sbz * We will return 0 if we should bypass source address selection in favour 2710202468Sbz * of the primary jail IPv4 address. Only in this case *ia will be updated and 2711202468Sbz * returned in NBO. 2712202468Sbz * Return EAFNOSUPPORT, in case this jail does not allow IPv4. 2713202468Sbz */ 2714202468Sbzint 2715202468Sbzprison_saddrsel_ip4(struct ucred *cred, struct in_addr *ia) 2716202468Sbz{ 2717202468Sbz struct prison *pr; 2718202468Sbz struct in_addr lia; 2719202468Sbz int error; 2720202468Sbz 2721202468Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2722202468Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2723202468Sbz 2724202468Sbz if (!jailed(cred)) 2725202468Sbz return (1); 2726202468Sbz 2727202468Sbz pr = cred->cr_prison; 2728202468Sbz if (pr->pr_flags & PR_IP4_SADDRSEL) 2729202468Sbz return (1); 2730202468Sbz 2731202468Sbz lia.s_addr = INADDR_ANY; 2732202468Sbz error = prison_get_ip4(cred, &lia); 2733202468Sbz if (error) 2734202468Sbz return (error); 2735202468Sbz if (lia.s_addr == INADDR_ANY) 2736202468Sbz return (1); 2737202468Sbz 2738202468Sbz ia->s_addr = lia.s_addr; 2739202468Sbz return (0); 2740202468Sbz} 2741202468Sbz 2742202468Sbz/* 2743192895Sjamie * Return true if pr1 and pr2 have the same IPv4 address restrictions. 2744192895Sjamie */ 2745192895Sjamieint 2746192895Sjamieprison_equal_ip4(struct prison *pr1, struct prison *pr2) 2747192895Sjamie{ 2748192895Sjamie 2749192895Sjamie if (pr1 == pr2) 2750192895Sjamie return (1); 2751192895Sjamie 2752192895Sjamie /* 2753195974Sjamie * No need to lock since the PR_IP4_USER flag can't be altered for 2754195974Sjamie * existing prisons. 2755192895Sjamie */ 2756195945Sjamie while (pr1 != &prison0 && 2757195945Sjamie#ifdef VIMAGE 2758195945Sjamie !(pr1->pr_flags & PR_VNET) && 2759195945Sjamie#endif 2760195945Sjamie !(pr1->pr_flags & PR_IP4_USER)) 2761192895Sjamie pr1 = pr1->pr_parent; 2762195945Sjamie while (pr2 != &prison0 && 2763195945Sjamie#ifdef VIMAGE 2764195945Sjamie !(pr2->pr_flags & PR_VNET) && 2765195945Sjamie#endif 2766195945Sjamie !(pr2->pr_flags & PR_IP4_USER)) 2767192895Sjamie pr2 = pr2->pr_parent; 2768192895Sjamie return (pr1 == pr2); 2769192895Sjamie} 2770192895Sjamie 2771192895Sjamie/* 2772185435Sbz * Make sure our (source) address is set to something meaningful to this 2773185435Sbz * jail. 2774185435Sbz * 2775192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail, 2776192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 2777192895Sjamie * doesn't allow IPv4. Address passed in in NBO and returned in NBO. 2778185435Sbz */ 2779185435Sbzint 2780185435Sbzprison_local_ip4(struct ucred *cred, struct in_addr *ia) 2781185435Sbz{ 2782191673Sjamie struct prison *pr; 2783185435Sbz struct in_addr ia0; 2784191673Sjamie int error; 2785185435Sbz 2786185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2787185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2788185435Sbz 2789192895Sjamie pr = cred->cr_prison; 2790192895Sjamie if (!(pr->pr_flags & PR_IP4)) 279146155Sphk return (0); 2792191673Sjamie mtx_lock(&pr->pr_mtx); 2793192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2794192895Sjamie mtx_unlock(&pr->pr_mtx); 2795192895Sjamie return (0); 2796192895Sjamie } 2797191673Sjamie if (pr->pr_ip4 == NULL) { 2798191673Sjamie mtx_unlock(&pr->pr_mtx); 2799188144Sjamie return (EAFNOSUPPORT); 2800191673Sjamie } 2801185435Sbz 2802185435Sbz ia0.s_addr = ntohl(ia->s_addr); 2803185435Sbz if (ia0.s_addr == INADDR_LOOPBACK) { 2804191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2805191673Sjamie mtx_unlock(&pr->pr_mtx); 2806185435Sbz return (0); 280746155Sphk } 2808185435Sbz 2809188144Sjamie if (ia0.s_addr == INADDR_ANY) { 2810188144Sjamie /* 2811188144Sjamie * In case there is only 1 IPv4 address, bind directly. 2812188144Sjamie */ 2813191673Sjamie if (pr->pr_ip4s == 1) 2814191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2815191673Sjamie mtx_unlock(&pr->pr_mtx); 2816185435Sbz return (0); 2817185435Sbz } 2818185435Sbz 2819191673Sjamie error = _prison_check_ip4(pr, ia); 2820191673Sjamie mtx_unlock(&pr->pr_mtx); 2821191673Sjamie return (error); 2822185435Sbz} 2823185435Sbz 2824185435Sbz/* 2825185435Sbz * Rewrite destination address in case we will connect to loopback address. 2826185435Sbz * 2827188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4. 2828188144Sjamie * Address passed in in NBO and returned in NBO. 2829185435Sbz */ 2830185435Sbzint 2831185435Sbzprison_remote_ip4(struct ucred *cred, struct in_addr *ia) 2832185435Sbz{ 2833191673Sjamie struct prison *pr; 2834185435Sbz 2835185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2836185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2837185435Sbz 2838192895Sjamie pr = cred->cr_prison; 2839192895Sjamie if (!(pr->pr_flags & PR_IP4)) 2840185435Sbz return (0); 2841191673Sjamie mtx_lock(&pr->pr_mtx); 2842192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2843192895Sjamie mtx_unlock(&pr->pr_mtx); 2844192895Sjamie return (0); 2845192895Sjamie } 2846191673Sjamie if (pr->pr_ip4 == NULL) { 2847191673Sjamie mtx_unlock(&pr->pr_mtx); 2848188144Sjamie return (EAFNOSUPPORT); 2849191673Sjamie } 2850188144Sjamie 2851185435Sbz if (ntohl(ia->s_addr) == INADDR_LOOPBACK) { 2852191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2853191673Sjamie mtx_unlock(&pr->pr_mtx); 2854185435Sbz return (0); 2855185435Sbz } 2856185435Sbz 2857185435Sbz /* 2858185435Sbz * Return success because nothing had to be changed. 2859185435Sbz */ 2860191673Sjamie mtx_unlock(&pr->pr_mtx); 2861185435Sbz return (0); 2862185435Sbz} 2863185435Sbz 2864185435Sbz/* 2865188144Sjamie * Check if given address belongs to the jail referenced by cred/prison. 2866185435Sbz * 2867192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail, 2868192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 2869192895Sjamie * doesn't allow IPv4. Address passed in in NBO. 2870185435Sbz */ 2871185435Sbzstatic int 2872185435Sbz_prison_check_ip4(struct prison *pr, struct in_addr *ia) 2873185435Sbz{ 2874185435Sbz int i, a, z, d; 2875185435Sbz 2876185435Sbz /* 2877185435Sbz * Check the primary IP. 2878185435Sbz */ 2879185435Sbz if (pr->pr_ip4[0].s_addr == ia->s_addr) 2880188144Sjamie return (0); 2881185435Sbz 2882185435Sbz /* 2883185435Sbz * All the other IPs are sorted so we can do a binary search. 2884185435Sbz */ 2885185435Sbz a = 0; 2886185435Sbz z = pr->pr_ip4s - 2; 2887185435Sbz while (a <= z) { 2888185435Sbz i = (a + z) / 2; 2889185435Sbz d = qcmp_v4(&pr->pr_ip4[i+1], ia); 2890185435Sbz if (d > 0) 2891185435Sbz z = i - 1; 2892185435Sbz else if (d < 0) 2893185435Sbz a = i + 1; 289481114Srwatson else 2895188144Sjamie return (0); 2896185435Sbz } 2897188144Sjamie 2898188144Sjamie return (EADDRNOTAVAIL); 2899185435Sbz} 2900185435Sbz 2901185435Sbzint 2902185435Sbzprison_check_ip4(struct ucred *cred, struct in_addr *ia) 2903185435Sbz{ 2904191673Sjamie struct prison *pr; 2905191673Sjamie int error; 2906185435Sbz 2907185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2908185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2909185435Sbz 2910192895Sjamie pr = cred->cr_prison; 2911192895Sjamie if (!(pr->pr_flags & PR_IP4)) 2912188144Sjamie return (0); 2913191673Sjamie mtx_lock(&pr->pr_mtx); 2914192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2915192895Sjamie mtx_unlock(&pr->pr_mtx); 2916192895Sjamie return (0); 2917192895Sjamie } 2918191673Sjamie if (pr->pr_ip4 == NULL) { 2919191673Sjamie mtx_unlock(&pr->pr_mtx); 2920188144Sjamie return (EAFNOSUPPORT); 2921191673Sjamie } 2922185435Sbz 2923191673Sjamie error = _prison_check_ip4(pr, ia); 2924191673Sjamie mtx_unlock(&pr->pr_mtx); 2925191673Sjamie return (error); 2926185435Sbz} 2927185435Sbz#endif 2928185435Sbz 2929185435Sbz#ifdef INET6 2930192895Sjamiestatic int 2931192895Sjamieprison_restrict_ip6(struct prison *pr, struct in6_addr *newip6) 2932192895Sjamie{ 2933192895Sjamie int ii, ij, used; 2934192895Sjamie struct prison *ppr; 2935192895Sjamie 2936192895Sjamie ppr = pr->pr_parent; 2937192895Sjamie if (!(pr->pr_flags & PR_IP6_USER)) { 2938192895Sjamie /* This has no user settings, so just copy the parent's list. */ 2939192895Sjamie if (pr->pr_ip6s < ppr->pr_ip6s) { 2940192895Sjamie /* 2941192895Sjamie * There's no room for the parent's list. Use the 2942192895Sjamie * new list buffer, which is assumed to be big enough 2943192895Sjamie * (if it was passed). If there's no buffer, try to 2944192895Sjamie * allocate one. 2945192895Sjamie */ 2946192895Sjamie used = 1; 2947192895Sjamie if (newip6 == NULL) { 2948192895Sjamie newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6), 2949192895Sjamie M_PRISON, M_NOWAIT); 2950192895Sjamie if (newip6 != NULL) 2951192895Sjamie used = 0; 2952192895Sjamie } 2953192895Sjamie if (newip6 != NULL) { 2954192895Sjamie bcopy(ppr->pr_ip6, newip6, 2955192895Sjamie ppr->pr_ip6s * sizeof(*newip6)); 2956192895Sjamie free(pr->pr_ip6, M_PRISON); 2957192895Sjamie pr->pr_ip6 = newip6; 2958192895Sjamie pr->pr_ip6s = ppr->pr_ip6s; 2959192895Sjamie } 2960192895Sjamie return (used); 2961192895Sjamie } 2962192895Sjamie pr->pr_ip6s = ppr->pr_ip6s; 2963192895Sjamie if (pr->pr_ip6s > 0) 2964192895Sjamie bcopy(ppr->pr_ip6, pr->pr_ip6, 2965192895Sjamie pr->pr_ip6s * sizeof(*newip6)); 2966192895Sjamie else if (pr->pr_ip6 != NULL) { 2967192895Sjamie free(pr->pr_ip6, M_PRISON); 2968192895Sjamie pr->pr_ip6 = NULL; 2969192895Sjamie } 2970195974Sjamie } else if (pr->pr_ip6s > 0) { 2971192895Sjamie /* Remove addresses that aren't in the parent. */ 2972192895Sjamie for (ij = 0; ij < ppr->pr_ip6s; ij++) 2973192895Sjamie if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], 2974192895Sjamie &ppr->pr_ip6[ij])) 2975192895Sjamie break; 2976192895Sjamie if (ij < ppr->pr_ip6s) 2977192895Sjamie ii = 1; 2978192895Sjamie else { 2979192895Sjamie bcopy(pr->pr_ip6 + 1, pr->pr_ip6, 2980192895Sjamie --pr->pr_ip6s * sizeof(*pr->pr_ip6)); 2981192895Sjamie ii = 0; 2982192895Sjamie } 2983192895Sjamie for (ij = 1; ii < pr->pr_ip6s; ) { 2984192895Sjamie if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii], 2985192895Sjamie &ppr->pr_ip6[0])) { 2986192895Sjamie ii++; 2987192895Sjamie continue; 2988192895Sjamie } 2989192895Sjamie switch (ij >= ppr->pr_ip4s ? -1 : 2990192895Sjamie qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) { 2991192895Sjamie case -1: 2992192895Sjamie bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii, 2993192895Sjamie (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6)); 2994192895Sjamie break; 2995192895Sjamie case 0: 2996192895Sjamie ii++; 2997192895Sjamie ij++; 2998192895Sjamie break; 2999192895Sjamie case 1: 3000192895Sjamie ij++; 3001192895Sjamie break; 3002192895Sjamie } 3003192895Sjamie } 3004192895Sjamie if (pr->pr_ip6s == 0) { 3005195870Sjamie pr->pr_flags |= PR_IP6_DISABLE; 3006192895Sjamie free(pr->pr_ip6, M_PRISON); 3007192895Sjamie pr->pr_ip6 = NULL; 3008192895Sjamie } 3009192895Sjamie } 3010192895Sjamie return 0; 3011192895Sjamie} 3012192895Sjamie 3013185435Sbz/* 3014185435Sbz * Pass back primary IPv6 address for this jail. 3015185435Sbz * 3016192895Sjamie * If not restricted return success but do not alter the address. Caller has 3017192895Sjamie * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT). 3018185435Sbz * 3019188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6. 3020185435Sbz */ 3021185435Sbzint 3022187684Sbzprison_get_ip6(struct ucred *cred, struct in6_addr *ia6) 3023185435Sbz{ 3024191673Sjamie struct prison *pr; 3025185435Sbz 3026185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3027185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3028185435Sbz 3029192895Sjamie pr = cred->cr_prison; 3030192895Sjamie if (!(pr->pr_flags & PR_IP6)) 303181114Srwatson return (0); 3032191673Sjamie mtx_lock(&pr->pr_mtx); 3033192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3034192895Sjamie mtx_unlock(&pr->pr_mtx); 3035192895Sjamie return (0); 3036192895Sjamie } 3037191673Sjamie if (pr->pr_ip6 == NULL) { 3038191673Sjamie mtx_unlock(&pr->pr_mtx); 3039188144Sjamie return (EAFNOSUPPORT); 3040191673Sjamie } 3041188144Sjamie 3042191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3043191673Sjamie mtx_unlock(&pr->pr_mtx); 3044185435Sbz return (0); 3045185435Sbz} 3046185435Sbz 3047185435Sbz/* 3048202468Sbz * Return 1 if we should do proper source address selection or are not jailed. 3049202468Sbz * We will return 0 if we should bypass source address selection in favour 3050202468Sbz * of the primary jail IPv6 address. Only in this case *ia will be updated and 3051202468Sbz * returned in NBO. 3052202468Sbz * Return EAFNOSUPPORT, in case this jail does not allow IPv6. 3053202468Sbz */ 3054202468Sbzint 3055202468Sbzprison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia6) 3056202468Sbz{ 3057202468Sbz struct prison *pr; 3058202468Sbz struct in6_addr lia6; 3059202468Sbz int error; 3060202468Sbz 3061202468Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3062202468Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3063202468Sbz 3064202468Sbz if (!jailed(cred)) 3065202468Sbz return (1); 3066202468Sbz 3067202468Sbz pr = cred->cr_prison; 3068202468Sbz if (pr->pr_flags & PR_IP6_SADDRSEL) 3069202468Sbz return (1); 3070202468Sbz 3071202468Sbz lia6 = in6addr_any; 3072202468Sbz error = prison_get_ip6(cred, &lia6); 3073202468Sbz if (error) 3074202468Sbz return (error); 3075202468Sbz if (IN6_IS_ADDR_UNSPECIFIED(&lia6)) 3076202468Sbz return (1); 3077202468Sbz 3078202468Sbz bcopy(&lia6, ia6, sizeof(struct in6_addr)); 3079202468Sbz return (0); 3080202468Sbz} 3081202468Sbz 3082202468Sbz/* 3083192895Sjamie * Return true if pr1 and pr2 have the same IPv6 address restrictions. 3084192895Sjamie */ 3085192895Sjamieint 3086192895Sjamieprison_equal_ip6(struct prison *pr1, struct prison *pr2) 3087192895Sjamie{ 3088192895Sjamie 3089192895Sjamie if (pr1 == pr2) 3090192895Sjamie return (1); 3091192895Sjamie 3092195945Sjamie while (pr1 != &prison0 && 3093195945Sjamie#ifdef VIMAGE 3094195945Sjamie !(pr1->pr_flags & PR_VNET) && 3095195945Sjamie#endif 3096195945Sjamie !(pr1->pr_flags & PR_IP6_USER)) 3097192895Sjamie pr1 = pr1->pr_parent; 3098195945Sjamie while (pr2 != &prison0 && 3099195945Sjamie#ifdef VIMAGE 3100195945Sjamie !(pr2->pr_flags & PR_VNET) && 3101195945Sjamie#endif 3102195945Sjamie !(pr2->pr_flags & PR_IP6_USER)) 3103192895Sjamie pr2 = pr2->pr_parent; 3104192895Sjamie return (pr1 == pr2); 3105192895Sjamie} 3106192895Sjamie 3107192895Sjamie/* 3108185435Sbz * Make sure our (source) address is set to something meaningful to this jail. 3109185435Sbz * 3110185435Sbz * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0) 3111185435Sbz * when needed while binding. 3112185435Sbz * 3113192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail, 3114192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 3115192895Sjamie * doesn't allow IPv6. 3116185435Sbz */ 3117185435Sbzint 3118185435Sbzprison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only) 3119185435Sbz{ 3120191673Sjamie struct prison *pr; 3121191673Sjamie int error; 3122185435Sbz 3123185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3124185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3125185435Sbz 3126192895Sjamie pr = cred->cr_prison; 3127192895Sjamie if (!(pr->pr_flags & PR_IP6)) 3128185435Sbz return (0); 3129191673Sjamie mtx_lock(&pr->pr_mtx); 3130192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3131192895Sjamie mtx_unlock(&pr->pr_mtx); 3132192895Sjamie return (0); 3133192895Sjamie } 3134191673Sjamie if (pr->pr_ip6 == NULL) { 3135191673Sjamie mtx_unlock(&pr->pr_mtx); 3136188144Sjamie return (EAFNOSUPPORT); 3137191673Sjamie } 3138188144Sjamie 3139185435Sbz if (IN6_IS_ADDR_LOOPBACK(ia6)) { 3140191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3141191673Sjamie mtx_unlock(&pr->pr_mtx); 3142185435Sbz return (0); 314381114Srwatson } 3144185435Sbz 3145188144Sjamie if (IN6_IS_ADDR_UNSPECIFIED(ia6)) { 3146188144Sjamie /* 3147188144Sjamie * In case there is only 1 IPv6 address, and v6only is true, 3148188144Sjamie * then bind directly. 3149188144Sjamie */ 3150191673Sjamie if (v6only != 0 && pr->pr_ip6s == 1) 3151191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3152191673Sjamie mtx_unlock(&pr->pr_mtx); 3153185435Sbz return (0); 3154185435Sbz } 3155188144Sjamie 3156191673Sjamie error = _prison_check_ip6(pr, ia6); 3157191673Sjamie mtx_unlock(&pr->pr_mtx); 3158191673Sjamie return (error); 3159185435Sbz} 3160185435Sbz 3161185435Sbz/* 3162185435Sbz * Rewrite destination address in case we will connect to loopback address. 3163185435Sbz * 3164188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6. 3165185435Sbz */ 3166185435Sbzint 3167185435Sbzprison_remote_ip6(struct ucred *cred, struct in6_addr *ia6) 3168185435Sbz{ 3169191673Sjamie struct prison *pr; 3170185435Sbz 3171185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3172185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3173185435Sbz 3174192895Sjamie pr = cred->cr_prison; 3175192895Sjamie if (!(pr->pr_flags & PR_IP6)) 3176185435Sbz return (0); 3177191673Sjamie mtx_lock(&pr->pr_mtx); 3178192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3179192895Sjamie mtx_unlock(&pr->pr_mtx); 3180192895Sjamie return (0); 3181192895Sjamie } 3182191673Sjamie if (pr->pr_ip6 == NULL) { 3183191673Sjamie mtx_unlock(&pr->pr_mtx); 3184188144Sjamie return (EAFNOSUPPORT); 3185191673Sjamie } 3186188144Sjamie 3187185435Sbz if (IN6_IS_ADDR_LOOPBACK(ia6)) { 3188191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3189191673Sjamie mtx_unlock(&pr->pr_mtx); 3190185435Sbz return (0); 3191185435Sbz } 3192185435Sbz 3193185435Sbz /* 3194185435Sbz * Return success because nothing had to be changed. 3195185435Sbz */ 3196191673Sjamie mtx_unlock(&pr->pr_mtx); 319746155Sphk return (0); 319846155Sphk} 319946155Sphk 3200185435Sbz/* 3201188144Sjamie * Check if given address belongs to the jail referenced by cred/prison. 3202185435Sbz * 3203192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail, 3204192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 3205192895Sjamie * doesn't allow IPv6. 3206185435Sbz */ 3207185435Sbzstatic int 3208185435Sbz_prison_check_ip6(struct prison *pr, struct in6_addr *ia6) 320946155Sphk{ 3210185435Sbz int i, a, z, d; 321146155Sphk 3212185435Sbz /* 3213185435Sbz * Check the primary IP. 3214185435Sbz */ 3215185435Sbz if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6)) 3216188144Sjamie return (0); 3217185435Sbz 3218185435Sbz /* 3219185435Sbz * All the other IPs are sorted so we can do a binary search. 3220185435Sbz */ 3221185435Sbz a = 0; 3222185435Sbz z = pr->pr_ip6s - 2; 3223185435Sbz while (a <= z) { 3224185435Sbz i = (a + z) / 2; 3225185435Sbz d = qcmp_v6(&pr->pr_ip6[i+1], ia6); 3226185435Sbz if (d > 0) 3227185435Sbz z = i - 1; 3228185435Sbz else if (d < 0) 3229185435Sbz a = i + 1; 323046155Sphk else 3231188144Sjamie return (0); 323246155Sphk } 3233188144Sjamie 3234188144Sjamie return (EADDRNOTAVAIL); 323546155Sphk} 323646155Sphk 323746155Sphkint 3238185435Sbzprison_check_ip6(struct ucred *cred, struct in6_addr *ia6) 3239185435Sbz{ 3240191673Sjamie struct prison *pr; 3241191673Sjamie int error; 3242185435Sbz 3243185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3244185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3245185435Sbz 3246192895Sjamie pr = cred->cr_prison; 3247192895Sjamie if (!(pr->pr_flags & PR_IP6)) 3248188144Sjamie return (0); 3249191673Sjamie mtx_lock(&pr->pr_mtx); 3250192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3251192895Sjamie mtx_unlock(&pr->pr_mtx); 3252192895Sjamie return (0); 3253192895Sjamie } 3254191673Sjamie if (pr->pr_ip6 == NULL) { 3255191673Sjamie mtx_unlock(&pr->pr_mtx); 3256188144Sjamie return (EAFNOSUPPORT); 3257191673Sjamie } 3258185435Sbz 3259191673Sjamie error = _prison_check_ip6(pr, ia6); 3260191673Sjamie mtx_unlock(&pr->pr_mtx); 3261191673Sjamie return (error); 3262185435Sbz} 3263185435Sbz#endif 3264185435Sbz 3265185435Sbz/* 3266188146Sjamie * Check if a jail supports the given address family. 3267188146Sjamie * 3268188146Sjamie * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT 3269188146Sjamie * if not. 3270188146Sjamie */ 3271188146Sjamieint 3272188146Sjamieprison_check_af(struct ucred *cred, int af) 3273188146Sjamie{ 3274192895Sjamie struct prison *pr; 3275188146Sjamie int error; 3276188146Sjamie 3277188146Sjamie KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3278188146Sjamie 3279192895Sjamie pr = cred->cr_prison; 3280194923Sjamie#ifdef VIMAGE 3281194915Sjamie /* Prisons with their own network stack are not limited. */ 3282200473Sbz if (prison_owns_vnet(cred)) 3283194915Sjamie return (0); 3284194923Sjamie#endif 3285194915Sjamie 3286188146Sjamie error = 0; 3287188146Sjamie switch (af) 3288188146Sjamie { 3289188146Sjamie#ifdef INET 3290188146Sjamie case AF_INET: 3291192895Sjamie if (pr->pr_flags & PR_IP4) 3292192895Sjamie { 3293192895Sjamie mtx_lock(&pr->pr_mtx); 3294192895Sjamie if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL) 3295192895Sjamie error = EAFNOSUPPORT; 3296192895Sjamie mtx_unlock(&pr->pr_mtx); 3297192895Sjamie } 3298188146Sjamie break; 3299188146Sjamie#endif 3300188146Sjamie#ifdef INET6 3301188146Sjamie case AF_INET6: 3302192895Sjamie if (pr->pr_flags & PR_IP6) 3303192895Sjamie { 3304192895Sjamie mtx_lock(&pr->pr_mtx); 3305192895Sjamie if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL) 3306192895Sjamie error = EAFNOSUPPORT; 3307192895Sjamie mtx_unlock(&pr->pr_mtx); 3308192895Sjamie } 3309188146Sjamie break; 3310188146Sjamie#endif 3311188146Sjamie case AF_LOCAL: 3312188146Sjamie case AF_ROUTE: 3313188146Sjamie break; 3314188146Sjamie default: 3315192895Sjamie if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF)) 3316188146Sjamie error = EAFNOSUPPORT; 3317188146Sjamie } 3318188146Sjamie return (error); 3319188146Sjamie} 3320188146Sjamie 3321188146Sjamie/* 3322185435Sbz * Check if given address belongs to the jail referenced by cred (wrapper to 3323185435Sbz * prison_check_ip[46]). 3324185435Sbz * 3325192895Sjamie * Returns 0 if jail doesn't restrict the address family or if address belongs 3326192895Sjamie * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if 3327192895Sjamie * the jail doesn't allow the address family. IPv4 Address passed in in NBO. 3328185435Sbz */ 3329185435Sbzint 333072786Srwatsonprison_if(struct ucred *cred, struct sockaddr *sa) 333146155Sphk{ 3332185435Sbz#ifdef INET 3333114168Smike struct sockaddr_in *sai; 3334185435Sbz#endif 3335185435Sbz#ifdef INET6 3336185435Sbz struct sockaddr_in6 *sai6; 3337185435Sbz#endif 3338188144Sjamie int error; 333946155Sphk 3340185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3341185435Sbz KASSERT(sa != NULL, ("%s: sa is NULL", __func__)); 3342185435Sbz 3343200473Sbz#ifdef VIMAGE 3344200473Sbz if (prison_owns_vnet(cred)) 3345200473Sbz return (0); 3346200473Sbz#endif 3347200473Sbz 3348188144Sjamie error = 0; 3349188144Sjamie switch (sa->sa_family) 3350185435Sbz { 3351185435Sbz#ifdef INET 3352185435Sbz case AF_INET: 3353185435Sbz sai = (struct sockaddr_in *)sa; 3354188144Sjamie error = prison_check_ip4(cred, &sai->sin_addr); 3355185435Sbz break; 3356185435Sbz#endif 3357185435Sbz#ifdef INET6 3358185435Sbz case AF_INET6: 3359185435Sbz sai6 = (struct sockaddr_in6 *)sa; 3360188144Sjamie error = prison_check_ip6(cred, &sai6->sin6_addr); 3361185435Sbz break; 3362185435Sbz#endif 3363185435Sbz default: 3364192895Sjamie if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF)) 3365188144Sjamie error = EAFNOSUPPORT; 3366185435Sbz } 3367188144Sjamie return (error); 336846155Sphk} 336972786Srwatson 337072786Srwatson/* 337172786Srwatson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH. 337272786Srwatson */ 337372786Srwatsonint 3374114168Smikeprison_check(struct ucred *cred1, struct ucred *cred2) 337572786Srwatson{ 337672786Srwatson 3377192895Sjamie return ((cred1->cr_prison == cred2->cr_prison || 3378192895Sjamie prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH); 3379192895Sjamie} 338072786Srwatson 3381192895Sjamie/* 3382192895Sjamie * Return 1 if p2 is a child of p1, otherwise 0. 3383192895Sjamie */ 3384192895Sjamieint 3385192895Sjamieprison_ischild(struct prison *pr1, struct prison *pr2) 3386192895Sjamie{ 3387192895Sjamie 3388192895Sjamie for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent) 3389192895Sjamie if (pr1 == pr2) 3390192895Sjamie return (1); 339172786Srwatson return (0); 339272786Srwatson} 339372786Srwatson 339472786Srwatson/* 339572786Srwatson * Return 1 if the passed credential is in a jail, otherwise 0. 339672786Srwatson */ 339772786Srwatsonint 3398114168Smikejailed(struct ucred *cred) 339972786Srwatson{ 340072786Srwatson 3401192895Sjamie return (cred->cr_prison != &prison0); 340272786Srwatson} 340391384Srobert 340491384Srobert/* 3405200473Sbz * Return 1 if the passed credential is in a jail and that jail does not 3406200473Sbz * have its own virtual network stack, otherwise 0. 3407200473Sbz */ 3408200473Sbzint 3409200473Sbzjailed_without_vnet(struct ucred *cred) 3410200473Sbz{ 3411200473Sbz 3412200473Sbz if (!jailed(cred)) 3413200473Sbz return (0); 3414200473Sbz#ifdef VIMAGE 3415200473Sbz if (prison_owns_vnet(cred)) 3416200473Sbz return (0); 3417200473Sbz#endif 3418200473Sbz 3419200473Sbz return (1); 3420200473Sbz} 3421200473Sbz 3422200473Sbz/* 3423194090Sjamie * Return the correct hostname (domainname, et al) for the passed credential. 342491384Srobert */ 342591391Srobertvoid 3426114168Smikegetcredhostname(struct ucred *cred, char *buf, size_t size) 342791384Srobert{ 3428193066Sjamie struct prison *pr; 342991384Srobert 3430194090Sjamie /* 3431194090Sjamie * A NULL credential can be used to shortcut to the physical 3432194090Sjamie * system's hostname. 3433194090Sjamie */ 3434193066Sjamie pr = (cred != NULL) ? cred->cr_prison : &prison0; 3435193066Sjamie mtx_lock(&pr->pr_mtx); 3436194118Sjamie strlcpy(buf, pr->pr_hostname, size); 3437193066Sjamie mtx_unlock(&pr->pr_mtx); 343891384Srobert} 3439113275Smike 3440194090Sjamievoid 3441194090Sjamiegetcreddomainname(struct ucred *cred, char *buf, size_t size) 3442194090Sjamie{ 3443194090Sjamie 3444194090Sjamie mtx_lock(&cred->cr_prison->pr_mtx); 3445194118Sjamie strlcpy(buf, cred->cr_prison->pr_domainname, size); 3446194090Sjamie mtx_unlock(&cred->cr_prison->pr_mtx); 3447194090Sjamie} 3448194090Sjamie 3449194090Sjamievoid 3450194090Sjamiegetcredhostuuid(struct ucred *cred, char *buf, size_t size) 3451194090Sjamie{ 3452194090Sjamie 3453194090Sjamie mtx_lock(&cred->cr_prison->pr_mtx); 3454194118Sjamie strlcpy(buf, cred->cr_prison->pr_hostuuid, size); 3455194090Sjamie mtx_unlock(&cred->cr_prison->pr_mtx); 3456194090Sjamie} 3457194090Sjamie 3458194090Sjamievoid 3459194090Sjamiegetcredhostid(struct ucred *cred, unsigned long *hostid) 3460194090Sjamie{ 3461194090Sjamie 3462194090Sjamie mtx_lock(&cred->cr_prison->pr_mtx); 3463194090Sjamie *hostid = cred->cr_prison->pr_hostid; 3464194090Sjamie mtx_unlock(&cred->cr_prison->pr_mtx); 3465194090Sjamie} 3466194090Sjamie 3467196176Sbz#ifdef VIMAGE 3468125804Srwatson/* 3469196176Sbz * Determine whether the prison represented by cred owns 3470196176Sbz * its vnet rather than having it inherited. 3471196176Sbz * 3472196176Sbz * Returns 1 in case the prison owns the vnet, 0 otherwise. 3473196176Sbz */ 3474196176Sbzint 3475196176Sbzprison_owns_vnet(struct ucred *cred) 3476196176Sbz{ 3477196176Sbz 3478196176Sbz /* 3479196176Sbz * vnets cannot be added/removed after jail creation, 3480196176Sbz * so no need to lock here. 3481196176Sbz */ 3482196176Sbz return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0); 3483196176Sbz} 3484196176Sbz#endif 3485196176Sbz 3486196176Sbz/* 3487147185Spjd * Determine whether the subject represented by cred can "see" 3488147185Spjd * status of a mount point. 3489147185Spjd * Returns: 0 for permitted, ENOENT otherwise. 3490147185Spjd * XXX: This function should be called cr_canseemount() and should be 3491147185Spjd * placed in kern_prot.c. 3492125804Srwatson */ 3493125804Srwatsonint 3494147185Spjdprison_canseemount(struct ucred *cred, struct mount *mp) 3495125804Srwatson{ 3496147185Spjd struct prison *pr; 3497147185Spjd struct statfs *sp; 3498147185Spjd size_t len; 3499125804Srwatson 3500192895Sjamie pr = cred->cr_prison; 3501192895Sjamie if (pr->pr_enforce_statfs == 0) 3502147185Spjd return (0); 3503147185Spjd if (pr->pr_root->v_mount == mp) 3504147185Spjd return (0); 3505192895Sjamie if (pr->pr_enforce_statfs == 2) 3506147185Spjd return (ENOENT); 3507147185Spjd /* 3508147185Spjd * If jail's chroot directory is set to "/" we should be able to see 3509147185Spjd * all mount-points from inside a jail. 3510147185Spjd * This is ugly check, but this is the only situation when jail's 3511147185Spjd * directory ends with '/'. 3512147185Spjd */ 3513147185Spjd if (strcmp(pr->pr_path, "/") == 0) 3514147185Spjd return (0); 3515147185Spjd len = strlen(pr->pr_path); 3516147185Spjd sp = &mp->mnt_stat; 3517147185Spjd if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0) 3518147185Spjd return (ENOENT); 3519147185Spjd /* 3520147185Spjd * Be sure that we don't have situation where jail's root directory 3521147185Spjd * is "/some/path" and mount point is "/some/pathpath". 3522147185Spjd */ 3523147185Spjd if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/') 3524147185Spjd return (ENOENT); 3525147185Spjd return (0); 3526147185Spjd} 3527147185Spjd 3528147185Spjdvoid 3529147185Spjdprison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp) 3530147185Spjd{ 3531147185Spjd char jpath[MAXPATHLEN]; 3532147185Spjd struct prison *pr; 3533147185Spjd size_t len; 3534147185Spjd 3535192895Sjamie pr = cred->cr_prison; 3536192895Sjamie if (pr->pr_enforce_statfs == 0) 3537147185Spjd return; 3538147185Spjd if (prison_canseemount(cred, mp) != 0) { 3539147185Spjd bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3540147185Spjd strlcpy(sp->f_mntonname, "[restricted]", 3541147185Spjd sizeof(sp->f_mntonname)); 3542147185Spjd return; 3543125804Srwatson } 3544147185Spjd if (pr->pr_root->v_mount == mp) { 3545147185Spjd /* 3546147185Spjd * Clear current buffer data, so we are sure nothing from 3547147185Spjd * the valid path left there. 3548147185Spjd */ 3549147185Spjd bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3550147185Spjd *sp->f_mntonname = '/'; 3551147185Spjd return; 3552147185Spjd } 3553147185Spjd /* 3554147185Spjd * If jail's chroot directory is set to "/" we should be able to see 3555147185Spjd * all mount-points from inside a jail. 3556147185Spjd */ 3557147185Spjd if (strcmp(pr->pr_path, "/") == 0) 3558147185Spjd return; 3559147185Spjd len = strlen(pr->pr_path); 3560147185Spjd strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath)); 3561147185Spjd /* 3562147185Spjd * Clear current buffer data, so we are sure nothing from 3563147185Spjd * the valid path left there. 3564147185Spjd */ 3565147185Spjd bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3566147185Spjd if (*jpath == '\0') { 3567147185Spjd /* Should never happen. */ 3568147185Spjd *sp->f_mntonname = '/'; 3569147185Spjd } else { 3570147185Spjd strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname)); 3571147185Spjd } 3572125804Srwatson} 3573125804Srwatson 3574164032Srwatson/* 3575164032Srwatson * Check with permission for a specific privilege is granted within jail. We 3576164032Srwatson * have a specific list of accepted privileges; the rest are denied. 3577164032Srwatson */ 3578164032Srwatsonint 3579164032Srwatsonprison_priv_check(struct ucred *cred, int priv) 3580164032Srwatson{ 3581164032Srwatson 3582164032Srwatson if (!jailed(cred)) 3583164032Srwatson return (0); 3584164032Srwatson 3585194915Sjamie#ifdef VIMAGE 3586194915Sjamie /* 3587194915Sjamie * Privileges specific to prisons with a virtual network stack. 3588194915Sjamie * There might be a duplicate entry here in case the privilege 3589194915Sjamie * is only granted conditionally in the legacy jail case. 3590194915Sjamie */ 3591164032Srwatson switch (priv) { 3592194915Sjamie#ifdef notyet 3593194915Sjamie /* 3594194915Sjamie * NFS-specific privileges. 3595194915Sjamie */ 3596194915Sjamie case PRIV_NFS_DAEMON: 3597194915Sjamie case PRIV_NFS_LOCKD: 3598194915Sjamie#endif 3599194915Sjamie /* 3600194915Sjamie * Network stack privileges. 3601194915Sjamie */ 3602194915Sjamie case PRIV_NET_BRIDGE: 3603194915Sjamie case PRIV_NET_GRE: 3604194915Sjamie case PRIV_NET_BPF: 3605194915Sjamie case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */ 3606194915Sjamie case PRIV_NET_ROUTE: 3607194915Sjamie case PRIV_NET_TAP: 3608194915Sjamie case PRIV_NET_SETIFMTU: 3609194915Sjamie case PRIV_NET_SETIFFLAGS: 3610194915Sjamie case PRIV_NET_SETIFCAP: 3611203052Sdelphij case PRIV_NET_SETIFDESCR: 3612194915Sjamie case PRIV_NET_SETIFNAME : 3613194915Sjamie case PRIV_NET_SETIFMETRIC: 3614194915Sjamie case PRIV_NET_SETIFPHYS: 3615194915Sjamie case PRIV_NET_SETIFMAC: 3616194915Sjamie case PRIV_NET_ADDMULTI: 3617194915Sjamie case PRIV_NET_DELMULTI: 3618194915Sjamie case PRIV_NET_HWIOCTL: 3619194915Sjamie case PRIV_NET_SETLLADDR: 3620194915Sjamie case PRIV_NET_ADDIFGROUP: 3621194915Sjamie case PRIV_NET_DELIFGROUP: 3622194915Sjamie case PRIV_NET_IFCREATE: 3623194915Sjamie case PRIV_NET_IFDESTROY: 3624194915Sjamie case PRIV_NET_ADDIFADDR: 3625194915Sjamie case PRIV_NET_DELIFADDR: 3626194915Sjamie case PRIV_NET_LAGG: 3627194915Sjamie case PRIV_NET_GIF: 3628194915Sjamie case PRIV_NET_SETIFVNET: 3629164032Srwatson 3630164032Srwatson /* 3631194915Sjamie * 802.11-related privileges. 3632194915Sjamie */ 3633194915Sjamie case PRIV_NET80211_GETKEY: 3634194915Sjamie#ifdef notyet 3635194915Sjamie case PRIV_NET80211_MANAGE: /* XXX-BZ discuss with sam@ */ 3636194915Sjamie#endif 3637194915Sjamie 3638194915Sjamie#ifdef notyet 3639194915Sjamie /* 3640194915Sjamie * AppleTalk privileges. 3641194915Sjamie */ 3642194915Sjamie case PRIV_NETATALK_RESERVEDPORT: 3643194915Sjamie 3644194915Sjamie /* 3645194915Sjamie * ATM privileges. 3646194915Sjamie */ 3647194915Sjamie case PRIV_NETATM_CFG: 3648194915Sjamie case PRIV_NETATM_ADD: 3649194915Sjamie case PRIV_NETATM_DEL: 3650194915Sjamie case PRIV_NETATM_SET: 3651194915Sjamie 3652194915Sjamie /* 3653194915Sjamie * Bluetooth privileges. 3654194915Sjamie */ 3655194915Sjamie case PRIV_NETBLUETOOTH_RAW: 3656194915Sjamie#endif 3657194915Sjamie 3658194915Sjamie /* 3659194915Sjamie * Netgraph and netgraph module privileges. 3660194915Sjamie */ 3661194915Sjamie case PRIV_NETGRAPH_CONTROL: 3662194915Sjamie#ifdef notyet 3663194915Sjamie case PRIV_NETGRAPH_TTY: 3664194915Sjamie#endif 3665194915Sjamie 3666194915Sjamie /* 3667194915Sjamie * IPv4 and IPv6 privileges. 3668194915Sjamie */ 3669194915Sjamie case PRIV_NETINET_IPFW: 3670194915Sjamie case PRIV_NETINET_DIVERT: 3671194915Sjamie case PRIV_NETINET_PF: 3672194915Sjamie case PRIV_NETINET_DUMMYNET: 3673194915Sjamie case PRIV_NETINET_CARP: 3674194915Sjamie case PRIV_NETINET_MROUTE: 3675194915Sjamie case PRIV_NETINET_RAW: 3676194915Sjamie case PRIV_NETINET_ADDRCTRL6: 3677194915Sjamie case PRIV_NETINET_ND6: 3678194915Sjamie case PRIV_NETINET_SCOPE6: 3679194915Sjamie case PRIV_NETINET_ALIFETIME6: 3680194915Sjamie case PRIV_NETINET_IPSEC: 3681194915Sjamie case PRIV_NETINET_BINDANY: 3682194915Sjamie 3683194915Sjamie#ifdef notyet 3684194915Sjamie /* 3685194915Sjamie * IPX/SPX privileges. 3686194915Sjamie */ 3687194915Sjamie case PRIV_NETIPX_RESERVEDPORT: 3688194915Sjamie case PRIV_NETIPX_RAW: 3689194915Sjamie 3690194915Sjamie /* 3691194915Sjamie * NCP privileges. 3692194915Sjamie */ 3693194915Sjamie case PRIV_NETNCP: 3694194915Sjamie 3695194915Sjamie /* 3696194915Sjamie * SMB privileges. 3697194915Sjamie */ 3698194915Sjamie case PRIV_NETSMB: 3699194915Sjamie#endif 3700194915Sjamie 3701194915Sjamie /* 3702194915Sjamie * No default: or deny here. 3703194915Sjamie * In case of no permit fall through to next switch(). 3704194915Sjamie */ 3705194915Sjamie if (cred->cr_prison->pr_flags & PR_VNET) 3706194915Sjamie return (0); 3707194915Sjamie } 3708194915Sjamie#endif /* VIMAGE */ 3709194915Sjamie 3710194915Sjamie switch (priv) { 3711194915Sjamie 3712194915Sjamie /* 3713164032Srwatson * Allow ktrace privileges for root in jail. 3714164032Srwatson */ 3715164032Srwatson case PRIV_KTRACE: 3716164032Srwatson 3717166827Srwatson#if 0 3718164032Srwatson /* 3719164032Srwatson * Allow jailed processes to configure audit identity and 3720164032Srwatson * submit audit records (login, etc). In the future we may 3721164032Srwatson * want to further refine the relationship between audit and 3722164032Srwatson * jail. 3723164032Srwatson */ 3724164032Srwatson case PRIV_AUDIT_GETAUDIT: 3725164032Srwatson case PRIV_AUDIT_SETAUDIT: 3726164032Srwatson case PRIV_AUDIT_SUBMIT: 3727166827Srwatson#endif 3728164032Srwatson 3729164032Srwatson /* 3730164032Srwatson * Allow jailed processes to manipulate process UNIX 3731164032Srwatson * credentials in any way they see fit. 3732164032Srwatson */ 3733164032Srwatson case PRIV_CRED_SETUID: 3734164032Srwatson case PRIV_CRED_SETEUID: 3735164032Srwatson case PRIV_CRED_SETGID: 3736164032Srwatson case PRIV_CRED_SETEGID: 3737164032Srwatson case PRIV_CRED_SETGROUPS: 3738164032Srwatson case PRIV_CRED_SETREUID: 3739164032Srwatson case PRIV_CRED_SETREGID: 3740164032Srwatson case PRIV_CRED_SETRESUID: 3741164032Srwatson case PRIV_CRED_SETRESGID: 3742164032Srwatson 3743164032Srwatson /* 3744164032Srwatson * Jail implements visibility constraints already, so allow 3745164032Srwatson * jailed root to override uid/gid-based constraints. 3746164032Srwatson */ 3747164032Srwatson case PRIV_SEEOTHERGIDS: 3748164032Srwatson case PRIV_SEEOTHERUIDS: 3749164032Srwatson 3750164032Srwatson /* 3751164032Srwatson * Jail implements inter-process debugging limits already, so 3752164032Srwatson * allow jailed root various debugging privileges. 3753164032Srwatson */ 3754164032Srwatson case PRIV_DEBUG_DIFFCRED: 3755164032Srwatson case PRIV_DEBUG_SUGID: 3756164032Srwatson case PRIV_DEBUG_UNPRIV: 3757164032Srwatson 3758164032Srwatson /* 3759164032Srwatson * Allow jail to set various resource limits and login 3760164032Srwatson * properties, and for now, exceed process resource limits. 3761164032Srwatson */ 3762164032Srwatson case PRIV_PROC_LIMIT: 3763164032Srwatson case PRIV_PROC_SETLOGIN: 3764164032Srwatson case PRIV_PROC_SETRLIMIT: 3765164032Srwatson 3766164032Srwatson /* 3767164032Srwatson * System V and POSIX IPC privileges are granted in jail. 3768164032Srwatson */ 3769164032Srwatson case PRIV_IPC_READ: 3770164032Srwatson case PRIV_IPC_WRITE: 3771164032Srwatson case PRIV_IPC_ADMIN: 3772164032Srwatson case PRIV_IPC_MSGSIZE: 3773164032Srwatson case PRIV_MQ_ADMIN: 3774164032Srwatson 3775164032Srwatson /* 3776192895Sjamie * Jail operations within a jail work on child jails. 3777192895Sjamie */ 3778192895Sjamie case PRIV_JAIL_ATTACH: 3779192895Sjamie case PRIV_JAIL_SET: 3780192895Sjamie case PRIV_JAIL_REMOVE: 3781192895Sjamie 3782192895Sjamie /* 3783164032Srwatson * Jail implements its own inter-process limits, so allow 3784164032Srwatson * root processes in jail to change scheduling on other 3785164032Srwatson * processes in the same jail. Likewise for signalling. 3786164032Srwatson */ 3787164032Srwatson case PRIV_SCHED_DIFFCRED: 3788185435Sbz case PRIV_SCHED_CPUSET: 3789164032Srwatson case PRIV_SIGNAL_DIFFCRED: 3790164032Srwatson case PRIV_SIGNAL_SUGID: 3791164032Srwatson 3792164032Srwatson /* 3793164032Srwatson * Allow jailed processes to write to sysctls marked as jail 3794164032Srwatson * writable. 3795164032Srwatson */ 3796164032Srwatson case PRIV_SYSCTL_WRITEJAIL: 3797164032Srwatson 3798164032Srwatson /* 3799164032Srwatson * Allow root in jail to manage a variety of quota 3800166831Srwatson * properties. These should likely be conditional on a 3801166831Srwatson * configuration option. 3802164032Srwatson */ 3803166832Srwatson case PRIV_VFS_GETQUOTA: 3804166832Srwatson case PRIV_VFS_SETQUOTA: 3805164032Srwatson 3806164032Srwatson /* 3807164032Srwatson * Since Jail relies on chroot() to implement file system 3808164032Srwatson * protections, grant many VFS privileges to root in jail. 3809164032Srwatson * Be careful to exclude mount-related and NFS-related 3810164032Srwatson * privileges. 3811164032Srwatson */ 3812164032Srwatson case PRIV_VFS_READ: 3813164032Srwatson case PRIV_VFS_WRITE: 3814164032Srwatson case PRIV_VFS_ADMIN: 3815164032Srwatson case PRIV_VFS_EXEC: 3816164032Srwatson case PRIV_VFS_LOOKUP: 3817164032Srwatson case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */ 3818164032Srwatson case PRIV_VFS_CHFLAGS_DEV: 3819164032Srwatson case PRIV_VFS_CHOWN: 3820164032Srwatson case PRIV_VFS_CHROOT: 3821167152Spjd case PRIV_VFS_RETAINSUGID: 3822164032Srwatson case PRIV_VFS_FCHROOT: 3823164032Srwatson case PRIV_VFS_LINK: 3824164032Srwatson case PRIV_VFS_SETGID: 3825172860Srwatson case PRIV_VFS_STAT: 3826164032Srwatson case PRIV_VFS_STICKYFILE: 3827164032Srwatson return (0); 3828164032Srwatson 3829164032Srwatson /* 3830164032Srwatson * Depending on the global setting, allow privilege of 3831164032Srwatson * setting system flags. 3832164032Srwatson */ 3833164032Srwatson case PRIV_VFS_SYSFLAGS: 3834192895Sjamie if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS) 3835164032Srwatson return (0); 3836164032Srwatson else 3837164032Srwatson return (EPERM); 3838164032Srwatson 3839164032Srwatson /* 3840168396Spjd * Depending on the global setting, allow privilege of 3841168396Spjd * mounting/unmounting file systems. 3842168396Spjd */ 3843168396Spjd case PRIV_VFS_MOUNT: 3844168396Spjd case PRIV_VFS_UNMOUNT: 3845168396Spjd case PRIV_VFS_MOUNT_NONUSER: 3846168699Spjd case PRIV_VFS_MOUNT_OWNER: 3847192895Sjamie if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT) 3848168396Spjd return (0); 3849168396Spjd else 3850168396Spjd return (EPERM); 3851168396Spjd 3852168396Spjd /* 3853168591Srwatson * Allow jailed root to bind reserved ports and reuse in-use 3854168591Srwatson * ports. 3855164032Srwatson */ 3856164032Srwatson case PRIV_NETINET_RESERVEDPORT: 3857168591Srwatson case PRIV_NETINET_REUSEPORT: 3858164032Srwatson return (0); 3859164032Srwatson 3860164032Srwatson /* 3861175630Sbz * Allow jailed root to set certian IPv4/6 (option) headers. 3862175630Sbz */ 3863175630Sbz case PRIV_NETINET_SETHDROPTS: 3864175630Sbz return (0); 3865175630Sbz 3866175630Sbz /* 3867164032Srwatson * Conditionally allow creating raw sockets in jail. 3868164032Srwatson */ 3869164032Srwatson case PRIV_NETINET_RAW: 3870192895Sjamie if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS) 3871164032Srwatson return (0); 3872164032Srwatson else 3873164032Srwatson return (EPERM); 3874164032Srwatson 3875164032Srwatson /* 3876164032Srwatson * Since jail implements its own visibility limits on netstat 3877164032Srwatson * sysctls, allow getcred. This allows identd to work in 3878164032Srwatson * jail. 3879164032Srwatson */ 3880164032Srwatson case PRIV_NETINET_GETCRED: 3881164032Srwatson return (0); 3882164032Srwatson 3883219304Strasz /* 3884219304Strasz * Allow jailed root to set loginclass. 3885219304Strasz */ 3886219304Strasz case PRIV_PROC_SETLOGINCLASS: 3887219304Strasz return (0); 3888219304Strasz 3889164032Srwatson default: 3890164032Srwatson /* 3891164032Srwatson * In all remaining cases, deny the privilege request. This 3892164032Srwatson * includes almost all network privileges, many system 3893164032Srwatson * configuration privileges. 3894164032Srwatson */ 3895164032Srwatson return (EPERM); 3896164032Srwatson } 3897164032Srwatson} 3898164032Srwatson 3899192895Sjamie/* 3900192895Sjamie * Return the part of pr2's name that is relative to pr1, or the whole name 3901192895Sjamie * if it does not directly follow. 3902192895Sjamie */ 3903192895Sjamie 3904192895Sjamiechar * 3905192895Sjamieprison_name(struct prison *pr1, struct prison *pr2) 3906192895Sjamie{ 3907192895Sjamie char *name; 3908192895Sjamie 3909192895Sjamie /* Jails see themselves as "0" (if they see themselves at all). */ 3910192895Sjamie if (pr1 == pr2) 3911192895Sjamie return "0"; 3912192895Sjamie name = pr2->pr_name; 3913192895Sjamie if (prison_ischild(pr1, pr2)) { 3914192895Sjamie /* 3915192895Sjamie * pr1 isn't locked (and allprison_lock may not be either) 3916192895Sjamie * so its length can't be counted on. But the number of dots 3917192895Sjamie * can be counted on - and counted. 3918192895Sjamie */ 3919192895Sjamie for (; pr1 != &prison0; pr1 = pr1->pr_parent) 3920192895Sjamie name = strchr(name, '.') + 1; 3921192895Sjamie } 3922192895Sjamie return (name); 3923192895Sjamie} 3924192895Sjamie 3925192895Sjamie/* 3926192895Sjamie * Return the part of pr2's path that is relative to pr1, or the whole path 3927192895Sjamie * if it does not directly follow. 3928192895Sjamie */ 3929192895Sjamiestatic char * 3930192895Sjamieprison_path(struct prison *pr1, struct prison *pr2) 3931192895Sjamie{ 3932192895Sjamie char *path1, *path2; 3933192895Sjamie int len1; 3934192895Sjamie 3935192895Sjamie path1 = pr1->pr_path; 3936192895Sjamie path2 = pr2->pr_path; 3937192895Sjamie if (!strcmp(path1, "/")) 3938192895Sjamie return (path2); 3939192895Sjamie len1 = strlen(path1); 3940192895Sjamie if (strncmp(path1, path2, len1)) 3941192895Sjamie return (path2); 3942192895Sjamie if (path2[len1] == '\0') 3943192895Sjamie return "/"; 3944192895Sjamie if (path2[len1] == '/') 3945192895Sjamie return (path2 + len1); 3946192895Sjamie return (path2); 3947192895Sjamie} 3948192895Sjamie 3949192895Sjamie 3950192895Sjamie/* 3951192895Sjamie * Jail-related sysctls. 3952192895Sjamie */ 3953192895SjamieSYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0, 3954192895Sjamie "Jails"); 3955192895Sjamie 3956113275Smikestatic int 3957113275Smikesysctl_jail_list(SYSCTL_HANDLER_ARGS) 3958113275Smike{ 3959191673Sjamie struct xprison *xp; 3960192895Sjamie struct prison *pr, *cpr; 3961191673Sjamie#ifdef INET 3962191673Sjamie struct in_addr *ip4 = NULL; 3963191673Sjamie int ip4s = 0; 3964191673Sjamie#endif 3965191673Sjamie#ifdef INET6 3966208803Scperciva struct in6_addr *ip6 = NULL; 3967191673Sjamie int ip6s = 0; 3968191673Sjamie#endif 3969192895Sjamie int descend, error; 3970113275Smike 3971191673Sjamie xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK); 3972192895Sjamie pr = req->td->td_ucred->cr_prison; 3973191673Sjamie error = 0; 3974168401Spjd sx_slock(&allprison_lock); 3975192895Sjamie FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 3976192895Sjamie#if defined(INET) || defined(INET6) 3977191673Sjamie again: 3978192895Sjamie#endif 3979192895Sjamie mtx_lock(&cpr->pr_mtx); 3980185435Sbz#ifdef INET 3981192895Sjamie if (cpr->pr_ip4s > 0) { 3982192895Sjamie if (ip4s < cpr->pr_ip4s) { 3983192895Sjamie ip4s = cpr->pr_ip4s; 3984192895Sjamie mtx_unlock(&cpr->pr_mtx); 3985191673Sjamie ip4 = realloc(ip4, ip4s * 3986191673Sjamie sizeof(struct in_addr), M_TEMP, M_WAITOK); 3987191673Sjamie goto again; 3988191673Sjamie } 3989192895Sjamie bcopy(cpr->pr_ip4, ip4, 3990192895Sjamie cpr->pr_ip4s * sizeof(struct in_addr)); 3991191673Sjamie } 3992185435Sbz#endif 3993185435Sbz#ifdef INET6 3994192895Sjamie if (cpr->pr_ip6s > 0) { 3995192895Sjamie if (ip6s < cpr->pr_ip6s) { 3996192895Sjamie ip6s = cpr->pr_ip6s; 3997192895Sjamie mtx_unlock(&cpr->pr_mtx); 3998191673Sjamie ip6 = realloc(ip6, ip6s * 3999191673Sjamie sizeof(struct in6_addr), M_TEMP, M_WAITOK); 4000191673Sjamie goto again; 4001191673Sjamie } 4002192895Sjamie bcopy(cpr->pr_ip6, ip6, 4003192895Sjamie cpr->pr_ip6s * sizeof(struct in6_addr)); 4004191673Sjamie } 4005185435Sbz#endif 4006192895Sjamie if (cpr->pr_ref == 0) { 4007192895Sjamie mtx_unlock(&cpr->pr_mtx); 4008191673Sjamie continue; 4009191673Sjamie } 4010191673Sjamie bzero(xp, sizeof(*xp)); 4011113275Smike xp->pr_version = XPRISON_VERSION; 4012192895Sjamie xp->pr_id = cpr->pr_id; 4013192895Sjamie xp->pr_state = cpr->pr_uref > 0 4014191673Sjamie ? PRISON_STATE_ALIVE : PRISON_STATE_DYING; 4015192895Sjamie strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path)); 4016194118Sjamie strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host)); 4017192895Sjamie strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name)); 4018185435Sbz#ifdef INET 4019192895Sjamie xp->pr_ip4s = cpr->pr_ip4s; 4020185435Sbz#endif 4021185435Sbz#ifdef INET6 4022192895Sjamie xp->pr_ip6s = cpr->pr_ip6s; 4023185435Sbz#endif 4024192895Sjamie mtx_unlock(&cpr->pr_mtx); 4025191673Sjamie error = SYSCTL_OUT(req, xp, sizeof(*xp)); 4026191673Sjamie if (error) 4027191673Sjamie break; 4028185435Sbz#ifdef INET 4029191673Sjamie if (xp->pr_ip4s > 0) { 4030191673Sjamie error = SYSCTL_OUT(req, ip4, 4031191673Sjamie xp->pr_ip4s * sizeof(struct in_addr)); 4032191673Sjamie if (error) 4033191673Sjamie break; 4034185435Sbz } 4035185435Sbz#endif 4036185435Sbz#ifdef INET6 4037191673Sjamie if (xp->pr_ip6s > 0) { 4038191673Sjamie error = SYSCTL_OUT(req, ip6, 4039191673Sjamie xp->pr_ip6s * sizeof(struct in6_addr)); 4040191673Sjamie if (error) 4041191673Sjamie break; 4042185435Sbz } 4043185435Sbz#endif 4044113275Smike } 4045168401Spjd sx_sunlock(&allprison_lock); 4046191673Sjamie free(xp, M_TEMP); 4047191673Sjamie#ifdef INET 4048191673Sjamie free(ip4, M_TEMP); 4049191673Sjamie#endif 4050191673Sjamie#ifdef INET6 4051191673Sjamie free(ip6, M_TEMP); 4052191673Sjamie#endif 4053167354Spjd return (error); 4054113275Smike} 4055113275Smike 4056187864SedSYSCTL_OID(_security_jail, OID_AUTO, list, 4057187864Sed CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4058187864Sed sysctl_jail_list, "S", "List of active jails"); 4059126004Spjd 4060126004Spjdstatic int 4061126004Spjdsysctl_jail_jailed(SYSCTL_HANDLER_ARGS) 4062126004Spjd{ 4063126004Spjd int error, injail; 4064126004Spjd 4065126004Spjd injail = jailed(req->td->td_ucred); 4066126004Spjd error = SYSCTL_OUT(req, &injail, sizeof(injail)); 4067126004Spjd 4068126004Spjd return (error); 4069126004Spjd} 4070192895Sjamie 4071187864SedSYSCTL_PROC(_security_jail, OID_AUTO, jailed, 4072187864Sed CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4073187864Sed sysctl_jail_jailed, "I", "Process in jail?"); 4074185435Sbz 4075192895Sjamie#if defined(INET) || defined(INET6) 4076193865SjamieSYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW, 4077192895Sjamie &jail_max_af_ips, 0, 4078192895Sjamie "Number of IP addresses a jail may have at most per address family"); 4079192895Sjamie#endif 4080192895Sjamie 4081192895Sjamie/* 4082192895Sjamie * Default parameters for jail(2) compatability. For historical reasons, 4083192895Sjamie * the sysctl names have varying similarity to the parameter names. Prisons 4084192895Sjamie * just see their own parameters, and can't change them. 4085192895Sjamie */ 4086192895Sjamiestatic int 4087192895Sjamiesysctl_jail_default_allow(SYSCTL_HANDLER_ARGS) 4088192895Sjamie{ 4089192895Sjamie struct prison *pr; 4090192895Sjamie int allow, error, i; 4091192895Sjamie 4092192895Sjamie pr = req->td->td_ucred->cr_prison; 4093192895Sjamie allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow; 4094192895Sjamie 4095192895Sjamie /* Get the current flag value, and convert it to a boolean. */ 4096192895Sjamie i = (allow & arg2) ? 1 : 0; 4097192895Sjamie if (arg1 != NULL) 4098192895Sjamie i = !i; 4099192895Sjamie error = sysctl_handle_int(oidp, &i, 0, req); 4100192895Sjamie if (error || !req->newptr) 4101192895Sjamie return (error); 4102192895Sjamie i = i ? arg2 : 0; 4103192895Sjamie if (arg1 != NULL) 4104192895Sjamie i ^= arg2; 4105192895Sjamie /* 4106192895Sjamie * The sysctls don't have CTLFLAGS_PRISON, so assume prison0 4107192895Sjamie * for writing. 4108192895Sjamie */ 4109192895Sjamie mtx_lock(&prison0.pr_mtx); 4110192895Sjamie jail_default_allow = (jail_default_allow & ~arg2) | i; 4111192895Sjamie mtx_unlock(&prison0.pr_mtx); 4112192895Sjamie return (0); 4113192895Sjamie} 4114192895Sjamie 4115192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed, 4116192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4117192895Sjamie NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I", 4118192895Sjamie "Processes in jail can set their hostnames"); 4119192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only, 4120192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4121192895Sjamie (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I", 4122192895Sjamie "Processes in jail are limited to creating UNIX/IP/route sockets only"); 4123192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed, 4124192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4125192895Sjamie NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I", 4126192895Sjamie "Processes in jail can use System V IPC primitives"); 4127192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets, 4128192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4129192895Sjamie NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I", 4130192895Sjamie "Prison root can create raw sockets"); 4131192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed, 4132192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4133192895Sjamie NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I", 4134192895Sjamie "Processes in jail can alter system file flags"); 4135192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed, 4136192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4137192895Sjamie NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I", 4138192895Sjamie "Processes in jail can mount/unmount jail-friendly file systems"); 4139192895Sjamie 4140192895Sjamiestatic int 4141192895Sjamiesysctl_jail_default_level(SYSCTL_HANDLER_ARGS) 4142192895Sjamie{ 4143192895Sjamie struct prison *pr; 4144192895Sjamie int level, error; 4145192895Sjamie 4146192895Sjamie pr = req->td->td_ucred->cr_prison; 4147192895Sjamie level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2); 4148192895Sjamie error = sysctl_handle_int(oidp, &level, 0, req); 4149192895Sjamie if (error || !req->newptr) 4150192895Sjamie return (error); 4151192895Sjamie *(int *)arg1 = level; 4152192895Sjamie return (0); 4153192895Sjamie} 4154192895Sjamie 4155192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs, 4156192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4157192895Sjamie &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs), 4158192895Sjamie sysctl_jail_default_level, "I", 4159192895Sjamie "Processes in jail cannot see all mounted file systems"); 4160192895Sjamie 4161192895Sjamie/* 4162192895Sjamie * Nodes to describe jail parameters. Maximum length of string parameters 4163192895Sjamie * is returned in the string itself, and the other parameters exist merely 4164192895Sjamie * to make themselves and their types known. 4165192895Sjamie */ 4166192895SjamieSYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0, 4167192895Sjamie "Jail parameters"); 4168192895Sjamie 4169192895Sjamieint 4170192895Sjamiesysctl_jail_param(SYSCTL_HANDLER_ARGS) 4171192895Sjamie{ 4172192895Sjamie int i; 4173192895Sjamie long l; 4174192895Sjamie size_t s; 4175192895Sjamie char numbuf[12]; 4176192895Sjamie 4177192895Sjamie switch (oidp->oid_kind & CTLTYPE) 4178192895Sjamie { 4179192895Sjamie case CTLTYPE_LONG: 4180192895Sjamie case CTLTYPE_ULONG: 4181192895Sjamie l = 0; 4182192895Sjamie#ifdef SCTL_MASK32 4183192895Sjamie if (!(req->flags & SCTL_MASK32)) 4184192895Sjamie#endif 4185192895Sjamie return (SYSCTL_OUT(req, &l, sizeof(l))); 4186192895Sjamie case CTLTYPE_INT: 4187192895Sjamie case CTLTYPE_UINT: 4188192895Sjamie i = 0; 4189192895Sjamie return (SYSCTL_OUT(req, &i, sizeof(i))); 4190192895Sjamie case CTLTYPE_STRING: 4191219819Sjeff snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2); 4192192895Sjamie return 4193192895Sjamie (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req)); 4194192895Sjamie case CTLTYPE_STRUCT: 4195192895Sjamie s = (size_t)arg2; 4196192895Sjamie return (SYSCTL_OUT(req, &s, sizeof(s))); 4197192895Sjamie } 4198192895Sjamie return (0); 4199192895Sjamie} 4200192895Sjamie 4201192895SjamieSYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID"); 4202192895SjamieSYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID"); 4203192895SjamieSYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name"); 4204192895SjamieSYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path"); 4205192895SjamieSYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW, 4206192895Sjamie "I", "Jail secure level"); 4207192895SjamieSYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW, 4208192895Sjamie "I", "Jail cannot see all mounted file systems"); 4209192895SjamieSYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW, 4210192895Sjamie "B", "Jail persistence"); 4211194251Sjamie#ifdef VIMAGE 4212194251SjamieSYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN, 4213195870Sjamie "E,jailsys", "Virtual network stack"); 4214194251Sjamie#endif 4215192895SjamieSYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD, 4216192895Sjamie "B", "Jail is in the process of shutting down"); 4217192895Sjamie 4218194762SjamieSYSCTL_JAIL_PARAM_NODE(children, "Number of child jails"); 4219194762SjamieSYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD, 4220194762Sjamie "I", "Current number of child jails"); 4221194762SjamieSYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW, 4222194762Sjamie "I", "Maximum number of child jails"); 4223194762Sjamie 4224195870SjamieSYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info"); 4225192895SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN, 4226192895Sjamie "Jail hostname"); 4227193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN, 4228193066Sjamie "Jail NIS domainname"); 4229193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN, 4230193066Sjamie "Jail host UUID"); 4231193066SjamieSYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW, 4232193066Sjamie "LU", "Jail host ID"); 4233192895Sjamie 4234192895SjamieSYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset"); 4235192895SjamieSYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID"); 4236192895Sjamie 4237192895Sjamie#ifdef INET 4238195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN, 4239195974Sjamie "Jail IPv4 address virtualization"); 4240192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr), 4241192895Sjamie "S,in_addr,a", "Jail IPv4 addresses"); 4242202468SbzSYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4243202468Sbz "B", "Do (not) use IPv4 source address selection rather than the " 4244202468Sbz "primary jail IPv4 address."); 4245192895Sjamie#endif 4246192895Sjamie#ifdef INET6 4247195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN, 4248195974Sjamie "Jail IPv6 address virtualization"); 4249192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr), 4250192895Sjamie "S,in6_addr,a", "Jail IPv6 addresses"); 4251202468SbzSYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4252202468Sbz "B", "Do (not) use IPv6 source address selection rather than the " 4253202468Sbz "primary jail IPv6 address."); 4254192895Sjamie#endif 4255192895Sjamie 4256192895SjamieSYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags"); 4257192895SjamieSYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW, 4258192895Sjamie "B", "Jail may set hostname"); 4259192895SjamieSYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW, 4260192895Sjamie "B", "Jail may use SYSV IPC"); 4261192895SjamieSYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW, 4262192895Sjamie "B", "Jail may create raw sockets"); 4263192895SjamieSYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW, 4264192895Sjamie "B", "Jail may alter system file flags"); 4265192895SjamieSYSCTL_JAIL_PARAM(_allow, mount, CTLTYPE_INT | CTLFLAG_RW, 4266192895Sjamie "B", "Jail may mount/unmount jail-friendly file systems"); 4267192895SjamieSYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW, 4268192895Sjamie "B", "Jail may set file quotas"); 4269192895SjamieSYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW, 4270192895Sjamie "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route"); 4271192895Sjamie 4272220137Straszvoid 4273220137Straszprison_racct_foreach(void (*callback)(struct racct *racct, 4274220137Strasz void *arg2, void *arg3), void *arg2, void *arg3) 4275220137Strasz{ 4276220137Strasz struct prison *pr; 4277192895Sjamie 4278220137Strasz sx_slock(&allprison_lock); 4279220137Strasz TAILQ_FOREACH(pr, &allprison, pr_list) 4280220137Strasz (callback)(pr->pr_racct, arg2, arg3); 4281220137Strasz sx_sunlock(&allprison_lock); 4282220137Strasz} 4283220137Strasz 4284185435Sbz#ifdef DDB 4285191673Sjamie 4286191673Sjamiestatic void 4287191673Sjamiedb_show_prison(struct prison *pr) 4288185435Sbz{ 4289192895Sjamie int fi; 4290191673Sjamie#if defined(INET) || defined(INET6) 4291191673Sjamie int ii; 4292185435Sbz#endif 4293195870Sjamie unsigned jsf; 4294185435Sbz#ifdef INET6 4295185435Sbz char ip6buf[INET6_ADDRSTRLEN]; 4296185435Sbz#endif 4297185435Sbz 4298191673Sjamie db_printf("prison %p:\n", pr); 4299191673Sjamie db_printf(" jid = %d\n", pr->pr_id); 4300191673Sjamie db_printf(" name = %s\n", pr->pr_name); 4301192895Sjamie db_printf(" parent = %p\n", pr->pr_parent); 4302191673Sjamie db_printf(" ref = %d\n", pr->pr_ref); 4303191673Sjamie db_printf(" uref = %d\n", pr->pr_uref); 4304191673Sjamie db_printf(" path = %s\n", pr->pr_path); 4305191673Sjamie db_printf(" cpuset = %d\n", pr->pr_cpuset 4306191673Sjamie ? pr->pr_cpuset->cs_id : -1); 4307194251Sjamie#ifdef VIMAGE 4308194251Sjamie db_printf(" vnet = %p\n", pr->pr_vnet); 4309194251Sjamie#endif 4310191673Sjamie db_printf(" root = %p\n", pr->pr_root); 4311191673Sjamie db_printf(" securelevel = %d\n", pr->pr_securelevel); 4312202123Sbz db_printf(" children.max = %d\n", pr->pr_childmax); 4313202123Sbz db_printf(" children.cur = %d\n", pr->pr_childcount); 4314192895Sjamie db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children)); 4315192895Sjamie db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling)); 4316202123Sbz db_printf(" flags = 0x%x", pr->pr_flags); 4317192895Sjamie for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 4318192895Sjamie fi++) 4319192895Sjamie if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi))) 4320192895Sjamie db_printf(" %s", pr_flag_names[fi]); 4321195870Sjamie for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 4322195870Sjamie fi++) { 4323195870Sjamie jsf = pr->pr_flags & 4324195870Sjamie (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new); 4325195870Sjamie db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name, 4326195870Sjamie pr_flag_jailsys[fi].disable && 4327195870Sjamie (jsf == pr_flag_jailsys[fi].disable) ? "disable" 4328195870Sjamie : (jsf == pr_flag_jailsys[fi].new) ? "new" 4329195870Sjamie : "inherit"); 4330195870Sjamie } 4331202123Sbz db_printf(" allow = 0x%x", pr->pr_allow); 4332192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 4333192895Sjamie fi++) 4334192895Sjamie if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi))) 4335192895Sjamie db_printf(" %s", pr_allow_names[fi]); 4336191673Sjamie db_printf("\n"); 4337192895Sjamie db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs); 4338194118Sjamie db_printf(" host.hostname = %s\n", pr->pr_hostname); 4339194118Sjamie db_printf(" host.domainname = %s\n", pr->pr_domainname); 4340194118Sjamie db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid); 4341193066Sjamie db_printf(" host.hostid = %lu\n", pr->pr_hostid); 4342185435Sbz#ifdef INET 4343191673Sjamie db_printf(" ip4s = %d\n", pr->pr_ip4s); 4344191673Sjamie for (ii = 0; ii < pr->pr_ip4s; ii++) 4345191673Sjamie db_printf(" %s %s\n", 4346202123Sbz ii == 0 ? "ip4.addr =" : " ", 4347191673Sjamie inet_ntoa(pr->pr_ip4[ii])); 4348185435Sbz#endif 4349185435Sbz#ifdef INET6 4350191673Sjamie db_printf(" ip6s = %d\n", pr->pr_ip6s); 4351191673Sjamie for (ii = 0; ii < pr->pr_ip6s; ii++) 4352191673Sjamie db_printf(" %s %s\n", 4353202123Sbz ii == 0 ? "ip6.addr =" : " ", 4354191673Sjamie ip6_sprintf(ip6buf, &pr->pr_ip6[ii])); 4355191673Sjamie#endif 4356191673Sjamie} 4357191673Sjamie 4358191673SjamieDB_SHOW_COMMAND(prison, db_show_prison_command) 4359191673Sjamie{ 4360191673Sjamie struct prison *pr; 4361191673Sjamie 4362191673Sjamie if (!have_addr) { 4363192895Sjamie /* 4364192895Sjamie * Show all prisons in the list, and prison0 which is not 4365192895Sjamie * listed. 4366192895Sjamie */ 4367192895Sjamie db_show_prison(&prison0); 4368192895Sjamie if (!db_pager_quit) { 4369192895Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) { 4370192895Sjamie db_show_prison(pr); 4371192895Sjamie if (db_pager_quit) 4372192895Sjamie break; 4373192895Sjamie } 4374191673Sjamie } 4375191673Sjamie return; 4376191673Sjamie } 4377191673Sjamie 4378192895Sjamie if (addr == 0) 4379192895Sjamie pr = &prison0; 4380192895Sjamie else { 4381192895Sjamie /* Look for a prison with the ID and with references. */ 4382191673Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) 4383192895Sjamie if (pr->pr_id == addr && pr->pr_ref > 0) 4384191673Sjamie break; 4385192895Sjamie if (pr == NULL) 4386192895Sjamie /* Look again, without requiring a reference. */ 4387192895Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) 4388192895Sjamie if (pr->pr_id == addr) 4389192895Sjamie break; 4390192895Sjamie if (pr == NULL) 4391192895Sjamie /* Assume address points to a valid prison. */ 4392192895Sjamie pr = (struct prison *)addr; 4393192895Sjamie } 4394191673Sjamie db_show_prison(pr); 4395185435Sbz} 4396191673Sjamie 4397185435Sbz#endif /* DDB */ 4398