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