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