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