Skip to contentSkip to conversation

tpm: add mssim backend

PATCH In review James Bottomley 4 participants 9 messages Dec 12, 2024 – Jan 7, 2025 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

v11 cover tpm: add mssim backend

The requested feedback was to convert the tpmdev handler to being json based, which requires rethreading all the backends. The good news is this reduced quite a bit of code (especially as I converted it to error_fatal handling as well, which removes the return status threading).

v3 pulls out more unneeded code in the visitor conversion, makes migration work on external state preservation of the simulator and adds documentation

v4 puts back the wrapper options (but doesn't add any for mssim since it post dates the necessity)

v5 rebases to the latest master branch and adjusts for removed use_FOO ptrs

v5 updates help to exit zero; does some checkpatch tidying

v7 merge review feedback and add acks.

v8 adds better error handling, more code tidies and adds command
socket disconnection/reconnection (instead of trying to keep the socket open the whole time). This adds overhead, but makes debugging guest kernel TPM issues much easier.

v9 Fix merge conflict with optarg->optstr conversion

v10 Fix more merge conflicts and update API versions

v11 Fix another merge conflict and correct a warm reboot problem where
the TPM isn't getting reset (meaning the PCR values are wrong).

James

---

James Bottomley (2):
tpm: convert tpmdev options processing to new visitor format
tpm: add backend for mssim

 MAINTAINERS                    |   6 +
 backends/tpm/Kconfig           |   5 +
 backends/tpm/meson.build       |   1 +
 backends/tpm/tpm_emulator.c    |  25 +--
 backends/tpm/tpm_mssim.c       | 335 +++++++++++++++++++++++++++++++++
 backends/tpm/tpm_mssim.h       |  44 +++++
 backends/tpm/tpm_passthrough.c |  23 +--
 docs/specs/tpm.rst             |  39 ++++
 include/sysemu/tpm.h           |   5 +-
 include/sysemu/tpm_backend.h   |   2 +-
 qapi/tpm.json                  |  50 ++++-
 system/tpm-hmp-cmds.c          |   9 +
 system/tpm.c                   |  91 ++++-----
 system/vl.c                    |  19 +-
 14 files changed, 546 insertions(+), 108 deletions(-)
 create mode 100644 backends/tpm/tpm_mssim.c
 create mode 100644 backends/tpm/tpm_mssim.h
Signature · 1 line
2.35.3
#1 lore raw

PATCH v11 1/2 tpm: convert tpmdev options processing to new visitor format

Instead of processing the tpmdev options using the old qemu options, convert to the new visitor format which also allows the passing of json on the command line.

---
v4: add TpmConfiOptions
v5: exit(0) for help
v7: adjust line lengths, free options
v8: minor updates; add tested/reviewed-by
v9: optarg->optstr

---
 backends/tpm/tpm_emulator.c    | 25 ++++------
 backends/tpm/tpm_passthrough.c | 23 +++------
 include/sysemu/tpm.h           |  5 +-
 include/sysemu/tpm_backend.h   |  2 +-
 qapi/tpm.json                  | 21 ++++++++
 system/tpm.c                   | 91 ++++++++++++++--------------------
 system/vl.c                    | 19 +------
 7 files changed, 81 insertions(+), 105 deletions(-)
backends/tpm/tpm_emulator.c +10 -15
index aa05dab6ae..d60d7d9328 100644
@@ -609,33 +609,29 @@ err_exit:
     return -1;
 }
 
-static int tpm_emulator_handle_device_opts(TPMEmulator *tpm_emu, QemuOpts *opts)
+static int tpm_emulator_handle_device_opts(TPMEmulator *tpm_emu,
+                                           TpmCreateOptions *opts)
 {
-    const char *value;
     Error *err = NULL;
     Chardev *dev;
 
-    value = qemu_opt_get(opts, "chardev");
-    if (!value) {
-        error_report("tpm-emulator: parameter 'chardev' is missing");
-        goto err;
-    }
+    tpm_emu->options = QAPI_CLONE(TPMEmulatorOptions, &opts->u.emulator);
+    tpm_emu->data_ioc = NULL;
 
-    dev = qemu_chr_find(value);
+    dev = qemu_chr_find(opts->u.emulator.chardev);
     if (!dev) {
-        error_report("tpm-emulator: tpm chardev '%s' not found", value);
+        error_report("tpm-emulator: tpm chardev '%s' not found",
+                     opts->u.emulator.chardev);
         goto err;
     }
 
     if (!qemu_chr_fe_init(&tpm_emu->ctrl_chr, dev, &err)) {
         error_prepend(&err, "tpm-emulator: No valid chardev found at '%s':",
-                      value);
+                      opts->u.emulator.chardev);
         error_report_err(err);
         goto err;
     }
 
-    tpm_emu->options->chardev = g_strdup(value);
-
     if (tpm_emulator_prepare_data_fd(tpm_emu) < 0) {
         goto err;
     }
@@ -674,7 +670,7 @@ err:
     return -1;
 }
 
