algboss.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. struct {
  27. struct rtattr attr;
  28. struct crypto_attr_alg data;
  29. } attrs[CRYPTO_MAX_ATTRS];
  30. char template[CRYPTO_MAX_ALG_NAME];
  31. struct crypto_larval *larval;
  32. u32 otype;
  33. u32 omask;
  34. };
  35. struct crypto_test_param {
  36. char driver[CRYPTO_MAX_ALG_NAME];
  37. char alg[CRYPTO_MAX_ALG_NAME];
  38. u32 type;
  39. };
  40. static int cryptomgr_probe(void *data)
  41. {
  42. struct cryptomgr_param *param = data;
  43. struct crypto_template *tmpl;
  44. int err = -ENOENT;
  45. tmpl = crypto_lookup_template(param->template);
  46. if (!tmpl)
  47. goto out;
  48. do {
  49. err = tmpl->create(tmpl, param->tb);
  50. } while (err == -EAGAIN && !signal_pending(current));
  51. crypto_tmpl_put(tmpl);
  52. out:
  53. param->larval->adult = ERR_PTR(err);
  54. param->larval->alg.cra_flags |= CRYPTO_ALG_DEAD;
  55. complete_all(&param->larval->completion);
  56. crypto_alg_put(&param->larval->alg);
  57. kfree(param);
  58. module_put_and_kthread_exit(0);
  59. }
  60. static int cryptomgr_schedule_probe(struct crypto_larval *larval)
  61. {
  62. struct task_struct *thread;
  63. struct cryptomgr_param *param;
  64. const char *name = larval->alg.cra_name;
  65. const char *p;
  66. unsigned int len;
  67. int i;
  68. if (!try_module_get(THIS_MODULE))
  69. goto err;
  70. param = kzalloc(sizeof(*param), GFP_KERNEL);
  71. if (!param)
  72. goto err_put_module;
  73. for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
  74. ;
  75. len = p - name;
  76. if (!len || *p != '(')
  77. goto err_free_param;
  78. memcpy(param->template, name, len);
  79. i = 0;
  80. for (;;) {
  81. name = ++p;
  82. for (; isalnum(*p) || *p == '-' || *p == '_'; p++)
  83. ;
  84. if (*p == '(') {
  85. int recursion = 0;
  86. for (;;) {
  87. if (!*++p)
  88. goto err_free_param;
  89. if (*p == '(')
  90. recursion++;
  91. else if (*p == ')' && !recursion--)
  92. break;
  93. }
  94. p++;
  95. }
  96. len = p - name;
  97. if (!len)
  98. goto err_free_param;
  99. param->attrs[i].attr.rta_len = sizeof(param->attrs[i]);
  100. param->attrs[i].attr.rta_type = CRYPTOA_ALG;
  101. memcpy(param->attrs[i].data.name, name, len);
  102. param->tb[i + 1] = &param->attrs[i].attr;
  103. i++;
  104. if (i >= CRYPTO_MAX_ATTRS)
  105. goto err_free_param;
  106. if (*p == ')')
  107. break;
  108. if (*p != ',')
  109. goto err_free_param;
  110. }
  111. param->tb[i + 1] = NULL;
  112. param->type.attr.rta_len = sizeof(param->type);
  113. param->type.attr.rta_type = CRYPTOA_TYPE;
  114. param->type.data.type = larval->alg.cra_flags & ~CRYPTO_ALG_TESTED;
  115. param->type.data.mask = larval->mask & ~CRYPTO_ALG_TESTED;
  116. param->tb[0] = &param->type.attr;
  117. param->otype = larval->alg.cra_flags;
  118. param->omask = larval->mask;
  119. crypto_alg_get(&larval->alg);
  120. param->larval = larval;
  121. thread = kthread_run(cryptomgr_probe, param, "cryptomgr_probe");
  122. if (IS_ERR(thread))
  123. goto err_put_larval;
  124. return NOTIFY_STOP;
  125. err_put_larval:
  126. crypto_alg_put(&larval->alg);
  127. err_free_param:
  128. kfree(param);
  129. err_put_module:
  130. module_put(THIS_MODULE);
  131. err:
  132. return NOTIFY_OK;
  133. }
  134. static int cryptomgr_test(void *data)
  135. {
  136. struct crypto_test_param *param = data;
  137. u32 type = param->type;
  138. int err;
  139. err = alg_test(param->driver, param->alg, type, CRYPTO_ALG_TESTED);
  140. crypto_alg_tested(param->driver, err);
  141. kfree(param);
  142. module_put_and_kthread_exit(0);
  143. }
  144. static int cryptomgr_schedule_test(struct crypto_alg *alg)
  145. {
  146. struct task_struct *thread;
  147. struct crypto_test_param *param;
  148. if (IS_ENABLED(CONFIG_CRYPTO_MANAGER_DISABLE_TESTS))
  149. return NOTIFY_DONE;
  150. if (!try_module_get(THIS_MODULE))
  151. goto err;
  152. param = kzalloc(sizeof(*param), GFP_KERNEL);
  153. if (!param)
  154. goto err_put_module;
  155. memcpy(param->driver, alg->cra_driver_name, sizeof(param->driver));
  156. memcpy(param->alg, alg->cra_name, sizeof(param->alg));
  157. param->type = alg->cra_flags;
  158. thread = kthread_run(cryptomgr_test, param, "cryptomgr_test");
  159. if (IS_ERR(thread))
  160. goto err_free_param;
  161. return NOTIFY_STOP;
  162. err_free_param:
  163. kfree(param);
  164. err_put_module:
  165. module_put(THIS_MODULE);
  166. err:
  167. return NOTIFY_OK;
  168. }
  169. static int cryptomgr_notify(struct notifier_block *this, unsigned long msg,
  170. void *data)
  171. {
  172. switch (msg) {
  173. case CRYPTO_MSG_ALG_REQUEST:
  174. return cryptomgr_schedule_probe(data);
  175. case CRYPTO_MSG_ALG_REGISTER:
  176. return cryptomgr_schedule_test(data);
  177. case CRYPTO_MSG_ALG_LOADED:
  178. break;
  179. }
  180. return NOTIFY_DONE;
  181. }
  182. static struct notifier_block cryptomgr_notifier = {
  183. .notifier_call = cryptomgr_notify,
  184. };
  185. static int __init cryptomgr_init(void)
  186. {
  187. return crypto_register_notifier(&cryptomgr_notifier);
  188. }
  189. static void __exit cryptomgr_exit(void)
  190. {
  191. int err = crypto_unregister_notifier(&cryptomgr_notifier);
  192. BUG_ON(err);
  193. }
  194. /*
  195. * This is arch_initcall() so that the crypto self-tests are run on algorithms
  196. * registered early by subsys_initcall(). subsys_initcall() is needed for
  197. * generic implementations so that they're available for comparison tests when
  198. * other implementations are registered later by module_init().
  199. */
  200. arch_initcall(cryptomgr_init);
  201. module_exit(cryptomgr_exit);
  202. MODULE_LICENSE("GPL");
  203. MODULE_DESCRIPTION("Crypto Algorithm Manager");