]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - crypto/af_alg.c
crypto: api - Check spawn->alg under lock in crypto_drop_spawn
[mirror_ubuntu-bionic-kernel.git] / crypto / af_alg.c
1 /*
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
15 #include <linux/atomic.h>
16 #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>
24 #include <linux/sched/signal.h>
25 #include <linux/security.h>
26
27 struct alg_type_list {
28 const struct af_alg_type *type;
29 struct list_head list;
30 };
31
32 static atomic_long_t alg_memory_allocated;
33
34 static 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
41 static LIST_HEAD(alg_types);
42 static DECLARE_RWSEM(alg_types_sem);
43
44 static 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
63 int 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;
80 if (type->ops_nokey)
81 type->ops_nokey->owner = THIS_MODULE;
82 node->type = type;
83 list_add(&node->list, &alg_types);
84 err = 0;
85
86 unlock:
87 up_write(&alg_types_sem);
88
89 return err;
90 }
91 EXPORT_SYMBOL_GPL(af_alg_register_type);
92
93 int 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 }
112 EXPORT_SYMBOL_GPL(af_alg_unregister_type);
113
114 static 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
123 int af_alg_release(struct socket *sock)
124 {
125 if (sock->sk) {
126 sock_put(sock->sk);
127 sock->sk = NULL;
128 }
129 return 0;
130 }
131 EXPORT_SYMBOL_GPL(af_alg_release);
132
133 void af_alg_release_parent(struct sock *sk)
134 {
135 struct alg_sock *ask = alg_sk(sk);
136 unsigned int nokey = ask->nokey_refcnt;
137 bool last = nokey && !ask->refcnt;
138
139 sk = ask->parent;
140 ask = alg_sk(sk);
141
142 local_bh_disable();
143 bh_lock_sock(sk);
144 ask->nokey_refcnt -= nokey;
145 if (!last)
146 last = !--ask->refcnt;
147 bh_unlock_sock(sk);
148 local_bh_enable();
149
150 if (last)
151 sock_put(sk);
152 }
153 EXPORT_SYMBOL_GPL(af_alg_release_parent);
154
155 static int alg_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
156 {
157 const u32 allowed = CRYPTO_ALG_KERN_DRIVER_ONLY;
158 struct sock *sk = sock->sk;
159 struct alg_sock *ask = alg_sk(sk);
160 struct sockaddr_alg *sa = (void *)uaddr;
161 const struct af_alg_type *type;
162 void *private;
163 int err;
164
165 if (sock->state == SS_CONNECTED)
166 return -EINVAL;
167
168 if (addr_len < sizeof(*sa))
169 return -EINVAL;
170
171 /* If caller uses non-allowed flag, return error. */
172 if ((sa->salg_feat & ~allowed) || (sa->salg_mask & ~allowed))
173 return -EINVAL;
174
175 sa->salg_type[sizeof(sa->salg_type) - 1] = 0;
176 sa->salg_name[sizeof(sa->salg_name) + addr_len - sizeof(*sa) - 1] = 0;
177
178 type = alg_get_type(sa->salg_type);
179 if (IS_ERR(type) && PTR_ERR(type) == -ENOENT) {
180 request_module("algif-%s", sa->salg_type);
181 type = alg_get_type(sa->salg_type);
182 }
183
184 if (IS_ERR(type))
185 return PTR_ERR(type);
186
187 private = type->bind(sa->salg_name, sa->salg_feat, sa->salg_mask);
188 if (IS_ERR(private)) {
189 module_put(type->owner);
190 return PTR_ERR(private);
191 }
192
193 err = -EBUSY;
194 lock_sock(sk);
195 if (ask->refcnt | ask->nokey_refcnt)
196 goto unlock;
197
198 swap(ask->type, type);
199 swap(ask->private, private);
200
201 err = 0;
202
203 unlock:
204 release_sock(sk);
205
206 alg_do_release(type, private);
207
208 return err;
209 }
210
211 static int alg_setkey(struct sock *sk, char __user *ukey,
212 unsigned int keylen)
213 {
214 struct alg_sock *ask = alg_sk(sk);
215 const struct af_alg_type *type = ask->type;
216 u8 *key;
217 int err;
218
219 key = sock_kmalloc(sk, keylen, GFP_KERNEL);
220 if (!key)
221 return -ENOMEM;
222
223 err = -EFAULT;
224 if (copy_from_user(key, ukey, keylen))
225 goto out;
226
227 err = type->setkey(ask->private, key, keylen);
228
229 out:
230 sock_kzfree_s(sk, key, keylen);
231
232 return err;
233 }
234
235 static int alg_setsockopt(struct socket *sock, int level, int optname,
236 char __user *optval, unsigned int optlen)
237 {
238 struct sock *sk = sock->sk;
239 struct alg_sock *ask = alg_sk(sk);
240 const struct af_alg_type *type;
241 int err = -EBUSY;
242
243 lock_sock(sk);
244 if (ask->refcnt)
245 goto unlock;
246
247 type = ask->type;
248
249 err = -ENOPROTOOPT;
250 if (level != SOL_ALG || !type)
251 goto unlock;
252
253 switch (optname) {
254 case ALG_SET_KEY:
255 if (sock->state == SS_CONNECTED)
256 goto unlock;
257 if (!type->setkey)
258 goto unlock;
259
260 err = alg_setkey(sk, optval, optlen);
261 break;
262 case ALG_SET_AEAD_AUTHSIZE:
263 if (sock->state == SS_CONNECTED)
264 goto unlock;
265 if (!type->setauthsize)
266 goto unlock;
267 err = type->setauthsize(ask->private, optlen);
268 }
269
270 unlock:
271 release_sock(sk);
272
273 return err;
274 }
275
276 int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern)
277 {
278 struct alg_sock *ask = alg_sk(sk);
279 const struct af_alg_type *type;
280 struct sock *sk2;
281 unsigned int nokey;
282 int err;
283
284 lock_sock(sk);
285 type = ask->type;
286
287 err = -EINVAL;
288 if (!type)
289 goto unlock;
290
291 sk2 = sk_alloc(sock_net(sk), PF_ALG, GFP_KERNEL, &alg_proto, kern);
292 err = -ENOMEM;
293 if (!sk2)
294 goto unlock;
295
296 sock_init_data(newsock, sk2);
297 security_sock_graft(sk2, newsock);
298 security_sk_clone(sk, sk2);
299
300 err = type->accept(ask->private, sk2);
301
302 nokey = err == -ENOKEY;
303 if (nokey && type->accept_nokey)
304 err = type->accept_nokey(ask->private, sk2);
305
306 if (err)
307 goto unlock;
308
309 sk2->sk_family = PF_ALG;
310
311 if (nokey || !ask->refcnt++)
312 sock_hold(sk);
313 ask->nokey_refcnt += nokey;
314 alg_sk(sk2)->parent = sk;
315 alg_sk(sk2)->type = type;
316 alg_sk(sk2)->nokey_refcnt = nokey;
317
318 newsock->ops = type->ops;
319 newsock->state = SS_CONNECTED;
320
321 if (nokey)
322 newsock->ops = type->ops_nokey;
323
324 err = 0;
325
326 unlock:
327 release_sock(sk);
328
329 return err;
330 }
331 EXPORT_SYMBOL_GPL(af_alg_accept);
332
333 static int alg_accept(struct socket *sock, struct socket *newsock, int flags,
334 bool kern)
335 {
336 return af_alg_accept(sock->sk, newsock, kern);
337 }
338
339 static const struct proto_ops alg_proto_ops = {
340 .family = PF_ALG,
341 .owner = THIS_MODULE,
342
343 .connect = sock_no_connect,
344 .socketpair = sock_no_socketpair,
345 .getname = sock_no_getname,
346 .ioctl = sock_no_ioctl,
347 .listen = sock_no_listen,
348 .shutdown = sock_no_shutdown,
349 .getsockopt = sock_no_getsockopt,
350 .mmap = sock_no_mmap,
351 .sendpage = sock_no_sendpage,
352 .sendmsg = sock_no_sendmsg,
353 .recvmsg = sock_no_recvmsg,
354 .poll = sock_no_poll,
355
356 .bind = alg_bind,
357 .release = af_alg_release,
358 .setsockopt = alg_setsockopt,
359 .accept = alg_accept,
360 };
361
362 static void alg_sock_destruct(struct sock *sk)
363 {
364 struct alg_sock *ask = alg_sk(sk);
365
366 alg_do_release(ask->type, ask->private);
367 }
368
369 static int alg_create(struct net *net, struct socket *sock, int protocol,
370 int kern)
371 {
372 struct sock *sk;
373 int err;
374
375 if (sock->type != SOCK_SEQPACKET)
376 return -ESOCKTNOSUPPORT;
377 if (protocol != 0)
378 return -EPROTONOSUPPORT;
379
380 err = -ENOMEM;
381 sk = sk_alloc(net, PF_ALG, GFP_KERNEL, &alg_proto, kern);
382 if (!sk)
383 goto out;
384
385 sock->ops = &alg_proto_ops;
386 sock_init_data(sock, sk);
387
388 sk->sk_family = PF_ALG;
389 sk->sk_destruct = alg_sock_destruct;
390
391 return 0;
392 out:
393 return err;
394 }
395
396 static const struct net_proto_family alg_family = {
397 .family = PF_ALG,
398 .create = alg_create,
399 .owner = THIS_MODULE,
400 };
401
402 int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len)
403 {
404 size_t off;
405 ssize_t n;
406 int npages, i;
407
408 n = iov_iter_get_pages(iter, sgl->pages, len, ALG_MAX_PAGES, &off);
409 if (n < 0)
410 return n;
411
412 npages = (off + n + PAGE_SIZE - 1) >> PAGE_SHIFT;
413 if (WARN_ON(npages == 0))
414 return -EINVAL;
415 /* Add one extra for linking */
416 sg_init_table(sgl->sg, npages + 1);
417
418 for (i = 0, len = n; i < npages; i++) {
419 int plen = min_t(int, len, PAGE_SIZE - off);
420
421 sg_set_page(sgl->sg + i, sgl->pages[i], plen, off);
422
423 off = 0;
424 len -= plen;
425 }
426 sg_mark_end(sgl->sg + npages - 1);
427 sgl->npages = npages;
428
429 return n;
430 }
431 EXPORT_SYMBOL_GPL(af_alg_make_sg);
432
433 void af_alg_link_sg(struct af_alg_sgl *sgl_prev, struct af_alg_sgl *sgl_new)
434 {
435 sg_unmark_end(sgl_prev->sg + sgl_prev->npages - 1);
436 sg_chain(sgl_prev->sg, sgl_prev->npages + 1, sgl_new->sg);
437 }
438 EXPORT_SYMBOL_GPL(af_alg_link_sg);
439
440 void af_alg_free_sg(struct af_alg_sgl *sgl)
441 {
442 int i;
443
444 for (i = 0; i < sgl->npages; i++)
445 put_page(sgl->pages[i]);
446 }
447 EXPORT_SYMBOL_GPL(af_alg_free_sg);
448
449 int af_alg_cmsg_send(struct msghdr *msg, struct af_alg_control *con)
450 {
451 struct cmsghdr *cmsg;
452
453 for_each_cmsghdr(cmsg, msg) {
454 if (!CMSG_OK(msg, cmsg))
455 return -EINVAL;
456 if (cmsg->cmsg_level != SOL_ALG)
457 continue;
458
459 switch (cmsg->cmsg_type) {
460 case ALG_SET_IV:
461 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*con->iv)))
462 return -EINVAL;
463 con->iv = (void *)CMSG_DATA(cmsg);
464 if (cmsg->cmsg_len < CMSG_LEN(con->iv->ivlen +
465 sizeof(*con->iv)))
466 return -EINVAL;
467 break;
468
469 case ALG_SET_OP:
470 if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
471 return -EINVAL;
472 con->op = *(u32 *)CMSG_DATA(cmsg);
473 break;
474
475 case ALG_SET_AEAD_ASSOCLEN:
476 if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
477 return -EINVAL;
478 con->aead_assoclen = *(u32 *)CMSG_DATA(cmsg);
479 break;
480
481 default:
482 return -EINVAL;
483 }
484 }
485
486 return 0;
487 }
488 EXPORT_SYMBOL_GPL(af_alg_cmsg_send);
489
490 /**
491 * af_alg_alloc_tsgl - allocate the TX SGL
492 *
493 * @sk socket of connection to user space
494 * @return: 0 upon success, < 0 upon error
495 */
496 int af_alg_alloc_tsgl(struct sock *sk)
497 {
498 struct alg_sock *ask = alg_sk(sk);
499 struct af_alg_ctx *ctx = ask->private;
500 struct af_alg_tsgl *sgl;
501 struct scatterlist *sg = NULL;
502
503 sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list);
504 if (!list_empty(&ctx->tsgl_list))
505 sg = sgl->sg;
506
507 if (!sg || sgl->cur >= MAX_SGL_ENTS) {
508 sgl = sock_kmalloc(sk, sizeof(*sgl) +
509 sizeof(sgl->sg[0]) * (MAX_SGL_ENTS + 1),
510 GFP_KERNEL);
511 if (!sgl)
512 return -ENOMEM;
513
514 sg_init_table(sgl->sg, MAX_SGL_ENTS + 1);
515 sgl->cur = 0;
516
517 if (sg)
518 sg_chain(sg, MAX_SGL_ENTS + 1, sgl->sg);
519
520 list_add_tail(&sgl->list, &ctx->tsgl_list);
521 }
522
523 return 0;
524 }
525 EXPORT_SYMBOL_GPL(af_alg_alloc_tsgl);
526
527 /**
528 * aead_count_tsgl - Count number of TX SG entries
529 *
530 * The counting starts from the beginning of the SGL to @bytes. If
531 * an offset is provided, the counting of the SG entries starts at the offset.
532 *
533 * @sk socket of connection to user space
534 * @bytes Count the number of SG entries holding given number of bytes.
535 * @offset Start the counting of SG entries from the given offset.
536 * @return Number of TX SG entries found given the constraints
537 */
538 unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset)
539 {
540 struct alg_sock *ask = alg_sk(sk);
541 struct af_alg_ctx *ctx = ask->private;
542 struct af_alg_tsgl *sgl, *tmp;
543 unsigned int i;
544 unsigned int sgl_count = 0;
545
546 if (!bytes)
547 return 0;
548
549 list_for_each_entry_safe(sgl, tmp, &ctx->tsgl_list, list) {
550 struct scatterlist *sg = sgl->sg;
551
552 for (i = 0; i < sgl->cur; i++) {
553 size_t bytes_count;
554
555 /* Skip offset */
556 if (offset >= sg[i].length) {
557 offset -= sg[i].length;
558 bytes -= sg[i].length;
559 continue;
560 }
561
562 bytes_count = sg[i].length - offset;
563
564 offset = 0;
565 sgl_count++;
566
567 /* If we have seen requested number of bytes, stop */
568 if (bytes_count >= bytes)
569 return sgl_count;
570
571 bytes -= bytes_count;
572 }
573 }
574
575 return sgl_count;
576 }
577 EXPORT_SYMBOL_GPL(af_alg_count_tsgl);
578
579 /**
580 * aead_pull_tsgl - Release the specified buffers from TX SGL
581 *
582 * If @dst is non-null, reassign the pages to dst. The caller must release
583 * the pages. If @dst_offset is given only reassign the pages to @dst starting
584 * at the @dst_offset (byte). The caller must ensure that @dst is large
585 * enough (e.g. by using af_alg_count_tsgl with the same offset).
586 *
587 * @sk socket of connection to user space
588 * @used Number of bytes to pull from TX SGL
589 * @dst If non-NULL, buffer is reassigned to dst SGL instead of releasing. The
590 * caller must release the buffers in dst.
591 * @dst_offset Reassign the TX SGL from given offset. All buffers before
592 * reaching the offset is released.
593 */
594 void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
595 size_t dst_offset)
596 {
597 struct alg_sock *ask = alg_sk(sk);
598 struct af_alg_ctx *ctx = ask->private;
599 struct af_alg_tsgl *sgl;
600 struct scatterlist *sg;
601 unsigned int i, j = 0;
602
603 while (!list_empty(&ctx->tsgl_list)) {
604 sgl = list_first_entry(&ctx->tsgl_list, struct af_alg_tsgl,
605 list);
606 sg = sgl->sg;
607
608 for (i = 0; i < sgl->cur; i++) {
609 size_t plen = min_t(size_t, used, sg[i].length);
610 struct page *page = sg_page(sg + i);
611
612 if (!page)
613 continue;
614
615 /*
616 * Assumption: caller created af_alg_count_tsgl(len)
617 * SG entries in dst.
618 */
619 if (dst) {
620 if (dst_offset >= plen) {
621 /* discard page before offset */
622 dst_offset -= plen;
623 } else {
624 /* reassign page to dst after offset */
625 get_page(page);
626 sg_set_page(dst + j, page,
627 plen - dst_offset,
628 sg[i].offset + dst_offset);
629 dst_offset = 0;
630 j++;
631 }
632 }
633
634 sg[i].length -= plen;
635 sg[i].offset += plen;
636
637 used -= plen;
638 ctx->used -= plen;
639
640 if (sg[i].length)
641 return;
642
643 put_page(page);
644 sg_assign_page(sg + i, NULL);
645 }
646
647 list_del(&sgl->list);
648 sock_kfree_s(sk, sgl, sizeof(*sgl) + sizeof(sgl->sg[0]) *
649 (MAX_SGL_ENTS + 1));
650 }
651
652 if (!ctx->used)
653 ctx->merge = 0;
654 }
655 EXPORT_SYMBOL_GPL(af_alg_pull_tsgl);
656
657 /**
658 * af_alg_free_areq_sgls - Release TX and RX SGLs of the request
659 *
660 * @areq Request holding the TX and RX SGL
661 */
662 void af_alg_free_areq_sgls(struct af_alg_async_req *areq)
663 {
664 struct sock *sk = areq->sk;
665 struct alg_sock *ask = alg_sk(sk);
666 struct af_alg_ctx *ctx = ask->private;
667 struct af_alg_rsgl *rsgl, *tmp;
668 struct scatterlist *tsgl;
669 struct scatterlist *sg;
670 unsigned int i;
671
672 list_for_each_entry_safe(rsgl, tmp, &areq->rsgl_list, list) {
673 atomic_sub(rsgl->sg_num_bytes, &ctx->rcvused);
674 af_alg_free_sg(&rsgl->sgl);
675 list_del(&rsgl->list);
676 if (rsgl != &areq->first_rsgl)
677 sock_kfree_s(sk, rsgl, sizeof(*rsgl));
678 }
679
680 tsgl = areq->tsgl;
681 if (tsgl) {
682 for_each_sg(tsgl, sg, areq->tsgl_entries, i) {
683 if (!sg_page(sg))
684 continue;
685 put_page(sg_page(sg));
686 }
687
688 sock_kfree_s(sk, tsgl, areq->tsgl_entries * sizeof(*tsgl));
689 }
690 }
691 EXPORT_SYMBOL_GPL(af_alg_free_areq_sgls);
692
693 /**
694 * af_alg_wait_for_wmem - wait for availability of writable memory
695 *
696 * @sk socket of connection to user space
697 * @flags If MSG_DONTWAIT is set, then only report if function would sleep
698 * @return 0 when writable memory is available, < 0 upon error
699 */
700 int af_alg_wait_for_wmem(struct sock *sk, unsigned int flags)
701 {
702 DEFINE_WAIT_FUNC(wait, woken_wake_function);
703 int err = -ERESTARTSYS;
704 long timeout;
705
706 if (flags & MSG_DONTWAIT)
707 return -EAGAIN;
708
709 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
710
711 add_wait_queue(sk_sleep(sk), &wait);
712 for (;;) {
713 if (signal_pending(current))
714 break;
715 timeout = MAX_SCHEDULE_TIMEOUT;
716 if (sk_wait_event(sk, &timeout, af_alg_writable(sk), &wait)) {
717 err = 0;
718 break;
719 }
720 }
721 remove_wait_queue(sk_sleep(sk), &wait);
722
723 return err;
724 }
725 EXPORT_SYMBOL_GPL(af_alg_wait_for_wmem);
726
727 /**
728 * af_alg_wmem_wakeup - wakeup caller when writable memory is available
729 *
730 * @sk socket of connection to user space
731 */
732 void af_alg_wmem_wakeup(struct sock *sk)
733 {
734 struct socket_wq *wq;
735
736 if (!af_alg_writable(sk))
737 return;
738
739 rcu_read_lock();
740 wq = rcu_dereference(sk->sk_wq);
741 if (skwq_has_sleeper(wq))
742 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
743 POLLRDNORM |
744 POLLRDBAND);
745 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
746 rcu_read_unlock();
747 }
748 EXPORT_SYMBOL_GPL(af_alg_wmem_wakeup);
749
750 /**
751 * af_alg_wait_for_data - wait for availability of TX data
752 *
753 * @sk socket of connection to user space
754 * @flags If MSG_DONTWAIT is set, then only report if function would sleep
755 * @return 0 when writable memory is available, < 0 upon error
756 */
757 int af_alg_wait_for_data(struct sock *sk, unsigned flags)
758 {
759 DEFINE_WAIT_FUNC(wait, woken_wake_function);
760 struct alg_sock *ask = alg_sk(sk);
761 struct af_alg_ctx *ctx = ask->private;
762 long timeout;
763 int err = -ERESTARTSYS;
764
765 if (flags & MSG_DONTWAIT)
766 return -EAGAIN;
767
768 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
769
770 add_wait_queue(sk_sleep(sk), &wait);
771 for (;;) {
772 if (signal_pending(current))
773 break;
774 timeout = MAX_SCHEDULE_TIMEOUT;
775 if (sk_wait_event(sk, &timeout, (ctx->used || !ctx->more),
776 &wait)) {
777 err = 0;
778 break;
779 }
780 }
781 remove_wait_queue(sk_sleep(sk), &wait);
782
783 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
784
785 return err;
786 }
787 EXPORT_SYMBOL_GPL(af_alg_wait_for_data);
788
789 /**
790 * af_alg_data_wakeup - wakeup caller when new data can be sent to kernel
791 *
792 * @sk socket of connection to user space
793 */
794
795 void af_alg_data_wakeup(struct sock *sk)
796 {
797 struct alg_sock *ask = alg_sk(sk);
798 struct af_alg_ctx *ctx = ask->private;
799 struct socket_wq *wq;
800
801 if (!ctx->used)
802 return;
803
804 rcu_read_lock();
805 wq = rcu_dereference(sk->sk_wq);
806 if (skwq_has_sleeper(wq))
807 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
808 POLLRDNORM |
809 POLLRDBAND);
810 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
811 rcu_read_unlock();
812 }
813 EXPORT_SYMBOL_GPL(af_alg_data_wakeup);
814
815 /**
816 * af_alg_sendmsg - implementation of sendmsg system call handler
817 *
818 * The sendmsg system call handler obtains the user data and stores it
819 * in ctx->tsgl_list. This implies allocation of the required numbers of
820 * struct af_alg_tsgl.
821 *
822 * In addition, the ctx is filled with the information sent via CMSG.
823 *
824 * @sock socket of connection to user space
825 * @msg message from user space
826 * @size size of message from user space
827 * @ivsize the size of the IV for the cipher operation to verify that the
828 * user-space-provided IV has the right size
829 * @return the number of copied data upon success, < 0 upon error
830 */
831 int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
832 unsigned int ivsize)
833 {
834 struct sock *sk = sock->sk;
835 struct alg_sock *ask = alg_sk(sk);
836 struct af_alg_ctx *ctx = ask->private;
837 struct af_alg_tsgl *sgl;
838 struct af_alg_control con = {};
839 long copied = 0;
840 bool enc = 0;
841 bool init = 0;
842 int err = 0;
843
844 if (msg->msg_controllen) {
845 err = af_alg_cmsg_send(msg, &con);
846 if (err)
847 return err;
848
849 init = 1;
850 switch (con.op) {
851 case ALG_OP_ENCRYPT:
852 enc = 1;
853 break;
854 case ALG_OP_DECRYPT:
855 enc = 0;
856 break;
857 default:
858 return -EINVAL;
859 }
860
861 if (con.iv && con.iv->ivlen != ivsize)
862 return -EINVAL;
863 }
864
865 lock_sock(sk);
866 if (!ctx->more && ctx->used) {
867 err = -EINVAL;
868 goto unlock;
869 }
870
871 if (init) {
872 ctx->enc = enc;
873 if (con.iv)
874 memcpy(ctx->iv, con.iv->iv, ivsize);
875
876 ctx->aead_assoclen = con.aead_assoclen;
877 }
878
879 while (size) {
880 struct scatterlist *sg;
881 size_t len = size;
882 size_t plen;
883
884 /* use the existing memory in an allocated page */
885 if (ctx->merge) {
886 sgl = list_entry(ctx->tsgl_list.prev,
887 struct af_alg_tsgl, list);
888 sg = sgl->sg + sgl->cur - 1;
889 len = min_t(size_t, len,
890 PAGE_SIZE - sg->offset - sg->length);
891
892 err = memcpy_from_msg(page_address(sg_page(sg)) +
893 sg->offset + sg->length,
894 msg, len);
895 if (err)
896 goto unlock;
897
898 sg->length += len;
899 ctx->merge = (sg->offset + sg->length) &
900 (PAGE_SIZE - 1);
901
902 ctx->used += len;
903 copied += len;
904 size -= len;
905 continue;
906 }
907
908 if (!af_alg_writable(sk)) {
909 err = af_alg_wait_for_wmem(sk, msg->msg_flags);
910 if (err)
911 goto unlock;
912 }
913
914 /* allocate a new page */
915 len = min_t(unsigned long, len, af_alg_sndbuf(sk));
916
917 err = af_alg_alloc_tsgl(sk);
918 if (err)
919 goto unlock;
920
921 sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl,
922 list);
923 sg = sgl->sg;
924 if (sgl->cur)
925 sg_unmark_end(sg + sgl->cur - 1);
926
927 do {
928 unsigned int i = sgl->cur;
929
930 plen = min_t(size_t, len, PAGE_SIZE);
931
932 sg_assign_page(sg + i, alloc_page(GFP_KERNEL));
933 if (!sg_page(sg + i)) {
934 err = -ENOMEM;
935 goto unlock;
936 }
937
938 err = memcpy_from_msg(page_address(sg_page(sg + i)),
939 msg, plen);
940 if (err) {
941 __free_page(sg_page(sg + i));
942 sg_assign_page(sg + i, NULL);
943 goto unlock;
944 }
945
946 sg[i].length = plen;
947 len -= plen;
948 ctx->used += plen;
949 copied += plen;
950 size -= plen;
951 sgl->cur++;
952 } while (len && sgl->cur < MAX_SGL_ENTS);
953
954 if (!size)
955 sg_mark_end(sg + sgl->cur - 1);
956
957 ctx->merge = plen & (PAGE_SIZE - 1);
958 }
959
960 err = 0;
961
962 ctx->more = msg->msg_flags & MSG_MORE;
963
964 unlock:
965 af_alg_data_wakeup(sk);
966 release_sock(sk);
967
968 return copied ?: err;
969 }
970 EXPORT_SYMBOL_GPL(af_alg_sendmsg);
971
972 /**
973 * af_alg_sendpage - sendpage system call handler
974 *
975 * This is a generic implementation of sendpage to fill ctx->tsgl_list.
976 */
977 ssize_t af_alg_sendpage(struct socket *sock, struct page *page,
978 int offset, size_t size, int flags)
979 {
980 struct sock *sk = sock->sk;
981 struct alg_sock *ask = alg_sk(sk);
982 struct af_alg_ctx *ctx = ask->private;
983 struct af_alg_tsgl *sgl;
984 int err = -EINVAL;
985
986 if (flags & MSG_SENDPAGE_NOTLAST)
987 flags |= MSG_MORE;
988
989 lock_sock(sk);
990 if (!ctx->more && ctx->used)
991 goto unlock;
992
993 if (!size)
994 goto done;
995
996 if (!af_alg_writable(sk)) {
997 err = af_alg_wait_for_wmem(sk, flags);
998 if (err)
999 goto unlock;
1000 }
1001
1002 err = af_alg_alloc_tsgl(sk);
1003 if (err)
1004 goto unlock;
1005
1006 ctx->merge = 0;
1007 sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list);
1008
1009 if (sgl->cur)
1010 sg_unmark_end(sgl->sg + sgl->cur - 1);
1011
1012 sg_mark_end(sgl->sg + sgl->cur);
1013
1014 get_page(page);
1015 sg_set_page(sgl->sg + sgl->cur, page, size, offset);
1016 sgl->cur++;
1017 ctx->used += size;
1018
1019 done:
1020 ctx->more = flags & MSG_MORE;
1021
1022 unlock:
1023 af_alg_data_wakeup(sk);
1024 release_sock(sk);
1025
1026 return err ?: size;
1027 }
1028 EXPORT_SYMBOL_GPL(af_alg_sendpage);
1029
1030 /**
1031 * af_alg_free_resources - release resources required for crypto request
1032 */
1033 void af_alg_free_resources(struct af_alg_async_req *areq)
1034 {
1035 struct sock *sk = areq->sk;
1036
1037 af_alg_free_areq_sgls(areq);
1038 sock_kfree_s(sk, areq, areq->areqlen);
1039 }
1040 EXPORT_SYMBOL_GPL(af_alg_free_resources);
1041
1042 /**
1043 * af_alg_async_cb - AIO callback handler
1044 *
1045 * This handler cleans up the struct af_alg_async_req upon completion of the
1046 * AIO operation.
1047 *
1048 * The number of bytes to be generated with the AIO operation must be set
1049 * in areq->outlen before the AIO callback handler is invoked.
1050 */
1051 void af_alg_async_cb(struct crypto_async_request *_req, int err)
1052 {
1053 struct af_alg_async_req *areq = _req->data;
1054 struct sock *sk = areq->sk;
1055 struct kiocb *iocb = areq->iocb;
1056 unsigned int resultlen;
1057
1058 /* Buffer size written by crypto operation. */
1059 resultlen = areq->outlen;
1060
1061 af_alg_free_resources(areq);
1062 sock_put(sk);
1063
1064 iocb->ki_complete(iocb, err ? err : (int)resultlen, 0);
1065 }
1066 EXPORT_SYMBOL_GPL(af_alg_async_cb);
1067
1068 /**
1069 * af_alg_poll - poll system call handler
1070 */
1071 unsigned int af_alg_poll(struct file *file, struct socket *sock,
1072 poll_table *wait)
1073 {
1074 struct sock *sk = sock->sk;
1075 struct alg_sock *ask = alg_sk(sk);
1076 struct af_alg_ctx *ctx = ask->private;
1077 unsigned int mask;
1078
1079 sock_poll_wait(file, sk_sleep(sk), wait);
1080 mask = 0;
1081
1082 if (!ctx->more || ctx->used)
1083 mask |= POLLIN | POLLRDNORM;
1084
1085 if (af_alg_writable(sk))
1086 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1087
1088 return mask;
1089 }
1090 EXPORT_SYMBOL_GPL(af_alg_poll);
1091
1092 /**
1093 * af_alg_alloc_areq - allocate struct af_alg_async_req
1094 *
1095 * @sk socket of connection to user space
1096 * @areqlen size of struct af_alg_async_req + crypto_*_reqsize
1097 * @return allocated data structure or ERR_PTR upon error
1098 */
1099 struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
1100 unsigned int areqlen)
1101 {
1102 struct af_alg_async_req *areq = sock_kmalloc(sk, areqlen, GFP_KERNEL);
1103
1104 if (unlikely(!areq))
1105 return ERR_PTR(-ENOMEM);
1106
1107 areq->areqlen = areqlen;
1108 areq->sk = sk;
1109 areq->last_rsgl = NULL;
1110 INIT_LIST_HEAD(&areq->rsgl_list);
1111 areq->tsgl = NULL;
1112 areq->tsgl_entries = 0;
1113
1114 return areq;
1115 }
1116 EXPORT_SYMBOL_GPL(af_alg_alloc_areq);
1117
1118 /**
1119 * af_alg_get_rsgl - create the RX SGL for the output data from the crypto
1120 * operation
1121 *
1122 * @sk socket of connection to user space
1123 * @msg user space message
1124 * @flags flags used to invoke recvmsg with
1125 * @areq instance of the cryptographic request that will hold the RX SGL
1126 * @maxsize maximum number of bytes to be pulled from user space
1127 * @outlen number of bytes in the RX SGL
1128 * @return 0 on success, < 0 upon error
1129 */
1130 int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
1131 struct af_alg_async_req *areq, size_t maxsize,
1132 size_t *outlen)
1133 {
1134 struct alg_sock *ask = alg_sk(sk);
1135 struct af_alg_ctx *ctx = ask->private;
1136 size_t len = 0;
1137
1138 while (maxsize > len && msg_data_left(msg)) {
1139 struct af_alg_rsgl *rsgl;
1140 size_t seglen;
1141 int err;
1142
1143 /* limit the amount of readable buffers */
1144 if (!af_alg_readable(sk))
1145 break;
1146
1147 seglen = min_t(size_t, (maxsize - len),
1148 msg_data_left(msg));
1149
1150 if (list_empty(&areq->rsgl_list)) {
1151 rsgl = &areq->first_rsgl;
1152 } else {
1153 rsgl = sock_kmalloc(sk, sizeof(*rsgl), GFP_KERNEL);
1154 if (unlikely(!rsgl))
1155 return -ENOMEM;
1156 }
1157
1158 rsgl->sgl.npages = 0;
1159 list_add_tail(&rsgl->list, &areq->rsgl_list);
1160
1161 /* make one iovec available as scatterlist */
1162 err = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, seglen);
1163 if (err < 0) {
1164 rsgl->sg_num_bytes = 0;
1165 return err;
1166 }
1167
1168 /* chain the new scatterlist with previous one */
1169 if (areq->last_rsgl)
1170 af_alg_link_sg(&areq->last_rsgl->sgl, &rsgl->sgl);
1171
1172 areq->last_rsgl = rsgl;
1173 len += err;
1174 atomic_add(err, &ctx->rcvused);
1175 rsgl->sg_num_bytes = err;
1176 iov_iter_advance(&msg->msg_iter, err);
1177 }
1178
1179 *outlen = len;
1180 return 0;
1181 }
1182 EXPORT_SYMBOL_GPL(af_alg_get_rsgl);
1183
1184 static int __init af_alg_init(void)
1185 {
1186 int err = proto_register(&alg_proto, 0);
1187
1188 if (err)
1189 goto out;
1190
1191 err = sock_register(&alg_family);
1192 if (err != 0)
1193 goto out_unregister_proto;
1194
1195 out:
1196 return err;
1197
1198 out_unregister_proto:
1199 proto_unregister(&alg_proto);
1200 goto out;
1201 }
1202
1203 static void __exit af_alg_exit(void)
1204 {
1205 sock_unregister(PF_ALG);
1206 proto_unregister(&alg_proto);
1207 }
1208
1209 module_init(af_alg_init);
1210 module_exit(af_alg_exit);
1211 MODULE_LICENSE("GPL");
1212 MODULE_ALIAS_NETPROTO(AF_ALG);