-static TPMBackend *tpm_emulator_create(QemuOpts *opts)
+static TPMBackend *tpm_emulator_create(TpmCreateOptions *opts)
 {
     TPMBackend *tb = TPM_BACKEND(object_new(TYPE_TPM_EMULATOR));
 
@@ -999,7 +995,6 @@ static void tpm_emulator_inst_init(Object *obj)
 
     trace_tpm_emulator_inst_init();
 
-    tpm_emu->options = g_new0(TPMEmulatorOptions, 1);
     tpm_emu->cur_locty_number = ~0;
     qemu_mutex_init(&tpm_emu->mutex);
     tpm_emu->vmstate =
@@ -1016,7 +1011,7 @@ static void tpm_emulator_shutdown(TPMEmulator *tpm_emu)
 {
     ptm_res res;
 
-    if (!tpm_emu->options->chardev) {
+    if (!tpm_emu->data_ioc) {
         /* was never properly initialized */
         return;
     }
backends/tpm/tpm_passthrough.c +7 -16
index 179697a3a9..54183b89a4 100644
@@ -252,21 +252,13 @@ static int tpm_passthrough_open_sysfs_cancel(TPMPassthruState *tpm_pt)
 }
 
 static int
-tpm_passthrough_handle_device_opts(TPMPassthruState *tpm_pt, QemuOpts *opts)
+tpm_passthrough_handle_device_opts(TPMPassthruState *tpm_pt,
+                                   TpmCreateOptions *opts)
 {
-    const char *value;
+    tpm_pt->options = QAPI_CLONE(TPMPassthroughOptions, &opts->u.passthrough);
 
-    value = qemu_opt_get(opts, "cancel-path");
-    if (value) {
-        tpm_pt->options->cancel_path = g_strdup(value);
-    }
-
-    value = qemu_opt_get(opts, "path");
-    if (value) {
-        tpm_pt->options->path = g_strdup(value);
-    }
-
-    tpm_pt->tpm_dev = value ? value : TPM_PASSTHROUGH_DEFAULT_DEVICE;
+    tpm_pt->tpm_dev = opts->u.passthrough.path ? opts->u.passthrough.path :
+            TPM_PASSTHROUGH_DEFAULT_DEVICE;
     tpm_pt->tpm_fd = qemu_open_old(tpm_pt->tpm_dev, O_RDWR);
     if (tpm_pt->tpm_fd < 0) {
         error_report("Cannot access TPM device using '%s': %s",
@@ -288,11 +280,11 @@ tpm_passthrough_handle_device_opts(TPMPassthruState *tpm_pt, QemuOpts *opts)
     return 0;
 }
 
-static TPMBackend *tpm_passthrough_create(QemuOpts *opts)
+static TPMBackend *tpm_passthrough_create(TpmCreateOptions *tco)
 {
     Object *obj = object_new(TYPE_TPM_PASSTHROUGH);
 
-    if (tpm_passthrough_handle_device_opts(TPM_PASSTHROUGH(obj), opts)) {
+    if (tpm_passthrough_handle_device_opts(TPM_PASSTHROUGH(obj), tco)) {
         object_unref(obj);
         return NULL;
     }
@@ -344,7 +336,6 @@ static void tpm_passthrough_inst_init(Object *obj)
 {
     TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(obj);
 
-    tpm_pt->options = g_new0(TPMPassthroughOptions, 1);
     tpm_pt->tpm_fd = -1;
     tpm_pt->cancel_fd = -1;
 }
include/sysemu/tpm.h +3 -2
index 1ee568b3b6..e9ac0e0a3a 100644
@@ -17,8 +17,9 @@
 
 #ifdef CONFIG_TPM
 
-int tpm_config_parse(QemuOptsList *opts_list, const char *optstr);
-int tpm_init(void);
+void tpm_config_parse(const char *optstr);
+void tpm_init(void);
+
 void tpm_cleanup(void);
 
 typedef enum TPMVersion {
include/sysemu/tpm_backend.h +1 -1
index 7fabafefee..56b80cddbe 100644
@@ -57,7 +57,7 @@ struct TPMBackendClass {
     /* get a descriptive text of the backend to display to the user */
     const char *desc;
 
-    TPMBackend *(*create)(QemuOpts *opts);
+    TPMBackend *(*create)(TpmCreateOptions *tco);
 
     /* start up the TPM on the backend - optional */
     int (*startup_tpm)(TPMBackend *t, size_t buffersize);
qapi/tpm.json +21 -0
index a16a72edb9..e6345d424b 100644
@@ -142,6 +142,27 @@
             'emulator': 'TPMEmulatorOptionsWrapper' },
   'if': 'CONFIG_TPM' }
 
+##
+# @TpmCreateOptions:
+#
+# A union referencing different TPM backend types' configuration options
+#   without the wrapper to be usable by visitors.
+#
+# @type: - 'passthrough' The configuration options for the TPM passthrough type
+#        - 'emulator' The configuration options for TPM emulator backend type
+#
+# @id: The Id of the TPM
+#
+# Since: 9.0
+##
+{ 'union': 'TpmCreateOptions',
+  'base': { 'type': 'TpmType',
+            'id' : 'str' },
+  'discriminator': 'type',
+  'data': { 'passthrough' : 'TPMPassthroughOptions',
+            'emulator': 'TPMEmulatorOptions' },
+  'if': 'CONFIG_TPM' }
+
 ##
 # @TPMInfo:
 #
system/tpm.c +37 -54

147 lines not on this page · raw message

system/vl.c +2 -17
index 2f855d83fb..e1af87b31e 100644
@@ -331,16 +331,6 @@ static QemuOptsList qemu_object_opts = {
     },
 };
 
-static QemuOptsList qemu_tpmdev_opts = {
-    .name = "tpmdev",
-    .implied_opt_name = "type",
-    .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
-    .desc = {
-        /* options are defined in the TPM backends */
-        { /* end of list */ }
-    },
-};
-
 static QemuOptsList qemu_overcommit_opts = {
     .name = "overcommit",
     .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
@@ -2036,9 +2026,7 @@ static void qemu_create_late_backends(void)
         exit(1);
     }
 
-    if (tpm_init() < 0) {
-        exit(1);
-    }
+    tpm_init();
 
     qemu_opts_foreach(qemu_find_opts("mon"),
                       mon_init_func, NULL, &error_fatal);
@@ -2778,7 +2766,6 @@ void qemu_init(int argc, char **argv)
     qemu_add_opts(&qemu_boot_opts);
     qemu_add_opts(&qemu_add_fd_opts);
     qemu_add_opts(&qemu_object_opts);
-    qemu_add_opts(&qemu_tpmdev_opts);
     qemu_add_opts(&qemu_overcommit_opts);
     qemu_add_opts(&qemu_msg_opts);
     qemu_add_opts(&qemu_name_opts);
@@ -3014,9 +3001,7 @@ void qemu_init(int argc, char **argv)
                 break;
 #ifdef CONFIG_TPM
             case QEMU_OPTION_tpmdev:
-                if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
-                    exit(1);
-                }
+                tpm_config_parse(optarg);
                 break;
 #endif
             case QEMU_OPTION_mempath:
Signature · 1 line
2.35.3
#2 lore raw

Re: [PATCH v11 1/2] tpm: convert tpmdev options processing to new visitor format

Quoted 35 lines · James Bottomley

Instead of processing the tpmdev options using the old qemu options, convert to the new visitor format which also allows the passing of json on the command line.

---
v4: add TpmConfiOptions
v5: exit(0) for help
v7: adjust line lengths, free options
v8: minor updates; add tested/reviewed-by
v9: optarg->optstr

---
  backends/tpm/tpm_emulator.c    | 25 ++++------
  backends/tpm/tpm_passthrough.c | 23 +++------
  include/sysemu/tpm.h           |  5 +-
  include/sysemu/tpm_backend.h   |  2 +-
  qapi/tpm.json                  | 21 ++++++++
  system/tpm.c                   | 91 ++++++++++++++--------------------
  system/vl.c                    | 19 +------
  7 files changed, 81 insertions(+), 105 deletions(-)
qapi/tpm.json +2 -0
index a16a72edb9..e6345d424b 100644
@@ -142,6 +142,27 @@
              'emulator': 'TPMEmulatorOptionsWrapper' },
    'if': 'CONFIG_TPM' }
 
+##
+# @TpmCreateOptions:

qapi/tpm.json line 156

Quoted 10 lines
qapi/tpm.json +10 -0
+#
+# A union referencing different TPM backend types' configuration options
+#   without the wrapper to be usable by visitors.
+#
+# @type: - 'passthrough' The configuration options for the TPM passthrough type
+#        - 'emulator' The configuration options for TPM emulator backend type
+#
+# @id: The Id of the TPM
+#
+# Since: 9.0

(Now 10.0)

Quoted 12 lines
qapi/tpm.json +9 -0
+##
+{ 'union': 'TpmCreateOptions',
+  'base': { 'type': 'TpmType',
+            'id' : 'str' },
+  'discriminator': 'type',
+  'data': { 'passthrough' : 'TPMPassthroughOptions',
+            'emulator': 'TPMEmulatorOptions' },
+  'if': 'CONFIG_TPM' }
+
  ##
  # @TPMInfo:
  #
#3 lore raw

Re: [PATCH v11 1/2] tpm: convert tpmdev options processing to new visitor format

In reply to James Bottomley
Quoted 7 lines · James Bottomley

Instead of processing the tpmdev options using the old qemu options, convert to the new visitor format which also allows the passing of json on the command line.

[...]

qapi/tpm.json

Quoted 4 lines
qapi/tpm.json +0 -0
index a16a72edb9..e6345d424b 100644

Looks like you missed my review of v10. I'll copy it below for your convenience.

qapi/tpm.json line 149

Quoted 9 lines
qapi/tpm.json +5 -0
@@ -142,6 +142,27 @@
             'emulator': 'TPMEmulatorOptionsWrapper' },
   'if': 'CONFIG_TPM' }
 
