OSDN Git Service

SUNRPC: Fix a compile warning for cmpxchg64()
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / crypto / crypto_user.c
1 /*
2  * Crypto user configuration API.
3  *
4  * Copyright (C) 2011 secunet Security Networks AG
5  * Copyright (C) 2011 Steffen Klassert <steffen.klassert@secunet.com>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms and conditions of the GNU General Public License,
9  * version 2, as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include <linux/module.h>
22 #include <linux/crypto.h>
23 #include <linux/cryptouser.h>
24 #include <linux/sched.h>
25 #include <net/netlink.h>
26 #include <linux/security.h>
27 #include <net/net_namespace.h>
28 #include <crypto/internal/skcipher.h>
29 #include <crypto/internal/rng.h>
30 #include <crypto/akcipher.h>
31
32 #include "internal.h"
33
34 #define null_terminated(x)      (strnlen(x, sizeof(x)) < sizeof(x))
35
36 static DEFINE_MUTEX(crypto_cfg_mutex);
37
38 /* The crypto netlink socket */
39 static struct sock *crypto_nlsk;
40
41 struct crypto_dump_info {
42         struct sk_buff *in_skb;
43         struct sk_buff *out_skb;
44         u32 nlmsg_seq;
45         u16 nlmsg_flags;
46 };
47
48 static struct crypto_alg *crypto_alg_match(struct crypto_user_alg *p, int exact)
49 {
50         struct crypto_alg *q, *alg = NULL;
51
52         down_read(&crypto_alg_sem);
53
54         list_for_each_entry(q, &crypto_alg_list, cra_list) {
55                 int match = 0;
56
57                 if (crypto_is_larval(q))
58                         continue;
59
60                 if ((q->cra_flags ^ p->cru_type) & p->cru_mask)
61                         continue;
62
63                 if (strlen(p->cru_driver_name))
64                         match = !strcmp(q->cra_driver_name,
65                                         p->cru_driver_name);
66                 else if (!exact)
67                         match = !strcmp(q->cra_name, p->cru_name);
68
69                 if (!match)
70                         continue;
71
72                 if (unlikely(!crypto_mod_get(q)))
73                         continue;
74
75                 alg = q;
76                 break;
77         }
78
79         up_read(&crypto_alg_sem);
80
81         return alg;
82 }
83
84 static int crypto_report_cipher(struct sk_buff *skb, struct crypto_alg *alg)
85 {
86         struct crypto_report_cipher rcipher;
87
88         strncpy(rcipher.type, "cipher", sizeof(rcipher.type));
89
90         rcipher.blocksize = alg->cra_blocksize;
91         rcipher.min_keysize = alg->cra_cipher.cia_min_keysize;
92         rcipher.max_keysize = alg->cra_cipher.cia_max_keysize;
93
94         if (nla_put(skb, CRYPTOCFGA_REPORT_CIPHER,
95                     sizeof(struct crypto_report_cipher), &rcipher))
96                 goto nla_put_failure;
97         return 0;
98
99 nla_put_failure:
100         return -EMSGSIZE;
101 }
102
103 static int crypto_report_comp(struct sk_buff *skb, struct crypto_alg *alg)
104 {
105         struct crypto_report_comp rcomp;
106
107         strncpy(rcomp.type, "compression", sizeof(rcomp.type));
108         if (nla_put(skb, CRYPTOCFGA_REPORT_COMPRESS,
109                     sizeof(struct crypto_report_comp), &rcomp))
110                 goto nla_put_failure;
111         return 0;
112
113 nla_put_failure:
114         return -EMSGSIZE;
115 }
116
117 static int crypto_report_akcipher(struct sk_buff *skb, struct crypto_alg *alg)
118 {
119         struct crypto_report_akcipher rakcipher;
120
121         strncpy(rakcipher.type, "akcipher", sizeof(rakcipher.type));
122
123         if (nla_put(skb, CRYPTOCFGA_REPORT_AKCIPHER,
124                     sizeof(struct crypto_report_akcipher), &rakcipher))
125                 goto nla_put_failure;
126         return 0;
127
128 nla_put_failure:
129         return -EMSGSIZE;
130 }
131
132 static int crypto_report_one(struct crypto_alg *alg,
133                              struct crypto_user_alg *ualg, struct sk_buff *skb)
134 {
135         strncpy(ualg->cru_name, alg->cra_name, sizeof(ualg->cru_name));
136         strncpy(ualg->cru_driver_name, alg->cra_driver_name,
137                 sizeof(ualg->cru_driver_name));
138         strncpy(ualg->cru_module_name, module_name(alg->cra_module),
139                 sizeof(ualg->cru_module_name));
140
141         ualg->cru_type = 0;
142         ualg->cru_mask = 0;
143         ualg->cru_flags = alg->cra_flags;
144         ualg->cru_refcnt = atomic_read(&alg->cra_refcnt);
145
146         if (nla_put_u32(skb, CRYPTOCFGA_PRIORITY_VAL, alg->cra_priority))
147                 goto nla_put_failure;
148         if (alg->cra_flags & CRYPTO_ALG_LARVAL) {
149                 struct crypto_report_larval rl;
150
151                 strncpy(rl.type, "larval", sizeof(rl.type));
152                 if (nla_put(skb, CRYPTOCFGA_REPORT_LARVAL,
153                             sizeof(struct crypto_report_larval), &rl))
154                         goto nla_put_failure;
155                 goto out;
156         }
157
158         if (alg->cra_type && alg->cra_type->report) {
159                 if (alg->cra_type->report(skb, alg))
160                         goto nla_put_failure;
161
162                 goto out;
163         }
164
165         switch (alg->cra_flags & (CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_LARVAL)) {
166         case CRYPTO_ALG_TYPE_CIPHER:
167                 if (crypto_report_cipher(skb, alg))
168                         goto nla_put_failure;
169
170                 break;
171         case CRYPTO_ALG_TYPE_COMPRESS:
172                 if (crypto_report_comp(skb, alg))
173                         goto nla_put_failure;
174
175                 break;
176
177         case CRYPTO_ALG_TYPE_AKCIPHER:
178                 if (crypto_report_akcipher(skb, alg))
179                         goto nla_put_failure;
180
181                 break;
182         }
183
184 out:
185         return 0;
186
187 nla_put_failure:
188         return -EMSGSIZE;
189 }
190
191 static int crypto_report_alg(struct crypto_alg *alg,
192                              struct crypto_dump_info *info)
193 {
194         struct sk_buff *in_skb = info->in_skb;
195         struct sk_buff *skb = info->out_skb;
196         struct nlmsghdr *nlh;
197         struct crypto_user_alg *ualg;
198         int err = 0;
199
200         nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, info->nlmsg_seq,
201                         CRYPTO_MSG_GETALG, sizeof(*ualg), info->nlmsg_flags);
202         if (!nlh) {
203                 err = -EMSGSIZE;
204                 goto out;
205         }
206
207         ualg = nlmsg_data(nlh);
208
209         err = crypto_report_one(alg, ualg, skb);
210         if (err) {
211                 nlmsg_cancel(skb, nlh);
212                 goto out;
213         }
214
215         nlmsg_end(skb, nlh);
216
217 out:
218         return err;
219 }
220
221 static int crypto_report(struct sk_buff *in_skb, struct nlmsghdr *in_nlh,
222                          struct nlattr **attrs)
223 {
224         struct crypto_user_alg *p = nlmsg_data(in_nlh);
225         struct crypto_alg *alg;
226         struct sk_buff *skb;
227         struct crypto_dump_info info;
228         int err;
229
230         if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
231                 return -EINVAL;
232
233         alg = crypto_alg_match(p, 0);
234         if (!alg)
235                 return -ENOENT;
236
237         err = -ENOMEM;
238         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
239         if (!skb)
240                 goto drop_alg;
241
242         info.in_skb = in_skb;
243         info.out_skb = skb;
244         info.nlmsg_seq = in_nlh->nlmsg_seq;
245         info.nlmsg_flags = 0;
246
247         err = crypto_report_alg(alg, &info);
248
249 drop_alg:
250         crypto_mod_put(alg);
251
252         if (err)
253                 return err;
254
255         return nlmsg_unicast(crypto_nlsk, skb, NETLINK_CB(in_skb).portid);
256 }
257
258 static int crypto_dump_report(struct sk_buff *skb, struct netlink_callback *cb)
259 {
260         struct crypto_alg *alg;
261         struct crypto_dump_info info;
262         int err;
263
264         if (cb->args[0])
265                 goto out;
266
267         cb->args[0] = 1;
268
269         info.in_skb = cb->skb;
270         info.out_skb = skb;
271         info.nlmsg_seq = cb->nlh->nlmsg_seq;
272         info.nlmsg_flags = NLM_F_MULTI;
273
274         list_for_each_entry(alg, &crypto_alg_list, cra_list) {
275                 err = crypto_report_alg(alg, &info);
276                 if (err)
277                         goto out_err;
278         }
279
280 out:
281         return skb->len;
282 out_err:
283         return err;
284 }
285
286 static int crypto_dump_report_done(struct netlink_callback *cb)
287 {
288         return 0;
289 }
290
291 static int crypto_update_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
292                              struct nlattr **attrs)
293 {
294         struct crypto_alg *alg;
295         struct crypto_user_alg *p = nlmsg_data(nlh);
296         struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
297         LIST_HEAD(list);
298
299         if (!netlink_capable(skb, CAP_NET_ADMIN))
300                 return -EPERM;
301
302         if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
303                 return -EINVAL;
304
305         if (priority && !strlen(p->cru_driver_name))
306                 return -EINVAL;
307
308         alg = crypto_alg_match(p, 1);
309         if (!alg)
310                 return -ENOENT;
311
312         down_write(&crypto_alg_sem);
313
314         crypto_remove_spawns(alg, &list, NULL);
315
316         if (priority)
317                 alg->cra_priority = nla_get_u32(priority);
318
319         up_write(&crypto_alg_sem);
320
321         crypto_mod_put(alg);
322         crypto_remove_final(&list);
323
324         return 0;
325 }
326
327 static int crypto_del_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
328                           struct nlattr **attrs)
329 {
330         struct crypto_alg *alg;
331         struct crypto_user_alg *p = nlmsg_data(nlh);
332         int err;
333
334         if (!netlink_capable(skb, CAP_NET_ADMIN))
335                 return -EPERM;
336
337         if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
338                 return -EINVAL;
339
340         alg = crypto_alg_match(p, 1);
341         if (!alg)
342                 return -ENOENT;
343
344         /* We can not unregister core algorithms such as aes-generic.
345          * We would loose the reference in the crypto_alg_list to this algorithm
346          * if we try to unregister. Unregistering such an algorithm without
347          * removing the module is not possible, so we restrict to crypto
348          * instances that are build from templates. */
349         err = -EINVAL;
350         if (!(alg->cra_flags & CRYPTO_ALG_INSTANCE))
351                 goto drop_alg;
352
353         err = -EBUSY;
354         if (atomic_read(&alg->cra_refcnt) > 2)
355                 goto drop_alg;
356
357         err = crypto_unregister_instance((struct crypto_instance *)alg);
358
359 drop_alg:
360         crypto_mod_put(alg);
361         return err;
362 }
363
364 static struct crypto_alg *crypto_user_skcipher_alg(const char *name, u32 type,
365                                                    u32 mask)
366 {
367         int err;
368         struct crypto_alg *alg;
369
370         type = crypto_skcipher_type(type);
371         mask = crypto_skcipher_mask(mask);
372
373         for (;;) {
374                 alg = crypto_lookup_skcipher(name,  type, mask);
375                 if (!IS_ERR(alg))
376                         return alg;
377
378                 err = PTR_ERR(alg);
379                 if (err != -EAGAIN)
380                         break;
381                 if (fatal_signal_pending(current)) {
382                         err = -EINTR;
383                         break;
384                 }
385         }
386
387         return ERR_PTR(err);
388 }
389
390 static int crypto_add_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
391                           struct nlattr **attrs)
392 {
393         int exact = 0;
394         const char *name;
395         struct crypto_alg *alg;
396         struct crypto_user_alg *p = nlmsg_data(nlh);
397         struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
398
399         if (!netlink_capable(skb, CAP_NET_ADMIN))
400                 return -EPERM;
401
402         if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
403                 return -EINVAL;
404
405         if (strlen(p->cru_driver_name))
406                 exact = 1;
407
408         if (priority && !exact)
409                 return -EINVAL;
410
411         alg = crypto_alg_match(p, exact);
412         if (alg) {
413                 crypto_mod_put(alg);
414                 return -EEXIST;
415         }
416
417         if (strlen(p->cru_driver_name))
418                 name = p->cru_driver_name;
419         else
420                 name = p->cru_name;
421
422         switch (p->cru_type & p->cru_mask & CRYPTO_ALG_TYPE_MASK) {
423         case CRYPTO_ALG_TYPE_GIVCIPHER:
424         case CRYPTO_ALG_TYPE_BLKCIPHER:
425         case CRYPTO_ALG_TYPE_ABLKCIPHER:
426                 alg = crypto_user_skcipher_alg(name, p->cru_type, p->cru_mask);
427                 break;
428         default:
429                 alg = crypto_alg_mod_lookup(name, p->cru_type, p->cru_mask);
430         }
431
432         if (IS_ERR(alg))
433                 return PTR_ERR(alg);
434
435         down_write(&crypto_alg_sem);
436
437         if (priority)
438                 alg->cra_priority = nla_get_u32(priority);
439
440         up_write(&crypto_alg_sem);
441
442         crypto_mod_put(alg);
443
444         return 0;
445 }
446
447 static int crypto_del_rng(struct sk_buff *skb, struct nlmsghdr *nlh,
448                           struct nlattr **attrs)
449 {
450         if (!netlink_capable(skb, CAP_NET_ADMIN))
451                 return -EPERM;
452         return crypto_del_default_rng();
453 }
454
455 #define MSGSIZE(type) sizeof(struct type)
456
457 static const int crypto_msg_min[CRYPTO_NR_MSGTYPES] = {
458         [CRYPTO_MSG_NEWALG      - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
459         [CRYPTO_MSG_DELALG      - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
460         [CRYPTO_MSG_UPDATEALG   - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
461         [CRYPTO_MSG_GETALG      - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
462         [CRYPTO_MSG_DELRNG      - CRYPTO_MSG_BASE] = 0,
463 };
464
465 static const struct nla_policy crypto_policy[CRYPTOCFGA_MAX+1] = {
466         [CRYPTOCFGA_PRIORITY_VAL]   = { .type = NLA_U32},
467 };
468
469 #undef MSGSIZE
470
471 static const struct crypto_link {
472         int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
473         int (*dump)(struct sk_buff *, struct netlink_callback *);
474         int (*done)(struct netlink_callback *);
475 } crypto_dispatch[CRYPTO_NR_MSGTYPES] = {
476         [CRYPTO_MSG_NEWALG      - CRYPTO_MSG_BASE] = { .doit = crypto_add_alg},
477         [CRYPTO_MSG_DELALG      - CRYPTO_MSG_BASE] = { .doit = crypto_del_alg},
478         [CRYPTO_MSG_UPDATEALG   - CRYPTO_MSG_BASE] = { .doit = crypto_update_alg},
479         [CRYPTO_MSG_GETALG      - CRYPTO_MSG_BASE] = { .doit = crypto_report,
480                                                        .dump = crypto_dump_report,
481                                                        .done = crypto_dump_report_done},
482         [CRYPTO_MSG_DELRNG      - CRYPTO_MSG_BASE] = { .doit = crypto_del_rng },
483 };
484
485 static int crypto_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
486 {
487         struct nlattr *attrs[CRYPTOCFGA_MAX+1];
488         const struct crypto_link *link;
489         int type, err;
490
491         type = nlh->nlmsg_type;
492         if (type > CRYPTO_MSG_MAX)
493                 return -EINVAL;
494
495         type -= CRYPTO_MSG_BASE;
496         link = &crypto_dispatch[type];
497
498         if ((type == (CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE) &&
499             (nlh->nlmsg_flags & NLM_F_DUMP))) {
500                 struct crypto_alg *alg;
501                 u16 dump_alloc = 0;
502
503                 if (link->dump == NULL)
504                         return -EINVAL;
505
506                 down_read(&crypto_alg_sem);
507                 list_for_each_entry(alg, &crypto_alg_list, cra_list)
508                         dump_alloc += CRYPTO_REPORT_MAXSIZE;
509
510                 {
511                         struct netlink_dump_control c = {
512                                 .dump = link->dump,
513                                 .done = link->done,
514                                 .min_dump_alloc = dump_alloc,
515                         };
516                         err = netlink_dump_start(crypto_nlsk, skb, nlh, &c);
517                 }
518                 up_read(&crypto_alg_sem);
519
520                 return err;
521         }
522
523         err = nlmsg_parse(nlh, crypto_msg_min[type], attrs, CRYPTOCFGA_MAX,
524                           crypto_policy);
525         if (err < 0)
526                 return err;
527
528         if (link->doit == NULL)
529                 return -EINVAL;
530
531         return link->doit(skb, nlh, attrs);
532 }
533
534 static void crypto_netlink_rcv(struct sk_buff *skb)
535 {
536         mutex_lock(&crypto_cfg_mutex);
537         netlink_rcv_skb(skb, &crypto_user_rcv_msg);
538         mutex_unlock(&crypto_cfg_mutex);
539 }
540
541 static int __init crypto_user_init(void)
542 {
543         struct netlink_kernel_cfg cfg = {
544                 .input  = crypto_netlink_rcv,
545         };
546
547         crypto_nlsk = netlink_kernel_create(&init_net, NETLINK_CRYPTO, &cfg);
548         if (!crypto_nlsk)
549                 return -ENOMEM;
550
551         return 0;
552 }
553
554 static void __exit crypto_user_exit(void)
555 {
556         netlink_kernel_release(crypto_nlsk);
557 }
558
559 module_init(crypto_user_init);
560 module_exit(crypto_user_exit);
561 MODULE_LICENSE("GPL");
562 MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
563 MODULE_DESCRIPTION("Crypto userspace configuration API");
564 MODULE_ALIAS("net-pf-16-proto-21");