6ae9ed
From 155f32761f7f6569fde7c4c718169fb4f514d2c3 Mon Sep 17 00:00:00 2001
6ae9ed
Message-Id: <155f32761f7f6569fde7c4c718169fb4f514d2c3@dist-git>
6ae9ed
From: John Ferlan <jferlan@redhat.com>
6ae9ed
Date: Mon, 25 Jul 2016 12:42:48 -0400
6ae9ed
Subject: [PATCH] encryption: Add luks parsing for storageencryption
6ae9ed
6ae9ed
https://bugzilla.redhat.com/show_bug.cgi?id=1301021
6ae9ed
6ae9ed
Add parse and format of the luks/passphrase secret including tests for
6ae9ed
volume XML parsing.
6ae9ed
6ae9ed
Signed-off-by: John Ferlan <jferlan@redhat.com>
6ae9ed
(cherry picked from commit 9bbf0d7e6457e5a0668017eb8e17276f747fc9cc)
6ae9ed
6ae9ed
Resolved conflict in docs/formatsecret.html.in since commit id
6ae9ed
c84380106fe69f86842dc7e8c032af6740bef77e is not present downstream
6ae9ed
6ae9ed
Signed-off-by: John Ferlan <jferlan@redhat.com>
6ae9ed
---
6ae9ed
 docs/formatstorageencryption.html.in               | 26 ++++++++++++-
6ae9ed
 docs/schemas/storagecommon.rng                     |  2 +
6ae9ed
 src/qemu/qemu_process.c                            |  6 +++
6ae9ed
 src/storage/storage_backend.c                      |  3 +-
6ae9ed
 src/storage/storage_backend_fs.c                   |  7 +++-
6ae9ed
 src/storage/storage_backend_gluster.c              |  2 +
6ae9ed
 src/util/virstorageencryption.c                    |  2 +-
6ae9ed
 src/util/virstorageencryption.h                    |  1 +
6ae9ed
 tests/qemuxml2argvdata/qemuxml2argv-luks-disks.xml | 45 ++++++++++++++++++++++
6ae9ed
 .../qemuxml2xmlout-luks-disks.xml                  |  1 +
6ae9ed
 tests/qemuxml2xmltest.c                            |  1 +
6ae9ed
 tests/storagevolxml2xmlin/vol-luks.xml             | 21 ++++++++++
6ae9ed
 tests/storagevolxml2xmlout/vol-luks.xml            | 21 ++++++++++
6ae9ed
 tests/storagevolxml2xmltest.c                      |  1 +
6ae9ed
 14 files changed, 134 insertions(+), 5 deletions(-)
6ae9ed
 create mode 100644 tests/qemuxml2argvdata/qemuxml2argv-luks-disks.xml
6ae9ed
 create mode 120000 tests/qemuxml2xmloutdata/qemuxml2xmlout-luks-disks.xml
6ae9ed
 create mode 100644 tests/storagevolxml2xmlin/vol-luks.xml
6ae9ed
 create mode 100644 tests/storagevolxml2xmlout/vol-luks.xml
6ae9ed
6ae9ed
diff --git a/docs/formatstorageencryption.html.in b/docs/formatstorageencryption.html.in
6ae9ed
index 58e1073..380763e 100644
6ae9ed
--- a/docs/formatstorageencryption.html.in
6ae9ed
+++ b/docs/formatstorageencryption.html.in
6ae9ed
@@ -56,8 +56,20 @@
6ae9ed
       the secret element is not present during volume creation,
6ae9ed
       a secret is automatically generated and attached to the volume.
6ae9ed
     

6ae9ed
+    

"luks" format

6ae9ed
+    

6ae9ed
+      The luks format is specific to a luks encrypted volume
6ae9ed
+      and the secret used in order to either encrypt or decrypt the volume.
6ae9ed
+      A single <secret type='passphrase'...> element is
6ae9ed
+      expected. The secret may be referenced via either a uuid or
6ae9ed
+      usage attribute. One of the two must be present. When
6ae9ed
+      present for volume creation, the secret will be used in order for
6ae9ed
+      volume encryption.  When present for domain usage, the secret will
6ae9ed
+      be used as the passphrase to decrypt the volume.
6ae9ed
+      Since 2.1.0.
6ae9ed
+    

