tsm-mr: Unified Measurement Register ABI for TVMs
Subsystems (4)
v3 cover tsm-mr: Unified Measurement Register ABI for TVMs
NOTE: This patch series introduces the Measurement Register (MR) ABI, and is a continuation of the RFC series on the same topic [1].
Introduce the CONFIG_TSM_MEASUREMENTS helper library (tsm-mr) as a cross-vendor transport schema to allow TVM (TEE VM) guest drives to export CC (Confidential Compute) architecture-specific MRs (Measurement Registers) as sysfs attributes/files. Enable applications to read, write/extend MRs like regular files, supporting various usages such as configuration verification (e.g., verify a TVM's configuration against digests stored in static/immutable MRs like MRCONFIGID on TDX or HOSTDATA on SEV) and runtime measurements (e.g., extend the measurement of a container image to an RTMR before running it).
Patches included in this series:
- Patch 1 adds the tsm-mr library to help TVM guest drivers exposing MRs as sysfs attributes.
- Patch 2 provides a sample module demonstrating the usage of the new tsm-mr library.
- The remaining patches update the TDX guest driver to expose TDX MRs with the help of the tsm-mr library.
[1]: https://lore.kernel.org/linux-coco/[email protected]/
---
Changes in v3:
- tsm-mr: Separate measurement support (tsm-mr) from the original tsm source code. Modules depending on tsm-mr should select TSM_MEASUREMENTS in Kconfig.
- tsm-mr: Revise tsm-mr APIs to allow callers to decide where to host the MR attributes in sysfs.
- tsm-mr: Drop TSM_MR_F_EXTENSIBLE and route all "write" requests to the CC guest driver, which would decide how to handle writes (e.g., as extension to the specified MR).
- tsm-mr: Update the naming pattern for MR attributes from MRNAME/HASH/digest to MRNAME:HASH.
- tsm-mr: Drop TSM_MR_MAXBANKS kernel config.
- x86/tdx: Return -EBUSY from tdx_mcall_get_report0 on TDCALL_OPERAND_BUSY error.
- tdx-guest: Move MR attributes from /sys/kernel/tsm/tdx/ to /sys/class/misc/tdx_guest/ because MR names are architecture-specific, so their attributes should reside in an architecture-specific location.
- tdx-guest: Remove hash from mrconfigid, mrowner, mrownerconfig.
- tdx-guest: Remove servtd_hash, report0, and reportdata.
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Added TSM_MR_MAXBANKS Kconfig option
- Updated Kconfig dependency for TSM_REPORTS
- Updated comments in include/linux/tsm.h
- Updated drivers/virt/coco/tdx-guest/tdx-guest.c to use IS_BUILTIN() for determining if static buffer addresses can be converted to GPAs by virt_to_phys()
- Renamed function tdx_mcall_rtmr_extend() -> tdx_mcall_extend_rtmr()
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Cedric Xing (5):
tsm-mr: Add TVM Measurement Register support
tsm-mr: Add tsm-mr sample code
x86/tdx: Add tdx_mcall_extend_rtmr() interface
x86/tdx: tdx_mcall_get_report0: Return -EBUSY on TDCALL_OPERAND_BUSY error
virt: tdx-guest: Expose TDX MRs as sysfs attributes
.../sysfs-devices-virtual-misc-tdx_guest-mr | 48 +++++ MAINTAINERS | 6 +- arch/x86/coco/tdx/tdx.c | 42 ++++- arch/x86/include/asm/shared/tdx.h | 1 + arch/x86/include/asm/tdx.h | 2 + drivers/virt/coco/Kconfig | 5 + drivers/virt/coco/Makefile | 1 + drivers/virt/coco/tdx-guest/Kconfig | 1 + drivers/virt/coco/tdx-guest/tdx-guest.c | 169 ++++++++++++++++- drivers/virt/coco/tsm-mr.c | 209 +++++++++++++++++++++ include/linux/tsm-mr.h | 93 +++++++++ samples/Kconfig | 10 + samples/Makefile | 1 + samples/tsm-mr/Makefile | 2 + samples/tsm-mr/tsm_mr_sample.c | 138 ++++++++++++++ 15 files changed, 722 insertions(+), 6 deletions(-)
---
base-commit: 0af2f6be1b4281385b618cb86ad946eded089ac8
change-id: 20250209-tdx-rtmr-255479667146
Best regards,
Signature · 1 line
Cedric Xing <[email protected]>
PATCH v3 1/5 tsm-mr: Add TVM Measurement Register support
Introduce new TSM Measurement helper library (tsm-mr) for TVM guest drivers to expose MRs (Measurement Registers) as sysfs attributes, with Crypto Agility support.
Add the following new APIs (see include/linux/tsm-mr.h for details):
- tsm_mr_create_attribute_group(): Take on input a struct tsm_measurements instance, which includes one struct tsm_measurement_register per MR with properties like TSM_MR_F_READABLE and TSM_MR_F_WRITABLE, to determine the supported operations and create the sysfs attributes accordingly. On success, return a struct attribute_group instance that will typically be included by the guest driver into miscdevice.groups before calling misc_register().
- tsm_mr_free_attribute_group(): Free the memory allocated to the attrubute group returned by tsm_mr_create_attribute_group().
tsm_mr_create_attribute_group() creates one attribute for each MR, with names following this pattern:
MRNAME[:HASH]
- MRNAME - Placeholder for the MR name, as specified by tsm_measurement_register.mr_name.
- :HASH - Optional suffix indicating the hash algorithm associated with this MR, as specified by tsm_measurement_register.mr_hash.
Support Crypto Agility by allowing multiple definitions of the same MR (i.e., with the same mr_name) with distinct HASH algorithms.
NOTE: Crypto Agility, introduced in TPM 2.0, allows new hash algorithms to be introduced without breaking compatibility with applications using older algorithms. CC architectures may face the same challenge in the future, needing new hashes for security while retaining compatibility with older hashes, hence the need for Crypto Agility.
--- MAINTAINERS | 4 +- drivers/virt/coco/Kconfig | 5 ++ drivers/virt/coco/Makefile | 1 + drivers/virt/coco/tsm-mr.c | 209 +++++++++++++++++++++++++++++++++++++++++++++ include/linux/tsm-mr.h | 93 ++++++++++++++++++++ 5 files changed, 310 insertions(+), 2 deletions(-)
MAINTAINERS +2 -2
@@ -24558,8 +24558,8 @@ M: Dan Williams <[email protected]> L: [email protected] S: Maintained F: Documentation/ABI/testing/configfs-tsm -F: drivers/virt/coco/tsm.c -F: include/linux/tsm.h +F: drivers/virt/coco/tsm*.c +F: include/linux/tsm*.h TRUSTED SERVICES TEE DRIVER M: Balint Dobszay <[email protected]>
drivers/virt/coco/Kconfig +5 -0
@@ -7,6 +7,11 @@ config TSM_REPORTS select CONFIGFS_FS tristate +config TSM_MEASUREMENTS + select CRYPTO_HASH_INFO + select CRYPTO + bool + source "drivers/virt/coco/efi_secret/Kconfig" source "drivers/virt/coco/pkvm-guest/Kconfig"
drivers/virt/coco/Makefile +1 -0
@@ -3,6 +3,7 @@ # Confidential computing related collateral # obj-$(CONFIG_TSM_REPORTS) += tsm.o +obj-$(CONFIG_TSM_MEASUREMENTS) += tsm-mr.o obj-$(CONFIG_EFI_SECRET) += efi_secret/ obj-$(CONFIG_ARM_PKVM_GUEST) += pkvm-guest/ obj-$(CONFIG_SEV_GUEST) += sev-guest/
drivers/virt/coco/tsm-mr.c new file +209 -0
209 lines not on this page · raw message
include/linux/tsm-mr.h new file +93 -0
93 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v3 1/5] tsm-mr: Add TVM Measurement Register support
Quoted 30 lines · Cedric Xing
Introduce new TSM Measurement helper library (tsm-mr) for TVM guest drivers to expose MRs (Measurement Registers) as sysfs attributes, with Crypto Agility support.
Add the following new APIs (see
include/linux/tsm-mr.hfor details):-
tsm_mr_create_attribute_group(): Take on input astruct tsm_measurementsinstance, which includes onestruct tsm_measurement_registerper MR with properties likeTSM_MR_F_READABLEandTSM_MR_F_WRITABLE, to determine the supported operations and create the sysfs attributes accordingly. On success, return astruct attribute_groupinstance that will typically be included by the guest driver intomiscdevice.groupsbefore callingmisc_register().-
tsm_mr_free_attribute_group(): Free the memory allocated to the attrubute group returned bytsm_mr_create_attribute_group().
tsm_mr_create_attribute_group()creates one attribute for each MR, with names following this pattern:MRNAME[:HASH]
- MRNAME - Placeholder for the MR name, as specified by
tsm_measurement_register.mr_name.
- :HASH - Optional suffix indicating the hash algorithm associated with this MR, as specified bytsm_measurement_register.mr_hash.Support Crypto Agility by allowing multiple definitions of the same MR (i.e., with the same
mr_name) with distinct HASH algorithms.
Quoted 7 lines
NOTE: Crypto Agility, introduced in TPM 2.0, allows new hash algorithms to be introduced without breaking compatibility with applications using older algorithms. CC architectures may face the same challenge in the future, needing new hashes for security while retaining compatibility with older hashes, hence the need for Crypto Agility.
Signed-off-by Cedric Xing <[email protected]>
Given that this defines a shared ABI scheme for "measurement registers" lets add a Documentation/ entry for those shared mechanics that per-arch implementations can reference from their Documentation/ABI/ entry.
"Documentation/driver-api/measurement-registers.rst" seems suitable, and that can pull in the kernel-doc commentary from tsm-mr.[ch]. Here's a template to get that started:
Documentation/driver-api/coco/index.rst new file +12 -0
@@ -0,0 +1,12 @@ +.. SPDX-License-Identifier: GPL-2.0 + +====================== +Confidential Computing +====================== + +.. toctree:: + :maxdepth: 1 + + measurement-registers + +.. only:: subproject and html
Documentation/driver-api/coco/measurement-registers.rst new file +12 -0
@@ -0,0 +1,12 @@ +.. SPDX-License-Identifier: GPL-2.0 +.. include:: <isonum.txt> + +===================== +Measurement Registers +===================== + +.. kernel-doc:: include/linux/tsm-mr.h + :internal: + +.. kernel-doc:: drivers/virt/coco/tsm-mr.c + :export:
Documentation/driver-api/index.rst +1 -0
@@ -81,6 +81,7 @@ Subsystem-specific APIs acpi/index backlight/lp855x-driver.rst clk + coco/index console crypto/index dmaengine/index
Quoted 6 lines
--- MAINTAINERS | 4 +- drivers/virt/coco/Kconfig | 5 ++ drivers/virt/coco/Makefile | 1 + drivers/virt/coco/tsm-mr.c | 209 +++++++++++++++++++++++++++++++++++++++++++++ include/linux/tsm-mr.h | 93 ++++++++++++++++++++
I note that the pending proposals for TEE I/O suggests splitting drivers/virt/coco/ into drivers/virt/coco/{host,guest} [1] [2] [3].
[1]: http://lore.kernel.org/174107246021.1288555.7203769833791489618.stgit@dwillia2-xfh.jf.intel.com
[2]: http://lore.kernel.org/[email protected]
[3]: http://lore.kernel.org/[email protected]
So if I take this through devsec.git I will get that rename queued and ask you to rebase on top of that.
Quoted 78 lines
5 files changed, 310 insertions(+), 2 deletions(-)MAINTAINERS +2
-2@@ -24558,8 +24558,8 @@ M: Dan Williams <[email protected]> L: [email protected] S: Maintained F: Documentation/ABI/testing/configfs-tsm -F: drivers/virt/coco/tsm.c -F: include/linux/tsm.h +F: drivers/virt/coco/tsm*.c +F: include/linux/tsm*.h TRUSTED SERVICES TEE DRIVER M: Balint Dobszay <[email protected]>drivers/virt/coco/Kconfig +5
-0@@ -7,6 +7,11 @@ config TSM_REPORTS select CONFIGFS_FS tristate +config TSM_MEASUREMENTS + select CRYPTO_HASH_INFO + select CRYPTO + bool + source "drivers/virt/coco/efi_secret/Kconfig" source "drivers/virt/coco/pkvm-guest/Kconfig"drivers/virt/coco/Makefile +1
-0@@ -3,6 +3,7 @@ # Confidential computing related collateral # obj-$(CONFIG_TSM_REPORTS) += tsm.o +obj-$(CONFIG_TSM_MEASUREMENTS) += tsm-mr.o obj-$(CONFIG_EFI_SECRET) += efi_secret/ obj-$(CONFIG_ARM_PKVM_GUEST) += pkvm-guest/ obj-$(CONFIG_SEV_GUEST) += sev-guest/drivers/virt/coco/tsm-mr.c new file +27
-0@@ -0,0 +1,209 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright(c) 2024-2025 Intel Corporation. All rights reserved. */ + +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt + +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/sysfs.h> +#include <linux/tsm-mr.h> + +struct tm_context { +struct rw_semaphore rwsem; +struct attribute_group agrp; +const struct tsm_measurements *tm; +bool in_sync; +struct bin_attribute mrs[]; +}; + +static ssize_t tm_digest_read (struct file *filp,struct kobject *kobj, +const struct bin_attribute *attr,char *buffer, +loff_t off,size_t count) +{ +struct tm_context *ctx; +const struct tsm_measurement_register *mr; +int rc; + + ctx = attr->private;
drivers/virt/coco/tsm-mr.c line 37
Quoted 10 lines
drivers/virt/coco/tsm-mr.c new file +10
-0+ rc =down_read_interruptible (&ctx->rwsem); +if (rc) +return rc; + +/* +* @ctx->in_sync indicates if any MRs have been written since the last +* ctx->refresh() call. When @ctx->in_sync is false, ctx->refresh() is +* necessary to sync the cached values of all live MRs (i.e., with +* %TSM_MR_F_LIVE set) with the underlying hardware. +*/
Code comments should add to the understanding of the code, not simply restate the code in prose. So I would replace this comment with some non-obvious insight to aid future maintenance, something like:
/*
* Note that the typical read path for MRs is via an attestation report,
* this is why the ->write() path does not automatically ->refresh()
* invalidated data for TSM_MR_LIVE registers. The use case for reading
* back a individual hash-extending-write to an MR is for debug not
* attestation.
*/
...at least an explanation like that would have helped me understand the locking and caching model of this implementation.
Quoted 32 lines
drivers/virt/coco/tsm-mr.c new file +32
-0+ mr = &ctx->tm->mrs[attr - ctx->mrs]; +if ((mr->mr_flags &TSM_MR_F_LIVE ) && !ctx->in_sync) { +up_read (&ctx->rwsem); + + rc =down_write_killable (&ctx->rwsem); +if (rc) +return rc; + +if (!ctx->in_sync) { + rc = ctx->tm->refresh (ctx->tm, mr); + ctx->in_sync = !rc; + } + +downgrade_write (&ctx->rwsem); + } + +memcpy (buffer, mr->mr_value + off, count); + +up_read (&ctx->rwsem); +return rc ?: count; +} + +static ssize_t tm_digest_write (struct file *filp,struct kobject *kobj, +const struct bin_attribute *attr,char *buffer, +loff_t off,size_t count) +{ +struct tm_context *ctx; +const struct tsm_measurement_register *mr; +ssize_t rc; + +/* partial writes are not supported */ +if (off !=0 || count != attr->size)
drivers/virt/coco/tsm-mr.c line 79
Quoted 10 lines
drivers/virt/coco/tsm-mr.c new file +10
-0+return -EINVAL ; + + ctx = attr->private; + mr = &ctx->tm->mrs[attr - ctx->mrs]; + + rc =down_write_killable (&ctx->rwsem); +if (rc) +return rc; + + rc = ctx->tm->write (ctx->tm, mr, buffer);
There needs to be explicit ABI and driver-api documentation here for what are the allowed error codes that ->write() can return so as not to be confused with EINVAL or EINTR arising from user error or interrupt.
Quoted 18 lines
drivers/virt/coco/tsm-mr.c new file +18
-0+ +/* reset @ctx->in_sync to refresh LIVE MRs on next read */ +if (!rc) + ctx->in_sync = false; + +up_write (&ctx->rwsem); +return rc ?: count; +} + +/** +* tsm_mr_create_attribute_group() - creates an attribute group for measurement +* registers +* @tm: pointer to &struct tsm_measurements containing the MR definitions. +* +* This function creates attributes corresponding to the MR definitions +* provided by @tm->mrs. +* +* The created attributes will reference @tm and its members. The caller must
drivers/virt/coco/tsm-mr.c line 107
Quoted 10 lines
drivers/virt/coco/tsm-mr.c new file +10
-0+* not free @tm until after tsm_mr_free_attribute_group() is called. +* +* Context: Process context. May sleep due to memory allocation. +* +* Return: +* * On success, the pointer to a an attribute group is returned; otherwise +* * %-EINVAL - Invalid MR definitions. +* * %-ENOMEM - Out of memory. +*/ +const struct attribute_group *__must_check
No need to mark this function as __must_check. That attribute is typically reserved for core-apis.
drivers/virt/coco/tsm-mr.c line 111
Quoted 4 lines
drivers/virt/coco/tsm-mr.c new file +4
-0+tsm_mr_create_attribute_group(const struct tsm_measurements *tm) +{ +if (!tm->mrs) +return ERR_PTR (-EINVAL );
If you're going to check for !tm->mrs, might as well also check for !tm.
drivers/virt/coco/tsm-mr.c line 114
Quoted 3 lines
drivers/virt/coco/tsm-mr.c new file +3
-0+ +/* aggregated length of all MR names */ +size_t nlen =0 ;
Typically the only exceptions for not declaring variables at the top of a function are for "for ()" loops and scope-based cleanup.
drivers/virt/coco/tsm-mr.c line 126
Quoted 12 lines
drivers/virt/coco/tsm-mr.c new file +12
-0+ +for (size_t i =0 ; i < tm->nr_mrs; ++i) { +if ((tm->mrs[i].mr_flags &TSM_MR_F_LIVE ) && !tm->refresh) +return ERR_PTR (-EINVAL ); + +if ((tm->mrs[i].mr_flags &TSM_MR_F_WRITABLE ) && !tm->write) +return ERR_PTR (-EINVAL ); + +if (tm->mrs[i].mr_flags &TSM_MR_F_NOHASH ) +continue ; + +if (WARN_ON (tm->mrs[i].mr_hash >=HASH_ALGO__LAST ))
Why potentially crash the kernel here? EINVAL should be sufficient.
Quoted 13 lines
drivers/virt/coco/tsm-mr.c new file +13
-0+return ERR_PTR (-EINVAL ); + +/* MR sysfs attribute names have the form of MRNAME:HASH */ + nlen +=strlen (tm->mrs[i].mr_name) +1 + +strlen (hash_algo_name[tm->mrs[i].mr_hash]) +1 ; + } + +/* +* @bas and the MR name strings are combined into a single allocation +* so that we don't have to free MR names one-by-one in +* tsm_mr_free_attribute_group() +*/ + struct bin_attribute **bas __free(kfree) =
drivers/virt/coco/tsm-mr.c line 149
Quoted 10 lines
drivers/virt/coco/tsm-mr.c new file +10
-0+kzalloc (sizeof (*bas) * (tm->nr_mrs +1 ) + nlen,GFP_KERNEL ); + struct tm_context *ctx __free(kfree) = +kzalloc (struct_size (ctx, mrs, tm->nr_mrs),GFP_KERNEL ); +char *name, *end; + +if (!ctx || !bas) +return ERR_PTR (-ENOMEM ); + +/* @bas is followed immediately by MR name strings */ + name = (char *)&bas[tm->nr_mrs +1 ];
I looked for a helper macro for a "buffer at the end of a structure", but could not immediately find one. It feels like something Linux should already have.
Quoted 19 lines
drivers/virt/coco/tsm-mr.c new file +19
-0+ end = name + nlen; + +for (size_t i =0 ; i < tm->nr_mrs; ++i) { + bas[i] = &ctx->mrs[i]; +sysfs_bin_attr_init (bas[i]); + +if (tm->mrs[i].mr_flags &TSM_MR_F_NOHASH ) + bas[i]->attr.name = tm->mrs[i].mr_name; +else if (name < end) { + bas[i]->attr.name = name; + name +=snprintf (name, end - name,"%s:%s" , + tm->mrs[i].mr_name, + hash_algo_name[tm->mrs[i].mr_hash]); + ++name; + }else +return ERR_PTR (-EINVAL ); + +/* check for duplicated MR definitions */ +for (size_t j =0 ; j < i; ++j)
drivers/virt/coco/tsm-mr.c line 178
Quoted 10 lines
drivers/virt/coco/tsm-mr.c new file +10
-0+if (!strcmp (bas[i]->attr.name, bas[j]->attr.name)) +return ERR_PTR (-EINVAL ); + +if (tm->mrs[i].mr_flags &TSM_MR_F_READABLE ) { + bas[i]->attr.mode |=0444 ; + bas[i]->read_new = tm_digest_read; + } + +if (tm->mrs[i].mr_flags &TSM_MR_F_WRITABLE ) { + bas[i]->attr.mode |=0220 ;
Typical expectation for writable attributes is 0200.
Quoted 17 lines
drivers/virt/coco/tsm-mr.c new file +17
-0+ bas[i]->write_new = tm_digest_write; + } + + bas[i]->size = tm->mrs[i].mr_size; + bas[i]->private = ctx; + } + +if (name != end) +return ERR_PTR (-EINVAL ); + +init_rwsem (&ctx->rwsem); + ctx->agrp.name = tm->name; + ctx->agrp.bin_attrs =no_free_ptr (bas); + ctx->tm = tm; +return &no_free_ptr (ctx)->agrp; +} +EXPORT_SYMBOL_GPL (tsm_mr_create_attribute_group);
drivers/virt/coco/tsm-mr.c line 205
Quoted 10 lines
drivers/virt/coco/tsm-mr.c new file +10
-0+ +/** +* tsm_mr_free_attribute_group() - frees the attribute group returned by +* tsm_mr_create_attribute_group() +* @attr_grp: attribute group returned by tsm_mr_create_attribute_group() +* +* Context: Process context. +*/ +void tsm_mr_free_attribute_group (const struct attribute_group *attr_grp) +{
Related to the removal of __must_check add safety here for cases where someone passes in an ERR_PTR():
if (IS_ERR_OR_NULL(attr_grp) return;
This also makes the function amenable to scope-based cleanup.
Quoted 29 lines
drivers/virt/coco/tsm-mr.c new file +4
-0+kfree (attr_grp->bin_attrs); +kfree (container_of(attr_grp ,struct tm_context ,agrp )); +} +EXPORT_SYMBOL_GPL (tsm_mr_free_attribute_group);include/linux/tsm-mr.h new file +19
-0@@ -0,0 +1,93 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __TSM_MR_H +#define __TSM_MR_H + +#include <crypto/hash_info.h> + +/** +* struct tsm_measurement_register - describes an architectural measurement +* register (MR) +* @mr_name: name of the MR +* @mr_value: buffer containing the current value of the MR +* @mr_size: size of the MR - typically the digest size of @mr_hash +* @mr_flags: bitwise OR of one or more flags, detailed below +* @mr_hash: optional hash identifier defined in include/uapi/linux/hash_info.h. +* +* A CC guest driver encloses an array of this structure in struct +* tsm_measurements to detail the measurement facility supported by the +* underlying CC hardware.
include/linux/tsm-mr.h line 29
Quoted 10 lines
include/linux/tsm-mr.h new file +10
-0+* +* @mr_name and @mr_value must stay valid until this structure is no longer in +* use. +* +* @mr_flags is the bitwise-OR of zero or more of the flags below. +* +* * %TSM_MR_F_READABLE - the sysfs attribute corresponding to this MR is readable. +* * %TSM_MR_F_WRITABLE - the sysfs attribute corresponding to this MR is writable. +* * %TSM_MR_F_LIVE - this MR's value may differ from the last value written, so +* must be read back from the underlying CC hardware/firmware.
Maybe use the word "extend" somewhere in this description for clarity.
Quoted 42 lines
include/linux/tsm-mr.h new file +42
-0+* * %TSM_MR_F_RTMR - bitwise-OR of %TSM_MR_F_LIVE and %TSM_MR_F_WRITABLE. +* * %TSM_MR_F_NOHASH - this MR does NOT have an associated hash algorithm. +* @mr_hash will be ignored when this flag is set. +*/ +struct tsm_measurement_register { +const char *mr_name; +void *mr_value; +u32 mr_size; +u32 mr_flags; +enum hash_algo mr_hash; +}; + +#define TSM_MR_F_NOHASH 1 +#define TSM_MR_F_WRITABLE 2 +#define TSM_MR_F_READABLE 4 +#define TSM_MR_F_LIVE 8 +#define TSM_MR_F_RTMR (TSM_MR_F_LIVE | TSM_MR_F_WRITABLE) + +#define TSM_MR_ (mr, hash) \ + .mr_name = #mr, .mr_size = hash##_DIGEST_SIZE, \ + .mr_hash = HASH_ALGO_##hash, .mr_flags = TSM_MR_F_READABLE + +/** +* struct tsm_measurements - Defines the CC-specific measurement facility and +* methods for updating measurement registers (MRs). +* @name: Optional parent directory name. +* @mrs: Array of MR definitions. +* @nr_mrs: Number of elements in @mrs. +* @refresh: Callback function to load/sync all MRs from TVM hardware/firmware +* into the kernel cache. +* @write: Callback function to write to the MR specified by the parameter @mr. +* +* @refresh takes two parameters: +* +* * @tm - points back to this structure. +* * @mr - points to the MR (an element of @mrs) being read (hence triggered +* this callback). +* +* Note that @refresh is invoked only when an MR with %TSM_MR_F_LIVE set is +* being read and the cache is stale. However, @refresh must reload not only +* the MR being read (@mr) but also all MRs with %TSM_MR_F_LIVE set. +*
include/linux/tsm-mr.h line 81
Quoted 10 lines
include/linux/tsm-mr.h new file +10
-0+* @write takes an additional parameter besides @tm and @mr: +* +* * @data - contains the bytes to write and whose size is @mr->mr_size. +* +* Both @refresh and @write should return 0 on success and an appropriate error +* code on failure. +*/ +struct tsm_measurements { +const char *name; + const struct tsm_measurement_register *mrs __counted_by(nr_mrs);
I had assumed that __counted_by() is only for inline flexible arrays, not out-of-line dynamically allocated arrays. Are you sure this does what you expect?
Quoted 16 lines
include/linux/tsm-mr.h new file +12
-0+size_t nr_mrs; +int (*refresh)(const struct tsm_measurements *tm, +const struct tsm_measurement_register *mr); +int (*write)(const struct tsm_measurements *tm, + const struct tsm_measurement_register *mr, const u8 *data); +}; + +const struct attribute_group *__must_check +tsm_mr_create_attribute_group (const struct tsm_measurements *tm); +void tsm_mr_free_attribute_group (const struct attribute_group *attr_grp); + +#endif Signature · 2 lines
-- 2.43.0
Re: [PATCH v3 1/5] tsm-mr: Add TVM Measurement Register support
Quoted 1 line · Dan Williams
Cedric Xing wrote:
[...]
Quoted 10 lines
Quoted 6 lines
--- MAINTAINERS | 4 +- drivers/virt/coco/Kconfig | 5 ++ drivers/virt/coco/Makefile | 1 + drivers/virt/coco/tsm-mr.c | 209 +++++++++++++++++++++++++++++++++++++++++++++ include/linux/tsm-mr.h | 93 ++++++++++++++++++++I note that the pending proposals for TEE I/O suggests splitting
drivers/virt/coco/intodrivers/virt/coco/{host,guest}[1] [2] [3].
Quoted 7 lines
[1]: http://lore.kernel.org/174107246021.1288555.7203769833791489618.stgit@dwillia2-xfh.jf.intel.com
[2]: http://lore.kernel.org/[email protected]
[3]: http://lore.kernel.org/[email protected]So if I take this through devsec.git I will get that rename queued and ask you to rebase on top of that.
No problem.
[...]
Quoted 19 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+ /* + * @ctx->in_sync indicates if any MRs have been written since the last + * ctx->refresh() call. When @ctx->in_sync is false, ctx->refresh() is + * necessary to sync the cached values of all live MRs (i.e., with + * %TSM_MR_F_LIVE set) with the underlying hardware. + */Code comments should add to the understanding of the code, not simply restate the code in prose. So I would replace this comment with some non-obvious insight to aid future maintenance, something like:
/*
* Note that the typical read path for MRs is via an attestation report,
* this is why the ->write() path does not automatically ->refresh()
* invalidated data forTSM_MR_LIVEregisters. The use case for reading
* back a individual hash-extending-write to an MR is for debug not
* attestation.
*/
Quoted 3 lines
...at least an explanation like that would have helped me understand the locking and caching model of this implementation.
The reasoning behind this involves not only ->refresh() and ->write() but also LIVE and ->in_sync. Generally, both ->refresh() and ->write() could be expensive so we are trying to do only the minimum. I'll add comments to the definition of this context structure.
[...]
Quoted 16 lines
Quoted 16 lines
(quoted diff) +16
-0+static ssize_t tm_digest_write(struct file *filp, struct kobject *kobj, + const struct bin_attribute *attr, char *buffer, + loff_t off, size_t count) +{ + struct tm_context *ctx; + const struct tsm_measurement_register *mr; + ssize_t rc; + + /* partial writes are not supported */ + if (off != 0 || count != attr->size) + return -EINVAL; + + ctx = attr->private; + mr = &ctx->tm->mrs[attr - ctx->mrs]; + + rc = down_write_killable(&ctx->rwsem);
Quoted 9 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+ if (rc) + return rc; + + rc = ctx->tm->write(ctx->tm, mr, buffer);There needs to be explicit ABI and driver-api documentation here for what are the allowed error codes that ->write() can return so as not to be confused with EINVAL or EINTR arising from user error or interrupt.
I understand your point. But different CC archs may have arch specific reasons for failures. It'd be hard to whitelist all the allowed errors; while blacklisting EINVAL may make more sense, as users have no control over the input (hence cannot provide invalid input) to arch specific write/extend functions. I'll add to the description of ->write() in its kernel-doc comments.
[...]
Quoted 14 lines
Quoted 14 lines
+/**
+ *tsm_mr_create_attribute_group()- creates an attribute group for measurement
+ * registers
+ * @tm: pointer to &structtsm_measurementscontaining the MR definitions.
+ *
+ * This function creates attributes corresponding to the MR definitions
+ * provided by @tm->mrs.
+ *
+ * The created attributes will reference @tm and its members. The caller must
+ * not free @tm until aftertsm_mr_free_attribute_group()is called.
+ *
+ * Context: Process context. May sleep due to memory allocation.
+ *
+ * Return:
Quoted 9 lines
Quoted 5 lines
+ * * On success, the pointer to a an attribute group is returned; otherwise
+ * * %-EINVAL - Invalid MR definitions.
+ * * %-ENOMEM - Out of memory.
+ */
+const structattribute_group*__must_checkNo need to mark this function as
__must_check. That attribute is typically reserved for core-apis.
Will remove.
Quoted 7 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+tsm_mr_create_attribute_group(const struct tsm_measurements *tm) +{ + if (!tm->mrs) + return ERR_PTR(-EINVAL);If you're going to check for !tm->mrs, might as well also check for !tm.
Good catch! Will change.
Quoted 7 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+ + /* aggregated length of all MR names */ + size_t nlen = 0;Typically the only exceptions for not declaring variables at the top of a function are for "for ()" loops and scope-based cleanup.
Will move it to the top.
Quoted 6 lines
Quoted 6 lines
(quoted diff) +6
-0+ + for (size_t i = 0; i < tm->nr_mrs; ++i) { + if ((tm->mrs[i].mr_flags & TSM_MR_F_LIVE) && !tm->refresh) + return ERR_PTR(-EINVAL); + + if ((tm->mrs[i].mr_flags & TSM_MR_F_WRITABLE) && !tm->write)
Quoted 9 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+ return ERR_PTR(-EINVAL); + + if (tm->mrs[i].mr_flags & TSM_MR_F_NOHASH) + continue; + + if (WARN_ON(tm->mrs[i].mr_hash >= HASH_ALGO__LAST))Why potentially crash the kernel here? EINVAL should be sufficient.
Agreed! Will change.
Quoted 19 lines
Quoted 19 lines
(quoted diff) +19
-0+ return ERR_PTR(-EINVAL); + + /* MR sysfs attribute names have the form of MRNAME:HASH */ + nlen += strlen(tm->mrs[i].mr_name) + 1 + + strlen(hash_algo_name[tm->mrs[i].mr_hash]) + 1; + } + + /* + * @bas and the MR name strings are combined into a single allocation + * so that we don't have to free MR names one-by-one in + * tsm_mr_free_attribute_group() + */ + struct bin_attribute **bas __free(kfree) = + kzalloc(sizeof(*bas) * (tm->nr_mrs + 1) + nlen, GFP_KERNEL); + struct tm_context *ctx __free(kfree) = + kzalloc(struct_size(ctx, mrs, tm->nr_mrs), GFP_KERNEL); + char *name, *end; + + if (!ctx || !bas)
Quoted 9 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+ return ERR_PTR(-ENOMEM); + + /* @bas is followed immediately by MR name strings */ + name = (char *)&bas[tm->nr_mrs + 1];I looked for a helper macro for a "buffer at the end of a structure", but could not immediately find one. It feels like something Linux should already have.
Given a pointer some_struct_p, the end of it will be (some_struct_p + 1) or &some_struct_p[1]. It'd be more readable to be wrapped by a macro for sure.
Quoted 23 lines
Quoted 23 lines
(quoted diff) +23
-0+ end = name + nlen; + + for (size_t i = 0; i < tm->nr_mrs; ++i) { + bas[i] = &ctx->mrs[i]; + sysfs_bin_attr_init(bas[i]); + + if (tm->mrs[i].mr_flags & TSM_MR_F_NOHASH) + bas[i]->attr.name = tm->mrs[i].mr_name; + else if (name < end) { + bas[i]->attr.name = name; + name += snprintf(name, end - name, "%s:%s", + tm->mrs[i].mr_name, + hash_algo_name[tm->mrs[i].mr_hash]); + ++name; + } else + return ERR_PTR(-EINVAL); + + /* check for duplicated MR definitions */ + for (size_t j = 0; j < i; ++j) + if (!strcmp(bas[i]->attr.name, bas[j]->attr.name)) + return ERR_PTR(-EINVAL); + + if (tm->mrs[i].mr_flags & TSM_MR_F_READABLE) {
Quoted 9 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+ bas[i]->attr.mode |= 0444; + bas[i]->read_new = tm_digest_read; + } + + if (tm->mrs[i].mr_flags & TSM_MR_F_WRITABLE) { + bas[i]->attr.mode |= 0220;Typical expectation for writable attributes is 0200.
Will change.
[...]
Quoted 13 lines
Review
Quoted 9 lines
(quoted diff) +9
-0+/** + * tsm_mr_free_attribute_group() - frees the attribute group returned by + * tsm_mr_create_attribute_group() + * @attr_grp: attribute group returned by tsm_mr_create_attribute_group() + * + * Context: Process context. + */ +void tsm_mr_free_attribute_group(const struct attribute_group *attr_grp) +{Related to the removal of
__must_checkadd safety here for cases where someone passes in anERR_PTR():
Quoted 5 lines
if (IS_ERR_OR_NULL(attr_grp) return;This also makes the function amenable to scope-based cleanup.
Will change.
[...]
Quoted 23 lines
Quoted 14 lines
+/**
+ * structtsm_measurement_register- describes an architectural measurement
+ * register (MR)
+ * @mr_name: name of the MR
+ * @mr_value: buffer containing the current value of the MR
+ * @mr_size: size of the MR - typically the digest size of @mr_hash
+ * @mr_flags: bitwise OR of one or more flags, detailed below
+ * @mr_hash: optional hash identifier defined ininclude/uapi/linux/hash_info.h.
+ *
+ * A CC guest driver encloses an array of this structure in struct
+ *tsm_measurementsto detail the measurement facility supported by the
+ * underlying CC hardware.
+ *
+ * @mr_name and @mr_value must stay valid until this structure is no longer inQuoted 8 lines
+ * use.
+ *
+ * @mr_flags is the bitwise-OR of zero or more of the flags below.
+ *
+ * * %TSM_MR_F_READABLE - the sysfs attribute corresponding to this MR is readable.
+ * * %TSM_MR_F_WRITABLE - the sysfs attribute corresponding to this MR is writable.
+ * * %TSM_MR_F_LIVE - this MR's value may differ from the last value written, so
+ * must be read back from the underlying CC hardware/firmware.
Quoted 2 lines
Maybe use the word "extend" somewhere in this description for clarity.
Will clarify.
[...]
Quoted 8 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+struct tsm_measurements { + const char *name; + const struct tsm_measurement_register *mrs __counted_by(nr_mrs);I had assumed that
__counted_by()is only for inline flexible arrays, not out-of-line dynamically allocated arrays. Are you sure this does what you expect?
Thanks for pointing this out! I misunderstood __counted_by, and will remove it.
-Cedric
PATCH v3 2/5 tsm-mr: Add tsm-mr sample code
In reply to Cedric XingThis sample kernel module demonstrates how to make MRs accessible to user mode through the tsm-mr library.
Once loaded, this module registers a miscdevice that host a set of emulated measurement registers as shown in the directory tree below.
/sys/class/misc/tsm_mr_sample
└── emulated_mr
├── config_mr
├── report_digest:sha512
├── rtmr0:sha256
├── rtmr1:sha384
├── rtmr_crypto_agile:sha256
├── rtmr_crypto_agile:sha384
└── static_mr:sha384Among the MRs in this example:
- config_mr demonstrates a hashless MR, like MRCONFIGID in Intel TDX or HOSTDATA in AMD SEV.
- static_mr demonstrates a static MR. The suffix :sha384 indicates its value is a sha384 digest.
- rtmr0 is an RTMR with TSM_MR_F_WRITABLE **cleared**, preventing direct extensions; as a result, the attribute rtmr0:sha256 is read-only.
- rtmr1 is an RTMR with TSM_MR_F_WRITABLE **set**, permitting direct extensions; thus, the attribute rtmr1:sha384 is writable.
- rtmr_crypto_agile demonstrates a "single" MR that supports multiple hash algorithms. Each supported algorithm has a corresponding digest, usually referred to as a "bank" in TCG terminology. In this specific sample, the 2 banks are aliased to rtmr0 and rtmr1, respectively.
- report_digest contains the digest of the internal report structure living in this sample module's memory. It is to demonstrate the use of the TSM_MR_F_LIVE flag. Its value changes each time an RTMR is extended.
--- MAINTAINERS | 1 + samples/Kconfig | 10 +++ samples/Makefile | 1 + samples/tsm-mr/Makefile | 2 + samples/tsm-mr/tsm_mr_sample.c | 138 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 152 insertions(+)
MAINTAINERS +1 -0
@@ -24560,6 +24560,7 @@ S: Maintained F: Documentation/ABI/testing/configfs-tsm F: drivers/virt/coco/tsm*.c F: include/linux/tsm*.h +F: samples/tsm/* TRUSTED SERVICES TEE DRIVER M: Balint Dobszay <[email protected]>
samples/Kconfig +10 -0
@@ -184,6 +184,16 @@ config SAMPLE_TIMER bool "Timer sample" depends on CC_CAN_LINK && HEADERS_INSTALL +config SAMPLE_TSM_MR + tristate "TSM measurement sample" + select TSM_MEASUREMENTS + help + Build a sample module that emulates MRs (Measurement Registers) and + exposes them to user mode applications through the TSM sysfs + interface (/sys/class/misc/tsm_mr_sample/emulated_mr/). + + The module name will be tsm-mr-sample when built as a module. + config SAMPLE_UHID bool "UHID sample" depends on CC_CAN_LINK && HEADERS_INSTALL
samples/Makefile +1 -0
@@ -43,3 +43,4 @@ obj-$(CONFIG_SAMPLES_RUST) += rust/ obj-$(CONFIG_SAMPLE_DAMON_WSSE) += damon/ obj-$(CONFIG_SAMPLE_DAMON_PRCL) += damon/ obj-$(CONFIG_SAMPLE_HUNG_TASK) += hung_task/ +obj-$(CONFIG_SAMPLE_TSM_MR) += tsm-mr/
samples/tsm-mr/Makefile new file +2 -0
@@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-only +obj-$(CONFIG_SAMPLE_TSM_MR) += tsm_mr_sample.o
samples/tsm-mr/tsm_mr_sample.c new file +138 -0
138 lines not on this page · raw message
Signature · 1 line
2.43.0
kernel test robot
build botRe: [PATCH v3 2/5] tsm-mr: Add tsm-mr sample code
kernel test robot noticed the following build errors:
Hi Cedric,
kernel test robot noticed the following build errors:
[auto build test ERROR on 0af2f6be1b4281385b618cb86ad946eded089ac8]
url: https://github.com/intel-lab-lkp/linux/commits/Cedric-Xing/tsm-mr-Add-TVM-Measurement-Register-support/20250408-032813 base: 0af2f6be1b4281385b618cb86ad946eded089ac8 patch link: https://lore.kernel.org/r/20250407-tdx-rtmr-v3-2-54f17bc65228%40intel.com patch subject: [PATCH v3 2/5] tsm-mr: Add tsm-mr sample code config: x86_64-allyesconfig (https://download.01.org/0day-ci/archive/20250409/[email protected]/config) compiler: clang version 20.1.2 (https://github.com/llvm/llvm-project 58df0ef89dd64126512e4ee27b4ac3fd8ddf6247) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250409/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
In file included from samples/tsm-mr/tsm_mr_sample.c:8:
>> include/linux/tsm-mr.h:81:58: error: use of undeclared identifier 'nr_mrs'
81 | const struct tsm_measurement_register *mrs __counted_by(nr_mrs);
| ^1 error generated.
vim +/nr_mrs +81 include/linux/tsm-mr.h
b6f2f446f66ff9 Cedric Xing 2025-04-07 47
b6f2f446f66ff9 Cedric Xing 2025-04-07 48 #define TSM_MR_(mr, hash) \
b6f2f446f66ff9 Cedric Xing 2025-04-07 49 .mr_name = #mr, .mr_size = hash##_DIGEST_SIZE, \
b6f2f446f66ff9 Cedric Xing 2025-04-07 50 .mr_hash = HASH_ALGO_##hash, .mr_flags = TSM_MR_F_READABLE
b6f2f446f66ff9 Cedric Xing 2025-04-07 51
b6f2f446f66ff9 Cedric Xing 2025-04-07 52 /**
b6f2f446f66ff9 Cedric Xing 2025-04-07 53 * struct tsm_measurements - Defines the CC-specific measurement facility and
b6f2f446f66ff9 Cedric Xing 2025-04-07 54 * methods for updating measurement registers (MRs).
b6f2f446f66ff9 Cedric Xing 2025-04-07 55 * @name: Optional parent directory name.
b6f2f446f66ff9 Cedric Xing 2025-04-07 56 * @mrs: Array of MR definitions.
b6f2f446f66ff9 Cedric Xing 2025-04-07 57 * @nr_mrs: Number of elements in @mrs.
b6f2f446f66ff9 Cedric Xing 2025-04-07 58 * @refresh: Callback function to load/sync all MRs from TVM hardware/firmware
b6f2f446f66ff9 Cedric Xing 2025-04-07 59 * into the kernel cache.
b6f2f446f66ff9 Cedric Xing 2025-04-07 60 * @write: Callback function to write to the MR specified by the parameter @mr.
b6f2f446f66ff9 Cedric Xing 2025-04-07 61 *
b6f2f446f66ff9 Cedric Xing 2025-04-07 62 * @refresh takes two parameters:
b6f2f446f66ff9 Cedric Xing 2025-04-07 63 *
b6f2f446f66ff9 Cedric Xing 2025-04-07 64 * * @tm - points back to this structure.
b6f2f446f66ff9 Cedric Xing 2025-04-07 65 * * @mr - points to the MR (an element of @mrs) being read (hence triggered
b6f2f446f66ff9 Cedric Xing 2025-04-07 66 * this callback).
b6f2f446f66ff9 Cedric Xing 2025-04-07 67 *
b6f2f446f66ff9 Cedric Xing 2025-04-07 68 * Note that @refresh is invoked only when an MR with %TSM_MR_F_LIVE set is
b6f2f446f66ff9 Cedric Xing 2025-04-07 69 * being read and the cache is stale. However, @refresh must reload not only
b6f2f446f66ff9 Cedric Xing 2025-04-07 70 * the MR being read (@mr) but also all MRs with %TSM_MR_F_LIVE set.
b6f2f446f66ff9 Cedric Xing 2025-04-07 71 *
b6f2f446f66ff9 Cedric Xing 2025-04-07 72 * @write takes an additional parameter besides @tm and @mr:
b6f2f446f66ff9 Cedric Xing 2025-04-07 73 *
b6f2f446f66ff9 Cedric Xing 2025-04-07 74 * * @data - contains the bytes to write and whose size is @mr->mr_size.
b6f2f446f66ff9 Cedric Xing 2025-04-07 75 *
b6f2f446f66ff9 Cedric Xing 2025-04-07 76 * Both @refresh and @write should return 0 on success and an appropriate error
b6f2f446f66ff9 Cedric Xing 2025-04-07 77 * code on failure.
b6f2f446f66ff9 Cedric Xing 2025-04-07 78 */
b6f2f446f66ff9 Cedric Xing 2025-04-07 79 struct tsm_measurements {
b6f2f446f66ff9 Cedric Xing 2025-04-07 80 const char *name;
b6f2f446f66ff9 Cedric Xing 2025-04-07 @81 const struct tsm_measurement_register *mrs __counted_by(nr_mrs);
b6f2f446f66ff9 Cedric Xing 2025-04-07 82 size_t nr_mrs;
b6f2f446f66ff9 Cedric Xing 2025-04-07 83 int (*refresh)(const struct tsm_measurements *tm,
b6f2f446f66ff9 Cedric Xing 2025-04-07 84 const struct tsm_measurement_register *mr);
b6f2f446f66ff9 Cedric Xing 2025-04-07 85 int (*write)(const struct tsm_measurements *tm,
b6f2f446f66ff9 Cedric Xing 2025-04-07 86 const struct tsm_measurement_register *mr, const u8 *data);
b6f2f446f66ff9 Cedric Xing 2025-04-07 87 };b6f2f446f66ff9 Cedric Xing 2025-04-07 88
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [PATCH v3 2/5] tsm-mr: Add tsm-mr sample code
In reply to Cedric XingQuoted 37 lines · Cedric Xing
This sample kernel module demonstrates how to make MRs accessible to user mode through the tsm-mr library.
Once loaded, this module registers a
miscdevicethat host a set of emulated measurement registers as shown in the directory tree below.
/sys/class/misc/tsm_mr_sample└── emulated_mr ├── config_mr ├── report_digest:sha512 ├── rtmr0:sha256 ├── rtmr1:sha384 ├── rtmr_crypto_agile:sha256 ├── rtmr_crypto_agile:sha384 └── static_mr:sha384Among the MRs in this example:
-
config_mrdemonstrates a hashless MR, like MRCONFIGID in Intel TDX or HOSTDATA in AMD SEV.
-static_mrdemonstrates a static MR. The suffix:sha384indicates its value is a sha384 digest.
-rtmr0is an RTMR withTSM_MR_F_WRITABLE**cleared**, preventing direct extensions; as a result, the attributertmr0:sha256is read-only.
-rtmr1is an RTMR withTSM_MR_F_WRITABLE**set**, permitting direct extensions; thus, the attributertmr1:sha384is writable.
-rtmr_crypto_agiledemonstrates a "single" MR that supports multiple hash algorithms. Each supported algorithm has a corresponding digest, usually referred to as a "bank" in TCG terminology. In this specific sample, the 2 banks are aliased tortmr0andrtmr1, respectively.
-report_digestcontains the digest of the internal report structure living in this sample module's memory. It is to demonstrate the use of theTSM_MR_F_LIVEflag. Its value changes each time an RTMR is extended.Signed-off-by Cedric Xing <[email protected]>
[..]
samples/tsm-mr/tsm_mr_sample.c line 96
Quoted 2 lines
samples/tsm-mr/tsm_mr_sample.c new file +2
-0+static struct tsm_measurements emulated_mr = { + .name ="emulated_mr" ,
I think the convention should be that all consumers use a common name for this common ABI, similar to a sysfs-class. So, I would say set the name to "measurement_registers" inside tsm_mr_create_attribute_group(), and make the "custom name" or "no-name" case an isolated corner case.
Other than that, thanks for taking the time to build this sample it makes the reviewing the implementation easier and allows for some ABI testing.
PATCH v3 3/5 x86/tdx: Add tdx_mcall_extend_rtmr() interface
In reply to Cedric XingThe TDX guest exposes one MRTD (Build-time Measurement Register) and four RTMR (Run-time Measurement Register) registers to record the build and boot measurements of a virtual machine (VM). These registers are similar to PCR (Platform Configuration Register) registers in the TPM (Trusted Platform Module) space. This measurement data is used to implement security features like attestation and trusted boot.
To facilitate updating the RTMR registers, the TDX module provides support for the TDG.MR.RTMR.EXTEND TDCALL which can be used to securely extend the RTMR registers.
Add helper function to update RTMR registers. It will be used by the TDX guest driver in enabling RTMR extension support.
--- arch/x86/coco/tdx/tdx.c | 36 ++++++++++++++++++++++++++++++++++++ arch/x86/include/asm/shared/tdx.h | 1 + arch/x86/include/asm/tdx.h | 2 ++ 3 files changed, 39 insertions(+)
arch/x86/coco/tdx/tdx.c +36 -0
@@ -36,6 +36,7 @@/* TDX Module call error codes */ #define TDCALL_RETURN_CODE (a) ((a) >> 32)#define TDCALL_INVALID_OPERAND 0xc0000100 +#define TDCALL_OPERAND_BUSY 0x80000200#define TDREPORT_SUBTYPE_0 0
@@ -136,6 +137,41 @@ int tdx_mcall_get_report0(u8 *reportdata, u8 *tdreport) }EXPORT_SYMBOL_GPL (tdx_mcall_get_report0); +/** +* tdx_mcall_extend_rtmr() - Wrapper to extend RTMR registers using +* TDG.MR.RTMR.EXTEND TDCALL. +* @index: Index of RTMR register to be extended. +* @data: Address of the input buffer with RTMR register extend data. +* +* Refer to section titled "TDG.MR.RTMR.EXTEND leaf" in the TDX Module +* v1.0 specification for more information on TDG.MR.RTMR.EXTEND TDCALL. +* It is used in the TDX guest driver module to allow user extend the +* RTMR registers (index > 1). +* +* Return 0 on success, -EINVAL for invalid operands, -EBUSY for busy +* operation or -EIO on other TDCALL failures. +*/ +int tdx_mcall_extend_rtmr (u8 index,u8 *data) +{ +struct tdx_module_args args = { + .rcx =virt_to_phys (data), + .rdx = index, + }; +u64 ret; + + ret =__tdcall (TDG_MR_RTMR_EXTEND , &args); +if (ret) { +if (TDCALL_RETURN_CODE (ret) ==TDCALL_INVALID_OPERAND ) +return -EINVAL ; +if (TDCALL_RETURN_CODE (ret) ==TDCALL_OPERAND_BUSY ) +return -EBUSY ; +return -EIO ; + } + +return 0 ; +} +EXPORT_SYMBOL_GPL (tdx_mcall_extend_rtmr); +/** * tdx_hcall_get_quote() - Wrapper to request TD Quote using GetQuote * hypercall.
arch/x86/include/asm/shared/tdx.h +1 -0
@@ -13,6 +13,7 @@/* TDX module Call Leaf IDs */ #define TDG_VP_VMCALL 0#define TDG_VP_INFO 1 +#define TDG_MR_RTMR_EXTEND 2#define TDG_VP_VEINFO_GET 3#define TDG_MR_REPORT 4#define TDG_MEM_PAGE_ACCEPT 6
arch/x86/include/asm/tdx.h +2 -0
@@ -64,6 +64,8 @@ bool tdx_early_handle_ve(struct pt_regs *regs);int tdx_mcall_get_report0 (u8 *reportdata,u8 *tdreport); +int tdx_mcall_extend_rtmr (u8 index,u8 *data); +u64 tdx_hcall_get_quote (u8 *buf,size_t size);void __init tdx_dump_attributes (u64 td_attr);
Signature · 1 line
2.43.0
Re: [PATCH v3 3/5] x86/tdx: Add tdx_mcall_extend_rtmr() interface
Quoted 7 lines · Cedric Xing
The TDX guest exposes one MRTD (Build-time Measurement Register) and four RTMR (Run-time Measurement Register) registers to record the build and boot measurements of a virtual machine (VM). These registers are similar to PCR (Platform Configuration Register) registers in the TPM (Trusted Platform Module) space. This measurement data is used to implement security features like attestation and trusted boot.
Quoted 8 lines
To facilitate updating the RTMR registers, the TDX module provides support for the
TDG.MR.RTMR.EXTENDTDCALL which can be used to securely extend the RTMR registers.Add helper function to update RTMR registers. It will be used by the TDX guest driver in enabling RTMR extension support.
Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>
Typically Signed-off-by without Co-developed-by means that the patch was submitted upstream be Sathya, so did you also intend to add a Co-developed-by or should this solo tag just be Reviewed-by?
Quoted 39 lines
Signed-off-by Cedric Xing <[email protected]>--- arch/x86/coco/tdx/tdx.c | 36 ++++++++++++++++++++++++++++++++++++ arch/x86/include/asm/shared/tdx.h | 1 + arch/x86/include/asm/tdx.h | 2 ++ 3 files changed, 39 insertions(+)arch/x86/coco/tdx/tdx.c +17
-0@@ -36,6 +36,7 @@/* TDX Module call error codes */ #define TDCALL_RETURN_CODE (a) ((a) >> 32)#define TDCALL_INVALID_OPERAND 0xc0000100 +#define TDCALL_OPERAND_BUSY 0x80000200#define TDREPORT_SUBTYPE_0 0@@ -136,6 +137,41 @@ int tdx_mcall_get_report0(u8 *reportdata, u8 *tdreport) }EXPORT_SYMBOL_GPL (tdx_mcall_get_report0); +/** +* tdx_mcall_extend_rtmr() - Wrapper to extend RTMR registers using +* TDG.MR.RTMR.EXTEND TDCALL. +* @index: Index of RTMR register to be extended. +* @data: Address of the input buffer with RTMR register extend data. +* +* Refer to section titled "TDG.MR.RTMR.EXTEND leaf" in the TDX Module +* v1.0 specification for more information on TDG.MR.RTMR.EXTEND TDCALL. +* It is used in the TDX guest driver module to allow user extend the +* RTMR registers (index > 1). +* +* Return 0 on success, -EINVAL for invalid operands, -EBUSY for busy +* operation or -EIO on other TDCALL failures. +*/ +int tdx_mcall_extend_rtmr (u8 index,u8 *data) +{
arch/x86/coco/tdx/tdx.c int tdx_mcall_get_report0(u8 *reportdata, u8 *tdreport) · line 165
Quoted 10 lines
arch/x86/coco/tdx/tdx.c +10
-0+struct tdx_module_args args = { + .rcx =virt_to_phys (data), + .rdx = index, + }; +u64 ret; + + ret =__tdcall (TDG_MR_RTMR_EXTEND , &args); +if (ret) { +if (TDCALL_RETURN_CODE (ret) ==TDCALL_INVALID_OPERAND ) +return -EINVAL ;
Here is where the ABI documentation can help to make sure that userspace can tell the difference between userspace bugs, kernel bugs, or TDX internal errors. So perhaps translate this EINVAL to
ENXIO in tsm-mr.c. Otherwise, this patch looks good to me:
[..]
Re: [PATCH v3 3/5] x86/tdx: Add tdx_mcall_extend_rtmr() interface
Quoted 12 lines · Dan Williams
Quoted 11 lines · Cedric Xing
The TDX guest exposes one MRTD (Build-time Measurement Register) and four RTMR (Run-time Measurement Register) registers to record the build and boot measurements of a virtual machine (VM). These registers are similar to PCR (Platform Configuration Register) registers in the TPM (Trusted Platform Module) space. This measurement data is used to implement security features like attestation and trusted boot.
To facilitate updating the RTMR registers, the TDX module provides support for the
TDG.MR.RTMR.EXTENDTDCALL which can be used to securely extend the RTMR registers.
Quoted 9 lines
Quoted 4 lines
Add helper function to update RTMR registers. It will be used by the TDX guest driver in enabling RTMR extension support.
Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>Typically Signed-off-by without Co-developed-by means that the patch was submitted upstream be Sathya, so did you also intend to add a Co-developed-by or should this solo tag just be Reviewed-by?
I did modify slightly this commit from Sathya. I could be wrong but was told that "Signed-off-by:" was necessary to certify I had the authority to submit this patch. scripts/checkpatch.py complained about the co-existence of "Co-developed-by" and "Signed-off-by". So I had to keep the "Signed-off-by" tag only.
Quoted 55 lines
Quoted 39 lines
Signed-off-by Cedric Xing <[email protected]>--- arch/x86/coco/tdx/tdx.c | 36 ++++++++++++++++++++++++++++++++++++ arch/x86/include/asm/shared/tdx.h | 1 + arch/x86/include/asm/tdx.h | 2 ++ 3 files changed, 39 insertions(+)arch/x86/coco/tdx/tdx.c +17
-0@@ -36,6 +36,7 @@/* TDX Module call error codes */ #define TDCALL_RETURN_CODE (a) ((a) >> 32)#define TDCALL_INVALID_OPERAND 0xc0000100 +#define TDCALL_OPERAND_BUSY 0x80000200#define TDREPORT_SUBTYPE_0 0@@ -136,6 +137,41 @@ int tdx_mcall_get_report0(u8 *reportdata, u8 *tdreport) }EXPORT_SYMBOL_GPL (tdx_mcall_get_report0); +/** +* tdx_mcall_extend_rtmr() - Wrapper to extend RTMR registers using +* TDG.MR.RTMR.EXTEND TDCALL. +* @index: Index of RTMR register to be extended. +* @data: Address of the input buffer with RTMR register extend data. +* +* Refer to section titled "TDG.MR.RTMR.EXTEND leaf" in the TDX Module +* v1.0 specification for more information on TDG.MR.RTMR.EXTEND TDCALL. +* It is used in the TDX guest driver module to allow user extend the +* RTMR registers (index > 1). +* +* Return 0 on success, -EINVAL for invalid operands, -EBUSY for busy +* operation or -EIO on other TDCALL failures. +*/ +int tdx_mcall_extend_rtmr (u8 index,u8 *data) +{arch/x86/coco/tdx/tdx.c int tdx_mcall_get_report0(u8 *reportdata, u8 *tdreport) · line 165
Quoted 10 lines
arch/x86/coco/tdx/tdx.c +10
-0+struct tdx_module_args args = { + .rcx =virt_to_phys (data), + .rdx = index, + }; +u64 ret; + + ret =__tdcall (TDG_MR_RTMR_EXTEND , &args); +if (ret) { +if (TDCALL_RETURN_CODE (ret) ==TDCALL_INVALID_OPERAND ) +return -EINVAL ;Here is where the ABI documentation can help to make sure that userspace can tell the difference between userspace bugs, kernel bugs, or TDX internal errors. So perhaps translate this EINVAL to
ENXIO intsm-mr.c. Otherwise, this patch looks good to me:
Quoted 2 lines
Reviewed-by Dan Williams <[email protected]>
Agreed. I'll change -EINVAL to -ENXIO as this would be due to the inability to convert VA to GPA.
Quoted 1 line
[..]
PATCH v3 4/5 x86/tdx: tdx_mcall_get_report0: Return -EBUSY on TDCALL_OPERAND_BUSY error
In reply to Cedric XingReturn -EBUSY from tdx_mcall_get_report0() when TDG.MR.REPORT returns TDCALL_OPERAND_BUSY. This enables the caller to retry obtaining a TDREPORT later if another VCPU is extending an RTMR concurrently.
--- arch/x86/coco/tdx/tdx.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
arch/x86/coco/tdx/tdx.c +4 -2
@@ -114,8 +114,8 @@ static inline u64 tdg_vm_wr(u64 field, u64 value, u64 mask)* v1.0 specification for more information on TDG.MR.REPORT TDCALL. * It is used in the TDX guest driver module to get the TDREPORT0. * -* Return 0 on success, -EINVAL for invalid operands, or -EIO on -* other TDCALL failures. +* Return 0 on success, -EINVAL for invalid operands, -EBUSY for busy +* operation or -EIO on other TDCALL failures. */ int tdx_mcall_get_report0 (u8 *reportdata,u8 *tdreport) {
@@ -130,6 +130,8 @@ int tdx_mcall_get_report0(u8 *reportdata, u8 *tdreport)if (ret) {if (TDCALL_RETURN_CODE (ret) ==TDCALL_INVALID_OPERAND )return -EINVAL ; +else if (TDCALL_RETURN_CODE (ret) ==TDCALL_OPERAND_BUSY ) +return -EBUSY ;return -EIO ; }
Signature · 1 line
2.43.0
Re: [PATCH v3 4/5] x86/tdx: tdx_mcall_get_report0: Return -EBUSY on TDCALL_OPERAND_BUSY error
Quoted 3 lines · Cedric Xing
Return
-EBUSYfromtdx_mcall_get_report0()whenTDG.MR.REPORTreturnsTDCALL_OPERAND_BUSY. This enables the caller to retry obtaining a TDREPORT later if another VCPU is extending an RTMR concurrently.
Can this not be prevented by proper locking? Otherwise this type of collision sounds like a kernel bug, not something that should escape to userspace.
I.e. userspace can not reasonably know when it is safe to retry, so take locks to ensure forward progress.
Re: [PATCH v3 4/5] x86/tdx: tdx_mcall_get_report0: Return -EBUSY on TDCALL_OPERAND_BUSY error
Quoted 9 lines · Dan Williams
Quoted 3 lines · Cedric Xing
Return
-EBUSYfromtdx_mcall_get_report0()whenTDG.MR.REPORTreturnsTDCALL_OPERAND_BUSY. This enables the caller to retry obtaining a TDREPORT later if another VCPU is extending an RTMR concurrently.Can this not be prevented by proper locking? Otherwise this type of collision sounds like a kernel bug, not something that should escape to userspace.
Yes, -EBUSY should never happen with proper locking, which however is implemented in the upper layer.
Similarly, -EINVAL will also indicate a kernel bug but is left for the upper layer to handle.
Quoted 2 lines
I.e. userspace can not reasonably know when it is safe to retry, so take locks to ensure forward progress.
tdx-guest does WARN() on errors. There are no other users of this function currently. Returning an error, however, will allow different error handling in the future (e.g., retry instead of WARN on -EBUSY).
PATCH v3 5/5 virt: tdx-guest: Expose TDX MRs as sysfs attributes
In reply to Cedric XingExpose the most commonly used TDX MRs (Measurement Registers) as sysfs attributes. Use the ioctl() interface of /dev/tdx_guest to request a full TDREPORT for access to other TD measurements.
Directory structure of TDX MRs inside a TDVM is as follows:
/sys/class/misc/tdx_guest
└── mr
├── mrconfigid
├── mrowner
├── mrownerconfig
├── mrtd:sha384
├── rtmr0:sha384
├── rtmr1:sha384
├── rtmr2:sha384
└── rtmr3:sha384Read the file/attribute to retrieve the current value of an MR. Write to the file/attribute (if writable) to extend the corresponding RTMR. Refer to Documentation/ABI/testing/sysfs-devices-virtual-misc-tdx_guest-mr for more information.
--- .../sysfs-devices-virtual-misc-tdx_guest-mr | 48 ++++++ MAINTAINERS | 1 + drivers/virt/coco/tdx-guest/Kconfig | 1 + drivers/virt/coco/tdx-guest/tdx-guest.c | 169 ++++++++++++++++++++- 4 files changed, 217 insertions(+), 2 deletions(-)
Documentation/ABI/testing/sysfs-devices-virtual-misc-tdx_guest-mr new file +48 -0
@@ -0,0 +1,48 @@ +What: /sys/devices/virtual/misc/tdx_guest/mr/MRNAME[:HASH] +Date: April, 2025 +KernelVersion: v6.16 +Contact: [email protected] +Description: + Value of a TDX measurement register (MR). MRNAME and HASH above + are placeholders. The optional suffix :HASH is used for MRs + that have associated hash algorithms. See below for a complete + list of TDX MRs exposed via sysfs. Comprehensive information is + available at https://intel.com/tdx + +What: /sys/devices/virtual/misc/tdx_guest/mr/mrconfigid +Date: April, 2025 +KernelVersion: v6.16 +Contact: [email protected] +Description: + (RO) Value of MRCONFIGID - immutable storage for SW use. + +What: /sys/devices/virtual/misc/tdx_guest/mr/mrowner +Date: April, 2025 +KernelVersion: v6.16 +Contact: [email protected] +Description: + (RO) Value of MROWNER - immutable storage for SW use. + +What: /sys/devices/virtual/misc/tdx_guest/mr/mrownerconfig +Date: April, 2025 +KernelVersion: v6.16 +Contact: [email protected] +Description: + (RO) Value of MROWNERCONFIG - immutable storage for SW use. + +What: /sys/devices/virtual/misc/tdx_guest/mr/mrtd:sha384 +Date: April, 2025 +KernelVersion: v6.16 +Contact: [email protected] +Description: + (RO) Value of MRTD - the measurement of the initial memory + image of the current TD. + +What: /sys/devices/virtual/misc/tdx_guest/mr/rtmr[0123]:sha384 +Date: April, 2025 +KernelVersion: v6.16 +Contact: [email protected] +Description: + (RW) Read returns the current value of the RTMR. Write extends + the written buffer to the RTMR. All writes must start at offset + 0 and be 48 bytes in size. Partial writes are not supported.
MAINTAINERS +1 -0
@@ -26226,6 +26226,7 @@ L: [email protected] L: [email protected] S: Supported T: git git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git x86/tdx +F: Documentation/ABI/testing/sysfs-devices-virtual-misc-tdx_guest-mr F: arch/x86/boot/compressed/tdx* F: arch/x86/coco/tdx/ F: arch/x86/include/asm/shared/tdx.h
drivers/virt/coco/tdx-guest/Kconfig +1 -0
@@ -2,6 +2,7 @@ config TDX_GUEST_DRIVER tristate "TDX Guest driver" depends on INTEL_TDX_GUEST select TSM_REPORTS + select TSM_MEASUREMENTS help The driver provides userspace interface to communicate with the TDX module to request the TDX guest details like attestation
drivers/virt/coco/tdx-guest/tdx-guest.c +167 -2
225 lines not on this page · raw message
Signature · 1 line
2.43.0
Quoted 5 lines · Cedric Xing
NOTE: This patch series introduces the Measurement Register (MR) ABI, and is a continuation of the RFC series on the same topic [1].
Introduce the
CONFIG_TSM_MEASUREMENTShelper library (tsm-mr) as a cross-vendor transport schema to allow TVM (TEE VM) guest drives to export
s/drives/drivers like tdx-guest/
Quoted 7 lines
CC (Confidential Compute) architecture-specific MRs (Measurement Registers) as sysfs attributes/files. Enable applications to read, write/extend MRs like regular files, supporting various usages such as configuration verification (e.g., verify a TVM's configuration against digests stored in static/immutable MRs like MRCONFIGID on TDX or HOSTDATA on SEV) and runtime measurements (e.g., extend the measurement of a container image to an RTMR before running it).
What I do not see in this cover letter or patch1 is a brief summary of the major assumptions contributing to the design for folks that have not been closely following the threads. To me those are:
- Measurement registers are an architecture specific building block to enable attestation of system state. That can either be a vTPM application, or raw application direct use/extension of measurement values.
- By the nature of needing to expose architecture specific values the names and numbers of these measurements registers are not amenable to a shared transport mechanism like configs-tsm-reports, but the common operations are amenable to a shared library driven by a provided 'struct tsm_measurements' template.
Quoted 9 lines
Patches included in this series:
- Patch 1 adds the tsm-mr library to help TVM guest drivers exposing MRs as sysfs attributes.
- Patch 2 provides a sample module demonstrating the usage of the new tsm-mr library.
- The remaining patches update the TDX guest driver to expose TDX MRs with the help of the tsm-mr library.
Quoted 8 lines
[1]: https://lore.kernel.org/linux-coco/[email protected]/
Signed-off-by Cedric Xing <[email protected]>---
Changes in v3:
- tsm-mr: Separate measurement support (tsm-mr) from the original tsm source code. Modules depending on tsm-mr shouldselect TSM_MEASUREMENTSinKconfig.
It helps to note who gave the feedback leading to changes. Besides the small courtesy of credit it also helps to speed conflict resolution (blame) when the parties are aware of each other. So for v4 please note changes like this:
---
Changes in v3:
- tsm-mr: Separate measurement support (tsm-mr) from the original tsm source code. Modules depending on tsm-mr should select TSM_MEASUREMENTS in Kconfig. (Dan)
[..]
---
Lastly it helps to declare what you expect to happen with these patches. At a minimum these need an x86 ack. For upstream merge these can either go through the tip tree, or I can take them through devsec.git with other "TSM" work. Absent someone hollering, devsec.git is my expectation.