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