Skip to contentSkip to conversation

hw/misc: Add simple measurement hardware

PATCH In review Matthew Garrett 4 participants 17 messages Jun 23, 2016 – Aug 9, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/misc: Add simple measurement hardware

Trusted Boot is based around having a trusted store of measurement data and a secure communications channel between that store and an attestation target. In actual hardware, that's a TPM. Since the TPM can only be accessed via the host system, this in turn requires that the TPM be able to perform reasonably complicated cryptographic functions in order to demonstrate its trusted state.

In cloud environments, qemu is inherently trusted and the hypervisor infrastructure provides a trusted mechanism for extracting information from qemu and providing it to another system. This means we can skip the crypto and stick with the basic functionality - ie, providing a trusted store of measurement data.

This driver provides a very small subset of TPM 1.2 functionality in the form of a bank of registers that can store SHA1 measurements of boot components. Performing a write to one of these registers will append the new 20 byte hash to the 20 bytes currently stored within the register, take a SHA1 of this 40 byte value and then replace the existing register contents with the new value. This ensures that a given value can only be obtained by performing the same sequence of writes. It also adds a monitor command to allow an external agent to extract this information from the running system and provide it over a secure communications channel. Finally, it measures each of the loaded ROMs into one of the registers at reset time.

In combination with work in SeaBIOS and the kernel, this permits a fully measured boot in a virtualised environment without the overhead of a full TPM implementation.

This version of the implementation depends on port io, but if there's interest I'll add mmio as well.

---
 default-configs/x86_64-softmmu.mak |   1 +
 hmp-commands.hx                    |  13 +++
 hw/core/loader.c                   |  12 +++
 hw/i386/acpi-build.c               |  22 ++++
 hw/misc/Makefile.objs              |   1 +
 hw/misc/measurements.c             | 206 +++++++++++++++++++++++++++++++++++++
 hw/misc/measurements.h             |   2 +
 include/hw/isa/isa.h               |  13 +++
 include/hw/loader.h                |   1 +
 monitor.c                          |   1 +
 stubs/Makefile.objs                |   1 +
 stubs/measurements.c               |  13 +++
 12 files changed, 286 insertions(+)
 create mode 100644 hw/misc/measurements.c
 create mode 100644 hw/misc/measurements.h
 create mode 100644 stubs/measurements.c
default-configs/x86_64-softmmu.mak +1 -0
index 6e3b312..6f0fcc3 100644
@@ -58,3 +58,4 @@ CONFIG_IOH3420=y
 CONFIG_I82801B11=y
 CONFIG_SMBIOS=y
 CONFIG_HYPERV_TESTDEV=$(CONFIG_KVM)
+CONFIG_MEASUREMENTS=y
hmp-commands.hx +13 -0
index 98b4b1a..6a31392 100644
@@ -1748,6 +1748,19 @@ Set QOM property @var{property} of object at location @var{path} to value @var{v
 ETEXI
 
     {
+        .name       = "measurements",
+        .args_type  = "",
+        .params     = "",
+        .help       = "Print system measurements",
+        .mhandler.cmd = print_measurements,
+    },
+STEXI
+@item measurements
+@findex measurements
+Redirect Print system measurements
+ETEXI
+
+    {
         .name       = "info",
         .args_type  = "item:s?",
         .params     = "[subcommand]",
hw/core/loader.c +12 -0
index 53e0e41..e1b7af7 100644
@@ -55,6 +55,7 @@
 #include "exec/address-spaces.h"
 #include "hw/boards.h"
 #include "qemu/cutils.h"
+#include "hw/misc/measurements.h"
 
 #include <zlib.h>
 
@@ -1026,6 +1027,17 @@ static void rom_reset(void *unused)
     }
 }
 
