From patchwork Fri Nov 19 11:47:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilias Apalodimas X-Patchwork-Id: 518327 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1ADD4C433F5 for ; Fri, 19 Nov 2021 11:48:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EFB1B61AED for ; Fri, 19 Nov 2021 11:48:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231466AbhKSLvH (ORCPT ); Fri, 19 Nov 2021 06:51:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229687AbhKSLvH (ORCPT ); Fri, 19 Nov 2021 06:51:07 -0500 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E91EFC061574 for ; Fri, 19 Nov 2021 03:48:05 -0800 (PST) Received: by mail-wr1-x42f.google.com with SMTP id r8so17653970wra.7 for ; Fri, 19 Nov 2021 03:48:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=cBpcZaFhz4p6OdqDxzcgAE37Gskr5/xHfO3KYMeY7Y4=; b=ctApfwYeYAPY4vALr8d/IVqUOt0XguArk6Of2QK9RXZ9FYSG5bZYN9unmjb1kJ4a/r BSLR0hd8rXaDTKE9qjApU5a2W66xQeu4lcgpFNNoppfhyeGUbTzmdfw5VdhYXNRoUpt/ I66DVG7eulE5wsoQSYD+XsU6sulUFGn1qYCrss80wcH3rW9zs/0zhhIBvKTYgeSsCGVT jyPGSap1x0HDPi7o4rPvSPx4vx1nrDlcFjJVnJ9bj0G5/0sIApla0KKFdSrJLcOeEaEd H1ZWm8LvojcZW7K3WkHqUrmO3FYF6e/OSEWWZ04EQlPhldoZ9KV0QfgZFL4Qdn2eV+Dd EIlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=cBpcZaFhz4p6OdqDxzcgAE37Gskr5/xHfO3KYMeY7Y4=; b=zgGhOZaCt/fNP/kZjb9rcNxpt1stSzdu7CRVNqwPr1n8CXAQqwKfLymshkyy4fyZ37 g4NZWgNECmaxuLnaEJ+EA8Nyz9zGlgk1/Fa7CCrvL4jOHjGVHDqpGLiSIRoY4AmJ77z/ XlZokT29me8q4irw5nEMXcYvGmRUskvA2C0vLlbJ6gh+kL5gVod/bAHzvvPSqmfoQ1H2 MLq77rP3uiN2RPuQtbf+s8gGMbiOIyCOmwl4y+iWDmt/z+Vc5fLfRjipPAZO9Aqgb8Tj X681aMGz0A447QlLC1LneWNLdAswl/xjjOV4+sdzRQn/XbOLZnl5fzRHgfBVf/gQrfs+ IwiA== X-Gm-Message-State: AOAM53111Guuhmdx3gM7JB9kCCPcrIQk/dY2pkOG7Hw4glrlzb0WHC7f D7BDFqk9McRT8V8S4SphDsNFLapuOoqr1A== X-Google-Smtp-Source: ABdhPJzumenuqpyMcUAxEGwXwiGMbXePmzzTRrVpLlsDf8XAXcB/GiqQ/R4V+PKEcd+mqJSNnxd6Pw== X-Received: by 2002:adf:e2c5:: with SMTP id d5mr6652879wrj.338.1637322484409; Fri, 19 Nov 2021 03:48:04 -0800 (PST) Received: from apalos.home ([2a02:587:4627:18c0:2e56:dcff:fe9a:8f06]) by smtp.gmail.com with ESMTPSA id c4sm2688039wrr.37.2021.11.19.03.48.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Nov 2021 03:48:03 -0800 (PST) From: Ilias Apalodimas To: linux-efi@vger.kernel.org Cc: ardb@kernel.org, pjones@redhat.com, nivedita@alum.mit.edu, mjg59@google.com, daniel.kiper@oracle.com, James.Bottomley@hansenpartnership.com, leif@nuviainc.com, jroedel@suse.de, Ilias Apalodimas Subject: [PATCH 1/4 v3] efi/libstub: add prototype of efi_tcg2_protocol::hash_log_extend_event() Date: Fri, 19 Nov 2021 13:47:42 +0200 Message-Id: <20211119114745.1560453-2-ilias.apalodimas@linaro.org> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211119114745.1560453-1-ilias.apalodimas@linaro.org> References: <20211119114745.1560453-1-ilias.apalodimas@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org From: Ard Biesheuvel Define the right prototype for efi_tcg2_protocol::hash_log_extend_event() and add the required structs so we can start using it to measure the initrd into the TPM if it was loaded by the EFI stub itself. Signed-off-by: Ard Biesheuvel Signed-off-by: Ilias Apalodimas --- Changes since v1,2: - None Changes since Ard's RFC: - Added event_tag related definitions - added a missing __packed on efi_tcg2_event arch/x86/include/asm/efi.h | 4 ++++ drivers/firmware/efi/libstub/efistub.h | 29 +++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/efi.h b/arch/x86/include/asm/efi.h index 4d0b126835b8..85f156f8ef81 100644 --- a/arch/x86/include/asm/efi.h +++ b/arch/x86/include/asm/efi.h @@ -308,6 +308,10 @@ static inline u32 efi64_convert_status(efi_status_t status) #define __efi64_argmap_query_mode(gop, mode, size, info) \ ((gop), (mode), efi64_zero_upper(size), efi64_zero_upper(info)) +/* TCG2 protocol */ +#define __efi64_argmap_hash_log_extend_event(prot, fl, addr, size, ev) \ + ((prot), (fl), 0ULL, (u64)(addr), 0ULL, (u64)(size), 0ULL, ev) + /* * The macros below handle the plumbing for the argument mapping. To add a * mapping for a specific EFI method, simply define a macro diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h index cde0a2ef507d..a2825c435158 100644 --- a/drivers/firmware/efi/libstub/efistub.h +++ b/drivers/firmware/efi/libstub/efistub.h @@ -667,6 +667,29 @@ union apple_properties_protocol { typedef u32 efi_tcg2_event_log_format; +#define INITRD_EVENT_TAG_ID 0x8F3B22ECU +#define EV_EVENT_TAG 0x00000006U +#define EFI_TCG2_EVENT_HEADER_VERSION 0x1 + +struct efi_tcg2_event { + u32 event_size; + struct { + u32 header_size; + u16 header_version; + u32 pcr_index; + u32 event_type; + } __packed event_header; + /* u8[] event follows here */ +} __packed; + +struct efi_tcg2_tagged_event { + u32 tagged_event_id; + u32 tagged_event_data_size; + /* u8 tagged event data follows here */ +} __packed; + +typedef struct efi_tcg2_event efi_tcg2_event_t; +typedef struct efi_tcg2_tagged_event efi_tcg2_tagged_event_t; typedef union efi_tcg2_protocol efi_tcg2_protocol_t; union efi_tcg2_protocol { @@ -677,7 +700,11 @@ union efi_tcg2_protocol { efi_physical_addr_t *, efi_physical_addr_t *, efi_bool_t *); - void *hash_log_extend_event; + efi_status_t (__efiapi *hash_log_extend_event)(efi_tcg2_protocol_t *, + u64, + efi_physical_addr_t, + u64, + const efi_tcg2_event_t *); void *submit_command; void *get_active_pcr_banks; void *set_active_pcr_banks; From patchwork Fri Nov 19 11:47:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilias Apalodimas X-Patchwork-Id: 517035 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 58DC2C433FE for ; Fri, 19 Nov 2021 11:48:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3B0CC61AED for ; Fri, 19 Nov 2021 11:48:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231529AbhKSLvJ (ORCPT ); Fri, 19 Nov 2021 06:51:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50862 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229687AbhKSLvI (ORCPT ); Fri, 19 Nov 2021 06:51:08 -0500 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 35412C061574 for ; Fri, 19 Nov 2021 03:48:07 -0800 (PST) Received: by mail-wm1-x333.google.com with SMTP id z1-20020a05600c220100b00337f97d2464so7556090wml.1 for ; Fri, 19 Nov 2021 03:48:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=jzbLIXzxwn+BG1Jh+SPLy6v+awyEDiVu+TM/OwiCocQ=; b=mKUnIHVzxUVp4UyiIQp1qyf3qxycL8MU5enasac5ZEwd4qQz0fDgQEd/nyYOlbyoiJ qnkjl0wh/3WptNBi7M2fEq8thHktUOHKDSB/loQeaDuaZLJwHlTreg2Vgj89SD7LiuIn M23MRZzDXE1pmujjCFvmImBiUfsHDAcssgJpC44KF3julFAVAwgxL+cG5kbog0t9jp+G Wajwj/9C1Leuv1QPnVMAsBKSsCCXYTxIzq7hcjxKYnutPjcE4ION6EydatNBoVSvxAl9 1vI1QECH0pvY7NdqPgbML1J8pWTZDz9eEynQ6WfpB6dKHAH5v+X/LOp0qXM5ZNX7rXh1 Tv7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jzbLIXzxwn+BG1Jh+SPLy6v+awyEDiVu+TM/OwiCocQ=; b=xEUmYO2LZ15cEfqlCNi5pT1+9dR6OuyVxNCwwfTrNhSsrB9S/yJo8KYhYa7xkRAb6K xBbs9mgnRif9uZbpdg770B+nyTUSo7P0LadpZTfZ2KTfnUiDdJsV5/S2PwyO+PUlPJT6 +s473J/Y/VeJ0CpZKLH9ZoaUk0UF9wV8BgeXQa30yrezgHq0royC7FcVg8V04KBAwO0S VsVfe7KFayab94pbXyRWzibba3Qmzhf5EjD6w7iM7GMmmczBgxWLsn8HgKetbW/AEqE8 gBRgOeIMlncEITgWhvYa0VbvZPwVFnz63yOw6di3qRgh0DDmUZ+zahXyrtkt4D/P3cQz p9SQ== X-Gm-Message-State: AOAM532kzoXzPLQGu5fRrqjSH/n9il6PfJa9oQkttaXh25isI7yVQO4z YRva6Tt5y3LMvCt8yqdzcnFsLV4imOQ4aQ== X-Google-Smtp-Source: ABdhPJyb71IUvhUFpBMQ3BNGf/djJgCWtSf2iNcPwWBS7yIoXp1CV9QSb/OUYhAtuFGVGoKCRarrNA== X-Received: by 2002:a05:600c:282:: with SMTP id 2mr6095354wmk.91.1637322485675; Fri, 19 Nov 2021 03:48:05 -0800 (PST) Received: from apalos.home ([2a02:587:4627:18c0:2e56:dcff:fe9a:8f06]) by smtp.gmail.com with ESMTPSA id c4sm2688039wrr.37.2021.11.19.03.48.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Nov 2021 03:48:05 -0800 (PST) From: Ilias Apalodimas To: linux-efi@vger.kernel.org Cc: ardb@kernel.org, pjones@redhat.com, nivedita@alum.mit.edu, mjg59@google.com, daniel.kiper@oracle.com, James.Bottomley@hansenpartnership.com, leif@nuviainc.com, jroedel@suse.de, Ilias Apalodimas Subject: [PATCH 2/4 v3] efi/libstub: x86/mixed: increase supported argument count Date: Fri, 19 Nov 2021 13:47:43 +0200 Message-Id: <20211119114745.1560453-3-ilias.apalodimas@linaro.org> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211119114745.1560453-1-ilias.apalodimas@linaro.org> References: <20211119114745.1560453-1-ilias.apalodimas@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org From: Ard Biesheuvel Increase the number of arguments supported by mixed mode calls, so that we will be able to call into the TCG2 protocol to measure the initrd and extend the associated PCR. This involves the TCG2 protocol's hash_log_extend_event() method, which takes five arguments, three of which are u64 and need to be split, producing a total of 8 outgoing Signed-off-by: Ard Biesheuvel Signed-off-by: Ilias Apalodimas --- Changes since v1,2: - Rebased on top of 22aa45cb465b arch/x86/boot/compressed/efi_thunk_64.S | 14 +++++++++++--- arch/x86/include/asm/efi.h | 10 ++++++---- arch/x86/platform/efi/efi_thunk_64.S | 14 ++++++++++++-- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/arch/x86/boot/compressed/efi_thunk_64.S b/arch/x86/boot/compressed/efi_thunk_64.S index 8bb92e9f4e97..d05f781d54f2 100644 --- a/arch/x86/boot/compressed/efi_thunk_64.S +++ b/arch/x86/boot/compressed/efi_thunk_64.S @@ -26,8 +26,6 @@ SYM_FUNC_START(__efi64_thunk) push %rbp push %rbx - leaq 1f(%rip), %rbp - movl %ds, %eax push %rax movl %es, %eax @@ -35,6 +33,11 @@ SYM_FUNC_START(__efi64_thunk) movl %ss, %eax push %rax + /* Copy args passed on stack */ + movq 0x30(%rsp), %rbp + movq 0x38(%rsp), %rbx + movq 0x40(%rsp), %rax + /* * Convert x86-64 ABI params to i386 ABI */ @@ -44,13 +47,18 @@ SYM_FUNC_START(__efi64_thunk) movl %ecx, 0x8(%rsp) movl %r8d, 0xc(%rsp) movl %r9d, 0x10(%rsp) + movl %ebp, 0x14(%rsp) + movl %ebx, 0x18(%rsp) + movl %eax, 0x1c(%rsp) - leaq 0x14(%rsp), %rbx + leaq 0x20(%rsp), %rbx sgdt (%rbx) addq $16, %rbx sidt (%rbx) + leaq 1f(%rip), %rbp + /* * Switch to IDT and GDT with 32-bit segments. This is the firmware GDT * and IDT that was installed when the kernel started executing. The diff --git a/arch/x86/include/asm/efi.h b/arch/x86/include/asm/efi.h index 85f156f8ef81..a323dbac9182 100644 --- a/arch/x86/include/asm/efi.h +++ b/arch/x86/include/asm/efi.h @@ -46,13 +46,14 @@ extern unsigned long efi_mixed_mode_stack_pa; #define __efi_nargs(...) __efi_nargs_(__VA_ARGS__) #define __efi_nargs_(...) __efi_nargs__(0, ##__VA_ARGS__, \ + __efi_arg_sentinel(9), __efi_arg_sentinel(8), \ __efi_arg_sentinel(7), __efi_arg_sentinel(6), \ __efi_arg_sentinel(5), __efi_arg_sentinel(4), \ __efi_arg_sentinel(3), __efi_arg_sentinel(2), \ __efi_arg_sentinel(1), __efi_arg_sentinel(0)) -#define __efi_nargs__(_0, _1, _2, _3, _4, _5, _6, _7, n, ...) \ +#define __efi_nargs__(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, n, ...) \ __take_second_arg(n, \ - ({ BUILD_BUG_ON_MSG(1, "__efi_nargs limit exceeded"); 8; })) + ({ BUILD_BUG_ON_MSG(1, "__efi_nargs limit exceeded"); 10; })) #define __efi_arg_sentinel(n) , n /* @@ -176,8 +177,9 @@ extern u64 efi_setup; extern efi_status_t __efi64_thunk(u32, ...); #define efi64_thunk(...) ({ \ - __efi_nargs_check(efi64_thunk, 6, __VA_ARGS__); \ - __efi64_thunk(__VA_ARGS__); \ + u64 __pad[3]; /* must have space for 3 args on the stack */ \ + __efi_nargs_check(efi64_thunk, 9, __VA_ARGS__); \ + __efi64_thunk(__VA_ARGS__, __pad); \ }) static inline bool efi_is_mixed(void) diff --git a/arch/x86/platform/efi/efi_thunk_64.S b/arch/x86/platform/efi/efi_thunk_64.S index fd3dd1708eba..5b7c6e09954e 100644 --- a/arch/x86/platform/efi/efi_thunk_64.S +++ b/arch/x86/platform/efi/efi_thunk_64.S @@ -36,6 +36,17 @@ SYM_CODE_START(__efi64_thunk) movq efi_mixed_mode_stack_pa(%rip), %rsp push %rax + /* + * Copy args passed via the stack + */ + subq $0x24, %rsp + movq 0x18(%rax), %rbp + movq 0x20(%rax), %rbx + movq 0x28(%rax), %rax + movl %ebp, 0x18(%rsp) + movl %ebx, 0x1c(%rsp) + movl %eax, 0x20(%rsp) + /* * Calculate the physical address of the kernel text. */ @@ -47,7 +58,6 @@ SYM_CODE_START(__efi64_thunk) subq %rax, %rbp subq %rax, %rbx - subq $28, %rsp movl %ebx, 0x0(%rsp) /* return address */ movl %esi, 0x4(%rsp) movl %edx, 0x8(%rsp) @@ -60,7 +70,7 @@ SYM_CODE_START(__efi64_thunk) pushq %rdi /* EFI runtime service address */ lretq -1: movq 24(%rsp), %rsp +1: movq 0x20(%rsp), %rsp pop %rbx pop %rbp retq From patchwork Fri Nov 19 11:47:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilias Apalodimas X-Patchwork-Id: 518326 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6ABBAC433F5 for ; Fri, 19 Nov 2021 11:48:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4821A61AED for ; Fri, 19 Nov 2021 11:48:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231358AbhKSLvK (ORCPT ); Fri, 19 Nov 2021 06:51:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50868 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229687AbhKSLvK (ORCPT ); Fri, 19 Nov 2021 06:51:10 -0500 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 79D39C061574 for ; Fri, 19 Nov 2021 03:48:08 -0800 (PST) Received: by mail-wr1-x434.google.com with SMTP id w29so17650939wra.12 for ; Fri, 19 Nov 2021 03:48:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ABfGpH73O9yZVHQSBed5M+fhfnMXynghFvTmjcQhZhg=; b=v5vrRjM+nhP1x7NZ9jqInHkUBabdkk5uvbyxdTQU6CKmlqw/0ncLgzPo42lvxde9/Q UVnBqHtNDTnwb3EVGdmYYvPM9lsJ3F+eVRcQyPzw0st/vvKU6atqcCVOwZYaTDJzHM93 8XOogfjj9Fw1qxj7dVO2Asj3Kv1NJOp7O7DEvrh1PyGiw8qy3YB4wK+3Pg+uZT9qR7tK MkUxaKofqr/KXKz1N8GHzzdtmZrlTIJT7CmUm5z2FdHCeMD4xrWf1ghip/brCDpbcaC/ BGI5/ESmUuPTE3PQ55qnMWLiwrnV9uYJfWmvMi1geQGEs/FGMcj1LUJ8yczwUa8Q7Tr+ MQZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ABfGpH73O9yZVHQSBed5M+fhfnMXynghFvTmjcQhZhg=; b=bazcQrpT2uSPSNu9aiRIWvRgZDE+bpGNSTUgan6MzUJS5tnJr3oBW5wjc3Zv+IjYCv RVswLKisZDq6rvOjbeTll5fgUF3GVYLBx7wdjMEZnjgulFilLcH+Vqu4/syrlICapc8C bL3BJneFN6Ut2rRovvni5MBsw0QL9ypc7F3Ap6KwkLdv1dqhW2wNPLRzjbcfkcUFck2x 1vVpw1WfOBLBdQWER41kkBlRGcMCkfj5ZP6iVULHp2JrUzHiq6OOmDg5Y6QJoATE/jvl 9L2OOBHIcf9D62yk+l5M4uqVynlQKuEo+SwqNbe0t6HBch1Vdivn8MasKkmYpgkP5RTu gCaQ== X-Gm-Message-State: AOAM533YEgr4v50FHWeqLbE8UE7quIxhP7EmbXZk+1pV2CmMyx5PyQgv H5xGLV2j+nNq5XpgZJnlLHnm8N3tG/Y//w== X-Google-Smtp-Source: ABdhPJz+0Nklr1k4oa3OYnu+Yu5yINXpcLknaSEr19XkvgyDDJnkRLJhCVMBW/U9vSQoYlgedWsTlg== X-Received: by 2002:a5d:66cb:: with SMTP id k11mr6405940wrw.253.1637322487048; Fri, 19 Nov 2021 03:48:07 -0800 (PST) Received: from apalos.home ([2a02:587:4627:18c0:2e56:dcff:fe9a:8f06]) by smtp.gmail.com with ESMTPSA id c4sm2688039wrr.37.2021.11.19.03.48.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Nov 2021 03:48:06 -0800 (PST) From: Ilias Apalodimas To: linux-efi@vger.kernel.org Cc: ardb@kernel.org, pjones@redhat.com, nivedita@alum.mit.edu, mjg59@google.com, daniel.kiper@oracle.com, James.Bottomley@hansenpartnership.com, leif@nuviainc.com, jroedel@suse.de, Ilias Apalodimas Subject: [PATCH 3/4 v3] efi/libstub: consolidate initrd handling across architectures Date: Fri, 19 Nov 2021 13:47:44 +0200 Message-Id: <20211119114745.1560453-4-ilias.apalodimas@linaro.org> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211119114745.1560453-1-ilias.apalodimas@linaro.org> References: <20211119114745.1560453-1-ilias.apalodimas@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org From: Ard Biesheuvel Before adding TPM measurement of the initrd contents, refactor the initrd handling slightly to be more self-contained and consistent. Signed-off-by: Ard Biesheuvel Signed-off-by: Ilias Apalodimas --- Changes since v1,2: - None .../firmware/efi/libstub/efi-stub-helper.c | 13 +++++++--- drivers/firmware/efi/libstub/efi-stub.c | 10 ++----- drivers/firmware/efi/libstub/efistub.h | 1 - drivers/firmware/efi/libstub/x86-stub.c | 26 +++++++------------ 4 files changed, 21 insertions(+), 29 deletions(-) diff --git a/drivers/firmware/efi/libstub/efi-stub-helper.c b/drivers/firmware/efi/libstub/efi-stub-helper.c index d489bdc645fe..01677181453d 100644 --- a/drivers/firmware/efi/libstub/efi-stub-helper.c +++ b/drivers/firmware/efi/libstub/efi-stub-helper.c @@ -20,10 +20,10 @@ bool efi_nochunk; bool efi_nokaslr = !IS_ENABLED(CONFIG_RANDOMIZE_BASE); -bool efi_noinitrd; int efi_loglevel = CONSOLE_LOGLEVEL_DEFAULT; bool efi_novamap; +static bool efi_noinitrd; static bool efi_nosoftreserve; static bool efi_disable_pci_dma = IS_ENABLED(CONFIG_EFI_DISABLE_PCI_DMA); @@ -643,8 +643,10 @@ efi_status_t efi_load_initrd(efi_loaded_image_t *image, { efi_status_t status; - if (!load_addr || !load_size) - return EFI_INVALID_PARAMETER; + if (efi_noinitrd) { + *load_addr = *load_size = 0; + return EFI_SUCCESS; + } status = efi_load_initrd_dev_path(load_addr, load_size, hard_limit); if (status == EFI_SUCCESS) { @@ -655,7 +657,10 @@ efi_status_t efi_load_initrd(efi_loaded_image_t *image, if (status == EFI_SUCCESS && *load_size > 0) efi_info("Loaded initrd from command line option\n"); } - + if (status != EFI_SUCCESS) { + efi_err("Failed to load initrd: 0x%lx\n", status); + *load_addr = *load_size = 0; + } return status; } diff --git a/drivers/firmware/efi/libstub/efi-stub.c b/drivers/firmware/efi/libstub/efi-stub.c index 26e69788f27a..e87e7f1b1a33 100644 --- a/drivers/firmware/efi/libstub/efi-stub.c +++ b/drivers/firmware/efi/libstub/efi-stub.c @@ -134,7 +134,6 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle, enum efi_secureboot_mode secure_boot; struct screen_info *si; efi_properties_table_t *prop_tbl; - unsigned long max_addr; efi_system_table = sys_table_arg; @@ -240,13 +239,8 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle, if (!fdt_addr) efi_info("Generating empty DTB\n"); - if (!efi_noinitrd) { - max_addr = efi_get_max_initrd_addr(image_addr); - status = efi_load_initrd(image, &initrd_addr, &initrd_size, - ULONG_MAX, max_addr); - if (status != EFI_SUCCESS) - efi_err("Failed to load initrd!\n"); - } + efi_load_initrd(image, &initrd_addr, &initrd_size, ULONG_MAX, + efi_get_max_initrd_addr(image_addr)); efi_random_get_seed(); diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h index a2825c435158..edb77b0621ea 100644 --- a/drivers/firmware/efi/libstub/efistub.h +++ b/drivers/firmware/efi/libstub/efistub.h @@ -31,7 +31,6 @@ extern bool efi_nochunk; extern bool efi_nokaslr; -extern bool efi_noinitrd; extern int efi_loglevel; extern bool efi_novamap; diff --git a/drivers/firmware/efi/libstub/x86-stub.c b/drivers/firmware/efi/libstub/x86-stub.c index f14c4ff5839f..01ddd4502e28 100644 --- a/drivers/firmware/efi/libstub/x86-stub.c +++ b/drivers/firmware/efi/libstub/x86-stub.c @@ -673,6 +673,7 @@ unsigned long efi_main(efi_handle_t handle, unsigned long bzimage_addr = (unsigned long)startup_32; unsigned long buffer_start, buffer_end; struct setup_header *hdr = &boot_params->hdr; + unsigned long addr, size; efi_status_t status; efi_system_table = sys_table_arg; @@ -761,22 +762,15 @@ unsigned long efi_main(efi_handle_t handle, * arguments will be processed only if image is not NULL, which will be * the case only if we were loaded via the PE entry point. */ - if (!efi_noinitrd) { - unsigned long addr, size; - - status = efi_load_initrd(image, &addr, &size, - hdr->initrd_addr_max, ULONG_MAX); - - if (status != EFI_SUCCESS) { - efi_err("Failed to load initrd!\n"); - goto fail; - } - if (size > 0) { - efi_set_u64_split(addr, &hdr->ramdisk_image, - &boot_params->ext_ramdisk_image); - efi_set_u64_split(size, &hdr->ramdisk_size, - &boot_params->ext_ramdisk_size); - } + status = efi_load_initrd(image, &addr, &size, hdr->initrd_addr_max, + ULONG_MAX); + if (status != EFI_SUCCESS) + goto fail; + if (size > 0) { + efi_set_u64_split(addr, &hdr->ramdisk_image, + &boot_params->ext_ramdisk_image); + efi_set_u64_split(size, &hdr->ramdisk_size, + &boot_params->ext_ramdisk_size); } /* From patchwork Fri Nov 19 11:47:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilias Apalodimas X-Patchwork-Id: 517034 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 23211C433EF for ; Fri, 19 Nov 2021 11:48:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0296661AED for ; Fri, 19 Nov 2021 11:48:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231639AbhKSLvQ (ORCPT ); Fri, 19 Nov 2021 06:51:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50878 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229687AbhKSLvP (ORCPT ); Fri, 19 Nov 2021 06:51:15 -0500 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6440C061574 for ; Fri, 19 Nov 2021 03:48:09 -0800 (PST) Received: by mail-wr1-x42e.google.com with SMTP id u1so17653582wru.13 for ; Fri, 19 Nov 2021 03:48:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=xiO21zSqE5ENxg4llYKgkKic/silz8q1gk5tkTFY9/0=; b=XB/2BKbJk+Vu0+q1OYePVA/F97FMtwkwemqg6Gs3xxVh4okVFp8BDnVW5p8T+6I/ZP vC3rYxBFuKZ5Q1xQ/AFvPiRztmEZYv7c1ncgqekMB85FLrJr8xNYtq/Y+yUQDnnnMj7p XoDSJMtGrJ0qE7inxOwbkSRj8f7nBAv+wX0EQfYgrt0dH0X4HC+Mut+VTiPi6ozq9y5X jeJLp8oMBt2zUQV1kPQ0G35sj6j65X/SGF6t5r65Erzugn/k/4yPQZzbWSAHnKgxfcNi qnm+0e/jDMvY4spF7YEaTqJjMIj34rCHjz+IiUSu3ZIR023yLx/MdALosRH8ivav+bJN x0uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=xiO21zSqE5ENxg4llYKgkKic/silz8q1gk5tkTFY9/0=; b=eqoEsNv6xDFuzOmIclOYgKSxC+NX+Lco/UDjGxFbQye00g0u8hUVr6IfxwhKWNucmJ dfXtS5VeCSe8/l8OQcNhqE5hozqZWkm2kO8BMCCNrvjYX5okVRybWUavfucsjQYlW7qC GYJkH7oVg9yXqgsBqEPwPWAvtxPUGQS+VDV0Fq9ImLeUP3LlCfR3kFcb2ltxDEBKFoVh Mp2bNYSai/gsHwt/mq4y8aSQIDocpT8Nw7CL8zg2zpMmTgePZuuUdpiEvlbhPqk0KyrQ VAwLwTBmgO/uaxIfLTqXdC6m94ZR8JUHeUZ8NsrYbFZxvsvFW/6pCYFgBZ2hYwUaaX6g vnKQ== X-Gm-Message-State: AOAM530Zw+suLhecx4AxgRzONiEg1GZcHNra6siwgH7XNLB3iLyC1XOj eKBQhviCQ50yUCoam9fMMC01dzYaaf/cFg== X-Google-Smtp-Source: ABdhPJy2OfT881a+0Ih+DBV2/HUb9ux48DGUYvvuEMoUVbpZZYb58mWevABkdYoiXYmW4ZAXQCojQg== X-Received: by 2002:a05:6000:110a:: with SMTP id z10mr6811780wrw.396.1637322488344; Fri, 19 Nov 2021 03:48:08 -0800 (PST) Received: from apalos.home ([2a02:587:4627:18c0:2e56:dcff:fe9a:8f06]) by smtp.gmail.com with ESMTPSA id c4sm2688039wrr.37.2021.11.19.03.48.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Nov 2021 03:48:07 -0800 (PST) From: Ilias Apalodimas To: linux-efi@vger.kernel.org Cc: ardb@kernel.org, pjones@redhat.com, nivedita@alum.mit.edu, mjg59@google.com, daniel.kiper@oracle.com, James.Bottomley@hansenpartnership.com, leif@nuviainc.com, jroedel@suse.de, Ilias Apalodimas Subject: [PATCH 4/4 v3] efi/libstub: measure loaded initrd info into the TPM Date: Fri, 19 Nov 2021 13:47:45 +0200 Message-Id: <20211119114745.1560453-5-ilias.apalodimas@linaro.org> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211119114745.1560453-1-ilias.apalodimas@linaro.org> References: <20211119114745.1560453-1-ilias.apalodimas@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org In an effort to ensure the initrd observed and used by the OS is the same one that was meant to be loaded, which is difficult to guarantee otherwise, let's measure the initrd if the EFI stub and specifically the newly introduced LOAD_FILE2 protocol was used. Modify the initrd loading sequence so that the contents of the initrd are measured into PCR9. Note that the patch is currently using EV_EVENT_TAG to create the eventlog entry instead of EV_IPL. According to the TCP PC Client specification this is used for PCRs defined for OS and application usage. Co-developed-by: Ard Biesheuvel Signed-off-by: Ard Biesheuvel Signed-off-by: Ilias Apalodimas --- Changes since v1,2: - None .../firmware/efi/libstub/efi-stub-helper.c | 72 +++++++++++++++---- 1 file changed, 58 insertions(+), 14 deletions(-) diff --git a/drivers/firmware/efi/libstub/efi-stub-helper.c b/drivers/firmware/efi/libstub/efi-stub-helper.c index 01677181453d..0bd01da1f0df 100644 --- a/drivers/firmware/efi/libstub/efi-stub-helper.c +++ b/drivers/firmware/efi/libstub/efi-stub-helper.c @@ -625,6 +625,47 @@ efi_status_t efi_load_initrd_cmdline(efi_loaded_image_t *image, load_addr, load_size); } +static const struct { + efi_tcg2_event_t event_data; + efi_tcg2_tagged_event_t tagged_event; + u8 tagged_event_data[]; +} initrd_tcg2_event = { + { + sizeof(initrd_tcg2_event) + sizeof("Linux initrd"), + { + sizeof(initrd_tcg2_event.event_data.event_header), + EFI_TCG2_EVENT_HEADER_VERSION, + 9, + EV_EVENT_TAG, + }, + }, + { + INITRD_EVENT_TAG_ID, + sizeof("Linux initrd"), + }, + "Linux initrd", +}; + +void efi_measure_initrd(unsigned long load_addr, unsigned long load_size) +{ + efi_guid_t tcg2_guid = EFI_TCG2_PROTOCOL_GUID; + efi_tcg2_protocol_t *tcg2 = NULL; + efi_status_t status; + + efi_bs_call(locate_protocol, &tcg2_guid, NULL, (void **)&tcg2); + if (tcg2) { + status = efi_call_proto(tcg2, hash_log_extend_event, + 0, load_addr, load_size, + &initrd_tcg2_event.event_data); + if (status != EFI_SUCCESS) + efi_warn("Failed to measure initrd data: 0x%lx\n", + status); + else + efi_info("Measured initrd data into PCR %d\n", + initrd_tcg2_event.event_data.event_header.pcr_index); + } +} + /** * efi_load_initrd() - Load initial RAM disk * @image: EFI loaded image protocol @@ -645,22 +686,25 @@ efi_status_t efi_load_initrd(efi_loaded_image_t *image, if (efi_noinitrd) { *load_addr = *load_size = 0; - return EFI_SUCCESS; + status = EFI_SUCCESS; + } else { + status = efi_load_initrd_dev_path(load_addr, load_size, hard_limit); + if (status == EFI_SUCCESS) { + efi_info("Loaded initrd from LINUX_EFI_INITRD_MEDIA_GUID device path\n"); + if (*load_size > 0) + efi_measure_initrd(*load_addr, *load_size); + } else if (status == EFI_NOT_FOUND) { + status = efi_load_initrd_cmdline(image, load_addr, load_size, + soft_limit, hard_limit); + if (status == EFI_SUCCESS && *load_size > 0) + efi_info("Loaded initrd from command line option\n"); + } + if (status != EFI_SUCCESS) { + efi_err("Failed to load initrd: 0x%lx\n", status); + *load_addr = *load_size = 0; + } } - status = efi_load_initrd_dev_path(load_addr, load_size, hard_limit); - if (status == EFI_SUCCESS) { - efi_info("Loaded initrd from LINUX_EFI_INITRD_MEDIA_GUID device path\n"); - } else if (status == EFI_NOT_FOUND) { - status = efi_load_initrd_cmdline(image, load_addr, load_size, - soft_limit, hard_limit); - if (status == EFI_SUCCESS && *load_size > 0) - efi_info("Loaded initrd from command line option\n"); - } - if (status != EFI_SUCCESS) { - efi_err("Failed to load initrd: 0x%lx\n", status); - *load_addr = *load_size = 0; - } return status; }