1 /*- 2 * Copyright (c) 2005-2010 Pawel Jakub Dawidek <pjd@FreeBSD.org> 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/linker.h> 34 #include <sys/module.h> 35 #include <sys/lock.h> 36 #include <sys/mutex.h> 37 #include <sys/bio.h> 38 #include <sys/sysctl.h> 39 #include <sys/malloc.h> 40 #include <sys/eventhandler.h> 41 #include <sys/kthread.h> 42 #include <sys/proc.h> 43 #include <sys/sched.h> 44 #include <sys/smp.h> 45 #include <sys/uio.h> 46 #include <sys/vnode.h> 47 48 #include <vm/uma.h> 49 50 #include <geom/geom.h> 51 #include <geom/eli/g_eli.h> 52 #include <geom/eli/pkcs5v2.h> 53 54 55 MALLOC_DEFINE(M_ELI, "eli data", "GEOM_ELI Data"); 56 57 SYSCTL_DECL(_kern_geom); 58 SYSCTL_NODE(_kern_geom, OID_AUTO, eli, CTLFLAG_RW, 0, "GEOM_ELI stuff"); 59 int g_eli_debug = 0; 60 TUNABLE_INT("kern.geom.eli.debug", &g_eli_debug); 61 SYSCTL_INT(_kern_geom_eli, OID_AUTO, debug, CTLFLAG_RW, &g_eli_debug, 0, 62 "Debug level"); 63 static u_int g_eli_tries = 3; 64 TUNABLE_INT("kern.geom.eli.tries", &g_eli_tries); 65 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, tries, CTLFLAG_RW, &g_eli_tries, 0, 66 "Number of tries for entering the passphrase"); 67 static u_int g_eli_visible_passphrase = 0; 68 TUNABLE_INT("kern.geom.eli.visible_passphrase", &g_eli_visible_passphrase); 69 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, visible_passphrase, CTLFLAG_RW, 70 &g_eli_visible_passphrase, 0, 71 "Turn on echo when entering the passphrase (for debug purposes only!!)"); 72 u_int g_eli_overwrites = G_ELI_OVERWRITES; 73 TUNABLE_INT("kern.geom.eli.overwrites", &g_eli_overwrites); 74 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, overwrites, CTLFLAG_RW, &g_eli_overwrites, 75 0, "Number of times on-disk keys should be overwritten when destroying them"); 76 static u_int g_eli_threads = 0; 77 TUNABLE_INT("kern.geom.eli.threads", &g_eli_threads); 78 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, threads, CTLFLAG_RW, &g_eli_threads, 0, 79 "Number of threads doing crypto work"); 80 u_int g_eli_batch = 0; 81 TUNABLE_INT("kern.geom.eli.batch", &g_eli_batch); 82 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, batch, CTLFLAG_RW, &g_eli_batch, 0, 83 "Use crypto operations batching"); 84 85 static eventhandler_tag g_eli_pre_sync = NULL; 86 87 static int g_eli_destroy_geom(struct gctl_req *req, struct g_class *mp, 88 struct g_geom *gp); 89 static void g_eli_init(struct g_class *mp); 90 static void g_eli_fini(struct g_class *mp); 91 92 static g_taste_t g_eli_taste; 93 static g_dumpconf_t g_eli_dumpconf; 94 95 struct g_class g_eli_class = { 96 .name = G_ELI_CLASS_NAME, 97 .version = G_VERSION, 98 .ctlreq = g_eli_config, 99 .taste = g_eli_taste, 100 .destroy_geom = g_eli_destroy_geom, 101 .init = g_eli_init, 102 .fini = g_eli_fini 103 }; 104 105 106 /* 107 * Code paths: 108 * BIO_READ: 109 * g_eli_start -> g_io_request -> g_eli_read_done -> g_eli_crypto_run -> g_eli_crypto_read_done -> g_io_deliver 110 * BIO_WRITE: 111 * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver 112 */ 113 114 115 /* 116 * EAGAIN from crypto(9) means, that we were probably balanced to another crypto 117 * accelerator or something like this. 118 * The function updates the SID and rerun the operation. 119 */ 120 int 121 g_eli_crypto_rerun(struct cryptop *crp) 122 { 123 struct g_eli_softc *sc; 124 struct g_eli_worker *wr; 125 struct bio *bp; 126 int error; 127 128 bp = (struct bio *)crp->crp_opaque; 129 sc = bp->bio_to->geom->softc; 130 LIST_FOREACH(wr, &sc->sc_workers, w_next) { 131 if (wr->w_number == bp->bio_pflags) 132 break; 133 } 134 KASSERT(wr != NULL, ("Invalid worker (%u).", bp->bio_pflags)); 135 G_ELI_DEBUG(1, "Rerunning crypto %s request (sid: %ju -> %ju).", 136 bp->bio_cmd == BIO_READ ? "READ" : "WRITE", (uintmax_t)wr->w_sid, 137 (uintmax_t)crp->crp_sid); 138 wr->w_sid = crp->crp_sid; 139 crp->crp_etype = 0; 140 error = crypto_dispatch(crp); 141 if (error == 0) 142 return (0); 143 G_ELI_DEBUG(1, "%s: crypto_dispatch() returned %d.", __func__, error); 144 crp->crp_etype = error; 145 return (error); 146 } 147 148 /* 149 * The function is called afer reading encrypted data from the provider. 150 * 151 * g_eli_start -> g_io_request -> G_ELI_READ_DONE -> g_eli_crypto_run -> g_eli_crypto_read_done -> g_io_deliver 152 */ 153 void 154 g_eli_read_done(struct bio *bp) 155 { 156 struct g_eli_softc *sc; 157 struct bio *pbp; 158 159 G_ELI_LOGREQ(2, bp, "Request done."); 160 pbp = bp->bio_parent; 161 if (pbp->bio_error == 0) 162 pbp->bio_error = bp->bio_error; 163 /* 164 * Do we have all sectors already? 165 */ 166 pbp->bio_inbed++; 167 if (pbp->bio_inbed < pbp->bio_children) 168 return; 169 g_destroy_bio(bp); 170 if (pbp->bio_error != 0) { 171 G_ELI_LOGREQ(0, pbp, "%s() failed", __func__); 172 pbp->bio_completed = 0; 173 if (pbp->bio_driver2 != NULL) { 174 free(pbp->bio_driver2, M_ELI); 175 pbp->bio_driver2 = NULL; 176 } 177 g_io_deliver(pbp, pbp->bio_error); 178 return; 179 } 180 sc = pbp->bio_to->geom->softc; 181 mtx_lock(&sc->sc_queue_mtx); 182 bioq_insert_tail(&sc->sc_queue, pbp); 183 mtx_unlock(&sc->sc_queue_mtx); 184 wakeup(sc); 185 } 186 187 /* 188 * The function is called after we encrypt and write data. 189 * 190 * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> G_ELI_WRITE_DONE -> g_io_deliver 191 */ 192 void 193 g_eli_write_done(struct bio *bp) 194 { 195 struct bio *pbp; 196 197 G_ELI_LOGREQ(2, bp, "Request done."); 198 pbp = bp->bio_parent; 199 if (pbp->bio_error == 0) { 200 if (bp->bio_error != 0) 201 pbp->bio_error = bp->bio_error; 202 } 203 /* 204 * Do we have all sectors already? 205 */ 206 pbp->bio_inbed++; 207 if (pbp->bio_inbed < pbp->bio_children) 208 return; 209 free(pbp->bio_driver2, M_ELI); 210 pbp->bio_driver2 = NULL; 211 if (pbp->bio_error != 0) { 212 G_ELI_LOGREQ(0, pbp, "Crypto WRITE request failed (error=%d).", 213 pbp->bio_error); 214 pbp->bio_completed = 0; 215 } 216 g_destroy_bio(bp); 217 /* 218 * Write is finished, send it up. 219 */ 220 pbp->bio_completed = pbp->bio_length; 221 g_io_deliver(pbp, pbp->bio_error); 222 } 223 224 /* 225 * This function should never be called, but GEOM made as it set ->orphan() 226 * method for every geom. 227 */ 228 static void 229 g_eli_orphan_spoil_assert(struct g_consumer *cp) 230 { 231 232 panic("Function %s() called for %s.", __func__, cp->geom->name); 233 } 234 235 static void 236 g_eli_orphan(struct g_consumer *cp) 237 { 238 struct g_eli_softc *sc; 239 240 g_topology_assert(); 241 sc = cp->geom->softc; 242 if (sc == NULL) 243 return; 244 g_eli_destroy(sc, 1); 245 } 246 247 /* 248 * BIO_READ : G_ELI_START -> g_io_request -> g_eli_read_done -> g_eli_crypto_run -> g_eli_crypto_read_done -> g_io_deliver 249 * BIO_WRITE: G_ELI_START -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver 250 */ 251 static void 252 g_eli_start(struct bio *bp) 253 { 254 struct g_eli_softc *sc; 255 struct g_consumer *cp; 256 struct bio *cbp; 257 258 sc = bp->bio_to->geom->softc; 259 KASSERT(sc != NULL, 260 ("Provider's error should be set (error=%d)(device=%s).", 261 bp->bio_to->error, bp->bio_to->name)); 262 G_ELI_LOGREQ(2, bp, "Request received."); 263 264 switch (bp->bio_cmd) { 265 case BIO_READ: 266 case BIO_WRITE: 267 case BIO_GETATTR: 268 case BIO_FLUSH: 269 break; 270 case BIO_DELETE: 271 /* 272 * We could eventually support BIO_DELETE request. 273 * It could be done by overwritting requested sector with 274 * random data g_eli_overwrites number of times. 275 */ 276 default: 277 g_io_deliver(bp, EOPNOTSUPP); 278 return; 279 } 280 cbp = g_clone_bio(bp); 281 if (cbp == NULL) { 282 g_io_deliver(bp, ENOMEM); 283 return; 284 } 285 switch (bp->bio_cmd) { 286 case BIO_READ: 287 if (!(sc->sc_flags & G_ELI_FLAG_AUTH)) { 288 bp->bio_driver2 = NULL; 289 cbp->bio_done = g_eli_read_done; 290 cp = LIST_FIRST(&sc->sc_geom->consumer); 291 cbp->bio_to = cp->provider; 292 G_ELI_LOGREQ(2, cbp, "Sending request."); 293 /* 294 * Read encrypted data from provider. 295 */ 296 g_io_request(cbp, cp); 297 break; 298 } 299 bp->bio_pflags = 255; 300 /* FALLTHROUGH */ 301 case BIO_WRITE: 302 bp->bio_driver1 = cbp; 303 mtx_lock(&sc->sc_queue_mtx); 304 bioq_insert_tail(&sc->sc_queue, bp); 305 mtx_unlock(&sc->sc_queue_mtx); 306 wakeup(sc); 307 break; 308 case BIO_GETATTR: 309 case BIO_FLUSH: 310 cbp->bio_done = g_std_done; 311 cp = LIST_FIRST(&sc->sc_geom->consumer); 312 cbp->bio_to = cp->provider; 313 G_ELI_LOGREQ(2, cbp, "Sending request."); 314 g_io_request(cbp, cp); 315 break; 316 } 317 } 318 319 /* 320 * This is the main function for kernel worker thread when we don't have 321 * hardware acceleration and we have to do cryptography in software. 322 * Dedicated thread is needed, so we don't slow down g_up/g_down GEOM 323 * threads with crypto work. 324 */ 325 static void 326 g_eli_worker(void *arg) 327 { 328 struct g_eli_softc *sc; 329 struct g_eli_worker *wr; 330 struct bio *bp; 331 332 wr = arg; 333 sc = wr->w_softc; 334 #ifdef SMP 335 /* Before sched_bind() to a CPU, wait for all CPUs to go on-line. */ 336 if (mp_ncpus > 1 && sc->sc_crypto == G_ELI_CRYPTO_SW && 337 g_eli_threads == 0) { 338 while (!smp_started) 339 tsleep(wr, 0, "geli:smp", hz / 4); 340 } 341 #endif 342 thread_lock(curthread); 343 sched_prio(curthread, PUSER); 344 if (sc->sc_crypto == G_ELI_CRYPTO_SW && g_eli_threads == 0) 345 sched_bind(curthread, wr->w_number); 346 thread_unlock(curthread); 347 348 G_ELI_DEBUG(1, "Thread %s started.", curthread->td_proc->p_comm); 349 350 for (;;) { 351 mtx_lock(&sc->sc_queue_mtx); 352 bp = bioq_takefirst(&sc->sc_queue); 353 if (bp == NULL) { 354 if (sc->sc_flags & G_ELI_FLAG_DESTROY) { 355 LIST_REMOVE(wr, w_next); 356 crypto_freesession(wr->w_sid); 357 free(wr, M_ELI); 358 G_ELI_DEBUG(1, "Thread %s exiting.", 359 curthread->td_proc->p_comm); 360 wakeup(&sc->sc_workers); 361 mtx_unlock(&sc->sc_queue_mtx); 362 kproc_exit(0); 363 } 364 msleep(sc, &sc->sc_queue_mtx, PDROP, "geli:w", 0); 365 continue; 366 } 367 mtx_unlock(&sc->sc_queue_mtx); 368 if (bp->bio_cmd == BIO_READ && bp->bio_pflags == 255) 369 g_eli_auth_read(sc, bp); 370 else if (sc->sc_flags & G_ELI_FLAG_AUTH) 371 g_eli_auth_run(wr, bp); 372 else 373 g_eli_crypto_run(wr, bp); 374 } 375 } 376 377 /* 378 * Select encryption key. If G_ELI_FLAG_SINGLE_KEY is present we only have one 379 * key available for all the data. If the flag is not present select the key 380 * based on data offset. 381 */ 382 uint8_t * 383 g_eli_crypto_key(struct g_eli_softc *sc, off_t offset, size_t blocksize) 384 { 385 u_int nkey; 386 387 if (sc->sc_nekeys == 1) 388 return (sc->sc_ekeys[0]); 389 390 KASSERT(sc->sc_nekeys > 1, ("%s: sc_nekeys=%u", __func__, 391 sc->sc_nekeys)); 392 KASSERT((sc->sc_flags & G_ELI_FLAG_SINGLE_KEY) == 0, 393 ("%s: SINGLE_KEY flag set, but sc_nekeys=%u", __func__, 394 sc->sc_nekeys)); 395 396 /* We switch key every 2^G_ELI_KEY_SHIFT blocks. */ 397 nkey = (offset >> G_ELI_KEY_SHIFT) / blocksize; 398 399 KASSERT(nkey < sc->sc_nekeys, ("%s: nkey=%u >= sc_nekeys=%u", __func__, 400 nkey, sc->sc_nekeys)); 401 402 return (sc->sc_ekeys[nkey]); 403 } 404 405 /* 406 * Here we generate IV. It is unique for every sector. 407 */ 408 void 409 g_eli_crypto_ivgen(struct g_eli_softc *sc, off_t offset, u_char *iv, 410 size_t size) 411 { 412 uint8_t off[8]; 413 414 if ((sc->sc_flags & G_ELI_FLAG_NATIVE_BYTE_ORDER) != 0) 415 bcopy(&offset, off, sizeof(off)); 416 else 417 le64enc(off, (uint64_t)offset); 418 419 switch (sc->sc_ealgo) { 420 case CRYPTO_AES_XTS: 421 bcopy(off, iv, sizeof(off)); 422 bzero(iv + sizeof(off), size - sizeof(off)); 423 break; 424 default: 425 { 426 u_char hash[SHA256_DIGEST_LENGTH]; 427 SHA256_CTX ctx; 428 429 /* Copy precalculated SHA256 context for IV-Key. */ 430 bcopy(&sc->sc_ivctx, &ctx, sizeof(ctx)); 431 SHA256_Update(&ctx, off, sizeof(off)); 432 SHA256_Final(hash, &ctx); 433 bcopy(hash, iv, MIN(sizeof(hash), size)); 434 break; 435 } 436 } 437 } 438 439 int 440 g_eli_read_metadata(struct g_class *mp, struct g_provider *pp, 441 struct g_eli_metadata *md) 442 { 443 struct g_geom *gp; 444 struct g_consumer *cp; 445 u_char *buf = NULL; 446 int error; 447 448 g_topology_assert(); 449 450 gp = g_new_geomf(mp, "eli:taste"); 451 gp->start = g_eli_start; 452 gp->access = g_std_access; 453 /* 454 * g_eli_read_metadata() is always called from the event thread. 455 * Our geom is created and destroyed in the same event, so there 456 * could be no orphan nor spoil event in the meantime. 457 */ 458 gp->orphan = g_eli_orphan_spoil_assert; 459 gp->spoiled = g_eli_orphan_spoil_assert; 460 cp = g_new_consumer(gp); 461 error = g_attach(cp, pp); 462 if (error != 0) 463 goto end; 464 error = g_access(cp, 1, 0, 0); 465 if (error != 0) 466 goto end; 467 g_topology_unlock(); 468 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize, 469 &error); 470 g_topology_lock(); 471 if (buf == NULL) 472 goto end; 473 eli_metadata_decode(buf, md); 474 end: 475 if (buf != NULL) 476 g_free(buf); 477 if (cp->provider != NULL) { 478 if (cp->acr == 1) 479 g_access(cp, -1, 0, 0); 480 g_detach(cp); 481 } 482 g_destroy_consumer(cp); 483 g_destroy_geom(gp); 484 return (error); 485 } 486 487 /* 488 * The function is called when we had last close on provider and user requested 489 * to close it when this situation occur. 490 */ 491 static void 492 g_eli_last_close(struct g_eli_softc *sc) 493 { 494 struct g_geom *gp; 495 struct g_provider *pp; 496 char ppname[64]; 497 int error; 498 499 g_topology_assert(); 500 gp = sc->sc_geom; 501 pp = LIST_FIRST(&gp->provider); 502 strlcpy(ppname, pp->name, sizeof(ppname)); 503 error = g_eli_destroy(sc, 1); 504 KASSERT(error == 0, ("Cannot detach %s on last close (error=%d).", 505 ppname, error)); 506 G_ELI_DEBUG(0, "Detached %s on last close.", ppname); 507 } 508 509 int 510 g_eli_access(struct g_provider *pp, int dr, int dw, int de) 511 { 512 struct g_eli_softc *sc; 513 struct g_geom *gp; 514 515 gp = pp->geom; 516 sc = gp->softc; 517 518 if (dw > 0) { 519 if (sc->sc_flags & G_ELI_FLAG_RO) { 520 /* Deny write attempts. */ 521 return (EROFS); 522 } 523 /* Someone is opening us for write, we need to remember that. */ 524 sc->sc_flags |= G_ELI_FLAG_WOPEN; 525 return (0); 526 } 527 /* Is this the last close? */ 528 if (pp->acr + dr > 0 || pp->acw + dw > 0 || pp->ace + de > 0) 529 return (0); 530 531 /* 532 * Automatically detach on last close if requested. 533 */ 534 if ((sc->sc_flags & G_ELI_FLAG_RW_DETACH) || 535 (sc->sc_flags & G_ELI_FLAG_WOPEN)) { 536 g_eli_last_close(sc); 537 } 538 return (0); 539 } 540 541 static int 542 g_eli_cpu_is_disabled(int cpu) 543 { 544 #ifdef SMP 545 return ((hlt_cpus_mask & (1 << cpu)) != 0); 546 #else 547 return (0); 548 #endif 549 } 550 551 struct g_geom * 552 g_eli_create(struct gctl_req *req, struct g_class *mp, struct g_provider *bpp, 553 const struct g_eli_metadata *md, const u_char *mkey, int nkey) 554 { 555 struct g_eli_softc *sc; 556 struct g_eli_worker *wr; 557 struct g_geom *gp; 558 struct g_provider *pp; 559 struct g_consumer *cp; 560 struct cryptoini crie, cria; 561 u_int i, threads; 562 int error; 563 564 G_ELI_DEBUG(1, "Creating device %s%s.", bpp->name, G_ELI_SUFFIX); 565 566 gp = g_new_geomf(mp, "%s%s", bpp->name, G_ELI_SUFFIX); 567 gp->softc = NULL; /* for a moment */ 568 569 sc = malloc(sizeof(*sc), M_ELI, M_WAITOK | M_ZERO); 570 gp->start = g_eli_start; 571 /* 572 * Spoiling cannot happen actually, because we keep provider open for 573 * writing all the time or provider is read-only. 574 */ 575 gp->spoiled = g_eli_orphan_spoil_assert; 576 gp->orphan = g_eli_orphan; 577 gp->dumpconf = g_eli_dumpconf; 578 /* 579 * If detach-on-last-close feature is not enabled and we don't operate 580 * on read-only provider, we can simply use g_std_access(). 581 */ 582 if (md->md_flags & (G_ELI_FLAG_WO_DETACH | G_ELI_FLAG_RO)) 583 gp->access = g_eli_access; 584 else 585 gp->access = g_std_access; 586 587 sc->sc_crypto = G_ELI_CRYPTO_SW; 588 sc->sc_flags = md->md_flags; 589 /* Backward compatibility. */ 590 if (md->md_version < 4) 591 sc->sc_flags |= G_ELI_FLAG_NATIVE_BYTE_ORDER; 592 if (md->md_version < 5) 593 sc->sc_flags |= G_ELI_FLAG_SINGLE_KEY; 594 sc->sc_ealgo = md->md_ealgo; 595 sc->sc_nkey = nkey; 596 597 if (sc->sc_flags & G_ELI_FLAG_AUTH) { 598 sc->sc_akeylen = sizeof(sc->sc_akey) * 8; 599 sc->sc_aalgo = md->md_aalgo; 600 sc->sc_alen = g_eli_hashlen(sc->sc_aalgo); 601 602 sc->sc_data_per_sector = bpp->sectorsize - sc->sc_alen; 603 /* 604 * Some hash functions (like SHA1 and RIPEMD160) generates hash 605 * which length is not multiple of 128 bits, but we want data 606 * length to be multiple of 128, so we can encrypt without 607 * padding. The line below rounds down data length to multiple 608 * of 128 bits. 609 */ 610 sc->sc_data_per_sector -= sc->sc_data_per_sector % 16; 611 612 sc->sc_bytes_per_sector = 613 (md->md_sectorsize - 1) / sc->sc_data_per_sector + 1; 614 sc->sc_bytes_per_sector *= bpp->sectorsize; 615 /* 616 * Precalculate SHA256 for HMAC key generation. 617 * This is expensive operation and we can do it only once now or 618 * for every access to sector, so now will be much better. 619 */ 620 SHA256_Init(&sc->sc_akeyctx); 621 SHA256_Update(&sc->sc_akeyctx, sc->sc_akey, 622 sizeof(sc->sc_akey)); 623 } 624 625 gp->softc = sc; 626 sc->sc_geom = gp; 627 628 bioq_init(&sc->sc_queue); 629 mtx_init(&sc->sc_queue_mtx, "geli:queue", NULL, MTX_DEF); 630 631 pp = NULL; 632 cp = g_new_consumer(gp); 633 error = g_attach(cp, bpp); 634 if (error != 0) { 635 if (req != NULL) { 636 gctl_error(req, "Cannot attach to %s (error=%d).", 637 bpp->name, error); 638 } else { 639 G_ELI_DEBUG(1, "Cannot attach to %s (error=%d).", 640 bpp->name, error); 641 } 642 goto failed; 643 } 644 /* 645 * Keep provider open all the time, so we can run critical tasks, 646 * like Master Keys deletion, without wondering if we can open 647 * provider or not. 648 * We don't open provider for writing only when user requested read-only 649 * access. 650 */ 651 if (sc->sc_flags & G_ELI_FLAG_RO) 652 error = g_access(cp, 1, 0, 1); 653 else 654 error = g_access(cp, 1, 1, 1); 655 if (error != 0) { 656 if (req != NULL) { 657 gctl_error(req, "Cannot access %s (error=%d).", 658 bpp->name, error); 659 } else { 660 G_ELI_DEBUG(1, "Cannot access %s (error=%d).", 661 bpp->name, error); 662 } 663 goto failed; 664 } 665 666 sc->sc_sectorsize = md->md_sectorsize; 667 sc->sc_mediasize = bpp->mediasize; 668 if (!(sc->sc_flags & G_ELI_FLAG_ONETIME)) 669 sc->sc_mediasize -= bpp->sectorsize; 670 if (!(sc->sc_flags & G_ELI_FLAG_AUTH)) 671 sc->sc_mediasize -= (sc->sc_mediasize % sc->sc_sectorsize); 672 else { 673 sc->sc_mediasize /= sc->sc_bytes_per_sector; 674 sc->sc_mediasize *= sc->sc_sectorsize; 675 } 676 677 /* 678 * Remember the keys in our softc structure. 679 */ 680 g_eli_mkey_propagate(sc, mkey); 681 sc->sc_ekeylen = md->md_keylen; 682 683 /* 684 * Precalculate SHA256 for IV generation. 685 * This is expensive operation and we can do it only once now or for 686 * every access to sector, so now will be much better. 687 */ 688 switch (sc->sc_ealgo) { 689 case CRYPTO_AES_XTS: 690 break; 691 default: 692 SHA256_Init(&sc->sc_ivctx); 693 SHA256_Update(&sc->sc_ivctx, sc->sc_ivkey, 694 sizeof(sc->sc_ivkey)); 695 break; 696 } 697 698 LIST_INIT(&sc->sc_workers); 699 700 bzero(&crie, sizeof(crie)); 701 crie.cri_alg = sc->sc_ealgo; 702 crie.cri_klen = sc->sc_ekeylen; 703 if (sc->sc_ealgo == CRYPTO_AES_XTS) 704 crie.cri_klen <<= 1; 705 crie.cri_key = sc->sc_ekeys[0]; 706 if (sc->sc_flags & G_ELI_FLAG_AUTH) { 707 bzero(&cria, sizeof(cria)); 708 cria.cri_alg = sc->sc_aalgo; 709 cria.cri_klen = sc->sc_akeylen; 710 cria.cri_key = sc->sc_akey; 711 crie.cri_next = &cria; 712 } 713 714 threads = g_eli_threads; 715 if (threads == 0) 716 threads = mp_ncpus; 717 else if (threads > mp_ncpus) { 718 /* There is really no need for too many worker threads. */ 719 threads = mp_ncpus; 720 G_ELI_DEBUG(0, "Reducing number of threads to %u.", threads); 721 } 722 for (i = 0; i < threads; i++) { 723 if (g_eli_cpu_is_disabled(i)) { 724 G_ELI_DEBUG(1, "%s: CPU %u disabled, skipping.", 725 bpp->name, i); 726 continue; 727 } 728 wr = malloc(sizeof(*wr), M_ELI, M_WAITOK | M_ZERO); 729 wr->w_softc = sc; 730 wr->w_number = i; 731 732 /* 733 * If this is the first pass, try to get hardware support. 734 * Use software cryptography, if we cannot get it. 735 */ 736 if (LIST_EMPTY(&sc->sc_workers)) { 737 error = crypto_newsession(&wr->w_sid, &crie, 738 CRYPTOCAP_F_HARDWARE); 739 if (error == 0) 740 sc->sc_crypto = G_ELI_CRYPTO_HW; 741 } 742 if (sc->sc_crypto == G_ELI_CRYPTO_SW) { 743 error = crypto_newsession(&wr->w_sid, &crie, 744 CRYPTOCAP_F_SOFTWARE); 745 } 746 if (error != 0) { 747 free(wr, M_ELI); 748 if (req != NULL) { 749 gctl_error(req, "Cannot set up crypto session " 750 "for %s (error=%d).", bpp->name, error); 751 } else { 752 G_ELI_DEBUG(1, "Cannot set up crypto session " 753 "for %s (error=%d).", bpp->name, error); 754 } 755 goto failed; 756 } 757 758 error = kproc_create(g_eli_worker, wr, &wr->w_proc, 0, 0, 759 "g_eli[%u] %s", i, bpp->name); 760 if (error != 0) { 761 crypto_freesession(wr->w_sid); 762 free(wr, M_ELI); 763 if (req != NULL) { 764 gctl_error(req, "Cannot create kernel thread " 765 "for %s (error=%d).", bpp->name, error); 766 } else { 767 G_ELI_DEBUG(1, "Cannot create kernel thread " 768 "for %s (error=%d).", bpp->name, error); 769 } 770 goto failed; 771 } 772 LIST_INSERT_HEAD(&sc->sc_workers, wr, w_next); 773 /* If we have hardware support, one thread is enough. */ 774 if (sc->sc_crypto == G_ELI_CRYPTO_HW) 775 break; 776 } 777 778 /* 779 * Create decrypted provider. 780 */ 781 pp = g_new_providerf(gp, "%s%s", bpp->name, G_ELI_SUFFIX); 782 pp->mediasize = sc->sc_mediasize; 783 pp->sectorsize = sc->sc_sectorsize; 784 785 g_error_provider(pp, 0); 786 787 G_ELI_DEBUG(0, "Device %s created.", pp->name); 788 G_ELI_DEBUG(0, "Encryption: %s %u", g_eli_algo2str(sc->sc_ealgo), 789 sc->sc_ekeylen); 790 if (sc->sc_flags & G_ELI_FLAG_AUTH) 791 G_ELI_DEBUG(0, " Integrity: %s", g_eli_algo2str(sc->sc_aalgo)); 792 G_ELI_DEBUG(0, " Crypto: %s", 793 sc->sc_crypto == G_ELI_CRYPTO_SW ? "software" : "hardware"); 794 return (gp); 795 failed: 796 mtx_lock(&sc->sc_queue_mtx); 797 sc->sc_flags |= G_ELI_FLAG_DESTROY; 798 wakeup(sc); 799 /* 800 * Wait for kernel threads self destruction. 801 */ 802 while (!LIST_EMPTY(&sc->sc_workers)) { 803 msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO, 804 "geli:destroy", 0); 805 } 806 mtx_destroy(&sc->sc_queue_mtx); 807 if (cp->provider != NULL) { 808 if (cp->acr == 1) 809 g_access(cp, -1, -1, -1); 810 g_detach(cp); 811 } 812 g_destroy_consumer(cp); 813 g_destroy_geom(gp); 814 if (sc->sc_ekeys != NULL) { 815 bzero(sc->sc_ekeys, 816 sc->sc_nekeys * (sizeof(uint8_t *) + G_ELI_DATAKEYLEN)); 817 free(sc->sc_ekeys, M_ELI); 818 } 819 bzero(sc, sizeof(*sc)); 820 free(sc, M_ELI); 821 return (NULL); 822 } 823 824 int 825 g_eli_destroy(struct g_eli_softc *sc, boolean_t force) 826 { 827 struct g_geom *gp; 828 struct g_provider *pp; 829 830 g_topology_assert(); 831 832 if (sc == NULL) 833 return (ENXIO); 834 835 gp = sc->sc_geom; 836 pp = LIST_FIRST(&gp->provider); 837 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) { 838 if (force) { 839 G_ELI_DEBUG(1, "Device %s is still open, so it " 840 "cannot be definitely removed.", pp->name); 841 } else { 842 G_ELI_DEBUG(1, 843 "Device %s is still open (r%dw%de%d).", pp->name, 844 pp->acr, pp->acw, pp->ace); 845 return (EBUSY); 846 } 847 } 848 849 mtx_lock(&sc->sc_queue_mtx); 850 sc->sc_flags |= G_ELI_FLAG_DESTROY; 851 wakeup(sc); 852 while (!LIST_EMPTY(&sc->sc_workers)) { 853 msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO, 854 "geli:destroy", 0); 855 } 856 mtx_destroy(&sc->sc_queue_mtx); 857 gp->softc = NULL; 858 bzero(sc->sc_ekeys, 859 sc->sc_nekeys * (sizeof(uint8_t *) + G_ELI_DATAKEYLEN)); 860 free(sc->sc_ekeys, M_ELI); 861 bzero(sc, sizeof(*sc)); 862 free(sc, M_ELI); 863 864 if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)) 865 G_ELI_DEBUG(0, "Device %s destroyed.", gp->name); 866 g_wither_geom_close(gp, ENXIO); 867 868 return (0); 869 } 870 871 static int 872 g_eli_destroy_geom(struct gctl_req *req __unused, 873 struct g_class *mp __unused, struct g_geom *gp) 874 { 875 struct g_eli_softc *sc; 876 877 sc = gp->softc; 878 return (g_eli_destroy(sc, 0)); 879 } 880 881 static int 882 g_eli_keyfiles_load(struct hmac_ctx *ctx, const char *provider) 883 { 884 u_char *keyfile, *data, *size; 885 char *file, name[64]; 886 int i; 887 888 for (i = 0; ; i++) { 889 snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i); 890 keyfile = preload_search_by_type(name); 891 if (keyfile == NULL) 892 return (i); /* Return number of loaded keyfiles. */ 893 data = preload_search_info(keyfile, MODINFO_ADDR); 894 if (data == NULL) { 895 G_ELI_DEBUG(0, "Cannot find key file data for %s.", 896 name); 897 return (0); 898 } 899 data = *(void **)data; 900 size = preload_search_info(keyfile, MODINFO_SIZE); 901 if (size == NULL) { 902 G_ELI_DEBUG(0, "Cannot find key file size for %s.", 903 name); 904 return (0); 905 } 906 file = preload_search_info(keyfile, MODINFO_NAME); 907 if (file == NULL) { 908 G_ELI_DEBUG(0, "Cannot find key file name for %s.", 909 name); 910 return (0); 911 } 912 G_ELI_DEBUG(1, "Loaded keyfile %s for %s (type: %s).", file, 913 provider, name); 914 g_eli_crypto_hmac_update(ctx, data, *(size_t *)size); 915 } 916 } 917 918 static void 919 g_eli_keyfiles_clear(const char *provider) 920 { 921 u_char *keyfile, *data, *size; 922 char name[64]; 923 int i; 924 925 for (i = 0; ; i++) { 926 snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i); 927 keyfile = preload_search_by_type(name); 928 if (keyfile == NULL) 929 return; 930 data = preload_search_info(keyfile, MODINFO_ADDR); 931 size = preload_search_info(keyfile, MODINFO_SIZE); 932 if (data == NULL || size == NULL) 933 continue; 934 data = *(void **)data; 935 bzero(data, *(size_t *)size); 936 } 937 } 938 939 /* 940 * Tasting is only made on boot. 941 * We detect providers which should be attached before root is mounted. 942 */ 943 static struct g_geom * 944 g_eli_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 945 { 946 struct g_eli_metadata md; 947 struct g_geom *gp; 948 struct hmac_ctx ctx; 949 char passphrase[256]; 950 u_char key[G_ELI_USERKEYLEN], mkey[G_ELI_DATAIVKEYLEN]; 951 u_int i, nkey, nkeyfiles, tries; 952 int error; 953 954 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name); 955 g_topology_assert(); 956 957 if (root_mounted() || g_eli_tries == 0) 958 return (NULL); 959 960 G_ELI_DEBUG(3, "Tasting %s.", pp->name); 961 962 error = g_eli_read_metadata(mp, pp, &md); 963 if (error != 0) 964 return (NULL); 965 gp = NULL; 966 967 if (strcmp(md.md_magic, G_ELI_MAGIC) != 0) 968 return (NULL); 969 if (md.md_version > G_ELI_VERSION) { 970 printf("geom_eli.ko module is too old to handle %s.\n", 971 pp->name); 972 return (NULL); 973 } 974 if (md.md_provsize != pp->mediasize) 975 return (NULL); 976 /* Should we attach it on boot? */ 977 if (!(md.md_flags & G_ELI_FLAG_BOOT)) 978 return (NULL); 979 if (md.md_keys == 0x00) { 980 G_ELI_DEBUG(0, "No valid keys on %s.", pp->name); 981 return (NULL); 982 } 983 if (md.md_iterations == -1) { 984 /* If there is no passphrase, we try only once. */ 985 tries = 1; 986 } else { 987 /* Ask for the passphrase no more than g_eli_tries times. */ 988 tries = g_eli_tries; 989 } 990 991 for (i = 0; i < tries; i++) { 992 g_eli_crypto_hmac_init(&ctx, NULL, 0); 993 994 /* 995 * Load all key files. 996 */ 997 nkeyfiles = g_eli_keyfiles_load(&ctx, pp->name); 998 999 if (nkeyfiles == 0 && md.md_iterations == -1) { 1000 /* 1001 * No key files and no passphrase, something is 1002 * definitely wrong here. 1003 * geli(8) doesn't allow for such situation, so assume 1004 * that there was really no passphrase and in that case 1005 * key files are no properly defined in loader.conf. 1006 */ 1007 G_ELI_DEBUG(0, 1008 "Found no key files in loader.conf for %s.", 1009 pp->name); 1010 return (NULL); 1011 } 1012 1013 /* Ask for the passphrase if defined. */ 1014 if (md.md_iterations >= 0) { 1015 printf("Enter passphrase for %s: ", pp->name); 1016 gets(passphrase, sizeof(passphrase), 1017 g_eli_visible_passphrase); 1018 } 1019 1020 /* 1021 * Prepare Derived-Key from the user passphrase. 1022 */ 1023 if (md.md_iterations == 0) { 1024 g_eli_crypto_hmac_update(&ctx, md.md_salt, 1025 sizeof(md.md_salt)); 1026 g_eli_crypto_hmac_update(&ctx, passphrase, 1027 strlen(passphrase)); 1028 bzero(passphrase, sizeof(passphrase)); 1029 } else if (md.md_iterations > 0) { 1030 u_char dkey[G_ELI_USERKEYLEN]; 1031 1032 pkcs5v2_genkey(dkey, sizeof(dkey), md.md_salt, 1033 sizeof(md.md_salt), passphrase, md.md_iterations); 1034 bzero(passphrase, sizeof(passphrase)); 1035 g_eli_crypto_hmac_update(&ctx, dkey, sizeof(dkey)); 1036 bzero(dkey, sizeof(dkey)); 1037 } 1038 1039 g_eli_crypto_hmac_final(&ctx, key, 0); 1040 1041 /* 1042 * Decrypt Master-Key. 1043 */ 1044 error = g_eli_mkey_decrypt(&md, key, mkey, &nkey); 1045 bzero(key, sizeof(key)); 1046 if (error == -1) { 1047 if (i == tries - 1) { 1048 G_ELI_DEBUG(0, 1049 "Wrong key for %s. No tries left.", 1050 pp->name); 1051 g_eli_keyfiles_clear(pp->name); 1052 return (NULL); 1053 } 1054 G_ELI_DEBUG(0, "Wrong key for %s. Tries left: %u.", 1055 pp->name, tries - i - 1); 1056 /* Try again. */ 1057 continue; 1058 } else if (error > 0) { 1059 G_ELI_DEBUG(0, "Cannot decrypt Master Key for %s (error=%d).", 1060 pp->name, error); 1061 g_eli_keyfiles_clear(pp->name); 1062 return (NULL); 1063 } 1064 G_ELI_DEBUG(1, "Using Master Key %u for %s.", nkey, pp->name); 1065 break; 1066 } 1067 1068 /* 1069 * We have correct key, let's attach provider. 1070 */ 1071 gp = g_eli_create(NULL, mp, pp, &md, mkey, nkey); 1072 bzero(mkey, sizeof(mkey)); 1073 bzero(&md, sizeof(md)); 1074 if (gp == NULL) { 1075 G_ELI_DEBUG(0, "Cannot create device %s%s.", pp->name, 1076 G_ELI_SUFFIX); 1077 return (NULL); 1078 } 1079 return (gp); 1080 } 1081 1082 static void 1083 g_eli_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 1084 struct g_consumer *cp, struct g_provider *pp) 1085 { 1086 struct g_eli_softc *sc; 1087 1088 g_topology_assert(); 1089 sc = gp->softc; 1090 if (sc == NULL) 1091 return; 1092 if (pp != NULL || cp != NULL) 1093 return; /* Nothing here. */ 1094 sbuf_printf(sb, "%s<Flags>", indent); 1095 if (sc->sc_flags == 0) 1096 sbuf_printf(sb, "NONE"); 1097 else { 1098 int first = 1; 1099 1100 #define ADD_FLAG(flag, name) do { \ 1101 if (sc->sc_flags & (flag)) { \ 1102 if (!first) \ 1103 sbuf_printf(sb, ", "); \ 1104 else \ 1105 first = 0; \ 1106 sbuf_printf(sb, name); \ 1107 } \ 1108 } while (0) 1109 ADD_FLAG(G_ELI_FLAG_SINGLE_KEY, "SINGLE-KEY"); 1110 ADD_FLAG(G_ELI_FLAG_NATIVE_BYTE_ORDER, "NATIVE-BYTE-ORDER"); 1111 ADD_FLAG(G_ELI_FLAG_ONETIME, "ONETIME"); 1112 ADD_FLAG(G_ELI_FLAG_BOOT, "BOOT"); 1113 ADD_FLAG(G_ELI_FLAG_WO_DETACH, "W-DETACH"); 1114 ADD_FLAG(G_ELI_FLAG_RW_DETACH, "RW-DETACH"); 1115 ADD_FLAG(G_ELI_FLAG_AUTH, "AUTH"); 1116 ADD_FLAG(G_ELI_FLAG_WOPEN, "W-OPEN"); 1117 ADD_FLAG(G_ELI_FLAG_DESTROY, "DESTROY"); 1118 ADD_FLAG(G_ELI_FLAG_RO, "READ-ONLY"); 1119 #undef ADD_FLAG 1120 } 1121 sbuf_printf(sb, "</Flags>\n"); 1122 1123 if (!(sc->sc_flags & G_ELI_FLAG_ONETIME)) { 1124 sbuf_printf(sb, "%s<UsedKey>%u</UsedKey>\n", indent, 1125 sc->sc_nkey); 1126 } 1127 sbuf_printf(sb, "%s<Crypto>", indent); 1128 switch (sc->sc_crypto) { 1129 case G_ELI_CRYPTO_HW: 1130 sbuf_printf(sb, "hardware"); 1131 break; 1132 case G_ELI_CRYPTO_SW: 1133 sbuf_printf(sb, "software"); 1134 break; 1135 default: 1136 sbuf_printf(sb, "UNKNOWN"); 1137 break; 1138 } 1139 sbuf_printf(sb, "</Crypto>\n"); 1140 if (sc->sc_flags & G_ELI_FLAG_AUTH) { 1141 sbuf_printf(sb, 1142 "%s<AuthenticationAlgorithm>%s</AuthenticationAlgorithm>\n", 1143 indent, g_eli_algo2str(sc->sc_aalgo)); 1144 } 1145 sbuf_printf(sb, "%s<KeyLength>%u</KeyLength>\n", indent, 1146 sc->sc_ekeylen); 1147 sbuf_printf(sb, "%s<EncryptionAlgorithm>%s</EncryptionAlgorithm>\n", indent, 1148 g_eli_algo2str(sc->sc_ealgo)); 1149 } 1150 1151 static void 1152 g_eli_shutdown_pre_sync(void *arg, int howto) 1153 { 1154 struct g_class *mp; 1155 struct g_geom *gp, *gp2; 1156 struct g_provider *pp; 1157 struct g_eli_softc *sc; 1158 int error; 1159 1160 mp = arg; 1161 DROP_GIANT(); 1162 g_topology_lock(); 1163 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) { 1164 sc = gp->softc; 1165 if (sc == NULL) 1166 continue; 1167 pp = LIST_FIRST(&gp->provider); 1168 KASSERT(pp != NULL, ("No provider? gp=%p (%s)", gp, gp->name)); 1169 if (pp->acr + pp->acw + pp->ace == 0) 1170 error = g_eli_destroy(sc, 1); 1171 else { 1172 sc->sc_flags |= G_ELI_FLAG_RW_DETACH; 1173 gp->access = g_eli_access; 1174 } 1175 } 1176 g_topology_unlock(); 1177 PICKUP_GIANT(); 1178 } 1179 1180 static void 1181 g_eli_init(struct g_class *mp) 1182 { 1183 1184 g_eli_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync, 1185 g_eli_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST); 1186 if (g_eli_pre_sync == NULL) 1187 G_ELI_DEBUG(0, "Warning! Cannot register shutdown event."); 1188 } 1189 1190 static void 1191 g_eli_fini(struct g_class *mp) 1192 { 1193 1194 if (g_eli_pre_sync != NULL) 1195 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_eli_pre_sync); 1196 } 1197 1198 DECLARE_GEOM_CLASS(g_eli_class, g_eli); 1199 MODULE_DEPEND(g_eli, crypto, 1, 1, 1); 1200