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