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