crypto: af_alg - Disallow bind/setkey/... after accept(2)
[pandora-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/security.h>
25
26 struct alg_type_list {
27         const struct af_alg_type *type;
28         struct list_head list;
29 };
30
31 static atomic_long_t alg_memory_allocated;
32
33 static 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
40 static LIST_HEAD(alg_types);
41 static DECLARE_RWSEM(alg_types_sem);
42
43 static 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
62 int 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
83 unlock:
84         up_write(&alg_types_sem);
85
86         return err;
87 }
88 EXPORT_SYMBOL_GPL(af_alg_register_type);
89
90 int 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 }
109 EXPORT_SYMBOL_GPL(af_alg_unregister_type);
110
111 static 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
120 int af_alg_release(struct socket *sock)
121 {
122         if (sock->sk)
123                 sock_put(sock->sk);
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         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 }
143 EXPORT_SYMBOL_GPL(af_alg_release_parent);
144
145 static int alg_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
146 {
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         sa->salg_type[sizeof(sa->salg_type) - 1] = 0;
161         sa->salg_name[sizeof(sa->salg_name) - 1] = 0;
162
163         type = alg_get_type(sa->salg_type);
164         if (IS_ERR(type) && PTR_ERR(type) == -ENOENT) {
165                 request_module("algif-%s", sa->salg_type);
166                 type = alg_get_type(sa->salg_type);
167         }
168
169         if (IS_ERR(type))
170                 return PTR_ERR(type);
171
172         private = type->bind(sa->salg_name, sa->salg_feat, sa->salg_mask);
173         if (IS_ERR(private)) {
174                 module_put(type->owner);
175                 return PTR_ERR(private);
176         }
177
178         err = -EBUSY;
179         lock_sock(sk);
180         if (ask->refcnt)
181                 goto unlock;
182
183         swap(ask->type, type);
184         swap(ask->private, private);
185
186         err = 0;
187
188 unlock:
189         release_sock(sk);
190
191         alg_do_release(type, private);
192
193         return err;
194 }
195
196 static int alg_setkey(struct sock *sk, char __user *ukey,
197                       unsigned int keylen)
198 {
199         struct alg_sock *ask = alg_sk(sk);
200         const struct af_alg_type *type = ask->type;
201         u8 *key;
202         int err;
203
204         key = sock_kmalloc(sk, keylen, GFP_KERNEL);
205         if (!key)
206                 return -ENOMEM;
207
208         err = -EFAULT;
209         if (copy_from_user(key, ukey, keylen))
210                 goto out;
211
212         err = type->setkey(ask->private, key, keylen);
213
214 out:
215         sock_kfree_s(sk, key, keylen);
216
217         return err;
218 }
219
220 static int alg_setsockopt(struct socket *sock, int level, int optname,
221                           char __user *optval, unsigned int optlen)
222 {
223         struct sock *sk = sock->sk;
224         struct alg_sock *ask = alg_sk(sk);
225         const struct af_alg_type *type;
226         int err = -EBUSY;
227
228         lock_sock(sk);
229         if (ask->refcnt)
230                 goto unlock;
231
232         type = ask->type;
233
234         err = -ENOPROTOOPT;
235         if (level != SOL_ALG || !type)
236                 goto unlock;
237
238         switch (optname) {
239         case ALG_SET_KEY:
240                 if (sock->state == SS_CONNECTED)
241                         goto unlock;
242                 if (!type->setkey)
243                         goto unlock;
244
245                 err = alg_setkey(sk, optval, optlen);
246         }
247
248 unlock:
249         release_sock(sk);
250
251         return err;
252 }
253
254 int af_alg_accept(struct sock *sk, struct socket *newsock)
255 {
256         struct alg_sock *ask = alg_sk(sk);
257         const struct af_alg_type *type;
258         struct sock *sk2;
259         int err;
260
261         lock_sock(sk);
262         type = ask->type;
263
264         err = -EINVAL;
265         if (!type)
266                 goto unlock;
267
268         sk2 = sk_alloc(sock_net(sk), PF_ALG, GFP_KERNEL, &alg_proto);
269         err = -ENOMEM;
270         if (!sk2)
271                 goto unlock;
272
273         sock_init_data(newsock, sk2);
274         sock_graft(sk2, newsock);
275         security_sk_clone(sk, sk2);
276
277         err = type->accept(ask->private, sk2);
278         if (err)
279                 goto unlock;
280
281         sk2->sk_family = PF_ALG;
282
283         if (!ask->refcnt++)
284                 sock_hold(sk);
285         alg_sk(sk2)->parent = sk;
286         alg_sk(sk2)->type = type;
287
288         newsock->ops = type->ops;
289         newsock->state = SS_CONNECTED;
290
291         err = 0;
292
293 unlock:
294         release_sock(sk);
295
296         return err;
297 }
298 EXPORT_SYMBOL_GPL(af_alg_accept);
299
300 static int alg_accept(struct socket *sock, struct socket *newsock, int flags)
301 {
302         return af_alg_accept(sock->sk, newsock);
303 }
304
305 static const struct proto_ops alg_proto_ops = {
306         .family         =       PF_ALG,
307         .owner          =       THIS_MODULE,
308
309         .connect        =       sock_no_connect,
310         .socketpair     =       sock_no_socketpair,
311         .getname        =       sock_no_getname,
312         .ioctl          =       sock_no_ioctl,
313         .listen         =       sock_no_listen,
314         .shutdown       =       sock_no_shutdown,
315         .getsockopt     =       sock_no_getsockopt,
316         .mmap           =       sock_no_mmap,
317         .sendpage       =       sock_no_sendpage,
318         .sendmsg        =       sock_no_sendmsg,
319         .recvmsg        =       sock_no_recvmsg,
320         .poll           =       sock_no_poll,
321
322         .bind           =       alg_bind,
323         .release        =       af_alg_release,
324         .setsockopt     =       alg_setsockopt,
325         .accept         =       alg_accept,
326 };
327
328 static void alg_sock_destruct(struct sock *sk)
329 {
330         struct alg_sock *ask = alg_sk(sk);
331
332         alg_do_release(ask->type, ask->private);
333 }
334
335 static int alg_create(struct net *net, struct socket *sock, int protocol,
336                       int kern)
337 {
338         struct sock *sk;
339         int err;
340
341         if (sock->type != SOCK_SEQPACKET)
342                 return -ESOCKTNOSUPPORT;
343         if (protocol != 0)
344                 return -EPROTONOSUPPORT;
345
346         err = -ENOMEM;
347         sk = sk_alloc(net, PF_ALG, GFP_KERNEL, &alg_proto);
348         if (!sk)
349                 goto out;
350
351         sock->ops = &alg_proto_ops;
352         sock_init_data(sock, sk);
353
354         sk->sk_family = PF_ALG;
355         sk->sk_destruct = alg_sock_destruct;
356
357         return 0;
358 out:
359         return err;
360 }
361
362 static const struct net_proto_family alg_family = {
363         .family =       PF_ALG,
364         .create =       alg_create,
365         .owner  =       THIS_MODULE,
366 };
367
368 int af_alg_make_sg(struct af_alg_sgl *sgl, void __user *addr, int len,
369                    int write)
370 {
371         unsigned long from = (unsigned long)addr;
372         unsigned long npages;
373         unsigned off;
374         int err;
375         int i;
376
377         err = -EFAULT;
378         if (!access_ok(write ? VERIFY_READ : VERIFY_WRITE, addr, len))
379                 goto out;
380
381         off = from & ~PAGE_MASK;
382         npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
383         if (npages > ALG_MAX_PAGES)
384                 npages = ALG_MAX_PAGES;
385
386         err = get_user_pages_fast(from, npages, write, sgl->pages);
387         if (err < 0)
388                 goto out;
389
390         npages = err;
391         err = -EINVAL;
392         if (WARN_ON(npages == 0))
393                 goto out;
394
395         err = 0;
396
397         sg_init_table(sgl->sg, npages);
398
399         for (i = 0; i < npages; i++) {
400                 int plen = min_t(int, len, PAGE_SIZE - off);
401
402                 sg_set_page(sgl->sg + i, sgl->pages[i], plen, off);
403
404                 off = 0;
405                 len -= plen;
406                 err += plen;
407         }
408
409 out:
410         return err;
411 }
412 EXPORT_SYMBOL_GPL(af_alg_make_sg);
413
414 void af_alg_free_sg(struct af_alg_sgl *sgl)
415 {
416         int i;
417
418         i = 0;
419         do {
420                 put_page(sgl->pages[i]);
421         } while (!sg_is_last(sgl->sg + (i++)));
422 }
423 EXPORT_SYMBOL_GPL(af_alg_free_sg);
424
425 int af_alg_cmsg_send(struct msghdr *msg, struct af_alg_control *con)
426 {
427         struct cmsghdr *cmsg;
428
429         for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
430                 if (!CMSG_OK(msg, cmsg))
431                         return -EINVAL;
432                 if (cmsg->cmsg_level != SOL_ALG)
433                         continue;
434
435                 switch(cmsg->cmsg_type) {
436                 case ALG_SET_IV:
437                         if (cmsg->cmsg_len < CMSG_LEN(sizeof(*con->iv)))
438                                 return -EINVAL;
439                         con->iv = (void *)CMSG_DATA(cmsg);
440                         if (cmsg->cmsg_len < CMSG_LEN(con->iv->ivlen +
441                                                       sizeof(*con->iv)))
442                                 return -EINVAL;
443                         break;
444
445                 case ALG_SET_OP:
446                         if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
447                                 return -EINVAL;
448                         con->op = *(u32 *)CMSG_DATA(cmsg);
449                         break;
450
451                 default:
452                         return -EINVAL;
453                 }
454         }
455
456         return 0;
457 }
458 EXPORT_SYMBOL_GPL(af_alg_cmsg_send);
459
460 int af_alg_wait_for_completion(int err, struct af_alg_completion *completion)
461 {
462         switch (err) {
463         case -EINPROGRESS:
464         case -EBUSY:
465                 wait_for_completion(&completion->completion);
466                 INIT_COMPLETION(completion->completion);
467                 err = completion->err;
468                 break;
469         };
470
471         return err;
472 }
473 EXPORT_SYMBOL_GPL(af_alg_wait_for_completion);
474
475 void af_alg_complete(struct crypto_async_request *req, int err)
476 {
477         struct af_alg_completion *completion = req->data;
478
479         if (err == -EINPROGRESS)
480                 return;
481
482         completion->err = err;
483         complete(&completion->completion);
484 }
485 EXPORT_SYMBOL_GPL(af_alg_complete);
486
487 static int __init af_alg_init(void)
488 {
489         int err = proto_register(&alg_proto, 0);
490
491         if (err)
492                 goto out;
493
494         err = sock_register(&alg_family);
495         if (err != 0)
496                 goto out_unregister_proto;
497
498 out:
499         return err;
500
501 out_unregister_proto:
502         proto_unregister(&alg_proto);
503         goto out;
504 }
505
506 static void __exit af_alg_exit(void)
507 {
508         sock_unregister(PF_ALG);
509         proto_unregister(&alg_proto);
510 }
511
512 module_init(af_alg_init);
513 module_exit(af_alg_exit);
514 MODULE_LICENSE("GPL");
515 MODULE_ALIAS_NETPROTO(AF_ALG);