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