sb.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2020 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. * UEFI Secure Boot related checkings.
  19. */
  20. #include <grub/efi/efi.h>
  21. #include <grub/efi/pe32.h>
  22. #include <grub/efi/sb.h>
  23. #include <grub/env.h>
  24. #include <grub/err.h>
  25. #include <grub/file.h>
  26. #include <grub/i386/linux.h>
  27. #include <grub/kernel.h>
  28. #include <grub/mm.h>
  29. #include <grub/types.h>
  30. #include <grub/verify.h>
  31. static grub_guid_t shim_lock_guid = GRUB_EFI_SHIM_LOCK_GUID;
  32. static bool shim_lock_enabled = false;
  33. /*
  34. * Determine whether we're in secure boot mode.
  35. *
  36. * Please keep the logic in sync with the Linux kernel,
  37. * drivers/firmware/efi/libstub/secureboot.c:efi_get_secureboot().
  38. */
  39. grub_uint8_t
  40. grub_efi_get_secureboot (void)
  41. {
  42. static grub_guid_t efi_variable_guid = GRUB_EFI_GLOBAL_VARIABLE_GUID;
  43. grub_efi_status_t status;
  44. grub_efi_uint32_t attr = 0;
  45. grub_size_t size = 0;
  46. grub_uint8_t *secboot = NULL;
  47. grub_uint8_t *setupmode = NULL;
  48. grub_uint8_t *moksbstate = NULL;
  49. grub_uint8_t secureboot = GRUB_EFI_SECUREBOOT_MODE_UNKNOWN;
  50. const char *secureboot_str = "UNKNOWN";
  51. status = grub_efi_get_variable ("SecureBoot", &efi_variable_guid,
  52. &size, (void **) &secboot);
  53. if (status == GRUB_EFI_NOT_FOUND)
  54. {
  55. secureboot = GRUB_EFI_SECUREBOOT_MODE_DISABLED;
  56. goto out;
  57. }
  58. if (status != GRUB_EFI_SUCCESS)
  59. goto out;
  60. status = grub_efi_get_variable ("SetupMode", &efi_variable_guid,
  61. &size, (void **) &setupmode);
  62. if (status != GRUB_EFI_SUCCESS)
  63. goto out;
  64. if ((*secboot == 0) || (*setupmode == 1))
  65. {
  66. secureboot = GRUB_EFI_SECUREBOOT_MODE_DISABLED;
  67. goto out;
  68. }
  69. /*
  70. * See if a user has put the shim into insecure mode. If so, and if the
  71. * variable doesn't have the runtime attribute set, we might as well
  72. * honor that.
  73. */
  74. status = grub_efi_get_variable_with_attributes ("MokSBState", &shim_lock_guid,
  75. &size, (void **) &moksbstate, &attr);
  76. /* If it fails, we don't care why. Default to secure. */
  77. if (status != GRUB_EFI_SUCCESS)
  78. {
  79. secureboot = GRUB_EFI_SECUREBOOT_MODE_ENABLED;
  80. goto out;
  81. }
  82. if (!(attr & GRUB_EFI_VARIABLE_RUNTIME_ACCESS) && *moksbstate == 1)
  83. {
  84. secureboot = GRUB_EFI_SECUREBOOT_MODE_DISABLED;
  85. goto out;
  86. }
  87. secureboot = GRUB_EFI_SECUREBOOT_MODE_ENABLED;
  88. out:
  89. grub_free (moksbstate);
  90. grub_free (setupmode);
  91. grub_free (secboot);
  92. if (secureboot == GRUB_EFI_SECUREBOOT_MODE_DISABLED)
  93. secureboot_str = "Disabled";
  94. else if (secureboot == GRUB_EFI_SECUREBOOT_MODE_ENABLED)
  95. secureboot_str = "Enabled";
  96. grub_dprintf ("efi", "UEFI Secure Boot state: %s\n", secureboot_str);
  97. return secureboot;
  98. }
  99. static grub_err_t
  100. shim_lock_verifier_init (grub_file_t io __attribute__ ((unused)),
  101. enum grub_file_type type,
  102. void **context __attribute__ ((unused)),
  103. enum grub_verify_flags *flags)
  104. {
  105. *flags = GRUB_VERIFY_FLAGS_NONE;
  106. switch (type & GRUB_FILE_TYPE_MASK)
  107. {
  108. /* Files we check. */
  109. case GRUB_FILE_TYPE_LINUX_KERNEL:
  110. case GRUB_FILE_TYPE_MULTIBOOT_KERNEL:
  111. case GRUB_FILE_TYPE_BSD_KERNEL:
  112. case GRUB_FILE_TYPE_XNU_KERNEL:
  113. case GRUB_FILE_TYPE_PLAN9_KERNEL:
  114. case GRUB_FILE_TYPE_EFI_CHAINLOADED_IMAGE:
  115. *flags = GRUB_VERIFY_FLAGS_SINGLE_CHUNK;
  116. return GRUB_ERR_NONE;
  117. /* Files that do not affect secureboot state. */
  118. case GRUB_FILE_TYPE_NONE:
  119. case GRUB_FILE_TYPE_LOOPBACK:
  120. case GRUB_FILE_TYPE_LINUX_INITRD:
  121. case GRUB_FILE_TYPE_OPENBSD_RAMDISK:
  122. case GRUB_FILE_TYPE_XNU_RAMDISK:
  123. case GRUB_FILE_TYPE_SIGNATURE:
  124. case GRUB_FILE_TYPE_PUBLIC_KEY:
  125. case GRUB_FILE_TYPE_PUBLIC_KEY_TRUST:
  126. case GRUB_FILE_TYPE_PRINT_BLOCKLIST:
  127. case GRUB_FILE_TYPE_TESTLOAD:
  128. case GRUB_FILE_TYPE_GET_SIZE:
  129. case GRUB_FILE_TYPE_ZFS_ENCRYPTION_KEY:
  130. case GRUB_FILE_TYPE_CAT:
  131. case GRUB_FILE_TYPE_HEXCAT:
  132. case GRUB_FILE_TYPE_CMP:
  133. case GRUB_FILE_TYPE_HASHLIST:
  134. case GRUB_FILE_TYPE_TO_HASH:
  135. case GRUB_FILE_TYPE_KEYBOARD_LAYOUT:
  136. case GRUB_FILE_TYPE_PIXMAP:
  137. case GRUB_FILE_TYPE_GRUB_MODULE_LIST:
  138. case GRUB_FILE_TYPE_CONFIG:
  139. case GRUB_FILE_TYPE_THEME:
  140. case GRUB_FILE_TYPE_GETTEXT_CATALOG:
  141. case GRUB_FILE_TYPE_FS_SEARCH:
  142. case GRUB_FILE_TYPE_LOADENV:
  143. case GRUB_FILE_TYPE_SAVEENV:
  144. case GRUB_FILE_TYPE_VERIFY_SIGNATURE:
  145. *flags = GRUB_VERIFY_FLAGS_SKIP_VERIFICATION;
  146. return GRUB_ERR_NONE;
  147. /* Other files. */
  148. default:
  149. return grub_error (GRUB_ERR_ACCESS_DENIED, N_("prohibited by secure boot policy"));
  150. }
  151. }
  152. static grub_err_t
  153. shim_lock_verifier_write (void *context __attribute__ ((unused)), void *buf, grub_size_t size)
  154. {
  155. grub_efi_shim_lock_protocol_t *sl = grub_efi_locate_protocol (&shim_lock_guid, 0);
  156. if (!sl)
  157. return grub_error (GRUB_ERR_ACCESS_DENIED, N_("shim_lock protocol not found"));
  158. if (sl->verify (buf, size) != GRUB_EFI_SUCCESS)
  159. return grub_error (GRUB_ERR_BAD_SIGNATURE, N_("bad shim signature"));
  160. return GRUB_ERR_NONE;
  161. }
  162. struct grub_file_verifier shim_lock_verifier =
  163. {
  164. .name = "shim_lock_verifier",
  165. .init = shim_lock_verifier_init,
  166. .write = shim_lock_verifier_write
  167. };
  168. void
  169. grub_shim_lock_verifier_setup (void)
  170. {
  171. struct grub_module_header *header;
  172. grub_efi_shim_lock_protocol_t *sl =
  173. grub_efi_locate_protocol (&shim_lock_guid, 0);
  174. /* shim_lock is missing, check if GRUB image is built with --disable-shim-lock. */
  175. if (!sl)
  176. {
  177. FOR_MODULES (header)
  178. {
  179. if (header->type == OBJ_TYPE_DISABLE_SHIM_LOCK)
  180. return;
  181. }
  182. }
  183. /* Secure Boot is off. Do not load shim_lock. */
  184. if (grub_efi_get_secureboot () != GRUB_EFI_SECUREBOOT_MODE_ENABLED)
  185. return;
  186. /* Enforce shim_lock_verifier. */
  187. grub_verifier_register (&shim_lock_verifier);
  188. shim_lock_enabled = true;
  189. grub_env_set ("shim_lock", "y");
  190. grub_env_export ("shim_lock");
  191. }
  192. bool
  193. grub_is_shim_lock_enabled (void)
  194. {
  195. return shim_lock_enabled;
  196. }