1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * This module contains functions used to bring up and tear down the 31 * Virtual Platform: [un]mounting file-systems, [un]plumbing network 32 * interfaces, [un]configuring devices, establishing resource controls, 33 * and creating/destroying the zone in the kernel. These actions, on 34 * the way up, ready the zone; on the way down, they halt the zone. 35 * See the much longer block comment at the beginning of zoneadmd.c 36 * for a bigger picture of how the whole program functions. 37 * 38 * This module also has primary responsibility for the layout of "scratch 39 * zones." These are mounted, but inactive, zones that are used during 40 * operating system upgrade and potentially other administrative action. The 41 * scratch zone environment is similar to the miniroot environment. The zone's 42 * actual root is mounted read-write on /a, and the standard paths (/usr, 43 * /sbin, /lib) all lead to read-only copies of the running system's binaries. 44 * This allows the administrative tools to manipulate the zone using "-R /a" 45 * without relying on any binaries in the zone itself. 46 * 47 * If the scratch zone is on an alternate root (Live Upgrade [LU] boot 48 * environment), then we must resolve the lofs mounts used there to uncover 49 * writable (unshared) resources. Shared resources, though, are always 50 * read-only. In addition, if the "same" zone with a different root path is 51 * currently running, then "/b" inside the zone points to the running zone's 52 * root. This allows LU to synchronize configuration files during the upgrade 53 * process. 54 * 55 * To construct this environment, this module creates a tmpfs mount on 56 * $ZONEPATH/lu. Inside this scratch area, the miniroot-like environment as 57 * described above is constructed on the fly. The zone is then created using 58 * $ZONEPATH/lu as the root. 59 * 60 * Note that scratch zones are inactive. The zone's bits are not running and 61 * likely cannot be run correctly until upgrade is done. Init is not running 62 * there, nor is SMF. Because of this, the "mounted" state of a scratch zone 63 * is not a part of the usual halt/ready/boot state machine. 64 */ 65 66 #include <sys/param.h> 67 #include <sys/mount.h> 68 #include <sys/mntent.h> 69 #include <sys/socket.h> 70 #include <sys/utsname.h> 71 #include <sys/types.h> 72 #include <sys/stat.h> 73 #include <sys/sockio.h> 74 #include <sys/stropts.h> 75 #include <sys/conf.h> 76 77 #include <inet/tcp.h> 78 #include <arpa/inet.h> 79 #include <netinet/in.h> 80 #include <net/route.h> 81 #include <netdb.h> 82 83 #include <stdio.h> 84 #include <errno.h> 85 #include <fcntl.h> 86 #include <unistd.h> 87 #include <rctl.h> 88 #include <stdlib.h> 89 #include <string.h> 90 #include <strings.h> 91 #include <wait.h> 92 #include <limits.h> 93 #include <libgen.h> 94 #include <libzfs.h> 95 #include <zone.h> 96 #include <assert.h> 97 98 #include <sys/mntio.h> 99 #include <sys/mnttab.h> 100 #include <sys/fs/autofs.h> /* for _autofssys() */ 101 #include <sys/fs/lofs_info.h> 102 #include <sys/fs/zfs.h> 103 104 #include <pool.h> 105 #include <sys/pool.h> 106 107 #include <libzonecfg.h> 108 #include "zoneadmd.h" 109 #include <tsol/label.h> 110 #include <libtsnet.h> 111 #include <sys/priv.h> 112 113 #define V4_ADDR_LEN 32 114 #define V6_ADDR_LEN 128 115 116 /* 0755 is the default directory mode. */ 117 #define DEFAULT_DIR_MODE \ 118 (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) 119 120 #define IPD_DEFAULT_OPTS \ 121 MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES 122 123 #define DFSTYPES "/etc/dfs/fstypes" 124 #define MAXTNZLEN 2048 125 126 /* 127 * A list of directories which should be created. 128 */ 129 130 struct dir_info { 131 char *dir_name; 132 mode_t dir_mode; 133 }; 134 135 /* 136 * The pathnames below are relative to the zonepath 137 */ 138 static struct dir_info dev_dirs[] = { 139 { "/dev", 0755 }, 140 { "/dev/dsk", 0755 }, 141 { "/dev/fd", 0555 }, 142 { "/dev/pts", 0755 }, 143 { "/dev/rdsk", 0755 }, 144 { "/dev/rmt", 0755 }, 145 { "/dev/sad", 0755 }, 146 { "/dev/swap", 0755 }, 147 { "/dev/term", 0755 }, 148 }; 149 150 /* 151 * A list of devices which should be symlinked to /dev/zconsole. 152 */ 153 154 struct symlink_info { 155 char *sl_source; 156 char *sl_target; 157 }; 158 159 /* 160 * The "source" paths are relative to the zonepath 161 */ 162 static struct symlink_info dev_symlinks[] = { 163 { "/dev/stderr", "./fd/2" }, 164 { "/dev/stdin", "./fd/0" }, 165 { "/dev/stdout", "./fd/1" }, 166 { "/dev/dtremote", "/dev/null" }, 167 { "/dev/console", "zconsole" }, 168 { "/dev/syscon", "zconsole" }, 169 { "/dev/sysmsg", "zconsole" }, 170 { "/dev/systty", "zconsole" }, 171 { "/dev/msglog", "zconsole" }, 172 }; 173 174 /* for routing socket */ 175 static int rts_seqno = 0; 176 177 /* mangled zone name when mounting in an alternate root environment */ 178 static char kernzone[ZONENAME_MAX]; 179 180 /* array of cached mount entries for resolve_lofs */ 181 static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max; 182 183 /* for Trusted Extensions */ 184 static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *); 185 static int tsol_mounts(zlog_t *, char *, char *); 186 static void tsol_unmounts(zlog_t *, char *); 187 static m_label_t *zlabel = NULL; 188 static m_label_t *zid_label = NULL; 189 static priv_set_t *zprivs = NULL; 190 191 /* from libsocket, not in any header file */ 192 extern int getnetmaskbyaddr(struct in_addr, struct in_addr *); 193 194 /* 195 * An optimization for build_mnttable: reallocate (and potentially copy the 196 * data) only once every N times through the loop. 197 */ 198 #define MNTTAB_HUNK 32 199 200 /* 201 * Private autofs system call 202 */ 203 extern int _autofssys(int, void *); 204 205 static int 206 autofs_cleanup(zoneid_t zoneid) 207 { 208 /* 209 * Ask autofs to unmount all trigger nodes in the given zone. 210 */ 211 return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid)); 212 } 213 214 static void 215 free_mnttable(struct mnttab *mnt_array, uint_t nelem) 216 { 217 uint_t i; 218 219 if (mnt_array == NULL) 220 return; 221 for (i = 0; i < nelem; i++) { 222 free(mnt_array[i].mnt_mountp); 223 free(mnt_array[i].mnt_fstype); 224 free(mnt_array[i].mnt_special); 225 free(mnt_array[i].mnt_mntopts); 226 assert(mnt_array[i].mnt_time == NULL); 227 } 228 free(mnt_array); 229 } 230 231 /* 232 * Build the mount table for the zone rooted at "zroot", storing the resulting 233 * array of struct mnttabs in "mnt_arrayp" and the number of elements in the 234 * array in "nelemp". 235 */ 236 static int 237 build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab, 238 struct mnttab **mnt_arrayp, uint_t *nelemp) 239 { 240 struct mnttab mnt; 241 struct mnttab *mnts; 242 struct mnttab *mnp; 243 uint_t nmnt; 244 245 rewind(mnttab); 246 resetmnttab(mnttab); 247 nmnt = 0; 248 mnts = NULL; 249 while (getmntent(mnttab, &mnt) == 0) { 250 struct mnttab *tmp_array; 251 252 if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0) 253 continue; 254 if (nmnt % MNTTAB_HUNK == 0) { 255 tmp_array = realloc(mnts, 256 (nmnt + MNTTAB_HUNK) * sizeof (*mnts)); 257 if (tmp_array == NULL) { 258 free_mnttable(mnts, nmnt); 259 return (-1); 260 } 261 mnts = tmp_array; 262 } 263 mnp = &mnts[nmnt++]; 264 265 /* 266 * Zero out any fields we're not using. 267 */ 268 (void) memset(mnp, 0, sizeof (*mnp)); 269 270 if (mnt.mnt_special != NULL) 271 mnp->mnt_special = strdup(mnt.mnt_special); 272 if (mnt.mnt_mntopts != NULL) 273 mnp->mnt_mntopts = strdup(mnt.mnt_mntopts); 274 mnp->mnt_mountp = strdup(mnt.mnt_mountp); 275 mnp->mnt_fstype = strdup(mnt.mnt_fstype); 276 if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) || 277 (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) || 278 mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) { 279 zerror(zlogp, B_TRUE, "memory allocation failed"); 280 free_mnttable(mnts, nmnt); 281 return (-1); 282 } 283 } 284 *mnt_arrayp = mnts; 285 *nelemp = nmnt; 286 return (0); 287 } 288 289 /* 290 * This is an optimization. The resolve_lofs function is used quite frequently 291 * to manipulate file paths, and on a machine with a large number of zones, 292 * there will be a huge number of mounted file systems. Thus, we trigger a 293 * reread of the list of mount points 294 */ 295 static void 296 lofs_discard_mnttab(void) 297 { 298 free_mnttable(resolve_lofs_mnts, 299 resolve_lofs_mnt_max - resolve_lofs_mnts); 300 resolve_lofs_mnts = resolve_lofs_mnt_max = NULL; 301 } 302 303 static int 304 lofs_read_mnttab(zlog_t *zlogp) 305 { 306 FILE *mnttab; 307 uint_t nmnts; 308 309 if ((mnttab = fopen(MNTTAB, "r")) == NULL) 310 return (-1); 311 if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts, 312 &nmnts) == -1) { 313 (void) fclose(mnttab); 314 return (-1); 315 } 316 (void) fclose(mnttab); 317 resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts; 318 return (0); 319 } 320 321 /* 322 * This function loops over potential loopback mounts and symlinks in a given 323 * path and resolves them all down to an absolute path. 324 */ 325 static void 326 resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen) 327 { 328 int len, arlen; 329 const char *altroot; 330 char tmppath[MAXPATHLEN]; 331 boolean_t outside_altroot; 332 333 if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1) 334 return; 335 tmppath[len] = '\0'; 336 (void) strlcpy(path, tmppath, sizeof (tmppath)); 337 338 /* This happens once per zoneadmd operation. */ 339 if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1) 340 return; 341 342 altroot = zonecfg_get_root(); 343 arlen = strlen(altroot); 344 outside_altroot = B_FALSE; 345 for (;;) { 346 struct mnttab *mnp; 347 348 for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; 349 mnp++) { 350 if (mnp->mnt_fstype == NULL || 351 mnp->mnt_mountp == NULL || 352 mnp->mnt_special == NULL || 353 strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0) 354 continue; 355 len = strlen(mnp->mnt_mountp); 356 if (strncmp(mnp->mnt_mountp, path, len) == 0 && 357 (path[len] == '/' || path[len] == '\0')) 358 break; 359 } 360 if (mnp >= resolve_lofs_mnt_max) 361 break; 362 if (outside_altroot) { 363 char *cp; 364 int olen = sizeof (MNTOPT_RO) - 1; 365 366 /* 367 * If we run into a read-only mount outside of the 368 * alternate root environment, then the user doesn't 369 * want this path to be made read-write. 370 */ 371 if (mnp->mnt_mntopts != NULL && 372 (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) != 373 NULL && 374 (cp == mnp->mnt_mntopts || cp[-1] == ',') && 375 (cp[olen] == '\0' || cp[olen] == ',')) { 376 break; 377 } 378 } else if (arlen > 0 && 379 (strncmp(mnp->mnt_special, altroot, arlen) != 0 || 380 (mnp->mnt_special[arlen] != '\0' && 381 mnp->mnt_special[arlen] != '/'))) { 382 outside_altroot = B_TRUE; 383 } 384 /* use temporary buffer because new path might be longer */ 385 (void) snprintf(tmppath, sizeof (tmppath), "%s%s", 386 mnp->mnt_special, path + len); 387 if ((len = resolvepath(tmppath, path, pathlen)) == -1) 388 break; 389 path[len] = '\0'; 390 } 391 } 392 393 /* 394 * For a regular mount, check if a replacement lofs mount is needed because the 395 * referenced device is already mounted somewhere. 396 */ 397 static int 398 check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr) 399 { 400 struct mnttab *mnp; 401 zone_fsopt_t *optptr, *onext; 402 403 /* This happens once per zoneadmd operation. */ 404 if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1) 405 return (-1); 406 407 /* 408 * If this special node isn't already in use, then it's ours alone; 409 * no need to worry about conflicting mounts. 410 */ 411 for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; 412 mnp++) { 413 if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0) 414 break; 415 } 416 if (mnp >= resolve_lofs_mnt_max) 417 return (0); 418 419 /* 420 * Convert this duplicate mount into a lofs mount. 421 */ 422 (void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp, 423 sizeof (fsptr->zone_fs_special)); 424 (void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS, 425 sizeof (fsptr->zone_fs_type)); 426 fsptr->zone_fs_raw[0] = '\0'; 427 428 /* 429 * Discard all but one of the original options and set that to be the 430 * same set of options used for inherit package directory resources. 431 */ 432 optptr = fsptr->zone_fs_options; 433 if (optptr == NULL) { 434 optptr = malloc(sizeof (*optptr)); 435 if (optptr == NULL) { 436 zerror(zlogp, B_TRUE, "cannot mount %s", 437 fsptr->zone_fs_dir); 438 return (-1); 439 } 440 } else { 441 while ((onext = optptr->zone_fsopt_next) != NULL) { 442 optptr->zone_fsopt_next = onext->zone_fsopt_next; 443 free(onext); 444 } 445 } 446 (void) strcpy(optptr->zone_fsopt_opt, IPD_DEFAULT_OPTS); 447 optptr->zone_fsopt_next = NULL; 448 fsptr->zone_fs_options = optptr; 449 return (0); 450 } 451 452 static int 453 make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode) 454 { 455 char path[MAXPATHLEN]; 456 struct stat st; 457 458 if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) > 459 sizeof (path)) { 460 zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix, 461 subdir); 462 return (-1); 463 } 464 465 if (lstat(path, &st) == 0) { 466 /* 467 * We don't check the file mode since presumably the zone 468 * administrator may have had good reason to change the mode, 469 * and we don't need to second guess him. 470 */ 471 if (!S_ISDIR(st.st_mode)) { 472 if (is_system_labeled() && 473 S_ISREG(st.st_mode)) { 474 /* 475 * The need to mount readonly copies of 476 * global zone /etc/ files is unique to 477 * Trusted Extensions. 478 */ 479 if (strncmp(subdir, "/etc/", 480 strlen("/etc/")) != 0) { 481 zerror(zlogp, B_FALSE, 482 "%s is not in /etc", path); 483 return (-1); 484 } 485 } else { 486 zerror(zlogp, B_FALSE, 487 "%s is not a directory", path); 488 return (-1); 489 } 490 } 491 } else if (mkdirp(path, mode) != 0) { 492 if (errno == EROFS) 493 zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on " 494 "a read-only file system in this local zone.\nMake " 495 "sure %s exists in the global zone.", path, subdir); 496 else 497 zerror(zlogp, B_TRUE, "mkdirp of %s failed", path); 498 return (-1); 499 } 500 return (0); 501 } 502 503 /* 504 * Make /dev and various directories underneath it. 505 */ 506 static int 507 make_dev_dirs(zlog_t *zlogp, const char *zonepath) 508 { 509 int i; 510 511 for (i = 0; i < sizeof (dev_dirs) / sizeof (struct dir_info); i++) { 512 if (make_one_dir(zlogp, zonepath, dev_dirs[i].dir_name, 513 dev_dirs[i].dir_mode) != 0) 514 return (-1); 515 } 516 return (0); 517 } 518 519 /* 520 * Make various sym-links underneath /dev. 521 */ 522 static int 523 make_dev_links(zlog_t *zlogp, char *zonepath) 524 { 525 int i; 526 527 for (i = 0; i < sizeof (dev_symlinks) / sizeof (struct symlink_info); 528 i++) { 529 char dev[MAXPATHLEN]; 530 struct stat st; 531 532 (void) snprintf(dev, sizeof (dev), "%s%s", zonepath, 533 dev_symlinks[i].sl_source); 534 if (lstat(dev, &st) == 0) { 535 /* 536 * Try not to call unlink(2) on directories, since that 537 * makes UFS unhappy. 538 */ 539 if (S_ISDIR(st.st_mode)) { 540 zerror(zlogp, B_FALSE, "symlink path %s is a " 541 "directory", dev_symlinks[i].sl_source); 542 return (-1); 543 } 544 (void) unlink(dev); 545 } 546 if (symlink(dev_symlinks[i].sl_target, dev) != 0) { 547 zerror(zlogp, B_TRUE, "could not setup %s->%s symlink", 548 dev_symlinks[i].sl_source, 549 dev_symlinks[i].sl_target); 550 return (-1); 551 } 552 } 553 return (0); 554 } 555 556 /* 557 * Create various directories and sym-links under /dev. 558 */ 559 static int 560 create_dev_files(zlog_t *zlogp) 561 { 562 char zonepath[MAXPATHLEN]; 563 564 if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) { 565 zerror(zlogp, B_TRUE, "unable to determine zone root"); 566 return (-1); 567 } 568 if (zonecfg_in_alt_root()) 569 resolve_lofs(zlogp, zonepath, sizeof (zonepath)); 570 571 if (make_dev_dirs(zlogp, zonepath) != 0) 572 return (-1); 573 if (make_dev_links(zlogp, zonepath) != 0) 574 return (-1); 575 return (0); 576 } 577 578 static void 579 free_remote_fstypes(char **types) 580 { 581 uint_t i; 582 583 if (types == NULL) 584 return; 585 for (i = 0; types[i] != NULL; i++) 586 free(types[i]); 587 free(types); 588 } 589 590 static char ** 591 get_remote_fstypes(zlog_t *zlogp) 592 { 593 char **types = NULL; 594 FILE *fp; 595 char buf[MAXPATHLEN]; 596 char fstype[MAXPATHLEN]; 597 uint_t lines = 0; 598 uint_t i; 599 600 if ((fp = fopen(DFSTYPES, "r")) == NULL) { 601 zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES); 602 return (NULL); 603 } 604 /* 605 * Count the number of lines 606 */ 607 while (fgets(buf, sizeof (buf), fp) != NULL) 608 lines++; 609 if (lines == 0) /* didn't read anything; empty file */ 610 goto out; 611 rewind(fp); 612 /* 613 * Allocate enough space for a NULL-terminated array. 614 */ 615 types = calloc(lines + 1, sizeof (char *)); 616 if (types == NULL) { 617 zerror(zlogp, B_TRUE, "memory allocation failed"); 618 goto out; 619 } 620 i = 0; 621 while (fgets(buf, sizeof (buf), fp) != NULL) { 622 /* LINTED - fstype is big enough to hold buf */ 623 if (sscanf(buf, "%s", fstype) == 0) { 624 zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES); 625 free_remote_fstypes(types); 626 types = NULL; 627 goto out; 628 } 629 types[i] = strdup(fstype); 630 if (types[i] == NULL) { 631 zerror(zlogp, B_TRUE, "memory allocation failed"); 632 free_remote_fstypes(types); 633 types = NULL; 634 goto out; 635 } 636 i++; 637 } 638 out: 639 (void) fclose(fp); 640 return (types); 641 } 642 643 static boolean_t 644 is_remote_fstype(const char *fstype, char *const *remote_fstypes) 645 { 646 uint_t i; 647 648 if (remote_fstypes == NULL) 649 return (B_FALSE); 650 for (i = 0; remote_fstypes[i] != NULL; i++) { 651 if (strcmp(remote_fstypes[i], fstype) == 0) 652 return (B_TRUE); 653 } 654 return (B_FALSE); 655 } 656 657 /* 658 * This converts a zone root path (normally of the form .../root) to a Live 659 * Upgrade scratch zone root (of the form .../lu). 660 */ 661 static void 662 root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved) 663 { 664 if (!isresolved && zonecfg_in_alt_root()) 665 resolve_lofs(zlogp, zroot, zrootlen); 666 (void) strcpy(strrchr(zroot, '/') + 1, "lu"); 667 } 668 669 /* 670 * The general strategy for unmounting filesystems is as follows: 671 * 672 * - Remote filesystems may be dead, and attempting to contact them as 673 * part of a regular unmount may hang forever; we want to always try to 674 * forcibly unmount such filesystems and only fall back to regular 675 * unmounts if the filesystem doesn't support forced unmounts. 676 * 677 * - We don't want to unnecessarily corrupt metadata on local 678 * filesystems (ie UFS), so we want to start off with graceful unmounts, 679 * and only escalate to doing forced unmounts if we get stuck. 680 * 681 * We start off walking backwards through the mount table. This doesn't 682 * give us strict ordering but ensures that we try to unmount submounts 683 * first. We thus limit the number of failed umount2(2) calls. 684 * 685 * The mechanism for determining if we're stuck is to count the number 686 * of failed unmounts each iteration through the mount table. This 687 * gives us an upper bound on the number of filesystems which remain 688 * mounted (autofs trigger nodes are dealt with separately). If at the 689 * end of one unmount+autofs_cleanup cycle we still have the same number 690 * of mounts that we started out with, we're stuck and try a forced 691 * unmount. If that fails (filesystem doesn't support forced unmounts) 692 * then we bail and are unable to teardown the zone. If it succeeds, 693 * we're no longer stuck so we continue with our policy of trying 694 * graceful mounts first. 695 * 696 * Zone must be down (ie, no processes or threads active). 697 */ 698 static int 699 unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd) 700 { 701 int error = 0; 702 FILE *mnttab; 703 struct mnttab *mnts; 704 uint_t nmnt; 705 char zroot[MAXPATHLEN + 1]; 706 size_t zrootlen; 707 uint_t oldcount = UINT_MAX; 708 boolean_t stuck = B_FALSE; 709 char **remote_fstypes = NULL; 710 711 if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) { 712 zerror(zlogp, B_FALSE, "unable to determine zone root"); 713 return (-1); 714 } 715 if (unmount_cmd) 716 root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE); 717 718 (void) strcat(zroot, "/"); 719 zrootlen = strlen(zroot); 720 721 /* 722 * For Trusted Extensions unmount each higher level zone's mount 723 * of our zone's /export/home 724 */ 725 tsol_unmounts(zlogp, zone_name); 726 727 if ((mnttab = fopen(MNTTAB, "r")) == NULL) { 728 zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB); 729 return (-1); 730 } 731 /* 732 * Use our hacky mntfs ioctl so we see everything, even mounts with 733 * MS_NOMNTTAB. 734 */ 735 if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) { 736 zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB); 737 error++; 738 goto out; 739 } 740 741 /* 742 * Build the list of remote fstypes so we know which ones we 743 * should forcibly unmount. 744 */ 745 remote_fstypes = get_remote_fstypes(zlogp); 746 for (; /* ever */; ) { 747 uint_t newcount = 0; 748 boolean_t unmounted; 749 struct mnttab *mnp; 750 char *path; 751 uint_t i; 752 753 mnts = NULL; 754 nmnt = 0; 755 /* 756 * MNTTAB gives us a way to walk through mounted 757 * filesystems; we need to be able to walk them in 758 * reverse order, so we build a list of all mounted 759 * filesystems. 760 */ 761 if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts, 762 &nmnt) != 0) { 763 error++; 764 goto out; 765 } 766 for (i = 0; i < nmnt; i++) { 767 mnp = &mnts[nmnt - i - 1]; /* access in reverse order */ 768 path = mnp->mnt_mountp; 769 unmounted = B_FALSE; 770 /* 771 * Try forced unmount first for remote filesystems. 772 * 773 * Not all remote filesystems support forced unmounts, 774 * so if this fails (ENOTSUP) we'll continue on 775 * and try a regular unmount. 776 */ 777 if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) { 778 if (umount2(path, MS_FORCE) == 0) 779 unmounted = B_TRUE; 780 } 781 /* 782 * Try forced unmount if we're stuck. 783 */ 784 if (stuck) { 785 if (umount2(path, MS_FORCE) == 0) { 786 unmounted = B_TRUE; 787 stuck = B_FALSE; 788 } else { 789 /* 790 * The first failure indicates a 791 * mount we won't be able to get 792 * rid of automatically, so we 793 * bail. 794 */ 795 error++; 796 zerror(zlogp, B_FALSE, 797 "unable to unmount '%s'", path); 798 free_mnttable(mnts, nmnt); 799 goto out; 800 } 801 } 802 /* 803 * Try regular unmounts for everything else. 804 */ 805 if (!unmounted && umount2(path, 0) != 0) 806 newcount++; 807 } 808 free_mnttable(mnts, nmnt); 809 810 if (newcount == 0) 811 break; 812 if (newcount >= oldcount) { 813 /* 814 * Last round didn't unmount anything; we're stuck and 815 * should start trying forced unmounts. 816 */ 817 stuck = B_TRUE; 818 } 819 oldcount = newcount; 820 821 /* 822 * Autofs doesn't let you unmount its trigger nodes from 823 * userland so we have to tell the kernel to cleanup for us. 824 */ 825 if (autofs_cleanup(zoneid) != 0) { 826 zerror(zlogp, B_TRUE, "unable to remove autofs nodes"); 827 error++; 828 goto out; 829 } 830 } 831 832 out: 833 free_remote_fstypes(remote_fstypes); 834 (void) fclose(mnttab); 835 return (error ? -1 : 0); 836 } 837 838 static int 839 fs_compare(const void *m1, const void *m2) 840 { 841 struct zone_fstab *i = (struct zone_fstab *)m1; 842 struct zone_fstab *j = (struct zone_fstab *)m2; 843 844 return (strcmp(i->zone_fs_dir, j->zone_fs_dir)); 845 } 846 847 /* 848 * Fork and exec (and wait for) the mentioned binary with the provided 849 * arguments. Returns (-1) if something went wrong with fork(2) or exec(2), 850 * returns the exit status otherwise. 851 * 852 * If we were unable to exec the provided pathname (for whatever 853 * reason), we return the special token ZEXIT_EXEC. The current value 854 * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the 855 * consumers of this function; any future consumers must make sure this 856 * remains the case. 857 */ 858 static int 859 forkexec(zlog_t *zlogp, const char *path, char *const argv[]) 860 { 861 pid_t child_pid; 862 int child_status = 0; 863 864 /* 865 * Do not let another thread localize a message while we are forking. 866 */ 867 (void) mutex_lock(&msglock); 868 child_pid = fork(); 869 (void) mutex_unlock(&msglock); 870 if (child_pid == -1) { 871 zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]); 872 return (-1); 873 } else if (child_pid == 0) { 874 closefrom(0); 875 (void) execv(path, argv); 876 /* 877 * Since we are in the child, there is no point calling zerror() 878 * since there is nobody waiting to consume it. So exit with a 879 * special code that the parent will recognize and call zerror() 880 * accordingly. 881 */ 882 883 _exit(ZEXIT_EXEC); 884 } else { 885 (void) waitpid(child_pid, &child_status, 0); 886 } 887 888 if (WIFSIGNALED(child_status)) { 889 zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to " 890 "signal %d", path, WTERMSIG(child_status)); 891 return (-1); 892 } 893 assert(WIFEXITED(child_status)); 894 if (WEXITSTATUS(child_status) == ZEXIT_EXEC) { 895 zerror(zlogp, B_FALSE, "failed to exec %s", path); 896 return (-1); 897 } 898 return (WEXITSTATUS(child_status)); 899 } 900 901 static int 902 dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev) 903 { 904 char cmdbuf[MAXPATHLEN]; 905 char *argv[4]; 906 int status; 907 908 /* 909 * We could alternatively have called /usr/sbin/fsck -F <fstype>, but 910 * that would cost us an extra fork/exec without buying us anything. 911 */ 912 if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype) 913 > sizeof (cmdbuf)) { 914 zerror(zlogp, B_FALSE, "file-system type %s too long", fstype); 915 return (-1); 916 } 917 918 argv[0] = "fsck"; 919 argv[1] = "-m"; 920 argv[2] = (char *)rawdev; 921 argv[3] = NULL; 922 923 status = forkexec(zlogp, cmdbuf, argv); 924 if (status == 0 || status == -1) 925 return (status); 926 zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; " 927 "run fsck manually", rawdev, status); 928 return (-1); 929 } 930 931 static int 932 domount(zlog_t *zlogp, const char *fstype, const char *opts, 933 const char *special, const char *directory) 934 { 935 char cmdbuf[MAXPATHLEN]; 936 char *argv[6]; 937 int status; 938 939 /* 940 * We could alternatively have called /usr/sbin/mount -F <fstype>, but 941 * that would cost us an extra fork/exec without buying us anything. 942 */ 943 if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype) 944 > sizeof (cmdbuf)) { 945 zerror(zlogp, B_FALSE, "file-system type %s too long", fstype); 946 return (-1); 947 } 948 argv[0] = "mount"; 949 if (opts[0] == '\0') { 950 argv[1] = (char *)special; 951 argv[2] = (char *)directory; 952 argv[3] = NULL; 953 } else { 954 argv[1] = "-o"; 955 argv[2] = (char *)opts; 956 argv[3] = (char *)special; 957 argv[4] = (char *)directory; 958 argv[5] = NULL; 959 } 960 961 status = forkexec(zlogp, cmdbuf, argv); 962 if (status == 0 || status == -1) 963 return (status); 964 if (opts[0] == '\0') 965 zerror(zlogp, B_FALSE, "\"%s %s %s\" " 966 "failed with exit code %d", 967 cmdbuf, special, directory, status); 968 else 969 zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" " 970 "failed with exit code %d", 971 cmdbuf, opts, special, directory, status); 972 return (-1); 973 } 974 975 /* 976 * Make sure if a given path exists, it is not a sym-link, and is a directory. 977 */ 978 static int 979 check_path(zlog_t *zlogp, const char *path) 980 { 981 struct stat statbuf; 982 char respath[MAXPATHLEN]; 983 int res; 984 985 if (lstat(path, &statbuf) != 0) { 986 if (errno == ENOENT) 987 return (0); 988 zerror(zlogp, B_TRUE, "can't stat %s", path); 989 return (-1); 990 } 991 if (S_ISLNK(statbuf.st_mode)) { 992 zerror(zlogp, B_FALSE, "%s is a symlink", path); 993 return (-1); 994 } 995 if (!S_ISDIR(statbuf.st_mode)) { 996 if (is_system_labeled() && S_ISREG(statbuf.st_mode)) { 997 /* 998 * The need to mount readonly copies of 999 * global zone /etc/ files is unique to 1000 * Trusted Extensions. 1001 * The check for /etc/ via strstr() is to 1002 * allow paths like $ZONEROOT/etc/passwd 1003 */ 1004 if (strstr(path, "/etc/") == NULL) { 1005 zerror(zlogp, B_FALSE, 1006 "%s is not in /etc", path); 1007 return (-1); 1008 } 1009 } else { 1010 zerror(zlogp, B_FALSE, "%s is not a directory", path); 1011 return (-1); 1012 } 1013 } 1014 if ((res = resolvepath(path, respath, sizeof (respath))) == -1) { 1015 zerror(zlogp, B_TRUE, "unable to resolve path %s", path); 1016 return (-1); 1017 } 1018 respath[res] = '\0'; 1019 if (strcmp(path, respath) != 0) { 1020 /* 1021 * We don't like ".."s and "."s throwing us off 1022 */ 1023 zerror(zlogp, B_FALSE, "%s is not a canonical path", path); 1024 return (-1); 1025 } 1026 return (0); 1027 } 1028 1029 /* 1030 * Check every component of rootpath/relpath. If any component fails (ie, 1031 * exists but isn't the canonical path to a directory), it is returned in 1032 * badpath, which is assumed to be at least of size MAXPATHLEN. 1033 * 1034 * Relpath must begin with '/'. 1035 */ 1036 static boolean_t 1037 valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *relpath) 1038 { 1039 char abspath[MAXPATHLEN], *slashp; 1040 1041 /* 1042 * Make sure abspath has at least one '/' after its rootpath 1043 * component, and ends with '/'. 1044 */ 1045 if (snprintf(abspath, sizeof (abspath), "%s%s/", rootpath, relpath) > 1046 sizeof (abspath)) { 1047 zerror(zlogp, B_FALSE, "pathname %s%s is too long", rootpath, 1048 relpath); 1049 return (B_FALSE); 1050 } 1051 1052 slashp = &abspath[strlen(rootpath)]; 1053 assert(*slashp == '/'); 1054 do { 1055 *slashp = '\0'; 1056 if (check_path(zlogp, abspath) != 0) 1057 return (B_FALSE); 1058 *slashp = '/'; 1059 slashp++; 1060 } while ((slashp = strchr(slashp, '/')) != NULL); 1061 return (B_TRUE); 1062 } 1063 1064 static int 1065 mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath) 1066 { 1067 char path[MAXPATHLEN]; 1068 char specpath[MAXPATHLEN]; 1069 char optstr[MAX_MNTOPT_STR]; 1070 zone_fsopt_t *optptr; 1071 1072 if (!valid_mount_path(zlogp, rootpath, fsptr->zone_fs_dir)) { 1073 zerror(zlogp, B_FALSE, "%s%s is not a valid mount point", 1074 rootpath, fsptr->zone_fs_dir); 1075 return (-1); 1076 } 1077 1078 if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir, 1079 DEFAULT_DIR_MODE) != 0) 1080 return (-1); 1081 1082 (void) snprintf(path, sizeof (path), "%s%s", rootpath, 1083 fsptr->zone_fs_dir); 1084 1085 if (strlen(fsptr->zone_fs_special) == 0) { 1086 /* 1087 * A zero-length special is how we distinguish IPDs from 1088 * general-purpose FSs. Make sure it mounts from a place that 1089 * can be seen via the alternate zone's root. 1090 */ 1091 if (snprintf(specpath, sizeof (specpath), "%s%s", 1092 zonecfg_get_root(), fsptr->zone_fs_dir) >= 1093 sizeof (specpath)) { 1094 zerror(zlogp, B_FALSE, "cannot mount %s: path too " 1095 "long in alternate root", fsptr->zone_fs_dir); 1096 return (-1); 1097 } 1098 if (zonecfg_in_alt_root()) 1099 resolve_lofs(zlogp, specpath, sizeof (specpath)); 1100 if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, 1101 specpath, path) != 0) { 1102 zerror(zlogp, B_TRUE, "failed to loopback mount %s", 1103 specpath); 1104 return (-1); 1105 } 1106 return (0); 1107 } 1108 1109 /* 1110 * In general the strategy here is to do just as much verification as 1111 * necessary to avoid crashing or otherwise doing something bad; if the 1112 * administrator initiated the operation via zoneadm(1m), he'll get 1113 * auto-verification which will let him know what's wrong. If he 1114 * modifies the zone configuration of a running zone and doesn't attempt 1115 * to verify that it's OK we won't crash but won't bother trying to be 1116 * too helpful either. zoneadm verify is only a couple keystrokes away. 1117 */ 1118 if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) { 1119 zerror(zlogp, B_FALSE, "cannot mount %s on %s: " 1120 "invalid file-system type %s", fsptr->zone_fs_special, 1121 fsptr->zone_fs_dir, fsptr->zone_fs_type); 1122 return (-1); 1123 } 1124 1125 /* 1126 * If we're looking at an alternate root environment, then construct 1127 * read-only loopback mounts as necessary. For all lofs mounts, make 1128 * sure that the 'special' entry points inside the alternate root. (We 1129 * don't do this with other mounts, as devfs isn't in the alternate 1130 * root, and we need to assume the device environment is roughly the 1131 * same.) 1132 */ 1133 if (zonecfg_in_alt_root()) { 1134 struct stat64 st; 1135 1136 if (stat64(fsptr->zone_fs_special, &st) != -1 && 1137 S_ISBLK(st.st_mode) && 1138 check_lofs_needed(zlogp, fsptr) == -1) 1139 return (-1); 1140 if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) { 1141 if (snprintf(specpath, sizeof (specpath), "%s%s", 1142 zonecfg_get_root(), fsptr->zone_fs_special) >= 1143 sizeof (specpath)) { 1144 zerror(zlogp, B_FALSE, "cannot mount %s: path " 1145 "too long in alternate root", 1146 fsptr->zone_fs_special); 1147 return (-1); 1148 } 1149 resolve_lofs(zlogp, specpath, sizeof (specpath)); 1150 (void) strlcpy(fsptr->zone_fs_special, specpath, 1151 sizeof (fsptr->zone_fs_special)); 1152 } 1153 } 1154 1155 /* 1156 * Run 'fsck -m' if there's a device to fsck. 1157 */ 1158 if (fsptr->zone_fs_raw[0] != '\0' && 1159 dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0) 1160 return (-1); 1161 1162 /* 1163 * Build up mount option string. 1164 */ 1165 optstr[0] = '\0'; 1166 if (fsptr->zone_fs_options != NULL) { 1167 (void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt, 1168 sizeof (optstr)); 1169 for (optptr = fsptr->zone_fs_options->zone_fsopt_next; 1170 optptr != NULL; optptr = optptr->zone_fsopt_next) { 1171 (void) strlcat(optstr, ",", sizeof (optstr)); 1172 (void) strlcat(optstr, optptr->zone_fsopt_opt, 1173 sizeof (optstr)); 1174 } 1175 } 1176 return (domount(zlogp, fsptr->zone_fs_type, optstr, 1177 fsptr->zone_fs_special, path)); 1178 } 1179 1180 static void 1181 free_fs_data(struct zone_fstab *fsarray, uint_t nelem) 1182 { 1183 uint_t i; 1184 1185 if (fsarray == NULL) 1186 return; 1187 for (i = 0; i < nelem; i++) 1188 zonecfg_free_fs_option_list(fsarray[i].zone_fs_options); 1189 free(fsarray); 1190 } 1191 1192 /* 1193 * This function constructs the miniroot-like "scratch zone" environment. If 1194 * it returns B_FALSE, then the error has already been logged. 1195 */ 1196 static boolean_t 1197 build_mounted(zlog_t *zlogp, char *rootpath, size_t rootlen, 1198 const char *zonepath) 1199 { 1200 char tmp[MAXPATHLEN], fromdir[MAXPATHLEN]; 1201 char luroot[MAXPATHLEN]; 1202 const char **cpp; 1203 static const char *mkdirs[] = { 1204 "/system", "/system/contract", "/proc", "/dev", "/tmp", 1205 "/a", NULL 1206 }; 1207 static const char *localdirs[] = { 1208 "/etc", "/var", NULL 1209 }; 1210 static const char *loopdirs[] = { 1211 "/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform", 1212 "/usr", NULL 1213 }; 1214 static const char *tmpdirs[] = { 1215 "/tmp", "/var/run", NULL 1216 }; 1217 FILE *fp; 1218 struct stat st; 1219 char *altstr; 1220 uuid_t uuid; 1221 1222 /* 1223 * Construct a small Solaris environment, including the zone root 1224 * mounted on '/a' inside that environment. 1225 */ 1226 resolve_lofs(zlogp, rootpath, rootlen); 1227 (void) snprintf(luroot, sizeof (luroot), "%s/lu", zonepath); 1228 resolve_lofs(zlogp, luroot, sizeof (luroot)); 1229 (void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot); 1230 (void) symlink("./usr/bin", tmp); 1231 1232 /* 1233 * These are mostly special mount points; not handled here. (See 1234 * zone_mount_early.) 1235 */ 1236 for (cpp = mkdirs; *cpp != NULL; cpp++) { 1237 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp); 1238 if (mkdir(tmp, 0755) != 0) { 1239 zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1240 return (B_FALSE); 1241 } 1242 } 1243 1244 /* 1245 * These are mounted read-write from the zone undergoing upgrade. We 1246 * must be careful not to 'leak' things from the main system into the 1247 * zone, and this accomplishes that goal. 1248 */ 1249 for (cpp = localdirs; *cpp != NULL; cpp++) { 1250 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp); 1251 (void) snprintf(fromdir, sizeof (fromdir), "%s%s", rootpath, 1252 *cpp); 1253 if (mkdir(tmp, 0755) != 0) { 1254 zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1255 return (B_FALSE); 1256 } 1257 if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp) != 0) { 1258 zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp, 1259 *cpp); 1260 return (B_FALSE); 1261 } 1262 } 1263 1264 /* 1265 * These are things mounted read-only from the running system because 1266 * they contain binaries that must match system. 1267 */ 1268 for (cpp = loopdirs; *cpp != NULL; cpp++) { 1269 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp); 1270 if (mkdir(tmp, 0755) != 0) { 1271 if (errno != EEXIST) { 1272 zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1273 return (B_FALSE); 1274 } 1275 if (lstat(tmp, &st) != 0) { 1276 zerror(zlogp, B_TRUE, "cannot stat %s", tmp); 1277 return (B_FALSE); 1278 } 1279 /* 1280 * Ignore any non-directories encountered. These are 1281 * things that have been converted into symlinks 1282 * (/etc/fs and /etc/lib) and no longer need a lofs 1283 * fixup. 1284 */ 1285 if (!S_ISDIR(st.st_mode)) 1286 continue; 1287 } 1288 if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, *cpp, 1289 tmp) != 0) { 1290 zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp, 1291 *cpp); 1292 return (B_FALSE); 1293 } 1294 } 1295 1296 /* 1297 * These are things with tmpfs mounted inside. 1298 */ 1299 for (cpp = tmpdirs; *cpp != NULL; cpp++) { 1300 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp); 1301 if (mkdir(tmp, 0755) != 0 && errno != EEXIST) { 1302 zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1303 return (B_FALSE); 1304 } 1305 if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) { 1306 zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp); 1307 return (B_FALSE); 1308 } 1309 } 1310 1311 /* 1312 * This is here to support lucopy. If there's an instance of this same 1313 * zone on the current running system, then we mount its root up as 1314 * read-only inside the scratch zone. 1315 */ 1316 (void) zonecfg_get_uuid(zone_name, uuid); 1317 altstr = strdup(zonecfg_get_root()); 1318 if (altstr == NULL) { 1319 zerror(zlogp, B_TRUE, "out of memory"); 1320 return (B_FALSE); 1321 } 1322 zonecfg_set_root(""); 1323 (void) strlcpy(tmp, zone_name, sizeof (tmp)); 1324 (void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp)); 1325 if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK && 1326 strcmp(fromdir, rootpath) != 0) { 1327 (void) snprintf(tmp, sizeof (tmp), "%s/b", luroot); 1328 if (mkdir(tmp, 0755) != 0) { 1329 zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1330 return (B_FALSE); 1331 } 1332 if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, fromdir, 1333 tmp) != 0) { 1334 zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp, 1335 fromdir); 1336 return (B_FALSE); 1337 } 1338 } 1339 zonecfg_set_root(altstr); 1340 free(altstr); 1341 1342 if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) { 1343 zerror(zlogp, B_TRUE, "cannot open zone mapfile"); 1344 return (B_FALSE); 1345 } 1346 (void) ftruncate(fileno(fp), 0); 1347 if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) { 1348 zerror(zlogp, B_TRUE, "cannot add zone mapfile entry"); 1349 } 1350 zonecfg_close_scratch(fp); 1351 (void) snprintf(tmp, sizeof (tmp), "%s/a", luroot); 1352 if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0) 1353 return (B_FALSE); 1354 (void) strlcpy(rootpath, tmp, rootlen); 1355 return (B_TRUE); 1356 } 1357 1358 static int 1359 mount_filesystems(zlog_t *zlogp, boolean_t mount_cmd) 1360 { 1361 char rootpath[MAXPATHLEN]; 1362 char zonepath[MAXPATHLEN]; 1363 int num_fs = 0, i; 1364 struct zone_fstab fstab, *fs_ptr = NULL, *tmp_ptr; 1365 struct zone_fstab *fsp; 1366 zone_dochandle_t handle = NULL; 1367 zone_state_t zstate; 1368 1369 if (zone_get_state(zone_name, &zstate) != Z_OK || 1370 (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) { 1371 zerror(zlogp, B_FALSE, 1372 "zone must be in '%s' or '%s' state to mount file-systems", 1373 zone_state_str(ZONE_STATE_READY), 1374 zone_state_str(ZONE_STATE_MOUNTED)); 1375 goto bad; 1376 } 1377 1378 if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) { 1379 zerror(zlogp, B_TRUE, "unable to determine zone path"); 1380 goto bad; 1381 } 1382 1383 if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) { 1384 zerror(zlogp, B_TRUE, "unable to determine zone root"); 1385 goto bad; 1386 } 1387 1388 if ((handle = zonecfg_init_handle()) == NULL) { 1389 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 1390 goto bad; 1391 } 1392 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK || 1393 zonecfg_setfsent(handle) != Z_OK) { 1394 zerror(zlogp, B_FALSE, "invalid configuration"); 1395 goto bad; 1396 } 1397 1398 /* 1399 * /dev in the zone is loopback'd from the external /dev repository, 1400 * in order to provide a largely read-only semantic. But because 1401 * processes in the zone need to be able to chown, chmod, etc. zone 1402 * /dev files, we can't use a 'ro' lofs mount. Instead we use a 1403 * special mode just for zones, "zonedevfs". 1404 * 1405 * In the future we should front /dev with a full-fledged filesystem. 1406 */ 1407 num_fs++; 1408 if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) { 1409 zerror(zlogp, B_TRUE, "memory allocation failed"); 1410 num_fs--; 1411 goto bad; 1412 } 1413 fs_ptr = tmp_ptr; 1414 fsp = &fs_ptr[num_fs - 1]; 1415 /* 1416 * Note that mount_one will prepend the alternate root to 1417 * zone_fs_special and do the necessary resolution, so all that is 1418 * needed here is to strip the root added by zone_get_zonepath. 1419 */ 1420 (void) strlcpy(fsp->zone_fs_dir, "/dev", sizeof (fsp->zone_fs_dir)); 1421 (void) snprintf(fsp->zone_fs_special, sizeof (fsp->zone_fs_special), 1422 "%s/dev", zonepath + strlen(zonecfg_get_root())); 1423 fsp->zone_fs_raw[0] = '\0'; 1424 (void) strlcpy(fsp->zone_fs_type, MNTTYPE_LOFS, 1425 sizeof (fsp->zone_fs_type)); 1426 fsp->zone_fs_options = NULL; 1427 if (zonecfg_add_fs_option(fsp, MNTOPT_LOFS_ZONEDEVFS) != Z_OK) { 1428 zerror(zlogp, B_FALSE, "error adding property"); 1429 goto bad; 1430 } 1431 1432 /* 1433 * Iterate through the rest of the filesystems, first the IPDs, then 1434 * the general FSs. Sort them all, then mount them in sorted order. 1435 * This is to make sure the higher level directories (e.g., /usr) 1436 * get mounted before any beneath them (e.g., /usr/local). 1437 */ 1438 if (zonecfg_setipdent(handle) != Z_OK) { 1439 zerror(zlogp, B_FALSE, "invalid configuration"); 1440 goto bad; 1441 } 1442 while (zonecfg_getipdent(handle, &fstab) == Z_OK) { 1443 num_fs++; 1444 if ((tmp_ptr = realloc(fs_ptr, 1445 num_fs * sizeof (*tmp_ptr))) == NULL) { 1446 zerror(zlogp, B_TRUE, "memory allocation failed"); 1447 num_fs--; 1448 (void) zonecfg_endipdent(handle); 1449 goto bad; 1450 } 1451 fs_ptr = tmp_ptr; 1452 fsp = &fs_ptr[num_fs - 1]; 1453 /* 1454 * IPDs logically only have a mount point; all other properties 1455 * are implied. 1456 */ 1457 (void) strlcpy(fsp->zone_fs_dir, 1458 fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir)); 1459 fsp->zone_fs_special[0] = '\0'; 1460 fsp->zone_fs_raw[0] = '\0'; 1461 fsp->zone_fs_type[0] = '\0'; 1462 fsp->zone_fs_options = NULL; 1463 } 1464 (void) zonecfg_endipdent(handle); 1465 1466 if (zonecfg_setfsent(handle) != Z_OK) { 1467 zerror(zlogp, B_FALSE, "invalid configuration"); 1468 goto bad; 1469 } 1470 while (zonecfg_getfsent(handle, &fstab) == Z_OK) { 1471 /* 1472 * ZFS filesystems will not be accessible under an alternate 1473 * root, since the pool will not be known. Ignore them in this 1474 * case. 1475 */ 1476 if (mount_cmd && strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0) 1477 continue; 1478 1479 num_fs++; 1480 if ((tmp_ptr = realloc(fs_ptr, 1481 num_fs * sizeof (*tmp_ptr))) == NULL) { 1482 zerror(zlogp, B_TRUE, "memory allocation failed"); 1483 num_fs--; 1484 (void) zonecfg_endfsent(handle); 1485 goto bad; 1486 } 1487 fs_ptr = tmp_ptr; 1488 fsp = &fs_ptr[num_fs - 1]; 1489 (void) strlcpy(fsp->zone_fs_dir, 1490 fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir)); 1491 (void) strlcpy(fsp->zone_fs_special, fstab.zone_fs_special, 1492 sizeof (fsp->zone_fs_special)); 1493 (void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw, 1494 sizeof (fsp->zone_fs_raw)); 1495 (void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type, 1496 sizeof (fsp->zone_fs_type)); 1497 fsp->zone_fs_options = fstab.zone_fs_options; 1498 } 1499 (void) zonecfg_endfsent(handle); 1500 zonecfg_fini_handle(handle); 1501 handle = NULL; 1502 1503 /* 1504 * If we're mounting a zone for administration, then we need to set up 1505 * the "/a" environment inside the zone so that the commands that run 1506 * in there have access to both the running system's utilities and the 1507 * to-be-modified zone's files. 1508 */ 1509 if (mount_cmd && 1510 !build_mounted(zlogp, rootpath, sizeof (rootpath), zonepath)) 1511 goto bad; 1512 1513 qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare); 1514 for (i = 0; i < num_fs; i++) { 1515 if (mount_cmd && strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) { 1516 size_t slen = strlen(rootpath) - 2; 1517 1518 /* /dev is special and always goes at the top */ 1519 rootpath[slen] = '\0'; 1520 if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0) 1521 goto bad; 1522 rootpath[slen] = '/'; 1523 continue; 1524 } 1525 if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0) 1526 goto bad; 1527 } 1528 1529 /* 1530 * For Trusted Extensions cross-mount each lower level /export/home 1531 */ 1532 if (tsol_mounts(zlogp, zone_name, rootpath) != 0) 1533 goto bad; 1534 1535 free_fs_data(fs_ptr, num_fs); 1536 1537 /* 1538 * Everything looks fine. 1539 */ 1540 return (0); 1541 1542 bad: 1543 if (handle != NULL) 1544 zonecfg_fini_handle(handle); 1545 free_fs_data(fs_ptr, num_fs); 1546 return (-1); 1547 } 1548 1549 /* caller makes sure neither parameter is NULL */ 1550 static int 1551 addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr) 1552 { 1553 int prefixlen; 1554 1555 prefixlen = atoi(prefixstr); 1556 if (prefixlen < 0 || prefixlen > maxprefixlen) 1557 return (1); 1558 while (prefixlen > 0) { 1559 if (prefixlen >= 8) { 1560 *maskstr++ = 0xFF; 1561 prefixlen -= 8; 1562 continue; 1563 } 1564 *maskstr |= 1 << (8 - prefixlen); 1565 prefixlen--; 1566 } 1567 return (0); 1568 } 1569 1570 /* 1571 * Tear down all interfaces belonging to the given zone. This should 1572 * be called with the zone in a state other than "running", so that 1573 * interfaces can't be assigned to the zone after this returns. 1574 * 1575 * If anything goes wrong, log an error message and return an error. 1576 */ 1577 static int 1578 unconfigure_network_interfaces(zlog_t *zlogp, zoneid_t zone_id) 1579 { 1580 struct lifnum lifn; 1581 struct lifconf lifc; 1582 struct lifreq *lifrp, lifrl; 1583 int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES; 1584 int num_ifs, s, i, ret_code = 0; 1585 uint_t bufsize; 1586 char *buf = NULL; 1587 1588 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 1589 zerror(zlogp, B_TRUE, "could not get socket"); 1590 ret_code = -1; 1591 goto bad; 1592 } 1593 lifn.lifn_family = AF_UNSPEC; 1594 lifn.lifn_flags = (int)lifc_flags; 1595 if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) { 1596 zerror(zlogp, B_TRUE, 1597 "could not determine number of interfaces"); 1598 ret_code = -1; 1599 goto bad; 1600 } 1601 num_ifs = lifn.lifn_count; 1602 bufsize = num_ifs * sizeof (struct lifreq); 1603 if ((buf = malloc(bufsize)) == NULL) { 1604 zerror(zlogp, B_TRUE, "memory allocation failed"); 1605 ret_code = -1; 1606 goto bad; 1607 } 1608 lifc.lifc_family = AF_UNSPEC; 1609 lifc.lifc_flags = (int)lifc_flags; 1610 lifc.lifc_len = bufsize; 1611 lifc.lifc_buf = buf; 1612 if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) { 1613 zerror(zlogp, B_TRUE, "could not get configured interfaces"); 1614 ret_code = -1; 1615 goto bad; 1616 } 1617 lifrp = lifc.lifc_req; 1618 for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) { 1619 (void) close(s); 1620 if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) < 1621 0) { 1622 zerror(zlogp, B_TRUE, "%s: could not get socket", 1623 lifrl.lifr_name); 1624 ret_code = -1; 1625 continue; 1626 } 1627 (void) memset(&lifrl, 0, sizeof (lifrl)); 1628 (void) strncpy(lifrl.lifr_name, lifrp->lifr_name, 1629 sizeof (lifrl.lifr_name)); 1630 if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) { 1631 zerror(zlogp, B_TRUE, 1632 "%s: could not determine zone interface belongs to", 1633 lifrl.lifr_name); 1634 ret_code = -1; 1635 continue; 1636 } 1637 if (lifrl.lifr_zoneid == zone_id) { 1638 if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) { 1639 zerror(zlogp, B_TRUE, 1640 "%s: could not remove interface", 1641 lifrl.lifr_name); 1642 ret_code = -1; 1643 continue; 1644 } 1645 } 1646 } 1647 bad: 1648 if (s > 0) 1649 (void) close(s); 1650 if (buf) 1651 free(buf); 1652 return (ret_code); 1653 } 1654 1655 static union sockunion { 1656 struct sockaddr sa; 1657 struct sockaddr_in sin; 1658 struct sockaddr_dl sdl; 1659 struct sockaddr_in6 sin6; 1660 } so_dst, so_ifp; 1661 1662 static struct { 1663 struct rt_msghdr hdr; 1664 char space[512]; 1665 } rtmsg; 1666 1667 static int 1668 salen(struct sockaddr *sa) 1669 { 1670 switch (sa->sa_family) { 1671 case AF_INET: 1672 return (sizeof (struct sockaddr_in)); 1673 case AF_LINK: 1674 return (sizeof (struct sockaddr_dl)); 1675 case AF_INET6: 1676 return (sizeof (struct sockaddr_in6)); 1677 default: 1678 return (sizeof (struct sockaddr)); 1679 } 1680 } 1681 1682 #define ROUNDUP_LONG(a) \ 1683 ((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long)) 1684 1685 /* 1686 * Look up which zone is using a given IP address. The address in question 1687 * is expected to have been stuffed into the structure to which lifr points 1688 * via a previous SIOCGLIFADDR ioctl(). 1689 * 1690 * This is done using black router socket magic. 1691 * 1692 * Return the name of the zone on success or NULL on failure. 1693 * 1694 * This is a lot of code for a simple task; a new ioctl request to take care 1695 * of this might be a useful RFE. 1696 */ 1697 1698 static char * 1699 who_is_using(zlog_t *zlogp, struct lifreq *lifr) 1700 { 1701 static char answer[ZONENAME_MAX]; 1702 pid_t pid; 1703 int s, rlen, l, i; 1704 char *cp = rtmsg.space; 1705 struct sockaddr_dl *ifp = NULL; 1706 struct sockaddr *sa; 1707 char save_if_name[LIFNAMSIZ]; 1708 1709 answer[0] = '\0'; 1710 1711 pid = getpid(); 1712 if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) { 1713 zerror(zlogp, B_TRUE, "could not get routing socket"); 1714 return (NULL); 1715 } 1716 1717 if (lifr->lifr_addr.ss_family == AF_INET) { 1718 struct sockaddr_in *sin4; 1719 1720 so_dst.sa.sa_family = AF_INET; 1721 sin4 = (struct sockaddr_in *)&lifr->lifr_addr; 1722 so_dst.sin.sin_addr = sin4->sin_addr; 1723 } else { 1724 struct sockaddr_in6 *sin6; 1725 1726 so_dst.sa.sa_family = AF_INET6; 1727 sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr; 1728 so_dst.sin6.sin6_addr = sin6->sin6_addr; 1729 } 1730 1731 so_ifp.sa.sa_family = AF_LINK; 1732 1733 (void) memset(&rtmsg, 0, sizeof (rtmsg)); 1734 rtmsg.hdr.rtm_type = RTM_GET; 1735 rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST; 1736 rtmsg.hdr.rtm_version = RTM_VERSION; 1737 rtmsg.hdr.rtm_seq = ++rts_seqno; 1738 rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST; 1739 1740 l = ROUNDUP_LONG(salen(&so_dst.sa)); 1741 (void) memmove(cp, &(so_dst), l); 1742 cp += l; 1743 l = ROUNDUP_LONG(salen(&so_ifp.sa)); 1744 (void) memmove(cp, &(so_ifp), l); 1745 cp += l; 1746 1747 rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg; 1748 1749 if ((rlen = write(s, &rtmsg, l)) < 0) { 1750 zerror(zlogp, B_TRUE, "writing to routing socket"); 1751 return (NULL); 1752 } else if (rlen < (int)rtmsg.hdr.rtm_msglen) { 1753 zerror(zlogp, B_TRUE, 1754 "write to routing socket got only %d for len\n", rlen); 1755 return (NULL); 1756 } 1757 do { 1758 l = read(s, &rtmsg, sizeof (rtmsg)); 1759 } while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno || 1760 rtmsg.hdr.rtm_pid != pid)); 1761 if (l < 0) { 1762 zerror(zlogp, B_TRUE, "reading from routing socket"); 1763 return (NULL); 1764 } 1765 1766 if (rtmsg.hdr.rtm_version != RTM_VERSION) { 1767 zerror(zlogp, B_FALSE, 1768 "routing message version %d not understood", 1769 rtmsg.hdr.rtm_version); 1770 return (NULL); 1771 } 1772 if (rtmsg.hdr.rtm_msglen != (ushort_t)l) { 1773 zerror(zlogp, B_FALSE, "message length mismatch, " 1774 "expected %d bytes, returned %d bytes", 1775 rtmsg.hdr.rtm_msglen, l); 1776 return (NULL); 1777 } 1778 if (rtmsg.hdr.rtm_errno != 0) { 1779 errno = rtmsg.hdr.rtm_errno; 1780 zerror(zlogp, B_TRUE, "RTM_GET routing socket message"); 1781 return (NULL); 1782 } 1783 if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) { 1784 zerror(zlogp, B_FALSE, "interface not found"); 1785 return (NULL); 1786 } 1787 cp = ((char *)(&rtmsg.hdr + 1)); 1788 for (i = 1; i != 0; i <<= 1) { 1789 /* LINTED E_BAD_PTR_CAST_ALIGN */ 1790 sa = (struct sockaddr *)cp; 1791 if (i != RTA_IFP) { 1792 if ((i & rtmsg.hdr.rtm_addrs) != 0) 1793 cp += ROUNDUP_LONG(salen(sa)); 1794 continue; 1795 } 1796 if (sa->sa_family == AF_LINK && 1797 ((struct sockaddr_dl *)sa)->sdl_nlen != 0) 1798 ifp = (struct sockaddr_dl *)sa; 1799 break; 1800 } 1801 if (ifp == NULL) { 1802 zerror(zlogp, B_FALSE, "interface could not be determined"); 1803 return (NULL); 1804 } 1805 1806 /* 1807 * We need to set the I/F name to what we got above, then do the 1808 * appropriate ioctl to get its zone name. But lifr->lifr_name is 1809 * used by the calling function to do a REMOVEIF, so if we leave the 1810 * "good" zone's I/F name in place, *that* I/F will be removed instead 1811 * of the bad one. So we save the old (bad) I/F name before over- 1812 * writing it and doing the ioctl, then restore it after the ioctl. 1813 */ 1814 (void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name)); 1815 (void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen); 1816 lifr->lifr_name[ifp->sdl_nlen] = '\0'; 1817 i = ioctl(s, SIOCGLIFZONE, lifr); 1818 (void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name)); 1819 if (i < 0) { 1820 zerror(zlogp, B_TRUE, 1821 "%s: could not determine the zone interface belongs to", 1822 lifr->lifr_name); 1823 return (NULL); 1824 } 1825 if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0) 1826 (void) snprintf(answer, sizeof (answer), "%d", 1827 lifr->lifr_zoneid); 1828 1829 if (strlen(answer) > 0) 1830 return (answer); 1831 return (NULL); 1832 } 1833 1834 typedef struct mcast_rtmsg_s { 1835 struct rt_msghdr m_rtm; 1836 union { 1837 struct { 1838 struct sockaddr_in m_dst; 1839 struct sockaddr_in m_gw; 1840 struct sockaddr_in m_netmask; 1841 } m_v4; 1842 struct { 1843 struct sockaddr_in6 m_dst; 1844 struct sockaddr_in6 m_gw; 1845 struct sockaddr_in6 m_netmask; 1846 } m_v6; 1847 } m_u; 1848 } mcast_rtmsg_t; 1849 #define m_dst4 m_u.m_v4.m_dst 1850 #define m_dst6 m_u.m_v6.m_dst 1851 #define m_gw4 m_u.m_v4.m_gw 1852 #define m_gw6 m_u.m_v6.m_gw 1853 #define m_netmask4 m_u.m_v4.m_netmask 1854 #define m_netmask6 m_u.m_v6.m_netmask 1855 1856 /* 1857 * Configures a single interface: a new virtual interface is added, based on 1858 * the physical interface nwiftabptr->zone_nwif_physical, with the address 1859 * specified in nwiftabptr->zone_nwif_address, for zone zone_id. Note that 1860 * the "address" can be an IPv6 address (with a /prefixlength required), an 1861 * IPv4 address (with a /prefixlength optional), or a name; for the latter, 1862 * an IPv4 name-to-address resolution will be attempted. 1863 * 1864 * A default interface route for multicast is created on the first IPv4 and 1865 * IPv6 interfaces (that have the IFF_MULTICAST flag set), respectively. 1866 * This should really be done in the init scripts if we ever allow zones to 1867 * modify the routing tables. 1868 * 1869 * If anything goes wrong, we log an detailed error message, attempt to tear 1870 * down whatever we set up and return an error. 1871 */ 1872 static int 1873 configure_one_interface(zlog_t *zlogp, zoneid_t zone_id, 1874 struct zone_nwiftab *nwiftabptr, boolean_t *mcast_rt_v4_setp, 1875 boolean_t *mcast_rt_v6_setp) 1876 { 1877 struct lifreq lifr; 1878 struct sockaddr_in netmask4; 1879 struct sockaddr_in6 netmask6; 1880 struct in_addr in4; 1881 struct in6_addr in6; 1882 sa_family_t af; 1883 char *slashp = strchr(nwiftabptr->zone_nwif_address, '/'); 1884 mcast_rtmsg_t mcast_rtmsg; 1885 int s; 1886 int rs; 1887 int rlen; 1888 boolean_t got_netmask = B_FALSE; 1889 char addrstr4[INET_ADDRSTRLEN]; 1890 int res; 1891 1892 res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr); 1893 if (res != Z_OK) { 1894 zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res), 1895 nwiftabptr->zone_nwif_address); 1896 return (-1); 1897 } 1898 af = lifr.lifr_addr.ss_family; 1899 if (af == AF_INET) 1900 in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr; 1901 else 1902 in6 = ((struct sockaddr_in6 *)(&lifr.lifr_addr))->sin6_addr; 1903 1904 if ((s = socket(af, SOCK_DGRAM, 0)) < 0) { 1905 zerror(zlogp, B_TRUE, "could not get socket"); 1906 return (-1); 1907 } 1908 1909 (void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical, 1910 sizeof (lifr.lifr_name)); 1911 if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) { 1912 zerror(zlogp, B_TRUE, "%s: could not add interface", 1913 lifr.lifr_name); 1914 (void) close(s); 1915 return (-1); 1916 } 1917 1918 if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) { 1919 zerror(zlogp, B_TRUE, 1920 "%s: could not set IP address to %s", 1921 lifr.lifr_name, nwiftabptr->zone_nwif_address); 1922 goto bad; 1923 } 1924 1925 /* Preserve literal IPv4 address for later potential printing. */ 1926 if (af == AF_INET) 1927 (void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN); 1928 1929 lifr.lifr_zoneid = zone_id; 1930 if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) { 1931 zerror(zlogp, B_TRUE, "%s: could not place interface into zone", 1932 lifr.lifr_name); 1933 goto bad; 1934 } 1935 1936 if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) { 1937 got_netmask = B_TRUE; /* default setting will be correct */ 1938 } else { 1939 if (af == AF_INET) { 1940 /* 1941 * The IPv4 netmask can be determined either 1942 * directly if a prefix length was supplied with 1943 * the address or via the netmasks database. Not 1944 * being able to determine it is a common failure, 1945 * but it often is not fatal to operation of the 1946 * interface. In that case, a warning will be 1947 * printed after the rest of the interface's 1948 * parameters have been configured. 1949 */ 1950 (void) memset(&netmask4, 0, sizeof (netmask4)); 1951 if (slashp != NULL) { 1952 if (addr2netmask(slashp + 1, V4_ADDR_LEN, 1953 (uchar_t *)&netmask4.sin_addr) != 0) { 1954 *slashp = '/'; 1955 zerror(zlogp, B_FALSE, 1956 "%s: invalid prefix length in %s", 1957 lifr.lifr_name, 1958 nwiftabptr->zone_nwif_address); 1959 goto bad; 1960 } 1961 got_netmask = B_TRUE; 1962 } else if (getnetmaskbyaddr(in4, 1963 &netmask4.sin_addr) == 0) { 1964 got_netmask = B_TRUE; 1965 } 1966 if (got_netmask) { 1967 netmask4.sin_family = af; 1968 (void) memcpy(&lifr.lifr_addr, &netmask4, 1969 sizeof (netmask4)); 1970 } 1971 } else { 1972 (void) memset(&netmask6, 0, sizeof (netmask6)); 1973 if (addr2netmask(slashp + 1, V6_ADDR_LEN, 1974 (uchar_t *)&netmask6.sin6_addr) != 0) { 1975 *slashp = '/'; 1976 zerror(zlogp, B_FALSE, 1977 "%s: invalid prefix length in %s", 1978 lifr.lifr_name, 1979 nwiftabptr->zone_nwif_address); 1980 goto bad; 1981 } 1982 got_netmask = B_TRUE; 1983 netmask6.sin6_family = af; 1984 (void) memcpy(&lifr.lifr_addr, &netmask6, 1985 sizeof (netmask6)); 1986 } 1987 if (got_netmask && 1988 ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) { 1989 zerror(zlogp, B_TRUE, "%s: could not set netmask", 1990 lifr.lifr_name); 1991 goto bad; 1992 } 1993 1994 /* 1995 * This doesn't set the broadcast address at all. Rather, it 1996 * gets, then sets the interface's address, relying on the fact 1997 * that resetting the address will reset the broadcast address. 1998 */ 1999 if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 2000 zerror(zlogp, B_TRUE, "%s: could not get address", 2001 lifr.lifr_name); 2002 goto bad; 2003 } 2004 if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) { 2005 zerror(zlogp, B_TRUE, 2006 "%s: could not reset broadcast address", 2007 lifr.lifr_name); 2008 goto bad; 2009 } 2010 } 2011 2012 if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 2013 zerror(zlogp, B_TRUE, "%s: could not get flags", 2014 lifr.lifr_name); 2015 goto bad; 2016 } 2017 lifr.lifr_flags |= IFF_UP; 2018 if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) { 2019 int save_errno = errno; 2020 char *zone_using; 2021 2022 /* 2023 * If we failed with something other than EADDRNOTAVAIL, 2024 * then skip to the end. Otherwise, look up our address, 2025 * then call a function to determine which zone is already 2026 * using that address. 2027 */ 2028 if (errno != EADDRNOTAVAIL) { 2029 zerror(zlogp, B_TRUE, 2030 "%s: could not bring interface up", lifr.lifr_name); 2031 goto bad; 2032 } 2033 if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 2034 zerror(zlogp, B_TRUE, "%s: could not get address", 2035 lifr.lifr_name); 2036 goto bad; 2037 } 2038 zone_using = who_is_using(zlogp, &lifr); 2039 errno = save_errno; 2040 if (zone_using == NULL) 2041 zerror(zlogp, B_TRUE, 2042 "%s: could not bring interface up", lifr.lifr_name); 2043 else 2044 zerror(zlogp, B_TRUE, "%s: could not bring interface " 2045 "up: address in use by zone '%s'", lifr.lifr_name, 2046 zone_using); 2047 goto bad; 2048 } 2049 if ((lifr.lifr_flags & IFF_MULTICAST) && ((af == AF_INET && 2050 mcast_rt_v4_setp != NULL && *mcast_rt_v4_setp == B_FALSE) || 2051 (af == AF_INET6 && 2052 mcast_rt_v6_setp != NULL && *mcast_rt_v6_setp == B_FALSE))) { 2053 rs = socket(PF_ROUTE, SOCK_RAW, 0); 2054 if (rs < 0) { 2055 zerror(zlogp, B_TRUE, "%s: could not create " 2056 "routing socket", lifr.lifr_name); 2057 goto bad; 2058 } 2059 (void) shutdown(rs, 0); 2060 (void) memset((void *)&mcast_rtmsg, 0, sizeof (mcast_rtmsg_t)); 2061 mcast_rtmsg.m_rtm.rtm_msglen = sizeof (struct rt_msghdr) + 2062 3 * (af == AF_INET ? sizeof (struct sockaddr_in) : 2063 sizeof (struct sockaddr_in6)); 2064 mcast_rtmsg.m_rtm.rtm_version = RTM_VERSION; 2065 mcast_rtmsg.m_rtm.rtm_type = RTM_ADD; 2066 mcast_rtmsg.m_rtm.rtm_flags = RTF_UP; 2067 mcast_rtmsg.m_rtm.rtm_addrs = 2068 RTA_DST | RTA_GATEWAY | RTA_NETMASK; 2069 mcast_rtmsg.m_rtm.rtm_seq = ++rts_seqno; 2070 if (af == AF_INET) { 2071 mcast_rtmsg.m_dst4.sin_family = AF_INET; 2072 mcast_rtmsg.m_dst4.sin_addr.s_addr = 2073 htonl(INADDR_UNSPEC_GROUP); 2074 mcast_rtmsg.m_gw4.sin_family = AF_INET; 2075 mcast_rtmsg.m_gw4.sin_addr = in4; 2076 mcast_rtmsg.m_netmask4.sin_family = AF_INET; 2077 mcast_rtmsg.m_netmask4.sin_addr.s_addr = 2078 htonl(IN_CLASSD_NET); 2079 } else { 2080 mcast_rtmsg.m_dst6.sin6_family = AF_INET6; 2081 mcast_rtmsg.m_dst6.sin6_addr.s6_addr[0] = 0xffU; 2082 mcast_rtmsg.m_gw6.sin6_family = AF_INET6; 2083 mcast_rtmsg.m_gw6.sin6_addr = in6; 2084 mcast_rtmsg.m_netmask6.sin6_family = AF_INET6; 2085 mcast_rtmsg.m_netmask6.sin6_addr.s6_addr[0] = 0xffU; 2086 } 2087 rlen = write(rs, (char *)&mcast_rtmsg, 2088 mcast_rtmsg.m_rtm.rtm_msglen); 2089 if (rlen < mcast_rtmsg.m_rtm.rtm_msglen) { 2090 if (rlen < 0) { 2091 zerror(zlogp, B_TRUE, "%s: could not set " 2092 "default interface for multicast", 2093 lifr.lifr_name); 2094 } else { 2095 zerror(zlogp, B_FALSE, "%s: write to routing " 2096 "socket returned %d", lifr.lifr_name, rlen); 2097 } 2098 (void) close(rs); 2099 goto bad; 2100 } 2101 if (af == AF_INET) { 2102 *mcast_rt_v4_setp = B_TRUE; 2103 } else { 2104 *mcast_rt_v6_setp = B_TRUE; 2105 } 2106 (void) close(rs); 2107 } 2108 2109 if (!got_netmask) { 2110 /* 2111 * A common, but often non-fatal problem, is that the system 2112 * cannot find the netmask for an interface address. This is 2113 * often caused by it being only in /etc/inet/netmasks, but 2114 * /etc/nsswitch.conf says to use NIS or NIS+ and it's not 2115 * in that. This doesn't show up at boot because the netmask 2116 * is obtained from /etc/inet/netmasks when no network 2117 * interfaces are up, but isn't consulted when NIS/NIS+ is 2118 * available. We warn the user here that something like this 2119 * has happened and we're just running with a default and 2120 * possible incorrect netmask. 2121 */ 2122 char buffer[INET6_ADDRSTRLEN]; 2123 void *addr; 2124 2125 if (af == AF_INET) 2126 addr = &((struct sockaddr_in *) 2127 (&lifr.lifr_addr))->sin_addr; 2128 else 2129 addr = &((struct sockaddr_in6 *) 2130 (&lifr.lifr_addr))->sin6_addr; 2131 2132 /* Find out what netmask interface is going to be using */ 2133 if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 || 2134 inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL) 2135 goto bad; 2136 zerror(zlogp, B_FALSE, 2137 "WARNING: %s: no matching subnet found in netmasks(4) for " 2138 "%s; using default of %s.", 2139 lifr.lifr_name, addrstr4, buffer); 2140 } 2141 2142 (void) close(s); 2143 return (Z_OK); 2144 bad: 2145 (void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr); 2146 (void) close(s); 2147 return (-1); 2148 } 2149 2150 /* 2151 * Sets up network interfaces based on information from the zone configuration. 2152 * An IPv4 loopback interface is set up "for free", modeling the global system. 2153 * If any of the configuration interfaces were IPv6, then an IPv6 loopback 2154 * address is set up as well. 2155 * 2156 * If anything goes wrong, we log a general error message, attempt to tear down 2157 * whatever we set up, and return an error. 2158 */ 2159 static int 2160 configure_network_interfaces(zlog_t *zlogp) 2161 { 2162 zone_dochandle_t handle; 2163 struct zone_nwiftab nwiftab, loopback_iftab; 2164 boolean_t saw_v6 = B_FALSE; 2165 boolean_t mcast_rt_v4_set = B_FALSE; 2166 boolean_t mcast_rt_v6_set = B_FALSE; 2167 zoneid_t zoneid; 2168 2169 if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) { 2170 zerror(zlogp, B_TRUE, "unable to get zoneid"); 2171 return (-1); 2172 } 2173 2174 if ((handle = zonecfg_init_handle()) == NULL) { 2175 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 2176 return (-1); 2177 } 2178 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 2179 zerror(zlogp, B_FALSE, "invalid configuration"); 2180 zonecfg_fini_handle(handle); 2181 return (-1); 2182 } 2183 if (zonecfg_setnwifent(handle) == Z_OK) { 2184 for (;;) { 2185 struct in6_addr in6; 2186 2187 if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK) 2188 break; 2189 if (configure_one_interface(zlogp, zoneid, 2190 &nwiftab, &mcast_rt_v4_set, &mcast_rt_v6_set) != 2191 Z_OK) { 2192 (void) zonecfg_endnwifent(handle); 2193 zonecfg_fini_handle(handle); 2194 return (-1); 2195 } 2196 if (inet_pton(AF_INET6, nwiftab.zone_nwif_address, 2197 &in6) == 1) 2198 saw_v6 = B_TRUE; 2199 } 2200 (void) zonecfg_endnwifent(handle); 2201 } 2202 zonecfg_fini_handle(handle); 2203 (void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0", 2204 sizeof (loopback_iftab.zone_nwif_physical)); 2205 (void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1", 2206 sizeof (loopback_iftab.zone_nwif_address)); 2207 if (configure_one_interface(zlogp, zoneid, &loopback_iftab, NULL, NULL) 2208 != Z_OK) { 2209 return (-1); 2210 } 2211 if (saw_v6) { 2212 (void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128", 2213 sizeof (loopback_iftab.zone_nwif_address)); 2214 if (configure_one_interface(zlogp, zoneid, 2215 &loopback_iftab, NULL, NULL) != Z_OK) { 2216 return (-1); 2217 } 2218 } 2219 return (0); 2220 } 2221 2222 static int 2223 tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid, 2224 const struct sockaddr_storage *local, const struct sockaddr_storage *remote) 2225 { 2226 int fd; 2227 struct strioctl ioc; 2228 tcp_ioc_abort_conn_t conn; 2229 int error; 2230 2231 conn.ac_local = *local; 2232 conn.ac_remote = *remote; 2233 conn.ac_start = TCPS_SYN_SENT; 2234 conn.ac_end = TCPS_TIME_WAIT; 2235 conn.ac_zoneid = zoneid; 2236 2237 ioc.ic_cmd = TCP_IOC_ABORT_CONN; 2238 ioc.ic_timout = -1; /* infinite timeout */ 2239 ioc.ic_len = sizeof (conn); 2240 ioc.ic_dp = (char *)&conn; 2241 2242 if ((fd = open("/dev/tcp", O_RDONLY)) < 0) { 2243 zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp"); 2244 return (-1); 2245 } 2246 2247 error = ioctl(fd, I_STR, &ioc); 2248 (void) close(fd); 2249 if (error == 0 || errno == ENOENT) /* ENOENT is not an error */ 2250 return (0); 2251 return (-1); 2252 } 2253 2254 static int 2255 tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid) 2256 { 2257 struct sockaddr_storage l, r; 2258 struct sockaddr_in *local, *remote; 2259 struct sockaddr_in6 *local6, *remote6; 2260 int error; 2261 2262 /* 2263 * Abort IPv4 connections. 2264 */ 2265 bzero(&l, sizeof (*local)); 2266 local = (struct sockaddr_in *)&l; 2267 local->sin_family = AF_INET; 2268 local->sin_addr.s_addr = INADDR_ANY; 2269 local->sin_port = 0; 2270 2271 bzero(&r, sizeof (*remote)); 2272 remote = (struct sockaddr_in *)&r; 2273 remote->sin_family = AF_INET; 2274 remote->sin_addr.s_addr = INADDR_ANY; 2275 remote->sin_port = 0; 2276 2277 if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0) 2278 return (error); 2279 2280 /* 2281 * Abort IPv6 connections. 2282 */ 2283 bzero(&l, sizeof (*local6)); 2284 local6 = (struct sockaddr_in6 *)&l; 2285 local6->sin6_family = AF_INET6; 2286 local6->sin6_port = 0; 2287 local6->sin6_addr = in6addr_any; 2288 2289 bzero(&r, sizeof (*remote6)); 2290 remote6 = (struct sockaddr_in6 *)&r; 2291 remote6->sin6_family = AF_INET6; 2292 remote6->sin6_port = 0; 2293 remote6->sin6_addr = in6addr_any; 2294 2295 if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0) 2296 return (error); 2297 return (0); 2298 } 2299 2300 static int 2301 devfsadm_call(zlog_t *zlogp, const char *arg) 2302 { 2303 char *argv[4]; 2304 int status; 2305 2306 argv[0] = DEVFSADM; 2307 argv[1] = (char *)arg; 2308 argv[2] = zone_name; 2309 argv[3] = NULL; 2310 status = forkexec(zlogp, DEVFSADM_PATH, argv); 2311 if (status == 0 || status == -1) 2312 return (status); 2313 zerror(zlogp, B_FALSE, "%s call (%s %s %s) unexpectedly returned %d", 2314 DEVFSADM, DEVFSADM_PATH, arg, zone_name, status); 2315 return (-1); 2316 } 2317 2318 static int 2319 devfsadm_register(zlog_t *zlogp) 2320 { 2321 /* 2322 * Ready the zone's devices. 2323 */ 2324 return (devfsadm_call(zlogp, "-z")); 2325 } 2326 2327 static int 2328 devfsadm_unregister(zlog_t *zlogp) 2329 { 2330 return (devfsadm_call(zlogp, "-Z")); 2331 } 2332 2333 static int 2334 get_privset(zlog_t *zlogp, priv_set_t *privs, boolean_t mount_cmd) 2335 { 2336 int error = -1; 2337 zone_dochandle_t handle; 2338 char *privname = NULL; 2339 2340 if (mount_cmd) { 2341 if (zonecfg_default_privset(privs) == Z_OK) 2342 return (0); 2343 zerror(zlogp, B_FALSE, 2344 "failed to determine the zone's default privilege set"); 2345 return (-1); 2346 } 2347 2348 if ((handle = zonecfg_init_handle()) == NULL) { 2349 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 2350 return (-1); 2351 } 2352 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 2353 zerror(zlogp, B_FALSE, "invalid configuration"); 2354 zonecfg_fini_handle(handle); 2355 return (-1); 2356 } 2357 2358 switch (zonecfg_get_privset(handle, privs, &privname)) { 2359 case Z_OK: 2360 error = 0; 2361 break; 2362 case Z_PRIV_PROHIBITED: 2363 zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted " 2364 "within the zone's privilege set", privname); 2365 break; 2366 case Z_PRIV_REQUIRED: 2367 zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing " 2368 "from the zone's privilege set", privname); 2369 break; 2370 case Z_PRIV_UNKNOWN: 2371 zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified " 2372 "in the zone's privilege set", privname); 2373 break; 2374 default: 2375 zerror(zlogp, B_FALSE, "failed to determine the zone's " 2376 "privilege set"); 2377 break; 2378 } 2379 2380 free(privname); 2381 zonecfg_fini_handle(handle); 2382 return (error); 2383 } 2384 2385 static int 2386 get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep) 2387 { 2388 nvlist_t *nvl = NULL; 2389 char *nvl_packed = NULL; 2390 size_t nvl_size = 0; 2391 nvlist_t **nvlv = NULL; 2392 int rctlcount = 0; 2393 int error = -1; 2394 zone_dochandle_t handle; 2395 struct zone_rctltab rctltab; 2396 rctlblk_t *rctlblk = NULL; 2397 2398 *bufp = NULL; 2399 *bufsizep = 0; 2400 2401 if ((handle = zonecfg_init_handle()) == NULL) { 2402 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 2403 return (-1); 2404 } 2405 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 2406 zerror(zlogp, B_FALSE, "invalid configuration"); 2407 zonecfg_fini_handle(handle); 2408 return (-1); 2409 } 2410 2411 rctltab.zone_rctl_valptr = NULL; 2412 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 2413 zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc"); 2414 goto out; 2415 } 2416 2417 if (zonecfg_setrctlent(handle) != Z_OK) { 2418 zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent"); 2419 goto out; 2420 } 2421 2422 if ((rctlblk = malloc(rctlblk_size())) == NULL) { 2423 zerror(zlogp, B_TRUE, "memory allocation failed"); 2424 goto out; 2425 } 2426 while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) { 2427 struct zone_rctlvaltab *rctlval; 2428 uint_t i, count; 2429 const char *name = rctltab.zone_rctl_name; 2430 2431 /* zoneadm should have already warned about unknown rctls. */ 2432 if (!zonecfg_is_rctl(name)) { 2433 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 2434 rctltab.zone_rctl_valptr = NULL; 2435 continue; 2436 } 2437 count = 0; 2438 for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 2439 rctlval = rctlval->zone_rctlval_next) { 2440 count++; 2441 } 2442 if (count == 0) { /* ignore */ 2443 continue; /* Nothing to free */ 2444 } 2445 if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL) 2446 goto out; 2447 i = 0; 2448 for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 2449 rctlval = rctlval->zone_rctlval_next, i++) { 2450 if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) { 2451 zerror(zlogp, B_TRUE, "%s failed", 2452 "nvlist_alloc"); 2453 goto out; 2454 } 2455 if (zonecfg_construct_rctlblk(rctlval, rctlblk) 2456 != Z_OK) { 2457 zerror(zlogp, B_FALSE, "invalid rctl value: " 2458 "(priv=%s,limit=%s,action=%s)", 2459 rctlval->zone_rctlval_priv, 2460 rctlval->zone_rctlval_limit, 2461 rctlval->zone_rctlval_action); 2462 goto out; 2463 } 2464 if (!zonecfg_valid_rctl(name, rctlblk)) { 2465 zerror(zlogp, B_FALSE, 2466 "(priv=%s,limit=%s,action=%s) is not a " 2467 "valid value for rctl '%s'", 2468 rctlval->zone_rctlval_priv, 2469 rctlval->zone_rctlval_limit, 2470 rctlval->zone_rctlval_action, 2471 name); 2472 goto out; 2473 } 2474 if (nvlist_add_uint64(nvlv[i], "privilege", 2475 rctlblk_get_privilege(rctlblk)) != 0) { 2476 zerror(zlogp, B_FALSE, "%s failed", 2477 "nvlist_add_uint64"); 2478 goto out; 2479 } 2480 if (nvlist_add_uint64(nvlv[i], "limit", 2481 rctlblk_get_value(rctlblk)) != 0) { 2482 zerror(zlogp, B_FALSE, "%s failed", 2483 "nvlist_add_uint64"); 2484 goto out; 2485 } 2486 if (nvlist_add_uint64(nvlv[i], "action", 2487 (uint_t)rctlblk_get_local_action(rctlblk, NULL)) 2488 != 0) { 2489 zerror(zlogp, B_FALSE, "%s failed", 2490 "nvlist_add_uint64"); 2491 goto out; 2492 } 2493 } 2494 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 2495 rctltab.zone_rctl_valptr = NULL; 2496 if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count) 2497 != 0) { 2498 zerror(zlogp, B_FALSE, "%s failed", 2499 "nvlist_add_nvlist_array"); 2500 goto out; 2501 } 2502 for (i = 0; i < count; i++) 2503 nvlist_free(nvlv[i]); 2504 free(nvlv); 2505 nvlv = NULL; 2506 rctlcount++; 2507 } 2508 (void) zonecfg_endrctlent(handle); 2509 2510 if (rctlcount == 0) { 2511 error = 0; 2512 goto out; 2513 } 2514 if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0) 2515 != 0) { 2516 zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack"); 2517 goto out; 2518 } 2519 2520 error = 0; 2521 *bufp = nvl_packed; 2522 *bufsizep = nvl_size; 2523 2524 out: 2525 free(rctlblk); 2526 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 2527 if (error && nvl_packed != NULL) 2528 free(nvl_packed); 2529 if (nvl != NULL) 2530 nvlist_free(nvl); 2531 if (nvlv != NULL) 2532 free(nvlv); 2533 if (handle != NULL) 2534 zonecfg_fini_handle(handle); 2535 return (error); 2536 } 2537 2538 static int 2539 get_zone_pool(zlog_t *zlogp, char *poolbuf, size_t bufsz) 2540 { 2541 zone_dochandle_t handle; 2542 int error; 2543 2544 if ((handle = zonecfg_init_handle()) == NULL) { 2545 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 2546 return (Z_NOMEM); 2547 } 2548 error = zonecfg_get_snapshot_handle(zone_name, handle); 2549 if (error != Z_OK) { 2550 zerror(zlogp, B_FALSE, "invalid configuration"); 2551 zonecfg_fini_handle(handle); 2552 return (error); 2553 } 2554 error = zonecfg_get_pool(handle, poolbuf, bufsz); 2555 zonecfg_fini_handle(handle); 2556 return (error); 2557 } 2558 2559 static int 2560 get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep) 2561 { 2562 zone_dochandle_t handle; 2563 struct zone_dstab dstab; 2564 size_t total, offset, len; 2565 int error = -1; 2566 char *str; 2567 2568 *bufp = NULL; 2569 *bufsizep = 0; 2570 2571 if ((handle = zonecfg_init_handle()) == NULL) { 2572 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 2573 return (-1); 2574 } 2575 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 2576 zerror(zlogp, B_FALSE, "invalid configuration"); 2577 zonecfg_fini_handle(handle); 2578 return (-1); 2579 } 2580 2581 if (zonecfg_setdsent(handle) != Z_OK) { 2582 zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent"); 2583 goto out; 2584 } 2585 2586 total = 0; 2587 while (zonecfg_getdsent(handle, &dstab) == Z_OK) 2588 total += strlen(dstab.zone_dataset_name) + 1; 2589 (void) zonecfg_enddsent(handle); 2590 2591 if (total == 0) { 2592 error = 0; 2593 goto out; 2594 } 2595 2596 if ((str = malloc(total)) == NULL) { 2597 zerror(zlogp, B_TRUE, "memory allocation failed"); 2598 goto out; 2599 } 2600 2601 if (zonecfg_setdsent(handle) != Z_OK) { 2602 zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent"); 2603 goto out; 2604 } 2605 offset = 0; 2606 while (zonecfg_getdsent(handle, &dstab) == Z_OK) { 2607 len = strlen(dstab.zone_dataset_name); 2608 (void) strlcpy(str + offset, dstab.zone_dataset_name, 2609 sizeof (dstab.zone_dataset_name) - offset); 2610 offset += len; 2611 if (offset != total - 1) 2612 str[offset++] = ','; 2613 } 2614 (void) zonecfg_enddsent(handle); 2615 2616 error = 0; 2617 *bufp = str; 2618 *bufsizep = total; 2619 2620 out: 2621 if (error != 0 && str != NULL) 2622 free(str); 2623 if (handle != NULL) 2624 zonecfg_fini_handle(handle); 2625 2626 return (error); 2627 } 2628 2629 /* ARGSUSED */ 2630 static void 2631 zfs_error_handler(const char *fmt, va_list ap) 2632 { 2633 /* 2634 * Do nothing - we interpret the failures from each libzfs call below. 2635 */ 2636 } 2637 2638 static int 2639 validate_datasets(zlog_t *zlogp) 2640 { 2641 zone_dochandle_t handle; 2642 struct zone_dstab dstab; 2643 zfs_handle_t *zhp; 2644 2645 if ((handle = zonecfg_init_handle()) == NULL) { 2646 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 2647 return (-1); 2648 } 2649 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 2650 zerror(zlogp, B_FALSE, "invalid configuration"); 2651 zonecfg_fini_handle(handle); 2652 return (-1); 2653 } 2654 2655 if (zonecfg_setdsent(handle) != Z_OK) { 2656 zerror(zlogp, B_FALSE, "invalid configuration"); 2657 zonecfg_fini_handle(handle); 2658 return (-1); 2659 } 2660 2661 zfs_set_error_handler(zfs_error_handler); 2662 2663 while (zonecfg_getdsent(handle, &dstab) == Z_OK) { 2664 2665 if ((zhp = zfs_open(dstab.zone_dataset_name, 2666 ZFS_TYPE_FILESYSTEM)) == NULL) { 2667 zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'", 2668 dstab.zone_dataset_name); 2669 zonecfg_fini_handle(handle); 2670 return (-1); 2671 } 2672 2673 /* 2674 * Automatically set the 'zoned' property. We check the value 2675 * first because we'll get EPERM if it is already set. 2676 */ 2677 if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) && 2678 zfs_prop_set(zhp, ZFS_PROP_ZONED, "on") != 0) { 2679 zerror(zlogp, B_FALSE, "cannot set 'zoned' " 2680 "property for ZFS dataset '%s'\n", 2681 dstab.zone_dataset_name); 2682 zonecfg_fini_handle(handle); 2683 zfs_close(zhp); 2684 return (-1); 2685 } 2686 2687 zfs_close(zhp); 2688 } 2689 (void) zonecfg_enddsent(handle); 2690 2691 zonecfg_fini_handle(handle); 2692 2693 return (0); 2694 } 2695 2696 static int 2697 bind_to_pool(zlog_t *zlogp, zoneid_t zoneid) 2698 { 2699 pool_conf_t *poolconf; 2700 pool_t *pool; 2701 char poolname[MAXPATHLEN]; 2702 int status; 2703 int error; 2704 2705 /* 2706 * Find the pool mentioned in the zone configuration, and bind to it. 2707 */ 2708 error = get_zone_pool(zlogp, poolname, sizeof (poolname)); 2709 if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) { 2710 /* 2711 * The property is not set on the zone, so the pool 2712 * should be bound to the default pool. But that's 2713 * already done by the kernel, so we can just return. 2714 */ 2715 return (0); 2716 } 2717 if (error != Z_OK) { 2718 /* 2719 * Not an error, even though it shouldn't be happening. 2720 */ 2721 zerror(zlogp, B_FALSE, 2722 "WARNING: unable to retrieve default pool."); 2723 return (0); 2724 } 2725 /* 2726 * Don't do anything if pools aren't enabled. 2727 */ 2728 if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) { 2729 zerror(zlogp, B_FALSE, "WARNING: pools facility not active; " 2730 "zone will not be bound to pool '%s'.", poolname); 2731 return (0); 2732 } 2733 /* 2734 * Try to provide a sane error message if the requested pool doesn't 2735 * exist. 2736 */ 2737 if ((poolconf = pool_conf_alloc()) == NULL) { 2738 zerror(zlogp, B_FALSE, "%s failed", "pool_conf_alloc"); 2739 return (-1); 2740 } 2741 if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) != 2742 PO_SUCCESS) { 2743 zerror(zlogp, B_FALSE, "%s failed", "pool_conf_open"); 2744 pool_conf_free(poolconf); 2745 return (-1); 2746 } 2747 pool = pool_get_pool(poolconf, poolname); 2748 (void) pool_conf_close(poolconf); 2749 pool_conf_free(poolconf); 2750 if (pool == NULL) { 2751 zerror(zlogp, B_FALSE, "WARNING: pool '%s' not found; " 2752 "using default pool.", poolname); 2753 return (0); 2754 } 2755 /* 2756 * Bind the zone to the pool. 2757 */ 2758 if (pool_set_binding(poolname, P_ZONEID, zoneid) != PO_SUCCESS) { 2759 zerror(zlogp, B_FALSE, "WARNING: unable to bind to pool '%s'; " 2760 "using default pool.", poolname); 2761 } 2762 return (0); 2763 } 2764 2765 /* 2766 * Mount lower level home directories into/from current zone 2767 * Share exported directories specified in dfstab for zone 2768 */ 2769 static int 2770 tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath) 2771 { 2772 zoneid_t *zids = NULL; 2773 priv_set_t *zid_privs; 2774 const priv_impl_info_t *ip = NULL; 2775 uint_t nzents_saved; 2776 uint_t nzents; 2777 int i; 2778 char readonly[] = "ro"; 2779 struct zone_fstab lower_fstab; 2780 char *argv[4]; 2781 2782 if (!is_system_labeled()) 2783 return (0); 2784 2785 if (zid_label == NULL) { 2786 zid_label = m_label_alloc(MAC_LABEL); 2787 if (zid_label == NULL) 2788 return (-1); 2789 } 2790 2791 /* Make sure our zone has an /export/home dir */ 2792 (void) make_one_dir(zlogp, rootpath, "/export/home", 2793 DEFAULT_DIR_MODE); 2794 2795 lower_fstab.zone_fs_raw[0] = '\0'; 2796 (void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS, 2797 sizeof (lower_fstab.zone_fs_type)); 2798 lower_fstab.zone_fs_options = NULL; 2799 (void) zonecfg_add_fs_option(&lower_fstab, readonly); 2800 2801 /* 2802 * Get the list of zones from the kernel 2803 */ 2804 if (zone_list(NULL, &nzents) != 0) { 2805 zerror(zlogp, B_TRUE, "unable to list zones"); 2806 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 2807 return (-1); 2808 } 2809 again: 2810 if (nzents == 0) { 2811 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 2812 return (-1); 2813 } 2814 2815 zids = malloc(nzents * sizeof (zoneid_t)); 2816 if (zids == NULL) { 2817 zerror(zlogp, B_TRUE, "unable to allocate memory"); 2818 return (-1); 2819 } 2820 nzents_saved = nzents; 2821 2822 if (zone_list(zids, &nzents) != 0) { 2823 zerror(zlogp, B_TRUE, "unable to list zones"); 2824 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 2825 free(zids); 2826 return (-1); 2827 } 2828 if (nzents != nzents_saved) { 2829 /* list changed, try again */ 2830 free(zids); 2831 goto again; 2832 } 2833 2834 ip = getprivimplinfo(); 2835 if ((zid_privs = priv_allocset()) == NULL) { 2836 zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 2837 zonecfg_free_fs_option_list( 2838 lower_fstab.zone_fs_options); 2839 free(zids); 2840 return (-1); 2841 } 2842 2843 for (i = 0; i < nzents; i++) { 2844 char zid_name[ZONENAME_MAX]; 2845 zone_state_t zid_state; 2846 char zid_rpath[MAXPATHLEN]; 2847 struct stat stat_buf; 2848 2849 if (zids[i] == GLOBAL_ZONEID) 2850 continue; 2851 2852 if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1) 2853 continue; 2854 2855 /* 2856 * Do special setup for the zone we are booting 2857 */ 2858 if (strcmp(zid_name, zone_name) == 0) { 2859 struct zone_fstab autofs_fstab; 2860 char map_path[MAXPATHLEN]; 2861 int fd; 2862 2863 /* 2864 * Create auto_home_<zone> map for this zone 2865 * in the global zone. The local zone entry 2866 * will be created by automount when the zone 2867 * is booted. 2868 */ 2869 2870 (void) snprintf(autofs_fstab.zone_fs_special, 2871 MAXPATHLEN, "auto_home_%s", zid_name); 2872 2873 (void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN, 2874 "/zone/%s/home", zid_name); 2875 2876 (void) snprintf(map_path, sizeof (map_path), 2877 "/etc/%s", autofs_fstab.zone_fs_special); 2878 /* 2879 * If the map file doesn't exist create a template 2880 */ 2881 if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL, 2882 S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) { 2883 int len; 2884 char map_rec[MAXPATHLEN]; 2885 2886 len = snprintf(map_rec, sizeof (map_rec), 2887 "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n", 2888 autofs_fstab.zone_fs_special, rootpath); 2889 (void) write(fd, map_rec, len); 2890 (void) close(fd); 2891 } 2892 2893 /* 2894 * Mount auto_home_<zone> in the global zone if absent. 2895 * If it's already of type autofs, then 2896 * don't mount it again. 2897 */ 2898 if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) || 2899 strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) { 2900 char optstr[] = "indirect,ignore,nobrowse"; 2901 2902 (void) make_one_dir(zlogp, "", 2903 autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE); 2904 2905 /* 2906 * Mount will fail if automounter has already 2907 * processed the auto_home_<zonename> map 2908 */ 2909 (void) domount(zlogp, MNTTYPE_AUTOFS, optstr, 2910 autofs_fstab.zone_fs_special, 2911 autofs_fstab.zone_fs_dir); 2912 } 2913 continue; 2914 } 2915 2916 2917 if (zone_get_state(zid_name, &zid_state) != Z_OK || 2918 (zid_state != ZONE_STATE_MOUNTED && 2919 zid_state != ZONE_STATE_RUNNING)) { 2920 2921 /* Skip over zones without mounted filesystems */ 2922 continue; 2923 } 2924 2925 if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label, 2926 sizeof (m_label_t)) < 0) 2927 /* Skip over zones with unspecified label */ 2928 continue; 2929 2930 if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath, 2931 sizeof (zid_rpath)) == -1) 2932 /* Skip over zones with bad path */ 2933 continue; 2934 2935 if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs, 2936 sizeof (priv_chunk_t) * ip->priv_setsize) == -1) 2937 /* Skip over zones with bad privs */ 2938 continue; 2939 2940 /* 2941 * Reading down is valid according to our label model 2942 * but some customers want to disable it because it 2943 * allows execute down and other possible attacks. 2944 * Therefore, we restrict this feature to zones that 2945 * have the NET_MAC_AWARE privilege which is required 2946 * for NFS read-down semantics. 2947 */ 2948 if ((bldominates(zlabel, zid_label)) && 2949 (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) { 2950 /* 2951 * Our zone dominates this one. 2952 * Create a lofs mount from lower zone's /export/home 2953 */ 2954 (void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN, 2955 "%s/zone/%s/export/home", rootpath, zid_name); 2956 2957 /* 2958 * If the target is already an LOFS mount 2959 * then don't do it again. 2960 */ 2961 if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) || 2962 strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) { 2963 2964 if (snprintf(lower_fstab.zone_fs_special, 2965 MAXPATHLEN, "%s/export", 2966 zid_rpath) > MAXPATHLEN) 2967 continue; 2968 2969 /* 2970 * Make sure the lower-level home exists 2971 */ 2972 if (make_one_dir(zlogp, 2973 lower_fstab.zone_fs_special, 2974 "/home", DEFAULT_DIR_MODE) != 0) 2975 continue; 2976 2977 (void) strlcat(lower_fstab.zone_fs_special, 2978 "/home", MAXPATHLEN); 2979 2980 /* 2981 * Mount can fail because the lower-level 2982 * zone may have already done a mount up. 2983 */ 2984 (void) mount_one(zlogp, &lower_fstab, ""); 2985 } 2986 } else if ((bldominates(zid_label, zlabel)) && 2987 (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) { 2988 /* 2989 * This zone dominates our zone. 2990 * Create a lofs mount from our zone's /export/home 2991 */ 2992 if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN, 2993 "%s/zone/%s/export/home", zid_rpath, 2994 zone_name) > MAXPATHLEN) 2995 continue; 2996 2997 /* 2998 * If the target is already an LOFS mount 2999 * then don't do it again. 3000 */ 3001 if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) || 3002 strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) { 3003 3004 (void) snprintf(lower_fstab.zone_fs_special, 3005 MAXPATHLEN, "%s/export/home", rootpath); 3006 3007 /* 3008 * Mount can fail because the higher-level 3009 * zone may have already done a mount down. 3010 */ 3011 (void) mount_one(zlogp, &lower_fstab, ""); 3012 } 3013 } 3014 } 3015 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 3016 priv_freeset(zid_privs); 3017 free(zids); 3018 3019 /* 3020 * Now share any exported directories from this zone. 3021 * Each zone can have its own dfstab. 3022 */ 3023 3024 argv[0] = "zoneshare"; 3025 argv[1] = "-z"; 3026 argv[2] = zone_name; 3027 argv[3] = NULL; 3028 3029 (void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv); 3030 /* Don't check for errors since they don't affect the zone */ 3031 3032 return (0); 3033 } 3034 3035 /* 3036 * Unmount lofs mounts from higher level zones 3037 * Unshare nfs exported directories 3038 */ 3039 static void 3040 tsol_unmounts(zlog_t *zlogp, char *zone_name) 3041 { 3042 zoneid_t *zids = NULL; 3043 uint_t nzents_saved; 3044 uint_t nzents; 3045 int i; 3046 char *argv[4]; 3047 char path[MAXPATHLEN]; 3048 3049 if (!is_system_labeled()) 3050 return; 3051 3052 /* 3053 * Get the list of zones from the kernel 3054 */ 3055 if (zone_list(NULL, &nzents) != 0) { 3056 return; 3057 } 3058 3059 if (zid_label == NULL) { 3060 zid_label = m_label_alloc(MAC_LABEL); 3061 if (zid_label == NULL) 3062 return; 3063 } 3064 3065 again: 3066 if (nzents == 0) 3067 return; 3068 3069 zids = malloc(nzents * sizeof (zoneid_t)); 3070 if (zids == NULL) { 3071 zerror(zlogp, B_TRUE, "unable to allocate memory"); 3072 return; 3073 } 3074 nzents_saved = nzents; 3075 3076 if (zone_list(zids, &nzents) != 0) { 3077 free(zids); 3078 return; 3079 } 3080 if (nzents != nzents_saved) { 3081 /* list changed, try again */ 3082 free(zids); 3083 goto again; 3084 } 3085 3086 for (i = 0; i < nzents; i++) { 3087 char zid_name[ZONENAME_MAX]; 3088 zone_state_t zid_state; 3089 char zid_rpath[MAXPATHLEN]; 3090 3091 if (zids[i] == GLOBAL_ZONEID) 3092 continue; 3093 3094 if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1) 3095 continue; 3096 3097 /* 3098 * Skip the zone we are halting 3099 */ 3100 if (strcmp(zid_name, zone_name) == 0) 3101 continue; 3102 3103 if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state, 3104 sizeof (zid_state)) < 0) || 3105 (zid_state < ZONE_IS_READY)) 3106 /* Skip over zones without mounted filesystems */ 3107 continue; 3108 3109 if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label, 3110 sizeof (m_label_t)) < 0) 3111 /* Skip over zones with unspecified label */ 3112 continue; 3113 3114 if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath, 3115 sizeof (zid_rpath)) == -1) 3116 /* Skip over zones with bad path */ 3117 continue; 3118 3119 if (zlabel != NULL && bldominates(zid_label, zlabel)) { 3120 /* 3121 * This zone dominates our zone. 3122 * Unmount the lofs mount of our zone's /export/home 3123 */ 3124 3125 if (snprintf(path, MAXPATHLEN, 3126 "%s/zone/%s/export/home", zid_rpath, 3127 zone_name) > MAXPATHLEN) 3128 continue; 3129 3130 /* Skip over mount failures */ 3131 (void) umount(path); 3132 } 3133 } 3134 free(zids); 3135 3136 /* 3137 * Unmount global zone autofs trigger for this zone 3138 */ 3139 (void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name); 3140 /* Skip over mount failures */ 3141 (void) umount(path); 3142 3143 /* 3144 * Next unshare any exported directories from this zone. 3145 */ 3146 3147 argv[0] = "zoneunshare"; 3148 argv[1] = "-z"; 3149 argv[2] = zone_name; 3150 argv[3] = NULL; 3151 3152 (void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv); 3153 /* Don't check for errors since they don't affect the zone */ 3154 3155 /* 3156 * Finally, deallocate any devices in the zone. 3157 */ 3158 3159 argv[0] = "deallocate"; 3160 argv[1] = "-Isz"; 3161 argv[2] = zone_name; 3162 argv[3] = NULL; 3163 3164 (void) forkexec(zlogp, "/usr/sbin/deallocate", argv); 3165 /* Don't check for errors since they don't affect the zone */ 3166 } 3167 3168 /* 3169 * Fetch the Trusted Extensions label and multi-level ports (MLPs) for 3170 * this zone. 3171 */ 3172 static tsol_zcent_t * 3173 get_zone_label(zlog_t *zlogp, priv_set_t *privs) 3174 { 3175 FILE *fp; 3176 tsol_zcent_t *zcent = NULL; 3177 char line[MAXTNZLEN]; 3178 3179 if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) { 3180 zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH); 3181 return (NULL); 3182 } 3183 3184 while (fgets(line, sizeof (line), fp) != NULL) { 3185 /* 3186 * Check for malformed database 3187 */ 3188 if (strlen(line) == MAXTNZLEN - 1) 3189 break; 3190 if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL) 3191 continue; 3192 if (strcmp(zcent->zc_name, zone_name) == 0) 3193 break; 3194 tsol_freezcent(zcent); 3195 zcent = NULL; 3196 } 3197 (void) fclose(fp); 3198 3199 if (zcent == NULL) { 3200 zerror(zlogp, B_FALSE, "zone requires a label assignment. " 3201 "See tnzonecfg(4)"); 3202 } else { 3203 if (zlabel == NULL) 3204 zlabel = m_label_alloc(MAC_LABEL); 3205 /* 3206 * Save this zone's privileges for later read-down processing 3207 */ 3208 if ((zprivs = priv_allocset()) == NULL) { 3209 zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 3210 return (NULL); 3211 } else { 3212 priv_copyset(privs, zprivs); 3213 } 3214 } 3215 return (zcent); 3216 } 3217 3218 /* 3219 * Add the Trusted Extensions multi-level ports for this zone. 3220 */ 3221 static void 3222 set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent) 3223 { 3224 tsol_mlp_t *mlp; 3225 tsol_mlpent_t tsme; 3226 3227 if (!is_system_labeled()) 3228 return; 3229 3230 tsme.tsme_zoneid = zoneid; 3231 tsme.tsme_flags = 0; 3232 for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) { 3233 tsme.tsme_mlp = *mlp; 3234 if (tnmlp(TNDB_LOAD, &tsme) != 0) { 3235 zerror(zlogp, B_TRUE, "cannot set zone-specific MLP " 3236 "on %d-%d/%d", mlp->mlp_port, 3237 mlp->mlp_port_upper, mlp->mlp_ipp); 3238 } 3239 } 3240 3241 tsme.tsme_flags = TSOL_MEF_SHARED; 3242 for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) { 3243 tsme.tsme_mlp = *mlp; 3244 if (tnmlp(TNDB_LOAD, &tsme) != 0) { 3245 zerror(zlogp, B_TRUE, "cannot set shared MLP " 3246 "on %d-%d/%d", mlp->mlp_port, 3247 mlp->mlp_port_upper, mlp->mlp_ipp); 3248 } 3249 } 3250 } 3251 3252 static void 3253 remove_mlps(zlog_t *zlogp, zoneid_t zoneid) 3254 { 3255 tsol_mlpent_t tsme; 3256 3257 if (!is_system_labeled()) 3258 return; 3259 3260 (void) memset(&tsme, 0, sizeof (tsme)); 3261 tsme.tsme_zoneid = zoneid; 3262 if (tnmlp(TNDB_FLUSH, &tsme) != 0) 3263 zerror(zlogp, B_TRUE, "cannot flush MLPs"); 3264 } 3265 3266 int 3267 prtmount(const char *fs, void *x) { 3268 zerror((zlog_t *)x, B_FALSE, " %s", fs); 3269 return (0); 3270 } 3271 3272 /* 3273 * Look for zones running on the main system that are using this root (or any 3274 * subdirectory of it). Return B_TRUE and print an error if a conflicting zone 3275 * is found or if we can't tell. 3276 */ 3277 static boolean_t 3278 duplicate_zone_root(zlog_t *zlogp, const char *rootpath) 3279 { 3280 zoneid_t *zids = NULL; 3281 uint_t nzids = 0; 3282 boolean_t retv; 3283 int rlen, zlen; 3284 char zroot[MAXPATHLEN]; 3285 char zonename[ZONENAME_MAX]; 3286 3287 for (;;) { 3288 nzids += 10; 3289 zids = malloc(nzids * sizeof (*zids)); 3290 if (zids == NULL) { 3291 zerror(zlogp, B_TRUE, "unable to allocate memory"); 3292 return (B_TRUE); 3293 } 3294 if (zone_list(zids, &nzids) == 0) 3295 break; 3296 free(zids); 3297 } 3298 retv = B_FALSE; 3299 rlen = strlen(rootpath); 3300 while (nzids > 0) { 3301 /* 3302 * Ignore errors; they just mean that the zone has disappeared 3303 * while we were busy. 3304 */ 3305 if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot, 3306 sizeof (zroot)) == -1) 3307 continue; 3308 zlen = strlen(zroot); 3309 if (zlen > rlen) 3310 zlen = rlen; 3311 if (strncmp(rootpath, zroot, zlen) == 0 && 3312 (zroot[zlen] == '\0' || zroot[zlen] == '/') && 3313 (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) { 3314 if (getzonenamebyid(zids[nzids], zonename, 3315 sizeof (zonename)) == -1) 3316 (void) snprintf(zonename, sizeof (zonename), 3317 "id %d", (int)zids[nzids]); 3318 zerror(zlogp, B_FALSE, 3319 "zone root %s already in use by zone %s", 3320 rootpath, zonename); 3321 retv = B_TRUE; 3322 break; 3323 } 3324 } 3325 free(zids); 3326 return (retv); 3327 } 3328 3329 /* 3330 * Search for loopback mounts that use this same source node (same device and 3331 * inode). Return B_TRUE if there is one or if we can't tell. 3332 */ 3333 static boolean_t 3334 duplicate_reachable_path(zlog_t *zlogp, const char *rootpath) 3335 { 3336 struct stat64 rst, zst; 3337 struct mnttab *mnp; 3338 3339 if (stat64(rootpath, &rst) == -1) { 3340 zerror(zlogp, B_TRUE, "can't stat %s", rootpath); 3341 return (B_TRUE); 3342 } 3343 if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1) 3344 return (B_TRUE); 3345 for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) { 3346 if (mnp->mnt_fstype == NULL || 3347 strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0) 3348 continue; 3349 /* We're looking at a loopback mount. Stat it. */ 3350 if (mnp->mnt_special != NULL && 3351 stat64(mnp->mnt_special, &zst) != -1 && 3352 rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) { 3353 zerror(zlogp, B_FALSE, 3354 "zone root %s is reachable through %s", 3355 rootpath, mnp->mnt_mountp); 3356 return (B_TRUE); 3357 } 3358 } 3359 return (B_FALSE); 3360 } 3361 3362 zoneid_t 3363 vplat_create(zlog_t *zlogp, boolean_t mount_cmd) 3364 { 3365 zoneid_t rval = -1; 3366 priv_set_t *privs; 3367 char rootpath[MAXPATHLEN]; 3368 char *rctlbuf = NULL; 3369 size_t rctlbufsz = 0; 3370 char *zfsbuf = NULL; 3371 size_t zfsbufsz = 0; 3372 zoneid_t zoneid = -1; 3373 int xerr; 3374 char *kzone; 3375 FILE *fp = NULL; 3376 tsol_zcent_t *zcent = NULL; 3377 int match = 0; 3378 int doi = 0; 3379 3380 if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) { 3381 zerror(zlogp, B_TRUE, "unable to determine zone root"); 3382 return (-1); 3383 } 3384 if (zonecfg_in_alt_root()) 3385 resolve_lofs(zlogp, rootpath, sizeof (rootpath)); 3386 3387 if ((privs = priv_allocset()) == NULL) { 3388 zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 3389 return (-1); 3390 } 3391 priv_emptyset(privs); 3392 if (get_privset(zlogp, privs, mount_cmd) != 0) 3393 goto error; 3394 3395 if (!mount_cmd && get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) { 3396 zerror(zlogp, B_FALSE, "Unable to get list of rctls"); 3397 goto error; 3398 } 3399 3400 if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) { 3401 zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets"); 3402 goto error; 3403 } 3404 3405 if (is_system_labeled()) { 3406 zcent = get_zone_label(zlogp, privs); 3407 if (zcent) { 3408 match = zcent->zc_match; 3409 doi = zcent->zc_doi; 3410 *zlabel = zcent->zc_label; 3411 } else { 3412 goto error; 3413 } 3414 } 3415 3416 kzone = zone_name; 3417 3418 /* 3419 * We must do this scan twice. First, we look for zones running on the 3420 * main system that are using this root (or any subdirectory of it). 3421 * Next, we reduce to the shortest path and search for loopback mounts 3422 * that use this same source node (same device and inode). 3423 */ 3424 if (duplicate_zone_root(zlogp, rootpath)) 3425 goto error; 3426 if (duplicate_reachable_path(zlogp, rootpath)) 3427 goto error; 3428 3429 if (mount_cmd) { 3430 root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE); 3431 3432 /* 3433 * Forge up a special root for this zone. When a zone is 3434 * mounted, we can't let the zone have its own root because the 3435 * tools that will be used in this "scratch zone" need access 3436 * to both the zone's resources and the running machine's 3437 * executables. 3438 * 3439 * Note that the mkdir here also catches read-only filesystems. 3440 */ 3441 if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) { 3442 zerror(zlogp, B_TRUE, "cannot create %s", rootpath); 3443 goto error; 3444 } 3445 if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0) 3446 goto error; 3447 } 3448 3449 if (zonecfg_in_alt_root()) { 3450 /* 3451 * If we are mounting up a zone in an alternate root partition, 3452 * then we have some additional work to do before starting the 3453 * zone. First, resolve the root path down so that we're not 3454 * fooled by duplicates. Then forge up an internal name for 3455 * the zone. 3456 */ 3457 if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) { 3458 zerror(zlogp, B_TRUE, "cannot open mapfile"); 3459 goto error; 3460 } 3461 if (zonecfg_lock_scratch(fp) != 0) { 3462 zerror(zlogp, B_TRUE, "cannot lock mapfile"); 3463 goto error; 3464 } 3465 if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(), 3466 NULL, 0) == 0) { 3467 zerror(zlogp, B_FALSE, "scratch zone already running"); 3468 goto error; 3469 } 3470 /* This is the preferred name */ 3471 (void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s", 3472 zone_name); 3473 srandom(getpid()); 3474 while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL, 3475 0) == 0) { 3476 /* This is just an arbitrary name; note "." usage */ 3477 (void) snprintf(kernzone, sizeof (kernzone), 3478 "SUNWlu.%08lX%08lX", random(), random()); 3479 } 3480 kzone = kernzone; 3481 } 3482 3483 xerr = 0; 3484 if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf, 3485 rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel)) == -1) { 3486 if (xerr == ZE_AREMOUNTS) { 3487 if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) { 3488 zerror(zlogp, B_FALSE, 3489 "An unknown file-system is mounted on " 3490 "a subdirectory of %s", rootpath); 3491 } else { 3492 3493 zerror(zlogp, B_FALSE, 3494 "These file-systems are mounted on " 3495 "subdirectories of %s:", rootpath); 3496 (void) zonecfg_find_mounts(rootpath, 3497 prtmount, zlogp); 3498 } 3499 } else if (xerr == ZE_CHROOTED) { 3500 zerror(zlogp, B_FALSE, "%s: " 3501 "cannot create a zone from a chrooted " 3502 "environment", "zone_create"); 3503 } else { 3504 zerror(zlogp, B_TRUE, "%s failed", "zone_create"); 3505 } 3506 goto error; 3507 } 3508 3509 if (zonecfg_in_alt_root() && 3510 zonecfg_add_scratch(fp, zone_name, kernzone, 3511 zonecfg_get_root()) == -1) { 3512 zerror(zlogp, B_TRUE, "cannot add mapfile entry"); 3513 goto error; 3514 } 3515 3516 /* 3517 * The following is a warning, not an error, and is not performed when 3518 * merely mounting a zone for administrative use. 3519 */ 3520 if (!mount_cmd && bind_to_pool(zlogp, zoneid) != 0) 3521 zerror(zlogp, B_FALSE, "WARNING: unable to bind zone to " 3522 "requested pool; using default pool."); 3523 set_mlps(zlogp, zoneid, zcent); 3524 rval = zoneid; 3525 zoneid = -1; 3526 3527 error: 3528 if (zoneid != -1) 3529 (void) zone_destroy(zoneid); 3530 if (rctlbuf != NULL) 3531 free(rctlbuf); 3532 priv_freeset(privs); 3533 if (fp != NULL) 3534 zonecfg_close_scratch(fp); 3535 lofs_discard_mnttab(); 3536 if (zcent != NULL) 3537 tsol_freezcent(zcent); 3538 return (rval); 3539 } 3540 3541 int 3542 vplat_bringup(zlog_t *zlogp, boolean_t mount_cmd) 3543 { 3544 if (!mount_cmd && validate_datasets(zlogp) != 0) { 3545 lofs_discard_mnttab(); 3546 return (-1); 3547 } 3548 3549 if (create_dev_files(zlogp) != 0 || 3550 mount_filesystems(zlogp, mount_cmd) != 0) { 3551 lofs_discard_mnttab(); 3552 return (-1); 3553 } 3554 if (!mount_cmd && (devfsadm_register(zlogp) != 0 || 3555 configure_network_interfaces(zlogp) != 0)) { 3556 lofs_discard_mnttab(); 3557 return (-1); 3558 } 3559 lofs_discard_mnttab(); 3560 return (0); 3561 } 3562 3563 static int 3564 lu_root_teardown(zlog_t *zlogp) 3565 { 3566 char zroot[MAXPATHLEN]; 3567 3568 if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) { 3569 zerror(zlogp, B_FALSE, "unable to determine zone root"); 3570 return (-1); 3571 } 3572 root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE); 3573 3574 /* 3575 * At this point, the processes are gone, the filesystems (save the 3576 * root) are unmounted, and the zone is on death row. But there may 3577 * still be creds floating about in the system that reference the 3578 * zone_t, and which pin down zone_rootvp causing this call to fail 3579 * with EBUSY. Thus, we try for a little while before just giving up. 3580 * (How I wish this were not true, and umount2 just did the right 3581 * thing, or tmpfs supported MS_FORCE This is a gross hack.) 3582 */ 3583 if (umount2(zroot, MS_FORCE) != 0) { 3584 if (errno == ENOTSUP && umount2(zroot, 0) == 0) 3585 goto unmounted; 3586 if (errno == EBUSY) { 3587 int tries = 10; 3588 3589 while (--tries >= 0) { 3590 (void) sleep(1); 3591 if (umount2(zroot, 0) == 0) 3592 goto unmounted; 3593 if (errno != EBUSY) 3594 break; 3595 } 3596 } 3597 zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot); 3598 return (-1); 3599 } 3600 unmounted: 3601 3602 /* 3603 * Only zones in an alternate root environment have scratch zone 3604 * entries. 3605 */ 3606 if (zonecfg_in_alt_root()) { 3607 FILE *fp; 3608 int retv; 3609 3610 if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 3611 zerror(zlogp, B_TRUE, "cannot open mapfile"); 3612 return (-1); 3613 } 3614 retv = -1; 3615 if (zonecfg_lock_scratch(fp) != 0) 3616 zerror(zlogp, B_TRUE, "cannot lock mapfile"); 3617 else if (zonecfg_delete_scratch(fp, kernzone) != 0) 3618 zerror(zlogp, B_TRUE, "cannot delete map entry"); 3619 else 3620 retv = 0; 3621 zonecfg_close_scratch(fp); 3622 return (retv); 3623 } else { 3624 return (0); 3625 } 3626 } 3627 3628 int 3629 vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd) 3630 { 3631 char *kzone; 3632 zoneid_t zoneid; 3633 3634 kzone = zone_name; 3635 if (zonecfg_in_alt_root()) { 3636 FILE *fp; 3637 3638 if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 3639 zerror(zlogp, B_TRUE, "unable to open map file"); 3640 goto error; 3641 } 3642 if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(), 3643 kernzone, sizeof (kernzone)) != 0) { 3644 zerror(zlogp, B_FALSE, "unable to find scratch zone"); 3645 zonecfg_close_scratch(fp); 3646 goto error; 3647 } 3648 zonecfg_close_scratch(fp); 3649 kzone = kernzone; 3650 } 3651 3652 if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) { 3653 if (!bringup_failure_recovery) 3654 zerror(zlogp, B_TRUE, "unable to get zoneid"); 3655 if (unmount_cmd) 3656 (void) lu_root_teardown(zlogp); 3657 goto error; 3658 } 3659 3660 if (zone_shutdown(zoneid) != 0) { 3661 zerror(zlogp, B_TRUE, "unable to shutdown zone"); 3662 goto error; 3663 } 3664 3665 if (!unmount_cmd && devfsadm_unregister(zlogp) != 0) 3666 goto error; 3667 3668 if (!unmount_cmd && 3669 unconfigure_network_interfaces(zlogp, zoneid) != 0) { 3670 zerror(zlogp, B_FALSE, 3671 "unable to unconfigure network interfaces in zone"); 3672 goto error; 3673 } 3674 3675 if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) { 3676 zerror(zlogp, B_TRUE, "unable to abort TCP connections"); 3677 goto error; 3678 } 3679 3680 if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) { 3681 zerror(zlogp, B_FALSE, 3682 "unable to unmount file systems in zone"); 3683 goto error; 3684 } 3685 3686 remove_mlps(zlogp, zoneid); 3687 3688 if (zone_destroy(zoneid) != 0) { 3689 zerror(zlogp, B_TRUE, "unable to destroy zone"); 3690 goto error; 3691 } 3692 3693 /* 3694 * Special teardown for alternate boot environments: remove the tmpfs 3695 * root for the zone and then remove it from the map file. 3696 */ 3697 if (unmount_cmd && lu_root_teardown(zlogp) != 0) 3698 goto error; 3699 3700 if (!unmount_cmd) 3701 destroy_console_slave(); 3702 3703 lofs_discard_mnttab(); 3704 return (0); 3705 3706 error: 3707 lofs_discard_mnttab(); 3708 return (-1); 3709 } 3710