1 /*- 2 * Copyright (c) 2005-2011 Pawel Jakub Dawidek <pawel@dawidek.net> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/kernel.h> 33 #include <sys/module.h> 34 #include <sys/lock.h> 35 #include <sys/mutex.h> 36 #include <sys/bio.h> 37 #include <sys/sysctl.h> 38 #include <sys/malloc.h> 39 #include <sys/kthread.h> 40 #include <sys/proc.h> 41 #include <sys/sched.h> 42 #include <sys/uio.h> 43 44 #include <vm/uma.h> 45 46 #include <geom/geom.h> 47 #include <geom/eli/g_eli.h> 48 49 50 MALLOC_DECLARE(M_ELI); 51 52 53 static void 54 g_eli_ctl_attach(struct gctl_req *req, struct g_class *mp) 55 { 56 struct g_eli_metadata md; 57 struct g_provider *pp; 58 const char *name; 59 u_char *key, mkey[G_ELI_DATAIVKEYLEN]; 60 int *nargs, *detach, *readonly; 61 int keysize, error; 62 u_int nkey; 63 64 g_topology_assert(); 65 66 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 67 if (nargs == NULL) { 68 gctl_error(req, "No '%s' argument.", "nargs"); 69 return; 70 } 71 if (*nargs != 1) { 72 gctl_error(req, "Invalid number of arguments."); 73 return; 74 } 75 76 detach = gctl_get_paraml(req, "detach", sizeof(*detach)); 77 if (detach == NULL) { 78 gctl_error(req, "No '%s' argument.", "detach"); 79 return; 80 } 81 82 readonly = gctl_get_paraml(req, "readonly", sizeof(*readonly)); 83 if (readonly == NULL) { 84 gctl_error(req, "No '%s' argument.", "readonly"); 85 return; 86 } 87 88 if (*detach && *readonly) { 89 gctl_error(req, "Options -d and -r are mutually exclusive."); 90 return; 91 } 92 93 name = gctl_get_asciiparam(req, "arg0"); 94 if (name == NULL) { 95 gctl_error(req, "No 'arg%u' argument.", 0); 96 return; 97 } 98 if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 99 name += strlen("/dev/"); 100 pp = g_provider_by_name(name); 101 if (pp == NULL) { 102 gctl_error(req, "Provider %s is invalid.", name); 103 return; 104 } 105 error = g_eli_read_metadata(mp, pp, &md); 106 if (error != 0) { 107 gctl_error(req, "Cannot read metadata from %s (error=%d).", 108 name, error); 109 return; 110 } 111 if (md.md_keys == 0x00) { 112 explicit_bzero(&md, sizeof(md)); 113 gctl_error(req, "No valid keys on %s.", pp->name); 114 return; 115 } 116 117 key = gctl_get_param(req, "key", &keysize); 118 if (key == NULL || keysize != G_ELI_USERKEYLEN) { 119 explicit_bzero(&md, sizeof(md)); 120 gctl_error(req, "No '%s' argument.", "key"); 121 return; 122 } 123 124 error = g_eli_mkey_decrypt(&md, key, mkey, &nkey); 125 explicit_bzero(key, keysize); 126 if (error == -1) { 127 explicit_bzero(&md, sizeof(md)); 128 gctl_error(req, "Wrong key for %s.", pp->name); 129 return; 130 } else if (error > 0) { 131 explicit_bzero(&md, sizeof(md)); 132 gctl_error(req, "Cannot decrypt Master Key for %s (error=%d).", 133 pp->name, error); 134 return; 135 } 136 G_ELI_DEBUG(1, "Using Master Key %u for %s.", nkey, pp->name); 137 138 if (*detach) 139 md.md_flags |= G_ELI_FLAG_WO_DETACH; 140 if (*readonly) 141 md.md_flags |= G_ELI_FLAG_RO; 142 g_eli_create(req, mp, pp, &md, mkey, nkey); 143 explicit_bzero(mkey, sizeof(mkey)); 144 explicit_bzero(&md, sizeof(md)); 145 } 146 147 static struct g_eli_softc * 148 g_eli_find_device(struct g_class *mp, const char *prov) 149 { 150 struct g_eli_softc *sc; 151 struct g_geom *gp; 152 struct g_provider *pp; 153 struct g_consumer *cp; 154 155 if (strncmp(prov, "/dev/", strlen("/dev/")) == 0) 156 prov += strlen("/dev/"); 157 LIST_FOREACH(gp, &mp->geom, geom) { 158 sc = gp->softc; 159 if (sc == NULL) 160 continue; 161 pp = LIST_FIRST(&gp->provider); 162 if (pp != NULL && strcmp(pp->name, prov) == 0) 163 return (sc); 164 cp = LIST_FIRST(&gp->consumer); 165 if (cp != NULL && cp->provider != NULL && 166 strcmp(cp->provider->name, prov) == 0) { 167 return (sc); 168 } 169 } 170 return (NULL); 171 } 172 173 static void 174 g_eli_ctl_detach(struct gctl_req *req, struct g_class *mp) 175 { 176 struct g_eli_softc *sc; 177 int *force, *last, *nargs, error; 178 const char *prov; 179 char param[16]; 180 int i; 181 182 g_topology_assert(); 183 184 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 185 if (nargs == NULL) { 186 gctl_error(req, "No '%s' argument.", "nargs"); 187 return; 188 } 189 if (*nargs <= 0) { 190 gctl_error(req, "Missing device(s)."); 191 return; 192 } 193 force = gctl_get_paraml(req, "force", sizeof(*force)); 194 if (force == NULL) { 195 gctl_error(req, "No '%s' argument.", "force"); 196 return; 197 } 198 last = gctl_get_paraml(req, "last", sizeof(*last)); 199 if (last == NULL) { 200 gctl_error(req, "No '%s' argument.", "last"); 201 return; 202 } 203 204 for (i = 0; i < *nargs; i++) { 205 snprintf(param, sizeof(param), "arg%d", i); 206 prov = gctl_get_asciiparam(req, param); 207 if (prov == NULL) { 208 gctl_error(req, "No 'arg%d' argument.", i); 209 return; 210 } 211 sc = g_eli_find_device(mp, prov); 212 if (sc == NULL) { 213 gctl_error(req, "No such device: %s.", prov); 214 return; 215 } 216 if (*last) { 217 sc->sc_flags |= G_ELI_FLAG_RW_DETACH; 218 sc->sc_geom->access = g_eli_access; 219 } else { 220 error = g_eli_destroy(sc, *force ? TRUE : FALSE); 221 if (error != 0) { 222 gctl_error(req, 223 "Cannot destroy device %s (error=%d).", 224 sc->sc_name, error); 225 return; 226 } 227 } 228 } 229 } 230 231 static void 232 g_eli_ctl_onetime(struct gctl_req *req, struct g_class *mp) 233 { 234 struct g_eli_metadata md; 235 struct g_provider *pp; 236 const char *name; 237 intmax_t *keylen, *sectorsize; 238 u_char mkey[G_ELI_DATAIVKEYLEN]; 239 int *nargs, *detach, *notrim; 240 241 g_topology_assert(); 242 bzero(&md, sizeof(md)); 243 244 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 245 if (nargs == NULL) { 246 gctl_error(req, "No '%s' argument.", "nargs"); 247 return; 248 } 249 if (*nargs != 1) { 250 gctl_error(req, "Invalid number of arguments."); 251 return; 252 } 253 254 strlcpy(md.md_magic, G_ELI_MAGIC, sizeof(md.md_magic)); 255 md.md_version = G_ELI_VERSION; 256 md.md_flags |= G_ELI_FLAG_ONETIME; 257 258 detach = gctl_get_paraml(req, "detach", sizeof(*detach)); 259 if (detach != NULL && *detach) 260 md.md_flags |= G_ELI_FLAG_WO_DETACH; 261 notrim = gctl_get_paraml(req, "notrim", sizeof(*notrim)); 262 if (notrim != NULL && *notrim) 263 md.md_flags |= G_ELI_FLAG_NODELETE; 264 265 md.md_ealgo = CRYPTO_ALGORITHM_MIN - 1; 266 name = gctl_get_asciiparam(req, "aalgo"); 267 if (name == NULL) { 268 gctl_error(req, "No '%s' argument.", "aalgo"); 269 return; 270 } 271 if (*name != '\0') { 272 md.md_aalgo = g_eli_str2aalgo(name); 273 if (md.md_aalgo >= CRYPTO_ALGORITHM_MIN && 274 md.md_aalgo <= CRYPTO_ALGORITHM_MAX) { 275 md.md_flags |= G_ELI_FLAG_AUTH; 276 } else { 277 /* 278 * For backward compatibility, check if the -a option 279 * was used to provide encryption algorithm. 280 */ 281 md.md_ealgo = g_eli_str2ealgo(name); 282 if (md.md_ealgo < CRYPTO_ALGORITHM_MIN || 283 md.md_ealgo > CRYPTO_ALGORITHM_MAX) { 284 gctl_error(req, 285 "Invalid authentication algorithm."); 286 return; 287 } else { 288 gctl_error(req, "warning: The -e option, not " 289 "the -a option is now used to specify " 290 "encryption algorithm to use."); 291 } 292 } 293 } 294 295 if (md.md_ealgo < CRYPTO_ALGORITHM_MIN || 296 md.md_ealgo > CRYPTO_ALGORITHM_MAX) { 297 name = gctl_get_asciiparam(req, "ealgo"); 298 if (name == NULL) { 299 gctl_error(req, "No '%s' argument.", "ealgo"); 300 return; 301 } 302 md.md_ealgo = g_eli_str2ealgo(name); 303 if (md.md_ealgo < CRYPTO_ALGORITHM_MIN || 304 md.md_ealgo > CRYPTO_ALGORITHM_MAX) { 305 gctl_error(req, "Invalid encryption algorithm."); 306 return; 307 } 308 } 309 310 keylen = gctl_get_paraml(req, "keylen", sizeof(*keylen)); 311 if (keylen == NULL) { 312 gctl_error(req, "No '%s' argument.", "keylen"); 313 return; 314 } 315 md.md_keylen = g_eli_keylen(md.md_ealgo, *keylen); 316 if (md.md_keylen == 0) { 317 gctl_error(req, "Invalid '%s' argument.", "keylen"); 318 return; 319 } 320 321 /* Not important here. */ 322 md.md_provsize = 0; 323 /* Not important here. */ 324 bzero(md.md_salt, sizeof(md.md_salt)); 325 326 md.md_keys = 0x01; 327 arc4rand(mkey, sizeof(mkey), 0); 328 329 /* Not important here. */ 330 bzero(md.md_hash, sizeof(md.md_hash)); 331 332 name = gctl_get_asciiparam(req, "arg0"); 333 if (name == NULL) { 334 gctl_error(req, "No 'arg%u' argument.", 0); 335 return; 336 } 337 if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 338 name += strlen("/dev/"); 339 pp = g_provider_by_name(name); 340 if (pp == NULL) { 341 gctl_error(req, "Provider %s is invalid.", name); 342 return; 343 } 344 345 sectorsize = gctl_get_paraml(req, "sectorsize", sizeof(*sectorsize)); 346 if (sectorsize == NULL) { 347 gctl_error(req, "No '%s' argument.", "sectorsize"); 348 return; 349 } 350 if (*sectorsize == 0) 351 md.md_sectorsize = pp->sectorsize; 352 else { 353 if (*sectorsize < 0 || (*sectorsize % pp->sectorsize) != 0) { 354 gctl_error(req, "Invalid sector size."); 355 return; 356 } 357 if (*sectorsize > PAGE_SIZE) { 358 gctl_error(req, "warning: Using sectorsize bigger than " 359 "the page size!"); 360 } 361 md.md_sectorsize = *sectorsize; 362 } 363 364 g_eli_create(req, mp, pp, &md, mkey, -1); 365 explicit_bzero(mkey, sizeof(mkey)); 366 explicit_bzero(&md, sizeof(md)); 367 } 368 369 static void 370 g_eli_ctl_configure(struct gctl_req *req, struct g_class *mp) 371 { 372 struct g_eli_softc *sc; 373 struct g_eli_metadata md; 374 struct g_provider *pp; 375 struct g_consumer *cp; 376 char param[16]; 377 const char *prov; 378 u_char *sector; 379 int *nargs, *boot, *noboot, *trim, *notrim, *geliboot, *nogeliboot; 380 int zero, error, changed; 381 u_int i; 382 383 g_topology_assert(); 384 385 changed = 0; 386 zero = 0; 387 388 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 389 if (nargs == NULL) { 390 gctl_error(req, "No '%s' argument.", "nargs"); 391 return; 392 } 393 if (*nargs <= 0) { 394 gctl_error(req, "Missing device(s)."); 395 return; 396 } 397 398 boot = gctl_get_paraml(req, "boot", sizeof(*boot)); 399 if (boot == NULL) 400 boot = &zero; 401 noboot = gctl_get_paraml(req, "noboot", sizeof(*noboot)); 402 if (noboot == NULL) 403 noboot = &zero; 404 if (*boot && *noboot) { 405 gctl_error(req, "Options -b and -B are mutually exclusive."); 406 return; 407 } 408 if (*boot || *noboot) 409 changed = 1; 410 411 trim = gctl_get_paraml(req, "trim", sizeof(*trim)); 412 if (trim == NULL) 413 trim = &zero; 414 notrim = gctl_get_paraml(req, "notrim", sizeof(*notrim)); 415 if (notrim == NULL) 416 notrim = &zero; 417 if (*trim && *notrim) { 418 gctl_error(req, "Options -t and -T are mutually exclusive."); 419 return; 420 } 421 if (*trim || *notrim) 422 changed = 1; 423 424 geliboot = gctl_get_paraml(req, "geliboot", sizeof(*geliboot)); 425 if (geliboot == NULL) 426 geliboot = &zero; 427 nogeliboot = gctl_get_paraml(req, "nogeliboot", sizeof(*nogeliboot)); 428 if (nogeliboot == NULL) 429 nogeliboot = &zero; 430 if (*geliboot && *nogeliboot) { 431 gctl_error(req, "Options -g and -G are mutually exclusive."); 432 return; 433 } 434 if (*geliboot || *nogeliboot) 435 changed = 1; 436 437 if (!changed) { 438 gctl_error(req, "No option given."); 439 return; 440 } 441 442 for (i = 0; i < *nargs; i++) { 443 snprintf(param, sizeof(param), "arg%d", i); 444 prov = gctl_get_asciiparam(req, param); 445 if (prov == NULL) { 446 gctl_error(req, "No 'arg%d' argument.", i); 447 return; 448 } 449 sc = g_eli_find_device(mp, prov); 450 if (sc == NULL) { 451 /* 452 * We ignore not attached providers, userland part will 453 * take care of them. 454 */ 455 G_ELI_DEBUG(1, "Skipping configuration of not attached " 456 "provider %s.", prov); 457 continue; 458 } 459 if (sc->sc_flags & G_ELI_FLAG_RO) { 460 gctl_error(req, "Cannot change configuration of " 461 "read-only provider %s.", prov); 462 continue; 463 } 464 465 if (*boot && (sc->sc_flags & G_ELI_FLAG_BOOT)) { 466 G_ELI_DEBUG(1, "BOOT flag already configured for %s.", 467 prov); 468 continue; 469 } else if (*noboot && !(sc->sc_flags & G_ELI_FLAG_BOOT)) { 470 G_ELI_DEBUG(1, "BOOT flag not configured for %s.", 471 prov); 472 continue; 473 } 474 475 if (*notrim && (sc->sc_flags & G_ELI_FLAG_NODELETE)) { 476 G_ELI_DEBUG(1, "TRIM disable flag already configured for %s.", 477 prov); 478 continue; 479 } else if (*trim && !(sc->sc_flags & G_ELI_FLAG_NODELETE)) { 480 G_ELI_DEBUG(1, "TRIM disable flag not configured for %s.", 481 prov); 482 continue; 483 } 484 485 if (*geliboot && (sc->sc_flags & G_ELI_FLAG_GELIBOOT)) { 486 G_ELI_DEBUG(1, "GELIBOOT flag already configured for %s.", 487 prov); 488 continue; 489 } else if (*nogeliboot && !(sc->sc_flags & G_ELI_FLAG_GELIBOOT)) { 490 G_ELI_DEBUG(1, "GELIBOOT flag not configured for %s.", 491 prov); 492 continue; 493 } 494 495 if (!(sc->sc_flags & G_ELI_FLAG_ONETIME)) { 496 /* 497 * ONETIME providers don't write metadata to 498 * disk, so don't try reading it. This means 499 * we're bit-flipping uninitialized memory in md 500 * below, but that's OK; we don't do anything 501 * with it later. 502 */ 503 cp = LIST_FIRST(&sc->sc_geom->consumer); 504 pp = cp->provider; 505 error = g_eli_read_metadata(mp, pp, &md); 506 if (error != 0) { 507 gctl_error(req, 508 "Cannot read metadata from %s (error=%d).", 509 prov, error); 510 continue; 511 } 512 } 513 514 if (*boot) { 515 md.md_flags |= G_ELI_FLAG_BOOT; 516 sc->sc_flags |= G_ELI_FLAG_BOOT; 517 } else if (*noboot) { 518 md.md_flags &= ~G_ELI_FLAG_BOOT; 519 sc->sc_flags &= ~G_ELI_FLAG_BOOT; 520 } 521 522 if (*notrim) { 523 md.md_flags |= G_ELI_FLAG_NODELETE; 524 sc->sc_flags |= G_ELI_FLAG_NODELETE; 525 } else if (*trim) { 526 md.md_flags &= ~G_ELI_FLAG_NODELETE; 527 sc->sc_flags &= ~G_ELI_FLAG_NODELETE; 528 } 529 530 if (*geliboot) { 531 md.md_flags |= G_ELI_FLAG_GELIBOOT; 532 sc->sc_flags |= G_ELI_FLAG_GELIBOOT; 533 } else if (*nogeliboot) { 534 md.md_flags &= ~G_ELI_FLAG_GELIBOOT; 535 sc->sc_flags &= ~G_ELI_FLAG_GELIBOOT; 536 } 537 538 if (sc->sc_flags & G_ELI_FLAG_ONETIME) { 539 /* There's no metadata on disk so we are done here. */ 540 continue; 541 } 542 543 sector = malloc(pp->sectorsize, M_ELI, M_WAITOK | M_ZERO); 544 eli_metadata_encode(&md, sector); 545 error = g_write_data(cp, pp->mediasize - pp->sectorsize, sector, 546 pp->sectorsize); 547 if (error != 0) { 548 gctl_error(req, 549 "Cannot store metadata on %s (error=%d).", 550 prov, error); 551 } 552 explicit_bzero(&md, sizeof(md)); 553 explicit_bzero(sector, pp->sectorsize); 554 free(sector, M_ELI); 555 } 556 } 557 558 static void 559 g_eli_ctl_setkey(struct gctl_req *req, struct g_class *mp) 560 { 561 struct g_eli_softc *sc; 562 struct g_eli_metadata md; 563 struct g_provider *pp; 564 struct g_consumer *cp; 565 const char *name; 566 u_char *key, *mkeydst, *sector; 567 intmax_t *valp; 568 int keysize, nkey, error; 569 570 g_topology_assert(); 571 572 name = gctl_get_asciiparam(req, "arg0"); 573 if (name == NULL) { 574 gctl_error(req, "No 'arg%u' argument.", 0); 575 return; 576 } 577 key = gctl_get_param(req, "key", &keysize); 578 if (key == NULL || keysize != G_ELI_USERKEYLEN) { 579 gctl_error(req, "No '%s' argument.", "key"); 580 return; 581 } 582 sc = g_eli_find_device(mp, name); 583 if (sc == NULL) { 584 gctl_error(req, "Provider %s is invalid.", name); 585 return; 586 } 587 if (sc->sc_flags & G_ELI_FLAG_RO) { 588 gctl_error(req, "Cannot change keys for read-only provider."); 589 return; 590 } 591 cp = LIST_FIRST(&sc->sc_geom->consumer); 592 pp = cp->provider; 593 594 error = g_eli_read_metadata(mp, pp, &md); 595 if (error != 0) { 596 gctl_error(req, "Cannot read metadata from %s (error=%d).", 597 name, error); 598 return; 599 } 600 601 valp = gctl_get_paraml(req, "keyno", sizeof(*valp)); 602 if (valp == NULL) { 603 gctl_error(req, "No '%s' argument.", "keyno"); 604 return; 605 } 606 if (*valp != -1) 607 nkey = *valp; 608 else 609 nkey = sc->sc_nkey; 610 if (nkey < 0 || nkey >= G_ELI_MAXMKEYS) { 611 gctl_error(req, "Invalid '%s' argument.", "keyno"); 612 return; 613 } 614 615 valp = gctl_get_paraml(req, "iterations", sizeof(*valp)); 616 if (valp == NULL) { 617 gctl_error(req, "No '%s' argument.", "iterations"); 618 return; 619 } 620 /* Check if iterations number should and can be changed. */ 621 if (*valp != -1) { 622 if (bitcount32(md.md_keys) != 1) { 623 gctl_error(req, "To be able to use '-i' option, only " 624 "one key can be defined."); 625 return; 626 } 627 if (md.md_keys != (1 << nkey)) { 628 gctl_error(req, "Only already defined key can be " 629 "changed when '-i' option is used."); 630 return; 631 } 632 md.md_iterations = *valp; 633 } 634 635 mkeydst = md.md_mkeys + nkey * G_ELI_MKEYLEN; 636 md.md_keys |= (1 << nkey); 637 638 bcopy(sc->sc_mkey, mkeydst, sizeof(sc->sc_mkey)); 639 640 /* Encrypt Master Key with the new key. */ 641 error = g_eli_mkey_encrypt(md.md_ealgo, key, md.md_keylen, mkeydst); 642 explicit_bzero(key, keysize); 643 if (error != 0) { 644 explicit_bzero(&md, sizeof(md)); 645 gctl_error(req, "Cannot encrypt Master Key (error=%d).", error); 646 return; 647 } 648 649 sector = malloc(pp->sectorsize, M_ELI, M_WAITOK | M_ZERO); 650 /* Store metadata with fresh key. */ 651 eli_metadata_encode(&md, sector); 652 explicit_bzero(&md, sizeof(md)); 653 error = g_write_data(cp, pp->mediasize - pp->sectorsize, sector, 654 pp->sectorsize); 655 explicit_bzero(sector, pp->sectorsize); 656 free(sector, M_ELI); 657 if (error != 0) { 658 gctl_error(req, "Cannot store metadata on %s (error=%d).", 659 pp->name, error); 660 return; 661 } 662 G_ELI_DEBUG(1, "Key %u changed on %s.", nkey, pp->name); 663 } 664 665 static void 666 g_eli_ctl_delkey(struct gctl_req *req, struct g_class *mp) 667 { 668 struct g_eli_softc *sc; 669 struct g_eli_metadata md; 670 struct g_provider *pp; 671 struct g_consumer *cp; 672 const char *name; 673 u_char *mkeydst, *sector; 674 intmax_t *valp; 675 size_t keysize; 676 int error, nkey, *all, *force; 677 u_int i; 678 679 g_topology_assert(); 680 681 nkey = 0; /* fixes causeless gcc warning */ 682 683 name = gctl_get_asciiparam(req, "arg0"); 684 if (name == NULL) { 685 gctl_error(req, "No 'arg%u' argument.", 0); 686 return; 687 } 688 sc = g_eli_find_device(mp, name); 689 if (sc == NULL) { 690 gctl_error(req, "Provider %s is invalid.", name); 691 return; 692 } 693 if (sc->sc_flags & G_ELI_FLAG_RO) { 694 gctl_error(req, "Cannot delete keys for read-only provider."); 695 return; 696 } 697 cp = LIST_FIRST(&sc->sc_geom->consumer); 698 pp = cp->provider; 699 700 error = g_eli_read_metadata(mp, pp, &md); 701 if (error != 0) { 702 gctl_error(req, "Cannot read metadata from %s (error=%d).", 703 name, error); 704 return; 705 } 706 707 all = gctl_get_paraml(req, "all", sizeof(*all)); 708 if (all == NULL) { 709 gctl_error(req, "No '%s' argument.", "all"); 710 return; 711 } 712 713 if (*all) { 714 mkeydst = md.md_mkeys; 715 keysize = sizeof(md.md_mkeys); 716 } else { 717 force = gctl_get_paraml(req, "force", sizeof(*force)); 718 if (force == NULL) { 719 gctl_error(req, "No '%s' argument.", "force"); 720 return; 721 } 722 723 valp = gctl_get_paraml(req, "keyno", sizeof(*valp)); 724 if (valp == NULL) { 725 gctl_error(req, "No '%s' argument.", "keyno"); 726 return; 727 } 728 if (*valp != -1) 729 nkey = *valp; 730 else 731 nkey = sc->sc_nkey; 732 if (nkey < 0 || nkey >= G_ELI_MAXMKEYS) { 733 gctl_error(req, "Invalid '%s' argument.", "keyno"); 734 return; 735 } 736 if (!(md.md_keys & (1 << nkey)) && !*force) { 737 gctl_error(req, "Master Key %u is not set.", nkey); 738 return; 739 } 740 md.md_keys &= ~(1 << nkey); 741 if (md.md_keys == 0 && !*force) { 742 gctl_error(req, "This is the last Master Key. Use '-f' " 743 "flag if you really want to remove it."); 744 return; 745 } 746 mkeydst = md.md_mkeys + nkey * G_ELI_MKEYLEN; 747 keysize = G_ELI_MKEYLEN; 748 } 749 750 sector = malloc(pp->sectorsize, M_ELI, M_WAITOK | M_ZERO); 751 for (i = 0; i <= g_eli_overwrites; i++) { 752 if (i == g_eli_overwrites) 753 explicit_bzero(mkeydst, keysize); 754 else 755 arc4rand(mkeydst, keysize, 0); 756 /* Store metadata with destroyed key. */ 757 eli_metadata_encode(&md, sector); 758 error = g_write_data(cp, pp->mediasize - pp->sectorsize, sector, 759 pp->sectorsize); 760 if (error != 0) { 761 G_ELI_DEBUG(0, "Cannot store metadata on %s " 762 "(error=%d).", pp->name, error); 763 } 764 /* 765 * Flush write cache so we don't overwrite data N times in cache 766 * and only once on disk. 767 */ 768 (void)g_io_flush(cp); 769 } 770 explicit_bzero(&md, sizeof(md)); 771 explicit_bzero(sector, pp->sectorsize); 772 free(sector, M_ELI); 773 if (*all) 774 G_ELI_DEBUG(1, "All keys removed from %s.", pp->name); 775 else 776 G_ELI_DEBUG(1, "Key %d removed from %s.", nkey, pp->name); 777 } 778 779 static void 780 g_eli_suspend_one(struct g_eli_softc *sc, struct gctl_req *req) 781 { 782 struct g_eli_worker *wr; 783 784 g_topology_assert(); 785 786 KASSERT(sc != NULL, ("NULL sc")); 787 788 if (sc->sc_flags & G_ELI_FLAG_ONETIME) { 789 gctl_error(req, 790 "Device %s is using one-time key, suspend not supported.", 791 sc->sc_name); 792 return; 793 } 794 795 mtx_lock(&sc->sc_queue_mtx); 796 if (sc->sc_flags & G_ELI_FLAG_SUSPEND) { 797 mtx_unlock(&sc->sc_queue_mtx); 798 gctl_error(req, "Device %s already suspended.", 799 sc->sc_name); 800 return; 801 } 802 sc->sc_flags |= G_ELI_FLAG_SUSPEND; 803 wakeup(sc); 804 for (;;) { 805 LIST_FOREACH(wr, &sc->sc_workers, w_next) { 806 if (wr->w_active) 807 break; 808 } 809 if (wr == NULL) 810 break; 811 /* Not all threads suspended. */ 812 msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO, 813 "geli:suspend", 0); 814 } 815 /* 816 * Clear sensitive data on suspend, they will be recovered on resume. 817 */ 818 explicit_bzero(sc->sc_mkey, sizeof(sc->sc_mkey)); 819 g_eli_key_destroy(sc); 820 explicit_bzero(sc->sc_akey, sizeof(sc->sc_akey)); 821 explicit_bzero(&sc->sc_akeyctx, sizeof(sc->sc_akeyctx)); 822 explicit_bzero(sc->sc_ivkey, sizeof(sc->sc_ivkey)); 823 explicit_bzero(&sc->sc_ivctx, sizeof(sc->sc_ivctx)); 824 mtx_unlock(&sc->sc_queue_mtx); 825 G_ELI_DEBUG(0, "Device %s has been suspended.", sc->sc_name); 826 } 827 828 static void 829 g_eli_ctl_suspend(struct gctl_req *req, struct g_class *mp) 830 { 831 struct g_eli_softc *sc; 832 int *all, *nargs; 833 834 g_topology_assert(); 835 836 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 837 if (nargs == NULL) { 838 gctl_error(req, "No '%s' argument.", "nargs"); 839 return; 840 } 841 all = gctl_get_paraml(req, "all", sizeof(*all)); 842 if (all == NULL) { 843 gctl_error(req, "No '%s' argument.", "all"); 844 return; 845 } 846 if (!*all && *nargs == 0) { 847 gctl_error(req, "Too few arguments."); 848 return; 849 } 850 851 if (*all) { 852 struct g_geom *gp, *gp2; 853 854 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) { 855 sc = gp->softc; 856 if (sc->sc_flags & G_ELI_FLAG_ONETIME) { 857 G_ELI_DEBUG(0, 858 "Device %s is using one-time key, suspend not supported, skipping.", 859 sc->sc_name); 860 continue; 861 } 862 g_eli_suspend_one(sc, req); 863 } 864 } else { 865 const char *prov; 866 char param[16]; 867 int i; 868 869 for (i = 0; i < *nargs; i++) { 870 snprintf(param, sizeof(param), "arg%d", i); 871 prov = gctl_get_asciiparam(req, param); 872 if (prov == NULL) { 873 G_ELI_DEBUG(0, "No 'arg%d' argument.", i); 874 continue; 875 } 876 877 sc = g_eli_find_device(mp, prov); 878 if (sc == NULL) { 879 G_ELI_DEBUG(0, "No such provider: %s.", prov); 880 continue; 881 } 882 g_eli_suspend_one(sc, req); 883 } 884 } 885 } 886 887 static void 888 g_eli_ctl_resume(struct gctl_req *req, struct g_class *mp) 889 { 890 struct g_eli_metadata md; 891 struct g_eli_softc *sc; 892 struct g_provider *pp; 893 struct g_consumer *cp; 894 const char *name; 895 u_char *key, mkey[G_ELI_DATAIVKEYLEN]; 896 int *nargs, keysize, error; 897 u_int nkey; 898 899 g_topology_assert(); 900 901 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 902 if (nargs == NULL) { 903 gctl_error(req, "No '%s' argument.", "nargs"); 904 return; 905 } 906 if (*nargs != 1) { 907 gctl_error(req, "Invalid number of arguments."); 908 return; 909 } 910 911 name = gctl_get_asciiparam(req, "arg0"); 912 if (name == NULL) { 913 gctl_error(req, "No 'arg%u' argument.", 0); 914 return; 915 } 916 key = gctl_get_param(req, "key", &keysize); 917 if (key == NULL || keysize != G_ELI_USERKEYLEN) { 918 gctl_error(req, "No '%s' argument.", "key"); 919 return; 920 } 921 sc = g_eli_find_device(mp, name); 922 if (sc == NULL) { 923 gctl_error(req, "Provider %s is invalid.", name); 924 return; 925 } 926 cp = LIST_FIRST(&sc->sc_geom->consumer); 927 pp = cp->provider; 928 error = g_eli_read_metadata(mp, pp, &md); 929 if (error != 0) { 930 gctl_error(req, "Cannot read metadata from %s (error=%d).", 931 name, error); 932 return; 933 } 934 if (md.md_keys == 0x00) { 935 explicit_bzero(&md, sizeof(md)); 936 gctl_error(req, "No valid keys on %s.", pp->name); 937 return; 938 } 939 940 error = g_eli_mkey_decrypt(&md, key, mkey, &nkey); 941 explicit_bzero(key, keysize); 942 if (error == -1) { 943 explicit_bzero(&md, sizeof(md)); 944 gctl_error(req, "Wrong key for %s.", pp->name); 945 return; 946 } else if (error > 0) { 947 explicit_bzero(&md, sizeof(md)); 948 gctl_error(req, "Cannot decrypt Master Key for %s (error=%d).", 949 pp->name, error); 950 return; 951 } 952 G_ELI_DEBUG(1, "Using Master Key %u for %s.", nkey, pp->name); 953 954 mtx_lock(&sc->sc_queue_mtx); 955 if (!(sc->sc_flags & G_ELI_FLAG_SUSPEND)) 956 gctl_error(req, "Device %s is not suspended.", name); 957 else { 958 /* Restore sc_mkey, sc_ekeys, sc_akey and sc_ivkey. */ 959 g_eli_mkey_propagate(sc, mkey); 960 sc->sc_flags &= ~G_ELI_FLAG_SUSPEND; 961 G_ELI_DEBUG(1, "Resumed %s.", pp->name); 962 wakeup(sc); 963 } 964 mtx_unlock(&sc->sc_queue_mtx); 965 explicit_bzero(mkey, sizeof(mkey)); 966 explicit_bzero(&md, sizeof(md)); 967 } 968 969 static int 970 g_eli_kill_one(struct g_eli_softc *sc) 971 { 972 struct g_provider *pp; 973 struct g_consumer *cp; 974 int error = 0; 975 976 g_topology_assert(); 977 978 if (sc == NULL) 979 return (ENOENT); 980 981 pp = LIST_FIRST(&sc->sc_geom->provider); 982 g_error_provider(pp, ENXIO); 983 984 cp = LIST_FIRST(&sc->sc_geom->consumer); 985 pp = cp->provider; 986 987 if (sc->sc_flags & G_ELI_FLAG_RO) { 988 G_ELI_DEBUG(0, "WARNING: Metadata won't be erased on read-only " 989 "provider: %s.", pp->name); 990 } else { 991 u_char *sector; 992 u_int i; 993 int err; 994 995 sector = malloc(pp->sectorsize, M_ELI, M_WAITOK); 996 for (i = 0; i <= g_eli_overwrites; i++) { 997 if (i == g_eli_overwrites) 998 bzero(sector, pp->sectorsize); 999 else 1000 arc4rand(sector, pp->sectorsize, 0); 1001 err = g_write_data(cp, pp->mediasize - pp->sectorsize, 1002 sector, pp->sectorsize); 1003 if (err != 0) { 1004 G_ELI_DEBUG(0, "Cannot erase metadata on %s " 1005 "(error=%d).", pp->name, err); 1006 if (error == 0) 1007 error = err; 1008 } 1009 /* 1010 * Flush write cache so we don't overwrite data N times 1011 * in cache and only once on disk. 1012 */ 1013 (void)g_io_flush(cp); 1014 } 1015 free(sector, M_ELI); 1016 } 1017 if (error == 0) 1018 G_ELI_DEBUG(0, "%s has been killed.", pp->name); 1019 g_eli_destroy(sc, TRUE); 1020 return (error); 1021 } 1022 1023 static void 1024 g_eli_ctl_kill(struct gctl_req *req, struct g_class *mp) 1025 { 1026 int *all, *nargs; 1027 int error; 1028 1029 g_topology_assert(); 1030 1031 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 1032 if (nargs == NULL) { 1033 gctl_error(req, "No '%s' argument.", "nargs"); 1034 return; 1035 } 1036 all = gctl_get_paraml(req, "all", sizeof(*all)); 1037 if (all == NULL) { 1038 gctl_error(req, "No '%s' argument.", "all"); 1039 return; 1040 } 1041 if (!*all && *nargs == 0) { 1042 gctl_error(req, "Too few arguments."); 1043 return; 1044 } 1045 1046 if (*all) { 1047 struct g_geom *gp, *gp2; 1048 1049 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) { 1050 error = g_eli_kill_one(gp->softc); 1051 if (error != 0) 1052 gctl_error(req, "Not fully done."); 1053 } 1054 } else { 1055 struct g_eli_softc *sc; 1056 const char *prov; 1057 char param[16]; 1058 int i; 1059 1060 for (i = 0; i < *nargs; i++) { 1061 snprintf(param, sizeof(param), "arg%d", i); 1062 prov = gctl_get_asciiparam(req, param); 1063 if (prov == NULL) { 1064 G_ELI_DEBUG(0, "No 'arg%d' argument.", i); 1065 continue; 1066 } 1067 1068 sc = g_eli_find_device(mp, prov); 1069 if (sc == NULL) { 1070 G_ELI_DEBUG(0, "No such provider: %s.", prov); 1071 continue; 1072 } 1073 error = g_eli_kill_one(sc); 1074 if (error != 0) 1075 gctl_error(req, "Not fully done."); 1076 } 1077 } 1078 } 1079 1080 void 1081 g_eli_config(struct gctl_req *req, struct g_class *mp, const char *verb) 1082 { 1083 uint32_t *version; 1084 1085 g_topology_assert(); 1086 1087 version = gctl_get_paraml(req, "version", sizeof(*version)); 1088 if (version == NULL) { 1089 gctl_error(req, "No '%s' argument.", "version"); 1090 return; 1091 } 1092 while (*version != G_ELI_VERSION) { 1093 if (G_ELI_VERSION == G_ELI_VERSION_06 && 1094 *version == G_ELI_VERSION_05) { 1095 /* Compatible. */ 1096 break; 1097 } 1098 if (G_ELI_VERSION == G_ELI_VERSION_07 && 1099 (*version == G_ELI_VERSION_05 || 1100 *version == G_ELI_VERSION_06)) { 1101 /* Compatible. */ 1102 break; 1103 } 1104 gctl_error(req, "Userland and kernel parts are out of sync."); 1105 return; 1106 } 1107 1108 if (strcmp(verb, "attach") == 0) 1109 g_eli_ctl_attach(req, mp); 1110 else if (strcmp(verb, "detach") == 0 || strcmp(verb, "stop") == 0) 1111 g_eli_ctl_detach(req, mp); 1112 else if (strcmp(verb, "onetime") == 0) 1113 g_eli_ctl_onetime(req, mp); 1114 else if (strcmp(verb, "configure") == 0) 1115 g_eli_ctl_configure(req, mp); 1116 else if (strcmp(verb, "setkey") == 0) 1117 g_eli_ctl_setkey(req, mp); 1118 else if (strcmp(verb, "delkey") == 0) 1119 g_eli_ctl_delkey(req, mp); 1120 else if (strcmp(verb, "suspend") == 0) 1121 g_eli_ctl_suspend(req, mp); 1122 else if (strcmp(verb, "resume") == 0) 1123 g_eli_ctl_resume(req, mp); 1124 else if (strcmp(verb, "kill") == 0) 1125 g_eli_ctl_kill(req, mp); 1126 else 1127 gctl_error(req, "Unknown verb."); 1128 } 1129