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