+##
+# @TpmCreateOptions:
+#
+# A union referencing different TPM backend types' configuration options
+#   without the wrapper to be usable by visitors.

reST trap: this is a definition list. Delete the second line's indentation to make it a paragraph:

# A union referencing different TPM backend types' configuration

   # options without the wrapper to be usable by visitors.

qapi/tpm.json line 152

Quoted 3 lines
qapi/tpm.json +3 -0
+#
+# @type: - 'passthrough' The configuration options for the TPM passthrough type
+#        - 'emulator' The configuration options for TPM emulator backend type

docs/devel/qapi-code-gen.rst:

Descriptions start with '\@name:'. The description text must be indented like this::

# @name: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed
# do eiusmod tempor incididunt ut labore et dolore magna aliqua.

You indent more to make the '-' line up. Hmm.

Let's indent this like @TpmTypeOptions right above, namely

# @type:
# - 'passthrough' The configuration options for the TPM
# passthrough type
# - 'emulator' The configuration options for TPM emulator backend
# type

qapi/tpm.json line 154

Quoted 2 lines
qapi/tpm.json +2 -0
+#
+# @id: The Id of the TPM

What kind of Id is this?

qapi/tpm.json line 156

Quoted 2 lines
qapi/tpm.json +2 -0
+#
+# Since: 9.0

10.0

qapi/tpm.json line 164

Quoted 8 lines
qapi/tpm.json +8 -0
+##
+{ 'union': 'TpmCreateOptions',
+  'base': { 'type': 'TpmType',
+            'id' : 'str' },
+  'discriminator': 'type',
+  'data': { 'passthrough' : 'TPMPassthroughOptions',
+            'emulator': 'TPMEmulatorOptions' },
+  'if': 'CONFIG_TPM' }

This is a flattened version of TpmTypeOptions with additional member @id.

Flattened: the union branches use Foo instead of FooWrapper.

@id: I guess query-tpm has it one level up, in TPMInfo.

Okay.

Quoted 4 lines
qapi/tpm.json +1 -0
+
 ##
 # @TPMInfo:
 #

[...]

#4 lore raw

PATCH v11 2/2 tpm: add backend for mssim

In reply to James Bottomley

The Microsoft Simulator (mssim) is the reference emulation platform for the TCG TPM 2.0 specification.

https://github.com/Microsoft/ms-tpm-20-ref.git

It exports a fairly simple network socket based protocol on two sockets, one for command (default 2321) and one for control (default 2322). This patch adds a simple backend that can speak the mssim protocol over the network. It also allows the two sockets to be specified on the command line. The benefits are twofold: firstly it gives us a backend that actually speaks a standard TPM emulation protocol instead of the linux specific TPM driver format of the current emulated TPM backend and secondly, using the microsoft protocol, the end point of the emulator can be anywhere on the network, facilitating the cloud use case where a central TPM service can be used over a control network.

The implementation does basic control commands like power off/on, but doesn't implement cancellation or startup. The former because cancellation is pretty much useless on a fast operating TPM emulator and the latter because this emulator is designed to be used with OVMF which itself does TPM startup and I wanted to validate that.

To run this, simply download an emulator based on the MS specification (package ibmswtpm2 on openSUSE) and run it, then add these two lines to the qemu command and it will use the emulator.

    -tpmdev mssim,id=tpm0 \
    -device tpm-crb,tpmdev=tpm0 \

to use a remote emulator replace the first line with

    -tpmdev "{'type':'mssim','id':'tpm0','command':{'type':inet,'host':'remote','port':'2321'}}"

