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