+void measure_roms(void)
+{
+    Rom *rom;
+    QTAILQ_FOREACH(rom, &roms, next) {
+        if (rom->data == NULL) {
+            continue;
+        }
+        extend_data(0, rom->data, rom->datasize);
+    }
+}
+
 int rom_check_and_register_reset(void)
 {
     hwaddr addr = 0;
hw/i386/acpi-build.c +22 -0
index 8ca2032..92129d1 100644
@@ -107,6 +107,7 @@ typedef struct AcpiMiscInfo {
     unsigned dsdt_size;
     uint16_t pvpanic_port;
     uint16_t applesmc_io_base;
+    uint16_t measurements_io_base;
 } AcpiMiscInfo;
 
 typedef struct AcpiBuildPciBusHotplugState {
@@ -203,6 +204,7 @@ static void acpi_get_misc_info(AcpiMiscInfo *info)
     info->tpm_version = tpm_get_version();
     info->pvpanic_port = pvpanic_port();
     info->applesmc_io_base = applesmc_port();
+    info->measurements_io_base = measurements_port();
 }
 
 /*
@@ -2185,6 +2187,26 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
         aml_append(dsdt, scope);
     }
 
+    if (misc->measurements_io_base) {
+        scope = aml_scope("\\_SB.PCI0.ISA");
+        dev = aml_device("PCRS");
+
+        aml_append(dev, aml_name_decl("_HID", aml_eisaid("CORE0001")));
+        /* device present, functioning, decoding, not shown in UI */
+        aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
+
+        crs = aml_resource_template();
+        aml_append(crs,
+               aml_io(AML_DECODE16, misc->measurements_io_base,
+                      misc->measurements_io_base,
+                      0x01, 2)
+        );
+        aml_append(dev, aml_name_decl("_CRS", crs));
+
+        aml_append(scope, dev);
+        aml_append(dsdt, scope);
+    }
+
     sb_scope = aml_scope("\\_SB");
     {
         build_memory_devices(sb_scope, nr_mem, pm->mem_hp_io_base,
hw/misc/Makefile.objs +1 -0
index ffb49c1..e7a784b 100644
@@ -5,6 +5,7 @@ common-obj-$(CONFIG_ISA_DEBUG) += debugexit.o
 common-obj-$(CONFIG_SGA) += sga.o
 common-obj-$(CONFIG_ISA_TESTDEV) += pc-testdev.o
 common-obj-$(CONFIG_PCI_TESTDEV) += pci-testdev.o
+common-obj-$(CONFIG_MEASUREMENTS) += measurements.o
 
 obj-$(CONFIG_VMPORT) += vmport.o
 
hw/misc/measurements.c new file +206 -0

206 lines not on this page · raw message

hw/misc/measurements.h new file +2 -0
new file mode 100644
index 0000000..65ad246
@@ -0,0 +1,2 @@
+void print_measurements(Monitor *mon, const QDict *qdict);
+void extend_data(int pcrnum, uint8_t *data, size_t len);
include/hw/isa/isa.h +13 -0
index c87fbad..00694fd 100644
@@ -24,6 +24,9 @@
 #define APPLESMC_MAX_DATA_LENGTH       32
 #define APPLESMC_PROP_IO_BASE "iobase"
 
+#define TYPE_MEASUREMENTS "measurements"
+#define MEASUREMENTS_PROP_IO_BASE "iobase"
+
 static inline uint16_t applesmc_port(void)
 {
     Object *obj = object_resolve_path_type("", TYPE_APPLE_SMC, NULL);
@@ -34,6 +37,16 @@ static inline uint16_t applesmc_port(void)
     return 0;
 }
 
+static inline uint16_t measurements_port(void)
+{
+    Object *obj = object_resolve_path_type("", TYPE_MEASUREMENTS, NULL);
+
+    if (obj) {
+        return object_property_get_int(obj, MEASUREMENTS_PROP_IO_BASE, NULL);
+    }
+    return 0;
+}
+
 #define TYPE_ISADMA "isa-dma"
 
 #define ISADMA_CLASS(klass) \
include/hw/loader.h +1 -0
index 4879b63..cc3157d 100644
@@ -133,6 +133,7 @@ void rom_reset_order_override(void);
 int rom_copy(uint8_t *dest, hwaddr addr, size_t size);
 void *rom_ptr(hwaddr addr);
 void hmp_info_roms(Monitor *mon, const QDict *qdict);
+void measure_roms(void);
 
 #define rom_add_file_fixed(_f, _a, _i)          \
     rom_add_file(_f, NULL, _a, _i, false, NULL)
monitor.c +1 -0
index 6f960f1..6aa7ebc 100644
@@ -32,6 +32,7 @@
 #include "hw/pci/pci.h"
 #include "sysemu/watchdog.h"
 #include "hw/loader.h"
+#include "hw/misc/measurements.h"
 #include "exec/gdbstub.h"
 #include "net/net.h"
 #include "net/slirp.h"
stubs/Makefile.objs +1 -0
index 4b258a6..2ad7f81 100644
@@ -41,3 +41,4 @@ stub-obj-y += target-monitor-defs.o
 stub-obj-y += target-get-monitor-def.o
 stub-obj-y += vhost.o
 stub-obj-y += iohandler.o
+stub-obj-y += measurements.o
stubs/measurements.c new file +13 -0
new file mode 100644
index 0000000..0485d2e
@@ -0,0 +1,13 @@
+#include "qemu/osdep.h"
+#include "monitor/monitor.h"
+#include "hw/misc/measurements.h"
+
+void print_measurements(Monitor *mon, const QDict *qdict)
+{
+    monitor_printf(mon, "No measurement support\n");
+}
+
+void extend_data(int pcrnum, uint8_t *data, size_t len)
+{
+    return;
+}
Signature · 1 line
2.7.4
#1 lore raw

* Matthew Garrett ([email protected]) wrote:

Hi Matthew,
(Ccing in Stefan who has been trying to get vTPM in for years and
Paolo for any x86ism and especially the ACPIisms, and Daniel for crypto stuff)

I'll repeat some of my comments from yesterday's irc chat so you can reply on list.

So overall the plus point is it's simple (much smaller than even the interface to the vTPM), the minus is it's very non-standard.

Quoted 10 lines

Trusted Boot is based around having a trusted store of measurement data and a secure communications channel between that store and an attestation target. In actual hardware, that's a TPM. Since the TPM can only be accessed via the host system, this in turn requires that the TPM be able to perform reasonably complicated cryptographic functions in order to demonstrate its trusted state.

In cloud environments, qemu is inherently trusted and the hypervisor infrastructure provides a trusted mechanism for extracting information from qemu and providing it to another system. This means we can skip the crypto and stick with the basic functionality - ie, providing a trusted store of measurement data.

I think the big question for me is what uses this system and in particular how the users can guarantee who they're speaking to; I'd like to understand the cases it works for and those it doesn't; for example:

a) (one that works) 'are all the VMs on my hosts running trusted OSs' That works with this just as well as with a vTPM; you ask your hypervisor to give you the measurements for your guests; you trust your hypervisor. Although I think you've somehow got to extract the measurement log from the guest and get it to the hypervisor if it's going to make sense of the measurements.

b) (one that doesn't?) I'm connecting to a VM remotely over a network, I want to check the VM really is who it says it is and is running a trusted OS. As a remote entity I don't know which hypervisor is running the VM, the VM itself can't sign anything to give me back because it might just sign a reply
for a different VM. On a real TPM the attestation results would be signed
using one of the keys in the TPM (I can't remember which).

c) (similar to b) 'I paid you to give me a ... VM - can I check it really is that' how do I externally to the cloud show that the measurement came from the same VM I'm asking about.

and then I'm not clear which of the existing TPM users could be munged into working with it; can you make an existing trusted-grub or trousers write measurements and log into it?

Quoted 10 lines

This driver provides a very small subset of TPM 1.2 functionality in the form of a bank of registers that can store SHA1 measurements of boot components. Performing a write to one of these registers will append the new 20 byte hash to the 20 bytes currently stored within the register, take a SHA1 of this 40 byte value and then replace the existing register contents with the new value. This ensures that a given value can only be obtained by performing the same sequence of writes. It also adds a monitor command to allow an external agent to extract this information from the running system and provide it over a secure communications channel. Finally, it measures each of the loaded ROMs into one of the registers at reset time.

Quoted 3 lines

In combination with work in SeaBIOS and the kernel, this permits a fully measured boot in a virtualised environment without the overhead of a full TPM implementation.

So only SeaBIOS for now? Would it work for EFI in principle?

Quoted 2 lines

This version of the implementation depends on port io, but if there's interest I'll add mmio as well.

I guess port IO has the advantage of making it easy to glue into the early parts of the BIOS.

Some things I can see are missing:
Migration support: You probably want to migrate the current measurements and
make sure you don't include the measurements of ROMs on the destination until after reset.
(Search for places that use dc->vmsd = .... as examples) You might want to add a measurement to indicate a migration happened; but that's a separate question.

QMP support: You should wire it up to the qmp monitor as well.
Generally the management tools use qmp rather than hmp,

What about hotplug? If I hotplug a NIC should it measure the new ROM?
What happens then if I restart the VM from clean with the ROM added.

Why do SHA1 based TPM 1.2 now, shouldn't you start with TPM2/newer SHAs?

I guess another approach to the same thing would be to bundle this up into
something that responded to TPM commands like a vTPM but just had less inside it; then it could attach to the existing TPM interfaces?

Quoted 68 lines
---
 default-configs/x86_64-softmmu.mak |   1 +
 hmp-commands.hx                    |  13 +++
 hw/core/loader.c                   |  12 +++
 hw/i386/acpi-build.c               |  22 ++++
 hw/misc/Makefile.objs              |   1 +
 hw/misc/measurements.c             | 206 +++++++++++++++++++++++++++++++++++++
 hw/misc/measurements.h             |   2 +
 include/hw/isa/isa.h               |  13 +++
 include/hw/loader.h                |   1 +
 monitor.c                          |   1 +
 stubs/Makefile.objs                |   1 +
 stubs/measurements.c               |  13 +++
 12 files changed, 286 insertions(+)
 create mode 100644 hw/misc/measurements.c
 create mode 100644 hw/misc/measurements.h
 create mode 100644 stubs/measurements.c
default-configs/x86_64-softmmu.mak +1 -0
index 6e3b312..6f0fcc3 100644
@@ -58,3 +58,4 @@ CONFIG_IOH3420=y
 CONFIG_I82801B11=y
 CONFIG_SMBIOS=y
 CONFIG_HYPERV_TESTDEV=$(CONFIG_KVM)
+CONFIG_MEASUREMENTS=y
hmp-commands.hx +13 -0
index 98b4b1a..6a31392 100644
@@ -1748,6 +1748,19 @@ Set QOM property @var{property} of object at location @var{path} to value @var{v
 ETEXI
 
     {
+        .name       = "measurements",
+        .args_type  = "",
+        .params     = "",
+        .help       = "Print system measurements",
+        .mhandler.cmd = print_measurements,
+    },
+STEXI
+@item measurements
+@findex measurements
+Redirect Print system measurements
+ETEXI
+
+    {
         .name       = "info",
         .args_type  = "item:s?",
         .params     = "[subcommand]",
hw/core/loader.c +1 -0
index 53e0e41..e1b7af7 100644
@@ -55,6 +55,7 @@
 #include "exec/address-spaces.h"
 #include "hw/boards.h"
 #include "qemu/cutils.h"
+#include "hw/misc/measurements.h"
 
 #include <zlib.h>
 
@@ -1026,6 +1027,17 @@ static void rom_reset(void *unused)
     }
 }
 

hw/core/loader.c static void rom_reset(void *unused) · line 1039

Quoted 10 lines
hw/core/loader.c +10 -0
+void measure_roms(void)
+{
+    Rom *rom;
+    QTAILQ_FOREACH(rom, &roms, next) {
+        if (rom->data == NULL) {
+            continue;
+        }
+        extend_data(0, rom->data, rom->datasize);
+    }
+}

Are you making unpredictable assumptions about ROM order? You're explicitly measuring these into PCR 0 - I guess that's probably reasonable but it should be documented.

Quoted 99 lines
hw/core/loader.c +0 -0
 int rom_check_and_register_reset(void)
 {
     hwaddr addr = 0;
hw/i386/acpi-build.c +22 -0
index 8ca2032..92129d1 100644
@@ -107,6 +107,7 @@ typedef struct AcpiMiscInfo {
     unsigned dsdt_size;
     uint16_t pvpanic_port;
     uint16_t applesmc_io_base;
+    uint16_t measurements_io_base;
 } AcpiMiscInfo;
 
 typedef struct AcpiBuildPciBusHotplugState {
@@ -203,6 +204,7 @@ static void acpi_get_misc_info(AcpiMiscInfo *info)
     info->tpm_version = tpm_get_version();
     info->pvpanic_port = pvpanic_port();
     info->applesmc_io_base = applesmc_port();
+    info->measurements_io_base = measurements_port();
 }
 
 /*
@@ -2185,6 +2187,26 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
         aml_append(dsdt, scope);
     }
 
+    if (misc->measurements_io_base) {
+        scope = aml_scope("\\_SB.PCI0.ISA");
+        dev = aml_device("PCRS");
+
+        aml_append(dev, aml_name_decl("_HID", aml_eisaid("CORE0001")));
+        /* device present, functioning, decoding, not shown in UI */
+        aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
+
+        crs = aml_resource_template();
+        aml_append(crs,
+               aml_io(AML_DECODE16, misc->measurements_io_base,
+                      misc->measurements_io_base,
+                      0x01, 2)
+        );
+        aml_append(dev, aml_name_decl("_CRS", crs));
+
+        aml_append(scope, dev);
+        aml_append(dsdt, scope);
+    }
+
     sb_scope = aml_scope("\\_SB");
     {
         build_memory_devices(sb_scope, nr_mem, pm->mem_hp_io_base,
hw/misc/Makefile.objs +1 -0
index ffb49c1..e7a784b 100644
@@ -5,6 +5,7 @@ common-obj-$(CONFIG_ISA_DEBUG) += debugexit.o
 common-obj-$(CONFIG_SGA) += sga.o
 common-obj-$(CONFIG_ISA_TESTDEV) += pc-testdev.o
 common-obj-$(CONFIG_PCI_TESTDEV) += pci-testdev.o
+common-obj-$(CONFIG_MEASUREMENTS) += measurements.o
 
 obj-$(CONFIG_VMPORT) += vmport.o
 
hw/misc/measurements.c new file +31 -0
new file mode 100644
index 0000000..3940d31
@@ -0,0 +1,206 @@
+/*
+ * QEMU boot measurement
+ *
+ * Copyright (c) 2016 CoreOS, Inc <[email protected]>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#include "qemu/osdep.h"
+#include "hw/isa/isa.h"
+#include "crypto/hash.h"
+#include "hw/misc/measurements.h"
+#include "monitor/monitor.h"
+#include "hw/loader.h"
+
+#define MEASUREMENT(obj) OBJECT_CHECK(MeasurementState, (obj), TYPE_MEASUREMENTS)

hw/misc/measurements.c line 41

Quoted 10 lines
hw/misc/measurements.c new file +10 -0
+
+typedef struct MeasurementState MeasurementState;
+
+struct MeasurementState {
+    ISADevice parent_obj;
+    MemoryRegion io_select;
+    MemoryRegion io_value;
+    uint16_t iobase;
+    uint8_t measurements[24][20];
+    uint8_t tmpmeasurement[20];

Magic numbers; please use #define's somewhere.

hw/misc/measurements.c line 52

Quoted 11 lines
hw/misc/measurements.c new file +11 -0
+    int write_count;
+    int read_count;
+    uint8_t pcr;
+};
+
+static void measurement_reset(DeviceState *dev)
+{
+    MeasurementState *s = MEASUREMENT(dev);
+
+    memset(s->measurements, 0, sizeof(s->measurements));
+    measure_roms();

If you're assuming that can be none-0 then don't you also want to zero pcr, read_count, write_count?

hw/misc/measurements.c line 60

Quoted 8 lines
hw/misc/measurements.c new file +8 -0
+}
+
+static void measurement_select(void *opaque, hwaddr addr, uint64_t val, unsigned size)
+{
+    MeasurementState *s = MEASUREMENT(opaque);
+    s->pcr = val;
+    s->read_count = 0;
+    s->write_count = 0;

What stops me selecting pcr 25 and overwriting stuff with junk?

Quoted 17 lines
hw/misc/measurements.c new file +17 -0
+}
+
+static uint64_t measurement_version(void *opaque, hwaddr addr, unsigned size)
+{
+    return 0;
+}
+
+static uint64_t measurement_read(void *opaque, hwaddr addr, unsigned size)
+{
+    MeasurementState *s = MEASUREMENT(opaque);
+
+    if (s->read_count == 20) {
+        s->read_count = 0;
+    }
+    return s->measurements[s->pcr][s->read_count++];
+}
+

hw/misc/measurements.c line 87

Quoted 10 lines
hw/misc/measurements.c new file +10 -0
+static void extend(MeasurementState *s, int pcrnum, uint8_t *data)
+{
+    uint8_t *result;
+    char tmpbuf[40];
+    Error *err;
+    size_t resultlen = 0;
+
+    memcpy(tmpbuf, s->measurements[pcrnum], 20);
+    memcpy(tmpbuf + 20, data, 20);
+    qcrypto_hash_bytes(QCRYPTO_HASH_ALG_SHA1, tmpbuf, 40, &result, &resultlen, &err);

I think if you're ignoring any errors then using NULL instead of &err is better.

Quoted 19 lines
hw/misc/measurements.c new file +19 -0
+    memcpy(s->measurements[pcrnum], result, 20);
+    g_free(result);
+}
+
+static void measurement_value(void *opaque, hwaddr addr, uint64_t val, unsigned size)
+{
+    MeasurementState *s = opaque;
+
+    s->tmpmeasurement[s->write_count++] = val;
+    if (s->write_count == 20) {
+        extend(s, s->pcr, s->tmpmeasurement);
+        s->write_count = 0;
+    }
+}
+
+void extend_data(int pcrnum, uint8_t *data, size_t len)
+{
+    uint8_t *result;
+    Error *err;

hw/misc/measurements.c line 116

Quoted 10 lines
hw/misc/measurements.c new file +10 -0
+    size_t resultlen = 0;
+    int ret;
+    Object *obj = object_resolve_path_type("", TYPE_MEASUREMENTS, NULL);
+
+    if (!obj) {
+        return;
+    }
+
+    ret = qcrypto_hash_bytes(QCRYPTO_HASH_ALG_SHA1, (char *)data, len, &result,
+                             &resultlen, &err);

Again probably NULL here for the err unless you actually want to report it.

hw/misc/measurements.c line 119

Quoted 3 lines
hw/misc/measurements.c new file +3 -0
+    if (ret < 0) {
+        return;
+    }

Hmm, what are the rules on freeing result on qcrypto_has_bytes returning an error?

Quoted 37 lines
hw/misc/measurements.c new file +37 -0
+
+    extend(MEASUREMENT(obj), pcrnum, result);
+    g_free(result);
+}
+
+static const MemoryRegionOps measurement_select_ops = {
+    .write = measurement_select,
+    .read = measurement_version,
+    .endianness = DEVICE_NATIVE_ENDIAN,
+    .impl = {
+        .min_access_size = 1,
+        .max_access_size = 1,
+    },
+};
+
+static const MemoryRegionOps measurement_value_ops = {
+    .write = measurement_value,
+    .read = measurement_read,
+    .endianness = DEVICE_NATIVE_ENDIAN,
+    .impl = {
+        .min_access_size = 1,
+        .max_access_size = 1,
+    },
+};
+
+static void measurement_realize(DeviceState *dev, Error **errp)
+{
+    MeasurementState *s = MEASUREMENT(dev);
+
+    memory_region_init_io(&s->io_select, OBJECT(s), &measurement_select_ops, s,
+                          "measurement-select", 1);
+    isa_register_ioport(&s->parent_obj, &s->io_select, s->iobase);
+    memory_region_init_io(&s->io_value, OBJECT(s), &measurement_value_ops, s,
+                          "measurement-value", 1);
+    isa_register_ioport(&s->parent_obj, &s->io_value, s->iobase + 1);
+    measurement_reset(dev);
+}

hw/misc/measurements.c line 166

Quoted 10 lines
hw/misc/measurements.c new file +10 -0
+
+static Property measurement_props[] = {
+    DEFINE_PROP_UINT16(MEASUREMENTS_PROP_IO_BASE, MeasurementState, iobase, 0x620),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void measurement_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    fprintf(stderr, "CLASS INIT\n");

Oops, fprintf escaped into the wild. You might like to add some trace entries.

Quoted 23 lines
hw/misc/measurements.c new file +23 -0
+    dc->realize = measurement_realize;
+    dc->reset = measurement_reset;
+    dc->props = measurement_props;
+    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
+}
+
+static const TypeInfo measurement = {
+    .name          = TYPE_MEASUREMENTS,
+    .parent        = TYPE_ISA_DEVICE,
+    .instance_size = sizeof(MeasurementState),
+    .class_init    = measurement_class_init,
+};
+
+static void measurement_register_types(void)
+{
+    type_register_static(&measurement);
+}
+
+type_init(measurement_register_types);
+
+void print_measurements(Monitor *mon, const QDict *qdict)
+{
+    int i, j;

hw/misc/measurements.c line 199

Quoted 10 lines
hw/misc/measurements.c new file +10 -0
+    Object *obj = object_resolve_path_type("", TYPE_MEASUREMENTS, NULL);
+    MeasurementState *s;
+
+    if (!obj) {
+        return;
+    }
+
+    s = MEASUREMENT(obj);
+
+    for (i = 0; i < 24; i++) {

for (pcr = ....

Quoted 102 lines
hw/misc/measurements.c new file +7 -0
+        monitor_printf(mon, "0x%02x: ", i);
+        for (j = 0; j < 20; j++) {
+            monitor_printf(mon, "0x%02x ", s->measurements[i][j]);
+        }
+        monitor_printf(mon, "\n");
+    }
+}
hw/misc/measurements.h new file +2 -0
new file mode 100644
index 0000000..65ad246
@@ -0,0 +1,2 @@
+void print_measurements(Monitor *mon, const QDict *qdict);
+void extend_data(int pcrnum, uint8_t *data, size_t len);
include/hw/isa/isa.h +13 -0
index c87fbad..00694fd 100644
@@ -24,6 +24,9 @@
 #define APPLESMC_MAX_DATA_LENGTH       32
 #define APPLESMC_PROP_IO_BASE "iobase"
 
+#define TYPE_MEASUREMENTS "measurements"
+#define MEASUREMENTS_PROP_IO_BASE "iobase"
+
 static inline uint16_t applesmc_port(void)
 {
     Object *obj = object_resolve_path_type("", TYPE_APPLE_SMC, NULL);
@@ -34,6 +37,16 @@ static inline uint16_t applesmc_port(void)
     return 0;
 }
 
+static inline uint16_t measurements_port(void)
+{
+    Object *obj = object_resolve_path_type("", TYPE_MEASUREMENTS, NULL);
+
+    if (obj) {
+        return object_property_get_int(obj, MEASUREMENTS_PROP_IO_BASE, NULL);
+    }
+    return 0;
+}
+
 #define TYPE_ISADMA "isa-dma"
 
 #define ISADMA_CLASS(klass) \
include/hw/loader.h +1 -0
index 4879b63..cc3157d 100644
@@ -133,6 +133,7 @@ void rom_reset_order_override(void);
 int rom_copy(uint8_t *dest, hwaddr addr, size_t size);
 void *rom_ptr(hwaddr addr);
 void hmp_info_roms(Monitor *mon, const QDict *qdict);
+void measure_roms(void);
 
 #define rom_add_file_fixed(_f, _a, _i)          \
     rom_add_file(_f, NULL, _a, _i, false, NULL)
monitor.c +1 -0
index 6f960f1..6aa7ebc 100644
@@ -32,6 +32,7 @@
 #include "hw/pci/pci.h"
 #include "sysemu/watchdog.h"
 #include "hw/loader.h"
+#include "hw/misc/measurements.h"
 #include "exec/gdbstub.h"
 #include "net/net.h"
 #include "net/slirp.h"
stubs/Makefile.objs +1 -0
index 4b258a6..2ad7f81 100644
@@ -41,3 +41,4 @@ stub-obj-y += target-monitor-defs.o
 stub-obj-y += target-get-monitor-def.o
 stub-obj-y += vhost.o
 stub-obj-y += iohandler.o
+stub-obj-y += measurements.o
stubs/measurements.c new file +13 -0
new file mode 100644
index 0000000..0485d2e
@@ -0,0 +1,13 @@
+#include "qemu/osdep.h"
+#include "monitor/monitor.h"
+#include "hw/misc/measurements.h"
+
+void print_measurements(Monitor *mon, const QDict *qdict)
+{
+    monitor_printf(mon, "No measurement support\n");
+}
+
+void extend_data(int pcrnum, uint8_t *data, size_t len)
+{
+    return;
+}
Signature · 1 line
2.7.4

Dave

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#2 lore raw

"Dr. David Alan Gilbert" <[email protected]> wrote on 07/15/2016 07:29:24 AM:

Quoted 8 lines · * Matthew Garrett ([email protected]) wrote:

Hi Matthew,
(Ccing in Stefan who has been trying to get vTPM in for years and
Paolo for any x86ism and especially the ACPIisms, and Daniel for
crypto stuff)

Quoted 4 lines

I'll repeat some of my comments from yesterday's irc chat so you can reply on list.

So overall the plus point is it's simple (much smaller than even

theinterface

Quoted 3 lines

to the vTPM), the minus is it's very non-standard.

Quoted 1 line

Trusted Boot is based around having a trusted store of measurementdata

and a

Quoted 5 lines
Quoted 1 line

secure communications channel between that store and an

attestation target. In

Quoted 1 line

actual hardware, that's a TPM. Since the TPM can only be accessed

via the host

Quoted 1 line

system, this in turn requires that the TPM be able to perform

reasonably

Quoted 17 lines
Quoted 1 line

complicated cryptographic functions in order to demonstrate its

trusted state.

Quoted 2 lines

In cloud environments, qemu is inherently trusted and the

hypervisor infrastructure

Quoted 1 line

provides a trusted mechanism for extracting information from qemu

and providing it

Quoted 1 line

to another system. This means we can skip the crypto and stick

with the basic

Quoted 1 line

functionality - ie, providing a trusted store of measurement data.

I think the big question for me is what uses this system and in particular how the users
can guarantee who they're speaking to; I'd like to understand the cases it works
for and those it doesn't; for example:

Quoted 4 lines

a) (one that works) 'are all the VMs on my hosts running trusted OSs' That works with this just as well as with a vTPM; you ask your
hypervisor to
give you the measurements for your guests; you trust your

hypervisor.

Quoted 3 lines

Although I think you've somehow got to extract the measurement
log from the
guest and get it to the hypervisor if it's going to make sense of

the

Quoted 5 lines

measurements.

b) (one that doesn't?) I'm connecting to a VM remotely over a
network, I want
to check the VM really is who it says it is and is running a

trusted OS.

Quoted 7 lines

As a remote entity I don't know which hypervisor is running
the VM, the VM
itself can't sign anything to give me back because it might
just sign a reply
for a different VM. On a real TPM the attestation results
would be signed
using one of the keys in the TPM (I can't remember which).

Attestation Identity Key (AIK)

Quoted 21 lines

c) (similar to b) 'I paid you to give me a ... VM - can I check
it really is that'
how do I externally to the cloud show that the measurement
came from the same VM
I'm asking about.

and then I'm not clear which of the existing TPM users could be munged into working
with it; can you make an existing trusted-grub or trousers write measurements and log
into it?

Quoted 1 line

This driver provides a very small subset of TPM 1.2 functionality

in the form of a

Quoted 1 line

bank of registers that can store SHA1 measurements of boot

components. Performing a

Quoted 1 line

write to one of these registers will append the new 20 byte hash

to the 20 bytes

Quoted 1 line

currently stored within the register, take a SHA1 of this 40 byte

value and then

Quoted 9 lines
Quoted 1 line

replace the existing register contents with the new value. This

ensures that a

Quoted 1 line

given value can only be obtained by performing the same sequence

of writes. It also

Quoted 1 line

adds a monitor command to allow an external agent to extract this

information from

Quoted 1 line

the running system and provide it over a secure communications

channel. Finally, it

Quoted 1 line

measures each of the loaded ROMs into one of the registers at reset

time.

Are you also providing a measurement log that goes along with these PCR extensions? Like a measurement log we have in the TCPA ACPI table? Just measurements without knowing what was measured wouldn't be all that helpful. Typically recipients of the measurement list would inspect the individual measurements and replay the extensions to come up with the same state of the PCRs that was quoted (signed) by the TPM. Also, are you going to instrument Linux IMA to use this device? And the list goes on into higher level tools that may work with a measurement list from /sys/kernel/security/{tpm0,ima}/*_measurement_list and assume there's a /dev/tpm0 there that can issue a quote with the AIK. Well, one problem is there's little traction for the vTPM but this device here will require new support in existing tools.

Typically the TPM is there for the reason: it is a hardware root of trust that signs the current state of the PCRs that were accumulated by measurements starting early on during BIOS init. Now with this device, apart from exposing this via HMP, how would one be sure that, if the current list of the PCRs is presented to an attesting client, that the kernel or attestation server not just completely fake the state of the PCRs? My assumption here is that the state of this device's PCRs will be exposed to user level application that can then use this in some form of attestation, right?

Quoted 7 lines
Quoted 2 lines

In combination with work in SeaBIOS and the kernel, this permits a

fully measured

Quoted 2 lines

boot in a virtualised environment without the overhead of a full TPM implementation.

So only SeaBIOS for now? Would it work for EFI in principle?

With a measurement list I would hope -- in some form of custom ACPI table or some other buffer?

Stefan

Quoted 8 lines
Quoted 2 lines

This version of the implementation depends on port io, but if

there's interest I'll

Quoted 1 line

add mmio as well.

I guess port IO has the advantage of making it easy to glue into the early parts of the BIOS.

Quoted 7 lines

Some things I can see are missing:
Migration support: You probably want to migrate the current
measurements and
make sure you don't include the measurements
of ROMs on the
destination until after reset. (Search for places that use dc->vmsd = ....

asexamples)

Quoted 4 lines

You might want to add a measurement to
indicate a migration
happened; but that's a separate question.

Quoted 8 lines

QMP support: You should wire it up to the qmp monitor as well.
Generally the management tools use qmp rather than hmp,

What about hotplug? If I hotplug a NIC should it measure the new ROM?
What happens then if I restart the VM from clean with the ROM added.

Why do SHA1 based TPM 1.2 now, shouldn't you start with TPM2/newer

SHAs?

Quoted 2 lines

I guess another approach to the same thing would be to bundle this up

into

Quoted 111 lines

something that responded to TPM commands like a vTPM but just had less inside it; then it could attach to the existing TPM interfaces?

Quoted 8 lines
---
 default-configs/x86_64-softmmu.mak |   1 +
 hmp-commands.hx                    |  13 +++
 hw/core/loader.c                   |  12 +++
 hw/i386/acpi-build.c               |  22 ++++
 hw/misc/Makefile.objs              |   1 +
 hw/misc/measurements.c             | 206 ++++++++++++++++++++++++

+++++++++++++

default-configs/

Quoted 12 lines
 hw/misc/measurements.h             |   2 +
 include/hw/isa/isa.h               |  13 +++
 include/hw/loader.h                |   1 +
 monitor.c                          |   1 +
 stubs/Makefile.objs                |   1 +
 stubs/measurements.c               |  13 +++
 12 files changed, 286 insertions(+)
 create mode 100644 hw/misc/measurements.c
 create mode 100644 hw/misc/measurements.h
 create mode 100644 stubs/measurements.c
default-configs/ renamed from default-configs/x86_64-softmmu.mak +0 -0

x86_64-softmmu.mak

hmp-commands.hx Set QOM property @var{property} of object

Quoted 13 lines

index 6e3b312..6f0fcc3 100644

default-configs/x86_64-softmmu.mak +1 -0
@@ -58,3 +58,4 @@ CONFIG_IOH3420=y
 CONFIG_I82801B11=y
 CONFIG_SMBIOS=y
 CONFIG_HYPERV_TESTDEV=$(CONFIG_KVM)
+CONFIG_MEASUREMENTS=y
hmp-commands.hx +0 -0
index 98b4b1a..6a31392 100644
@@ -1748,6 +1748,19 @@ Set QOM property @var{property} of object 

at location @var{path} to value @var{v

Quoted 35 lines
hmp-commands.hx +13 -0
 ETEXI
 
     {
+        .name       = "measurements",
+        .args_type  = "",
+        .params     = "",
+        .help       = "Print system measurements",
+        .mhandler.cmd = print_measurements,
+    },
+STEXI
+@item measurements
+@findex measurements
+Redirect Print system measurements
+ETEXI
+
+    {
         .name       = "info",
         .args_type  = "item:s?",
         .params     = "[subcommand]",
hw/core/loader.c +1 -0
index 53e0e41..e1b7af7 100644
@@ -55,6 +55,7 @@
 #include "exec/address-spaces.h"
 #include "hw/boards.h"
 #include "qemu/cutils.h"
+#include "hw/misc/measurements.h"
 
 #include <zlib.h>
 
@@ -1026,6 +1027,17 @@ static void rom_reset(void *unused)
     }
 }
 

hw/core/loader.c static void rom_reset(void *unused) · line 1039

Quoted 10 lines
hw/core/loader.c +10 -0
+void measure_roms(void)
+{
+    Rom *rom;
+    QTAILQ_FOREACH(rom, &roms, next) {
+        if (rom->data == NULL) {
+            continue;
+        }
+        extend_data(0, rom->data, rom->datasize);
+    }
+}

Are you making unpredictable assumptions about ROM order? You're explicitly measuring these into PCR 0 - I guess that's probably reasonable but it should be documented.

Quoted 22 lines
hw/core/loader.c +0 -0
 int rom_check_and_register_reset(void)
 {
     hwaddr addr = 0;
hw/i386/acpi-build.c +2 -0
index 8ca2032..92129d1 100644
@@ -107,6 +107,7 @@ typedef struct AcpiMiscInfo {
     unsigned dsdt_size;
     uint16_t pvpanic_port;
     uint16_t applesmc_io_base;
+    uint16_t measurements_io_base;
 } AcpiMiscInfo;
 
 typedef struct AcpiBuildPciBusHotplugState {
@@ -203,6 +204,7 @@ static void acpi_get_misc_info(AcpiMiscInfo *info)
     info->tpm_version = tpm_get_version();
     info->pvpanic_port = pvpanic_port();
     info->applesmc_io_base = applesmc_port();
+    info->measurements_io_base = measurements_port();
 }
 
Quoted 2 lines

hw/i386/acpi-build.c build_dsdt(GArray *table_data, BIOSLinker

Quoted 2 lines
hw/i386/acpi-build.c +0 -0
 /*
@@ -2185,6 +2187,26 @@ build_dsdt(GArray *table_data, BIOSLinker 

*linker,

Quoted 8 lines

hw/i386/acpi-build.c build_dsdt(GArray *table_data, BIOSLinker *linker,

Quoted 8 lines
hw/i386/acpi-build.c +5 -0
         aml_append(dsdt, scope);
     }
 
+    if (misc->measurements_io_base) {
+        scope = aml_scope("\\_SB.PCI0.ISA");
+        dev = aml_device("PCRS");
+
+        aml_append(dev, aml_name_decl("_HID", 

aml_eisaid("CORE0001")));

Quoted 43 lines
Quoted 36 lines
hw/i386/acpi-build.c +15 -0
+        /* device present, functioning, decoding, not shown in UI */
+        aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
+
+        crs = aml_resource_template();
+        aml_append(crs,
+               aml_io(AML_DECODE16, misc->measurements_io_base,
+                      misc->measurements_io_base,
+                      0x01, 2)
+        );
+        aml_append(dev, aml_name_decl("_CRS", crs));
+
+        aml_append(scope, dev);
+        aml_append(dsdt, scope);
+    }
+
     sb_scope = aml_scope("\\_SB");
     {
         build_memory_devices(sb_scope, nr_mem, pm->mem_hp_io_base,
hw/misc/Makefile.objs +1 -0
index ffb49c1..e7a784b 100644
@@ -5,6 +5,7 @@ common-obj-$(CONFIG_ISA_DEBUG) += debugexit.o
 common-obj-$(CONFIG_SGA) += sga.o
 common-obj-$(CONFIG_ISA_TESTDEV) += pc-testdev.o
 common-obj-$(CONFIG_PCI_TESTDEV) += pci-testdev.o
+common-obj-$(CONFIG_MEASUREMENTS) += measurements.o
 
 obj-$(CONFIG_VMPORT) += vmport.o
 
hw/misc/measurements.c new file +0 -0
new file mode 100644
index 0000000..3940d31

hw/misc/measurements.c line 6

Quoted 6 lines
hw/misc/measurements.c new file +6 -0
@@ -0,0 +1,206 @@
+/*
+ * QEMU boot measurement
+ *
+ * Copyright (c) 2016 CoreOS, Inc <[email protected]>
+ *
+ * Permission is hereby granted, free of charge, to any person 

obtaining a copy

Quoted 7 lines

hw/misc/measurements.c line 7

Quoted 1 line
hw/misc/measurements.c new file +1 -0
+ * of this software and associated documentation files (the 

"Software"), to deal

hw/misc/measurements.c line 8

Quoted 1 line
hw/misc/measurements.c new file +1 -0
+ * in the Software without restriction, including without 

limitation the rights

hw/misc/measurements.c line 9

Quoted 1 line
hw/misc/measurements.c new file +1 -0
+ * to use, copy, modify, merge, publish, distribute, sublicense, 

and/or sell

Quoted 1 line
hw/misc/measurements.c new file +1 -0
+ * copies of the Software, and to permit persons to whom the Software 

is

Quoted 9 lines

hw/misc/measurements.c

Quoted 3 lines
hw/misc/measurements.c new file +3 -0
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be

included in

hw/misc/measurements.c

Quoted 3 lines
hw/misc/measurements.c new file +3 -0
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY 

KIND, EXPRESS OR

Quoted 1 line
hw/misc/measurements.c new file +1 -0
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 

MERCHANTABILITY,

Quoted 1 line

hw/misc/measurements.c line 18

Quoted 1 line
hw/misc/measurements.c new file +1 -0
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 

SHALL

Quoted 61 lines

hw/misc/measurements.c line 19

Quoted 1 line
hw/misc/measurements.c new file +1 -0
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 

DAMAGES OR OTHER

hw/misc/measurements.c line 20

Quoted 1 line
hw/misc/measurements.c new file +1 -0
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 

OTHERWISE, ARISING FROM,

hw/misc/measurements.c line 21

Quoted 1 line
hw/misc/measurements.c new file +1 -0
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 

DEALINGS IN

hw/misc/measurements.c

Quoted 10 lines
hw/misc/measurements.c new file +10 -0
+ * THE SOFTWARE.
+ */
+#include "qemu/osdep.h"
+#include "hw/isa/isa.h"
+#include "crypto/hash.h"
+#include "hw/misc/measurements.h"
+#include "monitor/monitor.h"
+#include "hw/loader.h"
+
+#define MEASUREMENT(obj) OBJECT_CHECK(MeasurementState, (obj), 

TYPE_MEASUREMENTS)

hw/misc/measurements.c line 41

Quoted 10 lines
hw/misc/measurements.c new file +10 -0
+
+typedef struct MeasurementState MeasurementState;
+
+struct MeasurementState {
+    ISADevice parent_obj;
+    MemoryRegion io_select;
+    MemoryRegion io_value;
+    uint16_t iobase;
+    uint8_t measurements[24][20];
+    uint8_t tmpmeasurement[20];

Magic numbers; please use #define's somewhere.

hw/misc/measurements.c line 52

Quoted 11 lines
hw/misc/measurements.c new file +11 -0
+    int write_count;
+    int read_count;
+    uint8_t pcr;
+};
+
+static void measurement_reset(DeviceState *dev)
+{
+    MeasurementState *s = MEASUREMENT(dev);
+
+    memset(s->measurements, 0, sizeof(s->measurements));
+    measure_roms();

If you're assuming that can be none-0 then don't you also want to zero pcr, read_count, write_count?

hw/misc/measurements.c line 55

Quoted 3 lines
hw/misc/measurements.c new file +3 -0
+}
+
+static void measurement_select(void *opaque, hwaddr addr, 

uint64_t val, unsigned size)

hw/misc/measurements.c line 60

Quoted 5 lines
hw/misc/measurements.c new file +5 -0
+{
+    MeasurementState *s = MEASUREMENT(opaque);
+    s->pcr = val;
+    s->read_count = 0;
+    s->write_count = 0;

What stops me selecting pcr 25 and overwriting stuff with junk?

hw/misc/measurements.c line 63

Quoted 3 lines
hw/misc/measurements.c new file +3 -0
+}
+
+static uint64_t measurement_version(void *opaque, hwaddr addr, 

unsigned size)

Quoted 5 lines

hw/misc/measurements.c

Quoted 5 lines
hw/misc/measurements.c new file +5 -0
+{
+    return 0;
+}
+
+static uint64_t measurement_read(void *opaque, hwaddr addr, unsigned 

size)

Quoted 39 lines
Quoted 9 lines
hw/misc/measurements.c new file +9 -0
+{
+    MeasurementState *s = MEASUREMENT(opaque);
+
+    if (s->read_count == 20) {
+        s->read_count = 0;
+    }
+    return s->measurements[s->pcr][s->read_count++];
+}
+

hw/misc/measurements.c

Quoted 10 lines
hw/misc/measurements.c new file +10 -0
+static void extend(MeasurementState *s, int pcrnum, uint8_t *data)
+{
+    uint8_t *result;
+    char tmpbuf[40];
+    Error *err;
+    size_t resultlen = 0;
+
+    memcpy(tmpbuf, s->measurements[pcrnum], 20);
+    memcpy(tmpbuf + 20, data, 20);
+    qcrypto_hash_bytes(QCRYPTO_HASH_ALG_SHA1, tmpbuf, 40, 

&result, &resultlen, &err);

I think if you're ignoring any errors then using NULL instead of &err is better.

hw/misc/measurements.c

Quoted 5 lines
hw/misc/measurements.c new file +5 -0
+    memcpy(s->measurements[pcrnum], result, 20);
+    g_free(result);
+}
+
+static void measurement_value(void *opaque, hwaddr addr, uint64_t

val, unsigned size)

Quoted 9 lines
hw/misc/measurements.c new file +9 -0
+{
+    MeasurementState *s = opaque;
+
+    s->tmpmeasurement[s->write_count++] = val;
+    if (s->write_count == 20) {
+        extend(s, s->pcr, s->tmpmeasurement);
+        s->write_count = 0;
+    }
+}
Quoted 8 lines

hw/misc/measurements.c

Quoted 8 lines
hw/misc/measurements.c new file +8 -0
+
+void extend_data(int pcrnum, uint8_t *data, size_t len)
+{
+    uint8_t *result;
+    Error *err;
+    size_t resultlen = 0;
+    int ret;
+    Object *obj = object_resolve_path_type("", TYPE_MEASUREMENTS, 

NULL);

Quoted 10 lines

hw/misc/measurements.c

Quoted 6 lines
hw/misc/measurements.c new file +6 -0
+
+    if (!obj) {
+        return;
+    }
+
+    ret = qcrypto_hash_bytes(QCRYPTO_HASH_ALG_SHA1, (char *)data,

len, &result,

hw/misc/measurements.c line 116

Quoted 1 line
hw/misc/measurements.c new file +1 -0
+                             &resultlen, &err);

Again probably NULL here for the err unless you actually want to report

it.

Quoted 78 lines

hw/misc/measurements.c line 119

Quoted 4 lines
hw/misc/measurements.c new file +3 -0
+    if (ret < 0) {
+        return;
+    }

Hmm, what are the rules on freeing result on qcrypto_has_bytes returning an error?

Quoted 20 lines
hw/misc/measurements.c new file +20 -0
+
+    extend(MEASUREMENT(obj), pcrnum, result);
+    g_free(result);
+}
+
+static const MemoryRegionOps measurement_select_ops = {
+    .write = measurement_select,
+    .read = measurement_version,
+    .endianness = DEVICE_NATIVE_ENDIAN,
+    .impl = {
+        .min_access_size = 1,
+        .max_access_size = 1,
+    },
+};
+
+static const MemoryRegionOps measurement_value_ops = {
+    .write = measurement_value,
+    .read = measurement_read,
+    .endianness = DEVICE_NATIVE_ENDIAN,
+    .impl = {

hw/misc/measurements.c

Quoted 10 lines
hw/misc/measurements.c new file +10 -0
+        .min_access_size = 1,
+        .max_access_size = 1,
+    },
+};
+
+static void measurement_realize(DeviceState *dev, Error **errp)
+{
+    MeasurementState *s = MEASUREMENT(dev);
+
+    memory_region_init_io(&s->io_select, OBJECT(s), 

&measurement_select_ops, s,

hw/misc/measurements.c

Quoted 3 lines
hw/misc/measurements.c new file +3 -0
+                          "measurement-select", 1);
+    isa_register_ioport(&s->parent_obj, &s->io_select, s->iobase);
+    memory_region_init_io(&s->io_value, OBJECT(s), 

&measurement_value_ops, s,

hw/misc/measurements.c

Quoted 7 lines
hw/misc/measurements.c new file +7 -0
+                          "measurement-value", 1);
+    isa_register_ioport(&s->parent_obj, &s->io_value, s->iobase + 1);
+    measurement_reset(dev);
+}
+
+static Property measurement_props[] = {
+    DEFINE_PROP_UINT16(MEASUREMENTS_PROP_IO_BASE, 

MeasurementState, iobase, 0x620),

hw/misc/measurements.c line 166

Quoted 7 lines
hw/misc/measurements.c new file +7 -0
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void measurement_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    fprintf(stderr, "CLASS INIT\n");

Oops, fprintf escaped into the wild. You might like to add some trace entries.

Quoted 16 lines
hw/misc/measurements.c new file +16 -0
+    dc->realize = measurement_realize;
+    dc->reset = measurement_reset;
+    dc->props = measurement_props;
+    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
+}
+
+static const TypeInfo measurement = {
+    .name          = TYPE_MEASUREMENTS,
+    .parent        = TYPE_ISA_DEVICE,
+    .instance_size = sizeof(MeasurementState),
+    .class_init    = measurement_class_init,
+};
+
+static void measurement_register_types(void)
+{
+    type_register_static(&measurement);
Quoted 8 lines

hw/misc/measurements.c

Quoted 8 lines
hw/misc/measurements.c new file +8 -0
+}
+
+type_init(measurement_register_types);
+
+void print_measurements(Monitor *mon, const QDict *qdict)
+{
+    int i, j;
+    Object *obj = object_resolve_path_type("", TYPE_MEASUREMENTS, 

NULL);

Quoted 45 lines

hw/misc/measurements.c line 199

Quoted 9 lines
hw/misc/measurements.c new file +9 -0
+    MeasurementState *s;
+
+    if (!obj) {
+        return;
+    }
+
+    s = MEASUREMENT(obj);
+
+    for (i = 0; i < 24; i++) {

for (pcr = ....

Quoted 33 lines
hw/misc/measurements.c new file +7 -0
+        monitor_printf(mon, "0x%02x: ", i);
+        for (j = 0; j < 20; j++) {
+            monitor_printf(mon, "0x%02x ", s->measurements[i][j]);
+        }
+        monitor_printf(mon, "\n");
+    }
+}
hw/misc/measurements.h new file +2 -0
new file mode 100644
index 0000000..65ad246
@@ -0,0 +1,2 @@
+void print_measurements(Monitor *mon, const QDict *qdict);
+void extend_data(int pcrnum, uint8_t *data, size_t len);
include/hw/isa/isa.h +3 -0
index c87fbad..00694fd 100644
@@ -24,6 +24,9 @@
 #define APPLESMC_MAX_DATA_LENGTH       32
 #define APPLESMC_PROP_IO_BASE "iobase"
 
+#define TYPE_MEASUREMENTS "measurements"
+#define MEASUREMENTS_PROP_IO_BASE "iobase"
+
 static inline uint16_t applesmc_port(void)
 {
     Object *obj = object_resolve_path_type("", TYPE_APPLE_SMC, NULL);
@@ -34,6 +37,16 @@ static inline uint16_t applesmc_port(void)
     return 0;
 }
 
Quoted 3 lines

include/hw/isa/isa.h static inline uint16_t applesmc_port(void)

Quoted 3 lines
include/hw/isa/isa.h +3 -0
+static inline uint16_t measurements_port(void)
+{
+    Object *obj = object_resolve_path_type("", TYPE_MEASUREMENTS, 

NULL);

Quoted 73 lines

include/hw/isa/isa.h static inline uint16_t applesmc_port(void)

Quoted 3 lines
include/hw/isa/isa.h +3 -0
+
+    if (obj) {
+        return object_property_get_int(obj, 

MEASUREMENTS_PROP_IO_BASE, NULL);

Quoted 63 lines
include/hw/isa/isa.h +4 -0
+    }
+    return 0;
+}
+
 #define TYPE_ISADMA "isa-dma"
 
 #define ISADMA_CLASS(klass) \
include/hw/loader.h +1 -0
index 4879b63..cc3157d 100644
@@ -133,6 +133,7 @@ void rom_reset_order_override(void);
 int rom_copy(uint8_t *dest, hwaddr addr, size_t size);
 void *rom_ptr(hwaddr addr);
 void hmp_info_roms(Monitor *mon, const QDict *qdict);
+void measure_roms(void);
 
 #define rom_add_file_fixed(_f, _a, _i)          \
     rom_add_file(_f, NULL, _a, _i, false, NULL)
monitor.c +1 -0
index 6f960f1..6aa7ebc 100644
@@ -32,6 +32,7 @@
 #include "hw/pci/pci.h"
 #include "sysemu/watchdog.h"
 #include "hw/loader.h"
+#include "hw/misc/measurements.h"
 #include "exec/gdbstub.h"
 #include "net/net.h"
 #include "net/slirp.h"
stubs/Makefile.objs +1 -0
index 4b258a6..2ad7f81 100644
@@ -41,3 +41,4 @@ stub-obj-y += target-monitor-defs.o
 stub-obj-y += target-get-monitor-def.o
 stub-obj-y += vhost.o
 stub-obj-y += iohandler.o
+stub-obj-y += measurements.o
stubs/measurements.c new file +13 -0
new file mode 100644
index 0000000..0485d2e
@@ -0,0 +1,13 @@
+#include "qemu/osdep.h"
+#include "monitor/monitor.h"
+#include "hw/misc/measurements.h"
+
+void print_measurements(Monitor *mon, const QDict *qdict)
+{
+    monitor_printf(mon, "No measurement support\n");
+}
+
+void extend_data(int pcrnum, uint8_t *data, size_t len)
+{
+    return;
+}
Signature · 1 line
2.7.4

Dave

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#3 lore raw
Quoted 2 lines · Stefan Berger

Are you also providing a measurement log that goes along with these PCR extensions? Like a measurement log we have in the TCPA ACPI table? Just measurements without knowing what was measured wouldn't be all that helpful. Typically recipients of the measurement list would inspect the individual measurements and replay the extensions to come up with the same state of the PCRs that was quoted (signed) by the TPM. Also, are you going to instrument Linux IMA to use this device? And the list goes on into higher level tools that may work with a measurement list from /sys/kernel/security/{tpm0,ima}/*_measurement_list and assume there's a /dev/tpm0 there that can issue a quote with the AIK. Well, one problem is there's little traction for the vTPM but this device here will require new support in existing tools.

Yes, the firmware will build a log - right now I'm using the TCPA format. However, the initial measurements from qemu aren't being handed over to the firmware, so I need to fix that. There's no fundamental difficulty in adding IMA support, I have a small driver that can register with enough of the TPM layer to allow its extend calls to work. I'm not too worried about the existing tooling not working, to be honest. Making that work would require the hypervisor to be able to provide a signed quote in TPM format, and that seems like a much harder sell.

Quoted 2 lines

Typically the TPM is there for the reason: it is a hardware root of trust that signs the current state of the PCRs that were accumulated by measurements starting early on during BIOS init. Now with this device, apart from exposing this via HMP, how would one be sure that, if the current list of the PCRs is presented to an attesting client, that the kernel or attestation server not just completely fake the state of the PCRs? My assumption here is that the state of this device's PCRs will be exposed to user level application that can then use this in some form of attestation, right?

Userspace will be able to grab it, but the idea is that the hypervisor API will allow a copy to be obtained - either a signed copy from the local API endpoint, or directly via a remote API endpoint. The guest won't be able to fake the former case, and isn't involved at all in the latter case.

#4 lore raw

Matthew Garrett <[email protected]> wrote on 07/18/2016 05:26:03 PM:

Quoted 2 lines

On Fri, Jul 15, 2016 at 11:11 AM, Stefan Berger <[email protected]>

Quoted 13 lines · wrote:
Quoted 3 lines

Typically the TPM is there for the reason: it is a hardware root

of trust that signs the current state of the PCRs that were accumulated by measurements starting early on during BIOS init. Now with this device, apart from exposing this via HMP, how would one be sure that, if the current list of the PCRs is presented to an attesting client, that the kernel or attestation server not just completely fake the state of the PCRs? My assumption here is that the state of this device's PCRs will be exposed to user level application that can then use this in some form of attestation, right?

Quoted 6 lines

Userspace will be able to grab it, but the idea is that the hypervisor API will allow a copy to be obtained - either a signed copy from the local API endpoint, or directly via a remote API endpoint. The guest won't be able to fake the former case, and isn't involved at all in the latter case.

The TPM security's model related to logs, the state of the PCRs, and attestation involves the following pieces:

- PCRs
- measurement log
- EK + certificate
- platform certificate
- AIK + certificate
- quotes (signatures) on PCR state with keys that cannot leave the TPM (AIKs)
- infrastructure to issue the AIK certificates based on EK + certificate + platform certificate

How does the security model of this device and its presumed infrastructure look like? Does the hypervisor then also support IMA measurement lists or is this restricted to firmware?

Stefan

#5 lore raw
Quoted 13 lines · Stefan Berger

The TPM security's model related to logs, the state of the PCRs, and attestation involves the following pieces:

- PCRs
- measurement log
- EK + certificate
- platform certificate
- AIK + certificate
- quotes (signatures) on PCR state with keys that cannot leave the TPM (AIKs)
- infrastructure to issue the AIK certificates based on EK + certificate + platform certificate

Quoted 3 lines

How does the security model of this device and its presumed infrastructure look like? Does the hypervisor then also support IMA measurement lists or is this restricted to firmware?

The model here is:
- PCRs
- measurement log
- quote on PCR state with key held by hypervisor

There's no fundamental reason why additional layers of key can't be introduced, but since all that complexity is on the hypervisor side it's out of scope for the qemu implementation. A mechanism for establishing trust between the hypervisor and the customer is obviously necessary, but there are already examples such as Amazon's Instance Identity Documents (
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-identity-documents.html
). The OS is free to continue to extend the PCRs after boot, so IMA could certainly be integrated with this.

#6 lore raw

Matthew Garrett <[email protected]> wrote on 07/18/2016 07:52:22 PM:

Quoted 1 line

Subject: Re: [Qemu-devel] [PATCH] hw/misc: Add simple measurement

hardware

Quoted 2 lines

On Mon, Jul 18, 2016 at 4:40 PM, Stefan Berger <[email protected]>

Quoted 11 lines · wrote:
Quoted 11 lines

The TPM security's model related to logs, the state of the PCRs, and attestation involves the following pieces:

- PCRs
- measurement log
- EK + certificate
- platform certificate
- AIK + certificate
- quotes (signatures) on PCR state with keys that cannot leave the TPM (AIKs)
- infrastructure to issue the AIK certificates based on EK +

certificate +

Quoted 3 lines
Quoted 3 lines

platform certificate

How does the security model of this device and its presumed

infrastructure

Quoted 1 line
Quoted 1 line

look like? Does the hypervisor then also support IMA measurement lists

or is

Quoted 7 lines
Quoted 1 line

this restricted to firmware?

The model here is:
- PCRs
- measurement log
- quote on PCR state with key held by hypervisor

Quoted 11 lines

There's no fundamental reason why additional layers of key can't be introduced, but since all that complexity is on the hypervisor side it's out of scope for the qemu implementation. A mechanism for establishing trust between the hypervisor and the customer is obviously necessary, but there are already examples such as Amazon's Instance Identity Documents (
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-
identity-documents.html
). The OS is free to continue to extend the PCRs after boot, so IMA could certainly be integrated with this.

The point of the TPM is that the device that holds the state of the PCRs provides the signatures over their state rather than some other 'entity' whose trustworthiness wouldn't be clear. Admittedly the device comes with its own set of challenges.

#7 lore raw
Quoted 1 line · Stefan Berger

The point of the TPM is that the device that holds the state of the PCRs

provides the signatures over their state rather than some other 'entity' whose trustworthiness wouldn't be clear. Admittedly the device comes with its own set of challenges.

The hypervisor holds the PCR state and also provides the signature. If the hypervisor is untrustworthy than the state of the virtualised system can never be verified, since it could simply have faked the measurements passed to whatever the root of trust is.

#8 lore raw

Matthew Garrett <[email protected]> wrote on 07/18/2016 08:39:07 PM:

Quoted 11 lines · Stefan Berger
Quoted 1 line

The point of the TPM is that the device that holds the state of

the PCRs provides the signatures over their state rather than some other 'entity' whose trustworthiness wouldn't be clear. Admittedly the device comes with its own set of challenges.

The hypervisor holds the PCR state and also provides the signature. If the hypervisor is untrustworthy than the state of the virtualised system can never be verified, since it could simply have faked the measurements passed to whatever the root of trust is.

So the hypervisor will have the key for signing and provide the quote ?

Stefan

#9 lore raw
Quoted 12 lines · Stefan Berger

Matthew Garrett <[email protected]> wrote on 07/18/2016 08:39:07 PM:

Quoted 6 lines · Stefan Berger
Quoted 1 line

The point of the TPM is that the device that holds the state of

the PCRs provides the signatures over their state rather than some other 'entity' whose trustworthiness wouldn't be clear. Admittedly the device comes with its own set of challenges.

Quoted 6 lines
Quoted 4 lines

The hypervisor holds the PCR state and also provides the signature. If the hypervisor is untrustworthy than the state of the virtualised system can never be verified, since it could simply have faked the measurements passed to whatever the root of trust is.

So the hypervisor will have the key for signing and provide the quote ?

Either the hypervisor itself or part of the associated platform. This framework is typically inside the same trust boundary.

#10 lore raw
In reply to Dr. David Alan Gilbert

On Fri, Jul 15, 2016 at 4:29 AM, Dr. David Alan Gilbert <[email protected]

Quoted 12 lines

wrote:

* Matthew Garrett ([email protected]) wrote:
a) (one that works) 'are all the VMs on my hosts running trusted OSs' That works with this just as well as with a vTPM; you ask your
hypervisor to
give you the measurements for your guests; you trust your hypervisor. Although I think you've somehow got to extract the measurement log
from the
guest and get it to the hypervisor if it's going to make sense of the measurements.

The guest can provide the log, but you'd want to obtain the measurements from the hypervisor. The precise implementation of this would probably be somewhat provider-specific - AWS has support for providing signed copies of image metadata, so this could be implemented in a similar way (eg, guest hits the local API endpoint, hypervisor extracts measurement data and signs it, provides that back to guest, guest hands over log and signed measurements to whatever's handling the attestation)

Quoted 12 lines

b) (one that doesn't?) I'm connecting to a VM remotely over a network,
I want
to check the VM really is who it says it is and is running a trusted
OS.
As a remote entity I don't know which hypervisor is running the VM,
the VM
itself can't sign anything to give me back because it might just
sign a reply
for a different VM. On a real TPM the attestation results would be
signed
using one of the keys in the TPM (I can't remember which).

You have the same problem with a TPM - the quote you get back has a chain of trust back to the TPM vendor, but unless you've previously established some specific trust with that system you have no way of knowing whether it's the specific TPM you want to talk to or not. In some ways it's easier with a hypervisor, because it has more information about the guest than a TPM does about the OS. For example, the hypervisor can provide a signed measurement alongside signed metadata that includes the guest's IP address. If they're signed with the same key and the IP address matches what you're talking to, you've established that trust in a much more straightforward manner.

Quoted 6 lines

c) (similar to b) 'I paid you to give me a ... VM - can I check it
really is that'
how do I externally to the cloud show that the measurement came from
the same VM
I'm asking about.

I think that's covered as above.

Quoted 6 lines

and then I'm not clear which of the existing TPM users could be munged into working
with it; can you make an existing trusted-grub or trousers write measurements and log
into it?

Yes - my modified SeaBIOS provides the TCG measurement functions and simply stubs them into this rather than the real TPM. Running
https://github.com/coreos/grub against that gives me a full set of boot measurements, including log.

Quoted 16 lines
Quoted 1 line

This driver provides a very small subset of TPM 1.2 functionality in the

form of a

Quoted 1 line

bank of registers that can store SHA1 measurements of boot components.

Performing a

Quoted 1 line

write to one of these registers will append the new 20 byte hash to the

20 bytes

Quoted 1 line

currently stored within the register, take a SHA1 of this 40 byte value

and then

Quoted 1 line

replace the existing register contents with the new value. This ensures

that a

Quoted 1 line

given value can only be obtained by performing the same sequence of

writes. It also

Quoted 1 line

adds a monitor command to allow an external agent to extract this

information from

Quoted 1 line

the running system and provide it over a secure communications channel.

Finally, it

Quoted 9 lines
Quoted 3 lines

measures each of the loaded ROMs into one of the registers at reset time.

In combination with work in SeaBIOS and the kernel, this permits a fully

measured

Quoted 2 lines

boot in a virtualised environment without the overhead of a full TPM implementation.

So only SeaBIOS for now? Would it work for EFI in principle?

For now, because building EFI is tedious. There's nothing BIOS specific, and I'll add support to OVMF once we've nailed down what the interface looks like.

Quoted 7 lines
Quoted 1 line

This version of the implementation depends on port io, but if there's

interest I'll

Quoted 1 line

add mmio as well.

I guess port IO has the advantage of making it easy to glue into the early parts of the BIOS.

Yeah. Not sure whether the right approach is to use port IO where available and MMIO elsewhere, or just suck up the additional implementation work and do MMIO everywhere.

Quoted 12 lines

Some things I can see are missing:
Migration support: You probably want to migrate the current
measurements and
make sure you don't include the measurements of ROMs
on the
destination until after reset.
(Search for places that use dc->vmsd = .... as
examples)
You might want to add a measurement to indicate a
migration
happened; but that's a separate question.

Hmm, yes. I think we'd need to carefully consider what the semantics of measurement over migration are - do you think this is necessary for an initial implementation, or could it come later?

Quoted 3 lines

QMP support: You should wire it up to the qmp monitor as well.
Generally the management tools use qmp rather than hmp,

Good call.

Quoted 4 lines

What about hotplug? If I hotplug a NIC should it measure the new ROM?
What happens then if I restart the VM from clean with the ROM added.

Mm good question. I *think* my argument would be that, unless we're executing code from that ROM, it shouldn't be measured. That's how it would behave with a real TPM on real hardware. So no to measurement on hotplug, yes to measurement if it's present after reboot.

Quoted 2 lines

Why do SHA1 based TPM 1.2 now, shouldn't you start with TPM2/newer SHAs?

There's no TPM2 spec for BIOS, so it'd involve adding an incompatible interface to BIOS to make use of it. However, adding support for additional hashes and then just having the BIOS code always use SHA1 ought to be fine.

Quoted 4 lines

I guess another approach to the same thing would be to bundle this up into
something that responded to TPM commands like a vTPM but just had less inside it; then it could attach to the existing TPM interfaces?

My plan was to abstract this at the firmware interface level, rather than at the hardware level - for the functionality I'm looking at, emulating the TPM command set would add a *lot* of additional complexity. The benefit would be being able to use existing drivers, but I don't even know how much functionality I'd need to implement in order to get, say, Trousers running.

hw/core/loader.c static void rom_reset(void *unused) · line 1039

Quoted 10 lines
hw/core/loader.c +10 -0
+void measure_roms(void)
+{
+    Rom *rom;
+    QTAILQ_FOREACH(rom, &roms, next) {
+        if (rom->data == NULL) {
+            continue;
+        }
+        extend_data(0, rom->data, rom->datasize);
+    }
+}

Are you making unpredictable assumptions about ROM order?

Quoted 3 lines

You're explicitly measuring these into PCR 0 - I guess that's probably reasonable but it should be documented.

Hm. The ordering issue can be basically avoided by having this be logged, which means passing the data over from qemu to the firmware and letting the firmware use that to populate the log. What's the best way to do that likely to be?

hw/misc/measurements.c line 41

Quoted 7 lines
hw/misc/measurements.c new file +7 -0
+struct MeasurementState {
+    ISADevice parent_obj;
+    MemoryRegion io_select;
+    MemoryRegion io_value;
+    uint16_t iobase;
+    uint8_t measurements[24][20];
+    uint8_t tmpmeasurement[20];

Magic numbers; please use #define's somewhere.

Quoted 1 line

Ok.

Quoted 6 lines
Quoted 6 lines
hw/misc/measurements.c new file +6 -0
+    int write_count;
+    int read_count;
+    uint8_t pcr;
+};
+
+static void measurement_reset(DeviceState *dev)
Quoted 9 lines

hw/misc/measurements.c line 52

Quoted 5 lines
hw/misc/measurements.c new file +5 -0
+{
+    MeasurementState *s = MEASUREMENT(dev);
+
+    memset(s->measurements, 0, sizeof(s->measurements));
+    measure_roms();

If you're assuming that can be none-0 then don't you also want to zero pcr, read_count, write_count?

Hm, yes.

Quoted 4 lines

hw/core/loader.c static void rom_reset(void *unused)

Quoted 3 lines
hw/core/loader.c +3 -0
+}
+
+static void measurement_select(void *opaque, hwaddr addr, uint64_t val,

unsigned size)

Quoted 8 lines

hw/misc/measurements.c line 60

Quoted 5 lines
hw/misc/measurements.c new file +5 -0
+{
+    MeasurementState *s = MEASUREMENT(opaque);
+    s->pcr = val;
+    s->read_count = 0;
+    s->write_count = 0;

What stops me selecting pcr 25 and overwriting stuff with junk?

Oops! Yeah uh that's a really rather good point and I am a bad programmer.

hw/misc/measurements.c

Quoted 3 lines
hw/misc/measurements.c new file +3 -0
+    memcpy(tmpbuf, s->measurements[pcrnum], 20);
+    memcpy(tmpbuf + 20, data, 20);
+    qcrypto_hash_bytes(QCRYPTO_HASH_ALG_SHA1, tmpbuf, 40, &result,

&resultlen, &err);

I think if you're ignoring any errors then using NULL instead of &err is

Quoted 2 lines

better.

Ok.

Quoted 7 lines

hw/misc/measurements.c line 119

Quoted 3 lines
hw/misc/measurements.c new file +3 -0
+    if (ret < 0) {
+        return;
+    }

Hmm, what are the rules on freeing result on qcrypto_has_bytes returning an error?

I'll dig into the code.

hw/misc/measurements.c line 166

Quoted 2 lines
hw/misc/measurements.c new file +2 -0
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    fprintf(stderr, "CLASS INIT\n");

Oops, fprintf escaped into the wild. You might like to add some trace

Quoted 2 lines

entries.

Heh. Yup.

Thanks for the review!

--

Quoted 2 lines

Dr. David Alan Gilbert / [email protected] / Manchester, UK

#11 lore raw
Quoted 14 lines · * Matthew Garrett ([email protected]) wrote:

On Fri, Jul 15, 2016 at 4:29 AM, Dr. David Alan Gilbert <[email protected]

Quoted 12 lines

wrote:

* Matthew Garrett ([email protected]) wrote:
a) (one that works) 'are all the VMs on my hosts running trusted OSs' That works with this just as well as with a vTPM; you ask your
hypervisor to
give you the measurements for your guests; you trust your hypervisor. Although I think you've somehow got to extract the measurement log
from the
guest and get it to the hypervisor if it's going to make sense of the measurements.

Quoted 7 lines

The guest can provide the log, but you'd want to obtain the measurements from the hypervisor. The precise implementation of this would probably be somewhat provider-specific - AWS has support for providing signed copies of image metadata, so this could be implemented in a similar way (eg, guest hits the local API endpoint, hypervisor extracts measurement data and signs it, provides that back to guest, guest hands over log and signed measurements to whatever's handling the attestation)

Yes, this doesn't seem too bad.

Quoted 13 lines
Quoted 12 lines

b) (one that doesn't?) I'm connecting to a VM remotely over a network,
I want
to check the VM really is who it says it is and is running a trusted
OS.
As a remote entity I don't know which hypervisor is running the VM,
the VM
itself can't sign anything to give me back because it might just
sign a reply
for a different VM. On a real TPM the attestation results would be
signed
using one of the keys in the TPM (I can't remember which).

Quoted 10 lines

You have the same problem with a TPM - the quote you get back has a chain of trust back to the TPM vendor, but unless you've previously established some specific trust with that system you have no way of knowing whether it's the specific TPM you want to talk to or not. In some ways it's easier with a hypervisor, because it has more information about the guest than a TPM does about the OS. For example, the hypervisor can provide a signed measurement alongside signed metadata that includes the guest's IP address. If they're signed with the same key and the IP address matches what you're talking to, you've established that trust in a much more straightforward manner.

Hmm true; with TPM once you have done it once and enrolled it you do know that you're making the measurement against the same device; but in your world that's probably true if the host is involved somewhere with getting the measurement. But I don't see in your description how the guest would get a copy of it's PCRs signed by the host.

Quoted 8 lines
Quoted 6 lines

c) (similar to b) 'I paid you to give me a ... VM - can I check it
really is that'
how do I externally to the cloud show that the measurement came from
the same VM
I'm asking about.

I think that's covered as above.

Yep, same.

Quoted 11 lines
Quoted 6 lines

and then I'm not clear which of the existing TPM users could be munged into working
with it; can you make an existing trusted-grub or trousers write measurements and log
into it?

Yes - my modified SeaBIOS provides the TCG measurement functions and simply stubs them into this rather than the real TPM. Running
https://github.com/coreos/grub against that gives me a full set of boot measurements, including log.

Is that reimplementing TPM support in grub? Isn't there a version that already has it?
Dealing with those measurements is really difficult in practice - in particular things like knowing whether a kernel command line is sane/safe is pretty difficult, as is knowing the sanity of an initrd; so please make sure those measurements as close to normal TPM behaviour as possible.

Quoted 57 lines
Quoted 16 lines
Quoted 1 line

This driver provides a very small subset of TPM 1.2 functionality in the

form of a

Quoted 1 line

bank of registers that can store SHA1 measurements of boot components.

Performing a

Quoted 1 line

write to one of these registers will append the new 20 byte hash to the

20 bytes

Quoted 1 line

currently stored within the register, take a SHA1 of this 40 byte value

and then

Quoted 1 line

replace the existing register contents with the new value. This ensures

that a

Quoted 1 line

given value can only be obtained by performing the same sequence of

writes. It also

Quoted 1 line

adds a monitor command to allow an external agent to extract this

information from

Quoted 1 line

the running system and provide it over a secure communications channel.

Finally, it

Quoted 9 lines
Quoted 3 lines

measures each of the loaded ROMs into one of the registers at reset time.

In combination with work in SeaBIOS and the kernel, this permits a fully

measured

Quoted 2 lines

boot in a virtualised environment without the overhead of a full TPM implementation.

So only SeaBIOS for now? Would it work for EFI in principle?

For now, because building EFI is tedious. There's nothing BIOS specific, and I'll add support to OVMF once we've nailed down what the interface looks like.

Quoted 7 lines
Quoted 1 line

This version of the implementation depends on port io, but if there's

interest I'll

Quoted 1 line

add mmio as well.

I guess port IO has the advantage of making it easy to glue into the early parts of the BIOS.

Yeah. Not sure whether the right approach is to use port IO where available and MMIO elsewhere, or just suck up the additional implementation work and do MMIO everywhere.

Quoted 12 lines

Some things I can see are missing:
Migration support: You probably want to migrate the current
measurements and
make sure you don't include the measurements of ROMs
on the
destination until after reset.
(Search for places that use dc->vmsd = .... as
examples)
You might want to add a measurement to indicate a
migration
happened; but that's a separate question.

Quoted 3 lines

Hmm, yes. I think we'd need to carefully consider what the semantics of measurement over migration are - do you think this is necessary for an initial implementation, or could it come later?

My main work is migration so of course I'd say you should think about it; but you're right you can probably leave it at first.
My assumption would be that you'd just migrate the values as part of the stream (pretty easy see VMState stuff in other drivers). I can't remember the behaviour you'll see after a reset on the destination and whether that means you get the destinations ROMs or the ROMs that were migrated.

Quoted 12 lines
Quoted 3 lines

QMP support: You should wire it up to the qmp monitor as well.
Generally the management tools use qmp rather than hmp,

Good call.

Quoted 4 lines

What about hotplug? If I hotplug a NIC should it measure the new ROM?
What happens then if I restart the VM from clean with the ROM added.

Quoted 4 lines

Mm good question. I *think* my argument would be that, unless we're executing code from that ROM, it shouldn't be measured. That's how it would behave with a real TPM on real hardware. So no to measurement on hotplug, yes to measurement if it's present after reboot.

OK.

Quoted 6 lines
Quoted 2 lines

Why do SHA1 based TPM 1.2 now, shouldn't you start with TPM2/newer SHAs?

There's no TPM2 spec for BIOS, so it'd involve adding an incompatible interface to BIOS to make use of it. However, adding support for additional hashes and then just having the BIOS code always use SHA1 ought to be fine.

Yes, although it's probably messier than that; if I remember correctly the new hashes are longer.

Quoted 23 lines
Quoted 4 lines

I guess another approach to the same thing would be to bundle this up into
something that responded to TPM commands like a vTPM but just had less inside it; then it could attach to the existing TPM interfaces?

My plan was to abstract this at the firmware interface level, rather than at the hardware level - for the functionality I'm looking at, emulating the TPM command set would add a *lot* of additional complexity. The benefit would be being able to use existing drivers, but I don't even know how much functionality I'd need to implement in order to get, say, Trousers running.

hw/core/loader.c static void rom_reset(void *unused) · line 1039

Quoted 10 lines
hw/core/loader.c +10 -0
+void measure_roms(void)
+{
+    Rom *rom;
+    QTAILQ_FOREACH(rom, &roms, next) {
+        if (rom->data == NULL) {
+            continue;
+        }
+        extend_data(0, rom->data, rom->datasize);
+    }
+}

Are you making unpredictable assumptions about ROM order?

Quoted 8 lines
Quoted 3 lines

You're explicitly measuring these into PCR 0 - I guess that's probably reasonable but it should be documented.

Hm. The ordering issue can be basically avoided by having this be logged, which means passing the data over from qemu to the firmware and letting the firmware use that to populate the log. What's the best way to do that likely to be?

Hmm, not sure. We know this is happening before the guest starts running, so if it somehow made it's way to the code that sets up ACPI perhaps that could do it; but I'm not sure.

Quoted 73 lines

hw/misc/measurements.c line 41

Quoted 7 lines
hw/misc/measurements.c new file +7 -0
+struct MeasurementState {
+    ISADevice parent_obj;
+    MemoryRegion io_select;
+    MemoryRegion io_value;
+    uint16_t iobase;
+    uint8_t measurements[24][20];
+    uint8_t tmpmeasurement[20];

Magic numbers; please use #define's somewhere.

Quoted 1 line

Ok.

Quoted 6 lines
Quoted 6 lines
hw/misc/measurements.c new file +6 -0
+    int write_count;
+    int read_count;
+    uint8_t pcr;
+};
+
+static void measurement_reset(DeviceState *dev)
Quoted 9 lines

hw/misc/measurements.c line 52

Quoted 5 lines
hw/misc/measurements.c new file +5 -0
+{
+    MeasurementState *s = MEASUREMENT(dev);
+
+    memset(s->measurements, 0, sizeof(s->measurements));
+    measure_roms();

If you're assuming that can be none-0 then don't you also want to zero pcr, read_count, write_count?

Hm, yes.

Quoted 4 lines

hw/core/loader.c static void rom_reset(void *unused)

Quoted 3 lines
hw/core/loader.c +3 -0
+}
+
+static void measurement_select(void *opaque, hwaddr addr, uint64_t val,

unsigned size)

Quoted 8 lines

hw/misc/measurements.c line 60

Quoted 5 lines
hw/misc/measurements.c new file +5 -0
+{
+    MeasurementState *s = MEASUREMENT(opaque);
+    s->pcr = val;
+    s->read_count = 0;
+    s->write_count = 0;

What stops me selecting pcr 25 and overwriting stuff with junk?

Oops! Yeah uh that's a really rather good point and I am a bad programmer.

hw/misc/measurements.c

Quoted 3 lines
hw/misc/measurements.c new file +3 -0
+    memcpy(tmpbuf, s->measurements[pcrnum], 20);
+    memcpy(tmpbuf + 20, data, 20);
+    qcrypto_hash_bytes(QCRYPTO_HASH_ALG_SHA1, tmpbuf, 40, &result,

&resultlen, &err);

I think if you're ignoring any errors then using NULL instead of &err is

Quoted 2 lines

better.

Ok.

Quoted 7 lines

hw/misc/measurements.c line 119

Quoted 3 lines
hw/misc/measurements.c new file +3 -0
+    if (ret < 0) {
+        return;
+    }

Hmm, what are the rules on freeing result on qcrypto_has_bytes returning an error?

I'll dig into the code.

hw/misc/measurements.c line 166

Quoted 2 lines
hw/misc/measurements.c new file +2 -0
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    fprintf(stderr, "CLASS INIT\n");
Quoted 7 lines

Oops, fprintf escaped into the wild. You might like to add some trace

Quoted 2 lines

entries.

Heh. Yup.

Thanks for the review!

No problem.

Dave

Quoted 4 lines

--

Quoted 2 lines

Dr. David Alan Gilbert / [email protected] / Manchester, UK

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#12 lore raw

PATCH v2 1/1 hw/misc: Add simple measurement hardware

In reply to Matthew Garrett

Trusted Boot is based around having a trusted store of measurement data and a secure communications channel between that store and an attestation target. In actual hardware, that's a TPM. Since the TPM can only be accessed via the host system, this in turn requires that the TPM be able to perform reasonably complicated cryptographic functions in order to demonstrate its trusted state.

In cloud environments, qemu is inherently trusted and the hypervisor infrastructure provides a trusted mechanism for extracting information from qemu and providing it to another system. This means we can skip the crypto and stick with the basic functionality - ie, providing a trusted store of measurement data.

This driver provides a very small subset of TPM 1.2 functionality in the form of a bank of registers that can store SHA1 measurements of boot components. Performing a write to one of these registers will append the new 20 byte hash to the 20 bytes currently stored within the register, take a SHA1 of this 40 byte value and then replace the existing register contents with the new value. This ensures that a given value can only be obtained by performing the same sequence of writes. It also adds a monitor command to allow an external agent to extract this information from the running system and provide it over a secure communications channel. Finally, it measures each of the loaded ROMs into one of the registers at reset time.

In combination with work in SeaBIOS and the kernel, this permits a fully measured boot in a virtualised environment without the overhead of a full TPM implementation.

This version of the implementation depends on port io, but if there's interest I'll add mmio as well.

---

This should cover the initial review feedback, with the exception of porting it to MMIO. It seems easier to keep it in port io space on x86, and I'll add MMIO support once it looks like we're happy with this implementation.

 default-configs/x86_64-softmmu.mak |   1 +
 hmp-commands-info.hx               |  14 ++
 hmp.c                              |  13 ++
 hmp.h                              |   1 +
 hw/core/loader.c                   |  12 ++
 hw/i386/acpi-build.c               |  29 +++-
 hw/misc/Makefile.objs              |   1 +
 hw/misc/measurements.c             | 295 +++++++++++++++++++++++++++++++++++++
 hw/misc/measurements.h             |   4 +
 include/hw/isa/isa.h               |  13 ++
 include/hw/loader.h                |   1 +
 monitor.c                          |   1 +
 qapi-schema.json                   |  26 ++++
 qmp-commands.hx                    |  20 +++
 stubs/Makefile.objs                |   1 +
 stubs/measurements.c               |  18 +++
 16 files changed, 448 insertions(+), 2 deletions(-)
 create mode 100644 hw/misc/measurements.c
 create mode 100644 hw/misc/measurements.h
 create mode 100644 stubs/measurements.c
default-configs/x86_64-softmmu.mak +1 -0
index 6e3b312..6f0fcc3 100644
@@ -58,3 +58,4 @@ CONFIG_IOH3420=y
 CONFIG_I82801B11=y
 CONFIG_SMBIOS=y
 CONFIG_HYPERV_TESTDEV=$(CONFIG_KVM)
+CONFIG_MEASUREMENTS=y
hmp-commands-info.hx +14 -0
index 74446c6..bf1cf67 100644
@@ -816,6 +816,20 @@ STEXI
 Show information about hotpluggable CPUs
 ETEXI
 
+    {
+        .name       = "measurements",
+        .args_type  = "",
+        .params     = "",
+        .help       = "show PCR measurements",
+        .mhandler.cmd = hmp_info_measurements,
+    },
+
+STEXI
+@item info measurements
+@findex measurements
+Show PCR measurements
+ETEXI
+
 STEXI
 @end table
 ETEXI
hmp.c +13 -0
index cc2056e..c2ef869 100644
@@ -2038,6 +2038,19 @@ void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
     qapi_free_IOThreadInfoList(info_list);
 }
 
+void hmp_info_measurements(Monitor *mon, const QDict *qdict)
+{
+    MeasurementList *info_list = qmp_query_measurements(NULL);
+    MeasurementList *info;
+
+    for (info = info_list; info; info = info->next) {
+        monitor_printf(mon, "%02ld: %s\n", info->value->pcr,
+                       info->value->hash);
+    }
+
+    qapi_free_MeasurementList(info_list);
+}
+
 void hmp_qom_list(Monitor *mon, const QDict *qdict)
 {
     const char *path = qdict_get_try_str(qdict, "path");
hmp.h +1 -0
index 0876ec0..6afb1d9 100644
@@ -40,6 +40,7 @@ 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_info_iothreads(Monitor *mon, const QDict *qdict);
+void hmp_info_measurements(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);
hw/core/loader.c +12 -0
index 53e0e41..d7ed110 100644
@@ -55,6 +55,7 @@
 #include "exec/address-spaces.h"
 #include "hw/boards.h"
 #include "qemu/cutils.h"
+#include "hw/misc/measurements.h"
 
 #include <zlib.h>
 
@@ -1026,6 +1027,17 @@ static void rom_reset(void *unused)
     }
 }
 
