crypto: Add a report function pointer to crypto_type
[firefly-linux-kernel-4.4.55.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 <net/netlink.h>
25 #include <linux/security.h>
26 #include <net/net_namespace.h>
27 #include "internal.h"
28
29 DEFINE_MUTEX(crypto_cfg_mutex);
30
31 /* The crypto netlink socket */
32 static struct sock *crypto_nlsk;
33
34 struct crypto_dump_info {
35         struct sk_buff *in_skb;
36         struct sk_buff *out_skb;
37         u32 nlmsg_seq;
38         u16 nlmsg_flags;
39 };
40
41 static struct crypto_alg *crypto_alg_match(struct crypto_user_alg *p, int exact)
42 {
43         int match;
44         struct crypto_alg *q, *alg = NULL;
45
46         down_read(&crypto_alg_sem);
47
48         if (list_empty(&crypto_alg_list))
49                 return NULL;
50
51         list_for_each_entry(q, &crypto_alg_list, cra_list) {
52
53                 if ((q->cra_flags ^ p->cru_type) & p->cru_mask)
54                         continue;
55
56                 if (strlen(p->cru_driver_name))
57                         match = !strcmp(q->cra_driver_name,
58                                         p->cru_driver_name);
59                 else if (!exact)
60                         match = !strcmp(q->cra_name, p->cru_name);
61
62                 if (match) {
63                         alg = q;
64                         break;
65                 }
66         }
67
68         up_read(&crypto_alg_sem);
69
70         return alg;
71 }
72
73 static int crypto_report_one(struct crypto_alg *alg,
74                              struct crypto_user_alg *ualg, struct sk_buff *skb)
75 {
76         memcpy(&ualg->cru_name, &alg->cra_name, sizeof(ualg->cru_name));
77         memcpy(&ualg->cru_driver_name, &alg->cra_driver_name,
78                sizeof(ualg->cru_driver_name));
79         memcpy(&ualg->cru_module_name, module_name(alg->cra_module),
80                CRYPTO_MAX_ALG_NAME);
81
82         ualg->cru_flags = alg->cra_flags;
83         ualg->cru_refcnt = atomic_read(&alg->cra_refcnt);
84
85         NLA_PUT_U32(skb, CRYPTOCFGA_PRIORITY_VAL, alg->cra_priority);
86
87         if (alg->cra_type && alg->cra_type->report) {
88                 if (alg->cra_type->report(skb, alg))
89                         goto nla_put_failure;
90         }
91
92         return 0;
93
94 nla_put_failure:
95         return -EMSGSIZE;
96 }
97
98 static int crypto_report_alg(struct crypto_alg *alg,
99                              struct crypto_dump_info *info)
100 {
101         struct sk_buff *in_skb = info->in_skb;
102         struct sk_buff *skb = info->out_skb;
103         struct nlmsghdr *nlh;
104         struct crypto_user_alg *ualg;
105         int err = 0;
106
107         nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, info->nlmsg_seq,
108                         CRYPTO_MSG_GETALG, sizeof(*ualg), info->nlmsg_flags);
109         if (!nlh) {
110                 err = -EMSGSIZE;
111                 goto out;
112         }
113
114         ualg = nlmsg_data(nlh);
115
116         err = crypto_report_one(alg, ualg, skb);
117         if (err) {
118                 nlmsg_cancel(skb, nlh);
119                 goto out;
120         }
121
122         nlmsg_end(skb, nlh);
123
124 out:
125         return err;
126 }
127
128 static int crypto_report(struct sk_buff *in_skb, struct nlmsghdr *in_nlh,
129                          struct nlattr **attrs)
130 {
131         struct crypto_user_alg *p = nlmsg_data(in_nlh);
132         struct crypto_alg *alg;
133         struct sk_buff *skb;
134         struct crypto_dump_info info;
135         int err;
136
137         if (!p->cru_driver_name)
138                 return -EINVAL;
139
140         alg = crypto_alg_match(p, 1);
141         if (!alg)
142                 return -ENOENT;
143
144         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
145         if (!skb)
146                 return -ENOMEM;
147
148         info.in_skb = in_skb;
149         info.out_skb = skb;
150         info.nlmsg_seq = in_nlh->nlmsg_seq;
151         info.nlmsg_flags = 0;
152
153         err = crypto_report_alg(alg, &info);
154         if (err)
155                 return err;
156
157         return nlmsg_unicast(crypto_nlsk, skb, NETLINK_CB(in_skb).pid);
158 }
159
160 static int crypto_dump_report(struct sk_buff *skb, struct netlink_callback *cb)
161 {
162         struct crypto_alg *alg;
163         struct crypto_dump_info info;
164         int err;
165
166         if (cb->args[0])
167                 goto out;
168
169         cb->args[0] = 1;
170
171         info.in_skb = cb->skb;
172         info.out_skb = skb;
173         info.nlmsg_seq = cb->nlh->nlmsg_seq;
174         info.nlmsg_flags = NLM_F_MULTI;
175
176         list_for_each_entry(alg, &crypto_alg_list, cra_list) {
177                 err = crypto_report_alg(alg, &info);
178                 if (err)
179                         goto out_err;
180         }
181
182 out:
183         return skb->len;
184 out_err:
185         return err;
186 }
187
188 static int crypto_dump_report_done(struct netlink_callback *cb)
189 {
190         return 0;
191 }
192
193 static int crypto_update_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
194                              struct nlattr **attrs)
195 {
196         struct crypto_alg *alg;
197         struct crypto_user_alg *p = nlmsg_data(nlh);
198         struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
199         LIST_HEAD(list);
200
201         if (priority && !strlen(p->cru_driver_name))
202                 return -EINVAL;
203
204         alg = crypto_alg_match(p, 1);
205         if (!alg)
206                 return -ENOENT;
207
208         down_write(&crypto_alg_sem);
209
210         crypto_remove_spawns(alg, &list, NULL);
211
212         if (priority)
213                 alg->cra_priority = nla_get_u32(priority);
214
215         up_write(&crypto_alg_sem);
216
217         crypto_remove_final(&list);
218
219         return 0;
220 }
221
222 static int crypto_del_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
223                           struct nlattr **attrs)
224 {
225         struct crypto_alg *alg;
226         struct crypto_user_alg *p = nlmsg_data(nlh);
227
228         alg = crypto_alg_match(p, 1);
229         if (!alg)
230                 return -ENOENT;
231
232         /* We can not unregister core algorithms such as aes-generic.
233          * We would loose the reference in the crypto_alg_list to this algorithm
234          * if we try to unregister. Unregistering such an algorithm without
235          * removing the module is not possible, so we restrict to crypto
236          * instances that are build from templates. */
237         if (!(alg->cra_flags & CRYPTO_ALG_INSTANCE))
238                 return -EINVAL;
239
240         if (atomic_read(&alg->cra_refcnt) != 1)
241                 return -EBUSY;
242
243         return crypto_unregister_alg(alg);
244 }
245
246 static int crypto_add_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
247                           struct nlattr **attrs)
248 {
249         int exact;
250         const char *name;
251         struct crypto_alg *alg;
252         struct crypto_user_alg *p = nlmsg_data(nlh);
253         struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
254
255         if (strlen(p->cru_driver_name))
256                 exact = 1;
257
258         if (priority && !exact)
259                 return -EINVAL;
260
261         alg = crypto_alg_match(p, exact);
262         if (alg)
263                 return -EEXIST;
264
265         if (strlen(p->cru_driver_name))
266                 name = p->cru_driver_name;
267         else
268                 name = p->cru_name;
269
270         alg = crypto_alg_mod_lookup(name, p->cru_type, p->cru_mask);
271         if (IS_ERR(alg))
272                 return PTR_ERR(alg);
273
274         down_write(&crypto_alg_sem);
275
276         if (priority)
277                 alg->cra_priority = nla_get_u32(priority);
278
279         up_write(&crypto_alg_sem);
280
281         crypto_mod_put(alg);
282
283         return 0;
284 }
285
286 #define MSGSIZE(type) sizeof(struct type)
287
288 static const int crypto_msg_min[CRYPTO_NR_MSGTYPES] = {
289         [CRYPTO_MSG_NEWALG      - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
290         [CRYPTO_MSG_DELALG      - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
291         [CRYPTO_MSG_UPDATEALG   - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
292         [CRYPTO_MSG_GETALG      - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
293 };
294
295 static const struct nla_policy crypto_policy[CRYPTOCFGA_MAX+1] = {
296         [CRYPTOCFGA_PRIORITY_VAL]   = { .type = NLA_U32},
297 };
298
299 #undef MSGSIZE
300
301 static struct crypto_link {
302         int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
303         int (*dump)(struct sk_buff *, struct netlink_callback *);
304         int (*done)(struct netlink_callback *);
305 } crypto_dispatch[CRYPTO_NR_MSGTYPES] = {
306         [CRYPTO_MSG_NEWALG      - CRYPTO_MSG_BASE] = { .doit = crypto_add_alg},
307         [CRYPTO_MSG_DELALG      - CRYPTO_MSG_BASE] = { .doit = crypto_del_alg},
308         [CRYPTO_MSG_UPDATEALG   - CRYPTO_MSG_BASE] = { .doit = crypto_update_alg},
309         [CRYPTO_MSG_GETALG      - CRYPTO_MSG_BASE] = { .doit = crypto_report,
310                                                        .dump = crypto_dump_report,
311                                                        .done = crypto_dump_report_done},
312 };
313
314 static int crypto_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
315 {
316         struct nlattr *attrs[CRYPTOCFGA_MAX+1];
317         struct crypto_link *link;
318         int type, err;
319
320         type = nlh->nlmsg_type;
321         if (type > CRYPTO_MSG_MAX)
322                 return -EINVAL;
323
324         type -= CRYPTO_MSG_BASE;
325         link = &crypto_dispatch[type];
326
327         if (security_netlink_recv(skb, CAP_NET_ADMIN))
328                 return -EPERM;
329
330         if ((type == (CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE) &&
331             (nlh->nlmsg_flags & NLM_F_DUMP))) {
332                 if (link->dump == NULL)
333                         return -EINVAL;
334
335                 return netlink_dump_start(crypto_nlsk, skb, nlh,
336                                           link->dump, link->done, 0);
337         }
338
339         err = nlmsg_parse(nlh, crypto_msg_min[type], attrs, CRYPTOCFGA_MAX,
340                           crypto_policy);
341         if (err < 0)
342                 return err;
343
344         if (link->doit == NULL)
345                 return -EINVAL;
346
347         return link->doit(skb, nlh, attrs);
348 }
349
350 static void crypto_netlink_rcv(struct sk_buff *skb)
351 {
352         mutex_lock(&crypto_cfg_mutex);
353         netlink_rcv_skb(skb, &crypto_user_rcv_msg);
354         mutex_unlock(&crypto_cfg_mutex);
355 }
356
357 static int __init crypto_user_init(void)
358 {
359         crypto_nlsk = netlink_kernel_create(&init_net, NETLINK_CRYPTO,
360                                             0, crypto_netlink_rcv,
361                                             NULL, THIS_MODULE);
362         if (!crypto_nlsk)
363                 return -ENOMEM;
364
365         return 0;
366 }
367
368 static void __exit crypto_user_exit(void)
369 {
370         netlink_kernel_release(crypto_nlsk);
371 }
372
373 module_init(crypto_user_init);
374 module_exit(crypto_user_exit);
375 MODULE_LICENSE("GPL");
376 MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
377 MODULE_DESCRIPTION("Crypto userspace configuration API");