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