xref: /freebsd/sys/geom/eli/g_eli_privacy.c (revision 9a14aa017b21c292740c00ee098195cd46642730)
1 /*-
2  * Copyright (c) 2005-2011 Pawel Jakub Dawidek <pawel@dawidek.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/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/kthread.h>
41 #include <sys/proc.h>
42 #include <sys/sched.h>
43 #include <sys/smp.h>
44 #include <sys/uio.h>
45 #include <sys/vnode.h>
46 
47 #include <vm/uma.h>
48 
49 #include <geom/geom.h>
50 #include <geom/eli/g_eli.h>
51 #include <geom/eli/pkcs5v2.h>
52 
53 /*
54  * Code paths:
55  * BIO_READ:
56  *	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
57  * BIO_WRITE:
58  *	g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
59  */
60 
61 MALLOC_DECLARE(M_ELI);
62 
63 /*
64  * The function is called after we read and decrypt data.
65  *
66  * 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
67  */
68 static int
69 g_eli_crypto_read_done(struct cryptop *crp)
70 {
71 	struct g_eli_softc *sc;
72 	struct bio *bp;
73 
74 	if (crp->crp_etype == EAGAIN) {
75 		if (g_eli_crypto_rerun(crp) == 0)
76 			return (0);
77 	}
78 	bp = (struct bio *)crp->crp_opaque;
79 	bp->bio_inbed++;
80 	if (crp->crp_etype == 0) {
81 		G_ELI_DEBUG(3, "Crypto READ request done (%d/%d).",
82 		    bp->bio_inbed, bp->bio_children);
83 		bp->bio_completed += crp->crp_olen;
84 	} else {
85 		G_ELI_DEBUG(1, "Crypto READ request failed (%d/%d) error=%d.",
86 		    bp->bio_inbed, bp->bio_children, crp->crp_etype);
87 		if (bp->bio_error == 0)
88 			bp->bio_error = crp->crp_etype;
89 	}
90 	sc = bp->bio_to->geom->softc;
91 	g_eli_key_drop(sc, crp->crp_desc->crd_key);
92 	/*
93 	 * Do we have all sectors already?
94 	 */
95 	if (bp->bio_inbed < bp->bio_children)
96 		return (0);
97 	free(bp->bio_driver2, M_ELI);
98 	bp->bio_driver2 = NULL;
99 	if (bp->bio_error != 0) {
100 		G_ELI_LOGREQ(0, bp, "Crypto READ request failed (error=%d).",
101 		    bp->bio_error);
102 		bp->bio_completed = 0;
103 	}
104 	/*
105 	 * Read is finished, send it up.
106 	 */
107 	g_io_deliver(bp, bp->bio_error);
108 	atomic_subtract_int(&sc->sc_inflight, 1);
109 	return (0);
110 }
111 
112 /*
113  * The function is called after data encryption.
114  *
115  * g_eli_start -> g_eli_crypto_run -> G_ELI_CRYPTO_WRITE_DONE -> g_io_request -> g_eli_write_done -> g_io_deliver
116  */
117 static int
118 g_eli_crypto_write_done(struct cryptop *crp)
119 {
120 	struct g_eli_softc *sc;
121 	struct g_geom *gp;
122 	struct g_consumer *cp;
123 	struct bio *bp, *cbp;
124 
125 	if (crp->crp_etype == EAGAIN) {
126 		if (g_eli_crypto_rerun(crp) == 0)
127 			return (0);
128 	}
129 	bp = (struct bio *)crp->crp_opaque;
130 	bp->bio_inbed++;
131 	if (crp->crp_etype == 0) {
132 		G_ELI_DEBUG(3, "Crypto WRITE request done (%d/%d).",
133 		    bp->bio_inbed, bp->bio_children);
134 	} else {
135 		G_ELI_DEBUG(1, "Crypto WRITE request failed (%d/%d) error=%d.",
136 		    bp->bio_inbed, bp->bio_children, crp->crp_etype);
137 		if (bp->bio_error == 0)
138 			bp->bio_error = crp->crp_etype;
139 	}
140 	gp = bp->bio_to->geom;
141 	sc = gp->softc;
142 	g_eli_key_drop(sc, crp->crp_desc->crd_key);
143 	/*
144 	 * All sectors are already encrypted?
145 	 */
146 	if (bp->bio_inbed < bp->bio_children)
147 		return (0);
148 	bp->bio_inbed = 0;
149 	bp->bio_children = 1;
150 	cbp = bp->bio_driver1;
151 	bp->bio_driver1 = NULL;
152 	if (bp->bio_error != 0) {
153 		G_ELI_LOGREQ(0, bp, "Crypto WRITE request failed (error=%d).",
154 		    bp->bio_error);
155 		free(bp->bio_driver2, M_ELI);
156 		bp->bio_driver2 = NULL;
157 		g_destroy_bio(cbp);
158 		g_io_deliver(bp, bp->bio_error);
159 		atomic_subtract_int(&sc->sc_inflight, 1);
160 		return (0);
161 	}
162 	cbp->bio_data = bp->bio_driver2;
163 	cbp->bio_done = g_eli_write_done;
164 	cp = LIST_FIRST(&gp->consumer);
165 	cbp->bio_to = cp->provider;
166 	G_ELI_LOGREQ(2, cbp, "Sending request.");
167 	/*
168 	 * Send encrypted data to the provider.
169 	 */
170 	g_io_request(cbp, cp);
171 	return (0);
172 }
173 
174 /*
175  * The function is called to read encrypted data.
176  *
177  * 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
178  */
179 void
180 g_eli_crypto_read(struct g_eli_softc *sc, struct bio *bp, boolean_t fromworker)
181 {
182 	struct g_consumer *cp;
183 	struct bio *cbp;
184 
185 	if (!fromworker) {
186 		/*
187 		 * We are not called from the worker thread, so check if
188 		 * device is suspended.
189 		 */
190 		mtx_lock(&sc->sc_queue_mtx);
191 		if (sc->sc_flags & G_ELI_FLAG_SUSPEND) {
192 			/*
193 			 * If device is suspended, we place the request onto
194 			 * the queue, so it can be handled after resume.
195 			 */
196 			G_ELI_DEBUG(0, "device suspended, move onto queue");
197 			bioq_insert_tail(&sc->sc_queue, bp);
198 			mtx_unlock(&sc->sc_queue_mtx);
199 			wakeup(sc);
200 			return;
201 		}
202 		atomic_add_int(&sc->sc_inflight, 1);
203 		mtx_unlock(&sc->sc_queue_mtx);
204 	}
205 	bp->bio_pflags = 0;
206 	bp->bio_driver2 = NULL;
207 	cbp = bp->bio_driver1;
208 	cbp->bio_done = g_eli_read_done;
209 	cp = LIST_FIRST(&sc->sc_geom->consumer);
210 	cbp->bio_to = cp->provider;
211 	G_ELI_LOGREQ(2, cbp, "Sending request.");
212 	/*
213 	 * Read encrypted data from provider.
214 	 */
215 	g_io_request(cbp, cp);
216 }
217 
218 /*
219  * This is the main function responsible for cryptography (ie. communication
220  * with crypto(9) subsystem).
221  *
222  * BIO_READ:
223  *	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
224  * BIO_WRITE:
225  *	g_eli_start -> G_ELI_CRYPTO_RUN -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
226  */
227 void
228 g_eli_crypto_run(struct g_eli_worker *wr, struct bio *bp)
229 {
230 	struct g_eli_softc *sc;
231 	struct cryptop *crp;
232 	struct cryptodesc *crd;
233 	struct uio *uio;
234 	struct iovec *iov;
235 	u_int i, nsec, secsize;
236 	int err, error;
237 	off_t dstoff;
238 	size_t size;
239 	u_char *p, *data;
240 
241 	G_ELI_LOGREQ(3, bp, "%s", __func__);
242 
243 	bp->bio_pflags = wr->w_number;
244 	sc = wr->w_softc;
245 	secsize = LIST_FIRST(&sc->sc_geom->provider)->sectorsize;
246 	nsec = bp->bio_length / secsize;
247 
248 	/*
249 	 * Calculate how much memory do we need.
250 	 * We need separate crypto operation for every single sector.
251 	 * It is much faster to calculate total amount of needed memory here and
252 	 * do the allocation once instead of allocating memory in pieces (many,
253 	 * many pieces).
254 	 */
255 	size = sizeof(*crp) * nsec;
256 	size += sizeof(*crd) * nsec;
257 	size += sizeof(*uio) * nsec;
258 	size += sizeof(*iov) * nsec;
259 	/*
260 	 * If we write the data we cannot destroy current bio_data content,
261 	 * so we need to allocate more memory for encrypted data.
262 	 */
263 	if (bp->bio_cmd == BIO_WRITE)
264 		size += bp->bio_length;
265 	p = malloc(size, M_ELI, M_WAITOK);
266 
267 	bp->bio_inbed = 0;
268 	bp->bio_children = nsec;
269 	bp->bio_driver2 = p;
270 
271 	if (bp->bio_cmd == BIO_READ)
272 		data = bp->bio_data;
273 	else {
274 		data = p;
275 		p += bp->bio_length;
276 		bcopy(bp->bio_data, data, bp->bio_length);
277 	}
278 
279 	error = 0;
280 	for (i = 0, dstoff = bp->bio_offset; i < nsec; i++, dstoff += secsize) {
281 		crp = (struct cryptop *)p;	p += sizeof(*crp);
282 		crd = (struct cryptodesc *)p;	p += sizeof(*crd);
283 		uio = (struct uio *)p;		p += sizeof(*uio);
284 		iov = (struct iovec *)p;	p += sizeof(*iov);
285 
286 		iov->iov_len = secsize;
287 		iov->iov_base = data;
288 		data += secsize;
289 
290 		uio->uio_iov = iov;
291 		uio->uio_iovcnt = 1;
292 		uio->uio_segflg = UIO_SYSSPACE;
293 		uio->uio_resid = secsize;
294 
295 		crp->crp_sid = wr->w_sid;
296 		crp->crp_ilen = secsize;
297 		crp->crp_olen = secsize;
298 		crp->crp_opaque = (void *)bp;
299 		crp->crp_buf = (void *)uio;
300 		if (bp->bio_cmd == BIO_WRITE)
301 			crp->crp_callback = g_eli_crypto_write_done;
302 		else /* if (bp->bio_cmd == BIO_READ) */
303 			crp->crp_callback = g_eli_crypto_read_done;
304 		crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIFSYNC | CRYPTO_F_REL;
305 		if (g_eli_batch)
306 			crp->crp_flags |= CRYPTO_F_BATCH;
307 		crp->crp_desc = crd;
308 
309 		crd->crd_skip = 0;
310 		crd->crd_len = secsize;
311 		crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
312 		if ((sc->sc_flags & G_ELI_FLAG_SINGLE_KEY) == 0)
313 			crd->crd_flags |= CRD_F_KEY_EXPLICIT;
314 		if (bp->bio_cmd == BIO_WRITE)
315 			crd->crd_flags |= CRD_F_ENCRYPT;
316 		crd->crd_alg = sc->sc_ealgo;
317 		crd->crd_key = g_eli_key_hold(sc, dstoff, secsize);
318 		crd->crd_klen = sc->sc_ekeylen;
319 		if (sc->sc_ealgo == CRYPTO_AES_XTS)
320 			crd->crd_klen <<= 1;
321 		g_eli_crypto_ivgen(sc, dstoff, crd->crd_iv,
322 		    sizeof(crd->crd_iv));
323 		crd->crd_next = NULL;
324 
325 		crp->crp_etype = 0;
326 		err = crypto_dispatch(crp);
327 		if (error == 0)
328 			error = err;
329 	}
330 	if (bp->bio_error == 0)
331 		bp->bio_error = error;
332 }
333