+void measure_roms(void)
+{
+    Rom *rom;
+    QTAILQ_FOREACH(rom, &roms, next) {
+        if (rom->data == NULL) {
+            continue;
+        }
+        measurements_extend_data(0, rom->data, rom->datasize, rom->name);
+    }
+}
+
 int rom_check_and_register_reset(void)
 {
     hwaddr addr = 0;
hw/i386/acpi-build.c +27 -2
index a26a4bb..c9b5f12 100644
@@ -34,6 +34,7 @@
 #include "hw/acpi/acpi-defs.h"
 #include "hw/acpi/acpi.h"
 #include "hw/acpi/cpu.h"
+#include "hw/misc/measurements.h"
 #include "hw/nvram/fw_cfg.h"
 #include "hw/acpi/bios-linker-loader.h"
 #include "hw/loader.h"
@@ -115,6 +116,7 @@ typedef struct AcpiMiscInfo {
     unsigned dsdt_size;
     uint16_t pvpanic_port;
     uint16_t applesmc_io_base;
+    uint16_t measurements_io_base;
 } AcpiMiscInfo;
 
 typedef struct AcpiBuildPciBusHotplugState {
@@ -211,6 +213,7 @@ static void acpi_get_misc_info(AcpiMiscInfo *info)
     info->tpm_version = tpm_get_version();
     info->pvpanic_port = pvpanic_port();
     info->applesmc_io_base = applesmc_port();
+    info->measurements_io_base = measurements_port();
 }
 
 /*
@@ -2282,6 +2285,26 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
         aml_append(dsdt, scope);
     }
 
+    if (misc->measurements_io_base) {
+        scope = aml_scope("\\_SB.PCI0.ISA");
+        dev = aml_device("PCRS");
+
+        aml_append(dev, aml_name_decl("_HID", aml_string("CORE0001")));
+        /* device present, functioning, decoding, not shown in UI */
+        aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
+
+        crs = aml_resource_template();
+        aml_append(crs,
+               aml_io(AML_DECODE16, misc->measurements_io_base,
+                      misc->measurements_io_base,
+                      0x01, 2)
+        );
+        aml_append(dev, aml_name_decl("_CRS", crs));
+
+        aml_append(scope, dev);
+        aml_append(dsdt, scope);
+    }
+
     sb_scope = aml_scope("\\_SB");
     {
         build_memory_devices(sb_scope, nr_mem, pm->mem_hp_io_base,
@@ -2689,11 +2712,13 @@ void acpi_build(AcpiBuildTables *tables, MachineState *machine)
         acpi_add_table(table_offsets, tables_blob);
         build_hpet(tables_blob, tables->linker);
     }
-    if (misc.tpm_version != TPM_VERSION_UNSPEC) {
+    if (misc.tpm_version != TPM_VERSION_UNSPEC || misc.measurements_io_base) {
         acpi_add_table(table_offsets, tables_blob);
         build_tpm_tcpa(tables_blob, tables->linker, tables->tcpalog);
 
-        if (misc.tpm_version == TPM_VERSION_2_0) {
+        if (misc.measurements_io_base) {
+            measurements_set_log(tables->tcpalog->data);
+        } else if (misc.tpm_version == TPM_VERSION_2_0) {
             acpi_add_table(table_offsets, tables_blob);
             build_tpm2(tables_blob, tables->linker);
         }
hw/misc/Makefile.objs +1 -0
index 4cfbd10..b76e60c 100644
@@ -5,6 +5,7 @@ common-obj-$(CONFIG_ISA_DEBUG) += debugexit.o
 common-obj-$(CONFIG_SGA) += sga.o
 common-obj-$(CONFIG_ISA_TESTDEV) += pc-testdev.o
 common-obj-$(CONFIG_PCI_TESTDEV) += pci-testdev.o
+common-obj-$(CONFIG_MEASUREMENTS) += measurements.o
 
 obj-$(CONFIG_VMPORT) += vmport.o
 
hw/misc/measurements.c new file +295 -0

295 lines not on this page · raw message

hw/misc/measurements.h new file +4 -0
new file mode 100644
index 0000000..7cb8db2
@@ -0,0 +1,4 @@
+#include "hw/sysbus.h"
+
+void measurements_extend_data(int pcrnum, uint8_t *data, size_t len, char *description);
+void measurements_set_log(gchar *log);
include/hw/isa/isa.h +13 -0
index 7693ac5..55e5472 100644
@@ -24,6 +24,9 @@
 #define APPLESMC_MAX_DATA_LENGTH       32
 #define APPLESMC_PROP_IO_BASE "iobase"
 
+#define TYPE_MEASUREMENTS "measurements"
+#define MEASUREMENTS_PROP_IO_BASE "iobase"
+
 static inline uint16_t applesmc_port(void)
 {
     Object *obj = object_resolve_path_type("", TYPE_APPLE_SMC, NULL);
@@ -34,6 +37,16 @@ static inline uint16_t applesmc_port(void)
     return 0;
 }
 
+static inline uint16_t measurements_port(void)
+{
+    Object *obj = object_resolve_path_type("", TYPE_MEASUREMENTS, NULL);
+
+    if (obj) {
+        return object_property_get_int(obj, MEASUREMENTS_PROP_IO_BASE, NULL);
+    }
+    return 0;
+}
+
 #define TYPE_ISADMA "isa-dma"
 
 #define ISADMA_CLASS(klass) \
include/hw/loader.h +1 -0
index 4879b63..cc3157d 100644
@@ -133,6 +133,7 @@ void rom_reset_order_override(void);
 int rom_copy(uint8_t *dest, hwaddr addr, size_t size);
 void *rom_ptr(hwaddr addr);
 void hmp_info_roms(Monitor *mon, const QDict *qdict);
+void measure_roms(void);
 
 #define rom_add_file_fixed(_f, _a, _i)          \
     rom_add_file(_f, NULL, _a, _i, false, NULL)
monitor.c +1 -0
index 5d68a5d..3affc90 100644
@@ -32,6 +32,7 @@
 #include "hw/pci/pci.h"
 #include "sysemu/watchdog.h"
 #include "hw/loader.h"
+#include "hw/misc/measurements.h"
 #include "exec/gdbstub.h"
 #include "net/net.h"
 #include "net/slirp.h"
qapi-schema.json +26 -0
index 5658723..6151fcd 100644
@@ -4338,3 +4338,29 @@
 # Since: 2.7
 ##
 { 'command': 'query-hotpluggable-cpus', 'returns': ['HotpluggableCPU'] }
+
+##
+# @Measurement
+#
+# @pcr: The PCR in the measurement
+# @value: The hash value
+# @vcpus-count: number of logical VCPU threads @HotpluggableCPU provides
+# @qom-path: #optional link to existing CPU object if CPU is present or
+#            omitted if CPU is not present.
+#
+# Since: 2.7
+##
+{ 'struct': 'Measurement',
+  'data': { 'pcr': 'int',
+            'hash': 'str'
+          }
+}
+
+##
+# @query-measurements
+#
+# Returns: a list of Measurement objects
+#
+# Since: 2.7
+##
+{ 'command': 'query-measurements', 'returns': ['Measurement'] }
qmp-commands.hx +20 -0
index c8d360a..a13f939 100644
@@ -5041,3 +5041,23 @@ Example for pc machine type started with
             "props": {"core-id": 0, "socket-id": 0, "thread-id": 0}
          }
        ]}
+
+EQMP
+    {
+        .name       = "query-measurements",
+        .args_type  = "",
+        .mhandler.cmd_new = qmp_marshal_query_measurements,
+    },
+SQMP
+Show system measurements
+------------------------
+
+Arguments: None.
+
+Example:
+
+-> { "execute": "query-measurements" }
+<- {"return": [
+     { "pcr": 0, "hash": "2cfb9f764876a5c7a3a96770fb79043353a5fa38"},
+     { "pcr": 1, "hash": "30b2c318442bd985ce9394ff649056ffde691617"}
+     ]}'
stubs/Makefile.objs +1 -0
index 55edd15..0ec2f7b 100644
@@ -45,3 +45,4 @@ stub-obj-y += iohandler.o
 stub-obj-y += smbios_type_38.o
 stub-obj-y += ipmi.o
 stub-obj-y += pc_madt_cpu_entry.o
+stub-obj-y += measurements.o
stubs/measurements.c new file +18 -0
new file mode 100644
index 0000000..49fc4bd
@@ -0,0 +1,18 @@
+#include "qemu/osdep.h"
+#include "hw/misc/measurements.h"
+#include "qmp-commands.h"
+
+MeasurementList *qmp_query_measurements(Error **errp)
+{
+    return NULL;
+}
+
+void measurements_extend_data(int pcrnum, uint8_t *data, size_t len, char *description)
+{
+    return;
+}
+
+void measurements_set_log(gchar *log)
+{
+    return;
+}
Signature · 1 line
2.7.4
#13 lore raw

On 08/05/2016 05:17 PM, Matthew Garrett wrote:

Generally, we recommend that v2 patches be sent as their own top-level thread, rather than in-reply-to v1, because several tooling scripts get confused and don't look for deep patches.

Quoted 14 lines

Trusted Boot is based around having a trusted store of measurement data and a secure communications channel between that store and an attestation target. In actual hardware, that's a TPM. Since the TPM can only be accessed via the host system, this in turn requires that the TPM be able to perform reasonably complicated cryptographic functions in order to demonstrate its trusted state.

---

This should cover the initial review feedback, with the exception of porting it to MMIO. It seems easier to keep it in port io space on x86, and I'll add MMIO support once it looks like we're happy with this implementation.

Quoted 14 lines
 default-configs/x86_64-softmmu.mak |   1 +
 hmp-commands-info.hx               |  14 ++
 hmp.c                              |  13 ++
 hmp.h                              |   1 +
 hw/core/loader.c                   |  12 ++
 hw/i386/acpi-build.c               |  29 +++-
 hw/misc/Makefile.objs              |   1 +
 hw/misc/measurements.c             | 295 +++++++++++++++++++++++++++++++++++++
 hw/misc/measurements.h             |   4 +
 include/hw/isa/isa.h               |  13 ++
 include/hw/loader.h                |   1 +
 monitor.c                          |   1 +
 qapi-schema.json                   |  26 ++++
 qmp-commands.hx                    |  20 +++

I'm just focusing on the interface review:

hmp.c void hmp_info_iothreads(Monitor *mon, const QDict *qdict) · line 2043

Quoted 8 lines

+++ b/hmp.c

hmp.c +3 -0
@@ -2038,6 +2038,19 @@ void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
     qapi_free_IOThreadInfoList(info_list);
 }
 
