]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - crypto/af_alg.c
crypto: af_alg - Disallow bind/setkey/... after accept(2)
[mirror_ubuntu-artful-kernel.git] / crypto / af_alg.c
CommitLineData
03c8efc1
HX
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
60063497 15#include <linux/atomic.h>
03c8efc1
HX
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>
4c63f83c 24#include <linux/security.h>
03c8efc1
HX
25
26struct alg_type_list {
27 const struct af_alg_type *type;
28 struct list_head list;
29};
30
06869524 31static atomic_long_t alg_memory_allocated;
03c8efc1
HX
32
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{
122 if (sock->sk)
123 sock_put(sock->sk);
124 return 0;
125}
126EXPORT_SYMBOL_GPL(af_alg_release);
127
c840ac6a
HX
128void af_alg_release_parent(struct sock *sk)
129{
130 struct alg_sock *ask = alg_sk(sk);
131 bool last;
132
133 sk = ask->parent;
134 ask = alg_sk(sk);
135
136 lock_sock(sk);
137 last = !--ask->refcnt;
138 release_sock(sk);
139
140 if (last)
141 sock_put(sk);
142}
143EXPORT_SYMBOL_GPL(af_alg_release_parent);
144
03c8efc1
HX
145static int alg_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
146{
15539de5 147 const u32 forbidden = CRYPTO_ALG_INTERNAL;
03c8efc1
HX
148 struct sock *sk = sock->sk;
149 struct alg_sock *ask = alg_sk(sk);
150 struct sockaddr_alg *sa = (void *)uaddr;
151 const struct af_alg_type *type;
152 void *private;
c840ac6a 153 int err;
03c8efc1
HX
154
155 if (sock->state == SS_CONNECTED)
156 return -EINVAL;
157
158 if (addr_len != sizeof(*sa))
159 return -EINVAL;
160
161 sa->salg_type[sizeof(sa->salg_type) - 1] = 0;
162 sa->salg_name[sizeof(sa->salg_name) - 1] = 0;
163
164 type = alg_get_type(sa->salg_type);
165 if (IS_ERR(type) && PTR_ERR(type) == -ENOENT) {
166 request_module("algif-%s", sa->salg_type);
167 type = alg_get_type(sa->salg_type);
168 }
169
170 if (IS_ERR(type))
171 return PTR_ERR(type);
172
15539de5
HX
173 private = type->bind(sa->salg_name,
174 sa->salg_feat & ~forbidden,
175 sa->salg_mask & ~forbidden);
03c8efc1
HX
176 if (IS_ERR(private)) {
177 module_put(type->owner);
178 return PTR_ERR(private);
179 }
180
c840ac6a 181 err = -EBUSY;
03c8efc1 182 lock_sock(sk);
c840ac6a
HX
183 if (ask->refcnt)
184 goto unlock;
03c8efc1
HX
185
186 swap(ask->type, type);
187 swap(ask->private, private);
188
c840ac6a
HX
189 err = 0;
190
191unlock:
03c8efc1
HX
192 release_sock(sk);
193
194 alg_do_release(type, private);
195
c840ac6a 196 return err;
03c8efc1
HX
197}
198
199static int alg_setkey(struct sock *sk, char __user *ukey,
200 unsigned int keylen)
201{
202 struct alg_sock *ask = alg_sk(sk);
203 const struct af_alg_type *type = ask->type;
204 u8 *key;
205 int err;
206
207 key = sock_kmalloc(sk, keylen, GFP_KERNEL);
208 if (!key)
209 return -ENOMEM;
210
211 err = -EFAULT;
212 if (copy_from_user(key, ukey, keylen))
213 goto out;
214
215 err = type->setkey(ask->private, key, keylen);
216
217out:
ad202c8c 218 sock_kzfree_s(sk, key, keylen);
03c8efc1
HX
219
220 return err;
221}
222
223static int alg_setsockopt(struct socket *sock, int level, int optname,
224 char __user *optval, unsigned int optlen)
225{
226 struct sock *sk = sock->sk;
227 struct alg_sock *ask = alg_sk(sk);
228 const struct af_alg_type *type;
c840ac6a 229 int err = -EBUSY;
03c8efc1
HX
230
231 lock_sock(sk);
c840ac6a
HX
232 if (ask->refcnt)
233 goto unlock;
234
03c8efc1
HX
235 type = ask->type;
236
c840ac6a 237 err = -ENOPROTOOPT;
03c8efc1
HX
238 if (level != SOL_ALG || !type)
239 goto unlock;
240
241 switch (optname) {
242 case ALG_SET_KEY:
243 if (sock->state == SS_CONNECTED)
244 goto unlock;
245 if (!type->setkey)
246 goto unlock;
247
248 err = alg_setkey(sk, optval, optlen);
25fb8638
SM
249 break;
250 case ALG_SET_AEAD_AUTHSIZE:
251 if (sock->state == SS_CONNECTED)
252 goto unlock;
253 if (!type->setauthsize)
254 goto unlock;
255 err = type->setauthsize(ask->private, optlen);
03c8efc1
HX
256 }
257
258unlock:
259 release_sock(sk);
260
261 return err;
262}
263
264int af_alg_accept(struct sock *sk, struct socket *newsock)
265{
266 struct alg_sock *ask = alg_sk(sk);
267 const struct af_alg_type *type;
268 struct sock *sk2;
269 int err;
270
271 lock_sock(sk);
272 type = ask->type;
273
274 err = -EINVAL;
275 if (!type)
276 goto unlock;
277
11aa9c28 278 sk2 = sk_alloc(sock_net(sk), PF_ALG, GFP_KERNEL, &alg_proto, 0);
03c8efc1
HX
279 err = -ENOMEM;
280 if (!sk2)
281 goto unlock;
282
283 sock_init_data(newsock, sk2);
507cad35 284 sock_graft(sk2, newsock);
4c63f83c 285 security_sk_clone(sk, sk2);
03c8efc1
HX
286
287 err = type->accept(ask->private, sk2);
288 if (err) {
289 sk_free(sk2);
290 goto unlock;
291 }
292
293 sk2->sk_family = PF_ALG;
294
c840ac6a
HX
295 if (!ask->refcnt++)
296 sock_hold(sk);
03c8efc1
HX
297 alg_sk(sk2)->parent = sk;
298 alg_sk(sk2)->type = type;
299
300 newsock->ops = type->ops;
301 newsock->state = SS_CONNECTED;
302
303 err = 0;
304
305unlock:
306 release_sock(sk);
307
308 return err;
309}
310EXPORT_SYMBOL_GPL(af_alg_accept);
311
312static int alg_accept(struct socket *sock, struct socket *newsock, int flags)
313{
314 return af_alg_accept(sock->sk, newsock);
315}
316
317static const struct proto_ops alg_proto_ops = {
318 .family = PF_ALG,
319 .owner = THIS_MODULE,
320
321 .connect = sock_no_connect,
322 .socketpair = sock_no_socketpair,
323 .getname = sock_no_getname,
324 .ioctl = sock_no_ioctl,
325 .listen = sock_no_listen,
326 .shutdown = sock_no_shutdown,
327 .getsockopt = sock_no_getsockopt,
328 .mmap = sock_no_mmap,
329 .sendpage = sock_no_sendpage,
330 .sendmsg = sock_no_sendmsg,
331 .recvmsg = sock_no_recvmsg,
332 .poll = sock_no_poll,
333
334 .bind = alg_bind,
335 .release = af_alg_release,
336 .setsockopt = alg_setsockopt,
337 .accept = alg_accept,
338};
339
340static void alg_sock_destruct(struct sock *sk)
341{
342 struct alg_sock *ask = alg_sk(sk);
343
344 alg_do_release(ask->type, ask->private);
345}
346
347static int alg_create(struct net *net, struct socket *sock, int protocol,
348 int kern)
349{
350 struct sock *sk;
351 int err;
352
353 if (sock->type != SOCK_SEQPACKET)
354 return -ESOCKTNOSUPPORT;
355 if (protocol != 0)
356 return -EPROTONOSUPPORT;
357
358 err = -ENOMEM;
11aa9c28 359 sk = sk_alloc(net, PF_ALG, GFP_KERNEL, &alg_proto, kern);
03c8efc1
HX
360 if (!sk)
361 goto out;
362
363 sock->ops = &alg_proto_ops;
364 sock_init_data(sock, sk);
365
366 sk->sk_family = PF_ALG;
367 sk->sk_destruct = alg_sock_destruct;
368
369 return 0;
370out:
371 return err;
372}
373
374static const struct net_proto_family alg_family = {
375 .family = PF_ALG,
376 .create = alg_create,
377 .owner = THIS_MODULE,
378};
379
1d10eb2f 380int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len)
03c8efc1 381{
1d10eb2f
AV
382 size_t off;
383 ssize_t n;
384 int npages, i;
03c8efc1 385
1d10eb2f
AV
386 n = iov_iter_get_pages(iter, sgl->pages, len, ALG_MAX_PAGES, &off);
387 if (n < 0)
388 return n;
03c8efc1 389
9399f0c5 390 npages = (off + n + PAGE_SIZE - 1) >> PAGE_SHIFT;
03c8efc1 391 if (WARN_ON(npages == 0))
1d10eb2f 392 return -EINVAL;
66db3739
TS
393 /* Add one extra for linking */
394 sg_init_table(sgl->sg, npages + 1);
03c8efc1 395
1d10eb2f 396 for (i = 0, len = n; i < npages; i++) {
03c8efc1
HX
397 int plen = min_t(int, len, PAGE_SIZE - off);
398
399 sg_set_page(sgl->sg + i, sgl->pages[i], plen, off);
400
401 off = 0;
402 len -= plen;
03c8efc1 403 }
66db3739
TS
404 sg_mark_end(sgl->sg + npages - 1);
405 sgl->npages = npages;
406
1d10eb2f 407 return n;
03c8efc1
HX
408}
409EXPORT_SYMBOL_GPL(af_alg_make_sg);
410
66db3739
TS
411void af_alg_link_sg(struct af_alg_sgl *sgl_prev, struct af_alg_sgl *sgl_new)
412{
413 sg_unmark_end(sgl_prev->sg + sgl_prev->npages - 1);
414 sg_chain(sgl_prev->sg, sgl_prev->npages + 1, sgl_new->sg);
415}
bd507520 416EXPORT_SYMBOL_GPL(af_alg_link_sg);
66db3739 417
03c8efc1
HX
418void af_alg_free_sg(struct af_alg_sgl *sgl)
419{
420 int i;
421
66db3739 422 for (i = 0; i < sgl->npages; i++)
03c8efc1 423 put_page(sgl->pages[i]);
03c8efc1
HX
424}
425EXPORT_SYMBOL_GPL(af_alg_free_sg);
426
427int af_alg_cmsg_send(struct msghdr *msg, struct af_alg_control *con)
428{
429 struct cmsghdr *cmsg;
430
f95b414e 431 for_each_cmsghdr(cmsg, msg) {
03c8efc1
HX
432 if (!CMSG_OK(msg, cmsg))
433 return -EINVAL;
434 if (cmsg->cmsg_level != SOL_ALG)
435 continue;
436
267c4221 437 switch (cmsg->cmsg_type) {
03c8efc1
HX
438 case ALG_SET_IV:
439 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*con->iv)))
440 return -EINVAL;
441 con->iv = (void *)CMSG_DATA(cmsg);
442 if (cmsg->cmsg_len < CMSG_LEN(con->iv->ivlen +
443 sizeof(*con->iv)))
444 return -EINVAL;
445 break;
446
447 case ALG_SET_OP:
448 if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
449 return -EINVAL;
450 con->op = *(u32 *)CMSG_DATA(cmsg);
451 break;
452
af8e8073
SM
453 case ALG_SET_AEAD_ASSOCLEN:
454 if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
455 return -EINVAL;
456 con->aead_assoclen = *(u32 *)CMSG_DATA(cmsg);
457 break;
458
03c8efc1
HX
459 default:
460 return -EINVAL;
461 }
462 }
463
464 return 0;
465}
466EXPORT_SYMBOL_GPL(af_alg_cmsg_send);
467
468int af_alg_wait_for_completion(int err, struct af_alg_completion *completion)
469{
470 switch (err) {
471 case -EINPROGRESS:
472 case -EBUSY:
473 wait_for_completion(&completion->completion);
16735d02 474 reinit_completion(&completion->completion);
03c8efc1
HX
475 err = completion->err;
476 break;
477 };
478
479 return err;
480}
481EXPORT_SYMBOL_GPL(af_alg_wait_for_completion);
482
483void af_alg_complete(struct crypto_async_request *req, int err)
484{
485 struct af_alg_completion *completion = req->data;
486
7e77bdeb
RV
487 if (err == -EINPROGRESS)
488 return;
489
03c8efc1
HX
490 completion->err = err;
491 complete(&completion->completion);
492}
493EXPORT_SYMBOL_GPL(af_alg_complete);
494
495static int __init af_alg_init(void)
496{
497 int err = proto_register(&alg_proto, 0);
498
499 if (err)
500 goto out;
501
502 err = sock_register(&alg_family);
503 if (err != 0)
504 goto out_unregister_proto;
505
506out:
507 return err;
508
509out_unregister_proto:
510 proto_unregister(&alg_proto);
511 goto out;
512}
513
514static void __exit af_alg_exit(void)
515{
516 sock_unregister(PF_ALG);
517 proto_unregister(&alg_proto);
518}
519
520module_init(af_alg_init);
521module_exit(af_alg_exit);
522MODULE_LICENSE("GPL");
523MODULE_ALIAS_NETPROTO(AF_ALG);