From patchwork Mon Nov 2 22:37:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 11875483 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 637F292C for ; Mon, 2 Nov 2020 22:38:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 48B9B223BF for ; Mon, 2 Nov 2020 22:38:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604356706; bh=W6puH/ry+3qo06s1xpa3gn2nlx7yJ1jJ2qy1z+5RQKo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=r20AUAN0HWDYeLVTwp1ZJtAyab0Ida5mShZGRpcW/7VjVswcMsSBG857AV4/u1q8C rOyKkKdAa7U9scAOvjOse4R8GFRX4KgpHOESJvJ17gjfI7ruyiGvO+nR6yy3QLGJXU G+0Evo2N4tDFkZIuQn/iV6fSZVBBsZCh6IrPTgSY= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726126AbgKBWi0 (ORCPT ); Mon, 2 Nov 2020 17:38:26 -0500 Received: from mail.kernel.org ([198.145.29.99]:55236 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726109AbgKBWiZ (ORCPT ); Mon, 2 Nov 2020 17:38:25 -0500 Received: from e123331-lin.nice.arm.com (lfbn-nic-1-188-42.w2-15.abo.wanadoo.fr [2.15.37.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 14EEC22384; Mon, 2 Nov 2020 22:38:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604356704; bh=W6puH/ry+3qo06s1xpa3gn2nlx7yJ1jJ2qy1z+5RQKo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CCskhuPZ+cYFf+ExURczE6E1XnYSL1h7baPT5oi9FwHXRGt4sASBs/Zz79KTB5/hK et4gbTN+a8oxI0yZo12brEg2hALvJ9Q3bJ54/yN96/QqR+aL3kjrDP3NsqfNQMB+RS 3D3Ajekj7unLylICLIHETflTKnd/6w/FxHHVOs5M= From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: Ard Biesheuvel , zohar@linux.ibm.com, jmorris@namei.org, serge@hallyn.com, dmitry.kasatkin@gmail.com, catalin.marinas@arm.com, will@kernel.org, clin@suse.com, x86@kernel.org, jlee@suse.com, linux-integrity@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH v4 1/3] efi: generalize efi_get_secureboot Date: Mon, 2 Nov 2020 23:37:58 +0100 Message-Id: <20201102223800.12181-2-ardb@kernel.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20201102223800.12181-1-ardb@kernel.org> References: <20201102223800.12181-1-ardb@kernel.org> Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org From: Chester Lin 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 Signed-off-by: Chester Lin Signed-off-by: Ard Biesheuvel --- 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(-) 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); From patchwork Mon Nov 2 22:37:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 11875485 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 22F341130 for ; Mon, 2 Nov 2020 22:38:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0446122280 for ; Mon, 2 Nov 2020 22:38:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604356710; bh=jd0lc7xktVJyaT/5sIC7f3m8eRs2yFAtQQ04Ih829IY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=plOu2eO2demPG9uwY+UFySrQEqU3IxPCS3Hyn54dCRvzBMDwwAPPHk/8VV7sYC4KL uWlUHor5xU0HKt1bE8U6dbKASGcF+p2pSbqL1IvebS3NSLKSVQoBtWOfu283huvfHT DZa5QMB0Ry8+8AN5B57l2GoNTe2/QvwRTCMgxCQ4= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726361AbgKBWi3 (ORCPT ); Mon, 2 Nov 2020 17:38:29 -0500 Received: from mail.kernel.org ([198.145.29.99]:55294 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725993AbgKBWi3 (ORCPT ); Mon, 2 Nov 2020 17:38:29 -0500 Received: from e123331-lin.nice.arm.com (lfbn-nic-1-188-42.w2-15.abo.wanadoo.fr [2.15.37.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 4C657223BD; Mon, 2 Nov 2020 22:38:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604356708; bh=jd0lc7xktVJyaT/5sIC7f3m8eRs2yFAtQQ04Ih829IY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IchjoCRxZbA3ne8F7g7b654KXF+ZkWB8urgEdXLMh0b2IGqdm5GjtSgqKT22Sd0rE YjNVvJwsGkdQDIaBMxr0DPrF+gK8SXv85xrZR0BMPevMYtFK95JqMW9eG0GLJ9GJD3 KADjwzNTU6f5tB596kdj+VyWJuPfcTP7ZxR7VvYQ= From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: Ard Biesheuvel , zohar@linux.ibm.com, jmorris@namei.org, serge@hallyn.com, dmitry.kasatkin@gmail.com, catalin.marinas@arm.com, will@kernel.org, clin@suse.com, x86@kernel.org, jlee@suse.com, linux-integrity@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH v4 2/3] ima: generalize x86/EFI arch glue for other EFI architectures Date: Mon, 2 Nov 2020 23:37:59 +0100 Message-Id: <20201102223800.12181-3-ardb@kernel.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20201102223800.12181-1-ardb@kernel.org> References: <20201102223800.12181-1-ardb@kernel.org> Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org From: Chester Lin Move the x86 IMA arch code into security/integrity/ima/ima_efi.c, so that we will be able to wire it up for arm64 in a future patch. Co-developed-by: Chester Lin Signed-off-by: Chester Lin Signed-off-by: Ard Biesheuvel --- arch/x86/include/asm/efi.h | 3 ++ arch/x86/kernel/Makefile | 2 - security/integrity/ima/Makefile | 4 ++ arch/x86/kernel/ima_arch.c => security/integrity/ima/ima_efi.c | 45 ++++++-------------- 4 files changed, 19 insertions(+), 35 deletions(-) diff --git a/arch/x86/include/asm/efi.h b/arch/x86/include/asm/efi.h index 7673dc833232..c98f78330b09 100644 --- a/arch/x86/include/asm/efi.h +++ b/arch/x86/include/asm/efi.h @@ -380,4 +380,7 @@ static inline void efi_fake_memmap_early(void) } #endif +#define arch_ima_efi_boot_mode \ + ({ extern struct boot_params boot_params; boot_params.secure_boot; }) + #endif /* _ASM_X86_EFI_H */ diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile index 68608bd892c0..5eeb808eb024 100644 --- a/arch/x86/kernel/Makefile +++ b/arch/x86/kernel/Makefile @@ -161,5 +161,3 @@ ifeq ($(CONFIG_X86_64),y) obj-$(CONFIG_MMCONF_FAM10H) += mmconf-fam10h_64.o obj-y += vsmp_64.o endif - -obj-$(CONFIG_IMA_SECURE_AND_OR_TRUSTED_BOOT) += ima_arch.o diff --git a/security/integrity/ima/Makefile b/security/integrity/ima/Makefile index 67dabca670e2..2499f2485c04 100644 --- a/security/integrity/ima/Makefile +++ b/security/integrity/ima/Makefile @@ -14,3 +14,7 @@ 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 + +ifeq ($(CONFIG_EFI),y) +ima-$(CONFIG_IMA_SECURE_AND_OR_TRUSTED_BOOT) += ima_efi.o +endif diff --git a/arch/x86/kernel/ima_arch.c b/security/integrity/ima/ima_efi.c similarity index 60% rename from arch/x86/kernel/ima_arch.c rename to security/integrity/ima/ima_efi.c index 7dfb1e808928..233627a9d4b8 100644 --- a/arch/x86/kernel/ima_arch.c +++ b/security/integrity/ima/ima_efi.c @@ -5,50 +5,29 @@ #include #include #include +#include -extern struct boot_params boot_params; +#ifndef arch_ima_efi_boot_mode +#define arch_ima_efi_boot_mode efi_secureboot_mode_unknown +#endif 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); + enum efi_secureboot_mode mode; 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) { + mode = efi_get_secureboot_mode(efi.get_variable); + if (mode == efi_secureboot_mode_disabled) pr_info("ima: secureboot mode disabled\n"); - return efi_secureboot_mode_disabled; - } - - if (status != EFI_SUCCESS) { + else if (mode == efi_secureboot_mode_unknown) 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; + else + pr_info("ima: secureboot mode enabled\n"); + return mode; } bool arch_ima_get_secureboot(void) @@ -57,7 +36,7 @@ bool arch_ima_get_secureboot(void) static bool initialized; if (!initialized && efi_enabled(EFI_BOOT)) { - sb_mode = boot_params.secure_boot; + sb_mode = arch_ima_efi_boot_mode; if (sb_mode == efi_secureboot_mode_unset) sb_mode = get_sb_mode(); From patchwork Mon Nov 2 22:38:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 11875487 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BC0081130 for ; Mon, 2 Nov 2020 22:38:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9D7C722280 for ; Mon, 2 Nov 2020 22:38:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604356712; bh=KVk3UfB7fHvlt+4j/Zyd5nzE1y9+9mnY2YhO3zMimQo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=zgGmuF5FatHgg5Obw4Uv57b7BSzDpgn9kigJom8yjPtwElrxPrSXkwVm5Pl6hsOZl Ld/Ic+qbKNMnid9VUj+bu5ZlQCNwohKW837ylsDuZZxdbx8cG9JD2NOreczYIKu46d gC2R4e8BMbyS+vBkELSM9CRYR0HUM5CUubDGo39U= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726561AbgKBWic (ORCPT ); Mon, 2 Nov 2020 17:38:32 -0500 Received: from mail.kernel.org ([198.145.29.99]:55350 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726388AbgKBWic (ORCPT ); Mon, 2 Nov 2020 17:38:32 -0500 Received: from e123331-lin.nice.arm.com (lfbn-nic-1-188-42.w2-15.abo.wanadoo.fr [2.15.37.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id CDC5820870; Mon, 2 Nov 2020 22:38:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604356711; bh=KVk3UfB7fHvlt+4j/Zyd5nzE1y9+9mnY2YhO3zMimQo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hxtt0ti6RiEIR1iAyKE9/phzwtWG0D4yC7rCIMiFhrH+cry1E+GMVlqgFBgfPKsjd V4BGj3Qefv7UQ5bu3aD17aNDhYa0I4WZB6gYvjT5sH5P/3JJVPYXqGQ3Wx6CbxikOo VdNz9W2QyYOdN92DZlDcDMqTVDJVMYJs71XZfy1c= From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: Ard Biesheuvel , zohar@linux.ibm.com, jmorris@namei.org, serge@hallyn.com, dmitry.kasatkin@gmail.com, catalin.marinas@arm.com, will@kernel.org, clin@suse.com, x86@kernel.org, jlee@suse.com, linux-integrity@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH v4 3/3] arm64/ima: add ima_arch support Date: Mon, 2 Nov 2020 23:38:00 +0100 Message-Id: <20201102223800.12181-4-ardb@kernel.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20201102223800.12181-1-ardb@kernel.org> References: <20201102223800.12181-1-ardb@kernel.org> Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org From: Chester Lin Add arm64 IMA arch support. The code and arch policy is mainly inherited from x86. Co-developed-by: Chester Lin Signed-off-by: Chester Lin Signed-off-by: Ard Biesheuvel Acked-by: Catalin Marinas --- arch/arm64/Kconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index f858c352f72a..ea1b608a0fad 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -200,6 +200,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.