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