crypto_user.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /*
  2. * Crypto user configuration API.
  3. *
  4. * Copyright (C) 2011 secunet Security Networks AG
  5. * Copyright (C) 2011 Steffen Klassert <[email protected]>
  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 = atomic_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_ATOMIC);
  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. return err;
  229. return nlmsg_unicast(crypto_nlsk, skb, NETLINK_CB(in_skb).portid);
  230. }
  231. static int crypto_dump_report(struct sk_buff *skb, struct netlink_callback *cb)
  232. {
  233. struct crypto_alg *alg;
  234. struct crypto_dump_info info;
  235. int err;
  236. if (cb->args[0])
  237. goto out;
  238. cb->args[0] = 1;
  239. info.in_skb = cb->skb;
  240. info.out_skb = skb;
  241. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  242. info.nlmsg_flags = NLM_F_MULTI;
  243. list_for_each_entry(alg, &crypto_alg_list, cra_list) {
  244. err = crypto_report_alg(alg, &info);
  245. if (err)
  246. goto out_err;
  247. }
  248. out:
  249. return skb->len;
  250. out_err:
  251. return err;
  252. }
  253. static int crypto_dump_report_done(struct netlink_callback *cb)
  254. {
  255. return 0;
  256. }
  257. static int crypto_update_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
  258. struct nlattr **attrs)
  259. {
  260. struct crypto_alg *alg;
  261. struct crypto_user_alg *p = nlmsg_data(nlh);
  262. struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
  263. LIST_HEAD(list);
  264. if (!netlink_capable(skb, CAP_NET_ADMIN))
  265. return -EPERM;
  266. if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
  267. return -EINVAL;
  268. if (priority && !strlen(p->cru_driver_name))
  269. return -EINVAL;
  270. alg = crypto_alg_match(p, 1);
  271. if (!alg)
  272. return -ENOENT;
  273. down_write(&crypto_alg_sem);
  274. crypto_remove_spawns(alg, &list, NULL);
  275. if (priority)
  276. alg->cra_priority = nla_get_u32(priority);
  277. up_write(&crypto_alg_sem);
  278. crypto_mod_put(alg);
  279. crypto_remove_final(&list);
  280. return 0;
  281. }
  282. static int crypto_del_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
  283. struct nlattr **attrs)
  284. {
  285. struct crypto_alg *alg;
  286. struct crypto_user_alg *p = nlmsg_data(nlh);
  287. int err;
  288. if (!netlink_capable(skb, CAP_NET_ADMIN))
  289. return -EPERM;
  290. if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
  291. return -EINVAL;
  292. alg = crypto_alg_match(p, 1);
  293. if (!alg)
  294. return -ENOENT;
  295. /* We can not unregister core algorithms such as aes-generic.
  296. * We would loose the reference in the crypto_alg_list to this algorithm
  297. * if we try to unregister. Unregistering such an algorithm without
  298. * removing the module is not possible, so we restrict to crypto
  299. * instances that are build from templates. */
  300. err = -EINVAL;
  301. if (!(alg->cra_flags & CRYPTO_ALG_INSTANCE))
  302. goto drop_alg;
  303. err = -EBUSY;
  304. if (atomic_read(&alg->cra_refcnt) > 2)
  305. goto drop_alg;
  306. err = crypto_unregister_instance((struct crypto_instance *)alg);
  307. drop_alg:
  308. crypto_mod_put(alg);
  309. return err;
  310. }
  311. static int crypto_add_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
  312. struct nlattr **attrs)
  313. {
  314. int exact = 0;
  315. const char *name;
  316. struct crypto_alg *alg;
  317. struct crypto_user_alg *p = nlmsg_data(nlh);
  318. struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
  319. if (!netlink_capable(skb, CAP_NET_ADMIN))
  320. return -EPERM;
  321. if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
  322. return -EINVAL;
  323. if (strlen(p->cru_driver_name))
  324. exact = 1;
  325. if (priority && !exact)
  326. return -EINVAL;
  327. alg = crypto_alg_match(p, exact);
  328. if (alg) {
  329. crypto_mod_put(alg);
  330. return -EEXIST;
  331. }
  332. if (strlen(p->cru_driver_name))
  333. name = p->cru_driver_name;
  334. else
  335. name = p->cru_name;
  336. alg = crypto_alg_mod_lookup(name, p->cru_type, p->cru_mask);
  337. if (IS_ERR(alg))
  338. return PTR_ERR(alg);
  339. down_write(&crypto_alg_sem);
  340. if (priority)
  341. alg->cra_priority = nla_get_u32(priority);
  342. up_write(&crypto_alg_sem);
  343. crypto_mod_put(alg);
  344. return 0;
  345. }
  346. static int crypto_del_rng(struct sk_buff *skb, struct nlmsghdr *nlh,
  347. struct nlattr **attrs)
  348. {
  349. if (!netlink_capable(skb, CAP_NET_ADMIN))
  350. return -EPERM;
  351. return crypto_del_default_rng();
  352. }
  353. #define MSGSIZE(type) sizeof(struct type)
  354. static const int crypto_msg_min[CRYPTO_NR_MSGTYPES] = {
  355. [CRYPTO_MSG_NEWALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
  356. [CRYPTO_MSG_DELALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
  357. [CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
  358. [CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
  359. [CRYPTO_MSG_DELRNG - CRYPTO_MSG_BASE] = 0,
  360. };
  361. static const struct nla_policy crypto_policy[CRYPTOCFGA_MAX+1] = {
  362. [CRYPTOCFGA_PRIORITY_VAL] = { .type = NLA_U32},
  363. };
  364. #undef MSGSIZE
  365. static const struct crypto_link {
  366. int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
  367. int (*dump)(struct sk_buff *, struct netlink_callback *);
  368. int (*done)(struct netlink_callback *);
  369. } crypto_dispatch[CRYPTO_NR_MSGTYPES] = {
  370. [CRYPTO_MSG_NEWALG - CRYPTO_MSG_BASE] = { .doit = crypto_add_alg},
  371. [CRYPTO_MSG_DELALG - CRYPTO_MSG_BASE] = { .doit = crypto_del_alg},
  372. [CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = { .doit = crypto_update_alg},
  373. [CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = { .doit = crypto_report,
  374. .dump = crypto_dump_report,
  375. .done = crypto_dump_report_done},
  376. [CRYPTO_MSG_DELRNG - CRYPTO_MSG_BASE] = { .doit = crypto_del_rng },
  377. };
  378. static int crypto_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  379. {
  380. struct nlattr *attrs[CRYPTOCFGA_MAX+1];
  381. const struct crypto_link *link;
  382. int type, err;
  383. type = nlh->nlmsg_type;
  384. if (type > CRYPTO_MSG_MAX)
  385. return -EINVAL;
  386. type -= CRYPTO_MSG_BASE;
  387. link = &crypto_dispatch[type];
  388. if ((type == (CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE) &&
  389. (nlh->nlmsg_flags & NLM_F_DUMP))) {
  390. struct crypto_alg *alg;
  391. u16 dump_alloc = 0;
  392. if (link->dump == NULL)
  393. return -EINVAL;
  394. down_read(&crypto_alg_sem);
  395. list_for_each_entry(alg, &crypto_alg_list, cra_list)
  396. dump_alloc += CRYPTO_REPORT_MAXSIZE;
  397. {
  398. struct netlink_dump_control c = {
  399. .dump = link->dump,
  400. .done = link->done,
  401. .min_dump_alloc = dump_alloc,
  402. };
  403. err = netlink_dump_start(crypto_nlsk, skb, nlh, &c);
  404. }
  405. up_read(&crypto_alg_sem);
  406. return err;
  407. }
  408. err = nlmsg_parse(nlh, crypto_msg_min[type], attrs, CRYPTOCFGA_MAX,
  409. crypto_policy);
  410. if (err < 0)
  411. return err;
  412. if (link->doit == NULL)
  413. return -EINVAL;
  414. return link->doit(skb, nlh, attrs);
  415. }
  416. static void crypto_netlink_rcv(struct sk_buff *skb)
  417. {
  418. mutex_lock(&crypto_cfg_mutex);
  419. netlink_rcv_skb(skb, &crypto_user_rcv_msg);
  420. mutex_unlock(&crypto_cfg_mutex);
  421. }
  422. static int __init crypto_user_init(void)
  423. {
  424. struct netlink_kernel_cfg cfg = {
  425. .input = crypto_netlink_rcv,
  426. };
  427. crypto_nlsk = netlink_kernel_create(&init_net, NETLINK_CRYPTO, &cfg);
  428. if (!crypto_nlsk)
  429. return -ENOMEM;
  430. return 0;
  431. }
  432. static void __exit crypto_user_exit(void)
  433. {
  434. netlink_kernel_release(crypto_nlsk);
  435. }
  436. module_init(crypto_user_init);
  437. module_exit(crypto_user_exit);
  438. MODULE_LICENSE("GPL");
  439. MODULE_AUTHOR("Steffen Klassert <[email protected]>");
  440. MODULE_DESCRIPTION("Crypto userspace configuration API");
  441. MODULE_ALIAS("net-pf-16-proto-21");