xref: /freebsd-13-stable/sys/geom/eli/g_eli_privacy.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #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/vnode.h>
45 
46 #include <vm/uma.h>
47 
48 #include <geom/geom.h>
49 #include <geom/geom_dbg.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  * Copy data from a (potentially unmapped) bio to a kernelspace buffer.
65  *
66  * The buffer must have at least as much room as bp->bio_length.
67  */
68 static void
g_eli_bio_copyin(struct bio * bp,void * kaddr)69 g_eli_bio_copyin(struct bio *bp, void *kaddr)
70 {
71 	struct uio uio;
72 	struct iovec iov[1];
73 
74 	iov[0].iov_base = kaddr;
75 	iov[0].iov_len = bp->bio_length;
76 	uio.uio_iov = iov;
77 	uio.uio_iovcnt = 1;
78 	uio.uio_offset = 0;
79 	uio.uio_resid = bp->bio_length;
80 	uio.uio_segflg = UIO_SYSSPACE;
81 	uio.uio_rw = UIO_READ;
82 	uiomove_fromphys(bp->bio_ma, bp->bio_ma_offset, bp->bio_length, &uio);
83 }
84 
85 /*
86  * The function is called after we read and decrypt data.
87  *
88  * 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
89  */
90 static int
g_eli_crypto_read_done(struct cryptop * crp)91 g_eli_crypto_read_done(struct cryptop *crp)
92 {
93 	struct g_eli_softc *sc;
94 	struct bio *bp;
95 
96 	if (crp->crp_etype == EAGAIN) {
97 		if (g_eli_crypto_rerun(crp) == 0)
98 			return (0);
99 	}
100 	bp = (struct bio *)crp->crp_opaque;
101 	bp->bio_inbed++;
102 	if (crp->crp_etype == 0) {
103 		G_ELI_DEBUG(3, "Crypto READ request done (%d/%d).",
104 		    bp->bio_inbed, bp->bio_children);
105 		bp->bio_completed += crp->crp_payload_length;
106 	} else {
107 		G_ELI_DEBUG(1, "Crypto READ request failed (%d/%d) error=%d.",
108 		    bp->bio_inbed, bp->bio_children, crp->crp_etype);
109 		if (bp->bio_error == 0)
110 			bp->bio_error = crp->crp_etype;
111 	}
112 	sc = bp->bio_to->geom->softc;
113 	if (sc != NULL && crp->crp_cipher_key != NULL)
114 		g_eli_key_drop(sc, __DECONST(void *, crp->crp_cipher_key));
115 	crypto_freereq(crp);
116 	/*
117 	 * Do we have all sectors already?
118 	 */
119 	if (bp->bio_inbed < bp->bio_children)
120 		return (0);
121 
122 	if (bp->bio_error != 0) {
123 		G_ELI_LOGREQ(0, bp, "Crypto READ request failed (error=%d).",
124 		    bp->bio_error);
125 		bp->bio_completed = 0;
126 	}
127 	/*
128 	 * Read is finished, send it up.
129 	 */
130 	g_io_deliver(bp, bp->bio_error);
131 	if (sc != NULL)
132 		atomic_subtract_int(&sc->sc_inflight, 1);
133 	return (0);
134 }
135 
136 /*
137  * The function is called after data encryption.
138  *
139  * g_eli_start -> g_eli_crypto_run -> G_ELI_CRYPTO_WRITE_DONE -> g_io_request -> g_eli_write_done -> g_io_deliver
140  */
141 static int
g_eli_crypto_write_done(struct cryptop * crp)142 g_eli_crypto_write_done(struct cryptop *crp)
143 {
144 	struct g_eli_softc *sc;
145 	struct g_geom *gp;
146 	struct g_consumer *cp;
147 	struct bio *bp, *cbp;
148 
149 	if (crp->crp_etype == EAGAIN) {
150 		if (g_eli_crypto_rerun(crp) == 0)
151 			return (0);
152 	}
153 	bp = (struct bio *)crp->crp_opaque;
154 	bp->bio_inbed++;
155 	if (crp->crp_etype == 0) {
156 		G_ELI_DEBUG(3, "Crypto WRITE request done (%d/%d).",
157 		    bp->bio_inbed, bp->bio_children);
158 	} else {
159 		G_ELI_DEBUG(1, "Crypto WRITE request failed (%d/%d) error=%d.",
160 		    bp->bio_inbed, bp->bio_children, crp->crp_etype);
161 		if (bp->bio_error == 0)
162 			bp->bio_error = crp->crp_etype;
163 	}
164 	gp = bp->bio_to->geom;
165 	sc = gp->softc;
166 	if (crp->crp_cipher_key != NULL)
167 		g_eli_key_drop(sc, __DECONST(void *, crp->crp_cipher_key));
168 	crypto_freereq(crp);
169 	/*
170 	 * All sectors are already encrypted?
171 	 */
172 	if (bp->bio_inbed < bp->bio_children)
173 		return (0);
174 	bp->bio_inbed = 0;
175 	bp->bio_children = 1;
176 	cbp = bp->bio_driver1;
177 	bp->bio_driver1 = NULL;
178 	if (bp->bio_error != 0) {
179 		G_ELI_LOGREQ(0, bp, "Crypto WRITE request failed (error=%d).",
180 		    bp->bio_error);
181 		free(bp->bio_driver2, M_ELI);
182 		bp->bio_driver2 = NULL;
183 		g_destroy_bio(cbp);
184 		g_io_deliver(bp, bp->bio_error);
185 		atomic_subtract_int(&sc->sc_inflight, 1);
186 		return (0);
187 	}
188 	cbp->bio_data = bp->bio_driver2;
189 	/*
190 	 * Clear BIO_UNMAPPED, which was inherited from where we cloned the bio
191 	 * in g_eli_start, because we manually set bio_data
192 	 */
193 	cbp->bio_flags &= ~BIO_UNMAPPED;
194 	cbp->bio_done = g_eli_write_done;
195 	cp = LIST_FIRST(&gp->consumer);
196 	cbp->bio_to = cp->provider;
197 	G_ELI_LOGREQ(2, cbp, "Sending request.");
198 	/*
199 	 * Send encrypted data to the provider.
200 	 */
201 	g_io_request(cbp, cp);
202 	return (0);
203 }
204 
205 /*
206  * The function is called to read encrypted data.
207  *
208  * 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
209  */
210 void
g_eli_crypto_read(struct g_eli_softc * sc,struct bio * bp,boolean_t fromworker)211 g_eli_crypto_read(struct g_eli_softc *sc, struct bio *bp, boolean_t fromworker)
212 {
213 	struct g_consumer *cp;
214 	struct bio *cbp;
215 
216 	if (!fromworker) {
217 		/*
218 		 * We are not called from the worker thread, so check if
219 		 * device is suspended.
220 		 */
221 		mtx_lock(&sc->sc_queue_mtx);
222 		if (sc->sc_flags & G_ELI_FLAG_SUSPEND) {
223 			/*
224 			 * If device is suspended, we place the request onto
225 			 * the queue, so it can be handled after resume.
226 			 */
227 			G_ELI_DEBUG(0, "device suspended, move onto queue");
228 			bioq_insert_tail(&sc->sc_queue, bp);
229 			mtx_unlock(&sc->sc_queue_mtx);
230 			wakeup(sc);
231 			return;
232 		}
233 		atomic_add_int(&sc->sc_inflight, 1);
234 		mtx_unlock(&sc->sc_queue_mtx);
235 	}
236 	bp->bio_pflags = 0;
237 	bp->bio_driver2 = NULL;
238 	cbp = bp->bio_driver1;
239 	cbp->bio_done = g_eli_read_done;
240 	cp = LIST_FIRST(&sc->sc_geom->consumer);
241 	cbp->bio_to = cp->provider;
242 	G_ELI_LOGREQ(2, cbp, "Sending request.");
243 	/*
244 	 * Read encrypted data from provider.
245 	 */
246 	g_io_request(cbp, cp);
247 }
248 
249 /*
250  * This is the main function responsible for cryptography (ie. communication
251  * with crypto(9) subsystem).
252  *
253  * BIO_READ:
254  *	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
255  * BIO_WRITE:
256  *	g_eli_start -> G_ELI_CRYPTO_RUN -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
257  */
258 void
g_eli_crypto_run(struct g_eli_worker * wr,struct bio * bp)259 g_eli_crypto_run(struct g_eli_worker *wr, struct bio *bp)
260 {
261 	struct g_eli_softc *sc;
262 	struct cryptop *crp;
263 	vm_page_t *pages;
264 	u_int i, nsec, secsize;
265 	off_t dstoff;
266 	u_char *data = NULL;
267 	int error;
268 	int pages_offset;
269 
270 	G_ELI_LOGREQ(3, bp, "%s", __func__);
271 
272 	bp->bio_pflags = wr->w_number;
273 	sc = wr->w_softc;
274 	secsize = LIST_FIRST(&sc->sc_geom->provider)->sectorsize;
275 	nsec = bp->bio_length / secsize;
276 
277 	bp->bio_inbed = 0;
278 	bp->bio_children = nsec;
279 
280 	/*
281 	 * If we write the data we cannot destroy current bio_data content,
282 	 * so we need to allocate more memory for encrypted data.
283 	 */
284 	if (bp->bio_cmd == BIO_WRITE) {
285 		data = malloc(bp->bio_length, M_ELI, M_WAITOK);
286 		bp->bio_driver2 = data;
287 		/*
288 		 * This copy could be eliminated by using crypto's output
289 		 * buffer, instead of using a single overwriting buffer.
290 		 */
291 		if ((bp->bio_flags & BIO_UNMAPPED) != 0)
292 			g_eli_bio_copyin(bp, data);
293 		else
294 			bcopy(bp->bio_data, data, bp->bio_length);
295 	} else {
296 		if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
297 			pages = bp->bio_ma;
298 			pages_offset = bp->bio_ma_offset;
299 		} else {
300 			data = bp->bio_data;
301 		}
302 	}
303 
304 	for (i = 0, dstoff = bp->bio_offset; i < nsec; i++, dstoff += secsize) {
305 		crp = crypto_getreq(wr->w_sid, M_WAITOK);
306 
307 		if (data) {
308 			crypto_use_buf(crp, data, secsize);
309 			data += secsize;
310 		} else {
311 			MPASS(pages != NULL);
312 			crypto_use_vmpage(crp, pages, secsize, pages_offset);
313 			pages_offset += secsize;
314 			pages += pages_offset >> PAGE_SHIFT;
315 			pages_offset &= PAGE_MASK;
316 		}
317 		crp->crp_opaque = (void *)bp;
318 		if (bp->bio_cmd == BIO_WRITE) {
319 			crp->crp_op = CRYPTO_OP_ENCRYPT;
320 			crp->crp_callback = g_eli_crypto_write_done;
321 		} else /* if (bp->bio_cmd == BIO_READ) */ {
322 			crp->crp_op = CRYPTO_OP_DECRYPT;
323 			crp->crp_callback = g_eli_crypto_read_done;
324 		}
325 		crp->crp_flags = CRYPTO_F_CBIFSYNC;
326 		if (g_eli_batch)
327 			crp->crp_flags |= CRYPTO_F_BATCH;
328 
329 		crp->crp_payload_start = 0;
330 		crp->crp_payload_length = secsize;
331 		if ((sc->sc_flags & G_ELI_FLAG_SINGLE_KEY) == 0) {
332 			crp->crp_cipher_key = g_eli_key_hold(sc, dstoff,
333 			    secsize);
334 		}
335 		if (g_eli_ivlen(sc->sc_ealgo) != 0) {
336 			crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
337 			g_eli_crypto_ivgen(sc, dstoff, crp->crp_iv,
338 			    sizeof(crp->crp_iv));
339 		}
340 
341 		error = crypto_dispatch(crp);
342 		KASSERT(error == 0, ("crypto_dispatch() failed (error=%d)",
343 		    error));
344 	}
345 }
346