Skip to contentSkip to conversation

fw_cfg: Add edk2_add_host_crypto_policy()

PATCH Superseded Philippe Mathieu-Daudé 3 participants 8 messages Mar 12, 2019 – Mar 13, 2019

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover fw_cfg: Add edk2_add_host_crypto_policy()

Hi,

This series consists of:
- add fw_cfg_add_file_from_host()
- add edk2_add_host_crypto_policy() and the Edk2Crypto object

The Edk2Crypto object is used to hold configuration values specific to EDK2.

The edk2_add_host_crypto_policy() function loads crypto policies from the host, and register them as fw_cfg named file items.

So far only the 'https' policy is supported.

A usercase example is the 'HTTPS Boof' feature of OVMF [*].

Usage example:

$ qemu-system-x86_64 \
    --object edk2_crypto,id=https,\
        ciphers=/etc/crypto-policies/back-ends/openssl.config,\
        cacerts=/etc/pki/ca-trust/extracted/edk2/cacerts.bin

(On Fedora these files are provided by the ca-certificates and crypto-policies packages).

[*]: https://github.com/tianocore/edk2/blob/master/OvmfPkg/README

Since v3:
- Addressed Markus' comments (do not care about heap) Since v2:
- Split of
Since v1:
- Addressed Michael and Laszlo comments.

Please review,

Phil.

