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