crypto_user.c 13 KB

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