$ git backport-diff -u fw_cfg_edk2_crypto_policies-v3
Key:
[####] : number of functional differences between upstream/downstream patch
The flags [FC] indicate (F)unctional and (C)ontextual differences, respectively

001/4:[0022] [FC] 'hw/nvram/fw_cfg: Add fw_cfg_add_file_from_host()' 002/4:[0085] [FC] 'hw/firmware: Add Edk2Crypto and edk2_add_host_crypto_policy()' 003/4:[0002] [FC] 'hw/i386: Use edk2_add_host_crypto_policy()' 004/4:[0002] [FC] 'hw/arm/virt: Use edk2_add_host_crypto_policy()'

v3: https://lists.gnu.org/archive/html/qemu-devel/2019-03/msg02965.html
v2: https://lists.gnu.org/archive/html/qemu-devel/2019-03/msg02522.html
v1: https://lists.gnu.org/archive/html/qemu-devel/2018-12/msg01598.html

Philippe Mathieu-Daudé (4):
hw/nvram/fw_cfg: Add fw_cfg_add_file_from_host() hw/firmware: Add Edk2Crypto and edk2_add_host_crypto_policy() hw/i386: Use edk2_add_host_crypto_policy() hw/arm/virt: Use edk2_add_host_crypto_policy()

 MAINTAINERS                             |   8 ++
 hw/Makefile.objs                        |   1 +
 hw/arm/virt.c                           |   7 +
 hw/firmware/Makefile.objs               |   1 +
 hw/firmware/uefi_edk2_crypto_policies.c | 182 ++++++++++++++++++++++++
 hw/i386/pc.c                            |   7 +
 hw/nvram/fw_cfg.c                       |  23 +++
 include/hw/firmware/uefi_edk2.h         |  30 ++++
 include/hw/nvram/fw_cfg.h               |  25 ++++
 9 files changed, 284 insertions(+)
 create mode 100644 hw/firmware/Makefile.objs
 create mode 100644 hw/firmware/uefi_edk2_crypto_policies.c
 create mode 100644 include/hw/firmware/uefi_edk2.h
Signature · 1 line
2.20.1
#1 lore raw

PATCH v4 1/4 hw/nvram/fw_cfg: Add fw_cfg_add_file_from_host()

Add a function to read the full content of file on the host, and add a new 'file' name item to the fw_cfg device.

---
v2: s/ptr/data, corrected documentation (Laszlo)
v3: inverted the if() logic
v4: does not return pointer to alloc'd data (Markus)

---
 hw/nvram/fw_cfg.c         | 23 +++++++++++++++++++++++
 include/hw/nvram/fw_cfg.h | 25 +++++++++++++++++++++++++
 2 files changed, 48 insertions(+)
hw/nvram/fw_cfg.c +23 -0
index 5c3a46ce6f..a8fb829162 100644
@@ -825,6 +825,29 @@ void fw_cfg_add_file(FWCfgState *s,  const char *filename,
     fw_cfg_add_file_callback(s, filename, NULL, NULL, NULL, data, len, true);
 }
 
+bool fw_cfg_add_file_from_host(FWCfgState *s, const char *filename,
+                               const char *host_path, size_t *len,
+                               Error **errp)
+{
+    GError *gerr = NULL;
+    gchar *data = NULL;
+    gsize contents_len = 0;
+
+    if (!g_file_get_contents(host_path, &data, &contents_len, &gerr)) {
+        error_setg(errp, "%s", gerr->message);
+        g_error_free(gerr);
+        return false;
+    }
+    fw_cfg_add_file(s, filename, data, contents_len);
+    /* TODO g_free 'data' */
+
+    if (len) {
+        *len = contents_len;
+    }
+
+    return true;
+}
+
 void *fw_cfg_modify_file(FWCfgState *s, const char *filename,
                         void *data, size_t len)
 {
include/hw/nvram/fw_cfg.h +25 -0
index f5a6895a74..4c8cdd89bb 100644
@@ -166,6 +166,31 @@ void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value);
 void fw_cfg_add_file(FWCfgState *s, const char *filename, void *data,
                      size_t len);
 
+/**
+ * fw_cfg_add_file_from_host:
+ * @s: fw_cfg device being modified
+ * @filename: name of new fw_cfg file item
+ * @host_path: path of the host file to read the data from
+ * @len: pointer to hold the length of the host file (optional)
+ * @errp: pointer to a NULL initialized error object
+ *
+ * Read the content of a host file as a raw "blob" then add a new NAMED
+ * fw_cfg item of the file size. If @len is provided, it will contain the
+ * total length read from the host file. The data read from the host
+ * filesystem is owned by the new fw_cfg entry, and is stored into the data
+ * structure of the fw_cfg device.
+ * The next available (unused) selector key starting at FW_CFG_FILE_FIRST
+ * will be used; also, a new entry will be added to the file directory
+ * structure residing at key value FW_CFG_FILE_DIR, containing the item name,
+ * data size, and assigned selector key value.
+ *
+ * Returns true on success, false on failure. In the latter case,
+ * an Error object is returned through @errp.
+ */
+bool fw_cfg_add_file_from_host(FWCfgState *s, const char *filename,
+                               const char *host_path, size_t *len,
+                               Error **errp);
+
 /**
  * fw_cfg_add_file_callback:
  * @s: fw_cfg device being modified
Signature · 1 line
2.20.1
#2 lore raw

PATCH v4 2/4 hw/firmware: Add Edk2Crypto and edk2_add_host_crypto_policy()

In reply to Philippe Mathieu-Daudé

The Edk2Crypto object is used to hold configuration values specific to EDK2.

The edk2_add_host_crypto_policy() function loads crypto policies from the host, and register them as fw_cfg named file items. So far only the 'https' policy is supported.

A usercase example is the 'HTTPS Boof' feature of OVMF [*].

Usage example:

  $ qemu-system-x86_64 \
      --object edk2_crypto,id=https,\
              ciphers=/etc/crypto-policies/back-ends/openssl.config,\
              cacerts=/etc/pki/ca-trust/extracted/edk2/cacerts.bin

(On Fedora these files are provided by the ca-certificates and crypto-policies packages).

[*]: https://github.com/tianocore/edk2/blob/master/OvmfPkg/README

---
v3:
- '-object' -> '--object' in commit description (Eric)
- reworded the 'TODO: g_free' comment
v4:
- INTERFACE_CHECK -> OBJECT_CLASS_CHECK (Markus)
- path -> filename (Markus)
- dropped the 'TODO: g_free' comment (Markus)
- only allow 1 singleton (Markus)

---
 MAINTAINERS                             |   8 ++
 hw/Makefile.objs                        |   1 +
 hw/firmware/Makefile.objs               |   1 +
 hw/firmware/uefi_edk2_crypto_policies.c | 182 ++++++++++++++++++++++++
 include/hw/firmware/uefi_edk2.h         |  30 ++++
 5 files changed, 222 insertions(+)
 create mode 100644 hw/firmware/Makefile.objs
 create mode 100644 hw/firmware/uefi_edk2_crypto_policies.c
 create mode 100644 include/hw/firmware/uefi_edk2.h
MAINTAINERS +8 -0
index d326756079..1d6c30cab4 100644
@@ -2207,6 +2207,14 @@ F: include/hw/i2c/smbus_master.h
 F: include/hw/i2c/smbus_slave.h
 F: include/hw/i2c/smbus_eeprom.h
 
+EDK2 Firmware
+M: Laszlo Ersek <[email protected]>
+M: Philippe Mathieu-Daudé <[email protected]>
+S: Maintained
+F: docs/interop/firmware.json
+F: hw/firmware/uefi_edk2_crypto_policies.c
+F: include/hw/firmware/uefi_edk2.h
+
 Usermode Emulation
 ------------------
 Overall
hw/Makefile.objs +1 -0
index 82aa7fab8e..2b075aa1e0 100644
@@ -8,6 +8,7 @@ devices-dirs-$(CONFIG_SOFTMMU) += char/
 devices-dirs-$(CONFIG_SOFTMMU) += cpu/
 devices-dirs-$(CONFIG_SOFTMMU) += display/
 devices-dirs-$(CONFIG_SOFTMMU) += dma/
+devices-dirs-$(CONFIG_SOFTMMU) += firmware/
 devices-dirs-$(CONFIG_SOFTMMU) += gpio/
 devices-dirs-$(CONFIG_HYPERV) += hyperv/
 devices-dirs-$(CONFIG_I2C) += i2c/
hw/firmware/Makefile.objs new file +1 -0
new file mode 100644
index 0000000000..ea1f6d44df
@@ -0,0 +1 @@
+common-obj-y += uefi_edk2_crypto_policies.o
hw/firmware/uefi_edk2_crypto_policies.c new file +182 -0

182 lines not on this page · raw message

include/hw/firmware/uefi_edk2.h new file +30 -0
new file mode 100644
index 0000000000..ff4a5439ce
@@ -0,0 +1,30 @@
+/*
+ * UEFI EDK2 Support
+ *
+ * Copyright (c) 2019 Red Hat Inc.
+ *
+ * Author:
+ *  Philippe Mathieu-Daudé <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef HW_FIRMWARE_UEFI_EDK2_H
+#define HW_FIRMWARE_UEFI_EDK2_H
+
+#include "hw/nvram/fw_cfg.h"
+
+/**
+ * edk2_add_host_crypto_policy:
+ * @s: fw_cfg device being modified
+ * @errp: pointer to a NULL initialized error object
+ *
+ * Add a new named file containing the host crypto policy.
+ *
+ * Returns true on success, false on failure. In the latter case,
+ * an Error object is returned through @errp.
+ */
+bool edk2_add_host_crypto_policy(FWCfgState *fw_cfg, Error **errp);
+
+#endif /* HW_FIRMWARE_UEFI_EDK2_H */
Signature · 1 line
2.20.1
#3 lore raw

PATCH v4 3/4 hw/i386: Use edk2_add_host_crypto_policy()

In reply to Philippe Mathieu-Daudé

Enable the EDK2 Crypto Policy features on the PC machine.

---
 hw/i386/pc.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/i386/pc.c +7 -0
index 1cdaff5f4d..f0bef84ccf 100644
@@ -38,6 +38,7 @@
 #include "hw/nvram/fw_cfg.h"
 #include "hw/timer/hpet.h"
 #include "hw/firmware/smbios.h"
+#include "hw/firmware/uefi_edk2.h"
 #include "hw/loader.h"
 #include "elf.h"
 #include "multiboot.h"
@@ -1047,6 +1048,11 @@ static FWCfgState *bochs_bios_init(AddressSpace *as, PCMachineState *pcms)
     return fw_cfg;
 }
 
