ima_init.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2005,2006,2007,2008 IBM Corporation
  4. *
  5. * Authors:
  6. * Reiner Sailer <sailer@watson.ibm.com>
  7. * Leendert van Doorn <leendert@watson.ibm.com>
  8. * Mimi Zohar <zohar@us.ibm.com>
  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 ima_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. struct ima_digest_data *hash_hdr = container_of(&hash.hdr,
  48. struct ima_digest_data, hdr);
  49. int result = -ENOMEM;
  50. int violation = 0;
  51. memset(iint, 0, sizeof(*iint));
  52. memset(&hash, 0, sizeof(hash));
  53. iint->ima_hash = hash_hdr;
  54. iint->ima_hash->algo = ima_hash_algo;
  55. iint->ima_hash->length = hash_digest_size[ima_hash_algo];
  56. /*
  57. * With TPM 2.0 hash agility, TPM chips could support multiple TPM
  58. * PCR banks, allowing firmware to configure and enable different
  59. * banks. The SHA1 bank is not necessarily enabled.
  60. *
  61. * Use the same hash algorithm for reading the TPM PCRs as for
  62. * calculating the boot aggregate digest. Preference is given to
  63. * the configured IMA default hash algorithm. Otherwise, use the
  64. * TCG required banks - SHA256 for TPM 2.0, SHA1 for TPM 1.2.
  65. * Ultimately select SHA1 also for TPM 2.0 if the SHA256 PCR bank
  66. * is not found.
  67. */
  68. if (ima_tpm_chip) {
  69. result = ima_calc_boot_aggregate(hash_hdr);
  70. if (result < 0) {
  71. audit_cause = "hashing_error";
  72. goto err_out;
  73. }
  74. }
  75. result = ima_alloc_init_template(&event_data, &entry, NULL);
  76. if (result < 0) {
  77. audit_cause = "alloc_entry";
  78. goto err_out;
  79. }
  80. result = ima_store_template(entry, violation, NULL,
  81. boot_aggregate_name,
  82. CONFIG_IMA_MEASURE_PCR_IDX);
  83. if (result < 0) {
  84. ima_free_template_entry(entry);
  85. audit_cause = "store_entry";
  86. goto err_out;
  87. }
  88. return 0;
  89. err_out:
  90. integrity_audit_msg(AUDIT_INTEGRITY_PCR, NULL, boot_aggregate_name, op,
  91. audit_cause, result, 0);
  92. return result;
  93. }
  94. #ifdef CONFIG_IMA_LOAD_X509
  95. void __init ima_load_x509(void)
  96. {
  97. int unset_flags = ima_policy_flag & IMA_APPRAISE;
  98. ima_policy_flag &= ~unset_flags;
  99. integrity_load_x509(INTEGRITY_KEYRING_IMA, CONFIG_IMA_X509_PATH);
  100. /* load also EVM key to avoid appraisal */
  101. evm_load_x509();
  102. ima_policy_flag |= unset_flags;
  103. }
  104. #endif
  105. int __init ima_init(void)
  106. {
  107. int rc;
  108. ima_tpm_chip = tpm_default_chip();
  109. if (!ima_tpm_chip)
  110. pr_info("No TPM chip found, activating TPM-bypass!\n");
  111. rc = integrity_init_keyring(INTEGRITY_KEYRING_IMA);
  112. if (rc)
  113. return rc;
  114. rc = ima_init_crypto();
  115. if (rc)
  116. return rc;
  117. rc = ima_init_template();
  118. if (rc != 0)
  119. return rc;
  120. /* It can be called before ima_init_digests(), it does not use TPM. */
  121. ima_load_kexec_buffer();
  122. rc = ima_init_digests();
  123. if (rc != 0)
  124. return rc;
  125. rc = ima_add_boot_aggregate(); /* boot aggregate must be first entry */
  126. if (rc != 0)
  127. return rc;
  128. ima_init_policy();
  129. rc = ima_fs_init();
  130. if (rc != 0)
  131. return rc;
  132. ima_init_key_queue();
  133. ima_init_reboot_notifier();
  134. ima_measure_critical_data("kernel_info", "kernel_version",
  135. UTS_RELEASE, strlen(UTS_RELEASE), false,
  136. NULL, 0);
  137. return rc;
  138. }