crypto_user.c 13 KB

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