crypto_user.c 13 KB

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