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