+void hmp_info_measurements(Monitor *mon, const QDict *qdict)
+{
+    MeasurementList *info_list = qmp_query_measurements(NULL);

Is it really a good idea to ignore errors?

Quoted 4 lines
hw/misc/measurements.c new file +4 -0
+
+MeasurementList *qmp_query_measurements(Error **errp)
+{
+    MeasurementList *head = NULL;

hw/misc/measurements.c line 270

Quoted 10 lines
hw/misc/measurements.c new file +10 -0
+    MeasurementList **prev = &head;
+    MeasurementList *elem;
+    Measurement *info;
+    Object *obj = object_resolve_path_type("", TYPE_MEASUREMENTS, NULL);
+    MeasurementState *s;
+    int pcr, i;
+
+    if (!obj) {
+        return NULL;
+    }

Returning NULL in a qmp_* function requires that errp be set first.

hw/misc/measurements.c line 277

Quoted 7 lines
hw/misc/measurements.c new file +7 -0
+
+    s = MEASUREMENT(obj);
+
+    for (pcr = 0; pcr < PCR_COUNT; pcr++) {
+        info = g_new0(Measurement, 1);
+        info->pcr = pcr;
+        info->hash = g_malloc0(DIGEST_SIZE*2+1);

Spaces around binary operators, please.

Quoted 14 lines
hw/misc/measurements.c new file +12 -0
+        for (i = 0; i < DIGEST_SIZE; i++) {
+            sprintf(info->hash + i * 2, "%02x", s->measurements[pcr][i]);
+        }
+        elem = g_new0(MeasurementList, 1);
+        elem->value = info;
+        *prev = elem;
+        prev = &elem->next;
+    }
+    return head;
+}
+
 
+++ b/qapi-schema.json

hw/misc/measurements.c line 4345

Quoted 8 lines
hw/misc/measurements.c new file +5 -0
@@ -4338,3 +4338,29 @@
 # Since: 2.7
 ##
 { 'command': 'query-hotpluggable-cpus', 'returns': ['HotpluggableCPU'] }
+
+##
+# @Measurement
+#
+# @pcr: The PCR in the measurement

Might be worth spelling out what the acronym PCR means.

qapi-schema.json line 4349

Quoted 4 lines
qapi-schema.json +4 -0
+# @value: The hash value
+# @vcpus-count: number of logical VCPU threads @HotpluggableCPU provides
+# @qom-path: #optional link to existing CPU object if CPU is present or
+#            omitted if CPU is not present.

Bad copy-and-paste. @pcr is correct, @hash is missing, and @value, @vcpus-count, and @qom-path are wrong.

Review line 4351

Quoted 2 lines
(quoted diff) +2 -0
+#
+# Since: 2.7

You've missed 2.7 hard freeze. This is 2.8 material.

qapi-schema.json line 4363

Quoted 12 lines
qapi-schema.json +12 -0
+##
+{ 'struct': 'Measurement',
+  'data': { 'pcr': 'int',
+            'hash': 'str'
+          }
+}
+
+##
+# @query-measurements
+#
+# Returns: a list of Measurement objects
+#

A little more detail on what a Measurement object represents would be worthwhile. Reading just the .json file gives me no idea why I'd want to query this, or what I'm really getting as a result.

Review line 4364

Quoted 1 line
(quoted diff) +1 -0
+# Since: 2.7

2.8

Quoted 7 lines
(quoted diff) +2 -0
+##
+{ 'command': 'query-measurements', 'returns': ['Measurement'] }
qmp-commands.hx +0 -0
index c8d360a..a13f939 100644

qmp-commands.hx Example for pc machine type started with · line 5049

Quoted 9 lines
qmp-commands.hx +6 -0
@@ -5041,3 +5041,23 @@ Example for pc machine type started with
             "props": {"core-id": 0, "socket-id": 0, "thread-id": 0}
          }
        ]}
