blob: 0b2a4de1a080cf14c18b9c1971270ca079dc5bdc [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 Sharma60063492011-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>
Milan Broz21489732014-07-29 18:41:09 +000024#include <linux/security.h>
Herbert Xu03c8efc2010-10-19 21:12:39 +080025
26struct alg_type_list {
27 const struct af_alg_type *type;
28 struct list_head list;
29};
30
Randy Dunlap06869522010-12-21 22:22:40 +110031static atomic_long_t alg_memory_allocated;
Herbert Xu03c8efc2010-10-19 21:12:39 +080032
33static struct proto alg_proto = {
34 .name = "ALG",
35 .owner = THIS_MODULE,
36 .memory_allocated = &alg_memory_allocated,
37 .obj_size = sizeof(struct alg_sock),
38};
39
40static LIST_HEAD(alg_types);
41static DECLARE_RWSEM(alg_types_sem);
42
43static const struct af_alg_type *alg_get_type(const char *name)
44{
45 const struct af_alg_type *type = ERR_PTR(-ENOENT);
46 struct alg_type_list *node;
47
48 down_read(&alg_types_sem);
49 list_for_each_entry(node, &alg_types, list) {
50 if (strcmp(node->type->name, name))
51 continue;
52
53 if (try_module_get(node->type->owner))
54 type = node->type;
55 break;
56 }
57 up_read(&alg_types_sem);
58
59 return type;
60}
61
62int af_alg_register_type(const struct af_alg_type *type)
63{
64 struct alg_type_list *node;
65 int err = -EEXIST;
66
67 down_write(&alg_types_sem);
68 list_for_each_entry(node, &alg_types, list) {
69 if (!strcmp(node->type->name, type->name))
70 goto unlock;
71 }
72
73 node = kmalloc(sizeof(*node), GFP_KERNEL);
74 err = -ENOMEM;
75 if (!node)
76 goto unlock;
77
78 type->ops->owner = THIS_MODULE;
79 node->type = type;
80 list_add(&node->list, &alg_types);
81 err = 0;
82
83unlock:
84 up_write(&alg_types_sem);
85
86 return err;
87}
88EXPORT_SYMBOL_GPL(af_alg_register_type);
89
90int af_alg_unregister_type(const struct af_alg_type *type)
91{
92 struct alg_type_list *node;
93 int err = -ENOENT;
94
95 down_write(&alg_types_sem);
96 list_for_each_entry(node, &alg_types, list) {
97 if (strcmp(node->type->name, type->name))
98 continue;
99
100 list_del(&node->list);
101 kfree(node);
102 err = 0;
103 break;
104 }
105 up_write(&alg_types_sem);
106
107 return err;
108}
109EXPORT_SYMBOL_GPL(af_alg_unregister_type);
110
111static void alg_do_release(const struct af_alg_type *type, void *private)
112{
113 if (!type)
114 return;
115
116 type->release(private);
117 module_put(type->owner);
118}
119
120int af_alg_release(struct socket *sock)
121{
Mao Wenan4101cbe2019-02-15 22:24:15 +0800122 if (sock->sk) {
Herbert Xu03c8efc2010-10-19 21:12:39 +0800123 sock_put(sock->sk);
Mao Wenan4101cbe2019-02-15 22:24:15 +0800124 sock->sk = NULL;
125 }
Herbert Xu03c8efc2010-10-19 21:12:39 +0800126 return 0;
127}
128EXPORT_SYMBOL_GPL(af_alg_release);
129
130static int alg_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
131{
132 struct sock *sk = sock->sk;
133 struct alg_sock *ask = alg_sk(sk);
134 struct sockaddr_alg *sa = (void *)uaddr;
135 const struct af_alg_type *type;
136 void *private;
137
138 if (sock->state == SS_CONNECTED)
139 return -EINVAL;
140
141 if (addr_len != sizeof(*sa))
142 return -EINVAL;
143
144 sa->salg_type[sizeof(sa->salg_type) - 1] = 0;
145 sa->salg_name[sizeof(sa->salg_name) - 1] = 0;
146
147 type = alg_get_type(sa->salg_type);
148 if (IS_ERR(type) && PTR_ERR(type) == -ENOENT) {
149 request_module("algif-%s", sa->salg_type);
150 type = alg_get_type(sa->salg_type);
151 }
152
153 if (IS_ERR(type))
154 return PTR_ERR(type);
155
156 private = type->bind(sa->salg_name, sa->salg_feat, sa->salg_mask);
157 if (IS_ERR(private)) {
158 module_put(type->owner);
159 return PTR_ERR(private);
160 }
161
162 lock_sock(sk);
163
164 swap(ask->type, type);
165 swap(ask->private, private);
166
167 release_sock(sk);
168
169 alg_do_release(type, private);
170
171 return 0;
172}
173
174static int alg_setkey(struct sock *sk, char __user *ukey,
175 unsigned int keylen)
176{
177 struct alg_sock *ask = alg_sk(sk);
178 const struct af_alg_type *type = ask->type;
179 u8 *key;
180 int err;
181
182 key = sock_kmalloc(sk, keylen, GFP_KERNEL);
183 if (!key)
184 return -ENOMEM;
185
186 err = -EFAULT;
187 if (copy_from_user(key, ukey, keylen))
188 goto out;
189
190 err = type->setkey(ask->private, key, keylen);
191
192out:
193 sock_kfree_s(sk, key, keylen);
194
195 return err;
196}
197
198static int alg_setsockopt(struct socket *sock, int level, int optname,
199 char __user *optval, unsigned int optlen)
200{
201 struct sock *sk = sock->sk;
202 struct alg_sock *ask = alg_sk(sk);
203 const struct af_alg_type *type;
204 int err = -ENOPROTOOPT;
205
206 lock_sock(sk);
207 type = ask->type;
208
209 if (level != SOL_ALG || !type)
210 goto unlock;
211
212 switch (optname) {
213 case ALG_SET_KEY:
214 if (sock->state == SS_CONNECTED)
215 goto unlock;
216 if (!type->setkey)
217 goto unlock;
218
219 err = alg_setkey(sk, optval, optlen);
220 }
221
222unlock:
223 release_sock(sk);
224
225 return err;
226}
227
228int af_alg_accept(struct sock *sk, struct socket *newsock)
229{
230 struct alg_sock *ask = alg_sk(sk);
231 const struct af_alg_type *type;
232 struct sock *sk2;
233 int err;
234
235 lock_sock(sk);
236 type = ask->type;
237
238 err = -EINVAL;
239 if (!type)
240 goto unlock;
241
242 sk2 = sk_alloc(sock_net(sk), PF_ALG, GFP_KERNEL, &alg_proto);
243 err = -ENOMEM;
244 if (!sk2)
245 goto unlock;
246
247 sock_init_data(newsock, sk2);
Miloslav Trmač507cad32010-12-08 14:36:19 +0800248 sock_graft(sk2, newsock);
Milan Broz21489732014-07-29 18:41:09 +0000249 security_sk_clone(sk, sk2);
Herbert Xu03c8efc2010-10-19 21:12:39 +0800250
251 err = type->accept(ask->private, sk2);
252 if (err) {
253 sk_free(sk2);
254 goto unlock;
255 }
256
257 sk2->sk_family = PF_ALG;
258
259 sock_hold(sk);
260 alg_sk(sk2)->parent = sk;
261 alg_sk(sk2)->type = type;
262
263 newsock->ops = type->ops;
264 newsock->state = SS_CONNECTED;
265
266 err = 0;
267
268unlock:
269 release_sock(sk);
270
271 return err;
272}
273EXPORT_SYMBOL_GPL(af_alg_accept);
274
275static int alg_accept(struct socket *sock, struct socket *newsock, int flags)
276{
277 return af_alg_accept(sock->sk, newsock);
278}
279
280static const struct proto_ops alg_proto_ops = {
281 .family = PF_ALG,
282 .owner = THIS_MODULE,
283
284 .connect = sock_no_connect,
285 .socketpair = sock_no_socketpair,
286 .getname = sock_no_getname,
287 .ioctl = sock_no_ioctl,
288 .listen = sock_no_listen,
289 .shutdown = sock_no_shutdown,
290 .getsockopt = sock_no_getsockopt,
291 .mmap = sock_no_mmap,
292 .sendpage = sock_no_sendpage,
293 .sendmsg = sock_no_sendmsg,
294 .recvmsg = sock_no_recvmsg,
295 .poll = sock_no_poll,
296
297 .bind = alg_bind,
298 .release = af_alg_release,
299 .setsockopt = alg_setsockopt,
300 .accept = alg_accept,
301};
302
303static void alg_sock_destruct(struct sock *sk)
304{
305 struct alg_sock *ask = alg_sk(sk);
306
307 alg_do_release(ask->type, ask->private);
308}
309
310static int alg_create(struct net *net, struct socket *sock, int protocol,
311 int kern)
312{
313 struct sock *sk;
314 int err;
315
316 if (sock->type != SOCK_SEQPACKET)
317 return -ESOCKTNOSUPPORT;
318 if (protocol != 0)
319 return -EPROTONOSUPPORT;
320
321 err = -ENOMEM;
322 sk = sk_alloc(net, PF_ALG, GFP_KERNEL, &alg_proto);
323 if (!sk)
324 goto out;
325
326 sock->ops = &alg_proto_ops;
327 sock_init_data(sock, sk);
328
329 sk->sk_family = PF_ALG;
330 sk->sk_destruct = alg_sock_destruct;
331
332 return 0;
333out:
334 return err;
335}
336
337static const struct net_proto_family alg_family = {
338 .family = PF_ALG,
339 .create = alg_create,
340 .owner = THIS_MODULE,
341};
342
343int af_alg_make_sg(struct af_alg_sgl *sgl, void __user *addr, int len,
344 int write)
345{
346 unsigned long from = (unsigned long)addr;
347 unsigned long npages;
348 unsigned off;
349 int err;
350 int i;
351
352 err = -EFAULT;
353 if (!access_ok(write ? VERIFY_READ : VERIFY_WRITE, addr, len))
354 goto out;
355
356 off = from & ~PAGE_MASK;
357 npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
358 if (npages > ALG_MAX_PAGES)
359 npages = ALG_MAX_PAGES;
360
361 err = get_user_pages_fast(from, npages, write, sgl->pages);
362 if (err < 0)
363 goto out;
364
365 npages = err;
366 err = -EINVAL;
367 if (WARN_ON(npages == 0))
368 goto out;
369
370 err = 0;
371
372 sg_init_table(sgl->sg, npages);
373
374 for (i = 0; i < npages; i++) {
375 int plen = min_t(int, len, PAGE_SIZE - off);
376
377 sg_set_page(sgl->sg + i, sgl->pages[i], plen, off);
378
379 off = 0;
380 len -= plen;
381 err += plen;
382 }
383
384out:
385 return err;
386}
387EXPORT_SYMBOL_GPL(af_alg_make_sg);
388
389void af_alg_free_sg(struct af_alg_sgl *sgl)
390{
391 int i;
392
393 i = 0;
394 do {
395 put_page(sgl->pages[i]);
396 } while (!sg_is_last(sgl->sg + (i++)));
397}
398EXPORT_SYMBOL_GPL(af_alg_free_sg);
399
400int af_alg_cmsg_send(struct msghdr *msg, struct af_alg_control *con)
401{
402 struct cmsghdr *cmsg;
403
404 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
405 if (!CMSG_OK(msg, cmsg))
406 return -EINVAL;
407 if (cmsg->cmsg_level != SOL_ALG)
408 continue;
409
410 switch(cmsg->cmsg_type) {
411 case ALG_SET_IV:
412 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*con->iv)))
413 return -EINVAL;
414 con->iv = (void *)CMSG_DATA(cmsg);
415 if (cmsg->cmsg_len < CMSG_LEN(con->iv->ivlen +
416 sizeof(*con->iv)))
417 return -EINVAL;
418 break;
419
420 case ALG_SET_OP:
421 if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
422 return -EINVAL;
423 con->op = *(u32 *)CMSG_DATA(cmsg);
424 break;
425
426 default:
427 return -EINVAL;
428 }
429 }
430
431 return 0;
432}
433EXPORT_SYMBOL_GPL(af_alg_cmsg_send);
434
435int af_alg_wait_for_completion(int err, struct af_alg_completion *completion)
436{
437 switch (err) {
438 case -EINPROGRESS:
439 case -EBUSY:
440 wait_for_completion(&completion->completion);
441 INIT_COMPLETION(completion->completion);
442 err = completion->err;
443 break;
444 };
445
446 return err;
447}
448EXPORT_SYMBOL_GPL(af_alg_wait_for_completion);
449
450void af_alg_complete(struct crypto_async_request *req, int err)
451{
452 struct af_alg_completion *completion = req->data;
453
Rabin Vincent30c51ba2014-12-19 13:36:08 +0100454 if (err == -EINPROGRESS)
455 return;
456
Herbert Xu03c8efc2010-10-19 21:12:39 +0800457 completion->err = err;
458 complete(&completion->completion);
459}
460EXPORT_SYMBOL_GPL(af_alg_complete);
461
462static int __init af_alg_init(void)
463{
464 int err = proto_register(&alg_proto, 0);
465
466 if (err)
467 goto out;
468
469 err = sock_register(&alg_family);
470 if (err != 0)
471 goto out_unregister_proto;
472
473out:
474 return err;
475
476out_unregister_proto:
477 proto_unregister(&alg_proto);
478 goto out;
479}
480
481static void __exit af_alg_exit(void)
482{
483 sock_unregister(PF_ALG);
484 proto_unregister(&alg_proto);
485}
486
487module_init(af_alg_init);
488module_exit(af_alg_exit);
489MODULE_LICENSE("GPL");
490MODULE_ALIAS_NETPROTO(AF_ALG);