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