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