crypto_user_base.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Crypto user configuration API.
  4. *
  5. * Copyright (C) 2011 secunet Security Networks AG
  6. * Copyright (C) 2011 Steffen Klassert <steffen.klassert@secunet.com>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/crypto.h>
  10. #include <linux/cryptouser.h>
  11. #include <linux/sched.h>
  12. #include <linux/security.h>
  13. #include <net/netlink.h>
  14. #include <net/net_namespace.h>
  15. #include <net/sock.h>
  16. #include <crypto/internal/skcipher.h>
  17. #include <crypto/internal/rng.h>
  18. #include <crypto/akcipher.h>
  19. #include <crypto/kpp.h>
  20. #include <crypto/internal/cryptouser.h>
  21. #include "internal.h"
  22. #define null_terminated(x) (strnlen(x, sizeof(x)) < sizeof(x))
  23. static DEFINE_MUTEX(crypto_cfg_mutex);
  24. struct crypto_dump_info {
  25. struct sk_buff *in_skb;
  26. struct sk_buff *out_skb;
  27. u32 nlmsg_seq;
  28. u16 nlmsg_flags;
  29. };
  30. struct crypto_alg *crypto_alg_match(struct crypto_user_alg *p, int exact)
  31. {
  32. struct crypto_alg *q, *alg = NULL;
  33. down_read(&crypto_alg_sem);
  34. list_for_each_entry(q, &crypto_alg_list, cra_list) {
  35. int match = 0;
  36. if (crypto_is_larval(q))
  37. continue;
  38. if ((q->cra_flags ^ p->cru_type) & p->cru_mask)
  39. continue;
  40. if (strlen(p->cru_driver_name))
  41. match = !strcmp(q->cra_driver_name,
  42. p->cru_driver_name);
  43. else if (!exact)
  44. match = !strcmp(q->cra_name, p->cru_name);
  45. if (!match)
  46. continue;
  47. if (unlikely(!crypto_mod_get(q)))
  48. continue;
  49. alg = q;
  50. break;
  51. }
  52. up_read(&crypto_alg_sem);
  53. return alg;
  54. }
  55. static int crypto_report_cipher(struct sk_buff *skb, struct crypto_alg *alg)
  56. {
  57. struct crypto_report_cipher rcipher;
  58. memset(&rcipher, 0, sizeof(rcipher));
  59. strscpy(rcipher.type, "cipher", sizeof(rcipher.type));
  60. rcipher.blocksize = alg->cra_blocksize;
  61. rcipher.min_keysize = alg->cra_cipher.cia_min_keysize;
  62. rcipher.max_keysize = alg->cra_cipher.cia_max_keysize;
  63. return nla_put(skb, CRYPTOCFGA_REPORT_CIPHER,
  64. sizeof(rcipher), &rcipher);
  65. }
  66. static int crypto_report_comp(struct sk_buff *skb, struct crypto_alg *alg)
  67. {
  68. struct crypto_report_comp rcomp;
  69. memset(&rcomp, 0, sizeof(rcomp));
  70. strscpy(rcomp.type, "compression", sizeof(rcomp.type));
  71. return nla_put(skb, CRYPTOCFGA_REPORT_COMPRESS, sizeof(rcomp), &rcomp);
  72. }
  73. static int crypto_report_one(struct crypto_alg *alg,
  74. struct crypto_user_alg *ualg, struct sk_buff *skb)
  75. {
  76. memset(ualg, 0, sizeof(*ualg));
  77. strscpy(ualg->cru_name, alg->cra_name, sizeof(ualg->cru_name));
  78. strscpy(ualg->cru_driver_name, alg->cra_driver_name,
  79. sizeof(ualg->cru_driver_name));
  80. strscpy(ualg->cru_module_name, module_name(alg->cra_module),
  81. sizeof(ualg->cru_module_name));
  82. ualg->cru_type = 0;
  83. ualg->cru_mask = 0;
  84. ualg->cru_flags = alg->cra_flags;
  85. ualg->cru_refcnt = refcount_read(&alg->cra_refcnt);
  86. if (nla_put_u32(skb, CRYPTOCFGA_PRIORITY_VAL, alg->cra_priority))
  87. goto nla_put_failure;
  88. if (alg->cra_flags & CRYPTO_ALG_LARVAL) {
  89. struct crypto_report_larval rl;
  90. memset(&rl, 0, sizeof(rl));
  91. strscpy(rl.type, "larval", sizeof(rl.type));
  92. if (nla_put(skb, CRYPTOCFGA_REPORT_LARVAL, sizeof(rl), &rl))
  93. goto nla_put_failure;
  94. goto out;
  95. }
  96. if (alg->cra_type && alg->cra_type->report) {
  97. if (alg->cra_type->report(skb, alg))
  98. goto nla_put_failure;
  99. goto out;
  100. }
  101. switch (alg->cra_flags & (CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_LARVAL)) {
  102. case CRYPTO_ALG_TYPE_CIPHER:
  103. if (crypto_report_cipher(skb, alg))
  104. goto nla_put_failure;
  105. break;
  106. case CRYPTO_ALG_TYPE_COMPRESS:
  107. if (crypto_report_comp(skb, alg))
  108. goto nla_put_failure;
  109. break;
  110. }
  111. out:
  112. return 0;
  113. nla_put_failure:
  114. return -EMSGSIZE;
  115. }
  116. static int crypto_report_alg(struct crypto_alg *alg,
  117. struct crypto_dump_info *info)
  118. {
  119. struct sk_buff *in_skb = info->in_skb;
  120. struct sk_buff *skb = info->out_skb;
  121. struct nlmsghdr *nlh;
  122. struct crypto_user_alg *ualg;
  123. int err = 0;
  124. nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, info->nlmsg_seq,
  125. CRYPTO_MSG_GETALG, sizeof(*ualg), info->nlmsg_flags);
  126. if (!nlh) {
  127. err = -EMSGSIZE;
  128. goto out;
  129. }
  130. ualg = nlmsg_data(nlh);
  131. err = crypto_report_one(alg, ualg, skb);
  132. if (err) {
  133. nlmsg_cancel(skb, nlh);
  134. goto out;
  135. }
  136. nlmsg_end(skb, nlh);
  137. out:
  138. return err;
  139. }
  140. static int crypto_report(struct sk_buff *in_skb, struct nlmsghdr *in_nlh,
  141. struct nlattr **attrs)
  142. {
  143. struct net *net = sock_net(in_skb->sk);
  144. struct crypto_user_alg *p = nlmsg_data(in_nlh);
  145. struct crypto_alg *alg;
  146. struct sk_buff *skb;
  147. struct crypto_dump_info info;
  148. int err;
  149. if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
  150. return -EINVAL;
  151. alg = crypto_alg_match(p, 0);
  152. if (!alg)
  153. return -ENOENT;
  154. err = -ENOMEM;
  155. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  156. if (!skb)
  157. goto drop_alg;
  158. info.in_skb = in_skb;
  159. info.out_skb = skb;
  160. info.nlmsg_seq = in_nlh->nlmsg_seq;
  161. info.nlmsg_flags = 0;
  162. err = crypto_report_alg(alg, &info);
  163. drop_alg:
  164. crypto_mod_put(alg);
  165. if (err) {
  166. kfree_skb(skb);
  167. return err;
  168. }
  169. return nlmsg_unicast(net->crypto_nlsk, skb, NETLINK_CB(in_skb).portid);
  170. }
  171. static int crypto_dump_report(struct sk_buff *skb, struct netlink_callback *cb)
  172. {
  173. const size_t start_pos = cb->args[0];
  174. size_t pos = 0;
  175. struct crypto_dump_info info;
  176. struct crypto_alg *alg;
  177. int res;
  178. info.in_skb = cb->skb;
  179. info.out_skb = skb;
  180. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  181. info.nlmsg_flags = NLM_F_MULTI;
  182. down_read(&crypto_alg_sem);
  183. list_for_each_entry(alg, &crypto_alg_list, cra_list) {
  184. if (pos >= start_pos) {
  185. res = crypto_report_alg(alg, &info);
  186. if (res == -EMSGSIZE)
  187. break;
  188. if (res)
  189. goto out;
  190. }
  191. pos++;
  192. }
  193. cb->args[0] = pos;
  194. res = skb->len;
  195. out:
  196. up_read(&crypto_alg_sem);
  197. return res;
  198. }
  199. static int crypto_dump_report_done(struct netlink_callback *cb)
  200. {
  201. return 0;
  202. }
  203. static int crypto_update_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
  204. struct nlattr **attrs)
  205. {
  206. struct crypto_alg *alg;
  207. struct crypto_user_alg *p = nlmsg_data(nlh);
  208. struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
  209. LIST_HEAD(list);
  210. if (!netlink_capable(skb, CAP_NET_ADMIN))
  211. return -EPERM;
  212. if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
  213. return -EINVAL;
  214. if (priority && !strlen(p->cru_driver_name))
  215. return -EINVAL;
  216. alg = crypto_alg_match(p, 1);
  217. if (!alg)
  218. return -ENOENT;
  219. down_write(&crypto_alg_sem);
  220. crypto_remove_spawns(alg, &list, NULL);
  221. if (priority)
  222. alg->cra_priority = nla_get_u32(priority);
  223. up_write(&crypto_alg_sem);
  224. crypto_mod_put(alg);
  225. crypto_remove_final(&list);
  226. return 0;
  227. }
  228. static int crypto_del_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
  229. struct nlattr **attrs)
  230. {
  231. struct crypto_alg *alg;
  232. struct crypto_user_alg *p = nlmsg_data(nlh);
  233. int err;
  234. if (!netlink_capable(skb, CAP_NET_ADMIN))
  235. return -EPERM;
  236. if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
  237. return -EINVAL;
  238. alg = crypto_alg_match(p, 1);
  239. if (!alg)
  240. return -ENOENT;
  241. /* We can not unregister core algorithms such as aes-generic.
  242. * We would loose the reference in the crypto_alg_list to this algorithm
  243. * if we try to unregister. Unregistering such an algorithm without
  244. * removing the module is not possible, so we restrict to crypto
  245. * instances that are build from templates. */
  246. err = -EINVAL;
  247. if (!(alg->cra_flags & CRYPTO_ALG_INSTANCE))
  248. goto drop_alg;
  249. err = -EBUSY;
  250. if (refcount_read(&alg->cra_refcnt) > 2)
  251. goto drop_alg;
  252. crypto_unregister_instance((struct crypto_instance *)alg);
  253. err = 0;
  254. drop_alg:
  255. crypto_mod_put(alg);
  256. return err;
  257. }
  258. static int crypto_add_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
  259. struct nlattr **attrs)
  260. {
  261. int exact = 0;
  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. if (!netlink_capable(skb, CAP_NET_ADMIN))
  267. return -EPERM;
  268. if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
  269. return -EINVAL;
  270. if (strlen(p->cru_driver_name))
  271. exact = 1;
  272. if (priority && !exact)
  273. return -EINVAL;
  274. alg = crypto_alg_match(p, exact);
  275. if (alg) {
  276. crypto_mod_put(alg);
  277. return -EEXIST;
  278. }
  279. if (strlen(p->cru_driver_name))
  280. name = p->cru_driver_name;
  281. else
  282. name = p->cru_name;
  283. alg = crypto_alg_mod_lookup(name, p->cru_type, p->cru_mask);
  284. if (IS_ERR(alg))
  285. return PTR_ERR(alg);
  286. down_write(&crypto_alg_sem);
  287. if (priority)
  288. alg->cra_priority = nla_get_u32(priority);
  289. up_write(&crypto_alg_sem);
  290. crypto_mod_put(alg);
  291. return 0;
  292. }
  293. static int crypto_del_rng(struct sk_buff *skb, struct nlmsghdr *nlh,
  294. struct nlattr **attrs)
  295. {
  296. if (!netlink_capable(skb, CAP_NET_ADMIN))
  297. return -EPERM;
  298. return crypto_del_default_rng();
  299. }
  300. #define MSGSIZE(type) sizeof(struct type)
  301. static const int crypto_msg_min[CRYPTO_NR_MSGTYPES] = {
  302. [CRYPTO_MSG_NEWALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
  303. [CRYPTO_MSG_DELALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
  304. [CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
  305. [CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
  306. [CRYPTO_MSG_DELRNG - CRYPTO_MSG_BASE] = 0,
  307. [CRYPTO_MSG_GETSTAT - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
  308. };
  309. static const struct nla_policy crypto_policy[CRYPTOCFGA_MAX+1] = {
  310. [CRYPTOCFGA_PRIORITY_VAL] = { .type = NLA_U32},
  311. };
  312. #undef MSGSIZE
  313. static const 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. [CRYPTO_MSG_DELRNG - CRYPTO_MSG_BASE] = { .doit = crypto_del_rng },
  325. [CRYPTO_MSG_GETSTAT - CRYPTO_MSG_BASE] = { .doit = crypto_reportstat},
  326. };
  327. static int crypto_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
  328. struct netlink_ext_ack *extack)
  329. {
  330. struct net *net = sock_net(skb->sk);
  331. struct nlattr *attrs[CRYPTOCFGA_MAX+1];
  332. const struct crypto_link *link;
  333. int type, err;
  334. type = nlh->nlmsg_type;
  335. if (type > CRYPTO_MSG_MAX)
  336. return -EINVAL;
  337. type -= CRYPTO_MSG_BASE;
  338. link = &crypto_dispatch[type];
  339. if ((type == (CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE) &&
  340. (nlh->nlmsg_flags & NLM_F_DUMP))) {
  341. struct crypto_alg *alg;
  342. unsigned long dump_alloc = 0;
  343. if (link->dump == NULL)
  344. return -EINVAL;
  345. down_read(&crypto_alg_sem);
  346. list_for_each_entry(alg, &crypto_alg_list, cra_list)
  347. dump_alloc += CRYPTO_REPORT_MAXSIZE;
  348. up_read(&crypto_alg_sem);
  349. {
  350. struct netlink_dump_control c = {
  351. .dump = link->dump,
  352. .done = link->done,
  353. .min_dump_alloc = min(dump_alloc, 65535UL),
  354. };
  355. err = netlink_dump_start(net->crypto_nlsk, skb, nlh, &c);
  356. }
  357. return err;
  358. }
  359. err = nlmsg_parse_deprecated(nlh, crypto_msg_min[type], attrs,
  360. CRYPTOCFGA_MAX, crypto_policy, extack);
  361. if (err < 0)
  362. return err;
  363. if (link->doit == NULL)
  364. return -EINVAL;
  365. return link->doit(skb, nlh, attrs);
  366. }
  367. static void crypto_netlink_rcv(struct sk_buff *skb)
  368. {
  369. mutex_lock(&crypto_cfg_mutex);
  370. netlink_rcv_skb(skb, &crypto_user_rcv_msg);
  371. mutex_unlock(&crypto_cfg_mutex);
  372. }
  373. static int __net_init crypto_netlink_init(struct net *net)
  374. {
  375. struct netlink_kernel_cfg cfg = {
  376. .input = crypto_netlink_rcv,
  377. };
  378. net->crypto_nlsk = netlink_kernel_create(net, NETLINK_CRYPTO, &cfg);
  379. return net->crypto_nlsk == NULL ? -ENOMEM : 0;
  380. }
  381. static void __net_exit crypto_netlink_exit(struct net *net)
  382. {
  383. netlink_kernel_release(net->crypto_nlsk);
  384. net->crypto_nlsk = NULL;
  385. }
  386. static struct pernet_operations crypto_netlink_net_ops = {
  387. .init = crypto_netlink_init,
  388. .exit = crypto_netlink_exit,
  389. };
  390. static int __init crypto_user_init(void)
  391. {
  392. return register_pernet_subsys(&crypto_netlink_net_ops);
  393. }
  394. static void __exit crypto_user_exit(void)
  395. {
  396. unregister_pernet_subsys(&crypto_netlink_net_ops);
  397. }
  398. module_init(crypto_user_init);
  399. module_exit(crypto_user_exit);
  400. MODULE_LICENSE("GPL");
  401. MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
  402. MODULE_DESCRIPTION("Crypto userspace configuration API");
  403. MODULE_ALIAS("net-pf-16-proto-21");