crypto_user.c 13 KB

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