ima_init.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (C) 2005,2006,2007,2008 IBM Corporation
  3. *
  4. * Authors:
  5. * Reiner Sailer <sailer@watson.ibm.com>
  6. * Leendert van Doorn <leendert@watson.ibm.com>
  7. * Mimi Zohar <zohar@us.ibm.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation, version 2 of the
  12. * License.
  13. *
  14. * File: ima_init.c
  15. * initialization and cleanup functions
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/module.h>
  19. #include <linux/scatterlist.h>
  20. #include <linux/slab.h>
  21. #include <linux/err.h>
  22. #include <crypto/hash_info.h>
  23. #include "ima.h"
  24. #ifdef CONFIG_IMA_X509_PATH
  25. #define IMA_X509_PATH CONFIG_IMA_X509_PATH
  26. #else
  27. #define IMA_X509_PATH "/etc/keys/x509_ima.der"
  28. #endif
  29. /* name for boot aggregate entry */
  30. static const char *boot_aggregate_name = "boot_aggregate";
  31. int ima_used_chip;
  32. /* Add the boot aggregate to the IMA measurement list and extend
  33. * the PCR register.
  34. *
  35. * Calculate the boot aggregate, a SHA1 over tpm registers 0-7,
  36. * assuming a TPM chip exists, and zeroes if the TPM chip does not
  37. * exist. Add the boot aggregate measurement to the measurement
  38. * list and extend the PCR register.
  39. *
  40. * If a tpm chip does not exist, indicate the core root of trust is
  41. * not hardware based by invalidating the aggregate PCR value.
  42. * (The aggregate PCR value is invalidated by adding one value to
  43. * the measurement list and extending the aggregate PCR value with
  44. * a different value.) Violations add a zero entry to the measurement
  45. * list and extend the aggregate PCR value with ff...ff's.
  46. */
  47. static int __init ima_add_boot_aggregate(void)
  48. {
  49. static const char op[] = "add_boot_aggregate";
  50. const char *audit_cause = "ENOMEM";
  51. struct ima_template_entry *entry;
  52. struct integrity_iint_cache tmp_iint, *iint = &tmp_iint;
  53. int result = -ENOMEM;
  54. int violation = 0;
  55. struct {
  56. struct ima_digest_data hdr;
  57. char digest[TPM_DIGEST_SIZE];
  58. } hash;
  59. memset(iint, 0, sizeof(*iint));
  60. memset(&hash, 0, sizeof(hash));
  61. iint->ima_hash = &hash.hdr;
  62. iint->ima_hash->algo = HASH_ALGO_SHA1;
  63. iint->ima_hash->length = SHA1_DIGEST_SIZE;
  64. if (ima_used_chip) {
  65. result = ima_calc_boot_aggregate(&hash.hdr);
  66. if (result < 0) {
  67. audit_cause = "hashing_error";
  68. goto err_out;
  69. }
  70. }
  71. result = ima_alloc_init_template(iint, NULL, boot_aggregate_name,
  72. NULL, 0, &entry);
  73. if (result < 0) {
  74. audit_cause = "alloc_entry";
  75. goto err_out;
  76. }
  77. result = ima_store_template(entry, violation, NULL,
  78. boot_aggregate_name);
  79. if (result < 0) {
  80. ima_free_template_entry(entry);
  81. audit_cause = "store_entry";
  82. goto err_out;
  83. }
  84. return 0;
  85. err_out:
  86. integrity_audit_msg(AUDIT_INTEGRITY_PCR, NULL, boot_aggregate_name, op,
  87. audit_cause, result, 0);
  88. return result;
  89. }
  90. #ifdef CONFIG_IMA_LOAD_X509
  91. void __init ima_load_x509(void)
  92. {
  93. int unset_flags = ima_policy_flag & IMA_APPRAISE;
  94. ima_policy_flag &= ~unset_flags;
  95. integrity_load_x509(INTEGRITY_KEYRING_IMA, IMA_X509_PATH);
  96. ima_policy_flag |= unset_flags;
  97. }
  98. #endif
  99. int __init ima_init(void)
  100. {
  101. u8 pcr_i[TPM_DIGEST_SIZE];
  102. int rc;
  103. ima_used_chip = 0;
  104. rc = tpm_pcr_read(TPM_ANY_NUM, 0, pcr_i);
  105. if (rc == 0)
  106. ima_used_chip = 1;
  107. if (!ima_used_chip)
  108. pr_info("No TPM chip found, activating TPM-bypass!\n");
  109. rc = ima_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. rc = ima_add_boot_aggregate(); /* boot aggregate must be first entry */
  119. if (rc != 0)
  120. return rc;
  121. ima_init_policy();
  122. return ima_fs_init();
  123. }