crypto_user.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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 (!netlink_capable(skb, CAP_NET_ADMIN))
  213. return -EPERM;
  214. if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
  215. return -EINVAL;
  216. if (priority && !strlen(p->cru_driver_name))
  217. return -EINVAL;
  218. alg = crypto_alg_match(p, 1);
  219. if (!alg)
  220. return -ENOENT;
  221. down_write(&crypto_alg_sem);
  222. crypto_remove_spawns(alg, &list, NULL);
  223. if (priority)
  224. alg->cra_priority = nla_get_u32(priority);
  225. up_write(&crypto_alg_sem);
  226. crypto_remove_final(&list);
  227. return 0;
  228. }
  229. static int crypto_del_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
  230. struct nlattr **attrs)
  231. {
  232. struct crypto_alg *alg;
  233. struct crypto_user_alg *p = nlmsg_data(nlh);
  234. if (!netlink_capable(skb, CAP_NET_ADMIN))
  235. return -EPERM;
  236. if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
  237. return -EINVAL;
  238. alg = crypto_alg_match(p, 1);
  239. if (!alg)
  240. return -ENOENT;
  241. /* We can not unregister core algorithms such as aes-generic.
  242. * We would loose the reference in the crypto_alg_list to this algorithm
  243. * if we try to unregister. Unregistering such an algorithm without
  244. * removing the module is not possible, so we restrict to crypto
  245. * instances that are build from templates. */
  246. if (!(alg->cra_flags & CRYPTO_ALG_INSTANCE))
  247. return -EINVAL;
  248. if (atomic_read(&alg->cra_refcnt) != 1)
  249. return -EBUSY;
  250. return crypto_unregister_instance(alg);
  251. }
  252. static struct crypto_alg *crypto_user_skcipher_alg(const char *name, u32 type,
  253. u32 mask)
  254. {
  255. int err;
  256. struct crypto_alg *alg;
  257. type = crypto_skcipher_type(type);
  258. mask = crypto_skcipher_mask(mask);
  259. for (;;) {
  260. alg = crypto_lookup_skcipher(name, type, mask);
  261. if (!IS_ERR(alg))
  262. return alg;
  263. err = PTR_ERR(alg);
  264. if (err != -EAGAIN)
  265. break;
  266. if (signal_pending(current)) {
  267. err = -EINTR;
  268. break;
  269. }
  270. }
  271. return ERR_PTR(err);
  272. }
  273. static struct crypto_alg *crypto_user_aead_alg(const char *name, u32 type,
  274. u32 mask)
  275. {
  276. int err;
  277. struct crypto_alg *alg;
  278. type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
  279. type |= CRYPTO_ALG_TYPE_AEAD;
  280. mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
  281. mask |= CRYPTO_ALG_TYPE_MASK;
  282. for (;;) {
  283. alg = crypto_lookup_aead(name, type, mask);
  284. if (!IS_ERR(alg))
  285. return alg;
  286. err = PTR_ERR(alg);
  287. if (err != -EAGAIN)
  288. break;
  289. if (signal_pending(current)) {
  290. err = -EINTR;
  291. break;
  292. }
  293. }
  294. return ERR_PTR(err);
  295. }
  296. static int crypto_add_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
  297. struct nlattr **attrs)
  298. {
  299. int exact = 0;
  300. const char *name;
  301. struct crypto_alg *alg;
  302. struct crypto_user_alg *p = nlmsg_data(nlh);
  303. struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
  304. if (!netlink_capable(skb, CAP_NET_ADMIN))
  305. return -EPERM;
  306. if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
  307. return -EINVAL;
  308. if (strlen(p->cru_driver_name))
  309. exact = 1;
  310. if (priority && !exact)
  311. return -EINVAL;
  312. alg = crypto_alg_match(p, exact);
  313. if (alg)
  314. return -EEXIST;
  315. if (strlen(p->cru_driver_name))
  316. name = p->cru_driver_name;
  317. else
  318. name = p->cru_name;
  319. switch (p->cru_type & p->cru_mask & CRYPTO_ALG_TYPE_MASK) {
  320. case CRYPTO_ALG_TYPE_AEAD:
  321. alg = crypto_user_aead_alg(name, p->cru_type, p->cru_mask);
  322. break;
  323. case CRYPTO_ALG_TYPE_GIVCIPHER:
  324. case CRYPTO_ALG_TYPE_BLKCIPHER:
  325. case CRYPTO_ALG_TYPE_ABLKCIPHER:
  326. alg = crypto_user_skcipher_alg(name, p->cru_type, p->cru_mask);
  327. break;
  328. default:
  329. alg = crypto_alg_mod_lookup(name, p->cru_type, p->cru_mask);
  330. }
  331. if (IS_ERR(alg))
  332. return PTR_ERR(alg);
  333. down_write(&crypto_alg_sem);
  334. if (priority)
  335. alg->cra_priority = nla_get_u32(priority);
  336. up_write(&crypto_alg_sem);
  337. crypto_mod_put(alg);
  338. return 0;
  339. }
  340. #define MSGSIZE(type) sizeof(struct type)
  341. static const int crypto_msg_min[CRYPTO_NR_MSGTYPES] = {
  342. [CRYPTO_MSG_NEWALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
  343. [CRYPTO_MSG_DELALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
  344. [CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
  345. [CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
  346. };
  347. static const struct nla_policy crypto_policy[CRYPTOCFGA_MAX+1] = {
  348. [CRYPTOCFGA_PRIORITY_VAL] = { .type = NLA_U32},
  349. };
  350. #undef MSGSIZE
  351. static const struct crypto_link {
  352. int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
  353. int (*dump)(struct sk_buff *, struct netlink_callback *);
  354. int (*done)(struct netlink_callback *);
  355. } crypto_dispatch[CRYPTO_NR_MSGTYPES] = {
  356. [CRYPTO_MSG_NEWALG - CRYPTO_MSG_BASE] = { .doit = crypto_add_alg},
  357. [CRYPTO_MSG_DELALG - CRYPTO_MSG_BASE] = { .doit = crypto_del_alg},
  358. [CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = { .doit = crypto_update_alg},
  359. [CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = { .doit = crypto_report,
  360. .dump = crypto_dump_report,
  361. .done = crypto_dump_report_done},
  362. };
  363. static int crypto_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  364. {
  365. struct nlattr *attrs[CRYPTOCFGA_MAX+1];
  366. const struct crypto_link *link;
  367. int type, err;
  368. type = nlh->nlmsg_type;
  369. if (type > CRYPTO_MSG_MAX)
  370. return -EINVAL;
  371. type -= CRYPTO_MSG_BASE;
  372. link = &crypto_dispatch[type];
  373. if ((type == (CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE) &&
  374. (nlh->nlmsg_flags & NLM_F_DUMP))) {
  375. struct crypto_alg *alg;
  376. u16 dump_alloc = 0;
  377. if (link->dump == NULL)
  378. return -EINVAL;
  379. list_for_each_entry(alg, &crypto_alg_list, cra_list)
  380. dump_alloc += CRYPTO_REPORT_MAXSIZE;
  381. {
  382. struct netlink_dump_control c = {
  383. .dump = link->dump,
  384. .done = link->done,
  385. .min_dump_alloc = dump_alloc,
  386. };
  387. return netlink_dump_start(crypto_nlsk, skb, nlh, &c);
  388. }
  389. }
  390. err = nlmsg_parse(nlh, crypto_msg_min[type], attrs, CRYPTOCFGA_MAX,
  391. crypto_policy);
  392. if (err < 0)
  393. return err;
  394. if (link->doit == NULL)
  395. return -EINVAL;
  396. return link->doit(skb, nlh, attrs);
  397. }
  398. static void crypto_netlink_rcv(struct sk_buff *skb)
  399. {
  400. mutex_lock(&crypto_cfg_mutex);
  401. netlink_rcv_skb(skb, &crypto_user_rcv_msg);
  402. mutex_unlock(&crypto_cfg_mutex);
  403. }
  404. static int __init crypto_user_init(void)
  405. {
  406. struct netlink_kernel_cfg cfg = {
  407. .input = crypto_netlink_rcv,
  408. };
  409. crypto_nlsk = netlink_kernel_create(&init_net, NETLINK_CRYPTO, &cfg);
  410. if (!crypto_nlsk)
  411. return -ENOMEM;
  412. return 0;
  413. }
  414. static void __exit crypto_user_exit(void)
  415. {
  416. netlink_kernel_release(crypto_nlsk);
  417. }
  418. module_init(crypto_user_init);
  419. module_exit(crypto_user_exit);
  420. MODULE_LICENSE("GPL");
  421. MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
  422. MODULE_DESCRIPTION("Crypto userspace configuration API");