+
+EQMP
+    {
+        .name       = "query-measurements",
+        .args_type  = "",
+        .mhandler.cmd_new = qmp_marshal_query_measurements,

This part conflicts with Marc-Andre's patches to kill qmp-commands.hx as redundant. Depending on what goes in first, there will be some rebasing required from the other party.

Quoted 17 lines
qmp-commands.hx +15 -0
+    },
+SQMP
+Show system measurements
+------------------------
+
+Arguments: None.
+
+Example:
+
+-> { "execute": "query-measurements" }
+<- {"return": [
+     { "pcr": 0, "hash": "2cfb9f764876a5c7a3a96770fb79043353a5fa38"},
+     { "pcr": 1, "hash": "30b2c318442bd985ce9394ff649056ffde691617"}
+     ]}'
 
+++ b/stubs/measurements.c

qmp-commands.hx line 7

Quoted 7 lines
qmp-commands.hx +7 -0
@@ -0,0 +1,18 @@
+#include "qemu/osdep.h"
+#include "hw/misc/measurements.h"
+#include "qmp-commands.h"
+
+MeasurementList *qmp_query_measurements(Error **errp)
+{
+    return NULL;

If you return NULL, you should set errp.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#14 lore raw
In reply to Matthew Garrett

Matthew Garrett <[email protected]> wrote on 08/05/2016 07:17:12 PM:

Quoted 2 lines

Trusted Boot is based around having a trusted store of measurement data

and

Quoted 2 lines

a secure communications channel between that store and an attestation target. In actual hardware, that's a TPM. Since the TPM can only be

accessed

Quoted 1 line

via the host system, this in turn requires that the TPM be able to

perform

Quoted 1 line

reasonably complicated cryptographic functions in order to demonstrate

its

Quoted 4 lines

trusted state.

In cloud environments, qemu is inherently trusted and the hypervisor infrastructure provides a trusted mechanism for extracting information

from

Quoted 1 line

qemu and providing it to another system. This means we can skip the

crypto

Quoted 1 line

and stick with the basic functionality - ie, providing a trusted store

of

Quoted 5 lines

measurement data.

This driver provides a very small subset of TPM 1.2 functionality in the form of a bank of registers that can store SHA1 measurements of boot components. Performing a write to one of these registers will append the

new

Quoted 1 line

20 byte hash to the 20 bytes currently stored within the register, take

a

Quoted 1 line

SHA1 of this 40 byte value and then replace the existing register

contents

Quoted 1 line

with the new value. This ensures that a given value can only be obtained

by

Quoted 1 line

performing the same sequence of writes. It also adds a monitor command

to

Quoted 1 line

allow an external agent to extract this information from the running

system

Quoted 1 line

and provide it over a secure communications channel. Finally, it

measures

Quoted 4 lines

each of the loaded ROMs into one of the registers at reset time.

In combination with work in SeaBIOS and the kernel, this permits a fully measured boot in a virtualised environment without the overhead of a

full

Quoted 4 lines

TPM implementation.

This version of the implementation depends on port io, but if there's interest I'll add mmio as well.

Port io is x86 specific, right? I don't think it should stay an x86 specific device.

Quoted 5 lines

---

This should cover the initial review feedback, with the exception of

porting

Quoted 1 line

it to MMIO. It seems easier to keep it in port io space on x86, and I'll

add

Quoted 110 lines

MMIO support once it looks like we're happy with this implementation.

 default-configs/x86_64-softmmu.mak |   1 +
 hmp-commands-info.hx               |  14 ++
 hmp.c                              |  13 ++
 hmp.h                              |   1 +
 hw/core/loader.c                   |  12 ++
 hw/i386/acpi-build.c               |  29 +++-
 hw/misc/Makefile.objs              |   1 +
 hw/misc/measurements.c             | 295 ++++++++++++++++++++++++++
+++++++++++
 hw/misc/measurements.h             |   4 +
 include/hw/isa/isa.h               |  13 ++
 include/hw/loader.h                |   1 +
 monitor.c                          |   1 +
 qapi-schema.json                   |  26 ++++
 qmp-commands.hx                    |  20 +++
 stubs/Makefile.objs                |   1 +
 stubs/measurements.c               |  18 +++
 16 files changed, 448 insertions(+), 2 deletions(-)
 create mode 100644 hw/misc/measurements.c
 create mode 100644 hw/misc/measurements.h
 create mode 100644 stubs/measurements.c
default-configs/ renamed from default-configs/x86_64-softmmu.mak +0 -0

x86_64-softmmu.mak
index 6e3b312..6f0fcc3 100644

default-configs/x86_64-softmmu.mak +1 -0
@@ -58,3 +58,4 @@ CONFIG_IOH3420=y
 CONFIG_I82801B11=y
 CONFIG_SMBIOS=y
 CONFIG_HYPERV_TESTDEV=$(CONFIG_KVM)
+CONFIG_MEASUREMENTS=y
hmp-commands-info.hx +14 -0
index 74446c6..bf1cf67 100644
@@ -816,6 +816,20 @@ STEXI
 Show information about hotpluggable CPUs
 ETEXI
 
+    {
+        .name       = "measurements",
+        .args_type  = "",
+        .params     = "",
+        .help       = "show PCR measurements",
+        .mhandler.cmd = hmp_info_measurements,
+    },
+
+STEXI
+@item info measurements
+@findex measurements
+Show PCR measurements
+ETEXI
+
 STEXI
 @end table
 ETEXI
hmp.c +13 -0
index cc2056e..c2ef869 100644
@@ -2038,6 +2038,19 @@ void hmp_info_iothreads(Monitor *mon, const 
 QDict *qdict)
     qapi_free_IOThreadInfoList(info_list);
 }
 
