crypto_user.c 12 KB

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