xref: /freebsd/sys/geom/eli/g_eli_privacy.c (revision d65cd7a57bf0600b722afc770838a5d0c1c3a8e1)
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/geom_dbg.h>
52 #include <geom/eli/g_eli.h>
53 #include <geom/eli/pkcs5v2.h>
54 
55 /*
56  * Code paths:
57  * BIO_READ:
58  *	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
59  * BIO_WRITE:
60  *	g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
61  */
62 
63 MALLOC_DECLARE(M_ELI);
64 
65 /*
66  * The function is called after we read and decrypt data.
67  *
68  * 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
69  */
70 static int
71 g_eli_crypto_read_done(struct cryptop *crp)
72 {
73 	struct g_eli_softc *sc;
74 	struct bio *bp;
75 
76 	if (crp->crp_etype == EAGAIN) {
77 		if (g_eli_crypto_rerun(crp) == 0)
78 			return (0);
79 	}
80 	bp = (struct bio *)crp->crp_opaque;
81 	bp->bio_inbed++;
82 	if (crp->crp_etype == 0) {
83 		G_ELI_DEBUG(3, "Crypto READ request done (%d/%d).",
84 		    bp->bio_inbed, bp->bio_children);
85 		bp->bio_completed += crp->crp_ilen;
86 	} else {
87 		G_ELI_DEBUG(1, "Crypto READ request failed (%d/%d) error=%d.",
88 		    bp->bio_inbed, bp->bio_children, crp->crp_etype);
89 		if (bp->bio_error == 0)
90 			bp->bio_error = crp->crp_etype;
91 	}
92 	sc = bp->bio_to->geom->softc;
93 	if (sc != NULL && crp->crp_cipher_key != NULL)
94 		g_eli_key_drop(sc, __DECONST(void *, crp->crp_cipher_key));
95 	crypto_freereq(crp);
96 	/*
97 	 * Do we have all sectors already?
98 	 */
99 	if (bp->bio_inbed < bp->bio_children)
100 		return (0);
101 	free(bp->bio_driver2, M_ELI);
102 	bp->bio_driver2 = NULL;
103 	if (bp->bio_error != 0) {
104 		G_ELI_LOGREQ(0, bp, "Crypto READ request failed (error=%d).",
105 		    bp->bio_error);
106 		bp->bio_completed = 0;
107 	}
108 	/*
109 	 * Read is finished, send it up.
110 	 */
111 	g_io_deliver(bp, bp->bio_error);
112 	if (sc != NULL)
113 		atomic_subtract_int(&sc->sc_inflight, 1);
114 	return (0);
115 }
116 
117 /*
118  * The function is called after data encryption.
119  *
120  * g_eli_start -> g_eli_crypto_run -> G_ELI_CRYPTO_WRITE_DONE -> g_io_request -> g_eli_write_done -> g_io_deliver
121  */
122 static int
123 g_eli_crypto_write_done(struct cryptop *crp)
124 {
125 	struct g_eli_softc *sc;
126 	struct g_geom *gp;
127 	struct g_consumer *cp;
128 	struct bio *bp, *cbp;
129 
130 	if (crp->crp_etype == EAGAIN) {
131 		if (g_eli_crypto_rerun(crp) == 0)
132 			return (0);
133 	}
134 	bp = (struct bio *)crp->crp_opaque;
135 	bp->bio_inbed++;
136 	if (crp->crp_etype == 0) {
137 		G_ELI_DEBUG(3, "Crypto WRITE request done (%d/%d).",
138 		    bp->bio_inbed, bp->bio_children);
139 	} else {
140 		G_ELI_DEBUG(1, "Crypto WRITE request failed (%d/%d) error=%d.",
141 		    bp->bio_inbed, bp->bio_children, crp->crp_etype);
142 		if (bp->bio_error == 0)
143 			bp->bio_error = crp->crp_etype;
144 	}
145 	gp = bp->bio_to->geom;
146 	sc = gp->softc;
147 	if (crp->crp_cipher_key != NULL)
148 		g_eli_key_drop(sc, __DECONST(void *, crp->crp_cipher_key));
149 	crypto_freereq(crp);
150 	/*
151 	 * All sectors are already encrypted?
152 	 */
153 	if (bp->bio_inbed < bp->bio_children)
154 		return (0);
155 	bp->bio_inbed = 0;
156 	bp->bio_children = 1;
157 	cbp = bp->bio_driver1;
158 	bp->bio_driver1 = NULL;
159 	if (bp->bio_error != 0) {
160 		G_ELI_LOGREQ(0, bp, "Crypto WRITE request failed (error=%d).",
161 		    bp->bio_error);
162 		free(bp->bio_driver2, M_ELI);
163 		bp->bio_driver2 = NULL;
164 		g_destroy_bio(cbp);
165 		g_io_deliver(bp, bp->bio_error);
166 		atomic_subtract_int(&sc->sc_inflight, 1);
167 		return (0);
168 	}
169 	cbp->bio_data = bp->bio_driver2;
170 	cbp->bio_done = g_eli_write_done;
171 	cp = LIST_FIRST(&gp->consumer);
172 	cbp->bio_to = cp->provider;
173 	G_ELI_LOGREQ(2, cbp, "Sending request.");
174 	/*
175 	 * Send encrypted data to the provider.
176 	 */
177 	g_io_request(cbp, cp);
178 	return (0);
179 }
180 
181 /*
182  * The function is called to read encrypted data.
183  *
184  * 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
185  */
186 void
187 g_eli_crypto_read(struct g_eli_softc *sc, struct bio *bp, boolean_t fromworker)
188 {
189 	struct g_consumer *cp;
190 	struct bio *cbp;
191 
192 	if (!fromworker) {
193 		/*
194 		 * We are not called from the worker thread, so check if
195 		 * device is suspended.
196 		 */
197 		mtx_lock(&sc->sc_queue_mtx);
198 		if (sc->sc_flags & G_ELI_FLAG_SUSPEND) {
199 			/*
200 			 * If device is suspended, we place the request onto
201 			 * the queue, so it can be handled after resume.
202 			 */
203 			G_ELI_DEBUG(0, "device suspended, move onto queue");
204 			bioq_insert_tail(&sc->sc_queue, bp);
205 			mtx_unlock(&sc->sc_queue_mtx);
206 			wakeup(sc);
207 			return;
208 		}
209 		atomic_add_int(&sc->sc_inflight, 1);
210 		mtx_unlock(&sc->sc_queue_mtx);
211 	}
212 	bp->bio_pflags = 0;
213 	bp->bio_driver2 = NULL;
214 	cbp = bp->bio_driver1;
215 	cbp->bio_done = g_eli_read_done;
216 	cp = LIST_FIRST(&sc->sc_geom->consumer);
217 	cbp->bio_to = cp->provider;
218 	G_ELI_LOGREQ(2, cbp, "Sending request.");
219 	/*
220 	 * Read encrypted data from provider.
221 	 */
222 	g_io_request(cbp, cp);
223 }
224 
225 /*
226  * This is the main function responsible for cryptography (ie. communication
227  * with crypto(9) subsystem).
228  *
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 void
235 g_eli_crypto_run(struct g_eli_worker *wr, struct bio *bp)
236 {
237 	struct g_eli_softc *sc;
238 	struct cryptop *crp;
239 	u_int i, nsec, secsize;
240 	off_t dstoff;
241 	u_char *data;
242 	int error;
243 
244 	G_ELI_LOGREQ(3, bp, "%s", __func__);
245 
246 	bp->bio_pflags = wr->w_number;
247 	sc = wr->w_softc;
248 	secsize = LIST_FIRST(&sc->sc_geom->provider)->sectorsize;
249 	nsec = bp->bio_length / secsize;
250 
251 	bp->bio_inbed = 0;
252 	bp->bio_children = nsec;
253 
254 	/*
255 	 * If we write the data we cannot destroy current bio_data content,
256 	 * so we need to allocate more memory for encrypted data.
257 	 */
258 	if (bp->bio_cmd == BIO_WRITE) {
259 		data = malloc(bp->bio_length, M_ELI, M_WAITOK);
260 		bp->bio_driver2 = data;
261 		bcopy(bp->bio_data, data, bp->bio_length);
262 	} else
263 		data = bp->bio_data;
264 
265 	for (i = 0, dstoff = bp->bio_offset; i < nsec; i++, dstoff += secsize) {
266 		crp = crypto_getreq(wr->w_sid, M_WAITOK);
267 
268 		crp->crp_ilen = secsize;
269 		crp->crp_opaque = (void *)bp;
270 		crp->crp_buf_type = CRYPTO_BUF_CONTIG;
271 		crp->crp_buf = (void *)data;
272 		data += secsize;
273 		if (bp->bio_cmd == BIO_WRITE) {
274 			crp->crp_op = CRYPTO_OP_ENCRYPT;
275 			crp->crp_callback = g_eli_crypto_write_done;
276 		} else /* if (bp->bio_cmd == BIO_READ) */ {
277 			crp->crp_op = CRYPTO_OP_DECRYPT;
278 			crp->crp_callback = g_eli_crypto_read_done;
279 		}
280 		crp->crp_flags = CRYPTO_F_CBIFSYNC;
281 		if (g_eli_batch)
282 			crp->crp_flags |= CRYPTO_F_BATCH;
283 
284 		crp->crp_payload_start = 0;
285 		crp->crp_payload_length = secsize;
286 		crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
287 		if ((sc->sc_flags & G_ELI_FLAG_SINGLE_KEY) == 0) {
288 			crp->crp_cipher_key = g_eli_key_hold(sc, dstoff,
289 			    secsize);
290 		}
291 		g_eli_crypto_ivgen(sc, dstoff, crp->crp_iv,
292 		    sizeof(crp->crp_iv));
293 
294 		error = crypto_dispatch(crp);
295 		KASSERT(error == 0, ("crypto_dispatch() failed (error=%d)",
296 		    error));
297 	}
298 }
299