1 // SPDX-License-Identifier: GPL-2.0-only
3 * Crypto user configuration API.
5 * Copyright (C) 2011 secunet Security Networks AG
6 * Copyright (C) 2011 Steffen Klassert <steffen.klassert@secunet.com>
9 #include <linux/module.h>
10 #include <linux/crypto.h>
11 #include <linux/cryptouser.h>
12 #include <linux/sched.h>
13 #include <linux/security.h>
14 #include <net/netlink.h>
15 #include <net/net_namespace.h>
17 #include <crypto/internal/skcipher.h>
18 #include <crypto/internal/rng.h>
19 #include <crypto/akcipher.h>
20 #include <crypto/kpp.h>
21 #include <crypto/internal/cryptouser.h>
25 #define null_terminated(x) (strnlen(x, sizeof(x)) < sizeof(x))
27 static DEFINE_MUTEX(crypto_cfg_mutex
);
29 struct crypto_dump_info
{
30 struct sk_buff
*in_skb
;
31 struct sk_buff
*out_skb
;
36 struct crypto_alg
*crypto_alg_match(struct crypto_user_alg
*p
, int exact
)
38 struct crypto_alg
*q
, *alg
= NULL
;
40 down_read(&crypto_alg_sem
);
42 list_for_each_entry(q
, &crypto_alg_list
, cra_list
) {
45 if (crypto_is_larval(q
))
48 if ((q
->cra_flags
^ p
->cru_type
) & p
->cru_mask
)
51 if (strlen(p
->cru_driver_name
))
52 match
= !strcmp(q
->cra_driver_name
,
55 match
= !strcmp(q
->cra_name
, p
->cru_name
);
60 if (unlikely(!crypto_mod_get(q
)))
67 up_read(&crypto_alg_sem
);
72 static int crypto_report_cipher(struct sk_buff
*skb
, struct crypto_alg
*alg
)
74 struct crypto_report_cipher rcipher
;
76 memset(&rcipher
, 0, sizeof(rcipher
));
78 strscpy(rcipher
.type
, "cipher", sizeof(rcipher
.type
));
80 rcipher
.blocksize
= alg
->cra_blocksize
;
81 rcipher
.min_keysize
= alg
->cra_cipher
.cia_min_keysize
;
82 rcipher
.max_keysize
= alg
->cra_cipher
.cia_max_keysize
;
84 return nla_put(skb
, CRYPTOCFGA_REPORT_CIPHER
,
85 sizeof(rcipher
), &rcipher
);
88 static int crypto_report_comp(struct sk_buff
*skb
, struct crypto_alg
*alg
)
90 struct crypto_report_comp rcomp
;
92 memset(&rcomp
, 0, sizeof(rcomp
));
94 strscpy(rcomp
.type
, "compression", sizeof(rcomp
.type
));
96 return nla_put(skb
, CRYPTOCFGA_REPORT_COMPRESS
, sizeof(rcomp
), &rcomp
);
99 static int crypto_report_one(struct crypto_alg
*alg
,
100 struct crypto_user_alg
*ualg
, struct sk_buff
*skb
)
102 memset(ualg
, 0, sizeof(*ualg
));
104 strscpy(ualg
->cru_name
, alg
->cra_name
, sizeof(ualg
->cru_name
));
105 strscpy(ualg
->cru_driver_name
, alg
->cra_driver_name
,
106 sizeof(ualg
->cru_driver_name
));
107 strscpy(ualg
->cru_module_name
, module_name(alg
->cra_module
),
108 sizeof(ualg
->cru_module_name
));
112 ualg
->cru_flags
= alg
->cra_flags
;
113 ualg
->cru_refcnt
= refcount_read(&alg
->cra_refcnt
);
115 if (nla_put_u32(skb
, CRYPTOCFGA_PRIORITY_VAL
, alg
->cra_priority
))
116 goto nla_put_failure
;
117 if (alg
->cra_flags
& CRYPTO_ALG_LARVAL
) {
118 struct crypto_report_larval rl
;
120 memset(&rl
, 0, sizeof(rl
));
121 strscpy(rl
.type
, "larval", sizeof(rl
.type
));
122 if (nla_put(skb
, CRYPTOCFGA_REPORT_LARVAL
, sizeof(rl
), &rl
))
123 goto nla_put_failure
;
127 if (alg
->cra_type
&& alg
->cra_type
->report
) {
128 if (alg
->cra_type
->report(skb
, alg
))
129 goto nla_put_failure
;
134 switch (alg
->cra_flags
& (CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_LARVAL
)) {
135 case CRYPTO_ALG_TYPE_CIPHER
:
136 if (crypto_report_cipher(skb
, alg
))
137 goto nla_put_failure
;
140 case CRYPTO_ALG_TYPE_COMPRESS
:
141 if (crypto_report_comp(skb
, alg
))
142 goto nla_put_failure
;
154 static int crypto_report_alg(struct crypto_alg
*alg
,
155 struct crypto_dump_info
*info
)
157 struct sk_buff
*in_skb
= info
->in_skb
;
158 struct sk_buff
*skb
= info
->out_skb
;
159 struct nlmsghdr
*nlh
;
160 struct crypto_user_alg
*ualg
;
163 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).portid
, info
->nlmsg_seq
,
164 CRYPTO_MSG_GETALG
, sizeof(*ualg
), info
->nlmsg_flags
);
170 ualg
= nlmsg_data(nlh
);
172 err
= crypto_report_one(alg
, ualg
, skb
);
174 nlmsg_cancel(skb
, nlh
);
184 static int crypto_report(struct sk_buff
*in_skb
, struct nlmsghdr
*in_nlh
,
185 struct nlattr
**attrs
)
187 struct net
*net
= sock_net(in_skb
->sk
);
188 struct crypto_user_alg
*p
= nlmsg_data(in_nlh
);
189 struct crypto_alg
*alg
;
191 struct crypto_dump_info info
;
194 if (!null_terminated(p
->cru_name
) || !null_terminated(p
->cru_driver_name
))
197 alg
= crypto_alg_match(p
, 0);
202 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
206 info
.in_skb
= in_skb
;
208 info
.nlmsg_seq
= in_nlh
->nlmsg_seq
;
209 info
.nlmsg_flags
= 0;
211 err
= crypto_report_alg(alg
, &info
);
221 return nlmsg_unicast(net
->crypto_nlsk
, skb
, NETLINK_CB(in_skb
).portid
);
224 static int crypto_dump_report(struct sk_buff
*skb
, struct netlink_callback
*cb
)
226 const size_t start_pos
= cb
->args
[0];
228 struct crypto_dump_info info
;
229 struct crypto_alg
*alg
;
232 info
.in_skb
= cb
->skb
;
234 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
235 info
.nlmsg_flags
= NLM_F_MULTI
;
237 down_read(&crypto_alg_sem
);
238 list_for_each_entry(alg
, &crypto_alg_list
, cra_list
) {
239 if (pos
>= start_pos
) {
240 res
= crypto_report_alg(alg
, &info
);
241 if (res
== -EMSGSIZE
)
251 up_read(&crypto_alg_sem
);
255 static int crypto_dump_report_done(struct netlink_callback
*cb
)
260 static int crypto_update_alg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
261 struct nlattr
**attrs
)
263 struct crypto_alg
*alg
;
264 struct crypto_user_alg
*p
= nlmsg_data(nlh
);
265 struct nlattr
*priority
= attrs
[CRYPTOCFGA_PRIORITY_VAL
];
268 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
271 if (!null_terminated(p
->cru_name
) || !null_terminated(p
->cru_driver_name
))
274 if (priority
&& !strlen(p
->cru_driver_name
))
277 alg
= crypto_alg_match(p
, 1);
281 down_write(&crypto_alg_sem
);
283 crypto_remove_spawns(alg
, &list
, NULL
);
286 alg
->cra_priority
= nla_get_u32(priority
);
288 up_write(&crypto_alg_sem
);
291 crypto_remove_final(&list
);
296 static int crypto_del_alg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
297 struct nlattr
**attrs
)
299 struct crypto_alg
*alg
;
300 struct crypto_user_alg
*p
= nlmsg_data(nlh
);
303 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
306 if (!null_terminated(p
->cru_name
) || !null_terminated(p
->cru_driver_name
))
309 alg
= crypto_alg_match(p
, 1);
313 /* We can not unregister core algorithms such as aes-generic.
314 * We would loose the reference in the crypto_alg_list to this algorithm
315 * if we try to unregister. Unregistering such an algorithm without
316 * removing the module is not possible, so we restrict to crypto
317 * instances that are build from templates. */
319 if (!(alg
->cra_flags
& CRYPTO_ALG_INSTANCE
))
323 if (refcount_read(&alg
->cra_refcnt
) > 2)
326 err
= crypto_unregister_instance((struct crypto_instance
*)alg
);
333 static int crypto_add_alg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
334 struct nlattr
**attrs
)
338 struct crypto_alg
*alg
;
339 struct crypto_user_alg
*p
= nlmsg_data(nlh
);
340 struct nlattr
*priority
= attrs
[CRYPTOCFGA_PRIORITY_VAL
];
342 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
345 if (!null_terminated(p
->cru_name
) || !null_terminated(p
->cru_driver_name
))
348 if (strlen(p
->cru_driver_name
))
351 if (priority
&& !exact
)
354 alg
= crypto_alg_match(p
, exact
);
360 if (strlen(p
->cru_driver_name
))
361 name
= p
->cru_driver_name
;
365 alg
= crypto_alg_mod_lookup(name
, p
->cru_type
, p
->cru_mask
);
369 down_write(&crypto_alg_sem
);
372 alg
->cra_priority
= nla_get_u32(priority
);
374 up_write(&crypto_alg_sem
);
381 static int crypto_del_rng(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
382 struct nlattr
**attrs
)
384 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
386 return crypto_del_default_rng();
389 #define MSGSIZE(type) sizeof(struct type)
391 static const int crypto_msg_min
[CRYPTO_NR_MSGTYPES
] = {
392 [CRYPTO_MSG_NEWALG
- CRYPTO_MSG_BASE
] = MSGSIZE(crypto_user_alg
),
393 [CRYPTO_MSG_DELALG
- CRYPTO_MSG_BASE
] = MSGSIZE(crypto_user_alg
),
394 [CRYPTO_MSG_UPDATEALG
- CRYPTO_MSG_BASE
] = MSGSIZE(crypto_user_alg
),
395 [CRYPTO_MSG_GETALG
- CRYPTO_MSG_BASE
] = MSGSIZE(crypto_user_alg
),
396 [CRYPTO_MSG_DELRNG
- CRYPTO_MSG_BASE
] = 0,
397 [CRYPTO_MSG_GETSTAT
- CRYPTO_MSG_BASE
] = MSGSIZE(crypto_user_alg
),
400 static const struct nla_policy crypto_policy
[CRYPTOCFGA_MAX
+1] = {
401 [CRYPTOCFGA_PRIORITY_VAL
] = { .type
= NLA_U32
},
406 static const struct crypto_link
{
407 int (*doit
)(struct sk_buff
*, struct nlmsghdr
*, struct nlattr
**);
408 int (*dump
)(struct sk_buff
*, struct netlink_callback
*);
409 int (*done
)(struct netlink_callback
*);
410 } crypto_dispatch
[CRYPTO_NR_MSGTYPES
] = {
411 [CRYPTO_MSG_NEWALG
- CRYPTO_MSG_BASE
] = { .doit
= crypto_add_alg
},
412 [CRYPTO_MSG_DELALG
- CRYPTO_MSG_BASE
] = { .doit
= crypto_del_alg
},
413 [CRYPTO_MSG_UPDATEALG
- CRYPTO_MSG_BASE
] = { .doit
= crypto_update_alg
},
414 [CRYPTO_MSG_GETALG
- CRYPTO_MSG_BASE
] = { .doit
= crypto_report
,
415 .dump
= crypto_dump_report
,
416 .done
= crypto_dump_report_done
},
417 [CRYPTO_MSG_DELRNG
- CRYPTO_MSG_BASE
] = { .doit
= crypto_del_rng
},
418 [CRYPTO_MSG_GETSTAT
- CRYPTO_MSG_BASE
] = { .doit
= crypto_reportstat
},
421 static int crypto_user_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
422 struct netlink_ext_ack
*extack
)
424 struct net
*net
= sock_net(skb
->sk
);
425 struct nlattr
*attrs
[CRYPTOCFGA_MAX
+1];
426 const struct crypto_link
*link
;
429 type
= nlh
->nlmsg_type
;
430 if (type
> CRYPTO_MSG_MAX
)
433 type
-= CRYPTO_MSG_BASE
;
434 link
= &crypto_dispatch
[type
];
436 if ((type
== (CRYPTO_MSG_GETALG
- CRYPTO_MSG_BASE
) &&
437 (nlh
->nlmsg_flags
& NLM_F_DUMP
))) {
438 struct crypto_alg
*alg
;
439 unsigned long dump_alloc
= 0;
441 if (link
->dump
== NULL
)
444 down_read(&crypto_alg_sem
);
445 list_for_each_entry(alg
, &crypto_alg_list
, cra_list
)
446 dump_alloc
+= CRYPTO_REPORT_MAXSIZE
;
447 up_read(&crypto_alg_sem
);
450 struct netlink_dump_control c
= {
453 .min_dump_alloc
= min(dump_alloc
, 65535UL),
455 err
= netlink_dump_start(net
->crypto_nlsk
, skb
, nlh
, &c
);
461 err
= nlmsg_parse_deprecated(nlh
, crypto_msg_min
[type
], attrs
,
462 CRYPTOCFGA_MAX
, crypto_policy
, extack
);
466 if (link
->doit
== NULL
)
469 return link
->doit(skb
, nlh
, attrs
);
472 static void crypto_netlink_rcv(struct sk_buff
*skb
)
474 mutex_lock(&crypto_cfg_mutex
);
475 netlink_rcv_skb(skb
, &crypto_user_rcv_msg
);
476 mutex_unlock(&crypto_cfg_mutex
);
479 static int __net_init
crypto_netlink_init(struct net
*net
)
481 struct netlink_kernel_cfg cfg
= {
482 .input
= crypto_netlink_rcv
,
485 net
->crypto_nlsk
= netlink_kernel_create(net
, NETLINK_CRYPTO
, &cfg
);
486 return net
->crypto_nlsk
== NULL
? -ENOMEM
: 0;
489 static void __net_exit
crypto_netlink_exit(struct net
*net
)
491 netlink_kernel_release(net
->crypto_nlsk
);
492 net
->crypto_nlsk
= NULL
;
495 static struct pernet_operations crypto_netlink_net_ops
= {
496 .init
= crypto_netlink_init
,
497 .exit
= crypto_netlink_exit
,
500 static int __init
crypto_user_init(void)
502 return register_pernet_subsys(&crypto_netlink_net_ops
);
505 static void __exit
crypto_user_exit(void)
507 unregister_pernet_subsys(&crypto_netlink_net_ops
);
510 module_init(crypto_user_init
);
511 module_exit(crypto_user_exit
);
512 MODULE_LICENSE("GPL");
513 MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
514 MODULE_DESCRIPTION("Crypto userspace configuration API");
515 MODULE_ALIAS("net-pf-16-proto-21");