xref: /linux/crypto/algif_aead.c (revision eed5fde79651c66e0e24ba3d78a92afb63a76215)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * algif_aead: User-space interface for AEAD algorithms
4  *
5  * Copyright (C) 2014, Stephan Mueller <smueller@chronox.de>
6  *
7  * This file provides the user-space API for AEAD ciphers.
8  *
9  * The following concept of the memory management is used:
10  *
11  * The kernel maintains two SGLs, the TX SGL and the RX SGL. The TX SGL is
12  * filled by user space with the data submitted via sendmsg.  Filling up the TX
13  * SGL does not cause a crypto operation -- the data will only be tracked by the
14  * kernel. Upon receipt of one recvmsg call, the caller must provide a buffer
15  * which is tracked with the RX SGL.
16  *
17  * During the processing of the recvmsg operation, the cipher request is
18  * allocated and prepared. As part of the recvmsg operation, the processed
19  * TX buffers are extracted from the TX SGL into a separate SGL.
20  *
21  * After the completion of the crypto operation, the RX SGL and the cipher
22  * request is released. The extracted TX SGL parts are released together with
23  * the RX SGL release.
24  */
25 
26 #include <crypto/internal/aead.h>
27 #include <crypto/scatterwalk.h>
28 #include <crypto/if_alg.h>
29 #include <linux/init.h>
30 #include <linux/list.h>
31 #include <linux/kernel.h>
32 #include <linux/mm.h>
33 #include <linux/module.h>
34 #include <linux/net.h>
35 #include <net/sock.h>
36 
37 static const struct af_alg_allowlist_entry aead_allowlist[] = {
38 	{ "ccm(aes)", true }, /* bluez */
39 	{},
40 };
41 
42 static inline bool aead_sufficient_data(struct sock *sk)
43 {
44 	struct alg_sock *ask = alg_sk(sk);
45 	struct sock *psk = ask->parent;
46 	struct alg_sock *pask = alg_sk(psk);
47 	struct af_alg_ctx *ctx = ask->private;
48 	struct crypto_aead *tfm = pask->private;
49 	unsigned int as = crypto_aead_authsize(tfm);
50 
51 	/*
52 	 * The minimum amount of memory needed for an AEAD cipher is
53 	 * the AAD and in case of decryption the tag.
54 	 */
55 	return ctx->used >= ctx->aead_assoclen + (ctx->enc ? 0 : as);
56 }
57 
58 static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
59 {
60 	struct sock *sk = sock->sk;
61 	struct alg_sock *ask = alg_sk(sk);
62 	struct sock *psk = ask->parent;
63 	struct alg_sock *pask = alg_sk(psk);
64 	struct crypto_aead *tfm = pask->private;
65 	unsigned int ivsize = crypto_aead_ivsize(tfm);
66 
67 	return af_alg_sendmsg(sock, msg, size, ivsize);
68 }
69 
70 static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
71 			 size_t ignored, int flags)
72 {
73 	struct sock *sk = sock->sk;
74 	struct alg_sock *ask = alg_sk(sk);
75 	struct sock *psk = ask->parent;
76 	struct alg_sock *pask = alg_sk(psk);
77 	struct af_alg_ctx *ctx = ask->private;
78 	struct crypto_aead *tfm = pask->private;
79 	unsigned int as = crypto_aead_authsize(tfm);
80 	unsigned int ivsize = crypto_aead_ivsize(tfm);
81 	struct af_alg_async_req *areq;
82 	struct scatterlist *rsgl_src, *tsgl_src = NULL;
83 	void *iv;
84 	int err = 0;
85 	size_t used = 0;		/* [in]  TX bufs to be en/decrypted */
86 	size_t outlen = 0;		/* [out] RX bufs produced by kernel */
87 	size_t usedpages = 0;		/* [in]  RX bufs to be used from user */
88 	size_t processed = 0;		/* [in]  TX bufs to be consumed */
89 
90 	if (!ctx->init || ctx->more) {
91 		err = af_alg_wait_for_data(sk, flags, 0);
92 		if (err)
93 			return err;
94 	}
95 
96 	/*
97 	 * Data length provided by caller via sendmsg that has not yet been
98 	 * processed.
99 	 */
100 	used = ctx->used;
101 
102 	/*
103 	 * Make sure sufficient data is present -- note, the same check is also
104 	 * present in sendmsg. The checks in sendmsg shall provide an
105 	 * information to the data sender that something is wrong, but they are
106 	 * irrelevant to maintain the kernel integrity.  We need this check
107 	 * here too in case user space decides to not honor the error message
108 	 * in sendmsg and still call recvmsg. This check here protects the
109 	 * kernel integrity.
110 	 */
111 	if (!aead_sufficient_data(sk))
112 		return -EINVAL;
113 
114 	/*
115 	 * Calculate the minimum output buffer size holding the result of the
116 	 * cipher operation. When encrypting data, the receiving buffer is
117 	 * larger by the tag length compared to the input buffer as the
118 	 * encryption operation generates the tag. For decryption, the input
119 	 * buffer provides the tag which is consumed resulting in only the
120 	 * plaintext without a buffer for the tag returned to the caller.
121 	 */
122 	if (ctx->enc)
123 		outlen = used + as;
124 	else
125 		outlen = used - as;
126 
127 	/*
128 	 * The cipher operation input data is reduced by the associated data
129 	 * length as this data is processed separately later on.
130 	 */
131 	used -= ctx->aead_assoclen;
132 
133 	/* Allocate cipher request for current operation. */
134 	areq = af_alg_alloc_areq(sk, sizeof(struct af_alg_async_req) +
135 				     crypto_aead_reqsize(tfm) + ivsize);
136 	if (IS_ERR(areq))
137 		return PTR_ERR(areq);
138 
139 	iv = (u8 *)aead_request_ctx(&areq->cra_u.aead_req) +
140 	     crypto_aead_reqsize(tfm);
141 	memcpy(iv, ctx->iv, ivsize);
142 
143 	/* convert iovecs of output buffers into RX SGL */
144 	err = af_alg_get_rsgl(sk, msg, flags, areq, outlen, &usedpages);
145 	if (err)
146 		goto free;
147 
148 	/*
149 	 * Ensure output buffer is sufficiently large. If the caller provides
150 	 * less buffer space, only use the relative required input size. This
151 	 * allows AIO operation where the caller sent all data to be processed
152 	 * and the AIO operation performs the operation on the different chunks
153 	 * of the input data.
154 	 */
155 	if (usedpages < outlen) {
156 		size_t less = outlen - usedpages;
157 
158 		if (used < less + (ctx->enc ? 0 : as)) {
159 			err = -EINVAL;
160 			goto free;
161 		}
162 		used -= less;
163 		outlen -= less;
164 	}
165 
166 	/*
167 	 * Create a per request TX SGL for this request which tracks the
168 	 * SG entries from the global TX SGL.
169 	 */
170 	processed = used + ctx->aead_assoclen;
171 	areq->tsgl_entries = af_alg_count_tsgl(sk, processed);
172 	if (!areq->tsgl_entries)
173 		areq->tsgl_entries = 1;
174 	areq->tsgl = sock_kmalloc(sk, array_size(sizeof(*areq->tsgl),
175 					         areq->tsgl_entries),
176 				  GFP_KERNEL);
177 	if (!areq->tsgl) {
178 		err = -ENOMEM;
179 		goto free;
180 	}
181 	sg_init_table(areq->tsgl, areq->tsgl_entries);
182 	af_alg_pull_tsgl(sk, processed, areq->tsgl);
183 	tsgl_src = areq->tsgl;
184 
185 	/*
186 	 * Copy of AAD from source to destination
187 	 *
188 	 * The AAD is copied to the destination buffer without change. Even
189 	 * when user space uses an in-place cipher operation, the kernel
190 	 * will copy the data as it does not see whether such in-place operation
191 	 * is initiated.
192 	 */
193 
194 	/* Use the RX SGL as source (and destination) for crypto op. */
195 	rsgl_src = areq->first_rsgl.sgl.sgt.sgl;
196 
197 	memcpy_sglist(rsgl_src, tsgl_src, ctx->aead_assoclen);
198 
199 	/* Initialize the crypto operation */
200 	aead_request_set_crypt(&areq->cra_u.aead_req, tsgl_src,
201 			       areq->first_rsgl.sgl.sgt.sgl, used, iv);
202 	aead_request_set_ad(&areq->cra_u.aead_req, ctx->aead_assoclen);
203 	aead_request_set_tfm(&areq->cra_u.aead_req, tfm);
204 
205 	aead_request_set_callback(&areq->cra_u.aead_req,
206 				  CRYPTO_TFM_REQ_MAY_SLEEP |
207 				  CRYPTO_TFM_REQ_MAY_BACKLOG,
208 				  crypto_req_done, &ctx->wait);
209 	err = crypto_wait_req(ctx->enc ?
210 			crypto_aead_encrypt(&areq->cra_u.aead_req) :
211 			crypto_aead_decrypt(&areq->cra_u.aead_req),
212 			&ctx->wait);
213 
214 free:
215 	af_alg_free_resources(areq);
216 
217 	return err ? err : outlen;
218 }
219 
220 static int aead_recvmsg(struct socket *sock, struct msghdr *msg,
221 			size_t ignored, int flags)
222 {
223 	struct sock *sk = sock->sk;
224 	int ret = 0;
225 
226 	lock_sock(sk);
227 	while (msg_data_left(msg)) {
228 		int err = _aead_recvmsg(sock, msg, ignored, flags);
229 
230 		/*
231 		 * This error covers -EIOCBQUEUED which implies that we can
232 		 * only handle one AIO request. If the caller wants to have
233 		 * multiple AIO requests in parallel, he must make multiple
234 		 * separate AIO calls.
235 		 *
236 		 * Also return the error if no data has been processed so far.
237 		 */
238 		if (err <= 0) {
239 			if (err == -EIOCBQUEUED || err == -EBADMSG || !ret)
240 				ret = err;
241 			goto out;
242 		}
243 
244 		ret += err;
245 	}
246 
247 out:
248 	af_alg_wmem_wakeup(sk);
249 	release_sock(sk);
250 	return ret;
251 }
252 
253 static struct proto_ops algif_aead_ops = {
254 	.family		=	PF_ALG,
255 
256 	.connect	=	sock_no_connect,
257 	.socketpair	=	sock_no_socketpair,
258 	.getname	=	sock_no_getname,
259 	.ioctl		=	sock_no_ioctl,
260 	.listen		=	sock_no_listen,
261 	.shutdown	=	sock_no_shutdown,
262 	.mmap		=	sock_no_mmap,
263 	.bind		=	sock_no_bind,
264 	.accept		=	sock_no_accept,
265 
266 	.release	=	af_alg_release,
267 	.sendmsg	=	aead_sendmsg,
268 	.recvmsg	=	aead_recvmsg,
269 	.poll		=	af_alg_poll,
270 };
271 
272 static int aead_check_key(struct socket *sock)
273 {
274 	int err = 0;
275 	struct sock *psk;
276 	struct alg_sock *pask;
277 	struct crypto_aead *tfm;
278 	struct sock *sk = sock->sk;
279 	struct alg_sock *ask = alg_sk(sk);
280 
281 	lock_sock(sk);
282 	if (!atomic_read(&ask->nokey_refcnt))
283 		goto unlock_child;
284 
285 	psk = ask->parent;
286 	pask = alg_sk(ask->parent);
287 	tfm = pask->private;
288 
289 	err = -ENOKEY;
290 	lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
291 	if (crypto_aead_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
292 		goto unlock;
293 
294 	atomic_dec(&pask->nokey_refcnt);
295 	atomic_set(&ask->nokey_refcnt, 0);
296 
297 	err = 0;
298 
299 unlock:
300 	release_sock(psk);
301 unlock_child:
302 	release_sock(sk);
303 
304 	return err;
305 }
306 
307 static int aead_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
308 				  size_t size)
309 {
310 	int err;
311 
312 	err = aead_check_key(sock);
313 	if (err)
314 		return err;
315 
316 	return aead_sendmsg(sock, msg, size);
317 }
318 
319 static int aead_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
320 				  size_t ignored, int flags)
321 {
322 	int err;
323 
324 	err = aead_check_key(sock);
325 	if (err)
326 		return err;
327 
328 	return aead_recvmsg(sock, msg, ignored, flags);
329 }
330 
331 static struct proto_ops algif_aead_ops_nokey = {
332 	.family		=	PF_ALG,
333 
334 	.connect	=	sock_no_connect,
335 	.socketpair	=	sock_no_socketpair,
336 	.getname	=	sock_no_getname,
337 	.ioctl		=	sock_no_ioctl,
338 	.listen		=	sock_no_listen,
339 	.shutdown	=	sock_no_shutdown,
340 	.mmap		=	sock_no_mmap,
341 	.bind		=	sock_no_bind,
342 	.accept		=	sock_no_accept,
343 
344 	.release	=	af_alg_release,
345 	.sendmsg	=	aead_sendmsg_nokey,
346 	.recvmsg	=	aead_recvmsg_nokey,
347 	.poll		=	af_alg_poll,
348 };
349 
350 static void *aead_bind(const char *name)
351 {
352 	int err;
353 
354 	err = af_alg_check_restriction(name, aead_allowlist);
355 	if (err)
356 		return ERR_PTR(err);
357 
358 	return crypto_alloc_aead(name, 0, AF_ALG_CRYPTOAPI_MASK);
359 }
360 
361 static void aead_release(void *private)
362 {
363 	crypto_free_aead(private);
364 }
365 
366 static int aead_setauthsize(void *private, unsigned int authsize)
367 {
368 	return crypto_aead_setauthsize(private, authsize);
369 }
370 
371 static int aead_setkey(void *private, const u8 *key, unsigned int keylen)
372 {
373 	return crypto_aead_setkey(private, key, keylen);
374 }
375 
376 static void aead_sock_destruct(struct sock *sk)
377 {
378 	struct alg_sock *ask = alg_sk(sk);
379 	struct af_alg_ctx *ctx = ask->private;
380 	struct sock *psk = ask->parent;
381 	struct alg_sock *pask = alg_sk(psk);
382 	struct crypto_aead *tfm = pask->private;
383 	unsigned int ivlen = crypto_aead_ivsize(tfm);
384 
385 	af_alg_pull_tsgl(sk, ctx->used, NULL);
386 	sock_kzfree_s(sk, ctx->iv, ivlen);
387 	sock_kfree_s(sk, ctx, ctx->len);
388 	af_alg_release_parent(sk);
389 }
390 
391 static int aead_accept_parent_nokey(void *private, struct sock *sk)
392 {
393 	struct af_alg_ctx *ctx;
394 	struct alg_sock *ask = alg_sk(sk);
395 	struct crypto_aead *tfm = private;
396 	unsigned int len = sizeof(*ctx);
397 	unsigned int ivlen = crypto_aead_ivsize(tfm);
398 
399 	ctx = sock_kmalloc(sk, len, GFP_KERNEL);
400 	if (!ctx)
401 		return -ENOMEM;
402 	memset(ctx, 0, len);
403 
404 	ctx->iv = sock_kmalloc(sk, ivlen, GFP_KERNEL);
405 	if (!ctx->iv) {
406 		sock_kfree_s(sk, ctx, len);
407 		return -ENOMEM;
408 	}
409 	memset(ctx->iv, 0, ivlen);
410 
411 	INIT_LIST_HEAD(&ctx->tsgl_list);
412 	ctx->len = len;
413 	crypto_init_wait(&ctx->wait);
414 
415 	ask->private = ctx;
416 
417 	sk->sk_destruct = aead_sock_destruct;
418 
419 	return 0;
420 }
421 
422 static int aead_accept_parent(void *private, struct sock *sk)
423 {
424 	struct crypto_aead *tfm = private;
425 
426 	if (crypto_aead_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
427 		return -ENOKEY;
428 
429 	return aead_accept_parent_nokey(private, sk);
430 }
431 
432 static const struct af_alg_type algif_type_aead = {
433 	.bind		=	aead_bind,
434 	.release	=	aead_release,
435 	.setkey		=	aead_setkey,
436 	.setauthsize	=	aead_setauthsize,
437 	.accept		=	aead_accept_parent,
438 	.accept_nokey	=	aead_accept_parent_nokey,
439 	.ops		=	&algif_aead_ops,
440 	.ops_nokey	=	&algif_aead_ops_nokey,
441 	.name		=	"aead",
442 	.owner		=	THIS_MODULE
443 };
444 
445 static int __init algif_aead_init(void)
446 {
447 	return af_alg_register_type(&algif_type_aead);
448 }
449 
450 static void __exit algif_aead_exit(void)
451 {
452 	int err = af_alg_unregister_type(&algif_type_aead);
453 	BUG_ON(err);
454 }
455 
456 module_init(algif_aead_init);
457 module_exit(algif_aead_exit);
458 MODULE_LICENSE("GPL");
459 MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
460 MODULE_DESCRIPTION("AEAD kernel crypto API user space interface");
461