diff mbox series

[v4,1/3] efi: generalize efi_get_secureboot

Message ID 20201102223800.12181-2-ardb@kernel.org
State Accepted
Commit e1ac4b2406d94eddce8ac2c5ab4235f6075a9602
Headers show
Series [v4,1/3] efi: generalize efi_get_secureboot | expand

Commit Message

Ard Biesheuvel Nov. 2, 2020, 10:37 p.m. UTC
From: Chester Lin <clin@suse.com>

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

Note that the MokSbState handling is not factored out: the variable is
boot time only, and so it cannot be parameterized as easily. Also, the
IMA code will switch to this version in a future patch, and it does not
incorporate the MokSbState exception in the first place.

Note that the new efi_get_secureboot_mode() helper treats any failures
to read SetupMode as setup mode being disabled.

Co-developed-by: Chester Lin <clin@suse.com>
Signed-off-by: Chester Lin <clin@suse.com>
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 arch/x86/boot/compressed/Makefile         |  2 +-
 drivers/firmware/efi/libstub/efistub.h    |  2 +
 drivers/firmware/efi/libstub/secureboot.c | 41 +++++++-------------
 include/linux/efi.h                       | 23 ++++++++++-
 4 files changed, 40 insertions(+), 28 deletions(-)

Comments

Mimi Zohar Nov. 3, 2020, 6:48 p.m. UTC | #1
On Mon, 2020-11-02 at 23:37 +0100, Ard Biesheuvel wrote:
> From: Chester Lin <clin@suse.com>

> 

> Generalize the efi_get_secureboot() function so not only efistub but also

> other subsystems can use it.

> 

> Note that the MokSbState handling is not factored out: the variable is

> boot time only, and so it cannot be parameterized as easily. Also, the

> IMA code will switch to this version in a future patch, and it does not

> incorporate the MokSbState exception in the first place.

> 

> Note that the new efi_get_secureboot_mode() helper treats any failures

> to read SetupMode as setup mode being disabled.

> 

> Co-developed-by: Chester Lin <clin@suse.com>

> Signed-off-by: Chester Lin <clin@suse.com>

> Signed-off-by: Ard Biesheuvel <ardb@kernel.org>


Thanks, Ard.  Other than one minor thing inline below, the patch looks
good.  I haven't done any testing yet.

> diff --git a/include/linux/efi.h b/include/linux/efi.h

> index bd9d83a94173..79b2d4de62e0 100644

> --- a/include/linux/efi.h

> +++ b/include/linux/efi.h

> @@ -1082,7 +1082,28 @@ 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_mode(efi_get_variable_t *get_var)


get_var() should be defined as "efi_status_t".   If this is being
upstreamed via integrity, I can make the change.

thanks,

Mimi
Ard Biesheuvel Nov. 3, 2020, 7:01 p.m. UTC | #2
On Tue, 3 Nov 2020 at 19:49, Mimi Zohar <zohar@linux.ibm.com> wrote:
>

> On Mon, 2020-11-02 at 23:37 +0100, Ard Biesheuvel wrote:

> > From: Chester Lin <clin@suse.com>

> >

> > Generalize the efi_get_secureboot() function so not only efistub but also

> > other subsystems can use it.

> >

> > Note that the MokSbState handling is not factored out: the variable is

> > boot time only, and so it cannot be parameterized as easily. Also, the

> > IMA code will switch to this version in a future patch, and it does not

> > incorporate the MokSbState exception in the first place.

> >

> > Note that the new efi_get_secureboot_mode() helper treats any failures

> > to read SetupMode as setup mode being disabled.

> >

> > Co-developed-by: Chester Lin <clin@suse.com>

> > Signed-off-by: Chester Lin <clin@suse.com>

> > Signed-off-by: Ard Biesheuvel <ardb@kernel.org>

>

> Thanks, Ard.  Other than one minor thing inline below, the patch looks

> good.  I haven't done any testing yet.

>

> > diff --git a/include/linux/efi.h b/include/linux/efi.h

> > index bd9d83a94173..79b2d4de62e0 100644

> > --- a/include/linux/efi.h

> > +++ b/include/linux/efi.h

> > @@ -1082,7 +1082,28 @@ 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_mode(efi_get_variable_t *get_var)

>

> get_var() should be defined as "efi_status_t".   If this is being

> upstreamed via integrity, I can make the change.

>


No, get_var is a pointer to a function returning efi_status_t, check
include/linux/efi.h for details.
Mimi Zohar Nov. 3, 2020, 8:03 p.m. UTC | #3
On Tue, 2020-11-03 at 20:01 +0100, Ard Biesheuvel wrote:
> > get_var() should be defined as "efi_status_t".   If this is being

