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