crypto_user.c 12 KB

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