tpm.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2018 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * Core TPM support code.
  19. */
  20. #include <grub/err.h>
  21. #include <grub/i18n.h>
  22. #include <grub/misc.h>
  23. #include <grub/mm.h>
  24. #include <grub/tpm.h>
  25. #include <grub/term.h>
  26. #include <grub/verify.h>
  27. #include <grub/dl.h>
  28. GRUB_MOD_LICENSE ("GPLv3+");
  29. static grub_err_t
  30. grub_tpm_verify_init (grub_file_t io,
  31. enum grub_file_type type __attribute__ ((unused)),
  32. void **context, enum grub_verify_flags *flags)
  33. {
  34. *context = io->name;
  35. *flags |= GRUB_VERIFY_FLAGS_SINGLE_CHUNK;
  36. /*
  37. * The loopback image is mapped as a disk allowing it to function like
  38. * a block device. However, we measure files read from the block device
  39. * not the device itself. For example, we don't measure block devices like
  40. * hd0 disk directly. This process is crucial to prevent out-of-memory
  41. * errors as loopback images are inherently large.
  42. */
  43. if ((type & GRUB_FILE_TYPE_MASK) == GRUB_FILE_TYPE_LOOPBACK)
  44. *flags = GRUB_VERIFY_FLAGS_SKIP_VERIFICATION;
  45. return GRUB_ERR_NONE;
  46. }
  47. static grub_err_t
  48. grub_tpm_verify_write (void *context, void *buf, grub_size_t size)
  49. {
  50. grub_err_t status = grub_tpm_measure (buf, size, GRUB_BINARY_PCR, context);
  51. if (status == GRUB_ERR_NONE)
  52. return GRUB_ERR_NONE;
  53. grub_dprintf ("tpm", "Measuring buffer failed: %d\n", status);
  54. return grub_is_tpm_fail_fatal () ? status : GRUB_ERR_NONE;
  55. }
  56. static grub_err_t
  57. grub_tpm_verify_string (char *str, enum grub_verify_string_type type)
  58. {
  59. const char *prefix = NULL;
  60. char *description;
  61. grub_err_t status;
  62. switch (type)
  63. {
  64. case GRUB_VERIFY_KERNEL_CMDLINE:
  65. prefix = "kernel_cmdline: ";
  66. break;
  67. case GRUB_VERIFY_MODULE_CMDLINE:
  68. prefix = "module_cmdline: ";
  69. break;
  70. case GRUB_VERIFY_COMMAND:
  71. prefix = "grub_cmd: ";
  72. break;
  73. }
  74. description = grub_malloc (grub_strlen (str) + grub_strlen (prefix) + 1);
  75. if (!description)
  76. return grub_errno;
  77. grub_memcpy (description, prefix, grub_strlen (prefix));
  78. grub_memcpy (description + grub_strlen (prefix), str,
  79. grub_strlen (str) + 1);
  80. status =
  81. grub_tpm_measure ((unsigned char *) str, grub_strlen (str),
  82. GRUB_STRING_PCR, description);
  83. grub_free (description);
  84. if (status == GRUB_ERR_NONE)
  85. return GRUB_ERR_NONE;
  86. grub_dprintf ("tpm", "Measuring string %s failed: %d\n", str, status);
  87. return grub_is_tpm_fail_fatal () ? status : GRUB_ERR_NONE;
  88. }
  89. struct grub_file_verifier grub_tpm_verifier = {
  90. .name = "tpm",
  91. .init = grub_tpm_verify_init,
  92. .write = grub_tpm_verify_write,
  93. .verify_string = grub_tpm_verify_string,
  94. };
  95. GRUB_MOD_INIT (tpm)
  96. {
  97. /*
  98. * Even though this now calls ibmvtpm's grub_tpm_present() from GRUB_MOD_INIT(),
  99. * it does seem to call it late enough in the initialization sequence so
  100. * that whatever discovered "device nodes" before this GRUB_MOD_INIT() is
  101. * called, enables the ibmvtpm driver to see the device nodes.
  102. */
  103. if (!grub_tpm_present())
  104. return;
  105. grub_verifier_register (&grub_tpm_verifier);
  106. }
  107. GRUB_MOD_FINI (tpm)
  108. {
  109. if (!grub_tpm_present())
  110. return;
  111. grub_verifier_unregister (&grub_tpm_verifier);
  112. }