+void hmp_info_measurements(Monitor *mon, const QDict *qdict)
+{
+    MeasurementList *info_list = qmp_query_measurements(NULL);
+    MeasurementList *info;
+
+    for (info = info_list; info; info = info->next) {
+        monitor_printf(mon, "%02ld: %s\n", info->value->pcr,
+                       info->value->hash);
+    }
+
+    qapi_free_MeasurementList(info_list);
+}
+
 void hmp_qom_list(Monitor *mon, const QDict *qdict)
 {
     const char *path = qdict_get_try_str(qdict, "path");
hmp.h +1 -0
index 0876ec0..6afb1d9 100644
@@ -40,6 +40,7 @@ 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_info_iothreads(Monitor *mon, const QDict *qdict);
+void hmp_info_measurements(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);
hw/core/loader.c +1 -0
index 53e0e41..d7ed110 100644
@@ -55,6 +55,7 @@
 #include "exec/address-spaces.h"
 #include "hw/boards.h"
 #include "qemu/cutils.h"
+#include "hw/misc/measurements.h"
 
 #include <zlib.h>
 
@@ -1026,6 +1027,17 @@ static void rom_reset(void *unused)
     }

hw/core/loader.c static void rom_reset(void *unused) · line 1037

Quoted 10 lines
hw/core/loader.c +8 -0
 }
 
