blob: ccae4a7ada8ab68d04bac8b0a1355edf3ec87947 [file] [log] [blame]
Herbert Xu03c8efc2010-10-19 21:12:39 +08001/*
2 * af_alg: User-space algorithm interface
3 *
4 * This file provides the user-space API for algorithms.
5 *
6 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 */
14
Arun Sharma600634972011-07-26 16:09:06 -070015#include <linux/atomic.h>
Herbert Xu03c8efc2010-10-19 21:12:39 +080016#include <crypto/if_alg.h>
17#include <linux/crypto.h>
18#include <linux/init.h>
19#include <linux/kernel.h>
20#include <linux/list.h>
21#include <linux/module.h>
22#include <linux/net.h>
23#include <linux/rwsem.h>
Stephan Mueller2d975912017-08-02 07:56:19 +020024#include <linux/sched/signal.h>
Milan Broz4c63f832014-07-29 18:41:09 +000025#include <linux/security.h>
Herbert Xu03c8efc2010-10-19 21:12:39 +080026
27struct alg_type_list {
28 const struct af_alg_type *type;
29 struct list_head list;
30};
31
Randy Dunlap06869522010-12-21 22:22:40 +110032static atomic_long_t alg_memory_allocated;
Herbert Xu03c8efc2010-10-19 21:12:39 +080033
34static struct proto alg_proto = {
35 .name = "ALG",
36 .owner = THIS_MODULE,
37 .memory_allocated = &alg_memory_allocated,
38 .obj_size = sizeof(struct alg_sock),
39};
40
41static LIST_HEAD(alg_types);
42static DECLARE_RWSEM(alg_types_sem);
43
44static const struct af_alg_type *alg_get_type(const char *name)
45{
46 const struct af_alg_type *type = ERR_PTR(-ENOENT);
47 struct alg_type_list *node;
48
49 down_read(&alg_types_sem);
50 list_for_each_entry(node, &alg_types, list) {
51 if (strcmp(node->type->name, name))
52 continue;
53
54 if (try_module_get(node->type->owner))
55 type = node->type;
56 break;
57 }
58 up_read(&alg_types_sem);
59
60 return type;
61}
62
63int af_alg_register_type(const struct af_alg_type *type)
64{
65 struct alg_type_list *node;
66 int err = -EEXIST;
67
68 down_write(&alg_types_sem);
69 list_for_each_entry(node, &alg_types, list) {
70 if (!strcmp(node->type->name, type->name))
71 goto unlock;
72 }
73
74 node = kmalloc(sizeof(*node), GFP_KERNEL);
75 err = -ENOMEM;
76 if (!node)
77 goto unlock;
78
79 type->ops->owner = THIS_MODULE;
Herbert Xu37766582016-01-04 13:35:18 +090080 if (type->ops_nokey)
81 type->ops_nokey->owner = THIS_MODULE;
Herbert Xu03c8efc2010-10-19 21:12:39 +080082 node->type = type;
83 list_add(&node->list, &alg_types);
84 err = 0;
85
86unlock:
87 up_write(&alg_types_sem);
88
89 return err;
90}
91EXPORT_SYMBOL_GPL(af_alg_register_type);
92
93int af_alg_unregister_type(const struct af_alg_type *type)
94{
95 struct alg_type_list *node;
96 int err = -ENOENT;
97
98 down_write(&alg_types_sem);
99 list_for_each_entry(node, &alg_types, list) {
100 if (strcmp(node->type->name, type->name))
101 continue;
102
103 list_del(&node->list);
104 kfree(node);
105 err = 0;
106 break;
107 }
108 up_write(&alg_types_sem);
109
110 return err;
111}
112EXPORT_SYMBOL_GPL(af_alg_unregister_type);
113
114static void alg_do_release(const struct af_alg_type *type, void *private)
115{
116 if (!type)
117 return;
118
119 type->release(private);
120 module_put(type->owner);
121}
122
123int af_alg_release(struct socket *sock)
124{
125 if (sock->sk)
126 sock_put(sock->sk);
127 return 0;
128}
129EXPORT_SYMBOL_GPL(af_alg_release);
130
Herbert Xuc840ac62015-12-30 11:47:53 +0800131void af_alg_release_parent(struct sock *sk)
132{
133 struct alg_sock *ask = alg_sk(sk);
Herbert Xua6a48c52016-01-13 15:03:32 +0800134 unsigned int nokey = ask->nokey_refcnt;
135 bool last = nokey && !ask->refcnt;
Herbert Xuc840ac62015-12-30 11:47:53 +0800136
137 sk = ask->parent;
138 ask = alg_sk(sk);
139
140 lock_sock(sk);
Herbert Xua6a48c52016-01-13 15:03:32 +0800141 ask->nokey_refcnt -= nokey;
142 if (!last)
143 last = !--ask->refcnt;
Herbert Xuc840ac62015-12-30 11:47:53 +0800144 release_sock(sk);
145
146 if (last)
147 sock_put(sk);
148}
149EXPORT_SYMBOL_GPL(af_alg_release_parent);
150
Herbert Xu03c8efc2010-10-19 21:12:39 +0800151static int alg_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
152{
Stephan Muellerbb30b882018-01-02 08:55:25 +0100153 const u32 allowed = CRYPTO_ALG_KERN_DRIVER_ONLY;
Herbert Xu03c8efc2010-10-19 21:12:39 +0800154 struct sock *sk = sock->sk;
155 struct alg_sock *ask = alg_sk(sk);
156 struct sockaddr_alg *sa = (void *)uaddr;
157 const struct af_alg_type *type;
158 void *private;
Herbert Xuc840ac62015-12-30 11:47:53 +0800159 int err;
Herbert Xu03c8efc2010-10-19 21:12:39 +0800160
161 if (sock->state == SS_CONNECTED)
162 return -EINVAL;
163
Herbert Xu3f69cc62017-04-06 16:16:09 +0800164 if (addr_len < sizeof(*sa))
Herbert Xu03c8efc2010-10-19 21:12:39 +0800165 return -EINVAL;
166
Eric Dumazeta4668562018-04-07 13:42:36 -0700167 /* If caller uses non-allowed flag, return error. */
168 if ((sa->salg_feat & ~allowed) || (sa->salg_mask & ~allowed))
169 return -EINVAL;
170
Herbert Xu03c8efc2010-10-19 21:12:39 +0800171 sa->salg_type[sizeof(sa->salg_type) - 1] = 0;
Herbert Xu3f69cc62017-04-06 16:16:09 +0800172 sa->salg_name[sizeof(sa->salg_name) + addr_len - sizeof(*sa) - 1] = 0;
Herbert Xu03c8efc2010-10-19 21:12:39 +0800173
174 type = alg_get_type(sa->salg_type);
175 if (IS_ERR(type) && PTR_ERR(type) == -ENOENT) {
176 request_module("algif-%s", sa->salg_type);
177 type = alg_get_type(sa->salg_type);
178 }
179
180 if (IS_ERR(type))
181 return PTR_ERR(type);
182
Stephan Muellerbb30b882018-01-02 08:55:25 +0100183 private = type->bind(sa->salg_name, sa->salg_feat, sa->salg_mask);
Herbert Xu03c8efc2010-10-19 21:12:39 +0800184 if (IS_ERR(private)) {
185 module_put(type->owner);
186 return PTR_ERR(private);
187 }
188
Herbert Xuc840ac62015-12-30 11:47:53 +0800189 err = -EBUSY;
Herbert Xu03c8efc2010-10-19 21:12:39 +0800190 lock_sock(sk);
Herbert Xua6a48c52016-01-13 15:03:32 +0800191 if (ask->refcnt | ask->nokey_refcnt)
Herbert Xuc840ac62015-12-30 11:47:53 +0800192 goto unlock;
Herbert Xu03c8efc2010-10-19 21:12:39 +0800193
194 swap(ask->type, type);
195 swap(ask->private, private);
196
Herbert Xuc840ac62015-12-30 11:47:53 +0800197 err = 0;
198
199unlock:
Herbert Xu03c8efc2010-10-19 21:12:39 +0800200 release_sock(sk);
201
202 alg_do_release(type, private);
203
Herbert Xuc840ac62015-12-30 11:47:53 +0800204 return err;
Herbert Xu03c8efc2010-10-19 21:12:39 +0800205}
206
207static int alg_setkey(struct sock *sk, char __user *ukey,
208 unsigned int keylen)
209{
210 struct alg_sock *ask = alg_sk(sk);
211 const struct af_alg_type *type = ask->type;
212 u8 *key;
213 int err;
214
215 key = sock_kmalloc(sk, keylen, GFP_KERNEL);
216 if (!key)
217 return -ENOMEM;
218
219 err = -EFAULT;
220 if (copy_from_user(key, ukey, keylen))
221 goto out;
222
223 err = type->setkey(ask->private, key, keylen);
224
225out:
Stephan Muellerad202c82014-12-23 09:34:03 +0100226 sock_kzfree_s(sk, key, keylen);
Herbert Xu03c8efc2010-10-19 21:12:39 +0800227
228 return err;
229}
230
231static int alg_setsockopt(struct socket *sock, int level, int optname,
232 char __user *optval, unsigned int optlen)
233{
234 struct sock *sk = sock->sk;
235 struct alg_sock *ask = alg_sk(sk);
236 const struct af_alg_type *type;
Herbert Xuc840ac62015-12-30 11:47:53 +0800237 int err = -EBUSY;
Herbert Xu03c8efc2010-10-19 21:12:39 +0800238
239 lock_sock(sk);
Herbert Xuc840ac62015-12-30 11:47:53 +0800240 if (ask->refcnt)
241 goto unlock;
242
Herbert Xu03c8efc2010-10-19 21:12:39 +0800243 type = ask->type;
244
Herbert Xuc840ac62015-12-30 11:47:53 +0800245 err = -ENOPROTOOPT;
Herbert Xu03c8efc2010-10-19 21:12:39 +0800246 if (level != SOL_ALG || !type)
247 goto unlock;
248
249 switch (optname) {
250 case ALG_SET_KEY:
251 if (sock->state == SS_CONNECTED)
252 goto unlock;
253 if (!type->setkey)
254 goto unlock;
255
256 err = alg_setkey(sk, optval, optlen);
Stephan Mueller25fb8632014-12-07 23:21:42 +0100257 break;
258 case ALG_SET_AEAD_AUTHSIZE:
259 if (sock->state == SS_CONNECTED)
260 goto unlock;
261 if (!type->setauthsize)
262 goto unlock;
263 err = type->setauthsize(ask->private, optlen);
Herbert Xu03c8efc2010-10-19 21:12:39 +0800264 }
265
266unlock:
267 release_sock(sk);
268
269 return err;
270}
271
David Howellscdfbabf2017-03-09 08:09:05 +0000272int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern)
Herbert Xu03c8efc2010-10-19 21:12:39 +0800273{
274 struct alg_sock *ask = alg_sk(sk);
275 const struct af_alg_type *type;
276 struct sock *sk2;
Herbert Xu6a935172016-01-13 14:59:03 +0800277 unsigned int nokey;
Herbert Xu03c8efc2010-10-19 21:12:39 +0800278 int err;
279
280 lock_sock(sk);
281 type = ask->type;
282
283 err = -EINVAL;
284 if (!type)
285 goto unlock;
286
David Howellscdfbabf2017-03-09 08:09:05 +0000287 sk2 = sk_alloc(sock_net(sk), PF_ALG, GFP_KERNEL, &alg_proto, kern);
Herbert Xu03c8efc2010-10-19 21:12:39 +0800288 err = -ENOMEM;
289 if (!sk2)
290 goto unlock;
291
292 sock_init_data(newsock, sk2);
Herbert Xu2acce6a2017-07-10 22:00:48 +0800293 security_sock_graft(sk2, newsock);
Milan Broz4c63f832014-07-29 18:41:09 +0000294 security_sk_clone(sk, sk2);
Herbert Xu03c8efc2010-10-19 21:12:39 +0800295
296 err = type->accept(ask->private, sk2);
Herbert Xu37766582016-01-04 13:35:18 +0900297
298 nokey = err == -ENOKEY;
299 if (nokey && type->accept_nokey)
300 err = type->accept_nokey(ask->private, sk2);
301
Herbert Xua3832922015-12-30 20:24:17 +0800302 if (err)
Herbert Xu03c8efc2010-10-19 21:12:39 +0800303 goto unlock;
Herbert Xu03c8efc2010-10-19 21:12:39 +0800304
305 sk2->sk_family = PF_ALG;
306
Herbert Xu37766582016-01-04 13:35:18 +0900307 if (nokey || !ask->refcnt++)
Herbert Xuc840ac62015-12-30 11:47:53 +0800308 sock_hold(sk);
Herbert Xua6a48c52016-01-13 15:03:32 +0800309 ask->nokey_refcnt += nokey;
Herbert Xu03c8efc2010-10-19 21:12:39 +0800310 alg_sk(sk2)->parent = sk;
311 alg_sk(sk2)->type = type;
Herbert Xu6a935172016-01-13 14:59:03 +0800312 alg_sk(sk2)->nokey_refcnt = nokey;
Herbert Xu03c8efc2010-10-19 21:12:39 +0800313
314 newsock->ops = type->ops;
315 newsock->state = SS_CONNECTED;
316
Herbert Xu37766582016-01-04 13:35:18 +0900317 if (nokey)
318 newsock->ops = type->ops_nokey;
319
Herbert Xu03c8efc2010-10-19 21:12:39 +0800320 err = 0;
321
322unlock:
323 release_sock(sk);
324
325 return err;
326}
327EXPORT_SYMBOL_GPL(af_alg_accept);
328
David Howellscdfbabf2017-03-09 08:09:05 +0000329static int alg_accept(struct socket *sock, struct socket *newsock, int flags,
330 bool kern)
Herbert Xu03c8efc2010-10-19 21:12:39 +0800331{
David Howellscdfbabf2017-03-09 08:09:05 +0000332 return af_alg_accept(sock->sk, newsock, kern);
Herbert Xu03c8efc2010-10-19 21:12:39 +0800333}
334
335static const struct proto_ops alg_proto_ops = {
336 .family = PF_ALG,
337 .owner = THIS_MODULE,
338
339 .connect = sock_no_connect,
340 .socketpair = sock_no_socketpair,
341 .getname = sock_no_getname,
342 .ioctl = sock_no_ioctl,
343 .listen = sock_no_listen,
344 .shutdown = sock_no_shutdown,
345 .getsockopt = sock_no_getsockopt,
346 .mmap = sock_no_mmap,
347 .sendpage = sock_no_sendpage,
348 .sendmsg = sock_no_sendmsg,
349 .recvmsg = sock_no_recvmsg,
Herbert Xu03c8efc2010-10-19 21:12:39 +0800350
351 .bind = alg_bind,
352 .release = af_alg_release,
353 .setsockopt = alg_setsockopt,
354 .accept = alg_accept,
355};
356
357static void alg_sock_destruct(struct sock *sk)
358{
359 struct alg_sock *ask = alg_sk(sk);
360
361 alg_do_release(ask->type, ask->private);
362}
363
364static int alg_create(struct net *net, struct socket *sock, int protocol,
365 int kern)
366{
367 struct sock *sk;
368 int err;
369
370 if (sock->type != SOCK_SEQPACKET)
371 return -ESOCKTNOSUPPORT;
372 if (protocol != 0)
373 return -EPROTONOSUPPORT;
374
375 err = -ENOMEM;
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500376 sk = sk_alloc(net, PF_ALG, GFP_KERNEL, &alg_proto, kern);
Herbert Xu03c8efc2010-10-19 21:12:39 +0800377 if (!sk)
378 goto out;
379
380 sock->ops = &alg_proto_ops;
381 sock_init_data(sock, sk);
382
383 sk->sk_family = PF_ALG;
384 sk->sk_destruct = alg_sock_destruct;
385
386 return 0;
387out:
388 return err;
389}
390
391static const struct net_proto_family alg_family = {
392 .family = PF_ALG,
393 .create = alg_create,
394 .owner = THIS_MODULE,
395};
396
Al Viro1d10eb22014-11-28 16:39:25 -0500397int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len)
Herbert Xu03c8efc2010-10-19 21:12:39 +0800398{
Al Viro1d10eb22014-11-28 16:39:25 -0500399 size_t off;
400 ssize_t n;
401 int npages, i;
Herbert Xu03c8efc2010-10-19 21:12:39 +0800402
Al Viro1d10eb22014-11-28 16:39:25 -0500403 n = iov_iter_get_pages(iter, sgl->pages, len, ALG_MAX_PAGES, &off);
404 if (n < 0)
405 return n;
Herbert Xu03c8efc2010-10-19 21:12:39 +0800406
Linus Torvalds9399f0c2015-02-10 19:55:45 -0800407 npages = (off + n + PAGE_SIZE - 1) >> PAGE_SHIFT;
Herbert Xu03c8efc2010-10-19 21:12:39 +0800408 if (WARN_ON(npages == 0))
Al Viro1d10eb22014-11-28 16:39:25 -0500409 return -EINVAL;
Tadeusz Struk66db3732015-03-19 12:31:30 -0700410 /* Add one extra for linking */
411 sg_init_table(sgl->sg, npages + 1);
Herbert Xu03c8efc2010-10-19 21:12:39 +0800412
Al Viro1d10eb22014-11-28 16:39:25 -0500413 for (i = 0, len = n; i < npages; i++) {
Herbert Xu03c8efc2010-10-19 21:12:39 +0800414 int plen = min_t(int, len, PAGE_SIZE - off);
415
416 sg_set_page(sgl->sg + i, sgl->pages[i], plen, off);
417
418 off = 0;
419 len -= plen;
Herbert Xu03c8efc2010-10-19 21:12:39 +0800420 }
Tadeusz Struk66db3732015-03-19 12:31:30 -0700421 sg_mark_end(sgl->sg + npages - 1);
422 sgl->npages = npages;
423
Al Viro1d10eb22014-11-28 16:39:25 -0500424 return n;
Herbert Xu03c8efc2010-10-19 21:12:39 +0800425}
426EXPORT_SYMBOL_GPL(af_alg_make_sg);
427
Eric Biggers466e0752019-01-10 12:18:00 -0800428static void af_alg_link_sg(struct af_alg_sgl *sgl_prev,
429 struct af_alg_sgl *sgl_new)
Tadeusz Struk66db3732015-03-19 12:31:30 -0700430{
431 sg_unmark_end(sgl_prev->sg + sgl_prev->npages - 1);
432 sg_chain(sgl_prev->sg, sgl_prev->npages + 1, sgl_new->sg);
433}
Tadeusz Struk66db3732015-03-19 12:31:30 -0700434
Herbert Xu03c8efc2010-10-19 21:12:39 +0800435void af_alg_free_sg(struct af_alg_sgl *sgl)
436{
437 int i;
438
Tadeusz Struk66db3732015-03-19 12:31:30 -0700439 for (i = 0; i < sgl->npages; i++)
Herbert Xu03c8efc2010-10-19 21:12:39 +0800440 put_page(sgl->pages[i]);
Herbert Xu03c8efc2010-10-19 21:12:39 +0800441}
442EXPORT_SYMBOL_GPL(af_alg_free_sg);
443
Eric Biggers466e0752019-01-10 12:18:00 -0800444static int af_alg_cmsg_send(struct msghdr *msg, struct af_alg_control *con)
Herbert Xu03c8efc2010-10-19 21:12:39 +0800445{
446 struct cmsghdr *cmsg;
447
Gu Zhengf95b4142014-12-11 11:22:04 +0800448 for_each_cmsghdr(cmsg, msg) {
Herbert Xu03c8efc2010-10-19 21:12:39 +0800449 if (!CMSG_OK(msg, cmsg))
450 return -EINVAL;
451 if (cmsg->cmsg_level != SOL_ALG)
452 continue;
453
Joshua I. James267c4222014-12-05 14:38:40 +0900454 switch (cmsg->cmsg_type) {
Herbert Xu03c8efc2010-10-19 21:12:39 +0800455 case ALG_SET_IV:
456 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*con->iv)))
457 return -EINVAL;
458 con->iv = (void *)CMSG_DATA(cmsg);
459 if (cmsg->cmsg_len < CMSG_LEN(con->iv->ivlen +
460 sizeof(*con->iv)))
461 return -EINVAL;
462 break;
463
464 case ALG_SET_OP:
465 if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
466 return -EINVAL;
467 con->op = *(u32 *)CMSG_DATA(cmsg);
468 break;
469
Stephan Muelleraf8e8072014-12-03 20:55:42 +0100470 case ALG_SET_AEAD_ASSOCLEN:
471 if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
472 return -EINVAL;
473 con->aead_assoclen = *(u32 *)CMSG_DATA(cmsg);
474 break;
475
Herbert Xu03c8efc2010-10-19 21:12:39 +0800476 default:
477 return -EINVAL;
478 }
479 }
480
481 return 0;
482}
Herbert Xu03c8efc2010-10-19 21:12:39 +0800483
Stephan Mueller2d975912017-08-02 07:56:19 +0200484/**
485 * af_alg_alloc_tsgl - allocate the TX SGL
486 *
487 * @sk socket of connection to user space
488 * @return: 0 upon success, < 0 upon error
489 */
Eric Biggers466e0752019-01-10 12:18:00 -0800490static int af_alg_alloc_tsgl(struct sock *sk)
Stephan Mueller2d975912017-08-02 07:56:19 +0200491{
492 struct alg_sock *ask = alg_sk(sk);
493 struct af_alg_ctx *ctx = ask->private;
494 struct af_alg_tsgl *sgl;
495 struct scatterlist *sg = NULL;
496
497 sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list);
498 if (!list_empty(&ctx->tsgl_list))
499 sg = sgl->sg;
500
501 if (!sg || sgl->cur >= MAX_SGL_ENTS) {
Kees Cook0ed2dd02018-05-08 16:08:53 -0700502 sgl = sock_kmalloc(sk,
503 struct_size(sgl, sg, (MAX_SGL_ENTS + 1)),
Stephan Mueller2d975912017-08-02 07:56:19 +0200504 GFP_KERNEL);
505 if (!sgl)
506 return -ENOMEM;
507
508 sg_init_table(sgl->sg, MAX_SGL_ENTS + 1);
509 sgl->cur = 0;
510
511 if (sg)
512 sg_chain(sg, MAX_SGL_ENTS + 1, sgl->sg);
513
514 list_add_tail(&sgl->list, &ctx->tsgl_list);
515 }
516
517 return 0;
518}
Stephan Mueller2d975912017-08-02 07:56:19 +0200519
520/**
521 * aead_count_tsgl - Count number of TX SG entries
522 *
523 * The counting starts from the beginning of the SGL to @bytes. If
524 * an offset is provided, the counting of the SG entries starts at the offset.
525 *
526 * @sk socket of connection to user space
527 * @bytes Count the number of SG entries holding given number of bytes.
528 * @offset Start the counting of SG entries from the given offset.
529 * @return Number of TX SG entries found given the constraints
530 */
531unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset)
532{
533 struct alg_sock *ask = alg_sk(sk);
534 struct af_alg_ctx *ctx = ask->private;
535 struct af_alg_tsgl *sgl, *tmp;
536 unsigned int i;
537 unsigned int sgl_count = 0;
538
539 if (!bytes)
540 return 0;
541
542 list_for_each_entry_safe(sgl, tmp, &ctx->tsgl_list, list) {
543 struct scatterlist *sg = sgl->sg;
544
545 for (i = 0; i < sgl->cur; i++) {
546 size_t bytes_count;
547
548 /* Skip offset */
549 if (offset >= sg[i].length) {
550 offset -= sg[i].length;
551 bytes -= sg[i].length;
552 continue;
553 }
554
555 bytes_count = sg[i].length - offset;
556
557 offset = 0;
558 sgl_count++;
559
560 /* If we have seen requested number of bytes, stop */
561 if (bytes_count >= bytes)
562 return sgl_count;
563
564 bytes -= bytes_count;
565 }
566 }
567
568 return sgl_count;
569}
570EXPORT_SYMBOL_GPL(af_alg_count_tsgl);
571
572/**
573 * aead_pull_tsgl - Release the specified buffers from TX SGL
574 *
575 * If @dst is non-null, reassign the pages to dst. The caller must release
576 * the pages. If @dst_offset is given only reassign the pages to @dst starting
577 * at the @dst_offset (byte). The caller must ensure that @dst is large
578 * enough (e.g. by using af_alg_count_tsgl with the same offset).
579 *
580 * @sk socket of connection to user space
581 * @used Number of bytes to pull from TX SGL
582 * @dst If non-NULL, buffer is reassigned to dst SGL instead of releasing. The
583 * caller must release the buffers in dst.
584 * @dst_offset Reassign the TX SGL from given offset. All buffers before
585 * reaching the offset is released.
586 */
587void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
588 size_t dst_offset)
589{
590 struct alg_sock *ask = alg_sk(sk);
591 struct af_alg_ctx *ctx = ask->private;
592 struct af_alg_tsgl *sgl;
593 struct scatterlist *sg;
Stephan Muellere1177652017-08-30 09:17:39 +0200594 unsigned int i, j = 0;
Stephan Mueller2d975912017-08-02 07:56:19 +0200595
596 while (!list_empty(&ctx->tsgl_list)) {
597 sgl = list_first_entry(&ctx->tsgl_list, struct af_alg_tsgl,
598 list);
599 sg = sgl->sg;
600
Stephan Muellere1177652017-08-30 09:17:39 +0200601 for (i = 0; i < sgl->cur; i++) {
Stephan Mueller2d975912017-08-02 07:56:19 +0200602 size_t plen = min_t(size_t, used, sg[i].length);
603 struct page *page = sg_page(sg + i);
604
605 if (!page)
606 continue;
607
608 /*
609 * Assumption: caller created af_alg_count_tsgl(len)
610 * SG entries in dst.
611 */
612 if (dst) {
613 if (dst_offset >= plen) {
614 /* discard page before offset */
615 dst_offset -= plen;
Stephan Mueller2d975912017-08-02 07:56:19 +0200616 } else {
617 /* reassign page to dst after offset */
Stephan Mueller2d45a7e2017-08-10 16:40:03 +0200618 get_page(page);
Stephan Mueller2d975912017-08-02 07:56:19 +0200619 sg_set_page(dst + j, page,
620 plen - dst_offset,
621 sg[i].offset + dst_offset);
622 dst_offset = 0;
623 j++;
624 }
625 }
626
627 sg[i].length -= plen;
628 sg[i].offset += plen;
629
630 used -= plen;
631 ctx->used -= plen;
632
633 if (sg[i].length)
634 return;
635
Stephan Mueller2d45a7e2017-08-10 16:40:03 +0200636 put_page(page);
Stephan Mueller2d975912017-08-02 07:56:19 +0200637 sg_assign_page(sg + i, NULL);
638 }
639
640 list_del(&sgl->list);
641 sock_kfree_s(sk, sgl, sizeof(*sgl) + sizeof(sgl->sg[0]) *
642 (MAX_SGL_ENTS + 1));
643 }
644
645 if (!ctx->used)
646 ctx->merge = 0;
647}
648EXPORT_SYMBOL_GPL(af_alg_pull_tsgl);
649
650/**
651 * af_alg_free_areq_sgls - Release TX and RX SGLs of the request
652 *
653 * @areq Request holding the TX and RX SGL
654 */
Eric Biggers466e0752019-01-10 12:18:00 -0800655static void af_alg_free_areq_sgls(struct af_alg_async_req *areq)
Stephan Mueller2d975912017-08-02 07:56:19 +0200656{
657 struct sock *sk = areq->sk;
658 struct alg_sock *ask = alg_sk(sk);
659 struct af_alg_ctx *ctx = ask->private;
660 struct af_alg_rsgl *rsgl, *tmp;
661 struct scatterlist *tsgl;
662 struct scatterlist *sg;
663 unsigned int i;
664
665 list_for_each_entry_safe(rsgl, tmp, &areq->rsgl_list, list) {
Jonathan Cameronaf955bf2017-12-19 10:27:24 +0000666 atomic_sub(rsgl->sg_num_bytes, &ctx->rcvused);
Stephan Mueller2d975912017-08-02 07:56:19 +0200667 af_alg_free_sg(&rsgl->sgl);
668 list_del(&rsgl->list);
669 if (rsgl != &areq->first_rsgl)
670 sock_kfree_s(sk, rsgl, sizeof(*rsgl));
671 }
672
673 tsgl = areq->tsgl;
Eric Biggers887207e2017-11-28 00:46:24 -0800674 if (tsgl) {
675 for_each_sg(tsgl, sg, areq->tsgl_entries, i) {
676 if (!sg_page(sg))
677 continue;
678 put_page(sg_page(sg));
679 }
Stephan Mueller2d975912017-08-02 07:56:19 +0200680
Stephan Mueller2d975912017-08-02 07:56:19 +0200681 sock_kfree_s(sk, tsgl, areq->tsgl_entries * sizeof(*tsgl));
Eric Biggers887207e2017-11-28 00:46:24 -0800682 }
Stephan Mueller2d975912017-08-02 07:56:19 +0200683}
Stephan Mueller2d975912017-08-02 07:56:19 +0200684
685/**
686 * af_alg_wait_for_wmem - wait for availability of writable memory
687 *
688 * @sk socket of connection to user space
689 * @flags If MSG_DONTWAIT is set, then only report if function would sleep
690 * @return 0 when writable memory is available, < 0 upon error
691 */
Eric Biggers466e0752019-01-10 12:18:00 -0800692static int af_alg_wait_for_wmem(struct sock *sk, unsigned int flags)
Stephan Mueller2d975912017-08-02 07:56:19 +0200693{
694 DEFINE_WAIT_FUNC(wait, woken_wake_function);
695 int err = -ERESTARTSYS;
696 long timeout;
697
698 if (flags & MSG_DONTWAIT)
699 return -EAGAIN;
700
701 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
702
703 add_wait_queue(sk_sleep(sk), &wait);
704 for (;;) {
705 if (signal_pending(current))
706 break;
707 timeout = MAX_SCHEDULE_TIMEOUT;
708 if (sk_wait_event(sk, &timeout, af_alg_writable(sk), &wait)) {
709 err = 0;
710 break;
711 }
712 }
713 remove_wait_queue(sk_sleep(sk), &wait);
714
715 return err;
716}
Stephan Mueller2d975912017-08-02 07:56:19 +0200717
718/**
719 * af_alg_wmem_wakeup - wakeup caller when writable memory is available
720 *
721 * @sk socket of connection to user space
722 */
723void af_alg_wmem_wakeup(struct sock *sk)
724{
725 struct socket_wq *wq;
726
727 if (!af_alg_writable(sk))
728 return;
729
730 rcu_read_lock();
731 wq = rcu_dereference(sk->sk_wq);
732 if (skwq_has_sleeper(wq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800733 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
734 EPOLLRDNORM |
735 EPOLLRDBAND);
Stephan Mueller2d975912017-08-02 07:56:19 +0200736 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
737 rcu_read_unlock();
738}
739EXPORT_SYMBOL_GPL(af_alg_wmem_wakeup);
740
741/**
742 * af_alg_wait_for_data - wait for availability of TX data
743 *
744 * @sk socket of connection to user space
745 * @flags If MSG_DONTWAIT is set, then only report if function would sleep
746 * @return 0 when writable memory is available, < 0 upon error
747 */
748int af_alg_wait_for_data(struct sock *sk, unsigned flags)
749{
750 DEFINE_WAIT_FUNC(wait, woken_wake_function);
751 struct alg_sock *ask = alg_sk(sk);
752 struct af_alg_ctx *ctx = ask->private;
753 long timeout;
754 int err = -ERESTARTSYS;
755
756 if (flags & MSG_DONTWAIT)
757 return -EAGAIN;
758
759 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
760
761 add_wait_queue(sk_sleep(sk), &wait);
762 for (;;) {
763 if (signal_pending(current))
764 break;
765 timeout = MAX_SCHEDULE_TIMEOUT;
766 if (sk_wait_event(sk, &timeout, (ctx->used || !ctx->more),
767 &wait)) {
768 err = 0;
769 break;
770 }
771 }
772 remove_wait_queue(sk_sleep(sk), &wait);
773
774 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
775
776 return err;
777}
778EXPORT_SYMBOL_GPL(af_alg_wait_for_data);
779
780/**
781 * af_alg_data_wakeup - wakeup caller when new data can be sent to kernel
782 *
783 * @sk socket of connection to user space
784 */
Eric Biggers466e0752019-01-10 12:18:00 -0800785static void af_alg_data_wakeup(struct sock *sk)
Stephan Mueller2d975912017-08-02 07:56:19 +0200786{
787 struct alg_sock *ask = alg_sk(sk);
788 struct af_alg_ctx *ctx = ask->private;
789 struct socket_wq *wq;
790
791 if (!ctx->used)
792 return;
793
794 rcu_read_lock();
795 wq = rcu_dereference(sk->sk_wq);
796 if (skwq_has_sleeper(wq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800797 wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
798 EPOLLRDNORM |
799 EPOLLRDBAND);
Stephan Mueller2d975912017-08-02 07:56:19 +0200800 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
801 rcu_read_unlock();
802}
Stephan Mueller2d975912017-08-02 07:56:19 +0200803
804/**
805 * af_alg_sendmsg - implementation of sendmsg system call handler
806 *
807 * The sendmsg system call handler obtains the user data and stores it
808 * in ctx->tsgl_list. This implies allocation of the required numbers of
809 * struct af_alg_tsgl.
810 *
811 * In addition, the ctx is filled with the information sent via CMSG.
812 *
813 * @sock socket of connection to user space
814 * @msg message from user space
815 * @size size of message from user space
816 * @ivsize the size of the IV for the cipher operation to verify that the
817 * user-space-provided IV has the right size
818 * @return the number of copied data upon success, < 0 upon error
819 */
820int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
821 unsigned int ivsize)
822{
823 struct sock *sk = sock->sk;
824 struct alg_sock *ask = alg_sk(sk);
825 struct af_alg_ctx *ctx = ask->private;
826 struct af_alg_tsgl *sgl;
827 struct af_alg_control con = {};
828 long copied = 0;
829 bool enc = 0;
830 bool init = 0;
831 int err = 0;
832
833 if (msg->msg_controllen) {
834 err = af_alg_cmsg_send(msg, &con);
835 if (err)
836 return err;
837
838 init = 1;
839 switch (con.op) {
840 case ALG_OP_ENCRYPT:
841 enc = 1;
842 break;
843 case ALG_OP_DECRYPT:
844 enc = 0;
845 break;
846 default:
847 return -EINVAL;
848 }
849
850 if (con.iv && con.iv->ivlen != ivsize)
851 return -EINVAL;
852 }
853
854 lock_sock(sk);
855 if (!ctx->more && ctx->used) {
856 err = -EINVAL;
857 goto unlock;
858 }
859
860 if (init) {
861 ctx->enc = enc;
862 if (con.iv)
863 memcpy(ctx->iv, con.iv->iv, ivsize);
864
865 ctx->aead_assoclen = con.aead_assoclen;
866 }
867
868 while (size) {
869 struct scatterlist *sg;
870 size_t len = size;
871 size_t plen;
872
873 /* use the existing memory in an allocated page */
874 if (ctx->merge) {
875 sgl = list_entry(ctx->tsgl_list.prev,
876 struct af_alg_tsgl, list);
877 sg = sgl->sg + sgl->cur - 1;
878 len = min_t(size_t, len,
879 PAGE_SIZE - sg->offset - sg->length);
880
881 err = memcpy_from_msg(page_address(sg_page(sg)) +
882 sg->offset + sg->length,
883 msg, len);
884 if (err)
885 goto unlock;
886
887 sg->length += len;
888 ctx->merge = (sg->offset + sg->length) &
889 (PAGE_SIZE - 1);
890
891 ctx->used += len;
892 copied += len;
893 size -= len;
894 continue;
895 }
896
897 if (!af_alg_writable(sk)) {
898 err = af_alg_wait_for_wmem(sk, msg->msg_flags);
899 if (err)
900 goto unlock;
901 }
902
903 /* allocate a new page */
904 len = min_t(unsigned long, len, af_alg_sndbuf(sk));
905
906 err = af_alg_alloc_tsgl(sk);
907 if (err)
908 goto unlock;
909
910 sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl,
911 list);
912 sg = sgl->sg;
913 if (sgl->cur)
914 sg_unmark_end(sg + sgl->cur - 1);
915
916 do {
917 unsigned int i = sgl->cur;
918
919 plen = min_t(size_t, len, PAGE_SIZE);
920
921 sg_assign_page(sg + i, alloc_page(GFP_KERNEL));
922 if (!sg_page(sg + i)) {
923 err = -ENOMEM;
924 goto unlock;
925 }
926
927 err = memcpy_from_msg(page_address(sg_page(sg + i)),
928 msg, plen);
929 if (err) {
930 __free_page(sg_page(sg + i));
931 sg_assign_page(sg + i, NULL);
932 goto unlock;
933 }
934
935 sg[i].length = plen;
936 len -= plen;
937 ctx->used += plen;
938 copied += plen;
939 size -= plen;
940 sgl->cur++;
941 } while (len && sgl->cur < MAX_SGL_ENTS);
942
943 if (!size)
944 sg_mark_end(sg + sgl->cur - 1);
945
946 ctx->merge = plen & (PAGE_SIZE - 1);
947 }
948
949 err = 0;
950
951 ctx->more = msg->msg_flags & MSG_MORE;
952
953unlock:
954 af_alg_data_wakeup(sk);
955 release_sock(sk);
956
957 return copied ?: err;
958}
959EXPORT_SYMBOL_GPL(af_alg_sendmsg);
960
961/**
962 * af_alg_sendpage - sendpage system call handler
963 *
964 * This is a generic implementation of sendpage to fill ctx->tsgl_list.
965 */
966ssize_t af_alg_sendpage(struct socket *sock, struct page *page,
967 int offset, size_t size, int flags)
968{
969 struct sock *sk = sock->sk;
970 struct alg_sock *ask = alg_sk(sk);
971 struct af_alg_ctx *ctx = ask->private;
972 struct af_alg_tsgl *sgl;
973 int err = -EINVAL;
974
975 if (flags & MSG_SENDPAGE_NOTLAST)
976 flags |= MSG_MORE;
977
978 lock_sock(sk);
979 if (!ctx->more && ctx->used)
980 goto unlock;
981
982 if (!size)
983 goto done;
984
985 if (!af_alg_writable(sk)) {
986 err = af_alg_wait_for_wmem(sk, flags);
987 if (err)
988 goto unlock;
989 }
990
991 err = af_alg_alloc_tsgl(sk);
992 if (err)
993 goto unlock;
994
995 ctx->merge = 0;
996 sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list);
997
998 if (sgl->cur)
999 sg_unmark_end(sgl->sg + sgl->cur - 1);
1000
1001 sg_mark_end(sgl->sg + sgl->cur);
1002
1003 get_page(page);
1004 sg_set_page(sgl->sg + sgl->cur, page, size, offset);
1005 sgl->cur++;
1006 ctx->used += size;
1007
1008done:
1009 ctx->more = flags & MSG_MORE;
1010
1011unlock:
1012 af_alg_data_wakeup(sk);
1013 release_sock(sk);
1014
1015 return err ?: size;
1016}
1017EXPORT_SYMBOL_GPL(af_alg_sendpage);
1018
1019/**
Stephan Mueller7d2c3f52017-11-10 13:20:55 +01001020 * af_alg_free_resources - release resources required for crypto request
1021 */
1022void af_alg_free_resources(struct af_alg_async_req *areq)
1023{
1024 struct sock *sk = areq->sk;
1025
1026 af_alg_free_areq_sgls(areq);
1027 sock_kfree_s(sk, areq, areq->areqlen);
1028}
1029EXPORT_SYMBOL_GPL(af_alg_free_resources);
1030
1031/**
Stephan Mueller2d975912017-08-02 07:56:19 +02001032 * af_alg_async_cb - AIO callback handler
1033 *
1034 * This handler cleans up the struct af_alg_async_req upon completion of the
1035 * AIO operation.
1036 *
1037 * The number of bytes to be generated with the AIO operation must be set
1038 * in areq->outlen before the AIO callback handler is invoked.
1039 */
1040void af_alg_async_cb(struct crypto_async_request *_req, int err)
1041{
1042 struct af_alg_async_req *areq = _req->data;
1043 struct sock *sk = areq->sk;
1044 struct kiocb *iocb = areq->iocb;
1045 unsigned int resultlen;
1046
Stephan Mueller2d975912017-08-02 07:56:19 +02001047 /* Buffer size written by crypto operation. */
1048 resultlen = areq->outlen;
1049
Stephan Mueller7d2c3f52017-11-10 13:20:55 +01001050 af_alg_free_resources(areq);
1051 sock_put(sk);
Stephan Mueller2d975912017-08-02 07:56:19 +02001052
1053 iocb->ki_complete(iocb, err ? err : resultlen, 0);
Stephan Mueller2d975912017-08-02 07:56:19 +02001054}
1055EXPORT_SYMBOL_GPL(af_alg_async_cb);
1056
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001057/**
1058 * af_alg_poll - poll system call handler
1059 */
1060__poll_t af_alg_poll(struct file *file, struct socket *sock,
1061 poll_table *wait)
Stephan Mueller2d975912017-08-02 07:56:19 +02001062{
1063 struct sock *sk = sock->sk;
1064 struct alg_sock *ask = alg_sk(sk);
1065 struct af_alg_ctx *ctx = ask->private;
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001066 __poll_t mask;
1067
Karsten Graul89ab0662018-10-23 13:40:39 +02001068 sock_poll_wait(file, sock, wait);
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001069 mask = 0;
Stephan Mueller2d975912017-08-02 07:56:19 +02001070
1071 if (!ctx->more || ctx->used)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001072 mask |= EPOLLIN | EPOLLRDNORM;
Stephan Mueller2d975912017-08-02 07:56:19 +02001073
1074 if (af_alg_writable(sk))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001075 mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
Stephan Mueller2d975912017-08-02 07:56:19 +02001076
1077 return mask;
1078}
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001079EXPORT_SYMBOL_GPL(af_alg_poll);
Stephan Mueller2d975912017-08-02 07:56:19 +02001080
1081/**
1082 * af_alg_alloc_areq - allocate struct af_alg_async_req
1083 *
1084 * @sk socket of connection to user space
1085 * @areqlen size of struct af_alg_async_req + crypto_*_reqsize
1086 * @return allocated data structure or ERR_PTR upon error
1087 */
1088struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
1089 unsigned int areqlen)
1090{
1091 struct af_alg_async_req *areq = sock_kmalloc(sk, areqlen, GFP_KERNEL);
1092
1093 if (unlikely(!areq))
1094 return ERR_PTR(-ENOMEM);
1095
1096 areq->areqlen = areqlen;
1097 areq->sk = sk;
1098 areq->last_rsgl = NULL;
1099 INIT_LIST_HEAD(&areq->rsgl_list);
1100 areq->tsgl = NULL;
1101 areq->tsgl_entries = 0;
1102
1103 return areq;
1104}
1105EXPORT_SYMBOL_GPL(af_alg_alloc_areq);
1106
1107/**
1108 * af_alg_get_rsgl - create the RX SGL for the output data from the crypto
1109 * operation
1110 *
1111 * @sk socket of connection to user space
1112 * @msg user space message
1113 * @flags flags used to invoke recvmsg with
1114 * @areq instance of the cryptographic request that will hold the RX SGL
1115 * @maxsize maximum number of bytes to be pulled from user space
1116 * @outlen number of bytes in the RX SGL
1117 * @return 0 on success, < 0 upon error
1118 */
1119int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
1120 struct af_alg_async_req *areq, size_t maxsize,
1121 size_t *outlen)
1122{
1123 struct alg_sock *ask = alg_sk(sk);
1124 struct af_alg_ctx *ctx = ask->private;
1125 size_t len = 0;
1126
1127 while (maxsize > len && msg_data_left(msg)) {
1128 struct af_alg_rsgl *rsgl;
1129 size_t seglen;
1130 int err;
1131
1132 /* limit the amount of readable buffers */
1133 if (!af_alg_readable(sk))
1134 break;
1135
Stephan Mueller2d975912017-08-02 07:56:19 +02001136 seglen = min_t(size_t, (maxsize - len),
1137 msg_data_left(msg));
1138
1139 if (list_empty(&areq->rsgl_list)) {
1140 rsgl = &areq->first_rsgl;
1141 } else {
1142 rsgl = sock_kmalloc(sk, sizeof(*rsgl), GFP_KERNEL);
1143 if (unlikely(!rsgl))
1144 return -ENOMEM;
1145 }
1146
1147 rsgl->sgl.npages = 0;
1148 list_add_tail(&rsgl->list, &areq->rsgl_list);
1149
1150 /* make one iovec available as scatterlist */
1151 err = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, seglen);
Stephan Mueller2546da92018-07-07 20:41:47 +02001152 if (err < 0) {
1153 rsgl->sg_num_bytes = 0;
Stephan Mueller2d975912017-08-02 07:56:19 +02001154 return err;
Stephan Mueller2546da92018-07-07 20:41:47 +02001155 }
Stephan Mueller2d975912017-08-02 07:56:19 +02001156
1157 /* chain the new scatterlist with previous one */
1158 if (areq->last_rsgl)
1159 af_alg_link_sg(&areq->last_rsgl->sgl, &rsgl->sgl);
1160
1161 areq->last_rsgl = rsgl;
1162 len += err;
Jonathan Cameronaf955bf2017-12-19 10:27:24 +00001163 atomic_add(err, &ctx->rcvused);
Stephan Mueller2d975912017-08-02 07:56:19 +02001164 rsgl->sg_num_bytes = err;
1165 iov_iter_advance(&msg->msg_iter, err);
1166 }
1167
1168 *outlen = len;
1169 return 0;
1170}
1171EXPORT_SYMBOL_GPL(af_alg_get_rsgl);
1172
Herbert Xu03c8efc2010-10-19 21:12:39 +08001173static int __init af_alg_init(void)
1174{
1175 int err = proto_register(&alg_proto, 0);
1176
1177 if (err)
1178 goto out;
1179
1180 err = sock_register(&alg_family);
1181 if (err != 0)
1182 goto out_unregister_proto;
1183
1184out:
1185 return err;
1186
1187out_unregister_proto:
1188 proto_unregister(&alg_proto);
1189 goto out;
1190}
1191
1192static void __exit af_alg_exit(void)
1193{
1194 sock_unregister(PF_ALG);
1195 proto_unregister(&alg_proto);
1196}
1197
1198module_init(af_alg_init);
1199module_exit(af_alg_exit);
1200MODULE_LICENSE("GPL");
1201MODULE_ALIAS_NETPROTO(AF_ALG);