tpm-tis also works as the backend.

---

v2: convert to SocketAddr json and use qio_channel_socket_connect_sync()
v3: gate control power off by migration state keep control socket disconnected
to test outside influence and add docs.
v7: TPMmssim -> TPMMssim; doc and json fixes
Make command socket open each time (makes OS debugging easier)
v11: add startup method to make sure TPM is reset on reboot

---
 MAINTAINERS              |   6 +
 backends/tpm/Kconfig     |   5 +
 backends/tpm/meson.build |   1 +
 backends/tpm/tpm_mssim.c | 335 +++++++++++++++++++++++++++++++++++++++
 backends/tpm/tpm_mssim.h |  44 +++++
 docs/specs/tpm.rst       |  39 +++++
 qapi/tpm.json            |  31 +++-
 system/tpm-hmp-cmds.c    |   9 ++
 8 files changed, 466 insertions(+), 4 deletions(-)
 create mode 100644 backends/tpm/tpm_mssim.c
 create mode 100644 backends/tpm/tpm_mssim.h
MAINTAINERS +6 -0
index aaf0505a21..26f577699d 100644
@@ -3398,10 +3398,16 @@ F: include/hw/acpi/tpm.h
 F: include/sysemu/tpm*
 F: qapi/tpm.json
 F: backends/tpm/
