1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * This module contains functions used to bring up and tear down the 31 * Virtual Platform: [un]mounting file-systems, [un]plumbing network 32 * interfaces, [un]configuring devices, establishing resource controls, 33 * and creating/destroying the zone in the kernel. These actions, on 34 * the way up, ready the zone; on the way down, they halt the zone. 35 * See the much longer block comment at the beginning of zoneadmd.c 36 * for a bigger picture of how the whole program functions. 37 * 38 * This module also has primary responsibility for the layout of "scratch 39 * zones." These are mounted, but inactive, zones that are used during 40 * operating system upgrade and potentially other administrative action. The 41 * scratch zone environment is similar to the miniroot environment. The zone's 42 * actual root is mounted read-write on /a, and the standard paths (/usr, 43 * /sbin, /lib) all lead to read-only copies of the running system's binaries. 44 * This allows the administrative tools to manipulate the zone using "-R /a" 45 * without relying on any binaries in the zone itself. 46 * 47 * If the scratch zone is on an alternate root (Live Upgrade [LU] boot 48 * environment), then we must resolve the lofs mounts used there to uncover 49 * writable (unshared) resources. Shared resources, though, are always 50 * read-only. In addition, if the "same" zone with a different root path is 51 * currently running, then "/b" inside the zone points to the running zone's 52 * root. This allows LU to synchronize configuration files during the upgrade 53 * process. 54 * 55 * To construct this environment, this module creates a tmpfs mount on 56 * $ZONEPATH/lu. Inside this scratch area, the miniroot-like environment as 57 * described above is constructed on the fly. The zone is then created using 58 * $ZONEPATH/lu as the root. 59 * 60 * Note that scratch zones are inactive. The zone's bits are not running and 61 * likely cannot be run correctly until upgrade is done. Init is not running 62 * there, nor is SMF. Because of this, the "mounted" state of a scratch zone 63 * is not a part of the usual halt/ready/boot state machine. 64 */ 65 66 #include <sys/param.h> 67 #include <sys/mount.h> 68 #include <sys/mntent.h> 69 #include <sys/socket.h> 70 #include <sys/utsname.h> 71 #include <sys/types.h> 72 #include <sys/stat.h> 73 #include <sys/sockio.h> 74 #include <sys/stropts.h> 75 #include <sys/conf.h> 76 77 #include <inet/tcp.h> 78 #include <arpa/inet.h> 79 #include <netinet/in.h> 80 #include <net/route.h> 81 82 #include <stdio.h> 83 #include <errno.h> 84 #include <fcntl.h> 85 #include <unistd.h> 86 #include <rctl.h> 87 #include <stdlib.h> 88 #include <string.h> 89 #include <strings.h> 90 #include <wait.h> 91 #include <limits.h> 92 #include <libgen.h> 93 #include <libzfs.h> 94 #include <libdevinfo.h> 95 #include <zone.h> 96 #include <assert.h> 97 #include <libcontract.h> 98 #include <libcontract_priv.h> 99 #include <uuid/uuid.h> 100 101 #include <sys/mntio.h> 102 #include <sys/mnttab.h> 103 #include <sys/fs/autofs.h> /* for _autofssys() */ 104 #include <sys/fs/lofs_info.h> 105 #include <sys/fs/zfs.h> 106 107 #include <pool.h> 108 #include <sys/pool.h> 109 #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 zerror(zlogp, B_TRUE, 1772 "%s: could not determine zone interface belongs to", 1773 lifrl.lifr_name); 1774 ret_code = -1; 1775 continue; 1776 } 1777 if (lifrl.lifr_zoneid == zone_id) { 1778 if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) { 1779 zerror(zlogp, B_TRUE, 1780 "%s: could not remove interface", 1781 lifrl.lifr_name); 1782 ret_code = -1; 1783 continue; 1784 } 1785 } 1786 } 1787 bad: 1788 if (s > 0) 1789 (void) close(s); 1790 if (buf) 1791 free(buf); 1792 return (ret_code); 1793 } 1794 1795 static union sockunion { 1796 struct sockaddr sa; 1797 struct sockaddr_in sin; 1798 struct sockaddr_dl sdl; 1799 struct sockaddr_in6 sin6; 1800 } so_dst, so_ifp; 1801 1802 static struct { 1803 struct rt_msghdr hdr; 1804 char space[512]; 1805 } rtmsg; 1806 1807 static int 1808 salen(struct sockaddr *sa) 1809 { 1810 switch (sa->sa_family) { 1811 case AF_INET: 1812 return (sizeof (struct sockaddr_in)); 1813 case AF_LINK: 1814 return (sizeof (struct sockaddr_dl)); 1815 case AF_INET6: 1816 return (sizeof (struct sockaddr_in6)); 1817 default: 1818 return (sizeof (struct sockaddr)); 1819 } 1820 } 1821 1822 #define ROUNDUP_LONG(a) \ 1823 ((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long)) 1824 1825 /* 1826 * Look up which zone is using a given IP address. The address in question 1827 * is expected to have been stuffed into the structure to which lifr points 1828 * via a previous SIOCGLIFADDR ioctl(). 1829 * 1830 * This is done using black router socket magic. 1831 * 1832 * Return the name of the zone on success or NULL on failure. 1833 * 1834 * This is a lot of code for a simple task; a new ioctl request to take care 1835 * of this might be a useful RFE. 1836 */ 1837 1838 static char * 1839 who_is_using(zlog_t *zlogp, struct lifreq *lifr) 1840 { 1841 static char answer[ZONENAME_MAX]; 1842 pid_t pid; 1843 int s, rlen, l, i; 1844 char *cp = rtmsg.space; 1845 struct sockaddr_dl *ifp = NULL; 1846 struct sockaddr *sa; 1847 char save_if_name[LIFNAMSIZ]; 1848 1849 answer[0] = '\0'; 1850 1851 pid = getpid(); 1852 if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) { 1853 zerror(zlogp, B_TRUE, "could not get routing socket"); 1854 return (NULL); 1855 } 1856 1857 if (lifr->lifr_addr.ss_family == AF_INET) { 1858 struct sockaddr_in *sin4; 1859 1860 so_dst.sa.sa_family = AF_INET; 1861 sin4 = (struct sockaddr_in *)&lifr->lifr_addr; 1862 so_dst.sin.sin_addr = sin4->sin_addr; 1863 } else { 1864 struct sockaddr_in6 *sin6; 1865 1866 so_dst.sa.sa_family = AF_INET6; 1867 sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr; 1868 so_dst.sin6.sin6_addr = sin6->sin6_addr; 1869 } 1870 1871 so_ifp.sa.sa_family = AF_LINK; 1872 1873 (void) memset(&rtmsg, 0, sizeof (rtmsg)); 1874 rtmsg.hdr.rtm_type = RTM_GET; 1875 rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST; 1876 rtmsg.hdr.rtm_version = RTM_VERSION; 1877 rtmsg.hdr.rtm_seq = ++rts_seqno; 1878 rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST; 1879 1880 l = ROUNDUP_LONG(salen(&so_dst.sa)); 1881 (void) memmove(cp, &(so_dst), l); 1882 cp += l; 1883 l = ROUNDUP_LONG(salen(&so_ifp.sa)); 1884 (void) memmove(cp, &(so_ifp), l); 1885 cp += l; 1886 1887 rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg; 1888 1889 if ((rlen = write(s, &rtmsg, l)) < 0) { 1890 zerror(zlogp, B_TRUE, "writing to routing socket"); 1891 return (NULL); 1892 } else if (rlen < (int)rtmsg.hdr.rtm_msglen) { 1893 zerror(zlogp, B_TRUE, 1894 "write to routing socket got only %d for len\n", rlen); 1895 return (NULL); 1896 } 1897 do { 1898 l = read(s, &rtmsg, sizeof (rtmsg)); 1899 } while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno || 1900 rtmsg.hdr.rtm_pid != pid)); 1901 if (l < 0) { 1902 zerror(zlogp, B_TRUE, "reading from routing socket"); 1903 return (NULL); 1904 } 1905 1906 if (rtmsg.hdr.rtm_version != RTM_VERSION) { 1907 zerror(zlogp, B_FALSE, 1908 "routing message version %d not understood", 1909 rtmsg.hdr.rtm_version); 1910 return (NULL); 1911 } 1912 if (rtmsg.hdr.rtm_msglen != (ushort_t)l) { 1913 zerror(zlogp, B_FALSE, "message length mismatch, " 1914 "expected %d bytes, returned %d bytes", 1915 rtmsg.hdr.rtm_msglen, l); 1916 return (NULL); 1917 } 1918 if (rtmsg.hdr.rtm_errno != 0) { 1919 errno = rtmsg.hdr.rtm_errno; 1920 zerror(zlogp, B_TRUE, "RTM_GET routing socket message"); 1921 return (NULL); 1922 } 1923 if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) { 1924 zerror(zlogp, B_FALSE, "interface not found"); 1925 return (NULL); 1926 } 1927 cp = ((char *)(&rtmsg.hdr + 1)); 1928 for (i = 1; i != 0; i <<= 1) { 1929 /* LINTED E_BAD_PTR_CAST_ALIGN */ 1930 sa = (struct sockaddr *)cp; 1931 if (i != RTA_IFP) { 1932 if ((i & rtmsg.hdr.rtm_addrs) != 0) 1933 cp += ROUNDUP_LONG(salen(sa)); 1934 continue; 1935 } 1936 if (sa->sa_family == AF_LINK && 1937 ((struct sockaddr_dl *)sa)->sdl_nlen != 0) 1938 ifp = (struct sockaddr_dl *)sa; 1939 break; 1940 } 1941 if (ifp == NULL) { 1942 zerror(zlogp, B_FALSE, "interface could not be determined"); 1943 return (NULL); 1944 } 1945 1946 /* 1947 * We need to set the I/F name to what we got above, then do the 1948 * appropriate ioctl to get its zone name. But lifr->lifr_name is 1949 * used by the calling function to do a REMOVEIF, so if we leave the 1950 * "good" zone's I/F name in place, *that* I/F will be removed instead 1951 * of the bad one. So we save the old (bad) I/F name before over- 1952 * writing it and doing the ioctl, then restore it after the ioctl. 1953 */ 1954 (void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name)); 1955 (void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen); 1956 lifr->lifr_name[ifp->sdl_nlen] = '\0'; 1957 i = ioctl(s, SIOCGLIFZONE, lifr); 1958 (void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name)); 1959 if (i < 0) { 1960 zerror(zlogp, B_TRUE, 1961 "%s: could not determine the zone interface belongs to", 1962 lifr->lifr_name); 1963 return (NULL); 1964 } 1965 if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0) 1966 (void) snprintf(answer, sizeof (answer), "%d", 1967 lifr->lifr_zoneid); 1968 1969 if (strlen(answer) > 0) 1970 return (answer); 1971 return (NULL); 1972 } 1973 1974 typedef struct mcast_rtmsg_s { 1975 struct rt_msghdr m_rtm; 1976 union { 1977 struct { 1978 struct sockaddr_in m_dst; 1979 struct sockaddr_in m_gw; 1980 struct sockaddr_in m_netmask; 1981 } m_v4; 1982 struct { 1983 struct sockaddr_in6 m_dst; 1984 struct sockaddr_in6 m_gw; 1985 struct sockaddr_in6 m_netmask; 1986 } m_v6; 1987 } m_u; 1988 } mcast_rtmsg_t; 1989 #define m_dst4 m_u.m_v4.m_dst 1990 #define m_dst6 m_u.m_v6.m_dst 1991 #define m_gw4 m_u.m_v4.m_gw 1992 #define m_gw6 m_u.m_v6.m_gw 1993 #define m_netmask4 m_u.m_v4.m_netmask 1994 #define m_netmask6 m_u.m_v6.m_netmask 1995 1996 /* 1997 * Configures a single interface: a new virtual interface is added, based on 1998 * the physical interface nwiftabptr->zone_nwif_physical, with the address 1999 * specified in nwiftabptr->zone_nwif_address, for zone zone_id. Note that 2000 * the "address" can be an IPv6 address (with a /prefixlength required), an 2001 * IPv4 address (with a /prefixlength optional), or a name; for the latter, 2002 * an IPv4 name-to-address resolution will be attempted. 2003 * 2004 * A default interface route for multicast is created on the first IPv4 and 2005 * IPv6 interfaces (that have the IFF_MULTICAST flag set), respectively. 2006 * This should really be done in the init scripts if we ever allow zones to 2007 * modify the routing tables. 2008 * 2009 * If anything goes wrong, we log an detailed error message, attempt to tear 2010 * down whatever we set up and return an error. 2011 */ 2012 static int 2013 configure_one_interface(zlog_t *zlogp, zoneid_t zone_id, 2014 struct zone_nwiftab *nwiftabptr, boolean_t *mcast_rt_v4_setp, 2015 boolean_t *mcast_rt_v6_setp) 2016 { 2017 struct lifreq lifr; 2018 struct sockaddr_in netmask4; 2019 struct sockaddr_in6 netmask6; 2020 struct in_addr in4; 2021 struct in6_addr in6; 2022 sa_family_t af; 2023 char *slashp = strchr(nwiftabptr->zone_nwif_address, '/'); 2024 mcast_rtmsg_t mcast_rtmsg; 2025 int s; 2026 int rs; 2027 int rlen; 2028 boolean_t got_netmask = B_FALSE; 2029 char addrstr4[INET_ADDRSTRLEN]; 2030 int res; 2031 2032 res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr); 2033 if (res != Z_OK) { 2034 zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res), 2035 nwiftabptr->zone_nwif_address); 2036 return (-1); 2037 } 2038 af = lifr.lifr_addr.ss_family; 2039 if (af == AF_INET) 2040 in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr; 2041 else 2042 in6 = ((struct sockaddr_in6 *)(&lifr.lifr_addr))->sin6_addr; 2043 2044 if ((s = socket(af, SOCK_DGRAM, 0)) < 0) { 2045 zerror(zlogp, B_TRUE, "could not get socket"); 2046 return (-1); 2047 } 2048 2049 (void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical, 2050 sizeof (lifr.lifr_name)); 2051 if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) { 2052 /* 2053 * Here, we know that the interface can't be brought up. 2054 * A similar warning message was already printed out to 2055 * the console by zoneadm(1M) so instead we log the 2056 * message to syslog and continue. 2057 */ 2058 zerror(&logsys, B_TRUE, "WARNING: skipping interface " 2059 "'%s' which may not be present/plumbed in the " 2060 "global zone.", lifr.lifr_name); 2061 (void) close(s); 2062 return (Z_OK); 2063 } 2064 2065 if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) { 2066 zerror(zlogp, B_TRUE, 2067 "%s: could not set IP address to %s", 2068 lifr.lifr_name, nwiftabptr->zone_nwif_address); 2069 goto bad; 2070 } 2071 2072 /* Preserve literal IPv4 address for later potential printing. */ 2073 if (af == AF_INET) 2074 (void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN); 2075 2076 lifr.lifr_zoneid = zone_id; 2077 if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) { 2078 zerror(zlogp, B_TRUE, "%s: could not place interface into zone", 2079 lifr.lifr_name); 2080 goto bad; 2081 } 2082 2083 if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) { 2084 got_netmask = B_TRUE; /* default setting will be correct */ 2085 } else { 2086 if (af == AF_INET) { 2087 /* 2088 * The IPv4 netmask can be determined either 2089 * directly if a prefix length was supplied with 2090 * the address or via the netmasks database. Not 2091 * being able to determine it is a common failure, 2092 * but it often is not fatal to operation of the 2093 * interface. In that case, a warning will be 2094 * printed after the rest of the interface's 2095 * parameters have been configured. 2096 */ 2097 (void) memset(&netmask4, 0, sizeof (netmask4)); 2098 if (slashp != NULL) { 2099 if (addr2netmask(slashp + 1, V4_ADDR_LEN, 2100 (uchar_t *)&netmask4.sin_addr) != 0) { 2101 *slashp = '/'; 2102 zerror(zlogp, B_FALSE, 2103 "%s: invalid prefix length in %s", 2104 lifr.lifr_name, 2105 nwiftabptr->zone_nwif_address); 2106 goto bad; 2107 } 2108 got_netmask = B_TRUE; 2109 } else if (getnetmaskbyaddr(in4, 2110 &netmask4.sin_addr) == 0) { 2111 got_netmask = B_TRUE; 2112 } 2113 if (got_netmask) { 2114 netmask4.sin_family = af; 2115 (void) memcpy(&lifr.lifr_addr, &netmask4, 2116 sizeof (netmask4)); 2117 } 2118 } else { 2119 (void) memset(&netmask6, 0, sizeof (netmask6)); 2120 if (addr2netmask(slashp + 1, V6_ADDR_LEN, 2121 (uchar_t *)&netmask6.sin6_addr) != 0) { 2122 *slashp = '/'; 2123 zerror(zlogp, B_FALSE, 2124 "%s: invalid prefix length in %s", 2125 lifr.lifr_name, 2126 nwiftabptr->zone_nwif_address); 2127 goto bad; 2128 } 2129 got_netmask = B_TRUE; 2130 netmask6.sin6_family = af; 2131 (void) memcpy(&lifr.lifr_addr, &netmask6, 2132 sizeof (netmask6)); 2133 } 2134 if (got_netmask && 2135 ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) { 2136 zerror(zlogp, B_TRUE, "%s: could not set netmask", 2137 lifr.lifr_name); 2138 goto bad; 2139 } 2140 2141 /* 2142 * This doesn't set the broadcast address at all. Rather, it 2143 * gets, then sets the interface's address, relying on the fact 2144 * that resetting the address will reset the broadcast address. 2145 */ 2146 if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 2147 zerror(zlogp, B_TRUE, "%s: could not get address", 2148 lifr.lifr_name); 2149 goto bad; 2150 } 2151 if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) { 2152 zerror(zlogp, B_TRUE, 2153 "%s: could not reset broadcast address", 2154 lifr.lifr_name); 2155 goto bad; 2156 } 2157 } 2158 2159 if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 2160 zerror(zlogp, B_TRUE, "%s: could not get flags", 2161 lifr.lifr_name); 2162 goto bad; 2163 } 2164 lifr.lifr_flags |= IFF_UP; 2165 if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) { 2166 int save_errno = errno; 2167 char *zone_using; 2168 2169 /* 2170 * If we failed with something other than EADDRNOTAVAIL, 2171 * then skip to the end. Otherwise, look up our address, 2172 * then call a function to determine which zone is already 2173 * using that address. 2174 */ 2175 if (errno != EADDRNOTAVAIL) { 2176 zerror(zlogp, B_TRUE, 2177 "%s: could not bring interface up", lifr.lifr_name); 2178 goto bad; 2179 } 2180 if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 2181 zerror(zlogp, B_TRUE, "%s: could not get address", 2182 lifr.lifr_name); 2183 goto bad; 2184 } 2185 zone_using = who_is_using(zlogp, &lifr); 2186 errno = save_errno; 2187 if (zone_using == NULL) 2188 zerror(zlogp, B_TRUE, 2189 "%s: could not bring interface up", lifr.lifr_name); 2190 else 2191 zerror(zlogp, B_TRUE, "%s: could not bring interface " 2192 "up: address in use by zone '%s'", lifr.lifr_name, 2193 zone_using); 2194 goto bad; 2195 } 2196 if ((lifr.lifr_flags & IFF_MULTICAST) && ((af == AF_INET && 2197 mcast_rt_v4_setp != NULL && *mcast_rt_v4_setp == B_FALSE) || 2198 (af == AF_INET6 && 2199 mcast_rt_v6_setp != NULL && *mcast_rt_v6_setp == B_FALSE))) { 2200 rs = socket(PF_ROUTE, SOCK_RAW, 0); 2201 if (rs < 0) { 2202 zerror(zlogp, B_TRUE, "%s: could not create " 2203 "routing socket", lifr.lifr_name); 2204 goto bad; 2205 } 2206 (void) shutdown(rs, 0); 2207 (void) memset((void *)&mcast_rtmsg, 0, sizeof (mcast_rtmsg_t)); 2208 mcast_rtmsg.m_rtm.rtm_msglen = sizeof (struct rt_msghdr) + 2209 3 * (af == AF_INET ? sizeof (struct sockaddr_in) : 2210 sizeof (struct sockaddr_in6)); 2211 mcast_rtmsg.m_rtm.rtm_version = RTM_VERSION; 2212 mcast_rtmsg.m_rtm.rtm_type = RTM_ADD; 2213 mcast_rtmsg.m_rtm.rtm_flags = RTF_UP; 2214 mcast_rtmsg.m_rtm.rtm_addrs = 2215 RTA_DST | RTA_GATEWAY | RTA_NETMASK; 2216 mcast_rtmsg.m_rtm.rtm_seq = ++rts_seqno; 2217 if (af == AF_INET) { 2218 mcast_rtmsg.m_dst4.sin_family = AF_INET; 2219 mcast_rtmsg.m_dst4.sin_addr.s_addr = 2220 htonl(INADDR_UNSPEC_GROUP); 2221 mcast_rtmsg.m_gw4.sin_family = AF_INET; 2222 mcast_rtmsg.m_gw4.sin_addr = in4; 2223 mcast_rtmsg.m_netmask4.sin_family = AF_INET; 2224 mcast_rtmsg.m_netmask4.sin_addr.s_addr = 2225 htonl(IN_CLASSD_NET); 2226 } else { 2227 mcast_rtmsg.m_dst6.sin6_family = AF_INET6; 2228 mcast_rtmsg.m_dst6.sin6_addr.s6_addr[0] = 0xffU; 2229 mcast_rtmsg.m_gw6.sin6_family = AF_INET6; 2230 mcast_rtmsg.m_gw6.sin6_addr = in6; 2231 mcast_rtmsg.m_netmask6.sin6_family = AF_INET6; 2232 mcast_rtmsg.m_netmask6.sin6_addr.s6_addr[0] = 0xffU; 2233 } 2234 rlen = write(rs, (char *)&mcast_rtmsg, 2235 mcast_rtmsg.m_rtm.rtm_msglen); 2236 /* 2237 * The write to the multicast socket will fail if the 2238 * interface belongs to a failed IPMP group. This is a 2239 * non-fatal error and the zone will continue booting. 2240 * While the zone is running, if any interface in the 2241 * failed IPMP group recovers, the zone will fallback to 2242 * using that interface. 2243 */ 2244 if (rlen < mcast_rtmsg.m_rtm.rtm_msglen) { 2245 if (rlen < 0) { 2246 zerror(zlogp, B_TRUE, "WARNING: interface " 2247 "'%s' not available as default for " 2248 "multicast.", lifr.lifr_name); 2249 } else { 2250 zerror(zlogp, B_FALSE, "WARNING: interface " 2251 "'%s' not available as default for " 2252 "multicast; routing socket returned " 2253 "unexpected %d bytes.", 2254 lifr.lifr_name, rlen); 2255 } 2256 } else { 2257 2258 if (af == AF_INET) { 2259 *mcast_rt_v4_setp = B_TRUE; 2260 } else { 2261 *mcast_rt_v6_setp = B_TRUE; 2262 } 2263 } 2264 (void) close(rs); 2265 } 2266 2267 if (!got_netmask) { 2268 /* 2269 * A common, but often non-fatal problem, is that the system 2270 * cannot find the netmask for an interface address. This is 2271 * often caused by it being only in /etc/inet/netmasks, but 2272 * /etc/nsswitch.conf says to use NIS or NIS+ and it's not 2273 * in that. This doesn't show up at boot because the netmask 2274 * is obtained from /etc/inet/netmasks when no network 2275 * interfaces are up, but isn't consulted when NIS/NIS+ is 2276 * available. We warn the user here that something like this 2277 * has happened and we're just running with a default and 2278 * possible incorrect netmask. 2279 */ 2280 char buffer[INET6_ADDRSTRLEN]; 2281 void *addr; 2282 2283 if (af == AF_INET) 2284 addr = &((struct sockaddr_in *) 2285 (&lifr.lifr_addr))->sin_addr; 2286 else 2287 addr = &((struct sockaddr_in6 *) 2288 (&lifr.lifr_addr))->sin6_addr; 2289 2290 /* Find out what netmask interface is going to be using */ 2291 if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 || 2292 inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL) 2293 goto bad; 2294 zerror(zlogp, B_FALSE, 2295 "WARNING: %s: no matching subnet found in netmasks(4) for " 2296 "%s; using default of %s.", 2297 lifr.lifr_name, addrstr4, buffer); 2298 } 2299 2300 (void) close(s); 2301 return (Z_OK); 2302 bad: 2303 (void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr); 2304 (void) close(s); 2305 return (-1); 2306 } 2307 2308 /* 2309 * Sets up network interfaces based on information from the zone configuration. 2310 * An IPv4 loopback interface is set up "for free", modeling the global system. 2311 * If any of the configuration interfaces were IPv6, then an IPv6 loopback 2312 * address is set up as well. 2313 * 2314 * If anything goes wrong, we log a general error message, attempt to tear down 2315 * whatever we set up, and return an error. 2316 */ 2317 static int 2318 configure_network_interfaces(zlog_t *zlogp) 2319 { 2320 zone_dochandle_t handle; 2321 struct zone_nwiftab nwiftab, loopback_iftab; 2322 boolean_t saw_v6 = B_FALSE; 2323 boolean_t mcast_rt_v4_set = B_FALSE; 2324 boolean_t mcast_rt_v6_set = B_FALSE; 2325 zoneid_t zoneid; 2326 2327 if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) { 2328 zerror(zlogp, B_TRUE, "unable to get zoneid"); 2329 return (-1); 2330 } 2331 2332 if ((handle = zonecfg_init_handle()) == NULL) { 2333 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 2334 return (-1); 2335 } 2336 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 2337 zerror(zlogp, B_FALSE, "invalid configuration"); 2338 zonecfg_fini_handle(handle); 2339 return (-1); 2340 } 2341 if (zonecfg_setnwifent(handle) == Z_OK) { 2342 for (;;) { 2343 struct in6_addr in6; 2344 2345 if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK) 2346 break; 2347 if (configure_one_interface(zlogp, zoneid, 2348 &nwiftab, &mcast_rt_v4_set, &mcast_rt_v6_set) != 2349 Z_OK) { 2350 (void) zonecfg_endnwifent(handle); 2351 zonecfg_fini_handle(handle); 2352 return (-1); 2353 } 2354 if (inet_pton(AF_INET6, nwiftab.zone_nwif_address, 2355 &in6) == 1) 2356 saw_v6 = B_TRUE; 2357 } 2358 (void) zonecfg_endnwifent(handle); 2359 } 2360 zonecfg_fini_handle(handle); 2361 (void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0", 2362 sizeof (loopback_iftab.zone_nwif_physical)); 2363 (void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1", 2364 sizeof (loopback_iftab.zone_nwif_address)); 2365 if (configure_one_interface(zlogp, zoneid, &loopback_iftab, NULL, NULL) 2366 != Z_OK) { 2367 return (-1); 2368 } 2369 if (saw_v6) { 2370 (void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128", 2371 sizeof (loopback_iftab.zone_nwif_address)); 2372 if (configure_one_interface(zlogp, zoneid, 2373 &loopback_iftab, NULL, NULL) != Z_OK) { 2374 return (-1); 2375 } 2376 } 2377 return (0); 2378 } 2379 2380 static int 2381 tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid, 2382 const struct sockaddr_storage *local, const struct sockaddr_storage *remote) 2383 { 2384 int fd; 2385 struct strioctl ioc; 2386 tcp_ioc_abort_conn_t conn; 2387 int error; 2388 2389 conn.ac_local = *local; 2390 conn.ac_remote = *remote; 2391 conn.ac_start = TCPS_SYN_SENT; 2392 conn.ac_end = TCPS_TIME_WAIT; 2393 conn.ac_zoneid = zoneid; 2394 2395 ioc.ic_cmd = TCP_IOC_ABORT_CONN; 2396 ioc.ic_timout = -1; /* infinite timeout */ 2397 ioc.ic_len = sizeof (conn); 2398 ioc.ic_dp = (char *)&conn; 2399 2400 if ((fd = open("/dev/tcp", O_RDONLY)) < 0) { 2401 zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp"); 2402 return (-1); 2403 } 2404 2405 error = ioctl(fd, I_STR, &ioc); 2406 (void) close(fd); 2407 if (error == 0 || errno == ENOENT) /* ENOENT is not an error */ 2408 return (0); 2409 return (-1); 2410 } 2411 2412 static int 2413 tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid) 2414 { 2415 struct sockaddr_storage l, r; 2416 struct sockaddr_in *local, *remote; 2417 struct sockaddr_in6 *local6, *remote6; 2418 int error; 2419 2420 /* 2421 * Abort IPv4 connections. 2422 */ 2423 bzero(&l, sizeof (*local)); 2424 local = (struct sockaddr_in *)&l; 2425 local->sin_family = AF_INET; 2426 local->sin_addr.s_addr = INADDR_ANY; 2427 local->sin_port = 0; 2428 2429 bzero(&r, sizeof (*remote)); 2430 remote = (struct sockaddr_in *)&r; 2431 remote->sin_family = AF_INET; 2432 remote->sin_addr.s_addr = INADDR_ANY; 2433 remote->sin_port = 0; 2434 2435 if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0) 2436 return (error); 2437 2438 /* 2439 * Abort IPv6 connections. 2440 */ 2441 bzero(&l, sizeof (*local6)); 2442 local6 = (struct sockaddr_in6 *)&l; 2443 local6->sin6_family = AF_INET6; 2444 local6->sin6_port = 0; 2445 local6->sin6_addr = in6addr_any; 2446 2447 bzero(&r, sizeof (*remote6)); 2448 remote6 = (struct sockaddr_in6 *)&r; 2449 remote6->sin6_family = AF_INET6; 2450 remote6->sin6_port = 0; 2451 remote6->sin6_addr = in6addr_any; 2452 2453 if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0) 2454 return (error); 2455 return (0); 2456 } 2457 2458 static int 2459 get_privset(zlog_t *zlogp, priv_set_t *privs, boolean_t mount_cmd) 2460 { 2461 int error = -1; 2462 zone_dochandle_t handle; 2463 char *privname = NULL; 2464 2465 if ((handle = zonecfg_init_handle()) == NULL) { 2466 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 2467 return (-1); 2468 } 2469 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 2470 zerror(zlogp, B_FALSE, "invalid configuration"); 2471 zonecfg_fini_handle(handle); 2472 return (-1); 2473 } 2474 2475 if (mount_cmd) { 2476 if (zonecfg_default_privset(privs) == Z_OK) 2477 return (0); 2478 zerror(zlogp, B_FALSE, 2479 "failed to determine the zone's default privilege set"); 2480 zonecfg_fini_handle(handle); 2481 return (-1); 2482 } 2483 2484 switch (zonecfg_get_privset(handle, privs, &privname)) { 2485 case Z_OK: 2486 error = 0; 2487 break; 2488 case Z_PRIV_PROHIBITED: 2489 zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted " 2490 "within the zone's privilege set", privname); 2491 break; 2492 case Z_PRIV_REQUIRED: 2493 zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing " 2494 "from the zone's privilege set", privname); 2495 break; 2496 case Z_PRIV_UNKNOWN: 2497 zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified " 2498 "in the zone's privilege set", privname); 2499 break; 2500 default: 2501 zerror(zlogp, B_FALSE, "failed to determine the zone's " 2502 "privilege set"); 2503 break; 2504 } 2505 2506 free(privname); 2507 zonecfg_fini_handle(handle); 2508 return (error); 2509 } 2510 2511 static int 2512 get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep) 2513 { 2514 nvlist_t *nvl = NULL; 2515 char *nvl_packed = NULL; 2516 size_t nvl_size = 0; 2517 nvlist_t **nvlv = NULL; 2518 int rctlcount = 0; 2519 int error = -1; 2520 zone_dochandle_t handle; 2521 struct zone_rctltab rctltab; 2522 rctlblk_t *rctlblk = NULL; 2523 2524 *bufp = NULL; 2525 *bufsizep = 0; 2526 2527 if ((handle = zonecfg_init_handle()) == NULL) { 2528 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 2529 return (-1); 2530 } 2531 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 2532 zerror(zlogp, B_FALSE, "invalid configuration"); 2533 zonecfg_fini_handle(handle); 2534 return (-1); 2535 } 2536 2537 rctltab.zone_rctl_valptr = NULL; 2538 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 2539 zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc"); 2540 goto out; 2541 } 2542 2543 if (zonecfg_setrctlent(handle) != Z_OK) { 2544 zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent"); 2545 goto out; 2546 } 2547 2548 if ((rctlblk = malloc(rctlblk_size())) == NULL) { 2549 zerror(zlogp, B_TRUE, "memory allocation failed"); 2550 goto out; 2551 } 2552 while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) { 2553 struct zone_rctlvaltab *rctlval; 2554 uint_t i, count; 2555 const char *name = rctltab.zone_rctl_name; 2556 2557 /* zoneadm should have already warned about unknown rctls. */ 2558 if (!zonecfg_is_rctl(name)) { 2559 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 2560 rctltab.zone_rctl_valptr = NULL; 2561 continue; 2562 } 2563 count = 0; 2564 for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 2565 rctlval = rctlval->zone_rctlval_next) { 2566 count++; 2567 } 2568 if (count == 0) { /* ignore */ 2569 continue; /* Nothing to free */ 2570 } 2571 if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL) 2572 goto out; 2573 i = 0; 2574 for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 2575 rctlval = rctlval->zone_rctlval_next, i++) { 2576 if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) { 2577 zerror(zlogp, B_TRUE, "%s failed", 2578 "nvlist_alloc"); 2579 goto out; 2580 } 2581 if (zonecfg_construct_rctlblk(rctlval, rctlblk) 2582 != Z_OK) { 2583 zerror(zlogp, B_FALSE, "invalid rctl value: " 2584 "(priv=%s,limit=%s,action=%s)", 2585 rctlval->zone_rctlval_priv, 2586 rctlval->zone_rctlval_limit, 2587 rctlval->zone_rctlval_action); 2588 goto out; 2589 } 2590 if (!zonecfg_valid_rctl(name, rctlblk)) { 2591 zerror(zlogp, B_FALSE, 2592 "(priv=%s,limit=%s,action=%s) is not a " 2593 "valid value for rctl '%s'", 2594 rctlval->zone_rctlval_priv, 2595 rctlval->zone_rctlval_limit, 2596 rctlval->zone_rctlval_action, 2597 name); 2598 goto out; 2599 } 2600 if (nvlist_add_uint64(nvlv[i], "privilege", 2601 rctlblk_get_privilege(rctlblk)) != 0) { 2602 zerror(zlogp, B_FALSE, "%s failed", 2603 "nvlist_add_uint64"); 2604 goto out; 2605 } 2606 if (nvlist_add_uint64(nvlv[i], "limit", 2607 rctlblk_get_value(rctlblk)) != 0) { 2608 zerror(zlogp, B_FALSE, "%s failed", 2609 "nvlist_add_uint64"); 2610 goto out; 2611 } 2612 if (nvlist_add_uint64(nvlv[i], "action", 2613 (uint_t)rctlblk_get_local_action(rctlblk, NULL)) 2614 != 0) { 2615 zerror(zlogp, B_FALSE, "%s failed", 2616 "nvlist_add_uint64"); 2617 goto out; 2618 } 2619 } 2620 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 2621 rctltab.zone_rctl_valptr = NULL; 2622 if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count) 2623 != 0) { 2624 zerror(zlogp, B_FALSE, "%s failed", 2625 "nvlist_add_nvlist_array"); 2626 goto out; 2627 } 2628 for (i = 0; i < count; i++) 2629 nvlist_free(nvlv[i]); 2630 free(nvlv); 2631 nvlv = NULL; 2632 rctlcount++; 2633 } 2634 (void) zonecfg_endrctlent(handle); 2635 2636 if (rctlcount == 0) { 2637 error = 0; 2638 goto out; 2639 } 2640 if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0) 2641 != 0) { 2642 zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack"); 2643 goto out; 2644 } 2645 2646 error = 0; 2647 *bufp = nvl_packed; 2648 *bufsizep = nvl_size; 2649 2650 out: 2651 free(rctlblk); 2652 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 2653 if (error && nvl_packed != NULL) 2654 free(nvl_packed); 2655 if (nvl != NULL) 2656 nvlist_free(nvl); 2657 if (nvlv != NULL) 2658 free(nvlv); 2659 if (handle != NULL) 2660 zonecfg_fini_handle(handle); 2661 return (error); 2662 } 2663 2664 static int 2665 get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep) 2666 { 2667 zone_dochandle_t handle; 2668 struct zone_dstab dstab; 2669 size_t total, offset, len; 2670 int error = -1; 2671 char *str; 2672 2673 *bufp = NULL; 2674 *bufsizep = 0; 2675 2676 if ((handle = zonecfg_init_handle()) == NULL) { 2677 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 2678 return (-1); 2679 } 2680 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 2681 zerror(zlogp, B_FALSE, "invalid configuration"); 2682 zonecfg_fini_handle(handle); 2683 return (-1); 2684 } 2685 2686 if (zonecfg_setdsent(handle) != Z_OK) { 2687 zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent"); 2688 goto out; 2689 } 2690 2691 total = 0; 2692 while (zonecfg_getdsent(handle, &dstab) == Z_OK) 2693 total += strlen(dstab.zone_dataset_name) + 1; 2694 (void) zonecfg_enddsent(handle); 2695 2696 if (total == 0) { 2697 error = 0; 2698 goto out; 2699 } 2700 2701 if ((str = malloc(total)) == NULL) { 2702 zerror(zlogp, B_TRUE, "memory allocation failed"); 2703 goto out; 2704 } 2705 2706 if (zonecfg_setdsent(handle) != Z_OK) { 2707 zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent"); 2708 goto out; 2709 } 2710 offset = 0; 2711 while (zonecfg_getdsent(handle, &dstab) == Z_OK) { 2712 len = strlen(dstab.zone_dataset_name); 2713 (void) strlcpy(str + offset, dstab.zone_dataset_name, 2714 sizeof (dstab.zone_dataset_name) - offset); 2715 offset += len; 2716 if (offset != total - 1) 2717 str[offset++] = ','; 2718 } 2719 (void) zonecfg_enddsent(handle); 2720 2721 error = 0; 2722 *bufp = str; 2723 *bufsizep = total; 2724 2725 out: 2726 if (error != 0 && str != NULL) 2727 free(str); 2728 if (handle != NULL) 2729 zonecfg_fini_handle(handle); 2730 2731 return (error); 2732 } 2733 2734 static int 2735 validate_datasets(zlog_t *zlogp) 2736 { 2737 zone_dochandle_t handle; 2738 struct zone_dstab dstab; 2739 zfs_handle_t *zhp; 2740 libzfs_handle_t *hdl; 2741 2742 if ((handle = zonecfg_init_handle()) == NULL) { 2743 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 2744 return (-1); 2745 } 2746 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 2747 zerror(zlogp, B_FALSE, "invalid configuration"); 2748 zonecfg_fini_handle(handle); 2749 return (-1); 2750 } 2751 2752 if (zonecfg_setdsent(handle) != Z_OK) { 2753 zerror(zlogp, B_FALSE, "invalid configuration"); 2754 zonecfg_fini_handle(handle); 2755 return (-1); 2756 } 2757 2758 if ((hdl = libzfs_init()) == NULL) { 2759 zerror(zlogp, B_FALSE, "opening ZFS library"); 2760 zonecfg_fini_handle(handle); 2761 return (-1); 2762 } 2763 2764 while (zonecfg_getdsent(handle, &dstab) == Z_OK) { 2765 2766 if ((zhp = zfs_open(hdl, dstab.zone_dataset_name, 2767 ZFS_TYPE_FILESYSTEM)) == NULL) { 2768 zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'", 2769 dstab.zone_dataset_name); 2770 zonecfg_fini_handle(handle); 2771 libzfs_fini(hdl); 2772 return (-1); 2773 } 2774 2775 /* 2776 * Automatically set the 'zoned' property. We check the value 2777 * first because we'll get EPERM if it is already set. 2778 */ 2779 if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) && 2780 zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_ZONED), 2781 "on") != 0) { 2782 zerror(zlogp, B_FALSE, "cannot set 'zoned' " 2783 "property for ZFS dataset '%s'\n", 2784 dstab.zone_dataset_name); 2785 zonecfg_fini_handle(handle); 2786 zfs_close(zhp); 2787 libzfs_fini(hdl); 2788 return (-1); 2789 } 2790 2791 zfs_close(zhp); 2792 } 2793 (void) zonecfg_enddsent(handle); 2794 2795 zonecfg_fini_handle(handle); 2796 libzfs_fini(hdl); 2797 2798 return (0); 2799 } 2800 2801 /* 2802 * Mount lower level home directories into/from current zone 2803 * Share exported directories specified in dfstab for zone 2804 */ 2805 static int 2806 tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath) 2807 { 2808 zoneid_t *zids = NULL; 2809 priv_set_t *zid_privs; 2810 const priv_impl_info_t *ip = NULL; 2811 uint_t nzents_saved; 2812 uint_t nzents; 2813 int i; 2814 char readonly[] = "ro"; 2815 struct zone_fstab lower_fstab; 2816 char *argv[4]; 2817 2818 if (!is_system_labeled()) 2819 return (0); 2820 2821 if (zid_label == NULL) { 2822 zid_label = m_label_alloc(MAC_LABEL); 2823 if (zid_label == NULL) 2824 return (-1); 2825 } 2826 2827 /* Make sure our zone has an /export/home dir */ 2828 (void) make_one_dir(zlogp, rootpath, "/export/home", 2829 DEFAULT_DIR_MODE); 2830 2831 lower_fstab.zone_fs_raw[0] = '\0'; 2832 (void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS, 2833 sizeof (lower_fstab.zone_fs_type)); 2834 lower_fstab.zone_fs_options = NULL; 2835 (void) zonecfg_add_fs_option(&lower_fstab, readonly); 2836 2837 /* 2838 * Get the list of zones from the kernel 2839 */ 2840 if (zone_list(NULL, &nzents) != 0) { 2841 zerror(zlogp, B_TRUE, "unable to list zones"); 2842 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 2843 return (-1); 2844 } 2845 again: 2846 if (nzents == 0) { 2847 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 2848 return (-1); 2849 } 2850 2851 zids = malloc(nzents * sizeof (zoneid_t)); 2852 if (zids == NULL) { 2853 zerror(zlogp, B_TRUE, "memory allocation failed"); 2854 return (-1); 2855 } 2856 nzents_saved = nzents; 2857 2858 if (zone_list(zids, &nzents) != 0) { 2859 zerror(zlogp, B_TRUE, "unable to list zones"); 2860 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 2861 free(zids); 2862 return (-1); 2863 } 2864 if (nzents != nzents_saved) { 2865 /* list changed, try again */ 2866 free(zids); 2867 goto again; 2868 } 2869 2870 ip = getprivimplinfo(); 2871 if ((zid_privs = priv_allocset()) == NULL) { 2872 zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 2873 zonecfg_free_fs_option_list( 2874 lower_fstab.zone_fs_options); 2875 free(zids); 2876 return (-1); 2877 } 2878 2879 for (i = 0; i < nzents; i++) { 2880 char zid_name[ZONENAME_MAX]; 2881 zone_state_t zid_state; 2882 char zid_rpath[MAXPATHLEN]; 2883 struct stat stat_buf; 2884 2885 if (zids[i] == GLOBAL_ZONEID) 2886 continue; 2887 2888 if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1) 2889 continue; 2890 2891 /* 2892 * Do special setup for the zone we are booting 2893 */ 2894 if (strcmp(zid_name, zone_name) == 0) { 2895 struct zone_fstab autofs_fstab; 2896 char map_path[MAXPATHLEN]; 2897 int fd; 2898 2899 /* 2900 * Create auto_home_<zone> map for this zone 2901 * in the global zone. The non-global zone entry 2902 * will be created by automount when the zone 2903 * is booted. 2904 */ 2905 2906 (void) snprintf(autofs_fstab.zone_fs_special, 2907 MAXPATHLEN, "auto_home_%s", zid_name); 2908 2909 (void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN, 2910 "/zone/%s/home", zid_name); 2911 2912 (void) snprintf(map_path, sizeof (map_path), 2913 "/etc/%s", autofs_fstab.zone_fs_special); 2914 /* 2915 * If the map file doesn't exist create a template 2916 */ 2917 if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL, 2918 S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) { 2919 int len; 2920 char map_rec[MAXPATHLEN]; 2921 2922 len = snprintf(map_rec, sizeof (map_rec), 2923 "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n", 2924 autofs_fstab.zone_fs_special, rootpath); 2925 (void) write(fd, map_rec, len); 2926 (void) close(fd); 2927 } 2928 2929 /* 2930 * Mount auto_home_<zone> in the global zone if absent. 2931 * If it's already of type autofs, then 2932 * don't mount it again. 2933 */ 2934 if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) || 2935 strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) { 2936 char optstr[] = "indirect,ignore,nobrowse"; 2937 2938 (void) make_one_dir(zlogp, "", 2939 autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE); 2940 2941 /* 2942 * Mount will fail if automounter has already 2943 * processed the auto_home_<zonename> map 2944 */ 2945 (void) domount(zlogp, MNTTYPE_AUTOFS, optstr, 2946 autofs_fstab.zone_fs_special, 2947 autofs_fstab.zone_fs_dir); 2948 } 2949 continue; 2950 } 2951 2952 2953 if (zone_get_state(zid_name, &zid_state) != Z_OK || 2954 (zid_state != ZONE_STATE_READY && 2955 zid_state != ZONE_STATE_RUNNING)) 2956 /* Skip over zones without mounted filesystems */ 2957 continue; 2958 2959 if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label, 2960 sizeof (m_label_t)) < 0) 2961 /* Skip over zones with unspecified label */ 2962 continue; 2963 2964 if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath, 2965 sizeof (zid_rpath)) == -1) 2966 /* Skip over zones with bad path */ 2967 continue; 2968 2969 if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs, 2970 sizeof (priv_chunk_t) * ip->priv_setsize) == -1) 2971 /* Skip over zones with bad privs */ 2972 continue; 2973 2974 /* 2975 * Reading down is valid according to our label model 2976 * but some customers want to disable it because it 2977 * allows execute down and other possible attacks. 2978 * Therefore, we restrict this feature to zones that 2979 * have the NET_MAC_AWARE privilege which is required 2980 * for NFS read-down semantics. 2981 */ 2982 if ((bldominates(zlabel, zid_label)) && 2983 (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) { 2984 /* 2985 * Our zone dominates this one. 2986 * Create a lofs mount from lower zone's /export/home 2987 */ 2988 (void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN, 2989 "%s/zone/%s/export/home", rootpath, zid_name); 2990 2991 /* 2992 * If the target is already an LOFS mount 2993 * then don't do it again. 2994 */ 2995 if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) || 2996 strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) { 2997 2998 if (snprintf(lower_fstab.zone_fs_special, 2999 MAXPATHLEN, "%s/export", 3000 zid_rpath) > MAXPATHLEN) 3001 continue; 3002 3003 /* 3004 * Make sure the lower-level home exists 3005 */ 3006 if (make_one_dir(zlogp, 3007 lower_fstab.zone_fs_special, 3008 "/home", DEFAULT_DIR_MODE) != 0) 3009 continue; 3010 3011 (void) strlcat(lower_fstab.zone_fs_special, 3012 "/home", MAXPATHLEN); 3013 3014 /* 3015 * Mount can fail because the lower-level 3016 * zone may have already done a mount up. 3017 */ 3018 (void) mount_one(zlogp, &lower_fstab, ""); 3019 } 3020 } else if ((bldominates(zid_label, zlabel)) && 3021 (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) { 3022 /* 3023 * This zone dominates our zone. 3024 * Create a lofs mount from our zone's /export/home 3025 */ 3026 if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN, 3027 "%s/zone/%s/export/home", zid_rpath, 3028 zone_name) > MAXPATHLEN) 3029 continue; 3030 3031 /* 3032 * If the target is already an LOFS mount 3033 * then don't do it again. 3034 */ 3035 if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) || 3036 strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) { 3037 3038 (void) snprintf(lower_fstab.zone_fs_special, 3039 MAXPATHLEN, "%s/export/home", rootpath); 3040 3041 /* 3042 * Mount can fail because the higher-level 3043 * zone may have already done a mount down. 3044 */ 3045 (void) mount_one(zlogp, &lower_fstab, ""); 3046 } 3047 } 3048 } 3049 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 3050 priv_freeset(zid_privs); 3051 free(zids); 3052 3053 /* 3054 * Now share any exported directories from this zone. 3055 * Each zone can have its own dfstab. 3056 */ 3057 3058 argv[0] = "zoneshare"; 3059 argv[1] = "-z"; 3060 argv[2] = zone_name; 3061 argv[3] = NULL; 3062 3063 (void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv); 3064 /* Don't check for errors since they don't affect the zone */ 3065 3066 return (0); 3067 } 3068 3069 /* 3070 * Unmount lofs mounts from higher level zones 3071 * Unshare nfs exported directories 3072 */ 3073 static void 3074 tsol_unmounts(zlog_t *zlogp, char *zone_name) 3075 { 3076 zoneid_t *zids = NULL; 3077 uint_t nzents_saved; 3078 uint_t nzents; 3079 int i; 3080 char *argv[4]; 3081 char path[MAXPATHLEN]; 3082 3083 if (!is_system_labeled()) 3084 return; 3085 3086 /* 3087 * Get the list of zones from the kernel 3088 */ 3089 if (zone_list(NULL, &nzents) != 0) { 3090 return; 3091 } 3092 3093 if (zid_label == NULL) { 3094 zid_label = m_label_alloc(MAC_LABEL); 3095 if (zid_label == NULL) 3096 return; 3097 } 3098 3099 again: 3100 if (nzents == 0) 3101 return; 3102 3103 zids = malloc(nzents * sizeof (zoneid_t)); 3104 if (zids == NULL) { 3105 zerror(zlogp, B_TRUE, "memory allocation failed"); 3106 return; 3107 } 3108 nzents_saved = nzents; 3109 3110 if (zone_list(zids, &nzents) != 0) { 3111 free(zids); 3112 return; 3113 } 3114 if (nzents != nzents_saved) { 3115 /* list changed, try again */ 3116 free(zids); 3117 goto again; 3118 } 3119 3120 for (i = 0; i < nzents; i++) { 3121 char zid_name[ZONENAME_MAX]; 3122 zone_state_t zid_state; 3123 char zid_rpath[MAXPATHLEN]; 3124 3125 if (zids[i] == GLOBAL_ZONEID) 3126 continue; 3127 3128 if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1) 3129 continue; 3130 3131 /* 3132 * Skip the zone we are halting 3133 */ 3134 if (strcmp(zid_name, zone_name) == 0) 3135 continue; 3136 3137 if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state, 3138 sizeof (zid_state)) < 0) || 3139 (zid_state < ZONE_IS_READY)) 3140 /* Skip over zones without mounted filesystems */ 3141 continue; 3142 3143 if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label, 3144 sizeof (m_label_t)) < 0) 3145 /* Skip over zones with unspecified label */ 3146 continue; 3147 3148 if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath, 3149 sizeof (zid_rpath)) == -1) 3150 /* Skip over zones with bad path */ 3151 continue; 3152 3153 if (zlabel != NULL && bldominates(zid_label, zlabel)) { 3154 /* 3155 * This zone dominates our zone. 3156 * Unmount the lofs mount of our zone's /export/home 3157 */ 3158 3159 if (snprintf(path, MAXPATHLEN, 3160 "%s/zone/%s/export/home", zid_rpath, 3161 zone_name) > MAXPATHLEN) 3162 continue; 3163 3164 /* Skip over mount failures */ 3165 (void) umount(path); 3166 } 3167 } 3168 free(zids); 3169 3170 /* 3171 * Unmount global zone autofs trigger for this zone 3172 */ 3173 (void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name); 3174 /* Skip over mount failures */ 3175 (void) umount(path); 3176 3177 /* 3178 * Next unshare any exported directories from this zone. 3179 */ 3180 3181 argv[0] = "zoneunshare"; 3182 argv[1] = "-z"; 3183 argv[2] = zone_name; 3184 argv[3] = NULL; 3185 3186 (void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv); 3187 /* Don't check for errors since they don't affect the zone */ 3188 3189 /* 3190 * Finally, deallocate any devices in the zone. 3191 */ 3192 3193 argv[0] = "deallocate"; 3194 argv[1] = "-Isz"; 3195 argv[2] = zone_name; 3196 argv[3] = NULL; 3197 3198 (void) forkexec(zlogp, "/usr/sbin/deallocate", argv); 3199 /* Don't check for errors since they don't affect the zone */ 3200 } 3201 3202 /* 3203 * Fetch the Trusted Extensions label and multi-level ports (MLPs) for 3204 * this zone. 3205 */ 3206 static tsol_zcent_t * 3207 get_zone_label(zlog_t *zlogp, priv_set_t *privs) 3208 { 3209 FILE *fp; 3210 tsol_zcent_t *zcent = NULL; 3211 char line[MAXTNZLEN]; 3212 3213 if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) { 3214 zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH); 3215 return (NULL); 3216 } 3217 3218 while (fgets(line, sizeof (line), fp) != NULL) { 3219 /* 3220 * Check for malformed database 3221 */ 3222 if (strlen(line) == MAXTNZLEN - 1) 3223 break; 3224 if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL) 3225 continue; 3226 if (strcmp(zcent->zc_name, zone_name) == 0) 3227 break; 3228 tsol_freezcent(zcent); 3229 zcent = NULL; 3230 } 3231 (void) fclose(fp); 3232 3233 if (zcent == NULL) { 3234 zerror(zlogp, B_FALSE, "zone requires a label assignment. " 3235 "See tnzonecfg(4)"); 3236 } else { 3237 if (zlabel == NULL) 3238 zlabel = m_label_alloc(MAC_LABEL); 3239 /* 3240 * Save this zone's privileges for later read-down processing 3241 */ 3242 if ((zprivs = priv_allocset()) == NULL) { 3243 zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 3244 return (NULL); 3245 } else { 3246 priv_copyset(privs, zprivs); 3247 } 3248 } 3249 return (zcent); 3250 } 3251 3252 /* 3253 * Add the Trusted Extensions multi-level ports for this zone. 3254 */ 3255 static void 3256 set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent) 3257 { 3258 tsol_mlp_t *mlp; 3259 tsol_mlpent_t tsme; 3260 3261 if (!is_system_labeled()) 3262 return; 3263 3264 tsme.tsme_zoneid = zoneid; 3265 tsme.tsme_flags = 0; 3266 for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) { 3267 tsme.tsme_mlp = *mlp; 3268 if (tnmlp(TNDB_LOAD, &tsme) != 0) { 3269 zerror(zlogp, B_TRUE, "cannot set zone-specific MLP " 3270 "on %d-%d/%d", mlp->mlp_port, 3271 mlp->mlp_port_upper, mlp->mlp_ipp); 3272 } 3273 } 3274 3275 tsme.tsme_flags = TSOL_MEF_SHARED; 3276 for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) { 3277 tsme.tsme_mlp = *mlp; 3278 if (tnmlp(TNDB_LOAD, &tsme) != 0) { 3279 zerror(zlogp, B_TRUE, "cannot set shared MLP " 3280 "on %d-%d/%d", mlp->mlp_port, 3281 mlp->mlp_port_upper, mlp->mlp_ipp); 3282 } 3283 } 3284 } 3285 3286 static void 3287 remove_mlps(zlog_t *zlogp, zoneid_t zoneid) 3288 { 3289 tsol_mlpent_t tsme; 3290 3291 if (!is_system_labeled()) 3292 return; 3293 3294 (void) memset(&tsme, 0, sizeof (tsme)); 3295 tsme.tsme_zoneid = zoneid; 3296 if (tnmlp(TNDB_FLUSH, &tsme) != 0) 3297 zerror(zlogp, B_TRUE, "cannot flush MLPs"); 3298 } 3299 3300 int 3301 prtmount(const char *fs, void *x) { 3302 zerror((zlog_t *)x, B_FALSE, " %s", fs); 3303 return (0); 3304 } 3305 3306 /* 3307 * Look for zones running on the main system that are using this root (or any 3308 * subdirectory of it). Return B_TRUE and print an error if a conflicting zone 3309 * is found or if we can't tell. 3310 */ 3311 static boolean_t 3312 duplicate_zone_root(zlog_t *zlogp, const char *rootpath) 3313 { 3314 zoneid_t *zids = NULL; 3315 uint_t nzids = 0; 3316 boolean_t retv; 3317 int rlen, zlen; 3318 char zroot[MAXPATHLEN]; 3319 char zonename[ZONENAME_MAX]; 3320 3321 for (;;) { 3322 nzids += 10; 3323 zids = malloc(nzids * sizeof (*zids)); 3324 if (zids == NULL) { 3325 zerror(zlogp, B_TRUE, "memory allocation failed"); 3326 return (B_TRUE); 3327 } 3328 if (zone_list(zids, &nzids) == 0) 3329 break; 3330 free(zids); 3331 } 3332 retv = B_FALSE; 3333 rlen = strlen(rootpath); 3334 while (nzids > 0) { 3335 /* 3336 * Ignore errors; they just mean that the zone has disappeared 3337 * while we were busy. 3338 */ 3339 if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot, 3340 sizeof (zroot)) == -1) 3341 continue; 3342 zlen = strlen(zroot); 3343 if (zlen > rlen) 3344 zlen = rlen; 3345 if (strncmp(rootpath, zroot, zlen) == 0 && 3346 (zroot[zlen] == '\0' || zroot[zlen] == '/') && 3347 (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) { 3348 if (getzonenamebyid(zids[nzids], zonename, 3349 sizeof (zonename)) == -1) 3350 (void) snprintf(zonename, sizeof (zonename), 3351 "id %d", (int)zids[nzids]); 3352 zerror(zlogp, B_FALSE, 3353 "zone root %s already in use by zone %s", 3354 rootpath, zonename); 3355 retv = B_TRUE; 3356 break; 3357 } 3358 } 3359 free(zids); 3360 return (retv); 3361 } 3362 3363 /* 3364 * Search for loopback mounts that use this same source node (same device and 3365 * inode). Return B_TRUE if there is one or if we can't tell. 3366 */ 3367 static boolean_t 3368 duplicate_reachable_path(zlog_t *zlogp, const char *rootpath) 3369 { 3370 struct stat64 rst, zst; 3371 struct mnttab *mnp; 3372 3373 if (stat64(rootpath, &rst) == -1) { 3374 zerror(zlogp, B_TRUE, "can't stat %s", rootpath); 3375 return (B_TRUE); 3376 } 3377 if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1) 3378 return (B_TRUE); 3379 for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) { 3380 if (mnp->mnt_fstype == NULL || 3381 strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0) 3382 continue; 3383 /* We're looking at a loopback mount. Stat it. */ 3384 if (mnp->mnt_special != NULL && 3385 stat64(mnp->mnt_special, &zst) != -1 && 3386 rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) { 3387 zerror(zlogp, B_FALSE, 3388 "zone root %s is reachable through %s", 3389 rootpath, mnp->mnt_mountp); 3390 return (B_TRUE); 3391 } 3392 } 3393 return (B_FALSE); 3394 } 3395 3396 /* 3397 * Set memory cap and pool info for the zone's resource management 3398 * configuration. 3399 */ 3400 static int 3401 setup_zone_rm(zlog_t *zlogp, char *zone_name, zoneid_t zoneid) 3402 { 3403 int res; 3404 uint64_t tmp; 3405 struct zone_mcaptab mcap; 3406 char sched[MAXNAMELEN]; 3407 zone_dochandle_t handle = NULL; 3408 char pool_err[128]; 3409 3410 if ((handle = zonecfg_init_handle()) == NULL) { 3411 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 3412 return (Z_BAD_HANDLE); 3413 } 3414 3415 if ((res = zonecfg_get_snapshot_handle(zone_name, handle)) != Z_OK) { 3416 zerror(zlogp, B_FALSE, "invalid configuration"); 3417 zonecfg_fini_handle(handle); 3418 return (res); 3419 } 3420 3421 /* 3422 * If a memory cap is configured, set the cap in the kernel using 3423 * zone_setattr() and make sure the rcapd SMF service is enabled. 3424 */ 3425 if (zonecfg_getmcapent(handle, &mcap) == Z_OK) { 3426 uint64_t num; 3427 char smf_err[128]; 3428 3429 num = (uint64_t)strtoull(mcap.zone_physmem_cap, NULL, 10); 3430 if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) { 3431 zerror(zlogp, B_TRUE, "could not set zone memory cap"); 3432 zonecfg_fini_handle(handle); 3433 return (Z_INVAL); 3434 } 3435 3436 if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) { 3437 zerror(zlogp, B_FALSE, "enabling system/rcap service " 3438 "failed: %s", smf_err); 3439 zonecfg_fini_handle(handle); 3440 return (Z_INVAL); 3441 } 3442 } 3443 3444 /* Get the scheduling class set in the zone configuration. */ 3445 if (zonecfg_get_sched_class(handle, sched, sizeof (sched)) == Z_OK && 3446 strlen(sched) > 0) { 3447 if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, sched, 3448 strlen(sched)) == -1) 3449 zerror(zlogp, B_TRUE, "WARNING: unable to set the " 3450 "default scheduling class"); 3451 3452 } else if (zonecfg_get_aliased_rctl(handle, ALIAS_SHARES, &tmp) 3453 == Z_OK) { 3454 /* 3455 * If the zone has the zone.cpu-shares rctl set then we want to 3456 * use the Fair Share Scheduler (FSS) for processes in the 3457 * zone. Check what scheduling class the zone would be running 3458 * in by default so we can print a warning and modify the class 3459 * if we wouldn't be using FSS. 3460 */ 3461 char class_name[PC_CLNMSZ]; 3462 3463 if (zonecfg_get_dflt_sched_class(handle, class_name, 3464 sizeof (class_name)) != Z_OK) { 3465 zerror(zlogp, B_FALSE, "WARNING: unable to determine " 3466 "the zone's scheduling class"); 3467 3468 } else if (strcmp("FSS", class_name) != 0) { 3469 zerror(zlogp, B_FALSE, "WARNING: The zone.cpu-shares " 3470 "rctl is set but\nFSS is not the default " 3471 "scheduling class for\nthis zone. FSS will be " 3472 "used for processes\nin the zone but to get the " 3473 "full benefit of FSS,\nit should be the default " 3474 "scheduling class.\nSee dispadmin(1M) for more " 3475 "details."); 3476 3477 if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, "FSS", 3478 strlen("FSS")) == -1) 3479 zerror(zlogp, B_TRUE, "WARNING: unable to set " 3480 "zone scheduling class to FSS"); 3481 } 3482 } 3483 3484 /* 3485 * The next few blocks of code attempt to set up temporary pools as 3486 * well as persistent pools. In all cases we call the functions 3487 * unconditionally. Within each funtion the code will check if the 3488 * zone is actually configured for a temporary pool or persistent pool 3489 * and just return if there is nothing to do. 3490 * 3491 * If we are rebooting we want to attempt to reuse any temporary pool 3492 * that was previously set up. zonecfg_bind_tmp_pool() will do the 3493 * right thing in all cases (reuse or create) based on the current 3494 * zonecfg. 3495 */ 3496 if ((res = zonecfg_bind_tmp_pool(handle, zoneid, pool_err, 3497 sizeof (pool_err))) != Z_OK) { 3498 if (res == Z_POOL || res == Z_POOL_CREATE || res == Z_POOL_BIND) 3499 zerror(zlogp, B_FALSE, "%s: %s\ndedicated-cpu setting " 3500 "cannot be instantiated", zonecfg_strerror(res), 3501 pool_err); 3502 else 3503 zerror(zlogp, B_FALSE, "could not bind zone to " 3504 "temporary pool: %s", zonecfg_strerror(res)); 3505 zonecfg_fini_handle(handle); 3506 return (Z_POOL_BIND); 3507 } 3508 3509 /* 3510 * Check if we need to warn about poold not being enabled. 3511 */ 3512 if (zonecfg_warn_poold(handle)) { 3513 zerror(zlogp, B_FALSE, "WARNING: A range of dedicated-cpus has " 3514 "been specified\nbut the dynamic pool service is not " 3515 "enabled.\nThe system will not dynamically adjust the\n" 3516 "processor allocation within the specified range\n" 3517 "until svc:/system/pools/dynamic is enabled.\n" 3518 "See poold(1M)."); 3519 } 3520 3521 /* The following is a warning, not an error. */ 3522 if ((res = zonecfg_bind_pool(handle, zoneid, pool_err, 3523 sizeof (pool_err))) != Z_OK) { 3524 if (res == Z_POOL_BIND) 3525 zerror(zlogp, B_FALSE, "WARNING: unable to bind to " 3526 "pool '%s'; using default pool.", pool_err); 3527 else if (res == Z_POOL) 3528 zerror(zlogp, B_FALSE, "WARNING: %s: %s", 3529 zonecfg_strerror(res), pool_err); 3530 else 3531 zerror(zlogp, B_FALSE, "WARNING: %s", 3532 zonecfg_strerror(res)); 3533 } 3534 3535 zonecfg_fini_handle(handle); 3536 return (Z_OK); 3537 } 3538 3539 zoneid_t 3540 vplat_create(zlog_t *zlogp, boolean_t mount_cmd) 3541 { 3542 zoneid_t rval = -1; 3543 priv_set_t *privs; 3544 char rootpath[MAXPATHLEN]; 3545 char modname[MAXPATHLEN]; 3546 struct brand_attr attr; 3547 brand_handle_t bh; 3548 char *rctlbuf = NULL; 3549 size_t rctlbufsz = 0; 3550 char *zfsbuf = NULL; 3551 size_t zfsbufsz = 0; 3552 zoneid_t zoneid = -1; 3553 int xerr; 3554 char *kzone; 3555 FILE *fp = NULL; 3556 tsol_zcent_t *zcent = NULL; 3557 int match = 0; 3558 int doi = 0; 3559 3560 if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) { 3561 zerror(zlogp, B_TRUE, "unable to determine zone root"); 3562 return (-1); 3563 } 3564 if (zonecfg_in_alt_root()) 3565 resolve_lofs(zlogp, rootpath, sizeof (rootpath)); 3566 3567 if ((privs = priv_allocset()) == NULL) { 3568 zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 3569 return (-1); 3570 } 3571 priv_emptyset(privs); 3572 if (get_privset(zlogp, privs, mount_cmd) != 0) 3573 goto error; 3574 3575 if (!mount_cmd && get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) { 3576 zerror(zlogp, B_FALSE, "Unable to get list of rctls"); 3577 goto error; 3578 } 3579 3580 if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) { 3581 zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets"); 3582 goto error; 3583 } 3584 3585 if (!mount_cmd && is_system_labeled()) { 3586 zcent = get_zone_label(zlogp, privs); 3587 if (zcent != NULL) { 3588 match = zcent->zc_match; 3589 doi = zcent->zc_doi; 3590 *zlabel = zcent->zc_label; 3591 } else { 3592 goto error; 3593 } 3594 } 3595 3596 kzone = zone_name; 3597 3598 /* 3599 * We must do this scan twice. First, we look for zones running on the 3600 * main system that are using this root (or any subdirectory of it). 3601 * Next, we reduce to the shortest path and search for loopback mounts 3602 * that use this same source node (same device and inode). 3603 */ 3604 if (duplicate_zone_root(zlogp, rootpath)) 3605 goto error; 3606 if (duplicate_reachable_path(zlogp, rootpath)) 3607 goto error; 3608 3609 if (mount_cmd) { 3610 assert(zone_isnative); 3611 root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE); 3612 3613 /* 3614 * Forge up a special root for this zone. When a zone is 3615 * mounted, we can't let the zone have its own root because the 3616 * tools that will be used in this "scratch zone" need access 3617 * to both the zone's resources and the running machine's 3618 * executables. 3619 * 3620 * Note that the mkdir here also catches read-only filesystems. 3621 */ 3622 if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) { 3623 zerror(zlogp, B_TRUE, "cannot create %s", rootpath); 3624 goto error; 3625 } 3626 if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0) 3627 goto error; 3628 } 3629 3630 if (zonecfg_in_alt_root()) { 3631 /* 3632 * If we are mounting up a zone in an alternate root partition, 3633 * then we have some additional work to do before starting the 3634 * zone. First, resolve the root path down so that we're not 3635 * fooled by duplicates. Then forge up an internal name for 3636 * the zone. 3637 */ 3638 if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) { 3639 zerror(zlogp, B_TRUE, "cannot open mapfile"); 3640 goto error; 3641 } 3642 if (zonecfg_lock_scratch(fp) != 0) { 3643 zerror(zlogp, B_TRUE, "cannot lock mapfile"); 3644 goto error; 3645 } 3646 if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(), 3647 NULL, 0) == 0) { 3648 zerror(zlogp, B_FALSE, "scratch zone already running"); 3649 goto error; 3650 } 3651 /* This is the preferred name */ 3652 (void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s", 3653 zone_name); 3654 srandom(getpid()); 3655 while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL, 3656 0) == 0) { 3657 /* This is just an arbitrary name; note "." usage */ 3658 (void) snprintf(kernzone, sizeof (kernzone), 3659 "SUNWlu.%08lX%08lX", random(), random()); 3660 } 3661 kzone = kernzone; 3662 } 3663 3664 xerr = 0; 3665 if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf, 3666 rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel)) == -1) { 3667 if (xerr == ZE_AREMOUNTS) { 3668 if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) { 3669 zerror(zlogp, B_FALSE, 3670 "An unknown file-system is mounted on " 3671 "a subdirectory of %s", rootpath); 3672 } else { 3673 3674 zerror(zlogp, B_FALSE, 3675 "These file-systems are mounted on " 3676 "subdirectories of %s:", rootpath); 3677 (void) zonecfg_find_mounts(rootpath, 3678 prtmount, zlogp); 3679 } 3680 } else if (xerr == ZE_CHROOTED) { 3681 zerror(zlogp, B_FALSE, "%s: " 3682 "cannot create a zone from a chrooted " 3683 "environment", "zone_create"); 3684 } else { 3685 zerror(zlogp, B_TRUE, "%s failed", "zone_create"); 3686 } 3687 goto error; 3688 } 3689 3690 if (zonecfg_in_alt_root() && 3691 zonecfg_add_scratch(fp, zone_name, kernzone, 3692 zonecfg_get_root()) == -1) { 3693 zerror(zlogp, B_TRUE, "cannot add mapfile entry"); 3694 goto error; 3695 } 3696 3697 if ((zone_get_brand(zone_name, attr.ba_brandname, 3698 MAXNAMELEN) != Z_OK) || 3699 (bh = brand_open(attr.ba_brandname)) == NULL) { 3700 zerror(zlogp, B_FALSE, "unable to determine brand name"); 3701 return (-1); 3702 } 3703 3704 /* 3705 * If this brand requires any kernel support, now is the time to 3706 * get it loaded and initialized. 3707 */ 3708 if (brand_get_modname(bh, modname, MAXPATHLEN) < 0) { 3709 zerror(zlogp, B_FALSE, "unable to determine brand kernel " 3710 "module"); 3711 return (-1); 3712 } 3713 3714 if (strlen(modname) > 0) { 3715 (void) strlcpy(attr.ba_modname, modname, MAXPATHLEN); 3716 if (zone_setattr(zoneid, ZONE_ATTR_BRAND, &attr, 3717 sizeof (attr) != 0)) { 3718 zerror(zlogp, B_TRUE, "could not set zone brand " 3719 "attribute."); 3720 goto error; 3721 } 3722 } 3723 3724 /* 3725 * The following actions are not performed when merely mounting a zone 3726 * for administrative use. 3727 */ 3728 if (!mount_cmd) { 3729 if (setup_zone_rm(zlogp, zone_name, zoneid) != Z_OK) { 3730 (void) zone_shutdown(zoneid); 3731 goto error; 3732 } 3733 3734 set_mlps(zlogp, zoneid, zcent); 3735 } 3736 3737 rval = zoneid; 3738 zoneid = -1; 3739 3740 error: 3741 if (zoneid != -1) 3742 (void) zone_destroy(zoneid); 3743 if (rctlbuf != NULL) 3744 free(rctlbuf); 3745 priv_freeset(privs); 3746 if (fp != NULL) 3747 zonecfg_close_scratch(fp); 3748 lofs_discard_mnttab(); 3749 if (zcent != NULL) 3750 tsol_freezcent(zcent); 3751 return (rval); 3752 } 3753 3754 /* 3755 * Enter the zone and write a /etc/zones/index file there. This allows 3756 * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone 3757 * details from inside the zone. 3758 */ 3759 static void 3760 write_index_file(zoneid_t zoneid) 3761 { 3762 FILE *zef; 3763 FILE *zet; 3764 struct zoneent *zep; 3765 pid_t child; 3766 int tmpl_fd; 3767 ctid_t ct; 3768 int fd; 3769 char uuidstr[UUID_PRINTABLE_STRING_LENGTH]; 3770 3771 /* Locate the zone entry in the global zone's index file */ 3772 if ((zef = setzoneent()) == NULL) 3773 return; 3774 while ((zep = getzoneent_private(zef)) != NULL) { 3775 if (strcmp(zep->zone_name, zone_name) == 0) 3776 break; 3777 free(zep); 3778 } 3779 endzoneent(zef); 3780 if (zep == NULL) 3781 return; 3782 3783 if ((tmpl_fd = init_template()) == -1) { 3784 free(zep); 3785 return; 3786 } 3787 3788 if ((child = fork()) == -1) { 3789 (void) ct_tmpl_clear(tmpl_fd); 3790 (void) close(tmpl_fd); 3791 free(zep); 3792 return; 3793 } 3794 3795 /* parent waits for child to finish */ 3796 if (child != 0) { 3797 free(zep); 3798 if (contract_latest(&ct) == -1) 3799 ct = -1; 3800 (void) ct_tmpl_clear(tmpl_fd); 3801 (void) close(tmpl_fd); 3802 (void) waitpid(child, NULL, 0); 3803 (void) contract_abandon_id(ct); 3804 return; 3805 } 3806 3807 /* child enters zone and sets up index file */ 3808 (void) ct_tmpl_clear(tmpl_fd); 3809 if (zone_enter(zoneid) != -1) { 3810 (void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE); 3811 (void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID, 3812 ZONE_CONFIG_GID); 3813 fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC, 3814 ZONE_INDEX_MODE); 3815 if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) { 3816 (void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID); 3817 if (uuid_is_null(zep->zone_uuid)) 3818 uuidstr[0] = '\0'; 3819 else 3820 uuid_unparse(zep->zone_uuid, uuidstr); 3821 (void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name, 3822 zone_state_str(zep->zone_state), 3823 uuidstr); 3824 (void) fclose(zet); 3825 } 3826 } 3827 _exit(0); 3828 } 3829 3830 int 3831 vplat_bringup(zlog_t *zlogp, boolean_t mount_cmd, zoneid_t zoneid) 3832 { 3833 char zonepath[MAXPATHLEN]; 3834 3835 if (!mount_cmd && validate_datasets(zlogp) != 0) { 3836 lofs_discard_mnttab(); 3837 return (-1); 3838 } 3839 3840 /* 3841 * Before we try to mount filesystems we need to create the 3842 * attribute backing store for /dev 3843 */ 3844 if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) { 3845 lofs_discard_mnttab(); 3846 return (-1); 3847 } 3848 3849 resolve_lofs(zlogp, zonepath, sizeof (zonepath)); 3850 if (make_one_dir(zlogp, zonepath, "/dev", DEFAULT_DIR_MODE) != 0) { 3851 lofs_discard_mnttab(); 3852 return (-1); 3853 } 3854 3855 if (mount_filesystems(zlogp, mount_cmd) != 0) { 3856 lofs_discard_mnttab(); 3857 return (-1); 3858 } 3859 3860 if (!mount_cmd && configure_network_interfaces(zlogp) != 0) { 3861 lofs_discard_mnttab(); 3862 return (-1); 3863 } 3864 3865 write_index_file(zoneid); 3866 3867 lofs_discard_mnttab(); 3868 return (0); 3869 } 3870 3871 static int 3872 lu_root_teardown(zlog_t *zlogp) 3873 { 3874 char zroot[MAXPATHLEN]; 3875 3876 assert(zone_isnative); 3877 3878 if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) { 3879 zerror(zlogp, B_FALSE, "unable to determine zone root"); 3880 return (-1); 3881 } 3882 root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE); 3883 3884 /* 3885 * At this point, the processes are gone, the filesystems (save the 3886 * root) are unmounted, and the zone is on death row. But there may 3887 * still be creds floating about in the system that reference the 3888 * zone_t, and which pin down zone_rootvp causing this call to fail 3889 * with EBUSY. Thus, we try for a little while before just giving up. 3890 * (How I wish this were not true, and umount2 just did the right 3891 * thing, or tmpfs supported MS_FORCE This is a gross hack.) 3892 */ 3893 if (umount2(zroot, MS_FORCE) != 0) { 3894 if (errno == ENOTSUP && umount2(zroot, 0) == 0) 3895 goto unmounted; 3896 if (errno == EBUSY) { 3897 int tries = 10; 3898 3899 while (--tries >= 0) { 3900 (void) sleep(1); 3901 if (umount2(zroot, 0) == 0) 3902 goto unmounted; 3903 if (errno != EBUSY) 3904 break; 3905 } 3906 } 3907 zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot); 3908 return (-1); 3909 } 3910 unmounted: 3911 3912 /* 3913 * Only zones in an alternate root environment have scratch zone 3914 * entries. 3915 */ 3916 if (zonecfg_in_alt_root()) { 3917 FILE *fp; 3918 int retv; 3919 3920 if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 3921 zerror(zlogp, B_TRUE, "cannot open mapfile"); 3922 return (-1); 3923 } 3924 retv = -1; 3925 if (zonecfg_lock_scratch(fp) != 0) 3926 zerror(zlogp, B_TRUE, "cannot lock mapfile"); 3927 else if (zonecfg_delete_scratch(fp, kernzone) != 0) 3928 zerror(zlogp, B_TRUE, "cannot delete map entry"); 3929 else 3930 retv = 0; 3931 zonecfg_close_scratch(fp); 3932 return (retv); 3933 } else { 3934 return (0); 3935 } 3936 } 3937 3938 int 3939 vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting) 3940 { 3941 char *kzone; 3942 zoneid_t zoneid; 3943 int res; 3944 char pool_err[128]; 3945 char zroot[MAXPATHLEN]; 3946 char cmdbuf[MAXPATHLEN]; 3947 char brand[MAXNAMELEN]; 3948 brand_handle_t bh = NULL; 3949 3950 kzone = zone_name; 3951 if (zonecfg_in_alt_root()) { 3952 FILE *fp; 3953 3954 if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 3955 zerror(zlogp, B_TRUE, "unable to open map file"); 3956 goto error; 3957 } 3958 if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(), 3959 kernzone, sizeof (kernzone)) != 0) { 3960 zerror(zlogp, B_FALSE, "unable to find scratch zone"); 3961 zonecfg_close_scratch(fp); 3962 goto error; 3963 } 3964 zonecfg_close_scratch(fp); 3965 kzone = kernzone; 3966 } 3967 3968 if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) { 3969 if (!bringup_failure_recovery) 3970 zerror(zlogp, B_TRUE, "unable to get zoneid"); 3971 if (unmount_cmd) 3972 (void) lu_root_teardown(zlogp); 3973 goto error; 3974 } 3975 3976 if (zone_shutdown(zoneid) != 0) { 3977 zerror(zlogp, B_TRUE, "unable to shutdown zone"); 3978 goto error; 3979 } 3980 3981 /* Get the path to the root of this zone */ 3982 if (zone_get_zonepath(zone_name, zroot, sizeof (zroot)) != Z_OK) { 3983 zerror(zlogp, B_FALSE, "unable to determine zone root"); 3984 goto error; 3985 } 3986 3987 /* Get a handle to the brand info for this zone */ 3988 if ((zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) || 3989 (bh = brand_open(brand)) == NULL) { 3990 zerror(zlogp, B_FALSE, "unable to determine zone brand"); 3991 return (-1); 3992 } 3993 /* 3994 * If there is a brand 'halt' callback, execute it now to give the 3995 * brand a chance to cleanup any custom configuration. 3996 */ 3997 (void) strcpy(cmdbuf, EXEC_PREFIX); 3998 if (brand_get_halt(bh, zone_name, zroot, cmdbuf + EXEC_LEN, 3999 sizeof (cmdbuf) - EXEC_LEN, 0, NULL) < 0) { 4000 brand_close(bh); 4001 zerror(zlogp, B_FALSE, "unable to determine branded zone's " 4002 "halt callback."); 4003 goto error; 4004 } 4005 brand_close(bh); 4006 4007 if ((strlen(cmdbuf) > EXEC_LEN) && 4008 (do_subproc(zlogp, cmdbuf) != Z_OK)) { 4009 zerror(zlogp, B_FALSE, "%s failed", cmdbuf); 4010 goto error; 4011 } 4012 4013 if (!unmount_cmd && 4014 unconfigure_network_interfaces(zlogp, zoneid) != 0) { 4015 zerror(zlogp, B_FALSE, 4016 "unable to unconfigure network interfaces in zone"); 4017 goto error; 4018 } 4019 4020 if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) { 4021 zerror(zlogp, B_TRUE, "unable to abort TCP connections"); 4022 goto error; 4023 } 4024 4025 /* destroy zconsole before umount /dev */ 4026 if (!unmount_cmd) 4027 destroy_console_slave(); 4028 4029 if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) { 4030 zerror(zlogp, B_FALSE, 4031 "unable to unmount file systems in zone"); 4032 goto error; 4033 } 4034 4035 /* 4036 * If we are rebooting then we don't want to destroy an existing 4037 * temporary pool at this point so that we can just reuse it when the 4038 * zone boots back up. 4039 */ 4040 if (!unmount_cmd && !rebooting) { 4041 if ((res = zonecfg_destroy_tmp_pool(zone_name, pool_err, 4042 sizeof (pool_err))) != Z_OK) { 4043 if (res == Z_POOL) 4044 zerror(zlogp, B_FALSE, pool_err); 4045 } 4046 } 4047 4048 remove_mlps(zlogp, zoneid); 4049 4050 if (zone_destroy(zoneid) != 0) { 4051 zerror(zlogp, B_TRUE, "unable to destroy zone"); 4052 goto error; 4053 } 4054 4055 /* 4056 * Special teardown for alternate boot environments: remove the tmpfs 4057 * root for the zone and then remove it from the map file. 4058 */ 4059 if (unmount_cmd && lu_root_teardown(zlogp) != 0) 4060 goto error; 4061 4062 lofs_discard_mnttab(); 4063 return (0); 4064 4065 error: 4066 lofs_discard_mnttab(); 4067 return (-1); 4068 } 4069