crypto_user.c 12 KB

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