sb.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. /*
  86. * TODO: Replace this all with shim's LoadImage protocol, delegating policy to it.
  87. *
  88. * We need to set shim_lock_enabled here because we disabled secure boot
  89. * validation *inside* shim but not in the firmware, so we set this variable
  90. * here to trigger that code path, whereas the actual verifier is not enabled.
  91. */
  92. shim_lock_enabled = true;
  93. goto out;
  94. }
  95. secureboot = GRUB_EFI_SECUREBOOT_MODE_ENABLED;
  96. out:
  97. grub_free (moksbstate);
  98. grub_free (setupmode);
  99. grub_free (secboot);
  100. if (secureboot == GRUB_EFI_SECUREBOOT_MODE_DISABLED)
  101. secureboot_str = "Disabled";
  102. else if (secureboot == GRUB_EFI_SECUREBOOT_MODE_ENABLED)
  103. secureboot_str = "Enabled";
  104. grub_dprintf ("efi", "UEFI Secure Boot state: %s\n", secureboot_str);
  105. return secureboot;
  106. }
  107. static grub_err_t
  108. shim_lock_verifier_init (grub_file_t io __attribute__ ((unused)),
  109. enum grub_file_type type,
  110. void **context __attribute__ ((unused)),
  111. enum grub_verify_flags *flags)
  112. {
  113. *flags = GRUB_VERIFY_FLAGS_NONE;
  114. switch (type & GRUB_FILE_TYPE_MASK)
  115. {
  116. /* Files we check. */
  117. case GRUB_FILE_TYPE_LINUX_KERNEL:
  118. case GRUB_FILE_TYPE_MULTIBOOT_KERNEL:
  119. case GRUB_FILE_TYPE_BSD_KERNEL:
  120. case GRUB_FILE_TYPE_XNU_KERNEL:
  121. case GRUB_FILE_TYPE_PLAN9_KERNEL:
  122. case GRUB_FILE_TYPE_EFI_CHAINLOADED_IMAGE:
  123. *flags = GRUB_VERIFY_FLAGS_SINGLE_CHUNK;
  124. return GRUB_ERR_NONE;
  125. /* Files that do not affect secureboot state. */
  126. case GRUB_FILE_TYPE_NONE:
  127. case GRUB_FILE_TYPE_LOOPBACK:
  128. case GRUB_FILE_TYPE_LINUX_INITRD:
  129. case GRUB_FILE_TYPE_OPENBSD_RAMDISK:
  130. case GRUB_FILE_TYPE_XNU_RAMDISK:
  131. case GRUB_FILE_TYPE_SIGNATURE:
  132. case GRUB_FILE_TYPE_PUBLIC_KEY:
  133. case GRUB_FILE_TYPE_PUBLIC_KEY_TRUST:
  134. case GRUB_FILE_TYPE_PRINT_BLOCKLIST:
  135. case GRUB_FILE_TYPE_TESTLOAD:
  136. case GRUB_FILE_TYPE_GET_SIZE:
  137. case GRUB_FILE_TYPE_ZFS_ENCRYPTION_KEY:
  138. case GRUB_FILE_TYPE_CAT:
  139. case GRUB_FILE_TYPE_HEXCAT:
  140. case GRUB_FILE_TYPE_CMP:
  141. case GRUB_FILE_TYPE_HASHLIST:
  142. case GRUB_FILE_TYPE_TO_HASH:
  143. case GRUB_FILE_TYPE_KEYBOARD_LAYOUT:
  144. case GRUB_FILE_TYPE_PIXMAP:
  145. case GRUB_FILE_TYPE_GRUB_MODULE_LIST:
  146. case GRUB_FILE_TYPE_CONFIG:
  147. case GRUB_FILE_TYPE_THEME:
  148. case GRUB_FILE_TYPE_GETTEXT_CATALOG:
  149. case GRUB_FILE_TYPE_FS_SEARCH:
  150. case GRUB_FILE_TYPE_LOADENV:
  151. case GRUB_FILE_TYPE_SAVEENV:
  152. case GRUB_FILE_TYPE_VERIFY_SIGNATURE:
  153. *flags = GRUB_VERIFY_FLAGS_SKIP_VERIFICATION;
  154. return GRUB_ERR_NONE;
  155. /* Other files. */
  156. default:
  157. return grub_error (GRUB_ERR_ACCESS_DENIED, N_("prohibited by secure boot policy"));
  158. }
  159. }
  160. static grub_err_t
  161. shim_lock_verifier_write (void *context __attribute__ ((unused)), void *buf, grub_size_t size)
  162. {
  163. grub_efi_shim_lock_protocol_t *sl = grub_efi_locate_protocol (&shim_lock_guid, 0);
  164. if (!sl)
  165. return grub_error (GRUB_ERR_ACCESS_DENIED, N_("shim_lock protocol not found"));
  166. if (sl->verify (buf, size) != GRUB_EFI_SUCCESS)
  167. return grub_error (GRUB_ERR_BAD_SIGNATURE, N_("bad shim signature"));
  168. return GRUB_ERR_NONE;
  169. }
  170. struct grub_file_verifier shim_lock_verifier =
  171. {
  172. .name = "shim_lock_verifier",
  173. .init = shim_lock_verifier_init,
  174. .write = shim_lock_verifier_write
  175. };
  176. void
  177. grub_shim_lock_verifier_setup (void)
  178. {
  179. struct grub_module_header *header;
  180. grub_efi_shim_lock_protocol_t *sl =
  181. grub_efi_locate_protocol (&shim_lock_guid, 0);
  182. /* shim_lock is missing, check if GRUB image is built with --disable-shim-lock. */
  183. if (!sl)
  184. {
  185. FOR_MODULES (header)
  186. {
  187. if (header->type == OBJ_TYPE_DISABLE_SHIM_LOCK)
  188. return;
  189. }
  190. }
  191. /* Secure Boot is off. Do not load shim_lock. */
  192. if (grub_efi_get_secureboot () != GRUB_EFI_SECUREBOOT_MODE_ENABLED)
  193. return;
  194. /* Enforce shim_lock_verifier. */
  195. grub_verifier_register (&shim_lock_verifier);
  196. shim_lock_enabled = true;
  197. grub_env_set ("shim_lock", "y");
  198. grub_env_export ("shim_lock");
  199. }
  200. bool
  201. grub_is_shim_lock_enabled (void)
  202. {
  203. return shim_lock_enabled;
  204. }