ima_init.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #include <linux/module.h>
  18. #include <linux/scatterlist.h>
  19. #include <linux/err.h>
  20. #include "ima.h"
  21. /* name for boot aggregate entry */
  22. static const char *boot_aggregate_name = "boot_aggregate";
  23. int ima_used_chip;
  24. /* Add the boot aggregate to the IMA measurement list and extend
  25. * the PCR register.
  26. *
  27. * Calculate the boot aggregate, a SHA1 over tpm registers 0-7,
  28. * assuming a TPM chip exists, and zeroes if the TPM chip does not
  29. * exist. Add the boot aggregate measurement to the measurement
  30. * list and extend the PCR register.
  31. *
  32. * If a tpm chip does not exist, indicate the core root of trust is
  33. * not hardware based by invalidating the aggregate PCR value.
  34. * (The aggregate PCR value is invalidated by adding one value to
  35. * the measurement list and extending the aggregate PCR value with
  36. * a different value.) Violations add a zero entry to the measurement
  37. * list and extend the aggregate PCR value with ff...ff's.
  38. */
  39. static void __init ima_add_boot_aggregate(void)
  40. {
  41. struct ima_template_entry *entry;
  42. const char *op = "add_boot_aggregate";
  43. const char *audit_cause = "ENOMEM";
  44. int result = -ENOMEM;
  45. int violation = 1;
  46. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  47. if (!entry)
  48. goto err_out;
  49. memset(&entry->template, 0, sizeof(entry->template));
  50. strncpy(entry->template.file_name, boot_aggregate_name,
  51. IMA_EVENT_NAME_LEN_MAX);
  52. if (ima_used_chip) {
  53. violation = 0;
  54. result = ima_calc_boot_aggregate(entry->template.digest);
  55. if (result < 0) {
  56. audit_cause = "hashing_error";
  57. kfree(entry);
  58. goto err_out;
  59. }
  60. }
  61. result = ima_store_template(entry, violation, NULL);
  62. if (result < 0)
  63. kfree(entry);
  64. return;
  65. err_out:
  66. integrity_audit_msg(AUDIT_INTEGRITY_PCR, NULL, boot_aggregate_name, op,
  67. audit_cause, result, 0);
  68. }
  69. int __init ima_init(void)
  70. {
  71. u8 pcr_i[IMA_DIGEST_SIZE];
  72. int rc;
  73. ima_used_chip = 0;
  74. rc = tpm_pcr_read(TPM_ANY_NUM, 0, pcr_i);
  75. if (rc == 0)
  76. ima_used_chip = 1;
  77. if (!ima_used_chip)
  78. pr_info("No TPM chip found, activating TPM-bypass!\n");
  79. ima_add_boot_aggregate(); /* boot aggregate must be first entry */
  80. ima_init_policy();
  81. return ima_fs_init();
  82. }
  83. void __exit ima_cleanup(void)
  84. {
  85. ima_fs_cleanup();
  86. }