6ae9ed
 
6ae9ed
-    

Example

6ae9ed
+    

Examples

6ae9ed
 
6ae9ed
     

6ae9ed
       Here is a simple example, specifying use of the qcow format:
6ae9ed
@@ -67,5 +79,17 @@
6ae9ed
       <encryption format='qcow'>
6ae9ed
          <secret type='passphrase' uuid='c1f11a6d-8c5d-4a3e-ac7a-4e171c5e0d4a' />
6ae9ed
       </encryption>
6ae9ed
+
6ae9ed
+    

6ae9ed
+      Here is a simple example, specifying use of the luks format
6ae9ed
+      where it's assumed that a secret has been defined using a
6ae9ed
+      usage element with a id of "luks_example":
6ae9ed
+    

6ae9ed
+    
6ae9ed
+      <encryption format='luks'>
6ae9ed
+         <secret type='passphrase' usage='luks_example'/>
6ae9ed
+      </encryption>
6ae9ed
+    
6ae9ed
+
6ae9ed
   </body>
6ae9ed
 </html>
6ae9ed
diff --git a/docs/schemas/storagecommon.rng b/docs/schemas/storagecommon.rng
6ae9ed
index c5b71de..63b55b4 100644
6ae9ed
--- a/docs/schemas/storagecommon.rng
6ae9ed
+++ b/docs/schemas/storagecommon.rng
6ae9ed
@@ -12,6 +12,7 @@
6ae9ed
         <choice>
6ae9ed
           <value>default</value>
6ae9ed
           <value>qcow</value>
6ae9ed
+          <value>luks</value>
6ae9ed
         </choice>
6ae9ed
       </attribute>
6ae9ed
       <zeroOrMore>
6ae9ed
@@ -81,6 +82,7 @@
6ae9ed
       <value>fat</value>
6ae9ed
       <value>vhd</value>
6ae9ed
       <value>ploop</value>
6ae9ed
+      <value>luks</value>
6ae9ed
       <ref name='storageFormatBacking'/>
6ae9ed
     </choice>
6ae9ed
   </define>
6ae9ed
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
6ae9ed
index 4ffd9b7..129c070 100644
6ae9ed
--- a/src/qemu/qemu_process.c
6ae9ed
+++ b/src/qemu/qemu_process.c
6ae9ed
@@ -2414,6 +2414,12 @@ qemuProcessInitPasswords(virConnectPtr conn,
6ae9ed
             !virDomainDiskGetSource(vm->def->disks[i]))
6ae9ed
             continue;
6ae9ed
 
6ae9ed
+        if (vm->def->disks[i]->src->encryption->format !=
6ae9ed
+            VIR_STORAGE_ENCRYPTION_FORMAT_DEFAULT &&
6ae9ed
+            vm->def->disks[i]->src->encryption->format !=
6ae9ed
+            VIR_STORAGE_ENCRYPTION_FORMAT_QCOW)
6ae9ed
+            continue;
6ae9ed
+
6ae9ed
         VIR_FREE(secret);
6ae9ed
         if (qemuProcessGetVolumeQcowPassphrase(conn,
6ae9ed
                                                vm->def->disks[i],
6ae9ed
diff --git a/src/storage/storage_backend.c b/src/storage/storage_backend.c
6ae9ed
index d6a451d..97f6ffe 100644
6ae9ed
--- a/src/storage/storage_backend.c
6ae9ed
+++ b/src/storage/storage_backend.c
6ae9ed
@@ -1027,8 +1027,7 @@ virStorageBackendCreateQemuImgCheckEncryption(int format,
6ae9ed
         }
6ae9ed
     } else {
6ae9ed
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
6ae9ed
-                       _("qcow volume encryption unsupported with "
6ae9ed
-                         "volume format %s"), type);
6ae9ed
+                       _("volume encryption unsupported with format %s"), type);
6ae9ed
         return -1;
6ae9ed
     }
6ae9ed
 
