Patchwork [V5,07/12] Implementation of the libtpms-based backend

login
register
mail settings
Submitter Stefan Berger
Date May 20, 2011, 3:42 p.m.
Message ID <20110520154303.336944152@linux.vnet.ibm.com>
Download mbox | patch
Permalink /patch/96633/
State New
Headers show

Comments

Stefan Berger - May 20, 2011, 3:42 p.m.
This patch provides the glue for the TPM TIS interface (frontend) to
the libtpms that provides the actual TPM functionality.

Some details:

This part of the patch provides support for the spawning of a thread
that will interact with the libtpms-based TPM. It expects a signal
from the frontend to wake and pick up the TPM command that is supposed
to be processed and delivers the response packet using a callback
function provided by the frontend.

The backend connects itself to the frontend by filling out an interface
structure with pointers to the function implementing support for various
operations.

In this part a structure with callback functions with is registered with
libtpms. Those callback functions mostly deal with persistent storage.

The libtpms-based backend implements functionality to write into a 
Qemu block storage device rather than to plain files. With that we
can support VM snapshotting and we also get the possibility to use
encrypted QCoW2 for free. Thanks to Anthony for pointing this out.
The storage part of the driver has been split off into its own patch.

v5:
  - check access() to TPM's state file and report error if file is not
    accessible

v3:
  - temporarily deactivate the building of the tpm_builtin.c until
    subsequent patch completely converts it to the libtpms based driver

v2:
  - fixes to adhere to the qemu coding style


Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>

---
 configure        |    1 
 hw/tpm_builtin.c |  422 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
 hw/tpm_tis.h     |   17 ++
 3 files changed, 419 insertions(+), 21 deletions(-)

Patch

Index: qemu-git/hw/tpm_tis.h
===================================================================
--- qemu-git.orig/hw/tpm_tis.h
+++ qemu-git/hw/tpm_tis.h
@@ -73,4 +73,21 @@  static inline void dumpBuffer(FILE *stre
     fprintf(stream, "\n");
 }
 
+static inline void clear_sized_buffer(TPMSizedBuffer *tpmsb)
+{
+    if (tpmsb->buffer) {
+       tpmsb->size = 0;
+       qemu_free(tpmsb->buffer);
+       tpmsb->buffer = NULL;
+    }
+}
+
+static inline void set_sized_buffer(TPMSizedBuffer *tpmsb,
+                                    uint8_t *buffer, uint32_t size)
+{
+    clear_sized_buffer(tpmsb);
+    tpmsb->size = size;
+    tpmsb->buffer = buffer;
+}
+
 #endif /* _HW_TPM_TIS_H */
Index: qemu-git/hw/tpm_builtin.c
===================================================================
--- qemu-git.orig/hw/tpm_builtin.c
+++ qemu-git/hw/tpm_builtin.c
@@ -1,5 +1,5 @@ 
 /*
- *  builtin 'null' TPM driver
+ *  builtin TPM driver based on libtpms
  *
  *  Copyright (c) 2010, 2011 IBM Corporation
  *  Copyright (c) 2010, 2011 Stefan Berger
@@ -18,17 +18,36 @@ 
  * License along with this library; if not, see <http://www.gnu.org/licenses/>
  */
 
+#include "blockdev.h"
+#include "block_int.h"
 #include "qemu-common.h"
 #include "tpm.h"
 #include "hw/hw.h"
 #include "hw/tpm_tis.h"
 #include "hw/pc.h"
+#include "migration.h"
+#include "sysemu.h"
+
+#include <libtpms/tpm_library.h>
+#include <libtpms/tpm_error.h>
+#include <libtpms/tpm_memory.h>
+#include <libtpms/tpm_nvfilename.h>
+#include <libtpms/tpm_tis.h>
+
+#include <zlib.h>
 
 
 //#define DEBUG_TPM
 //#define DEBUG_TPM_SR /* suspend - resume */
 
 
