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