6ae9ed
diff --git a/src/storage/storage_backend_fs.c b/src/storage/storage_backend_fs.c
6ae9ed
index 839a2c7..0a12ecb 100644
6ae9ed
--- a/src/storage/storage_backend_fs.c
6ae9ed
+++ b/src/storage/storage_backend_fs.c
6ae9ed
@@ -157,7 +157,12 @@ virStorageBackendProbeTarget(virStorageSourcePtr target,
6ae9ed
         case VIR_STORAGE_FILE_QCOW2:
6ae9ed
             (*encryption)->format = VIR_STORAGE_ENCRYPTION_FORMAT_QCOW;
6ae9ed
             break;
6ae9ed
-        default:
6ae9ed
+
6ae9ed
+        case VIR_STORAGE_FILE_LUKS:
6ae9ed
+            (*encryption)->format = VIR_STORAGE_ENCRYPTION_FORMAT_LUKS;
6ae9ed
+            break;
6ae9ed
+
6ae9ed
+        case VIR_STORAGE_ENCRYPTION_FORMAT_LAST:
6ae9ed
             break;
6ae9ed
         }
6ae9ed
 
6ae9ed
diff --git a/src/storage/storage_backend_gluster.c b/src/storage/storage_backend_gluster.c
6ae9ed
index 0085052..eda060d 100644
6ae9ed
--- a/src/storage/storage_backend_gluster.c
6ae9ed
+++ b/src/storage/storage_backend_gluster.c
6ae9ed
@@ -321,6 +321,8 @@ virStorageBackendGlusterRefreshVol(virStorageBackendGlusterStatePtr state,
6ae9ed
         if (vol->target.format == VIR_STORAGE_FILE_QCOW ||
6ae9ed
             vol->target.format == VIR_STORAGE_FILE_QCOW2)
6ae9ed
             vol->target.encryption->format = VIR_STORAGE_ENCRYPTION_FORMAT_QCOW;
6ae9ed
+        if (vol->target.format == VIR_STORAGE_FILE_LUKS)
6ae9ed
+            vol->target.encryption->format = VIR_STORAGE_ENCRYPTION_FORMAT_LUKS;
6ae9ed
     }
6ae9ed
     vol->target.features = meta->features;
6ae9ed
     meta->features = NULL;
6ae9ed
diff --git a/src/util/virstorageencryption.c b/src/util/virstorageencryption.c
6ae9ed
index cd17554..2b59420 100644
6ae9ed
--- a/src/util/virstorageencryption.c
6ae9ed
+++ b/src/util/virstorageencryption.c
6ae9ed
@@ -43,7 +43,7 @@ VIR_ENUM_IMPL(virStorageEncryptionSecret,
6ae9ed
 
6ae9ed
 VIR_ENUM_IMPL(virStorageEncryptionFormat,
6ae9ed
               VIR_STORAGE_ENCRYPTION_FORMAT_LAST,
6ae9ed
-              "default", "qcow")
6ae9ed
+              "default", "qcow", "luks")
6ae9ed
 
6ae9ed
 static void
6ae9ed
 virStorageEncryptionSecretFree(virStorageEncryptionSecretPtr secret)
6ae9ed
diff --git a/src/util/virstorageencryption.h b/src/util/virstorageencryption.h
6ae9ed
index c68c66e..5e1be3b 100644
6ae9ed
--- a/src/util/virstorageencryption.h
6ae9ed
+++ b/src/util/virstorageencryption.h
6ae9ed
@@ -48,6 +48,7 @@ typedef enum {
6ae9ed
     /* "default" is only valid for volume creation */
6ae9ed
     VIR_STORAGE_ENCRYPTION_FORMAT_DEFAULT = 0,
6ae9ed
     VIR_STORAGE_ENCRYPTION_FORMAT_QCOW, /* Both qcow and qcow2 */
6ae9ed
+    VIR_STORAGE_ENCRYPTION_FORMAT_LUKS,
6ae9ed
 
6ae9ed
     VIR_STORAGE_ENCRYPTION_FORMAT_LAST,
6ae9ed
 } virStorageEncryptionFormatType;
