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