crypto_user.c 12 KB

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