crypto_user.c 13 KB

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