ima_init.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2005,2006,2007,2008 IBM Corporation
  4. *
  5. * Authors:
  6. * Reiner Sailer <[email protected]>
  7. * Leendert van Doorn <[email protected]>
  8. * Mimi Zohar <[email protected]>
  9. *
  10. * File: ima_init.c
  11. * initialization and cleanup functions
  12. */
  13. #include <linux/init.h>
  14. #include <linux/scatterlist.h>
  15. #include <linux/slab.h>
  16. #include <linux/err.h>
  17. #include <linux/ima.h>
  18. #include <generated/utsrelease.h>
  19. #include "ima.h"
  20. /* name for boot aggregate entry */
  21. const char boot_aggregate_name[] = "boot_aggregate";
  22. struct tpm_chip *ima_tpm_chip;
  23. /* Add the boot aggregate to the IMA measurement list and extend
  24. * the PCR register.
  25. *
  26. * Calculate the boot aggregate, a hash over tpm registers 0-7,
  27. * assuming a TPM chip exists, and zeroes if the TPM chip does not
  28. * exist. Add the boot aggregate measurement to the measurement
  29. * list and extend the PCR register.
  30. *
  31. * If a tpm chip does not exist, indicate the core root of trust is
  32. * not hardware based by invalidating the aggregate PCR value.
  33. * (The aggregate PCR value is invalidated by adding one value to
  34. * the measurement list and extending the aggregate PCR value with
  35. * a different value.) Violations add a zero entry to the measurement
  36. * list and extend the aggregate PCR value with ff...ff's.
  37. */
  38. static int __init ima_add_boot_aggregate(void)
  39. {
  40. static const char op[] = "add_boot_aggregate";
  41. const char *audit_cause = "ENOMEM";
  42. struct ima_template_entry *entry;
  43. struct integrity_iint_cache tmp_iint, *iint = &tmp_iint;
  44. struct ima_event_data event_data = { .iint = iint,
  45. .filename = boot_aggregate_name };
  46. struct ima_max_digest_data hash;
  47. int result = -ENOMEM;
  48. int violation = 0;
  49. memset(iint, 0, sizeof(*iint));
  50. memset(&hash, 0, sizeof(hash));
  51. iint->ima_hash = &hash.hdr;
  52. iint->ima_hash->algo = ima_hash_algo;
  53. iint->ima_hash->length = hash_digest_size[ima_hash_algo];
  54. /*
  55. * With TPM 2.0 hash agility, TPM chips could support multiple TPM
  56. * PCR banks, allowing firmware to configure and enable different
  57. * banks. The SHA1 bank is not necessarily enabled.
  58. *
  59. * Use the same hash algorithm for reading the TPM PCRs as for
  60. * calculating the boot aggregate digest. Preference is given to
  61. * the configured IMA default hash algorithm. Otherwise, use the
  62. * TCG required banks - SHA256 for TPM 2.0, SHA1 for TPM 1.2.
  63. * Ultimately select SHA1 also for TPM 2.0 if the SHA256 PCR bank
  64. * is not found.
  65. */
  66. if (ima_tpm_chip) {
  67. result = ima_calc_boot_aggregate(&hash.hdr);
  68. if (result < 0) {
  69. audit_cause = "hashing_error";
  70. goto err_out;
  71. }
  72. }
  73. result = ima_alloc_init_template(&event_data, &entry, NULL);
  74. if (result < 0) {
  75. audit_cause = "alloc_entry";
  76. goto err_out;
  77. }
  78. result = ima_store_template(entry, violation, NULL,
  79. boot_aggregate_name,
  80. CONFIG_IMA_MEASURE_PCR_IDX);
  81. if (result < 0) {
  82. ima_free_template_entry(entry);
  83. audit_cause = "store_entry";
  84. goto err_out;
  85. }
  86. return 0;
  87. err_out:
  88. integrity_audit_msg(AUDIT_INTEGRITY_PCR, NULL, boot_aggregate_name, op,
  89. audit_cause, result, 0);
  90. return result;
  91. }
  92. #ifdef CONFIG_IMA_LOAD_X509
  93. void __init ima_load_x509(void)
  94. {
  95. int unset_flags = ima_policy_flag & IMA_APPRAISE;
  96. ima_policy_flag &= ~unset_flags;
  97. integrity_load_x509(INTEGRITY_KEYRING_IMA, CONFIG_IMA_X509_PATH);
  98. /* load also EVM key to avoid appraisal */
  99. evm_load_x509();
  100. ima_policy_flag |= unset_flags;
  101. }
  102. #endif
  103. int __init ima_init(void)
  104. {
  105. int rc;
  106. ima_tpm_chip = tpm_default_chip();
  107. if (!ima_tpm_chip)
  108. pr_info("No TPM chip found, activating TPM-bypass!\n");
  109. rc = integrity_init_keyring(INTEGRITY_KEYRING_IMA);
  110. if (rc)
  111. return rc;
  112. rc = ima_init_crypto();
  113. if (rc)
  114. return rc;
  115. rc = ima_init_template();
  116. if (rc != 0)
  117. return rc;
  118. /* It can be called before ima_init_digests(), it does not use TPM. */
  119. ima_load_kexec_buffer();
  120. rc = ima_init_digests();
  121. if (rc != 0)
  122. return rc;
  123. rc = ima_add_boot_aggregate(); /* boot aggregate must be first entry */
  124. if (rc != 0)
  125. return rc;
  126. ima_init_policy();
  127. rc = ima_fs_init();
  128. if (rc != 0)
  129. return rc;
  130. ima_init_key_queue();
  131. ima_measure_critical_data("kernel_info", "kernel_version",
  132. UTS_RELEASE, strlen(UTS_RELEASE), false,
  133. NULL, 0);
  134. return rc;
  135. }