kern_jail.c revision 202116
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 202116 2010-01-11 21:21:30Z bz $"); 31116182Sobrien 32193066Sjamie#include "opt_compat.h" 33185435Sbz#include "opt_ddb.h" 34185435Sbz#include "opt_inet.h" 35185435Sbz#include "opt_inet6.h" 36131177Spjd 3746155Sphk#include <sys/param.h> 3846155Sphk#include <sys/types.h> 3946155Sphk#include <sys/kernel.h> 4046155Sphk#include <sys/systm.h> 4146155Sphk#include <sys/errno.h> 4246155Sphk#include <sys/sysproto.h> 4346155Sphk#include <sys/malloc.h> 44192895Sjamie#include <sys/osd.h> 45164032Srwatson#include <sys/priv.h> 4646155Sphk#include <sys/proc.h> 47124882Srwatson#include <sys/taskqueue.h> 48177785Skib#include <sys/fcntl.h> 4946155Sphk#include <sys/jail.h> 5087275Srwatson#include <sys/lock.h> 5187275Srwatson#include <sys/mutex.h> 52168401Spjd#include <sys/sx.h> 53193066Sjamie#include <sys/sysent.h> 54113275Smike#include <sys/namei.h> 55147185Spjd#include <sys/mount.h> 56113275Smike#include <sys/queue.h> 5746155Sphk#include <sys/socket.h> 58113275Smike#include <sys/syscallsubr.h> 5957163Srwatson#include <sys/sysctl.h> 60113275Smike#include <sys/vnode.h> 61196019Srwatson 6246155Sphk#include <net/if.h> 63196019Srwatson#include <net/vnet.h> 64196019Srwatson 6546155Sphk#include <netinet/in.h> 66196019Srwatson 67185435Sbz#ifdef DDB 68185435Sbz#include <ddb/ddb.h> 69185435Sbz#ifdef INET6 70185435Sbz#include <netinet6/in6_var.h> 71185435Sbz#endif /* INET6 */ 72185435Sbz#endif /* DDB */ 7346155Sphk 74163606Srwatson#include <security/mac/mac_framework.h> 75163606Srwatson 76195944Sjamie#define DEFAULT_HOSTUUID "00000000-0000-0000-0000-000000000000" 77195944Sjamie 7846155SphkMALLOC_DEFINE(M_PRISON, "prison", "Prison structures"); 7946155Sphk 80192895Sjamie/* prison0 describes what is "real" about the system. */ 81192895Sjamiestruct prison prison0 = { 82192895Sjamie .pr_id = 0, 83192895Sjamie .pr_name = "0", 84192895Sjamie .pr_ref = 1, 85192895Sjamie .pr_uref = 1, 86192895Sjamie .pr_path = "/", 87192895Sjamie .pr_securelevel = -1, 88194762Sjamie .pr_childmax = JAIL_MAX, 89195944Sjamie .pr_hostuuid = DEFAULT_HOSTUUID, 90201145Santoine .pr_children = LIST_HEAD_INITIALIZER(prison0.pr_children), 91196176Sbz#ifdef VIMAGE 92196176Sbz .pr_flags = PR_HOST|PR_VNET, 93196176Sbz#else 94193066Sjamie .pr_flags = PR_HOST, 95196176Sbz#endif 96192895Sjamie .pr_allow = PR_ALLOW_ALL, 97192895Sjamie}; 98192895SjamieMTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF); 9957163Srwatson 100192895Sjamie/* allprison and lastprid are protected by allprison_lock. */ 101168401Spjdstruct sx allprison_lock; 102191673SjamieSX_SYSINIT(allprison_lock, &allprison_lock, "allprison"); 103191673Sjamiestruct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison); 104179881Sdelphijint lastprid = 0; 105113275Smike 106191673Sjamiestatic int do_jail_attach(struct thread *td, struct prison *pr); 107190466Sjamiestatic void prison_complete(void *context, int pending); 108191673Sjamiestatic void prison_deref(struct prison *pr, int flags); 109192895Sjamiestatic char *prison_path(struct prison *pr1, struct prison *pr2); 110192895Sjamiestatic void prison_remove_one(struct prison *pr); 111185435Sbz#ifdef INET 112190466Sjamiestatic int _prison_check_ip4(struct prison *pr, struct in_addr *ia); 113192895Sjamiestatic int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4); 114185435Sbz#endif 115185435Sbz#ifdef INET6 116190466Sjamiestatic int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6); 117192895Sjamiestatic int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6); 118185435Sbz#endif 119113275Smike 120191673Sjamie/* Flags for prison_deref */ 121191673Sjamie#define PD_DEREF 0x01 122191673Sjamie#define PD_DEUREF 0x02 123191673Sjamie#define PD_LOCKED 0x04 124191673Sjamie#define PD_LIST_SLOCKED 0x08 125191673Sjamie#define PD_LIST_XLOCKED 0x10 126113275Smike 127192895Sjamie/* 128192895Sjamie * Parameter names corresponding to PR_* flag values 129192895Sjamie */ 130192895Sjamiestatic char *pr_flag_names[] = { 131192895Sjamie [0] = "persist", 132192895Sjamie}; 133192895Sjamie 134192895Sjamiestatic char *pr_flag_nonames[] = { 135192895Sjamie [0] = "nopersist", 136195870Sjamie}; 137195870Sjamie 138195870Sjamiestruct jailsys_flags { 139195870Sjamie const char *name; 140195870Sjamie unsigned disable; 141195870Sjamie unsigned new; 142195870Sjamie} pr_flag_jailsys[] = { 143195870Sjamie { "host", 0, PR_HOST }, 144195870Sjamie#ifdef VIMAGE 145195870Sjamie { "vnet", 0, PR_VNET }, 146195870Sjamie#endif 147192895Sjamie#ifdef INET 148195870Sjamie { "ip4", PR_IP4_USER | PR_IP4_DISABLE, PR_IP4_USER }, 149192895Sjamie#endif 150192895Sjamie#ifdef INET6 151195870Sjamie { "ip6", PR_IP6_USER | PR_IP6_DISABLE, PR_IP6_USER }, 152192895Sjamie#endif 153192895Sjamie}; 154192895Sjamie 155192895Sjamiestatic char *pr_allow_names[] = { 156192895Sjamie "allow.set_hostname", 157192895Sjamie "allow.sysvipc", 158192895Sjamie "allow.raw_sockets", 159192895Sjamie "allow.chflags", 160192895Sjamie "allow.mount", 161192895Sjamie "allow.quotas", 162192895Sjamie "allow.socket_af", 163192895Sjamie}; 164192895Sjamie 165192895Sjamiestatic char *pr_allow_nonames[] = { 166192895Sjamie "allow.noset_hostname", 167192895Sjamie "allow.nosysvipc", 168192895Sjamie "allow.noraw_sockets", 169192895Sjamie "allow.nochflags", 170192895Sjamie "allow.nomount", 171192895Sjamie "allow.noquotas", 172192895Sjamie "allow.nosocket_af", 173192895Sjamie}; 174192895Sjamie 175196002Sjamie#define JAIL_DEFAULT_ALLOW PR_ALLOW_SET_HOSTNAME 176196002Sjamie#define JAIL_DEFAULT_ENFORCE_STATFS 2 177192895Sjamiestatic unsigned jail_default_allow = JAIL_DEFAULT_ALLOW; 178196002Sjamiestatic int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 179192895Sjamie#if defined(INET) || defined(INET6) 180193865Sjamiestatic unsigned jail_max_af_ips = 255; 181192895Sjamie#endif 182192895Sjamie 183192895Sjamie#ifdef INET 184185435Sbzstatic int 185185435Sbzqcmp_v4(const void *ip1, const void *ip2) 186185435Sbz{ 187185435Sbz in_addr_t iaa, iab; 188185435Sbz 189185435Sbz /* 190185435Sbz * We need to compare in HBO here to get the list sorted as expected 191185435Sbz * by the result of the code. Sorting NBO addresses gives you 192185435Sbz * interesting results. If you do not understand, do not try. 193185435Sbz */ 194185435Sbz iaa = ntohl(((const struct in_addr *)ip1)->s_addr); 195185435Sbz iab = ntohl(((const struct in_addr *)ip2)->s_addr); 196185435Sbz 197185435Sbz /* 198185435Sbz * Do not simply return the difference of the two numbers, the int is 199185435Sbz * not wide enough. 200185435Sbz */ 201185435Sbz if (iaa > iab) 202185435Sbz return (1); 203185435Sbz else if (iaa < iab) 204185435Sbz return (-1); 205185435Sbz else 206185435Sbz return (0); 207185435Sbz} 208185435Sbz#endif 209185435Sbz 210185435Sbz#ifdef INET6 211185435Sbzstatic int 212185435Sbzqcmp_v6(const void *ip1, const void *ip2) 213185435Sbz{ 214185435Sbz const struct in6_addr *ia6a, *ia6b; 215185435Sbz int i, rc; 216185435Sbz 217185435Sbz ia6a = (const struct in6_addr *)ip1; 218185435Sbz ia6b = (const struct in6_addr *)ip2; 219185435Sbz 220185435Sbz rc = 0; 221190466Sjamie for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) { 222185435Sbz if (ia6a->s6_addr[i] > ia6b->s6_addr[i]) 223185435Sbz rc = 1; 224185435Sbz else if (ia6a->s6_addr[i] < ia6b->s6_addr[i]) 225185435Sbz rc = -1; 226185435Sbz } 227185435Sbz return (rc); 228185435Sbz} 229185435Sbz#endif 230185435Sbz 231191673Sjamie/* 232191673Sjamie * struct jail_args { 233191673Sjamie * struct jail *jail; 234191673Sjamie * }; 235191673Sjamie */ 236191673Sjamieint 237191673Sjamiejail(struct thread *td, struct jail_args *uap) 238185435Sbz{ 239191673Sjamie uint32_t version; 240191673Sjamie int error; 241192895Sjamie struct jail j; 242185435Sbz 243191673Sjamie error = copyin(uap->jail, &version, sizeof(uint32_t)); 244191673Sjamie if (error) 245191673Sjamie return (error); 246185435Sbz 247191673Sjamie switch (version) { 248191673Sjamie case 0: 249191673Sjamie { 250191673Sjamie struct jail_v0 j0; 251185435Sbz 252192895Sjamie /* FreeBSD single IPv4 jails. */ 253192895Sjamie bzero(&j, sizeof(struct jail)); 254191673Sjamie error = copyin(uap->jail, &j0, sizeof(struct jail_v0)); 255191673Sjamie if (error) 256191673Sjamie return (error); 257192895Sjamie j.version = j0.version; 258192895Sjamie j.path = j0.path; 259192895Sjamie j.hostname = j0.hostname; 260192895Sjamie j.ip4s = j0.ip_number; 261191673Sjamie break; 262191673Sjamie } 263191673Sjamie 264191673Sjamie case 1: 265185435Sbz /* 266191673Sjamie * Version 1 was used by multi-IPv4 jail implementations 267191673Sjamie * that never made it into the official kernel. 268185435Sbz */ 269191673Sjamie return (EINVAL); 270185435Sbz 271191673Sjamie case 2: /* JAIL_API_VERSION */ 272191673Sjamie /* FreeBSD multi-IPv4/IPv6,noIP jails. */ 273191673Sjamie error = copyin(uap->jail, &j, sizeof(struct jail)); 274191673Sjamie if (error) 275191673Sjamie return (error); 276192895Sjamie break; 277192895Sjamie 278192895Sjamie default: 279192895Sjamie /* Sci-Fi jails are not supported, sorry. */ 280192895Sjamie return (EINVAL); 281192895Sjamie } 282192895Sjamie return (kern_jail(td, &j)); 283192895Sjamie} 284192895Sjamie 285192895Sjamieint 286192895Sjamiekern_jail(struct thread *td, struct jail *j) 287192895Sjamie{ 288193865Sjamie struct iovec optiov[2 * (4 289193865Sjamie + sizeof(pr_allow_names) / sizeof(pr_allow_names[0]) 290193865Sjamie#ifdef INET 291193865Sjamie + 1 292193865Sjamie#endif 293193865Sjamie#ifdef INET6 294193865Sjamie + 1 295193865Sjamie#endif 296193865Sjamie )]; 297192895Sjamie struct uio opt; 298192895Sjamie char *u_path, *u_hostname, *u_name; 299185435Sbz#ifdef INET 300193865Sjamie uint32_t ip4s; 301192895Sjamie struct in_addr *u_ip4; 302192895Sjamie#endif 303192895Sjamie#ifdef INET6 304192895Sjamie struct in6_addr *u_ip6; 305192895Sjamie#endif 306192895Sjamie size_t tmplen; 307192895Sjamie int error, enforce_statfs, fi; 308192895Sjamie 309192895Sjamie bzero(&optiov, sizeof(optiov)); 310192895Sjamie opt.uio_iov = optiov; 311192895Sjamie opt.uio_iovcnt = 0; 312192895Sjamie opt.uio_offset = -1; 313192895Sjamie opt.uio_resid = -1; 314192895Sjamie opt.uio_segflg = UIO_SYSSPACE; 315192895Sjamie opt.uio_rw = UIO_READ; 316192895Sjamie opt.uio_td = td; 317192895Sjamie 318192895Sjamie /* Set permissions for top-level jails from sysctls. */ 319192895Sjamie if (!jailed(td->td_ucred)) { 320192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / 321192895Sjamie sizeof(pr_allow_names[0]); fi++) { 322192895Sjamie optiov[opt.uio_iovcnt].iov_base = 323192895Sjamie (jail_default_allow & (1 << fi)) 324192895Sjamie ? pr_allow_names[fi] : pr_allow_nonames[fi]; 325192895Sjamie optiov[opt.uio_iovcnt].iov_len = 326192895Sjamie strlen(optiov[opt.uio_iovcnt].iov_base) + 1; 327192895Sjamie opt.uio_iovcnt += 2; 328192895Sjamie } 329192895Sjamie optiov[opt.uio_iovcnt].iov_base = "enforce_statfs"; 330192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs"); 331192895Sjamie opt.uio_iovcnt++; 332192895Sjamie enforce_statfs = jail_default_enforce_statfs; 333192895Sjamie optiov[opt.uio_iovcnt].iov_base = &enforce_statfs; 334192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs); 335192895Sjamie opt.uio_iovcnt++; 336192895Sjamie } 337192895Sjamie 338192895Sjamie tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN; 339192895Sjamie#ifdef INET 340192895Sjamie ip4s = (j->version == 0) ? 1 : j->ip4s; 341192895Sjamie if (ip4s > jail_max_af_ips) 342192895Sjamie return (EINVAL); 343192895Sjamie tmplen += ip4s * sizeof(struct in_addr); 344191673Sjamie#else 345192895Sjamie if (j->ip4s > 0) 346192895Sjamie return (EINVAL); 347191673Sjamie#endif 348191673Sjamie#ifdef INET6 349192895Sjamie if (j->ip6s > jail_max_af_ips) 350192895Sjamie return (EINVAL); 351192895Sjamie tmplen += j->ip6s * sizeof(struct in6_addr); 352191673Sjamie#else 353192895Sjamie if (j->ip6s > 0) 354192895Sjamie return (EINVAL); 355191673Sjamie#endif 356192895Sjamie u_path = malloc(tmplen, M_TEMP, M_WAITOK); 357192895Sjamie u_hostname = u_path + MAXPATHLEN; 358192895Sjamie u_name = u_hostname + MAXHOSTNAMELEN; 359191673Sjamie#ifdef INET 360192895Sjamie u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN); 361191673Sjamie#endif 362191673Sjamie#ifdef INET6 363191673Sjamie#ifdef INET 364192895Sjamie u_ip6 = (struct in6_addr *)(u_ip4 + ip4s); 365191673Sjamie#else 366192895Sjamie u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN); 367191673Sjamie#endif 368191673Sjamie#endif 369192895Sjamie optiov[opt.uio_iovcnt].iov_base = "path"; 370192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("path"); 371192895Sjamie opt.uio_iovcnt++; 372192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_path; 373192895Sjamie error = copyinstr(j->path, u_path, MAXPATHLEN, 374192895Sjamie &optiov[opt.uio_iovcnt].iov_len); 375192895Sjamie if (error) { 376192895Sjamie free(u_path, M_TEMP); 377192895Sjamie return (error); 378192895Sjamie } 379192895Sjamie opt.uio_iovcnt++; 380192895Sjamie optiov[opt.uio_iovcnt].iov_base = "host.hostname"; 381192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname"); 382192895Sjamie opt.uio_iovcnt++; 383192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_hostname; 384192895Sjamie error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN, 385192895Sjamie &optiov[opt.uio_iovcnt].iov_len); 386192895Sjamie if (error) { 387192895Sjamie free(u_path, M_TEMP); 388192895Sjamie return (error); 389192895Sjamie } 390192895Sjamie opt.uio_iovcnt++; 391192895Sjamie if (j->jailname != NULL) { 392192895Sjamie optiov[opt.uio_iovcnt].iov_base = "name"; 393192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("name"); 394192895Sjamie opt.uio_iovcnt++; 395192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_name; 396192895Sjamie error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN, 397192895Sjamie &optiov[opt.uio_iovcnt].iov_len); 398191673Sjamie if (error) { 399191673Sjamie free(u_path, M_TEMP); 400191673Sjamie return (error); 401191673Sjamie } 402192895Sjamie opt.uio_iovcnt++; 403192895Sjamie } 404191673Sjamie#ifdef INET 405192895Sjamie optiov[opt.uio_iovcnt].iov_base = "ip4.addr"; 406192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr"); 407192895Sjamie opt.uio_iovcnt++; 408192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_ip4; 409192895Sjamie optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr); 410192895Sjamie if (j->version == 0) 411192895Sjamie u_ip4->s_addr = j->ip4s; 412192895Sjamie else { 413192895Sjamie error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len); 414191673Sjamie if (error) { 415191673Sjamie free(u_path, M_TEMP); 416191673Sjamie return (error); 417191673Sjamie } 418192895Sjamie } 419192895Sjamie opt.uio_iovcnt++; 420185435Sbz#endif 421185435Sbz#ifdef INET6 422192895Sjamie optiov[opt.uio_iovcnt].iov_base = "ip6.addr"; 423192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr"); 424192895Sjamie opt.uio_iovcnt++; 425192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_ip6; 426192895Sjamie optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr); 427192895Sjamie error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len); 428192895Sjamie if (error) { 429192895Sjamie free(u_path, M_TEMP); 430192895Sjamie return (error); 431192895Sjamie } 432192895Sjamie opt.uio_iovcnt++; 433185435Sbz#endif 434192895Sjamie KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]), 435192895Sjamie ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt)); 436191673Sjamie error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH); 437191673Sjamie free(u_path, M_TEMP); 438191673Sjamie return (error); 439185435Sbz} 440185435Sbz 441192895Sjamie 442191673Sjamie/* 443191673Sjamie * struct jail_set_args { 444191673Sjamie * struct iovec *iovp; 445191673Sjamie * unsigned int iovcnt; 446191673Sjamie * int flags; 447191673Sjamie * }; 448191673Sjamie */ 449191673Sjamieint 450191673Sjamiejail_set(struct thread *td, struct jail_set_args *uap) 451185435Sbz{ 452191673Sjamie struct uio *auio; 453191673Sjamie int error; 454191673Sjamie 455191673Sjamie /* Check that we have an even number of iovecs. */ 456191673Sjamie if (uap->iovcnt & 1) 457191673Sjamie return (EINVAL); 458191673Sjamie 459191673Sjamie error = copyinuio(uap->iovp, uap->iovcnt, &auio); 460191673Sjamie if (error) 461191673Sjamie return (error); 462191673Sjamie error = kern_jail_set(td, auio, uap->flags); 463191673Sjamie free(auio, M_IOV); 464191673Sjamie return (error); 465191673Sjamie} 466191673Sjamie 467191673Sjamieint 468191673Sjamiekern_jail_set(struct thread *td, struct uio *optuio, int flags) 469191673Sjamie{ 470191673Sjamie struct nameidata nd; 471185435Sbz#ifdef INET 472190466Sjamie struct in_addr *ip4; 473185435Sbz#endif 474185435Sbz#ifdef INET6 475185435Sbz struct in6_addr *ip6; 476185435Sbz#endif 477191673Sjamie struct vfsopt *opt; 478191673Sjamie struct vfsoptlist *opts; 479196135Sbz struct prison *pr, *deadpr, *mypr, *ppr, *tpr; 480191673Sjamie struct vnode *root; 481196835Sjamie char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid; 482192895Sjamie#if defined(INET) || defined(INET6) 483196135Sbz struct prison *tppr; 484191673Sjamie void *op; 485192895Sjamie#endif 486193066Sjamie unsigned long hid; 487192895Sjamie size_t namelen, onamelen; 488192895Sjamie int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos; 489195870Sjamie int gotchildmax, gotenforce, gothid, gotslevel; 490195870Sjamie int fi, jid, jsys, len, level; 491194762Sjamie int childmax, slevel, vfslocked; 492191673Sjamie#if defined(INET) || defined(INET6) 493192895Sjamie int ii, ij; 494191673Sjamie#endif 495191673Sjamie#ifdef INET 496195974Sjamie int ip4s, redo_ip4; 497191673Sjamie#endif 498191673Sjamie#ifdef INET6 499195974Sjamie int ip6s, redo_ip6; 500191673Sjamie#endif 501191673Sjamie unsigned pr_flags, ch_flags; 502192895Sjamie unsigned pr_allow, ch_allow, tallow; 503191673Sjamie char numbuf[12]; 504185435Sbz 505191673Sjamie error = priv_check(td, PRIV_JAIL_SET); 506191673Sjamie if (!error && (flags & JAIL_ATTACH)) 507191673Sjamie error = priv_check(td, PRIV_JAIL_ATTACH); 508191673Sjamie if (error) 509191673Sjamie return (error); 510192895Sjamie mypr = ppr = td->td_ucred->cr_prison; 511194762Sjamie if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0) 512192895Sjamie return (EPERM); 513191673Sjamie if (flags & ~JAIL_SET_MASK) 514191673Sjamie return (EINVAL); 515191673Sjamie 516185435Sbz /* 517191673Sjamie * Check all the parameters before committing to anything. Not all 518191673Sjamie * errors can be caught early, but we may as well try. Also, this 519191673Sjamie * takes care of some expensive stuff (path lookup) before getting 520191673Sjamie * the allprison lock. 521185435Sbz * 522191673Sjamie * XXX Jails are not filesystems, and jail parameters are not mount 523191673Sjamie * options. But it makes more sense to re-use the vfsopt code 524191673Sjamie * than duplicate it under a different name. 525185435Sbz */ 526191673Sjamie error = vfs_buildopts(optuio, &opts); 527191673Sjamie if (error) 528191673Sjamie return (error); 529185435Sbz#ifdef INET 530185435Sbz ip4 = NULL; 531185435Sbz#endif 532185435Sbz#ifdef INET6 533185435Sbz ip6 = NULL; 534185435Sbz#endif 535191673Sjamie 536191673Sjamie error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 537191673Sjamie if (error == ENOENT) 538191673Sjamie jid = 0; 539191673Sjamie else if (error != 0) 540191673Sjamie goto done_free; 541191673Sjamie 542191673Sjamie error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel)); 543191673Sjamie if (error == ENOENT) 544191673Sjamie gotslevel = 0; 545191673Sjamie else if (error != 0) 546191673Sjamie goto done_free; 547191673Sjamie else 548191673Sjamie gotslevel = 1; 549191673Sjamie 550194762Sjamie error = 551194762Sjamie vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax)); 552194762Sjamie if (error == ENOENT) 553194762Sjamie gotchildmax = 0; 554194762Sjamie else if (error != 0) 555194762Sjamie goto done_free; 556194762Sjamie else 557194762Sjamie gotchildmax = 1; 558194762Sjamie 559192895Sjamie error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce)); 560192895Sjamie gotenforce = (error == 0); 561192895Sjamie if (gotenforce) { 562192895Sjamie if (enforce < 0 || enforce > 2) 563192895Sjamie return (EINVAL); 564192895Sjamie } else if (error != ENOENT) 565192895Sjamie goto done_free; 566192895Sjamie 567191673Sjamie pr_flags = ch_flags = 0; 568192895Sjamie for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 569192895Sjamie fi++) { 570192895Sjamie if (pr_flag_names[fi] == NULL) 571192895Sjamie continue; 572192895Sjamie vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi); 573192895Sjamie vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi); 574192895Sjamie } 575191673Sjamie ch_flags |= pr_flags; 576195870Sjamie for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 577195870Sjamie fi++) { 578195870Sjamie error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys, 579195870Sjamie sizeof(jsys)); 580195870Sjamie if (error == ENOENT) 581195870Sjamie continue; 582195870Sjamie if (error != 0) 583195870Sjamie goto done_free; 584195870Sjamie switch (jsys) { 585195870Sjamie case JAIL_SYS_DISABLE: 586195870Sjamie if (!pr_flag_jailsys[fi].disable) { 587195870Sjamie error = EINVAL; 588195870Sjamie goto done_free; 589195870Sjamie } 590195870Sjamie pr_flags |= pr_flag_jailsys[fi].disable; 591195870Sjamie break; 592195870Sjamie case JAIL_SYS_NEW: 593195870Sjamie pr_flags |= pr_flag_jailsys[fi].new; 594195870Sjamie break; 595195870Sjamie case JAIL_SYS_INHERIT: 596195870Sjamie break; 597195870Sjamie default: 598195870Sjamie error = EINVAL; 599195870Sjamie goto done_free; 600195870Sjamie } 601195870Sjamie ch_flags |= 602195870Sjamie pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable; 603195870Sjamie } 604191673Sjamie if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE 605191673Sjamie && !(pr_flags & PR_PERSIST)) { 606191673Sjamie error = EINVAL; 607191673Sjamie vfs_opterror(opts, "new jail must persist or attach"); 608191673Sjamie goto done_errmsg; 609191673Sjamie } 610194251Sjamie#ifdef VIMAGE 611194251Sjamie if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) { 612194251Sjamie error = EINVAL; 613194251Sjamie vfs_opterror(opts, "vnet cannot be changed after creation"); 614194251Sjamie goto done_errmsg; 615194251Sjamie } 616194251Sjamie#endif 617195974Sjamie#ifdef INET 618195974Sjamie if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) { 619195974Sjamie error = EINVAL; 620195974Sjamie vfs_opterror(opts, "ip4 cannot be changed after creation"); 621195974Sjamie goto done_errmsg; 622195974Sjamie } 623195974Sjamie#endif 624195974Sjamie#ifdef INET6 625195974Sjamie if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) { 626195974Sjamie error = EINVAL; 627195974Sjamie vfs_opterror(opts, "ip6 cannot be changed after creation"); 628195974Sjamie goto done_errmsg; 629195974Sjamie } 630195974Sjamie#endif 631191673Sjamie 632192895Sjamie pr_allow = ch_allow = 0; 633192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 634192895Sjamie fi++) { 635192895Sjamie vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi); 636192895Sjamie vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi); 637192895Sjamie } 638192895Sjamie ch_allow |= pr_allow; 639192895Sjamie 640191673Sjamie error = vfs_getopt(opts, "name", (void **)&name, &len); 641191673Sjamie if (error == ENOENT) 642191673Sjamie name = NULL; 643191673Sjamie else if (error != 0) 644191673Sjamie goto done_free; 645191673Sjamie else { 646191673Sjamie if (len == 0 || name[len - 1] != '\0') { 647191673Sjamie error = EINVAL; 648191673Sjamie goto done_free; 649191673Sjamie } 650191673Sjamie if (len > MAXHOSTNAMELEN) { 651191673Sjamie error = ENAMETOOLONG; 652191673Sjamie goto done_free; 653191673Sjamie } 654191673Sjamie } 655191673Sjamie 656191673Sjamie error = vfs_getopt(opts, "host.hostname", (void **)&host, &len); 657191673Sjamie if (error == ENOENT) 658191673Sjamie host = NULL; 659191673Sjamie else if (error != 0) 660191673Sjamie goto done_free; 661191673Sjamie else { 662193066Sjamie ch_flags |= PR_HOST; 663193066Sjamie pr_flags |= PR_HOST; 664191673Sjamie if (len == 0 || host[len - 1] != '\0') { 665191673Sjamie error = EINVAL; 666191673Sjamie goto done_free; 667191673Sjamie } 668191673Sjamie if (len > MAXHOSTNAMELEN) { 669191673Sjamie error = ENAMETOOLONG; 670191673Sjamie goto done_free; 671191673Sjamie } 672191673Sjamie } 673191673Sjamie 674193066Sjamie error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len); 675193066Sjamie if (error == ENOENT) 676193066Sjamie domain = NULL; 677193066Sjamie else if (error != 0) 678193066Sjamie goto done_free; 679193066Sjamie else { 680193066Sjamie ch_flags |= PR_HOST; 681193066Sjamie pr_flags |= PR_HOST; 682193066Sjamie if (len == 0 || domain[len - 1] != '\0') { 683193066Sjamie error = EINVAL; 684193066Sjamie goto done_free; 685193066Sjamie } 686193066Sjamie if (len > MAXHOSTNAMELEN) { 687193066Sjamie error = ENAMETOOLONG; 688193066Sjamie goto done_free; 689193066Sjamie } 690193066Sjamie } 691193066Sjamie 692193066Sjamie error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len); 693193066Sjamie if (error == ENOENT) 694193066Sjamie uuid = NULL; 695193066Sjamie else if (error != 0) 696193066Sjamie goto done_free; 697193066Sjamie else { 698193066Sjamie ch_flags |= PR_HOST; 699193066Sjamie pr_flags |= PR_HOST; 700193066Sjamie if (len == 0 || uuid[len - 1] != '\0') { 701193066Sjamie error = EINVAL; 702193066Sjamie goto done_free; 703193066Sjamie } 704193066Sjamie if (len > HOSTUUIDLEN) { 705193066Sjamie error = ENAMETOOLONG; 706193066Sjamie goto done_free; 707193066Sjamie } 708193066Sjamie } 709193066Sjamie 710193066Sjamie#ifdef COMPAT_IA32 711193066Sjamie if (td->td_proc->p_sysent->sv_flags & SV_IA32) { 712193066Sjamie uint32_t hid32; 713193066Sjamie 714193066Sjamie error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32)); 715193066Sjamie hid = hid32; 716193066Sjamie } else 717193066Sjamie#endif 718193066Sjamie error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid)); 719193066Sjamie if (error == ENOENT) 720193066Sjamie gothid = 0; 721193066Sjamie else if (error != 0) 722193066Sjamie goto done_free; 723193066Sjamie else { 724193066Sjamie gothid = 1; 725193066Sjamie ch_flags |= PR_HOST; 726193066Sjamie pr_flags |= PR_HOST; 727193066Sjamie } 728193066Sjamie 729185435Sbz#ifdef INET 730191673Sjamie error = vfs_getopt(opts, "ip4.addr", &op, &ip4s); 731191673Sjamie if (error == ENOENT) 732195870Sjamie ip4s = (pr_flags & PR_IP4_DISABLE) ? 0 : -1; 733191673Sjamie else if (error != 0) 734191673Sjamie goto done_free; 735191673Sjamie else if (ip4s & (sizeof(*ip4) - 1)) { 736191673Sjamie error = EINVAL; 737191673Sjamie goto done_free; 738192895Sjamie } else { 739195870Sjamie ch_flags |= PR_IP4_USER | PR_IP4_DISABLE; 740195870Sjamie if (ip4s == 0) 741195870Sjamie pr_flags |= PR_IP4_USER | PR_IP4_DISABLE; 742195870Sjamie else { 743195870Sjamie pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER; 744192895Sjamie ip4s /= sizeof(*ip4); 745192895Sjamie if (ip4s > jail_max_af_ips) { 746185435Sbz error = EINVAL; 747192895Sjamie vfs_opterror(opts, "too many IPv4 addresses"); 748192895Sjamie goto done_errmsg; 749185435Sbz } 750195974Sjamie ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 751192895Sjamie bcopy(op, ip4, ip4s * sizeof(*ip4)); 752192895Sjamie /* 753192895Sjamie * IP addresses are all sorted but ip[0] to preserve 754192895Sjamie * the primary IP address as given from userland. 755192895Sjamie * This special IP is used for unbound outgoing 756202116Sbz * connections as well for "loopback" traffic in case 757202116Sbz * source address selection cannot find any more fitting 758202116Sbz * address to connect from. 759192895Sjamie */ 760192895Sjamie if (ip4s > 1) 761192895Sjamie qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4); 762192895Sjamie /* 763192895Sjamie * Check for duplicate addresses and do some simple 764192895Sjamie * zero and broadcast checks. If users give other bogus 765192895Sjamie * addresses it is their problem. 766192895Sjamie * 767192895Sjamie * We do not have to care about byte order for these 768192895Sjamie * checks so we will do them in NBO. 769192895Sjamie */ 770192895Sjamie for (ii = 0; ii < ip4s; ii++) { 771192895Sjamie if (ip4[ii].s_addr == INADDR_ANY || 772192895Sjamie ip4[ii].s_addr == INADDR_BROADCAST) { 773192895Sjamie error = EINVAL; 774192895Sjamie goto done_free; 775192895Sjamie } 776192895Sjamie if ((ii+1) < ip4s && 777192895Sjamie (ip4[0].s_addr == ip4[ii+1].s_addr || 778192895Sjamie ip4[ii].s_addr == ip4[ii+1].s_addr)) { 779192895Sjamie error = EINVAL; 780192895Sjamie goto done_free; 781192895Sjamie } 782192895Sjamie } 783185435Sbz } 784191673Sjamie } 785191673Sjamie#endif 786185435Sbz 787185435Sbz#ifdef INET6 788191673Sjamie error = vfs_getopt(opts, "ip6.addr", &op, &ip6s); 789191673Sjamie if (error == ENOENT) 790195870Sjamie ip6s = (pr_flags & PR_IP6_DISABLE) ? 0 : -1; 791191673Sjamie else if (error != 0) 792191673Sjamie goto done_free; 793191673Sjamie else if (ip6s & (sizeof(*ip6) - 1)) { 794191673Sjamie error = EINVAL; 795191673Sjamie goto done_free; 796192895Sjamie } else { 797195870Sjamie ch_flags |= PR_IP6_USER | PR_IP6_DISABLE; 798195870Sjamie if (ip6s == 0) 799195870Sjamie pr_flags |= PR_IP6_USER | PR_IP6_DISABLE; 800195870Sjamie else { 801195870Sjamie pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER; 802192895Sjamie ip6s /= sizeof(*ip6); 803192895Sjamie if (ip6s > jail_max_af_ips) { 804185435Sbz error = EINVAL; 805192895Sjamie vfs_opterror(opts, "too many IPv6 addresses"); 806192895Sjamie goto done_errmsg; 807185435Sbz } 808195974Sjamie ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 809192895Sjamie bcopy(op, ip6, ip6s * sizeof(*ip6)); 810192895Sjamie if (ip6s > 1) 811192895Sjamie qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6); 812192895Sjamie for (ii = 0; ii < ip6s; ii++) { 813192895Sjamie if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) { 814192895Sjamie error = EINVAL; 815192895Sjamie goto done_free; 816192895Sjamie } 817192895Sjamie if ((ii+1) < ip6s && 818192895Sjamie (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) || 819192895Sjamie IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1]))) 820192895Sjamie { 821192895Sjamie error = EINVAL; 822192895Sjamie goto done_free; 823192895Sjamie } 824192895Sjamie } 825185435Sbz } 826191673Sjamie } 827185435Sbz#endif 828185435Sbz 829195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6)) 830195945Sjamie if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 831195945Sjamie error = EINVAL; 832195945Sjamie vfs_opterror(opts, 833195945Sjamie "vnet jails cannot have IP address restrictions"); 834195945Sjamie goto done_errmsg; 835195945Sjamie } 836195945Sjamie#endif 837195945Sjamie 838191673Sjamie root = NULL; 839191673Sjamie error = vfs_getopt(opts, "path", (void **)&path, &len); 840191673Sjamie if (error == ENOENT) 841191673Sjamie path = NULL; 842191673Sjamie else if (error != 0) 843191673Sjamie goto done_free; 844191673Sjamie else { 845191673Sjamie if (flags & JAIL_UPDATE) { 846191673Sjamie error = EINVAL; 847191673Sjamie vfs_opterror(opts, 848191673Sjamie "path cannot be changed after creation"); 849191673Sjamie goto done_errmsg; 850191673Sjamie } 851191673Sjamie if (len == 0 || path[len - 1] != '\0') { 852191673Sjamie error = EINVAL; 853191673Sjamie goto done_free; 854191673Sjamie } 855191673Sjamie if (len < 2 || (len == 2 && path[0] == '/')) 856191673Sjamie path = NULL; 857191673Sjamie else { 858192895Sjamie /* Leave room for a real-root full pathname. */ 859192895Sjamie if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/") 860192895Sjamie ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) { 861192895Sjamie error = ENAMETOOLONG; 862192895Sjamie goto done_free; 863192895Sjamie } 864191673Sjamie NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_SYSSPACE, 865191673Sjamie path, td); 866191673Sjamie error = namei(&nd); 867191673Sjamie if (error) 868191673Sjamie goto done_free; 869191673Sjamie vfslocked = NDHASGIANT(&nd); 870191673Sjamie root = nd.ni_vp; 871191673Sjamie NDFREE(&nd, NDF_ONLY_PNBUF); 872191673Sjamie if (root->v_type != VDIR) { 873191673Sjamie error = ENOTDIR; 874191673Sjamie vrele(root); 875191673Sjamie VFS_UNLOCK_GIANT(vfslocked); 876191673Sjamie goto done_free; 877191673Sjamie } 878191673Sjamie VFS_UNLOCK_GIANT(vfslocked); 879191673Sjamie } 880191673Sjamie } 881185435Sbz 882191673Sjamie /* 883191673Sjamie * Grab the allprison lock before letting modules check their 884191673Sjamie * parameters. Once we have it, do not let go so we'll have a 885191673Sjamie * consistent view of the OSD list. 886191673Sjamie */ 887191673Sjamie sx_xlock(&allprison_lock); 888191673Sjamie error = osd_jail_call(NULL, PR_METHOD_CHECK, opts); 889191673Sjamie if (error) 890191673Sjamie goto done_unlock_list; 891185435Sbz 892191673Sjamie /* By now, all parameters should have been noted. */ 893191673Sjamie TAILQ_FOREACH(opt, opts, link) { 894191673Sjamie if (!opt->seen && strcmp(opt->name, "errmsg")) { 895191673Sjamie error = EINVAL; 896191673Sjamie vfs_opterror(opts, "unknown parameter: %s", opt->name); 897191673Sjamie goto done_unlock_list; 898191673Sjamie } 899191673Sjamie } 900191673Sjamie 901185435Sbz /* 902191673Sjamie * See if we are creating a new record or updating an existing one. 903191673Sjamie * This abuses the file error codes ENOENT and EEXIST. 904185435Sbz */ 905191673Sjamie cuflags = flags & (JAIL_CREATE | JAIL_UPDATE); 906191673Sjamie if (!cuflags) { 907191673Sjamie error = EINVAL; 908191673Sjamie vfs_opterror(opts, "no valid operation (create or update)"); 909191673Sjamie goto done_unlock_list; 910191673Sjamie } 911191673Sjamie pr = NULL; 912196835Sjamie namelc = NULL; 913196835Sjamie if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) { 914196835Sjamie namelc = strrchr(name, '.'); 915196835Sjamie jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10); 916196835Sjamie if (*p != '\0') 917196835Sjamie jid = 0; 918196835Sjamie } 919191673Sjamie if (jid != 0) { 920192895Sjamie /* 921192895Sjamie * See if a requested jid already exists. There is an 922192895Sjamie * information leak here if the jid exists but is not within 923192895Sjamie * the caller's jail hierarchy. Jail creators will get EEXIST 924192895Sjamie * even though they cannot see the jail, and CREATE | UPDATE 925192895Sjamie * will return ENOENT which is not normally a valid error. 926192895Sjamie */ 927191673Sjamie if (jid < 0) { 928191673Sjamie error = EINVAL; 929191673Sjamie vfs_opterror(opts, "negative jid"); 930191673Sjamie goto done_unlock_list; 931191673Sjamie } 932191673Sjamie pr = prison_find(jid); 933191673Sjamie if (pr != NULL) { 934192895Sjamie ppr = pr->pr_parent; 935191673Sjamie /* Create: jid must not exist. */ 936191673Sjamie if (cuflags == JAIL_CREATE) { 937191673Sjamie mtx_unlock(&pr->pr_mtx); 938191673Sjamie error = EEXIST; 939191673Sjamie vfs_opterror(opts, "jail %d already exists", 940191673Sjamie jid); 941191673Sjamie goto done_unlock_list; 942191673Sjamie } 943192895Sjamie if (!prison_ischild(mypr, pr)) { 944192895Sjamie mtx_unlock(&pr->pr_mtx); 945192895Sjamie pr = NULL; 946192895Sjamie } else if (pr->pr_uref == 0) { 947191673Sjamie if (!(flags & JAIL_DYING)) { 948191673Sjamie mtx_unlock(&pr->pr_mtx); 949191673Sjamie error = ENOENT; 950191673Sjamie vfs_opterror(opts, "jail %d is dying", 951191673Sjamie jid); 952191673Sjamie goto done_unlock_list; 953191673Sjamie } else if ((flags & JAIL_ATTACH) || 954191673Sjamie (pr_flags & PR_PERSIST)) { 955191673Sjamie /* 956191673Sjamie * A dying jail might be resurrected 957191673Sjamie * (via attach or persist), but first 958191673Sjamie * it must determine if another jail 959191673Sjamie * has claimed its name. Accomplish 960191673Sjamie * this by implicitly re-setting the 961191673Sjamie * name. 962191673Sjamie */ 963191673Sjamie if (name == NULL) 964192895Sjamie name = prison_name(mypr, pr); 965191673Sjamie } 966191673Sjamie } 967191673Sjamie } 968191673Sjamie if (pr == NULL) { 969191673Sjamie /* Update: jid must exist. */ 970191673Sjamie if (cuflags == JAIL_UPDATE) { 971191673Sjamie error = ENOENT; 972191673Sjamie vfs_opterror(opts, "jail %d not found", jid); 973191673Sjamie goto done_unlock_list; 974191673Sjamie } 975191673Sjamie } 976191673Sjamie } 977191673Sjamie /* 978191673Sjamie * If the caller provided a name, look for a jail by that name. 979191673Sjamie * This has different semantics for creates and updates keyed by jid 980191673Sjamie * (where the name must not already exist in a different jail), 981191673Sjamie * and updates keyed by the name itself (where the name must exist 982191673Sjamie * because that is the jail being updated). 983191673Sjamie */ 984191673Sjamie if (name != NULL) { 985196835Sjamie namelc = strrchr(name, '.'); 986196835Sjamie if (namelc == NULL) 987196835Sjamie namelc = name; 988196835Sjamie else { 989192895Sjamie /* 990192895Sjamie * This is a hierarchical name. Split it into the 991192895Sjamie * parent and child names, and make sure the parent 992192895Sjamie * exists or matches an already found jail. 993192895Sjamie */ 994196835Sjamie *namelc = '\0'; 995192895Sjamie if (pr != NULL) { 996196835Sjamie if (strncmp(name, ppr->pr_name, namelc - name) 997196835Sjamie || ppr->pr_name[namelc - name] != '\0') { 998192895Sjamie mtx_unlock(&pr->pr_mtx); 999192895Sjamie error = EINVAL; 1000192895Sjamie vfs_opterror(opts, 1001192895Sjamie "cannot change jail's parent"); 1002192895Sjamie goto done_unlock_list; 1003192895Sjamie } 1004192895Sjamie } else { 1005192895Sjamie ppr = prison_find_name(mypr, name); 1006192895Sjamie if (ppr == NULL) { 1007192895Sjamie error = ENOENT; 1008192895Sjamie vfs_opterror(opts, 1009192895Sjamie "jail \"%s\" not found", name); 1010192895Sjamie goto done_unlock_list; 1011192895Sjamie } 1012192895Sjamie mtx_unlock(&ppr->pr_mtx); 1013192895Sjamie } 1014196835Sjamie name = ++namelc; 1015192895Sjamie } 1016191673Sjamie if (name[0] != '\0') { 1017192895Sjamie namelen = 1018192895Sjamie (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 1019192895Sjamie name_again: 1020191673Sjamie deadpr = NULL; 1021192895Sjamie FOREACH_PRISON_CHILD(ppr, tpr) { 1022191673Sjamie if (tpr != pr && tpr->pr_ref > 0 && 1023192895Sjamie !strcmp(tpr->pr_name + namelen, name)) { 1024191673Sjamie if (pr == NULL && 1025191673Sjamie cuflags != JAIL_CREATE) { 1026191673Sjamie mtx_lock(&tpr->pr_mtx); 1027191673Sjamie if (tpr->pr_ref > 0) { 1028191673Sjamie /* 1029191673Sjamie * Use this jail 1030191673Sjamie * for updates. 1031191673Sjamie */ 1032191673Sjamie if (tpr->pr_uref > 0) { 1033191673Sjamie pr = tpr; 1034191673Sjamie break; 1035191673Sjamie } 1036191673Sjamie deadpr = tpr; 1037191673Sjamie } 1038191673Sjamie mtx_unlock(&tpr->pr_mtx); 1039191673Sjamie } else if (tpr->pr_uref > 0) { 1040191673Sjamie /* 1041191673Sjamie * Create, or update(jid): 1042191673Sjamie * name must not exist in an 1043192895Sjamie * active sibling jail. 1044191673Sjamie */ 1045191673Sjamie error = EEXIST; 1046191673Sjamie if (pr != NULL) 1047191673Sjamie mtx_unlock(&pr->pr_mtx); 1048191673Sjamie vfs_opterror(opts, 1049191673Sjamie "jail \"%s\" already exists", 1050191673Sjamie name); 1051191673Sjamie goto done_unlock_list; 1052191673Sjamie } 1053191673Sjamie } 1054191673Sjamie } 1055191673Sjamie /* If no active jail is found, use a dying one. */ 1056191673Sjamie if (deadpr != NULL && pr == NULL) { 1057191673Sjamie if (flags & JAIL_DYING) { 1058191673Sjamie mtx_lock(&deadpr->pr_mtx); 1059191673Sjamie if (deadpr->pr_ref == 0) { 1060191673Sjamie mtx_unlock(&deadpr->pr_mtx); 1061191673Sjamie goto name_again; 1062191673Sjamie } 1063191673Sjamie pr = deadpr; 1064191673Sjamie } else if (cuflags == JAIL_UPDATE) { 1065191673Sjamie error = ENOENT; 1066191673Sjamie vfs_opterror(opts, 1067191673Sjamie "jail \"%s\" is dying", name); 1068191673Sjamie goto done_unlock_list; 1069191673Sjamie } 1070191673Sjamie } 1071191673Sjamie /* Update: name must exist if no jid. */ 1072191673Sjamie else if (cuflags == JAIL_UPDATE && pr == NULL) { 1073191673Sjamie error = ENOENT; 1074191673Sjamie vfs_opterror(opts, "jail \"%s\" not found", 1075191673Sjamie name); 1076191673Sjamie goto done_unlock_list; 1077191673Sjamie } 1078191673Sjamie } 1079191673Sjamie } 1080191673Sjamie /* Update: must provide a jid or name. */ 1081191673Sjamie else if (cuflags == JAIL_UPDATE && pr == NULL) { 1082191673Sjamie error = ENOENT; 1083191673Sjamie vfs_opterror(opts, "update specified no jail"); 1084191673Sjamie goto done_unlock_list; 1085191673Sjamie } 1086185435Sbz 1087191673Sjamie /* If there's no prison to update, create a new one and link it in. */ 1088191673Sjamie if (pr == NULL) { 1089194762Sjamie for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent) 1090194762Sjamie if (tpr->pr_childcount >= tpr->pr_childmax) { 1091194762Sjamie error = EPERM; 1092194762Sjamie vfs_opterror(opts, "prison limit exceeded"); 1093194762Sjamie goto done_unlock_list; 1094194762Sjamie } 1095191673Sjamie created = 1; 1096192895Sjamie mtx_lock(&ppr->pr_mtx); 1097192895Sjamie if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) { 1098192895Sjamie mtx_unlock(&ppr->pr_mtx); 1099192895Sjamie error = ENOENT; 1100192895Sjamie vfs_opterror(opts, "parent jail went away!"); 1101192895Sjamie goto done_unlock_list; 1102192895Sjamie } 1103192895Sjamie ppr->pr_ref++; 1104192895Sjamie ppr->pr_uref++; 1105192895Sjamie mtx_unlock(&ppr->pr_mtx); 1106191673Sjamie pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO); 1107191673Sjamie if (jid == 0) { 1108191673Sjamie /* Find the next free jid. */ 1109191673Sjamie jid = lastprid + 1; 1110191673Sjamie findnext: 1111191673Sjamie if (jid == JAIL_MAX) 1112191673Sjamie jid = 1; 1113191673Sjamie TAILQ_FOREACH(tpr, &allprison, pr_list) { 1114191673Sjamie if (tpr->pr_id < jid) 1115191673Sjamie continue; 1116191673Sjamie if (tpr->pr_id > jid || tpr->pr_ref == 0) { 1117191673Sjamie TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1118191673Sjamie break; 1119191673Sjamie } 1120191673Sjamie if (jid == lastprid) { 1121191673Sjamie error = EAGAIN; 1122191673Sjamie vfs_opterror(opts, 1123191673Sjamie "no available jail IDs"); 1124191673Sjamie free(pr, M_PRISON); 1125192895Sjamie prison_deref(ppr, PD_DEREF | 1126192895Sjamie PD_DEUREF | PD_LIST_XLOCKED); 1127192895Sjamie goto done_releroot; 1128191673Sjamie } 1129191673Sjamie jid++; 1130191673Sjamie goto findnext; 1131191673Sjamie } 1132191673Sjamie lastprid = jid; 1133191673Sjamie } else { 1134191673Sjamie /* 1135191673Sjamie * The jail already has a jid (that did not yet exist), 1136191673Sjamie * so just find where to insert it. 1137191673Sjamie */ 1138191673Sjamie TAILQ_FOREACH(tpr, &allprison, pr_list) 1139191673Sjamie if (tpr->pr_id >= jid) { 1140191673Sjamie TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1141191673Sjamie break; 1142191673Sjamie } 1143191673Sjamie } 1144191673Sjamie if (tpr == NULL) 1145191673Sjamie TAILQ_INSERT_TAIL(&allprison, pr, pr_list); 1146192895Sjamie LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling); 1147192895Sjamie for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 1148194762Sjamie tpr->pr_childcount++; 1149185435Sbz 1150192895Sjamie pr->pr_parent = ppr; 1151191673Sjamie pr->pr_id = jid; 1152192895Sjamie 1153192895Sjamie /* Set some default values, and inherit some from the parent. */ 1154191673Sjamie if (name == NULL) 1155191673Sjamie name = ""; 1156191673Sjamie if (path == NULL) { 1157191673Sjamie path = "/"; 1158192895Sjamie root = mypr->pr_root; 1159191673Sjamie vref(root); 1160191673Sjamie } 1161195944Sjamie strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN); 1162195944Sjamie pr->pr_flags |= PR_HOST; 1163195945Sjamie#if defined(INET) || defined(INET6) 1164195945Sjamie#ifdef VIMAGE 1165195945Sjamie if (!(pr_flags & PR_VNET)) 1166195945Sjamie#endif 1167195945Sjamie { 1168192895Sjamie#ifdef INET 1169195974Sjamie if (!(ch_flags & PR_IP4_USER)) 1170195974Sjamie pr->pr_flags |= 1171195974Sjamie PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE; 1172195974Sjamie else if (!(pr_flags & PR_IP4_USER)) { 1173195974Sjamie pr->pr_flags |= ppr->pr_flags & PR_IP4; 1174195974Sjamie if (ppr->pr_ip4 != NULL) { 1175195974Sjamie pr->pr_ip4s = ppr->pr_ip4s; 1176195974Sjamie pr->pr_ip4 = malloc(pr->pr_ip4s * 1177195974Sjamie sizeof(struct in_addr), M_PRISON, 1178195974Sjamie M_WAITOK); 1179195974Sjamie bcopy(ppr->pr_ip4, pr->pr_ip4, 1180195974Sjamie pr->pr_ip4s * sizeof(*pr->pr_ip4)); 1181195974Sjamie } 1182195974Sjamie } 1183192895Sjamie#endif 1184192895Sjamie#ifdef INET6 1185195974Sjamie if (!(ch_flags & PR_IP6_USER)) 1186195974Sjamie pr->pr_flags |= 1187195974Sjamie PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE; 1188195974Sjamie else if (!(pr_flags & PR_IP6_USER)) { 1189195974Sjamie pr->pr_flags |= ppr->pr_flags & PR_IP6; 1190195974Sjamie if (ppr->pr_ip6 != NULL) { 1191195974Sjamie pr->pr_ip6s = ppr->pr_ip6s; 1192195974Sjamie pr->pr_ip6 = malloc(pr->pr_ip6s * 1193195974Sjamie sizeof(struct in6_addr), M_PRISON, 1194195974Sjamie M_WAITOK); 1195195974Sjamie bcopy(ppr->pr_ip6, pr->pr_ip6, 1196195974Sjamie pr->pr_ip6s * sizeof(*pr->pr_ip6)); 1197195974Sjamie } 1198195974Sjamie } 1199192895Sjamie#endif 1200195945Sjamie } 1201195945Sjamie#endif 1202192895Sjamie pr->pr_securelevel = ppr->pr_securelevel; 1203192895Sjamie pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow; 1204196002Sjamie pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 1205191673Sjamie 1206192895Sjamie LIST_INIT(&pr->pr_children); 1207192895Sjamie mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK); 1208191673Sjamie 1209194251Sjamie#ifdef VIMAGE 1210194251Sjamie /* Allocate a new vnet if specified. */ 1211194251Sjamie pr->pr_vnet = (pr_flags & PR_VNET) 1212194251Sjamie ? vnet_alloc() : ppr->pr_vnet; 1213194251Sjamie#endif 1214185435Sbz /* 1215191673Sjamie * Allocate a dedicated cpuset for each jail. 1216191673Sjamie * Unlike other initial settings, this may return an erorr. 1217185435Sbz */ 1218192895Sjamie error = cpuset_create_root(ppr, &pr->pr_cpuset); 1219191673Sjamie if (error) { 1220191673Sjamie prison_deref(pr, PD_LIST_XLOCKED); 1221191673Sjamie goto done_releroot; 1222191673Sjamie } 1223185435Sbz 1224191673Sjamie mtx_lock(&pr->pr_mtx); 1225185435Sbz /* 1226191673Sjamie * New prisons do not yet have a reference, because we do not 1227191673Sjamie * want other to see the incomplete prison once the 1228191673Sjamie * allprison_lock is downgraded. 1229185435Sbz */ 1230191673Sjamie } else { 1231191673Sjamie created = 0; 1232195974Sjamie /* 1233195974Sjamie * Grab a reference for existing prisons, to ensure they 1234195974Sjamie * continue to exist for the duration of the call. 1235195974Sjamie */ 1236195974Sjamie pr->pr_ref++; 1237195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6)) 1238195945Sjamie if ((pr->pr_flags & PR_VNET) && 1239195945Sjamie (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 1240195945Sjamie error = EINVAL; 1241195945Sjamie vfs_opterror(opts, 1242195945Sjamie "vnet jails cannot have IP address restrictions"); 1243195945Sjamie goto done_deref_locked; 1244195945Sjamie } 1245195945Sjamie#endif 1246195974Sjamie#ifdef INET 1247195974Sjamie if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 1248195974Sjamie error = EINVAL; 1249195974Sjamie vfs_opterror(opts, 1250195974Sjamie "ip4 cannot be changed after creation"); 1251195974Sjamie goto done_deref_locked; 1252195974Sjamie } 1253195974Sjamie#endif 1254195974Sjamie#ifdef INET6 1255195974Sjamie if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 1256195974Sjamie error = EINVAL; 1257195974Sjamie vfs_opterror(opts, 1258195974Sjamie "ip6 cannot be changed after creation"); 1259195974Sjamie goto done_deref_locked; 1260195974Sjamie } 1261195974Sjamie#endif 1262191673Sjamie } 1263185435Sbz 1264191673Sjamie /* Do final error checking before setting anything. */ 1265192895Sjamie if (gotslevel) { 1266192895Sjamie if (slevel < ppr->pr_securelevel) { 1267192895Sjamie error = EPERM; 1268192895Sjamie goto done_deref_locked; 1269192895Sjamie } 1270192895Sjamie } 1271194762Sjamie if (gotchildmax) { 1272194762Sjamie if (childmax >= ppr->pr_childmax) { 1273194762Sjamie error = EPERM; 1274194762Sjamie goto done_deref_locked; 1275194762Sjamie } 1276194762Sjamie } 1277192895Sjamie if (gotenforce) { 1278192895Sjamie if (enforce < ppr->pr_enforce_statfs) { 1279192895Sjamie error = EPERM; 1280192895Sjamie goto done_deref_locked; 1281192895Sjamie } 1282192895Sjamie } 1283185435Sbz#ifdef INET 1284195974Sjamie if (ip4s > 0) { 1285192895Sjamie if (ppr->pr_flags & PR_IP4) { 1286195974Sjamie /* 1287195974Sjamie * Make sure the new set of IP addresses is a 1288195974Sjamie * subset of the parent's list. Don't worry 1289195974Sjamie * about the parent being unlocked, as any 1290195974Sjamie * setting is done with allprison_lock held. 1291195974Sjamie */ 1292195974Sjamie for (ij = 0; ij < ppr->pr_ip4s; ij++) 1293195974Sjamie if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 1294195974Sjamie break; 1295195974Sjamie if (ij == ppr->pr_ip4s) { 1296195974Sjamie error = EPERM; 1297195974Sjamie goto done_deref_locked; 1298195974Sjamie } 1299195974Sjamie if (ip4s > 1) { 1300195974Sjamie for (ii = ij = 1; ii < ip4s; ii++) { 1301195974Sjamie if (ip4[ii].s_addr == 1302195974Sjamie ppr->pr_ip4[0].s_addr) 1303195974Sjamie continue; 1304195974Sjamie for (; ij < ppr->pr_ip4s; ij++) 1305195974Sjamie if (ip4[ii].s_addr == 1306195974Sjamie ppr->pr_ip4[ij].s_addr) 1307195974Sjamie break; 1308195974Sjamie if (ij == ppr->pr_ip4s) 1309195974Sjamie break; 1310192895Sjamie } 1311192895Sjamie if (ij == ppr->pr_ip4s) { 1312192895Sjamie error = EPERM; 1313192895Sjamie goto done_deref_locked; 1314192895Sjamie } 1315192895Sjamie } 1316192895Sjamie } 1317195974Sjamie /* 1318195974Sjamie * Check for conflicting IP addresses. We permit them 1319195974Sjamie * if there is no more than one IP on each jail. If 1320195974Sjamie * there is a duplicate on a jail with more than one 1321195974Sjamie * IP stop checking and return error. 1322195974Sjamie */ 1323195974Sjamie tppr = ppr; 1324195945Sjamie#ifdef VIMAGE 1325195974Sjamie for (; tppr != &prison0; tppr = tppr->pr_parent) 1326195974Sjamie if (tppr->pr_flags & PR_VNET) 1327195974Sjamie break; 1328195945Sjamie#endif 1329195974Sjamie FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1330195974Sjamie if (tpr == pr || 1331195945Sjamie#ifdef VIMAGE 1332195974Sjamie (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1333195945Sjamie#endif 1334195974Sjamie tpr->pr_uref == 0) { 1335192895Sjamie descend = 0; 1336195974Sjamie continue; 1337195974Sjamie } 1338195974Sjamie if (!(tpr->pr_flags & PR_IP4_USER)) 1339195974Sjamie continue; 1340195974Sjamie descend = 0; 1341195974Sjamie if (tpr->pr_ip4 == NULL || 1342195974Sjamie (ip4s == 1 && tpr->pr_ip4s == 1)) 1343195974Sjamie continue; 1344195974Sjamie for (ii = 0; ii < ip4s; ii++) { 1345195974Sjamie if (_prison_check_ip4(tpr, &ip4[ii]) == 0) { 1346195974Sjamie error = EADDRINUSE; 1347195974Sjamie vfs_opterror(opts, 1348195974Sjamie "IPv4 addresses clash"); 1349195974Sjamie goto done_deref_locked; 1350192895Sjamie } 1351192895Sjamie } 1352192895Sjamie } 1353192895Sjamie } 1354185435Sbz#endif 1355191673Sjamie#ifdef INET6 1356195974Sjamie if (ip6s > 0) { 1357192895Sjamie if (ppr->pr_flags & PR_IP6) { 1358195974Sjamie /* 1359195974Sjamie * Make sure the new set of IP addresses is a 1360195974Sjamie * subset of the parent's list. 1361195974Sjamie */ 1362195974Sjamie for (ij = 0; ij < ppr->pr_ip6s; ij++) 1363195974Sjamie if (IN6_ARE_ADDR_EQUAL(&ip6[0], 1364195974Sjamie &ppr->pr_ip6[ij])) 1365195974Sjamie break; 1366195974Sjamie if (ij == ppr->pr_ip6s) { 1367195974Sjamie error = EPERM; 1368195974Sjamie goto done_deref_locked; 1369195974Sjamie } 1370195974Sjamie if (ip6s > 1) { 1371195974Sjamie for (ii = ij = 1; ii < ip6s; ii++) { 1372195974Sjamie if (IN6_ARE_ADDR_EQUAL(&ip6[ii], 1373195974Sjamie &ppr->pr_ip6[0])) 1374195974Sjamie continue; 1375195974Sjamie for (; ij < ppr->pr_ip6s; ij++) 1376195974Sjamie if (IN6_ARE_ADDR_EQUAL( 1377195974Sjamie &ip6[ii], &ppr->pr_ip6[ij])) 1378195974Sjamie break; 1379195974Sjamie if (ij == ppr->pr_ip6s) 1380195974Sjamie break; 1381192895Sjamie } 1382192895Sjamie if (ij == ppr->pr_ip6s) { 1383192895Sjamie error = EPERM; 1384192895Sjamie goto done_deref_locked; 1385192895Sjamie } 1386192895Sjamie } 1387192895Sjamie } 1388195974Sjamie /* Check for conflicting IP addresses. */ 1389195974Sjamie tppr = ppr; 1390195945Sjamie#ifdef VIMAGE 1391195974Sjamie for (; tppr != &prison0; tppr = tppr->pr_parent) 1392195974Sjamie if (tppr->pr_flags & PR_VNET) 1393195974Sjamie break; 1394195945Sjamie#endif 1395195974Sjamie FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1396195974Sjamie if (tpr == pr || 1397195945Sjamie#ifdef VIMAGE 1398195974Sjamie (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1399195945Sjamie#endif 1400195974Sjamie tpr->pr_uref == 0) { 1401192895Sjamie descend = 0; 1402195974Sjamie continue; 1403195974Sjamie } 1404195974Sjamie if (!(tpr->pr_flags & PR_IP6_USER)) 1405195974Sjamie continue; 1406195974Sjamie descend = 0; 1407195974Sjamie if (tpr->pr_ip6 == NULL || 1408195974Sjamie (ip6s == 1 && tpr->pr_ip6s == 1)) 1409195974Sjamie continue; 1410195974Sjamie for (ii = 0; ii < ip6s; ii++) { 1411195974Sjamie if (_prison_check_ip6(tpr, &ip6[ii]) == 0) { 1412195974Sjamie error = EADDRINUSE; 1413195974Sjamie vfs_opterror(opts, 1414195974Sjamie "IPv6 addresses clash"); 1415195974Sjamie goto done_deref_locked; 1416192895Sjamie } 1417192895Sjamie } 1418191673Sjamie } 1419192895Sjamie } 1420191673Sjamie#endif 1421192895Sjamie onamelen = namelen = 0; 1422192895Sjamie if (name != NULL) { 1423191673Sjamie /* Give a default name of the jid. */ 1424191673Sjamie if (name[0] == '\0') 1425191673Sjamie snprintf(name = numbuf, sizeof(numbuf), "%d", jid); 1426196835Sjamie else if (*namelc == '0' || (strtoul(namelc, &p, 10) != jid && 1427196835Sjamie *p == '\0')) { 1428191673Sjamie error = EINVAL; 1429196835Sjamie vfs_opterror(opts, 1430196835Sjamie "name cannot be numeric (unless it is the jid)"); 1431192895Sjamie goto done_deref_locked; 1432191673Sjamie } 1433191673Sjamie /* 1434192895Sjamie * Make sure the name isn't too long for the prison or its 1435192895Sjamie * children. 1436191673Sjamie */ 1437192895Sjamie onamelen = strlen(pr->pr_name); 1438192895Sjamie namelen = strlen(name); 1439192895Sjamie if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) { 1440192895Sjamie error = ENAMETOOLONG; 1441192895Sjamie goto done_deref_locked; 1442192895Sjamie } 1443192895Sjamie FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 1444192895Sjamie if (strlen(tpr->pr_name) + (namelen - onamelen) >= 1445192895Sjamie sizeof(pr->pr_name)) { 1446192895Sjamie error = ENAMETOOLONG; 1447192895Sjamie goto done_deref_locked; 1448192895Sjamie } 1449192895Sjamie } 1450191673Sjamie } 1451192895Sjamie if (pr_allow & ~ppr->pr_allow) { 1452192895Sjamie error = EPERM; 1453192895Sjamie goto done_deref_locked; 1454192895Sjamie } 1455185435Sbz 1456191673Sjamie /* Set the parameters of the prison. */ 1457191673Sjamie#ifdef INET 1458192895Sjamie redo_ip4 = 0; 1459195974Sjamie if (pr_flags & PR_IP4_USER) { 1460195974Sjamie pr->pr_flags |= PR_IP4; 1461195974Sjamie free(pr->pr_ip4, M_PRISON); 1462195974Sjamie pr->pr_ip4s = ip4s; 1463195974Sjamie pr->pr_ip4 = ip4; 1464195974Sjamie ip4 = NULL; 1465192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1466195945Sjamie#ifdef VIMAGE 1467195945Sjamie if (tpr->pr_flags & PR_VNET) { 1468195945Sjamie descend = 0; 1469195945Sjamie continue; 1470195945Sjamie } 1471195945Sjamie#endif 1472192895Sjamie if (prison_restrict_ip4(tpr, NULL)) { 1473192895Sjamie redo_ip4 = 1; 1474192895Sjamie descend = 0; 1475192895Sjamie } 1476192895Sjamie } 1477185435Sbz } 1478191673Sjamie#endif 1479191673Sjamie#ifdef INET6 1480192895Sjamie redo_ip6 = 0; 1481195974Sjamie if (pr_flags & PR_IP6_USER) { 1482195974Sjamie pr->pr_flags |= PR_IP6; 1483195974Sjamie free(pr->pr_ip6, M_PRISON); 1484195974Sjamie pr->pr_ip6s = ip6s; 1485195974Sjamie pr->pr_ip6 = ip6; 1486195974Sjamie ip6 = NULL; 1487192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1488195945Sjamie#ifdef VIMAGE 1489195945Sjamie if (tpr->pr_flags & PR_VNET) { 1490195945Sjamie descend = 0; 1491195945Sjamie continue; 1492195945Sjamie } 1493195945Sjamie#endif 1494192895Sjamie if (prison_restrict_ip6(tpr, NULL)) { 1495192895Sjamie redo_ip6 = 1; 1496192895Sjamie descend = 0; 1497192895Sjamie } 1498192895Sjamie } 1499191673Sjamie } 1500191673Sjamie#endif 1501192895Sjamie if (gotslevel) { 1502191673Sjamie pr->pr_securelevel = slevel; 1503192895Sjamie /* Set all child jails to be at least this level. */ 1504192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1505192895Sjamie if (tpr->pr_securelevel < slevel) 1506192895Sjamie tpr->pr_securelevel = slevel; 1507192895Sjamie } 1508194762Sjamie if (gotchildmax) { 1509194762Sjamie pr->pr_childmax = childmax; 1510194762Sjamie /* Set all child jails to under this limit. */ 1511194762Sjamie FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level) 1512194762Sjamie if (tpr->pr_childmax > childmax - level) 1513194762Sjamie tpr->pr_childmax = childmax > level 1514194762Sjamie ? childmax - level : 0; 1515194762Sjamie } 1516192895Sjamie if (gotenforce) { 1517192895Sjamie pr->pr_enforce_statfs = enforce; 1518192895Sjamie /* Pass this restriction on to the children. */ 1519192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1520192895Sjamie if (tpr->pr_enforce_statfs < enforce) 1521192895Sjamie tpr->pr_enforce_statfs = enforce; 1522192895Sjamie } 1523192895Sjamie if (name != NULL) { 1524192895Sjamie if (ppr == &prison0) 1525192895Sjamie strlcpy(pr->pr_name, name, sizeof(pr->pr_name)); 1526192895Sjamie else 1527192895Sjamie snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s", 1528192895Sjamie ppr->pr_name, name); 1529192895Sjamie /* Change this component of child names. */ 1530192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1531192895Sjamie bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen, 1532192895Sjamie strlen(tpr->pr_name + onamelen) + 1); 1533192895Sjamie bcopy(pr->pr_name, tpr->pr_name, namelen); 1534192895Sjamie } 1535192895Sjamie } 1536191673Sjamie if (path != NULL) { 1537192895Sjamie /* Try to keep a real-rooted full pathname. */ 1538192895Sjamie if (path[0] == '/' && strcmp(mypr->pr_path, "/")) 1539192895Sjamie snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s", 1540192895Sjamie mypr->pr_path, path); 1541192895Sjamie else 1542192895Sjamie strlcpy(pr->pr_path, path, sizeof(pr->pr_path)); 1543191673Sjamie pr->pr_root = root; 1544191673Sjamie } 1545193066Sjamie if (PR_HOST & ch_flags & ~pr_flags) { 1546193066Sjamie if (pr->pr_flags & PR_HOST) { 1547193066Sjamie /* 1548193066Sjamie * Copy the parent's host info. As with pr_ip4 above, 1549193066Sjamie * the lack of a lock on the parent is not a problem; 1550193066Sjamie * it is always set with allprison_lock at least 1551193066Sjamie * shared, and is held exclusively here. 1552193066Sjamie */ 1553194118Sjamie strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname, 1554194118Sjamie sizeof(pr->pr_hostname)); 1555194118Sjamie strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname, 1556194118Sjamie sizeof(pr->pr_domainname)); 1557194118Sjamie strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid, 1558194118Sjamie sizeof(pr->pr_hostuuid)); 1559193066Sjamie pr->pr_hostid = pr->pr_parent->pr_hostid; 1560193066Sjamie } 1561193066Sjamie } else if (host != NULL || domain != NULL || uuid != NULL || gothid) { 1562193066Sjamie /* Set this prison, and any descendants without PR_HOST. */ 1563193066Sjamie if (host != NULL) 1564194118Sjamie strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname)); 1565193066Sjamie if (domain != NULL) 1566194118Sjamie strlcpy(pr->pr_domainname, domain, 1567194118Sjamie sizeof(pr->pr_domainname)); 1568193066Sjamie if (uuid != NULL) 1569194118Sjamie strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid)); 1570193066Sjamie if (gothid) 1571193066Sjamie pr->pr_hostid = hid; 1572193066Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1573193066Sjamie if (tpr->pr_flags & PR_HOST) 1574193066Sjamie descend = 0; 1575193066Sjamie else { 1576193066Sjamie if (host != NULL) 1577194118Sjamie strlcpy(tpr->pr_hostname, 1578194118Sjamie pr->pr_hostname, 1579194118Sjamie sizeof(tpr->pr_hostname)); 1580193066Sjamie if (domain != NULL) 1581194118Sjamie strlcpy(tpr->pr_domainname, 1582194118Sjamie pr->pr_domainname, 1583194118Sjamie sizeof(tpr->pr_domainname)); 1584193066Sjamie if (uuid != NULL) 1585194118Sjamie strlcpy(tpr->pr_hostuuid, 1586194118Sjamie pr->pr_hostuuid, 1587194118Sjamie sizeof(tpr->pr_hostuuid)); 1588193066Sjamie if (gothid) 1589193066Sjamie tpr->pr_hostid = hid; 1590193066Sjamie } 1591193066Sjamie } 1592193066Sjamie } 1593192895Sjamie if ((tallow = ch_allow & ~pr_allow)) { 1594192895Sjamie /* Clear allow bits in all children. */ 1595192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1596192895Sjamie tpr->pr_allow &= ~tallow; 1597192895Sjamie } 1598192895Sjamie pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow; 1599191673Sjamie /* 1600191673Sjamie * Persistent prisons get an extra reference, and prisons losing their 1601191673Sjamie * persist flag lose that reference. Only do this for existing prisons 1602191673Sjamie * for now, so new ones will remain unseen until after the module 1603191673Sjamie * handlers have completed. 1604191673Sjamie */ 1605191673Sjamie if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) { 1606191673Sjamie if (pr_flags & PR_PERSIST) { 1607191673Sjamie pr->pr_ref++; 1608191673Sjamie pr->pr_uref++; 1609191673Sjamie } else { 1610191673Sjamie pr->pr_ref--; 1611191673Sjamie pr->pr_uref--; 1612191673Sjamie } 1613191673Sjamie } 1614191673Sjamie pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags; 1615191673Sjamie mtx_unlock(&pr->pr_mtx); 1616185435Sbz 1617192895Sjamie /* Locks may have prevented a complete restriction of child IP 1618192895Sjamie * addresses. If so, allocate some more memory and try again. 1619192895Sjamie */ 1620192895Sjamie#ifdef INET 1621192895Sjamie while (redo_ip4) { 1622192895Sjamie ip4s = pr->pr_ip4s; 1623192895Sjamie ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 1624192895Sjamie mtx_lock(&pr->pr_mtx); 1625192895Sjamie redo_ip4 = 0; 1626192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1627195945Sjamie#ifdef VIMAGE 1628195945Sjamie if (tpr->pr_flags & PR_VNET) { 1629195945Sjamie descend = 0; 1630195945Sjamie continue; 1631195945Sjamie } 1632195945Sjamie#endif 1633192895Sjamie if (prison_restrict_ip4(tpr, ip4)) { 1634192895Sjamie if (ip4 != NULL) 1635192895Sjamie ip4 = NULL; 1636192895Sjamie else 1637192895Sjamie redo_ip4 = 1; 1638192895Sjamie } 1639192895Sjamie } 1640192895Sjamie mtx_unlock(&pr->pr_mtx); 1641192895Sjamie } 1642192895Sjamie#endif 1643192895Sjamie#ifdef INET6 1644192895Sjamie while (redo_ip6) { 1645192895Sjamie ip6s = pr->pr_ip6s; 1646192895Sjamie ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 1647192895Sjamie mtx_lock(&pr->pr_mtx); 1648192895Sjamie redo_ip6 = 0; 1649192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1650195945Sjamie#ifdef VIMAGE 1651195945Sjamie if (tpr->pr_flags & PR_VNET) { 1652195945Sjamie descend = 0; 1653195945Sjamie continue; 1654195945Sjamie } 1655195945Sjamie#endif 1656192895Sjamie if (prison_restrict_ip6(tpr, ip6)) { 1657192895Sjamie if (ip6 != NULL) 1658192895Sjamie ip6 = NULL; 1659192895Sjamie else 1660192895Sjamie redo_ip6 = 1; 1661192895Sjamie } 1662192895Sjamie } 1663192895Sjamie mtx_unlock(&pr->pr_mtx); 1664192895Sjamie } 1665192895Sjamie#endif 1666192895Sjamie 1667191673Sjamie /* Let the modules do their work. */ 1668191673Sjamie sx_downgrade(&allprison_lock); 1669191673Sjamie if (created) { 1670191673Sjamie error = osd_jail_call(pr, PR_METHOD_CREATE, opts); 1671191673Sjamie if (error) { 1672191673Sjamie prison_deref(pr, PD_LIST_SLOCKED); 1673191673Sjamie goto done_errmsg; 1674191673Sjamie } 1675191673Sjamie } 1676191673Sjamie error = osd_jail_call(pr, PR_METHOD_SET, opts); 1677191673Sjamie if (error) { 1678191673Sjamie prison_deref(pr, created 1679191673Sjamie ? PD_LIST_SLOCKED 1680191673Sjamie : PD_DEREF | PD_LIST_SLOCKED); 1681191673Sjamie goto done_errmsg; 1682191673Sjamie } 1683191673Sjamie 1684191673Sjamie /* Attach this process to the prison if requested. */ 1685191673Sjamie if (flags & JAIL_ATTACH) { 1686191673Sjamie mtx_lock(&pr->pr_mtx); 1687191673Sjamie error = do_jail_attach(td, pr); 1688191673Sjamie if (error) { 1689191673Sjamie vfs_opterror(opts, "attach failed"); 1690191673Sjamie if (!created) 1691191673Sjamie prison_deref(pr, PD_DEREF); 1692191673Sjamie goto done_errmsg; 1693191673Sjamie } 1694191673Sjamie } 1695191673Sjamie 1696191673Sjamie /* 1697191673Sjamie * Now that it is all there, drop the temporary reference from existing 1698191673Sjamie * prisons. Or add a reference to newly created persistent prisons 1699191673Sjamie * (which was not done earlier so that the prison would not be publicly 1700191673Sjamie * visible). 1701191673Sjamie */ 1702191673Sjamie if (!created) { 1703191673Sjamie prison_deref(pr, (flags & JAIL_ATTACH) 1704191673Sjamie ? PD_DEREF 1705191673Sjamie : PD_DEREF | PD_LIST_SLOCKED); 1706191673Sjamie } else { 1707191673Sjamie if (pr_flags & PR_PERSIST) { 1708191673Sjamie mtx_lock(&pr->pr_mtx); 1709191673Sjamie pr->pr_ref++; 1710191673Sjamie pr->pr_uref++; 1711191673Sjamie mtx_unlock(&pr->pr_mtx); 1712191673Sjamie } 1713191673Sjamie if (!(flags & JAIL_ATTACH)) 1714191673Sjamie sx_sunlock(&allprison_lock); 1715191673Sjamie } 1716191673Sjamie td->td_retval[0] = pr->pr_id; 1717191673Sjamie goto done_errmsg; 1718191673Sjamie 1719192895Sjamie done_deref_locked: 1720192895Sjamie prison_deref(pr, created 1721192895Sjamie ? PD_LOCKED | PD_LIST_XLOCKED 1722192895Sjamie : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 1723192895Sjamie goto done_releroot; 1724191673Sjamie done_unlock_list: 1725191673Sjamie sx_xunlock(&allprison_lock); 1726191673Sjamie done_releroot: 1727191673Sjamie if (root != NULL) { 1728191673Sjamie vfslocked = VFS_LOCK_GIANT(root->v_mount); 1729191673Sjamie vrele(root); 1730191673Sjamie VFS_UNLOCK_GIANT(vfslocked); 1731191673Sjamie } 1732191673Sjamie done_errmsg: 1733191673Sjamie if (error) { 1734191673Sjamie vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 1735191673Sjamie if (errmsg_len > 0) { 1736191673Sjamie errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1; 1737191673Sjamie if (errmsg_pos > 0) { 1738191673Sjamie if (optuio->uio_segflg == UIO_SYSSPACE) 1739191673Sjamie bcopy(errmsg, 1740191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 1741191673Sjamie errmsg_len); 1742191673Sjamie else 1743191673Sjamie copyout(errmsg, 1744191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 1745191673Sjamie errmsg_len); 1746191673Sjamie } 1747191673Sjamie } 1748191673Sjamie } 1749191673Sjamie done_free: 1750191673Sjamie#ifdef INET 1751191673Sjamie free(ip4, M_PRISON); 1752191673Sjamie#endif 1753191673Sjamie#ifdef INET6 1754191673Sjamie free(ip6, M_PRISON); 1755191673Sjamie#endif 1756191673Sjamie vfs_freeopts(opts); 1757191673Sjamie return (error); 1758191673Sjamie} 1759191673Sjamie 1760191673Sjamie 176182710Sdillon/* 1762191673Sjamie * struct jail_get_args { 1763191673Sjamie * struct iovec *iovp; 1764191673Sjamie * unsigned int iovcnt; 1765191673Sjamie * int flags; 1766114168Smike * }; 176782710Sdillon */ 176846155Sphkint 1769191673Sjamiejail_get(struct thread *td, struct jail_get_args *uap) 177046155Sphk{ 1771191673Sjamie struct uio *auio; 1772185435Sbz int error; 1773185435Sbz 1774191673Sjamie /* Check that we have an even number of iovecs. */ 1775191673Sjamie if (uap->iovcnt & 1) 1776191673Sjamie return (EINVAL); 1777191673Sjamie 1778191673Sjamie error = copyinuio(uap->iovp, uap->iovcnt, &auio); 1779185435Sbz if (error) 1780185435Sbz return (error); 1781191673Sjamie error = kern_jail_get(td, auio, uap->flags); 1782191673Sjamie if (error == 0) 1783191673Sjamie error = copyout(auio->uio_iov, uap->iovp, 1784191673Sjamie uap->iovcnt * sizeof (struct iovec)); 1785191673Sjamie free(auio, M_IOV); 1786191673Sjamie return (error); 1787191673Sjamie} 1788185435Sbz 1789191673Sjamieint 1790191673Sjamiekern_jail_get(struct thread *td, struct uio *optuio, int flags) 1791191673Sjamie{ 1792192895Sjamie struct prison *pr, *mypr; 1793191673Sjamie struct vfsopt *opt; 1794191673Sjamie struct vfsoptlist *opts; 1795191673Sjamie char *errmsg, *name; 1796192895Sjamie int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos; 1797185435Sbz 1798191673Sjamie if (flags & ~JAIL_GET_MASK) 1799191673Sjamie return (EINVAL); 1800185435Sbz 1801191673Sjamie /* Get the parameter list. */ 1802191673Sjamie error = vfs_buildopts(optuio, &opts); 1803191673Sjamie if (error) 1804191673Sjamie return (error); 1805191673Sjamie errmsg_pos = vfs_getopt_pos(opts, "errmsg"); 1806192895Sjamie mypr = td->td_ucred->cr_prison; 1807185435Sbz 1808191673Sjamie /* 1809191673Sjamie * Find the prison specified by one of: lastjid, jid, name. 1810191673Sjamie */ 1811191673Sjamie sx_slock(&allprison_lock); 1812191673Sjamie error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid)); 1813191673Sjamie if (error == 0) { 1814191673Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) { 1815192895Sjamie if (pr->pr_id > jid && prison_ischild(mypr, pr)) { 1816191673Sjamie mtx_lock(&pr->pr_mtx); 1817191673Sjamie if (pr->pr_ref > 0 && 1818191673Sjamie (pr->pr_uref > 0 || (flags & JAIL_DYING))) 1819191673Sjamie break; 1820191673Sjamie mtx_unlock(&pr->pr_mtx); 1821191673Sjamie } 1822191673Sjamie } 1823191673Sjamie if (pr != NULL) 1824191673Sjamie goto found_prison; 1825191673Sjamie error = ENOENT; 1826191673Sjamie vfs_opterror(opts, "no jail after %d", jid); 1827191673Sjamie goto done_unlock_list; 1828191673Sjamie } else if (error != ENOENT) 1829191673Sjamie goto done_unlock_list; 1830185435Sbz 1831191673Sjamie error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 1832191673Sjamie if (error == 0) { 1833191673Sjamie if (jid != 0) { 1834192895Sjamie pr = prison_find_child(mypr, jid); 1835191673Sjamie if (pr != NULL) { 1836191673Sjamie if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 1837191673Sjamie mtx_unlock(&pr->pr_mtx); 1838191673Sjamie error = ENOENT; 1839191673Sjamie vfs_opterror(opts, "jail %d is dying", 1840191673Sjamie jid); 1841191673Sjamie goto done_unlock_list; 1842191673Sjamie } 1843191673Sjamie goto found_prison; 1844191673Sjamie } 1845191673Sjamie error = ENOENT; 1846191673Sjamie vfs_opterror(opts, "jail %d not found", jid); 1847191673Sjamie goto done_unlock_list; 1848191673Sjamie } 1849191673Sjamie } else if (error != ENOENT) 1850191673Sjamie goto done_unlock_list; 185146155Sphk 1852191673Sjamie error = vfs_getopt(opts, "name", (void **)&name, &len); 1853191673Sjamie if (error == 0) { 1854191673Sjamie if (len == 0 || name[len - 1] != '\0') { 1855191673Sjamie error = EINVAL; 1856191673Sjamie goto done_unlock_list; 1857191673Sjamie } 1858192895Sjamie pr = prison_find_name(mypr, name); 1859191673Sjamie if (pr != NULL) { 1860191673Sjamie if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 1861191673Sjamie mtx_unlock(&pr->pr_mtx); 1862191673Sjamie error = ENOENT; 1863191673Sjamie vfs_opterror(opts, "jail \"%s\" is dying", 1864191673Sjamie name); 1865191673Sjamie goto done_unlock_list; 1866191673Sjamie } 1867191673Sjamie goto found_prison; 1868191673Sjamie } 1869191673Sjamie error = ENOENT; 1870191673Sjamie vfs_opterror(opts, "jail \"%s\" not found", name); 1871191673Sjamie goto done_unlock_list; 1872191673Sjamie } else if (error != ENOENT) 1873191673Sjamie goto done_unlock_list; 1874185435Sbz 1875191673Sjamie vfs_opterror(opts, "no jail specified"); 1876191673Sjamie error = ENOENT; 1877191673Sjamie goto done_unlock_list; 1878191673Sjamie 1879191673Sjamie found_prison: 1880191673Sjamie /* Get the parameters of the prison. */ 1881191673Sjamie pr->pr_ref++; 1882191673Sjamie locked = PD_LOCKED; 1883191673Sjamie td->td_retval[0] = pr->pr_id; 1884191673Sjamie error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id)); 1885191673Sjamie if (error != 0 && error != ENOENT) 1886191673Sjamie goto done_deref; 1887192895Sjamie i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id; 1888192895Sjamie error = vfs_setopt(opts, "parent", &i, sizeof(i)); 1889191673Sjamie if (error != 0 && error != ENOENT) 1890191673Sjamie goto done_deref; 1891192895Sjamie error = vfs_setopts(opts, "name", prison_name(mypr, pr)); 1892192895Sjamie if (error != 0 && error != ENOENT) 1893192895Sjamie goto done_deref; 1894192895Sjamie error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id, 1895191673Sjamie sizeof(pr->pr_cpuset->cs_id)); 1896191673Sjamie if (error != 0 && error != ENOENT) 1897191673Sjamie goto done_deref; 1898192895Sjamie error = vfs_setopts(opts, "path", prison_path(mypr, pr)); 1899191673Sjamie if (error != 0 && error != ENOENT) 1900191673Sjamie goto done_deref; 1901191673Sjamie#ifdef INET 1902191673Sjamie error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4, 1903191673Sjamie pr->pr_ip4s * sizeof(*pr->pr_ip4)); 1904191673Sjamie if (error != 0 && error != ENOENT) 1905191673Sjamie goto done_deref; 1906191673Sjamie#endif 1907191673Sjamie#ifdef INET6 1908191673Sjamie error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6, 1909191673Sjamie pr->pr_ip6s * sizeof(*pr->pr_ip6)); 1910191673Sjamie if (error != 0 && error != ENOENT) 1911191673Sjamie goto done_deref; 1912191673Sjamie#endif 1913191673Sjamie error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel, 1914191673Sjamie sizeof(pr->pr_securelevel)); 1915191673Sjamie if (error != 0 && error != ENOENT) 1916191673Sjamie goto done_deref; 1917194762Sjamie error = vfs_setopt(opts, "children.cur", &pr->pr_childcount, 1918194762Sjamie sizeof(pr->pr_childcount)); 1919194762Sjamie if (error != 0 && error != ENOENT) 1920194762Sjamie goto done_deref; 1921194762Sjamie error = vfs_setopt(opts, "children.max", &pr->pr_childmax, 1922194762Sjamie sizeof(pr->pr_childmax)); 1923194762Sjamie if (error != 0 && error != ENOENT) 1924194762Sjamie goto done_deref; 1925194118Sjamie error = vfs_setopts(opts, "host.hostname", pr->pr_hostname); 1926191673Sjamie if (error != 0 && error != ENOENT) 1927191673Sjamie goto done_deref; 1928194118Sjamie error = vfs_setopts(opts, "host.domainname", pr->pr_domainname); 1929193066Sjamie if (error != 0 && error != ENOENT) 1930193066Sjamie goto done_deref; 1931194118Sjamie error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid); 1932193066Sjamie if (error != 0 && error != ENOENT) 1933193066Sjamie goto done_deref; 1934193066Sjamie#ifdef COMPAT_IA32 1935193066Sjamie if (td->td_proc->p_sysent->sv_flags & SV_IA32) { 1936193066Sjamie uint32_t hid32 = pr->pr_hostid; 1937193066Sjamie 1938193066Sjamie error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32)); 1939193066Sjamie } else 1940193066Sjamie#endif 1941193066Sjamie error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid, 1942193066Sjamie sizeof(pr->pr_hostid)); 1943193066Sjamie if (error != 0 && error != ENOENT) 1944193066Sjamie goto done_deref; 1945192895Sjamie error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs, 1946192895Sjamie sizeof(pr->pr_enforce_statfs)); 1947191673Sjamie if (error != 0 && error != ENOENT) 1948191673Sjamie goto done_deref; 1949192895Sjamie for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 1950192895Sjamie fi++) { 1951192895Sjamie if (pr_flag_names[fi] == NULL) 1952192895Sjamie continue; 1953192895Sjamie i = (pr->pr_flags & (1 << fi)) ? 1 : 0; 1954192895Sjamie error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i)); 1955192895Sjamie if (error != 0 && error != ENOENT) 1956192895Sjamie goto done_deref; 1957192895Sjamie i = !i; 1958192895Sjamie error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i)); 1959192895Sjamie if (error != 0 && error != ENOENT) 1960192895Sjamie goto done_deref; 1961192895Sjamie } 1962195870Sjamie for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 1963195870Sjamie fi++) { 1964195870Sjamie i = pr->pr_flags & 1965195870Sjamie (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new); 1966195870Sjamie i = pr_flag_jailsys[fi].disable && 1967195870Sjamie (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE 1968195870Sjamie : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW 1969195870Sjamie : JAIL_SYS_INHERIT; 1970195870Sjamie error = 1971195870Sjamie vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i)); 1972195870Sjamie if (error != 0 && error != ENOENT) 1973195870Sjamie goto done_deref; 1974195870Sjamie } 1975192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 1976192895Sjamie fi++) { 1977192895Sjamie if (pr_allow_names[fi] == NULL) 1978192895Sjamie continue; 1979192895Sjamie i = (pr->pr_allow & (1 << fi)) ? 1 : 0; 1980192895Sjamie error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i)); 1981192895Sjamie if (error != 0 && error != ENOENT) 1982192895Sjamie goto done_deref; 1983192895Sjamie i = !i; 1984192895Sjamie error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i)); 1985192895Sjamie if (error != 0 && error != ENOENT) 1986192895Sjamie goto done_deref; 1987192895Sjamie } 1988191673Sjamie i = (pr->pr_uref == 0); 1989191673Sjamie error = vfs_setopt(opts, "dying", &i, sizeof(i)); 1990191673Sjamie if (error != 0 && error != ENOENT) 1991191673Sjamie goto done_deref; 1992191673Sjamie i = !i; 1993191673Sjamie error = vfs_setopt(opts, "nodying", &i, sizeof(i)); 1994191673Sjamie if (error != 0 && error != ENOENT) 1995191673Sjamie goto done_deref; 1996191673Sjamie 1997191673Sjamie /* Get the module parameters. */ 1998191673Sjamie mtx_unlock(&pr->pr_mtx); 1999191673Sjamie locked = 0; 2000191673Sjamie error = osd_jail_call(pr, PR_METHOD_GET, opts); 200146155Sphk if (error) 2002191673Sjamie goto done_deref; 2003191673Sjamie prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED); 200484828Sjhb 2005191673Sjamie /* By now, all parameters should have been noted. */ 2006191673Sjamie TAILQ_FOREACH(opt, opts, link) { 2007191673Sjamie if (!opt->seen && strcmp(opt->name, "errmsg")) { 2008191673Sjamie error = EINVAL; 2009191673Sjamie vfs_opterror(opts, "unknown parameter: %s", opt->name); 2010191673Sjamie goto done_errmsg; 2011191673Sjamie } 2012185435Sbz } 2013191673Sjamie 2014191673Sjamie /* Write the fetched parameters back to userspace. */ 2015191673Sjamie error = 0; 2016191673Sjamie TAILQ_FOREACH(opt, opts, link) { 2017191673Sjamie if (opt->pos >= 0 && opt->pos != errmsg_pos) { 2018191673Sjamie pos = 2 * opt->pos + 1; 2019191673Sjamie optuio->uio_iov[pos].iov_len = opt->len; 2020191673Sjamie if (opt->value != NULL) { 2021191673Sjamie if (optuio->uio_segflg == UIO_SYSSPACE) { 2022191673Sjamie bcopy(opt->value, 2023191673Sjamie optuio->uio_iov[pos].iov_base, 2024191673Sjamie opt->len); 2025191673Sjamie } else { 2026191673Sjamie error = copyout(opt->value, 2027191673Sjamie optuio->uio_iov[pos].iov_base, 2028191673Sjamie opt->len); 2029191673Sjamie if (error) 2030191673Sjamie break; 2031191673Sjamie } 2032191673Sjamie } 2033191673Sjamie } 2034185435Sbz } 2035191673Sjamie goto done_errmsg; 2036191673Sjamie 2037191673Sjamie done_deref: 2038191673Sjamie prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED); 2039191673Sjamie goto done_errmsg; 2040191673Sjamie 2041191673Sjamie done_unlock_list: 2042191673Sjamie sx_sunlock(&allprison_lock); 2043191673Sjamie done_errmsg: 2044191673Sjamie if (error && errmsg_pos >= 0) { 2045191673Sjamie vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 2046191673Sjamie errmsg_pos = 2 * errmsg_pos + 1; 2047191673Sjamie if (errmsg_len > 0) { 2048191673Sjamie if (optuio->uio_segflg == UIO_SYSSPACE) 2049191673Sjamie bcopy(errmsg, 2050191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 2051191673Sjamie errmsg_len); 2052191673Sjamie else 2053191673Sjamie copyout(errmsg, 2054191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 2055191673Sjamie errmsg_len); 2056191673Sjamie } 2057185435Sbz } 2058191673Sjamie vfs_freeopts(opts); 2059191673Sjamie return (error); 2060191673Sjamie} 2061113275Smike 2062192895Sjamie 2063191673Sjamie/* 2064191673Sjamie * struct jail_remove_args { 2065191673Sjamie * int jid; 2066191673Sjamie * }; 2067191673Sjamie */ 2068191673Sjamieint 2069191673Sjamiejail_remove(struct thread *td, struct jail_remove_args *uap) 2070191673Sjamie{ 2071192895Sjamie struct prison *pr, *cpr, *lpr, *tpr; 2072192895Sjamie int descend, error; 2073185435Sbz 2074191673Sjamie error = priv_check(td, PRIV_JAIL_REMOVE); 2075185435Sbz if (error) 2076191673Sjamie return (error); 2077185435Sbz 2078185435Sbz sx_xlock(&allprison_lock); 2079192895Sjamie pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2080191673Sjamie if (pr == NULL) { 2081185435Sbz sx_xunlock(&allprison_lock); 2082191673Sjamie return (EINVAL); 2083185435Sbz } 2084185435Sbz 2085192895Sjamie /* Remove all descendants of this prison, then remove this prison. */ 2086192895Sjamie pr->pr_ref++; 2087192895Sjamie pr->pr_flags |= PR_REMOVE; 2088192895Sjamie if (!LIST_EMPTY(&pr->pr_children)) { 2089192895Sjamie mtx_unlock(&pr->pr_mtx); 2090192895Sjamie lpr = NULL; 2091192895Sjamie FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 2092192895Sjamie mtx_lock(&cpr->pr_mtx); 2093192895Sjamie if (cpr->pr_ref > 0) { 2094192895Sjamie tpr = cpr; 2095192895Sjamie cpr->pr_ref++; 2096192895Sjamie cpr->pr_flags |= PR_REMOVE; 2097192895Sjamie } else { 2098192895Sjamie /* Already removed - do not do it again. */ 2099192895Sjamie tpr = NULL; 2100192895Sjamie } 2101192895Sjamie mtx_unlock(&cpr->pr_mtx); 2102192895Sjamie if (lpr != NULL) { 2103192895Sjamie mtx_lock(&lpr->pr_mtx); 2104192895Sjamie prison_remove_one(lpr); 2105192895Sjamie sx_xlock(&allprison_lock); 2106192895Sjamie } 2107192895Sjamie lpr = tpr; 2108192895Sjamie } 2109192895Sjamie if (lpr != NULL) { 2110192895Sjamie mtx_lock(&lpr->pr_mtx); 2111192895Sjamie prison_remove_one(lpr); 2112192895Sjamie sx_xlock(&allprison_lock); 2113192895Sjamie } 2114192895Sjamie mtx_lock(&pr->pr_mtx); 2115192895Sjamie } 2116192895Sjamie prison_remove_one(pr); 2117192895Sjamie return (0); 2118192895Sjamie} 2119192895Sjamie 2120192895Sjamiestatic void 2121192895Sjamieprison_remove_one(struct prison *pr) 2122192895Sjamie{ 2123192895Sjamie struct proc *p; 2124192895Sjamie int deuref; 2125192895Sjamie 2126191673Sjamie /* If the prison was persistent, it is not anymore. */ 2127191673Sjamie deuref = 0; 2128191673Sjamie if (pr->pr_flags & PR_PERSIST) { 2129191673Sjamie pr->pr_ref--; 2130191673Sjamie deuref = PD_DEUREF; 2131191673Sjamie pr->pr_flags &= ~PR_PERSIST; 2132179881Sdelphij } 2133113275Smike 2134192895Sjamie /* 2135192895Sjamie * jail_remove added a reference. If that's the only one, remove 2136192895Sjamie * the prison now. 2137192895Sjamie */ 2138192895Sjamie KASSERT(pr->pr_ref > 0, 2139192895Sjamie ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id)); 2140192895Sjamie if (pr->pr_ref == 1) { 2141191673Sjamie prison_deref(pr, 2142191673Sjamie deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 2143192895Sjamie return; 2144191673Sjamie } 2145191673Sjamie 2146113275Smike mtx_unlock(&pr->pr_mtx); 2147191673Sjamie sx_xunlock(&allprison_lock); 2148191673Sjamie /* 2149191673Sjamie * Kill all processes unfortunate enough to be attached to this prison. 2150191673Sjamie */ 2151191673Sjamie sx_slock(&allproc_lock); 2152191673Sjamie LIST_FOREACH(p, &allproc, p_list) { 2153191673Sjamie PROC_LOCK(p); 2154191673Sjamie if (p->p_state != PRS_NEW && p->p_ucred && 2155191673Sjamie p->p_ucred->cr_prison == pr) 2156191673Sjamie psignal(p, SIGKILL); 2157191673Sjamie PROC_UNLOCK(p); 2158191673Sjamie } 2159191673Sjamie sx_sunlock(&allproc_lock); 2160192895Sjamie /* Remove the temporary reference added by jail_remove. */ 2161191673Sjamie prison_deref(pr, deuref | PD_DEREF); 2162113275Smike} 2163113275Smike 2164190466Sjamie 2165113275Smike/* 2166114168Smike * struct jail_attach_args { 2167114168Smike * int jid; 2168114168Smike * }; 2169113275Smike */ 2170113275Smikeint 2171114168Smikejail_attach(struct thread *td, struct jail_attach_args *uap) 2172113275Smike{ 2173113275Smike struct prison *pr; 2174191673Sjamie int error; 2175167309Spjd 2176164032Srwatson error = priv_check(td, PRIV_JAIL_ATTACH); 2177126023Snectar if (error) 2178126023Snectar return (error); 2179126023Snectar 2180168401Spjd sx_slock(&allprison_lock); 2181192895Sjamie pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2182113275Smike if (pr == NULL) { 2183168401Spjd sx_sunlock(&allprison_lock); 2184113275Smike return (EINVAL); 2185113275Smike } 2186185435Sbz 2187185435Sbz /* 2188185435Sbz * Do not allow a process to attach to a prison that is not 2189191673Sjamie * considered to be "alive". 2190185435Sbz */ 2191191673Sjamie if (pr->pr_uref == 0) { 2192185435Sbz mtx_unlock(&pr->pr_mtx); 2193185435Sbz sx_sunlock(&allprison_lock); 2194185435Sbz return (EINVAL); 2195185435Sbz } 2196191673Sjamie 2197191673Sjamie return (do_jail_attach(td, pr)); 2198191673Sjamie} 2199191673Sjamie 2200191673Sjamiestatic int 2201191673Sjamiedo_jail_attach(struct thread *td, struct prison *pr) 2202191673Sjamie{ 2203192895Sjamie struct prison *ppr; 2204191673Sjamie struct proc *p; 2205191673Sjamie struct ucred *newcred, *oldcred; 2206191673Sjamie int vfslocked, error; 2207191673Sjamie 2208191673Sjamie /* 2209191673Sjamie * XXX: Note that there is a slight race here if two threads 2210191673Sjamie * in the same privileged process attempt to attach to two 2211191673Sjamie * different jails at the same time. It is important for 2212191673Sjamie * user processes not to do this, or they might end up with 2213191673Sjamie * a process root from one prison, but attached to the jail 2214191673Sjamie * of another. 2215191673Sjamie */ 2216113275Smike pr->pr_ref++; 2217191673Sjamie pr->pr_uref++; 2218113275Smike mtx_unlock(&pr->pr_mtx); 2219191673Sjamie 2220191673Sjamie /* Let modules do whatever they need to prepare for attaching. */ 2221191673Sjamie error = osd_jail_call(pr, PR_METHOD_ATTACH, td); 2222191673Sjamie if (error) { 2223191673Sjamie prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED); 2224191673Sjamie return (error); 2225191673Sjamie } 2226168401Spjd sx_sunlock(&allprison_lock); 2227113275Smike 2228185435Sbz /* 2229185435Sbz * Reparent the newly attached process to this jail. 2230185435Sbz */ 2231192895Sjamie ppr = td->td_ucred->cr_prison; 2232191673Sjamie p = td->td_proc; 2233185435Sbz error = cpuset_setproc_update_set(p, pr->pr_cpuset); 2234185435Sbz if (error) 2235191673Sjamie goto e_revert_osd; 2236185435Sbz 2237150652Scsjp vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount); 2238175202Sattilio vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY); 2239113275Smike if ((error = change_dir(pr->pr_root, td)) != 0) 2240113275Smike goto e_unlock; 2241113275Smike#ifdef MAC 2242172930Srwatson if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root))) 2243113275Smike goto e_unlock; 2244113275Smike#endif 2245175294Sattilio VOP_UNLOCK(pr->pr_root, 0); 2246191673Sjamie if ((error = change_root(pr->pr_root, td))) 2247191673Sjamie goto e_unlock_giant; 2248150652Scsjp VFS_UNLOCK_GIANT(vfslocked); 2249113275Smike 225084828Sjhb newcred = crget(); 225184828Sjhb PROC_LOCK(p); 225284828Sjhb oldcred = p->p_ucred; 2253113275Smike setsugid(p); 225484828Sjhb crcopy(newcred, oldcred); 2255113630Sjhb newcred->cr_prison = pr; 225684828Sjhb p->p_ucred = newcred; 225784828Sjhb PROC_UNLOCK(p); 225884828Sjhb crfree(oldcred); 2259192895Sjamie prison_deref(ppr, PD_DEREF | PD_DEUREF); 226046155Sphk return (0); 2261191673Sjamie e_unlock: 2262175294Sattilio VOP_UNLOCK(pr->pr_root, 0); 2263191673Sjamie e_unlock_giant: 2264150652Scsjp VFS_UNLOCK_GIANT(vfslocked); 2265191673Sjamie e_revert_osd: 2266191673Sjamie /* Tell modules this thread is still in its old jail after all. */ 2267192895Sjamie (void)osd_jail_call(ppr, PR_METHOD_ATTACH, td); 2268191673Sjamie prison_deref(pr, PD_DEREF | PD_DEUREF); 226946155Sphk return (error); 227046155Sphk} 227146155Sphk 2272192895Sjamie 2273113275Smike/* 2274113275Smike * Returns a locked prison instance, or NULL on failure. 2275113275Smike */ 2276168399Spjdstruct prison * 2277113275Smikeprison_find(int prid) 2278113275Smike{ 2279113275Smike struct prison *pr; 2280113275Smike 2281168401Spjd sx_assert(&allprison_lock, SX_LOCKED); 2282191673Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) { 2283113275Smike if (pr->pr_id == prid) { 2284113275Smike mtx_lock(&pr->pr_mtx); 2285191673Sjamie if (pr->pr_ref > 0) 2286191673Sjamie return (pr); 2287191673Sjamie mtx_unlock(&pr->pr_mtx); 2288113275Smike } 2289113275Smike } 2290113275Smike return (NULL); 2291113275Smike} 2292113275Smike 2293191673Sjamie/* 2294192895Sjamie * Find a prison that is a descendant of mypr. Returns a locked prison or NULL. 2295191673Sjamie */ 2296191673Sjamiestruct prison * 2297192895Sjamieprison_find_child(struct prison *mypr, int prid) 2298191673Sjamie{ 2299192895Sjamie struct prison *pr; 2300192895Sjamie int descend; 2301192895Sjamie 2302192895Sjamie sx_assert(&allprison_lock, SX_LOCKED); 2303192895Sjamie FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 2304192895Sjamie if (pr->pr_id == prid) { 2305192895Sjamie mtx_lock(&pr->pr_mtx); 2306192895Sjamie if (pr->pr_ref > 0) 2307192895Sjamie return (pr); 2308192895Sjamie mtx_unlock(&pr->pr_mtx); 2309192895Sjamie } 2310192895Sjamie } 2311192895Sjamie return (NULL); 2312192895Sjamie} 2313192895Sjamie 2314192895Sjamie/* 2315192895Sjamie * Look for the name relative to mypr. Returns a locked prison or NULL. 2316192895Sjamie */ 2317192895Sjamiestruct prison * 2318192895Sjamieprison_find_name(struct prison *mypr, const char *name) 2319192895Sjamie{ 2320191673Sjamie struct prison *pr, *deadpr; 2321192895Sjamie size_t mylen; 2322192895Sjamie int descend; 2323191673Sjamie 2324191673Sjamie sx_assert(&allprison_lock, SX_LOCKED); 2325192895Sjamie mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1; 2326191673Sjamie again: 2327191673Sjamie deadpr = NULL; 2328192895Sjamie FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 2329192895Sjamie if (!strcmp(pr->pr_name + mylen, name)) { 2330191673Sjamie mtx_lock(&pr->pr_mtx); 2331191673Sjamie if (pr->pr_ref > 0) { 2332191673Sjamie if (pr->pr_uref > 0) 2333191673Sjamie return (pr); 2334191673Sjamie deadpr = pr; 2335191673Sjamie } 2336191673Sjamie mtx_unlock(&pr->pr_mtx); 2337191673Sjamie } 2338191673Sjamie } 2339192895Sjamie /* There was no valid prison - perhaps there was a dying one. */ 2340191673Sjamie if (deadpr != NULL) { 2341191673Sjamie mtx_lock(&deadpr->pr_mtx); 2342191673Sjamie if (deadpr->pr_ref == 0) { 2343191673Sjamie mtx_unlock(&deadpr->pr_mtx); 2344191673Sjamie goto again; 2345191673Sjamie } 2346191673Sjamie } 2347191673Sjamie return (deadpr); 2348191673Sjamie} 2349191673Sjamie 2350191673Sjamie/* 2351192895Sjamie * See if a prison has the specific flag set. 2352192895Sjamie */ 2353192895Sjamieint 2354192895Sjamieprison_flag(struct ucred *cred, unsigned flag) 2355192895Sjamie{ 2356192895Sjamie 2357192895Sjamie /* This is an atomic read, so no locking is necessary. */ 2358192895Sjamie return (cred->cr_prison->pr_flags & flag); 2359192895Sjamie} 2360192895Sjamie 2361192895Sjamieint 2362192895Sjamieprison_allow(struct ucred *cred, unsigned flag) 2363192895Sjamie{ 2364192895Sjamie 2365192895Sjamie /* This is an atomic read, so no locking is necessary. */ 2366192895Sjamie return (cred->cr_prison->pr_allow & flag); 2367192895Sjamie} 2368192895Sjamie 2369192895Sjamie/* 2370191673Sjamie * Remove a prison reference. If that was the last reference, remove the 2371191673Sjamie * prison itself - but not in this context in case there are locks held. 2372191673Sjamie */ 237372786Srwatsonvoid 2374185029Spjdprison_free_locked(struct prison *pr) 237572786Srwatson{ 237672786Srwatson 2377185029Spjd mtx_assert(&pr->pr_mtx, MA_OWNED); 237872786Srwatson pr->pr_ref--; 237972786Srwatson if (pr->pr_ref == 0) { 2380168483Spjd mtx_unlock(&pr->pr_mtx); 2381124882Srwatson TASK_INIT(&pr->pr_task, 0, prison_complete, pr); 2382144660Sjeff taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 238387275Srwatson return; 238472786Srwatson } 238587275Srwatson mtx_unlock(&pr->pr_mtx); 238672786Srwatson} 238772786Srwatson 2388185029Spjdvoid 2389185029Spjdprison_free(struct prison *pr) 2390185029Spjd{ 2391185029Spjd 2392185029Spjd mtx_lock(&pr->pr_mtx); 2393185029Spjd prison_free_locked(pr); 2394185029Spjd} 2395185029Spjd 2396124882Srwatsonstatic void 2397124882Srwatsonprison_complete(void *context, int pending) 2398124882Srwatson{ 2399191673Sjamie 2400191673Sjamie prison_deref((struct prison *)context, 0); 2401191673Sjamie} 2402191673Sjamie 2403191673Sjamie/* 2404191673Sjamie * Remove a prison reference (usually). This internal version assumes no 2405191673Sjamie * mutexes are held, except perhaps the prison itself. If there are no more 2406191673Sjamie * references, release and delist the prison. On completion, the prison lock 2407191673Sjamie * and the allprison lock are both unlocked. 2408191673Sjamie */ 2409191673Sjamiestatic void 2410191673Sjamieprison_deref(struct prison *pr, int flags) 2411191673Sjamie{ 2412192895Sjamie struct prison *ppr, *tpr; 2413150652Scsjp int vfslocked; 2414124882Srwatson 2415191673Sjamie if (!(flags & PD_LOCKED)) 2416191673Sjamie mtx_lock(&pr->pr_mtx); 2417192895Sjamie /* Decrement the user references in a separate loop. */ 2418191673Sjamie if (flags & PD_DEUREF) { 2419192895Sjamie for (tpr = pr;; tpr = tpr->pr_parent) { 2420192895Sjamie if (tpr != pr) 2421192895Sjamie mtx_lock(&tpr->pr_mtx); 2422192895Sjamie if (--tpr->pr_uref > 0) 2423192895Sjamie break; 2424192895Sjamie KASSERT(tpr != &prison0, ("prison0 pr_uref=0")); 2425192895Sjamie mtx_unlock(&tpr->pr_mtx); 2426192895Sjamie } 2427191673Sjamie /* Done if there were only user references to remove. */ 2428191673Sjamie if (!(flags & PD_DEREF)) { 2429192895Sjamie mtx_unlock(&tpr->pr_mtx); 2430191673Sjamie if (flags & PD_LIST_SLOCKED) 2431191673Sjamie sx_sunlock(&allprison_lock); 2432191673Sjamie else if (flags & PD_LIST_XLOCKED) 2433191673Sjamie sx_xunlock(&allprison_lock); 2434191673Sjamie return; 2435191673Sjamie } 2436192895Sjamie if (tpr != pr) { 2437192895Sjamie mtx_unlock(&tpr->pr_mtx); 2438192895Sjamie mtx_lock(&pr->pr_mtx); 2439192895Sjamie } 2440191673Sjamie } 2441124882Srwatson 2442192895Sjamie for (;;) { 2443192895Sjamie if (flags & PD_DEREF) 2444192895Sjamie pr->pr_ref--; 2445192895Sjamie /* If the prison still has references, nothing else to do. */ 2446192895Sjamie if (pr->pr_ref > 0) { 2447192895Sjamie mtx_unlock(&pr->pr_mtx); 2448192895Sjamie if (flags & PD_LIST_SLOCKED) 2449192895Sjamie sx_sunlock(&allprison_lock); 2450192895Sjamie else if (flags & PD_LIST_XLOCKED) 2451192895Sjamie sx_xunlock(&allprison_lock); 2452192895Sjamie return; 2453191673Sjamie } 2454191673Sjamie 2455192895Sjamie mtx_unlock(&pr->pr_mtx); 2456192895Sjamie if (flags & PD_LIST_SLOCKED) { 2457192895Sjamie if (!sx_try_upgrade(&allprison_lock)) { 2458192895Sjamie sx_sunlock(&allprison_lock); 2459192895Sjamie sx_xlock(&allprison_lock); 2460192895Sjamie } 2461192895Sjamie } else if (!(flags & PD_LIST_XLOCKED)) 2462192895Sjamie sx_xlock(&allprison_lock); 2463168489Spjd 2464192895Sjamie TAILQ_REMOVE(&allprison, pr, pr_list); 2465192895Sjamie LIST_REMOVE(pr, pr_sibling); 2466192895Sjamie ppr = pr->pr_parent; 2467192895Sjamie for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 2468194762Sjamie tpr->pr_childcount--; 2469196592Sjamie sx_xunlock(&allprison_lock); 2470192895Sjamie 2471194251Sjamie#ifdef VIMAGE 2472196505Szec if (pr->pr_vnet != ppr->pr_vnet) 2473194251Sjamie vnet_destroy(pr->pr_vnet); 2474194251Sjamie#endif 2475192895Sjamie if (pr->pr_root != NULL) { 2476192895Sjamie vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount); 2477192895Sjamie vrele(pr->pr_root); 2478192895Sjamie VFS_UNLOCK_GIANT(vfslocked); 2479192895Sjamie } 2480192895Sjamie mtx_destroy(&pr->pr_mtx); 2481191673Sjamie#ifdef INET 2482192895Sjamie free(pr->pr_ip4, M_PRISON); 2483191673Sjamie#endif 2484185435Sbz#ifdef INET6 2485192895Sjamie free(pr->pr_ip6, M_PRISON); 2486185435Sbz#endif 2487192895Sjamie if (pr->pr_cpuset != NULL) 2488192895Sjamie cpuset_rel(pr->pr_cpuset); 2489192895Sjamie osd_jail_exit(pr); 2490192895Sjamie free(pr, M_PRISON); 2491192895Sjamie 2492192895Sjamie /* Removing a prison frees a reference on its parent. */ 2493192895Sjamie pr = ppr; 2494192895Sjamie mtx_lock(&pr->pr_mtx); 2495196592Sjamie flags = PD_DEREF; 2496192895Sjamie } 2497124882Srwatson} 2498124882Srwatson 249972786Srwatsonvoid 2500185029Spjdprison_hold_locked(struct prison *pr) 250172786Srwatson{ 250272786Srwatson 2503185029Spjd mtx_assert(&pr->pr_mtx, MA_OWNED); 2504168489Spjd KASSERT(pr->pr_ref > 0, 2505191671Sjamie ("Trying to hold dead prison (jid=%d).", pr->pr_id)); 250672786Srwatson pr->pr_ref++; 2507185029Spjd} 2508185029Spjd 2509185029Spjdvoid 2510185029Spjdprison_hold(struct prison *pr) 2511185029Spjd{ 2512185029Spjd 2513185029Spjd mtx_lock(&pr->pr_mtx); 2514185029Spjd prison_hold_locked(pr); 251587275Srwatson mtx_unlock(&pr->pr_mtx); 251672786Srwatson} 251772786Srwatson 2518185435Sbzvoid 2519185435Sbzprison_proc_hold(struct prison *pr) 252087275Srwatson{ 252187275Srwatson 2522185435Sbz mtx_lock(&pr->pr_mtx); 2523191673Sjamie KASSERT(pr->pr_uref > 0, 2524191673Sjamie ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id)); 2525191673Sjamie pr->pr_uref++; 2526185435Sbz mtx_unlock(&pr->pr_mtx); 252787275Srwatson} 252887275Srwatson 2529185435Sbzvoid 2530185435Sbzprison_proc_free(struct prison *pr) 2531185435Sbz{ 2532185435Sbz 2533185435Sbz mtx_lock(&pr->pr_mtx); 2534191673Sjamie KASSERT(pr->pr_uref > 0, 2535191673Sjamie ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id)); 2536191673Sjamie prison_deref(pr, PD_DEUREF | PD_LOCKED); 2537185435Sbz} 2538185435Sbz 2539185435Sbz 2540185435Sbz#ifdef INET 2541185435Sbz/* 2542192895Sjamie * Restrict a prison's IP address list with its parent's, possibly replacing 2543192895Sjamie * it. Return true if the replacement buffer was used (or would have been). 2544192895Sjamie */ 2545192895Sjamiestatic int 2546192895Sjamieprison_restrict_ip4(struct prison *pr, struct in_addr *newip4) 2547192895Sjamie{ 2548192895Sjamie int ii, ij, used; 2549192895Sjamie struct prison *ppr; 2550192895Sjamie 2551192895Sjamie ppr = pr->pr_parent; 2552192895Sjamie if (!(pr->pr_flags & PR_IP4_USER)) { 2553192895Sjamie /* This has no user settings, so just copy the parent's list. */ 2554192895Sjamie if (pr->pr_ip4s < ppr->pr_ip4s) { 2555192895Sjamie /* 2556192895Sjamie * There's no room for the parent's list. Use the 2557192895Sjamie * new list buffer, which is assumed to be big enough 2558192895Sjamie * (if it was passed). If there's no buffer, try to 2559192895Sjamie * allocate one. 2560192895Sjamie */ 2561192895Sjamie used = 1; 2562192895Sjamie if (newip4 == NULL) { 2563192895Sjamie newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4), 2564192895Sjamie M_PRISON, M_NOWAIT); 2565192895Sjamie if (newip4 != NULL) 2566192895Sjamie used = 0; 2567192895Sjamie } 2568192895Sjamie if (newip4 != NULL) { 2569192895Sjamie bcopy(ppr->pr_ip4, newip4, 2570192895Sjamie ppr->pr_ip4s * sizeof(*newip4)); 2571192895Sjamie free(pr->pr_ip4, M_PRISON); 2572192895Sjamie pr->pr_ip4 = newip4; 2573192895Sjamie pr->pr_ip4s = ppr->pr_ip4s; 2574192895Sjamie } 2575192895Sjamie return (used); 2576192895Sjamie } 2577192895Sjamie pr->pr_ip4s = ppr->pr_ip4s; 2578192895Sjamie if (pr->pr_ip4s > 0) 2579192895Sjamie bcopy(ppr->pr_ip4, pr->pr_ip4, 2580192895Sjamie pr->pr_ip4s * sizeof(*newip4)); 2581192895Sjamie else if (pr->pr_ip4 != NULL) { 2582192895Sjamie free(pr->pr_ip4, M_PRISON); 2583192895Sjamie pr->pr_ip4 = NULL; 2584192895Sjamie } 2585195974Sjamie } else if (pr->pr_ip4s > 0) { 2586192895Sjamie /* Remove addresses that aren't in the parent. */ 2587192895Sjamie for (ij = 0; ij < ppr->pr_ip4s; ij++) 2588192895Sjamie if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 2589192895Sjamie break; 2590192895Sjamie if (ij < ppr->pr_ip4s) 2591192895Sjamie ii = 1; 2592192895Sjamie else { 2593192895Sjamie bcopy(pr->pr_ip4 + 1, pr->pr_ip4, 2594192895Sjamie --pr->pr_ip4s * sizeof(*pr->pr_ip4)); 2595192895Sjamie ii = 0; 2596192895Sjamie } 2597192895Sjamie for (ij = 1; ii < pr->pr_ip4s; ) { 2598192895Sjamie if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) { 2599192895Sjamie ii++; 2600192895Sjamie continue; 2601192895Sjamie } 2602192895Sjamie switch (ij >= ppr->pr_ip4s ? -1 : 2603192895Sjamie qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) { 2604192895Sjamie case -1: 2605192895Sjamie bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii, 2606192895Sjamie (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4)); 2607192895Sjamie break; 2608192895Sjamie case 0: 2609192895Sjamie ii++; 2610192895Sjamie ij++; 2611192895Sjamie break; 2612192895Sjamie case 1: 2613192895Sjamie ij++; 2614192895Sjamie break; 2615192895Sjamie } 2616192895Sjamie } 2617192895Sjamie if (pr->pr_ip4s == 0) { 2618195870Sjamie pr->pr_flags |= PR_IP4_DISABLE; 2619192895Sjamie free(pr->pr_ip4, M_PRISON); 2620192895Sjamie pr->pr_ip4 = NULL; 2621192895Sjamie } 2622192895Sjamie } 2623192895Sjamie return (0); 2624192895Sjamie} 2625192895Sjamie 2626192895Sjamie/* 2627185435Sbz * Pass back primary IPv4 address of this jail. 2628185435Sbz * 2629192895Sjamie * If not restricted return success but do not alter the address. Caller has 2630192895Sjamie * to make sure to initialize it correctly (e.g. INADDR_ANY). 2631185435Sbz * 2632188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4. 2633188144Sjamie * Address returned in NBO. 2634185435Sbz */ 263546155Sphkint 2636187684Sbzprison_get_ip4(struct ucred *cred, struct in_addr *ia) 263746155Sphk{ 2638191673Sjamie struct prison *pr; 263946155Sphk 2640185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2641185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2642185435Sbz 2643192895Sjamie pr = cred->cr_prison; 2644192895Sjamie if (!(pr->pr_flags & PR_IP4)) 264546155Sphk return (0); 2646191673Sjamie mtx_lock(&pr->pr_mtx); 2647192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2648192895Sjamie mtx_unlock(&pr->pr_mtx); 2649192895Sjamie return (0); 2650192895Sjamie } 2651191673Sjamie if (pr->pr_ip4 == NULL) { 2652191673Sjamie mtx_unlock(&pr->pr_mtx); 2653188144Sjamie return (EAFNOSUPPORT); 2654191673Sjamie } 2655185435Sbz 2656191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2657191673Sjamie mtx_unlock(&pr->pr_mtx); 2658185435Sbz return (0); 2659185435Sbz} 2660185435Sbz 2661185435Sbz/* 2662192895Sjamie * Return true if pr1 and pr2 have the same IPv4 address restrictions. 2663192895Sjamie */ 2664192895Sjamieint 2665192895Sjamieprison_equal_ip4(struct prison *pr1, struct prison *pr2) 2666192895Sjamie{ 2667192895Sjamie 2668192895Sjamie if (pr1 == pr2) 2669192895Sjamie return (1); 2670192895Sjamie 2671192895Sjamie /* 2672195974Sjamie * No need to lock since the PR_IP4_USER flag can't be altered for 2673195974Sjamie * existing prisons. 2674192895Sjamie */ 2675195945Sjamie while (pr1 != &prison0 && 2676195945Sjamie#ifdef VIMAGE 2677195945Sjamie !(pr1->pr_flags & PR_VNET) && 2678195945Sjamie#endif 2679195945Sjamie !(pr1->pr_flags & PR_IP4_USER)) 2680192895Sjamie pr1 = pr1->pr_parent; 2681195945Sjamie while (pr2 != &prison0 && 2682195945Sjamie#ifdef VIMAGE 2683195945Sjamie !(pr2->pr_flags & PR_VNET) && 2684195945Sjamie#endif 2685195945Sjamie !(pr2->pr_flags & PR_IP4_USER)) 2686192895Sjamie pr2 = pr2->pr_parent; 2687192895Sjamie return (pr1 == pr2); 2688192895Sjamie} 2689192895Sjamie 2690192895Sjamie/* 2691185435Sbz * Make sure our (source) address is set to something meaningful to this 2692185435Sbz * jail. 2693185435Sbz * 2694192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail, 2695192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 2696192895Sjamie * doesn't allow IPv4. Address passed in in NBO and returned in NBO. 2697185435Sbz */ 2698185435Sbzint 2699185435Sbzprison_local_ip4(struct ucred *cred, struct in_addr *ia) 2700185435Sbz{ 2701191673Sjamie struct prison *pr; 2702185435Sbz struct in_addr ia0; 2703191673Sjamie int error; 2704185435Sbz 2705185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2706185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2707185435Sbz 2708192895Sjamie pr = cred->cr_prison; 2709192895Sjamie if (!(pr->pr_flags & PR_IP4)) 271046155Sphk return (0); 2711191673Sjamie mtx_lock(&pr->pr_mtx); 2712192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2713192895Sjamie mtx_unlock(&pr->pr_mtx); 2714192895Sjamie return (0); 2715192895Sjamie } 2716191673Sjamie if (pr->pr_ip4 == NULL) { 2717191673Sjamie mtx_unlock(&pr->pr_mtx); 2718188144Sjamie return (EAFNOSUPPORT); 2719191673Sjamie } 2720185435Sbz 2721185435Sbz ia0.s_addr = ntohl(ia->s_addr); 2722185435Sbz if (ia0.s_addr == INADDR_LOOPBACK) { 2723191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2724191673Sjamie mtx_unlock(&pr->pr_mtx); 2725185435Sbz return (0); 272646155Sphk } 2727185435Sbz 2728188144Sjamie if (ia0.s_addr == INADDR_ANY) { 2729188144Sjamie /* 2730188144Sjamie * In case there is only 1 IPv4 address, bind directly. 2731188144Sjamie */ 2732191673Sjamie if (pr->pr_ip4s == 1) 2733191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2734191673Sjamie mtx_unlock(&pr->pr_mtx); 2735185435Sbz return (0); 2736185435Sbz } 2737185435Sbz 2738191673Sjamie error = _prison_check_ip4(pr, ia); 2739191673Sjamie mtx_unlock(&pr->pr_mtx); 2740191673Sjamie return (error); 2741185435Sbz} 2742185435Sbz 2743185435Sbz/* 2744185435Sbz * Rewrite destination address in case we will connect to loopback address. 2745185435Sbz * 2746188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4. 2747188144Sjamie * Address passed in in NBO and returned in NBO. 2748185435Sbz */ 2749185435Sbzint 2750185435Sbzprison_remote_ip4(struct ucred *cred, struct in_addr *ia) 2751185435Sbz{ 2752191673Sjamie struct prison *pr; 2753185435Sbz 2754185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2755185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2756185435Sbz 2757192895Sjamie pr = cred->cr_prison; 2758192895Sjamie if (!(pr->pr_flags & PR_IP4)) 2759185435Sbz return (0); 2760191673Sjamie mtx_lock(&pr->pr_mtx); 2761192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2762192895Sjamie mtx_unlock(&pr->pr_mtx); 2763192895Sjamie return (0); 2764192895Sjamie } 2765191673Sjamie if (pr->pr_ip4 == NULL) { 2766191673Sjamie mtx_unlock(&pr->pr_mtx); 2767188144Sjamie return (EAFNOSUPPORT); 2768191673Sjamie } 2769188144Sjamie 2770185435Sbz if (ntohl(ia->s_addr) == INADDR_LOOPBACK) { 2771191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2772191673Sjamie mtx_unlock(&pr->pr_mtx); 2773185435Sbz return (0); 2774185435Sbz } 2775185435Sbz 2776185435Sbz /* 2777185435Sbz * Return success because nothing had to be changed. 2778185435Sbz */ 2779191673Sjamie mtx_unlock(&pr->pr_mtx); 2780185435Sbz return (0); 2781185435Sbz} 2782185435Sbz 2783185435Sbz/* 2784188144Sjamie * Check if given address belongs to the jail referenced by cred/prison. 2785185435Sbz * 2786192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail, 2787192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 2788192895Sjamie * doesn't allow IPv4. Address passed in in NBO. 2789185435Sbz */ 2790185435Sbzstatic int 2791185435Sbz_prison_check_ip4(struct prison *pr, struct in_addr *ia) 2792185435Sbz{ 2793185435Sbz int i, a, z, d; 2794185435Sbz 2795185435Sbz /* 2796185435Sbz * Check the primary IP. 2797185435Sbz */ 2798185435Sbz if (pr->pr_ip4[0].s_addr == ia->s_addr) 2799188144Sjamie return (0); 2800185435Sbz 2801185435Sbz /* 2802185435Sbz * All the other IPs are sorted so we can do a binary search. 2803185435Sbz */ 2804185435Sbz a = 0; 2805185435Sbz z = pr->pr_ip4s - 2; 2806185435Sbz while (a <= z) { 2807185435Sbz i = (a + z) / 2; 2808185435Sbz d = qcmp_v4(&pr->pr_ip4[i+1], ia); 2809185435Sbz if (d > 0) 2810185435Sbz z = i - 1; 2811185435Sbz else if (d < 0) 2812185435Sbz a = i + 1; 281381114Srwatson else 2814188144Sjamie return (0); 2815185435Sbz } 2816188144Sjamie 2817188144Sjamie return (EADDRNOTAVAIL); 2818185435Sbz} 2819185435Sbz 2820185435Sbzint 2821185435Sbzprison_check_ip4(struct ucred *cred, struct in_addr *ia) 2822185435Sbz{ 2823191673Sjamie struct prison *pr; 2824191673Sjamie int error; 2825185435Sbz 2826185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2827185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2828185435Sbz 2829192895Sjamie pr = cred->cr_prison; 2830192895Sjamie if (!(pr->pr_flags & PR_IP4)) 2831188144Sjamie return (0); 2832191673Sjamie mtx_lock(&pr->pr_mtx); 2833192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2834192895Sjamie mtx_unlock(&pr->pr_mtx); 2835192895Sjamie return (0); 2836192895Sjamie } 2837191673Sjamie if (pr->pr_ip4 == NULL) { 2838191673Sjamie mtx_unlock(&pr->pr_mtx); 2839188144Sjamie return (EAFNOSUPPORT); 2840191673Sjamie } 2841185435Sbz 2842191673Sjamie error = _prison_check_ip4(pr, ia); 2843191673Sjamie mtx_unlock(&pr->pr_mtx); 2844191673Sjamie return (error); 2845185435Sbz} 2846185435Sbz#endif 2847185435Sbz 2848185435Sbz#ifdef INET6 2849192895Sjamiestatic int 2850192895Sjamieprison_restrict_ip6(struct prison *pr, struct in6_addr *newip6) 2851192895Sjamie{ 2852192895Sjamie int ii, ij, used; 2853192895Sjamie struct prison *ppr; 2854192895Sjamie 2855192895Sjamie ppr = pr->pr_parent; 2856192895Sjamie if (!(pr->pr_flags & PR_IP6_USER)) { 2857192895Sjamie /* This has no user settings, so just copy the parent's list. */ 2858192895Sjamie if (pr->pr_ip6s < ppr->pr_ip6s) { 2859192895Sjamie /* 2860192895Sjamie * There's no room for the parent's list. Use the 2861192895Sjamie * new list buffer, which is assumed to be big enough 2862192895Sjamie * (if it was passed). If there's no buffer, try to 2863192895Sjamie * allocate one. 2864192895Sjamie */ 2865192895Sjamie used = 1; 2866192895Sjamie if (newip6 == NULL) { 2867192895Sjamie newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6), 2868192895Sjamie M_PRISON, M_NOWAIT); 2869192895Sjamie if (newip6 != NULL) 2870192895Sjamie used = 0; 2871192895Sjamie } 2872192895Sjamie if (newip6 != NULL) { 2873192895Sjamie bcopy(ppr->pr_ip6, newip6, 2874192895Sjamie ppr->pr_ip6s * sizeof(*newip6)); 2875192895Sjamie free(pr->pr_ip6, M_PRISON); 2876192895Sjamie pr->pr_ip6 = newip6; 2877192895Sjamie pr->pr_ip6s = ppr->pr_ip6s; 2878192895Sjamie } 2879192895Sjamie return (used); 2880192895Sjamie } 2881192895Sjamie pr->pr_ip6s = ppr->pr_ip6s; 2882192895Sjamie if (pr->pr_ip6s > 0) 2883192895Sjamie bcopy(ppr->pr_ip6, pr->pr_ip6, 2884192895Sjamie pr->pr_ip6s * sizeof(*newip6)); 2885192895Sjamie else if (pr->pr_ip6 != NULL) { 2886192895Sjamie free(pr->pr_ip6, M_PRISON); 2887192895Sjamie pr->pr_ip6 = NULL; 2888192895Sjamie } 2889195974Sjamie } else if (pr->pr_ip6s > 0) { 2890192895Sjamie /* Remove addresses that aren't in the parent. */ 2891192895Sjamie for (ij = 0; ij < ppr->pr_ip6s; ij++) 2892192895Sjamie if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], 2893192895Sjamie &ppr->pr_ip6[ij])) 2894192895Sjamie break; 2895192895Sjamie if (ij < ppr->pr_ip6s) 2896192895Sjamie ii = 1; 2897192895Sjamie else { 2898192895Sjamie bcopy(pr->pr_ip6 + 1, pr->pr_ip6, 2899192895Sjamie --pr->pr_ip6s * sizeof(*pr->pr_ip6)); 2900192895Sjamie ii = 0; 2901192895Sjamie } 2902192895Sjamie for (ij = 1; ii < pr->pr_ip6s; ) { 2903192895Sjamie if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii], 2904192895Sjamie &ppr->pr_ip6[0])) { 2905192895Sjamie ii++; 2906192895Sjamie continue; 2907192895Sjamie } 2908192895Sjamie switch (ij >= ppr->pr_ip4s ? -1 : 2909192895Sjamie qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) { 2910192895Sjamie case -1: 2911192895Sjamie bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii, 2912192895Sjamie (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6)); 2913192895Sjamie break; 2914192895Sjamie case 0: 2915192895Sjamie ii++; 2916192895Sjamie ij++; 2917192895Sjamie break; 2918192895Sjamie case 1: 2919192895Sjamie ij++; 2920192895Sjamie break; 2921192895Sjamie } 2922192895Sjamie } 2923192895Sjamie if (pr->pr_ip6s == 0) { 2924195870Sjamie pr->pr_flags |= PR_IP6_DISABLE; 2925192895Sjamie free(pr->pr_ip6, M_PRISON); 2926192895Sjamie pr->pr_ip6 = NULL; 2927192895Sjamie } 2928192895Sjamie } 2929192895Sjamie return 0; 2930192895Sjamie} 2931192895Sjamie 2932185435Sbz/* 2933185435Sbz * Pass back primary IPv6 address for this jail. 2934185435Sbz * 2935192895Sjamie * If not restricted return success but do not alter the address. Caller has 2936192895Sjamie * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT). 2937185435Sbz * 2938188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6. 2939185435Sbz */ 2940185435Sbzint 2941187684Sbzprison_get_ip6(struct ucred *cred, struct in6_addr *ia6) 2942185435Sbz{ 2943191673Sjamie struct prison *pr; 2944185435Sbz 2945185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2946185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 2947185435Sbz 2948192895Sjamie pr = cred->cr_prison; 2949192895Sjamie if (!(pr->pr_flags & PR_IP6)) 295081114Srwatson return (0); 2951191673Sjamie mtx_lock(&pr->pr_mtx); 2952192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 2953192895Sjamie mtx_unlock(&pr->pr_mtx); 2954192895Sjamie return (0); 2955192895Sjamie } 2956191673Sjamie if (pr->pr_ip6 == NULL) { 2957191673Sjamie mtx_unlock(&pr->pr_mtx); 2958188144Sjamie return (EAFNOSUPPORT); 2959191673Sjamie } 2960188144Sjamie 2961191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 2962191673Sjamie mtx_unlock(&pr->pr_mtx); 2963185435Sbz return (0); 2964185435Sbz} 2965185435Sbz 2966185435Sbz/* 2967192895Sjamie * Return true if pr1 and pr2 have the same IPv6 address restrictions. 2968192895Sjamie */ 2969192895Sjamieint 2970192895Sjamieprison_equal_ip6(struct prison *pr1, struct prison *pr2) 2971192895Sjamie{ 2972192895Sjamie 2973192895Sjamie if (pr1 == pr2) 2974192895Sjamie return (1); 2975192895Sjamie 2976195945Sjamie while (pr1 != &prison0 && 2977195945Sjamie#ifdef VIMAGE 2978195945Sjamie !(pr1->pr_flags & PR_VNET) && 2979195945Sjamie#endif 2980195945Sjamie !(pr1->pr_flags & PR_IP6_USER)) 2981192895Sjamie pr1 = pr1->pr_parent; 2982195945Sjamie while (pr2 != &prison0 && 2983195945Sjamie#ifdef VIMAGE 2984195945Sjamie !(pr2->pr_flags & PR_VNET) && 2985195945Sjamie#endif 2986195945Sjamie !(pr2->pr_flags & PR_IP6_USER)) 2987192895Sjamie pr2 = pr2->pr_parent; 2988192895Sjamie return (pr1 == pr2); 2989192895Sjamie} 2990192895Sjamie 2991192895Sjamie/* 2992185435Sbz * Make sure our (source) address is set to something meaningful to this jail. 2993185435Sbz * 2994185435Sbz * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0) 2995185435Sbz * when needed while binding. 2996185435Sbz * 2997192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail, 2998192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 2999192895Sjamie * doesn't allow IPv6. 3000185435Sbz */ 3001185435Sbzint 3002185435Sbzprison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only) 3003185435Sbz{ 3004191673Sjamie struct prison *pr; 3005191673Sjamie int error; 3006185435Sbz 3007185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3008185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3009185435Sbz 3010192895Sjamie pr = cred->cr_prison; 3011192895Sjamie if (!(pr->pr_flags & PR_IP6)) 3012185435Sbz return (0); 3013191673Sjamie mtx_lock(&pr->pr_mtx); 3014192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3015192895Sjamie mtx_unlock(&pr->pr_mtx); 3016192895Sjamie return (0); 3017192895Sjamie } 3018191673Sjamie if (pr->pr_ip6 == NULL) { 3019191673Sjamie mtx_unlock(&pr->pr_mtx); 3020188144Sjamie return (EAFNOSUPPORT); 3021191673Sjamie } 3022188144Sjamie 3023185435Sbz if (IN6_IS_ADDR_LOOPBACK(ia6)) { 3024191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3025191673Sjamie mtx_unlock(&pr->pr_mtx); 3026185435Sbz return (0); 302781114Srwatson } 3028185435Sbz 3029188144Sjamie if (IN6_IS_ADDR_UNSPECIFIED(ia6)) { 3030188144Sjamie /* 3031188144Sjamie * In case there is only 1 IPv6 address, and v6only is true, 3032188144Sjamie * then bind directly. 3033188144Sjamie */ 3034191673Sjamie if (v6only != 0 && pr->pr_ip6s == 1) 3035191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3036191673Sjamie mtx_unlock(&pr->pr_mtx); 3037185435Sbz return (0); 3038185435Sbz } 3039188144Sjamie 3040191673Sjamie error = _prison_check_ip6(pr, ia6); 3041191673Sjamie mtx_unlock(&pr->pr_mtx); 3042191673Sjamie return (error); 3043185435Sbz} 3044185435Sbz 3045185435Sbz/* 3046185435Sbz * Rewrite destination address in case we will connect to loopback address. 3047185435Sbz * 3048188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6. 3049185435Sbz */ 3050185435Sbzint 3051185435Sbzprison_remote_ip6(struct ucred *cred, struct in6_addr *ia6) 3052185435Sbz{ 3053191673Sjamie struct prison *pr; 3054185435Sbz 3055185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3056185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3057185435Sbz 3058192895Sjamie pr = cred->cr_prison; 3059192895Sjamie if (!(pr->pr_flags & PR_IP6)) 3060185435Sbz return (0); 3061191673Sjamie mtx_lock(&pr->pr_mtx); 3062192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3063192895Sjamie mtx_unlock(&pr->pr_mtx); 3064192895Sjamie return (0); 3065192895Sjamie } 3066191673Sjamie if (pr->pr_ip6 == NULL) { 3067191673Sjamie mtx_unlock(&pr->pr_mtx); 3068188144Sjamie return (EAFNOSUPPORT); 3069191673Sjamie } 3070188144Sjamie 3071185435Sbz if (IN6_IS_ADDR_LOOPBACK(ia6)) { 3072191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3073191673Sjamie mtx_unlock(&pr->pr_mtx); 3074185435Sbz return (0); 3075185435Sbz } 3076185435Sbz 3077185435Sbz /* 3078185435Sbz * Return success because nothing had to be changed. 3079185435Sbz */ 3080191673Sjamie mtx_unlock(&pr->pr_mtx); 308146155Sphk return (0); 308246155Sphk} 308346155Sphk 3084185435Sbz/* 3085188144Sjamie * Check if given address belongs to the jail referenced by cred/prison. 3086185435Sbz * 3087192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail, 3088192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 3089192895Sjamie * doesn't allow IPv6. 3090185435Sbz */ 3091185435Sbzstatic int 3092185435Sbz_prison_check_ip6(struct prison *pr, struct in6_addr *ia6) 309346155Sphk{ 3094185435Sbz int i, a, z, d; 309546155Sphk 3096185435Sbz /* 3097185435Sbz * Check the primary IP. 3098185435Sbz */ 3099185435Sbz if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6)) 3100188144Sjamie return (0); 3101185435Sbz 3102185435Sbz /* 3103185435Sbz * All the other IPs are sorted so we can do a binary search. 3104185435Sbz */ 3105185435Sbz a = 0; 3106185435Sbz z = pr->pr_ip6s - 2; 3107185435Sbz while (a <= z) { 3108185435Sbz i = (a + z) / 2; 3109185435Sbz d = qcmp_v6(&pr->pr_ip6[i+1], ia6); 3110185435Sbz if (d > 0) 3111185435Sbz z = i - 1; 3112185435Sbz else if (d < 0) 3113185435Sbz a = i + 1; 311446155Sphk else 3115188144Sjamie return (0); 311646155Sphk } 3117188144Sjamie 3118188144Sjamie return (EADDRNOTAVAIL); 311946155Sphk} 312046155Sphk 312146155Sphkint 3122185435Sbzprison_check_ip6(struct ucred *cred, struct in6_addr *ia6) 3123185435Sbz{ 3124191673Sjamie struct prison *pr; 3125191673Sjamie int error; 3126185435Sbz 3127185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3128185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3129185435Sbz 3130192895Sjamie pr = cred->cr_prison; 3131192895Sjamie if (!(pr->pr_flags & PR_IP6)) 3132188144Sjamie return (0); 3133191673Sjamie mtx_lock(&pr->pr_mtx); 3134192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3135192895Sjamie mtx_unlock(&pr->pr_mtx); 3136192895Sjamie return (0); 3137192895Sjamie } 3138191673Sjamie if (pr->pr_ip6 == NULL) { 3139191673Sjamie mtx_unlock(&pr->pr_mtx); 3140188144Sjamie return (EAFNOSUPPORT); 3141191673Sjamie } 3142185435Sbz 3143191673Sjamie error = _prison_check_ip6(pr, ia6); 3144191673Sjamie mtx_unlock(&pr->pr_mtx); 3145191673Sjamie return (error); 3146185435Sbz} 3147185435Sbz#endif 3148185435Sbz 3149185435Sbz/* 3150188146Sjamie * Check if a jail supports the given address family. 3151188146Sjamie * 3152188146Sjamie * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT 3153188146Sjamie * if not. 3154188146Sjamie */ 3155188146Sjamieint 3156188146Sjamieprison_check_af(struct ucred *cred, int af) 3157188146Sjamie{ 3158192895Sjamie struct prison *pr; 3159188146Sjamie int error; 3160188146Sjamie 3161188146Sjamie KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3162188146Sjamie 3163192895Sjamie pr = cred->cr_prison; 3164194923Sjamie#ifdef VIMAGE 3165194915Sjamie /* Prisons with their own network stack are not limited. */ 3166200473Sbz if (prison_owns_vnet(cred)) 3167194915Sjamie return (0); 3168194923Sjamie#endif 3169194915Sjamie 3170188146Sjamie error = 0; 3171188146Sjamie switch (af) 3172188146Sjamie { 3173188146Sjamie#ifdef INET 3174188146Sjamie case AF_INET: 3175192895Sjamie if (pr->pr_flags & PR_IP4) 3176192895Sjamie { 3177192895Sjamie mtx_lock(&pr->pr_mtx); 3178192895Sjamie if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL) 3179192895Sjamie error = EAFNOSUPPORT; 3180192895Sjamie mtx_unlock(&pr->pr_mtx); 3181192895Sjamie } 3182188146Sjamie break; 3183188146Sjamie#endif 3184188146Sjamie#ifdef INET6 3185188146Sjamie case AF_INET6: 3186192895Sjamie if (pr->pr_flags & PR_IP6) 3187192895Sjamie { 3188192895Sjamie mtx_lock(&pr->pr_mtx); 3189192895Sjamie if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL) 3190192895Sjamie error = EAFNOSUPPORT; 3191192895Sjamie mtx_unlock(&pr->pr_mtx); 3192192895Sjamie } 3193188146Sjamie break; 3194188146Sjamie#endif 3195188146Sjamie case AF_LOCAL: 3196188146Sjamie case AF_ROUTE: 3197188146Sjamie break; 3198188146Sjamie default: 3199192895Sjamie if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF)) 3200188146Sjamie error = EAFNOSUPPORT; 3201188146Sjamie } 3202188146Sjamie return (error); 3203188146Sjamie} 3204188146Sjamie 3205188146Sjamie/* 3206185435Sbz * Check if given address belongs to the jail referenced by cred (wrapper to 3207185435Sbz * prison_check_ip[46]). 3208185435Sbz * 3209192895Sjamie * Returns 0 if jail doesn't restrict the address family or if address belongs 3210192895Sjamie * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if 3211192895Sjamie * the jail doesn't allow the address family. IPv4 Address passed in in NBO. 3212185435Sbz */ 3213185435Sbzint 321472786Srwatsonprison_if(struct ucred *cred, struct sockaddr *sa) 321546155Sphk{ 3216185435Sbz#ifdef INET 3217114168Smike struct sockaddr_in *sai; 3218185435Sbz#endif 3219185435Sbz#ifdef INET6 3220185435Sbz struct sockaddr_in6 *sai6; 3221185435Sbz#endif 3222188144Sjamie int error; 322346155Sphk 3224185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3225185435Sbz KASSERT(sa != NULL, ("%s: sa is NULL", __func__)); 3226185435Sbz 3227200473Sbz#ifdef VIMAGE 3228200473Sbz if (prison_owns_vnet(cred)) 3229200473Sbz return (0); 3230200473Sbz#endif 3231200473Sbz 3232188144Sjamie error = 0; 3233188144Sjamie switch (sa->sa_family) 3234185435Sbz { 3235185435Sbz#ifdef INET 3236185435Sbz case AF_INET: 3237185435Sbz sai = (struct sockaddr_in *)sa; 3238188144Sjamie error = prison_check_ip4(cred, &sai->sin_addr); 3239185435Sbz break; 3240185435Sbz#endif 3241185435Sbz#ifdef INET6 3242185435Sbz case AF_INET6: 3243185435Sbz sai6 = (struct sockaddr_in6 *)sa; 3244188144Sjamie error = prison_check_ip6(cred, &sai6->sin6_addr); 3245185435Sbz break; 3246185435Sbz#endif 3247185435Sbz default: 3248192895Sjamie if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF)) 3249188144Sjamie error = EAFNOSUPPORT; 3250185435Sbz } 3251188144Sjamie return (error); 325246155Sphk} 325372786Srwatson 325472786Srwatson/* 325572786Srwatson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH. 325672786Srwatson */ 325772786Srwatsonint 3258114168Smikeprison_check(struct ucred *cred1, struct ucred *cred2) 325972786Srwatson{ 326072786Srwatson 3261192895Sjamie return ((cred1->cr_prison == cred2->cr_prison || 3262192895Sjamie prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH); 3263192895Sjamie} 326472786Srwatson 3265192895Sjamie/* 3266192895Sjamie * Return 1 if p2 is a child of p1, otherwise 0. 3267192895Sjamie */ 3268192895Sjamieint 3269192895Sjamieprison_ischild(struct prison *pr1, struct prison *pr2) 3270192895Sjamie{ 3271192895Sjamie 3272192895Sjamie for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent) 3273192895Sjamie if (pr1 == pr2) 3274192895Sjamie return (1); 327572786Srwatson return (0); 327672786Srwatson} 327772786Srwatson 327872786Srwatson/* 327972786Srwatson * Return 1 if the passed credential is in a jail, otherwise 0. 328072786Srwatson */ 328172786Srwatsonint 3282114168Smikejailed(struct ucred *cred) 328372786Srwatson{ 328472786Srwatson 3285192895Sjamie return (cred->cr_prison != &prison0); 328672786Srwatson} 328791384Srobert 328891384Srobert/* 3289200473Sbz * Return 1 if the passed credential is in a jail and that jail does not 3290200473Sbz * have its own virtual network stack, otherwise 0. 3291200473Sbz */ 3292200473Sbzint 3293200473Sbzjailed_without_vnet(struct ucred *cred) 3294200473Sbz{ 3295200473Sbz 3296200473Sbz if (!jailed(cred)) 3297200473Sbz return (0); 3298200473Sbz#ifdef VIMAGE 3299200473Sbz if (prison_owns_vnet(cred)) 3300200473Sbz return (0); 3301200473Sbz#endif 3302200473Sbz 3303200473Sbz return (1); 3304200473Sbz} 3305200473Sbz 3306200473Sbz/* 3307194090Sjamie * Return the correct hostname (domainname, et al) for the passed credential. 330891384Srobert */ 330991391Srobertvoid 3310114168Smikegetcredhostname(struct ucred *cred, char *buf, size_t size) 331191384Srobert{ 3312193066Sjamie struct prison *pr; 331391384Srobert 3314194090Sjamie /* 3315194090Sjamie * A NULL credential can be used to shortcut to the physical 3316194090Sjamie * system's hostname. 3317194090Sjamie */ 3318193066Sjamie pr = (cred != NULL) ? cred->cr_prison : &prison0; 3319193066Sjamie mtx_lock(&pr->pr_mtx); 3320194118Sjamie strlcpy(buf, pr->pr_hostname, size); 3321193066Sjamie mtx_unlock(&pr->pr_mtx); 332291384Srobert} 3323113275Smike 3324194090Sjamievoid 3325194090Sjamiegetcreddomainname(struct ucred *cred, char *buf, size_t size) 3326194090Sjamie{ 3327194090Sjamie 3328194090Sjamie mtx_lock(&cred->cr_prison->pr_mtx); 3329194118Sjamie strlcpy(buf, cred->cr_prison->pr_domainname, size); 3330194090Sjamie mtx_unlock(&cred->cr_prison->pr_mtx); 3331194090Sjamie} 3332194090Sjamie 3333194090Sjamievoid 3334194090Sjamiegetcredhostuuid(struct ucred *cred, char *buf, size_t size) 3335194090Sjamie{ 3336194090Sjamie 3337194090Sjamie mtx_lock(&cred->cr_prison->pr_mtx); 3338194118Sjamie strlcpy(buf, cred->cr_prison->pr_hostuuid, size); 3339194090Sjamie mtx_unlock(&cred->cr_prison->pr_mtx); 3340194090Sjamie} 3341194090Sjamie 3342194090Sjamievoid 3343194090Sjamiegetcredhostid(struct ucred *cred, unsigned long *hostid) 3344194090Sjamie{ 3345194090Sjamie 3346194090Sjamie mtx_lock(&cred->cr_prison->pr_mtx); 3347194090Sjamie *hostid = cred->cr_prison->pr_hostid; 3348194090Sjamie mtx_unlock(&cred->cr_prison->pr_mtx); 3349194090Sjamie} 3350194090Sjamie 3351196176Sbz#ifdef VIMAGE 3352125804Srwatson/* 3353196176Sbz * Determine whether the prison represented by cred owns 3354196176Sbz * its vnet rather than having it inherited. 3355196176Sbz * 3356196176Sbz * Returns 1 in case the prison owns the vnet, 0 otherwise. 3357196176Sbz */ 3358196176Sbzint 3359196176Sbzprison_owns_vnet(struct ucred *cred) 3360196176Sbz{ 3361196176Sbz 3362196176Sbz /* 3363196176Sbz * vnets cannot be added/removed after jail creation, 3364196176Sbz * so no need to lock here. 3365196176Sbz */ 3366196176Sbz return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0); 3367196176Sbz} 3368196176Sbz#endif 3369196176Sbz 3370196176Sbz/* 3371147185Spjd * Determine whether the subject represented by cred can "see" 3372147185Spjd * status of a mount point. 3373147185Spjd * Returns: 0 for permitted, ENOENT otherwise. 3374147185Spjd * XXX: This function should be called cr_canseemount() and should be 3375147185Spjd * placed in kern_prot.c. 3376125804Srwatson */ 3377125804Srwatsonint 3378147185Spjdprison_canseemount(struct ucred *cred, struct mount *mp) 3379125804Srwatson{ 3380147185Spjd struct prison *pr; 3381147185Spjd struct statfs *sp; 3382147185Spjd size_t len; 3383125804Srwatson 3384192895Sjamie pr = cred->cr_prison; 3385192895Sjamie if (pr->pr_enforce_statfs == 0) 3386147185Spjd return (0); 3387147185Spjd if (pr->pr_root->v_mount == mp) 3388147185Spjd return (0); 3389192895Sjamie if (pr->pr_enforce_statfs == 2) 3390147185Spjd return (ENOENT); 3391147185Spjd /* 3392147185Spjd * If jail's chroot directory is set to "/" we should be able to see 3393147185Spjd * all mount-points from inside a jail. 3394147185Spjd * This is ugly check, but this is the only situation when jail's 3395147185Spjd * directory ends with '/'. 3396147185Spjd */ 3397147185Spjd if (strcmp(pr->pr_path, "/") == 0) 3398147185Spjd return (0); 3399147185Spjd len = strlen(pr->pr_path); 3400147185Spjd sp = &mp->mnt_stat; 3401147185Spjd if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0) 3402147185Spjd return (ENOENT); 3403147185Spjd /* 3404147185Spjd * Be sure that we don't have situation where jail's root directory 3405147185Spjd * is "/some/path" and mount point is "/some/pathpath". 3406147185Spjd */ 3407147185Spjd if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/') 3408147185Spjd return (ENOENT); 3409147185Spjd return (0); 3410147185Spjd} 3411147185Spjd 3412147185Spjdvoid 3413147185Spjdprison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp) 3414147185Spjd{ 3415147185Spjd char jpath[MAXPATHLEN]; 3416147185Spjd struct prison *pr; 3417147185Spjd size_t len; 3418147185Spjd 3419192895Sjamie pr = cred->cr_prison; 3420192895Sjamie if (pr->pr_enforce_statfs == 0) 3421147185Spjd return; 3422147185Spjd if (prison_canseemount(cred, mp) != 0) { 3423147185Spjd bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3424147185Spjd strlcpy(sp->f_mntonname, "[restricted]", 3425147185Spjd sizeof(sp->f_mntonname)); 3426147185Spjd return; 3427125804Srwatson } 3428147185Spjd if (pr->pr_root->v_mount == mp) { 3429147185Spjd /* 3430147185Spjd * Clear current buffer data, so we are sure nothing from 3431147185Spjd * the valid path left there. 3432147185Spjd */ 3433147185Spjd bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3434147185Spjd *sp->f_mntonname = '/'; 3435147185Spjd return; 3436147185Spjd } 3437147185Spjd /* 3438147185Spjd * If jail's chroot directory is set to "/" we should be able to see 3439147185Spjd * all mount-points from inside a jail. 3440147185Spjd */ 3441147185Spjd if (strcmp(pr->pr_path, "/") == 0) 3442147185Spjd return; 3443147185Spjd len = strlen(pr->pr_path); 3444147185Spjd strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath)); 3445147185Spjd /* 3446147185Spjd * Clear current buffer data, so we are sure nothing from 3447147185Spjd * the valid path left there. 3448147185Spjd */ 3449147185Spjd bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3450147185Spjd if (*jpath == '\0') { 3451147185Spjd /* Should never happen. */ 3452147185Spjd *sp->f_mntonname = '/'; 3453147185Spjd } else { 3454147185Spjd strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname)); 3455147185Spjd } 3456125804Srwatson} 3457125804Srwatson 3458164032Srwatson/* 3459164032Srwatson * Check with permission for a specific privilege is granted within jail. We 3460164032Srwatson * have a specific list of accepted privileges; the rest are denied. 3461164032Srwatson */ 3462164032Srwatsonint 3463164032Srwatsonprison_priv_check(struct ucred *cred, int priv) 3464164032Srwatson{ 3465164032Srwatson 3466164032Srwatson if (!jailed(cred)) 3467164032Srwatson return (0); 3468164032Srwatson 3469194915Sjamie#ifdef VIMAGE 3470194915Sjamie /* 3471194915Sjamie * Privileges specific to prisons with a virtual network stack. 3472194915Sjamie * There might be a duplicate entry here in case the privilege 3473194915Sjamie * is only granted conditionally in the legacy jail case. 3474194915Sjamie */ 3475164032Srwatson switch (priv) { 3476194915Sjamie#ifdef notyet 3477194915Sjamie /* 3478194915Sjamie * NFS-specific privileges. 3479194915Sjamie */ 3480194915Sjamie case PRIV_NFS_DAEMON: 3481194915Sjamie case PRIV_NFS_LOCKD: 3482194915Sjamie#endif 3483194915Sjamie /* 3484194915Sjamie * Network stack privileges. 3485194915Sjamie */ 3486194915Sjamie case PRIV_NET_BRIDGE: 3487194915Sjamie case PRIV_NET_GRE: 3488194915Sjamie case PRIV_NET_BPF: 3489194915Sjamie case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */ 3490194915Sjamie case PRIV_NET_ROUTE: 3491194915Sjamie case PRIV_NET_TAP: 3492194915Sjamie case PRIV_NET_SETIFMTU: 3493194915Sjamie case PRIV_NET_SETIFFLAGS: 3494194915Sjamie case PRIV_NET_SETIFCAP: 3495194915Sjamie case PRIV_NET_SETIFNAME : 3496194915Sjamie case PRIV_NET_SETIFMETRIC: 3497194915Sjamie case PRIV_NET_SETIFPHYS: 3498194915Sjamie case PRIV_NET_SETIFMAC: 3499194915Sjamie case PRIV_NET_ADDMULTI: 3500194915Sjamie case PRIV_NET_DELMULTI: 3501194915Sjamie case PRIV_NET_HWIOCTL: 3502194915Sjamie case PRIV_NET_SETLLADDR: 3503194915Sjamie case PRIV_NET_ADDIFGROUP: 3504194915Sjamie case PRIV_NET_DELIFGROUP: 3505194915Sjamie case PRIV_NET_IFCREATE: 3506194915Sjamie case PRIV_NET_IFDESTROY: 3507194915Sjamie case PRIV_NET_ADDIFADDR: 3508194915Sjamie case PRIV_NET_DELIFADDR: 3509194915Sjamie case PRIV_NET_LAGG: 3510194915Sjamie case PRIV_NET_GIF: 3511194915Sjamie case PRIV_NET_SETIFVNET: 3512164032Srwatson 3513164032Srwatson /* 3514194915Sjamie * 802.11-related privileges. 3515194915Sjamie */ 3516194915Sjamie case PRIV_NET80211_GETKEY: 3517194915Sjamie#ifdef notyet 3518194915Sjamie case PRIV_NET80211_MANAGE: /* XXX-BZ discuss with sam@ */ 3519194915Sjamie#endif 3520194915Sjamie 3521194915Sjamie#ifdef notyet 3522194915Sjamie /* 3523194915Sjamie * AppleTalk privileges. 3524194915Sjamie */ 3525194915Sjamie case PRIV_NETATALK_RESERVEDPORT: 3526194915Sjamie 3527194915Sjamie /* 3528194915Sjamie * ATM privileges. 3529194915Sjamie */ 3530194915Sjamie case PRIV_NETATM_CFG: 3531194915Sjamie case PRIV_NETATM_ADD: 3532194915Sjamie case PRIV_NETATM_DEL: 3533194915Sjamie case PRIV_NETATM_SET: 3534194915Sjamie 3535194915Sjamie /* 3536194915Sjamie * Bluetooth privileges. 3537194915Sjamie */ 3538194915Sjamie case PRIV_NETBLUETOOTH_RAW: 3539194915Sjamie#endif 3540194915Sjamie 3541194915Sjamie /* 3542194915Sjamie * Netgraph and netgraph module privileges. 3543194915Sjamie */ 3544194915Sjamie case PRIV_NETGRAPH_CONTROL: 3545194915Sjamie#ifdef notyet 3546194915Sjamie case PRIV_NETGRAPH_TTY: 3547194915Sjamie#endif 3548194915Sjamie 3549194915Sjamie /* 3550194915Sjamie * IPv4 and IPv6 privileges. 3551194915Sjamie */ 3552194915Sjamie case PRIV_NETINET_IPFW: 3553194915Sjamie case PRIV_NETINET_DIVERT: 3554194915Sjamie case PRIV_NETINET_PF: 3555194915Sjamie case PRIV_NETINET_DUMMYNET: 3556194915Sjamie case PRIV_NETINET_CARP: 3557194915Sjamie case PRIV_NETINET_MROUTE: 3558194915Sjamie case PRIV_NETINET_RAW: 3559194915Sjamie case PRIV_NETINET_ADDRCTRL6: 3560194915Sjamie case PRIV_NETINET_ND6: 3561194915Sjamie case PRIV_NETINET_SCOPE6: 3562194915Sjamie case PRIV_NETINET_ALIFETIME6: 3563194915Sjamie case PRIV_NETINET_IPSEC: 3564194915Sjamie case PRIV_NETINET_BINDANY: 3565194915Sjamie 3566194915Sjamie#ifdef notyet 3567194915Sjamie /* 3568194915Sjamie * IPX/SPX privileges. 3569194915Sjamie */ 3570194915Sjamie case PRIV_NETIPX_RESERVEDPORT: 3571194915Sjamie case PRIV_NETIPX_RAW: 3572194915Sjamie 3573194915Sjamie /* 3574194915Sjamie * NCP privileges. 3575194915Sjamie */ 3576194915Sjamie case PRIV_NETNCP: 3577194915Sjamie 3578194915Sjamie /* 3579194915Sjamie * SMB privileges. 3580194915Sjamie */ 3581194915Sjamie case PRIV_NETSMB: 3582194915Sjamie#endif 3583194915Sjamie 3584194915Sjamie /* 3585194915Sjamie * No default: or deny here. 3586194915Sjamie * In case of no permit fall through to next switch(). 3587194915Sjamie */ 3588194915Sjamie if (cred->cr_prison->pr_flags & PR_VNET) 3589194915Sjamie return (0); 3590194915Sjamie } 3591194915Sjamie#endif /* VIMAGE */ 3592194915Sjamie 3593194915Sjamie switch (priv) { 3594194915Sjamie 3595194915Sjamie /* 3596164032Srwatson * Allow ktrace privileges for root in jail. 3597164032Srwatson */ 3598164032Srwatson case PRIV_KTRACE: 3599164032Srwatson 3600166827Srwatson#if 0 3601164032Srwatson /* 3602164032Srwatson * Allow jailed processes to configure audit identity and 3603164032Srwatson * submit audit records (login, etc). In the future we may 3604164032Srwatson * want to further refine the relationship between audit and 3605164032Srwatson * jail. 3606164032Srwatson */ 3607164032Srwatson case PRIV_AUDIT_GETAUDIT: 3608164032Srwatson case PRIV_AUDIT_SETAUDIT: 3609164032Srwatson case PRIV_AUDIT_SUBMIT: 3610166827Srwatson#endif 3611164032Srwatson 3612164032Srwatson /* 3613164032Srwatson * Allow jailed processes to manipulate process UNIX 3614164032Srwatson * credentials in any way they see fit. 3615164032Srwatson */ 3616164032Srwatson case PRIV_CRED_SETUID: 3617164032Srwatson case PRIV_CRED_SETEUID: 3618164032Srwatson case PRIV_CRED_SETGID: 3619164032Srwatson case PRIV_CRED_SETEGID: 3620164032Srwatson case PRIV_CRED_SETGROUPS: 3621164032Srwatson case PRIV_CRED_SETREUID: 3622164032Srwatson case PRIV_CRED_SETREGID: 3623164032Srwatson case PRIV_CRED_SETRESUID: 3624164032Srwatson case PRIV_CRED_SETRESGID: 3625164032Srwatson 3626164032Srwatson /* 3627164032Srwatson * Jail implements visibility constraints already, so allow 3628164032Srwatson * jailed root to override uid/gid-based constraints. 3629164032Srwatson */ 3630164032Srwatson case PRIV_SEEOTHERGIDS: 3631164032Srwatson case PRIV_SEEOTHERUIDS: 3632164032Srwatson 3633164032Srwatson /* 3634164032Srwatson * Jail implements inter-process debugging limits already, so 3635164032Srwatson * allow jailed root various debugging privileges. 3636164032Srwatson */ 3637164032Srwatson case PRIV_DEBUG_DIFFCRED: 3638164032Srwatson case PRIV_DEBUG_SUGID: 3639164032Srwatson case PRIV_DEBUG_UNPRIV: 3640164032Srwatson 3641164032Srwatson /* 3642164032Srwatson * Allow jail to set various resource limits and login 3643164032Srwatson * properties, and for now, exceed process resource limits. 3644164032Srwatson */ 3645164032Srwatson case PRIV_PROC_LIMIT: 3646164032Srwatson case PRIV_PROC_SETLOGIN: 3647164032Srwatson case PRIV_PROC_SETRLIMIT: 3648164032Srwatson 3649164032Srwatson /* 3650164032Srwatson * System V and POSIX IPC privileges are granted in jail. 3651164032Srwatson */ 3652164032Srwatson case PRIV_IPC_READ: 3653164032Srwatson case PRIV_IPC_WRITE: 3654164032Srwatson case PRIV_IPC_ADMIN: 3655164032Srwatson case PRIV_IPC_MSGSIZE: 3656164032Srwatson case PRIV_MQ_ADMIN: 3657164032Srwatson 3658164032Srwatson /* 3659192895Sjamie * Jail operations within a jail work on child jails. 3660192895Sjamie */ 3661192895Sjamie case PRIV_JAIL_ATTACH: 3662192895Sjamie case PRIV_JAIL_SET: 3663192895Sjamie case PRIV_JAIL_REMOVE: 3664192895Sjamie 3665192895Sjamie /* 3666164032Srwatson * Jail implements its own inter-process limits, so allow 3667164032Srwatson * root processes in jail to change scheduling on other 3668164032Srwatson * processes in the same jail. Likewise for signalling. 3669164032Srwatson */ 3670164032Srwatson case PRIV_SCHED_DIFFCRED: 3671185435Sbz case PRIV_SCHED_CPUSET: 3672164032Srwatson case PRIV_SIGNAL_DIFFCRED: 3673164032Srwatson case PRIV_SIGNAL_SUGID: 3674164032Srwatson 3675164032Srwatson /* 3676164032Srwatson * Allow jailed processes to write to sysctls marked as jail 3677164032Srwatson * writable. 3678164032Srwatson */ 3679164032Srwatson case PRIV_SYSCTL_WRITEJAIL: 3680164032Srwatson 3681164032Srwatson /* 3682164032Srwatson * Allow root in jail to manage a variety of quota 3683166831Srwatson * properties. These should likely be conditional on a 3684166831Srwatson * configuration option. 3685164032Srwatson */ 3686166832Srwatson case PRIV_VFS_GETQUOTA: 3687166832Srwatson case PRIV_VFS_SETQUOTA: 3688164032Srwatson 3689164032Srwatson /* 3690164032Srwatson * Since Jail relies on chroot() to implement file system 3691164032Srwatson * protections, grant many VFS privileges to root in jail. 3692164032Srwatson * Be careful to exclude mount-related and NFS-related 3693164032Srwatson * privileges. 3694164032Srwatson */ 3695164032Srwatson case PRIV_VFS_READ: 3696164032Srwatson case PRIV_VFS_WRITE: 3697164032Srwatson case PRIV_VFS_ADMIN: 3698164032Srwatson case PRIV_VFS_EXEC: 3699164032Srwatson case PRIV_VFS_LOOKUP: 3700164032Srwatson case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */ 3701164032Srwatson case PRIV_VFS_CHFLAGS_DEV: 3702164032Srwatson case PRIV_VFS_CHOWN: 3703164032Srwatson case PRIV_VFS_CHROOT: 3704167152Spjd case PRIV_VFS_RETAINSUGID: 3705164032Srwatson case PRIV_VFS_FCHROOT: 3706164032Srwatson case PRIV_VFS_LINK: 3707164032Srwatson case PRIV_VFS_SETGID: 3708172860Srwatson case PRIV_VFS_STAT: 3709164032Srwatson case PRIV_VFS_STICKYFILE: 3710164032Srwatson return (0); 3711164032Srwatson 3712164032Srwatson /* 3713164032Srwatson * Depending on the global setting, allow privilege of 3714164032Srwatson * setting system flags. 3715164032Srwatson */ 3716164032Srwatson case PRIV_VFS_SYSFLAGS: 3717192895Sjamie if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS) 3718164032Srwatson return (0); 3719164032Srwatson else 3720164032Srwatson return (EPERM); 3721164032Srwatson 3722164032Srwatson /* 3723168396Spjd * Depending on the global setting, allow privilege of 3724168396Spjd * mounting/unmounting file systems. 3725168396Spjd */ 3726168396Spjd case PRIV_VFS_MOUNT: 3727168396Spjd case PRIV_VFS_UNMOUNT: 3728168396Spjd case PRIV_VFS_MOUNT_NONUSER: 3729168699Spjd case PRIV_VFS_MOUNT_OWNER: 3730192895Sjamie if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT) 3731168396Spjd return (0); 3732168396Spjd else 3733168396Spjd return (EPERM); 3734168396Spjd 3735168396Spjd /* 3736168591Srwatson * Allow jailed root to bind reserved ports and reuse in-use 3737168591Srwatson * ports. 3738164032Srwatson */ 3739164032Srwatson case PRIV_NETINET_RESERVEDPORT: 3740168591Srwatson case PRIV_NETINET_REUSEPORT: 3741164032Srwatson return (0); 3742164032Srwatson 3743164032Srwatson /* 3744175630Sbz * Allow jailed root to set certian IPv4/6 (option) headers. 3745175630Sbz */ 3746175630Sbz case PRIV_NETINET_SETHDROPTS: 3747175630Sbz return (0); 3748175630Sbz 3749175630Sbz /* 3750164032Srwatson * Conditionally allow creating raw sockets in jail. 3751164032Srwatson */ 3752164032Srwatson case PRIV_NETINET_RAW: 3753192895Sjamie if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS) 3754164032Srwatson return (0); 3755164032Srwatson else 3756164032Srwatson return (EPERM); 3757164032Srwatson 3758164032Srwatson /* 3759164032Srwatson * Since jail implements its own visibility limits on netstat 3760164032Srwatson * sysctls, allow getcred. This allows identd to work in 3761164032Srwatson * jail. 3762164032Srwatson */ 3763164032Srwatson case PRIV_NETINET_GETCRED: 3764164032Srwatson return (0); 3765164032Srwatson 3766164032Srwatson default: 3767164032Srwatson /* 3768164032Srwatson * In all remaining cases, deny the privilege request. This 3769164032Srwatson * includes almost all network privileges, many system 3770164032Srwatson * configuration privileges. 3771164032Srwatson */ 3772164032Srwatson return (EPERM); 3773164032Srwatson } 3774164032Srwatson} 3775164032Srwatson 3776192895Sjamie/* 3777192895Sjamie * Return the part of pr2's name that is relative to pr1, or the whole name 3778192895Sjamie * if it does not directly follow. 3779192895Sjamie */ 3780192895Sjamie 3781192895Sjamiechar * 3782192895Sjamieprison_name(struct prison *pr1, struct prison *pr2) 3783192895Sjamie{ 3784192895Sjamie char *name; 3785192895Sjamie 3786192895Sjamie /* Jails see themselves as "0" (if they see themselves at all). */ 3787192895Sjamie if (pr1 == pr2) 3788192895Sjamie return "0"; 3789192895Sjamie name = pr2->pr_name; 3790192895Sjamie if (prison_ischild(pr1, pr2)) { 3791192895Sjamie /* 3792192895Sjamie * pr1 isn't locked (and allprison_lock may not be either) 3793192895Sjamie * so its length can't be counted on. But the number of dots 3794192895Sjamie * can be counted on - and counted. 3795192895Sjamie */ 3796192895Sjamie for (; pr1 != &prison0; pr1 = pr1->pr_parent) 3797192895Sjamie name = strchr(name, '.') + 1; 3798192895Sjamie } 3799192895Sjamie return (name); 3800192895Sjamie} 3801192895Sjamie 3802192895Sjamie/* 3803192895Sjamie * Return the part of pr2's path that is relative to pr1, or the whole path 3804192895Sjamie * if it does not directly follow. 3805192895Sjamie */ 3806192895Sjamiestatic char * 3807192895Sjamieprison_path(struct prison *pr1, struct prison *pr2) 3808192895Sjamie{ 3809192895Sjamie char *path1, *path2; 3810192895Sjamie int len1; 3811192895Sjamie 3812192895Sjamie path1 = pr1->pr_path; 3813192895Sjamie path2 = pr2->pr_path; 3814192895Sjamie if (!strcmp(path1, "/")) 3815192895Sjamie return (path2); 3816192895Sjamie len1 = strlen(path1); 3817192895Sjamie if (strncmp(path1, path2, len1)) 3818192895Sjamie return (path2); 3819192895Sjamie if (path2[len1] == '\0') 3820192895Sjamie return "/"; 3821192895Sjamie if (path2[len1] == '/') 3822192895Sjamie return (path2 + len1); 3823192895Sjamie return (path2); 3824192895Sjamie} 3825192895Sjamie 3826192895Sjamie 3827192895Sjamie/* 3828192895Sjamie * Jail-related sysctls. 3829192895Sjamie */ 3830192895SjamieSYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0, 3831192895Sjamie "Jails"); 3832192895Sjamie 3833113275Smikestatic int 3834113275Smikesysctl_jail_list(SYSCTL_HANDLER_ARGS) 3835113275Smike{ 3836191673Sjamie struct xprison *xp; 3837192895Sjamie struct prison *pr, *cpr; 3838191673Sjamie#ifdef INET 3839191673Sjamie struct in_addr *ip4 = NULL; 3840191673Sjamie int ip4s = 0; 3841191673Sjamie#endif 3842191673Sjamie#ifdef INET6 3843191673Sjamie struct in_addr *ip6 = NULL; 3844191673Sjamie int ip6s = 0; 3845191673Sjamie#endif 3846192895Sjamie int descend, error; 3847113275Smike 3848191673Sjamie xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK); 3849192895Sjamie pr = req->td->td_ucred->cr_prison; 3850191673Sjamie error = 0; 3851168401Spjd sx_slock(&allprison_lock); 3852192895Sjamie FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 3853192895Sjamie#if defined(INET) || defined(INET6) 3854191673Sjamie again: 3855192895Sjamie#endif 3856192895Sjamie mtx_lock(&cpr->pr_mtx); 3857185435Sbz#ifdef INET 3858192895Sjamie if (cpr->pr_ip4s > 0) { 3859192895Sjamie if (ip4s < cpr->pr_ip4s) { 3860192895Sjamie ip4s = cpr->pr_ip4s; 3861192895Sjamie mtx_unlock(&cpr->pr_mtx); 3862191673Sjamie ip4 = realloc(ip4, ip4s * 3863191673Sjamie sizeof(struct in_addr), M_TEMP, M_WAITOK); 3864191673Sjamie goto again; 3865191673Sjamie } 3866192895Sjamie bcopy(cpr->pr_ip4, ip4, 3867192895Sjamie cpr->pr_ip4s * sizeof(struct in_addr)); 3868191673Sjamie } 3869185435Sbz#endif 3870185435Sbz#ifdef INET6 3871192895Sjamie if (cpr->pr_ip6s > 0) { 3872192895Sjamie if (ip6s < cpr->pr_ip6s) { 3873192895Sjamie ip6s = cpr->pr_ip6s; 3874192895Sjamie mtx_unlock(&cpr->pr_mtx); 3875191673Sjamie ip6 = realloc(ip6, ip6s * 3876191673Sjamie sizeof(struct in6_addr), M_TEMP, M_WAITOK); 3877191673Sjamie goto again; 3878191673Sjamie } 3879192895Sjamie bcopy(cpr->pr_ip6, ip6, 3880192895Sjamie cpr->pr_ip6s * sizeof(struct in6_addr)); 3881191673Sjamie } 3882185435Sbz#endif 3883192895Sjamie if (cpr->pr_ref == 0) { 3884192895Sjamie mtx_unlock(&cpr->pr_mtx); 3885191673Sjamie continue; 3886191673Sjamie } 3887191673Sjamie bzero(xp, sizeof(*xp)); 3888113275Smike xp->pr_version = XPRISON_VERSION; 3889192895Sjamie xp->pr_id = cpr->pr_id; 3890192895Sjamie xp->pr_state = cpr->pr_uref > 0 3891191673Sjamie ? PRISON_STATE_ALIVE : PRISON_STATE_DYING; 3892192895Sjamie strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path)); 3893194118Sjamie strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host)); 3894192895Sjamie strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name)); 3895185435Sbz#ifdef INET 3896192895Sjamie xp->pr_ip4s = cpr->pr_ip4s; 3897185435Sbz#endif 3898185435Sbz#ifdef INET6 3899192895Sjamie xp->pr_ip6s = cpr->pr_ip6s; 3900185435Sbz#endif 3901192895Sjamie mtx_unlock(&cpr->pr_mtx); 3902191673Sjamie error = SYSCTL_OUT(req, xp, sizeof(*xp)); 3903191673Sjamie if (error) 3904191673Sjamie break; 3905185435Sbz#ifdef INET 3906191673Sjamie if (xp->pr_ip4s > 0) { 3907191673Sjamie error = SYSCTL_OUT(req, ip4, 3908191673Sjamie xp->pr_ip4s * sizeof(struct in_addr)); 3909191673Sjamie if (error) 3910191673Sjamie break; 3911185435Sbz } 3912185435Sbz#endif 3913185435Sbz#ifdef INET6 3914191673Sjamie if (xp->pr_ip6s > 0) { 3915191673Sjamie error = SYSCTL_OUT(req, ip6, 3916191673Sjamie xp->pr_ip6s * sizeof(struct in6_addr)); 3917191673Sjamie if (error) 3918191673Sjamie break; 3919185435Sbz } 3920185435Sbz#endif 3921113275Smike } 3922168401Spjd sx_sunlock(&allprison_lock); 3923191673Sjamie free(xp, M_TEMP); 3924191673Sjamie#ifdef INET 3925191673Sjamie free(ip4, M_TEMP); 3926191673Sjamie#endif 3927191673Sjamie#ifdef INET6 3928191673Sjamie free(ip6, M_TEMP); 3929191673Sjamie#endif 3930167354Spjd return (error); 3931113275Smike} 3932113275Smike 3933187864SedSYSCTL_OID(_security_jail, OID_AUTO, list, 3934187864Sed CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 3935187864Sed sysctl_jail_list, "S", "List of active jails"); 3936126004Spjd 3937126004Spjdstatic int 3938126004Spjdsysctl_jail_jailed(SYSCTL_HANDLER_ARGS) 3939126004Spjd{ 3940126004Spjd int error, injail; 3941126004Spjd 3942126004Spjd injail = jailed(req->td->td_ucred); 3943126004Spjd error = SYSCTL_OUT(req, &injail, sizeof(injail)); 3944126004Spjd 3945126004Spjd return (error); 3946126004Spjd} 3947192895Sjamie 3948187864SedSYSCTL_PROC(_security_jail, OID_AUTO, jailed, 3949187864Sed CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 3950187864Sed sysctl_jail_jailed, "I", "Process in jail?"); 3951185435Sbz 3952192895Sjamie#if defined(INET) || defined(INET6) 3953193865SjamieSYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW, 3954192895Sjamie &jail_max_af_ips, 0, 3955192895Sjamie "Number of IP addresses a jail may have at most per address family"); 3956192895Sjamie#endif 3957192895Sjamie 3958192895Sjamie/* 3959192895Sjamie * Default parameters for jail(2) compatability. For historical reasons, 3960192895Sjamie * the sysctl names have varying similarity to the parameter names. Prisons 3961192895Sjamie * just see their own parameters, and can't change them. 3962192895Sjamie */ 3963192895Sjamiestatic int 3964192895Sjamiesysctl_jail_default_allow(SYSCTL_HANDLER_ARGS) 3965192895Sjamie{ 3966192895Sjamie struct prison *pr; 3967192895Sjamie int allow, error, i; 3968192895Sjamie 3969192895Sjamie pr = req->td->td_ucred->cr_prison; 3970192895Sjamie allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow; 3971192895Sjamie 3972192895Sjamie /* Get the current flag value, and convert it to a boolean. */ 3973192895Sjamie i = (allow & arg2) ? 1 : 0; 3974192895Sjamie if (arg1 != NULL) 3975192895Sjamie i = !i; 3976192895Sjamie error = sysctl_handle_int(oidp, &i, 0, req); 3977192895Sjamie if (error || !req->newptr) 3978192895Sjamie return (error); 3979192895Sjamie i = i ? arg2 : 0; 3980192895Sjamie if (arg1 != NULL) 3981192895Sjamie i ^= arg2; 3982192895Sjamie /* 3983192895Sjamie * The sysctls don't have CTLFLAGS_PRISON, so assume prison0 3984192895Sjamie * for writing. 3985192895Sjamie */ 3986192895Sjamie mtx_lock(&prison0.pr_mtx); 3987192895Sjamie jail_default_allow = (jail_default_allow & ~arg2) | i; 3988192895Sjamie mtx_unlock(&prison0.pr_mtx); 3989192895Sjamie return (0); 3990192895Sjamie} 3991192895Sjamie 3992192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed, 3993192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 3994192895Sjamie NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I", 3995192895Sjamie "Processes in jail can set their hostnames"); 3996192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only, 3997192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 3998192895Sjamie (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I", 3999192895Sjamie "Processes in jail are limited to creating UNIX/IP/route sockets only"); 4000192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed, 4001192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4002192895Sjamie NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I", 4003192895Sjamie "Processes in jail can use System V IPC primitives"); 4004192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets, 4005192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4006192895Sjamie NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I", 4007192895Sjamie "Prison root can create raw sockets"); 4008192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed, 4009192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4010192895Sjamie NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I", 4011192895Sjamie "Processes in jail can alter system file flags"); 4012192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed, 4013192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4014192895Sjamie NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I", 4015192895Sjamie "Processes in jail can mount/unmount jail-friendly file systems"); 4016192895Sjamie 4017192895Sjamiestatic int 4018192895Sjamiesysctl_jail_default_level(SYSCTL_HANDLER_ARGS) 4019192895Sjamie{ 4020192895Sjamie struct prison *pr; 4021192895Sjamie int level, error; 4022192895Sjamie 4023192895Sjamie pr = req->td->td_ucred->cr_prison; 4024192895Sjamie level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2); 4025192895Sjamie error = sysctl_handle_int(oidp, &level, 0, req); 4026192895Sjamie if (error || !req->newptr) 4027192895Sjamie return (error); 4028192895Sjamie *(int *)arg1 = level; 4029192895Sjamie return (0); 4030192895Sjamie} 4031192895Sjamie 4032192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs, 4033192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4034192895Sjamie &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs), 4035192895Sjamie sysctl_jail_default_level, "I", 4036192895Sjamie "Processes in jail cannot see all mounted file systems"); 4037192895Sjamie 4038192895Sjamie/* 4039192895Sjamie * Nodes to describe jail parameters. Maximum length of string parameters 4040192895Sjamie * is returned in the string itself, and the other parameters exist merely 4041192895Sjamie * to make themselves and their types known. 4042192895Sjamie */ 4043192895SjamieSYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0, 4044192895Sjamie "Jail parameters"); 4045192895Sjamie 4046192895Sjamieint 4047192895Sjamiesysctl_jail_param(SYSCTL_HANDLER_ARGS) 4048192895Sjamie{ 4049192895Sjamie int i; 4050192895Sjamie long l; 4051192895Sjamie size_t s; 4052192895Sjamie char numbuf[12]; 4053192895Sjamie 4054192895Sjamie switch (oidp->oid_kind & CTLTYPE) 4055192895Sjamie { 4056192895Sjamie case CTLTYPE_LONG: 4057192895Sjamie case CTLTYPE_ULONG: 4058192895Sjamie l = 0; 4059192895Sjamie#ifdef SCTL_MASK32 4060192895Sjamie if (!(req->flags & SCTL_MASK32)) 4061192895Sjamie#endif 4062192895Sjamie return (SYSCTL_OUT(req, &l, sizeof(l))); 4063192895Sjamie case CTLTYPE_INT: 4064192895Sjamie case CTLTYPE_UINT: 4065192895Sjamie i = 0; 4066192895Sjamie return (SYSCTL_OUT(req, &i, sizeof(i))); 4067192895Sjamie case CTLTYPE_STRING: 4068192895Sjamie snprintf(numbuf, sizeof(numbuf), "%d", arg2); 4069192895Sjamie return 4070192895Sjamie (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req)); 4071192895Sjamie case CTLTYPE_STRUCT: 4072192895Sjamie s = (size_t)arg2; 4073192895Sjamie return (SYSCTL_OUT(req, &s, sizeof(s))); 4074192895Sjamie } 4075192895Sjamie return (0); 4076192895Sjamie} 4077192895Sjamie 4078192895SjamieSYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID"); 4079192895SjamieSYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID"); 4080192895SjamieSYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name"); 4081192895SjamieSYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path"); 4082192895SjamieSYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW, 4083192895Sjamie "I", "Jail secure level"); 4084192895SjamieSYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW, 4085192895Sjamie "I", "Jail cannot see all mounted file systems"); 4086192895SjamieSYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW, 4087192895Sjamie "B", "Jail persistence"); 4088194251Sjamie#ifdef VIMAGE 4089194251SjamieSYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN, 4090195870Sjamie "E,jailsys", "Virtual network stack"); 4091194251Sjamie#endif 4092192895SjamieSYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD, 4093192895Sjamie "B", "Jail is in the process of shutting down"); 4094192895Sjamie 4095194762SjamieSYSCTL_JAIL_PARAM_NODE(children, "Number of child jails"); 4096194762SjamieSYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD, 4097194762Sjamie "I", "Current number of child jails"); 4098194762SjamieSYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW, 4099194762Sjamie "I", "Maximum number of child jails"); 4100194762Sjamie 4101195870SjamieSYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info"); 4102192895SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN, 4103192895Sjamie "Jail hostname"); 4104193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN, 4105193066Sjamie "Jail NIS domainname"); 4106193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN, 4107193066Sjamie "Jail host UUID"); 4108193066SjamieSYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW, 4109193066Sjamie "LU", "Jail host ID"); 4110192895Sjamie 4111192895SjamieSYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset"); 4112192895SjamieSYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID"); 4113192895Sjamie 4114192895Sjamie#ifdef INET 4115195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN, 4116195974Sjamie "Jail IPv4 address virtualization"); 4117192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr), 4118192895Sjamie "S,in_addr,a", "Jail IPv4 addresses"); 4119192895Sjamie#endif 4120192895Sjamie#ifdef INET6 4121195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN, 4122195974Sjamie "Jail IPv6 address virtualization"); 4123192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr), 4124192895Sjamie "S,in6_addr,a", "Jail IPv6 addresses"); 4125192895Sjamie#endif 4126192895Sjamie 4127192895SjamieSYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags"); 4128192895SjamieSYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW, 4129192895Sjamie "B", "Jail may set hostname"); 4130192895SjamieSYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW, 4131192895Sjamie "B", "Jail may use SYSV IPC"); 4132192895SjamieSYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW, 4133192895Sjamie "B", "Jail may create raw sockets"); 4134192895SjamieSYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW, 4135192895Sjamie "B", "Jail may alter system file flags"); 4136192895SjamieSYSCTL_JAIL_PARAM(_allow, mount, CTLTYPE_INT | CTLFLAG_RW, 4137192895Sjamie "B", "Jail may mount/unmount jail-friendly file systems"); 4138192895SjamieSYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW, 4139192895Sjamie "B", "Jail may set file quotas"); 4140192895SjamieSYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW, 4141192895Sjamie "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route"); 4142192895Sjamie 4143192895Sjamie 4144185435Sbz#ifdef DDB 4145191673Sjamie 4146191673Sjamiestatic void 4147191673Sjamiedb_show_prison(struct prison *pr) 4148185435Sbz{ 4149192895Sjamie int fi; 4150191673Sjamie#if defined(INET) || defined(INET6) 4151191673Sjamie int ii; 4152185435Sbz#endif 4153195870Sjamie unsigned jsf; 4154185435Sbz#ifdef INET6 4155185435Sbz char ip6buf[INET6_ADDRSTRLEN]; 4156185435Sbz#endif 4157185435Sbz 4158191673Sjamie db_printf("prison %p:\n", pr); 4159191673Sjamie db_printf(" jid = %d\n", pr->pr_id); 4160191673Sjamie db_printf(" name = %s\n", pr->pr_name); 4161192895Sjamie db_printf(" parent = %p\n", pr->pr_parent); 4162191673Sjamie db_printf(" ref = %d\n", pr->pr_ref); 4163191673Sjamie db_printf(" uref = %d\n", pr->pr_uref); 4164191673Sjamie db_printf(" path = %s\n", pr->pr_path); 4165191673Sjamie db_printf(" cpuset = %d\n", pr->pr_cpuset 4166191673Sjamie ? pr->pr_cpuset->cs_id : -1); 4167194251Sjamie#ifdef VIMAGE 4168194251Sjamie db_printf(" vnet = %p\n", pr->pr_vnet); 4169194251Sjamie#endif 4170191673Sjamie db_printf(" root = %p\n", pr->pr_root); 4171191673Sjamie db_printf(" securelevel = %d\n", pr->pr_securelevel); 4172194762Sjamie db_printf(" childcount = %d\n", pr->pr_childcount); 4173192895Sjamie db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children)); 4174192895Sjamie db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling)); 4175191673Sjamie db_printf(" flags = %x", pr->pr_flags); 4176192895Sjamie for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 4177192895Sjamie fi++) 4178192895Sjamie if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi))) 4179192895Sjamie db_printf(" %s", pr_flag_names[fi]); 4180195870Sjamie for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 4181195870Sjamie fi++) { 4182195870Sjamie jsf = pr->pr_flags & 4183195870Sjamie (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new); 4184195870Sjamie db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name, 4185195870Sjamie pr_flag_jailsys[fi].disable && 4186195870Sjamie (jsf == pr_flag_jailsys[fi].disable) ? "disable" 4187195870Sjamie : (jsf == pr_flag_jailsys[fi].new) ? "new" 4188195870Sjamie : "inherit"); 4189195870Sjamie } 4190192895Sjamie db_printf(" allow = %x", pr->pr_allow); 4191192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 4192192895Sjamie fi++) 4193192895Sjamie if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi))) 4194192895Sjamie db_printf(" %s", pr_allow_names[fi]); 4195191673Sjamie db_printf("\n"); 4196192895Sjamie db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs); 4197194118Sjamie db_printf(" host.hostname = %s\n", pr->pr_hostname); 4198194118Sjamie db_printf(" host.domainname = %s\n", pr->pr_domainname); 4199194118Sjamie db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid); 4200193066Sjamie db_printf(" host.hostid = %lu\n", pr->pr_hostid); 4201185435Sbz#ifdef INET 4202191673Sjamie db_printf(" ip4s = %d\n", pr->pr_ip4s); 4203191673Sjamie for (ii = 0; ii < pr->pr_ip4s; ii++) 4204191673Sjamie db_printf(" %s %s\n", 4205191673Sjamie ii == 0 ? "ip4 =" : " ", 4206191673Sjamie inet_ntoa(pr->pr_ip4[ii])); 4207185435Sbz#endif 4208185435Sbz#ifdef INET6 4209191673Sjamie db_printf(" ip6s = %d\n", pr->pr_ip6s); 4210191673Sjamie for (ii = 0; ii < pr->pr_ip6s; ii++) 4211191673Sjamie db_printf(" %s %s\n", 4212191673Sjamie ii == 0 ? "ip6 =" : " ", 4213191673Sjamie ip6_sprintf(ip6buf, &pr->pr_ip6[ii])); 4214191673Sjamie#endif 4215191673Sjamie} 4216191673Sjamie 4217191673SjamieDB_SHOW_COMMAND(prison, db_show_prison_command) 4218191673Sjamie{ 4219191673Sjamie struct prison *pr; 4220191673Sjamie 4221191673Sjamie if (!have_addr) { 4222192895Sjamie /* 4223192895Sjamie * Show all prisons in the list, and prison0 which is not 4224192895Sjamie * listed. 4225192895Sjamie */ 4226192895Sjamie db_show_prison(&prison0); 4227192895Sjamie if (!db_pager_quit) { 4228192895Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) { 4229192895Sjamie db_show_prison(pr); 4230192895Sjamie if (db_pager_quit) 4231192895Sjamie break; 4232192895Sjamie } 4233191673Sjamie } 4234191673Sjamie return; 4235191673Sjamie } 4236191673Sjamie 4237192895Sjamie if (addr == 0) 4238192895Sjamie pr = &prison0; 4239192895Sjamie else { 4240192895Sjamie /* Look for a prison with the ID and with references. */ 4241191673Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) 4242192895Sjamie if (pr->pr_id == addr && pr->pr_ref > 0) 4243191673Sjamie break; 4244192895Sjamie if (pr == NULL) 4245192895Sjamie /* Look again, without requiring a reference. */ 4246192895Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) 4247192895Sjamie if (pr->pr_id == addr) 4248192895Sjamie break; 4249192895Sjamie if (pr == NULL) 4250192895Sjamie /* Assume address points to a valid prison. */ 4251192895Sjamie pr = (struct prison *)addr; 4252192895Sjamie } 4253191673Sjamie db_show_prison(pr); 4254185435Sbz} 4255191673Sjamie 4256185435Sbz#endif /* DDB */ 4257