crypto_user.c 12 KB

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