ima_modsig.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * IMA support for appraising module-style appended signatures.
  4. *
  5. * Copyright (C) 2019 IBM Corporation
  6. *
  7. * Author:
  8. * Thiago Jung Bauermann <[email protected]>
  9. */
  10. #include <linux/types.h>
  11. #include <linux/module_signature.h>
  12. #include <keys/asymmetric-type.h>
  13. #include <crypto/pkcs7.h>
  14. #include "ima.h"
  15. struct modsig {
  16. struct pkcs7_message *pkcs7_msg;
  17. enum hash_algo hash_algo;
  18. /* This digest will go in the 'd-modsig' field of the IMA template. */
  19. const u8 *digest;
  20. u32 digest_size;
  21. /*
  22. * This is what will go to the measurement list if the template requires
  23. * storing the signature.
  24. */
  25. int raw_pkcs7_len;
  26. u8 raw_pkcs7[];
  27. };
  28. /*
  29. * ima_read_modsig - Read modsig from buf.
  30. *
  31. * Return: 0 on success, error code otherwise.
  32. */
  33. int ima_read_modsig(enum ima_hooks func, const void *buf, loff_t buf_len,
  34. struct modsig **modsig)
  35. {
  36. const size_t marker_len = strlen(MODULE_SIG_STRING);
  37. const struct module_signature *sig;
  38. struct modsig *hdr;
  39. size_t sig_len;
  40. const void *p;
  41. int rc;
  42. if (buf_len <= marker_len + sizeof(*sig))
  43. return -ENOENT;
  44. p = buf + buf_len - marker_len;
  45. if (memcmp(p, MODULE_SIG_STRING, marker_len))
  46. return -ENOENT;
  47. buf_len -= marker_len;
  48. sig = (const struct module_signature *)(p - sizeof(*sig));
  49. rc = mod_check_sig(sig, buf_len, func_tokens[func]);
  50. if (rc)
  51. return rc;
  52. sig_len = be32_to_cpu(sig->sig_len);
  53. buf_len -= sig_len + sizeof(*sig);
  54. /* Allocate sig_len additional bytes to hold the raw PKCS#7 data. */
  55. hdr = kzalloc(sizeof(*hdr) + sig_len, GFP_KERNEL);
  56. if (!hdr)
  57. return -ENOMEM;
  58. hdr->pkcs7_msg = pkcs7_parse_message(buf + buf_len, sig_len);
  59. if (IS_ERR(hdr->pkcs7_msg)) {
  60. rc = PTR_ERR(hdr->pkcs7_msg);
  61. kfree(hdr);
  62. return rc;
  63. }
  64. memcpy(hdr->raw_pkcs7, buf + buf_len, sig_len);
  65. hdr->raw_pkcs7_len = sig_len;
  66. /* We don't know the hash algorithm yet. */
  67. hdr->hash_algo = HASH_ALGO__LAST;
  68. *modsig = hdr;
  69. return 0;
  70. }
  71. /**
  72. * ima_collect_modsig - Calculate the file hash without the appended signature.
  73. * @modsig: parsed module signature
  74. * @buf: data to verify the signature on
  75. * @size: data size
  76. *
  77. * Since the modsig is part of the file contents, the hash used in its signature
  78. * isn't the same one ordinarily calculated by IMA. Therefore PKCS7 code
  79. * calculates a separate one for signature verification.
  80. */
  81. void ima_collect_modsig(struct modsig *modsig, const void *buf, loff_t size)
  82. {
  83. int rc;
  84. /*
  85. * Provide the file contents (minus the appended sig) so that the PKCS7
  86. * code can calculate the file hash.
  87. */
  88. size -= modsig->raw_pkcs7_len + strlen(MODULE_SIG_STRING) +
  89. sizeof(struct module_signature);
  90. rc = pkcs7_supply_detached_data(modsig->pkcs7_msg, buf, size);
  91. if (rc)
  92. return;
  93. /* Ask the PKCS7 code to calculate the file hash. */
  94. rc = pkcs7_get_digest(modsig->pkcs7_msg, &modsig->digest,
  95. &modsig->digest_size, &modsig->hash_algo);
  96. }
  97. int ima_modsig_verify(struct key *keyring, const struct modsig *modsig)
  98. {
  99. return verify_pkcs7_message_sig(NULL, 0, modsig->pkcs7_msg, keyring,
  100. VERIFYING_MODULE_SIGNATURE, NULL, NULL);
  101. }
  102. int ima_get_modsig_digest(const struct modsig *modsig, enum hash_algo *algo,
  103. const u8 **digest, u32 *digest_size)
  104. {
  105. *algo = modsig->hash_algo;
  106. *digest = modsig->digest;
  107. *digest_size = modsig->digest_size;
  108. return 0;
  109. }
  110. int ima_get_raw_modsig(const struct modsig *modsig, const void **data,
  111. u32 *data_len)
  112. {
  113. *data = &modsig->raw_pkcs7;
  114. *data_len = modsig->raw_pkcs7_len;
  115. return 0;
  116. }
  117. void ima_free_modsig(struct modsig *modsig)
  118. {
  119. if (!modsig)
  120. return;
  121. pkcs7_free_message(modsig->pkcs7_msg);
  122. kfree(modsig);
  123. }