> > upstreamed via integrity, I can make the change.

> >

> 

> No, get_var is a pointer to a function returning efi_status_t, check

> include/linux/efi.h for details.


Got it.

thanks,

Mimi
diff mbox series

Patch

diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile
index ee249088cbfe..8d358a6fe6ec 100644
--- a/arch/x86/boot/compressed/Makefile
+++ b/arch/x86/boot/compressed/Makefile
@@ -35,7 +35,7 @@  cflags-$(CONFIG_X86_32) := -march=i386
 cflags-$(CONFIG_X86_64) := -mcmodel=small -mno-red-zone
 KBUILD_CFLAGS += $(cflags-y)
 KBUILD_CFLAGS += -mno-mmx -mno-sse
-KBUILD_CFLAGS += -ffreestanding
+KBUILD_CFLAGS += -ffreestanding -fshort-wchar
 KBUILD_CFLAGS += -fno-stack-protector
 KBUILD_CFLAGS += $(call cc-disable-warning, address-of-packed-member)
 KBUILD_CFLAGS += $(call cc-disable-warning, gnu)
diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h
index 2d7abcd99de9..b8ec29d6a74a 100644
--- a/drivers/firmware/efi/libstub/efistub.h
+++ b/drivers/firmware/efi/libstub/efistub.h
@@ -848,4 +848,6 @@  asmlinkage void __noreturn efi_enter_kernel(unsigned long entrypoint,
 
 void efi_handle_post_ebs_state(void);
 
+enum efi_secureboot_mode efi_get_secureboot(void);
+
 #endif
diff --git a/drivers/firmware/efi/libstub/secureboot.c b/drivers/firmware/efi/libstub/secureboot.c
index 5efc524b14be..af18d86c1604 100644
--- a/drivers/firmware/efi/libstub/secureboot.c
+++ b/drivers/firmware/efi/libstub/secureboot.c
@@ -12,15 +12,16 @@ 
 
 #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";
 
+static efi_status_t get_var(efi_char16_t *name, efi_guid_t *vendor, u32 *attr,
+			    unsigned long *data_size, void *data)
+{
+	return get_efi_var(name, vendor, attr, data_size, data);
+}
+
 /*
  * Determine whether we're in secure boot mode.
  *
@@ -30,26 +31,18 @@  static const efi_char16_t shim_MokSBState_name[] = L"MokSBState";
 enum efi_secureboot_mode efi_get_secureboot(void)
 {
 	u32 attr;
-	u8 secboot, setupmode, moksbstate;
 	unsigned long size;
+	enum efi_secureboot_mode mode;
 	efi_status_t status;
+	u8 moksbstate;
 
-	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;
+	mode = efi_get_secureboot_mode(get_var);
+	if (mode == efi_secureboot_mode_unknown) {
+		efi_err("Could not determine UEFI Secure Boot status.\n");
+		return efi_secureboot_mode_unknown;
+	}
+	if (mode != efi_secureboot_mode_enabled)
+		return mode;
 
 	/*
 	 * See if a user has put the shim into insecure mode. If so, and if the
@@ -69,8 +62,4 @@  enum efi_secureboot_mode efi_get_secureboot(void)
 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;
 }
diff --git a/include/linux/efi.h b/include/linux/efi.h
index bd9d83a94173..79b2d4de62e0 100644
--- a/include/linux/efi.h
+++ b/include/linux/efi.h
@@ -1082,7 +1082,28 @@  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_mode(efi_get_variable_t *get_var)
+{
+	u8 secboot, setupmode = 0;
+	efi_status_t status;
+	unsigned long size;
+
+	size = sizeof(secboot);
+	status = get_var(L"SecureBoot", &EFI_GLOBAL_VARIABLE_GUID, NULL, &size,
+			 &secboot);
+	if (status == EFI_NOT_FOUND)
+		return efi_secureboot_mode_disabled;
+	if (status != EFI_SUCCESS)
+		return efi_secureboot_mode_unknown;
+
+	size = sizeof(setupmode);
+	get_var(L"SetupMode", &EFI_GLOBAL_VARIABLE_GUID, NULL, &size, &setupmode);
+	if (secboot == 0 || setupmode == 1)
+		return efi_secureboot_mode_disabled;
+	return efi_secureboot_mode_enabled;
+}
 
 #ifdef CONFIG_RESET_ATTACK_MITIGATION
 void efi_enable_reset_attack_mitigation(void);