crypto_user.c 12 KB

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