crypto_user.c 12 KB

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