kern_jail.c revision 231267
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 231267 2012-02-09 10:22:08Z mm $"); 31116182Sobrien 32193066Sjamie#include "opt_compat.h" 33185435Sbz#include "opt_ddb.h" 34185435Sbz#include "opt_inet.h" 35185435Sbz#include "opt_inet6.h" 36131177Spjd 3746155Sphk#include <sys/param.h> 3846155Sphk#include <sys/types.h> 3946155Sphk#include <sys/kernel.h> 4046155Sphk#include <sys/systm.h> 4146155Sphk#include <sys/errno.h> 4246155Sphk#include <sys/sysproto.h> 4346155Sphk#include <sys/malloc.h> 44192895Sjamie#include <sys/osd.h> 45164032Srwatson#include <sys/priv.h> 4646155Sphk#include <sys/proc.h> 47124882Srwatson#include <sys/taskqueue.h> 48177785Skib#include <sys/fcntl.h> 4946155Sphk#include <sys/jail.h> 5087275Srwatson#include <sys/lock.h> 5187275Srwatson#include <sys/mutex.h> 52220137Strasz#include <sys/racct.h> 53221362Strasz#include <sys/refcount.h> 54168401Spjd#include <sys/sx.h> 55193066Sjamie#include <sys/sysent.h> 56113275Smike#include <sys/namei.h> 57147185Spjd#include <sys/mount.h> 58113275Smike#include <sys/queue.h> 5946155Sphk#include <sys/socket.h> 60113275Smike#include <sys/syscallsubr.h> 6157163Srwatson#include <sys/sysctl.h> 62113275Smike#include <sys/vnode.h> 63196019Srwatson 6446155Sphk#include <net/if.h> 65196019Srwatson#include <net/vnet.h> 66196019Srwatson 6746155Sphk#include <netinet/in.h> 68196019Srwatson 69185435Sbz#ifdef DDB 70185435Sbz#include <ddb/ddb.h> 71185435Sbz#ifdef INET6 72185435Sbz#include <netinet6/in6_var.h> 73185435Sbz#endif /* INET6 */ 74185435Sbz#endif /* DDB */ 7546155Sphk 76163606Srwatson#include <security/mac/mac_framework.h> 77163606Srwatson 78195944Sjamie#define DEFAULT_HOSTUUID "00000000-0000-0000-0000-000000000000" 79195944Sjamie 8046155SphkMALLOC_DEFINE(M_PRISON, "prison", "Prison structures"); 81227293Sedstatic MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures"); 8246155Sphk 83202468Sbz/* Keep struct prison prison0 and some code in kern_jail_set() readable. */ 84202468Sbz#ifdef INET 85202468Sbz#ifdef INET6 86202468Sbz#define _PR_IP_SADDRSEL PR_IP4_SADDRSEL|PR_IP6_SADDRSEL 87202468Sbz#else 88202468Sbz#define _PR_IP_SADDRSEL PR_IP4_SADDRSEL 89202468Sbz#endif 90202468Sbz#else /* !INET */ 91202468Sbz#ifdef INET6 92202468Sbz#define _PR_IP_SADDRSEL PR_IP6_SADDRSEL 93202468Sbz#else 94202468Sbz#define _PR_IP_SADDRSEL 0 95202468Sbz#endif 96202468Sbz#endif 97202468Sbz 98192895Sjamie/* prison0 describes what is "real" about the system. */ 99192895Sjamiestruct prison prison0 = { 100192895Sjamie .pr_id = 0, 101192895Sjamie .pr_name = "0", 102192895Sjamie .pr_ref = 1, 103192895Sjamie .pr_uref = 1, 104192895Sjamie .pr_path = "/", 105192895Sjamie .pr_securelevel = -1, 106231267Smm .pr_devfs_rsnum = 0, 107194762Sjamie .pr_childmax = JAIL_MAX, 108195944Sjamie .pr_hostuuid = DEFAULT_HOSTUUID, 109201145Santoine .pr_children = LIST_HEAD_INITIALIZER(prison0.pr_children), 110196176Sbz#ifdef VIMAGE 111202468Sbz .pr_flags = PR_HOST|PR_VNET|_PR_IP_SADDRSEL, 112196176Sbz#else 113202468Sbz .pr_flags = PR_HOST|_PR_IP_SADDRSEL, 114196176Sbz#endif 115192895Sjamie .pr_allow = PR_ALLOW_ALL, 116192895Sjamie}; 117192895SjamieMTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF); 11857163Srwatson 119221362Strasz/* allprison, allprison_racct and lastprid are protected by allprison_lock. */ 120168401Spjdstruct sx allprison_lock; 121191673SjamieSX_SYSINIT(allprison_lock, &allprison_lock, "allprison"); 122191673Sjamiestruct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison); 123221362StraszLIST_HEAD(, prison_racct) allprison_racct; 124179881Sdelphijint lastprid = 0; 125113275Smike 126191673Sjamiestatic int do_jail_attach(struct thread *td, struct prison *pr); 127190466Sjamiestatic void prison_complete(void *context, int pending); 128191673Sjamiestatic void prison_deref(struct prison *pr, int flags); 129192895Sjamiestatic char *prison_path(struct prison *pr1, struct prison *pr2); 130192895Sjamiestatic void prison_remove_one(struct prison *pr); 131221362Strasz#ifdef RACCT 132221362Straszstatic void prison_racct_attach(struct prison *pr); 133221362Straszstatic void prison_racct_detach(struct prison *pr); 134221362Strasz#endif 135185435Sbz#ifdef INET 136190466Sjamiestatic int _prison_check_ip4(struct prison *pr, struct in_addr *ia); 137192895Sjamiestatic int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4); 138185435Sbz#endif 139185435Sbz#ifdef INET6 140190466Sjamiestatic int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6); 141192895Sjamiestatic int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6); 142185435Sbz#endif 143113275Smike 144191673Sjamie/* Flags for prison_deref */ 145191673Sjamie#define PD_DEREF 0x01 146191673Sjamie#define PD_DEUREF 0x02 147191673Sjamie#define PD_LOCKED 0x04 148191673Sjamie#define PD_LIST_SLOCKED 0x08 149191673Sjamie#define PD_LIST_XLOCKED 0x10 150113275Smike 151192895Sjamie/* 152216861Sbz * Parameter names corresponding to PR_* flag values. Size values are for kvm 153216861Sbz * as we cannot figure out the size of a sparse array, or an array without a 154216861Sbz * terminating entry. 155192895Sjamie */ 156192895Sjamiestatic char *pr_flag_names[] = { 157192895Sjamie [0] = "persist", 158202468Sbz#ifdef INET 159202468Sbz [7] = "ip4.saddrsel", 160202468Sbz#endif 161202468Sbz#ifdef INET6 162202468Sbz [8] = "ip6.saddrsel", 163202468Sbz#endif 164192895Sjamie}; 165216861Sbzconst size_t pr_flag_names_size = sizeof(pr_flag_names); 166192895Sjamie 167192895Sjamiestatic char *pr_flag_nonames[] = { 168192895Sjamie [0] = "nopersist", 169202468Sbz#ifdef INET 170202468Sbz [7] = "ip4.nosaddrsel", 171202468Sbz#endif 172202468Sbz#ifdef INET6 173202468Sbz [8] = "ip6.nosaddrsel", 174202468Sbz#endif 175195870Sjamie}; 176216861Sbzconst size_t pr_flag_nonames_size = sizeof(pr_flag_nonames); 177195870Sjamie 178195870Sjamiestruct jailsys_flags { 179195870Sjamie const char *name; 180195870Sjamie unsigned disable; 181195870Sjamie unsigned new; 182195870Sjamie} pr_flag_jailsys[] = { 183195870Sjamie { "host", 0, PR_HOST }, 184195870Sjamie#ifdef VIMAGE 185195870Sjamie { "vnet", 0, PR_VNET }, 186195870Sjamie#endif 187192895Sjamie#ifdef INET 188195870Sjamie { "ip4", PR_IP4_USER | PR_IP4_DISABLE, PR_IP4_USER }, 189192895Sjamie#endif 190192895Sjamie#ifdef INET6 191195870Sjamie { "ip6", PR_IP6_USER | PR_IP6_DISABLE, PR_IP6_USER }, 192192895Sjamie#endif 193192895Sjamie}; 194216861Sbzconst size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys); 195192895Sjamie 196192895Sjamiestatic char *pr_allow_names[] = { 197192895Sjamie "allow.set_hostname", 198192895Sjamie "allow.sysvipc", 199192895Sjamie "allow.raw_sockets", 200192895Sjamie "allow.chflags", 201192895Sjamie "allow.mount", 202192895Sjamie "allow.quotas", 203192895Sjamie "allow.socket_af", 204192895Sjamie}; 205216861Sbzconst size_t pr_allow_names_size = sizeof(pr_allow_names); 206192895Sjamie 207192895Sjamiestatic char *pr_allow_nonames[] = { 208192895Sjamie "allow.noset_hostname", 209192895Sjamie "allow.nosysvipc", 210192895Sjamie "allow.noraw_sockets", 211192895Sjamie "allow.nochflags", 212192895Sjamie "allow.nomount", 213192895Sjamie "allow.noquotas", 214192895Sjamie "allow.nosocket_af", 215192895Sjamie}; 216216861Sbzconst size_t pr_allow_nonames_size = sizeof(pr_allow_nonames); 217192895Sjamie 218196002Sjamie#define JAIL_DEFAULT_ALLOW PR_ALLOW_SET_HOSTNAME 219196002Sjamie#define JAIL_DEFAULT_ENFORCE_STATFS 2 220231267Smm#define JAIL_DEFAULT_DEVFS_RSNUM -1 221192895Sjamiestatic unsigned jail_default_allow = JAIL_DEFAULT_ALLOW; 222196002Sjamiestatic int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 223231267Smmstatic int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM; 224192895Sjamie#if defined(INET) || defined(INET6) 225193865Sjamiestatic unsigned jail_max_af_ips = 255; 226192895Sjamie#endif 227192895Sjamie 228192895Sjamie#ifdef INET 229185435Sbzstatic int 230185435Sbzqcmp_v4(const void *ip1, const void *ip2) 231185435Sbz{ 232185435Sbz in_addr_t iaa, iab; 233185435Sbz 234185435Sbz /* 235185435Sbz * We need to compare in HBO here to get the list sorted as expected 236185435Sbz * by the result of the code. Sorting NBO addresses gives you 237185435Sbz * interesting results. If you do not understand, do not try. 238185435Sbz */ 239185435Sbz iaa = ntohl(((const struct in_addr *)ip1)->s_addr); 240185435Sbz iab = ntohl(((const struct in_addr *)ip2)->s_addr); 241185435Sbz 242185435Sbz /* 243185435Sbz * Do not simply return the difference of the two numbers, the int is 244185435Sbz * not wide enough. 245185435Sbz */ 246185435Sbz if (iaa > iab) 247185435Sbz return (1); 248185435Sbz else if (iaa < iab) 249185435Sbz return (-1); 250185435Sbz else 251185435Sbz return (0); 252185435Sbz} 253185435Sbz#endif 254185435Sbz 255185435Sbz#ifdef INET6 256185435Sbzstatic int 257185435Sbzqcmp_v6(const void *ip1, const void *ip2) 258185435Sbz{ 259185435Sbz const struct in6_addr *ia6a, *ia6b; 260185435Sbz int i, rc; 261185435Sbz 262185435Sbz ia6a = (const struct in6_addr *)ip1; 263185435Sbz ia6b = (const struct in6_addr *)ip2; 264185435Sbz 265185435Sbz rc = 0; 266190466Sjamie for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) { 267185435Sbz if (ia6a->s6_addr[i] > ia6b->s6_addr[i]) 268185435Sbz rc = 1; 269185435Sbz else if (ia6a->s6_addr[i] < ia6b->s6_addr[i]) 270185435Sbz rc = -1; 271185435Sbz } 272185435Sbz return (rc); 273185435Sbz} 274185435Sbz#endif 275185435Sbz 276191673Sjamie/* 277191673Sjamie * struct jail_args { 278191673Sjamie * struct jail *jail; 279191673Sjamie * }; 280191673Sjamie */ 281191673Sjamieint 282225617Skmacysys_jail(struct thread *td, struct jail_args *uap) 283185435Sbz{ 284191673Sjamie uint32_t version; 285191673Sjamie int error; 286192895Sjamie struct jail j; 287185435Sbz 288191673Sjamie error = copyin(uap->jail, &version, sizeof(uint32_t)); 289191673Sjamie if (error) 290191673Sjamie return (error); 291185435Sbz 292191673Sjamie switch (version) { 293191673Sjamie case 0: 294191673Sjamie { 295191673Sjamie struct jail_v0 j0; 296185435Sbz 297192895Sjamie /* FreeBSD single IPv4 jails. */ 298192895Sjamie bzero(&j, sizeof(struct jail)); 299191673Sjamie error = copyin(uap->jail, &j0, sizeof(struct jail_v0)); 300191673Sjamie if (error) 301191673Sjamie return (error); 302192895Sjamie j.version = j0.version; 303192895Sjamie j.path = j0.path; 304192895Sjamie j.hostname = j0.hostname; 305192895Sjamie j.ip4s = j0.ip_number; 306191673Sjamie break; 307191673Sjamie } 308191673Sjamie 309191673Sjamie case 1: 310185435Sbz /* 311191673Sjamie * Version 1 was used by multi-IPv4 jail implementations 312191673Sjamie * that never made it into the official kernel. 313185435Sbz */ 314191673Sjamie return (EINVAL); 315185435Sbz 316191673Sjamie case 2: /* JAIL_API_VERSION */ 317191673Sjamie /* FreeBSD multi-IPv4/IPv6,noIP jails. */ 318191673Sjamie error = copyin(uap->jail, &j, sizeof(struct jail)); 319191673Sjamie if (error) 320191673Sjamie return (error); 321192895Sjamie break; 322192895Sjamie 323192895Sjamie default: 324192895Sjamie /* Sci-Fi jails are not supported, sorry. */ 325192895Sjamie return (EINVAL); 326192895Sjamie } 327192895Sjamie return (kern_jail(td, &j)); 328192895Sjamie} 329192895Sjamie 330192895Sjamieint 331192895Sjamiekern_jail(struct thread *td, struct jail *j) 332192895Sjamie{ 333193865Sjamie struct iovec optiov[2 * (4 334193865Sjamie + sizeof(pr_allow_names) / sizeof(pr_allow_names[0]) 335193865Sjamie#ifdef INET 336193865Sjamie + 1 337193865Sjamie#endif 338193865Sjamie#ifdef INET6 339193865Sjamie + 1 340193865Sjamie#endif 341193865Sjamie )]; 342192895Sjamie struct uio opt; 343192895Sjamie char *u_path, *u_hostname, *u_name; 344185435Sbz#ifdef INET 345193865Sjamie uint32_t ip4s; 346192895Sjamie struct in_addr *u_ip4; 347192895Sjamie#endif 348192895Sjamie#ifdef INET6 349192895Sjamie struct in6_addr *u_ip6; 350192895Sjamie#endif 351192895Sjamie size_t tmplen; 352192895Sjamie int error, enforce_statfs, fi; 353192895Sjamie 354192895Sjamie bzero(&optiov, sizeof(optiov)); 355192895Sjamie opt.uio_iov = optiov; 356192895Sjamie opt.uio_iovcnt = 0; 357192895Sjamie opt.uio_offset = -1; 358192895Sjamie opt.uio_resid = -1; 359192895Sjamie opt.uio_segflg = UIO_SYSSPACE; 360192895Sjamie opt.uio_rw = UIO_READ; 361192895Sjamie opt.uio_td = td; 362192895Sjamie 363192895Sjamie /* Set permissions for top-level jails from sysctls. */ 364192895Sjamie if (!jailed(td->td_ucred)) { 365192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / 366192895Sjamie sizeof(pr_allow_names[0]); fi++) { 367192895Sjamie optiov[opt.uio_iovcnt].iov_base = 368192895Sjamie (jail_default_allow & (1 << fi)) 369192895Sjamie ? pr_allow_names[fi] : pr_allow_nonames[fi]; 370192895Sjamie optiov[opt.uio_iovcnt].iov_len = 371192895Sjamie strlen(optiov[opt.uio_iovcnt].iov_base) + 1; 372192895Sjamie opt.uio_iovcnt += 2; 373192895Sjamie } 374192895Sjamie optiov[opt.uio_iovcnt].iov_base = "enforce_statfs"; 375192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs"); 376192895Sjamie opt.uio_iovcnt++; 377192895Sjamie enforce_statfs = jail_default_enforce_statfs; 378192895Sjamie optiov[opt.uio_iovcnt].iov_base = &enforce_statfs; 379192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs); 380192895Sjamie opt.uio_iovcnt++; 381192895Sjamie } 382192895Sjamie 383192895Sjamie tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN; 384192895Sjamie#ifdef INET 385192895Sjamie ip4s = (j->version == 0) ? 1 : j->ip4s; 386192895Sjamie if (ip4s > jail_max_af_ips) 387192895Sjamie return (EINVAL); 388192895Sjamie tmplen += ip4s * sizeof(struct in_addr); 389191673Sjamie#else 390192895Sjamie if (j->ip4s > 0) 391192895Sjamie return (EINVAL); 392191673Sjamie#endif 393191673Sjamie#ifdef INET6 394192895Sjamie if (j->ip6s > jail_max_af_ips) 395192895Sjamie return (EINVAL); 396192895Sjamie tmplen += j->ip6s * sizeof(struct in6_addr); 397191673Sjamie#else 398192895Sjamie if (j->ip6s > 0) 399192895Sjamie return (EINVAL); 400191673Sjamie#endif 401192895Sjamie u_path = malloc(tmplen, M_TEMP, M_WAITOK); 402192895Sjamie u_hostname = u_path + MAXPATHLEN; 403192895Sjamie u_name = u_hostname + MAXHOSTNAMELEN; 404191673Sjamie#ifdef INET 405192895Sjamie u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN); 406191673Sjamie#endif 407191673Sjamie#ifdef INET6 408191673Sjamie#ifdef INET 409192895Sjamie u_ip6 = (struct in6_addr *)(u_ip4 + ip4s); 410191673Sjamie#else 411192895Sjamie u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN); 412191673Sjamie#endif 413191673Sjamie#endif 414192895Sjamie optiov[opt.uio_iovcnt].iov_base = "path"; 415192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("path"); 416192895Sjamie opt.uio_iovcnt++; 417192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_path; 418192895Sjamie error = copyinstr(j->path, u_path, MAXPATHLEN, 419192895Sjamie &optiov[opt.uio_iovcnt].iov_len); 420192895Sjamie if (error) { 421192895Sjamie free(u_path, M_TEMP); 422192895Sjamie return (error); 423192895Sjamie } 424192895Sjamie opt.uio_iovcnt++; 425192895Sjamie optiov[opt.uio_iovcnt].iov_base = "host.hostname"; 426192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname"); 427192895Sjamie opt.uio_iovcnt++; 428192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_hostname; 429192895Sjamie error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN, 430192895Sjamie &optiov[opt.uio_iovcnt].iov_len); 431192895Sjamie if (error) { 432192895Sjamie free(u_path, M_TEMP); 433192895Sjamie return (error); 434192895Sjamie } 435192895Sjamie opt.uio_iovcnt++; 436192895Sjamie if (j->jailname != NULL) { 437192895Sjamie optiov[opt.uio_iovcnt].iov_base = "name"; 438192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("name"); 439192895Sjamie opt.uio_iovcnt++; 440192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_name; 441192895Sjamie error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN, 442192895Sjamie &optiov[opt.uio_iovcnt].iov_len); 443191673Sjamie if (error) { 444191673Sjamie free(u_path, M_TEMP); 445191673Sjamie return (error); 446191673Sjamie } 447192895Sjamie opt.uio_iovcnt++; 448192895Sjamie } 449191673Sjamie#ifdef INET 450192895Sjamie optiov[opt.uio_iovcnt].iov_base = "ip4.addr"; 451192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr"); 452192895Sjamie opt.uio_iovcnt++; 453192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_ip4; 454192895Sjamie optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr); 455192895Sjamie if (j->version == 0) 456192895Sjamie u_ip4->s_addr = j->ip4s; 457192895Sjamie else { 458192895Sjamie error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len); 459191673Sjamie if (error) { 460191673Sjamie free(u_path, M_TEMP); 461191673Sjamie return (error); 462191673Sjamie } 463192895Sjamie } 464192895Sjamie opt.uio_iovcnt++; 465185435Sbz#endif 466185435Sbz#ifdef INET6 467192895Sjamie optiov[opt.uio_iovcnt].iov_base = "ip6.addr"; 468192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr"); 469192895Sjamie opt.uio_iovcnt++; 470192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_ip6; 471192895Sjamie optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr); 472192895Sjamie error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len); 473192895Sjamie if (error) { 474192895Sjamie free(u_path, M_TEMP); 475192895Sjamie return (error); 476192895Sjamie } 477192895Sjamie opt.uio_iovcnt++; 478185435Sbz#endif 479192895Sjamie KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]), 480192895Sjamie ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt)); 481191673Sjamie error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH); 482191673Sjamie free(u_path, M_TEMP); 483191673Sjamie return (error); 484185435Sbz} 485185435Sbz 486192895Sjamie 487191673Sjamie/* 488191673Sjamie * struct jail_set_args { 489191673Sjamie * struct iovec *iovp; 490191673Sjamie * unsigned int iovcnt; 491191673Sjamie * int flags; 492191673Sjamie * }; 493191673Sjamie */ 494191673Sjamieint 495225617Skmacysys_jail_set(struct thread *td, struct jail_set_args *uap) 496185435Sbz{ 497191673Sjamie struct uio *auio; 498191673Sjamie int error; 499191673Sjamie 500191673Sjamie /* Check that we have an even number of iovecs. */ 501191673Sjamie if (uap->iovcnt & 1) 502191673Sjamie return (EINVAL); 503191673Sjamie 504191673Sjamie error = copyinuio(uap->iovp, uap->iovcnt, &auio); 505191673Sjamie if (error) 506191673Sjamie return (error); 507191673Sjamie error = kern_jail_set(td, auio, uap->flags); 508191673Sjamie free(auio, M_IOV); 509191673Sjamie return (error); 510191673Sjamie} 511191673Sjamie 512191673Sjamieint 513191673Sjamiekern_jail_set(struct thread *td, struct uio *optuio, int flags) 514191673Sjamie{ 515191673Sjamie struct nameidata nd; 516185435Sbz#ifdef INET 517190466Sjamie struct in_addr *ip4; 518185435Sbz#endif 519185435Sbz#ifdef INET6 520185435Sbz struct in6_addr *ip6; 521185435Sbz#endif 522191673Sjamie struct vfsopt *opt; 523191673Sjamie struct vfsoptlist *opts; 524196135Sbz struct prison *pr, *deadpr, *mypr, *ppr, *tpr; 525191673Sjamie struct vnode *root; 526196835Sjamie char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid; 527230407Smm char *g_path; 528192895Sjamie#if defined(INET) || defined(INET6) 529196135Sbz struct prison *tppr; 530191673Sjamie void *op; 531192895Sjamie#endif 532193066Sjamie unsigned long hid; 533192895Sjamie size_t namelen, onamelen; 534192895Sjamie int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos; 535231267Smm int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel; 536195870Sjamie int fi, jid, jsys, len, level; 537231267Smm int childmax, rsnum, slevel, vfslocked; 538230129Smm int fullpath_disabled; 539191673Sjamie#if defined(INET) || defined(INET6) 540192895Sjamie int ii, ij; 541191673Sjamie#endif 542191673Sjamie#ifdef INET 543195974Sjamie int ip4s, redo_ip4; 544191673Sjamie#endif 545191673Sjamie#ifdef INET6 546195974Sjamie int ip6s, redo_ip6; 547191673Sjamie#endif 548224290Smckusick uint64_t pr_allow, ch_allow, pr_flags, ch_flags; 549224290Smckusick unsigned tallow; 550191673Sjamie char numbuf[12]; 551185435Sbz 552191673Sjamie error = priv_check(td, PRIV_JAIL_SET); 553191673Sjamie if (!error && (flags & JAIL_ATTACH)) 554191673Sjamie error = priv_check(td, PRIV_JAIL_ATTACH); 555191673Sjamie if (error) 556191673Sjamie return (error); 557192895Sjamie mypr = ppr = td->td_ucred->cr_prison; 558194762Sjamie if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0) 559192895Sjamie return (EPERM); 560191673Sjamie if (flags & ~JAIL_SET_MASK) 561191673Sjamie return (EINVAL); 562191673Sjamie 563185435Sbz /* 564191673Sjamie * Check all the parameters before committing to anything. Not all 565191673Sjamie * errors can be caught early, but we may as well try. Also, this 566191673Sjamie * takes care of some expensive stuff (path lookup) before getting 567191673Sjamie * the allprison lock. 568185435Sbz * 569191673Sjamie * XXX Jails are not filesystems, and jail parameters are not mount 570191673Sjamie * options. But it makes more sense to re-use the vfsopt code 571191673Sjamie * than duplicate it under a different name. 572185435Sbz */ 573191673Sjamie error = vfs_buildopts(optuio, &opts); 574191673Sjamie if (error) 575191673Sjamie return (error); 576185435Sbz#ifdef INET 577185435Sbz ip4 = NULL; 578185435Sbz#endif 579185435Sbz#ifdef INET6 580185435Sbz ip6 = NULL; 581185435Sbz#endif 582230407Smm g_path = NULL; 583191673Sjamie 584191673Sjamie error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 585191673Sjamie if (error == ENOENT) 586191673Sjamie jid = 0; 587191673Sjamie else if (error != 0) 588191673Sjamie goto done_free; 589191673Sjamie 590191673Sjamie error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel)); 591191673Sjamie if (error == ENOENT) 592191673Sjamie gotslevel = 0; 593191673Sjamie else if (error != 0) 594191673Sjamie goto done_free; 595191673Sjamie else 596191673Sjamie gotslevel = 1; 597191673Sjamie 598194762Sjamie error = 599194762Sjamie vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax)); 600194762Sjamie if (error == ENOENT) 601194762Sjamie gotchildmax = 0; 602194762Sjamie else if (error != 0) 603194762Sjamie goto done_free; 604194762Sjamie else 605194762Sjamie gotchildmax = 1; 606194762Sjamie 607192895Sjamie error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce)); 608212436Sjamie if (error == ENOENT) 609212436Sjamie gotenforce = 0; 610212436Sjamie else if (error != 0) 611192895Sjamie goto done_free; 612212436Sjamie else if (enforce < 0 || enforce > 2) { 613212436Sjamie error = EINVAL; 614212436Sjamie goto done_free; 615212436Sjamie } else 616212436Sjamie gotenforce = 1; 617192895Sjamie 618231267Smm error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum)); 619231267Smm if (error == ENOENT) 620231267Smm gotrsnum = 0; 621231267Smm else if (error != 0) 622231267Smm goto done_free; 623231267Smm else 624231267Smm gotrsnum = 1; 625231267Smm 626191673Sjamie pr_flags = ch_flags = 0; 627192895Sjamie for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 628192895Sjamie fi++) { 629192895Sjamie if (pr_flag_names[fi] == NULL) 630192895Sjamie continue; 631192895Sjamie vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi); 632192895Sjamie vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi); 633192895Sjamie } 634191673Sjamie ch_flags |= pr_flags; 635195870Sjamie for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 636195870Sjamie fi++) { 637195870Sjamie error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys, 638195870Sjamie sizeof(jsys)); 639195870Sjamie if (error == ENOENT) 640195870Sjamie continue; 641195870Sjamie if (error != 0) 642195870Sjamie goto done_free; 643195870Sjamie switch (jsys) { 644195870Sjamie case JAIL_SYS_DISABLE: 645195870Sjamie if (!pr_flag_jailsys[fi].disable) { 646195870Sjamie error = EINVAL; 647195870Sjamie goto done_free; 648195870Sjamie } 649195870Sjamie pr_flags |= pr_flag_jailsys[fi].disable; 650195870Sjamie break; 651195870Sjamie case JAIL_SYS_NEW: 652195870Sjamie pr_flags |= pr_flag_jailsys[fi].new; 653195870Sjamie break; 654195870Sjamie case JAIL_SYS_INHERIT: 655195870Sjamie break; 656195870Sjamie default: 657195870Sjamie error = EINVAL; 658195870Sjamie goto done_free; 659195870Sjamie } 660195870Sjamie ch_flags |= 661195870Sjamie pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable; 662195870Sjamie } 663211085Sjamie if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE 664211085Sjamie && !(pr_flags & PR_PERSIST)) { 665211085Sjamie error = EINVAL; 666211085Sjamie vfs_opterror(opts, "new jail must persist or attach"); 667211085Sjamie goto done_errmsg; 668211085Sjamie } 669194251Sjamie#ifdef VIMAGE 670194251Sjamie if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) { 671194251Sjamie error = EINVAL; 672194251Sjamie vfs_opterror(opts, "vnet cannot be changed after creation"); 673194251Sjamie goto done_errmsg; 674194251Sjamie } 675194251Sjamie#endif 676195974Sjamie#ifdef INET 677195974Sjamie if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) { 678195974Sjamie error = EINVAL; 679195974Sjamie vfs_opterror(opts, "ip4 cannot be changed after creation"); 680195974Sjamie goto done_errmsg; 681195974Sjamie } 682195974Sjamie#endif 683195974Sjamie#ifdef INET6 684195974Sjamie if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) { 685195974Sjamie error = EINVAL; 686195974Sjamie vfs_opterror(opts, "ip6 cannot be changed after creation"); 687195974Sjamie goto done_errmsg; 688195974Sjamie } 689195974Sjamie#endif 690191673Sjamie 691192895Sjamie pr_allow = ch_allow = 0; 692192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 693192895Sjamie fi++) { 694192895Sjamie vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi); 695192895Sjamie vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi); 696192895Sjamie } 697192895Sjamie ch_allow |= pr_allow; 698192895Sjamie 699191673Sjamie error = vfs_getopt(opts, "name", (void **)&name, &len); 700191673Sjamie if (error == ENOENT) 701191673Sjamie name = NULL; 702191673Sjamie else if (error != 0) 703191673Sjamie goto done_free; 704191673Sjamie else { 705191673Sjamie if (len == 0 || name[len - 1] != '\0') { 706191673Sjamie error = EINVAL; 707191673Sjamie goto done_free; 708191673Sjamie } 709191673Sjamie if (len > MAXHOSTNAMELEN) { 710191673Sjamie error = ENAMETOOLONG; 711191673Sjamie goto done_free; 712191673Sjamie } 713191673Sjamie } 714191673Sjamie 715191673Sjamie error = vfs_getopt(opts, "host.hostname", (void **)&host, &len); 716191673Sjamie if (error == ENOENT) 717191673Sjamie host = NULL; 718191673Sjamie else if (error != 0) 719191673Sjamie goto done_free; 720191673Sjamie else { 721193066Sjamie ch_flags |= PR_HOST; 722193066Sjamie pr_flags |= PR_HOST; 723191673Sjamie if (len == 0 || host[len - 1] != '\0') { 724191673Sjamie error = EINVAL; 725191673Sjamie goto done_free; 726191673Sjamie } 727191673Sjamie if (len > MAXHOSTNAMELEN) { 728191673Sjamie error = ENAMETOOLONG; 729191673Sjamie goto done_free; 730191673Sjamie } 731191673Sjamie } 732191673Sjamie 733193066Sjamie error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len); 734193066Sjamie if (error == ENOENT) 735193066Sjamie domain = NULL; 736193066Sjamie else if (error != 0) 737193066Sjamie goto done_free; 738193066Sjamie else { 739193066Sjamie ch_flags |= PR_HOST; 740193066Sjamie pr_flags |= PR_HOST; 741193066Sjamie if (len == 0 || domain[len - 1] != '\0') { 742193066Sjamie error = EINVAL; 743193066Sjamie goto done_free; 744193066Sjamie } 745193066Sjamie if (len > MAXHOSTNAMELEN) { 746193066Sjamie error = ENAMETOOLONG; 747193066Sjamie goto done_free; 748193066Sjamie } 749193066Sjamie } 750193066Sjamie 751193066Sjamie error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len); 752193066Sjamie if (error == ENOENT) 753193066Sjamie uuid = NULL; 754193066Sjamie else if (error != 0) 755193066Sjamie goto done_free; 756193066Sjamie else { 757193066Sjamie ch_flags |= PR_HOST; 758193066Sjamie pr_flags |= PR_HOST; 759193066Sjamie if (len == 0 || uuid[len - 1] != '\0') { 760193066Sjamie error = EINVAL; 761193066Sjamie goto done_free; 762193066Sjamie } 763193066Sjamie if (len > HOSTUUIDLEN) { 764193066Sjamie error = ENAMETOOLONG; 765193066Sjamie goto done_free; 766193066Sjamie } 767193066Sjamie } 768193066Sjamie 769205014Snwhitehorn#ifdef COMPAT_FREEBSD32 770217896Sdchagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 771193066Sjamie uint32_t hid32; 772193066Sjamie 773193066Sjamie error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32)); 774193066Sjamie hid = hid32; 775193066Sjamie } else 776193066Sjamie#endif 777193066Sjamie error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid)); 778193066Sjamie if (error == ENOENT) 779193066Sjamie gothid = 0; 780193066Sjamie else if (error != 0) 781193066Sjamie goto done_free; 782193066Sjamie else { 783193066Sjamie gothid = 1; 784193066Sjamie ch_flags |= PR_HOST; 785193066Sjamie pr_flags |= PR_HOST; 786193066Sjamie } 787193066Sjamie 788185435Sbz#ifdef INET 789191673Sjamie error = vfs_getopt(opts, "ip4.addr", &op, &ip4s); 790191673Sjamie if (error == ENOENT) 791195870Sjamie ip4s = (pr_flags & PR_IP4_DISABLE) ? 0 : -1; 792191673Sjamie else if (error != 0) 793191673Sjamie goto done_free; 794191673Sjamie else if (ip4s & (sizeof(*ip4) - 1)) { 795191673Sjamie error = EINVAL; 796191673Sjamie goto done_free; 797192895Sjamie } else { 798195870Sjamie ch_flags |= PR_IP4_USER | PR_IP4_DISABLE; 799195870Sjamie if (ip4s == 0) 800195870Sjamie pr_flags |= PR_IP4_USER | PR_IP4_DISABLE; 801195870Sjamie else { 802195870Sjamie pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER; 803192895Sjamie ip4s /= sizeof(*ip4); 804192895Sjamie if (ip4s > jail_max_af_ips) { 805185435Sbz error = EINVAL; 806192895Sjamie vfs_opterror(opts, "too many IPv4 addresses"); 807192895Sjamie goto done_errmsg; 808185435Sbz } 809195974Sjamie ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 810192895Sjamie bcopy(op, ip4, ip4s * sizeof(*ip4)); 811192895Sjamie /* 812192895Sjamie * IP addresses are all sorted but ip[0] to preserve 813192895Sjamie * the primary IP address as given from userland. 814192895Sjamie * This special IP is used for unbound outgoing 815202116Sbz * connections as well for "loopback" traffic in case 816202116Sbz * source address selection cannot find any more fitting 817202116Sbz * address to connect from. 818192895Sjamie */ 819192895Sjamie if (ip4s > 1) 820192895Sjamie qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4); 821192895Sjamie /* 822192895Sjamie * Check for duplicate addresses and do some simple 823192895Sjamie * zero and broadcast checks. If users give other bogus 824192895Sjamie * addresses it is their problem. 825192895Sjamie * 826192895Sjamie * We do not have to care about byte order for these 827192895Sjamie * checks so we will do them in NBO. 828192895Sjamie */ 829192895Sjamie for (ii = 0; ii < ip4s; ii++) { 830192895Sjamie if (ip4[ii].s_addr == INADDR_ANY || 831192895Sjamie ip4[ii].s_addr == INADDR_BROADCAST) { 832192895Sjamie error = EINVAL; 833192895Sjamie goto done_free; 834192895Sjamie } 835192895Sjamie if ((ii+1) < ip4s && 836192895Sjamie (ip4[0].s_addr == ip4[ii+1].s_addr || 837192895Sjamie ip4[ii].s_addr == ip4[ii+1].s_addr)) { 838192895Sjamie error = EINVAL; 839192895Sjamie goto done_free; 840192895Sjamie } 841192895Sjamie } 842185435Sbz } 843191673Sjamie } 844191673Sjamie#endif 845185435Sbz 846185435Sbz#ifdef INET6 847191673Sjamie error = vfs_getopt(opts, "ip6.addr", &op, &ip6s); 848191673Sjamie if (error == ENOENT) 849195870Sjamie ip6s = (pr_flags & PR_IP6_DISABLE) ? 0 : -1; 850191673Sjamie else if (error != 0) 851191673Sjamie goto done_free; 852191673Sjamie else if (ip6s & (sizeof(*ip6) - 1)) { 853191673Sjamie error = EINVAL; 854191673Sjamie goto done_free; 855192895Sjamie } else { 856195870Sjamie ch_flags |= PR_IP6_USER | PR_IP6_DISABLE; 857195870Sjamie if (ip6s == 0) 858195870Sjamie pr_flags |= PR_IP6_USER | PR_IP6_DISABLE; 859195870Sjamie else { 860195870Sjamie pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER; 861192895Sjamie ip6s /= sizeof(*ip6); 862192895Sjamie if (ip6s > jail_max_af_ips) { 863185435Sbz error = EINVAL; 864192895Sjamie vfs_opterror(opts, "too many IPv6 addresses"); 865192895Sjamie goto done_errmsg; 866185435Sbz } 867195974Sjamie ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 868192895Sjamie bcopy(op, ip6, ip6s * sizeof(*ip6)); 869192895Sjamie if (ip6s > 1) 870192895Sjamie qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6); 871192895Sjamie for (ii = 0; ii < ip6s; ii++) { 872192895Sjamie if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) { 873192895Sjamie error = EINVAL; 874192895Sjamie goto done_free; 875192895Sjamie } 876192895Sjamie if ((ii+1) < ip6s && 877192895Sjamie (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) || 878192895Sjamie IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1]))) 879192895Sjamie { 880192895Sjamie error = EINVAL; 881192895Sjamie goto done_free; 882192895Sjamie } 883192895Sjamie } 884185435Sbz } 885191673Sjamie } 886185435Sbz#endif 887185435Sbz 888195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6)) 889195945Sjamie if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 890195945Sjamie error = EINVAL; 891195945Sjamie vfs_opterror(opts, 892195945Sjamie "vnet jails cannot have IP address restrictions"); 893195945Sjamie goto done_errmsg; 894195945Sjamie } 895195945Sjamie#endif 896195945Sjamie 897230143Smm fullpath_disabled = 0; 898191673Sjamie root = NULL; 899191673Sjamie error = vfs_getopt(opts, "path", (void **)&path, &len); 900191673Sjamie if (error == ENOENT) 901191673Sjamie path = NULL; 902191673Sjamie else if (error != 0) 903191673Sjamie goto done_free; 904191673Sjamie else { 905191673Sjamie if (flags & JAIL_UPDATE) { 906191673Sjamie error = EINVAL; 907191673Sjamie vfs_opterror(opts, 908191673Sjamie "path cannot be changed after creation"); 909191673Sjamie goto done_errmsg; 910191673Sjamie } 911191673Sjamie if (len == 0 || path[len - 1] != '\0') { 912191673Sjamie error = EINVAL; 913191673Sjamie goto done_free; 914191673Sjamie } 915230129Smm NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE, UIO_SYSSPACE, 916230129Smm path, td); 917230129Smm error = namei(&nd); 918230129Smm if (error) 919230129Smm goto done_free; 920230129Smm vfslocked = NDHASGIANT(&nd); 921230129Smm root = nd.ni_vp; 922230129Smm NDFREE(&nd, NDF_ONLY_PNBUF); 923230407Smm g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 924230407Smm strlcpy(g_path, path, MAXPATHLEN); 925230407Smm error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN); 926230407Smm if (error == 0) 927230407Smm path = g_path; 928230407Smm else if (error == ENODEV) { 929230129Smm /* proceed if sysctl debug.disablefullpath == 1 */ 930230129Smm fullpath_disabled = 1; 931230129Smm if (len < 2 || (len == 2 && path[0] == '/')) 932230129Smm path = NULL; 933230407Smm } else { 934230129Smm /* exit on other errors */ 935230129Smm VFS_UNLOCK_GIANT(vfslocked); 936230129Smm goto done_free; 937230129Smm } 938230129Smm if (root->v_type != VDIR) { 939230129Smm error = ENOTDIR; 940230129Smm vput(root); 941230129Smm VFS_UNLOCK_GIANT(vfslocked); 942230129Smm goto done_free; 943230129Smm } 944230129Smm VOP_UNLOCK(root, 0); 945230129Smm VFS_UNLOCK_GIANT(vfslocked); 946230129Smm if (fullpath_disabled) { 947192895Sjamie /* Leave room for a real-root full pathname. */ 948192895Sjamie if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/") 949192895Sjamie ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) { 950192895Sjamie error = ENAMETOOLONG; 951192895Sjamie goto done_free; 952192895Sjamie } 953191673Sjamie } 954191673Sjamie } 955185435Sbz 956191673Sjamie /* 957191673Sjamie * Grab the allprison lock before letting modules check their 958191673Sjamie * parameters. Once we have it, do not let go so we'll have a 959191673Sjamie * consistent view of the OSD list. 960191673Sjamie */ 961191673Sjamie sx_xlock(&allprison_lock); 962191673Sjamie error = osd_jail_call(NULL, PR_METHOD_CHECK, opts); 963191673Sjamie if (error) 964191673Sjamie goto done_unlock_list; 965185435Sbz 966191673Sjamie /* By now, all parameters should have been noted. */ 967191673Sjamie TAILQ_FOREACH(opt, opts, link) { 968191673Sjamie if (!opt->seen && strcmp(opt->name, "errmsg")) { 969191673Sjamie error = EINVAL; 970191673Sjamie vfs_opterror(opts, "unknown parameter: %s", opt->name); 971191673Sjamie goto done_unlock_list; 972191673Sjamie } 973191673Sjamie } 974191673Sjamie 975185435Sbz /* 976191673Sjamie * See if we are creating a new record or updating an existing one. 977191673Sjamie * This abuses the file error codes ENOENT and EEXIST. 978185435Sbz */ 979191673Sjamie cuflags = flags & (JAIL_CREATE | JAIL_UPDATE); 980191673Sjamie if (!cuflags) { 981191673Sjamie error = EINVAL; 982191673Sjamie vfs_opterror(opts, "no valid operation (create or update)"); 983191673Sjamie goto done_unlock_list; 984191673Sjamie } 985191673Sjamie pr = NULL; 986196835Sjamie namelc = NULL; 987196835Sjamie if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) { 988196835Sjamie namelc = strrchr(name, '.'); 989196835Sjamie jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10); 990196835Sjamie if (*p != '\0') 991196835Sjamie jid = 0; 992196835Sjamie } 993191673Sjamie if (jid != 0) { 994192895Sjamie /* 995192895Sjamie * See if a requested jid already exists. There is an 996192895Sjamie * information leak here if the jid exists but is not within 997192895Sjamie * the caller's jail hierarchy. Jail creators will get EEXIST 998192895Sjamie * even though they cannot see the jail, and CREATE | UPDATE 999192895Sjamie * will return ENOENT which is not normally a valid error. 1000192895Sjamie */ 1001191673Sjamie if (jid < 0) { 1002191673Sjamie error = EINVAL; 1003191673Sjamie vfs_opterror(opts, "negative jid"); 1004191673Sjamie goto done_unlock_list; 1005191673Sjamie } 1006191673Sjamie pr = prison_find(jid); 1007191673Sjamie if (pr != NULL) { 1008192895Sjamie ppr = pr->pr_parent; 1009191673Sjamie /* Create: jid must not exist. */ 1010191673Sjamie if (cuflags == JAIL_CREATE) { 1011191673Sjamie mtx_unlock(&pr->pr_mtx); 1012191673Sjamie error = EEXIST; 1013191673Sjamie vfs_opterror(opts, "jail %d already exists", 1014191673Sjamie jid); 1015191673Sjamie goto done_unlock_list; 1016191673Sjamie } 1017192895Sjamie if (!prison_ischild(mypr, pr)) { 1018192895Sjamie mtx_unlock(&pr->pr_mtx); 1019192895Sjamie pr = NULL; 1020192895Sjamie } else if (pr->pr_uref == 0) { 1021191673Sjamie if (!(flags & JAIL_DYING)) { 1022191673Sjamie mtx_unlock(&pr->pr_mtx); 1023191673Sjamie error = ENOENT; 1024191673Sjamie vfs_opterror(opts, "jail %d is dying", 1025191673Sjamie jid); 1026191673Sjamie goto done_unlock_list; 1027191673Sjamie } else if ((flags & JAIL_ATTACH) || 1028191673Sjamie (pr_flags & PR_PERSIST)) { 1029191673Sjamie /* 1030191673Sjamie * A dying jail might be resurrected 1031191673Sjamie * (via attach or persist), but first 1032191673Sjamie * it must determine if another jail 1033191673Sjamie * has claimed its name. Accomplish 1034191673Sjamie * this by implicitly re-setting the 1035191673Sjamie * name. 1036191673Sjamie */ 1037191673Sjamie if (name == NULL) 1038192895Sjamie name = prison_name(mypr, pr); 1039191673Sjamie } 1040191673Sjamie } 1041191673Sjamie } 1042191673Sjamie if (pr == NULL) { 1043191673Sjamie /* Update: jid must exist. */ 1044191673Sjamie if (cuflags == JAIL_UPDATE) { 1045191673Sjamie error = ENOENT; 1046191673Sjamie vfs_opterror(opts, "jail %d not found", jid); 1047191673Sjamie goto done_unlock_list; 1048191673Sjamie } 1049191673Sjamie } 1050191673Sjamie } 1051191673Sjamie /* 1052191673Sjamie * If the caller provided a name, look for a jail by that name. 1053191673Sjamie * This has different semantics for creates and updates keyed by jid 1054191673Sjamie * (where the name must not already exist in a different jail), 1055191673Sjamie * and updates keyed by the name itself (where the name must exist 1056191673Sjamie * because that is the jail being updated). 1057191673Sjamie */ 1058191673Sjamie if (name != NULL) { 1059196835Sjamie namelc = strrchr(name, '.'); 1060196835Sjamie if (namelc == NULL) 1061196835Sjamie namelc = name; 1062196835Sjamie else { 1063192895Sjamie /* 1064192895Sjamie * This is a hierarchical name. Split it into the 1065192895Sjamie * parent and child names, and make sure the parent 1066192895Sjamie * exists or matches an already found jail. 1067192895Sjamie */ 1068196835Sjamie *namelc = '\0'; 1069192895Sjamie if (pr != NULL) { 1070196835Sjamie if (strncmp(name, ppr->pr_name, namelc - name) 1071196835Sjamie || ppr->pr_name[namelc - name] != '\0') { 1072192895Sjamie mtx_unlock(&pr->pr_mtx); 1073192895Sjamie error = EINVAL; 1074192895Sjamie vfs_opterror(opts, 1075192895Sjamie "cannot change jail's parent"); 1076192895Sjamie goto done_unlock_list; 1077192895Sjamie } 1078192895Sjamie } else { 1079192895Sjamie ppr = prison_find_name(mypr, name); 1080192895Sjamie if (ppr == NULL) { 1081192895Sjamie error = ENOENT; 1082192895Sjamie vfs_opterror(opts, 1083192895Sjamie "jail \"%s\" not found", name); 1084192895Sjamie goto done_unlock_list; 1085192895Sjamie } 1086192895Sjamie mtx_unlock(&ppr->pr_mtx); 1087192895Sjamie } 1088196835Sjamie name = ++namelc; 1089192895Sjamie } 1090191673Sjamie if (name[0] != '\0') { 1091192895Sjamie namelen = 1092192895Sjamie (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 1093192895Sjamie name_again: 1094191673Sjamie deadpr = NULL; 1095192895Sjamie FOREACH_PRISON_CHILD(ppr, tpr) { 1096191673Sjamie if (tpr != pr && tpr->pr_ref > 0 && 1097192895Sjamie !strcmp(tpr->pr_name + namelen, name)) { 1098191673Sjamie if (pr == NULL && 1099191673Sjamie cuflags != JAIL_CREATE) { 1100191673Sjamie mtx_lock(&tpr->pr_mtx); 1101191673Sjamie if (tpr->pr_ref > 0) { 1102191673Sjamie /* 1103191673Sjamie * Use this jail 1104191673Sjamie * for updates. 1105191673Sjamie */ 1106191673Sjamie if (tpr->pr_uref > 0) { 1107191673Sjamie pr = tpr; 1108191673Sjamie break; 1109191673Sjamie } 1110191673Sjamie deadpr = tpr; 1111191673Sjamie } 1112191673Sjamie mtx_unlock(&tpr->pr_mtx); 1113191673Sjamie } else if (tpr->pr_uref > 0) { 1114191673Sjamie /* 1115191673Sjamie * Create, or update(jid): 1116191673Sjamie * name must not exist in an 1117192895Sjamie * active sibling jail. 1118191673Sjamie */ 1119191673Sjamie error = EEXIST; 1120191673Sjamie if (pr != NULL) 1121191673Sjamie mtx_unlock(&pr->pr_mtx); 1122191673Sjamie vfs_opterror(opts, 1123191673Sjamie "jail \"%s\" already exists", 1124191673Sjamie name); 1125191673Sjamie goto done_unlock_list; 1126191673Sjamie } 1127191673Sjamie } 1128191673Sjamie } 1129191673Sjamie /* If no active jail is found, use a dying one. */ 1130191673Sjamie if (deadpr != NULL && pr == NULL) { 1131191673Sjamie if (flags & JAIL_DYING) { 1132191673Sjamie mtx_lock(&deadpr->pr_mtx); 1133191673Sjamie if (deadpr->pr_ref == 0) { 1134191673Sjamie mtx_unlock(&deadpr->pr_mtx); 1135191673Sjamie goto name_again; 1136191673Sjamie } 1137191673Sjamie pr = deadpr; 1138191673Sjamie } else if (cuflags == JAIL_UPDATE) { 1139191673Sjamie error = ENOENT; 1140191673Sjamie vfs_opterror(opts, 1141191673Sjamie "jail \"%s\" is dying", name); 1142191673Sjamie goto done_unlock_list; 1143191673Sjamie } 1144191673Sjamie } 1145191673Sjamie /* Update: name must exist if no jid. */ 1146191673Sjamie else if (cuflags == JAIL_UPDATE && pr == NULL) { 1147191673Sjamie error = ENOENT; 1148191673Sjamie vfs_opterror(opts, "jail \"%s\" not found", 1149191673Sjamie name); 1150191673Sjamie goto done_unlock_list; 1151191673Sjamie } 1152191673Sjamie } 1153191673Sjamie } 1154191673Sjamie /* Update: must provide a jid or name. */ 1155191673Sjamie else if (cuflags == JAIL_UPDATE && pr == NULL) { 1156191673Sjamie error = ENOENT; 1157191673Sjamie vfs_opterror(opts, "update specified no jail"); 1158191673Sjamie goto done_unlock_list; 1159191673Sjamie } 1160185435Sbz 1161191673Sjamie /* If there's no prison to update, create a new one and link it in. */ 1162191673Sjamie if (pr == NULL) { 1163194762Sjamie for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent) 1164194762Sjamie if (tpr->pr_childcount >= tpr->pr_childmax) { 1165194762Sjamie error = EPERM; 1166194762Sjamie vfs_opterror(opts, "prison limit exceeded"); 1167194762Sjamie goto done_unlock_list; 1168194762Sjamie } 1169191673Sjamie created = 1; 1170192895Sjamie mtx_lock(&ppr->pr_mtx); 1171192895Sjamie if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) { 1172192895Sjamie mtx_unlock(&ppr->pr_mtx); 1173192895Sjamie error = ENOENT; 1174192895Sjamie vfs_opterror(opts, "parent jail went away!"); 1175192895Sjamie goto done_unlock_list; 1176192895Sjamie } 1177192895Sjamie ppr->pr_ref++; 1178192895Sjamie ppr->pr_uref++; 1179192895Sjamie mtx_unlock(&ppr->pr_mtx); 1180191673Sjamie pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO); 1181191673Sjamie if (jid == 0) { 1182191673Sjamie /* Find the next free jid. */ 1183191673Sjamie jid = lastprid + 1; 1184191673Sjamie findnext: 1185191673Sjamie if (jid == JAIL_MAX) 1186191673Sjamie jid = 1; 1187191673Sjamie TAILQ_FOREACH(tpr, &allprison, pr_list) { 1188191673Sjamie if (tpr->pr_id < jid) 1189191673Sjamie continue; 1190191673Sjamie if (tpr->pr_id > jid || tpr->pr_ref == 0) { 1191191673Sjamie TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1192191673Sjamie break; 1193191673Sjamie } 1194191673Sjamie if (jid == lastprid) { 1195191673Sjamie error = EAGAIN; 1196191673Sjamie vfs_opterror(opts, 1197191673Sjamie "no available jail IDs"); 1198191673Sjamie free(pr, M_PRISON); 1199192895Sjamie prison_deref(ppr, PD_DEREF | 1200192895Sjamie PD_DEUREF | PD_LIST_XLOCKED); 1201192895Sjamie goto done_releroot; 1202191673Sjamie } 1203191673Sjamie jid++; 1204191673Sjamie goto findnext; 1205191673Sjamie } 1206191673Sjamie lastprid = jid; 1207191673Sjamie } else { 1208191673Sjamie /* 1209191673Sjamie * The jail already has a jid (that did not yet exist), 1210191673Sjamie * so just find where to insert it. 1211191673Sjamie */ 1212191673Sjamie TAILQ_FOREACH(tpr, &allprison, pr_list) 1213191673Sjamie if (tpr->pr_id >= jid) { 1214191673Sjamie TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1215191673Sjamie break; 1216191673Sjamie } 1217191673Sjamie } 1218191673Sjamie if (tpr == NULL) 1219191673Sjamie TAILQ_INSERT_TAIL(&allprison, pr, pr_list); 1220192895Sjamie LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling); 1221192895Sjamie for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 1222194762Sjamie tpr->pr_childcount++; 1223185435Sbz 1224192895Sjamie pr->pr_parent = ppr; 1225191673Sjamie pr->pr_id = jid; 1226192895Sjamie 1227192895Sjamie /* Set some default values, and inherit some from the parent. */ 1228191673Sjamie if (name == NULL) 1229191673Sjamie name = ""; 1230191673Sjamie if (path == NULL) { 1231191673Sjamie path = "/"; 1232192895Sjamie root = mypr->pr_root; 1233191673Sjamie vref(root); 1234191673Sjamie } 1235195944Sjamie strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN); 1236195944Sjamie pr->pr_flags |= PR_HOST; 1237195945Sjamie#if defined(INET) || defined(INET6) 1238195945Sjamie#ifdef VIMAGE 1239195945Sjamie if (!(pr_flags & PR_VNET)) 1240195945Sjamie#endif 1241195945Sjamie { 1242192895Sjamie#ifdef INET 1243195974Sjamie if (!(ch_flags & PR_IP4_USER)) 1244195974Sjamie pr->pr_flags |= 1245195974Sjamie PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE; 1246195974Sjamie else if (!(pr_flags & PR_IP4_USER)) { 1247195974Sjamie pr->pr_flags |= ppr->pr_flags & PR_IP4; 1248195974Sjamie if (ppr->pr_ip4 != NULL) { 1249195974Sjamie pr->pr_ip4s = ppr->pr_ip4s; 1250195974Sjamie pr->pr_ip4 = malloc(pr->pr_ip4s * 1251195974Sjamie sizeof(struct in_addr), M_PRISON, 1252195974Sjamie M_WAITOK); 1253195974Sjamie bcopy(ppr->pr_ip4, pr->pr_ip4, 1254195974Sjamie pr->pr_ip4s * sizeof(*pr->pr_ip4)); 1255195974Sjamie } 1256195974Sjamie } 1257192895Sjamie#endif 1258192895Sjamie#ifdef INET6 1259195974Sjamie if (!(ch_flags & PR_IP6_USER)) 1260195974Sjamie pr->pr_flags |= 1261195974Sjamie PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE; 1262195974Sjamie else if (!(pr_flags & PR_IP6_USER)) { 1263195974Sjamie pr->pr_flags |= ppr->pr_flags & PR_IP6; 1264195974Sjamie if (ppr->pr_ip6 != NULL) { 1265195974Sjamie pr->pr_ip6s = ppr->pr_ip6s; 1266195974Sjamie pr->pr_ip6 = malloc(pr->pr_ip6s * 1267195974Sjamie sizeof(struct in6_addr), M_PRISON, 1268195974Sjamie M_WAITOK); 1269195974Sjamie bcopy(ppr->pr_ip6, pr->pr_ip6, 1270195974Sjamie pr->pr_ip6s * sizeof(*pr->pr_ip6)); 1271195974Sjamie } 1272195974Sjamie } 1273192895Sjamie#endif 1274195945Sjamie } 1275195945Sjamie#endif 1276202468Sbz /* Source address selection is always on by default. */ 1277202468Sbz pr->pr_flags |= _PR_IP_SADDRSEL; 1278202468Sbz 1279192895Sjamie pr->pr_securelevel = ppr->pr_securelevel; 1280192895Sjamie pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow; 1281196002Sjamie pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 1282231267Smm pr->pr_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM; 1283191673Sjamie 1284192895Sjamie LIST_INIT(&pr->pr_children); 1285192895Sjamie mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK); 1286191673Sjamie 1287194251Sjamie#ifdef VIMAGE 1288194251Sjamie /* Allocate a new vnet if specified. */ 1289194251Sjamie pr->pr_vnet = (pr_flags & PR_VNET) 1290194251Sjamie ? vnet_alloc() : ppr->pr_vnet; 1291194251Sjamie#endif 1292185435Sbz /* 1293191673Sjamie * Allocate a dedicated cpuset for each jail. 1294191673Sjamie * Unlike other initial settings, this may return an erorr. 1295185435Sbz */ 1296192895Sjamie error = cpuset_create_root(ppr, &pr->pr_cpuset); 1297191673Sjamie if (error) { 1298191673Sjamie prison_deref(pr, PD_LIST_XLOCKED); 1299191673Sjamie goto done_releroot; 1300191673Sjamie } 1301185435Sbz 1302191673Sjamie mtx_lock(&pr->pr_mtx); 1303185435Sbz /* 1304191673Sjamie * New prisons do not yet have a reference, because we do not 1305191673Sjamie * want other to see the incomplete prison once the 1306191673Sjamie * allprison_lock is downgraded. 1307185435Sbz */ 1308191673Sjamie } else { 1309191673Sjamie created = 0; 1310195974Sjamie /* 1311195974Sjamie * Grab a reference for existing prisons, to ensure they 1312195974Sjamie * continue to exist for the duration of the call. 1313195974Sjamie */ 1314195974Sjamie pr->pr_ref++; 1315195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6)) 1316195945Sjamie if ((pr->pr_flags & PR_VNET) && 1317195945Sjamie (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 1318195945Sjamie error = EINVAL; 1319195945Sjamie vfs_opterror(opts, 1320195945Sjamie "vnet jails cannot have IP address restrictions"); 1321195945Sjamie goto done_deref_locked; 1322195945Sjamie } 1323195945Sjamie#endif 1324195974Sjamie#ifdef INET 1325195974Sjamie if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 1326195974Sjamie error = EINVAL; 1327195974Sjamie vfs_opterror(opts, 1328195974Sjamie "ip4 cannot be changed after creation"); 1329195974Sjamie goto done_deref_locked; 1330195974Sjamie } 1331195974Sjamie#endif 1332195974Sjamie#ifdef INET6 1333195974Sjamie if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 1334195974Sjamie error = EINVAL; 1335195974Sjamie vfs_opterror(opts, 1336195974Sjamie "ip6 cannot be changed after creation"); 1337195974Sjamie goto done_deref_locked; 1338195974Sjamie } 1339195974Sjamie#endif 1340191673Sjamie } 1341185435Sbz 1342191673Sjamie /* Do final error checking before setting anything. */ 1343192895Sjamie if (gotslevel) { 1344192895Sjamie if (slevel < ppr->pr_securelevel) { 1345192895Sjamie error = EPERM; 1346192895Sjamie goto done_deref_locked; 1347192895Sjamie } 1348192895Sjamie } 1349194762Sjamie if (gotchildmax) { 1350194762Sjamie if (childmax >= ppr->pr_childmax) { 1351194762Sjamie error = EPERM; 1352194762Sjamie goto done_deref_locked; 1353194762Sjamie } 1354194762Sjamie } 1355192895Sjamie if (gotenforce) { 1356192895Sjamie if (enforce < ppr->pr_enforce_statfs) { 1357192895Sjamie error = EPERM; 1358192895Sjamie goto done_deref_locked; 1359192895Sjamie } 1360192895Sjamie } 1361231267Smm if (gotrsnum) { 1362231267Smm /* 1363231267Smm * devfs_rsnum is a uint16_t 1364231267Smm * value of -1 disables devfs mounts 1365231267Smm */ 1366231267Smm if (rsnum < -1 || rsnum > 65535) { 1367231267Smm error = EINVAL; 1368231267Smm goto done_deref_locked; 1369231267Smm } 1370231267Smm /* 1371231267Smm * Nested jails may inherit parent's devfs ruleset 1372231267Smm * or disable devfs 1373231267Smm */ 1374231267Smm if (jailed(td->td_ucred)) { 1375231267Smm if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) { 1376231267Smm error = EPERM; 1377231267Smm goto done_deref_locked; 1378231267Smm } else if (rsnum == 0) 1379231267Smm rsnum = ppr->pr_devfs_rsnum; 1380231267Smm } 1381231267Smm } 1382185435Sbz#ifdef INET 1383195974Sjamie if (ip4s > 0) { 1384192895Sjamie if (ppr->pr_flags & PR_IP4) { 1385195974Sjamie /* 1386195974Sjamie * Make sure the new set of IP addresses is a 1387195974Sjamie * subset of the parent's list. Don't worry 1388195974Sjamie * about the parent being unlocked, as any 1389195974Sjamie * setting is done with allprison_lock held. 1390195974Sjamie */ 1391195974Sjamie for (ij = 0; ij < ppr->pr_ip4s; ij++) 1392195974Sjamie if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 1393195974Sjamie break; 1394195974Sjamie if (ij == ppr->pr_ip4s) { 1395195974Sjamie error = EPERM; 1396195974Sjamie goto done_deref_locked; 1397195974Sjamie } 1398195974Sjamie if (ip4s > 1) { 1399195974Sjamie for (ii = ij = 1; ii < ip4s; ii++) { 1400195974Sjamie if (ip4[ii].s_addr == 1401195974Sjamie ppr->pr_ip4[0].s_addr) 1402195974Sjamie continue; 1403195974Sjamie for (; ij < ppr->pr_ip4s; ij++) 1404195974Sjamie if (ip4[ii].s_addr == 1405195974Sjamie ppr->pr_ip4[ij].s_addr) 1406195974Sjamie break; 1407195974Sjamie if (ij == ppr->pr_ip4s) 1408195974Sjamie break; 1409192895Sjamie } 1410192895Sjamie if (ij == ppr->pr_ip4s) { 1411192895Sjamie error = EPERM; 1412192895Sjamie goto done_deref_locked; 1413192895Sjamie } 1414192895Sjamie } 1415192895Sjamie } 1416195974Sjamie /* 1417195974Sjamie * Check for conflicting IP addresses. We permit them 1418195974Sjamie * if there is no more than one IP on each jail. If 1419195974Sjamie * there is a duplicate on a jail with more than one 1420195974Sjamie * IP stop checking and return error. 1421195974Sjamie */ 1422195974Sjamie tppr = ppr; 1423195945Sjamie#ifdef VIMAGE 1424195974Sjamie for (; tppr != &prison0; tppr = tppr->pr_parent) 1425195974Sjamie if (tppr->pr_flags & PR_VNET) 1426195974Sjamie break; 1427195945Sjamie#endif 1428195974Sjamie FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1429195974Sjamie if (tpr == pr || 1430195945Sjamie#ifdef VIMAGE 1431195974Sjamie (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1432195945Sjamie#endif 1433195974Sjamie tpr->pr_uref == 0) { 1434192895Sjamie descend = 0; 1435195974Sjamie continue; 1436195974Sjamie } 1437195974Sjamie if (!(tpr->pr_flags & PR_IP4_USER)) 1438195974Sjamie continue; 1439195974Sjamie descend = 0; 1440195974Sjamie if (tpr->pr_ip4 == NULL || 1441195974Sjamie (ip4s == 1 && tpr->pr_ip4s == 1)) 1442195974Sjamie continue; 1443195974Sjamie for (ii = 0; ii < ip4s; ii++) { 1444195974Sjamie if (_prison_check_ip4(tpr, &ip4[ii]) == 0) { 1445195974Sjamie error = EADDRINUSE; 1446195974Sjamie vfs_opterror(opts, 1447195974Sjamie "IPv4 addresses clash"); 1448195974Sjamie goto done_deref_locked; 1449192895Sjamie } 1450192895Sjamie } 1451192895Sjamie } 1452192895Sjamie } 1453185435Sbz#endif 1454191673Sjamie#ifdef INET6 1455195974Sjamie if (ip6s > 0) { 1456192895Sjamie if (ppr->pr_flags & PR_IP6) { 1457195974Sjamie /* 1458195974Sjamie * Make sure the new set of IP addresses is a 1459195974Sjamie * subset of the parent's list. 1460195974Sjamie */ 1461195974Sjamie for (ij = 0; ij < ppr->pr_ip6s; ij++) 1462195974Sjamie if (IN6_ARE_ADDR_EQUAL(&ip6[0], 1463195974Sjamie &ppr->pr_ip6[ij])) 1464195974Sjamie break; 1465195974Sjamie if (ij == ppr->pr_ip6s) { 1466195974Sjamie error = EPERM; 1467195974Sjamie goto done_deref_locked; 1468195974Sjamie } 1469195974Sjamie if (ip6s > 1) { 1470195974Sjamie for (ii = ij = 1; ii < ip6s; ii++) { 1471195974Sjamie if (IN6_ARE_ADDR_EQUAL(&ip6[ii], 1472195974Sjamie &ppr->pr_ip6[0])) 1473195974Sjamie continue; 1474195974Sjamie for (; ij < ppr->pr_ip6s; ij++) 1475195974Sjamie if (IN6_ARE_ADDR_EQUAL( 1476195974Sjamie &ip6[ii], &ppr->pr_ip6[ij])) 1477195974Sjamie break; 1478195974Sjamie if (ij == ppr->pr_ip6s) 1479195974Sjamie break; 1480192895Sjamie } 1481192895Sjamie if (ij == ppr->pr_ip6s) { 1482192895Sjamie error = EPERM; 1483192895Sjamie goto done_deref_locked; 1484192895Sjamie } 1485192895Sjamie } 1486192895Sjamie } 1487195974Sjamie /* Check for conflicting IP addresses. */ 1488195974Sjamie tppr = ppr; 1489195945Sjamie#ifdef VIMAGE 1490195974Sjamie for (; tppr != &prison0; tppr = tppr->pr_parent) 1491195974Sjamie if (tppr->pr_flags & PR_VNET) 1492195974Sjamie break; 1493195945Sjamie#endif 1494195974Sjamie FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1495195974Sjamie if (tpr == pr || 1496195945Sjamie#ifdef VIMAGE 1497195974Sjamie (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1498195945Sjamie#endif 1499195974Sjamie tpr->pr_uref == 0) { 1500192895Sjamie descend = 0; 1501195974Sjamie continue; 1502195974Sjamie } 1503195974Sjamie if (!(tpr->pr_flags & PR_IP6_USER)) 1504195974Sjamie continue; 1505195974Sjamie descend = 0; 1506195974Sjamie if (tpr->pr_ip6 == NULL || 1507195974Sjamie (ip6s == 1 && tpr->pr_ip6s == 1)) 1508195974Sjamie continue; 1509195974Sjamie for (ii = 0; ii < ip6s; ii++) { 1510195974Sjamie if (_prison_check_ip6(tpr, &ip6[ii]) == 0) { 1511195974Sjamie error = EADDRINUSE; 1512195974Sjamie vfs_opterror(opts, 1513195974Sjamie "IPv6 addresses clash"); 1514195974Sjamie goto done_deref_locked; 1515192895Sjamie } 1516192895Sjamie } 1517191673Sjamie } 1518192895Sjamie } 1519191673Sjamie#endif 1520192895Sjamie onamelen = namelen = 0; 1521192895Sjamie if (name != NULL) { 1522191673Sjamie /* Give a default name of the jid. */ 1523191673Sjamie if (name[0] == '\0') 1524191673Sjamie snprintf(name = numbuf, sizeof(numbuf), "%d", jid); 1525196835Sjamie else if (*namelc == '0' || (strtoul(namelc, &p, 10) != jid && 1526196835Sjamie *p == '\0')) { 1527191673Sjamie error = EINVAL; 1528196835Sjamie vfs_opterror(opts, 1529196835Sjamie "name cannot be numeric (unless it is the jid)"); 1530192895Sjamie goto done_deref_locked; 1531191673Sjamie } 1532191673Sjamie /* 1533192895Sjamie * Make sure the name isn't too long for the prison or its 1534192895Sjamie * children. 1535191673Sjamie */ 1536192895Sjamie onamelen = strlen(pr->pr_name); 1537192895Sjamie namelen = strlen(name); 1538192895Sjamie if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) { 1539192895Sjamie error = ENAMETOOLONG; 1540192895Sjamie goto done_deref_locked; 1541192895Sjamie } 1542192895Sjamie FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 1543192895Sjamie if (strlen(tpr->pr_name) + (namelen - onamelen) >= 1544192895Sjamie sizeof(pr->pr_name)) { 1545192895Sjamie error = ENAMETOOLONG; 1546192895Sjamie goto done_deref_locked; 1547192895Sjamie } 1548192895Sjamie } 1549191673Sjamie } 1550192895Sjamie if (pr_allow & ~ppr->pr_allow) { 1551192895Sjamie error = EPERM; 1552192895Sjamie goto done_deref_locked; 1553192895Sjamie } 1554185435Sbz 1555191673Sjamie /* Set the parameters of the prison. */ 1556191673Sjamie#ifdef INET 1557192895Sjamie redo_ip4 = 0; 1558195974Sjamie if (pr_flags & PR_IP4_USER) { 1559195974Sjamie pr->pr_flags |= PR_IP4; 1560195974Sjamie free(pr->pr_ip4, M_PRISON); 1561195974Sjamie pr->pr_ip4s = ip4s; 1562195974Sjamie pr->pr_ip4 = ip4; 1563195974Sjamie ip4 = NULL; 1564192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1565195945Sjamie#ifdef VIMAGE 1566195945Sjamie if (tpr->pr_flags & PR_VNET) { 1567195945Sjamie descend = 0; 1568195945Sjamie continue; 1569195945Sjamie } 1570195945Sjamie#endif 1571192895Sjamie if (prison_restrict_ip4(tpr, NULL)) { 1572192895Sjamie redo_ip4 = 1; 1573192895Sjamie descend = 0; 1574192895Sjamie } 1575192895Sjamie } 1576185435Sbz } 1577191673Sjamie#endif 1578191673Sjamie#ifdef INET6 1579192895Sjamie redo_ip6 = 0; 1580195974Sjamie if (pr_flags & PR_IP6_USER) { 1581195974Sjamie pr->pr_flags |= PR_IP6; 1582195974Sjamie free(pr->pr_ip6, M_PRISON); 1583195974Sjamie pr->pr_ip6s = ip6s; 1584195974Sjamie pr->pr_ip6 = ip6; 1585195974Sjamie ip6 = NULL; 1586192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1587195945Sjamie#ifdef VIMAGE 1588195945Sjamie if (tpr->pr_flags & PR_VNET) { 1589195945Sjamie descend = 0; 1590195945Sjamie continue; 1591195945Sjamie } 1592195945Sjamie#endif 1593192895Sjamie if (prison_restrict_ip6(tpr, NULL)) { 1594192895Sjamie redo_ip6 = 1; 1595192895Sjamie descend = 0; 1596192895Sjamie } 1597192895Sjamie } 1598191673Sjamie } 1599191673Sjamie#endif 1600192895Sjamie if (gotslevel) { 1601191673Sjamie pr->pr_securelevel = slevel; 1602192895Sjamie /* Set all child jails to be at least this level. */ 1603192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1604192895Sjamie if (tpr->pr_securelevel < slevel) 1605192895Sjamie tpr->pr_securelevel = slevel; 1606192895Sjamie } 1607194762Sjamie if (gotchildmax) { 1608194762Sjamie pr->pr_childmax = childmax; 1609194762Sjamie /* Set all child jails to under this limit. */ 1610194762Sjamie FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level) 1611194762Sjamie if (tpr->pr_childmax > childmax - level) 1612194762Sjamie tpr->pr_childmax = childmax > level 1613194762Sjamie ? childmax - level : 0; 1614194762Sjamie } 1615192895Sjamie if (gotenforce) { 1616192895Sjamie pr->pr_enforce_statfs = enforce; 1617192895Sjamie /* Pass this restriction on to the children. */ 1618192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1619192895Sjamie if (tpr->pr_enforce_statfs < enforce) 1620192895Sjamie tpr->pr_enforce_statfs = enforce; 1621192895Sjamie } 1622231267Smm if (gotrsnum) { 1623231267Smm pr->pr_devfs_rsnum = rsnum; 1624231267Smm /* Pass this restriction on to the children. */ 1625231267Smm FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1626231267Smm if (tpr->pr_devfs_rsnum != -1) 1627231267Smm tpr->pr_devfs_rsnum = rsnum; 1628231267Smm } 1629192895Sjamie if (name != NULL) { 1630192895Sjamie if (ppr == &prison0) 1631192895Sjamie strlcpy(pr->pr_name, name, sizeof(pr->pr_name)); 1632192895Sjamie else 1633192895Sjamie snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s", 1634192895Sjamie ppr->pr_name, name); 1635192895Sjamie /* Change this component of child names. */ 1636192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1637192895Sjamie bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen, 1638192895Sjamie strlen(tpr->pr_name + onamelen) + 1); 1639192895Sjamie bcopy(pr->pr_name, tpr->pr_name, namelen); 1640192895Sjamie } 1641192895Sjamie } 1642191673Sjamie if (path != NULL) { 1643192895Sjamie /* Try to keep a real-rooted full pathname. */ 1644230129Smm if (fullpath_disabled && path[0] == '/' && 1645230129Smm strcmp(mypr->pr_path, "/")) 1646192895Sjamie snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s", 1647192895Sjamie mypr->pr_path, path); 1648192895Sjamie else 1649192895Sjamie strlcpy(pr->pr_path, path, sizeof(pr->pr_path)); 1650191673Sjamie pr->pr_root = root; 1651191673Sjamie } 1652193066Sjamie if (PR_HOST & ch_flags & ~pr_flags) { 1653193066Sjamie if (pr->pr_flags & PR_HOST) { 1654193066Sjamie /* 1655193066Sjamie * Copy the parent's host info. As with pr_ip4 above, 1656193066Sjamie * the lack of a lock on the parent is not a problem; 1657193066Sjamie * it is always set with allprison_lock at least 1658193066Sjamie * shared, and is held exclusively here. 1659193066Sjamie */ 1660194118Sjamie strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname, 1661194118Sjamie sizeof(pr->pr_hostname)); 1662194118Sjamie strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname, 1663194118Sjamie sizeof(pr->pr_domainname)); 1664194118Sjamie strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid, 1665194118Sjamie sizeof(pr->pr_hostuuid)); 1666193066Sjamie pr->pr_hostid = pr->pr_parent->pr_hostid; 1667193066Sjamie } 1668193066Sjamie } else if (host != NULL || domain != NULL || uuid != NULL || gothid) { 1669193066Sjamie /* Set this prison, and any descendants without PR_HOST. */ 1670193066Sjamie if (host != NULL) 1671194118Sjamie strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname)); 1672193066Sjamie if (domain != NULL) 1673194118Sjamie strlcpy(pr->pr_domainname, domain, 1674194118Sjamie sizeof(pr->pr_domainname)); 1675193066Sjamie if (uuid != NULL) 1676194118Sjamie strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid)); 1677193066Sjamie if (gothid) 1678193066Sjamie pr->pr_hostid = hid; 1679193066Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1680193066Sjamie if (tpr->pr_flags & PR_HOST) 1681193066Sjamie descend = 0; 1682193066Sjamie else { 1683193066Sjamie if (host != NULL) 1684194118Sjamie strlcpy(tpr->pr_hostname, 1685194118Sjamie pr->pr_hostname, 1686194118Sjamie sizeof(tpr->pr_hostname)); 1687193066Sjamie if (domain != NULL) 1688194118Sjamie strlcpy(tpr->pr_domainname, 1689194118Sjamie pr->pr_domainname, 1690194118Sjamie sizeof(tpr->pr_domainname)); 1691193066Sjamie if (uuid != NULL) 1692194118Sjamie strlcpy(tpr->pr_hostuuid, 1693194118Sjamie pr->pr_hostuuid, 1694194118Sjamie sizeof(tpr->pr_hostuuid)); 1695193066Sjamie if (gothid) 1696193066Sjamie tpr->pr_hostid = hid; 1697193066Sjamie } 1698193066Sjamie } 1699193066Sjamie } 1700192895Sjamie if ((tallow = ch_allow & ~pr_allow)) { 1701192895Sjamie /* Clear allow bits in all children. */ 1702192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1703192895Sjamie tpr->pr_allow &= ~tallow; 1704192895Sjamie } 1705192895Sjamie pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow; 1706191673Sjamie /* 1707191673Sjamie * Persistent prisons get an extra reference, and prisons losing their 1708191673Sjamie * persist flag lose that reference. Only do this for existing prisons 1709191673Sjamie * for now, so new ones will remain unseen until after the module 1710191673Sjamie * handlers have completed. 1711191673Sjamie */ 1712191673Sjamie if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) { 1713191673Sjamie if (pr_flags & PR_PERSIST) { 1714191673Sjamie pr->pr_ref++; 1715191673Sjamie pr->pr_uref++; 1716191673Sjamie } else { 1717191673Sjamie pr->pr_ref--; 1718191673Sjamie pr->pr_uref--; 1719191673Sjamie } 1720191673Sjamie } 1721191673Sjamie pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags; 1722191673Sjamie mtx_unlock(&pr->pr_mtx); 1723185435Sbz 1724221362Strasz#ifdef RACCT 1725221362Strasz if (created) 1726221362Strasz prison_racct_attach(pr); 1727221362Strasz#endif 1728221362Strasz 1729192895Sjamie /* Locks may have prevented a complete restriction of child IP 1730192895Sjamie * addresses. If so, allocate some more memory and try again. 1731192895Sjamie */ 1732192895Sjamie#ifdef INET 1733192895Sjamie while (redo_ip4) { 1734192895Sjamie ip4s = pr->pr_ip4s; 1735192895Sjamie ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 1736192895Sjamie mtx_lock(&pr->pr_mtx); 1737192895Sjamie redo_ip4 = 0; 1738192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1739195945Sjamie#ifdef VIMAGE 1740195945Sjamie if (tpr->pr_flags & PR_VNET) { 1741195945Sjamie descend = 0; 1742195945Sjamie continue; 1743195945Sjamie } 1744195945Sjamie#endif 1745192895Sjamie if (prison_restrict_ip4(tpr, ip4)) { 1746192895Sjamie if (ip4 != NULL) 1747192895Sjamie ip4 = NULL; 1748192895Sjamie else 1749192895Sjamie redo_ip4 = 1; 1750192895Sjamie } 1751192895Sjamie } 1752192895Sjamie mtx_unlock(&pr->pr_mtx); 1753192895Sjamie } 1754192895Sjamie#endif 1755192895Sjamie#ifdef INET6 1756192895Sjamie while (redo_ip6) { 1757192895Sjamie ip6s = pr->pr_ip6s; 1758192895Sjamie ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 1759192895Sjamie mtx_lock(&pr->pr_mtx); 1760192895Sjamie redo_ip6 = 0; 1761192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1762195945Sjamie#ifdef VIMAGE 1763195945Sjamie if (tpr->pr_flags & PR_VNET) { 1764195945Sjamie descend = 0; 1765195945Sjamie continue; 1766195945Sjamie } 1767195945Sjamie#endif 1768192895Sjamie if (prison_restrict_ip6(tpr, ip6)) { 1769192895Sjamie if (ip6 != NULL) 1770192895Sjamie ip6 = NULL; 1771192895Sjamie else 1772192895Sjamie redo_ip6 = 1; 1773192895Sjamie } 1774192895Sjamie } 1775192895Sjamie mtx_unlock(&pr->pr_mtx); 1776192895Sjamie } 1777192895Sjamie#endif 1778192895Sjamie 1779191673Sjamie /* Let the modules do their work. */ 1780191673Sjamie sx_downgrade(&allprison_lock); 1781191673Sjamie if (created) { 1782191673Sjamie error = osd_jail_call(pr, PR_METHOD_CREATE, opts); 1783191673Sjamie if (error) { 1784191673Sjamie prison_deref(pr, PD_LIST_SLOCKED); 1785191673Sjamie goto done_errmsg; 1786191673Sjamie } 1787191673Sjamie } 1788191673Sjamie error = osd_jail_call(pr, PR_METHOD_SET, opts); 1789191673Sjamie if (error) { 1790191673Sjamie prison_deref(pr, created 1791191673Sjamie ? PD_LIST_SLOCKED 1792191673Sjamie : PD_DEREF | PD_LIST_SLOCKED); 1793191673Sjamie goto done_errmsg; 1794191673Sjamie } 1795191673Sjamie 1796191673Sjamie /* Attach this process to the prison if requested. */ 1797191673Sjamie if (flags & JAIL_ATTACH) { 1798191673Sjamie mtx_lock(&pr->pr_mtx); 1799191673Sjamie error = do_jail_attach(td, pr); 1800191673Sjamie if (error) { 1801191673Sjamie vfs_opterror(opts, "attach failed"); 1802191673Sjamie if (!created) 1803191673Sjamie prison_deref(pr, PD_DEREF); 1804191673Sjamie goto done_errmsg; 1805191673Sjamie } 1806191673Sjamie } 1807191673Sjamie 1808191673Sjamie /* 1809191673Sjamie * Now that it is all there, drop the temporary reference from existing 1810191673Sjamie * prisons. Or add a reference to newly created persistent prisons 1811191673Sjamie * (which was not done earlier so that the prison would not be publicly 1812191673Sjamie * visible). 1813191673Sjamie */ 1814191673Sjamie if (!created) { 1815191673Sjamie prison_deref(pr, (flags & JAIL_ATTACH) 1816191673Sjamie ? PD_DEREF 1817191673Sjamie : PD_DEREF | PD_LIST_SLOCKED); 1818191673Sjamie } else { 1819191673Sjamie if (pr_flags & PR_PERSIST) { 1820191673Sjamie mtx_lock(&pr->pr_mtx); 1821191673Sjamie pr->pr_ref++; 1822191673Sjamie pr->pr_uref++; 1823191673Sjamie mtx_unlock(&pr->pr_mtx); 1824191673Sjamie } 1825191673Sjamie if (!(flags & JAIL_ATTACH)) 1826191673Sjamie sx_sunlock(&allprison_lock); 1827191673Sjamie } 1828191673Sjamie td->td_retval[0] = pr->pr_id; 1829191673Sjamie goto done_errmsg; 1830191673Sjamie 1831192895Sjamie done_deref_locked: 1832192895Sjamie prison_deref(pr, created 1833192895Sjamie ? PD_LOCKED | PD_LIST_XLOCKED 1834192895Sjamie : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 1835192895Sjamie goto done_releroot; 1836191673Sjamie done_unlock_list: 1837191673Sjamie sx_xunlock(&allprison_lock); 1838191673Sjamie done_releroot: 1839191673Sjamie if (root != NULL) { 1840191673Sjamie vfslocked = VFS_LOCK_GIANT(root->v_mount); 1841191673Sjamie vrele(root); 1842191673Sjamie VFS_UNLOCK_GIANT(vfslocked); 1843191673Sjamie } 1844191673Sjamie done_errmsg: 1845191673Sjamie if (error) { 1846191673Sjamie vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 1847191673Sjamie if (errmsg_len > 0) { 1848191673Sjamie errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1; 1849191673Sjamie if (errmsg_pos > 0) { 1850191673Sjamie if (optuio->uio_segflg == UIO_SYSSPACE) 1851191673Sjamie bcopy(errmsg, 1852191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 1853191673Sjamie errmsg_len); 1854191673Sjamie else 1855191673Sjamie copyout(errmsg, 1856191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 1857191673Sjamie errmsg_len); 1858191673Sjamie } 1859191673Sjamie } 1860191673Sjamie } 1861191673Sjamie done_free: 1862191673Sjamie#ifdef INET 1863191673Sjamie free(ip4, M_PRISON); 1864191673Sjamie#endif 1865191673Sjamie#ifdef INET6 1866191673Sjamie free(ip6, M_PRISON); 1867191673Sjamie#endif 1868230407Smm if (g_path != NULL) 1869230407Smm free(g_path, M_TEMP); 1870191673Sjamie vfs_freeopts(opts); 1871191673Sjamie return (error); 1872191673Sjamie} 1873191673Sjamie 1874191673Sjamie 187582710Sdillon/* 1876191673Sjamie * struct jail_get_args { 1877191673Sjamie * struct iovec *iovp; 1878191673Sjamie * unsigned int iovcnt; 1879191673Sjamie * int flags; 1880114168Smike * }; 188182710Sdillon */ 188246155Sphkint 1883225617Skmacysys_jail_get(struct thread *td, struct jail_get_args *uap) 188446155Sphk{ 1885191673Sjamie struct uio *auio; 1886185435Sbz int error; 1887185435Sbz 1888191673Sjamie /* Check that we have an even number of iovecs. */ 1889191673Sjamie if (uap->iovcnt & 1) 1890191673Sjamie return (EINVAL); 1891191673Sjamie 1892191673Sjamie error = copyinuio(uap->iovp, uap->iovcnt, &auio); 1893185435Sbz if (error) 1894185435Sbz return (error); 1895191673Sjamie error = kern_jail_get(td, auio, uap->flags); 1896191673Sjamie if (error == 0) 1897191673Sjamie error = copyout(auio->uio_iov, uap->iovp, 1898191673Sjamie uap->iovcnt * sizeof (struct iovec)); 1899191673Sjamie free(auio, M_IOV); 1900191673Sjamie return (error); 1901191673Sjamie} 1902185435Sbz 1903191673Sjamieint 1904191673Sjamiekern_jail_get(struct thread *td, struct uio *optuio, int flags) 1905191673Sjamie{ 1906192895Sjamie struct prison *pr, *mypr; 1907191673Sjamie struct vfsopt *opt; 1908191673Sjamie struct vfsoptlist *opts; 1909191673Sjamie char *errmsg, *name; 1910192895Sjamie int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos; 1911185435Sbz 1912191673Sjamie if (flags & ~JAIL_GET_MASK) 1913191673Sjamie return (EINVAL); 1914185435Sbz 1915191673Sjamie /* Get the parameter list. */ 1916191673Sjamie error = vfs_buildopts(optuio, &opts); 1917191673Sjamie if (error) 1918191673Sjamie return (error); 1919191673Sjamie errmsg_pos = vfs_getopt_pos(opts, "errmsg"); 1920192895Sjamie mypr = td->td_ucred->cr_prison; 1921185435Sbz 1922191673Sjamie /* 1923191673Sjamie * Find the prison specified by one of: lastjid, jid, name. 1924191673Sjamie */ 1925191673Sjamie sx_slock(&allprison_lock); 1926191673Sjamie error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid)); 1927191673Sjamie if (error == 0) { 1928191673Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) { 1929192895Sjamie if (pr->pr_id > jid && prison_ischild(mypr, pr)) { 1930191673Sjamie mtx_lock(&pr->pr_mtx); 1931191673Sjamie if (pr->pr_ref > 0 && 1932191673Sjamie (pr->pr_uref > 0 || (flags & JAIL_DYING))) 1933191673Sjamie break; 1934191673Sjamie mtx_unlock(&pr->pr_mtx); 1935191673Sjamie } 1936191673Sjamie } 1937191673Sjamie if (pr != NULL) 1938191673Sjamie goto found_prison; 1939191673Sjamie error = ENOENT; 1940191673Sjamie vfs_opterror(opts, "no jail after %d", jid); 1941191673Sjamie goto done_unlock_list; 1942191673Sjamie } else if (error != ENOENT) 1943191673Sjamie goto done_unlock_list; 1944185435Sbz 1945191673Sjamie error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 1946191673Sjamie if (error == 0) { 1947191673Sjamie if (jid != 0) { 1948192895Sjamie pr = prison_find_child(mypr, jid); 1949191673Sjamie if (pr != NULL) { 1950191673Sjamie if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 1951191673Sjamie mtx_unlock(&pr->pr_mtx); 1952191673Sjamie error = ENOENT; 1953191673Sjamie vfs_opterror(opts, "jail %d is dying", 1954191673Sjamie jid); 1955191673Sjamie goto done_unlock_list; 1956191673Sjamie } 1957191673Sjamie goto found_prison; 1958191673Sjamie } 1959191673Sjamie error = ENOENT; 1960191673Sjamie vfs_opterror(opts, "jail %d not found", jid); 1961191673Sjamie goto done_unlock_list; 1962191673Sjamie } 1963191673Sjamie } else if (error != ENOENT) 1964191673Sjamie goto done_unlock_list; 196546155Sphk 1966191673Sjamie error = vfs_getopt(opts, "name", (void **)&name, &len); 1967191673Sjamie if (error == 0) { 1968191673Sjamie if (len == 0 || name[len - 1] != '\0') { 1969191673Sjamie error = EINVAL; 1970191673Sjamie goto done_unlock_list; 1971191673Sjamie } 1972192895Sjamie pr = prison_find_name(mypr, name); 1973191673Sjamie if (pr != NULL) { 1974191673Sjamie if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 1975191673Sjamie mtx_unlock(&pr->pr_mtx); 1976191673Sjamie error = ENOENT; 1977191673Sjamie vfs_opterror(opts, "jail \"%s\" is dying", 1978191673Sjamie name); 1979191673Sjamie goto done_unlock_list; 1980191673Sjamie } 1981191673Sjamie goto found_prison; 1982191673Sjamie } 1983191673Sjamie error = ENOENT; 1984191673Sjamie vfs_opterror(opts, "jail \"%s\" not found", name); 1985191673Sjamie goto done_unlock_list; 1986191673Sjamie } else if (error != ENOENT) 1987191673Sjamie goto done_unlock_list; 1988185435Sbz 1989191673Sjamie vfs_opterror(opts, "no jail specified"); 1990191673Sjamie error = ENOENT; 1991191673Sjamie goto done_unlock_list; 1992191673Sjamie 1993191673Sjamie found_prison: 1994191673Sjamie /* Get the parameters of the prison. */ 1995191673Sjamie pr->pr_ref++; 1996191673Sjamie locked = PD_LOCKED; 1997191673Sjamie td->td_retval[0] = pr->pr_id; 1998191673Sjamie error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id)); 1999191673Sjamie if (error != 0 && error != ENOENT) 2000191673Sjamie goto done_deref; 2001192895Sjamie i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id; 2002192895Sjamie error = vfs_setopt(opts, "parent", &i, sizeof(i)); 2003191673Sjamie if (error != 0 && error != ENOENT) 2004191673Sjamie goto done_deref; 2005192895Sjamie error = vfs_setopts(opts, "name", prison_name(mypr, pr)); 2006192895Sjamie if (error != 0 && error != ENOENT) 2007192895Sjamie goto done_deref; 2008192895Sjamie error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id, 2009191673Sjamie sizeof(pr->pr_cpuset->cs_id)); 2010191673Sjamie if (error != 0 && error != ENOENT) 2011191673Sjamie goto done_deref; 2012192895Sjamie error = vfs_setopts(opts, "path", prison_path(mypr, pr)); 2013191673Sjamie if (error != 0 && error != ENOENT) 2014191673Sjamie goto done_deref; 2015191673Sjamie#ifdef INET 2016191673Sjamie error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4, 2017191673Sjamie pr->pr_ip4s * sizeof(*pr->pr_ip4)); 2018191673Sjamie if (error != 0 && error != ENOENT) 2019191673Sjamie goto done_deref; 2020191673Sjamie#endif 2021191673Sjamie#ifdef INET6 2022191673Sjamie error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6, 2023191673Sjamie pr->pr_ip6s * sizeof(*pr->pr_ip6)); 2024191673Sjamie if (error != 0 && error != ENOENT) 2025191673Sjamie goto done_deref; 2026191673Sjamie#endif 2027191673Sjamie error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel, 2028191673Sjamie sizeof(pr->pr_securelevel)); 2029191673Sjamie if (error != 0 && error != ENOENT) 2030191673Sjamie goto done_deref; 2031194762Sjamie error = vfs_setopt(opts, "children.cur", &pr->pr_childcount, 2032194762Sjamie sizeof(pr->pr_childcount)); 2033194762Sjamie if (error != 0 && error != ENOENT) 2034194762Sjamie goto done_deref; 2035194762Sjamie error = vfs_setopt(opts, "children.max", &pr->pr_childmax, 2036194762Sjamie sizeof(pr->pr_childmax)); 2037194762Sjamie if (error != 0 && error != ENOENT) 2038194762Sjamie goto done_deref; 2039194118Sjamie error = vfs_setopts(opts, "host.hostname", pr->pr_hostname); 2040191673Sjamie if (error != 0 && error != ENOENT) 2041191673Sjamie goto done_deref; 2042194118Sjamie error = vfs_setopts(opts, "host.domainname", pr->pr_domainname); 2043193066Sjamie if (error != 0 && error != ENOENT) 2044193066Sjamie goto done_deref; 2045194118Sjamie error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid); 2046193066Sjamie if (error != 0 && error != ENOENT) 2047193066Sjamie goto done_deref; 2048205014Snwhitehorn#ifdef COMPAT_FREEBSD32 2049217896Sdchagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 2050193066Sjamie uint32_t hid32 = pr->pr_hostid; 2051193066Sjamie 2052193066Sjamie error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32)); 2053193066Sjamie } else 2054193066Sjamie#endif 2055193066Sjamie error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid, 2056193066Sjamie sizeof(pr->pr_hostid)); 2057193066Sjamie if (error != 0 && error != ENOENT) 2058193066Sjamie goto done_deref; 2059192895Sjamie error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs, 2060192895Sjamie sizeof(pr->pr_enforce_statfs)); 2061191673Sjamie if (error != 0 && error != ENOENT) 2062191673Sjamie goto done_deref; 2063231267Smm error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum, 2064231267Smm sizeof(pr->pr_devfs_rsnum)); 2065231267Smm if (error != 0 && error != ENOENT) 2066231267Smm goto done_deref; 2067192895Sjamie for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 2068192895Sjamie fi++) { 2069192895Sjamie if (pr_flag_names[fi] == NULL) 2070192895Sjamie continue; 2071192895Sjamie i = (pr->pr_flags & (1 << fi)) ? 1 : 0; 2072192895Sjamie error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i)); 2073192895Sjamie if (error != 0 && error != ENOENT) 2074192895Sjamie goto done_deref; 2075192895Sjamie i = !i; 2076192895Sjamie error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i)); 2077192895Sjamie if (error != 0 && error != ENOENT) 2078192895Sjamie goto done_deref; 2079192895Sjamie } 2080195870Sjamie for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 2081195870Sjamie fi++) { 2082195870Sjamie i = pr->pr_flags & 2083195870Sjamie (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new); 2084195870Sjamie i = pr_flag_jailsys[fi].disable && 2085195870Sjamie (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE 2086195870Sjamie : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW 2087195870Sjamie : JAIL_SYS_INHERIT; 2088195870Sjamie error = 2089195870Sjamie vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i)); 2090195870Sjamie if (error != 0 && error != ENOENT) 2091195870Sjamie goto done_deref; 2092195870Sjamie } 2093192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 2094192895Sjamie fi++) { 2095192895Sjamie if (pr_allow_names[fi] == NULL) 2096192895Sjamie continue; 2097192895Sjamie i = (pr->pr_allow & (1 << fi)) ? 1 : 0; 2098192895Sjamie error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i)); 2099192895Sjamie if (error != 0 && error != ENOENT) 2100192895Sjamie goto done_deref; 2101192895Sjamie i = !i; 2102192895Sjamie error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i)); 2103192895Sjamie if (error != 0 && error != ENOENT) 2104192895Sjamie goto done_deref; 2105192895Sjamie } 2106191673Sjamie i = (pr->pr_uref == 0); 2107191673Sjamie error = vfs_setopt(opts, "dying", &i, sizeof(i)); 2108191673Sjamie if (error != 0 && error != ENOENT) 2109191673Sjamie goto done_deref; 2110191673Sjamie i = !i; 2111191673Sjamie error = vfs_setopt(opts, "nodying", &i, sizeof(i)); 2112191673Sjamie if (error != 0 && error != ENOENT) 2113191673Sjamie goto done_deref; 2114191673Sjamie 2115191673Sjamie /* Get the module parameters. */ 2116191673Sjamie mtx_unlock(&pr->pr_mtx); 2117191673Sjamie locked = 0; 2118191673Sjamie error = osd_jail_call(pr, PR_METHOD_GET, opts); 211946155Sphk if (error) 2120191673Sjamie goto done_deref; 2121191673Sjamie prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED); 212284828Sjhb 2123191673Sjamie /* By now, all parameters should have been noted. */ 2124191673Sjamie TAILQ_FOREACH(opt, opts, link) { 2125191673Sjamie if (!opt->seen && strcmp(opt->name, "errmsg")) { 2126191673Sjamie error = EINVAL; 2127191673Sjamie vfs_opterror(opts, "unknown parameter: %s", opt->name); 2128191673Sjamie goto done_errmsg; 2129191673Sjamie } 2130185435Sbz } 2131191673Sjamie 2132191673Sjamie /* Write the fetched parameters back to userspace. */ 2133191673Sjamie error = 0; 2134191673Sjamie TAILQ_FOREACH(opt, opts, link) { 2135191673Sjamie if (opt->pos >= 0 && opt->pos != errmsg_pos) { 2136191673Sjamie pos = 2 * opt->pos + 1; 2137191673Sjamie optuio->uio_iov[pos].iov_len = opt->len; 2138191673Sjamie if (opt->value != NULL) { 2139191673Sjamie if (optuio->uio_segflg == UIO_SYSSPACE) { 2140191673Sjamie bcopy(opt->value, 2141191673Sjamie optuio->uio_iov[pos].iov_base, 2142191673Sjamie opt->len); 2143191673Sjamie } else { 2144191673Sjamie error = copyout(opt->value, 2145191673Sjamie optuio->uio_iov[pos].iov_base, 2146191673Sjamie opt->len); 2147191673Sjamie if (error) 2148191673Sjamie break; 2149191673Sjamie } 2150191673Sjamie } 2151191673Sjamie } 2152185435Sbz } 2153191673Sjamie goto done_errmsg; 2154191673Sjamie 2155191673Sjamie done_deref: 2156191673Sjamie prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED); 2157191673Sjamie goto done_errmsg; 2158191673Sjamie 2159191673Sjamie done_unlock_list: 2160191673Sjamie sx_sunlock(&allprison_lock); 2161191673Sjamie done_errmsg: 2162191673Sjamie if (error && errmsg_pos >= 0) { 2163191673Sjamie vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 2164191673Sjamie errmsg_pos = 2 * errmsg_pos + 1; 2165191673Sjamie if (errmsg_len > 0) { 2166191673Sjamie if (optuio->uio_segflg == UIO_SYSSPACE) 2167191673Sjamie bcopy(errmsg, 2168191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 2169191673Sjamie errmsg_len); 2170191673Sjamie else 2171191673Sjamie copyout(errmsg, 2172191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 2173191673Sjamie errmsg_len); 2174191673Sjamie } 2175185435Sbz } 2176191673Sjamie vfs_freeopts(opts); 2177191673Sjamie return (error); 2178191673Sjamie} 2179113275Smike 2180192895Sjamie 2181191673Sjamie/* 2182191673Sjamie * struct jail_remove_args { 2183191673Sjamie * int jid; 2184191673Sjamie * }; 2185191673Sjamie */ 2186191673Sjamieint 2187225617Skmacysys_jail_remove(struct thread *td, struct jail_remove_args *uap) 2188191673Sjamie{ 2189192895Sjamie struct prison *pr, *cpr, *lpr, *tpr; 2190192895Sjamie int descend, error; 2191185435Sbz 2192191673Sjamie error = priv_check(td, PRIV_JAIL_REMOVE); 2193185435Sbz if (error) 2194191673Sjamie return (error); 2195185435Sbz 2196185435Sbz sx_xlock(&allprison_lock); 2197192895Sjamie pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2198191673Sjamie if (pr == NULL) { 2199185435Sbz sx_xunlock(&allprison_lock); 2200191673Sjamie return (EINVAL); 2201185435Sbz } 2202185435Sbz 2203192895Sjamie /* Remove all descendants of this prison, then remove this prison. */ 2204192895Sjamie pr->pr_ref++; 2205192895Sjamie pr->pr_flags |= PR_REMOVE; 2206192895Sjamie if (!LIST_EMPTY(&pr->pr_children)) { 2207192895Sjamie mtx_unlock(&pr->pr_mtx); 2208192895Sjamie lpr = NULL; 2209192895Sjamie FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 2210192895Sjamie mtx_lock(&cpr->pr_mtx); 2211192895Sjamie if (cpr->pr_ref > 0) { 2212192895Sjamie tpr = cpr; 2213192895Sjamie cpr->pr_ref++; 2214192895Sjamie cpr->pr_flags |= PR_REMOVE; 2215192895Sjamie } else { 2216192895Sjamie /* Already removed - do not do it again. */ 2217192895Sjamie tpr = NULL; 2218192895Sjamie } 2219192895Sjamie mtx_unlock(&cpr->pr_mtx); 2220192895Sjamie if (lpr != NULL) { 2221192895Sjamie mtx_lock(&lpr->pr_mtx); 2222192895Sjamie prison_remove_one(lpr); 2223192895Sjamie sx_xlock(&allprison_lock); 2224192895Sjamie } 2225192895Sjamie lpr = tpr; 2226192895Sjamie } 2227192895Sjamie if (lpr != NULL) { 2228192895Sjamie mtx_lock(&lpr->pr_mtx); 2229192895Sjamie prison_remove_one(lpr); 2230192895Sjamie sx_xlock(&allprison_lock); 2231192895Sjamie } 2232192895Sjamie mtx_lock(&pr->pr_mtx); 2233192895Sjamie } 2234192895Sjamie prison_remove_one(pr); 2235192895Sjamie return (0); 2236192895Sjamie} 2237192895Sjamie 2238192895Sjamiestatic void 2239192895Sjamieprison_remove_one(struct prison *pr) 2240192895Sjamie{ 2241192895Sjamie struct proc *p; 2242192895Sjamie int deuref; 2243192895Sjamie 2244191673Sjamie /* If the prison was persistent, it is not anymore. */ 2245191673Sjamie deuref = 0; 2246191673Sjamie if (pr->pr_flags & PR_PERSIST) { 2247191673Sjamie pr->pr_ref--; 2248191673Sjamie deuref = PD_DEUREF; 2249191673Sjamie pr->pr_flags &= ~PR_PERSIST; 2250179881Sdelphij } 2251113275Smike 2252192895Sjamie /* 2253192895Sjamie * jail_remove added a reference. If that's the only one, remove 2254192895Sjamie * the prison now. 2255192895Sjamie */ 2256192895Sjamie KASSERT(pr->pr_ref > 0, 2257192895Sjamie ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id)); 2258192895Sjamie if (pr->pr_ref == 1) { 2259191673Sjamie prison_deref(pr, 2260191673Sjamie deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 2261192895Sjamie return; 2262191673Sjamie } 2263191673Sjamie 2264113275Smike mtx_unlock(&pr->pr_mtx); 2265191673Sjamie sx_xunlock(&allprison_lock); 2266191673Sjamie /* 2267191673Sjamie * Kill all processes unfortunate enough to be attached to this prison. 2268191673Sjamie */ 2269191673Sjamie sx_slock(&allproc_lock); 2270191673Sjamie LIST_FOREACH(p, &allproc, p_list) { 2271191673Sjamie PROC_LOCK(p); 2272191673Sjamie if (p->p_state != PRS_NEW && p->p_ucred && 2273191673Sjamie p->p_ucred->cr_prison == pr) 2274225617Skmacy kern_psignal(p, SIGKILL); 2275191673Sjamie PROC_UNLOCK(p); 2276191673Sjamie } 2277191673Sjamie sx_sunlock(&allproc_lock); 2278192895Sjamie /* Remove the temporary reference added by jail_remove. */ 2279191673Sjamie prison_deref(pr, deuref | PD_DEREF); 2280113275Smike} 2281113275Smike 2282190466Sjamie 2283113275Smike/* 2284114168Smike * struct jail_attach_args { 2285114168Smike * int jid; 2286114168Smike * }; 2287113275Smike */ 2288113275Smikeint 2289225617Skmacysys_jail_attach(struct thread *td, struct jail_attach_args *uap) 2290113275Smike{ 2291113275Smike struct prison *pr; 2292191673Sjamie int error; 2293167309Spjd 2294164032Srwatson error = priv_check(td, PRIV_JAIL_ATTACH); 2295126023Snectar if (error) 2296126023Snectar return (error); 2297126023Snectar 2298168401Spjd sx_slock(&allprison_lock); 2299192895Sjamie pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2300113275Smike if (pr == NULL) { 2301168401Spjd sx_sunlock(&allprison_lock); 2302113275Smike return (EINVAL); 2303113275Smike } 2304185435Sbz 2305185435Sbz /* 2306185435Sbz * Do not allow a process to attach to a prison that is not 2307191673Sjamie * considered to be "alive". 2308185435Sbz */ 2309191673Sjamie if (pr->pr_uref == 0) { 2310185435Sbz mtx_unlock(&pr->pr_mtx); 2311185435Sbz sx_sunlock(&allprison_lock); 2312185435Sbz return (EINVAL); 2313185435Sbz } 2314191673Sjamie 2315191673Sjamie return (do_jail_attach(td, pr)); 2316191673Sjamie} 2317191673Sjamie 2318191673Sjamiestatic int 2319191673Sjamiedo_jail_attach(struct thread *td, struct prison *pr) 2320191673Sjamie{ 2321192895Sjamie struct prison *ppr; 2322191673Sjamie struct proc *p; 2323191673Sjamie struct ucred *newcred, *oldcred; 2324191673Sjamie int vfslocked, error; 2325191673Sjamie 2326191673Sjamie /* 2327191673Sjamie * XXX: Note that there is a slight race here if two threads 2328191673Sjamie * in the same privileged process attempt to attach to two 2329191673Sjamie * different jails at the same time. It is important for 2330191673Sjamie * user processes not to do this, or they might end up with 2331191673Sjamie * a process root from one prison, but attached to the jail 2332191673Sjamie * of another. 2333191673Sjamie */ 2334113275Smike pr->pr_ref++; 2335191673Sjamie pr->pr_uref++; 2336113275Smike mtx_unlock(&pr->pr_mtx); 2337191673Sjamie 2338191673Sjamie /* Let modules do whatever they need to prepare for attaching. */ 2339191673Sjamie error = osd_jail_call(pr, PR_METHOD_ATTACH, td); 2340191673Sjamie if (error) { 2341191673Sjamie prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED); 2342191673Sjamie return (error); 2343191673Sjamie } 2344168401Spjd sx_sunlock(&allprison_lock); 2345113275Smike 2346185435Sbz /* 2347185435Sbz * Reparent the newly attached process to this jail. 2348185435Sbz */ 2349192895Sjamie ppr = td->td_ucred->cr_prison; 2350191673Sjamie p = td->td_proc; 2351185435Sbz error = cpuset_setproc_update_set(p, pr->pr_cpuset); 2352185435Sbz if (error) 2353191673Sjamie goto e_revert_osd; 2354185435Sbz 2355150652Scsjp vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount); 2356175202Sattilio vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY); 2357113275Smike if ((error = change_dir(pr->pr_root, td)) != 0) 2358113275Smike goto e_unlock; 2359113275Smike#ifdef MAC 2360172930Srwatson if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root))) 2361113275Smike goto e_unlock; 2362113275Smike#endif 2363175294Sattilio VOP_UNLOCK(pr->pr_root, 0); 2364191673Sjamie if ((error = change_root(pr->pr_root, td))) 2365191673Sjamie goto e_unlock_giant; 2366150652Scsjp VFS_UNLOCK_GIANT(vfslocked); 2367113275Smike 236884828Sjhb newcred = crget(); 236984828Sjhb PROC_LOCK(p); 237084828Sjhb oldcred = p->p_ucred; 2371113275Smike setsugid(p); 237284828Sjhb crcopy(newcred, oldcred); 2373113630Sjhb newcred->cr_prison = pr; 237484828Sjhb p->p_ucred = newcred; 237584828Sjhb PROC_UNLOCK(p); 2376220137Strasz#ifdef RACCT 2377220137Strasz racct_proc_ucred_changed(p, oldcred, newcred); 2378220137Strasz#endif 237984828Sjhb crfree(oldcred); 2380192895Sjamie prison_deref(ppr, PD_DEREF | PD_DEUREF); 238146155Sphk return (0); 2382191673Sjamie e_unlock: 2383175294Sattilio VOP_UNLOCK(pr->pr_root, 0); 2384191673Sjamie e_unlock_giant: 2385150652Scsjp VFS_UNLOCK_GIANT(vfslocked); 2386191673Sjamie e_revert_osd: 2387191673Sjamie /* Tell modules this thread is still in its old jail after all. */ 2388192895Sjamie (void)osd_jail_call(ppr, PR_METHOD_ATTACH, td); 2389191673Sjamie prison_deref(pr, PD_DEREF | PD_DEUREF); 239046155Sphk return (error); 239146155Sphk} 239246155Sphk 2393192895Sjamie 2394113275Smike/* 2395113275Smike * Returns a locked prison instance, or NULL on failure. 2396113275Smike */ 2397168399Spjdstruct prison * 2398113275Smikeprison_find(int prid) 2399113275Smike{ 2400113275Smike struct prison *pr; 2401113275Smike 2402168401Spjd sx_assert(&allprison_lock, SX_LOCKED); 2403191673Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) { 2404113275Smike if (pr->pr_id == prid) { 2405113275Smike mtx_lock(&pr->pr_mtx); 2406191673Sjamie if (pr->pr_ref > 0) 2407191673Sjamie return (pr); 2408191673Sjamie mtx_unlock(&pr->pr_mtx); 2409113275Smike } 2410113275Smike } 2411113275Smike return (NULL); 2412113275Smike} 2413113275Smike 2414191673Sjamie/* 2415192895Sjamie * Find a prison that is a descendant of mypr. Returns a locked prison or NULL. 2416191673Sjamie */ 2417191673Sjamiestruct prison * 2418192895Sjamieprison_find_child(struct prison *mypr, int prid) 2419191673Sjamie{ 2420192895Sjamie struct prison *pr; 2421192895Sjamie int descend; 2422192895Sjamie 2423192895Sjamie sx_assert(&allprison_lock, SX_LOCKED); 2424192895Sjamie FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 2425192895Sjamie if (pr->pr_id == prid) { 2426192895Sjamie mtx_lock(&pr->pr_mtx); 2427192895Sjamie if (pr->pr_ref > 0) 2428192895Sjamie return (pr); 2429192895Sjamie mtx_unlock(&pr->pr_mtx); 2430192895Sjamie } 2431192895Sjamie } 2432192895Sjamie return (NULL); 2433192895Sjamie} 2434192895Sjamie 2435192895Sjamie/* 2436192895Sjamie * Look for the name relative to mypr. Returns a locked prison or NULL. 2437192895Sjamie */ 2438192895Sjamiestruct prison * 2439192895Sjamieprison_find_name(struct prison *mypr, const char *name) 2440192895Sjamie{ 2441191673Sjamie struct prison *pr, *deadpr; 2442192895Sjamie size_t mylen; 2443192895Sjamie int descend; 2444191673Sjamie 2445191673Sjamie sx_assert(&allprison_lock, SX_LOCKED); 2446192895Sjamie mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1; 2447191673Sjamie again: 2448191673Sjamie deadpr = NULL; 2449192895Sjamie FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 2450192895Sjamie if (!strcmp(pr->pr_name + mylen, name)) { 2451191673Sjamie mtx_lock(&pr->pr_mtx); 2452191673Sjamie if (pr->pr_ref > 0) { 2453191673Sjamie if (pr->pr_uref > 0) 2454191673Sjamie return (pr); 2455191673Sjamie deadpr = pr; 2456191673Sjamie } 2457191673Sjamie mtx_unlock(&pr->pr_mtx); 2458191673Sjamie } 2459191673Sjamie } 2460192895Sjamie /* There was no valid prison - perhaps there was a dying one. */ 2461191673Sjamie if (deadpr != NULL) { 2462191673Sjamie mtx_lock(&deadpr->pr_mtx); 2463191673Sjamie if (deadpr->pr_ref == 0) { 2464191673Sjamie mtx_unlock(&deadpr->pr_mtx); 2465191673Sjamie goto again; 2466191673Sjamie } 2467191673Sjamie } 2468191673Sjamie return (deadpr); 2469191673Sjamie} 2470191673Sjamie 2471191673Sjamie/* 2472192895Sjamie * See if a prison has the specific flag set. 2473192895Sjamie */ 2474192895Sjamieint 2475192895Sjamieprison_flag(struct ucred *cred, unsigned flag) 2476192895Sjamie{ 2477192895Sjamie 2478192895Sjamie /* This is an atomic read, so no locking is necessary. */ 2479192895Sjamie return (cred->cr_prison->pr_flags & flag); 2480192895Sjamie} 2481192895Sjamie 2482192895Sjamieint 2483192895Sjamieprison_allow(struct ucred *cred, unsigned flag) 2484192895Sjamie{ 2485192895Sjamie 2486192895Sjamie /* This is an atomic read, so no locking is necessary. */ 2487192895Sjamie return (cred->cr_prison->pr_allow & flag); 2488192895Sjamie} 2489192895Sjamie 2490192895Sjamie/* 2491191673Sjamie * Remove a prison reference. If that was the last reference, remove the 2492191673Sjamie * prison itself - but not in this context in case there are locks held. 2493191673Sjamie */ 249472786Srwatsonvoid 2495185029Spjdprison_free_locked(struct prison *pr) 249672786Srwatson{ 249772786Srwatson 2498185029Spjd mtx_assert(&pr->pr_mtx, MA_OWNED); 249972786Srwatson pr->pr_ref--; 250072786Srwatson if (pr->pr_ref == 0) { 2501168483Spjd mtx_unlock(&pr->pr_mtx); 2502124882Srwatson TASK_INIT(&pr->pr_task, 0, prison_complete, pr); 2503144660Sjeff taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 250487275Srwatson return; 250572786Srwatson } 250687275Srwatson mtx_unlock(&pr->pr_mtx); 250772786Srwatson} 250872786Srwatson 2509185029Spjdvoid 2510185029Spjdprison_free(struct prison *pr) 2511185029Spjd{ 2512185029Spjd 2513185029Spjd mtx_lock(&pr->pr_mtx); 2514185029Spjd prison_free_locked(pr); 2515185029Spjd} 2516185029Spjd 2517124882Srwatsonstatic void 2518124882Srwatsonprison_complete(void *context, int pending) 2519124882Srwatson{ 2520191673Sjamie 2521191673Sjamie prison_deref((struct prison *)context, 0); 2522191673Sjamie} 2523191673Sjamie 2524191673Sjamie/* 2525191673Sjamie * Remove a prison reference (usually). This internal version assumes no 2526191673Sjamie * mutexes are held, except perhaps the prison itself. If there are no more 2527191673Sjamie * references, release and delist the prison. On completion, the prison lock 2528191673Sjamie * and the allprison lock are both unlocked. 2529191673Sjamie */ 2530191673Sjamiestatic void 2531191673Sjamieprison_deref(struct prison *pr, int flags) 2532191673Sjamie{ 2533192895Sjamie struct prison *ppr, *tpr; 2534150652Scsjp int vfslocked; 2535124882Srwatson 2536191673Sjamie if (!(flags & PD_LOCKED)) 2537191673Sjamie mtx_lock(&pr->pr_mtx); 2538225191Sjamie for (;;) { 2539225191Sjamie if (flags & PD_DEUREF) { 2540225191Sjamie pr->pr_uref--; 2541225191Sjamie KASSERT(prison0.pr_uref != 0, ("prison0 pr_uref=0")); 2542192895Sjamie } 2543192895Sjamie if (flags & PD_DEREF) 2544192895Sjamie pr->pr_ref--; 2545192895Sjamie /* If the prison still has references, nothing else to do. */ 2546192895Sjamie if (pr->pr_ref > 0) { 2547192895Sjamie mtx_unlock(&pr->pr_mtx); 2548192895Sjamie if (flags & PD_LIST_SLOCKED) 2549192895Sjamie sx_sunlock(&allprison_lock); 2550192895Sjamie else if (flags & PD_LIST_XLOCKED) 2551192895Sjamie sx_xunlock(&allprison_lock); 2552192895Sjamie return; 2553191673Sjamie } 2554191673Sjamie 2555192895Sjamie mtx_unlock(&pr->pr_mtx); 2556192895Sjamie if (flags & PD_LIST_SLOCKED) { 2557192895Sjamie if (!sx_try_upgrade(&allprison_lock)) { 2558192895Sjamie sx_sunlock(&allprison_lock); 2559192895Sjamie sx_xlock(&allprison_lock); 2560192895Sjamie } 2561192895Sjamie } else if (!(flags & PD_LIST_XLOCKED)) 2562192895Sjamie sx_xlock(&allprison_lock); 2563168489Spjd 2564192895Sjamie TAILQ_REMOVE(&allprison, pr, pr_list); 2565192895Sjamie LIST_REMOVE(pr, pr_sibling); 2566192895Sjamie ppr = pr->pr_parent; 2567192895Sjamie for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 2568194762Sjamie tpr->pr_childcount--; 2569196592Sjamie sx_xunlock(&allprison_lock); 2570192895Sjamie 2571194251Sjamie#ifdef VIMAGE 2572196505Szec if (pr->pr_vnet != ppr->pr_vnet) 2573194251Sjamie vnet_destroy(pr->pr_vnet); 2574194251Sjamie#endif 2575192895Sjamie if (pr->pr_root != NULL) { 2576192895Sjamie vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount); 2577192895Sjamie vrele(pr->pr_root); 2578192895Sjamie VFS_UNLOCK_GIANT(vfslocked); 2579192895Sjamie } 2580192895Sjamie mtx_destroy(&pr->pr_mtx); 2581191673Sjamie#ifdef INET 2582192895Sjamie free(pr->pr_ip4, M_PRISON); 2583191673Sjamie#endif 2584185435Sbz#ifdef INET6 2585192895Sjamie free(pr->pr_ip6, M_PRISON); 2586185435Sbz#endif 2587192895Sjamie if (pr->pr_cpuset != NULL) 2588192895Sjamie cpuset_rel(pr->pr_cpuset); 2589192895Sjamie osd_jail_exit(pr); 2590221362Strasz#ifdef RACCT 2591221362Strasz prison_racct_detach(pr); 2592220163Strasz#endif 2593192895Sjamie free(pr, M_PRISON); 2594192895Sjamie 2595192895Sjamie /* Removing a prison frees a reference on its parent. */ 2596192895Sjamie pr = ppr; 2597192895Sjamie mtx_lock(&pr->pr_mtx); 2598225191Sjamie flags = PD_DEREF | PD_DEUREF; 2599192895Sjamie } 2600124882Srwatson} 2601124882Srwatson 260272786Srwatsonvoid 2603185029Spjdprison_hold_locked(struct prison *pr) 260472786Srwatson{ 260572786Srwatson 2606185029Spjd mtx_assert(&pr->pr_mtx, MA_OWNED); 2607168489Spjd KASSERT(pr->pr_ref > 0, 2608191671Sjamie ("Trying to hold dead prison (jid=%d).", pr->pr_id)); 260972786Srwatson pr->pr_ref++; 2610185029Spjd} 2611185029Spjd 2612185029Spjdvoid 2613185029Spjdprison_hold(struct prison *pr) 2614185029Spjd{ 2615185029Spjd 2616185029Spjd mtx_lock(&pr->pr_mtx); 2617185029Spjd prison_hold_locked(pr); 261887275Srwatson mtx_unlock(&pr->pr_mtx); 261972786Srwatson} 262072786Srwatson 2621185435Sbzvoid 2622185435Sbzprison_proc_hold(struct prison *pr) 262387275Srwatson{ 262487275Srwatson 2625185435Sbz mtx_lock(&pr->pr_mtx); 2626191673Sjamie KASSERT(pr->pr_uref > 0, 2627191673Sjamie ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id)); 2628191673Sjamie pr->pr_uref++; 2629185435Sbz mtx_unlock(&pr->pr_mtx); 263087275Srwatson} 263187275Srwatson 2632185435Sbzvoid 2633185435Sbzprison_proc_free(struct prison *pr) 2634185435Sbz{ 2635185435Sbz 2636185435Sbz mtx_lock(&pr->pr_mtx); 2637191673Sjamie KASSERT(pr->pr_uref > 0, 2638191673Sjamie ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id)); 2639191673Sjamie prison_deref(pr, PD_DEUREF | PD_LOCKED); 2640185435Sbz} 2641185435Sbz 2642185435Sbz 2643185435Sbz#ifdef INET 2644185435Sbz/* 2645192895Sjamie * Restrict a prison's IP address list with its parent's, possibly replacing 2646192895Sjamie * it. Return true if the replacement buffer was used (or would have been). 2647192895Sjamie */ 2648192895Sjamiestatic int 2649192895Sjamieprison_restrict_ip4(struct prison *pr, struct in_addr *newip4) 2650192895Sjamie{ 2651192895Sjamie int ii, ij, used; 2652192895Sjamie struct prison *ppr; 2653192895Sjamie 2654192895Sjamie ppr = pr->pr_parent; 2655192895Sjamie if (!(pr->pr_flags & PR_IP4_USER)) { 2656192895Sjamie /* This has no user settings, so just copy the parent's list. */ 2657192895Sjamie if (pr->pr_ip4s < ppr->pr_ip4s) { 2658192895Sjamie /* 2659192895Sjamie * There's no room for the parent's list. Use the 2660192895Sjamie * new list buffer, which is assumed to be big enough 2661192895Sjamie * (if it was passed). If there's no buffer, try to 2662192895Sjamie * allocate one. 2663192895Sjamie */ 2664192895Sjamie used = 1; 2665192895Sjamie if (newip4 == NULL) { 2666192895Sjamie newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4), 2667192895Sjamie M_PRISON, M_NOWAIT); 2668192895Sjamie if (newip4 != NULL) 2669192895Sjamie used = 0; 2670192895Sjamie } 2671192895Sjamie if (newip4 != NULL) { 2672192895Sjamie bcopy(ppr->pr_ip4, newip4, 2673192895Sjamie ppr->pr_ip4s * sizeof(*newip4)); 2674192895Sjamie free(pr->pr_ip4, M_PRISON); 2675192895Sjamie pr->pr_ip4 = newip4; 2676192895Sjamie pr->pr_ip4s = ppr->pr_ip4s; 2677192895Sjamie } 2678192895Sjamie return (used); 2679192895Sjamie } 2680192895Sjamie pr->pr_ip4s = ppr->pr_ip4s; 2681192895Sjamie if (pr->pr_ip4s > 0) 2682192895Sjamie bcopy(ppr->pr_ip4, pr->pr_ip4, 2683192895Sjamie pr->pr_ip4s * sizeof(*newip4)); 2684192895Sjamie else if (pr->pr_ip4 != NULL) { 2685192895Sjamie free(pr->pr_ip4, M_PRISON); 2686192895Sjamie pr->pr_ip4 = NULL; 2687192895Sjamie } 2688195974Sjamie } else if (pr->pr_ip4s > 0) { 2689192895Sjamie /* Remove addresses that aren't in the parent. */ 2690192895Sjamie for (ij = 0; ij < ppr->pr_ip4s; ij++) 2691192895Sjamie if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 2692192895Sjamie break; 2693192895Sjamie if (ij < ppr->pr_ip4s) 2694192895Sjamie ii = 1; 2695192895Sjamie else { 2696192895Sjamie bcopy(pr->pr_ip4 + 1, pr->pr_ip4, 2697192895Sjamie --pr->pr_ip4s * sizeof(*pr->pr_ip4)); 2698192895Sjamie ii = 0; 2699192895Sjamie } 2700192895Sjamie for (ij = 1; ii < pr->pr_ip4s; ) { 2701192895Sjamie if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) { 2702192895Sjamie ii++; 2703192895Sjamie continue; 2704192895Sjamie } 2705192895Sjamie switch (ij >= ppr->pr_ip4s ? -1 : 2706192895Sjamie qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) { 2707192895Sjamie case -1: 2708192895Sjamie bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii, 2709192895Sjamie (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4)); 2710192895Sjamie break; 2711192895Sjamie case 0: 2712192895Sjamie ii++; 2713192895Sjamie ij++; 2714192895Sjamie break; 2715192895Sjamie case 1: 2716192895Sjamie ij++; 2717192895Sjamie break; 2718192895Sjamie } 2719192895Sjamie } 2720192895Sjamie if (pr->pr_ip4s == 0) { 2721195870Sjamie pr->pr_flags |= PR_IP4_DISABLE; 2722192895Sjamie free(pr->pr_ip4, M_PRISON); 2723192895Sjamie pr->pr_ip4 = NULL; 2724192895Sjamie } 2725192895Sjamie } 2726192895Sjamie return (0); 2727192895Sjamie} 2728192895Sjamie 2729192895Sjamie/* 2730185435Sbz * Pass back primary IPv4 address of this jail. 2731185435Sbz * 2732192895Sjamie * If not restricted return success but do not alter the address. Caller has 2733192895Sjamie * to make sure to initialize it correctly (e.g. INADDR_ANY). 2734185435Sbz * 2735188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4. 2736188144Sjamie * Address returned in NBO. 2737185435Sbz */ 273846155Sphkint 2739187684Sbzprison_get_ip4(struct ucred *cred, struct in_addr *ia) 274046155Sphk{ 2741191673Sjamie struct prison *pr; 274246155Sphk 2743185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2744185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2745185435Sbz 2746192895Sjamie pr = cred->cr_prison; 2747192895Sjamie if (!(pr->pr_flags & PR_IP4)) 274846155Sphk return (0); 2749191673Sjamie mtx_lock(&pr->pr_mtx); 2750192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2751192895Sjamie mtx_unlock(&pr->pr_mtx); 2752192895Sjamie return (0); 2753192895Sjamie } 2754191673Sjamie if (pr->pr_ip4 == NULL) { 2755191673Sjamie mtx_unlock(&pr->pr_mtx); 2756188144Sjamie return (EAFNOSUPPORT); 2757191673Sjamie } 2758185435Sbz 2759191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2760191673Sjamie mtx_unlock(&pr->pr_mtx); 2761185435Sbz return (0); 2762185435Sbz} 2763185435Sbz 2764185435Sbz/* 2765202468Sbz * Return 1 if we should do proper source address selection or are not jailed. 2766202468Sbz * We will return 0 if we should bypass source address selection in favour 2767202468Sbz * of the primary jail IPv4 address. Only in this case *ia will be updated and 2768202468Sbz * returned in NBO. 2769202468Sbz * Return EAFNOSUPPORT, in case this jail does not allow IPv4. 2770202468Sbz */ 2771202468Sbzint 2772202468Sbzprison_saddrsel_ip4(struct ucred *cred, struct in_addr *ia) 2773202468Sbz{ 2774202468Sbz struct prison *pr; 2775202468Sbz struct in_addr lia; 2776202468Sbz int error; 2777202468Sbz 2778202468Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2779202468Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2780202468Sbz 2781202468Sbz if (!jailed(cred)) 2782202468Sbz return (1); 2783202468Sbz 2784202468Sbz pr = cred->cr_prison; 2785202468Sbz if (pr->pr_flags & PR_IP4_SADDRSEL) 2786202468Sbz return (1); 2787202468Sbz 2788202468Sbz lia.s_addr = INADDR_ANY; 2789202468Sbz error = prison_get_ip4(cred, &lia); 2790202468Sbz if (error) 2791202468Sbz return (error); 2792202468Sbz if (lia.s_addr == INADDR_ANY) 2793202468Sbz return (1); 2794202468Sbz 2795202468Sbz ia->s_addr = lia.s_addr; 2796202468Sbz return (0); 2797202468Sbz} 2798202468Sbz 2799202468Sbz/* 2800192895Sjamie * Return true if pr1 and pr2 have the same IPv4 address restrictions. 2801192895Sjamie */ 2802192895Sjamieint 2803192895Sjamieprison_equal_ip4(struct prison *pr1, struct prison *pr2) 2804192895Sjamie{ 2805192895Sjamie 2806192895Sjamie if (pr1 == pr2) 2807192895Sjamie return (1); 2808192895Sjamie 2809192895Sjamie /* 2810195974Sjamie * No need to lock since the PR_IP4_USER flag can't be altered for 2811195974Sjamie * existing prisons. 2812192895Sjamie */ 2813195945Sjamie while (pr1 != &prison0 && 2814195945Sjamie#ifdef VIMAGE 2815195945Sjamie !(pr1->pr_flags & PR_VNET) && 2816195945Sjamie#endif 2817195945Sjamie !(pr1->pr_flags & PR_IP4_USER)) 2818192895Sjamie pr1 = pr1->pr_parent; 2819195945Sjamie while (pr2 != &prison0 && 2820195945Sjamie#ifdef VIMAGE 2821195945Sjamie !(pr2->pr_flags & PR_VNET) && 2822195945Sjamie#endif 2823195945Sjamie !(pr2->pr_flags & PR_IP4_USER)) 2824192895Sjamie pr2 = pr2->pr_parent; 2825192895Sjamie return (pr1 == pr2); 2826192895Sjamie} 2827192895Sjamie 2828192895Sjamie/* 2829185435Sbz * Make sure our (source) address is set to something meaningful to this 2830185435Sbz * jail. 2831185435Sbz * 2832192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail, 2833192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 2834192895Sjamie * doesn't allow IPv4. Address passed in in NBO and returned in NBO. 2835185435Sbz */ 2836185435Sbzint 2837185435Sbzprison_local_ip4(struct ucred *cred, struct in_addr *ia) 2838185435Sbz{ 2839191673Sjamie struct prison *pr; 2840185435Sbz struct in_addr ia0; 2841191673Sjamie int error; 2842185435Sbz 2843185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2844185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2845185435Sbz 2846192895Sjamie pr = cred->cr_prison; 2847192895Sjamie if (!(pr->pr_flags & PR_IP4)) 284846155Sphk return (0); 2849191673Sjamie mtx_lock(&pr->pr_mtx); 2850192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2851192895Sjamie mtx_unlock(&pr->pr_mtx); 2852192895Sjamie return (0); 2853192895Sjamie } 2854191673Sjamie if (pr->pr_ip4 == NULL) { 2855191673Sjamie mtx_unlock(&pr->pr_mtx); 2856188144Sjamie return (EAFNOSUPPORT); 2857191673Sjamie } 2858185435Sbz 2859185435Sbz ia0.s_addr = ntohl(ia->s_addr); 2860185435Sbz if (ia0.s_addr == INADDR_LOOPBACK) { 2861191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2862191673Sjamie mtx_unlock(&pr->pr_mtx); 2863185435Sbz return (0); 286446155Sphk } 2865185435Sbz 2866188144Sjamie if (ia0.s_addr == INADDR_ANY) { 2867188144Sjamie /* 2868188144Sjamie * In case there is only 1 IPv4 address, bind directly. 2869188144Sjamie */ 2870191673Sjamie if (pr->pr_ip4s == 1) 2871191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2872191673Sjamie mtx_unlock(&pr->pr_mtx); 2873185435Sbz return (0); 2874185435Sbz } 2875185435Sbz 2876191673Sjamie error = _prison_check_ip4(pr, ia); 2877191673Sjamie mtx_unlock(&pr->pr_mtx); 2878191673Sjamie return (error); 2879185435Sbz} 2880185435Sbz 2881185435Sbz/* 2882185435Sbz * Rewrite destination address in case we will connect to loopback address. 2883185435Sbz * 2884188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4. 2885188144Sjamie * Address passed in in NBO and returned in NBO. 2886185435Sbz */ 2887185435Sbzint 2888185435Sbzprison_remote_ip4(struct ucred *cred, struct in_addr *ia) 2889185435Sbz{ 2890191673Sjamie struct prison *pr; 2891185435Sbz 2892185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2893185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2894185435Sbz 2895192895Sjamie pr = cred->cr_prison; 2896192895Sjamie if (!(pr->pr_flags & PR_IP4)) 2897185435Sbz return (0); 2898191673Sjamie mtx_lock(&pr->pr_mtx); 2899192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2900192895Sjamie mtx_unlock(&pr->pr_mtx); 2901192895Sjamie return (0); 2902192895Sjamie } 2903191673Sjamie if (pr->pr_ip4 == NULL) { 2904191673Sjamie mtx_unlock(&pr->pr_mtx); 2905188144Sjamie return (EAFNOSUPPORT); 2906191673Sjamie } 2907188144Sjamie 2908185435Sbz if (ntohl(ia->s_addr) == INADDR_LOOPBACK) { 2909191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2910191673Sjamie mtx_unlock(&pr->pr_mtx); 2911185435Sbz return (0); 2912185435Sbz } 2913185435Sbz 2914185435Sbz /* 2915185435Sbz * Return success because nothing had to be changed. 2916185435Sbz */ 2917191673Sjamie mtx_unlock(&pr->pr_mtx); 2918185435Sbz return (0); 2919185435Sbz} 2920185435Sbz 2921185435Sbz/* 2922188144Sjamie * Check if given address belongs to the jail referenced by cred/prison. 2923185435Sbz * 2924192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail, 2925192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 2926192895Sjamie * doesn't allow IPv4. Address passed in in NBO. 2927185435Sbz */ 2928185435Sbzstatic int 2929185435Sbz_prison_check_ip4(struct prison *pr, struct in_addr *ia) 2930185435Sbz{ 2931185435Sbz int i, a, z, d; 2932185435Sbz 2933185435Sbz /* 2934185435Sbz * Check the primary IP. 2935185435Sbz */ 2936185435Sbz if (pr->pr_ip4[0].s_addr == ia->s_addr) 2937188144Sjamie return (0); 2938185435Sbz 2939185435Sbz /* 2940185435Sbz * All the other IPs are sorted so we can do a binary search. 2941185435Sbz */ 2942185435Sbz a = 0; 2943185435Sbz z = pr->pr_ip4s - 2; 2944185435Sbz while (a <= z) { 2945185435Sbz i = (a + z) / 2; 2946185435Sbz d = qcmp_v4(&pr->pr_ip4[i+1], ia); 2947185435Sbz if (d > 0) 2948185435Sbz z = i - 1; 2949185435Sbz else if (d < 0) 2950185435Sbz a = i + 1; 295181114Srwatson else 2952188144Sjamie return (0); 2953185435Sbz } 2954188144Sjamie 2955188144Sjamie return (EADDRNOTAVAIL); 2956185435Sbz} 2957185435Sbz 2958185435Sbzint 2959185435Sbzprison_check_ip4(struct ucred *cred, struct in_addr *ia) 2960185435Sbz{ 2961191673Sjamie struct prison *pr; 2962191673Sjamie int error; 2963185435Sbz 2964185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2965185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2966185435Sbz 2967192895Sjamie pr = cred->cr_prison; 2968192895Sjamie if (!(pr->pr_flags & PR_IP4)) 2969188144Sjamie return (0); 2970191673Sjamie mtx_lock(&pr->pr_mtx); 2971192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2972192895Sjamie mtx_unlock(&pr->pr_mtx); 2973192895Sjamie return (0); 2974192895Sjamie } 2975191673Sjamie if (pr->pr_ip4 == NULL) { 2976191673Sjamie mtx_unlock(&pr->pr_mtx); 2977188144Sjamie return (EAFNOSUPPORT); 2978191673Sjamie } 2979185435Sbz 2980191673Sjamie error = _prison_check_ip4(pr, ia); 2981191673Sjamie mtx_unlock(&pr->pr_mtx); 2982191673Sjamie return (error); 2983185435Sbz} 2984185435Sbz#endif 2985185435Sbz 2986185435Sbz#ifdef INET6 2987192895Sjamiestatic int 2988192895Sjamieprison_restrict_ip6(struct prison *pr, struct in6_addr *newip6) 2989192895Sjamie{ 2990192895Sjamie int ii, ij, used; 2991192895Sjamie struct prison *ppr; 2992192895Sjamie 2993192895Sjamie ppr = pr->pr_parent; 2994192895Sjamie if (!(pr->pr_flags & PR_IP6_USER)) { 2995192895Sjamie /* This has no user settings, so just copy the parent's list. */ 2996192895Sjamie if (pr->pr_ip6s < ppr->pr_ip6s) { 2997192895Sjamie /* 2998192895Sjamie * There's no room for the parent's list. Use the 2999192895Sjamie * new list buffer, which is assumed to be big enough 3000192895Sjamie * (if it was passed). If there's no buffer, try to 3001192895Sjamie * allocate one. 3002192895Sjamie */ 3003192895Sjamie used = 1; 3004192895Sjamie if (newip6 == NULL) { 3005192895Sjamie newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6), 3006192895Sjamie M_PRISON, M_NOWAIT); 3007192895Sjamie if (newip6 != NULL) 3008192895Sjamie used = 0; 3009192895Sjamie } 3010192895Sjamie if (newip6 != NULL) { 3011192895Sjamie bcopy(ppr->pr_ip6, newip6, 3012192895Sjamie ppr->pr_ip6s * sizeof(*newip6)); 3013192895Sjamie free(pr->pr_ip6, M_PRISON); 3014192895Sjamie pr->pr_ip6 = newip6; 3015192895Sjamie pr->pr_ip6s = ppr->pr_ip6s; 3016192895Sjamie } 3017192895Sjamie return (used); 3018192895Sjamie } 3019192895Sjamie pr->pr_ip6s = ppr->pr_ip6s; 3020192895Sjamie if (pr->pr_ip6s > 0) 3021192895Sjamie bcopy(ppr->pr_ip6, pr->pr_ip6, 3022192895Sjamie pr->pr_ip6s * sizeof(*newip6)); 3023192895Sjamie else if (pr->pr_ip6 != NULL) { 3024192895Sjamie free(pr->pr_ip6, M_PRISON); 3025192895Sjamie pr->pr_ip6 = NULL; 3026192895Sjamie } 3027195974Sjamie } else if (pr->pr_ip6s > 0) { 3028192895Sjamie /* Remove addresses that aren't in the parent. */ 3029192895Sjamie for (ij = 0; ij < ppr->pr_ip6s; ij++) 3030192895Sjamie if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], 3031192895Sjamie &ppr->pr_ip6[ij])) 3032192895Sjamie break; 3033192895Sjamie if (ij < ppr->pr_ip6s) 3034192895Sjamie ii = 1; 3035192895Sjamie else { 3036192895Sjamie bcopy(pr->pr_ip6 + 1, pr->pr_ip6, 3037192895Sjamie --pr->pr_ip6s * sizeof(*pr->pr_ip6)); 3038192895Sjamie ii = 0; 3039192895Sjamie } 3040192895Sjamie for (ij = 1; ii < pr->pr_ip6s; ) { 3041192895Sjamie if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii], 3042192895Sjamie &ppr->pr_ip6[0])) { 3043192895Sjamie ii++; 3044192895Sjamie continue; 3045192895Sjamie } 3046192895Sjamie switch (ij >= ppr->pr_ip4s ? -1 : 3047192895Sjamie qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) { 3048192895Sjamie case -1: 3049192895Sjamie bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii, 3050192895Sjamie (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6)); 3051192895Sjamie break; 3052192895Sjamie case 0: 3053192895Sjamie ii++; 3054192895Sjamie ij++; 3055192895Sjamie break; 3056192895Sjamie case 1: 3057192895Sjamie ij++; 3058192895Sjamie break; 3059192895Sjamie } 3060192895Sjamie } 3061192895Sjamie if (pr->pr_ip6s == 0) { 3062195870Sjamie pr->pr_flags |= PR_IP6_DISABLE; 3063192895Sjamie free(pr->pr_ip6, M_PRISON); 3064192895Sjamie pr->pr_ip6 = NULL; 3065192895Sjamie } 3066192895Sjamie } 3067192895Sjamie return 0; 3068192895Sjamie} 3069192895Sjamie 3070185435Sbz/* 3071185435Sbz * Pass back primary IPv6 address for this jail. 3072185435Sbz * 3073192895Sjamie * If not restricted return success but do not alter the address. Caller has 3074192895Sjamie * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT). 3075185435Sbz * 3076188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6. 3077185435Sbz */ 3078185435Sbzint 3079187684Sbzprison_get_ip6(struct ucred *cred, struct in6_addr *ia6) 3080185435Sbz{ 3081191673Sjamie struct prison *pr; 3082185435Sbz 3083185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3084185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3085185435Sbz 3086192895Sjamie pr = cred->cr_prison; 3087192895Sjamie if (!(pr->pr_flags & PR_IP6)) 308881114Srwatson return (0); 3089191673Sjamie mtx_lock(&pr->pr_mtx); 3090192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3091192895Sjamie mtx_unlock(&pr->pr_mtx); 3092192895Sjamie return (0); 3093192895Sjamie } 3094191673Sjamie if (pr->pr_ip6 == NULL) { 3095191673Sjamie mtx_unlock(&pr->pr_mtx); 3096188144Sjamie return (EAFNOSUPPORT); 3097191673Sjamie } 3098188144Sjamie 3099191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3100191673Sjamie mtx_unlock(&pr->pr_mtx); 3101185435Sbz return (0); 3102185435Sbz} 3103185435Sbz 3104185435Sbz/* 3105202468Sbz * Return 1 if we should do proper source address selection or are not jailed. 3106202468Sbz * We will return 0 if we should bypass source address selection in favour 3107202468Sbz * of the primary jail IPv6 address. Only in this case *ia will be updated and 3108202468Sbz * returned in NBO. 3109202468Sbz * Return EAFNOSUPPORT, in case this jail does not allow IPv6. 3110202468Sbz */ 3111202468Sbzint 3112202468Sbzprison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia6) 3113202468Sbz{ 3114202468Sbz struct prison *pr; 3115202468Sbz struct in6_addr lia6; 3116202468Sbz int error; 3117202468Sbz 3118202468Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3119202468Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3120202468Sbz 3121202468Sbz if (!jailed(cred)) 3122202468Sbz return (1); 3123202468Sbz 3124202468Sbz pr = cred->cr_prison; 3125202468Sbz if (pr->pr_flags & PR_IP6_SADDRSEL) 3126202468Sbz return (1); 3127202468Sbz 3128202468Sbz lia6 = in6addr_any; 3129202468Sbz error = prison_get_ip6(cred, &lia6); 3130202468Sbz if (error) 3131202468Sbz return (error); 3132202468Sbz if (IN6_IS_ADDR_UNSPECIFIED(&lia6)) 3133202468Sbz return (1); 3134202468Sbz 3135202468Sbz bcopy(&lia6, ia6, sizeof(struct in6_addr)); 3136202468Sbz return (0); 3137202468Sbz} 3138202468Sbz 3139202468Sbz/* 3140192895Sjamie * Return true if pr1 and pr2 have the same IPv6 address restrictions. 3141192895Sjamie */ 3142192895Sjamieint 3143192895Sjamieprison_equal_ip6(struct prison *pr1, struct prison *pr2) 3144192895Sjamie{ 3145192895Sjamie 3146192895Sjamie if (pr1 == pr2) 3147192895Sjamie return (1); 3148192895Sjamie 3149195945Sjamie while (pr1 != &prison0 && 3150195945Sjamie#ifdef VIMAGE 3151195945Sjamie !(pr1->pr_flags & PR_VNET) && 3152195945Sjamie#endif 3153195945Sjamie !(pr1->pr_flags & PR_IP6_USER)) 3154192895Sjamie pr1 = pr1->pr_parent; 3155195945Sjamie while (pr2 != &prison0 && 3156195945Sjamie#ifdef VIMAGE 3157195945Sjamie !(pr2->pr_flags & PR_VNET) && 3158195945Sjamie#endif 3159195945Sjamie !(pr2->pr_flags & PR_IP6_USER)) 3160192895Sjamie pr2 = pr2->pr_parent; 3161192895Sjamie return (pr1 == pr2); 3162192895Sjamie} 3163192895Sjamie 3164192895Sjamie/* 3165185435Sbz * Make sure our (source) address is set to something meaningful to this jail. 3166185435Sbz * 3167185435Sbz * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0) 3168185435Sbz * when needed while binding. 3169185435Sbz * 3170192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail, 3171192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 3172192895Sjamie * doesn't allow IPv6. 3173185435Sbz */ 3174185435Sbzint 3175185435Sbzprison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only) 3176185435Sbz{ 3177191673Sjamie struct prison *pr; 3178191673Sjamie int error; 3179185435Sbz 3180185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3181185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3182185435Sbz 3183192895Sjamie pr = cred->cr_prison; 3184192895Sjamie if (!(pr->pr_flags & PR_IP6)) 3185185435Sbz return (0); 3186191673Sjamie mtx_lock(&pr->pr_mtx); 3187192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3188192895Sjamie mtx_unlock(&pr->pr_mtx); 3189192895Sjamie return (0); 3190192895Sjamie } 3191191673Sjamie if (pr->pr_ip6 == NULL) { 3192191673Sjamie mtx_unlock(&pr->pr_mtx); 3193188144Sjamie return (EAFNOSUPPORT); 3194191673Sjamie } 3195188144Sjamie 3196185435Sbz if (IN6_IS_ADDR_LOOPBACK(ia6)) { 3197191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3198191673Sjamie mtx_unlock(&pr->pr_mtx); 3199185435Sbz return (0); 320081114Srwatson } 3201185435Sbz 3202188144Sjamie if (IN6_IS_ADDR_UNSPECIFIED(ia6)) { 3203188144Sjamie /* 3204188144Sjamie * In case there is only 1 IPv6 address, and v6only is true, 3205188144Sjamie * then bind directly. 3206188144Sjamie */ 3207191673Sjamie if (v6only != 0 && pr->pr_ip6s == 1) 3208191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3209191673Sjamie mtx_unlock(&pr->pr_mtx); 3210185435Sbz return (0); 3211185435Sbz } 3212188144Sjamie 3213191673Sjamie error = _prison_check_ip6(pr, ia6); 3214191673Sjamie mtx_unlock(&pr->pr_mtx); 3215191673Sjamie return (error); 3216185435Sbz} 3217185435Sbz 3218185435Sbz/* 3219185435Sbz * Rewrite destination address in case we will connect to loopback address. 3220185435Sbz * 3221188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6. 3222185435Sbz */ 3223185435Sbzint 3224185435Sbzprison_remote_ip6(struct ucred *cred, struct in6_addr *ia6) 3225185435Sbz{ 3226191673Sjamie struct prison *pr; 3227185435Sbz 3228185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3229185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3230185435Sbz 3231192895Sjamie pr = cred->cr_prison; 3232192895Sjamie if (!(pr->pr_flags & PR_IP6)) 3233185435Sbz return (0); 3234191673Sjamie mtx_lock(&pr->pr_mtx); 3235192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3236192895Sjamie mtx_unlock(&pr->pr_mtx); 3237192895Sjamie return (0); 3238192895Sjamie } 3239191673Sjamie if (pr->pr_ip6 == NULL) { 3240191673Sjamie mtx_unlock(&pr->pr_mtx); 3241188144Sjamie return (EAFNOSUPPORT); 3242191673Sjamie } 3243188144Sjamie 3244185435Sbz if (IN6_IS_ADDR_LOOPBACK(ia6)) { 3245191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3246191673Sjamie mtx_unlock(&pr->pr_mtx); 3247185435Sbz return (0); 3248185435Sbz } 3249185435Sbz 3250185435Sbz /* 3251185435Sbz * Return success because nothing had to be changed. 3252185435Sbz */ 3253191673Sjamie mtx_unlock(&pr->pr_mtx); 325446155Sphk return (0); 325546155Sphk} 325646155Sphk 3257185435Sbz/* 3258188144Sjamie * Check if given address belongs to the jail referenced by cred/prison. 3259185435Sbz * 3260192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail, 3261192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 3262192895Sjamie * doesn't allow IPv6. 3263185435Sbz */ 3264185435Sbzstatic int 3265185435Sbz_prison_check_ip6(struct prison *pr, struct in6_addr *ia6) 326646155Sphk{ 3267185435Sbz int i, a, z, d; 326846155Sphk 3269185435Sbz /* 3270185435Sbz * Check the primary IP. 3271185435Sbz */ 3272185435Sbz if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6)) 3273188144Sjamie return (0); 3274185435Sbz 3275185435Sbz /* 3276185435Sbz * All the other IPs are sorted so we can do a binary search. 3277185435Sbz */ 3278185435Sbz a = 0; 3279185435Sbz z = pr->pr_ip6s - 2; 3280185435Sbz while (a <= z) { 3281185435Sbz i = (a + z) / 2; 3282185435Sbz d = qcmp_v6(&pr->pr_ip6[i+1], ia6); 3283185435Sbz if (d > 0) 3284185435Sbz z = i - 1; 3285185435Sbz else if (d < 0) 3286185435Sbz a = i + 1; 328746155Sphk else 3288188144Sjamie return (0); 328946155Sphk } 3290188144Sjamie 3291188144Sjamie return (EADDRNOTAVAIL); 329246155Sphk} 329346155Sphk 329446155Sphkint 3295185435Sbzprison_check_ip6(struct ucred *cred, struct in6_addr *ia6) 3296185435Sbz{ 3297191673Sjamie struct prison *pr; 3298191673Sjamie int error; 3299185435Sbz 3300185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3301185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3302185435Sbz 3303192895Sjamie pr = cred->cr_prison; 3304192895Sjamie if (!(pr->pr_flags & PR_IP6)) 3305188144Sjamie return (0); 3306191673Sjamie mtx_lock(&pr->pr_mtx); 3307192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3308192895Sjamie mtx_unlock(&pr->pr_mtx); 3309192895Sjamie return (0); 3310192895Sjamie } 3311191673Sjamie if (pr->pr_ip6 == NULL) { 3312191673Sjamie mtx_unlock(&pr->pr_mtx); 3313188144Sjamie return (EAFNOSUPPORT); 3314191673Sjamie } 3315185435Sbz 3316191673Sjamie error = _prison_check_ip6(pr, ia6); 3317191673Sjamie mtx_unlock(&pr->pr_mtx); 3318191673Sjamie return (error); 3319185435Sbz} 3320185435Sbz#endif 3321185435Sbz 3322185435Sbz/* 3323188146Sjamie * Check if a jail supports the given address family. 3324188146Sjamie * 3325188146Sjamie * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT 3326188146Sjamie * if not. 3327188146Sjamie */ 3328188146Sjamieint 3329188146Sjamieprison_check_af(struct ucred *cred, int af) 3330188146Sjamie{ 3331192895Sjamie struct prison *pr; 3332188146Sjamie int error; 3333188146Sjamie 3334188146Sjamie KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3335188146Sjamie 3336192895Sjamie pr = cred->cr_prison; 3337194923Sjamie#ifdef VIMAGE 3338194915Sjamie /* Prisons with their own network stack are not limited. */ 3339200473Sbz if (prison_owns_vnet(cred)) 3340194915Sjamie return (0); 3341194923Sjamie#endif 3342194915Sjamie 3343188146Sjamie error = 0; 3344188146Sjamie switch (af) 3345188146Sjamie { 3346188146Sjamie#ifdef INET 3347188146Sjamie case AF_INET: 3348192895Sjamie if (pr->pr_flags & PR_IP4) 3349192895Sjamie { 3350192895Sjamie mtx_lock(&pr->pr_mtx); 3351192895Sjamie if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL) 3352192895Sjamie error = EAFNOSUPPORT; 3353192895Sjamie mtx_unlock(&pr->pr_mtx); 3354192895Sjamie } 3355188146Sjamie break; 3356188146Sjamie#endif 3357188146Sjamie#ifdef INET6 3358188146Sjamie case AF_INET6: 3359192895Sjamie if (pr->pr_flags & PR_IP6) 3360192895Sjamie { 3361192895Sjamie mtx_lock(&pr->pr_mtx); 3362192895Sjamie if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL) 3363192895Sjamie error = EAFNOSUPPORT; 3364192895Sjamie mtx_unlock(&pr->pr_mtx); 3365192895Sjamie } 3366188146Sjamie break; 3367188146Sjamie#endif 3368188146Sjamie case AF_LOCAL: 3369188146Sjamie case AF_ROUTE: 3370188146Sjamie break; 3371188146Sjamie default: 3372192895Sjamie if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF)) 3373188146Sjamie error = EAFNOSUPPORT; 3374188146Sjamie } 3375188146Sjamie return (error); 3376188146Sjamie} 3377188146Sjamie 3378188146Sjamie/* 3379185435Sbz * Check if given address belongs to the jail referenced by cred (wrapper to 3380185435Sbz * prison_check_ip[46]). 3381185435Sbz * 3382192895Sjamie * Returns 0 if jail doesn't restrict the address family or if address belongs 3383192895Sjamie * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if 3384192895Sjamie * the jail doesn't allow the address family. IPv4 Address passed in in NBO. 3385185435Sbz */ 3386185435Sbzint 338772786Srwatsonprison_if(struct ucred *cred, struct sockaddr *sa) 338846155Sphk{ 3389185435Sbz#ifdef INET 3390114168Smike struct sockaddr_in *sai; 3391185435Sbz#endif 3392185435Sbz#ifdef INET6 3393185435Sbz struct sockaddr_in6 *sai6; 3394185435Sbz#endif 3395188144Sjamie int error; 339646155Sphk 3397185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3398185435Sbz KASSERT(sa != NULL, ("%s: sa is NULL", __func__)); 3399185435Sbz 3400200473Sbz#ifdef VIMAGE 3401200473Sbz if (prison_owns_vnet(cred)) 3402200473Sbz return (0); 3403200473Sbz#endif 3404200473Sbz 3405188144Sjamie error = 0; 3406188144Sjamie switch (sa->sa_family) 3407185435Sbz { 3408185435Sbz#ifdef INET 3409185435Sbz case AF_INET: 3410185435Sbz sai = (struct sockaddr_in *)sa; 3411188144Sjamie error = prison_check_ip4(cred, &sai->sin_addr); 3412185435Sbz break; 3413185435Sbz#endif 3414185435Sbz#ifdef INET6 3415185435Sbz case AF_INET6: 3416185435Sbz sai6 = (struct sockaddr_in6 *)sa; 3417188144Sjamie error = prison_check_ip6(cred, &sai6->sin6_addr); 3418185435Sbz break; 3419185435Sbz#endif 3420185435Sbz default: 3421192895Sjamie if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF)) 3422188144Sjamie error = EAFNOSUPPORT; 3423185435Sbz } 3424188144Sjamie return (error); 342546155Sphk} 342672786Srwatson 342772786Srwatson/* 342872786Srwatson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH. 342972786Srwatson */ 343072786Srwatsonint 3431114168Smikeprison_check(struct ucred *cred1, struct ucred *cred2) 343272786Srwatson{ 343372786Srwatson 3434192895Sjamie return ((cred1->cr_prison == cred2->cr_prison || 3435192895Sjamie prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH); 3436192895Sjamie} 343772786Srwatson 3438192895Sjamie/* 3439192895Sjamie * Return 1 if p2 is a child of p1, otherwise 0. 3440192895Sjamie */ 3441192895Sjamieint 3442192895Sjamieprison_ischild(struct prison *pr1, struct prison *pr2) 3443192895Sjamie{ 3444192895Sjamie 3445192895Sjamie for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent) 3446192895Sjamie if (pr1 == pr2) 3447192895Sjamie return (1); 344872786Srwatson return (0); 344972786Srwatson} 345072786Srwatson 345172786Srwatson/* 345272786Srwatson * Return 1 if the passed credential is in a jail, otherwise 0. 345372786Srwatson */ 345472786Srwatsonint 3455114168Smikejailed(struct ucred *cred) 345672786Srwatson{ 345772786Srwatson 3458192895Sjamie return (cred->cr_prison != &prison0); 345972786Srwatson} 346091384Srobert 346191384Srobert/* 3462200473Sbz * Return 1 if the passed credential is in a jail and that jail does not 3463200473Sbz * have its own virtual network stack, otherwise 0. 3464200473Sbz */ 3465200473Sbzint 3466200473Sbzjailed_without_vnet(struct ucred *cred) 3467200473Sbz{ 3468200473Sbz 3469200473Sbz if (!jailed(cred)) 3470200473Sbz return (0); 3471200473Sbz#ifdef VIMAGE 3472200473Sbz if (prison_owns_vnet(cred)) 3473200473Sbz return (0); 3474200473Sbz#endif 3475200473Sbz 3476200473Sbz return (1); 3477200473Sbz} 3478200473Sbz 3479200473Sbz/* 3480194090Sjamie * Return the correct hostname (domainname, et al) for the passed credential. 348191384Srobert */ 348291391Srobertvoid 3483114168Smikegetcredhostname(struct ucred *cred, char *buf, size_t size) 348491384Srobert{ 3485193066Sjamie struct prison *pr; 348691384Srobert 3487194090Sjamie /* 3488194090Sjamie * A NULL credential can be used to shortcut to the physical 3489194090Sjamie * system's hostname. 3490194090Sjamie */ 3491193066Sjamie pr = (cred != NULL) ? cred->cr_prison : &prison0; 3492193066Sjamie mtx_lock(&pr->pr_mtx); 3493194118Sjamie strlcpy(buf, pr->pr_hostname, size); 3494193066Sjamie mtx_unlock(&pr->pr_mtx); 349591384Srobert} 3496113275Smike 3497194090Sjamievoid 3498194090Sjamiegetcreddomainname(struct ucred *cred, char *buf, size_t size) 3499194090Sjamie{ 3500194090Sjamie 3501194090Sjamie mtx_lock(&cred->cr_prison->pr_mtx); 3502194118Sjamie strlcpy(buf, cred->cr_prison->pr_domainname, size); 3503194090Sjamie mtx_unlock(&cred->cr_prison->pr_mtx); 3504194090Sjamie} 3505194090Sjamie 3506194090Sjamievoid 3507194090Sjamiegetcredhostuuid(struct ucred *cred, char *buf, size_t size) 3508194090Sjamie{ 3509194090Sjamie 3510194090Sjamie mtx_lock(&cred->cr_prison->pr_mtx); 3511194118Sjamie strlcpy(buf, cred->cr_prison->pr_hostuuid, size); 3512194090Sjamie mtx_unlock(&cred->cr_prison->pr_mtx); 3513194090Sjamie} 3514194090Sjamie 3515194090Sjamievoid 3516194090Sjamiegetcredhostid(struct ucred *cred, unsigned long *hostid) 3517194090Sjamie{ 3518194090Sjamie 3519194090Sjamie mtx_lock(&cred->cr_prison->pr_mtx); 3520194090Sjamie *hostid = cred->cr_prison->pr_hostid; 3521194090Sjamie mtx_unlock(&cred->cr_prison->pr_mtx); 3522194090Sjamie} 3523194090Sjamie 3524196176Sbz#ifdef VIMAGE 3525125804Srwatson/* 3526196176Sbz * Determine whether the prison represented by cred owns 3527196176Sbz * its vnet rather than having it inherited. 3528196176Sbz * 3529196176Sbz * Returns 1 in case the prison owns the vnet, 0 otherwise. 3530196176Sbz */ 3531196176Sbzint 3532196176Sbzprison_owns_vnet(struct ucred *cred) 3533196176Sbz{ 3534196176Sbz 3535196176Sbz /* 3536196176Sbz * vnets cannot be added/removed after jail creation, 3537196176Sbz * so no need to lock here. 3538196176Sbz */ 3539196176Sbz return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0); 3540196176Sbz} 3541196176Sbz#endif 3542196176Sbz 3543196176Sbz/* 3544147185Spjd * Determine whether the subject represented by cred can "see" 3545147185Spjd * status of a mount point. 3546147185Spjd * Returns: 0 for permitted, ENOENT otherwise. 3547147185Spjd * XXX: This function should be called cr_canseemount() and should be 3548147185Spjd * placed in kern_prot.c. 3549125804Srwatson */ 3550125804Srwatsonint 3551147185Spjdprison_canseemount(struct ucred *cred, struct mount *mp) 3552125804Srwatson{ 3553147185Spjd struct prison *pr; 3554147185Spjd struct statfs *sp; 3555147185Spjd size_t len; 3556125804Srwatson 3557192895Sjamie pr = cred->cr_prison; 3558192895Sjamie if (pr->pr_enforce_statfs == 0) 3559147185Spjd return (0); 3560147185Spjd if (pr->pr_root->v_mount == mp) 3561147185Spjd return (0); 3562192895Sjamie if (pr->pr_enforce_statfs == 2) 3563147185Spjd return (ENOENT); 3564147185Spjd /* 3565147185Spjd * If jail's chroot directory is set to "/" we should be able to see 3566147185Spjd * all mount-points from inside a jail. 3567147185Spjd * This is ugly check, but this is the only situation when jail's 3568147185Spjd * directory ends with '/'. 3569147185Spjd */ 3570147185Spjd if (strcmp(pr->pr_path, "/") == 0) 3571147185Spjd return (0); 3572147185Spjd len = strlen(pr->pr_path); 3573147185Spjd sp = &mp->mnt_stat; 3574147185Spjd if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0) 3575147185Spjd return (ENOENT); 3576147185Spjd /* 3577147185Spjd * Be sure that we don't have situation where jail's root directory 3578147185Spjd * is "/some/path" and mount point is "/some/pathpath". 3579147185Spjd */ 3580147185Spjd if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/') 3581147185Spjd return (ENOENT); 3582147185Spjd return (0); 3583147185Spjd} 3584147185Spjd 3585147185Spjdvoid 3586147185Spjdprison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp) 3587147185Spjd{ 3588147185Spjd char jpath[MAXPATHLEN]; 3589147185Spjd struct prison *pr; 3590147185Spjd size_t len; 3591147185Spjd 3592192895Sjamie pr = cred->cr_prison; 3593192895Sjamie if (pr->pr_enforce_statfs == 0) 3594147185Spjd return; 3595147185Spjd if (prison_canseemount(cred, mp) != 0) { 3596147185Spjd bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3597147185Spjd strlcpy(sp->f_mntonname, "[restricted]", 3598147185Spjd sizeof(sp->f_mntonname)); 3599147185Spjd return; 3600125804Srwatson } 3601147185Spjd if (pr->pr_root->v_mount == mp) { 3602147185Spjd /* 3603147185Spjd * Clear current buffer data, so we are sure nothing from 3604147185Spjd * the valid path left there. 3605147185Spjd */ 3606147185Spjd bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3607147185Spjd *sp->f_mntonname = '/'; 3608147185Spjd return; 3609147185Spjd } 3610147185Spjd /* 3611147185Spjd * If jail's chroot directory is set to "/" we should be able to see 3612147185Spjd * all mount-points from inside a jail. 3613147185Spjd */ 3614147185Spjd if (strcmp(pr->pr_path, "/") == 0) 3615147185Spjd return; 3616147185Spjd len = strlen(pr->pr_path); 3617147185Spjd strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath)); 3618147185Spjd /* 3619147185Spjd * Clear current buffer data, so we are sure nothing from 3620147185Spjd * the valid path left there. 3621147185Spjd */ 3622147185Spjd bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3623147185Spjd if (*jpath == '\0') { 3624147185Spjd /* Should never happen. */ 3625147185Spjd *sp->f_mntonname = '/'; 3626147185Spjd } else { 3627147185Spjd strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname)); 3628147185Spjd } 3629125804Srwatson} 3630125804Srwatson 3631164032Srwatson/* 3632164032Srwatson * Check with permission for a specific privilege is granted within jail. We 3633164032Srwatson * have a specific list of accepted privileges; the rest are denied. 3634164032Srwatson */ 3635164032Srwatsonint 3636164032Srwatsonprison_priv_check(struct ucred *cred, int priv) 3637164032Srwatson{ 3638164032Srwatson 3639164032Srwatson if (!jailed(cred)) 3640164032Srwatson return (0); 3641164032Srwatson 3642194915Sjamie#ifdef VIMAGE 3643194915Sjamie /* 3644194915Sjamie * Privileges specific to prisons with a virtual network stack. 3645194915Sjamie * There might be a duplicate entry here in case the privilege 3646194915Sjamie * is only granted conditionally in the legacy jail case. 3647194915Sjamie */ 3648164032Srwatson switch (priv) { 3649194915Sjamie#ifdef notyet 3650194915Sjamie /* 3651194915Sjamie * NFS-specific privileges. 3652194915Sjamie */ 3653194915Sjamie case PRIV_NFS_DAEMON: 3654194915Sjamie case PRIV_NFS_LOCKD: 3655194915Sjamie#endif 3656194915Sjamie /* 3657194915Sjamie * Network stack privileges. 3658194915Sjamie */ 3659194915Sjamie case PRIV_NET_BRIDGE: 3660194915Sjamie case PRIV_NET_GRE: 3661194915Sjamie case PRIV_NET_BPF: 3662194915Sjamie case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */ 3663194915Sjamie case PRIV_NET_ROUTE: 3664194915Sjamie case PRIV_NET_TAP: 3665194915Sjamie case PRIV_NET_SETIFMTU: 3666194915Sjamie case PRIV_NET_SETIFFLAGS: 3667194915Sjamie case PRIV_NET_SETIFCAP: 3668203052Sdelphij case PRIV_NET_SETIFDESCR: 3669194915Sjamie case PRIV_NET_SETIFNAME : 3670194915Sjamie case PRIV_NET_SETIFMETRIC: 3671194915Sjamie case PRIV_NET_SETIFPHYS: 3672194915Sjamie case PRIV_NET_SETIFMAC: 3673194915Sjamie case PRIV_NET_ADDMULTI: 3674194915Sjamie case PRIV_NET_DELMULTI: 3675194915Sjamie case PRIV_NET_HWIOCTL: 3676194915Sjamie case PRIV_NET_SETLLADDR: 3677194915Sjamie case PRIV_NET_ADDIFGROUP: 3678194915Sjamie case PRIV_NET_DELIFGROUP: 3679194915Sjamie case PRIV_NET_IFCREATE: 3680194915Sjamie case PRIV_NET_IFDESTROY: 3681194915Sjamie case PRIV_NET_ADDIFADDR: 3682194915Sjamie case PRIV_NET_DELIFADDR: 3683194915Sjamie case PRIV_NET_LAGG: 3684194915Sjamie case PRIV_NET_GIF: 3685194915Sjamie case PRIV_NET_SETIFVNET: 3686223735Sbz case PRIV_NET_SETIFFIB: 3687164032Srwatson 3688164032Srwatson /* 3689194915Sjamie * 802.11-related privileges. 3690194915Sjamie */ 3691194915Sjamie case PRIV_NET80211_GETKEY: 3692194915Sjamie#ifdef notyet 3693194915Sjamie case PRIV_NET80211_MANAGE: /* XXX-BZ discuss with sam@ */ 3694194915Sjamie#endif 3695194915Sjamie 3696194915Sjamie#ifdef notyet 3697194915Sjamie /* 3698194915Sjamie * AppleTalk privileges. 3699194915Sjamie */ 3700194915Sjamie case PRIV_NETATALK_RESERVEDPORT: 3701194915Sjamie 3702194915Sjamie /* 3703194915Sjamie * ATM privileges. 3704194915Sjamie */ 3705194915Sjamie case PRIV_NETATM_CFG: 3706194915Sjamie case PRIV_NETATM_ADD: 3707194915Sjamie case PRIV_NETATM_DEL: 3708194915Sjamie case PRIV_NETATM_SET: 3709194915Sjamie 3710194915Sjamie /* 3711194915Sjamie * Bluetooth privileges. 3712194915Sjamie */ 3713194915Sjamie case PRIV_NETBLUETOOTH_RAW: 3714194915Sjamie#endif 3715194915Sjamie 3716194915Sjamie /* 3717194915Sjamie * Netgraph and netgraph module privileges. 3718194915Sjamie */ 3719194915Sjamie case PRIV_NETGRAPH_CONTROL: 3720194915Sjamie#ifdef notyet 3721194915Sjamie case PRIV_NETGRAPH_TTY: 3722194915Sjamie#endif 3723194915Sjamie 3724194915Sjamie /* 3725194915Sjamie * IPv4 and IPv6 privileges. 3726194915Sjamie */ 3727194915Sjamie case PRIV_NETINET_IPFW: 3728194915Sjamie case PRIV_NETINET_DIVERT: 3729194915Sjamie case PRIV_NETINET_PF: 3730194915Sjamie case PRIV_NETINET_DUMMYNET: 3731194915Sjamie case PRIV_NETINET_CARP: 3732194915Sjamie case PRIV_NETINET_MROUTE: 3733194915Sjamie case PRIV_NETINET_RAW: 3734194915Sjamie case PRIV_NETINET_ADDRCTRL6: 3735194915Sjamie case PRIV_NETINET_ND6: 3736194915Sjamie case PRIV_NETINET_SCOPE6: 3737194915Sjamie case PRIV_NETINET_ALIFETIME6: 3738194915Sjamie case PRIV_NETINET_IPSEC: 3739194915Sjamie case PRIV_NETINET_BINDANY: 3740194915Sjamie 3741194915Sjamie#ifdef notyet 3742194915Sjamie /* 3743194915Sjamie * IPX/SPX privileges. 3744194915Sjamie */ 3745194915Sjamie case PRIV_NETIPX_RESERVEDPORT: 3746194915Sjamie case PRIV_NETIPX_RAW: 3747194915Sjamie 3748194915Sjamie /* 3749194915Sjamie * NCP privileges. 3750194915Sjamie */ 3751194915Sjamie case PRIV_NETNCP: 3752194915Sjamie 3753194915Sjamie /* 3754194915Sjamie * SMB privileges. 3755194915Sjamie */ 3756194915Sjamie case PRIV_NETSMB: 3757194915Sjamie#endif 3758194915Sjamie 3759194915Sjamie /* 3760194915Sjamie * No default: or deny here. 3761194915Sjamie * In case of no permit fall through to next switch(). 3762194915Sjamie */ 3763194915Sjamie if (cred->cr_prison->pr_flags & PR_VNET) 3764194915Sjamie return (0); 3765194915Sjamie } 3766194915Sjamie#endif /* VIMAGE */ 3767194915Sjamie 3768194915Sjamie switch (priv) { 3769194915Sjamie 3770194915Sjamie /* 3771164032Srwatson * Allow ktrace privileges for root in jail. 3772164032Srwatson */ 3773164032Srwatson case PRIV_KTRACE: 3774164032Srwatson 3775166827Srwatson#if 0 3776164032Srwatson /* 3777164032Srwatson * Allow jailed processes to configure audit identity and 3778164032Srwatson * submit audit records (login, etc). In the future we may 3779164032Srwatson * want to further refine the relationship between audit and 3780164032Srwatson * jail. 3781164032Srwatson */ 3782164032Srwatson case PRIV_AUDIT_GETAUDIT: 3783164032Srwatson case PRIV_AUDIT_SETAUDIT: 3784164032Srwatson case PRIV_AUDIT_SUBMIT: 3785166827Srwatson#endif 3786164032Srwatson 3787164032Srwatson /* 3788164032Srwatson * Allow jailed processes to manipulate process UNIX 3789164032Srwatson * credentials in any way they see fit. 3790164032Srwatson */ 3791164032Srwatson case PRIV_CRED_SETUID: 3792164032Srwatson case PRIV_CRED_SETEUID: 3793164032Srwatson case PRIV_CRED_SETGID: 3794164032Srwatson case PRIV_CRED_SETEGID: 3795164032Srwatson case PRIV_CRED_SETGROUPS: 3796164032Srwatson case PRIV_CRED_SETREUID: 3797164032Srwatson case PRIV_CRED_SETREGID: 3798164032Srwatson case PRIV_CRED_SETRESUID: 3799164032Srwatson case PRIV_CRED_SETRESGID: 3800164032Srwatson 3801164032Srwatson /* 3802164032Srwatson * Jail implements visibility constraints already, so allow 3803164032Srwatson * jailed root to override uid/gid-based constraints. 3804164032Srwatson */ 3805164032Srwatson case PRIV_SEEOTHERGIDS: 3806164032Srwatson case PRIV_SEEOTHERUIDS: 3807164032Srwatson 3808164032Srwatson /* 3809164032Srwatson * Jail implements inter-process debugging limits already, so 3810164032Srwatson * allow jailed root various debugging privileges. 3811164032Srwatson */ 3812164032Srwatson case PRIV_DEBUG_DIFFCRED: 3813164032Srwatson case PRIV_DEBUG_SUGID: 3814164032Srwatson case PRIV_DEBUG_UNPRIV: 3815164032Srwatson 3816164032Srwatson /* 3817164032Srwatson * Allow jail to set various resource limits and login 3818164032Srwatson * properties, and for now, exceed process resource limits. 3819164032Srwatson */ 3820164032Srwatson case PRIV_PROC_LIMIT: 3821164032Srwatson case PRIV_PROC_SETLOGIN: 3822164032Srwatson case PRIV_PROC_SETRLIMIT: 3823164032Srwatson 3824164032Srwatson /* 3825164032Srwatson * System V and POSIX IPC privileges are granted in jail. 3826164032Srwatson */ 3827164032Srwatson case PRIV_IPC_READ: 3828164032Srwatson case PRIV_IPC_WRITE: 3829164032Srwatson case PRIV_IPC_ADMIN: 3830164032Srwatson case PRIV_IPC_MSGSIZE: 3831164032Srwatson case PRIV_MQ_ADMIN: 3832164032Srwatson 3833164032Srwatson /* 3834192895Sjamie * Jail operations within a jail work on child jails. 3835192895Sjamie */ 3836192895Sjamie case PRIV_JAIL_ATTACH: 3837192895Sjamie case PRIV_JAIL_SET: 3838192895Sjamie case PRIV_JAIL_REMOVE: 3839192895Sjamie 3840192895Sjamie /* 3841164032Srwatson * Jail implements its own inter-process limits, so allow 3842164032Srwatson * root processes in jail to change scheduling on other 3843164032Srwatson * processes in the same jail. Likewise for signalling. 3844164032Srwatson */ 3845164032Srwatson case PRIV_SCHED_DIFFCRED: 3846185435Sbz case PRIV_SCHED_CPUSET: 3847164032Srwatson case PRIV_SIGNAL_DIFFCRED: 3848164032Srwatson case PRIV_SIGNAL_SUGID: 3849164032Srwatson 3850164032Srwatson /* 3851164032Srwatson * Allow jailed processes to write to sysctls marked as jail 3852164032Srwatson * writable. 3853164032Srwatson */ 3854164032Srwatson case PRIV_SYSCTL_WRITEJAIL: 3855164032Srwatson 3856164032Srwatson /* 3857164032Srwatson * Allow root in jail to manage a variety of quota 3858166831Srwatson * properties. These should likely be conditional on a 3859166831Srwatson * configuration option. 3860164032Srwatson */ 3861166832Srwatson case PRIV_VFS_GETQUOTA: 3862166832Srwatson case PRIV_VFS_SETQUOTA: 3863164032Srwatson 3864164032Srwatson /* 3865164032Srwatson * Since Jail relies on chroot() to implement file system 3866164032Srwatson * protections, grant many VFS privileges to root in jail. 3867164032Srwatson * Be careful to exclude mount-related and NFS-related 3868164032Srwatson * privileges. 3869164032Srwatson */ 3870164032Srwatson case PRIV_VFS_READ: 3871164032Srwatson case PRIV_VFS_WRITE: 3872164032Srwatson case PRIV_VFS_ADMIN: 3873164032Srwatson case PRIV_VFS_EXEC: 3874164032Srwatson case PRIV_VFS_LOOKUP: 3875164032Srwatson case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */ 3876164032Srwatson case PRIV_VFS_CHFLAGS_DEV: 3877164032Srwatson case PRIV_VFS_CHOWN: 3878164032Srwatson case PRIV_VFS_CHROOT: 3879167152Spjd case PRIV_VFS_RETAINSUGID: 3880164032Srwatson case PRIV_VFS_FCHROOT: 3881164032Srwatson case PRIV_VFS_LINK: 3882164032Srwatson case PRIV_VFS_SETGID: 3883172860Srwatson case PRIV_VFS_STAT: 3884164032Srwatson case PRIV_VFS_STICKYFILE: 3885164032Srwatson return (0); 3886164032Srwatson 3887164032Srwatson /* 3888164032Srwatson * Depending on the global setting, allow privilege of 3889164032Srwatson * setting system flags. 3890164032Srwatson */ 3891164032Srwatson case PRIV_VFS_SYSFLAGS: 3892192895Sjamie if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS) 3893164032Srwatson return (0); 3894164032Srwatson else 3895164032Srwatson return (EPERM); 3896164032Srwatson 3897164032Srwatson /* 3898168396Spjd * Depending on the global setting, allow privilege of 3899168396Spjd * mounting/unmounting file systems. 3900168396Spjd */ 3901168396Spjd case PRIV_VFS_MOUNT: 3902168396Spjd case PRIV_VFS_UNMOUNT: 3903168396Spjd case PRIV_VFS_MOUNT_NONUSER: 3904168699Spjd case PRIV_VFS_MOUNT_OWNER: 3905224615Smm if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT && 3906224615Smm cred->cr_prison->pr_enforce_statfs < 2) 3907168396Spjd return (0); 3908168396Spjd else 3909168396Spjd return (EPERM); 3910168396Spjd 3911168396Spjd /* 3912168591Srwatson * Allow jailed root to bind reserved ports and reuse in-use 3913168591Srwatson * ports. 3914164032Srwatson */ 3915164032Srwatson case PRIV_NETINET_RESERVEDPORT: 3916168591Srwatson case PRIV_NETINET_REUSEPORT: 3917164032Srwatson return (0); 3918164032Srwatson 3919164032Srwatson /* 3920175630Sbz * Allow jailed root to set certian IPv4/6 (option) headers. 3921175630Sbz */ 3922175630Sbz case PRIV_NETINET_SETHDROPTS: 3923175630Sbz return (0); 3924175630Sbz 3925175630Sbz /* 3926164032Srwatson * Conditionally allow creating raw sockets in jail. 3927164032Srwatson */ 3928164032Srwatson case PRIV_NETINET_RAW: 3929192895Sjamie if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS) 3930164032Srwatson return (0); 3931164032Srwatson else 3932164032Srwatson return (EPERM); 3933164032Srwatson 3934164032Srwatson /* 3935164032Srwatson * Since jail implements its own visibility limits on netstat 3936164032Srwatson * sysctls, allow getcred. This allows identd to work in 3937164032Srwatson * jail. 3938164032Srwatson */ 3939164032Srwatson case PRIV_NETINET_GETCRED: 3940164032Srwatson return (0); 3941164032Srwatson 3942219304Strasz /* 3943219304Strasz * Allow jailed root to set loginclass. 3944219304Strasz */ 3945219304Strasz case PRIV_PROC_SETLOGINCLASS: 3946219304Strasz return (0); 3947219304Strasz 3948164032Srwatson default: 3949164032Srwatson /* 3950164032Srwatson * In all remaining cases, deny the privilege request. This 3951164032Srwatson * includes almost all network privileges, many system 3952164032Srwatson * configuration privileges. 3953164032Srwatson */ 3954164032Srwatson return (EPERM); 3955164032Srwatson } 3956164032Srwatson} 3957164032Srwatson 3958192895Sjamie/* 3959192895Sjamie * Return the part of pr2's name that is relative to pr1, or the whole name 3960192895Sjamie * if it does not directly follow. 3961192895Sjamie */ 3962192895Sjamie 3963192895Sjamiechar * 3964192895Sjamieprison_name(struct prison *pr1, struct prison *pr2) 3965192895Sjamie{ 3966192895Sjamie char *name; 3967192895Sjamie 3968192895Sjamie /* Jails see themselves as "0" (if they see themselves at all). */ 3969192895Sjamie if (pr1 == pr2) 3970192895Sjamie return "0"; 3971192895Sjamie name = pr2->pr_name; 3972192895Sjamie if (prison_ischild(pr1, pr2)) { 3973192895Sjamie /* 3974192895Sjamie * pr1 isn't locked (and allprison_lock may not be either) 3975192895Sjamie * so its length can't be counted on. But the number of dots 3976192895Sjamie * can be counted on - and counted. 3977192895Sjamie */ 3978192895Sjamie for (; pr1 != &prison0; pr1 = pr1->pr_parent) 3979192895Sjamie name = strchr(name, '.') + 1; 3980192895Sjamie } 3981192895Sjamie return (name); 3982192895Sjamie} 3983192895Sjamie 3984192895Sjamie/* 3985192895Sjamie * Return the part of pr2's path that is relative to pr1, or the whole path 3986192895Sjamie * if it does not directly follow. 3987192895Sjamie */ 3988192895Sjamiestatic char * 3989192895Sjamieprison_path(struct prison *pr1, struct prison *pr2) 3990192895Sjamie{ 3991192895Sjamie char *path1, *path2; 3992192895Sjamie int len1; 3993192895Sjamie 3994192895Sjamie path1 = pr1->pr_path; 3995192895Sjamie path2 = pr2->pr_path; 3996192895Sjamie if (!strcmp(path1, "/")) 3997192895Sjamie return (path2); 3998192895Sjamie len1 = strlen(path1); 3999192895Sjamie if (strncmp(path1, path2, len1)) 4000192895Sjamie return (path2); 4001192895Sjamie if (path2[len1] == '\0') 4002192895Sjamie return "/"; 4003192895Sjamie if (path2[len1] == '/') 4004192895Sjamie return (path2 + len1); 4005192895Sjamie return (path2); 4006192895Sjamie} 4007192895Sjamie 4008192895Sjamie 4009192895Sjamie/* 4010192895Sjamie * Jail-related sysctls. 4011192895Sjamie */ 4012227309Sedstatic SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0, 4013192895Sjamie "Jails"); 4014192895Sjamie 4015113275Smikestatic int 4016113275Smikesysctl_jail_list(SYSCTL_HANDLER_ARGS) 4017113275Smike{ 4018191673Sjamie struct xprison *xp; 4019192895Sjamie struct prison *pr, *cpr; 4020191673Sjamie#ifdef INET 4021191673Sjamie struct in_addr *ip4 = NULL; 4022191673Sjamie int ip4s = 0; 4023191673Sjamie#endif 4024191673Sjamie#ifdef INET6 4025208803Scperciva struct in6_addr *ip6 = NULL; 4026191673Sjamie int ip6s = 0; 4027191673Sjamie#endif 4028192895Sjamie int descend, error; 4029113275Smike 4030191673Sjamie xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK); 4031192895Sjamie pr = req->td->td_ucred->cr_prison; 4032191673Sjamie error = 0; 4033168401Spjd sx_slock(&allprison_lock); 4034192895Sjamie FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 4035192895Sjamie#if defined(INET) || defined(INET6) 4036191673Sjamie again: 4037192895Sjamie#endif 4038192895Sjamie mtx_lock(&cpr->pr_mtx); 4039185435Sbz#ifdef INET 4040192895Sjamie if (cpr->pr_ip4s > 0) { 4041192895Sjamie if (ip4s < cpr->pr_ip4s) { 4042192895Sjamie ip4s = cpr->pr_ip4s; 4043192895Sjamie mtx_unlock(&cpr->pr_mtx); 4044191673Sjamie ip4 = realloc(ip4, ip4s * 4045191673Sjamie sizeof(struct in_addr), M_TEMP, M_WAITOK); 4046191673Sjamie goto again; 4047191673Sjamie } 4048192895Sjamie bcopy(cpr->pr_ip4, ip4, 4049192895Sjamie cpr->pr_ip4s * sizeof(struct in_addr)); 4050191673Sjamie } 4051185435Sbz#endif 4052185435Sbz#ifdef INET6 4053192895Sjamie if (cpr->pr_ip6s > 0) { 4054192895Sjamie if (ip6s < cpr->pr_ip6s) { 4055192895Sjamie ip6s = cpr->pr_ip6s; 4056192895Sjamie mtx_unlock(&cpr->pr_mtx); 4057191673Sjamie ip6 = realloc(ip6, ip6s * 4058191673Sjamie sizeof(struct in6_addr), M_TEMP, M_WAITOK); 4059191673Sjamie goto again; 4060191673Sjamie } 4061192895Sjamie bcopy(cpr->pr_ip6, ip6, 4062192895Sjamie cpr->pr_ip6s * sizeof(struct in6_addr)); 4063191673Sjamie } 4064185435Sbz#endif 4065192895Sjamie if (cpr->pr_ref == 0) { 4066192895Sjamie mtx_unlock(&cpr->pr_mtx); 4067191673Sjamie continue; 4068191673Sjamie } 4069191673Sjamie bzero(xp, sizeof(*xp)); 4070113275Smike xp->pr_version = XPRISON_VERSION; 4071192895Sjamie xp->pr_id = cpr->pr_id; 4072192895Sjamie xp->pr_state = cpr->pr_uref > 0 4073191673Sjamie ? PRISON_STATE_ALIVE : PRISON_STATE_DYING; 4074192895Sjamie strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path)); 4075194118Sjamie strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host)); 4076192895Sjamie strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name)); 4077185435Sbz#ifdef INET 4078192895Sjamie xp->pr_ip4s = cpr->pr_ip4s; 4079185435Sbz#endif 4080185435Sbz#ifdef INET6 4081192895Sjamie xp->pr_ip6s = cpr->pr_ip6s; 4082185435Sbz#endif 4083192895Sjamie mtx_unlock(&cpr->pr_mtx); 4084191673Sjamie error = SYSCTL_OUT(req, xp, sizeof(*xp)); 4085191673Sjamie if (error) 4086191673Sjamie break; 4087185435Sbz#ifdef INET 4088191673Sjamie if (xp->pr_ip4s > 0) { 4089191673Sjamie error = SYSCTL_OUT(req, ip4, 4090191673Sjamie xp->pr_ip4s * sizeof(struct in_addr)); 4091191673Sjamie if (error) 4092191673Sjamie break; 4093185435Sbz } 4094185435Sbz#endif 4095185435Sbz#ifdef INET6 4096191673Sjamie if (xp->pr_ip6s > 0) { 4097191673Sjamie error = SYSCTL_OUT(req, ip6, 4098191673Sjamie xp->pr_ip6s * sizeof(struct in6_addr)); 4099191673Sjamie if (error) 4100191673Sjamie break; 4101185435Sbz } 4102185435Sbz#endif 4103113275Smike } 4104168401Spjd sx_sunlock(&allprison_lock); 4105191673Sjamie free(xp, M_TEMP); 4106191673Sjamie#ifdef INET 4107191673Sjamie free(ip4, M_TEMP); 4108191673Sjamie#endif 4109191673Sjamie#ifdef INET6 4110191673Sjamie free(ip6, M_TEMP); 4111191673Sjamie#endif 4112167354Spjd return (error); 4113113275Smike} 4114113275Smike 4115187864SedSYSCTL_OID(_security_jail, OID_AUTO, list, 4116187864Sed CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4117187864Sed sysctl_jail_list, "S", "List of active jails"); 4118126004Spjd 4119126004Spjdstatic int 4120126004Spjdsysctl_jail_jailed(SYSCTL_HANDLER_ARGS) 4121126004Spjd{ 4122126004Spjd int error, injail; 4123126004Spjd 4124126004Spjd injail = jailed(req->td->td_ucred); 4125126004Spjd error = SYSCTL_OUT(req, &injail, sizeof(injail)); 4126126004Spjd 4127126004Spjd return (error); 4128126004Spjd} 4129192895Sjamie 4130187864SedSYSCTL_PROC(_security_jail, OID_AUTO, jailed, 4131187864Sed CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4132187864Sed sysctl_jail_jailed, "I", "Process in jail?"); 4133185435Sbz 4134192895Sjamie#if defined(INET) || defined(INET6) 4135193865SjamieSYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW, 4136192895Sjamie &jail_max_af_ips, 0, 4137192895Sjamie "Number of IP addresses a jail may have at most per address family"); 4138192895Sjamie#endif 4139192895Sjamie 4140192895Sjamie/* 4141192895Sjamie * Default parameters for jail(2) compatability. For historical reasons, 4142192895Sjamie * the sysctl names have varying similarity to the parameter names. Prisons 4143192895Sjamie * just see their own parameters, and can't change them. 4144192895Sjamie */ 4145192895Sjamiestatic int 4146192895Sjamiesysctl_jail_default_allow(SYSCTL_HANDLER_ARGS) 4147192895Sjamie{ 4148192895Sjamie struct prison *pr; 4149192895Sjamie int allow, error, i; 4150192895Sjamie 4151192895Sjamie pr = req->td->td_ucred->cr_prison; 4152192895Sjamie allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow; 4153192895Sjamie 4154192895Sjamie /* Get the current flag value, and convert it to a boolean. */ 4155192895Sjamie i = (allow & arg2) ? 1 : 0; 4156192895Sjamie if (arg1 != NULL) 4157192895Sjamie i = !i; 4158192895Sjamie error = sysctl_handle_int(oidp, &i, 0, req); 4159192895Sjamie if (error || !req->newptr) 4160192895Sjamie return (error); 4161192895Sjamie i = i ? arg2 : 0; 4162192895Sjamie if (arg1 != NULL) 4163192895Sjamie i ^= arg2; 4164192895Sjamie /* 4165192895Sjamie * The sysctls don't have CTLFLAGS_PRISON, so assume prison0 4166192895Sjamie * for writing. 4167192895Sjamie */ 4168192895Sjamie mtx_lock(&prison0.pr_mtx); 4169192895Sjamie jail_default_allow = (jail_default_allow & ~arg2) | i; 4170192895Sjamie mtx_unlock(&prison0.pr_mtx); 4171192895Sjamie return (0); 4172192895Sjamie} 4173192895Sjamie 4174192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed, 4175192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4176192895Sjamie NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I", 4177192895Sjamie "Processes in jail can set their hostnames"); 4178192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only, 4179192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4180192895Sjamie (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I", 4181192895Sjamie "Processes in jail are limited to creating UNIX/IP/route sockets only"); 4182192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed, 4183192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4184192895Sjamie NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I", 4185192895Sjamie "Processes in jail can use System V IPC primitives"); 4186192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets, 4187192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4188192895Sjamie NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I", 4189192895Sjamie "Prison root can create raw sockets"); 4190192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed, 4191192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4192192895Sjamie NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I", 4193192895Sjamie "Processes in jail can alter system file flags"); 4194192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed, 4195192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4196192895Sjamie NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I", 4197192895Sjamie "Processes in jail can mount/unmount jail-friendly file systems"); 4198192895Sjamie 4199192895Sjamiestatic int 4200192895Sjamiesysctl_jail_default_level(SYSCTL_HANDLER_ARGS) 4201192895Sjamie{ 4202192895Sjamie struct prison *pr; 4203192895Sjamie int level, error; 4204192895Sjamie 4205192895Sjamie pr = req->td->td_ucred->cr_prison; 4206192895Sjamie level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2); 4207192895Sjamie error = sysctl_handle_int(oidp, &level, 0, req); 4208192895Sjamie if (error || !req->newptr) 4209192895Sjamie return (error); 4210192895Sjamie *(int *)arg1 = level; 4211192895Sjamie return (0); 4212192895Sjamie} 4213192895Sjamie 4214192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs, 4215192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4216192895Sjamie &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs), 4217192895Sjamie sysctl_jail_default_level, "I", 4218192895Sjamie "Processes in jail cannot see all mounted file systems"); 4219192895Sjamie 4220231267SmmSYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset, 4221231267Smm CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 4222231267Smm &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum), 4223231267Smm sysctl_jail_default_level, "I", 4224231267Smm "Ruleset for the devfs filesystem in jail"); 4225231267Smm 4226192895Sjamie/* 4227192895Sjamie * Nodes to describe jail parameters. Maximum length of string parameters 4228192895Sjamie * is returned in the string itself, and the other parameters exist merely 4229192895Sjamie * to make themselves and their types known. 4230192895Sjamie */ 4231192895SjamieSYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0, 4232192895Sjamie "Jail parameters"); 4233192895Sjamie 4234192895Sjamieint 4235192895Sjamiesysctl_jail_param(SYSCTL_HANDLER_ARGS) 4236192895Sjamie{ 4237192895Sjamie int i; 4238192895Sjamie long l; 4239192895Sjamie size_t s; 4240192895Sjamie char numbuf[12]; 4241192895Sjamie 4242192895Sjamie switch (oidp->oid_kind & CTLTYPE) 4243192895Sjamie { 4244192895Sjamie case CTLTYPE_LONG: 4245192895Sjamie case CTLTYPE_ULONG: 4246192895Sjamie l = 0; 4247192895Sjamie#ifdef SCTL_MASK32 4248192895Sjamie if (!(req->flags & SCTL_MASK32)) 4249192895Sjamie#endif 4250192895Sjamie return (SYSCTL_OUT(req, &l, sizeof(l))); 4251192895Sjamie case CTLTYPE_INT: 4252192895Sjamie case CTLTYPE_UINT: 4253192895Sjamie i = 0; 4254192895Sjamie return (SYSCTL_OUT(req, &i, sizeof(i))); 4255192895Sjamie case CTLTYPE_STRING: 4256219819Sjeff snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2); 4257192895Sjamie return 4258192895Sjamie (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req)); 4259192895Sjamie case CTLTYPE_STRUCT: 4260192895Sjamie s = (size_t)arg2; 4261192895Sjamie return (SYSCTL_OUT(req, &s, sizeof(s))); 4262192895Sjamie } 4263192895Sjamie return (0); 4264192895Sjamie} 4265192895Sjamie 4266192895SjamieSYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID"); 4267192895SjamieSYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID"); 4268192895SjamieSYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name"); 4269192895SjamieSYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path"); 4270192895SjamieSYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW, 4271192895Sjamie "I", "Jail secure level"); 4272192895SjamieSYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW, 4273192895Sjamie "I", "Jail cannot see all mounted file systems"); 4274231267SmmSYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW, 4275231267Smm "I", "Ruleset for in-jail devfs mounts"); 4276192895SjamieSYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW, 4277192895Sjamie "B", "Jail persistence"); 4278194251Sjamie#ifdef VIMAGE 4279194251SjamieSYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN, 4280195870Sjamie "E,jailsys", "Virtual network stack"); 4281194251Sjamie#endif 4282192895SjamieSYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD, 4283192895Sjamie "B", "Jail is in the process of shutting down"); 4284192895Sjamie 4285194762SjamieSYSCTL_JAIL_PARAM_NODE(children, "Number of child jails"); 4286194762SjamieSYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD, 4287194762Sjamie "I", "Current number of child jails"); 4288194762SjamieSYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW, 4289194762Sjamie "I", "Maximum number of child jails"); 4290194762Sjamie 4291195870SjamieSYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info"); 4292192895SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN, 4293192895Sjamie "Jail hostname"); 4294193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN, 4295193066Sjamie "Jail NIS domainname"); 4296193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN, 4297193066Sjamie "Jail host UUID"); 4298193066SjamieSYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW, 4299193066Sjamie "LU", "Jail host ID"); 4300192895Sjamie 4301192895SjamieSYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset"); 4302192895SjamieSYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID"); 4303192895Sjamie 4304192895Sjamie#ifdef INET 4305195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN, 4306195974Sjamie "Jail IPv4 address virtualization"); 4307192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr), 4308192895Sjamie "S,in_addr,a", "Jail IPv4 addresses"); 4309202468SbzSYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4310202468Sbz "B", "Do (not) use IPv4 source address selection rather than the " 4311202468Sbz "primary jail IPv4 address."); 4312192895Sjamie#endif 4313192895Sjamie#ifdef INET6 4314195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN, 4315195974Sjamie "Jail IPv6 address virtualization"); 4316192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr), 4317192895Sjamie "S,in6_addr,a", "Jail IPv6 addresses"); 4318202468SbzSYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4319202468Sbz "B", "Do (not) use IPv6 source address selection rather than the " 4320202468Sbz "primary jail IPv6 address."); 4321192895Sjamie#endif 4322192895Sjamie 4323192895SjamieSYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags"); 4324192895SjamieSYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW, 4325192895Sjamie "B", "Jail may set hostname"); 4326192895SjamieSYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW, 4327192895Sjamie "B", "Jail may use SYSV IPC"); 4328192895SjamieSYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW, 4329192895Sjamie "B", "Jail may create raw sockets"); 4330192895SjamieSYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW, 4331192895Sjamie "B", "Jail may alter system file flags"); 4332192895SjamieSYSCTL_JAIL_PARAM(_allow, mount, CTLTYPE_INT | CTLFLAG_RW, 4333192895Sjamie "B", "Jail may mount/unmount jail-friendly file systems"); 4334192895SjamieSYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW, 4335192895Sjamie "B", "Jail may set file quotas"); 4336192895SjamieSYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW, 4337192895Sjamie "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route"); 4338192895Sjamie 4339220137Straszvoid 4340220137Straszprison_racct_foreach(void (*callback)(struct racct *racct, 4341220137Strasz void *arg2, void *arg3), void *arg2, void *arg3) 4342220137Strasz{ 4343221362Strasz struct prison_racct *prr; 4344192895Sjamie 4345220137Strasz sx_slock(&allprison_lock); 4346221362Strasz LIST_FOREACH(prr, &allprison_racct, prr_next) 4347221362Strasz (callback)(prr->prr_racct, arg2, arg3); 4348220137Strasz sx_sunlock(&allprison_lock); 4349220137Strasz} 4350220137Strasz 4351221362Straszstatic struct prison_racct * 4352221362Straszprison_racct_find_locked(const char *name) 4353221362Strasz{ 4354221362Strasz struct prison_racct *prr; 4355221362Strasz 4356221362Strasz sx_assert(&allprison_lock, SA_XLOCKED); 4357221362Strasz 4358221362Strasz if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN) 4359221362Strasz return (NULL); 4360221362Strasz 4361221362Strasz LIST_FOREACH(prr, &allprison_racct, prr_next) { 4362221362Strasz if (strcmp(name, prr->prr_name) != 0) 4363221362Strasz continue; 4364221362Strasz 4365221362Strasz /* Found prison_racct with a matching name? */ 4366221362Strasz prison_racct_hold(prr); 4367221362Strasz return (prr); 4368221362Strasz } 4369221362Strasz 4370221362Strasz /* Add new prison_racct. */ 4371221362Strasz prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK); 4372221362Strasz racct_create(&prr->prr_racct); 4373221362Strasz 4374221362Strasz strcpy(prr->prr_name, name); 4375221362Strasz refcount_init(&prr->prr_refcount, 1); 4376221362Strasz LIST_INSERT_HEAD(&allprison_racct, prr, prr_next); 4377221362Strasz 4378221362Strasz return (prr); 4379221362Strasz} 4380221362Strasz 4381221362Straszstruct prison_racct * 4382221362Straszprison_racct_find(const char *name) 4383221362Strasz{ 4384221362Strasz struct prison_racct *prr; 4385221362Strasz 4386221362Strasz sx_xlock(&allprison_lock); 4387221362Strasz prr = prison_racct_find_locked(name); 4388221362Strasz sx_xunlock(&allprison_lock); 4389221362Strasz return (prr); 4390221362Strasz} 4391221362Strasz 4392221362Straszvoid 4393221362Straszprison_racct_hold(struct prison_racct *prr) 4394221362Strasz{ 4395221362Strasz 4396221362Strasz refcount_acquire(&prr->prr_refcount); 4397221362Strasz} 4398221362Strasz 4399221362Straszvoid 4400221362Straszprison_racct_free(struct prison_racct *prr) 4401221362Strasz{ 4402221362Strasz int old; 4403221362Strasz 4404221362Strasz old = prr->prr_refcount; 4405221362Strasz if (old > 1 && atomic_cmpset_int(&prr->prr_refcount, old, old - 1)) 4406221362Strasz return; 4407221362Strasz 4408221362Strasz sx_xlock(&allprison_lock); 4409221362Strasz if (refcount_release(&prr->prr_refcount)) { 4410221362Strasz racct_destroy(&prr->prr_racct); 4411221362Strasz LIST_REMOVE(prr, prr_next); 4412221362Strasz sx_xunlock(&allprison_lock); 4413221362Strasz free(prr, M_PRISON_RACCT); 4414221362Strasz 4415221362Strasz return; 4416221362Strasz } 4417221362Strasz sx_xunlock(&allprison_lock); 4418221362Strasz} 4419221362Strasz 4420221362Strasz#ifdef RACCT 4421221362Straszstatic void 4422221362Straszprison_racct_attach(struct prison *pr) 4423221362Strasz{ 4424221362Strasz struct prison_racct *prr; 4425221362Strasz 4426221362Strasz prr = prison_racct_find_locked(pr->pr_name); 4427221362Strasz KASSERT(prr != NULL, ("cannot find prison_racct")); 4428221362Strasz 4429221362Strasz pr->pr_prison_racct = prr; 4430221362Strasz} 4431221362Strasz 4432221362Straszstatic void 4433221362Straszprison_racct_detach(struct prison *pr) 4434221362Strasz{ 4435221362Strasz prison_racct_free(pr->pr_prison_racct); 4436221362Strasz pr->pr_prison_racct = NULL; 4437221362Strasz} 4438221362Strasz#endif /* RACCT */ 4439221362Strasz 4440185435Sbz#ifdef DDB 4441191673Sjamie 4442191673Sjamiestatic void 4443191673Sjamiedb_show_prison(struct prison *pr) 4444185435Sbz{ 4445192895Sjamie int fi; 4446191673Sjamie#if defined(INET) || defined(INET6) 4447191673Sjamie int ii; 4448185435Sbz#endif 4449195870Sjamie unsigned jsf; 4450185435Sbz#ifdef INET6 4451185435Sbz char ip6buf[INET6_ADDRSTRLEN]; 4452185435Sbz#endif 4453185435Sbz 4454191673Sjamie db_printf("prison %p:\n", pr); 4455191673Sjamie db_printf(" jid = %d\n", pr->pr_id); 4456191673Sjamie db_printf(" name = %s\n", pr->pr_name); 4457192895Sjamie db_printf(" parent = %p\n", pr->pr_parent); 4458191673Sjamie db_printf(" ref = %d\n", pr->pr_ref); 4459191673Sjamie db_printf(" uref = %d\n", pr->pr_uref); 4460191673Sjamie db_printf(" path = %s\n", pr->pr_path); 4461191673Sjamie db_printf(" cpuset = %d\n", pr->pr_cpuset 4462191673Sjamie ? pr->pr_cpuset->cs_id : -1); 4463194251Sjamie#ifdef VIMAGE 4464194251Sjamie db_printf(" vnet = %p\n", pr->pr_vnet); 4465194251Sjamie#endif 4466191673Sjamie db_printf(" root = %p\n", pr->pr_root); 4467191673Sjamie db_printf(" securelevel = %d\n", pr->pr_securelevel); 4468231267Smm db_printf(" devfs_rsnum = %d\n", pr->pr_devfs_rsnum); 4469202123Sbz db_printf(" children.max = %d\n", pr->pr_childmax); 4470202123Sbz db_printf(" children.cur = %d\n", pr->pr_childcount); 4471192895Sjamie db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children)); 4472192895Sjamie db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling)); 4473202123Sbz db_printf(" flags = 0x%x", pr->pr_flags); 4474192895Sjamie for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 4475192895Sjamie fi++) 4476192895Sjamie if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi))) 4477192895Sjamie db_printf(" %s", pr_flag_names[fi]); 4478195870Sjamie for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 4479195870Sjamie fi++) { 4480195870Sjamie jsf = pr->pr_flags & 4481195870Sjamie (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new); 4482195870Sjamie db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name, 4483195870Sjamie pr_flag_jailsys[fi].disable && 4484195870Sjamie (jsf == pr_flag_jailsys[fi].disable) ? "disable" 4485195870Sjamie : (jsf == pr_flag_jailsys[fi].new) ? "new" 4486195870Sjamie : "inherit"); 4487195870Sjamie } 4488202123Sbz db_printf(" allow = 0x%x", pr->pr_allow); 4489192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 4490192895Sjamie fi++) 4491192895Sjamie if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi))) 4492192895Sjamie db_printf(" %s", pr_allow_names[fi]); 4493191673Sjamie db_printf("\n"); 4494192895Sjamie db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs); 4495194118Sjamie db_printf(" host.hostname = %s\n", pr->pr_hostname); 4496194118Sjamie db_printf(" host.domainname = %s\n", pr->pr_domainname); 4497194118Sjamie db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid); 4498193066Sjamie db_printf(" host.hostid = %lu\n", pr->pr_hostid); 4499185435Sbz#ifdef INET 4500191673Sjamie db_printf(" ip4s = %d\n", pr->pr_ip4s); 4501191673Sjamie for (ii = 0; ii < pr->pr_ip4s; ii++) 4502191673Sjamie db_printf(" %s %s\n", 4503202123Sbz ii == 0 ? "ip4.addr =" : " ", 4504191673Sjamie inet_ntoa(pr->pr_ip4[ii])); 4505185435Sbz#endif 4506185435Sbz#ifdef INET6 4507191673Sjamie db_printf(" ip6s = %d\n", pr->pr_ip6s); 4508191673Sjamie for (ii = 0; ii < pr->pr_ip6s; ii++) 4509191673Sjamie db_printf(" %s %s\n", 4510202123Sbz ii == 0 ? "ip6.addr =" : " ", 4511191673Sjamie ip6_sprintf(ip6buf, &pr->pr_ip6[ii])); 4512191673Sjamie#endif 4513191673Sjamie} 4514191673Sjamie 4515191673SjamieDB_SHOW_COMMAND(prison, db_show_prison_command) 4516191673Sjamie{ 4517191673Sjamie struct prison *pr; 4518191673Sjamie 4519191673Sjamie if (!have_addr) { 4520192895Sjamie /* 4521192895Sjamie * Show all prisons in the list, and prison0 which is not 4522192895Sjamie * listed. 4523192895Sjamie */ 4524192895Sjamie db_show_prison(&prison0); 4525192895Sjamie if (!db_pager_quit) { 4526192895Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) { 4527192895Sjamie db_show_prison(pr); 4528192895Sjamie if (db_pager_quit) 4529192895Sjamie break; 4530192895Sjamie } 4531191673Sjamie } 4532191673Sjamie return; 4533191673Sjamie } 4534191673Sjamie 4535192895Sjamie if (addr == 0) 4536192895Sjamie pr = &prison0; 4537192895Sjamie else { 4538192895Sjamie /* Look for a prison with the ID and with references. */ 4539191673Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) 4540192895Sjamie if (pr->pr_id == addr && pr->pr_ref > 0) 4541191673Sjamie break; 4542192895Sjamie if (pr == NULL) 4543192895Sjamie /* Look again, without requiring a reference. */ 4544192895Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) 4545192895Sjamie if (pr->pr_id == addr) 4546192895Sjamie break; 4547192895Sjamie if (pr == NULL) 4548192895Sjamie /* Assume address points to a valid prison. */ 4549192895Sjamie pr = (struct prison *)addr; 4550192895Sjamie } 4551191673Sjamie db_show_prison(pr); 4552185435Sbz} 4553191673Sjamie 4554185435Sbz#endif /* DDB */ 4555