Framework for securely passing secrets to QEMU
Subsystems (2)
cover Framework for securely passing secrets to QEMU
There are a variety of places where QEMU needs to have access to passwords, encryption keys or similar kinds of secrets.
- VNC / SPICE user passwords
- Curl block http / proxy passwords
- RBD auth password
- iSCSI CHAP password
- x509 private key password
- QCow/QCow2 encryption key
QEMU has a variety of ways of dealing with this problem, some good, some ugly, some bad.
- The RBD block driver accepts the password in plaintext via a private RBD config option. This is a pending CVE
https://security-tracker.debian.org/tracker/CVE-2015-5160
- The iSCSI driver accepts the password in plaintext as a block driver option. This is the same as the RBD CVE essentially, just a QEMU option, rather than a librbd option
- The VNC / SPICE servers only accept the passwords via the QEMU monitor. This is secure, but it inconvenient for adhoc developer usage where security of CLI args does not matter.
- QCow/QCow2 encryption keys can be provided by the monitor but this is not available for qemu-img, qemu-io and qemu-nbd. QEMU falls back to doing interactive console prompting to get keys.
- x509 privte key passwords are not supported at all by QEMU which forces users to store their key in plaintext on their host FS.
- The CURL driver doesn't support HTTP auth at all currently.
It is obvious there there is a wide variety of functionality in QEMU that needs access to "secrets". This need will only grow over time. We need to stop having everyone invent their own dangerous wheels and provide a standard mechanism for securely passing secrets to QEMU.
To this end, this series introduces a QCryptoSecret object class with short name "secret". All the places which needs passwords/keys are then converted to get their via this API, except VNC/SPICE which are a future exercise.
Example usage for creating secrets...
Direct password, insecure, for ad-hoc developer testing only
$QEMU -object secret,id=sec0,data=letmein
Indirect password via a file, good for production
echo -n "letmein" > mypasswd.txt
$QEMU -object secret,id=sec0,file=mypasswd.txt
The file based approach supports file descriptor passing, so mgmt apps can use that to dynamically add passwords to running QEMU.
There is a better way though, which is to use an encrypted secret. The intent here is that a mgmt app (like libvirt) will generate a random AES-256 key for each virtual machine it starts (saved in eg /var/run/libvirt/qemu/$GUEST.key) It can then use the direct password passing, but encrypt the data.
$QEMU \
-object secret,id=secmaster0,file=/var/run/libvirt/qemu/$GUEST.key,format=base64 \
-object secret,id=sec0,data=[base64 ciphertext],\
keyid=secmaster0,iv=[base64 initialization vector]This means that the mgmt app does not need to worry about file descriptor passing at all. It can just use regular object properties, safe in the knowledge that the data is protected by a secret AES key shared only between QEMU and the mgmt app.
Use of encrypted secrets is not restricted to directly provided inline data. If the secret is stored in an external file, that can be encrypted too
$QEMU \
-object secret,id=secmaster0,file=/var/run/libvirt/qemu/$GUEST.key,format=base64 \
-object secret,id=sec0,file=/some/secret/file.aes,\
keyid=secmaster0,iv=[base64 initialization vector]Example usage for referencing secrets...
CURL:
$QEMU -object secret,id=sec0... \
-drive driver=http,url=http://example.com/someimg.qcow2,\
user=dan,passwordid=sec0
$QEMU -object secret,id=sec0... -object secret,id=sec1 \
-drive driver=http,url=http://example.com/someimg.qcow2,\
user=dan,passwordid=sec0,proxyuser=dan,passwordid=sec1iSCSI:
$QEMU -object secret,id=sec0... \
-drive driver=iscsi,url=iscsi://example.com/target-foo/lun1,\
user=dan,passwordid=sec0RBD:
$QEMU -object secret,id=sec0... \
-drive driver=rbd,file=rbd:pool/image:id=myname,\
auth-supported-cephx,authsecret=sec0QCow/Qcow2 encryption
$QEMU -object secret,id=sec0... \
-drive file=someimage.qcow2,keyid=sec0Finally, this extends qemu-img, qemu-nbd and qemu-io. All of them gain a new '--object' parameter which provides the same functionality as '-object' with QEMU system emulators. This lets us create QCryptoSecret object instances in those tools
The tools also then get support for a new '--source IMG-OPTS' parameter to allow a full set of image options to be specified, instead of just separate hardcoded args for format + filename which they currently permit. This is probably the area I am least sure of. I struggled to understand what the "best practice" is for turning a QemuOpts into something you can use to instantiate block backends. So I may well have not done the right thing.
Towards the end I rip out the current encryption key handling from the block layer so all the hairy code for dealing with encrypted block devices disappears, and encryption can be a 100% private matter for the block driver internal impl. This is obviously not backwards compatible, but we have been warning users we're dropping qcow2 encryption support for a while.
Finally I disable support for writing to encrypted qcow2 files, but keep the ability to read them, so users can liberate data. Originally we were intending to fully delete encryption support, due to the burden it places on the internal boock API. Since I removed that burden I figured it is reasonable to keep read-only support around.
The only real missing thing is wiring up the VNC/SPICE servers. There is one complication here in that it is common to change the VNC/SPICE password at runtime, and I'm not sure what the best way to deal with this is.
There are two obvious choices
a. Create a new secret, tell the VNC server to use
the new secret, delete the old secret. This will need a new 'graphics_secret_update' command in the monitor, to use alongside object_add/del.
b. Allow the existing secret to be updated via some
new 'object_update' method, and internally notify the VNC/SPICE server when the secret is updated. This would probably need a new QOM interface UserUpdatableObject to be defined, as an refinement of UserCreatableObject.
Daniel P. Berrange (17):
crypto: add QCryptoSecret object class for password/key handling
crypto: add support for loading encrypted x509 keys
rbd: add support for getting password from QCryptoSecret object
curl: add support for HTTP authentication parameters
iscsi: add support for getting CHAP password via QCryptoSecret API
qcow: add a 'keyid' parameter to qcow options
qcow2: add a 'keyid' parameter to qcow2 options
qom: add user_creatable_add & user_creatable_del methods
qemu-img: add support for --object command line arg
qemu-nbd: add support for --object command line arg
qemu-io: add support for --object command line arg
qemu-io: allow specifying image as a set of options args
qemu-nbd: allow specifying image as a set of options args
qemu-img: allow specifying image as a set of options args
block: rip out all traces of password prompting
block: remove all encryption handling APIs
block: remove support for writing to qcow/qcow2 encrypted images
block.c | 88 +---- block/curl.c | 66 ++++ block/iscsi.c | 24 +- block/qapi.c | 2 +- block/qcow.c | 122 +++++-- block/qcow2.c | 116 +++--- block/qcow2.h | 1 + block/rbd.c | 42 +++ blockdev.c | 69 +--- crypto/Makefile.objs | 1 + crypto/secret.c | 513 ++++++++++++++++++++++++++ crypto/tlscredsx509.c | 47 +++ hmp.c | 42 +-- hw/usb/dev-storage.c | 32 -- include/block/block.h | 5 +- include/crypto/secret.h | 139 +++++++ include/crypto/tlscredsx509.h | 1 + include/monitor/monitor.h | 10 - include/qemu/option.h | 1 + include/qemu/osdep.h | 2 - include/qom/object_interfaces.h | 31 ++ monitor.c | 65 ---- qapi/block-core.json | 23 +- qapi/crypto.json | 14 + qemu-img-cmds.hx | 44 +-- qemu-img.c | 788 +++++++++++++++++++++++++++++++++++----- qemu-img.texi | 8 + qemu-io.c | 145 ++++++-- qemu-nbd.c | 142 +++++++- qemu-nbd.texi | 7 + qemu-options.hx | 84 ++++- qmp.c | 83 +---- qom/object_interfaces.c | 76 ++++ tests/.gitignore | 1 + tests/Makefile | 2 + tests/qemu-iotests/087 | 20 + tests/qemu-iotests/087.out | 26 +- tests/qemu-iotests/134 | 17 +- tests/qemu-iotests/134.out | 44 +-- tests/qemu-iotests/common.rc | 4 +- tests/test-crypto-secret.c | 440 ++++++++++++++++++++++ util/oslib-posix.c | 66 ---- util/oslib-win32.c | 24 -- util/qemu-option.c | 6 + vl.c | 8 +- 45 files changed, 2740 insertions(+), 751 deletions(-) create mode 100644 crypto/secret.c create mode 100644 include/crypto/secret.h create mode 100644 tests/test-crypto-secret.c
Signature · 1 line
2.4.3
PATCH 1/17 crypto: add QCryptoSecret object class for password/key handling
Introduce a new QCryptoSecret object class which will be used for providing passwords and keys to other objects which need sensitive credentials.
The new object can provide secret values directly as properties, or indirectly via a file. The latter includes support for file descriptor passing syntax on UNIX platforms. Ordinarily passing secret values directly as properties is insecure, since they are visible in process listings, or in log files showing the CLI args / QMP commands. It is possible to use AES-256-CBC to encrypt the secret values though, in which case all that is visible is the ciphertext. For adhoc developer testing though, it is fine to provide the secrets directly without encryption so this is not explicitly forbidden.
The anticipated scenario is that libvirtd will create a random master key per QEMU instance (eg /var/run/libvirt/qemu/$VMNAME.key) and will use that key to encrypt all passwords it provides to QEMU via '-object secret,....'. This avoids the need for libvirt (or other mgmt apps) to worry about file descriptor passing.
It also makes life easier for people who are scripting the management of QEMU, for whom FD passing is significantly more complex.
Providing data inline (insecure, only for adhoc dev tetsing)
$QEMU -object secret,id=sec0,data=letmein
Providing data indirectly
echo -n "letmein" > mypasswd.txt
$QEMU -object secret,id=sec0,file=mypasswd.txt
Providing binary data
$QEMU -object secret,id=sec0,file=mykey.bin,format=base64
Providing data with encyption
$QEMU -object secret,id=master0,file=mykey.bin,format=base64 \
-object secret,id=sec0,data=[base64 ciphertext],\
keyid=master0,iv=[base64 IV],format=utf8Note that 'format' here refers to the format of the decrypted data, which is independant of the ciphertext which must always be in base64.
More examples are shown in the updated docs.
--- crypto/Makefile.objs | 1 + crypto/secret.c | 513 +++++++++++++++++++++++++++++++++++++++++++++ include/crypto/secret.h | 139 ++++++++++++ qapi/crypto.json | 14 ++ qemu-options.hx | 76 +++++++ tests/.gitignore | 1 + tests/Makefile | 2 + tests/test-crypto-secret.c | 440 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 1186 insertions(+) create mode 100644 crypto/secret.c create mode 100644 include/crypto/secret.h create mode 100644 tests/test-crypto-secret.c
crypto/Makefile.objs +1 -0
@@ -7,6 +7,7 @@ crypto-obj-y += tlscreds.o crypto-obj-y += tlscredsanon.o crypto-obj-y += tlscredsx509.o crypto-obj-y += tlssession.o +crypto-obj-y += secret.o # Let the userspace emulators avoid linking gnutls/etc crypto-aes-obj-y = aes.o
crypto/secret.c new file +513 -0
513 lines not on this page · raw message
include/crypto/secret.h new file +139 -0
139 lines not on this page · raw message
qapi/crypto.json +14 -0
@@ -19,3 +19,17 @@ { 'enum': 'QCryptoTLSCredsEndpoint', 'prefix': 'QCRYPTO_TLS_CREDS_ENDPOINT', 'data': ['client', 'server']} + + +## +# QCryptoSecretFormat: +# +# The data format that the secret is provided in +# +# @utf8: printable UTF-8 encoded text +# @base64: arbitrary base64 encoded binary data +# Since: 2.5 +## +{ 'enum': 'QCryptoSecretFormat', + 'prefix': 'QCRYPTO_SECRET_FORMAT', + 'data': ['utf8', 'base64']}
qemu-options.hx +76 -0
82 lines not on this page · raw message
tests/.gitignore +1 -0
@@ -12,6 +12,7 @@ test-bitops test-coroutine test-crypto-cipher test-crypto-hash +test-crypto-secret test-crypto-tlscredsx509 test-crypto-tlscredsx509-work/ test-crypto-tlscredsx509-certs/
tests/Makefile +2 -0
@@ -78,6 +78,7 @@ check-unit-y += tests/test-write-threshold$(EXESUF) gcov-files-test-write-threshold-y = block/write-threshold.c check-unit-$(CONFIG_GNUTLS_HASH) += tests/test-crypto-hash$(EXESUF) check-unit-y += tests/test-crypto-cipher$(EXESUF) +check-unit-y += tests/test-crypto-secret$(EXESUF) check-unit-$(CONFIG_GNUTLS) += tests/test-crypto-tlscredsx509$(EXESUF) check-unit-$(CONFIG_GNUTLS) += tests/test-crypto-tlssession$(EXESUF)
@@ -443,6 +444,7 @@ tests/test-mul64$(EXESUF): tests/test-mul64.o $(test-util-obj-y) tests/test-bitops$(EXESUF): tests/test-bitops.o $(test-util-obj-y) tests/test-crypto-hash$(EXESUF): tests/test-crypto-hash.o $(test-crypto-obj-y) tests/test-crypto-cipher$(EXESUF): tests/test-crypto-cipher.o $(test-crypto-obj-y) +tests/test-crypto-secret$(EXESUF): tests/test-crypto-secret.o $(test-crypto-obj-y) tests/test-crypto-tlscredsx509$(EXESUF): tests/test-crypto-tlscredsx509.o \ tests/crypto-tls-x509-helpers.o tests/pkix_asn1_tab.o $(test-crypto-obj-y) tests/test-crypto-tlssession$(EXESUF): tests/test-crypto-tlssession.o \
tests/test-crypto-secret.c new file +440 -0
440 lines not on this page · raw message
Signature · 1 line
2.4.3
Re: [Qemu-devel] [PATCH 01/17] crypto: add QCryptoSecret object class for password/key handling
crypto/secret.c line 404
Quoted 12 lines · Daniel P. Berrange
crypto/secret.c new file +12
-0+ +switch (secret->format) { +case QCRYPTO_SECRET_FORMAT_UTF8 : +if (!g_utf8_validate (input,strlen (input),NULL )) { +error_setg (errp, +"Data from secret %s is not valid UTF-8" , + secretid); + goto cleanup; + } + output = input; + input =NULL ; +break ;
Why validate secrets as UTF-8? In other words why have "utf8" instead of "binary" as a possible QCryptoSecretFormat?
Paolo
Re: [Qemu-devel] [PATCH 01/17] crypto: add QCryptoSecret object class for password/key handling
Quoted 16 lines · Paolo Bonzini
crypto/secret.c line 404
Quoted 12 lines · Daniel P. Berrange
crypto/secret.c new file +12
-0+ +switch (secret->format) { +case QCRYPTO_SECRET_FORMAT_UTF8 : +if (!g_utf8_validate (input,strlen (input),NULL )) { +error_setg (errp, +"Data from secret %s is not valid UTF-8" , + secretid); + goto cleanup; + } + output = input; + input =NULL ; +break ;
Quoted 2 lines
Why validate secrets as UTF-8? In other words why have "utf8" instead of "binary" as a possible QCryptoSecretFormat?
JSON doesn't accept arbitrary 8-bit binary data, so the alternative 'base64' is effectively providing binary data facility. Having to use base64 for plain passwords is rather tedious though, so allowing utf8 is a much more developer friendly approach for people using QEMU without a mgmt tool like libvirt.
NB, this dual-format utf8-or-base64 approach matches the approach used in QEMU guest agent for the guest-file-read/write commands for the same reason.
Regards,
Daniel
Signature · 4 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
Re: [Qemu-devel] [PATCH 01/17] crypto: add QCryptoSecret object class for password/key handling
Quoted 9 lines · Daniel P. Berrange
JSON doesn't accept arbitrary 8-bit binary data, so the alternative 'base64' is effectively providing binary data facility. Having to use base64 for plain passwords is rather tedious though, so allowing utf8 is a much more developer friendly approach for people using QEMU without a mgmt tool like libvirt.
NB, this dual-format utf8-or-base64 approach matches the approach used in QEMU guest agent for the guest-file-read/write commands for the same reason.
The difference is that guest-file-read/write have the payload in JSON; for file-based secrets the payload is not JSON.
So I think that "binary" (which is the default anyway) would fit all the usecases (direct over JSON, file-based, direct over command line). Direct over JSON would be limited to valid UTF-8, but that's just a limitation of the transport.
Paolo
Re: [Qemu-devel] [PATCH 01/17] crypto: add QCryptoSecret object class for password/key handling
Quoted 9 lines · Paolo Bonzini
Quoted 6 lines · Daniel P. Berrange
JSON doesn't accept arbitrary 8-bit binary data, so the alternative 'base64' is effectively providing binary data facility. Having to use base64 for plain passwords is rather tedious though, so allowing utf8 is a much more developer friendly approach for people using QEMU without a mgmt tool like libvirt.
Quoted 6 lines
Quoted 3 lines
NB, this dual-format utf8-or-base64 approach matches the approach used in QEMU guest agent for the guest-file-read/write commands for the same reason.
The difference is that guest-file-read/write have the payload in JSON; for file-based secrets the payload is not JSON.
For non-file based secrets though, the payload *is* in the JSON, and per the cover letter, I actually anticipate passing all secrets inline in the JSON and only using the file backend for loading the initial master key. This avoids the need to do file handle passing and/or create lots of temporary files, when hotplugging resources.
Quoted 4 lines
So I think that "binary" (which is the default anyway) would fit all the usecases (direct over JSON, file-based, direct over command line). Direct over JSON would be limited to valid UTF-8, but that's just a limitation of the transport.
I don't think that's actually an acceptable limitation - I want the inline data passing to be fully usable for non-UTF-8 data too.
Regards,
Daniel
Signature · 4 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
Re: [Qemu-devel] [PATCH 01/17] crypto: add QCryptoSecret object class for password/key handling
Quoted 9 lines · Daniel P. Berrange
Quoted 2 lines
Quoted 2 lines
The difference is that guest-file-read/write have the payload in JSON; for file-based secrets the payload is not JSON.
For non-file based secrets though, the payload *is* in the JSON, and per the cover letter, I actually anticipate passing all secrets inline in the JSON and only using the file backend for loading the initial master key. This avoids the need to do file handle passing and/or create lots of temporary files, when hotplugging resources.
Quoted 7 lines
Quoted 4 lines
So I think that "binary" (which is the default anyway) would fit all the usecases (direct over JSON, file-based, direct over command line). Direct over JSON would be limited to valid UTF-8, but that's just a limitation of the transport.
I don't think that's actually an acceptable limitation - I want the inline data passing to be fully usable for non-UTF-8 data too.
Of course, there's base64 for that. On the other hand, I think there's no reason for unencoded file-based data passing to be usable for UTF-8 data only.
Paolo
Re: [Qemu-devel] [PATCH 01/17] crypto: add QCryptoSecret object class for password/key handling
Quoted 17 lines · Paolo Bonzini
Quoted 14 lines · Daniel P. Berrange
Quoted 2 lines
Quoted 2 lines
The difference is that guest-file-read/write have the payload in JSON; for file-based secrets the payload is not JSON.
For non-file based secrets though, the payload *is* in the JSON, and per the cover letter, I actually anticipate passing all secrets inline in the JSON and only using the file backend for loading the initial master key. This avoids the need to do file handle passing and/or create lots of temporary files, when hotplugging resources.
Quoted 4 lines
So I think that "binary" (which is the default anyway) would fit all the usecases (direct over JSON, file-based, direct over command line). Direct over JSON would be limited to valid UTF-8, but that's just a limitation of the transport.
Quoted 6 lines
Quoted 2 lines
I don't think that's actually an acceptable limitation - I want the inline data passing to be fully usable for non-UTF-8 data too.
Of course, there's base64 for that. On the other hand, I think there's no reason for unencoded file-based data passing to be usable for UTF-8 data only.
Ok, so there's really two separate formats at play here.
The input format, eg the encoding of the data=XXXX value, or the contents of the file, and the output format, which is that required by the consumer inside QEMU. We convert between the two. eg you can provide data in base64 even if QEMU ultimately needs to use it in plain utf-8 format, or vica-verca.
IIUC, you're suggesting that for the input format, the data=XXX value should allow a choice of utf8 or base64, while the external file could just take raw or base64 data. That's easy enough to wire up - just add a 3rd option to the format enum and make raw be the default for files.
Regards,
Daniel
Signature · 4 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
Re: [Qemu-devel] [PATCH 01/17] crypto: add QCryptoSecret object class for password/key handling
Quoted 4 lines · Daniel P. Berrange
The input format, eg the encoding of the data=XXXX value, or the contents of the file, and the output format, which is that required by the consumer inside QEMU. We convert between the two. eg you can provide data in base64 even if QEMU ultimately needs to use it in plain utf-8 format, or vica-verca.
Right. In the end QCryptoSecret only needs to provide a raw output; converting it to something else, and possibly applying restrictions such as UTF-8, should depend on the user. Of course the API can include helper functions for common restrictions, but in general a "secret storage" module is independent of them.
Quoted 4 lines
IIUC, you're suggesting that for the input format, the data=XXX value should allow a choice of utf8 or base64, while the external file could just take raw or base64 data. That's easy enough to wire up - just add a 3rd option to the format enum and make raw be the default for files.
Almost.
I am also saying that the utf8 case for data=XXX actually should be raw, because utf8 is just a limitation of JSON and not of the data=XXX interface. Non-UTF8 data=XXX would then be accepted for the -object command line option.
Paolo
Re: [Qemu-devel] [PATCH 01/17] crypto: add QCryptoSecret object class for password/key handling
Quoted 19 lines · Paolo Bonzini
Quoted 4 lines · Daniel P. Berrange
The input format, eg the encoding of the data=XXXX value, or the contents of the file, and the output format, which is that required by the consumer inside QEMU. We convert between the two. eg you can provide data in base64 even if QEMU ultimately needs to use it in plain utf-8 format, or vica-verca.
Right. In the end QCryptoSecret only needs to provide a raw output; converting it to something else, and possibly applying restrictions such as UTF-8, should depend on the user. Of course the API can include helper functions for common restrictions, but in general a "secret storage" module is independent of them.
Quoted 4 lines
IIUC, you're suggesting that for the input format, the data=XXX value should allow a choice of utf8 or base64, while the external file could just take raw or base64 data. That's easy enough to wire up - just add a 3rd option to the format enum and make raw be the default for files.
Quoted 6 lines
Almost.
I am also saying that the utf8 case for data=XXX actually should be raw, because utf8 is just a limitation of JSON and not of the data=XXX interface. Non-UTF8 data=XXX would then be accepted for the -object command line option.
Ah ok, I see what you mean now
Regards,
Daniel
Signature · 4 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
PATCH 2/17 crypto: add support for loading encrypted x509 keys
In reply to Daniel P. BerrangeMake use of the QCryptoSecret object to support loading of encrypted x509 keys. The optional 'passwordid' parameter to the tls-creds-x509 object type, provides the ID of a secret object instance that holds the decryption password for the PEM file.
# echo "123456" > mypasswd.txt
# $QEMU \
-object secret,id=sec0,filename=mypasswd.txt \
-object tls-creds-x509,passwordid=sec0,id=creds0,\
dir=/home/berrange/.pki/qemu,endpoint=server \
-vnc :1,tls-creds=creds0This requires QEMU to be linked to GNUTLS >= 3.1.11. If GNUTLS is too old an error will be reported if an attempt is made to pass a decryption password.
--- crypto/tlscredsx509.c | 47 +++++++++++++++++++++++++++++++++++++++++++ include/crypto/tlscredsx509.h | 1 + qemu-options.hx | 8 +++++++- 3 files changed, 55 insertions(+), 1 deletion(-)
crypto/tlscredsx509.c +47 -0
@@ -20,6 +20,7 @@#include "crypto/tlscredsx509.h" #include "crypto/tlscredspriv.h" +#include "crypto/secret.h" #include "qom/object_interfaces.h" #include "trace.h"
@@ -605,9 +606,29 @@ qcrypto_tls_creds_x509_load(QCryptoTLSCredsX509 *creds, }if (cert !=NULL && key !=NULL ) { +#if GNUTLS_VERSION_NUMBER >=0x030111 +char *password =NULL ; +if (creds->passwordid) { + password =qcrypto_secret_lookup_as_utf8 (creds->passwordid, + errp); +if (!password) { + goto cleanup; + } + } + ret =gnutls_certificate_set_x509_key_file2 (creds->data, + cert, key, +GNUTLS_X509_FMT_PEM , + password, +0 ); +#else /* GNUTLS_VERSION_NUMBER < 0x030111 */ +if (creds->passwordid) { +error_setg (errp,"PKCS8 decryption requires GNUTLS >= 3.1.11" ); + goto cleanup; + } ret =gnutls_certificate_set_x509_key_file (creds->data, cert, key,GNUTLS_X509_FMT_PEM ); +#endif /* GNUTLS_VERSION_NUMBER < 0x030111 */ if (ret <0 ) {error_setg (errp,"Cannot load certificate '%s' & key '%s': %s" , cert, key,gnutls_strerror (ret));
@@ -731,6 +752,27 @@ qcrypto_tls_creds_x509_prop_set_sanity(Object *obj, } +static void +qcrypto_tls_creds_x509_prop_set_passwordid (Object *obj, +const char *value, + Error **errp G_GNUC_UNUSED) +{ +QCryptoTLSCredsX509 *creds =QCRYPTO_TLS_CREDS_X509 (obj); + + creds->passwordid =g_strdup (value); +} + + +static char * +qcrypto_tls_creds_x509_prop_get_passwordid (Object *obj, + Error **errp G_GNUC_UNUSED) +{ +QCryptoTLSCredsX509 *creds =QCRYPTO_TLS_CREDS_X509 (obj); + +return g_strdup (creds->passwordid); +} + +static bool qcrypto_tls_creds_x509_prop_get_sanity (Object *obj, Error **errp G_GNUC_UNUSED)
@@ -763,6 +805,10 @@ qcrypto_tls_creds_x509_init(Object *obj) qcrypto_tls_creds_x509_prop_get_sanity, qcrypto_tls_creds_x509_prop_set_sanity, NULL); +object_property_add_str (obj,"passwordid" , + qcrypto_tls_creds_x509_prop_get_passwordid, + qcrypto_tls_creds_x509_prop_set_passwordid, +NULL ); }
@@ -771,6 +817,7 @@ qcrypto_tls_creds_x509_finalize(Object *obj) {QCryptoTLSCredsX509 *creds =QCRYPTO_TLS_CREDS_X509 (obj); +g_free (creds->passwordid);qcrypto_tls_creds_x509_unload (creds); }
include/crypto/tlscredsx509.h +1 -0
@@ -101,6 +101,7 @@ struct QCryptoTLSCredsX509 {gnutls_certificate_credentials_t data;#endif bool sanityCheck; +char *passwordid; };
qemu-options.hx +7 -1
@@ -3616,7 +3616,7 @@ expensive operation that consumes random pool entropy, so it is recommended that a persistent set of parameters be generated upfront and saved. -@item -object tls-creds-x509,id=@var{id},endpoint=@var{endpoint},dir=@var{/path/to/cred/dir},verify-peer=@var{on|off} +@item -object tls-creds-x509,id=@var{id},endpoint=@var{endpoint},dir=@var{/path/to/cred/dir},verify-peer=@var{on|off},passwordid=@var{id} Creates a TLS anonymous credentials object, which can be used to provide TLS support on network backends. The @option{id} parameter is a unique
@@ -3643,6 +3643,12 @@ in PEM format, in filenames @var{ca-cert.pem}, @var{ca-crl.pem} (optional), @var{server-cert.pem} (only servers), @var{server-key.pem} (only servers), @var{client-cert.pem} (only clients), and @var{client-key.pem} (only clients). +For the @var{server-key.pem} and @var{client-key.pem} files which +contain sensitive private keys, it is possible to use an encrypted +version by providing the @var{passwordid} parameter. This provides +the ID of a previously created @code{secret} object containing the +password for decryption. + @item -object filter-buffer,id=@var{id},netdev=@var{netdevid},interval=@var{t}[,queue=@var{all|rx|tx}] Interval @var{t} can't be 0, this filter batches the packet delivery: all
Signature · 1 line
2.4.3
PATCH 3/17 rbd: add support for getting password from QCryptoSecret object
In reply to Daniel P. BerrangeCurrently RBD passwords must be provided on the command line via
$QEMU -drive file=rbd:pool/image:id=myname:\
key=QVFDVm41aE82SHpGQWhBQXEwTkN2OGp0SmNJY0UrSE9CbE1RMUE=:\
auth_supported=cephxThis is insecure because the key is visible in the OS process listing.
This adds support for an 'authsecret' parameter in the RBD parameters that can be used with the QCryptoSecret object to provide the password via a file:
echo "QVFDVm41aE82SHpGQWhBQXEwTkN2OGp0SmNJY0UrSE9CbE1RMUE=" > poolkey.b64
$QEMU -object secret,id=secret0,file=poolkey.b64,format=base64 \
-drive file=rbd:pool/image:id=myname:\
auth_supported=cephx,authsecret=secret0--- block/rbd.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+)
block/rbd.c +42 -0
@@ -16,6 +16,7 @@#include "qemu-common.h" #include "qemu/error-report.h" #include "block/block_int.h" +#include "crypto/secret.h" #include <rbd/librbd.h>
@@ -228,6 +229,23 @@ static char *qemu_rbd_parse_clientname(const char *conf, char *clientname)return NULL ; } + +static int qemu_rbd_set_auth (rados_t cluster,const char *secretid, +Error **errp) +{ +gchar *secret =qcrypto_secret_lookup_as_base64 (secretid, + errp); +if (!secret) { +return -1 ; + } + +rados_conf_set (cluster,"key" , secret); +g_free (secret); + +return 0 ; +} + +static int qemu_rbd_set_conf (rados_t cluster,const char *conf,bool only_read_conf_file,Error **errp)
@@ -299,10 +317,13 @@ static int qemu_rbd_create(const char *filename, QemuOpts *opts, Error **errp)char conf[RBD_MAX_CONF_SIZE ];char clientname_buf[RBD_MAX_CONF_SIZE ];char *clientname; +const char *secretid;rados_t cluster;rados_ioctx_t io_ctx;int ret; + secretid =qemu_opt_get (opts,"authsecret" ); + if (qemu_rbd_parsename(filename, pool, sizeof(pool), snap_buf, sizeof(snap_buf), name, sizeof(name),
@@ -350,6 +371,11 @@ static int qemu_rbd_create(const char *filename, QemuOpts *opts, Error **errp)return -EIO ; } +if (qemu_rbd_set_auth (cluster, secretid, errp) <0 ) { +rados_shutdown (cluster); +return -EIO ; + } +if (rados_connect (cluster) <0 ) {error_setg (errp,"error connecting" );rados_shutdown (cluster);
@@ -423,6 +449,11 @@ static QemuOptsList runtime_opts = { .type = QEMU_OPT_STRING, .help = "Specification of the rbd image", }, + { + .name = "authsecret", + .type = QEMU_OPT_STRING, + .help = "ID of secret providing the password", + }, {/* end of list */ } }, };
@@ -436,6 +467,7 @@ static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags,char conf[RBD_MAX_CONF_SIZE ];char clientname_buf[RBD_MAX_CONF_SIZE ];char *clientname; +const char *secretid;QemuOpts *opts;Error *local_err =NULL ;const char *filename;
@@ -450,6 +482,7 @@ static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags, } filename =qemu_opt_get (opts,"filename" ); + secretid =qemu_opt_get (opts,"authsecret" ); if (qemu_rbd_parsename(filename, pool, sizeof(pool), snap_buf, sizeof(snap_buf),
@@ -488,6 +521,10 @@ static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags, } } +if (qemu_rbd_set_auth (s->cluster, secretid, errp) <0 ) { + goto failed_shutdown; + } +/* * Fallback to more conservative semantics if setting cache * options fails. Ignore errors from setting rbd_cache because the
@@ -919,6 +956,11 @@ static QemuOptsList qemu_rbd_create_opts = { .type = QEMU_OPT_SIZE, .help = "RBD object size" }, + { + .name = "authsecret", + .type = QEMU_OPT_STRING, + .help = "ID of secret providing the password", + }, {/* end of list */ } } };
Signature · 1 line
2.4.3
Re: [Qemu-devel] [PATCH 03/17] rbd: add support for getting password from QCryptoSecret object
Quoted 14 lines · Daniel P. Berrange
Currently RBD passwords must be provided on the command line via
$QEMU -drive file=rbd:pool/image:id=myname:\ key=QVFDVm41aE82SHpGQWhBQXEwTkN2OGp0SmNJY0UrSE9CbE1RMUE=:\ auth_supported=cephxThis is insecure because the key is visible in the OS process listing.
This adds support for an 'authsecret' parameter in the RBD parameters that can be used with the QCryptoSecret object to provide the password via a file:
Quoted 7 lines
echo "QVFDVm41aE82SHpGQWhBQXEwTkN2OGp0SmNJY0UrSE9CbE1RMUE=" > poolkey.b64 $QEMU -object secret,id=secret0,file=poolkey.b64,format=base64 \ -drive file=rbd:pool/image:id=myname:\ auth_supported=cephx,authsecret=secret0Signed-off-by Daniel P. Berrange <[email protected]>---
Looks good in general, thanks for fixing this! Just one thing to fix below.
Quoted 18 lines
block/rbd.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+)block/rbd.c +1
-0@@ -16,6 +16,7 @@#include "qemu-common.h" #include "qemu/error-report.h" #include "block/block_int.h" +#include "crypto/secret.h" #include <rbd/librbd.h> @@ -228,6 +229,23 @@ static char *qemu_rbd_parse_clientname(const char *conf, char *clientname)return NULL ; }
block/rbd.c static char *qemu_rbd_parse_clientname(const char *conf, char *clientname) · line 240
Quoted 10 lines
block/rbd.c +9
-0+ +static int qemu_rbd_set_auth (rados_t cluster,const char *secretid, +Error **errp) +{ +gchar *secret =qcrypto_secret_lookup_as_base64 (secretid, + errp); +if (!secret) { +return -1 ; + }
It looks like this fails if no authsecret is provided. Ceph auth can be disabled, so it seems like we should skip the qemu_rbd_set_auth() calls in this case.
Quoted 88 lines
block/rbd.c +32
-0+ +rados_conf_set (cluster,"key" , secret); +g_free (secret); + +return 0 ; +} + +static int qemu_rbd_set_conf (rados_t cluster,const char *conf,bool only_read_conf_file,Error **errp)@@ -299,10 +317,13 @@ static int qemu_rbd_create(const char *filename, QemuOpts *opts, Error **errp)char conf[RBD_MAX_CONF_SIZE ];char clientname_buf[RBD_MAX_CONF_SIZE ];char *clientname; +const char *secretid;rados_t cluster;rados_ioctx_t io_ctx;int ret; + secretid =qemu_opt_get (opts,"authsecret" ); + if (qemu_rbd_parsename(filename, pool, sizeof(pool), snap_buf, sizeof(snap_buf), name, sizeof(name),@@ -350,6 +371,11 @@ static int qemu_rbd_create(const char *filename, QemuOpts *opts, Error **errp)return -EIO ; } +if (qemu_rbd_set_auth (cluster, secretid, errp) <0 ) { +rados_shutdown (cluster); +return -EIO ; + } +if (rados_connect (cluster) <0 ) {error_setg (errp,"error connecting" );rados_shutdown (cluster);@@ -423,6 +449,11 @@ static QemuOptsList runtime_opts = { .type = QEMU_OPT_STRING, .help = "Specification of the rbd image", }, + { + .name = "authsecret", + .type = QEMU_OPT_STRING, + .help = "ID of secret providing the password", + }, {/* end of list */ } }, };@@ -436,6 +467,7 @@ static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags,char conf[RBD_MAX_CONF_SIZE ];char clientname_buf[RBD_MAX_CONF_SIZE ];char *clientname; +const char *secretid;QemuOpts *opts;Error *local_err =NULL ;const char *filename;@@ -450,6 +482,7 @@ static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags, } filename =qemu_opt_get (opts,"filename" ); + secretid =qemu_opt_get (opts,"authsecret" ); if (qemu_rbd_parsename(filename, pool, sizeof(pool), snap_buf, sizeof(snap_buf),@@ -488,6 +521,10 @@ static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags, } } +if (qemu_rbd_set_auth (s->cluster, secretid, errp) <0 ) { + goto failed_shutdown; + } +/* * Fallback to more conservative semantics if setting cache * options fails. Ignore errors from setting rbd_cache because the @@ -919,6 +956,11 @@ static QemuOptsList qemu_rbd_create_opts = { .type = QEMU_OPT_SIZE, .help = "RBD object size" }, + { + .name = "authsecret", + .type = QEMU_OPT_STRING, + .help = "ID of secret providing the password", + }, {/* end of list */ } } };
Re: [Qemu-devel] [PATCH 03/17] rbd: add support for getting password from QCryptoSecret object
Quoted 45 lines · Josh Durgin
Quoted 14 lines · Daniel P. Berrange
Currently RBD passwords must be provided on the command line via
$QEMU -drive file=rbd:pool/image:id=myname:\ key=QVFDVm41aE82SHpGQWhBQXEwTkN2OGp0SmNJY0UrSE9CbE1RMUE=:\ auth_supported=cephxThis is insecure because the key is visible in the OS process listing.
This adds support for an 'authsecret' parameter in the RBD parameters that can be used with the QCryptoSecret object to provide the password via a file:
Quoted 7 lines
echo "QVFDVm41aE82SHpGQWhBQXEwTkN2OGp0SmNJY0UrSE9CbE1RMUE=" > poolkey.b64 $QEMU -object secret,id=secret0,file=poolkey.b64,format=base64 \ -drive file=rbd:pool/image:id=myname:\ auth_supported=cephx,authsecret=secret0Signed-off-by Daniel P. Berrange <[email protected]>---
Looks good in general, thanks for fixing this! Just one thing to fix below.
Quoted 19 lines
block/rbd.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+)block/rbd.c +1
-0@@ -16,6 +16,7 @@#include "qemu-common.h" #include "qemu/error-report.h" #include "block/block_int.h" +#include "crypto/secret.h" #include <rbd/librbd.h> @@ -228,6 +229,23 @@ static char *qemu_rbd_parse_clientname(const char *conf, char *clientname)return NULL ; }
Quoted 13 lines
block/rbd.c static char *qemu_rbd_parse_clientname(const char *conf, char *clientname) · line 240
Quoted 9 lines
block/rbd.c +9
-0+ +static int qemu_rbd_set_auth (rados_t cluster,const char *secretid, +Error **errp) +{ +gchar *secret =qcrypto_secret_lookup_as_base64 (secretid, + errp); +if (!secret) { +return -1 ; + }It looks like this fails if no authsecret is provided. Ceph auth can be disabled, so it seems like we should skip the
qemu_rbd_set_auth()calls in this case.
This failure scenario happens if the user provides a key ID, but the corresponding QCryptoSecret does not exist. This is a usage error by the user, so it is appropriate to have it be a fatal error. In the case that they don't want any auth, they can just leave out the keyid parameter.
Regards,
Daniel
Signature · 4 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
PATCH 4/17 curl: add support for HTTP authentication parameters
In reply to Daniel P. BerrangeIf connecting to a web server which has authentication turned on, QEMU gets a 401 as curl has not been configured with any authentication credentials.
This adds 4 new parameters to the curl block driver options, username, passwordid, proxyusername and proxypasswordid.
$QEMU \
-object secret,id=sec0,filename=/home/berrange/example.pw \
-object secret,id=sec1,filename=/home/berrange/proxy.pw \
-drive driver=http,url=http://example.com/some.img,\
username=dan,passwordid=sec0,\
proxyusername=dan,passwordid=sec1Of course it is possible to use the same secret for both the proxy & server passwords if desired.
--- block/curl.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+)
block/curl.c +66 -0
@@ -26,6 +26,7 @@#include "block/block_int.h" #include "qapi/qmp/qbool.h" #include "qapi/qmp/qstring.h" +#include "crypto/secret.h" #include <curl/curl.h> // #define DEBUG_CURL
@@ -77,6 +78,10 @@ static CURLMcode __curl_multi_socket_action(CURLM *multi_handle,#define CURL_BLOCK_OPT_SSLVERIFY "sslverify"#define CURL_BLOCK_OPT_TIMEOUT "timeout"#define CURL_BLOCK_OPT_COOKIE "cookie" +#define CURL_BLOCK_OPT_USERNAME "username" +#define CURL_BLOCK_OPT_PASSWORDID "passwordid" +#define CURL_BLOCK_OPT_PROXY_USERNAME "proxyusername" +#define CURL_BLOCK_OPT_PROXY_PASSWORDID "proxypasswordid"struct BDRVCURLState ;
@@ -119,6 +124,10 @@ typedef struct BDRVCURLState {char *cookie;bool accept_range;AioContext *aio_context; +char *username; +char *password; +char *proxyusername; +char *proxypassword; } BDRVCURLState;static void curl_clean_state (CURLState *s);
@@ -416,6 +425,21 @@ static CURLState *curl_init_state(BlockDriverState *bs, BDRVCURLState *s)curl_easy_setopt (state->curl,CURLOPT_ERRORBUFFER , state->errmsg);curl_easy_setopt (state->curl,CURLOPT_FAILONERROR ,1 ); +if (s->username) { +curl_easy_setopt (state->curl,CURLOPT_USERNAME , s->username); + } +if (s->password) { +curl_easy_setopt (state->curl,CURLOPT_PASSWORD , s->password); + } +if (s->proxyusername) { +curl_easy_setopt (state->curl, +CURLOPT_PROXYUSERNAME , s->proxyusername); + } +if (s->proxypassword) { +curl_easy_setopt (state->curl, +CURLOPT_PROXYPASSWORD , s->proxypassword); + } +/* Restrict supported protocols to avoid security issues in the more * obscure protocols. For example, do not allow POP3/SMTP/IMAP see * CVE-2013-0249.
@@ -522,10 +546,31 @@ static QemuOptsList runtime_opts = { .type = QEMU_OPT_STRING, .help = "Pass the cookie or list of cookies with each request" }, + { + .name = CURL_BLOCK_OPT_USERNAME, + .type = QEMU_OPT_STRING, + .help ="Username for HTTP auth" + }, + { + .name = CURL_BLOCK_OPT_PASSWORDID, + .type = QEMU_OPT_STRING, + .help = "ID of secret used as password for HTTP auth", + }, + { + .name = CURL_BLOCK_OPT_PROXY_USERNAME, + .type = QEMU_OPT_STRING, + .help ="Username for HTTP proxy auth" + }, + { + .name = CURL_BLOCK_OPT_PROXY_PASSWORDID, + .type = QEMU_OPT_STRING, + .help = "ID of secret used as password for HTTP proxy auth", + }, { /* end of list */ } }, }; +static int curl_open (BlockDriverState *bs,QDict *options,int flags,Error **errp) {
@@ -536,6 +581,7 @@ static int curl_open(BlockDriverState *bs, QDict *options, int flags,const char *file;const char *cookie;double d; +const char *passwordid;static int inited =0 ;
@@ -577,6 +623,26 @@ static int curl_open(BlockDriverState *bs, QDict *options, int flags, goto out_noclean; } + s->username =g_strdup (qemu_opt_get (opts,CURL_BLOCK_OPT_USERNAME )); + passwordid =qemu_opt_get (opts,CURL_BLOCK_OPT_PASSWORDID ); + +if (passwordid) { + s->password =qcrypto_secret_lookup_as_utf8 (passwordid, errp); +if (!s->password) { + goto out_noclean; + } + } + + s->proxyusername =g_strdup ( +qemu_opt_get (opts,CURL_BLOCK_OPT_PROXY_USERNAME )); + passwordid =qemu_opt_get (opts,CURL_BLOCK_OPT_PROXY_PASSWORDID ); +if (passwordid) { + s->proxypassword =qcrypto_secret_lookup_as_utf8 (passwordid, errp); +if (!s->proxypassword) { + goto out_noclean; + } + } +if (!inited) {curl_global_init (CURL_GLOBAL_ALL ); inited =1 ;
Signature · 1 line
2.4.3
PATCH 5/17 iscsi: add support for getting CHAP password via QCryptoSecret API
In reply to Daniel P. BerrangeThe iSCSI driver currently accepts the CHAP password in plain text as a block driver property. This change adds a new "passwordid" property that accepts the ID of a QCryptoSecret instance.
$QEMU \
-object secret,id=sec0,filename=/home/berrange/example.pw \
-drive driver=iscsi,url=iscsi://example.com/target-foo/lun1,\
user=dan,passwordid=sec0--- block/iscsi.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-)
block/iscsi.c +23 -1
@@ -39,6 +39,7 @@#include "sysemu/sysemu.h" #include "qmp-commands.h" #include "qapi/qmp/qstring.h" +#include "crypto/secret.h" #include <iscsi/iscsi.h> #include <iscsi/scsi-lowlevel.h>
@@ -1018,6 +1019,8 @@ static void parse_chap(struct iscsi_context *iscsi, const char *target,QemuOpts *opts;const char *user =NULL ;const char *password =NULL ; +const char *passwordid; +char *secret =NULL ; list =qemu_find_opts ("iscsi" );if (!list) {
@@ -1037,8 +1040,20 @@ static void parse_chap(struct iscsi_context *iscsi, const char *target,return ; } + passwordid =qemu_opt_get (opts,"passwordid" ); password =qemu_opt_get (opts,"password" ); -if (!password) { +if (passwordid && password) { +error_setg (errp,"'password' and 'passwordid' properties are " +"mutually exclusive" ); +return ; + } +if (passwordid) { + secret =qcrypto_secret_lookup_as_utf8 (passwordid, errp); +if (!secret) { +return ; + } + password = secret; + }else if (!password) {error_setg (errp,"CHAP username specified but no password was given" );return ; }
@@ -1046,6 +1061,8 @@ static void parse_chap(struct iscsi_context *iscsi, const char *target,if (iscsi_set_initiator_username_pwd (iscsi, user, password)) {error_setg (errp,"Failed to set initiator username and password" ); } + +g_free (secret); } static void parse_header_digest(struct iscsi_context *iscsi, const char *target,
@@ -1793,6 +1810,11 @@ static QemuOptsList qemu_iscsi_opts = { .type = QEMU_OPT_STRING, .help = "password for CHAP authentication to target", },{ + .name = "passwordid", + .type = QEMU_OPT_STRING, + .help = "ID of the secret providing password for CHAP " + "authentication to target", + },{ .name = "header-digest", .type =QEMU_OPT_STRING , .help = "HeaderDigest setting. "
Signature · 1 line
2.4.3
PATCH 6/17 qcow: add a 'keyid' parameter to qcow options
In reply to Daniel P. BerrangeAdd a 'keyid' parameter that refers to the ID of a QCryptoSecret instance that provides the encryption key. eg
$QEMU \
-object secret,id=sec0,filename=/home/berrange/encrypted.pw \
-drive file=/home/berrange/encrypted.qcow,keyid=sec0--- block/qcow.c | 94 +++++++++++++++++++++++++++++++++++++++------------- qapi/block-core.json | 17 +++++++++- 2 files changed, 87 insertions(+), 24 deletions(-)
block/qcow.c +71 -23
@@ -27,6 +27,7 @@#include <zlib.h> #include "qapi/qmp/qerror.h" #include "crypto/cipher.h" +#include "crypto/secret.h" #include "migration/migration.h" /**************************************************************/
@@ -40,6 +41,8 @@#define QCOW_OFLAG_COMPRESSED (1LL << 63) +#define QCOW_OPT_KEY_ID "keyid" + typedef struct QCowHeader { uint32_t magic; uint32_t version;
@@ -92,6 +95,43 @@ static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)return 0 ; } +static QCryptoCipher *qcow_get_cipher_from_key (const char *key, +Error **errp) +{ +uint8_t keybuf[16 ]; +int len, i; + +memset (keybuf,0 ,16 ); + len =strlen (key); +if (len >16 ) { + len =16 ; + } +/* XXX: we could compress the chars to 7 bits to increase +entropy */ +for (i =0 ; i < len; i++) { + keybuf[i] = key[i]; + } + +return qcrypto_cipher_new ( +QCRYPTO_CIPHER_ALG_AES_128 , +QCRYPTO_CIPHER_MODE_CBC , + keybuf,G_N_ELEMENTS (keybuf), + errp); +} + +static QemuOptsList qcow_runtime_opts = { + .name ="qcow" , + .head =QTAILQ_HEAD_INITIALIZER (qcow_runtime_opts.head), + .desc = { + { + .name =QCOW_OPT_KEY_ID , + .type =QEMU_OPT_STRING , + .help ="ID of the secret that provides the encryption key" , + }, + {/* end of list */ } + }, +}; +static int qcow_open (BlockDriverState *bs,QDict *options,int flags,Error **errp) {
@@ -99,6 +139,10 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags,unsigned int len, i, shift;int ret;QCowHeader header; +QemuOpts *opts =NULL ; +const char *keyid; +char *key; +Error *local_err =NULL ; ret =bdrv_pread (bs->file->bs,0 , &header,sizeof (header));if (ret <0 ) {
@@ -147,6 +191,32 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags, goto fail; } + opts =qemu_opts_create (&qcow_runtime_opts,NULL ,0 , &error_abort); +qemu_opts_absorb_qdict (opts, options, &local_err); +if (local_err) { +error_propagate (errp, local_err); + ret = -EINVAL ; + goto fail; + } + + keyid =qemu_opt_get (opts,QCOW_OPT_KEY_ID ); +if (keyid) { + key =qcrypto_secret_lookup_as_utf8 (keyid, + errp); +if (!key) { + ret = -ENOENT ; + goto fail; + } + + s->cipher =qcow_get_cipher_from_key (key, + errp); +g_free (key); +if (!s->cipher) { + ret = -ENOSYS ; + goto fail; + } + } +if (header.crypt_method >QCOW_CRYPT_AES ) {error_setg (errp,"invalid encryption method in qcow header" ); ret = -EINVAL ;
@@ -261,33 +331,11 @@ static int qcow_reopen_prepare(BDRVReopenState *state,static int qcow_set_key (BlockDriverState *bs,const char *key) {BDRVQcowState *s = bs->opaque; -uint8_t keybuf[16 ]; -int len, i; -Error *err; -memset (keybuf,0 ,16 ); - len =strlen (key); -if (len >16 ) - len =16 ; -/* XXX: we could compress the chars to 7 bits to increase -entropy */ -for (i =0 ;i < len;i++) { - keybuf[i] = key[i]; - }assert (bs->encrypted); -qcrypto_cipher_free (s->cipher); - s->cipher =qcrypto_cipher_new ( -QCRYPTO_CIPHER_ALG_AES_128 , -QCRYPTO_CIPHER_MODE_CBC , - keybuf,G_N_ELEMENTS (keybuf), - &err); - + s->cipher =qcow_get_cipher_from_key (key,NULL );if (!s->cipher) { -/* XXX would be nice if errors in this method could -* be properly propagate to the caller. Would need -* the bdrv_set_key() API signature to be fixed. */ -error_free (err);return -1 ; }return 0 ;
qapi/block-core.json +16 -1
@@ -1562,6 +1562,21 @@ 'mode': 'Qcow2OverlapCheckMode' } } ## +# @BlockdevOptionsQcow +# +# Driver specific block device options for qcow. +# +# @keyid: #optional ID of the "secret" object providing the +# AES decryption key. +# +# Since: 2.5 +## +{ 'struct': 'BlockdevOptionsQcow', + 'base': 'BlockdevOptionsGenericCOWFormat', + 'data': { '*keyid': 'str' } } + + +## # @BlockdevOptionsQcow2 # # Driver specific block device options for qcow2.
@@ -1826,7 +1841,7 @@ 'null-co': 'BlockdevOptionsNull', 'parallels': 'BlockdevOptionsGenericFormat', 'qcow2': 'BlockdevOptionsQcow2', - 'qcow': 'BlockdevOptionsGenericCOWFormat', + 'qcow': 'BlockdevOptionsQcow', 'qed': 'BlockdevOptionsGenericCOWFormat', 'quorum': 'BlockdevOptionsQuorum', 'raw': 'BlockdevOptionsGenericFormat',
Signature · 1 line
2.4.3
Re: [Qemu-devel] [PATCH 06/17] qcow: add a 'keyid' parameter to qcow options
Quoted 21 lines · Daniel P. Berrange
Add a 'keyid' parameter that refers to the ID of a QCryptoSecret instance that provides the encryption key. eg
$QEMU \ -object secret,id=sec0,filename=/home/berrange/encrypted.pw \ -drive file=/home/berrange/encrypted.qcow,keyid=sec0Signed-off-by Daniel P. Berrange <[email protected]>--- block/qcow.c | 94 +++++++++++++++++++++++++++++++++++++++------------- qapi/block-core.json | 17 +++++++++- 2 files changed, 87 insertions(+), 24 deletions(-)block/qcow.c +6
-0+static QCryptoCipher *qcow_get_cipher_from_key (const char *key, +Error **errp) +{ +uint8_t keybuf[16 ]; +int len, i; +
block/qcow.c static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename) · line 113
Quoted 10 lines
block/qcow.c +10
-0+memset (keybuf,0 ,16 ); + len =strlen (key); +if (len >16 ) { + len =16 ; + } +/* XXX: we could compress the chars to 7 bits to increase +entropy */ +for (i =0 ; i < len; i++) { + keybuf[i] = key[i]; + }
Would memcpy() be more efficient?
Quoted 7 lines
block/qcow.c +0
-3@@ -261,33 +331,11 @@ static int qcow_reopen_prepare(BDRVReopenState *state,static int qcow_set_key (BlockDriverState *bs,const char *key) {BDRVQcowState *s = bs->opaque; -uint8_t keybuf[16 ]; -int len, i; -Error *err;
block/qcow.c static int qcow_reopen_prepare(BDRVReopenState *state, · line 276
Quoted 10 lines
block/qcow.c +0
-9-memset (keybuf,0 ,16 ); - len =strlen (key); -if (len >16 ) - len =16 ; -/* XXX: we could compress the chars to 7 bits to increase -entropy */ -for (i =0 ;i < len;i++) { - keybuf[i] = key[i]; - }
Oh, I see - code motion.
qapi/block-core.json line 1571
Quoted 12 lines
qapi/block-core.json +8
-0+++ b/qapi/block-core.json@@ -1562,6 +1562,21 @@ 'mode': 'Qcow2OverlapCheckMode' } } ## +# @BlockdevOptionsQcow +# +# Driver specific block device options for qcow. +# +# @keyid: #optional ID of the "secret" object providing the +# AES decryption key. +#
That's a lot of whitespace, but it doesn't hurt.
qapi/block-core.json line 1576
Quoted 5 lines
qapi/block-core.json +5
-0+# Since: 2.5 +## +{ 'struct': 'BlockdevOptionsQcow', + 'base': 'BlockdevOptionsGenericCOWFormat', + 'data': { '*keyid': 'str' } }
Interface looks fine.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH 7/17 qcow2: add a 'keyid' parameter to qcow2 options
In reply to Daniel P. BerrangeAdd a 'keyid' parameter that refers to the ID of a QCryptoSecret instance that provides the encryption key.
$QEMU \
-object secret,id=sec0,filename=/home/berrange/encrypted.pw \
-drive file=/home/berrange/encrypted.qcow2,keyid=sec0--- block/qcow2.c | 80 +++++++++++++++++++++++++++++++++++++--------------- block/qcow2.h | 1 + qapi/block-core.json | 8 ++++-- 3 files changed, 64 insertions(+), 25 deletions(-)
block/qcow2.c +57 -23
@@ -34,6 +34,7 @@#include "qapi-event.h" #include "trace.h" #include "qemu/option_int.h" +#include "crypto/secret.h" /* Differences with QCOW:
@@ -472,6 +473,11 @@ static QemuOptsList qcow2_runtime_opts = { .type = QEMU_OPT_NUMBER, .help = "Clean unused cache entries after this time (in seconds)", }, + { + .name = QCOW2_OPT_KEY_ID, + .type = QEMU_OPT_STRING, + .help = "ID of the secret that provides the encryption key", + }, {/* end of list */ } }, };
@@ -589,6 +595,31 @@ static void read_cache_sizes(BlockDriverState *bs, QemuOpts *opts, } } +static QCryptoCipher *qcow2_get_cipher_from_key (const char *key, +Error **errp) +{ +uint8_t keybuf[16 ]; +int len, i; + +memset (keybuf,0 ,16 ); + len =strlen (key); +if (len >16 ) { + len =16 ; + } +/* XXX: we could compress the chars to 7 bits to increase +entropy */ +for (i =0 ; i < len; i++) { + keybuf[i] = key[i]; + } + +return qcrypto_cipher_new ( +QCRYPTO_CIPHER_ALG_AES_128 , +QCRYPTO_CIPHER_MODE_CBC , + keybuf,G_N_ELEMENTS (keybuf), + errp); +} + + typedef struct Qcow2ReopenState { Qcow2Cache *l2_table_cache; Qcow2Cache *refcount_block_cache;
@@ -596,6 +627,7 @@ typedef struct Qcow2ReopenState {int overlap_check;bool discard_passthrough[QCOW2_DISCARD_MAX ];uint64_t cache_clean_interval; +QCryptoCipher *cipher; } Qcow2ReopenState; static int qcow2_update_options_prepare(BlockDriverState *bs,
@@ -611,6 +643,8 @@ static int qcow2_update_options_prepare(BlockDriverState *bs,int i;Error *local_err =NULL ;int ret; +const char *keyid; +char *key; opts =qemu_opts_create (&qcow2_runtime_opts,NULL ,0 , &error_abort);qemu_opts_absorb_qdict (opts, options, &local_err);
@@ -754,6 +788,24 @@ static int qcow2_update_options_prepare(BlockDriverState *bs, r->discard_passthrough[QCOW2_DISCARD_OTHER ] =qemu_opt_get_bool (opts,QCOW2_OPT_DISCARD_OTHER , false); + keyid =qemu_opt_get (opts,QCOW2_OPT_KEY_ID ); +if (keyid) { + key =qcrypto_secret_lookup_as_utf8 (keyid, + errp); +if (!key) { + ret = -ENOENT ; + goto fail; + } + + r->cipher =qcow2_get_cipher_from_key (key, + errp); +g_free (key); +if (!r->cipher) { + ret = -ENOSYS ; + goto fail; + } + } + ret =0 ; fail:qemu_opts_del (opts);
@@ -788,6 +840,9 @@ static void qcow2_update_options_commit(BlockDriverState *bs, s->cache_clean_interval = r->cache_clean_interval;cache_clean_timer_init (bs,bdrv_get_aio_context (bs)); } + +qcrypto_cipher_free (s->cipher); + s->cipher = r->cipher; } static void qcow2_update_options_abort(BlockDriverState *bs,
@@ -799,6 +854,7 @@ static void qcow2_update_options_abort(BlockDriverState *bs,if (r->refcount_block_cache) {qcow2_cache_destroy (bs, r->refcount_block_cache); } +qcrypto_cipher_free (r->cipher); } static int qcow2_update_options(BlockDriverState *bs, QDict *options,
@@ -1202,33 +1258,11 @@ static void qcow2_refresh_limits(BlockDriverState *bs, Error **errp)static int qcow2_set_key (BlockDriverState *bs,const char *key) {BDRVQcow2State *s = bs->opaque; -uint8_t keybuf[16 ]; -int len, i; -Error *err =NULL ; -memset (keybuf,0 ,16 ); - len =strlen (key); -if (len >16 ) - len =16 ; -/* XXX: we could compress the chars to 7 bits to increase -entropy */ -for (i =0 ;i < len;i++) { - keybuf[i] = key[i]; - }assert (bs->encrypted); -qcrypto_cipher_free (s->cipher); - s->cipher =qcrypto_cipher_new ( -QCRYPTO_CIPHER_ALG_AES_128 , -QCRYPTO_CIPHER_MODE_CBC , - keybuf,G_N_ELEMENTS (keybuf), - &err); - + s->cipher =qcow2_get_cipher_from_key (key,NULL );if (!s->cipher) { -/* XXX would be nice if errors in this method could -* be properly propagate to the caller. Would need -* the bdrv_set_key() API signature to be fixed. */ -error_free (err);return -1 ; }return 0 ;
block/qcow2.h +1 -0
@@ -97,6 +97,7 @@#define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size"#define QCOW2_OPT_REFCOUNT_CACHE_SIZE "refcount-cache-size"#define QCOW2_OPT_CACHE_CLEAN_INTERVAL "cache-clean-interval" +#define QCOW2_OPT_KEY_ID "keyid" typedef struct QCowHeader { uint32_t magic;
qapi/block-core.json +6 -2
@@ -1567,7 +1567,7 @@ # Driver specific block device options for qcow. # # @keyid: #optional ID of the "secret" object providing the -# AES decryption key. +# AES decryption key (since 2.5) # # Since: 2.5 ##
@@ -1611,6 +1611,9 @@ # caches. The interval is in seconds. The default value # is 0 and it disables this feature (since 2.5) # +# @keyid: #optional ID of the "secret" object providing the +# AES decryption key. +# # Since: 1.7 ## { 'struct': 'BlockdevOptionsQcow2',
@@ -1623,7 +1626,8 @@ '*cache-size': 'int', '*l2-cache-size': 'int', '*refcount-cache-size': 'int', - '*cache-clean-interval': 'int' } } + '*cache-clean-interval': 'int', + '*keyid': 'str' } } ##
Signature · 1 line
2.4.3
Re: [Qemu-devel] [PATCH 07/17] qcow2: add a 'keyid' parameter to qcow2 options
Quoted 17 lines · Daniel P. Berrange
Add a 'keyid' parameter that refers to the ID of a QCryptoSecret instance that provides the encryption key.
$QEMU \ -object secret,id=sec0,filename=/home/berrange/encrypted.pw \ -drive file=/home/berrange/encrypted.qcow2,keyid=sec0Signed-off-by Daniel P. Berrange <[email protected]>--- block/qcow2.c | 80 +++++++++++++++++++++++++++++++++++++--------------- block/qcow2.h | 1 + qapi/block-core.json | 8 ++++-- 3 files changed, 64 insertions(+), 25 deletions(-)+++
b/qapi/block-core.json
qapi/block-core.json line 1570
Quoted 5 lines
qapi/block-core.json +1
-1@@ -1567,7 +1567,7 @@ # Driver specific block device options for qcow. # # @keyid: #optional ID of the "secret" object providing the -# AES decryption key. +# AES decryption key (since 2.5)
Looks like this line...
qapi/block-core.json line 1615
Quoted 9 lines
qapi/block-core.json +2
-0# # Since: 2.5 ##@@ -1611,6 +1611,9 @@ # caches. The interval is in seconds. The default value # is 0 and it disables this feature (since 2.5) # +# @keyid: #optional ID of the "secret" object providing the +# AES decryption key.
...and this line should be swapped.
qapi/block-core.json line 1617
Quoted 2 lines
qapi/block-core.json +1
-0+# # Since: 1.7
(For qcow, the entire struct is new so @keyid doesn't need versioning; for qcow2, the struct existed since 1.7 and we are extending it in 2.5)
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH 07/17] qcow2: add a 'keyid' parameter to qcow2 options
Quoted 26 lines · Eric Blake
Quoted 17 lines · Daniel P. Berrange
Add a 'keyid' parameter that refers to the ID of a QCryptoSecret instance that provides the encryption key.
$QEMU \ -object secret,id=sec0,filename=/home/berrange/encrypted.pw \ -drive file=/home/berrange/encrypted.qcow2,keyid=sec0Signed-off-by Daniel P. Berrange <[email protected]>--- block/qcow2.c | 80 +++++++++++++++++++++++++++++++++++++--------------- block/qcow2.h | 1 + qapi/block-core.json | 8 ++++-- 3 files changed, 64 insertions(+), 25 deletions(-)+++
b/qapi/block-core.jsonqapi/block-core.json line 1570
Quoted 5 lines
qapi/block-core.json +1
-1@@ -1567,7 +1567,7 @@ # Driver specific block device options for qcow. # # @keyid: #optional ID of the "secret" object providing the -# AES decryption key. +# AES decryption key (since 2.5)Looks like this line...
Quoted 12 lines
qapi/block-core.json line 1615
Quoted 9 lines
qapi/block-core.json +2
-0# # Since: 2.5 ##@@ -1611,6 +1611,9 @@ # caches. The interval is in seconds. The default value # is 0 and it disables this feature (since 2.5) # +# @keyid: #optional ID of the "secret" object providing the +# AES decryption key....and this line should be swapped.
Also, do you want to change BlockdevOptionsQcow2 to have a base class of BlockdevOptionsQcow, and get keyid by inheritance rather than by direct declaration? Doesn't matter in the long run (once my qapi patches land that provide the information without going through an extra 'base->' layer).
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH 8/17 qom: add user_creatable_add & user_creatable_del methods
In reply to Daniel P. BerrangeThe QMP monitor code has two helper methods object_add and qmp_object_del that are called from several places in the code (QMP, HMP and main emulator startup).
We soon need to use this code from qemu-img, qemu-io and qemu-nbd too, but don't want those to depend on the monitor.
To avoid this, move object_add to user_creatable_add an qmp_object_del to user_creatable_del, in the object_interfaces.c file
--- hmp.c | 11 ++++-- include/monitor/monitor.h | 3 -- include/qom/object_interfaces.h | 31 +++++++++++++++++ qmp.c | 75 ++++------------------------------------ qom/object_interfaces.c | 76 +++++++++++++++++++++++++++++++++++++++++ vl.c | 8 +++-- 6 files changed, 127 insertions(+), 77 deletions(-)
hmp.c +8 -3
@@ -28,6 +28,7 @@#include "qapi/qmp/qerror.h" #include "qapi/string-output-visitor.h" #include "qapi-visit.h" +#include "qom/object_interfaces.h" #include "ui/console.h" #include "block/qapi.h" #include "qemu-io.h"
@@ -1630,6 +1631,7 @@ void hmp_object_add(Monitor *mon, const QDict *qdict)void *dummy =NULL ;OptsVisitor *ov;QDict *pdict; +Object *obj =NULL ; opts =qemu_opts_from_qdict (qemu_find_opts ("object" ), qdict, &err);if (err) {
@@ -1656,12 +1658,12 @@ void hmp_object_add(Monitor *mon, const QDict *qdict) goto out_end; } -object_add (type, id, pdict,opts_get_visitor (ov), &err); + obj =user_creatable_add (type, id, pdict,opts_get_visitor (ov), &err); out_end:visit_end_struct (opts_get_visitor (ov), &err_end);if (!err && err_end) { -qmp_object_del (id,NULL ); +user_creatable_del (id,NULL ); }error_propagate (&err, err_end); out_clean:
@@ -1672,6 +1674,9 @@ out_clean:g_free (id);g_free (type);g_free (dummy); +if (obj) { +object_unref (obj); + } out:hmp_handle_error (mon, &err);
@@ -1904,7 +1909,7 @@ void hmp_object_del(Monitor *mon, const QDict *qdict)const char *id =qdict_get_str (qdict,"id" );Error *err =NULL ; -qmp_object_del (id, &err); +user_creatable_del (id, &err);hmp_handle_error (mon, &err); }
include/monitor/monitor.h +0 -3
@@ -43,9 +43,6 @@ void monitor_read_command(Monitor *mon, int show_prompt);int monitor_read_password (Monitor *mon,ReadLineFunc *readline_func,void *opaque); -void object_add (const char *type,const char *id,const QDict *qdict, -Visitor *v,Error **errp); -AddfdInfo *monitor_fdset_add_fd (int fd,bool has_fdset_id,int64_t fdset_id,bool has_opaque,const char *opaque,Error **errp);
include/qom/object_interfaces.h +31 -0
@@ -2,6 +2,8 @@#define OBJECT_INTERFACES_H #include "qom/object.h" +#include "qapi/qmp/qdict.h" +#include "qapi/visitor.h" #define TYPE_USER_CREATABLE "user-creatable"
@@ -72,4 +74,33 @@ void user_creatable_complete(Object *obj, Error **errp);* from implements USER_CREATABLE interface. */ bool user_creatable_can_be_deleted (UserCreatable *uc,Error **errp); + +/** +* user_creatable_add: +* @type: the object type name +* @id: the unique ID for the object +* @qdict: the object parameters +* @v: the visitor +* @errp: if an error occurs, a pointer to an area to store the error +* +* Create an instance of the user creatable object @type, placing +* it in the object composition tree with name @id, initializing +* it with properties from @qdict +* +* Returns: the newly created object or NULL on error +*/ +Object *user_creatable_add (const char *type,const char *id, +const QDict *qdict, +Visitor *v,Error **errp); + +/** +* user_creatable_del: +* @id: the unique ID for the object +* @errp: if an error occurs, a pointer to an area to store the error +* +* Delete an instance of the user creatable object identified +* by @id. +*/ +void user_creatable_del (const char *id,Error **errp); +#endif
qmp.c +6 -69
98 lines not on this page · raw message
qom/object_interfaces.c +76 -0
82 lines not on this page · raw message
vl.c +6 -2
@@ -2775,6 +2775,7 @@ static int object_create(void *opaque, QemuOpts *opts, Error **errp)OptsVisitor *ov;QDict *pdict;bool (*type_predicate)(const char *) = opaque; +Object *obj =NULL ; ov =opts_visitor_new (opts); pdict =qemu_opts_to_qdict (opts,NULL );
@@ -2799,13 +2800,13 @@ static int object_create(void *opaque, QemuOpts *opts, Error **errp) goto out; } -object_add (type, id, pdict,opts_get_visitor (ov), &err); + obj =user_creatable_add (type, id, pdict,opts_get_visitor (ov), &err);if (err) { goto out; }visit_end_struct (opts_get_visitor (ov), &err);if (err) { -qmp_object_del (id,NULL ); +user_creatable_del (id,NULL ); } out:
@@ -2815,6 +2816,9 @@ out:g_free (id);g_free (type);g_free (dummy); +if (obj) { +object_unref (obj); + }if (err) {error_report_err (err);return -1 ;
Signature · 1 line
2.4.3
PATCH 9/17 qemu-img: add support for --object command line arg
In reply to Daniel P. BerrangeAllow creation of user creatable object types with qemu-img via a --object command line arg. This will be used to supply passwords and/or encryption keys to the various block driver backends via the recently added 'secret' object type.
# echo -n letmein > mypasswd.txt
# qemu-img info --object secret,id=sec0,file=mypasswd.txt \
...other info args...--- qemu-img-cmds.hx | 44 ++++---- qemu-img.c | 300 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- qemu-img.texi | 8 ++ 3 files changed, 322 insertions(+), 30 deletions(-)
qemu-img-cmds.hx +22 -22
@@ -10,68 +10,68 @@ STEXI ETEXI DEF("check", img_check, - "check [-q] [-f fmt] [--output=ofmt] [-r [leaks | all]] [-T src_cache] filename") + "check [-q] [--object objectdef] [-f fmt] [--output=ofmt] [-r [leaks | all]] [-T src_cache] filename") STEXI -@item check [-q] [-f @var{fmt}] [--output=@var{ofmt}] [-r [leaks | all]] [-T @var{src_cache}] @var{filename} +@item check [--object objectdef] [-q] [-f @var{fmt}] [--output=@var{ofmt}] [-r [leaks | all]] [-T @var{src_cache}] @var{filename} ETEXI DEF("create", img_create, - "create [-q] [-f fmt] [-o options] filename [size]") + "create [-q] [--object objectdef] [-f fmt] [-o options] filename [size]") STEXI -@item create [-q] [-f @var{fmt}] [-o @var{options}] @var{filename} [@var{size}] +@item create [--object objectdef] [-q] [-f @var{fmt}] [-o @var{options}] @var{filename} [@var{size}] ETEXI DEF("commit", img_commit, - "commit [-q] [-f fmt] [-t cache] [-b base] [-d] [-p] filename") + "commit [-q] [--object objectdef] [-f fmt] [-t cache] [-b base] [-d] [-p] filename") STEXI -@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-b @var{base}] [-d] [-p] @var{filename} +@item commit [--object objectdef] [-q] [-f @var{fmt}] [-t @var{cache}] [-b @var{base}] [-d] [-p] @var{filename} ETEXI DEF("compare", img_compare, - "compare [-f fmt] [-F fmt] [-T src_cache] [-p] [-q] [-s] filename1 filename2") + "compare [--object objectdef] [-f fmt] [-F fmt] [-T src_cache] [-p] [-q] [-s] filename1 filename2") STEXI -@item compare [-f @var{fmt}] [-F @var{fmt}] [-T @var{src_cache}] [-p] [-q] [-s] @var{filename1} @var{filename2} +@item compare [--object objectdef] [-f @var{fmt}] [-F @var{fmt}] [-T @var{src_cache}] [-p] [-q] [-s] @var{filename1} @var{filename2} ETEXI DEF("convert", img_convert, - "convert [-c] [-p] [-q] [-n] [-f fmt] [-t cache] [-T src_cache] [-O output_fmt] [-o options] [-s snapshot_id_or_name] [-l snapshot_param] [-S sparse_size] filename [filename2 [...]] output_filename") + "convert [--object objectdef] [-c] [-p] [-q] [-n] [-f fmt] [-t cache] [-T src_cache] [-O output_fmt] [-o options] [-s snapshot_id_or_name] [-l snapshot_param] [-S sparse_size] filename [filename2 [...]] output_filename") STEXI -@item convert [-c] [-p] [-q] [-n] [-f @var{fmt}] [-t @var{cache}] [-T @var{src_cache}] [-O @var{output_fmt}] [-o @var{options}] [-s @var{snapshot_id_or_name}] [-l @var{snapshot_param}] [-S @var{sparse_size}] @var{filename} [@var{filename2} [...]] @var{output_filename} +@item convert [--object objectdef] [-c] [-p] [-q] [-n] [-f @var{fmt}] [-t @var{cache}] [-T @var{src_cache}] [-O @var{output_fmt}] [-o @var{options}] [-s @var{snapshot_id_or_name}] [-l @var{snapshot_param}] [-S @var{sparse_size}] @var{filename} [@var{filename2} [...]] @var{output_filename} ETEXI DEF("info", img_info, - "info [-f fmt] [--output=ofmt] [--backing-chain] filename") + "info [--object objectdef] [-f fmt] [--output=ofmt] [--backing-chain] filename") STEXI -@item info [-f @var{fmt}] [--output=@var{ofmt}] [--backing-chain] @var{filename} +@item info [--object objectdef] [-f @var{fmt}] [--output=@var{ofmt}] [--backing-chain] @var{filename} ETEXI DEF("map", img_map, - "map [-f fmt] [--output=ofmt] filename") + "map [--object objectdef] [-f fmt] [--output=ofmt] filename") STEXI -@item map [-f @var{fmt}] [--output=@var{ofmt}] @var{filename} +@item map [--object objectdef] [-f @var{fmt}] [--output=@var{ofmt}] @var{filename} ETEXI DEF("snapshot", img_snapshot, - "snapshot [-q] [-l | -a snapshot | -c snapshot | -d snapshot] filename") + "snapshot [--object objectdef] [-q] [-l | -a snapshot | -c snapshot | -d snapshot] filename") STEXI -@item snapshot [-q] [-l | -a @var{snapshot} | -c @var{snapshot} | -d @var{snapshot}] @var{filename} +@item snapshot [--object objectdef] [-q] [-l | -a @var{snapshot} | -c @var{snapshot} | -d @var{snapshot}] @var{filename} ETEXI DEF("rebase", img_rebase, - "rebase [-q] [-f fmt] [-t cache] [-T src_cache] [-p] [-u] -b backing_file [-F backing_fmt] filename") + "rebase [--object objectdef] [-q] [-f fmt] [-t cache] [-T src_cache] [-p] [-u] -b backing_file [-F backing_fmt] filename") STEXI -@item rebase [-q] [-f @var{fmt}] [-t @var{cache}] [-T @var{src_cache}] [-p] [-u] -b @var{backing_file} [-F @var{backing_fmt}] @var{filename} +@item rebase [--object objectdef] [-q] [-f @var{fmt}] [-t @var{cache}] [-T @var{src_cache}] [-p] [-u] -b @var{backing_file} [-F @var{backing_fmt}] @var{filename} ETEXI DEF("resize", img_resize, - "resize [-q] filename [+ | -]size") + "resize [--object objectdef] [-q] filename [+ | -]size") STEXI -@item resize [-q] @var{filename} [+ | -]@var{size} +@item resize [--object objectdef] [-q] @var{filename} [+ | -]@var{size} ETEXI DEF("amend", img_amend, - "amend [-p] [-q] [-f fmt] [-t cache] -o options filename") + "amend [--object objectdef] [-p] [-q] [-f fmt] [-t cache] -o options filename") STEXI -@item amend [-p] [-q] [-f @var{fmt}] [-t @var{cache}] -o @var{options} @var{filename} +@item amend [--object objectdef] [-p] [-q] [-f @var{fmt}] [-t @var{cache}] -o @var{options} @var{filename} @end table ETEXI
qemu-img.c +292 -8
575 lines not on this page · raw message
qemu-img.texi +8 -0
@@ -24,6 +24,14 @@ Command parameters: @table @var @item filename is a disk image filename + +@item objectdef + +is a QEMU user creatable object definition. See the @code{qemu(1)} manual +page for a description of the object properties. The only object type that +it makes sense to define is the @code{secret} object, which is used to +supply passwords and/or encryption keys. + @item fmt is the disk image format. It is guessed automatically in most cases. See below for a description of the supported disk formats.
Signature · 1 line
2.4.3
PATCH 10/17 qemu-nbd: add support for --object command line arg
In reply to Daniel P. BerrangeAllow creation of user creatable object types with qemu-nbd via a --object command line arg. This will be used to supply passwords and/or encryption keys to the various block driver backends via the recently added 'secret' object type.
# echo -n letmein > mypasswd.txt
# qemu-nbd --object secret,id=sec0,file=mypasswd.txt \
...other nbd args...--- qemu-nbd.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-nbd.texi | 7 +++++ 2 files changed, 92 insertions(+)
qemu-nbd.c +85 -0
@@ -23,9 +23,12 @@#include "qemu/main-loop.h" #include "qemu/sockets.h" #include "qemu/error-report.h" +#include "qemu/config-file.h" #include "block/snapshot.h" #include "qapi/util.h" #include "qapi/qmp/qstring.h" +#include "qapi/opts-visitor.h" +#include "qom/object_interfaces.h" #include <stdarg.h> #include <stdio.h>
@@ -45,6 +48,7 @@#define QEMU_NBD_OPT_AIO 2#define QEMU_NBD_OPT_DISCARD 3#define QEMU_NBD_OPT_DETECT_ZEROES 4 +#define QEMU_NBD_OPT_OBJECT 5static NBDExport *exp;static int verbose;
@@ -78,6 +82,9 @@ static void usage(const char *name)" -o, --offset=OFFSET offset into the image\n" " -P, --partition=NUM only expose partition NUM\n" "\n" +"General purpose options:\n" +" --object type,id=ID,... define an object such as 'secret' for providing\n" +" passwords and/or encryption keys\n" #ifdef __linux__"Kernel NBD client support:\n" " -c, --connect=DEV connect FILE to the local NBD device DEV\n"
@@ -380,6 +387,67 @@ static SocketAddress *nbd_build_socket_address(const char *sockpath, } +static QemuOptsList qemu_object_opts = { + .name ="object" , + .implied_opt_name ="qom-type" , + .head =QTAILQ_HEAD_INITIALIZER (qemu_object_opts.head), + .desc = { + { } + }, +}; + +static int object_create (void *opaque,QemuOpts *opts,Error **errp) +{ +Error *err =NULL ; +char *type =NULL ; +char *id =NULL ; +void *dummy =NULL ; +OptsVisitor *ov; +QDict *pdict; + + ov =opts_visitor_new (opts); + pdict =qemu_opts_to_qdict (opts,NULL ); + +visit_start_struct (opts_get_visitor (ov), &dummy,NULL ,NULL ,0 , &err); +if (err) { + goto out; + } + +qdict_del (pdict,"qom-type" ); +visit_type_str (opts_get_visitor (ov), &type,"qom-type" , &err); +if (err) { + goto out; + } + +qdict_del (pdict,"id" ); +visit_type_str (opts_get_visitor (ov), &id,"id" , &err); +if (err) { + goto out; + } + +user_creatable_add (type, id, pdict,opts_get_visitor (ov), &err); +if (err) { + goto out; + } +visit_end_struct (opts_get_visitor (ov), &err); +if (err) { +user_creatable_del (id,NULL ); + } + +out: +opts_visitor_cleanup (ov); + +QDECREF (pdict); +g_free (id); +g_free (type); +g_free (dummy); +if (err) { +error_report_err (err); +return -1 ; + } +return 0 ; +} +int main (int argc,char **argv) {BlockBackend *blk;
@@ -417,6 +485,7 @@ int main(int argc, char **argv) { "format", 1, NULL, 'f' }, { "persistent", 0, NULL, 't' }, { "verbose", 0, NULL, 'v' }, + { "object", 1, NULL, QEMU_NBD_OPT_OBJECT }, { NULL, 0, NULL, 0 } };int ch;
@@ -434,6 +503,7 @@ int main(int argc, char **argv)Error *local_err =NULL ;BlockdevDetectZeroesOptions detect_zeroes =BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF ;QDict *options =NULL ; +QemuOpts *opts;/* The client thread uses SIGTERM to interrupt the server. A signal * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
@@ -442,6 +512,8 @@ int main(int argc, char **argv)memset (&sa_sigterm,0 ,sizeof (sa_sigterm)); sa_sigterm.sa_handler = termsig_handler;sigaction (SIGTERM , &sa_sigterm,NULL ); +module_call_init (MODULE_INIT_QOM ); +qemu_add_opts (&qemu_object_opts);qemu_init_exec_dir (argv[0 ]);while ((ch =getopt_long (argc, argv, sopt, lopt, &opt_ind)) !=-1 ) {
@@ -578,6 +650,13 @@ int main(int argc, char **argv)usage (argv[0 ]);exit (0 );break ; +case QEMU_NBD_OPT_OBJECT : + opts =qemu_opts_parse_noisily (qemu_find_opts ("object" ), + optarg, true); +if (!opts) { +exit (1 ); + } +break ;case '?' :errx (EXIT_FAILURE ,"Try `%s --help' for more information." , argv[0 ]);
@@ -590,6 +669,12 @@ int main(int argc, char **argv) argv[0]); } +if (qemu_opts_foreach (qemu_find_opts ("object" ), + object_create, +NULL ,NULL )) { +exit (1 ); + } +if (disconnect) { fd =open (argv[optind],O_RDWR );if (fd <0 ) {
qemu-nbd.texi +7 -0
@@ -14,6 +14,13 @@ Export QEMU disk image using NBD protocol. @table @option @item @var{filename} is a disk image filename +@item --object type,id=@var{id},...props... + define a new instance of the @var{type} object class + identified by @var{id}. See the @code{qemu(1)} manual + page for full details of the properties supported. + The only object type that it makes sense to define + is the @code{secret} object, which is used to supply + passwords and/or encryption keys. @item -p, --port=@var{port} port to listen on (default @samp{10809}) @item -o, --offset=@var{offset}
Signature · 1 line
2.4.3
PATCH 11/17 qemu-io: add support for --object command line arg
In reply to Daniel P. BerrangeAllow creation of user creatable object types with qemu-io via a --object command line arg. This will be used to supply passwords and/or encryption keys to the various block driver backends via the recently added 'secret' object type.
# echo -n letmein > mypasswd.txt
# qemu-io --object secret,id=sec0,file=mypasswd.txt \
...other args...--- qemu-io.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+)
qemu-io.c +87 -0
@@ -21,6 +21,8 @@#include "qemu/config-file.h" #include "qemu/readline.h" #include "qapi/qmp/qstring.h" +#include "qapi/opts-visitor.h" +#include "qom/object_interfaces.h" #include "sysemu/block-backend.h" #include "block/block_int.h" #include "trace/control.h"
@@ -205,6 +207,9 @@ static void usage(const char *name)"Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n" "QEMU Disk exerciser\n" "\n" +" --object OBJECTDEF define a object such as 'secret' for\n" +" providing passwords and/or encryption\n" +" keys\n" " -c, --cmd STRING execute command with its arguments\n" " from the given string\n" " -f, --format FMT specifies the block driver to use\n"
@@ -366,6 +371,71 @@ static void reenable_tty_echo(void)qemu_set_tty_echo (STDIN_FILENO , true); } +enum { +OPTION_OBJECT =258 , +}; + +static QemuOptsList qemu_object_opts = { + .name ="object" , + .implied_opt_name ="qom-type" , + .head =QTAILQ_HEAD_INITIALIZER (qemu_object_opts.head), + .desc = { + { } + }, +}; + +static int object_create (void *opaque,QemuOpts *opts,Error **errp) +{ +Error *err =NULL ; +char *type =NULL ; +char *id =NULL ; +void *dummy =NULL ; +OptsVisitor *ov; +QDict *pdict; + + ov =opts_visitor_new (opts); + pdict =qemu_opts_to_qdict (opts,NULL ); + +visit_start_struct (opts_get_visitor (ov), &dummy,NULL ,NULL ,0 , &err); +if (err) { + goto out; + } + +qdict_del (pdict,"qom-type" ); +visit_type_str (opts_get_visitor (ov), &type,"qom-type" , &err); +if (err) { + goto out; + } + +qdict_del (pdict,"id" ); +visit_type_str (opts_get_visitor (ov), &id,"id" , &err); +if (err) { + goto out; + } + +user_creatable_add (type, id, pdict,opts_get_visitor (ov), &err); +if (err) { + goto out; + } +visit_end_struct (opts_get_visitor (ov), &err); +if (err) { +user_creatable_del (id,NULL ); + } + +out: +opts_visitor_cleanup (ov); + +QDECREF (pdict); +g_free (id); +g_free (type); +g_free (dummy); +if (err) { +error_report_err (err); +return -1 ; + } +return 0 ; +} +int main (int argc,char **argv) {int readonly =0 ;
@@ -384,6 +454,7 @@ int main(int argc, char **argv) { "discard", 1, NULL, 'd' }, { "cache", 1, NULL, 't' }, { "trace", 1, NULL, 'T' }, + { "object", 1, NULL, OPTION_OBJECT }, { NULL, 0, NULL, 0 } };int c;
@@ -391,6 +462,7 @@ int main(int argc, char **argv)int flags =BDRV_O_UNMAP ;Error *local_error =NULL ;QDict *opts =NULL ; +QemuOpts *qopts =NULL ;#ifdef CONFIG_POSIX signal (SIGPIPE ,SIG_IGN );
@@ -399,6 +471,8 @@ int main(int argc, char **argv) progname =basename (argv[0 ]);qemu_init_exec_dir (argv[0 ]); +module_call_init (MODULE_INIT_QOM ); +qemu_add_opts (&qemu_object_opts);bdrv_init ();while ((c =getopt_long (argc, argv, sopt, lopt, &opt_index)) !=-1 ) {
@@ -450,6 +524,13 @@ int main(int argc, char **argv)case 'h' :usage (progname);exit (0 ); +case OPTION_OBJECT : + qopts =qemu_opts_parse_noisily (qemu_find_opts ("object" ), + optarg, true); +if (!qopts) { +exit (1 ); + } +break ;default :usage (progname);exit (1 );
@@ -466,6 +547,12 @@ int main(int argc, char **argv)exit (1 ); } +if (qemu_opts_foreach (qemu_find_opts ("object" ), + object_create, +NULL ,NULL )) { +exit (1 ); + } +/* initialize commands */ qemuio_add_command (&quit_cmd);qemuio_add_command (&open_cmd);
Signature · 1 line
2.4.3
PATCH 12/17 qemu-io: allow specifying image as a set of options args
In reply to Daniel P. BerrangeCurrently qemu-io allows an image filename to be passed on the command line, but does not have a way to set any options except the format eg
qemu-io https://127.0.0.1/images/centos7.iso
qemu-io /home/berrange/demo.qcow2
This adds a --source arg (that is mutually exclusive with a positional filename arg and -f arg) that accepts a full option string, as well as the original syntax eg
qemu-io --source driver=http,url=https://127.0.0.1/images,sslverify=off qemu-io --source https://127.0.0.1/images/centos7.iso
qemu-io --source file=/home/berrange/demo.qcow2
qemu-io --source /home/berrange/demo.qcow2
--- qemu-io.c | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-)
qemu-io.c +36 -1
@@ -373,6 +373,7 @@ static void reenable_tty_echo(void)enum {OPTION_OBJECT =258 , +OPTION_SOURCE =259 , };static QemuOptsList qemu_object_opts = {
@@ -436,6 +437,16 @@ out:return 0 ; } +static QemuOptsList file_opts = { + .name ="file" , + .implied_opt_name ="file" , + .head =QTAILQ_HEAD_INITIALIZER (file_opts.head), + .desc = { +/* no elements => accept any params */ + {/* end of list */ } + }, +}; +int main (int argc,char **argv) {int readonly =0 ;
@@ -455,6 +466,7 @@ int main(int argc, char **argv) { "cache", 1, NULL, 't' }, { "trace", 1, NULL, 'T' }, { "object", 1, NULL, OPTION_OBJECT }, + { "source", 1, NULL, OPTION_SOURCE }, { NULL, 0, NULL, 0 } };int c;
@@ -531,6 +543,12 @@ int main(int argc, char **argv)exit (1 ); }break ; +case OPTION_SOURCE : +if (!qemu_opts_parse_noisily (&file_opts, optarg, false)) { +qemu_opts_reset (&file_opts); +return 0 ; + } +break ;default :usage (progname);exit (1 );
@@ -572,7 +590,24 @@ int main(int argc, char **argv) flags |=BDRV_O_RDWR ; } -if ((argc - optind) ==1 ) { + qopts =qemu_opts_find (&file_opts,NULL ); +if (qopts) { +char *file; +if (opts) { +error_report ("--source and -f are mutually exclusive" ); +exit (1 ); + } +if ((argc - optind) ==1 ) { +error_report ("--source and filename are mutually exclusive" ); +exit (1 ); + } + file =g_strdup (qemu_opt_get (qopts,"file" )); +qemu_opt_unset (qopts,"file" ); + opts =qemu_opts_to_qdict (qopts,NULL ); +qemu_opts_reset (&file_opts); +openfile (file, flags, opts); +g_free (file); + }else if ((argc - optind) ==1 ) {openfile (argv[optind], flags, opts); }command_loop ();
Signature · 1 line
2.4.3
PATCH 13/17 qemu-nbd: allow specifying image as a set of options args
In reply to Daniel P. BerrangeCurrently qemu-nbd allows an image filename to be passed on the command line, but does not have a way to set any options except the format eg
qemu-nbd https://127.0.0.1/images/centos7.iso
qemu-nbd /home/berrange/demo.qcow2
This adds a --source arg (that is mutually exclusive with a positional filename arg and -f arg) that accepts a full option string, as well as the original syntax eg
qemu-nbd --source driver=http,url=https://127.0.0.1/images,sslverify=off qemu-nbd --source https://127.0.0.1/images/centos7.iso qemu-nbd --source file=/home/berrange/demo.qcow2 qemu-nbd --source /home/berrange/demo.qcow2
--- qemu-nbd.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 7 deletions(-)
qemu-nbd.c +50 -7
@@ -49,6 +49,7 @@#define QEMU_NBD_OPT_DISCARD 3#define QEMU_NBD_OPT_DETECT_ZEROES 4#define QEMU_NBD_OPT_OBJECT 5 +#define QEMU_NBD_OPT_SOURCE 6static NBDExport *exp;static int verbose;
@@ -387,6 +388,16 @@ static SocketAddress *nbd_build_socket_address(const char *sockpath, } +static QemuOptsList file_opts = { + .name ="file" , + .implied_opt_name ="file" , + .head =QTAILQ_HEAD_INITIALIZER (file_opts.head), + .desc = { +/* no elements => accept any params */ + {/* end of list */ } + }, +}; +static QemuOptsList qemu_object_opts = { .name ="object" , .implied_opt_name ="qom-type" ,
@@ -486,6 +497,7 @@ int main(int argc, char **argv) { "persistent", 0, NULL, 't' }, { "verbose", 0, NULL, 'v' }, { "object", 1, NULL, QEMU_NBD_OPT_OBJECT }, + { "source", 1, NULL, QEMU_NBD_OPT_SOURCE }, { NULL, 0, NULL, 0 } };int ch;
@@ -657,13 +669,23 @@ int main(int argc, char **argv)exit (1 ); }break ; +case QEMU_NBD_OPT_SOURCE : +if (srcpath) { +errx (EXIT_FAILURE ,"--source can only be used once" ); + } +if (!qemu_opts_parse_noisily (&file_opts, optarg, true)) { +qemu_opts_reset (&file_opts); +exit (EXIT_FAILURE ); + } + srcpath = optarg; +break ;case '?' :errx (EXIT_FAILURE ,"Try `%s --help' for more information." , argv[0 ]); } } -if ((argc - optind) !=1 ) { +if ((argc - optind) >1 ) {errx (EXIT_FAILURE ,"Invalid number of argument.\n" "Try `%s --help' for more information." , argv[0 ]);
@@ -757,15 +779,36 @@ int main(int argc, char **argv)bdrv_init ();atexit (bdrv_close_all); -if (fmt) { - options =qdict_new (); -qdict_put (options,"driver" ,qstring_from_str (fmt)); +if (srcpath) { +char *file =NULL ; + opts =qemu_opts_find (&file_opts,NULL ); +if (fmt) { +errx (EXIT_FAILURE ,"--source and -f are mutually exclusive" ); + } +if ((argc - optind) >1 ) { +errx (EXIT_FAILURE ,"--source and filename are mutually exclusive" ); + } + file =g_strdup (qemu_opt_get (opts,"file" )); +qemu_opt_unset (opts,"file" ); + options =qemu_opts_to_qdict (opts,NULL ); +qemu_opts_reset (&file_opts); + blk =blk_new_open ("hda" , file,NULL , options, flags, &local_err); +g_free (file); + }else { +if (fmt) { + options =qdict_new (); +qdict_put (options,"driver" ,qstring_from_str (fmt)); + } +if ((argc - optind) !=1 ) { +errx (EXIT_FAILURE ,"one of --source or filename are expected" ); + } + + srcpath = argv[optind]; + blk =blk_new_open ("hda" , srcpath,NULL , options, flags, &local_err); } - srcpath = argv[optind]; - blk =blk_new_open ("hda" , srcpath,NULL , options, flags, &local_err);if (!blk) { -errx (EXIT_FAILURE ,"Failed to blk_new_open '%s': %s" , argv[optind], +errx (EXIT_FAILURE ,"Failed to blk_new_open '%s': %s" , srcpath,error_get_pretty (local_err)); } bs =blk_bs (blk);
Signature · 1 line
2.4.3
PATCH 14/17 qemu-img: allow specifying image as a set of options args
In reply to Daniel P. BerrangeCurrently qemu-img allows an image filename to be passed on the command line, but does not have a way to set any options except the format eg
qemu-img info https://127.0.0.1/images/centos7.iso
This adds a --source arg (that is mutually exclusive with a positional filename arg and -f arg) that accepts a full option string, as well as the original syntax eg
qemu-img info driver=http,url=https://127.0.0.1/images,sslverify=off
--- include/qemu/option.h | 1 + qemu-img.c | 474 ++++++++++++++++++++++++++++++++++++++++++-------- util/qemu-option.c | 6 + 3 files changed, 407 insertions(+), 74 deletions(-)
include/qemu/option.h +1 -0
@@ -104,6 +104,7 @@ int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque, Error **errp);QemuOpts *qemu_opts_find (QemuOptsList *list,const char *id); +QemuOpts *qemu_opts_next (QemuOpts *opts);QemuOpts *qemu_opts_create (QemuOptsList *list,const char *id,int fail_if_exists,Error **errp);void qemu_opts_reset (QemuOptsList *list);
qemu-img.c +400 -74
841 lines not on this page · raw message
util/qemu-option.c +6 -0
@@ -637,6 +637,12 @@ QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)return NULL ; } + +QemuOpts *qemu_opts_next (QemuOpts *opts) +{ +return QTAILQ_NEXT (opts, next); +} +QemuOpts *qemu_opts_create (QemuOptsList *list,const char *id,int fail_if_exists,Error **errp) {
Signature · 1 line
2.4.3
PATCH 15/17 block: rip out all traces of password prompting
In reply to Daniel P. BerrangeNow that qcow & qcow2 are wired up to get encryption keys via the QCryptoSecret object, all traces of code which had to deal with prompting for passwords can be ripped out.
When the image is initially opened, the encryption key must be available immediately, or an error will be reported.
$ qemu-system-x86_64 -drive file=secret.qcow2 qemu-system-x86_64: -drive file=secret.qcow2: Image is encrypted but no secret is provided
Users must provide the secret with -object
$ echo 123456 > mypasswd.txt qemu-system-x86_64 -drive file=secret.qcow2,keysecret=sec0 -object secret,file=mypasswd.txt,id=sec0
The BDRV_O_NO_IO flag allows this error to be skipped, for use when 'qemu-img info' wants to open the file to query the headers, but not perform any actual I/O operations.
--- block/qcow.c | 7 +++++ block/qcow2.c | 13 +++++++-- hmp.c | 31 --------------------- hw/usb/dev-storage.c | 32 --------------------- include/block/block.h | 1 + include/monitor/monitor.h | 7 ----- include/qemu/osdep.h | 2 -- monitor.c | 65 ------------------------------------------- qemu-img.c | 46 ++++++++++++------------------ qemu-io.c | 21 -------------- qmp.c | 8 ------ tests/qemu-iotests/087 | 20 ++++++++++++++ tests/qemu-iotests/087.out | 2 ++ tests/qemu-iotests/134 | 17 ++++++++---- tests/qemu-iotests/134.out | 8 ------ tests/qemu-iotests/common.rc | 4 +-- util/oslib-posix.c | 66 -------------------------------------------- util/oslib-win32.c | 24 ---------------- 18 files changed, 72 insertions(+), 302 deletions(-)
block/qcow.c +7 -0
@@ -231,6 +231,13 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags,if (s->crypt_method_header) { bs->encrypted =1 ; } +if (!(flags &BDRV_O_NO_IO ) && + bs->encrypted && !s->cipher) { +error_setg (errp,"Image is encrypted but no secret is provided" ); + ret = -EINVAL ; + goto fail; + } + s->cluster_bits = header.cluster_bits; s->cluster_size =1 << s->cluster_bits; s->cluster_sectors =1 << (s->cluster_bits -9 );
block/qcow2.c +11 -2
@@ -1136,6 +1136,13 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags, goto fail; } +if (!(flags &BDRV_O_NO_IO ) && + bs->encrypted && !s->cipher) { +error_setg (errp,"Image is encrypted but no secret is provided" ); + ret = -EINVAL ; + goto fail; + } + s->cluster_cache =g_malloc (s->cluster_size);/* one more sector for decompressed data alignment */ s->cluster_data = qemu_try_blockalign(bs->file->bs, QCOW_MAX_CRYPT_CLUSTERS
@@ -2207,7 +2214,8 @@ static int qcow2_create2(const char *filename, int64_t total_size, options =qdict_new ();qdict_put (options,"driver" ,qstring_from_str ("qcow2" )); ret =bdrv_open (&bs, filename,NULL , options, -BDRV_O_RDWR |BDRV_O_CACHE_WB |BDRV_O_NO_FLUSH , +BDRV_O_RDWR |BDRV_O_CACHE_WB |BDRV_O_NO_FLUSH | +BDRV_O_NO_IO , &local_err);if (ret <0 ) {error_propagate (errp, local_err);
@@ -2261,7 +2269,8 @@ static int qcow2_create2(const char *filename, int64_t total_size, options =qdict_new ();qdict_put (options,"driver" ,qstring_from_str ("qcow2" )); ret =bdrv_open (&bs, filename,NULL , options, -BDRV_O_RDWR |BDRV_O_CACHE_WB |BDRV_O_NO_BACKING , +BDRV_O_RDWR |BDRV_O_CACHE_WB |BDRV_O_NO_BACKING | +BDRV_O_NO_IO , &local_err);if (local_err) {error_propagate (errp, local_err);
hmp.c +0 -31
@@ -959,37 +959,12 @@ void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)g_free (data); } -static void hmp_cont_cb (void *opaque,int err) -{ -if (!err) { -qmp_cont (NULL ); - } -} - -static bool key_is_missing (const BlockInfo *bdev) -{ -return (bdev->inserted && bdev->inserted->encryption_key_missing); -} -void hmp_cont (Monitor *mon,const QDict *qdict) { -BlockInfoList *bdev_list, *bdev;Error *err =NULL ; - bdev_list =qmp_query_block (NULL ); -for (bdev = bdev_list; bdev; bdev = bdev->next) { -if (key_is_missing (bdev->value)) { -monitor_read_block_device_key (mon, bdev->value->device, - hmp_cont_cb,NULL ); - goto out; - } - } -qmp_cont (&err);hmp_handle_error (mon, &err); - -out: -qapi_free_BlockInfoList (bdev_list); }void hmp_system_wakeup (Monitor *mon,const QDict *qdict)
@@ -1349,12 +1324,6 @@ void hmp_change(Monitor *mon, const QDict *qdict) }qmp_change (device, target, !!arg, arg, &err); -if (err && -error_get_class (err) ==ERROR_CLASS_DEVICE_ENCRYPTED ) { -error_free (err); -monitor_read_block_device_key (mon, device,NULL ,NULL ); -return ; - }hmp_handle_error (mon, &err); }
hw/usb/dev-storage.c +0 -32
@@ -553,21 +553,6 @@ static void usb_msd_handle_data(USBDevice *dev, USBPacket *p) } } -static void usb_msd_password_cb (void *opaque,int err) -{ -MSDState *s = opaque; -Error *local_err =NULL ; - -if (!err) { -usb_device_attach (&s->dev, &local_err); - } - -if (local_err) { -error_report_err (local_err); -qdev_unplug (&s->dev.qdev,NULL ); - } -} -static void *usb_msd_load_request (QEMUFile *f,SCSIRequest *req) {MSDState *s =DO_UPCAST (MSDState, dev.qdev, req->bus->qbus.parent);
@@ -613,23 +598,6 @@ static void usb_msd_realize_storage(USBDevice *dev, Error **errp)return ; } -bdrv_add_key (blk_bs (blk),NULL , &err); -if (err) { -if (monitor_cur_is_qmp ()) { -error_propagate (errp, err); -return ; - } -error_free (err); - err =NULL ; -if (cur_mon) { -monitor_read_bdrv_key_start (cur_mon,blk_bs (blk), - usb_msd_password_cb, s); - s->dev.auto_attach =0 ; - }else { - autostart =0 ; - } - } -blkconf_serial (&s->conf, &dev->serial);blkconf_blocksizes (&s->conf);
include/block/block.h +1 -0
@@ -90,6 +90,7 @@ typedef struct HDGeometry {#define BDRV_O_PROTOCOL 0x8000/* if no block driver is explicitly given: select an appropriate protocol driver, ignoring the format layer */ +#define BDRV_O_NO_IO 0x10000/* don't initialize for I/O */ #define BDRV_O_CACHE_MASK (BDRV_O_NOCACHE | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH)
include/monitor/monitor.h +0 -7
@@ -21,13 +21,6 @@ void monitor_init(CharDriverState *chr, int flags);int monitor_suspend (Monitor *mon);void monitor_resume (Monitor *mon); -int monitor_read_bdrv_key_start (Monitor *mon,BlockDriverState *bs, -BlockCompletionFunc *completion_cb, -void *opaque); -int monitor_read_block_device_key (Monitor *mon,const char *device, -BlockCompletionFunc *completion_cb, -void *opaque); -int monitor_get_fd (Monitor *mon,const char *fdname,Error **errp);int monitor_fd_param (Monitor *mon,const char *fdname,Error **errp);
include/qemu/osdep.h +0 -2
@@ -284,6 +284,4 @@ void qemu_set_tty_echo(int fd, bool echo);void os_mem_prealloc (int fd,char *area,size_t sz); -int qemu_read_password (char *buf,int buf_size); -#endif
monitor.c +0 -65
71 lines not on this page · raw message
qemu-img.c +17 -29
134 lines not on this page · raw message
qemu-io.c +0 -21
@@ -56,7 +56,6 @@ static const cmdinfo_t close_cmd = {static int openfile (char *name,int flags,QDict *opts) {Error *local_err =NULL ; -BlockDriverState *bs;if (qemuio_blk) {fprintf (stderr,"file open already, try 'help close'\n" );
@@ -73,27 +72,7 @@ static int openfile(char *name, int flags, QDict *opts)return 1 ; } - bs =blk_bs (qemuio_blk); -if (bdrv_is_encrypted (bs)) { -char password[256 ]; -printf ("Disk image '%s' is encrypted.\n" , name); -if (qemu_read_password (password,sizeof (password)) <0 ) { -error_report ("No password given" ); - goto error; - } -if (bdrv_set_key (bs, password) <0 ) { -error_report ("invalid password" ); - goto error; - } - } - -return 0 ; - - error: -blk_unref (qemuio_blk); - qemuio_blk =NULL ; -return 1 ; }static void open_help (void )
qmp.c +0 -8
@@ -169,7 +169,6 @@ SpiceInfo *qmp_query_spice(Error **errp)void qmp_cont (Error **errp) { -Error *local_err =NULL ;BlockDriverState *bs;if (runstate_needs_reset ()) {
@@ -182,13 +181,6 @@ void qmp_cont(Error **errp)for (bs =bdrv_next (NULL ); bs; bs =bdrv_next (bs)) {bdrv_iostatus_reset (bs); } -for (bs =bdrv_next (NULL ); bs; bs =bdrv_next (bs)) { -bdrv_add_key (bs,NULL , &local_err); -if (local_err) { -error_propagate (errp, local_err); -return ; - } - }if (runstate_check (RUN_STATE_INMIGRATE )) { autostart =1 ;
tests/qemu-iotests/087 +20 -0
@@ -187,11 +187,21 @@ echo _make_test_img -o encryption=on $size run_qemu -S <<EOF { "execute": "qmp_capabilities" } +{ "execute": "object-add", + "arguments": { + "qom-type": "secret", + "id": "sec0", + "props": { + "data": "123456" + } + } +} { "execute": "blockdev-add", "arguments": { "options": { "driver": "$IMGFMT", "id": "disk", + "keyid": "sec0", "file": { "driver": "file", "filename": "$TEST_IMG"
@@ -204,11 +214,21 @@ EOF run_qemu <<EOF { "execute": "qmp_capabilities" } +{ "execute": "object-add", + "arguments": { + "qom-type": "secret", + "id": "sec0", + "props": { + "data": "123456" + } + } +} { "execute": "blockdev-add", "arguments": { "options": { "driver": "$IMGFMT", "id": "disk", + "keyid": "sec0", "file": { "driver": "file", "filename": "$TEST_IMG"
tests/qemu-iotests/087.out +2 -0
@@ -48,6 +48,7 @@ Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 encryption=on Testing: -S QMP_VERSION {"return": {}} +{"return": {}} Encrypted images are deprecated Support for them will be removed in a future release. You can use 'qemu-img convert' to convert your image to an unencrypted one.
@@ -58,6 +59,7 @@ You can use 'qemu-img convert' to convert your image to an unencrypted one. Testing: QMP_VERSION {"return": {}} +{"return": {}} Encrypted images are deprecated Support for them will be removed in a future release. You can use 'qemu-img convert' to convert your image to an unencrypted one.
tests/qemu-iotests/134 +12 -5
@@ -44,23 +44,30 @@ _supported_os Linux size=128M -IMGOPTS="encryption=on" _make_test_img $size +SECRET1=secret,id=sec0,data=astrochicken +SECRET2=secret,id=sec0,data=platypus + +EXTRA_IMG_ARGS="--object $SECRET1" IMGOPTS="encryption=on" _make_test_img $size + + +QEMU_IO_OPTIONS=`echo $QEMU_IO_OPTIONS | sed -e 's/-f qcow2//'` +TEST_IMG="driver=qcow2,file=$TEST_IMG,keyid=sec0" echo echo "== reading whole image ==" -echo "astrochicken" | $QEMU_IO -c "read 0 $size" "$TEST_IMG" | _filter_qemu_io | _filter_testdir +$QEMU_IO --object $SECRET1 -c "read 0 $size" --source "$TEST_IMG" | _filter_qemu_io | _filter_testdir echo echo "== rewriting whole image ==" -echo "astrochicken" | $QEMU_IO -c "write -P 0xa 0 $size" "$TEST_IMG" | _filter_qemu_io | _filter_testdir +$QEMU_IO --object $SECRET1 -c "write -P 0xa 0 $size" --source "$TEST_IMG" | _filter_qemu_io | _filter_testdir echo echo "== verify pattern ==" -echo "astrochicken" | $QEMU_IO -c "read -P 0xa 0 $size" "$TEST_IMG" | _filter_qemu_io | _filter_testdir +$QEMU_IO --object $SECRET1 -c "read -P 0xa 0 $size" --source "$TEST_IMG" | _filter_qemu_io | _filter_testdir echo echo "== verify pattern failure with wrong password ==" -echo "platypus" | $QEMU_IO -c "read -P 0xa 0 $size" "$TEST_IMG" | _filter_qemu_io | _filter_testdir +$QEMU_IO --object $SECRET2 -c "read -P 0xa 0 $size" --source "$TEST_IMG" | _filter_qemu_io | _filter_testdir # success, all done
tests/qemu-iotests/134.out +0 -8
@@ -11,8 +11,6 @@ Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 encryption=on Encrypted images are deprecated Support for them will be removed in a future release. You can use 'qemu-img convert' to convert your image to an unencrypted one. -Disk image 'TEST_DIR/t.qcow2' is encrypted. -password: read 134217728/134217728 bytes at offset 0 128 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
@@ -20,8 +18,6 @@ read 134217728/134217728 bytes at offset 0 Encrypted images are deprecated Support for them will be removed in a future release. You can use 'qemu-img convert' to convert your image to an unencrypted one. -Disk image 'TEST_DIR/t.qcow2' is encrypted. -password: wrote 134217728/134217728 bytes at offset 0 128 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
@@ -29,8 +25,6 @@ wrote 134217728/134217728 bytes at offset 0 Encrypted images are deprecated Support for them will be removed in a future release. You can use 'qemu-img convert' to convert your image to an unencrypted one. -Disk image 'TEST_DIR/t.qcow2' is encrypted. -password: read 134217728/134217728 bytes at offset 0 128 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
@@ -38,8 +32,6 @@ read 134217728/134217728 bytes at offset 0 Encrypted images are deprecated Support for them will be removed in a future release. You can use 'qemu-img convert' to convert your image to an unencrypted one. -Disk image 'TEST_DIR/t.qcow2' is encrypted. -password: Pattern verification failed at offset 0, 134217728 bytes read 134217728/134217728 bytes at offset 0 128 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
tests/qemu-iotests/common.rc +2 -2
@@ -145,9 +145,9 @@ _make_test_img() # XXX(hch): have global image options? ( if [ $use_backing = 1 ]; then - $QEMU_IMG create -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" $image_size 2>&1 + $QEMU_IMG create $EXTRA_IMG_ARGS -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" $image_size 2>&1 else - $QEMU_IMG create -f $IMGFMT $extra_img_options "$img_name" $image_size 2>&1 + $QEMU_IMG create $EXTRA_IMG_ARGS -f $IMGFMT $extra_img_options "$img_name" $image_size 2>&1 fi ) | _filter_img_create
util/oslib-posix.c +0 -66
69 lines not on this page · raw message
util/oslib-win32.c +0 -24
@@ -472,27 +472,3 @@ void os_mem_prealloc(int fd, char *area, size_t memory)memset (area + pagesize * i,0 ,1 ); } } - - -/* XXX: put correct support for win32 */ -int qemu_read_password (char *buf,int buf_size) -{ -int c, i; - -printf ("Password: " ); -fflush (stdout); - i =0 ; -for (;;) { - c =getchar (); -if (c <0 ) { - buf[i] ='\0' ; -return -1 ; - }else if (c =='\n' ) { -break ; - }else if (i < (buf_size -1 )) { - buf[i++] = c; - } - } - buf[i] ='\0' ; -return 0 ; -}
Signature · 1 line
2.4.3
PATCH 16/17 block: remove all encryption handling APIs
In reply to Daniel P. BerrangeNow that all encryption keys must be provided upfront via the QCryptoSecret API and associated block driver properties there is no need for any explicit encryption handling APIs in the block layer. Encryption can be handled transparently within the block driver. We only retain an API for querying whether an image is encrypted or not, since that is a potentially useful piece of metadata to report to the user.
--- block.c | 81 ++-------------------------------------------- block/qapi.c | 2 +- block/qcow.c | 19 ----------- block/qcow2.c | 19 ----------- blockdev.c | 69 +++++++++------------------------------ include/block/block.h | 4 --- tests/qemu-iotests/087.out | 4 +-- 7 files changed, 21 insertions(+), 177 deletions(-)
block.c +2 -79
@@ -1542,17 +1542,8 @@ static int bdrv_open_inherit(BlockDriverState **pbs, const char *filename, goto close_and_fail; } -if (!bdrv_key_required (bs)) { -if (bs->blk) { -blk_dev_change_media_cb (bs->blk, true); - } - }else if (!runstate_check (RUN_STATE_PRELAUNCH ) - && !runstate_check (RUN_STATE_INMIGRATE ) - && !runstate_check (RUN_STATE_PAUSED )) {/* HACK */ -error_setg (errp, -"Guest must be stopped for opening of encrypted image" ); - ret = -EBUSY ; - goto close_and_fail; +if (bs->blk) { +blk_dev_change_media_cb (bs->blk, true); }QDECREF (options);
@@ -2608,74 +2599,6 @@ int bdrv_is_encrypted(BlockDriverState *bs)return bs->encrypted; } -int bdrv_key_required (BlockDriverState *bs) -{ -BdrvChild *backing = bs->backing; - -if (backing && backing->bs->encrypted && !backing->bs->valid_key) { -return 1 ; - } -return (bs->encrypted && !bs->valid_key); -} - -int bdrv_set_key (BlockDriverState *bs,const char *key) -{ -int ret; -if (bs->backing && bs->backing->bs->encrypted) { - ret =bdrv_set_key (bs->backing->bs, key); -if (ret <0 ) -return ret; -if (!bs->encrypted) -return 0 ; - } -if (!bs->encrypted) { -return -EINVAL ; - }else if (!bs->drv || !bs->drv->bdrv_set_key) { -return -ENOMEDIUM ; - } - ret = bs->drv->bdrv_set_key (bs, key); -if (ret <0 ) { - bs->valid_key =0 ; - }else if (!bs->valid_key) { - bs->valid_key =1 ; -if (bs->blk) { -/* call the change callback now, we skipped it on open */ -blk_dev_change_media_cb (bs->blk, true); - } - } -return ret; -} - -/* -* Provide an encryption key for @bs. -* If @key is non-null: -* If @bs is not encrypted, fail. -* Else if the key is invalid, fail. -* Else set @bs's key to @key, replacing the existing key, if any. -* If @key is null: -* If @bs is encrypted and still lacks a key, fail. -* Else do nothing. -* On failure, store an error object through @errp if non-null. -*/ -void bdrv_add_key (BlockDriverState *bs,const char *key,Error **errp) -{ -if (key) { -if (!bdrv_is_encrypted (bs)) { -error_setg (errp,"Node '%s' is not encrypted" , -bdrv_get_device_or_node_name (bs)); - }else if (bdrv_set_key (bs, key) <0 ) { -error_setg (errp,QERR_INVALID_PASSWORD ); - } - }else { -if (bdrv_key_required (bs)) { -error_set (errp,ERROR_CLASS_DEVICE_ENCRYPTED , -"'%s' (%s) is encrypted" , -bdrv_get_device_or_node_name (bs), -bdrv_get_encrypted_filename (bs)); - } - } -} -const char *bdrv_get_format_name (BlockDriverState *bs) {return bs->drv ? bs->drv->format_name :NULL ;
block/qapi.c +1 -1
@@ -42,7 +42,7 @@ BlockDeviceInfo *bdrv_block_device_info(BlockDriverState *bs, Error **errp) info->ro = bs->read_only; info->drv =g_strdup (bs->drv->format_name); info->encrypted = bs->encrypted; - info->encryption_key_missing =bdrv_key_required (bs); + info->encryption_key_missing = false; info->cache =g_new (BlockdevCacheInfo,1 ); *info->cache = (BlockdevCacheInfo ) {
block/qcow.c +0 -19
@@ -222,11 +222,6 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags, ret = -EINVAL ; goto fail; } -if (!qcrypto_cipher_supports (QCRYPTO_CIPHER_ALG_AES_128 )) { -error_setg (errp,"AES cipher not available" ); - ret = -EINVAL ; - goto fail; - } s->crypt_method_header = header.crypt_method;if (s->crypt_method_header) { bs->encrypted =1 ;
@@ -335,19 +330,6 @@ static int qcow_reopen_prepare(BDRVReopenState *state,return 0 ; } -static int qcow_set_key (BlockDriverState *bs,const char *key) -{ -BDRVQcowState *s = bs->opaque; - -assert (bs->encrypted); -qcrypto_cipher_free (s->cipher); - s->cipher =qcow_get_cipher_from_key (key,NULL ); -if (!s->cipher) { -return -1 ; - } -return 0 ; -} -/* The crypt function is compatible with the linux cryptoloop algorithm for < 4 GB images. NOTE: out_buf == in_buf is supported */
@@ -1078,7 +1060,6 @@ static BlockDriver bdrv_qcow = { .bdrv_co_writev = qcow_co_writev, .bdrv_co_get_block_status = qcow_co_get_block_status, - .bdrv_set_key = qcow_set_key, .bdrv_make_empty = qcow_make_empty, .bdrv_write_compressed = qcow_write_compressed, .bdrv_get_info = qcow_get_info,
block/qcow2.c +0 -19
@@ -1026,11 +1026,6 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags, ret = -EINVAL ; goto fail; } -if (!qcrypto_cipher_supports (QCRYPTO_CIPHER_ALG_AES_128 )) { -error_setg (errp,"AES cipher not available" ); - ret = -EINVAL ; - goto fail; - } s->crypt_method_header = header.crypt_method;if (s->crypt_method_header) { bs->encrypted =1 ;
@@ -1262,19 +1257,6 @@ static void qcow2_refresh_limits(BlockDriverState *bs, Error **errp) bs->bl.write_zeroes_alignment = s->cluster_sectors; } -static int qcow2_set_key (BlockDriverState *bs,const char *key) -{ -BDRVQcow2State *s = bs->opaque; - -assert (bs->encrypted); -qcrypto_cipher_free (s->cipher); - s->cipher =qcow2_get_cipher_from_key (key,NULL ); -if (!s->cipher) { -return -1 ; - } -return 0 ; -} -static int qcow2_reopen_prepare (BDRVReopenState *state,BlockReopenQueue *queue,Error **errp) {
@@ -3193,7 +3175,6 @@ BlockDriver bdrv_qcow2 = { .bdrv_create = qcow2_create, .bdrv_has_zero_init = bdrv_has_zero_init_1, .bdrv_co_get_block_status = qcow2_co_get_block_status, - .bdrv_set_key = qcow2_set_key, .bdrv_co_readv = qcow2_co_readv, .bdrv_co_writev = qcow2_co_writev,
blockdev.c +16 -53
112 lines not on this page · raw message
include/block/block.h +0 -4
@@ -414,10 +414,6 @@ bool bdrv_chain_contains(BlockDriverState *top, BlockDriverState *base);BlockDriverState *bdrv_next_node (BlockDriverState *bs);BlockDriverState *bdrv_next (BlockDriverState *bs);int bdrv_is_encrypted (BlockDriverState *bs); -int bdrv_key_required (BlockDriverState *bs); -int bdrv_set_key (BlockDriverState *bs,const char *key); -void bdrv_add_key (BlockDriverState *bs,const char *key,Error **errp); -int bdrv_query_missing_keys (void );void bdrv_iterate_format (void (*it)(void *opaque,const char *name),void *opaque);const char *bdrv_get_node_name (const BlockDriverState *bs);
tests/qemu-iotests/087.out +2 -2
@@ -52,7 +52,7 @@ QMP_VERSION Encrypted images are deprecated Support for them will be removed in a future release. You can use 'qemu-img convert' to convert your image to an unencrypted one. -{"error": {"class": "GenericError", "desc": "blockdev-add doesn't support encrypted devices"}} +{"return": {}} {"return": {}} {"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event": "SHUTDOWN"}
@@ -63,7 +63,7 @@ QMP_VERSION Encrypted images are deprecated Support for them will be removed in a future release. You can use 'qemu-img convert' to convert your image to an unencrypted one. -{"error": {"class": "GenericError", "desc": "Guest must be stopped for opening of encrypted image"}} +{"return": {}} {"return": {}} {"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event": "SHUTDOWN"}
Signature · 1 line
2.4.3
PATCH 17/17 block: remove support for writing to qcow/qcow2 encrypted images
In reply to Daniel P. BerrangeRefuse to open a qcow/qcow2 image with encryption if write access has been requested. To enable historic data to be liberated support for reading images is retained, as it does not pose an unreasonable support burden now that the new key handling infrastructure is inplace.
--- block.c | 7 ------- block/qcow.c | 6 ++++++ block/qcow2.c | 6 ++++++ tests/qemu-iotests/087.out | 24 ++++-------------------- tests/qemu-iotests/134 | 8 ++++---- tests/qemu-iotests/134.out | 36 +++++++++--------------------------- 6 files changed, 29 insertions(+), 58 deletions(-)
block.c +0 -7
@@ -923,13 +923,6 @@ static int bdrv_open_common(BlockDriverState *bs, BdrvChild *file, goto free_and_fail; } -if (bs->encrypted) { -error_report ("Encrypted images are deprecated" ); -error_printf ("Support for them will be removed in a future release.\n" -"You can use 'qemu-img convert' to convert your image" -" to an unencrypted one.\n" ); - } - ret =refresh_total_sectors (bs, bs->total_sectors);if (ret <0 ) {error_setg_errno (errp, -ret,"Could not refresh total sector count" );
block/qcow.c +6 -0
@@ -224,6 +224,12 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags, } s->crypt_method_header = header.crypt_method;if (s->crypt_method_header) { +if (flags &BDRV_O_RDWR ) { +error_setg (errp, +"Writing of encrypted qcow images is no longer supported" ); + ret = -ENOSYS ; + goto fail; + } bs->encrypted =1 ; } if (!(flags & BDRV_O_NO_IO) &&
block/qcow2.c +6 -0
@@ -1028,6 +1028,12 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags, } s->crypt_method_header = header.crypt_method;if (s->crypt_method_header) { +if (flags &BDRV_O_RDWR ) { +error_setg (errp, +"Writing of encrypted qcow2 images is no longer supported" ); + ret = -ENOSYS ; + goto fail; + } bs->encrypted =1 ; }
tests/qemu-iotests/087.out +4 -20
@@ -38,21 +38,13 @@ QMP_VERSION === Encrypted image === -qemu-img: Encrypted images are deprecated -Support for them will be removed in a future release. -You can use 'qemu-img convert' to convert your image to an unencrypted one. -qemu-img: Encrypted images are deprecated -Support for them will be removed in a future release. -You can use 'qemu-img convert' to convert your image to an unencrypted one. +qemu-img: TEST_DIR/t.IMGFMT: Writing of encrypted IMGFMT images is no longer supported Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 encryption=on Testing: -S QMP_VERSION {"return": {}} {"return": {}} -Encrypted images are deprecated -Support for them will be removed in a future release. -You can use 'qemu-img convert' to convert your image to an unencrypted one. -{"return": {}} +{"error": {"class": "GenericError", "desc": "Writing of encrypted qcow2 images is no longer supported"}} {"return": {}} {"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event": "SHUTDOWN"}
@@ -60,22 +52,14 @@ Testing: QMP_VERSION {"return": {}} {"return": {}} -Encrypted images are deprecated -Support for them will be removed in a future release. -You can use 'qemu-img convert' to convert your image to an unencrypted one. -{"return": {}} +{"error": {"class": "GenericError", "desc": "Writing of encrypted qcow2 images is no longer supported"}} {"return": {}} {"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event": "SHUTDOWN"} === Missing driver === -qemu-img: Encrypted images are deprecated -Support for them will be removed in a future release. -You can use 'qemu-img convert' to convert your image to an unencrypted one. -qemu-img: Encrypted images are deprecated -Support for them will be removed in a future release. -You can use 'qemu-img convert' to convert your image to an unencrypted one. +qemu-img: TEST_DIR/t.IMGFMT: Writing of encrypted IMGFMT images is no longer supported Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 encryption=on Testing: -S QMP_VERSION
tests/qemu-iotests/134 +4 -4
@@ -55,19 +55,19 @@ TEST_IMG="driver=qcow2,file=$TEST_IMG,keyid=sec0" echo echo "== reading whole image ==" -$QEMU_IO --object $SECRET1 -c "read 0 $size" --source "$TEST_IMG" | _filter_qemu_io | _filter_testdir +$QEMU_IO --object $SECRET1 -c "read 0 $size" --source "$TEST_IMG" 2>&1 | _filter_qemu_io | _filter_testdir echo echo "== rewriting whole image ==" -$QEMU_IO --object $SECRET1 -c "write -P 0xa 0 $size" --source "$TEST_IMG" | _filter_qemu_io | _filter_testdir +$QEMU_IO --object $SECRET1 -c "write -P 0xa 0 $size" --source "$TEST_IMG" 2>&1 | _filter_qemu_io | _filter_testdir echo echo "== verify pattern ==" -$QEMU_IO --object $SECRET1 -c "read -P 0xa 0 $size" --source "$TEST_IMG" | _filter_qemu_io | _filter_testdir +$QEMU_IO --object $SECRET1 -c "read -P 0xa 0 $size" --source "$TEST_IMG" 2>&1 | _filter_qemu_io | _filter_testdir echo echo "== verify pattern failure with wrong password ==" -$QEMU_IO --object $SECRET2 -c "read -P 0xa 0 $size" --source "$TEST_IMG" | _filter_qemu_io | _filter_testdir +$QEMU_IO --object $SECRET2 -c "read -P 0xa 0 $size" --source "$TEST_IMG" 2>&1 | _filter_qemu_io | _filter_testdir # success, all done
tests/qemu-iotests/134.out +9 -27
@@ -1,38 +1,20 @@ QA output created by 134 -qemu-img: Encrypted images are deprecated -Support for them will be removed in a future release. -You can use 'qemu-img convert' to convert your image to an unencrypted one. -qemu-img: Encrypted images are deprecated -Support for them will be removed in a future release. -You can use 'qemu-img convert' to convert your image to an unencrypted one. +qemu-img: TEST_DIR/t.IMGFMT: Writing of encrypted IMGFMT images is no longer supported Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 encryption=on == reading whole image == -Encrypted images are deprecated -Support for them will be removed in a future release. -You can use 'qemu-img convert' to convert your image to an unencrypted one. -read 134217728/134217728 bytes at offset 0 -128 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +qemu-io: can't open device TEST_DIR/t.qcow2: Writing of encrypted qcow2 images is no longer supported +no file open, try 'help open' == rewriting whole image == -Encrypted images are deprecated -Support for them will be removed in a future release. -You can use 'qemu-img convert' to convert your image to an unencrypted one. -wrote 134217728/134217728 bytes at offset 0 -128 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +qemu-io: can't open device TEST_DIR/t.qcow2: Writing of encrypted qcow2 images is no longer supported +no file open, try 'help open' == verify pattern == -Encrypted images are deprecated -Support for them will be removed in a future release. -You can use 'qemu-img convert' to convert your image to an unencrypted one. -read 134217728/134217728 bytes at offset 0 -128 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +qemu-io: can't open device TEST_DIR/t.qcow2: Writing of encrypted qcow2 images is no longer supported +no file open, try 'help open' == verify pattern failure with wrong password == -Encrypted images are deprecated -Support for them will be removed in a future release. -You can use 'qemu-img convert' to convert your image to an unencrypted one. -Pattern verification failed at offset 0, 134217728 bytes -read 134217728/134217728 bytes at offset 0 -128 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +qemu-io: can't open device TEST_DIR/t.qcow2: Writing of encrypted qcow2 images is no longer supported +no file open, try 'help open' *** done
Signature · 1 line
2.4.3
Quoted 3 lines · Daniel P. Berrange
There are a variety of places where QEMU needs to have access to passwords, encryption keys or similar kinds of secrets.
<snip>
Quoted 65 lines
Example usage for creating secrets...
Direct password, insecure, for ad-hoc developer testing only
$QEMU -object secret,id=sec0,data=letmein
Indirect password via a file, good for production
echo -n "letmein" > mypasswd.txt
$QEMU -object secret,id=sec0,file=mypasswd.txtThe file based approach supports file descriptor passing, so mgmt apps can use that to dynamically add passwords to running QEMU.
There is a better way though, which is to use an encrypted secret. The intent here is that a mgmt app (like libvirt) will generate a random AES-256 key for each virtual machine it starts (saved in eg
/var/run/libvirt/qemu/$GUEST.key) It can then use the direct password passing, but encrypt the data.$QEMU \ -object secret,id=secmaster0,file=/var/run/libvirt/qemu/$GUEST.key,format=base64 \ -object secret,id=sec0,data=[base64 ciphertext],\ keyid=secmaster0,iv=[base64 initialization vector]This means that the mgmt app does not need to worry about file descriptor passing at all. It can just use regular object properties, safe in the knowledge that the data is protected by a secret AES key shared only between QEMU and the mgmt app.
Use of encrypted secrets is not restricted to directly provided inline data. If the secret is stored in an external file, that can be encrypted too
$QEMU \ -object secret,id=secmaster0,file=/var/run/libvirt/qemu/$GUEST.key,format=base64 \ -object secret,id=sec0,file=/some/secret/file.aes,\ keyid=secmaster0,iv=[base64 initialization vector]Example usage for referencing secrets...
CURL:
$QEMU -object secret,id=sec0... \ -drive driver=http,url=http://example.com/someimg.qcow2,\ user=dan,passwordid=sec0 $QEMU -object secret,id=sec0... -object secret,id=sec1 \ -drive driver=http,url=http://example.com/someimg.qcow2,\ user=dan,passwordid=sec0,proxyuser=dan,passwordid=sec1iSCSI:
$QEMU -object secret,id=sec0... \ -drive driver=iscsi,url=iscsi://example.com/target-foo/lun1,\ user=dan,passwordid=sec0RBD:
Quoted 8 lines
$QEMU -object secret,id=sec0... \ -drive driver=rbd,file=rbd:pool/image:id=myname,\ auth-supported-cephx,authsecret=sec0QCow/Qcow2 encryption
$QEMU -object secret,id=sec0... \ -drive file=someimage.qcow2,keyid=sec0
So one use case which I don't see here but do on other programs that need to access secrets is calling an external program and reading its stdout. This is simpler than having to mess around with passing FDs although there may be security concerns having QEMU create new tasks on the system.
Signature · 1 line
Alex Bennée
Quoted 77 lines · Alex Bennée
Quoted 3 lines · Daniel P. Berrange
There are a variety of places where QEMU needs to have access to passwords, encryption keys or similar kinds of secrets.
<snip>
Quoted 71 lines
Example usage for creating secrets...
Direct password, insecure, for ad-hoc developer testing only
$QEMU -object secret,id=sec0,data=letmein
Indirect password via a file, good for production
echo -n "letmein" > mypasswd.txt
$QEMU -object secret,id=sec0,file=mypasswd.txtThe file based approach supports file descriptor passing, so mgmt apps can use that to dynamically add passwords to running QEMU.
There is a better way though, which is to use an encrypted secret. The intent here is that a mgmt app (like libvirt) will generate a random AES-256 key for each virtual machine it starts (saved in eg
/var/run/libvirt/qemu/$GUEST.key) It can then use the direct password passing, but encrypt the data.$QEMU \ -object secret,id=secmaster0,file=/var/run/libvirt/qemu/$GUEST.key,format=base64 \ -object secret,id=sec0,data=[base64 ciphertext],\ keyid=secmaster0,iv=[base64 initialization vector]This means that the mgmt app does not need to worry about file descriptor passing at all. It can just use regular object properties, safe in the knowledge that the data is protected by a secret AES key shared only between QEMU and the mgmt app.
Use of encrypted secrets is not restricted to directly provided inline data. If the secret is stored in an external file, that can be encrypted too
$QEMU \ -object secret,id=secmaster0,file=/var/run/libvirt/qemu/$GUEST.key,format=base64 \ -object secret,id=sec0,file=/some/secret/file.aes,\ keyid=secmaster0,iv=[base64 initialization vector]Example usage for referencing secrets...
CURL:
$QEMU -object secret,id=sec0... \ -drive driver=http,url=http://example.com/someimg.qcow2,\ user=dan,passwordid=sec0 $QEMU -object secret,id=sec0... -object secret,id=sec1 \ -drive driver=http,url=http://example.com/someimg.qcow2,\ user=dan,passwordid=sec0,proxyuser=dan,passwordid=sec1iSCSI:
$QEMU -object secret,id=sec0... \ -drive driver=iscsi,url=iscsi://example.com/target-foo/lun1,\ user=dan,passwordid=sec0RBD:
$QEMU -object secret,id=sec0... \ -drive driver=rbd,file=rbd:pool/image:id=myname,\ auth-supported-cephx,authsecret=sec0QCow/Qcow2 encryption
Quoted 8 lines
Quoted 2 lines
$QEMU -object secret,id=sec0... \ -drive file=someimage.qcow2,keyid=sec0So one use case which I don't see here but do on other programs that need to access secrets is calling an external program and reading its stdout. This is simpler than having to mess around with passing FDs although there may be security concerns having QEMU create new tasks on the system.
Spawning external programs is a rather heavyweight approach. I can see it being useful if you were locked into an existing API/syntax which you couldn't modify, but we don't have that restriction here. I'm also not a huge fan of having QEMU spawn a program that potentially has access to a large number of passwords that QEMU should not be able to access. I think passing in the required passwords explicitly as done here is a better approach.
As noted earlier, FD passing is supported, but I don't think it is going to be commonly needed or used. At least libvirt would not use it, as providing the passwords directly, with encryption, is a more straightforward approach to implement.
Regards,
Daniel
Signature · 4 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
* Daniel P. Berrange ([email protected]) wrote:
<snip>
Quoted 5 lines
It is obvious there there is a wide variety of functionality in QEMU that needs access to "secrets". This need will only grow over time. We need to stop having everyone invent their own dangerous wheels and provide a standard mechanism for securely passing secrets to QEMU.
Agreed.
Quoted 12 lines
To this end, this series introduces a QCryptoSecret object class with short name "secret". All the places which needs passwords/keys are then converted to get their via this API, except VNC/SPICE which are a future exercise.
Example usage for creating secrets...
Direct password, insecure, for ad-hoc developer testing only
$QEMU -object secret,id=sec0,data=letmein
Quoted 8 lines
Indirect password via a file, good for production
echo -n "letmein" > mypasswd.txt
$QEMU -object secret,id=sec0,file=mypasswd.txtThe file based approach supports file descriptor passing, so mgmt apps can use that to dynamically add passwords to running QEMU.
Would it make any sense to support the Linux kernel key system? That seems to have a way of passing keys between a limited set of processes and protecting them using SELinux and the like.
(I can also imagine that people might want to feed it with keys from a TPM or other hardware security device, but fortunately I can't remember enough about TPMs to remember how getting keys worked).
Dave
Quoted 169 lines
There is a better way though, which is to use an encrypted secret. The intent here is that a mgmt app (like libvirt) will generate a random AES-256 key for each virtual machine it starts (saved in eg
/var/run/libvirt/qemu/$GUEST.key) It can then use the direct password passing, but encrypt the data.$QEMU \ -object secret,id=secmaster0,file=/var/run/libvirt/qemu/$GUEST.key,format=base64 \ -object secret,id=sec0,data=[base64 ciphertext],\ keyid=secmaster0,iv=[base64 initialization vector]This means that the mgmt app does not need to worry about file descriptor passing at all. It can just use regular object properties, safe in the knowledge that the data is protected by a secret AES key shared only between QEMU and the mgmt app.
Use of encrypted secrets is not restricted to directly provided inline data. If the secret is stored in an external file, that can be encrypted too
$QEMU \ -object secret,id=secmaster0,file=/var/run/libvirt/qemu/$GUEST.key,format=base64 \ -object secret,id=sec0,file=/some/secret/file.aes,\ keyid=secmaster0,iv=[base64 initialization vector]Example usage for referencing secrets...
CURL:
$QEMU -object secret,id=sec0... \ -drive driver=http,url=http://example.com/someimg.qcow2,\ user=dan,passwordid=sec0 $QEMU -object secret,id=sec0... -object secret,id=sec1 \ -drive driver=http,url=http://example.com/someimg.qcow2,\ user=dan,passwordid=sec0,proxyuser=dan,passwordid=sec1iSCSI:
$QEMU -object secret,id=sec0... \ -drive driver=iscsi,url=iscsi://example.com/target-foo/lun1,\ user=dan,passwordid=sec0RBD:
$QEMU -object secret,id=sec0... \ -drive driver=rbd,file=rbd:pool/image:id=myname,\ auth-supported-cephx,authsecret=sec0QCow/Qcow2 encryption
$QEMU -object secret,id=sec0... \ -drive file=someimage.qcow2,keyid=sec0Finally, this extends qemu-img, qemu-nbd and qemu-io. All of them gain a new '--object' parameter which provides the same functionality as '-object' with QEMU system emulators. This lets us create QCryptoSecret object instances in those tools
The tools also then get support for a new '--source IMG-OPTS' parameter to allow a full set of image options to be specified, instead of just separate hardcoded args for format + filename which they currently permit. This is probably the area I am least sure of. I struggled to understand what the "best practice" is for turning a QemuOpts into something you can use to instantiate block backends. So I may well have not done the right thing.
Towards the end I rip out the current encryption key handling from the block layer so all the hairy code for dealing with encrypted block devices disappears, and encryption can be a 100% private matter for the block driver internal impl. This is obviously not backwards compatible, but we have been warning users we're dropping qcow2 encryption support for a while.
Finally I disable support for writing to encrypted qcow2 files, but keep the ability to read them, so users can liberate data. Originally we were intending to fully delete encryption support, due to the burden it places on the internal boock API. Since I removed that burden I figured it is reasonable to keep read-only support around.
The only real missing thing is wiring up the VNC/SPICE servers. There is one complication here in that it is common to change the VNC/SPICE password at runtime, and I'm not sure what the best way to deal with this is.
There are two obvious choices
a. Create a new secret, tell the VNC server to use
the new secret, delete the old secret. This will need a new 'graphics_secret_update' command in the monitor, to use alongside object_add/del.b. Allow the existing secret to be updated via some
new 'object_update' method, and internally notify the VNC/SPICE server when the secret is updated. This would probably need a new QOM interface UserUpdatableObject to be defined, as an refinement of UserCreatableObject.Daniel P. Berrange (17):
crypto: add QCryptoSecret object class for password/key handling
crypto: add support for loading encrypted x509 keys
rbd: add support for getting password from QCryptoSecret object
curl: add support for HTTP authentication parameters
iscsi: add support for getting CHAP password via QCryptoSecret API
qcow: add a 'keyid' parameter to qcow options
qcow2: add a 'keyid' parameter to qcow2 options
qom: adduser_creatable_add&user_creatable_delmethods
qemu-img: add support for --object command line arg
qemu-nbd: add support for --object command line arg
qemu-io: add support for --object command line arg
qemu-io: allow specifying image as a set of options args
qemu-nbd: allow specifying image as a set of options args
qemu-img: allow specifying image as a set of options args
block: rip out all traces of password prompting
block: remove all encryption handling APIs
block: remove support for writing to qcow/qcow2 encrypted imagesblock.c | 88 +---- block/curl.c | 66 ++++ block/iscsi.c | 24 +- block/qapi.c | 2 +- block/qcow.c | 122 +++++-- block/qcow2.c | 116 +++--- block/qcow2.h | 1 + block/rbd.c | 42 +++ blockdev.c | 69 +--- crypto/Makefile.objs | 1 + crypto/secret.c | 513 ++++++++++++++++++++++++++ crypto/tlscredsx509.c | 47 +++ hmp.c | 42 +-- hw/usb/dev-storage.c | 32 -- include/block/block.h | 5 +- include/crypto/secret.h | 139 +++++++ include/crypto/tlscredsx509.h | 1 + include/monitor/monitor.h | 10 - include/qemu/option.h | 1 + include/qemu/osdep.h | 2 - include/qom/object_interfaces.h | 31 ++ monitor.c | 65 ---- qapi/block-core.json | 23 +- qapi/crypto.json | 14 + qemu-img-cmds.hx | 44 +-- qemu-img.c | 788 +++++++++++++++++++++++++++++++++++----- qemu-img.texi | 8 + qemu-io.c | 145 ++++++-- qemu-nbd.c | 142 +++++++- qemu-nbd.texi | 7 + qemu-options.hx | 84 ++++- qmp.c | 83 +---- qom/object_interfaces.c | 76 ++++ tests/.gitignore | 1 + tests/Makefile | 2 + tests/qemu-iotests/087 | 20 + tests/qemu-iotests/087.out | 26 +- tests/qemu-iotests/134 | 17 +- tests/qemu-iotests/134.out | 44 +-- tests/qemu-iotests/common.rc | 4 +- tests/test-crypto-secret.c | 440 ++++++++++++++++++++++
Quoted 13 lines
util/oslib-posix.c | 66 ---- util/oslib-win32.c | 24 -- util/qemu-option.c | 6 + vl.c | 8 +- 45 files changed, 2740 insertions(+), 751 deletions(-) create mode 100644 crypto/secret.c create mode 100644 include/crypto/secret.h create mode 100644 tests/test-crypto-secret.cSignature · 1 line
2.4.3
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Quoted 33 lines · Dr. David Alan Gilbert
* Daniel P. Berrange ([email protected]) wrote:
<snip>
Quoted 5 lines
It is obvious there there is a wide variety of functionality in QEMU that needs access to "secrets". This need will only grow over time. We need to stop having everyone invent their own dangerous wheels and provide a standard mechanism for securely passing secrets to QEMU.
Agreed.
Quoted 12 lines
To this end, this series introduces a QCryptoSecret object class with short name "secret". All the places which needs passwords/keys are then converted to get their via this API, except VNC/SPICE which are a future exercise.
Example usage for creating secrets...
Direct password, insecure, for ad-hoc developer testing only
$QEMU -object secret,id=sec0,data=letmein
Quoted 8 lines
Indirect password via a file, good for production
echo -n "letmein" > mypasswd.txt
$QEMU -object secret,id=sec0,file=mypasswd.txtThe file based approach supports file descriptor passing, so mgmt apps can use that to dynamically add passwords to running QEMU.
Quoted 8 lines
Would it make any sense to support the Linux kernel key system? That seems to have a way of passing keys between a limited set of processes and protecting them using SELinux and the like.
(I can also imagine that people might want to feed it with keys from a TPM or other hardware security device, but fortunately I can't remember enough about TPMs to remember how getting keys worked).
I don't really know enough about the Linux key system to answer that very well.
It does of course have the obvious downside that it is Linux specific, so it would have to be an optional further extension to the basic portable framework I've implemented so. Given that I'll say this is an area someone else can explore in the future if they have desire todo so :-)
Regards,
Daniel
Signature · 4 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
Quoted 234 lines · Daniel P. Berrange
There are a variety of places where QEMU needs to have access to passwords, encryption keys or similar kinds of secrets.
- VNC / SPICE user passwords
- Curl block http / proxy passwords
- RBD auth password
- iSCSI CHAP password
- x509 private key password
- QCow/QCow2 encryption keyQEMU has a variety of ways of dealing with this problem, some good, some ugly, some bad.
- The RBD block driver accepts the password in plaintext via a private RBD config option. This is a pending CVE
https://security-tracker.debian.org/tracker/CVE-2015-5160- The iSCSI driver accepts the password in plaintext as a block driver option. This is the same as the RBD CVE essentially, just a QEMU option, rather than a librbd option
- The VNC / SPICE servers only accept the passwords via the QEMU monitor. This is secure, but it inconvenient for adhoc developer usage where security of CLI args does not matter.
- QCow/QCow2 encryption keys can be provided by the monitor but this is not available for qemu-img, qemu-io and qemu-nbd. QEMU falls back to doing interactive console prompting to get keys.
- x509 privte key passwords are not supported at all by QEMU which forces users to store their key in plaintext on their host FS.
- The CURL driver doesn't support HTTP auth at all currently.
It is obvious there there is a wide variety of functionality in QEMU that needs access to "secrets". This need will only grow over time. We need to stop having everyone invent their own dangerous wheels and provide a standard mechanism for securely passing secrets to QEMU.
To this end, this series introduces a QCryptoSecret object class with short name "secret". All the places which needs passwords/keys are then converted to get their via this API, except VNC/SPICE which are a future exercise.
Example usage for creating secrets...
Direct password, insecure, for ad-hoc developer testing only
$QEMU -object secret,id=sec0,data=letmein
Indirect password via a file, good for production
echo -n "letmein" > mypasswd.txt
$QEMU -object secret,id=sec0,file=mypasswd.txtThe file based approach supports file descriptor passing, so mgmt apps can use that to dynamically add passwords to running QEMU.
There is a better way though, which is to use an encrypted secret. The intent here is that a mgmt app (like libvirt) will generate a random AES-256 key for each virtual machine it starts (saved in eg
/var/run/libvirt/qemu/$GUEST.key) It can then use the direct password passing, but encrypt the data.$QEMU \ -object secret,id=secmaster0,file=/var/run/libvirt/qemu/$GUEST.key,format=base64 \ -object secret,id=sec0,data=[base64 ciphertext],\ keyid=secmaster0,iv=[base64 initialization vector]This means that the mgmt app does not need to worry about file descriptor passing at all. It can just use regular object properties, safe in the knowledge that the data is protected by a secret AES key shared only between QEMU and the mgmt app.
Use of encrypted secrets is not restricted to directly provided inline data. If the secret is stored in an external file, that can be encrypted too
$QEMU \ -object secret,id=secmaster0,file=/var/run/libvirt/qemu/$GUEST.key,format=base64 \ -object secret,id=sec0,file=/some/secret/file.aes,\ keyid=secmaster0,iv=[base64 initialization vector]Example usage for referencing secrets...
CURL:
$QEMU -object secret,id=sec0... \ -drive driver=http,url=http://example.com/someimg.qcow2,\ user=dan,passwordid=sec0 $QEMU -object secret,id=sec0... -object secret,id=sec1 \ -drive driver=http,url=http://example.com/someimg.qcow2,\ user=dan,passwordid=sec0,proxyuser=dan,passwordid=sec1iSCSI:
$QEMU -object secret,id=sec0... \ -drive driver=iscsi,url=iscsi://example.com/target-foo/lun1,\ user=dan,passwordid=sec0RBD:
$QEMU -object secret,id=sec0... \ -drive driver=rbd,file=rbd:pool/image:id=myname,\ auth-supported-cephx,authsecret=sec0QCow/Qcow2 encryption
$QEMU -object secret,id=sec0... \ -drive file=someimage.qcow2,keyid=sec0Finally, this extends qemu-img, qemu-nbd and qemu-io. All of them gain a new '--object' parameter which provides the same functionality as '-object' with QEMU system emulators. This lets us create QCryptoSecret object instances in those tools
The tools also then get support for a new '--source IMG-OPTS' parameter to allow a full set of image options to be specified, instead of just separate hardcoded args for format + filename which they currently permit. This is probably the area I am least sure of. I struggled to understand what the "best practice" is for turning a QemuOpts into something you can use to instantiate block backends. So I may well have not done the right thing.
Towards the end I rip out the current encryption key handling from the block layer so all the hairy code for dealing with encrypted block devices disappears, and encryption can be a 100% private matter for the block driver internal impl. This is obviously not backwards compatible, but we have been warning users we're dropping qcow2 encryption support for a while.
Finally I disable support for writing to encrypted qcow2 files, but keep the ability to read them, so users can liberate data. Originally we were intending to fully delete encryption support, due to the burden it places on the internal boock API. Since I removed that burden I figured it is reasonable to keep read-only support around.
The only real missing thing is wiring up the VNC/SPICE servers. There is one complication here in that it is common to change the VNC/SPICE password at runtime, and I'm not sure what the best way to deal with this is.
There are two obvious choices
a. Create a new secret, tell the VNC server to use
the new secret, delete the old secret. This will need a new 'graphics_secret_update' command in the monitor, to use alongside object_add/del.b. Allow the existing secret to be updated via some
new 'object_update' method, and internally notify the VNC/SPICE server when the secret is updated. This would probably need a new QOM interface UserUpdatableObject to be defined, as an refinement of UserCreatableObject.Daniel P. Berrange (17):
crypto: add QCryptoSecret object class for password/key handling
crypto: add support for loading encrypted x509 keys
rbd: add support for getting password from QCryptoSecret object
curl: add support for HTTP authentication parameters
iscsi: add support for getting CHAP password via QCryptoSecret API
qcow: add a 'keyid' parameter to qcow options
qcow2: add a 'keyid' parameter to qcow2 options
qom: adduser_creatable_add&user_creatable_delmethods
qemu-img: add support for --object command line arg
qemu-nbd: add support for --object command line arg
qemu-io: add support for --object command line arg
qemu-io: allow specifying image as a set of options args
qemu-nbd: allow specifying image as a set of options args
qemu-img: allow specifying image as a set of options args
block: rip out all traces of password prompting
block: remove all encryption handling APIs
block: remove support for writing to qcow/qcow2 encrypted imagesblock.c | 88 +---- block/curl.c | 66 ++++ block/iscsi.c | 24 +- block/qapi.c | 2 +- block/qcow.c | 122 +++++-- block/qcow2.c | 116 +++--- block/qcow2.h | 1 + block/rbd.c | 42 +++ blockdev.c | 69 +--- crypto/Makefile.objs | 1 + crypto/secret.c | 513 ++++++++++++++++++++++++++ crypto/tlscredsx509.c | 47 +++ hmp.c | 42 +-- hw/usb/dev-storage.c | 32 -- include/block/block.h | 5 +- include/crypto/secret.h | 139 +++++++ include/crypto/tlscredsx509.h | 1 + include/monitor/monitor.h | 10 - include/qemu/option.h | 1 + include/qemu/osdep.h | 2 - include/qom/object_interfaces.h | 31 ++ monitor.c | 65 ---- qapi/block-core.json | 23 +- qapi/crypto.json | 14 + qemu-img-cmds.hx | 44 +-- qemu-img.c | 788 +++++++++++++++++++++++++++++++++++----- qemu-img.texi | 8 + qemu-io.c | 145 ++++++-- qemu-nbd.c | 142 +++++++- qemu-nbd.texi | 7 + qemu-options.hx | 84 ++++- qmp.c | 83 +---- qom/object_interfaces.c | 76 ++++ tests/.gitignore | 1 + tests/Makefile | 2 + tests/qemu-iotests/087 | 20 + tests/qemu-iotests/087.out | 26 +- tests/qemu-iotests/134 | 17 +- tests/qemu-iotests/134.out | 44 +-- tests/qemu-iotests/common.rc | 4 +- tests/test-crypto-secret.c | 440 ++++++++++++++++++++++
Quoted 8 lines
util/oslib-posix.c | 66 ---- util/oslib-win32.c | 24 -- util/qemu-option.c | 6 + vl.c | 8 +- 45 files changed, 2740 insertions(+), 751 deletions(-) create mode 100644 crypto/secret.c create mode 100644 include/crypto/secret.h create mode 100644 tests/test-crypto-secret.c
i.e. makes sense to me but I haven't reviewed patches in detail