xref: /freebsd/sys/geom/eli/g_eli_privacy.c (revision eb69d1f144a6fcc765d1b9d44a5ae8082353e70b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2005-2011 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/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/linker.h>
36 #include <sys/module.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 #include <sys/bio.h>
40 #include <sys/sysctl.h>
41 #include <sys/malloc.h>
42 #include <sys/kthread.h>
43 #include <sys/proc.h>
44 #include <sys/sched.h>
45 #include <sys/smp.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  * Code paths:
56  * BIO_READ:
57  *	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
58  * BIO_WRITE:
59  *	g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
60  */
61 
62 MALLOC_DECLARE(M_ELI);
63 
64 /*
65  * The function is called after we read and decrypt data.
66  *
67  * 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
68  */
69 static int
70 g_eli_crypto_read_done(struct cryptop *crp)
71 {
72 	struct g_eli_softc *sc;
73 	struct bio *bp;
74 
75 	if (crp->crp_etype == EAGAIN) {
76 		if (g_eli_crypto_rerun(crp) == 0)
77 			return (0);
78 	}
79 	bp = (struct bio *)crp->crp_opaque;
80 	bp->bio_inbed++;
81 	if (crp->crp_etype == 0) {
82 		G_ELI_DEBUG(3, "Crypto READ request done (%d/%d).",
83 		    bp->bio_inbed, bp->bio_children);
84 		bp->bio_completed += crp->crp_olen;
85 	} else {
86 		G_ELI_DEBUG(1, "Crypto READ request failed (%d/%d) error=%d.",
87 		    bp->bio_inbed, bp->bio_children, crp->crp_etype);
88 		if (bp->bio_error == 0)
89 			bp->bio_error = crp->crp_etype;
90 	}
91 	sc = bp->bio_to->geom->softc;
92 	g_eli_key_drop(sc, crp->crp_desc->crd_key);
93 	/*
94 	 * Do we have all sectors already?
95 	 */
96 	if (bp->bio_inbed < bp->bio_children)
97 		return (0);
98 	free(bp->bio_driver2, M_ELI);
99 	bp->bio_driver2 = NULL;
100 	if (bp->bio_error != 0) {
101 		G_ELI_LOGREQ(0, bp, "Crypto READ request failed (error=%d).",
102 		    bp->bio_error);
103 		bp->bio_completed = 0;
104 	}
105 	/*
106 	 * Read is finished, send it up.
107 	 */
108 	g_io_deliver(bp, bp->bio_error);
109 	atomic_subtract_int(&sc->sc_inflight, 1);
110 	return (0);
111 }
112 
113 /*
114  * The function is called after data encryption.
115  *
116  * g_eli_start -> g_eli_crypto_run -> G_ELI_CRYPTO_WRITE_DONE -> g_io_request -> g_eli_write_done -> g_io_deliver
117  */
118 static int
119 g_eli_crypto_write_done(struct cryptop *crp)
120 {
121 	struct g_eli_softc *sc;
122 	struct g_geom *gp;
123 	struct g_consumer *cp;
124 	struct bio *bp, *cbp;
125 
126 	if (crp->crp_etype == EAGAIN) {
127 		if (g_eli_crypto_rerun(crp) == 0)
128 			return (0);
129 	}
130 	bp = (struct bio *)crp->crp_opaque;
131 	bp->bio_inbed++;
132 	if (crp->crp_etype == 0) {
133 		G_ELI_DEBUG(3, "Crypto WRITE request done (%d/%d).",
134 		    bp->bio_inbed, bp->bio_children);
135 	} else {
136 		G_ELI_DEBUG(1, "Crypto WRITE request failed (%d/%d) error=%d.",
137 		    bp->bio_inbed, bp->bio_children, crp->crp_etype);
138 		if (bp->bio_error == 0)
139 			bp->bio_error = crp->crp_etype;
140 	}
141 	gp = bp->bio_to->geom;
142 	sc = gp->softc;
143 	g_eli_key_drop(sc, crp->crp_desc->crd_key);
144 	/*
145 	 * All sectors are already encrypted?
146 	 */
147 	if (bp->bio_inbed < bp->bio_children)
148 		return (0);
149 	bp->bio_inbed = 0;
150 	bp->bio_children = 1;
151 	cbp = bp->bio_driver1;
152 	bp->bio_driver1 = NULL;
153 	if (bp->bio_error != 0) {
154 		G_ELI_LOGREQ(0, bp, "Crypto WRITE request failed (error=%d).",
155 		    bp->bio_error);
156 		free(bp->bio_driver2, M_ELI);
157 		bp->bio_driver2 = NULL;
158 		g_destroy_bio(cbp);
159 		g_io_deliver(bp, bp->bio_error);
160 		atomic_subtract_int(&sc->sc_inflight, 1);
161 		return (0);
162 	}
163 	cbp->bio_data = bp->bio_driver2;
164 	cbp->bio_done = g_eli_write_done;
165 	cp = LIST_FIRST(&gp->consumer);
166 	cbp->bio_to = cp->provider;
167 	G_ELI_LOGREQ(2, cbp, "Sending request.");
168 	/*
169 	 * Send encrypted data to the provider.
170 	 */
171 	g_io_request(cbp, cp);
172 	return (0);
173 }
174 
175 /*
176  * The function is called to read encrypted data.
177  *
178  * 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
179  */
180 void
181 g_eli_crypto_read(struct g_eli_softc *sc, struct bio *bp, boolean_t fromworker)
182 {
183 	struct g_consumer *cp;
184 	struct bio *cbp;
185 
186 	if (!fromworker) {
187 		/*
188 		 * We are not called from the worker thread, so check if
189 		 * device is suspended.
190 		 */
191 		mtx_lock(&sc->sc_queue_mtx);
192 		if (sc->sc_flags & G_ELI_FLAG_SUSPEND) {
193 			/*
194 			 * If device is suspended, we place the request onto
195 			 * the queue, so it can be handled after resume.
196 			 */
197 			G_ELI_DEBUG(0, "device suspended, move onto queue");
198 			bioq_insert_tail(&sc->sc_queue, bp);
199 			mtx_unlock(&sc->sc_queue_mtx);
200 			wakeup(sc);
201 			return;
202 		}
203 		atomic_add_int(&sc->sc_inflight, 1);
204 		mtx_unlock(&sc->sc_queue_mtx);
205 	}
206 	bp->bio_pflags = 0;
207 	bp->bio_driver2 = NULL;
208 	cbp = bp->bio_driver1;
209 	cbp->bio_done = g_eli_read_done;
210 	cp = LIST_FIRST(&sc->sc_geom->consumer);
211 	cbp->bio_to = cp->provider;
212 	G_ELI_LOGREQ(2, cbp, "Sending request.");
213 	/*
214 	 * Read encrypted data from provider.
215 	 */
216 	g_io_request(cbp, cp);
217 }
218 
219 /*
220  * This is the main function responsible for cryptography (ie. communication
221  * with crypto(9) subsystem).
222  *
223  * BIO_READ:
224  *	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
225  * BIO_WRITE:
226  *	g_eli_start -> G_ELI_CRYPTO_RUN -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
227  */
228 void
229 g_eli_crypto_run(struct g_eli_worker *wr, struct bio *bp)
230 {
231 	struct g_eli_softc *sc;
232 	struct cryptop *crp;
233 	struct cryptodesc *crd;
234 	u_int i, nsec, secsize;
235 	off_t dstoff;
236 	size_t size;
237 	u_char *p, *data;
238 	int error;
239 
240 	G_ELI_LOGREQ(3, bp, "%s", __func__);
241 
242 	bp->bio_pflags = wr->w_number;
243 	sc = wr->w_softc;
244 	secsize = LIST_FIRST(&sc->sc_geom->provider)->sectorsize;
245 	nsec = bp->bio_length / secsize;
246 
247 	/*
248 	 * Calculate how much memory do we need.
249 	 * We need separate crypto operation for every single sector.
250 	 * It is much faster to calculate total amount of needed memory here and
251 	 * do the allocation once instead of allocating memory in pieces (many,
252 	 * many pieces).
253 	 */
254 	size = sizeof(*crp) * nsec;
255 	size += sizeof(*crd) * nsec;
256 	/*
257 	 * If we write the data we cannot destroy current bio_data content,
258 	 * so we need to allocate more memory for encrypted data.
259 	 */
260 	if (bp->bio_cmd == BIO_WRITE)
261 		size += bp->bio_length;
262 	p = malloc(size, M_ELI, M_WAITOK);
263 
264 	bp->bio_inbed = 0;
265 	bp->bio_children = nsec;
266 	bp->bio_driver2 = p;
267 
268 	if (bp->bio_cmd == BIO_READ)
269 		data = bp->bio_data;
270 	else {
271 		data = p;
272 		p += bp->bio_length;
273 		bcopy(bp->bio_data, data, bp->bio_length);
274 	}
275 
276 	for (i = 0, dstoff = bp->bio_offset; i < nsec; i++, dstoff += secsize) {
277 		crp = (struct cryptop *)p;	p += sizeof(*crp);
278 		crd = (struct cryptodesc *)p;	p += sizeof(*crd);
279 
280 		crp->crp_sid = wr->w_sid;
281 		crp->crp_ilen = secsize;
282 		crp->crp_olen = secsize;
283 		crp->crp_opaque = (void *)bp;
284 		crp->crp_buf = (void *)data;
285 		data += secsize;
286 		if (bp->bio_cmd == BIO_WRITE)
287 			crp->crp_callback = g_eli_crypto_write_done;
288 		else /* if (bp->bio_cmd == BIO_READ) */
289 			crp->crp_callback = g_eli_crypto_read_done;
290 		crp->crp_flags = CRYPTO_F_CBIFSYNC;
291 		if (g_eli_batch)
292 			crp->crp_flags |= CRYPTO_F_BATCH;
293 		crp->crp_desc = crd;
294 
295 		crd->crd_skip = 0;
296 		crd->crd_len = secsize;
297 		crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
298 		if ((sc->sc_flags & G_ELI_FLAG_SINGLE_KEY) == 0)
299 			crd->crd_flags |= CRD_F_KEY_EXPLICIT;
300 		if (bp->bio_cmd == BIO_WRITE)
301 			crd->crd_flags |= CRD_F_ENCRYPT;
302 		crd->crd_alg = sc->sc_ealgo;
303 		crd->crd_key = g_eli_key_hold(sc, dstoff, secsize);
304 		crd->crd_klen = sc->sc_ekeylen;
305 		if (sc->sc_ealgo == CRYPTO_AES_XTS)
306 			crd->crd_klen <<= 1;
307 		g_eli_crypto_ivgen(sc, dstoff, crd->crd_iv,
308 		    sizeof(crd->crd_iv));
309 		crd->crd_next = NULL;
310 
311 		crp->crp_etype = 0;
312 		error = crypto_dispatch(crp);
313 		KASSERT(error == 0, ("crypto_dispatch() failed (error=%d)",
314 		    error));
315 	}
316 }
317