machine_keyring.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Machine keyring routines.
  4. *
  5. * Copyright (c) 2021, Oracle and/or its affiliates.
  6. */
  7. #include <linux/efi.h>
  8. #include "../integrity.h"
  9. static __init int machine_keyring_init(void)
  10. {
  11. int rc;
  12. rc = integrity_init_keyring(INTEGRITY_KEYRING_MACHINE);
  13. if (rc)
  14. return rc;
  15. pr_notice("Machine keyring initialized\n");
  16. return 0;
  17. }
  18. device_initcall(machine_keyring_init);
  19. void __init add_to_machine_keyring(const char *source, const void *data, size_t len)
  20. {
  21. key_perm_t perm;
  22. int rc;
  23. perm = (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_VIEW;
  24. rc = integrity_load_cert(INTEGRITY_KEYRING_MACHINE, source, data, len, perm);
  25. /*
  26. * Some MOKList keys may not pass the machine keyring restrictions.
  27. * If the restriction check does not pass and the platform keyring
  28. * is configured, try to add it into that keyring instead.
  29. */
  30. if (rc && efi_enabled(EFI_BOOT) &&
  31. IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING))
  32. rc = integrity_load_cert(INTEGRITY_KEYRING_PLATFORM, source,
  33. data, len, perm);
  34. if (rc)
  35. pr_info("Error adding keys to machine keyring %s\n", source);
  36. }
  37. /*
  38. * Try to load the MokListTrustedRT MOK variable to see if we should trust
  39. * the MOK keys within the kernel. It is not an error if this variable
  40. * does not exist. If it does not exist, MOK keys should not be trusted
  41. * within the machine keyring.
  42. */
  43. static __init bool uefi_check_trust_mok_keys(void)
  44. {
  45. struct efi_mokvar_table_entry *mokvar_entry;
  46. mokvar_entry = efi_mokvar_entry_find("MokListTrustedRT");
  47. if (mokvar_entry)
  48. return true;
  49. return false;
  50. }
  51. static bool __init trust_moklist(void)
  52. {
  53. static bool initialized;
  54. static bool trust_mok;
  55. if (!initialized) {
  56. initialized = true;
  57. trust_mok = false;
  58. if (uefi_check_trust_mok_keys())
  59. trust_mok = true;
  60. }
  61. return trust_mok;
  62. }
  63. /*
  64. * Provides platform specific check for trusting imputed keys before loading
  65. * on .machine keyring. UEFI systems enable this trust based on a variable,
  66. * and for other platforms, it is always enabled.
  67. */
  68. bool __init imputed_trust_enabled(void)
  69. {
  70. if (efi_enabled(EFI_BOOT))
  71. return trust_moklist();
  72. return true;
  73. }