Skip to contentSkip to conversation

x86: Trenchboot secure dynamic launch Linux kernel support

PATCH Superseded Ross Philipson 11 participants 116 messages May 31, 2024 – Feb 24 Page 1 of 2

Subsystems (12)
Documentation x86 Crypto API IOMMU +8 more subsystems

v9 of 14 Superseded Latest is v14 →

lore
Share & tools
lore
#0 lore raw

v9 cover x86: Trenchboot secure dynamic launch Linux kernel support

The larger focus of the TrenchBoot project (https://github.com/TrenchBoot) is to enhance the boot security and integrity in a unified manner. The first area of focus has been on the Trusted Computing Group's Dynamic Launch for establishing a hardware Root of Trust for Measurement, also know as DRTM (Dynamic Root of Trust for Measurement). The project has been and continues to work on providing a unified means to Dynamic Launch that is a cross-platform (Intel and AMD) and cross-architecture (x86 and Arm), with our recent involvment in the upcoming Arm DRTM specification. The order of introducing DRTM to the Linux kernel follows the maturity of DRTM in the architectures. Intel's Trusted eXecution Technology (TXT) is present today and only requires a preamble loader, e.g. a boot loader, and an OS kernel that is TXT-aware. AMD DRTM implementation has been present since the introduction of AMD-V but requires an additional component that is AMD specific and referred to in the specification as the Secure Loader, which the TrenchBoot project has an active prototype in development. Finally Arm's implementation is in specification development stage and the project is looking to support it when it becomes available.

This patchset provides detailed documentation of DRTM, the approach used for adding the capbility, and relevant API/ABI documentation. In addition to the documentation the patch set introduces Intel TXT support as the first platform for Linux Secure Launch.

A quick note on terminology. The larger open source project itself is called TrenchBoot, which is hosted on Github (links below). The kernel feature enabling the use of Dynamic Launch technology is referred to as "Secure Launch" within the kernel code. As such the prefixes sl_/SL_ or slaunch/SLAUNCH will be seen in the code. The stub code discussed above is referred to as the SL stub.

The Secure Launch feature starts with patch #2. Patch #1 was authored by Arvind Sankar. There is no further status on this patch at this point but Secure Launch depends on it so it is included with the set.

Links:

The TrenchBoot project including documentation:

https://trenchboot.org

The TrenchBoot project on Github:

https://github.com/trenchboot

Intel TXT is documented in its own specification and in the SDM Instruction Set volume:

https://www.intel.com/content/dam/www/public/us/en/documents/guides/intel-txt-software-development-guide.pdf
https://software.intel.com/en-us/articles/intel-sdm

AMD SKINIT is documented in the System Programming manual:

https://www.amd.com/system/files/TechDocs/24593.pdf

The TrenchBoot project provides a quick start guide to help get a system up and running with Secure Launch for Linux:

https://github.com/TrenchBoot/documentation/blob/master/QUICKSTART.md

Patch set based on commit:

torvalds/master/ea5f6ad9ad9645733b72ab53a98e719b460d36a6

Thanks
Ross Philipson and Daniel P. Smith

Changes in v2:

- Modified 32b entry code to prevent causing relocations in the compressed kernel.
- Dropped patches for compressed kernel TPM PCR extender.
- Modified event log code to insert log delimiter events and not rely on TPM access.
- Stop extending PCRs in the early Secure Launch stub code.
- Removed Kconfig options for hash algorithms and use the algorithms the ACM used.
- Match Secure Launch measurement algorithm use to those reported in the TPM 2.0 event log.
- Read the TPM events out of the TPM and extend them into the PCRs using the mainline TPM driver. This is done in the late initcall module.
- Allow use of alternate PCR 19 and 20 for post ACM measurements.
- Add Kconfig constraints needed by Secure Launch (disable KASLR and add x2apic dependency).
- Fix testing of SL_FLAGS when determining if Secure Launch is active and the architecture is TXT.
- Use SYM_DATA_START_LOCAL macros in early entry point code.
- Security audit changes:
- Validate buffers passed to MLE do not overlap the MLE and are properly laid out.
- Validate buffers and memory regions used by the MLE are protected by IOMMU PMRs.
- Force IOMMU to not use passthrough mode during a Secure Launch.
- Prevent KASLR use during a Secure Launch.

Changes in v3:

- Introduce x86 documentation patch to provide background, overview and configuration/ABI information for the Secure Launch kernel feature.
- Remove the IOMMU patch with special cases for disabling IOMMU passthrough. Configuring the IOMMU is now a documentation matter in the previously mentioned new patch.
- Remove special case KASLR disabling code. Configuring KASLR is now a documentation matter in the previously mentioned new patch.
- Fix incorrect panic on TXT public register read.
- Properly handle and measure setup_indirect bootparams in the early launch code.
- Use correct compressed kernel image base address when testing buffers in the early launch stub code. This bug was introduced by the changes to avoid relocation in the compressed kernel.
- Use CPUID feature bits instead of CPUID vendor strings to determine if SMX mode is supported and the system is Intel.
- Remove early NMI re-enable on the BSP. This can be safely done later on the BSP after an IDT is setup.

Changes in v4:
- Expand the cover letter to provide more context to the order that DRTM support will be added.
- Removed debug tracing in TPM request locality funciton and fixed local variable declarations.
- Fixed missing break in default case in slmodule.c.
- Reworded commit messages in patches 1 and 2 per suggestions.

Changes in v5:
- Comprehensive documentation rewrite.
- Use boot param loadflags to communicate Secure Launch status to kernel proper.
- Fix incorrect check of X86_FEATURE_BIT_SMX bit.
- Rename the alternate details and authorities PCR support.
- Refactor the securityfs directory and file setup in slmodule.c.
- Misc. cleanup from internal code reviews.
- Use reverse fir tree format for variables.

Changes in v6:
- Support for the new Secure Launch Resourse Table that standardizes the information passed and forms the ABI between the pre and post launch code.
- Support for booting Linux through the EFI stub entry point and then being able to do a Secure Launch once EFI stub is done and EBS is called.
- Updates to the documentation to reflect the previous two items listed.

Changes in v7:
- Switch to using MONITOR/MWAIT instead of NMIs to park the APs for later bringup by the SMP code.
- Use static inline dummy functions instead of macros when the Secure Launch feature is disabled.
- Move early SHA1 code to lib/crypto and pull it in from there.
- Numerous formatting fixes from comments on LKML.
- Remove efi-stub/DL stub patch temporarily for redesign/rework.

Changes in v8:
- Reintroduce efi-stub Linux kernel booting through the dynamic launch stub (DL stub).
- Add new approach to setting localities > 0 through kernel and sysfs interfaces in the TPM mainline driver.
- General code cleanup from v7 post comments.

Changes in v9:
- Updated DL stub support for recent changes to EFI stub in the kernel.
- Added patches to fix locality changing support in the TPM driver (these patches originally were posted as a separate set).
- Enhanced Secure Launch TPM locality 2 setting in the TPM driver.
- Added locality setting support through sysfs for user land to access.
- Split up SHA1 and SHA256 changes into separate patches and updated the commit messages to be more clear (per request from upstream review).
- Fix Clang compile issues detected by kernel test robot.
- Modifications to the Secure Launch Resource Table ABI:
. Use flex arrays in table structures.
. Update and move fields in tables to make everything 8b aligned. . Add 2 new DLME fields and a txt_heap address field.
. Remove platform specific tables that are not defined yet (AMD/ARM).
- Update Kconfig dependencies for Secure Launch with SHA1/SHA256/TPM.
- Remove push/pop of rsi since boot params is now stored in r15.
- Update outdated kernel documentation.
- Misc. comment fixes for type-os and mispellings.

Arvind Sankar (1):
x86/boot: Place kernel_info at a fixed offset

Daniel P. Smith (6):
x86: Add early SHA-1 support for Secure Launch early measurements
x86: Add early SHA-256 support for Secure Launch early measurements
tpm: Protect against locality counter underflow
tpm: Ensure tpm is in known state at startup
tpm: Make locality requests return consistent values
x86: Secure Launch late initcall platform module

Ross Philipson (12):
Documentation/x86: Secure Launch kernel documentation
x86: Secure Launch Kconfig
x86: Secure Launch Resource Table header file
x86: Secure Launch main header file
x86: Secure Launch kernel early boot stub
x86: Secure Launch kernel late boot stub
x86: Secure Launch SMP bringup support
kexec: Secure Launch kexec SEXIT support
reboot: Secure Launch SEXIT support on reboot paths
tpm: Add ability to set the preferred locality the TPM chip uses
tpm: Add sysfs interface to allow setting and querying the preferred
locality
x86: EFI stub DRTM launch support for Secure Launch

 Documentation/arch/x86/boot.rst               |  21 +
 Documentation/security/index.rst              |   1 +
 .../security/launch-integrity/index.rst       |  11 +
 .../security/launch-integrity/principles.rst  | 320 ++++++++
 .../secure_launch_details.rst                 | 587 ++++++++++++++
 .../secure_launch_overview.rst                | 227 ++++++
 arch/x86/Kconfig                              |  11 +
 arch/x86/boot/compressed/Makefile             |   3 +
 arch/x86/boot/compressed/early_sha1.c         |  12 +
 arch/x86/boot/compressed/early_sha256.c       |   6 +
 arch/x86/boot/compressed/head_64.S            |  30 +
 arch/x86/boot/compressed/kernel_info.S        |  53 +-
 arch/x86/boot/compressed/kernel_info.h        |  12 +
 arch/x86/boot/compressed/sl_main.c            | 577 ++++++++++++++
 arch/x86/boot/compressed/sl_stub.S            | 725 ++++++++++++++++++
 arch/x86/boot/compressed/vmlinux.lds.S        |   6 +
 arch/x86/include/asm/msr-index.h              |   5 +
 arch/x86/include/asm/realmode.h               |   3 +
 arch/x86/include/uapi/asm/bootparam.h         |   1 +
 arch/x86/kernel/Makefile                      |   2 +
 arch/x86/kernel/asm-offsets.c                 |  20 +
 arch/x86/kernel/reboot.c                      |  10 +
 arch/x86/kernel/setup.c                       |   3 +
 arch/x86/kernel/slaunch.c                     | 598 +++++++++++++++
 arch/x86/kernel/slmodule.c                    | 513 +++++++++++++
 arch/x86/kernel/smpboot.c                     |  58 +-
 arch/x86/realmode/init.c                      |   3 +
 arch/x86/realmode/rm/header.S                 |   3 +
 arch/x86/realmode/rm/trampoline_64.S          |  32 +
 drivers/char/tpm/tpm-chip.c                   |  24 +-
 drivers/char/tpm/tpm-interface.c              |  15 +
 drivers/char/tpm/tpm-sysfs.c                  |  30 +
 drivers/char/tpm/tpm.h                        |   1 +
 drivers/char/tpm/tpm_tis_core.c               |  25 +-
 drivers/firmware/efi/libstub/x86-stub.c       |  98 +++
 drivers/iommu/intel/dmar.c                    |   4 +
 include/crypto/sha1.h                         |   1 +
 include/linux/slaunch.h                       | 542 +++++++++++++
 include/linux/slr_table.h                     | 271 +++++++
 include/linux/tpm.h                           |  10 +
 kernel/kexec_core.c                           |   4 +
 lib/crypto/sha1.c                             |  81 ++
 42 files changed, 4946 insertions(+), 13 deletions(-)
 create mode 100644 Documentation/security/launch-integrity/index.rst
 create mode 100644 Documentation/security/launch-integrity/principles.rst
 create mode 100644 Documentation/security/launch-integrity/secure_launch_details.rst
 create mode 100644 Documentation/security/launch-integrity/secure_launch_overview.rst
 create mode 100644 arch/x86/boot/compressed/early_sha1.c
 create mode 100644 arch/x86/boot/compressed/early_sha256.c
 create mode 100644 arch/x86/boot/compressed/kernel_info.h
 create mode 100644 arch/x86/boot/compressed/sl_main.c
 create mode 100644 arch/x86/boot/compressed/sl_stub.S
 create mode 100644 arch/x86/kernel/slaunch.c
 create mode 100644 arch/x86/kernel/slmodule.c
 create mode 100644 include/linux/slaunch.h
 create mode 100644 include/linux/slr_table.h
Signature · 1 line
2.39.3
#1 lore raw

PATCH v9 1/19 x86/boot: Place kernel_info at a fixed offset

From: Arvind Sankar <[email protected]>

There are use cases for storing the offset of a symbol in kernel_info. For example, the trenchboot series [0] needs to store the offset of the Measured Launch Environment header in kernel_info.

Since commit (note: commit ID from tip/master)

commit 527afc212231 ("x86/boot: Check that there are no run-time relocations")

run-time relocations are not allowed in the compressed kernel, so simply using the symbol in kernel_info, as

.long symbol

will cause a linker error because this is not position-independent.

With kernel_info being a separate object file and in a different section from startup_32, there is no way to calculate the offset of a symbol from the start of the image in a position-independent way.

To enable such use cases, put kernel_info into its own section which is placed at a predetermined offset (KERNEL_INFO_OFFSET) via the linker script. This will allow calculating the symbol offset in a position-independent way, by adding the offset from the start of kernel_info to KERNEL_INFO_OFFSET.

Ensure that kernel_info is aligned, and use the SYM_DATA.* macros instead of bare labels. This stores the size of the kernel_info structure in the ELF symbol table.

---
 arch/x86/boot/compressed/kernel_info.S | 19 +++++++++++++++----
 arch/x86/boot/compressed/kernel_info.h | 12 ++++++++++++
 arch/x86/boot/compressed/vmlinux.lds.S |  6 ++++++
 3 files changed, 33 insertions(+), 4 deletions(-)
 create mode 100644 arch/x86/boot/compressed/kernel_info.h
arch/x86/boot/compressed/kernel_info.S +15 -4
index f818ee8fba38..c18f07181dd5 100644
@@ -1,12 +1,23 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 
+#include <linux/linkage.h>
 #include <asm/bootparam.h>
+#include "kernel_info.h"
 
-	.section ".rodata.kernel_info", "a"
+/*
+ * If a field needs to hold the offset of a symbol from the start
+ * of the image, use the macro below, eg
+ *	.long	rva(symbol)
+ * This will avoid creating run-time relocations, which are not
+ * allowed in the compressed kernel.
+ */
+
+#define rva(X) (((X) - kernel_info) + KERNEL_INFO_OFFSET)
 
-	.global kernel_info
+	.section ".rodata.kernel_info", "a"
 
-kernel_info:
+	.balign	16
+SYM_DATA_START(kernel_info)
 	/* Header, Linux top (structure). */
 	.ascii	"LToP"
 	/* Size. */
@@ -19,4 +30,4 @@ kernel_info:
 
 kernel_info_var_len_data:
 	/* Empty for time being... */
-kernel_info_end:
+SYM_DATA_END_LABEL(kernel_info, SYM_L_LOCAL, kernel_info_end)
arch/x86/boot/compressed/kernel_info.h new file +12 -0
new file mode 100644
index 000000000000..c127f84aec63
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef BOOT_COMPRESSED_KERNEL_INFO_H
+#define BOOT_COMPRESSED_KERNEL_INFO_H
+
+#ifdef CONFIG_X86_64
+#define KERNEL_INFO_OFFSET 0x500
+#else /* 32-bit */
+#define KERNEL_INFO_OFFSET 0x100
+#endif
+
+#endif /* BOOT_COMPRESSED_KERNEL_INFO_H */
arch/x86/boot/compressed/vmlinux.lds.S +6 -0
index 083ec6d7722a..718c52f3f1e6 100644
@@ -7,6 +7,7 @@ OUTPUT_FORMAT(CONFIG_OUTPUT_FORMAT)
 
 #include <asm/cache.h>
 #include <asm/page_types.h>
+#include "kernel_info.h"
 
 #ifdef CONFIG_X86_64
 OUTPUT_ARCH(i386:x86-64)
@@ -27,6 +28,11 @@ SECTIONS
 		HEAD_TEXT
 		_ehead = . ;
 	}
+	.rodata.kernel_info KERNEL_INFO_OFFSET : {
+		*(.rodata.kernel_info)
+	}
+	ASSERT(ABSOLUTE(kernel_info) == KERNEL_INFO_OFFSET, "kernel_info at bad address!")
+
 	.rodata..compressed : {
 		*(.rodata..compressed)
 	}
Signature · 7 lines
2.39.3


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
maintainer
#2 lore raw

Re: [PATCH v9 01/19] x86/boot: Place kernel_info at a fixed offset

Quoted 5 lines · Ross Philipson

From: Arvind Sankar <[email protected]>

There are use cases for storing the offset of a symbol in kernel_info. For example, the trenchboot series [0] needs to store the offset of the Measured Launch Environment header in kernel_info.

So either there are other use cases that you should enumerate, or just be straight and state that this is done for Trenchboot.

I believe latter is the case, and there is no reason to project further. If it does not interfere kernel otherwise, it should be fine just by that.

Also I believe that it is written as Trenchboot, without "series" ;-) Think when writing commit message that it will some day be part of the commit log, not a series flying in the air.

Sorry for the nitpicks but better to be punctual and that way also transparent as possible, right?

Quoted 10 lines

Since commit (note: commit ID from tip/master)

commit 527afc212231 ("x86/boot: Check that there are no run-time relocations")

run-time relocations are not allowed in the compressed kernel, so simply using the symbol in kernel_info, as

.long symbol

Quoted 7 lines

will cause a linker error because this is not position-independent.

With kernel_info being a separate object file and in a different section from startup_32, there is no way to calculate the offset of a symbol from the start of the image in a position-independent way.

To enable such use cases, put kernel_info into its own section which is

"To allow Trenchboot to access the fields of kernel_info..."

Much more understandable.

Quoted 8 lines

placed at a predetermined offset (KERNEL_INFO_OFFSET) via the linker script. This will allow calculating the symbol offset in a position-independent way, by adding the offset from the start of kernel_info to KERNEL_INFO_OFFSET.

Ensure that kernel_info is aligned, and use the SYM_DATA.* macros instead of bare labels. This stores the size of the kernel_info structure in the ELF symbol table.

Aligned to which boundary and short explanation why to that boundary, i.e. state the obvious if you bring it up anyway here.

Just seems to be progressing pretty well so taking my eye glass and looking into nitty gritty details...

BR, Jarkko

#3 lore raw

Re: [PATCH v9 01/19] x86/boot: Place kernel_info at a fixed offset

Quoted 9 lines · Jarkko Sakkinen
Quoted 5 lines · Ross Philipson

From: Arvind Sankar <[email protected]>

There are use cases for storing the offset of a symbol in kernel_info. For example, the trenchboot series [0] needs to store the offset of the Measured Launch Environment header in kernel_info.

So either there are other use cases that you should enumerate, or just be straight and state that this is done for Trenchboot.

The kernel_info concept came about because of the work we were doing on TrenchBoot but it was not done for TrenchBoot. It was a collaborative effort between the TrenchBoot team and H. Peter Anvin at Intel. He actually envisioned it being useful elsewhere. If you find the original commits for it (that went in stand-alone) from Daniel Kiper, there is a fair amount of detail what kernel_info is supposed to be and should be used for.

Quoted 11 lines

I believe latter is the case, and there is no reason to project further. If it does not interfere kernel otherwise, it should be fine just by that.

Also I believe that it is written as Trenchboot, without "series" ;-) Think when writing commit message that it will some day be part of the commit log, not a series flying in the air.

Sorry for the nitpicks but better to be punctual and that way also transparent as possible, right?

No problem. We submit the patch sets to get feedback :)

Thanks for the feedback.

Quoted 32 lines
Quoted 11 lines

Since commit (note: commit ID from tip/master)

commit 527afc212231 ("x86/boot: Check that there are no run-time relocations")

run-time relocations are not allowed in the compressed kernel, so simply using the symbol in kernel_info, as

.long symbol

Quoted 7 lines

will cause a linker error because this is not position-independent.

With kernel_info being a separate object file and in a different section from startup_32, there is no way to calculate the offset of a symbol from the start of the image in a position-independent way.

To enable such use cases, put kernel_info into its own section which is

"To allow Trenchboot to access the fields of kernel_info..."

Much more understandable.

Quoted 8 lines

placed at a predetermined offset (KERNEL_INFO_OFFSET) via the linker script. This will allow calculating the symbol offset in a position-independent way, by adding the offset from the start of kernel_info to KERNEL_INFO_OFFSET.

Ensure that kernel_info is aligned, and use the SYM_DATA.* macros instead of bare labels. This stores the size of the kernel_info structure in the ELF symbol table.

Quoted 5 lines

Aligned to which boundary and short explanation why to that boundary, i.e. state the obvious if you bring it up anyway here.

Just seems to be progressing pretty well so taking my eye glass and looking into nitty gritty details...

So a lot of this is up in the air if you read the responses between us and Ard Biesheuvel. It would be nice to get rid of the part where kernel_info is forced to a fixed offset in the setup kernel.

Thanks
Ross

Quoted 2 lines

BR, Jarkko

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#4 lore raw

PATCH v9 2/19 Documentation/x86: Secure Launch kernel documentation

In reply to Ross Philipson

Introduce background, overview and configuration/ABI information for the Secure Launch kernel feature.

---
 Documentation/security/index.rst              |   1 +
 .../security/launch-integrity/index.rst       |  11 +
 .../security/launch-integrity/principles.rst  | 320 ++++++++++
 .../secure_launch_details.rst                 | 587 ++++++++++++++++++
 .../secure_launch_overview.rst                | 227 +++++++
 5 files changed, 1146 insertions(+)
 create mode 100644 Documentation/security/launch-integrity/index.rst
 create mode 100644 Documentation/security/launch-integrity/principles.rst
 create mode 100644 Documentation/security/launch-integrity/secure_launch_details.rst
 create mode 100644 Documentation/security/launch-integrity/secure_launch_overview.rst
Documentation/security/index.rst +1 -0
index 59f8fc106cb0..56e31fb3d91f 100644
@@ -19,3 +19,4 @@ Security Documentation
    digsig
    landlock
    secrets/index
+   launch-integrity/index
Documentation/security/launch-integrity/index.rst new file +11 -0
new file mode 100644
index 000000000000..838328186dd2
@@ -0,0 +1,11 @@
+=====================================
+System Launch Integrity documentation
+=====================================
+
+.. toctree::
+   :maxdepth: 1
+
+   principles
+   secure_launch_overview
+   secure_launch_details
+
Documentation/security/launch-integrity/principles.rst new file +320 -0

320 lines not on this page · raw message

Documentation/security/launch-integrity/secure_launch_details.rst new file +587 -0

587 lines not on this page · raw message

Documentation/security/launch-integrity/secure_launch_overview.rst new file +227 -0

227 lines not on this page · raw message

Signature · 1 line
2.39.3
#5 lore raw

PATCH v9 3/19 x86: Secure Launch Kconfig

In reply to Ross Philipson

Initial bits to bring in Secure Launch functionality. Add Kconfig options for compiling in/out the Secure Launch code.

---
 arch/x86/Kconfig | 11 +++++++++++
 1 file changed, 11 insertions(+)
arch/x86/Kconfig +11 -0
index bc47bc9841ff..ee8e0cbc9a3e 100644
@@ -2067,6 +2067,17 @@ config EFI_RUNTIME_MAP
 
 	  See also Documentation/ABI/testing/sysfs-firmware-efi-runtime-map.
 
+config SECURE_LAUNCH
+	bool "Secure Launch support"
+	depends on X86_64 && X86_X2APIC && TCG_TPM && CRYPTO_LIB_SHA1 && CRYPTO_LIB_SHA256
+	help
+	   The Secure Launch feature allows a kernel to be loaded
+	   directly through an Intel TXT measured launch. Intel TXT
+	   establishes a Dynamic Root of Trust for Measurement (DRTM)
+	   where the CPU measures the kernel image. This feature then
+	   continues the measurement chain over kernel configuration
+	   information and init images.
+
 source "kernel/Kconfig.hz"
 
 config ARCH_SUPPORTS_KEXEC
Signature · 1 line
2.39.3
#6 lore raw

PATCH v9 4/19 x86: Secure Launch Resource Table header file

In reply to Ross Philipson

Introduce the Secure Launch Resource Table which forms the formal interface between the pre and post launch code.

---
 include/linux/slr_table.h | 271 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 271 insertions(+)
 create mode 100644 include/linux/slr_table.h
include/linux/slr_table.h new file +271 -0
new file mode 100644
index 000000000000..213d8ac16f0f
@@ -0,0 +1,271 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Secure Launch Resource Table
+ *
+ * Copyright (c) 2024, Oracle and/or its affiliates.
+ */
+
+#ifndef _LINUX_SLR_TABLE_H
+#define _LINUX_SLR_TABLE_H
+
+/* Put this in efi.h if it becomes a standard */
+#define SLR_TABLE_GUID				EFI_GUID(0x877a9b2a, 0x0385, 0x45d1, 0xa0, 0x34, 0x9d, 0xac, 0x9c, 0x9e, 0x56, 0x5f)
+
+/* SLR table header values */
+#define SLR_TABLE_MAGIC		0x4452544d
+#define SLR_TABLE_REVISION	1
+
+/* Current revisions for the policy and UEFI config */
+#define SLR_POLICY_REVISION		1
+#define SLR_UEFI_CONFIG_REVISION	1
+
+/* SLR defined architectures */
+#define SLR_INTEL_TXT		1
+#define SLR_AMD_SKINIT		2
+
+/* SLR defined bootloaders */
+#define SLR_BOOTLOADER_INVALID	0
+#define SLR_BOOTLOADER_GRUB	1
+
+/* Log formats */
+#define SLR_DRTM_TPM12_LOG	1
+#define SLR_DRTM_TPM20_LOG	2
+
+/* DRTM Policy Entry Flags */
+#define SLR_POLICY_FLAG_MEASURED	0x1
+#define SLR_POLICY_IMPLICIT_SIZE	0x2
+
+/* Array Lengths */
+#define TPM_EVENT_INFO_LENGTH		32
+#define TXT_VARIABLE_MTRRS_LENGTH	32
+
+/* Tags */
+#define SLR_ENTRY_INVALID	0x0000
+#define SLR_ENTRY_DL_INFO	0x0001
+#define SLR_ENTRY_LOG_INFO	0x0002
+#define SLR_ENTRY_ENTRY_POLICY	0x0003
+#define SLR_ENTRY_INTEL_INFO	0x0004
+#define SLR_ENTRY_AMD_INFO	0x0005
+#define SLR_ENTRY_ARM_INFO	0x0006
+#define SLR_ENTRY_UEFI_INFO	0x0007
+#define SLR_ENTRY_UEFI_CONFIG	0x0008
+#define SLR_ENTRY_END		0xffff
+
+/* Entity Types */
+#define SLR_ET_UNSPECIFIED	0x0000
+#define SLR_ET_SLRT		0x0001
+#define SLR_ET_BOOT_PARAMS	0x0002
+#define SLR_ET_SETUP_DATA	0x0003
+#define SLR_ET_CMDLINE		0x0004
+#define SLR_ET_UEFI_MEMMAP	0x0005
+#define SLR_ET_RAMDISK		0x0006
+#define SLR_ET_TXT_OS2MLE	0x0010
+#define SLR_ET_UNUSED		0xffff
+
+#ifndef __ASSEMBLY__
+
+/*
+ * Primary Secure Launch Resource Table Header
+ */
+struct slr_table {
+	u32 magic;
+	u16 revision;
+	u16 architecture;
+	u32 size;
+	u32 max_size;
+	/* table entries */
+} __packed;
+
+/*
+ * Common SLRT Table Header
+ */
+struct slr_entry_hdr {
+	u16 tag;
+	u16 size;
+} __packed;
+
+/*
+ * Boot loader context
+ */
+struct slr_bl_context {
+	u16 bootloader;
+	u16 reserved[3];
+	u64 context;
+} __packed;
+
+/*
+ * Dynamic Launch Callback Function type
+ */
+typedef void (*dl_handler_func)(struct slr_bl_context *bl_context);
+
+/*
+ * DRTM Dynamic Launch Configuration
+ */
+struct slr_entry_dl_info {
+	struct slr_entry_hdr hdr;
+	u32 dce_size;
+	u64 dce_base;
+	u64 dlme_size;
+	u64 dlme_base;
+	u64 dlme_entry;
+	struct slr_bl_context bl_context;
+	u64 dl_handler;
+} __packed;
+
+/*
+ * TPM Log Information
+ */
+struct slr_entry_log_info {
+	struct slr_entry_hdr hdr;
+	u16 format;
+	u16 reserved[3];
+	u32 size;
+	u64 addr;
+} __packed;
+
+/*
+ * DRTM Measurement Entry
+ */
+struct slr_policy_entry {
+	u16 pcr;
+	u16 entity_type;
+	u16 flags;
+	u16 reserved;
+	u64 size;
+	u64 entity;
+	char evt_info[TPM_EVENT_INFO_LENGTH];
+} __packed;
+
+/*
+ * DRTM Measurement Policy
+ */
+struct slr_entry_policy {
+	struct slr_entry_hdr hdr;
+	u16 revision;
+	u16 nr_entries;
+	struct slr_policy_entry policy_entries[];
+} __packed;
+
+/*
+ * Secure Launch defined MTRR saving structures
+ */
+struct slr_txt_mtrr_pair {
+	u64 mtrr_physbase;
+	u64 mtrr_physmask;
+} __packed;
+
+struct slr_txt_mtrr_state {
+	u64 default_mem_type;
+	u64 mtrr_vcnt;
+	struct slr_txt_mtrr_pair mtrr_pair[TXT_VARIABLE_MTRRS_LENGTH];
+} __packed;
+
+/*
+ * Intel TXT Info table
+ */
+struct slr_entry_intel_info {
+	struct slr_entry_hdr hdr;
+	u16 reserved[2];
+	u64 txt_heap;
+	u64 saved_misc_enable_msr;
+	struct slr_txt_mtrr_state saved_bsp_mtrrs;
+} __packed;
+
+/*
+ * UEFI config measurement entry
+ */
+struct slr_uefi_cfg_entry {
+	u16 pcr;
+	u16 reserved;
+	u32 size;
+	u64 cfg; /* address or value */
+	char evt_info[TPM_EVENT_INFO_LENGTH];
+} __packed;
+
+/*
+ * UEFI config measurements
+ */
+struct slr_entry_uefi_config {
+	struct slr_entry_hdr hdr;
+	u16 revision;
+	u16 nr_entries;
+	struct slr_uefi_cfg_entry uefi_cfg_entries[];
+} __packed;
+
+static inline void *slr_end_of_entries(struct slr_table *table)
+{
+	return (void *)table + table->size;
+}
+
+static inline void *
+slr_next_entry(struct slr_table *table,
+	       struct slr_entry_hdr *curr)
+{
+	struct slr_entry_hdr *next = (struct slr_entry_hdr *)
+				((u8 *)curr + curr->size);
+
+	if ((void *)next >= slr_end_of_entries(table))
+		return NULL;
+	if (next->tag == SLR_ENTRY_END)
+		return NULL;
+
+	return next;
+}
+
+static inline void *
+slr_next_entry_by_tag(struct slr_table *table,
+		      struct slr_entry_hdr *entry,
+		      u16 tag)
+{
+	if (!entry) /* Start from the beginning */
+		entry = (struct slr_entry_hdr *)(((u8 *)table) + sizeof(*table));
+
+	for ( ; ; ) {
+		if (entry->tag == tag)
+			return entry;
+
+		entry = slr_next_entry(table, entry);
+		if (!entry)
+			return NULL;
+	}
+
+	return NULL;
+}
+
+static inline int
+slr_add_entry(struct slr_table *table,
+	      struct slr_entry_hdr *entry)
+{
+	struct slr_entry_hdr *end;
+
+	if ((table->size + entry->size) > table->max_size)
+		return -1;
+
+	memcpy((u8 *)table + table->size - sizeof(*end), entry, entry->size);
+	table->size += entry->size;
+
+	end  = (struct slr_entry_hdr *)((u8 *)table + table->size - sizeof(*end));
+	end->tag = SLR_ENTRY_END;
+	end->size = sizeof(*end);
+
+	return 0;
+}
+
+static inline void
+slr_init_table(struct slr_table *slrt, u16 architecture, u32 max_size)
+{
+	struct slr_entry_hdr *end;
+
+	slrt->magic = SLR_TABLE_MAGIC;
+	slrt->revision = SLR_TABLE_REVISION;
+	slrt->architecture = architecture;
+	slrt->size = sizeof(*slrt) + sizeof(*end);
+	slrt->max_size = max_size;
+	end = (struct slr_entry_hdr *)((u8 *)slrt + sizeof(*slrt));
+	end->tag = SLR_ENTRY_END;
+	end->size = sizeof(*end);
+}
+
+#endif /* !__ASSEMBLY */
+
+#endif /* _LINUX_SLR_TABLE_H */
Signature · 7 lines
2.39.3


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
maintainer
#7 lore raw

Re: [PATCH v9 04/19] x86: Secure Launch Resource Table header file

Quoted 4 lines · Ross Philipson

Introduce the Secure Launch Resource Table which forms the formal interface between the pre and post launch code.

If a uarch specific, I'd appreciate Intel SDM reference here so that I can look it up and compare. Like in section granularity.

BR, Jarkko

#8 lore raw

Re: [PATCH v9 04/19] x86: Secure Launch Resource Table header file

Quoted 8 lines · Jarkko Sakkinen
Quoted 4 lines · Ross Philipson

Introduce the Secure Launch Resource Table which forms the formal interface between the pre and post launch code.

If a uarch specific, I'd appreciate Intel SDM reference here so that I can look it up and compare. Like in section granularity.

This table is meant to not be architecture specific though it can contain architecture specific sub-entities. E.g. there is a TXT specific table and in the future there will be an AMD and ARM one (and hopefully some others). I hope that addresses what you are pointing out or maybe I don't fully understand what you mean here...

Thanks
Ross

Quoted 2 lines

BR, Jarkko

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

maintainer
#9 lore raw

Re: [PATCH v9 04/19] x86: Secure Launch Resource Table header file

Quoted 7 lines · EEST,
Quoted 6 lines · Jarkko Sakkinen
Quoted 4 lines · Ross Philipson

Introduce the Secure Launch Resource Table which forms the formal interface between the pre and post launch code.

Quoted 8 lines
Quoted 2 lines

If a uarch specific, I'd appreciate Intel SDM reference here so that I can look it up and compare. Like in section granularity.

This table is meant to not be architecture specific though it can contain architecture specific sub-entities. E.g. there is a TXT specific table and in the future there will be an AMD and ARM one (and hopefully some others). I hope that addresses what you are pointing out or maybe I don't fully understand what you mean here...

At least Intel SDM has a definition of any possible architecture specific data structure. It is handy to also have this available in inline comment for any possible such structure pointing out the section where it is defined.

BR, Jarkko

#10 lore raw

Re: [PATCH v9 04/19] x86: Secure Launch Resource Table header file

Quoted 17 lines · Jarkko Sakkinen
Quoted 7 lines · EEST,
Quoted 6 lines · Jarkko Sakkinen
Quoted 4 lines · Ross Philipson

Introduce the Secure Launch Resource Table which forms the formal interface between the pre and post launch code.

Quoted 8 lines
Quoted 2 lines

If a uarch specific, I'd appreciate Intel SDM reference here so that I can look it up and compare. Like in section granularity.

This table is meant to not be architecture specific though it can contain architecture specific sub-entities. E.g. there is a TXT specific table and in the future there will be an AMD and ARM one (and hopefully some others). I hope that addresses what you are pointing out or maybe I don't fully understand what you mean here...

Quoted 4 lines

At least Intel SDM has a definition of any possible architecture specific data structure. It is handy to also have this available in inline comment for any possible such structure pointing out the section where it is defined.

The TXT specific structure is not defined in the SDM or the TXT dev guide. Part of it is driven by requirements in the TXT dev guide but that guide does not contain implementation details.

That said, if you would like links to relevant documents in the comments before arch specific structures, I can add them.

Ross

Quoted 2 lines

BR, Jarkko

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

maintainer
#11 lore raw

Re: [PATCH v9 04/19] x86: Secure Launch Resource Table header file

Quoted 23 lines · EEST,
Quoted 17 lines · Jarkko Sakkinen
Quoted 7 lines · EEST,
Quoted 6 lines · Jarkko Sakkinen
Quoted 4 lines · Ross Philipson

Introduce the Secure Launch Resource Table which forms the formal interface between the pre and post launch code.

Quoted 8 lines
Quoted 2 lines

If a uarch specific, I'd appreciate Intel SDM reference here so that I can look it up and compare. Like in section granularity.

This table is meant to not be architecture specific though it can contain architecture specific sub-entities. E.g. there is a TXT specific table and in the future there will be an AMD and ARM one (and hopefully some others). I hope that addresses what you are pointing out or maybe I don't fully understand what you mean here...

Quoted 4 lines

At least Intel SDM has a definition of any possible architecture specific data structure. It is handy to also have this available in inline comment for any possible such structure pointing out the section where it is defined.

Quoted 6 lines

The TXT specific structure is not defined in the SDM or the TXT dev guide. Part of it is driven by requirements in the TXT dev guide but that guide does not contain implementation details.

That said, if you would like links to relevant documents in the comments before arch specific structures, I can add them.

Vol. 2D 7-40, in the description of GETSEC[WAKEUP] there is in fact a description of MLE JOINT structure at least:

1. GDT limit (offset 0)
2. GDT base (offset 4)
3. Segment selector initializer (offset 8)
4. EIP (offset 12)

So is this only exercised in protect mode, and not in long mode? Just wondering whether I should make a bug report on this for SDM or not.

Especially this puzzles me, given that x86s won't have protected mode in the first place...

BR, Jarkko

maintainer
#12 lore raw

Re: [PATCH v9 04/19] x86: Secure Launch Resource Table header file

Quoted 39 lines · Jarkko Sakkinen
Quoted 23 lines · EEST,
Quoted 17 lines · Jarkko Sakkinen
Quoted 7 lines · EEST,
Quoted 6 lines · Jarkko Sakkinen
Quoted 4 lines · Ross Philipson

Introduce the Secure Launch Resource Table which forms the formal interface between the pre and post launch code.

Quoted 8 lines
Quoted 2 lines

If a uarch specific, I'd appreciate Intel SDM reference here so that I can look it up and compare. Like in section granularity.

This table is meant to not be architecture specific though it can contain architecture specific sub-entities. E.g. there is a TXT specific table and in the future there will be an AMD and ARM one (and hopefully some others). I hope that addresses what you are pointing out or maybe I don't fully understand what you mean here...

Quoted 4 lines

At least Intel SDM has a definition of any possible architecture specific data structure. It is handy to also have this available in inline comment for any possible such structure pointing out the section where it is defined.

Quoted 6 lines

The TXT specific structure is not defined in the SDM or the TXT dev guide. Part of it is driven by requirements in the TXT dev guide but that guide does not contain implementation details.

That said, if you would like links to relevant documents in the comments before arch specific structures, I can add them.

Vol. 2D 7-40, in the description of GETSEC[WAKEUP] there is in fact a description of MLE JOINT structure at least:

1. GDT limit (offset 0)
2. GDT base (offset 4)
3. Segment selector initializer (offset 8)
4. EIP (offset 12)

Quoted 5 lines

So is this only exercised in protect mode, and not in long mode? Just wondering whether I should make a bug report on this for SDM or not.

Especially this puzzles me, given that x86s won't have protected mode in the first place...

That raises a relevant question: will this ever work in x86s? SDM does not really support that it would but it could be also just outdated information.

I'm neither sure how or will AMD align with x86s.

Just point out a glitch...

BR, Jarkko

#13 lore raw

Re: [PATCH v9 04/19] x86: Secure Launch Resource Table header file

In reply to Jarkko Sakkinen
Quoted 34 lines · Jarkko Sakkinen
Quoted 23 lines · EEST,
Quoted 17 lines · Jarkko Sakkinen
Quoted 7 lines · EEST,
Quoted 6 lines · Jarkko Sakkinen
Quoted 4 lines · Ross Philipson

Introduce the Secure Launch Resource Table which forms the formal interface between the pre and post launch code.

Quoted 8 lines
Quoted 2 lines

If a uarch specific, I'd appreciate Intel SDM reference here so that I can look it up and compare. Like in section granularity.

This table is meant to not be architecture specific though it can contain architecture specific sub-entities. E.g. there is a TXT specific table and in the future there will be an AMD and ARM one (and hopefully some others). I hope that addresses what you are pointing out or maybe I don't fully understand what you mean here...

Quoted 4 lines

At least Intel SDM has a definition of any possible architecture specific data structure. It is handy to also have this available in inline comment for any possible such structure pointing out the section where it is defined.

Quoted 6 lines

The TXT specific structure is not defined in the SDM or the TXT dev guide. Part of it is driven by requirements in the TXT dev guide but that guide does not contain implementation details.

That said, if you would like links to relevant documents in the comments before arch specific structures, I can add them.

Vol. 2D 7-40, in the description of GETSEC[WAKEUP] there is in fact a description of MLE JOINT structure at least:

Quoted 7 lines

1. GDT limit (offset 0)
2. GDT base (offset 4)
3. Segment selector initializer (offset 8)
4. EIP (offset 12)

So is this only exercised in protect mode, and not in long mode? Just wondering whether I should make a bug report on this for SDM or not.

I believe you can issue the SENTER instruction in long mode, compat mode or protected mode. On the other side thought, you will pop out of the TXT initialization in protected mode. The SDM outlines what registers will hold what values and what is valid and not valid. The APs will also vector through the join structure mentioned above to the location specified in protected mode using the GDT information you provide.

Quoted 3 lines

Especially this puzzles me, given that x86s won't have protected mode in the first place...

My guess is the simplified x86 architecture will not support TXT. It is not supported on a number of CPUs/chipsets as it stands today. Just a guess but we know only vPro systems support TXT today.

Thanks
Ross

Quoted 3 lines

BR, Jarkko

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

maintainer
#14 lore raw

Re: [PATCH v9 04/19] x86: Secure Launch Resource Table header file

Quoted 51 lines · EEST,
Quoted 34 lines · Jarkko Sakkinen
Quoted 23 lines · EEST,
Quoted 17 lines · Jarkko Sakkinen
Quoted 7 lines · EEST,
Quoted 6 lines · Jarkko Sakkinen
Quoted 4 lines · Ross Philipson

Introduce the Secure Launch Resource Table which forms the formal interface between the pre and post launch code.

Quoted 8 lines
Quoted 2 lines

If a uarch specific, I'd appreciate Intel SDM reference here so that I can look it up and compare. Like in section granularity.

This table is meant to not be architecture specific though it can contain architecture specific sub-entities. E.g. there is a TXT specific table and in the future there will be an AMD and ARM one (and hopefully some others). I hope that addresses what you are pointing out or maybe I don't fully understand what you mean here...

Quoted 4 lines

At least Intel SDM has a definition of any possible architecture specific data structure. It is handy to also have this available in inline comment for any possible such structure pointing out the section where it is defined.

Quoted 6 lines

The TXT specific structure is not defined in the SDM or the TXT dev guide. Part of it is driven by requirements in the TXT dev guide but that guide does not contain implementation details.

That said, if you would like links to relevant documents in the comments before arch specific structures, I can add them.

Vol. 2D 7-40, in the description of GETSEC[WAKEUP] there is in fact a description of MLE JOINT structure at least:

Quoted 7 lines

1. GDT limit (offset 0)
2. GDT base (offset 4)
3. Segment selector initializer (offset 8)
4. EIP (offset 12)

So is this only exercised in protect mode, and not in long mode? Just wondering whether I should make a bug report on this for SDM or not.

I believe you can issue the SENTER instruction in long mode, compat mode or protected mode. On the other side thought, you will pop out of the TXT initialization in protected mode. The SDM outlines what registers will hold what values and what is valid and not valid. The APs will also vector through the join structure mentioned above to the location specified in protected mode using the GDT information you provide.

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

Especially this puzzles me, given that x86s won't have protected mode in the first place...

My guess is the simplified x86 architecture will not support TXT. It is not supported on a number of CPUs/chipsets as it stands today. Just a guess but we know only vPro systems support TXT today.

I'm wondering could this bootstrap itself inside TDX or SNP, and that way provide path forward? AFAIK, TDX can be nested straight of the bat and SNP from 2nd generation EPYC's, which contain the feature.

I do buy the idea of attesting the host, not just the guests, even in the "confidential world". That said, I'm not sure does it make sense to add all this infrastructure for a technology with such a short expiration date?

I would not want to say this at v9, and it is not really your fault either, but for me this would make a lot more sense if the core of Trenchboot was redesigned around these newer technologies with a long-term future.

The idea itself is great!

BR, Jarkko

#15 lore raw

Re: [PATCH v9 04/19] x86: Secure Launch Resource Table header file

Quoted 68 lines · Jarkko Sakkinen
Quoted 51 lines · EEST,
Quoted 34 lines · Jarkko Sakkinen
Quoted 23 lines · EEST,
Quoted 17 lines · Jarkko Sakkinen
Quoted 7 lines · EEST,
Quoted 6 lines · Jarkko Sakkinen
Quoted 4 lines · Ross Philipson

Introduce the Secure Launch Resource Table which forms the formal interface between the pre and post launch code.

Quoted 8 lines
Quoted 2 lines

If a uarch specific, I'd appreciate Intel SDM reference here so that I can look it up and compare. Like in section granularity.

This table is meant to not be architecture specific though it can contain architecture specific sub-entities. E.g. there is a TXT specific table and in the future there will be an AMD and ARM one (and hopefully some others). I hope that addresses what you are pointing out or maybe I don't fully understand what you mean here...

Quoted 4 lines

At least Intel SDM has a definition of any possible architecture specific data structure. It is handy to also have this available in inline comment for any possible such structure pointing out the section where it is defined.

Quoted 6 lines

The TXT specific structure is not defined in the SDM or the TXT dev guide. Part of it is driven by requirements in the TXT dev guide but that guide does not contain implementation details.

That said, if you would like links to relevant documents in the comments before arch specific structures, I can add them.

Vol. 2D 7-40, in the description of GETSEC[WAKEUP] there is in fact a description of MLE JOINT structure at least:

Quoted 7 lines

1. GDT limit (offset 0)
2. GDT base (offset 4)
3. Segment selector initializer (offset 8)
4. EIP (offset 12)

So is this only exercised in protect mode, and not in long mode? Just wondering whether I should make a bug report on this for SDM or not.

I believe you can issue the SENTER instruction in long mode, compat mode or protected mode. On the other side thought, you will pop out of the TXT initialization in protected mode. The SDM outlines what registers will hold what values and what is valid and not valid. The APs will also vector through the join structure mentioned above to the location specified in protected mode using the GDT information you provide.

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

Especially this puzzles me, given that x86s won't have protected mode in the first place...

My guess is the simplified x86 architecture will not support TXT. It is not supported on a number of CPUs/chipsets as it stands today. Just a guess but we know only vPro systems support TXT today.

I'm wondering could this bootstrap itself inside TDX or SNP, and that way provide path forward? AFAIK, TDX can be nested straight of the bat and SNP from 2nd generation EPYC's, which contain the feature.

I do buy the idea of attesting the host, not just the guests, even in the "confidential world". That said, I'm not sure does it make sense to add all this infrastructure for a technology with such a short expiration date?

Quoted 4 lines

I would not want to say this at v9, and it is not really your fault either, but for me this would make a lot more sense if the core of Trenchboot was redesigned around these newer technologies with a long-term future.

So I did not mean to imply that DRTM support on various platforms/architectures has a short expiration date. In fact we are actively working on DRTM support through the TrenchBoot project on several platforms/architectures. Just a quick rundown here:

Intel: Plenty of Intel platforms are vPro with TXT. It is really just the lower end systems that don't have it available (like Core i3). And my guess was wrong about x86s. You can find the spec on the page in the following link. There is an entire subsection on SMX support on x86s and the changes to the various GETSEC instruction leaves that were made to make it work there (see 3.15).

https://www.intel.com/content/www/us/en/developer/articles/technical/envisioning-future-simplified-architecture.html

AMD: We are actively working on SKINIT DRTM support that will go into TrenchBoot. There are changes coming soon to AMD SKINIT to make it more robust and address some earlier issues. We hope to be able to start sending AMD DRTM support up in the posts to LKML in the not too distant future.

Arm: They have recently released their DRTM specification and at least one Arm vendor is close to releasing firmware that will support DRTM. Again we are actively working in this area on the TrenchBoot project.

https://developer.arm.com/documentation/den0113/latest/

One final thought I had. The technologies you mentioned above seem to be to be complementary to DRTM as opposed to being a replacement for it, at least to me but I am not an expert on them.

Perhaps Daniel Smith would like to expand on what I have said here.

Thanks
Ross

Quoted 5 lines

The idea itself is great!

BR, Jarkko

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

maintainer
#16 lore raw

Re: [PATCH v9 04/19] x86: Secure Launch Resource Table header file

Quoted 5 lines · EEST,

So I did not mean to imply that DRTM support on various platforms/architectures has a short expiration date. In fact we are actively working on DRTM support through the TrenchBoot project on several platforms/architectures. Just a quick rundown here:

Quoted 8 lines

Intel: Plenty of Intel platforms are vPro with TXT. It is really just the lower end systems that don't have it available (like Core i3). And my guess was wrong about x86s. You can find the spec on the page in the following link. There is an entire subsection on SMX support on x86s and the changes to the various GETSEC instruction leaves that were made to make it work there (see 3.15).

https://www.intel.com/content/www/us/en/developer/articles/technical/envisioning-future-simplified-architecture.html

Happend to bump into same PDF specification and exactly the seeked information is "3.15 SMX Changes". So just write this down to some patch that starts adding SMX things.

So link and document, and other stuff above is not relevant from upstream context, only potential maintenance burden :-)

For any architectures dig a similar fact:

1. Is not dead.
2. Will be there also in future.

Make any architecture existentially relevant for and not too much coloring in the text that is easy to check.

It is nearing 5k lines so you should be really good with measured facts too (not just launch) :-)

BR, Jarkko

#17 lore raw

Re: [PATCH v9 04/19] x86: Secure Launch Resource Table header file

Quoted 15 lines · Jarkko Sakkinen
Quoted 5 lines · EEST,

So I did not mean to imply that DRTM support on various platforms/architectures has a short expiration date. In fact we are actively working on DRTM support through the TrenchBoot project on several platforms/architectures. Just a quick rundown here:

Quoted 8 lines

Intel: Plenty of Intel platforms are vPro with TXT. It is really just the lower end systems that don't have it available (like Core i3). And my guess was wrong about x86s. You can find the spec on the page in the following link. There is an entire subsection on SMX support on x86s and the changes to the various GETSEC instruction leaves that were made to make it work there (see 3.15).

https://urldefense.com/v3/__https://www.intel.com/content/www/us/en/developer/articles/technical/envisioning-future-simplified-architecture.html__;!!ACWV5N9M2RV99hQ!Lt-srkRLHstA9PPCB-NWogvHP-9mfh2bHjkml-lARY79BhYlWJjhrHb6RyCN_WdGstcABq1FdqPUKn5dCdw$

Quoted 8 lines

Happend to bump into same PDF specification and exactly the seeked information is "3.15 SMX Changes". So just write this down to some patch that starts adding SMX things.

So link and document, and other stuff above is not relevant from upstream context, only potential maintenance burden :-)

I am not 100% sure what you mean exactly here...

Quoted 11 lines

For any architectures dig a similar fact:

1. Is not dead.
2. Will be there also in future.

Make any architecture existentially relevant for and not too much coloring in the text that is easy to check.

It is nearing 5k lines so you should be really good with measured facts too (not just launch) :-)

... but overall I get your meaning. We will spend time on this sort of documentation for the v10 release.

Thanks for the feedback,
Ross

Quoted 3 lines

BR, Jarkko

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

maintainer
#18 lore raw

Re: [PATCH v9 04/19] x86: Secure Launch Resource Table header file

Quoted 5 lines · EEST,
Quoted 5 lines

For any architectures dig a similar fact:

1. Is not dead.
2. Will be there also in future.

Quoted 8 lines
Quoted 5 lines

Make any architecture existentially relevant for and not too much coloring in the text that is easy to check.

It is nearing 5k lines so you should be really good with measured facts too (not just launch) :-)

... but overall I get your meaning. We will spend time on this sort of documentation for the v10 release.

Yeah, I mean we live in the universe of 3 letter acronyms so it is better to summarize the existential part, especially in a ~5 KSLOC patch set ;-)

BR, Jarkko

#19 lore raw

Re: [PATCH v9 04/19] x86: Secure Launch Resource Table header file

Quoted 12 lines · Jarkko Sakkinen
Quoted 11 lines · EEST,
Quoted 10 lines

For any architectures dig a similar fact:

1. Is not dead.
2. Will be there also in future.

Make any architecture existentially relevant for and not too much coloring in the text that is easy to check.

It is nearing 5k lines so you should be really good with measured facts too (not just launch) :-)

Quoted 6 lines
Quoted 2 lines

... but overall I get your meaning. We will spend time on this sort of documentation for the v10 release.

Yeah, I mean we live in the universe of 3 letter acronyms so it is better to summarize the existential part, especially in a ~5 KSLOC patch set ;-)

Indeed, thanks.

Ross

Quoted 3 lines

BR, Jarkko

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#20 lore raw

PATCH v9 5/19 x86: Secure Launch main header file

In reply to Ross Philipson

Introduce the main Secure Launch header file used in the early SL stub and the early setup code.

---
 include/linux/slaunch.h | 542 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 542 insertions(+)
 create mode 100644 include/linux/slaunch.h
include/linux/slaunch.h new file +542 -0
new file mode 100644
index 000000000000..90a7f22ddbdd
@@ -0,0 +1,542 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Main Secure Launch header file.
+ *
+ * Copyright (c) 2024, Oracle and/or its affiliates.
+ */
+
+#ifndef _LINUX_SLAUNCH_H
+#define _LINUX_SLAUNCH_H
+
+/*
+ * Secure Launch Defined State Flags
+ */
+#define SL_FLAG_ACTIVE		0x00000001
+#define SL_FLAG_ARCH_SKINIT	0x00000002
+#define SL_FLAG_ARCH_TXT	0x00000004
+
+/*
+ * Secure Launch CPU Type
+ */
+#define SL_CPU_AMD	1
+#define SL_CPU_INTEL	2
+
+#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
+
+#define __SL32_CS	0x0008
+#define __SL32_DS	0x0010
+
+/*
+ * Intel Safer Mode Extensions (SMX)
+ *
+ * Intel SMX provides a programming interface to establish a Measured Launched
+ * Environment (MLE). The measurement and protection mechanisms supported by the
+ * capabilities of an Intel Trusted Execution Technology (TXT) platform. SMX is
+ * the processor’s programming interface in an Intel TXT platform.
+ *
+ * See Intel SDM Volume 2 - 6.1 "Safer Mode Extensions Reference"
+ */
+
+/*
+ * SMX GETSEC Leaf Functions
+ */
+#define SMX_X86_GETSEC_SEXIT	5
+#define SMX_X86_GETSEC_SMCTRL	7
+#define SMX_X86_GETSEC_WAKEUP	8
+
+/*
+ * Intel Trusted Execution Technology MMIO Registers Banks
+ */
+#define TXT_PUB_CONFIG_REGS_BASE	0xfed30000
+#define TXT_PRIV_CONFIG_REGS_BASE	0xfed20000
+#define TXT_NR_CONFIG_PAGES     ((TXT_PUB_CONFIG_REGS_BASE - \
+				  TXT_PRIV_CONFIG_REGS_BASE) >> PAGE_SHIFT)
+
+/*
+ * Intel Trusted Execution Technology (TXT) Registers
+ */
+#define TXT_CR_STS			0x0000
+#define TXT_CR_ESTS			0x0008
+#define TXT_CR_ERRORCODE		0x0030
+#define TXT_CR_CMD_RESET		0x0038
+#define TXT_CR_CMD_CLOSE_PRIVATE	0x0048
+#define TXT_CR_DIDVID			0x0110
+#define TXT_CR_VER_EMIF			0x0200
+#define TXT_CR_CMD_UNLOCK_MEM_CONFIG	0x0218
+#define TXT_CR_SINIT_BASE		0x0270
+#define TXT_CR_SINIT_SIZE		0x0278
+#define TXT_CR_MLE_JOIN			0x0290
+#define TXT_CR_HEAP_BASE		0x0300
+#define TXT_CR_HEAP_SIZE		0x0308
+#define TXT_CR_SCRATCHPAD		0x0378
+#define TXT_CR_CMD_OPEN_LOCALITY1	0x0380
+#define TXT_CR_CMD_CLOSE_LOCALITY1	0x0388
+#define TXT_CR_CMD_OPEN_LOCALITY2	0x0390
+#define TXT_CR_CMD_CLOSE_LOCALITY2	0x0398
+#define TXT_CR_CMD_SECRETS		0x08e0
+#define TXT_CR_CMD_NO_SECRETS		0x08e8
+#define TXT_CR_E2STS			0x08f0
+
+/* TXT default register value */
+#define TXT_REGVALUE_ONE		0x1ULL
+
+/* TXTCR_STS status bits */
+#define TXT_SENTER_DONE_STS		BIT(0)
+#define TXT_SEXIT_DONE_STS		BIT(1)
+
+/*
+ * SINIT/MLE Capabilities Field Bit Definitions
+ */
+#define TXT_SINIT_MLE_CAP_WAKE_GETSEC	0
+#define TXT_SINIT_MLE_CAP_WAKE_MONITOR	1
+
+/*
+ * OS/MLE Secure Launch Specific Definitions
+ */
+#define TXT_OS_MLE_STRUCT_VERSION	1
+#define TXT_OS_MLE_MAX_VARIABLE_MTRRS	32
+
+/*
+ * TXT Heap Table Enumeration
+ */
+#define TXT_BIOS_DATA_TABLE		1
+#define TXT_OS_MLE_DATA_TABLE		2
+#define TXT_OS_SINIT_DATA_TABLE		3
+#define TXT_SINIT_MLE_DATA_TABLE	4
+#define TXT_SINIT_TABLE_MAX		TXT_SINIT_MLE_DATA_TABLE
+
+/*
+ * Secure Launch Defined Error Codes used in MLE-initiated TXT resets.
+ *
+ * TXT Specification
+ * Appendix I ACM Error Codes
+ */
+#define SL_ERROR_GENERIC		0xc0008001
+#define SL_ERROR_TPM_INIT		0xc0008002
+#define SL_ERROR_TPM_INVALID_LOG20	0xc0008003
+#define SL_ERROR_TPM_LOGGING_FAILED	0xc0008004
+#define SL_ERROR_REGION_STRADDLE_4GB	0xc0008005
+#define SL_ERROR_TPM_EXTEND		0xc0008006
+#define SL_ERROR_MTRR_INV_VCNT		0xc0008007
+#define SL_ERROR_MTRR_INV_DEF_TYPE	0xc0008008
+#define SL_ERROR_MTRR_INV_BASE		0xc0008009
+#define SL_ERROR_MTRR_INV_MASK		0xc000800a
+#define SL_ERROR_MSR_INV_MISC_EN	0xc000800b
+#define SL_ERROR_INV_AP_INTERRUPT	0xc000800c
+#define SL_ERROR_INTEGER_OVERFLOW	0xc000800d
+#define SL_ERROR_HEAP_WALK		0xc000800e
+#define SL_ERROR_HEAP_MAP		0xc000800f
+#define SL_ERROR_REGION_ABOVE_4GB	0xc0008010
+#define SL_ERROR_HEAP_INVALID_DMAR	0xc0008011
+#define SL_ERROR_HEAP_DMAR_SIZE		0xc0008012
+#define SL_ERROR_HEAP_DMAR_MAP		0xc0008013
+#define SL_ERROR_HI_PMR_BASE		0xc0008014
+#define SL_ERROR_HI_PMR_SIZE		0xc0008015
+#define SL_ERROR_LO_PMR_BASE		0xc0008016
+#define SL_ERROR_LO_PMR_MLE		0xc0008017
+#define SL_ERROR_INITRD_TOO_BIG		0xc0008018
+#define SL_ERROR_HEAP_ZERO_OFFSET	0xc0008019
+#define SL_ERROR_WAKE_BLOCK_TOO_SMALL	0xc000801a
+#define SL_ERROR_MLE_BUFFER_OVERLAP	0xc000801b
+#define SL_ERROR_BUFFER_BEYOND_PMR	0xc000801c
+#define SL_ERROR_OS_SINIT_BAD_VERSION	0xc000801d
+#define SL_ERROR_EVENTLOG_MAP		0xc000801e
+#define SL_ERROR_TPM_NUMBER_ALGS	0xc000801f
+#define SL_ERROR_TPM_UNKNOWN_DIGEST	0xc0008020
+#define SL_ERROR_TPM_INVALID_EVENT	0xc0008021
+#define SL_ERROR_INVALID_SLRT		0xc0008022
+#define SL_ERROR_SLRT_MISSING_ENTRY	0xc0008023
+#define SL_ERROR_SLRT_MAP		0xc0008024
+
+/*
+ * Secure Launch Defined Limits
+ */
+#define TXT_MAX_CPUS		512
+#define TXT_BOOT_STACK_SIZE	128
+
+/*
+ * Secure Launch event log entry type. The TXT specification defines the
+ * base event value as 0x400 for DRTM values.
+ */
+#define TXT_EVTYPE_BASE			0x400
+#define TXT_EVTYPE_SLAUNCH		(TXT_EVTYPE_BASE + 0x102)
+#define TXT_EVTYPE_SLAUNCH_START	(TXT_EVTYPE_BASE + 0x103)
+#define TXT_EVTYPE_SLAUNCH_END		(TXT_EVTYPE_BASE + 0x104)
+
+/*
+ * Measured Launch PCRs
+ */
+#define SL_DEF_DLME_DETAIL_PCR17	17
+#define SL_DEF_DLME_AUTHORITY_PCR18	18
+#define SL_ALT_DLME_AUTHORITY_PCR19	19
+#define SL_ALT_DLME_DETAIL_PCR20	20
+
+/*
+ * MLE scratch area offsets
+ */
+#define SL_SCRATCH_AP_EBX		0
+#define SL_SCRATCH_AP_JMP_OFFSET	4
+#define SL_SCRATCH_AP_STACKS_OFFSET	8
+
+#ifndef __ASSEMBLY__
+
+#include <linux/io.h>
+#include <linux/tpm.h>
+#include <linux/tpm_eventlog.h>
+
+/*
+ * Secure Launch AP stack and monitor block
+ */
+struct sl_ap_stack_and_monitor {
+	u32 monitor;
+	u32 cache_pad[15];
+	u32 stack_pad[15];
+	u32 apicid;
+} __packed;
+
+/*
+ * Secure Launch AP wakeup information fetched in SMP boot code.
+ */
+struct sl_ap_wake_info {
+	u32 ap_wake_block;
+	u32 ap_wake_block_size;
+	u32 ap_jmp_offset;
+	u32 ap_stacks_offset;
+};
+
+/*
+ * TXT heap extended data elements.
+ */
+struct txt_heap_ext_data_element {
+	u32 type;
+	u32 size;
+	/* Data */
+} __packed;
+
+#define TXT_HEAP_EXTDATA_TYPE_END			0
+
+struct txt_heap_end_element {
+	u32 type;
+	u32 size;
+} __packed;
+
+#define TXT_HEAP_EXTDATA_TYPE_TPM_EVENT_LOG_PTR		5
+
+struct txt_heap_event_log_element {
+	u64 event_log_phys_addr;
+} __packed;
+
+#define TXT_HEAP_EXTDATA_TYPE_EVENT_LOG_POINTER2_1	8
+
+struct txt_heap_event_log_pointer2_1_element {
+	u64 phys_addr;
+	u32 allocated_event_container_size;
+	u32 first_record_offset;
+	u32 next_record_offset;
+} __packed;
+
+/*
+ * Secure Launch defined OS/MLE TXT Heap table
+ */
+struct txt_os_mle_data {
+	u32 version;
+	u32 boot_params_addr;
+	u64 slrt;
+	u64 txt_info;
+	u32 ap_wake_block;
+	u32 ap_wake_block_size;
+	u8 mle_scratch[64];
+} __packed;
+
+/*
+ * TXT specification defined BIOS data TXT Heap table
+ */
+struct txt_bios_data {
+	u32 version; /* Currently 5 for TPM 1.2 and 6 for TPM 2.0 */
+	u32 bios_sinit_size;
+	u64 reserved1;
+	u64 reserved2;
+	u32 num_logical_procs;
+	/* Versions >= 5 with updates in version 6 */
+	u32 sinit_flags;
+	u32 mle_flags;
+	/* Versions >= 4 */
+	/* Ext Data Elements */
+} __packed;
+
+/*
+ * TXT specification defined OS/SINIT TXT Heap table
+ */
+struct txt_os_sinit_data {
+	u32 version; /* Currently 6 for TPM 1.2 and 7 for TPM 2.0 */
+	u32 flags;
+	u64 mle_ptab;
+	u64 mle_size;
+	u64 mle_hdr_base;
+	u64 vtd_pmr_lo_base;
+	u64 vtd_pmr_lo_size;
+	u64 vtd_pmr_hi_base;
+	u64 vtd_pmr_hi_size;
+	u64 lcp_po_base;
+	u64 lcp_po_size;
+	u32 capabilities;
+	/* Version = 5 */
+	u64 efi_rsdt_ptr;
+	/* Versions >= 6 */
+	/* Ext Data Elements */
+} __packed;
+
+/*
+ * TXT specification defined SINIT/MLE TXT Heap table
+ */
+struct txt_sinit_mle_data {
+	u32 version;             /* Current values are 6 through 9 */
+	/* Versions <= 8 */
+	u8 bios_acm_id[20];
+	u32 edx_senter_flags;
+	u64 mseg_valid;
+	u8 sinit_hash[20];
+	u8 mle_hash[20];
+	u8 stm_hash[20];
+	u8 lcp_policy_hash[20];
+	u32 lcp_policy_control;
+	/* Versions >= 7 */
+	u32 rlp_wakeup_addr;
+	u32 reserved;
+	u32 num_of_sinit_mdrs;
+	u32 sinit_mdrs_table_offset;
+	u32 sinit_vtd_dmar_table_size;
+	u32 sinit_vtd_dmar_table_offset;
+	/* Versions >= 8 */
+	u32 processor_scrtm_status;
+	/* Versions >= 9 */
+	/* Ext Data Elements */
+} __packed;
+
+/*
+ * TXT data reporting structure for memory types
+ */
+struct txt_sinit_memory_descriptor_record {
+	u64 address;
+	u64 length;
+	u8 type;
+	u8 reserved[7];
+} __packed;
+
+/*
+ * TXT data structure used by a responsive local processor (RLP) to start
+ * execution in response to a GETSEC[WAKEUP].
+ */
+struct smx_rlp_mle_join {
+	u32 rlp_gdt_limit;
+	u32 rlp_gdt_base;
+	u32 rlp_seg_sel;     /* cs (ds, es, ss are seg_sel+8) */
+	u32 rlp_entry_point; /* phys addr */
+} __packed;
+
+/*
+ * TPM event log structures defined in both the TXT specification and
+ * the TCG documentation.
+ */
+#define TPM12_EVTLOG_SIGNATURE "TXT Event Container"
+
+struct tpm12_event_log_header {
+	char signature[20];
+	char reserved[12];
+	u8 container_ver_major;
+	u8 container_ver_minor;
+	u8 pcr_event_ver_major;
+	u8 pcr_event_ver_minor;
+	u32 container_size;
+	u32 pcr_events_offset;
+	u32 next_event_offset;
+	/* PCREvents[] */
+} __packed;
+
+/*
+ * Functions to extract data from the Intel TXT Heap Memory. The layout
+ * of the heap is as follows:
+ *  +----------------------------+
+ *  | Size Bios Data table (u64) |
+ *  +----------------------------+
+ *  | Bios Data table            |
+ *  +----------------------------+
+ *  | Size OS MLE table (u64)    |
+ *  +----------------------------+
+ *  | OS MLE table               |
+ *  +--------------------------- +
+ *  | Size OS SINIT table (u64)  |
+ *  +----------------------------+
+ *  | OS SINIT table             |
+ *  +----------------------------+
+ *  | Size SINIT MLE table (u64) |
+ *  +----------------------------+
+ *  | SINIT MLE table            |
+ *  +----------------------------+
+ *
+ *  NOTE: the table size fields include the 8 byte size field itself.
+ */
+static inline u64 txt_bios_data_size(void *heap)
+{
+	return *((u64 *)heap);
+}
+
+static inline void *txt_bios_data_start(void *heap)
+{
+	return heap + sizeof(u64);
+}
+
+static inline u64 txt_os_mle_data_size(void *heap)
+{
+	return *((u64 *)(heap + txt_bios_data_size(heap)));
+}
+
+static inline void *txt_os_mle_data_start(void *heap)
+{
+	return heap + txt_bios_data_size(heap) + sizeof(u64);
+}
+
+static inline u64 txt_os_sinit_data_size(void *heap)
+{
+	return *((u64 *)(heap + txt_bios_data_size(heap) +
+			txt_os_mle_data_size(heap)));
+}
+
+static inline void *txt_os_sinit_data_start(void *heap)
+{
+	return heap + txt_bios_data_size(heap) +
+		txt_os_mle_data_size(heap) + sizeof(u64);
+}
+
+static inline u64 txt_sinit_mle_data_size(void *heap)
+{
+	return *((u64 *)(heap + txt_bios_data_size(heap) +
+			txt_os_mle_data_size(heap) +
+			txt_os_sinit_data_size(heap)));
+}
+
+static inline void *txt_sinit_mle_data_start(void *heap)
+{
+	return heap + txt_bios_data_size(heap) +
+		txt_os_mle_data_size(heap) +
+		txt_os_sinit_data_size(heap) + sizeof(u64);
+}
+
+/*
+ * TPM event logging functions.
+ */
+static inline struct txt_heap_event_log_pointer2_1_element*
+tpm20_find_log2_1_element(struct txt_os_sinit_data *os_sinit_data)
+{
+	struct txt_heap_ext_data_element *ext_elem;
+
+	/* The extended element array as at the end of this table */
+	ext_elem = (struct txt_heap_ext_data_element *)
+		((u8 *)os_sinit_data + sizeof(struct txt_os_sinit_data));
+
+	while (ext_elem->type != TXT_HEAP_EXTDATA_TYPE_END) {
+		if (ext_elem->type ==
+		    TXT_HEAP_EXTDATA_TYPE_EVENT_LOG_POINTER2_1) {
+			return (struct txt_heap_event_log_pointer2_1_element *)
+				((u8 *)ext_elem +
+					sizeof(struct txt_heap_ext_data_element));
+		}
+		ext_elem =
+			(struct txt_heap_ext_data_element *)
+			((u8 *)ext_elem + ext_elem->size);
+	}
+
+	return NULL;
+}
+
+static inline int tpm12_log_event(void *evtlog_base, u32 evtlog_size,
+				  u32 event_size, void *event)
+{
+	struct tpm12_event_log_header *evtlog =
+		(struct tpm12_event_log_header *)evtlog_base;
+
+	if (memcmp(evtlog->signature, TPM12_EVTLOG_SIGNATURE,
+		   sizeof(TPM12_EVTLOG_SIGNATURE)))
+		return -EINVAL;
+
+	if (evtlog->container_size > evtlog_size)
+		return -EINVAL;
+
+	if (evtlog->next_event_offset + event_size > evtlog->container_size)
+		return -E2BIG;
+
+	memcpy(evtlog_base + evtlog->next_event_offset, event, event_size);
+	evtlog->next_event_offset += event_size;
+
+	return 0;
+}
+
+static inline int tpm20_log_event(struct txt_heap_event_log_pointer2_1_element *elem,
+				  void *evtlog_base, u32 evtlog_size,
+				  u32 event_size, void *event)
+{
+	struct tcg_pcr_event *header =
+		(struct tcg_pcr_event *)evtlog_base;
+
+	/* Has to be at least big enough for the signature */
+	if (header->event_size < sizeof(TCG_SPECID_SIG))
+		return -EINVAL;
+
+	if (memcmp((u8 *)header + sizeof(struct tcg_pcr_event),
+		   TCG_SPECID_SIG, sizeof(TCG_SPECID_SIG)))
+		return -EINVAL;
+
+	if (elem->allocated_event_container_size > evtlog_size)
+		return -EINVAL;
+
+	if (elem->next_record_offset + event_size >
+	    elem->allocated_event_container_size)
+		return -E2BIG;
+
+	memcpy(evtlog_base + elem->next_record_offset, event, event_size);
+	elem->next_record_offset += event_size;
+
+	return 0;
+}
+
+/*
+ * External functions avalailable in mainline kernel.
+ */
+void slaunch_setup_txt(void);
+void slaunch_fixup_jump_vector(void);
+u32 slaunch_get_flags(void);
+struct sl_ap_wake_info *slaunch_get_ap_wake_info(void);
+struct acpi_table_header *slaunch_get_dmar_table(struct acpi_table_header *dmar);
+void __noreturn slaunch_txt_reset(void __iomem *txt,
+					 const char *msg, u64 error);
+extern void slaunch_finalize(int do_sexit);
+
+#endif /* !__ASSEMBLY */
+
+#else
+
+static inline void slaunch_setup_txt(void)
+{
+}
+
+static inline void slaunch_fixup_jump_vector(void)
+{
+}
+
+static inline u32 slaunch_get_flags(void)
+{
+	return 0;
+}
+
+static inline struct acpi_table_header *slaunch_get_dmar_table(struct acpi_table_header *dmar)
+{
+	return dmar;
+}
+
+static inline void slaunch_finalize(int do_sexit)
+{
+}
+
+#endif /* !IS_ENABLED(CONFIG_SECURE_LAUNCH) */
+
+#endif /* _LINUX_SLAUNCH_H */
Signature · 1 line
2.39.3
maintainer
#21 lore raw

Re: [PATCH v9 05/19] x86: Secure Launch main header file

Quoted 4 lines · Ross Philipson

Introduce the main Secure Launch header file used in the early SL stub and the early setup code.

Right and anything AMD specific should also have legit references. I actually always compare to the spec when I review, so not just nitpicking really.

I'm actually bit confused, is this usable both Intel and AMD in the current state? Might be just that have not had time follow this for some time.

BR, Jarkko

#22 lore raw

Re: [PATCH v9 05/19] x86: Secure Launch main header file

Quoted 6 lines · Jarkko Sakkinen
Quoted 4 lines · Ross Philipson

Introduce the main Secure Launch header file used in the early SL stub and the early setup code.

Quoted 7 lines

Right and anything AMD specific should also have legit references. I actually always compare to the spec when I review, so not just nitpicking really.

I'm actually bit confused, is this usable both Intel and AMD in the current state? Might be just that have not had time follow this for some time.

This header file mostly has TXT/Intel specific definitions in it right now but that is just because TXT is the first target architecture. I am working on the AMD side of things as we speak and yes, AMD specific definitions will go in here and later ARM specific definitions too.

If you would like to see say a comment block with links to relevant specifications in this header file, that can be done and they will be added as new support is added.

Thanks
Ross

Quoted 3 lines

BR, Jarkko

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#23 lore raw

PATCH v9 6/19 x86: Add early SHA-1 support for Secure Launch early measurements

In reply to Ross Philipson

From: "Daniel P. Smith" <[email protected]>

For better or worse, Secure Launch needs SHA-1 and SHA-256. The choice of hashes used lie with the platform firmware, not with software, and is often outside of the users control.

Even if we'd prefer to use SHA-256-only, if firmware elected to start us with the SHA-1 and SHA-256 backs active, we still need SHA-1 to parse the TPM event log thus far, and deliberately cap the SHA-1 PCRs in order to safely use SHA-256 for everything else.

The SHA-1 code here has its origins in the code from the main kernel:

commit c4d5b9ffa31f ("crypto: sha1 - implement base layer for SHA-1")

A modified version of this code was introduced to the lib/crypto/sha1.c to bring it in line with the SHA-256 code and allow it to be pulled into the setup kernel in the same manner as SHA-256 is.

---
 arch/x86/boot/compressed/Makefile     |  2 +
 arch/x86/boot/compressed/early_sha1.c | 12 ++++
 include/crypto/sha1.h                 |  1 +
 lib/crypto/sha1.c                     | 81 +++++++++++++++++++++++++++
 4 files changed, 96 insertions(+)
 create mode 100644 arch/x86/boot/compressed/early_sha1.c
arch/x86/boot/compressed/Makefile +2 -0
index e9522c6893be..3307ebef4e1b 100644
@@ -118,6 +118,8 @@ vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi.o
 vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_mixed.o
 vmlinux-objs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
 
+vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o
+
 $(obj)/vmlinux: $(vmlinux-objs-y) FORCE
 	$(call if_changed,ld)
 
arch/x86/boot/compressed/early_sha1.c new file +12 -0
new file mode 100644
index 000000000000..8a9b904a73ab
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2024 Apertus Solutions, LLC.
+ */
+
+#include <linux/init.h>
+#include <linux/linkage.h>
+#include <linux/string.h>
+#include <asm/boot.h>
+#include <asm/unaligned.h>
+
+#include "../../../../lib/crypto/sha1.c"
include/crypto/sha1.h +1 -0
index 044ecea60ac8..d715dd5332e1 100644
@@ -42,5 +42,6 @@ extern int crypto_sha1_finup(struct shash_desc *desc, const u8 *data,
 #define SHA1_WORKSPACE_WORDS	16
 void sha1_init(__u32 *buf);
 void sha1_transform(__u32 *digest, const char *data, __u32 *W);
+void sha1(const u8 *data, unsigned int len, u8 *out);
 
 #endif /* _CRYPTO_SHA1_H */
lib/crypto/sha1.c +81 -0

85 lines not on this page · raw message

Signature · 1 line
2.39.3
maintainer
#24 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 13 lines · Ross Philipson

From: "Daniel P. Smith" <[email protected]>

For better or worse, Secure Launch needs SHA-1 and SHA-256. The choice of hashes used lie with the platform firmware, not with software, and is often outside of the users control.

Even if we'd prefer to use SHA-256-only, if firmware elected to start us with the SHA-1 and SHA-256 backs active, we still need SHA-1 to parse the TPM event log thus far, and deliberately cap the SHA-1 PCRs in order to safely use SHA-256 for everything else.

The SHA-1 code here has its origins in the code from the main kernel:

Quoted 8 lines

commit c4d5b9ffa31f ("crypto: sha1 - implement base layer for SHA-1")

A modified version of this code was introduced to the lib/crypto/sha1.c to bring it in line with the SHA-256 code and allow it to be pulled into the setup kernel in the same manner as SHA-256 is.

Thanks. This explanation doesn't seem to have made it into the actual code or documentation. Can you please get it into a more permanent location?

Also, can you point to where the "deliberately cap the SHA-1 PCRs" thing happens in the code?

That paragraph is also phrased as a hypothetical, "Even if we'd prefer to use SHA-256-only". That implies that you do not, in fact, prefer SHA-256 only. Is that the case? Sure, maybe there are situations where you *have* to use SHA-1, but why would you not at least *prefer* SHA-256?

Quoted 10 lines

/*
* An implementation of SHA-1's compression function. Don't use in new code!
* You shouldn't be using SHA-1, and even if you *have* to use SHA-1, this isn't
* the correct way to hash something with SHA-1 (use crypto_shash instead). */
#define SHA1_DIGEST_WORDS (SHA1_DIGEST_SIZE / 4)
#define SHA1_WORKSPACE_WORDS 16
void sha1_init(__u32 *buf);
void sha1_transform(__u32 *digest, const char *data, __u32 *W); +void sha1(const u8 *data, unsigned int len, u8 *out);

Also, the comment above needs to be updated.

- Eric

#25 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 26 lines · Eric Biggers
Quoted 13 lines · Ross Philipson

From: "Daniel P. Smith" <[email protected]>

For better or worse, Secure Launch needs SHA-1 and SHA-256. The choice of hashes used lie with the platform firmware, not with software, and is often outside of the users control.

Even if we'd prefer to use SHA-256-only, if firmware elected to start us with the SHA-1 and SHA-256 backs active, we still need SHA-1 to parse the TPM event log thus far, and deliberately cap the SHA-1 PCRs in order to safely use SHA-256 for everything else.

The SHA-1 code here has its origins in the code from the main kernel:

Quoted 8 lines

commit c4d5b9ffa31f ("crypto: sha1 - implement base layer for SHA-1")

A modified version of this code was introduced to the lib/crypto/sha1.c to bring it in line with the SHA-256 code and allow it to be pulled into the setup kernel in the same manner as SHA-256 is.

Thanks. This explanation doesn't seem to have made it into the actual code or documentation. Can you please get it into a more permanent location?

Quoted 7 lines

Also, can you point to where the "deliberately cap the SHA-1 PCRs" thing happens in the code?

That paragraph is also phrased as a hypothetical, "Even if we'd prefer to use SHA-256-only". That implies that you do not, in fact, prefer SHA-256 only. Is that the case? Sure, maybe there are situations where you *have* to use SHA-1, but why would you not at least *prefer* SHA-256?

Yes. Please prefer to use SHA-256.

Have you considered implementing I think it is SHA1-DC (as git has) that is compatible with SHA1 but blocks the known class of attacks where sha1 is actively broken at this point?

No offense to your Trenchboot project but my gut says that anything new relying on SHA-1 is probably a bad joke at this point.

Firmware can most definitely be upgraded and if the goal is a more secure boot the usual backwards compatibility concerns for supporting old firmware really don't apply.

Perhaps hide all of the SHA-1 stuff behind CONFIG_TRENCHBOOT_PROTOTYPE or something like that to make it clear that SHA-1 is not appropriate for any kind of production deployment and is only good for prototyping your implementation until properly implemented firmware comes along.

Eric

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#26 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 36 lines · Eric W. Biederman
Quoted 26 lines · Eric Biggers
Quoted 13 lines · Ross Philipson

From: "Daniel P. Smith" <[email protected]>

For better or worse, Secure Launch needs SHA-1 and SHA-256. The choice of hashes used lie with the platform firmware, not with software, and is often outside of the users control.

Even if we'd prefer to use SHA-256-only, if firmware elected to start us with the SHA-1 and SHA-256 backs active, we still need SHA-1 to parse the TPM event log thus far, and deliberately cap the SHA-1 PCRs in order to safely use SHA-256 for everything else.

The SHA-1 code here has its origins in the code from the main kernel:

Quoted 8 lines

commit c4d5b9ffa31f ("crypto: sha1 - implement base layer for SHA-1")

A modified version of this code was introduced to the lib/crypto/sha1.c to bring it in line with the SHA-256 code and allow it to be pulled into the setup kernel in the same manner as SHA-256 is.

Thanks. This explanation doesn't seem to have made it into the actual code or documentation. Can you please get it into a more permanent location?

Quoted 7 lines

Also, can you point to where the "deliberately cap the SHA-1 PCRs" thing happens in the code?

That paragraph is also phrased as a hypothetical, "Even if we'd prefer to use SHA-256-only". That implies that you do not, in fact, prefer SHA-256 only. Is that the case? Sure, maybe there are situations where you *have* to use SHA-1, but why would you not at least *prefer* SHA-256?

Quoted 5 lines

Yes. Please prefer to use SHA-256.

Have you considered implementing I think it is SHA1-DC (as git has) that is compatible with SHA1 but blocks the known class of attacks where sha1 is actively broken at this point?

We are using the kernel's implementation, addressing what the kernel provides is beyond our efforts. Perhaps someone who is interested in improving the kernel's SHA1 could submit a patch implementing/replacing it with SHA1-DC, as I am sure the maintainers would welcome the help.

v/r,
dps

#27 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 9 lines · Daniel P. Smith
Quoted 8 lines · Eric W. Biederman
Quoted 4 lines · Eric Biggers

That paragraph is also phrased as a hypothetical, "Even if we'd prefer to use SHA-256-only". That implies that you do not, in fact, prefer SHA-256 only. Is that the case? Sure, maybe there are situations where you *have* to use SHA-1, but why would you not at least *prefer* SHA-256?

Yes. Please prefer to use SHA-256.

Quoted 8 lines
Quoted 3 lines

Have you considered implementing I think it is SHA1-DC (as git has) that is compatible with SHA1 but blocks the known class of attacks where sha1 is actively broken at this point?

We are using the kernel's implementation, addressing what the kernel provides is beyond our efforts. Perhaps someone who is interested in improving the kernel's SHA1 could submit a patch implementing/replacing it with SHA1-DC, as I am sure the maintainers would welcome the help.

Well, someone who is interested to get his "secure" code merged should have a vested interested to have a non-broken SHA1 implementation if there is a sensible requirement to use SHA1 in that new "secure" code, no?

Just for the record. The related maintainers can rightfully decide to reject known broken "secure" code on a purely technical argument.

Thanks,

tglx

maintainer
#28 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 10 lines · Thomas Gleixner
Quoted 9 lines · Daniel P. Smith
Quoted 8 lines · Eric W. Biederman
Quoted 4 lines · Eric Biggers

That paragraph is also phrased as a hypothetical, "Even if we'd prefer to use SHA-256-only". That implies that you do not, in fact, prefer SHA-256 only. Is that the case? Sure, maybe there are situations where you *have* to use SHA-1, but why would you not at least *prefer* SHA-256?

Yes. Please prefer to use SHA-256.

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

Have you considered implementing I think it is SHA1-DC (as git has) that is compatible with SHA1 but blocks the known class of attacks where sha1 is actively broken at this point?

We are using the kernel's implementation, addressing what the kernel provides is beyond our efforts. Perhaps someone who is interested in improving the kernel's SHA1 could submit a patch implementing/replacing it with SHA1-DC, as I am sure the maintainers would welcome the help.

Git also has a bit more wide than secure launch, and the timeline is also completely different. Git maintains legacy, while has also introduced SHA-256 support in 2018. This as a new feature in the kernel stack.

The purpose of SHA1-DC has obviously been to extend the lifespan, not fix SHA-1.

Linux will be better of not adding anything new related to SHA-1 or TPM 1.2. They still have a maintenance cost and I think that time would be better spent of for almost anything else (starting from taking your trashes out or boiling coffee) ;-)

Quoted 6 lines

Well, someone who is interested to get his "secure" code merged should have a vested interested to have a non-broken SHA1 implementation if there is a sensible requirement to use SHA1 in that new "secure" code, no?

Quoted 6 lines

Just for the record. The related maintainers can rightfully decide to reject known broken "secure" code on a purely technical argument.

Thanks,

tglx

BR, Jarkko

#29 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

In reply to Thomas Gleixner
Quoted 12 lines · Thomas Gleixner
Quoted 11 lines · Daniel P. Smith
Quoted 10 lines · Eric W. Biederman
Quoted 4 lines · Eric Biggers

That paragraph is also phrased as a hypothetical, "Even if we'd prefer to use SHA-256-only". That implies that you do not, in fact, prefer SHA-256 only. Is that the case? Sure, maybe there are situations where you *have* to use SHA-1, but why would you not at least *prefer* SHA-256?

Yes. Please prefer to use SHA-256.

Have you considered implementing I think it is SHA1-DC (as git has) that is compatible with SHA1 but blocks the known class of attacks where sha1 is actively broken at this point?

Quoted 8 lines
Quoted 4 lines

We are using the kernel's implementation, addressing what the kernel provides is beyond our efforts. Perhaps someone who is interested in improving the kernel's SHA1 could submit a patch implementing/replacing it with SHA1-DC, as I am sure the maintainers would welcome the help.

Well, someone who is interested to get his "secure" code merged should have a vested interested to have a non-broken SHA1 implementation if there is a sensible requirement to use SHA1 in that new "secure" code, no?

No.

The use of SHA-1 is necessary even on modern systems to avoid a vulnerability.

It is the platform, not Linux, which decides which TPM PCR banks are active.

Linux *must* have an algorithm for every active bank (which is the platform's choice), even if the single thing it intends to do is cap the bank and use better ones.

Capping a bank requires updating the TPM Log without corrupting it, which requires a hash calculation of the correct type for the bank.

~Andrew

maintainer
#30 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 24 lines · Andrew Cooper
Quoted 12 lines · Thomas Gleixner
Quoted 11 lines · Daniel P. Smith
Quoted 10 lines · Eric W. Biederman
Quoted 4 lines · Eric Biggers

That paragraph is also phrased as a hypothetical, "Even if we'd prefer to use SHA-256-only". That implies that you do not, in fact, prefer SHA-256 only. Is that the case? Sure, maybe there are situations where you *have* to use SHA-1, but why would you not at least *prefer* SHA-256?

Yes. Please prefer to use SHA-256.

Have you considered implementing I think it is SHA1-DC (as git has) that is compatible with SHA1 but blocks the known class of attacks where sha1 is actively broken at this point?

Quoted 8 lines
Quoted 4 lines

We are using the kernel's implementation, addressing what the kernel provides is beyond our efforts. Perhaps someone who is interested in improving the kernel's SHA1 could submit a patch implementing/replacing it with SHA1-DC, as I am sure the maintainers would welcome the help.

Well, someone who is interested to get his "secure" code merged should have a vested interested to have a non-broken SHA1 implementation if there is a sensible requirement to use SHA1 in that new "secure" code, no?

No.

Quoted 8 lines

The use of SHA-1 is necessary even on modern systems to avoid a vulnerability.

It is the platform, not Linux, which decides which TPM PCR banks are active.

Linux *must* have an algorithm for every active bank (which is the platform's choice), even if the single thing it intends to do is cap the bank and use better ones.

For (any) non-legacy features we can choose, which choices we choose to support, and which we do not. This is not an oppositive view just saying how it is, and platforms set of choices is not a selling argument.

Quoted 5 lines

Capping a bank requires updating the TPM Log without corrupting it, which requires a hash calculation of the correct type for the bank.

~Andrew

BR, Jarkko

#31 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 3 lines · Jarkko Sakkinen

For (any) non-legacy features we can choose, which choices we choose to support, and which we do not. This is not an oppositive view just saying how it is, and platforms set of choices is not a selling argument.

NIST still permits the use of SHA-1 until 2030, and the most significant demonstrated weaknesses in it don't seem applicable to the use case here. We certainly shouldn't encourage any new uses of it, and anyone who's able to use SHA-2 should be doing that instead, but it feels like people are arguing about not supporting hardware that exists in the real world for vibes reasons rather than it being a realistically attackable weakness (and if we really *are* that concerned about SHA-1, why are we still supporting TPM 1.2 at all?)

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

maintainer
#32 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 6 lines · Matthew Garrett
Quoted 3 lines · Jarkko Sakkinen

For (any) non-legacy features we can choose, which choices we choose to support, and which we do not. This is not an oppositive view just saying how it is, and platforms set of choices is not a selling argument.

Quoted 8 lines

NIST still permits the use of SHA-1 until 2030, and the most significant demonstrated weaknesses in it don't seem applicable to the use case here. We certainly shouldn't encourage any new uses of it, and anyone who's able to use SHA-2 should be doing that instead, but it feels like people are arguing about not supporting hardware that exists in the real world for vibes reasons rather than it being a realistically attackable weakness (and if we really *are* that concerned about SHA-1, why are we still supporting TPM 1.2 at all?)

We are life-supporting TPM 1.2 as long as necessary but neither the support is extended nor new features will gain TPM 1.2 support. So that is at least my policy for that feature.

BR, Jarkko

#33 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 16 lines · Jarkko Sakkinen
Quoted 6 lines · Matthew Garrett
Quoted 3 lines · Jarkko Sakkinen

For (any) non-legacy features we can choose, which choices we choose to support, and which we do not. This is not an oppositive view just saying how it is, and platforms set of choices is not a selling argument.

Quoted 8 lines

NIST still permits the use of SHA-1 until 2030, and the most significant demonstrated weaknesses in it don't seem applicable to the use case here. We certainly shouldn't encourage any new uses of it, and anyone who's able to use SHA-2 should be doing that instead, but it feels like people are arguing about not supporting hardware that exists in the real world for vibes reasons rather than it being a realistically attackable weakness (and if we really *are* that concerned about SHA-1, why are we still supporting TPM 1.2 at all?)

Quoted 3 lines

We are life-supporting TPM 1.2 as long as necessary but neither the support is extended nor new features will gain TPM 1.2 support. So that is at least my policy for that feature.

But the fact that we support it and provide no warning labels is a pretty clear indication that we're not actively trying to prevent people from using SHA-1 in the general case. Why is this a different case? Failing to support it actually opens an entire separate set of footgun opportunities in terms of the SHA-1 banks now being out of sync with the SHA-2 ones, so either way we're leaving people open to making poor choices.

maintainer
#34 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 21 lines · Matthew Garrett
Quoted 16 lines · Jarkko Sakkinen
Quoted 6 lines · Matthew Garrett
Quoted 3 lines · Jarkko Sakkinen

For (any) non-legacy features we can choose, which choices we choose to support, and which we do not. This is not an oppositive view just saying how it is, and platforms set of choices is not a selling argument.

Quoted 8 lines

NIST still permits the use of SHA-1 until 2030, and the most significant demonstrated weaknesses in it don't seem applicable to the use case here. We certainly shouldn't encourage any new uses of it, and anyone who's able to use SHA-2 should be doing that instead, but it feels like people are arguing about not supporting hardware that exists in the real world for vibes reasons rather than it being a realistically attackable weakness (and if we really *are* that concerned about SHA-1, why are we still supporting TPM 1.2 at all?)

Quoted 3 lines

We are life-supporting TPM 1.2 as long as necessary but neither the support is extended nor new features will gain TPM 1.2 support. So that is at least my policy for that feature.

Quoted 7 lines

But the fact that we support it and provide no warning labels is a pretty clear indication that we're not actively trying to prevent people from using SHA-1 in the general case. Why is this a different case? Failing to support it actually opens an entire separate set of footgun opportunities in terms of the SHA-1 banks now being out of sync with the SHA-2 ones, so either way we're leaving people open to making poor choices.

This is a fair and enclosing argument. I get where you are coming from now. Please as material for the commit message.

BR, Jarkko

#35 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

In reply to Thomas Gleixner
Quoted 24 lines · Thomas Gleixner
Quoted 9 lines · Daniel P. Smith
Quoted 8 lines · Eric W. Biederman
Quoted 4 lines · Eric Biggers

That paragraph is also phrased as a hypothetical, "Even if we'd prefer to use SHA-256-only". That implies that you do not, in fact, prefer SHA-256 only. Is that the case? Sure, maybe there are situations where you *have* to use SHA-1, but why would you not at least *prefer* SHA-256?

Yes. Please prefer to use SHA-256.

Quoted 8 lines
Quoted 3 lines

Have you considered implementing I think it is SHA1-DC (as git has) that is compatible with SHA1 but blocks the known class of attacks where sha1 is actively broken at this point?

We are using the kernel's implementation, addressing what the kernel provides is beyond our efforts. Perhaps someone who is interested in improving the kernel's SHA1 could submit a patch implementing/replacing it with SHA1-DC, as I am sure the maintainers would welcome the help.

Well, someone who is interested to get his "secure" code merged should have a vested interested to have a non-broken SHA1 implementation if there is a sensible requirement to use SHA1 in that new "secure" code, no?

Quoted 7 lines

Just for the record. The related maintainers can rightfully decide to reject known broken "secure" code on a purely technical argument.

Thanks,

tglx

There is one simple question, does allowing the Secure Launch code to record SHA1 measurements make the system insecure, and the answer is absolutely not.

The role of the Secure Launch code base in the context of the larger launch process is to function as observer. Within this role, its only responsibility is continuing the trust chain(s) that were started by the CPU/Hardware. It does so by measuring the components and configuration it is responsible for loading and applying, i.e. in TCG parlance, it is continuing the construction of the transitive trust for the system. In this aspect, the only degradation of security that can affect the kernel's role is whether all the necessary entities are safely measured and not what algorithms are used.

If the system integrator, whether that be the OEM, your employer, the distro maintainer, the system administrator, or the end user, configures the DL preamble to only use SHA1 or used older hardware that has a TPM1.2, then they are accepting the risk it creates in their solution. In fact, a greater threat to the security of the launch is the misconfiguration of the IOMMU, which risks the kernel's ability to safely make measurements, as compared to the use of SHA1. Yet it was insisted in past reviews that we allow the user to specify an incorrect IOMMU policy.

In the end, the "security" of an RTM solution is how and what measurements are used to assess the health of a system. Thus bringing it back to the opening question, if SHA1 measurements are made but not used, i.e. the attestation enforcement only uses SHA2, then it has zero impact on the security of the system.

Another fact to consider is that the current Intel's TXT MLE specification dictates SHA1 as a valid configuration. Secure Launch's use of SHA1 is therefore to comply with Intel's specification for TXT. And like the IOMMU situation, having the option available allows the user to determine how they ultimately want to integrate Secure Launch into their integrity management. And because Secure Launch will only attempt SHA1 if it was in the TXT configuration, when either Intel removes SHA1 from the MLE specification or firmware manufactures begin disabling the SHA1 banks, this will obviously mean that Secure Launch will not produce SHA1 measurements.

On a side note, with my remote attestation hat on, the SHA1 measurements can in fact be extremely useful. If an attestation was made containing both SHA1 and SHA2 chains, and the SHA1 of an event was correct but the SHA2 was not, either a natural collision happened or someone maliciously caused a collision. The former has an extremely low probability, while the latter is highly probable.

Thus, with this information alone, it is possible to make the reasonable determination the device is compromised. Whereas if both hashes are mismatched, without any additional information it is equally probable of either misconfiguration or compromise. And to state the obvious, with only SHA2, further information is needed to distinguish between misconfiguration and compromise.

V/r,
Daniel P. Smith

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

maintainer
#36 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Coming back to this old thread after having spent some time playing with the code:

On Thu, 22 Aug 2024, at 20:29, Daniel P. Smith wrote:

<selective snip>

Quoted 3 lines

Another fact to consider is that the current Intel's TXT MLE specification dictates SHA1 as a valid configuration. Secure Launch's use of SHA1 is therefore to comply with Intel's specification for TXT.

As I understand the Intel TXT spec and the code:

- TPM 1.2 is no longer supported by the TXT spec (since 2023)
- TPM 1.2 is not supported by your GRUB implementation
- in TPM 2.0 mode, SHA1 is only supported by the TXT spec if it is the /only/ algo supported by the TPM
- the proposed kernel implementation ignores any SHA-384 and SM3-256 PCR banks if they are active, and caps them using a { 1, 0, ... } fake digest.

So apologies for being slow, but I still struggle to understand why it is so important to have a SHA-1 implementation to cap those PCRs. Is it just to support systems with a TPM 2.0 that only has SHA-1 banks enabled?

Assuming that this code will get merged this year, it will be in a LTS branch by 2027, by which time distros like Debian will pick it up.

I fully understand that this code has lived out-of-tree for more than a decade, and you likely prefer to get everything upstream that your current users may be relying on. But for Linux, this is a new feature, and merging code now that is basically obsolete on day 1 is not something we should entertain imo.

(and apologies for re-opening yet another can of worms - I assure you I am trying to be constructive here)

Signature · 1 line
Ard.
#37 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 16 lines · Ard Biesheuvel

Coming back to this old thread after having spent some time playing with the code:

On Thu, 22 Aug 2024, at 20:29, Daniel P. Smith wrote:

<selective snip>

Quoted 3 lines

Another fact to consider is that the current Intel's TXT MLE specification dictates SHA1 as a valid configuration. Secure Launch's use of SHA1 is therefore to comply with Intel's specification for TXT.

As I understand the Intel TXT spec and the code:

- TPM 1.2 is no longer supported by the TXT spec (since 2023)
- TPM 1.2 is not supported by your GRUB implementation
- in TPM 2.0 mode, SHA1 is only supported by the TXT spec if it is the /only/ algo supported by the TPM
- the proposed kernel implementation ignores any SHA-384 and SM3-256 PCR banks if they are active, and caps them using a { 1, 0, ... } fake digest.

Quoted 8 lines

So apologies for being slow, but I still struggle to understand why it is so important to have a SHA-1 implementation to cap those PCRs. Is it just to support systems with a TPM 2.0 that only has SHA-1 banks enabled?

Assuming that this code will get merged this year, it will be in a LTS branch by 2027, by which time distros like Debian will pick it up.

I fully understand that this code has lived out-of-tree for more than a decade, and you likely prefer to get everything upstream that your current users may be relying on. But for Linux, this is a new feature, and merging code now that is basically obsolete on day 1 is not something we should entertain imo.

(and apologies for re-opening yet another can of worms - I assure you I am trying to be constructive here)

I appreciate that you've got concerns, but I don't think these characterisations are fair.  Ultimately yes, we do want to support TPM 1.2 systems because we have users wanting a way off the out-of-tree patches.

3-year-old hardware is new enough to still be in full support from OEMs, and totally fair game for distro LTS's even at this juncture.

The TXT spec does not speak for what OEMs choose to support, nor for the TPM spec (where SHA1 is a mandatory algorithm for TPM 2.0), nor does it speak for other silicon vendors we're trying to support (there are AMD patches waiting for the interface to stop changing).

But lets ask the question the other way around.  What does refusing SHA1 gain?

AFAICT, there's no meaningful reduction in complexity in the early code; the TPM library speaks TPM 1.2 and 2.0.  There is a small reduction in binary size for not including SHA-1, but including it is no extra C because we reuse the library that already exists.

The cost is a failure to operate in some TXT configurations that exist on in-support CPUs, as well as older systems.

~Andrew

maintainer
#38 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 21 lines · Andrew Cooper
Quoted 20 lines · Ard Biesheuvel

Coming back to this old thread after having spent some time playing with the code:

On Thu, 22 Aug 2024, at 20:29, Daniel P. Smith wrote:

<selective snip>

Quoted 3 lines

Another fact to consider is that the current Intel's TXT MLE specification dictates SHA1 as a valid configuration. Secure Launch's use of SHA1 is therefore to comply with Intel's specification for TXT.

As I understand the Intel TXT spec and the code:

- TPM 1.2 is no longer supported by the TXT spec (since 2023)
- TPM 1.2 is not supported by your GRUB implementation
- in TPM 2.0 mode, SHA1 is only supported by the TXT spec if it is the /only/ algo supported by the TPM
- the proposed kernel implementation ignores any SHA-384 and SM3-256 PCR banks if they are active, and caps them using a { 1, 0, ... } fake digest.

So apologies for being slow, but I still struggle to understand why it is so important to have a SHA-1 implementation to cap those PCRs. Is it just to support systems with a TPM 2.0 that only has SHA-1 banks enabled?

Assuming that this code will get merged this year, it will be in a LTS branch by 2027, by which time distros like Debian will pick it up.

Quoted 9 lines
Quoted 4 lines

I fully understand that this code has lived out-of-tree for more than a decade, and you likely prefer to get everything upstream that your current users may be relying on. But for Linux, this is a new feature, and merging code now that is basically obsolete on day 1 is not something we should entertain imo.

(and apologies for re-opening yet another can of worms - I assure you I am trying to be constructive here)

I appreciate that you've got concerns, but I don't think these characterisations are fair.  Ultimately yes, we do want to support TPM 1.2 systems because we have users wanting a way off the out-of-tree patches.

That was my question, yes. So supporting TPM 1.2 as well as TPM 2.0 with only SHA-1 banks enabled is needed not to comply with the spec, but to support existing out-of-tree users. That needs to be called out, because what I am quoting above is a claim that it is needed to comply with the TXT MLE spec.

So the debate is really about whether or not such systems should be considered obsolete at this point.

And why is it ok to just cap active SHA-384 banks rather than perform the proper measurements?

Quoted 3 lines

3-year-old hardware is new enough to still be in full support from OEMs, and totally fair game for distro LTS's even at this juncture.

3-year old hardware that shipped with a TPM 1.2?

Quoted 6 lines

The TXT spec does not speak for what OEMs choose to support, nor for the TPM spec (where SHA1 is a mandatory algorithm for TPM 2.0), nor does it speak for other silicon vendors we're trying to support (there are AMD patches waiting for the interface to stop changing).

Quoted 8 lines

But lets ask the question the other way around.  What does refusing SHA1 gain?

AFAICT, there's no meaningful reduction in complexity in the early code; the TPM library speaks TPM 1.2 and 2.0.  There is a small reduction in binary size for not including SHA-1, but including it is no extra C because we reuse the library that already exists.

The complexity of the code is not the problem. The problem is that we'll be stuck supporting it long past the point where anyone is making meaningful use of it. I'm not keen on being on the receiving end of a bug report from your last remaining TPM 1.2 user, with no means whatsoever to test or reproduce.

Quoted 3 lines

The cost is a failure to operate in some TXT configurations that exist on in-support CPUs, as well as older systems.

Sure, some systems will be left behind. I just wonder how many of those would be upgrading to the v7.3 LTS kernel to begin with, 4 years after TPM 1.2 support was dropped from the TXT spec.

In any case, the code changes themselves look fine to me. I am more concerned about the long term maintenance: the x86 boot code is very complex, given how many boot methods it supports, and it is rather difficult to regression test all of those.

#39 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

In reply to Thomas Gleixner
Quoted 20 lines · Thomas Gleixner
Quoted 9 lines · Daniel P. Smith
Quoted 8 lines · Eric W. Biederman
Quoted 4 lines · Eric Biggers

That paragraph is also phrased as a hypothetical, "Even if we'd prefer to use SHA-256-only". That implies that you do not, in fact, prefer SHA-256 only. Is that the case? Sure, maybe there are situations where you *have* to use SHA-1, but why would you not at least *prefer* SHA-256?

Yes. Please prefer to use SHA-256.

Quoted 8 lines
Quoted 3 lines

Have you considered implementing I think it is SHA1-DC (as git has) that is compatible with SHA1 but blocks the known class of attacks where sha1 is actively broken at this point?

We are using the kernel's implementation, addressing what the kernel provides is beyond our efforts. Perhaps someone who is interested in improving the kernel's SHA1 could submit a patch implementing/replacing it with SHA1-DC, as I am sure the maintainers would welcome the help.

Quoted 8 lines

Well, someone who is interested to get his "secure" code merged should have a vested interested to have a non-broken SHA1 implementation if there is a sensible requirement to use SHA1 in that new "secure" code, no?

Just for the record. The related maintainers can rightfully decide to reject known broken "secure" code on a purely technical argument.

Wait, hold on a second.

SHA1-DC isn't SHA1. It's a different hash function that is mostly compatible with SHA1, is different on some inputs, and is maybe more secure. But the _whole point_ of using SHA1 in the TPM code (well, this really should be the whole point for new applications) is to correctly cap the SHA1 PCRs so we can correctly _turn them off_ in the best way without breaking compatibility with everything that might read the event log. I think that anyone suggesting using SHA1-DC for this purpose should give some actual analysis as to why they think it's an improvement, let alone even valid.

Ross et al, can you confirm that your code actually, at least by default and with a monstrous warning to anyone who tries to change the default, caps SHA1 PCRs if SHA256 is available? And then can we maybe all stop hassling the people trying to develop this series about the fact that they're doing their best with the obnoxious system that the TPM designers gave them?

Thanks,
Andy

#40 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 6 lines · Andy Lutomirski

Ross et al, can you confirm that your code actually, at least by default and with a monstrous warning to anyone who tries to change the default, caps SHA1 PCRs if SHA256 is available? And then can we maybe all stop hassling the people trying to develop this series about the fact that they're doing their best with the obnoxious system that the TPM designers gave them?

Presumably this would be dependent upon non-SHA1 banks being enabled?

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#41 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 11 lines · Matthew Garrett
Quoted 6 lines · Andy Lutomirski

Ross et al, can you confirm that your code actually, at least by default and with a monstrous warning to anyone who tries to change the default, caps SHA1 PCRs if SHA256 is available? And then can we maybe all stop hassling the people trying to develop this series about the fact that they're doing their best with the obnoxious system that the TPM designers gave them?

Presumably this would be dependent upon non-SHA1 banks being enabled?

Of course. It's also not immediately obvious to me what layer of the stack should be responsible for capping SHA1 PCRs. Should it be the kernel? Userspace?

It seems like a whole lot of people, for better or for worse, want to minimize the amount of code that even knows how to compute SHA1 hashes. I'm not personally convinced I agree with this strategy, but it is what it is. And maybe people would be happier if the default behavior of the kernel is to notice that SHA256 is available and then cap SHA1 before even asking user code's permission.

--Andy

#42 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

In reply to Andy Lutomirski

Hi Luto.

Quoted 32 lines · Andy Lutomirski
Quoted 20 lines · Thomas Gleixner
Quoted 9 lines · Daniel P. Smith
Quoted 8 lines · Eric W. Biederman
Quoted 4 lines · Eric Biggers

That paragraph is also phrased as a hypothetical, "Even if we'd prefer to use SHA-256-only". That implies that you do not, in fact, prefer SHA-256 only. Is that the case? Sure, maybe there are situations where you *have* to use SHA-1, but why would you not at least *prefer* SHA-256?

Yes. Please prefer to use SHA-256.

Quoted 8 lines
Quoted 3 lines

Have you considered implementing I think it is SHA1-DC (as git has) that is compatible with SHA1 but blocks the known class of attacks where sha1 is actively broken at this point?

We are using the kernel's implementation, addressing what the kernel provides is beyond our efforts. Perhaps someone who is interested in improving the kernel's SHA1 could submit a patch implementing/replacing it with SHA1-DC, as I am sure the maintainers would welcome the help.

Quoted 8 lines

Well, someone who is interested to get his "secure" code merged should have a vested interested to have a non-broken SHA1 implementation if there is a sensible requirement to use SHA1 in that new "secure" code, no?

Just for the record. The related maintainers can rightfully decide to reject known broken "secure" code on a purely technical argument.

Wait, hold on a second.

Quoted 9 lines

SHA1-DC isn't SHA1. It's a different hash function that is mostly compatible with SHA1, is different on some inputs, and is maybe more secure. But the _whole point_ of using SHA1 in the TPM code (well, this really should be the whole point for new applications) is to correctly cap the SHA1 PCRs so we can correctly _turn them off_ in the best way without breaking compatibility with everything that might read the event log. I think that anyone suggesting using SHA1-DC for this purpose should give some actual analysis as to why they think it's an improvement, let alone even valid.

I would say at a minimum it is to provide a means to cap the PCRs. Devices with TPM1.2 are still prevalent in the wild for which members of the TrenchBoot community support, and there are still valid (and secure) verification uses for SHA1 that I outlined in my previous response.

Quoted 6 lines

Ross et al, can you confirm that your code actually, at least by default and with a monstrous warning to anyone who tries to change the default, caps SHA1 PCRs if SHA256 is available? And then can we maybe all stop hassling the people trying to develop this series about the fact that they're doing their best with the obnoxious system that the TPM designers gave them?

Our goal is to keep control in the hands of the user, not making unilateral decisions on their behalf. In the currently deployed solutions it is left to the initrd (user) to cap the PCRs. After some thinking, we can still ensure user control and give an option to cap the PCRs earlier. We hope to post a v11 later this week or early next week that introduces a new policy field to the existing measurement policy framework. Will add/update the kernel docs with respect to the policy expansion. We are also looking the best way we might add a warning to the kernel log if the SHA1 bank is used beyond capping the PCRs.

Hopefully this answers the outstanding comments on the SHA1 thread.

v/r,
dps

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#43 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Hey again,

Quoted 47 lines · Daniel P. Smith

Hi Luto.

Quoted 32 lines · Andy Lutomirski
Quoted 20 lines · Thomas Gleixner
Quoted 9 lines · Daniel P. Smith
Quoted 8 lines · Eric W. Biederman
Quoted 4 lines · Eric Biggers

That paragraph is also phrased as a hypothetical, "Even if we'd prefer to use SHA-256-only".  That implies that you do not, in fact, prefer SHA-256 only.  Is that the case?  Sure, maybe there are situations where you *have* to use SHA-1, but why would you not at least *prefer* SHA-256?

Yes.  Please prefer to use SHA-256.

Quoted 8 lines
Quoted 3 lines

Have you considered implementing I think it is SHA1-DC (as git has) that is compatible with SHA1 but blocks the known class of attacks where sha1 is actively broken at this point?

We are using the kernel's implementation, addressing what the kernel provides is beyond our efforts. Perhaps someone who is interested in improving the kernel's SHA1 could submit a patch implementing/replacing it with SHA1-DC, as I am sure the maintainers would welcome the help.

Quoted 8 lines

Well, someone who is interested to get his "secure" code merged should have a vested interested to have a non-broken SHA1 implementation if there is a sensible requirement to use SHA1 in that new "secure" code, no?

Just for the record. The related maintainers can rightfully decide to reject known broken "secure" code on a purely technical argument.

Wait, hold on a second.

Quoted 9 lines

SHA1-DC isn't SHA1.  It's a different hash function that is mostly compatible with SHA1, is different on some inputs, and is maybe more secure.  But the _whole point_ of using SHA1 in the TPM code (well, this really should be the whole point for new applications) is to correctly cap the SHA1 PCRs so we can correctly _turn them off_ in the best way without breaking compatibility with everything that might read the event log.  I think that anyone suggesting using SHA1-DC for this purpose should give some actual analysis as to why they think it's an improvement, let alone even valid.

I would say at a minimum it is to provide a means to cap the PCRs. Devices with TPM1.2 are still prevalent in the wild for which members of the TrenchBoot community support, and there are still valid (and secure) verification uses for SHA1 that I outlined in my previous response.

Quoted 8 lines
Quoted 6 lines

Ross et al, can you confirm that your code actually, at least by default and with a monstrous warning to anyone who tries to change the default, caps SHA1 PCRs if SHA256 is available?  And then can we maybe all stop hassling the people trying to develop this series about the fact that they're doing their best with the obnoxious system that the TPM designers gave them?

Our goal is to keep control in the hands of the user, not making unilateral decisions on their behalf. In the currently deployed solutions it is left to the initrd (user) to cap the PCRs. After some thinking, we can still ensure user control and give an option to cap the PCRs earlier. We hope to post a v11 later this week or early next week that introduces a new policy field to the existing measurement policy framework. Will add/update the kernel docs with respect to the policy expansion. We are also looking the best way we might add a warning to the kernel log if the SHA1 bank is used beyond capping the PCRs.

As the attempt was made to lay in the policy logic, it started to become convoluted and unnecessarily complicated. Thus creating more risk with all the bookkeeping and yet sha1 hashes still have to be sent, the null hash in this case, since the TPM driver will reject extends that do not have hashes for all active banks. At this point, we have opted to keep the logic simple and add a section to our documentation advising of the potential risk should one choose to incorporate SHA1 in their attestations of the platform.

v/r,
dps

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#44 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 76 lines · Daniel P. Smith

Hey again,

Quoted 62 lines · Daniel P. Smith

Hi Luto.

Quoted 37 lines · Andy Lutomirski
Quoted 24 lines · Thomas Gleixner
Quoted 17 lines · Daniel P. Smith
Quoted 9 lines · Eric W. Biederman
Quoted 7 lines · Eric Biggers

That paragraph is also phrased as a hypothetical, "Even if we'd prefer to use
SHA-256-only". That implies that you do not, in fact, prefer SHA-256 only. Is
that the case? Sure, maybe there are situations where you *have* to use SHA-1,
but why would you not at least *prefer* SHA-256?

Quoted 6 lines

Yes. Please prefer to use SHA-256.

Have you considered implementing I think it is SHA1-DC (as git has) that
is compatible with SHA1 but blocks the known class of attacks where sha1 is actively broken at this point?

Quoted 4 lines

We are using the kernel's implementation, addressing what the kernel provides is beyond our efforts. Perhaps someone who is interested in improving the kernel's SHA1 could submit a patch implementing/replacing it with SHA1-DC, as I am sure the maintainers would welcome the help.

Quoted 8 lines

Well, someone who is interested to get his "secure" code merged should have a vested interested to have a non-broken SHA1 implementation if there is a sensible requirement to use SHA1 in that new "secure" code, no?

Just for the record. The related maintainers can rightfully decide to reject known broken "secure" code on a purely technical argument.

Wait, hold on a second.

Quoted 9 lines

SHA1-DC isn't SHA1. It's a different hash function that is mostly compatible with SHA1, is different on some inputs, and is maybe more secure. But the _whole point_ of using SHA1 in the TPM code (well, this really should be the whole point for new applications) is to correctly cap the SHA1 PCRs so we can correctly _turn them off_ in the best way without breaking compatibility with everything that might read the event log. I think that anyone suggesting using SHA1-DC for this purpose should give some actual analysis as to why they think it's an improvement, let alone even valid.

I would say at a minimum it is to provide a means to cap the PCRs. Devices with TPM1.2 are still prevalent in the wild for which members of the TrenchBoot community support, and there are still valid (and secure) verification uses for SHA1 that I outlined in my previous response.

Quoted 6 lines

Ross et al, can you confirm that your code actually, at least by default and with a monstrous warning to anyone who tries to change the default, caps SHA1 PCRs if SHA256 is available? And then can we maybe all stop hassling the people trying to develop this series about the fact that they're doing their best with the obnoxious system that the TPM designers gave them?

Quoted 9 lines

Our goal is to keep control in the hands of the user, not making unilateral decisions on their behalf. In the currently deployed solutions it is left to the initrd (user) to cap the PCRs. After some thinking, we can still ensure user control and give an option to cap the PCRs earlier. We hope to post a v11 later this week or early next week that introduces a new policy field to the existing measurement policy framework. Will add/update the kernel docs with respect to the policy expansion. We are also looking the best way we might add a warning to the kernel log if the SHA1 bank is used beyond capping the PCRs.

Quoted 9 lines

As the attempt was made to lay in the policy logic, it started to become convoluted and unnecessarily complicated. Thus creating more risk with all the bookkeeping and yet sha1 hashes still have to be sent, the null hash in this case, since the TPM driver will reject extends that do not have hashes for all active banks. At this point, we have opted to keep the logic simple and add a section to our documentation advising of the potential risk should one choose to incorporate SHA1 in their attestations of the platform.

I've read the TPM standard a bit, but it's been awhile, and it's too complicated anyway. So, can you remind me (and probably 3/4 of the other people on this thread, too):

What, exactly, is your patchset doing that requires hashing at all? (I assume it's extending a PCR and generating an event log entry.). What, exactly, does it mean to "cap" a PCR? How is this different from what your patchset does?

With that answered, it will hopefully be easy to see that you're making the right call :)

--Andy

Signature · 2 lines
Andy Lutomirski
AMA Capital Management, LLC
#45 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 88 lines · Andy Lutomirski
Quoted 76 lines · Daniel P. Smith

Hey again,

Quoted 62 lines · Daniel P. Smith

Hi Luto.

Quoted 37 lines · Andy Lutomirski
Quoted 24 lines · Thomas Gleixner
Quoted 17 lines · Daniel P. Smith
Quoted 9 lines · Eric W. Biederman
Quoted 7 lines · Eric Biggers

That paragraph is also phrased as a hypothetical, "Even if we'd prefer to use
SHA-256-only". That implies that you do not, in fact, prefer SHA-256 only. Is
that the case? Sure, maybe there are situations where you *have* to use SHA-1,
but why would you not at least *prefer* SHA-256?

Quoted 6 lines

Yes. Please prefer to use SHA-256.

Have you considered implementing I think it is SHA1-DC (as git has) that
is compatible with SHA1 but blocks the known class of attacks where sha1 is actively broken at this point?

Quoted 4 lines

We are using the kernel's implementation, addressing what the kernel provides is beyond our efforts. Perhaps someone who is interested in improving the kernel's SHA1 could submit a patch implementing/replacing it with SHA1-DC, as I am sure the maintainers would welcome the help.

Quoted 8 lines

Well, someone who is interested to get his "secure" code merged should have a vested interested to have a non-broken SHA1 implementation if there is a sensible requirement to use SHA1 in that new "secure" code, no?

Just for the record. The related maintainers can rightfully decide to reject known broken "secure" code on a purely technical argument.

Wait, hold on a second.

Quoted 9 lines

SHA1-DC isn't SHA1. It's a different hash function that is mostly compatible with SHA1, is different on some inputs, and is maybe more secure. But the _whole point_ of using SHA1 in the TPM code (well, this really should be the whole point for new applications) is to correctly cap the SHA1 PCRs so we can correctly _turn them off_ in the best way without breaking compatibility with everything that might read the event log. I think that anyone suggesting using SHA1-DC for this purpose should give some actual analysis as to why they think it's an improvement, let alone even valid.

I would say at a minimum it is to provide a means to cap the PCRs. Devices with TPM1.2 are still prevalent in the wild for which members of the TrenchBoot community support, and there are still valid (and secure) verification uses for SHA1 that I outlined in my previous response.

Quoted 6 lines

Ross et al, can you confirm that your code actually, at least by default and with a monstrous warning to anyone who tries to change the default, caps SHA1 PCRs if SHA256 is available? And then can we maybe all stop hassling the people trying to develop this series about the fact that they're doing their best with the obnoxious system that the TPM designers gave them?

Quoted 9 lines

Our goal is to keep control in the hands of the user, not making unilateral decisions on their behalf. In the currently deployed solutions it is left to the initrd (user) to cap the PCRs. After some thinking, we can still ensure user control and give an option to cap the PCRs earlier. We hope to post a v11 later this week or early next week that introduces a new policy field to the existing measurement policy framework. Will add/update the kernel docs with respect to the policy expansion. We are also looking the best way we might add a warning to the kernel log if the SHA1 bank is used beyond capping the PCRs.

Quoted 9 lines

As the attempt was made to lay in the policy logic, it started to become convoluted and unnecessarily complicated. Thus creating more risk with all the bookkeeping and yet sha1 hashes still have to be sent, the null hash in this case, since the TPM driver will reject extends that do not have hashes for all active banks. At this point, we have opted to keep the logic simple and add a section to our documentation advising of the potential risk should one choose to incorporate SHA1 in their attestations of the platform.

Quoted 3 lines

I've read the TPM standard a bit, but it's been awhile, and it's too complicated anyway. So, can you remind me (and probably 3/4 of the other people on this thread, too):

Sure, but honestly if you were to ask me in person, I would have given you the explanation as provided in the Secure Launch Overview in the documentation patch.

Quoted 4 lines

What, exactly, is your patchset doing that requires hashing at all? (I assume it's extending a PCR and generating an event log entry.). What, exactly, does it mean to "cap" a PCR? How is this different from what your patchset does?

The SINIT ACM is provided a structure that basically says, here is an address and size of what it will execute next. It will use that information to take its transitive trust measurement of the kernel before handing control to the Linux kernel. The Secure Launch code is responsible for ensuring everything that can influences its execution to be measured and stored into the TPM for attestations to be made at a latter time. The most important part is the transitive trust measurement of the next part to be executed, the initramfs. Specifically, the Secure Launch code must be able to handle the situation where the initramfs independent of the kernel and loaded separately. Additionally, the policy function provided for optional system state to also be measured and recorded, as the attestation evaluator might want them.

At the end of the day, this capability is strictly a passive (mostly, see note [1] below) solution with the responsibility to maintain the DRTM trust chain by taking meaningful measurements. This includes the next component in the trust chain and then hand execution to that next component.

The TCG specs and good practices provide that a component in either SRTM or DRTM trust chains should extend a non-event record to the tpm and/or its log. This is to indicate the transition point from one component in the trust chain to the next component. Under the client profile, firmware is required to do this by extending an event of type EV_SEPARATOR before "Ready to Boot".

I did not see the term actually defined in the client profile, but the term "cap" refers to the specific action of hashing a value across a set of PCRs. This is to reflect that certain events have occurred and will result in a different but predictable change to the PCR value. Often times this is to ensure that if there are TPM objects sealed to the system with either that event having or have not occurred, they cannot be unsealed. Thus, one has "capped" the PCRs as a means to close access to the “acceptable” system state.

To close and reiterate, Secure Launch only responsibility is to send measurements to the TPM. The TPM and TPM driver has an expectation that every PCR extend event contains a hash for every active algorithm bank. For Secure Launch, to send SHA1 measurements has zero impact on the security of the system. Whether those measurements are used for TPM integrity reporting and security policy enforcement by user space or an enterprise is outside the scope of the Secure Launch capability and the kernel.

[1] A future expansion of Secure Launch will be to enable usage of Intel's Hardware Shield, link below, to provide runtime trustworthy determination of SMM. The full extent of this capability can only be achieved under a DRTM launch of the system with Intel TXT. When enabled, this can be used to verify the SMM protections are in place and inform the kernel's memory management which regions of memory are safe from SMM tampering.

https://www.intel.com/content/dam/www/central-libraries/us/en/documents/drtm-based-computing-whitepaper.pdf

Quoted 5 lines

With that answered, it will hopefully be easy to see that you're making the right call :)

--Andy

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#46 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 12 lines · Daniel P. Smith
Quoted 8 lines · Andy Lutomirski
Quoted 1 line · Daniel P. Smith

What, exactly, is your patchset doing that requires hashing at all? (I assume it's extending a PCR and generating an event log entry.). What, exactly, does it mean to "cap" a PCR? How is this different from what your patchset does?

...

Quoted 8 lines

I did not see the term actually defined in the client profile, but the term "cap" refers to the specific action of hashing a value across a set of PCRs. This is to reflect that certain events have occurred and will result in a different but predictable change to the PCR value. Often times this is to ensure that if there are TPM objects sealed to the system with either that event having or have not occurred, they cannot be unsealed. Thus, one has "capped" the PCRs as a means to close access to the “acceptable” system state.

Okay, so I read Ross's earlier email rather differently:

Quoted 4 lines

Even if we'd prefer to use SHA-256-only, if firmware elected to start us with the SHA-1 and SHA-256 backs active, we still need SHA-1 to parse the TPM event log thus far, and deliberately cap the SHA-1 PCRs in order to safely use SHA-256 for everything else.

I assumed that "deliberately cap" meant that there was an actual feature where you write something to the event log (if applicable) and extend the PCR in a special way that *turns that PCR off*. That is, it does something such that later-loaded software *can't* use that PCR to attest or unseal anything, etc.

But it sounds like you're saying that no such feature exists. And a quick skim of the specs doesn't come up with anything. And the SHA1 banks may well be susceptible to a collision attack.

So what are the kernel's choices wrt the SHA-1 PCRs? It can:

a) Perform business as usual: extend them consistently with the SHA-256 PCRs. This is sort of *fine*: the kernel code in question is not relying on the security of SHA-1, but it is making it possible for future code to (unwisely) rely on them. (Although, if the kernel is loading a trustworthy initramfs, then there won't be a collision, and there is no known second-preimage attack against SHA-1.)

b) Same as (a), but with countermeasures: do something to the effect of *detecting* the attack a la SHA1-DC and panic if an attack is detected. Maybe this is wise; maybe it's not.

c) Do not extend the SHA-1 PCRs and pretend they don't exist. This seems likely to cause massive security problems, and having the kernel try to defend its behavior by saying "we don't support SHA-1 -- this is a problem downstream" seems unwise to me.

d) Extend them but in an unconventional way that makes using them extra secure. For example, calculate SHA-256(next stage), then extend with (next stage || "Linux thinks this is better" || SHA-256(next stage). This makes the SHA-1 banks usable, and it seems like it will probably defeat anything resembling a current attack. But maybe this is silly. It would probably require doing the same thing to the SHA-256 banks for the benefit of any software that checks whether the SHA-1 and SHA-256 banks are consistent with each other.

e) Actually try to make the SHA-1 PCRs unusable. For example, extend them with random numbers.

My inclination is that having some kind of Linux "policy" that SHA-1 is forbidden adds no actual security value. Option (a) honestly seems fine. Nothing in the kernel *relies* on the SHA-1 hash being secure. But option (b) also seems okay if someone is willing to put the effort into implementing it and creating a proper test case.

But the description of all this could certainly do a better job of explaining what's going on.

--Andy

Quoted 9 lines

[1] A future expansion of Secure Launch will be to enable usage of Intel's Hardware Shield, link below, to provide runtime trustworthy determination of SMM. The full extent of this capability can only be achieved under a DRTM launch of the system with Intel TXT. When enabled, this can be used to verify the SMM protections are in place and inform the kernel's memory management which regions of memory are safe from SMM tampering.

https://www.intel.com/content/dam/www/central-libraries/us/en/documents/drtm-based-computing-whitepaper.pdf

Wow. I skimmed this paper. What an overcomplicated solution to a problem that doesn't deserve to exist in the first place.

#47 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Hi Luto,

My apologies, I missed this response and the active on v11 cause me to get an inquiry why I hadn't responded.

Quoted 39 lines · Andy Lutomirski
Quoted 12 lines · Daniel P. Smith
Quoted 8 lines · Andy Lutomirski
Quoted 1 line · Daniel P. Smith

What, exactly, is your patchset doing that requires hashing at all? (I assume it's extending a PCR and generating an event log entry.). What, exactly, does it mean to "cap" a PCR? How is this different from what your patchset does?

...

Quoted 8 lines

I did not see the term actually defined in the client profile, but the term "cap" refers to the specific action of hashing a value across a set of PCRs. This is to reflect that certain events have occurred and will result in a different but predictable change to the PCR value. Often times this is to ensure that if there are TPM objects sealed to the system with either that event having or have not occurred, they cannot be unsealed. Thus, one has "capped" the PCRs as a means to close access to the “acceptable” system state.

Okay, so I read Ross's earlier email rather differently:

Quoted 4 lines

Even if we'd prefer to use SHA-256-only, if firmware elected to start us with the SHA-1 and SHA-256 backs active, we still need SHA-1 to parse the TPM event log thus far, and deliberately cap the SHA-1 PCRs in order to safely use SHA-256 for everything else.

I assumed that "deliberately cap" meant that there was an actual feature where you write something to the event log (if applicable) and extend the PCR in a special way that *turns that PCR off*. That is, it does something such that later-loaded software *can't* use that PCR to attest or unseal anything, etc.

Quoted 3 lines

But it sounds like you're saying that no such feature exists. And a quick skim of the specs doesn't come up with anything. And the SHA1 banks may well be susceptible to a collision attack.

Correct, the only entity that can disable PCR banks is the firmware. When it initializes the TPM, it can disable banks/algorithms. After that, when an extend operation is done, the TPM is expecting an entry for all active PCR banks and the TPM itself does the extend hash that is stored into the PCRs.

Quoted 9 lines

So what are the kernel's choices wrt the SHA-1 PCRs? It can:

a) Perform business as usual: extend them consistently with the SHA-256 PCRs. This is sort of *fine*: the kernel code in question is not relying on the security of SHA-1, but it is making it possible for future code to (unwisely) rely on them. (Although, if the kernel is loading a trustworthy initramfs, then there won't be a collision, and there is no known second-preimage attack against SHA-1.)

Quoted 8 lines

b) Same as (a), but with countermeasures: do something to the effect of *detecting* the attack a la SHA1-DC and panic if an attack is detected. Maybe this is wise; maybe it's not.

c) Do not extend the SHA-1 PCRs and pretend they don't exist. This seems likely to cause massive security problems, and having the kernel try to defend its behavior by saying "we don't support SHA-1 -- this is a problem downstream" seems unwise to me.

I will chime in here to say that you can't ignore them, but you can send a fixed value, either well-known or junk, as the SHA1 value when doing the extend operation as you suggest in (e).

Quoted 9 lines

d) Extend them but in an unconventional way that makes using them extra secure. For example, calculate SHA-256(next stage), then extend with (next stage || "Linux thinks this is better" || SHA-256(next stage). This makes the SHA-1 banks usable, and it seems like it will probably defeat anything resembling a current attack. But maybe this is silly. It would probably require doing the same thing to the SHA-256 banks for the benefit of any software that checks whether the SHA-1 and SHA-256 banks are consistent with each other.

Quoted 8 lines

e) Actually try to make the SHA-1 PCRs unusable. For example, extend them with random numbers.

My inclination is that having some kind of Linux "policy" that SHA-1 is forbidden adds no actual security value. Option (a) honestly seems fine. Nothing in the kernel *relies* on the SHA-1 hash being secure. But option (b) also seems okay if someone is willing to put the effort into implementing it and creating a proper test case.

Obviously, for the most part, we are in agreement. The one caveat is that I don't think the effort to shore-up SHA1 provides a good return on the costs it would incur. With no intent to disparage any one person, there generally will be two groups that would use SHA1. The first would be those limited by their platform and understand the risks. The second would be those attempting to do a cryptographic-based security solution that has either been living under a rock the last few years or has done zero research into the capabilities they are using for their solution. IMHO it is better to not inhibit the first group trying to save the latter group as the latter are always doomed to failure.

Quoted 2 lines

But the description of all this could certainly do a better job of explaining what's going on.

I would be glad to do so, and have tried several ways to explain it. Even working with multiple people that understand the problem to draft a better explanation. It would be greatly appreciated if you could provide what points you think should be clarified to better help convey the situation.

Quoted 10 lines

--Andy

Quoted 8 lines

[1] A future expansion of Secure Launch will be to enable usage of Intel's Hardware Shield, link below, to provide runtime trustworthy determination of SMM. The full extent of this capability can only be achieved under a DRTM launch of the system with Intel TXT. When enabled, this can be used to verify the SMM protections are in place and inform the kernel's memory management which regions of memory are safe from SMM tampering.

Quoted 4 lines
Quoted 1 line

https://www.intel.com/content/dam/www/central-libraries/us/en/documents/drtm-based-computing-whitepaper.pdf

Wow. I skimmed this paper. What an overcomplicated solution to a problem that doesn't deserve to exist in the first place.

While we could have a long discussion over the merits of SMM, the fact we have to face is that it is here, and it is not going anywhere any time soon. I honestly found AMD's SMM Containerization (Appendix D of the AMD64 Architecture Programmer’s Manual - Volume 2) the better approach, and it saddens me that it is completely disabled.

v/r,
dps

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

maintainer
#48 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 7 lines · Daniel P. Smith

Hi Luto,

My apologies, I missed this response and the active on v11 cause me to
get an inquiry why I hadn't responded.

On 9/21/24 18:40, Andy Lutomirski wrote:

[...]

Quoted 11 lines
Quoted 9 lines

I assumed that "deliberately cap" meant that there was an actual feature where you write something to the event log (if applicable) and extend the PCR in a special way that *turns that PCR off*. That is, it does something such that later-loaded software *can't* use that PCR to attest or unseal anything, etc.

But it sounds like you're saying that no such feature exists.  And a quick skim of the specs doesn't come up with anything.  And the SHA1 banks may well be susceptible to a collision attack.

Correct, the only entity that can disable PCR banks is the firmware.

No, that's not correct. Any user can use TPM_PCR_Allocate to activate or deactivate individual banks. The caveat is the change is not implemented until the next TPM reset (which should involve a reboot). BIOS also gets to the TPM before the kernel does, so it can, in theory, check what banks a TPM has and call TPM_PCR_Allocate to change them. In practice, because this requires a reboot, this is usually only done from the BIOS menus not on a direct boot ... so you can be reasonably sure that whatever changes were made will stick.

Quoted 4 lines

When it initializes the TPM, it can disable banks/algorithms. After that, when an extend operation is done, the TPM is expecting an entry for all active PCR banks and the TPM itself does the extend hash that is stored into the PCRs.

This, also, is not quite correct: an extend is allowed to specify banks that don't exist (in which case nothing happens and no error is reported) and miss banks that do (in which case no extend is done to that bank). In the early days of TPM2, some BIOS implementations only extended sha1 for instance, meaning the sha256 banks were all zero when the kernel started.

Even today, if you activate a bank the BIOS doesn't know about, it likely won't extend it. You can see this in VM boots with OVMF and software TPMs having esoteric banks like SM3.

Regards,

James

#49 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 21 lines · James Bottomley
Quoted 7 lines · Daniel P. Smith

Hi Luto,

My apologies, I missed this response and the active on v11 cause me to
get an inquiry why I hadn't responded.

On 9/21/24 18:40, Andy Lutomirski wrote:

[...]

Quoted 11 lines
Quoted 9 lines

I assumed that "deliberately cap" meant that there was an actual feature where you write something to the event log (if applicable) and extend the PCR in a special way that *turns that PCR off*. That is, it does something such that later-loaded software *can't* use that PCR to attest or unseal anything, etc.

But it sounds like you're saying that no such feature exists.  And a quick skim of the specs doesn't come up with anything.  And the SHA1 banks may well be susceptible to a collision attack.

Correct, the only entity that can disable PCR banks is the firmware.

Quoted 8 lines

No, that's not correct. Any user can use TPM_PCR_Allocate to activate or deactivate individual banks. The caveat is the change is not implemented until the next TPM reset (which should involve a reboot). BIOS also gets to the TPM before the kernel does, so it can, in theory, check what banks a TPM has and call TPM_PCR_Allocate to change them. In practice, because this requires a reboot, this is usually only done from the BIOS menus not on a direct boot ... so you can be reasonably sure that whatever changes were made will stick.

Okay, since there is a desire for exactness. Any system software can send the TPM_PCR_Allocate command, specifying which PCRs should be activated on next _TPM_init. There are restrictions such that if DRTM_PCR is defined, then at least one bank must have a D-RTM PCR allocation. In agreement with my statement, this is the mechanism used by firmware to select the banks. Depending on the firmware implementation, the firmware request will likely override the request sent by the system software.

This brings us back to an earlier point, if one disables the SHA1 banks in BIOS menu, then TXT will not use them and thus neither will Secure Launch. Secure Launch will only use the algorithms used by the CPU and the ACM.

Quoted 12 lines
Quoted 4 lines

When it initializes the TPM, it can disable banks/algorithms. After that, when an extend operation is done, the TPM is expecting an entry for all active PCR banks and the TPM itself does the extend hash that is stored into the PCRs.

This, also, is not quite correct: an extend is allowed to specify banks that don't exist (in which case nothing happens and no error is reported) and miss banks that do (in which case no extend is done to that bank). In the early days of TPM2, some BIOS implementations only extended sha1 for instance, meaning the sha256 banks were all zero when the kernel started.

Quoted 3 lines

Even today, if you activate a bank the BIOS doesn't know about, it likely won't extend it. You can see this in VM boots with OVMF and software TPMs having esoteric banks like SM3.

Let me correct myself here and again be extremely precise. When an extend operation is done, the TPM driver expects to receive an array of digests that is the same size as the number of allocated/active banks. Specifically, it loops from 0 to chip->nr_allocated_banks, filling TPML_DIGEST_VALUES with an entry for all the active banks, to include SHA1 if it is active. Coming back to my response to Luto, we can either populate it with 0 or a well-known value for each extend we send. Regardless of what the value is, the TPM will use its implementation of SHA1 to calculate the resulting extend value.

Even with these clarifications, the conclusion does not change. If the firmware enables SHA1, there is nothing that can be done to disable or block its usage from the user. Linux Secure Launch sending measurements to all the banks that the hardware used to start the DRTM chain does not create a vulnerability in and of itself. The user is free to leverage the SHA1 bank in any of the TPM's Integrity Collection suite of operations, regardless of what Secure Launch sends for the SHA1 hash. Whereas, neutering the solution of SHA1 breaks the ability for it to support any hardware that has a TPM1.2, of which there are still many in use.

V/r,
Daniel P. Smith

#50 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 41 lines · Daniel P. Smith
Quoted 21 lines · James Bottomley
Quoted 7 lines · Daniel P. Smith

Hi Luto,

My apologies, I missed this response and the active on v11 cause me to
get an inquiry why I hadn't responded.

On 9/21/24 18:40, Andy Lutomirski wrote:

[...]

Quoted 11 lines
Quoted 9 lines

I assumed that "deliberately cap" meant that there was an actual feature where you write something to the event log (if applicable) and extend the PCR in a special way that *turns that PCR off*. That is, it does something such that later-loaded software *can't* use that PCR to attest or unseal anything, etc.

But it sounds like you're saying that no such feature exists. And a quick skim of the specs doesn't come up with anything. And the SHA1 banks may well be susceptible to a collision attack.

Correct, the only entity that can disable PCR banks is the firmware.

Quoted 8 lines

No, that's not correct. Any user can use TPM_PCR_Allocate to activate or deactivate individual banks. The caveat is the change is not implemented until the next TPM reset (which should involve a reboot). BIOS also gets to the TPM before the kernel does, so it can, in theory, check what banks a TPM has and call TPM_PCR_Allocate to change them. In practice, because this requires a reboot, this is usually only done from the BIOS menus not on a direct boot ... so you can be reasonably sure that whatever changes were made will stick.

Okay, since there is a desire for exactness. Any system software can send the TPM_PCR_Allocate command, specifying which PCRs should be activated on next _TPM_init. There are restrictions such that if DRTM_PCR is defined, then at least one bank must have a D-RTM PCR allocation. In agreement with my statement, this is the mechanism used by firmware to select the banks. Depending on the firmware implementation, the firmware request will likely override the request sent by the system software.

Quoted 20 lines

This brings us back to an earlier point, if one disables the SHA1 banks in BIOS menu, then TXT will not use them and thus neither will Secure Launch. Secure Launch will only use the algorithms used by the CPU and the ACM.

Quoted 15 lines
Quoted 4 lines

When it initializes the TPM, it can disable banks/algorithms. After that, when an extend operation is done, the TPM is expecting an entry for all active PCR banks and the TPM itself does the extend hash that is stored into the PCRs.

This, also, is not quite correct: an extend is allowed to specify banks that don't exist (in which case nothing happens and no error is reported) and miss banks that do (in which case no extend is done to that bank). In the early days of TPM2, some BIOS implementations only extended sha1 for instance, meaning the sha256 banks were all zero when the kernel started.

Even today, if you activate a bank the BIOS doesn't know about, it likely won't extend it. You can see this in VM boots with OVMF and software TPMs having esoteric banks like SM3.

How is this not a security hole you could drive a truck through? Indeed, looking at the docs, TPM2_PCR_Extend says "If no digest value is specified for a bank, then the PCR in that bank is not modified."

Quoted 10 lines

Let me correct myself here and again be extremely precise. When an extend operation is done, the TPM driver expects to receive an array of digests that is the same size as the number of allocated/active banks. Specifically, it loops from 0 to chip->nr_allocated_banks, filling TPML_DIGEST_VALUES with an entry for all the active banks, to include SHA1 if it is active. Coming back to my response to Luto, we can either populate it with 0 or a well-known value for each extend we send. Regardless of what the value is, the TPM will use its implementation of SHA1 to calculate the resulting extend value.

At least extending unknown/unsupported banks with 0 modifies the bank, which gives software that might rely on that bank an indication that something in the chain doesn't support the bank. But does actual TPM-using software in the wild actually look up the event log and notice that it contains a 0?

This sucks. How on Earth didn't the TPM2 spec do this instead of having explicit handling for "a PCR got extended, and the code that extended it didn't support a given bank, and therefore *the resulting PCR value cannot be relied on*? It would have been *one single bit per PCR, bank* indicating that the PCR's value is incomplete, along with some basic logic that an incomplete PCR cannot magically become complete, nor can it be used to authorize anything unless the authorization policy explicitly allows it?

Anyway, other than the fact that everyone (presumably?) expects software to be aware of SHA-1 and (mostly) SHA256, and presumably users of SM3 already expect that a lot of things don't support it, SHA1 doesn't seem very different from SM3 in the sense that (a) people might not want to support it and (b) the actual behavior of a boot chain component that doesn't support a cryptosystem is FUNDAMENTALLY DANGEROUS.

Is there explicit guidance from TCG as to how this is supposed to work?

In any case, I have a strawman suggestion to resolve this issue much better from Linux's perspective. It's a strawman because, while I attempted to read the relevant part of the specs, the specs and the ecosystem are a mess, so I could be wrong.

Linux should not use TPM2_PCR_Extend *at all*. Instead, Linux should exclusively use TPM2_PCR_Event. I would expect that passing, say, the entire kernel image to TPM2_PCR_Event would be a big mistake, so instead Linux should hash the relevant data with a reasonable suggestion of hashes (which includes, mandatorily, SHA-384 and *does not* include SHA-1, and may or may not be configurable at build time to include things like SM3), concatenate them, and pass that to TPM2_PCR_Event. And Linux should make the value that it passed to TPM2_PCR_Event readily accessible to software using it, and should also include some straightforward tooling to calculate it from a given input so that software that wants to figure out what value to expect in a PCR can easily do so.

And then software that wants to use a SHA-1 bank will work every bit as well as it would if Linux actually implemented it, but Linux can happily not implement it, and even users of oddball algorithms that Linux has never heard of will get secure behavior.

(Why SHA-384? Because it's mandatory in the TPM Client profile, and anyone who's happy with SHA-256 should also be willing to accept SHA-384.)

Quoted 16 lines

Even with these clarifications, the conclusion does not change. If the firmware enables SHA1, there is nothing that can be done to disable or block its usage from the user. Linux Secure Launch sending measurements to all the banks that the hardware used to start the DRTM chain does not create a vulnerability in and of itself. The user is free to leverage the SHA1 bank in any of the TPM's Integrity Collection suite of operations, regardless of what Secure Launch sends for the SHA1 hash. Whereas, neutering the solution of SHA1 breaks the ability for it to support any hardware that has a TPM1.2, of which there are still many in use.

V/r,
Daniel P. Smith

Signature · 2 lines
Andy Lutomirski
AMA Capital Management, LLC
#51 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 14 lines · Andy Lutomirski

Linux should not use TPM2_PCR_Extend *at all*. Instead, Linux should exclusively use TPM2_PCR_Event. I would expect that passing, say, the entire kernel image to TPM2_PCR_Event would be a big mistake, so instead Linux should hash the relevant data with a reasonable suggestion of hashes (which includes, mandatorily, SHA-384 and *does not* include SHA-1, and may or may not be configurable at build time to include things like SM3), concatenate them, and pass that to TPM2_PCR_Event. And Linux should make the value that it passed to TPM2_PCR_Event readily accessible to software using it, and should also include some straightforward tooling to calculate it from a given input so that software that wants to figure out what value to expect in a PCR can easily do so.

Whoops, putting on my "knows a bit about crypto" hat for a second, this is not great, as the algorithms aren't distinguished, and one could hypothetically add a wildly insecure hash to the list that breaks it. Instead it should be something like:

"SHA-384 48 bytes: [the SHA-384 data], someotherhash 71 bytes: [other data], ..."

It might even be polite to include some human readable text that also indicates what got hashed, e.g. "initramfs", so that anyone reading the event log can see what got hashed. On that note, maybe making the whole thing human readable and using base64 would be nice:

"initramfs\nsha384 [base64 data]\nblake3 [base64 data]\nsm3 [base64 data]"

Whatever format is used should be unambiguously parseable. And who knows, maybe there's already some kind of industry standard for how TPM-using software is expected to behave here.

Quoted 28 lines

And then software that wants to use a SHA-1 bank will work every bit as well as it would if Linux actually implemented it, but Linux can happily not implement it, and even users of oddball algorithms that Linux has never heard of will get secure behavior.

(Why SHA-384? Because it's mandatory in the TPM Client profile, and anyone who's happy with SHA-256 should also be willing to accept SHA-384.)

Quoted 12 lines

Even with these clarifications, the conclusion does not change. If the firmware enables SHA1, there is nothing that can be done to disable or block its usage from the user. Linux Secure Launch sending measurements to all the banks that the hardware used to start the DRTM chain does not create a vulnerability in and of itself. The user is free to leverage the SHA1 bank in any of the TPM's Integrity Collection suite of operations, regardless of what Secure Launch sends for the SHA1 hash. Whereas, neutering the solution of SHA1 breaks the ability for it to support any hardware that has a TPM1.2, of which there are still many in use.

Quoted 4 lines

V/r,
Daniel P. Smith

Quoted 3 lines

--
Andy Lutomirski
AMA Capital Management, LLC

--
Andy Lutomirski
AMA Capital Management, LLC

maintainer
#52 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

In reply to Andy Lutomirski
Quoted 4 lines · Andy Lutomirski

Linux should not use TPM2_PCR_Extend *at all*.  Instead, Linux should exclusively use TPM2_PCR_Event.  I would expect that passing, say, the entire kernel image to TPM2_PCR_Event would be a big mistake, so instead Linux should hash the relevant data with a reasonable

Quoted 8 lines

suggestion of hashes (which includes, mandatorily, SHA-384 and *does not* include SHA-1, and may or may not be configurable at build time to include things like SM3), concatenate them, and pass that to TPM2_PCR_Event.  And Linux should make the value that it passed to TPM2_PCR_Event readily accessible to software using it, and should also include some straightforward tooling to calculate it from a given input so that software that wants to figure out what value to expect in a PCR can easily do so.

Just for clarity, this is about how the agile log format works. Each event entry in the log contains a list of bank hashes and the extends occur in log event order, so replaying a log should get you to exactly the head PCR value of each bank. If a log doesn't understand a format, like SM3, then an entry for it doesn't appear in the log and a replay says nothing about the PCR value.

For some events, the hash is actually the hash of the event entry itself and for others, the entry is just a hint and the hash is of something else.

I think part of the confusion stems from the twofold issues of PCRs: at their simplest they were expected to provide the end policy values (this turns out to be problematic because there are quite a few ways, that will produce different end PCR values, that a system could get to the same state). If you don't trust a bank (or don't know about it), you don't code it into a required policy statement and its value becomes irrelevant. If, as most remote attestation systems do, you're analysing log entries, then you can calculate end PCR points for all banks mentioned in the log and you could ask the TPM to quote all of them. In practice, you tend to pick a bank you prefer (sha256 usually) and quote only that. Again, if a bank doesn't appear in the log, you're not going to ask for a quote from it, so what it contains is irrelevant to the analysis of the log.

The point being that in neither case would the fact that boot software failed to extend a bank it didn't have a hash for result in some type of compromise.

Note that one of the things you can do with the log (because the entries are separable) is strip out all the hashes for a bank. However, the remote is likely to refuse to accept the log if you, say, strip the sha256 ones because you think a collision allows you to fake a sha1 log because it would know you should have had sha256 entries as well.

By the way, the only modern hash you can rely on a TPM2 having is sha256. Most of the older ones don't have sha384. They all do have sha1 for backwards compatibility with TPM1.2

James

#53 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 15 lines · James Bottomley
Quoted 4 lines · Andy Lutomirski

Linux should not use TPM2_PCR_Extend *at all*. Instead, Linux should exclusively use TPM2_PCR_Event. I would expect that passing, say, the entire kernel image to TPM2_PCR_Event would be a big mistake, so instead Linux should hash the relevant data with a reasonable

Quoted 8 lines

suggestion of hashes (which includes, mandatorily, SHA-384 and *does not* include SHA-1, and may or may not be configurable at build time to include things like SM3), concatenate them, and pass that to TPM2_PCR_Event. And Linux should make the value that it passed to TPM2_PCR_Event readily accessible to software using it, and should also include some straightforward tooling to calculate it from a given input so that software that wants to figure out what value to expect in a PCR can easily do so.

Quoted 6 lines

Just for clarity, this is about how the agile log format works. Each event entry in the log contains a list of bank hashes and the extends occur in log event order, so replaying a log should get you to exactly the head PCR value of each bank. If a log doesn't understand a format, like SM3, then an entry for it doesn't appear in the log and a replay says nothing about the PCR value.

I have no idea what the "agile log format" is or what all the formats in existence are. I found section 4.2.4 here:

https://trustedcomputinggroup.org/wp-content/uploads/TCG_IWG_CEL_v1_r0p41_pub.pdf

It says:

This field contains the list of the digest values Extended. The Extend method varies with TPM command, so there is
no uniform meaning of TPM Extend in this instance, and separate descriptions are unavoidable. If using the
TPM2_PCR_Extend command, this field is the data sent to the TPM (i.e., not the resulting value of the PCR after the TPM2_PCR_Extend command completes). If using the TPM2_PCR_Event command, this field contains the digest
structure returned by the TPM2_PCR_Event command (that contains the digest(s) submitted to each PCR bank as
the internal Extend operation). This field SHALL contain the information from the TPML_DIGEST_VALUES used in
the Extend operation.

So we're logging the values with which we extend the PCRs. Once upon a time, someone decided it was okay to skip extending a PCR bank:

https://google.github.io/security-research/pocs/bios/tpm-carte-blanche/writeup.html

and it was not a great idea.

There seem to be six (!) currently defined hashes: SHA1, SHA256, SHA384, SHA512, SM2 and SM3. I haven't spotted anything promising not to add more. It seems to be that Linux really really ought to:

(a) extend all banks. Not all banks that the maintainers like, and not all banks that the maintainers knew about when having this discussion. *All* banks. That means TPM2_PCR_Event(). (Or we refuse to boot if there's a bank we don't like.)

(b) Make a best effort to notice if something is wrong with the TPM and/or someone is MITMing us and messing with us. That means computing the hash algorithms we actually support and checking whether TPM2_PCR_Event() returns the right thing. I'm not seeing a specific attack that seems likely that this prevents, but it does seem like decent defense in depth, and if someone chooses to provision a machine by reading its event log and then subsequently getting an attestation that a future event log matches what was read, then avoiding letting an attacker who temporarily controls the TPM connection from corrupting the results seems wise. And I don't see anything at all that we gain by removing a check that (TPM's reported SHA1 == what we calculated) in the name of "not supporting SHA1") other than a few hundred bytes of object code. (And yes, SHA1 is much more likely to be supported than SM3, so it's not absurd to implement SHA1 and not implement SM3.)

Quoted 5 lines

For some events, the hash is actually the hash of the event entry itself and for others, the entry is just a hint and the hash is of something else.

Quoted 7 lines

I think part of the confusion stems from the twofold issues of PCRs: at their simplest they were expected to provide the end policy values (this turns out to be problematic because there are quite a few ways, that will produce different end PCR values, that a system could get to the same state). If you don't trust a bank (or don't know about it), you don't code it into a required policy statement and its value becomes irrelevant.

I think that "you" refers to multiple entities, and this is a problem.

If the vendor of an attestation-dependent thing trusts SM3 but *Linux* does not like SM3, then the vendor's software should not become wildly insecure because Linux does not like SM3. And, as that 2004 CVE shows, even two groups that are nominally associated with Microsoft can disagree on which banks they like, causing a vulnerability.

#54 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 90 lines · Andy Lutomirski
Quoted 15 lines · James Bottomley
Quoted 4 lines · Andy Lutomirski

Linux should not use TPM2_PCR_Extend *at all*. Instead, Linux should exclusively use TPM2_PCR_Event. I would expect that passing, say, the entire kernel image to TPM2_PCR_Event would be a big mistake, so instead Linux should hash the relevant data with a reasonable

Quoted 8 lines

suggestion of hashes (which includes, mandatorily, SHA-384 and *does not* include SHA-1, and may or may not be configurable at build time to include things like SM3), concatenate them, and pass that to TPM2_PCR_Event. And Linux should make the value that it passed to TPM2_PCR_Event readily accessible to software using it, and should also include some straightforward tooling to calculate it from a given input so that software that wants to figure out what value to expect in a PCR can easily do so.

Quoted 6 lines

Just for clarity, this is about how the agile log format works. Each event entry in the log contains a list of bank hashes and the extends occur in log event order, so replaying a log should get you to exactly the head PCR value of each bank. If a log doesn't understand a format, like SM3, then an entry for it doesn't appear in the log and a replay says nothing about the PCR value.

I have no idea what the "agile log format" is or what all the formats in existence are. I found section 4.2.4 here:

https://urldefense.com/v3/__https://trustedcomputinggroup.org/wp-content/uploads/TCG_IWG_CEL_v1_r0p41_pub.pdf__;!!ACWV5N9M2RV99hQ!Iw9aAHcJMT6j3t_DSb7cOk8iWy8VJYkJOlGQ_gtLUz0XwPcIZclY4I8GZJ5VP4OScLjBaz3RX1QlGGBWWZw$

It says:

This field contains the list of the digest values Extended. The Extend method varies with TPM command, so there is
no uniform meaning of TPM Extend in this instance, and separate descriptions are unavoidable. If using the
TPM2_PCR_Extend command, this field is the data sent to the TPM (i.e., not the resulting value of the PCR after the TPM2_PCR_Extend command completes). If using the TPM2_PCR_Event command, this field contains the digest
structure returned by the TPM2_PCR_Event command (that contains the digest(s) submitted to each PCR bank as
the internal Extend operation). This field SHALL contain the information from the TPML_DIGEST_VALUES used in
the Extend operation.

So we're logging the values with which we extend the PCRs. Once upon a time, someone decided it was okay to skip extending a PCR bank:

https://urldefense.com/v3/__https://google.github.io/security-research/pocs/bios/tpm-carte-blanche/writeup.html__;!!ACWV5N9M2RV99hQ!Iw9aAHcJMT6j3t_DSb7cOk8iWy8VJYkJOlGQ_gtLUz0XwPcIZclY4I8GZJ5VP4OScLjBaz3RX1QlKxD4S1w$

and it was not a great idea.

There seem to be six (!) currently defined hashes: SHA1, SHA256, SHA384, SHA512, SM2 and SM3. I haven't spotted anything promising not to add more. It seems to be that Linux really really ought to:

(a) extend all banks. Not all banks that the maintainers like, and not all banks that the maintainers knew about when having this discussion. *All* banks. That means TPM2_PCR_Event(). (Or we refuse to boot if there's a bank we don't like.)

(b) Make a best effort to notice if something is wrong with the TPM and/or someone is MITMing us and messing with us. That means computing the hash algorithms we actually support and checking whether TPM2_PCR_Event() returns the right thing. I'm not seeing a specific attack that seems likely that this prevents, but it does seem like decent defense in depth, and if someone chooses to provision a machine by reading its event log and then subsequently getting an attestation that a future event log matches what was read, then avoiding letting an attacker who temporarily controls the TPM connection from corrupting the results seems wise. And I don't see anything at all that we gain by removing a check that (TPM's reported SHA1 == what we calculated) in the name of "not supporting SHA1") other than a few hundred bytes of object code. (And yes, SHA1 is much more likely to be supported than SM3, so it's not absurd to implement SHA1 and not implement SM3.)

Quoted 5 lines

For some events, the hash is actually the hash of the event entry itself and for others, the entry is just a hint and the hash is of something else.

Quoted 7 lines

I think part of the confusion stems from the twofold issues of PCRs: at their simplest they were expected to provide the end policy values (this turns out to be problematic because there are quite a few ways, that will produce different end PCR values, that a system could get to the same state). If you don't trust a bank (or don't know about it), you don't code it into a required policy statement and its value becomes irrelevant.

Quoted 7 lines

I think that "you" refers to multiple entities, and this is a problem.

If the vendor of an attestation-dependent thing trusts SM3 but *Linux* does not like SM3, then the vendor's software should not become wildly insecure because Linux does not like SM3. And, as that 2004 CVE shows, even two groups that are nominally associated with Microsoft can disagree on which banks they like, causing a vulnerability.

Thanks everyone for all the feedback and discussions on this. I understand it is important and perhaps the Linux TPM code should be modified to do the extend operations differently but this seems like it is outside the scope of our Secure Launch feature patch set.

As far as our patch series goes, we have done the things that were asked of us like documenting SHA-1 usage, fixing comments and commit message and breaking up the original patch into two (one for SHA-1 and one for SHA-256). It seems we should be able to submit our next version at this point.

Thanks
Ross

#55 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 9 lines · On Thu, Nov 21, 2024 at 12:11 PM <[email protected]> wrote:
Quoted 5 lines · Andy Lutomirski

If the vendor of an attestation-dependent thing trusts SM3 but *Linux* does not like SM3, then the vendor's software should not become wildly insecure because Linux does not like SM3. And, as that 2004 CVE shows, even two groups that are nominally associated with Microsoft can disagree on which banks they like, causing a vulnerability.

Quoted 4 lines

Thanks everyone for all the feedback and discussions on this. I understand it is important and perhaps the Linux TPM code should be modified to do the extend operations differently but this seems like it is outside the scope of our Secure Launch feature patch set.

It's absolutely not outside the scope. Look, this is quoted verbatim from your patchset (v11, but I don't think this has materially changed):

+ /* Early SL code ensured there was a max count of 2 digests */
+ for (i = 0; i < event->count; i++) {
+ dptr = (u8 *)alg_id_field + sizeof(u16);
+
+ for (j = 0; j < tpm->nr_allocated_banks; j++) {
+ if (digests[j].alg_id != *alg_id_field)
+ continue;

^^^^^^^^^^^^^^^^^^^^^ excuse me?

+
+ switch (digests[j].alg_id) {
+ case TPM_ALG_SHA256:
+ memcpy(&digests[j].digest[0], dptr,
+ SHA256_DIGEST_SIZE);
+ alg_id_field = (u16 *)((u8 *)alg_id_field +
+ SHA256_DIGEST_SIZE + sizeof(u16));
+ break;
+ case TPM_ALG_SHA1:
+ memcpy(&digests[j].digest[0], dptr,
+ SHA1_DIGEST_SIZE);
+ alg_id_field = (u16 *)((u8 *)alg_id_field +
+ SHA1_DIGEST_SIZE + sizeof(u16));
+ break;
+ default:
+ break;
+ }
+ }
+ }
+
+ ret = tpm_pcr_extend(tpm, event->pcr_idx, digests);
+ if (ret) {
+ pr_err("Error extending TPM20 PCR, result: %d\n", ret);
+ slaunch_txt_reset(txt, "Failed to extend TPM20 PCR\n",
+ SL_ERROR_TPM_EXTEND);
+ }

I haven't even tried to see what happens if there are more than two allocated banks, but regardless, that 'continue' statement is a vulnerability, and it's introduced in the patchset. I'm not the maintainer of this code, but I would NAK this.

I'm sure there's some reason that the TPM spec even makes code like this possible, but it sure looks like the TPM2_PCR_Event operation exists more or less to avoid this vulnerability. I think you should either use it or you should explain, convincingly, why Linux should add code that does not use it and thus has a vulnerability in certain, entirely plausible, firmware configurations.

This is brand new code that is explicitly security code. I don't think it's valid to spell "crud, we can't handle this case at all, and failing to handle it is a security vulnerability" as "continue". If *I* were writing this code, I would use TPM2_PCR_Event, which is entirely immune to this particular failure as far as I can see.

--Andy

#56 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 11 lines · Andy Lutomirski
Quoted 9 lines · On Thu, Nov 21, 2024 at 12:11 PM <[email protected]> wrote:
Quoted 5 lines · Andy Lutomirski

If the vendor of an attestation-dependent thing trusts SM3 but *Linux* does not like SM3, then the vendor's software should not become wildly insecure because Linux does not like SM3. And, as that 2004 CVE shows, even two groups that are nominally associated with Microsoft can disagree on which banks they like, causing a vulnerability.

Quoted 8 lines
Quoted 4 lines

Thanks everyone for all the feedback and discussions on this. I understand it is important and perhaps the Linux TPM code should be modified to do the extend operations differently but this seems like it is outside the scope of our Secure Launch feature patch set.

It's absolutely not outside the scope. Look, this is quoted verbatim from your patchset (v11, but I don't think this has materially changed):

... I apologize -- I've misread the code. That code is still wrong, I think, but for an entirely different reason:

Quoted 20 lines
(quoted diff) +7 -0
+       /* Early SL code ensured there was a max count of 2 digests */
+       for (i = 0; i < event->count; i++) {
+               dptr = (u8 *)alg_id_field + sizeof(u16);
+
+               for (j = 0; j < tpm->nr_allocated_banks; j++) {
+                       if (digests[j].alg_id != *alg_id_field)
+                               continue;

^^^^^^^^^^^^^^^^^^^^^ excuse me?

(quoted diff) +9 -0
+
+                       switch (digests[j].alg_id) {
+                       case TPM_ALG_SHA256:
+                               memcpy(&digests[j].digest[0], dptr,
+                                      SHA256_DIGEST_SIZE);
+                               alg_id_field = (u16 *)((u8 *)alg_id_field +
+                                       SHA256_DIGEST_SIZE + sizeof(u16));
+                               break;
+                       case TPM_ALG_SHA1:

Review

Quoted 10 lines
(quoted diff) +10 -0
+                               memcpy(&digests[j].digest[0], dptr,
+                                      SHA1_DIGEST_SIZE);
+                               alg_id_field = (u16 *)((u8 *)alg_id_field +
+                                       SHA1_DIGEST_SIZE + sizeof(u16));
+                               break;
+                       default:
+                               break;
+                       }
+               }
+       }

If we fall off the end of the loop, we never increase alg_id_field, and subsequent iterations will malfunction. But we apparently will write zeros (or fail?) if we have an unsupported algorithm, because we are asking to extend all allocated banks. I think. This code is gross. It's plausible that this whole sequence is impossible unless something malicious is going on.

Also, and I'm sort of replying to the wrong patch here, how trustworthy is the data that's used to populate tpm_algs in the stub? I don't think the results will be very pretty if tpm_algs ends up being incorrect.

#57 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 12 lines · Andy Lutomirski
Quoted 11 lines · Andy Lutomirski
Quoted 9 lines · On Thu, Nov 21, 2024 at 12:11 PM <[email protected]> wrote:
Quoted 5 lines · Andy Lutomirski

If the vendor of an attestation-dependent thing trusts SM3 but *Linux* does not like SM3, then the vendor's software should not become wildly insecure because Linux does not like SM3. And, as that 2004 CVE shows, even two groups that are nominally associated with Microsoft can disagree on which banks they like, causing a vulnerability.

Quoted 9 lines
Quoted 8 lines
Quoted 4 lines

Thanks everyone for all the feedback and discussions on this. I understand it is important and perhaps the Linux TPM code should be modified to do the extend operations differently but this seems like it is outside the scope of our Secure Launch feature patch set.

It's absolutely not outside the scope. Look, this is quoted verbatim from your patchset (v11, but I don't think this has materially changed):

Concerning my previous response, I realized that I did not fully understand what you were suggesting/proposing so I am sorry about that. You are correct that addressing this is within the scope of what we are doing. I have reread all the emails again and I/we now understand what you are saying.

We are now exploring how we might use TPM2_PCR_Event, whether it introduces other issues with respect to how TXT/ACM might behave and what would be needed to adopt this approach. More to come on that front. I will mention that the TPM code in the Linux kernel does not currently support the TPM2_PCR_Event command. The functionality needs to be added and that needs buy in from the TPM maintainers (probably guidance too).

A bit more below to clarify a few things...

Quoted 33 lines

... I apologize -- I've misread the code. That code is still wrong, I think, but for an entirely different reason:

Quoted 29 lines
(quoted diff) +7 -0
+       /* Early SL code ensured there was a max count of 2 digests */
+       for (i = 0; i < event->count; i++) {
+               dptr = (u8 *)alg_id_field + sizeof(u16);
+
+               for (j = 0; j < tpm->nr_allocated_banks; j++) {
+                       if (digests[j].alg_id != *alg_id_field)
+                               continue;

^^^^^^^^^^^^^^^^^^^^^ excuse me?

(quoted diff) +18 -0
+
+                       switch (digests[j].alg_id) {
+                       case TPM_ALG_SHA256:
+                               memcpy(&digests[j].digest[0], dptr,
+                                      SHA256_DIGEST_SIZE);
+                               alg_id_field = (u16 *)((u8 *)alg_id_field +
+                                       SHA256_DIGEST_SIZE + sizeof(u16));
+                               break;
+                       case TPM_ALG_SHA1:
+                               memcpy(&digests[j].digest[0], dptr,
+                                      SHA1_DIGEST_SIZE);
+                               alg_id_field = (u16 *)((u8 *)alg_id_field +
+                                       SHA1_DIGEST_SIZE + sizeof(u16));
+                               break;
+                       default:
+                               break;
+                       }
+               }
Quoted 8 lines
Quoted 1 line

+ }

If we fall off the end of the loop, we never increase alg_id_field, and subsequent iterations will malfunction. But we apparently will write zeros (or fail?) if we have an unsupported algorithm, because we are asking to extend all allocated banks. I think. This code is gross. It's plausible that this whole sequence is impossible unless something malicious is going on.

Noted, there does look like there is an issue there. Thank you for the analysis.

Quoted 5 lines

Also, and I'm sort of replying to the wrong patch here, how trustworthy is the data that's used to populate tpm_algs in the stub? I don't think the results will be very pretty if tpm_algs ends up being incorrect.

We gather the list of algorithms used from the DRTM event log which the TXT/ACM phase initializes and begins populating. One thing to note here is that in the early setup kernel stub code, we would fail to boot if we saw an algorithm we did not support so we would not reach a state where we were simply ignoring an active bank as you mentioned in an earlier reply. But I also understand your point about limiting the functionality to just a subset of algorithms.

Thank you for your feedback,
Ross

#58 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

In reply to Andy Lutomirski

Hey Luto!

Let me try to address your concerns below.

Quoted 31 lines · Andy Lutomirski
Quoted 15 lines · James Bottomley
Quoted 4 lines · Andy Lutomirski

Linux should not use TPM2_PCR_Extend *at all*. Instead, Linux should exclusively use TPM2_PCR_Event. I would expect that passing, say, the entire kernel image to TPM2_PCR_Event would be a big mistake, so instead Linux should hash the relevant data with a reasonable

Quoted 8 lines

suggestion of hashes (which includes, mandatorily, SHA-384 and *does not* include SHA-1, and may or may not be configurable at build time to include things like SM3), concatenate them, and pass that to TPM2_PCR_Event. And Linux should make the value that it passed to TPM2_PCR_Event readily accessible to software using it, and should also include some straightforward tooling to calculate it from a given input so that software that wants to figure out what value to expect in a PCR can easily do so.

Quoted 6 lines

Just for clarity, this is about how the agile log format works. Each event entry in the log contains a list of bank hashes and the extends occur in log event order, so replaying a log should get you to exactly the head PCR value of each bank. If a log doesn't understand a format, like SM3, then an entry for it doesn't appear in the log and a replay says nothing about the PCR value.

I have no idea what the "agile log format" is or what all the formats in existence are. I found section 4.2.4 here:

https://trustedcomputinggroup.org/wp-content/uploads/TCG_IWG_CEL_v1_r0p41_pub.pdf

It says:

Quoted 13 lines

This field contains the list of the digest values Extended. The Extend method varies with TPM command, so there is
no uniform meaning of TPM Extend in this instance, and separate descriptions are unavoidable. If using the
TPM2_PCR_Extend command, this field is the data sent to the TPM (i.e., not the resulting value of the PCR after the TPM2_PCR_Extend command completes). If using the TPM2_PCR_Event command, this field contains the digest
structure returned by the TPM2_PCR_Event command (that contains the digest(s) submitted to each PCR bank as
the internal Extend operation). This field SHALL contain the information from the TPML_DIGEST_VALUES used in
the Extend operation.

Let me start with providing background on the two measurement policies that is implemented by Intel TXT (from Intel TXT Developers Guide):

- Maximum Agility PCR Extend Policy: ACM can support algorithm agile
commands TPM2_PCR_Event; TPM2_HashSequenceStart; TPM2_HashUpdate; TPM2_EventSequenceComplete. When this policy is selected, ACM will use the commands above if not all PCR algorithms are covered by embedded set of algorithms and will extend all existing PCR banks. Side effect of this policy is possible performance loss.

‒ Maximum Performance PCR Extend Policy: ACM can support several hash algorithms via embedded SW. When this policy is selected, ACM will use embedded SW to compute hashes and then will use TPM2_PCR_Extend commands to extend them into PCRs. If PCRs utilizing hash algorithms not supported by SW are discovered, they will be capped with “1” value. This policy, when selected, will ensure maximum possible performance but has side effect of possible capping of some of the PCRs.

Allow me to clarify/expand on the last statement in Maximum Agility. There is almost certainly a performance loss as anything larger than 1024 bytes, for example the Linux kernel, the ACM will bit-banging the bytes to the TPM using the TPM2_Hash* functions.

Before addressing the next point, I would also clarify how the D-CRTM measurement taken by the CPU is done. It uses the _TPM_HASH_* functions, Section 22.9 of TPM2 Commands specification, to store SHA256(SINIT ACM) | EDX into all active PCR banks. For clarity, when this done, EDX holds the 4-byte value of the SENTER parameters for which 0 is the only valid value currently.

Quoted 6 lines

So we're logging the values with which we extend the PCRs. Once upon a time, someone decided it was okay to skip extending a PCR bank:

https://google.github.io/security-research/pocs/bios/tpm-carte-blanche/writeup.html

and it was not a great idea.

Let's begin by why/how that attack occurs. The TPM Carte Blanche attack took advantage of the fact that without BootGuard in place, the SRTM measurements are done by the software/firmware, to include the self-referential S-CRTM measurement. In particular, for the target platform, it just so happens that it was possible to construct a configuration where not a single hash would be sent to the SHA256 bank. This allowed the attacker the ability to replay any set of measurements, i.e. carte blanche control, into a completely empty PCR bank for which the attestation service would accept quotes. The key to this attack requires both, access to an empty PCR bank, and an attestation service that will accept a quote with only the exploited bank present.

Let us return to my statements above, which will demonstrate why TXT/DRTM completely invalidates the attack. First, as noted above, when the CPU is processing the GETSEC[SENTER] instruction, it (the CPU) will compute the D-CRTM as SHA256(SINIT ACM) | EDX, sending it to the TPM using _TPM_HASH_* functions. The _TPM_HASH_* functions result in all PCR banks to be extended with the D-CRTM value. If Maximum Performance PCR Extend policy is in use, which is the default policy used by TrenchBoot, any algorithm not supported by the ACM is capped by sending the value "1" as the digest value for the extend. Therefore, after the TXT sequence has completed and before control is given to the Linux kernel by the ACM, all PCR banks will consist of either, the D-CRTM + all ACM measurements, or the D-CRTM + TPM2_PCR_Extend(0x1). There will be no PCR banks with empty DRTM PCRs, thus none of the banks would be usable for a TPM Carte Blanche-style attack.

Quoted 9 lines

There seem to be six (!) currently defined hashes: SHA1, SHA256, SHA384, SHA512, SM2 and SM3. I haven't spotted anything promising not to add more. It seems to be that Linux really really ought to:

(a) extend all banks. Not all banks that the maintainers like, and not all banks that the maintainers knew about when having this discussion. *All* banks. That means TPM2_PCR_Event(). (Or we refuse to boot if there's a bank we don't like.)

Quoted 15 lines

(b) Make a best effort to notice if something is wrong with the TPM and/or someone is MITMing us and messing with us. That means computing the hash algorithms we actually support and checking whether TPM2_PCR_Event() returns the right thing. I'm not seeing a specific attack that seems likely that this prevents, but it does seem like decent defense in depth, and if someone chooses to provision a machine by reading its event log and then subsequently getting an attestation that a future event log matches what was read, then avoiding letting an attacker who temporarily controls the TPM connection from corrupting the results seems wise. And I don't see anything at all that we gain by removing a check that (TPM's reported SHA1 == what we calculated) in the name of "not supporting SHA1") other than a few hundred bytes of object code. (And yes, SHA1 is much more likely to be supported than SM3, so it's not absurd to implement SHA1 and not implement SM3.)

Or,

(c) Upon initialization, cap the PCR banks with unsupported algorithms using a well-known value.

A problem with (a) is that the result will be an unorthodox event, PCR_EXTEND(H(H'(data))). An attestation verifier will have to be aware of that this is being done, and have a way to determine which method was used for each event. This creates a potentially expensive cost for any existing attestation solutions to incorporate support for the unorthodox event. At least for DRTM solutions, it seeks to solve a problem that TXT does not experience.

For Linux Secure Launch, I would like to propose an alternative to what the current logic does in the setup kernel. Specifically, Secure Launch will trigger a TXT reset when an unsupported algorithm is encountered. Instead, I would like to propose the adoption of (c), and have it extends a well-known, fixed value for unsupported algorithms. Secure Launch can leverage the fact that the TPM driver's extend function already expects to be given digests for all active algorithms. Therefore, it will record the well-known value, 0x01 to follow the ACM, into the digest buffers of any algorithms that Secure Launch does not support. This will result in the well-known value being extended each time a measurement is recorded. This will not be a problem as no one should be using those banks for attestation and can ignore those digests in the event log.

I would like to note that we made a conscious design decision early on to use the PCR performance policy approach. We weighed a variety of security concerns, hardware availability, and the practicality of integrating the capability into our respective efforts. I do not want you to feel as though we are not taking your comments seriously. Ross reached out to some their contacts, and I reached out to a colleague with domain experience as well. From a cursory review, no one saw an issue from a crypto standpoint, beyond some algorithm recommendations. As we highlighted, they did caution about the resulting unorthodox measurement that would impose a burden on attestation solutions.

Hopefully With the background and context presented, you would agree the above is a reasonable approach. If you do have concerns, please let us know.

V/r,
Daniel P. Smith

#59 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 62 lines · Daniel P. Smith

Hey Luto!

Let me try to address your concerns below.

Quoted 31 lines · Andy Lutomirski
Quoted 15 lines · James Bottomley
Quoted 4 lines · Andy Lutomirski

Linux should not use TPM2_PCR_Extend *at all*. Instead, Linux should exclusively use TPM2_PCR_Event. I would expect that passing, say, the entire kernel image to TPM2_PCR_Event would be a big mistake, so instead Linux should hash the relevant data with a reasonable

Quoted 8 lines

suggestion of hashes (which includes, mandatorily, SHA-384 and *does not* include SHA-1, and may or may not be configurable at build time to include things like SM3), concatenate them, and pass that to TPM2_PCR_Event. And Linux should make the value that it passed to TPM2_PCR_Event readily accessible to software using it, and should also include some straightforward tooling to calculate it from a given input so that software that wants to figure out what value to expect in a PCR can easily do so.

Quoted 6 lines

Just for clarity, this is about how the agile log format works. Each event entry in the log contains a list of bank hashes and the extends occur in log event order, so replaying a log should get you to exactly the head PCR value of each bank. If a log doesn't understand a format, like SM3, then an entry for it doesn't appear in the log and a replay says nothing about the PCR value.

I have no idea what the "agile log format" is or what all the formats in existence are. I found section 4.2.4 here:

https://trustedcomputinggroup.org/wp-content/uploads/TCG_IWG_CEL_v1_r0p41_pub.pdf

It says:

Quoted 13 lines

This field contains the list of the digest values Extended. The Extend method varies with TPM command, so there is
no uniform meaning of TPM Extend in this instance, and separate descriptions are unavoidable. If using the
TPM2_PCR_Extend command, this field is the data sent to the TPM (i.e., not the resulting value of the PCR after the TPM2_PCR_Extend command completes). If using the TPM2_PCR_Event command, this field contains the digest
structure returned by the TPM2_PCR_Event command (that contains the digest(s) submitted to each PCR bank as
the internal Extend operation). This field SHALL contain the information from the TPML_DIGEST_VALUES used in
the Extend operation.

Let me start with providing background on the two measurement policies that is implemented by Intel TXT (from Intel TXT Developers Guide):

- Maximum Agility PCR Extend Policy: ACM can support algorithm agile
commands TPM2_PCR_Event; TPM2_HashSequenceStart; TPM2_HashUpdate; TPM2_EventSequenceComplete. When this policy is selected, ACM will use the commands above if not all PCR algorithms are covered by embedded set of algorithms and will extend all existing PCR banks. Side effect of this policy is possible performance loss.

Quoted 8 lines

‒ Maximum Performance PCR Extend Policy: ACM can support several hash algorithms via embedded SW. When this policy is selected, ACM will use embedded SW to compute hashes and then will use TPM2_PCR_Extend commands to extend them into PCRs. If PCRs utilizing hash algorithms not supported by SW are discovered, they will be capped with “1” value. This policy, when selected, will ensure maximum possible performance but has side effect of possible capping of some of the PCRs.

What is responsible for choosing which of these policies to use?

Quoted 4 lines

Allow me to clarify/expand on the last statement in Maximum Agility. There is almost certainly a performance loss as anything larger than 1024 bytes, for example the Linux kernel, the ACM will bit-banging the bytes to the TPM using the TPM2_Hash* functions.

Surely, if Linux's stub started using TPM2_PCR_Event, it would first hash any large inputs and then send the _hash_ to TPM2_PCR_Event instead of, say, bit-banging the entire initramfs to the TPM. But you're talking about the ACM and I'm talking about the Linux stub code in this patchset.

But the capping-with-"1" does suggest that maybe one can actually cap with "1" without preventing downstream software from consuming the event log.

Quoted 29 lines

Before addressing the next point, I would also clarify how the D-CRTM measurement taken by the CPU is done. It uses the _TPM_HASH_* functions, Section 22.9 of TPM2 Commands specification, to store SHA256(SINIT ACM) | EDX into all active PCR banks. For clarity, when this done, EDX holds the 4-byte value of the SENTER parameters for which 0 is the only valid value currently.

Quoted 6 lines

So we're logging the values with which we extend the PCRs. Once upon a time, someone decided it was okay to skip extending a PCR bank:

https://google.github.io/security-research/pocs/bios/tpm-carte-blanche/writeup.html

and it was not a great idea.

Let's begin by why/how that attack occurs. The TPM Carte Blanche attack took advantage of the fact that without BootGuard in place, the SRTM measurements are done by the software/firmware, to include the self-referential S-CRTM measurement. In particular, for the target platform, it just so happens that it was possible to construct a configuration where not a single hash would be sent to the SHA256 bank. This allowed the attacker the ability to replay any set of measurements, i.e. carte blanche control, into a completely empty PCR bank for which the attestation service would accept quotes. The key to this attack requires both, access to an empty PCR bank, and an attestation service that will accept a quote with only the exploited bank present.

Quoted 14 lines

Let us return to my statements above, which will demonstrate why TXT/DRTM completely invalidates the attack. First, as noted above, when the CPU is processing the GETSEC[SENTER] instruction, it (the CPU) will compute the D-CRTM as SHA256(SINIT ACM) | EDX, sending it to the TPM using _TPM_HASH_* functions. The _TPM_HASH_* functions result in all PCR banks to be extended with the D-CRTM value. If Maximum Performance PCR Extend policy is in use, which is the default policy used by TrenchBoot, any algorithm not supported by the ACM is capped by sending the value "1" as the digest value for the extend. Therefore, after the TXT sequence has completed and before control is given to the Linux kernel by the ACM, all PCR banks will consist of either, the D-CRTM + all ACM measurements, or the D-CRTM + TPM2_PCR_Extend(0x1). There will be no PCR banks with empty DRTM PCRs, thus none of the banks would be usable for a TPM Carte Blanche-style attack.

Sure, a "carte blanche" attack in the sense that the PCRs are entirely blank won't happen, but if any component in the chain does not extend a bank, then an attacker can replace the code _after_ that component without being noticed.

Quoted 11 lines

(c) Upon initialization, cap the PCR banks with unsupported algorithms using a well-known value.

A problem with (a) is that the result will be an unorthodox event, PCR_EXTEND(H(H'(data))). An attestation verifier will have to be aware of that this is being done, and have a way to determine which method was used for each event. This creates a potentially expensive cost for any existing attestation solutions to incorporate support for the unorthodox event. At least for DRTM solutions, it seeks to solve a problem that TXT does not experience.

Quoted 13 lines

For Linux Secure Launch, I would like to propose an alternative to what the current logic does in the setup kernel. Specifically, Secure Launch will trigger a TXT reset when an unsupported algorithm is encountered. Instead, I would like to propose the adoption of (c), and have it extends a well-known, fixed value for unsupported algorithms. Secure Launch can leverage the fact that the TPM driver's extend function already expects to be given digests for all active algorithms. Therefore, it will record the well-known value, 0x01 to follow the ACM, into the digest buffers of any algorithms that Secure Launch does not support. This will result in the well-known value being extended each time a measurement is recorded. This will not be a problem as no one should be using those banks for attestation and can ignore those digests in the event log.

This seems to be at least not terrible.

Or I suppose one could use TPM_HASH_ functions? The spec for them is somewhat impenetrable to me.

Quoted 17 lines

I would like to note that we made a conscious design decision early on to use the PCR performance policy approach. We weighed a variety of security concerns, hardware availability, and the practicality of integrating the capability into our respective efforts. I do not want you to feel as though we are not taking your comments seriously. Ross reached out to some their contacts, and I reached out to a colleague with domain experience as well. From a cursory review, no one saw an issue from a crypto standpoint, beyond some algorithm recommendations. As we highlighted, they did caution about the resulting unorthodox measurement that would impose a burden on attestation solutions.

Hopefully With the background and context presented, you would agree the above is a reasonable approach. If you do have concerns, please let us know.

V/r,
Daniel P. Smith

Signature · 2 lines
Andy Lutomirski
AMA Capital Management, LLC
#60 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 73 lines · Andy Lutomirski
Quoted 62 lines · Daniel P. Smith

Hey Luto!

Let me try to address your concerns below.

Quoted 31 lines · Andy Lutomirski
Quoted 15 lines · James Bottomley
Quoted 4 lines · Andy Lutomirski

Linux should not use TPM2_PCR_Extend *at all*. Instead, Linux should exclusively use TPM2_PCR_Event. I would expect that passing, say, the entire kernel image to TPM2_PCR_Event would be a big mistake, so instead Linux should hash the relevant data with a reasonable

Quoted 8 lines

suggestion of hashes (which includes, mandatorily, SHA-384 and *does not* include SHA-1, and may or may not be configurable at build time to include things like SM3), concatenate them, and pass that to TPM2_PCR_Event. And Linux should make the value that it passed to TPM2_PCR_Event readily accessible to software using it, and should also include some straightforward tooling to calculate it from a given input so that software that wants to figure out what value to expect in a PCR can easily do so.

Quoted 6 lines

Just for clarity, this is about how the agile log format works. Each event entry in the log contains a list of bank hashes and the extends occur in log event order, so replaying a log should get you to exactly the head PCR value of each bank. If a log doesn't understand a format, like SM3, then an entry for it doesn't appear in the log and a replay says nothing about the PCR value.

I have no idea what the "agile log format" is or what all the formats in existence are. I found section 4.2.4 here:

https://trustedcomputinggroup.org/wp-content/uploads/TCG_IWG_CEL_v1_r0p41_pub.pdf

It says:

Quoted 13 lines

This field contains the list of the digest values Extended. The Extend method varies with TPM command, so there is
no uniform meaning of TPM Extend in this instance, and separate descriptions are unavoidable. If using the
TPM2_PCR_Extend command, this field is the data sent to the TPM (i.e., not the resulting value of the PCR after the TPM2_PCR_Extend command completes). If using the TPM2_PCR_Event command, this field contains the digest
structure returned by the TPM2_PCR_Event command (that contains the digest(s) submitted to each PCR bank as
the internal Extend operation). This field SHALL contain the information from the TPML_DIGEST_VALUES used in
the Extend operation.

Let me start with providing background on the two measurement policies that is implemented by Intel TXT (from Intel TXT Developers Guide):

- Maximum Agility PCR Extend Policy: ACM can support algorithm agile
commands TPM2_PCR_Event; TPM2_HashSequenceStart; TPM2_HashUpdate; TPM2_EventSequenceComplete. When this policy is selected, ACM will use the commands above if not all PCR algorithms are covered by embedded set of algorithms and will extend all existing PCR banks. Side effect of this policy is possible performance loss.

Quoted 8 lines

‒ Maximum Performance PCR Extend Policy: ACM can support several hash algorithms via embedded SW. When this policy is selected, ACM will use embedded SW to compute hashes and then will use TPM2_PCR_Extend commands to extend them into PCRs. If PCRs utilizing hash algorithms not supported by SW are discovered, they will be capped with “1” value. This policy, when selected, will ensure maximum possible performance but has side effect of possible capping of some of the PCRs.

Quoted 1 line

What is responsible for choosing which of these policies to use?

It is setup by the Dynamic Launch Preamble (GRUB) that will invoke the Dynamic Launch Event (GETSEC[SENTER]).

Quoted 10 lines
Quoted 4 lines

Allow me to clarify/expand on the last statement in Maximum Agility. There is almost certainly a performance loss as anything larger than 1024 bytes, for example the Linux kernel, the ACM will bit-banging the bytes to the TPM using the TPM2_Hash* functions.

Surely, if Linux's stub started using TPM2_PCR_Event, it would first hash any large inputs and then send the _hash_ to TPM2_PCR_Event instead of, say, bit-banging the entire initramfs to the TPM. But you're talking about the ACM and I'm talking about the Linux stub code in this patchset.

I think there might be some confusion on how this starts, let me provide a quick, enumerated flow:

1. GRUB prepares launch environment (Linux kernel, SINIT ACM, OsSinitData) and calls GETSEC[SENTER] instruction
2. The CPU signals PCH that DRTM has started, unlocking Locality 4
3. The CPU loads the SINIT ACM into CRAM, measures it with SHA256 and uses _TPM_HASH_START /_TPM_HASH_DATA / _TPM_HASH / _TPM_HASH_END to extend SHA256(ACM) | EDX into PCR17 for all PCR banks
4. The CPU then jumps into the ACM
5. The ACM does a series of actions and measurements of the system into PCRs 17 and 18 for SHA1, SHA256, SHA384, and SM3_256 (PCR Performance Policy)
6. The ACM then measures the "MLE", the Linux kernel in this case, using the same algorithms
7. The ACM then caps PCRs 17 and 18 by extending 0x01 for any unsupported algorithm the TPM supports.
8. The ACM then jumps to the Secure Launch entry point in the setup kernel

If the initramfs is external, then yes, the setup kernel would get to do the measurement. If it is a unified kernel image, well, that whole thing will be bit banged to the TPM by the ACM if the PCR Agile Policy is used.

Quoted 3 lines

But the capping-with-"1" does suggest that maybe one can actually cap with "1" without preventing downstream software from consuming the event log.

Correct, capping the PCR should never block downstream software from consuming the event log.

Quoted 44 lines
Quoted 29 lines

Before addressing the next point, I would also clarify how the D-CRTM measurement taken by the CPU is done. It uses the _TPM_HASH_* functions, Section 22.9 of TPM2 Commands specification, to store SHA256(SINIT ACM) | EDX into all active PCR banks. For clarity, when this done, EDX holds the 4-byte value of the SENTER parameters for which 0 is the only valid value currently.

Quoted 6 lines

So we're logging the values with which we extend the PCRs. Once upon a time, someone decided it was okay to skip extending a PCR bank:

https://google.github.io/security-research/pocs/bios/tpm-carte-blanche/writeup.html

and it was not a great idea.

Let's begin by why/how that attack occurs. The TPM Carte Blanche attack took advantage of the fact that without BootGuard in place, the SRTM measurements are done by the software/firmware, to include the self-referential S-CRTM measurement. In particular, for the target platform, it just so happens that it was possible to construct a configuration where not a single hash would be sent to the SHA256 bank. This allowed the attacker the ability to replay any set of measurements, i.e. carte blanche control, into a completely empty PCR bank for which the attestation service would accept quotes. The key to this attack requires both, access to an empty PCR bank, and an attestation service that will accept a quote with only the exploited bank present.

Quoted 14 lines

Let us return to my statements above, which will demonstrate why TXT/DRTM completely invalidates the attack. First, as noted above, when the CPU is processing the GETSEC[SENTER] instruction, it (the CPU) will compute the D-CRTM as SHA256(SINIT ACM) | EDX, sending it to the TPM using _TPM_HASH_* functions. The _TPM_HASH_* functions result in all PCR banks to be extended with the D-CRTM value. If Maximum Performance PCR Extend policy is in use, which is the default policy used by TrenchBoot, any algorithm not supported by the ACM is capped by sending the value "1" as the digest value for the extend. Therefore, after the TXT sequence has completed and before control is given to the Linux kernel by the ACM, all PCR banks will consist of either, the D-CRTM + all ACM measurements, or the D-CRTM + TPM2_PCR_Extend(0x1). There will be no PCR banks with empty DRTM PCRs, thus none of the banks would be usable for a TPM Carte Blanche-style attack.

Quoted 4 lines

Sure, a "carte blanche" attack in the sense that the PCRs are entirely blank won't happen, but if any component in the chain does not extend a bank, then an attacker can replace the code _after_ that component without being noticed.

There are two scenarios that could be considered here. The first is if an algorithm is added to the TPM that neither the ACM nor the TPM driver supports. The second is if an algorithm supported by the ACM and the TPM driver is added, but not supported by Secure Launch.

In the first case, the PCR and corresponding log entries for the new algorithm will have the D-CRTM event and Cap (0x01) event. Rendering the bank unusable for the attacker.

For the second case, this is a "it all depends on the usage". First and foremost, I would say it is ultimately a failure of the Attestation Service if it evaluated algorithms that Secure Launch is not advertised to support.

With that, let's consider the options for bad components under the second case:
- A bad kernel image will be detected since the ACM is what measures it.
- A bad initramfs, assuming that (c) below was not implemented, then the initramfs could add a fake entry for itself into the bank in question.
- Any component after this depends on the use case. The primary use case currently is to combine Secure Launch with TrenchBoot additions to u-root making it an intermediate bootloader that kexec's into the target kernel. I bring this up because when the kexec is done, Secure Launch does GETSEC[SEXIT]. The result being that it closes all localities except Locality 0. They cannot be re-opened without doing a new Dynamic Launch, which will reset them all back to 0.

I would also like to note that Secure Launch would not be the only in-kernel capability that would have to scramble to add support for the additional algorithm if it suddenly became available to block a Carte Blanche attack.

Quoted 25 lines
Quoted 11 lines

(c) Upon initialization, cap the PCR banks with unsupported algorithms using a well-known value.

A problem with (a) is that the result will be an unorthodox event, PCR_EXTEND(H(H'(data))). An attestation verifier will have to be aware of that this is being done, and have a way to determine which method was used for each event. This creates a potentially expensive cost for any existing attestation solutions to incorporate support for the unorthodox event. At least for DRTM solutions, it seeks to solve a problem that TXT does not experience.

Quoted 13 lines

For Linux Secure Launch, I would like to propose an alternative to what the current logic does in the setup kernel. Specifically, Secure Launch will trigger a TXT reset when an unsupported algorithm is encountered. Instead, I would like to propose the adoption of (c), and have it extends a well-known, fixed value for unsupported algorithms. Secure Launch can leverage the fact that the TPM driver's extend function already expects to be given digests for all active algorithms. Therefore, it will record the well-known value, 0x01 to follow the ACM, into the digest buffers of any algorithms that Secure Launch does not support. This will result in the well-known value being extended each time a measurement is recorded. This will not be a problem as no one should be using those banks for attestation and can ignore those digests in the event log.

Quoted 1 line

This seems to be at least not terrible.

With (c) in place, it would completely close off the second case for the case when the initramfs is not part of the kernel image.

Quoted 2 lines

Or I suppose one could use TPM_HASH_ functions? The spec for them is somewhat impenetrable to me.

Do you mean TPM2_HashSequenceStart / TPM2_SequenceUpdate / TPM2_EventSequenceComplete? If you really meant _TPM_Hash_Start / _TPM_Hash_Data / _TPM_Hash_End, then no, you cannot call these. They are not sent via the Command Buffer but directly through the TPM interface. Have a look at Section 34 of the TPM Architecture[1]. While the spec authors wrote it generically, as if there is more than one way for an H-CRTM to be established, the only way available for x86 is DRTM.

[1] https://trustedcomputinggroup.org/wp-content/uploads/TPM-2.0-1.83-Part-1-Architecture.pdf

Ross has prepared a new version of the series using (c), which actually reduces the size and complexity of the SL setup code. Once it has been tested, Ross will post it so you can have some concrete code to look at and consider the proposal.

V/r,
Daniel P. Smith

#61 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

In reply to Eric Biggers
Quoted 26 lines · Eric Biggers
Quoted 13 lines · Ross Philipson

From: "Daniel P. Smith" <[email protected]>

For better or worse, Secure Launch needs SHA-1 and SHA-256. The choice of hashes used lie with the platform firmware, not with software, and is often outside of the users control.

Even if we'd prefer to use SHA-256-only, if firmware elected to start us with the SHA-1 and SHA-256 backs active, we still need SHA-1 to parse the TPM event log thus far, and deliberately cap the SHA-1 PCRs in order to safely use SHA-256 for everything else.

The SHA-1 code here has its origins in the code from the main kernel:

Quoted 8 lines

commit c4d5b9ffa31f ("crypto: sha1 - implement base layer for SHA-1")

A modified version of this code was introduced to the lib/crypto/sha1.c to bring it in line with the SHA-256 code and allow it to be pulled into the setup kernel in the same manner as SHA-256 is.

Thanks. This explanation doesn't seem to have made it into the actual code or documentation. Can you please get it into a more permanent location?

Quoted 7 lines

Also, can you point to where the "deliberately cap the SHA-1 PCRs" thing happens in the code?

That paragraph is also phrased as a hypothetical, "Even if we'd prefer to use SHA-256-only". That implies that you do not, in fact, prefer SHA-256 only. Is that the case? Sure, maybe there are situations where you *have* to use SHA-1, but why would you not at least *prefer* SHA-256?

Yes those are fair points. We will address them and indicate we prefer SHA-256 or better.

Quoted 12 lines
Quoted 12 lines

/*
* An implementation of SHA-1's compression function. Don't use in new code!
* You shouldn't be using SHA-1, and even if you *have* to use SHA-1, this isn't
* the correct way to hash something with SHA-1 (use crypto_shash instead). */
#define SHA1_DIGEST_WORDS (SHA1_DIGEST_SIZE / 4)
#define SHA1_WORKSPACE_WORDS 16
void sha1_init(__u32 *buf);
void sha1_transform(__u32 *digest, const char *data, __u32 *W); +void sha1(const u8 *data, unsigned int len, u8 *out);
Also, the comment above needs to be updated.

Ack, will address.

Thank you

Quoted 2 lines

- Eric

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

maintainer
#62 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

In reply to Eric Biggers
Quoted 20 lines · Eric Biggers
Quoted 19 lines · Ross Philipson

From: "Daniel P. Smith" <[email protected]>

For better or worse, Secure Launch needs SHA-1 and SHA-256. The choice of hashes used lie with the platform firmware, not with software, and is often outside of the users control.

Even if we'd prefer to use SHA-256-only, if firmware elected to start us with the SHA-1 and SHA-256 backs active, we still need SHA-1 to parse the TPM event log thus far, and deliberately cap the SHA-1 PCRs in order to safely use SHA-256 for everything else.

The SHA-1 code here has its origins in the code from the main kernel:

commit c4d5b9ffa31f ("crypto: sha1 - implement base layer for SHA-1")

A modified version of this code was introduced to the lib/crypto/sha1.c to bring it in line with the SHA-256 code and allow it to be pulled into the setup kernel in the same manner as SHA-256 is.

Quoted 5 lines
Quoted 2 lines

Thanks. This explanation doesn't seem to have made it into the actual code or documentation. Can you please get it into a more permanent location?

I see that a new version of the patchset was sent out but this suggestion was not taken. Are you planning to address it?

- Eric

#63 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 21 lines · Eric Biggers' via trenchboot-devel
Quoted 20 lines · Eric Biggers
Quoted 19 lines · Ross Philipson

From: "Daniel P. Smith" <[email protected]>

For better or worse, Secure Launch needs SHA-1 and SHA-256. The choice of hashes used lie with the platform firmware, not with software, and is often outside of the users control.

Even if we'd prefer to use SHA-256-only, if firmware elected to start us with the SHA-1 and SHA-256 backs active, we still need SHA-1 to parse the TPM event log thus far, and deliberately cap the SHA-1 PCRs in order to safely use SHA-256 for everything else.

The SHA-1 code here has its origins in the code from the main kernel:

commit c4d5b9ffa31f ("crypto: sha1 - implement base layer for SHA-1")

A modified version of this code was introduced to the lib/crypto/sha1.c to bring it in line with the SHA-256 code and allow it to be pulled into the setup kernel in the same manner as SHA-256 is.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Thanks. This explanation doesn't seem to have made it into the actual code or documentation. Can you please get it into a more permanent location?

I see that a new version of the patchset was sent out but this suggestion was not taken. Are you planning to address it?

Sorry we sort of overlooked that part of the request. We will take the latest commit message, clean it up a little and put it in boot/compressed/sha1.c file as a comment. I believe that is what you would like us to do.

Thanks
Ross

Quoted 3 lines

- Eric

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

maintainer
#64 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 28 lines · On Wed, Aug 28, 2024 at 01:14:45PM -0700, [email protected] wrote:
Quoted 27 lines · Eric Biggers' via trenchboot-devel
Quoted 25 lines · Eric Biggers
Quoted 13 lines · Ross Philipson

From: "Daniel P. Smith" <[email protected]>

For better or worse, Secure Launch needs SHA-1 and SHA-256. The choice of hashes used lie with the platform firmware, not with software, and is often outside of the users control.

Even if we'd prefer to use SHA-256-only, if firmware elected to start us with the SHA-1 and SHA-256 backs active, we still need SHA-1 to parse the TPM event log thus far, and deliberately cap the SHA-1 PCRs in order to safely use SHA-256 for everything else.

The SHA-1 code here has its origins in the code from the main kernel:

Quoted 8 lines

commit c4d5b9ffa31f ("crypto: sha1 - implement base layer for SHA-1")

A modified version of this code was introduced to the lib/crypto/sha1.c to bring it in line with the SHA-256 code and allow it to be pulled into the setup kernel in the same manner as SHA-256 is.

Thanks. This explanation doesn't seem to have made it into the actual code or documentation. Can you please get it into a more permanent location?

Quoted 8 lines
Quoted 2 lines

I see that a new version of the patchset was sent out but this suggestion was not taken. Are you planning to address it?

Sorry we sort of overlooked that part of the request. We will take the latest commit message, clean it up a little and put it in boot/compressed/sha1.c file as a comment. I believe that is what you would like us to do.

Do users of this feature need to make a decision about SHA-1? If so there needs to be guidance in Documentation/. A comment in a .c file is not user facing.

- Eric

maintainer
#65 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

In reply to Ross Philipson
Quoted 50 lines · Ross Philipson

From: "Daniel P. Smith" <[email protected]>

For better or worse, Secure Launch needs SHA-1 and SHA-256. The choice of hashes used lie with the platform firmware, not with software, and is often outside of the users control.

Even if we'd prefer to use SHA-256-only, if firmware elected to start us with the SHA-1 and SHA-256 backs active, we still need SHA-1 to parse the TPM event log thus far, and deliberately cap the SHA-1 PCRs in order to safely use SHA-256 for everything else.

The SHA-1 code here has its origins in the code from the main kernel:

commit c4d5b9ffa31f ("crypto: sha1 - implement base layer for SHA-1")

A modified version of this code was introduced to the lib/crypto/sha1.c to bring it in line with the SHA-256 code and allow it to be pulled into the setup kernel in the same manner as SHA-256 is.

---
 arch/x86/boot/compressed/Makefile     |  2 +
 arch/x86/boot/compressed/early_sha1.c | 12 ++++
 include/crypto/sha1.h                 |  1 +
 lib/crypto/sha1.c                     | 81 +++++++++++++++++++++++++++
 4 files changed, 96 insertions(+)
 create mode 100644 arch/x86/boot/compressed/early_sha1.c
arch/x86/boot/compressed/Makefile +2 -0
index e9522c6893be..3307ebef4e1b 100644
@@ -118,6 +118,8 @@ vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi.o
 vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_mixed.o
 vmlinux-objs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
 
+vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o
+
 $(obj)/vmlinux: $(vmlinux-objs-y) FORCE
 	$(call if_changed,ld)
 
arch/x86/boot/compressed/early_sha1.c new file +2 -0
new file mode 100644
index 000000000000..8a9b904a73ab
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
+/*

arch/x86/boot/compressed/early_sha1.c line 12

Quoted 10 lines
arch/x86/boot/compressed/early_sha1.c new file +10 -0
+ * Copyright (c) 2024 Apertus Solutions, LLC.
+ */
+
+#include <linux/init.h>
+#include <linux/linkage.h>
+#include <linux/string.h>
+#include <asm/boot.h>
+#include <asm/unaligned.h>
+
+#include "../../../../lib/crypto/sha1.c"

}

Yep, make sense. Thinking only that should this be just sha1.c.

Comparing this to mainly drivers/firmware/efi/tpm.c, which is not early_tpm.c where the early actually probably would make more sense than here. Here sha1 primitive is just needed.

This is definitely a nitpick but why carry a prefix that is not that useful, right?

Quoted 16 lines
include/crypto/sha1.h +1 -0
index 044ecea60ac8..d715dd5332e1 100644
@@ -42,5 +42,6 @@ extern int crypto_sha1_finup(struct shash_desc *desc, const u8 *data,
 #define SHA1_WORKSPACE_WORDS	16
 void sha1_init(__u32 *buf);
 void sha1_transform(__u32 *digest, const char *data, __u32 *W);
+void sha1(const u8 *data, unsigned int len, u8 *out);
 
 #endif /* _CRYPTO_SHA1_H */
lib/crypto/sha1.c +0 -0
index 1aebe7be9401..10152125b338 100644

lib/crypto/sha1.c void sha1_init(__u32 *buf) · line 146

Quoted 10 lines
lib/crypto/sha1.c +7 -0
@@ -137,4 +137,85 @@ void sha1_init(__u32 *buf)
 }
 EXPORT_SYMBOL(sha1_init);
 
+static void __sha1_transform(u32 *digest, const char *data)
+{
+       u32 ws[SHA1_WORKSPACE_WORDS];
+
+       sha1_transform(digest, data, ws);
+
+       memzero_explicit(ws, sizeof(ws));

For the sake of future reference I'd carry always some inline comment with any memzero_explicit() call site.

lib/crypto/sha1.c void sha1_init(__u32 *buf) · line 155

Quoted 9 lines
lib/crypto/sha1.c +9 -0
+}
+
+static void sha1_update(struct sha1_state *sctx, const u8 *data, unsigned int len)
+{
+	unsigned int partial = sctx->count % SHA1_BLOCK_SIZE;
+
+	sctx->count += len;
+
+	if (likely((partial + len) >= SHA1_BLOCK_SIZE)) {
	if (unlikely((partial + len) < SHA1_BLOCK_SIZE))
		goto out;

?

Quoted 14 lines
lib/crypto/sha1.c +14 -0
+		int blocks;
+
+		if (partial) {
+			int p = SHA1_BLOCK_SIZE - partial;
+
+			memcpy(sctx->buffer + partial, data, p);
+			data += p;
+			len -= p;
+
+			__sha1_transform(sctx->state, sctx->buffer);
+		}
+
+		blocks = len / SHA1_BLOCK_SIZE;
+		len %= SHA1_BLOCK_SIZE;

lib/crypto/sha1.c void sha1_init(__u32 *buf) · line 179

Quoted 10 lines
lib/crypto/sha1.c +10 -0
+
+		if (blocks) {
+			while (blocks--) {
+				__sha1_transform(sctx->state, data);
+				data += SHA1_BLOCK_SIZE;
+			}
+		}
+		partial = 0;
+	}
+

out:

lib/crypto/sha1.c void sha1_init(__u32 *buf) · line 181

Quoted 2 lines
lib/crypto/sha1.c +2 -0
+	if (len)
+		memcpy(sctx->buffer + partial, data, len);

Why not just memcpy() unconditionally?

Quoted 24 lines
lib/crypto/sha1.c +24 -0
+}
+
+static void sha1_final(struct sha1_state *sctx, u8 *out)
+{
+	const int bit_offset = SHA1_BLOCK_SIZE - sizeof(__be64);
+	unsigned int partial = sctx->count % SHA1_BLOCK_SIZE;
+	__be64 *bits = (__be64 *)(sctx->buffer + bit_offset);
+	__be32 *digest = (__be32 *)out;
+	int i;
+
+	sctx->buffer[partial++] = 0x80;
+	if (partial > bit_offset) {
+		memset(sctx->buffer + partial, 0x0, SHA1_BLOCK_SIZE - partial);
+		partial = 0;
+
+		__sha1_transform(sctx->state, sctx->buffer);
+	}
+
+	memset(sctx->buffer + partial, 0x0, bit_offset - partial);
+	*bits = cpu_to_be64(sctx->count << 3);
+	__sha1_transform(sctx->state, sctx->buffer);
+
+	for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(__be32); i++)
+		put_unaligned_be32(sctx->state[i], digest++);

lib/crypto/sha1.c void sha1_init(__u32 *buf) · line 215

Quoted 10 lines
lib/crypto/sha1.c +10 -0
+
+	*sctx = (struct sha1_state){};
+}
+
+void sha1(const u8 *data, unsigned int len, u8 *out)
+{
+	struct sha1_state sctx = {0};
+
+	sha1_init(sctx.state);
+	sctx.count = 0;

Hmm... so shouldn't C99 take care of this given the initialization above? I'm not 100% sure here. I.e. given "= {0}", shouldn't this already be zero?

lib/crypto/sha1.c void sha1_init(__u32 *buf) · line 221

Quoted 6 lines
lib/crypto/sha1.c +5 -0
+	sha1_update(&sctx, data, len);
+	sha1_final(&sctx, out);
+}
+EXPORT_SYMBOL(sha1);
+
 MODULE_LICENSE("GPL");

BR, Jarkko

#66 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 63 lines · Jarkko Sakkinen
Quoted 50 lines · Ross Philipson

From: "Daniel P. Smith" <[email protected]>

For better or worse, Secure Launch needs SHA-1 and SHA-256. The choice of hashes used lie with the platform firmware, not with software, and is often outside of the users control.

Even if we'd prefer to use SHA-256-only, if firmware elected to start us with the SHA-1 and SHA-256 backs active, we still need SHA-1 to parse the TPM event log thus far, and deliberately cap the SHA-1 PCRs in order to safely use SHA-256 for everything else.

The SHA-1 code here has its origins in the code from the main kernel:

commit c4d5b9ffa31f ("crypto: sha1 - implement base layer for SHA-1")

A modified version of this code was introduced to the lib/crypto/sha1.c to bring it in line with the SHA-256 code and allow it to be pulled into the setup kernel in the same manner as SHA-256 is.

---
  arch/x86/boot/compressed/Makefile     |  2 +
  arch/x86/boot/compressed/early_sha1.c | 12 ++++
  include/crypto/sha1.h                 |  1 +
  lib/crypto/sha1.c                     | 81 +++++++++++++++++++++++++++
  4 files changed, 96 insertions(+)

create mode 100644 arch/x86/boot/compressed/early_sha1.c

arch/x86/boot/compressed/Makefile +2 -0
index e9522c6893be..3307ebef4e1b 100644
@@ -118,6 +118,8 @@ vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi.o
  vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_mixed.o
  vmlinux-objs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
 
+vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o
+
  $(obj)/vmlinux: $(vmlinux-objs-y) FORCE
  	$(call if_changed,ld)
 
arch/x86/boot/compressed/early_sha1.c new file +2 -0
new file mode 100644
index 000000000000..8a9b904a73ab
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
+/*

arch/x86/boot/compressed/early_sha1.c line 12

Quoted 10 lines
arch/x86/boot/compressed/early_sha1.c new file +10 -0
+ * Copyright (c) 2024 Apertus Solutions, LLC.
+ */
+
+#include <linux/init.h>
+#include <linux/linkage.h>
+#include <linux/string.h>
+#include <asm/boot.h>
+#include <asm/unaligned.h>
+
+#include "../../../../lib/crypto/sha1.c"

}

Quoted 8 lines

Yep, make sense. Thinking only that should this be just sha1.c.

Comparing this to mainly drivers/firmware/efi/tpm.c, which is not early_tpm.c where the early actually probably would make more sense than here. Here sha1 primitive is just needed.

This is definitely a nitpick but why carry a prefix that is not that useful, right?

I am not 100% sure what you mean here, sorry. Could you clarify about the prefix? Do you mean why did we choose early_*? There was precedent for doing that like early_serial_console.c.

Quoted 20 lines
Quoted 20 lines
include/crypto/sha1.h +1 -0
index 044ecea60ac8..d715dd5332e1 100644
@@ -42,5 +42,6 @@ extern int crypto_sha1_finup(struct shash_desc *desc, const u8 *data,
  #define SHA1_WORKSPACE_WORDS	16
  void sha1_init(__u32 *buf);
  void sha1_transform(__u32 *digest, const char *data, __u32 *W);
+void sha1(const u8 *data, unsigned int len, u8 *out);
 
  #endif /* _CRYPTO_SHA1_H */
lib/crypto/sha1.c +0 -0
index 1aebe7be9401..10152125b338 100644
@@ -137,4 +137,85 @@ void sha1_init(__u32 *buf)
  }
  EXPORT_SYMBOL(sha1_init);
 
Quoted 10 lines

lib/crypto/sha1.c void sha1_init(__u32 *buf) · line 146

Quoted 7 lines
lib/crypto/sha1.c +7 -0
+static void __sha1_transform(u32 *digest, const char *data)
+{
+       u32 ws[SHA1_WORKSPACE_WORDS];
+
+       sha1_transform(digest, data, ws);
+
+       memzero_explicit(ws, sizeof(ws));

For the sake of future reference I'd carry always some inline comment with any memzero_explicit() call site.

We can do that.

Quoted 15 lines

lib/crypto/sha1.c void sha1_init(__u32 *buf) · line 155

Quoted 10 lines
lib/crypto/sha1.c +9 -0
+}
+
+static void sha1_update(struct sha1_state *sctx, const u8 *data, unsigned int len)
+{
+	unsigned int partial = sctx->count % SHA1_BLOCK_SIZE;
+
+	sctx->count += len;
+
+	if (likely((partial + len) >= SHA1_BLOCK_SIZE)) {
	if (unlikely((partial + len) < SHA1_BLOCK_SIZE))
		goto out;
Quoted 1 line

?

We could do it that way. I guess it would cut down in indenting. I defer to Daniel Smith on this...

Quoted 28 lines
Quoted 15 lines
lib/crypto/sha1.c +14 -0
+		int blocks;
+
+		if (partial) {
+			int p = SHA1_BLOCK_SIZE - partial;
+
+			memcpy(sctx->buffer + partial, data, p);
+			data += p;
+			len -= p;
+
+			__sha1_transform(sctx->state, sctx->buffer);
+		}
+
+		blocks = len / SHA1_BLOCK_SIZE;
+		len %= SHA1_BLOCK_SIZE;

lib/crypto/sha1.c void sha1_init(__u32 *buf) · line 179

Quoted 10 lines
lib/crypto/sha1.c +10 -0
+
+		if (blocks) {
+			while (blocks--) {
+				__sha1_transform(sctx->state, data);
+				data += SHA1_BLOCK_SIZE;
+			}
+		}
+		partial = 0;
+	}
+

out:

Quoted 5 lines

lib/crypto/sha1.c void sha1_init(__u32 *buf) · line 181

Quoted 2 lines
lib/crypto/sha1.c +2 -0
+	if (len)
+		memcpy(sctx->buffer + partial, data, len);

Why not just memcpy() unconditionally?

... and this.

Quoted 30 lines
Quoted 30 lines
lib/crypto/sha1.c +30 -0
+}
+
+static void sha1_final(struct sha1_state *sctx, u8 *out)
+{
+	const int bit_offset = SHA1_BLOCK_SIZE - sizeof(__be64);
+	unsigned int partial = sctx->count % SHA1_BLOCK_SIZE;
+	__be64 *bits = (__be64 *)(sctx->buffer + bit_offset);
+	__be32 *digest = (__be32 *)out;
+	int i;
+
+	sctx->buffer[partial++] = 0x80;
+	if (partial > bit_offset) {
+		memset(sctx->buffer + partial, 0x0, SHA1_BLOCK_SIZE - partial);
+		partial = 0;
+
+		__sha1_transform(sctx->state, sctx->buffer);
+	}
+
+	memset(sctx->buffer + partial, 0x0, bit_offset - partial);
+	*bits = cpu_to_be64(sctx->count << 3);
+	__sha1_transform(sctx->state, sctx->buffer);
+
+	for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(__be32); i++)
+		put_unaligned_be32(sctx->state[i], digest++);
+
+	*sctx = (struct sha1_state){};
+}
+
+void sha1(const u8 *data, unsigned int len, u8 *out)
+{
Quoted 8 lines

lib/crypto/sha1.c void sha1_init(__u32 *buf) · line 215

Quoted 4 lines
lib/crypto/sha1.c +4 -0
+	struct sha1_state sctx = {0};
+
+	sha1_init(sctx.state);
+	sctx.count = 0;

Hmm... so shouldn't C99 take care of this given the initialization above? I'm not 100% sure here. I.e. given "= {0}", shouldn't this already be zero?

Yes it seems so. We will look at changing that.

Quoted 9 lines

lib/crypto/sha1.c void sha1_init(__u32 *buf) · line 221

Quoted 7 lines
lib/crypto/sha1.c +5 -0
+	sha1_update(&sctx, data, len);
+	sha1_final(&sctx, out);
+}
+EXPORT_SYMBOL(sha1);
+
  MODULE_LICENSE("GPL");

BR, Jarkko

Thanks
Ross

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

maintainer
#67 lore raw

Re: [PATCH v9 06/19] x86: Add early SHA-1 support for Secure Launch early measurements

Quoted 70 lines · EEST,
Quoted 69 lines · Jarkko Sakkinen
Quoted 50 lines · Ross Philipson

From: "Daniel P. Smith" <[email protected]>

For better or worse, Secure Launch needs SHA-1 and SHA-256. The choice of hashes used lie with the platform firmware, not with software, and is often outside of the users control.

Even if we'd prefer to use SHA-256-only, if firmware elected to start us with the SHA-1 and SHA-256 backs active, we still need SHA-1 to parse the TPM event log thus far, and deliberately cap the SHA-1 PCRs in order to safely use SHA-256 for everything else.

The SHA-1 code here has its origins in the code from the main kernel:

commit c4d5b9ffa31f ("crypto: sha1 - implement base layer for SHA-1")

A modified version of this code was introduced to the lib/crypto/sha1.c to bring it in line with the SHA-256 code and allow it to be pulled into the setup kernel in the same manner as SHA-256 is.

---
  arch/x86/boot/compressed/Makefile     |  2 +
  arch/x86/boot/compressed/early_sha1.c | 12 ++++
  include/crypto/sha1.h                 |  1 +
  lib/crypto/sha1.c                     | 81 +++++++++++++++++++++++++++
  4 files changed, 96 insertions(+)

create mode 100644 arch/x86/boot/compressed/early_sha1.c

arch/x86/boot/compressed/Makefile +2 -0
index e9522c6893be..3307ebef4e1b 100644
@@ -118,6 +118,8 @@ vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi.o
  vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_mixed.o
  vmlinux-objs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
 
+vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o
+
  $(obj)/vmlinux: $(vmlinux-objs-y) FORCE
  	$(call if_changed,ld)
 
arch/x86/boot/compressed/early_sha1.c new file +2 -0
new file mode 100644
index 000000000000..8a9b904a73ab
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
+/*

arch/x86/boot/compressed/early_sha1.c line 12

Quoted 10 lines
arch/x86/boot/compressed/early_sha1.c new file +10 -0
+ * Copyright (c) 2024 Apertus Solutions, LLC.
+ */
+
+#include <linux/init.h>
+#include <linux/linkage.h>
+#include <linux/string.h>
+#include <asm/boot.h>
+#include <asm/unaligned.h>
+
+#include "../../../../lib/crypto/sha1.c"

}

Yep, make sense. Thinking only that should this be just sha1.c.

Comparing this to mainly drivers/firmware/efi/tpm.c, which is not early_tpm.c where the early actually probably would make more sense than here. Here sha1 primitive is just needed.

Quoted 6 lines
Quoted 2 lines

This is definitely a nitpick but why carry a prefix that is not that useful, right?

I am not 100% sure what you mean here, sorry. Could you clarify about the prefix? Do you mean why did we choose early_*? There was precedent for doing that like early_serial_console.c.

Yep, that exactly. I'd just name as sha1.c.

Quoted 49 lines
Quoted 21 lines
Quoted 21 lines
include/crypto/sha1.h +1 -0
index 044ecea60ac8..d715dd5332e1 100644
@@ -42,5 +42,6 @@ extern int crypto_sha1_finup(struct shash_desc *desc, const u8 *data,
  #define SHA1_WORKSPACE_WORDS	16
  void sha1_init(__u32 *buf);
  void sha1_transform(__u32 *digest, const char *data, __u32 *W);
+void sha1(const u8 *data, unsigned int len, u8 *out);
 
  #endif /* _CRYPTO_SHA1_H */
lib/crypto/sha1.c +0 -0
index 1aebe7be9401..10152125b338 100644
@@ -137,4 +137,85 @@ void sha1_init(__u32 *buf)
  }
  EXPORT_SYMBOL(sha1_init);
 
Quoted 10 lines

lib/crypto/sha1.c void sha1_init(__u32 *buf) · line 146

Quoted 7 lines
lib/crypto/sha1.c +7 -0
+static void __sha1_transform(u32 *digest, const char *data)
+{
+       u32 ws[SHA1_WORKSPACE_WORDS];
+
+       sha1_transform(digest, data, ws);
+
+       memzero_explicit(ws, sizeof(ws));

For the sake of future reference I'd carry always some inline comment with any memzero_explicit() call site.

We can do that.

Quoted 15 lines

lib/crypto/sha1.c void sha1_init(__u32 *buf) · line 155

Quoted 10 lines
lib/crypto/sha1.c +9 -0
+}
+
+static void sha1_update(struct sha1_state *sctx, const u8 *data, unsigned int len)
+{
+	unsigned int partial = sctx->count % SHA1_BLOCK_SIZE;
+
+	sctx->count += len;
+
+	if (likely((partial + len) >= SHA1_BLOCK_SIZE)) {
	if (unlikely((partial + len) < SHA1_BLOCK_SIZE))
		goto out;
Quoted 4 lines
Quoted 1 line

?

We could do it that way. I guess it would cut down in indenting. I defer to Daniel Smith on this...

Yep, that's why I requested this.

Quoted 29 lines
Quoted 29 lines
Quoted 16 lines
lib/crypto/sha1.c +14 -0
+		int blocks;
+
+		if (partial) {
+			int p = SHA1_BLOCK_SIZE - partial;
+
+			memcpy(sctx->buffer + partial, data, p);
+			data += p;
+			len -= p;
+
+			__sha1_transform(sctx->state, sctx->buffer);
+		}
+
+		blocks = len / SHA1_BLOCK_SIZE;
+		len %= SHA1_BLOCK_SIZE;

lib/crypto/sha1.c void sha1_init(__u32 *buf) · line 179

Quoted 10 lines
lib/crypto/sha1.c +10 -0
+
+		if (blocks) {
+			while (blocks--) {
+				__sha1_transform(sctx->state, data);
+				data += SHA1_BLOCK_SIZE;
+			}
+		}
+		partial = 0;
+	}
+

out:

Quoted 7 lines
Quoted 5 lines

lib/crypto/sha1.c void sha1_init(__u32 *buf) · line 181

Quoted 2 lines
lib/crypto/sha1.c +2 -0
+	if (len)
+		memcpy(sctx->buffer + partial, data, len);

Why not just memcpy() unconditionally?

... and this.

It only adds complexity with no gain.

Quoted 40 lines
Quoted 31 lines
Quoted 31 lines
lib/crypto/sha1.c +30 -0
+}
+
+static void sha1_final(struct sha1_state *sctx, u8 *out)
+{
+	const int bit_offset = SHA1_BLOCK_SIZE - sizeof(__be64);
+	unsigned int partial = sctx->count % SHA1_BLOCK_SIZE;
+	__be64 *bits = (__be64 *)(sctx->buffer + bit_offset);
+	__be32 *digest = (__be32 *)out;
+	int i;
+
+	sctx->buffer[partial++] = 0x80;
+	if (partial > bit_offset) {
+		memset(sctx->buffer + partial, 0x0, SHA1_BLOCK_SIZE - partial);
+		partial = 0;
+
+		__sha1_transform(sctx->state, sctx->buffer);
+	}
+
+	memset(sctx->buffer + partial, 0x0, bit_offset - partial);
+	*bits = cpu_to_be64(sctx->count << 3);
+	__sha1_transform(sctx->state, sctx->buffer);
+
+	for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(__be32); i++)
+		put_unaligned_be32(sctx->state[i], digest++);
+
+	*sctx = (struct sha1_state){};
+}
+
+void sha1(const u8 *data, unsigned int len, u8 *out)
+{
Quoted 8 lines

lib/crypto/sha1.c void sha1_init(__u32 *buf) · line 215

Quoted 4 lines
lib/crypto/sha1.c +4 -0
+	struct sha1_state sctx = {0};
+
+	sha1_init(sctx.state);
+	sctx.count = 0;

Hmm... so shouldn't C99 take care of this given the initialization above? I'm not 100% sure here. I.e. given "= {0}", shouldn't this already be zero?

Quoted 1 line

Yes it seems so. We will look at changing that.

Yeah, AFAIK C99 should zero out anything remaining.

Quoted 11 lines
Quoted 10 lines

lib/crypto/sha1.c void sha1_init(__u32 *buf) · line 221

Quoted 8 lines
lib/crypto/sha1.c +5 -0
+	sha1_update(&sctx, data, len);
+	sha1_final(&sctx, out);
+}
+EXPORT_SYMBOL(sha1);
+
  MODULE_LICENSE("GPL");

BR, Jarkko

Quoted 2 lines

Thanks
Ross

BR, Jarkko

#68 lore raw

PATCH v9 7/19 x86: Add early SHA-256 support for Secure Launch early measurements

In reply to Ross Philipson

From: "Daniel P. Smith" <[email protected]>

The SHA-256 algorithm is necessary to measure configuration information into the TPM as early as possible before using the values. This implementation uses the established approach of #including the SHA-256 libraries directly in the code since the compressed kernel is not uncompressed at this point.

---
 arch/x86/boot/compressed/Makefile       | 2 +-
 arch/x86/boot/compressed/early_sha256.c | 6 ++++++
 2 files changed, 7 insertions(+), 1 deletion(-)
 create mode 100644 arch/x86/boot/compressed/early_sha256.c
arch/x86/boot/compressed/Makefile +1 -1
index 3307ebef4e1b..9189a0e28686 100644
@@ -118,7 +118,7 @@ vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi.o
 vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_mixed.o
 vmlinux-objs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
 
-vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o
+vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o $(obj)/early_sha256.o
 
 $(obj)/vmlinux: $(vmlinux-objs-y) FORCE
 	$(call if_changed,ld)
arch/x86/boot/compressed/early_sha256.c new file +6 -0
new file mode 100644
index 000000000000..293742a90ddc
@@ -0,0 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2024 Apertus Solutions, LLC
+ */
+
+#include "../../../../lib/crypto/sha256.c"
Signature · 7 lines
2.39.3


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#69 lore raw

PATCH v9 8/19 x86: Secure Launch kernel early boot stub

In reply to Ross Philipson

The Secure Launch (SL) stub provides the entry point for Intel TXT (and later AMD SKINIT) to vector to during the late launch. The symbol sl_stub_entry is that entry point and its offset into the kernel is conveyed to the launching code using the MLE (Measured Launch Environment) header in the structure named mle_header. The offset of the MLE header is set in the kernel_info. The routine sl_stub contains the very early late launch setup code responsible for setting up the basic environment to allow the normal kernel startup_32 code to proceed. It is also responsible for properly waking and handling the APs on Intel platforms. The routine sl_main which runs after entering 64b mode is responsible for measuring configuration and module information before it is used like the boot params, the kernel command line, the TXT heap, an external initramfs, etc.

---
 Documentation/arch/x86/boot.rst        |  21 +
 arch/x86/boot/compressed/Makefile      |   3 +-
 arch/x86/boot/compressed/head_64.S     |  30 +
 arch/x86/boot/compressed/kernel_info.S |  34 ++
 arch/x86/boot/compressed/sl_main.c     | 577 ++++++++++++++++++++
 arch/x86/boot/compressed/sl_stub.S     | 725 +++++++++++++++++++++++++
 arch/x86/include/asm/msr-index.h       |   5 +
 arch/x86/include/uapi/asm/bootparam.h  |   1 +
 arch/x86/kernel/asm-offsets.c          |  20 +
 9 files changed, 1415 insertions(+), 1 deletion(-)
 create mode 100644 arch/x86/boot/compressed/sl_main.c
 create mode 100644 arch/x86/boot/compressed/sl_stub.S
Documentation/arch/x86/boot.rst +21 -0
index 4fd492cb4970..295cdf9bcbdb 100644
@@ -482,6 +482,14 @@ Protocol:	2.00+
 	    - If 1, KASLR enabled.
 	    - If 0, KASLR disabled.
 
+  Bit 2 (kernel internal): SLAUNCH_FLAG
+
+	- Used internally by the setup kernel to communicate
+	  Secure Launch status to kernel proper.
+
+	    - If 1, Secure Launch enabled.
+	    - If 0, Secure Launch disabled.
+
   Bit 5 (write): QUIET_FLAG
 
 	- If 0, print early messages.
@@ -1028,6 +1036,19 @@ Offset/size:	0x000c/4
 
   This field contains maximal allowed type for setup_data and setup_indirect structs.
 
+============	=================
+Field name:	mle_header_offset
+Offset/size:	0x0010/4
+============	=================
+
+  This field contains the offset to the Secure Launch Measured Launch Environment
+  (MLE) header. This offset is used to locate information needed during a secure
+  late launch using Intel TXT. If the offset is zero, the kernel does not have
+  Secure Launch capabilities. The MLE entry point is called from TXT on the BSP
+  following a success measured launch. The specific state of the processors is
+  outlined in the TXT Software Development Guide, the latest can be found here:
+  https://www.intel.com/content/dam/www/public/us/en/documents/guides/intel-txt-software-development-guide.pdf
+
 
 The Image Checksum
 ==================
arch/x86/boot/compressed/Makefile +2 -1
index 9189a0e28686..9076a248d4b4 100644
@@ -118,7 +118,8 @@ vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi.o
 vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_mixed.o
 vmlinux-objs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
 
-vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o $(obj)/early_sha256.o
+vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o $(obj)/early_sha256.o \
+	$(obj)/sl_main.o $(obj)/sl_stub.o
 
 $(obj)/vmlinux: $(vmlinux-objs-y) FORCE
 	$(call if_changed,ld)
arch/x86/boot/compressed/head_64.S +30 -0
index 1dcb794c5479..803c9e2e6d85 100644
@@ -420,6 +420,13 @@ SYM_CODE_START(startup_64)
 	pushq	$0
 	popfq
 
+#ifdef CONFIG_SECURE_LAUNCH
+	/* Ensure the relocation region is coverd by a PMR */
+	movq	%rbx, %rdi
+	movl	$(_bss - startup_32), %esi
+	callq	sl_check_region
+#endif
+
 /*
  * Copy the compressed kernel to the end of our buffer
  * where decompression in place becomes safe.
@@ -462,6 +469,29 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated)
 	shrq	$3, %rcx
 	rep	stosq
 
+#ifdef CONFIG_SECURE_LAUNCH
+	/*
+	 * Have to do the final early sl stub work in 64b area.
+	 *
+	 * *********** NOTE ***********
+	 *
+	 * Several boot params get used before we get a chance to measure
+	 * them in this call. This is a known issue and we currently don't
+	 * have a solution. The scratch field doesn't matter. There is no
+	 * obvious way to do anything about the use of kernel_alignment or
+	 * init_size though these seem low risk with all the PMR and overlap
+	 * checks in place.
+	 */
+	movq	%r15, %rdi
+	callq	sl_main
+
+	/* Ensure the decompression location is covered by a PMR */
+	movq	%rbp, %rdi
+	movq	output_len(%rip), %rsi
+	callq	sl_check_region
+#endif
+
+	pushq	%rsi
 	call	load_stage2_idt
 
 	/* Pass boot_params to initialize_identity_maps() */
arch/x86/boot/compressed/kernel_info.S +34 -0
index c18f07181dd5..e199b87764e9 100644
@@ -28,6 +28,40 @@ SYM_DATA_START(kernel_info)
 	/* Maximal allowed type for setup_data and setup_indirect structs. */
 	.long	SETUP_TYPE_MAX
 
+	/* Offset to the MLE header structure */
+#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
+	.long	rva(mle_header)
+#else
+	.long	0
+#endif
+
 kernel_info_var_len_data:
 	/* Empty for time being... */
 SYM_DATA_END_LABEL(kernel_info, SYM_L_LOCAL, kernel_info_end)
+
+#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
+	/*
+	 * The MLE Header per the TXT Specification, section 2.1
+	 * MLE capabilities, see table 4. Capabilities set:
+	 * bit 0: Support for GETSEC[WAKEUP] for RLP wakeup
+	 * bit 1: Support for RLP wakeup using MONITOR address
+	 * bit 2: The ECX register will contain the pointer to the MLE page table
+	 * bit 5: TPM 1.2 family: Details/authorities PCR usage support
+	 * bit 9: Supported format of TPM 2.0 event log - TCG compliant
+	 */
+SYM_DATA_START(mle_header)
+	.long	0x9082ac5a  /* UUID0 */
+	.long	0x74a7476f  /* UUID1 */
+	.long	0xa2555c0f  /* UUID2 */
+	.long	0x42b651cb  /* UUID3 */
+	.long	0x00000034  /* MLE header size */
+	.long	0x00020002  /* MLE version 2.2 */
+	.long	rva(sl_stub_entry) /* Linear entry point of MLE (virt. address) */
+	.long	0x00000000  /* First valid page of MLE */
+	.long	0x00000000  /* Offset within binary of first byte of MLE */
+	.long	rva(_edata) /* Offset within binary of last byte + 1 of MLE */
+	.long	0x00000227  /* Bit vector of MLE-supported capabilities */
+	.long	0x00000000  /* Starting linear address of command line (unused) */
+	.long	0x00000000  /* Ending linear address of command line (unused) */
+SYM_DATA_END(mle_header)
+#endif
arch/x86/boot/compressed/sl_main.c new file +577 -0

577 lines not on this page · raw message

arch/x86/boot/compressed/sl_stub.S new file +725 -0

725 lines not on this page · raw message

arch/x86/include/asm/msr-index.h +5 -0
index e022e6eb766c..37f6167f28ba 100644
@@ -348,6 +348,9 @@
 #define MSR_IA32_RTIT_OUTPUT_BASE	0x00000560
 #define MSR_IA32_RTIT_OUTPUT_MASK	0x00000561
 
+#define MSR_MTRRphysBase0		0x00000200
+#define MSR_MTRRphysMask0		0x00000201
+
 #define MSR_MTRRfix64K_00000		0x00000250
 #define MSR_MTRRfix16K_80000		0x00000258
 #define MSR_MTRRfix16K_A0000		0x00000259
@@ -849,6 +852,8 @@
 #define MSR_IA32_APICBASE_ENABLE	(1<<11)
 #define MSR_IA32_APICBASE_BASE		(0xfffff<<12)
 
+#define MSR_IA32_X2APIC_APICID		0x00000802
+
 #define MSR_IA32_UCODE_WRITE		0x00000079
 #define MSR_IA32_UCODE_REV		0x0000008b
 
arch/x86/include/uapi/asm/bootparam.h +1 -0
index 9b82eebd7add..7ce283a22d6b 100644
@@ -12,6 +12,7 @@
 /* loadflags */
 #define LOADED_HIGH	(1<<0)
 #define KASLR_FLAG	(1<<1)
+#define SLAUNCH_FLAG	(1<<2)
 #define QUIET_FLAG	(1<<5)
 #define KEEP_SEGMENTS	(1<<6)
 #define CAN_USE_HEAP	(1<<7)
arch/x86/kernel/asm-offsets.c +20 -0
index a98020bf31bb..925adce6e2c7 100644
@@ -13,6 +13,8 @@
 #include <linux/hardirq.h>
 #include <linux/suspend.h>
 #include <linux/kbuild.h>
+#include <linux/slr_table.h>
+#include <linux/slaunch.h>
 #include <asm/processor.h>
 #include <asm/thread_info.h>
 #include <asm/sigframe.h>
@@ -120,4 +122,22 @@ static void __used common(void)
 	OFFSET(ARIA_CTX_rounds, aria_ctx, rounds);
 #endif
 
+#ifdef CONFIG_SECURE_LAUNCH
+	BLANK();
+	OFFSET(SL_txt_info, txt_os_mle_data, txt_info);
+	OFFSET(SL_mle_scratch, txt_os_mle_data, mle_scratch);
+	OFFSET(SL_boot_params_addr, txt_os_mle_data, boot_params_addr);
+	OFFSET(SL_ap_wake_block, txt_os_mle_data, ap_wake_block);
+	OFFSET(SL_ap_wake_block_size, txt_os_mle_data, ap_wake_block_size);
+	OFFSET(SL_saved_misc_enable_msr, slr_entry_intel_info, saved_misc_enable_msr);
+	OFFSET(SL_saved_bsp_mtrrs, slr_entry_intel_info, saved_bsp_mtrrs);
+	OFFSET(SL_num_logical_procs, txt_bios_data, num_logical_procs);
+	OFFSET(SL_capabilities, txt_os_sinit_data, capabilities);
+	OFFSET(SL_mle_size, txt_os_sinit_data, mle_size);
+	OFFSET(SL_vtd_pmr_lo_base, txt_os_sinit_data, vtd_pmr_lo_base);
+	OFFSET(SL_vtd_pmr_lo_size, txt_os_sinit_data, vtd_pmr_lo_size);
+	OFFSET(SL_rlp_wakeup_addr, txt_sinit_mle_data, rlp_wakeup_addr);
+	OFFSET(SL_rlp_gdt_base, smx_rlp_mle_join, rlp_gdt_base);
+	OFFSET(SL_rlp_entry_point, smx_rlp_mle_join, rlp_entry_point);
+#endif
 }
Signature · 1 line
2.39.3
maintainer
#70 lore raw

Re: [PATCH v9 08/19] x86: Secure Launch kernel early boot stub

Hello Ross,

Quoted 57 lines · Ross Philipson

The Secure Launch (SL) stub provides the entry point for Intel TXT (and later AMD SKINIT) to vector to during the late launch. The symbol sl_stub_entry is that entry point and its offset into the kernel is conveyed to the launching code using the MLE (Measured Launch Environment) header in the structure named mle_header. The offset of the MLE header is set in the kernel_info. The routine sl_stub contains the very early late launch setup code responsible for setting up the basic environment to allow the normal kernel startup_32 code to proceed. It is also responsible for properly waking and handling the APs on Intel platforms. The routine sl_main which runs after entering 64b mode is responsible for measuring configuration and module information before it is used like the boot params, the kernel command line, the TXT heap, an external initramfs, etc.

---
 Documentation/arch/x86/boot.rst        |  21 +
 arch/x86/boot/compressed/Makefile      |   3 +-
 arch/x86/boot/compressed/head_64.S     |  30 +
 arch/x86/boot/compressed/kernel_info.S |  34 ++
 arch/x86/boot/compressed/sl_main.c     | 577 ++++++++++++++++++++
 arch/x86/boot/compressed/sl_stub.S     | 725 +++++++++++++++++++++++++
 arch/x86/include/asm/msr-index.h       |   5 +
 arch/x86/include/uapi/asm/bootparam.h  |   1 +
 arch/x86/kernel/asm-offsets.c          |  20 +
 9 files changed, 1415 insertions(+), 1 deletion(-)
 create mode 100644 arch/x86/boot/compressed/sl_main.c
 create mode 100644 arch/x86/boot/compressed/sl_stub.S
Documentation/arch/x86/boot.rst +12 -0
index 4fd492cb4970..295cdf9bcbdb 100644
@@ -482,6 +482,14 @@ Protocol:  2.00+
            - If 1, KASLR enabled.
            - If 0, KASLR disabled.
 
+  Bit 2 (kernel internal): SLAUNCH_FLAG
+
+       - Used internally by the setup kernel to communicate
+         Secure Launch status to kernel proper.
+
+           - If 1, Secure Launch enabled.
+           - If 0, Secure Launch disabled.
+
   Bit 5 (write): QUIET_FLAG
 
        - If 0, print early messages.
@@ -1028,6 +1036,19 @@ Offset/size:     0x000c/4
 
   This field contains maximal allowed type for setup_data and setup_indirect structs.
 
+============   =================
+Field name:    mle_header_offset
+Offset/size:   0x0010/4
+============   =================

Documentation/arch/x86/boot.rst Offset/size: 0x000c/4 · line 1052

Quoted 10 lines
Documentation/arch/x86/boot.rst +9 -0
+
+  This field contains the offset to the Secure Launch Measured Launch Environment
+  (MLE) header. This offset is used to locate information needed during a secure
+  late launch using Intel TXT. If the offset is zero, the kernel does not have
+  Secure Launch capabilities. The MLE entry point is called from TXT on the BSP
+  following a success measured launch. The specific state of the processors is
+  outlined in the TXT Software Development Guide, the latest can be found here:
+  https://www.intel.com/content/dam/www/public/us/en/documents/guides/intel-txt-software-development-guide.pdf
+
 

Could we just repaint this field as the offset relative to the start of kernel_info rather than relative to the start of the image? That way, there is no need for patch #1, and given that the consumer of this field accesses it via kernel_info, I wouldn't expect any issues in applying this offset to obtain the actual address.

Quoted 21 lines
Documentation/arch/x86/boot.rst +0 -0
 The Image Checksum
 ==================
arch/x86/boot/compressed/Makefile +2 -1
index 9189a0e28686..9076a248d4b4 100644
@@ -118,7 +118,8 @@ vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi.o
 vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_mixed.o
 vmlinux-objs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
 
-vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o $(obj)/early_sha256.o
+vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o $(obj)/early_sha256.o \
+       $(obj)/sl_main.o $(obj)/sl_stub.o
 
 $(obj)/vmlinux: $(vmlinux-objs-y) FORCE
        $(call if_changed,ld)
arch/x86/boot/compressed/head_64.S +0 -0
index 1dcb794c5479..803c9e2e6d85 100644

arch/x86/boot/compressed/head_64.S SYM_CODE_START(startup_64) · line 424

Quoted 5 lines
arch/x86/boot/compressed/head_64.S +2 -0
@@ -420,6 +420,13 @@ SYM_CODE_START(startup_64)
        pushq   $0
        popfq
 
+#ifdef CONFIG_SECURE_LAUNCH
+       /* Ensure the relocation region is coverd by a PMR */

covered

Quoted 25 lines
arch/x86/boot/compressed/head_64.S +18 -0
+       movq    %rbx, %rdi
+       movl    $(_bss - startup_32), %esi
+       callq   sl_check_region
+#endif
+
 /*
  * Copy the compressed kernel to the end of our buffer
  * where decompression in place becomes safe.
@@ -462,6 +469,29 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated)
        shrq    $3, %rcx
        rep     stosq
 
+#ifdef CONFIG_SECURE_LAUNCH
+       /*
+        * Have to do the final early sl stub work in 64b area.
+        *
+        * *********** NOTE ***********
+        *
+        * Several boot params get used before we get a chance to measure
+        * them in this call. This is a known issue and we currently don't
+        * have a solution. The scratch field doesn't matter. There is no
+        * obvious way to do anything about the use of kernel_alignment or
+        * init_size though these seem low risk with all the PMR and overlap
+        * checks in place.
+        */

arch/x86/boot/compressed/head_64.S SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated) · line 494

Quoted 10 lines
arch/x86/boot/compressed/head_64.S +10 -0
+       movq    %r15, %rdi
+       callq   sl_main
+
+       /* Ensure the decompression location is covered by a PMR */
+       movq    %rbp, %rdi
+       movq    output_len(%rip), %rsi
+       callq   sl_check_region
+#endif
+
+       pushq   %rsi

This looks like a rebase error.

Quoted 8 lines
arch/x86/boot/compressed/head_64.S +0 -0
        call    load_stage2_idt
 
        /* Pass boot_params to initialize_identity_maps() */
arch/x86/boot/compressed/kernel_info.S +0 -0
index c18f07181dd5..e199b87764e9 100644

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 33

Quoted 6 lines
arch/x86/boot/compressed/kernel_info.S +3 -0
@@ -28,6 +28,40 @@ SYM_DATA_START(kernel_info)
        /* Maximal allowed type for setup_data and setup_indirect structs. */
        .long   SETUP_TYPE_MAX
 
+       /* Offset to the MLE header structure */
+#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
+       .long   rva(mle_header)

... so this could just be mle_header - kernel_info, and the consumer can do the math instead.

Quoted 16 lines
arch/x86/boot/compressed/kernel_info.S +13 -0
+#else
+       .long   0
+#endif
+
 kernel_info_var_len_data:
        /* Empty for time being... */
 SYM_DATA_END_LABEL(kernel_info, SYM_L_LOCAL, kernel_info_end)
+
+#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
+       /*
+        * The MLE Header per the TXT Specification, section 2.1
+        * MLE capabilities, see table 4. Capabilities set:
+        * bit 0: Support for GETSEC[WAKEUP] for RLP wakeup
+        * bit 1: Support for RLP wakeup using MONITOR address
+        * bit 2: The ECX register will contain the pointer to the MLE page table
+        * bit 5: TPM 1.2 family: Details/authorities PCR usage support

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 59

Quoted 10 lines
arch/x86/boot/compressed/kernel_info.S +10 -0
+        * bit 9: Supported format of TPM 2.0 event log - TCG compliant
+        */
+SYM_DATA_START(mle_header)
+       .long   0x9082ac5a  /* UUID0 */
+       .long   0x74a7476f  /* UUID1 */
+       .long   0xa2555c0f  /* UUID2 */
+       .long   0x42b651cb  /* UUID3 */
+       .long   0x00000034  /* MLE header size */
+       .long   0x00020002  /* MLE version 2.2 */
+       .long   rva(sl_stub_entry) /* Linear entry point of MLE (virt. address) */

and these should perhaps be relative to mle_header?

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 62

Quoted 3 lines
arch/x86/boot/compressed/kernel_info.S +3 -0
+       .long   0x00000000  /* First valid page of MLE */
+       .long   0x00000000  /* Offset within binary of first byte of MLE */
+       .long   rva(_edata) /* Offset within binary of last byte + 1 of MLE */

and here

Quoted 126 lines
arch/x86/boot/compressed/kernel_info.S +5 -0
+       .long   0x00000227  /* Bit vector of MLE-supported capabilities */
+       .long   0x00000000  /* Starting linear address of command line (unused) */
+       .long   0x00000000  /* Ending linear address of command line (unused) */
+SYM_DATA_END(mle_header)
+#endif
arch/x86/boot/compressed/sl_main.c new file +115 -0
new file mode 100644
index 000000000000..61e9baf410fd
@@ -0,0 +1,577 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Secure Launch early measurement and validation routines.
+ *
+ * Copyright (c) 2024, Oracle and/or its affiliates.
+ */
+
+#include <linux/init.h>
+#include <linux/string.h>
+#include <linux/linkage.h>
+#include <asm/segment.h>
+#include <asm/boot.h>
+#include <asm/msr.h>
+#include <asm/mtrr.h>
+#include <asm/processor-flags.h>
+#include <asm/asm-offsets.h>
+#include <asm/bootparam.h>
+#include <asm/bootparam_utils.h>
+#include <linux/slr_table.h>
+#include <linux/slaunch.h>
+#include <crypto/sha1.h>
+#include <crypto/sha2.h>
+
+#define CAPS_VARIABLE_MTRR_COUNT_MASK  0xff
+
+#define SL_TPM12_LOG           1
+#define SL_TPM20_LOG           2
+
+#define SL_TPM20_MAX_ALGS      2
+
+#define SL_MAX_EVENT_DATA      64
+#define SL_TPM12_LOG_SIZE      (sizeof(struct tcg_pcr_event) + \
+                               SL_MAX_EVENT_DATA)
+#define SL_TPM20_LOG_SIZE      (sizeof(struct tcg_pcr_event2_head) + \
+                               SHA1_DIGEST_SIZE + SHA256_DIGEST_SIZE + \
+                               sizeof(struct tcg_event_field) + \
+                               SL_MAX_EVENT_DATA)
+
+static void *evtlog_base;
+static u32 evtlog_size;
+static struct txt_heap_event_log_pointer2_1_element *log20_elem;
+static u32 tpm_log_ver = SL_TPM12_LOG;
+static struct tcg_efi_specid_event_algs tpm_algs[SL_TPM20_MAX_ALGS] = {0};
+
+extern u32 sl_cpu_type;
+extern u32 sl_mle_start;
+
+static u64 sl_txt_read(u32 reg)
+{
+       return readq((void *)(u64)(TXT_PRIV_CONFIG_REGS_BASE + reg));
+}
+
+static void sl_txt_write(u32 reg, u64 val)
+{
+       writeq(val, (void *)(u64)(TXT_PRIV_CONFIG_REGS_BASE + reg));
+}
+
+static void __noreturn sl_txt_reset(u64 error)
+{
+       /* Reading the E2STS register acts as a barrier for TXT registers */
+       sl_txt_write(TXT_CR_ERRORCODE, error);
+       sl_txt_read(TXT_CR_E2STS);
+       sl_txt_write(TXT_CR_CMD_UNLOCK_MEM_CONFIG, 1);
+       sl_txt_read(TXT_CR_E2STS);
+       sl_txt_write(TXT_CR_CMD_RESET, 1);
+
+       for ( ; ; )
+               asm volatile ("hlt");
+
+       unreachable();
+}
+
+static u64 sl_rdmsr(u32 reg)
+{
+       u64 lo, hi;
+
+       asm volatile ("rdmsr" : "=a" (lo), "=d" (hi) : "c" (reg));
+
+       return (hi << 32) | lo;
+}
+
+static struct slr_table *sl_locate_and_validate_slrt(void)
+{
+       struct txt_os_mle_data *os_mle_data;
+       struct slr_table *slrt;
+       void *txt_heap;
+
+       txt_heap = (void *)sl_txt_read(TXT_CR_HEAP_BASE);
+       os_mle_data = txt_os_mle_data_start(txt_heap);
+
+       if (!os_mle_data->slrt)
+               sl_txt_reset(SL_ERROR_INVALID_SLRT);
+
+       slrt = (struct slr_table *)os_mle_data->slrt;
+
+       if (slrt->magic != SLR_TABLE_MAGIC)
+               sl_txt_reset(SL_ERROR_INVALID_SLRT);
+
+       if (slrt->architecture != SLR_INTEL_TXT)
+               sl_txt_reset(SL_ERROR_INVALID_SLRT);
+
+       return slrt;
+}
+
+static void sl_check_pmr_coverage(void *base, u32 size, bool allow_hi)
+{
+       struct txt_os_sinit_data *os_sinit_data;
+       void *end = base + size;
+       void *txt_heap;
+
+       if (!(sl_cpu_type & SL_CPU_INTEL))
+               return;
+
+       txt_heap = (void *)sl_txt_read(TXT_CR_HEAP_BASE);
+       os_sinit_data = txt_os_sinit_data_start(txt_heap);

arch/x86/boot/compressed/sl_main.c line 125

Quoted 10 lines
arch/x86/boot/compressed/sl_main.c new file +10 -0
+
+       if ((end >= (void *)0x100000000ULL) && (base < (void *)0x100000000ULL))
+               sl_txt_reset(SL_ERROR_REGION_STRADDLE_4GB);
+
+       /*
+        * Note that the late stub code validates that the hi PMR covers
+        * all memory above 4G. At this point the code can only check that
+        * regions are within the hi PMR but that is sufficient.
+        */
+       if ((end > (void *)0x100000000ULL) && (base >= (void *)0x100000000ULL)) {

Better to put the cast on the pointers, given that we are doing arithmetic, and use SZ_4G instead of open coding this constant 4 times.

Quoted 521 lines
arch/x86/boot/compressed/sl_main.c new file +452 -0
+               if (allow_hi) {
+                       if (end >= (void *)(os_sinit_data->vtd_pmr_hi_base +
+                                          os_sinit_data->vtd_pmr_hi_size))
+                               sl_txt_reset(SL_ERROR_BUFFER_BEYOND_PMR);
+               } else {
+                       sl_txt_reset(SL_ERROR_REGION_ABOVE_4GB);
+               }
+       }
+
+       if (end >= (void *)os_sinit_data->vtd_pmr_lo_size)
+               sl_txt_reset(SL_ERROR_BUFFER_BEYOND_PMR);
+}
+
+/*
+ * Some MSRs are modified by the pre-launch code including the MTRRs.
+ * The early MLE code has to restore these values. This code validates
+ * the values after they are measured.
+ */
+static void sl_txt_validate_msrs(struct txt_os_mle_data *os_mle_data)
+{
+       struct slr_txt_mtrr_state *saved_bsp_mtrrs;
+       u64 mtrr_caps, mtrr_def_type, mtrr_var;
+       struct slr_entry_intel_info *txt_info;
+       u64 misc_en_msr;
+       u32 vcnt, i;
+
+       txt_info = (struct slr_entry_intel_info *)os_mle_data->txt_info;
+       saved_bsp_mtrrs = &txt_info->saved_bsp_mtrrs;
+
+       mtrr_caps = sl_rdmsr(MSR_MTRRcap);
+       vcnt = (u32)(mtrr_caps & CAPS_VARIABLE_MTRR_COUNT_MASK);
+
+       if (saved_bsp_mtrrs->mtrr_vcnt > vcnt)
+               sl_txt_reset(SL_ERROR_MTRR_INV_VCNT);
+       if (saved_bsp_mtrrs->mtrr_vcnt > TXT_OS_MLE_MAX_VARIABLE_MTRRS)
+               sl_txt_reset(SL_ERROR_MTRR_INV_VCNT);
+
+       mtrr_def_type = sl_rdmsr(MSR_MTRRdefType);
+       if (saved_bsp_mtrrs->default_mem_type != mtrr_def_type)
+               sl_txt_reset(SL_ERROR_MTRR_INV_DEF_TYPE);
+
+       for (i = 0; i < saved_bsp_mtrrs->mtrr_vcnt; i++) {
+               mtrr_var = sl_rdmsr(MTRRphysBase_MSR(i));
+               if (saved_bsp_mtrrs->mtrr_pair[i].mtrr_physbase != mtrr_var)
+                       sl_txt_reset(SL_ERROR_MTRR_INV_BASE);
+               mtrr_var = sl_rdmsr(MTRRphysMask_MSR(i));
+               if (saved_bsp_mtrrs->mtrr_pair[i].mtrr_physmask != mtrr_var)
+                       sl_txt_reset(SL_ERROR_MTRR_INV_MASK);
+       }
+
+       misc_en_msr = sl_rdmsr(MSR_IA32_MISC_ENABLE);
+       if (txt_info->saved_misc_enable_msr != misc_en_msr)
+               sl_txt_reset(SL_ERROR_MSR_INV_MISC_EN);
+}
+
+static void sl_find_drtm_event_log(struct slr_table *slrt)
+{
+       struct txt_os_sinit_data *os_sinit_data;
+       struct slr_entry_log_info *log_info;
+       void *txt_heap;
+
+       log_info = slr_next_entry_by_tag(slrt, NULL, SLR_ENTRY_LOG_INFO);
+       if (!log_info)
+               sl_txt_reset(SL_ERROR_SLRT_MISSING_ENTRY);
+
+       evtlog_base = (void *)log_info->addr;
+       evtlog_size = log_info->size;
+
+       txt_heap = (void *)sl_txt_read(TXT_CR_HEAP_BASE);
+
+       /*
+        * For TPM 2.0, the event log 2.1 extended data structure has to also
+        * be located and fixed up.
+        */
+       os_sinit_data = txt_os_sinit_data_start(txt_heap);
+
+       /*
+        * Only support version 6 and later that properly handle the
+        * list of ExtDataElements in the OS-SINIT structure.
+        */
+       if (os_sinit_data->version < 6)
+               sl_txt_reset(SL_ERROR_OS_SINIT_BAD_VERSION);
+
+       /* Find the TPM2.0 logging extended heap element */
+       log20_elem = tpm20_find_log2_1_element(os_sinit_data);
+
+       /* If found, this implies TPM20 log and family */
+       if (log20_elem)
+               tpm_log_ver = SL_TPM20_LOG;
+}
+
+static void sl_validate_event_log_buffer(void)
+{
+       struct txt_os_sinit_data *os_sinit_data;
+       void *txt_heap, *txt_end;
+       void *mle_base, *mle_end;
+       void *evtlog_end;
+
+       if ((u64)evtlog_size > (LLONG_MAX - (u64)evtlog_base))
+               sl_txt_reset(SL_ERROR_INTEGER_OVERFLOW);
+       evtlog_end = evtlog_base + evtlog_size;
+
+       txt_heap = (void *)sl_txt_read(TXT_CR_HEAP_BASE);
+       txt_end = txt_heap + sl_txt_read(TXT_CR_HEAP_SIZE);
+       os_sinit_data = txt_os_sinit_data_start(txt_heap);
+
+       mle_base = (void *)(u64)sl_mle_start;
+       mle_end = mle_base + os_sinit_data->mle_size;
+
+       /*
+        * This check is to ensure the event log buffer does not overlap with
+        * the MLE image.
+        */
+       if (evtlog_base >= mle_end && evtlog_end > mle_end)
+               goto pmr_check; /* above */
+
+       if (evtlog_end <= mle_base && evtlog_base < mle_base)
+               goto pmr_check; /* below */
+
+       sl_txt_reset(SL_ERROR_MLE_BUFFER_OVERLAP);
+
+pmr_check:
+       /*
+        * The TXT heap is protected by the DPR. If the TPM event log is
+        * inside the TXT heap, there is no need for a PMR check.
+        */
+       if (evtlog_base > txt_heap && evtlog_end < txt_end)
+               return;
+
+       sl_check_pmr_coverage(evtlog_base, evtlog_size, true);
+}
+
+static void sl_find_event_log_algorithms(void)
+{
+       struct tcg_efi_specid_event_head *efi_head =
+               (struct tcg_efi_specid_event_head *)(evtlog_base +
+                                       log20_elem->first_record_offset +
+                                       sizeof(struct tcg_pcr_event));
+
+       if (efi_head->num_algs == 0 || efi_head->num_algs > 2)
+               sl_txt_reset(SL_ERROR_TPM_NUMBER_ALGS);
+
+       memcpy(&tpm_algs[0], &efi_head->digest_sizes[0],
+              sizeof(struct tcg_efi_specid_event_algs) * efi_head->num_algs);
+}
+
+static void sl_tpm12_log_event(u32 pcr, u32 event_type,
+                              const u8 *data, u32 length,
+                              const u8 *event_data, u32 event_size)
+{
+       u8 sha1_hash[SHA1_DIGEST_SIZE] = {0};
+       u8 log_buf[SL_TPM12_LOG_SIZE] = {0};
+       struct tcg_pcr_event *pcr_event;
+       u32 total_size;
+
+       pcr_event = (struct tcg_pcr_event *)log_buf;
+       pcr_event->pcr_idx = pcr;
+       pcr_event->event_type = event_type;
+       if (length > 0) {
+               sha1(data, length, &sha1_hash[0]);
+               memcpy(&pcr_event->digest[0], &sha1_hash[0], SHA1_DIGEST_SIZE);
+       }
+       pcr_event->event_size = event_size;
+       if (event_size > 0)
+               memcpy((u8 *)pcr_event + sizeof(struct tcg_pcr_event),
+                      event_data, event_size);
+
+       total_size = sizeof(struct tcg_pcr_event) + event_size;
+
+       if (tpm12_log_event(evtlog_base, evtlog_size, total_size, pcr_event))
+               sl_txt_reset(SL_ERROR_TPM_LOGGING_FAILED);
+}
+
+static void sl_tpm20_log_event(u32 pcr, u32 event_type,
+                              const u8 *data, u32 length,
+                              const u8 *event_data, u32 event_size)
+{
+       u8 sha256_hash[SHA256_DIGEST_SIZE] = {0};
+       u8 sha1_hash[SHA1_DIGEST_SIZE] = {0};
+       u8 log_buf[SL_TPM20_LOG_SIZE] = {0};
+       struct sha256_state sctx256 = {0};
+       struct tcg_pcr_event2_head *head;
+       struct tcg_event_field *event;
+       u32 total_size;
+       u16 *alg_ptr;
+       u8 *dgst_ptr;
+
+       head = (struct tcg_pcr_event2_head *)log_buf;
+       head->pcr_idx = pcr;
+       head->event_type = event_type;
+       total_size = sizeof(struct tcg_pcr_event2_head);
+       alg_ptr = (u16 *)(log_buf + sizeof(struct tcg_pcr_event2_head));
+
+       for ( ; head->count < 2; head->count++) {
+               if (!tpm_algs[head->count].alg_id)
+                       break;
+
+               *alg_ptr = tpm_algs[head->count].alg_id;
+               dgst_ptr = (u8 *)alg_ptr + sizeof(u16);
+
+               if (tpm_algs[head->count].alg_id == TPM_ALG_SHA256 &&
+                   length) {
+                       sha256_init(&sctx256);
+                       sha256_update(&sctx256, data, length);
+                       sha256_final(&sctx256, &sha256_hash[0]);
+               } else if (tpm_algs[head->count].alg_id == TPM_ALG_SHA1 &&
+                          length) {
+                       sha1(data, length, &sha1_hash[0]);
+               }
+
+               if (tpm_algs[head->count].alg_id == TPM_ALG_SHA256) {
+                       memcpy(dgst_ptr, &sha256_hash[0], SHA256_DIGEST_SIZE);
+                       total_size += SHA256_DIGEST_SIZE + sizeof(u16);
+                       alg_ptr = (u16 *)((u8 *)alg_ptr + SHA256_DIGEST_SIZE + sizeof(u16));
+               } else if (tpm_algs[head->count].alg_id == TPM_ALG_SHA1) {
+                       memcpy(dgst_ptr, &sha1_hash[0], SHA1_DIGEST_SIZE);
+                       total_size += SHA1_DIGEST_SIZE + sizeof(u16);
+                       alg_ptr = (u16 *)((u8 *)alg_ptr + SHA1_DIGEST_SIZE + sizeof(u16));
+               } else {
+                       sl_txt_reset(SL_ERROR_TPM_UNKNOWN_DIGEST);
+               }
+       }
+
+       event = (struct tcg_event_field *)(log_buf + total_size);
+       event->event_size = event_size;
+       if (event_size > 0)
+               memcpy((u8 *)event + sizeof(struct tcg_event_field), event_data, event_size);
+       total_size += sizeof(struct tcg_event_field) + event_size;
+
+       if (tpm20_log_event(log20_elem, evtlog_base, evtlog_size, total_size, &log_buf[0]))
+               sl_txt_reset(SL_ERROR_TPM_LOGGING_FAILED);
+}
+
+static void sl_tpm_extend_evtlog(u32 pcr, u32 type,
+                                const u8 *data, u32 length, const char *desc)
+{
+       if (tpm_log_ver == SL_TPM20_LOG)
+               sl_tpm20_log_event(pcr, type, data, length,
+                                  (const u8 *)desc, strlen(desc));
+       else
+               sl_tpm12_log_event(pcr, type, data, length,
+                                  (const u8 *)desc, strlen(desc));
+}
+
+static struct setup_data *sl_handle_setup_data(struct setup_data *curr,
+                                              struct slr_policy_entry *entry)
+{
+       struct setup_indirect *ind;
+       struct setup_data *next;
+
+       if (!curr)
+               return NULL;
+
+       next = (struct setup_data *)(unsigned long)curr->next;
+
+       /* SETUP_INDIRECT instances have to be handled differently */
+       if (curr->type == SETUP_INDIRECT) {
+               ind = (struct setup_indirect *)((u8 *)curr + offsetof(struct setup_data, data));
+
+               sl_check_pmr_coverage((void *)ind->addr, ind->len, true);
+
+               sl_tpm_extend_evtlog(entry->pcr, TXT_EVTYPE_SLAUNCH,
+                                    (void *)ind->addr, ind->len,
+                                    entry->evt_info);
+
+               return next;
+       }
+
+       sl_check_pmr_coverage(((u8 *)curr) + sizeof(struct setup_data),
+                             curr->len, true);
+
+       sl_tpm_extend_evtlog(entry->pcr, TXT_EVTYPE_SLAUNCH,
+                            ((u8 *)curr) + sizeof(struct setup_data),
+                            curr->len,
+                            entry->evt_info);
+
+       return next;
+}
+
+static void sl_extend_setup_data(struct slr_policy_entry *entry)
+{
+       struct setup_data *data;
+
+       /*
+        * Measuring the boot params measured the fixed e820 memory map.
+        * Measure any setup_data entries including e820 extended entries.
+        */
+       data = (struct setup_data *)(unsigned long)entry->entity;
+       while (data)
+               data = sl_handle_setup_data(data, entry);
+}
+
+static void sl_extend_slrt(struct slr_policy_entry *entry)
+{
+       struct slr_table *slrt = (struct slr_table *)entry->entity;
+       struct slr_entry_intel_info *intel_info;
+
+       /*
+        * In revision one of the SLRT, the only table that needs to be
+        * measured is the Intel info table. Everything else is meta-data,
+        * addresses and sizes. Note the size of what to measure is not set.
+        * The flag SLR_POLICY_IMPLICIT_SIZE leaves it to the measuring code
+        * to sort out.
+        */
+       if (slrt->revision == 1) {
+               intel_info = slr_next_entry_by_tag(slrt, NULL, SLR_ENTRY_INTEL_INFO);
+               if (!intel_info)
+                       sl_txt_reset(SL_ERROR_SLRT_MISSING_ENTRY);
+
+               sl_tpm_extend_evtlog(entry->pcr, TXT_EVTYPE_SLAUNCH,
+                                    (void *)entry->entity, sizeof(struct slr_entry_intel_info),
+                                    entry->evt_info);
+       }
+}
+
+static void sl_extend_txt_os2mle(struct slr_policy_entry *entry)
+{
+       struct txt_os_mle_data *os_mle_data;
+       void *txt_heap;
+
+       txt_heap = (void *)sl_txt_read(TXT_CR_HEAP_BASE);
+       os_mle_data = txt_os_mle_data_start(txt_heap);
+
+       /*
+        * Version 1 of the OS-MLE heap structure has no fields to measure. It just
+        * has addresses and sizes and a scratch buffer.
+        */
+       if (os_mle_data->version == 1)
+               return;
+}
+
+static void sl_process_extend_policy(struct slr_table *slrt)
+{
+       struct slr_entry_policy *policy;
+       u16 i;
+
+       policy = slr_next_entry_by_tag(slrt, NULL, SLR_ENTRY_ENTRY_POLICY);
+       if (!policy)
+               sl_txt_reset(SL_ERROR_SLRT_MISSING_ENTRY);
+
+       for (i = 0; i < policy->nr_entries; i++) {
+               switch (policy->policy_entries[i].entity_type) {
+               case SLR_ET_SETUP_DATA:
+                       sl_extend_setup_data(&policy->policy_entries[i]);
+                       break;
+               case SLR_ET_SLRT:
+                       sl_extend_slrt(&policy->policy_entries[i]);
+                       break;
+               case SLR_ET_TXT_OS2MLE:
+                       sl_extend_txt_os2mle(&policy->policy_entries[i]);
+                       break;
+               case SLR_ET_UNUSED:
+                       continue;
+               default:
+                       sl_tpm_extend_evtlog(policy->policy_entries[i].pcr, TXT_EVTYPE_SLAUNCH,
+                                            (void *)policy->policy_entries[i].entity,
+                                            policy->policy_entries[i].size,
+                                            policy->policy_entries[i].evt_info);
+               }
+       }
+}
+
+static void sl_process_extend_uefi_config(struct slr_table *slrt)
+{
+       struct slr_entry_uefi_config *uefi_config;
+       u16 i;
+
+       uefi_config = slr_next_entry_by_tag(slrt, NULL, SLR_ENTRY_UEFI_CONFIG);
+
+       /* Optionally here depending on how SL kernel was booted */
+       if (!uefi_config)
+               return;
+
+       for (i = 0; i < uefi_config->nr_entries; i++) {
+               sl_tpm_extend_evtlog(uefi_config->uefi_cfg_entries[i].pcr, TXT_EVTYPE_SLAUNCH,
+                                    (void *)uefi_config->uefi_cfg_entries[i].cfg,
+                                    uefi_config->uefi_cfg_entries[i].size,
+                                    uefi_config->uefi_cfg_entries[i].evt_info);
+       }
+}
+
+asmlinkage __visible void sl_check_region(void *base, u32 size)
+{
+       sl_check_pmr_coverage(base, size, false);
+}
+
+asmlinkage __visible void sl_main(void *bootparams)
+{
+       struct boot_params *bp  = (struct boot_params *)bootparams;
+       struct txt_os_mle_data *os_mle_data;
+       struct slr_table *slrt;
+       void *txt_heap;
+
+       /*
+        * Ensure loadflags do not indicate a secure launch was done
+        * unless it really was.
+        */
+       bp->hdr.loadflags &= ~SLAUNCH_FLAG;
+
+       /*
+        * Currently only Intel TXT is supported for Secure Launch. Testing
+        * this value also indicates that the kernel was booted successfully
+        * through the Secure Launch entry point and is in SMX mode.
+        */
+       if (!(sl_cpu_type & SL_CPU_INTEL))
+               return;
+
+       slrt = sl_locate_and_validate_slrt();
+
+       /* Locate the TPM event log. */
+       sl_find_drtm_event_log(slrt);
+
+       /* Validate the location of the event log buffer before using it */
+       sl_validate_event_log_buffer();
+
+       /*
+        * Find the TPM hash algorithms used by the ACM and recorded in the
+        * event log.
+        */
+       if (tpm_log_ver == SL_TPM20_LOG)
+               sl_find_event_log_algorithms();
+
+       /*
+        * Sanitize them before measuring. Set the SLAUNCH_FLAG early since if
+        * anything fails, the system will reset anyway.
+        */
+       sanitize_boot_params(bp);
+       bp->hdr.loadflags |= SLAUNCH_FLAG;
+
+       sl_check_pmr_coverage(bootparams, PAGE_SIZE, false);
+
+       /* Place event log SL specific tags before and after measurements */
+       sl_tpm_extend_evtlog(17, TXT_EVTYPE_SLAUNCH_START, NULL, 0, "");
+
+       /* Process all policy entries and extend the measurements to the evtlog */
+       sl_process_extend_policy(slrt);
+
+       /* Process all EFI config entries and extend the measurements to the evtlog */
+       sl_process_extend_uefi_config(slrt);
+
+       sl_tpm_extend_evtlog(17, TXT_EVTYPE_SLAUNCH_END, NULL, 0, "");
+
+       /* No PMR check is needed, the TXT heap is covered by the DPR */
+       txt_heap = (void *)sl_txt_read(TXT_CR_HEAP_BASE);
+       os_mle_data = txt_os_mle_data_start(txt_heap);
+
+       /*
+        * Now that the OS-MLE data is measured, ensure the MTRR and
+        * misc enable MSRs are what we expect.
+        */
+       sl_txt_validate_msrs(os_mle_data);
+}
arch/x86/boot/compressed/sl_stub.S new file +63 -0
new file mode 100644
index 000000000000..24b8f23d5dcc
@@ -0,0 +1,725 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Secure Launch protected mode entry point.
+ *
+ * Copyright (c) 2024, Oracle and/or its affiliates.
+ */
+       .code32
+       .text
+#include <linux/linkage.h>
+#include <asm/segment.h>
+#include <asm/msr.h>
+#include <asm/apicdef.h>
+#include <asm/trapnr.h>
+#include <asm/processor-flags.h>
+#include <asm/asm-offsets.h>
+#include <asm/bootparam.h>
+#include <asm/page_types.h>
+#include <asm/irq_vectors.h>
+#include <linux/slr_table.h>
+#include <linux/slaunch.h>
+
+/* CPUID: leaf 1, ECX, SMX feature bit */
+#define X86_FEATURE_BIT_SMX    (1 << 6)
+
+#define IDT_VECTOR_LO_BITS     0
+#define IDT_VECTOR_HI_BITS     6
+
+/*
+ * See the comment in head_64.S for detailed information on what this macro
+ * and others like it are used for. The comment appears right at the top of
+ * the file.
+ */
+#define rva(X) ((X) - sl_stub_entry)
+
+/*
+ * The GETSEC op code is open coded because older versions of
+ * GCC do not support the getsec mnemonic.
+ */
+.macro GETSEC leaf
+       pushl   %ebx
+       xorl    %ebx, %ebx      /* Must be zero for SMCTRL */
+       movl    \leaf, %eax     /* Leaf function */
+       .byte   0x0f, 0x37      /* GETSEC opcode */
+       popl    %ebx
+.endm
+
+.macro TXT_RESET error
+       /*
+        * Set a sticky error value and reset. Note the movs to %eax act as
+        * TXT register barriers.
+        */
+       movl    \error, (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_ERRORCODE)
+       movl    (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_E2STS), %eax
+       movl    $1, (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_CMD_NO_SECRETS)
+       movl    (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_E2STS), %eax
+       movl    $1, (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_CMD_UNLOCK_MEM_CONFIG)
+       movl    (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_E2STS), %eax
+       movl    $1, (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_CMD_RESET)
+1:
+       hlt
+       jmp     1b
+.endm

arch/x86/boot/compressed/sl_stub.S line 73

Quoted 10 lines
arch/x86/boot/compressed/sl_stub.S new file +10 -0
+
+       .code32
+SYM_FUNC_START(sl_stub_entry)
+       cli
+       cld
+
+       /*
+        * On entry, %ebx has the entry abs offset to sl_stub_entry. This
+        * will be correctly scaled using the rva macro and avoid causing
+        * relocations. Only %cs and %ds segments are known good.

Could you please clarify this? 'scaling' is unidiomatic in this context, and actually means something different in my book. AIUI, %ebx is guaranteed to carry the actual address of sl_stub_entry(), and rva() is used to generate relative references using %ebx as a base, as to avoid /absolute/ relocations, which would require fixups at runtime.

Quoted 726 lines
arch/x86/boot/compressed/sl_stub.S new file +652 -0
+        */
+
+       /* Load GDT, set segment regs and lret to __SL32_CS */
+       leal    rva(sl_gdt_desc)(%ebx), %eax
+       addl    %eax, 2(%eax)
+       lgdt    (%eax)
+
+       movl    $(__SL32_DS), %eax
+       movw    %ax, %ds
+       movw    %ax, %es
+       movw    %ax, %fs
+       movw    %ax, %gs
+       movw    %ax, %ss
+
+       /*
+        * Now that %ss is known good, take the first stack for the BSP. The
+        * AP stacks are only used on Intel.
+        */
+       leal    rva(sl_stacks_end)(%ebx), %esp
+
+       leal    rva(.Lsl_cs)(%ebx), %eax
+       pushl   $(__SL32_CS)
+       pushl   %eax
+       lret
+
+.Lsl_cs:
+       /* Save our base pointer reg and page table for MLE */
+       pushl   %ebx
+       pushl   %ecx
+
+       /* See if SMX feature is supported. */
+       movl    $1, %eax
+       cpuid
+       testl   $(X86_FEATURE_BIT_SMX), %ecx
+       jz      .Ldo_unknown_cpu
+
+       popl    %ecx
+       popl    %ebx
+
+       /* Know it is Intel */
+       movl    $(SL_CPU_INTEL), rva(sl_cpu_type)(%ebx)
+
+       /* Locate the base of the MLE using the page tables in %ecx */
+       call    sl_find_mle_base
+
+       /* Increment CPU count for BSP */
+       incl    rva(sl_txt_cpu_count)(%ebx)
+
+       /*
+        * Enable SMI with GETSEC[SMCTRL] which were disabled by SENTER.
+        * NMIs were also disabled by SENTER. Since there is no IDT for the BSP,
+        * allow the mainline kernel re-enable them in the normal course of
+        * booting.
+        */
+       GETSEC  $(SMX_X86_GETSEC_SMCTRL)
+
+       /* Clear the TXT error registers for a clean start of day */
+       movl    $0, (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_ERRORCODE)
+       movl    $0xffffffff, (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_ESTS)
+
+       /* On Intel, the zero page address is passed in the TXT heap */
+       /* Read physical base of heap into EAX */
+       movl    (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_HEAP_BASE), %eax
+       /* Read the size of the BIOS data into ECX (first 8 bytes) */
+       movl    (%eax), %ecx
+       /* Skip over BIOS data and size of OS to MLE data section */
+       leal    8(%eax, %ecx), %eax
+
+       /* Need to verify the values in the OS-MLE struct passed in */
+       call    sl_txt_verify_os_mle_struct
+
+       /*
+        * Get the boot params address from the heap. Note %esi and %ebx MUST
+        * be preserved across calls and operations.
+        */
+       movl    SL_boot_params_addr(%eax), %esi
+
+       /* Save %ebx so the APs can find their way home */
+       movl    %ebx, (SL_mle_scratch + SL_SCRATCH_AP_EBX)(%eax)
+
+       /* Fetch the AP wake code block address from the heap */
+       movl    SL_ap_wake_block(%eax), %edi
+       movl    %edi, rva(sl_txt_ap_wake_block)(%ebx)
+
+       /* Store the offset in the AP wake block to the jmp address */
+       movl    $(sl_ap_jmp_offset - sl_txt_ap_wake_begin), \
+               (SL_mle_scratch + SL_SCRATCH_AP_JMP_OFFSET)(%eax)
+
+       /* Store the offset in the AP wake block to the AP stacks block */
+       movl    $(sl_stacks - sl_txt_ap_wake_begin), \
+               (SL_mle_scratch + SL_SCRATCH_AP_STACKS_OFFSET)(%eax)
+
+       /* %eax still is the base of the OS-MLE block, save it */
+       pushl   %eax
+
+       /* Relocate the AP wake code to the safe block */
+       call    sl_txt_reloc_ap_wake
+
+       /*
+        * Wake up all APs that are blocked in the ACM and wait for them to
+        * halt. This should be done before restoring the MTRRs so the ACM is
+        * still properly in WB memory.
+        */
+       call    sl_txt_wake_aps
+
+       /* Restore OS-MLE in %eax */
+       popl    %eax
+
+       /*
+        * %edi is used by this routine to find the MTRRs which are in the SLRT
+        * in the Intel info.
+        */
+       movl    SL_txt_info(%eax), %edi
+       call    sl_txt_load_regs
+
+       jmp     .Lcpu_setup_done
+
+.Ldo_unknown_cpu:
+       /* Non-Intel CPUs are not yet supported */
+       ud2
+
+.Lcpu_setup_done:
+       /*
+        * Don't enable MCE at this point. The kernel will enable
+        * it on the BSP later when it is ready.
+        */
+
+       /* Done, jump to normal 32b pm entry */
+       jmp     startup_32
+SYM_FUNC_END(sl_stub_entry)
+
+SYM_FUNC_START(sl_find_mle_base)
+       /* %ecx has PDPT, get first PD */
+       movl    (%ecx), %eax
+       andl    $(PAGE_MASK), %eax
+       /* Get first PT from first PDE */
+       movl    (%eax), %eax
+       andl    $(PAGE_MASK), %eax
+       /* Get MLE base from first PTE */
+       movl    (%eax), %eax
+       andl    $(PAGE_MASK), %eax
+
+       movl    %eax, rva(sl_mle_start)(%ebx)
+       ret
+SYM_FUNC_END(sl_find_mle_base)
+
+SYM_FUNC_START(sl_check_buffer_mle_overlap)
+       /* %ecx: buffer begin %edx: buffer end */
+       /* %ebx: MLE begin %edi: MLE end */
+       /* %eax: region may be inside MLE */
+
+       cmpl    %edi, %ecx
+       jb      .Lnext_check
+       cmpl    %edi, %edx
+       jbe     .Lnext_check
+       jmp     .Lvalid /* Buffer above MLE */
+
+.Lnext_check:
+       cmpl    %ebx, %edx
+       ja      .Linside_check
+       cmpl    %ebx, %ecx
+       jae     .Linside_check
+       jmp     .Lvalid /* Buffer below MLE */
+
+.Linside_check:
+       cmpl    $0, %eax
+       jz      .Linvalid
+       cmpl    %ebx, %ecx
+       jb      .Linvalid
+       cmpl    %edi, %edx
+       ja      .Linvalid
+       jmp     .Lvalid /* Buffer in MLE */
+
+.Linvalid:
+       TXT_RESET $(SL_ERROR_MLE_BUFFER_OVERLAP)
+
+.Lvalid:
+       ret
+SYM_FUNC_END(sl_check_buffer_mle_overlap)
+
+SYM_FUNC_START(sl_txt_verify_os_mle_struct)
+       pushl   %ebx
+       /*
+        * %eax points to the base of the OS-MLE struct. Need to also
+        * read some values from the OS-SINIT struct too.
+        */
+       movl    -8(%eax), %ecx
+       /* Skip over OS to MLE data section and size of OS-SINIT structure */
+       leal    (%eax, %ecx), %edx
+
+       /* Load MLE image base absolute offset */
+       movl    rva(sl_mle_start)(%ebx), %ebx
+
+       /* Verify the value of the low PMR base. It should always be 0. */
+       movl    SL_vtd_pmr_lo_base(%edx), %esi
+       cmpl    $0, %esi
+       jz      .Lvalid_pmr_base
+       TXT_RESET $(SL_ERROR_LO_PMR_BASE)
+
+.Lvalid_pmr_base:
+       /* Grab some values from OS-SINIT structure */
+       movl    SL_mle_size(%edx), %edi
+       addl    %ebx, %edi
+       jc      .Loverflow_detected
+       movl    SL_vtd_pmr_lo_size(%edx), %esi
+
+       /* Check the AP wake block */
+       movl    SL_ap_wake_block(%eax), %ecx
+       movl    SL_ap_wake_block_size(%eax), %edx
+       addl    %ecx, %edx
+       jc      .Loverflow_detected
+       pushl   %eax
+       xorl    %eax, %eax
+       call    sl_check_buffer_mle_overlap
+       popl    %eax
+       cmpl    %esi, %edx
+       ja      .Lbuffer_beyond_pmr
+
+       /*
+        * Check the boot params. Note during a UEFI boot, the boot
+        * params will be inside the MLE image. Test for this case
+        * in the overlap case.
+        */
+       movl    SL_boot_params_addr(%eax), %ecx
+       movl    $(PAGE_SIZE), %edx
+       addl    %ecx, %edx
+       jc      .Loverflow_detected
+       pushl   %eax
+       movl    $1, %eax
+       call    sl_check_buffer_mle_overlap
+       popl    %eax
+       cmpl    %esi, %edx
+       ja      .Lbuffer_beyond_pmr
+
+       /* Check that the AP wake block is big enough */
+       cmpl    $(sl_txt_ap_wake_end - sl_txt_ap_wake_begin), \
+               SL_ap_wake_block_size(%eax)
+       jae     .Lwake_block_ok
+       TXT_RESET $(SL_ERROR_WAKE_BLOCK_TOO_SMALL)
+
+.Lwake_block_ok:
+       popl    %ebx
+       ret
+
+.Loverflow_detected:
+       TXT_RESET $(SL_ERROR_INTEGER_OVERFLOW)
+
+.Lbuffer_beyond_pmr:
+       TXT_RESET $(SL_ERROR_BUFFER_BEYOND_PMR)
+SYM_FUNC_END(sl_txt_verify_os_mle_struct)
+
+SYM_FUNC_START(sl_txt_ap_entry)
+       cli
+       cld
+       /*
+        * The %cs and %ds segments are known good after waking the AP.
+        * First order of business is to find where we are and
+        * save it in %ebx.
+        */
+
+       /* Read physical base of heap into EAX */
+       movl    (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_HEAP_BASE), %eax
+       /* Read the size of the BIOS data into ECX (first 8 bytes) */
+       movl    (%eax), %ecx
+       /* Skip over BIOS data and size of OS to MLE data section */
+       leal    8(%eax, %ecx), %eax
+
+       /* Saved %ebx from the BSP and stash OS-MLE pointer */
+       movl    (SL_mle_scratch + SL_SCRATCH_AP_EBX)(%eax), %ebx
+
+       /* Save TXT info ptr in %edi for call to sl_txt_load_regs */
+       movl    SL_txt_info(%eax), %edi
+
+       /* Lock and get our stack index */
+       movl    $1, %ecx
+.Lspin:
+       xorl    %eax, %eax
+       lock cmpxchgl   %ecx, rva(sl_txt_spin_lock)(%ebx)
+       pause
+       jnz     .Lspin
+
+       /* Increment the stack index and use the next value inside lock */
+       incl    rva(sl_txt_stack_index)(%ebx)
+       movl    rva(sl_txt_stack_index)(%ebx), %eax
+
+       /* Unlock */
+       movl    $0, rva(sl_txt_spin_lock)(%ebx)
+
+       /* Location of the relocated AP wake block */
+       movl    rva(sl_txt_ap_wake_block)(%ebx), %ecx
+
+       /* Load reloc GDT, set segment regs and lret to __SL32_CS */
+       lgdt    (sl_ap_gdt_desc - sl_txt_ap_wake_begin)(%ecx)
+
+       movl    $(__SL32_DS), %edx
+       movw    %dx, %ds
+       movw    %dx, %es
+       movw    %dx, %fs
+       movw    %dx, %gs
+       movw    %dx, %ss
+
+       /* Load our reloc AP stack */
+       movl    $(TXT_BOOT_STACK_SIZE), %edx
+       mull    %edx
+       leal    (sl_stacks_end - sl_txt_ap_wake_begin)(%ecx), %esp
+       subl    %eax, %esp
+
+       /* Switch to AP code segment */
+       leal    rva(.Lsl_ap_cs)(%ebx), %eax
+       pushl   $(__SL32_CS)
+       pushl   %eax
+       lret
+
+.Lsl_ap_cs:
+       /* Load the relocated AP IDT */
+       lidt    (sl_ap_idt_desc - sl_txt_ap_wake_begin)(%ecx)
+
+       /* Fixup MTRRs and misc enable MSR on APs too */
+       call    sl_txt_load_regs
+
+       /* Enable SMI with GETSEC[SMCTRL] */
+       GETSEC $(SMX_X86_GETSEC_SMCTRL)
+
+       /* IRET-to-self can be used to enable NMIs which SENTER disabled */
+       leal    rva(.Lnmi_enabled_ap)(%ebx), %eax
+       pushfl
+       pushl   $(__SL32_CS)
+       pushl   %eax
+       iret
+
+.Lnmi_enabled_ap:
+       /* Put APs in X2APIC mode like the BSP */
+       movl    $(MSR_IA32_APICBASE), %ecx
+       rdmsr
+       orl     $(XAPIC_ENABLE | X2APIC_ENABLE), %eax
+       wrmsr
+
+       /*
+        * Basically done, increment the CPU count and jump off to the AP
+        * wake block to wait.
+        */
+       lock incl       rva(sl_txt_cpu_count)(%ebx)
+
+       movl    rva(sl_txt_ap_wake_block)(%ebx), %eax
+       jmp     *%eax
+SYM_FUNC_END(sl_txt_ap_entry)
+
+SYM_FUNC_START(sl_txt_reloc_ap_wake)
+       /* Save boot params register */
+       pushl   %esi
+
+       movl    rva(sl_txt_ap_wake_block)(%ebx), %edi
+
+       /* Fixup AP IDT and GDT descriptor before relocating */
+       leal    rva(sl_ap_idt_desc)(%ebx), %eax
+       addl    %edi, 2(%eax)
+       leal    rva(sl_ap_gdt_desc)(%ebx), %eax
+       addl    %edi, 2(%eax)
+
+       /*
+        * Copy the AP wake code and AP GDT/IDT to the protected wake block
+        * provided by the loader. Destination already in %edi.
+        */
+       movl    $(sl_txt_ap_wake_end - sl_txt_ap_wake_begin), %ecx
+       leal    rva(sl_txt_ap_wake_begin)(%ebx), %esi
+       rep movsb
+
+       /* Setup the IDT for the APs to use in the relocation block */
+       movl    rva(sl_txt_ap_wake_block)(%ebx), %ecx
+       addl    $(sl_ap_idt - sl_txt_ap_wake_begin), %ecx
+       xorl    %edx, %edx
+
+       /* Form the default reset vector relocation address */
+       movl    rva(sl_txt_ap_wake_block)(%ebx), %esi
+       addl    $(sl_txt_int_reset - sl_txt_ap_wake_begin), %esi
+
+1:
+       cmpw    $(NR_VECTORS), %dx
+       jz      .Lap_idt_done
+
+       cmpw    $(X86_TRAP_NMI), %dx
+       jz      2f
+
+       /* Load all other fixed vectors with reset handler */
+       movl    %esi, %eax
+       movw    %ax, (IDT_VECTOR_LO_BITS)(%ecx)
+       shrl    $16, %eax
+       movw    %ax, (IDT_VECTOR_HI_BITS)(%ecx)
+       jmp     3f
+
+2:
+       /* Load single wake NMI IPI vector at the relocation address */
+       movl    rva(sl_txt_ap_wake_block)(%ebx), %eax
+       addl    $(sl_txt_int_nmi - sl_txt_ap_wake_begin), %eax
+       movw    %ax, (IDT_VECTOR_LO_BITS)(%ecx)
+       shrl    $16, %eax
+       movw    %ax, (IDT_VECTOR_HI_BITS)(%ecx)
+
+3:
+       incw    %dx
+       addl    $8, %ecx
+       jmp     1b
+
+.Lap_idt_done:
+       popl    %esi
+       ret
+SYM_FUNC_END(sl_txt_reloc_ap_wake)
+
+SYM_FUNC_START(sl_txt_load_regs)
+       /* Save base pointer register */
+       pushl   %ebx
+
+       /*
+        * On Intel, the original variable MTRRs and Misc Enable MSR are
+        * restored on the BSP at early boot. Each AP will also restore
+        * its MTRRs and Misc Enable MSR.
+        */
+       pushl   %edi
+       addl    $(SL_saved_bsp_mtrrs), %edi
+       movl    (%edi), %ebx
+       pushl   %ebx /* default_mem_type lo */
+       addl    $4, %edi
+       movl    (%edi), %ebx
+       pushl   %ebx /* default_mem_type hi */
+       addl    $4, %edi
+       movl    (%edi), %ebx /* mtrr_vcnt lo, don't care about hi part */
+       addl    $8, %edi /* now at MTRR pair array */
+       /* Write the variable MTRRs */
+       movl    $(MSR_MTRRphysBase0), %ecx
+1:
+       cmpl    $0, %ebx
+       jz      2f
+
+       movl    (%edi), %eax /* MTRRphysBaseX lo */
+       addl    $4, %edi
+       movl    (%edi), %edx /* MTRRphysBaseX hi */
+       wrmsr
+       addl    $4, %edi
+       incl    %ecx
+       movl    (%edi), %eax /* MTRRphysMaskX lo */
+       addl    $4, %edi
+       movl    (%edi), %edx /* MTRRphysMaskX hi */
+       wrmsr
+       addl    $4, %edi
+       incl    %ecx
+
+       decl    %ebx
+       jmp     1b
+2:
+       /* Write the default MTRR register */
+       popl    %edx
+       popl    %eax
+       movl    $(MSR_MTRRdefType), %ecx
+       wrmsr
+
+       /* Return to beginning and write the misc enable msr */
+       popl    %edi
+       addl    $(SL_saved_misc_enable_msr), %edi
+       movl    (%edi), %eax /* saved_misc_enable_msr lo */
+       addl    $4, %edi
+       movl    (%edi), %edx /* saved_misc_enable_msr hi */
+       movl    $(MSR_IA32_MISC_ENABLE), %ecx
+       wrmsr
+
+       popl    %ebx
+       ret
+SYM_FUNC_END(sl_txt_load_regs)
+
+SYM_FUNC_START(sl_txt_wake_aps)
+       /* Save boot params register */
+       pushl   %esi
+
+       /* First setup the MLE join structure and load it into TXT reg */
+       leal    rva(sl_gdt)(%ebx), %eax
+       leal    rva(sl_txt_ap_entry)(%ebx), %ecx
+       leal    rva(sl_smx_rlp_mle_join)(%ebx), %edx
+       movl    %eax, SL_rlp_gdt_base(%edx)
+       movl    %ecx, SL_rlp_entry_point(%edx)
+       movl    %edx, (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_MLE_JOIN)
+
+       /* Another TXT heap walk to find various values needed to wake APs */
+       movl    (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_HEAP_BASE), %eax
+       /* At BIOS data size, find the number of logical processors */
+       movl    (SL_num_logical_procs + 8)(%eax), %edx
+       /* Skip over BIOS data */
+       movl    (%eax), %ecx
+       addl    %ecx, %eax
+       /* Skip over OS to MLE */
+       movl    (%eax), %ecx
+       addl    %ecx, %eax
+       /* At OS-SNIT size, get capabilities to know how to wake up the APs */
+       movl    (SL_capabilities + 8)(%eax), %esi
+       /* Skip over OS to SNIT */
+       movl    (%eax), %ecx
+       addl    %ecx, %eax
+       /* At SINIT-MLE size, get the AP wake MONITOR address */
+       movl    (SL_rlp_wakeup_addr + 8)(%eax), %edi
+
+       /* Determine how to wake up the APs */
+       testl   $(1 << TXT_SINIT_MLE_CAP_WAKE_MONITOR), %esi
+       jz      .Lwake_getsec
+
+       /* Wake using MWAIT MONITOR */
+       movl    $1, (%edi)
+       jmp     .Laps_awake
+
+.Lwake_getsec:
+       /* Wake using GETSEC(WAKEUP) */
+       GETSEC  $(SMX_X86_GETSEC_WAKEUP)
+
+.Laps_awake:
+       /*
+        * All of the APs are woken up and rendesvous in the relocated wake
+        * block starting at sl_txt_ap_wake_begin. Wait for all of them to
+        * halt.
+        */
+       pause
+       cmpl    rva(sl_txt_cpu_count)(%ebx), %edx
+       jne     .Laps_awake
+
+       popl    %esi
+       ret
+SYM_FUNC_END(sl_txt_wake_aps)
+
+/* This is the beginning of the relocated AP wake code block */
+       .global sl_txt_ap_wake_begin
+sl_txt_ap_wake_begin:
+
+       /* Get the LAPIC ID for each AP and stash it on the stack */
+       movl    $(MSR_IA32_X2APIC_APICID), %ecx
+       rdmsr
+       pushl   %eax
+
+       /*
+        * Get a pointer to the monitor location on this APs stack to test below
+        * after mwait returns. Currently %esp points to just past the pushed APIC
+        * ID value.
+        */
+       movl    %esp, %eax
+       subl    $(TXT_BOOT_STACK_SIZE - 4), %eax
+       movl    $0, (%eax)
+
+       /* Clear ecx/edx so no invalid extensions or hints are passed to monitor */
+       xorl    %ecx, %ecx
+       xorl    %edx, %edx
+
+       /*
+        * Arm the monitor and wait for it to be poked by he SMP bringup code. The mwait
+        * instruction can return for a number of reasons. Test to see if it returned
+        * because the monitor was written to.
+        */
+       monitor
+
+1:
+       mfence
+       mwait
+       movl    (%eax), %edx
+       testl   %edx, %edx
+       jz      1b
+
+       /*
+        * This is the long absolute jump to the 32b Secure Launch protected mode stub
+        * code in sl_trampoline_start32() in the rmpiggy. The jump address will be
+        * fixed in the SMP boot code when the first AP is brought up. This whole area
+        * is provided and protected in the memory map by the prelaunch code.
+        */
+       .byte   0xea
+sl_ap_jmp_offset:
+       .long   0x00000000
+       .word   __SL32_CS
+
+SYM_FUNC_START(sl_txt_int_nmi)
+       /* NMI context, just IRET */
+       iret
+SYM_FUNC_END(sl_txt_int_nmi)
+
+SYM_FUNC_START(sl_txt_int_reset)
+       TXT_RESET $(SL_ERROR_INV_AP_INTERRUPT)
+SYM_FUNC_END(sl_txt_int_reset)
+
+       .balign 8
+SYM_DATA_START_LOCAL(sl_ap_idt_desc)
+       .word   sl_ap_idt_end - sl_ap_idt - 1           /* Limit */
+       .long   sl_ap_idt - sl_txt_ap_wake_begin        /* Base */
+SYM_DATA_END_LABEL(sl_ap_idt_desc, SYM_L_LOCAL, sl_ap_idt_desc_end)
+
+       .balign 8
+SYM_DATA_START_LOCAL(sl_ap_idt)
+       .rept   NR_VECTORS
+       .word   0x0000          /* Offset 15 to 0 */
+       .word   __SL32_CS       /* Segment selector */
+       .word   0x8e00          /* Present, DPL=0, 32b Vector, Interrupt */
+       .word   0x0000          /* Offset 31 to 16 */
+       .endr
+SYM_DATA_END_LABEL(sl_ap_idt, SYM_L_LOCAL, sl_ap_idt_end)
+
+       .balign 8
+SYM_DATA_START_LOCAL(sl_ap_gdt_desc)
+       .word   sl_ap_gdt_end - sl_ap_gdt - 1
+       .long   sl_ap_gdt - sl_txt_ap_wake_begin
+SYM_DATA_END_LABEL(sl_ap_gdt_desc, SYM_L_LOCAL, sl_ap_gdt_desc_end)
+
+       .balign 8
+SYM_DATA_START_LOCAL(sl_ap_gdt)
+       .quad   0x0000000000000000      /* NULL */
+       .quad   0x00cf9a000000ffff      /* __SL32_CS */
+       .quad   0x00cf92000000ffff      /* __SL32_DS */
+SYM_DATA_END_LABEL(sl_ap_gdt, SYM_L_LOCAL, sl_ap_gdt_end)
+
+       /* Small stacks for BSP and APs to work with */
+       .balign 64
+SYM_DATA_START_LOCAL(sl_stacks)
+       .fill (TXT_MAX_CPUS * TXT_BOOT_STACK_SIZE), 1, 0
+SYM_DATA_END_LABEL(sl_stacks, SYM_L_LOCAL, sl_stacks_end)
+
+/* This is the end of the relocated AP wake code block */
+       .global sl_txt_ap_wake_end
+sl_txt_ap_wake_end:
+
+       .data
+       .balign 8
+SYM_DATA_START_LOCAL(sl_gdt_desc)
+       .word   sl_gdt_end - sl_gdt - 1
+       .long   sl_gdt - sl_gdt_desc
+SYM_DATA_END_LABEL(sl_gdt_desc, SYM_L_LOCAL, sl_gdt_desc_end)
+
+       .balign 8
+SYM_DATA_START_LOCAL(sl_gdt)
+       .quad   0x0000000000000000      /* NULL */
+       .quad   0x00cf9a000000ffff      /* __SL32_CS */
+       .quad   0x00cf92000000ffff      /* __SL32_DS */
+SYM_DATA_END_LABEL(sl_gdt, SYM_L_LOCAL, sl_gdt_end)
+
+       .balign 8
+SYM_DATA_START_LOCAL(sl_smx_rlp_mle_join)
+       .long   sl_gdt_end - sl_gdt - 1 /* GDT limit */
+       .long   0x00000000              /* GDT base */
+       .long   __SL32_CS       /* Seg Sel - CS (DS, ES, SS = seg_sel+8) */
+       .long   0x00000000      /* Entry point physical address */
+SYM_DATA_END(sl_smx_rlp_mle_join)
+
+SYM_DATA(sl_cpu_type, .long 0x00000000)
+
+SYM_DATA(sl_mle_start, .long 0x00000000)
+
+SYM_DATA_LOCAL(sl_txt_spin_lock, .long 0x00000000)
+
+SYM_DATA_LOCAL(sl_txt_stack_index, .long 0x00000000)
+
+SYM_DATA_LOCAL(sl_txt_cpu_count, .long 0x00000000)
+
+SYM_DATA_LOCAL(sl_txt_ap_wake_block, .long 0x00000000)
arch/x86/include/asm/msr-index.h +5 -0
index e022e6eb766c..37f6167f28ba 100644
@@ -348,6 +348,9 @@
 #define MSR_IA32_RTIT_OUTPUT_BASE      0x00000560
 #define MSR_IA32_RTIT_OUTPUT_MASK      0x00000561
 
+#define MSR_MTRRphysBase0              0x00000200
+#define MSR_MTRRphysMask0              0x00000201
+
 #define MSR_MTRRfix64K_00000           0x00000250
 #define MSR_MTRRfix16K_80000           0x00000258
 #define MSR_MTRRfix16K_A0000           0x00000259
@@ -849,6 +852,8 @@
 #define MSR_IA32_APICBASE_ENABLE       (1<<11)
 #define MSR_IA32_APICBASE_BASE         (0xfffff<<12)
 
+#define MSR_IA32_X2APIC_APICID         0x00000802
+
 #define MSR_IA32_UCODE_WRITE           0x00000079
 #define MSR_IA32_UCODE_REV             0x0000008b
 
arch/x86/include/uapi/asm/bootparam.h +1 -0
index 9b82eebd7add..7ce283a22d6b 100644
@@ -12,6 +12,7 @@
 /* loadflags */
 #define LOADED_HIGH    (1<<0)
 #define KASLR_FLAG     (1<<1)
+#define SLAUNCH_FLAG   (1<<2)
 #define QUIET_FLAG     (1<<5)
 #define KEEP_SEGMENTS  (1<<6)
 #define CAN_USE_HEAP   (1<<7)
arch/x86/kernel/asm-offsets.c +20 -1
index a98020bf31bb..925adce6e2c7 100644
@@ -13,6 +13,8 @@
 #include <linux/hardirq.h>
 #include <linux/suspend.h>
 #include <linux/kbuild.h>
+#include <linux/slr_table.h>
+#include <linux/slaunch.h>
 #include <asm/processor.h>
 #include <asm/thread_info.h>
 #include <asm/sigframe.h>
@@ -120,4 +122,22 @@ static void __used common(void)
        OFFSET(ARIA_CTX_rounds, aria_ctx, rounds);
 #endif
 
+#ifdef CONFIG_SECURE_LAUNCH
+       BLANK();
+       OFFSET(SL_txt_info, txt_os_mle_data, txt_info);
+       OFFSET(SL_mle_scratch, txt_os_mle_data, mle_scratch);
+       OFFSET(SL_boot_params_addr, txt_os_mle_data, boot_params_addr);
+       OFFSET(SL_ap_wake_block, txt_os_mle_data, ap_wake_block);
+       OFFSET(SL_ap_wake_block_size, txt_os_mle_data, ap_wake_block_size);
+       OFFSET(SL_saved_misc_enable_msr, slr_entry_intel_info, saved_misc_enable_msr);
+       OFFSET(SL_saved_bsp_mtrrs, slr_entry_intel_info, saved_bsp_mtrrs);
+       OFFSET(SL_num_logical_procs, txt_bios_data, num_logical_procs);
+       OFFSET(SL_capabilities, txt_os_sinit_data, capabilities);
+       OFFSET(SL_mle_size, txt_os_sinit_data, mle_size);
+       OFFSET(SL_vtd_pmr_lo_base, txt_os_sinit_data, vtd_pmr_lo_base);
+       OFFSET(SL_vtd_pmr_lo_size, txt_os_sinit_data, vtd_pmr_lo_size);
+       OFFSET(SL_rlp_wakeup_addr, txt_sinit_mle_data, rlp_wakeup_addr);
+       OFFSET(SL_rlp_gdt_base, smx_rlp_mle_join, rlp_gdt_base);
+       OFFSET(SL_rlp_entry_point, smx_rlp_mle_join, rlp_entry_point);
+#endif
 }
--

2.39.3

maintainer
#71 lore raw

Re: [PATCH v9 08/19] x86: Secure Launch kernel early boot stub

Quoted 193 lines · Ard Biesheuvel

Hello Ross,

Quoted 57 lines · Ross Philipson

The Secure Launch (SL) stub provides the entry point for Intel TXT (and later AMD SKINIT) to vector to during the late launch. The symbol sl_stub_entry is that entry point and its offset into the kernel is conveyed to the launching code using the MLE (Measured Launch Environment) header in the structure named mle_header. The offset of the MLE header is set in the kernel_info. The routine sl_stub contains the very early late launch setup code responsible for setting up the basic environment to allow the normal kernel startup_32 code to proceed. It is also responsible for properly waking and handling the APs on Intel platforms. The routine sl_main which runs after entering 64b mode is responsible for measuring configuration and module information before it is used like the boot params, the kernel command line, the TXT heap, an external initramfs, etc.

---
 Documentation/arch/x86/boot.rst        |  21 +
 arch/x86/boot/compressed/Makefile      |   3 +-
 arch/x86/boot/compressed/head_64.S     |  30 +
 arch/x86/boot/compressed/kernel_info.S |  34 ++
 arch/x86/boot/compressed/sl_main.c     | 577 ++++++++++++++++++++
 arch/x86/boot/compressed/sl_stub.S     | 725 +++++++++++++++++++++++++
 arch/x86/include/asm/msr-index.h       |   5 +
 arch/x86/include/uapi/asm/bootparam.h  |   1 +
 arch/x86/kernel/asm-offsets.c          |  20 +
 9 files changed, 1415 insertions(+), 1 deletion(-)
 create mode 100644 arch/x86/boot/compressed/sl_main.c
 create mode 100644 arch/x86/boot/compressed/sl_stub.S
Documentation/arch/x86/boot.rst +12 -0
index 4fd492cb4970..295cdf9bcbdb 100644
@@ -482,6 +482,14 @@ Protocol:  2.00+
            - If 1, KASLR enabled.
            - If 0, KASLR disabled.
 
+  Bit 2 (kernel internal): SLAUNCH_FLAG
+
+       - Used internally by the setup kernel to communicate
+         Secure Launch status to kernel proper.
+
+           - If 1, Secure Launch enabled.
+           - If 0, Secure Launch disabled.
+
   Bit 5 (write): QUIET_FLAG
 
        - If 0, print early messages.
@@ -1028,6 +1036,19 @@ Offset/size:     0x000c/4
 
   This field contains maximal allowed type for setup_data and setup_indirect structs.
 
+============   =================
+Field name:    mle_header_offset
+Offset/size:   0x0010/4
+============   =================

Documentation/arch/x86/boot.rst Offset/size: 0x000c/4 · line 1052

Quoted 10 lines
Documentation/arch/x86/boot.rst +9 -0
+
+  This field contains the offset to the Secure Launch Measured Launch Environment
+  (MLE) header. This offset is used to locate information needed during a secure
+  late launch using Intel TXT. If the offset is zero, the kernel does not have
+  Secure Launch capabilities. The MLE entry point is called from TXT on the BSP
+  following a success measured launch. The specific state of the processors is
+  outlined in the TXT Software Development Guide, the latest can be found here:
+  https://www.intel.com/content/dam/www/public/us/en/documents/guides/intel-txt-software-development-guide.pdf
+
 

Could we just repaint this field as the offset relative to the start of kernel_info rather than relative to the start of the image? That way, there is no need for patch #1, and given that the consumer of this field accesses it via kernel_info, I wouldn't expect any issues in applying this offset to obtain the actual address.

Quoted 21 lines
Documentation/arch/x86/boot.rst +0 -0
 The Image Checksum
 ==================
arch/x86/boot/compressed/Makefile +2 -1
index 9189a0e28686..9076a248d4b4 100644
@@ -118,7 +118,8 @@ vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi.o
 vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_mixed.o
 vmlinux-objs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
 
-vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o $(obj)/early_sha256.o
+vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o $(obj)/early_sha256.o \
+       $(obj)/sl_main.o $(obj)/sl_stub.o
 
 $(obj)/vmlinux: $(vmlinux-objs-y) FORCE
        $(call if_changed,ld)
arch/x86/boot/compressed/head_64.S +0 -0
index 1dcb794c5479..803c9e2e6d85 100644

arch/x86/boot/compressed/head_64.S SYM_CODE_START(startup_64) · line 424

Quoted 5 lines
arch/x86/boot/compressed/head_64.S +2 -0
@@ -420,6 +420,13 @@ SYM_CODE_START(startup_64)
        pushq   $0
        popfq
 
+#ifdef CONFIG_SECURE_LAUNCH
+       /* Ensure the relocation region is coverd by a PMR */

covered

Quoted 25 lines
arch/x86/boot/compressed/head_64.S +18 -0
+       movq    %rbx, %rdi
+       movl    $(_bss - startup_32), %esi
+       callq   sl_check_region
+#endif
+
 /*
  * Copy the compressed kernel to the end of our buffer
  * where decompression in place becomes safe.
@@ -462,6 +469,29 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated)
        shrq    $3, %rcx
        rep     stosq
 
+#ifdef CONFIG_SECURE_LAUNCH
+       /*
+        * Have to do the final early sl stub work in 64b area.
+        *
+        * *********** NOTE ***********
+        *
+        * Several boot params get used before we get a chance to measure
+        * them in this call. This is a known issue and we currently don't
+        * have a solution. The scratch field doesn't matter. There is no
+        * obvious way to do anything about the use of kernel_alignment or
+        * init_size though these seem low risk with all the PMR and overlap
+        * checks in place.
+        */

arch/x86/boot/compressed/head_64.S SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated) · line 494

Quoted 10 lines
arch/x86/boot/compressed/head_64.S +10 -0
+       movq    %r15, %rdi
+       callq   sl_main
+
+       /* Ensure the decompression location is covered by a PMR */
+       movq    %rbp, %rdi
+       movq    output_len(%rip), %rsi
+       callq   sl_check_region
+#endif
+
+       pushq   %rsi

This looks like a rebase error.

Quoted 8 lines
arch/x86/boot/compressed/head_64.S +0 -0
        call    load_stage2_idt
 
        /* Pass boot_params to initialize_identity_maps() */
arch/x86/boot/compressed/kernel_info.S +0 -0
index c18f07181dd5..e199b87764e9 100644

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 33

Quoted 6 lines
arch/x86/boot/compressed/kernel_info.S +3 -0
@@ -28,6 +28,40 @@ SYM_DATA_START(kernel_info)
        /* Maximal allowed type for setup_data and setup_indirect structs. */
        .long   SETUP_TYPE_MAX
 
+       /* Offset to the MLE header structure */
+#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
+       .long   rva(mle_header)

... so this could just be mle_header - kernel_info, and the consumer can do the math instead.

Quoted 16 lines
arch/x86/boot/compressed/kernel_info.S +13 -0
+#else
+       .long   0
+#endif
+
 kernel_info_var_len_data:
        /* Empty for time being... */
 SYM_DATA_END_LABEL(kernel_info, SYM_L_LOCAL, kernel_info_end)
+
+#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
+       /*
+        * The MLE Header per the TXT Specification, section 2.1
+        * MLE capabilities, see table 4. Capabilities set:
+        * bit 0: Support for GETSEC[WAKEUP] for RLP wakeup
+        * bit 1: Support for RLP wakeup using MONITOR address
+        * bit 2: The ECX register will contain the pointer to the MLE page table
+        * bit 5: TPM 1.2 family: Details/authorities PCR usage support

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 59

Quoted 10 lines
arch/x86/boot/compressed/kernel_info.S +10 -0
+        * bit 9: Supported format of TPM 2.0 event log - TCG compliant
+        */
+SYM_DATA_START(mle_header)
+       .long   0x9082ac5a  /* UUID0 */
+       .long   0x74a7476f  /* UUID1 */
+       .long   0xa2555c0f  /* UUID2 */
+       .long   0x42b651cb  /* UUID3 */
+       .long   0x00000034  /* MLE header size */
+       .long   0x00020002  /* MLE version 2.2 */
+       .long   rva(sl_stub_entry) /* Linear entry point of MLE (virt. address) */

and these should perhaps be relative to mle_header?

Quoted 6 lines

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 62

Quoted 3 lines
arch/x86/boot/compressed/kernel_info.S +3 -0
+       .long   0x00000000  /* First valid page of MLE */
+       .long   0x00000000  /* Offset within binary of first byte of MLE */
+       .long   rva(_edata) /* Offset within binary of last byte + 1 of MLE */

and here

Ugh never mind - these are specified externally.

maintainer
#72 lore raw

Re: [PATCH v9 08/19] x86: Secure Launch kernel early boot stub

Quoted 195 lines · Ard Biesheuvel
Quoted 193 lines · Ard Biesheuvel

Hello Ross,

Quoted 57 lines · Ross Philipson

The Secure Launch (SL) stub provides the entry point for Intel TXT (and later AMD SKINIT) to vector to during the late launch. The symbol sl_stub_entry is that entry point and its offset into the kernel is conveyed to the launching code using the MLE (Measured Launch Environment) header in the structure named mle_header. The offset of the MLE header is set in the kernel_info. The routine sl_stub contains the very early late launch setup code responsible for setting up the basic environment to allow the normal kernel startup_32 code to proceed. It is also responsible for properly waking and handling the APs on Intel platforms. The routine sl_main which runs after entering 64b mode is responsible for measuring configuration and module information before it is used like the boot params, the kernel command line, the TXT heap, an external initramfs, etc.

---
 Documentation/arch/x86/boot.rst        |  21 +
 arch/x86/boot/compressed/Makefile      |   3 +-
 arch/x86/boot/compressed/head_64.S     |  30 +
 arch/x86/boot/compressed/kernel_info.S |  34 ++
 arch/x86/boot/compressed/sl_main.c     | 577 ++++++++++++++++++++
 arch/x86/boot/compressed/sl_stub.S     | 725 +++++++++++++++++++++++++
 arch/x86/include/asm/msr-index.h       |   5 +
 arch/x86/include/uapi/asm/bootparam.h  |   1 +
 arch/x86/kernel/asm-offsets.c          |  20 +
 9 files changed, 1415 insertions(+), 1 deletion(-)
 create mode 100644 arch/x86/boot/compressed/sl_main.c
 create mode 100644 arch/x86/boot/compressed/sl_stub.S
Documentation/arch/x86/boot.rst +12 -0
index 4fd492cb4970..295cdf9bcbdb 100644
@@ -482,6 +482,14 @@ Protocol:  2.00+
            - If 1, KASLR enabled.
            - If 0, KASLR disabled.
 
+  Bit 2 (kernel internal): SLAUNCH_FLAG
+
+       - Used internally by the setup kernel to communicate
+         Secure Launch status to kernel proper.
+
+           - If 1, Secure Launch enabled.
+           - If 0, Secure Launch disabled.
+
   Bit 5 (write): QUIET_FLAG
 
        - If 0, print early messages.
@@ -1028,6 +1036,19 @@ Offset/size:     0x000c/4
 
   This field contains maximal allowed type for setup_data and setup_indirect structs.
 
+============   =================
+Field name:    mle_header_offset
+Offset/size:   0x0010/4
+============   =================

Documentation/arch/x86/boot.rst Offset/size: 0x000c/4 · line 1052

Quoted 10 lines
Documentation/arch/x86/boot.rst +9 -0
+
+  This field contains the offset to the Secure Launch Measured Launch Environment
+  (MLE) header. This offset is used to locate information needed during a secure
+  late launch using Intel TXT. If the offset is zero, the kernel does not have
+  Secure Launch capabilities. The MLE entry point is called from TXT on the BSP
+  following a success measured launch. The specific state of the processors is
+  outlined in the TXT Software Development Guide, the latest can be found here:
+  https://www.intel.com/content/dam/www/public/us/en/documents/guides/intel-txt-software-development-guide.pdf
+
 

Could we just repaint this field as the offset relative to the start of kernel_info rather than relative to the start of the image? That way, there is no need for patch #1, and given that the consumer of this field accesses it via kernel_info, I wouldn't expect any issues in applying this offset to obtain the actual address.

Quoted 21 lines
Documentation/arch/x86/boot.rst +0 -0
 The Image Checksum
 ==================
arch/x86/boot/compressed/Makefile +2 -1
index 9189a0e28686..9076a248d4b4 100644
@@ -118,7 +118,8 @@ vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi.o
 vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_mixed.o
 vmlinux-objs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
 
-vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o $(obj)/early_sha256.o
+vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o $(obj)/early_sha256.o \
+       $(obj)/sl_main.o $(obj)/sl_stub.o
 
 $(obj)/vmlinux: $(vmlinux-objs-y) FORCE
        $(call if_changed,ld)
arch/x86/boot/compressed/head_64.S +0 -0
index 1dcb794c5479..803c9e2e6d85 100644

arch/x86/boot/compressed/head_64.S SYM_CODE_START(startup_64) · line 424

Quoted 5 lines
arch/x86/boot/compressed/head_64.S +2 -0
@@ -420,6 +420,13 @@ SYM_CODE_START(startup_64)
        pushq   $0
        popfq
 
+#ifdef CONFIG_SECURE_LAUNCH
+       /* Ensure the relocation region is coverd by a PMR */

covered

Quoted 25 lines
arch/x86/boot/compressed/head_64.S +18 -0
+       movq    %rbx, %rdi
+       movl    $(_bss - startup_32), %esi
+       callq   sl_check_region
+#endif
+
 /*
  * Copy the compressed kernel to the end of our buffer
  * where decompression in place becomes safe.
@@ -462,6 +469,29 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated)
        shrq    $3, %rcx
        rep     stosq
 
+#ifdef CONFIG_SECURE_LAUNCH
+       /*
+        * Have to do the final early sl stub work in 64b area.
+        *
+        * *********** NOTE ***********
+        *
+        * Several boot params get used before we get a chance to measure
+        * them in this call. This is a known issue and we currently don't
+        * have a solution. The scratch field doesn't matter. There is no
+        * obvious way to do anything about the use of kernel_alignment or
+        * init_size though these seem low risk with all the PMR and overlap
+        * checks in place.
+        */

arch/x86/boot/compressed/head_64.S SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated) · line 494

Quoted 10 lines
arch/x86/boot/compressed/head_64.S +10 -0
+       movq    %r15, %rdi
+       callq   sl_main
+
+       /* Ensure the decompression location is covered by a PMR */
+       movq    %rbp, %rdi
+       movq    output_len(%rip), %rsi
+       callq   sl_check_region
+#endif
+
+       pushq   %rsi

This looks like a rebase error.

Quoted 8 lines
arch/x86/boot/compressed/head_64.S +0 -0
        call    load_stage2_idt
 
        /* Pass boot_params to initialize_identity_maps() */
arch/x86/boot/compressed/kernel_info.S +0 -0
index c18f07181dd5..e199b87764e9 100644

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 33

Quoted 6 lines
arch/x86/boot/compressed/kernel_info.S +3 -0
@@ -28,6 +28,40 @@ SYM_DATA_START(kernel_info)
        /* Maximal allowed type for setup_data and setup_indirect structs. */
        .long   SETUP_TYPE_MAX
 
+       /* Offset to the MLE header structure */
+#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
+       .long   rva(mle_header)

... so this could just be mle_header - kernel_info, and the consumer can do the math instead.

Quoted 16 lines
arch/x86/boot/compressed/kernel_info.S +13 -0
+#else
+       .long   0
+#endif
+
 kernel_info_var_len_data:
        /* Empty for time being... */
 SYM_DATA_END_LABEL(kernel_info, SYM_L_LOCAL, kernel_info_end)
+
+#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
+       /*
+        * The MLE Header per the TXT Specification, section 2.1
+        * MLE capabilities, see table 4. Capabilities set:
+        * bit 0: Support for GETSEC[WAKEUP] for RLP wakeup
+        * bit 1: Support for RLP wakeup using MONITOR address
+        * bit 2: The ECX register will contain the pointer to the MLE page table
+        * bit 5: TPM 1.2 family: Details/authorities PCR usage support

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 59

Quoted 10 lines
arch/x86/boot/compressed/kernel_info.S +10 -0
+        * bit 9: Supported format of TPM 2.0 event log - TCG compliant
+        */
+SYM_DATA_START(mle_header)
+       .long   0x9082ac5a  /* UUID0 */
+       .long   0x74a7476f  /* UUID1 */
+       .long   0xa2555c0f  /* UUID2 */
+       .long   0x42b651cb  /* UUID3 */
+       .long   0x00000034  /* MLE header size */
+       .long   0x00020002  /* MLE version 2.2 */
+       .long   rva(sl_stub_entry) /* Linear entry point of MLE (virt. address) */

and these should perhaps be relative to mle_header?

Quoted 8 lines
Quoted 6 lines

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 62

Quoted 3 lines
arch/x86/boot/compressed/kernel_info.S +3 -0
+       .long   0x00000000  /* First valid page of MLE */
+       .long   0x00000000  /* Offset within binary of first byte of MLE */
+       .long   rva(_edata) /* Offset within binary of last byte + 1 of MLE */

and here

Ugh never mind - these are specified externally.

OK, so instead of patch #1, please use the linker script to generate these constants.

I.e., add this to arch/x86/boot/compressed/vmlinux.lds.S

#ifdef CONFIG_SECURE_LAUNCH

PROVIDE(mle_header_offset       = mle_header - startup_32);
PROVIDE(sl_stub_entry_offset    = sl_stub_entry - startup_32);
PROVIDE(_edata_offset           = _edata - startup_32);

#endif

and use the symbols on the left hand side in the code.

maintainer
#73 lore raw

Re: [PATCH v9 08/19] x86: Secure Launch kernel early boot stub

Quoted 211 lines · Ard Biesheuvel
Quoted 195 lines · Ard Biesheuvel
Quoted 193 lines · Ard Biesheuvel

Hello Ross,

Quoted 57 lines · Ross Philipson

The Secure Launch (SL) stub provides the entry point for Intel TXT (and later AMD SKINIT) to vector to during the late launch. The symbol sl_stub_entry is that entry point and its offset into the kernel is conveyed to the launching code using the MLE (Measured Launch Environment) header in the structure named mle_header. The offset of the MLE header is set in the kernel_info. The routine sl_stub contains the very early late launch setup code responsible for setting up the basic environment to allow the normal kernel startup_32 code to proceed. It is also responsible for properly waking and handling the APs on Intel platforms. The routine sl_main which runs after entering 64b mode is responsible for measuring configuration and module information before it is used like the boot params, the kernel command line, the TXT heap, an external initramfs, etc.

---
 Documentation/arch/x86/boot.rst        |  21 +
 arch/x86/boot/compressed/Makefile      |   3 +-
 arch/x86/boot/compressed/head_64.S     |  30 +
 arch/x86/boot/compressed/kernel_info.S |  34 ++
 arch/x86/boot/compressed/sl_main.c     | 577 ++++++++++++++++++++
 arch/x86/boot/compressed/sl_stub.S     | 725 +++++++++++++++++++++++++
 arch/x86/include/asm/msr-index.h       |   5 +
 arch/x86/include/uapi/asm/bootparam.h  |   1 +
 arch/x86/kernel/asm-offsets.c          |  20 +
 9 files changed, 1415 insertions(+), 1 deletion(-)
 create mode 100644 arch/x86/boot/compressed/sl_main.c
 create mode 100644 arch/x86/boot/compressed/sl_stub.S
Documentation/arch/x86/boot.rst +12 -0
index 4fd492cb4970..295cdf9bcbdb 100644
@@ -482,6 +482,14 @@ Protocol:  2.00+
            - If 1, KASLR enabled.
            - If 0, KASLR disabled.
 
+  Bit 2 (kernel internal): SLAUNCH_FLAG
+
+       - Used internally by the setup kernel to communicate
+         Secure Launch status to kernel proper.
+
+           - If 1, Secure Launch enabled.
+           - If 0, Secure Launch disabled.
+
   Bit 5 (write): QUIET_FLAG
 
        - If 0, print early messages.
@@ -1028,6 +1036,19 @@ Offset/size:     0x000c/4
 
   This field contains maximal allowed type for setup_data and setup_indirect structs.
 
+============   =================
+Field name:    mle_header_offset
+Offset/size:   0x0010/4
+============   =================

Documentation/arch/x86/boot.rst Offset/size: 0x000c/4 · line 1052

Quoted 10 lines
Documentation/arch/x86/boot.rst +9 -0
+
+  This field contains the offset to the Secure Launch Measured Launch Environment
+  (MLE) header. This offset is used to locate information needed during a secure
+  late launch using Intel TXT. If the offset is zero, the kernel does not have
+  Secure Launch capabilities. The MLE entry point is called from TXT on the BSP
+  following a success measured launch. The specific state of the processors is
+  outlined in the TXT Software Development Guide, the latest can be found here:
+  https://www.intel.com/content/dam/www/public/us/en/documents/guides/intel-txt-software-development-guide.pdf
+
 

Could we just repaint this field as the offset relative to the start of kernel_info rather than relative to the start of the image? That way, there is no need for patch #1, and given that the consumer of this field accesses it via kernel_info, I wouldn't expect any issues in applying this offset to obtain the actual address.

Quoted 21 lines
Documentation/arch/x86/boot.rst +0 -0
 The Image Checksum
 ==================
arch/x86/boot/compressed/Makefile +2 -1
index 9189a0e28686..9076a248d4b4 100644
@@ -118,7 +118,8 @@ vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi.o
 vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_mixed.o
 vmlinux-objs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
 
-vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o $(obj)/early_sha256.o
+vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o $(obj)/early_sha256.o \
+       $(obj)/sl_main.o $(obj)/sl_stub.o
 
 $(obj)/vmlinux: $(vmlinux-objs-y) FORCE
        $(call if_changed,ld)
arch/x86/boot/compressed/head_64.S +0 -0
index 1dcb794c5479..803c9e2e6d85 100644

arch/x86/boot/compressed/head_64.S SYM_CODE_START(startup_64) · line 424

Quoted 5 lines
arch/x86/boot/compressed/head_64.S +2 -0
@@ -420,6 +420,13 @@ SYM_CODE_START(startup_64)
        pushq   $0
        popfq
 
+#ifdef CONFIG_SECURE_LAUNCH
+       /* Ensure the relocation region is coverd by a PMR */

covered

Quoted 25 lines
arch/x86/boot/compressed/head_64.S +18 -0
+       movq    %rbx, %rdi
+       movl    $(_bss - startup_32), %esi
+       callq   sl_check_region
+#endif
+
 /*
  * Copy the compressed kernel to the end of our buffer
  * where decompression in place becomes safe.
@@ -462,6 +469,29 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated)
        shrq    $3, %rcx
        rep     stosq
 
+#ifdef CONFIG_SECURE_LAUNCH
+       /*
+        * Have to do the final early sl stub work in 64b area.
+        *
+        * *********** NOTE ***********
+        *
+        * Several boot params get used before we get a chance to measure
+        * them in this call. This is a known issue and we currently don't
+        * have a solution. The scratch field doesn't matter. There is no
+        * obvious way to do anything about the use of kernel_alignment or
+        * init_size though these seem low risk with all the PMR and overlap
+        * checks in place.
+        */

arch/x86/boot/compressed/head_64.S SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated) · line 494

Quoted 10 lines
arch/x86/boot/compressed/head_64.S +10 -0
+       movq    %r15, %rdi
+       callq   sl_main
+
+       /* Ensure the decompression location is covered by a PMR */
+       movq    %rbp, %rdi
+       movq    output_len(%rip), %rsi
+       callq   sl_check_region
+#endif
+
+       pushq   %rsi

This looks like a rebase error.

Quoted 8 lines
arch/x86/boot/compressed/head_64.S +0 -0
        call    load_stage2_idt
 
        /* Pass boot_params to initialize_identity_maps() */
arch/x86/boot/compressed/kernel_info.S +0 -0
index c18f07181dd5..e199b87764e9 100644

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 33

Quoted 6 lines
arch/x86/boot/compressed/kernel_info.S +3 -0
@@ -28,6 +28,40 @@ SYM_DATA_START(kernel_info)
        /* Maximal allowed type for setup_data and setup_indirect structs. */
        .long   SETUP_TYPE_MAX
 
+       /* Offset to the MLE header structure */
+#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
+       .long   rva(mle_header)

... so this could just be mle_header - kernel_info, and the consumer can do the math instead.

Quoted 16 lines
arch/x86/boot/compressed/kernel_info.S +13 -0
+#else
+       .long   0
+#endif
+
 kernel_info_var_len_data:
        /* Empty for time being... */
 SYM_DATA_END_LABEL(kernel_info, SYM_L_LOCAL, kernel_info_end)
+
+#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
+       /*
+        * The MLE Header per the TXT Specification, section 2.1
+        * MLE capabilities, see table 4. Capabilities set:
+        * bit 0: Support for GETSEC[WAKEUP] for RLP wakeup
+        * bit 1: Support for RLP wakeup using MONITOR address
+        * bit 2: The ECX register will contain the pointer to the MLE page table
+        * bit 5: TPM 1.2 family: Details/authorities PCR usage support

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 59

Quoted 10 lines
arch/x86/boot/compressed/kernel_info.S +10 -0
+        * bit 9: Supported format of TPM 2.0 event log - TCG compliant
+        */
+SYM_DATA_START(mle_header)
+       .long   0x9082ac5a  /* UUID0 */
+       .long   0x74a7476f  /* UUID1 */
+       .long   0xa2555c0f  /* UUID2 */
+       .long   0x42b651cb  /* UUID3 */
+       .long   0x00000034  /* MLE header size */
+       .long   0x00020002  /* MLE version 2.2 */
+       .long   rva(sl_stub_entry) /* Linear entry point of MLE (virt. address) */

and these should perhaps be relative to mle_header?

Quoted 8 lines
Quoted 6 lines

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 62

Quoted 3 lines
arch/x86/boot/compressed/kernel_info.S +3 -0
+       .long   0x00000000  /* First valid page of MLE */
+       .long   0x00000000  /* Offset within binary of first byte of MLE */
+       .long   rva(_edata) /* Offset within binary of last byte + 1 of MLE */

and here

Ugh never mind - these are specified externally.

OK, so instead of patch #1, please use the linker script to generate these constants.

I.e., add this to arch/x86/boot/compressed/vmlinux.lds.S

Quoted 7 lines

#ifdef CONFIG_SECURE_LAUNCH

PROVIDE(mle_header_offset       = mle_header - startup_32);
PROVIDE(sl_stub_entry_offset    = sl_stub_entry - startup_32);
PROVIDE(_edata_offset           = _edata - startup_32);

#endif

and use the symbols on the left hand side in the code.

Bah this works with Clang but not with GCC/ld.bfd

#74 lore raw

Re: [PATCH v9 08/19] x86: Secure Launch kernel early boot stub

In reply to Ard Biesheuvel
Quoted 210 lines · Ard Biesheuvel
Quoted 195 lines · Ard Biesheuvel
Quoted 193 lines · Ard Biesheuvel

Hello Ross,

Quoted 57 lines · Ross Philipson

The Secure Launch (SL) stub provides the entry point for Intel TXT (and later AMD SKINIT) to vector to during the late launch. The symbol sl_stub_entry is that entry point and its offset into the kernel is conveyed to the launching code using the MLE (Measured Launch Environment) header in the structure named mle_header. The offset of the MLE header is set in the kernel_info. The routine sl_stub contains the very early late launch setup code responsible for setting up the basic environment to allow the normal kernel startup_32 code to proceed. It is also responsible for properly waking and handling the APs on Intel platforms. The routine sl_main which runs after entering 64b mode is responsible for measuring configuration and module information before it is used like the boot params, the kernel command line, the TXT heap, an external initramfs, etc.

---
  Documentation/arch/x86/boot.rst        |  21 +
  arch/x86/boot/compressed/Makefile      |   3 +-
  arch/x86/boot/compressed/head_64.S     |  30 +
  arch/x86/boot/compressed/kernel_info.S |  34 ++
  arch/x86/boot/compressed/sl_main.c     | 577 ++++++++++++++++++++
  arch/x86/boot/compressed/sl_stub.S     | 725 +++++++++++++++++++++++++
  arch/x86/include/asm/msr-index.h       |   5 +
  arch/x86/include/uapi/asm/bootparam.h  |   1 +
  arch/x86/kernel/asm-offsets.c          |  20 +
  9 files changed, 1415 insertions(+), 1 deletion(-)

create mode 100644 arch/x86/boot/compressed/sl_main.c create mode 100644 arch/x86/boot/compressed/sl_stub.S

Documentation/arch/x86/boot.rst +12 -0
index 4fd492cb4970..295cdf9bcbdb 100644
@@ -482,6 +482,14 @@ Protocol:  2.00+
             - If 1, KASLR enabled.
             - If 0, KASLR disabled.
 
+  Bit 2 (kernel internal): SLAUNCH_FLAG
+
+       - Used internally by the setup kernel to communicate
+         Secure Launch status to kernel proper.
+
+           - If 1, Secure Launch enabled.
+           - If 0, Secure Launch disabled.
+
    Bit 5 (write): QUIET_FLAG
 
         - If 0, print early messages.
@@ -1028,6 +1036,19 @@ Offset/size:     0x000c/4
 
    This field contains maximal allowed type for setup_data and setup_indirect structs.
 
+============   =================
+Field name:    mle_header_offset
+Offset/size:   0x0010/4
+============   =================

Documentation/arch/x86/boot.rst Offset/size: 0x000c/4 · line 1052

Quoted 10 lines
Documentation/arch/x86/boot.rst +9 -0
+
+  This field contains the offset to the Secure Launch Measured Launch Environment
+  (MLE) header. This offset is used to locate information needed during a secure
+  late launch using Intel TXT. If the offset is zero, the kernel does not have
+  Secure Launch capabilities. The MLE entry point is called from TXT on the BSP
+  following a success measured launch. The specific state of the processors is
+  outlined in the TXT Software Development Guide, the latest can be found here:
+  https://urldefense.com/v3/__https://www.intel.com/content/dam/www/public/us/en/documents/guides/intel-txt-software-development-guide.pdf__;!!ACWV5N9M2RV99hQ!ItP96GzpIqxa7wGXth63mmzkWPbBgoixpG3-Gj1tlstBVkReH_hagE-Sa_E6DwcvYtu5xLOwbVWeeXGa$
+
 

Could we just repaint this field as the offset relative to the start of kernel_info rather than relative to the start of the image? That way, there is no need for patch #1, and given that the consumer of this field accesses it via kernel_info, I wouldn't expect any issues in applying this offset to obtain the actual address.

Quoted 21 lines
Documentation/arch/x86/boot.rst +0 -0
  The Image Checksum
  ==================
arch/x86/boot/compressed/Makefile +2 -1
index 9189a0e28686..9076a248d4b4 100644
@@ -118,7 +118,8 @@ vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi.o
  vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_mixed.o
  vmlinux-objs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
 
-vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o $(obj)/early_sha256.o
+vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o $(obj)/early_sha256.o \
+       $(obj)/sl_main.o $(obj)/sl_stub.o
 
  $(obj)/vmlinux: $(vmlinux-objs-y) FORCE
         $(call if_changed,ld)
arch/x86/boot/compressed/head_64.S +0 -0
index 1dcb794c5479..803c9e2e6d85 100644

arch/x86/boot/compressed/head_64.S SYM_CODE_START(startup_64) · line 424

Quoted 5 lines
arch/x86/boot/compressed/head_64.S +2 -0
@@ -420,6 +420,13 @@ SYM_CODE_START(startup_64)
         pushq   $0
         popfq
 
+#ifdef CONFIG_SECURE_LAUNCH
+       /* Ensure the relocation region is coverd by a PMR */

covered

Quoted 25 lines
arch/x86/boot/compressed/head_64.S +18 -0
+       movq    %rbx, %rdi
+       movl    $(_bss - startup_32), %esi
+       callq   sl_check_region
+#endif
+
  /*
   * Copy the compressed kernel to the end of our buffer
   * where decompression in place becomes safe.
@@ -462,6 +469,29 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated)
         shrq    $3, %rcx
         rep     stosq
 
+#ifdef CONFIG_SECURE_LAUNCH
+       /*
+        * Have to do the final early sl stub work in 64b area.
+        *
+        * *********** NOTE ***********
+        *
+        * Several boot params get used before we get a chance to measure
+        * them in this call. This is a known issue and we currently don't
+        * have a solution. The scratch field doesn't matter. There is no
+        * obvious way to do anything about the use of kernel_alignment or
+        * init_size though these seem low risk with all the PMR and overlap
+        * checks in place.
+        */

arch/x86/boot/compressed/head_64.S SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated) · line 494

Quoted 10 lines
arch/x86/boot/compressed/head_64.S +10 -0
+       movq    %r15, %rdi
+       callq   sl_main
+
+       /* Ensure the decompression location is covered by a PMR */
+       movq    %rbp, %rdi
+       movq    output_len(%rip), %rsi
+       callq   sl_check_region
+#endif
+
+       pushq   %rsi

This looks like a rebase error.

Quoted 8 lines
arch/x86/boot/compressed/head_64.S +0 -0
         call    load_stage2_idt
 
         /* Pass boot_params to initialize_identity_maps() */
arch/x86/boot/compressed/kernel_info.S +0 -0
index c18f07181dd5..e199b87764e9 100644

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 33

Quoted 6 lines
arch/x86/boot/compressed/kernel_info.S +3 -0
@@ -28,6 +28,40 @@ SYM_DATA_START(kernel_info)
         /* Maximal allowed type for setup_data and setup_indirect structs. */
         .long   SETUP_TYPE_MAX
 
+       /* Offset to the MLE header structure */
+#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
+       .long   rva(mle_header)

... so this could just be mle_header - kernel_info, and the consumer can do the math instead.

Quoted 16 lines
arch/x86/boot/compressed/kernel_info.S +13 -0
+#else
+       .long   0
+#endif
+
  kernel_info_var_len_data:
         /* Empty for time being... */
  SYM_DATA_END_LABEL(kernel_info, SYM_L_LOCAL, kernel_info_end)
+
+#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
+       /*
+        * The MLE Header per the TXT Specification, section 2.1
+        * MLE capabilities, see table 4. Capabilities set:
+        * bit 0: Support for GETSEC[WAKEUP] for RLP wakeup
+        * bit 1: Support for RLP wakeup using MONITOR address
+        * bit 2: The ECX register will contain the pointer to the MLE page table
+        * bit 5: TPM 1.2 family: Details/authorities PCR usage support

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 59

Quoted 10 lines
arch/x86/boot/compressed/kernel_info.S +10 -0
+        * bit 9: Supported format of TPM 2.0 event log - TCG compliant
+        */
+SYM_DATA_START(mle_header)
+       .long   0x9082ac5a  /* UUID0 */
+       .long   0x74a7476f  /* UUID1 */
+       .long   0xa2555c0f  /* UUID2 */
+       .long   0x42b651cb  /* UUID3 */
+       .long   0x00000034  /* MLE header size */
+       .long   0x00020002  /* MLE version 2.2 */
+       .long   rva(sl_stub_entry) /* Linear entry point of MLE (virt. address) */

and these should perhaps be relative to mle_header?

Quoted 8 lines
Quoted 6 lines

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 62

Quoted 3 lines
arch/x86/boot/compressed/kernel_info.S +3 -0
+       .long   0x00000000  /* First valid page of MLE */
+       .long   0x00000000  /* Offset within binary of first byte of MLE */
+       .long   rva(_edata) /* Offset within binary of last byte + 1 of MLE */

and here

Ugh never mind - these are specified externally.

OK, so instead of patch #1, please use the linker script to generate these constants.

I.e., add this to arch/x86/boot/compressed/vmlinux.lds.S

Quoted 7 lines

#ifdef CONFIG_SECURE_LAUNCH

PROVIDE(mle_header_offset       = mle_header - startup_32);
PROVIDE(sl_stub_entry_offset    = sl_stub_entry - startup_32);
PROVIDE(_edata_offset           = _edata - startup_32);

#endif

and use the symbols on the left hand side in the code.

Hmmm that is an interesting approach we had not considered but we surely will now. We are not wedded to keeping patch #1 by any means. Thank you for your suggestions.

Ross

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#75 lore raw

Re: [PATCH v9 08/19] x86: Secure Launch kernel early boot stub

In reply to Ard Biesheuvel
Quoted 194 lines · Ard Biesheuvel
Quoted 193 lines · Ard Biesheuvel

Hello Ross,

Quoted 57 lines · Ross Philipson

The Secure Launch (SL) stub provides the entry point for Intel TXT (and later AMD SKINIT) to vector to during the late launch. The symbol sl_stub_entry is that entry point and its offset into the kernel is conveyed to the launching code using the MLE (Measured Launch Environment) header in the structure named mle_header. The offset of the MLE header is set in the kernel_info. The routine sl_stub contains the very early late launch setup code responsible for setting up the basic environment to allow the normal kernel startup_32 code to proceed. It is also responsible for properly waking and handling the APs on Intel platforms. The routine sl_main which runs after entering 64b mode is responsible for measuring configuration and module information before it is used like the boot params, the kernel command line, the TXT heap, an external initramfs, etc.

---
  Documentation/arch/x86/boot.rst        |  21 +
  arch/x86/boot/compressed/Makefile      |   3 +-
  arch/x86/boot/compressed/head_64.S     |  30 +
  arch/x86/boot/compressed/kernel_info.S |  34 ++
  arch/x86/boot/compressed/sl_main.c     | 577 ++++++++++++++++++++
  arch/x86/boot/compressed/sl_stub.S     | 725 +++++++++++++++++++++++++
  arch/x86/include/asm/msr-index.h       |   5 +
  arch/x86/include/uapi/asm/bootparam.h  |   1 +
  arch/x86/kernel/asm-offsets.c          |  20 +
  9 files changed, 1415 insertions(+), 1 deletion(-)

create mode 100644 arch/x86/boot/compressed/sl_main.c create mode 100644 arch/x86/boot/compressed/sl_stub.S

Documentation/arch/x86/boot.rst +12 -0
index 4fd492cb4970..295cdf9bcbdb 100644
@@ -482,6 +482,14 @@ Protocol:  2.00+
             - If 1, KASLR enabled.
             - If 0, KASLR disabled.
 
+  Bit 2 (kernel internal): SLAUNCH_FLAG
+
+       - Used internally by the setup kernel to communicate
+         Secure Launch status to kernel proper.
+
+           - If 1, Secure Launch enabled.
+           - If 0, Secure Launch disabled.
+
    Bit 5 (write): QUIET_FLAG
 
         - If 0, print early messages.
@@ -1028,6 +1036,19 @@ Offset/size:     0x000c/4
 
    This field contains maximal allowed type for setup_data and setup_indirect structs.
 
+============   =================
+Field name:    mle_header_offset
+Offset/size:   0x0010/4
+============   =================

Documentation/arch/x86/boot.rst Offset/size: 0x000c/4 · line 1052

Quoted 10 lines
Documentation/arch/x86/boot.rst +9 -0
+
+  This field contains the offset to the Secure Launch Measured Launch Environment
+  (MLE) header. This offset is used to locate information needed during a secure
+  late launch using Intel TXT. If the offset is zero, the kernel does not have
+  Secure Launch capabilities. The MLE entry point is called from TXT on the BSP
+  following a success measured launch. The specific state of the processors is
+  outlined in the TXT Software Development Guide, the latest can be found here:
+  https://urldefense.com/v3/__https://www.intel.com/content/dam/www/public/us/en/documents/guides/intel-txt-software-development-guide.pdf__;!!ACWV5N9M2RV99hQ!Mng0gnPhOYZ8D02t1rYwQfY6U3uWaypJyd1T2rsWz3QNHr9GhIZ9ANB_-cgPExxX0e0KmCpda-3VX8Fj$
+
 

Could we just repaint this field as the offset relative to the start of kernel_info rather than relative to the start of the image? That way, there is no need for patch #1, and given that the consumer of this field accesses it via kernel_info, I wouldn't expect any issues in applying this offset to obtain the actual address.

Quoted 21 lines
Documentation/arch/x86/boot.rst +0 -0
  The Image Checksum
  ==================
arch/x86/boot/compressed/Makefile +2 -1
index 9189a0e28686..9076a248d4b4 100644
@@ -118,7 +118,8 @@ vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi.o
  vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_mixed.o
  vmlinux-objs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
 
-vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o $(obj)/early_sha256.o
+vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o $(obj)/early_sha256.o \
+       $(obj)/sl_main.o $(obj)/sl_stub.o
 
  $(obj)/vmlinux: $(vmlinux-objs-y) FORCE
         $(call if_changed,ld)
arch/x86/boot/compressed/head_64.S +0 -0
index 1dcb794c5479..803c9e2e6d85 100644

arch/x86/boot/compressed/head_64.S SYM_CODE_START(startup_64) · line 424

Quoted 5 lines
arch/x86/boot/compressed/head_64.S +2 -0
@@ -420,6 +420,13 @@ SYM_CODE_START(startup_64)
         pushq   $0
         popfq
 
+#ifdef CONFIG_SECURE_LAUNCH
+       /* Ensure the relocation region is coverd by a PMR */

covered

Quoted 25 lines
arch/x86/boot/compressed/head_64.S +18 -0
+       movq    %rbx, %rdi
+       movl    $(_bss - startup_32), %esi
+       callq   sl_check_region
+#endif
+
  /*
   * Copy the compressed kernel to the end of our buffer
   * where decompression in place becomes safe.
@@ -462,6 +469,29 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated)
         shrq    $3, %rcx
         rep     stosq
 
+#ifdef CONFIG_SECURE_LAUNCH
+       /*
+        * Have to do the final early sl stub work in 64b area.
+        *
+        * *********** NOTE ***********
+        *
+        * Several boot params get used before we get a chance to measure
+        * them in this call. This is a known issue and we currently don't
+        * have a solution. The scratch field doesn't matter. There is no
+        * obvious way to do anything about the use of kernel_alignment or
+        * init_size though these seem low risk with all the PMR and overlap
+        * checks in place.
+        */

arch/x86/boot/compressed/head_64.S SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated) · line 494

Quoted 10 lines
arch/x86/boot/compressed/head_64.S +10 -0
+       movq    %r15, %rdi
+       callq   sl_main
+
+       /* Ensure the decompression location is covered by a PMR */
+       movq    %rbp, %rdi
+       movq    output_len(%rip), %rsi
+       callq   sl_check_region
+#endif
+
+       pushq   %rsi

This looks like a rebase error.

Quoted 8 lines
arch/x86/boot/compressed/head_64.S +0 -0
         call    load_stage2_idt
 
         /* Pass boot_params to initialize_identity_maps() */
arch/x86/boot/compressed/kernel_info.S +0 -0
index c18f07181dd5..e199b87764e9 100644

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 33

Quoted 6 lines
arch/x86/boot/compressed/kernel_info.S +3 -0
@@ -28,6 +28,40 @@ SYM_DATA_START(kernel_info)
         /* Maximal allowed type for setup_data and setup_indirect structs. */
         .long   SETUP_TYPE_MAX
 
+       /* Offset to the MLE header structure */
+#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
+       .long   rva(mle_header)

... so this could just be mle_header - kernel_info, and the consumer can do the math instead.

Quoted 16 lines
arch/x86/boot/compressed/kernel_info.S +13 -0
+#else
+       .long   0
+#endif
+
  kernel_info_var_len_data:
         /* Empty for time being... */
  SYM_DATA_END_LABEL(kernel_info, SYM_L_LOCAL, kernel_info_end)
+
+#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
+       /*
+        * The MLE Header per the TXT Specification, section 2.1
+        * MLE capabilities, see table 4. Capabilities set:
+        * bit 0: Support for GETSEC[WAKEUP] for RLP wakeup
+        * bit 1: Support for RLP wakeup using MONITOR address
+        * bit 2: The ECX register will contain the pointer to the MLE page table
+        * bit 5: TPM 1.2 family: Details/authorities PCR usage support

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 59

Quoted 10 lines
arch/x86/boot/compressed/kernel_info.S +10 -0
+        * bit 9: Supported format of TPM 2.0 event log - TCG compliant
+        */
+SYM_DATA_START(mle_header)
+       .long   0x9082ac5a  /* UUID0 */
+       .long   0x74a7476f  /* UUID1 */
+       .long   0xa2555c0f  /* UUID2 */
+       .long   0x42b651cb  /* UUID3 */
+       .long   0x00000034  /* MLE header size */
+       .long   0x00020002  /* MLE version 2.2 */
+       .long   rva(sl_stub_entry) /* Linear entry point of MLE (virt. address) */

and these should perhaps be relative to mle_header?

Quoted 8 lines
Quoted 6 lines

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 62

Quoted 3 lines
arch/x86/boot/compressed/kernel_info.S +3 -0
+       .long   0x00000000  /* First valid page of MLE */
+       .long   0x00000000  /* Offset within binary of first byte of MLE */
+       .long   rva(_edata) /* Offset within binary of last byte + 1 of MLE */

and here

Ugh never mind - these are specified externally.

Can you clarify your follow on comment here?

Thank you,
Ross

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

maintainer
#76 lore raw

Re: [PATCH v9 08/19] x86: Secure Launch kernel early boot stub

Quoted 200 lines · On Tue, 4 Jun 2024 at 19:24, <[email protected]> wrote:
Quoted 198 lines · Ard Biesheuvel
Quoted 197 lines · Ard Biesheuvel

Hello Ross,

Quoted 57 lines · Ross Philipson

The Secure Launch (SL) stub provides the entry point for Intel TXT (and later AMD SKINIT) to vector to during the late launch. The symbol sl_stub_entry is that entry point and its offset into the kernel is conveyed to the launching code using the MLE (Measured Launch Environment) header in the structure named mle_header. The offset of the MLE header is set in the kernel_info. The routine sl_stub contains the very early late launch setup code responsible for setting up the basic environment to allow the normal kernel startup_32 code to proceed. It is also responsible for properly waking and handling the APs on Intel platforms. The routine sl_main which runs after entering 64b mode is responsible for measuring configuration and module information before it is used like the boot params, the kernel command line, the TXT heap, an external initramfs, etc.

---
  Documentation/arch/x86/boot.rst        |  21 +
  arch/x86/boot/compressed/Makefile      |   3 +-
  arch/x86/boot/compressed/head_64.S     |  30 +
  arch/x86/boot/compressed/kernel_info.S |  34 ++
  arch/x86/boot/compressed/sl_main.c     | 577 ++++++++++++++++++++
  arch/x86/boot/compressed/sl_stub.S     | 725 +++++++++++++++++++++++++
  arch/x86/include/asm/msr-index.h       |   5 +
  arch/x86/include/uapi/asm/bootparam.h  |   1 +
  arch/x86/kernel/asm-offsets.c          |  20 +
  9 files changed, 1415 insertions(+), 1 deletion(-)

create mode 100644 arch/x86/boot/compressed/sl_main.c create mode 100644 arch/x86/boot/compressed/sl_stub.S

Documentation/arch/x86/boot.rst +12 -0
index 4fd492cb4970..295cdf9bcbdb 100644
@@ -482,6 +482,14 @@ Protocol:  2.00+
             - If 1, KASLR enabled.
             - If 0, KASLR disabled.
 
+  Bit 2 (kernel internal): SLAUNCH_FLAG
+
+       - Used internally by the setup kernel to communicate
+         Secure Launch status to kernel proper.
+
+           - If 1, Secure Launch enabled.
+           - If 0, Secure Launch disabled.
+
    Bit 5 (write): QUIET_FLAG
 
         - If 0, print early messages.
@@ -1028,6 +1036,19 @@ Offset/size:     0x000c/4
 
    This field contains maximal allowed type for setup_data and setup_indirect structs.
 
+============   =================
+Field name:    mle_header_offset
+Offset/size:   0x0010/4
+============   =================

Documentation/arch/x86/boot.rst Offset/size: 0x000c/4 · line 1052

Quoted 10 lines
Documentation/arch/x86/boot.rst +9 -0
+
+  This field contains the offset to the Secure Launch Measured Launch Environment
+  (MLE) header. This offset is used to locate information needed during a secure
+  late launch using Intel TXT. If the offset is zero, the kernel does not have
+  Secure Launch capabilities. The MLE entry point is called from TXT on the BSP
+  following a success measured launch. The specific state of the processors is
+  outlined in the TXT Software Development Guide, the latest can be found here:
+  https://urldefense.com/v3/__https://www.intel.com/content/dam/www/public/us/en/documents/guides/intel-txt-software-development-guide.pdf__;!!ACWV5N9M2RV99hQ!Mng0gnPhOYZ8D02t1rYwQfY6U3uWaypJyd1T2rsWz3QNHr9GhIZ9ANB_-cgPExxX0e0KmCpda-3VX8Fj$
+
 

Could we just repaint this field as the offset relative to the start of kernel_info rather than relative to the start of the image? That way, there is no need for patch #1, and given that the consumer of this field accesses it via kernel_info, I wouldn't expect any issues in applying this offset to obtain the actual address.

Quoted 21 lines
Documentation/arch/x86/boot.rst +0 -0
  The Image Checksum
  ==================
arch/x86/boot/compressed/Makefile +2 -1
index 9189a0e28686..9076a248d4b4 100644
@@ -118,7 +118,8 @@ vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi.o
  vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_mixed.o
  vmlinux-objs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
 
-vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o $(obj)/early_sha256.o
+vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o $(obj)/early_sha256.o \
+       $(obj)/sl_main.o $(obj)/sl_stub.o
 
  $(obj)/vmlinux: $(vmlinux-objs-y) FORCE
         $(call if_changed,ld)
arch/x86/boot/compressed/head_64.S +0 -0
index 1dcb794c5479..803c9e2e6d85 100644

arch/x86/boot/compressed/head_64.S SYM_CODE_START(startup_64) · line 424

Quoted 5 lines
arch/x86/boot/compressed/head_64.S +2 -0
@@ -420,6 +420,13 @@ SYM_CODE_START(startup_64)
         pushq   $0
         popfq
 
+#ifdef CONFIG_SECURE_LAUNCH
+       /* Ensure the relocation region is coverd by a PMR */

covered

Quoted 25 lines
arch/x86/boot/compressed/head_64.S +18 -0
+       movq    %rbx, %rdi
+       movl    $(_bss - startup_32), %esi
+       callq   sl_check_region
+#endif
+
  /*
   * Copy the compressed kernel to the end of our buffer
   * where decompression in place becomes safe.
@@ -462,6 +469,29 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated)
         shrq    $3, %rcx
         rep     stosq
 
+#ifdef CONFIG_SECURE_LAUNCH
+       /*
+        * Have to do the final early sl stub work in 64b area.
+        *
+        * *********** NOTE ***********
+        *
+        * Several boot params get used before we get a chance to measure
+        * them in this call. This is a known issue and we currently don't
+        * have a solution. The scratch field doesn't matter. There is no
+        * obvious way to do anything about the use of kernel_alignment or
+        * init_size though these seem low risk with all the PMR and overlap
+        * checks in place.
+        */

arch/x86/boot/compressed/head_64.S SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated) · line 494

Quoted 10 lines
arch/x86/boot/compressed/head_64.S +10 -0
+       movq    %r15, %rdi
+       callq   sl_main
+
+       /* Ensure the decompression location is covered by a PMR */
+       movq    %rbp, %rdi
+       movq    output_len(%rip), %rsi
+       callq   sl_check_region
+#endif
+
+       pushq   %rsi

This looks like a rebase error.

Quoted 8 lines
arch/x86/boot/compressed/head_64.S +0 -0
         call    load_stage2_idt
 
         /* Pass boot_params to initialize_identity_maps() */
arch/x86/boot/compressed/kernel_info.S +0 -0
index c18f07181dd5..e199b87764e9 100644

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 33

Quoted 6 lines
arch/x86/boot/compressed/kernel_info.S +3 -0
@@ -28,6 +28,40 @@ SYM_DATA_START(kernel_info)
         /* Maximal allowed type for setup_data and setup_indirect structs. */
         .long   SETUP_TYPE_MAX
 
+       /* Offset to the MLE header structure */
+#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
+       .long   rva(mle_header)

... so this could just be mle_header - kernel_info, and the consumer can do the math instead.

Quoted 16 lines
arch/x86/boot/compressed/kernel_info.S +13 -0
+#else
+       .long   0
+#endif
+
  kernel_info_var_len_data:
         /* Empty for time being... */
  SYM_DATA_END_LABEL(kernel_info, SYM_L_LOCAL, kernel_info_end)
+
+#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
+       /*
+        * The MLE Header per the TXT Specification, section 2.1
+        * MLE capabilities, see table 4. Capabilities set:
+        * bit 0: Support for GETSEC[WAKEUP] for RLP wakeup
+        * bit 1: Support for RLP wakeup using MONITOR address
+        * bit 2: The ECX register will contain the pointer to the MLE page table
+        * bit 5: TPM 1.2 family: Details/authorities PCR usage support

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 59

Quoted 10 lines
arch/x86/boot/compressed/kernel_info.S +10 -0
+        * bit 9: Supported format of TPM 2.0 event log - TCG compliant
+        */
+SYM_DATA_START(mle_header)
+       .long   0x9082ac5a  /* UUID0 */
+       .long   0x74a7476f  /* UUID1 */
+       .long   0xa2555c0f  /* UUID2 */
+       .long   0x42b651cb  /* UUID3 */
+       .long   0x00000034  /* MLE header size */
+       .long   0x00020002  /* MLE version 2.2 */
+       .long   rva(sl_stub_entry) /* Linear entry point of MLE (virt. address) */

and these should perhaps be relative to mle_header?

Quoted 3 lines
arch/x86/boot/compressed/kernel_info.S +3 -0
+       .long   0x00000000  /* First valid page of MLE */
+       .long   0x00000000  /* Offset within binary of first byte of MLE */
+       .long   rva(_edata) /* Offset within binary of last byte + 1 of MLE */
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

and here

Ugh never mind - these are specified externally.

Can you clarify your follow on comment here?

I noticed that -as you pointed out in your previous reply- these fields cannot be repainted at will, as they are defined by an external specification.

I'll play a bit more with this code tomorrow - I would *really* like to avoid the need for patch #1, as it adds another constraint on how we construct the boot image, and this is already riddled with legacy and other complications.

#77 lore raw

Re: [PATCH v9 08/19] x86: Secure Launch kernel early boot stub

Quoted 209 lines · Ard Biesheuvel
Quoted 200 lines · On Tue, 4 Jun 2024 at 19:24, <[email protected]> wrote:
Quoted 198 lines · Ard Biesheuvel
Quoted 197 lines · Ard Biesheuvel

Hello Ross,

Quoted 57 lines · Ross Philipson

The Secure Launch (SL) stub provides the entry point for Intel TXT (and later AMD SKINIT) to vector to during the late launch. The symbol sl_stub_entry is that entry point and its offset into the kernel is conveyed to the launching code using the MLE (Measured Launch Environment) header in the structure named mle_header. The offset of the MLE header is set in the kernel_info. The routine sl_stub contains the very early late launch setup code responsible for setting up the basic environment to allow the normal kernel startup_32 code to proceed. It is also responsible for properly waking and handling the APs on Intel platforms. The routine sl_main which runs after entering 64b mode is responsible for measuring configuration and module information before it is used like the boot params, the kernel command line, the TXT heap, an external initramfs, etc.

---
   Documentation/arch/x86/boot.rst        |  21 +
   arch/x86/boot/compressed/Makefile      |   3 +-
   arch/x86/boot/compressed/head_64.S     |  30 +
   arch/x86/boot/compressed/kernel_info.S |  34 ++
   arch/x86/boot/compressed/sl_main.c     | 577 ++++++++++++++++++++
   arch/x86/boot/compressed/sl_stub.S     | 725 +++++++++++++++++++++++++
   arch/x86/include/asm/msr-index.h       |   5 +
   arch/x86/include/uapi/asm/bootparam.h  |   1 +
   arch/x86/kernel/asm-offsets.c          |  20 +
   9 files changed, 1415 insertions(+), 1 deletion(-)

create mode 100644 arch/x86/boot/compressed/sl_main.c create mode 100644 arch/x86/boot/compressed/sl_stub.S

Documentation/arch/x86/boot.rst +12 -0
index 4fd492cb4970..295cdf9bcbdb 100644
@@ -482,6 +482,14 @@ Protocol:  2.00+
              - If 1, KASLR enabled.
              - If 0, KASLR disabled.
 
+  Bit 2 (kernel internal): SLAUNCH_FLAG
+
+       - Used internally by the setup kernel to communicate
+         Secure Launch status to kernel proper.
+
+           - If 1, Secure Launch enabled.
+           - If 0, Secure Launch disabled.
+
     Bit 5 (write): QUIET_FLAG
 
          - If 0, print early messages.
@@ -1028,6 +1036,19 @@ Offset/size:     0x000c/4
 
     This field contains maximal allowed type for setup_data and setup_indirect structs.
 
+============   =================
+Field name:    mle_header_offset
+Offset/size:   0x0010/4
+============   =================

Documentation/arch/x86/boot.rst Offset/size: 0x000c/4 · line 1052

Quoted 10 lines
Documentation/arch/x86/boot.rst +9 -0
+
+  This field contains the offset to the Secure Launch Measured Launch Environment
+  (MLE) header. This offset is used to locate information needed during a secure
+  late launch using Intel TXT. If the offset is zero, the kernel does not have
+  Secure Launch capabilities. The MLE entry point is called from TXT on the BSP
+  following a success measured launch. The specific state of the processors is
+  outlined in the TXT Software Development Guide, the latest can be found here:
+  https://urldefense.com/v3/__https://www.intel.com/content/dam/www/public/us/en/documents/guides/intel-txt-software-development-guide.pdf__;!!ACWV5N9M2RV99hQ!Mng0gnPhOYZ8D02t1rYwQfY6U3uWaypJyd1T2rsWz3QNHr9GhIZ9ANB_-cgPExxX0e0KmCpda-3VX8Fj$
+
 

Could we just repaint this field as the offset relative to the start of kernel_info rather than relative to the start of the image? That way, there is no need for patch #1, and given that the consumer of this field accesses it via kernel_info, I wouldn't expect any issues in applying this offset to obtain the actual address.

Quoted 21 lines
Documentation/arch/x86/boot.rst +0 -0
   The Image Checksum
   ==================
arch/x86/boot/compressed/Makefile +2 -1
index 9189a0e28686..9076a248d4b4 100644
@@ -118,7 +118,8 @@ vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi.o
   vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_mixed.o
   vmlinux-objs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
 
-vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o $(obj)/early_sha256.o
+vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o $(obj)/early_sha256.o \
+       $(obj)/sl_main.o $(obj)/sl_stub.o
 
   $(obj)/vmlinux: $(vmlinux-objs-y) FORCE
          $(call if_changed,ld)
arch/x86/boot/compressed/head_64.S +0 -0
index 1dcb794c5479..803c9e2e6d85 100644

arch/x86/boot/compressed/head_64.S SYM_CODE_START(startup_64) · line 424

Quoted 5 lines
arch/x86/boot/compressed/head_64.S +2 -0
@@ -420,6 +420,13 @@ SYM_CODE_START(startup_64)
          pushq   $0
          popfq
 
+#ifdef CONFIG_SECURE_LAUNCH
+       /* Ensure the relocation region is coverd by a PMR */

covered

Quoted 25 lines
arch/x86/boot/compressed/head_64.S +18 -0
+       movq    %rbx, %rdi
+       movl    $(_bss - startup_32), %esi
+       callq   sl_check_region
+#endif
+
   /*
    * Copy the compressed kernel to the end of our buffer
    * where decompression in place becomes safe.
@@ -462,6 +469,29 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated)
          shrq    $3, %rcx
          rep     stosq
 
+#ifdef CONFIG_SECURE_LAUNCH
+       /*
+        * Have to do the final early sl stub work in 64b area.
+        *
+        * *********** NOTE ***********
+        *
+        * Several boot params get used before we get a chance to measure
+        * them in this call. This is a known issue and we currently don't
+        * have a solution. The scratch field doesn't matter. There is no
+        * obvious way to do anything about the use of kernel_alignment or
+        * init_size though these seem low risk with all the PMR and overlap
+        * checks in place.
+        */

arch/x86/boot/compressed/head_64.S SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated) · line 494

Quoted 10 lines
arch/x86/boot/compressed/head_64.S +10 -0
+       movq    %r15, %rdi
+       callq   sl_main
+
+       /* Ensure the decompression location is covered by a PMR */
+       movq    %rbp, %rdi
+       movq    output_len(%rip), %rsi
+       callq   sl_check_region
+#endif
+
+       pushq   %rsi

This looks like a rebase error.

Quoted 8 lines
arch/x86/boot/compressed/head_64.S +0 -0
          call    load_stage2_idt
 
          /* Pass boot_params to initialize_identity_maps() */
arch/x86/boot/compressed/kernel_info.S +0 -0
index c18f07181dd5..e199b87764e9 100644

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 33

Quoted 6 lines
arch/x86/boot/compressed/kernel_info.S +3 -0
@@ -28,6 +28,40 @@ SYM_DATA_START(kernel_info)
          /* Maximal allowed type for setup_data and setup_indirect structs. */
          .long   SETUP_TYPE_MAX
 
+       /* Offset to the MLE header structure */
+#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
+       .long   rva(mle_header)

... so this could just be mle_header - kernel_info, and the consumer can do the math instead.

Quoted 16 lines
arch/x86/boot/compressed/kernel_info.S +13 -0
+#else
+       .long   0
+#endif
+
   kernel_info_var_len_data:
          /* Empty for time being... */
   SYM_DATA_END_LABEL(kernel_info, SYM_L_LOCAL, kernel_info_end)
+
+#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
+       /*
+        * The MLE Header per the TXT Specification, section 2.1
+        * MLE capabilities, see table 4. Capabilities set:
+        * bit 0: Support for GETSEC[WAKEUP] for RLP wakeup
+        * bit 1: Support for RLP wakeup using MONITOR address
+        * bit 2: The ECX register will contain the pointer to the MLE page table
+        * bit 5: TPM 1.2 family: Details/authorities PCR usage support

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 59

Quoted 10 lines
arch/x86/boot/compressed/kernel_info.S +10 -0
+        * bit 9: Supported format of TPM 2.0 event log - TCG compliant
+        */
+SYM_DATA_START(mle_header)
+       .long   0x9082ac5a  /* UUID0 */
+       .long   0x74a7476f  /* UUID1 */
+       .long   0xa2555c0f  /* UUID2 */
+       .long   0x42b651cb  /* UUID3 */
+       .long   0x00000034  /* MLE header size */
+       .long   0x00020002  /* MLE version 2.2 */
+       .long   rva(sl_stub_entry) /* Linear entry point of MLE (virt. address) */

and these should perhaps be relative to mle_header?

Quoted 3 lines
arch/x86/boot/compressed/kernel_info.S +3 -0
+       .long   0x00000000  /* First valid page of MLE */
+       .long   0x00000000  /* Offset within binary of first byte of MLE */
+       .long   rva(_edata) /* Offset within binary of last byte + 1 of MLE */
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

and here

Ugh never mind - these are specified externally.

Can you clarify your follow on comment here?

Quoted 8 lines

I noticed that -as you pointed out in your previous reply- these fields cannot be repainted at will, as they are defined by an external specification.

I'll play a bit more with this code tomorrow - I would *really* like to avoid the need for patch #1, as it adds another constraint on how we construct the boot image, and this is already riddled with legacy and other complications.

Yea I should have read forward through all your replies before responding to the first one but I think it clarified things as you point out here. We appreciate you help and suggestions.

Ross

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

maintainer
#78 lore raw

Re: [PATCH v9 08/19] x86: Secure Launch kernel early boot stub

Quoted 215 lines · On Tue, 4 Jun 2024 at 19:34, <[email protected]> wrote:
Quoted 213 lines · Ard Biesheuvel
Quoted 200 lines · On Tue, 4 Jun 2024 at 19:24, <[email protected]> wrote:
Quoted 198 lines · Ard Biesheuvel
Quoted 197 lines · Ard Biesheuvel

Hello Ross,

Quoted 57 lines · Ross Philipson

The Secure Launch (SL) stub provides the entry point for Intel TXT (and later AMD SKINIT) to vector to during the late launch. The symbol sl_stub_entry is that entry point and its offset into the kernel is conveyed to the launching code using the MLE (Measured Launch Environment) header in the structure named mle_header. The offset of the MLE header is set in the kernel_info. The routine sl_stub contains the very early late launch setup code responsible for setting up the basic environment to allow the normal kernel startup_32 code to proceed. It is also responsible for properly waking and handling the APs on Intel platforms. The routine sl_main which runs after entering 64b mode is responsible for measuring configuration and module information before it is used like the boot params, the kernel command line, the TXT heap, an external initramfs, etc.

---
   Documentation/arch/x86/boot.rst        |  21 +
   arch/x86/boot/compressed/Makefile      |   3 +-
   arch/x86/boot/compressed/head_64.S     |  30 +
   arch/x86/boot/compressed/kernel_info.S |  34 ++
   arch/x86/boot/compressed/sl_main.c     | 577 ++++++++++++++++++++
   arch/x86/boot/compressed/sl_stub.S     | 725 +++++++++++++++++++++++++
   arch/x86/include/asm/msr-index.h       |   5 +
   arch/x86/include/uapi/asm/bootparam.h  |   1 +
   arch/x86/kernel/asm-offsets.c          |  20 +
   9 files changed, 1415 insertions(+), 1 deletion(-)

create mode 100644 arch/x86/boot/compressed/sl_main.c create mode 100644 arch/x86/boot/compressed/sl_stub.S

Documentation/arch/x86/boot.rst +12 -0
index 4fd492cb4970..295cdf9bcbdb 100644
@@ -482,6 +482,14 @@ Protocol:  2.00+
              - If 1, KASLR enabled.
              - If 0, KASLR disabled.
 
+  Bit 2 (kernel internal): SLAUNCH_FLAG
+
+       - Used internally by the setup kernel to communicate
+         Secure Launch status to kernel proper.
+
+           - If 1, Secure Launch enabled.
+           - If 0, Secure Launch disabled.
+
     Bit 5 (write): QUIET_FLAG
 
          - If 0, print early messages.
@@ -1028,6 +1036,19 @@ Offset/size:     0x000c/4
 
     This field contains maximal allowed type for setup_data and setup_indirect structs.
 
+============   =================
+Field name:    mle_header_offset
+Offset/size:   0x0010/4
+============   =================

Documentation/arch/x86/boot.rst Offset/size: 0x000c/4 · line 1052

Quoted 10 lines
Documentation/arch/x86/boot.rst +9 -0
+
+  This field contains the offset to the Secure Launch Measured Launch Environment
+  (MLE) header. This offset is used to locate information needed during a secure
+  late launch using Intel TXT. If the offset is zero, the kernel does not have
+  Secure Launch capabilities. The MLE entry point is called from TXT on the BSP
+  following a success measured launch. The specific state of the processors is
+  outlined in the TXT Software Development Guide, the latest can be found here:
+  https://urldefense.com/v3/__https://www.intel.com/content/dam/www/public/us/en/documents/guides/intel-txt-software-development-guide.pdf__;!!ACWV5N9M2RV99hQ!Mng0gnPhOYZ8D02t1rYwQfY6U3uWaypJyd1T2rsWz3QNHr9GhIZ9ANB_-cgPExxX0e0KmCpda-3VX8Fj$
+
 

Could we just repaint this field as the offset relative to the start of kernel_info rather than relative to the start of the image? That way, there is no need for patch #1, and given that the consumer of this field accesses it via kernel_info, I wouldn't expect any issues in applying this offset to obtain the actual address.

Quoted 21 lines
Documentation/arch/x86/boot.rst +0 -0
   The Image Checksum
   ==================
arch/x86/boot/compressed/Makefile +2 -1
index 9189a0e28686..9076a248d4b4 100644
@@ -118,7 +118,8 @@ vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi.o
   vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_mixed.o
   vmlinux-objs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
 
-vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o $(obj)/early_sha256.o
+vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o $(obj)/early_sha256.o \
+       $(obj)/sl_main.o $(obj)/sl_stub.o
 
   $(obj)/vmlinux: $(vmlinux-objs-y) FORCE
          $(call if_changed,ld)
arch/x86/boot/compressed/head_64.S +0 -0
index 1dcb794c5479..803c9e2e6d85 100644

arch/x86/boot/compressed/head_64.S SYM_CODE_START(startup_64) · line 424

Quoted 5 lines
arch/x86/boot/compressed/head_64.S +2 -0
@@ -420,6 +420,13 @@ SYM_CODE_START(startup_64)
          pushq   $0
          popfq
 
+#ifdef CONFIG_SECURE_LAUNCH
+       /* Ensure the relocation region is coverd by a PMR */

covered

Quoted 25 lines
arch/x86/boot/compressed/head_64.S +18 -0
+       movq    %rbx, %rdi
+       movl    $(_bss - startup_32), %esi
+       callq   sl_check_region
+#endif
+
   /*
    * Copy the compressed kernel to the end of our buffer
    * where decompression in place becomes safe.
@@ -462,6 +469,29 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated)
          shrq    $3, %rcx
          rep     stosq
 
+#ifdef CONFIG_SECURE_LAUNCH
+       /*
+        * Have to do the final early sl stub work in 64b area.
+        *
+        * *********** NOTE ***********
+        *
+        * Several boot params get used before we get a chance to measure
+        * them in this call. This is a known issue and we currently don't
+        * have a solution. The scratch field doesn't matter. There is no
+        * obvious way to do anything about the use of kernel_alignment or
+        * init_size though these seem low risk with all the PMR and overlap
+        * checks in place.
+        */

arch/x86/boot/compressed/head_64.S SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated) · line 494

Quoted 10 lines
arch/x86/boot/compressed/head_64.S +10 -0
+       movq    %r15, %rdi
+       callq   sl_main
+
+       /* Ensure the decompression location is covered by a PMR */
+       movq    %rbp, %rdi
+       movq    output_len(%rip), %rsi
+       callq   sl_check_region
+#endif
+
+       pushq   %rsi

This looks like a rebase error.

Quoted 8 lines
arch/x86/boot/compressed/head_64.S +0 -0
          call    load_stage2_idt
 
          /* Pass boot_params to initialize_identity_maps() */
arch/x86/boot/compressed/kernel_info.S +0 -0
index c18f07181dd5..e199b87764e9 100644

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 33

Quoted 6 lines
arch/x86/boot/compressed/kernel_info.S +3 -0
@@ -28,6 +28,40 @@ SYM_DATA_START(kernel_info)
          /* Maximal allowed type for setup_data and setup_indirect structs. */
          .long   SETUP_TYPE_MAX
 
+       /* Offset to the MLE header structure */
+#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
+       .long   rva(mle_header)

... so this could just be mle_header - kernel_info, and the consumer can do the math instead.

Quoted 16 lines
arch/x86/boot/compressed/kernel_info.S +13 -0
+#else
+       .long   0
+#endif
+
   kernel_info_var_len_data:
          /* Empty for time being... */
   SYM_DATA_END_LABEL(kernel_info, SYM_L_LOCAL, kernel_info_end)
+
+#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
+       /*
+        * The MLE Header per the TXT Specification, section 2.1
+        * MLE capabilities, see table 4. Capabilities set:
+        * bit 0: Support for GETSEC[WAKEUP] for RLP wakeup
+        * bit 1: Support for RLP wakeup using MONITOR address
+        * bit 2: The ECX register will contain the pointer to the MLE page table
+        * bit 5: TPM 1.2 family: Details/authorities PCR usage support

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 59

Quoted 10 lines
arch/x86/boot/compressed/kernel_info.S +10 -0
+        * bit 9: Supported format of TPM 2.0 event log - TCG compliant
+        */
+SYM_DATA_START(mle_header)
+       .long   0x9082ac5a  /* UUID0 */
+       .long   0x74a7476f  /* UUID1 */
+       .long   0xa2555c0f  /* UUID2 */
+       .long   0x42b651cb  /* UUID3 */
+       .long   0x00000034  /* MLE header size */
+       .long   0x00020002  /* MLE version 2.2 */
+       .long   rva(sl_stub_entry) /* Linear entry point of MLE (virt. address) */

and these should perhaps be relative to mle_header?

Quoted 3 lines
arch/x86/boot/compressed/kernel_info.S +3 -0
+       .long   0x00000000  /* First valid page of MLE */
+       .long   0x00000000  /* Offset within binary of first byte of MLE */
+       .long   rva(_edata) /* Offset within binary of last byte + 1 of MLE */
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

and here

Ugh never mind - these are specified externally.

Can you clarify your follow on comment here?

I noticed that -as you pointed out in your previous reply- these fields cannot be repainted at will, as they are defined by an external specification.

Quoted 9 lines
Quoted 4 lines

I'll play a bit more with this code tomorrow - I would *really* like to avoid the need for patch #1, as it adds another constraint on how we construct the boot image, and this is already riddled with legacy and other complications.

Yea I should have read forward through all your replies before responding to the first one but I think it clarified things as you point out here. We appreciate you help and suggestions.

OK, so I have a solution that does not require kernel_info at a fixed offset:

- put this at the end of arch/x86/boot/compressed/vmlinux.lds.S

#ifdef CONFIG_SECURE_LAUNCH
PROVIDE(kernel_info_offset = ABSOLUTE(kernel_info - startup_32));
PROVIDE(mle_header_offset = kernel_info_offset +
ABSOLUTE(mle_header - startup_32));
PROVIDE(sl_stub_entry_offset = kernel_info_offset +
ABSOLUTE(sl_stub_entry - startup_32));
PROVIDE(_edata_offset = kernel_info_offset + ABSOLUTE(_edata
- startup_32));
#endif

and use this for the header fields:

        /* Offset to the MLE header structure */

#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
.long mle_header_offset - kernel_info
#else
.long 0
#endif

SYM_DATA_START(mle_header)

        .long   0x9082ac5a  /* UUID0 */
        .long   0x74a7476f  /* UUID1 */
        .long   0xa2555c0f  /* UUID2 */
        .long   0x42b651cb  /* UUID3 */
        .long   0x00000034  /* MLE header size */
        .long   0x00020002  /* MLE version 2.2 */
        .long   sl_stub_entry_offset - kernel_info /* Linear entry
point of MLE (virt. address) */
        .long   0x00000000  /* First valid page of MLE */
        .long   0x00000000  /* Offset within binary of first byte of MLE */
        .long   _edata_offset - kernel_info /* Offset within binary of
last byte + 1 of MLE */
        .long   0x00000227  /* Bit vector of MLE-supported capabilities */
        .long   0x00000000  /* Starting linear address of command line
(unused) */
        .long   0x00000000  /* Ending linear address of command line (unused) */
#79 lore raw

Re: [PATCH v9 08/19] x86: Secure Launch kernel early boot stub

Quoted 241 lines · Ard Biesheuvel
Quoted 215 lines · On Tue, 4 Jun 2024 at 19:34, <[email protected]> wrote:
Quoted 213 lines · Ard Biesheuvel
Quoted 200 lines · On Tue, 4 Jun 2024 at 19:24, <[email protected]> wrote:
Quoted 198 lines · Ard Biesheuvel
Quoted 197 lines · Ard Biesheuvel

Hello Ross,

Quoted 57 lines · Ross Philipson

The Secure Launch (SL) stub provides the entry point for Intel TXT (and later AMD SKINIT) to vector to during the late launch. The symbol sl_stub_entry is that entry point and its offset into the kernel is conveyed to the launching code using the MLE (Measured Launch Environment) header in the structure named mle_header. The offset of the MLE header is set in the kernel_info. The routine sl_stub contains the very early late launch setup code responsible for setting up the basic environment to allow the normal kernel startup_32 code to proceed. It is also responsible for properly waking and handling the APs on Intel platforms. The routine sl_main which runs after entering 64b mode is responsible for measuring configuration and module information before it is used like the boot params, the kernel command line, the TXT heap, an external initramfs, etc.

---
    Documentation/arch/x86/boot.rst        |  21 +
    arch/x86/boot/compressed/Makefile      |   3 +-
    arch/x86/boot/compressed/head_64.S     |  30 +
    arch/x86/boot/compressed/kernel_info.S |  34 ++
    arch/x86/boot/compressed/sl_main.c     | 577 ++++++++++++++++++++
    arch/x86/boot/compressed/sl_stub.S     | 725 +++++++++++++++++++++++++
    arch/x86/include/asm/msr-index.h       |   5 +
    arch/x86/include/uapi/asm/bootparam.h  |   1 +
    arch/x86/kernel/asm-offsets.c          |  20 +
    9 files changed, 1415 insertions(+), 1 deletion(-)
    create mode 100644 arch/x86/boot/compressed/sl_main.c
    create mode 100644 arch/x86/boot/compressed/sl_stub.S
Documentation/arch/x86/boot.rst +12 -0
index 4fd492cb4970..295cdf9bcbdb 100644
@@ -482,6 +482,14 @@ Protocol:  2.00+
               - If 1, KASLR enabled.
               - If 0, KASLR disabled.
 
+  Bit 2 (kernel internal): SLAUNCH_FLAG
+
+       - Used internally by the setup kernel to communicate
+         Secure Launch status to kernel proper.
+
+           - If 1, Secure Launch enabled.
+           - If 0, Secure Launch disabled.
+
      Bit 5 (write): QUIET_FLAG
 
           - If 0, print early messages.
@@ -1028,6 +1036,19 @@ Offset/size:     0x000c/4
 
      This field contains maximal allowed type for setup_data and setup_indirect structs.
 
+============   =================
+Field name:    mle_header_offset
+Offset/size:   0x0010/4
+============   =================

Documentation/arch/x86/boot.rst Offset/size: 0x000c/4 · line 1052

Quoted 10 lines
Documentation/arch/x86/boot.rst +9 -0
+
+  This field contains the offset to the Secure Launch Measured Launch Environment
+  (MLE) header. This offset is used to locate information needed during a secure
+  late launch using Intel TXT. If the offset is zero, the kernel does not have
+  Secure Launch capabilities. The MLE entry point is called from TXT on the BSP
+  following a success measured launch. The specific state of the processors is
+  outlined in the TXT Software Development Guide, the latest can be found here:
+  https://urldefense.com/v3/__https://www.intel.com/content/dam/www/public/us/en/documents/guides/intel-txt-software-development-guide.pdf__;!!ACWV5N9M2RV99hQ!Mng0gnPhOYZ8D02t1rYwQfY6U3uWaypJyd1T2rsWz3QNHr9GhIZ9ANB_-cgPExxX0e0KmCpda-3VX8Fj$
+
 

Could we just repaint this field as the offset relative to the start of kernel_info rather than relative to the start of the image? That way, there is no need for patch #1, and given that the consumer of this field accesses it via kernel_info, I wouldn't expect any issues in applying this offset to obtain the actual address.

Quoted 21 lines
Documentation/arch/x86/boot.rst +0 -0
    The Image Checksum
    ==================
arch/x86/boot/compressed/Makefile +2 -1
index 9189a0e28686..9076a248d4b4 100644
@@ -118,7 +118,8 @@ vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi.o
    vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_mixed.o
    vmlinux-objs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
 
-vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o $(obj)/early_sha256.o
+vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o $(obj)/early_sha256.o \
+       $(obj)/sl_main.o $(obj)/sl_stub.o
 
    $(obj)/vmlinux: $(vmlinux-objs-y) FORCE
           $(call if_changed,ld)
arch/x86/boot/compressed/head_64.S +0 -0
index 1dcb794c5479..803c9e2e6d85 100644

arch/x86/boot/compressed/head_64.S SYM_CODE_START(startup_64) · line 424

Quoted 5 lines
arch/x86/boot/compressed/head_64.S +2 -0
@@ -420,6 +420,13 @@ SYM_CODE_START(startup_64)
           pushq   $0
           popfq
 
+#ifdef CONFIG_SECURE_LAUNCH
+       /* Ensure the relocation region is coverd by a PMR */

covered

Quoted 25 lines
arch/x86/boot/compressed/head_64.S +18 -0
+       movq    %rbx, %rdi
+       movl    $(_bss - startup_32), %esi
+       callq   sl_check_region
+#endif
+
    /*
     * Copy the compressed kernel to the end of our buffer
     * where decompression in place becomes safe.
@@ -462,6 +469,29 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated)
           shrq    $3, %rcx
           rep     stosq
 
+#ifdef CONFIG_SECURE_LAUNCH
+       /*
+        * Have to do the final early sl stub work in 64b area.
+        *
+        * *********** NOTE ***********
+        *
+        * Several boot params get used before we get a chance to measure
+        * them in this call. This is a known issue and we currently don't
+        * have a solution. The scratch field doesn't matter. There is no
+        * obvious way to do anything about the use of kernel_alignment or
+        * init_size though these seem low risk with all the PMR and overlap
+        * checks in place.
+        */

arch/x86/boot/compressed/head_64.S SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated) · line 494

Quoted 10 lines
arch/x86/boot/compressed/head_64.S +10 -0
+       movq    %r15, %rdi
+       callq   sl_main
+
+       /* Ensure the decompression location is covered by a PMR */
+       movq    %rbp, %rdi
+       movq    output_len(%rip), %rsi
+       callq   sl_check_region
+#endif
+
+       pushq   %rsi

This looks like a rebase error.

Quoted 8 lines
arch/x86/boot/compressed/head_64.S +0 -0
           call    load_stage2_idt
 
           /* Pass boot_params to initialize_identity_maps() */
arch/x86/boot/compressed/kernel_info.S +0 -0
index c18f07181dd5..e199b87764e9 100644

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 33

Quoted 6 lines
arch/x86/boot/compressed/kernel_info.S +3 -0
@@ -28,6 +28,40 @@ SYM_DATA_START(kernel_info)
           /* Maximal allowed type for setup_data and setup_indirect structs. */
           .long   SETUP_TYPE_MAX
 
+       /* Offset to the MLE header structure */
+#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
+       .long   rva(mle_header)

... so this could just be mle_header - kernel_info, and the consumer can do the math instead.

Quoted 16 lines
arch/x86/boot/compressed/kernel_info.S +13 -0
+#else
+       .long   0
+#endif
+
    kernel_info_var_len_data:
           /* Empty for time being... */
    SYM_DATA_END_LABEL(kernel_info, SYM_L_LOCAL, kernel_info_end)
+
+#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
+       /*
+        * The MLE Header per the TXT Specification, section 2.1
+        * MLE capabilities, see table 4. Capabilities set:
+        * bit 0: Support for GETSEC[WAKEUP] for RLP wakeup
+        * bit 1: Support for RLP wakeup using MONITOR address
+        * bit 2: The ECX register will contain the pointer to the MLE page table
+        * bit 5: TPM 1.2 family: Details/authorities PCR usage support

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 59

Quoted 10 lines
arch/x86/boot/compressed/kernel_info.S +10 -0
+        * bit 9: Supported format of TPM 2.0 event log - TCG compliant
+        */
+SYM_DATA_START(mle_header)
+       .long   0x9082ac5a  /* UUID0 */
+       .long   0x74a7476f  /* UUID1 */
+       .long   0xa2555c0f  /* UUID2 */
+       .long   0x42b651cb  /* UUID3 */
+       .long   0x00000034  /* MLE header size */
+       .long   0x00020002  /* MLE version 2.2 */
+       .long   rva(sl_stub_entry) /* Linear entry point of MLE (virt. address) */

and these should perhaps be relative to mle_header?

Quoted 3 lines
arch/x86/boot/compressed/kernel_info.S +3 -0
+       .long   0x00000000  /* First valid page of MLE */
+       .long   0x00000000  /* Offset within binary of first byte of MLE */
+       .long   rva(_edata) /* Offset within binary of last byte + 1 of MLE */
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

and here

Ugh never mind - these are specified externally.

Can you clarify your follow on comment here?

I noticed that -as you pointed out in your previous reply- these fields cannot be repainted at will, as they are defined by an external specification.

Quoted 9 lines
Quoted 4 lines

I'll play a bit more with this code tomorrow - I would *really* like to avoid the need for patch #1, as it adds another constraint on how we construct the boot image, and this is already riddled with legacy and other complications.

Yea I should have read forward through all your replies before responding to the first one but I think it clarified things as you point out here. We appreciate you help and suggestions.

OK, so I have a solution that does not require kernel_info at a fixed offset:

- put this at the end of arch/x86/boot/compressed/vmlinux.lds.S

#ifdef CONFIG_SECURE_LAUNCH
PROVIDE(kernel_info_offset = ABSOLUTE(kernel_info - startup_32));
PROVIDE(mle_header_offset = kernel_info_offset +
ABSOLUTE(mle_header - startup_32));
PROVIDE(sl_stub_entry_offset = kernel_info_offset +
ABSOLUTE(sl_stub_entry - startup_32));
PROVIDE(_edata_offset = kernel_info_offset + ABSOLUTE(_edata
- startup_32));
#endif

Quoted 9 lines

and use this for the header fields:

         /* Offset to the MLE header structure */

#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
.long mle_header_offset - kernel_info
#else
.long 0
#endif

Awesome thank you! We will work on incorporating this unless someone else sees a problem with it (or we run into problems and need to revisit).

Ross

Quoted 20 lines

SYM_DATA_START(mle_header)

         .long   0x9082ac5a  /* UUID0 */
         .long   0x74a7476f  /* UUID1 */
         .long   0xa2555c0f  /* UUID2 */
         .long   0x42b651cb  /* UUID3 */
         .long   0x00000034  /* MLE header size */
         .long   0x00020002  /* MLE version 2.2 */
         .long   sl_stub_entry_offset - kernel_info /* Linear entry
point of MLE (virt. address) */
         .long   0x00000000  /* First valid page of MLE */
         .long   0x00000000  /* Offset within binary of first byte of MLE */
         .long   _edata_offset - kernel_info /* Offset within binary of
last byte + 1 of MLE */
         .long   0x00000227  /* Bit vector of MLE-supported capabilities */
         .long   0x00000000  /* Starting linear address of command line
(unused) */
         .long   0x00000000  /* Ending linear address of command line (unused) */

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#80 lore raw

Re: [PATCH v9 08/19] x86: Secure Launch kernel early boot stub

In reply to Ard Biesheuvel
Quoted 1 line · Ard Biesheuvel

Hello Ross,

Hi Ard,

Quoted 67 lines · Ross Philipson
Quoted 65 lines

The Secure Launch (SL) stub provides the entry point for Intel TXT (and later AMD SKINIT) to vector to during the late launch. The symbol sl_stub_entry is that entry point and its offset into the kernel is conveyed to the launching code using the MLE (Measured Launch Environment) header in the structure named mle_header. The offset of the MLE header is set in the kernel_info. The routine sl_stub contains the very early late launch setup code responsible for setting up the basic environment to allow the normal kernel startup_32 code to proceed. It is also responsible for properly waking and handling the APs on Intel platforms. The routine sl_main which runs after entering 64b mode is responsible for measuring configuration and module information before it is used like the boot params, the kernel command line, the TXT heap, an external initramfs, etc.

---
  Documentation/arch/x86/boot.rst        |  21 +
  arch/x86/boot/compressed/Makefile      |   3 +-
  arch/x86/boot/compressed/head_64.S     |  30 +
  arch/x86/boot/compressed/kernel_info.S |  34 ++
  arch/x86/boot/compressed/sl_main.c     | 577 ++++++++++++++++++++
  arch/x86/boot/compressed/sl_stub.S     | 725 +++++++++++++++++++++++++
  arch/x86/include/asm/msr-index.h       |   5 +
  arch/x86/include/uapi/asm/bootparam.h  |   1 +
  arch/x86/kernel/asm-offsets.c          |  20 +
  9 files changed, 1415 insertions(+), 1 deletion(-)

create mode 100644 arch/x86/boot/compressed/sl_main.c create mode 100644 arch/x86/boot/compressed/sl_stub.S

Documentation/arch/x86/boot.rst +20 -0
index 4fd492cb4970..295cdf9bcbdb 100644
@@ -482,6 +482,14 @@ Protocol:  2.00+
             - If 1, KASLR enabled.
             - If 0, KASLR disabled.
 
+  Bit 2 (kernel internal): SLAUNCH_FLAG
+
+       - Used internally by the setup kernel to communicate
+         Secure Launch status to kernel proper.
+
+           - If 1, Secure Launch enabled.
+           - If 0, Secure Launch disabled.
+
    Bit 5 (write): QUIET_FLAG
 
         - If 0, print early messages.
@@ -1028,6 +1036,19 @@ Offset/size:     0x000c/4
 
    This field contains maximal allowed type for setup_data and setup_indirect structs.
 
+============   =================
+Field name:    mle_header_offset
+Offset/size:   0x0010/4
+============   =================
+
+  This field contains the offset to the Secure Launch Measured Launch Environment
+  (MLE) header. This offset is used to locate information needed during a secure
+  late launch using Intel TXT. If the offset is zero, the kernel does not have
+  Secure Launch capabilities. The MLE entry point is called from TXT on the BSP
+  following a success measured launch. The specific state of the processors is
+  outlined in the TXT Software Development Guide, the latest can be found here:
+  https://urldefense.com/v3/__https://www.intel.com/content/dam/www/public/us/en/documents/guides/intel-txt-software-development-guide.pdf__;!!ACWV5N9M2RV99hQ!MdqTLUxfB5YUUOB1qyhkN8TF9HAoGW7yy_2qwZGWz8eb73CYkDXY-h1SeyaZjwzsSWz408D3LgDD8Zmw$
Quoted 8 lines

Documentation/arch/x86/boot.rst Offset/size: 0x000c/4 · line 1052

Quoted 2 lines
Documentation/arch/x86/boot.rst +1 -0
+
 

Could we just repaint this field as the offset relative to the start of kernel_info rather than relative to the start of the image? That way, there is no need for patch #1, and given that the consumer of this field accesses it via kernel_info, I wouldn't expect any issues in applying this offset to obtain the actual address.

What you suggest here may be possible with respect to the location of the MLE header itself, we need to give that more thought. The real issue though is covered in my response below concerning the fields in the MLE header.

Quoted 26 lines
Quoted 26 lines
Documentation/arch/x86/boot.rst +0 -0
  The Image Checksum
  ==================
arch/x86/boot/compressed/Makefile +2 -1
index 9189a0e28686..9076a248d4b4 100644
@@ -118,7 +118,8 @@ vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi.o
  vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_mixed.o
  vmlinux-objs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
 
-vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o $(obj)/early_sha256.o
+vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o $(obj)/early_sha256.o \
+       $(obj)/sl_main.o $(obj)/sl_stub.o
 
  $(obj)/vmlinux: $(vmlinux-objs-y) FORCE
         $(call if_changed,ld)
arch/x86/boot/compressed/head_64.S +0 -0
index 1dcb794c5479..803c9e2e6d85 100644
@@ -420,6 +420,13 @@ SYM_CODE_START(startup_64)
         pushq   $0
         popfq
 
Quoted 4 lines

arch/x86/boot/compressed/head_64.S SYM_CODE_START(startup_64) · line 424

Quoted 2 lines
arch/x86/boot/compressed/head_64.S +2 -0
+#ifdef CONFIG_SECURE_LAUNCH
+       /* Ensure the relocation region is coverd by a PMR */

covered

Ack

Quoted 30 lines
Quoted 30 lines
arch/x86/boot/compressed/head_64.S +22 -0
+       movq    %rbx, %rdi
+       movl    $(_bss - startup_32), %esi
+       callq   sl_check_region
+#endif
+
  /*
   * Copy the compressed kernel to the end of our buffer
   * where decompression in place becomes safe.
@@ -462,6 +469,29 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated)
         shrq    $3, %rcx
         rep     stosq
 
+#ifdef CONFIG_SECURE_LAUNCH
+       /*
+        * Have to do the final early sl stub work in 64b area.
+        *
+        * *********** NOTE ***********
+        *
+        * Several boot params get used before we get a chance to measure
+        * them in this call. This is a known issue and we currently don't
+        * have a solution. The scratch field doesn't matter. There is no
+        * obvious way to do anything about the use of kernel_alignment or
+        * init_size though these seem low risk with all the PMR and overlap
+        * checks in place.
+        */
+       movq    %r15, %rdi
+       callq   sl_main
+
+       /* Ensure the decompression location is covered by a PMR */
Quoted 8 lines

arch/x86/boot/compressed/head_64.S SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated) · line 494

Quoted 6 lines
arch/x86/boot/compressed/head_64.S +6 -0
+       movq    %rbp, %rdi
+       movq    output_len(%rip), %rsi
+       callq   sl_check_region
+#endif
+
+       pushq   %rsi

This looks like a rebase error.

I will need to look closely at this. I remember removing the push/pop we added but this does seem wrong.

Quoted 12 lines
Quoted 12 lines
arch/x86/boot/compressed/head_64.S +0 -0
         call    load_stage2_idt
 
         /* Pass boot_params to initialize_identity_maps() */
arch/x86/boot/compressed/kernel_info.S +0 -0
index c18f07181dd5..e199b87764e9 100644
@@ -28,6 +28,40 @@ SYM_DATA_START(kernel_info)
         /* Maximal allowed type for setup_data and setup_indirect structs. */
         .long   SETUP_TYPE_MAX
 
Quoted 6 lines

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 33

Quoted 3 lines
arch/x86/boot/compressed/kernel_info.S +3 -0
+       /* Offset to the MLE header structure */
+#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
+       .long   rva(mle_header)

... so this could just be mle_header - kernel_info, and the consumer can do the math instead.

While we might be able to do this...

Quoted 21 lines
Quoted 21 lines
arch/x86/boot/compressed/kernel_info.S +17 -0
+#else
+       .long   0
+#endif
+
  kernel_info_var_len_data:
         /* Empty for time being... */
  SYM_DATA_END_LABEL(kernel_info, SYM_L_LOCAL, kernel_info_end)
+
+#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
+       /*
+        * The MLE Header per the TXT Specification, section 2.1
+        * MLE capabilities, see table 4. Capabilities set:
+        * bit 0: Support for GETSEC[WAKEUP] for RLP wakeup
+        * bit 1: Support for RLP wakeup using MONITOR address
+        * bit 2: The ECX register will contain the pointer to the MLE page table
+        * bit 5: TPM 1.2 family: Details/authorities PCR usage support
+        * bit 9: Supported format of TPM 2.0 event log - TCG compliant
+        */
+SYM_DATA_START(mle_header)
+       .long   0x9082ac5a  /* UUID0 */
Quoted 8 lines

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 59

Quoted 6 lines
arch/x86/boot/compressed/kernel_info.S +6 -0
+       .long   0x74a7476f  /* UUID1 */
+       .long   0xa2555c0f  /* UUID2 */
+       .long   0x42b651cb  /* UUID3 */
+       .long   0x00000034  /* MLE header size */
+       .long   0x00020002  /* MLE version 2.2 */
+       .long   rva(sl_stub_entry) /* Linear entry point of MLE (virt. address) */

and these should perhaps be relative to mle_header?

... this would be much more problematic. For TXT, the SINIT ACM is the consumer of this field and it expects it to be relative to the beginning of the MLE image (i.e. the setup kernl and piggy in this case) and we have no control over that. We used to fix this value up in the prologue code for the launch but this was not a clean approach. The same issue applies to the next comment too. We will have to see what other options we may be able to adopt here.

Quoted 6 lines

arch/x86/boot/compressed/kernel_info.S SYM_DATA_START(kernel_info) · line 62

Quoted 4 lines
arch/x86/boot/compressed/kernel_info.S +3 -0
+       .long   0x00000000  /* First valid page of MLE */
+       .long   0x00000000  /* Offset within binary of first byte of MLE */
+       .long   rva(_edata) /* Offset within binary of last byte + 1 of MLE */

and here

See last comment.

Quoted 133 lines
Quoted 133 lines
arch/x86/boot/compressed/kernel_info.S +5 -0
+       .long   0x00000227  /* Bit vector of MLE-supported capabilities */
+       .long   0x00000000  /* Starting linear address of command line (unused) */
+       .long   0x00000000  /* Ending linear address of command line (unused) */
+SYM_DATA_END(mle_header)
+#endif
arch/x86/boot/compressed/sl_main.c new file +121 -0
new file mode 100644
index 000000000000..61e9baf410fd
@@ -0,0 +1,577 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Secure Launch early measurement and validation routines.
+ *
+ * Copyright (c) 2024, Oracle and/or its affiliates.
+ */
+
+#include <linux/init.h>
+#include <linux/string.h>
+#include <linux/linkage.h>
+#include <asm/segment.h>
+#include <asm/boot.h>
+#include <asm/msr.h>
+#include <asm/mtrr.h>
+#include <asm/processor-flags.h>
+#include <asm/asm-offsets.h>
+#include <asm/bootparam.h>
+#include <asm/bootparam_utils.h>
+#include <linux/slr_table.h>
+#include <linux/slaunch.h>
+#include <crypto/sha1.h>
+#include <crypto/sha2.h>
+
+#define CAPS_VARIABLE_MTRR_COUNT_MASK  0xff
+
+#define SL_TPM12_LOG           1
+#define SL_TPM20_LOG           2
+
+#define SL_TPM20_MAX_ALGS      2
+
+#define SL_MAX_EVENT_DATA      64
+#define SL_TPM12_LOG_SIZE      (sizeof(struct tcg_pcr_event) + \
+                               SL_MAX_EVENT_DATA)
+#define SL_TPM20_LOG_SIZE      (sizeof(struct tcg_pcr_event2_head) + \
+                               SHA1_DIGEST_SIZE + SHA256_DIGEST_SIZE + \
+                               sizeof(struct tcg_event_field) + \
+                               SL_MAX_EVENT_DATA)
+
+static void *evtlog_base;
+static u32 evtlog_size;
+static struct txt_heap_event_log_pointer2_1_element *log20_elem;
+static u32 tpm_log_ver = SL_TPM12_LOG;
+static struct tcg_efi_specid_event_algs tpm_algs[SL_TPM20_MAX_ALGS] = {0};
+
+extern u32 sl_cpu_type;
+extern u32 sl_mle_start;
+
+static u64 sl_txt_read(u32 reg)
+{
+       return readq((void *)(u64)(TXT_PRIV_CONFIG_REGS_BASE + reg));
+}
+
+static void sl_txt_write(u32 reg, u64 val)
+{
+       writeq(val, (void *)(u64)(TXT_PRIV_CONFIG_REGS_BASE + reg));
+}
+
+static void __noreturn sl_txt_reset(u64 error)
+{
+       /* Reading the E2STS register acts as a barrier for TXT registers */
+       sl_txt_write(TXT_CR_ERRORCODE, error);
+       sl_txt_read(TXT_CR_E2STS);
+       sl_txt_write(TXT_CR_CMD_UNLOCK_MEM_CONFIG, 1);
+       sl_txt_read(TXT_CR_E2STS);
+       sl_txt_write(TXT_CR_CMD_RESET, 1);
+
+       for ( ; ; )
+               asm volatile ("hlt");
+
+       unreachable();
+}
+
+static u64 sl_rdmsr(u32 reg)
+{
+       u64 lo, hi;
+
+       asm volatile ("rdmsr" : "=a" (lo), "=d" (hi) : "c" (reg));
+
+       return (hi << 32) | lo;
+}
+
+static struct slr_table *sl_locate_and_validate_slrt(void)
+{
+       struct txt_os_mle_data *os_mle_data;
+       struct slr_table *slrt;
+       void *txt_heap;
+
+       txt_heap = (void *)sl_txt_read(TXT_CR_HEAP_BASE);
+       os_mle_data = txt_os_mle_data_start(txt_heap);
+
+       if (!os_mle_data->slrt)
+               sl_txt_reset(SL_ERROR_INVALID_SLRT);
+
+       slrt = (struct slr_table *)os_mle_data->slrt;
+
+       if (slrt->magic != SLR_TABLE_MAGIC)
+               sl_txt_reset(SL_ERROR_INVALID_SLRT);
+
+       if (slrt->architecture != SLR_INTEL_TXT)
+               sl_txt_reset(SL_ERROR_INVALID_SLRT);
+
+       return slrt;
+}
+
+static void sl_check_pmr_coverage(void *base, u32 size, bool allow_hi)
+{
+       struct txt_os_sinit_data *os_sinit_data;
+       void *end = base + size;
+       void *txt_heap;
+
+       if (!(sl_cpu_type & SL_CPU_INTEL))
+               return;
+
+       txt_heap = (void *)sl_txt_read(TXT_CR_HEAP_BASE);
+       os_sinit_data = txt_os_sinit_data_start(txt_heap);
+
+       if ((end >= (void *)0x100000000ULL) && (base < (void *)0x100000000ULL))
+               sl_txt_reset(SL_ERROR_REGION_STRADDLE_4GB);
+
+       /*
+        * Note that the late stub code validates that the hi PMR covers
Quoted 8 lines

arch/x86/boot/compressed/sl_main.c line 125

Quoted 4 lines
arch/x86/boot/compressed/sl_main.c new file +4 -0
+        * all memory above 4G. At this point the code can only check that
+        * regions are within the hi PMR but that is sufficient.
+        */
+       if ((end > (void *)0x100000000ULL) && (base >= (void *)0x100000000ULL)) {

Better to put the cast on the pointers, given that we are doing arithmetic, and use SZ_4G instead of open coding this constant 4 times.

Good idea, we will do that.

Quoted 533 lines
Quoted 533 lines
arch/x86/boot/compressed/sl_main.c new file +452 -0
+               if (allow_hi) {
+                       if (end >= (void *)(os_sinit_data->vtd_pmr_hi_base +
+                                          os_sinit_data->vtd_pmr_hi_size))
+                               sl_txt_reset(SL_ERROR_BUFFER_BEYOND_PMR);
+               } else {
+                       sl_txt_reset(SL_ERROR_REGION_ABOVE_4GB);
+               }
+       }
+
+       if (end >= (void *)os_sinit_data->vtd_pmr_lo_size)
+               sl_txt_reset(SL_ERROR_BUFFER_BEYOND_PMR);
+}
+
+/*
+ * Some MSRs are modified by the pre-launch code including the MTRRs.
+ * The early MLE code has to restore these values. This code validates
+ * the values after they are measured.
+ */
+static void sl_txt_validate_msrs(struct txt_os_mle_data *os_mle_data)
+{
+       struct slr_txt_mtrr_state *saved_bsp_mtrrs;
+       u64 mtrr_caps, mtrr_def_type, mtrr_var;
+       struct slr_entry_intel_info *txt_info;
+       u64 misc_en_msr;
+       u32 vcnt, i;
+
+       txt_info = (struct slr_entry_intel_info *)os_mle_data->txt_info;
+       saved_bsp_mtrrs = &txt_info->saved_bsp_mtrrs;
+
+       mtrr_caps = sl_rdmsr(MSR_MTRRcap);
+       vcnt = (u32)(mtrr_caps & CAPS_VARIABLE_MTRR_COUNT_MASK);
+
+       if (saved_bsp_mtrrs->mtrr_vcnt > vcnt)
+               sl_txt_reset(SL_ERROR_MTRR_INV_VCNT);
+       if (saved_bsp_mtrrs->mtrr_vcnt > TXT_OS_MLE_MAX_VARIABLE_MTRRS)
+               sl_txt_reset(SL_ERROR_MTRR_INV_VCNT);
+
+       mtrr_def_type = sl_rdmsr(MSR_MTRRdefType);
+       if (saved_bsp_mtrrs->default_mem_type != mtrr_def_type)
+               sl_txt_reset(SL_ERROR_MTRR_INV_DEF_TYPE);
+
+       for (i = 0; i < saved_bsp_mtrrs->mtrr_vcnt; i++) {
+               mtrr_var = sl_rdmsr(MTRRphysBase_MSR(i));
+               if (saved_bsp_mtrrs->mtrr_pair[i].mtrr_physbase != mtrr_var)
+                       sl_txt_reset(SL_ERROR_MTRR_INV_BASE);
+               mtrr_var = sl_rdmsr(MTRRphysMask_MSR(i));
+               if (saved_bsp_mtrrs->mtrr_pair[i].mtrr_physmask != mtrr_var)
+                       sl_txt_reset(SL_ERROR_MTRR_INV_MASK);
+       }
+
+       misc_en_msr = sl_rdmsr(MSR_IA32_MISC_ENABLE);
+       if (txt_info->saved_misc_enable_msr != misc_en_msr)
+               sl_txt_reset(SL_ERROR_MSR_INV_MISC_EN);
+}
+
+static void sl_find_drtm_event_log(struct slr_table *slrt)
+{
+       struct txt_os_sinit_data *os_sinit_data;
+       struct slr_entry_log_info *log_info;
+       void *txt_heap;
+
+       log_info = slr_next_entry_by_tag(slrt, NULL, SLR_ENTRY_LOG_INFO);
+       if (!log_info)
+               sl_txt_reset(SL_ERROR_SLRT_MISSING_ENTRY);
+
+       evtlog_base = (void *)log_info->addr;
+       evtlog_size = log_info->size;
+
+       txt_heap = (void *)sl_txt_read(TXT_CR_HEAP_BASE);
+
+       /*
+        * For TPM 2.0, the event log 2.1 extended data structure has to also
+        * be located and fixed up.
+        */
+       os_sinit_data = txt_os_sinit_data_start(txt_heap);
+
+       /*
+        * Only support version 6 and later that properly handle the
+        * list of ExtDataElements in the OS-SINIT structure.
+        */
+       if (os_sinit_data->version < 6)
+               sl_txt_reset(SL_ERROR_OS_SINIT_BAD_VERSION);
+
+       /* Find the TPM2.0 logging extended heap element */
+       log20_elem = tpm20_find_log2_1_element(os_sinit_data);
+
+       /* If found, this implies TPM20 log and family */
+       if (log20_elem)
+               tpm_log_ver = SL_TPM20_LOG;
+}
+
+static void sl_validate_event_log_buffer(void)
+{
+       struct txt_os_sinit_data *os_sinit_data;
+       void *txt_heap, *txt_end;
+       void *mle_base, *mle_end;
+       void *evtlog_end;
+
+       if ((u64)evtlog_size > (LLONG_MAX - (u64)evtlog_base))
+               sl_txt_reset(SL_ERROR_INTEGER_OVERFLOW);
+       evtlog_end = evtlog_base + evtlog_size;
+
+       txt_heap = (void *)sl_txt_read(TXT_CR_HEAP_BASE);
+       txt_end = txt_heap + sl_txt_read(TXT_CR_HEAP_SIZE);
+       os_sinit_data = txt_os_sinit_data_start(txt_heap);
+
+       mle_base = (void *)(u64)sl_mle_start;
+       mle_end = mle_base + os_sinit_data->mle_size;
+
+       /*
+        * This check is to ensure the event log buffer does not overlap with
+        * the MLE image.
+        */
+       if (evtlog_base >= mle_end && evtlog_end > mle_end)
+               goto pmr_check; /* above */
+
+       if (evtlog_end <= mle_base && evtlog_base < mle_base)
+               goto pmr_check; /* below */
+
+       sl_txt_reset(SL_ERROR_MLE_BUFFER_OVERLAP);
+
+pmr_check:
+       /*
+        * The TXT heap is protected by the DPR. If the TPM event log is
+        * inside the TXT heap, there is no need for a PMR check.
+        */
+       if (evtlog_base > txt_heap && evtlog_end < txt_end)
+               return;
+
+       sl_check_pmr_coverage(evtlog_base, evtlog_size, true);
+}
+
+static void sl_find_event_log_algorithms(void)
+{
+       struct tcg_efi_specid_event_head *efi_head =
+               (struct tcg_efi_specid_event_head *)(evtlog_base +
+                                       log20_elem->first_record_offset +
+                                       sizeof(struct tcg_pcr_event));
+
+       if (efi_head->num_algs == 0 || efi_head->num_algs > 2)
+               sl_txt_reset(SL_ERROR_TPM_NUMBER_ALGS);
+
+       memcpy(&tpm_algs[0], &efi_head->digest_sizes[0],
+              sizeof(struct tcg_efi_specid_event_algs) * efi_head->num_algs);
+}
+
+static void sl_tpm12_log_event(u32 pcr, u32 event_type,
+                              const u8 *data, u32 length,
+                              const u8 *event_data, u32 event_size)
+{
+       u8 sha1_hash[SHA1_DIGEST_SIZE] = {0};
+       u8 log_buf[SL_TPM12_LOG_SIZE] = {0};
+       struct tcg_pcr_event *pcr_event;
+       u32 total_size;
+
+       pcr_event = (struct tcg_pcr_event *)log_buf;
+       pcr_event->pcr_idx = pcr;
+       pcr_event->event_type = event_type;
+       if (length > 0) {
+               sha1(data, length, &sha1_hash[0]);
+               memcpy(&pcr_event->digest[0], &sha1_hash[0], SHA1_DIGEST_SIZE);
+       }
+       pcr_event->event_size = event_size;
+       if (event_size > 0)
+               memcpy((u8 *)pcr_event + sizeof(struct tcg_pcr_event),
+                      event_data, event_size);
+
+       total_size = sizeof(struct tcg_pcr_event) + event_size;
+
+       if (tpm12_log_event(evtlog_base, evtlog_size, total_size, pcr_event))
+               sl_txt_reset(SL_ERROR_TPM_LOGGING_FAILED);
+}
+
+static void sl_tpm20_log_event(u32 pcr, u32 event_type,
+                              const u8 *data, u32 length,
+                              const u8 *event_data, u32 event_size)
+{
+       u8 sha256_hash[SHA256_DIGEST_SIZE] = {0};
+       u8 sha1_hash[SHA1_DIGEST_SIZE] = {0};
+       u8 log_buf[SL_TPM20_LOG_SIZE] = {0};
+       struct sha256_state sctx256 = {0};
+       struct tcg_pcr_event2_head *head;
+       struct tcg_event_field *event;
+       u32 total_size;
+       u16 *alg_ptr;
+       u8 *dgst_ptr;
+
+       head = (struct tcg_pcr_event2_head *)log_buf;
+       head->pcr_idx = pcr;
+       head->event_type = event_type;
+       total_size = sizeof(struct tcg_pcr_event2_head);
+       alg_ptr = (u16 *)(log_buf + sizeof(struct tcg_pcr_event2_head));
+
+       for ( ; head->count < 2; head->count++) {
+               if (!tpm_algs[head->count].alg_id)
+                       break;
+
+               *alg_ptr = tpm_algs[head->count].alg_id;
+               dgst_ptr = (u8 *)alg_ptr + sizeof(u16);
+
+               if (tpm_algs[head->count].alg_id == TPM_ALG_SHA256 &&
+                   length) {
+                       sha256_init(&sctx256);
+                       sha256_update(&sctx256, data, length);
+                       sha256_final(&sctx256, &sha256_hash[0]);
+               } else if (tpm_algs[head->count].alg_id == TPM_ALG_SHA1 &&
+                          length) {
+                       sha1(data, length, &sha1_hash[0]);
+               }
+
+               if (tpm_algs[head->count].alg_id == TPM_ALG_SHA256) {
+                       memcpy(dgst_ptr, &sha256_hash[0], SHA256_DIGEST_SIZE);
+                       total_size += SHA256_DIGEST_SIZE + sizeof(u16);
+                       alg_ptr = (u16 *)((u8 *)alg_ptr + SHA256_DIGEST_SIZE + sizeof(u16));
+               } else if (tpm_algs[head->count].alg_id == TPM_ALG_SHA1) {
+                       memcpy(dgst_ptr, &sha1_hash[0], SHA1_DIGEST_SIZE);
+                       total_size += SHA1_DIGEST_SIZE + sizeof(u16);
+                       alg_ptr = (u16 *)((u8 *)alg_ptr + SHA1_DIGEST_SIZE + sizeof(u16));
+               } else {
+                       sl_txt_reset(SL_ERROR_TPM_UNKNOWN_DIGEST);
+               }
+       }
+
+       event = (struct tcg_event_field *)(log_buf + total_size);
+       event->event_size = event_size;
+       if (event_size > 0)
+               memcpy((u8 *)event + sizeof(struct tcg_event_field), event_data, event_size);
+       total_size += sizeof(struct tcg_event_field) + event_size;
+
+       if (tpm20_log_event(log20_elem, evtlog_base, evtlog_size, total_size, &log_buf[0]))
+               sl_txt_reset(SL_ERROR_TPM_LOGGING_FAILED);
+}
+
+static void sl_tpm_extend_evtlog(u32 pcr, u32 type,
+                                const u8 *data, u32 length, const char *desc)
+{
+       if (tpm_log_ver == SL_TPM20_LOG)
+               sl_tpm20_log_event(pcr, type, data, length,
+                                  (const u8 *)desc, strlen(desc));
+       else
+               sl_tpm12_log_event(pcr, type, data, length,
+                                  (const u8 *)desc, strlen(desc));
+}
+
+static struct setup_data *sl_handle_setup_data(struct setup_data *curr,
+                                              struct slr_policy_entry *entry)
+{
+       struct setup_indirect *ind;
+       struct setup_data *next;
+
+       if (!curr)
+               return NULL;
+
+       next = (struct setup_data *)(unsigned long)curr->next;
+
+       /* SETUP_INDIRECT instances have to be handled differently */
+       if (curr->type == SETUP_INDIRECT) {
+               ind = (struct setup_indirect *)((u8 *)curr + offsetof(struct setup_data, data));
+
+               sl_check_pmr_coverage((void *)ind->addr, ind->len, true);
+
+               sl_tpm_extend_evtlog(entry->pcr, TXT_EVTYPE_SLAUNCH,
+                                    (void *)ind->addr, ind->len,
+                                    entry->evt_info);
+
+               return next;
+       }
+
+       sl_check_pmr_coverage(((u8 *)curr) + sizeof(struct setup_data),
+                             curr->len, true);
+
+       sl_tpm_extend_evtlog(entry->pcr, TXT_EVTYPE_SLAUNCH,
+                            ((u8 *)curr) + sizeof(struct setup_data),
+                            curr->len,
+                            entry->evt_info);
+
+       return next;
+}
+
+static void sl_extend_setup_data(struct slr_policy_entry *entry)
+{
+       struct setup_data *data;
+
+       /*
+        * Measuring the boot params measured the fixed e820 memory map.
+        * Measure any setup_data entries including e820 extended entries.
+        */
+       data = (struct setup_data *)(unsigned long)entry->entity;
+       while (data)
+               data = sl_handle_setup_data(data, entry);
+}
+
+static void sl_extend_slrt(struct slr_policy_entry *entry)
+{
+       struct slr_table *slrt = (struct slr_table *)entry->entity;
+       struct slr_entry_intel_info *intel_info;
+
+       /*
+        * In revision one of the SLRT, the only table that needs to be
+        * measured is the Intel info table. Everything else is meta-data,
+        * addresses and sizes. Note the size of what to measure is not set.
+        * The flag SLR_POLICY_IMPLICIT_SIZE leaves it to the measuring code
+        * to sort out.
+        */
+       if (slrt->revision == 1) {
+               intel_info = slr_next_entry_by_tag(slrt, NULL, SLR_ENTRY_INTEL_INFO);
+               if (!intel_info)
+                       sl_txt_reset(SL_ERROR_SLRT_MISSING_ENTRY);
+
+               sl_tpm_extend_evtlog(entry->pcr, TXT_EVTYPE_SLAUNCH,
+                                    (void *)entry->entity, sizeof(struct slr_entry_intel_info),
+                                    entry->evt_info);
+       }
+}
+
+static void sl_extend_txt_os2mle(struct slr_policy_entry *entry)
+{
+       struct txt_os_mle_data *os_mle_data;
+       void *txt_heap;
+
+       txt_heap = (void *)sl_txt_read(TXT_CR_HEAP_BASE);
+       os_mle_data = txt_os_mle_data_start(txt_heap);
+
+       /*
+        * Version 1 of the OS-MLE heap structure has no fields to measure. It just
+        * has addresses and sizes and a scratch buffer.
+        */
+       if (os_mle_data->version == 1)
+               return;
+}
+
+static void sl_process_extend_policy(struct slr_table *slrt)
+{
+       struct slr_entry_policy *policy;
+       u16 i;
+
+       policy = slr_next_entry_by_tag(slrt, NULL, SLR_ENTRY_ENTRY_POLICY);
+       if (!policy)
+               sl_txt_reset(SL_ERROR_SLRT_MISSING_ENTRY);
+
+       for (i = 0; i < policy->nr_entries; i++) {
+               switch (policy->policy_entries[i].entity_type) {
+               case SLR_ET_SETUP_DATA:
+                       sl_extend_setup_data(&policy->policy_entries[i]);
+                       break;
+               case SLR_ET_SLRT:
+                       sl_extend_slrt(&policy->policy_entries[i]);
+                       break;
+               case SLR_ET_TXT_OS2MLE:
+                       sl_extend_txt_os2mle(&policy->policy_entries[i]);
+                       break;
+               case SLR_ET_UNUSED:
+                       continue;
+               default:
+                       sl_tpm_extend_evtlog(policy->policy_entries[i].pcr, TXT_EVTYPE_SLAUNCH,
+                                            (void *)policy->policy_entries[i].entity,
+                                            policy->policy_entries[i].size,
+                                            policy->policy_entries[i].evt_info);
+               }
+       }
+}
+
+static void sl_process_extend_uefi_config(struct slr_table *slrt)
+{
+       struct slr_entry_uefi_config *uefi_config;
+       u16 i;
+
+       uefi_config = slr_next_entry_by_tag(slrt, NULL, SLR_ENTRY_UEFI_CONFIG);
+
+       /* Optionally here depending on how SL kernel was booted */
+       if (!uefi_config)
+               return;
+
+       for (i = 0; i < uefi_config->nr_entries; i++) {
+               sl_tpm_extend_evtlog(uefi_config->uefi_cfg_entries[i].pcr, TXT_EVTYPE_SLAUNCH,
+                                    (void *)uefi_config->uefi_cfg_entries[i].cfg,
+                                    uefi_config->uefi_cfg_entries[i].size,
+                                    uefi_config->uefi_cfg_entries[i].evt_info);
+       }
+}
+
+asmlinkage __visible void sl_check_region(void *base, u32 size)
+{
+       sl_check_pmr_coverage(base, size, false);
+}
+
+asmlinkage __visible void sl_main(void *bootparams)
+{
+       struct boot_params *bp  = (struct boot_params *)bootparams;
+       struct txt_os_mle_data *os_mle_data;
+       struct slr_table *slrt;
+       void *txt_heap;
+
+       /*
+        * Ensure loadflags do not indicate a secure launch was done
+        * unless it really was.
+        */
+       bp->hdr.loadflags &= ~SLAUNCH_FLAG;
+
+       /*
+        * Currently only Intel TXT is supported for Secure Launch. Testing
+        * this value also indicates that the kernel was booted successfully
+        * through the Secure Launch entry point and is in SMX mode.
+        */
+       if (!(sl_cpu_type & SL_CPU_INTEL))
+               return;
+
+       slrt = sl_locate_and_validate_slrt();
+
+       /* Locate the TPM event log. */
+       sl_find_drtm_event_log(slrt);
+
+       /* Validate the location of the event log buffer before using it */
+       sl_validate_event_log_buffer();
+
+       /*
+        * Find the TPM hash algorithms used by the ACM and recorded in the
+        * event log.
+        */
+       if (tpm_log_ver == SL_TPM20_LOG)
+               sl_find_event_log_algorithms();
+
+       /*
+        * Sanitize them before measuring. Set the SLAUNCH_FLAG early since if
+        * anything fails, the system will reset anyway.
+        */
+       sanitize_boot_params(bp);
+       bp->hdr.loadflags |= SLAUNCH_FLAG;
+
+       sl_check_pmr_coverage(bootparams, PAGE_SIZE, false);
+
+       /* Place event log SL specific tags before and after measurements */
+       sl_tpm_extend_evtlog(17, TXT_EVTYPE_SLAUNCH_START, NULL, 0, "");
+
+       /* Process all policy entries and extend the measurements to the evtlog */
+       sl_process_extend_policy(slrt);
+
+       /* Process all EFI config entries and extend the measurements to the evtlog */
+       sl_process_extend_uefi_config(slrt);
+
+       sl_tpm_extend_evtlog(17, TXT_EVTYPE_SLAUNCH_END, NULL, 0, "");
+
+       /* No PMR check is needed, the TXT heap is covered by the DPR */
+       txt_heap = (void *)sl_txt_read(TXT_CR_HEAP_BASE);
+       os_mle_data = txt_os_mle_data_start(txt_heap);
+
+       /*
+        * Now that the OS-MLE data is measured, ensure the MTRR and
+        * misc enable MSRs are what we expect.
+        */
+       sl_txt_validate_msrs(os_mle_data);
+}
arch/x86/boot/compressed/sl_stub.S new file +72 -0
new file mode 100644
index 000000000000..24b8f23d5dcc
@@ -0,0 +1,725 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Secure Launch protected mode entry point.
+ *
+ * Copyright (c) 2024, Oracle and/or its affiliates.
+ */
+       .code32
+       .text
+#include <linux/linkage.h>
+#include <asm/segment.h>
+#include <asm/msr.h>
+#include <asm/apicdef.h>
+#include <asm/trapnr.h>
+#include <asm/processor-flags.h>
+#include <asm/asm-offsets.h>
+#include <asm/bootparam.h>
+#include <asm/page_types.h>
+#include <asm/irq_vectors.h>
+#include <linux/slr_table.h>
+#include <linux/slaunch.h>
+
+/* CPUID: leaf 1, ECX, SMX feature bit */
+#define X86_FEATURE_BIT_SMX    (1 << 6)
+
+#define IDT_VECTOR_LO_BITS     0
+#define IDT_VECTOR_HI_BITS     6
+
+/*
+ * See the comment in head_64.S for detailed information on what this macro
+ * and others like it are used for. The comment appears right at the top of
+ * the file.
+ */
+#define rva(X) ((X) - sl_stub_entry)
+
+/*
+ * The GETSEC op code is open coded because older versions of
+ * GCC do not support the getsec mnemonic.
+ */
+.macro GETSEC leaf
+       pushl   %ebx
+       xorl    %ebx, %ebx      /* Must be zero for SMCTRL */
+       movl    \leaf, %eax     /* Leaf function */
+       .byte   0x0f, 0x37      /* GETSEC opcode */
+       popl    %ebx
+.endm
+
+.macro TXT_RESET error
+       /*
+        * Set a sticky error value and reset. Note the movs to %eax act as
+        * TXT register barriers.
+        */
+       movl    \error, (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_ERRORCODE)
+       movl    (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_E2STS), %eax
+       movl    $1, (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_CMD_NO_SECRETS)
+       movl    (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_E2STS), %eax
+       movl    $1, (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_CMD_UNLOCK_MEM_CONFIG)
+       movl    (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_E2STS), %eax
+       movl    $1, (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_CMD_RESET)
+1:
+       hlt
+       jmp     1b
+.endm
+
+       .code32
+SYM_FUNC_START(sl_stub_entry)
+       cli
+       cld
+
+       /*
+        * On entry, %ebx has the entry abs offset to sl_stub_entry. This
+        * will be correctly scaled using the rva macro and avoid causing
Quoted 8 lines

arch/x86/boot/compressed/sl_stub.S line 73

Quoted 1 line
arch/x86/boot/compressed/sl_stub.S new file +1 -0
+        * relocations. Only %cs and %ds segments are known good.

Could you please clarify this? 'scaling' is unidiomatic in this context, and actually means something different in my book. AIUI, %ebx is guaranteed to carry the actual address of sl_stub_entry(), and rva() is used to generate relative references using %ebx as a base, as to avoid /absolute/ relocations, which would require fixups at runtime.

Yes, we will reword this to be clearer.

Quoted 728 lines
Quoted 728 lines
arch/x86/boot/compressed/sl_stub.S new file +652 -0
+        */
+
+       /* Load GDT, set segment regs and lret to __SL32_CS */
+       leal    rva(sl_gdt_desc)(%ebx), %eax
+       addl    %eax, 2(%eax)
+       lgdt    (%eax)
+
+       movl    $(__SL32_DS), %eax
+       movw    %ax, %ds
+       movw    %ax, %es
+       movw    %ax, %fs
+       movw    %ax, %gs
+       movw    %ax, %ss
+
+       /*
+        * Now that %ss is known good, take the first stack for the BSP. The
+        * AP stacks are only used on Intel.
+        */
+       leal    rva(sl_stacks_end)(%ebx), %esp
+
+       leal    rva(.Lsl_cs)(%ebx), %eax
+       pushl   $(__SL32_CS)
+       pushl   %eax
+       lret
+
+.Lsl_cs:
+       /* Save our base pointer reg and page table for MLE */
+       pushl   %ebx
+       pushl   %ecx
+
+       /* See if SMX feature is supported. */
+       movl    $1, %eax
+       cpuid
+       testl   $(X86_FEATURE_BIT_SMX), %ecx
+       jz      .Ldo_unknown_cpu
+
+       popl    %ecx
+       popl    %ebx
+
+       /* Know it is Intel */
+       movl    $(SL_CPU_INTEL), rva(sl_cpu_type)(%ebx)
+
+       /* Locate the base of the MLE using the page tables in %ecx */
+       call    sl_find_mle_base
+
+       /* Increment CPU count for BSP */
+       incl    rva(sl_txt_cpu_count)(%ebx)
+
+       /*
+        * Enable SMI with GETSEC[SMCTRL] which were disabled by SENTER.
+        * NMIs were also disabled by SENTER. Since there is no IDT for the BSP,
+        * allow the mainline kernel re-enable them in the normal course of
+        * booting.
+        */
+       GETSEC  $(SMX_X86_GETSEC_SMCTRL)
+
+       /* Clear the TXT error registers for a clean start of day */
+       movl    $0, (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_ERRORCODE)
+       movl    $0xffffffff, (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_ESTS)
+
+       /* On Intel, the zero page address is passed in the TXT heap */
+       /* Read physical base of heap into EAX */
+       movl    (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_HEAP_BASE), %eax
+       /* Read the size of the BIOS data into ECX (first 8 bytes) */
+       movl    (%eax), %ecx
+       /* Skip over BIOS data and size of OS to MLE data section */
+       leal    8(%eax, %ecx), %eax
+
+       /* Need to verify the values in the OS-MLE struct passed in */
+       call    sl_txt_verify_os_mle_struct
+
+       /*
+        * Get the boot params address from the heap. Note %esi and %ebx MUST
+        * be preserved across calls and operations.
+        */
+       movl    SL_boot_params_addr(%eax), %esi
+
+       /* Save %ebx so the APs can find their way home */
+       movl    %ebx, (SL_mle_scratch + SL_SCRATCH_AP_EBX)(%eax)
+
+       /* Fetch the AP wake code block address from the heap */
+       movl    SL_ap_wake_block(%eax), %edi
+       movl    %edi, rva(sl_txt_ap_wake_block)(%ebx)
+
+       /* Store the offset in the AP wake block to the jmp address */
+       movl    $(sl_ap_jmp_offset - sl_txt_ap_wake_begin), \
+               (SL_mle_scratch + SL_SCRATCH_AP_JMP_OFFSET)(%eax)
+
+       /* Store the offset in the AP wake block to the AP stacks block */
+       movl    $(sl_stacks - sl_txt_ap_wake_begin), \
+               (SL_mle_scratch + SL_SCRATCH_AP_STACKS_OFFSET)(%eax)
+
+       /* %eax still is the base of the OS-MLE block, save it */
+       pushl   %eax
+
+       /* Relocate the AP wake code to the safe block */
+       call    sl_txt_reloc_ap_wake
+
+       /*
+        * Wake up all APs that are blocked in the ACM and wait for them to
+        * halt. This should be done before restoring the MTRRs so the ACM is
+        * still properly in WB memory.
+        */
+       call    sl_txt_wake_aps
+
+       /* Restore OS-MLE in %eax */
+       popl    %eax
+
+       /*
+        * %edi is used by this routine to find the MTRRs which are in the SLRT
+        * in the Intel info.
+        */
+       movl    SL_txt_info(%eax), %edi
+       call    sl_txt_load_regs
+
+       jmp     .Lcpu_setup_done
+
+.Ldo_unknown_cpu:
+       /* Non-Intel CPUs are not yet supported */
+       ud2
+
+.Lcpu_setup_done:
+       /*
+        * Don't enable MCE at this point. The kernel will enable
+        * it on the BSP later when it is ready.
+        */
+
+       /* Done, jump to normal 32b pm entry */
+       jmp     startup_32
+SYM_FUNC_END(sl_stub_entry)
+
+SYM_FUNC_START(sl_find_mle_base)
+       /* %ecx has PDPT, get first PD */
+       movl    (%ecx), %eax
+       andl    $(PAGE_MASK), %eax
+       /* Get first PT from first PDE */
+       movl    (%eax), %eax
+       andl    $(PAGE_MASK), %eax
+       /* Get MLE base from first PTE */
+       movl    (%eax), %eax
+       andl    $(PAGE_MASK), %eax
+
+       movl    %eax, rva(sl_mle_start)(%ebx)
+       ret
+SYM_FUNC_END(sl_find_mle_base)
+
+SYM_FUNC_START(sl_check_buffer_mle_overlap)
+       /* %ecx: buffer begin %edx: buffer end */
+       /* %ebx: MLE begin %edi: MLE end */
+       /* %eax: region may be inside MLE */
+
+       cmpl    %edi, %ecx
+       jb      .Lnext_check
+       cmpl    %edi, %edx
+       jbe     .Lnext_check
+       jmp     .Lvalid /* Buffer above MLE */
+
+.Lnext_check:
+       cmpl    %ebx, %edx
+       ja      .Linside_check
+       cmpl    %ebx, %ecx
+       jae     .Linside_check
+       jmp     .Lvalid /* Buffer below MLE */
+
+.Linside_check:
+       cmpl    $0, %eax
+       jz      .Linvalid
+       cmpl    %ebx, %ecx
+       jb      .Linvalid
+       cmpl    %edi, %edx
+       ja      .Linvalid
+       jmp     .Lvalid /* Buffer in MLE */
+
+.Linvalid:
+       TXT_RESET $(SL_ERROR_MLE_BUFFER_OVERLAP)
+
+.Lvalid:
+       ret
+SYM_FUNC_END(sl_check_buffer_mle_overlap)
+
+SYM_FUNC_START(sl_txt_verify_os_mle_struct)
+       pushl   %ebx
+       /*
+        * %eax points to the base of the OS-MLE struct. Need to also
+        * read some values from the OS-SINIT struct too.
+        */
+       movl    -8(%eax), %ecx
+       /* Skip over OS to MLE data section and size of OS-SINIT structure */
+       leal    (%eax, %ecx), %edx
+
+       /* Load MLE image base absolute offset */
+       movl    rva(sl_mle_start)(%ebx), %ebx
+
+       /* Verify the value of the low PMR base. It should always be 0. */
+       movl    SL_vtd_pmr_lo_base(%edx), %esi
+       cmpl    $0, %esi
+       jz      .Lvalid_pmr_base
+       TXT_RESET $(SL_ERROR_LO_PMR_BASE)
+
+.Lvalid_pmr_base:
+       /* Grab some values from OS-SINIT structure */
+       movl    SL_mle_size(%edx), %edi
+       addl    %ebx, %edi
+       jc      .Loverflow_detected
+       movl    SL_vtd_pmr_lo_size(%edx), %esi
+
+       /* Check the AP wake block */
+       movl    SL_ap_wake_block(%eax), %ecx
+       movl    SL_ap_wake_block_size(%eax), %edx
+       addl    %ecx, %edx
+       jc      .Loverflow_detected
+       pushl   %eax
+       xorl    %eax, %eax
+       call    sl_check_buffer_mle_overlap
+       popl    %eax
+       cmpl    %esi, %edx
+       ja      .Lbuffer_beyond_pmr
+
+       /*
+        * Check the boot params. Note during a UEFI boot, the boot
+        * params will be inside the MLE image. Test for this case
+        * in the overlap case.
+        */
+       movl    SL_boot_params_addr(%eax), %ecx
+       movl    $(PAGE_SIZE), %edx
+       addl    %ecx, %edx
+       jc      .Loverflow_detected
+       pushl   %eax
+       movl    $1, %eax
+       call    sl_check_buffer_mle_overlap
+       popl    %eax
+       cmpl    %esi, %edx
+       ja      .Lbuffer_beyond_pmr
+
+       /* Check that the AP wake block is big enough */
+       cmpl    $(sl_txt_ap_wake_end - sl_txt_ap_wake_begin), \
+               SL_ap_wake_block_size(%eax)
+       jae     .Lwake_block_ok
+       TXT_RESET $(SL_ERROR_WAKE_BLOCK_TOO_SMALL)
+
+.Lwake_block_ok:
+       popl    %ebx
+       ret
+
+.Loverflow_detected:
+       TXT_RESET $(SL_ERROR_INTEGER_OVERFLOW)
+
+.Lbuffer_beyond_pmr:
+       TXT_RESET $(SL_ERROR_BUFFER_BEYOND_PMR)
+SYM_FUNC_END(sl_txt_verify_os_mle_struct)
+
+SYM_FUNC_START(sl_txt_ap_entry)
+       cli
+       cld
+       /*
+        * The %cs and %ds segments are known good after waking the AP.
+        * First order of business is to find where we are and
+        * save it in %ebx.
+        */
+
+       /* Read physical base of heap into EAX */
+       movl    (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_HEAP_BASE), %eax
+       /* Read the size of the BIOS data into ECX (first 8 bytes) */
+       movl    (%eax), %ecx
+       /* Skip over BIOS data and size of OS to MLE data section */
+       leal    8(%eax, %ecx), %eax
+
+       /* Saved %ebx from the BSP and stash OS-MLE pointer */
+       movl    (SL_mle_scratch + SL_SCRATCH_AP_EBX)(%eax), %ebx
+
+       /* Save TXT info ptr in %edi for call to sl_txt_load_regs */
+       movl    SL_txt_info(%eax), %edi
+
+       /* Lock and get our stack index */
+       movl    $1, %ecx
+.Lspin:
+       xorl    %eax, %eax
+       lock cmpxchgl   %ecx, rva(sl_txt_spin_lock)(%ebx)
+       pause
+       jnz     .Lspin
+
+       /* Increment the stack index and use the next value inside lock */
+       incl    rva(sl_txt_stack_index)(%ebx)
+       movl    rva(sl_txt_stack_index)(%ebx), %eax
+
+       /* Unlock */
+       movl    $0, rva(sl_txt_spin_lock)(%ebx)
+
+       /* Location of the relocated AP wake block */
+       movl    rva(sl_txt_ap_wake_block)(%ebx), %ecx
+
+       /* Load reloc GDT, set segment regs and lret to __SL32_CS */
+       lgdt    (sl_ap_gdt_desc - sl_txt_ap_wake_begin)(%ecx)
+
+       movl    $(__SL32_DS), %edx
+       movw    %dx, %ds
+       movw    %dx, %es
+       movw    %dx, %fs
+       movw    %dx, %gs
+       movw    %dx, %ss
+
+       /* Load our reloc AP stack */
+       movl    $(TXT_BOOT_STACK_SIZE), %edx
+       mull    %edx
+       leal    (sl_stacks_end - sl_txt_ap_wake_begin)(%ecx), %esp
+       subl    %eax, %esp
+
+       /* Switch to AP code segment */
+       leal    rva(.Lsl_ap_cs)(%ebx), %eax
+       pushl   $(__SL32_CS)
+       pushl   %eax
+       lret
+
+.Lsl_ap_cs:
+       /* Load the relocated AP IDT */
+       lidt    (sl_ap_idt_desc - sl_txt_ap_wake_begin)(%ecx)
+
+       /* Fixup MTRRs and misc enable MSR on APs too */
+       call    sl_txt_load_regs
+
+       /* Enable SMI with GETSEC[SMCTRL] */
+       GETSEC $(SMX_X86_GETSEC_SMCTRL)
+
+       /* IRET-to-self can be used to enable NMIs which SENTER disabled */
+       leal    rva(.Lnmi_enabled_ap)(%ebx), %eax
+       pushfl
+       pushl   $(__SL32_CS)
+       pushl   %eax
+       iret
+
+.Lnmi_enabled_ap:
+       /* Put APs in X2APIC mode like the BSP */
+       movl    $(MSR_IA32_APICBASE), %ecx
+       rdmsr
+       orl     $(XAPIC_ENABLE | X2APIC_ENABLE), %eax
+       wrmsr
+
+       /*
+        * Basically done, increment the CPU count and jump off to the AP
+        * wake block to wait.
+        */
+       lock incl       rva(sl_txt_cpu_count)(%ebx)
+
+       movl    rva(sl_txt_ap_wake_block)(%ebx), %eax
+       jmp     *%eax
+SYM_FUNC_END(sl_txt_ap_entry)
+
+SYM_FUNC_START(sl_txt_reloc_ap_wake)
+       /* Save boot params register */
+       pushl   %esi
+
+       movl    rva(sl_txt_ap_wake_block)(%ebx), %edi
+
+       /* Fixup AP IDT and GDT descriptor before relocating */
+       leal    rva(sl_ap_idt_desc)(%ebx), %eax
+       addl    %edi, 2(%eax)
+       leal    rva(sl_ap_gdt_desc)(%ebx), %eax
+       addl    %edi, 2(%eax)
+
+       /*
+        * Copy the AP wake code and AP GDT/IDT to the protected wake block
+        * provided by the loader. Destination already in %edi.
+        */
+       movl    $(sl_txt_ap_wake_end - sl_txt_ap_wake_begin), %ecx
+       leal    rva(sl_txt_ap_wake_begin)(%ebx), %esi
+       rep movsb
+
+       /* Setup the IDT for the APs to use in the relocation block */
+       movl    rva(sl_txt_ap_wake_block)(%ebx), %ecx
+       addl    $(sl_ap_idt - sl_txt_ap_wake_begin), %ecx
+       xorl    %edx, %edx
+
+       /* Form the default reset vector relocation address */
+       movl    rva(sl_txt_ap_wake_block)(%ebx), %esi
+       addl    $(sl_txt_int_reset - sl_txt_ap_wake_begin), %esi
+
+1:
+       cmpw    $(NR_VECTORS), %dx
+       jz      .Lap_idt_done
+
+       cmpw    $(X86_TRAP_NMI), %dx
+       jz      2f
+
+       /* Load all other fixed vectors with reset handler */
+       movl    %esi, %eax
+       movw    %ax, (IDT_VECTOR_LO_BITS)(%ecx)
+       shrl    $16, %eax
+       movw    %ax, (IDT_VECTOR_HI_BITS)(%ecx)
+       jmp     3f
+
+2:
+       /* Load single wake NMI IPI vector at the relocation address */
+       movl    rva(sl_txt_ap_wake_block)(%ebx), %eax
+       addl    $(sl_txt_int_nmi - sl_txt_ap_wake_begin), %eax
+       movw    %ax, (IDT_VECTOR_LO_BITS)(%ecx)
+       shrl    $16, %eax
+       movw    %ax, (IDT_VECTOR_HI_BITS)(%ecx)
+
+3:
+       incw    %dx
+       addl    $8, %ecx
+       jmp     1b
+
+.Lap_idt_done:
+       popl    %esi
+       ret
+SYM_FUNC_END(sl_txt_reloc_ap_wake)
+
+SYM_FUNC_START(sl_txt_load_regs)
+       /* Save base pointer register */
+       pushl   %ebx
+
+       /*
+        * On Intel, the original variable MTRRs and Misc Enable MSR are
+        * restored on the BSP at early boot. Each AP will also restore
+        * its MTRRs and Misc Enable MSR.
+        */
+       pushl   %edi
+       addl    $(SL_saved_bsp_mtrrs), %edi
+       movl    (%edi), %ebx
+       pushl   %ebx /* default_mem_type lo */
+       addl    $4, %edi
+       movl    (%edi), %ebx
+       pushl   %ebx /* default_mem_type hi */
+       addl    $4, %edi
+       movl    (%edi), %ebx /* mtrr_vcnt lo, don't care about hi part */
+       addl    $8, %edi /* now at MTRR pair array */
+       /* Write the variable MTRRs */
+       movl    $(MSR_MTRRphysBase0), %ecx
+1:
+       cmpl    $0, %ebx
+       jz      2f
+
+       movl    (%edi), %eax /* MTRRphysBaseX lo */
+       addl    $4, %edi
+       movl    (%edi), %edx /* MTRRphysBaseX hi */
+       wrmsr
+       addl    $4, %edi
+       incl    %ecx
+       movl    (%edi), %eax /* MTRRphysMaskX lo */
+       addl    $4, %edi
+       movl    (%edi), %edx /* MTRRphysMaskX hi */
+       wrmsr
+       addl    $4, %edi
+       incl    %ecx
+
+       decl    %ebx
+       jmp     1b
+2:
+       /* Write the default MTRR register */
+       popl    %edx
+       popl    %eax
+       movl    $(MSR_MTRRdefType), %ecx
+       wrmsr
+
+       /* Return to beginning and write the misc enable msr */
+       popl    %edi
+       addl    $(SL_saved_misc_enable_msr), %edi
+       movl    (%edi), %eax /* saved_misc_enable_msr lo */
+       addl    $4, %edi
+       movl    (%edi), %edx /* saved_misc_enable_msr hi */
+       movl    $(MSR_IA32_MISC_ENABLE), %ecx
+       wrmsr
+
+       popl    %ebx
+       ret
+SYM_FUNC_END(sl_txt_load_regs)
+
+SYM_FUNC_START(sl_txt_wake_aps)
+       /* Save boot params register */
+       pushl   %esi
+
+       /* First setup the MLE join structure and load it into TXT reg */
+       leal    rva(sl_gdt)(%ebx), %eax
+       leal    rva(sl_txt_ap_entry)(%ebx), %ecx
+       leal    rva(sl_smx_rlp_mle_join)(%ebx), %edx
+       movl    %eax, SL_rlp_gdt_base(%edx)
+       movl    %ecx, SL_rlp_entry_point(%edx)
+       movl    %edx, (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_MLE_JOIN)
+
+       /* Another TXT heap walk to find various values needed to wake APs */
+       movl    (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_HEAP_BASE), %eax
+       /* At BIOS data size, find the number of logical processors */
+       movl    (SL_num_logical_procs + 8)(%eax), %edx
+       /* Skip over BIOS data */
+       movl    (%eax), %ecx
+       addl    %ecx, %eax
+       /* Skip over OS to MLE */
+       movl    (%eax), %ecx
+       addl    %ecx, %eax
+       /* At OS-SNIT size, get capabilities to know how to wake up the APs */
+       movl    (SL_capabilities + 8)(%eax), %esi
+       /* Skip over OS to SNIT */
+       movl    (%eax), %ecx
+       addl    %ecx, %eax
+       /* At SINIT-MLE size, get the AP wake MONITOR address */
+       movl    (SL_rlp_wakeup_addr + 8)(%eax), %edi
+
+       /* Determine how to wake up the APs */
+       testl   $(1 << TXT_SINIT_MLE_CAP_WAKE_MONITOR), %esi
+       jz      .Lwake_getsec
+
+       /* Wake using MWAIT MONITOR */
+       movl    $1, (%edi)
+       jmp     .Laps_awake
+
+.Lwake_getsec:
+       /* Wake using GETSEC(WAKEUP) */
+       GETSEC  $(SMX_X86_GETSEC_WAKEUP)
+
+.Laps_awake:
+       /*
+        * All of the APs are woken up and rendesvous in the relocated wake
+        * block starting at sl_txt_ap_wake_begin. Wait for all of them to
+        * halt.
+        */
+       pause
+       cmpl    rva(sl_txt_cpu_count)(%ebx), %edx
+       jne     .Laps_awake
+
+       popl    %esi
+       ret
+SYM_FUNC_END(sl_txt_wake_aps)
+
+/* This is the beginning of the relocated AP wake code block */
+       .global sl_txt_ap_wake_begin
+sl_txt_ap_wake_begin:
+
+       /* Get the LAPIC ID for each AP and stash it on the stack */
+       movl    $(MSR_IA32_X2APIC_APICID), %ecx
+       rdmsr
+       pushl   %eax
+
+       /*
+        * Get a pointer to the monitor location on this APs stack to test below
+        * after mwait returns. Currently %esp points to just past the pushed APIC
+        * ID value.
+        */
+       movl    %esp, %eax
+       subl    $(TXT_BOOT_STACK_SIZE - 4), %eax
+       movl    $0, (%eax)
+
+       /* Clear ecx/edx so no invalid extensions or hints are passed to monitor */
+       xorl    %ecx, %ecx
+       xorl    %edx, %edx
+
+       /*
+        * Arm the monitor and wait for it to be poked by he SMP bringup code. The mwait
+        * instruction can return for a number of reasons. Test to see if it returned
+        * because the monitor was written to.
+        */
+       monitor
+
+1:
+       mfence
+       mwait
+       movl    (%eax), %edx
+       testl   %edx, %edx
+       jz      1b
+
+       /*
+        * This is the long absolute jump to the 32b Secure Launch protected mode stub
+        * code in sl_trampoline_start32() in the rmpiggy. The jump address will be
+        * fixed in the SMP boot code when the first AP is brought up. This whole area
+        * is provided and protected in the memory map by the prelaunch code.
+        */
+       .byte   0xea
+sl_ap_jmp_offset:
+       .long   0x00000000
+       .word   __SL32_CS
+
+SYM_FUNC_START(sl_txt_int_nmi)
+       /* NMI context, just IRET */
+       iret
+SYM_FUNC_END(sl_txt_int_nmi)
+
+SYM_FUNC_START(sl_txt_int_reset)
+       TXT_RESET $(SL_ERROR_INV_AP_INTERRUPT)
+SYM_FUNC_END(sl_txt_int_reset)
+
+       .balign 8
+SYM_DATA_START_LOCAL(sl_ap_idt_desc)
+       .word   sl_ap_idt_end - sl_ap_idt - 1           /* Limit */
+       .long   sl_ap_idt - sl_txt_ap_wake_begin        /* Base */
+SYM_DATA_END_LABEL(sl_ap_idt_desc, SYM_L_LOCAL, sl_ap_idt_desc_end)
+
+       .balign 8
+SYM_DATA_START_LOCAL(sl_ap_idt)
+       .rept   NR_VECTORS
+       .word   0x0000          /* Offset 15 to 0 */
+       .word   __SL32_CS       /* Segment selector */
+       .word   0x8e00          /* Present, DPL=0, 32b Vector, Interrupt */
+       .word   0x0000          /* Offset 31 to 16 */
+       .endr
+SYM_DATA_END_LABEL(sl_ap_idt, SYM_L_LOCAL, sl_ap_idt_end)
+
+       .balign 8
+SYM_DATA_START_LOCAL(sl_ap_gdt_desc)
+       .word   sl_ap_gdt_end - sl_ap_gdt - 1
+       .long   sl_ap_gdt - sl_txt_ap_wake_begin
+SYM_DATA_END_LABEL(sl_ap_gdt_desc, SYM_L_LOCAL, sl_ap_gdt_desc_end)
+
+       .balign 8
+SYM_DATA_START_LOCAL(sl_ap_gdt)
+       .quad   0x0000000000000000      /* NULL */
+       .quad   0x00cf9a000000ffff      /* __SL32_CS */
+       .quad   0x00cf92000000ffff      /* __SL32_DS */
+SYM_DATA_END_LABEL(sl_ap_gdt, SYM_L_LOCAL, sl_ap_gdt_end)
+
+       /* Small stacks for BSP and APs to work with */
+       .balign 64
+SYM_DATA_START_LOCAL(sl_stacks)
+       .fill (TXT_MAX_CPUS * TXT_BOOT_STACK_SIZE), 1, 0
+SYM_DATA_END_LABEL(sl_stacks, SYM_L_LOCAL, sl_stacks_end)
+
+/* This is the end of the relocated AP wake code block */
+       .global sl_txt_ap_wake_end
+sl_txt_ap_wake_end:
+
+       .data
+       .balign 8
+SYM_DATA_START_LOCAL(sl_gdt_desc)
+       .word   sl_gdt_end - sl_gdt - 1
+       .long   sl_gdt - sl_gdt_desc
+SYM_DATA_END_LABEL(sl_gdt_desc, SYM_L_LOCAL, sl_gdt_desc_end)
+
+       .balign 8
+SYM_DATA_START_LOCAL(sl_gdt)
+       .quad   0x0000000000000000      /* NULL */
+       .quad   0x00cf9a000000ffff      /* __SL32_CS */
+       .quad   0x00cf92000000ffff      /* __SL32_DS */
+SYM_DATA_END_LABEL(sl_gdt, SYM_L_LOCAL, sl_gdt_end)
+
+       .balign 8
+SYM_DATA_START_LOCAL(sl_smx_rlp_mle_join)
+       .long   sl_gdt_end - sl_gdt - 1 /* GDT limit */
+       .long   0x00000000              /* GDT base */
+       .long   __SL32_CS       /* Seg Sel - CS (DS, ES, SS = seg_sel+8) */
+       .long   0x00000000      /* Entry point physical address */
+SYM_DATA_END(sl_smx_rlp_mle_join)
+
+SYM_DATA(sl_cpu_type, .long 0x00000000)
+
+SYM_DATA(sl_mle_start, .long 0x00000000)
+
+SYM_DATA_LOCAL(sl_txt_spin_lock, .long 0x00000000)
+
+SYM_DATA_LOCAL(sl_txt_stack_index, .long 0x00000000)
+
+SYM_DATA_LOCAL(sl_txt_cpu_count, .long 0x00000000)
+
+SYM_DATA_LOCAL(sl_txt_ap_wake_block, .long 0x00000000)
arch/x86/include/asm/msr-index.h +5 -0
index e022e6eb766c..37f6167f28ba 100644
@@ -348,6 +348,9 @@
  #define MSR_IA32_RTIT_OUTPUT_BASE      0x00000560
  #define MSR_IA32_RTIT_OUTPUT_MASK      0x00000561
 
+#define MSR_MTRRphysBase0              0x00000200
+#define MSR_MTRRphysMask0              0x00000201
+
  #define MSR_MTRRfix64K_00000           0x00000250
  #define MSR_MTRRfix16K_80000           0x00000258
  #define MSR_MTRRfix16K_A0000           0x00000259
@@ -849,6 +852,8 @@
  #define MSR_IA32_APICBASE_ENABLE       (1<<11)
  #define MSR_IA32_APICBASE_BASE         (0xfffff<<12)
 
+#define MSR_IA32_X2APIC_APICID         0x00000802
+
  #define MSR_IA32_UCODE_WRITE           0x00000079
  #define MSR_IA32_UCODE_REV             0x0000008b
 
arch/x86/include/uapi/asm/bootparam.h +1 -0
index 9b82eebd7add..7ce283a22d6b 100644
@@ -12,6 +12,7 @@
  /* loadflags */
  #define LOADED_HIGH    (1<<0)
  #define KASLR_FLAG     (1<<1)
+#define SLAUNCH_FLAG   (1<<2)
  #define QUIET_FLAG     (1<<5)
  #define KEEP_SEGMENTS  (1<<6)
  #define CAN_USE_HEAP   (1<<7)
arch/x86/kernel/asm-offsets.c +20 -1
index a98020bf31bb..925adce6e2c7 100644
@@ -13,6 +13,8 @@
  #include <linux/hardirq.h>
  #include <linux/suspend.h>
  #include <linux/kbuild.h>
+#include <linux/slr_table.h>
+#include <linux/slaunch.h>
  #include <asm/processor.h>
  #include <asm/thread_info.h>
  #include <asm/sigframe.h>
@@ -120,4 +122,22 @@ static void __used common(void)
         OFFSET(ARIA_CTX_rounds, aria_ctx, rounds);
  #endif
 
+#ifdef CONFIG_SECURE_LAUNCH
+       BLANK();
+       OFFSET(SL_txt_info, txt_os_mle_data, txt_info);
+       OFFSET(SL_mle_scratch, txt_os_mle_data, mle_scratch);
+       OFFSET(SL_boot_params_addr, txt_os_mle_data, boot_params_addr);
+       OFFSET(SL_ap_wake_block, txt_os_mle_data, ap_wake_block);
+       OFFSET(SL_ap_wake_block_size, txt_os_mle_data, ap_wake_block_size);
+       OFFSET(SL_saved_misc_enable_msr, slr_entry_intel_info, saved_misc_enable_msr);
+       OFFSET(SL_saved_bsp_mtrrs, slr_entry_intel_info, saved_bsp_mtrrs);
+       OFFSET(SL_num_logical_procs, txt_bios_data, num_logical_procs);
+       OFFSET(SL_capabilities, txt_os_sinit_data, capabilities);
+       OFFSET(SL_mle_size, txt_os_sinit_data, mle_size);
+       OFFSET(SL_vtd_pmr_lo_base, txt_os_sinit_data, vtd_pmr_lo_base);
+       OFFSET(SL_vtd_pmr_lo_size, txt_os_sinit_data, vtd_pmr_lo_size);
+       OFFSET(SL_rlp_wakeup_addr, txt_sinit_mle_data, rlp_wakeup_addr);
+       OFFSET(SL_rlp_gdt_base, smx_rlp_mle_join, rlp_gdt_base);
+       OFFSET(SL_rlp_entry_point, smx_rlp_mle_join, rlp_entry_point);
+#endif
  }
--

2.39.3

maintainer
#81 lore raw

Re: [PATCH v9 08/19] x86: Secure Launch kernel early boot stub

In reply to Ross Philipson
Quoted 381 lines · Ross Philipson

The Secure Launch (SL) stub provides the entry point for Intel TXT (and later AMD SKINIT) to vector to during the late launch. The symbol sl_stub_entry is that entry point and its offset into the kernel is conveyed to the launching code using the MLE (Measured Launch Environment) header in the structure named mle_header. The offset of the MLE header is set in the kernel_info. The routine sl_stub contains the very early late launch setup code responsible for setting up the basic environment to allow the normal kernel startup_32 code to proceed. It is also responsible for properly waking and handling the APs on Intel platforms. The routine sl_main which runs after entering 64b mode is responsible for measuring configuration and module information before it is used like the boot params, the kernel command line, the TXT heap, an external initramfs, etc.

---
 Documentation/arch/x86/boot.rst        |  21 +
 arch/x86/boot/compressed/Makefile      |   3 +-
 arch/x86/boot/compressed/head_64.S     |  30 +
 arch/x86/boot/compressed/kernel_info.S |  34 ++
 arch/x86/boot/compressed/sl_main.c     | 577 ++++++++++++++++++++
 arch/x86/boot/compressed/sl_stub.S     | 725 +++++++++++++++++++++++++
 arch/x86/include/asm/msr-index.h       |   5 +
 arch/x86/include/uapi/asm/bootparam.h  |   1 +
 arch/x86/kernel/asm-offsets.c          |  20 +
 9 files changed, 1415 insertions(+), 1 deletion(-)
 create mode 100644 arch/x86/boot/compressed/sl_main.c
 create mode 100644 arch/x86/boot/compressed/sl_stub.S
Documentation/arch/x86/boot.rst +21 -0
index 4fd492cb4970..295cdf9bcbdb 100644
@@ -482,6 +482,14 @@ Protocol:	2.00+
 	    - If 1, KASLR enabled.
 	    - If 0, KASLR disabled.
 
+  Bit 2 (kernel internal): SLAUNCH_FLAG
+
+	- Used internally by the setup kernel to communicate
+	  Secure Launch status to kernel proper.
+
+	    - If 1, Secure Launch enabled.
+	    - If 0, Secure Launch disabled.
+
   Bit 5 (write): QUIET_FLAG
 
 	- If 0, print early messages.
@@ -1028,6 +1036,19 @@ Offset/size:	0x000c/4
 
   This field contains maximal allowed type for setup_data and setup_indirect structs.
 
+============	=================
+Field name:	mle_header_offset
+Offset/size:	0x0010/4
+============	=================
+
+  This field contains the offset to the Secure Launch Measured Launch Environment
+  (MLE) header. This offset is used to locate information needed during a secure
+  late launch using Intel TXT. If the offset is zero, the kernel does not have
+  Secure Launch capabilities. The MLE entry point is called from TXT on the BSP
+  following a success measured launch. The specific state of the processors is
+  outlined in the TXT Software Development Guide, the latest can be found here:
+  https://www.intel.com/content/dam/www/public/us/en/documents/guides/intel-txt-software-development-guide.pdf
+
 
 The Image Checksum
 ==================
arch/x86/boot/compressed/Makefile +2 -1
index 9189a0e28686..9076a248d4b4 100644
@@ -118,7 +118,8 @@ vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi.o
 vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_mixed.o
 vmlinux-objs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
 
-vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o $(obj)/early_sha256.o
+vmlinux-objs-$(CONFIG_SECURE_LAUNCH) += $(obj)/early_sha1.o $(obj)/early_sha256.o \
+	$(obj)/sl_main.o $(obj)/sl_stub.o
 
 $(obj)/vmlinux: $(vmlinux-objs-y) FORCE
 	$(call if_changed,ld)
arch/x86/boot/compressed/head_64.S +30 -0
index 1dcb794c5479..803c9e2e6d85 100644
@@ -420,6 +420,13 @@ SYM_CODE_START(startup_64)
 	pushq	$0
 	popfq
 
+#ifdef CONFIG_SECURE_LAUNCH
+	/* Ensure the relocation region is coverd by a PMR */
+	movq	%rbx, %rdi
+	movl	$(_bss - startup_32), %esi
+	callq	sl_check_region
+#endif
+
 /*
  * Copy the compressed kernel to the end of our buffer
  * where decompression in place becomes safe.
@@ -462,6 +469,29 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated)
 	shrq	$3, %rcx
 	rep	stosq
 
+#ifdef CONFIG_SECURE_LAUNCH
+	/*
+	 * Have to do the final early sl stub work in 64b area.
+	 *
+	 * *********** NOTE ***********
+	 *
+	 * Several boot params get used before we get a chance to measure
+	 * them in this call. This is a known issue and we currently don't
+	 * have a solution. The scratch field doesn't matter. There is no
+	 * obvious way to do anything about the use of kernel_alignment or
+	 * init_size though these seem low risk with all the PMR and overlap
+	 * checks in place.
+	 */
+	movq	%r15, %rdi
+	callq	sl_main
+
+	/* Ensure the decompression location is covered by a PMR */
+	movq	%rbp, %rdi
+	movq	output_len(%rip), %rsi
+	callq	sl_check_region
+#endif
+
+	pushq	%rsi
 	call	load_stage2_idt
 
 	/* Pass boot_params to initialize_identity_maps() */
arch/x86/boot/compressed/kernel_info.S +34 -0
index c18f07181dd5..e199b87764e9 100644
@@ -28,6 +28,40 @@ SYM_DATA_START(kernel_info)
 	/* Maximal allowed type for setup_data and setup_indirect structs. */
 	.long	SETUP_TYPE_MAX
 
+	/* Offset to the MLE header structure */
+#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
+	.long	rva(mle_header)
+#else
+	.long	0
+#endif
+
 kernel_info_var_len_data:
 	/* Empty for time being... */
 SYM_DATA_END_LABEL(kernel_info, SYM_L_LOCAL, kernel_info_end)
+
+#if IS_ENABLED(CONFIG_SECURE_LAUNCH)
+	/*
+	 * The MLE Header per the TXT Specification, section 2.1
+	 * MLE capabilities, see table 4. Capabilities set:
+	 * bit 0: Support for GETSEC[WAKEUP] for RLP wakeup
+	 * bit 1: Support for RLP wakeup using MONITOR address
+	 * bit 2: The ECX register will contain the pointer to the MLE page table
+	 * bit 5: TPM 1.2 family: Details/authorities PCR usage support
+	 * bit 9: Supported format of TPM 2.0 event log - TCG compliant
+	 */
+SYM_DATA_START(mle_header)
+	.long	0x9082ac5a  /* UUID0 */
+	.long	0x74a7476f  /* UUID1 */
+	.long	0xa2555c0f  /* UUID2 */
+	.long	0x42b651cb  /* UUID3 */
+	.long	0x00000034  /* MLE header size */
+	.long	0x00020002  /* MLE version 2.2 */
+	.long	rva(sl_stub_entry) /* Linear entry point of MLE (virt. address) */
+	.long	0x00000000  /* First valid page of MLE */
+	.long	0x00000000  /* Offset within binary of first byte of MLE */
+	.long	rva(_edata) /* Offset within binary of last byte + 1 of MLE */
+	.long	0x00000227  /* Bit vector of MLE-supported capabilities */
+	.long	0x00000000  /* Starting linear address of command line (unused) */
+	.long	0x00000000  /* Ending linear address of command line (unused) */
+SYM_DATA_END(mle_header)
+#endif
arch/x86/boot/compressed/sl_main.c new file +200 -0
new file mode 100644
index 000000000000..61e9baf410fd
@@ -0,0 +1,577 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Secure Launch early measurement and validation routines.
+ *
+ * Copyright (c) 2024, Oracle and/or its affiliates.
+ */
+
+#include <linux/init.h>
+#include <linux/string.h>
+#include <linux/linkage.h>
+#include <asm/segment.h>
+#include <asm/boot.h>
+#include <asm/msr.h>
+#include <asm/mtrr.h>
+#include <asm/processor-flags.h>
+#include <asm/asm-offsets.h>
+#include <asm/bootparam.h>
+#include <asm/bootparam_utils.h>
+#include <linux/slr_table.h>
+#include <linux/slaunch.h>
+#include <crypto/sha1.h>
+#include <crypto/sha2.h>
+
+#define CAPS_VARIABLE_MTRR_COUNT_MASK	0xff
+
+#define SL_TPM12_LOG		1
+#define SL_TPM20_LOG		2
+
+#define SL_TPM20_MAX_ALGS	2
+
+#define SL_MAX_EVENT_DATA	64
+#define SL_TPM12_LOG_SIZE	(sizeof(struct tcg_pcr_event) + \
+				SL_MAX_EVENT_DATA)
+#define SL_TPM20_LOG_SIZE	(sizeof(struct tcg_pcr_event2_head) + \
+				SHA1_DIGEST_SIZE + SHA256_DIGEST_SIZE + \
+				sizeof(struct tcg_event_field) + \
+				SL_MAX_EVENT_DATA)
+
+static void *evtlog_base;
+static u32 evtlog_size;
+static struct txt_heap_event_log_pointer2_1_element *log20_elem;
+static u32 tpm_log_ver = SL_TPM12_LOG;
+static struct tcg_efi_specid_event_algs tpm_algs[SL_TPM20_MAX_ALGS] = {0};
+
+extern u32 sl_cpu_type;
+extern u32 sl_mle_start;
+
+static u64 sl_txt_read(u32 reg)
+{
+	return readq((void *)(u64)(TXT_PRIV_CONFIG_REGS_BASE + reg));
+}
+
+static void sl_txt_write(u32 reg, u64 val)
+{
+	writeq(val, (void *)(u64)(TXT_PRIV_CONFIG_REGS_BASE + reg));
+}
+
+static void __noreturn sl_txt_reset(u64 error)
+{
+	/* Reading the E2STS register acts as a barrier for TXT registers */
+	sl_txt_write(TXT_CR_ERRORCODE, error);
+	sl_txt_read(TXT_CR_E2STS);
+	sl_txt_write(TXT_CR_CMD_UNLOCK_MEM_CONFIG, 1);
+	sl_txt_read(TXT_CR_E2STS);
+	sl_txt_write(TXT_CR_CMD_RESET, 1);
+
+	for ( ; ; )
+		asm volatile ("hlt");
+
+	unreachable();
+}
+
+static u64 sl_rdmsr(u32 reg)
+{
+	u64 lo, hi;
+
+	asm volatile ("rdmsr" : "=a" (lo), "=d" (hi) : "c" (reg));
+
+	return (hi << 32) | lo;
+}
+
+static struct slr_table *sl_locate_and_validate_slrt(void)
+{
+	struct txt_os_mle_data *os_mle_data;
+	struct slr_table *slrt;
+	void *txt_heap;
+
+	txt_heap = (void *)sl_txt_read(TXT_CR_HEAP_BASE);
+	os_mle_data = txt_os_mle_data_start(txt_heap);
+
+	if (!os_mle_data->slrt)
+		sl_txt_reset(SL_ERROR_INVALID_SLRT);
+
+	slrt = (struct slr_table *)os_mle_data->slrt;
+
+	if (slrt->magic != SLR_TABLE_MAGIC)
+		sl_txt_reset(SL_ERROR_INVALID_SLRT);
+
+	if (slrt->architecture != SLR_INTEL_TXT)
+		sl_txt_reset(SL_ERROR_INVALID_SLRT);
+
+	return slrt;
+}
+
+static void sl_check_pmr_coverage(void *base, u32 size, bool allow_hi)
+{
+	struct txt_os_sinit_data *os_sinit_data;
+	void *end = base + size;
+	void *txt_heap;
+
+	if (!(sl_cpu_type & SL_CPU_INTEL))
+		return;
+
+	txt_heap = (void *)sl_txt_read(TXT_CR_HEAP_BASE);
+	os_sinit_data = txt_os_sinit_data_start(txt_heap);
+
+	if ((end >= (void *)0x100000000ULL) && (base < (void *)0x100000000ULL))
+		sl_txt_reset(SL_ERROR_REGION_STRADDLE_4GB);
+
+	/*
+	 * Note that the late stub code validates that the hi PMR covers
+	 * all memory above 4G. At this point the code can only check that
+	 * regions are within the hi PMR but that is sufficient.
+	 */
+	if ((end > (void *)0x100000000ULL) && (base >= (void *)0x100000000ULL)) {
+		if (allow_hi) {
+			if (end >= (void *)(os_sinit_data->vtd_pmr_hi_base +
+					   os_sinit_data->vtd_pmr_hi_size))
+				sl_txt_reset(SL_ERROR_BUFFER_BEYOND_PMR);
+		} else {
+			sl_txt_reset(SL_ERROR_REGION_ABOVE_4GB);
+		}
+	}
+
+	if (end >= (void *)os_sinit_data->vtd_pmr_lo_size)
+		sl_txt_reset(SL_ERROR_BUFFER_BEYOND_PMR);
+}
+
+/*
+ * Some MSRs are modified by the pre-launch code including the MTRRs.
+ * The early MLE code has to restore these values. This code validates
+ * the values after they are measured.
+ */
+static void sl_txt_validate_msrs(struct txt_os_mle_data *os_mle_data)
+{
+	struct slr_txt_mtrr_state *saved_bsp_mtrrs;
+	u64 mtrr_caps, mtrr_def_type, mtrr_var;
+	struct slr_entry_intel_info *txt_info;
+	u64 misc_en_msr;
+	u32 vcnt, i;
+
+	txt_info = (struct slr_entry_intel_info *)os_mle_data->txt_info;
+	saved_bsp_mtrrs = &txt_info->saved_bsp_mtrrs;
+
+	mtrr_caps = sl_rdmsr(MSR_MTRRcap);
+	vcnt = (u32)(mtrr_caps & CAPS_VARIABLE_MTRR_COUNT_MASK);
+
+	if (saved_bsp_mtrrs->mtrr_vcnt > vcnt)
+		sl_txt_reset(SL_ERROR_MTRR_INV_VCNT);
+	if (saved_bsp_mtrrs->mtrr_vcnt > TXT_OS_MLE_MAX_VARIABLE_MTRRS)
+		sl_txt_reset(SL_ERROR_MTRR_INV_VCNT);
+
+	mtrr_def_type = sl_rdmsr(MSR_MTRRdefType);
+	if (saved_bsp_mtrrs->default_mem_type != mtrr_def_type)
+		sl_txt_reset(SL_ERROR_MTRR_INV_DEF_TYPE);
+
+	for (i = 0; i < saved_bsp_mtrrs->mtrr_vcnt; i++) {
+		mtrr_var = sl_rdmsr(MTRRphysBase_MSR(i));
+		if (saved_bsp_mtrrs->mtrr_pair[i].mtrr_physbase != mtrr_var)
+			sl_txt_reset(SL_ERROR_MTRR_INV_BASE);
+		mtrr_var = sl_rdmsr(MTRRphysMask_MSR(i));
+		if (saved_bsp_mtrrs->mtrr_pair[i].mtrr_physmask != mtrr_var)
+			sl_txt_reset(SL_ERROR_MTRR_INV_MASK);
+	}
+
+	misc_en_msr = sl_rdmsr(MSR_IA32_MISC_ENABLE);
+	if (txt_info->saved_misc_enable_msr != misc_en_msr)
+		sl_txt_reset(SL_ERROR_MSR_INV_MISC_EN);
+}
+
+static void sl_find_drtm_event_log(struct slr_table *slrt)
+{
+	struct txt_os_sinit_data *os_sinit_data;
+	struct slr_entry_log_info *log_info;
+	void *txt_heap;
+
+	log_info = slr_next_entry_by_tag(slrt, NULL, SLR_ENTRY_LOG_INFO);
+	if (!log_info)
+		sl_txt_reset(SL_ERROR_SLRT_MISSING_ENTRY);
+
+	evtlog_base = (void *)log_info->addr;
+	evtlog_size = log_info->size;
+
+	txt_heap = (void *)sl_txt_read(TXT_CR_HEAP_BASE);
+
+	/*
+	 * For TPM 2.0, the event log 2.1 extended data structure has to also
+	 * be located and fixed up.
+	 */
+	os_sinit_data = txt_os_sinit_data_start(txt_heap);

arch/x86/boot/compressed/sl_main.c line 210

Quoted 10 lines
arch/x86/boot/compressed/sl_main.c new file +10 -0
+
+	/*
+	 * Only support version 6 and later that properly handle the
+	 * list of ExtDataElements in the OS-SINIT structure.
+	 */
+	if (os_sinit_data->version < 6)
+		sl_txt_reset(SL_ERROR_OS_SINIT_BAD_VERSION);
+
+	/* Find the TPM2.0 logging extended heap element */
+	log20_elem = tpm20_find_log2_1_element(os_sinit_data);

s/tpm20/tpm2/

Quoted 340 lines
arch/x86/boot/compressed/sl_main.c new file +340 -0
+
+	/* If found, this implies TPM20 log and family */
+	if (log20_elem)
+		tpm_log_ver = SL_TPM20_LOG;
+}
+
+static void sl_validate_event_log_buffer(void)
+{
+	struct txt_os_sinit_data *os_sinit_data;
+	void *txt_heap, *txt_end;
+	void *mle_base, *mle_end;
+	void *evtlog_end;
+
+	if ((u64)evtlog_size > (LLONG_MAX - (u64)evtlog_base))
+		sl_txt_reset(SL_ERROR_INTEGER_OVERFLOW);
+	evtlog_end = evtlog_base + evtlog_size;
+
+	txt_heap = (void *)sl_txt_read(TXT_CR_HEAP_BASE);
+	txt_end = txt_heap + sl_txt_read(TXT_CR_HEAP_SIZE);
+	os_sinit_data = txt_os_sinit_data_start(txt_heap);
+
+	mle_base = (void *)(u64)sl_mle_start;
+	mle_end = mle_base + os_sinit_data->mle_size;
+
+	/*
+	 * This check is to ensure the event log buffer does not overlap with
+	 * the MLE image.
+	 */
+	if (evtlog_base >= mle_end && evtlog_end > mle_end)
+		goto pmr_check; /* above */
+
+	if (evtlog_end <= mle_base && evtlog_base < mle_base)
+		goto pmr_check; /* below */
+
+	sl_txt_reset(SL_ERROR_MLE_BUFFER_OVERLAP);
+
+pmr_check:
+	/*
+	 * The TXT heap is protected by the DPR. If the TPM event log is
+	 * inside the TXT heap, there is no need for a PMR check.
+	 */
+	if (evtlog_base > txt_heap && evtlog_end < txt_end)
+		return;
+
+	sl_check_pmr_coverage(evtlog_base, evtlog_size, true);
+}
+
+static void sl_find_event_log_algorithms(void)
+{
+	struct tcg_efi_specid_event_head *efi_head =
+		(struct tcg_efi_specid_event_head *)(evtlog_base +
+					log20_elem->first_record_offset +
+					sizeof(struct tcg_pcr_event));
+
+	if (efi_head->num_algs == 0 || efi_head->num_algs > 2)
+		sl_txt_reset(SL_ERROR_TPM_NUMBER_ALGS);
+
+	memcpy(&tpm_algs[0], &efi_head->digest_sizes[0],
+	       sizeof(struct tcg_efi_specid_event_algs) * efi_head->num_algs);
+}
+
+static void sl_tpm12_log_event(u32 pcr, u32 event_type,
+			       const u8 *data, u32 length,
+			       const u8 *event_data, u32 event_size)
+{
+	u8 sha1_hash[SHA1_DIGEST_SIZE] = {0};
+	u8 log_buf[SL_TPM12_LOG_SIZE] = {0};
+	struct tcg_pcr_event *pcr_event;
+	u32 total_size;
+
+	pcr_event = (struct tcg_pcr_event *)log_buf;
+	pcr_event->pcr_idx = pcr;
+	pcr_event->event_type = event_type;
+	if (length > 0) {
+		sha1(data, length, &sha1_hash[0]);
+		memcpy(&pcr_event->digest[0], &sha1_hash[0], SHA1_DIGEST_SIZE);
+	}
+	pcr_event->event_size = event_size;
+	if (event_size > 0)
+		memcpy((u8 *)pcr_event + sizeof(struct tcg_pcr_event),
+		       event_data, event_size);
+
+	total_size = sizeof(struct tcg_pcr_event) + event_size;
+
+	if (tpm12_log_event(evtlog_base, evtlog_size, total_size, pcr_event))
+		sl_txt_reset(SL_ERROR_TPM_LOGGING_FAILED);
+}
+
+static void sl_tpm20_log_event(u32 pcr, u32 event_type,
+			       const u8 *data, u32 length,
+			       const u8 *event_data, u32 event_size)
+{
+	u8 sha256_hash[SHA256_DIGEST_SIZE] = {0};
+	u8 sha1_hash[SHA1_DIGEST_SIZE] = {0};
+	u8 log_buf[SL_TPM20_LOG_SIZE] = {0};
+	struct sha256_state sctx256 = {0};
+	struct tcg_pcr_event2_head *head;
+	struct tcg_event_field *event;
+	u32 total_size;
+	u16 *alg_ptr;
+	u8 *dgst_ptr;
+
+	head = (struct tcg_pcr_event2_head *)log_buf;
+	head->pcr_idx = pcr;
+	head->event_type = event_type;
+	total_size = sizeof(struct tcg_pcr_event2_head);
+	alg_ptr = (u16 *)(log_buf + sizeof(struct tcg_pcr_event2_head));
+
+	for ( ; head->count < 2; head->count++) {
+		if (!tpm_algs[head->count].alg_id)
+			break;
+
+		*alg_ptr = tpm_algs[head->count].alg_id;
+		dgst_ptr = (u8 *)alg_ptr + sizeof(u16);
+
+		if (tpm_algs[head->count].alg_id == TPM_ALG_SHA256 &&
+		    length) {
+			sha256_init(&sctx256);
+			sha256_update(&sctx256, data, length);
+			sha256_final(&sctx256, &sha256_hash[0]);
+		} else if (tpm_algs[head->count].alg_id == TPM_ALG_SHA1 &&
+			   length) {
+			sha1(data, length, &sha1_hash[0]);
+		}
+
+		if (tpm_algs[head->count].alg_id == TPM_ALG_SHA256) {
+			memcpy(dgst_ptr, &sha256_hash[0], SHA256_DIGEST_SIZE);
+			total_size += SHA256_DIGEST_SIZE + sizeof(u16);
+			alg_ptr = (u16 *)((u8 *)alg_ptr + SHA256_DIGEST_SIZE + sizeof(u16));
+		} else if (tpm_algs[head->count].alg_id == TPM_ALG_SHA1) {
+			memcpy(dgst_ptr, &sha1_hash[0], SHA1_DIGEST_SIZE);
+			total_size += SHA1_DIGEST_SIZE + sizeof(u16);
+			alg_ptr = (u16 *)((u8 *)alg_ptr + SHA1_DIGEST_SIZE + sizeof(u16));
+		} else {
+			sl_txt_reset(SL_ERROR_TPM_UNKNOWN_DIGEST);
+		}
+	}
+
+	event = (struct tcg_event_field *)(log_buf + total_size);
+	event->event_size = event_size;
+	if (event_size > 0)
+		memcpy((u8 *)event + sizeof(struct tcg_event_field), event_data, event_size);
+	total_size += sizeof(struct tcg_event_field) + event_size;
+
+	if (tpm20_log_event(log20_elem, evtlog_base, evtlog_size, total_size, &log_buf[0]))
+		sl_txt_reset(SL_ERROR_TPM_LOGGING_FAILED);
+}
+
+static void sl_tpm_extend_evtlog(u32 pcr, u32 type,
+				 const u8 *data, u32 length, const char *desc)
+{
+	if (tpm_log_ver == SL_TPM20_LOG)
+		sl_tpm20_log_event(pcr, type, data, length,
+				   (const u8 *)desc, strlen(desc));
+	else
+		sl_tpm12_log_event(pcr, type, data, length,
+				   (const u8 *)desc, strlen(desc));
+}
+
+static struct setup_data *sl_handle_setup_data(struct setup_data *curr,
+					       struct slr_policy_entry *entry)
+{
+	struct setup_indirect *ind;
+	struct setup_data *next;
+
+	if (!curr)
+		return NULL;
+
+	next = (struct setup_data *)(unsigned long)curr->next;
+
+	/* SETUP_INDIRECT instances have to be handled differently */
+	if (curr->type == SETUP_INDIRECT) {
+		ind = (struct setup_indirect *)((u8 *)curr + offsetof(struct setup_data, data));
+
+		sl_check_pmr_coverage((void *)ind->addr, ind->len, true);
+
+		sl_tpm_extend_evtlog(entry->pcr, TXT_EVTYPE_SLAUNCH,
+				     (void *)ind->addr, ind->len,
+				     entry->evt_info);
+
+		return next;
+	}
+
+	sl_check_pmr_coverage(((u8 *)curr) + sizeof(struct setup_data),
+			      curr->len, true);
+
+	sl_tpm_extend_evtlog(entry->pcr, TXT_EVTYPE_SLAUNCH,
+			     ((u8 *)curr) + sizeof(struct setup_data),
+			     curr->len,
+			     entry->evt_info);
+
+	return next;
+}
+
+static void sl_extend_setup_data(struct slr_policy_entry *entry)
+{
+	struct setup_data *data;
+
+	/*
+	 * Measuring the boot params measured the fixed e820 memory map.
+	 * Measure any setup_data entries including e820 extended entries.
+	 */
+	data = (struct setup_data *)(unsigned long)entry->entity;
+	while (data)
+		data = sl_handle_setup_data(data, entry);
+}
+
+static void sl_extend_slrt(struct slr_policy_entry *entry)
+{
+	struct slr_table *slrt = (struct slr_table *)entry->entity;
+	struct slr_entry_intel_info *intel_info;
+
+	/*
+	 * In revision one of the SLRT, the only table that needs to be
+	 * measured is the Intel info table. Everything else is meta-data,
+	 * addresses and sizes. Note the size of what to measure is not set.
+	 * The flag SLR_POLICY_IMPLICIT_SIZE leaves it to the measuring code
+	 * to sort out.
+	 */
+	if (slrt->revision == 1) {
+		intel_info = slr_next_entry_by_tag(slrt, NULL, SLR_ENTRY_INTEL_INFO);
+		if (!intel_info)
+			sl_txt_reset(SL_ERROR_SLRT_MISSING_ENTRY);
+
+		sl_tpm_extend_evtlog(entry->pcr, TXT_EVTYPE_SLAUNCH,
+				     (void *)entry->entity, sizeof(struct slr_entry_intel_info),
+				     entry->evt_info);
+	}
+}
+
+static void sl_extend_txt_os2mle(struct slr_policy_entry *entry)
+{
+	struct txt_os_mle_data *os_mle_data;
+	void *txt_heap;
+
+	txt_heap = (void *)sl_txt_read(TXT_CR_HEAP_BASE);
+	os_mle_data = txt_os_mle_data_start(txt_heap);
+
+	/*
+	 * Version 1 of the OS-MLE heap structure has no fields to measure. It just
+	 * has addresses and sizes and a scratch buffer.
+	 */
+	if (os_mle_data->version == 1)
+		return;
+}
+
+static void sl_process_extend_policy(struct slr_table *slrt)
+{
+	struct slr_entry_policy *policy;
+	u16 i;
+
+	policy = slr_next_entry_by_tag(slrt, NULL, SLR_ENTRY_ENTRY_POLICY);
+	if (!policy)
+		sl_txt_reset(SL_ERROR_SLRT_MISSING_ENTRY);
+
+	for (i = 0; i < policy->nr_entries; i++) {
+		switch (policy->policy_entries[i].entity_type) {
+		case SLR_ET_SETUP_DATA:
+			sl_extend_setup_data(&policy->policy_entries[i]);
+			break;
+		case SLR_ET_SLRT:
+			sl_extend_slrt(&policy->policy_entries[i]);
+			break;
+		case SLR_ET_TXT_OS2MLE:
+			sl_extend_txt_os2mle(&policy->policy_entries[i]);
+			break;
+		case SLR_ET_UNUSED:
+			continue;
+		default:
+			sl_tpm_extend_evtlog(policy->policy_entries[i].pcr, TXT_EVTYPE_SLAUNCH,
+					     (void *)policy->policy_entries[i].entity,
+					     policy->policy_entries[i].size,
+					     policy->policy_entries[i].evt_info);
+		}
+	}
+}
+
+static void sl_process_extend_uefi_config(struct slr_table *slrt)
+{
+	struct slr_entry_uefi_config *uefi_config;
+	u16 i;
+
+	uefi_config = slr_next_entry_by_tag(slrt, NULL, SLR_ENTRY_UEFI_CONFIG);
+
+	/* Optionally here depending on how SL kernel was booted */
+	if (!uefi_config)
+		return;
+
+	for (i = 0; i < uefi_config->nr_entries; i++) {
+		sl_tpm_extend_evtlog(uefi_config->uefi_cfg_entries[i].pcr, TXT_EVTYPE_SLAUNCH,
+				     (void *)uefi_config->uefi_cfg_entries[i].cfg,
+				     uefi_config->uefi_cfg_entries[i].size,
+				     uefi_config->uefi_cfg_entries[i].evt_info);
+	}
+}
+
+asmlinkage __visible void sl_check_region(void *base, u32 size)
+{
+	sl_check_pmr_coverage(base, size, false);
+}
+
+asmlinkage __visible void sl_main(void *bootparams)
+{
+	struct boot_params *bp  = (struct boot_params *)bootparams;
+	struct txt_os_mle_data *os_mle_data;
+	struct slr_table *slrt;
+	void *txt_heap;
+
+	/*
+	 * Ensure loadflags do not indicate a secure launch was done
+	 * unless it really was.
+	 */
+	bp->hdr.loadflags &= ~SLAUNCH_FLAG;
+
+	/*
+	 * Currently only Intel TXT is supported for Secure Launch. Testing
+	 * this value also indicates that the kernel was booted successfully
+	 * through the Secure Launch entry point and is in SMX mode.
+	 */
+	if (!(sl_cpu_type & SL_CPU_INTEL))
+		return;
+
+	slrt = sl_locate_and_validate_slrt();
+
+	/* Locate the TPM event log. */
+	sl_find_drtm_event_log(slrt);
+
+	/* Validate the location of the event log buffer before using it */
+	sl_validate_event_log_buffer();
+
+	/*
+	 * Find the TPM hash algorithms used by the ACM and recorded in the
+	 * event log.
+	 */
+	if (tpm_log_ver == SL_TPM20_LOG)
+		sl_find_event_log_algorithms();
+
+	/*
+	 * Sanitize them before measuring. Set the SLAUNCH_FLAG early since if
+	 * anything fails, the system will reset anyway.

arch/x86/boot/compressed/sl_main.c line 560

Quoted 10 lines
arch/x86/boot/compressed/sl_main.c new file +10 -0
+	 */
+	sanitize_boot_params(bp);
+	bp->hdr.loadflags |= SLAUNCH_FLAG;
+
+	sl_check_pmr_coverage(bootparams, PAGE_SIZE, false);
+
+	/* Place event log SL specific tags before and after measurements */
+	sl_tpm_extend_evtlog(17, TXT_EVTYPE_SLAUNCH_START, NULL, 0, "");
+
+	/* Process all policy entries and extend the measurements to the evtlog */

These comments obfuscate code here but would make a lot more sense in the beginning of each corresponding function.

/*
* Process all policy entries and extend the measurements to the evtlog */
static void sl_process_extend_policy(struct slr_table *slrt)

{
	/* ... */
}

BTW what good that "process" does here? Why not just sl_extend_policy()?

arch/x86/boot/compressed/sl_main.c line 564

Quoted 4 lines
arch/x86/boot/compressed/sl_main.c new file +4 -0
+	sl_process_extend_policy(slrt);
+
+	/* Process all EFI config entries and extend the measurements to the evtlog */
+	sl_process_extend_uefi_config(slrt);

Ditto.

Quoted 759 lines
arch/x86/boot/compressed/sl_main.c new file +13 -0
+
+	sl_tpm_extend_evtlog(17, TXT_EVTYPE_SLAUNCH_END, NULL, 0, "");
+
+	/* No PMR check is needed, the TXT heap is covered by the DPR */
+	txt_heap = (void *)sl_txt_read(TXT_CR_HEAP_BASE);
+	os_mle_data = txt_os_mle_data_start(txt_heap);
+
+	/*
+	 * Now that the OS-MLE data is measured, ensure the MTRR and
+	 * misc enable MSRs are what we expect.
+	 */
+	sl_txt_validate_msrs(os_mle_data);
+}
arch/x86/boot/compressed/sl_stub.S new file +725 -0
new file mode 100644
index 000000000000..24b8f23d5dcc
@@ -0,0 +1,725 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Secure Launch protected mode entry point.
+ *
+ * Copyright (c) 2024, Oracle and/or its affiliates.
+ */
+	.code32
+	.text
+#include <linux/linkage.h>
+#include <asm/segment.h>
+#include <asm/msr.h>
+#include <asm/apicdef.h>
+#include <asm/trapnr.h>
+#include <asm/processor-flags.h>
+#include <asm/asm-offsets.h>
+#include <asm/bootparam.h>
+#include <asm/page_types.h>
+#include <asm/irq_vectors.h>
+#include <linux/slr_table.h>
+#include <linux/slaunch.h>
+
+/* CPUID: leaf 1, ECX, SMX feature bit */
+#define X86_FEATURE_BIT_SMX	(1 << 6)
+
+#define IDT_VECTOR_LO_BITS	0
+#define IDT_VECTOR_HI_BITS	6
+
+/*
+ * See the comment in head_64.S for detailed information on what this macro
+ * and others like it are used for. The comment appears right at the top of
+ * the file.
+ */
+#define rva(X) ((X) - sl_stub_entry)
+
+/*
+ * The GETSEC op code is open coded because older versions of
+ * GCC do not support the getsec mnemonic.
+ */
+.macro GETSEC leaf
+	pushl	%ebx
+	xorl	%ebx, %ebx	/* Must be zero for SMCTRL */
+	movl	\leaf, %eax	/* Leaf function */
+	.byte 	0x0f, 0x37	/* GETSEC opcode */
+	popl	%ebx
+.endm
+
+.macro TXT_RESET error
+	/*
+	 * Set a sticky error value and reset. Note the movs to %eax act as
+	 * TXT register barriers.
+	 */
+	movl	\error, (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_ERRORCODE)
+	movl	(TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_E2STS), %eax
+	movl	$1, (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_CMD_NO_SECRETS)
+	movl	(TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_E2STS), %eax
+	movl	$1, (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_CMD_UNLOCK_MEM_CONFIG)
+	movl	(TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_E2STS), %eax
+	movl	$1, (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_CMD_RESET)
+1:
+	hlt
+	jmp	1b
+.endm
+
+	.code32
+SYM_FUNC_START(sl_stub_entry)
+	cli
+	cld
+
+	/*
+	 * On entry, %ebx has the entry abs offset to sl_stub_entry. This
+	 * will be correctly scaled using the rva macro and avoid causing
+	 * relocations. Only %cs and %ds segments are known good.
+	 */
+
+	/* Load GDT, set segment regs and lret to __SL32_CS */
+	leal	rva(sl_gdt_desc)(%ebx), %eax
+	addl	%eax, 2(%eax)
+	lgdt	(%eax)
+
+	movl	$(__SL32_DS), %eax
+	movw	%ax, %ds
+	movw	%ax, %es
+	movw	%ax, %fs
+	movw	%ax, %gs
+	movw	%ax, %ss
+
+	/*
+	 * Now that %ss is known good, take the first stack for the BSP. The
+	 * AP stacks are only used on Intel.
+	 */
+	leal	rva(sl_stacks_end)(%ebx), %esp
+
+	leal	rva(.Lsl_cs)(%ebx), %eax
+	pushl	$(__SL32_CS)
+	pushl	%eax
+	lret
+
+.Lsl_cs:
+	/* Save our base pointer reg and page table for MLE */
+	pushl	%ebx
+	pushl	%ecx
+
+	/* See if SMX feature is supported. */
+	movl	$1, %eax
+	cpuid
+	testl	$(X86_FEATURE_BIT_SMX), %ecx
+	jz	.Ldo_unknown_cpu
+
+	popl	%ecx
+	popl	%ebx
+
+	/* Know it is Intel */
+	movl	$(SL_CPU_INTEL), rva(sl_cpu_type)(%ebx)
+
+	/* Locate the base of the MLE using the page tables in %ecx */
+	call	sl_find_mle_base
+
+	/* Increment CPU count for BSP */
+	incl	rva(sl_txt_cpu_count)(%ebx)
+
+	/*
+	 * Enable SMI with GETSEC[SMCTRL] which were disabled by SENTER.
+	 * NMIs were also disabled by SENTER. Since there is no IDT for the BSP,
+	 * allow the mainline kernel re-enable them in the normal course of
+	 * booting.
+	 */
+	GETSEC	$(SMX_X86_GETSEC_SMCTRL)
+
+	/* Clear the TXT error registers for a clean start of day */
+	movl	$0, (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_ERRORCODE)
+	movl	$0xffffffff, (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_ESTS)
+
+	/* On Intel, the zero page address is passed in the TXT heap */
+	/* Read physical base of heap into EAX */
+	movl	(TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_HEAP_BASE), %eax
+	/* Read the size of the BIOS data into ECX (first 8 bytes) */
+	movl	(%eax), %ecx
+	/* Skip over BIOS data and size of OS to MLE data section */
+	leal	8(%eax, %ecx), %eax
+
+	/* Need to verify the values in the OS-MLE struct passed in */
+	call	sl_txt_verify_os_mle_struct
+
+	/*
+	 * Get the boot params address from the heap. Note %esi and %ebx MUST
+	 * be preserved across calls and operations.
+	 */
+	movl	SL_boot_params_addr(%eax), %esi
+
+	/* Save %ebx so the APs can find their way home */
+	movl	%ebx, (SL_mle_scratch + SL_SCRATCH_AP_EBX)(%eax)
+
+	/* Fetch the AP wake code block address from the heap */
+	movl	SL_ap_wake_block(%eax), %edi
+	movl	%edi, rva(sl_txt_ap_wake_block)(%ebx)
+
+	/* Store the offset in the AP wake block to the jmp address */
+	movl	$(sl_ap_jmp_offset - sl_txt_ap_wake_begin), \
+		(SL_mle_scratch + SL_SCRATCH_AP_JMP_OFFSET)(%eax)
+
+	/* Store the offset in the AP wake block to the AP stacks block */
+	movl	$(sl_stacks - sl_txt_ap_wake_begin), \
+		(SL_mle_scratch + SL_SCRATCH_AP_STACKS_OFFSET)(%eax)
+
+	/* %eax still is the base of the OS-MLE block, save it */
+	pushl	%eax
+
+	/* Relocate the AP wake code to the safe block */
+	call	sl_txt_reloc_ap_wake
+
+	/*
+	 * Wake up all APs that are blocked in the ACM and wait for them to
+	 * halt. This should be done before restoring the MTRRs so the ACM is
+	 * still properly in WB memory.
+	 */
+	call	sl_txt_wake_aps
+
+	/* Restore OS-MLE in %eax */
+	popl	%eax
+
+	/*
+	 * %edi is used by this routine to find the MTRRs which are in the SLRT
+	 * in the Intel info.
+	 */
+	movl	SL_txt_info(%eax), %edi
+	call	sl_txt_load_regs
+
+	jmp	.Lcpu_setup_done
+
+.Ldo_unknown_cpu:
+	/* Non-Intel CPUs are not yet supported */
+	ud2
+
+.Lcpu_setup_done:
+	/*
+	 * Don't enable MCE at this point. The kernel will enable
+	 * it on the BSP later when it is ready.
+	 */
+
+	/* Done, jump to normal 32b pm entry */
+	jmp	startup_32
+SYM_FUNC_END(sl_stub_entry)
+
+SYM_FUNC_START(sl_find_mle_base)
+	/* %ecx has PDPT, get first PD */
+	movl	(%ecx), %eax
+	andl	$(PAGE_MASK), %eax
+	/* Get first PT from first PDE */
+	movl	(%eax), %eax
+	andl	$(PAGE_MASK), %eax
+	/* Get MLE base from first PTE */
+	movl	(%eax), %eax
+	andl	$(PAGE_MASK), %eax
+
+	movl	%eax, rva(sl_mle_start)(%ebx)
+	ret
+SYM_FUNC_END(sl_find_mle_base)
+
+SYM_FUNC_START(sl_check_buffer_mle_overlap)
+	/* %ecx: buffer begin %edx: buffer end */
+	/* %ebx: MLE begin %edi: MLE end */
+	/* %eax: region may be inside MLE */
+
+	cmpl	%edi, %ecx
+	jb	.Lnext_check
+	cmpl	%edi, %edx
+	jbe	.Lnext_check
+	jmp	.Lvalid /* Buffer above MLE */
+
+.Lnext_check:
+	cmpl	%ebx, %edx
+	ja	.Linside_check
+	cmpl	%ebx, %ecx
+	jae	.Linside_check
+	jmp	.Lvalid /* Buffer below MLE */
+
+.Linside_check:
+	cmpl	$0, %eax
+	jz	.Linvalid
+	cmpl	%ebx, %ecx
+	jb	.Linvalid
+	cmpl	%edi, %edx
+	ja	.Linvalid
+	jmp	.Lvalid /* Buffer in MLE */
+
+.Linvalid:
+	TXT_RESET $(SL_ERROR_MLE_BUFFER_OVERLAP)
+
+.Lvalid:
+	ret
+SYM_FUNC_END(sl_check_buffer_mle_overlap)
+
+SYM_FUNC_START(sl_txt_verify_os_mle_struct)
+	pushl	%ebx
+	/*
+	 * %eax points to the base of the OS-MLE struct. Need to also
+	 * read some values from the OS-SINIT struct too.
+	 */
+	movl	-8(%eax), %ecx
+	/* Skip over OS to MLE data section and size of OS-SINIT structure */
+	leal	(%eax, %ecx), %edx
+
+	/* Load MLE image base absolute offset */
+	movl	rva(sl_mle_start)(%ebx), %ebx
+
+	/* Verify the value of the low PMR base. It should always be 0. */
+	movl	SL_vtd_pmr_lo_base(%edx), %esi
+	cmpl	$0, %esi
+	jz	.Lvalid_pmr_base
+	TXT_RESET $(SL_ERROR_LO_PMR_BASE)
+
+.Lvalid_pmr_base:
+	/* Grab some values from OS-SINIT structure */
+	movl	SL_mle_size(%edx), %edi
+	addl	%ebx, %edi
+	jc	.Loverflow_detected
+	movl	SL_vtd_pmr_lo_size(%edx), %esi
+
+	/* Check the AP wake block */
+	movl	SL_ap_wake_block(%eax), %ecx
+	movl	SL_ap_wake_block_size(%eax), %edx
+	addl	%ecx, %edx
+	jc	.Loverflow_detected
+	pushl	%eax
+	xorl	%eax, %eax
+	call	sl_check_buffer_mle_overlap
+	popl	%eax
+	cmpl	%esi, %edx
+	ja	.Lbuffer_beyond_pmr
+
+	/*
+	 * Check the boot params. Note during a UEFI boot, the boot
+	 * params will be inside the MLE image. Test for this case
+	 * in the overlap case.
+	 */
+	movl	SL_boot_params_addr(%eax), %ecx
+	movl	$(PAGE_SIZE), %edx
+	addl	%ecx, %edx
+	jc	.Loverflow_detected
+	pushl	%eax
+	movl	$1, %eax
+	call	sl_check_buffer_mle_overlap
+	popl	%eax
+	cmpl	%esi, %edx
+	ja	.Lbuffer_beyond_pmr
+
+	/* Check that the AP wake block is big enough */
+	cmpl	$(sl_txt_ap_wake_end - sl_txt_ap_wake_begin), \
+		SL_ap_wake_block_size(%eax)
+	jae	.Lwake_block_ok
+	TXT_RESET $(SL_ERROR_WAKE_BLOCK_TOO_SMALL)
+
+.Lwake_block_ok:
+	popl	%ebx
+	ret
+
+.Loverflow_detected:
+	TXT_RESET $(SL_ERROR_INTEGER_OVERFLOW)
+
+.Lbuffer_beyond_pmr:
+	TXT_RESET $(SL_ERROR_BUFFER_BEYOND_PMR)
+SYM_FUNC_END(sl_txt_verify_os_mle_struct)
+
+SYM_FUNC_START(sl_txt_ap_entry)
+	cli
+	cld
+	/*
+	 * The %cs and %ds segments are known good after waking the AP.
+	 * First order of business is to find where we are and
+	 * save it in %ebx.
+	 */
+
+	/* Read physical base of heap into EAX */
+	movl	(TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_HEAP_BASE), %eax
+	/* Read the size of the BIOS data into ECX (first 8 bytes) */
+	movl	(%eax), %ecx
+	/* Skip over BIOS data and size of OS to MLE data section */
+	leal	8(%eax, %ecx), %eax
+
+	/* Saved %ebx from the BSP and stash OS-MLE pointer */
+	movl	(SL_mle_scratch + SL_SCRATCH_AP_EBX)(%eax), %ebx
+
+	/* Save TXT info ptr in %edi for call to sl_txt_load_regs */
+	movl	SL_txt_info(%eax), %edi
+
+	/* Lock and get our stack index */
+	movl	$1, %ecx
+.Lspin:
+	xorl	%eax, %eax
+	lock cmpxchgl	%ecx, rva(sl_txt_spin_lock)(%ebx)
+	pause
+	jnz	.Lspin
+
+	/* Increment the stack index and use the next value inside lock */
+	incl	rva(sl_txt_stack_index)(%ebx)
+	movl	rva(sl_txt_stack_index)(%ebx), %eax
+
+	/* Unlock */
+	movl	$0, rva(sl_txt_spin_lock)(%ebx)
+
+	/* Location of the relocated AP wake block */
+	movl	rva(sl_txt_ap_wake_block)(%ebx), %ecx
+
+	/* Load reloc GDT, set segment regs and lret to __SL32_CS */
+	lgdt	(sl_ap_gdt_desc - sl_txt_ap_wake_begin)(%ecx)
+
+	movl	$(__SL32_DS), %edx
+	movw	%dx, %ds
+	movw	%dx, %es
+	movw	%dx, %fs
+	movw	%dx, %gs
+	movw	%dx, %ss
+
+	/* Load our reloc AP stack */
+	movl	$(TXT_BOOT_STACK_SIZE), %edx
+	mull	%edx
+	leal	(sl_stacks_end - sl_txt_ap_wake_begin)(%ecx), %esp
+	subl	%eax, %esp
+
+	/* Switch to AP code segment */
+	leal	rva(.Lsl_ap_cs)(%ebx), %eax
+	pushl	$(__SL32_CS)
+	pushl	%eax
+	lret
+
+.Lsl_ap_cs:
+	/* Load the relocated AP IDT */
+	lidt	(sl_ap_idt_desc - sl_txt_ap_wake_begin)(%ecx)
+
+	/* Fixup MTRRs and misc enable MSR on APs too */
+	call	sl_txt_load_regs
+
+	/* Enable SMI with GETSEC[SMCTRL] */
+	GETSEC $(SMX_X86_GETSEC_SMCTRL)
+
+	/* IRET-to-self can be used to enable NMIs which SENTER disabled */
+	leal	rva(.Lnmi_enabled_ap)(%ebx), %eax
+	pushfl
+	pushl	$(__SL32_CS)
+	pushl	%eax
+	iret
+
+.Lnmi_enabled_ap:
+	/* Put APs in X2APIC mode like the BSP */
+	movl	$(MSR_IA32_APICBASE), %ecx
+	rdmsr
+	orl	$(XAPIC_ENABLE | X2APIC_ENABLE), %eax
+	wrmsr
+
+	/*
+	 * Basically done, increment the CPU count and jump off to the AP
+	 * wake block to wait.
+	 */
+	lock incl	rva(sl_txt_cpu_count)(%ebx)
+
+	movl	rva(sl_txt_ap_wake_block)(%ebx), %eax
+	jmp	*%eax
+SYM_FUNC_END(sl_txt_ap_entry)
+
+SYM_FUNC_START(sl_txt_reloc_ap_wake)
+	/* Save boot params register */
+	pushl	%esi
+
+	movl	rva(sl_txt_ap_wake_block)(%ebx), %edi
+
+	/* Fixup AP IDT and GDT descriptor before relocating */
+	leal	rva(sl_ap_idt_desc)(%ebx), %eax
+	addl	%edi, 2(%eax)
+	leal	rva(sl_ap_gdt_desc)(%ebx), %eax
+	addl	%edi, 2(%eax)
+
+	/*
+	 * Copy the AP wake code and AP GDT/IDT to the protected wake block
+	 * provided by the loader. Destination already in %edi.
+	 */
+	movl	$(sl_txt_ap_wake_end - sl_txt_ap_wake_begin), %ecx
+	leal	rva(sl_txt_ap_wake_begin)(%ebx), %esi
+	rep movsb
+
+	/* Setup the IDT for the APs to use in the relocation block */
+	movl	rva(sl_txt_ap_wake_block)(%ebx), %ecx
+	addl	$(sl_ap_idt - sl_txt_ap_wake_begin), %ecx
+	xorl	%edx, %edx
+
+	/* Form the default reset vector relocation address */
+	movl	rva(sl_txt_ap_wake_block)(%ebx), %esi
+	addl	$(sl_txt_int_reset - sl_txt_ap_wake_begin), %esi
+
+1:
+	cmpw	$(NR_VECTORS), %dx
+	jz	.Lap_idt_done
+
+	cmpw	$(X86_TRAP_NMI), %dx
+	jz	2f
+
+	/* Load all other fixed vectors with reset handler */
+	movl	%esi, %eax
+	movw	%ax, (IDT_VECTOR_LO_BITS)(%ecx)
+	shrl	$16, %eax
+	movw	%ax, (IDT_VECTOR_HI_BITS)(%ecx)
+	jmp	3f
+
+2:
+	/* Load single wake NMI IPI vector at the relocation address */
+	movl	rva(sl_txt_ap_wake_block)(%ebx), %eax
+	addl	$(sl_txt_int_nmi - sl_txt_ap_wake_begin), %eax
+	movw	%ax, (IDT_VECTOR_LO_BITS)(%ecx)
+	shrl	$16, %eax
+	movw	%ax, (IDT_VECTOR_HI_BITS)(%ecx)
+
+3:
+	incw	%dx
+	addl	$8, %ecx
+	jmp	1b
+
+.Lap_idt_done:
+	popl	%esi
+	ret
+SYM_FUNC_END(sl_txt_reloc_ap_wake)
+
+SYM_FUNC_START(sl_txt_load_regs)
+	/* Save base pointer register */
+	pushl	%ebx
+
+	/*
+	 * On Intel, the original variable MTRRs and Misc Enable MSR are
+	 * restored on the BSP at early boot. Each AP will also restore
+	 * its MTRRs and Misc Enable MSR.
+	 */
+	pushl	%edi
+	addl	$(SL_saved_bsp_mtrrs), %edi
+	movl	(%edi), %ebx
+	pushl	%ebx /* default_mem_type lo */
+	addl	$4, %edi
+	movl	(%edi), %ebx
+	pushl	%ebx /* default_mem_type hi */
+	addl	$4, %edi
+	movl	(%edi), %ebx /* mtrr_vcnt lo, don't care about hi part */
+	addl	$8, %edi /* now at MTRR pair array */
+	/* Write the variable MTRRs */
+	movl	$(MSR_MTRRphysBase0), %ecx
+1:
+	cmpl	$0, %ebx
+	jz	2f
+
+	movl	(%edi), %eax /* MTRRphysBaseX lo */
+	addl	$4, %edi
+	movl	(%edi), %edx /* MTRRphysBaseX hi */
+	wrmsr
+	addl	$4, %edi
+	incl	%ecx
+	movl	(%edi), %eax /* MTRRphysMaskX lo */
+	addl	$4, %edi
+	movl	(%edi), %edx /* MTRRphysMaskX hi */
+	wrmsr
+	addl	$4, %edi
+	incl	%ecx
+
+	decl	%ebx
+	jmp	1b
+2:
+	/* Write the default MTRR register */
+	popl	%edx
+	popl	%eax
+	movl	$(MSR_MTRRdefType), %ecx
+	wrmsr
+
+	/* Return to beginning and write the misc enable msr */
+	popl	%edi
+	addl	$(SL_saved_misc_enable_msr), %edi
+	movl	(%edi), %eax /* saved_misc_enable_msr lo */
+	addl	$4, %edi
+	movl	(%edi), %edx /* saved_misc_enable_msr hi */
+	movl	$(MSR_IA32_MISC_ENABLE), %ecx
+	wrmsr
+
+	popl	%ebx
+	ret
+SYM_FUNC_END(sl_txt_load_regs)
+
+SYM_FUNC_START(sl_txt_wake_aps)
+	/* Save boot params register */
+	pushl	%esi
+
+	/* First setup the MLE join structure and load it into TXT reg */
+	leal	rva(sl_gdt)(%ebx), %eax
+	leal	rva(sl_txt_ap_entry)(%ebx), %ecx
+	leal	rva(sl_smx_rlp_mle_join)(%ebx), %edx
+	movl	%eax, SL_rlp_gdt_base(%edx)
+	movl	%ecx, SL_rlp_entry_point(%edx)
+	movl	%edx, (TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_MLE_JOIN)
+
+	/* Another TXT heap walk to find various values needed to wake APs */
+	movl	(TXT_PRIV_CONFIG_REGS_BASE + TXT_CR_HEAP_BASE), %eax
+	/* At BIOS data size, find the number of logical processors */
+	movl	(SL_num_logical_procs + 8)(%eax), %edx
+	/* Skip over BIOS data */
+	movl	(%eax), %ecx
+	addl	%ecx, %eax
+	/* Skip over OS to MLE */
+	movl	(%eax), %ecx
+	addl	%ecx, %eax
+	/* At OS-SNIT size, get capabilities to know how to wake up the APs */
+	movl	(SL_capabilities + 8)(%eax), %esi
+	/* Skip over OS to SNIT */
+	movl	(%eax), %ecx
+	addl	%ecx, %eax
+	/* At SINIT-MLE size, get the AP wake MONITOR address */
+	movl	(SL_rlp_wakeup_addr + 8)(%eax), %edi
+
+	/* Determine how to wake up the APs */
+	testl	$(1 << TXT_SINIT_MLE_CAP_WAKE_MONITOR), %esi
+	jz	.Lwake_getsec
+
+	/* Wake using MWAIT MONITOR */
+	movl	$1, (%edi)
+	jmp	.Laps_awake
+
+.Lwake_getsec:
+	/* Wake using GETSEC(WAKEUP) */
+	GETSEC	$(SMX_X86_GETSEC_WAKEUP)
+
+.Laps_awake:
+	/*
+	 * All of the APs are woken up and rendesvous in the relocated wake
+	 * block starting at sl_txt_ap_wake_begin. Wait for all of them to
+	 * halt.
+	 */
+	pause
+	cmpl	rva(sl_txt_cpu_count)(%ebx), %edx
+	jne	.Laps_awake
+
+	popl	%esi
+	ret
+SYM_FUNC_END(sl_txt_wake_aps)
+
+/* This is the beginning of the relocated AP wake code block */
+	.global sl_txt_ap_wake_begin
+sl_txt_ap_wake_begin:
+
+	/* Get the LAPIC ID for each AP and stash it on the stack */
+	movl	$(MSR_IA32_X2APIC_APICID), %ecx
+	rdmsr
+	pushl	%eax
+
+	/*
+	 * Get a pointer to the monitor location on this APs stack to test below
+	 * after mwait returns. Currently %esp points to just past the pushed APIC
+	 * ID value.
+	 */
+	movl	%esp, %eax
+	subl	$(TXT_BOOT_STACK_SIZE - 4), %eax
+	movl	$0, (%eax)
+
+	/* Clear ecx/edx so no invalid extensions or hints are passed to monitor */
+	xorl	%ecx, %ecx
+	xorl	%edx, %edx
+
+	/*
+	 * Arm the monitor and wait for it to be poked by he SMP bringup code. The mwait
+	 * instruction can return for a number of reasons. Test to see if it returned
+	 * because the monitor was written to.
+	 */
+	monitor
+
+1:
+	mfence
+	mwait
+	movl	(%eax), %edx
+	testl	%edx, %edx
+	jz	1b
+
+	/*
+	 * This is the long absolute jump to the 32b Secure Launch protected mode stub
+	 * code in sl_trampoline_start32() in the rmpiggy. The jump address will be
+	 * fixed in the SMP boot code when the first AP is brought up. This whole area
+	 * is provided and protected in the memory map by the prelaunch code.
+	 */
+	.byte	0xea
+sl_ap_jmp_offset:
+	.long	0x00000000
+	.word	__SL32_CS
+
+SYM_FUNC_START(sl_txt_int_nmi)
+	/* NMI context, just IRET */
+	iret
+SYM_FUNC_END(sl_txt_int_nmi)
+
+SYM_FUNC_START(sl_txt_int_reset)
+	TXT_RESET $(SL_ERROR_INV_AP_INTERRUPT)
+SYM_FUNC_END(sl_txt_int_reset)
+
+	.balign 8
+SYM_DATA_START_LOCAL(sl_ap_idt_desc)
+	.word	sl_ap_idt_end - sl_ap_idt - 1		/* Limit */
+	.long	sl_ap_idt - sl_txt_ap_wake_begin	/* Base */
+SYM_DATA_END_LABEL(sl_ap_idt_desc, SYM_L_LOCAL, sl_ap_idt_desc_end)
+
+	.balign 8
+SYM_DATA_START_LOCAL(sl_ap_idt)
+	.rept	NR_VECTORS
+	.word	0x0000		/* Offset 15 to 0 */
+	.word	__SL32_CS	/* Segment selector */
+	.word	0x8e00		/* Present, DPL=0, 32b Vector, Interrupt */
+	.word	0x0000		/* Offset 31 to 16 */
+	.endr
+SYM_DATA_END_LABEL(sl_ap_idt, SYM_L_LOCAL, sl_ap_idt_end)
+
+	.balign 8
+SYM_DATA_START_LOCAL(sl_ap_gdt_desc)
+	.word	sl_ap_gdt_end - sl_ap_gdt - 1
+	.long	sl_ap_gdt - sl_txt_ap_wake_begin
+SYM_DATA_END_LABEL(sl_ap_gdt_desc, SYM_L_LOCAL, sl_ap_gdt_desc_end)
+
+	.balign	8
+SYM_DATA_START_LOCAL(sl_ap_gdt)
+	.quad	0x0000000000000000	/* NULL */
+	.quad	0x00cf9a000000ffff	/* __SL32_CS */
+	.quad	0x00cf92000000ffff	/* __SL32_DS */
+SYM_DATA_END_LABEL(sl_ap_gdt, SYM_L_LOCAL, sl_ap_gdt_end)
+
+	/* Small stacks for BSP and APs to work with */
+	.balign 64
+SYM_DATA_START_LOCAL(sl_stacks)
+	.fill (TXT_MAX_CPUS * TXT_BOOT_STACK_SIZE), 1, 0
+SYM_DATA_END_LABEL(sl_stacks, SYM_L_LOCAL, sl_stacks_end)
+
+/* This is the end of the relocated AP wake code block */
+	.global sl_txt_ap_wake_end
+sl_txt_ap_wake_end:
+
+	.data
+	.balign 8
+SYM_DATA_START_LOCAL(sl_gdt_desc)
+	.word	sl_gdt_end - sl_gdt - 1
+	.long	sl_gdt - sl_gdt_desc
+SYM_DATA_END_LABEL(sl_gdt_desc, SYM_L_LOCAL, sl_gdt_desc_end)
+
+	.balign	8
+SYM_DATA_START_LOCAL(sl_gdt)
+	.quad	0x0000000000000000	/* NULL */
+	.quad	0x00cf9a000000ffff	/* __SL32_CS */
+	.quad	0x00cf92000000ffff	/* __SL32_DS */
+SYM_DATA_END_LABEL(sl_gdt, SYM_L_LOCAL, sl_gdt_end)
+
+	.balign 8
+SYM_DATA_START_LOCAL(sl_smx_rlp_mle_join)
+	.long	sl_gdt_end - sl_gdt - 1	/* GDT limit */
+	.long	0x00000000		/* GDT base */
+	.long	__SL32_CS	/* Seg Sel - CS (DS, ES, SS = seg_sel+8) */
+	.long	0x00000000	/* Entry point physical address */
+SYM_DATA_END(sl_smx_rlp_mle_join)
+
+SYM_DATA(sl_cpu_type, .long 0x00000000)
+
+SYM_DATA(sl_mle_start, .long 0x00000000)
+
+SYM_DATA_LOCAL(sl_txt_spin_lock, .long 0x00000000)
+
+SYM_DATA_LOCAL(sl_txt_stack_index, .long 0x00000000)
+
+SYM_DATA_LOCAL(sl_txt_cpu_count, .long 0x00000000)
+
+SYM_DATA_LOCAL(sl_txt_ap_wake_block, .long 0x00000000)
arch/x86/include/asm/msr-index.h +3 -0
index e022e6eb766c..37f6167f28ba 100644
@@ -348,6 +348,9 @@
 #define MSR_IA32_RTIT_OUTPUT_BASE	0x00000560
 #define MSR_IA32_RTIT_OUTPUT_MASK	0x00000561
 
+#define MSR_MTRRphysBase0		0x00000200
+#define MSR_MTRRphysMask0		0x00000201
+
 #define MSR_MTRRfix64K_00000		0x00000250
 #define MSR_MTRRfix16K_80000		0x00000258
 #define MSR_MTRRfix16K_A0000		0x00000259

arch/x86/include/asm/msr-index.h line 859

Quoted 8 lines
arch/x86/include/asm/msr-index.h +2 -0
@@ -849,6 +852,8 @@
 #define MSR_IA32_APICBASE_ENABLE	(1<<11)
 #define MSR_IA32_APICBASE_BASE		(0xfffff<<12)
 
+#define MSR_IA32_X2APIC_APICID		0x00000802
+
 #define MSR_IA32_UCODE_WRITE		0x00000079
 #define MSR_IA32_UCODE_REV		0x0000008b
 

MSR updates are better to be split to their own patch.

Quoted 38 lines
arch/x86/include/uapi/asm/bootparam.h +1 -0
index 9b82eebd7add..7ce283a22d6b 100644
@@ -12,6 +12,7 @@
 /* loadflags */
 #define LOADED_HIGH	(1<<0)
 #define KASLR_FLAG	(1<<1)
+#define SLAUNCH_FLAG	(1<<2)
 #define QUIET_FLAG	(1<<5)
 #define KEEP_SEGMENTS	(1<<6)
 #define CAN_USE_HEAP	(1<<7)
arch/x86/kernel/asm-offsets.c +11 -0
index a98020bf31bb..925adce6e2c7 100644
@@ -13,6 +13,8 @@
 #include <linux/hardirq.h>
 #include <linux/suspend.h>
 #include <linux/kbuild.h>
+#include <linux/slr_table.h>
+#include <linux/slaunch.h>
 #include <asm/processor.h>
 #include <asm/thread_info.h>
 #include <asm/sigframe.h>
@@ -120,4 +122,22 @@ static void __used common(void)
 	OFFSET(ARIA_CTX_rounds, aria_ctx, rounds);
 #endif
 
+#ifdef CONFIG_SECURE_LAUNCH
+	BLANK();
+	OFFSET(SL_txt_info, txt_os_mle_data, txt_info);
+	OFFSET(SL_mle_scratch, txt_os_mle_data, mle_scratch);
+	OFFSET(SL_boot_params_addr, txt_os_mle_data, boot_params_addr);
+	OFFSET(SL_ap_wake_block, txt_os_mle_data, ap_wake_block);
+	OFFSET(SL_ap_wake_block_size, txt_os_mle_data, ap_wake_block_size);
+	OFFSET(SL_saved_misc_enable_msr, slr_entry_intel_info, saved_misc_enable_msr);
+	OFFSET(SL_saved_bsp_mtrrs, slr_entry_intel_info, saved_bsp_mtrrs);

arch/x86/kernel/asm-offsets.c static void __used common(void) · line 143

Quoted 10 lines
arch/x86/kernel/asm-offsets.c +9 -0
+	OFFSET(SL_num_logical_procs, txt_bios_data, num_logical_procs);
+	OFFSET(SL_capabilities, txt_os_sinit_data, capabilities);
+	OFFSET(SL_mle_size, txt_os_sinit_data, mle_size);
+	OFFSET(SL_vtd_pmr_lo_base, txt_os_sinit_data, vtd_pmr_lo_base);
+	OFFSET(SL_vtd_pmr_lo_size, txt_os_sinit_data, vtd_pmr_lo_size);
+	OFFSET(SL_rlp_wakeup_addr, txt_sinit_mle_data, rlp_wakeup_addr);
+	OFFSET(SL_rlp_gdt_base, smx_rlp_mle_join, rlp_gdt_base);
+	OFFSET(SL_rlp_entry_point, smx_rlp_mle_join, rlp_entry_point);
+#endif
 }

BR, Jarkko