crypto_user.c 10 KB

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