crypto_user.c 10 KB

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