+static void pc_uefi_setup(PCMachineState *pcms)
+{
+    edk2_add_host_crypto_policy(pcms->fw_cfg, &error_fatal);
+}
+
 static long get_file_size(FILE *f)
 {
     long where, size;
@@ -1651,6 +1657,7 @@ void pc_machine_done(Notifier *notifier, void *data)
     if (pcms->fw_cfg) {
         pc_build_smbios(pcms);
         pc_build_feature_control_file(pcms);
+        pc_uefi_setup(pcms);
         /* update FW_CFG_NB_CPUS to account for -device added CPUs */
         fw_cfg_modify_i16(pcms->fw_cfg, FW_CFG_NB_CPUS, pcms->boot_cpus);
     }
Signature · 1 line
2.20.1
#4 lore raw

PATCH v4 4/4 hw/arm/virt: Use edk2_add_host_crypto_policy()

In reply to Philippe Mathieu-Daudé

Enable the EDK2 Crypto Policy features on the Virt machine.

---
 hw/arm/virt.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/arm/virt.c +7 -0
index ce2664a30b..927d4ff31a 100644
@@ -56,6 +56,7 @@
 #include "hw/intc/arm_gicv3_common.h"
 #include "kvm_arm.h"
 #include "hw/firmware/smbios.h"
+#include "hw/firmware/uefi_edk2.h"
 #include "qapi/visitor.h"
 #include "standard-headers/linux/input.h"
 #include "hw/arm/smmuv3.h"
@@ -1301,6 +1302,11 @@ static void virt_build_smbios(VirtMachineState *vms)
     }
 }
 
