crypto_user.c 13 KB

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