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