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