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