+void measure_roms(void)
+{
+    Rom *rom;
+    QTAILQ_FOREACH(rom, &roms, next) {
+        if (rom->data == NULL) {
+            continue;
+        }
+        measurements_extend_data(0, rom->data, rom->datasize, 

rom->name);

hw/i386/acpi-build.c build_dsdt(GArray *table_data, BIOSLinker

Quoted 35 lines
hw/core/loader.c +3 -0
+    }
+}
+
 int rom_check_and_register_reset(void)
 {
     hwaddr addr = 0;
hw/i386/acpi-build.c +3 -0
index a26a4bb..c9b5f12 100644
@@ -34,6 +34,7 @@
 #include "hw/acpi/acpi-defs.h"
 #include "hw/acpi/acpi.h"
 #include "hw/acpi/cpu.h"
+#include "hw/misc/measurements.h"
 #include "hw/nvram/fw_cfg.h"
 #include "hw/acpi/bios-linker-loader.h"
 #include "hw/loader.h"
@@ -115,6 +116,7 @@ typedef struct AcpiMiscInfo {
     unsigned dsdt_size;
     uint16_t pvpanic_port;
     uint16_t applesmc_io_base;
+    uint16_t measurements_io_base;
 } AcpiMiscInfo;
 
 typedef struct AcpiBuildPciBusHotplugState {
@@ -211,6 +213,7 @@ static void acpi_get_misc_info(AcpiMiscInfo *info)
     info->tpm_version = tpm_get_version();
     info->pvpanic_port = pvpanic_port();
     info->applesmc_io_base = applesmc_port();
+    info->measurements_io_base = measurements_port();
 }
 
 /*
@@ -2282,6 +2285,26 @@ build_dsdt(GArray *table_data, BIOSLinker 

*linker,

Quoted 27 lines
hw/i386/acpi-build.c +20 -0
         aml_append(dsdt, scope);
     }
 
+    if (misc->measurements_io_base) {
+        scope = aml_scope("\\_SB.PCI0.ISA");
+        dev = aml_device("PCRS");
+
+        aml_append(dev, aml_name_decl("_HID", aml_string("CORE0001")));
+        /* device present, functioning, decoding, not shown in UI */
+        aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
+
+        crs = aml_resource_template();
+        aml_append(crs,
+               aml_io(AML_DECODE16, misc->measurements_io_base,
+                      misc->measurements_io_base,
+                      0x01, 2)
+        );
+        aml_append(dev, aml_name_decl("_CRS", crs));
+
+        aml_append(scope, dev);
+        aml_append(dsdt, scope);
+    }
+
     sb_scope = aml_scope("\\_SB");
     {
         build_memory_devices(sb_scope, nr_mem, pm->mem_hp_io_base,

hw/i386/acpi-build.c void acpi_build(AcpiBuildTables *tables, · line 2720

Quoted 10 lines
hw/i386/acpi-build.c +1 -1
@@ -2689,11 +2712,13 @@ void acpi_build(AcpiBuildTables *tables, 
 MachineState *machine)
         acpi_add_table(table_offsets, tables_blob);
         build_hpet(tables_blob, tables->linker);
     }
-    if (misc.tpm_version != TPM_VERSION_UNSPEC) {
+    if (misc.tpm_version != TPM_VERSION_UNSPEC || 
 misc.measurements_io_base) {
         acpi_add_table(table_offsets, tables_blob);
         build_tpm_tcpa(tables_blob, tables->linker, tables->tcpalog);
 

If this device is hitchhiking on the TPM's ACPI tables, then are you also making this device mutually exclusive with the TPM ? Of not please do so.

Quoted 25 lines
hw/i386/acpi-build.c +3 -1
-        if (misc.tpm_version == TPM_VERSION_2_0) {
+        if (misc.measurements_io_base) {
+            measurements_set_log(tables->tcpalog->data);
+        } else if (misc.tpm_version == TPM_VERSION_2_0) {
             acpi_add_table(table_offsets, tables_blob);
             build_tpm2(tables_blob, tables->linker);
         }
hw/misc/Makefile.objs +1 -0
index 4cfbd10..b76e60c 100644
@@ -5,6 +5,7 @@ common-obj-$(CONFIG_ISA_DEBUG) += debugexit.o
 common-obj-$(CONFIG_SGA) += sga.o
 common-obj-$(CONFIG_ISA_TESTDEV) += pc-testdev.o
 common-obj-$(CONFIG_PCI_TESTDEV) += pci-testdev.o
+common-obj-$(CONFIG_MEASUREMENTS) += measurements.o
 
 obj-$(CONFIG_VMPORT) += vmport.o
 
hw/misc/measurements.c new file +0 -0
new file mode 100644
index 0000000..4d55f81

hw/misc/measurements.c line 6

Quoted 6 lines
hw/misc/measurements.c new file +6 -0
@@ -0,0 +1,295 @@
+/*
+ * QEMU boot measurement
+ *
+ * Copyright (c) 2016 CoreOS, Inc <[email protected]>
+ *
+ * Permission is hereby granted, free of charge, to any person 

obtaining a

hw/misc/measurements.c

Quoted 4 lines
hw/misc/measurements.c new file +4 -0
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to 

permit

hw/misc/measurements.c

Quoted 4 lines
hw/misc/measurements.c new file +4 -0
+ * persons to whom the Software is furnished to do so, subject to the
+ * following conditions:
+ *
+ * The above copyright notice and this permission notice shall be 

included

hw/misc/measurements.c

Quoted 3 lines
hw/misc/measurements.c new file +3 -0
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 

EXPRESS

hw/misc/measurements.c line 19

Quoted 2 lines
hw/misc/measurements.c new file +2 -0
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 

NONINFRINGEMENT. IN

hw/misc/measurements.c line 20

Quoted 1 line
hw/misc/measurements.c new file +1 -0
+ * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 

CLAIM,

hw/misc/measurements.c line 21

Quoted 1 line
hw/misc/measurements.c new file +1 -0
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT 

OR

hw/misc/measurements.c line 22

Quoted 1 line
hw/misc/measurements.c new file +1 -0
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 

OR THE

Quoted 60 lines
hw/misc/measurements.c new file +11 -0
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include "qemu/osdep.h"
+#include "crypto/hash.h"
+#include "monitor/monitor.h"
+#include "hw/loader.h"
+#include "hw/isa/isa.h"
+#include "hw/misc/measurements.h"
+#include "qmp-commands.h"
+
+#define MEASUREMENT(obj) OBJECT_CHECK(MeasurementState, (obj), 

TYPE_MEASUREMENTS)

hw/misc/measurements.c new file +45 -0
+
+#define DIGEST_SIZE 20
+#define PCR_COUNT 24
+
+typedef struct MeasurementState MeasurementState;
+
+struct MeasurementState {
+    ISADevice parent_obj;
+    MemoryRegion io_select;
+    MemoryRegion io_value;
+    uint16_t iobase;
+    uint8_t measurements[PCR_COUNT][DIGEST_SIZE];
+    uint8_t tmpmeasurement[DIGEST_SIZE];
+    int write_count;
+    int read_count;
+    uint8_t pcr;
+    int logsize;
+    struct tpm_event *log;
+};
+
+struct tpm_event {
+    uint32_t pcrindex;
+    uint32_t eventtype;
+    uint8_t digest[DIGEST_SIZE];
+    uint32_t eventdatasize;
+    uint8_t event[0];
+};
+
+static Object *measurement_dev_find(void)
+{
+    return object_resolve_path_type("", TYPE_MEASUREMENTS, NULL);
+}
+
+static void measurement_reset(DeviceState *dev)
+{
+    MeasurementState *s = MEASUREMENT(dev);
+
+    s->read_count = 0;
+    s->write_count = 0;
+    s->logsize = 0;
+    memset(s->measurements, 0, sizeof(s->measurements));
+    measure_roms();
+}
+
+static void measurement_select(void *opaque, hwaddr addr, uint64_t 

val, unsigned size)

hw/misc/measurements.c new file +2 -0
+{
+    MeasurementState *s = MEASUREMENT(opaque);

hw/misc/measurements.c

Quoted 10 lines
hw/misc/measurements.c new file +10 -0
+
+    if (val > PCR_COUNT)
+        return;
+
+    s->pcr = val;
+    s->read_count = 0;
+    s->write_count = 0;
+}
+
+static uint64_t measurement_version(void *opaque, hwaddr addr, unsigned 

size)

hw/misc/measurements.c

Quoted 5 lines
hw/misc/measurements.c new file +5 -0
+{
+    return 0;
+}
+
+static uint64_t measurement_read(void *opaque, hwaddr addr, unsigned 

size)

Quoted 64 lines
hw/misc/measurements.c new file +19 -0
+{
+    MeasurementState *s = MEASUREMENT(opaque);
+
+    if (s->read_count == DIGEST_SIZE) {
+        s->read_count = 0;
+    }
+    return s->measurements[s->pcr][s->read_count++];
+}
+
+static void extend(MeasurementState *s, int pcrnum, uint8_t *data)
+{
+    Error *err;
+    char tmpbuf[40];
+    size_t resultlen = 0;
+    uint8_t *result = NULL;
+
+    memcpy(tmpbuf, s->measurements[pcrnum], DIGEST_SIZE);
+    memcpy(tmpbuf + DIGEST_SIZE, data, DIGEST_SIZE);
+    if (qcrypto_hash_bytes(QCRYPTO_HASH_ALG_SHA1, tmpbuf, 40, 

&result, &resultlen, &err) == 0) {

hw/misc/measurements.c new file +11 -0
+        memcpy(s->measurements[pcrnum], result, DIGEST_SIZE);
+    } else {
+        const char *msg = error_get_pretty(err);
+        fprintf(stderr, "Failed to measure data: %s\n", msg);
+        error_free(err);
+    }
+
+    g_free(result);
+}
+
+static void measurement_value(void *opaque, hwaddr addr, uint64_t 

val, unsigned size)

hw/misc/measurements.c new file +11 -0
+{
+    MeasurementState *s = opaque;
+
+    s->tmpmeasurement[s->write_count++] = val;
+    if (s->write_count == DIGEST_SIZE) {
+        extend(s, s->pcr, s->tmpmeasurement);
+        s->write_count = 0;
+    }
+}
+
+static void log_data(MeasurementState *s, int pcrnum, uint8_t 

*hash, char *description)

hw/misc/measurements.c new file +19 -0
+{
+    int eventlen = strlen(description);
+    int entrylen = eventlen + sizeof(struct tpm_event);
+    struct tpm_event *logentry;
+
+    if (!s->log)
+        return;
+
+    logentry = (struct tpm_event *)(((void *)s->log) + s->logsize);
+    logentry->pcrindex = pcrnum;
+    logentry->eventtype = 1;
+    memcpy(logentry->digest, hash, DIGEST_SIZE);
+    logentry->eventdatasize = eventlen;
+    memcpy(logentry->event, description, eventlen);
+
+    s->logsize += entrylen;
+}
+
+void measurements_extend_data(int pcrnum, uint8_t *data, size_t 

len, char *description)

hw/misc/measurements.c

Quoted 6 lines
hw/misc/measurements.c new file +6 -0
+{
+    int ret;
+    Error *err;
+    uint8_t *result;
+    size_t resultlen = 0;
+    Object *obj = object_resolve_path_type("", TYPE_MEASUREMENTS, 

NULL);

Quoted 34 lines
hw/misc/measurements.c new file +6 -0
+
+    if (!obj) {
+        return;
+    }
+
+    ret = qcrypto_hash_bytes(QCRYPTO_HASH_ALG_SHA1, (char *)data, 

len, &result,

hw/misc/measurements.c new file +27 -0
+                             &resultlen, &err);
+    if (ret < 0) {
+        const char *msg = error_get_pretty(err);
+        fprintf(stderr, "Failed to hash extension data: %s\n", msg);
+        return;
+    }
+
+    extend(MEASUREMENT(obj), pcrnum, result);
+    log_data(MEASUREMENT(obj), pcrnum, result, description);
+    g_free(result);
+}
+
+static const MemoryRegionOps measurement_select_ops = {
+    .write = measurement_select,
+    .read = measurement_version,
+    .endianness = DEVICE_NATIVE_ENDIAN,
+    .impl = {
+        .min_access_size = 1,
+        .max_access_size = 1,
+    },
+};
+
+static const MemoryRegionOps measurement_value_ops = {
+    .write = measurement_value,
+    .read = measurement_read,
+    .endianness = DEVICE_NATIVE_ENDIAN,
+    .impl = {

hw/misc/measurements.c

Quoted 10 lines
hw/misc/measurements.c new file +10 -0
+        .min_access_size = 1,
+        .max_access_size = 1,
+    },
+};
+
+static void measurement_realize(DeviceState *dev, Error **errp)
+{
+    MeasurementState *s = MEASUREMENT(dev);
+
+    memory_region_init_io(&s->io_select, OBJECT(s), 

&measurement_select_ops,

hw/misc/measurements.c

Quoted 3 lines
hw/misc/measurements.c new file +3 -0
+                          s, "measurement-select", 1);
+    isa_register_ioport(&s->parent_obj, &s->io_select, s->iobase);
+    memory_region_init_io(&s->io_value, OBJECT(s), 

&measurement_value_ops, s,

hw/misc/measurements.c

Quoted 7 lines
hw/misc/measurements.c new file +7 -0
+                          "measurement-value", 1);
+    isa_register_ioport(&s->parent_obj, &s->io_value, s->iobase + 1);
+    measurement_reset(dev);
+}
+
+static Property measurement_props[] = {
+    DEFINE_PROP_UINT16(MEASUREMENTS_PROP_IO_BASE, MeasurementState, 

iobase,

Quoted 41 lines
hw/misc/measurements.c new file +11 -0
+                       0x620),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static const VMStateDescription measurement_state = {
+    .name = "measurements",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT16(iobase, MeasurementState),
+        VMSTATE_BUFFER_UNSAFE(measurements, MeasurementState, 0, 

PCR_COUNT * DIGEST_SIZE),

hw/misc/measurements.c new file +29 -0
+        VMSTATE_BUFFER(tmpmeasurement, MeasurementState),
+        VMSTATE_INT32(write_count, MeasurementState),
+        VMSTATE_INT32(read_count, MeasurementState),
+        VMSTATE_UINT8(pcr, MeasurementState),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static void measurement_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    dc->realize = measurement_realize;
+    dc->reset = measurement_reset;
+    dc->props = measurement_props;
+    dc->vmsd = &measurement_state;
+    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
+}
+
+static const TypeInfo measurement = {
+    .name          = TYPE_MEASUREMENTS,
+    .parent        = TYPE_ISA_DEVICE,
+    .instance_size = sizeof(MeasurementState),
+    .class_init    = measurement_class_init,
+};
+
+static void measurement_register_types(void)
+{
+    type_register_static(&measurement);
+}

hw/misc/measurements.c

Quoted 10 lines
hw/misc/measurements.c new file +10 -0
+
+type_init(measurement_register_types);
+
+MeasurementList *qmp_query_measurements(Error **errp)
+{
+    MeasurementList *head = NULL;
+    MeasurementList **prev = &head;
+    MeasurementList *elem;
+    Measurement *info;
+    Object *obj = object_resolve_path_type("", TYPE_MEASUREMENTS, 

NULL);

Quoted 5 lines
hw/misc/measurements.c new file +5 -0
+    MeasurementState *s;
+    int pcr, i;
+
+    if (!obj) {
+        return NULL;

hw/misc/measurements.c

Quoted 10 lines
hw/misc/measurements.c new file +10 -0
+    }
+
+    s = MEASUREMENT(obj);
+
+    for (pcr = 0; pcr < PCR_COUNT; pcr++) {
+        info = g_new0(Measurement, 1);
+        info->pcr = pcr;
+        info->hash = g_malloc0(DIGEST_SIZE*2+1);
+        for (i = 0; i < DIGEST_SIZE; i++) {
+            sprintf(info->hash + i * 2, "%02x", 

s->measurements[pcr][i]);

Quoted 42 lines
hw/misc/measurements.c new file +16 -0
+        }
+        elem = g_new0(MeasurementList, 1);
+        elem->value = info;
+        *prev = elem;
+        prev = &elem->next;
+    }
+    return head;
+}
+
+void measurements_set_log(gchar *log)
+{
+    Object *obj = measurement_dev_find();
+    MeasurementState *s = MEASUREMENT(obj);
+
+    s->log = (struct tpm_event *)log;
+}
hw/misc/measurements.h new file +3 -0
new file mode 100644
index 0000000..7cb8db2
@@ -0,0 +1,4 @@
+#include "hw/sysbus.h"
+
+void measurements_extend_data(int pcrnum, uint8_t *data, size_t 

len, char *description);

hw/misc/measurements.h new file +1 -0
+void measurements_set_log(gchar *log);
include/hw/isa/isa.h +3 -0
index 7693ac5..55e5472 100644
@@ -24,6 +24,9 @@
 #define APPLESMC_MAX_DATA_LENGTH       32
 #define APPLESMC_PROP_IO_BASE "iobase"
 
+#define TYPE_MEASUREMENTS "measurements"
+#define MEASUREMENTS_PROP_IO_BASE "iobase"
+
 static inline uint16_t applesmc_port(void)
 {
     Object *obj = object_resolve_path_type("", TYPE_APPLE_SMC, NULL);

include/hw/isa/isa.h static inline uint16_t applesmc_port(void) · line 42

Quoted 6 lines
include/hw/isa/isa.h +3 -0
@@ -34,6 +37,16 @@ static inline uint16_t applesmc_port(void)
     return 0;
 }
 
+static inline uint16_t measurements_port(void)
+{
+    Object *obj = object_resolve_path_type("", TYPE_MEASUREMENTS, 

NULL);

include/hw/isa/isa.h static inline uint16_t applesmc_port(void)

Quoted 3 lines
include/hw/isa/isa.h +3 -0
+
+    if (obj) {
+        return object_property_get_int(obj, MEASUREMENTS_PROP_IO_BASE, 

NULL);

Quoted 36 lines
include/hw/isa/isa.h +4 -0
+    }
+    return 0;
+}
+
 #define TYPE_ISADMA "isa-dma"
 
 #define ISADMA_CLASS(klass) \
include/hw/loader.h +1 -0
index 4879b63..cc3157d 100644
@@ -133,6 +133,7 @@ void rom_reset_order_override(void);
 int rom_copy(uint8_t *dest, hwaddr addr, size_t size);
 void *rom_ptr(hwaddr addr);
 void hmp_info_roms(Monitor *mon, const QDict *qdict);
+void measure_roms(void);
 
 #define rom_add_file_fixed(_f, _a, _i)          \
     rom_add_file(_f, NULL, _a, _i, false, NULL)
monitor.c +1 -0
index 5d68a5d..3affc90 100644
@@ -32,6 +32,7 @@
 #include "hw/pci/pci.h"
 #include "sysemu/watchdog.h"
 #include "hw/loader.h"
+#include "hw/misc/measurements.h"
 #include "exec/gdbstub.h"
 #include "net/net.h"
 #include "net/slirp.h"
qapi-schema.json +0 -0
index 5658723..6151fcd 100644

qapi-schema.json line 4340

Quoted 3 lines
qapi-schema.json +0 -0
@@ -4338,3 +4338,29 @@
 # Since: 2.7
 ##
 { 'command': 'query-hotpluggable-cpus', 'returns': ['HotpluggableCPU'] 

}

qapi-schema.json

Quoted 7 lines
qapi-schema.json +7 -0
+
+##
+# @Measurement
+#
+# @pcr: The PCR in the measurement
+# @value: The hash value
+# @vcpus-count: number of logical VCPU threads @HotpluggableCPU 

provides

Quoted 84 lines
qapi-schema.json +19 -0
+# @qom-path: #optional link to existing CPU object if CPU is present or
+#            omitted if CPU is not present.
+#
+# Since: 2.7
+##
+{ 'struct': 'Measurement',
+  'data': { 'pcr': 'int',
+            'hash': 'str'
+          }
+}
+
+##
+# @query-measurements
+#
+# Returns: a list of Measurement objects
+#
+# Since: 2.7
+##
+{ 'command': 'query-measurements', 'returns': ['Measurement'] }
qmp-commands.hx +20 -0
index c8d360a..a13f939 100644
@@ -5041,3 +5041,23 @@ Example for pc machine type started with
             "props": {"core-id": 0, "socket-id": 0, "thread-id": 0}
          }
        ]}
+
+EQMP
+    {
+        .name       = "query-measurements",
+        .args_type  = "",
+        .mhandler.cmd_new = qmp_marshal_query_measurements,
+    },
+SQMP
+Show system measurements
+------------------------
+
+Arguments: None.
+
+Example:
+
+-> { "execute": "query-measurements" }
+<- {"return": [
+     { "pcr": 0, "hash": "2cfb9f764876a5c7a3a96770fb79043353a5fa38"},
+     { "pcr": 1, "hash": "30b2c318442bd985ce9394ff649056ffde691617"}
+     ]}'
stubs/Makefile.objs +1 -0
index 55edd15..0ec2f7b 100644
@@ -45,3 +45,4 @@ stub-obj-y += iohandler.o
 stub-obj-y += smbios_type_38.o
 stub-obj-y += ipmi.o
 stub-obj-y += pc_madt_cpu_entry.o
+stub-obj-y += measurements.o
stubs/measurements.c new file +10 -0
new file mode 100644
index 0000000..49fc4bd
@@ -0,0 +1,18 @@
+#include "qemu/osdep.h"
+#include "hw/misc/measurements.h"
+#include "qmp-commands.h"
+
+MeasurementList *qmp_query_measurements(Error **errp)
+{
+    return NULL;
+}
+
+void measurements_extend_data(int pcrnum, uint8_t *data, size_t 

len, char *description)

stubs/measurements.c new file +8 -0
+{
+    return;
+}
+
+void measurements_set_log(gchar *log)
+{
+    return;
+}
Signature · 1 line
2.7.4
#15 lore raw
Quoted 6 lines · Stefan Berger

Matthew Garrett <[email protected]> wrote on 08/05/2016 07:17:12 PM:

Quoted 2 lines

This version of the implementation depends on port io, but if there's interest I'll add mmio as well.

Port io is x86 specific, right? I don't think it should stay an x86 specific device.

Not strictly, there are other architectures that implement it. But yes, most other platforms will want MMIO support. The intent is certainly to add that.

Quoted 9 lines
Quoted 6 lines
(quoted diff) +1 -1
-    if (misc.tpm_version != TPM_VERSION_UNSPEC) {
+    if (misc.tpm_version != TPM_VERSION_UNSPEC ||
misc.measurements_io_base) {
         acpi_add_table(table_offsets, tables_blob);
         build_tpm_tcpa(tables_blob, tables->linker, tables->tcpalog);

If this device is hitchhiking on the TPM's ACPI tables, then are you also making this device mutually exclusive with the TPM ? Of not please do so.

I'll look into the best way to do that.

#16 lore raw

Matthew Garrett <[email protected]> wrote on 08/08/2016 03:43:57 PM:

Quoted 2 lines

On Fri, Aug 5, 2016 at 8:56 PM, Stefan Berger <[email protected]>

Quoted 5 lines · wrote:
Quoted 5 lines

Matthew Garrett <[email protected]> wrote on 08/05/2016 07:17:12 PM:

Quoted 2 lines

This version of the implementation depends on port io, but if there's interest I'll add mmio as well.

Port io is x86 specific, right? I don't think it should stay an x86

specific

Quoted 11 lines
Quoted 1 line

device.

Not strictly, there are other architectures that implement it. But yes, most other platforms will want MMIO support. The intent is certainly to add that.

Quoted 5 lines
Quoted 5 lines
(quoted diff) +1 -1
-    if (misc.tpm_version != TPM_VERSION_UNSPEC) {
+    if (misc.tpm_version != TPM_VERSION_UNSPEC ||
misc.measurements_io_base) {
         acpi_add_table(table_offsets, tables_blob);
         build_tpm_tcpa(tables_blob, tables->linker, 

tables->tcpalog);

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

If this device is hitchhiking on the TPM's ACPI tables, then are you

also

Quoted 1 line
Quoted 1 line

making this device mutually exclusive with the TPM ? Of not please do

so.

Quoted 3 lines

I'll look into the best way to do that.

What's the plan for the driver level in Linux? Are you going to have this device's measurement log appear under another path than the TPM path /sys/kernel/security/tpm0/{ascii|binary}_bios_measurements? Also, I hope that not /dev/tpm0 will be made available. Basically I think it is a mess to use another device's ACPI table while not being compatible at all. Though a measurement list is necessary for the state of the PCRs to make sense.