kern_jail.c revision 202123
1/*- 2 * Copyright (c) 1999 Poul-Henning Kamp. 3 * Copyright (c) 2008 Bjoern A. Zeeb. 4 * Copyright (c) 2009 James Gritton. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD: head/sys/kern/kern_jail.c 202123 2010-01-11 22:34:25Z bz $"); 31 32#include "opt_compat.h" 33#include "opt_ddb.h" 34#include "opt_inet.h" 35#include "opt_inet6.h" 36 37#include <sys/param.h> 38#include <sys/types.h> 39#include <sys/kernel.h> 40#include <sys/systm.h> 41#include <sys/errno.h> 42#include <sys/sysproto.h> 43#include <sys/malloc.h> 44#include <sys/osd.h> 45#include <sys/priv.h> 46#include <sys/proc.h> 47#include <sys/taskqueue.h> 48#include <sys/fcntl.h> 49#include <sys/jail.h> 50#include <sys/lock.h> 51#include <sys/mutex.h> 52#include <sys/sx.h> 53#include <sys/sysent.h> 54#include <sys/namei.h> 55#include <sys/mount.h> 56#include <sys/queue.h> 57#include <sys/socket.h> 58#include <sys/syscallsubr.h> 59#include <sys/sysctl.h> 60#include <sys/vnode.h> 61 62#include <net/if.h> 63#include <net/vnet.h> 64 65#include <netinet/in.h> 66 67#ifdef DDB 68#include <ddb/ddb.h> 69#ifdef INET6 70#include <netinet6/in6_var.h> 71#endif /* INET6 */ 72#endif /* DDB */ 73 74#include <security/mac/mac_framework.h> 75 76#define DEFAULT_HOSTUUID "00000000-0000-0000-0000-000000000000" 77 78MALLOC_DEFINE(M_PRISON, "prison", "Prison structures"); 79 80/* prison0 describes what is "real" about the system. */ 81struct prison prison0 = { 82 .pr_id = 0, 83 .pr_name = "0", 84 .pr_ref = 1, 85 .pr_uref = 1, 86 .pr_path = "/", 87 .pr_securelevel = -1, 88 .pr_childmax = JAIL_MAX, 89 .pr_hostuuid = DEFAULT_HOSTUUID, 90 .pr_children = LIST_HEAD_INITIALIZER(prison0.pr_children), 91#ifdef VIMAGE 92 .pr_flags = PR_HOST|PR_VNET, 93#else 94 .pr_flags = PR_HOST, 95#endif 96 .pr_allow = PR_ALLOW_ALL, 97}; 98MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF); 99 100/* allprison and lastprid are protected by allprison_lock. */ 101struct sx allprison_lock; 102SX_SYSINIT(allprison_lock, &allprison_lock, "allprison"); 103struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison); 104int lastprid = 0; 105 106static int do_jail_attach(struct thread *td, struct prison *pr); 107static void prison_complete(void *context, int pending); 108static void prison_deref(struct prison *pr, int flags); 109static char *prison_path(struct prison *pr1, struct prison *pr2); 110static void prison_remove_one(struct prison *pr); 111#ifdef INET 112static int _prison_check_ip4(struct prison *pr, struct in_addr *ia); 113static int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4); 114#endif 115#ifdef INET6 116static int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6); 117static int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6); 118#endif 119 120/* Flags for prison_deref */ 121#define PD_DEREF 0x01 122#define PD_DEUREF 0x02 123#define PD_LOCKED 0x04 124#define PD_LIST_SLOCKED 0x08 125#define PD_LIST_XLOCKED 0x10 126 127/* 128 * Parameter names corresponding to PR_* flag values 129 */ 130static char *pr_flag_names[] = { 131 [0] = "persist", 132}; 133 134static char *pr_flag_nonames[] = { 135 [0] = "nopersist", 136}; 137 138struct jailsys_flags { 139 const char *name; 140 unsigned disable; 141 unsigned new; 142} pr_flag_jailsys[] = { 143 { "host", 0, PR_HOST }, 144#ifdef VIMAGE 145 { "vnet", 0, PR_VNET }, 146#endif 147#ifdef INET 148 { "ip4", PR_IP4_USER | PR_IP4_DISABLE, PR_IP4_USER }, 149#endif 150#ifdef INET6 151 { "ip6", PR_IP6_USER | PR_IP6_DISABLE, PR_IP6_USER }, 152#endif 153}; 154 155static char *pr_allow_names[] = { 156 "allow.set_hostname", 157 "allow.sysvipc", 158 "allow.raw_sockets", 159 "allow.chflags", 160 "allow.mount", 161 "allow.quotas", 162 "allow.socket_af", 163}; 164 165static char *pr_allow_nonames[] = { 166 "allow.noset_hostname", 167 "allow.nosysvipc", 168 "allow.noraw_sockets", 169 "allow.nochflags", 170 "allow.nomount", 171 "allow.noquotas", 172 "allow.nosocket_af", 173}; 174 175#define JAIL_DEFAULT_ALLOW PR_ALLOW_SET_HOSTNAME 176#define JAIL_DEFAULT_ENFORCE_STATFS 2 177static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW; 178static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 179#if defined(INET) || defined(INET6) 180static unsigned jail_max_af_ips = 255; 181#endif 182 183#ifdef INET 184static int 185qcmp_v4(const void *ip1, const void *ip2) 186{ 187 in_addr_t iaa, iab; 188 189 /* 190 * We need to compare in HBO here to get the list sorted as expected 191 * by the result of the code. Sorting NBO addresses gives you 192 * interesting results. If you do not understand, do not try. 193 */ 194 iaa = ntohl(((const struct in_addr *)ip1)->s_addr); 195 iab = ntohl(((const struct in_addr *)ip2)->s_addr); 196 197 /* 198 * Do not simply return the difference of the two numbers, the int is 199 * not wide enough. 200 */ 201 if (iaa > iab) 202 return (1); 203 else if (iaa < iab) 204 return (-1); 205 else 206 return (0); 207} 208#endif 209 210#ifdef INET6 211static int 212qcmp_v6(const void *ip1, const void *ip2) 213{ 214 const struct in6_addr *ia6a, *ia6b; 215 int i, rc; 216 217 ia6a = (const struct in6_addr *)ip1; 218 ia6b = (const struct in6_addr *)ip2; 219 220 rc = 0; 221 for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) { 222 if (ia6a->s6_addr[i] > ia6b->s6_addr[i]) 223 rc = 1; 224 else if (ia6a->s6_addr[i] < ia6b->s6_addr[i]) 225 rc = -1; 226 } 227 return (rc); 228} 229#endif 230 231/* 232 * struct jail_args { 233 * struct jail *jail; 234 * }; 235 */ 236int 237jail(struct thread *td, struct jail_args *uap) 238{ 239 uint32_t version; 240 int error; 241 struct jail j; 242 243 error = copyin(uap->jail, &version, sizeof(uint32_t)); 244 if (error) 245 return (error); 246 247 switch (version) { 248 case 0: 249 { 250 struct jail_v0 j0; 251 252 /* FreeBSD single IPv4 jails. */ 253 bzero(&j, sizeof(struct jail)); 254 error = copyin(uap->jail, &j0, sizeof(struct jail_v0)); 255 if (error) 256 return (error); 257 j.version = j0.version; 258 j.path = j0.path; 259 j.hostname = j0.hostname; 260 j.ip4s = j0.ip_number; 261 break; 262 } 263 264 case 1: 265 /* 266 * Version 1 was used by multi-IPv4 jail implementations 267 * that never made it into the official kernel. 268 */ 269 return (EINVAL); 270 271 case 2: /* JAIL_API_VERSION */ 272 /* FreeBSD multi-IPv4/IPv6,noIP jails. */ 273 error = copyin(uap->jail, &j, sizeof(struct jail)); 274 if (error) 275 return (error); 276 break; 277 278 default: 279 /* Sci-Fi jails are not supported, sorry. */ 280 return (EINVAL); 281 } 282 return (kern_jail(td, &j)); 283} 284 285int 286kern_jail(struct thread *td, struct jail *j) 287{ 288 struct iovec optiov[2 * (4 289 + sizeof(pr_allow_names) / sizeof(pr_allow_names[0]) 290#ifdef INET 291 + 1 292#endif 293#ifdef INET6 294 + 1 295#endif 296 )]; 297 struct uio opt; 298 char *u_path, *u_hostname, *u_name; 299#ifdef INET 300 uint32_t ip4s; 301 struct in_addr *u_ip4; 302#endif 303#ifdef INET6 304 struct in6_addr *u_ip6; 305#endif 306 size_t tmplen; 307 int error, enforce_statfs, fi; 308 309 bzero(&optiov, sizeof(optiov)); 310 opt.uio_iov = optiov; 311 opt.uio_iovcnt = 0; 312 opt.uio_offset = -1; 313 opt.uio_resid = -1; 314 opt.uio_segflg = UIO_SYSSPACE; 315 opt.uio_rw = UIO_READ; 316 opt.uio_td = td; 317 318 /* Set permissions for top-level jails from sysctls. */ 319 if (!jailed(td->td_ucred)) { 320 for (fi = 0; fi < sizeof(pr_allow_names) / 321 sizeof(pr_allow_names[0]); fi++) { 322 optiov[opt.uio_iovcnt].iov_base = 323 (jail_default_allow & (1 << fi)) 324 ? pr_allow_names[fi] : pr_allow_nonames[fi]; 325 optiov[opt.uio_iovcnt].iov_len = 326 strlen(optiov[opt.uio_iovcnt].iov_base) + 1; 327 opt.uio_iovcnt += 2; 328 } 329 optiov[opt.uio_iovcnt].iov_base = "enforce_statfs"; 330 optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs"); 331 opt.uio_iovcnt++; 332 enforce_statfs = jail_default_enforce_statfs; 333 optiov[opt.uio_iovcnt].iov_base = &enforce_statfs; 334 optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs); 335 opt.uio_iovcnt++; 336 } 337 338 tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN; 339#ifdef INET 340 ip4s = (j->version == 0) ? 1 : j->ip4s; 341 if (ip4s > jail_max_af_ips) 342 return (EINVAL); 343 tmplen += ip4s * sizeof(struct in_addr); 344#else 345 if (j->ip4s > 0) 346 return (EINVAL); 347#endif 348#ifdef INET6 349 if (j->ip6s > jail_max_af_ips) 350 return (EINVAL); 351 tmplen += j->ip6s * sizeof(struct in6_addr); 352#else 353 if (j->ip6s > 0) 354 return (EINVAL); 355#endif 356 u_path = malloc(tmplen, M_TEMP, M_WAITOK); 357 u_hostname = u_path + MAXPATHLEN; 358 u_name = u_hostname + MAXHOSTNAMELEN; 359#ifdef INET 360 u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN); 361#endif 362#ifdef INET6 363#ifdef INET 364 u_ip6 = (struct in6_addr *)(u_ip4 + ip4s); 365#else 366 u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN); 367#endif 368#endif 369 optiov[opt.uio_iovcnt].iov_base = "path"; 370 optiov[opt.uio_iovcnt].iov_len = sizeof("path"); 371 opt.uio_iovcnt++; 372 optiov[opt.uio_iovcnt].iov_base = u_path; 373 error = copyinstr(j->path, u_path, MAXPATHLEN, 374 &optiov[opt.uio_iovcnt].iov_len); 375 if (error) { 376 free(u_path, M_TEMP); 377 return (error); 378 } 379 opt.uio_iovcnt++; 380 optiov[opt.uio_iovcnt].iov_base = "host.hostname"; 381 optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname"); 382 opt.uio_iovcnt++; 383 optiov[opt.uio_iovcnt].iov_base = u_hostname; 384 error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN, 385 &optiov[opt.uio_iovcnt].iov_len); 386 if (error) { 387 free(u_path, M_TEMP); 388 return (error); 389 } 390 opt.uio_iovcnt++; 391 if (j->jailname != NULL) { 392 optiov[opt.uio_iovcnt].iov_base = "name"; 393 optiov[opt.uio_iovcnt].iov_len = sizeof("name"); 394 opt.uio_iovcnt++; 395 optiov[opt.uio_iovcnt].iov_base = u_name; 396 error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN, 397 &optiov[opt.uio_iovcnt].iov_len); 398 if (error) { 399 free(u_path, M_TEMP); 400 return (error); 401 } 402 opt.uio_iovcnt++; 403 } 404#ifdef INET 405 optiov[opt.uio_iovcnt].iov_base = "ip4.addr"; 406 optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr"); 407 opt.uio_iovcnt++; 408 optiov[opt.uio_iovcnt].iov_base = u_ip4; 409 optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr); 410 if (j->version == 0) 411 u_ip4->s_addr = j->ip4s; 412 else { 413 error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len); 414 if (error) { 415 free(u_path, M_TEMP); 416 return (error); 417 } 418 } 419 opt.uio_iovcnt++; 420#endif 421#ifdef INET6 422 optiov[opt.uio_iovcnt].iov_base = "ip6.addr"; 423 optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr"); 424 opt.uio_iovcnt++; 425 optiov[opt.uio_iovcnt].iov_base = u_ip6; 426 optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr); 427 error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len); 428 if (error) { 429 free(u_path, M_TEMP); 430 return (error); 431 } 432 opt.uio_iovcnt++; 433#endif 434 KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]), 435 ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt)); 436 error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH); 437 free(u_path, M_TEMP); 438 return (error); 439} 440 441 442/* 443 * struct jail_set_args { 444 * struct iovec *iovp; 445 * unsigned int iovcnt; 446 * int flags; 447 * }; 448 */ 449int 450jail_set(struct thread *td, struct jail_set_args *uap) 451{ 452 struct uio *auio; 453 int error; 454 455 /* Check that we have an even number of iovecs. */ 456 if (uap->iovcnt & 1) 457 return (EINVAL); 458 459 error = copyinuio(uap->iovp, uap->iovcnt, &auio); 460 if (error) 461 return (error); 462 error = kern_jail_set(td, auio, uap->flags); 463 free(auio, M_IOV); 464 return (error); 465} 466 467int 468kern_jail_set(struct thread *td, struct uio *optuio, int flags) 469{ 470 struct nameidata nd; 471#ifdef INET 472 struct in_addr *ip4; 473#endif 474#ifdef INET6 475 struct in6_addr *ip6; 476#endif 477 struct vfsopt *opt; 478 struct vfsoptlist *opts; 479 struct prison *pr, *deadpr, *mypr, *ppr, *tpr; 480 struct vnode *root; 481 char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid; 482#if defined(INET) || defined(INET6) 483 struct prison *tppr; 484 void *op; 485#endif 486 unsigned long hid; 487 size_t namelen, onamelen; 488 int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos; 489 int gotchildmax, gotenforce, gothid, gotslevel; 490 int fi, jid, jsys, len, level; 491 int childmax, slevel, vfslocked; 492#if defined(INET) || defined(INET6) 493 int ii, ij; 494#endif 495#ifdef INET 496 int ip4s, redo_ip4; 497#endif 498#ifdef INET6 499 int ip6s, redo_ip6; 500#endif 501 unsigned pr_flags, ch_flags; 502 unsigned pr_allow, ch_allow, tallow; 503 char numbuf[12]; 504 505 error = priv_check(td, PRIV_JAIL_SET); 506 if (!error && (flags & JAIL_ATTACH)) 507 error = priv_check(td, PRIV_JAIL_ATTACH); 508 if (error) 509 return (error); 510 mypr = ppr = td->td_ucred->cr_prison; 511 if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0) 512 return (EPERM); 513 if (flags & ~JAIL_SET_MASK) 514 return (EINVAL); 515 516 /* 517 * Check all the parameters before committing to anything. Not all 518 * errors can be caught early, but we may as well try. Also, this 519 * takes care of some expensive stuff (path lookup) before getting 520 * the allprison lock. 521 * 522 * XXX Jails are not filesystems, and jail parameters are not mount 523 * options. But it makes more sense to re-use the vfsopt code 524 * than duplicate it under a different name. 525 */ 526 error = vfs_buildopts(optuio, &opts); 527 if (error) 528 return (error); 529#ifdef INET 530 ip4 = NULL; 531#endif 532#ifdef INET6 533 ip6 = NULL; 534#endif 535 536 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 537 if (error == ENOENT) 538 jid = 0; 539 else if (error != 0) 540 goto done_free; 541 542 error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel)); 543 if (error == ENOENT) 544 gotslevel = 0; 545 else if (error != 0) 546 goto done_free; 547 else 548 gotslevel = 1; 549 550 error = 551 vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax)); 552 if (error == ENOENT) 553 gotchildmax = 0; 554 else if (error != 0) 555 goto done_free; 556 else 557 gotchildmax = 1; 558 559 error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce)); 560 gotenforce = (error == 0); 561 if (gotenforce) { 562 if (enforce < 0 || enforce > 2) 563 return (EINVAL); 564 } else if (error != ENOENT) 565 goto done_free; 566 567 pr_flags = ch_flags = 0; 568 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 569 fi++) { 570 if (pr_flag_names[fi] == NULL) 571 continue; 572 vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi); 573 vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi); 574 } 575 ch_flags |= pr_flags; 576 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 577 fi++) { 578 error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys, 579 sizeof(jsys)); 580 if (error == ENOENT) 581 continue; 582 if (error != 0) 583 goto done_free; 584 switch (jsys) { 585 case JAIL_SYS_DISABLE: 586 if (!pr_flag_jailsys[fi].disable) { 587 error = EINVAL; 588 goto done_free; 589 } 590 pr_flags |= pr_flag_jailsys[fi].disable; 591 break; 592 case JAIL_SYS_NEW: 593 pr_flags |= pr_flag_jailsys[fi].new; 594 break; 595 case JAIL_SYS_INHERIT: 596 break; 597 default: 598 error = EINVAL; 599 goto done_free; 600 } 601 ch_flags |= 602 pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable; 603 } 604 if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE 605 && !(pr_flags & PR_PERSIST)) { 606 error = EINVAL; 607 vfs_opterror(opts, "new jail must persist or attach"); 608 goto done_errmsg; 609 } 610#ifdef VIMAGE 611 if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) { 612 error = EINVAL; 613 vfs_opterror(opts, "vnet cannot be changed after creation"); 614 goto done_errmsg; 615 } 616#endif 617#ifdef INET 618 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) { 619 error = EINVAL; 620 vfs_opterror(opts, "ip4 cannot be changed after creation"); 621 goto done_errmsg; 622 } 623#endif 624#ifdef INET6 625 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) { 626 error = EINVAL; 627 vfs_opterror(opts, "ip6 cannot be changed after creation"); 628 goto done_errmsg; 629 } 630#endif 631 632 pr_allow = ch_allow = 0; 633 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 634 fi++) { 635 vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi); 636 vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi); 637 } 638 ch_allow |= pr_allow; 639 640 error = vfs_getopt(opts, "name", (void **)&name, &len); 641 if (error == ENOENT) 642 name = NULL; 643 else if (error != 0) 644 goto done_free; 645 else { 646 if (len == 0 || name[len - 1] != '\0') { 647 error = EINVAL; 648 goto done_free; 649 } 650 if (len > MAXHOSTNAMELEN) { 651 error = ENAMETOOLONG; 652 goto done_free; 653 } 654 } 655 656 error = vfs_getopt(opts, "host.hostname", (void **)&host, &len); 657 if (error == ENOENT) 658 host = NULL; 659 else if (error != 0) 660 goto done_free; 661 else { 662 ch_flags |= PR_HOST; 663 pr_flags |= PR_HOST; 664 if (len == 0 || host[len - 1] != '\0') { 665 error = EINVAL; 666 goto done_free; 667 } 668 if (len > MAXHOSTNAMELEN) { 669 error = ENAMETOOLONG; 670 goto done_free; 671 } 672 } 673 674 error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len); 675 if (error == ENOENT) 676 domain = NULL; 677 else if (error != 0) 678 goto done_free; 679 else { 680 ch_flags |= PR_HOST; 681 pr_flags |= PR_HOST; 682 if (len == 0 || domain[len - 1] != '\0') { 683 error = EINVAL; 684 goto done_free; 685 } 686 if (len > MAXHOSTNAMELEN) { 687 error = ENAMETOOLONG; 688 goto done_free; 689 } 690 } 691 692 error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len); 693 if (error == ENOENT) 694 uuid = NULL; 695 else if (error != 0) 696 goto done_free; 697 else { 698 ch_flags |= PR_HOST; 699 pr_flags |= PR_HOST; 700 if (len == 0 || uuid[len - 1] != '\0') { 701 error = EINVAL; 702 goto done_free; 703 } 704 if (len > HOSTUUIDLEN) { 705 error = ENAMETOOLONG; 706 goto done_free; 707 } 708 } 709 710#ifdef COMPAT_IA32 711 if (td->td_proc->p_sysent->sv_flags & SV_IA32) { 712 uint32_t hid32; 713 714 error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32)); 715 hid = hid32; 716 } else 717#endif 718 error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid)); 719 if (error == ENOENT) 720 gothid = 0; 721 else if (error != 0) 722 goto done_free; 723 else { 724 gothid = 1; 725 ch_flags |= PR_HOST; 726 pr_flags |= PR_HOST; 727 } 728 729#ifdef INET 730 error = vfs_getopt(opts, "ip4.addr", &op, &ip4s); 731 if (error == ENOENT) 732 ip4s = (pr_flags & PR_IP4_DISABLE) ? 0 : -1; 733 else if (error != 0) 734 goto done_free; 735 else if (ip4s & (sizeof(*ip4) - 1)) { 736 error = EINVAL; 737 goto done_free; 738 } else { 739 ch_flags |= PR_IP4_USER | PR_IP4_DISABLE; 740 if (ip4s == 0) 741 pr_flags |= PR_IP4_USER | PR_IP4_DISABLE; 742 else { 743 pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER; 744 ip4s /= sizeof(*ip4); 745 if (ip4s > jail_max_af_ips) { 746 error = EINVAL; 747 vfs_opterror(opts, "too many IPv4 addresses"); 748 goto done_errmsg; 749 } 750 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 751 bcopy(op, ip4, ip4s * sizeof(*ip4)); 752 /* 753 * IP addresses are all sorted but ip[0] to preserve 754 * the primary IP address as given from userland. 755 * This special IP is used for unbound outgoing 756 * connections as well for "loopback" traffic in case 757 * source address selection cannot find any more fitting 758 * address to connect from. 759 */ 760 if (ip4s > 1) 761 qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4); 762 /* 763 * Check for duplicate addresses and do some simple 764 * zero and broadcast checks. If users give other bogus 765 * addresses it is their problem. 766 * 767 * We do not have to care about byte order for these 768 * checks so we will do them in NBO. 769 */ 770 for (ii = 0; ii < ip4s; ii++) { 771 if (ip4[ii].s_addr == INADDR_ANY || 772 ip4[ii].s_addr == INADDR_BROADCAST) { 773 error = EINVAL; 774 goto done_free; 775 } 776 if ((ii+1) < ip4s && 777 (ip4[0].s_addr == ip4[ii+1].s_addr || 778 ip4[ii].s_addr == ip4[ii+1].s_addr)) { 779 error = EINVAL; 780 goto done_free; 781 } 782 } 783 } 784 } 785#endif 786 787#ifdef INET6 788 error = vfs_getopt(opts, "ip6.addr", &op, &ip6s); 789 if (error == ENOENT) 790 ip6s = (pr_flags & PR_IP6_DISABLE) ? 0 : -1; 791 else if (error != 0) 792 goto done_free; 793 else if (ip6s & (sizeof(*ip6) - 1)) { 794 error = EINVAL; 795 goto done_free; 796 } else { 797 ch_flags |= PR_IP6_USER | PR_IP6_DISABLE; 798 if (ip6s == 0) 799 pr_flags |= PR_IP6_USER | PR_IP6_DISABLE; 800 else { 801 pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER; 802 ip6s /= sizeof(*ip6); 803 if (ip6s > jail_max_af_ips) { 804 error = EINVAL; 805 vfs_opterror(opts, "too many IPv6 addresses"); 806 goto done_errmsg; 807 } 808 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 809 bcopy(op, ip6, ip6s * sizeof(*ip6)); 810 if (ip6s > 1) 811 qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6); 812 for (ii = 0; ii < ip6s; ii++) { 813 if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) { 814 error = EINVAL; 815 goto done_free; 816 } 817 if ((ii+1) < ip6s && 818 (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) || 819 IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1]))) 820 { 821 error = EINVAL; 822 goto done_free; 823 } 824 } 825 } 826 } 827#endif 828 829#if defined(VIMAGE) && (defined(INET) || defined(INET6)) 830 if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 831 error = EINVAL; 832 vfs_opterror(opts, 833 "vnet jails cannot have IP address restrictions"); 834 goto done_errmsg; 835 } 836#endif 837 838 root = NULL; 839 error = vfs_getopt(opts, "path", (void **)&path, &len); 840 if (error == ENOENT) 841 path = NULL; 842 else if (error != 0) 843 goto done_free; 844 else { 845 if (flags & JAIL_UPDATE) { 846 error = EINVAL; 847 vfs_opterror(opts, 848 "path cannot be changed after creation"); 849 goto done_errmsg; 850 } 851 if (len == 0 || path[len - 1] != '\0') { 852 error = EINVAL; 853 goto done_free; 854 } 855 if (len < 2 || (len == 2 && path[0] == '/')) 856 path = NULL; 857 else { 858 /* Leave room for a real-root full pathname. */ 859 if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/") 860 ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) { 861 error = ENAMETOOLONG; 862 goto done_free; 863 } 864 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_SYSSPACE, 865 path, td); 866 error = namei(&nd); 867 if (error) 868 goto done_free; 869 vfslocked = NDHASGIANT(&nd); 870 root = nd.ni_vp; 871 NDFREE(&nd, NDF_ONLY_PNBUF); 872 if (root->v_type != VDIR) { 873 error = ENOTDIR; 874 vrele(root); 875 VFS_UNLOCK_GIANT(vfslocked); 876 goto done_free; 877 } 878 VFS_UNLOCK_GIANT(vfslocked); 879 } 880 } 881 882 /* 883 * Grab the allprison lock before letting modules check their 884 * parameters. Once we have it, do not let go so we'll have a 885 * consistent view of the OSD list. 886 */ 887 sx_xlock(&allprison_lock); 888 error = osd_jail_call(NULL, PR_METHOD_CHECK, opts); 889 if (error) 890 goto done_unlock_list; 891 892 /* By now, all parameters should have been noted. */ 893 TAILQ_FOREACH(opt, opts, link) { 894 if (!opt->seen && strcmp(opt->name, "errmsg")) { 895 error = EINVAL; 896 vfs_opterror(opts, "unknown parameter: %s", opt->name); 897 goto done_unlock_list; 898 } 899 } 900 901 /* 902 * See if we are creating a new record or updating an existing one. 903 * This abuses the file error codes ENOENT and EEXIST. 904 */ 905 cuflags = flags & (JAIL_CREATE | JAIL_UPDATE); 906 if (!cuflags) { 907 error = EINVAL; 908 vfs_opterror(opts, "no valid operation (create or update)"); 909 goto done_unlock_list; 910 } 911 pr = NULL; 912 namelc = NULL; 913 if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) { 914 namelc = strrchr(name, '.'); 915 jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10); 916 if (*p != '\0') 917 jid = 0; 918 } 919 if (jid != 0) { 920 /* 921 * See if a requested jid already exists. There is an 922 * information leak here if the jid exists but is not within 923 * the caller's jail hierarchy. Jail creators will get EEXIST 924 * even though they cannot see the jail, and CREATE | UPDATE 925 * will return ENOENT which is not normally a valid error. 926 */ 927 if (jid < 0) { 928 error = EINVAL; 929 vfs_opterror(opts, "negative jid"); 930 goto done_unlock_list; 931 } 932 pr = prison_find(jid); 933 if (pr != NULL) { 934 ppr = pr->pr_parent; 935 /* Create: jid must not exist. */ 936 if (cuflags == JAIL_CREATE) { 937 mtx_unlock(&pr->pr_mtx); 938 error = EEXIST; 939 vfs_opterror(opts, "jail %d already exists", 940 jid); 941 goto done_unlock_list; 942 } 943 if (!prison_ischild(mypr, pr)) { 944 mtx_unlock(&pr->pr_mtx); 945 pr = NULL; 946 } else if (pr->pr_uref == 0) { 947 if (!(flags & JAIL_DYING)) { 948 mtx_unlock(&pr->pr_mtx); 949 error = ENOENT; 950 vfs_opterror(opts, "jail %d is dying", 951 jid); 952 goto done_unlock_list; 953 } else if ((flags & JAIL_ATTACH) || 954 (pr_flags & PR_PERSIST)) { 955 /* 956 * A dying jail might be resurrected 957 * (via attach or persist), but first 958 * it must determine if another jail 959 * has claimed its name. Accomplish 960 * this by implicitly re-setting the 961 * name. 962 */ 963 if (name == NULL) 964 name = prison_name(mypr, pr); 965 } 966 } 967 } 968 if (pr == NULL) { 969 /* Update: jid must exist. */ 970 if (cuflags == JAIL_UPDATE) { 971 error = ENOENT; 972 vfs_opterror(opts, "jail %d not found", jid); 973 goto done_unlock_list; 974 } 975 } 976 } 977 /* 978 * If the caller provided a name, look for a jail by that name. 979 * This has different semantics for creates and updates keyed by jid 980 * (where the name must not already exist in a different jail), 981 * and updates keyed by the name itself (where the name must exist 982 * because that is the jail being updated). 983 */ 984 if (name != NULL) { 985 namelc = strrchr(name, '.'); 986 if (namelc == NULL) 987 namelc = name; 988 else { 989 /* 990 * This is a hierarchical name. Split it into the 991 * parent and child names, and make sure the parent 992 * exists or matches an already found jail. 993 */ 994 *namelc = '\0'; 995 if (pr != NULL) { 996 if (strncmp(name, ppr->pr_name, namelc - name) 997 || ppr->pr_name[namelc - name] != '\0') { 998 mtx_unlock(&pr->pr_mtx); 999 error = EINVAL; 1000 vfs_opterror(opts, 1001 "cannot change jail's parent"); 1002 goto done_unlock_list; 1003 } 1004 } else { 1005 ppr = prison_find_name(mypr, name); 1006 if (ppr == NULL) { 1007 error = ENOENT; 1008 vfs_opterror(opts, 1009 "jail \"%s\" not found", name); 1010 goto done_unlock_list; 1011 } 1012 mtx_unlock(&ppr->pr_mtx); 1013 } 1014 name = ++namelc; 1015 } 1016 if (name[0] != '\0') { 1017 namelen = 1018 (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 1019 name_again: 1020 deadpr = NULL; 1021 FOREACH_PRISON_CHILD(ppr, tpr) { 1022 if (tpr != pr && tpr->pr_ref > 0 && 1023 !strcmp(tpr->pr_name + namelen, name)) { 1024 if (pr == NULL && 1025 cuflags != JAIL_CREATE) { 1026 mtx_lock(&tpr->pr_mtx); 1027 if (tpr->pr_ref > 0) { 1028 /* 1029 * Use this jail 1030 * for updates. 1031 */ 1032 if (tpr->pr_uref > 0) { 1033 pr = tpr; 1034 break; 1035 } 1036 deadpr = tpr; 1037 } 1038 mtx_unlock(&tpr->pr_mtx); 1039 } else if (tpr->pr_uref > 0) { 1040 /* 1041 * Create, or update(jid): 1042 * name must not exist in an 1043 * active sibling jail. 1044 */ 1045 error = EEXIST; 1046 if (pr != NULL) 1047 mtx_unlock(&pr->pr_mtx); 1048 vfs_opterror(opts, 1049 "jail \"%s\" already exists", 1050 name); 1051 goto done_unlock_list; 1052 } 1053 } 1054 } 1055 /* If no active jail is found, use a dying one. */ 1056 if (deadpr != NULL && pr == NULL) { 1057 if (flags & JAIL_DYING) { 1058 mtx_lock(&deadpr->pr_mtx); 1059 if (deadpr->pr_ref == 0) { 1060 mtx_unlock(&deadpr->pr_mtx); 1061 goto name_again; 1062 } 1063 pr = deadpr; 1064 } else if (cuflags == JAIL_UPDATE) { 1065 error = ENOENT; 1066 vfs_opterror(opts, 1067 "jail \"%s\" is dying", name); 1068 goto done_unlock_list; 1069 } 1070 } 1071 /* Update: name must exist if no jid. */ 1072 else if (cuflags == JAIL_UPDATE && pr == NULL) { 1073 error = ENOENT; 1074 vfs_opterror(opts, "jail \"%s\" not found", 1075 name); 1076 goto done_unlock_list; 1077 } 1078 } 1079 } 1080 /* Update: must provide a jid or name. */ 1081 else if (cuflags == JAIL_UPDATE && pr == NULL) { 1082 error = ENOENT; 1083 vfs_opterror(opts, "update specified no jail"); 1084 goto done_unlock_list; 1085 } 1086 1087 /* If there's no prison to update, create a new one and link it in. */ 1088 if (pr == NULL) { 1089 for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent) 1090 if (tpr->pr_childcount >= tpr->pr_childmax) { 1091 error = EPERM; 1092 vfs_opterror(opts, "prison limit exceeded"); 1093 goto done_unlock_list; 1094 } 1095 created = 1; 1096 mtx_lock(&ppr->pr_mtx); 1097 if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) { 1098 mtx_unlock(&ppr->pr_mtx); 1099 error = ENOENT; 1100 vfs_opterror(opts, "parent jail went away!"); 1101 goto done_unlock_list; 1102 } 1103 ppr->pr_ref++; 1104 ppr->pr_uref++; 1105 mtx_unlock(&ppr->pr_mtx); 1106 pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO); 1107 if (jid == 0) { 1108 /* Find the next free jid. */ 1109 jid = lastprid + 1; 1110 findnext: 1111 if (jid == JAIL_MAX) 1112 jid = 1; 1113 TAILQ_FOREACH(tpr, &allprison, pr_list) { 1114 if (tpr->pr_id < jid) 1115 continue; 1116 if (tpr->pr_id > jid || tpr->pr_ref == 0) { 1117 TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1118 break; 1119 } 1120 if (jid == lastprid) { 1121 error = EAGAIN; 1122 vfs_opterror(opts, 1123 "no available jail IDs"); 1124 free(pr, M_PRISON); 1125 prison_deref(ppr, PD_DEREF | 1126 PD_DEUREF | PD_LIST_XLOCKED); 1127 goto done_releroot; 1128 } 1129 jid++; 1130 goto findnext; 1131 } 1132 lastprid = jid; 1133 } else { 1134 /* 1135 * The jail already has a jid (that did not yet exist), 1136 * so just find where to insert it. 1137 */ 1138 TAILQ_FOREACH(tpr, &allprison, pr_list) 1139 if (tpr->pr_id >= jid) { 1140 TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1141 break; 1142 } 1143 } 1144 if (tpr == NULL) 1145 TAILQ_INSERT_TAIL(&allprison, pr, pr_list); 1146 LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling); 1147 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 1148 tpr->pr_childcount++; 1149 1150 pr->pr_parent = ppr; 1151 pr->pr_id = jid; 1152 1153 /* Set some default values, and inherit some from the parent. */ 1154 if (name == NULL) 1155 name = ""; 1156 if (path == NULL) { 1157 path = "/"; 1158 root = mypr->pr_root; 1159 vref(root); 1160 } 1161 strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN); 1162 pr->pr_flags |= PR_HOST; 1163#if defined(INET) || defined(INET6) 1164#ifdef VIMAGE 1165 if (!(pr_flags & PR_VNET)) 1166#endif 1167 { 1168#ifdef INET 1169 if (!(ch_flags & PR_IP4_USER)) 1170 pr->pr_flags |= 1171 PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE; 1172 else if (!(pr_flags & PR_IP4_USER)) { 1173 pr->pr_flags |= ppr->pr_flags & PR_IP4; 1174 if (ppr->pr_ip4 != NULL) { 1175 pr->pr_ip4s = ppr->pr_ip4s; 1176 pr->pr_ip4 = malloc(pr->pr_ip4s * 1177 sizeof(struct in_addr), M_PRISON, 1178 M_WAITOK); 1179 bcopy(ppr->pr_ip4, pr->pr_ip4, 1180 pr->pr_ip4s * sizeof(*pr->pr_ip4)); 1181 } 1182 } 1183#endif 1184#ifdef INET6 1185 if (!(ch_flags & PR_IP6_USER)) 1186 pr->pr_flags |= 1187 PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE; 1188 else if (!(pr_flags & PR_IP6_USER)) { 1189 pr->pr_flags |= ppr->pr_flags & PR_IP6; 1190 if (ppr->pr_ip6 != NULL) { 1191 pr->pr_ip6s = ppr->pr_ip6s; 1192 pr->pr_ip6 = malloc(pr->pr_ip6s * 1193 sizeof(struct in6_addr), M_PRISON, 1194 M_WAITOK); 1195 bcopy(ppr->pr_ip6, pr->pr_ip6, 1196 pr->pr_ip6s * sizeof(*pr->pr_ip6)); 1197 } 1198 } 1199#endif 1200 } 1201#endif 1202 pr->pr_securelevel = ppr->pr_securelevel; 1203 pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow; 1204 pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 1205 1206 LIST_INIT(&pr->pr_children); 1207 mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK); 1208 1209#ifdef VIMAGE 1210 /* Allocate a new vnet if specified. */ 1211 pr->pr_vnet = (pr_flags & PR_VNET) 1212 ? vnet_alloc() : ppr->pr_vnet; 1213#endif 1214 /* 1215 * Allocate a dedicated cpuset for each jail. 1216 * Unlike other initial settings, this may return an erorr. 1217 */ 1218 error = cpuset_create_root(ppr, &pr->pr_cpuset); 1219 if (error) { 1220 prison_deref(pr, PD_LIST_XLOCKED); 1221 goto done_releroot; 1222 } 1223 1224 mtx_lock(&pr->pr_mtx); 1225 /* 1226 * New prisons do not yet have a reference, because we do not 1227 * want other to see the incomplete prison once the 1228 * allprison_lock is downgraded. 1229 */ 1230 } else { 1231 created = 0; 1232 /* 1233 * Grab a reference for existing prisons, to ensure they 1234 * continue to exist for the duration of the call. 1235 */ 1236 pr->pr_ref++; 1237#if defined(VIMAGE) && (defined(INET) || defined(INET6)) 1238 if ((pr->pr_flags & PR_VNET) && 1239 (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 1240 error = EINVAL; 1241 vfs_opterror(opts, 1242 "vnet jails cannot have IP address restrictions"); 1243 goto done_deref_locked; 1244 } 1245#endif 1246#ifdef INET 1247 if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 1248 error = EINVAL; 1249 vfs_opterror(opts, 1250 "ip4 cannot be changed after creation"); 1251 goto done_deref_locked; 1252 } 1253#endif 1254#ifdef INET6 1255 if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 1256 error = EINVAL; 1257 vfs_opterror(opts, 1258 "ip6 cannot be changed after creation"); 1259 goto done_deref_locked; 1260 } 1261#endif 1262 } 1263 1264 /* Do final error checking before setting anything. */ 1265 if (gotslevel) { 1266 if (slevel < ppr->pr_securelevel) { 1267 error = EPERM; 1268 goto done_deref_locked; 1269 } 1270 } 1271 if (gotchildmax) { 1272 if (childmax >= ppr->pr_childmax) { 1273 error = EPERM; 1274 goto done_deref_locked; 1275 } 1276 } 1277 if (gotenforce) { 1278 if (enforce < ppr->pr_enforce_statfs) { 1279 error = EPERM; 1280 goto done_deref_locked; 1281 } 1282 } 1283#ifdef INET 1284 if (ip4s > 0) { 1285 if (ppr->pr_flags & PR_IP4) { 1286 /* 1287 * Make sure the new set of IP addresses is a 1288 * subset of the parent's list. Don't worry 1289 * about the parent being unlocked, as any 1290 * setting is done with allprison_lock held. 1291 */ 1292 for (ij = 0; ij < ppr->pr_ip4s; ij++) 1293 if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 1294 break; 1295 if (ij == ppr->pr_ip4s) { 1296 error = EPERM; 1297 goto done_deref_locked; 1298 } 1299 if (ip4s > 1) { 1300 for (ii = ij = 1; ii < ip4s; ii++) { 1301 if (ip4[ii].s_addr == 1302 ppr->pr_ip4[0].s_addr) 1303 continue; 1304 for (; ij < ppr->pr_ip4s; ij++) 1305 if (ip4[ii].s_addr == 1306 ppr->pr_ip4[ij].s_addr) 1307 break; 1308 if (ij == ppr->pr_ip4s) 1309 break; 1310 } 1311 if (ij == ppr->pr_ip4s) { 1312 error = EPERM; 1313 goto done_deref_locked; 1314 } 1315 } 1316 } 1317 /* 1318 * Check for conflicting IP addresses. We permit them 1319 * if there is no more than one IP on each jail. If 1320 * there is a duplicate on a jail with more than one 1321 * IP stop checking and return error. 1322 */ 1323 tppr = ppr; 1324#ifdef VIMAGE 1325 for (; tppr != &prison0; tppr = tppr->pr_parent) 1326 if (tppr->pr_flags & PR_VNET) 1327 break; 1328#endif 1329 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1330 if (tpr == pr || 1331#ifdef VIMAGE 1332 (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1333#endif 1334 tpr->pr_uref == 0) { 1335 descend = 0; 1336 continue; 1337 } 1338 if (!(tpr->pr_flags & PR_IP4_USER)) 1339 continue; 1340 descend = 0; 1341 if (tpr->pr_ip4 == NULL || 1342 (ip4s == 1 && tpr->pr_ip4s == 1)) 1343 continue; 1344 for (ii = 0; ii < ip4s; ii++) { 1345 if (_prison_check_ip4(tpr, &ip4[ii]) == 0) { 1346 error = EADDRINUSE; 1347 vfs_opterror(opts, 1348 "IPv4 addresses clash"); 1349 goto done_deref_locked; 1350 } 1351 } 1352 } 1353 } 1354#endif 1355#ifdef INET6 1356 if (ip6s > 0) { 1357 if (ppr->pr_flags & PR_IP6) { 1358 /* 1359 * Make sure the new set of IP addresses is a 1360 * subset of the parent's list. 1361 */ 1362 for (ij = 0; ij < ppr->pr_ip6s; ij++) 1363 if (IN6_ARE_ADDR_EQUAL(&ip6[0], 1364 &ppr->pr_ip6[ij])) 1365 break; 1366 if (ij == ppr->pr_ip6s) { 1367 error = EPERM; 1368 goto done_deref_locked; 1369 } 1370 if (ip6s > 1) { 1371 for (ii = ij = 1; ii < ip6s; ii++) { 1372 if (IN6_ARE_ADDR_EQUAL(&ip6[ii], 1373 &ppr->pr_ip6[0])) 1374 continue; 1375 for (; ij < ppr->pr_ip6s; ij++) 1376 if (IN6_ARE_ADDR_EQUAL( 1377 &ip6[ii], &ppr->pr_ip6[ij])) 1378 break; 1379 if (ij == ppr->pr_ip6s) 1380 break; 1381 } 1382 if (ij == ppr->pr_ip6s) { 1383 error = EPERM; 1384 goto done_deref_locked; 1385 } 1386 } 1387 } 1388 /* Check for conflicting IP addresses. */ 1389 tppr = ppr; 1390#ifdef VIMAGE 1391 for (; tppr != &prison0; tppr = tppr->pr_parent) 1392 if (tppr->pr_flags & PR_VNET) 1393 break; 1394#endif 1395 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1396 if (tpr == pr || 1397#ifdef VIMAGE 1398 (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1399#endif 1400 tpr->pr_uref == 0) { 1401 descend = 0; 1402 continue; 1403 } 1404 if (!(tpr->pr_flags & PR_IP6_USER)) 1405 continue; 1406 descend = 0; 1407 if (tpr->pr_ip6 == NULL || 1408 (ip6s == 1 && tpr->pr_ip6s == 1)) 1409 continue; 1410 for (ii = 0; ii < ip6s; ii++) { 1411 if (_prison_check_ip6(tpr, &ip6[ii]) == 0) { 1412 error = EADDRINUSE; 1413 vfs_opterror(opts, 1414 "IPv6 addresses clash"); 1415 goto done_deref_locked; 1416 } 1417 } 1418 } 1419 } 1420#endif 1421 onamelen = namelen = 0; 1422 if (name != NULL) { 1423 /* Give a default name of the jid. */ 1424 if (name[0] == '\0') 1425 snprintf(name = numbuf, sizeof(numbuf), "%d", jid); 1426 else if (*namelc == '0' || (strtoul(namelc, &p, 10) != jid && 1427 *p == '\0')) { 1428 error = EINVAL; 1429 vfs_opterror(opts, 1430 "name cannot be numeric (unless it is the jid)"); 1431 goto done_deref_locked; 1432 } 1433 /* 1434 * Make sure the name isn't too long for the prison or its 1435 * children. 1436 */ 1437 onamelen = strlen(pr->pr_name); 1438 namelen = strlen(name); 1439 if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) { 1440 error = ENAMETOOLONG; 1441 goto done_deref_locked; 1442 } 1443 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 1444 if (strlen(tpr->pr_name) + (namelen - onamelen) >= 1445 sizeof(pr->pr_name)) { 1446 error = ENAMETOOLONG; 1447 goto done_deref_locked; 1448 } 1449 } 1450 } 1451 if (pr_allow & ~ppr->pr_allow) { 1452 error = EPERM; 1453 goto done_deref_locked; 1454 } 1455 1456 /* Set the parameters of the prison. */ 1457#ifdef INET 1458 redo_ip4 = 0; 1459 if (pr_flags & PR_IP4_USER) { 1460 pr->pr_flags |= PR_IP4; 1461 free(pr->pr_ip4, M_PRISON); 1462 pr->pr_ip4s = ip4s; 1463 pr->pr_ip4 = ip4; 1464 ip4 = NULL; 1465 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1466#ifdef VIMAGE 1467 if (tpr->pr_flags & PR_VNET) { 1468 descend = 0; 1469 continue; 1470 } 1471#endif 1472 if (prison_restrict_ip4(tpr, NULL)) { 1473 redo_ip4 = 1; 1474 descend = 0; 1475 } 1476 } 1477 } 1478#endif 1479#ifdef INET6 1480 redo_ip6 = 0; 1481 if (pr_flags & PR_IP6_USER) { 1482 pr->pr_flags |= PR_IP6; 1483 free(pr->pr_ip6, M_PRISON); 1484 pr->pr_ip6s = ip6s; 1485 pr->pr_ip6 = ip6; 1486 ip6 = NULL; 1487 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1488#ifdef VIMAGE 1489 if (tpr->pr_flags & PR_VNET) { 1490 descend = 0; 1491 continue; 1492 } 1493#endif 1494 if (prison_restrict_ip6(tpr, NULL)) { 1495 redo_ip6 = 1; 1496 descend = 0; 1497 } 1498 } 1499 } 1500#endif 1501 if (gotslevel) { 1502 pr->pr_securelevel = slevel; 1503 /* Set all child jails to be at least this level. */ 1504 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1505 if (tpr->pr_securelevel < slevel) 1506 tpr->pr_securelevel = slevel; 1507 } 1508 if (gotchildmax) { 1509 pr->pr_childmax = childmax; 1510 /* Set all child jails to under this limit. */ 1511 FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level) 1512 if (tpr->pr_childmax > childmax - level) 1513 tpr->pr_childmax = childmax > level 1514 ? childmax - level : 0; 1515 } 1516 if (gotenforce) { 1517 pr->pr_enforce_statfs = enforce; 1518 /* Pass this restriction on to the children. */ 1519 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1520 if (tpr->pr_enforce_statfs < enforce) 1521 tpr->pr_enforce_statfs = enforce; 1522 } 1523 if (name != NULL) { 1524 if (ppr == &prison0) 1525 strlcpy(pr->pr_name, name, sizeof(pr->pr_name)); 1526 else 1527 snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s", 1528 ppr->pr_name, name); 1529 /* Change this component of child names. */ 1530 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1531 bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen, 1532 strlen(tpr->pr_name + onamelen) + 1); 1533 bcopy(pr->pr_name, tpr->pr_name, namelen); 1534 } 1535 } 1536 if (path != NULL) { 1537 /* Try to keep a real-rooted full pathname. */ 1538 if (path[0] == '/' && strcmp(mypr->pr_path, "/")) 1539 snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s", 1540 mypr->pr_path, path); 1541 else 1542 strlcpy(pr->pr_path, path, sizeof(pr->pr_path)); 1543 pr->pr_root = root; 1544 } 1545 if (PR_HOST & ch_flags & ~pr_flags) { 1546 if (pr->pr_flags & PR_HOST) { 1547 /* 1548 * Copy the parent's host info. As with pr_ip4 above, 1549 * the lack of a lock on the parent is not a problem; 1550 * it is always set with allprison_lock at least 1551 * shared, and is held exclusively here. 1552 */ 1553 strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname, 1554 sizeof(pr->pr_hostname)); 1555 strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname, 1556 sizeof(pr->pr_domainname)); 1557 strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid, 1558 sizeof(pr->pr_hostuuid)); 1559 pr->pr_hostid = pr->pr_parent->pr_hostid; 1560 } 1561 } else if (host != NULL || domain != NULL || uuid != NULL || gothid) { 1562 /* Set this prison, and any descendants without PR_HOST. */ 1563 if (host != NULL) 1564 strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname)); 1565 if (domain != NULL) 1566 strlcpy(pr->pr_domainname, domain, 1567 sizeof(pr->pr_domainname)); 1568 if (uuid != NULL) 1569 strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid)); 1570 if (gothid) 1571 pr->pr_hostid = hid; 1572 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1573 if (tpr->pr_flags & PR_HOST) 1574 descend = 0; 1575 else { 1576 if (host != NULL) 1577 strlcpy(tpr->pr_hostname, 1578 pr->pr_hostname, 1579 sizeof(tpr->pr_hostname)); 1580 if (domain != NULL) 1581 strlcpy(tpr->pr_domainname, 1582 pr->pr_domainname, 1583 sizeof(tpr->pr_domainname)); 1584 if (uuid != NULL) 1585 strlcpy(tpr->pr_hostuuid, 1586 pr->pr_hostuuid, 1587 sizeof(tpr->pr_hostuuid)); 1588 if (gothid) 1589 tpr->pr_hostid = hid; 1590 } 1591 } 1592 } 1593 if ((tallow = ch_allow & ~pr_allow)) { 1594 /* Clear allow bits in all children. */ 1595 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 1596 tpr->pr_allow &= ~tallow; 1597 } 1598 pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow; 1599 /* 1600 * Persistent prisons get an extra reference, and prisons losing their 1601 * persist flag lose that reference. Only do this for existing prisons 1602 * for now, so new ones will remain unseen until after the module 1603 * handlers have completed. 1604 */ 1605 if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) { 1606 if (pr_flags & PR_PERSIST) { 1607 pr->pr_ref++; 1608 pr->pr_uref++; 1609 } else { 1610 pr->pr_ref--; 1611 pr->pr_uref--; 1612 } 1613 } 1614 pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags; 1615 mtx_unlock(&pr->pr_mtx); 1616 1617 /* Locks may have prevented a complete restriction of child IP 1618 * addresses. If so, allocate some more memory and try again. 1619 */ 1620#ifdef INET 1621 while (redo_ip4) { 1622 ip4s = pr->pr_ip4s; 1623 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 1624 mtx_lock(&pr->pr_mtx); 1625 redo_ip4 = 0; 1626 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1627#ifdef VIMAGE 1628 if (tpr->pr_flags & PR_VNET) { 1629 descend = 0; 1630 continue; 1631 } 1632#endif 1633 if (prison_restrict_ip4(tpr, ip4)) { 1634 if (ip4 != NULL) 1635 ip4 = NULL; 1636 else 1637 redo_ip4 = 1; 1638 } 1639 } 1640 mtx_unlock(&pr->pr_mtx); 1641 } 1642#endif 1643#ifdef INET6 1644 while (redo_ip6) { 1645 ip6s = pr->pr_ip6s; 1646 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 1647 mtx_lock(&pr->pr_mtx); 1648 redo_ip6 = 0; 1649 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1650#ifdef VIMAGE 1651 if (tpr->pr_flags & PR_VNET) { 1652 descend = 0; 1653 continue; 1654 } 1655#endif 1656 if (prison_restrict_ip6(tpr, ip6)) { 1657 if (ip6 != NULL) 1658 ip6 = NULL; 1659 else 1660 redo_ip6 = 1; 1661 } 1662 } 1663 mtx_unlock(&pr->pr_mtx); 1664 } 1665#endif 1666 1667 /* Let the modules do their work. */ 1668 sx_downgrade(&allprison_lock); 1669 if (created) { 1670 error = osd_jail_call(pr, PR_METHOD_CREATE, opts); 1671 if (error) { 1672 prison_deref(pr, PD_LIST_SLOCKED); 1673 goto done_errmsg; 1674 } 1675 } 1676 error = osd_jail_call(pr, PR_METHOD_SET, opts); 1677 if (error) { 1678 prison_deref(pr, created 1679 ? PD_LIST_SLOCKED 1680 : PD_DEREF | PD_LIST_SLOCKED); 1681 goto done_errmsg; 1682 } 1683 1684 /* Attach this process to the prison if requested. */ 1685 if (flags & JAIL_ATTACH) { 1686 mtx_lock(&pr->pr_mtx); 1687 error = do_jail_attach(td, pr); 1688 if (error) { 1689 vfs_opterror(opts, "attach failed"); 1690 if (!created) 1691 prison_deref(pr, PD_DEREF); 1692 goto done_errmsg; 1693 } 1694 } 1695 1696 /* 1697 * Now that it is all there, drop the temporary reference from existing 1698 * prisons. Or add a reference to newly created persistent prisons 1699 * (which was not done earlier so that the prison would not be publicly 1700 * visible). 1701 */ 1702 if (!created) { 1703 prison_deref(pr, (flags & JAIL_ATTACH) 1704 ? PD_DEREF 1705 : PD_DEREF | PD_LIST_SLOCKED); 1706 } else { 1707 if (pr_flags & PR_PERSIST) { 1708 mtx_lock(&pr->pr_mtx); 1709 pr->pr_ref++; 1710 pr->pr_uref++; 1711 mtx_unlock(&pr->pr_mtx); 1712 } 1713 if (!(flags & JAIL_ATTACH)) 1714 sx_sunlock(&allprison_lock); 1715 } 1716 td->td_retval[0] = pr->pr_id; 1717 goto done_errmsg; 1718 1719 done_deref_locked: 1720 prison_deref(pr, created 1721 ? PD_LOCKED | PD_LIST_XLOCKED 1722 : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 1723 goto done_releroot; 1724 done_unlock_list: 1725 sx_xunlock(&allprison_lock); 1726 done_releroot: 1727 if (root != NULL) { 1728 vfslocked = VFS_LOCK_GIANT(root->v_mount); 1729 vrele(root); 1730 VFS_UNLOCK_GIANT(vfslocked); 1731 } 1732 done_errmsg: 1733 if (error) { 1734 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 1735 if (errmsg_len > 0) { 1736 errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1; 1737 if (errmsg_pos > 0) { 1738 if (optuio->uio_segflg == UIO_SYSSPACE) 1739 bcopy(errmsg, 1740 optuio->uio_iov[errmsg_pos].iov_base, 1741 errmsg_len); 1742 else 1743 copyout(errmsg, 1744 optuio->uio_iov[errmsg_pos].iov_base, 1745 errmsg_len); 1746 } 1747 } 1748 } 1749 done_free: 1750#ifdef INET 1751 free(ip4, M_PRISON); 1752#endif 1753#ifdef INET6 1754 free(ip6, M_PRISON); 1755#endif 1756 vfs_freeopts(opts); 1757 return (error); 1758} 1759 1760 1761/* 1762 * struct jail_get_args { 1763 * struct iovec *iovp; 1764 * unsigned int iovcnt; 1765 * int flags; 1766 * }; 1767 */ 1768int 1769jail_get(struct thread *td, struct jail_get_args *uap) 1770{ 1771 struct uio *auio; 1772 int error; 1773 1774 /* Check that we have an even number of iovecs. */ 1775 if (uap->iovcnt & 1) 1776 return (EINVAL); 1777 1778 error = copyinuio(uap->iovp, uap->iovcnt, &auio); 1779 if (error) 1780 return (error); 1781 error = kern_jail_get(td, auio, uap->flags); 1782 if (error == 0) 1783 error = copyout(auio->uio_iov, uap->iovp, 1784 uap->iovcnt * sizeof (struct iovec)); 1785 free(auio, M_IOV); 1786 return (error); 1787} 1788 1789int 1790kern_jail_get(struct thread *td, struct uio *optuio, int flags) 1791{ 1792 struct prison *pr, *mypr; 1793 struct vfsopt *opt; 1794 struct vfsoptlist *opts; 1795 char *errmsg, *name; 1796 int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos; 1797 1798 if (flags & ~JAIL_GET_MASK) 1799 return (EINVAL); 1800 1801 /* Get the parameter list. */ 1802 error = vfs_buildopts(optuio, &opts); 1803 if (error) 1804 return (error); 1805 errmsg_pos = vfs_getopt_pos(opts, "errmsg"); 1806 mypr = td->td_ucred->cr_prison; 1807 1808 /* 1809 * Find the prison specified by one of: lastjid, jid, name. 1810 */ 1811 sx_slock(&allprison_lock); 1812 error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid)); 1813 if (error == 0) { 1814 TAILQ_FOREACH(pr, &allprison, pr_list) { 1815 if (pr->pr_id > jid && prison_ischild(mypr, pr)) { 1816 mtx_lock(&pr->pr_mtx); 1817 if (pr->pr_ref > 0 && 1818 (pr->pr_uref > 0 || (flags & JAIL_DYING))) 1819 break; 1820 mtx_unlock(&pr->pr_mtx); 1821 } 1822 } 1823 if (pr != NULL) 1824 goto found_prison; 1825 error = ENOENT; 1826 vfs_opterror(opts, "no jail after %d", jid); 1827 goto done_unlock_list; 1828 } else if (error != ENOENT) 1829 goto done_unlock_list; 1830 1831 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 1832 if (error == 0) { 1833 if (jid != 0) { 1834 pr = prison_find_child(mypr, jid); 1835 if (pr != NULL) { 1836 if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 1837 mtx_unlock(&pr->pr_mtx); 1838 error = ENOENT; 1839 vfs_opterror(opts, "jail %d is dying", 1840 jid); 1841 goto done_unlock_list; 1842 } 1843 goto found_prison; 1844 } 1845 error = ENOENT; 1846 vfs_opterror(opts, "jail %d not found", jid); 1847 goto done_unlock_list; 1848 } 1849 } else if (error != ENOENT) 1850 goto done_unlock_list; 1851 1852 error = vfs_getopt(opts, "name", (void **)&name, &len); 1853 if (error == 0) { 1854 if (len == 0 || name[len - 1] != '\0') { 1855 error = EINVAL; 1856 goto done_unlock_list; 1857 } 1858 pr = prison_find_name(mypr, name); 1859 if (pr != NULL) { 1860 if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 1861 mtx_unlock(&pr->pr_mtx); 1862 error = ENOENT; 1863 vfs_opterror(opts, "jail \"%s\" is dying", 1864 name); 1865 goto done_unlock_list; 1866 } 1867 goto found_prison; 1868 } 1869 error = ENOENT; 1870 vfs_opterror(opts, "jail \"%s\" not found", name); 1871 goto done_unlock_list; 1872 } else if (error != ENOENT) 1873 goto done_unlock_list; 1874 1875 vfs_opterror(opts, "no jail specified"); 1876 error = ENOENT; 1877 goto done_unlock_list; 1878 1879 found_prison: 1880 /* Get the parameters of the prison. */ 1881 pr->pr_ref++; 1882 locked = PD_LOCKED; 1883 td->td_retval[0] = pr->pr_id; 1884 error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id)); 1885 if (error != 0 && error != ENOENT) 1886 goto done_deref; 1887 i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id; 1888 error = vfs_setopt(opts, "parent", &i, sizeof(i)); 1889 if (error != 0 && error != ENOENT) 1890 goto done_deref; 1891 error = vfs_setopts(opts, "name", prison_name(mypr, pr)); 1892 if (error != 0 && error != ENOENT) 1893 goto done_deref; 1894 error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id, 1895 sizeof(pr->pr_cpuset->cs_id)); 1896 if (error != 0 && error != ENOENT) 1897 goto done_deref; 1898 error = vfs_setopts(opts, "path", prison_path(mypr, pr)); 1899 if (error != 0 && error != ENOENT) 1900 goto done_deref; 1901#ifdef INET 1902 error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4, 1903 pr->pr_ip4s * sizeof(*pr->pr_ip4)); 1904 if (error != 0 && error != ENOENT) 1905 goto done_deref; 1906#endif 1907#ifdef INET6 1908 error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6, 1909 pr->pr_ip6s * sizeof(*pr->pr_ip6)); 1910 if (error != 0 && error != ENOENT) 1911 goto done_deref; 1912#endif 1913 error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel, 1914 sizeof(pr->pr_securelevel)); 1915 if (error != 0 && error != ENOENT) 1916 goto done_deref; 1917 error = vfs_setopt(opts, "children.cur", &pr->pr_childcount, 1918 sizeof(pr->pr_childcount)); 1919 if (error != 0 && error != ENOENT) 1920 goto done_deref; 1921 error = vfs_setopt(opts, "children.max", &pr->pr_childmax, 1922 sizeof(pr->pr_childmax)); 1923 if (error != 0 && error != ENOENT) 1924 goto done_deref; 1925 error = vfs_setopts(opts, "host.hostname", pr->pr_hostname); 1926 if (error != 0 && error != ENOENT) 1927 goto done_deref; 1928 error = vfs_setopts(opts, "host.domainname", pr->pr_domainname); 1929 if (error != 0 && error != ENOENT) 1930 goto done_deref; 1931 error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid); 1932 if (error != 0 && error != ENOENT) 1933 goto done_deref; 1934#ifdef COMPAT_IA32 1935 if (td->td_proc->p_sysent->sv_flags & SV_IA32) { 1936 uint32_t hid32 = pr->pr_hostid; 1937 1938 error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32)); 1939 } else 1940#endif 1941 error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid, 1942 sizeof(pr->pr_hostid)); 1943 if (error != 0 && error != ENOENT) 1944 goto done_deref; 1945 error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs, 1946 sizeof(pr->pr_enforce_statfs)); 1947 if (error != 0 && error != ENOENT) 1948 goto done_deref; 1949 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 1950 fi++) { 1951 if (pr_flag_names[fi] == NULL) 1952 continue; 1953 i = (pr->pr_flags & (1 << fi)) ? 1 : 0; 1954 error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i)); 1955 if (error != 0 && error != ENOENT) 1956 goto done_deref; 1957 i = !i; 1958 error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i)); 1959 if (error != 0 && error != ENOENT) 1960 goto done_deref; 1961 } 1962 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 1963 fi++) { 1964 i = pr->pr_flags & 1965 (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new); 1966 i = pr_flag_jailsys[fi].disable && 1967 (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE 1968 : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW 1969 : JAIL_SYS_INHERIT; 1970 error = 1971 vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i)); 1972 if (error != 0 && error != ENOENT) 1973 goto done_deref; 1974 } 1975 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 1976 fi++) { 1977 if (pr_allow_names[fi] == NULL) 1978 continue; 1979 i = (pr->pr_allow & (1 << fi)) ? 1 : 0; 1980 error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i)); 1981 if (error != 0 && error != ENOENT) 1982 goto done_deref; 1983 i = !i; 1984 error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i)); 1985 if (error != 0 && error != ENOENT) 1986 goto done_deref; 1987 } 1988 i = (pr->pr_uref == 0); 1989 error = vfs_setopt(opts, "dying", &i, sizeof(i)); 1990 if (error != 0 && error != ENOENT) 1991 goto done_deref; 1992 i = !i; 1993 error = vfs_setopt(opts, "nodying", &i, sizeof(i)); 1994 if (error != 0 && error != ENOENT) 1995 goto done_deref; 1996 1997 /* Get the module parameters. */ 1998 mtx_unlock(&pr->pr_mtx); 1999 locked = 0; 2000 error = osd_jail_call(pr, PR_METHOD_GET, opts); 2001 if (error) 2002 goto done_deref; 2003 prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED); 2004 2005 /* By now, all parameters should have been noted. */ 2006 TAILQ_FOREACH(opt, opts, link) { 2007 if (!opt->seen && strcmp(opt->name, "errmsg")) { 2008 error = EINVAL; 2009 vfs_opterror(opts, "unknown parameter: %s", opt->name); 2010 goto done_errmsg; 2011 } 2012 } 2013 2014 /* Write the fetched parameters back to userspace. */ 2015 error = 0; 2016 TAILQ_FOREACH(opt, opts, link) { 2017 if (opt->pos >= 0 && opt->pos != errmsg_pos) { 2018 pos = 2 * opt->pos + 1; 2019 optuio->uio_iov[pos].iov_len = opt->len; 2020 if (opt->value != NULL) { 2021 if (optuio->uio_segflg == UIO_SYSSPACE) { 2022 bcopy(opt->value, 2023 optuio->uio_iov[pos].iov_base, 2024 opt->len); 2025 } else { 2026 error = copyout(opt->value, 2027 optuio->uio_iov[pos].iov_base, 2028 opt->len); 2029 if (error) 2030 break; 2031 } 2032 } 2033 } 2034 } 2035 goto done_errmsg; 2036 2037 done_deref: 2038 prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED); 2039 goto done_errmsg; 2040 2041 done_unlock_list: 2042 sx_sunlock(&allprison_lock); 2043 done_errmsg: 2044 if (error && errmsg_pos >= 0) { 2045 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 2046 errmsg_pos = 2 * errmsg_pos + 1; 2047 if (errmsg_len > 0) { 2048 if (optuio->uio_segflg == UIO_SYSSPACE) 2049 bcopy(errmsg, 2050 optuio->uio_iov[errmsg_pos].iov_base, 2051 errmsg_len); 2052 else 2053 copyout(errmsg, 2054 optuio->uio_iov[errmsg_pos].iov_base, 2055 errmsg_len); 2056 } 2057 } 2058 vfs_freeopts(opts); 2059 return (error); 2060} 2061 2062 2063/* 2064 * struct jail_remove_args { 2065 * int jid; 2066 * }; 2067 */ 2068int 2069jail_remove(struct thread *td, struct jail_remove_args *uap) 2070{ 2071 struct prison *pr, *cpr, *lpr, *tpr; 2072 int descend, error; 2073 2074 error = priv_check(td, PRIV_JAIL_REMOVE); 2075 if (error) 2076 return (error); 2077 2078 sx_xlock(&allprison_lock); 2079 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2080 if (pr == NULL) { 2081 sx_xunlock(&allprison_lock); 2082 return (EINVAL); 2083 } 2084 2085 /* Remove all descendants of this prison, then remove this prison. */ 2086 pr->pr_ref++; 2087 pr->pr_flags |= PR_REMOVE; 2088 if (!LIST_EMPTY(&pr->pr_children)) { 2089 mtx_unlock(&pr->pr_mtx); 2090 lpr = NULL; 2091 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 2092 mtx_lock(&cpr->pr_mtx); 2093 if (cpr->pr_ref > 0) { 2094 tpr = cpr; 2095 cpr->pr_ref++; 2096 cpr->pr_flags |= PR_REMOVE; 2097 } else { 2098 /* Already removed - do not do it again. */ 2099 tpr = NULL; 2100 } 2101 mtx_unlock(&cpr->pr_mtx); 2102 if (lpr != NULL) { 2103 mtx_lock(&lpr->pr_mtx); 2104 prison_remove_one(lpr); 2105 sx_xlock(&allprison_lock); 2106 } 2107 lpr = tpr; 2108 } 2109 if (lpr != NULL) { 2110 mtx_lock(&lpr->pr_mtx); 2111 prison_remove_one(lpr); 2112 sx_xlock(&allprison_lock); 2113 } 2114 mtx_lock(&pr->pr_mtx); 2115 } 2116 prison_remove_one(pr); 2117 return (0); 2118} 2119 2120static void 2121prison_remove_one(struct prison *pr) 2122{ 2123 struct proc *p; 2124 int deuref; 2125 2126 /* If the prison was persistent, it is not anymore. */ 2127 deuref = 0; 2128 if (pr->pr_flags & PR_PERSIST) { 2129 pr->pr_ref--; 2130 deuref = PD_DEUREF; 2131 pr->pr_flags &= ~PR_PERSIST; 2132 } 2133 2134 /* 2135 * jail_remove added a reference. If that's the only one, remove 2136 * the prison now. 2137 */ 2138 KASSERT(pr->pr_ref > 0, 2139 ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id)); 2140 if (pr->pr_ref == 1) { 2141 prison_deref(pr, 2142 deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 2143 return; 2144 } 2145 2146 mtx_unlock(&pr->pr_mtx); 2147 sx_xunlock(&allprison_lock); 2148 /* 2149 * Kill all processes unfortunate enough to be attached to this prison. 2150 */ 2151 sx_slock(&allproc_lock); 2152 LIST_FOREACH(p, &allproc, p_list) { 2153 PROC_LOCK(p); 2154 if (p->p_state != PRS_NEW && p->p_ucred && 2155 p->p_ucred->cr_prison == pr) 2156 psignal(p, SIGKILL); 2157 PROC_UNLOCK(p); 2158 } 2159 sx_sunlock(&allproc_lock); 2160 /* Remove the temporary reference added by jail_remove. */ 2161 prison_deref(pr, deuref | PD_DEREF); 2162} 2163 2164 2165/* 2166 * struct jail_attach_args { 2167 * int jid; 2168 * }; 2169 */ 2170int 2171jail_attach(struct thread *td, struct jail_attach_args *uap) 2172{ 2173 struct prison *pr; 2174 int error; 2175 2176 error = priv_check(td, PRIV_JAIL_ATTACH); 2177 if (error) 2178 return (error); 2179 2180 sx_slock(&allprison_lock); 2181 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2182 if (pr == NULL) { 2183 sx_sunlock(&allprison_lock); 2184 return (EINVAL); 2185 } 2186 2187 /* 2188 * Do not allow a process to attach to a prison that is not 2189 * considered to be "alive". 2190 */ 2191 if (pr->pr_uref == 0) { 2192 mtx_unlock(&pr->pr_mtx); 2193 sx_sunlock(&allprison_lock); 2194 return (EINVAL); 2195 } 2196 2197 return (do_jail_attach(td, pr)); 2198} 2199 2200static int 2201do_jail_attach(struct thread *td, struct prison *pr) 2202{ 2203 struct prison *ppr; 2204 struct proc *p; 2205 struct ucred *newcred, *oldcred; 2206 int vfslocked, error; 2207 2208 /* 2209 * XXX: Note that there is a slight race here if two threads 2210 * in the same privileged process attempt to attach to two 2211 * different jails at the same time. It is important for 2212 * user processes not to do this, or they might end up with 2213 * a process root from one prison, but attached to the jail 2214 * of another. 2215 */ 2216 pr->pr_ref++; 2217 pr->pr_uref++; 2218 mtx_unlock(&pr->pr_mtx); 2219 2220 /* Let modules do whatever they need to prepare for attaching. */ 2221 error = osd_jail_call(pr, PR_METHOD_ATTACH, td); 2222 if (error) { 2223 prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED); 2224 return (error); 2225 } 2226 sx_sunlock(&allprison_lock); 2227 2228 /* 2229 * Reparent the newly attached process to this jail. 2230 */ 2231 ppr = td->td_ucred->cr_prison; 2232 p = td->td_proc; 2233 error = cpuset_setproc_update_set(p, pr->pr_cpuset); 2234 if (error) 2235 goto e_revert_osd; 2236 2237 vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount); 2238 vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY); 2239 if ((error = change_dir(pr->pr_root, td)) != 0) 2240 goto e_unlock; 2241#ifdef MAC 2242 if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root))) 2243 goto e_unlock; 2244#endif 2245 VOP_UNLOCK(pr->pr_root, 0); 2246 if ((error = change_root(pr->pr_root, td))) 2247 goto e_unlock_giant; 2248 VFS_UNLOCK_GIANT(vfslocked); 2249 2250 newcred = crget(); 2251 PROC_LOCK(p); 2252 oldcred = p->p_ucred; 2253 setsugid(p); 2254 crcopy(newcred, oldcred); 2255 newcred->cr_prison = pr; 2256 p->p_ucred = newcred; 2257 PROC_UNLOCK(p); 2258 crfree(oldcred); 2259 prison_deref(ppr, PD_DEREF | PD_DEUREF); 2260 return (0); 2261 e_unlock: 2262 VOP_UNLOCK(pr->pr_root, 0); 2263 e_unlock_giant: 2264 VFS_UNLOCK_GIANT(vfslocked); 2265 e_revert_osd: 2266 /* Tell modules this thread is still in its old jail after all. */ 2267 (void)osd_jail_call(ppr, PR_METHOD_ATTACH, td); 2268 prison_deref(pr, PD_DEREF | PD_DEUREF); 2269 return (error); 2270} 2271 2272 2273/* 2274 * Returns a locked prison instance, or NULL on failure. 2275 */ 2276struct prison * 2277prison_find(int prid) 2278{ 2279 struct prison *pr; 2280 2281 sx_assert(&allprison_lock, SX_LOCKED); 2282 TAILQ_FOREACH(pr, &allprison, pr_list) { 2283 if (pr->pr_id == prid) { 2284 mtx_lock(&pr->pr_mtx); 2285 if (pr->pr_ref > 0) 2286 return (pr); 2287 mtx_unlock(&pr->pr_mtx); 2288 } 2289 } 2290 return (NULL); 2291} 2292 2293/* 2294 * Find a prison that is a descendant of mypr. Returns a locked prison or NULL. 2295 */ 2296struct prison * 2297prison_find_child(struct prison *mypr, int prid) 2298{ 2299 struct prison *pr; 2300 int descend; 2301 2302 sx_assert(&allprison_lock, SX_LOCKED); 2303 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 2304 if (pr->pr_id == prid) { 2305 mtx_lock(&pr->pr_mtx); 2306 if (pr->pr_ref > 0) 2307 return (pr); 2308 mtx_unlock(&pr->pr_mtx); 2309 } 2310 } 2311 return (NULL); 2312} 2313 2314/* 2315 * Look for the name relative to mypr. Returns a locked prison or NULL. 2316 */ 2317struct prison * 2318prison_find_name(struct prison *mypr, const char *name) 2319{ 2320 struct prison *pr, *deadpr; 2321 size_t mylen; 2322 int descend; 2323 2324 sx_assert(&allprison_lock, SX_LOCKED); 2325 mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1; 2326 again: 2327 deadpr = NULL; 2328 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 2329 if (!strcmp(pr->pr_name + mylen, name)) { 2330 mtx_lock(&pr->pr_mtx); 2331 if (pr->pr_ref > 0) { 2332 if (pr->pr_uref > 0) 2333 return (pr); 2334 deadpr = pr; 2335 } 2336 mtx_unlock(&pr->pr_mtx); 2337 } 2338 } 2339 /* There was no valid prison - perhaps there was a dying one. */ 2340 if (deadpr != NULL) { 2341 mtx_lock(&deadpr->pr_mtx); 2342 if (deadpr->pr_ref == 0) { 2343 mtx_unlock(&deadpr->pr_mtx); 2344 goto again; 2345 } 2346 } 2347 return (deadpr); 2348} 2349 2350/* 2351 * See if a prison has the specific flag set. 2352 */ 2353int 2354prison_flag(struct ucred *cred, unsigned flag) 2355{ 2356 2357 /* This is an atomic read, so no locking is necessary. */ 2358 return (cred->cr_prison->pr_flags & flag); 2359} 2360 2361int 2362prison_allow(struct ucred *cred, unsigned flag) 2363{ 2364 2365 /* This is an atomic read, so no locking is necessary. */ 2366 return (cred->cr_prison->pr_allow & flag); 2367} 2368 2369/* 2370 * Remove a prison reference. If that was the last reference, remove the 2371 * prison itself - but not in this context in case there are locks held. 2372 */ 2373void 2374prison_free_locked(struct prison *pr) 2375{ 2376 2377 mtx_assert(&pr->pr_mtx, MA_OWNED); 2378 pr->pr_ref--; 2379 if (pr->pr_ref == 0) { 2380 mtx_unlock(&pr->pr_mtx); 2381 TASK_INIT(&pr->pr_task, 0, prison_complete, pr); 2382 taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 2383 return; 2384 } 2385 mtx_unlock(&pr->pr_mtx); 2386} 2387 2388void 2389prison_free(struct prison *pr) 2390{ 2391 2392 mtx_lock(&pr->pr_mtx); 2393 prison_free_locked(pr); 2394} 2395 2396static void 2397prison_complete(void *context, int pending) 2398{ 2399 2400 prison_deref((struct prison *)context, 0); 2401} 2402 2403/* 2404 * Remove a prison reference (usually). This internal version assumes no 2405 * mutexes are held, except perhaps the prison itself. If there are no more 2406 * references, release and delist the prison. On completion, the prison lock 2407 * and the allprison lock are both unlocked. 2408 */ 2409static void 2410prison_deref(struct prison *pr, int flags) 2411{ 2412 struct prison *ppr, *tpr; 2413 int vfslocked; 2414 2415 if (!(flags & PD_LOCKED)) 2416 mtx_lock(&pr->pr_mtx); 2417 /* Decrement the user references in a separate loop. */ 2418 if (flags & PD_DEUREF) { 2419 for (tpr = pr;; tpr = tpr->pr_parent) { 2420 if (tpr != pr) 2421 mtx_lock(&tpr->pr_mtx); 2422 if (--tpr->pr_uref > 0) 2423 break; 2424 KASSERT(tpr != &prison0, ("prison0 pr_uref=0")); 2425 mtx_unlock(&tpr->pr_mtx); 2426 } 2427 /* Done if there were only user references to remove. */ 2428 if (!(flags & PD_DEREF)) { 2429 mtx_unlock(&tpr->pr_mtx); 2430 if (flags & PD_LIST_SLOCKED) 2431 sx_sunlock(&allprison_lock); 2432 else if (flags & PD_LIST_XLOCKED) 2433 sx_xunlock(&allprison_lock); 2434 return; 2435 } 2436 if (tpr != pr) { 2437 mtx_unlock(&tpr->pr_mtx); 2438 mtx_lock(&pr->pr_mtx); 2439 } 2440 } 2441 2442 for (;;) { 2443 if (flags & PD_DEREF) 2444 pr->pr_ref--; 2445 /* If the prison still has references, nothing else to do. */ 2446 if (pr->pr_ref > 0) { 2447 mtx_unlock(&pr->pr_mtx); 2448 if (flags & PD_LIST_SLOCKED) 2449 sx_sunlock(&allprison_lock); 2450 else if (flags & PD_LIST_XLOCKED) 2451 sx_xunlock(&allprison_lock); 2452 return; 2453 } 2454 2455 mtx_unlock(&pr->pr_mtx); 2456 if (flags & PD_LIST_SLOCKED) { 2457 if (!sx_try_upgrade(&allprison_lock)) { 2458 sx_sunlock(&allprison_lock); 2459 sx_xlock(&allprison_lock); 2460 } 2461 } else if (!(flags & PD_LIST_XLOCKED)) 2462 sx_xlock(&allprison_lock); 2463 2464 TAILQ_REMOVE(&allprison, pr, pr_list); 2465 LIST_REMOVE(pr, pr_sibling); 2466 ppr = pr->pr_parent; 2467 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 2468 tpr->pr_childcount--; 2469 sx_xunlock(&allprison_lock); 2470 2471#ifdef VIMAGE 2472 if (pr->pr_vnet != ppr->pr_vnet) 2473 vnet_destroy(pr->pr_vnet); 2474#endif 2475 if (pr->pr_root != NULL) { 2476 vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount); 2477 vrele(pr->pr_root); 2478 VFS_UNLOCK_GIANT(vfslocked); 2479 } 2480 mtx_destroy(&pr->pr_mtx); 2481#ifdef INET 2482 free(pr->pr_ip4, M_PRISON); 2483#endif 2484#ifdef INET6 2485 free(pr->pr_ip6, M_PRISON); 2486#endif 2487 if (pr->pr_cpuset != NULL) 2488 cpuset_rel(pr->pr_cpuset); 2489 osd_jail_exit(pr); 2490 free(pr, M_PRISON); 2491 2492 /* Removing a prison frees a reference on its parent. */ 2493 pr = ppr; 2494 mtx_lock(&pr->pr_mtx); 2495 flags = PD_DEREF; 2496 } 2497} 2498 2499void 2500prison_hold_locked(struct prison *pr) 2501{ 2502 2503 mtx_assert(&pr->pr_mtx, MA_OWNED); 2504 KASSERT(pr->pr_ref > 0, 2505 ("Trying to hold dead prison (jid=%d).", pr->pr_id)); 2506 pr->pr_ref++; 2507} 2508 2509void 2510prison_hold(struct prison *pr) 2511{ 2512 2513 mtx_lock(&pr->pr_mtx); 2514 prison_hold_locked(pr); 2515 mtx_unlock(&pr->pr_mtx); 2516} 2517 2518void 2519prison_proc_hold(struct prison *pr) 2520{ 2521 2522 mtx_lock(&pr->pr_mtx); 2523 KASSERT(pr->pr_uref > 0, 2524 ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id)); 2525 pr->pr_uref++; 2526 mtx_unlock(&pr->pr_mtx); 2527} 2528 2529void 2530prison_proc_free(struct prison *pr) 2531{ 2532 2533 mtx_lock(&pr->pr_mtx); 2534 KASSERT(pr->pr_uref > 0, 2535 ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id)); 2536 prison_deref(pr, PD_DEUREF | PD_LOCKED); 2537} 2538 2539 2540#ifdef INET 2541/* 2542 * Restrict a prison's IP address list with its parent's, possibly replacing 2543 * it. Return true if the replacement buffer was used (or would have been). 2544 */ 2545static int 2546prison_restrict_ip4(struct prison *pr, struct in_addr *newip4) 2547{ 2548 int ii, ij, used; 2549 struct prison *ppr; 2550 2551 ppr = pr->pr_parent; 2552 if (!(pr->pr_flags & PR_IP4_USER)) { 2553 /* This has no user settings, so just copy the parent's list. */ 2554 if (pr->pr_ip4s < ppr->pr_ip4s) { 2555 /* 2556 * There's no room for the parent's list. Use the 2557 * new list buffer, which is assumed to be big enough 2558 * (if it was passed). If there's no buffer, try to 2559 * allocate one. 2560 */ 2561 used = 1; 2562 if (newip4 == NULL) { 2563 newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4), 2564 M_PRISON, M_NOWAIT); 2565 if (newip4 != NULL) 2566 used = 0; 2567 } 2568 if (newip4 != NULL) { 2569 bcopy(ppr->pr_ip4, newip4, 2570 ppr->pr_ip4s * sizeof(*newip4)); 2571 free(pr->pr_ip4, M_PRISON); 2572 pr->pr_ip4 = newip4; 2573 pr->pr_ip4s = ppr->pr_ip4s; 2574 } 2575 return (used); 2576 } 2577 pr->pr_ip4s = ppr->pr_ip4s; 2578 if (pr->pr_ip4s > 0) 2579 bcopy(ppr->pr_ip4, pr->pr_ip4, 2580 pr->pr_ip4s * sizeof(*newip4)); 2581 else if (pr->pr_ip4 != NULL) { 2582 free(pr->pr_ip4, M_PRISON); 2583 pr->pr_ip4 = NULL; 2584 } 2585 } else if (pr->pr_ip4s > 0) { 2586 /* Remove addresses that aren't in the parent. */ 2587 for (ij = 0; ij < ppr->pr_ip4s; ij++) 2588 if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 2589 break; 2590 if (ij < ppr->pr_ip4s) 2591 ii = 1; 2592 else { 2593 bcopy(pr->pr_ip4 + 1, pr->pr_ip4, 2594 --pr->pr_ip4s * sizeof(*pr->pr_ip4)); 2595 ii = 0; 2596 } 2597 for (ij = 1; ii < pr->pr_ip4s; ) { 2598 if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) { 2599 ii++; 2600 continue; 2601 } 2602 switch (ij >= ppr->pr_ip4s ? -1 : 2603 qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) { 2604 case -1: 2605 bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii, 2606 (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4)); 2607 break; 2608 case 0: 2609 ii++; 2610 ij++; 2611 break; 2612 case 1: 2613 ij++; 2614 break; 2615 } 2616 } 2617 if (pr->pr_ip4s == 0) { 2618 pr->pr_flags |= PR_IP4_DISABLE; 2619 free(pr->pr_ip4, M_PRISON); 2620 pr->pr_ip4 = NULL; 2621 } 2622 } 2623 return (0); 2624} 2625 2626/* 2627 * Pass back primary IPv4 address of this jail. 2628 * 2629 * If not restricted return success but do not alter the address. Caller has 2630 * to make sure to initialize it correctly (e.g. INADDR_ANY). 2631 * 2632 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4. 2633 * Address returned in NBO. 2634 */ 2635int 2636prison_get_ip4(struct ucred *cred, struct in_addr *ia) 2637{ 2638 struct prison *pr; 2639 2640 KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2641 KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2642 2643 pr = cred->cr_prison; 2644 if (!(pr->pr_flags & PR_IP4)) 2645 return (0); 2646 mtx_lock(&pr->pr_mtx); 2647 if (!(pr->pr_flags & PR_IP4)) { 2648 mtx_unlock(&pr->pr_mtx); 2649 return (0); 2650 } 2651 if (pr->pr_ip4 == NULL) { 2652 mtx_unlock(&pr->pr_mtx); 2653 return (EAFNOSUPPORT); 2654 } 2655 2656 ia->s_addr = pr->pr_ip4[0].s_addr; 2657 mtx_unlock(&pr->pr_mtx); 2658 return (0); 2659} 2660 2661/* 2662 * Return true if pr1 and pr2 have the same IPv4 address restrictions. 2663 */ 2664int 2665prison_equal_ip4(struct prison *pr1, struct prison *pr2) 2666{ 2667 2668 if (pr1 == pr2) 2669 return (1); 2670 2671 /* 2672 * No need to lock since the PR_IP4_USER flag can't be altered for 2673 * existing prisons. 2674 */ 2675 while (pr1 != &prison0 && 2676#ifdef VIMAGE 2677 !(pr1->pr_flags & PR_VNET) && 2678#endif 2679 !(pr1->pr_flags & PR_IP4_USER)) 2680 pr1 = pr1->pr_parent; 2681 while (pr2 != &prison0 && 2682#ifdef VIMAGE 2683 !(pr2->pr_flags & PR_VNET) && 2684#endif 2685 !(pr2->pr_flags & PR_IP4_USER)) 2686 pr2 = pr2->pr_parent; 2687 return (pr1 == pr2); 2688} 2689 2690/* 2691 * Make sure our (source) address is set to something meaningful to this 2692 * jail. 2693 * 2694 * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail, 2695 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 2696 * doesn't allow IPv4. Address passed in in NBO and returned in NBO. 2697 */ 2698int 2699prison_local_ip4(struct ucred *cred, struct in_addr *ia) 2700{ 2701 struct prison *pr; 2702 struct in_addr ia0; 2703 int error; 2704 2705 KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2706 KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2707 2708 pr = cred->cr_prison; 2709 if (!(pr->pr_flags & PR_IP4)) 2710 return (0); 2711 mtx_lock(&pr->pr_mtx); 2712 if (!(pr->pr_flags & PR_IP4)) { 2713 mtx_unlock(&pr->pr_mtx); 2714 return (0); 2715 } 2716 if (pr->pr_ip4 == NULL) { 2717 mtx_unlock(&pr->pr_mtx); 2718 return (EAFNOSUPPORT); 2719 } 2720 2721 ia0.s_addr = ntohl(ia->s_addr); 2722 if (ia0.s_addr == INADDR_LOOPBACK) { 2723 ia->s_addr = pr->pr_ip4[0].s_addr; 2724 mtx_unlock(&pr->pr_mtx); 2725 return (0); 2726 } 2727 2728 if (ia0.s_addr == INADDR_ANY) { 2729 /* 2730 * In case there is only 1 IPv4 address, bind directly. 2731 */ 2732 if (pr->pr_ip4s == 1) 2733 ia->s_addr = pr->pr_ip4[0].s_addr; 2734 mtx_unlock(&pr->pr_mtx); 2735 return (0); 2736 } 2737 2738 error = _prison_check_ip4(pr, ia); 2739 mtx_unlock(&pr->pr_mtx); 2740 return (error); 2741} 2742 2743/* 2744 * Rewrite destination address in case we will connect to loopback address. 2745 * 2746 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4. 2747 * Address passed in in NBO and returned in NBO. 2748 */ 2749int 2750prison_remote_ip4(struct ucred *cred, struct in_addr *ia) 2751{ 2752 struct prison *pr; 2753 2754 KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2755 KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2756 2757 pr = cred->cr_prison; 2758 if (!(pr->pr_flags & PR_IP4)) 2759 return (0); 2760 mtx_lock(&pr->pr_mtx); 2761 if (!(pr->pr_flags & PR_IP4)) { 2762 mtx_unlock(&pr->pr_mtx); 2763 return (0); 2764 } 2765 if (pr->pr_ip4 == NULL) { 2766 mtx_unlock(&pr->pr_mtx); 2767 return (EAFNOSUPPORT); 2768 } 2769 2770 if (ntohl(ia->s_addr) == INADDR_LOOPBACK) { 2771 ia->s_addr = pr->pr_ip4[0].s_addr; 2772 mtx_unlock(&pr->pr_mtx); 2773 return (0); 2774 } 2775 2776 /* 2777 * Return success because nothing had to be changed. 2778 */ 2779 mtx_unlock(&pr->pr_mtx); 2780 return (0); 2781} 2782 2783/* 2784 * Check if given address belongs to the jail referenced by cred/prison. 2785 * 2786 * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail, 2787 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 2788 * doesn't allow IPv4. Address passed in in NBO. 2789 */ 2790static int 2791_prison_check_ip4(struct prison *pr, struct in_addr *ia) 2792{ 2793 int i, a, z, d; 2794 2795 /* 2796 * Check the primary IP. 2797 */ 2798 if (pr->pr_ip4[0].s_addr == ia->s_addr) 2799 return (0); 2800 2801 /* 2802 * All the other IPs are sorted so we can do a binary search. 2803 */ 2804 a = 0; 2805 z = pr->pr_ip4s - 2; 2806 while (a <= z) { 2807 i = (a + z) / 2; 2808 d = qcmp_v4(&pr->pr_ip4[i+1], ia); 2809 if (d > 0) 2810 z = i - 1; 2811 else if (d < 0) 2812 a = i + 1; 2813 else 2814 return (0); 2815 } 2816 2817 return (EADDRNOTAVAIL); 2818} 2819 2820int 2821prison_check_ip4(struct ucred *cred, struct in_addr *ia) 2822{ 2823 struct prison *pr; 2824 int error; 2825 2826 KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2827 KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2828 2829 pr = cred->cr_prison; 2830 if (!(pr->pr_flags & PR_IP4)) 2831 return (0); 2832 mtx_lock(&pr->pr_mtx); 2833 if (!(pr->pr_flags & PR_IP4)) { 2834 mtx_unlock(&pr->pr_mtx); 2835 return (0); 2836 } 2837 if (pr->pr_ip4 == NULL) { 2838 mtx_unlock(&pr->pr_mtx); 2839 return (EAFNOSUPPORT); 2840 } 2841 2842 error = _prison_check_ip4(pr, ia); 2843 mtx_unlock(&pr->pr_mtx); 2844 return (error); 2845} 2846#endif 2847 2848#ifdef INET6 2849static int 2850prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6) 2851{ 2852 int ii, ij, used; 2853 struct prison *ppr; 2854 2855 ppr = pr->pr_parent; 2856 if (!(pr->pr_flags & PR_IP6_USER)) { 2857 /* This has no user settings, so just copy the parent's list. */ 2858 if (pr->pr_ip6s < ppr->pr_ip6s) { 2859 /* 2860 * There's no room for the parent's list. Use the 2861 * new list buffer, which is assumed to be big enough 2862 * (if it was passed). If there's no buffer, try to 2863 * allocate one. 2864 */ 2865 used = 1; 2866 if (newip6 == NULL) { 2867 newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6), 2868 M_PRISON, M_NOWAIT); 2869 if (newip6 != NULL) 2870 used = 0; 2871 } 2872 if (newip6 != NULL) { 2873 bcopy(ppr->pr_ip6, newip6, 2874 ppr->pr_ip6s * sizeof(*newip6)); 2875 free(pr->pr_ip6, M_PRISON); 2876 pr->pr_ip6 = newip6; 2877 pr->pr_ip6s = ppr->pr_ip6s; 2878 } 2879 return (used); 2880 } 2881 pr->pr_ip6s = ppr->pr_ip6s; 2882 if (pr->pr_ip6s > 0) 2883 bcopy(ppr->pr_ip6, pr->pr_ip6, 2884 pr->pr_ip6s * sizeof(*newip6)); 2885 else if (pr->pr_ip6 != NULL) { 2886 free(pr->pr_ip6, M_PRISON); 2887 pr->pr_ip6 = NULL; 2888 } 2889 } else if (pr->pr_ip6s > 0) { 2890 /* Remove addresses that aren't in the parent. */ 2891 for (ij = 0; ij < ppr->pr_ip6s; ij++) 2892 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], 2893 &ppr->pr_ip6[ij])) 2894 break; 2895 if (ij < ppr->pr_ip6s) 2896 ii = 1; 2897 else { 2898 bcopy(pr->pr_ip6 + 1, pr->pr_ip6, 2899 --pr->pr_ip6s * sizeof(*pr->pr_ip6)); 2900 ii = 0; 2901 } 2902 for (ij = 1; ii < pr->pr_ip6s; ) { 2903 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii], 2904 &ppr->pr_ip6[0])) { 2905 ii++; 2906 continue; 2907 } 2908 switch (ij >= ppr->pr_ip4s ? -1 : 2909 qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) { 2910 case -1: 2911 bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii, 2912 (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6)); 2913 break; 2914 case 0: 2915 ii++; 2916 ij++; 2917 break; 2918 case 1: 2919 ij++; 2920 break; 2921 } 2922 } 2923 if (pr->pr_ip6s == 0) { 2924 pr->pr_flags |= PR_IP6_DISABLE; 2925 free(pr->pr_ip6, M_PRISON); 2926 pr->pr_ip6 = NULL; 2927 } 2928 } 2929 return 0; 2930} 2931 2932/* 2933 * Pass back primary IPv6 address for this jail. 2934 * 2935 * If not restricted return success but do not alter the address. Caller has 2936 * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT). 2937 * 2938 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6. 2939 */ 2940int 2941prison_get_ip6(struct ucred *cred, struct in6_addr *ia6) 2942{ 2943 struct prison *pr; 2944 2945 KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2946 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 2947 2948 pr = cred->cr_prison; 2949 if (!(pr->pr_flags & PR_IP6)) 2950 return (0); 2951 mtx_lock(&pr->pr_mtx); 2952 if (!(pr->pr_flags & PR_IP6)) { 2953 mtx_unlock(&pr->pr_mtx); 2954 return (0); 2955 } 2956 if (pr->pr_ip6 == NULL) { 2957 mtx_unlock(&pr->pr_mtx); 2958 return (EAFNOSUPPORT); 2959 } 2960 2961 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 2962 mtx_unlock(&pr->pr_mtx); 2963 return (0); 2964} 2965 2966/* 2967 * Return true if pr1 and pr2 have the same IPv6 address restrictions. 2968 */ 2969int 2970prison_equal_ip6(struct prison *pr1, struct prison *pr2) 2971{ 2972 2973 if (pr1 == pr2) 2974 return (1); 2975 2976 while (pr1 != &prison0 && 2977#ifdef VIMAGE 2978 !(pr1->pr_flags & PR_VNET) && 2979#endif 2980 !(pr1->pr_flags & PR_IP6_USER)) 2981 pr1 = pr1->pr_parent; 2982 while (pr2 != &prison0 && 2983#ifdef VIMAGE 2984 !(pr2->pr_flags & PR_VNET) && 2985#endif 2986 !(pr2->pr_flags & PR_IP6_USER)) 2987 pr2 = pr2->pr_parent; 2988 return (pr1 == pr2); 2989} 2990 2991/* 2992 * Make sure our (source) address is set to something meaningful to this jail. 2993 * 2994 * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0) 2995 * when needed while binding. 2996 * 2997 * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail, 2998 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 2999 * doesn't allow IPv6. 3000 */ 3001int 3002prison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only) 3003{ 3004 struct prison *pr; 3005 int error; 3006 3007 KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3008 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3009 3010 pr = cred->cr_prison; 3011 if (!(pr->pr_flags & PR_IP6)) 3012 return (0); 3013 mtx_lock(&pr->pr_mtx); 3014 if (!(pr->pr_flags & PR_IP6)) { 3015 mtx_unlock(&pr->pr_mtx); 3016 return (0); 3017 } 3018 if (pr->pr_ip6 == NULL) { 3019 mtx_unlock(&pr->pr_mtx); 3020 return (EAFNOSUPPORT); 3021 } 3022 3023 if (IN6_IS_ADDR_LOOPBACK(ia6)) { 3024 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3025 mtx_unlock(&pr->pr_mtx); 3026 return (0); 3027 } 3028 3029 if (IN6_IS_ADDR_UNSPECIFIED(ia6)) { 3030 /* 3031 * In case there is only 1 IPv6 address, and v6only is true, 3032 * then bind directly. 3033 */ 3034 if (v6only != 0 && pr->pr_ip6s == 1) 3035 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3036 mtx_unlock(&pr->pr_mtx); 3037 return (0); 3038 } 3039 3040 error = _prison_check_ip6(pr, ia6); 3041 mtx_unlock(&pr->pr_mtx); 3042 return (error); 3043} 3044 3045/* 3046 * Rewrite destination address in case we will connect to loopback address. 3047 * 3048 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6. 3049 */ 3050int 3051prison_remote_ip6(struct ucred *cred, struct in6_addr *ia6) 3052{ 3053 struct prison *pr; 3054 3055 KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3056 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3057 3058 pr = cred->cr_prison; 3059 if (!(pr->pr_flags & PR_IP6)) 3060 return (0); 3061 mtx_lock(&pr->pr_mtx); 3062 if (!(pr->pr_flags & PR_IP6)) { 3063 mtx_unlock(&pr->pr_mtx); 3064 return (0); 3065 } 3066 if (pr->pr_ip6 == NULL) { 3067 mtx_unlock(&pr->pr_mtx); 3068 return (EAFNOSUPPORT); 3069 } 3070 3071 if (IN6_IS_ADDR_LOOPBACK(ia6)) { 3072 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3073 mtx_unlock(&pr->pr_mtx); 3074 return (0); 3075 } 3076 3077 /* 3078 * Return success because nothing had to be changed. 3079 */ 3080 mtx_unlock(&pr->pr_mtx); 3081 return (0); 3082} 3083 3084/* 3085 * Check if given address belongs to the jail referenced by cred/prison. 3086 * 3087 * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail, 3088 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 3089 * doesn't allow IPv6. 3090 */ 3091static int 3092_prison_check_ip6(struct prison *pr, struct in6_addr *ia6) 3093{ 3094 int i, a, z, d; 3095 3096 /* 3097 * Check the primary IP. 3098 */ 3099 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6)) 3100 return (0); 3101 3102 /* 3103 * All the other IPs are sorted so we can do a binary search. 3104 */ 3105 a = 0; 3106 z = pr->pr_ip6s - 2; 3107 while (a <= z) { 3108 i = (a + z) / 2; 3109 d = qcmp_v6(&pr->pr_ip6[i+1], ia6); 3110 if (d > 0) 3111 z = i - 1; 3112 else if (d < 0) 3113 a = i + 1; 3114 else 3115 return (0); 3116 } 3117 3118 return (EADDRNOTAVAIL); 3119} 3120 3121int 3122prison_check_ip6(struct ucred *cred, struct in6_addr *ia6) 3123{ 3124 struct prison *pr; 3125 int error; 3126 3127 KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3128 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3129 3130 pr = cred->cr_prison; 3131 if (!(pr->pr_flags & PR_IP6)) 3132 return (0); 3133 mtx_lock(&pr->pr_mtx); 3134 if (!(pr->pr_flags & PR_IP6)) { 3135 mtx_unlock(&pr->pr_mtx); 3136 return (0); 3137 } 3138 if (pr->pr_ip6 == NULL) { 3139 mtx_unlock(&pr->pr_mtx); 3140 return (EAFNOSUPPORT); 3141 } 3142 3143 error = _prison_check_ip6(pr, ia6); 3144 mtx_unlock(&pr->pr_mtx); 3145 return (error); 3146} 3147#endif 3148 3149/* 3150 * Check if a jail supports the given address family. 3151 * 3152 * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT 3153 * if not. 3154 */ 3155int 3156prison_check_af(struct ucred *cred, int af) 3157{ 3158 struct prison *pr; 3159 int error; 3160 3161 KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3162 3163 pr = cred->cr_prison; 3164#ifdef VIMAGE 3165 /* Prisons with their own network stack are not limited. */ 3166 if (prison_owns_vnet(cred)) 3167 return (0); 3168#endif 3169 3170 error = 0; 3171 switch (af) 3172 { 3173#ifdef INET 3174 case AF_INET: 3175 if (pr->pr_flags & PR_IP4) 3176 { 3177 mtx_lock(&pr->pr_mtx); 3178 if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL) 3179 error = EAFNOSUPPORT; 3180 mtx_unlock(&pr->pr_mtx); 3181 } 3182 break; 3183#endif 3184#ifdef INET6 3185 case AF_INET6: 3186 if (pr->pr_flags & PR_IP6) 3187 { 3188 mtx_lock(&pr->pr_mtx); 3189 if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL) 3190 error = EAFNOSUPPORT; 3191 mtx_unlock(&pr->pr_mtx); 3192 } 3193 break; 3194#endif 3195 case AF_LOCAL: 3196 case AF_ROUTE: 3197 break; 3198 default: 3199 if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF)) 3200 error = EAFNOSUPPORT; 3201 } 3202 return (error); 3203} 3204 3205/* 3206 * Check if given address belongs to the jail referenced by cred (wrapper to 3207 * prison_check_ip[46]). 3208 * 3209 * Returns 0 if jail doesn't restrict the address family or if address belongs 3210 * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if 3211 * the jail doesn't allow the address family. IPv4 Address passed in in NBO. 3212 */ 3213int 3214prison_if(struct ucred *cred, struct sockaddr *sa) 3215{ 3216#ifdef INET 3217 struct sockaddr_in *sai; 3218#endif 3219#ifdef INET6 3220 struct sockaddr_in6 *sai6; 3221#endif 3222 int error; 3223 3224 KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3225 KASSERT(sa != NULL, ("%s: sa is NULL", __func__)); 3226 3227#ifdef VIMAGE 3228 if (prison_owns_vnet(cred)) 3229 return (0); 3230#endif 3231 3232 error = 0; 3233 switch (sa->sa_family) 3234 { 3235#ifdef INET 3236 case AF_INET: 3237 sai = (struct sockaddr_in *)sa; 3238 error = prison_check_ip4(cred, &sai->sin_addr); 3239 break; 3240#endif 3241#ifdef INET6 3242 case AF_INET6: 3243 sai6 = (struct sockaddr_in6 *)sa; 3244 error = prison_check_ip6(cred, &sai6->sin6_addr); 3245 break; 3246#endif 3247 default: 3248 if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF)) 3249 error = EAFNOSUPPORT; 3250 } 3251 return (error); 3252} 3253 3254/* 3255 * Return 0 if jails permit p1 to frob p2, otherwise ESRCH. 3256 */ 3257int 3258prison_check(struct ucred *cred1, struct ucred *cred2) 3259{ 3260 3261 return ((cred1->cr_prison == cred2->cr_prison || 3262 prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH); 3263} 3264 3265/* 3266 * Return 1 if p2 is a child of p1, otherwise 0. 3267 */ 3268int 3269prison_ischild(struct prison *pr1, struct prison *pr2) 3270{ 3271 3272 for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent) 3273 if (pr1 == pr2) 3274 return (1); 3275 return (0); 3276} 3277 3278/* 3279 * Return 1 if the passed credential is in a jail, otherwise 0. 3280 */ 3281int 3282jailed(struct ucred *cred) 3283{ 3284 3285 return (cred->cr_prison != &prison0); 3286} 3287 3288/* 3289 * Return 1 if the passed credential is in a jail and that jail does not 3290 * have its own virtual network stack, otherwise 0. 3291 */ 3292int 3293jailed_without_vnet(struct ucred *cred) 3294{ 3295 3296 if (!jailed(cred)) 3297 return (0); 3298#ifdef VIMAGE 3299 if (prison_owns_vnet(cred)) 3300 return (0); 3301#endif 3302 3303 return (1); 3304} 3305 3306/* 3307 * Return the correct hostname (domainname, et al) for the passed credential. 3308 */ 3309void 3310getcredhostname(struct ucred *cred, char *buf, size_t size) 3311{ 3312 struct prison *pr; 3313 3314 /* 3315 * A NULL credential can be used to shortcut to the physical 3316 * system's hostname. 3317 */ 3318 pr = (cred != NULL) ? cred->cr_prison : &prison0; 3319 mtx_lock(&pr->pr_mtx); 3320 strlcpy(buf, pr->pr_hostname, size); 3321 mtx_unlock(&pr->pr_mtx); 3322} 3323 3324void 3325getcreddomainname(struct ucred *cred, char *buf, size_t size) 3326{ 3327 3328 mtx_lock(&cred->cr_prison->pr_mtx); 3329 strlcpy(buf, cred->cr_prison->pr_domainname, size); 3330 mtx_unlock(&cred->cr_prison->pr_mtx); 3331} 3332 3333void 3334getcredhostuuid(struct ucred *cred, char *buf, size_t size) 3335{ 3336 3337 mtx_lock(&cred->cr_prison->pr_mtx); 3338 strlcpy(buf, cred->cr_prison->pr_hostuuid, size); 3339 mtx_unlock(&cred->cr_prison->pr_mtx); 3340} 3341 3342void 3343getcredhostid(struct ucred *cred, unsigned long *hostid) 3344{ 3345 3346 mtx_lock(&cred->cr_prison->pr_mtx); 3347 *hostid = cred->cr_prison->pr_hostid; 3348 mtx_unlock(&cred->cr_prison->pr_mtx); 3349} 3350 3351#ifdef VIMAGE 3352/* 3353 * Determine whether the prison represented by cred owns 3354 * its vnet rather than having it inherited. 3355 * 3356 * Returns 1 in case the prison owns the vnet, 0 otherwise. 3357 */ 3358int 3359prison_owns_vnet(struct ucred *cred) 3360{ 3361 3362 /* 3363 * vnets cannot be added/removed after jail creation, 3364 * so no need to lock here. 3365 */ 3366 return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0); 3367} 3368#endif 3369 3370/* 3371 * Determine whether the subject represented by cred can "see" 3372 * status of a mount point. 3373 * Returns: 0 for permitted, ENOENT otherwise. 3374 * XXX: This function should be called cr_canseemount() and should be 3375 * placed in kern_prot.c. 3376 */ 3377int 3378prison_canseemount(struct ucred *cred, struct mount *mp) 3379{ 3380 struct prison *pr; 3381 struct statfs *sp; 3382 size_t len; 3383 3384 pr = cred->cr_prison; 3385 if (pr->pr_enforce_statfs == 0) 3386 return (0); 3387 if (pr->pr_root->v_mount == mp) 3388 return (0); 3389 if (pr->pr_enforce_statfs == 2) 3390 return (ENOENT); 3391 /* 3392 * If jail's chroot directory is set to "/" we should be able to see 3393 * all mount-points from inside a jail. 3394 * This is ugly check, but this is the only situation when jail's 3395 * directory ends with '/'. 3396 */ 3397 if (strcmp(pr->pr_path, "/") == 0) 3398 return (0); 3399 len = strlen(pr->pr_path); 3400 sp = &mp->mnt_stat; 3401 if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0) 3402 return (ENOENT); 3403 /* 3404 * Be sure that we don't have situation where jail's root directory 3405 * is "/some/path" and mount point is "/some/pathpath". 3406 */ 3407 if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/') 3408 return (ENOENT); 3409 return (0); 3410} 3411 3412void 3413prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp) 3414{ 3415 char jpath[MAXPATHLEN]; 3416 struct prison *pr; 3417 size_t len; 3418 3419 pr = cred->cr_prison; 3420 if (pr->pr_enforce_statfs == 0) 3421 return; 3422 if (prison_canseemount(cred, mp) != 0) { 3423 bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3424 strlcpy(sp->f_mntonname, "[restricted]", 3425 sizeof(sp->f_mntonname)); 3426 return; 3427 } 3428 if (pr->pr_root->v_mount == mp) { 3429 /* 3430 * Clear current buffer data, so we are sure nothing from 3431 * the valid path left there. 3432 */ 3433 bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3434 *sp->f_mntonname = '/'; 3435 return; 3436 } 3437 /* 3438 * If jail's chroot directory is set to "/" we should be able to see 3439 * all mount-points from inside a jail. 3440 */ 3441 if (strcmp(pr->pr_path, "/") == 0) 3442 return; 3443 len = strlen(pr->pr_path); 3444 strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath)); 3445 /* 3446 * Clear current buffer data, so we are sure nothing from 3447 * the valid path left there. 3448 */ 3449 bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3450 if (*jpath == '\0') { 3451 /* Should never happen. */ 3452 *sp->f_mntonname = '/'; 3453 } else { 3454 strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname)); 3455 } 3456} 3457 3458/* 3459 * Check with permission for a specific privilege is granted within jail. We 3460 * have a specific list of accepted privileges; the rest are denied. 3461 */ 3462int 3463prison_priv_check(struct ucred *cred, int priv) 3464{ 3465 3466 if (!jailed(cred)) 3467 return (0); 3468 3469#ifdef VIMAGE 3470 /* 3471 * Privileges specific to prisons with a virtual network stack. 3472 * There might be a duplicate entry here in case the privilege 3473 * is only granted conditionally in the legacy jail case. 3474 */ 3475 switch (priv) { 3476#ifdef notyet 3477 /* 3478 * NFS-specific privileges. 3479 */ 3480 case PRIV_NFS_DAEMON: 3481 case PRIV_NFS_LOCKD: 3482#endif 3483 /* 3484 * Network stack privileges. 3485 */ 3486 case PRIV_NET_BRIDGE: 3487 case PRIV_NET_GRE: 3488 case PRIV_NET_BPF: 3489 case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */ 3490 case PRIV_NET_ROUTE: 3491 case PRIV_NET_TAP: 3492 case PRIV_NET_SETIFMTU: 3493 case PRIV_NET_SETIFFLAGS: 3494 case PRIV_NET_SETIFCAP: 3495 case PRIV_NET_SETIFNAME : 3496 case PRIV_NET_SETIFMETRIC: 3497 case PRIV_NET_SETIFPHYS: 3498 case PRIV_NET_SETIFMAC: 3499 case PRIV_NET_ADDMULTI: 3500 case PRIV_NET_DELMULTI: 3501 case PRIV_NET_HWIOCTL: 3502 case PRIV_NET_SETLLADDR: 3503 case PRIV_NET_ADDIFGROUP: 3504 case PRIV_NET_DELIFGROUP: 3505 case PRIV_NET_IFCREATE: 3506 case PRIV_NET_IFDESTROY: 3507 case PRIV_NET_ADDIFADDR: 3508 case PRIV_NET_DELIFADDR: 3509 case PRIV_NET_LAGG: 3510 case PRIV_NET_GIF: 3511 case PRIV_NET_SETIFVNET: 3512 3513 /* 3514 * 802.11-related privileges. 3515 */ 3516 case PRIV_NET80211_GETKEY: 3517#ifdef notyet 3518 case PRIV_NET80211_MANAGE: /* XXX-BZ discuss with sam@ */ 3519#endif 3520 3521#ifdef notyet 3522 /* 3523 * AppleTalk privileges. 3524 */ 3525 case PRIV_NETATALK_RESERVEDPORT: 3526 3527 /* 3528 * ATM privileges. 3529 */ 3530 case PRIV_NETATM_CFG: 3531 case PRIV_NETATM_ADD: 3532 case PRIV_NETATM_DEL: 3533 case PRIV_NETATM_SET: 3534 3535 /* 3536 * Bluetooth privileges. 3537 */ 3538 case PRIV_NETBLUETOOTH_RAW: 3539#endif 3540 3541 /* 3542 * Netgraph and netgraph module privileges. 3543 */ 3544 case PRIV_NETGRAPH_CONTROL: 3545#ifdef notyet 3546 case PRIV_NETGRAPH_TTY: 3547#endif 3548 3549 /* 3550 * IPv4 and IPv6 privileges. 3551 */ 3552 case PRIV_NETINET_IPFW: 3553 case PRIV_NETINET_DIVERT: 3554 case PRIV_NETINET_PF: 3555 case PRIV_NETINET_DUMMYNET: 3556 case PRIV_NETINET_CARP: 3557 case PRIV_NETINET_MROUTE: 3558 case PRIV_NETINET_RAW: 3559 case PRIV_NETINET_ADDRCTRL6: 3560 case PRIV_NETINET_ND6: 3561 case PRIV_NETINET_SCOPE6: 3562 case PRIV_NETINET_ALIFETIME6: 3563 case PRIV_NETINET_IPSEC: 3564 case PRIV_NETINET_BINDANY: 3565 3566#ifdef notyet 3567 /* 3568 * IPX/SPX privileges. 3569 */ 3570 case PRIV_NETIPX_RESERVEDPORT: 3571 case PRIV_NETIPX_RAW: 3572 3573 /* 3574 * NCP privileges. 3575 */ 3576 case PRIV_NETNCP: 3577 3578 /* 3579 * SMB privileges. 3580 */ 3581 case PRIV_NETSMB: 3582#endif 3583 3584 /* 3585 * No default: or deny here. 3586 * In case of no permit fall through to next switch(). 3587 */ 3588 if (cred->cr_prison->pr_flags & PR_VNET) 3589 return (0); 3590 } 3591#endif /* VIMAGE */ 3592 3593 switch (priv) { 3594 3595 /* 3596 * Allow ktrace privileges for root in jail. 3597 */ 3598 case PRIV_KTRACE: 3599 3600#if 0 3601 /* 3602 * Allow jailed processes to configure audit identity and 3603 * submit audit records (login, etc). In the future we may 3604 * want to further refine the relationship between audit and 3605 * jail. 3606 */ 3607 case PRIV_AUDIT_GETAUDIT: 3608 case PRIV_AUDIT_SETAUDIT: 3609 case PRIV_AUDIT_SUBMIT: 3610#endif 3611 3612 /* 3613 * Allow jailed processes to manipulate process UNIX 3614 * credentials in any way they see fit. 3615 */ 3616 case PRIV_CRED_SETUID: 3617 case PRIV_CRED_SETEUID: 3618 case PRIV_CRED_SETGID: 3619 case PRIV_CRED_SETEGID: 3620 case PRIV_CRED_SETGROUPS: 3621 case PRIV_CRED_SETREUID: 3622 case PRIV_CRED_SETREGID: 3623 case PRIV_CRED_SETRESUID: 3624 case PRIV_CRED_SETRESGID: 3625 3626 /* 3627 * Jail implements visibility constraints already, so allow 3628 * jailed root to override uid/gid-based constraints. 3629 */ 3630 case PRIV_SEEOTHERGIDS: 3631 case PRIV_SEEOTHERUIDS: 3632 3633 /* 3634 * Jail implements inter-process debugging limits already, so 3635 * allow jailed root various debugging privileges. 3636 */ 3637 case PRIV_DEBUG_DIFFCRED: 3638 case PRIV_DEBUG_SUGID: 3639 case PRIV_DEBUG_UNPRIV: 3640 3641 /* 3642 * Allow jail to set various resource limits and login 3643 * properties, and for now, exceed process resource limits. 3644 */ 3645 case PRIV_PROC_LIMIT: 3646 case PRIV_PROC_SETLOGIN: 3647 case PRIV_PROC_SETRLIMIT: 3648 3649 /* 3650 * System V and POSIX IPC privileges are granted in jail. 3651 */ 3652 case PRIV_IPC_READ: 3653 case PRIV_IPC_WRITE: 3654 case PRIV_IPC_ADMIN: 3655 case PRIV_IPC_MSGSIZE: 3656 case PRIV_MQ_ADMIN: 3657 3658 /* 3659 * Jail operations within a jail work on child jails. 3660 */ 3661 case PRIV_JAIL_ATTACH: 3662 case PRIV_JAIL_SET: 3663 case PRIV_JAIL_REMOVE: 3664 3665 /* 3666 * Jail implements its own inter-process limits, so allow 3667 * root processes in jail to change scheduling on other 3668 * processes in the same jail. Likewise for signalling. 3669 */ 3670 case PRIV_SCHED_DIFFCRED: 3671 case PRIV_SCHED_CPUSET: 3672 case PRIV_SIGNAL_DIFFCRED: 3673 case PRIV_SIGNAL_SUGID: 3674 3675 /* 3676 * Allow jailed processes to write to sysctls marked as jail 3677 * writable. 3678 */ 3679 case PRIV_SYSCTL_WRITEJAIL: 3680 3681 /* 3682 * Allow root in jail to manage a variety of quota 3683 * properties. These should likely be conditional on a 3684 * configuration option. 3685 */ 3686 case PRIV_VFS_GETQUOTA: 3687 case PRIV_VFS_SETQUOTA: 3688 3689 /* 3690 * Since Jail relies on chroot() to implement file system 3691 * protections, grant many VFS privileges to root in jail. 3692 * Be careful to exclude mount-related and NFS-related 3693 * privileges. 3694 */ 3695 case PRIV_VFS_READ: 3696 case PRIV_VFS_WRITE: 3697 case PRIV_VFS_ADMIN: 3698 case PRIV_VFS_EXEC: 3699 case PRIV_VFS_LOOKUP: 3700 case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */ 3701 case PRIV_VFS_CHFLAGS_DEV: 3702 case PRIV_VFS_CHOWN: 3703 case PRIV_VFS_CHROOT: 3704 case PRIV_VFS_RETAINSUGID: 3705 case PRIV_VFS_FCHROOT: 3706 case PRIV_VFS_LINK: 3707 case PRIV_VFS_SETGID: 3708 case PRIV_VFS_STAT: 3709 case PRIV_VFS_STICKYFILE: 3710 return (0); 3711 3712 /* 3713 * Depending on the global setting, allow privilege of 3714 * setting system flags. 3715 */ 3716 case PRIV_VFS_SYSFLAGS: 3717 if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS) 3718 return (0); 3719 else 3720 return (EPERM); 3721 3722 /* 3723 * Depending on the global setting, allow privilege of 3724 * mounting/unmounting file systems. 3725 */ 3726 case PRIV_VFS_MOUNT: 3727 case PRIV_VFS_UNMOUNT: 3728 case PRIV_VFS_MOUNT_NONUSER: 3729 case PRIV_VFS_MOUNT_OWNER: 3730 if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT) 3731 return (0); 3732 else 3733 return (EPERM); 3734 3735 /* 3736 * Allow jailed root to bind reserved ports and reuse in-use 3737 * ports. 3738 */ 3739 case PRIV_NETINET_RESERVEDPORT: 3740 case PRIV_NETINET_REUSEPORT: 3741 return (0); 3742 3743 /* 3744 * Allow jailed root to set certian IPv4/6 (option) headers. 3745 */ 3746 case PRIV_NETINET_SETHDROPTS: 3747 return (0); 3748 3749 /* 3750 * Conditionally allow creating raw sockets in jail. 3751 */ 3752 case PRIV_NETINET_RAW: 3753 if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS) 3754 return (0); 3755 else 3756 return (EPERM); 3757 3758 /* 3759 * Since jail implements its own visibility limits on netstat 3760 * sysctls, allow getcred. This allows identd to work in 3761 * jail. 3762 */ 3763 case PRIV_NETINET_GETCRED: 3764 return (0); 3765 3766 default: 3767 /* 3768 * In all remaining cases, deny the privilege request. This 3769 * includes almost all network privileges, many system 3770 * configuration privileges. 3771 */ 3772 return (EPERM); 3773 } 3774} 3775 3776/* 3777 * Return the part of pr2's name that is relative to pr1, or the whole name 3778 * if it does not directly follow. 3779 */ 3780 3781char * 3782prison_name(struct prison *pr1, struct prison *pr2) 3783{ 3784 char *name; 3785 3786 /* Jails see themselves as "0" (if they see themselves at all). */ 3787 if (pr1 == pr2) 3788 return "0"; 3789 name = pr2->pr_name; 3790 if (prison_ischild(pr1, pr2)) { 3791 /* 3792 * pr1 isn't locked (and allprison_lock may not be either) 3793 * so its length can't be counted on. But the number of dots 3794 * can be counted on - and counted. 3795 */ 3796 for (; pr1 != &prison0; pr1 = pr1->pr_parent) 3797 name = strchr(name, '.') + 1; 3798 } 3799 return (name); 3800} 3801 3802/* 3803 * Return the part of pr2's path that is relative to pr1, or the whole path 3804 * if it does not directly follow. 3805 */ 3806static char * 3807prison_path(struct prison *pr1, struct prison *pr2) 3808{ 3809 char *path1, *path2; 3810 int len1; 3811 3812 path1 = pr1->pr_path; 3813 path2 = pr2->pr_path; 3814 if (!strcmp(path1, "/")) 3815 return (path2); 3816 len1 = strlen(path1); 3817 if (strncmp(path1, path2, len1)) 3818 return (path2); 3819 if (path2[len1] == '\0') 3820 return "/"; 3821 if (path2[len1] == '/') 3822 return (path2 + len1); 3823 return (path2); 3824} 3825 3826 3827/* 3828 * Jail-related sysctls. 3829 */ 3830SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0, 3831 "Jails"); 3832 3833static int 3834sysctl_jail_list(SYSCTL_HANDLER_ARGS) 3835{ 3836 struct xprison *xp; 3837 struct prison *pr, *cpr; 3838#ifdef INET 3839 struct in_addr *ip4 = NULL; 3840 int ip4s = 0; 3841#endif 3842#ifdef INET6 3843 struct in_addr *ip6 = NULL; 3844 int ip6s = 0; 3845#endif 3846 int descend, error; 3847 3848 xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK); 3849 pr = req->td->td_ucred->cr_prison; 3850 error = 0; 3851 sx_slock(&allprison_lock); 3852 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 3853#if defined(INET) || defined(INET6) 3854 again: 3855#endif 3856 mtx_lock(&cpr->pr_mtx); 3857#ifdef INET 3858 if (cpr->pr_ip4s > 0) { 3859 if (ip4s < cpr->pr_ip4s) { 3860 ip4s = cpr->pr_ip4s; 3861 mtx_unlock(&cpr->pr_mtx); 3862 ip4 = realloc(ip4, ip4s * 3863 sizeof(struct in_addr), M_TEMP, M_WAITOK); 3864 goto again; 3865 } 3866 bcopy(cpr->pr_ip4, ip4, 3867 cpr->pr_ip4s * sizeof(struct in_addr)); 3868 } 3869#endif 3870#ifdef INET6 3871 if (cpr->pr_ip6s > 0) { 3872 if (ip6s < cpr->pr_ip6s) { 3873 ip6s = cpr->pr_ip6s; 3874 mtx_unlock(&cpr->pr_mtx); 3875 ip6 = realloc(ip6, ip6s * 3876 sizeof(struct in6_addr), M_TEMP, M_WAITOK); 3877 goto again; 3878 } 3879 bcopy(cpr->pr_ip6, ip6, 3880 cpr->pr_ip6s * sizeof(struct in6_addr)); 3881 } 3882#endif 3883 if (cpr->pr_ref == 0) { 3884 mtx_unlock(&cpr->pr_mtx); 3885 continue; 3886 } 3887 bzero(xp, sizeof(*xp)); 3888 xp->pr_version = XPRISON_VERSION; 3889 xp->pr_id = cpr->pr_id; 3890 xp->pr_state = cpr->pr_uref > 0 3891 ? PRISON_STATE_ALIVE : PRISON_STATE_DYING; 3892 strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path)); 3893 strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host)); 3894 strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name)); 3895#ifdef INET 3896 xp->pr_ip4s = cpr->pr_ip4s; 3897#endif 3898#ifdef INET6 3899 xp->pr_ip6s = cpr->pr_ip6s; 3900#endif 3901 mtx_unlock(&cpr->pr_mtx); 3902 error = SYSCTL_OUT(req, xp, sizeof(*xp)); 3903 if (error) 3904 break; 3905#ifdef INET 3906 if (xp->pr_ip4s > 0) { 3907 error = SYSCTL_OUT(req, ip4, 3908 xp->pr_ip4s * sizeof(struct in_addr)); 3909 if (error) 3910 break; 3911 } 3912#endif 3913#ifdef INET6 3914 if (xp->pr_ip6s > 0) { 3915 error = SYSCTL_OUT(req, ip6, 3916 xp->pr_ip6s * sizeof(struct in6_addr)); 3917 if (error) 3918 break; 3919 } 3920#endif 3921 } 3922 sx_sunlock(&allprison_lock); 3923 free(xp, M_TEMP); 3924#ifdef INET 3925 free(ip4, M_TEMP); 3926#endif 3927#ifdef INET6 3928 free(ip6, M_TEMP); 3929#endif 3930 return (error); 3931} 3932 3933SYSCTL_OID(_security_jail, OID_AUTO, list, 3934 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 3935 sysctl_jail_list, "S", "List of active jails"); 3936 3937static int 3938sysctl_jail_jailed(SYSCTL_HANDLER_ARGS) 3939{ 3940 int error, injail; 3941 3942 injail = jailed(req->td->td_ucred); 3943 error = SYSCTL_OUT(req, &injail, sizeof(injail)); 3944 3945 return (error); 3946} 3947 3948SYSCTL_PROC(_security_jail, OID_AUTO, jailed, 3949 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 3950 sysctl_jail_jailed, "I", "Process in jail?"); 3951 3952#if defined(INET) || defined(INET6) 3953SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW, 3954 &jail_max_af_ips, 0, 3955 "Number of IP addresses a jail may have at most per address family"); 3956#endif 3957 3958/* 3959 * Default parameters for jail(2) compatability. For historical reasons, 3960 * the sysctl names have varying similarity to the parameter names. Prisons 3961 * just see their own parameters, and can't change them. 3962 */ 3963static int 3964sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS) 3965{ 3966 struct prison *pr; 3967 int allow, error, i; 3968 3969 pr = req->td->td_ucred->cr_prison; 3970 allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow; 3971 3972 /* Get the current flag value, and convert it to a boolean. */ 3973 i = (allow & arg2) ? 1 : 0; 3974 if (arg1 != NULL) 3975 i = !i; 3976 error = sysctl_handle_int(oidp, &i, 0, req); 3977 if (error || !req->newptr) 3978 return (error); 3979 i = i ? arg2 : 0; 3980 if (arg1 != NULL) 3981 i ^= arg2; 3982 /* 3983 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0 3984 * for writing. 3985 */ 3986 mtx_lock(&prison0.pr_mtx); 3987 jail_default_allow = (jail_default_allow & ~arg2) | i; 3988 mtx_unlock(&prison0.pr_mtx); 3989 return (0); 3990} 3991 3992SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed, 3993 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 3994 NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I", 3995 "Processes in jail can set their hostnames"); 3996SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only, 3997 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 3998 (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I", 3999 "Processes in jail are limited to creating UNIX/IP/route sockets only"); 4000SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed, 4001 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4002 NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I", 4003 "Processes in jail can use System V IPC primitives"); 4004SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets, 4005 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4006 NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I", 4007 "Prison root can create raw sockets"); 4008SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed, 4009 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4010 NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I", 4011 "Processes in jail can alter system file flags"); 4012SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed, 4013 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4014 NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I", 4015 "Processes in jail can mount/unmount jail-friendly file systems"); 4016 4017static int 4018sysctl_jail_default_level(SYSCTL_HANDLER_ARGS) 4019{ 4020 struct prison *pr; 4021 int level, error; 4022 4023 pr = req->td->td_ucred->cr_prison; 4024 level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2); 4025 error = sysctl_handle_int(oidp, &level, 0, req); 4026 if (error || !req->newptr) 4027 return (error); 4028 *(int *)arg1 = level; 4029 return (0); 4030} 4031 4032SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs, 4033 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4034 &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs), 4035 sysctl_jail_default_level, "I", 4036 "Processes in jail cannot see all mounted file systems"); 4037 4038/* 4039 * Nodes to describe jail parameters. Maximum length of string parameters 4040 * is returned in the string itself, and the other parameters exist merely 4041 * to make themselves and their types known. 4042 */ 4043SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0, 4044 "Jail parameters"); 4045 4046int 4047sysctl_jail_param(SYSCTL_HANDLER_ARGS) 4048{ 4049 int i; 4050 long l; 4051 size_t s; 4052 char numbuf[12]; 4053 4054 switch (oidp->oid_kind & CTLTYPE) 4055 { 4056 case CTLTYPE_LONG: 4057 case CTLTYPE_ULONG: 4058 l = 0; 4059#ifdef SCTL_MASK32 4060 if (!(req->flags & SCTL_MASK32)) 4061#endif 4062 return (SYSCTL_OUT(req, &l, sizeof(l))); 4063 case CTLTYPE_INT: 4064 case CTLTYPE_UINT: 4065 i = 0; 4066 return (SYSCTL_OUT(req, &i, sizeof(i))); 4067 case CTLTYPE_STRING: 4068 snprintf(numbuf, sizeof(numbuf), "%d", arg2); 4069 return 4070 (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req)); 4071 case CTLTYPE_STRUCT: 4072 s = (size_t)arg2; 4073 return (SYSCTL_OUT(req, &s, sizeof(s))); 4074 } 4075 return (0); 4076} 4077 4078SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID"); 4079SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID"); 4080SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name"); 4081SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path"); 4082SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW, 4083 "I", "Jail secure level"); 4084SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW, 4085 "I", "Jail cannot see all mounted file systems"); 4086SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW, 4087 "B", "Jail persistence"); 4088#ifdef VIMAGE 4089SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN, 4090 "E,jailsys", "Virtual network stack"); 4091#endif 4092SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD, 4093 "B", "Jail is in the process of shutting down"); 4094 4095SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails"); 4096SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD, 4097 "I", "Current number of child jails"); 4098SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW, 4099 "I", "Maximum number of child jails"); 4100 4101SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info"); 4102SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN, 4103 "Jail hostname"); 4104SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN, 4105 "Jail NIS domainname"); 4106SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN, 4107 "Jail host UUID"); 4108SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW, 4109 "LU", "Jail host ID"); 4110 4111SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset"); 4112SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID"); 4113 4114#ifdef INET 4115SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN, 4116 "Jail IPv4 address virtualization"); 4117SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr), 4118 "S,in_addr,a", "Jail IPv4 addresses"); 4119#endif 4120#ifdef INET6 4121SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN, 4122 "Jail IPv6 address virtualization"); 4123SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr), 4124 "S,in6_addr,a", "Jail IPv6 addresses"); 4125#endif 4126 4127SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags"); 4128SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW, 4129 "B", "Jail may set hostname"); 4130SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW, 4131 "B", "Jail may use SYSV IPC"); 4132SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW, 4133 "B", "Jail may create raw sockets"); 4134SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW, 4135 "B", "Jail may alter system file flags"); 4136SYSCTL_JAIL_PARAM(_allow, mount, CTLTYPE_INT | CTLFLAG_RW, 4137 "B", "Jail may mount/unmount jail-friendly file systems"); 4138SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW, 4139 "B", "Jail may set file quotas"); 4140SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW, 4141 "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route"); 4142 4143 4144#ifdef DDB 4145 4146static void 4147db_show_prison(struct prison *pr) 4148{ 4149 int fi; 4150#if defined(INET) || defined(INET6) 4151 int ii; 4152#endif 4153 unsigned jsf; 4154#ifdef INET6 4155 char ip6buf[INET6_ADDRSTRLEN]; 4156#endif 4157 4158 db_printf("prison %p:\n", pr); 4159 db_printf(" jid = %d\n", pr->pr_id); 4160 db_printf(" name = %s\n", pr->pr_name); 4161 db_printf(" parent = %p\n", pr->pr_parent); 4162 db_printf(" ref = %d\n", pr->pr_ref); 4163 db_printf(" uref = %d\n", pr->pr_uref); 4164 db_printf(" path = %s\n", pr->pr_path); 4165 db_printf(" cpuset = %d\n", pr->pr_cpuset 4166 ? pr->pr_cpuset->cs_id : -1); 4167#ifdef VIMAGE 4168 db_printf(" vnet = %p\n", pr->pr_vnet); 4169#endif 4170 db_printf(" root = %p\n", pr->pr_root); 4171 db_printf(" securelevel = %d\n", pr->pr_securelevel); 4172 db_printf(" children.max = %d\n", pr->pr_childmax); 4173 db_printf(" children.cur = %d\n", pr->pr_childcount); 4174 db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children)); 4175 db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling)); 4176 db_printf(" flags = 0x%x", pr->pr_flags); 4177 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 4178 fi++) 4179 if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi))) 4180 db_printf(" %s", pr_flag_names[fi]); 4181 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 4182 fi++) { 4183 jsf = pr->pr_flags & 4184 (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new); 4185 db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name, 4186 pr_flag_jailsys[fi].disable && 4187 (jsf == pr_flag_jailsys[fi].disable) ? "disable" 4188 : (jsf == pr_flag_jailsys[fi].new) ? "new" 4189 : "inherit"); 4190 } 4191 db_printf(" allow = 0x%x", pr->pr_allow); 4192 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 4193 fi++) 4194 if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi))) 4195 db_printf(" %s", pr_allow_names[fi]); 4196 db_printf("\n"); 4197 db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs); 4198 db_printf(" host.hostname = %s\n", pr->pr_hostname); 4199 db_printf(" host.domainname = %s\n", pr->pr_domainname); 4200 db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid); 4201 db_printf(" host.hostid = %lu\n", pr->pr_hostid); 4202#ifdef INET 4203 db_printf(" ip4s = %d\n", pr->pr_ip4s); 4204 for (ii = 0; ii < pr->pr_ip4s; ii++) 4205 db_printf(" %s %s\n", 4206 ii == 0 ? "ip4.addr =" : " ", 4207 inet_ntoa(pr->pr_ip4[ii])); 4208#endif 4209#ifdef INET6 4210 db_printf(" ip6s = %d\n", pr->pr_ip6s); 4211 for (ii = 0; ii < pr->pr_ip6s; ii++) 4212 db_printf(" %s %s\n", 4213 ii == 0 ? "ip6.addr =" : " ", 4214 ip6_sprintf(ip6buf, &pr->pr_ip6[ii])); 4215#endif 4216} 4217 4218DB_SHOW_COMMAND(prison, db_show_prison_command) 4219{ 4220 struct prison *pr; 4221 4222 if (!have_addr) { 4223 /* 4224 * Show all prisons in the list, and prison0 which is not 4225 * listed. 4226 */ 4227 db_show_prison(&prison0); 4228 if (!db_pager_quit) { 4229 TAILQ_FOREACH(pr, &allprison, pr_list) { 4230 db_show_prison(pr); 4231 if (db_pager_quit) 4232 break; 4233 } 4234 } 4235 return; 4236 } 4237 4238 if (addr == 0) 4239 pr = &prison0; 4240 else { 4241 /* Look for a prison with the ID and with references. */ 4242 TAILQ_FOREACH(pr, &allprison, pr_list) 4243 if (pr->pr_id == addr && pr->pr_ref > 0) 4244 break; 4245 if (pr == NULL) 4246 /* Look again, without requiring a reference. */ 4247 TAILQ_FOREACH(pr, &allprison, pr_list) 4248 if (pr->pr_id == addr) 4249 break; 4250 if (pr == NULL) 4251 /* Assume address points to a valid prison. */ 4252 pr = (struct prison *)addr; 4253 } 4254 db_show_prison(pr); 4255} 4256 4257#endif /* DDB */ 4258