Print this page
7029 want per-process exploit mitigation features (secflags)
7030 want basic address space layout randomization (aslr)
7031 noexec_user_stack should be a secflag
7032 want a means to forbid mappings around NULL.
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/zoneadmd/vplat.c
+++ new/usr/src/cmd/zoneadmd/vplat.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 * Copyright (c) 2013, Joyent Inc. All rights reserved.
25 25 * Copyright (c) 2015 by Delphix. All rights reserved.
26 26 */
27 27
28 28 /*
29 29 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
30 30 */
31 31
32 32 /*
33 33 * This module contains functions used to bring up and tear down the
34 34 * Virtual Platform: [un]mounting file-systems, [un]plumbing network
35 35 * interfaces, [un]configuring devices, establishing resource controls,
36 36 * and creating/destroying the zone in the kernel. These actions, on
37 37 * the way up, ready the zone; on the way down, they halt the zone.
38 38 * See the much longer block comment at the beginning of zoneadmd.c
39 39 * for a bigger picture of how the whole program functions.
40 40 *
41 41 * This module also has primary responsibility for the layout of "scratch
42 42 * zones." These are mounted, but inactive, zones that are used during
43 43 * operating system upgrade and potentially other administrative action. The
44 44 * scratch zone environment is similar to the miniroot environment. The zone's
45 45 * actual root is mounted read-write on /a, and the standard paths (/usr,
46 46 * /sbin, /lib) all lead to read-only copies of the running system's binaries.
47 47 * This allows the administrative tools to manipulate the zone using "-R /a"
48 48 * without relying on any binaries in the zone itself.
49 49 *
50 50 * If the scratch zone is on an alternate root (Live Upgrade [LU] boot
51 51 * environment), then we must resolve the lofs mounts used there to uncover
52 52 * writable (unshared) resources. Shared resources, though, are always
53 53 * read-only. In addition, if the "same" zone with a different root path is
54 54 * currently running, then "/b" inside the zone points to the running zone's
55 55 * root. This allows LU to synchronize configuration files during the upgrade
56 56 * process.
57 57 *
58 58 * To construct this environment, this module creates a tmpfs mount on
59 59 * $ZONEPATH/lu. Inside this scratch area, the miniroot-like environment as
60 60 * described above is constructed on the fly. The zone is then created using
61 61 * $ZONEPATH/lu as the root.
62 62 *
63 63 * Note that scratch zones are inactive. The zone's bits are not running and
64 64 * likely cannot be run correctly until upgrade is done. Init is not running
65 65 * there, nor is SMF. Because of this, the "mounted" state of a scratch zone
66 66 * is not a part of the usual halt/ready/boot state machine.
67 67 */
68 68
69 69 #include <sys/param.h>
↓ open down ↓ |
69 lines elided |
↑ open up ↑ |
70 70 #include <sys/mount.h>
71 71 #include <sys/mntent.h>
72 72 #include <sys/socket.h>
73 73 #include <sys/utsname.h>
74 74 #include <sys/types.h>
75 75 #include <sys/stat.h>
76 76 #include <sys/sockio.h>
77 77 #include <sys/stropts.h>
78 78 #include <sys/conf.h>
79 79 #include <sys/systeminfo.h>
80 +#include <sys/secflags.h>
80 81
81 82 #include <libdlpi.h>
82 83 #include <libdllink.h>
83 84 #include <libdlvlan.h>
84 85
85 86 #include <inet/tcp.h>
86 87 #include <arpa/inet.h>
87 88 #include <netinet/in.h>
88 89 #include <net/route.h>
89 90
90 91 #include <stdio.h>
91 92 #include <errno.h>
92 93 #include <fcntl.h>
93 94 #include <unistd.h>
94 95 #include <rctl.h>
95 96 #include <stdlib.h>
96 97 #include <string.h>
97 98 #include <strings.h>
98 99 #include <wait.h>
99 100 #include <limits.h>
100 101 #include <libgen.h>
101 102 #include <libzfs.h>
102 103 #include <libdevinfo.h>
103 104 #include <zone.h>
104 105 #include <assert.h>
105 106 #include <libcontract.h>
106 107 #include <libcontract_priv.h>
107 108 #include <uuid/uuid.h>
108 109
109 110 #include <sys/mntio.h>
110 111 #include <sys/mnttab.h>
111 112 #include <sys/fs/autofs.h> /* for _autofssys() */
112 113 #include <sys/fs/lofs_info.h>
113 114 #include <sys/fs/zfs.h>
114 115
115 116 #include <pool.h>
116 117 #include <sys/pool.h>
117 118 #include <sys/priocntl.h>
118 119
119 120 #include <libbrand.h>
120 121 #include <sys/brand.h>
121 122 #include <libzonecfg.h>
122 123 #include <synch.h>
123 124
124 125 #include "zoneadmd.h"
125 126 #include <tsol/label.h>
126 127 #include <libtsnet.h>
127 128 #include <sys/priv.h>
128 129 #include <libinetutil.h>
129 130
130 131 #define V4_ADDR_LEN 32
131 132 #define V6_ADDR_LEN 128
132 133
133 134 #define RESOURCE_DEFAULT_OPTS \
134 135 MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES
135 136
136 137 #define DFSTYPES "/etc/dfs/fstypes"
137 138 #define MAXTNZLEN 2048
138 139
139 140 #define ALT_MOUNT(mount_cmd) ((mount_cmd) != Z_MNT_BOOT)
140 141
141 142 /* a reasonable estimate for the number of lwps per process */
142 143 #define LWPS_PER_PROCESS 10
143 144
144 145 /* for routing socket */
145 146 static int rts_seqno = 0;
146 147
147 148 /* mangled zone name when mounting in an alternate root environment */
148 149 static char kernzone[ZONENAME_MAX];
149 150
150 151 /* array of cached mount entries for resolve_lofs */
151 152 static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max;
152 153
153 154 /* for Trusted Extensions */
154 155 static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *);
155 156 static int tsol_mounts(zlog_t *, char *, char *);
156 157 static void tsol_unmounts(zlog_t *, char *);
157 158
158 159 static m_label_t *zlabel = NULL;
159 160 static m_label_t *zid_label = NULL;
160 161 static priv_set_t *zprivs = NULL;
161 162
162 163 static const char *DFLT_FS_ALLOWED = "hsfs,smbfs,nfs,nfs3,nfs4,nfsdyn";
163 164
164 165 /* from libsocket, not in any header file */
165 166 extern int getnetmaskbyaddr(struct in_addr, struct in_addr *);
166 167
167 168 /* from zoneadmd */
168 169 extern char query_hook[];
169 170
170 171 /*
171 172 * For each "net" resource configured in zonecfg, we track a zone_addr_list_t
172 173 * node in a linked list that is sorted by linkid. The list is constructed as
173 174 * the xml configuration file is parsed, and the information
174 175 * contained in each node is added to the kernel before the zone is
175 176 * booted, to be retrieved and applied from within the exclusive-IP NGZ
176 177 * on boot.
177 178 */
178 179 typedef struct zone_addr_list {
179 180 struct zone_addr_list *za_next;
180 181 datalink_id_t za_linkid; /* datalink_id_t of interface */
181 182 struct zone_nwiftab za_nwiftab; /* address, defrouter properties */
182 183 } zone_addr_list_t;
183 184
184 185 /*
185 186 * An optimization for build_mnttable: reallocate (and potentially copy the
186 187 * data) only once every N times through the loop.
187 188 */
188 189 #define MNTTAB_HUNK 32
189 190
190 191 /* some handy macros */
191 192 #define SIN(s) ((struct sockaddr_in *)s)
192 193 #define SIN6(s) ((struct sockaddr_in6 *)s)
193 194
194 195 /*
195 196 * Private autofs system call
196 197 */
197 198 extern int _autofssys(int, void *);
198 199
199 200 static int
200 201 autofs_cleanup(zoneid_t zoneid)
201 202 {
202 203 /*
203 204 * Ask autofs to unmount all trigger nodes in the given zone.
204 205 */
205 206 return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid));
206 207 }
207 208
208 209 static void
209 210 free_mnttable(struct mnttab *mnt_array, uint_t nelem)
210 211 {
211 212 uint_t i;
212 213
213 214 if (mnt_array == NULL)
214 215 return;
215 216 for (i = 0; i < nelem; i++) {
216 217 free(mnt_array[i].mnt_mountp);
217 218 free(mnt_array[i].mnt_fstype);
218 219 free(mnt_array[i].mnt_special);
219 220 free(mnt_array[i].mnt_mntopts);
220 221 assert(mnt_array[i].mnt_time == NULL);
221 222 }
222 223 free(mnt_array);
223 224 }
224 225
225 226 /*
226 227 * Build the mount table for the zone rooted at "zroot", storing the resulting
227 228 * array of struct mnttabs in "mnt_arrayp" and the number of elements in the
228 229 * array in "nelemp".
229 230 */
230 231 static int
231 232 build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab,
232 233 struct mnttab **mnt_arrayp, uint_t *nelemp)
233 234 {
234 235 struct mnttab mnt;
235 236 struct mnttab *mnts;
236 237 struct mnttab *mnp;
237 238 uint_t nmnt;
238 239
239 240 rewind(mnttab);
240 241 resetmnttab(mnttab);
241 242 nmnt = 0;
242 243 mnts = NULL;
243 244 while (getmntent(mnttab, &mnt) == 0) {
244 245 struct mnttab *tmp_array;
245 246
246 247 if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0)
247 248 continue;
248 249 if (nmnt % MNTTAB_HUNK == 0) {
249 250 tmp_array = realloc(mnts,
250 251 (nmnt + MNTTAB_HUNK) * sizeof (*mnts));
251 252 if (tmp_array == NULL) {
252 253 free_mnttable(mnts, nmnt);
253 254 return (-1);
254 255 }
255 256 mnts = tmp_array;
256 257 }
257 258 mnp = &mnts[nmnt++];
258 259
259 260 /*
260 261 * Zero out any fields we're not using.
261 262 */
262 263 (void) memset(mnp, 0, sizeof (*mnp));
263 264
264 265 if (mnt.mnt_special != NULL)
265 266 mnp->mnt_special = strdup(mnt.mnt_special);
266 267 if (mnt.mnt_mntopts != NULL)
267 268 mnp->mnt_mntopts = strdup(mnt.mnt_mntopts);
268 269 mnp->mnt_mountp = strdup(mnt.mnt_mountp);
269 270 mnp->mnt_fstype = strdup(mnt.mnt_fstype);
270 271 if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) ||
271 272 (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) ||
272 273 mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) {
273 274 zerror(zlogp, B_TRUE, "memory allocation failed");
274 275 free_mnttable(mnts, nmnt);
275 276 return (-1);
276 277 }
277 278 }
278 279 *mnt_arrayp = mnts;
279 280 *nelemp = nmnt;
280 281 return (0);
281 282 }
282 283
283 284 /*
284 285 * This is an optimization. The resolve_lofs function is used quite frequently
285 286 * to manipulate file paths, and on a machine with a large number of zones,
286 287 * there will be a huge number of mounted file systems. Thus, we trigger a
287 288 * reread of the list of mount points
288 289 */
289 290 static void
290 291 lofs_discard_mnttab(void)
291 292 {
292 293 free_mnttable(resolve_lofs_mnts,
293 294 resolve_lofs_mnt_max - resolve_lofs_mnts);
294 295 resolve_lofs_mnts = resolve_lofs_mnt_max = NULL;
295 296 }
296 297
297 298 static int
298 299 lofs_read_mnttab(zlog_t *zlogp)
299 300 {
300 301 FILE *mnttab;
301 302 uint_t nmnts;
302 303
303 304 if ((mnttab = fopen(MNTTAB, "r")) == NULL)
304 305 return (-1);
305 306 if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts,
306 307 &nmnts) == -1) {
307 308 (void) fclose(mnttab);
308 309 return (-1);
309 310 }
310 311 (void) fclose(mnttab);
311 312 resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts;
312 313 return (0);
313 314 }
314 315
315 316 /*
316 317 * This function loops over potential loopback mounts and symlinks in a given
317 318 * path and resolves them all down to an absolute path.
318 319 */
319 320 void
320 321 resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen)
321 322 {
322 323 int len, arlen;
323 324 const char *altroot;
324 325 char tmppath[MAXPATHLEN];
325 326 boolean_t outside_altroot;
326 327
327 328 if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1)
328 329 return;
329 330 tmppath[len] = '\0';
330 331 (void) strlcpy(path, tmppath, sizeof (tmppath));
331 332
332 333 /* This happens once per zoneadmd operation. */
333 334 if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
334 335 return;
335 336
336 337 altroot = zonecfg_get_root();
337 338 arlen = strlen(altroot);
338 339 outside_altroot = B_FALSE;
339 340 for (;;) {
340 341 struct mnttab *mnp;
341 342
342 343 /* Search in reverse order to find longest match */
343 344 for (mnp = resolve_lofs_mnt_max - 1; mnp >= resolve_lofs_mnts;
344 345 mnp--) {
345 346 if (mnp->mnt_fstype == NULL ||
346 347 mnp->mnt_mountp == NULL ||
347 348 mnp->mnt_special == NULL)
348 349 continue;
349 350 len = strlen(mnp->mnt_mountp);
350 351 if (strncmp(mnp->mnt_mountp, path, len) == 0 &&
351 352 (path[len] == '/' || path[len] == '\0'))
352 353 break;
353 354 }
354 355 if (mnp < resolve_lofs_mnts)
355 356 break;
356 357 /* If it's not a lofs then we're done */
357 358 if (strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0)
358 359 break;
359 360 if (outside_altroot) {
360 361 char *cp;
361 362 int olen = sizeof (MNTOPT_RO) - 1;
362 363
363 364 /*
364 365 * If we run into a read-only mount outside of the
365 366 * alternate root environment, then the user doesn't
366 367 * want this path to be made read-write.
367 368 */
368 369 if (mnp->mnt_mntopts != NULL &&
369 370 (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) !=
370 371 NULL &&
371 372 (cp == mnp->mnt_mntopts || cp[-1] == ',') &&
372 373 (cp[olen] == '\0' || cp[olen] == ',')) {
373 374 break;
374 375 }
375 376 } else if (arlen > 0 &&
376 377 (strncmp(mnp->mnt_special, altroot, arlen) != 0 ||
377 378 (mnp->mnt_special[arlen] != '\0' &&
378 379 mnp->mnt_special[arlen] != '/'))) {
379 380 outside_altroot = B_TRUE;
380 381 }
381 382 /* use temporary buffer because new path might be longer */
382 383 (void) snprintf(tmppath, sizeof (tmppath), "%s%s",
383 384 mnp->mnt_special, path + len);
384 385 if ((len = resolvepath(tmppath, path, pathlen)) == -1)
385 386 break;
386 387 path[len] = '\0';
387 388 }
388 389 }
389 390
390 391 /*
391 392 * For a regular mount, check if a replacement lofs mount is needed because the
392 393 * referenced device is already mounted somewhere.
393 394 */
394 395 static int
395 396 check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr)
396 397 {
397 398 struct mnttab *mnp;
398 399 zone_fsopt_t *optptr, *onext;
399 400
400 401 /* This happens once per zoneadmd operation. */
401 402 if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
402 403 return (-1);
403 404
404 405 /*
405 406 * If this special node isn't already in use, then it's ours alone;
406 407 * no need to worry about conflicting mounts.
407 408 */
408 409 for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
409 410 mnp++) {
410 411 if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0)
411 412 break;
412 413 }
413 414 if (mnp >= resolve_lofs_mnt_max)
414 415 return (0);
415 416
416 417 /*
417 418 * Convert this duplicate mount into a lofs mount.
418 419 */
419 420 (void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp,
420 421 sizeof (fsptr->zone_fs_special));
421 422 (void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS,
422 423 sizeof (fsptr->zone_fs_type));
423 424 fsptr->zone_fs_raw[0] = '\0';
424 425
425 426 /*
426 427 * Discard all but one of the original options and set that to our
427 428 * default set of options used for resources.
428 429 */
429 430 optptr = fsptr->zone_fs_options;
430 431 if (optptr == NULL) {
431 432 optptr = malloc(sizeof (*optptr));
432 433 if (optptr == NULL) {
433 434 zerror(zlogp, B_TRUE, "cannot mount %s",
434 435 fsptr->zone_fs_dir);
435 436 return (-1);
436 437 }
437 438 } else {
438 439 while ((onext = optptr->zone_fsopt_next) != NULL) {
439 440 optptr->zone_fsopt_next = onext->zone_fsopt_next;
440 441 free(onext);
441 442 }
442 443 }
443 444 (void) strcpy(optptr->zone_fsopt_opt, RESOURCE_DEFAULT_OPTS);
444 445 optptr->zone_fsopt_next = NULL;
445 446 fsptr->zone_fs_options = optptr;
446 447 return (0);
447 448 }
448 449
449 450 int
450 451 make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode,
451 452 uid_t userid, gid_t groupid)
452 453 {
453 454 char path[MAXPATHLEN];
454 455 struct stat st;
455 456
456 457 if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) >
457 458 sizeof (path)) {
458 459 zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix,
459 460 subdir);
460 461 return (-1);
461 462 }
462 463
463 464 if (lstat(path, &st) == 0) {
464 465 /*
465 466 * We don't check the file mode since presumably the zone
466 467 * administrator may have had good reason to change the mode,
467 468 * and we don't need to second guess him.
468 469 */
469 470 if (!S_ISDIR(st.st_mode)) {
470 471 if (S_ISREG(st.st_mode)) {
471 472 /*
472 473 * Allow readonly mounts of /etc/ files; this
473 474 * is needed most by Trusted Extensions.
474 475 */
475 476 if (strncmp(subdir, "/etc/",
476 477 strlen("/etc/")) != 0) {
477 478 zerror(zlogp, B_FALSE,
478 479 "%s is not in /etc", path);
479 480 return (-1);
480 481 }
481 482 } else {
482 483 zerror(zlogp, B_FALSE,
483 484 "%s is not a directory", path);
484 485 return (-1);
485 486 }
486 487 }
487 488 return (0);
488 489 }
489 490
490 491 if (mkdirp(path, mode) != 0) {
491 492 if (errno == EROFS)
492 493 zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on "
493 494 "a read-only file system in this local zone.\nMake "
494 495 "sure %s exists in the global zone.", path, subdir);
495 496 else
496 497 zerror(zlogp, B_TRUE, "mkdirp of %s failed", path);
497 498 return (-1);
498 499 }
499 500
500 501 (void) chown(path, userid, groupid);
501 502 return (0);
502 503 }
503 504
504 505 static void
505 506 free_remote_fstypes(char **types)
506 507 {
507 508 uint_t i;
508 509
509 510 if (types == NULL)
510 511 return;
511 512 for (i = 0; types[i] != NULL; i++)
512 513 free(types[i]);
513 514 free(types);
514 515 }
515 516
516 517 static char **
517 518 get_remote_fstypes(zlog_t *zlogp)
518 519 {
519 520 char **types = NULL;
520 521 FILE *fp;
521 522 char buf[MAXPATHLEN];
522 523 char fstype[MAXPATHLEN];
523 524 uint_t lines = 0;
524 525 uint_t i;
525 526
526 527 if ((fp = fopen(DFSTYPES, "r")) == NULL) {
527 528 zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES);
528 529 return (NULL);
529 530 }
530 531 /*
531 532 * Count the number of lines
532 533 */
533 534 while (fgets(buf, sizeof (buf), fp) != NULL)
534 535 lines++;
535 536 if (lines == 0) /* didn't read anything; empty file */
536 537 goto out;
537 538 rewind(fp);
538 539 /*
539 540 * Allocate enough space for a NULL-terminated array.
540 541 */
541 542 types = calloc(lines + 1, sizeof (char *));
542 543 if (types == NULL) {
543 544 zerror(zlogp, B_TRUE, "memory allocation failed");
544 545 goto out;
545 546 }
546 547 i = 0;
547 548 while (fgets(buf, sizeof (buf), fp) != NULL) {
548 549 /* LINTED - fstype is big enough to hold buf */
549 550 if (sscanf(buf, "%s", fstype) == 0) {
550 551 zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES);
551 552 free_remote_fstypes(types);
552 553 types = NULL;
553 554 goto out;
554 555 }
555 556 types[i] = strdup(fstype);
556 557 if (types[i] == NULL) {
557 558 zerror(zlogp, B_TRUE, "memory allocation failed");
558 559 free_remote_fstypes(types);
559 560 types = NULL;
560 561 goto out;
561 562 }
562 563 i++;
563 564 }
564 565 out:
565 566 (void) fclose(fp);
566 567 return (types);
567 568 }
568 569
569 570 static boolean_t
570 571 is_remote_fstype(const char *fstype, char *const *remote_fstypes)
571 572 {
572 573 uint_t i;
573 574
574 575 if (remote_fstypes == NULL)
575 576 return (B_FALSE);
576 577 for (i = 0; remote_fstypes[i] != NULL; i++) {
577 578 if (strcmp(remote_fstypes[i], fstype) == 0)
578 579 return (B_TRUE);
579 580 }
580 581 return (B_FALSE);
581 582 }
582 583
583 584 /*
584 585 * This converts a zone root path (normally of the form .../root) to a Live
585 586 * Upgrade scratch zone root (of the form .../lu).
586 587 */
587 588 static void
588 589 root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved)
589 590 {
590 591 if (!isresolved && zonecfg_in_alt_root())
591 592 resolve_lofs(zlogp, zroot, zrootlen);
592 593 (void) strcpy(strrchr(zroot, '/') + 1, "lu");
593 594 }
594 595
595 596 /*
596 597 * The general strategy for unmounting filesystems is as follows:
597 598 *
598 599 * - Remote filesystems may be dead, and attempting to contact them as
599 600 * part of a regular unmount may hang forever; we want to always try to
600 601 * forcibly unmount such filesystems and only fall back to regular
601 602 * unmounts if the filesystem doesn't support forced unmounts.
602 603 *
603 604 * - We don't want to unnecessarily corrupt metadata on local
604 605 * filesystems (ie UFS), so we want to start off with graceful unmounts,
605 606 * and only escalate to doing forced unmounts if we get stuck.
606 607 *
607 608 * We start off walking backwards through the mount table. This doesn't
608 609 * give us strict ordering but ensures that we try to unmount submounts
609 610 * first. We thus limit the number of failed umount2(2) calls.
610 611 *
611 612 * The mechanism for determining if we're stuck is to count the number
612 613 * of failed unmounts each iteration through the mount table. This
613 614 * gives us an upper bound on the number of filesystems which remain
614 615 * mounted (autofs trigger nodes are dealt with separately). If at the
615 616 * end of one unmount+autofs_cleanup cycle we still have the same number
616 617 * of mounts that we started out with, we're stuck and try a forced
617 618 * unmount. If that fails (filesystem doesn't support forced unmounts)
618 619 * then we bail and are unable to teardown the zone. If it succeeds,
619 620 * we're no longer stuck so we continue with our policy of trying
620 621 * graceful mounts first.
621 622 *
622 623 * Zone must be down (ie, no processes or threads active).
623 624 */
624 625 static int
625 626 unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd)
626 627 {
627 628 int error = 0;
628 629 FILE *mnttab;
629 630 struct mnttab *mnts;
630 631 uint_t nmnt;
631 632 char zroot[MAXPATHLEN + 1];
632 633 size_t zrootlen;
633 634 uint_t oldcount = UINT_MAX;
634 635 boolean_t stuck = B_FALSE;
635 636 char **remote_fstypes = NULL;
636 637
637 638 if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
638 639 zerror(zlogp, B_FALSE, "unable to determine zone root");
639 640 return (-1);
640 641 }
641 642 if (unmount_cmd)
642 643 root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
643 644
644 645 (void) strcat(zroot, "/");
645 646 zrootlen = strlen(zroot);
646 647
647 648 /*
648 649 * For Trusted Extensions unmount each higher level zone's mount
649 650 * of our zone's /export/home
650 651 */
651 652 if (!unmount_cmd)
652 653 tsol_unmounts(zlogp, zone_name);
653 654
654 655 if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
655 656 zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB);
656 657 return (-1);
657 658 }
658 659 /*
659 660 * Use our hacky mntfs ioctl so we see everything, even mounts with
660 661 * MS_NOMNTTAB.
661 662 */
662 663 if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) {
663 664 zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB);
664 665 error++;
665 666 goto out;
666 667 }
667 668
668 669 /*
669 670 * Build the list of remote fstypes so we know which ones we
670 671 * should forcibly unmount.
671 672 */
672 673 remote_fstypes = get_remote_fstypes(zlogp);
673 674 for (; /* ever */; ) {
674 675 uint_t newcount = 0;
675 676 boolean_t unmounted;
676 677 struct mnttab *mnp;
677 678 char *path;
678 679 uint_t i;
679 680
680 681 mnts = NULL;
681 682 nmnt = 0;
682 683 /*
683 684 * MNTTAB gives us a way to walk through mounted
684 685 * filesystems; we need to be able to walk them in
685 686 * reverse order, so we build a list of all mounted
686 687 * filesystems.
687 688 */
688 689 if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts,
689 690 &nmnt) != 0) {
690 691 error++;
691 692 goto out;
692 693 }
693 694 for (i = 0; i < nmnt; i++) {
694 695 mnp = &mnts[nmnt - i - 1]; /* access in reverse order */
695 696 path = mnp->mnt_mountp;
696 697 unmounted = B_FALSE;
697 698 /*
698 699 * Try forced unmount first for remote filesystems.
699 700 *
700 701 * Not all remote filesystems support forced unmounts,
701 702 * so if this fails (ENOTSUP) we'll continue on
702 703 * and try a regular unmount.
703 704 */
704 705 if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) {
705 706 if (umount2(path, MS_FORCE) == 0)
706 707 unmounted = B_TRUE;
707 708 }
708 709 /*
709 710 * Try forced unmount if we're stuck.
710 711 */
711 712 if (stuck) {
712 713 if (umount2(path, MS_FORCE) == 0) {
713 714 unmounted = B_TRUE;
714 715 stuck = B_FALSE;
715 716 } else {
716 717 /*
717 718 * The first failure indicates a
718 719 * mount we won't be able to get
719 720 * rid of automatically, so we
720 721 * bail.
721 722 */
722 723 error++;
723 724 zerror(zlogp, B_FALSE,
724 725 "unable to unmount '%s'", path);
725 726 free_mnttable(mnts, nmnt);
726 727 goto out;
727 728 }
728 729 }
729 730 /*
730 731 * Try regular unmounts for everything else.
731 732 */
732 733 if (!unmounted && umount2(path, 0) != 0)
733 734 newcount++;
734 735 }
735 736 free_mnttable(mnts, nmnt);
736 737
737 738 if (newcount == 0)
738 739 break;
739 740 if (newcount >= oldcount) {
740 741 /*
741 742 * Last round didn't unmount anything; we're stuck and
742 743 * should start trying forced unmounts.
743 744 */
744 745 stuck = B_TRUE;
745 746 }
746 747 oldcount = newcount;
747 748
748 749 /*
749 750 * Autofs doesn't let you unmount its trigger nodes from
750 751 * userland so we have to tell the kernel to cleanup for us.
751 752 */
752 753 if (autofs_cleanup(zoneid) != 0) {
753 754 zerror(zlogp, B_TRUE, "unable to remove autofs nodes");
754 755 error++;
755 756 goto out;
756 757 }
757 758 }
758 759
759 760 out:
760 761 free_remote_fstypes(remote_fstypes);
761 762 (void) fclose(mnttab);
762 763 return (error ? -1 : 0);
763 764 }
764 765
765 766 static int
766 767 fs_compare(const void *m1, const void *m2)
767 768 {
768 769 struct zone_fstab *i = (struct zone_fstab *)m1;
769 770 struct zone_fstab *j = (struct zone_fstab *)m2;
770 771
771 772 return (strcmp(i->zone_fs_dir, j->zone_fs_dir));
772 773 }
773 774
774 775 /*
775 776 * Fork and exec (and wait for) the mentioned binary with the provided
776 777 * arguments. Returns (-1) if something went wrong with fork(2) or exec(2),
777 778 * returns the exit status otherwise.
778 779 *
779 780 * If we were unable to exec the provided pathname (for whatever
780 781 * reason), we return the special token ZEXIT_EXEC. The current value
781 782 * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the
782 783 * consumers of this function; any future consumers must make sure this
783 784 * remains the case.
784 785 */
785 786 static int
786 787 forkexec(zlog_t *zlogp, const char *path, char *const argv[])
787 788 {
788 789 pid_t child_pid;
789 790 int child_status = 0;
790 791
791 792 /*
792 793 * Do not let another thread localize a message while we are forking.
793 794 */
794 795 (void) mutex_lock(&msglock);
795 796 child_pid = fork();
796 797 (void) mutex_unlock(&msglock);
797 798 if (child_pid == -1) {
798 799 zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]);
799 800 return (-1);
800 801 } else if (child_pid == 0) {
801 802 closefrom(0);
802 803 /* redirect stdin, stdout & stderr to /dev/null */
803 804 (void) open("/dev/null", O_RDONLY); /* stdin */
804 805 (void) open("/dev/null", O_WRONLY); /* stdout */
805 806 (void) open("/dev/null", O_WRONLY); /* stderr */
806 807 (void) execv(path, argv);
807 808 /*
808 809 * Since we are in the child, there is no point calling zerror()
809 810 * since there is nobody waiting to consume it. So exit with a
810 811 * special code that the parent will recognize and call zerror()
811 812 * accordingly.
812 813 */
813 814
814 815 _exit(ZEXIT_EXEC);
815 816 } else {
816 817 (void) waitpid(child_pid, &child_status, 0);
817 818 }
818 819
819 820 if (WIFSIGNALED(child_status)) {
820 821 zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
821 822 "signal %d", path, WTERMSIG(child_status));
822 823 return (-1);
823 824 }
824 825 assert(WIFEXITED(child_status));
825 826 if (WEXITSTATUS(child_status) == ZEXIT_EXEC) {
826 827 zerror(zlogp, B_FALSE, "failed to exec %s", path);
827 828 return (-1);
828 829 }
829 830 return (WEXITSTATUS(child_status));
830 831 }
831 832
832 833 static int
833 834 isregfile(const char *path)
834 835 {
835 836 struct stat64 st;
836 837
837 838 if (stat64(path, &st) == -1)
838 839 return (-1);
839 840
840 841 return (S_ISREG(st.st_mode));
841 842 }
842 843
843 844 static int
844 845 dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev)
845 846 {
846 847 char cmdbuf[MAXPATHLEN];
847 848 char *argv[5];
848 849 int status;
849 850
850 851 /*
851 852 * We could alternatively have called /usr/sbin/fsck -F <fstype>, but
852 853 * that would cost us an extra fork/exec without buying us anything.
853 854 */
854 855 if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype)
855 856 >= sizeof (cmdbuf)) {
856 857 zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
857 858 return (-1);
858 859 }
859 860
860 861 /*
861 862 * If it doesn't exist, that's OK: we verified this previously
862 863 * in zoneadm.
863 864 */
864 865 if (isregfile(cmdbuf) == -1)
865 866 return (0);
866 867
867 868 argv[0] = "fsck";
868 869 argv[1] = "-o";
869 870 argv[2] = "p";
870 871 argv[3] = (char *)rawdev;
871 872 argv[4] = NULL;
872 873
873 874 status = forkexec(zlogp, cmdbuf, argv);
874 875 if (status == 0 || status == -1)
875 876 return (status);
876 877 zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; "
877 878 "run fsck manually", rawdev, status);
878 879 return (-1);
879 880 }
880 881
881 882 static int
882 883 domount(zlog_t *zlogp, const char *fstype, const char *opts,
883 884 const char *special, const char *directory)
884 885 {
885 886 char cmdbuf[MAXPATHLEN];
886 887 char *argv[6];
887 888 int status;
888 889
889 890 /*
890 891 * We could alternatively have called /usr/sbin/mount -F <fstype>, but
891 892 * that would cost us an extra fork/exec without buying us anything.
892 893 */
893 894 if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype)
894 895 >= sizeof (cmdbuf)) {
895 896 zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
896 897 return (-1);
897 898 }
898 899 argv[0] = "mount";
899 900 if (opts[0] == '\0') {
900 901 argv[1] = (char *)special;
901 902 argv[2] = (char *)directory;
902 903 argv[3] = NULL;
903 904 } else {
904 905 argv[1] = "-o";
905 906 argv[2] = (char *)opts;
906 907 argv[3] = (char *)special;
907 908 argv[4] = (char *)directory;
908 909 argv[5] = NULL;
909 910 }
910 911
911 912 status = forkexec(zlogp, cmdbuf, argv);
912 913 if (status == 0 || status == -1)
913 914 return (status);
914 915 if (opts[0] == '\0')
915 916 zerror(zlogp, B_FALSE, "\"%s %s %s\" "
916 917 "failed with exit code %d",
917 918 cmdbuf, special, directory, status);
918 919 else
919 920 zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" "
920 921 "failed with exit code %d",
921 922 cmdbuf, opts, special, directory, status);
922 923 return (-1);
923 924 }
924 925
925 926 /*
926 927 * Check if a given mount point path exists.
927 928 * If it does, make sure it doesn't contain any symlinks.
928 929 * Note that if "leaf" is false we're checking an intermediate
929 930 * component of the mount point path, so it must be a directory.
930 931 * If "leaf" is true, then we're checking the entire mount point
931 932 * path, so the mount point itself can be anything aside from a
932 933 * symbolic link.
933 934 *
934 935 * If the path is invalid then a negative value is returned. If the
935 936 * path exists and is a valid mount point path then 0 is returned.
936 937 * If the path doesn't exist return a positive value.
937 938 */
938 939 static int
939 940 valid_mount_point(zlog_t *zlogp, const char *path, const boolean_t leaf)
940 941 {
941 942 struct stat statbuf;
942 943 char respath[MAXPATHLEN];
943 944 int res;
944 945
945 946 if (lstat(path, &statbuf) != 0) {
946 947 if (errno == ENOENT)
947 948 return (1);
948 949 zerror(zlogp, B_TRUE, "can't stat %s", path);
949 950 return (-1);
950 951 }
951 952 if (S_ISLNK(statbuf.st_mode)) {
952 953 zerror(zlogp, B_FALSE, "%s is a symlink", path);
953 954 return (-1);
954 955 }
955 956 if (!leaf && !S_ISDIR(statbuf.st_mode)) {
956 957 zerror(zlogp, B_FALSE, "%s is not a directory", path);
957 958 return (-1);
958 959 }
959 960 if ((res = resolvepath(path, respath, sizeof (respath))) == -1) {
960 961 zerror(zlogp, B_TRUE, "unable to resolve path %s", path);
961 962 return (-1);
962 963 }
963 964 respath[res] = '\0';
964 965 if (strcmp(path, respath) != 0) {
965 966 /*
966 967 * We don't like ".."s, "."s, or "//"s throwing us off
967 968 */
968 969 zerror(zlogp, B_FALSE, "%s is not a canonical path", path);
969 970 return (-1);
970 971 }
971 972 return (0);
972 973 }
973 974
974 975 /*
975 976 * Validate a mount point path. A valid mount point path is an
976 977 * absolute path that either doesn't exist, or, if it does exists it
977 978 * must be an absolute canonical path that doesn't have any symbolic
978 979 * links in it. The target of a mount point path can be any filesystem
979 980 * object. (Different filesystems can support different mount points,
980 981 * for example "lofs" and "mntfs" both support files and directories
981 982 * while "ufs" just supports directories.)
982 983 *
983 984 * If the path is invalid then a negative value is returned. If the
984 985 * path exists and is a valid mount point path then 0 is returned.
985 986 * If the path doesn't exist return a positive value.
986 987 */
987 988 int
988 989 valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *spec,
989 990 const char *dir, const char *fstype)
990 991 {
991 992 char abspath[MAXPATHLEN], *slashp, *slashp_next;
992 993 int rv;
993 994
994 995 /*
995 996 * Sanity check the target mount point path.
996 997 * It must be a non-null string that starts with a '/'.
997 998 */
998 999 if (dir[0] != '/') {
999 1000 /* Something went wrong. */
1000 1001 zerror(zlogp, B_FALSE, "invalid mount directory, "
1001 1002 "type: \"%s\", special: \"%s\", dir: \"%s\"",
1002 1003 fstype, spec, dir);
1003 1004 return (-1);
1004 1005 }
1005 1006
1006 1007 /*
1007 1008 * Join rootpath and dir. Make sure abspath ends with '/', this
1008 1009 * is added to all paths (even non-directory paths) to allow us
1009 1010 * to detect the end of paths below. If the path already ends
1010 1011 * in a '/', then that's ok too (although we'll fail the
1011 1012 * cannonical path check in valid_mount_point()).
1012 1013 */
1013 1014 if (snprintf(abspath, sizeof (abspath),
1014 1015 "%s%s/", rootpath, dir) >= sizeof (abspath)) {
1015 1016 zerror(zlogp, B_FALSE, "pathname %s%s is too long",
1016 1017 rootpath, dir);
1017 1018 return (-1);
1018 1019 }
1019 1020
1020 1021 /*
1021 1022 * Starting with rootpath, verify the mount path one component
1022 1023 * at a time. Continue until we've evaluated all of abspath.
1023 1024 */
1024 1025 slashp = &abspath[strlen(rootpath)];
1025 1026 assert(*slashp == '/');
1026 1027 do {
1027 1028 slashp_next = strchr(slashp + 1, '/');
1028 1029 *slashp = '\0';
1029 1030 if (slashp_next != NULL) {
1030 1031 /* This is an intermediary mount path component. */
1031 1032 rv = valid_mount_point(zlogp, abspath, B_FALSE);
1032 1033 } else {
1033 1034 /* This is the last component of the mount path. */
1034 1035 rv = valid_mount_point(zlogp, abspath, B_TRUE);
1035 1036 }
1036 1037 if (rv < 0)
1037 1038 return (rv);
1038 1039 *slashp = '/';
1039 1040 } while ((slashp = slashp_next) != NULL);
1040 1041 return (rv);
1041 1042 }
1042 1043
1043 1044 static int
1044 1045 mount_one_dev_device_cb(void *arg, const char *match, const char *name)
1045 1046 {
1046 1047 di_prof_t prof = arg;
1047 1048
1048 1049 if (name == NULL)
1049 1050 return (di_prof_add_dev(prof, match));
1050 1051 return (di_prof_add_map(prof, match, name));
1051 1052 }
1052 1053
1053 1054 static int
1054 1055 mount_one_dev_symlink_cb(void *arg, const char *source, const char *target)
1055 1056 {
1056 1057 di_prof_t prof = arg;
1057 1058
1058 1059 return (di_prof_add_symlink(prof, source, target));
1059 1060 }
1060 1061
1061 1062 int
1062 1063 vplat_get_iptype(zlog_t *zlogp, zone_iptype_t *iptypep)
1063 1064 {
1064 1065 zone_dochandle_t handle;
1065 1066
1066 1067 if ((handle = zonecfg_init_handle()) == NULL) {
1067 1068 zerror(zlogp, B_TRUE, "getting zone configuration handle");
1068 1069 return (-1);
1069 1070 }
1070 1071 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
1071 1072 zerror(zlogp, B_FALSE, "invalid configuration");
1072 1073 zonecfg_fini_handle(handle);
1073 1074 return (-1);
1074 1075 }
1075 1076 if (zonecfg_get_iptype(handle, iptypep) != Z_OK) {
1076 1077 zerror(zlogp, B_FALSE, "invalid ip-type configuration");
1077 1078 zonecfg_fini_handle(handle);
1078 1079 return (-1);
1079 1080 }
1080 1081 zonecfg_fini_handle(handle);
1081 1082 return (0);
1082 1083 }
1083 1084
1084 1085 /*
1085 1086 * Apply the standard lists of devices/symlinks/mappings and the user-specified
1086 1087 * list of devices (via zonecfg) to the /dev filesystem. The filesystem will
1087 1088 * use these as a profile/filter to determine what exists in /dev.
1088 1089 */
1089 1090 static int
1090 1091 mount_one_dev(zlog_t *zlogp, char *devpath, zone_mnt_t mount_cmd)
1091 1092 {
1092 1093 char brand[MAXNAMELEN];
1093 1094 zone_dochandle_t handle = NULL;
1094 1095 brand_handle_t bh = NULL;
1095 1096 struct zone_devtab ztab;
1096 1097 di_prof_t prof = NULL;
1097 1098 int err;
1098 1099 int retval = -1;
1099 1100 zone_iptype_t iptype;
1100 1101 const char *curr_iptype;
1101 1102
1102 1103 if (di_prof_init(devpath, &prof)) {
1103 1104 zerror(zlogp, B_TRUE, "failed to initialize profile");
1104 1105 goto cleanup;
1105 1106 }
1106 1107
1107 1108 /*
1108 1109 * Get a handle to the brand info for this zone.
1109 1110 * If we are mounting the zone, then we must always use the default
1110 1111 * brand device mounts.
1111 1112 */
1112 1113 if (ALT_MOUNT(mount_cmd)) {
1113 1114 (void) strlcpy(brand, default_brand, sizeof (brand));
1114 1115 } else {
1115 1116 (void) strlcpy(brand, brand_name, sizeof (brand));
1116 1117 }
1117 1118
1118 1119 if ((bh = brand_open(brand)) == NULL) {
1119 1120 zerror(zlogp, B_FALSE, "unable to determine zone brand");
1120 1121 goto cleanup;
1121 1122 }
1122 1123
1123 1124 if (vplat_get_iptype(zlogp, &iptype) < 0) {
1124 1125 zerror(zlogp, B_TRUE, "unable to determine ip-type");
1125 1126 goto cleanup;
1126 1127 }
1127 1128 switch (iptype) {
1128 1129 case ZS_SHARED:
1129 1130 curr_iptype = "shared";
1130 1131 break;
1131 1132 case ZS_EXCLUSIVE:
1132 1133 curr_iptype = "exclusive";
1133 1134 break;
1134 1135 }
1135 1136
1136 1137 if (brand_platform_iter_devices(bh, zone_name,
1137 1138 mount_one_dev_device_cb, prof, curr_iptype) != 0) {
1138 1139 zerror(zlogp, B_TRUE, "failed to add standard device");
1139 1140 goto cleanup;
1140 1141 }
1141 1142
1142 1143 if (brand_platform_iter_link(bh,
1143 1144 mount_one_dev_symlink_cb, prof) != 0) {
1144 1145 zerror(zlogp, B_TRUE, "failed to add standard symlink");
1145 1146 goto cleanup;
1146 1147 }
1147 1148
1148 1149 /* Add user-specified devices and directories */
1149 1150 if ((handle = zonecfg_init_handle()) == NULL) {
1150 1151 zerror(zlogp, B_FALSE, "can't initialize zone handle");
1151 1152 goto cleanup;
1152 1153 }
1153 1154 if (err = zonecfg_get_handle(zone_name, handle)) {
1154 1155 zerror(zlogp, B_FALSE, "can't get handle for zone "
1155 1156 "%s: %s", zone_name, zonecfg_strerror(err));
1156 1157 goto cleanup;
1157 1158 }
1158 1159 if (err = zonecfg_setdevent(handle)) {
1159 1160 zerror(zlogp, B_FALSE, "%s: %s", zone_name,
1160 1161 zonecfg_strerror(err));
1161 1162 goto cleanup;
1162 1163 }
1163 1164 while (zonecfg_getdevent(handle, &ztab) == Z_OK) {
1164 1165 if (di_prof_add_dev(prof, ztab.zone_dev_match)) {
1165 1166 zerror(zlogp, B_TRUE, "failed to add "
1166 1167 "user-specified device");
1167 1168 goto cleanup;
1168 1169 }
1169 1170 }
1170 1171 (void) zonecfg_enddevent(handle);
1171 1172
1172 1173 /* Send profile to kernel */
1173 1174 if (di_prof_commit(prof)) {
1174 1175 zerror(zlogp, B_TRUE, "failed to commit profile");
1175 1176 goto cleanup;
1176 1177 }
1177 1178
1178 1179 retval = 0;
1179 1180
1180 1181 cleanup:
1181 1182 if (bh != NULL)
1182 1183 brand_close(bh);
1183 1184 if (handle != NULL)
1184 1185 zonecfg_fini_handle(handle);
1185 1186 if (prof)
1186 1187 di_prof_fini(prof);
1187 1188 return (retval);
1188 1189 }
1189 1190
1190 1191 static int
1191 1192 mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath,
1192 1193 zone_mnt_t mount_cmd)
1193 1194 {
1194 1195 char path[MAXPATHLEN];
1195 1196 char optstr[MAX_MNTOPT_STR];
1196 1197 zone_fsopt_t *optptr;
1197 1198 int rv;
1198 1199
1199 1200 if ((rv = valid_mount_path(zlogp, rootpath, fsptr->zone_fs_special,
1200 1201 fsptr->zone_fs_dir, fsptr->zone_fs_type)) < 0) {
1201 1202 zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
1202 1203 rootpath, fsptr->zone_fs_dir);
1203 1204 return (-1);
1204 1205 } else if (rv > 0) {
1205 1206 /* The mount point path doesn't exist, create it now. */
1206 1207 if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir,
1207 1208 DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
1208 1209 DEFAULT_DIR_GROUP) != 0) {
1209 1210 zerror(zlogp, B_FALSE, "failed to create mount point");
1210 1211 return (-1);
1211 1212 }
1212 1213
1213 1214 /*
1214 1215 * Now this might seem weird, but we need to invoke
1215 1216 * valid_mount_path() again. Why? Because it checks
1216 1217 * to make sure that the mount point path is canonical,
1217 1218 * which it can only do if the path exists, so now that
1218 1219 * we've created the path we have to verify it again.
1219 1220 */
1220 1221 if ((rv = valid_mount_path(zlogp, rootpath,
1221 1222 fsptr->zone_fs_special, fsptr->zone_fs_dir,
1222 1223 fsptr->zone_fs_type)) < 0) {
1223 1224 zerror(zlogp, B_FALSE,
1224 1225 "%s%s is not a valid mount point",
1225 1226 rootpath, fsptr->zone_fs_dir);
1226 1227 return (-1);
1227 1228 }
1228 1229 }
1229 1230
1230 1231 (void) snprintf(path, sizeof (path), "%s%s", rootpath,
1231 1232 fsptr->zone_fs_dir);
1232 1233
1233 1234 /*
1234 1235 * In general the strategy here is to do just as much verification as
1235 1236 * necessary to avoid crashing or otherwise doing something bad; if the
1236 1237 * administrator initiated the operation via zoneadm(1m), he'll get
1237 1238 * auto-verification which will let him know what's wrong. If he
1238 1239 * modifies the zone configuration of a running zone and doesn't attempt
1239 1240 * to verify that it's OK we won't crash but won't bother trying to be
1240 1241 * too helpful either. zoneadm verify is only a couple keystrokes away.
1241 1242 */
1242 1243 if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) {
1243 1244 zerror(zlogp, B_FALSE, "cannot mount %s on %s: "
1244 1245 "invalid file-system type %s", fsptr->zone_fs_special,
1245 1246 fsptr->zone_fs_dir, fsptr->zone_fs_type);
1246 1247 return (-1);
1247 1248 }
1248 1249
1249 1250 /*
1250 1251 * If we're looking at an alternate root environment, then construct
1251 1252 * read-only loopback mounts as necessary. Note that any special
1252 1253 * paths for lofs zone mounts in an alternate root must have
1253 1254 * already been pre-pended with any alternate root path by the
1254 1255 * time we get here.
1255 1256 */
1256 1257 if (zonecfg_in_alt_root()) {
1257 1258 struct stat64 st;
1258 1259
1259 1260 if (stat64(fsptr->zone_fs_special, &st) != -1 &&
1260 1261 S_ISBLK(st.st_mode)) {
1261 1262 /*
1262 1263 * If we're going to mount a block device we need
1263 1264 * to check if that device is already mounted
1264 1265 * somewhere else, and if so, do a lofs mount
1265 1266 * of the device instead of a direct mount
1266 1267 */
1267 1268 if (check_lofs_needed(zlogp, fsptr) == -1)
1268 1269 return (-1);
1269 1270 } else if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) {
1270 1271 /*
1271 1272 * For lofs mounts, the special node is inside the
1272 1273 * alternate root. We need lofs resolution for
1273 1274 * this case in order to get at the underlying
1274 1275 * read-write path.
1275 1276 */
1276 1277 resolve_lofs(zlogp, fsptr->zone_fs_special,
1277 1278 sizeof (fsptr->zone_fs_special));
1278 1279 }
1279 1280 }
1280 1281
1281 1282 /*
1282 1283 * Run 'fsck -m' if there's a device to fsck.
1283 1284 */
1284 1285 if (fsptr->zone_fs_raw[0] != '\0' &&
1285 1286 dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0) {
1286 1287 return (-1);
1287 1288 } else if (isregfile(fsptr->zone_fs_special) == 1 &&
1288 1289 dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_special) != 0) {
1289 1290 return (-1);
1290 1291 }
1291 1292
1292 1293 /*
1293 1294 * Build up mount option string.
1294 1295 */
1295 1296 optstr[0] = '\0';
1296 1297 if (fsptr->zone_fs_options != NULL) {
1297 1298 (void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt,
1298 1299 sizeof (optstr));
1299 1300 for (optptr = fsptr->zone_fs_options->zone_fsopt_next;
1300 1301 optptr != NULL; optptr = optptr->zone_fsopt_next) {
1301 1302 (void) strlcat(optstr, ",", sizeof (optstr));
1302 1303 (void) strlcat(optstr, optptr->zone_fsopt_opt,
1303 1304 sizeof (optstr));
1304 1305 }
1305 1306 }
1306 1307
1307 1308 if ((rv = domount(zlogp, fsptr->zone_fs_type, optstr,
1308 1309 fsptr->zone_fs_special, path)) != 0)
1309 1310 return (rv);
1310 1311
1311 1312 /*
1312 1313 * The mount succeeded. If this was not a mount of /dev then
1313 1314 * we're done.
1314 1315 */
1315 1316 if (strcmp(fsptr->zone_fs_type, MNTTYPE_DEV) != 0)
1316 1317 return (0);
1317 1318
1318 1319 /*
1319 1320 * We just mounted an instance of a /dev filesystem, so now we
1320 1321 * need to configure it.
1321 1322 */
1322 1323 return (mount_one_dev(zlogp, path, mount_cmd));
1323 1324 }
1324 1325
1325 1326 static void
1326 1327 free_fs_data(struct zone_fstab *fsarray, uint_t nelem)
1327 1328 {
1328 1329 uint_t i;
1329 1330
1330 1331 if (fsarray == NULL)
1331 1332 return;
1332 1333 for (i = 0; i < nelem; i++)
1333 1334 zonecfg_free_fs_option_list(fsarray[i].zone_fs_options);
1334 1335 free(fsarray);
1335 1336 }
1336 1337
1337 1338 /*
1338 1339 * This function initiates the creation of a small Solaris Environment for
1339 1340 * scratch zone. The Environment creation process is split up into two
1340 1341 * functions(build_mounted_pre_var() and build_mounted_post_var()). It
1341 1342 * is done this way because:
1342 1343 * We need to have both /etc and /var in the root of the scratchzone.
1343 1344 * We loopback mount zone's own /etc and /var into the root of the
1344 1345 * scratch zone. Unlike /etc, /var can be a seperate filesystem. So we
1345 1346 * need to delay the mount of /var till the zone's root gets populated.
1346 1347 * So mounting of localdirs[](/etc and /var) have been moved to the
1347 1348 * build_mounted_post_var() which gets called only after the zone
1348 1349 * specific filesystems are mounted.
1349 1350 *
1350 1351 * Note that the scratch zone we set up for updating the zone (Z_MNT_UPDATE)
1351 1352 * does not loopback mount the zone's own /etc and /var into the root of the
1352 1353 * scratch zone.
1353 1354 */
1354 1355 static boolean_t
1355 1356 build_mounted_pre_var(zlog_t *zlogp, char *rootpath,
1356 1357 size_t rootlen, const char *zonepath, char *luroot, size_t lurootlen)
1357 1358 {
1358 1359 char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1359 1360 const char **cpp;
1360 1361 static const char *mkdirs[] = {
1361 1362 "/system", "/system/contract", "/system/object", "/proc",
1362 1363 "/dev", "/tmp", "/a", NULL
1363 1364 };
1364 1365 char *altstr;
1365 1366 FILE *fp;
1366 1367 uuid_t uuid;
1367 1368
1368 1369 resolve_lofs(zlogp, rootpath, rootlen);
1369 1370 (void) snprintf(luroot, lurootlen, "%s/lu", zonepath);
1370 1371 resolve_lofs(zlogp, luroot, lurootlen);
1371 1372 (void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot);
1372 1373 (void) symlink("./usr/bin", tmp);
1373 1374
1374 1375 /*
1375 1376 * These are mostly special mount points; not handled here. (See
1376 1377 * zone_mount_early.)
1377 1378 */
1378 1379 for (cpp = mkdirs; *cpp != NULL; cpp++) {
1379 1380 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1380 1381 if (mkdir(tmp, 0755) != 0) {
1381 1382 zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1382 1383 return (B_FALSE);
1383 1384 }
1384 1385 }
1385 1386 /*
1386 1387 * This is here to support lucopy. If there's an instance of this same
1387 1388 * zone on the current running system, then we mount its root up as
1388 1389 * read-only inside the scratch zone.
1389 1390 */
1390 1391 (void) zonecfg_get_uuid(zone_name, uuid);
1391 1392 altstr = strdup(zonecfg_get_root());
1392 1393 if (altstr == NULL) {
1393 1394 zerror(zlogp, B_TRUE, "memory allocation failed");
1394 1395 return (B_FALSE);
1395 1396 }
1396 1397 zonecfg_set_root("");
1397 1398 (void) strlcpy(tmp, zone_name, sizeof (tmp));
1398 1399 (void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp));
1399 1400 if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK &&
1400 1401 strcmp(fromdir, rootpath) != 0) {
1401 1402 (void) snprintf(tmp, sizeof (tmp), "%s/b", luroot);
1402 1403 if (mkdir(tmp, 0755) != 0) {
1403 1404 zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1404 1405 return (B_FALSE);
1405 1406 }
1406 1407 if (domount(zlogp, MNTTYPE_LOFS, RESOURCE_DEFAULT_OPTS, fromdir,
1407 1408 tmp) != 0) {
1408 1409 zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1409 1410 fromdir);
1410 1411 return (B_FALSE);
1411 1412 }
1412 1413 }
1413 1414 zonecfg_set_root(altstr);
1414 1415 free(altstr);
1415 1416
1416 1417 if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) {
1417 1418 zerror(zlogp, B_TRUE, "cannot open zone mapfile");
1418 1419 return (B_FALSE);
1419 1420 }
1420 1421 (void) ftruncate(fileno(fp), 0);
1421 1422 if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) {
1422 1423 zerror(zlogp, B_TRUE, "cannot add zone mapfile entry");
1423 1424 }
1424 1425 zonecfg_close_scratch(fp);
1425 1426 (void) snprintf(tmp, sizeof (tmp), "%s/a", luroot);
1426 1427 if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0)
1427 1428 return (B_FALSE);
1428 1429 (void) strlcpy(rootpath, tmp, rootlen);
1429 1430 return (B_TRUE);
1430 1431 }
1431 1432
1432 1433
1433 1434 static boolean_t
1434 1435 build_mounted_post_var(zlog_t *zlogp, zone_mnt_t mount_cmd, char *rootpath,
1435 1436 const char *luroot)
1436 1437 {
1437 1438 char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1438 1439 const char **cpp;
1439 1440 const char **loopdirs;
1440 1441 const char **tmpdirs;
1441 1442 static const char *localdirs[] = {
1442 1443 "/etc", "/var", NULL
1443 1444 };
1444 1445 static const char *scr_loopdirs[] = {
1445 1446 "/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform",
1446 1447 "/usr", NULL
1447 1448 };
1448 1449 static const char *upd_loopdirs[] = {
1449 1450 "/etc", "/kernel", "/lib", "/opt", "/platform", "/sbin",
1450 1451 "/usr", "/var", NULL
1451 1452 };
1452 1453 static const char *scr_tmpdirs[] = {
1453 1454 "/tmp", "/var/run", NULL
1454 1455 };
1455 1456 static const char *upd_tmpdirs[] = {
1456 1457 "/tmp", "/var/run", "/var/tmp", NULL
1457 1458 };
1458 1459 struct stat st;
1459 1460
1460 1461 if (mount_cmd == Z_MNT_SCRATCH) {
1461 1462 /*
1462 1463 * These are mounted read-write from the zone undergoing
1463 1464 * upgrade. We must be careful not to 'leak' things from the
1464 1465 * main system into the zone, and this accomplishes that goal.
1465 1466 */
1466 1467 for (cpp = localdirs; *cpp != NULL; cpp++) {
1467 1468 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot,
1468 1469 *cpp);
1469 1470 (void) snprintf(fromdir, sizeof (fromdir), "%s%s",
1470 1471 rootpath, *cpp);
1471 1472 if (mkdir(tmp, 0755) != 0) {
1472 1473 zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1473 1474 return (B_FALSE);
1474 1475 }
1475 1476 if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp)
1476 1477 != 0) {
1477 1478 zerror(zlogp, B_TRUE, "cannot mount %s on %s",
1478 1479 tmp, *cpp);
1479 1480 return (B_FALSE);
1480 1481 }
1481 1482 }
1482 1483 }
1483 1484
1484 1485 if (mount_cmd == Z_MNT_UPDATE)
1485 1486 loopdirs = upd_loopdirs;
1486 1487 else
1487 1488 loopdirs = scr_loopdirs;
1488 1489
1489 1490 /*
1490 1491 * These are things mounted read-only from the running system because
1491 1492 * they contain binaries that must match system.
1492 1493 */
1493 1494 for (cpp = loopdirs; *cpp != NULL; cpp++) {
1494 1495 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1495 1496 if (mkdir(tmp, 0755) != 0) {
1496 1497 if (errno != EEXIST) {
1497 1498 zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1498 1499 return (B_FALSE);
1499 1500 }
1500 1501 if (lstat(tmp, &st) != 0) {
1501 1502 zerror(zlogp, B_TRUE, "cannot stat %s", tmp);
1502 1503 return (B_FALSE);
1503 1504 }
1504 1505 /*
1505 1506 * Ignore any non-directories encountered. These are
1506 1507 * things that have been converted into symlinks
1507 1508 * (/etc/fs and /etc/lib) and no longer need a lofs
1508 1509 * fixup.
1509 1510 */
1510 1511 if (!S_ISDIR(st.st_mode))
1511 1512 continue;
1512 1513 }
1513 1514 if (domount(zlogp, MNTTYPE_LOFS, RESOURCE_DEFAULT_OPTS, *cpp,
1514 1515 tmp) != 0) {
1515 1516 zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1516 1517 *cpp);
1517 1518 return (B_FALSE);
1518 1519 }
1519 1520 }
1520 1521
1521 1522 if (mount_cmd == Z_MNT_UPDATE)
1522 1523 tmpdirs = upd_tmpdirs;
1523 1524 else
1524 1525 tmpdirs = scr_tmpdirs;
1525 1526
1526 1527 /*
1527 1528 * These are things with tmpfs mounted inside.
1528 1529 */
1529 1530 for (cpp = tmpdirs; *cpp != NULL; cpp++) {
1530 1531 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1531 1532 if (mount_cmd == Z_MNT_SCRATCH && mkdir(tmp, 0755) != 0 &&
1532 1533 errno != EEXIST) {
1533 1534 zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1534 1535 return (B_FALSE);
1535 1536 }
1536 1537
1537 1538 /*
1538 1539 * We could set the mode for /tmp when we do the mkdir but
1539 1540 * since that can be modified by the umask we will just set
1540 1541 * the correct mode for /tmp now.
1541 1542 */
1542 1543 if (strcmp(*cpp, "/tmp") == 0 && chmod(tmp, 01777) != 0) {
1543 1544 zerror(zlogp, B_TRUE, "cannot chmod %s", tmp);
1544 1545 return (B_FALSE);
1545 1546 }
1546 1547
1547 1548 if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) {
1548 1549 zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp);
1549 1550 return (B_FALSE);
1550 1551 }
1551 1552 }
1552 1553 return (B_TRUE);
1553 1554 }
1554 1555
1555 1556 typedef struct plat_gmount_cb_data {
1556 1557 zlog_t *pgcd_zlogp;
1557 1558 struct zone_fstab **pgcd_fs_tab;
1558 1559 int *pgcd_num_fs;
1559 1560 } plat_gmount_cb_data_t;
1560 1561
1561 1562 /*
1562 1563 * plat_gmount_cb() is a callback function invoked by libbrand to iterate
1563 1564 * through all global brand platform mounts.
1564 1565 */
1565 1566 int
1566 1567 plat_gmount_cb(void *data, const char *spec, const char *dir,
1567 1568 const char *fstype, const char *opt)
1568 1569 {
1569 1570 plat_gmount_cb_data_t *cp = data;
1570 1571 zlog_t *zlogp = cp->pgcd_zlogp;
1571 1572 struct zone_fstab *fs_ptr = *cp->pgcd_fs_tab;
1572 1573 int num_fs = *cp->pgcd_num_fs;
1573 1574 struct zone_fstab *fsp, *tmp_ptr;
1574 1575
1575 1576 num_fs++;
1576 1577 if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) {
1577 1578 zerror(zlogp, B_TRUE, "memory allocation failed");
1578 1579 return (-1);
1579 1580 }
1580 1581
1581 1582 fs_ptr = tmp_ptr;
1582 1583 fsp = &fs_ptr[num_fs - 1];
1583 1584
1584 1585 /* update the callback struct passed in */
1585 1586 *cp->pgcd_fs_tab = fs_ptr;
1586 1587 *cp->pgcd_num_fs = num_fs;
1587 1588
1588 1589 fsp->zone_fs_raw[0] = '\0';
1589 1590 (void) strlcpy(fsp->zone_fs_special, spec,
1590 1591 sizeof (fsp->zone_fs_special));
1591 1592 (void) strlcpy(fsp->zone_fs_dir, dir, sizeof (fsp->zone_fs_dir));
1592 1593 (void) strlcpy(fsp->zone_fs_type, fstype, sizeof (fsp->zone_fs_type));
1593 1594 fsp->zone_fs_options = NULL;
1594 1595 if ((opt != NULL) &&
1595 1596 (zonecfg_add_fs_option(fsp, (char *)opt) != Z_OK)) {
1596 1597 zerror(zlogp, B_FALSE, "error adding property");
1597 1598 return (-1);
1598 1599 }
1599 1600
1600 1601 return (0);
1601 1602 }
1602 1603
1603 1604 static int
1604 1605 mount_filesystems_fsent(zone_dochandle_t handle, zlog_t *zlogp,
1605 1606 struct zone_fstab **fs_tabp, int *num_fsp, zone_mnt_t mount_cmd)
1606 1607 {
1607 1608 struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
1608 1609 int num_fs;
1609 1610
1610 1611 num_fs = *num_fsp;
1611 1612 fs_ptr = *fs_tabp;
1612 1613
1613 1614 if (zonecfg_setfsent(handle) != Z_OK) {
1614 1615 zerror(zlogp, B_FALSE, "invalid configuration");
1615 1616 return (-1);
1616 1617 }
1617 1618 while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
1618 1619 /*
1619 1620 * ZFS filesystems will not be accessible under an alternate
1620 1621 * root, since the pool will not be known. Ignore them in this
1621 1622 * case.
1622 1623 */
1623 1624 if (ALT_MOUNT(mount_cmd) &&
1624 1625 strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0)
1625 1626 continue;
1626 1627
1627 1628 num_fs++;
1628 1629 if ((tmp_ptr = realloc(fs_ptr,
1629 1630 num_fs * sizeof (*tmp_ptr))) == NULL) {
1630 1631 zerror(zlogp, B_TRUE, "memory allocation failed");
1631 1632 (void) zonecfg_endfsent(handle);
1632 1633 return (-1);
1633 1634 }
1634 1635 /* update the pointers passed in */
1635 1636 *fs_tabp = tmp_ptr;
1636 1637 *num_fsp = num_fs;
1637 1638
1638 1639 fs_ptr = tmp_ptr;
1639 1640 fsp = &fs_ptr[num_fs - 1];
1640 1641 (void) strlcpy(fsp->zone_fs_dir,
1641 1642 fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
1642 1643 (void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw,
1643 1644 sizeof (fsp->zone_fs_raw));
1644 1645 (void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type,
1645 1646 sizeof (fsp->zone_fs_type));
1646 1647 fsp->zone_fs_options = fstab.zone_fs_options;
1647 1648
1648 1649 /*
1649 1650 * For all lofs mounts, make sure that the 'special'
1650 1651 * entry points inside the alternate root. The
1651 1652 * source path for a lofs mount in a given zone needs
1652 1653 * to be relative to the root of the boot environment
1653 1654 * that contains the zone. Note that we don't do this
1654 1655 * for non-lofs mounts since they will have a device
1655 1656 * as a backing store and device paths must always be
1656 1657 * specified relative to the current boot environment.
1657 1658 */
1658 1659 fsp->zone_fs_special[0] = '\0';
1659 1660 if (strcmp(fsp->zone_fs_type, MNTTYPE_LOFS) == 0) {
1660 1661 (void) strlcat(fsp->zone_fs_special, zonecfg_get_root(),
1661 1662 sizeof (fsp->zone_fs_special));
1662 1663 }
1663 1664 (void) strlcat(fsp->zone_fs_special, fstab.zone_fs_special,
1664 1665 sizeof (fsp->zone_fs_special));
1665 1666 }
1666 1667 (void) zonecfg_endfsent(handle);
1667 1668 return (0);
1668 1669 }
1669 1670
1670 1671 static int
1671 1672 mount_filesystems(zlog_t *zlogp, zone_mnt_t mount_cmd)
1672 1673 {
1673 1674 char rootpath[MAXPATHLEN];
1674 1675 char zonepath[MAXPATHLEN];
1675 1676 char brand[MAXNAMELEN];
1676 1677 char luroot[MAXPATHLEN];
1677 1678 int i, num_fs = 0;
1678 1679 struct zone_fstab *fs_ptr = NULL;
1679 1680 zone_dochandle_t handle = NULL;
1680 1681 zone_state_t zstate;
1681 1682 brand_handle_t bh;
1682 1683 plat_gmount_cb_data_t cb;
1683 1684
1684 1685 if (zone_get_state(zone_name, &zstate) != Z_OK ||
1685 1686 (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) {
1686 1687 zerror(zlogp, B_FALSE,
1687 1688 "zone must be in '%s' or '%s' state to mount file-systems",
1688 1689 zone_state_str(ZONE_STATE_READY),
1689 1690 zone_state_str(ZONE_STATE_MOUNTED));
1690 1691 goto bad;
1691 1692 }
1692 1693
1693 1694 if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
1694 1695 zerror(zlogp, B_TRUE, "unable to determine zone path");
1695 1696 goto bad;
1696 1697 }
1697 1698
1698 1699 if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
1699 1700 zerror(zlogp, B_TRUE, "unable to determine zone root");
1700 1701 goto bad;
1701 1702 }
1702 1703
1703 1704 if ((handle = zonecfg_init_handle()) == NULL) {
1704 1705 zerror(zlogp, B_TRUE, "getting zone configuration handle");
1705 1706 goto bad;
1706 1707 }
1707 1708 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK ||
1708 1709 zonecfg_setfsent(handle) != Z_OK) {
1709 1710 zerror(zlogp, B_FALSE, "invalid configuration");
1710 1711 goto bad;
1711 1712 }
1712 1713
1713 1714 /*
1714 1715 * If we are mounting the zone, then we must always use the default
1715 1716 * brand global mounts.
1716 1717 */
1717 1718 if (ALT_MOUNT(mount_cmd)) {
1718 1719 (void) strlcpy(brand, default_brand, sizeof (brand));
1719 1720 } else {
1720 1721 (void) strlcpy(brand, brand_name, sizeof (brand));
1721 1722 }
1722 1723
1723 1724 /* Get a handle to the brand info for this zone */
1724 1725 if ((bh = brand_open(brand)) == NULL) {
1725 1726 zerror(zlogp, B_FALSE, "unable to determine zone brand");
1726 1727 zonecfg_fini_handle(handle);
1727 1728 return (-1);
1728 1729 }
1729 1730
1730 1731 /*
1731 1732 * Get the list of global filesystems to mount from the brand
1732 1733 * configuration.
1733 1734 */
1734 1735 cb.pgcd_zlogp = zlogp;
1735 1736 cb.pgcd_fs_tab = &fs_ptr;
1736 1737 cb.pgcd_num_fs = &num_fs;
1737 1738 if (brand_platform_iter_gmounts(bh, zone_name, zonepath,
1738 1739 plat_gmount_cb, &cb) != 0) {
1739 1740 zerror(zlogp, B_FALSE, "unable to mount filesystems");
1740 1741 brand_close(bh);
1741 1742 zonecfg_fini_handle(handle);
1742 1743 return (-1);
1743 1744 }
1744 1745 brand_close(bh);
1745 1746
1746 1747 /*
1747 1748 * Iterate through the rest of the filesystems. Sort them all,
1748 1749 * then mount them in sorted order. This is to make sure the
1749 1750 * higher level directories (e.g., /usr) get mounted before
1750 1751 * any beneath them (e.g., /usr/local).
1751 1752 */
1752 1753 if (mount_filesystems_fsent(handle, zlogp, &fs_ptr, &num_fs,
1753 1754 mount_cmd) != 0)
1754 1755 goto bad;
1755 1756
1756 1757 zonecfg_fini_handle(handle);
1757 1758 handle = NULL;
1758 1759
1759 1760 /*
1760 1761 * Normally when we mount a zone all the zone filesystems
1761 1762 * get mounted relative to rootpath, which is usually
1762 1763 * <zonepath>/root. But when mounting a zone for administration
1763 1764 * purposes via the zone "mount" state, build_mounted_pre_var()
1764 1765 * updates rootpath to be <zonepath>/lu/a so we'll mount all
1765 1766 * the zones filesystems there instead.
1766 1767 *
1767 1768 * build_mounted_pre_var() and build_mounted_post_var() will
1768 1769 * also do some extra work to create directories and lofs mount
1769 1770 * a bunch of global zone file system paths into <zonepath>/lu.
1770 1771 *
1771 1772 * This allows us to be able to enter the zone (now rooted at
1772 1773 * <zonepath>/lu) and run the upgrade/patch tools that are in the
1773 1774 * global zone and have them upgrade the to-be-modified zone's
1774 1775 * files mounted on /a. (Which mirrors the existing standard
1775 1776 * upgrade environment.)
1776 1777 *
1777 1778 * There is of course one catch. When doing the upgrade
1778 1779 * we need <zoneroot>/lu/dev to be the /dev filesystem
1779 1780 * for the zone and we don't want to have any /dev filesystem
1780 1781 * mounted at <zoneroot>/lu/a/dev. Since /dev is specified
1781 1782 * as a normal zone filesystem by default we'll try to mount
1782 1783 * it at <zoneroot>/lu/a/dev, so we have to detect this
1783 1784 * case and instead mount it at <zoneroot>/lu/dev.
1784 1785 *
1785 1786 * All this work is done in three phases:
1786 1787 * 1) Create and populate lu directory (build_mounted_pre_var()).
1787 1788 * 2) Mount the required filesystems as per the zone configuration.
1788 1789 * 3) Set up the rest of the scratch zone environment
1789 1790 * (build_mounted_post_var()).
1790 1791 */
1791 1792 if (ALT_MOUNT(mount_cmd) && !build_mounted_pre_var(zlogp,
1792 1793 rootpath, sizeof (rootpath), zonepath, luroot, sizeof (luroot)))
1793 1794 goto bad;
1794 1795
1795 1796 qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare);
1796 1797
1797 1798 for (i = 0; i < num_fs; i++) {
1798 1799 if (ALT_MOUNT(mount_cmd) &&
1799 1800 strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) {
1800 1801 size_t slen = strlen(rootpath) - 2;
1801 1802
1802 1803 /*
1803 1804 * By default we'll try to mount /dev as /a/dev
1804 1805 * but /dev is special and always goes at the top
1805 1806 * so strip the trailing '/a' from the rootpath.
1806 1807 */
1807 1808 assert(strcmp(&rootpath[slen], "/a") == 0);
1808 1809 rootpath[slen] = '\0';
1809 1810 if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd)
1810 1811 != 0)
1811 1812 goto bad;
1812 1813 rootpath[slen] = '/';
1813 1814 continue;
1814 1815 }
1815 1816 if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd) != 0)
1816 1817 goto bad;
1817 1818 }
1818 1819 if (ALT_MOUNT(mount_cmd) &&
1819 1820 !build_mounted_post_var(zlogp, mount_cmd, rootpath, luroot))
1820 1821 goto bad;
1821 1822
1822 1823 /*
1823 1824 * For Trusted Extensions cross-mount each lower level /export/home
1824 1825 */
1825 1826 if (mount_cmd == Z_MNT_BOOT &&
1826 1827 tsol_mounts(zlogp, zone_name, rootpath) != 0)
1827 1828 goto bad;
1828 1829
1829 1830 free_fs_data(fs_ptr, num_fs);
1830 1831
1831 1832 /*
1832 1833 * Everything looks fine.
1833 1834 */
1834 1835 return (0);
1835 1836
1836 1837 bad:
1837 1838 if (handle != NULL)
1838 1839 zonecfg_fini_handle(handle);
1839 1840 free_fs_data(fs_ptr, num_fs);
1840 1841 return (-1);
1841 1842 }
1842 1843
1843 1844 /* caller makes sure neither parameter is NULL */
1844 1845 static int
1845 1846 addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr)
1846 1847 {
1847 1848 int prefixlen;
1848 1849
1849 1850 prefixlen = atoi(prefixstr);
1850 1851 if (prefixlen < 0 || prefixlen > maxprefixlen)
1851 1852 return (1);
1852 1853 while (prefixlen > 0) {
1853 1854 if (prefixlen >= 8) {
1854 1855 *maskstr++ = 0xFF;
1855 1856 prefixlen -= 8;
1856 1857 continue;
1857 1858 }
1858 1859 *maskstr |= 1 << (8 - prefixlen);
1859 1860 prefixlen--;
1860 1861 }
1861 1862 return (0);
1862 1863 }
1863 1864
1864 1865 /*
1865 1866 * Tear down all interfaces belonging to the given zone. This should
1866 1867 * be called with the zone in a state other than "running", so that
1867 1868 * interfaces can't be assigned to the zone after this returns.
1868 1869 *
1869 1870 * If anything goes wrong, log an error message and return an error.
1870 1871 */
1871 1872 static int
1872 1873 unconfigure_shared_network_interfaces(zlog_t *zlogp, zoneid_t zone_id)
1873 1874 {
1874 1875 struct lifnum lifn;
1875 1876 struct lifconf lifc;
1876 1877 struct lifreq *lifrp, lifrl;
1877 1878 int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES;
1878 1879 int num_ifs, s, i, ret_code = 0;
1879 1880 uint_t bufsize;
1880 1881 char *buf = NULL;
1881 1882
1882 1883 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1883 1884 zerror(zlogp, B_TRUE, "could not get socket");
1884 1885 ret_code = -1;
1885 1886 goto bad;
1886 1887 }
1887 1888 lifn.lifn_family = AF_UNSPEC;
1888 1889 lifn.lifn_flags = (int)lifc_flags;
1889 1890 if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
1890 1891 zerror(zlogp, B_TRUE,
1891 1892 "could not determine number of network interfaces");
1892 1893 ret_code = -1;
1893 1894 goto bad;
1894 1895 }
1895 1896 num_ifs = lifn.lifn_count;
1896 1897 bufsize = num_ifs * sizeof (struct lifreq);
1897 1898 if ((buf = malloc(bufsize)) == NULL) {
1898 1899 zerror(zlogp, B_TRUE, "memory allocation failed");
1899 1900 ret_code = -1;
1900 1901 goto bad;
1901 1902 }
1902 1903 lifc.lifc_family = AF_UNSPEC;
1903 1904 lifc.lifc_flags = (int)lifc_flags;
1904 1905 lifc.lifc_len = bufsize;
1905 1906 lifc.lifc_buf = buf;
1906 1907 if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
1907 1908 zerror(zlogp, B_TRUE, "could not get configured network "
1908 1909 "interfaces");
1909 1910 ret_code = -1;
1910 1911 goto bad;
1911 1912 }
1912 1913 lifrp = lifc.lifc_req;
1913 1914 for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) {
1914 1915 (void) close(s);
1915 1916 if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) <
1916 1917 0) {
1917 1918 zerror(zlogp, B_TRUE, "%s: could not get socket",
1918 1919 lifrl.lifr_name);
1919 1920 ret_code = -1;
1920 1921 continue;
1921 1922 }
1922 1923 (void) memset(&lifrl, 0, sizeof (lifrl));
1923 1924 (void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
1924 1925 sizeof (lifrl.lifr_name));
1925 1926 if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) {
1926 1927 if (errno == ENXIO)
1927 1928 /*
1928 1929 * Interface may have been removed by admin or
1929 1930 * another zone halting.
1930 1931 */
1931 1932 continue;
1932 1933 zerror(zlogp, B_TRUE,
1933 1934 "%s: could not determine the zone to which this "
1934 1935 "network interface is bound", lifrl.lifr_name);
1935 1936 ret_code = -1;
1936 1937 continue;
1937 1938 }
1938 1939 if (lifrl.lifr_zoneid == zone_id) {
1939 1940 if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) {
1940 1941 zerror(zlogp, B_TRUE,
1941 1942 "%s: could not remove network interface",
1942 1943 lifrl.lifr_name);
1943 1944 ret_code = -1;
1944 1945 continue;
1945 1946 }
1946 1947 }
1947 1948 }
1948 1949 bad:
1949 1950 if (s > 0)
1950 1951 (void) close(s);
1951 1952 if (buf)
1952 1953 free(buf);
1953 1954 return (ret_code);
1954 1955 }
1955 1956
1956 1957 static union sockunion {
1957 1958 struct sockaddr sa;
1958 1959 struct sockaddr_in sin;
1959 1960 struct sockaddr_dl sdl;
1960 1961 struct sockaddr_in6 sin6;
1961 1962 } so_dst, so_ifp;
1962 1963
1963 1964 static struct {
1964 1965 struct rt_msghdr hdr;
1965 1966 char space[512];
1966 1967 } rtmsg;
1967 1968
1968 1969 static int
1969 1970 salen(struct sockaddr *sa)
1970 1971 {
1971 1972 switch (sa->sa_family) {
1972 1973 case AF_INET:
1973 1974 return (sizeof (struct sockaddr_in));
1974 1975 case AF_LINK:
1975 1976 return (sizeof (struct sockaddr_dl));
1976 1977 case AF_INET6:
1977 1978 return (sizeof (struct sockaddr_in6));
1978 1979 default:
1979 1980 return (sizeof (struct sockaddr));
1980 1981 }
1981 1982 }
1982 1983
1983 1984 #define ROUNDUP_LONG(a) \
1984 1985 ((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long))
1985 1986
1986 1987 /*
1987 1988 * Look up which zone is using a given IP address. The address in question
1988 1989 * is expected to have been stuffed into the structure to which lifr points
1989 1990 * via a previous SIOCGLIFADDR ioctl().
1990 1991 *
1991 1992 * This is done using black router socket magic.
1992 1993 *
1993 1994 * Return the name of the zone on success or NULL on failure.
1994 1995 *
1995 1996 * This is a lot of code for a simple task; a new ioctl request to take care
1996 1997 * of this might be a useful RFE.
1997 1998 */
1998 1999
1999 2000 static char *
2000 2001 who_is_using(zlog_t *zlogp, struct lifreq *lifr)
2001 2002 {
2002 2003 static char answer[ZONENAME_MAX];
2003 2004 pid_t pid;
2004 2005 int s, rlen, l, i;
2005 2006 char *cp = rtmsg.space;
2006 2007 struct sockaddr_dl *ifp = NULL;
2007 2008 struct sockaddr *sa;
2008 2009 char save_if_name[LIFNAMSIZ];
2009 2010
2010 2011 answer[0] = '\0';
2011 2012
2012 2013 pid = getpid();
2013 2014 if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
2014 2015 zerror(zlogp, B_TRUE, "could not get routing socket");
2015 2016 return (NULL);
2016 2017 }
2017 2018
2018 2019 if (lifr->lifr_addr.ss_family == AF_INET) {
2019 2020 struct sockaddr_in *sin4;
2020 2021
2021 2022 so_dst.sa.sa_family = AF_INET;
2022 2023 sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
2023 2024 so_dst.sin.sin_addr = sin4->sin_addr;
2024 2025 } else {
2025 2026 struct sockaddr_in6 *sin6;
2026 2027
2027 2028 so_dst.sa.sa_family = AF_INET6;
2028 2029 sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
2029 2030 so_dst.sin6.sin6_addr = sin6->sin6_addr;
2030 2031 }
2031 2032
2032 2033 so_ifp.sa.sa_family = AF_LINK;
2033 2034
2034 2035 (void) memset(&rtmsg, 0, sizeof (rtmsg));
2035 2036 rtmsg.hdr.rtm_type = RTM_GET;
2036 2037 rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST;
2037 2038 rtmsg.hdr.rtm_version = RTM_VERSION;
2038 2039 rtmsg.hdr.rtm_seq = ++rts_seqno;
2039 2040 rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST;
2040 2041
2041 2042 l = ROUNDUP_LONG(salen(&so_dst.sa));
2042 2043 (void) memmove(cp, &(so_dst), l);
2043 2044 cp += l;
2044 2045 l = ROUNDUP_LONG(salen(&so_ifp.sa));
2045 2046 (void) memmove(cp, &(so_ifp), l);
2046 2047 cp += l;
2047 2048
2048 2049 rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg;
2049 2050
2050 2051 if ((rlen = write(s, &rtmsg, l)) < 0) {
2051 2052 zerror(zlogp, B_TRUE, "writing to routing socket");
2052 2053 return (NULL);
2053 2054 } else if (rlen < (int)rtmsg.hdr.rtm_msglen) {
2054 2055 zerror(zlogp, B_TRUE,
2055 2056 "write to routing socket got only %d for len\n", rlen);
2056 2057 return (NULL);
2057 2058 }
2058 2059 do {
2059 2060 l = read(s, &rtmsg, sizeof (rtmsg));
2060 2061 } while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno ||
2061 2062 rtmsg.hdr.rtm_pid != pid));
2062 2063 if (l < 0) {
2063 2064 zerror(zlogp, B_TRUE, "reading from routing socket");
2064 2065 return (NULL);
2065 2066 }
2066 2067
2067 2068 if (rtmsg.hdr.rtm_version != RTM_VERSION) {
2068 2069 zerror(zlogp, B_FALSE,
2069 2070 "routing message version %d not understood",
2070 2071 rtmsg.hdr.rtm_version);
2071 2072 return (NULL);
2072 2073 }
2073 2074 if (rtmsg.hdr.rtm_msglen != (ushort_t)l) {
2074 2075 zerror(zlogp, B_FALSE, "message length mismatch, "
2075 2076 "expected %d bytes, returned %d bytes",
2076 2077 rtmsg.hdr.rtm_msglen, l);
2077 2078 return (NULL);
2078 2079 }
2079 2080 if (rtmsg.hdr.rtm_errno != 0) {
2080 2081 errno = rtmsg.hdr.rtm_errno;
2081 2082 zerror(zlogp, B_TRUE, "RTM_GET routing socket message");
2082 2083 return (NULL);
2083 2084 }
2084 2085 if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) {
2085 2086 zerror(zlogp, B_FALSE, "network interface not found");
2086 2087 return (NULL);
2087 2088 }
2088 2089 cp = ((char *)(&rtmsg.hdr + 1));
2089 2090 for (i = 1; i != 0; i <<= 1) {
2090 2091 /* LINTED E_BAD_PTR_CAST_ALIGN */
2091 2092 sa = (struct sockaddr *)cp;
2092 2093 if (i != RTA_IFP) {
2093 2094 if ((i & rtmsg.hdr.rtm_addrs) != 0)
2094 2095 cp += ROUNDUP_LONG(salen(sa));
2095 2096 continue;
2096 2097 }
2097 2098 if (sa->sa_family == AF_LINK &&
2098 2099 ((struct sockaddr_dl *)sa)->sdl_nlen != 0)
2099 2100 ifp = (struct sockaddr_dl *)sa;
2100 2101 break;
2101 2102 }
2102 2103 if (ifp == NULL) {
2103 2104 zerror(zlogp, B_FALSE, "network interface could not be "
2104 2105 "determined");
2105 2106 return (NULL);
2106 2107 }
2107 2108
2108 2109 /*
2109 2110 * We need to set the I/F name to what we got above, then do the
2110 2111 * appropriate ioctl to get its zone name. But lifr->lifr_name is
2111 2112 * used by the calling function to do a REMOVEIF, so if we leave the
2112 2113 * "good" zone's I/F name in place, *that* I/F will be removed instead
2113 2114 * of the bad one. So we save the old (bad) I/F name before over-
2114 2115 * writing it and doing the ioctl, then restore it after the ioctl.
2115 2116 */
2116 2117 (void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name));
2117 2118 (void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen);
2118 2119 lifr->lifr_name[ifp->sdl_nlen] = '\0';
2119 2120 i = ioctl(s, SIOCGLIFZONE, lifr);
2120 2121 (void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name));
2121 2122 if (i < 0) {
2122 2123 zerror(zlogp, B_TRUE,
2123 2124 "%s: could not determine the zone network interface "
2124 2125 "belongs to", lifr->lifr_name);
2125 2126 return (NULL);
2126 2127 }
2127 2128 if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0)
2128 2129 (void) snprintf(answer, sizeof (answer), "%d",
2129 2130 lifr->lifr_zoneid);
2130 2131
2131 2132 if (strlen(answer) > 0)
2132 2133 return (answer);
2133 2134 return (NULL);
2134 2135 }
2135 2136
2136 2137 /*
2137 2138 * Configures a single interface: a new virtual interface is added, based on
2138 2139 * the physical interface nwiftabptr->zone_nwif_physical, with the address
2139 2140 * specified in nwiftabptr->zone_nwif_address, for zone zone_id. Note that
2140 2141 * the "address" can be an IPv6 address (with a /prefixlength required), an
2141 2142 * IPv4 address (with a /prefixlength optional), or a name; for the latter,
2142 2143 * an IPv4 name-to-address resolution will be attempted.
2143 2144 *
2144 2145 * If anything goes wrong, we log an detailed error message, attempt to tear
2145 2146 * down whatever we set up and return an error.
2146 2147 */
2147 2148 static int
2148 2149 configure_one_interface(zlog_t *zlogp, zoneid_t zone_id,
2149 2150 struct zone_nwiftab *nwiftabptr)
2150 2151 {
2151 2152 struct lifreq lifr;
2152 2153 struct sockaddr_in netmask4;
2153 2154 struct sockaddr_in6 netmask6;
2154 2155 struct sockaddr_storage laddr;
2155 2156 struct in_addr in4;
2156 2157 sa_family_t af;
2157 2158 char *slashp = strchr(nwiftabptr->zone_nwif_address, '/');
2158 2159 int s;
2159 2160 boolean_t got_netmask = B_FALSE;
2160 2161 boolean_t is_loopback = B_FALSE;
2161 2162 char addrstr4[INET_ADDRSTRLEN];
2162 2163 int res;
2163 2164
2164 2165 res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr);
2165 2166 if (res != Z_OK) {
2166 2167 zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res),
2167 2168 nwiftabptr->zone_nwif_address);
2168 2169 return (-1);
2169 2170 }
2170 2171 af = lifr.lifr_addr.ss_family;
2171 2172 if (af == AF_INET)
2172 2173 in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr;
2173 2174 if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
2174 2175 zerror(zlogp, B_TRUE, "could not get socket");
2175 2176 return (-1);
2176 2177 }
2177 2178
2178 2179 /*
2179 2180 * This is a similar kind of "hack" like in addif() to get around
2180 2181 * the problem of SIOCLIFADDIF. The problem is that this ioctl
2181 2182 * does not include the netmask when adding a logical interface.
2182 2183 * To get around this problem, we first add the logical interface
2183 2184 * with a 0 address. After that, we set the netmask if provided.
2184 2185 * Finally we set the interface address.
2185 2186 */
2186 2187 laddr = lifr.lifr_addr;
2187 2188 (void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical,
2188 2189 sizeof (lifr.lifr_name));
2189 2190 (void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
2190 2191
2191 2192 if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
2192 2193 /*
2193 2194 * Here, we know that the interface can't be brought up.
2194 2195 * A similar warning message was already printed out to
2195 2196 * the console by zoneadm(1M) so instead we log the
2196 2197 * message to syslog and continue.
2197 2198 */
2198 2199 zerror(&logsys, B_TRUE, "WARNING: skipping network interface "
2199 2200 "'%s' which may not be present/plumbed in the "
2200 2201 "global zone.", lifr.lifr_name);
2201 2202 (void) close(s);
2202 2203 return (Z_OK);
2203 2204 }
2204 2205
2205 2206 /* Preserve literal IPv4 address for later potential printing. */
2206 2207 if (af == AF_INET)
2207 2208 (void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN);
2208 2209
2209 2210 lifr.lifr_zoneid = zone_id;
2210 2211 if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) {
2211 2212 zerror(zlogp, B_TRUE, "%s: could not place network interface "
2212 2213 "into zone", lifr.lifr_name);
2213 2214 goto bad;
2214 2215 }
2215 2216
2216 2217 /*
2217 2218 * Loopback interface will use the default netmask assigned, if no
2218 2219 * netmask is found.
2219 2220 */
2220 2221 if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) {
2221 2222 is_loopback = B_TRUE;
2222 2223 }
2223 2224 if (af == AF_INET) {
2224 2225 /*
2225 2226 * The IPv4 netmask can be determined either
2226 2227 * directly if a prefix length was supplied with
2227 2228 * the address or via the netmasks database. Not
2228 2229 * being able to determine it is a common failure,
2229 2230 * but it often is not fatal to operation of the
2230 2231 * interface. In that case, a warning will be
2231 2232 * printed after the rest of the interface's
2232 2233 * parameters have been configured.
2233 2234 */
2234 2235 (void) memset(&netmask4, 0, sizeof (netmask4));
2235 2236 if (slashp != NULL) {
2236 2237 if (addr2netmask(slashp + 1, V4_ADDR_LEN,
2237 2238 (uchar_t *)&netmask4.sin_addr) != 0) {
2238 2239 *slashp = '/';
2239 2240 zerror(zlogp, B_FALSE,
2240 2241 "%s: invalid prefix length in %s",
2241 2242 lifr.lifr_name,
2242 2243 nwiftabptr->zone_nwif_address);
2243 2244 goto bad;
2244 2245 }
2245 2246 got_netmask = B_TRUE;
2246 2247 } else if (getnetmaskbyaddr(in4,
2247 2248 &netmask4.sin_addr) == 0) {
2248 2249 got_netmask = B_TRUE;
2249 2250 }
2250 2251 if (got_netmask) {
2251 2252 netmask4.sin_family = af;
2252 2253 (void) memcpy(&lifr.lifr_addr, &netmask4,
2253 2254 sizeof (netmask4));
2254 2255 }
2255 2256 } else {
2256 2257 (void) memset(&netmask6, 0, sizeof (netmask6));
2257 2258 if (addr2netmask(slashp + 1, V6_ADDR_LEN,
2258 2259 (uchar_t *)&netmask6.sin6_addr) != 0) {
2259 2260 *slashp = '/';
2260 2261 zerror(zlogp, B_FALSE,
2261 2262 "%s: invalid prefix length in %s",
2262 2263 lifr.lifr_name,
2263 2264 nwiftabptr->zone_nwif_address);
2264 2265 goto bad;
2265 2266 }
2266 2267 got_netmask = B_TRUE;
2267 2268 netmask6.sin6_family = af;
2268 2269 (void) memcpy(&lifr.lifr_addr, &netmask6,
2269 2270 sizeof (netmask6));
2270 2271 }
2271 2272 if (got_netmask &&
2272 2273 ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) {
2273 2274 zerror(zlogp, B_TRUE, "%s: could not set netmask",
2274 2275 lifr.lifr_name);
2275 2276 goto bad;
2276 2277 }
2277 2278
2278 2279 /* Set the interface address */
2279 2280 lifr.lifr_addr = laddr;
2280 2281 if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
2281 2282 zerror(zlogp, B_TRUE,
2282 2283 "%s: could not set IP address to %s",
2283 2284 lifr.lifr_name, nwiftabptr->zone_nwif_address);
2284 2285 goto bad;
2285 2286 }
2286 2287
2287 2288 if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
2288 2289 zerror(zlogp, B_TRUE, "%s: could not get flags",
2289 2290 lifr.lifr_name);
2290 2291 goto bad;
2291 2292 }
2292 2293 lifr.lifr_flags |= IFF_UP;
2293 2294 if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
2294 2295 int save_errno = errno;
2295 2296 char *zone_using;
2296 2297
2297 2298 /*
2298 2299 * If we failed with something other than EADDRNOTAVAIL,
2299 2300 * then skip to the end. Otherwise, look up our address,
2300 2301 * then call a function to determine which zone is already
2301 2302 * using that address.
2302 2303 */
2303 2304 if (errno != EADDRNOTAVAIL) {
2304 2305 zerror(zlogp, B_TRUE,
2305 2306 "%s: could not bring network interface up",
2306 2307 lifr.lifr_name);
2307 2308 goto bad;
2308 2309 }
2309 2310 if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
2310 2311 zerror(zlogp, B_TRUE, "%s: could not get address",
2311 2312 lifr.lifr_name);
2312 2313 goto bad;
2313 2314 }
2314 2315 zone_using = who_is_using(zlogp, &lifr);
2315 2316 errno = save_errno;
2316 2317 if (zone_using == NULL)
2317 2318 zerror(zlogp, B_TRUE,
2318 2319 "%s: could not bring network interface up",
2319 2320 lifr.lifr_name);
2320 2321 else
2321 2322 zerror(zlogp, B_TRUE, "%s: could not bring network "
2322 2323 "interface up: address in use by zone '%s'",
2323 2324 lifr.lifr_name, zone_using);
2324 2325 goto bad;
2325 2326 }
2326 2327
2327 2328 if (!got_netmask && !is_loopback) {
2328 2329 /*
2329 2330 * A common, but often non-fatal problem, is that the system
2330 2331 * cannot find the netmask for an interface address. This is
2331 2332 * often caused by it being only in /etc/inet/netmasks, but
2332 2333 * /etc/nsswitch.conf says to use NIS or NIS+ and it's not
2333 2334 * in that. This doesn't show up at boot because the netmask
2334 2335 * is obtained from /etc/inet/netmasks when no network
2335 2336 * interfaces are up, but isn't consulted when NIS/NIS+ is
2336 2337 * available. We warn the user here that something like this
2337 2338 * has happened and we're just running with a default and
2338 2339 * possible incorrect netmask.
2339 2340 */
2340 2341 char buffer[INET6_ADDRSTRLEN];
2341 2342 void *addr;
2342 2343 const char *nomatch = "no matching subnet found in netmasks(4)";
2343 2344
2344 2345 if (af == AF_INET)
2345 2346 addr = &((struct sockaddr_in *)
2346 2347 (&lifr.lifr_addr))->sin_addr;
2347 2348 else
2348 2349 addr = &((struct sockaddr_in6 *)
2349 2350 (&lifr.lifr_addr))->sin6_addr;
2350 2351
2351 2352 /*
2352 2353 * Find out what netmask the interface is going to be using.
2353 2354 * If we just brought up an IPMP data address on an underlying
2354 2355 * interface above, the address will have already migrated, so
2355 2356 * the SIOCGLIFNETMASK won't be able to find it (but we need
2356 2357 * to bring the address up to get the actual netmask). Just
2357 2358 * omit printing the actual netmask in this corner-case.
2358 2359 */
2359 2360 if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 ||
2360 2361 inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL) {
2361 2362 zerror(zlogp, B_FALSE, "WARNING: %s; using default.",
2362 2363 nomatch);
2363 2364 } else {
2364 2365 zerror(zlogp, B_FALSE,
2365 2366 "WARNING: %s: %s: %s; using default of %s.",
2366 2367 lifr.lifr_name, nomatch, addrstr4, buffer);
2367 2368 }
2368 2369 }
2369 2370
2370 2371 /*
2371 2372 * If a default router was specified for this interface
2372 2373 * set the route now. Ignore if already set.
2373 2374 */
2374 2375 if (strlen(nwiftabptr->zone_nwif_defrouter) > 0) {
2375 2376 int status;
2376 2377 char *argv[7];
2377 2378
2378 2379 argv[0] = "route";
2379 2380 argv[1] = "add";
2380 2381 argv[2] = "-ifp";
2381 2382 argv[3] = nwiftabptr->zone_nwif_physical;
2382 2383 argv[4] = "default";
2383 2384 argv[5] = nwiftabptr->zone_nwif_defrouter;
2384 2385 argv[6] = NULL;
2385 2386
2386 2387 status = forkexec(zlogp, "/usr/sbin/route", argv);
2387 2388 if (status != 0 && status != EEXIST)
2388 2389 zerror(zlogp, B_FALSE, "Unable to set route for "
2389 2390 "interface %s to %s\n",
2390 2391 nwiftabptr->zone_nwif_physical,
2391 2392 nwiftabptr->zone_nwif_defrouter);
2392 2393 }
2393 2394
2394 2395 (void) close(s);
2395 2396 return (Z_OK);
2396 2397 bad:
2397 2398 (void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
2398 2399 (void) close(s);
2399 2400 return (-1);
2400 2401 }
2401 2402
2402 2403 /*
2403 2404 * Sets up network interfaces based on information from the zone configuration.
2404 2405 * IPv4 and IPv6 loopback interfaces are set up "for free", modeling the global
2405 2406 * system.
2406 2407 *
2407 2408 * If anything goes wrong, we log a general error message, attempt to tear down
2408 2409 * whatever we set up, and return an error.
2409 2410 */
2410 2411 static int
2411 2412 configure_shared_network_interfaces(zlog_t *zlogp)
2412 2413 {
2413 2414 zone_dochandle_t handle;
2414 2415 struct zone_nwiftab nwiftab, loopback_iftab;
2415 2416 zoneid_t zoneid;
2416 2417
2417 2418 if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) {
2418 2419 zerror(zlogp, B_TRUE, "unable to get zoneid");
2419 2420 return (-1);
2420 2421 }
2421 2422
2422 2423 if ((handle = zonecfg_init_handle()) == NULL) {
2423 2424 zerror(zlogp, B_TRUE, "getting zone configuration handle");
2424 2425 return (-1);
2425 2426 }
2426 2427 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2427 2428 zerror(zlogp, B_FALSE, "invalid configuration");
2428 2429 zonecfg_fini_handle(handle);
2429 2430 return (-1);
2430 2431 }
2431 2432 if (zonecfg_setnwifent(handle) == Z_OK) {
2432 2433 for (;;) {
2433 2434 if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
2434 2435 break;
2435 2436 if (configure_one_interface(zlogp, zoneid, &nwiftab) !=
2436 2437 Z_OK) {
2437 2438 (void) zonecfg_endnwifent(handle);
2438 2439 zonecfg_fini_handle(handle);
2439 2440 return (-1);
2440 2441 }
2441 2442 }
2442 2443 (void) zonecfg_endnwifent(handle);
2443 2444 }
2444 2445 zonecfg_fini_handle(handle);
2445 2446 if (is_system_labeled()) {
2446 2447 /*
2447 2448 * Labeled zones share the loopback interface
2448 2449 * so it is not plumbed for shared stack instances.
2449 2450 */
2450 2451 return (0);
2451 2452 }
2452 2453 (void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0",
2453 2454 sizeof (loopback_iftab.zone_nwif_physical));
2454 2455 (void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1",
2455 2456 sizeof (loopback_iftab.zone_nwif_address));
2456 2457 loopback_iftab.zone_nwif_defrouter[0] = '\0';
2457 2458 if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
2458 2459 return (-1);
2459 2460
2460 2461 /* Always plumb up the IPv6 loopback interface. */
2461 2462 (void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128",
2462 2463 sizeof (loopback_iftab.zone_nwif_address));
2463 2464 if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
2464 2465 return (-1);
2465 2466 return (0);
2466 2467 }
2467 2468
2468 2469 static void
2469 2470 zdlerror(zlog_t *zlogp, dladm_status_t err, const char *dlname, const char *str)
2470 2471 {
2471 2472 char errmsg[DLADM_STRSIZE];
2472 2473
2473 2474 (void) dladm_status2str(err, errmsg);
2474 2475 zerror(zlogp, B_FALSE, "%s '%s': %s", str, dlname, errmsg);
2475 2476 }
2476 2477
2477 2478 static int
2478 2479 add_datalink(zlog_t *zlogp, char *zone_name, datalink_id_t linkid, char *dlname)
2479 2480 {
2480 2481 dladm_status_t err;
2481 2482 boolean_t cpuset, poolset;
2482 2483 char *poolp;
2483 2484
2484 2485 /* First check if it's in use by global zone. */
2485 2486 if (zonecfg_ifname_exists(AF_INET, dlname) ||
2486 2487 zonecfg_ifname_exists(AF_INET6, dlname)) {
2487 2488 zerror(zlogp, B_FALSE, "WARNING: skipping network interface "
2488 2489 "'%s' which is used in the global zone", dlname);
2489 2490 return (-1);
2490 2491 }
2491 2492
2492 2493 /* Set zoneid of this link. */
2493 2494 err = dladm_set_linkprop(dld_handle, linkid, "zone", &zone_name, 1,
2494 2495 DLADM_OPT_ACTIVE);
2495 2496 if (err != DLADM_STATUS_OK) {
2496 2497 zdlerror(zlogp, err, dlname,
2497 2498 "WARNING: unable to add network interface");
2498 2499 return (-1);
2499 2500 }
2500 2501
2501 2502 /*
2502 2503 * Set the pool of this link if the zone has a pool and
2503 2504 * neither the cpus nor the pool datalink property is
2504 2505 * already set.
2505 2506 */
2506 2507 err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT,
2507 2508 "cpus", &cpuset);
2508 2509 if (err != DLADM_STATUS_OK) {
2509 2510 zdlerror(zlogp, err, dlname,
2510 2511 "WARNING: unable to check if cpus link property is set");
2511 2512 }
2512 2513 err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT,
2513 2514 "pool", &poolset);
2514 2515 if (err != DLADM_STATUS_OK) {
2515 2516 zdlerror(zlogp, err, dlname,
2516 2517 "WARNING: unable to check if pool link property is set");
2517 2518 }
2518 2519
2519 2520 if ((strlen(pool_name) != 0) && !cpuset && !poolset) {
2520 2521 poolp = pool_name;
2521 2522 err = dladm_set_linkprop(dld_handle, linkid, "pool",
2522 2523 &poolp, 1, DLADM_OPT_ACTIVE);
2523 2524 if (err != DLADM_STATUS_OK) {
2524 2525 zerror(zlogp, B_FALSE, "WARNING: unable to set "
2525 2526 "pool %s to datalink %s", pool_name, dlname);
2526 2527 bzero(pool_name, sizeof (pool_name));
2527 2528 }
2528 2529 } else {
2529 2530 bzero(pool_name, sizeof (pool_name));
2530 2531 }
2531 2532 return (0);
2532 2533 }
2533 2534
2534 2535 static boolean_t
2535 2536 sockaddr_to_str(sa_family_t af, const struct sockaddr *sockaddr,
2536 2537 char *straddr, size_t len)
2537 2538 {
2538 2539 struct sockaddr_in *sin;
2539 2540 struct sockaddr_in6 *sin6;
2540 2541 const char *str = NULL;
2541 2542
2542 2543 if (af == AF_INET) {
2543 2544 /* LINTED E_BAD_PTR_CAST_ALIGN */
2544 2545 sin = SIN(sockaddr);
2545 2546 str = inet_ntop(AF_INET, (void *)&sin->sin_addr, straddr, len);
2546 2547 } else if (af == AF_INET6) {
2547 2548 /* LINTED E_BAD_PTR_CAST_ALIGN */
2548 2549 sin6 = SIN6(sockaddr);
2549 2550 str = inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, straddr,
2550 2551 len);
2551 2552 }
2552 2553
2553 2554 return (str != NULL);
2554 2555 }
2555 2556
2556 2557 static int
2557 2558 ipv4_prefixlen(struct sockaddr_in *sin)
2558 2559 {
2559 2560 struct sockaddr_in *m;
2560 2561 struct sockaddr_storage mask;
2561 2562
2562 2563 m = SIN(&mask);
2563 2564 m->sin_family = AF_INET;
2564 2565 if (getnetmaskbyaddr(sin->sin_addr, &m->sin_addr) == 0) {
2565 2566 return (mask2plen((struct sockaddr *)&mask));
2566 2567 } else if (IN_CLASSA(htonl(sin->sin_addr.s_addr))) {
2567 2568 return (8);
2568 2569 } else if (IN_CLASSB(ntohl(sin->sin_addr.s_addr))) {
2569 2570 return (16);
2570 2571 } else if (IN_CLASSC(ntohl(sin->sin_addr.s_addr))) {
2571 2572 return (24);
2572 2573 }
2573 2574 return (0);
2574 2575 }
2575 2576
2576 2577 static int
2577 2578 zone_setattr_network(int type, zoneid_t zoneid, datalink_id_t linkid,
2578 2579 void *buf, size_t bufsize)
2579 2580 {
2580 2581 zone_net_data_t *zndata;
2581 2582 size_t znsize;
2582 2583 int err;
2583 2584
2584 2585 znsize = sizeof (*zndata) + bufsize;
2585 2586 zndata = calloc(1, znsize);
2586 2587 if (zndata == NULL)
2587 2588 return (ENOMEM);
2588 2589 zndata->zn_type = type;
2589 2590 zndata->zn_len = bufsize;
2590 2591 zndata->zn_linkid = linkid;
2591 2592 bcopy(buf, zndata->zn_val, zndata->zn_len);
2592 2593 err = zone_setattr(zoneid, ZONE_ATTR_NETWORK, zndata, znsize);
2593 2594 free(zndata);
2594 2595 return (err);
2595 2596 }
2596 2597
2597 2598 static int
2598 2599 add_net_for_linkid(zlog_t *zlogp, zoneid_t zoneid, zone_addr_list_t *start)
2599 2600 {
2600 2601 struct lifreq lifr;
2601 2602 char **astr, *address;
2602 2603 dladm_status_t dlstatus;
2603 2604 char *ip_nospoof = "ip-nospoof";
2604 2605 int nnet, naddr, err = 0, j;
2605 2606 size_t zlen, cpleft;
2606 2607 zone_addr_list_t *ptr, *end;
2607 2608 char tmp[INET6_ADDRSTRLEN], *maskstr;
2608 2609 char *zaddr, *cp;
2609 2610 struct in6_addr *routes = NULL;
2610 2611 boolean_t is_set;
2611 2612 datalink_id_t linkid;
2612 2613
2613 2614 assert(start != NULL);
2614 2615 naddr = 0; /* number of addresses */
2615 2616 nnet = 0; /* number of net resources */
2616 2617 linkid = start->za_linkid;
2617 2618 for (ptr = start; ptr != NULL && ptr->za_linkid == linkid;
2618 2619 ptr = ptr->za_next) {
2619 2620 nnet++;
2620 2621 }
2621 2622 end = ptr;
2622 2623 zlen = nnet * (INET6_ADDRSTRLEN + 1);
2623 2624 astr = calloc(1, nnet * sizeof (uintptr_t));
2624 2625 zaddr = calloc(1, zlen);
2625 2626 if (astr == NULL || zaddr == NULL) {
2626 2627 err = ENOMEM;
2627 2628 goto done;
2628 2629 }
2629 2630 cp = zaddr;
2630 2631 cpleft = zlen;
2631 2632 j = 0;
2632 2633 for (ptr = start; ptr != end; ptr = ptr->za_next) {
2633 2634 address = ptr->za_nwiftab.zone_nwif_allowed_address;
2634 2635 if (address[0] == '\0')
2635 2636 continue;
2636 2637 (void) snprintf(tmp, sizeof (tmp), "%s", address);
2637 2638 /*
2638 2639 * Validate the data. zonecfg_valid_net_address() clobbers
2639 2640 * the /<mask> in the address string.
2640 2641 */
2641 2642 if (zonecfg_valid_net_address(address, &lifr) != Z_OK) {
2642 2643 zerror(zlogp, B_FALSE, "invalid address [%s]\n",
2643 2644 address);
2644 2645 err = EINVAL;
2645 2646 goto done;
2646 2647 }
2647 2648 /*
2648 2649 * convert any hostnames to numeric address strings.
2649 2650 */
2650 2651 if (!sockaddr_to_str(lifr.lifr_addr.ss_family,
2651 2652 (const struct sockaddr *)&lifr.lifr_addr, cp, cpleft)) {
2652 2653 err = EINVAL;
2653 2654 goto done;
2654 2655 }
2655 2656 /*
2656 2657 * make a copy of the numeric string for the data needed
2657 2658 * by the "allowed-ips" datalink property.
2658 2659 */
2659 2660 astr[j] = strdup(cp);
2660 2661 if (astr[j] == NULL) {
2661 2662 err = ENOMEM;
2662 2663 goto done;
2663 2664 }
2664 2665 j++;
2665 2666 /*
2666 2667 * compute the default netmask from the address, if necessary
2667 2668 */
2668 2669 if ((maskstr = strchr(tmp, '/')) == NULL) {
2669 2670 int prefixlen;
2670 2671
2671 2672 if (lifr.lifr_addr.ss_family == AF_INET) {
2672 2673 prefixlen = ipv4_prefixlen(
2673 2674 SIN(&lifr.lifr_addr));
2674 2675 } else {
2675 2676 struct sockaddr_in6 *sin6;
2676 2677
2677 2678 sin6 = SIN6(&lifr.lifr_addr);
2678 2679 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
2679 2680 prefixlen = 10;
2680 2681 else
2681 2682 prefixlen = 64;
2682 2683 }
2683 2684 (void) snprintf(tmp, sizeof (tmp), "%d", prefixlen);
2684 2685 maskstr = tmp;
2685 2686 } else {
2686 2687 maskstr++;
2687 2688 }
2688 2689 /* append the "/<netmask>" */
2689 2690 (void) strlcat(cp, "/", cpleft);
2690 2691 (void) strlcat(cp, maskstr, cpleft);
2691 2692 (void) strlcat(cp, ",", cpleft);
2692 2693 cp += strnlen(cp, zlen);
2693 2694 cpleft = &zaddr[INET6_ADDRSTRLEN] - cp;
2694 2695 }
2695 2696 naddr = j; /* the actual number of addresses in the net resource */
2696 2697 assert(naddr <= nnet);
2697 2698
2698 2699 /*
2699 2700 * zonecfg has already verified that the defrouter property can only
2700 2701 * be set if there is at least one address defined for the net resource.
2701 2702 * If j is 0, there are no addresses defined, and therefore no routers
2702 2703 * to configure, and we are done at that point.
2703 2704 */
2704 2705 if (j == 0)
2705 2706 goto done;
2706 2707
2707 2708 /* over-write last ',' with '\0' */
2708 2709 zaddr[strnlen(zaddr, zlen) + 1] = '\0';
2709 2710
2710 2711 /*
2711 2712 * First make sure L3 protection is not already set on the link.
2712 2713 */
2713 2714 dlstatus = dladm_linkprop_is_set(dld_handle, linkid, DLADM_OPT_ACTIVE,
2714 2715 "protection", &is_set);
2715 2716 if (dlstatus != DLADM_STATUS_OK) {
2716 2717 err = EINVAL;
2717 2718 zerror(zlogp, B_FALSE, "unable to check if protection is set");
2718 2719 goto done;
2719 2720 }
2720 2721 if (is_set) {
2721 2722 err = EINVAL;
2722 2723 zerror(zlogp, B_FALSE, "Protection is already set");
2723 2724 goto done;
2724 2725 }
2725 2726 dlstatus = dladm_linkprop_is_set(dld_handle, linkid, DLADM_OPT_ACTIVE,
2726 2727 "allowed-ips", &is_set);
2727 2728 if (dlstatus != DLADM_STATUS_OK) {
2728 2729 err = EINVAL;
2729 2730 zerror(zlogp, B_FALSE, "unable to check if allowed-ips is set");
2730 2731 goto done;
2731 2732 }
2732 2733 if (is_set) {
2733 2734 zerror(zlogp, B_FALSE, "allowed-ips is already set");
2734 2735 err = EINVAL;
2735 2736 goto done;
2736 2737 }
2737 2738
2738 2739 /*
2739 2740 * Enable ip-nospoof for the link, and add address to the allowed-ips
2740 2741 * list.
2741 2742 */
2742 2743 dlstatus = dladm_set_linkprop(dld_handle, linkid, "protection",
2743 2744 &ip_nospoof, 1, DLADM_OPT_ACTIVE);
2744 2745 if (dlstatus != DLADM_STATUS_OK) {
2745 2746 zerror(zlogp, B_FALSE, "could not set protection\n");
2746 2747 err = EINVAL;
2747 2748 goto done;
2748 2749 }
2749 2750 dlstatus = dladm_set_linkprop(dld_handle, linkid, "allowed-ips",
2750 2751 astr, naddr, DLADM_OPT_ACTIVE);
2751 2752 if (dlstatus != DLADM_STATUS_OK) {
2752 2753 zerror(zlogp, B_FALSE, "could not set allowed-ips\n");
2753 2754 err = EINVAL;
2754 2755 goto done;
2755 2756 }
2756 2757
2757 2758 /* now set the address in the data-store */
2758 2759 err = zone_setattr_network(ZONE_NETWORK_ADDRESS, zoneid, linkid,
2759 2760 zaddr, strnlen(zaddr, zlen) + 1);
2760 2761 if (err != 0)
2761 2762 goto done;
2762 2763
2763 2764 /*
2764 2765 * add the defaultrouters
2765 2766 */
2766 2767 routes = calloc(1, nnet * sizeof (*routes));
2767 2768 j = 0;
2768 2769 for (ptr = start; ptr != end; ptr = ptr->za_next) {
2769 2770 address = ptr->za_nwiftab.zone_nwif_defrouter;
2770 2771 if (address[0] == '\0')
2771 2772 continue;
2772 2773 if (strchr(address, '/') == NULL && strchr(address, ':') != 0) {
2773 2774 /*
2774 2775 * zonecfg_valid_net_address() expects numeric IPv6
2775 2776 * addresses to have a CIDR format netmask.
2776 2777 */
2777 2778 (void) snprintf(tmp, sizeof (tmp), "/%d", V6_ADDR_LEN);
2778 2779 (void) strlcat(address, tmp, INET6_ADDRSTRLEN);
2779 2780 }
2780 2781 if (zonecfg_valid_net_address(address, &lifr) != Z_OK) {
2781 2782 zerror(zlogp, B_FALSE,
2782 2783 "invalid router [%s]\n", address);
2783 2784 err = EINVAL;
2784 2785 goto done;
2785 2786 }
2786 2787 if (lifr.lifr_addr.ss_family == AF_INET6) {
2787 2788 routes[j] = SIN6(&lifr.lifr_addr)->sin6_addr;
2788 2789 } else {
2789 2790 IN6_INADDR_TO_V4MAPPED(&SIN(&lifr.lifr_addr)->sin_addr,
2790 2791 &routes[j]);
2791 2792 }
2792 2793 j++;
2793 2794 }
2794 2795 assert(j <= nnet);
2795 2796 if (j > 0) {
2796 2797 err = zone_setattr_network(ZONE_NETWORK_DEFROUTER, zoneid,
2797 2798 linkid, routes, j * sizeof (*routes));
2798 2799 }
2799 2800 done:
2800 2801 free(routes);
2801 2802 for (j = 0; j < naddr; j++)
2802 2803 free(astr[j]);
2803 2804 free(astr);
2804 2805 free(zaddr);
2805 2806 return (err);
2806 2807
2807 2808 }
2808 2809
2809 2810 static int
2810 2811 add_net(zlog_t *zlogp, zoneid_t zoneid, zone_addr_list_t *zalist)
2811 2812 {
2812 2813 zone_addr_list_t *ptr;
2813 2814 datalink_id_t linkid;
2814 2815 int err;
2815 2816
2816 2817 if (zalist == NULL)
2817 2818 return (0);
2818 2819
2819 2820 linkid = zalist->za_linkid;
2820 2821
2821 2822 err = add_net_for_linkid(zlogp, zoneid, zalist);
2822 2823 if (err != 0)
2823 2824 return (err);
2824 2825
2825 2826 for (ptr = zalist; ptr != NULL; ptr = ptr->za_next) {
2826 2827 if (ptr->za_linkid == linkid)
2827 2828 continue;
2828 2829 linkid = ptr->za_linkid;
2829 2830 err = add_net_for_linkid(zlogp, zoneid, ptr);
2830 2831 if (err != 0)
2831 2832 return (err);
2832 2833 }
2833 2834 return (0);
2834 2835 }
2835 2836
2836 2837 /*
2837 2838 * Add "new" to the list of network interfaces to be configured by
2838 2839 * add_net on zone boot in "old". The list of interfaces in "old" is
2839 2840 * sorted by datalink_id_t, with interfaces sorted FIFO for a given
2840 2841 * datalink_id_t.
2841 2842 *
2842 2843 * Returns the merged list of IP interfaces containing "old" and "new"
2843 2844 */
2844 2845 static zone_addr_list_t *
2845 2846 add_ip_interface(zone_addr_list_t *old, zone_addr_list_t *new)
2846 2847 {
2847 2848 zone_addr_list_t *ptr, *next;
2848 2849 datalink_id_t linkid = new->za_linkid;
2849 2850
2850 2851 assert(old != new);
2851 2852
2852 2853 if (old == NULL)
2853 2854 return (new);
2854 2855 for (ptr = old; ptr != NULL; ptr = ptr->za_next) {
2855 2856 if (ptr->za_linkid == linkid)
2856 2857 break;
2857 2858 }
2858 2859 if (ptr == NULL) {
2859 2860 /* linkid does not already exist, add to the beginning */
2860 2861 new->za_next = old;
2861 2862 return (new);
2862 2863 }
2863 2864 /*
2864 2865 * adding to the middle of the list; ptr points at the first
2865 2866 * occurrence of linkid. Find the last occurrence.
2866 2867 */
2867 2868 while ((next = ptr->za_next) != NULL) {
2868 2869 if (next->za_linkid != linkid)
2869 2870 break;
2870 2871 ptr = next;
2871 2872 }
2872 2873 /* insert new after ptr */
2873 2874 new->za_next = next;
2874 2875 ptr->za_next = new;
2875 2876 return (old);
2876 2877 }
2877 2878
2878 2879 void
2879 2880 free_ip_interface(zone_addr_list_t *zalist)
2880 2881 {
2881 2882 zone_addr_list_t *ptr, *new;
2882 2883
2883 2884 for (ptr = zalist; ptr != NULL; ) {
2884 2885 new = ptr;
2885 2886 ptr = ptr->za_next;
2886 2887 free(new);
2887 2888 }
2888 2889 }
2889 2890
2890 2891 /*
2891 2892 * Add the kernel access control information for the interface names.
2892 2893 * If anything goes wrong, we log a general error message, attempt to tear down
2893 2894 * whatever we set up, and return an error.
2894 2895 */
2895 2896 static int
2896 2897 configure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid)
2897 2898 {
2898 2899 zone_dochandle_t handle;
2899 2900 struct zone_nwiftab nwiftab;
2900 2901 char rootpath[MAXPATHLEN];
2901 2902 char path[MAXPATHLEN];
2902 2903 datalink_id_t linkid;
2903 2904 di_prof_t prof = NULL;
2904 2905 boolean_t added = B_FALSE;
2905 2906 zone_addr_list_t *zalist = NULL, *new;
2906 2907
2907 2908 if ((handle = zonecfg_init_handle()) == NULL) {
2908 2909 zerror(zlogp, B_TRUE, "getting zone configuration handle");
2909 2910 return (-1);
2910 2911 }
2911 2912 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2912 2913 zerror(zlogp, B_FALSE, "invalid configuration");
2913 2914 zonecfg_fini_handle(handle);
2914 2915 return (-1);
2915 2916 }
2916 2917
2917 2918 if (zonecfg_setnwifent(handle) != Z_OK) {
2918 2919 zonecfg_fini_handle(handle);
2919 2920 return (0);
2920 2921 }
2921 2922
2922 2923 for (;;) {
2923 2924 if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
2924 2925 break;
2925 2926
2926 2927 if (prof == NULL) {
2927 2928 if (zone_get_devroot(zone_name, rootpath,
2928 2929 sizeof (rootpath)) != Z_OK) {
2929 2930 (void) zonecfg_endnwifent(handle);
2930 2931 zonecfg_fini_handle(handle);
2931 2932 zerror(zlogp, B_TRUE,
2932 2933 "unable to determine dev root");
2933 2934 return (-1);
2934 2935 }
2935 2936 (void) snprintf(path, sizeof (path), "%s%s", rootpath,
2936 2937 "/dev");
2937 2938 if (di_prof_init(path, &prof) != 0) {
2938 2939 (void) zonecfg_endnwifent(handle);
2939 2940 zonecfg_fini_handle(handle);
2940 2941 zerror(zlogp, B_TRUE,
2941 2942 "failed to initialize profile");
2942 2943 return (-1);
2943 2944 }
2944 2945 }
2945 2946
2946 2947 /*
2947 2948 * Create the /dev entry for backward compatibility.
2948 2949 * Only create the /dev entry if it's not in use.
2949 2950 * Note that the zone still boots when the assigned
2950 2951 * interface is inaccessible, used by others, etc.
2951 2952 * Also, when vanity naming is used, some interface do
2952 2953 * do not have corresponding /dev node names (for example,
2953 2954 * vanity named aggregations). The /dev entry is not
2954 2955 * created in that case. The /dev/net entry is always
2955 2956 * accessible.
2956 2957 */
2957 2958 if (dladm_name2info(dld_handle, nwiftab.zone_nwif_physical,
2958 2959 &linkid, NULL, NULL, NULL) == DLADM_STATUS_OK &&
2959 2960 add_datalink(zlogp, zone_name, linkid,
2960 2961 nwiftab.zone_nwif_physical) == 0) {
2961 2962 added = B_TRUE;
2962 2963 } else {
2963 2964 (void) zonecfg_endnwifent(handle);
2964 2965 zonecfg_fini_handle(handle);
2965 2966 zerror(zlogp, B_TRUE, "failed to add network device");
2966 2967 return (-1);
2967 2968 }
2968 2969 /* set up the new IP interface, and add them all later */
2969 2970 new = malloc(sizeof (*new));
2970 2971 if (new == NULL) {
2971 2972 zerror(zlogp, B_TRUE, "no memory for %s",
2972 2973 nwiftab.zone_nwif_physical);
2973 2974 zonecfg_fini_handle(handle);
2974 2975 free_ip_interface(zalist);
2975 2976 }
2976 2977 bzero(new, sizeof (*new));
2977 2978 new->za_nwiftab = nwiftab;
2978 2979 new->za_linkid = linkid;
2979 2980 zalist = add_ip_interface(zalist, new);
2980 2981 }
2981 2982 if (zalist != NULL) {
2982 2983 if ((errno = add_net(zlogp, zoneid, zalist)) != 0) {
2983 2984 (void) zonecfg_endnwifent(handle);
2984 2985 zonecfg_fini_handle(handle);
2985 2986 zerror(zlogp, B_TRUE, "failed to add address");
2986 2987 free_ip_interface(zalist);
2987 2988 return (-1);
2988 2989 }
2989 2990 free_ip_interface(zalist);
2990 2991 }
2991 2992 (void) zonecfg_endnwifent(handle);
2992 2993 zonecfg_fini_handle(handle);
2993 2994
2994 2995 if (prof != NULL && added) {
2995 2996 if (di_prof_commit(prof) != 0) {
2996 2997 zerror(zlogp, B_TRUE, "failed to commit profile");
2997 2998 return (-1);
2998 2999 }
2999 3000 }
3000 3001 if (prof != NULL)
3001 3002 di_prof_fini(prof);
3002 3003
3003 3004 return (0);
3004 3005 }
3005 3006
3006 3007 static int
3007 3008 remove_datalink_pool(zlog_t *zlogp, zoneid_t zoneid)
3008 3009 {
3009 3010 ushort_t flags;
3010 3011 zone_iptype_t iptype;
3011 3012 int i, dlnum = 0;
3012 3013 datalink_id_t *dllink, *dllinks = NULL;
3013 3014 dladm_status_t err;
3014 3015
3015 3016 if (strlen(pool_name) == 0)
3016 3017 return (0);
3017 3018
3018 3019 if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
3019 3020 sizeof (flags)) < 0) {
3020 3021 if (vplat_get_iptype(zlogp, &iptype) < 0) {
3021 3022 zerror(zlogp, B_FALSE, "unable to determine ip-type");
3022 3023 return (-1);
3023 3024 }
3024 3025 } else {
3025 3026 if (flags & ZF_NET_EXCL)
3026 3027 iptype = ZS_EXCLUSIVE;
3027 3028 else
3028 3029 iptype = ZS_SHARED;
3029 3030 }
3030 3031
3031 3032 if (iptype == ZS_EXCLUSIVE) {
3032 3033 /*
3033 3034 * Get the datalink count and for each datalink,
3034 3035 * attempt to clear the pool property and clear
3035 3036 * the pool_name.
3036 3037 */
3037 3038 if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
3038 3039 zerror(zlogp, B_TRUE, "unable to count network "
3039 3040 "interfaces");
3040 3041 return (-1);
3041 3042 }
3042 3043
3043 3044 if (dlnum == 0)
3044 3045 return (0);
3045 3046
3046 3047 if ((dllinks = malloc(dlnum * sizeof (datalink_id_t)))
3047 3048 == NULL) {
3048 3049 zerror(zlogp, B_TRUE, "memory allocation failed");
3049 3050 return (-1);
3050 3051 }
3051 3052 if (zone_list_datalink(zoneid, &dlnum, dllinks) != 0) {
3052 3053 zerror(zlogp, B_TRUE, "unable to list network "
3053 3054 "interfaces");
3054 3055 return (-1);
3055 3056 }
3056 3057
3057 3058 bzero(pool_name, sizeof (pool_name));
3058 3059 for (i = 0, dllink = dllinks; i < dlnum; i++, dllink++) {
3059 3060 err = dladm_set_linkprop(dld_handle, *dllink, "pool",
3060 3061 NULL, 0, DLADM_OPT_ACTIVE);
3061 3062 if (err != DLADM_STATUS_OK) {
3062 3063 zerror(zlogp, B_TRUE,
3063 3064 "WARNING: unable to clear pool");
3064 3065 }
3065 3066 }
3066 3067 free(dllinks);
3067 3068 }
3068 3069 return (0);
3069 3070 }
3070 3071
3071 3072 static int
3072 3073 remove_datalink_protect(zlog_t *zlogp, zoneid_t zoneid)
3073 3074 {
3074 3075 ushort_t flags;
3075 3076 zone_iptype_t iptype;
3076 3077 int i, dlnum = 0;
3077 3078 dladm_status_t dlstatus;
3078 3079 datalink_id_t *dllink, *dllinks = NULL;
3079 3080
3080 3081 if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
3081 3082 sizeof (flags)) < 0) {
3082 3083 if (vplat_get_iptype(zlogp, &iptype) < 0) {
3083 3084 zerror(zlogp, B_FALSE, "unable to determine ip-type");
3084 3085 return (-1);
3085 3086 }
3086 3087 } else {
3087 3088 if (flags & ZF_NET_EXCL)
3088 3089 iptype = ZS_EXCLUSIVE;
3089 3090 else
3090 3091 iptype = ZS_SHARED;
3091 3092 }
3092 3093
3093 3094 if (iptype != ZS_EXCLUSIVE)
3094 3095 return (0);
3095 3096
3096 3097 /*
3097 3098 * Get the datalink count and for each datalink,
3098 3099 * attempt to clear the pool property and clear
3099 3100 * the pool_name.
3100 3101 */
3101 3102 if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
3102 3103 zerror(zlogp, B_TRUE, "unable to count network interfaces");
3103 3104 return (-1);
3104 3105 }
3105 3106
3106 3107 if (dlnum == 0)
3107 3108 return (0);
3108 3109
3109 3110 if ((dllinks = malloc(dlnum * sizeof (datalink_id_t))) == NULL) {
3110 3111 zerror(zlogp, B_TRUE, "memory allocation failed");
3111 3112 return (-1);
3112 3113 }
3113 3114 if (zone_list_datalink(zoneid, &dlnum, dllinks) != 0) {
3114 3115 zerror(zlogp, B_TRUE, "unable to list network interfaces");
3115 3116 free(dllinks);
3116 3117 return (-1);
3117 3118 }
3118 3119
3119 3120 for (i = 0, dllink = dllinks; i < dlnum; i++, dllink++) {
3120 3121 char dlerr[DLADM_STRSIZE];
3121 3122
3122 3123 dlstatus = dladm_set_linkprop(dld_handle, *dllink,
3123 3124 "protection", NULL, 0, DLADM_OPT_ACTIVE);
3124 3125 if (dlstatus == DLADM_STATUS_NOTFOUND) {
3125 3126 /* datalink does not belong to the GZ */
3126 3127 continue;
3127 3128 }
3128 3129 if (dlstatus != DLADM_STATUS_OK) {
3129 3130 zerror(zlogp, B_FALSE,
3130 3131 dladm_status2str(dlstatus, dlerr));
3131 3132 free(dllinks);
3132 3133 return (-1);
3133 3134 }
3134 3135 dlstatus = dladm_set_linkprop(dld_handle, *dllink,
3135 3136 "allowed-ips", NULL, 0, DLADM_OPT_ACTIVE);
3136 3137 if (dlstatus != DLADM_STATUS_OK) {
3137 3138 zerror(zlogp, B_FALSE,
3138 3139 dladm_status2str(dlstatus, dlerr));
3139 3140 free(dllinks);
3140 3141 return (-1);
3141 3142 }
3142 3143 }
3143 3144 free(dllinks);
3144 3145 return (0);
3145 3146 }
3146 3147
3147 3148 static int
3148 3149 unconfigure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid)
3149 3150 {
3150 3151 int dlnum = 0;
3151 3152
3152 3153 /*
3153 3154 * The kernel shutdown callback for the dls module should have removed
3154 3155 * all datalinks from this zone. If any remain, then there's a
3155 3156 * problem.
3156 3157 */
3157 3158 if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
3158 3159 zerror(zlogp, B_TRUE, "unable to list network interfaces");
3159 3160 return (-1);
3160 3161 }
3161 3162 if (dlnum != 0) {
3162 3163 zerror(zlogp, B_FALSE,
3163 3164 "datalinks remain in zone after shutdown");
3164 3165 return (-1);
3165 3166 }
3166 3167 return (0);
3167 3168 }
3168 3169
3169 3170 static int
3170 3171 tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid,
3171 3172 const struct sockaddr_storage *local, const struct sockaddr_storage *remote)
3172 3173 {
3173 3174 int fd;
3174 3175 struct strioctl ioc;
3175 3176 tcp_ioc_abort_conn_t conn;
3176 3177 int error;
3177 3178
3178 3179 conn.ac_local = *local;
3179 3180 conn.ac_remote = *remote;
3180 3181 conn.ac_start = TCPS_SYN_SENT;
3181 3182 conn.ac_end = TCPS_TIME_WAIT;
3182 3183 conn.ac_zoneid = zoneid;
3183 3184
3184 3185 ioc.ic_cmd = TCP_IOC_ABORT_CONN;
3185 3186 ioc.ic_timout = -1; /* infinite timeout */
3186 3187 ioc.ic_len = sizeof (conn);
3187 3188 ioc.ic_dp = (char *)&conn;
3188 3189
3189 3190 if ((fd = open("/dev/tcp", O_RDONLY)) < 0) {
3190 3191 zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp");
3191 3192 return (-1);
3192 3193 }
3193 3194
3194 3195 error = ioctl(fd, I_STR, &ioc);
3195 3196 (void) close(fd);
3196 3197 if (error == 0 || errno == ENOENT) /* ENOENT is not an error */
3197 3198 return (0);
3198 3199 return (-1);
3199 3200 }
3200 3201
3201 3202 static int
3202 3203 tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid)
3203 3204 {
3204 3205 struct sockaddr_storage l, r;
3205 3206 struct sockaddr_in *local, *remote;
3206 3207 struct sockaddr_in6 *local6, *remote6;
3207 3208 int error;
3208 3209
3209 3210 /*
3210 3211 * Abort IPv4 connections.
3211 3212 */
3212 3213 bzero(&l, sizeof (*local));
3213 3214 local = (struct sockaddr_in *)&l;
3214 3215 local->sin_family = AF_INET;
3215 3216 local->sin_addr.s_addr = INADDR_ANY;
3216 3217 local->sin_port = 0;
3217 3218
3218 3219 bzero(&r, sizeof (*remote));
3219 3220 remote = (struct sockaddr_in *)&r;
3220 3221 remote->sin_family = AF_INET;
3221 3222 remote->sin_addr.s_addr = INADDR_ANY;
3222 3223 remote->sin_port = 0;
3223 3224
3224 3225 if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
3225 3226 return (error);
3226 3227
3227 3228 /*
3228 3229 * Abort IPv6 connections.
3229 3230 */
3230 3231 bzero(&l, sizeof (*local6));
3231 3232 local6 = (struct sockaddr_in6 *)&l;
3232 3233 local6->sin6_family = AF_INET6;
3233 3234 local6->sin6_port = 0;
3234 3235 local6->sin6_addr = in6addr_any;
3235 3236
3236 3237 bzero(&r, sizeof (*remote6));
3237 3238 remote6 = (struct sockaddr_in6 *)&r;
3238 3239 remote6->sin6_family = AF_INET6;
3239 3240 remote6->sin6_port = 0;
3240 3241 remote6->sin6_addr = in6addr_any;
3241 3242
3242 3243 if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
3243 3244 return (error);
3244 3245 return (0);
3245 3246 }
3246 3247
3247 3248 static int
3248 3249 get_privset(zlog_t *zlogp, priv_set_t *privs, zone_mnt_t mount_cmd)
3249 3250 {
3250 3251 int error = -1;
3251 3252 zone_dochandle_t handle;
3252 3253 char *privname = NULL;
3253 3254
3254 3255 if ((handle = zonecfg_init_handle()) == NULL) {
3255 3256 zerror(zlogp, B_TRUE, "getting zone configuration handle");
3256 3257 return (-1);
3257 3258 }
3258 3259 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3259 3260 zerror(zlogp, B_FALSE, "invalid configuration");
3260 3261 zonecfg_fini_handle(handle);
3261 3262 return (-1);
3262 3263 }
3263 3264
3264 3265 if (ALT_MOUNT(mount_cmd)) {
3265 3266 zone_iptype_t iptype;
3266 3267 const char *curr_iptype;
3267 3268
3268 3269 if (zonecfg_get_iptype(handle, &iptype) != Z_OK) {
3269 3270 zerror(zlogp, B_TRUE, "unable to determine ip-type");
3270 3271 zonecfg_fini_handle(handle);
3271 3272 return (-1);
3272 3273 }
3273 3274
3274 3275 switch (iptype) {
3275 3276 case ZS_SHARED:
3276 3277 curr_iptype = "shared";
3277 3278 break;
3278 3279 case ZS_EXCLUSIVE:
3279 3280 curr_iptype = "exclusive";
3280 3281 break;
3281 3282 }
3282 3283
3283 3284 if (zonecfg_default_privset(privs, curr_iptype) == Z_OK) {
3284 3285 zonecfg_fini_handle(handle);
3285 3286 return (0);
3286 3287 }
3287 3288 zerror(zlogp, B_FALSE,
3288 3289 "failed to determine the zone's default privilege set");
3289 3290 zonecfg_fini_handle(handle);
3290 3291 return (-1);
3291 3292 }
3292 3293
3293 3294 switch (zonecfg_get_privset(handle, privs, &privname)) {
3294 3295 case Z_OK:
3295 3296 error = 0;
3296 3297 break;
3297 3298 case Z_PRIV_PROHIBITED:
3298 3299 zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted "
3299 3300 "within the zone's privilege set", privname);
3300 3301 break;
3301 3302 case Z_PRIV_REQUIRED:
3302 3303 zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing "
3303 3304 "from the zone's privilege set", privname);
3304 3305 break;
3305 3306 case Z_PRIV_UNKNOWN:
3306 3307 zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified "
3307 3308 "in the zone's privilege set", privname);
3308 3309 break;
3309 3310 default:
3310 3311 zerror(zlogp, B_FALSE, "failed to determine the zone's "
3311 3312 "privilege set");
3312 3313 break;
3313 3314 }
3314 3315
3315 3316 free(privname);
3316 3317 zonecfg_fini_handle(handle);
3317 3318 return (error);
3318 3319 }
3319 3320
3320 3321 static int
3321 3322 get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep)
3322 3323 {
3323 3324 nvlist_t *nvl = NULL;
3324 3325 char *nvl_packed = NULL;
3325 3326 size_t nvl_size = 0;
3326 3327 nvlist_t **nvlv = NULL;
3327 3328 int rctlcount = 0;
3328 3329 int error = -1;
3329 3330 zone_dochandle_t handle;
3330 3331 struct zone_rctltab rctltab;
3331 3332 rctlblk_t *rctlblk = NULL;
3332 3333 uint64_t maxlwps;
3333 3334 uint64_t maxprocs;
3334 3335
3335 3336 *bufp = NULL;
3336 3337 *bufsizep = 0;
3337 3338
3338 3339 if ((handle = zonecfg_init_handle()) == NULL) {
3339 3340 zerror(zlogp, B_TRUE, "getting zone configuration handle");
3340 3341 return (-1);
3341 3342 }
3342 3343 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3343 3344 zerror(zlogp, B_FALSE, "invalid configuration");
3344 3345 zonecfg_fini_handle(handle);
3345 3346 return (-1);
3346 3347 }
3347 3348
3348 3349 rctltab.zone_rctl_valptr = NULL;
3349 3350 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
3350 3351 zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc");
3351 3352 goto out;
3352 3353 }
3353 3354
3354 3355 /*
3355 3356 * Allow the administrator to control both the maximum number of
3356 3357 * process table slots and the maximum number of lwps with just the
3357 3358 * max-processes property. If only the max-processes property is set,
3358 3359 * we add a max-lwps property with a limit derived from max-processes.
3359 3360 */
3360 3361 if (zonecfg_get_aliased_rctl(handle, ALIAS_MAXPROCS, &maxprocs)
3361 3362 == Z_OK &&
3362 3363 zonecfg_get_aliased_rctl(handle, ALIAS_MAXLWPS, &maxlwps)
3363 3364 == Z_NO_ENTRY) {
3364 3365 if (zonecfg_set_aliased_rctl(handle, ALIAS_MAXLWPS,
3365 3366 maxprocs * LWPS_PER_PROCESS) != Z_OK) {
3366 3367 zerror(zlogp, B_FALSE, "unable to set max-lwps alias");
3367 3368 goto out;
3368 3369 }
3369 3370 }
3370 3371
3371 3372 if (zonecfg_setrctlent(handle) != Z_OK) {
3372 3373 zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent");
3373 3374 goto out;
3374 3375 }
3375 3376
3376 3377 if ((rctlblk = malloc(rctlblk_size())) == NULL) {
3377 3378 zerror(zlogp, B_TRUE, "memory allocation failed");
3378 3379 goto out;
3379 3380 }
3380 3381 while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
3381 3382 struct zone_rctlvaltab *rctlval;
3382 3383 uint_t i, count;
3383 3384 const char *name = rctltab.zone_rctl_name;
3384 3385
3385 3386 /* zoneadm should have already warned about unknown rctls. */
3386 3387 if (!zonecfg_is_rctl(name)) {
3387 3388 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
3388 3389 rctltab.zone_rctl_valptr = NULL;
3389 3390 continue;
3390 3391 }
3391 3392 count = 0;
3392 3393 for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
3393 3394 rctlval = rctlval->zone_rctlval_next) {
3394 3395 count++;
3395 3396 }
3396 3397 if (count == 0) { /* ignore */
3397 3398 continue; /* Nothing to free */
3398 3399 }
3399 3400 if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL)
3400 3401 goto out;
3401 3402 i = 0;
3402 3403 for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
3403 3404 rctlval = rctlval->zone_rctlval_next, i++) {
3404 3405 if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) {
3405 3406 zerror(zlogp, B_TRUE, "%s failed",
3406 3407 "nvlist_alloc");
3407 3408 goto out;
3408 3409 }
3409 3410 if (zonecfg_construct_rctlblk(rctlval, rctlblk)
3410 3411 != Z_OK) {
3411 3412 zerror(zlogp, B_FALSE, "invalid rctl value: "
3412 3413 "(priv=%s,limit=%s,action=%s)",
3413 3414 rctlval->zone_rctlval_priv,
3414 3415 rctlval->zone_rctlval_limit,
3415 3416 rctlval->zone_rctlval_action);
3416 3417 goto out;
3417 3418 }
3418 3419 if (!zonecfg_valid_rctl(name, rctlblk)) {
3419 3420 zerror(zlogp, B_FALSE,
3420 3421 "(priv=%s,limit=%s,action=%s) is not a "
3421 3422 "valid value for rctl '%s'",
3422 3423 rctlval->zone_rctlval_priv,
3423 3424 rctlval->zone_rctlval_limit,
3424 3425 rctlval->zone_rctlval_action,
3425 3426 name);
3426 3427 goto out;
3427 3428 }
3428 3429 if (nvlist_add_uint64(nvlv[i], "privilege",
3429 3430 rctlblk_get_privilege(rctlblk)) != 0) {
3430 3431 zerror(zlogp, B_FALSE, "%s failed",
3431 3432 "nvlist_add_uint64");
3432 3433 goto out;
3433 3434 }
3434 3435 if (nvlist_add_uint64(nvlv[i], "limit",
3435 3436 rctlblk_get_value(rctlblk)) != 0) {
3436 3437 zerror(zlogp, B_FALSE, "%s failed",
3437 3438 "nvlist_add_uint64");
3438 3439 goto out;
3439 3440 }
3440 3441 if (nvlist_add_uint64(nvlv[i], "action",
3441 3442 (uint_t)rctlblk_get_local_action(rctlblk, NULL))
3442 3443 != 0) {
3443 3444 zerror(zlogp, B_FALSE, "%s failed",
3444 3445 "nvlist_add_uint64");
3445 3446 goto out;
3446 3447 }
3447 3448 }
3448 3449 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
3449 3450 rctltab.zone_rctl_valptr = NULL;
3450 3451 if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count)
3451 3452 != 0) {
3452 3453 zerror(zlogp, B_FALSE, "%s failed",
3453 3454 "nvlist_add_nvlist_array");
3454 3455 goto out;
3455 3456 }
3456 3457 for (i = 0; i < count; i++)
3457 3458 nvlist_free(nvlv[i]);
3458 3459 free(nvlv);
3459 3460 nvlv = NULL;
3460 3461 rctlcount++;
3461 3462 }
3462 3463 (void) zonecfg_endrctlent(handle);
3463 3464
3464 3465 if (rctlcount == 0) {
3465 3466 error = 0;
3466 3467 goto out;
3467 3468 }
3468 3469 if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0)
3469 3470 != 0) {
3470 3471 zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack");
3471 3472 goto out;
3472 3473 }
3473 3474
3474 3475 error = 0;
3475 3476 *bufp = nvl_packed;
3476 3477 *bufsizep = nvl_size;
3477 3478
3478 3479 out:
3479 3480 free(rctlblk);
3480 3481 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
3481 3482 if (error && nvl_packed != NULL)
3482 3483 free(nvl_packed);
3483 3484 nvlist_free(nvl);
3484 3485 if (nvlv != NULL)
3485 3486 free(nvlv);
3486 3487 if (handle != NULL)
3487 3488 zonecfg_fini_handle(handle);
3488 3489 return (error);
3489 3490 }
3490 3491
3491 3492 static int
3492 3493 get_implicit_datasets(zlog_t *zlogp, char **retstr)
3493 3494 {
3494 3495 char cmdbuf[2 * MAXPATHLEN];
3495 3496
3496 3497 if (query_hook[0] == '\0')
3497 3498 return (0);
3498 3499
3499 3500 if (snprintf(cmdbuf, sizeof (cmdbuf), "%s datasets", query_hook)
3500 3501 > sizeof (cmdbuf))
3501 3502 return (-1);
3502 3503
3503 3504 if (do_subproc(zlogp, cmdbuf, retstr) != 0)
3504 3505 return (-1);
3505 3506
3506 3507 return (0);
3507 3508 }
3508 3509
3509 3510 static int
3510 3511 get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep)
3511 3512 {
3512 3513 zone_dochandle_t handle;
3513 3514 struct zone_dstab dstab;
3514 3515 size_t total, offset, len;
3515 3516 int error = -1;
3516 3517 char *str = NULL;
3517 3518 char *implicit_datasets = NULL;
3518 3519 int implicit_len = 0;
3519 3520
3520 3521 *bufp = NULL;
3521 3522 *bufsizep = 0;
3522 3523
3523 3524 if ((handle = zonecfg_init_handle()) == NULL) {
3524 3525 zerror(zlogp, B_TRUE, "getting zone configuration handle");
3525 3526 return (-1);
3526 3527 }
3527 3528 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3528 3529 zerror(zlogp, B_FALSE, "invalid configuration");
3529 3530 zonecfg_fini_handle(handle);
3530 3531 return (-1);
3531 3532 }
3532 3533
3533 3534 if (get_implicit_datasets(zlogp, &implicit_datasets) != 0) {
3534 3535 zerror(zlogp, B_FALSE, "getting implicit datasets failed");
3535 3536 goto out;
3536 3537 }
3537 3538
3538 3539 if (zonecfg_setdsent(handle) != Z_OK) {
3539 3540 zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3540 3541 goto out;
3541 3542 }
3542 3543
3543 3544 total = 0;
3544 3545 while (zonecfg_getdsent(handle, &dstab) == Z_OK)
3545 3546 total += strlen(dstab.zone_dataset_name) + 1;
3546 3547 (void) zonecfg_enddsent(handle);
3547 3548
3548 3549 if (implicit_datasets != NULL)
3549 3550 implicit_len = strlen(implicit_datasets);
3550 3551 if (implicit_len > 0)
3551 3552 total += implicit_len + 1;
3552 3553
3553 3554 if (total == 0) {
3554 3555 error = 0;
3555 3556 goto out;
3556 3557 }
3557 3558
3558 3559 if ((str = malloc(total)) == NULL) {
3559 3560 zerror(zlogp, B_TRUE, "memory allocation failed");
3560 3561 goto out;
3561 3562 }
3562 3563
3563 3564 if (zonecfg_setdsent(handle) != Z_OK) {
3564 3565 zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3565 3566 goto out;
3566 3567 }
3567 3568 offset = 0;
3568 3569 while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3569 3570 len = strlen(dstab.zone_dataset_name);
3570 3571 (void) strlcpy(str + offset, dstab.zone_dataset_name,
3571 3572 total - offset);
3572 3573 offset += len;
3573 3574 if (offset < total - 1)
3574 3575 str[offset++] = ',';
3575 3576 }
3576 3577 (void) zonecfg_enddsent(handle);
3577 3578
3578 3579 if (implicit_len > 0)
3579 3580 (void) strlcpy(str + offset, implicit_datasets, total - offset);
3580 3581
3581 3582 error = 0;
3582 3583 *bufp = str;
3583 3584 *bufsizep = total;
3584 3585
3585 3586 out:
3586 3587 if (error != 0 && str != NULL)
3587 3588 free(str);
3588 3589 if (handle != NULL)
3589 3590 zonecfg_fini_handle(handle);
3590 3591 if (implicit_datasets != NULL)
3591 3592 free(implicit_datasets);
3592 3593
3593 3594 return (error);
3594 3595 }
3595 3596
3596 3597 static int
3597 3598 validate_datasets(zlog_t *zlogp)
3598 3599 {
3599 3600 zone_dochandle_t handle;
3600 3601 struct zone_dstab dstab;
3601 3602 zfs_handle_t *zhp;
3602 3603 libzfs_handle_t *hdl;
3603 3604
3604 3605 if ((handle = zonecfg_init_handle()) == NULL) {
3605 3606 zerror(zlogp, B_TRUE, "getting zone configuration handle");
3606 3607 return (-1);
3607 3608 }
3608 3609 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3609 3610 zerror(zlogp, B_FALSE, "invalid configuration");
3610 3611 zonecfg_fini_handle(handle);
3611 3612 return (-1);
3612 3613 }
3613 3614
3614 3615 if (zonecfg_setdsent(handle) != Z_OK) {
3615 3616 zerror(zlogp, B_FALSE, "invalid configuration");
3616 3617 zonecfg_fini_handle(handle);
3617 3618 return (-1);
3618 3619 }
3619 3620
3620 3621 if ((hdl = libzfs_init()) == NULL) {
3621 3622 zerror(zlogp, B_FALSE, "opening ZFS library");
3622 3623 zonecfg_fini_handle(handle);
3623 3624 return (-1);
3624 3625 }
3625 3626
3626 3627 while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3627 3628
3628 3629 if ((zhp = zfs_open(hdl, dstab.zone_dataset_name,
3629 3630 ZFS_TYPE_FILESYSTEM)) == NULL) {
3630 3631 zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'",
3631 3632 dstab.zone_dataset_name);
3632 3633 zonecfg_fini_handle(handle);
3633 3634 libzfs_fini(hdl);
3634 3635 return (-1);
3635 3636 }
3636 3637
3637 3638 /*
3638 3639 * Automatically set the 'zoned' property. We check the value
3639 3640 * first because we'll get EPERM if it is already set.
3640 3641 */
3641 3642 if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
3642 3643 zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_ZONED),
3643 3644 "on") != 0) {
3644 3645 zerror(zlogp, B_FALSE, "cannot set 'zoned' "
3645 3646 "property for ZFS dataset '%s'\n",
3646 3647 dstab.zone_dataset_name);
3647 3648 zonecfg_fini_handle(handle);
3648 3649 zfs_close(zhp);
3649 3650 libzfs_fini(hdl);
3650 3651 return (-1);
3651 3652 }
3652 3653
3653 3654 zfs_close(zhp);
3654 3655 }
3655 3656 (void) zonecfg_enddsent(handle);
3656 3657
3657 3658 zonecfg_fini_handle(handle);
3658 3659 libzfs_fini(hdl);
3659 3660
3660 3661 return (0);
3661 3662 }
3662 3663
3663 3664 /*
3664 3665 * Return true if the path is its own zfs file system. We determine this
3665 3666 * by stat-ing the path to see if it is zfs and stat-ing the parent to see
3666 3667 * if it is a different fs.
3667 3668 */
3668 3669 boolean_t
3669 3670 is_zonepath_zfs(char *zonepath)
3670 3671 {
3671 3672 int res;
3672 3673 char *path;
3673 3674 char *parent;
3674 3675 struct statvfs64 buf1, buf2;
3675 3676
3676 3677 if (statvfs64(zonepath, &buf1) != 0)
3677 3678 return (B_FALSE);
3678 3679
3679 3680 if (strcmp(buf1.f_basetype, "zfs") != 0)
3680 3681 return (B_FALSE);
3681 3682
3682 3683 if ((path = strdup(zonepath)) == NULL)
3683 3684 return (B_FALSE);
3684 3685
3685 3686 parent = dirname(path);
3686 3687 res = statvfs64(parent, &buf2);
3687 3688 free(path);
3688 3689
3689 3690 if (res != 0)
3690 3691 return (B_FALSE);
3691 3692
3692 3693 if (buf1.f_fsid == buf2.f_fsid)
3693 3694 return (B_FALSE);
3694 3695
3695 3696 return (B_TRUE);
3696 3697 }
3697 3698
3698 3699 /*
3699 3700 * Verify the MAC label in the root dataset for the zone.
3700 3701 * If the label exists, it must match the label configured for the zone.
3701 3702 * Otherwise if there's no label on the dataset, create one here.
3702 3703 */
3703 3704
3704 3705 static int
3705 3706 validate_rootds_label(zlog_t *zlogp, char *rootpath, m_label_t *zone_sl)
3706 3707 {
3707 3708 int error = -1;
3708 3709 zfs_handle_t *zhp;
3709 3710 libzfs_handle_t *hdl;
3710 3711 m_label_t ds_sl;
3711 3712 char zonepath[MAXPATHLEN];
3712 3713 char ds_hexsl[MAXNAMELEN];
3713 3714
3714 3715 if (!is_system_labeled())
3715 3716 return (0);
3716 3717
3717 3718 if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
3718 3719 zerror(zlogp, B_TRUE, "unable to determine zone path");
3719 3720 return (-1);
3720 3721 }
3721 3722
3722 3723 if (!is_zonepath_zfs(zonepath))
3723 3724 return (0);
3724 3725
3725 3726 if ((hdl = libzfs_init()) == NULL) {
3726 3727 zerror(zlogp, B_FALSE, "opening ZFS library");
3727 3728 return (-1);
3728 3729 }
3729 3730
3730 3731 if ((zhp = zfs_path_to_zhandle(hdl, rootpath,
3731 3732 ZFS_TYPE_FILESYSTEM)) == NULL) {
3732 3733 zerror(zlogp, B_FALSE, "cannot open ZFS dataset for path '%s'",
3733 3734 rootpath);
3734 3735 libzfs_fini(hdl);
3735 3736 return (-1);
3736 3737 }
3737 3738
3738 3739 /* Get the mlslabel property if it exists. */
3739 3740 if ((zfs_prop_get(zhp, ZFS_PROP_MLSLABEL, ds_hexsl, MAXNAMELEN,
3740 3741 NULL, NULL, 0, B_TRUE) != 0) ||
3741 3742 (strcmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)) {
3742 3743 char *str2 = NULL;
3743 3744
3744 3745 /*
3745 3746 * No label on the dataset (or default only); create one.
3746 3747 * (Only do this automatic labeling for the labeled brand.)
3747 3748 */
3748 3749 if (strcmp(brand_name, LABELED_BRAND_NAME) != 0) {
3749 3750 error = 0;
3750 3751 goto out;
3751 3752 }
3752 3753
3753 3754 error = l_to_str_internal(zone_sl, &str2);
3754 3755 if (error)
3755 3756 goto out;
3756 3757 if (str2 == NULL) {
3757 3758 error = -1;
3758 3759 goto out;
3759 3760 }
3760 3761 if ((error = zfs_prop_set(zhp,
3761 3762 zfs_prop_to_name(ZFS_PROP_MLSLABEL), str2)) != 0) {
3762 3763 zerror(zlogp, B_FALSE, "cannot set 'mlslabel' "
3763 3764 "property for root dataset at '%s'\n", rootpath);
3764 3765 }
3765 3766 free(str2);
3766 3767 goto out;
3767 3768 }
3768 3769
3769 3770 /* Convert the retrieved dataset label to binary form. */
3770 3771 error = hexstr_to_label(ds_hexsl, &ds_sl);
3771 3772 if (error) {
3772 3773 zerror(zlogp, B_FALSE, "invalid 'mlslabel' "
3773 3774 "property on root dataset at '%s'\n", rootpath);
3774 3775 goto out; /* exit with error */
3775 3776 }
3776 3777
3777 3778 /*
3778 3779 * Perform a MAC check by comparing the zone label with the
3779 3780 * dataset label.
3780 3781 */
3781 3782 error = (!blequal(zone_sl, &ds_sl));
3782 3783 if (error)
3783 3784 zerror(zlogp, B_FALSE, "Rootpath dataset has mismatched label");
3784 3785 out:
3785 3786 zfs_close(zhp);
3786 3787 libzfs_fini(hdl);
3787 3788
3788 3789 return (error);
3789 3790 }
3790 3791
3791 3792 /*
3792 3793 * Mount lower level home directories into/from current zone
3793 3794 * Share exported directories specified in dfstab for zone
3794 3795 */
3795 3796 static int
3796 3797 tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath)
3797 3798 {
3798 3799 zoneid_t *zids = NULL;
3799 3800 priv_set_t *zid_privs;
3800 3801 const priv_impl_info_t *ip = NULL;
3801 3802 uint_t nzents_saved;
3802 3803 uint_t nzents;
3803 3804 int i;
3804 3805 char readonly[] = "ro";
3805 3806 struct zone_fstab lower_fstab;
3806 3807 char *argv[4];
3807 3808
3808 3809 if (!is_system_labeled())
3809 3810 return (0);
3810 3811
3811 3812 if (zid_label == NULL) {
3812 3813 zid_label = m_label_alloc(MAC_LABEL);
3813 3814 if (zid_label == NULL)
3814 3815 return (-1);
3815 3816 }
3816 3817
3817 3818 /* Make sure our zone has an /export/home dir */
3818 3819 (void) make_one_dir(zlogp, rootpath, "/export/home",
3819 3820 DEFAULT_DIR_MODE, DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
3820 3821
3821 3822 lower_fstab.zone_fs_raw[0] = '\0';
3822 3823 (void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS,
3823 3824 sizeof (lower_fstab.zone_fs_type));
3824 3825 lower_fstab.zone_fs_options = NULL;
3825 3826 (void) zonecfg_add_fs_option(&lower_fstab, readonly);
3826 3827
3827 3828 /*
3828 3829 * Get the list of zones from the kernel
3829 3830 */
3830 3831 if (zone_list(NULL, &nzents) != 0) {
3831 3832 zerror(zlogp, B_TRUE, "unable to list zones");
3832 3833 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
3833 3834 return (-1);
3834 3835 }
3835 3836 again:
3836 3837 if (nzents == 0) {
3837 3838 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
3838 3839 return (-1);
3839 3840 }
3840 3841
3841 3842 zids = malloc(nzents * sizeof (zoneid_t));
3842 3843 if (zids == NULL) {
3843 3844 zerror(zlogp, B_TRUE, "memory allocation failed");
3844 3845 return (-1);
3845 3846 }
3846 3847 nzents_saved = nzents;
3847 3848
3848 3849 if (zone_list(zids, &nzents) != 0) {
3849 3850 zerror(zlogp, B_TRUE, "unable to list zones");
3850 3851 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
3851 3852 free(zids);
3852 3853 return (-1);
3853 3854 }
3854 3855 if (nzents != nzents_saved) {
3855 3856 /* list changed, try again */
3856 3857 free(zids);
3857 3858 goto again;
3858 3859 }
3859 3860
3860 3861 ip = getprivimplinfo();
3861 3862 if ((zid_privs = priv_allocset()) == NULL) {
3862 3863 zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
3863 3864 zonecfg_free_fs_option_list(
3864 3865 lower_fstab.zone_fs_options);
3865 3866 free(zids);
3866 3867 return (-1);
3867 3868 }
3868 3869
3869 3870 for (i = 0; i < nzents; i++) {
3870 3871 char zid_name[ZONENAME_MAX];
3871 3872 zone_state_t zid_state;
3872 3873 char zid_rpath[MAXPATHLEN];
3873 3874 struct stat stat_buf;
3874 3875
3875 3876 if (zids[i] == GLOBAL_ZONEID)
3876 3877 continue;
3877 3878
3878 3879 if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
3879 3880 continue;
3880 3881
3881 3882 /*
3882 3883 * Do special setup for the zone we are booting
3883 3884 */
3884 3885 if (strcmp(zid_name, zone_name) == 0) {
3885 3886 struct zone_fstab autofs_fstab;
3886 3887 char map_path[MAXPATHLEN];
3887 3888 int fd;
3888 3889
3889 3890 /*
3890 3891 * Create auto_home_<zone> map for this zone
3891 3892 * in the global zone. The non-global zone entry
3892 3893 * will be created by automount when the zone
3893 3894 * is booted.
3894 3895 */
3895 3896
3896 3897 (void) snprintf(autofs_fstab.zone_fs_special,
3897 3898 MAXPATHLEN, "auto_home_%s", zid_name);
3898 3899
3899 3900 (void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN,
3900 3901 "/zone/%s/home", zid_name);
3901 3902
3902 3903 (void) snprintf(map_path, sizeof (map_path),
3903 3904 "/etc/%s", autofs_fstab.zone_fs_special);
3904 3905 /*
3905 3906 * If the map file doesn't exist create a template
3906 3907 */
3907 3908 if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL,
3908 3909 S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) {
3909 3910 int len;
3910 3911 char map_rec[MAXPATHLEN];
3911 3912
3912 3913 len = snprintf(map_rec, sizeof (map_rec),
3913 3914 "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n",
3914 3915 autofs_fstab.zone_fs_special, rootpath);
3915 3916 (void) write(fd, map_rec, len);
3916 3917 (void) close(fd);
3917 3918 }
3918 3919
3919 3920 /*
3920 3921 * Mount auto_home_<zone> in the global zone if absent.
3921 3922 * If it's already of type autofs, then
3922 3923 * don't mount it again.
3923 3924 */
3924 3925 if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) ||
3925 3926 strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) {
3926 3927 char optstr[] = "indirect,ignore,nobrowse";
3927 3928
3928 3929 (void) make_one_dir(zlogp, "",
3929 3930 autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE,
3930 3931 DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
3931 3932
3932 3933 /*
3933 3934 * Mount will fail if automounter has already
3934 3935 * processed the auto_home_<zonename> map
3935 3936 */
3936 3937 (void) domount(zlogp, MNTTYPE_AUTOFS, optstr,
3937 3938 autofs_fstab.zone_fs_special,
3938 3939 autofs_fstab.zone_fs_dir);
3939 3940 }
3940 3941 continue;
3941 3942 }
3942 3943
3943 3944
3944 3945 if (zone_get_state(zid_name, &zid_state) != Z_OK ||
3945 3946 (zid_state != ZONE_STATE_READY &&
3946 3947 zid_state != ZONE_STATE_RUNNING))
3947 3948 /* Skip over zones without mounted filesystems */
3948 3949 continue;
3949 3950
3950 3951 if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
3951 3952 sizeof (m_label_t)) < 0)
3952 3953 /* Skip over zones with unspecified label */
3953 3954 continue;
3954 3955
3955 3956 if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
3956 3957 sizeof (zid_rpath)) == -1)
3957 3958 /* Skip over zones with bad path */
3958 3959 continue;
3959 3960
3960 3961 if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs,
3961 3962 sizeof (priv_chunk_t) * ip->priv_setsize) == -1)
3962 3963 /* Skip over zones with bad privs */
3963 3964 continue;
3964 3965
3965 3966 /*
3966 3967 * Reading down is valid according to our label model
3967 3968 * but some customers want to disable it because it
3968 3969 * allows execute down and other possible attacks.
3969 3970 * Therefore, we restrict this feature to zones that
3970 3971 * have the NET_MAC_AWARE privilege which is required
3971 3972 * for NFS read-down semantics.
3972 3973 */
3973 3974 if ((bldominates(zlabel, zid_label)) &&
3974 3975 (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) {
3975 3976 /*
3976 3977 * Our zone dominates this one.
3977 3978 * Create a lofs mount from lower zone's /export/home
3978 3979 */
3979 3980 (void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
3980 3981 "%s/zone/%s/export/home", rootpath, zid_name);
3981 3982
3982 3983 /*
3983 3984 * If the target is already an LOFS mount
3984 3985 * then don't do it again.
3985 3986 */
3986 3987 if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
3987 3988 strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
3988 3989
3989 3990 if (snprintf(lower_fstab.zone_fs_special,
3990 3991 MAXPATHLEN, "%s/export",
3991 3992 zid_rpath) > MAXPATHLEN)
3992 3993 continue;
3993 3994
3994 3995 /*
3995 3996 * Make sure the lower-level home exists
3996 3997 */
3997 3998 if (make_one_dir(zlogp,
3998 3999 lower_fstab.zone_fs_special, "/home",
3999 4000 DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
4000 4001 DEFAULT_DIR_GROUP) != 0)
4001 4002 continue;
4002 4003
4003 4004 (void) strlcat(lower_fstab.zone_fs_special,
4004 4005 "/home", MAXPATHLEN);
4005 4006
4006 4007 /*
4007 4008 * Mount can fail because the lower-level
4008 4009 * zone may have already done a mount up.
4009 4010 */
4010 4011 (void) mount_one(zlogp, &lower_fstab, "",
4011 4012 Z_MNT_BOOT);
4012 4013 }
4013 4014 } else if ((bldominates(zid_label, zlabel)) &&
4014 4015 (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) {
4015 4016 /*
4016 4017 * This zone dominates our zone.
4017 4018 * Create a lofs mount from our zone's /export/home
4018 4019 */
4019 4020 if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
4020 4021 "%s/zone/%s/export/home", zid_rpath,
4021 4022 zone_name) > MAXPATHLEN)
4022 4023 continue;
4023 4024
4024 4025 /*
4025 4026 * If the target is already an LOFS mount
4026 4027 * then don't do it again.
4027 4028 */
4028 4029 if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
4029 4030 strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
4030 4031
4031 4032 (void) snprintf(lower_fstab.zone_fs_special,
4032 4033 MAXPATHLEN, "%s/export/home", rootpath);
4033 4034
4034 4035 /*
4035 4036 * Mount can fail because the higher-level
4036 4037 * zone may have already done a mount down.
4037 4038 */
4038 4039 (void) mount_one(zlogp, &lower_fstab, "",
4039 4040 Z_MNT_BOOT);
4040 4041 }
4041 4042 }
4042 4043 }
4043 4044 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
4044 4045 priv_freeset(zid_privs);
4045 4046 free(zids);
4046 4047
4047 4048 /*
4048 4049 * Now share any exported directories from this zone.
4049 4050 * Each zone can have its own dfstab.
4050 4051 */
4051 4052
4052 4053 argv[0] = "zoneshare";
4053 4054 argv[1] = "-z";
4054 4055 argv[2] = zone_name;
4055 4056 argv[3] = NULL;
4056 4057
4057 4058 (void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv);
4058 4059 /* Don't check for errors since they don't affect the zone */
4059 4060
4060 4061 return (0);
4061 4062 }
4062 4063
4063 4064 /*
4064 4065 * Unmount lofs mounts from higher level zones
4065 4066 * Unshare nfs exported directories
4066 4067 */
4067 4068 static void
4068 4069 tsol_unmounts(zlog_t *zlogp, char *zone_name)
4069 4070 {
4070 4071 zoneid_t *zids = NULL;
4071 4072 uint_t nzents_saved;
4072 4073 uint_t nzents;
4073 4074 int i;
4074 4075 char *argv[4];
4075 4076 char path[MAXPATHLEN];
4076 4077
4077 4078 if (!is_system_labeled())
4078 4079 return;
4079 4080
4080 4081 /*
4081 4082 * Get the list of zones from the kernel
4082 4083 */
4083 4084 if (zone_list(NULL, &nzents) != 0) {
4084 4085 return;
4085 4086 }
4086 4087
4087 4088 if (zid_label == NULL) {
4088 4089 zid_label = m_label_alloc(MAC_LABEL);
4089 4090 if (zid_label == NULL)
4090 4091 return;
4091 4092 }
4092 4093
4093 4094 again:
4094 4095 if (nzents == 0)
4095 4096 return;
4096 4097
4097 4098 zids = malloc(nzents * sizeof (zoneid_t));
4098 4099 if (zids == NULL) {
4099 4100 zerror(zlogp, B_TRUE, "memory allocation failed");
4100 4101 return;
4101 4102 }
4102 4103 nzents_saved = nzents;
4103 4104
4104 4105 if (zone_list(zids, &nzents) != 0) {
4105 4106 free(zids);
4106 4107 return;
4107 4108 }
4108 4109 if (nzents != nzents_saved) {
4109 4110 /* list changed, try again */
4110 4111 free(zids);
4111 4112 goto again;
4112 4113 }
4113 4114
4114 4115 for (i = 0; i < nzents; i++) {
4115 4116 char zid_name[ZONENAME_MAX];
4116 4117 zone_state_t zid_state;
4117 4118 char zid_rpath[MAXPATHLEN];
4118 4119
4119 4120 if (zids[i] == GLOBAL_ZONEID)
4120 4121 continue;
4121 4122
4122 4123 if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
4123 4124 continue;
4124 4125
4125 4126 /*
4126 4127 * Skip the zone we are halting
4127 4128 */
4128 4129 if (strcmp(zid_name, zone_name) == 0)
4129 4130 continue;
4130 4131
4131 4132 if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state,
4132 4133 sizeof (zid_state)) < 0) ||
4133 4134 (zid_state < ZONE_IS_READY))
4134 4135 /* Skip over zones without mounted filesystems */
4135 4136 continue;
4136 4137
4137 4138 if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
4138 4139 sizeof (m_label_t)) < 0)
4139 4140 /* Skip over zones with unspecified label */
4140 4141 continue;
4141 4142
4142 4143 if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
4143 4144 sizeof (zid_rpath)) == -1)
4144 4145 /* Skip over zones with bad path */
4145 4146 continue;
4146 4147
4147 4148 if (zlabel != NULL && bldominates(zid_label, zlabel)) {
4148 4149 /*
4149 4150 * This zone dominates our zone.
4150 4151 * Unmount the lofs mount of our zone's /export/home
4151 4152 */
4152 4153
4153 4154 if (snprintf(path, MAXPATHLEN,
4154 4155 "%s/zone/%s/export/home", zid_rpath,
4155 4156 zone_name) > MAXPATHLEN)
4156 4157 continue;
4157 4158
4158 4159 /* Skip over mount failures */
4159 4160 (void) umount(path);
4160 4161 }
4161 4162 }
4162 4163 free(zids);
4163 4164
4164 4165 /*
4165 4166 * Unmount global zone autofs trigger for this zone
4166 4167 */
4167 4168 (void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name);
4168 4169 /* Skip over mount failures */
4169 4170 (void) umount(path);
4170 4171
4171 4172 /*
4172 4173 * Next unshare any exported directories from this zone.
4173 4174 */
4174 4175
4175 4176 argv[0] = "zoneunshare";
4176 4177 argv[1] = "-z";
4177 4178 argv[2] = zone_name;
4178 4179 argv[3] = NULL;
4179 4180
4180 4181 (void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv);
4181 4182 /* Don't check for errors since they don't affect the zone */
4182 4183
4183 4184 /*
4184 4185 * Finally, deallocate any devices in the zone.
4185 4186 */
4186 4187
4187 4188 argv[0] = "deallocate";
4188 4189 argv[1] = "-Isz";
4189 4190 argv[2] = zone_name;
4190 4191 argv[3] = NULL;
4191 4192
4192 4193 (void) forkexec(zlogp, "/usr/sbin/deallocate", argv);
4193 4194 /* Don't check for errors since they don't affect the zone */
4194 4195 }
4195 4196
4196 4197 /*
4197 4198 * Fetch the Trusted Extensions label and multi-level ports (MLPs) for
4198 4199 * this zone.
4199 4200 */
4200 4201 static tsol_zcent_t *
4201 4202 get_zone_label(zlog_t *zlogp, priv_set_t *privs)
4202 4203 {
4203 4204 FILE *fp;
4204 4205 tsol_zcent_t *zcent = NULL;
4205 4206 char line[MAXTNZLEN];
4206 4207
4207 4208 if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) {
4208 4209 zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH);
4209 4210 return (NULL);
4210 4211 }
4211 4212
4212 4213 while (fgets(line, sizeof (line), fp) != NULL) {
4213 4214 /*
4214 4215 * Check for malformed database
4215 4216 */
4216 4217 if (strlen(line) == MAXTNZLEN - 1)
4217 4218 break;
4218 4219 if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL)
4219 4220 continue;
4220 4221 if (strcmp(zcent->zc_name, zone_name) == 0)
4221 4222 break;
4222 4223 tsol_freezcent(zcent);
4223 4224 zcent = NULL;
4224 4225 }
4225 4226 (void) fclose(fp);
4226 4227
4227 4228 if (zcent == NULL) {
4228 4229 zerror(zlogp, B_FALSE, "zone requires a label assignment. "
4229 4230 "See tnzonecfg(4)");
4230 4231 } else {
4231 4232 if (zlabel == NULL)
4232 4233 zlabel = m_label_alloc(MAC_LABEL);
4233 4234 /*
4234 4235 * Save this zone's privileges for later read-down processing
4235 4236 */
4236 4237 if ((zprivs = priv_allocset()) == NULL) {
4237 4238 zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
4238 4239 return (NULL);
4239 4240 } else {
4240 4241 priv_copyset(privs, zprivs);
4241 4242 }
4242 4243 }
4243 4244 return (zcent);
4244 4245 }
4245 4246
4246 4247 /*
4247 4248 * Add the Trusted Extensions multi-level ports for this zone.
4248 4249 */
4249 4250 static void
4250 4251 set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent)
4251 4252 {
4252 4253 tsol_mlp_t *mlp;
4253 4254 tsol_mlpent_t tsme;
4254 4255
4255 4256 if (!is_system_labeled())
4256 4257 return;
4257 4258
4258 4259 tsme.tsme_zoneid = zoneid;
4259 4260 tsme.tsme_flags = 0;
4260 4261 for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) {
4261 4262 tsme.tsme_mlp = *mlp;
4262 4263 if (tnmlp(TNDB_LOAD, &tsme) != 0) {
4263 4264 zerror(zlogp, B_TRUE, "cannot set zone-specific MLP "
4264 4265 "on %d-%d/%d", mlp->mlp_port,
4265 4266 mlp->mlp_port_upper, mlp->mlp_ipp);
4266 4267 }
4267 4268 }
4268 4269
4269 4270 tsme.tsme_flags = TSOL_MEF_SHARED;
4270 4271 for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) {
4271 4272 tsme.tsme_mlp = *mlp;
4272 4273 if (tnmlp(TNDB_LOAD, &tsme) != 0) {
4273 4274 zerror(zlogp, B_TRUE, "cannot set shared MLP "
4274 4275 "on %d-%d/%d", mlp->mlp_port,
4275 4276 mlp->mlp_port_upper, mlp->mlp_ipp);
4276 4277 }
4277 4278 }
4278 4279 }
4279 4280
4280 4281 static void
4281 4282 remove_mlps(zlog_t *zlogp, zoneid_t zoneid)
4282 4283 {
4283 4284 tsol_mlpent_t tsme;
4284 4285
4285 4286 if (!is_system_labeled())
4286 4287 return;
4287 4288
4288 4289 (void) memset(&tsme, 0, sizeof (tsme));
4289 4290 tsme.tsme_zoneid = zoneid;
4290 4291 if (tnmlp(TNDB_FLUSH, &tsme) != 0)
4291 4292 zerror(zlogp, B_TRUE, "cannot flush MLPs");
4292 4293 }
4293 4294
4294 4295 int
4295 4296 prtmount(const struct mnttab *fs, void *x)
4296 4297 {
4297 4298 zerror((zlog_t *)x, B_FALSE, " %s", fs->mnt_mountp);
4298 4299 return (0);
4299 4300 }
4300 4301
4301 4302 /*
4302 4303 * Look for zones running on the main system that are using this root (or any
4303 4304 * subdirectory of it). Return B_TRUE and print an error if a conflicting zone
4304 4305 * is found or if we can't tell.
4305 4306 */
4306 4307 static boolean_t
4307 4308 duplicate_zone_root(zlog_t *zlogp, const char *rootpath)
4308 4309 {
4309 4310 zoneid_t *zids = NULL;
4310 4311 uint_t nzids = 0;
4311 4312 boolean_t retv;
4312 4313 int rlen, zlen;
4313 4314 char zroot[MAXPATHLEN];
4314 4315 char zonename[ZONENAME_MAX];
4315 4316
4316 4317 for (;;) {
4317 4318 nzids += 10;
4318 4319 zids = malloc(nzids * sizeof (*zids));
4319 4320 if (zids == NULL) {
4320 4321 zerror(zlogp, B_TRUE, "memory allocation failed");
4321 4322 return (B_TRUE);
4322 4323 }
4323 4324 if (zone_list(zids, &nzids) == 0)
4324 4325 break;
4325 4326 free(zids);
4326 4327 }
4327 4328 retv = B_FALSE;
4328 4329 rlen = strlen(rootpath);
4329 4330 while (nzids > 0) {
4330 4331 /*
4331 4332 * Ignore errors; they just mean that the zone has disappeared
4332 4333 * while we were busy.
4333 4334 */
4334 4335 if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot,
4335 4336 sizeof (zroot)) == -1)
4336 4337 continue;
4337 4338 zlen = strlen(zroot);
4338 4339 if (zlen > rlen)
4339 4340 zlen = rlen;
4340 4341 if (strncmp(rootpath, zroot, zlen) == 0 &&
4341 4342 (zroot[zlen] == '\0' || zroot[zlen] == '/') &&
4342 4343 (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) {
4343 4344 if (getzonenamebyid(zids[nzids], zonename,
4344 4345 sizeof (zonename)) == -1)
4345 4346 (void) snprintf(zonename, sizeof (zonename),
4346 4347 "id %d", (int)zids[nzids]);
4347 4348 zerror(zlogp, B_FALSE,
4348 4349 "zone root %s already in use by zone %s",
4349 4350 rootpath, zonename);
4350 4351 retv = B_TRUE;
4351 4352 break;
4352 4353 }
4353 4354 }
4354 4355 free(zids);
4355 4356 return (retv);
4356 4357 }
4357 4358
4358 4359 /*
4359 4360 * Search for loopback mounts that use this same source node (same device and
4360 4361 * inode). Return B_TRUE if there is one or if we can't tell.
4361 4362 */
4362 4363 static boolean_t
4363 4364 duplicate_reachable_path(zlog_t *zlogp, const char *rootpath)
4364 4365 {
4365 4366 struct stat64 rst, zst;
4366 4367 struct mnttab *mnp;
4367 4368
4368 4369 if (stat64(rootpath, &rst) == -1) {
4369 4370 zerror(zlogp, B_TRUE, "can't stat %s", rootpath);
4370 4371 return (B_TRUE);
4371 4372 }
4372 4373 if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
4373 4374 return (B_TRUE);
4374 4375 for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) {
4375 4376 if (mnp->mnt_fstype == NULL ||
4376 4377 strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0)
4377 4378 continue;
4378 4379 /* We're looking at a loopback mount. Stat it. */
4379 4380 if (mnp->mnt_special != NULL &&
4380 4381 stat64(mnp->mnt_special, &zst) != -1 &&
4381 4382 rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) {
4382 4383 zerror(zlogp, B_FALSE,
4383 4384 "zone root %s is reachable through %s",
4384 4385 rootpath, mnp->mnt_mountp);
4385 4386 return (B_TRUE);
4386 4387 }
4387 4388 }
4388 4389 return (B_FALSE);
4389 4390 }
4390 4391
4391 4392 /*
4392 4393 * Set memory cap and pool info for the zone's resource management
4393 4394 * configuration.
4394 4395 */
4395 4396 static int
4396 4397 setup_zone_rm(zlog_t *zlogp, char *zone_name, zoneid_t zoneid)
4397 4398 {
4398 4399 int res;
4399 4400 uint64_t tmp;
4400 4401 struct zone_mcaptab mcap;
4401 4402 char sched[MAXNAMELEN];
4402 4403 zone_dochandle_t handle = NULL;
4403 4404 char pool_err[128];
4404 4405
4405 4406 if ((handle = zonecfg_init_handle()) == NULL) {
4406 4407 zerror(zlogp, B_TRUE, "getting zone configuration handle");
4407 4408 return (Z_BAD_HANDLE);
4408 4409 }
4409 4410
4410 4411 if ((res = zonecfg_get_snapshot_handle(zone_name, handle)) != Z_OK) {
4411 4412 zerror(zlogp, B_FALSE, "invalid configuration");
4412 4413 zonecfg_fini_handle(handle);
4413 4414 return (res);
4414 4415 }
4415 4416
4416 4417 /*
4417 4418 * If a memory cap is configured, set the cap in the kernel using
4418 4419 * zone_setattr() and make sure the rcapd SMF service is enabled.
4419 4420 */
4420 4421 if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
4421 4422 uint64_t num;
4422 4423 char smf_err[128];
4423 4424
4424 4425 num = (uint64_t)strtoull(mcap.zone_physmem_cap, NULL, 10);
4425 4426 if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
4426 4427 zerror(zlogp, B_TRUE, "could not set zone memory cap");
4427 4428 zonecfg_fini_handle(handle);
4428 4429 return (Z_INVAL);
4429 4430 }
4430 4431
4431 4432 if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
4432 4433 zerror(zlogp, B_FALSE, "enabling system/rcap service "
4433 4434 "failed: %s", smf_err);
4434 4435 zonecfg_fini_handle(handle);
4435 4436 return (Z_INVAL);
4436 4437 }
4437 4438 }
4438 4439
4439 4440 /* Get the scheduling class set in the zone configuration. */
4440 4441 if (zonecfg_get_sched_class(handle, sched, sizeof (sched)) == Z_OK &&
4441 4442 strlen(sched) > 0) {
4442 4443 if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, sched,
4443 4444 strlen(sched)) == -1)
4444 4445 zerror(zlogp, B_TRUE, "WARNING: unable to set the "
4445 4446 "default scheduling class");
4446 4447
4447 4448 } else if (zonecfg_get_aliased_rctl(handle, ALIAS_SHARES, &tmp)
4448 4449 == Z_OK) {
4449 4450 /*
4450 4451 * If the zone has the zone.cpu-shares rctl set then we want to
4451 4452 * use the Fair Share Scheduler (FSS) for processes in the
4452 4453 * zone. Check what scheduling class the zone would be running
4453 4454 * in by default so we can print a warning and modify the class
4454 4455 * if we wouldn't be using FSS.
4455 4456 */
4456 4457 char class_name[PC_CLNMSZ];
4457 4458
4458 4459 if (zonecfg_get_dflt_sched_class(handle, class_name,
4459 4460 sizeof (class_name)) != Z_OK) {
4460 4461 zerror(zlogp, B_FALSE, "WARNING: unable to determine "
4461 4462 "the zone's scheduling class");
4462 4463
4463 4464 } else if (strcmp("FSS", class_name) != 0) {
4464 4465 zerror(zlogp, B_FALSE, "WARNING: The zone.cpu-shares "
4465 4466 "rctl is set but\nFSS is not the default "
4466 4467 "scheduling class for\nthis zone. FSS will be "
4467 4468 "used for processes\nin the zone but to get the "
4468 4469 "full benefit of FSS,\nit should be the default "
4469 4470 "scheduling class.\nSee dispadmin(1M) for more "
4470 4471 "details.");
4471 4472
4472 4473 if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, "FSS",
4473 4474 strlen("FSS")) == -1)
4474 4475 zerror(zlogp, B_TRUE, "WARNING: unable to set "
4475 4476 "zone scheduling class to FSS");
4476 4477 }
4477 4478 }
4478 4479
4479 4480 /*
4480 4481 * The next few blocks of code attempt to set up temporary pools as
4481 4482 * well as persistent pools. In all cases we call the functions
4482 4483 * unconditionally. Within each funtion the code will check if the
4483 4484 * zone is actually configured for a temporary pool or persistent pool
4484 4485 * and just return if there is nothing to do.
4485 4486 *
4486 4487 * If we are rebooting we want to attempt to reuse any temporary pool
4487 4488 * that was previously set up. zonecfg_bind_tmp_pool() will do the
4488 4489 * right thing in all cases (reuse or create) based on the current
4489 4490 * zonecfg.
4490 4491 */
4491 4492 if ((res = zonecfg_bind_tmp_pool(handle, zoneid, pool_err,
4492 4493 sizeof (pool_err))) != Z_OK) {
4493 4494 if (res == Z_POOL || res == Z_POOL_CREATE || res == Z_POOL_BIND)
4494 4495 zerror(zlogp, B_FALSE, "%s: %s\ndedicated-cpu setting "
4495 4496 "cannot be instantiated", zonecfg_strerror(res),
4496 4497 pool_err);
4497 4498 else
4498 4499 zerror(zlogp, B_FALSE, "could not bind zone to "
4499 4500 "temporary pool: %s", zonecfg_strerror(res));
4500 4501 zonecfg_fini_handle(handle);
4501 4502 return (Z_POOL_BIND);
4502 4503 }
4503 4504
4504 4505 /*
4505 4506 * Check if we need to warn about poold not being enabled.
4506 4507 */
4507 4508 if (zonecfg_warn_poold(handle)) {
4508 4509 zerror(zlogp, B_FALSE, "WARNING: A range of dedicated-cpus has "
4509 4510 "been specified\nbut the dynamic pool service is not "
4510 4511 "enabled.\nThe system will not dynamically adjust the\n"
4511 4512 "processor allocation within the specified range\n"
4512 4513 "until svc:/system/pools/dynamic is enabled.\n"
4513 4514 "See poold(1M).");
4514 4515 }
4515 4516
4516 4517 /* The following is a warning, not an error. */
4517 4518 if ((res = zonecfg_bind_pool(handle, zoneid, pool_err,
4518 4519 sizeof (pool_err))) != Z_OK) {
4519 4520 if (res == Z_POOL_BIND)
4520 4521 zerror(zlogp, B_FALSE, "WARNING: unable to bind to "
4521 4522 "pool '%s'; using default pool.", pool_err);
4522 4523 else if (res == Z_POOL)
4523 4524 zerror(zlogp, B_FALSE, "WARNING: %s: %s",
4524 4525 zonecfg_strerror(res), pool_err);
4525 4526 else
4526 4527 zerror(zlogp, B_FALSE, "WARNING: %s",
4527 4528 zonecfg_strerror(res));
4528 4529 }
4529 4530
4530 4531 /* Update saved pool name in case it has changed */
4531 4532 (void) zonecfg_get_poolname(handle, zone_name, pool_name,
4532 4533 sizeof (pool_name));
4533 4534
4534 4535 zonecfg_fini_handle(handle);
4535 4536 return (Z_OK);
4536 4537 }
4537 4538
4538 4539 static void
4539 4540 report_prop_err(zlog_t *zlogp, const char *name, const char *value, int res)
4540 4541 {
4541 4542 switch (res) {
4542 4543 case Z_TOO_BIG:
4543 4544 zerror(zlogp, B_FALSE, "%s property value is too large.", name);
4544 4545 break;
4545 4546
4546 4547 case Z_INVALID_PROPERTY:
4547 4548 zerror(zlogp, B_FALSE, "%s property value \"%s\" is not valid",
4548 4549 name, value);
4549 4550 break;
4550 4551
4551 4552 default:
4552 4553 zerror(zlogp, B_TRUE, "fetching property %s: %d", name, res);
4553 4554 break;
4554 4555 }
4555 4556 }
4556 4557
4557 4558 /*
4558 4559 * Sets the hostid of the new zone based on its configured value. The zone's
4559 4560 * zone_t structure must already exist in kernel memory. 'zlogp' refers to the
4560 4561 * log used to report errors and warnings and must be non-NULL. 'zone_namep'
4561 4562 * is the name of the new zone and must be non-NULL. 'zoneid' is the numeric
4562 4563 * ID of the new zone.
4563 4564 *
4564 4565 * This function returns zero on success and a nonzero error code on failure.
4565 4566 */
4566 4567 static int
4567 4568 setup_zone_hostid(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid)
4568 4569 {
4569 4570 int res;
4570 4571 char hostidp[HW_HOSTID_LEN];
4571 4572 unsigned int hostid;
4572 4573
4573 4574 res = zonecfg_get_hostid(handle, hostidp, sizeof (hostidp));
4574 4575
4575 4576 if (res == Z_BAD_PROPERTY) {
4576 4577 return (Z_OK);
4577 4578 } else if (res != Z_OK) {
4578 4579 report_prop_err(zlogp, "hostid", hostidp, res);
4579 4580 return (res);
4580 4581 }
4581 4582
4582 4583 hostid = (unsigned int)strtoul(hostidp, NULL, 16);
4583 4584 if ((res = zone_setattr(zoneid, ZONE_ATTR_HOSTID, &hostid,
↓ open down ↓ |
4494 lines elided |
↑ open up ↑ |
4584 4585 sizeof (hostid))) != 0) {
4585 4586 zerror(zlogp, B_TRUE,
4586 4587 "zone hostid is not valid: %s: %d", hostidp, res);
4587 4588 return (Z_SYSTEM);
4588 4589 }
4589 4590
4590 4591 return (res);
4591 4592 }
4592 4593
4593 4594 static int
4595 +setup_zone_secflags(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid)
4596 +{
4597 + psecflags_t secflags;
4598 + struct zone_secflagstab tab = {0};
4599 + secflagdelta_t delt;
4600 + int res;
4601 +
4602 + res = zonecfg_lookup_secflags(handle, &tab);
4603 +
4604 + if ((res != Z_OK) &&
4605 + /* The general defaulting code will handle this */
4606 + (res != Z_NO_ENTRY) && (res != Z_BAD_PROPERTY)) {
4607 + zerror(zlogp, B_FALSE, "security-flags property is "
4608 + "invalid: %d", res);
4609 + return (res);
4610 + }
4611 +
4612 + if (strlen(tab.zone_secflags_lower) == 0)
4613 + (void) strlcpy(tab.zone_secflags_lower, "none",
4614 + sizeof (tab.zone_secflags_lower));
4615 + if (strlen(tab.zone_secflags_default) == 0)
4616 + (void) strlcpy(tab.zone_secflags_default,
4617 + tab.zone_secflags_lower,
4618 + sizeof (tab.zone_secflags_default));
4619 + if (strlen(tab.zone_secflags_upper) == 0)
4620 + (void) strlcpy(tab.zone_secflags_upper, "all",
4621 + sizeof (tab.zone_secflags_upper));
4622 +
4623 + if (secflags_parse(NULL, tab.zone_secflags_default,
4624 + &delt) == -1) {
4625 + zerror(zlogp, B_FALSE, "default security-flags: '%s'"
4626 + "are invalid", tab.zone_secflags_default);
4627 + return (Z_BAD_PROPERTY);
4628 + } else if (delt.psd_ass_active != B_TRUE) {
4629 + zerror(zlogp, B_FALSE, "relative security-flags are not "
4630 + "allowed in zone configuration (default "
4631 + "security-flags: '%s')",
4632 + tab.zone_secflags_default);
4633 + return (Z_BAD_PROPERTY);
4634 + } else {
4635 + secflags_copy(&secflags.psf_inherit, &delt.psd_assign);
4636 + secflags_copy(&secflags.psf_effective, &delt.psd_assign);
4637 + }
4638 +
4639 + if (secflags_parse(NULL, tab.zone_secflags_lower,
4640 + &delt) == -1) {
4641 + zerror(zlogp, B_FALSE, "lower security-flags: '%s'"
4642 + "are invalid", tab.zone_secflags_lower);
4643 + return (Z_BAD_PROPERTY);
4644 + } else if (delt.psd_ass_active != B_TRUE) {
4645 + zerror(zlogp, B_FALSE, "relative security-flags are not "
4646 + "allowed in zone configuration (lower "
4647 + "security-flags: '%s')",
4648 + tab.zone_secflags_lower);
4649 + return (Z_BAD_PROPERTY);
4650 + } else {
4651 + secflags_copy(&secflags.psf_lower, &delt.psd_assign);
4652 + }
4653 +
4654 + if (secflags_parse(NULL, tab.zone_secflags_upper,
4655 + &delt) == -1) {
4656 + zerror(zlogp, B_FALSE, "upper security-flags: '%s'"
4657 + "are invalid", tab.zone_secflags_upper);
4658 + return (Z_BAD_PROPERTY);
4659 + } else if (delt.psd_ass_active != B_TRUE) {
4660 + zerror(zlogp, B_FALSE, "relative security-flags are not "
4661 + "allowed in zone configuration (upper "
4662 + "security-flags: '%s')",
4663 + tab.zone_secflags_upper);
4664 + return (Z_BAD_PROPERTY);
4665 + } else {
4666 + secflags_copy(&secflags.psf_upper, &delt.psd_assign);
4667 + }
4668 +
4669 + if (!psecflags_validate(&secflags)) {
4670 + zerror(zlogp, B_TRUE, "security-flags violate invariants");
4671 + return (Z_BAD_PROPERTY);
4672 + }
4673 +
4674 + if ((res = zone_setattr(zoneid, ZONE_ATTR_SECFLAGS, &secflags,
4675 + sizeof (secflags))) != 0) {
4676 + zerror(zlogp, B_TRUE,
4677 + "security-flags couldn't be set: %d", res);
4678 + return (Z_SYSTEM);
4679 + }
4680 +
4681 + return (Z_OK);
4682 +}
4683 +
4684 +static int
4594 4685 setup_zone_fs_allowed(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid)
4595 4686 {
4596 4687 char fsallowed[ZONE_FS_ALLOWED_MAX];
4597 4688 char *fsallowedp = fsallowed;
4598 4689 int len = sizeof (fsallowed);
4599 4690 int res;
4600 4691
4601 4692 res = zonecfg_get_fs_allowed(handle, fsallowed, len);
4602 4693
4603 4694 if (res == Z_BAD_PROPERTY) {
4604 4695 /* No value, set the defaults */
4605 4696 (void) strlcpy(fsallowed, DFLT_FS_ALLOWED, len);
4606 4697 } else if (res != Z_OK) {
4607 4698 report_prop_err(zlogp, "fs-allowed", fsallowed, res);
4608 4699 return (res);
4609 4700 } else if (fsallowed[0] == '-') {
4610 - /* dropping default privs - use remaining list */
4701 + /* dropping default filesystems - use remaining list */
4611 4702 if (fsallowed[1] != ',')
4612 4703 return (Z_OK);
4613 4704 fsallowedp += 2;
4614 4705 len -= 2;
4615 4706 } else {
4616 4707 /* Has a value, append the defaults */
4617 4708 if (strlcat(fsallowed, ",", len) >= len ||
4618 4709 strlcat(fsallowed, DFLT_FS_ALLOWED, len) >= len) {
4619 4710 report_prop_err(zlogp, "fs-allowed", fsallowed,
4620 4711 Z_TOO_BIG);
4621 4712 return (Z_TOO_BIG);
4622 4713 }
4623 4714 }
4624 4715
4625 4716 if (zone_setattr(zoneid, ZONE_ATTR_FS_ALLOWED, fsallowedp, len) != 0) {
4626 4717 zerror(zlogp, B_TRUE,
4627 4718 "fs-allowed couldn't be set: %s: %d", fsallowedp, res);
4628 4719 return (Z_SYSTEM);
4629 4720 }
4630 4721
4631 4722 return (Z_OK);
4632 4723 }
4633 4724
4634 4725 static int
4635 4726 setup_zone_attrs(zlog_t *zlogp, char *zone_namep, zoneid_t zoneid)
4636 4727 {
4637 4728 zone_dochandle_t handle;
4638 4729 int res = Z_OK;
4639 4730
4640 4731 if ((handle = zonecfg_init_handle()) == NULL) {
4641 4732 zerror(zlogp, B_TRUE, "getting zone configuration handle");
4642 4733 return (Z_BAD_HANDLE);
4643 4734 }
4644 4735 if ((res = zonecfg_get_snapshot_handle(zone_namep, handle)) != Z_OK) {
↓ open down ↓ |
24 lines elided |
↑ open up ↑ |
4645 4736 zerror(zlogp, B_FALSE, "invalid configuration");
4646 4737 goto out;
4647 4738 }
4648 4739
4649 4740 if ((res = setup_zone_hostid(handle, zlogp, zoneid)) != Z_OK)
4650 4741 goto out;
4651 4742
4652 4743 if ((res = setup_zone_fs_allowed(handle, zlogp, zoneid)) != Z_OK)
4653 4744 goto out;
4654 4745
4746 + if ((res = setup_zone_secflags(handle, zlogp, zoneid)) != Z_OK)
4747 + goto out;
4748 +
4655 4749 out:
4656 4750 zonecfg_fini_handle(handle);
4657 4751 return (res);
4658 4752 }
4659 4753
4660 4754 zoneid_t
4661 4755 vplat_create(zlog_t *zlogp, zone_mnt_t mount_cmd)
4662 4756 {
4663 4757 zoneid_t rval = -1;
4664 4758 priv_set_t *privs;
4665 4759 char rootpath[MAXPATHLEN];
4666 4760 char *rctlbuf = NULL;
4667 4761 size_t rctlbufsz = 0;
4668 4762 char *zfsbuf = NULL;
4669 4763 size_t zfsbufsz = 0;
4670 4764 zoneid_t zoneid = -1;
4671 4765 int xerr;
4672 4766 char *kzone;
4673 4767 FILE *fp = NULL;
4674 4768 tsol_zcent_t *zcent = NULL;
4675 4769 int match = 0;
4676 4770 int doi = 0;
4677 4771 int flags;
4678 4772 zone_iptype_t iptype;
4679 4773
4680 4774 if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
4681 4775 zerror(zlogp, B_TRUE, "unable to determine zone root");
4682 4776 return (-1);
4683 4777 }
4684 4778 if (zonecfg_in_alt_root())
4685 4779 resolve_lofs(zlogp, rootpath, sizeof (rootpath));
4686 4780
4687 4781 if (vplat_get_iptype(zlogp, &iptype) < 0) {
4688 4782 zerror(zlogp, B_TRUE, "unable to determine ip-type");
4689 4783 return (-1);
4690 4784 }
4691 4785 switch (iptype) {
4692 4786 case ZS_SHARED:
4693 4787 flags = 0;
4694 4788 break;
4695 4789 case ZS_EXCLUSIVE:
4696 4790 flags = ZCF_NET_EXCL;
4697 4791 break;
4698 4792 }
4699 4793
4700 4794 if ((privs = priv_allocset()) == NULL) {
4701 4795 zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
4702 4796 return (-1);
4703 4797 }
4704 4798 priv_emptyset(privs);
4705 4799 if (get_privset(zlogp, privs, mount_cmd) != 0)
4706 4800 goto error;
4707 4801
4708 4802 if (mount_cmd == Z_MNT_BOOT &&
4709 4803 get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) {
4710 4804 zerror(zlogp, B_FALSE, "Unable to get list of rctls");
4711 4805 goto error;
4712 4806 }
4713 4807
4714 4808 if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) {
4715 4809 zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets");
4716 4810 goto error;
4717 4811 }
4718 4812
4719 4813 if (mount_cmd == Z_MNT_BOOT && is_system_labeled()) {
4720 4814 zcent = get_zone_label(zlogp, privs);
4721 4815 if (zcent != NULL) {
4722 4816 match = zcent->zc_match;
4723 4817 doi = zcent->zc_doi;
4724 4818 *zlabel = zcent->zc_label;
4725 4819 } else {
4726 4820 goto error;
4727 4821 }
4728 4822 if (validate_rootds_label(zlogp, rootpath, zlabel) != 0)
4729 4823 goto error;
4730 4824 }
4731 4825
4732 4826 kzone = zone_name;
4733 4827
4734 4828 /*
4735 4829 * We must do this scan twice. First, we look for zones running on the
4736 4830 * main system that are using this root (or any subdirectory of it).
4737 4831 * Next, we reduce to the shortest path and search for loopback mounts
4738 4832 * that use this same source node (same device and inode).
4739 4833 */
4740 4834 if (duplicate_zone_root(zlogp, rootpath))
4741 4835 goto error;
4742 4836 if (duplicate_reachable_path(zlogp, rootpath))
4743 4837 goto error;
4744 4838
4745 4839 if (ALT_MOUNT(mount_cmd)) {
4746 4840 root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE);
4747 4841
4748 4842 /*
4749 4843 * Forge up a special root for this zone. When a zone is
4750 4844 * mounted, we can't let the zone have its own root because the
4751 4845 * tools that will be used in this "scratch zone" need access
4752 4846 * to both the zone's resources and the running machine's
4753 4847 * executables.
4754 4848 *
4755 4849 * Note that the mkdir here also catches read-only filesystems.
4756 4850 */
4757 4851 if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) {
4758 4852 zerror(zlogp, B_TRUE, "cannot create %s", rootpath);
4759 4853 goto error;
4760 4854 }
4761 4855 if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0)
4762 4856 goto error;
4763 4857 }
4764 4858
4765 4859 if (zonecfg_in_alt_root()) {
4766 4860 /*
4767 4861 * If we are mounting up a zone in an alternate root partition,
4768 4862 * then we have some additional work to do before starting the
4769 4863 * zone. First, resolve the root path down so that we're not
4770 4864 * fooled by duplicates. Then forge up an internal name for
4771 4865 * the zone.
4772 4866 */
4773 4867 if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) {
4774 4868 zerror(zlogp, B_TRUE, "cannot open mapfile");
4775 4869 goto error;
4776 4870 }
4777 4871 if (zonecfg_lock_scratch(fp) != 0) {
4778 4872 zerror(zlogp, B_TRUE, "cannot lock mapfile");
4779 4873 goto error;
4780 4874 }
4781 4875 if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4782 4876 NULL, 0) == 0) {
4783 4877 zerror(zlogp, B_FALSE, "scratch zone already running");
4784 4878 goto error;
4785 4879 }
4786 4880 /* This is the preferred name */
4787 4881 (void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s",
4788 4882 zone_name);
4789 4883 srandom(getpid());
4790 4884 while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL,
4791 4885 0) == 0) {
4792 4886 /* This is just an arbitrary name; note "." usage */
4793 4887 (void) snprintf(kernzone, sizeof (kernzone),
4794 4888 "SUNWlu.%08lX%08lX", random(), random());
4795 4889 }
4796 4890 kzone = kernzone;
4797 4891 }
4798 4892
4799 4893 xerr = 0;
4800 4894 if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf,
4801 4895 rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel,
4802 4896 flags)) == -1) {
4803 4897 if (xerr == ZE_AREMOUNTS) {
4804 4898 if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) {
4805 4899 zerror(zlogp, B_FALSE,
4806 4900 "An unknown file-system is mounted on "
4807 4901 "a subdirectory of %s", rootpath);
4808 4902 } else {
4809 4903
4810 4904 zerror(zlogp, B_FALSE,
4811 4905 "These file-systems are mounted on "
4812 4906 "subdirectories of %s:", rootpath);
4813 4907 (void) zonecfg_find_mounts(rootpath,
4814 4908 prtmount, zlogp);
4815 4909 }
4816 4910 } else if (xerr == ZE_CHROOTED) {
4817 4911 zerror(zlogp, B_FALSE, "%s: "
4818 4912 "cannot create a zone from a chrooted "
4819 4913 "environment", "zone_create");
4820 4914 } else if (xerr == ZE_LABELINUSE) {
4821 4915 char zonename[ZONENAME_MAX];
4822 4916 (void) getzonenamebyid(getzoneidbylabel(zlabel),
4823 4917 zonename, ZONENAME_MAX);
4824 4918 zerror(zlogp, B_FALSE, "The zone label is already "
4825 4919 "used by the zone '%s'.", zonename);
4826 4920 } else {
4827 4921 zerror(zlogp, B_TRUE, "%s failed", "zone_create");
4828 4922 }
4829 4923 goto error;
4830 4924 }
4831 4925
4832 4926 if (zonecfg_in_alt_root() &&
4833 4927 zonecfg_add_scratch(fp, zone_name, kernzone,
4834 4928 zonecfg_get_root()) == -1) {
4835 4929 zerror(zlogp, B_TRUE, "cannot add mapfile entry");
4836 4930 goto error;
4837 4931 }
4838 4932
4839 4933 /*
4840 4934 * The following actions are not performed when merely mounting a zone
4841 4935 * for administrative use.
4842 4936 */
4843 4937 if (mount_cmd == Z_MNT_BOOT) {
4844 4938 brand_handle_t bh;
4845 4939 struct brand_attr attr;
4846 4940 char modname[MAXPATHLEN];
4847 4941
4848 4942 if (setup_zone_attrs(zlogp, zone_name, zoneid) != Z_OK)
4849 4943 goto error;
4850 4944
4851 4945 if ((bh = brand_open(brand_name)) == NULL) {
4852 4946 zerror(zlogp, B_FALSE,
4853 4947 "unable to determine brand name");
4854 4948 goto error;
4855 4949 }
4856 4950
4857 4951 if (!is_system_labeled() &&
4858 4952 (strcmp(brand_name, LABELED_BRAND_NAME) == 0)) {
4859 4953 brand_close(bh);
4860 4954 zerror(zlogp, B_FALSE,
4861 4955 "cannot boot labeled zone on unlabeled system");
4862 4956 goto error;
4863 4957 }
4864 4958
4865 4959 /*
4866 4960 * If this brand requires any kernel support, now is the time to
4867 4961 * get it loaded and initialized.
4868 4962 */
4869 4963 if (brand_get_modname(bh, modname, MAXPATHLEN) < 0) {
4870 4964 brand_close(bh);
4871 4965 zerror(zlogp, B_FALSE,
4872 4966 "unable to determine brand kernel module");
4873 4967 goto error;
4874 4968 }
4875 4969 brand_close(bh);
4876 4970
4877 4971 if (strlen(modname) > 0) {
4878 4972 (void) strlcpy(attr.ba_brandname, brand_name,
4879 4973 sizeof (attr.ba_brandname));
4880 4974 (void) strlcpy(attr.ba_modname, modname,
4881 4975 sizeof (attr.ba_modname));
4882 4976 if (zone_setattr(zoneid, ZONE_ATTR_BRAND, &attr,
4883 4977 sizeof (attr) != 0)) {
4884 4978 zerror(zlogp, B_TRUE,
4885 4979 "could not set zone brand attribute.");
4886 4980 goto error;
4887 4981 }
4888 4982 }
4889 4983
4890 4984 if (setup_zone_rm(zlogp, zone_name, zoneid) != Z_OK)
4891 4985 goto error;
4892 4986
4893 4987 set_mlps(zlogp, zoneid, zcent);
4894 4988 }
4895 4989
4896 4990 rval = zoneid;
4897 4991 zoneid = -1;
4898 4992
4899 4993 error:
4900 4994 if (zoneid != -1) {
4901 4995 (void) zone_shutdown(zoneid);
4902 4996 (void) zone_destroy(zoneid);
4903 4997 }
4904 4998 if (rctlbuf != NULL)
4905 4999 free(rctlbuf);
4906 5000 priv_freeset(privs);
4907 5001 if (fp != NULL)
4908 5002 zonecfg_close_scratch(fp);
4909 5003 lofs_discard_mnttab();
4910 5004 if (zcent != NULL)
4911 5005 tsol_freezcent(zcent);
4912 5006 return (rval);
4913 5007 }
4914 5008
4915 5009 /*
4916 5010 * Enter the zone and write a /etc/zones/index file there. This allows
4917 5011 * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone
4918 5012 * details from inside the zone.
4919 5013 */
4920 5014 static void
4921 5015 write_index_file(zoneid_t zoneid)
4922 5016 {
4923 5017 FILE *zef;
4924 5018 FILE *zet;
4925 5019 struct zoneent *zep;
4926 5020 pid_t child;
4927 5021 int tmpl_fd;
4928 5022 ctid_t ct;
4929 5023 int fd;
4930 5024 char uuidstr[UUID_PRINTABLE_STRING_LENGTH];
4931 5025
4932 5026 /* Locate the zone entry in the global zone's index file */
4933 5027 if ((zef = setzoneent()) == NULL)
4934 5028 return;
4935 5029 while ((zep = getzoneent_private(zef)) != NULL) {
4936 5030 if (strcmp(zep->zone_name, zone_name) == 0)
4937 5031 break;
4938 5032 free(zep);
4939 5033 }
4940 5034 endzoneent(zef);
4941 5035 if (zep == NULL)
4942 5036 return;
4943 5037
4944 5038 if ((tmpl_fd = init_template()) == -1) {
4945 5039 free(zep);
4946 5040 return;
4947 5041 }
4948 5042
4949 5043 if ((child = fork()) == -1) {
4950 5044 (void) ct_tmpl_clear(tmpl_fd);
4951 5045 (void) close(tmpl_fd);
4952 5046 free(zep);
4953 5047 return;
4954 5048 }
4955 5049
4956 5050 /* parent waits for child to finish */
4957 5051 if (child != 0) {
4958 5052 free(zep);
4959 5053 if (contract_latest(&ct) == -1)
4960 5054 ct = -1;
4961 5055 (void) ct_tmpl_clear(tmpl_fd);
4962 5056 (void) close(tmpl_fd);
4963 5057 (void) waitpid(child, NULL, 0);
4964 5058 (void) contract_abandon_id(ct);
4965 5059 return;
4966 5060 }
4967 5061
4968 5062 /* child enters zone and sets up index file */
4969 5063 (void) ct_tmpl_clear(tmpl_fd);
4970 5064 if (zone_enter(zoneid) != -1) {
4971 5065 (void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE);
4972 5066 (void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID,
4973 5067 ZONE_CONFIG_GID);
4974 5068 fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC,
4975 5069 ZONE_INDEX_MODE);
4976 5070 if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) {
4977 5071 (void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID);
4978 5072 if (uuid_is_null(zep->zone_uuid))
4979 5073 uuidstr[0] = '\0';
4980 5074 else
4981 5075 uuid_unparse(zep->zone_uuid, uuidstr);
4982 5076 (void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name,
4983 5077 zone_state_str(zep->zone_state),
4984 5078 uuidstr);
4985 5079 (void) fclose(zet);
4986 5080 }
4987 5081 }
4988 5082 _exit(0);
4989 5083 }
4990 5084
4991 5085 int
4992 5086 vplat_bringup(zlog_t *zlogp, zone_mnt_t mount_cmd, zoneid_t zoneid)
4993 5087 {
4994 5088 char zonepath[MAXPATHLEN];
4995 5089
4996 5090 if (mount_cmd == Z_MNT_BOOT && validate_datasets(zlogp) != 0) {
4997 5091 lofs_discard_mnttab();
4998 5092 return (-1);
4999 5093 }
5000 5094
5001 5095 /*
5002 5096 * Before we try to mount filesystems we need to create the
5003 5097 * attribute backing store for /dev
5004 5098 */
5005 5099 if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
5006 5100 lofs_discard_mnttab();
5007 5101 return (-1);
5008 5102 }
5009 5103 resolve_lofs(zlogp, zonepath, sizeof (zonepath));
5010 5104
5011 5105 /* Make /dev directory owned by root, grouped sys */
5012 5106 if (make_one_dir(zlogp, zonepath, "/dev", DEFAULT_DIR_MODE,
5013 5107 0, 3) != 0) {
5014 5108 lofs_discard_mnttab();
5015 5109 return (-1);
5016 5110 }
5017 5111
5018 5112 if (mount_filesystems(zlogp, mount_cmd) != 0) {
5019 5113 lofs_discard_mnttab();
5020 5114 return (-1);
5021 5115 }
5022 5116
5023 5117 if (mount_cmd == Z_MNT_BOOT) {
5024 5118 zone_iptype_t iptype;
5025 5119
5026 5120 if (vplat_get_iptype(zlogp, &iptype) < 0) {
5027 5121 zerror(zlogp, B_TRUE, "unable to determine ip-type");
5028 5122 lofs_discard_mnttab();
5029 5123 return (-1);
5030 5124 }
5031 5125
5032 5126 switch (iptype) {
5033 5127 case ZS_SHARED:
5034 5128 /* Always do this to make lo0 get configured */
5035 5129 if (configure_shared_network_interfaces(zlogp) != 0) {
5036 5130 lofs_discard_mnttab();
5037 5131 return (-1);
5038 5132 }
5039 5133 break;
5040 5134 case ZS_EXCLUSIVE:
5041 5135 if (configure_exclusive_network_interfaces(zlogp,
5042 5136 zoneid) !=
5043 5137 0) {
5044 5138 lofs_discard_mnttab();
5045 5139 return (-1);
5046 5140 }
5047 5141 break;
5048 5142 }
5049 5143 }
5050 5144
5051 5145 write_index_file(zoneid);
5052 5146
5053 5147 lofs_discard_mnttab();
5054 5148 return (0);
5055 5149 }
5056 5150
5057 5151 static int
5058 5152 lu_root_teardown(zlog_t *zlogp)
5059 5153 {
5060 5154 char zroot[MAXPATHLEN];
5061 5155
5062 5156 if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
5063 5157 zerror(zlogp, B_FALSE, "unable to determine zone root");
5064 5158 return (-1);
5065 5159 }
5066 5160 root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
5067 5161
5068 5162 /*
5069 5163 * At this point, the processes are gone, the filesystems (save the
5070 5164 * root) are unmounted, and the zone is on death row. But there may
5071 5165 * still be creds floating about in the system that reference the
5072 5166 * zone_t, and which pin down zone_rootvp causing this call to fail
5073 5167 * with EBUSY. Thus, we try for a little while before just giving up.
5074 5168 * (How I wish this were not true, and umount2 just did the right
5075 5169 * thing, or tmpfs supported MS_FORCE This is a gross hack.)
5076 5170 */
5077 5171 if (umount2(zroot, MS_FORCE) != 0) {
5078 5172 if (errno == ENOTSUP && umount2(zroot, 0) == 0)
5079 5173 goto unmounted;
5080 5174 if (errno == EBUSY) {
5081 5175 int tries = 10;
5082 5176
5083 5177 while (--tries >= 0) {
5084 5178 (void) sleep(1);
5085 5179 if (umount2(zroot, 0) == 0)
5086 5180 goto unmounted;
5087 5181 if (errno != EBUSY)
5088 5182 break;
5089 5183 }
5090 5184 }
5091 5185 zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot);
5092 5186 return (-1);
5093 5187 }
5094 5188 unmounted:
5095 5189
5096 5190 /*
5097 5191 * Only zones in an alternate root environment have scratch zone
5098 5192 * entries.
5099 5193 */
5100 5194 if (zonecfg_in_alt_root()) {
5101 5195 FILE *fp;
5102 5196 int retv;
5103 5197
5104 5198 if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
5105 5199 zerror(zlogp, B_TRUE, "cannot open mapfile");
5106 5200 return (-1);
5107 5201 }
5108 5202 retv = -1;
5109 5203 if (zonecfg_lock_scratch(fp) != 0)
5110 5204 zerror(zlogp, B_TRUE, "cannot lock mapfile");
5111 5205 else if (zonecfg_delete_scratch(fp, kernzone) != 0)
5112 5206 zerror(zlogp, B_TRUE, "cannot delete map entry");
5113 5207 else
5114 5208 retv = 0;
5115 5209 zonecfg_close_scratch(fp);
5116 5210 return (retv);
5117 5211 } else {
5118 5212 return (0);
5119 5213 }
5120 5214 }
5121 5215
5122 5216 int
5123 5217 vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting)
5124 5218 {
5125 5219 char *kzone;
5126 5220 zoneid_t zoneid;
5127 5221 int res;
5128 5222 char pool_err[128];
5129 5223 char zpath[MAXPATHLEN];
5130 5224 char cmdbuf[MAXPATHLEN];
5131 5225 brand_handle_t bh = NULL;
5132 5226 dladm_status_t status;
5133 5227 char errmsg[DLADM_STRSIZE];
5134 5228 ushort_t flags;
5135 5229
5136 5230 kzone = zone_name;
5137 5231 if (zonecfg_in_alt_root()) {
5138 5232 FILE *fp;
5139 5233
5140 5234 if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
5141 5235 zerror(zlogp, B_TRUE, "unable to open map file");
5142 5236 goto error;
5143 5237 }
5144 5238 if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
5145 5239 kernzone, sizeof (kernzone)) != 0) {
5146 5240 zerror(zlogp, B_FALSE, "unable to find scratch zone");
5147 5241 zonecfg_close_scratch(fp);
5148 5242 goto error;
5149 5243 }
5150 5244 zonecfg_close_scratch(fp);
5151 5245 kzone = kernzone;
5152 5246 }
5153 5247
5154 5248 if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) {
5155 5249 if (!bringup_failure_recovery)
5156 5250 zerror(zlogp, B_TRUE, "unable to get zoneid");
5157 5251 if (unmount_cmd)
5158 5252 (void) lu_root_teardown(zlogp);
5159 5253 goto error;
5160 5254 }
5161 5255
5162 5256 if (remove_datalink_pool(zlogp, zoneid) != 0) {
5163 5257 zerror(zlogp, B_FALSE, "unable clear datalink pool property");
5164 5258 goto error;
5165 5259 }
5166 5260
5167 5261 if (remove_datalink_protect(zlogp, zoneid) != 0) {
5168 5262 zerror(zlogp, B_FALSE,
5169 5263 "unable clear datalink protect property");
5170 5264 goto error;
5171 5265 }
5172 5266
5173 5267 /*
5174 5268 * The datalinks assigned to the zone will be removed from the NGZ as
5175 5269 * part of zone_shutdown() so that we need to remove protect/pool etc.
5176 5270 * before zone_shutdown(). Even if the shutdown itself fails, the zone
5177 5271 * will not be able to violate any constraints applied because the
5178 5272 * datalinks are no longer available to the zone.
5179 5273 */
5180 5274 if (zone_shutdown(zoneid) != 0) {
5181 5275 zerror(zlogp, B_TRUE, "unable to shutdown zone");
5182 5276 goto error;
5183 5277 }
5184 5278
5185 5279 /* Get the zonepath of this zone */
5186 5280 if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
5187 5281 zerror(zlogp, B_FALSE, "unable to determine zone path");
5188 5282 goto error;
5189 5283 }
5190 5284
5191 5285 /* Get a handle to the brand info for this zone */
5192 5286 if ((bh = brand_open(brand_name)) == NULL) {
5193 5287 zerror(zlogp, B_FALSE, "unable to determine zone brand");
5194 5288 return (-1);
5195 5289 }
5196 5290 /*
5197 5291 * If there is a brand 'halt' callback, execute it now to give the
5198 5292 * brand a chance to cleanup any custom configuration.
5199 5293 */
5200 5294 (void) strcpy(cmdbuf, EXEC_PREFIX);
5201 5295 if (brand_get_halt(bh, zone_name, zpath, cmdbuf + EXEC_LEN,
5202 5296 sizeof (cmdbuf) - EXEC_LEN) < 0) {
5203 5297 brand_close(bh);
5204 5298 zerror(zlogp, B_FALSE, "unable to determine branded zone's "
5205 5299 "halt callback.");
5206 5300 goto error;
5207 5301 }
5208 5302 brand_close(bh);
5209 5303
5210 5304 if ((strlen(cmdbuf) > EXEC_LEN) &&
5211 5305 (do_subproc(zlogp, cmdbuf, NULL) != Z_OK)) {
5212 5306 zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
5213 5307 goto error;
5214 5308 }
5215 5309
5216 5310 if (!unmount_cmd) {
5217 5311 zone_iptype_t iptype;
5218 5312
5219 5313 if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
5220 5314 sizeof (flags)) < 0) {
5221 5315 if (vplat_get_iptype(zlogp, &iptype) < 0) {
5222 5316 zerror(zlogp, B_TRUE, "unable to determine "
5223 5317 "ip-type");
5224 5318 goto error;
5225 5319 }
5226 5320 } else {
5227 5321 if (flags & ZF_NET_EXCL)
5228 5322 iptype = ZS_EXCLUSIVE;
5229 5323 else
5230 5324 iptype = ZS_SHARED;
5231 5325 }
5232 5326
5233 5327 switch (iptype) {
5234 5328 case ZS_SHARED:
5235 5329 if (unconfigure_shared_network_interfaces(zlogp,
5236 5330 zoneid) != 0) {
5237 5331 zerror(zlogp, B_FALSE, "unable to unconfigure "
5238 5332 "network interfaces in zone");
5239 5333 goto error;
5240 5334 }
5241 5335 break;
5242 5336 case ZS_EXCLUSIVE:
5243 5337 if (unconfigure_exclusive_network_interfaces(zlogp,
5244 5338 zoneid) != 0) {
5245 5339 zerror(zlogp, B_FALSE, "unable to unconfigure "
5246 5340 "network interfaces in zone");
5247 5341 goto error;
5248 5342 }
5249 5343 status = dladm_zone_halt(dld_handle, zoneid);
5250 5344 if (status != DLADM_STATUS_OK) {
5251 5345 zerror(zlogp, B_FALSE, "unable to notify "
5252 5346 "dlmgmtd of zone halt: %s",
5253 5347 dladm_status2str(status, errmsg));
5254 5348 }
5255 5349 break;
5256 5350 }
5257 5351 }
5258 5352
5259 5353 if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) {
5260 5354 zerror(zlogp, B_TRUE, "unable to abort TCP connections");
5261 5355 goto error;
5262 5356 }
5263 5357
5264 5358 if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) {
5265 5359 zerror(zlogp, B_FALSE,
5266 5360 "unable to unmount file systems in zone");
5267 5361 goto error;
5268 5362 }
5269 5363
5270 5364 /*
5271 5365 * If we are rebooting then we normally don't want to destroy an
5272 5366 * existing temporary pool at this point so that we can just reuse it
5273 5367 * when the zone boots back up. However, it is also possible we were
5274 5368 * running with a temporary pool and the zone configuration has been
5275 5369 * modified to no longer use a temporary pool. In that case we need
5276 5370 * to destroy the temporary pool now. This case looks like the case
5277 5371 * where we never had a temporary pool configured but
5278 5372 * zonecfg_destroy_tmp_pool will do the right thing either way.
5279 5373 */
5280 5374 if (!unmount_cmd) {
5281 5375 boolean_t destroy_tmp_pool = B_TRUE;
5282 5376
5283 5377 if (rebooting) {
5284 5378 struct zone_psettab pset_tab;
5285 5379 zone_dochandle_t handle;
5286 5380
5287 5381 if ((handle = zonecfg_init_handle()) != NULL &&
5288 5382 zonecfg_get_handle(zone_name, handle) == Z_OK &&
5289 5383 zonecfg_lookup_pset(handle, &pset_tab) == Z_OK)
5290 5384 destroy_tmp_pool = B_FALSE;
5291 5385
5292 5386 zonecfg_fini_handle(handle);
5293 5387 }
5294 5388
5295 5389 if (destroy_tmp_pool) {
5296 5390 if ((res = zonecfg_destroy_tmp_pool(zone_name, pool_err,
5297 5391 sizeof (pool_err))) != Z_OK) {
5298 5392 if (res == Z_POOL)
5299 5393 zerror(zlogp, B_FALSE, pool_err);
5300 5394 }
5301 5395 }
5302 5396 }
5303 5397
5304 5398 remove_mlps(zlogp, zoneid);
5305 5399
5306 5400 if (zone_destroy(zoneid) != 0) {
5307 5401 zerror(zlogp, B_TRUE, "unable to destroy zone");
5308 5402 goto error;
5309 5403 }
5310 5404
5311 5405 /*
5312 5406 * Special teardown for alternate boot environments: remove the tmpfs
5313 5407 * root for the zone and then remove it from the map file.
5314 5408 */
5315 5409 if (unmount_cmd && lu_root_teardown(zlogp) != 0)
5316 5410 goto error;
5317 5411
5318 5412 lofs_discard_mnttab();
5319 5413 return (0);
5320 5414
5321 5415 error:
5322 5416 lofs_discard_mnttab();
5323 5417 return (-1);
5324 5418 }
↓ open down ↓ |
660 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX