hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/crypto/af_alg.c
....@@ -1,15 +1,10 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * af_alg: User-space algorithm interface
34 *
45 * This file provides the user-space API for algorithms.
56 *
67 * 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
- *
138 */
149
1510 #include <linux/atomic.h>
....@@ -21,6 +16,7 @@
2116 #include <linux/module.h>
2217 #include <linux/net.h>
2318 #include <linux/rwsem.h>
19
+#include <linux/sched.h>
2420 #include <linux/sched/signal.h>
2521 #include <linux/security.h>
2622
....@@ -174,7 +170,7 @@
174170 sa->salg_name[addr_len - sizeof(*sa) - 1] = 0;
175171
176172 type = alg_get_type(sa->salg_type);
177
- if (IS_ERR(type) && PTR_ERR(type) == -ENOENT) {
173
+ if (PTR_ERR(type) == -ENOENT) {
178174 request_module("algif-%s", sa->salg_type);
179175 type = alg_get_type(sa->salg_type);
180176 }
....@@ -206,8 +202,7 @@
206202 return err;
207203 }
208204
209
-static int alg_setkey(struct sock *sk, char __user *ukey,
210
- unsigned int keylen)
205
+static int alg_setkey(struct sock *sk, sockptr_t ukey, unsigned int keylen)
211206 {
212207 struct alg_sock *ask = alg_sk(sk);
213208 const struct af_alg_type *type = ask->type;
....@@ -219,7 +214,7 @@
219214 return -ENOMEM;
220215
221216 err = -EFAULT;
222
- if (copy_from_user(key, ukey, keylen))
217
+ if (copy_from_sockptr(key, ukey, keylen))
223218 goto out;
224219
225220 err = type->setkey(ask->private, key, keylen);
....@@ -231,7 +226,7 @@
231226 }
232227
233228 static int alg_setsockopt(struct socket *sock, int level, int optname,
234
- char __user *optval, unsigned int optlen)
229
+ sockptr_t optval, unsigned int optlen)
235230 {
236231 struct sock *sk = sock->sk;
237232 struct alg_sock *ask = alg_sk(sk);
....@@ -263,6 +258,14 @@
263258 if (!type->setauthsize)
264259 goto unlock;
265260 err = type->setauthsize(ask->private, optlen);
261
+ break;
262
+ case ALG_SET_DRBG_ENTROPY:
263
+ if (sock->state == SS_CONNECTED)
264
+ goto unlock;
265
+ if (!type->setentropy)
266
+ goto unlock;
267
+
268
+ err = type->setentropy(ask->private, optval, optlen);
266269 }
267270
268271 unlock:
....@@ -295,6 +298,11 @@
295298 security_sock_graft(sk2, newsock);
296299 security_sk_clone(sk, sk2);
297300
301
+ /*
302
+ * newsock->ops assigned here to allow type->accept call to override
303
+ * them when required.
304
+ */
305
+ newsock->ops = type->ops;
298306 err = type->accept(ask->private, sk2);
299307
300308 nokey = err == -ENOKEY;
....@@ -303,8 +311,6 @@
303311
304312 if (err)
305313 goto unlock;
306
-
307
- sk2->sk_family = PF_ALG;
308314
309315 if (atomic_inc_return_relaxed(&ask->refcnt) == 1)
310316 sock_hold(sk);
....@@ -315,7 +321,6 @@
315321 alg_sk(sk2)->parent = sk;
316322 alg_sk(sk2)->type = type;
317323
318
- newsock->ops = type->ops;
319324 newsock->state = SS_CONNECTED;
320325
321326 if (nokey)
....@@ -346,7 +351,6 @@
346351 .ioctl = sock_no_ioctl,
347352 .listen = sock_no_listen,
348353 .shutdown = sock_no_shutdown,
349
- .getsockopt = sock_no_getsockopt,
350354 .mmap = sock_no_mmap,
351355 .sendpage = sock_no_sendpage,
352356 .sendmsg = sock_no_sendmsg,
....@@ -384,7 +388,6 @@
384388 sock->ops = &alg_proto_ops;
385389 sock_init_data(sock, sk);
386390
387
- sk->sk_family = PF_ALG;
388391 sk->sk_destruct = alg_sock_destruct;
389392
390393 return 0;
....@@ -429,12 +432,12 @@
429432 }
430433 EXPORT_SYMBOL_GPL(af_alg_make_sg);
431434
432
-void af_alg_link_sg(struct af_alg_sgl *sgl_prev, struct af_alg_sgl *sgl_new)
435
+static void af_alg_link_sg(struct af_alg_sgl *sgl_prev,
436
+ struct af_alg_sgl *sgl_new)
433437 {
434438 sg_unmark_end(sgl_prev->sg + sgl_prev->npages - 1);
435439 sg_chain(sgl_prev->sg, sgl_prev->npages + 1, sgl_new->sg);
436440 }
437
-EXPORT_SYMBOL_GPL(af_alg_link_sg);
438441
439442 void af_alg_free_sg(struct af_alg_sgl *sgl)
440443 {
....@@ -445,7 +448,7 @@
445448 }
446449 EXPORT_SYMBOL_GPL(af_alg_free_sg);
447450
448
-int af_alg_cmsg_send(struct msghdr *msg, struct af_alg_control *con)
451
+static int af_alg_cmsg_send(struct msghdr *msg, struct af_alg_control *con)
449452 {
450453 struct cmsghdr *cmsg;
451454
....@@ -484,7 +487,6 @@
484487
485488 return 0;
486489 }
487
-EXPORT_SYMBOL_GPL(af_alg_cmsg_send);
488490
489491 /**
490492 * af_alg_alloc_tsgl - allocate the TX SGL
....@@ -492,7 +494,7 @@
492494 * @sk socket of connection to user space
493495 * @return: 0 upon success, < 0 upon error
494496 */
495
-int af_alg_alloc_tsgl(struct sock *sk)
497
+static int af_alg_alloc_tsgl(struct sock *sk)
496498 {
497499 struct alg_sock *ask = alg_sk(sk);
498500 struct af_alg_ctx *ctx = ask->private;
....@@ -521,7 +523,6 @@
521523
522524 return 0;
523525 }
524
-EXPORT_SYMBOL_GPL(af_alg_alloc_tsgl);
525526
526527 /**
527528 * aead_count_tsgl - Count number of TX SG entries
....@@ -536,17 +537,17 @@
536537 */
537538 unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset)
538539 {
539
- struct alg_sock *ask = alg_sk(sk);
540
- struct af_alg_ctx *ctx = ask->private;
541
- struct af_alg_tsgl *sgl, *tmp;
540
+ const struct alg_sock *ask = alg_sk(sk);
541
+ const struct af_alg_ctx *ctx = ask->private;
542
+ const struct af_alg_tsgl *sgl;
542543 unsigned int i;
543544 unsigned int sgl_count = 0;
544545
545546 if (!bytes)
546547 return 0;
547548
548
- list_for_each_entry_safe(sgl, tmp, &ctx->tsgl_list, list) {
549
- struct scatterlist *sg = sgl->sg;
549
+ list_for_each_entry(sgl, &ctx->tsgl_list, list) {
550
+ const struct scatterlist *sg = sgl->sg;
550551
551552 for (i = 0; i < sgl->cur; i++) {
552553 size_t bytes_count;
....@@ -644,12 +645,12 @@
644645 }
645646
646647 list_del(&sgl->list);
647
- sock_kfree_s(sk, sgl, sizeof(*sgl) + sizeof(sgl->sg[0]) *
648
- (MAX_SGL_ENTS + 1));
648
+ sock_kfree_s(sk, sgl, struct_size(sgl, sg, MAX_SGL_ENTS + 1));
649649 }
650650
651651 if (!ctx->used)
652652 ctx->merge = 0;
653
+ ctx->init = ctx->more;
653654 }
654655 EXPORT_SYMBOL_GPL(af_alg_pull_tsgl);
655656
....@@ -658,7 +659,7 @@
658659 *
659660 * @areq Request holding the TX and RX SGL
660661 */
661
-void af_alg_free_areq_sgls(struct af_alg_async_req *areq)
662
+static void af_alg_free_areq_sgls(struct af_alg_async_req *areq)
662663 {
663664 struct sock *sk = areq->sk;
664665 struct alg_sock *ask = alg_sk(sk);
....@@ -687,7 +688,6 @@
687688 sock_kfree_s(sk, tsgl, areq->tsgl_entries * sizeof(*tsgl));
688689 }
689690 }
690
-EXPORT_SYMBOL_GPL(af_alg_free_areq_sgls);
691691
692692 /**
693693 * af_alg_wait_for_wmem - wait for availability of writable memory
....@@ -696,7 +696,7 @@
696696 * @flags If MSG_DONTWAIT is set, then only report if function would sleep
697697 * @return 0 when writable memory is available, < 0 upon error
698698 */
699
-int af_alg_wait_for_wmem(struct sock *sk, unsigned int flags)
699
+static int af_alg_wait_for_wmem(struct sock *sk, unsigned int flags)
700700 {
701701 DEFINE_WAIT_FUNC(wait, woken_wake_function);
702702 int err = -ERESTARTSYS;
....@@ -721,7 +721,6 @@
721721
722722 return err;
723723 }
724
-EXPORT_SYMBOL_GPL(af_alg_wait_for_wmem);
725724
726725 /**
727726 * af_alg_wmem_wakeup - wakeup caller when writable memory is available
....@@ -751,9 +750,10 @@
751750 *
752751 * @sk socket of connection to user space
753752 * @flags If MSG_DONTWAIT is set, then only report if function would sleep
753
+ * @min Set to minimum request size if partial requests are allowed.
754754 * @return 0 when writable memory is available, < 0 upon error
755755 */
756
-int af_alg_wait_for_data(struct sock *sk, unsigned flags)
756
+int af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min)
757757 {
758758 DEFINE_WAIT_FUNC(wait, woken_wake_function);
759759 struct alg_sock *ask = alg_sk(sk);
....@@ -771,7 +771,9 @@
771771 if (signal_pending(current))
772772 break;
773773 timeout = MAX_SCHEDULE_TIMEOUT;
774
- if (sk_wait_event(sk, &timeout, (ctx->used || !ctx->more),
774
+ if (sk_wait_event(sk, &timeout,
775
+ ctx->init && (!ctx->more ||
776
+ (min && ctx->used >= min)),
775777 &wait)) {
776778 err = 0;
777779 break;
....@@ -790,8 +792,7 @@
790792 *
791793 * @sk socket of connection to user space
792794 */
793
-
794
-void af_alg_data_wakeup(struct sock *sk)
795
+static void af_alg_data_wakeup(struct sock *sk)
795796 {
796797 struct alg_sock *ask = alg_sk(sk);
797798 struct af_alg_ctx *ctx = ask->private;
....@@ -809,7 +810,6 @@
809810 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
810811 rcu_read_unlock();
811812 }
812
-EXPORT_SYMBOL_GPL(af_alg_data_wakeup);
813813
814814 /**
815815 * af_alg_sendmsg - implementation of sendmsg system call handler
....@@ -836,8 +836,8 @@
836836 struct af_alg_tsgl *sgl;
837837 struct af_alg_control con = {};
838838 long copied = 0;
839
- bool enc = 0;
840
- bool init = 0;
839
+ bool enc = false;
840
+ bool init = false;
841841 int err = 0;
842842
843843 if (msg->msg_controllen) {
....@@ -845,13 +845,13 @@
845845 if (err)
846846 return err;
847847
848
- init = 1;
848
+ init = true;
849849 switch (con.op) {
850850 case ALG_OP_ENCRYPT:
851
- enc = 1;
851
+ enc = true;
852852 break;
853853 case ALG_OP_DECRYPT:
854
- enc = 0;
854
+ enc = false;
855855 break;
856856 default:
857857 return -EINVAL;
....@@ -862,10 +862,17 @@
862862 }
863863
864864 lock_sock(sk);
865
- if (!ctx->more && ctx->used) {
866
- err = -EINVAL;
867
- goto unlock;
865
+ if (ctx->init && !ctx->more) {
866
+ if (ctx->used) {
867
+ err = -EINVAL;
868
+ goto unlock;
869
+ }
870
+
871
+ pr_info_once(
872
+ "%s sent an empty control message without MSG_MORE.\n",
873
+ current->comm);
868874 }
875
+ ctx->init = true;
869876
870877 if (init) {
871878 ctx->enc = enc;