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