+X: backends/tpm/tpm_mssim.*
 F: tests/qtest/*tpm*
 F: docs/specs/tpm.rst
 T: git https://github.com/stefanberger/qemu-tpm.git tpm-next
 
+MSSIM TPM Backend
+M: James Bottomley <[email protected]>
+S: Maintained
+F: backends/tpm/tpm_mssim.*
+
 SPDM
 M: Alistair Francis <[email protected]>
 S: Maintained
backends/tpm/Kconfig +5 -0
index 5d91eb89c2..d6d6fa53e9 100644
@@ -12,3 +12,8 @@ config TPM_EMULATOR
     bool
     default y
     depends on TPM_BACKEND
+
+config TPM_MSSIM
+    bool
+    default y
+    depends on TPM_BACKEND
backends/tpm/meson.build +1 -0
index 0bfa6c422b..c6f7c24cb1 100644
@@ -3,4 +3,5 @@ if have_tpm
   system_ss.add(files('tpm_util.c'))
   system_ss.add(when: 'CONFIG_TPM_PASSTHROUGH', if_true: files('tpm_passthrough.c'))
   system_ss.add(when: 'CONFIG_TPM_EMULATOR', if_true: files('tpm_emulator.c'))
+  system_ss.add(when: 'CONFIG_TPM_MSSIM', if_true: files('tpm_mssim.c'))
 endif
backends/tpm/tpm_mssim.c new file +335 -0

335 lines not on this page · raw message

backends/tpm/tpm_mssim.h new file +44 -0

44 lines not on this page · raw message

docs/specs/tpm.rst +39 -0
index 1ad36ad709..b279780be2 100644
@@ -276,6 +276,42 @@ available as a module (assuming a TPM 2 is passed through):
   /sys/devices/LNXSYSTEM:00/LNXSYBUS:00/MSFT0101:00/tpm/tpm0/pcr-sha256/9
   ...
 
+The QEMU TPM Microsoft Simulator Device
+---------------------------------------
+
+The Microsoft Simulator (mssim) is the reference emulation platform
+for the TCG TPM 2.0 specification.  It provides a reference
+implementation for the TPM 2.0 written by Microsoft (See
+`ms-tpm-20-ref`_ on github).  The reference implementation starts a
+network server and listens for TPM commands on port 2321 and TPM
+Platform control commands on port 2322, although these can be altered.
+The QEMU mssim TPM backend talks to this implementation.  By default
+it connects to the default ports on localhost:
+
+.. code-block:: console
+
+  qemu-system-x86_64 <qemu-options> \
+    -tpmdev mssim,id=tpm0 \
+    -device tpm-crb,tpmdev=tpm0
+
+
+Although it can also communicate with a remote host, which must be
+specified as a SocketAddress via json or dotted keys on the command
+line for each of the command and control ports:
+
+.. code-block:: console
+
+  qemu-system-x86_64 <qemu-options> \
+    -tpmdev "{'type':'mssim','id':'tpm0','command':{'type':'inet','host':'remote','port':'2321'},'control':{'type':'inet','host':'remote','port':'2322'}}" \
+    -device tpm-crb,tpmdev=tpm0
+
+
+The mssim backend supports snapshotting and migration by not resetting
+the TPM on start up and not powering it down on halt if the VM is in
+migration, but the state of the Microsoft Simulator server must be
+preserved (or the server kept running) outside of QEMU for restore to
+be successful.
+
 The QEMU TPM emulator device
 ----------------------------
 
@@ -549,3 +585,6 @@ the following:
 
 .. _SWTPM protocol:
    https://github.com/stefanberger/swtpm/blob/master/man/man3/swtpm_ioctls.pod
+
+.. _ms-tpm-20-ref:
+   https://github.com/microsoft/ms-tpm-20-ref
qapi/tpm.json +27 -4
index e6345d424b..eed4cb9e82 100644
@@ -6,6 +6,8 @@
 # = TPM (trusted platform module) devices
 ##
 
+{ 'include': 'sockets.json' }
+
 ##
 # @TpmModel:
 #
@@ -48,9 +50,11 @@
 #
 # @emulator: Software Emulator TPM type (since 2.11)
 #
+# @mssim: Microsoft TPM Emulator (since 9.0)
+#
 # Since: 1.5
 ##
-{ 'enum': 'TpmType', 'data': [ 'passthrough', 'emulator' ],
+{ 'enum': 'TpmType', 'data': [ 'passthrough', 'emulator', 'mssim' ],
   'if': 'CONFIG_TPM' }
 
 ##
@@ -65,7 +69,7 @@
 # .. qmp-example::
 #
 #     -> { "execute": "query-tpm-types" }
-#     <- { "return": [ "passthrough", "emulator" ] }
+#     <- { "return": [ "passthrough", "emulator", "mssim" ] }
 ##
 { 'command': 'query-tpm-types', 'returns': ['TpmType'],
   'if': 'CONFIG_TPM' }
@@ -121,6 +125,22 @@
   'data': { 'data': 'TPMEmulatorOptions' },
   'if': 'CONFIG_TPM' }
 
+##
+# @TPMMssimOptions:
+#
+# Information for the mssim emulator connection
+#
+# @command: command socket for the TPM emulator
+#
+# @control: control socket for the TPM emulator
+#
+# Since: 9.0
+##
+{ 'struct': 'TPMMssimOptions',
+  'data': { '*command': 'SocketAddress',
+            '*control': 'SocketAddress' },
+  'if': 'CONFIG_TPM' }
+
 ##
 # @TpmTypeOptions:
 #
@@ -132,6 +152,7 @@
 #       passthrough type
 #     - 'emulator' The configuration options for TPM emulator backend
 #       type
+#     - 'mssim' The configuration options for TPM emulator mssim type
 #
 # Since: 1.5
 ##
@@ -139,7 +160,8 @@
   'base': { 'type': 'TpmType' },
   'discriminator': 'type',
   'data': { 'passthrough' : 'TPMPassthroughOptionsWrapper',
-            'emulator': 'TPMEmulatorOptionsWrapper' },
+            'emulator': 'TPMEmulatorOptionsWrapper',
+            'mssim' : 'TPMMssimOptions' },
   'if': 'CONFIG_TPM' }
 
 ##
@@ -160,7 +182,8 @@
             'id' : 'str' },
   'discriminator': 'type',
   'data': { 'passthrough' : 'TPMPassthroughOptions',
-            'emulator': 'TPMEmulatorOptions' },
+            'emulator': 'TPMEmulatorOptions',
+            'mssim': 'TPMMssimOptions' },
   'if': 'CONFIG_TPM' }
 
 ##
system/tpm-hmp-cmds.c +9 -0
index 9ed6ad6c4d..12293f86c1 100644
@@ -19,6 +19,7 @@ void hmp_info_tpm(Monitor *mon, const QDict *qdict)
     unsigned int c = 0;
     TPMPassthroughOptions *tpo;
     TPMEmulatorOptions *teo;
+    TPMMssimOptions *tmo;
 
     info_list = qmp_query_tpm(&err);
     if (err) {
@@ -52,6 +53,14 @@ void hmp_info_tpm(Monitor *mon, const QDict *qdict)
             teo = ti->options->u.emulator.data;
             monitor_printf(mon, ",chardev=%s", teo->chardev);
             break;
+        case TPM_TYPE_MSSIM:
+            tmo = &ti->options->u.mssim;
+            monitor_printf(mon, ",command=%s:%s,control=%s:%s",
+                           tmo->command->u.inet.host,
+                           tmo->command->u.inet.port,
+                           tmo->control->u.inet.host,
+                           tmo->control->u.inet.port);
+            break;
         case TPM_TYPE__MAX:
             break;
         }
Signature · 1 line
2.35.3
#5 lore raw

Re: [PATCH v11 2/2] tpm: add backend for mssim

Quoted 68 lines · James Bottomley

The Microsoft Simulator (mssim) is the reference emulation platform for the TCG TPM 2.0 specification.

https://github.com/Microsoft/ms-tpm-20-ref.git

It exports a fairly simple network socket based protocol on two sockets, one for command (default 2321) and one for control (default 2322). This patch adds a simple backend that can speak the mssim protocol over the network. It also allows the two sockets to be specified on the command line. The benefits are twofold: firstly it gives us a backend that actually speaks a standard TPM emulation protocol instead of the linux specific TPM driver format of the current emulated TPM backend and secondly, using the microsoft protocol, the end point of the emulator can be anywhere on the network, facilitating the cloud use case where a central TPM service can be used over a control network.

The implementation does basic control commands like power off/on, but doesn't implement cancellation or startup. The former because cancellation is pretty much useless on a fast operating TPM emulator and the latter because this emulator is designed to be used with OVMF which itself does TPM startup and I wanted to validate that.

To run this, simply download an emulator based on the MS specification (package ibmswtpm2 on openSUSE) and run it, then add these two lines to the qemu command and it will use the emulator.

    -tpmdev mssim,id=tpm0 \
    -device tpm-crb,tpmdev=tpm0 \

to use a remote emulator replace the first line with

    -tpmdev "{'type':'mssim','id':'tpm0','command':{'type':inet,'host':'remote','port':'2321'}}"

tpm-tis also works as the backend.

---

v2: convert to SocketAddr json and use qio_channel_socket_connect_sync()
v3: gate control power off by migration state keep control socket disconnected
to test outside influence and add docs.
v7: TPMmssim -> TPMMssim; doc and json fixes
Make command socket open each time (makes OS debugging easier)
v11: add startup method to make sure TPM is reset on reboot

---
 MAINTAINERS              |   6 +
 backends/tpm/Kconfig     |   5 +
 backends/tpm/meson.build |   1 +
 backends/tpm/tpm_mssim.c | 335 +++++++++++++++++++++++++++++++++++++++
 backends/tpm/tpm_mssim.h |  44 +++++
 docs/specs/tpm.rst       |  39 +++++
 qapi/tpm.json            |  31 +++-
 system/tpm-hmp-cmds.c    |   9 ++
 8 files changed, 466 insertions(+), 4 deletions(-)
 create mode 100644 backends/tpm/tpm_mssim.c
 create mode 100644 backends/tpm/tpm_mssim.h
backends/tpm/tpm_mssim.c new file +0 -0
new file mode 100644
index 0000000000..8f105fc924

backends/tpm/tpm_mssim.c line 5

Quoted 5 lines
backends/tpm/tpm_mssim.c new file +5 -0
@@ -0,0 +1,335 @@
+/*
+ * Emulator TPM driver which connects over the mssim protocol
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * Copyright (c) 2022

Copyright by whom ? I presume yourself, but I wouldn't normally assume the 'Author' line applies to the Copyright line.

Quoted 11 lines
backends/tpm/tpm_mssim.c new file +3 -0
+ * Author: James Bottomley <[email protected]>
+ */
+
backends/tpm/tpm_mssim.h new file +0 -0
new file mode 100644
index 0000000000..397474e4f6