6ae9ed
diff --git a/tests/qemuxml2argvdata/qemuxml2argv-luks-disks.xml b/tests/qemuxml2argvdata/qemuxml2argv-luks-disks.xml
6ae9ed
new file mode 100644
6ae9ed
index 0000000..9ce15c0
6ae9ed
--- /dev/null
6ae9ed
+++ b/tests/qemuxml2argvdata/qemuxml2argv-luks-disks.xml
6ae9ed
@@ -0,0 +1,45 @@
6ae9ed
+<domain type='qemu'>
6ae9ed
+  <name>encryptdisk</name>
6ae9ed
+  <uuid>496898a6-e6ff-f7c8-5dc2-3cf410945ee9</uuid>
6ae9ed
+  <memory unit='KiB'>1048576</memory>
6ae9ed
+  <currentMemory unit='KiB'>524288</currentMemory>
6ae9ed
+  <vcpu placement='static'>1</vcpu>
6ae9ed
+  <os>
6ae9ed
+    <type arch='x86_64' machine='pc-i440fx-2.1'>hvm</type>
6ae9ed
+    <boot dev='hd'/>
6ae9ed
+  </os>
6ae9ed
+  <clock offset='utc'/>
6ae9ed
+  <on_poweroff>destroy</on_poweroff>
6ae9ed
+  <on_reboot>restart</on_reboot>
6ae9ed
+  <on_crash>destroy</on_crash>
6ae9ed
+  <devices>
6ae9ed
+    <emulator>/usr/bin/qemu</emulator>
6ae9ed
+    <disk type='file' device='disk'>
6ae9ed
+      <driver name='qemu' type='luks'/>
6ae9ed
+      <source file='/storage/guest_disks/encryptdisk'/>
6ae9ed
+      <target dev='vda' bus='virtio'/>
6ae9ed
+      <encryption format='luks'>
6ae9ed
+        <secret type='passphrase' uuid='0a81f5b2-8403-7b23-c8d6-21ccc2f80d6f'/>
6ae9ed
+      </encryption>
6ae9ed
+      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
6ae9ed
+    </disk>
6ae9ed
+    <disk type='file' device='disk'>
6ae9ed
+      <driver name='qemu' type='luks'/>
6ae9ed
+      <source file='/storage/guest_disks/encryptdisk2'/>
6ae9ed
+      <target dev='vdb' bus='virtio'/>
6ae9ed
+      <encryption format='luks'>
6ae9ed
+        <secret type='passphrase' usage='mycluster_myname'/>
6ae9ed
+      </encryption>
6ae9ed
+      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
6ae9ed
+    </disk>
6ae9ed
+    <controller type='usb' index='0'>
6ae9ed
+      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
6ae9ed
+    </controller>
6ae9ed
+    <controller type='pci' index='0' model='pci-root'/>
6ae9ed
+    <input type='mouse' bus='ps2'/>
6ae9ed
+    <input type='keyboard' bus='ps2'/>
6ae9ed
+    <memballoon model='virtio'>
6ae9ed
+      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
6ae9ed
+    </memballoon>
6ae9ed
+  </devices>
6ae9ed
+</domain>
6ae9ed
diff --git a/tests/qemuxml2xmloutdata/qemuxml2xmlout-luks-disks.xml b/tests/qemuxml2xmloutdata/qemuxml2xmlout-luks-disks.xml
6ae9ed
new file mode 120000
6ae9ed
index 0000000..b59dc67
6ae9ed
--- /dev/null
6ae9ed
+++ b/tests/qemuxml2xmloutdata/qemuxml2xmlout-luks-disks.xml
6ae9ed
@@ -0,0 +1 @@
6ae9ed
+../qemuxml2argvdata/qemuxml2argv-luks-disks.xml
6ae9ed
\ No newline at end of file
6ae9ed
diff --git a/tests/qemuxml2xmltest.c b/tests/qemuxml2xmltest.c
6ae9ed
index 693e712..a757fdb 100644
6ae9ed
--- a/tests/qemuxml2xmltest.c
6ae9ed
+++ b/tests/qemuxml2xmltest.c
6ae9ed
@@ -502,6 +502,7 @@ mymain(void)
6ae9ed
 
6ae9ed
     DO_TEST("encrypted-disk");
6ae9ed
     DO_TEST("encrypted-disk-usage");
6ae9ed
+    DO_TEST("luks-disks");
6ae9ed
     DO_TEST("memtune");
6ae9ed
     DO_TEST("memtune-unlimited");
