From nobody Thu Dec 18 12:35:41 2025 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.14]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7E29A1F4623 for ; Thu, 24 Apr 2025 20:12:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.14 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745525579; cv=none; b=VC9t79zu4eD8ufjm+oNwldTW+GvFjyr3gYRyVXK7bTAMZHvSgiUXq2GWi2Vi3Hfln6T3/b0G4jxw28jRtEgPVtF3i6CHqKD3p1JwEHgfuEiLxhn2uuX8WlnNrXGf8WYYeWY3E7Z04eYW+5aX0Cm0czHhFeaaQTiRmr+j2XoL6Hk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745525579; c=relaxed/simple; bh=DqF6yLnP21b8Z0nf+/en+zidQPF3jPb/mLABtLaILCk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=X80tbY5zF+r7kxKgEsXc33knHLlXlgOtXpPjUTzUVyAEk1DNsl1SK9RMdf1q2yEzBOdiDX4V8pJT59aCPrDsDBniAHL38vgIeC9nQv+vJ/bsI3st94Y7LvwyQXHqXgyL1/Pe41noC8zcFFOHw6x/uJkzKa5Jb/k0Qtg1WgoDEUg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=RHymDyFm; arc=none smtp.client-ip=198.175.65.14 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="RHymDyFm" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1745525575; x=1777061575; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=DqF6yLnP21b8Z0nf+/en+zidQPF3jPb/mLABtLaILCk=; b=RHymDyFmq9Cc3CjbyblViAYQonKx4YAVA3Hcyhgf/i0rK3bgStGAI4Ip zpv7F7DTddJURDoj3t46zCl9AounVFt33GOWElNhl28+/m9mbtxxteHeg 0h7HYHuCzvqo5jjwh/IPmkxeCXtnOJJsId3X+b8dLk8Yz3dGJPmohh3B8 j4VAFEoKy2UxEpy2gSSBxxQeMs11hkld6aCwMNDawwl43glsM3+o4sflV tr9EejyANVGmCHw1ZpUIPkaHzUPZmcygyibsQigflC9cPciVC7jEO7KKf yHLiSorWX0WPSHDWHfaAwaLFK72Wl6p1r3HhnklkQKGFu/CeIgAGONQc5 w==; X-CSE-ConnectionGUID: ITY91B0pRPqXNE2Qjv9hwg== X-CSE-MsgGUID: +DzqNexJScqbCNTYRd9iOw== X-IronPort-AV: E=McAfee;i="6700,10204,11413"; a="50986696" X-IronPort-AV: E=Sophos;i="6.15,237,1739865600"; d="scan'208";a="50986696" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Apr 2025 13:12:55 -0700 X-CSE-ConnectionGUID: N3dAxudRRY+DuoImBHiM+w== X-CSE-MsgGUID: JWwNwbaMQQCCiMhc3TuMPw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.15,237,1739865600"; d="scan'208";a="155945362" Received: from ajatsiv-mobl.amr.corp.intel.com (HELO bxing-mobl1.clients.intel.com) ([10.246.112.111]) by fmviesa002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Apr 2025 13:12:55 -0700 From: Cedric Xing Date: Thu, 24 Apr 2025 15:12:25 -0500 Subject: [PATCH v5 1/5] tsm-mr: Add TVM Measurement Register support Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250424-tdx-rtmr-v5-1-4fe28ddf85d4@intel.com> References: <20250424-tdx-rtmr-v5-0-4fe28ddf85d4@intel.com> In-Reply-To: <20250424-tdx-rtmr-v5-0-4fe28ddf85d4@intel.com> To: Dan Williams , "Kirill A. Shutemov" , Dave Hansen , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Cc: linux-kernel@vger.kernel.org, linux-coco@lists.linux.dev, Dionna Amalie Glaze , Guorui Yu , James Bottomley , Dan Middleton , Mikko Ylinen , Sathyanarayanan Kuppuswamy , Cedric Xing X-Mailer: b4 0.13.0 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. Signed-off-by: Cedric Xing Reviewed-by: Dan Williams --- Documentation/driver-api/coco/index.rst | 12 + .../driver-api/coco/measurement-registers.rst | 12 + Documentation/driver-api/index.rst | 1 + MAINTAINERS | 4 +- drivers/virt/coco/Kconfig | 5 + drivers/virt/coco/Makefile | 1 + drivers/virt/coco/tsm-mr.c | 247 +++++++++++++++++= ++++ include/linux/tsm-mr.h | 94 ++++++++ include/trace/events/tsm_mr.h | 80 +++++++ 9 files changed, 454 insertions(+), 2 deletions(-) diff --git a/Documentation/driver-api/coco/index.rst b/Documentation/driver= -api/coco/index.rst new file mode 100644 index 000000000000..af9f08ca0cfd --- /dev/null +++ b/Documentation/driver-api/coco/index.rst @@ -0,0 +1,12 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +Confidential Computing +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +.. toctree:: + :maxdepth: 1 + + measurement-registers + +.. only:: subproject and html diff --git a/Documentation/driver-api/coco/measurement-registers.rst b/Docu= mentation/driver-api/coco/measurement-registers.rst new file mode 100644 index 000000000000..cef85945a9a7 --- /dev/null +++ b/Documentation/driver-api/coco/measurement-registers.rst @@ -0,0 +1,12 @@ +.. SPDX-License-Identifier: GPL-2.0 +.. include:: + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +Measurement Registers +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +.. kernel-doc:: include/linux/tsm-mr.h + :internal: + +.. kernel-doc:: drivers/virt/coco/tsm-mr.c + :export: diff --git a/Documentation/driver-api/index.rst b/Documentation/driver-api/= index.rst index 16e2c4ec3c01..3e2a270bd828 100644 --- a/Documentation/driver-api/index.rst +++ b/Documentation/driver-api/index.rst @@ -81,6 +81,7 @@ Subsystem-specific APIs acpi/index backlight/lp855x-driver.rst clk + coco/index console crypto/index dmaengine/index diff --git a/MAINTAINERS b/MAINTAINERS index fa1e04e87d1d..1d1426af3a68 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -24616,8 +24616,8 @@ M: Dan Williams L: linux-coco@lists.linux.dev 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 =20 TRUSTED SERVICES TEE DRIVER M: Balint Dobszay diff --git a/drivers/virt/coco/Kconfig b/drivers/virt/coco/Kconfig index ff869d883d95..737106d5dcbc 100644 --- a/drivers/virt/coco/Kconfig +++ b/drivers/virt/coco/Kconfig @@ -7,6 +7,11 @@ config TSM_REPORTS select CONFIGFS_FS tristate =20 +config TSM_MEASUREMENTS + select CRYPTO_HASH_INFO + select CRYPTO + bool + source "drivers/virt/coco/efi_secret/Kconfig" =20 source "drivers/virt/coco/pkvm-guest/Kconfig" diff --git a/drivers/virt/coco/Makefile b/drivers/virt/coco/Makefile index c3d07cfc087e..eb6ec5c1d2e1 100644 --- a/drivers/virt/coco/Makefile +++ b/drivers/virt/coco/Makefile @@ -3,6 +3,7 @@ # Confidential computing related collateral # obj-$(CONFIG_TSM_REPORTS) +=3D tsm.o +obj-$(CONFIG_TSM_MEASUREMENTS) +=3D tsm-mr.o obj-$(CONFIG_EFI_SECRET) +=3D efi_secret/ obj-$(CONFIG_ARM_PKVM_GUEST) +=3D pkvm-guest/ obj-$(CONFIG_SEV_GUEST) +=3D sev-guest/ diff --git a/drivers/virt/coco/tsm-mr.c b/drivers/virt/coco/tsm-mr.c new file mode 100644 index 000000000000..adc32328018e --- /dev/null +++ b/drivers/virt/coco/tsm-mr.c @@ -0,0 +1,247 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright(c) 2024-2025 Intel Corporation. All rights reserved. */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include + +#define CREATE_TRACE_POINTS +#include + +/* + * struct tm_context - contains everything necessary to implement sysfs + * attributes for MRs. + * @rwsem: protects the MR cache from concurrent access. + * @agrp: contains all MR attributes created by tsm_mr_create_attribute_gr= oup(). + * @tm: input to tsm_mr_create_attribute_group() containing MR definitions= /ops. + * @in_sync: %true if MR cache is up-to-date. + * @mrs: array of &struct bin_attribute, one for each MR. + * + * This internal structure contains everything needed to implement + * tm_digest_read() and tm_digest_write(). + * + * Given tm->refresh() is potentially expensive, tm_digest_read() caches MR + * values and calls tm->refresh() only when necessary. Only live MRs (i.e.= , with + * %TSM_MR_F_LIVE set) can trigger tm->refresh(), while others are assumed= to + * retain their values from the last tm->write(). @in_sync tracks if there= have + * been tm->write() calls since the last tm->refresh(). That is, tm->refre= sh() + * will be called only when a live MR is being read and the cache is stale + * (@in_sync is %false). + * + * tm_digest_write() sets @in_sync to %false and calls tm->write(), whose + * semantics is arch and MR specific. Most (if not all) writable MRs suppo= rt the + * extension semantics (i.e., tm->write() extends the input buffer into th= e MR). + */ +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 =3D attr->private; + rc =3D down_read_interruptible(&ctx->rwsem); + if (rc) + return rc; + + mr =3D &ctx->tm->mrs[attr - ctx->mrs]; + + /* + * @ctx->in_sync indicates if the MR cache is stale. It is a global + * instead of a per-MR flag for simplicity, as most (if not all) archs + * allow reading all MRs in oneshot. + * + * ctx->refresh() is necessary only for LIVE MRs, while others retain + * their values from their respective last ctx->write(). + */ + if ((mr->mr_flags & TSM_MR_F_LIVE) && !ctx->in_sync) { + up_read(&ctx->rwsem); + + rc =3D down_write_killable(&ctx->rwsem); + if (rc) + return rc; + + if (!ctx->in_sync) { + rc =3D ctx->tm->refresh(ctx->tm, mr); + ctx->in_sync =3D !rc; + trace_tsm_mr_refresh(mr, rc); + } + + downgrade_write(&ctx->rwsem); + } + + memcpy(buffer, mr->mr_value + off, count); + trace_tsm_mr_read(mr); + + 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 !=3D 0 || count !=3D attr->size) + return -EINVAL; + + ctx =3D attr->private; + mr =3D &ctx->tm->mrs[attr - ctx->mrs]; + trace_tsm_mr_write(mr, buffer); + + rc =3D down_write_killable(&ctx->rwsem); + if (rc) + return rc; + + rc =3D ctx->tm->write(ctx->tm, mr, buffer); + + /* mark MR cache stale */ + if (!rc) + ctx->in_sync =3D false; + + up_write(&ctx->rwsem); + return rc ?: count; +} + +/** + * tsm_mr_create_attribute_group() - creates an attribute group for measur= ement + * registers (MRs) + * @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 m= ust + * 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 * +tsm_mr_create_attribute_group(const struct tsm_measurements *tm) +{ + size_t nlen; + + if (!tm || !tm->mrs) + return ERR_PTR(-EINVAL); + + /* aggregated length of all MR names */ + nlen =3D 0; + for (size_t i =3D 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_name) + return ERR_PTR(-EINVAL); + + if (tm->mrs[i].mr_flags & TSM_MR_F_NOHASH) + continue; + + if (tm->mrs[i].mr_hash >=3D HASH_ALGO__LAST) + return ERR_PTR(-EINVAL); + + /* MR sysfs attribute names have the form of MRNAME:HASH */ + nlen +=3D 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) =3D + kzalloc(sizeof(*bas) * (tm->nr_mrs + 1) + nlen, GFP_KERNEL); + struct tm_context *ctx __free(kfree) =3D + 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 =3D (char *)&bas[tm->nr_mrs + 1]; + end =3D name + nlen; + + for (size_t i =3D 0; i < tm->nr_mrs; ++i) { + bas[i] =3D &ctx->mrs[i]; + sysfs_bin_attr_init(bas[i]); + + if (tm->mrs[i].mr_flags & TSM_MR_F_NOHASH) + bas[i]->attr.name =3D tm->mrs[i].mr_name; + else if (name < end) { + bas[i]->attr.name =3D name; + name +=3D 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 =3D 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) { + bas[i]->attr.mode |=3D 0444; + bas[i]->read_new =3D tm_digest_read; + } + + if (tm->mrs[i].mr_flags & TSM_MR_F_WRITABLE) { + bas[i]->attr.mode |=3D 0200; + bas[i]->write_new =3D tm_digest_write; + } + + bas[i]->size =3D tm->mrs[i].mr_size; + bas[i]->private =3D ctx; + } + + if (name !=3D end) + return ERR_PTR(-EINVAL); + + init_rwsem(&ctx->rwsem); + ctx->agrp.name =3D "mr"; + ctx->agrp.bin_attrs =3D no_free_ptr(bas); + ctx->tm =3D tm; + return &no_free_ptr(ctx)->agrp; +} +EXPORT_SYMBOL_GPL(tsm_mr_create_attribute_group); + +/** + * 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) +{ + if (!IS_ERR_OR_NULL(attr_grp)) { + kfree(attr_grp->bin_attrs); + kfree(container_of(attr_grp, struct tm_context, agrp)); + } +} +EXPORT_SYMBOL_GPL(tsm_mr_free_attribute_group); diff --git a/include/linux/tsm-mr.h b/include/linux/tsm-mr.h new file mode 100644 index 000000000000..3d4576849885 --- /dev/null +++ b/include/linux/tsm-mr.h @@ -0,0 +1,94 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __TSM_MR_H +#define __TSM_MR_H + +#include + +/** + * 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_i= nfo.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. + * + * @mr_name and @mr_value must stay valid until this structure is no longe= r 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. + * The semantics is typically to extend the MR but could vary depending = on the + * architecture and the MR. + * * %TSM_MR_F_LIVE - this MR's value may differ from the last value writt= en, so + * must be read back from the underlying CC hardware/firmware. + * * %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 =3D #mr, .mr_size =3D hash##_DIGEST_SIZE, \ + .mr_hash =3D HASH_ALGO_##hash, .mr_flags =3D TSM_MR_F_READABLE + +/** + * struct tsm_measurements - defines the CC architecture specific measurem= ent + * facility and methods for updating measurement registers (MRs) + * @mrs: Array of MR definitions. + * @nr_mrs: Number of elements in @mrs. + * @refresh: Callback function to load/sync all MRs from TVM hardware/firm= ware + * into the kernel cache. + * @write: Callback function to write to the MR specified by the parameter= @mr. + * Typically, writing to an MR extends the input buffer to that MR. + * + * @refresh takes two parameters: + * + * * @tm - points back to this structure. + * * @mr - points to the MR (an element of @mrs) being read (hence trigger= ed + * 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 on= ly + * the MR being read (@mr) but also all MRs with %TSM_MR_F_LIVE set. + * + * @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 struct tsm_measurement_register *mrs; + 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 * +tsm_mr_create_attribute_group(const struct tsm_measurements *tm); +void tsm_mr_free_attribute_group(const struct attribute_group *attr_grp); + +#endif diff --git a/include/trace/events/tsm_mr.h b/include/trace/events/tsm_mr.h new file mode 100644 index 000000000000..f40de4ad3e2d --- /dev/null +++ b/include/trace/events/tsm_mr.h @@ -0,0 +1,80 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM tsm_mr + +#if !defined(_TRACE_TSM_MR_H) || defined(TRACE_HEADER_MULTI_READ) +#define _TRACE_TSM_MR_H + +#include +#include + +TRACE_EVENT(tsm_mr_read, + + TP_PROTO(const struct tsm_measurement_register *mr), + + TP_ARGS(mr), + + TP_STRUCT__entry( + __string(mr, mr->mr_name) + __string(hash, mr->mr_flags & TSM_MR_F_NOHASH ? + "data" : hash_algo_name[mr->mr_hash]) + __dynamic_array(u8, d, mr->mr_size) + ), + + TP_fast_assign( + __assign_str(mr); + __assign_str(hash); + memcpy(__get_dynamic_array(d), mr->mr_value, __get_dynamic_array_len(d)); + ), + + TP_printk("[%s] %s:%s", __get_str(mr), __get_str(hash), + __print_hex_str(__get_dynamic_array(d), __get_dynamic_array_len(d))) +); + +TRACE_EVENT(tsm_mr_refresh, + + TP_PROTO(const struct tsm_measurement_register *mr, int rc), + + TP_ARGS(mr, rc), + + TP_STRUCT__entry( + __string(mr, mr->mr_name) + __field(int, rc) + ), + + TP_fast_assign( + __assign_str(mr); + __entry->rc =3D rc; + ), + + TP_printk("[%s] %s:%d", __get_str(mr), + __entry->rc ? "failed" : "succeeded", __entry->rc) +); + +TRACE_EVENT(tsm_mr_write, + + TP_PROTO(const struct tsm_measurement_register *mr, const u8 *data), + + TP_ARGS(mr, data), + + TP_STRUCT__entry( + __string(mr, mr->mr_name) + __string(hash, mr->mr_flags & TSM_MR_F_NOHASH ? + "data" : hash_algo_name[mr->mr_hash]) + __dynamic_array(u8, d, mr->mr_size) + ), + + TP_fast_assign( + __assign_str(mr); + __assign_str(hash); + memcpy(__get_dynamic_array(d), data, __get_dynamic_array_len(d)); + ), + + TP_printk("[%s] %s:%s", __get_str(mr), __get_str(hash), + __print_hex_str(__get_dynamic_array(d), __get_dynamic_array_len(d))) +); + +#endif + +/* This part must be outside protection */ +#include --=20 2.43.0 From nobody Thu Dec 18 12:35:41 2025 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.14]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 58BC11F542E for ; Thu, 24 Apr 2025 20:12:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.14 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745525580; cv=none; b=Jsd21hKtofkOdzJfNJ8f+OzrXS7YkRanqcQZ6uD7FNvgPO2OAhlYDJ0Hqt4QY9djQV0Os5b4DOaQ4bS/1XkL7dky5WLUiZgKCCKHjxl0RAjRT19qnJeIipz8oMmELpLbUkbRxYg8EQZgzURtGi1vHii0aLjYnaeRm65VBcLF4Us= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745525580; c=relaxed/simple; bh=xExtdz8+1dTYOoMu37+wPVOWjeqkoibsd4KQLAI0io0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=BdgrKUbahGLJQaFQClLszPOkol73jWRrVEvWtGmCjQQbR8OuWpBh7mpoWFLGhyrtaOsGgdZIe0Qr1A7zQzfSxUmi7i9DVGGV56Y08GE00anuJOblJFK6RNJiF5DuTf0+4aIquAwHeRXq43Ui4ba8xfSmol7KKidTaeAGR27k/CM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=LPxf3zMW; arc=none smtp.client-ip=198.175.65.14 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="LPxf3zMW" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1745525576; x=1777061576; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=xExtdz8+1dTYOoMu37+wPVOWjeqkoibsd4KQLAI0io0=; b=LPxf3zMWmKRG6DzpoF5pWBOgDvDn9/OPl2D0Tp16Y9aNWuZAoEXrKXcy 30oMbtead+uMCKn9hpskqqpZ4fL2pTjlFHY6CQEY/kH1wk8cN/HrkPpMr EIDVxbRLjZQOxHFFxuVwDT1BuPO1h+cvfURpNKnJm5dRSjx/y54OoDJ7Y NPHoYlPvOFLYYJvMYwPp8GUAE85WAHFbJATSZaISd9K+rSwTYcdndJ6o+ cWgXImS4M47qZZBJkw2ZFNZLJKCnggblpYyv/4JeOByuzU5OA6DNe2MHl Cx2NR3aJnDCm0lj+h1JCW+4wiZbgCPWum37Pfz5nZPkoQ1WKqsHeZ5L/E A==; X-CSE-ConnectionGUID: CFlyHBhUQYKfNxwJ/0Z9yg== X-CSE-MsgGUID: 4vGiNuLQTHm+I2e0vLuvdw== X-IronPort-AV: E=McAfee;i="6700,10204,11413"; a="50986707" X-IronPort-AV: E=Sophos;i="6.15,237,1739865600"; d="scan'208";a="50986707" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Apr 2025 13:12:56 -0700 X-CSE-ConnectionGUID: KSVPIDBTTi+ZjV0/UwEJiw== X-CSE-MsgGUID: s0qMXMrNRsWXQc4ydFz1Zg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.15,237,1739865600"; d="scan'208";a="155945368" Received: from ajatsiv-mobl.amr.corp.intel.com (HELO bxing-mobl1.clients.intel.com) ([10.246.112.111]) by fmviesa002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Apr 2025 13:12:57 -0700 From: Cedric Xing Date: Thu, 24 Apr 2025 15:12:26 -0500 Subject: [PATCH v5 2/5] tsm-mr: Add tsm-mr sample code Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250424-tdx-rtmr-v5-2-4fe28ddf85d4@intel.com> References: <20250424-tdx-rtmr-v5-0-4fe28ddf85d4@intel.com> In-Reply-To: <20250424-tdx-rtmr-v5-0-4fe28ddf85d4@intel.com> To: Dan Williams , "Kirill A. Shutemov" , Dave Hansen , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Cc: linux-kernel@vger.kernel.org, linux-coco@lists.linux.dev, Dionna Amalie Glaze , Guorui Yu , James Bottomley , Dan Middleton , Mikko Ylinen , Sathyanarayanan Kuppuswamy , Cedric Xing X-Mailer: b4 0.13.0 This 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 =E2=94=94=E2=94=80=E2=94=80 mr =E2=94=9C=E2=94=80=E2=94=80 config_mr =E2=94=9C=E2=94=80=E2=94=80 report_digest:sha512 =E2=94=9C=E2=94=80=E2=94=80 rtmr0:sha256 =E2=94=9C=E2=94=80=E2=94=80 rtmr1:sha384 =E2=94=9C=E2=94=80=E2=94=80 rtmr_crypto_agile:sha256 =E2=94=9C=E2=94=80=E2=94=80 rtmr_crypto_agile:sha384 =E2=94=94=E2=94=80=E2=94=80 static_mr:sha384 Among 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. Signed-off-by: Cedric Xing Reviewed-by: Dan Williams --- MAINTAINERS | 1 + samples/Kconfig | 11 ++++ samples/Makefile | 1 + samples/tsm-mr/Makefile | 2 + samples/tsm-mr/tsm_mr_sample.c | 137 +++++++++++++++++++++++++++++++++++++= ++++ 5 files changed, 152 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 1d1426af3a68..fd5bbf78ec8b 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -24618,6 +24618,7 @@ S: Maintained F: Documentation/ABI/testing/configfs-tsm F: drivers/virt/coco/tsm*.c F: include/linux/tsm*.h +F: samples/tsm/* =20 TRUSTED SERVICES TEE DRIVER M: Balint Dobszay diff --git a/samples/Kconfig b/samples/Kconfig index 09011be2391a..6ade17cb16b4 100644 --- a/samples/Kconfig +++ b/samples/Kconfig @@ -184,6 +184,17 @@ config SAMPLE_TIMER bool "Timer sample" depends on CC_CAN_LINK && HEADERS_INSTALL =20 +config SAMPLE_TSM_MR + tristate "TSM measurement sample" + select TSM_MEASUREMENTS + select VIRT_DRIVERS + 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 diff --git a/samples/Makefile b/samples/Makefile index bf6e6fca5410..c95bac31851c 100644 --- a/samples/Makefile +++ b/samples/Makefile @@ -43,3 +43,4 @@ obj-$(CONFIG_SAMPLES_RUST) +=3D rust/ obj-$(CONFIG_SAMPLE_DAMON_WSSE) +=3D damon/ obj-$(CONFIG_SAMPLE_DAMON_PRCL) +=3D damon/ obj-$(CONFIG_SAMPLE_HUNG_TASK) +=3D hung_task/ +obj-$(CONFIG_SAMPLE_TSM_MR) +=3D tsm-mr/ diff --git a/samples/tsm-mr/Makefile b/samples/tsm-mr/Makefile new file mode 100644 index 000000000000..587c3947b3a7 --- /dev/null +++ b/samples/tsm-mr/Makefile @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-only +obj-$(CONFIG_SAMPLE_TSM_MR) +=3D tsm_mr_sample.o diff --git a/samples/tsm-mr/tsm_mr_sample.c b/samples/tsm-mr/tsm_mr_sample.c new file mode 100644 index 000000000000..3ee35d87d275 --- /dev/null +++ b/samples/tsm-mr/tsm_mr_sample.c @@ -0,0 +1,137 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright(c) 2024 Intel Corporation. All rights reserved. */ + +#define DEBUG +#define pr_fmt(x) KBUILD_MODNAME ": " x + +#include +#include +#include +#include + +struct { + u8 static_mr[SHA384_DIGEST_SIZE]; + u8 config_mr[SHA512_DIGEST_SIZE]; + u8 rtmr0[SHA256_DIGEST_SIZE]; + u8 rtmr1[SHA384_DIGEST_SIZE]; + u8 report_digest[SHA512_DIGEST_SIZE]; +} sample_report =3D { + .static_mr =3D "static_mr", + .config_mr =3D "config_mr", + .rtmr0 =3D "rtmr0", + .rtmr1 =3D "rtmr1", +}; + +static int sample_report_refresh(const struct tsm_measurements *tm, + const struct tsm_measurement_register *mr) +{ + struct crypto_shash *tfm; + int rc; + + pr_debug("%s(%s) is called\n", __func__, mr ? mr->mr_name : ""); + + tfm =3D crypto_alloc_shash(hash_algo_name[HASH_ALGO_SHA512], 0, 0); + if (IS_ERR(tfm)) { + pr_err("crypto_alloc_shash failed: %ld\n", PTR_ERR(tfm)); + return PTR_ERR(tfm); + } + + rc =3D crypto_shash_tfm_digest(tfm, (u8 *)&sample_report, + offsetof(typeof(sample_report), + report_digest), + sample_report.report_digest); + crypto_free_shash(tfm); + if (rc) + pr_err("crypto_shash_tfm_digest failed: %d\n", rc); + return rc; +} + +static int sample_report_extend_mr(const struct tsm_measurements *tm, + const struct tsm_measurement_register *mr, + const u8 *data) +{ + SHASH_DESC_ON_STACK(desc, 0); + int rc; + + pr_debug("%s(%s) is called\n", __func__, mr->mr_name); + + desc->tfm =3D crypto_alloc_shash(hash_algo_name[mr->mr_hash], 0, 0); + if (IS_ERR(desc->tfm)) { + pr_err("crypto_alloc_shash failed: %ld\n", PTR_ERR(desc->tfm)); + return PTR_ERR(desc->tfm); + } + + rc =3D crypto_shash_init(desc); + if (!rc) + rc =3D crypto_shash_update(desc, mr->mr_value, mr->mr_size); + if (!rc) + rc =3D crypto_shash_finup(desc, data, mr->mr_size, mr->mr_value); + crypto_free_shash(desc->tfm); + if (rc) + pr_err("SHA calculation failed: %d\n", rc); + return rc; +} + +#define MR_(mr, hash) .mr_value =3D &sample_report.mr, TSM_MR_(mr, hash) +static const struct tsm_measurement_register sample_mrs[] =3D { + /* static MR, read-only */ + { MR_(static_mr, SHA384) }, + /* config MR, read-only */ + { MR_(config_mr, SHA512) | TSM_MR_F_NOHASH }, + /* RTMR, direct extension prohibited */ + { MR_(rtmr0, SHA256) | TSM_MR_F_LIVE }, + /* RTMR, direct extension allowed */ + { MR_(rtmr1, SHA384) | TSM_MR_F_RTMR }, + /* RTMR, crypto agile, alaised to rtmr0 and rtmr1, respectively */ + { .mr_value =3D &sample_report.rtmr0, + TSM_MR_(rtmr_crypto_agile, SHA256) | TSM_MR_F_RTMR }, + { .mr_value =3D &sample_report.rtmr1, + TSM_MR_(rtmr_crypto_agile, SHA384) | TSM_MR_F_RTMR }, + /* sha512 digest of the whole structure */ + { MR_(report_digest, SHA512) | TSM_MR_F_LIVE }, +}; +#undef MR_ + +static struct tsm_measurements sample_tm =3D { + .mrs =3D sample_mrs, + .nr_mrs =3D ARRAY_SIZE(sample_mrs), + .refresh =3D sample_report_refresh, + .write =3D sample_report_extend_mr, +}; + +static const struct attribute_group *sample_groups[] =3D { + NULL, + NULL, +}; + +static struct miscdevice sample_misc_dev =3D { + .name =3D KBUILD_MODNAME, + .minor =3D MISC_DYNAMIC_MINOR, + .groups =3D sample_groups, +}; + +static int __init tsm_mr_sample_init(void) +{ + int rc; + + sample_groups[0] =3D tsm_mr_create_attribute_group(&sample_tm); + if (IS_ERR(sample_groups[0])) + return PTR_ERR(sample_groups[0]); + + rc =3D misc_register(&sample_misc_dev); + if (rc) + tsm_mr_free_attribute_group(sample_groups[0]); + return rc; +} + +static void __exit tsm_mr_sample_exit(void) +{ + misc_deregister(&sample_misc_dev); + tsm_mr_free_attribute_group(sample_groups[0]); +} + +module_init(tsm_mr_sample_init); +module_exit(tsm_mr_sample_exit); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("Sample module using tsm-mr to expose emulated MRs"); --=20 2.43.0 From nobody Thu Dec 18 12:35:41 2025 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.14]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 34D4D1FCCF8 for ; Thu, 24 Apr 2025 20:13:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.14 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745525582; cv=none; b=ZtAKjNeUnYHHoJ0NI7OkY3gJoqlY6SiWM9IXZN0PFUqXHsKLI/IaqiQ8IZPc7OUx26Q8arHi6apHx4flLkCTAB5SzYwUGcJEtYwE90KQHMWVvMaQhZZjCxLFAG/W+gfWDfJkdP0ERF6a6iotTIjHI2pvfV649hlftVBPVDUQi00= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745525582; c=relaxed/simple; bh=J9lNEUBAS/HDgmd4lFHMr7W9GjeRlJkQxjD4rCxzUI0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=eaEreyXankIBnIgzDZomsaK15JOhPl6yVglXZlBin2feugL7HUEsYgd9/l4xmnplJz5EFaz/r5OuWOU+wqc1R1oXQuVZZkiDY+q7+b9QYByhqM3SgIxhRgpR/0oqkZwW4hj4u/iqx7kW6wP3o5r1rHkwM3SQtNexg6HnX1Qkob0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=SWQCSjQW; arc=none smtp.client-ip=198.175.65.14 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="SWQCSjQW" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1745525578; x=1777061578; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=J9lNEUBAS/HDgmd4lFHMr7W9GjeRlJkQxjD4rCxzUI0=; b=SWQCSjQW/+eSCO31LIwIkYHEG62PUEGSt31bNqZSx5FSW88GB/4Eok6b 9AQ+hfOU113a/4uT2SkPQ5v6VggUPkX/tvNYvDFVLcdqdxwuDf65wRAjz OEkl7JORWmCI0C5kGvuGUbRMCkc9WFJROyjqY4yrxPKtTRyyOifn3+NxU hAbbbkg0uqUi6SRn/2DBYy7uI8CrFx8JjInb9kpOFD0bQprAVRiHkq0up jwDDLhb6nIaywPD92xOtE/dKoDrOBWLvMkmBJcePGwSO8nclpfK47+Qg4 E++vdmnK+Gvcg3GxWNE53VwjXDzVklrMpCTH2GMw5LR7o/EwStKV3Ci8d g==; X-CSE-ConnectionGUID: UqZn4+kuRruaGzpQVrZXFg== X-CSE-MsgGUID: W8A3NRc7RoGw+/zyjGacVg== X-IronPort-AV: E=McAfee;i="6700,10204,11413"; a="50986719" X-IronPort-AV: E=Sophos;i="6.15,237,1739865600"; d="scan'208";a="50986719" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Apr 2025 13:12:57 -0700 X-CSE-ConnectionGUID: GpNOdKlyRmGc4QPrwypt7g== X-CSE-MsgGUID: ygPB50pGR+uTEupqElqdnQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.15,237,1739865600"; d="scan'208";a="155945375" Received: from ajatsiv-mobl.amr.corp.intel.com (HELO bxing-mobl1.clients.intel.com) ([10.246.112.111]) by fmviesa002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Apr 2025 13:12:58 -0700 From: Cedric Xing Date: Thu, 24 Apr 2025 15:12:27 -0500 Subject: [PATCH v5 3/5] x86/tdx: Add tdx_mcall_extend_rtmr() interface Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250424-tdx-rtmr-v5-3-4fe28ddf85d4@intel.com> References: <20250424-tdx-rtmr-v5-0-4fe28ddf85d4@intel.com> In-Reply-To: <20250424-tdx-rtmr-v5-0-4fe28ddf85d4@intel.com> To: Dan Williams , "Kirill A. Shutemov" , Dave Hansen , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Cc: linux-kernel@vger.kernel.org, linux-coco@lists.linux.dev, Dionna Amalie Glaze , Guorui Yu , James Bottomley , Dan Middleton , Mikko Ylinen , Sathyanarayanan Kuppuswamy , Cedric Xing X-Mailer: b4 0.13.0 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.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. Signed-off-by: Kuppuswamy Sathyanarayanan Signed-off-by: Cedric Xing Acked-by: Dave Hansen --- arch/x86/coco/tdx/tdx.c | 37 +++++++++++++++++++++++++++++++++++= ++ arch/x86/include/asm/shared/tdx.h | 1 + arch/x86/include/asm/tdx.h | 2 ++ 3 files changed, 40 insertions(+) diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index edab6d6049be..0b6804d2a5e1 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -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 =20 #define TDREPORT_SUBTYPE_0 0 =20 @@ -136,6 +137,42 @@ int tdx_mcall_get_report0(u8 *reportdata, u8 *tdreport) } EXPORT_SYMBOL_GPL(tdx_mcall_get_report0); =20 +/** + * 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 to extend the R= TMR + * registers. + * + * Return 0 on success, -ENXIO for invalid operands, -EBUSY for busy opera= tion, + * or -EIO on other TDCALL failures. + */ +int tdx_mcall_extend_rtmr(u8 index, u8 *data) +{ + struct tdx_module_args args =3D { + .rcx =3D virt_to_phys(data), + .rdx =3D index, + }; + u64 ret; + + ret =3D __tdcall(TDG_MR_RTMR_EXTEND, &args); + if (ret) { + if (TDCALL_RETURN_CODE(ret) =3D=3D TDCALL_INVALID_OPERAND) + return -ENXIO; + if (TDCALL_RETURN_CODE(ret) =3D=3D 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. diff --git a/arch/x86/include/asm/shared/tdx.h b/arch/x86/include/asm/share= d/tdx.h index a28ff6b14145..738f583f65cb 100644 --- a/arch/x86/include/asm/shared/tdx.h +++ b/arch/x86/include/asm/shared/tdx.h @@ -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 diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h index 4a1922ec80cf..12d17f3ca301 100644 --- a/arch/x86/include/asm/tdx.h +++ b/arch/x86/include/asm/tdx.h @@ -64,6 +64,8 @@ bool tdx_early_handle_ve(struct pt_regs *regs); =20 int tdx_mcall_get_report0(u8 *reportdata, u8 *tdreport); =20 +int tdx_mcall_extend_rtmr(u8 index, u8 *data); + u64 tdx_hcall_get_quote(u8 *buf, size_t size); =20 void __init tdx_dump_attributes(u64 td_attr); --=20 2.43.0 From nobody Thu Dec 18 12:35:41 2025 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.14]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F309D200B9B for ; Thu, 24 Apr 2025 20:13:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.14 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745525584; cv=none; b=FKKgq1zzrtho+cxCiXKU+gmOfL5WIMHIWa0gxKsSQRSWHCh3rAfRy5LHnEwgDiD50s5w1+y4ZKS4aW49bpqBKb9CCLVMuHvsYghCDygdD8aokb6pYElI2iOhBFNBCqaPsAB17FCt6Z42JEihGHEuGe792Z8oBw8RdRqNG61xAHM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745525584; c=relaxed/simple; bh=wcWUb4LEHMi+z65GmONMCHgkJDpHkk2hFLXwtJ4q1R4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=QOiKzluY8SkX7V44SKvs6c84+RpMaORPubD+zRFfHB+qCBdHFdX21nHtURuN1D+Wt/6frKlCI4aVy2JX73hO2pymjChDGjVSixjp4YC9GYZEgprw72ggjHtY/1g2/FDLVLG148JBxJ9m0FIpNc85L14UJrQeTamMIrppUCIuuZo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=QYLP1iYs; arc=none smtp.client-ip=198.175.65.14 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="QYLP1iYs" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1745525579; x=1777061579; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=wcWUb4LEHMi+z65GmONMCHgkJDpHkk2hFLXwtJ4q1R4=; b=QYLP1iYsrdwZ9QXqWw7Yasc/12/EBPmg10wVcmioa/JpMxMCKMjkB1Aj 40RgOHzVHj23nsKgGVkFOWyA4CRKgoKEsxCeEwyNTSu7Pm2gtUQS6me55 5jzOAFjtQhywRQCfzJ+rbpPvjxp/jweYtT1chZ77ddAE0okTVHIU3XLY1 jUbbGAm14qU+ldJI/5OCOq0zie/ZFkvlBtxV/nDp4wei6rwq/gb6JXRDk 6UqepK7aEyKW4T1f/jYLBzqv+ixsGT4CD+XhkWtSR1wryQ2BimvIU/XEr c8cUM9gg1R8M5gQ3FUX6NNVIFG20o8P4LKbDo3UFAFYzaAq69d3dDc7si g==; X-CSE-ConnectionGUID: YFupVUVaSfSvLBi3oPoPAg== X-CSE-MsgGUID: 2mkic6UYTbeXsElbWxF5dg== X-IronPort-AV: E=McAfee;i="6700,10204,11413"; a="50986732" X-IronPort-AV: E=Sophos;i="6.15,237,1739865600"; d="scan'208";a="50986732" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Apr 2025 13:12:59 -0700 X-CSE-ConnectionGUID: 8qUKluR1QP6JGga2XFcwAg== X-CSE-MsgGUID: ow7RcP5/S4uUZr9mVLdH7Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.15,237,1739865600"; d="scan'208";a="155945380" Received: from ajatsiv-mobl.amr.corp.intel.com (HELO bxing-mobl1.clients.intel.com) ([10.246.112.111]) by fmviesa002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Apr 2025 13:12:59 -0700 From: Cedric Xing Date: Thu, 24 Apr 2025 15:12:28 -0500 Subject: [PATCH v5 4/5] x86/tdx: tdx_mcall_get_report0: Return -EBUSY on TDCALL_OPERAND_BUSY error Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250424-tdx-rtmr-v5-4-4fe28ddf85d4@intel.com> References: <20250424-tdx-rtmr-v5-0-4fe28ddf85d4@intel.com> In-Reply-To: <20250424-tdx-rtmr-v5-0-4fe28ddf85d4@intel.com> To: Dan Williams , "Kirill A. Shutemov" , Dave Hansen , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Cc: linux-kernel@vger.kernel.org, linux-coco@lists.linux.dev, Dionna Amalie Glaze , Guorui Yu , James Bottomley , Dan Middleton , Mikko Ylinen , Sathyanarayanan Kuppuswamy , Cedric Xing X-Mailer: b4 0.13.0 Return `-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. Signed-off-by: Cedric Xing Acked-by: Dave Hansen --- arch/x86/coco/tdx/tdx.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index 0b6804d2a5e1..7b2833705d47 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -110,12 +110,13 @@ static inline u64 tdg_vm_wr(u64 field, u64 value, u64= mask) * REPORTDATA to be included into TDREPORT. * @tdreport: Address of the output buffer to store TDREPORT. * - * Refer to section titled "TDG.MR.REPORT leaf" in the TDX Module - * v1.0 specification for more information on TDG.MR.REPORT TDCALL. + * Refer to section titled "TDG.MR.REPORT leaf" in the TDX Module 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, -ENXIO for invalid operands, -EBUSY for busy opera= tion, + * or -EIO on other TDCALL failures. */ int tdx_mcall_get_report0(u8 *reportdata, u8 *tdreport) { @@ -129,7 +130,9 @@ int tdx_mcall_get_report0(u8 *reportdata, u8 *tdreport) ret =3D __tdcall(TDG_MR_REPORT, &args); if (ret) { if (TDCALL_RETURN_CODE(ret) =3D=3D TDCALL_INVALID_OPERAND) - return -EINVAL; + return -ENXIO; + else if (TDCALL_RETURN_CODE(ret) =3D=3D TDCALL_OPERAND_BUSY) + return -EBUSY; return -EIO; } =20 --=20 2.43.0 From nobody Thu Dec 18 12:35:41 2025 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.14]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A4D9E1F4CBE for ; Thu, 24 Apr 2025 20:13:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.14 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745525584; cv=none; b=tlvBswECBoutWND4wI3CW3ibxLZ1yYHuWJp+LGKjZ4qTsw5U8qed0rwUJZcv8/Zr9ykse4fIjHbfiw6cPgvbeBuJlf6WQ7wG+6s3jG/LNIIIyksgVBMdiU4y3UAp3hUeWfCsMoBMbBOrXcTv4gRBJy0ZMT1Z9HMLw0ZH45SLAJY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745525584; c=relaxed/simple; bh=CzslO/qJdYHI3PGng48OqW/c8FJSQM2QKEdpsLBTVRI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ILxE+7JU/5vQIGQcHpNw9v0rCkL9LYDVtanuAkwtCtxph00Eek/CqB+QzEQdCW1jqB/p2BCQYVYH9EiVtz4J5dZkqnhJwZhwkHExEw0Zk0FrqLa1SKmsQ/Au2EEf/wDM7cS4wPDpeZDKmMSNQMWq7dgF9UhP4zcB+ffwMPFc33Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=ihq0Af3/; arc=none smtp.client-ip=198.175.65.14 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="ihq0Af3/" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1745525580; x=1777061580; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=CzslO/qJdYHI3PGng48OqW/c8FJSQM2QKEdpsLBTVRI=; b=ihq0Af3/48Enobf7CtvrvKSjhLeC6LmTWsU7yBAbxqXlrOVUTQYwQIOj OVLvQPG88iIShFbUPCrDDUHBO3Xfq10Jt0OiVf37YL4kxyuD1GN9wD4Nt pHzeE7fY0t2pbcm4YWDMrB5LXL75HqdLLLpCPxOsThEhmSyTj9tLWRyzZ x1nivGovKQjk48MO38T5WT+WX+irKnzi1c4kdCV6Fq3ji2E+j520x1s4b UMPLUHrRb4NTGZyvH9eAniy18jvyH4YMEHWGHO3v9/+xYp/hSL1NDp/F9 HwuB7GJ6M1iXrA4KonCNOkAXFq1244M2g9TzaoDzRhvpYwwcy6VVUyckw Q==; X-CSE-ConnectionGUID: o8XEDZGJQ+O/6QgV+WU3tA== X-CSE-MsgGUID: v1yMeDMPTeClI3dS4+5cAA== X-IronPort-AV: E=McAfee;i="6700,10204,11413"; a="50986745" X-IronPort-AV: E=Sophos;i="6.15,237,1739865600"; d="scan'208";a="50986745" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Apr 2025 13:13:00 -0700 X-CSE-ConnectionGUID: ue/hGjiBRyKOK3hLdD1rgw== X-CSE-MsgGUID: hV+x9LtMTMGyHo2V952vNw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.15,237,1739865600"; d="scan'208";a="155945387" Received: from ajatsiv-mobl.amr.corp.intel.com (HELO bxing-mobl1.clients.intel.com) ([10.246.112.111]) by fmviesa002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Apr 2025 13:13:01 -0700 From: Cedric Xing Date: Thu, 24 Apr 2025 15:12:29 -0500 Subject: [PATCH v5 5/5] virt: tdx-guest: Expose TDX MRs as sysfs attributes Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250424-tdx-rtmr-v5-5-4fe28ddf85d4@intel.com> References: <20250424-tdx-rtmr-v5-0-4fe28ddf85d4@intel.com> In-Reply-To: <20250424-tdx-rtmr-v5-0-4fe28ddf85d4@intel.com> To: Dan Williams , "Kirill A. Shutemov" , Dave Hansen , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Cc: linux-kernel@vger.kernel.org, linux-coco@lists.linux.dev, Dionna Amalie Glaze , Guorui Yu , James Bottomley , Dan Middleton , Mikko Ylinen , Sathyanarayanan Kuppuswamy , Cedric Xing X-Mailer: b4 0.13.0 Expose 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 =E2=94=94=E2=94=80=E2=94=80 mr =E2=94=9C=E2=94=80=E2=94=80 mrconfigid =E2=94=9C=E2=94=80=E2=94=80 mrowner =E2=94=9C=E2=94=80=E2=94=80 mrownerconfig =E2=94=9C=E2=94=80=E2=94=80 mrtd:sha384 =E2=94=9C=E2=94=80=E2=94=80 rtmr0:sha384 =E2=94=9C=E2=94=80=E2=94=80 rtmr1:sha384 =E2=94=9C=E2=94=80=E2=94=80 rtmr2:sha384 =E2=94=94=E2=94=80=E2=94=80 rtmr3:sha384 Read 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 for more information. Signed-off-by: Cedric Xing --- .../testing/sysfs-devices-virtual-misc-tdx_guest | 60 +++++ MAINTAINERS | 1 + drivers/virt/coco/tdx-guest/Kconfig | 1 + drivers/virt/coco/tdx-guest/tdx-guest.c | 291 +++++++++++++----= ---- 4 files changed, 250 insertions(+), 103 deletions(-) diff --git a/Documentation/ABI/testing/sysfs-devices-virtual-misc-tdx_guest= b/Documentation/ABI/testing/sysfs-devices-virtual-misc-tdx_guest new file mode 100644 index 000000000000..fb7b27ae0b74 --- /dev/null +++ b/Documentation/ABI/testing/sysfs-devices-virtual-misc-tdx_guest @@ -0,0 +1,60 @@ +What: /sys/devices/virtual/misc/tdx_guest/mr/MRNAME[:HASH] +Date: April, 2025 +KernelVersion: v6.16 +Contact: linux-coco@lists.linux.dev +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. Refer to Intel TDX Module + ABI Specification for the definition of TDREPORT and the full + list of TDX measurements. + + Intel TDX Module ABI Specification can be found at: + https://www.intel.com/content/www/us/en/developer/tools/trust-domain-ext= ensions/documentation.html#architecture + +What: /sys/devices/virtual/misc/tdx_guest/mr/mrconfigid +Date: April, 2025 +KernelVersion: v6.16 +Contact: linux-coco@lists.linux.dev +Description: + (RO) MRCONFIGID - 48-byte immutable storage typically used for + software-defined ID for non-owner-defined configuration of the + guest TD =E2=80=93 e.g., run-time or OS configuration. + +What: /sys/devices/virtual/misc/tdx_guest/mr/mrowner +Date: April, 2025 +KernelVersion: v6.16 +Contact: linux-coco@lists.linux.dev +Description: + (RO) MROWNER - 48-byte immutable storage typically used for + software-defined ID for the guest TD=E2=80=99s owner. + +What: /sys/devices/virtual/misc/tdx_guest/mr/mrownerconfig +Date: April, 2025 +KernelVersion: v6.16 +Contact: linux-coco@lists.linux.dev +Description: + (RO) MROWNERCONFIG - 48-byte immutable storage typically used + for software-defined ID for owner-defined configuration of the + guest TD =E2=80=93 e.g., specific to the workload rather than the + run-time or OS. + +What: /sys/devices/virtual/misc/tdx_guest/mr/mrtd:sha384 +Date: April, 2025 +KernelVersion: v6.16 +Contact: linux-coco@lists.linux.dev +Description: + (RO) MRTD - Measurement of the initial contents of the TD. + +What: /sys/devices/virtual/misc/tdx_guest/mr/rtmr[0123]:sha384 +Date: April, 2025 +KernelVersion: v6.16 +Contact: linux-coco@lists.linux.dev +Description: + (RW) RTMR[0123] - 4 Run-Time extendable Measurement Registers. + Read from any of these returns the current value of the + corresponding RTMR. Write extends the written buffer to the + RTMR. All writes must start at offset 0 and be 48 bytes in + size. Partial writes will result in EINVAL returned by the + write() syscall. diff --git a/MAINTAINERS b/MAINTAINERS index fd5bbf78ec8b..3afbf6c310a7 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -26284,6 +26284,7 @@ L: x86@kernel.org L: linux-coco@lists.linux.dev 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 F: arch/x86/boot/compressed/tdx* F: arch/x86/coco/tdx/ F: arch/x86/include/asm/shared/tdx.h diff --git a/drivers/virt/coco/tdx-guest/Kconfig b/drivers/virt/coco/tdx-gu= est/Kconfig index 22dd59e19431..dbbdc14383b1 100644 --- a/drivers/virt/coco/tdx-guest/Kconfig +++ b/drivers/virt/coco/tdx-guest/Kconfig @@ -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 diff --git a/drivers/virt/coco/tdx-guest/tdx-guest.c b/drivers/virt/coco/td= x-guest/tdx-guest.c index 224e7dde9cde..e76810d89e0b 100644 --- a/drivers/virt/coco/tdx-guest/tdx-guest.c +++ b/drivers/virt/coco/tdx-guest/tdx-guest.c @@ -5,6 +5,8 @@ * Copyright (C) 2022 Intel Corporation */ =20 +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + #include #include #include @@ -15,8 +17,9 @@ #include #include #include +#include #include -#include +#include =20 #include =20 @@ -66,39 +69,144 @@ static DEFINE_MUTEX(quote_lock); */ static u32 getquote_timeout =3D 30; =20 -static long tdx_get_report0(struct tdx_report_req __user *req) +/* Buffers for TDREPORT and RTMR extension */ +static u8 *tdx_report_buf, *tdx_extend_buf; + +/* Lock to serialize TDG.MR.REPORT & TDG.MR.RTMR.EXTEND requests */ +static DEFINE_MUTEX(mr_lock); + +/* TDREPORT fields */ +enum { + TDREPORT_reportdata =3D 128, + TDREPORT_tee_tcb_info =3D 256, + TDREPORT_tdinfo =3D TDREPORT_tee_tcb_info + 256, + TDREPORT_attributes =3D TDREPORT_tdinfo, + TDREPORT_xfam =3D TDREPORT_attributes + sizeof(u64), + TDREPORT_mrtd =3D TDREPORT_xfam + sizeof(u64), + TDREPORT_mrconfigid =3D TDREPORT_mrtd + SHA384_DIGEST_SIZE, + TDREPORT_mrowner =3D TDREPORT_mrconfigid + SHA384_DIGEST_SIZE, + TDREPORT_mrownerconfig =3D TDREPORT_mrowner + SHA384_DIGEST_SIZE, + TDREPORT_rtmr0 =3D TDREPORT_mrownerconfig + SHA384_DIGEST_SIZE, + TDREPORT_rtmr1 =3D TDREPORT_rtmr0 + SHA384_DIGEST_SIZE, + TDREPORT_rtmr2 =3D TDREPORT_rtmr1 + SHA384_DIGEST_SIZE, + TDREPORT_rtmr3 =3D TDREPORT_rtmr2 + SHA384_DIGEST_SIZE, + TDREPORT_servtd_hash =3D TDREPORT_rtmr3 + SHA384_DIGEST_SIZE, +}; + +static inline int tdx_mr_report_locked(sockptr_t data, sockptr_t tdreport) { - u8 *reportdata, *tdreport; - long ret; + scoped_cond_guard(mutex_intr, return -EINTR, &mr_lock) { + u8 *p =3D tdx_report_buf + TDREPORT_reportdata; + int ret; =20 - reportdata =3D kmalloc(TDX_REPORTDATA_LEN, GFP_KERNEL); - if (!reportdata) - return -ENOMEM; + if (!sockptr_is_null(data) && + copy_from_sockptr(p, data, TDX_REPORTDATA_LEN)) + return -EFAULT; =20 - tdreport =3D kzalloc(TDX_REPORT_LEN, GFP_KERNEL); - if (!tdreport) { - ret =3D -ENOMEM; - goto out; + ret =3D tdx_mcall_get_report0(p, tdx_report_buf); + if (WARN(ret, "tdx_mcall_get_report0() failed: %d", ret)) + return ret; + + if (!sockptr_is_null(tdreport) && + copy_to_sockptr(tdreport, tdx_report_buf, TDX_REPORT_LEN)) + return -EFAULT; } + return 0; +} =20 - if (copy_from_user(reportdata, req->reportdata, TDX_REPORTDATA_LEN)) { - ret =3D -EFAULT; - goto out; +static inline int tdx_mr_extend_locked(ptrdiff_t mr_ind, const u8 *data) +{ + scoped_cond_guard(mutex_intr, return -EINTR, &mr_lock) { + int ret; + + memcpy(tdx_extend_buf, data, SHA384_DIGEST_SIZE); + + ret =3D tdx_mcall_extend_rtmr(mr_ind, tdx_extend_buf); + if (WARN(ret, "tdx_mcall_extend_rtmr(%ld) failed: %d", mr_ind, ret)) + return ret; } + return 0; +} + +#define TDX_MR_(r) .mr_value =3D (void *)TDREPORT_##r, TSM_MR_(r, SHA384) +static struct tsm_measurement_register tdx_mrs[] =3D { + { TDX_MR_(rtmr0) | TSM_MR_F_RTMR }, + { TDX_MR_(rtmr1) | TSM_MR_F_RTMR }, + { TDX_MR_(rtmr2) | TSM_MR_F_RTMR }, + { TDX_MR_(rtmr3) | TSM_MR_F_RTMR }, + { TDX_MR_(mrtd) }, + { TDX_MR_(mrconfigid) | TSM_MR_F_NOHASH }, + { TDX_MR_(mrowner) | TSM_MR_F_NOHASH }, + { TDX_MR_(mrownerconfig) | TSM_MR_F_NOHASH }, +}; +#undef TDX_MR_ + +static int tdx_mr_refresh(const struct tsm_measurements *tm, + const struct tsm_measurement_register *mr) +{ + return tdx_mr_report_locked(KERNEL_SOCKPTR(NULL), KERNEL_SOCKPTR(NULL)); +} =20 - /* Generate TDREPORT0 using "TDG.MR.REPORT" TDCALL */ - ret =3D tdx_mcall_get_report0(reportdata, tdreport); +static int tdx_mr_extend(const struct tsm_measurements *tm, + const struct tsm_measurement_register *mr, + const u8 *data) +{ + return tdx_mr_extend_locked(mr - tm->mrs, data); +} + +static struct tsm_measurements tdx_measurements =3D { + .mrs =3D tdx_mrs, + .nr_mrs =3D ARRAY_SIZE(tdx_mrs), + .refresh =3D tdx_mr_refresh, + .write =3D tdx_mr_extend, +}; + +/* attribute groups of tdx_misc_dev */ +static const struct attribute_group *tdx_attr_groups[] =3D { + NULL, + NULL, +}; + +static int tdx_mr_init(void) +{ + int ret; + + u8 *report_buf __free(kfree) =3D kzalloc(TDX_REPORT_LEN, GFP_KERNEL); + + /* TDG.MR.RTMR.EXTEND requires 64-byte aligned input */ + u8 *extend_buf __free(kfree) =3D kmalloc(64, GFP_KERNEL); + + if (!report_buf || !extend_buf) + return -ENOMEM; + + tdx_report_buf =3D report_buf; + ret =3D tdx_mr_refresh(&tdx_measurements, NULL); if (ret) - goto out; + return ret; =20 - if (copy_to_user(req->tdreport, tdreport, TDX_REPORT_LEN)) - ret =3D -EFAULT; + for (size_t i =3D 0; i < ARRAY_SIZE(tdx_mrs); ++i) + *(long *)&tdx_mrs[i].mr_value +=3D (long)report_buf; =20 -out: - kfree(reportdata); - kfree(tdreport); + tdx_attr_groups[0] =3D tsm_mr_create_attribute_group(&tdx_measurements); + if (IS_ERR(tdx_attr_groups[0])) + return PTR_ERR(tdx_attr_groups[0]); =20 - return ret; + tdx_report_buf =3D no_free_ptr(report_buf); + tdx_extend_buf =3D no_free_ptr(extend_buf); + return 0; +} + +static void tdx_mr_deinit(void) +{ + tsm_mr_free_attribute_group(tdx_attr_groups[0]); + kfree(tdx_extend_buf); + kfree(tdx_report_buf); +} + +static long tdx_get_report0(struct tdx_report_req __user *req) +{ + return tdx_mr_report_locked(USER_SOCKPTR(req->reportdata), + USER_SOCKPTR(req->tdreport)); } =20 static void free_quote_buf(void *buf) @@ -159,91 +267,60 @@ static int wait_for_quote_completion(struct tdx_quote= _buf *quote_buf, u32 timeou =20 static int tdx_report_new(struct tsm_report *report, void *data) { - u8 *buf, *reportdata =3D NULL, *tdreport =3D NULL; + u8 *buf; struct tdx_quote_buf *quote_buf =3D quote_data; struct tsm_desc *desc =3D &report->desc; int ret; u64 err; =20 - /* TODO: switch to guard(mutex_intr) */ - if (mutex_lock_interruptible("e_lock)) - return -EINTR; - - /* - * If the previous request is timedout or interrupted, and the - * Quote buf status is still in GET_QUOTE_IN_FLIGHT (owned by - * VMM), don't permit any new request. - */ - if (quote_buf->status =3D=3D GET_QUOTE_IN_FLIGHT) { - ret =3D -EBUSY; - goto done; - } - - if (desc->inblob_len !=3D TDX_REPORTDATA_LEN) { - ret =3D -EINVAL; - goto done; - } - - reportdata =3D kmalloc(TDX_REPORTDATA_LEN, GFP_KERNEL); - if (!reportdata) { - ret =3D -ENOMEM; - goto done; - } - - tdreport =3D kzalloc(TDX_REPORT_LEN, GFP_KERNEL); - if (!tdreport) { - ret =3D -ENOMEM; - goto done; - } - - memcpy(reportdata, desc->inblob, desc->inblob_len); - - /* Generate TDREPORT0 using "TDG.MR.REPORT" TDCALL */ - ret =3D tdx_mcall_get_report0(reportdata, tdreport); - if (ret) { - pr_err("GetReport call failed\n"); - goto done; + scoped_cond_guard(mutex_intr, ret =3D -EINTR, "e_lock) { + /* + * If the previous request is timedout or interrupted, and the + * Quote buf status is still in GET_QUOTE_IN_FLIGHT (owned by + * VMM), don't permit any new request. + */ + if (quote_buf->status =3D=3D GET_QUOTE_IN_FLIGHT) + return -EBUSY; + + if (desc->inblob_len !=3D TDX_REPORTDATA_LEN) + return -EINVAL; + + memset(quote_data, 0, GET_QUOTE_BUF_SIZE); + + /* Update Quote buffer header */ + quote_buf->version =3D GET_QUOTE_CMD_VER; + quote_buf->in_len =3D TDX_REPORT_LEN; + + /* Generate TDREPORT0 using "TDG.MR.REPORT" TDCALL */ + ret =3D tdx_mr_report_locked(KERNEL_SOCKPTR(desc->inblob), + KERNEL_SOCKPTR(quote_buf->data)); + if (ret) + break; + + err =3D tdx_hcall_get_quote(quote_data, GET_QUOTE_BUF_SIZE); + if (err) { + pr_err("GetQuote hypercall failed, status:%llx\n", err); + return -EIO; + } + + ret =3D wait_for_quote_completion(quote_buf, getquote_timeout); + if (ret) { + pr_err("GetQuote request timedout\n"); + break; + } + + buf =3D kvmemdup(quote_buf->data, quote_buf->out_len, GFP_KERNEL); + if (!buf) + return -ENOMEM; + + report->outblob =3D buf; + report->outblob_len =3D quote_buf->out_len; + + /* + * TODO: parse the PEM-formatted cert chain out of the quote + * buffer when provided + */ } - - memset(quote_data, 0, GET_QUOTE_BUF_SIZE); - - /* Update Quote buffer header */ - quote_buf->version =3D GET_QUOTE_CMD_VER; - quote_buf->in_len =3D TDX_REPORT_LEN; - - memcpy(quote_buf->data, tdreport, TDX_REPORT_LEN); - - err =3D tdx_hcall_get_quote(quote_data, GET_QUOTE_BUF_SIZE); - if (err) { - pr_err("GetQuote hypercall failed, status:%llx\n", err); - ret =3D -EIO; - goto done; - } - - ret =3D wait_for_quote_completion(quote_buf, getquote_timeout); - if (ret) { - pr_err("GetQuote request timedout\n"); - goto done; - } - - buf =3D kvmemdup(quote_buf->data, quote_buf->out_len, GFP_KERNEL); - if (!buf) { - ret =3D -ENOMEM; - goto done; - } - - report->outblob =3D buf; - report->outblob_len =3D quote_buf->out_len; - - /* - * TODO: parse the PEM-formatted cert chain out of the quote buffer when - * provided - */ -done: - mutex_unlock("e_lock); - kfree(reportdata); - kfree(tdreport); - return ret; } =20 @@ -289,6 +366,7 @@ static struct miscdevice tdx_misc_dev =3D { .name =3D KBUILD_MODNAME, .minor =3D MISC_DYNAMIC_MINOR, .fops =3D &tdx_guest_fops, + .groups =3D tdx_attr_groups, }; =20 static const struct x86_cpu_id tdx_guest_ids[] =3D { @@ -311,10 +389,14 @@ static int __init tdx_guest_init(void) if (!x86_match_cpu(tdx_guest_ids)) return -ENODEV; =20 - ret =3D misc_register(&tdx_misc_dev); + ret =3D tdx_mr_init(); if (ret) return ret; =20 + ret =3D misc_register(&tdx_misc_dev); + if (ret) + goto free_tsm_mr; + quote_data =3D alloc_quote_buf(); if (!quote_data) { pr_err("Failed to allocate Quote buffer\n"); @@ -332,6 +414,8 @@ static int __init tdx_guest_init(void) free_quote_buf(quote_data); free_misc: misc_deregister(&tdx_misc_dev); +free_tsm_mr: + tdx_mr_deinit(); =20 return ret; } @@ -342,6 +426,7 @@ static void __exit tdx_guest_exit(void) tsm_unregister(&tdx_tsm_ops); free_quote_buf(quote_data); misc_deregister(&tdx_misc_dev); + tdx_mr_deinit(); } module_exit(tdx_guest_exit); =20 --=20 2.43.0