[RFC PATCH v2 3/4] tsm: Map RTMRs to TCG TPM PCRs

Samuel Ortiz
2024-01-28T21:27:24Z

Many user space and internal kernel subsystems (e.g. the Linux IMA)
expect a Root of Trust for Storage (RTS) that allows for extending
and reading measurement registers that are compatible with the TCG TPM
PCRs layout, e.g. a TPM. In order to allow those components to
alternatively use a platform TSM as their RTS, a TVM could map the
available RTMRs to one or more TCG TPM PCRs. Once configured, those PCR
to RTMR mappings give the kernel TSM layer all the necessary information
to be a RTS for e.g. the Linux IMA or any other components that expects
a TCG compliant TPM PCRs layout.

TPM PCR mappings are statically configured through the TSM provider
capabilities. A TSM backend defines its number of RTMRs, and for each
one of them can define a bitmask of TCG TPM PCR it maps to. As they are
TSM backend specific, those mappings are to some extend architecture
specific. Each architecture is free to decide and choose how it builds
it, e.g. by requesting an EFI firmware when it supports the EFI_CC
protocol.

The tsm-configfs rtmrs/<rtmrN>tcg_map describes these static mappings.

Signed-off-by: Samuel Ortiz <sameo@rivosinc.com>
---
Documentation/ABI/testing/configfs-tsm | 14 +++++
drivers/virt/coco/tsm.c | 74 ++++++++++++++++++++++++++
2 files changed, 88 insertions(+)

diff --git a/Documentation/ABI/testing/configfs-tsm b/Documentation/ABI/testing/configfs-tsm
index 590e103a9bcd..5d20a872475e 100644
--- a/Documentation/ABI/testing/configfs-tsm
+++ b/Documentation/ABI/testing/configfs-tsm
@@ -91,3 +91,17 @@ Description:
can be mapped to a hardware RTMR by writing into its index
attribute. The TSM provider will then map the configfs entry to
its corresponding hardware register.
+
+What: /sys/kernel/config/tsm/rtmrs/$name/tcg_map
+Date: January, 2024
+KernelVersion: v6.8
+Contact: linux-coco@lists.linux.dev
+Description:
+ (RO) A representation of the architecturally defined mapping
+ between this RTMR and one or more TCG TPM PCRs [1]. When using
+ a TSM as Root of Trust for Storage (RTS), TCG TPM PCRs
+ associated semantics and indexes can be used when RTMRs are
+ logically mapped to TPM PCRs.
+
+ [1]: TCG PC Client Specific Platform Firmware Profile Specification
+ https://trustedcomputinggroup.org/resource/pc-client-specific-platform-firmware-profile-specification/
diff --git a/drivers/virt/coco/tsm.c b/drivers/virt/coco/tsm.c
index bb9ed2d2accc..d03cf5173bc9 100644
--- a/drivers/virt/coco/tsm.c
+++ b/drivers/virt/coco/tsm.c
@@ -419,6 +419,46 @@ static const struct config_item_type tsm_reports_type = {
.ct_group_ops = &tsm_report_group_ops,
};

+static int tsm_rtmr_build_tcg_map(const struct tsm_provider *tsm,
+ const struct tsm_rtmr_state *rtmr_state,
+ u32 rtmr_idx)
+{
+ const struct tsm_ops *ops;
+ unsigned long pcr_mask;
+ int i;
+
+ lockdep_assert_held_write(&tsm_rwsem);
+
+ ops = tsm->ops;
+ if (!ops)
+ return -ENOTTY;
+
+ if (!ops->capabilities.rtmrs)
+ return -ENXIO;
+
+ pcr_mask = ops->capabilities.rtmrs[rtmr_idx].tcg_pcr_mask;
+
+ /* Check that the PCR mask is valid */
+ for (i = 0; i < TPM2_PLATFORM_PCR; i++) {
+ if (!(pcr_mask & BIT(i)))
+ continue;
+
+ /* If another RTMR maps to this PCR, the mask is discarded */
+ if (tsm_rtmrs->tcg_map[i] &&
+ tsm_rtmrs->tcg_map[i] != rtmr_state)
+ return -EBUSY;
+ }
+
+ for (i = 0; i < TPM2_PLATFORM_PCR; i++) {
+ if (!(pcr_mask & BIT(i)))
+ continue;
+
+ tsm_rtmrs->tcg_map[i] = rtmr_state;
+ }
+
+ return 0;
+}
+
static ssize_t tsm_rtmr_index_store(struct config_item *cfg,
const char *buf, size_t len)
{
@@ -449,6 +489,10 @@ static ssize_t tsm_rtmr_index_store(struct config_item *cfg,
if (tsm_rtmrs->rtmrs[val])
return -EINVAL;

+ rc = tsm_rtmr_build_tcg_map(&provider, rtmr_state, val);
+ if (rc)
+ return rc;
+
rtmr_state->index = val;
rtmr_state->alg = ops->capabilities.rtmrs[val].hash_alg;

@@ -472,8 +516,38 @@ static ssize_t tsm_rtmr_index_show(struct config_item *cfg,
}
CONFIGFS_ATTR(tsm_rtmr_, index);

+static ssize_t tsm_rtmr_tcg_map_show(struct config_item *cfg,
+ char *buf)
+{
+ struct tsm_rtmr_state *rtmr_state = to_tsm_rtmr_state(cfg);
+ unsigned int nr_pcrs = ARRAY_SIZE(tsm_rtmrs->tcg_map), i;
+ unsigned long *pcr_mask;
+ ssize_t len;
+
+ /* Build a bitmap mask of all PCRs that this RTMR covers */
+ pcr_mask = bitmap_zalloc(nr_pcrs, GFP_KERNEL);
+ if (!pcr_mask)
+ return -ENOMEM;
+
+ guard(rwsem_read)(&tsm_rwsem);
+ for (i = 0; i < nr_pcrs; i++) {
+ if (tsm_rtmrs->tcg_map[i] != rtmr_state)
+ continue;
+
+ __set_bit(i, pcr_mask);
+ }
+
+ len = bitmap_print_list_to_buf(buf, pcr_mask, nr_pcrs, 0,
+ nr_pcrs * 3 /* 2 ASCII digits and one comma */);
+ bitmap_free(pcr_mask);
+
+ return len;
+}
+CONFIGFS_ATTR_RO(tsm_rtmr_, tcg_map);
+
static struct configfs_attribute *tsm_rtmr_attrs[] = {
&tsm_rtmr_attr_index,
+ &tsm_rtmr_attr_tcg_map,
NULL,
};

--
2.42.0