algboss.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Create default crypto algorithm instances.
  4. *
  5. * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
  6. */
  7. #include <crypto/internal/aead.h>
  8. #include <linux/completion.h>
  9. #include <linux/ctype.h>
  10. #include <linux/err.h>
  11. #include <linux/init.h>
  12. #include <linux/kthread.h>
  13. #include <linux/module.h>
  14. #include <linux/notifier.h>
  15. #include <linux/rtnetlink.h>
  16. #include <linux/sched/signal.h>
  17. #include <linux/slab.h>
  18. #include <linux/string.h>
  19. #include "internal.h"
  20. struct cryptomgr_param {
  21. struct rtattr *tb[CRYPTO_MAX_ATTRS + 2];
  22. struct {
  23. struct rtattr attr;
  24. struct crypto_attr_type data;
  25. } type;
  26. union {
  27. struct rtattr attr;
  28. struct {
  29. struct rtattr attr;
  30. struct crypto_attr_alg data;
  31. } alg;
  32. struct {
  33. struct rtattr attr;
  34. struct crypto_attr_u32 data;
  35. } nu32;
  36. } attrs[CRYPTO_MAX_ATTRS];
  37. char template[CRYPTO_MAX_ALG_NAME];
  38. struct crypto_larval *larval;
  39. u32 otype;
  40. u32 omask;
  41. };
  42. struct crypto_test_param {
  43. char driver[CRYPTO_MAX_ALG_NAME];
  44. char alg[CRYPTO_MAX_ALG_NAME];
  45. u32 type;
  46. };
  47. static int cryptomgr_probe(void *data)
  48. {
  49. struct cryptomgr_param *param = data;
  50. struct crypto_template *tmpl;
  51. int err;
  52. tmpl = crypto_lookup_template(param->template);
  53. if (!tmpl)
  54. goto out;
  55. do {
  56. err = tmpl->create(tmpl, param->tb);
  57. } while (err == -EAGAIN && !signal_pending(current));
  58. crypto_tmpl_put(tmpl);
  59. out:
  60. complete_all(&param->larval->completion);
  61. crypto_alg_put(&param->larval->alg);
  62. kfree(param);
  63. module_put_and_exit(0);
  64. }
  65. static int cryptomgr_schedule_probe(struct crypto_larval *larval)
  66. {
  67. struct task_struct *thread;
  68. struct cryptomgr_param *param;
  69. const char *name = larval->alg.cra_name;
  70. const char *p;
  71. unsigned int len;
  72. int i;
  73. if (!try_module_get(THIS_MODULE))
  74. goto err;
  75. param = kzalloc(sizeof(*param), GFP_KERNEL);
  76. if (!param)
  77. goto err_put_module;
  78. for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
  79. ;
  80. len = p - name;
  81. if (!len || *p != '(')
  82. goto err_free_param;
  83. memcpy(param->template, name, len);
  84. i = 0;
  85. for (;;) {
  86. int notnum = 0;
  87. name = ++p;
  88. for (; isalnum(*p) || *p == '-' || *p == '_'; p++)
  89. notnum |= !isdigit(*p);
  90. if (*p == '(') {
  91. int recursion = 0;
  92. for (;;) {
  93. if (!*++p)
  94. goto err_free_param;
  95. if (*p == '(')
  96. recursion++;
  97. else if (*p == ')' && !recursion--)
  98. break;
  99. }
  100. notnum = 1;
  101. p++;
  102. }
  103. len = p - name;
  104. if (!len)
  105. goto err_free_param;
  106. if (notnum) {
  107. param->attrs[i].alg.attr.rta_len =
  108. sizeof(param->attrs[i].alg);
  109. param->attrs[i].alg.attr.rta_type = CRYPTOA_ALG;
  110. memcpy(param->attrs[i].alg.data.name, name, len);
  111. } else {
  112. param->attrs[i].nu32.attr.rta_len =
  113. sizeof(param->attrs[i].nu32);
  114. param->attrs[i].nu32.attr.rta_type = CRYPTOA_U32;
  115. param->attrs[i].nu32.data.num =
  116. simple_strtol(name, NULL, 0);
  117. }
  118. param->tb[i + 1] = &param->attrs[i].attr;
  119. i++;
  120. if (i >= CRYPTO_MAX_ATTRS)
  121. goto err_free_param;
  122. if (*p == ')')
  123. break;
  124. if (*p != ',')
  125. goto err_free_param;
  126. }
  127. if (!i)
  128. goto err_free_param;
  129. param->tb[i + 1] = NULL;
  130. param->type.attr.rta_len = sizeof(param->type);
  131. param->type.attr.rta_type = CRYPTOA_TYPE;
  132. param->type.data.type = larval->alg.cra_flags & ~CRYPTO_ALG_TESTED;
  133. param->type.data.mask = larval->mask & ~CRYPTO_ALG_TESTED;
  134. param->tb[0] = &param->type.attr;
  135. param->otype = larval->alg.cra_flags;
  136. param->omask = larval->mask;
  137. crypto_alg_get(&larval->alg);
  138. param->larval = larval;
  139. thread = kthread_run(cryptomgr_probe, param, "cryptomgr_probe");
  140. if (IS_ERR(thread))
  141. goto err_put_larval;
  142. return NOTIFY_STOP;
  143. err_put_larval:
  144. crypto_alg_put(&larval->alg);
  145. err_free_param:
  146. kfree(param);
  147. err_put_module:
  148. module_put(THIS_MODULE);
  149. err:
  150. return NOTIFY_OK;
  151. }
  152. static int cryptomgr_test(void *data)
  153. {
  154. struct crypto_test_param *param = data;
  155. u32 type = param->type;
  156. int err = 0;
  157. #ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
  158. goto skiptest;
  159. #endif
  160. if (type & CRYPTO_ALG_TESTED)
  161. goto skiptest;
  162. err = alg_test(param->driver, param->alg, type, CRYPTO_ALG_TESTED);
  163. skiptest:
  164. crypto_alg_tested(param->driver, err);
  165. kfree(param);
  166. module_put_and_exit(0);
  167. }
  168. static int cryptomgr_schedule_test(struct crypto_alg *alg)
  169. {
  170. struct task_struct *thread;
  171. struct crypto_test_param *param;
  172. u32 type;
  173. if (!try_module_get(THIS_MODULE))
  174. goto err;
  175. param = kzalloc(sizeof(*param), GFP_KERNEL);
  176. if (!param)
  177. goto err_put_module;
  178. memcpy(param->driver, alg->cra_driver_name, sizeof(param->driver));
  179. memcpy(param->alg, alg->cra_name, sizeof(param->alg));
  180. type = alg->cra_flags;
  181. /* Do not test internal algorithms. */
  182. if (type & CRYPTO_ALG_INTERNAL)
  183. type |= CRYPTO_ALG_TESTED;
  184. param->type = type;
  185. thread = kthread_run(cryptomgr_test, param, "cryptomgr_test");
  186. if (IS_ERR(thread))
  187. goto err_free_param;
  188. return NOTIFY_STOP;
  189. err_free_param:
  190. kfree(param);
  191. err_put_module:
  192. module_put(THIS_MODULE);
  193. err:
  194. return NOTIFY_OK;
  195. }
  196. static int cryptomgr_notify(struct notifier_block *this, unsigned long msg,
  197. void *data)
  198. {
  199. switch (msg) {
  200. case CRYPTO_MSG_ALG_REQUEST:
  201. return cryptomgr_schedule_probe(data);
  202. case CRYPTO_MSG_ALG_REGISTER:
  203. return cryptomgr_schedule_test(data);
  204. case CRYPTO_MSG_ALG_LOADED:
  205. break;
  206. }
  207. return NOTIFY_DONE;
  208. }
  209. static struct notifier_block cryptomgr_notifier = {
  210. .notifier_call = cryptomgr_notify,
  211. };
  212. static int __init cryptomgr_init(void)
  213. {
  214. return crypto_register_notifier(&cryptomgr_notifier);
  215. }
  216. static void __exit cryptomgr_exit(void)
  217. {
  218. int err = crypto_unregister_notifier(&cryptomgr_notifier);
  219. BUG_ON(err);
  220. }
  221. /*
  222. * This is arch_initcall() so that the crypto self-tests are run on algorithms
  223. * registered early by subsys_initcall(). subsys_initcall() is needed for
  224. * generic implementations so that they're available for comparison tests when
  225. * other implementations are registered later by module_init().
  226. */
  227. arch_initcall(cryptomgr_init);
  228. module_exit(cryptomgr_exit);
  229. MODULE_LICENSE("GPL");
  230. MODULE_DESCRIPTION("Crypto Algorithm Manager");