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