From 1e39e9a997ea27921a25baaa4d173abab26116b6 Mon Sep 17 00:00:00 2001 From: Daman Arora Date: Thu, 26 Nov 2026 12:29:03 -0500 Subject: [PATCH] reject a qcow2 image registered as a raw template --- .../storage/template/RawImageProcessor.java | 20 +++++ .../template/RawImageProcessorTest.java | 81 +++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 core/src/test/java/com/cloud/storage/template/RawImageProcessorTest.java diff --git a/core/src/main/java/com/cloud/storage/template/RawImageProcessor.java b/core/src/main/java/com/cloud/storage/template/RawImageProcessor.java index d6c1f7a808f6..ad86846172a2 100644 --- a/core/src/main/java/com/cloud/storage/template/RawImageProcessor.java +++ b/core/src/main/java/com/cloud/storage/template/RawImageProcessor.java @@ -20,6 +20,9 @@ package com.cloud.storage.template; import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Arrays; import java.util.Map; import javax.naming.ConfigurationException; @@ -31,6 +34,8 @@ import com.cloud.utils.component.AdapterBase; public class RawImageProcessor extends AdapterBase implements Processor { + private static final byte[] QCOW2_MAGIC = {'Q', 'F', 'I', (byte) 0xfb}; + StorageLayer _storage; @Override @@ -60,6 +65,10 @@ public FormatInfo process(String templatePath, ImageFormat format, String templa logger.debug("Unable to find raw image:" + imgPath); return null; } + if (isQcow2(imgPath)) { + throw new InternalErrorException("Template " + templateName + " was registered as RAW but the image is QCOW2, " + + "register it again with format QCOW2"); + } FormatInfo info = new FormatInfo(); info.format = ImageFormat.RAW; info.filename = templateName + "." + ImageFormat.RAW.getFileExtension(); @@ -74,4 +83,15 @@ public long getVirtualSize(File file) { return file.length(); } + // Taking a QCOW2 file's length as its size makes block storage create a volume that is too small + boolean isQcow2(String path) { + byte[] magic = new byte[QCOW2_MAGIC.length]; + try (FileInputStream in = new FileInputStream(path)) { + return in.read(magic) == magic.length && Arrays.equals(magic, QCOW2_MAGIC); + } catch (IOException e) { + logger.debug("Unable to read the header of " + path + ": " + e.getMessage()); + return false; + } + } + } diff --git a/core/src/test/java/com/cloud/storage/template/RawImageProcessorTest.java b/core/src/test/java/com/cloud/storage/template/RawImageProcessorTest.java new file mode 100644 index 000000000000..97f7736e9cee --- /dev/null +++ b/core/src/test/java/com/cloud/storage/template/RawImageProcessorTest.java @@ -0,0 +1,81 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.storage.template; + +import java.io.File; +import java.nio.file.Files; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; + +import com.cloud.exception.InternalErrorException; +import com.cloud.storage.StorageLayer; + +@RunWith(MockitoJUnitRunner.class) +public class RawImageProcessorTest { + + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + @Mock + StorageLayer storage; + + private RawImageProcessor processor; + + @Before + public void setUp() throws Exception { + processor = new RawImageProcessor(); + Map params = new HashMap<>(); + params.put(StorageLayer.InstanceConfigKey, storage); + processor.configure("Raw Image Processor", params); + } + + private File image(byte[] header) throws Exception { + File file = folder.newFile("template.raw"); + byte[] content = new byte[4096]; + System.arraycopy(header, 0, content, 0, header.length); + Files.write(file.toPath(), content); + Mockito.when(storage.exists(file.getPath())).thenReturn(true); + Mockito.lenient().when(storage.getSize(file.getPath())).thenReturn(file.length()); + return file; + } + + @Test + public void processAcceptsRawImage() throws Exception { + image(new byte[] {(byte) 0xeb, 0x63, (byte) 0x90}); + + Processor.FormatInfo info = processor.process(folder.getRoot().getPath(), null, "template"); + + Assert.assertEquals(4096L, info.virtualSize); + } + + @Test(expected = InternalErrorException.class) + public void processRejectsQcow2RegisteredAsRaw() throws Exception { + image(new byte[] {'Q', 'F', 'I', (byte) 0xfb, 0, 0, 0, 3}); + + processor.process(folder.getRoot().getPath(), null, "template"); + } +}