+static void virt_uefi_setup(VirtMachineState *vms)
+{
+    edk2_add_host_crypto_policy(vms->fw_cfg, &error_fatal);
+}
+
 static
 void virt_machine_done(Notifier *notifier, void *data)
 {
@@ -1329,6 +1335,7 @@ void virt_machine_done(Notifier *notifier, void *data)
 
     virt_acpi_setup(vms);
     virt_build_smbios(vms);
+    virt_uefi_setup(vms);
 }
 
 static uint64_t virt_cpu_mp_affinity(VirtMachineState *vms, int idx)
Signature · 1 line
2.20.1
Reviewed-by on the v4 series
#5 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 28 lines · Philippe Mathieu-Daudé

Hi,

This series consists of:
- add fw_cfg_add_file_from_host()
- add edk2_add_host_crypto_policy() and the Edk2Crypto object

The Edk2Crypto object is used to hold configuration values specific to EDK2.

The edk2_add_host_crypto_policy() function loads crypto policies from the host, and register them as fw_cfg named file items.

So far only the 'https' policy is supported.

A usercase example is the 'HTTPS Boof' feature of OVMF [*].

Usage example:

$ qemu-system-x86_64 \
    --object edk2_crypto,id=https,\
        ciphers=/etc/crypto-policies/back-ends/openssl.config,\
        cacerts=/etc/pki/ca-trust/extracted/edk2/cacerts.bin

(On Fedora these files are provided by the ca-certificates and crypto-policies packages).

[*]: https://github.com/tianocore/edk2/blob/master/OvmfPkg/README

Quoted 8 lines

Since v3:
- Addressed Markus' comments (do not care about heap) Since v2:
- Split of
Since v1:
- Addressed Michael and Laszlo comments.

Please review,

I can't pass judgement on the feature's utility, but the code looks sane to me.

#6 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 28 lines · Philippe Mathieu-Daudé

Hi,

This series consists of:
- add fw_cfg_add_file_from_host()
- add edk2_add_host_crypto_policy() and the Edk2Crypto object

The Edk2Crypto object is used to hold configuration values specific to EDK2.

The edk2_add_host_crypto_policy() function loads crypto policies from the host, and register them as fw_cfg named file items.

So far only the 'https' policy is supported.

A usercase example is the 'HTTPS Boof' feature of OVMF [*].

Usage example:

$ qemu-system-x86_64 \
    --object edk2_crypto,id=https,\
        ciphers=/etc/crypto-policies/back-ends/openssl.config,\
        cacerts=/etc/pki/ca-trust/extracted/edk2/cacerts.bin

(On Fedora these files are provided by the ca-certificates and crypto-policies packages).

[*]: https://github.com/tianocore/edk2/blob/master/OvmfPkg/README

Quoted 8 lines

Since v3:
- Addressed Markus' comments (do not care about heap) Since v2:
- Split of
Since v1:
- Addressed Michael and Laszlo comments.

Please review,

sorry about seeing this just now. My email load has been extreme, and the only way I can deal with it is a *strict* FIFO approach. It gives me unparalleled throughput [*] and acceptable latency, but it does cause me to respond to out-of-date emails on occasion.

[*] "unparalleled" not by other people, but by other workflows I could choose

Nevertheless I think my comments under v3 apply just the same here.

Thanks
Laszlo

#7 lore raw
Quoted 43 lines · Laszlo Ersek
Quoted 28 lines · Philippe Mathieu-Daudé

Hi,

This series consists of:
- add fw_cfg_add_file_from_host()
- add edk2_add_host_crypto_policy() and the Edk2Crypto object

The Edk2Crypto object is used to hold configuration values specific to EDK2.

The edk2_add_host_crypto_policy() function loads crypto policies from the host, and register them as fw_cfg named file items.

So far only the 'https' policy is supported.

A usercase example is the 'HTTPS Boof' feature of OVMF [*].

Usage example:

$ qemu-system-x86_64 \
    --object edk2_crypto,id=https,\
        ciphers=/etc/crypto-policies/back-ends/openssl.config,\
        cacerts=/etc/pki/ca-trust/extracted/edk2/cacerts.bin

(On Fedora these files are provided by the ca-certificates and crypto-policies packages).

[*]: https://github.com/tianocore/edk2/blob/master/OvmfPkg/README

Quoted 8 lines

Since v3:
- Addressed Markus' comments (do not care about heap) Since v2:
- Split of
Since v1:
- Addressed Michael and Laszlo comments.

Please review,

sorry about seeing this just now. My email load has been extreme, and the only way I can deal with it is a *strict* FIFO approach. It gives me unparalleled throughput [*] and acceptable latency, but it does cause me to respond to out-of-date emails on occasion.

Quoted 4 lines

[*] "unparalleled" not by other people, but by other workflows I could choose

Nevertheless I think my comments under v3 apply just the same here.

Yes, I'll work on v5 based on your v3 comments.

Thanks!

Phil.