6ae9ed
     DO_TEST("blkiotune");
6ae9ed
diff --git a/tests/storagevolxml2xmlin/vol-luks.xml b/tests/storagevolxml2xmlin/vol-luks.xml
6ae9ed
new file mode 100644
6ae9ed
index 0000000..eb4dc41
6ae9ed
--- /dev/null
6ae9ed
+++ b/tests/storagevolxml2xmlin/vol-luks.xml
6ae9ed
@@ -0,0 +1,21 @@
6ae9ed
+<volume>
6ae9ed
+  <name>LuksDemo.img</name>
6ae9ed
+  <key>/var/lib/libvirt/images/LuksDemo.img</key>
6ae9ed
+  <source>
6ae9ed
+  </source>
6ae9ed
+  <capacity unit="G">5</capacity>
6ae9ed
+  <allocation>294912</allocation>
6ae9ed
+  <target>
6ae9ed
+    <path>/var/lib/libvirt/images/LuksDemo.img</path>
6ae9ed
+    <format type='luks'/>
6ae9ed
+    <permissions>
6ae9ed
+      <mode>0644</mode>
6ae9ed
+      <owner>0</owner>
6ae9ed
+      <group>0</group>
6ae9ed
+      <label>unconfined_u:object_r:virt_image_t:s0</label>
6ae9ed
+    </permissions>
6ae9ed
+    <encryption format='luks'>
6ae9ed
+      <secret type='passphrase' usage='mumblyfratz'/>
6ae9ed
+    </encryption>
6ae9ed
+  </target>
6ae9ed
+</volume>
6ae9ed
diff --git a/tests/storagevolxml2xmlout/vol-luks.xml b/tests/storagevolxml2xmlout/vol-luks.xml
6ae9ed
new file mode 100644
6ae9ed
index 0000000..5b764b7
6ae9ed
--- /dev/null
6ae9ed
+++ b/tests/storagevolxml2xmlout/vol-luks.xml
6ae9ed
@@ -0,0 +1,21 @@
6ae9ed
+<volume type='file'>
6ae9ed
+  <name>LuksDemo.img</name>
6ae9ed
+  <key>/var/lib/libvirt/images/LuksDemo.img</key>
6ae9ed
+  <source>
6ae9ed
+  </source>
6ae9ed
+  <capacity unit='bytes'>5368709120</capacity>
6ae9ed
+  <allocation unit='bytes'>294912</allocation>
6ae9ed
+  <target>
6ae9ed
+    <path>/var/lib/libvirt/images/LuksDemo.img</path>
6ae9ed
+    <format type='luks'/>
6ae9ed
+    <permissions>
6ae9ed
+      <mode>0644</mode>
6ae9ed
+      <owner>0</owner>
6ae9ed
+      <group>0</group>
6ae9ed
+      <label>unconfined_u:object_r:virt_image_t:s0</label>
6ae9ed
+    </permissions>
6ae9ed
+    <encryption format='luks'>
6ae9ed
+      <secret type='passphrase' usage='mumblyfratz'/>
6ae9ed
+    </encryption>
6ae9ed
+  </target>
6ae9ed
+</volume>
6ae9ed
diff --git a/tests/storagevolxml2xmltest.c b/tests/storagevolxml2xmltest.c
6ae9ed
index f722452..a36a706 100644
6ae9ed
--- a/tests/storagevolxml2xmltest.c
6ae9ed
+++ b/tests/storagevolxml2xmltest.c
6ae9ed
@@ -105,6 +105,7 @@ mymain(void)
6ae9ed
     DO_TEST("pool-dir", "vol-qcow2-lazy");
6ae9ed
     DO_TEST("pool-dir", "vol-qcow2-0.10-lazy");
6ae9ed
     DO_TEST("pool-dir", "vol-qcow2-nobacking");
6ae9ed
+    DO_TEST("pool-dir", "vol-luks");
6ae9ed
     DO_TEST("pool-disk", "vol-partition");
6ae9ed
     DO_TEST("pool-logical", "vol-logical");
6ae9ed
     DO_TEST("pool-logical", "vol-logical-backing");
6ae9ed
-- 
6ae9ed
2.9.2
6ae9ed