Skip to contentSkip to conversation

add ima_arch support for ARM64

PATCH In review Chester Lin 4 participants 13 messages Oct 30, 2020 – Nov 4, 2020

Subsystems (7)
x86 arm64 Security (LSM) EFI +3 more subsystems
lore
Share & tools
lore
#0 lore raw

v3 cover add ima_arch support for ARM64

Add IMA arch dependent support for ARM64. Some IMA functions can check arch-specific status before running. For example, the ima_load_data function or the boot param "ima_appraise=" should not be executed when UEFI secure boot is enabled. We want to fill the gap in order to complete the IMA support on ARM64.

Changes in v3:
- Generalize efi_get_secureboot() so both ima_arch and efistub can reuse it.
- Implement ima_get_efi_secureboot() as the replacement of get_sb_mode() so x86 and arm64 can share the same logic.

Changes in v2:
- Separate get_sb_mode() from x86 so all EFI-based architectures can reuse the same function.
- Refactor arch/arm64/kernel/ima_arch.c based on Ard's patch[1].

Test platforms:
- ARM64: QEMU [aarch64-virt] + EDK2/OVMF
- ARM64: NXP LX2160A-RDB + EDK2
- X86_64: Dell Lattitude 7490 + (BIOS 1.14.0 01/22/2020)

[1] https://www.spinics.net/lists/linux-efi/msg20645.html

Chester Lin (3):
efi: generalize efi_get_secureboot
ima: remove get_sb_mode() and create ima_get_efi_secureboot() arm64/ima: add ima_arch support

 arch/arm64/Kconfig                        |  1 +
 arch/arm64/kernel/Makefile                |  2 +
 arch/arm64/kernel/ima_arch.c              | 43 +++++++++++++
 arch/x86/kernel/ima_arch.c                | 69 +++++---------------
 drivers/firmware/efi/libstub/Makefile     |  2 +-
 drivers/firmware/efi/libstub/efi-stub.c   |  2 +-
 drivers/firmware/efi/libstub/efistub.h    | 22 ++++---
 drivers/firmware/efi/libstub/secureboot.c | 76 -----------------------
 drivers/firmware/efi/libstub/x86-stub.c   |  2 +-
 include/linux/efi.h                       | 41 +++++++++++-
 include/linux/ima.h                       | 10 +++
 security/integrity/ima/Makefile           |  1 +
 security/integrity/ima/ima_efi.c          | 26 ++++++++
 13 files changed, 154 insertions(+), 143 deletions(-)
 create mode 100644 arch/arm64/kernel/ima_arch.c
 delete mode 100644 drivers/firmware/efi/libstub/secureboot.c
 create mode 100644 security/integrity/ima/ima_efi.c
Signature · 1 line
2.28.0
#1 lore raw

PATCH v3 1/3 efi: generalize efi_get_secureboot

Generalize the efi_get_secureboot() function so not only efistub but also other subsystems can use it.

---
 drivers/firmware/efi/libstub/Makefile     |  2 +-
 drivers/firmware/efi/libstub/efi-stub.c   |  2 +-
 drivers/firmware/efi/libstub/efistub.h    | 22 ++++---
 drivers/firmware/efi/libstub/secureboot.c | 76 -----------------------
 drivers/firmware/efi/libstub/x86-stub.c   |  2 +-
 include/linux/efi.h                       | 41 +++++++++++-
 6 files changed, 57 insertions(+), 88 deletions(-)
 delete mode 100644 drivers/firmware/efi/libstub/secureboot.c
drivers/firmware/efi/libstub/Makefile +1 -1
index 8a94388e38b3..88e47b0ca09d 100644
@@ -49,7 +49,7 @@ OBJECT_FILES_NON_STANDARD	:= y
 # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in.
 KCOV_INSTRUMENT			:= n
 
-lib-y				:= efi-stub-helper.o gop.o secureboot.o tpm.o \
+lib-y				:= efi-stub-helper.o gop.o tpm.o \
 				   file.o mem.o random.o randomalloc.o pci.o \
 				   skip_spaces.o lib-cmdline.o lib-ctype.o \
 				   alignedmem.o relocate.o vsprintf.o
drivers/firmware/efi/libstub/efi-stub.c +1 -1
index 914a343c7785..ad96f1d786a9 100644
@@ -196,7 +196,7 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
 	/* Ask the firmware to clear memory on unclean shutdown */
 	efi_enable_reset_attack_mitigation();
 
-	secure_boot = efi_get_secureboot();
+	secure_boot = efi_get_secureboot(get_efi_var);
 
 	/*
 	 * Unauthenticated device tree data is a security hazard, so ignore
drivers/firmware/efi/libstub/efistub.h +14 -8
index 2d7abcd99de9..b1833b51e6d6 100644
@@ -91,14 +91,6 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
 	fdt_setprop((fdt), (node_offset), (name), &(var), sizeof(var))
 #endif
 
-#define get_efi_var(name, vendor, ...)				\
-	efi_rt_call(get_variable, (efi_char16_t *)(name),	\
-		    (efi_guid_t *)(vendor), __VA_ARGS__)
-
-#define set_efi_var(name, vendor, ...)				\
-	efi_rt_call(set_variable, (efi_char16_t *)(name),	\
-		    (efi_guid_t *)(vendor), __VA_ARGS__)
-
 #define efi_get_handle_at(array, idx)					\
 	(efi_is_native() ? (array)[idx] 				\
 		: (efi_handle_t)(unsigned long)((u32 *)(array))[idx])
@@ -112,6 +104,20 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
 		((handle = efi_get_handle_at((array), i)) || true);	\
 	     i++)
 
+static inline
+efi_status_t get_efi_var(efi_char16_t *name, efi_guid_t *vendor, u32 *attr,
+			 unsigned long *size, void *data)
+{
+	return efi_rt_call(get_variable, name, vendor, attr, size, data);
+}
+
+static inline
+efi_status_t set_efi_var(efi_char16_t *name, efi_guid_t *vendor, u32 attr,
+			 unsigned long size, void *data)
+{
+	return efi_rt_call(set_variable, name, vendor, attr, size, data);
+}
+
 static inline
 void efi_set_u64_split(u64 data, u32 *lo, u32 *hi)
 {
drivers/firmware/efi/libstub/secureboot.c deleted +0 -76

76 lines not on this page · raw message

drivers/firmware/efi/libstub/x86-stub.c +1 -1
index 3672539cb96e..3f9b492c566b 100644
@@ -781,7 +781,7 @@ unsigned long efi_main(efi_handle_t handle,
 	 * otherwise we ask the BIOS.
 	 */
 	if (boot_params->secure_boot == efi_secureboot_mode_unset)
-		boot_params->secure_boot = efi_get_secureboot();
+		boot_params->secure_boot = efi_get_secureboot(get_efi_var);
 
 	/* Ask the firmware to clear memory on unclean shutdown */
 	efi_enable_reset_attack_mitigation();
include/linux/efi.h +40 -1

47 lines not on this page · raw message

Signature · 1 line
2.28.0
maintainer
#2 lore raw

Re: [PATCH v3 1/3] efi: generalize efi_get_secureboot

Hello Chester,

Thanks again for looking into this.

drivers/firmware/efi/libstub/secureboot.c

Quoted 86 lines · Chester Lin

Generalize the efi_get_secureboot() function so not only efistub but also other subsystems can use it.

---
 drivers/firmware/efi/libstub/Makefile     |  2 +-
 drivers/firmware/efi/libstub/efi-stub.c   |  2 +-
 drivers/firmware/efi/libstub/efistub.h    | 22 ++++---
 drivers/firmware/efi/libstub/secureboot.c | 76 -----------------------
 drivers/firmware/efi/libstub/x86-stub.c   |  2 +-
 include/linux/efi.h                       | 41 +++++++++++-
 6 files changed, 57 insertions(+), 88 deletions(-)
 delete mode 100644 drivers/firmware/efi/libstub/secureboot.c
drivers/firmware/efi/libstub/Makefile +1 -1
index 8a94388e38b3..88e47b0ca09d 100644
@@ -49,7 +49,7 @@ OBJECT_FILES_NON_STANDARD     := y
 # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in.
 KCOV_INSTRUMENT                        := n
 
-lib-y                          := efi-stub-helper.o gop.o secureboot.o tpm.o \
+lib-y                          := efi-stub-helper.o gop.o tpm.o \
                                   file.o mem.o random.o randomalloc.o pci.o \
                                   skip_spaces.o lib-cmdline.o lib-ctype.o \
                                   alignedmem.o relocate.o vsprintf.o
drivers/firmware/efi/libstub/efi-stub.c +1 -1
index 914a343c7785..ad96f1d786a9 100644
@@ -196,7 +196,7 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
        /* Ask the firmware to clear memory on unclean shutdown */
        efi_enable_reset_attack_mitigation();
 
