From: Peter Krempa <[email protected]>

If the QCOW2 header had more than 64 cluster bits, our parser would do a
bitshift operation to determine cluster size beyond the amount of bits
in the variable which is undefined behaviour. Any normal implementation
would then have the clusterSize variable to be 0 which is what we've
wanted but to avoid relying on UB avoid the bitshift completely if the
QCOW2 header specifies 64 or more cluster bits.

Closes: https://gitlab.com/libvirt/libvirt/-/work_items/952
Signed-off-by: Peter Krempa <[email protected]>
---
 src/storage_file/storage_file_probe.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/storage_file/storage_file_probe.c 
b/src/storage_file/storage_file_probe.c
index a30528b02c..7436ce34a5 100644
--- a/src/storage_file/storage_file_probe.c
+++ b/src/storage_file/storage_file_probe.c
@@ -671,7 +671,8 @@ qcow2GetImageSpecific(virStorageSource *meta,
     if (buf_size > (QCOWX_HDR_CLUSTER_BITS_OFFSET + 4)) {
         int clusterBits = virReadBufInt32BE(buf + 
QCOWX_HDR_CLUSTER_BITS_OFFSET);

-        if (clusterBits > 0)
+        if (clusterBits > 0 &&
+            clusterBits < sizeof(meta->clusterSize) * 8)
             meta->clusterSize = 1ULL << clusterBits;
     }

-- 
2.56.0

Reply via email to