[28/30] efi: Add an EFI_SECURE_BOOT flag to indicate secure boot mode
authorDavid Howells <dhowells@redhat.com>
Mon, 18 Feb 2019 12:45:03 +0000 (12:45 +0000)
committerSalvatore Bonaccorso <carnil@debian.org>
Sat, 2 Aug 2025 13:13:02 +0000 (15:13 +0200)
Origin: https://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs.git/commit?id=a5d70c55c603233c192b375f72116a395909da28

UEFI machines can be booted in Secure Boot mode.  Add an EFI_SECURE_BOOT
flag that can be passed to efi_enabled() to find out whether secure boot is
enabled.

Move the switch-statement in x86's setup_arch() that inteprets the
secure_boot boot parameter to generic code and set the bit there.

Suggested-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
cc: linux-efi@vger.kernel.org
[rperier: Forward-ported to 5.5:
 - Use pr_warn()
 - Adjust context]
[bwh: Forward-ported to 5.6: adjust context]
[bwh: Forward-ported to 5.7:
 - Use the next available bit in efi.flags
 - Adjust context]

Gbp-Pq: Topic features/all/lockdown
Gbp-Pq: Name efi-add-an-efi_secure_boot-flag-to-indicate-secure-b.patch

arch/x86/kernel/setup.c
drivers/firmware/efi/Makefile
drivers/firmware/efi/secureboot.c [new file with mode: 0644]
include/linux/efi.h

index 18a034613d94d62ba9f438bad9aadcbe828c2969..5c2bc32778d1ab0424e443eb2679496da66d3ed0 100644 (file)
@@ -1202,19 +1202,7 @@ void __init setup_arch(char **cmdline_p)
        /* Allocate bigger log buffer */
        setup_log_buf(1);
 
-       if (efi_enabled(EFI_BOOT)) {
-               switch (boot_params.secure_boot) {
-               case efi_secureboot_mode_disabled:
-                       pr_info("Secure boot disabled\n");
-                       break;
-               case efi_secureboot_mode_enabled:
-                       pr_info("Secure boot enabled\n");
-                       break;
-               default:
-                       pr_info("Secure boot could not be determined\n");
-                       break;
-               }
-       }
+       efi_set_secure_boot(boot_params.secure_boot);
 
        reserve_initrd();
 
index 8d151e332584d0fd52cb4ed8a83373e01aac66b8..bd29fe4ddbf3192347553b5db839ff4d535efa58 100644 (file)
@@ -27,6 +27,7 @@ obj-$(CONFIG_EFI_FAKE_MEMMAP)         += fake_map.o
 obj-$(CONFIG_EFI_BOOTLOADER_CONTROL)   += efibc.o
 obj-$(CONFIG_EFI_TEST)                 += test/
 obj-$(CONFIG_EFI_DEV_PATH_PARSER)      += dev-path-parser.o
+obj-$(CONFIG_EFI)                      += secureboot.o
 obj-$(CONFIG_APPLE_PROPERTIES)         += apple-properties.o
 obj-$(CONFIG_EFI_RCI2_TABLE)           += rci2-table.o
 obj-$(CONFIG_EFI_EMBEDDED_FIRMWARE)    += embedded-firmware.o
diff --git a/drivers/firmware/efi/secureboot.c b/drivers/firmware/efi/secureboot.c
new file mode 100644 (file)
index 0000000..b662066
--- /dev/null
@@ -0,0 +1,39 @@
+
+/* Core kernel secure boot support.
+ *
+ * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells (dhowells@redhat.com)
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public Licence
+ * as published by the Free Software Foundation; either version
+ * 2 of the Licence, or (at your option) any later version.
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/efi.h>
+#include <linux/kernel.h>
+#include <linux/printk.h>
+
+/*
+ * Decide what to do when UEFI secure boot mode is enabled.
+ */
+void __init efi_set_secure_boot(enum efi_secureboot_mode mode)
+{
+       if (efi_enabled(EFI_BOOT)) {
+               switch (mode) {
+               case efi_secureboot_mode_disabled:
+                       pr_info("Secure boot disabled\n");
+                       break;
+               case efi_secureboot_mode_enabled:
+                       set_bit(EFI_SECURE_BOOT, &efi.flags);
+                       pr_info("Secure boot enabled\n");
+                       break;
+               default:
+                       pr_warn("Secure boot could not be determined (mode %u)\n",
+                               mode);
+                       break;
+               }
+       }
+}
index 1d73c77051c09b57b24d7ae38246e26fb567ba33..b54665ed0ba3a34f55aedc7a1dee4a535f4980b0 100644 (file)
@@ -859,6 +859,14 @@ extern int __init efi_setup_pcdp_console(char *);
 #define EFI_MEM_ATTR           10      /* Did firmware publish an EFI_MEMORY_ATTRIBUTES table? */
 #define EFI_MEM_NO_SOFT_RESERVE        11      /* Is the kernel configured to ignore soft reservations? */
 #define EFI_PRESERVE_BS_REGIONS        12      /* Are EFI boot-services memory segments available? */
+#define EFI_SECURE_BOOT                13      /* Are we in Secure Boot mode? */
+
+enum efi_secureboot_mode {
+       efi_secureboot_mode_unset,
+       efi_secureboot_mode_unknown,
+       efi_secureboot_mode_disabled,
+       efi_secureboot_mode_enabled,
+};
 
 #ifdef CONFIG_EFI
 /*
@@ -883,6 +891,7 @@ static inline bool efi_rt_services_supported(unsigned int mask)
        return (efi.runtime_supported_mask & mask) == mask;
 }
 extern void efi_find_mirror(void);
+extern void __init efi_set_secure_boot(enum efi_secureboot_mode mode);
 #else
 static inline bool efi_enabled(int feature)
 {
@@ -902,6 +911,7 @@ static inline bool efi_rt_services_supported(unsigned int mask)
 }
 
 static inline void efi_find_mirror(void) {}
+static inline void efi_set_secure_boot(enum efi_secureboot_mode mode) {}
 #endif
 
 extern int efi_status_to_err(efi_status_t status);
@@ -1130,13 +1140,6 @@ static inline bool efi_runtime_disabled(void) { return true; }
 extern void efi_call_virt_check_flags(unsigned long flags, const char *call);
 extern unsigned long efi_call_virt_save_flags(void);
 
-enum efi_secureboot_mode {
-       efi_secureboot_mode_unset,
-       efi_secureboot_mode_unknown,
-       efi_secureboot_mode_disabled,
-       efi_secureboot_mode_enabled,
-};
-
 static inline
 enum efi_secureboot_mode efi_get_secureboot_mode(efi_get_variable_t *get_var)
 {