-       secure_boot = efi_get_secureboot();
+       secure_boot = efi_get_secureboot(get_efi_var);
 
        /*
         * Unauthenticated device tree data is a security hazard, so ignore
drivers/firmware/efi/libstub/efistub.h +14 -8
index 2d7abcd99de9..b1833b51e6d6 100644
@@ -91,14 +91,6 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
        fdt_setprop((fdt), (node_offset), (name), &(var), sizeof(var))
 #endif
 
-#define get_efi_var(name, vendor, ...)                         \
-       efi_rt_call(get_variable, (efi_char16_t *)(name),       \
-                   (efi_guid_t *)(vendor), __VA_ARGS__)
-
-#define set_efi_var(name, vendor, ...)                         \
-       efi_rt_call(set_variable, (efi_char16_t *)(name),       \
-                   (efi_guid_t *)(vendor), __VA_ARGS__)
-
 #define efi_get_handle_at(array, idx)                                  \
        (efi_is_native() ? (array)[idx]                                 \
                : (efi_handle_t)(unsigned long)((u32 *)(array))[idx])
@@ -112,6 +104,20 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
                ((handle = efi_get_handle_at((array), i)) || true);     \
             i++)
 
+static inline
+efi_status_t get_efi_var(efi_char16_t *name, efi_guid_t *vendor, u32 *attr,
+                        unsigned long *size, void *data)
+{
+       return efi_rt_call(get_variable, name, vendor, attr, size, data);
+}
+
+static inline
+efi_status_t set_efi_var(efi_char16_t *name, efi_guid_t *vendor, u32 attr,
+                        unsigned long size, void *data)
+{
+       return efi_rt_call(set_variable, name, vendor, attr, size, data);
+}
+
 static inline
 void efi_set_u64_split(u64 data, u32 *lo, u32 *hi)
 {
drivers/firmware/efi/libstub/secureboot.c deleted +0 -0
deleted file mode 100644
index 5efc524b14be..000000000000

Please keep this file (see below)

Quoted 53 lines
drivers/firmware/efi/libstub/secureboot.c deleted +0 -52
@@ -1,76 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-/*
- * Secure boot handling.
- *
- * Copyright (C) 2013,2014 Linaro Limited
- *     Roy Franz <[email protected]
- * Copyright (C) 2013 Red Hat, Inc.
- *     Mark Salter <[email protected]>
- */
-#include <linux/efi.h>
-#include <asm/efi.h>
-
-#include "efistub.h"
-
-/* BIOS variables */
-static const efi_guid_t efi_variable_guid = EFI_GLOBAL_VARIABLE_GUID;
-static const efi_char16_t efi_SecureBoot_name[] = L"SecureBoot";
-static const efi_char16_t efi_SetupMode_name[] = L"SetupMode";
-
-/* SHIM variables */
-static const efi_guid_t shim_guid = EFI_SHIM_LOCK_GUID;
-static const efi_char16_t shim_MokSBState_name[] = L"MokSBState";
-
-/*
- * Determine whether we're in secure boot mode.
- *
- * Please keep the logic in sync with
- * arch/x86/xen/efi.c:xen_efi_get_secureboot().
- */
-enum efi_secureboot_mode efi_get_secureboot(void)
-{
-       u32 attr;
-       u8 secboot, setupmode, moksbstate;
-       unsigned long size;
-       efi_status_t status;
-
-       size = sizeof(secboot);
-       status = get_efi_var(efi_SecureBoot_name, &efi_variable_guid,
-                            NULL, &size, &secboot);
-       if (status == EFI_NOT_FOUND)
-               return efi_secureboot_mode_disabled;
-       if (status != EFI_SUCCESS)
-               goto out_efi_err;
-
-       size = sizeof(setupmode);
-       status = get_efi_var(efi_SetupMode_name, &efi_variable_guid,
-                            NULL, &size, &setupmode);
-       if (status != EFI_SUCCESS)
-               goto out_efi_err;
-
-       if (secboot == 0 || setupmode == 1)
-               return efi_secureboot_mode_disabled;

drivers/firmware/efi/libstub/secureboot.c line 62

Quoted 10 lines
drivers/firmware/efi/libstub/secureboot.c deleted +0 -10
-
-       /*
-        * See if a user has put the shim into insecure mode. If so, and if the
-        * variable doesn't have the runtime attribute set, we might as well
-        * honor that.
-        */
-       size = sizeof(moksbstate);
-       status = get_efi_var(shim_MokSBState_name, &shim_guid,
-                            &attr, &size, &moksbstate);
-

MokSBState is a boot time variable, so we cannot access it when running under the OS. Xen also has a code flow similar to this one, but it looks at MokSbStateRt instead (which may be a mistake but let's forget about that for now)

So what we will need to do is factor out only the top part of this function (which, incidentally, is the only part that IMA uses in the first place)

Quoted 4 lines
drivers/firmware/efi/libstub/secureboot.c deleted +0 -4
-       /* If it fails, we don't care why. Default to secure */
-       if (status != EFI_SUCCESS)
-               goto secure_boot_enabled;
-       if (!(attr & EFI_VARIABLE_RUNTIME_ACCESS) && moksbstate == 1)

drivers/firmware/efi/libstub/secureboot.c line 76

Quoted 10 lines
drivers/firmware/efi/libstub/secureboot.c deleted +0 -10
-               return efi_secureboot_mode_disabled;
-
-secure_boot_enabled:
-       efi_info("UEFI Secure Boot is enabled.\n");
-       return efi_secureboot_mode_enabled;
-
-out_efi_err:
-       efi_err("Could not determine UEFI Secure Boot status.\n");
-       return efi_secureboot_mode_unknown;
-}

So let's keep this file, and also, let's put a wrapper function around get_efi_var() here, of which you can take the address and pass to the static inline function.

Quoted 38 lines
drivers/firmware/efi/libstub/x86-stub.c +1 -1
index 3672539cb96e..3f9b492c566b 100644
@@ -781,7 +781,7 @@ unsigned long efi_main(efi_handle_t handle,
         * otherwise we ask the BIOS.
         */
        if (boot_params->secure_boot == efi_secureboot_mode_unset)
-               boot_params->secure_boot = efi_get_secureboot();
+               boot_params->secure_boot = efi_get_secureboot(get_efi_var);
 
        /* Ask the firmware to clear memory on unclean shutdown */
        efi_enable_reset_attack_mitigation();
include/linux/efi.h +16 -1
index d7c0e73af2b9..cc2d3de39031 100644
@@ -1089,7 +1089,46 @@ enum efi_secureboot_mode {
        efi_secureboot_mode_disabled,
        efi_secureboot_mode_enabled,
 };
-enum efi_secureboot_mode efi_get_secureboot(void);
+
+static inline enum efi_secureboot_mode efi_get_secureboot(efi_get_variable_t *get_var)
+{
+       efi_guid_t var_guid = EFI_GLOBAL_VARIABLE_GUID;
+       efi_guid_t shim_guid = EFI_SHIM_LOCK_GUID;
+       efi_status_t status;
+       unsigned long size;
+       u8 secboot, setupmode, moksbstate;
+       u32 attr;
+
+       size = sizeof(secboot);
+       status = get_var(L"SecureBoot", &var_guid, NULL, &size, &secboot);
+
+       if (status == EFI_NOT_FOUND)
+               return efi_secureboot_mode_disabled;
+       if (status != EFI_SUCCESS)

include/linux/efi.h enum efi_secureboot_mode { · line 1117

Quoted 10 lines
include/linux/efi.h +10 -0
+               return efi_secureboot_mode_unknown;
+
+       size = sizeof(setupmode);
+       status = get_var(L"SetupMode", &var_guid, NULL, &size, &setupmode);
+
+       if (status != EFI_SUCCESS)
+               return efi_secureboot_mode_unknown;
+       if (secboot == 0 || setupmode == 1)
+               return efi_secureboot_mode_disabled;
+

So keep until here and move the rest back into the .c file

Quoted 18 lines
include/linux/efi.h +14 -1
+       /*
+        * See if a user has put the shim into insecure mode. If so, and if the
+        * variable doesn't have the runtime attribute set, we might as well
+        * honor that.
+        */
+       size = sizeof(moksbstate);
+       status = get_var(L"MokSBState", &shim_guid, &attr, &size, &moksbstate);
+       /* If it fails, we don't care why. Default to secure */
+       if (status == EFI_SUCCESS && moksbstate == 1
+           && !(attr & EFI_VARIABLE_RUNTIME_ACCESS))
+               return efi_secureboot_mode_disabled;
+
+       return efi_secureboot_mode_enabled;
+}
 
 #ifdef CONFIG_RESET_ATTACK_MITIGATION
 void efi_enable_reset_attack_mitigation(void);
--
Quoted 2 lines

2.28.0

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#3 lore raw

Re: [PATCH v3 1/3] efi: generalize efi_get_secureboot

Hi Ard,

Thanks for your time and reviewing.

Quoted 163 lines · Ard Biesheuvel

Hello Chester,

Thanks again for looking into this.

drivers/firmware/efi/libstub/secureboot.c

Quoted 86 lines · Chester Lin

Generalize the efi_get_secureboot() function so not only efistub but also other subsystems can use it.

---
 drivers/firmware/efi/libstub/Makefile     |  2 +-
 drivers/firmware/efi/libstub/efi-stub.c   |  2 +-
 drivers/firmware/efi/libstub/efistub.h    | 22 ++++---
 drivers/firmware/efi/libstub/secureboot.c | 76 -----------------------
 drivers/firmware/efi/libstub/x86-stub.c   |  2 +-
 include/linux/efi.h                       | 41 +++++++++++-
 6 files changed, 57 insertions(+), 88 deletions(-)
 delete mode 100644 drivers/firmware/efi/libstub/secureboot.c
drivers/firmware/efi/libstub/Makefile +1 -1
index 8a94388e38b3..88e47b0ca09d 100644
@@ -49,7 +49,7 @@ OBJECT_FILES_NON_STANDARD     := y
 # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in.
 KCOV_INSTRUMENT                        := n
 
-lib-y                          := efi-stub-helper.o gop.o secureboot.o tpm.o \
+lib-y                          := efi-stub-helper.o gop.o tpm.o \
                                   file.o mem.o random.o randomalloc.o pci.o \
                                   skip_spaces.o lib-cmdline.o lib-ctype.o \
                                   alignedmem.o relocate.o vsprintf.o
drivers/firmware/efi/libstub/efi-stub.c +1 -1
index 914a343c7785..ad96f1d786a9 100644
@@ -196,7 +196,7 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
        /* Ask the firmware to clear memory on unclean shutdown */
        efi_enable_reset_attack_mitigation();
 
-       secure_boot = efi_get_secureboot();
+       secure_boot = efi_get_secureboot(get_efi_var);
 
        /*
         * Unauthenticated device tree data is a security hazard, so ignore
drivers/firmware/efi/libstub/efistub.h +14 -8
index 2d7abcd99de9..b1833b51e6d6 100644
@@ -91,14 +91,6 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
        fdt_setprop((fdt), (node_offset), (name), &(var), sizeof(var))
 #endif
 
-#define get_efi_var(name, vendor, ...)                         \
-       efi_rt_call(get_variable, (efi_char16_t *)(name),       \
-                   (efi_guid_t *)(vendor), __VA_ARGS__)
-
-#define set_efi_var(name, vendor, ...)                         \
-       efi_rt_call(set_variable, (efi_char16_t *)(name),       \
-                   (efi_guid_t *)(vendor), __VA_ARGS__)
-
 #define efi_get_handle_at(array, idx)                                  \
        (efi_is_native() ? (array)[idx]                                 \
                : (efi_handle_t)(unsigned long)((u32 *)(array))[idx])
@@ -112,6 +104,20 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
                ((handle = efi_get_handle_at((array), i)) || true);     \
             i++)
 
+static inline
+efi_status_t get_efi_var(efi_char16_t *name, efi_guid_t *vendor, u32 *attr,
+                        unsigned long *size, void *data)
+{
+       return efi_rt_call(get_variable, name, vendor, attr, size, data);
+}
+
+static inline
+efi_status_t set_efi_var(efi_char16_t *name, efi_guid_t *vendor, u32 attr,
+                        unsigned long size, void *data)
+{
+       return efi_rt_call(set_variable, name, vendor, attr, size, data);
+}
+
 static inline
 void efi_set_u64_split(u64 data, u32 *lo, u32 *hi)
 {
drivers/firmware/efi/libstub/secureboot.c deleted +0 -0
deleted file mode 100644
index 5efc524b14be..000000000000

Please keep this file (see below)

Quoted 53 lines
drivers/firmware/efi/libstub/secureboot.c deleted +0 -52
@@ -1,76 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-/*
- * Secure boot handling.
- *
- * Copyright (C) 2013,2014 Linaro Limited
- *     Roy Franz <[email protected]
- * Copyright (C) 2013 Red Hat, Inc.
- *     Mark Salter <[email protected]>
- */
-#include <linux/efi.h>
-#include <asm/efi.h>
-
-#include "efistub.h"
-
-/* BIOS variables */
-static const efi_guid_t efi_variable_guid = EFI_GLOBAL_VARIABLE_GUID;
-static const efi_char16_t efi_SecureBoot_name[] = L"SecureBoot";
-static const efi_char16_t efi_SetupMode_name[] = L"SetupMode";
-
-/* SHIM variables */
-static const efi_guid_t shim_guid = EFI_SHIM_LOCK_GUID;
-static const efi_char16_t shim_MokSBState_name[] = L"MokSBState";
-
-/*
- * Determine whether we're in secure boot mode.
- *
- * Please keep the logic in sync with
- * arch/x86/xen/efi.c:xen_efi_get_secureboot().
- */
-enum efi_secureboot_mode efi_get_secureboot(void)
-{
-       u32 attr;
-       u8 secboot, setupmode, moksbstate;
-       unsigned long size;
-       efi_status_t status;
-
-       size = sizeof(secboot);
-       status = get_efi_var(efi_SecureBoot_name, &efi_variable_guid,
-                            NULL, &size, &secboot);
-       if (status == EFI_NOT_FOUND)
-               return efi_secureboot_mode_disabled;
-       if (status != EFI_SUCCESS)
-               goto out_efi_err;
-
-       size = sizeof(setupmode);
-       status = get_efi_var(efi_SetupMode_name, &efi_variable_guid,
-                            NULL, &size, &setupmode);
-       if (status != EFI_SUCCESS)
-               goto out_efi_err;
-
-       if (secboot == 0 || setupmode == 1)
-               return efi_secureboot_mode_disabled;

drivers/firmware/efi/libstub/secureboot.c line 62

Quoted 10 lines
drivers/firmware/efi/libstub/secureboot.c deleted +0 -10
-
-       /*
-        * See if a user has put the shim into insecure mode. If so, and if the
-        * variable doesn't have the runtime attribute set, we might as well
-        * honor that.
-        */
-       size = sizeof(moksbstate);
-       status = get_efi_var(shim_MokSBState_name, &shim_guid,
-                            &attr, &size, &moksbstate);
-

MokSBState is a boot time variable, so we cannot access it when running under the OS. Xen also has a code flow similar to this one, but it looks at MokSbStateRt instead (which may be a mistake but let's forget about that for now)

Quoted 2 lines

So what we will need to do is factor out only the top part of this function (which, incidentally, is the only part that IMA uses in the

i> first place)

Quoted 1 line

Thanks for the reminder. I will take this change into next revision.

Quoted 10 lines
Quoted 10 lines
drivers/firmware/efi/libstub/secureboot.c deleted +0 -10
-       /* If it fails, we don't care why. Default to secure */
-       if (status != EFI_SUCCESS)
-               goto secure_boot_enabled;
-       if (!(attr & EFI_VARIABLE_RUNTIME_ACCESS) && moksbstate == 1)
-               return efi_secureboot_mode_disabled;
-
-secure_boot_enabled:
-       efi_info("UEFI Secure Boot is enabled.\n");
-       return efi_secureboot_mode_enabled;
-
Quoted 8 lines

drivers/firmware/efi/libstub/secureboot.c line 76

Quoted 4 lines
drivers/firmware/efi/libstub/secureboot.c deleted +0 -4
-out_efi_err:
-       efi_err("Could not determine UEFI Secure Boot status.\n");
-       return efi_secureboot_mode_unknown;
-}

So let's keep this file, and also, let's put a wrapper function around get_efi_var() here, of which you can take the address and pass to the static inline function.

If I understand correctly, that means it's better to define a new wrapper function around the get_efi_var() rather than changing it from a macro to an inline function. Please feel free to let me know if I misunderstand it.

Quoted 73 lines
Quoted 39 lines
drivers/firmware/efi/libstub/x86-stub.c +1 -1
index 3672539cb96e..3f9b492c566b 100644
@@ -781,7 +781,7 @@ unsigned long efi_main(efi_handle_t handle,
         * otherwise we ask the BIOS.
         */
        if (boot_params->secure_boot == efi_secureboot_mode_unset)
-               boot_params->secure_boot = efi_get_secureboot();
+               boot_params->secure_boot = efi_get_secureboot(get_efi_var);
 
        /* Ask the firmware to clear memory on unclean shutdown */
        efi_enable_reset_attack_mitigation();
include/linux/efi.h +16 -1
index d7c0e73af2b9..cc2d3de39031 100644
@@ -1089,7 +1089,46 @@ enum efi_secureboot_mode {
        efi_secureboot_mode_disabled,
        efi_secureboot_mode_enabled,
 };
-enum efi_secureboot_mode efi_get_secureboot(void);
+
+static inline enum efi_secureboot_mode efi_get_secureboot(efi_get_variable_t *get_var)
+{
+       efi_guid_t var_guid = EFI_GLOBAL_VARIABLE_GUID;
+       efi_guid_t shim_guid = EFI_SHIM_LOCK_GUID;
+       efi_status_t status;
+       unsigned long size;
+       u8 secboot, setupmode, moksbstate;
+       u32 attr;
+
+       size = sizeof(secboot);
+       status = get_var(L"SecureBoot", &var_guid, NULL, &size, &secboot);
+
+       if (status == EFI_NOT_FOUND)
+               return efi_secureboot_mode_disabled;
+       if (status != EFI_SUCCESS)

include/linux/efi.h enum efi_secureboot_mode { · line 1117

Quoted 10 lines
include/linux/efi.h +10 -0
+               return efi_secureboot_mode_unknown;
+
+       size = sizeof(setupmode);
+       status = get_var(L"SetupMode", &var_guid, NULL, &size, &setupmode);
+
+       if (status != EFI_SUCCESS)
+               return efi_secureboot_mode_unknown;
+       if (secboot == 0 || setupmode == 1)
+               return efi_secureboot_mode_disabled;
+

So keep until here and move the rest back into the .c file

Quoted 20 lines
include/linux/efi.h +14 -1
+       /*
+        * See if a user has put the shim into insecure mode. If so, and if the
+        * variable doesn't have the runtime attribute set, we might as well
+        * honor that.
+        */
+       size = sizeof(moksbstate);
+       status = get_var(L"MokSBState", &shim_guid, &attr, &size, &moksbstate);
+       /* If it fails, we don't care why. Default to secure */
+       if (status == EFI_SUCCESS && moksbstate == 1
+           && !(attr & EFI_VARIABLE_RUNTIME_ACCESS))
+               return efi_secureboot_mode_disabled;
+
+       return efi_secureboot_mode_enabled;
+}
 
 #ifdef CONFIG_RESET_ATTACK_MITIGATION
 void efi_enable_reset_attack_mitigation(void);
--

2.28.0

#4 lore raw

Re: [PATCH v3 1/3] efi: generalize efi_get_secureboot

In reply to Chester Lin
Thank you for the patch! Perhaps something to improve:

Hi Chester,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on efi/next]
[also build test WARNING on arm64/for-next/core integrity/next-integrity v5.10-rc1 next-20201030] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Chester-Lin/add-ima_arch-support-for-ARM64/20201030-141043
base:   https://git.kernel.org/pub/scm/linux/kernel/git/efi/efi.git next
config: i386-randconfig-c001-20201030 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0
reproduce (this is a W=1 build):
        # https://github.com/0day-ci/linux/commit/945001ead079043268e8ad1b9d1df9bd5cabf020
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Chester-Lin/add-ima_arch-support-for-ARM64/20201030-141043
        git checkout 945001ead079043268e8ad1b9d1df9bd5cabf020
        # save the attached .config to linux build tree
        make W=1 ARCH=i386 

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

In file included from arch/x86/boot/compressed/acpi.c:8:
include/linux/efi.h: In function 'efi_get_secureboot':

>> include/linux/efi.h:1103:19: warning: passing argument 1 of 'get_var' from incompatible pointer type [-Wincompatible-pointer-types]
    1103 |  status = get_var(L"SecureBoot", &var_guid, NULL, &size, &secboot);
         |                   ^~~~~~~~~~~~~
         |                   |
         |                   long int *
   include/linux/efi.h:1103:19: note: expected 'efi_char16_t *' {aka 'short unsigned int *'} but argument is of type 'long int *'
   include/linux/efi.h:1111:19: warning: passing argument 1 of 'get_var' from incompatible pointer type [-Wincompatible-pointer-types]
    1111 |  status = get_var(L"SetupMode", &var_guid, NULL, &size, &setupmode);
         |                   ^~~~~~~~~~~~
         |                   |
         |                   long int *
   include/linux/efi.h:1111:19: note: expected 'efi_char16_t *' {aka 'short unsigned int *'} but argument is of type 'long int *'
   include/linux/efi.h:1124:19: warning: passing argument 1 of 'get_var' from incompatible pointer type [-Wincompatible-pointer-types]
    1124 |  status = get_var(L"MokSBState", &shim_guid, &attr, &size, &moksbstate);
         |                   ^~~~~~~~~~~~~
         |                   |
         |                   long int *

include/linux/efi.h:1124:19: note: expected 'efi_char16_t *' {aka 'short unsigned int *'} but argument is of type 'long int *'

vim +/get_var +1103 include/linux/efi.h

1092
1093 static inline enum efi_secureboot_mode efi_get_secureboot(efi_get_variable_t *get_var)
1094 {
1095 efi_guid_t var_guid = EFI_GLOBAL_VARIABLE_GUID;
1096 efi_guid_t shim_guid = EFI_SHIM_LOCK_GUID;
1097 efi_status_t status;
1098 unsigned long size;
1099 u8 secboot, setupmode, moksbstate;
1100 u32 attr;
1101
1102 size = sizeof(secboot);

> 1103		status = get_var(L"SecureBoot", &var_guid, NULL, &size, &secboot);

1104
1105 if (status == EFI_NOT_FOUND)
1106 return efi_secureboot_mode_disabled;
1107 if (status != EFI_SUCCESS)
1108 return efi_secureboot_mode_unknown;
1109
1110 size = sizeof(setupmode);
1111 status = get_var(L"SetupMode", &var_guid, NULL, &size, &setupmode);
1112
1113 if (status != EFI_SUCCESS)
1114 return efi_secureboot_mode_unknown;
1115 if (secboot == 0 || setupmode == 1)
1116 return efi_secureboot_mode_disabled;
1117
1118 /*
1119 * See if a user has put the shim into insecure mode. If so, and if the
1120 * variable doesn't have the runtime attribute set, we might as well
1121 * honor that.
1122 */
1123 size = sizeof(moksbstate);
1124 status = get_var(L"MokSBState", &shim_guid, &attr, &size, &moksbstate);
1125 /* If it fails, we don't care why. Default to secure */
1126 if (status == EFI_SUCCESS && moksbstate == 1
1127 && !(attr & EFI_VARIABLE_RUNTIME_ACCESS))
1128 return efi_secureboot_mode_disabled;
1129
1130 return efi_secureboot_mode_enabled;
1131 }
1132

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#5 lore raw

Re: [PATCH v3 1/3] efi: generalize efi_get_secureboot

In reply to Chester Lin
Thank you for the patch! Perhaps something to improve:

Hi Chester,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on efi/next]
[also build test WARNING on arm64/for-next/core integrity/next-integrity v5.10-rc2 next-20201102] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Chester-Lin/add-ima_arch-support-for-ARM64/20201030-141043
base:   https://git.kernel.org/pub/scm/linux/kernel/git/efi/efi.git next
config: i386-allyesconfig (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0
reproduce (this is a W=1 build):
        # https://github.com/0day-ci/linux/commit/945001ead079043268e8ad1b9d1df9bd5cabf020
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Chester-Lin/add-ima_arch-support-for-ARM64/20201030-141043
        git checkout 945001ead079043268e8ad1b9d1df9bd5cabf020
        # save the attached .config to linux build tree
        make W=1 ARCH=i386 

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

drivers/firmware/efi/libstub/tpm.c: In function 'efi_enable_reset_attack_mitigation':

>> drivers/firmware/efi/libstub/tpm.c:36:23: warning: passing argument 1 of 'get_efi_var' discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
      36 |  status = get_efi_var(efi_MemoryOverWriteRequest_name, &var_guid,
         |                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   In file included from drivers/firmware/efi/libstub/tpm.c:14:
   drivers/firmware/efi/libstub/efistub.h:108:40: note: expected 'efi_char16_t *' {aka 'short unsigned int *'} but argument is of type 'const efi_char16_t *' {aka 'const short unsigned int *'}
     108 | efi_status_t get_efi_var(efi_char16_t *name, efi_guid_t *vendor, u32 *attr,
         |                          ~~~~~~~~~~~~~~^~~~
>> drivers/firmware/efi/libstub/tpm.c:42:14: warning: passing argument 1 of 'set_efi_var' discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
      42 |  set_efi_var(efi_MemoryOverWriteRequest_name, &var_guid,
         |              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   In file included from drivers/firmware/efi/libstub/tpm.c:14:
   drivers/firmware/efi/libstub/efistub.h:115:40: note: expected 'efi_char16_t *' {aka 'short unsigned int *'} but argument is of type 'const efi_char16_t *' {aka 'const short unsigned int *'}
     115 | efi_status_t set_efi_var(efi_char16_t *name, efi_guid_t *vendor, u32 attr,
         |                          ~~~~~~~~~~~~~~^~~~

vim +36 drivers/firmware/efi/libstub/tpm.c

ccc829ba3624beb Matthew Garrett   2017-08-25  15  
33b6d03469b2206 Thiebaud Weksteen 2017-09-20  16  #ifdef CONFIG_RESET_ATTACK_MITIGATION
36b649760e94968 Ard Biesheuvel    2018-03-12  17  static const efi_char16_t efi_MemoryOverWriteRequest_name[] =
36b649760e94968 Ard Biesheuvel    2018-03-12  18  	L"MemoryOverwriteRequestControl";
ccc829ba3624beb Matthew Garrett   2017-08-25  19  
ccc829ba3624beb Matthew Garrett   2017-08-25  20  #define MEMORY_ONLY_RESET_CONTROL_GUID \
ccc829ba3624beb Matthew Garrett   2017-08-25  21  	EFI_GUID(0xe20939be, 0x32d4, 0x41be, 0xa1, 0x50, 0x89, 0x7f, 0x85, 0xd4, 0x98, 0x29)
ccc829ba3624beb Matthew Garrett   2017-08-25  22  
ccc829ba3624beb Matthew Garrett   2017-08-25  23  /*
ccc829ba3624beb Matthew Garrett   2017-08-25  24   * Enable reboot attack mitigation. This requests that the firmware clear the
ccc829ba3624beb Matthew Garrett   2017-08-25  25   * RAM on next reboot before proceeding with boot, ensuring that any secrets
ccc829ba3624beb Matthew Garrett   2017-08-25  26   * are cleared. If userland has ensured that all secrets have been removed
ccc829ba3624beb Matthew Garrett   2017-08-25  27   * from RAM before reboot it can simply reset this variable.
ccc829ba3624beb Matthew Garrett   2017-08-25  28   */
cd33a5c1d53e43b Ard Biesheuvel    2019-12-24  29  void efi_enable_reset_attack_mitigation(void)
ccc829ba3624beb Matthew Garrett   2017-08-25  30  {
ccc829ba3624beb Matthew Garrett   2017-08-25  31  	u8 val = 1;
ccc829ba3624beb Matthew Garrett   2017-08-25  32  	efi_guid_t var_guid = MEMORY_ONLY_RESET_CONTROL_GUID;
ccc829ba3624beb Matthew Garrett   2017-08-25  33  	efi_status_t status;
ccc829ba3624beb Matthew Garrett   2017-08-25  34  	unsigned long datasize = 0;
ccc829ba3624beb Matthew Garrett   2017-08-25  35  
ccc829ba3624beb Matthew Garrett   2017-08-25 @36  	status = get_efi_var(efi_MemoryOverWriteRequest_name, &var_guid,
ccc829ba3624beb Matthew Garrett   2017-08-25  37  			     NULL, &datasize, NULL);
ccc829ba3624beb Matthew Garrett   2017-08-25  38  
ccc829ba3624beb Matthew Garrett   2017-08-25  39  	if (status == EFI_NOT_FOUND)
ccc829ba3624beb Matthew Garrett   2017-08-25  40  		return;
ccc829ba3624beb Matthew Garrett   2017-08-25  41  
ccc829ba3624beb Matthew Garrett   2017-08-25 @42  	set_efi_var(efi_MemoryOverWriteRequest_name, &var_guid,
ccc829ba3624beb Matthew Garrett   2017-08-25  43  		    EFI_VARIABLE_NON_VOLATILE |
ccc829ba3624beb Matthew Garrett   2017-08-25  44  		    EFI_VARIABLE_BOOTSERVICE_ACCESS |
ccc829ba3624beb Matthew Garrett   2017-08-25  45  		    EFI_VARIABLE_RUNTIME_ACCESS, sizeof(val), &val);
ccc829ba3624beb Matthew Garrett   2017-08-25  46  }

33b6d03469b2206 Thiebaud Weksteen 2017-09-20 47

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#6 lore raw

Re: [PATCH v3 1/3] efi: generalize efi_get_secureboot

In reply to Chester Lin
Thank you for the patch! Perhaps something to improve:

Hi Chester,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on efi/next]
[also build test WARNING on arm64/for-next/core integrity/next-integrity v5.10-rc2 next-20201103] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Chester-Lin/add-ima_arch-support-for-ARM64/20201030-141043
base:   https://git.kernel.org/pub/scm/linux/kernel/git/efi/efi.git next
config: i386-randconfig-s031-20201103 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0
reproduce:
        # apt-get install sparse
        # sparse version: v0.6.3-76-gf680124b-dirty
        # https://github.com/0day-ci/linux/commit/945001ead079043268e8ad1b9d1df9bd5cabf020
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Chester-Lin/add-ima_arch-support-for-ARM64/20201030-141043
        git checkout 945001ead079043268e8ad1b9d1df9bd5cabf020
        # save the attached .config to linux build tree
        make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' ARCH=i386 

If you fix the issue, kindly add following tag as appropriate

"sparse warnings: (new ones prefixed by >>)"

>> drivers/firmware/efi/libstub/tpm.c:36:30: sparse: sparse: incorrect type in argument 1 (different modifiers) @@     expected unsigned short [usertype] *name @@     got unsigned short const * @@
>> drivers/firmware/efi/libstub/tpm.c:36:30: sparse:     expected unsigned short [usertype] *name
>> drivers/firmware/efi/libstub/tpm.c:36:30: sparse:     got unsigned short const *

drivers/firmware/efi/libstub/tpm.c:42:21: sparse: sparse: incorrect type in argument 1 (different modifiers) @@ expected unsigned short [usertype] *name @@ got unsigned short const * @@
drivers/firmware/efi/libstub/tpm.c:42:21: sparse: expected unsigned short [usertype] *name
drivers/firmware/efi/libstub/tpm.c:42:21: sparse: got unsigned short const *

vim +36 drivers/firmware/efi/libstub/tpm.c

ccc829ba3624beb Matthew Garrett   2017-08-25  15  
33b6d03469b2206 Thiebaud Weksteen 2017-09-20  16  #ifdef CONFIG_RESET_ATTACK_MITIGATION
36b649760e94968 Ard Biesheuvel    2018-03-12  17  static const efi_char16_t efi_MemoryOverWriteRequest_name[] =
36b649760e94968 Ard Biesheuvel    2018-03-12  18  	L"MemoryOverwriteRequestControl";
ccc829ba3624beb Matthew Garrett   2017-08-25  19  
ccc829ba3624beb Matthew Garrett   2017-08-25  20  #define MEMORY_ONLY_RESET_CONTROL_GUID \
ccc829ba3624beb Matthew Garrett   2017-08-25  21  	EFI_GUID(0xe20939be, 0x32d4, 0x41be, 0xa1, 0x50, 0x89, 0x7f, 0x85, 0xd4, 0x98, 0x29)
ccc829ba3624beb Matthew Garrett   2017-08-25  22  
ccc829ba3624beb Matthew Garrett   2017-08-25  23  /*
ccc829ba3624beb Matthew Garrett   2017-08-25  24   * Enable reboot attack mitigation. This requests that the firmware clear the
ccc829ba3624beb Matthew Garrett   2017-08-25  25   * RAM on next reboot before proceeding with boot, ensuring that any secrets
ccc829ba3624beb Matthew Garrett   2017-08-25  26   * are cleared. If userland has ensured that all secrets have been removed
ccc829ba3624beb Matthew Garrett   2017-08-25  27   * from RAM before reboot it can simply reset this variable.
ccc829ba3624beb Matthew Garrett   2017-08-25  28   */
cd33a5c1d53e43b Ard Biesheuvel    2019-12-24  29  void efi_enable_reset_attack_mitigation(void)
ccc829ba3624beb Matthew Garrett   2017-08-25  30  {
ccc829ba3624beb Matthew Garrett   2017-08-25  31  	u8 val = 1;
ccc829ba3624beb Matthew Garrett   2017-08-25  32  	efi_guid_t var_guid = MEMORY_ONLY_RESET_CONTROL_GUID;
ccc829ba3624beb Matthew Garrett   2017-08-25  33  	efi_status_t status;
ccc829ba3624beb Matthew Garrett   2017-08-25  34  	unsigned long datasize = 0;
ccc829ba3624beb Matthew Garrett   2017-08-25  35  
ccc829ba3624beb Matthew Garrett   2017-08-25 @36  	status = get_efi_var(efi_MemoryOverWriteRequest_name, &var_guid,
ccc829ba3624beb Matthew Garrett   2017-08-25  37  			     NULL, &datasize, NULL);
ccc829ba3624beb Matthew Garrett   2017-08-25  38  
ccc829ba3624beb Matthew Garrett   2017-08-25  39  	if (status == EFI_NOT_FOUND)
ccc829ba3624beb Matthew Garrett   2017-08-25  40  		return;
ccc829ba3624beb Matthew Garrett   2017-08-25  41  
ccc829ba3624beb Matthew Garrett   2017-08-25  42  	set_efi_var(efi_MemoryOverWriteRequest_name, &var_guid,
ccc829ba3624beb Matthew Garrett   2017-08-25  43  		    EFI_VARIABLE_NON_VOLATILE |
ccc829ba3624beb Matthew Garrett   2017-08-25  44  		    EFI_VARIABLE_BOOTSERVICE_ACCESS |
ccc829ba3624beb Matthew Garrett   2017-08-25  45  		    EFI_VARIABLE_RUNTIME_ACCESS, sizeof(val), &val);
ccc829ba3624beb Matthew Garrett   2017-08-25  46  }

33b6d03469b2206 Thiebaud Weksteen 2017-09-20 47

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#7 lore raw

PATCH v3 2/3 ima: replace arch-specific get_sb_mode() with a common helper ima_get_efi_secureboot()

In reply to Chester Lin

remove the get_sb_mode() from x86/kernel/ima_arch.c and create a common helper ima_get_efi_secureboot() in IMA so that all EFI-based architectures can refer to the same procedure.

---
 arch/x86/kernel/ima_arch.c       | 69 +++++++-------------------------
 include/linux/ima.h              | 10 +++++
 security/integrity/ima/Makefile  |  1 +
 security/integrity/ima/ima_efi.c | 26 ++++++++++++
 4 files changed, 51 insertions(+), 55 deletions(-)
 create mode 100644 security/integrity/ima/ima_efi.c
arch/x86/kernel/ima_arch.c +14 -55
index 7dfb1e808928..2c773532ff0a 100644
@@ -8,69 +8,28 @@
 
 extern struct boot_params boot_params;
 
-static enum efi_secureboot_mode get_sb_mode(void)
-{
-	efi_guid_t efi_variable_guid = EFI_GLOBAL_VARIABLE_GUID;
-	efi_status_t status;
-	unsigned long size;
-	u8 secboot, setupmode;
-
-	size = sizeof(secboot);
-
-	if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE)) {
-		pr_info("ima: secureboot mode unknown, no efi\n");
-		return efi_secureboot_mode_unknown;
-	}
-
-	/* Get variable contents into buffer */
-	status = efi.get_variable(L"SecureBoot", &efi_variable_guid,
-				  NULL, &size, &secboot);
-	if (status == EFI_NOT_FOUND) {
-		pr_info("ima: secureboot mode disabled\n");
-		return efi_secureboot_mode_disabled;
-	}
-
-	if (status != EFI_SUCCESS) {
-		pr_info("ima: secureboot mode unknown\n");
-		return efi_secureboot_mode_unknown;
-	}
-
-	size = sizeof(setupmode);
-	status = efi.get_variable(L"SetupMode", &efi_variable_guid,
-				  NULL, &size, &setupmode);
-
-	if (status != EFI_SUCCESS)	/* ignore unknown SetupMode */
-		setupmode = 0;
-
-	if (secboot == 0 || setupmode == 1) {
-		pr_info("ima: secureboot mode disabled\n");
-		return efi_secureboot_mode_disabled;
-	}
-
-	pr_info("ima: secureboot mode enabled\n");
-	return efi_secureboot_mode_enabled;
-}
-
 bool arch_ima_get_secureboot(void)
 {
-	static enum efi_secureboot_mode sb_mode;
-	static bool initialized;
-
-	if (!initialized && efi_enabled(EFI_BOOT)) {
-		sb_mode = boot_params.secure_boot;
+	static bool sb_enabled, initialized;
 
-		if (sb_mode == efi_secureboot_mode_unset)
-			sb_mode = get_sb_mode();
+	if (initialized) {
+		return sb_enabled;
+	} else if (efi_enabled(EFI_BOOT)) {
 		initialized = true;
+
+		if (boot_params.secure_boot == efi_secureboot_mode_unset) {
+			sb_enabled = ima_get_efi_secureboot();
+			return sb_enabled;
+		}
 	}
 
-	if (sb_mode == efi_secureboot_mode_enabled)
-		return true;
-	else
-		return false;
+	if (boot_params.secure_boot == efi_secureboot_mode_enabled)
+		sb_enabled = true;
+
+	return sb_enabled;
 }
 
-/* secureboot arch rules */
+/* secure and trusted boot arch rules */
 static const char * const sb_arch_rules[] = {
 #if !IS_ENABLED(CONFIG_KEXEC_SIG)
 	"appraise func=KEXEC_KERNEL_CHECK appraise_type=imasig",
include/linux/ima.h +10 -0
index 8fa7bcfb2da2..9f9699f017be 100644
@@ -50,6 +50,16 @@ static inline const char * const *arch_get_ima_policy(void)
 }
 #endif
 
+#if defined(CONFIG_EFI) && defined(CONFIG_IMA_SECURE_AND_OR_TRUSTED_BOOT)
+extern bool ima_get_efi_secureboot(void);
+#else
+static inline bool ima_get_efi_secureboot(void)
+{
+	return false;
+}
+#endif
+
+
 #else
 static inline int ima_bprm_check(struct linux_binprm *bprm)
 {
security/integrity/ima/Makefile +1 -0
index 67dabca670e2..32076b3fd292 100644
@@ -14,3 +14,4 @@ ima-$(CONFIG_HAVE_IMA_KEXEC) += ima_kexec.o
 ima-$(CONFIG_IMA_BLACKLIST_KEYRING) += ima_mok.o
 ima-$(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) += ima_asymmetric_keys.o
 ima-$(CONFIG_IMA_QUEUE_EARLY_BOOT_KEYS) += ima_queue_keys.o
+ima-$(CONFIG_EFI) += ima_efi.o
security/integrity/ima/ima_efi.c new file +26 -0
new file mode 100644
index 000000000000..a78f66e19689
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2020 SUSE LLC
+ *
+ * Author:
+ * Chester Lin <[email protected]>
+ */
+
+#include <linux/efi.h>
+#include <linux/ima.h>
+
+#ifdef CONFIG_IMA_SECURE_AND_OR_TRUSTED_BOOT
+bool ima_get_efi_secureboot(void)
+{
+	enum efi_secureboot_mode sb_mode;
+
+	if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE)) {
+		pr_info("ima: secureboot mode unknown, no efi\n");
+		return false;
+	}
+
+	sb_mode = efi_get_secureboot(efi.get_variable);
+
+	return (sb_mode == efi_secureboot_mode_enabled) ? true : false;
+}
+#endif
Signature · 1 line
2.28.0
maintainer
#8 lore raw

Re: [PATCH v3 2/3] ima: replace arch-specific get_sb_mode() with a common helper ima_get_efi_secureboot()

Quoted 151 lines · Chester Lin

remove the get_sb_mode() from x86/kernel/ima_arch.c and create a common helper ima_get_efi_secureboot() in IMA so that all EFI-based architectures can refer to the same procedure.

---
 arch/x86/kernel/ima_arch.c       | 69 +++++++-------------------------
 include/linux/ima.h              | 10 +++++
 security/integrity/ima/Makefile  |  1 +
 security/integrity/ima/ima_efi.c | 26 ++++++++++++
 4 files changed, 51 insertions(+), 55 deletions(-)
 create mode 100644 security/integrity/ima/ima_efi.c
arch/x86/kernel/ima_arch.c +14 -55
index 7dfb1e808928..2c773532ff0a 100644
@@ -8,69 +8,28 @@
 
 extern struct boot_params boot_params;
 
-static enum efi_secureboot_mode get_sb_mode(void)
-{
-       efi_guid_t efi_variable_guid = EFI_GLOBAL_VARIABLE_GUID;
-       efi_status_t status;
-       unsigned long size;
-       u8 secboot, setupmode;
-
-       size = sizeof(secboot);
-
-       if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE)) {
-               pr_info("ima: secureboot mode unknown, no efi\n");
-               return efi_secureboot_mode_unknown;
-       }
-
-       /* Get variable contents into buffer */
-       status = efi.get_variable(L"SecureBoot", &efi_variable_guid,
-                                 NULL, &size, &secboot);
-       if (status == EFI_NOT_FOUND) {
-               pr_info("ima: secureboot mode disabled\n");
-               return efi_secureboot_mode_disabled;
-       }
-
-       if (status != EFI_SUCCESS) {
-               pr_info("ima: secureboot mode unknown\n");
-               return efi_secureboot_mode_unknown;
-       }
-
-       size = sizeof(setupmode);
-       status = efi.get_variable(L"SetupMode", &efi_variable_guid,
-                                 NULL, &size, &setupmode);
-
-       if (status != EFI_SUCCESS)      /* ignore unknown SetupMode */
-               setupmode = 0;
-
-       if (secboot == 0 || setupmode == 1) {
-               pr_info("ima: secureboot mode disabled\n");
-               return efi_secureboot_mode_disabled;
-       }
-
-       pr_info("ima: secureboot mode enabled\n");
-       return efi_secureboot_mode_enabled;
-}
-
 bool arch_ima_get_secureboot(void)
 {
-       static enum efi_secureboot_mode sb_mode;
-       static bool initialized;
-
-       if (!initialized && efi_enabled(EFI_BOOT)) {
-               sb_mode = boot_params.secure_boot;
+       static bool sb_enabled, initialized;
 
-               if (sb_mode == efi_secureboot_mode_unset)
-                       sb_mode = get_sb_mode();
+       if (initialized) {
+               return sb_enabled;
+       } else if (efi_enabled(EFI_BOOT)) {
                initialized = true;
+
+               if (boot_params.secure_boot == efi_secureboot_mode_unset) {
+                       sb_enabled = ima_get_efi_secureboot();
+                       return sb_enabled;
+               }
        }
 
-       if (sb_mode == efi_secureboot_mode_enabled)
-               return true;
-       else
-               return false;
+       if (boot_params.secure_boot == efi_secureboot_mode_enabled)
+               sb_enabled = true;
+
+       return sb_enabled;
 }
 
-/* secureboot arch rules */
+/* secure and trusted boot arch rules */
 static const char * const sb_arch_rules[] = {
 #if !IS_ENABLED(CONFIG_KEXEC_SIG)
        "appraise func=KEXEC_KERNEL_CHECK appraise_type=imasig",
include/linux/ima.h +10 -0
index 8fa7bcfb2da2..9f9699f017be 100644
@@ -50,6 +50,16 @@ static inline const char * const *arch_get_ima_policy(void)
 }
 #endif
 
+#if defined(CONFIG_EFI) && defined(CONFIG_IMA_SECURE_AND_OR_TRUSTED_BOOT)
+extern bool ima_get_efi_secureboot(void);
+#else
+static inline bool ima_get_efi_secureboot(void)
+{
+       return false;
+}
+#endif
+
+
 #else
 static inline int ima_bprm_check(struct linux_binprm *bprm)
 {
security/integrity/ima/Makefile +1 -0
index 67dabca670e2..32076b3fd292 100644
@@ -14,3 +14,4 @@ ima-$(CONFIG_HAVE_IMA_KEXEC) += ima_kexec.o
 ima-$(CONFIG_IMA_BLACKLIST_KEYRING) += ima_mok.o
 ima-$(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) += ima_asymmetric_keys.o
 ima-$(CONFIG_IMA_QUEUE_EARLY_BOOT_KEYS) += ima_queue_keys.o
+ima-$(CONFIG_EFI) += ima_efi.o
security/integrity/ima/ima_efi.c new file +13 -0
new file mode 100644
index 000000000000..a78f66e19689
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2020 SUSE LLC
+ *
+ * Author:
+ * Chester Lin <[email protected]>
+ */
+
+#include <linux/efi.h>
+#include <linux/ima.h>
+
+#ifdef CONFIG_IMA_SECURE_AND_OR_TRUSTED_BOOT
+bool ima_get_efi_secureboot(void)

security/integrity/ima/ima_efi.c line 23

Quoted 10 lines
security/integrity/ima/ima_efi.c new file +10 -0
+{
+       enum efi_secureboot_mode sb_mode;
+
+       if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE)) {
+               pr_info("ima: secureboot mode unknown, no efi\n");
+               return false;
+       }
+
+       sb_mode = efi_get_secureboot(efi.get_variable);
+

As I mentioned in the other patch, these are not equivalent - you are introducing a MokSbState check which doesn't make sense at runtime (or at all perhaps)

Quoted 6 lines
security/integrity/ima/ima_efi.c new file +3 -1
+       return (sb_mode == efi_secureboot_mode_enabled) ? true : false;
+}
+#endif
--

2.28.0

#9 lore raw

PATCH v3 3/3 arm64/ima: add ima_arch support

In reply to Chester Lin

Add arm64 IMA arch support. The code and arch policy is mainly inherited from x86.

---
 arch/arm64/Kconfig           |  1 +
 arch/arm64/kernel/Makefile   |  2 ++
 arch/arm64/kernel/ima_arch.c | 43 ++++++++++++++++++++++++++++++++++++
 3 files changed, 46 insertions(+)
 create mode 100644 arch/arm64/kernel/ima_arch.c
arch/arm64/Kconfig +1 -0
index a42e8d13cc88..496a4a26afc6 100644
@@ -201,6 +201,7 @@ config ARM64
 	select SWIOTLB
 	select SYSCTL_EXCEPTION_TRACE
 	select THREAD_INFO_IN_TASK
+	imply IMA_SECURE_AND_OR_TRUSTED_BOOT if EFI
 	help
 	  ARM 64-bit (AArch64) Linux support.
 
arch/arm64/kernel/Makefile +2 -0
index bbaf0bc4ad60..0f6cbb50668c 100644
@@ -69,3 +69,5 @@ extra-y					+= $(head-y) vmlinux.lds
 ifeq ($(CONFIG_DEBUG_EFI),y)
 AFLAGS_head.o += -DVMLINUX_PATH="\"$(realpath $(objtree)/vmlinux)\""
 endif
+
+obj-$(CONFIG_IMA_SECURE_AND_OR_TRUSTED_BOOT)	+= ima_arch.o
arch/arm64/kernel/ima_arch.c new file +43 -0

43 lines not on this page · raw message

Signature · 1 line
2.28.0
maintainer
#10 lore raw

Re: [PATCH v3 3/3] arm64/ima: add ima_arch support

Quoted 84 lines · Chester Lin

Add arm64 IMA arch support. The code and arch policy is mainly inherited from x86.

---
 arch/arm64/Kconfig           |  1 +
 arch/arm64/kernel/Makefile   |  2 ++
 arch/arm64/kernel/ima_arch.c | 43 ++++++++++++++++++++++++++++++++++++
 3 files changed, 46 insertions(+)
 create mode 100644 arch/arm64/kernel/ima_arch.c
arch/arm64/Kconfig +1 -0
index a42e8d13cc88..496a4a26afc6 100644
@@ -201,6 +201,7 @@ config ARM64
        select SWIOTLB
        select SYSCTL_EXCEPTION_TRACE
        select THREAD_INFO_IN_TASK
+       imply IMA_SECURE_AND_OR_TRUSTED_BOOT if EFI
        help
          ARM 64-bit (AArch64) Linux support.
 
arch/arm64/kernel/Makefile +2 -0
index bbaf0bc4ad60..0f6cbb50668c 100644
@@ -69,3 +69,5 @@ extra-y                                       += $(head-y) vmlinux.lds
 ifeq ($(CONFIG_DEBUG_EFI),y)
 AFLAGS_head.o += -DVMLINUX_PATH="\"$(realpath $(objtree)/vmlinux)\""
 endif
+
+obj-$(CONFIG_IMA_SECURE_AND_OR_TRUSTED_BOOT)   += ima_arch.o
arch/arm64/kernel/ima_arch.c new file +43 -1
new file mode 100644
index 000000000000..564236d77adc
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2018 IBM Corporation
+ */
+#include <linux/efi.h>
+#include <linux/module.h>
+#include <linux/ima.h>
+
+bool arch_ima_get_secureboot(void)
+{
+       static bool sb_enabled;
+       static bool initialized;
+
+       if (!initialized & efi_enabled(EFI_BOOT)) {
+               sb_enabled = ima_get_efi_secureboot();
+               initialized = true;
+       }
+
+       return sb_enabled;
+}
+
+/* secure and trusted boot arch rules */
+static const char * const sb_arch_rules[] = {
+#if !IS_ENABLED(CONFIG_KEXEC_SIG)
+       "appraise func=KEXEC_KERNEL_CHECK appraise_type=imasig",
+#endif /* CONFIG_KEXEC_SIG */
+       "measure func=KEXEC_KERNEL_CHECK",
+#if !IS_ENABLED(CONFIG_MODULE_SIG)
+       "appraise func=MODULE_CHECK appraise_type=imasig",
+#endif
+       "measure func=MODULE_CHECK",
+       NULL
+};
+
+const char * const *arch_get_ima_policy(void)
+{
+       if (IS_ENABLED(CONFIG_IMA_ARCH_POLICY) && arch_ima_get_secureboot()) {
+               if (IS_ENABLED(CONFIG_MODULE_SIG))
+                       set_module_sig_enforced();
+               return sb_arch_rules;
+       }
+       return NULL;
+}
--
Quoted 2 lines

2.28.0

Can we move all this stuff into security/integrity/ima/ima_efi.c instead?

#11 lore raw

Re: [PATCH v3 3/3] arm64/ima: add ima_arch support

Quoted 81 lines · Ard Biesheuvel
Quoted 80 lines · Chester Lin

Add arm64 IMA arch support. The code and arch policy is mainly inherited from x86.

---
 arch/arm64/Kconfig           |  1 +
 arch/arm64/kernel/Makefile   |  2 ++
 arch/arm64/kernel/ima_arch.c | 43 ++++++++++++++++++++++++++++++++++++
 3 files changed, 46 insertions(+)
 create mode 100644 arch/arm64/kernel/ima_arch.c
arch/arm64/Kconfig +1 -0
index a42e8d13cc88..496a4a26afc6 100644
@@ -201,6 +201,7 @@ config ARM64
        select SWIOTLB
        select SYSCTL_EXCEPTION_TRACE
        select THREAD_INFO_IN_TASK
+       imply IMA_SECURE_AND_OR_TRUSTED_BOOT if EFI
        help
          ARM 64-bit (AArch64) Linux support.
 
arch/arm64/kernel/Makefile +2 -0
index bbaf0bc4ad60..0f6cbb50668c 100644
@@ -69,3 +69,5 @@ extra-y                                       += $(head-y) vmlinux.lds
 ifeq ($(CONFIG_DEBUG_EFI),y)
 AFLAGS_head.o += -DVMLINUX_PATH="\"$(realpath $(objtree)/vmlinux)\""
 endif
+
+obj-$(CONFIG_IMA_SECURE_AND_OR_TRUSTED_BOOT)   += ima_arch.o
arch/arm64/kernel/ima_arch.c new file +40 -0
new file mode 100644
index 000000000000..564236d77adc
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2018 IBM Corporation
+ */
+#include <linux/efi.h>
+#include <linux/module.h>
+#include <linux/ima.h>
+
+bool arch_ima_get_secureboot(void)
+{
+       static bool sb_enabled;
+       static bool initialized;
+
+       if (!initialized & efi_enabled(EFI_BOOT)) {
+               sb_enabled = ima_get_efi_secureboot();
+               initialized = true;
+       }
+
+       return sb_enabled;
+}
+
+/* secure and trusted boot arch rules */
+static const char * const sb_arch_rules[] = {
+#if !IS_ENABLED(CONFIG_KEXEC_SIG)
+       "appraise func=KEXEC_KERNEL_CHECK appraise_type=imasig",
+#endif /* CONFIG_KEXEC_SIG */
+       "measure func=KEXEC_KERNEL_CHECK",
+#if !IS_ENABLED(CONFIG_MODULE_SIG)
+       "appraise func=MODULE_CHECK appraise_type=imasig",
+#endif
+       "measure func=MODULE_CHECK",
+       NULL
+};
+
+const char * const *arch_get_ima_policy(void)
+{
+       if (IS_ENABLED(CONFIG_IMA_ARCH_POLICY) && arch_ima_get_secureboot()) {
+               if (IS_ENABLED(CONFIG_MODULE_SIG))
+                       set_module_sig_enforced();
+               return sb_arch_rules;
Quoted 9 lines
Quoted 6 lines
arch/arm64/kernel/ima_arch.c new file +3 -1
+       }
+       return NULL;
+}
--

2.28.0

Can we move all this stuff into security/integrity/ima/ima_efi.c instead?

Actually I hesitated to move all this stuff into ima_efi.c when coding v3 because I haven't figured out a clear picture to achieve it. Since each architecture could still have different details to trigger secure boot detection and define their arch-specific rules [e.g. Having boot_params in x86_64 creates more conditions that need to be determined before calling get_sb_mode()], moving all this stuff seems to decrease the flexibility. Besides, it might also affect the consistency of ima_arch as well, for example, ppc and s390 still use these function prototypes defined in ima.h. Since these functions are already referred by non-EFI architectures, why don't we still reuse these prototypes? For example, we could remain a smaller arch_ima_get_secureboot() and the arch-specific rules but move the major part of arch_get_ima_policy() into ima_efi.c. For example, we could implement an efi_ima_policy() for arch_get_ima_policy() to call so that the arch_get_ima_policy() doesn't have to know some details such as checking conditions or calling set_module_sig_enforced().

Please feel free to let me know if any suggestions.

maintainer
#12 lore raw

Re: [PATCH v3 3/3] arm64/ima: add ima_arch support

Quoted 106 lines · Chester Lin
Quoted 81 lines · Ard Biesheuvel
Quoted 80 lines · Chester Lin

Add arm64 IMA arch support. The code and arch policy is mainly inherited from x86.

---
 arch/arm64/Kconfig           |  1 +
 arch/arm64/kernel/Makefile   |  2 ++
 arch/arm64/kernel/ima_arch.c | 43 ++++++++++++++++++++++++++++++++++++
 3 files changed, 46 insertions(+)
 create mode 100644 arch/arm64/kernel/ima_arch.c
arch/arm64/Kconfig +1 -0
index a42e8d13cc88..496a4a26afc6 100644
@@ -201,6 +201,7 @@ config ARM64
        select SWIOTLB
        select SYSCTL_EXCEPTION_TRACE
        select THREAD_INFO_IN_TASK
+       imply IMA_SECURE_AND_OR_TRUSTED_BOOT if EFI
        help
          ARM 64-bit (AArch64) Linux support.
 
arch/arm64/kernel/Makefile +2 -0
index bbaf0bc4ad60..0f6cbb50668c 100644
@@ -69,3 +69,5 @@ extra-y                                       += $(head-y) vmlinux.lds
 ifeq ($(CONFIG_DEBUG_EFI),y)
 AFLAGS_head.o += -DVMLINUX_PATH="\"$(realpath $(objtree)/vmlinux)\""
 endif
+
+obj-$(CONFIG_IMA_SECURE_AND_OR_TRUSTED_BOOT)   += ima_arch.o
arch/arm64/kernel/ima_arch.c new file +40 -0
new file mode 100644
index 000000000000..564236d77adc
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2018 IBM Corporation
+ */
+#include <linux/efi.h>
+#include <linux/module.h>
+#include <linux/ima.h>
+
+bool arch_ima_get_secureboot(void)
+{
+       static bool sb_enabled;
+       static bool initialized;
+
+       if (!initialized & efi_enabled(EFI_BOOT)) {
+               sb_enabled = ima_get_efi_secureboot();
+               initialized = true;
+       }
+
+       return sb_enabled;
+}
+
+/* secure and trusted boot arch rules */
+static const char * const sb_arch_rules[] = {
+#if !IS_ENABLED(CONFIG_KEXEC_SIG)
+       "appraise func=KEXEC_KERNEL_CHECK appraise_type=imasig",
+#endif /* CONFIG_KEXEC_SIG */
+       "measure func=KEXEC_KERNEL_CHECK",
+#if !IS_ENABLED(CONFIG_MODULE_SIG)
+       "appraise func=MODULE_CHECK appraise_type=imasig",
+#endif
+       "measure func=MODULE_CHECK",
+       NULL
+};
+
+const char * const *arch_get_ima_policy(void)
+{
+       if (IS_ENABLED(CONFIG_IMA_ARCH_POLICY) && arch_ima_get_secureboot()) {
+               if (IS_ENABLED(CONFIG_MODULE_SIG))
+                       set_module_sig_enforced();
+               return sb_arch_rules;
Quoted 9 lines
Quoted 6 lines
arch/arm64/kernel/ima_arch.c new file +3 -1
+       }
+       return NULL;
+}
--

2.28.0

Can we move all this stuff into security/integrity/ima/ima_efi.c instead?

Actually I hesitated to move all this stuff into ima_efi.c when coding v3 because I haven't figured out a clear picture to achieve it. Since each architecture could still have different details to trigger secure boot detection and define their arch-specific rules [e.g. Having boot_params in x86_64 creates more conditions that need to be determined before calling get_sb_mode()], moving all this stuff seems to decrease the flexibility. Besides, it might also affect the consistency of ima_arch as well, for example, ppc and s390 still use these function prototypes defined in ima.h. Since these functions are already referred by non-EFI architectures, why don't we still reuse these prototypes? For example, we could remain a smaller arch_ima_get_secureboot() and the arch-specific rules but move the major part of arch_get_ima_policy() into ima_efi.c. For example, we could implement an efi_ima_policy() for arch_get_ima_policy() to call so that the arch_get_ima_policy() doesn't have to know some details such as checking conditions or calling set_module_sig_enforced().

Quoted 1 line

Please feel free to let me know if any suggestions.

Yes, that is the point and the reason for defining ima_efi.c and conditionally including it only for EFI systems. The existing ppc and s390 code should remain unaffected by this change.

thanks,

Mimi