backends/tpm/tpm_mssim.h line 9

Quoted 9 lines
backends/tpm/tpm_mssim.h new file +9 -0
@@ -0,0 +1,44 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * The code below is copied from the Microsoft/TCG Reference implementation
+ *
+ *  https://github.com/Microsoft/ms-tpm-20-ref.git
+ *
+ * In file TPMCmd/Simulator/include/TpmTcpProtocol.h
+ */

That file has a volumous copyright header that I would expect to be preserved here.

Quoted 14 lines
qapi/tpm.json +2 -0
index e6345d424b..eed4cb9e82 100644
@@ -6,6 +6,8 @@
 # = TPM (trusted platform module) devices
 ##
 
+{ 'include': 'sockets.json' }
+
 ##
 # @TpmModel:
 #

qapi/tpm.json line 53

Quoted 4 lines
qapi/tpm.json +1 -0
@@ -48,9 +50,11 @@
 #
 # @emulator: Software Emulator TPM type (since 2.11)
 #
+# @mssim: Microsoft TPM Emulator (since 9.0)

Sorry, this needs a trivial bump to 10.0 now as that's the current dev cycle

qapi/tpm.json line 137

Quoted 10 lines
qapi/tpm.json +10 -0
+##
+# @TPMMssimOptions:
+#
+# Information for the mssim emulator connection
+#
+# @command: command socket for the TPM emulator
+#
+# @control: control socket for the TPM emulator
+#
+# Since: 9.0

Likewise.

qapi/tpm.json line 146

Quoted 9 lines
qapi/tpm.json +6 -0
+##
+{ 'struct': 'TPMMssimOptions',
+  'data': { '*command': 'SocketAddress',
+            '*control': 'SocketAddress' },
+  'if': 'CONFIG_TPM' }
+
 ##
 # @TpmTypeOptions:
 #

Functionally the code looks mergable with just those bureaucratic changes.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#6 lore raw

Re: [PATCH v11 2/2] tpm: add backend for mssim

Quoted 77 lines · Daniel P. Berrangé
Quoted 74 lines · James Bottomley

The Microsoft Simulator (mssim) is the reference emulation platform for the TCG TPM 2.0 specification.

https://github.com/Microsoft/ms-tpm-20-ref.git

It exports a fairly simple network socket based protocol on two sockets, one for command (default 2321) and one for control (default
2322).  This patch adds a simple backend that can speak the mssim protocol over the network.  It also allows the two sockets to be specified on the command line.  The benefits are twofold: firstly it
gives us a backend that actually speaks a standard TPM emulation protocol instead of the linux specific TPM driver format of the current emulated TPM backend and secondly, using the microsoft protocol, the end point of the emulator can be anywhere on the network, facilitating the cloud use case where a central TPM service
can be used over a control network.

The implementation does basic control commands like power off/on, but
doesn't implement cancellation or startup.  The former because cancellation is pretty much useless on a fast operating TPM emulator
and the latter because this emulator is designed to be used with OVMF
which itself does TPM startup and I wanted to validate that.

To run this, simply download an emulator based on the MS specification
(package ibmswtpm2 on openSUSE) and run it, then add these two lines
to the qemu command and it will use the emulator.

    -tpmdev mssim,id=tpm0 \
    -device tpm-crb,tpmdev=tpm0 \

to use a remote emulator replace the first line with

    -tpmdev "{'type':'mssim','id':'tpm0','command':{'type':inet,'host':'remote' ,'port':'2321'}}"

tpm-tis also works as the backend.

Signed-off-by: James Bottomley
<[email protected]>

---

v2: convert to SocketAddr json and use qio_channel_socket_connect_sync()
v3: gate control power off by migration state keep control socket disconnected
    to test outside influence and add docs.
v7: TPMmssim -> TPMMssim; doc and json fixes
    Make command socket open each time (makes OS debugging easier)
v11: add startup method to make sure TPM is reset on reboot
---
 MAINTAINERS              |   6 +
 backends/tpm/Kconfig     |   5 + backends/tpm/meson.build |   1 + backends/tpm/tpm_mssim.c | 335
+++++++++++++++++++++++++++++++++++++++
 backends/tpm/tpm_mssim.h |  44 +++++ docs/specs/tpm.rst       |  39 +++++
 qapi/tpm.json            |  31 +++- system/tpm-hmp-cmds.c    |   9 ++

 8 files changed, 466 insertions(+), 4 deletions(-)

 create mode 100644 backends/tpm/tpm_mssim.c
 create mode 100644 backends/tpm/tpm_mssim.h

Quoted 14 lines

backends/tpm/tpm_mssim.c line 5

Quoted 11 lines
backends/tpm/tpm_mssim.c new file +5 -0
new file mode 100644
index 0000000000..8f105fc924
@@ -0,0 +1,335 @@
+/*
+ * Emulator TPM driver which connects over the mssim protocol
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * Copyright (c) 2022

Copyright by whom ? I presume yourself, but I wouldn't normally assume the 'Author' line applies to the Copyright line.

I'll fix up this one (and all the others below) but:

[...]

Quoted 11 lines
Quoted 11 lines
backends/tpm/tpm_mssim.h new file +4 -0
new file mode 100644
index 0000000000..397474e4f6
@@ -0,0 +1,44 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * The code below is copied from the Microsoft/TCG Reference

implementation

Quoted 8 lines
Quoted 5 lines

+ *
+ *  https://github.com/Microsoft/ms-tpm-20-ref.git
+ *
+ * In file TPMCmd/Simulator/include/TpmTcpProtocol.h
+ */

That file has a volumous copyright header that I would expect to be preserved here.

Actually, the file itself has no copyright header at all (seems to be standard practice for all header files in the repository). Did you want me to paste the copyright lines from the LICENSE file; i.e.

Copyright 2010-2022 Microsoft Corporation
Copyright 2022-2024 Trusted Computing Group and its contributors

?

James

#7 lore raw

Re: [PATCH v11 2/2] tpm: add backend for mssim

Quoted 116 lines · James Bottomley
Quoted 77 lines · Daniel P. Berrangé
Quoted 74 lines · James Bottomley

