crypto_user_base.c 13 KB

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