TPM NVRAM persistent storage
v2 cover TPM NVRAM persistent storage
This patch series provides persistent storage support that a TPM can use to store NVRAM data. It uses QEMU's block driver to store data on a drive image. The libtpms TPM 1.2 backend will be the initial user of this functionality to store data that must persist through a reboot or migration. A sample command line may look like this:
qemu-system-x86_64 ...
-drive file=/path/to/nvram.qcow2,id=drive-nvram0-0-0 -tpmdev libtpms,id=tpm-tpm0
-device tpm-tis,tpmdev=tpm-tpm0,id=tpm0,drive=drive-nvram0-0-0
Thanks,
Corey
Corey Bryant (3):
nvram: Add TPM NVRAM implementation
nvram: Add tpm-tis drive support
TPM NVRAM test
hw/tpm/Makefile.objs | 1 + hw/tpm/tpm_int.h | 2 + hw/tpm/tpm_nvram.c | 326 ++++++++++++++++++++++++++++++++++++++++++++++ hw/tpm/tpm_nvram.h | 25 ++++ hw/tpm/tpm_passthrough.c | 85 ++++++++++++ hw/tpm/tpm_tis.c | 8 + 6 files changed, 447 insertions(+), 0 deletions(-) create mode 100644 hw/tpm/tpm_nvram.c create mode 100644 hw/tpm/tpm_nvram.h
PATCH v2 1/3 nvram: Add TPM NVRAM implementation
Provides TPM NVRAM implementation that enables storing of TPM NVRAM data in a persistent image file. The block driver is used to read/write the drive image. This will enable, for example, an encrypted QCOW2 image to be used to store sensitive keys.
This patch provides APIs that a TPM backend can use to read and write data.
---
v2
-Use non bit-rotting DPRINTF ([email protected])
-Use DIV_ROUND_UP ([email protected])
-Use bdrv_pread/bdrv_pwrite in coroutine - causes global
sector to byte I/O changes ([email protected], [email protected])
-Add tpm_nvram_required_size_kb() and update tpm_nvram_adjust_size() -Drop qemu_aio_wait() from tpm_nvram_do_co_read/write() and move
any completion code path to coroutines
-Replace tpm_nvram_rwrequest_free() with g_free(rwr) -Remove unneeded arg checks from tpm_nvram_read/write()
([email protected])
-Init rwr->rc to -EINPROGRESS and wrap qemu_cond_wait() with check
([email protected])
-Add init of completion_mutex and completion condition
--- hw/tpm/Makefile.objs | 1 + hw/tpm/tpm_nvram.c | 326 ++++++++++++++++++++++++++++++++++++++++++++++++++ hw/tpm/tpm_nvram.h | 25 ++++ 3 files changed, 352 insertions(+), 0 deletions(-) create mode 100644 hw/tpm/tpm_nvram.c create mode 100644 hw/tpm/tpm_nvram.h
hw/tpm/Makefile.objs +1 -0
@@ -1,2 +1,3 @@ common-obj-$(CONFIG_TPM_TIS) += tpm_tis.o +common-obj-$(CONFIG_TPM_TIS) += tpm_nvram.o common-obj-$(CONFIG_TPM_PASSTHROUGH) += tpm_passthrough.o
hw/tpm/tpm_nvram.c new file +326 -0
326 lines not on this page · raw message
hw/tpm/tpm_nvram.h new file +25 -0
@@ -0,0 +1,25 @@ +/* +* TPM NVRAM - enables storage of persistent NVRAM data on an image file +* +* Copyright (C) 2013 IBM Corporation +* +* Authors: +* Stefan Berger <[email protected]> +* Corey Bryant <[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 TPM_TPM_NVRAM_H +#define TPM_TPM_NVRAM_H + +#include "block/block.h" + +int tpm_nvram_init (BlockDriverState *bdrv); +int tpm_nvram_read (BlockDriverState *bdrv,uint64_t offset, +uint8_t **blob,uint32_t size); +int tpm_nvram_write (BlockDriverState *bdrv,uint64_t offset, +uint8_t *blob,uint32_t size); + +#endif
Signature · 1 line
1.7.1
Re: [Qemu-devel] [PATCH v2 1/3] nvram: Add TPM NVRAM implementation
Quoted 9 lines · Corey Bryant
hw/tpm/tpm_nvram.c new file +9
-0+/* +* Coroutine that reads a blob from the drive asynchronously +*/ +static void coroutine_fn tpm_nvram_co_read(void *opaque) +{ +TPMNvramRWRequest *rwr = opaque; + + *rwr->blob_r =g_malloc (rwr->size); +
hw/tpm/tpm_nvram.c line 105
Quoted 10 lines
hw/tpm/tpm_nvram.c new file +10
-0+ rwr->rc =bdrv_pread (rwr->bdrv, + rwr->offset, + *rwr->blob_r, + rwr->size); +if (rwr->rc != rwr->size) { +g_free (*rwr->blob_r); + *rwr->blob_r =NULL ; + } + +qemu_mutex_lock (&rwr->completion_mutex);
Race condition: we must only store rwr->rc while holding ->completion_mutex. Otherwise the other thread may see ->rc and g_free(rwr) before we leave this function, causing us to operate on freed memory.
I suggest storing rc into a local variable first and then assigning to rwr->rc here.
hw/tpm/tpm_nvram.c line 150
Quoted 13 lines
hw/tpm/tpm_nvram.c new file +13
-0+/* +* Enter a coroutine to write a blob to the drive +*/ +static void tpm_nvram_do_co_write (TPMNvramRWRequest *rwr) +{ +int rc; +Coroutine *co; + + rc =tpm_nvram_adjust_size (rwr->bdrv, rwr->offset, rwr->size); +if (rc <0 ) { + rwr->rc = rc; +return ; + }
Do this inside the tpm_nvram_co_write() coroutine so error return still signals the condvar. Right now the other thread may miss completion and deadlock.
Re: [Qemu-devel] [PATCH v2 1/3] nvram: Add TPM NVRAM implementation
Quoted 38 lines · Stefan Hajnoczi
Quoted 9 lines · Corey Bryant
hw/tpm/tpm_nvram.c new file +9
-0+/* +* Coroutine that reads a blob from the drive asynchronously +*/ +static void coroutine_fn tpm_nvram_co_read(void *opaque) +{ +TPMNvramRWRequest *rwr = opaque; + + *rwr->blob_r =g_malloc (rwr->size); +hw/tpm/tpm_nvram.c line 105
Quoted 10 lines
hw/tpm/tpm_nvram.c new file +10
-0+ rwr->rc =bdrv_pread (rwr->bdrv, + rwr->offset, + *rwr->blob_r, + rwr->size); +if (rwr->rc != rwr->size) { +g_free (*rwr->blob_r); + *rwr->blob_r =NULL ; + } + +qemu_mutex_lock (&rwr->completion_mutex);Race condition: we must only store
rwr->rcwhile holding ->completion_mutex. Otherwise the other thread may see ->rc andg_free(rwr)before we leave this function, causing us to operate on freed memory.I suggest storing rc into a local variable first and then assigning to
rwr->rchere.Quoted 9 lines
hw/tpm/tpm_nvram.c new file +9
-0+/* +* Enter a coroutine to write a blob to the drive +*/ +static void tpm_nvram_do_co_write (TPMNvramRWRequest *rwr) +{ +int rc; +Coroutine *co; + + rc =tpm_nvram_adjust_size (rwr->bdrv, rwr->offset, rwr->size);
Quoted 10 lines
hw/tpm/tpm_nvram.c line 150
Quoted 4 lines
hw/tpm/tpm_nvram.c new file +4
-0+if (rc <0 ) { + rwr->rc = rc; +return ; + }Do this inside the
tpm_nvram_co_write()coroutine so error return still signals the condvar. Right now the other thread may miss completion and deadlock.
Thanks for the review. Sending a v3 out to the list now.
Signature · 2 lines
Regards, Corey Bryant
PATCH v2 2/3 nvram: Add tpm-tis drive support
In reply to Corey BryantAdd a drive property to the tpm-tis device and initialize the TPM NVRAM if a drive is specified.
---
v2
-No changes
--- hw/tpm/tpm_int.h | 2 ++ hw/tpm/tpm_tis.c | 8 ++++++++ 2 files changed, 10 insertions(+), 0 deletions(-)
hw/tpm/tpm_int.h +2 -0
@@ -29,6 +29,8 @@ struct TPMState {char *backend;TPMBackend *be_driver; + +BlockDriverState *bdrv; };#define TPM (obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS)
hw/tpm/tpm_tis.c +8 -0
@@ -27,6 +27,7 @@#include "hw/i386/pc.h" #include "hw/pci/pci_ids.h" #include "tpm_tis.h" +#include "tpm_nvram.h" #include "qemu-common.h" /*#define DEBUG_TIS */
@@ -849,6 +850,7 @@ static Property tpm_tis_properties[] = {DEFINE_PROP_UINT32 ("irq" , TPMState, s.tis.irq_num,TPM_TIS_IRQ ),DEFINE_PROP_STRING ("tpmdev" , TPMState, backend), +DEFINE_PROP_DRIVE ("drive" , TPMState, bdrv), DEFINE_PROP_END_OF_LIST(), };
@@ -864,6 +866,12 @@ static void tpm_tis_realizefn(DeviceState *dev, Error **errp)return ; } +if (s->bdrv &&tpm_nvram_init (s->bdrv)) { +error_setg (errp,"tpm_tis: backend drive with id %s could not " +"initialize TPM NVRAM drive" , s->backend); +return ; + } + s->be_driver->fe_model =TPM_MODEL_TPM_TIS ;if (tpm_backend_init (s->be_driver, s, tpm_tis_receive_cb)) {
Signature · 1 line
1.7.1