1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2005-2019 Pawel Jakub Dawidek <pawel@dawidek.net>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/cons.h>
32 #include <sys/kenv.h>
33 #include <sys/kernel.h>
34 #include <sys/linker.h>
35 #include <sys/module.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/bio.h>
39 #include <sys/sbuf.h>
40 #include <sys/sysctl.h>
41 #include <sys/malloc.h>
42 #include <sys/eventhandler.h>
43 #include <sys/kthread.h>
44 #include <sys/proc.h>
45 #include <sys/sched.h>
46 #include <sys/smp.h>
47 #include <sys/uio.h>
48 #include <sys/vnode.h>
49
50 #include <machine/vmparam.h>
51
52 #include <vm/uma.h>
53 #include <vm/vm.h>
54 #include <vm/swap_pager.h>
55
56 #include <geom/geom.h>
57 #include <geom/geom_dbg.h>
58 #include <geom/eli/g_eli.h>
59 #include <geom/eli/pkcs5v2.h>
60
61 #include <crypto/intake.h>
62
63 FEATURE(geom_eli, "GEOM crypto module");
64
65 MALLOC_DEFINE(M_ELI, "eli_data", "GEOM_ELI Data");
66
67 SYSCTL_DECL(_kern_geom);
68 SYSCTL_NODE(_kern_geom, OID_AUTO, eli, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
69 "GEOM_ELI stuff");
70 static int g_eli_version = G_ELI_VERSION;
71 SYSCTL_INT(_kern_geom_eli, OID_AUTO, version, CTLFLAG_RD, &g_eli_version, 0,
72 "GELI version");
73 int g_eli_debug = 0;
74 SYSCTL_INT(_kern_geom_eli, OID_AUTO, debug, CTLFLAG_RWTUN, &g_eli_debug, 0,
75 "Debug level");
76 static u_int g_eli_tries = 3;
77 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, tries, CTLFLAG_RWTUN, &g_eli_tries, 0,
78 "Number of tries for entering the passphrase");
79 static u_int g_eli_visible_passphrase = GETS_NOECHO;
80 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, visible_passphrase, CTLFLAG_RWTUN,
81 &g_eli_visible_passphrase, 0,
82 "Visibility of passphrase prompt (0 = invisible, 1 = visible, 2 = asterisk)");
83 u_int g_eli_overwrites = G_ELI_OVERWRITES;
84 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, overwrites, CTLFLAG_RWTUN, &g_eli_overwrites,
85 0, "Number of times on-disk keys should be overwritten when destroying them");
86 static u_int g_eli_threads = 0;
87 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, threads, CTLFLAG_RWTUN, &g_eli_threads, 0,
88 "Number of threads doing crypto work");
89 u_int g_eli_batch = 0;
90 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, batch, CTLFLAG_RWTUN, &g_eli_batch, 0,
91 "Use crypto operations batching");
92 static u_int g_eli_minbufs = 16;
93 static int sysctl_g_eli_minbufs(SYSCTL_HANDLER_ARGS);
94 SYSCTL_PROC(_kern_geom_eli, OID_AUTO, minbufs, CTLTYPE_UINT | CTLFLAG_RW |
95 CTLFLAG_MPSAFE, NULL, 0, sysctl_g_eli_minbufs, "IU",
96 "Number of GELI bufs reserved for swap transactions");
97 static bool g_eli_blocking_malloc = false;
98 SYSCTL_BOOL(_kern_geom_eli, OID_AUTO, blocking_malloc, CTLFLAG_RWTUN,
99 &g_eli_blocking_malloc, 0, "Use blocking malloc calls for GELI buffers");
100 static bool g_eli_unmapped_io = true;
101 SYSCTL_BOOL(_kern_geom_eli, OID_AUTO, unmapped_io, CTLFLAG_RDTUN,
102 &g_eli_unmapped_io, 0, "Enable support for unmapped I/O");
103 static int g_eli_alloc_sz;
104 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, use_uma_bytes, CTLFLAG_RD,
105 &g_eli_alloc_sz, 0, "Use uma(9) for allocations of this size or smaller.");
106
107 static struct sx g_eli_umalock; /* Controls changes to UMA zone. */
108 SX_SYSINIT(g_eli_umalock, &g_eli_umalock, "GELI UMA");
109 static uma_zone_t g_eli_uma = NULL;
110 static volatile int g_eli_umaoutstanding;
111 static volatile int g_eli_devs;
112
113 /*
114 * Control the number of reserved entries in the GELI zone.
115 * If the GELI zone has already been allocated, update the zone. Otherwise,
116 * simply update the variable for use the next time the zone is created.
117 */
118 static int
sysctl_g_eli_minbufs(SYSCTL_HANDLER_ARGS)119 sysctl_g_eli_minbufs(SYSCTL_HANDLER_ARGS)
120 {
121 int error;
122 u_int new;
123
124 new = g_eli_minbufs;
125 error = sysctl_handle_int(oidp, &new, 0, req);
126 if (error != 0 || req->newptr == NULL)
127 return (error);
128 sx_xlock(&g_eli_umalock);
129 if (g_eli_uma != NULL) {
130 if (new != g_eli_minbufs)
131 uma_zone_reserve(g_eli_uma, new);
132 if (new > g_eli_minbufs)
133 uma_prealloc(g_eli_uma, new - g_eli_minbufs);
134 }
135 if (new != g_eli_minbufs)
136 g_eli_minbufs = new;
137 sx_xunlock(&g_eli_umalock);
138 return (0);
139 }
140
141 /*
142 * Passphrase cached during boot, in order to be more user-friendly if
143 * there are multiple providers using the same passphrase.
144 */
145 static char cached_passphrase[256];
146 static u_int g_eli_boot_passcache = 1;
147 TUNABLE_INT("kern.geom.eli.boot_passcache", &g_eli_boot_passcache);
148 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, boot_passcache, CTLFLAG_RD,
149 &g_eli_boot_passcache, 0,
150 "Passphrases are cached during boot process for possible reuse");
151 static void
fetch_loader_passphrase(void * dummy)152 fetch_loader_passphrase(void * dummy)
153 {
154 char * env_passphrase;
155
156 KASSERT(dynamic_kenv, ("need dynamic kenv"));
157
158 if ((env_passphrase = kern_getenv("kern.geom.eli.passphrase")) != NULL) {
159 /* Extract passphrase from the environment. */
160 strlcpy(cached_passphrase, env_passphrase,
161 sizeof(cached_passphrase));
162 freeenv(env_passphrase);
163
164 /* Wipe the passphrase from the environment. */
165 kern_unsetenv("kern.geom.eli.passphrase");
166 }
167 }
168 SYSINIT(geli_fetch_loader_passphrase, SI_SUB_KENV, SI_ORDER_ANY,
169 fetch_loader_passphrase, NULL);
170
171 static void
zero_boot_passcache(void)172 zero_boot_passcache(void)
173 {
174
175 explicit_bzero(cached_passphrase, sizeof(cached_passphrase));
176 }
177
178 static void
zero_geli_intake_keys(void)179 zero_geli_intake_keys(void)
180 {
181 struct keybuf *keybuf;
182 int i;
183
184 if ((keybuf = get_keybuf()) != NULL) {
185 /* Scan the key buffer, clear all GELI keys. */
186 for (i = 0; i < keybuf->kb_nents; i++) {
187 if (keybuf->kb_ents[i].ke_type == KEYBUF_TYPE_GELI) {
188 explicit_bzero(keybuf->kb_ents[i].ke_data,
189 sizeof(keybuf->kb_ents[i].ke_data));
190 keybuf->kb_ents[i].ke_type = KEYBUF_TYPE_NONE;
191 }
192 }
193 }
194 }
195
196 static void
zero_intake_passcache(void * dummy __unused)197 zero_intake_passcache(void *dummy __unused)
198 {
199 zero_boot_passcache();
200 zero_geli_intake_keys();
201 }
202 EVENTHANDLER_DEFINE(mountroot, zero_intake_passcache, NULL, 0);
203
204 static eventhandler_tag g_eli_pre_sync = NULL;
205
206 static int g_eli_read_metadata_offset(struct g_class *mp, struct g_provider *pp,
207 off_t offset, struct g_eli_metadata *md);
208
209 static int g_eli_destroy_geom(struct gctl_req *req, struct g_class *mp,
210 struct g_geom *gp);
211 static void g_eli_init(struct g_class *mp);
212 static void g_eli_fini(struct g_class *mp);
213
214 static g_taste_t g_eli_taste;
215 static g_dumpconf_t g_eli_dumpconf;
216
217 struct g_class g_eli_class = {
218 .name = G_ELI_CLASS_NAME,
219 .version = G_VERSION,
220 .ctlreq = g_eli_config,
221 .taste = g_eli_taste,
222 .destroy_geom = g_eli_destroy_geom,
223 .init = g_eli_init,
224 .fini = g_eli_fini
225 };
226
227 /*
228 * Code paths:
229 * BIO_READ:
230 * g_eli_start -> g_eli_crypto_read -> g_io_request -> g_eli_read_done -> g_eli_crypto_run -> g_eli_crypto_read_done -> g_io_deliver
231 * BIO_WRITE:
232 * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
233 */
234
235 /*
236 * EAGAIN from crypto(9) means, that we were probably balanced to another crypto
237 * accelerator or something like this.
238 * The function updates the SID and rerun the operation.
239 */
240 int
g_eli_crypto_rerun(struct cryptop * crp)241 g_eli_crypto_rerun(struct cryptop *crp)
242 {
243 struct g_eli_softc *sc;
244 struct g_eli_worker *wr;
245 struct bio *bp;
246 int error;
247
248 bp = (struct bio *)crp->crp_opaque;
249 sc = bp->bio_to->geom->softc;
250 LIST_FOREACH(wr, &sc->sc_workers, w_next) {
251 if (wr->w_number == G_ELI_WORKER(bp->bio_pflags))
252 break;
253 }
254 KASSERT(wr != NULL, ("Invalid worker (%u).",
255 G_ELI_WORKER(bp->bio_pflags)));
256 G_ELI_DEBUG(1, "Rerunning crypto %s request (sid: %p -> %p).",
257 bp->bio_cmd == BIO_READ ? "READ" : "WRITE", wr->w_sid,
258 crp->crp_session);
259 wr->w_sid = crp->crp_session;
260 crp->crp_etype = 0;
261 error = crypto_dispatch(crp);
262 if (error == 0)
263 return (0);
264 G_ELI_DEBUG(1, "%s: crypto_dispatch() returned %d.", __func__, error);
265 crp->crp_etype = error;
266 return (error);
267 }
268
269 static void
g_eli_getattr_done(struct bio * bp)270 g_eli_getattr_done(struct bio *bp)
271 {
272 if (bp->bio_error == 0 &&
273 !strcmp(bp->bio_attribute, "GEOM::physpath")) {
274 strlcat(bp->bio_data, "/eli", bp->bio_length);
275 }
276 g_std_done(bp);
277 }
278
279 /*
280 * The function is called afer reading encrypted data from the provider.
281 *
282 * g_eli_start -> g_eli_crypto_read -> g_io_request -> G_ELI_READ_DONE -> g_eli_crypto_run -> g_eli_crypto_read_done -> g_io_deliver
283 */
284 void
g_eli_read_done(struct bio * bp)285 g_eli_read_done(struct bio *bp)
286 {
287 struct g_eli_softc *sc;
288 struct bio *pbp;
289
290 G_ELI_LOGREQ(2, bp, "Request done.");
291 pbp = bp->bio_parent;
292 if (pbp->bio_error == 0 && bp->bio_error != 0)
293 pbp->bio_error = bp->bio_error;
294 g_destroy_bio(bp);
295 /*
296 * Do we have all sectors already?
297 */
298 pbp->bio_inbed++;
299 if (pbp->bio_inbed < pbp->bio_children)
300 return;
301 sc = pbp->bio_to->geom->softc;
302 if (pbp->bio_error != 0) {
303 G_ELI_LOGREQ(0, pbp, "%s() failed (error=%d)", __func__,
304 pbp->bio_error);
305 pbp->bio_completed = 0;
306 g_eli_free_data(pbp);
307 g_io_deliver(pbp, pbp->bio_error);
308 if (sc != NULL)
309 atomic_subtract_int(&sc->sc_inflight, 1);
310 return;
311 }
312 mtx_lock(&sc->sc_queue_mtx);
313 bioq_insert_tail(&sc->sc_queue, pbp);
314 mtx_unlock(&sc->sc_queue_mtx);
315 wakeup(sc);
316 }
317
318 /*
319 * The function is called after we encrypt and write data.
320 *
321 * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> G_ELI_WRITE_DONE -> g_io_deliver
322 */
323 void
g_eli_write_done(struct bio * bp)324 g_eli_write_done(struct bio *bp)
325 {
326 struct g_eli_softc *sc;
327 struct bio *pbp;
328
329 G_ELI_LOGREQ(2, bp, "Request done.");
330 pbp = bp->bio_parent;
331 if (pbp->bio_error == 0 && bp->bio_error != 0)
332 pbp->bio_error = bp->bio_error;
333 g_destroy_bio(bp);
334 /*
335 * Do we have all sectors already?
336 */
337 pbp->bio_inbed++;
338 if (pbp->bio_inbed < pbp->bio_children)
339 return;
340 sc = pbp->bio_to->geom->softc;
341 g_eli_free_data(pbp);
342 if (pbp->bio_error != 0) {
343 G_ELI_LOGREQ(0, pbp, "%s() failed (error=%d)", __func__,
344 pbp->bio_error);
345 pbp->bio_completed = 0;
346 } else
347 pbp->bio_completed = pbp->bio_length;
348
349 /*
350 * Write is finished, send it up.
351 */
352 g_io_deliver(pbp, pbp->bio_error);
353 if (sc != NULL)
354 atomic_subtract_int(&sc->sc_inflight, 1);
355 }
356
357 /*
358 * This function should never be called, but GEOM made as it set ->orphan()
359 * method for every geom.
360 */
361 static void
g_eli_orphan_spoil_assert(struct g_consumer * cp)362 g_eli_orphan_spoil_assert(struct g_consumer *cp)
363 {
364
365 panic("Function %s() called for %s.", __func__, cp->geom->name);
366 }
367
368 static void
g_eli_orphan(struct g_consumer * cp)369 g_eli_orphan(struct g_consumer *cp)
370 {
371 struct g_eli_softc *sc;
372
373 g_topology_assert();
374 sc = cp->geom->softc;
375 if (sc == NULL)
376 return;
377 g_eli_destroy(sc, TRUE);
378 }
379
380 static void
g_eli_resize(struct g_consumer * cp)381 g_eli_resize(struct g_consumer *cp)
382 {
383 struct g_eli_softc *sc;
384 struct g_provider *epp, *pp;
385 off_t oldsize;
386
387 g_topology_assert();
388 sc = cp->geom->softc;
389 if (sc == NULL)
390 return;
391
392 if ((sc->sc_flags & G_ELI_FLAG_AUTORESIZE) == 0) {
393 G_ELI_DEBUG(0, "Autoresize is turned off, old size: %jd.",
394 (intmax_t)sc->sc_provsize);
395 return;
396 }
397
398 pp = cp->provider;
399
400 if ((sc->sc_flags & G_ELI_FLAG_ONETIME) == 0) {
401 struct g_eli_metadata md;
402 u_char *sector;
403 int error;
404
405 sector = NULL;
406
407 error = g_eli_read_metadata_offset(cp->geom->class, pp,
408 sc->sc_provsize - pp->sectorsize, &md);
409 if (error != 0) {
410 G_ELI_DEBUG(0, "Cannot read metadata from %s (error=%d).",
411 pp->name, error);
412 goto iofail;
413 }
414
415 md.md_provsize = pp->mediasize;
416
417 sector = malloc(pp->sectorsize, M_ELI, M_WAITOK | M_ZERO);
418 eli_metadata_encode(&md, sector);
419 error = g_write_data(cp, pp->mediasize - pp->sectorsize, sector,
420 pp->sectorsize);
421 if (error != 0) {
422 G_ELI_DEBUG(0, "Cannot store metadata on %s (error=%d).",
423 pp->name, error);
424 goto iofail;
425 }
426 explicit_bzero(sector, pp->sectorsize);
427 error = g_write_data(cp, sc->sc_provsize - pp->sectorsize,
428 sector, pp->sectorsize);
429 if (error != 0) {
430 G_ELI_DEBUG(0, "Cannot clear old metadata from %s (error=%d).",
431 pp->name, error);
432 goto iofail;
433 }
434 iofail:
435 explicit_bzero(&md, sizeof(md));
436 zfree(sector, M_ELI);
437 }
438
439 oldsize = sc->sc_mediasize;
440 sc->sc_mediasize = eli_mediasize(sc, pp->mediasize, pp->sectorsize);
441 g_eli_key_resize(sc);
442 sc->sc_provsize = pp->mediasize;
443
444 epp = LIST_FIRST(&sc->sc_geom->provider);
445 g_resize_provider(epp, sc->sc_mediasize);
446 G_ELI_DEBUG(0, "Device %s size changed from %jd to %jd.", epp->name,
447 (intmax_t)oldsize, (intmax_t)sc->sc_mediasize);
448 }
449
450 /*
451 * BIO_READ:
452 * G_ELI_START -> g_eli_crypto_read -> g_io_request -> g_eli_read_done -> g_eli_crypto_run -> g_eli_crypto_read_done -> g_io_deliver
453 * BIO_WRITE:
454 * G_ELI_START -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
455 */
456 static void
g_eli_start(struct bio * bp)457 g_eli_start(struct bio *bp)
458 {
459 struct g_eli_softc *sc;
460 struct g_consumer *cp;
461 struct bio *cbp;
462
463 sc = bp->bio_to->geom->softc;
464 KASSERT(sc != NULL,
465 ("Provider's error should be set (error=%d)(device=%s).",
466 bp->bio_to->error, bp->bio_to->name));
467 G_ELI_LOGREQ(2, bp, "Request received.");
468
469 switch (bp->bio_cmd) {
470 case BIO_READ:
471 case BIO_WRITE:
472 case BIO_GETATTR:
473 case BIO_FLUSH:
474 case BIO_ZONE:
475 case BIO_SPEEDUP:
476 break;
477 case BIO_DELETE:
478 /*
479 * If the user hasn't set the NODELETE flag, we just pass
480 * it down the stack and let the layers beneath us do (or
481 * not) whatever they do with it. If they have, we
482 * reject it. A possible extension would be an
483 * additional flag to take it as a hint to shred the data
484 * with [multiple?] overwrites.
485 */
486 if (!(sc->sc_flags & G_ELI_FLAG_NODELETE))
487 break;
488 default:
489 g_io_deliver(bp, EOPNOTSUPP);
490 return;
491 }
492 cbp = g_clone_bio(bp);
493 if (cbp == NULL) {
494 g_io_deliver(bp, ENOMEM);
495 return;
496 }
497 bp->bio_driver1 = cbp;
498 bp->bio_pflags = 0;
499 G_ELI_SET_NEW_BIO(bp->bio_pflags);
500 switch (bp->bio_cmd) {
501 case BIO_READ:
502 if (!(sc->sc_flags & G_ELI_FLAG_AUTH)) {
503 g_eli_crypto_read(sc, bp, 0);
504 break;
505 }
506 /* FALLTHROUGH */
507 case BIO_WRITE:
508 mtx_lock(&sc->sc_queue_mtx);
509 bioq_insert_tail(&sc->sc_queue, bp);
510 mtx_unlock(&sc->sc_queue_mtx);
511 wakeup(sc);
512 break;
513 case BIO_GETATTR:
514 case BIO_FLUSH:
515 case BIO_DELETE:
516 case BIO_SPEEDUP:
517 case BIO_ZONE:
518 if (bp->bio_cmd == BIO_GETATTR)
519 cbp->bio_done = g_eli_getattr_done;
520 else
521 cbp->bio_done = g_std_done;
522 cp = LIST_FIRST(&sc->sc_geom->consumer);
523 cbp->bio_to = cp->provider;
524 G_ELI_LOGREQ(2, cbp, "Sending request.");
525 g_io_request(cbp, cp);
526 break;
527 }
528 }
529
530 static int
g_eli_newsession(struct g_eli_worker * wr)531 g_eli_newsession(struct g_eli_worker *wr)
532 {
533 struct g_eli_softc *sc;
534 struct crypto_session_params csp;
535 uint32_t caps;
536 int error, new_crypto;
537 void *key;
538
539 sc = wr->w_softc;
540
541 memset(&csp, 0, sizeof(csp));
542 csp.csp_mode = CSP_MODE_CIPHER;
543 csp.csp_cipher_alg = sc->sc_ealgo;
544 csp.csp_ivlen = g_eli_ivlen(sc->sc_ealgo);
545 csp.csp_cipher_klen = sc->sc_ekeylen / 8;
546 if (sc->sc_ealgo == CRYPTO_AES_XTS)
547 csp.csp_cipher_klen <<= 1;
548 if ((sc->sc_flags & G_ELI_FLAG_FIRST_KEY) != 0) {
549 key = g_eli_key_hold(sc, 0,
550 LIST_FIRST(&sc->sc_geom->consumer)->provider->sectorsize);
551 csp.csp_cipher_key = key;
552 } else {
553 key = NULL;
554 csp.csp_cipher_key = sc->sc_ekey;
555 }
556 if (sc->sc_flags & G_ELI_FLAG_AUTH) {
557 csp.csp_mode = CSP_MODE_ETA;
558 csp.csp_auth_alg = sc->sc_aalgo;
559 csp.csp_auth_klen = G_ELI_AUTH_SECKEYLEN;
560 }
561
562 switch (sc->sc_crypto) {
563 case G_ELI_CRYPTO_SW_ACCEL:
564 case G_ELI_CRYPTO_SW:
565 error = crypto_newsession(&wr->w_sid, &csp,
566 CRYPTOCAP_F_SOFTWARE);
567 break;
568 case G_ELI_CRYPTO_HW:
569 error = crypto_newsession(&wr->w_sid, &csp,
570 CRYPTOCAP_F_HARDWARE);
571 break;
572 case G_ELI_CRYPTO_UNKNOWN:
573 error = crypto_newsession(&wr->w_sid, &csp,
574 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE);
575 if (error == 0) {
576 caps = crypto_ses2caps(wr->w_sid);
577 if (caps & CRYPTOCAP_F_HARDWARE)
578 new_crypto = G_ELI_CRYPTO_HW;
579 else if (caps & CRYPTOCAP_F_ACCEL_SOFTWARE)
580 new_crypto = G_ELI_CRYPTO_SW_ACCEL;
581 else
582 new_crypto = G_ELI_CRYPTO_SW;
583 mtx_lock(&sc->sc_queue_mtx);
584 if (sc->sc_crypto == G_ELI_CRYPTO_UNKNOWN)
585 sc->sc_crypto = new_crypto;
586 mtx_unlock(&sc->sc_queue_mtx);
587 }
588 break;
589 default:
590 panic("%s: invalid condition", __func__);
591 }
592
593 if ((sc->sc_flags & G_ELI_FLAG_FIRST_KEY) != 0) {
594 if (error)
595 g_eli_key_drop(sc, key);
596 else
597 wr->w_first_key = key;
598 }
599
600 return (error);
601 }
602
603 static void
g_eli_freesession(struct g_eli_worker * wr)604 g_eli_freesession(struct g_eli_worker *wr)
605 {
606 struct g_eli_softc *sc;
607
608 crypto_freesession(wr->w_sid);
609 if (wr->w_first_key != NULL) {
610 sc = wr->w_softc;
611 g_eli_key_drop(sc, wr->w_first_key);
612 wr->w_first_key = NULL;
613 }
614 }
615
616 static void
g_eli_cancel(struct g_eli_softc * sc)617 g_eli_cancel(struct g_eli_softc *sc)
618 {
619 struct bio *bp;
620
621 mtx_assert(&sc->sc_queue_mtx, MA_OWNED);
622
623 while ((bp = bioq_takefirst(&sc->sc_queue)) != NULL) {
624 KASSERT(G_ELI_IS_NEW_BIO(bp->bio_pflags),
625 ("Not new bio when canceling (bp=%p).", bp));
626 g_io_deliver(bp, ENXIO);
627 }
628 }
629
630 static struct bio *
g_eli_takefirst(struct g_eli_softc * sc)631 g_eli_takefirst(struct g_eli_softc *sc)
632 {
633 struct bio *bp;
634
635 mtx_assert(&sc->sc_queue_mtx, MA_OWNED);
636
637 if (!(sc->sc_flags & G_ELI_FLAG_SUSPEND))
638 return (bioq_takefirst(&sc->sc_queue));
639 /*
640 * Device suspended, so we skip new I/O requests.
641 */
642 TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
643 if (!G_ELI_IS_NEW_BIO(bp->bio_pflags))
644 break;
645 }
646 if (bp != NULL)
647 bioq_remove(&sc->sc_queue, bp);
648 return (bp);
649 }
650
651 /*
652 * This is the main function for kernel worker thread when we don't have
653 * hardware acceleration and we have to do cryptography in software.
654 * Dedicated thread is needed, so we don't slow down g_up/g_down GEOM
655 * threads with crypto work.
656 */
657 static void
g_eli_worker(void * arg)658 g_eli_worker(void *arg)
659 {
660 struct g_eli_softc *sc;
661 struct g_eli_worker *wr;
662 struct bio *bp;
663 int error __diagused;
664
665 wr = arg;
666 sc = wr->w_softc;
667 #ifdef EARLY_AP_STARTUP
668 MPASS(!sc->sc_cpubind || smp_started);
669 #elif defined(SMP)
670 /* Before sched_bind() to a CPU, wait for all CPUs to go on-line. */
671 if (sc->sc_cpubind) {
672 while (!smp_started)
673 tsleep(wr, 0, "geli:smp", hz / 4);
674 }
675 #endif
676 thread_lock(curthread);
677 sched_prio(curthread, PUSER);
678 if (sc->sc_cpubind)
679 sched_bind(curthread, wr->w_number % mp_ncpus);
680 thread_unlock(curthread);
681
682 G_ELI_DEBUG(1, "Thread %s started.", curthread->td_proc->p_comm);
683
684 for (;;) {
685 mtx_lock(&sc->sc_queue_mtx);
686 again:
687 bp = g_eli_takefirst(sc);
688 if (bp == NULL) {
689 if (sc->sc_flags & G_ELI_FLAG_DESTROY) {
690 g_eli_cancel(sc);
691 LIST_REMOVE(wr, w_next);
692 g_eli_freesession(wr);
693 free(wr, M_ELI);
694 G_ELI_DEBUG(1, "Thread %s exiting.",
695 curthread->td_proc->p_comm);
696 wakeup(&sc->sc_workers);
697 mtx_unlock(&sc->sc_queue_mtx);
698 kproc_exit(0);
699 }
700 while (sc->sc_flags & G_ELI_FLAG_SUSPEND) {
701 if (sc->sc_inflight > 0) {
702 G_ELI_DEBUG(0, "inflight=%d",
703 sc->sc_inflight);
704 /*
705 * We still have inflight BIOs, so
706 * sleep and retry.
707 */
708 msleep(sc, &sc->sc_queue_mtx, PRIBIO,
709 "geli:inf", hz / 5);
710 goto again;
711 }
712 /*
713 * Suspend requested, mark the worker as
714 * suspended and go to sleep.
715 */
716 if (wr->w_active) {
717 g_eli_freesession(wr);
718 wr->w_active = FALSE;
719 }
720 wakeup(&sc->sc_workers);
721 msleep(sc, &sc->sc_queue_mtx, PRIBIO,
722 "geli:suspend", 0);
723 if (!wr->w_active &&
724 !(sc->sc_flags & G_ELI_FLAG_SUSPEND)) {
725 error = g_eli_newsession(wr);
726 KASSERT(error == 0,
727 ("g_eli_newsession() failed on resume (error=%d)",
728 error));
729 wr->w_active = TRUE;
730 }
731 goto again;
732 }
733 msleep(sc, &sc->sc_queue_mtx, PDROP, "geli:w", 0);
734 continue;
735 }
736 if (G_ELI_IS_NEW_BIO(bp->bio_pflags))
737 atomic_add_int(&sc->sc_inflight, 1);
738 mtx_unlock(&sc->sc_queue_mtx);
739 if (G_ELI_IS_NEW_BIO(bp->bio_pflags)) {
740 G_ELI_SETWORKER(bp->bio_pflags, 0);
741 if (sc->sc_flags & G_ELI_FLAG_AUTH) {
742 if (bp->bio_cmd == BIO_READ)
743 g_eli_auth_read(sc, bp);
744 else
745 g_eli_auth_run(wr, bp);
746 } else {
747 if (bp->bio_cmd == BIO_READ)
748 g_eli_crypto_read(sc, bp, 1);
749 else
750 g_eli_crypto_run(wr, bp);
751 }
752 } else {
753 if (sc->sc_flags & G_ELI_FLAG_AUTH)
754 g_eli_auth_run(wr, bp);
755 else
756 g_eli_crypto_run(wr, bp);
757 }
758 }
759 }
760
761 static int
g_eli_read_metadata_offset(struct g_class * mp,struct g_provider * pp,off_t offset,struct g_eli_metadata * md)762 g_eli_read_metadata_offset(struct g_class *mp, struct g_provider *pp,
763 off_t offset, struct g_eli_metadata *md)
764 {
765 struct g_geom *gp;
766 struct g_consumer *cp;
767 u_char *buf = NULL;
768 int error;
769
770 g_topology_assert();
771
772 gp = g_new_geom(mp, "eli:taste");
773 gp->start = g_eli_start;
774 gp->access = g_std_access;
775 /*
776 * g_eli_read_metadata() is always called from the event thread.
777 * Our geom is created and destroyed in the same event, so there
778 * could be no orphan nor spoil event in the meantime.
779 */
780 gp->orphan = g_eli_orphan_spoil_assert;
781 gp->spoiled = g_eli_orphan_spoil_assert;
782 cp = g_new_consumer(gp);
783 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
784 error = g_attach(cp, pp);
785 if (error != 0)
786 goto end;
787 error = g_access(cp, 1, 0, 0);
788 if (error != 0)
789 goto end;
790 g_topology_unlock();
791 buf = g_read_data(cp, offset, pp->sectorsize, &error);
792 g_topology_lock();
793 if (buf == NULL)
794 goto end;
795 error = eli_metadata_decode(buf, md);
796 if (error != 0)
797 goto end;
798 /* Metadata was read and decoded successfully. */
799 end:
800 g_free(buf);
801 if (cp->provider != NULL) {
802 if (cp->acr == 1)
803 g_access(cp, -1, 0, 0);
804 g_detach(cp);
805 }
806 g_destroy_consumer(cp);
807 g_destroy_geom(gp);
808 return (error);
809 }
810
811 int
g_eli_read_metadata(struct g_class * mp,struct g_provider * pp,struct g_eli_metadata * md)812 g_eli_read_metadata(struct g_class *mp, struct g_provider *pp,
813 struct g_eli_metadata *md)
814 {
815
816 return (g_eli_read_metadata_offset(mp, pp,
817 pp->mediasize - pp->sectorsize, md));
818 }
819
820 /*
821 * The function is called when we had last close on provider and user requested
822 * to close it when this situation occur.
823 */
824 static void
g_eli_last_close(void * arg,int flags __unused)825 g_eli_last_close(void *arg, int flags __unused)
826 {
827 struct g_geom *gp;
828 char gpname[64];
829 int error __diagused;
830
831 g_topology_assert();
832 gp = arg;
833 strlcpy(gpname, gp->name, sizeof(gpname));
834 error = g_eli_destroy(gp->softc, TRUE);
835 KASSERT(error == 0, ("Cannot detach %s on last close (error=%d).",
836 gpname, error));
837 G_ELI_DEBUG(0, "Detached %s on last close.", gpname);
838 }
839
840 int
g_eli_access(struct g_provider * pp,int dr,int dw,int de)841 g_eli_access(struct g_provider *pp, int dr, int dw, int de)
842 {
843 struct g_eli_softc *sc;
844 struct g_geom *gp;
845
846 gp = pp->geom;
847 sc = gp->softc;
848
849 if (dw > 0) {
850 if (sc->sc_flags & G_ELI_FLAG_RO) {
851 /* Deny write attempts. */
852 return (EROFS);
853 }
854 /* Someone is opening us for write, we need to remember that. */
855 sc->sc_flags |= G_ELI_FLAG_WOPEN;
856 return (0);
857 }
858 /* Is this the last close? */
859 if (pp->acr + dr > 0 || pp->acw + dw > 0 || pp->ace + de > 0)
860 return (0);
861
862 /*
863 * Automatically detach on last close if requested.
864 */
865 if ((sc->sc_flags & G_ELI_FLAG_RW_DETACH) ||
866 (sc->sc_flags & G_ELI_FLAG_WOPEN)) {
867 g_post_event(g_eli_last_close, gp, M_WAITOK, NULL);
868 }
869 return (0);
870 }
871
872 static void
g_eli_init_uma(void)873 g_eli_init_uma(void)
874 {
875
876 atomic_add_int(&g_eli_devs, 1);
877 sx_xlock(&g_eli_umalock);
878 if (g_eli_uma == NULL) {
879 /*
880 * Calculate the maximum-sized swap buffer we are
881 * likely to see.
882 */
883 g_eli_alloc_sz = roundup2((PAGE_SIZE + sizeof(int) +
884 G_ELI_AUTH_SECKEYLEN) * nsw_cluster_max +
885 sizeof(uintptr_t), PAGE_SIZE);
886
887 g_eli_uma = uma_zcreate("GELI buffers", g_eli_alloc_sz,
888 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
889
890 /* Reserve and pre-allocate pages, as appropriate. */
891 uma_zone_reserve(g_eli_uma, g_eli_minbufs);
892 uma_prealloc(g_eli_uma, g_eli_minbufs);
893 }
894 sx_xunlock(&g_eli_umalock);
895 }
896
897 /*
898 * Try to destroy the UMA pool. This will do nothing if there are existing
899 * GELI devices or existing UMA allocations.
900 */
901 static void
g_eli_destroy_uma(void)902 g_eli_destroy_uma(void)
903 {
904 uma_zone_t oldzone;
905
906 sx_xlock(&g_eli_umalock);
907 /* Ensure we really should be destroying this. */
908 if (atomic_load_int(&g_eli_devs) == 0 &&
909 atomic_load_int(&g_eli_umaoutstanding) == 0) {
910 oldzone = g_eli_uma;
911 g_eli_uma = NULL;
912 } else
913 oldzone = NULL;
914 sx_xunlock(&g_eli_umalock);
915
916 if (oldzone != NULL)
917 uma_zdestroy(oldzone);
918 }
919
920 static void
g_eli_fini_uma(void)921 g_eli_fini_uma(void)
922 {
923
924 /*
925 * If this is the last outstanding GELI device, try to
926 * destroy the UMA pool.
927 */
928 if (atomic_fetchadd_int(&g_eli_devs, -1) == 1)
929 g_eli_destroy_uma();
930 }
931
932 /*
933 * Allocate a data buffer. If the size fits within our swap-sized buffers,
934 * try to allocate a swap-sized buffer from the UMA pool. Otherwise, fall
935 * back to using malloc.
936 *
937 * Swap-related requests are special: they can only use the UMA pool, they
938 * use M_USE_RESERVE to let them dip farther into system resources, and
939 * they always use M_NOWAIT to prevent swap operations from deadlocking.
940 */
941 bool
g_eli_alloc_data(struct bio * bp,int sz)942 g_eli_alloc_data(struct bio *bp, int sz)
943 {
944
945 KASSERT(sz <= g_eli_alloc_sz || (bp->bio_flags & BIO_SWAP) == 0,
946 ("BIO_SWAP request for %d bytes exceeds the precalculated buffer"
947 " size (%d)", sz, g_eli_alloc_sz));
948 if (sz <= g_eli_alloc_sz) {
949 bp->bio_driver2 = uma_zalloc(g_eli_uma, M_NOWAIT |
950 ((bp->bio_flags & BIO_SWAP) != 0 ? M_USE_RESERVE : 0));
951 if (bp->bio_driver2 != NULL) {
952 bp->bio_pflags |= G_ELI_UMA_ALLOC;
953 atomic_add_int(&g_eli_umaoutstanding, 1);
954 }
955 if (bp->bio_driver2 != NULL || (bp->bio_flags & BIO_SWAP) != 0)
956 return (bp->bio_driver2 != NULL);
957 }
958 bp->bio_pflags &= ~(G_ELI_UMA_ALLOC);
959 bp->bio_driver2 = malloc(sz, M_ELI, g_eli_blocking_malloc ? M_WAITOK :
960 M_NOWAIT);
961 return (bp->bio_driver2 != NULL);
962 }
963
964 /*
965 * Free a buffer from bp->bio_driver2 which was allocated with
966 * g_eli_alloc_data(). This function makes sure that the memory is freed
967 * to the correct place.
968 *
969 * Additionally, if this function frees the last outstanding UMA request
970 * and there are no open GELI devices, this will destroy the UMA pool.
971 */
972 void
g_eli_free_data(struct bio * bp)973 g_eli_free_data(struct bio *bp)
974 {
975
976 /*
977 * Mimic the free(9) behavior of allowing a NULL pointer to be
978 * freed.
979 */
980 if (bp->bio_driver2 == NULL)
981 return;
982
983 if ((bp->bio_pflags & G_ELI_UMA_ALLOC) != 0) {
984 uma_zfree(g_eli_uma, bp->bio_driver2);
985 if (atomic_fetchadd_int(&g_eli_umaoutstanding, -1) == 1 &&
986 atomic_load_int(&g_eli_devs) == 0)
987 g_eli_destroy_uma();
988 } else
989 free(bp->bio_driver2, M_ELI);
990 bp->bio_driver2 = NULL;
991 }
992
993 static int
g_eli_create_worker(struct g_eli_softc * sc,struct gctl_req * req,struct g_provider * bpp,u_int idx)994 g_eli_create_worker(struct g_eli_softc *sc, struct gctl_req *req,
995 struct g_provider *bpp, u_int idx)
996 {
997 struct g_eli_worker *wr;
998 int error;
999
1000 wr = malloc(sizeof(*wr), M_ELI, M_WAITOK | M_ZERO);
1001 wr->w_softc = sc;
1002 wr->w_number = idx;
1003 wr->w_active = TRUE;
1004
1005 error = g_eli_newsession(wr);
1006 if (error != 0) {
1007 free(wr, M_ELI);
1008 if (req != NULL) {
1009 gctl_error(req, "Cannot set up crypto session "
1010 "for %s (error=%d).", bpp->name, error);
1011 } else {
1012 G_ELI_DEBUG(1, "Cannot set up crypto session "
1013 "for %s (error=%d).", bpp->name, error);
1014 }
1015 return (error);
1016 }
1017
1018 error = kproc_create(g_eli_worker, wr, &wr->w_proc, 0, 0,
1019 "g_eli[%u] %s", idx, bpp->name);
1020 if (error != 0) {
1021 g_eli_freesession(wr);
1022 free(wr, M_ELI);
1023 if (req != NULL) {
1024 gctl_error(req, "Cannot create kernel thread "
1025 "for %s (error=%d).", bpp->name, error);
1026 } else {
1027 G_ELI_DEBUG(1, "Cannot create kernel thread "
1028 "for %s (error=%d).", bpp->name, error);
1029 }
1030 return (error);
1031 }
1032
1033 mtx_lock(&sc->sc_queue_mtx);
1034 LIST_INSERT_HEAD(&sc->sc_workers, wr, w_next);
1035 mtx_unlock(&sc->sc_queue_mtx);
1036
1037 return (0);
1038 }
1039
1040 struct g_geom *
g_eli_create(struct gctl_req * req,struct g_class * mp,struct g_provider * bpp,const struct g_eli_metadata * md,const u_char * mkey,int nkey)1041 g_eli_create(struct gctl_req *req, struct g_class *mp, struct g_provider *bpp,
1042 const struct g_eli_metadata *md, const u_char *mkey, int nkey)
1043 {
1044 struct g_eli_softc *sc;
1045 struct g_geom *gp;
1046 struct g_provider *pp;
1047 struct g_consumer *cp;
1048 struct g_geom_alias *gap;
1049 u_int i, nthreads;
1050 int dcw, error;
1051
1052 G_ELI_DEBUG(1, "Creating device %s%s.", bpp->name, G_ELI_SUFFIX);
1053 KASSERT(eli_metadata_crypto_supported(md),
1054 ("%s: unsupported crypto for %s", __func__, bpp->name));
1055
1056 gp = g_new_geomf(mp, "%s%s", bpp->name, G_ELI_SUFFIX);
1057 sc = malloc(sizeof(*sc), M_ELI, M_WAITOK | M_ZERO);
1058 gp->start = g_eli_start;
1059 /*
1060 * Spoiling can happen even though we have the provider open
1061 * exclusively, e.g. through media change events.
1062 */
1063 gp->spoiled = g_eli_orphan;
1064 gp->orphan = g_eli_orphan;
1065 gp->resize = g_eli_resize;
1066 gp->dumpconf = g_eli_dumpconf;
1067 /*
1068 * If detach-on-last-close feature is not enabled and we don't operate
1069 * on read-only provider, we can simply use g_std_access().
1070 */
1071 if (md->md_flags & (G_ELI_FLAG_WO_DETACH | G_ELI_FLAG_RO))
1072 gp->access = g_eli_access;
1073 else
1074 gp->access = g_std_access;
1075
1076 eli_metadata_softc(sc, md, bpp->sectorsize, bpp->mediasize);
1077 sc->sc_nkey = nkey;
1078
1079 gp->softc = sc;
1080 sc->sc_geom = gp;
1081
1082 bioq_init(&sc->sc_queue);
1083 mtx_init(&sc->sc_queue_mtx, "geli:queue", NULL, MTX_DEF);
1084 mtx_init(&sc->sc_ekeys_lock, "geli:ekeys", NULL, MTX_DEF);
1085 g_eli_init_uma();
1086
1087 pp = NULL;
1088 cp = g_new_consumer(gp);
1089 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
1090
1091 error = g_attach(cp, bpp);
1092 if (error != 0) {
1093 if (req != NULL) {
1094 gctl_error(req, "Cannot attach to %s (error=%d).",
1095 bpp->name, error);
1096 } else {
1097 G_ELI_DEBUG(1, "Cannot attach to %s (error=%d).",
1098 bpp->name, error);
1099 }
1100 goto failed;
1101 }
1102 /*
1103 * Keep provider open all the time, so we can run critical tasks,
1104 * like Master Keys deletion, without wondering if we can open
1105 * provider or not.
1106 * We don't open provider for writing only when user requested read-only
1107 * access.
1108 */
1109 dcw = (sc->sc_flags & G_ELI_FLAG_RO) ? 0 : 1;
1110 error = g_access(cp, 1, dcw, 1);
1111 if (error != 0) {
1112 if (req != NULL) {
1113 gctl_error(req, "Cannot access %s (error=%d).",
1114 bpp->name, error);
1115 } else {
1116 G_ELI_DEBUG(1, "Cannot access %s (error=%d).",
1117 bpp->name, error);
1118 }
1119 goto failed;
1120 }
1121
1122 /*
1123 * Remember the keys in our softc structure.
1124 */
1125 g_eli_mkey_propagate(sc, mkey);
1126
1127 LIST_INIT(&sc->sc_workers);
1128
1129 /*
1130 * Create a pool of worker kthreads according to one of two schemes:
1131 *
1132 * 1. CPU-bound: one thread per entry in the CPU map, which
1133 * may be sparsely populated.
1134 *
1135 * 2. kern.geom.eli.threads: A linear pool of threads according to
1136 * the user-tuned value. This may be greater-than or less-than
1137 * mp_ncpus.
1138 */
1139 nthreads = g_eli_threads;
1140 if (nthreads == 0)
1141 nthreads = mp_ncpus;
1142 sc->sc_cpubind = mp_ncpus > 1 && nthreads == mp_ncpus;
1143
1144 if (sc->sc_cpubind) {
1145 CPU_FOREACH(i) {
1146 error = g_eli_create_worker(sc, req, bpp, i);
1147 if (error != 0)
1148 goto failed;
1149 }
1150 } else {
1151 for (i = 0; i < nthreads; i++) {
1152 error = g_eli_create_worker(sc, req, bpp, i);
1153 if (error != 0)
1154 goto failed;
1155 }
1156 }
1157
1158 /*
1159 * Create decrypted provider.
1160 */
1161 pp = g_new_providerf(gp, "%s%s", bpp->name, G_ELI_SUFFIX);
1162 pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
1163 if (g_eli_unmapped_io && CRYPTO_HAS_VMPAGE) {
1164 /*
1165 * On DMAP architectures we can use unmapped I/O. But don't
1166 * use it with data integrity verification. That code hasn't
1167 * been written yet.
1168 */
1169 if ((sc->sc_flags & G_ELI_FLAG_AUTH) == 0)
1170 pp->flags |= G_PF_ACCEPT_UNMAPPED;
1171 }
1172 pp->mediasize = sc->sc_mediasize;
1173 pp->sectorsize = sc->sc_sectorsize;
1174 if ((sc->sc_flags & G_ELI_FLAG_AUTH) == 0 &&
1175 bpp->stripesize > sc->sc_sectorsize &&
1176 bpp->stripesize % sc->sc_sectorsize == 0 &&
1177 bpp->stripeoffset % sc->sc_sectorsize == 0) {
1178 pp->stripesize = bpp->stripesize;
1179 pp->stripeoffset = bpp->stripeoffset;
1180 }
1181 LIST_FOREACH(gap, &bpp->aliases, ga_next)
1182 g_provider_add_alias(pp, "%s%s", gap->ga_alias, G_ELI_SUFFIX);
1183
1184 g_error_provider(pp, 0);
1185
1186 G_ELI_DEBUG(0, "Device %s created.", pp->name);
1187 G_ELI_DEBUG(0, "Encryption: %s %u", g_eli_algo2str(sc->sc_ealgo),
1188 sc->sc_ekeylen);
1189 if (sc->sc_flags & G_ELI_FLAG_AUTH)
1190 G_ELI_DEBUG(0, " Integrity: %s", g_eli_algo2str(sc->sc_aalgo));
1191 G_ELI_DEBUG(0, " Crypto: %s",
1192 sc->sc_crypto == G_ELI_CRYPTO_SW_ACCEL ? "accelerated software" :
1193 sc->sc_crypto == G_ELI_CRYPTO_SW ? "software" : "hardware");
1194 return (gp);
1195
1196 failed:
1197 mtx_lock(&sc->sc_queue_mtx);
1198 sc->sc_flags |= G_ELI_FLAG_DESTROY;
1199 wakeup(sc);
1200 /*
1201 * Wait for kernel threads self destruction.
1202 */
1203 while (!LIST_EMPTY(&sc->sc_workers)) {
1204 msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO,
1205 "geli:destroy", 0);
1206 }
1207 mtx_destroy(&sc->sc_queue_mtx);
1208 if (cp->provider != NULL) {
1209 if (cp->acr == 1)
1210 g_access(cp, -1, -dcw, -1);
1211 g_detach(cp);
1212 }
1213 g_destroy_consumer(cp);
1214 g_destroy_geom(gp);
1215 g_eli_key_destroy(sc);
1216 g_eli_fini_uma();
1217 zfree(sc, M_ELI);
1218 return (NULL);
1219 }
1220
1221 int
g_eli_destroy(struct g_eli_softc * sc,boolean_t force)1222 g_eli_destroy(struct g_eli_softc *sc, boolean_t force)
1223 {
1224 struct g_geom *gp;
1225 struct g_provider *pp;
1226
1227 g_topology_assert();
1228
1229 if (sc == NULL)
1230 return (ENXIO);
1231
1232 gp = sc->sc_geom;
1233 pp = LIST_FIRST(&gp->provider);
1234 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
1235 if (force) {
1236 G_ELI_DEBUG(1, "Device %s is still open, so it "
1237 "cannot be definitely removed.", pp->name);
1238 sc->sc_flags |= G_ELI_FLAG_RW_DETACH;
1239 gp->access = g_eli_access;
1240 g_wither_provider(pp, ENXIO);
1241 return (EBUSY);
1242 } else {
1243 G_ELI_DEBUG(1,
1244 "Device %s is still open (r%dw%de%d).", pp->name,
1245 pp->acr, pp->acw, pp->ace);
1246 return (EBUSY);
1247 }
1248 }
1249
1250 mtx_lock(&sc->sc_queue_mtx);
1251 sc->sc_flags |= G_ELI_FLAG_DESTROY;
1252 wakeup(sc);
1253 while (!LIST_EMPTY(&sc->sc_workers)) {
1254 msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO,
1255 "geli:destroy", 0);
1256 }
1257 mtx_destroy(&sc->sc_queue_mtx);
1258 gp->softc = NULL;
1259 g_eli_key_destroy(sc);
1260 g_eli_fini_uma();
1261 zfree(sc, M_ELI);
1262
1263 G_ELI_DEBUG(0, "Device %s destroyed.", gp->name);
1264 g_wither_geom_close(gp, ENXIO);
1265
1266 return (0);
1267 }
1268
1269 static int
g_eli_destroy_geom(struct gctl_req * req __unused,struct g_class * mp __unused,struct g_geom * gp)1270 g_eli_destroy_geom(struct gctl_req *req __unused,
1271 struct g_class *mp __unused, struct g_geom *gp)
1272 {
1273 struct g_eli_softc *sc;
1274
1275 sc = gp->softc;
1276 return (g_eli_destroy(sc, FALSE));
1277 }
1278
1279 static int
g_eli_keyfiles_load(struct hmac_ctx * ctx,const char * provider)1280 g_eli_keyfiles_load(struct hmac_ctx *ctx, const char *provider)
1281 {
1282 u_char *keyfile, *data;
1283 char *file, name[64];
1284 size_t size;
1285 int i;
1286
1287 for (i = 0; ; i++) {
1288 snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i);
1289 keyfile = preload_search_by_type(name);
1290 if (keyfile == NULL && i == 0) {
1291 /*
1292 * If there is only one keyfile, allow simpler name.
1293 */
1294 snprintf(name, sizeof(name), "%s:geli_keyfile", provider);
1295 keyfile = preload_search_by_type(name);
1296 }
1297 if (keyfile == NULL)
1298 return (i); /* Return number of loaded keyfiles. */
1299 data = preload_fetch_addr(keyfile);
1300 if (data == NULL) {
1301 G_ELI_DEBUG(0, "Cannot find key file data for %s.",
1302 name);
1303 return (0);
1304 }
1305 size = preload_fetch_size(keyfile);
1306 if (size == 0) {
1307 G_ELI_DEBUG(0, "Cannot find key file size for %s.",
1308 name);
1309 return (0);
1310 }
1311 file = preload_search_info(keyfile, MODINFO_NAME);
1312 if (file == NULL) {
1313 G_ELI_DEBUG(0, "Cannot find key file name for %s.",
1314 name);
1315 return (0);
1316 }
1317 G_ELI_DEBUG(1, "Loaded keyfile %s for %s (type: %s).", file,
1318 provider, name);
1319 g_eli_crypto_hmac_update(ctx, data, size);
1320 }
1321 }
1322
1323 static void
g_eli_keyfiles_clear(const char * provider)1324 g_eli_keyfiles_clear(const char *provider)
1325 {
1326 u_char *keyfile, *data;
1327 char name[64];
1328 size_t size;
1329 int i;
1330
1331 for (i = 0; ; i++) {
1332 snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i);
1333 keyfile = preload_search_by_type(name);
1334 if (keyfile == NULL)
1335 return;
1336 data = preload_fetch_addr(keyfile);
1337 size = preload_fetch_size(keyfile);
1338 if (data != NULL && size != 0)
1339 explicit_bzero(data, size);
1340 }
1341 }
1342
1343 /*
1344 * Tasting is only made on boot.
1345 * We detect providers which should be attached before root is mounted.
1346 */
1347 static struct g_geom *
g_eli_taste(struct g_class * mp,struct g_provider * pp,int flags __unused)1348 g_eli_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
1349 {
1350 struct g_eli_metadata md;
1351 struct g_geom *gp;
1352 struct hmac_ctx ctx;
1353 char passphrase[256];
1354 u_char key[G_ELI_USERKEYLEN], mkey[G_ELI_DATAIVKEYLEN];
1355 u_int i, nkey, nkeyfiles, tries, showpass;
1356 int error;
1357 struct keybuf *keybuf;
1358
1359 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
1360 g_topology_assert();
1361
1362 if (root_mounted() || g_eli_tries == 0)
1363 return (NULL);
1364
1365 G_ELI_DEBUG(3, "Tasting %s.", pp->name);
1366
1367 error = g_eli_read_metadata(mp, pp, &md);
1368 if (error != 0)
1369 return (NULL);
1370 gp = NULL;
1371
1372 if (strcmp(md.md_magic, G_ELI_MAGIC) != 0)
1373 return (NULL);
1374 if (md.md_version > G_ELI_VERSION) {
1375 printf("geom_eli.ko module is too old to handle %s.\n",
1376 pp->name);
1377 return (NULL);
1378 }
1379 if (md.md_provsize != pp->mediasize)
1380 return (NULL);
1381 /* Should we attach it on boot? */
1382 if (!(md.md_flags & G_ELI_FLAG_BOOT) &&
1383 !(md.md_flags & G_ELI_FLAG_GELIBOOT))
1384 return (NULL);
1385 if (md.md_keys == 0x00) {
1386 G_ELI_DEBUG(0, "No valid keys on %s.", pp->name);
1387 return (NULL);
1388 }
1389 if (!eli_metadata_crypto_supported(&md)) {
1390 G_ELI_DEBUG(0, "%s uses invalid or unsupported algorithms\n",
1391 pp->name);
1392 return (NULL);
1393 }
1394 if (md.md_iterations == -1) {
1395 /* If there is no passphrase, we try only once. */
1396 tries = 1;
1397 } else {
1398 /* Ask for the passphrase no more than g_eli_tries times. */
1399 tries = g_eli_tries;
1400 }
1401
1402 if ((keybuf = get_keybuf()) != NULL) {
1403 /* Scan the key buffer, try all GELI keys. */
1404 for (i = 0; i < keybuf->kb_nents; i++) {
1405 if (keybuf->kb_ents[i].ke_type == KEYBUF_TYPE_GELI) {
1406 memcpy(key, keybuf->kb_ents[i].ke_data,
1407 sizeof(key));
1408
1409 if (g_eli_mkey_decrypt_any(&md, key,
1410 mkey, &nkey) == 0 ) {
1411 explicit_bzero(key, sizeof(key));
1412 goto have_key;
1413 }
1414 }
1415 }
1416 }
1417
1418 for (i = 0; i <= tries; i++) {
1419 g_eli_crypto_hmac_init(&ctx, NULL, 0);
1420
1421 /*
1422 * Load all key files.
1423 */
1424 nkeyfiles = g_eli_keyfiles_load(&ctx, pp->name);
1425
1426 if (nkeyfiles == 0 && md.md_iterations == -1) {
1427 /*
1428 * No key files and no passphrase, something is
1429 * definitely wrong here.
1430 * geli(8) doesn't allow for such situation, so assume
1431 * that there was really no passphrase and in that case
1432 * key files are no properly defined in loader.conf.
1433 */
1434 G_ELI_DEBUG(0,
1435 "Found no key files in loader.conf for %s.",
1436 pp->name);
1437 return (NULL);
1438 }
1439
1440 /* Ask for the passphrase if defined. */
1441 if (md.md_iterations >= 0) {
1442 /* Try first with cached passphrase. */
1443 if (i == 0) {
1444 if (!g_eli_boot_passcache)
1445 continue;
1446 memcpy(passphrase, cached_passphrase,
1447 sizeof(passphrase));
1448 } else {
1449 printf("Enter passphrase for %s: ", pp->name);
1450 showpass = g_eli_visible_passphrase;
1451 if ((md.md_flags & G_ELI_FLAG_GELIDISPLAYPASS) != 0)
1452 showpass = GETS_ECHOPASS;
1453 cngets(passphrase, sizeof(passphrase),
1454 showpass);
1455 memcpy(cached_passphrase, passphrase,
1456 sizeof(passphrase));
1457 }
1458 }
1459
1460 /*
1461 * Prepare Derived-Key from the user passphrase.
1462 */
1463 if (md.md_iterations == 0) {
1464 g_eli_crypto_hmac_update(&ctx, md.md_salt,
1465 sizeof(md.md_salt));
1466 g_eli_crypto_hmac_update(&ctx, passphrase,
1467 strlen(passphrase));
1468 explicit_bzero(passphrase, sizeof(passphrase));
1469 } else if (md.md_iterations > 0) {
1470 u_char dkey[G_ELI_USERKEYLEN];
1471
1472 pkcs5v2_genkey(dkey, sizeof(dkey), md.md_salt,
1473 sizeof(md.md_salt), passphrase, md.md_iterations);
1474 explicit_bzero(passphrase, sizeof(passphrase));
1475 g_eli_crypto_hmac_update(&ctx, dkey, sizeof(dkey));
1476 explicit_bzero(dkey, sizeof(dkey));
1477 }
1478
1479 g_eli_crypto_hmac_final(&ctx, key, 0);
1480
1481 /*
1482 * Decrypt Master-Key.
1483 */
1484 error = g_eli_mkey_decrypt_any(&md, key, mkey, &nkey);
1485 explicit_bzero(key, sizeof(key));
1486 if (error == -1) {
1487 if (i == tries) {
1488 G_ELI_DEBUG(0,
1489 "Wrong key for %s. No tries left.",
1490 pp->name);
1491 g_eli_keyfiles_clear(pp->name);
1492 return (NULL);
1493 }
1494 if (i > 0) {
1495 G_ELI_DEBUG(0,
1496 "Wrong key for %s. Tries left: %u.",
1497 pp->name, tries - i);
1498 }
1499 /* Try again. */
1500 continue;
1501 } else if (error > 0) {
1502 G_ELI_DEBUG(0,
1503 "Cannot decrypt Master Key for %s (error=%d).",
1504 pp->name, error);
1505 g_eli_keyfiles_clear(pp->name);
1506 return (NULL);
1507 }
1508 g_eli_keyfiles_clear(pp->name);
1509 G_ELI_DEBUG(1, "Using Master Key %u for %s.", nkey, pp->name);
1510 break;
1511 }
1512 have_key:
1513
1514 /*
1515 * We have correct key, let's attach provider.
1516 */
1517 gp = g_eli_create(NULL, mp, pp, &md, mkey, nkey);
1518 explicit_bzero(mkey, sizeof(mkey));
1519 explicit_bzero(&md, sizeof(md));
1520 if (gp == NULL) {
1521 G_ELI_DEBUG(0, "Cannot create device %s%s.", pp->name,
1522 G_ELI_SUFFIX);
1523 return (NULL);
1524 }
1525 return (gp);
1526 }
1527
1528 static void
g_eli_dumpconf(struct sbuf * sb,const char * indent,struct g_geom * gp,struct g_consumer * cp,struct g_provider * pp)1529 g_eli_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1530 struct g_consumer *cp, struct g_provider *pp)
1531 {
1532 struct g_eli_softc *sc;
1533
1534 g_topology_assert();
1535 sc = gp->softc;
1536 if (sc == NULL)
1537 return;
1538 if (pp != NULL || cp != NULL)
1539 return; /* Nothing here. */
1540
1541 sbuf_printf(sb, "%s<KeysTotal>%ju</KeysTotal>\n", indent,
1542 (uintmax_t)sc->sc_ekeys_total);
1543 sbuf_printf(sb, "%s<KeysAllocated>%ju</KeysAllocated>\n", indent,
1544 (uintmax_t)sc->sc_ekeys_allocated);
1545 sbuf_printf(sb, "%s<Flags>", indent);
1546 if (sc->sc_flags == 0)
1547 sbuf_cat(sb, "NONE");
1548 else {
1549 int first = 1;
1550
1551 #define ADD_FLAG(flag, name) do { \
1552 if (sc->sc_flags & (flag)) { \
1553 if (!first) \
1554 sbuf_cat(sb, ", "); \
1555 else \
1556 first = 0; \
1557 sbuf_cat(sb, name); \
1558 } \
1559 } while (0)
1560 ADD_FLAG(G_ELI_FLAG_SUSPEND, "SUSPEND");
1561 ADD_FLAG(G_ELI_FLAG_SINGLE_KEY, "SINGLE-KEY");
1562 ADD_FLAG(G_ELI_FLAG_NATIVE_BYTE_ORDER, "NATIVE-BYTE-ORDER");
1563 ADD_FLAG(G_ELI_FLAG_ONETIME, "ONETIME");
1564 ADD_FLAG(G_ELI_FLAG_BOOT, "BOOT");
1565 ADD_FLAG(G_ELI_FLAG_WO_DETACH, "W-DETACH");
1566 ADD_FLAG(G_ELI_FLAG_RW_DETACH, "RW-DETACH");
1567 ADD_FLAG(G_ELI_FLAG_AUTH, "AUTH");
1568 ADD_FLAG(G_ELI_FLAG_WOPEN, "W-OPEN");
1569 ADD_FLAG(G_ELI_FLAG_DESTROY, "DESTROY");
1570 ADD_FLAG(G_ELI_FLAG_RO, "READ-ONLY");
1571 ADD_FLAG(G_ELI_FLAG_NODELETE, "NODELETE");
1572 ADD_FLAG(G_ELI_FLAG_GELIBOOT, "GELIBOOT");
1573 ADD_FLAG(G_ELI_FLAG_GELIDISPLAYPASS, "GELIDISPLAYPASS");
1574 ADD_FLAG(G_ELI_FLAG_AUTORESIZE, "AUTORESIZE");
1575 #undef ADD_FLAG
1576 }
1577 sbuf_cat(sb, "</Flags>\n");
1578
1579 if (!(sc->sc_flags & G_ELI_FLAG_ONETIME)) {
1580 sbuf_printf(sb, "%s<UsedKey>%u</UsedKey>\n", indent,
1581 sc->sc_nkey);
1582 }
1583 sbuf_printf(sb, "%s<Version>%u</Version>\n", indent, sc->sc_version);
1584 sbuf_printf(sb, "%s<Crypto>", indent);
1585 switch (sc->sc_crypto) {
1586 case G_ELI_CRYPTO_HW:
1587 sbuf_cat(sb, "hardware");
1588 break;
1589 case G_ELI_CRYPTO_SW:
1590 sbuf_cat(sb, "software");
1591 break;
1592 case G_ELI_CRYPTO_SW_ACCEL:
1593 sbuf_cat(sb, "accelerated software");
1594 break;
1595 default:
1596 sbuf_cat(sb, "UNKNOWN");
1597 break;
1598 }
1599 sbuf_cat(sb, "</Crypto>\n");
1600 if (sc->sc_flags & G_ELI_FLAG_AUTH) {
1601 sbuf_printf(sb,
1602 "%s<AuthenticationAlgorithm>%s</AuthenticationAlgorithm>\n",
1603 indent, g_eli_algo2str(sc->sc_aalgo));
1604 }
1605 sbuf_printf(sb, "%s<KeyLength>%u</KeyLength>\n", indent,
1606 sc->sc_ekeylen);
1607 sbuf_printf(sb, "%s<EncryptionAlgorithm>%s</EncryptionAlgorithm>\n",
1608 indent, g_eli_algo2str(sc->sc_ealgo));
1609 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
1610 (sc->sc_flags & G_ELI_FLAG_SUSPEND) ? "SUSPENDED" : "ACTIVE");
1611 }
1612
1613 static void
g_eli_shutdown_pre_sync(void * arg,int howto)1614 g_eli_shutdown_pre_sync(void *arg, int howto)
1615 {
1616 struct g_class *mp;
1617 struct g_geom *gp, *gp2;
1618 struct g_provider *pp;
1619 struct g_eli_softc *sc;
1620
1621 mp = arg;
1622 g_topology_lock();
1623 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
1624 sc = gp->softc;
1625 if (sc == NULL)
1626 continue;
1627 pp = LIST_FIRST(&gp->provider);
1628 KASSERT(pp != NULL, ("No provider? gp=%p (%s)", gp, gp->name));
1629 if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0 ||
1630 SCHEDULER_STOPPED())
1631 {
1632 sc->sc_flags |= G_ELI_FLAG_RW_DETACH;
1633 gp->access = g_eli_access;
1634 } else {
1635 (void) g_eli_destroy(sc, TRUE);
1636 }
1637 }
1638 g_topology_unlock();
1639 }
1640
1641 static void
g_eli_init(struct g_class * mp)1642 g_eli_init(struct g_class *mp)
1643 {
1644
1645 g_eli_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync,
1646 g_eli_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST);
1647 if (g_eli_pre_sync == NULL)
1648 G_ELI_DEBUG(0, "Warning! Cannot register shutdown event.");
1649 }
1650
1651 static void
g_eli_fini(struct g_class * mp)1652 g_eli_fini(struct g_class *mp)
1653 {
1654
1655 if (g_eli_pre_sync != NULL)
1656 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_eli_pre_sync);
1657 }
1658
1659 DECLARE_GEOM_CLASS(g_eli_class, g_eli);
1660 MODULE_DEPEND(g_eli, crypto, 1, 1, 1);
1661 MODULE_VERSION(geom_eli, 0);
1662