The Microsoft Simulator (mssim) is the reference emulation platform for the TCG TPM 2.0 specification.

https://github.com/Microsoft/ms-tpm-20-ref.git

It exports a fairly simple network socket based protocol on two sockets, one for command (default 2321) and one for control (default
2322).  This patch adds a simple backend that can speak the mssim protocol over the network.  It also allows the two sockets to be specified on the command line.  The benefits are twofold: firstly it
gives us a backend that actually speaks a standard TPM emulation protocol instead of the linux specific TPM driver format of the current emulated TPM backend and secondly, using the microsoft protocol, the end point of the emulator can be anywhere on the network, facilitating the cloud use case where a central TPM service
can be used over a control network.

The implementation does basic control commands like power off/on, but
doesn't implement cancellation or startup.  The former because cancellation is pretty much useless on a fast operating TPM emulator
and the latter because this emulator is designed to be used with OVMF
which itself does TPM startup and I wanted to validate that.

To run this, simply download an emulator based on the MS specification
(package ibmswtpm2 on openSUSE) and run it, then add these two lines
to the qemu command and it will use the emulator.

    -tpmdev mssim,id=tpm0 \
    -device tpm-crb,tpmdev=tpm0 \

to use a remote emulator replace the first line with

    -tpmdev "{'type':'mssim','id':'tpm0','command':{'type':inet,'host':'remote' ,'port':'2321'}}"

tpm-tis also works as the backend.

Signed-off-by: James Bottomley
<[email protected]>

---

v2: convert to SocketAddr json and use qio_channel_socket_connect_sync()
v3: gate control power off by migration state keep control socket disconnected
    to test outside influence and add docs.
v7: TPMmssim -> TPMMssim; doc and json fixes
    Make command socket open each time (makes OS debugging easier)
v11: add startup method to make sure TPM is reset on reboot
---
 MAINTAINERS              |   6 +
 backends/tpm/Kconfig     |   5 + backends/tpm/meson.build |   1 + backends/tpm/tpm_mssim.c | 335
+++++++++++++++++++++++++++++++++++++++
 backends/tpm/tpm_mssim.h |  44 +++++ docs/specs/tpm.rst       |  39 +++++
 qapi/tpm.json            |  31 +++- system/tpm-hmp-cmds.c    |   9 ++

 8 files changed, 466 insertions(+), 4 deletions(-)

 create mode 100644 backends/tpm/tpm_mssim.c
 create mode 100644 backends/tpm/tpm_mssim.h

Quoted 14 lines

backends/tpm/tpm_mssim.c line 5

Quoted 11 lines
backends/tpm/tpm_mssim.c new file +5 -0
new file mode 100644
index 0000000000..8f105fc924
@@ -0,0 +1,335 @@
+/*
+ * Emulator TPM driver which connects over the mssim protocol
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * Copyright (c) 2022

Copyright by whom ? I presume yourself, but I wouldn't normally assume the 'Author' line applies to the Copyright line.

I'll fix up this one (and all the others below) but:

[...]

Quoted 11 lines
Quoted 11 lines
backends/tpm/tpm_mssim.h new file +4 -0
new file mode 100644
index 0000000000..397474e4f6
@@ -0,0 +1,44 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * The code below is copied from the Microsoft/TCG Reference

implementation

Quoted 8 lines
Quoted 5 lines

+ *
+ *  https://github.com/Microsoft/ms-tpm-20-ref.git
+ *
+ * In file TPMCmd/Simulator/include/TpmTcpProtocol.h
+ */

That file has a volumous copyright header that I would expect to be preserved here.

Quoted 3 lines

Actually, the file itself has no copyright header at all (seems to be standard practice for all header files in the repository). Did you want me to paste the copyright lines from the LICENSE file; i.e.

Oh, my local checkout was out of date. A commit in Oct last year purged all the copyright headers that the files previously had:

https://github.com/microsoft/ms-tpm-20-ref/blob/482aaafc0523bb94de203c4975f72f0230becec4/TPMCmd/Simulator/include/TpmTcpProtocol.h

Quoted 2 lines

Copyright 2010-2022 Microsoft Corporation
Copyright 2022-2024 Trusted Computing Group and its contributors

Yes, I think adding those, would be sufficient.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#8 lore raw

Re: [PATCH v11 2/2] tpm: add backend for mssim

In reply to James Bottomley

Hi James,

Quoted 61 lines · James Bottomley

The Microsoft Simulator (mssim) is the reference emulation platform for the TCG TPM 2.0 specification.

https://github.com/Microsoft/ms-tpm-20-ref.git

It exports a fairly simple network socket based protocol on two sockets, one for command (default 2321) and one for control (default 2322). This patch adds a simple backend that can speak the mssim protocol over the network. It also allows the two sockets to be specified on the command line. The benefits are twofold: firstly it gives us a backend that actually speaks a standard TPM emulation protocol instead of the linux specific TPM driver format of the current emulated TPM backend and secondly, using the microsoft protocol, the end point of the emulator can be anywhere on the network, facilitating the cloud use case where a central TPM service can be used over a control network.

The implementation does basic control commands like power off/on, but doesn't implement cancellation or startup. The former because cancellation is pretty much useless on a fast operating TPM emulator and the latter because this emulator is designed to be used with OVMF which itself does TPM startup and I wanted to validate that.

To run this, simply download an emulator based on the MS specification (package ibmswtpm2 on openSUSE) and run it, then add these two lines to the qemu command and it will use the emulator.

     -tpmdev mssim,id=tpm0 \
     -device tpm-crb,tpmdev=tpm0 \

to use a remote emulator replace the first line with

     -tpmdev "{'type':'mssim','id':'tpm0','command':{'type':inet,'host':'remote','port':'2321'}}"

tpm-tis also works as the backend.

---

v2: convert to SocketAddr json and use qio_channel_socket_connect_sync()
v3: gate control power off by migration state keep control socket disconnected
to test outside influence and add docs.
v7: TPMmssim -> TPMMssim; doc and json fixes
Make command socket open each time (makes OS debugging easier)
v11: add startup method to make sure TPM is reset on reboot

