blob: 2abca780312d74246c60d7fcc9f6e9bf71f421c0 [file] [log] [blame]
Steffen Klasserta38f7902011-09-27 07:23:50 +02001/*
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
29DEFINE_MUTEX(crypto_cfg_mutex);
30
31/* The crypto netlink socket */
32static struct sock *crypto_nlsk;
33
34struct 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
41static struct crypto_alg *crypto_alg_match(struct crypto_user_alg *p, int exact)
42{
Steffen Klasserta38f7902011-09-27 07:23:50 +020043 struct crypto_alg *q, *alg = NULL;
44
45 down_read(&crypto_alg_sem);
46
47 if (list_empty(&crypto_alg_list))
48 return NULL;
49
50 list_for_each_entry(q, &crypto_alg_list, cra_list) {
Herbert Xue6ea64e2011-10-21 14:37:10 +020051 int match = 0;
Steffen Klasserta38f7902011-09-27 07:23:50 +020052
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
Steffen Klassert07a5fa42011-09-27 07:48:01 +020073static int crypto_report_cipher(struct sk_buff *skb, struct crypto_alg *alg)
74{
75 struct crypto_report_cipher rcipher;
76
77 snprintf(rcipher.type, CRYPTO_MAX_ALG_NAME, "%s", "cipher");
78
79 rcipher.blocksize = alg->cra_blocksize;
80 rcipher.min_keysize = alg->cra_cipher.cia_min_keysize;
81 rcipher.max_keysize = alg->cra_cipher.cia_max_keysize;
82
83 NLA_PUT(skb, CRYPTOCFGA_REPORT_CIPHER,
84 sizeof(struct crypto_report_cipher), &rcipher);
85
86 return 0;
87
88nla_put_failure:
89 return -EMSGSIZE;
90}
91
Steffen Klassert540b97c2011-09-27 07:48:48 +020092static int crypto_report_comp(struct sk_buff *skb, struct crypto_alg *alg)
93{
94 struct crypto_report_comp rcomp;
95
96 snprintf(rcomp.type, CRYPTO_MAX_ALG_NAME, "%s", "compression");
97
98 NLA_PUT(skb, CRYPTOCFGA_REPORT_COMPRESS,
99 sizeof(struct crypto_report_comp), &rcomp);
100
101 return 0;
102
103nla_put_failure:
104 return -EMSGSIZE;
105}
106
Steffen Klasserta38f7902011-09-27 07:23:50 +0200107static int crypto_report_one(struct crypto_alg *alg,
108 struct crypto_user_alg *ualg, struct sk_buff *skb)
109{
110 memcpy(&ualg->cru_name, &alg->cra_name, sizeof(ualg->cru_name));
111 memcpy(&ualg->cru_driver_name, &alg->cra_driver_name,
112 sizeof(ualg->cru_driver_name));
113 memcpy(&ualg->cru_module_name, module_name(alg->cra_module),
114 CRYPTO_MAX_ALG_NAME);
115
116 ualg->cru_flags = alg->cra_flags;
117 ualg->cru_refcnt = atomic_read(&alg->cra_refcnt);
118
119 NLA_PUT_U32(skb, CRYPTOCFGA_PRIORITY_VAL, alg->cra_priority);
120
Steffen Klassert6c5a86f52011-09-27 07:25:05 +0200121 if (alg->cra_flags & CRYPTO_ALG_LARVAL) {
122 struct crypto_report_larval rl;
123
124 snprintf(rl.type, CRYPTO_MAX_ALG_NAME, "%s", "larval");
125
126 NLA_PUT(skb, CRYPTOCFGA_REPORT_LARVAL,
127 sizeof(struct crypto_report_larval), &rl);
128
129 goto out;
130 }
131
Steffen Klassertb6aa63c2011-09-27 07:24:29 +0200132 if (alg->cra_type && alg->cra_type->report) {
133 if (alg->cra_type->report(skb, alg))
134 goto nla_put_failure;
Steffen Klassert07a5fa42011-09-27 07:48:01 +0200135
136 goto out;
137 }
138
139 switch (alg->cra_flags & (CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_LARVAL)) {
140 case CRYPTO_ALG_TYPE_CIPHER:
141 if (crypto_report_cipher(skb, alg))
142 goto nla_put_failure;
143
144 break;
Steffen Klassert540b97c2011-09-27 07:48:48 +0200145 case CRYPTO_ALG_TYPE_COMPRESS:
146 if (crypto_report_comp(skb, alg))
147 goto nla_put_failure;
148
149 break;
Steffen Klassertb6aa63c2011-09-27 07:24:29 +0200150 }
151
Steffen Klassert6c5a86f52011-09-27 07:25:05 +0200152out:
Steffen Klasserta38f7902011-09-27 07:23:50 +0200153 return 0;
154
155nla_put_failure:
156 return -EMSGSIZE;
157}
158
159static int crypto_report_alg(struct crypto_alg *alg,
160 struct crypto_dump_info *info)
161{
162 struct sk_buff *in_skb = info->in_skb;
163 struct sk_buff *skb = info->out_skb;
164 struct nlmsghdr *nlh;
165 struct crypto_user_alg *ualg;
166 int err = 0;
167
168 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, info->nlmsg_seq,
169 CRYPTO_MSG_GETALG, sizeof(*ualg), info->nlmsg_flags);
170 if (!nlh) {
171 err = -EMSGSIZE;
172 goto out;
173 }
174
175 ualg = nlmsg_data(nlh);
176
177 err = crypto_report_one(alg, ualg, skb);
178 if (err) {
179 nlmsg_cancel(skb, nlh);
180 goto out;
181 }
182
183 nlmsg_end(skb, nlh);
184
185out:
186 return err;
187}
188
189static int crypto_report(struct sk_buff *in_skb, struct nlmsghdr *in_nlh,
190 struct nlattr **attrs)
191{
192 struct crypto_user_alg *p = nlmsg_data(in_nlh);
193 struct crypto_alg *alg;
194 struct sk_buff *skb;
195 struct crypto_dump_info info;
196 int err;
197
198 if (!p->cru_driver_name)
199 return -EINVAL;
200
201 alg = crypto_alg_match(p, 1);
202 if (!alg)
203 return -ENOENT;
204
205 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
206 if (!skb)
207 return -ENOMEM;
208
209 info.in_skb = in_skb;
210 info.out_skb = skb;
211 info.nlmsg_seq = in_nlh->nlmsg_seq;
212 info.nlmsg_flags = 0;
213
214 err = crypto_report_alg(alg, &info);
215 if (err)
216 return err;
217
218 return nlmsg_unicast(crypto_nlsk, skb, NETLINK_CB(in_skb).pid);
219}
220
221static int crypto_dump_report(struct sk_buff *skb, struct netlink_callback *cb)
222{
223 struct crypto_alg *alg;
224 struct crypto_dump_info info;
225 int err;
226
227 if (cb->args[0])
228 goto out;
229
230 cb->args[0] = 1;
231
232 info.in_skb = cb->skb;
233 info.out_skb = skb;
234 info.nlmsg_seq = cb->nlh->nlmsg_seq;
235 info.nlmsg_flags = NLM_F_MULTI;
236
237 list_for_each_entry(alg, &crypto_alg_list, cra_list) {
238 err = crypto_report_alg(alg, &info);
239 if (err)
240 goto out_err;
241 }
242
243out:
244 return skb->len;
245out_err:
246 return err;
247}
248
249static int crypto_dump_report_done(struct netlink_callback *cb)
250{
251 return 0;
252}
253
254static int crypto_update_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
255 struct nlattr **attrs)
256{
257 struct crypto_alg *alg;
258 struct crypto_user_alg *p = nlmsg_data(nlh);
259 struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
260 LIST_HEAD(list);
261
262 if (priority && !strlen(p->cru_driver_name))
263 return -EINVAL;
264
265 alg = crypto_alg_match(p, 1);
266 if (!alg)
267 return -ENOENT;
268
269 down_write(&crypto_alg_sem);
270
271 crypto_remove_spawns(alg, &list, NULL);
272
273 if (priority)
274 alg->cra_priority = nla_get_u32(priority);
275
276 up_write(&crypto_alg_sem);
277
278 crypto_remove_final(&list);
279
280 return 0;
281}
282
283static int crypto_del_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
284 struct nlattr **attrs)
285{
286 struct crypto_alg *alg;
287 struct crypto_user_alg *p = nlmsg_data(nlh);
288
289 alg = crypto_alg_match(p, 1);
290 if (!alg)
291 return -ENOENT;
292
293 /* We can not unregister core algorithms such as aes-generic.
294 * We would loose the reference in the crypto_alg_list to this algorithm
295 * if we try to unregister. Unregistering such an algorithm without
296 * removing the module is not possible, so we restrict to crypto
297 * instances that are build from templates. */
298 if (!(alg->cra_flags & CRYPTO_ALG_INSTANCE))
299 return -EINVAL;
300
301 if (atomic_read(&alg->cra_refcnt) != 1)
302 return -EBUSY;
303
304 return crypto_unregister_alg(alg);
305}
306
307static int crypto_add_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
308 struct nlattr **attrs)
309{
310 int exact;
311 const char *name;
312 struct crypto_alg *alg;
313 struct crypto_user_alg *p = nlmsg_data(nlh);
314 struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
315
316 if (strlen(p->cru_driver_name))
317 exact = 1;
318
319 if (priority && !exact)
320 return -EINVAL;
321
322 alg = crypto_alg_match(p, exact);
323 if (alg)
324 return -EEXIST;
325
326 if (strlen(p->cru_driver_name))
327 name = p->cru_driver_name;
328 else
329 name = p->cru_name;
330
331 alg = crypto_alg_mod_lookup(name, p->cru_type, p->cru_mask);
332 if (IS_ERR(alg))
333 return PTR_ERR(alg);
334
335 down_write(&crypto_alg_sem);
336
337 if (priority)
338 alg->cra_priority = nla_get_u32(priority);
339
340 up_write(&crypto_alg_sem);
341
342 crypto_mod_put(alg);
343
344 return 0;
345}
346
347#define MSGSIZE(type) sizeof(struct type)
348
349static const int crypto_msg_min[CRYPTO_NR_MSGTYPES] = {
350 [CRYPTO_MSG_NEWALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
351 [CRYPTO_MSG_DELALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
352 [CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
353 [CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
354};
355
356static const struct nla_policy crypto_policy[CRYPTOCFGA_MAX+1] = {
357 [CRYPTOCFGA_PRIORITY_VAL] = { .type = NLA_U32},
358};
359
360#undef MSGSIZE
361
362static struct crypto_link {
363 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
364 int (*dump)(struct sk_buff *, struct netlink_callback *);
365 int (*done)(struct netlink_callback *);
366} crypto_dispatch[CRYPTO_NR_MSGTYPES] = {
367 [CRYPTO_MSG_NEWALG - CRYPTO_MSG_BASE] = { .doit = crypto_add_alg},
368 [CRYPTO_MSG_DELALG - CRYPTO_MSG_BASE] = { .doit = crypto_del_alg},
369 [CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = { .doit = crypto_update_alg},
370 [CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = { .doit = crypto_report,
371 .dump = crypto_dump_report,
372 .done = crypto_dump_report_done},
373};
374
375static int crypto_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
376{
377 struct nlattr *attrs[CRYPTOCFGA_MAX+1];
378 struct crypto_link *link;
379 int type, err;
380
381 type = nlh->nlmsg_type;
382 if (type > CRYPTO_MSG_MAX)
383 return -EINVAL;
384
385 type -= CRYPTO_MSG_BASE;
386 link = &crypto_dispatch[type];
387
388 if (security_netlink_recv(skb, CAP_NET_ADMIN))
389 return -EPERM;
390
391 if ((type == (CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE) &&
392 (nlh->nlmsg_flags & NLM_F_DUMP))) {
393 if (link->dump == NULL)
394 return -EINVAL;
395
396 return netlink_dump_start(crypto_nlsk, skb, nlh,
397 link->dump, link->done, 0);
398 }
399
400 err = nlmsg_parse(nlh, crypto_msg_min[type], attrs, CRYPTOCFGA_MAX,
401 crypto_policy);
402 if (err < 0)
403 return err;
404
405 if (link->doit == NULL)
406 return -EINVAL;
407
408 return link->doit(skb, nlh, attrs);
409}
410
411static void crypto_netlink_rcv(struct sk_buff *skb)
412{
413 mutex_lock(&crypto_cfg_mutex);
414 netlink_rcv_skb(skb, &crypto_user_rcv_msg);
415 mutex_unlock(&crypto_cfg_mutex);
416}
417
418static int __init crypto_user_init(void)
419{
420 crypto_nlsk = netlink_kernel_create(&init_net, NETLINK_CRYPTO,
421 0, crypto_netlink_rcv,
422 NULL, THIS_MODULE);
423 if (!crypto_nlsk)
424 return -ENOMEM;
425
426 return 0;
427}
428
429static void __exit crypto_user_exit(void)
430{
431 netlink_kernel_release(crypto_nlsk);
432}
433
434module_init(crypto_user_init);
435module_exit(crypto_user_exit);
436MODULE_LICENSE("GPL");
437MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
438MODULE_DESCRIPTION("Crypto userspace configuration API");