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