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