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