QEMU Trusted Platform Module (TPM) integration
A message that is not in the archive (sent off-list or not yet received): [email protected]
Anthony Liguori
AppliedApplied. Thanks.
Regards,
Anthony Liguori
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [Qemu-devel] [PATCH V27 1/7] Support for TPM command line options
I missed this one, because it wasn't cc'ed to QMP maintainers, the subject mentions only command line, not QMP, and even the body talks only about the human monitor command, not QMP. Noticed it only when git-pull touched qapi-schema.json. Please try harder to help Luiz and me keep track of QMP changes.
I gave the QMP interface and its documentation a look-over now. It's just a look-over, because passthrough requires a box with TPM enabled, which I don't have handy, so I can't test anything.
A few comments inline.
Quoted 324 lines · Stefan Berger
This patch adds support for TPM command line options. The command line options supported here are
./qemu-... -tpmdev passthrough,path=<path to TPM device>,id=<id>
-device tpm-tis,tpmdev=<id>,id=<other id>and
./qemu-... -tpmdev help
where the latter works similar to -soundhw help and shows a list of available TPM backends (for example 'passthrough').
Using the type parameter, the backend is chosen, i.e., 'passthrough' for the passthrough driver. The interpretation of the other parameters along with determining whether enough parameters were provided is pushed into the backend driver, which needs to implement the interface function 'create' and return a TPMDriverOpts structure if the VM can be started or 'NULL' if not enough or bad parameters were provided.
Monitor support for 'info tpm' has been added. It for example prints the following:
(qemu) info tpm
TPM devices:
tpm0: model=tpm-tis
\ tpm0: type=passthrough,path=/dev/tpm0,cancel-path=/sys/devices/pnp0/00:09/cancelSigned-off-by Stefan Berger <[email protected]>Reviewed-by Corey Bryant <[email protected]>--- Makefile.objs | 1 + hmp-commands.hx | 2 + hmp.c | 44 +++++++ hmp.h | 1 + include/tpm/tpm.h | 21 ++++ monitor.c | 8 ++ qapi-schema.json | 104 +++++++++++++++++ qemu-options.hx | 33 ++++++ qmp-commands.hx | 18 +++ tpm/Makefile.objs | 1 + tpm/tpm.c | 343 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ tpm/tpm_int.h | 83 +++++++++++++ tpm/tpm_tis.h | 80 +++++++++++++ vl.c | 37 ++++++ 14 files changed, 776 insertions(+) create mode 100644 include/tpm/tpm.h create mode 100644 tpm/Makefile.objs create mode 100644 tpm/tpm.c create mode 100644 tpm/tpm_int.h create mode 100644 tpm/tpm_tis.hMakefile.objs +1
-0@@ -73,6 +73,7 @@ common-obj-y += bt-host.o bt-vhci.o common-obj-y += dma-helpers.o common-obj-y += vl.o +common-obj-y += tpm/ common-obj-$(CONFIG_SLIRP) += slirp/hmp-commands.hx +2
-0@@ -1642,6 +1642,8 @@ show device tree show qdev device model list @item info roms show roms +@item info tpm +show the TPM device @end table ETEXIhmp.c +44
-0@@ -607,6 +607,50 @@ void hmp_info_block_jobs(Monitor *mon, const QDict *qdict) } } +void hmp_info_tpm (Monitor *mon,const QDict *qdict) +{ +TPMInfoList *info_list, *info; +Error *err =NULL ; +unsigned int c =0 ; +TPMPassthroughOptions *tpo; + + info_list =qmp_query_tpm (&err); +if (err) { +monitor_printf (mon,"TPM device not supported\n" ); +error_free (err); +return ; + } + +if (info_list) { +monitor_printf (mon,"TPM device:\n" ); + } + +for (info = info_list; info; info = info->next) { +TPMInfo *ti = info->value; +monitor_printf (mon," tpm%d: model=%s\n" , + c, TpmModel_lookup[ti->model]); + +monitor_printf (mon," \\ %s: type=%s" , + ti->id, TpmType_lookup[ti->type]); + +switch (ti->tpm_options->kind) { +case TPM_TYPE_OPTIONS_KIND_TPM_PASSTHROUGH_OPTIONS : + tpo = ti->tpm_options->tpm_passthrough_options; +monitor_printf (mon,"%s%s%s%s" , + tpo->has_path ?",path=" :"" , + tpo->has_path ? tpo->path :"" , + tpo->has_cancel_path ?",cancel-path=" :"" , + tpo->has_cancel_path ? tpo->cancel_path :"" ); +break ; +case TPM_TYPE_OPTIONS_KIND_MAX : +break ; + } +monitor_printf (mon,"\n" ); + c++; + } +qapi_free_TPMInfoList (info_list); +} +void hmp_quit (Monitor *mon,const QDict *qdict) {monitor_suspend (mon);hmp.h +1
-0@@ -36,6 +36,7 @@ void hmp_info_spice(Monitor *mon, const QDict *qdict);void hmp_info_balloon (Monitor *mon,const QDict *qdict);void hmp_info_pci (Monitor *mon,const QDict *qdict);void hmp_info_block_jobs (Monitor *mon,const QDict *qdict); +void hmp_info_tpm (Monitor *mon,const QDict *qdict);void hmp_quit (Monitor *mon,const QDict *qdict);void hmp_stop (Monitor *mon,const QDict *qdict);void hmp_system_reset (Monitor *mon,const QDict *qdict);include/tpm/tpm.h new file +21
-0@@ -0,0 +1,21 @@ +/* +* Public TPM functions +* +* Copyright (C) 2011-2013 IBM Corporation +* +* Authors: +* Stefan Berger <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ +#ifndef QEMU_TPM_H +#define QEMU_TPM_H + +#include "qemu/option.h" + +int tpm_config_parse (QemuOptsList *opts_list,const char *optarg); +int tpm_init (void ); +void tpm_cleanup (void ); + +#endif /* QEMU_TPM_H */ monitor.c +8
-0@@ -47,6 +47,7 @@#include "migration/migration.h" #include "sysemu/kvm.h" #include "qemu/acl.h" +#include "tpm/tpm.h" #include "qapi/qmp/qint.h" #include "qapi/qmp/qfloat.h" #include "qapi/qmp/qlist.h" @@ -2722,6 +2723,13 @@ static mon_cmd_t info_cmds[] = { .mhandler.cmd = do_trace_print_events, }, { + .name = "tpm", + .args_type = "", + .params = "", + .help = "show the TPM device", + .mhandler.cmd = hmp_info_tpm, + }, + { .name = NULL, }, };qapi-schema.json +104
-0@@ -3240,3 +3240,107 @@ # Since: 1.4 ## { 'command': 'chardev-remove', 'data': {'id': 'str'} } + +## +# @TpmModel: +# +# An enumeration of TPM models +# +# @tpm-tis: TPM TIS model +# +# Since: 1.5 +## +{ 'enum': 'TpmModel', 'data': [ 'tpm-tis' ] } + +## +# @query-tpm-models: +# +# Return a list of supported TPM models +# +# Returns: a list of TpmModel +# +# Since: 1.5 +## +{ 'command': 'query-tpm-models', 'returns': ['TpmModel'] } + +## +# @TpmType: +# +# An enumeration of TPM types +# +# @passthrough: TPM passthrough type +# +# Since: 1.5 +## +{ 'enum': 'TpmType', 'data': [ 'passthrough' ] } + +## +# @query-tpm-types: +# +# Return a list of supported TPM types +# +# Returns: a list of TpmType +# +# Since: 1.5 +## +{ 'command': 'query-tpm-types', 'returns': ['TpmType'] } + +## +# @TPMPassthroughOptions: +# +# Information about the TPM passthrough type +# +# @path: #optional string describing the path used for accessing the TPM device +# +# @cancel-path: #optional string showing the TPM's sysfs cancel file +# for cancellation of TPM commands while they are executing +# +# Since: 1.5 +## +{ 'type': 'TPMPassthroughOptions', 'data': { '*path' : 'str', + '*cancel-path' : 'str'} } + +## +# @TpmTypeOptions: +# +# A union referencing different TPM backend types' configuration options +# +# @tpm-passthough-options: TPMPassthroughOptions describing the TPM +# passthrough configuration options +# +# Since: 1.5 +## +{ 'union': 'TpmTypeOptions', + 'data': { 'tpm-passthrough-options' : 'TPMPassthroughOptions' } } + +## +# @TpmInfo: +# +# Information about the TPM +# +# @id: The Id of the TPM +# +# @model: The TPM frontend model +# +# @type: The TPM (backend) type being used +# +# @tpm-options: The TPM (backend) type configuration options +# +# Since: 1.5 +## +{ 'type': 'TPMInfo', + 'data': {'id': 'str', + 'model': 'TpmModel', + 'type': 'TpmType', + 'tpm-options': 'TpmTypeOptions' } } + +## +# @query-tpm: +# +# Return information about the TPM device +# +# Returns: @TPMInfo on success +# +# Since: 1.5 +## +{ 'command': 'query-tpm', 'returns': ['TPMInfo'] }qemu-options.hx +7
-0@@ -2218,6 +2218,39 @@ STEXI ETEXI DEFHEADING() +#ifdef CONFIG_TPM +DEFHEADING(TPM device options:) + +DEF("tpmdev", HAS_ARG, QEMU_OPTION_tpmdev, \ + "-tpmdev [<type>],id=str[,option][,option][,...]\n", + QEMU_ARCH_ALL) +STEXI
qemu-options.hx STEXI · line 2237
Quoted 10 lines
qemu-options.hx +10
-0+ +The general form of a TPM device option is: +@table @option + +@item -tpmdev @var{backend} ,id=@var{id} [,@var{options}] +@findex -tpmdev +Backend type must be: + +The specific backend type will determine the applicable options. +The @code{-tpmdev} option requires a @code{-device} option.
You mean -tpmdev creates just a backend, so for a usable device you also need to create a frontend with -device?
Quoted 37 lines
qemu-options.hx +15
-0+Options to each backend are described below. + +Use 'help' to print all available TPM backend types. +@example +qemu -tpmdev help +@end example + +@end table + +ETEXI + +DEFHEADING() + +#endif + DEFHEADING(Linux/Multiboot boot specific:) STEXIqmp-commands.hx +11
-0@@ -2716,6 +2716,24 @@ EQMP }, { + .name = "query-tpm", + .args_type = "", + .mhandler.cmd_new = qmp_marshal_input_query_tpm, + }, + + { + .name = "query-tpm-models", + .args_type = "", + .mhandler.cmd_new = qmp_marshal_input_query_tpm_models, + }, +
qmp-commands.hx EQMP · line 2739
Quoted 10 lines
qmp-commands.hx +7
-0+ { + .name = "query-tpm-types", + .args_type = "", + .mhandler.cmd_new = qmp_marshal_input_query_tpm_types, + }, + + { .name = "chardev-add", .args_type = "id:s,backend:q", .mhandler.cmd_new = qmp_marshal_input_chardev_add,
You imitated the bad examples that lack documentation instead the good ones that have it. Please fix that in a followup patch.
Each command definition should roughly like this:
{
.name = "query-tpm-models",
.args_type = "",
.mhandler.cmd_new = qmp_marshal_input_query_tpm_models,
},
SQMP
query-tpm-models
----------------
Show available TPM models.
Arguments: None
Example:
-> { "execute": "query-tpm-models" }
<- { "return": [ "tpm-tis" ] }
EQMPThis is particularly important for commands with complex replies such as query-tpm.
[...]
Quoted 19 lines
tpm/tpm.c new file +13
-0@@ -0,0 +1,343 @@ +/* +* TPM configuration +* +* Copyright (C) 2011-2013 IBM Corporation +* +* Authors: +* Stefan Berger <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +* Based on net.c +*/
[...]
Quoted 16 lines
tpm/tpm.c new file +16
-0+/* +* Walk the list of TPM backend drivers that are in use and call their +* destroy function to have them cleaned up. +*/ +void tpm_cleanup (void ) +{ +TPMBackend *drv, *next; + + QLIST_FOREACH_SAFE(drv, &tpm_backends, list, next) { +QLIST_REMOVE (drv, list); + drv->ops->destroy (drv); + } +} + +/* +* Initialize the TPM. Process the tpmdev command line options describing the
tpm/tpm.c
Quoted 10 lines
tpm/tpm.c new file +10
-0+* TPM backend. +*/ +int tpm_init (void ) +{ +if (qemu_opts_foreach (qemu_find_opts ("tpmdev" ), + tpm_init_tpmdev,NULL ,1 ) !=0 ) { +return -1 ; + } + +atexit (tpm_cleanup);
Routine atexit() question: what happens when the program terminates abnormally? atext() callbacks don't run then. Impact of not doing cleanup on the system?
Quoted 3 lines
tpm/tpm.c new file +3
-0+ +return 0 ; +}
[...]
Re: [Qemu-devel] [PATCH V27 1/7] Support for TPM command line options
Quoted 5 lines · Markus Armbruster
I missed this one, because it wasn't cc'ed to QMP maintainers, the subject mentions only command line, not QMP, and even the body talks only about the human monitor command, not QMP. Noticed it only when git-pull touched qapi-schema.json. Please try harder to help Luiz and me keep track of QMP changes.
I apologize for that.
Quoted 15 lines
I gave the QMP interface and its documentation a look-over now. It's just a look-over, because passthrough requires a box with TPM enabled, which I don't have handy, so I can't test anything.
A few comments inline.
Stefan Berger <[email protected]> writes:
(quoted diff) +7
-0+ +@item -tpmdev @var{backend} ,id=@var{id} [,@var{options}] +@findex -tpmdev +Backend type must be: + +The specific backend type will determine the applicable options. +The @code{-tpmdev} option requires a @code{-device} option.
Quoted 2 lines
You mean -tpmdev creates just a backend, so for a usable device you also need to create a frontend with -device?
Yes, just like any other device.
{
+ .name = "query-tpm",
+ .args_type = "",
+ .mhandler.cmd_new = qmp_marshal_input_query_tpm,
+ },
+
+ {
+ .name = "query-tpm-models",
+ .args_type = "",
+ .mhandler.cmd_new = qmp_marshal_input_query_tpm_models,
+ },
+
+ {
+ .name = "query-tpm-types",
+ .args_type = "",
+ .mhandler.cmd_new = qmp_marshal_input_query_tpm_types,+ },
+
+ {
.name = "chardev-add",
.args_type = "id:s,backend:q", .mhandler.cmd_new = qmp_marshal_input_chardev_add,
Quoted 2 lines
You imitated the bad examples that lack documentation instead the good ones that have it. Please fix that in a followup patch.
Will do by sending it to the list and cc'ing you.
+{
+ if (qemu_opts_foreach(qemu_find_opts("tpmdev"),
+ tpm_init_tpmdev, NULL, 1) != 0) {
+ return -1;
+ }
+
+ atexit(tpm_cleanup);
Quoted 3 lines
Routine
atexit()question: what happens when the program terminates abnormally?atext()callbacks don't run then. Impact of not doing cleanup on the system?
In case the TPM is currently not operating on a command there will be no impact. In case the TPM is operating on a command, it will hold the thread inside /dev/tpm0 until the command has finished. The solution here is to write a byte into sysfs file to terminate the TPM from further executing the command and return; this code exists and is being invoked in all other cases than abnormal termination obviously.
What other choices do we have? Do we need a signal handler that runs on SIGSEGV and provide a registration function for those functions that need to run on abnormal termination?
Stefan
Re: [Qemu-devel] [PATCH V27 1/7] Support for TPM command line options
Quoted 16 lines · Stefan Berger
Quoted 5 lines · Markus Armbruster
I missed this one, because it wasn't cc'ed to QMP maintainers, the subject mentions only command line, not QMP, and even the body talks only about the human monitor command, not QMP. Noticed it only when git-pull touched qapi-schema.json. Please try harder to help Luiz and me keep track of QMP changes.
I apologize for that.
Quoted 7 lines
I gave the QMP interface and its documentation a look-over now. It's just a look-over, because passthrough requires a box with TPM enabled, which I don't have handy, so I can't test anything.
A few comments inline.
Quoted 12 lines
Quoted 10 lines
Stefan Berger <[email protected]> writes:
(quoted diff) +7
-0+ +@item -tpmdev @var{backend} ,id=@var{id} [,@var{options}] +@findex -tpmdev +Backend type must be: + +The specific backend type will determine the applicable options. +The @code{-tpmdev} option requires a @code{-device} option.You mean -tpmdev creates just a backend, so for a usable device you also need to create a frontend with -device?
Yes, just like any other device.
Okay. Corey already posted a patch to clarify the documentation here. Appreciated!
[...]
Quoted 12 lines
(quoted diff) +7
-0+{ + if (qemu_opts_foreach(qemu_find_opts("tpmdev"), + tpm_init_tpmdev, NULL, 1) != 0) { + return -1; + } + + atexit(tpm_cleanup);Quoted 4 lines
Routine
atexit()question: what happens when the program terminates abnormally?atext()callbacks don't run then. Impact of not doing cleanup on the system?
Quoted 6 lines
In case the TPM is currently not operating on a command there will be no impact. In case the TPM is operating on a command, it will hold the thread inside
/dev/tpm0until the command has finished. The solution here is to write a byte into sysfs file to terminate the TPM from further executing the command and return; this code exists and is being invoked in all other cases than abnormal termination obviously.
Unlike other kernel resources such as file descriptors, this one isn't tied to a process and cleaned up automatically on process termination. Correct?
Therefore, we try to clean it up manually. Actual cleanup code is tpm_passthrough_cancel_cmd(). Correct?
The only existing cleanup function tpm_passthrough_destroy() does a whole lot more. Just to make sure I understand what's going on:
everything but tpm_passthrough_cancel_cmd() is effectively a no-op there, correct?
Quoted 3 lines
What other choices do we have? Do we need a signal handler that runs on SIGSEGV and provide a registration function for those functions that need to run on abnormal termination?
Not worth it.
Re: [Qemu-devel] [PATCH V27 1/7] Support for TPM command line options
Quoted 10 lines · Markus Armbruster
Quoted 7 lines
In case the TPM is currently not operating on a command there will be no impact. In case the TPM is operating on a command, it will hold the thread inside
/dev/tpm0until the command has finished. The solution here is to write a byte into sysfs file to terminate the TPM from further executing the command and return; this code exists and is being invoked in all other cases than abnormal termination obviously.Unlike other kernel resources such as file descriptors, this one isn't tied to a process and cleaned up automatically on process termination. Correct?
No, it's also a file descriptor and will be cleaned up upon process termination.
Quoted 3 lines
Therefore, we try to clean it up manually. Actual cleanup code is
tpm_passthrough_cancel_cmd(). Correct?
No, tpm_passthrough_cancel_cmd() sends a byte to the TPM's sysfs entry for canceling an ongoing command. QEMU has a thread that sends the commands to the TPM via write() and gets the responses in read(). The issue being the thread is held inside the write() (!) until the command has completed inside the host TPM. Inside the passthrough driver we set a flag while the command is executing and if a command is executing when for example QEMU is reset, then the sysfs entry is written to to get the thread out of the write() due to termination of the ongoing TPM command. Depending on which version of Fedora is run, the termination of the command may or may not get invoked. F14 for example does not seem to care about waiting until all user-level processes have properly shut down and would reset while F16 seems to wait for all processes to properly terminate. The cancellation of an ongoing command may be initiated inside the guest as well and will also cause tpm_passthrough_cancel_cmd() to be invoked if a command is executed by the host TPM on behalf of the guest.
There were/are(?) some issues with cancellation of ongoing commands. Different vendors seem to set different bits to indicate to the driver as to when a command has been canceled. The specs are not clear enough in that area. We fixed this for the TPMs that we have access (to wake up the thread inside the write()).
Quoted 5 lines
The only existing cleanup function
tpm_passthrough_destroy()does a whole lot more. Just to make sure I understand what's going on:
everything buttpm_passthrough_cancel_cmd()is effectively a no-op there, correct?
It depends on what you mean with no-op. In tpm_passthrough_destroy() a possibly ongoing command is canceled, then the thread is signaled to terminate and join()ed. The rest is closing of file descriptors and freeing of memory.
Stefan
Re: [Qemu-devel] [PATCH V27 1/7] Support for TPM command line options
Quoted 8 lines · Stefan Berger
Quoted 7 lines · Markus Armbruster
Quoted 7 lines
In case the TPM is currently not operating on a command there will be no impact. In case the TPM is operating on a command, it will hold the thread inside
/dev/tpm0until the command has finished. The solution here is to write a byte into sysfs file to terminate the TPM from further executing the command and return; this code exists and is being invoked in all other cases than abnormal termination obviously.
Quoted 6 lines
Quoted 3 lines
Unlike other kernel resources such as file descriptors, this one isn't tied to a process and cleaned up automatically on process termination. Correct?
No, it's also a file descriptor and will be cleaned up upon process termination.
Closing the file descriptor does *not* free kernel resources associated with it?!?
Quoted 8 lines
Quoted 2 lines
Therefore, we try to clean it up manually. Actual cleanup code is
tpm_passthrough_cancel_cmd(). Correct?No,
tpm_passthrough_cancel_cmd()sends a byte to the TPM's sysfs entry for canceling an ongoing command. QEMU has a thread that sends the commands to the TPM viawrite()and gets the responses inread(). The issue being the thread is held inside thewrite()(!) until the command has completed inside the host TPM.
You mean write() blocks?
Quoted 5 lines
Inside the passthrough
driver we set a flag while the command is executing and if a command is executing when for example QEMU is reset, then the sysfs entry is written to to get the thread out of thewrite()due to termination of the ongoing TPM command.
I can understand a need to abort the write() on guest reset, but why on QEMU exit()? Shouldn't process termination abort the write() just fine?
Quoted 2 lines
Depending on which version of Fedora is run,
the termination of the command may or may not get invoked.
May or may not get invoked on what condition?
Quoted 4 lines
F14 forexample does not seem to care about waiting until all user-level processes have properly shut down and would reset while F16 seems to wait for all processes to properly terminate.
F14 is more than nine months past it's shelf date. Why should we support it when its makers don't?
Quoted 4 lines
The cancellation of an ongoing command may be initiated inside the guest as well and will also cause
tpm_passthrough_cancel_cmd()to be invoked if a command is executed by the host TPM on behalf of the guest.
Passing through the guest's cancellation makes sense to me.
Quoted 5 lines
There were/are(?) some issues with cancellation of ongoing commands. Different vendors seem to set different bits to indicate to the driver as to when a command has been canceled. The specs are not clear enough in that area. We fixed this for the TPMs that we have access (to wake up the thread inside the
write()).
I'm not doubting the need to cancel commands. I'm just trying to understand why we do it on exit().
Quoted 9 lines
Quoted 4 lines
The only existing cleanup function
tpm_passthrough_destroy()does a whole lot more. Just to make sure I understand what's going on:
everything buttpm_passthrough_cancel_cmd()is effectively a no-op there, correct?It depends on what you mean with no-op. In
tpm_passthrough_destroy()a possibly ongoing command is canceled, then the thread is signaled to terminate and join()ed. The rest is closing of file descriptors and freeing of memory.
Closing file descriptors and freeing memory on exit() is a no-op at best, and a source of bugs otherwise. The kernel already cleans these up just fine.
Unless the thread does something interesting in response to its termination signal, making it terminate is also a no-op. I can't see anything interesting happening, please correct me if I'm wrong.
Re: [Qemu-devel] [PATCH V27 1/7] Support for TPM command line options
Quoted 9 lines · Markus Armbruster
Quoted 7 lines · Stefan Berger
Quoted 6 lines · Markus Armbruster
Quoted 6 lines
In case the TPM is currently not operating on a command there will be no impact. In case the TPM is operating on a command, it will hold the thread inside
/dev/tpm0until the command has finished. The solution here is to write a byte into sysfs file to terminate the TPM from further executing the command and return; this code exists and is being invoked in all other cases than abnormal termination obviously.
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Unlike other kernel resources such as file descriptors, this one isn't tied to a process and cleaned up automatically on process termination. Correct?
No, it's also a file descriptor and will be cleaned up upon process termination.
Closing the file descriptor does *not* free kernel resources associated with it?!?
That's not what I said. You suggested "this one isn't tied to a process and cleaned up automatically on process termination", which is not true but the problem lies somewhere else.
Quoted 9 lines
Quoted 8 lines
Quoted 3 lines
Therefore, we try to clean it up manually. Actual cleanup code is
tpm_passthrough_cancel_cmd(). Correct?No,
tpm_passthrough_cancel_cmd()sends a byte to the TPM's sysfs entry for canceling an ongoing command. QEMU has a thread that sends the commands to the TPM viawrite()and gets the responses inread(). The issue being the thread is held inside thewrite()(!) until the command has completed inside the host TPM.You mean
write()blocks?
Yep.
Quoted 8 lines
Quoted 6 lines
Inside the passthrough
driver we set a flag while the command is executing and if a command is executing when for example QEMU is reset, then the sysfs entry is written to to get the thread out of thewrite()due to termination of the ongoing TPM command.I can understand a need to abort the
write()on guest reset, but why on QEMUexit()? Shouldn't process termination abort thewrite()just fine?
Nope, process termination won't get you out of write(). Even if this was to be changed today in the kernel driver, the old kernels are out there for a while to be.
Quoted 4 lines
Quoted 3 lines
Depending on which version of Fedora is run,
the termination of the command may or may not get invoked.May or may not get invoked on what condition?
Behavior of the OS upon shutdown itself. Whether it waits for all processes to terminate or not.
Quoted 6 lines
Quoted 4 lines
F14 forexample does not seem to care about waiting until all user-level processes have properly shut down and would reset while F16 seems to wait for all processes to properly terminate.
F14 is more than nine months past it's shelf date. Why should we support it when its makers don't?
F14 is just an example of one OS that can be run just happens to be old by by now; there may be others that behave the same way.
Quoted 6 lines
Quoted 4 lines
The cancellation of an ongoing command may be initiated inside the guest as well and will also cause
tpm_passthrough_cancel_cmd()to be invoked if a command is executed by the host TPM on behalf of the guest.Passing through the guest's cancellation makes sense to me.
Quoted 7 lines
Quoted 5 lines
There were/are(?) some issues with cancellation of ongoing commands. Different vendors seem to set different bits to indicate to the driver as to when a command has been canceled. The specs are not clear enough in that area. We fixed this for the TPMs that we have access (to wake up the thread inside the
write()).I'm not doubting the need to cancel commands. I'm just trying to understand why we do it on
exit().
I can write an OS that sends a command to the TPM and then shuts down without waiting for the program to terminate or the even the response from the TPM to come back . In this case we would need to cancel the command on exit()l.
Quoted 9 lines
Quoted 9 lines
Quoted 5 lines
The only existing cleanup function
tpm_passthrough_destroy()does a whole lot more. Just to make sure I understand what's going on:
everything buttpm_passthrough_cancel_cmd()is effectively a no-op there, correct?It depends on what you mean with no-op. In
tpm_passthrough_destroy()a possibly ongoing command is canceled, then the thread is signaled to terminate and join()ed. The rest is closing of file descriptors and freeing of memory.
Quoted 8 lines
Closing file descriptors and freeing memory on
exit()is a no-op at best, and a source of bugs otherwise. The kernel already cleans these up just fine.Unless the thread does something interesting in response to its termination signal, making it terminate is also a no-op. I can't see anything interesting happening, please correct me if I'm wrong.
Per what you are saying only no-ops are happening.
Stefan
Re: [Qemu-devel] [PATCH V27 1/7] Support for TPM command line options
Quoted 13 lines · Stefan Berger
Quoted 12 lines · Markus Armbruster
Quoted 10 lines · Stefan Berger
Quoted 9 lines · Markus Armbruster
Quoted 6 lines
In case the TPM is currently not operating on a command there will be no impact. In case the TPM is operating on a command, it will hold the thread inside
/dev/tpm0until the command has finished. The solution here is to write a byte into sysfs file to terminate the TPM from further executing the command and return; this code exists and is being invoked in all other cases than abnormal termination obviously.Unlike other kernel resources such as file descriptors, this one isn't tied to a process and cleaned up automatically on process termination. Correct?
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
No, it's also a file descriptor and will be cleaned up upon process termination.
Closing the file descriptor does *not* free kernel resources associated with it?!?
That's not what I said. You suggested "this one isn't tied to a process and cleaned up automatically on process termination", which is not true but the problem lies somewhere else.
Well, if there's kernel action needed beyond the automatic closing of file descriptors on process termination, there's something weird going on. I'm trying to understand what. Please bear with me.
Quoted 10 lines
Quoted 8 lines
Quoted 7 lines
Quoted 2 lines
Therefore, we try to clean it up manually. Actual cleanup code is
tpm_passthrough_cancel_cmd(). Correct?No,
tpm_passthrough_cancel_cmd()sends a byte to the TPM's sysfs entry for canceling an ongoing command. QEMU has a thread that sends the commands to the TPM viawrite()and gets the responses inread(). The issue being the thread is held inside thewrite()(!) until the command has completed inside the host TPM.You mean
write()blocks?Yep.
Got it.
Quoted 11 lines
Quoted 7 lines
Quoted 5 lines
Inside the passthrough
driver we set a flag while the command is executing and if a command is executing when for example QEMU is reset, then the sysfs entry is written to to get the thread out of thewrite()due to termination of the ongoing TPM command.I can understand a need to abort the
write()on guest reset, but why on QEMUexit()? Shouldn't process termination abort thewrite()just fine?Nope, process termination won't get you out of
write(). Even if this was to be changed today in the kernel driver, the old kernels are out there for a while to be.
Now I'm confused. Are you telling me that exit() hangs until the thread currently blocked in write() to TPM is made runnable by the completion of the TPM command?
Quoted 6 lines
Quoted 3 lines
Quoted 2 lines
Depending on which version of Fedora is run,
the termination of the command may or may not get invoked.May or may not get invoked on what condition?
Behavior of the OS upon shutdown itself. Whether it waits for all processes to terminate or not.
I'm getting more confused.
Quoted 21 lines
Quoted 6 lines
Quoted 4 lines
F14 forexample does not seem to care about waiting until all user-level processes have properly shut down and would reset while F16 seems to wait for all processes to properly terminate.
F14 is more than nine months past it's shelf date. Why should we support it when its makers don't?
F14 is just an example of one OS that can be run just happens to be old by by now; there may be others that behave the same way.
Quoted 11 lines
Quoted 4 lines
The cancellation of an ongoing command may be initiated inside the guest as well and will also cause
tpm_passthrough_cancel_cmd()to be invoked if a command is executed by the host TPM on behalf of the guest.Passing through the guest's cancellation makes sense to me.
Quoted 5 lines
There were/are(?) some issues with cancellation of ongoing commands. Different vendors seem to set different bits to indicate to the driver as to when a command has been canceled. The specs are not clear enough in that area. We fixed this for the TPMs that we have access (to wake up the thread inside the
write()).
Quoted 7 lines
Quoted 2 lines
I'm not doubting the need to cancel commands. I'm just trying to understand why we do it on
exit().I can write an OS that sends a command to the TPM and then shuts down without waiting for the program to terminate or the even the response from the TPM to come back . In this case we would need to cancel the command on exit()l.
Aha, now we're getting somewhere.
Are you talking about the following scenario?
* Guest sends TPM command, QEMU passes it through, QEMU thread blocks in write() to TPM.
* Guest powers down, QEMU is configured to terminate on powerdown, calls exit().
* Unless you explicitly cancel the TPM command, $BAD_THINGS can happen.
If yes, please describe at least one instance of $BAD_THINGS for me again.
Quoted 12 lines
Quoted 12 lines
Quoted 8 lines
Quoted 4 lines
The only existing cleanup function
tpm_passthrough_destroy()does a whole lot more. Just to make sure I understand what's going on:
everything buttpm_passthrough_cancel_cmd()is effectively a no-op there, correct?It depends on what you mean with no-op. In
tpm_passthrough_destroy()a possibly ongoing command is canceled, then the thread is signaled to terminate and join()ed. The rest is closing of file descriptors and freeing of memory.Closing file descriptors and freeing memory on
exit()is a no-op at best, and a source of bugs otherwise. The kernel already cleans these up just fine.
Quoted 6 lines
Quoted 4 lines
Unless the thread does something interesting in response to its termination signal, making it terminate is also a no-op. I can't see anything interesting happening, please correct me if I'm wrong.
Per what you are saying only no-ops are happening.
Thanks.
Re: [Qemu-devel] [PATCH V27 1/7] Support for TPM command line options
Quoted 14 lines · Markus Armbruster
Quoted 5 lines · Stefan Berger
I can write an OS that sends a command to the TPM and then shuts down without waiting for the program to terminate or the even the response from the TPM to come back . In this case we would need to cancel the command on exit()l.
Aha, now we're getting somewhere.
Are you talking about the following scenario?
* Guest sends TPM command, QEMU passes it through, QEMU thread blocks in
write()to TPM.
Quoted 7 lines
* Guest powers down, QEMU is configured to terminate on powerdown, calls
exit().* Unless you explicitly cancel the TPM command, $BAD_THINGS can happen.
If yes, please describe at least one instance of $BAD_THINGS for me again.
BAD_THINGS = If a TPM command is executing as in the scenario you are describing, the thread will still be caught in the write(), thus the QEMU process cannot terminate and will be stuck in the thread-join as it is implemented now. If there was no thread-join QEMU would probably linger around as a zombie for a while.
Stefan
Re: [Qemu-devel] [PATCH V27 1/7] Support for TPM command line options
In reply to Markus ArmbrusterQuoted 426 lines · Markus Armbruster
I missed this one, because it wasn't cc'ed to QMP maintainers, the subject mentions only command line, not QMP, and even the body talks only about the human monitor command, not QMP. Noticed it only when git-pull touched qapi-schema.json. Please try harder to help Luiz and me keep track of QMP changes.
I gave the QMP interface and its documentation a look-over now. It's just a look-over, because passthrough requires a box with TPM enabled, which I don't have handy, so I can't test anything.
A few comments inline.
Quoted 324 lines · Stefan Berger
This patch adds support for TPM command line options. The command line options supported here are
./qemu-... -tpmdev passthrough,path=<path to TPM device>,id=<id>
-device tpm-tis,tpmdev=<id>,id=<other id>and
./qemu-... -tpmdev help
where the latter works similar to -soundhw help and shows a list of available TPM backends (for example 'passthrough').
Using the type parameter, the backend is chosen, i.e., 'passthrough' for the passthrough driver. The interpretation of the other parameters along with determining whether enough parameters were provided is pushed into the backend driver, which needs to implement the interface function 'create' and return a TPMDriverOpts structure if the VM can be started or 'NULL' if not enough or bad parameters were provided.
Monitor support for 'info tpm' has been added. It for example prints the following:
(qemu) info tpm
TPM devices:
tpm0: model=tpm-tis
\ tpm0: type=passthrough,path=/dev/tpm0,cancel-path=/sys/devices/pnp0/00:09/cancelSigned-off-by Stefan Berger <[email protected]>Reviewed-by Corey Bryant <[email protected]>--- Makefile.objs | 1 + hmp-commands.hx | 2 + hmp.c | 44 +++++++ hmp.h | 1 + include/tpm/tpm.h | 21 ++++ monitor.c | 8 ++ qapi-schema.json | 104 +++++++++++++++++ qemu-options.hx | 33 ++++++ qmp-commands.hx | 18 +++ tpm/Makefile.objs | 1 + tpm/tpm.c | 343 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ tpm/tpm_int.h | 83 +++++++++++++ tpm/tpm_tis.h | 80 +++++++++++++ vl.c | 37 ++++++ 14 files changed, 776 insertions(+)create mode 100644
include/tpm/tpm.h
create mode 100644 tpm/Makefile.objs
create mode 100644tpm/tpm.c
create mode 100644tpm/tpm_int.h
create mode 100644tpm/tpm_tis.hMakefile.objs +1
-0@@ -73,6 +73,7 @@ common-obj-y += bt-host.o bt-vhci.o common-obj-y += dma-helpers.o common-obj-y += vl.o +common-obj-y += tpm/ common-obj-$(CONFIG_SLIRP) += slirp/hmp-commands.hx +2
-0@@ -1642,6 +1642,8 @@ show device tree show qdev device model list @item info roms show roms +@item info tpm +show the TPM device @end table ETEXIhmp.c +44
-0@@ -607,6 +607,50 @@ void hmp_info_block_jobs(Monitor *mon, const QDict *qdict) } } +void hmp_info_tpm (Monitor *mon,const QDict *qdict) +{ +TPMInfoList *info_list, *info; +Error *err =NULL ; +unsigned int c =0 ; +TPMPassthroughOptions *tpo; + + info_list =qmp_query_tpm (&err); +if (err) { +monitor_printf (mon,"TPM device not supported\n" ); +error_free (err); +return ; + } + +if (info_list) { +monitor_printf (mon,"TPM device:\n" ); + } + +for (info = info_list; info; info = info->next) { +TPMInfo *ti = info->value; +monitor_printf (mon," tpm%d: model=%s\n" , + c, TpmModel_lookup[ti->model]); + +monitor_printf (mon," \\ %s: type=%s" , + ti->id, TpmType_lookup[ti->type]); + +switch (ti->tpm_options->kind) { +case TPM_TYPE_OPTIONS_KIND_TPM_PASSTHROUGH_OPTIONS : + tpo = ti->tpm_options->tpm_passthrough_options; +monitor_printf (mon,"%s%s%s%s" , + tpo->has_path ?",path=" :"" , + tpo->has_path ? tpo->path :"" , + tpo->has_cancel_path ?",cancel-path=" :"" , + tpo->has_cancel_path ? tpo->cancel_path :"" ); +break ; +case TPM_TYPE_OPTIONS_KIND_MAX : +break ; + } +monitor_printf (mon,"\n" ); + c++; + } +qapi_free_TPMInfoList (info_list); +} +void hmp_quit (Monitor *mon,const QDict *qdict) {monitor_suspend (mon);hmp.h +1
-0@@ -36,6 +36,7 @@ void hmp_info_spice(Monitor *mon, const QDict *qdict);void hmp_info_balloon (Monitor *mon,const QDict *qdict);void hmp_info_pci (Monitor *mon,const QDict *qdict);void hmp_info_block_jobs (Monitor *mon,const QDict *qdict); +void hmp_info_tpm (Monitor *mon,const QDict *qdict);void hmp_quit (Monitor *mon,const QDict *qdict);void hmp_stop (Monitor *mon,const QDict *qdict);void hmp_system_reset (Monitor *mon,const QDict *qdict);include/tpm/tpm.h new file +21
-0@@ -0,0 +1,21 @@ +/* +* Public TPM functions +* +* Copyright (C) 2011-2013 IBM Corporation +* +* Authors: +* Stefan Berger <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ +#ifndef QEMU_TPM_H +#define QEMU_TPM_H + +#include "qemu/option.h" + +int tpm_config_parse (QemuOptsList *opts_list,const char *optarg); +int tpm_init (void ); +void tpm_cleanup (void ); + +#endif /* QEMU_TPM_H */ monitor.c +8
-0@@ -47,6 +47,7 @@#include "migration/migration.h" #include "sysemu/kvm.h" #include "qemu/acl.h" +#include "tpm/tpm.h" #include "qapi/qmp/qint.h" #include "qapi/qmp/qfloat.h" #include "qapi/qmp/qlist.h" @@ -2722,6 +2723,13 @@ static mon_cmd_t info_cmds[] = { .mhandler.cmd = do_trace_print_events, }, { + .name = "tpm", + .args_type = "", + .params = "", + .help = "show the TPM device", + .mhandler.cmd = hmp_info_tpm, + }, + { .name = NULL, }, };qapi-schema.json +104
-0@@ -3240,3 +3240,107 @@ # Since: 1.4 ## { 'command': 'chardev-remove', 'data': {'id': 'str'} } + +## +# @TpmModel: +# +# An enumeration of TPM models +# +# @tpm-tis: TPM TIS model +# +# Since: 1.5 +## +{ 'enum': 'TpmModel', 'data': [ 'tpm-tis' ] } + +## +# @query-tpm-models: +# +# Return a list of supported TPM models +# +# Returns: a list of TpmModel +# +# Since: 1.5 +## +{ 'command': 'query-tpm-models', 'returns': ['TpmModel'] } + +## +# @TpmType: +# +# An enumeration of TPM types +# +# @passthrough: TPM passthrough type +# +# Since: 1.5 +## +{ 'enum': 'TpmType', 'data': [ 'passthrough' ] } + +## +# @query-tpm-types: +# +# Return a list of supported TPM types +# +# Returns: a list of TpmType +# +# Since: 1.5 +## +{ 'command': 'query-tpm-types', 'returns': ['TpmType'] } + +## +# @TPMPassthroughOptions: +# +# Information about the TPM passthrough type +# +# @path: #optional string describing the path used for accessing the TPM device +# +# @cancel-path: #optional string showing the TPM's sysfs cancel file +# for cancellation of TPM commands while they are executing +# +# Since: 1.5 +## +{ 'type': 'TPMPassthroughOptions', 'data': { '*path' : 'str', + '*cancel-path' : 'str'} } + +## +# @TpmTypeOptions: +# +# A union referencing different TPM backend types' configuration options +# +# @tpm-passthough-options: TPMPassthroughOptions describing the TPM +# passthrough configuration options +# +# Since: 1.5 +## +{ 'union': 'TpmTypeOptions', + 'data': { 'tpm-passthrough-options' : 'TPMPassthroughOptions' } } + +## +# @TpmInfo: +# +# Information about the TPM +# +# @id: The Id of the TPM +# +# @model: The TPM frontend model +# +# @type: The TPM (backend) type being used +# +# @tpm-options: The TPM (backend) type configuration options +# +# Since: 1.5 +## +{ 'type': 'TPMInfo', + 'data': {'id': 'str', + 'model': 'TpmModel', + 'type': 'TpmType', + 'tpm-options': 'TpmTypeOptions' } } + +## +# @query-tpm: +# +# Return information about the TPM device +# +# Returns: @TPMInfo on success +# +# Since: 1.5 +## +{ 'command': 'query-tpm', 'returns': ['TPMInfo'] }qemu-options.hx +7
-0@@ -2218,6 +2218,39 @@ STEXI ETEXI DEFHEADING() +#ifdef CONFIG_TPM +DEFHEADING(TPM device options:) + +DEF("tpmdev", HAS_ARG, QEMU_OPTION_tpmdev, \ + "-tpmdev [<type>],id=str[,option][,option][,...]\n", + QEMU_ARCH_ALL) +STEXIqemu-options.hx STEXI · line 2237
Quoted 10 lines
qemu-options.hx +10
-0+ +The general form of a TPM device option is: +@table @option + +@item -tpmdev @var{backend} ,id=@var{id} [,@var{options}] +@findex -tpmdev +Backend type must be: + +The specific backend type will determine the applicable options. +The @code{-tpmdev} option requires a @code{-device} option.You mean -tpmdev creates just a backend, so for a usable device you also need to create a frontend with -device?
Quoted 37 lines
qemu-options.hx +15
-0+Options to each backend are described below. + +Use 'help' to print all available TPM backend types. +@example +qemu -tpmdev help +@end example + +@end table + +ETEXI + +DEFHEADING() + +#endif + DEFHEADING(Linux/Multiboot boot specific:) STEXIqmp-commands.hx +11
-0@@ -2716,6 +2716,24 @@ EQMP }, { + .name = "query-tpm", + .args_type = "", + .mhandler.cmd_new = qmp_marshal_input_query_tpm, + }, + + { + .name = "query-tpm-models", + .args_type = "", + .mhandler.cmd_new = qmp_marshal_input_query_tpm_models, + }, +qmp-commands.hx EQMP · line 2739
Quoted 10 lines
qmp-commands.hx +7
-0+ { + .name = "query-tpm-types", + .args_type = "", + .mhandler.cmd_new = qmp_marshal_input_query_tpm_types, + }, + + { .name = "chardev-add", .args_type = "id:s,backend:q", .mhandler.cmd_new = qmp_marshal_input_chardev_add,You imitated the bad examples that lack documentation instead the good ones that have it. Please fix that in a followup patch.
Each command definition should roughly like this:
{ .name = "query-tpm-models", .args_type = "", .mhandler.cmd_new = qmp_marshal_input_query_tpm_models, }, SQMP query-tpm-models ---------------- Show available TPM models. Arguments: None Example: -> { "execute": "query-tpm-models" } <- { "return": [ "tpm-tis" ] } EQMP
Quoted 3 lines
This is particularly important for commands with complex replies such as query-tpm.
I'll submit a patch for this and the other issue you mentioned above.
Quoted 43 lines
[...]
Quoted 19 lines
tpm/tpm.c new file +13
-0@@ -0,0 +1,343 @@ +/* +* TPM configuration +* +* Copyright (C) 2011-2013 IBM Corporation +* +* Authors: +* Stefan Berger <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +* Based on net.c +*/ [...]
Quoted 22 lines
tpm/tpm.c new file +22
-0+/* +* Walk the list of TPM backend drivers that are in use and call their +* destroy function to have them cleaned up. +*/ +void tpm_cleanup (void ) +{ +TPMBackend *drv, *next; + + QLIST_FOREACH_SAFE(drv, &tpm_backends, list, next) { +QLIST_REMOVE (drv, list); + drv->ops->destroy (drv); + } +} + +/* +* Initialize the TPM. Process the tpmdev command line options describing the +* TPM backend. +*/ +int tpm_init (void ) +{ +if (qemu_opts_foreach (qemu_find_opts ("tpmdev" ), + tpm_init_tpmdev,NULL ,1 ) !=0 ) {
Quoted 9 lines
tpm/tpm.c
Quoted 4 lines
tpm/tpm.c new file +4
-0+return -1 ; + } + +atexit (tpm_cleanup);Routine
atexit()question: what happens when the program terminates abnormally?atext()callbacks don't run then. Impact of not doing cleanup on the system?
The qemu resource cleanup should be ok (freeing storage, closing files, freeing thread pools). I'm not sure what the implications would be to the host TPM state if qemu crashes. Perhaps there'd be a TPM command running at the time of the crash that would need to be cancelled through the sysfs interace. Stefan?
Quoted 7 lines
Quoted 3 lines
tpm/tpm.c new file +3
-0+ +return 0 ; +}[...]
Signature · 2 lines
Regards, Corey Bryant