---
  MAINTAINERS              |   6 +
  backends/tpm/Kconfig     |   5 +
  backends/tpm/meson.build |   1 +
  backends/tpm/tpm_mssim.c | 335 +++++++++++++++++++++++++++++++++++++++
  backends/tpm/tpm_mssim.h |  44 +++++
  docs/specs/tpm.rst       |  39 +++++
  qapi/tpm.json            |  31 +++-
  system/tpm-hmp-cmds.c    |   9 ++
  8 files changed, 466 insertions(+), 4 deletions(-)

create mode 100644 backends/tpm/tpm_mssim.c
create mode 100644 backends/tpm/tpm_mssim.h

backends/tpm/tpm_mssim.c line 49

Quoted 10 lines
backends/tpm/tpm_mssim.c new file +10 -0
+static int tpm_send_ctrl(TPMMssim *t, uint32_t cmd, Error **errp)
+{
+    int ret, retc;
+    Error *local_err = NULL;
+
+    ret = qio_channel_socket_connect_sync(t->ctrl_qc, t->opts.control, errp);
+    if (ret != 0) {
+        return ret;
+    }
+    cmd = htonl(cmd);

I'd rather not use <arpa/inet.h> here, but:

       cpu_to_be32s(&cmd);
Quoted 33 lines
backends/tpm/tpm_mssim.c new file +31 -0
+    ret = qio_channel_write_all(QIO_CHANNEL(t->ctrl_qc),
+                                (char *)&cmd, sizeof(cmd), errp);
+    if (ret != 0) {
+        goto out;
+    }
+
+    ret = qio_channel_read_all(QIO_CHANNEL(t->ctrl_qc),
+                               (char *)&cmd, sizeof(cmd), errp);
+    if (ret != 0) {
+        goto out;
+    }
+    if (cmd != 0) {
+        error_setg(errp, ERROR_PREFIX
+                   "Incorrect ACK recieved on control channel 0x%x", cmd);
+        ret = -1;
+    }
+ out:
+    /*
+     * need to close the channel here, but if that fails report it
+     * while not letting a prior failure get overwritten
+     */
+    retc = qio_channel_close(QIO_CHANNEL(t->ctrl_qc), &local_err);
+    error_propagate(errp, local_err);
+    return retc ? retc : ret;
+}
 
 
+static void tpm_mssim_handle_request(TPMBackend *tb, TPMBackendCmd *cmd,
+                                     Error **errp)
+{
+    TPMMssim *t = TPM_MSSIM(tb);
+    uint32_t header, len;
+    uint8_t locality = cmd->locty;

backends/tpm/tpm_mssim.c line 140

Quoted 10 lines
backends/tpm/tpm_mssim.c new file +10 -0
+    struct iovec iov[4];
+    int ret;
+
+    ret = qio_channel_socket_connect_sync(t->cmd_qc, t->opts.command, errp);
+    if (ret != 0) {
+        goto fail_msg;
+    }
+
+    header = htonl(TPM_SEND_COMMAND);
+    len = htonl(cmd->in_len);
       cpu_to_be32()
Quoted 11 lines
backends/tpm/tpm_mssim.c new file +11 -0
+    iov[0].iov_base = &header;
+    iov[0].iov_len = sizeof(header);
+    iov[1].iov_base = &locality;
+    iov[1].iov_len = sizeof(locality);
+    iov[2].iov_base = &len;
+    iov[2].iov_len = sizeof(len);
+    iov[3].iov_base = (void *)cmd->in;
+    iov[3].iov_len = cmd->in_len;
+
+    ret = qio_channel_writev_all(QIO_CHANNEL(t->cmd_qc), iov, 4, errp);
+    if (ret != 0) {

backends/tpm/tpm_mssim.c line 162

Quoted 10 lines
backends/tpm/tpm_mssim.c new file +10 -0
+        goto fail;
+    }
+
+    ret = qio_channel_read_all(QIO_CHANNEL(t->cmd_qc),
+                               (char *)&len, sizeof(len), errp);
+    if (ret != 0) {
+        goto fail;
+    }
+
+    len = ntohl(len);
       be32_to_cpu()
Quoted 42 lines
backends/tpm/tpm_mssim.c new file +40 -0
+    if (len > cmd->out_len) {
+        error_setg(errp, "receive size is too large");
+        goto fail;
+    }
+    ret = qio_channel_read_all(QIO_CHANNEL(t->cmd_qc),
+                               (char *)cmd->out, len, errp);
+    if (ret != 0) {
+        goto fail;
+    }
+
+    /* ACK packet */
+    ret = qio_channel_read_all(QIO_CHANNEL(t->cmd_qc),
+                               (char *)&header, sizeof(header), errp);
+    if (ret != 0) {
+        goto fail;
+    }
+    if (header != 0) {
+        error_setg(errp, "incorrect ACK received on command channel 0x%x", len);
+        goto fail;
+    }
+
+    ret = qio_channel_close(QIO_CHANNEL(t->cmd_qc), errp);
+    if (ret != 0) {
+        goto fail_msg;
+    }
+
+    return;
+
+ fail:
+    /* we're already failing, so don't worry if this fails too */
+    qio_channel_close(QIO_CHANNEL(t->cmd_qc), NULL);
+ fail_msg:
+    error_prepend(errp, ERROR_PREFIX);
+    tpm_util_write_fatal_error_response(cmd->out, cmd->out_len);
+}
 
 
+static const TypeInfo tpm_mssim_info = {
+    .name = TYPE_TPM_MSSIM,
+    .parent = TYPE_TPM_BACKEND,
+    .instance_size = sizeof(TPMMssim),
+    .class_init = tpm_mssim_class_init,

backends/tpm/tpm_mssim.c

Quoted 10 lines
backends/tpm/tpm_mssim.c new file +10 -0
+    .instance_init = tpm_mssim_instance_init,
+    .instance_finalize = tpm_mssim_instance_finalize,
+};
+
+static void tpm_mssim_register(void)
+{
+    type_register_static(&tpm_mssim_info);
+}
+
+type_init(tpm_mssim_register)

Preferably DEFINE_TYPES().

Otherwise (modulo Daniel comments), LGTM!

Regards,

Phil.