kern_jail.c revision 195974
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 195974 2009-07-30 14:28:56Z jamie $"); 31116182Sobrien 32193066Sjamie#include "opt_compat.h" 33185435Sbz#include "opt_ddb.h" 34185435Sbz#include "opt_inet.h" 35185435Sbz#include "opt_inet6.h" 36131177Spjd 3746155Sphk#include <sys/param.h> 3846155Sphk#include <sys/types.h> 3946155Sphk#include <sys/kernel.h> 4046155Sphk#include <sys/systm.h> 4146155Sphk#include <sys/errno.h> 4246155Sphk#include <sys/sysproto.h> 4346155Sphk#include <sys/malloc.h> 44192895Sjamie#include <sys/osd.h> 45164032Srwatson#include <sys/priv.h> 4646155Sphk#include <sys/proc.h> 47124882Srwatson#include <sys/taskqueue.h> 48177785Skib#include <sys/fcntl.h> 4946155Sphk#include <sys/jail.h> 5087275Srwatson#include <sys/lock.h> 5187275Srwatson#include <sys/mutex.h> 52168401Spjd#include <sys/sx.h> 53193066Sjamie#include <sys/sysent.h> 54113275Smike#include <sys/namei.h> 55147185Spjd#include <sys/mount.h> 56113275Smike#include <sys/queue.h> 5746155Sphk#include <sys/socket.h> 58113275Smike#include <sys/syscallsubr.h> 5957163Srwatson#include <sys/sysctl.h> 60113275Smike#include <sys/vnode.h> 61181803Sbz#include <sys/vimage.h> 6246155Sphk#include <net/if.h> 6346155Sphk#include <netinet/in.h> 64185435Sbz#ifdef DDB 65185435Sbz#include <ddb/ddb.h> 66185435Sbz#ifdef INET6 67185435Sbz#include <netinet6/in6_var.h> 68185435Sbz#endif /* INET6 */ 69185435Sbz#endif /* DDB */ 7046155Sphk 71163606Srwatson#include <security/mac/mac_framework.h> 72163606Srwatson 73195944Sjamie#define DEFAULT_HOSTUUID "00000000-0000-0000-0000-000000000000" 74195944Sjamie 7546155SphkMALLOC_DEFINE(M_PRISON, "prison", "Prison structures"); 7646155Sphk 77192895Sjamie/* prison0 describes what is "real" about the system. */ 78192895Sjamiestruct prison prison0 = { 79192895Sjamie .pr_id = 0, 80192895Sjamie .pr_name = "0", 81192895Sjamie .pr_ref = 1, 82192895Sjamie .pr_uref = 1, 83192895Sjamie .pr_path = "/", 84192895Sjamie .pr_securelevel = -1, 85194762Sjamie .pr_childmax = JAIL_MAX, 86195944Sjamie .pr_hostuuid = DEFAULT_HOSTUUID, 87192895Sjamie .pr_children = LIST_HEAD_INITIALIZER(&prison0.pr_children), 88193066Sjamie .pr_flags = PR_HOST, 89192895Sjamie .pr_allow = PR_ALLOW_ALL, 90192895Sjamie}; 91192895SjamieMTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF); 9257163Srwatson 93192895Sjamie/* allprison and lastprid are protected by allprison_lock. */ 94168401Spjdstruct sx allprison_lock; 95191673SjamieSX_SYSINIT(allprison_lock, &allprison_lock, "allprison"); 96191673Sjamiestruct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison); 97179881Sdelphijint lastprid = 0; 98113275Smike 99191673Sjamiestatic int do_jail_attach(struct thread *td, struct prison *pr); 100190466Sjamiestatic void prison_complete(void *context, int pending); 101191673Sjamiestatic void prison_deref(struct prison *pr, int flags); 102192895Sjamiestatic char *prison_path(struct prison *pr1, struct prison *pr2); 103192895Sjamiestatic void prison_remove_one(struct prison *pr); 104185435Sbz#ifdef INET 105190466Sjamiestatic int _prison_check_ip4(struct prison *pr, struct in_addr *ia); 106192895Sjamiestatic int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4); 107185435Sbz#endif 108185435Sbz#ifdef INET6 109190466Sjamiestatic int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6); 110192895Sjamiestatic int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6); 111185435Sbz#endif 112113275Smike 113191673Sjamie/* Flags for prison_deref */ 114191673Sjamie#define PD_DEREF 0x01 115191673Sjamie#define PD_DEUREF 0x02 116191673Sjamie#define PD_LOCKED 0x04 117191673Sjamie#define PD_LIST_SLOCKED 0x08 118191673Sjamie#define PD_LIST_XLOCKED 0x10 119113275Smike 120192895Sjamie/* 121192895Sjamie * Parameter names corresponding to PR_* flag values 122192895Sjamie */ 123192895Sjamiestatic char *pr_flag_names[] = { 124192895Sjamie [0] = "persist", 125192895Sjamie}; 126192895Sjamie 127192895Sjamiestatic char *pr_flag_nonames[] = { 128192895Sjamie [0] = "nopersist", 129195870Sjamie}; 130195870Sjamie 131195870Sjamiestruct jailsys_flags { 132195870Sjamie const char *name; 133195870Sjamie unsigned disable; 134195870Sjamie unsigned new; 135195870Sjamie} pr_flag_jailsys[] = { 136195870Sjamie { "host", 0, PR_HOST }, 137195870Sjamie#ifdef VIMAGE 138195870Sjamie { "vnet", 0, PR_VNET }, 139195870Sjamie#endif 140192895Sjamie#ifdef INET 141195870Sjamie { "ip4", PR_IP4_USER | PR_IP4_DISABLE, PR_IP4_USER }, 142192895Sjamie#endif 143192895Sjamie#ifdef INET6 144195870Sjamie { "ip6", PR_IP6_USER | PR_IP6_DISABLE, PR_IP6_USER }, 145192895Sjamie#endif 146192895Sjamie}; 147192895Sjamie 148192895Sjamiestatic char *pr_allow_names[] = { 149192895Sjamie "allow.set_hostname", 150192895Sjamie "allow.sysvipc", 151192895Sjamie "allow.raw_sockets", 152192895Sjamie "allow.chflags", 153192895Sjamie "allow.mount", 154192895Sjamie "allow.quotas", 155192895Sjamie "allow.socket_af", 156192895Sjamie}; 157192895Sjamie 158192895Sjamiestatic char *pr_allow_nonames[] = { 159192895Sjamie "allow.noset_hostname", 160192895Sjamie "allow.nosysvipc", 161192895Sjamie "allow.noraw_sockets", 162192895Sjamie "allow.nochflags", 163192895Sjamie "allow.nomount", 164192895Sjamie "allow.noquotas", 165192895Sjamie "allow.nosocket_af", 166192895Sjamie}; 167192895Sjamie 168192895Sjamie#define JAIL_DEFAULT_ALLOW PR_ALLOW_SET_HOSTNAME 169192895Sjamiestatic unsigned jail_default_allow = JAIL_DEFAULT_ALLOW; 170192895Sjamiestatic int jail_default_enforce_statfs = 2; 171192895Sjamie#if defined(INET) || defined(INET6) 172193865Sjamiestatic unsigned jail_max_af_ips = 255; 173192895Sjamie#endif 174192895Sjamie 175192895Sjamie#ifdef INET 176185435Sbzstatic int 177185435Sbzqcmp_v4(const void *ip1, const void *ip2) 178185435Sbz{ 179185435Sbz in_addr_t iaa, iab; 180185435Sbz 181185435Sbz /* 182185435Sbz * We need to compare in HBO here to get the list sorted as expected 183185435Sbz * by the result of the code. Sorting NBO addresses gives you 184185435Sbz * interesting results. If you do not understand, do not try. 185185435Sbz */ 186185435Sbz iaa = ntohl(((const struct in_addr *)ip1)->s_addr); 187185435Sbz iab = ntohl(((const struct in_addr *)ip2)->s_addr); 188185435Sbz 189185435Sbz /* 190185435Sbz * Do not simply return the difference of the two numbers, the int is 191185435Sbz * not wide enough. 192185435Sbz */ 193185435Sbz if (iaa > iab) 194185435Sbz return (1); 195185435Sbz else if (iaa < iab) 196185435Sbz return (-1); 197185435Sbz else 198185435Sbz return (0); 199185435Sbz} 200185435Sbz#endif 201185435Sbz 202185435Sbz#ifdef INET6 203185435Sbzstatic int 204185435Sbzqcmp_v6(const void *ip1, const void *ip2) 205185435Sbz{ 206185435Sbz const struct in6_addr *ia6a, *ia6b; 207185435Sbz int i, rc; 208185435Sbz 209185435Sbz ia6a = (const struct in6_addr *)ip1; 210185435Sbz ia6b = (const struct in6_addr *)ip2; 211185435Sbz 212185435Sbz rc = 0; 213190466Sjamie for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) { 214185435Sbz if (ia6a->s6_addr[i] > ia6b->s6_addr[i]) 215185435Sbz rc = 1; 216185435Sbz else if (ia6a->s6_addr[i] < ia6b->s6_addr[i]) 217185435Sbz rc = -1; 218185435Sbz } 219185435Sbz return (rc); 220185435Sbz} 221185435Sbz#endif 222185435Sbz 223191673Sjamie/* 224191673Sjamie * struct jail_args { 225191673Sjamie * struct jail *jail; 226191673Sjamie * }; 227191673Sjamie */ 228191673Sjamieint 229191673Sjamiejail(struct thread *td, struct jail_args *uap) 230185435Sbz{ 231191673Sjamie uint32_t version; 232191673Sjamie int error; 233192895Sjamie struct jail j; 234185435Sbz 235191673Sjamie error = copyin(uap->jail, &version, sizeof(uint32_t)); 236191673Sjamie if (error) 237191673Sjamie return (error); 238185435Sbz 239191673Sjamie switch (version) { 240191673Sjamie case 0: 241191673Sjamie { 242191673Sjamie struct jail_v0 j0; 243185435Sbz 244192895Sjamie /* FreeBSD single IPv4 jails. */ 245192895Sjamie bzero(&j, sizeof(struct jail)); 246191673Sjamie error = copyin(uap->jail, &j0, sizeof(struct jail_v0)); 247191673Sjamie if (error) 248191673Sjamie return (error); 249192895Sjamie j.version = j0.version; 250192895Sjamie j.path = j0.path; 251192895Sjamie j.hostname = j0.hostname; 252192895Sjamie j.ip4s = j0.ip_number; 253191673Sjamie break; 254191673Sjamie } 255191673Sjamie 256191673Sjamie case 1: 257185435Sbz /* 258191673Sjamie * Version 1 was used by multi-IPv4 jail implementations 259191673Sjamie * that never made it into the official kernel. 260185435Sbz */ 261191673Sjamie return (EINVAL); 262185435Sbz 263191673Sjamie case 2: /* JAIL_API_VERSION */ 264191673Sjamie /* FreeBSD multi-IPv4/IPv6,noIP jails. */ 265191673Sjamie error = copyin(uap->jail, &j, sizeof(struct jail)); 266191673Sjamie if (error) 267191673Sjamie return (error); 268192895Sjamie break; 269192895Sjamie 270192895Sjamie default: 271192895Sjamie /* Sci-Fi jails are not supported, sorry. */ 272192895Sjamie return (EINVAL); 273192895Sjamie } 274192895Sjamie return (kern_jail(td, &j)); 275192895Sjamie} 276192895Sjamie 277192895Sjamieint 278192895Sjamiekern_jail(struct thread *td, struct jail *j) 279192895Sjamie{ 280193865Sjamie struct iovec optiov[2 * (4 281193865Sjamie + sizeof(pr_allow_names) / sizeof(pr_allow_names[0]) 282193865Sjamie#ifdef INET 283193865Sjamie + 1 284193865Sjamie#endif 285193865Sjamie#ifdef INET6 286193865Sjamie + 1 287193865Sjamie#endif 288193865Sjamie )]; 289192895Sjamie struct uio opt; 290192895Sjamie char *u_path, *u_hostname, *u_name; 291185435Sbz#ifdef INET 292193865Sjamie uint32_t ip4s; 293192895Sjamie struct in_addr *u_ip4; 294192895Sjamie#endif 295192895Sjamie#ifdef INET6 296192895Sjamie struct in6_addr *u_ip6; 297192895Sjamie#endif 298192895Sjamie size_t tmplen; 299192895Sjamie int error, enforce_statfs, fi; 300192895Sjamie 301192895Sjamie bzero(&optiov, sizeof(optiov)); 302192895Sjamie opt.uio_iov = optiov; 303192895Sjamie opt.uio_iovcnt = 0; 304192895Sjamie opt.uio_offset = -1; 305192895Sjamie opt.uio_resid = -1; 306192895Sjamie opt.uio_segflg = UIO_SYSSPACE; 307192895Sjamie opt.uio_rw = UIO_READ; 308192895Sjamie opt.uio_td = td; 309192895Sjamie 310192895Sjamie /* Set permissions for top-level jails from sysctls. */ 311192895Sjamie if (!jailed(td->td_ucred)) { 312192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / 313192895Sjamie sizeof(pr_allow_names[0]); fi++) { 314192895Sjamie optiov[opt.uio_iovcnt].iov_base = 315192895Sjamie (jail_default_allow & (1 << fi)) 316192895Sjamie ? pr_allow_names[fi] : pr_allow_nonames[fi]; 317192895Sjamie optiov[opt.uio_iovcnt].iov_len = 318192895Sjamie strlen(optiov[opt.uio_iovcnt].iov_base) + 1; 319192895Sjamie opt.uio_iovcnt += 2; 320192895Sjamie } 321192895Sjamie optiov[opt.uio_iovcnt].iov_base = "enforce_statfs"; 322192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs"); 323192895Sjamie opt.uio_iovcnt++; 324192895Sjamie enforce_statfs = jail_default_enforce_statfs; 325192895Sjamie optiov[opt.uio_iovcnt].iov_base = &enforce_statfs; 326192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs); 327192895Sjamie opt.uio_iovcnt++; 328192895Sjamie } 329192895Sjamie 330192895Sjamie tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN; 331192895Sjamie#ifdef INET 332192895Sjamie ip4s = (j->version == 0) ? 1 : j->ip4s; 333192895Sjamie if (ip4s > jail_max_af_ips) 334192895Sjamie return (EINVAL); 335192895Sjamie tmplen += ip4s * sizeof(struct in_addr); 336191673Sjamie#else 337192895Sjamie if (j->ip4s > 0) 338192895Sjamie return (EINVAL); 339191673Sjamie#endif 340191673Sjamie#ifdef INET6 341192895Sjamie if (j->ip6s > jail_max_af_ips) 342192895Sjamie return (EINVAL); 343192895Sjamie tmplen += j->ip6s * sizeof(struct in6_addr); 344191673Sjamie#else 345192895Sjamie if (j->ip6s > 0) 346192895Sjamie return (EINVAL); 347191673Sjamie#endif 348192895Sjamie u_path = malloc(tmplen, M_TEMP, M_WAITOK); 349192895Sjamie u_hostname = u_path + MAXPATHLEN; 350192895Sjamie u_name = u_hostname + MAXHOSTNAMELEN; 351191673Sjamie#ifdef INET 352192895Sjamie u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN); 353191673Sjamie#endif 354191673Sjamie#ifdef INET6 355191673Sjamie#ifdef INET 356192895Sjamie u_ip6 = (struct in6_addr *)(u_ip4 + ip4s); 357191673Sjamie#else 358192895Sjamie u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN); 359191673Sjamie#endif 360191673Sjamie#endif 361192895Sjamie optiov[opt.uio_iovcnt].iov_base = "path"; 362192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("path"); 363192895Sjamie opt.uio_iovcnt++; 364192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_path; 365192895Sjamie error = copyinstr(j->path, u_path, MAXPATHLEN, 366192895Sjamie &optiov[opt.uio_iovcnt].iov_len); 367192895Sjamie if (error) { 368192895Sjamie free(u_path, M_TEMP); 369192895Sjamie return (error); 370192895Sjamie } 371192895Sjamie opt.uio_iovcnt++; 372192895Sjamie optiov[opt.uio_iovcnt].iov_base = "host.hostname"; 373192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname"); 374192895Sjamie opt.uio_iovcnt++; 375192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_hostname; 376192895Sjamie error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN, 377192895Sjamie &optiov[opt.uio_iovcnt].iov_len); 378192895Sjamie if (error) { 379192895Sjamie free(u_path, M_TEMP); 380192895Sjamie return (error); 381192895Sjamie } 382192895Sjamie opt.uio_iovcnt++; 383192895Sjamie if (j->jailname != NULL) { 384192895Sjamie optiov[opt.uio_iovcnt].iov_base = "name"; 385192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("name"); 386192895Sjamie opt.uio_iovcnt++; 387192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_name; 388192895Sjamie error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN, 389192895Sjamie &optiov[opt.uio_iovcnt].iov_len); 390191673Sjamie if (error) { 391191673Sjamie free(u_path, M_TEMP); 392191673Sjamie return (error); 393191673Sjamie } 394192895Sjamie opt.uio_iovcnt++; 395192895Sjamie } 396191673Sjamie#ifdef INET 397192895Sjamie optiov[opt.uio_iovcnt].iov_base = "ip4.addr"; 398192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr"); 399192895Sjamie opt.uio_iovcnt++; 400192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_ip4; 401192895Sjamie optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr); 402192895Sjamie if (j->version == 0) 403192895Sjamie u_ip4->s_addr = j->ip4s; 404192895Sjamie else { 405192895Sjamie error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len); 406191673Sjamie if (error) { 407191673Sjamie free(u_path, M_TEMP); 408191673Sjamie return (error); 409191673Sjamie } 410192895Sjamie } 411192895Sjamie opt.uio_iovcnt++; 412185435Sbz#endif 413185435Sbz#ifdef INET6 414192895Sjamie optiov[opt.uio_iovcnt].iov_base = "ip6.addr"; 415192895Sjamie optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr"); 416192895Sjamie opt.uio_iovcnt++; 417192895Sjamie optiov[opt.uio_iovcnt].iov_base = u_ip6; 418192895Sjamie optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr); 419192895Sjamie error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len); 420192895Sjamie if (error) { 421192895Sjamie free(u_path, M_TEMP); 422192895Sjamie return (error); 423192895Sjamie } 424192895Sjamie opt.uio_iovcnt++; 425185435Sbz#endif 426192895Sjamie KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]), 427192895Sjamie ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt)); 428191673Sjamie error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH); 429191673Sjamie free(u_path, M_TEMP); 430191673Sjamie return (error); 431185435Sbz} 432185435Sbz 433192895Sjamie 434191673Sjamie/* 435191673Sjamie * struct jail_set_args { 436191673Sjamie * struct iovec *iovp; 437191673Sjamie * unsigned int iovcnt; 438191673Sjamie * int flags; 439191673Sjamie * }; 440191673Sjamie */ 441191673Sjamieint 442191673Sjamiejail_set(struct thread *td, struct jail_set_args *uap) 443185435Sbz{ 444191673Sjamie struct uio *auio; 445191673Sjamie int error; 446191673Sjamie 447191673Sjamie /* Check that we have an even number of iovecs. */ 448191673Sjamie if (uap->iovcnt & 1) 449191673Sjamie return (EINVAL); 450191673Sjamie 451191673Sjamie error = copyinuio(uap->iovp, uap->iovcnt, &auio); 452191673Sjamie if (error) 453191673Sjamie return (error); 454191673Sjamie error = kern_jail_set(td, auio, uap->flags); 455191673Sjamie free(auio, M_IOV); 456191673Sjamie return (error); 457191673Sjamie} 458191673Sjamie 459191673Sjamieint 460191673Sjamiekern_jail_set(struct thread *td, struct uio *optuio, int flags) 461191673Sjamie{ 462191673Sjamie struct nameidata nd; 463185435Sbz#ifdef INET 464190466Sjamie struct in_addr *ip4; 465185435Sbz#endif 466185435Sbz#ifdef INET6 467185435Sbz struct in6_addr *ip6; 468185435Sbz#endif 469191673Sjamie struct vfsopt *opt; 470191673Sjamie struct vfsoptlist *opts; 471195945Sjamie struct prison *pr, *deadpr, *mypr, *ppr, *tpr, *tppr; 472191673Sjamie struct vnode *root; 473193066Sjamie char *domain, *errmsg, *host, *name, *p, *path, *uuid; 474192895Sjamie#if defined(INET) || defined(INET6) 475191673Sjamie void *op; 476192895Sjamie#endif 477193066Sjamie unsigned long hid; 478192895Sjamie size_t namelen, onamelen; 479192895Sjamie int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos; 480195870Sjamie int gotchildmax, gotenforce, gothid, gotslevel; 481195870Sjamie int fi, jid, jsys, len, level; 482194762Sjamie int childmax, slevel, vfslocked; 483191673Sjamie#if defined(INET) || defined(INET6) 484192895Sjamie int ii, ij; 485191673Sjamie#endif 486191673Sjamie#ifdef INET 487195974Sjamie int ip4s, redo_ip4; 488191673Sjamie#endif 489191673Sjamie#ifdef INET6 490195974Sjamie int ip6s, redo_ip6; 491191673Sjamie#endif 492191673Sjamie unsigned pr_flags, ch_flags; 493192895Sjamie unsigned pr_allow, ch_allow, tallow; 494191673Sjamie char numbuf[12]; 495185435Sbz 496191673Sjamie error = priv_check(td, PRIV_JAIL_SET); 497191673Sjamie if (!error && (flags & JAIL_ATTACH)) 498191673Sjamie error = priv_check(td, PRIV_JAIL_ATTACH); 499191673Sjamie if (error) 500191673Sjamie return (error); 501192895Sjamie mypr = ppr = td->td_ucred->cr_prison; 502194762Sjamie if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0) 503192895Sjamie return (EPERM); 504191673Sjamie if (flags & ~JAIL_SET_MASK) 505191673Sjamie return (EINVAL); 506191673Sjamie 507185435Sbz /* 508191673Sjamie * Check all the parameters before committing to anything. Not all 509191673Sjamie * errors can be caught early, but we may as well try. Also, this 510191673Sjamie * takes care of some expensive stuff (path lookup) before getting 511191673Sjamie * the allprison lock. 512185435Sbz * 513191673Sjamie * XXX Jails are not filesystems, and jail parameters are not mount 514191673Sjamie * options. But it makes more sense to re-use the vfsopt code 515191673Sjamie * than duplicate it under a different name. 516185435Sbz */ 517191673Sjamie error = vfs_buildopts(optuio, &opts); 518191673Sjamie if (error) 519191673Sjamie return (error); 520185435Sbz#ifdef INET 521185435Sbz ip4 = NULL; 522185435Sbz#endif 523185435Sbz#ifdef INET6 524185435Sbz ip6 = NULL; 525185435Sbz#endif 526191673Sjamie 527191673Sjamie error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 528191673Sjamie if (error == ENOENT) 529191673Sjamie jid = 0; 530191673Sjamie else if (error != 0) 531191673Sjamie goto done_free; 532191673Sjamie 533191673Sjamie error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel)); 534191673Sjamie if (error == ENOENT) 535191673Sjamie gotslevel = 0; 536191673Sjamie else if (error != 0) 537191673Sjamie goto done_free; 538191673Sjamie else 539191673Sjamie gotslevel = 1; 540191673Sjamie 541194762Sjamie error = 542194762Sjamie vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax)); 543194762Sjamie if (error == ENOENT) 544194762Sjamie gotchildmax = 0; 545194762Sjamie else if (error != 0) 546194762Sjamie goto done_free; 547194762Sjamie else 548194762Sjamie gotchildmax = 1; 549194762Sjamie 550192895Sjamie error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce)); 551192895Sjamie gotenforce = (error == 0); 552192895Sjamie if (gotenforce) { 553192895Sjamie if (enforce < 0 || enforce > 2) 554192895Sjamie return (EINVAL); 555192895Sjamie } else if (error != ENOENT) 556192895Sjamie goto done_free; 557192895Sjamie 558191673Sjamie pr_flags = ch_flags = 0; 559192895Sjamie for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 560192895Sjamie fi++) { 561192895Sjamie if (pr_flag_names[fi] == NULL) 562192895Sjamie continue; 563192895Sjamie vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi); 564192895Sjamie vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi); 565192895Sjamie } 566191673Sjamie ch_flags |= pr_flags; 567195870Sjamie for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 568195870Sjamie fi++) { 569195870Sjamie error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys, 570195870Sjamie sizeof(jsys)); 571195870Sjamie if (error == ENOENT) 572195870Sjamie continue; 573195870Sjamie if (error != 0) 574195870Sjamie goto done_free; 575195870Sjamie switch (jsys) { 576195870Sjamie case JAIL_SYS_DISABLE: 577195870Sjamie if (!pr_flag_jailsys[fi].disable) { 578195870Sjamie error = EINVAL; 579195870Sjamie goto done_free; 580195870Sjamie } 581195870Sjamie pr_flags |= pr_flag_jailsys[fi].disable; 582195870Sjamie break; 583195870Sjamie case JAIL_SYS_NEW: 584195870Sjamie pr_flags |= pr_flag_jailsys[fi].new; 585195870Sjamie break; 586195870Sjamie case JAIL_SYS_INHERIT: 587195870Sjamie break; 588195870Sjamie default: 589195870Sjamie error = EINVAL; 590195870Sjamie goto done_free; 591195870Sjamie } 592195870Sjamie ch_flags |= 593195870Sjamie pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable; 594195870Sjamie } 595191673Sjamie if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE 596191673Sjamie && !(pr_flags & PR_PERSIST)) { 597191673Sjamie error = EINVAL; 598191673Sjamie vfs_opterror(opts, "new jail must persist or attach"); 599191673Sjamie goto done_errmsg; 600191673Sjamie } 601194251Sjamie#ifdef VIMAGE 602194251Sjamie if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) { 603194251Sjamie error = EINVAL; 604194251Sjamie vfs_opterror(opts, "vnet cannot be changed after creation"); 605194251Sjamie goto done_errmsg; 606194251Sjamie } 607194251Sjamie#endif 608195974Sjamie#ifdef INET 609195974Sjamie if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) { 610195974Sjamie error = EINVAL; 611195974Sjamie vfs_opterror(opts, "ip4 cannot be changed after creation"); 612195974Sjamie goto done_errmsg; 613195974Sjamie } 614195974Sjamie#endif 615195974Sjamie#ifdef INET6 616195974Sjamie if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) { 617195974Sjamie error = EINVAL; 618195974Sjamie vfs_opterror(opts, "ip6 cannot be changed after creation"); 619195974Sjamie goto done_errmsg; 620195974Sjamie } 621195974Sjamie#endif 622191673Sjamie 623192895Sjamie pr_allow = ch_allow = 0; 624192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 625192895Sjamie fi++) { 626192895Sjamie vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi); 627192895Sjamie vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi); 628192895Sjamie } 629192895Sjamie ch_allow |= pr_allow; 630192895Sjamie 631191673Sjamie error = vfs_getopt(opts, "name", (void **)&name, &len); 632191673Sjamie if (error == ENOENT) 633191673Sjamie name = NULL; 634191673Sjamie else if (error != 0) 635191673Sjamie goto done_free; 636191673Sjamie else { 637191673Sjamie if (len == 0 || name[len - 1] != '\0') { 638191673Sjamie error = EINVAL; 639191673Sjamie goto done_free; 640191673Sjamie } 641191673Sjamie if (len > MAXHOSTNAMELEN) { 642191673Sjamie error = ENAMETOOLONG; 643191673Sjamie goto done_free; 644191673Sjamie } 645191673Sjamie } 646191673Sjamie 647191673Sjamie error = vfs_getopt(opts, "host.hostname", (void **)&host, &len); 648191673Sjamie if (error == ENOENT) 649191673Sjamie host = NULL; 650191673Sjamie else if (error != 0) 651191673Sjamie goto done_free; 652191673Sjamie else { 653193066Sjamie ch_flags |= PR_HOST; 654193066Sjamie pr_flags |= PR_HOST; 655191673Sjamie if (len == 0 || host[len - 1] != '\0') { 656191673Sjamie error = EINVAL; 657191673Sjamie goto done_free; 658191673Sjamie } 659191673Sjamie if (len > MAXHOSTNAMELEN) { 660191673Sjamie error = ENAMETOOLONG; 661191673Sjamie goto done_free; 662191673Sjamie } 663191673Sjamie } 664191673Sjamie 665193066Sjamie error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len); 666193066Sjamie if (error == ENOENT) 667193066Sjamie domain = NULL; 668193066Sjamie else if (error != 0) 669193066Sjamie goto done_free; 670193066Sjamie else { 671193066Sjamie ch_flags |= PR_HOST; 672193066Sjamie pr_flags |= PR_HOST; 673193066Sjamie if (len == 0 || domain[len - 1] != '\0') { 674193066Sjamie error = EINVAL; 675193066Sjamie goto done_free; 676193066Sjamie } 677193066Sjamie if (len > MAXHOSTNAMELEN) { 678193066Sjamie error = ENAMETOOLONG; 679193066Sjamie goto done_free; 680193066Sjamie } 681193066Sjamie } 682193066Sjamie 683193066Sjamie error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len); 684193066Sjamie if (error == ENOENT) 685193066Sjamie uuid = NULL; 686193066Sjamie else if (error != 0) 687193066Sjamie goto done_free; 688193066Sjamie else { 689193066Sjamie ch_flags |= PR_HOST; 690193066Sjamie pr_flags |= PR_HOST; 691193066Sjamie if (len == 0 || uuid[len - 1] != '\0') { 692193066Sjamie error = EINVAL; 693193066Sjamie goto done_free; 694193066Sjamie } 695193066Sjamie if (len > HOSTUUIDLEN) { 696193066Sjamie error = ENAMETOOLONG; 697193066Sjamie goto done_free; 698193066Sjamie } 699193066Sjamie } 700193066Sjamie 701193066Sjamie#ifdef COMPAT_IA32 702193066Sjamie if (td->td_proc->p_sysent->sv_flags & SV_IA32) { 703193066Sjamie uint32_t hid32; 704193066Sjamie 705193066Sjamie error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32)); 706193066Sjamie hid = hid32; 707193066Sjamie } else 708193066Sjamie#endif 709193066Sjamie error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid)); 710193066Sjamie if (error == ENOENT) 711193066Sjamie gothid = 0; 712193066Sjamie else if (error != 0) 713193066Sjamie goto done_free; 714193066Sjamie else { 715193066Sjamie gothid = 1; 716193066Sjamie ch_flags |= PR_HOST; 717193066Sjamie pr_flags |= PR_HOST; 718193066Sjamie } 719193066Sjamie 720185435Sbz#ifdef INET 721191673Sjamie error = vfs_getopt(opts, "ip4.addr", &op, &ip4s); 722191673Sjamie if (error == ENOENT) 723195870Sjamie ip4s = (pr_flags & PR_IP4_DISABLE) ? 0 : -1; 724191673Sjamie else if (error != 0) 725191673Sjamie goto done_free; 726191673Sjamie else if (ip4s & (sizeof(*ip4) - 1)) { 727191673Sjamie error = EINVAL; 728191673Sjamie goto done_free; 729192895Sjamie } else { 730195870Sjamie ch_flags |= PR_IP4_USER | PR_IP4_DISABLE; 731195870Sjamie if (ip4s == 0) 732195870Sjamie pr_flags |= PR_IP4_USER | PR_IP4_DISABLE; 733195870Sjamie else { 734195870Sjamie pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER; 735192895Sjamie ip4s /= sizeof(*ip4); 736192895Sjamie if (ip4s > jail_max_af_ips) { 737185435Sbz error = EINVAL; 738192895Sjamie vfs_opterror(opts, "too many IPv4 addresses"); 739192895Sjamie goto done_errmsg; 740185435Sbz } 741195974Sjamie ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 742192895Sjamie bcopy(op, ip4, ip4s * sizeof(*ip4)); 743192895Sjamie /* 744192895Sjamie * IP addresses are all sorted but ip[0] to preserve 745192895Sjamie * the primary IP address as given from userland. 746192895Sjamie * This special IP is used for unbound outgoing 747192895Sjamie * connections as well for "loopback" traffic. 748192895Sjamie */ 749192895Sjamie if (ip4s > 1) 750192895Sjamie qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4); 751192895Sjamie /* 752192895Sjamie * Check for duplicate addresses and do some simple 753192895Sjamie * zero and broadcast checks. If users give other bogus 754192895Sjamie * addresses it is their problem. 755192895Sjamie * 756192895Sjamie * We do not have to care about byte order for these 757192895Sjamie * checks so we will do them in NBO. 758192895Sjamie */ 759192895Sjamie for (ii = 0; ii < ip4s; ii++) { 760192895Sjamie if (ip4[ii].s_addr == INADDR_ANY || 761192895Sjamie ip4[ii].s_addr == INADDR_BROADCAST) { 762192895Sjamie error = EINVAL; 763192895Sjamie goto done_free; 764192895Sjamie } 765192895Sjamie if ((ii+1) < ip4s && 766192895Sjamie (ip4[0].s_addr == ip4[ii+1].s_addr || 767192895Sjamie ip4[ii].s_addr == ip4[ii+1].s_addr)) { 768192895Sjamie error = EINVAL; 769192895Sjamie goto done_free; 770192895Sjamie } 771192895Sjamie } 772185435Sbz } 773191673Sjamie } 774191673Sjamie#endif 775185435Sbz 776185435Sbz#ifdef INET6 777191673Sjamie error = vfs_getopt(opts, "ip6.addr", &op, &ip6s); 778191673Sjamie if (error == ENOENT) 779195870Sjamie ip6s = (pr_flags & PR_IP6_DISABLE) ? 0 : -1; 780191673Sjamie else if (error != 0) 781191673Sjamie goto done_free; 782191673Sjamie else if (ip6s & (sizeof(*ip6) - 1)) { 783191673Sjamie error = EINVAL; 784191673Sjamie goto done_free; 785192895Sjamie } else { 786195870Sjamie ch_flags |= PR_IP6_USER | PR_IP6_DISABLE; 787195870Sjamie if (ip6s == 0) 788195870Sjamie pr_flags |= PR_IP6_USER | PR_IP6_DISABLE; 789195870Sjamie else { 790195870Sjamie pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER; 791192895Sjamie ip6s /= sizeof(*ip6); 792192895Sjamie if (ip6s > jail_max_af_ips) { 793185435Sbz error = EINVAL; 794192895Sjamie vfs_opterror(opts, "too many IPv6 addresses"); 795192895Sjamie goto done_errmsg; 796185435Sbz } 797195974Sjamie ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 798192895Sjamie bcopy(op, ip6, ip6s * sizeof(*ip6)); 799192895Sjamie if (ip6s > 1) 800192895Sjamie qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6); 801192895Sjamie for (ii = 0; ii < ip6s; ii++) { 802192895Sjamie if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) { 803192895Sjamie error = EINVAL; 804192895Sjamie goto done_free; 805192895Sjamie } 806192895Sjamie if ((ii+1) < ip6s && 807192895Sjamie (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) || 808192895Sjamie IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1]))) 809192895Sjamie { 810192895Sjamie error = EINVAL; 811192895Sjamie goto done_free; 812192895Sjamie } 813192895Sjamie } 814185435Sbz } 815191673Sjamie } 816185435Sbz#endif 817185435Sbz 818195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6)) 819195945Sjamie if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 820195945Sjamie error = EINVAL; 821195945Sjamie vfs_opterror(opts, 822195945Sjamie "vnet jails cannot have IP address restrictions"); 823195945Sjamie goto done_errmsg; 824195945Sjamie } 825195945Sjamie#endif 826195945Sjamie 827191673Sjamie root = NULL; 828191673Sjamie error = vfs_getopt(opts, "path", (void **)&path, &len); 829191673Sjamie if (error == ENOENT) 830191673Sjamie path = NULL; 831191673Sjamie else if (error != 0) 832191673Sjamie goto done_free; 833191673Sjamie else { 834191673Sjamie if (flags & JAIL_UPDATE) { 835191673Sjamie error = EINVAL; 836191673Sjamie vfs_opterror(opts, 837191673Sjamie "path cannot be changed after creation"); 838191673Sjamie goto done_errmsg; 839191673Sjamie } 840191673Sjamie if (len == 0 || path[len - 1] != '\0') { 841191673Sjamie error = EINVAL; 842191673Sjamie goto done_free; 843191673Sjamie } 844191673Sjamie if (len < 2 || (len == 2 && path[0] == '/')) 845191673Sjamie path = NULL; 846191673Sjamie else { 847192895Sjamie /* Leave room for a real-root full pathname. */ 848192895Sjamie if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/") 849192895Sjamie ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) { 850192895Sjamie error = ENAMETOOLONG; 851192895Sjamie goto done_free; 852192895Sjamie } 853191673Sjamie NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_SYSSPACE, 854191673Sjamie path, td); 855191673Sjamie error = namei(&nd); 856191673Sjamie if (error) 857191673Sjamie goto done_free; 858191673Sjamie vfslocked = NDHASGIANT(&nd); 859191673Sjamie root = nd.ni_vp; 860191673Sjamie NDFREE(&nd, NDF_ONLY_PNBUF); 861191673Sjamie if (root->v_type != VDIR) { 862191673Sjamie error = ENOTDIR; 863191673Sjamie vrele(root); 864191673Sjamie VFS_UNLOCK_GIANT(vfslocked); 865191673Sjamie goto done_free; 866191673Sjamie } 867191673Sjamie VFS_UNLOCK_GIANT(vfslocked); 868191673Sjamie } 869191673Sjamie } 870185435Sbz 871191673Sjamie /* 872191673Sjamie * Grab the allprison lock before letting modules check their 873191673Sjamie * parameters. Once we have it, do not let go so we'll have a 874191673Sjamie * consistent view of the OSD list. 875191673Sjamie */ 876191673Sjamie sx_xlock(&allprison_lock); 877191673Sjamie error = osd_jail_call(NULL, PR_METHOD_CHECK, opts); 878191673Sjamie if (error) 879191673Sjamie goto done_unlock_list; 880185435Sbz 881191673Sjamie /* By now, all parameters should have been noted. */ 882191673Sjamie TAILQ_FOREACH(opt, opts, link) { 883191673Sjamie if (!opt->seen && strcmp(opt->name, "errmsg")) { 884191673Sjamie error = EINVAL; 885191673Sjamie vfs_opterror(opts, "unknown parameter: %s", opt->name); 886191673Sjamie goto done_unlock_list; 887191673Sjamie } 888191673Sjamie } 889191673Sjamie 890185435Sbz /* 891191673Sjamie * See if we are creating a new record or updating an existing one. 892191673Sjamie * This abuses the file error codes ENOENT and EEXIST. 893185435Sbz */ 894191673Sjamie cuflags = flags & (JAIL_CREATE | JAIL_UPDATE); 895191673Sjamie if (!cuflags) { 896191673Sjamie error = EINVAL; 897191673Sjamie vfs_opterror(opts, "no valid operation (create or update)"); 898191673Sjamie goto done_unlock_list; 899191673Sjamie } 900191673Sjamie pr = NULL; 901191673Sjamie if (jid != 0) { 902192895Sjamie /* 903192895Sjamie * See if a requested jid already exists. There is an 904192895Sjamie * information leak here if the jid exists but is not within 905192895Sjamie * the caller's jail hierarchy. Jail creators will get EEXIST 906192895Sjamie * even though they cannot see the jail, and CREATE | UPDATE 907192895Sjamie * will return ENOENT which is not normally a valid error. 908192895Sjamie */ 909191673Sjamie if (jid < 0) { 910191673Sjamie error = EINVAL; 911191673Sjamie vfs_opterror(opts, "negative jid"); 912191673Sjamie goto done_unlock_list; 913191673Sjamie } 914191673Sjamie pr = prison_find(jid); 915191673Sjamie if (pr != NULL) { 916192895Sjamie ppr = pr->pr_parent; 917191673Sjamie /* Create: jid must not exist. */ 918191673Sjamie if (cuflags == JAIL_CREATE) { 919191673Sjamie mtx_unlock(&pr->pr_mtx); 920191673Sjamie error = EEXIST; 921191673Sjamie vfs_opterror(opts, "jail %d already exists", 922191673Sjamie jid); 923191673Sjamie goto done_unlock_list; 924191673Sjamie } 925192895Sjamie if (!prison_ischild(mypr, pr)) { 926192895Sjamie mtx_unlock(&pr->pr_mtx); 927192895Sjamie pr = NULL; 928192895Sjamie } else if (pr->pr_uref == 0) { 929191673Sjamie if (!(flags & JAIL_DYING)) { 930191673Sjamie mtx_unlock(&pr->pr_mtx); 931191673Sjamie error = ENOENT; 932191673Sjamie vfs_opterror(opts, "jail %d is dying", 933191673Sjamie jid); 934191673Sjamie goto done_unlock_list; 935191673Sjamie } else if ((flags & JAIL_ATTACH) || 936191673Sjamie (pr_flags & PR_PERSIST)) { 937191673Sjamie /* 938191673Sjamie * A dying jail might be resurrected 939191673Sjamie * (via attach or persist), but first 940191673Sjamie * it must determine if another jail 941191673Sjamie * has claimed its name. Accomplish 942191673Sjamie * this by implicitly re-setting the 943191673Sjamie * name. 944191673Sjamie */ 945191673Sjamie if (name == NULL) 946192895Sjamie name = prison_name(mypr, pr); 947191673Sjamie } 948191673Sjamie } 949191673Sjamie } 950191673Sjamie if (pr == NULL) { 951191673Sjamie /* Update: jid must exist. */ 952191673Sjamie if (cuflags == JAIL_UPDATE) { 953191673Sjamie error = ENOENT; 954191673Sjamie vfs_opterror(opts, "jail %d not found", jid); 955191673Sjamie goto done_unlock_list; 956191673Sjamie } 957191673Sjamie } 958191673Sjamie } 959191673Sjamie /* 960191673Sjamie * If the caller provided a name, look for a jail by that name. 961191673Sjamie * This has different semantics for creates and updates keyed by jid 962191673Sjamie * (where the name must not already exist in a different jail), 963191673Sjamie * and updates keyed by the name itself (where the name must exist 964191673Sjamie * because that is the jail being updated). 965191673Sjamie */ 966191673Sjamie if (name != NULL) { 967192895Sjamie p = strrchr(name, '.'); 968192895Sjamie if (p != NULL) { 969192895Sjamie /* 970192895Sjamie * This is a hierarchical name. Split it into the 971192895Sjamie * parent and child names, and make sure the parent 972192895Sjamie * exists or matches an already found jail. 973192895Sjamie */ 974192895Sjamie *p = '\0'; 975192895Sjamie if (pr != NULL) { 976192895Sjamie if (strncmp(name, ppr->pr_name, p - name) || 977192895Sjamie ppr->pr_name[p - name] != '\0') { 978192895Sjamie mtx_unlock(&pr->pr_mtx); 979192895Sjamie error = EINVAL; 980192895Sjamie vfs_opterror(opts, 981192895Sjamie "cannot change jail's parent"); 982192895Sjamie goto done_unlock_list; 983192895Sjamie } 984192895Sjamie } else { 985192895Sjamie ppr = prison_find_name(mypr, name); 986192895Sjamie if (ppr == NULL) { 987192895Sjamie error = ENOENT; 988192895Sjamie vfs_opterror(opts, 989192895Sjamie "jail \"%s\" not found", name); 990192895Sjamie goto done_unlock_list; 991192895Sjamie } 992192895Sjamie mtx_unlock(&ppr->pr_mtx); 993192895Sjamie } 994192895Sjamie name = p + 1; 995192895Sjamie } 996191673Sjamie if (name[0] != '\0') { 997192895Sjamie namelen = 998192895Sjamie (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 999192895Sjamie name_again: 1000191673Sjamie deadpr = NULL; 1001192895Sjamie FOREACH_PRISON_CHILD(ppr, tpr) { 1002191673Sjamie if (tpr != pr && tpr->pr_ref > 0 && 1003192895Sjamie !strcmp(tpr->pr_name + namelen, name)) { 1004191673Sjamie if (pr == NULL && 1005191673Sjamie cuflags != JAIL_CREATE) { 1006191673Sjamie mtx_lock(&tpr->pr_mtx); 1007191673Sjamie if (tpr->pr_ref > 0) { 1008191673Sjamie /* 1009191673Sjamie * Use this jail 1010191673Sjamie * for updates. 1011191673Sjamie */ 1012191673Sjamie if (tpr->pr_uref > 0) { 1013191673Sjamie pr = tpr; 1014191673Sjamie break; 1015191673Sjamie } 1016191673Sjamie deadpr = tpr; 1017191673Sjamie } 1018191673Sjamie mtx_unlock(&tpr->pr_mtx); 1019191673Sjamie } else if (tpr->pr_uref > 0) { 1020191673Sjamie /* 1021191673Sjamie * Create, or update(jid): 1022191673Sjamie * name must not exist in an 1023192895Sjamie * active sibling jail. 1024191673Sjamie */ 1025191673Sjamie error = EEXIST; 1026191673Sjamie if (pr != NULL) 1027191673Sjamie mtx_unlock(&pr->pr_mtx); 1028191673Sjamie vfs_opterror(opts, 1029191673Sjamie "jail \"%s\" already exists", 1030191673Sjamie name); 1031191673Sjamie goto done_unlock_list; 1032191673Sjamie } 1033191673Sjamie } 1034191673Sjamie } 1035191673Sjamie /* If no active jail is found, use a dying one. */ 1036191673Sjamie if (deadpr != NULL && pr == NULL) { 1037191673Sjamie if (flags & JAIL_DYING) { 1038191673Sjamie mtx_lock(&deadpr->pr_mtx); 1039191673Sjamie if (deadpr->pr_ref == 0) { 1040191673Sjamie mtx_unlock(&deadpr->pr_mtx); 1041191673Sjamie goto name_again; 1042191673Sjamie } 1043191673Sjamie pr = deadpr; 1044191673Sjamie } else if (cuflags == JAIL_UPDATE) { 1045191673Sjamie error = ENOENT; 1046191673Sjamie vfs_opterror(opts, 1047191673Sjamie "jail \"%s\" is dying", name); 1048191673Sjamie goto done_unlock_list; 1049191673Sjamie } 1050191673Sjamie } 1051191673Sjamie /* Update: name must exist if no jid. */ 1052191673Sjamie else if (cuflags == JAIL_UPDATE && pr == NULL) { 1053191673Sjamie error = ENOENT; 1054191673Sjamie vfs_opterror(opts, "jail \"%s\" not found", 1055191673Sjamie name); 1056191673Sjamie goto done_unlock_list; 1057191673Sjamie } 1058191673Sjamie } 1059191673Sjamie } 1060191673Sjamie /* Update: must provide a jid or name. */ 1061191673Sjamie else if (cuflags == JAIL_UPDATE && pr == NULL) { 1062191673Sjamie error = ENOENT; 1063191673Sjamie vfs_opterror(opts, "update specified no jail"); 1064191673Sjamie goto done_unlock_list; 1065191673Sjamie } 1066185435Sbz 1067191673Sjamie /* If there's no prison to update, create a new one and link it in. */ 1068191673Sjamie if (pr == NULL) { 1069194762Sjamie for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent) 1070194762Sjamie if (tpr->pr_childcount >= tpr->pr_childmax) { 1071194762Sjamie error = EPERM; 1072194762Sjamie vfs_opterror(opts, "prison limit exceeded"); 1073194762Sjamie goto done_unlock_list; 1074194762Sjamie } 1075191673Sjamie created = 1; 1076192895Sjamie mtx_lock(&ppr->pr_mtx); 1077192895Sjamie if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) { 1078192895Sjamie mtx_unlock(&ppr->pr_mtx); 1079192895Sjamie error = ENOENT; 1080192895Sjamie vfs_opterror(opts, "parent jail went away!"); 1081192895Sjamie goto done_unlock_list; 1082192895Sjamie } 1083192895Sjamie ppr->pr_ref++; 1084192895Sjamie ppr->pr_uref++; 1085192895Sjamie mtx_unlock(&ppr->pr_mtx); 1086191673Sjamie pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO); 1087191673Sjamie if (jid == 0) { 1088191673Sjamie /* Find the next free jid. */ 1089191673Sjamie jid = lastprid + 1; 1090191673Sjamie findnext: 1091191673Sjamie if (jid == JAIL_MAX) 1092191673Sjamie jid = 1; 1093191673Sjamie TAILQ_FOREACH(tpr, &allprison, pr_list) { 1094191673Sjamie if (tpr->pr_id < jid) 1095191673Sjamie continue; 1096191673Sjamie if (tpr->pr_id > jid || tpr->pr_ref == 0) { 1097191673Sjamie TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1098191673Sjamie break; 1099191673Sjamie } 1100191673Sjamie if (jid == lastprid) { 1101191673Sjamie error = EAGAIN; 1102191673Sjamie vfs_opterror(opts, 1103191673Sjamie "no available jail IDs"); 1104191673Sjamie free(pr, M_PRISON); 1105192895Sjamie prison_deref(ppr, PD_DEREF | 1106192895Sjamie PD_DEUREF | PD_LIST_XLOCKED); 1107192895Sjamie goto done_releroot; 1108191673Sjamie } 1109191673Sjamie jid++; 1110191673Sjamie goto findnext; 1111191673Sjamie } 1112191673Sjamie lastprid = jid; 1113191673Sjamie } else { 1114191673Sjamie /* 1115191673Sjamie * The jail already has a jid (that did not yet exist), 1116191673Sjamie * so just find where to insert it. 1117191673Sjamie */ 1118191673Sjamie TAILQ_FOREACH(tpr, &allprison, pr_list) 1119191673Sjamie if (tpr->pr_id >= jid) { 1120191673Sjamie TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1121191673Sjamie break; 1122191673Sjamie } 1123191673Sjamie } 1124191673Sjamie if (tpr == NULL) 1125191673Sjamie TAILQ_INSERT_TAIL(&allprison, pr, pr_list); 1126192895Sjamie LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling); 1127192895Sjamie for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 1128194762Sjamie tpr->pr_childcount++; 1129185435Sbz 1130192895Sjamie pr->pr_parent = ppr; 1131191673Sjamie pr->pr_id = jid; 1132192895Sjamie 1133192895Sjamie /* Set some default values, and inherit some from the parent. */ 1134191673Sjamie if (name == NULL) 1135191673Sjamie name = ""; 1136191673Sjamie if (path == NULL) { 1137191673Sjamie path = "/"; 1138192895Sjamie root = mypr->pr_root; 1139191673Sjamie vref(root); 1140191673Sjamie } 1141195944Sjamie strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN); 1142195944Sjamie pr->pr_flags |= PR_HOST; 1143195945Sjamie#if defined(INET) || defined(INET6) 1144195945Sjamie#ifdef VIMAGE 1145195945Sjamie if (!(pr_flags & PR_VNET)) 1146195945Sjamie#endif 1147195945Sjamie { 1148192895Sjamie#ifdef INET 1149195974Sjamie if (!(ch_flags & PR_IP4_USER)) 1150195974Sjamie pr->pr_flags |= 1151195974Sjamie PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE; 1152195974Sjamie else if (!(pr_flags & PR_IP4_USER)) { 1153195974Sjamie pr->pr_flags |= ppr->pr_flags & PR_IP4; 1154195974Sjamie if (ppr->pr_ip4 != NULL) { 1155195974Sjamie pr->pr_ip4s = ppr->pr_ip4s; 1156195974Sjamie pr->pr_ip4 = malloc(pr->pr_ip4s * 1157195974Sjamie sizeof(struct in_addr), M_PRISON, 1158195974Sjamie M_WAITOK); 1159195974Sjamie bcopy(ppr->pr_ip4, pr->pr_ip4, 1160195974Sjamie pr->pr_ip4s * sizeof(*pr->pr_ip4)); 1161195974Sjamie } 1162195974Sjamie } 1163192895Sjamie#endif 1164192895Sjamie#ifdef INET6 1165195974Sjamie if (!(ch_flags & PR_IP6_USER)) 1166195974Sjamie pr->pr_flags |= 1167195974Sjamie PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE; 1168195974Sjamie else if (!(pr_flags & PR_IP6_USER)) { 1169195974Sjamie pr->pr_flags |= ppr->pr_flags & PR_IP6; 1170195974Sjamie if (ppr->pr_ip6 != NULL) { 1171195974Sjamie pr->pr_ip6s = ppr->pr_ip6s; 1172195974Sjamie pr->pr_ip6 = malloc(pr->pr_ip6s * 1173195974Sjamie sizeof(struct in6_addr), M_PRISON, 1174195974Sjamie M_WAITOK); 1175195974Sjamie bcopy(ppr->pr_ip6, pr->pr_ip6, 1176195974Sjamie pr->pr_ip6s * sizeof(*pr->pr_ip6)); 1177195974Sjamie } 1178195974Sjamie } 1179192895Sjamie#endif 1180195945Sjamie } 1181195945Sjamie#endif 1182192895Sjamie pr->pr_securelevel = ppr->pr_securelevel; 1183192895Sjamie pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow; 1184192895Sjamie pr->pr_enforce_statfs = ppr->pr_enforce_statfs; 1185191673Sjamie 1186192895Sjamie LIST_INIT(&pr->pr_children); 1187192895Sjamie mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK); 1188191673Sjamie 1189194251Sjamie#ifdef VIMAGE 1190194251Sjamie /* Allocate a new vnet if specified. */ 1191194251Sjamie pr->pr_vnet = (pr_flags & PR_VNET) 1192194251Sjamie ? vnet_alloc() : ppr->pr_vnet; 1193194251Sjamie#endif 1194185435Sbz /* 1195191673Sjamie * Allocate a dedicated cpuset for each jail. 1196191673Sjamie * Unlike other initial settings, this may return an erorr. 1197185435Sbz */ 1198192895Sjamie error = cpuset_create_root(ppr, &pr->pr_cpuset); 1199191673Sjamie if (error) { 1200191673Sjamie prison_deref(pr, PD_LIST_XLOCKED); 1201191673Sjamie goto done_releroot; 1202191673Sjamie } 1203185435Sbz 1204191673Sjamie mtx_lock(&pr->pr_mtx); 1205185435Sbz /* 1206191673Sjamie * New prisons do not yet have a reference, because we do not 1207191673Sjamie * want other to see the incomplete prison once the 1208191673Sjamie * allprison_lock is downgraded. 1209185435Sbz */ 1210191673Sjamie } else { 1211191673Sjamie created = 0; 1212195974Sjamie /* 1213195974Sjamie * Grab a reference for existing prisons, to ensure they 1214195974Sjamie * continue to exist for the duration of the call. 1215195974Sjamie */ 1216195974Sjamie pr->pr_ref++; 1217195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6)) 1218195945Sjamie if ((pr->pr_flags & PR_VNET) && 1219195945Sjamie (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 1220195945Sjamie error = EINVAL; 1221195945Sjamie vfs_opterror(opts, 1222195945Sjamie "vnet jails cannot have IP address restrictions"); 1223195945Sjamie goto done_deref_locked; 1224195945Sjamie } 1225195945Sjamie#endif 1226195974Sjamie#ifdef INET 1227195974Sjamie if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 1228195974Sjamie error = EINVAL; 1229195974Sjamie vfs_opterror(opts, 1230195974Sjamie "ip4 cannot be changed after creation"); 1231195974Sjamie goto done_deref_locked; 1232195974Sjamie } 1233195974Sjamie#endif 1234195974Sjamie#ifdef INET6 1235195974Sjamie if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 1236195974Sjamie error = EINVAL; 1237195974Sjamie vfs_opterror(opts, 1238195974Sjamie "ip6 cannot be changed after creation"); 1239195974Sjamie goto done_deref_locked; 1240195974Sjamie } 1241195974Sjamie#endif 1242191673Sjamie } 1243185435Sbz 1244191673Sjamie /* Do final error checking before setting anything. */ 1245192895Sjamie if (gotslevel) { 1246192895Sjamie if (slevel < ppr->pr_securelevel) { 1247192895Sjamie error = EPERM; 1248192895Sjamie goto done_deref_locked; 1249192895Sjamie } 1250192895Sjamie } 1251194762Sjamie if (gotchildmax) { 1252194762Sjamie if (childmax >= ppr->pr_childmax) { 1253194762Sjamie error = EPERM; 1254194762Sjamie goto done_deref_locked; 1255194762Sjamie } 1256194762Sjamie } 1257192895Sjamie if (gotenforce) { 1258192895Sjamie if (enforce < ppr->pr_enforce_statfs) { 1259192895Sjamie error = EPERM; 1260192895Sjamie goto done_deref_locked; 1261192895Sjamie } 1262192895Sjamie } 1263185435Sbz#ifdef INET 1264195974Sjamie if (ip4s > 0) { 1265192895Sjamie if (ppr->pr_flags & PR_IP4) { 1266195974Sjamie /* 1267195974Sjamie * Make sure the new set of IP addresses is a 1268195974Sjamie * subset of the parent's list. Don't worry 1269195974Sjamie * about the parent being unlocked, as any 1270195974Sjamie * setting is done with allprison_lock held. 1271195974Sjamie */ 1272195974Sjamie for (ij = 0; ij < ppr->pr_ip4s; ij++) 1273195974Sjamie if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 1274195974Sjamie break; 1275195974Sjamie if (ij == ppr->pr_ip4s) { 1276195974Sjamie error = EPERM; 1277195974Sjamie goto done_deref_locked; 1278195974Sjamie } 1279195974Sjamie if (ip4s > 1) { 1280195974Sjamie for (ii = ij = 1; ii < ip4s; ii++) { 1281195974Sjamie if (ip4[ii].s_addr == 1282195974Sjamie ppr->pr_ip4[0].s_addr) 1283195974Sjamie continue; 1284195974Sjamie for (; ij < ppr->pr_ip4s; ij++) 1285195974Sjamie if (ip4[ii].s_addr == 1286195974Sjamie ppr->pr_ip4[ij].s_addr) 1287195974Sjamie break; 1288195974Sjamie if (ij == ppr->pr_ip4s) 1289195974Sjamie break; 1290192895Sjamie } 1291192895Sjamie if (ij == ppr->pr_ip4s) { 1292192895Sjamie error = EPERM; 1293192895Sjamie goto done_deref_locked; 1294192895Sjamie } 1295192895Sjamie } 1296192895Sjamie } 1297195974Sjamie /* 1298195974Sjamie * Check for conflicting IP addresses. We permit them 1299195974Sjamie * if there is no more than one IP on each jail. If 1300195974Sjamie * there is a duplicate on a jail with more than one 1301195974Sjamie * IP stop checking and return error. 1302195974Sjamie */ 1303195974Sjamie tppr = ppr; 1304195945Sjamie#ifdef VIMAGE 1305195974Sjamie for (; tppr != &prison0; tppr = tppr->pr_parent) 1306195974Sjamie if (tppr->pr_flags & PR_VNET) 1307195974Sjamie break; 1308195945Sjamie#endif 1309195974Sjamie FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1310195974Sjamie if (tpr == pr || 1311195945Sjamie#ifdef VIMAGE 1312195974Sjamie (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1313195945Sjamie#endif 1314195974Sjamie tpr->pr_uref == 0) { 1315192895Sjamie descend = 0; 1316195974Sjamie continue; 1317195974Sjamie } 1318195974Sjamie if (!(tpr->pr_flags & PR_IP4_USER)) 1319195974Sjamie continue; 1320195974Sjamie descend = 0; 1321195974Sjamie if (tpr->pr_ip4 == NULL || 1322195974Sjamie (ip4s == 1 && tpr->pr_ip4s == 1)) 1323195974Sjamie continue; 1324195974Sjamie for (ii = 0; ii < ip4s; ii++) { 1325195974Sjamie if (_prison_check_ip4(tpr, &ip4[ii]) == 0) { 1326195974Sjamie error = EADDRINUSE; 1327195974Sjamie vfs_opterror(opts, 1328195974Sjamie "IPv4 addresses clash"); 1329195974Sjamie goto done_deref_locked; 1330192895Sjamie } 1331192895Sjamie } 1332192895Sjamie } 1333192895Sjamie } 1334185435Sbz#endif 1335191673Sjamie#ifdef INET6 1336195974Sjamie if (ip6s > 0) { 1337192895Sjamie if (ppr->pr_flags & PR_IP6) { 1338195974Sjamie /* 1339195974Sjamie * Make sure the new set of IP addresses is a 1340195974Sjamie * subset of the parent's list. 1341195974Sjamie */ 1342195974Sjamie for (ij = 0; ij < ppr->pr_ip6s; ij++) 1343195974Sjamie if (IN6_ARE_ADDR_EQUAL(&ip6[0], 1344195974Sjamie &ppr->pr_ip6[ij])) 1345195974Sjamie break; 1346195974Sjamie if (ij == ppr->pr_ip6s) { 1347195974Sjamie error = EPERM; 1348195974Sjamie goto done_deref_locked; 1349195974Sjamie } 1350195974Sjamie if (ip6s > 1) { 1351195974Sjamie for (ii = ij = 1; ii < ip6s; ii++) { 1352195974Sjamie if (IN6_ARE_ADDR_EQUAL(&ip6[ii], 1353195974Sjamie &ppr->pr_ip6[0])) 1354195974Sjamie continue; 1355195974Sjamie for (; ij < ppr->pr_ip6s; ij++) 1356195974Sjamie if (IN6_ARE_ADDR_EQUAL( 1357195974Sjamie &ip6[ii], &ppr->pr_ip6[ij])) 1358195974Sjamie break; 1359195974Sjamie if (ij == ppr->pr_ip6s) 1360195974Sjamie break; 1361192895Sjamie } 1362192895Sjamie if (ij == ppr->pr_ip6s) { 1363192895Sjamie error = EPERM; 1364192895Sjamie goto done_deref_locked; 1365192895Sjamie } 1366192895Sjamie } 1367192895Sjamie } 1368195974Sjamie /* Check for conflicting IP addresses. */ 1369195974Sjamie tppr = ppr; 1370195945Sjamie#ifdef VIMAGE 1371195974Sjamie for (; tppr != &prison0; tppr = tppr->pr_parent) 1372195974Sjamie if (tppr->pr_flags & PR_VNET) 1373195974Sjamie break; 1374195945Sjamie#endif 1375195974Sjamie FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1376195974Sjamie if (tpr == pr || 1377195945Sjamie#ifdef VIMAGE 1378195974Sjamie (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1379195945Sjamie#endif 1380195974Sjamie tpr->pr_uref == 0) { 1381192895Sjamie descend = 0; 1382195974Sjamie continue; 1383195974Sjamie } 1384195974Sjamie if (!(tpr->pr_flags & PR_IP6_USER)) 1385195974Sjamie continue; 1386195974Sjamie descend = 0; 1387195974Sjamie if (tpr->pr_ip6 == NULL || 1388195974Sjamie (ip6s == 1 && tpr->pr_ip6s == 1)) 1389195974Sjamie continue; 1390195974Sjamie for (ii = 0; ii < ip6s; ii++) { 1391195974Sjamie if (_prison_check_ip6(tpr, &ip6[ii]) == 0) { 1392195974Sjamie error = EADDRINUSE; 1393195974Sjamie vfs_opterror(opts, 1394195974Sjamie "IPv6 addresses clash"); 1395195974Sjamie goto done_deref_locked; 1396192895Sjamie } 1397192895Sjamie } 1398191673Sjamie } 1399192895Sjamie } 1400191673Sjamie#endif 1401192895Sjamie onamelen = namelen = 0; 1402192895Sjamie if (name != NULL) { 1403191673Sjamie /* Give a default name of the jid. */ 1404191673Sjamie if (name[0] == '\0') 1405191673Sjamie snprintf(name = numbuf, sizeof(numbuf), "%d", jid); 1406191673Sjamie else if (strtoul(name, &p, 10) != jid && *p == '\0') { 1407191673Sjamie error = EINVAL; 1408191673Sjamie vfs_opterror(opts, "name cannot be numeric"); 1409192895Sjamie goto done_deref_locked; 1410191673Sjamie } 1411191673Sjamie /* 1412192895Sjamie * Make sure the name isn't too long for the prison or its 1413192895Sjamie * children. 1414191673Sjamie */ 1415192895Sjamie onamelen = strlen(pr->pr_name); 1416192895Sjamie namelen = strlen(name); 1417192895Sjamie if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) { 1418192895Sjamie error = ENAMETOOLONG; 1419192895Sjamie goto done_deref_locked; 1420192895Sjamie } 1421192895Sjamie FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 1422192895Sjamie if (strlen(tpr->pr_name) + (namelen - onamelen) >= 1423192895Sjamie sizeof(pr->pr_name)) { 1424192895Sjamie error = ENAMETOOLONG; 1425192895Sjamie goto done_deref_locked; 1426192895Sjamie } 1427192895Sjamie } 1428191673Sjamie } 1429192895Sjamie if (pr_allow & ~ppr->pr_allow) { 1430192895Sjamie error = EPERM; 1431192895Sjamie goto done_deref_locked; 1432192895Sjamie } 1433185435Sbz 1434191673Sjamie /* Set the parameters of the prison. */ 1435191673Sjamie#ifdef INET 1436192895Sjamie redo_ip4 = 0; 1437195974Sjamie if (pr_flags & PR_IP4_USER) { 1438195974Sjamie pr->pr_flags |= PR_IP4; 1439195974Sjamie free(pr->pr_ip4, M_PRISON); 1440195974Sjamie pr->pr_ip4s = ip4s; 1441195974Sjamie pr->pr_ip4 = ip4; 1442195974Sjamie ip4 = NULL; 1443192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1444195945Sjamie#ifdef VIMAGE 1445195945Sjamie if (tpr->pr_flags & PR_VNET) { 1446195945Sjamie descend = 0; 1447195945Sjamie continue; 1448195945Sjamie } 1449195945Sjamie#endif 1450192895Sjamie if (prison_restrict_ip4(tpr, NULL)) { 1451192895Sjamie redo_ip4 = 1; 1452192895Sjamie descend = 0; 1453192895Sjamie } 1454192895Sjamie } 1455185435Sbz } 1456191673Sjamie#endif 1457191673Sjamie#ifdef INET6 1458192895Sjamie redo_ip6 = 0; 1459195974Sjamie if (pr_flags & PR_IP6_USER) { 1460195974Sjamie pr->pr_flags |= PR_IP6; 1461195974Sjamie free(pr->pr_ip6, M_PRISON); 1462195974Sjamie pr->pr_ip6s = ip6s; 1463195974Sjamie pr->pr_ip6 = ip6; 1464195974Sjamie ip6 = NULL; 1465192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1466195945Sjamie#ifdef VIMAGE 1467195945Sjamie if (tpr->pr_flags & PR_VNET) { 1468195945Sjamie descend = 0; 1469195945Sjamie continue; 1470195945Sjamie } 1471195945Sjamie#endif 1472192895Sjamie if (prison_restrict_ip6(tpr, NULL)) { 1473192895Sjamie redo_ip6 = 1; 1474192895Sjamie descend = 0; 1475192895Sjamie } 1476192895Sjamie } 1477191673Sjamie } 1478191673Sjamie#endif 1479192895Sjamie if (gotslevel) { 1480191673Sjamie pr->pr_securelevel = slevel; 1481192895Sjamie /* Set all child jails to be at least this level. */ 1482192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1483192895Sjamie if (tpr->pr_securelevel < slevel) 1484192895Sjamie tpr->pr_securelevel = slevel; 1485192895Sjamie } 1486194762Sjamie if (gotchildmax) { 1487194762Sjamie pr->pr_childmax = childmax; 1488194762Sjamie /* Set all child jails to under this limit. */ 1489194762Sjamie FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level) 1490194762Sjamie if (tpr->pr_childmax > childmax - level) 1491194762Sjamie tpr->pr_childmax = childmax > level 1492194762Sjamie ? childmax - level : 0; 1493194762Sjamie } 1494192895Sjamie if (gotenforce) { 1495192895Sjamie pr->pr_enforce_statfs = enforce; 1496192895Sjamie /* Pass this restriction on to the children. */ 1497192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1498192895Sjamie if (tpr->pr_enforce_statfs < enforce) 1499192895Sjamie tpr->pr_enforce_statfs = enforce; 1500192895Sjamie } 1501192895Sjamie if (name != NULL) { 1502192895Sjamie if (ppr == &prison0) 1503192895Sjamie strlcpy(pr->pr_name, name, sizeof(pr->pr_name)); 1504192895Sjamie else 1505192895Sjamie snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s", 1506192895Sjamie ppr->pr_name, name); 1507192895Sjamie /* Change this component of child names. */ 1508192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1509192895Sjamie bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen, 1510192895Sjamie strlen(tpr->pr_name + onamelen) + 1); 1511192895Sjamie bcopy(pr->pr_name, tpr->pr_name, namelen); 1512192895Sjamie } 1513192895Sjamie } 1514191673Sjamie if (path != NULL) { 1515192895Sjamie /* Try to keep a real-rooted full pathname. */ 1516192895Sjamie if (path[0] == '/' && strcmp(mypr->pr_path, "/")) 1517192895Sjamie snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s", 1518192895Sjamie mypr->pr_path, path); 1519192895Sjamie else 1520192895Sjamie strlcpy(pr->pr_path, path, sizeof(pr->pr_path)); 1521191673Sjamie pr->pr_root = root; 1522191673Sjamie } 1523193066Sjamie if (PR_HOST & ch_flags & ~pr_flags) { 1524193066Sjamie if (pr->pr_flags & PR_HOST) { 1525193066Sjamie /* 1526193066Sjamie * Copy the parent's host info. As with pr_ip4 above, 1527193066Sjamie * the lack of a lock on the parent is not a problem; 1528193066Sjamie * it is always set with allprison_lock at least 1529193066Sjamie * shared, and is held exclusively here. 1530193066Sjamie */ 1531194118Sjamie strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname, 1532194118Sjamie sizeof(pr->pr_hostname)); 1533194118Sjamie strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname, 1534194118Sjamie sizeof(pr->pr_domainname)); 1535194118Sjamie strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid, 1536194118Sjamie sizeof(pr->pr_hostuuid)); 1537193066Sjamie pr->pr_hostid = pr->pr_parent->pr_hostid; 1538193066Sjamie } 1539193066Sjamie } else if (host != NULL || domain != NULL || uuid != NULL || gothid) { 1540193066Sjamie /* Set this prison, and any descendants without PR_HOST. */ 1541193066Sjamie if (host != NULL) 1542194118Sjamie strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname)); 1543193066Sjamie if (domain != NULL) 1544194118Sjamie strlcpy(pr->pr_domainname, domain, 1545194118Sjamie sizeof(pr->pr_domainname)); 1546193066Sjamie if (uuid != NULL) 1547194118Sjamie strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid)); 1548193066Sjamie if (gothid) 1549193066Sjamie pr->pr_hostid = hid; 1550193066Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1551193066Sjamie if (tpr->pr_flags & PR_HOST) 1552193066Sjamie descend = 0; 1553193066Sjamie else { 1554193066Sjamie if (host != NULL) 1555194118Sjamie strlcpy(tpr->pr_hostname, 1556194118Sjamie pr->pr_hostname, 1557194118Sjamie sizeof(tpr->pr_hostname)); 1558193066Sjamie if (domain != NULL) 1559194118Sjamie strlcpy(tpr->pr_domainname, 1560194118Sjamie pr->pr_domainname, 1561194118Sjamie sizeof(tpr->pr_domainname)); 1562193066Sjamie if (uuid != NULL) 1563194118Sjamie strlcpy(tpr->pr_hostuuid, 1564194118Sjamie pr->pr_hostuuid, 1565194118Sjamie sizeof(tpr->pr_hostuuid)); 1566193066Sjamie if (gothid) 1567193066Sjamie tpr->pr_hostid = hid; 1568193066Sjamie } 1569193066Sjamie } 1570193066Sjamie } 1571192895Sjamie if ((tallow = ch_allow & ~pr_allow)) { 1572192895Sjamie /* Clear allow bits in all children. */ 1573192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1574192895Sjamie tpr->pr_allow &= ~tallow; 1575192895Sjamie } 1576192895Sjamie pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow; 1577191673Sjamie /* 1578191673Sjamie * Persistent prisons get an extra reference, and prisons losing their 1579191673Sjamie * persist flag lose that reference. Only do this for existing prisons 1580191673Sjamie * for now, so new ones will remain unseen until after the module 1581191673Sjamie * handlers have completed. 1582191673Sjamie */ 1583191673Sjamie if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) { 1584191673Sjamie if (pr_flags & PR_PERSIST) { 1585191673Sjamie pr->pr_ref++; 1586191673Sjamie pr->pr_uref++; 1587191673Sjamie } else { 1588191673Sjamie pr->pr_ref--; 1589191673Sjamie pr->pr_uref--; 1590191673Sjamie } 1591191673Sjamie } 1592191673Sjamie pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags; 1593191673Sjamie mtx_unlock(&pr->pr_mtx); 1594185435Sbz 1595192895Sjamie /* Locks may have prevented a complete restriction of child IP 1596192895Sjamie * addresses. If so, allocate some more memory and try again. 1597192895Sjamie */ 1598192895Sjamie#ifdef INET 1599192895Sjamie while (redo_ip4) { 1600192895Sjamie ip4s = pr->pr_ip4s; 1601192895Sjamie ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 1602192895Sjamie mtx_lock(&pr->pr_mtx); 1603192895Sjamie redo_ip4 = 0; 1604192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1605195945Sjamie#ifdef VIMAGE 1606195945Sjamie if (tpr->pr_flags & PR_VNET) { 1607195945Sjamie descend = 0; 1608195945Sjamie continue; 1609195945Sjamie } 1610195945Sjamie#endif 1611192895Sjamie if (prison_restrict_ip4(tpr, ip4)) { 1612192895Sjamie if (ip4 != NULL) 1613192895Sjamie ip4 = NULL; 1614192895Sjamie else 1615192895Sjamie redo_ip4 = 1; 1616192895Sjamie } 1617192895Sjamie } 1618192895Sjamie mtx_unlock(&pr->pr_mtx); 1619192895Sjamie } 1620192895Sjamie#endif 1621192895Sjamie#ifdef INET6 1622192895Sjamie while (redo_ip6) { 1623192895Sjamie ip6s = pr->pr_ip6s; 1624192895Sjamie ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 1625192895Sjamie mtx_lock(&pr->pr_mtx); 1626192895Sjamie redo_ip6 = 0; 1627192895Sjamie FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1628195945Sjamie#ifdef VIMAGE 1629195945Sjamie if (tpr->pr_flags & PR_VNET) { 1630195945Sjamie descend = 0; 1631195945Sjamie continue; 1632195945Sjamie } 1633195945Sjamie#endif 1634192895Sjamie if (prison_restrict_ip6(tpr, ip6)) { 1635192895Sjamie if (ip6 != NULL) 1636192895Sjamie ip6 = NULL; 1637192895Sjamie else 1638192895Sjamie redo_ip6 = 1; 1639192895Sjamie } 1640192895Sjamie } 1641192895Sjamie mtx_unlock(&pr->pr_mtx); 1642192895Sjamie } 1643192895Sjamie#endif 1644192895Sjamie 1645191673Sjamie /* Let the modules do their work. */ 1646191673Sjamie sx_downgrade(&allprison_lock); 1647191673Sjamie if (created) { 1648191673Sjamie error = osd_jail_call(pr, PR_METHOD_CREATE, opts); 1649191673Sjamie if (error) { 1650191673Sjamie prison_deref(pr, PD_LIST_SLOCKED); 1651191673Sjamie goto done_errmsg; 1652191673Sjamie } 1653191673Sjamie } 1654191673Sjamie error = osd_jail_call(pr, PR_METHOD_SET, opts); 1655191673Sjamie if (error) { 1656191673Sjamie prison_deref(pr, created 1657191673Sjamie ? PD_LIST_SLOCKED 1658191673Sjamie : PD_DEREF | PD_LIST_SLOCKED); 1659191673Sjamie goto done_errmsg; 1660191673Sjamie } 1661191673Sjamie 1662191673Sjamie /* Attach this process to the prison if requested. */ 1663191673Sjamie if (flags & JAIL_ATTACH) { 1664191673Sjamie mtx_lock(&pr->pr_mtx); 1665191673Sjamie error = do_jail_attach(td, pr); 1666191673Sjamie if (error) { 1667191673Sjamie vfs_opterror(opts, "attach failed"); 1668191673Sjamie if (!created) 1669191673Sjamie prison_deref(pr, PD_DEREF); 1670191673Sjamie goto done_errmsg; 1671191673Sjamie } 1672191673Sjamie } 1673191673Sjamie 1674191673Sjamie /* 1675191673Sjamie * Now that it is all there, drop the temporary reference from existing 1676191673Sjamie * prisons. Or add a reference to newly created persistent prisons 1677191673Sjamie * (which was not done earlier so that the prison would not be publicly 1678191673Sjamie * visible). 1679191673Sjamie */ 1680191673Sjamie if (!created) { 1681191673Sjamie prison_deref(pr, (flags & JAIL_ATTACH) 1682191673Sjamie ? PD_DEREF 1683191673Sjamie : PD_DEREF | PD_LIST_SLOCKED); 1684191673Sjamie } else { 1685191673Sjamie if (pr_flags & PR_PERSIST) { 1686191673Sjamie mtx_lock(&pr->pr_mtx); 1687191673Sjamie pr->pr_ref++; 1688191673Sjamie pr->pr_uref++; 1689191673Sjamie mtx_unlock(&pr->pr_mtx); 1690191673Sjamie } 1691191673Sjamie if (!(flags & JAIL_ATTACH)) 1692191673Sjamie sx_sunlock(&allprison_lock); 1693191673Sjamie } 1694191673Sjamie td->td_retval[0] = pr->pr_id; 1695191673Sjamie goto done_errmsg; 1696191673Sjamie 1697192895Sjamie done_deref_locked: 1698192895Sjamie prison_deref(pr, created 1699192895Sjamie ? PD_LOCKED | PD_LIST_XLOCKED 1700192895Sjamie : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 1701192895Sjamie goto done_releroot; 1702191673Sjamie done_unlock_list: 1703191673Sjamie sx_xunlock(&allprison_lock); 1704191673Sjamie done_releroot: 1705191673Sjamie if (root != NULL) { 1706191673Sjamie vfslocked = VFS_LOCK_GIANT(root->v_mount); 1707191673Sjamie vrele(root); 1708191673Sjamie VFS_UNLOCK_GIANT(vfslocked); 1709191673Sjamie } 1710191673Sjamie done_errmsg: 1711191673Sjamie if (error) { 1712191673Sjamie vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 1713191673Sjamie if (errmsg_len > 0) { 1714191673Sjamie errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1; 1715191673Sjamie if (errmsg_pos > 0) { 1716191673Sjamie if (optuio->uio_segflg == UIO_SYSSPACE) 1717191673Sjamie bcopy(errmsg, 1718191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 1719191673Sjamie errmsg_len); 1720191673Sjamie else 1721191673Sjamie copyout(errmsg, 1722191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 1723191673Sjamie errmsg_len); 1724191673Sjamie } 1725191673Sjamie } 1726191673Sjamie } 1727191673Sjamie done_free: 1728191673Sjamie#ifdef INET 1729191673Sjamie free(ip4, M_PRISON); 1730191673Sjamie#endif 1731191673Sjamie#ifdef INET6 1732191673Sjamie free(ip6, M_PRISON); 1733191673Sjamie#endif 1734191673Sjamie vfs_freeopts(opts); 1735191673Sjamie return (error); 1736191673Sjamie} 1737191673Sjamie 1738191673Sjamie 173982710Sdillon/* 1740191673Sjamie * struct jail_get_args { 1741191673Sjamie * struct iovec *iovp; 1742191673Sjamie * unsigned int iovcnt; 1743191673Sjamie * int flags; 1744114168Smike * }; 174582710Sdillon */ 174646155Sphkint 1747191673Sjamiejail_get(struct thread *td, struct jail_get_args *uap) 174846155Sphk{ 1749191673Sjamie struct uio *auio; 1750185435Sbz int error; 1751185435Sbz 1752191673Sjamie /* Check that we have an even number of iovecs. */ 1753191673Sjamie if (uap->iovcnt & 1) 1754191673Sjamie return (EINVAL); 1755191673Sjamie 1756191673Sjamie error = copyinuio(uap->iovp, uap->iovcnt, &auio); 1757185435Sbz if (error) 1758185435Sbz return (error); 1759191673Sjamie error = kern_jail_get(td, auio, uap->flags); 1760191673Sjamie if (error == 0) 1761191673Sjamie error = copyout(auio->uio_iov, uap->iovp, 1762191673Sjamie uap->iovcnt * sizeof (struct iovec)); 1763191673Sjamie free(auio, M_IOV); 1764191673Sjamie return (error); 1765191673Sjamie} 1766185435Sbz 1767191673Sjamieint 1768191673Sjamiekern_jail_get(struct thread *td, struct uio *optuio, int flags) 1769191673Sjamie{ 1770192895Sjamie struct prison *pr, *mypr; 1771191673Sjamie struct vfsopt *opt; 1772191673Sjamie struct vfsoptlist *opts; 1773191673Sjamie char *errmsg, *name; 1774192895Sjamie int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos; 1775185435Sbz 1776191673Sjamie if (flags & ~JAIL_GET_MASK) 1777191673Sjamie return (EINVAL); 1778185435Sbz 1779191673Sjamie /* Get the parameter list. */ 1780191673Sjamie error = vfs_buildopts(optuio, &opts); 1781191673Sjamie if (error) 1782191673Sjamie return (error); 1783191673Sjamie errmsg_pos = vfs_getopt_pos(opts, "errmsg"); 1784192895Sjamie mypr = td->td_ucred->cr_prison; 1785185435Sbz 1786191673Sjamie /* 1787191673Sjamie * Find the prison specified by one of: lastjid, jid, name. 1788191673Sjamie */ 1789191673Sjamie sx_slock(&allprison_lock); 1790191673Sjamie error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid)); 1791191673Sjamie if (error == 0) { 1792191673Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) { 1793192895Sjamie if (pr->pr_id > jid && prison_ischild(mypr, pr)) { 1794191673Sjamie mtx_lock(&pr->pr_mtx); 1795191673Sjamie if (pr->pr_ref > 0 && 1796191673Sjamie (pr->pr_uref > 0 || (flags & JAIL_DYING))) 1797191673Sjamie break; 1798191673Sjamie mtx_unlock(&pr->pr_mtx); 1799191673Sjamie } 1800191673Sjamie } 1801191673Sjamie if (pr != NULL) 1802191673Sjamie goto found_prison; 1803191673Sjamie error = ENOENT; 1804191673Sjamie vfs_opterror(opts, "no jail after %d", jid); 1805191673Sjamie goto done_unlock_list; 1806191673Sjamie } else if (error != ENOENT) 1807191673Sjamie goto done_unlock_list; 1808185435Sbz 1809191673Sjamie error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 1810191673Sjamie if (error == 0) { 1811191673Sjamie if (jid != 0) { 1812192895Sjamie pr = prison_find_child(mypr, jid); 1813191673Sjamie if (pr != NULL) { 1814191673Sjamie if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 1815191673Sjamie mtx_unlock(&pr->pr_mtx); 1816191673Sjamie error = ENOENT; 1817191673Sjamie vfs_opterror(opts, "jail %d is dying", 1818191673Sjamie jid); 1819191673Sjamie goto done_unlock_list; 1820191673Sjamie } 1821191673Sjamie goto found_prison; 1822191673Sjamie } 1823191673Sjamie error = ENOENT; 1824191673Sjamie vfs_opterror(opts, "jail %d not found", jid); 1825191673Sjamie goto done_unlock_list; 1826191673Sjamie } 1827191673Sjamie } else if (error != ENOENT) 1828191673Sjamie goto done_unlock_list; 182946155Sphk 1830191673Sjamie error = vfs_getopt(opts, "name", (void **)&name, &len); 1831191673Sjamie if (error == 0) { 1832191673Sjamie if (len == 0 || name[len - 1] != '\0') { 1833191673Sjamie error = EINVAL; 1834191673Sjamie goto done_unlock_list; 1835191673Sjamie } 1836192895Sjamie pr = prison_find_name(mypr, name); 1837191673Sjamie if (pr != NULL) { 1838191673Sjamie if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 1839191673Sjamie mtx_unlock(&pr->pr_mtx); 1840191673Sjamie error = ENOENT; 1841191673Sjamie vfs_opterror(opts, "jail \"%s\" is dying", 1842191673Sjamie name); 1843191673Sjamie goto done_unlock_list; 1844191673Sjamie } 1845191673Sjamie goto found_prison; 1846191673Sjamie } 1847191673Sjamie error = ENOENT; 1848191673Sjamie vfs_opterror(opts, "jail \"%s\" not found", name); 1849191673Sjamie goto done_unlock_list; 1850191673Sjamie } else if (error != ENOENT) 1851191673Sjamie goto done_unlock_list; 1852185435Sbz 1853191673Sjamie vfs_opterror(opts, "no jail specified"); 1854191673Sjamie error = ENOENT; 1855191673Sjamie goto done_unlock_list; 1856191673Sjamie 1857191673Sjamie found_prison: 1858191673Sjamie /* Get the parameters of the prison. */ 1859191673Sjamie pr->pr_ref++; 1860191673Sjamie locked = PD_LOCKED; 1861191673Sjamie td->td_retval[0] = pr->pr_id; 1862191673Sjamie error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id)); 1863191673Sjamie if (error != 0 && error != ENOENT) 1864191673Sjamie goto done_deref; 1865192895Sjamie i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id; 1866192895Sjamie error = vfs_setopt(opts, "parent", &i, sizeof(i)); 1867191673Sjamie if (error != 0 && error != ENOENT) 1868191673Sjamie goto done_deref; 1869192895Sjamie error = vfs_setopts(opts, "name", prison_name(mypr, pr)); 1870192895Sjamie if (error != 0 && error != ENOENT) 1871192895Sjamie goto done_deref; 1872192895Sjamie error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id, 1873191673Sjamie sizeof(pr->pr_cpuset->cs_id)); 1874191673Sjamie if (error != 0 && error != ENOENT) 1875191673Sjamie goto done_deref; 1876192895Sjamie error = vfs_setopts(opts, "path", prison_path(mypr, pr)); 1877191673Sjamie if (error != 0 && error != ENOENT) 1878191673Sjamie goto done_deref; 1879191673Sjamie#ifdef INET 1880191673Sjamie error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4, 1881191673Sjamie pr->pr_ip4s * sizeof(*pr->pr_ip4)); 1882191673Sjamie if (error != 0 && error != ENOENT) 1883191673Sjamie goto done_deref; 1884191673Sjamie#endif 1885191673Sjamie#ifdef INET6 1886191673Sjamie error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6, 1887191673Sjamie pr->pr_ip6s * sizeof(*pr->pr_ip6)); 1888191673Sjamie if (error != 0 && error != ENOENT) 1889191673Sjamie goto done_deref; 1890191673Sjamie#endif 1891191673Sjamie error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel, 1892191673Sjamie sizeof(pr->pr_securelevel)); 1893191673Sjamie if (error != 0 && error != ENOENT) 1894191673Sjamie goto done_deref; 1895194762Sjamie error = vfs_setopt(opts, "children.cur", &pr->pr_childcount, 1896194762Sjamie sizeof(pr->pr_childcount)); 1897194762Sjamie if (error != 0 && error != ENOENT) 1898194762Sjamie goto done_deref; 1899194762Sjamie error = vfs_setopt(opts, "children.max", &pr->pr_childmax, 1900194762Sjamie sizeof(pr->pr_childmax)); 1901194762Sjamie if (error != 0 && error != ENOENT) 1902194762Sjamie goto done_deref; 1903194118Sjamie error = vfs_setopts(opts, "host.hostname", pr->pr_hostname); 1904191673Sjamie if (error != 0 && error != ENOENT) 1905191673Sjamie goto done_deref; 1906194118Sjamie error = vfs_setopts(opts, "host.domainname", pr->pr_domainname); 1907193066Sjamie if (error != 0 && error != ENOENT) 1908193066Sjamie goto done_deref; 1909194118Sjamie error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid); 1910193066Sjamie if (error != 0 && error != ENOENT) 1911193066Sjamie goto done_deref; 1912193066Sjamie#ifdef COMPAT_IA32 1913193066Sjamie if (td->td_proc->p_sysent->sv_flags & SV_IA32) { 1914193066Sjamie uint32_t hid32 = pr->pr_hostid; 1915193066Sjamie 1916193066Sjamie error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32)); 1917193066Sjamie } else 1918193066Sjamie#endif 1919193066Sjamie error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid, 1920193066Sjamie sizeof(pr->pr_hostid)); 1921193066Sjamie if (error != 0 && error != ENOENT) 1922193066Sjamie goto done_deref; 1923192895Sjamie error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs, 1924192895Sjamie sizeof(pr->pr_enforce_statfs)); 1925191673Sjamie if (error != 0 && error != ENOENT) 1926191673Sjamie goto done_deref; 1927192895Sjamie for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 1928192895Sjamie fi++) { 1929192895Sjamie if (pr_flag_names[fi] == NULL) 1930192895Sjamie continue; 1931192895Sjamie i = (pr->pr_flags & (1 << fi)) ? 1 : 0; 1932192895Sjamie error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i)); 1933192895Sjamie if (error != 0 && error != ENOENT) 1934192895Sjamie goto done_deref; 1935192895Sjamie i = !i; 1936192895Sjamie error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i)); 1937192895Sjamie if (error != 0 && error != ENOENT) 1938192895Sjamie goto done_deref; 1939192895Sjamie } 1940195870Sjamie for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 1941195870Sjamie fi++) { 1942195870Sjamie i = pr->pr_flags & 1943195870Sjamie (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new); 1944195870Sjamie i = pr_flag_jailsys[fi].disable && 1945195870Sjamie (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE 1946195870Sjamie : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW 1947195870Sjamie : JAIL_SYS_INHERIT; 1948195870Sjamie error = 1949195870Sjamie vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i)); 1950195870Sjamie if (error != 0 && error != ENOENT) 1951195870Sjamie goto done_deref; 1952195870Sjamie } 1953192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 1954192895Sjamie fi++) { 1955192895Sjamie if (pr_allow_names[fi] == NULL) 1956192895Sjamie continue; 1957192895Sjamie i = (pr->pr_allow & (1 << fi)) ? 1 : 0; 1958192895Sjamie error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i)); 1959192895Sjamie if (error != 0 && error != ENOENT) 1960192895Sjamie goto done_deref; 1961192895Sjamie i = !i; 1962192895Sjamie error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i)); 1963192895Sjamie if (error != 0 && error != ENOENT) 1964192895Sjamie goto done_deref; 1965192895Sjamie } 1966191673Sjamie i = (pr->pr_uref == 0); 1967191673Sjamie error = vfs_setopt(opts, "dying", &i, sizeof(i)); 1968191673Sjamie if (error != 0 && error != ENOENT) 1969191673Sjamie goto done_deref; 1970191673Sjamie i = !i; 1971191673Sjamie error = vfs_setopt(opts, "nodying", &i, sizeof(i)); 1972191673Sjamie if (error != 0 && error != ENOENT) 1973191673Sjamie goto done_deref; 1974191673Sjamie 1975191673Sjamie /* Get the module parameters. */ 1976191673Sjamie mtx_unlock(&pr->pr_mtx); 1977191673Sjamie locked = 0; 1978191673Sjamie error = osd_jail_call(pr, PR_METHOD_GET, opts); 197946155Sphk if (error) 1980191673Sjamie goto done_deref; 1981191673Sjamie prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED); 198284828Sjhb 1983191673Sjamie /* By now, all parameters should have been noted. */ 1984191673Sjamie TAILQ_FOREACH(opt, opts, link) { 1985191673Sjamie if (!opt->seen && strcmp(opt->name, "errmsg")) { 1986191673Sjamie error = EINVAL; 1987191673Sjamie vfs_opterror(opts, "unknown parameter: %s", opt->name); 1988191673Sjamie goto done_errmsg; 1989191673Sjamie } 1990185435Sbz } 1991191673Sjamie 1992191673Sjamie /* Write the fetched parameters back to userspace. */ 1993191673Sjamie error = 0; 1994191673Sjamie TAILQ_FOREACH(opt, opts, link) { 1995191673Sjamie if (opt->pos >= 0 && opt->pos != errmsg_pos) { 1996191673Sjamie pos = 2 * opt->pos + 1; 1997191673Sjamie optuio->uio_iov[pos].iov_len = opt->len; 1998191673Sjamie if (opt->value != NULL) { 1999191673Sjamie if (optuio->uio_segflg == UIO_SYSSPACE) { 2000191673Sjamie bcopy(opt->value, 2001191673Sjamie optuio->uio_iov[pos].iov_base, 2002191673Sjamie opt->len); 2003191673Sjamie } else { 2004191673Sjamie error = copyout(opt->value, 2005191673Sjamie optuio->uio_iov[pos].iov_base, 2006191673Sjamie opt->len); 2007191673Sjamie if (error) 2008191673Sjamie break; 2009191673Sjamie } 2010191673Sjamie } 2011191673Sjamie } 2012185435Sbz } 2013191673Sjamie goto done_errmsg; 2014191673Sjamie 2015191673Sjamie done_deref: 2016191673Sjamie prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED); 2017191673Sjamie goto done_errmsg; 2018191673Sjamie 2019191673Sjamie done_unlock_list: 2020191673Sjamie sx_sunlock(&allprison_lock); 2021191673Sjamie done_errmsg: 2022191673Sjamie if (error && errmsg_pos >= 0) { 2023191673Sjamie vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 2024191673Sjamie errmsg_pos = 2 * errmsg_pos + 1; 2025191673Sjamie if (errmsg_len > 0) { 2026191673Sjamie if (optuio->uio_segflg == UIO_SYSSPACE) 2027191673Sjamie bcopy(errmsg, 2028191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 2029191673Sjamie errmsg_len); 2030191673Sjamie else 2031191673Sjamie copyout(errmsg, 2032191673Sjamie optuio->uio_iov[errmsg_pos].iov_base, 2033191673Sjamie errmsg_len); 2034191673Sjamie } 2035185435Sbz } 2036191673Sjamie vfs_freeopts(opts); 2037191673Sjamie return (error); 2038191673Sjamie} 2039113275Smike 2040192895Sjamie 2041191673Sjamie/* 2042191673Sjamie * struct jail_remove_args { 2043191673Sjamie * int jid; 2044191673Sjamie * }; 2045191673Sjamie */ 2046191673Sjamieint 2047191673Sjamiejail_remove(struct thread *td, struct jail_remove_args *uap) 2048191673Sjamie{ 2049192895Sjamie struct prison *pr, *cpr, *lpr, *tpr; 2050192895Sjamie int descend, error; 2051185435Sbz 2052191673Sjamie error = priv_check(td, PRIV_JAIL_REMOVE); 2053185435Sbz if (error) 2054191673Sjamie return (error); 2055185435Sbz 2056185435Sbz sx_xlock(&allprison_lock); 2057192895Sjamie pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2058191673Sjamie if (pr == NULL) { 2059185435Sbz sx_xunlock(&allprison_lock); 2060191673Sjamie return (EINVAL); 2061185435Sbz } 2062185435Sbz 2063192895Sjamie /* Remove all descendants of this prison, then remove this prison. */ 2064192895Sjamie pr->pr_ref++; 2065192895Sjamie pr->pr_flags |= PR_REMOVE; 2066192895Sjamie if (!LIST_EMPTY(&pr->pr_children)) { 2067192895Sjamie mtx_unlock(&pr->pr_mtx); 2068192895Sjamie lpr = NULL; 2069192895Sjamie FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 2070192895Sjamie mtx_lock(&cpr->pr_mtx); 2071192895Sjamie if (cpr->pr_ref > 0) { 2072192895Sjamie tpr = cpr; 2073192895Sjamie cpr->pr_ref++; 2074192895Sjamie cpr->pr_flags |= PR_REMOVE; 2075192895Sjamie } else { 2076192895Sjamie /* Already removed - do not do it again. */ 2077192895Sjamie tpr = NULL; 2078192895Sjamie } 2079192895Sjamie mtx_unlock(&cpr->pr_mtx); 2080192895Sjamie if (lpr != NULL) { 2081192895Sjamie mtx_lock(&lpr->pr_mtx); 2082192895Sjamie prison_remove_one(lpr); 2083192895Sjamie sx_xlock(&allprison_lock); 2084192895Sjamie } 2085192895Sjamie lpr = tpr; 2086192895Sjamie } 2087192895Sjamie if (lpr != NULL) { 2088192895Sjamie mtx_lock(&lpr->pr_mtx); 2089192895Sjamie prison_remove_one(lpr); 2090192895Sjamie sx_xlock(&allprison_lock); 2091192895Sjamie } 2092192895Sjamie mtx_lock(&pr->pr_mtx); 2093192895Sjamie } 2094192895Sjamie prison_remove_one(pr); 2095192895Sjamie return (0); 2096192895Sjamie} 2097192895Sjamie 2098192895Sjamiestatic void 2099192895Sjamieprison_remove_one(struct prison *pr) 2100192895Sjamie{ 2101192895Sjamie struct proc *p; 2102192895Sjamie int deuref; 2103192895Sjamie 2104191673Sjamie /* If the prison was persistent, it is not anymore. */ 2105191673Sjamie deuref = 0; 2106191673Sjamie if (pr->pr_flags & PR_PERSIST) { 2107191673Sjamie pr->pr_ref--; 2108191673Sjamie deuref = PD_DEUREF; 2109191673Sjamie pr->pr_flags &= ~PR_PERSIST; 2110179881Sdelphij } 2111113275Smike 2112192895Sjamie /* 2113192895Sjamie * jail_remove added a reference. If that's the only one, remove 2114192895Sjamie * the prison now. 2115192895Sjamie */ 2116192895Sjamie KASSERT(pr->pr_ref > 0, 2117192895Sjamie ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id)); 2118192895Sjamie if (pr->pr_ref == 1) { 2119191673Sjamie prison_deref(pr, 2120191673Sjamie deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 2121192895Sjamie return; 2122191673Sjamie } 2123191673Sjamie 2124113275Smike mtx_unlock(&pr->pr_mtx); 2125191673Sjamie sx_xunlock(&allprison_lock); 2126191673Sjamie /* 2127191673Sjamie * Kill all processes unfortunate enough to be attached to this prison. 2128191673Sjamie */ 2129191673Sjamie sx_slock(&allproc_lock); 2130191673Sjamie LIST_FOREACH(p, &allproc, p_list) { 2131191673Sjamie PROC_LOCK(p); 2132191673Sjamie if (p->p_state != PRS_NEW && p->p_ucred && 2133191673Sjamie p->p_ucred->cr_prison == pr) 2134191673Sjamie psignal(p, SIGKILL); 2135191673Sjamie PROC_UNLOCK(p); 2136191673Sjamie } 2137191673Sjamie sx_sunlock(&allproc_lock); 2138192895Sjamie /* Remove the temporary reference added by jail_remove. */ 2139191673Sjamie prison_deref(pr, deuref | PD_DEREF); 2140113275Smike} 2141113275Smike 2142190466Sjamie 2143113275Smike/* 2144114168Smike * struct jail_attach_args { 2145114168Smike * int jid; 2146114168Smike * }; 2147113275Smike */ 2148113275Smikeint 2149114168Smikejail_attach(struct thread *td, struct jail_attach_args *uap) 2150113275Smike{ 2151113275Smike struct prison *pr; 2152191673Sjamie int error; 2153167309Spjd 2154164032Srwatson error = priv_check(td, PRIV_JAIL_ATTACH); 2155126023Snectar if (error) 2156126023Snectar return (error); 2157126023Snectar 2158168401Spjd sx_slock(&allprison_lock); 2159192895Sjamie pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2160113275Smike if (pr == NULL) { 2161168401Spjd sx_sunlock(&allprison_lock); 2162113275Smike return (EINVAL); 2163113275Smike } 2164185435Sbz 2165185435Sbz /* 2166185435Sbz * Do not allow a process to attach to a prison that is not 2167191673Sjamie * considered to be "alive". 2168185435Sbz */ 2169191673Sjamie if (pr->pr_uref == 0) { 2170185435Sbz mtx_unlock(&pr->pr_mtx); 2171185435Sbz sx_sunlock(&allprison_lock); 2172185435Sbz return (EINVAL); 2173185435Sbz } 2174191673Sjamie 2175191673Sjamie return (do_jail_attach(td, pr)); 2176191673Sjamie} 2177191673Sjamie 2178191673Sjamiestatic int 2179191673Sjamiedo_jail_attach(struct thread *td, struct prison *pr) 2180191673Sjamie{ 2181192895Sjamie struct prison *ppr; 2182191673Sjamie struct proc *p; 2183191673Sjamie struct ucred *newcred, *oldcred; 2184191673Sjamie int vfslocked, error; 2185191673Sjamie 2186191673Sjamie /* 2187191673Sjamie * XXX: Note that there is a slight race here if two threads 2188191673Sjamie * in the same privileged process attempt to attach to two 2189191673Sjamie * different jails at the same time. It is important for 2190191673Sjamie * user processes not to do this, or they might end up with 2191191673Sjamie * a process root from one prison, but attached to the jail 2192191673Sjamie * of another. 2193191673Sjamie */ 2194113275Smike pr->pr_ref++; 2195191673Sjamie pr->pr_uref++; 2196113275Smike mtx_unlock(&pr->pr_mtx); 2197191673Sjamie 2198191673Sjamie /* Let modules do whatever they need to prepare for attaching. */ 2199191673Sjamie error = osd_jail_call(pr, PR_METHOD_ATTACH, td); 2200191673Sjamie if (error) { 2201191673Sjamie prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED); 2202191673Sjamie return (error); 2203191673Sjamie } 2204168401Spjd sx_sunlock(&allprison_lock); 2205113275Smike 2206185435Sbz /* 2207185435Sbz * Reparent the newly attached process to this jail. 2208185435Sbz */ 2209192895Sjamie ppr = td->td_ucred->cr_prison; 2210191673Sjamie p = td->td_proc; 2211185435Sbz error = cpuset_setproc_update_set(p, pr->pr_cpuset); 2212185435Sbz if (error) 2213191673Sjamie goto e_revert_osd; 2214185435Sbz 2215150652Scsjp vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount); 2216175202Sattilio vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY); 2217113275Smike if ((error = change_dir(pr->pr_root, td)) != 0) 2218113275Smike goto e_unlock; 2219113275Smike#ifdef MAC 2220172930Srwatson if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root))) 2221113275Smike goto e_unlock; 2222113275Smike#endif 2223175294Sattilio VOP_UNLOCK(pr->pr_root, 0); 2224191673Sjamie if ((error = change_root(pr->pr_root, td))) 2225191673Sjamie goto e_unlock_giant; 2226150652Scsjp VFS_UNLOCK_GIANT(vfslocked); 2227113275Smike 222884828Sjhb newcred = crget(); 222984828Sjhb PROC_LOCK(p); 223084828Sjhb oldcred = p->p_ucred; 2231113275Smike setsugid(p); 223284828Sjhb crcopy(newcred, oldcred); 2233113630Sjhb newcred->cr_prison = pr; 223484828Sjhb p->p_ucred = newcred; 223584828Sjhb PROC_UNLOCK(p); 223684828Sjhb crfree(oldcred); 2237192895Sjamie prison_deref(ppr, PD_DEREF | PD_DEUREF); 223846155Sphk return (0); 2239191673Sjamie e_unlock: 2240175294Sattilio VOP_UNLOCK(pr->pr_root, 0); 2241191673Sjamie e_unlock_giant: 2242150652Scsjp VFS_UNLOCK_GIANT(vfslocked); 2243191673Sjamie e_revert_osd: 2244191673Sjamie /* Tell modules this thread is still in its old jail after all. */ 2245192895Sjamie (void)osd_jail_call(ppr, PR_METHOD_ATTACH, td); 2246191673Sjamie prison_deref(pr, PD_DEREF | PD_DEUREF); 224746155Sphk return (error); 224846155Sphk} 224946155Sphk 2250192895Sjamie 2251113275Smike/* 2252113275Smike * Returns a locked prison instance, or NULL on failure. 2253113275Smike */ 2254168399Spjdstruct prison * 2255113275Smikeprison_find(int prid) 2256113275Smike{ 2257113275Smike struct prison *pr; 2258113275Smike 2259168401Spjd sx_assert(&allprison_lock, SX_LOCKED); 2260191673Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) { 2261113275Smike if (pr->pr_id == prid) { 2262113275Smike mtx_lock(&pr->pr_mtx); 2263191673Sjamie if (pr->pr_ref > 0) 2264191673Sjamie return (pr); 2265191673Sjamie mtx_unlock(&pr->pr_mtx); 2266113275Smike } 2267113275Smike } 2268113275Smike return (NULL); 2269113275Smike} 2270113275Smike 2271191673Sjamie/* 2272192895Sjamie * Find a prison that is a descendant of mypr. Returns a locked prison or NULL. 2273191673Sjamie */ 2274191673Sjamiestruct prison * 2275192895Sjamieprison_find_child(struct prison *mypr, int prid) 2276191673Sjamie{ 2277192895Sjamie struct prison *pr; 2278192895Sjamie int descend; 2279192895Sjamie 2280192895Sjamie sx_assert(&allprison_lock, SX_LOCKED); 2281192895Sjamie FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 2282192895Sjamie if (pr->pr_id == prid) { 2283192895Sjamie mtx_lock(&pr->pr_mtx); 2284192895Sjamie if (pr->pr_ref > 0) 2285192895Sjamie return (pr); 2286192895Sjamie mtx_unlock(&pr->pr_mtx); 2287192895Sjamie } 2288192895Sjamie } 2289192895Sjamie return (NULL); 2290192895Sjamie} 2291192895Sjamie 2292192895Sjamie/* 2293192895Sjamie * Look for the name relative to mypr. Returns a locked prison or NULL. 2294192895Sjamie */ 2295192895Sjamiestruct prison * 2296192895Sjamieprison_find_name(struct prison *mypr, const char *name) 2297192895Sjamie{ 2298191673Sjamie struct prison *pr, *deadpr; 2299192895Sjamie size_t mylen; 2300192895Sjamie int descend; 2301191673Sjamie 2302191673Sjamie sx_assert(&allprison_lock, SX_LOCKED); 2303192895Sjamie mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1; 2304191673Sjamie again: 2305191673Sjamie deadpr = NULL; 2306192895Sjamie FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 2307192895Sjamie if (!strcmp(pr->pr_name + mylen, name)) { 2308191673Sjamie mtx_lock(&pr->pr_mtx); 2309191673Sjamie if (pr->pr_ref > 0) { 2310191673Sjamie if (pr->pr_uref > 0) 2311191673Sjamie return (pr); 2312191673Sjamie deadpr = pr; 2313191673Sjamie } 2314191673Sjamie mtx_unlock(&pr->pr_mtx); 2315191673Sjamie } 2316191673Sjamie } 2317192895Sjamie /* There was no valid prison - perhaps there was a dying one. */ 2318191673Sjamie if (deadpr != NULL) { 2319191673Sjamie mtx_lock(&deadpr->pr_mtx); 2320191673Sjamie if (deadpr->pr_ref == 0) { 2321191673Sjamie mtx_unlock(&deadpr->pr_mtx); 2322191673Sjamie goto again; 2323191673Sjamie } 2324191673Sjamie } 2325191673Sjamie return (deadpr); 2326191673Sjamie} 2327191673Sjamie 2328191673Sjamie/* 2329192895Sjamie * See if a prison has the specific flag set. 2330192895Sjamie */ 2331192895Sjamieint 2332192895Sjamieprison_flag(struct ucred *cred, unsigned flag) 2333192895Sjamie{ 2334192895Sjamie 2335192895Sjamie /* This is an atomic read, so no locking is necessary. */ 2336192895Sjamie return (cred->cr_prison->pr_flags & flag); 2337192895Sjamie} 2338192895Sjamie 2339192895Sjamieint 2340192895Sjamieprison_allow(struct ucred *cred, unsigned flag) 2341192895Sjamie{ 2342192895Sjamie 2343192895Sjamie /* This is an atomic read, so no locking is necessary. */ 2344192895Sjamie return (cred->cr_prison->pr_allow & flag); 2345192895Sjamie} 2346192895Sjamie 2347192895Sjamie/* 2348191673Sjamie * Remove a prison reference. If that was the last reference, remove the 2349191673Sjamie * prison itself - but not in this context in case there are locks held. 2350191673Sjamie */ 235172786Srwatsonvoid 2352185029Spjdprison_free_locked(struct prison *pr) 235372786Srwatson{ 235472786Srwatson 2355185029Spjd mtx_assert(&pr->pr_mtx, MA_OWNED); 235672786Srwatson pr->pr_ref--; 235772786Srwatson if (pr->pr_ref == 0) { 2358168483Spjd mtx_unlock(&pr->pr_mtx); 2359124882Srwatson TASK_INIT(&pr->pr_task, 0, prison_complete, pr); 2360144660Sjeff taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 236187275Srwatson return; 236272786Srwatson } 236387275Srwatson mtx_unlock(&pr->pr_mtx); 236472786Srwatson} 236572786Srwatson 2366185029Spjdvoid 2367185029Spjdprison_free(struct prison *pr) 2368185029Spjd{ 2369185029Spjd 2370185029Spjd mtx_lock(&pr->pr_mtx); 2371185029Spjd prison_free_locked(pr); 2372185029Spjd} 2373185029Spjd 2374124882Srwatsonstatic void 2375124882Srwatsonprison_complete(void *context, int pending) 2376124882Srwatson{ 2377191673Sjamie 2378191673Sjamie prison_deref((struct prison *)context, 0); 2379191673Sjamie} 2380191673Sjamie 2381191673Sjamie/* 2382191673Sjamie * Remove a prison reference (usually). This internal version assumes no 2383191673Sjamie * mutexes are held, except perhaps the prison itself. If there are no more 2384191673Sjamie * references, release and delist the prison. On completion, the prison lock 2385191673Sjamie * and the allprison lock are both unlocked. 2386191673Sjamie */ 2387191673Sjamiestatic void 2388191673Sjamieprison_deref(struct prison *pr, int flags) 2389191673Sjamie{ 2390192895Sjamie struct prison *ppr, *tpr; 2391150652Scsjp int vfslocked; 2392124882Srwatson 2393191673Sjamie if (!(flags & PD_LOCKED)) 2394191673Sjamie mtx_lock(&pr->pr_mtx); 2395192895Sjamie /* Decrement the user references in a separate loop. */ 2396191673Sjamie if (flags & PD_DEUREF) { 2397192895Sjamie for (tpr = pr;; tpr = tpr->pr_parent) { 2398192895Sjamie if (tpr != pr) 2399192895Sjamie mtx_lock(&tpr->pr_mtx); 2400192895Sjamie if (--tpr->pr_uref > 0) 2401192895Sjamie break; 2402192895Sjamie KASSERT(tpr != &prison0, ("prison0 pr_uref=0")); 2403192895Sjamie mtx_unlock(&tpr->pr_mtx); 2404192895Sjamie } 2405191673Sjamie /* Done if there were only user references to remove. */ 2406191673Sjamie if (!(flags & PD_DEREF)) { 2407192895Sjamie mtx_unlock(&tpr->pr_mtx); 2408191673Sjamie if (flags & PD_LIST_SLOCKED) 2409191673Sjamie sx_sunlock(&allprison_lock); 2410191673Sjamie else if (flags & PD_LIST_XLOCKED) 2411191673Sjamie sx_xunlock(&allprison_lock); 2412191673Sjamie return; 2413191673Sjamie } 2414192895Sjamie if (tpr != pr) { 2415192895Sjamie mtx_unlock(&tpr->pr_mtx); 2416192895Sjamie mtx_lock(&pr->pr_mtx); 2417192895Sjamie } 2418191673Sjamie } 2419124882Srwatson 2420192895Sjamie for (;;) { 2421192895Sjamie if (flags & PD_DEREF) 2422192895Sjamie pr->pr_ref--; 2423192895Sjamie /* If the prison still has references, nothing else to do. */ 2424192895Sjamie if (pr->pr_ref > 0) { 2425192895Sjamie mtx_unlock(&pr->pr_mtx); 2426192895Sjamie if (flags & PD_LIST_SLOCKED) 2427192895Sjamie sx_sunlock(&allprison_lock); 2428192895Sjamie else if (flags & PD_LIST_XLOCKED) 2429192895Sjamie sx_xunlock(&allprison_lock); 2430192895Sjamie return; 2431191673Sjamie } 2432191673Sjamie 2433192895Sjamie mtx_unlock(&pr->pr_mtx); 2434192895Sjamie if (flags & PD_LIST_SLOCKED) { 2435192895Sjamie if (!sx_try_upgrade(&allprison_lock)) { 2436192895Sjamie sx_sunlock(&allprison_lock); 2437192895Sjamie sx_xlock(&allprison_lock); 2438192895Sjamie } 2439192895Sjamie } else if (!(flags & PD_LIST_XLOCKED)) 2440192895Sjamie sx_xlock(&allprison_lock); 2441168489Spjd 2442192895Sjamie TAILQ_REMOVE(&allprison, pr, pr_list); 2443192895Sjamie LIST_REMOVE(pr, pr_sibling); 2444192895Sjamie ppr = pr->pr_parent; 2445192895Sjamie for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 2446194762Sjamie tpr->pr_childcount--; 2447192895Sjamie sx_downgrade(&allprison_lock); 2448192895Sjamie 2449194251Sjamie#ifdef VIMAGE 2450194251Sjamie if (pr->pr_flags & PR_VNET) 2451194251Sjamie vnet_destroy(pr->pr_vnet); 2452194251Sjamie#endif 2453192895Sjamie if (pr->pr_root != NULL) { 2454192895Sjamie vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount); 2455192895Sjamie vrele(pr->pr_root); 2456192895Sjamie VFS_UNLOCK_GIANT(vfslocked); 2457192895Sjamie } 2458192895Sjamie mtx_destroy(&pr->pr_mtx); 2459191673Sjamie#ifdef INET 2460192895Sjamie free(pr->pr_ip4, M_PRISON); 2461191673Sjamie#endif 2462185435Sbz#ifdef INET6 2463192895Sjamie free(pr->pr_ip6, M_PRISON); 2464185435Sbz#endif 2465192895Sjamie if (pr->pr_cpuset != NULL) 2466192895Sjamie cpuset_rel(pr->pr_cpuset); 2467192895Sjamie osd_jail_exit(pr); 2468192895Sjamie free(pr, M_PRISON); 2469192895Sjamie 2470192895Sjamie /* Removing a prison frees a reference on its parent. */ 2471192895Sjamie pr = ppr; 2472192895Sjamie mtx_lock(&pr->pr_mtx); 2473192895Sjamie flags = PD_DEREF | PD_LIST_SLOCKED; 2474192895Sjamie } 2475124882Srwatson} 2476124882Srwatson 247772786Srwatsonvoid 2478185029Spjdprison_hold_locked(struct prison *pr) 247972786Srwatson{ 248072786Srwatson 2481185029Spjd mtx_assert(&pr->pr_mtx, MA_OWNED); 2482168489Spjd KASSERT(pr->pr_ref > 0, 2483191671Sjamie ("Trying to hold dead prison (jid=%d).", pr->pr_id)); 248472786Srwatson pr->pr_ref++; 2485185029Spjd} 2486185029Spjd 2487185029Spjdvoid 2488185029Spjdprison_hold(struct prison *pr) 2489185029Spjd{ 2490185029Spjd 2491185029Spjd mtx_lock(&pr->pr_mtx); 2492185029Spjd prison_hold_locked(pr); 249387275Srwatson mtx_unlock(&pr->pr_mtx); 249472786Srwatson} 249572786Srwatson 2496185435Sbzvoid 2497185435Sbzprison_proc_hold(struct prison *pr) 249887275Srwatson{ 249987275Srwatson 2500185435Sbz mtx_lock(&pr->pr_mtx); 2501191673Sjamie KASSERT(pr->pr_uref > 0, 2502191673Sjamie ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id)); 2503191673Sjamie pr->pr_uref++; 2504185435Sbz mtx_unlock(&pr->pr_mtx); 250587275Srwatson} 250687275Srwatson 2507185435Sbzvoid 2508185435Sbzprison_proc_free(struct prison *pr) 2509185435Sbz{ 2510185435Sbz 2511185435Sbz mtx_lock(&pr->pr_mtx); 2512191673Sjamie KASSERT(pr->pr_uref > 0, 2513191673Sjamie ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id)); 2514191673Sjamie prison_deref(pr, PD_DEUREF | PD_LOCKED); 2515185435Sbz} 2516185435Sbz 2517185435Sbz 2518185435Sbz#ifdef INET 2519185435Sbz/* 2520192895Sjamie * Restrict a prison's IP address list with its parent's, possibly replacing 2521192895Sjamie * it. Return true if the replacement buffer was used (or would have been). 2522192895Sjamie */ 2523192895Sjamiestatic int 2524192895Sjamieprison_restrict_ip4(struct prison *pr, struct in_addr *newip4) 2525192895Sjamie{ 2526192895Sjamie int ii, ij, used; 2527192895Sjamie struct prison *ppr; 2528192895Sjamie 2529192895Sjamie ppr = pr->pr_parent; 2530192895Sjamie if (!(pr->pr_flags & PR_IP4_USER)) { 2531192895Sjamie /* This has no user settings, so just copy the parent's list. */ 2532192895Sjamie if (pr->pr_ip4s < ppr->pr_ip4s) { 2533192895Sjamie /* 2534192895Sjamie * There's no room for the parent's list. Use the 2535192895Sjamie * new list buffer, which is assumed to be big enough 2536192895Sjamie * (if it was passed). If there's no buffer, try to 2537192895Sjamie * allocate one. 2538192895Sjamie */ 2539192895Sjamie used = 1; 2540192895Sjamie if (newip4 == NULL) { 2541192895Sjamie newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4), 2542192895Sjamie M_PRISON, M_NOWAIT); 2543192895Sjamie if (newip4 != NULL) 2544192895Sjamie used = 0; 2545192895Sjamie } 2546192895Sjamie if (newip4 != NULL) { 2547192895Sjamie bcopy(ppr->pr_ip4, newip4, 2548192895Sjamie ppr->pr_ip4s * sizeof(*newip4)); 2549192895Sjamie free(pr->pr_ip4, M_PRISON); 2550192895Sjamie pr->pr_ip4 = newip4; 2551192895Sjamie pr->pr_ip4s = ppr->pr_ip4s; 2552192895Sjamie } 2553192895Sjamie return (used); 2554192895Sjamie } 2555192895Sjamie pr->pr_ip4s = ppr->pr_ip4s; 2556192895Sjamie if (pr->pr_ip4s > 0) 2557192895Sjamie bcopy(ppr->pr_ip4, pr->pr_ip4, 2558192895Sjamie pr->pr_ip4s * sizeof(*newip4)); 2559192895Sjamie else if (pr->pr_ip4 != NULL) { 2560192895Sjamie free(pr->pr_ip4, M_PRISON); 2561192895Sjamie pr->pr_ip4 = NULL; 2562192895Sjamie } 2563195974Sjamie } else if (pr->pr_ip4s > 0) { 2564192895Sjamie /* Remove addresses that aren't in the parent. */ 2565192895Sjamie for (ij = 0; ij < ppr->pr_ip4s; ij++) 2566192895Sjamie if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 2567192895Sjamie break; 2568192895Sjamie if (ij < ppr->pr_ip4s) 2569192895Sjamie ii = 1; 2570192895Sjamie else { 2571192895Sjamie bcopy(pr->pr_ip4 + 1, pr->pr_ip4, 2572192895Sjamie --pr->pr_ip4s * sizeof(*pr->pr_ip4)); 2573192895Sjamie ii = 0; 2574192895Sjamie } 2575192895Sjamie for (ij = 1; ii < pr->pr_ip4s; ) { 2576192895Sjamie if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) { 2577192895Sjamie ii++; 2578192895Sjamie continue; 2579192895Sjamie } 2580192895Sjamie switch (ij >= ppr->pr_ip4s ? -1 : 2581192895Sjamie qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) { 2582192895Sjamie case -1: 2583192895Sjamie bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii, 2584192895Sjamie (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4)); 2585192895Sjamie break; 2586192895Sjamie case 0: 2587192895Sjamie ii++; 2588192895Sjamie ij++; 2589192895Sjamie break; 2590192895Sjamie case 1: 2591192895Sjamie ij++; 2592192895Sjamie break; 2593192895Sjamie } 2594192895Sjamie } 2595192895Sjamie if (pr->pr_ip4s == 0) { 2596195870Sjamie pr->pr_flags |= PR_IP4_DISABLE; 2597192895Sjamie free(pr->pr_ip4, M_PRISON); 2598192895Sjamie pr->pr_ip4 = NULL; 2599192895Sjamie } 2600192895Sjamie } 2601192895Sjamie return (0); 2602192895Sjamie} 2603192895Sjamie 2604192895Sjamie/* 2605185435Sbz * Pass back primary IPv4 address of this jail. 2606185435Sbz * 2607192895Sjamie * If not restricted return success but do not alter the address. Caller has 2608192895Sjamie * to make sure to initialize it correctly (e.g. INADDR_ANY). 2609185435Sbz * 2610188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4. 2611188144Sjamie * Address returned in NBO. 2612185435Sbz */ 261346155Sphkint 2614187684Sbzprison_get_ip4(struct ucred *cred, struct in_addr *ia) 261546155Sphk{ 2616191673Sjamie struct prison *pr; 261746155Sphk 2618185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2619185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2620185435Sbz 2621192895Sjamie pr = cred->cr_prison; 2622192895Sjamie if (!(pr->pr_flags & PR_IP4)) 262346155Sphk return (0); 2624191673Sjamie mtx_lock(&pr->pr_mtx); 2625192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2626192895Sjamie mtx_unlock(&pr->pr_mtx); 2627192895Sjamie return (0); 2628192895Sjamie } 2629191673Sjamie if (pr->pr_ip4 == NULL) { 2630191673Sjamie mtx_unlock(&pr->pr_mtx); 2631188144Sjamie return (EAFNOSUPPORT); 2632191673Sjamie } 2633185435Sbz 2634191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2635191673Sjamie mtx_unlock(&pr->pr_mtx); 2636185435Sbz return (0); 2637185435Sbz} 2638185435Sbz 2639185435Sbz/* 2640192895Sjamie * Return true if pr1 and pr2 have the same IPv4 address restrictions. 2641192895Sjamie */ 2642192895Sjamieint 2643192895Sjamieprison_equal_ip4(struct prison *pr1, struct prison *pr2) 2644192895Sjamie{ 2645192895Sjamie 2646192895Sjamie if (pr1 == pr2) 2647192895Sjamie return (1); 2648192895Sjamie 2649192895Sjamie /* 2650195974Sjamie * No need to lock since the PR_IP4_USER flag can't be altered for 2651195974Sjamie * existing prisons. 2652192895Sjamie */ 2653195945Sjamie while (pr1 != &prison0 && 2654195945Sjamie#ifdef VIMAGE 2655195945Sjamie !(pr1->pr_flags & PR_VNET) && 2656195945Sjamie#endif 2657195945Sjamie !(pr1->pr_flags & PR_IP4_USER)) 2658192895Sjamie pr1 = pr1->pr_parent; 2659195945Sjamie while (pr2 != &prison0 && 2660195945Sjamie#ifdef VIMAGE 2661195945Sjamie !(pr2->pr_flags & PR_VNET) && 2662195945Sjamie#endif 2663195945Sjamie !(pr2->pr_flags & PR_IP4_USER)) 2664192895Sjamie pr2 = pr2->pr_parent; 2665192895Sjamie return (pr1 == pr2); 2666192895Sjamie} 2667192895Sjamie 2668192895Sjamie/* 2669185435Sbz * Make sure our (source) address is set to something meaningful to this 2670185435Sbz * jail. 2671185435Sbz * 2672192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail, 2673192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 2674192895Sjamie * doesn't allow IPv4. Address passed in in NBO and returned in NBO. 2675185435Sbz */ 2676185435Sbzint 2677185435Sbzprison_local_ip4(struct ucred *cred, struct in_addr *ia) 2678185435Sbz{ 2679191673Sjamie struct prison *pr; 2680185435Sbz struct in_addr ia0; 2681191673Sjamie int error; 2682185435Sbz 2683185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2684185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2685185435Sbz 2686192895Sjamie pr = cred->cr_prison; 2687192895Sjamie if (!(pr->pr_flags & PR_IP4)) 268846155Sphk return (0); 2689191673Sjamie mtx_lock(&pr->pr_mtx); 2690192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2691192895Sjamie mtx_unlock(&pr->pr_mtx); 2692192895Sjamie return (0); 2693192895Sjamie } 2694191673Sjamie if (pr->pr_ip4 == NULL) { 2695191673Sjamie mtx_unlock(&pr->pr_mtx); 2696188144Sjamie return (EAFNOSUPPORT); 2697191673Sjamie } 2698185435Sbz 2699185435Sbz ia0.s_addr = ntohl(ia->s_addr); 2700185435Sbz if (ia0.s_addr == INADDR_LOOPBACK) { 2701191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2702191673Sjamie mtx_unlock(&pr->pr_mtx); 2703185435Sbz return (0); 270446155Sphk } 2705185435Sbz 2706188144Sjamie if (ia0.s_addr == INADDR_ANY) { 2707188144Sjamie /* 2708188144Sjamie * In case there is only 1 IPv4 address, bind directly. 2709188144Sjamie */ 2710191673Sjamie if (pr->pr_ip4s == 1) 2711191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2712191673Sjamie mtx_unlock(&pr->pr_mtx); 2713185435Sbz return (0); 2714185435Sbz } 2715185435Sbz 2716191673Sjamie error = _prison_check_ip4(pr, ia); 2717191673Sjamie mtx_unlock(&pr->pr_mtx); 2718191673Sjamie return (error); 2719185435Sbz} 2720185435Sbz 2721185435Sbz/* 2722185435Sbz * Rewrite destination address in case we will connect to loopback address. 2723185435Sbz * 2724188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4. 2725188144Sjamie * Address passed in in NBO and returned in NBO. 2726185435Sbz */ 2727185435Sbzint 2728185435Sbzprison_remote_ip4(struct ucred *cred, struct in_addr *ia) 2729185435Sbz{ 2730191673Sjamie struct prison *pr; 2731185435Sbz 2732185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2733185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2734185435Sbz 2735192895Sjamie pr = cred->cr_prison; 2736192895Sjamie if (!(pr->pr_flags & PR_IP4)) 2737185435Sbz return (0); 2738191673Sjamie mtx_lock(&pr->pr_mtx); 2739192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2740192895Sjamie mtx_unlock(&pr->pr_mtx); 2741192895Sjamie return (0); 2742192895Sjamie } 2743191673Sjamie if (pr->pr_ip4 == NULL) { 2744191673Sjamie mtx_unlock(&pr->pr_mtx); 2745188144Sjamie return (EAFNOSUPPORT); 2746191673Sjamie } 2747188144Sjamie 2748185435Sbz if (ntohl(ia->s_addr) == INADDR_LOOPBACK) { 2749191673Sjamie ia->s_addr = pr->pr_ip4[0].s_addr; 2750191673Sjamie mtx_unlock(&pr->pr_mtx); 2751185435Sbz return (0); 2752185435Sbz } 2753185435Sbz 2754185435Sbz /* 2755185435Sbz * Return success because nothing had to be changed. 2756185435Sbz */ 2757191673Sjamie mtx_unlock(&pr->pr_mtx); 2758185435Sbz return (0); 2759185435Sbz} 2760185435Sbz 2761185435Sbz/* 2762188144Sjamie * Check if given address belongs to the jail referenced by cred/prison. 2763185435Sbz * 2764192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail, 2765192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 2766192895Sjamie * doesn't allow IPv4. Address passed in in NBO. 2767185435Sbz */ 2768185435Sbzstatic int 2769185435Sbz_prison_check_ip4(struct prison *pr, struct in_addr *ia) 2770185435Sbz{ 2771185435Sbz int i, a, z, d; 2772185435Sbz 2773185435Sbz /* 2774185435Sbz * Check the primary IP. 2775185435Sbz */ 2776185435Sbz if (pr->pr_ip4[0].s_addr == ia->s_addr) 2777188144Sjamie return (0); 2778185435Sbz 2779185435Sbz /* 2780185435Sbz * All the other IPs are sorted so we can do a binary search. 2781185435Sbz */ 2782185435Sbz a = 0; 2783185435Sbz z = pr->pr_ip4s - 2; 2784185435Sbz while (a <= z) { 2785185435Sbz i = (a + z) / 2; 2786185435Sbz d = qcmp_v4(&pr->pr_ip4[i+1], ia); 2787185435Sbz if (d > 0) 2788185435Sbz z = i - 1; 2789185435Sbz else if (d < 0) 2790185435Sbz a = i + 1; 279181114Srwatson else 2792188144Sjamie return (0); 2793185435Sbz } 2794188144Sjamie 2795188144Sjamie return (EADDRNOTAVAIL); 2796185435Sbz} 2797185435Sbz 2798185435Sbzint 2799185435Sbzprison_check_ip4(struct ucred *cred, struct in_addr *ia) 2800185435Sbz{ 2801191673Sjamie struct prison *pr; 2802191673Sjamie int error; 2803185435Sbz 2804185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2805185435Sbz KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2806185435Sbz 2807192895Sjamie pr = cred->cr_prison; 2808192895Sjamie if (!(pr->pr_flags & PR_IP4)) 2809188144Sjamie return (0); 2810191673Sjamie mtx_lock(&pr->pr_mtx); 2811192895Sjamie if (!(pr->pr_flags & PR_IP4)) { 2812192895Sjamie mtx_unlock(&pr->pr_mtx); 2813192895Sjamie return (0); 2814192895Sjamie } 2815191673Sjamie if (pr->pr_ip4 == NULL) { 2816191673Sjamie mtx_unlock(&pr->pr_mtx); 2817188144Sjamie return (EAFNOSUPPORT); 2818191673Sjamie } 2819185435Sbz 2820191673Sjamie error = _prison_check_ip4(pr, ia); 2821191673Sjamie mtx_unlock(&pr->pr_mtx); 2822191673Sjamie return (error); 2823185435Sbz} 2824185435Sbz#endif 2825185435Sbz 2826185435Sbz#ifdef INET6 2827192895Sjamiestatic int 2828192895Sjamieprison_restrict_ip6(struct prison *pr, struct in6_addr *newip6) 2829192895Sjamie{ 2830192895Sjamie int ii, ij, used; 2831192895Sjamie struct prison *ppr; 2832192895Sjamie 2833192895Sjamie ppr = pr->pr_parent; 2834192895Sjamie if (!(pr->pr_flags & PR_IP6_USER)) { 2835192895Sjamie /* This has no user settings, so just copy the parent's list. */ 2836192895Sjamie if (pr->pr_ip6s < ppr->pr_ip6s) { 2837192895Sjamie /* 2838192895Sjamie * There's no room for the parent's list. Use the 2839192895Sjamie * new list buffer, which is assumed to be big enough 2840192895Sjamie * (if it was passed). If there's no buffer, try to 2841192895Sjamie * allocate one. 2842192895Sjamie */ 2843192895Sjamie used = 1; 2844192895Sjamie if (newip6 == NULL) { 2845192895Sjamie newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6), 2846192895Sjamie M_PRISON, M_NOWAIT); 2847192895Sjamie if (newip6 != NULL) 2848192895Sjamie used = 0; 2849192895Sjamie } 2850192895Sjamie if (newip6 != NULL) { 2851192895Sjamie bcopy(ppr->pr_ip6, newip6, 2852192895Sjamie ppr->pr_ip6s * sizeof(*newip6)); 2853192895Sjamie free(pr->pr_ip6, M_PRISON); 2854192895Sjamie pr->pr_ip6 = newip6; 2855192895Sjamie pr->pr_ip6s = ppr->pr_ip6s; 2856192895Sjamie } 2857192895Sjamie return (used); 2858192895Sjamie } 2859192895Sjamie pr->pr_ip6s = ppr->pr_ip6s; 2860192895Sjamie if (pr->pr_ip6s > 0) 2861192895Sjamie bcopy(ppr->pr_ip6, pr->pr_ip6, 2862192895Sjamie pr->pr_ip6s * sizeof(*newip6)); 2863192895Sjamie else if (pr->pr_ip6 != NULL) { 2864192895Sjamie free(pr->pr_ip6, M_PRISON); 2865192895Sjamie pr->pr_ip6 = NULL; 2866192895Sjamie } 2867195974Sjamie } else if (pr->pr_ip6s > 0) { 2868192895Sjamie /* Remove addresses that aren't in the parent. */ 2869192895Sjamie for (ij = 0; ij < ppr->pr_ip6s; ij++) 2870192895Sjamie if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], 2871192895Sjamie &ppr->pr_ip6[ij])) 2872192895Sjamie break; 2873192895Sjamie if (ij < ppr->pr_ip6s) 2874192895Sjamie ii = 1; 2875192895Sjamie else { 2876192895Sjamie bcopy(pr->pr_ip6 + 1, pr->pr_ip6, 2877192895Sjamie --pr->pr_ip6s * sizeof(*pr->pr_ip6)); 2878192895Sjamie ii = 0; 2879192895Sjamie } 2880192895Sjamie for (ij = 1; ii < pr->pr_ip6s; ) { 2881192895Sjamie if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii], 2882192895Sjamie &ppr->pr_ip6[0])) { 2883192895Sjamie ii++; 2884192895Sjamie continue; 2885192895Sjamie } 2886192895Sjamie switch (ij >= ppr->pr_ip4s ? -1 : 2887192895Sjamie qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) { 2888192895Sjamie case -1: 2889192895Sjamie bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii, 2890192895Sjamie (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6)); 2891192895Sjamie break; 2892192895Sjamie case 0: 2893192895Sjamie ii++; 2894192895Sjamie ij++; 2895192895Sjamie break; 2896192895Sjamie case 1: 2897192895Sjamie ij++; 2898192895Sjamie break; 2899192895Sjamie } 2900192895Sjamie } 2901192895Sjamie if (pr->pr_ip6s == 0) { 2902195870Sjamie pr->pr_flags |= PR_IP6_DISABLE; 2903192895Sjamie free(pr->pr_ip6, M_PRISON); 2904192895Sjamie pr->pr_ip6 = NULL; 2905192895Sjamie } 2906192895Sjamie } 2907192895Sjamie return 0; 2908192895Sjamie} 2909192895Sjamie 2910185435Sbz/* 2911185435Sbz * Pass back primary IPv6 address for this jail. 2912185435Sbz * 2913192895Sjamie * If not restricted return success but do not alter the address. Caller has 2914192895Sjamie * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT). 2915185435Sbz * 2916188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6. 2917185435Sbz */ 2918185435Sbzint 2919187684Sbzprison_get_ip6(struct ucred *cred, struct in6_addr *ia6) 2920185435Sbz{ 2921191673Sjamie struct prison *pr; 2922185435Sbz 2923185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2924185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 2925185435Sbz 2926192895Sjamie pr = cred->cr_prison; 2927192895Sjamie if (!(pr->pr_flags & PR_IP6)) 292881114Srwatson return (0); 2929191673Sjamie mtx_lock(&pr->pr_mtx); 2930192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 2931192895Sjamie mtx_unlock(&pr->pr_mtx); 2932192895Sjamie return (0); 2933192895Sjamie } 2934191673Sjamie if (pr->pr_ip6 == NULL) { 2935191673Sjamie mtx_unlock(&pr->pr_mtx); 2936188144Sjamie return (EAFNOSUPPORT); 2937191673Sjamie } 2938188144Sjamie 2939191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 2940191673Sjamie mtx_unlock(&pr->pr_mtx); 2941185435Sbz return (0); 2942185435Sbz} 2943185435Sbz 2944185435Sbz/* 2945192895Sjamie * Return true if pr1 and pr2 have the same IPv6 address restrictions. 2946192895Sjamie */ 2947192895Sjamieint 2948192895Sjamieprison_equal_ip6(struct prison *pr1, struct prison *pr2) 2949192895Sjamie{ 2950192895Sjamie 2951192895Sjamie if (pr1 == pr2) 2952192895Sjamie return (1); 2953192895Sjamie 2954195945Sjamie while (pr1 != &prison0 && 2955195945Sjamie#ifdef VIMAGE 2956195945Sjamie !(pr1->pr_flags & PR_VNET) && 2957195945Sjamie#endif 2958195945Sjamie !(pr1->pr_flags & PR_IP6_USER)) 2959192895Sjamie pr1 = pr1->pr_parent; 2960195945Sjamie while (pr2 != &prison0 && 2961195945Sjamie#ifdef VIMAGE 2962195945Sjamie !(pr2->pr_flags & PR_VNET) && 2963195945Sjamie#endif 2964195945Sjamie !(pr2->pr_flags & PR_IP6_USER)) 2965192895Sjamie pr2 = pr2->pr_parent; 2966192895Sjamie return (pr1 == pr2); 2967192895Sjamie} 2968192895Sjamie 2969192895Sjamie/* 2970185435Sbz * Make sure our (source) address is set to something meaningful to this jail. 2971185435Sbz * 2972185435Sbz * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0) 2973185435Sbz * when needed while binding. 2974185435Sbz * 2975192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail, 2976192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 2977192895Sjamie * doesn't allow IPv6. 2978185435Sbz */ 2979185435Sbzint 2980185435Sbzprison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only) 2981185435Sbz{ 2982191673Sjamie struct prison *pr; 2983191673Sjamie int error; 2984185435Sbz 2985185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2986185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 2987185435Sbz 2988192895Sjamie pr = cred->cr_prison; 2989192895Sjamie if (!(pr->pr_flags & PR_IP6)) 2990185435Sbz return (0); 2991191673Sjamie mtx_lock(&pr->pr_mtx); 2992192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 2993192895Sjamie mtx_unlock(&pr->pr_mtx); 2994192895Sjamie return (0); 2995192895Sjamie } 2996191673Sjamie if (pr->pr_ip6 == NULL) { 2997191673Sjamie mtx_unlock(&pr->pr_mtx); 2998188144Sjamie return (EAFNOSUPPORT); 2999191673Sjamie } 3000188144Sjamie 3001185435Sbz if (IN6_IS_ADDR_LOOPBACK(ia6)) { 3002191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3003191673Sjamie mtx_unlock(&pr->pr_mtx); 3004185435Sbz return (0); 300581114Srwatson } 3006185435Sbz 3007188144Sjamie if (IN6_IS_ADDR_UNSPECIFIED(ia6)) { 3008188144Sjamie /* 3009188144Sjamie * In case there is only 1 IPv6 address, and v6only is true, 3010188144Sjamie * then bind directly. 3011188144Sjamie */ 3012191673Sjamie if (v6only != 0 && pr->pr_ip6s == 1) 3013191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3014191673Sjamie mtx_unlock(&pr->pr_mtx); 3015185435Sbz return (0); 3016185435Sbz } 3017188144Sjamie 3018191673Sjamie error = _prison_check_ip6(pr, ia6); 3019191673Sjamie mtx_unlock(&pr->pr_mtx); 3020191673Sjamie return (error); 3021185435Sbz} 3022185435Sbz 3023185435Sbz/* 3024185435Sbz * Rewrite destination address in case we will connect to loopback address. 3025185435Sbz * 3026188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6. 3027185435Sbz */ 3028185435Sbzint 3029185435Sbzprison_remote_ip6(struct ucred *cred, struct in6_addr *ia6) 3030185435Sbz{ 3031191673Sjamie struct prison *pr; 3032185435Sbz 3033185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3034185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3035185435Sbz 3036192895Sjamie pr = cred->cr_prison; 3037192895Sjamie if (!(pr->pr_flags & PR_IP6)) 3038185435Sbz return (0); 3039191673Sjamie mtx_lock(&pr->pr_mtx); 3040192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3041192895Sjamie mtx_unlock(&pr->pr_mtx); 3042192895Sjamie return (0); 3043192895Sjamie } 3044191673Sjamie if (pr->pr_ip6 == NULL) { 3045191673Sjamie mtx_unlock(&pr->pr_mtx); 3046188144Sjamie return (EAFNOSUPPORT); 3047191673Sjamie } 3048188144Sjamie 3049185435Sbz if (IN6_IS_ADDR_LOOPBACK(ia6)) { 3050191673Sjamie bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3051191673Sjamie mtx_unlock(&pr->pr_mtx); 3052185435Sbz return (0); 3053185435Sbz } 3054185435Sbz 3055185435Sbz /* 3056185435Sbz * Return success because nothing had to be changed. 3057185435Sbz */ 3058191673Sjamie mtx_unlock(&pr->pr_mtx); 305946155Sphk return (0); 306046155Sphk} 306146155Sphk 3062185435Sbz/* 3063188144Sjamie * Check if given address belongs to the jail referenced by cred/prison. 3064185435Sbz * 3065192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail, 3066192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 3067192895Sjamie * doesn't allow IPv6. 3068185435Sbz */ 3069185435Sbzstatic int 3070185435Sbz_prison_check_ip6(struct prison *pr, struct in6_addr *ia6) 307146155Sphk{ 3072185435Sbz int i, a, z, d; 307346155Sphk 3074185435Sbz /* 3075185435Sbz * Check the primary IP. 3076185435Sbz */ 3077185435Sbz if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6)) 3078188144Sjamie return (0); 3079185435Sbz 3080185435Sbz /* 3081185435Sbz * All the other IPs are sorted so we can do a binary search. 3082185435Sbz */ 3083185435Sbz a = 0; 3084185435Sbz z = pr->pr_ip6s - 2; 3085185435Sbz while (a <= z) { 3086185435Sbz i = (a + z) / 2; 3087185435Sbz d = qcmp_v6(&pr->pr_ip6[i+1], ia6); 3088185435Sbz if (d > 0) 3089185435Sbz z = i - 1; 3090185435Sbz else if (d < 0) 3091185435Sbz a = i + 1; 309246155Sphk else 3093188144Sjamie return (0); 309446155Sphk } 3095188144Sjamie 3096188144Sjamie return (EADDRNOTAVAIL); 309746155Sphk} 309846155Sphk 309946155Sphkint 3100185435Sbzprison_check_ip6(struct ucred *cred, struct in6_addr *ia6) 3101185435Sbz{ 3102191673Sjamie struct prison *pr; 3103191673Sjamie int error; 3104185435Sbz 3105185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3106185435Sbz KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3107185435Sbz 3108192895Sjamie pr = cred->cr_prison; 3109192895Sjamie if (!(pr->pr_flags & PR_IP6)) 3110188144Sjamie return (0); 3111191673Sjamie mtx_lock(&pr->pr_mtx); 3112192895Sjamie if (!(pr->pr_flags & PR_IP6)) { 3113192895Sjamie mtx_unlock(&pr->pr_mtx); 3114192895Sjamie return (0); 3115192895Sjamie } 3116191673Sjamie if (pr->pr_ip6 == NULL) { 3117191673Sjamie mtx_unlock(&pr->pr_mtx); 3118188144Sjamie return (EAFNOSUPPORT); 3119191673Sjamie } 3120185435Sbz 3121191673Sjamie error = _prison_check_ip6(pr, ia6); 3122191673Sjamie mtx_unlock(&pr->pr_mtx); 3123191673Sjamie return (error); 3124185435Sbz} 3125185435Sbz#endif 3126185435Sbz 3127185435Sbz/* 3128188146Sjamie * Check if a jail supports the given address family. 3129188146Sjamie * 3130188146Sjamie * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT 3131188146Sjamie * if not. 3132188146Sjamie */ 3133188146Sjamieint 3134188146Sjamieprison_check_af(struct ucred *cred, int af) 3135188146Sjamie{ 3136192895Sjamie struct prison *pr; 3137188146Sjamie int error; 3138188146Sjamie 3139188146Sjamie KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3140188146Sjamie 3141192895Sjamie pr = cred->cr_prison; 3142194923Sjamie#ifdef VIMAGE 3143194915Sjamie /* Prisons with their own network stack are not limited. */ 3144194915Sjamie if (pr->pr_flags & PR_VNET) 3145194915Sjamie return (0); 3146194923Sjamie#endif 3147194915Sjamie 3148188146Sjamie error = 0; 3149188146Sjamie switch (af) 3150188146Sjamie { 3151188146Sjamie#ifdef INET 3152188146Sjamie case AF_INET: 3153192895Sjamie if (pr->pr_flags & PR_IP4) 3154192895Sjamie { 3155192895Sjamie mtx_lock(&pr->pr_mtx); 3156192895Sjamie if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL) 3157192895Sjamie error = EAFNOSUPPORT; 3158192895Sjamie mtx_unlock(&pr->pr_mtx); 3159192895Sjamie } 3160188146Sjamie break; 3161188146Sjamie#endif 3162188146Sjamie#ifdef INET6 3163188146Sjamie case AF_INET6: 3164192895Sjamie if (pr->pr_flags & PR_IP6) 3165192895Sjamie { 3166192895Sjamie mtx_lock(&pr->pr_mtx); 3167192895Sjamie if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL) 3168192895Sjamie error = EAFNOSUPPORT; 3169192895Sjamie mtx_unlock(&pr->pr_mtx); 3170192895Sjamie } 3171188146Sjamie break; 3172188146Sjamie#endif 3173188146Sjamie case AF_LOCAL: 3174188146Sjamie case AF_ROUTE: 3175188146Sjamie break; 3176188146Sjamie default: 3177192895Sjamie if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF)) 3178188146Sjamie error = EAFNOSUPPORT; 3179188146Sjamie } 3180188146Sjamie return (error); 3181188146Sjamie} 3182188146Sjamie 3183188146Sjamie/* 3184185435Sbz * Check if given address belongs to the jail referenced by cred (wrapper to 3185185435Sbz * prison_check_ip[46]). 3186185435Sbz * 3187192895Sjamie * Returns 0 if jail doesn't restrict the address family or if address belongs 3188192895Sjamie * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if 3189192895Sjamie * the jail doesn't allow the address family. IPv4 Address passed in in NBO. 3190185435Sbz */ 3191185435Sbzint 319272786Srwatsonprison_if(struct ucred *cred, struct sockaddr *sa) 319346155Sphk{ 3194185435Sbz#ifdef INET 3195114168Smike struct sockaddr_in *sai; 3196185435Sbz#endif 3197185435Sbz#ifdef INET6 3198185435Sbz struct sockaddr_in6 *sai6; 3199185435Sbz#endif 3200188144Sjamie int error; 320146155Sphk 3202185435Sbz KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3203185435Sbz KASSERT(sa != NULL, ("%s: sa is NULL", __func__)); 3204185435Sbz 3205188144Sjamie error = 0; 3206188144Sjamie switch (sa->sa_family) 3207185435Sbz { 3208185435Sbz#ifdef INET 3209185435Sbz case AF_INET: 3210185435Sbz sai = (struct sockaddr_in *)sa; 3211188144Sjamie error = prison_check_ip4(cred, &sai->sin_addr); 3212185435Sbz break; 3213185435Sbz#endif 3214185435Sbz#ifdef INET6 3215185435Sbz case AF_INET6: 3216185435Sbz sai6 = (struct sockaddr_in6 *)sa; 3217188144Sjamie error = prison_check_ip6(cred, &sai6->sin6_addr); 3218185435Sbz break; 3219185435Sbz#endif 3220185435Sbz default: 3221192895Sjamie if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF)) 3222188144Sjamie error = EAFNOSUPPORT; 3223185435Sbz } 3224188144Sjamie return (error); 322546155Sphk} 322672786Srwatson 322772786Srwatson/* 322872786Srwatson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH. 322972786Srwatson */ 323072786Srwatsonint 3231114168Smikeprison_check(struct ucred *cred1, struct ucred *cred2) 323272786Srwatson{ 323372786Srwatson 3234192895Sjamie return ((cred1->cr_prison == cred2->cr_prison || 3235192895Sjamie prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH); 3236192895Sjamie} 323772786Srwatson 3238192895Sjamie/* 3239192895Sjamie * Return 1 if p2 is a child of p1, otherwise 0. 3240192895Sjamie */ 3241192895Sjamieint 3242192895Sjamieprison_ischild(struct prison *pr1, struct prison *pr2) 3243192895Sjamie{ 3244192895Sjamie 3245192895Sjamie for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent) 3246192895Sjamie if (pr1 == pr2) 3247192895Sjamie return (1); 324872786Srwatson return (0); 324972786Srwatson} 325072786Srwatson 325172786Srwatson/* 325272786Srwatson * Return 1 if the passed credential is in a jail, otherwise 0. 325372786Srwatson */ 325472786Srwatsonint 3255114168Smikejailed(struct ucred *cred) 325672786Srwatson{ 325772786Srwatson 3258192895Sjamie return (cred->cr_prison != &prison0); 325972786Srwatson} 326091384Srobert 326191384Srobert/* 3262194090Sjamie * Return the correct hostname (domainname, et al) for the passed credential. 326391384Srobert */ 326491391Srobertvoid 3265114168Smikegetcredhostname(struct ucred *cred, char *buf, size_t size) 326691384Srobert{ 3267193066Sjamie struct prison *pr; 326891384Srobert 3269194090Sjamie /* 3270194090Sjamie * A NULL credential can be used to shortcut to the physical 3271194090Sjamie * system's hostname. 3272194090Sjamie */ 3273193066Sjamie pr = (cred != NULL) ? cred->cr_prison : &prison0; 3274193066Sjamie mtx_lock(&pr->pr_mtx); 3275194118Sjamie strlcpy(buf, pr->pr_hostname, size); 3276193066Sjamie mtx_unlock(&pr->pr_mtx); 327791384Srobert} 3278113275Smike 3279194090Sjamievoid 3280194090Sjamiegetcreddomainname(struct ucred *cred, char *buf, size_t size) 3281194090Sjamie{ 3282194090Sjamie 3283194090Sjamie mtx_lock(&cred->cr_prison->pr_mtx); 3284194118Sjamie strlcpy(buf, cred->cr_prison->pr_domainname, size); 3285194090Sjamie mtx_unlock(&cred->cr_prison->pr_mtx); 3286194090Sjamie} 3287194090Sjamie 3288194090Sjamievoid 3289194090Sjamiegetcredhostuuid(struct ucred *cred, char *buf, size_t size) 3290194090Sjamie{ 3291194090Sjamie 3292194090Sjamie mtx_lock(&cred->cr_prison->pr_mtx); 3293194118Sjamie strlcpy(buf, cred->cr_prison->pr_hostuuid, size); 3294194090Sjamie mtx_unlock(&cred->cr_prison->pr_mtx); 3295194090Sjamie} 3296194090Sjamie 3297194090Sjamievoid 3298194090Sjamiegetcredhostid(struct ucred *cred, unsigned long *hostid) 3299194090Sjamie{ 3300194090Sjamie 3301194090Sjamie mtx_lock(&cred->cr_prison->pr_mtx); 3302194090Sjamie *hostid = cred->cr_prison->pr_hostid; 3303194090Sjamie mtx_unlock(&cred->cr_prison->pr_mtx); 3304194090Sjamie} 3305194090Sjamie 3306125804Srwatson/* 3307147185Spjd * Determine whether the subject represented by cred can "see" 3308147185Spjd * status of a mount point. 3309147185Spjd * Returns: 0 for permitted, ENOENT otherwise. 3310147185Spjd * XXX: This function should be called cr_canseemount() and should be 3311147185Spjd * placed in kern_prot.c. 3312125804Srwatson */ 3313125804Srwatsonint 3314147185Spjdprison_canseemount(struct ucred *cred, struct mount *mp) 3315125804Srwatson{ 3316147185Spjd struct prison *pr; 3317147185Spjd struct statfs *sp; 3318147185Spjd size_t len; 3319125804Srwatson 3320192895Sjamie pr = cred->cr_prison; 3321192895Sjamie if (pr->pr_enforce_statfs == 0) 3322147185Spjd return (0); 3323147185Spjd if (pr->pr_root->v_mount == mp) 3324147185Spjd return (0); 3325192895Sjamie if (pr->pr_enforce_statfs == 2) 3326147185Spjd return (ENOENT); 3327147185Spjd /* 3328147185Spjd * If jail's chroot directory is set to "/" we should be able to see 3329147185Spjd * all mount-points from inside a jail. 3330147185Spjd * This is ugly check, but this is the only situation when jail's 3331147185Spjd * directory ends with '/'. 3332147185Spjd */ 3333147185Spjd if (strcmp(pr->pr_path, "/") == 0) 3334147185Spjd return (0); 3335147185Spjd len = strlen(pr->pr_path); 3336147185Spjd sp = &mp->mnt_stat; 3337147185Spjd if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0) 3338147185Spjd return (ENOENT); 3339147185Spjd /* 3340147185Spjd * Be sure that we don't have situation where jail's root directory 3341147185Spjd * is "/some/path" and mount point is "/some/pathpath". 3342147185Spjd */ 3343147185Spjd if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/') 3344147185Spjd return (ENOENT); 3345147185Spjd return (0); 3346147185Spjd} 3347147185Spjd 3348147185Spjdvoid 3349147185Spjdprison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp) 3350147185Spjd{ 3351147185Spjd char jpath[MAXPATHLEN]; 3352147185Spjd struct prison *pr; 3353147185Spjd size_t len; 3354147185Spjd 3355192895Sjamie pr = cred->cr_prison; 3356192895Sjamie if (pr->pr_enforce_statfs == 0) 3357147185Spjd return; 3358147185Spjd if (prison_canseemount(cred, mp) != 0) { 3359147185Spjd bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3360147185Spjd strlcpy(sp->f_mntonname, "[restricted]", 3361147185Spjd sizeof(sp->f_mntonname)); 3362147185Spjd return; 3363125804Srwatson } 3364147185Spjd if (pr->pr_root->v_mount == mp) { 3365147185Spjd /* 3366147185Spjd * Clear current buffer data, so we are sure nothing from 3367147185Spjd * the valid path left there. 3368147185Spjd */ 3369147185Spjd bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3370147185Spjd *sp->f_mntonname = '/'; 3371147185Spjd return; 3372147185Spjd } 3373147185Spjd /* 3374147185Spjd * If jail's chroot directory is set to "/" we should be able to see 3375147185Spjd * all mount-points from inside a jail. 3376147185Spjd */ 3377147185Spjd if (strcmp(pr->pr_path, "/") == 0) 3378147185Spjd return; 3379147185Spjd len = strlen(pr->pr_path); 3380147185Spjd strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath)); 3381147185Spjd /* 3382147185Spjd * Clear current buffer data, so we are sure nothing from 3383147185Spjd * the valid path left there. 3384147185Spjd */ 3385147185Spjd bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3386147185Spjd if (*jpath == '\0') { 3387147185Spjd /* Should never happen. */ 3388147185Spjd *sp->f_mntonname = '/'; 3389147185Spjd } else { 3390147185Spjd strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname)); 3391147185Spjd } 3392125804Srwatson} 3393125804Srwatson 3394164032Srwatson/* 3395164032Srwatson * Check with permission for a specific privilege is granted within jail. We 3396164032Srwatson * have a specific list of accepted privileges; the rest are denied. 3397164032Srwatson */ 3398164032Srwatsonint 3399164032Srwatsonprison_priv_check(struct ucred *cred, int priv) 3400164032Srwatson{ 3401164032Srwatson 3402164032Srwatson if (!jailed(cred)) 3403164032Srwatson return (0); 3404164032Srwatson 3405194915Sjamie#ifdef VIMAGE 3406194915Sjamie /* 3407194915Sjamie * Privileges specific to prisons with a virtual network stack. 3408194915Sjamie * There might be a duplicate entry here in case the privilege 3409194915Sjamie * is only granted conditionally in the legacy jail case. 3410194915Sjamie */ 3411164032Srwatson switch (priv) { 3412194915Sjamie#ifdef notyet 3413194915Sjamie /* 3414194915Sjamie * NFS-specific privileges. 3415194915Sjamie */ 3416194915Sjamie case PRIV_NFS_DAEMON: 3417194915Sjamie case PRIV_NFS_LOCKD: 3418194915Sjamie#endif 3419194915Sjamie /* 3420194915Sjamie * Network stack privileges. 3421194915Sjamie */ 3422194915Sjamie case PRIV_NET_BRIDGE: 3423194915Sjamie case PRIV_NET_GRE: 3424194915Sjamie case PRIV_NET_BPF: 3425194915Sjamie case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */ 3426194915Sjamie case PRIV_NET_ROUTE: 3427194915Sjamie case PRIV_NET_TAP: 3428194915Sjamie case PRIV_NET_SETIFMTU: 3429194915Sjamie case PRIV_NET_SETIFFLAGS: 3430194915Sjamie case PRIV_NET_SETIFCAP: 3431194915Sjamie case PRIV_NET_SETIFNAME : 3432194915Sjamie case PRIV_NET_SETIFMETRIC: 3433194915Sjamie case PRIV_NET_SETIFPHYS: 3434194915Sjamie case PRIV_NET_SETIFMAC: 3435194915Sjamie case PRIV_NET_ADDMULTI: 3436194915Sjamie case PRIV_NET_DELMULTI: 3437194915Sjamie case PRIV_NET_HWIOCTL: 3438194915Sjamie case PRIV_NET_SETLLADDR: 3439194915Sjamie case PRIV_NET_ADDIFGROUP: 3440194915Sjamie case PRIV_NET_DELIFGROUP: 3441194915Sjamie case PRIV_NET_IFCREATE: 3442194915Sjamie case PRIV_NET_IFDESTROY: 3443194915Sjamie case PRIV_NET_ADDIFADDR: 3444194915Sjamie case PRIV_NET_DELIFADDR: 3445194915Sjamie case PRIV_NET_LAGG: 3446194915Sjamie case PRIV_NET_GIF: 3447194915Sjamie case PRIV_NET_SETIFVNET: 3448164032Srwatson 3449164032Srwatson /* 3450194915Sjamie * 802.11-related privileges. 3451194915Sjamie */ 3452194915Sjamie case PRIV_NET80211_GETKEY: 3453194915Sjamie#ifdef notyet 3454194915Sjamie case PRIV_NET80211_MANAGE: /* XXX-BZ discuss with sam@ */ 3455194915Sjamie#endif 3456194915Sjamie 3457194915Sjamie#ifdef notyet 3458194915Sjamie /* 3459194915Sjamie * AppleTalk privileges. 3460194915Sjamie */ 3461194915Sjamie case PRIV_NETATALK_RESERVEDPORT: 3462194915Sjamie 3463194915Sjamie /* 3464194915Sjamie * ATM privileges. 3465194915Sjamie */ 3466194915Sjamie case PRIV_NETATM_CFG: 3467194915Sjamie case PRIV_NETATM_ADD: 3468194915Sjamie case PRIV_NETATM_DEL: 3469194915Sjamie case PRIV_NETATM_SET: 3470194915Sjamie 3471194915Sjamie /* 3472194915Sjamie * Bluetooth privileges. 3473194915Sjamie */ 3474194915Sjamie case PRIV_NETBLUETOOTH_RAW: 3475194915Sjamie#endif 3476194915Sjamie 3477194915Sjamie /* 3478194915Sjamie * Netgraph and netgraph module privileges. 3479194915Sjamie */ 3480194915Sjamie case PRIV_NETGRAPH_CONTROL: 3481194915Sjamie#ifdef notyet 3482194915Sjamie case PRIV_NETGRAPH_TTY: 3483194915Sjamie#endif 3484194915Sjamie 3485194915Sjamie /* 3486194915Sjamie * IPv4 and IPv6 privileges. 3487194915Sjamie */ 3488194915Sjamie case PRIV_NETINET_IPFW: 3489194915Sjamie case PRIV_NETINET_DIVERT: 3490194915Sjamie case PRIV_NETINET_PF: 3491194915Sjamie case PRIV_NETINET_DUMMYNET: 3492194915Sjamie case PRIV_NETINET_CARP: 3493194915Sjamie case PRIV_NETINET_MROUTE: 3494194915Sjamie case PRIV_NETINET_RAW: 3495194915Sjamie case PRIV_NETINET_ADDRCTRL6: 3496194915Sjamie case PRIV_NETINET_ND6: 3497194915Sjamie case PRIV_NETINET_SCOPE6: 3498194915Sjamie case PRIV_NETINET_ALIFETIME6: 3499194915Sjamie case PRIV_NETINET_IPSEC: 3500194915Sjamie case PRIV_NETINET_BINDANY: 3501194915Sjamie 3502194915Sjamie#ifdef notyet 3503194915Sjamie /* 3504194915Sjamie * IPX/SPX privileges. 3505194915Sjamie */ 3506194915Sjamie case PRIV_NETIPX_RESERVEDPORT: 3507194915Sjamie case PRIV_NETIPX_RAW: 3508194915Sjamie 3509194915Sjamie /* 3510194915Sjamie * NCP privileges. 3511194915Sjamie */ 3512194915Sjamie case PRIV_NETNCP: 3513194915Sjamie 3514194915Sjamie /* 3515194915Sjamie * SMB privileges. 3516194915Sjamie */ 3517194915Sjamie case PRIV_NETSMB: 3518194915Sjamie#endif 3519194915Sjamie 3520194915Sjamie /* 3521194915Sjamie * No default: or deny here. 3522194915Sjamie * In case of no permit fall through to next switch(). 3523194915Sjamie */ 3524194915Sjamie if (cred->cr_prison->pr_flags & PR_VNET) 3525194915Sjamie return (0); 3526194915Sjamie } 3527194915Sjamie#endif /* VIMAGE */ 3528194915Sjamie 3529194915Sjamie switch (priv) { 3530194915Sjamie 3531194915Sjamie /* 3532164032Srwatson * Allow ktrace privileges for root in jail. 3533164032Srwatson */ 3534164032Srwatson case PRIV_KTRACE: 3535164032Srwatson 3536166827Srwatson#if 0 3537164032Srwatson /* 3538164032Srwatson * Allow jailed processes to configure audit identity and 3539164032Srwatson * submit audit records (login, etc). In the future we may 3540164032Srwatson * want to further refine the relationship between audit and 3541164032Srwatson * jail. 3542164032Srwatson */ 3543164032Srwatson case PRIV_AUDIT_GETAUDIT: 3544164032Srwatson case PRIV_AUDIT_SETAUDIT: 3545164032Srwatson case PRIV_AUDIT_SUBMIT: 3546166827Srwatson#endif 3547164032Srwatson 3548164032Srwatson /* 3549164032Srwatson * Allow jailed processes to manipulate process UNIX 3550164032Srwatson * credentials in any way they see fit. 3551164032Srwatson */ 3552164032Srwatson case PRIV_CRED_SETUID: 3553164032Srwatson case PRIV_CRED_SETEUID: 3554164032Srwatson case PRIV_CRED_SETGID: 3555164032Srwatson case PRIV_CRED_SETEGID: 3556164032Srwatson case PRIV_CRED_SETGROUPS: 3557164032Srwatson case PRIV_CRED_SETREUID: 3558164032Srwatson case PRIV_CRED_SETREGID: 3559164032Srwatson case PRIV_CRED_SETRESUID: 3560164032Srwatson case PRIV_CRED_SETRESGID: 3561164032Srwatson 3562164032Srwatson /* 3563164032Srwatson * Jail implements visibility constraints already, so allow 3564164032Srwatson * jailed root to override uid/gid-based constraints. 3565164032Srwatson */ 3566164032Srwatson case PRIV_SEEOTHERGIDS: 3567164032Srwatson case PRIV_SEEOTHERUIDS: 3568164032Srwatson 3569164032Srwatson /* 3570164032Srwatson * Jail implements inter-process debugging limits already, so 3571164032Srwatson * allow jailed root various debugging privileges. 3572164032Srwatson */ 3573164032Srwatson case PRIV_DEBUG_DIFFCRED: 3574164032Srwatson case PRIV_DEBUG_SUGID: 3575164032Srwatson case PRIV_DEBUG_UNPRIV: 3576164032Srwatson 3577164032Srwatson /* 3578164032Srwatson * Allow jail to set various resource limits and login 3579164032Srwatson * properties, and for now, exceed process resource limits. 3580164032Srwatson */ 3581164032Srwatson case PRIV_PROC_LIMIT: 3582164032Srwatson case PRIV_PROC_SETLOGIN: 3583164032Srwatson case PRIV_PROC_SETRLIMIT: 3584164032Srwatson 3585164032Srwatson /* 3586164032Srwatson * System V and POSIX IPC privileges are granted in jail. 3587164032Srwatson */ 3588164032Srwatson case PRIV_IPC_READ: 3589164032Srwatson case PRIV_IPC_WRITE: 3590164032Srwatson case PRIV_IPC_ADMIN: 3591164032Srwatson case PRIV_IPC_MSGSIZE: 3592164032Srwatson case PRIV_MQ_ADMIN: 3593164032Srwatson 3594164032Srwatson /* 3595192895Sjamie * Jail operations within a jail work on child jails. 3596192895Sjamie */ 3597192895Sjamie case PRIV_JAIL_ATTACH: 3598192895Sjamie case PRIV_JAIL_SET: 3599192895Sjamie case PRIV_JAIL_REMOVE: 3600192895Sjamie 3601192895Sjamie /* 3602164032Srwatson * Jail implements its own inter-process limits, so allow 3603164032Srwatson * root processes in jail to change scheduling on other 3604164032Srwatson * processes in the same jail. Likewise for signalling. 3605164032Srwatson */ 3606164032Srwatson case PRIV_SCHED_DIFFCRED: 3607185435Sbz case PRIV_SCHED_CPUSET: 3608164032Srwatson case PRIV_SIGNAL_DIFFCRED: 3609164032Srwatson case PRIV_SIGNAL_SUGID: 3610164032Srwatson 3611164032Srwatson /* 3612164032Srwatson * Allow jailed processes to write to sysctls marked as jail 3613164032Srwatson * writable. 3614164032Srwatson */ 3615164032Srwatson case PRIV_SYSCTL_WRITEJAIL: 3616164032Srwatson 3617164032Srwatson /* 3618164032Srwatson * Allow root in jail to manage a variety of quota 3619166831Srwatson * properties. These should likely be conditional on a 3620166831Srwatson * configuration option. 3621164032Srwatson */ 3622166832Srwatson case PRIV_VFS_GETQUOTA: 3623166832Srwatson case PRIV_VFS_SETQUOTA: 3624164032Srwatson 3625164032Srwatson /* 3626164032Srwatson * Since Jail relies on chroot() to implement file system 3627164032Srwatson * protections, grant many VFS privileges to root in jail. 3628164032Srwatson * Be careful to exclude mount-related and NFS-related 3629164032Srwatson * privileges. 3630164032Srwatson */ 3631164032Srwatson case PRIV_VFS_READ: 3632164032Srwatson case PRIV_VFS_WRITE: 3633164032Srwatson case PRIV_VFS_ADMIN: 3634164032Srwatson case PRIV_VFS_EXEC: 3635164032Srwatson case PRIV_VFS_LOOKUP: 3636164032Srwatson case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */ 3637164032Srwatson case PRIV_VFS_CHFLAGS_DEV: 3638164032Srwatson case PRIV_VFS_CHOWN: 3639164032Srwatson case PRIV_VFS_CHROOT: 3640167152Spjd case PRIV_VFS_RETAINSUGID: 3641164032Srwatson case PRIV_VFS_FCHROOT: 3642164032Srwatson case PRIV_VFS_LINK: 3643164032Srwatson case PRIV_VFS_SETGID: 3644172860Srwatson case PRIV_VFS_STAT: 3645164032Srwatson case PRIV_VFS_STICKYFILE: 3646164032Srwatson return (0); 3647164032Srwatson 3648164032Srwatson /* 3649164032Srwatson * Depending on the global setting, allow privilege of 3650164032Srwatson * setting system flags. 3651164032Srwatson */ 3652164032Srwatson case PRIV_VFS_SYSFLAGS: 3653192895Sjamie if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS) 3654164032Srwatson return (0); 3655164032Srwatson else 3656164032Srwatson return (EPERM); 3657164032Srwatson 3658164032Srwatson /* 3659168396Spjd * Depending on the global setting, allow privilege of 3660168396Spjd * mounting/unmounting file systems. 3661168396Spjd */ 3662168396Spjd case PRIV_VFS_MOUNT: 3663168396Spjd case PRIV_VFS_UNMOUNT: 3664168396Spjd case PRIV_VFS_MOUNT_NONUSER: 3665168699Spjd case PRIV_VFS_MOUNT_OWNER: 3666192895Sjamie if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT) 3667168396Spjd return (0); 3668168396Spjd else 3669168396Spjd return (EPERM); 3670168396Spjd 3671168396Spjd /* 3672168591Srwatson * Allow jailed root to bind reserved ports and reuse in-use 3673168591Srwatson * ports. 3674164032Srwatson */ 3675164032Srwatson case PRIV_NETINET_RESERVEDPORT: 3676168591Srwatson case PRIV_NETINET_REUSEPORT: 3677164032Srwatson return (0); 3678164032Srwatson 3679164032Srwatson /* 3680175630Sbz * Allow jailed root to set certian IPv4/6 (option) headers. 3681175630Sbz */ 3682175630Sbz case PRIV_NETINET_SETHDROPTS: 3683175630Sbz return (0); 3684175630Sbz 3685175630Sbz /* 3686164032Srwatson * Conditionally allow creating raw sockets in jail. 3687164032Srwatson */ 3688164032Srwatson case PRIV_NETINET_RAW: 3689192895Sjamie if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS) 3690164032Srwatson return (0); 3691164032Srwatson else 3692164032Srwatson return (EPERM); 3693164032Srwatson 3694164032Srwatson /* 3695164032Srwatson * Since jail implements its own visibility limits on netstat 3696164032Srwatson * sysctls, allow getcred. This allows identd to work in 3697164032Srwatson * jail. 3698164032Srwatson */ 3699164032Srwatson case PRIV_NETINET_GETCRED: 3700164032Srwatson return (0); 3701164032Srwatson 3702164032Srwatson default: 3703164032Srwatson /* 3704164032Srwatson * In all remaining cases, deny the privilege request. This 3705164032Srwatson * includes almost all network privileges, many system 3706164032Srwatson * configuration privileges. 3707164032Srwatson */ 3708164032Srwatson return (EPERM); 3709164032Srwatson } 3710164032Srwatson} 3711164032Srwatson 3712192895Sjamie/* 3713192895Sjamie * Return the part of pr2's name that is relative to pr1, or the whole name 3714192895Sjamie * if it does not directly follow. 3715192895Sjamie */ 3716192895Sjamie 3717192895Sjamiechar * 3718192895Sjamieprison_name(struct prison *pr1, struct prison *pr2) 3719192895Sjamie{ 3720192895Sjamie char *name; 3721192895Sjamie 3722192895Sjamie /* Jails see themselves as "0" (if they see themselves at all). */ 3723192895Sjamie if (pr1 == pr2) 3724192895Sjamie return "0"; 3725192895Sjamie name = pr2->pr_name; 3726192895Sjamie if (prison_ischild(pr1, pr2)) { 3727192895Sjamie /* 3728192895Sjamie * pr1 isn't locked (and allprison_lock may not be either) 3729192895Sjamie * so its length can't be counted on. But the number of dots 3730192895Sjamie * can be counted on - and counted. 3731192895Sjamie */ 3732192895Sjamie for (; pr1 != &prison0; pr1 = pr1->pr_parent) 3733192895Sjamie name = strchr(name, '.') + 1; 3734192895Sjamie } 3735192895Sjamie return (name); 3736192895Sjamie} 3737192895Sjamie 3738192895Sjamie/* 3739192895Sjamie * Return the part of pr2's path that is relative to pr1, or the whole path 3740192895Sjamie * if it does not directly follow. 3741192895Sjamie */ 3742192895Sjamiestatic char * 3743192895Sjamieprison_path(struct prison *pr1, struct prison *pr2) 3744192895Sjamie{ 3745192895Sjamie char *path1, *path2; 3746192895Sjamie int len1; 3747192895Sjamie 3748192895Sjamie path1 = pr1->pr_path; 3749192895Sjamie path2 = pr2->pr_path; 3750192895Sjamie if (!strcmp(path1, "/")) 3751192895Sjamie return (path2); 3752192895Sjamie len1 = strlen(path1); 3753192895Sjamie if (strncmp(path1, path2, len1)) 3754192895Sjamie return (path2); 3755192895Sjamie if (path2[len1] == '\0') 3756192895Sjamie return "/"; 3757192895Sjamie if (path2[len1] == '/') 3758192895Sjamie return (path2 + len1); 3759192895Sjamie return (path2); 3760192895Sjamie} 3761192895Sjamie 3762192895Sjamie 3763192895Sjamie/* 3764192895Sjamie * Jail-related sysctls. 3765192895Sjamie */ 3766192895SjamieSYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0, 3767192895Sjamie "Jails"); 3768192895Sjamie 3769113275Smikestatic int 3770113275Smikesysctl_jail_list(SYSCTL_HANDLER_ARGS) 3771113275Smike{ 3772191673Sjamie struct xprison *xp; 3773192895Sjamie struct prison *pr, *cpr; 3774191673Sjamie#ifdef INET 3775191673Sjamie struct in_addr *ip4 = NULL; 3776191673Sjamie int ip4s = 0; 3777191673Sjamie#endif 3778191673Sjamie#ifdef INET6 3779191673Sjamie struct in_addr *ip6 = NULL; 3780191673Sjamie int ip6s = 0; 3781191673Sjamie#endif 3782192895Sjamie int descend, error; 3783113275Smike 3784191673Sjamie xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK); 3785192895Sjamie pr = req->td->td_ucred->cr_prison; 3786191673Sjamie error = 0; 3787168401Spjd sx_slock(&allprison_lock); 3788192895Sjamie FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 3789192895Sjamie#if defined(INET) || defined(INET6) 3790191673Sjamie again: 3791192895Sjamie#endif 3792192895Sjamie mtx_lock(&cpr->pr_mtx); 3793185435Sbz#ifdef INET 3794192895Sjamie if (cpr->pr_ip4s > 0) { 3795192895Sjamie if (ip4s < cpr->pr_ip4s) { 3796192895Sjamie ip4s = cpr->pr_ip4s; 3797192895Sjamie mtx_unlock(&cpr->pr_mtx); 3798191673Sjamie ip4 = realloc(ip4, ip4s * 3799191673Sjamie sizeof(struct in_addr), M_TEMP, M_WAITOK); 3800191673Sjamie goto again; 3801191673Sjamie } 3802192895Sjamie bcopy(cpr->pr_ip4, ip4, 3803192895Sjamie cpr->pr_ip4s * sizeof(struct in_addr)); 3804191673Sjamie } 3805185435Sbz#endif 3806185435Sbz#ifdef INET6 3807192895Sjamie if (cpr->pr_ip6s > 0) { 3808192895Sjamie if (ip6s < cpr->pr_ip6s) { 3809192895Sjamie ip6s = cpr->pr_ip6s; 3810192895Sjamie mtx_unlock(&cpr->pr_mtx); 3811191673Sjamie ip6 = realloc(ip6, ip6s * 3812191673Sjamie sizeof(struct in6_addr), M_TEMP, M_WAITOK); 3813191673Sjamie goto again; 3814191673Sjamie } 3815192895Sjamie bcopy(cpr->pr_ip6, ip6, 3816192895Sjamie cpr->pr_ip6s * sizeof(struct in6_addr)); 3817191673Sjamie } 3818185435Sbz#endif 3819192895Sjamie if (cpr->pr_ref == 0) { 3820192895Sjamie mtx_unlock(&cpr->pr_mtx); 3821191673Sjamie continue; 3822191673Sjamie } 3823191673Sjamie bzero(xp, sizeof(*xp)); 3824113275Smike xp->pr_version = XPRISON_VERSION; 3825192895Sjamie xp->pr_id = cpr->pr_id; 3826192895Sjamie xp->pr_state = cpr->pr_uref > 0 3827191673Sjamie ? PRISON_STATE_ALIVE : PRISON_STATE_DYING; 3828192895Sjamie strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path)); 3829194118Sjamie strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host)); 3830192895Sjamie strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name)); 3831185435Sbz#ifdef INET 3832192895Sjamie xp->pr_ip4s = cpr->pr_ip4s; 3833185435Sbz#endif 3834185435Sbz#ifdef INET6 3835192895Sjamie xp->pr_ip6s = cpr->pr_ip6s; 3836185435Sbz#endif 3837192895Sjamie mtx_unlock(&cpr->pr_mtx); 3838191673Sjamie error = SYSCTL_OUT(req, xp, sizeof(*xp)); 3839191673Sjamie if (error) 3840191673Sjamie break; 3841185435Sbz#ifdef INET 3842191673Sjamie if (xp->pr_ip4s > 0) { 3843191673Sjamie error = SYSCTL_OUT(req, ip4, 3844191673Sjamie xp->pr_ip4s * sizeof(struct in_addr)); 3845191673Sjamie if (error) 3846191673Sjamie break; 3847185435Sbz } 3848185435Sbz#endif 3849185435Sbz#ifdef INET6 3850191673Sjamie if (xp->pr_ip6s > 0) { 3851191673Sjamie error = SYSCTL_OUT(req, ip6, 3852191673Sjamie xp->pr_ip6s * sizeof(struct in6_addr)); 3853191673Sjamie if (error) 3854191673Sjamie break; 3855185435Sbz } 3856185435Sbz#endif 3857113275Smike } 3858168401Spjd sx_sunlock(&allprison_lock); 3859191673Sjamie free(xp, M_TEMP); 3860191673Sjamie#ifdef INET 3861191673Sjamie free(ip4, M_TEMP); 3862191673Sjamie#endif 3863191673Sjamie#ifdef INET6 3864191673Sjamie free(ip6, M_TEMP); 3865191673Sjamie#endif 3866167354Spjd return (error); 3867113275Smike} 3868113275Smike 3869187864SedSYSCTL_OID(_security_jail, OID_AUTO, list, 3870187864Sed CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 3871187864Sed sysctl_jail_list, "S", "List of active jails"); 3872126004Spjd 3873126004Spjdstatic int 3874126004Spjdsysctl_jail_jailed(SYSCTL_HANDLER_ARGS) 3875126004Spjd{ 3876126004Spjd int error, injail; 3877126004Spjd 3878126004Spjd injail = jailed(req->td->td_ucred); 3879126004Spjd error = SYSCTL_OUT(req, &injail, sizeof(injail)); 3880126004Spjd 3881126004Spjd return (error); 3882126004Spjd} 3883192895Sjamie 3884187864SedSYSCTL_PROC(_security_jail, OID_AUTO, jailed, 3885187864Sed CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 3886187864Sed sysctl_jail_jailed, "I", "Process in jail?"); 3887185435Sbz 3888192895Sjamie#if defined(INET) || defined(INET6) 3889193865SjamieSYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW, 3890192895Sjamie &jail_max_af_ips, 0, 3891192895Sjamie "Number of IP addresses a jail may have at most per address family"); 3892192895Sjamie#endif 3893192895Sjamie 3894192895Sjamie/* 3895192895Sjamie * Default parameters for jail(2) compatability. For historical reasons, 3896192895Sjamie * the sysctl names have varying similarity to the parameter names. Prisons 3897192895Sjamie * just see their own parameters, and can't change them. 3898192895Sjamie */ 3899192895Sjamiestatic int 3900192895Sjamiesysctl_jail_default_allow(SYSCTL_HANDLER_ARGS) 3901192895Sjamie{ 3902192895Sjamie struct prison *pr; 3903192895Sjamie int allow, error, i; 3904192895Sjamie 3905192895Sjamie pr = req->td->td_ucred->cr_prison; 3906192895Sjamie allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow; 3907192895Sjamie 3908192895Sjamie /* Get the current flag value, and convert it to a boolean. */ 3909192895Sjamie i = (allow & arg2) ? 1 : 0; 3910192895Sjamie if (arg1 != NULL) 3911192895Sjamie i = !i; 3912192895Sjamie error = sysctl_handle_int(oidp, &i, 0, req); 3913192895Sjamie if (error || !req->newptr) 3914192895Sjamie return (error); 3915192895Sjamie i = i ? arg2 : 0; 3916192895Sjamie if (arg1 != NULL) 3917192895Sjamie i ^= arg2; 3918192895Sjamie /* 3919192895Sjamie * The sysctls don't have CTLFLAGS_PRISON, so assume prison0 3920192895Sjamie * for writing. 3921192895Sjamie */ 3922192895Sjamie mtx_lock(&prison0.pr_mtx); 3923192895Sjamie jail_default_allow = (jail_default_allow & ~arg2) | i; 3924192895Sjamie mtx_unlock(&prison0.pr_mtx); 3925192895Sjamie return (0); 3926192895Sjamie} 3927192895Sjamie 3928192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed, 3929192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 3930192895Sjamie NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I", 3931192895Sjamie "Processes in jail can set their hostnames"); 3932192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only, 3933192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 3934192895Sjamie (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I", 3935192895Sjamie "Processes in jail are limited to creating UNIX/IP/route sockets only"); 3936192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed, 3937192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 3938192895Sjamie NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I", 3939192895Sjamie "Processes in jail can use System V IPC primitives"); 3940192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets, 3941192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 3942192895Sjamie NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I", 3943192895Sjamie "Prison root can create raw sockets"); 3944192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed, 3945192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 3946192895Sjamie NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I", 3947192895Sjamie "Processes in jail can alter system file flags"); 3948192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed, 3949192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 3950192895Sjamie NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I", 3951192895Sjamie "Processes in jail can mount/unmount jail-friendly file systems"); 3952192895Sjamie 3953192895Sjamiestatic int 3954192895Sjamiesysctl_jail_default_level(SYSCTL_HANDLER_ARGS) 3955192895Sjamie{ 3956192895Sjamie struct prison *pr; 3957192895Sjamie int level, error; 3958192895Sjamie 3959192895Sjamie pr = req->td->td_ucred->cr_prison; 3960192895Sjamie level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2); 3961192895Sjamie error = sysctl_handle_int(oidp, &level, 0, req); 3962192895Sjamie if (error || !req->newptr) 3963192895Sjamie return (error); 3964192895Sjamie *(int *)arg1 = level; 3965192895Sjamie return (0); 3966192895Sjamie} 3967192895Sjamie 3968192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs, 3969192895Sjamie CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 3970192895Sjamie &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs), 3971192895Sjamie sysctl_jail_default_level, "I", 3972192895Sjamie "Processes in jail cannot see all mounted file systems"); 3973192895Sjamie 3974192895Sjamie/* 3975192895Sjamie * Nodes to describe jail parameters. Maximum length of string parameters 3976192895Sjamie * is returned in the string itself, and the other parameters exist merely 3977192895Sjamie * to make themselves and their types known. 3978192895Sjamie */ 3979192895SjamieSYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0, 3980192895Sjamie "Jail parameters"); 3981192895Sjamie 3982192895Sjamieint 3983192895Sjamiesysctl_jail_param(SYSCTL_HANDLER_ARGS) 3984192895Sjamie{ 3985192895Sjamie int i; 3986192895Sjamie long l; 3987192895Sjamie size_t s; 3988192895Sjamie char numbuf[12]; 3989192895Sjamie 3990192895Sjamie switch (oidp->oid_kind & CTLTYPE) 3991192895Sjamie { 3992192895Sjamie case CTLTYPE_LONG: 3993192895Sjamie case CTLTYPE_ULONG: 3994192895Sjamie l = 0; 3995192895Sjamie#ifdef SCTL_MASK32 3996192895Sjamie if (!(req->flags & SCTL_MASK32)) 3997192895Sjamie#endif 3998192895Sjamie return (SYSCTL_OUT(req, &l, sizeof(l))); 3999192895Sjamie case CTLTYPE_INT: 4000192895Sjamie case CTLTYPE_UINT: 4001192895Sjamie i = 0; 4002192895Sjamie return (SYSCTL_OUT(req, &i, sizeof(i))); 4003192895Sjamie case CTLTYPE_STRING: 4004192895Sjamie snprintf(numbuf, sizeof(numbuf), "%d", arg2); 4005192895Sjamie return 4006192895Sjamie (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req)); 4007192895Sjamie case CTLTYPE_STRUCT: 4008192895Sjamie s = (size_t)arg2; 4009192895Sjamie return (SYSCTL_OUT(req, &s, sizeof(s))); 4010192895Sjamie } 4011192895Sjamie return (0); 4012192895Sjamie} 4013192895Sjamie 4014192895SjamieSYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID"); 4015192895SjamieSYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID"); 4016192895SjamieSYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name"); 4017192895SjamieSYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path"); 4018192895SjamieSYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW, 4019192895Sjamie "I", "Jail secure level"); 4020192895SjamieSYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW, 4021192895Sjamie "I", "Jail cannot see all mounted file systems"); 4022192895SjamieSYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW, 4023192895Sjamie "B", "Jail persistence"); 4024194251Sjamie#ifdef VIMAGE 4025194251SjamieSYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN, 4026195870Sjamie "E,jailsys", "Virtual network stack"); 4027194251Sjamie#endif 4028192895SjamieSYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD, 4029192895Sjamie "B", "Jail is in the process of shutting down"); 4030192895Sjamie 4031194762SjamieSYSCTL_JAIL_PARAM_NODE(children, "Number of child jails"); 4032194762SjamieSYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD, 4033194762Sjamie "I", "Current number of child jails"); 4034194762SjamieSYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW, 4035194762Sjamie "I", "Maximum number of child jails"); 4036194762Sjamie 4037195870SjamieSYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info"); 4038192895SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN, 4039192895Sjamie "Jail hostname"); 4040193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN, 4041193066Sjamie "Jail NIS domainname"); 4042193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN, 4043193066Sjamie "Jail host UUID"); 4044193066SjamieSYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW, 4045193066Sjamie "LU", "Jail host ID"); 4046192895Sjamie 4047192895SjamieSYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset"); 4048192895SjamieSYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID"); 4049192895Sjamie 4050192895Sjamie#ifdef INET 4051195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN, 4052195974Sjamie "Jail IPv4 address virtualization"); 4053192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr), 4054192895Sjamie "S,in_addr,a", "Jail IPv4 addresses"); 4055192895Sjamie#endif 4056192895Sjamie#ifdef INET6 4057195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN, 4058195974Sjamie "Jail IPv6 address virtualization"); 4059192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr), 4060192895Sjamie "S,in6_addr,a", "Jail IPv6 addresses"); 4061192895Sjamie#endif 4062192895Sjamie 4063192895SjamieSYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags"); 4064192895SjamieSYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW, 4065192895Sjamie "B", "Jail may set hostname"); 4066192895SjamieSYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW, 4067192895Sjamie "B", "Jail may use SYSV IPC"); 4068192895SjamieSYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW, 4069192895Sjamie "B", "Jail may create raw sockets"); 4070192895SjamieSYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW, 4071192895Sjamie "B", "Jail may alter system file flags"); 4072192895SjamieSYSCTL_JAIL_PARAM(_allow, mount, CTLTYPE_INT | CTLFLAG_RW, 4073192895Sjamie "B", "Jail may mount/unmount jail-friendly file systems"); 4074192895SjamieSYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW, 4075192895Sjamie "B", "Jail may set file quotas"); 4076192895SjamieSYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW, 4077192895Sjamie "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route"); 4078192895Sjamie 4079192895Sjamie 4080185435Sbz#ifdef DDB 4081191673Sjamie 4082191673Sjamiestatic void 4083191673Sjamiedb_show_prison(struct prison *pr) 4084185435Sbz{ 4085192895Sjamie int fi; 4086191673Sjamie#if defined(INET) || defined(INET6) 4087191673Sjamie int ii; 4088185435Sbz#endif 4089195870Sjamie unsigned jsf; 4090185435Sbz#ifdef INET6 4091185435Sbz char ip6buf[INET6_ADDRSTRLEN]; 4092185435Sbz#endif 4093185435Sbz 4094191673Sjamie db_printf("prison %p:\n", pr); 4095191673Sjamie db_printf(" jid = %d\n", pr->pr_id); 4096191673Sjamie db_printf(" name = %s\n", pr->pr_name); 4097192895Sjamie db_printf(" parent = %p\n", pr->pr_parent); 4098191673Sjamie db_printf(" ref = %d\n", pr->pr_ref); 4099191673Sjamie db_printf(" uref = %d\n", pr->pr_uref); 4100191673Sjamie db_printf(" path = %s\n", pr->pr_path); 4101191673Sjamie db_printf(" cpuset = %d\n", pr->pr_cpuset 4102191673Sjamie ? pr->pr_cpuset->cs_id : -1); 4103194251Sjamie#ifdef VIMAGE 4104194251Sjamie db_printf(" vnet = %p\n", pr->pr_vnet); 4105194251Sjamie#endif 4106191673Sjamie db_printf(" root = %p\n", pr->pr_root); 4107191673Sjamie db_printf(" securelevel = %d\n", pr->pr_securelevel); 4108194762Sjamie db_printf(" childcount = %d\n", pr->pr_childcount); 4109192895Sjamie db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children)); 4110192895Sjamie db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling)); 4111191673Sjamie db_printf(" flags = %x", pr->pr_flags); 4112192895Sjamie for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 4113192895Sjamie fi++) 4114192895Sjamie if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi))) 4115192895Sjamie db_printf(" %s", pr_flag_names[fi]); 4116195870Sjamie for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 4117195870Sjamie fi++) { 4118195870Sjamie jsf = pr->pr_flags & 4119195870Sjamie (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new); 4120195870Sjamie db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name, 4121195870Sjamie pr_flag_jailsys[fi].disable && 4122195870Sjamie (jsf == pr_flag_jailsys[fi].disable) ? "disable" 4123195870Sjamie : (jsf == pr_flag_jailsys[fi].new) ? "new" 4124195870Sjamie : "inherit"); 4125195870Sjamie } 4126192895Sjamie db_printf(" allow = %x", pr->pr_allow); 4127192895Sjamie for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 4128192895Sjamie fi++) 4129192895Sjamie if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi))) 4130192895Sjamie db_printf(" %s", pr_allow_names[fi]); 4131191673Sjamie db_printf("\n"); 4132192895Sjamie db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs); 4133194118Sjamie db_printf(" host.hostname = %s\n", pr->pr_hostname); 4134194118Sjamie db_printf(" host.domainname = %s\n", pr->pr_domainname); 4135194118Sjamie db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid); 4136193066Sjamie db_printf(" host.hostid = %lu\n", pr->pr_hostid); 4137185435Sbz#ifdef INET 4138191673Sjamie db_printf(" ip4s = %d\n", pr->pr_ip4s); 4139191673Sjamie for (ii = 0; ii < pr->pr_ip4s; ii++) 4140191673Sjamie db_printf(" %s %s\n", 4141191673Sjamie ii == 0 ? "ip4 =" : " ", 4142191673Sjamie inet_ntoa(pr->pr_ip4[ii])); 4143185435Sbz#endif 4144185435Sbz#ifdef INET6 4145191673Sjamie db_printf(" ip6s = %d\n", pr->pr_ip6s); 4146191673Sjamie for (ii = 0; ii < pr->pr_ip6s; ii++) 4147191673Sjamie db_printf(" %s %s\n", 4148191673Sjamie ii == 0 ? "ip6 =" : " ", 4149191673Sjamie ip6_sprintf(ip6buf, &pr->pr_ip6[ii])); 4150191673Sjamie#endif 4151191673Sjamie} 4152191673Sjamie 4153191673SjamieDB_SHOW_COMMAND(prison, db_show_prison_command) 4154191673Sjamie{ 4155191673Sjamie struct prison *pr; 4156191673Sjamie 4157191673Sjamie if (!have_addr) { 4158192895Sjamie /* 4159192895Sjamie * Show all prisons in the list, and prison0 which is not 4160192895Sjamie * listed. 4161192895Sjamie */ 4162192895Sjamie db_show_prison(&prison0); 4163192895Sjamie if (!db_pager_quit) { 4164192895Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) { 4165192895Sjamie db_show_prison(pr); 4166192895Sjamie if (db_pager_quit) 4167192895Sjamie break; 4168192895Sjamie } 4169191673Sjamie } 4170191673Sjamie return; 4171191673Sjamie } 4172191673Sjamie 4173192895Sjamie if (addr == 0) 4174192895Sjamie pr = &prison0; 4175192895Sjamie else { 4176192895Sjamie /* Look for a prison with the ID and with references. */ 4177191673Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) 4178192895Sjamie if (pr->pr_id == addr && pr->pr_ref > 0) 4179191673Sjamie break; 4180192895Sjamie if (pr == NULL) 4181192895Sjamie /* Look again, without requiring a reference. */ 4182192895Sjamie TAILQ_FOREACH(pr, &allprison, pr_list) 4183192895Sjamie if (pr->pr_id == addr) 4184192895Sjamie break; 4185192895Sjamie if (pr == NULL) 4186192895Sjamie /* Assume address points to a valid prison. */ 4187192895Sjamie pr = (struct prison *)addr; 4188192895Sjamie } 4189191673Sjamie db_show_prison(pr); 4190185435Sbz} 4191191673Sjamie 4192185435Sbz#endif /* DDB */ 4193