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