digsig_asymmetric.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2013 Intel Corporation
  4. *
  5. * Author:
  6. * Dmitry Kasatkin <dmitry.kasatkin@intel.com>
  7. */
  8. #include <linux/err.h>
  9. #include <linux/ratelimit.h>
  10. #include <linux/key-type.h>
  11. #include <crypto/public_key.h>
  12. #include <crypto/hash_info.h>
  13. #include <keys/asymmetric-type.h>
  14. #include <keys/system_keyring.h>
  15. #include "integrity.h"
  16. /*
  17. * Request an asymmetric key.
  18. */
  19. static struct key *request_asymmetric_key(struct key *keyring, uint32_t keyid)
  20. {
  21. struct key *key;
  22. char name[12];
  23. sprintf(name, "id:%08x", keyid);
  24. pr_debug("key search: \"%s\"\n", name);
  25. key = get_ima_blacklist_keyring();
  26. if (key) {
  27. key_ref_t kref;
  28. kref = keyring_search(make_key_ref(key, 1),
  29. &key_type_asymmetric, name, true);
  30. if (!IS_ERR(kref)) {
  31. pr_err("Key '%s' is in ima_blacklist_keyring\n", name);
  32. return ERR_PTR(-EKEYREJECTED);
  33. }
  34. }
  35. if (keyring) {
  36. /* search in specific keyring */
  37. key_ref_t kref;
  38. kref = keyring_search(make_key_ref(keyring, 1),
  39. &key_type_asymmetric, name, true);
  40. if (IS_ERR(kref))
  41. key = ERR_CAST(kref);
  42. else
  43. key = key_ref_to_ptr(kref);
  44. } else {
  45. key = request_key(&key_type_asymmetric, name, NULL);
  46. }
  47. if (IS_ERR(key)) {
  48. if (keyring)
  49. pr_err_ratelimited("Request for unknown key '%s' in '%s' keyring. err %ld\n",
  50. name, keyring->description,
  51. PTR_ERR(key));
  52. else
  53. pr_err_ratelimited("Request for unknown key '%s' err %ld\n",
  54. name, PTR_ERR(key));
  55. switch (PTR_ERR(key)) {
  56. /* Hide some search errors */
  57. case -EACCES:
  58. case -ENOTDIR:
  59. case -EAGAIN:
  60. return ERR_PTR(-ENOKEY);
  61. default:
  62. return key;
  63. }
  64. }
  65. pr_debug("%s() = 0 [%x]\n", __func__, key_serial(key));
  66. return key;
  67. }
  68. int asymmetric_verify(struct key *keyring, const char *sig,
  69. int siglen, const char *data, int datalen)
  70. {
  71. struct public_key_signature pks;
  72. struct signature_v2_hdr *hdr = (struct signature_v2_hdr *)sig;
  73. struct key *key;
  74. int ret;
  75. if (siglen <= sizeof(*hdr))
  76. return -EBADMSG;
  77. siglen -= sizeof(*hdr);
  78. if (siglen != be16_to_cpu(hdr->sig_size))
  79. return -EBADMSG;
  80. if (hdr->hash_algo >= HASH_ALGO__LAST)
  81. return -ENOPKG;
  82. key = request_asymmetric_key(keyring, be32_to_cpu(hdr->keyid));
  83. if (IS_ERR(key))
  84. return PTR_ERR(key);
  85. memset(&pks, 0, sizeof(pks));
  86. pks.hash_algo = hash_algo_name[hdr->hash_algo];
  87. switch (hdr->hash_algo) {
  88. case HASH_ALGO_STREEBOG_256:
  89. case HASH_ALGO_STREEBOG_512:
  90. /* EC-RDSA and Streebog should go together. */
  91. pks.pkey_algo = "ecrdsa";
  92. pks.encoding = "raw";
  93. break;
  94. case HASH_ALGO_SM3_256:
  95. /* SM2 and SM3 should go together. */
  96. pks.pkey_algo = "sm2";
  97. pks.encoding = "raw";
  98. break;
  99. default:
  100. pks.pkey_algo = "rsa";
  101. pks.encoding = "pkcs1";
  102. break;
  103. }
  104. pks.digest = (u8 *)data;
  105. pks.digest_size = datalen;
  106. pks.s = hdr->sig;
  107. pks.s_size = siglen;
  108. ret = verify_signature(key, &pks);
  109. key_put(key);
  110. pr_debug("%s() = %d\n", __func__, ret);
  111. return ret;
  112. }
  113. /**
  114. * integrity_kernel_module_request - prevent crypto-pkcs1pad(rsa,*) requests
  115. * @kmod_name: kernel module name
  116. *
  117. * We have situation, when public_key_verify_signature() in case of RSA
  118. * algorithm use alg_name to store internal information in order to
  119. * construct an algorithm on the fly, but crypto_larval_lookup() will try
  120. * to use alg_name in order to load kernel module with same name.
  121. * Since we don't have any real "crypto-pkcs1pad(rsa,*)" kernel modules,
  122. * we are safe to fail such module request from crypto_larval_lookup().
  123. *
  124. * In this way we prevent modprobe execution during digsig verification
  125. * and avoid possible deadlock if modprobe and/or it's dependencies
  126. * also signed with digsig.
  127. */
  128. int integrity_kernel_module_request(char *kmod_name)
  129. {
  130. if (strncmp(kmod_name, "crypto-pkcs1pad(rsa,", 20) == 0)
  131. return -EINVAL;
  132. return 0;
  133. }