+#define SAVESTATE_TYPE 'S'
+#define PERMSTATE_TYPE 'P'
+#define VOLASTATE_TYPE 'V'
+
+#define VTPM_DRIVE  "drive-vtpm0-nvram"
+#define TPM_OPTS "id=" VTPM_DRIVE
+
 /* data structures */
 
 typedef struct ThreadParams {
@@ -44,12 +63,18 @@  static QemuThread thread;
 
 static QemuMutex state_mutex; /* protects *_state below */
 static QemuMutex tpm_initialized_mutex; /* protect tpm_initialized */
+static QemuCond bs_write_result_cond;
+static TPMSizedBuffer permanent_state = { .size = 0, .buffer = NULL, };
+static TPMSizedBuffer volatile_state  = { .size = 0, .buffer = NULL, };
+static TPMSizedBuffer save_state      = { .size = 0, .buffer = NULL, };
+static int pipefd[2] =  {-1, -1};
 
 static bool thread_terminate = false;
 static bool tpm_initialized = false;
 static bool had_fatal_error = false;
 static bool had_startup_error = false;
 static bool thread_running = false;
+static bool need_read_volatile = false;
 
 static ThreadParams tpm_thread_params;
 
@@ -63,9 +88,21 @@  static const unsigned char tpm_std_fatal
 static char dev_description[80];
 
 
+static int tpmlib_get_prop(enum TPMLIB_TPMProperty prop)
+{
+    int result;
+
+    TPM_RESULT res = TPMLIB_GetTPMProperty(prop, &result);
+
+    assert(res == TPM_SUCCESS);
+
+    return result;
+}
+
+
 static void *tpm_builtin_main_loop(void *d)
 {
-    int res = 1;
+    TPM_RESULT res;
     ThreadParams *thr_parms = d;
     uint32_t in_len, out_len;
     uint8_t *in, *out;
@@ -75,9 +112,10 @@  static void *tpm_builtin_main_loop(void 
     fprintf(stderr, "tpm: THREAD IS STARTING\n");
 #endif
 
-    if (res != 0) {
+    res = TPMLIB_MainInit();
+    if (res != TPM_SUCCESS) {
 #if defined DEBUG_TPM || defined DEBUG_TPM_SR
-        fprintf(stderr, "tpm: Error: TPM initialization failed (rc=%d)\n",
+        fprintf(stderr," tpm: Error: Call to TPMLIB_MainInit() failed (rc=%d)\n",
                 res);
 #endif
 	had_fatal_error = true;
@@ -95,6 +133,8 @@  static void *tpm_builtin_main_loop(void 
         in_len = 0;
         do {
 #ifdef DEBUG_TPM
+            /* TPMLIB output goes to stderr */
+            fflush(stdout);
             fprintf(stderr, "tpm: waiting for commands...\n");
 #endif
 
@@ -138,13 +178,19 @@  static void *tpm_builtin_main_loop(void 
 
                 resp_size = 0;
 
-                /* !!! Send command to TPM & wait for response */
+                /* TPMLIB_Process may realloc the response buffer */
+                res = TPMLIB_Process(
+                    &thr_parms->tpm_state->loc[g_locty].r_buffer.buffer,
+                    &resp_size, &out_len,
+                    in, in_len);
 
-                if (res != 0) {
 #ifdef DEBUG_TPM
-                    fprintf(stderr,
-                            "tpm: Sending/receiving TPM request/response "
-                            "failed.\n");
+                fflush(stdout);
+#endif
+
+                if (res != TPM_SUCCESS) {
+#ifdef DEBUG_TPM
+                    fprintf(stderr, "tpm: TPMLIB_Process() failed\n");
 #endif
                     had_fatal_error = 1;
                 }
@@ -172,11 +218,13 @@  static void *tpm_builtin_main_loop(void 
 
     if (tpm_initialized) {
         tpm_initialized = false;
+        TPMLIB_Terminate();
     }
 
     qemu_mutex_unlock(&tpm_initialized_mutex);
 
 #ifdef DEBUG_TPM
+    fflush(stdout);
     fprintf(stderr, "tpm: THREAD IS ENDING\n");
 #endif
 
@@ -207,7 +255,7 @@  static void tpm_builtin_terminate_tpm_th
          * still ask us to write data to the disk, though.
          */
         while (thread_running) {
-            /* !!! write data to disk if necessary */
+            tpm_builtin_fulfill_sync_to_bs_request(NULL);
             usleep(100000);
         }
 
@@ -219,6 +267,12 @@  static void tpm_builtin_terminate_tpm_th
 static void tpm_builtin_tpm_atexit(void)
 {
     tpm_builtin_terminate_tpm_thread();
+
+    close(pipefd[0]);
+    pipefd[0] = -1;
+
+    close(pipefd[1]);
+    pipefd[1] = -1;
 }
 
 
@@ -242,8 +296,17 @@  static int tpm_builtin_startup_tpm(void)
 }
 
 
-static int tpm_builtin_do_startup_tpm(void)
+static int tpm_builtin_do_startup_tpm(bool fail_on_encrypted_drive)
 {
+    int rc;
+
+    rc = startup_bs(bs, fail_on_encrypted_drive);
+    if (rc) {
+        return rc;
+    }
+
+    tpm_builtin_load_tpm_state_from_bs(bs);
+
     return tpm_builtin_startup_tpm();
 }
 
@@ -254,7 +317,7 @@  static int tpm_builtin_do_startup_tpm(vo
  */
 static int tpm_builtin_early_startup_tpm(void)
 {
-    return tpm_builtin_do_startup_tpm();
+    return tpm_builtin_do_startup_tpm(true);
 }
 
 
@@ -270,7 +333,7 @@  static int tpm_builtin_late_startup_tpm(
     had_fatal_error = false;
     had_startup_error = false;
 
-    if (tpm_builtin_do_startup_tpm()) {
+    if (tpm_builtin_do_startup_tpm(false)) {
         had_fatal_error = true;
     }
 
@@ -278,6 +341,213 @@  static int tpm_builtin_late_startup_tpm(
 }
 
 
+/*****************************************************************
+ * call back functions for the libtpms TPM library
+ ****************************************************************/
+static TPM_RESULT tpm_builtin_nvram_init(void)
+{
+    return TPM_SUCCESS;
+}
+
+
+static TPM_RESULT tpm_builtin_nvram_loaddata(unsigned char **data,
+                                   uint32_t *length,
+                                   size_t tpm_number __attribute__((unused)),
+                                   const char *name)
+{
+    TPM_RESULT rc = TPM_SUCCESS;
+
+#if defined DEBUG_TPM || defined DEBUG_TPM_SR
+    fprintf(stderr, "tpm: TPM_NVRAM_LoadData: tpm_number = %d, name = %s\n",
+            (int)tpm_number, name);
+#endif
+    *length = 0;
+
+    if (!strcmp(name, TPM_PERMANENT_ALL_NAME)) {
+        *length = permanent_state.size;
+
+        if (*length == 0) {
+            rc = TPM_RETRY;
+        } else {
+            /*
+             * keep the permanent state for
+             * as long as possible. We may
+             * be in a resume operation and only
+             * get the volatile state later on when
+             * Qemu provides the state.
+             * Once the volatile state is there,
+             * we can discard the permanent state,
+             * otherwise the perment state will be
+             * discarded in other places
+             */
+            if (volatile_state.size == 0) {
+                rc = TPM_Malloc(data, *length);
+                if (rc == 0)
+                    memcpy(*data, permanent_state.buffer, *length);
+            } else {
+                *data = permanent_state.buffer;
+
+                permanent_state.size = 0;
+                permanent_state.buffer = NULL;
+            }
+        }
+    } else if (!strcmp(name, TPM_VOLATILESTATE_NAME)) {
+        *length = volatile_state.size;
+        if (*length == 0) {
+            rc = TPM_RETRY;
+        } else {
+            *data = volatile_state.buffer;
+
+            volatile_state.size = 0;
+            volatile_state.buffer = NULL;
+        }
+    } else if (!strcmp(name, TPM_SAVESTATE_NAME)) {
+        *length = save_state.size;
+        if (*length == 0) {
+            rc = TPM_RETRY;
+        } else {
+            *data = save_state.buffer;
+            save_state.size = 0;
+            save_state.buffer = NULL;
+        }
+    }
+
+#if defined DEBUG_TPM || defined DEBUG_TPM_SR
+    fprintf(stderr, "tpm: Read %d bytes of state [crc=%08x]; rc = %d\n",
+            *length, (int)crc32(0, *data, *length), rc);
+#endif
+
+    return rc;
+}
+
+
+/*
+ * Called by the TPM when permanent data, savestate or volatile state
+ * is updated or needs to be saved.
+ * Primarily we care about savestate and permanent data here.
+ */
+static TPM_RESULT tpm_builtin_nvram_storedata(const unsigned char *data,
+                                   uint32_t length,
+                                   size_t tpm_number __attribute__((unused)),
+                                   const char *name)
+{
+    TPM_RESULT rc = TPM_SUCCESS;
+    char what;
+    TPMSizedBuffer *tsb = NULL;
+
+    if (incoming_expected) {
+#if defined DEBUG_TPM || defined DEBUG_TPM_SR
+        fprintf(stderr, "tpm: Not storing data due to incoming migration\n");
+#endif
+        return TPM_SUCCESS;
+    }
+
+    if (!strcmp(name, TPM_PERMANENT_ALL_NAME)) {
+        tsb = &permanent_state;
+#if defined DEBUG_TPM || defined DEBUG_TPM_SR
+        fprintf(stderr, "tpm: STORING %5d BYTES OF PERMANENT ALL  [crc=%08x]\n",
+                length, (int)crc32(0, data, length));
+#endif
+        what = PERMSTATE_TYPE;
+    } else if (!strcmp(name, TPM_SAVESTATE_NAME)) {
+        tsb = &save_state;
+#if defined DEBUG_TPM || defined DEBUG_TPM_SR
+        fprintf(stderr, "tpm: STORING %5d BYTES OF SAVESTATE      [crc=%08x]\n",
+                length, (int)crc32(0, data, length));
+#endif
+        what = SAVESTATE_TYPE;
+#if defined DEBUG_TPM || defined DEBUG_TPM_SR
+    } else if (!strcmp(name, TPM_VOLATILESTATE_NAME)) {
+        fprintf(stderr, "tpm: GOT     %5d BYTES OF VOLATILE STATE [crc=%08x]\n",
+                length, (int)crc32(0, data, length));
+#endif
+    }
+
+    if (tsb) {
+        qemu_mutex_lock(&state_mutex);
+
+        set_sized_buffer(tsb, (unsigned char *)data, length);
+
+        if (request_sync_to_bs(what)) {
+            rc = TPM_FAIL;
+        }
+
+        /* TPM library will free */
+        tsb->size = 0;
+        tsb->buffer = NULL;
+
+        qemu_mutex_unlock(&state_mutex);
+    }
+
+    if (had_fatal_error) {
+        rc = TPM_FAIL;
+    }
+
+    return rc;
+}
+
+
+static TPM_RESULT tpm_builtin_nvram_deletename(
+                                  size_t tpm_number __attribute__((unused)),
+                                  const char *name,
+                                  TPM_BOOL mustExist)
+{
+    TPM_RESULT rc = TPM_SUCCESS;
+
+    /* only handle the savestate here */
+    if (!strcmp(name, TPM_SAVESTATE_NAME)) {
+        qemu_mutex_lock(&state_mutex);
+
+        clear_sized_buffer(&save_state);
+
+        if (request_sync_to_bs(SAVESTATE_TYPE)) {
+            rc = TPM_FAIL;
+        }
+
+        qemu_mutex_unlock(&state_mutex);
+    } else if (!strcmp(name, TPM_VOLATILESTATE_NAME)) {
+        qemu_mutex_lock(&state_mutex);
+
+        clear_sized_buffer(&volatile_state);
+
+        if (request_sync_to_bs(VOLASTATE_TYPE)) {
+            rc = TPM_FAIL;
+        }
+
+        qemu_mutex_unlock(&state_mutex);
+    }
+
+    return rc;
+}
+
+
+static TPM_RESULT tpm_builtin_io_init(void)
+{
+    return TPM_SUCCESS;
+}
+
+
+static TPM_RESULT tpm_builtin_io_getlocality(
+                                 TPM_MODIFIER_INDICATOR *localityModifier)
+{
+    *localityModifier = (TPM_MODIFIER_INDICATOR)g_locty;
+
+    return TPM_SUCCESS;
+}
+
+
+static TPM_RESULT tpm_builtin_io_getphysicalpresence(
+                                 TPM_BOOL *physicalPresence)
+{
+    *physicalPresence = FALSE;
+
+    return TPM_SUCCESS;
+}
+
+
+/*****************************************************************/
+
+
 static void tpm_builtin_reset(void)
 {
 #if defined DEBUG_TPM || defined DEBUG_TPM_SR
@@ -286,7 +556,11 @@  static void tpm_builtin_reset(void)
 
     tpm_builtin_terminate_tpm_thread();
 
+    clear_sized_buffer(&permanent_state);
+    clear_sized_buffer(&save_state);
+    clear_sized_buffer(&volatile_state);
     had_fatal_error = false;
+    need_read_volatile = false;
     had_startup_error = false;
 }
 
@@ -317,27 +591,95 @@  static int tpm_builtin_instantiate_with_
         tis_reset_for_snapshot_resume(s);
     }
 
+    /* we need to defer the read since we will not have the encryption key
+       in case storage is encrypted at this point */
+    need_read_volatile = true;
+
     return 0;
 }
 
 
+struct libtpms_callbacks callbacks = {
+    .sizeOfStruct               = sizeof(struct libtpms_callbacks),
+    .tpm_nvram_init             = tpm_builtin_nvram_init,
+    .tpm_nvram_loaddata         = tpm_builtin_nvram_loaddata,
+    .tpm_nvram_storedata        = tpm_builtin_nvram_storedata,
+    .tpm_nvram_deletename       = tpm_builtin_nvram_deletename,
+    .tpm_io_init                = tpm_builtin_io_init,
+    .tpm_io_getlocality         = tpm_builtin_io_getlocality,
+    .tpm_io_getphysicalpresence = tpm_builtin_io_getphysicalpresence,
+};
+
+
 static int tpm_builtin_init(TPMState *s, TPMRecvDataCB *recv_data_cb)
 {
+    int flags;
+
+    bs = bdrv_find(VTPM_DRIVE);
+    if (bs == NULL) {
+        fprintf(stderr, "The " VTPM_DRIVE " driver was not found.\n");
+        goto err_exit;
+    }
+
+    if (TPMLIB_RegisterCallbacks(&callbacks) != TPM_SUCCESS) {
+        goto err_exit;
+    }
+
+    if (tpm_builtin_check_bs(bs)) {
+        goto err_exit;
+    }
+
     tpm_thread_params.tpm_state = s;
     tpm_thread_params.recv_data_callback = recv_data_cb;
 
     qemu_mutex_init(&state_mutex);
     qemu_mutex_init(&tpm_initialized_mutex);
+    qemu_cond_init(&bs_write_result_cond);
+
+    if (pipe(pipefd)) {
+        goto err_exit;
+    }
+
+    flags = fcntl(pipefd[0], F_GETFL);
+    if (flags < 0) {
+        goto err_exit_close_pipe;
+    }
+
+    if (fcntl(pipefd[0], F_SETFL, flags | O_NONBLOCK ) < 0) {
+        goto err_exit_close_pipe;
+    }
+
+    qemu_set_fd_handler(pipefd[0], tpm_builtin_fulfill_sync_to_bs_request,
+                        NULL, NULL);
 
     atexit(tpm_builtin_tpm_atexit);
 
     return 0;
+
+err_exit_close_pipe:
+    close(pipefd[0]);
+    pipefd[0] = -1;
+    close(pipefd[1]);
+    pipefd[1] = -1;
+
+err_exit:
+    return 1;
 }
 
 
 static bool tpm_builtin_get_tpm_established_flag(void)
 {
-    return false;
+    TPM_BOOL tpmEstablished = false;
+
+    qemu_mutex_lock(&tpm_initialized_mutex);
+
+    if (tpm_initialized) {
+        TPM_IO_TpmEstablished_Get(&tpmEstablished);
+    }
+
+    qemu_mutex_unlock(&tpm_initialized_mutex);
+
+    return (bool)tpmEstablished;
 }
 
 
@@ -353,6 +695,10 @@  static bool tpm_builtin_get_startup_erro
  */
 static int tpm_builtin_save_volatile_data(void)
 {
+    TPM_RESULT res;
+    unsigned char *buffer;
+    uint32_t buflen;
+
     if (!tpm_initialized) {
         /* TPM was never initialized
            volatile_state.buffer may be NULL if TPM was never used.
@@ -360,17 +706,46 @@  static int tpm_builtin_save_volatile_dat
         return 0;
     }
 
+    /* have the serialized state written to a buffer only */
+#ifdef DEBUG_TPM_SR
+    fprintf(stderr, "tpm: Calling TPMLIB_VolatileAll_Store()\n");
+#endif
+    res = TPMLIB_VolatileAll_Store(&buffer, &buflen);
+
+    if (res != TPM_SUCCESS) {
+#ifdef DEBUG_TPM_SR
+        fprintf(stderr, "tpm: Error: Could not store TPM volatile state\n");
+#endif
+        return 1;
+    }
+
+#ifdef DEBUG_TPM_SR
+    fprintf(stderr, "tpm: got %d bytes of volatilestate [crc=%08x]\n",
+            buflen, (int)crc32(0, buffer, buflen));
+#endif
+
+    set_sized_buffer(&volatile_state, buffer, buflen);
+    if (tpm_builtin_write_state_to_bs(VOLASTATE_TYPE)) {
+        return 1;
+    }
+    volatile_state.size = 0;
+    volatile_state.buffer = NULL;
+
+    /* make sure that everything has been written to disk */
+    tpm_builtin_fulfill_sync_to_bs_request(NULL);
+
     return 0;
 }
 
 
 static size_t tpm_builtin_realloc_buffer(TPMSizedBuffer *sb)
 {
-    size_t wanted_size = 4096;
+    TPM_RESULT res;
+    size_t wanted_size = tpmlib_get_prop(TPMPROP_TPM_BUFFER_MAX);
 
     if (sb->size != wanted_size) {
-        sb->buffer = qemu_realloc(sb->buffer, wanted_size);
-        if (sb->buffer != NULL) {
+        res = TPM_Realloc(&sb->buffer, wanted_size);
+        if (res == TPM_SUCCESS) {
             sb->size = wanted_size;
         } else {
             sb->size = 0;
@@ -386,7 +761,8 @@  static const char *tpm_builtin_create_de
 
     if (!done) {
         snprintf(dev_description, sizeof(dev_description),
-                 "Skeleton TPM backend");
+                 "Qemu's built-in TPM; requires %ukb of block storage",
+                 MINIMUM_BS_SIZE_KB);
         done = 1;
     }
 
@@ -412,7 +788,13 @@  static TPMBackend *tpm_builtin_create(Qe
 
     value = qemu_opt_get(opts, "path");
     if (value) {
-        /* !!! handle file path */
+        if (access(value, R_OK|W_OK)) {
+            fprintf(stderr,
+                    "Cannot access file '%s' from tpm's path option.\n",
+                    value);
+            goto err_exit;
+        }
+        drive_add(IF_NONE, -1, value, TPM_OPTS);
     } else {
         fprintf(stderr, "-tpm is missing path= parameter\n");
         goto err_exit;
@@ -439,7 +821,7 @@  static void tpm_builtin_destroy(TPMBacke
 TPMDriverOps tpm_builtin = {
     .id                       = "builtin",
     .desc                     = tpm_builtin_create_desc,
-    .job_for_main_thread      = NULL,
+    .job_for_main_thread      = tpm_builtin_fulfill_sync_to_bs_request,
     .create                   = tpm_builtin_create,
     .destroy                  = tpm_builtin_destroy,
     .init                     = tpm_builtin_init,
Index: qemu-git/configure
===================================================================
--- qemu-git.orig/configure
+++ qemu-git/configure
@@ -3540,7 +3540,6 @@  if test "$tpm" = "yes"; then
   fi
 
   if test "$has_tpm" = "1"; then
-      echo "CONFIG_TPM_BUILTIN=y" >> $config_target_mak
       echo "CONFIG_TPM=y" >> $config_host_mak
   fi
 fi