block: always fill entire LUKS header space with zeros
PATCH v2 1/1 block: always fill entire LUKS header space with zeros
When initializing the LUKS header the size with default encryption parameters will currently be 2068480 bytes. This is rounded up to a multiple of the cluster size, 2081792, with 64k sectors. If the end of the header is not the same as the end of the cluster we fill the extra space with zeros. This was forgetting that not even the space allocated for the header will be fully initialized, as we only write key material for the first key slot. The space left for the other 7 slots is never written to.
An optimization to the ref count checking code:
commit a5fff8d4b4d928311a5005efa12d0991fe3b66f9 (refs/bisect/bad)
Author: Vladimir Sementsov-Ogievskiy <[email protected]>
Date: Wed Feb 27 16:14:30 2019 +0300
qcow2-refcount: avoid eating RAM
made the assumption that every cluster which was allocated would have at least some data written to it. This was violated by way the LUKS header is only partially written, with much space simply reserved for future use.
Depending on the cluster size this problem was masked by the logic which wrote zeros between the end of the LUKS header and the end of the cluster.
$ qemu-img create --object secret,id=cluster_encrypt0,data=123456 \
-f qcow2 -o cluster_size=2k,encrypt.iter-time=1,\
encrypt.format=luks,encrypt.key-secret=cluster_encrypt0 \
cluster_size_check.qcow2 100M
Formatting 'cluster_size_check.qcow2', fmt=qcow2 size=104857600
encrypt.format=luks encrypt.key-secret=cluster_encrypt0
encrypt.iter-time=1 cluster_size=2048 lazy_refcounts=off refcount_bits=16$ qemu-img check --object secret,id=cluster_encrypt0,data=redhat \
'json:{"driver": "qcow2", "encrypt.format": "luks", \
"encrypt.key-secret": "cluster_encrypt0", \
"file.driver": "file", "file.filename": "cluster_size_check.qcow2"}'
ERROR: counting reference for region exceeding the end of the file by one cluster or more: offset 0x2000 size 0x1f9000
Leaked cluster 4 refcount=1 reference=0
...snip...
Leaked cluster 130 refcount=1 reference=01 errors were found on the image.
Data may be corrupted, or further writes to the image may corrupt it.
127 leaked clusters were found on the image. This means waste of disk space, but no harm to data. Image end offset: 268288
The problem only exists when the disk image is entirely empty. Writing data to the disk image payload will solve the problem by causing the end of the file to be extended further.
The change fixes it by ensuring that the entire allocated LUKS header region is fully initialized with zeros. The qemu-img check will still fail for any pre-existing disk images created prior to this change, unless at least 1 byte of the payload is written to.
Fully writing zeros to the entire LUKS header is a good idea regardless as it ensures that space has been allocated on the host filesystem (or whatever block storage backend is used).
---
In v2:
- Cut down test scenarios to speed up
- Use _check_test_img helper
- Fix outdated comments
- Use space not tabs
block/qcow2.c | 11 +++-- tests/qemu-iotests/284 | 97 ++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/284.out | 62 ++++++++++++++++++++++++ tests/qemu-iotests/group | 1 + 4 files changed, 167 insertions(+), 4 deletions(-) create mode 100755 tests/qemu-iotests/284 create mode 100644 tests/qemu-iotests/284.out
block/qcow2.c +7 -4
@@ -135,13 +135,16 @@ static ssize_t qcow2_crypto_hdr_init_func(QCryptoBlock *block, size_t headerlen, s->crypto_header.length = headerlen; s->crypto_header.offset = ret; -/* Zero fill remaining space in cluster so it has predictable -* content in case of future spec changes */ +/* +* Zero fill all space in cluster so it has predictable +* content, as we may not initialize some regions of the +* header (eg only 1 out of 8 key slots will be initialized) +*/ clusterlen =size_to_clusters (s, headerlen) * s->cluster_size;assert (qcow2_pre_write_overlap_check (bs,0 , ret, clusterlen, false) ==0 ); ret =bdrv_pwrite_zeroes (bs->file, - ret + headerlen, - clusterlen - headerlen,0 ); + ret, + clusterlen,0 );if (ret <0 ) {error_setg_errno (errp, -ret,"Could not zero fill encryption header" );return -1 ;
tests/qemu-iotests/284 new file +97 -0
97 lines not on this page · raw message
tests/qemu-iotests/284.out new file +62 -0
62 lines not on this page · raw message
tests/qemu-iotests/group +1 -0
@@ -290,3 +290,4 @@ 280 rw migration quick 281 rw quick 283 auto quick +284 rw
Signature · 1 line
2.24.1
Quoted 14 lines · Daniel P. Berrangé
When initializing the LUKS header the size with default encryption parameters will currently be 2068480 bytes. This is rounded up to a multiple of the cluster size, 2081792, with 64k sectors. If the end of the header is not the same as the end of the cluster we fill the extra space with zeros. This was forgetting that not even the space allocated for the header will be fully initialized, as we only write key material for the first key slot. The space left for the other 7 slots is never written to.
The problem only exists when the disk image is entirely empty. Writing data to the disk image payload will solve the problem by causing the end of the file to be extended further.
Quoted 8 lines
The change fixes it by ensuring that the entire allocated LUKS header region is fully initialized with zeros. The qemu-img check will still fail for any pre-existing disk images created prior to this change, unless at least 1 byte of the payload is written to.
Fully writing zeros to the entire LUKS header is a good idea regardless as it ensures that space has been allocated on the host filesystem (or whatever block storage backend is used).
What's more, we avoid a possible bug where creating a LUKS image backed by a block device protocol where the block device happens to already contain stale data from an earlier use of that block device in a different LUKS image, which could make it appear as though we have populated key slots. It's unlikely that those other slots would decode the current image correctly (as the stale keyslot would decode to a different master key), but being able to supply the passphrase to that stale keyslot to decode garbage out of the new image does not seem desirable.
Quoted 17 lines
Signed-off-by Daniel P. Berrangé <[email protected]>---
+++
b/block/qcow2.cblock/qcow2.c +5
-2@@ -135,13 +135,16 @@ static ssize_t qcow2_crypto_hdr_init_func(QCryptoBlock *block, size_t headerlen, s->crypto_header.length = headerlen; s->crypto_header.offset = ret; -/* Zero fill remaining space in cluster so it has predictable -* content in case of future spec changes */ +/* +* Zero fill all space in cluster so it has predictable +* content, as we may not initialize some regions of the +* header (eg only 1 out of 8 key slots will be initialized) +*/
block/qcow2.c static ssize_t qcow2_crypto_hdr_init_func(QCryptoBlock *block, size_t headerlen, · line 150
Quoted 10 lines
block/qcow2.c +2
-2clusterlen =size_to_clusters (s, headerlen) * s->cluster_size;assert (qcow2_pre_write_overlap_check (bs,0 , ret, clusterlen, false) ==0 ); ret =bdrv_pwrite_zeroes (bs->file, - ret + headerlen, - clusterlen - headerlen,0 ); + ret, + clusterlen,0 );if (ret <0 ) {error_setg_errno (errp, -ret,"Could not zero fill encryption header" );return -1 ;
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
Max Reitz
AppliedQuoted 64 lines · Daniel P. Berrangé
When initializing the LUKS header the size with default encryption parameters will currently be 2068480 bytes. This is rounded up to a multiple of the cluster size, 2081792, with 64k sectors. If the end of the header is not the same as the end of the cluster we fill the extra space with zeros. This was forgetting that not even the space allocated for the header will be fully initialized, as we only write key material for the first key slot. The space left for the other 7 slots is never written to.
An optimization to the ref count checking code:
commit
a5fff8d4b4d928311a5005efa12d0991fe3b66f9(refs/bisect/bad)
Author: Vladimir Sementsov-Ogievskiy <[email protected]>
Date: Wed Feb 27 16:14:30 2019 +0300qcow2-refcount: avoid eating RAM
made the assumption that every cluster which was allocated would have at least some data written to it. This was violated by way the LUKS header is only partially written, with much space simply reserved for future use.
Depending on the cluster size this problem was masked by the logic which wrote zeros between the end of the LUKS header and the end of the cluster.
$ qemu-img create --object secret,id=cluster_encrypt0,data=123456 \ -f qcow2 -o cluster_size=2k,encrypt.iter-time=1,\ encrypt.format=luks,encrypt.key-secret=cluster_encrypt0 \ cluster_size_check.qcow2 100M Formatting 'cluster_size_check.qcow2', fmt=qcow2 size=104857600 encrypt.format=luks encrypt.key-secret=cluster_encrypt0 encrypt.iter-time=1 cluster_size=2048 lazy_refcounts=off refcount_bits=16$ qemu-img check --object secret,id=cluster_encrypt0,data=redhat \ 'json:{"driver": "qcow2", "encrypt.format": "luks", \ "encrypt.key-secret": "cluster_encrypt0", \ "file.driver": "file", "file.filename": "cluster_size_check.qcow2"}' ERROR: counting reference for region exceeding the end of the file by one cluster or more: offset 0x2000 size 0x1f9000 Leaked cluster 4 refcount=1 reference=0 ...snip... Leaked cluster 130 refcount=1 reference=01 errors were found on the image.
Data may be corrupted, or further writes to the image may corrupt it.127 leaked clusters were found on the image. This means waste of disk space, but no harm to data. Image end offset: 268288
The problem only exists when the disk image is entirely empty. Writing data to the disk image payload will solve the problem by causing the end of the file to be extended further.
The change fixes it by ensuring that the entire allocated LUKS header region is fully initialized with zeros. The qemu-img check will still fail for any pre-existing disk images created prior to this change, unless at least 1 byte of the payload is written to.
Fully writing zeros to the entire LUKS header is a good idea regardless as it ensures that space has been allocated on the host filesystem (or whatever block storage backend is used).
Signed-off-by Daniel P. Berrangé <[email protected]>
Quoted 8 lines
---
In v2:
- Cut down test scenarios to speed up
- Use_check_test_imghelper
- Fix outdated comments
- Use space not tabs
Using eight spaces for indentation is... Interesting, but at least consistent. :-)
Quoted 7 lines
block/qcow2.c | 11 +++-- tests/qemu-iotests/284 | 97 ++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/284.out | 62 ++++++++++++++++++++++++ tests/qemu-iotests/group | 1 + 4 files changed, 167 insertions(+), 4 deletions(-) create mode 100755 tests/qemu-iotests/284 create mode 100644 tests/qemu-iotests/284.out
Thanks, applied to my block branch:
https://git.xanclic.moe/XanClic/qemu/commits/branch/block
Max