[libvirt] [PATCH v2 1/2] Support shifted magic in storage files

Martin Kletzander mkletzan at redhat.com
Mon Feb 4 21:49:56 UTC 2013


Some files have the magic shifted to some offset other than 0, so we
have to support that.  I also cleaned up some lines to be more
readable and added missing magic for iso file format.
---
 src/util/virstoragefile.c | 55 ++++++++++++++++++++++++++---------------------
 1 file changed, 31 insertions(+), 24 deletions(-)

diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
index cdac5b1..4f31572 100644
--- a/src/util/virstoragefile.c
+++ b/src/util/virstoragefile.c
@@ -1,7 +1,7 @@
 /*
  * virstoragefile.c: file utility functions for FS storage backend
  *
- * Copyright (C) 2007-2012 Red Hat, Inc.
+ * Copyright (C) 2007-2013 Red Hat, Inc.
  * Copyright (C) 2007-2008 Daniel P. Berrange
  *
  * This library is free software; you can redistribute it and/or
@@ -66,6 +66,7 @@ enum {

 /* Either 'magic' or 'extension' *must* be provided */
 struct FileTypeInfo {
+    int magicOffset;    /* Byte offset of the magic */
     const char *magic;  /* Optional string of file magic
                          * to check at head of file */
     const char *extension; /* Optional file extension to check */
@@ -127,71 +128,75 @@ qedGetBackingStore(char **, int *, const unsigned char *, size_t);


 static struct FileTypeInfo const fileTypeInfo[] = {
-    [VIR_STORAGE_FILE_NONE] = { NULL, NULL, LV_LITTLE_ENDIAN,
+    [VIR_STORAGE_FILE_NONE] = { 0, NULL, NULL, LV_LITTLE_ENDIAN,
                                 -1, 0, 0, 0, 0, 0, NULL },
-    [VIR_STORAGE_FILE_RAW] = { NULL, NULL, LV_LITTLE_ENDIAN,
+    [VIR_STORAGE_FILE_RAW] = { 0, NULL, NULL, LV_LITTLE_ENDIAN,
                                -1, 0, 0, 0, 0, 0, NULL },
-    [VIR_STORAGE_FILE_DIR] = { NULL, NULL, LV_LITTLE_ENDIAN,
+    [VIR_STORAGE_FILE_DIR] = { 0, NULL, NULL, LV_LITTLE_ENDIAN,
                                -1, 0, 0, 0, 0, 0, NULL },
     [VIR_STORAGE_FILE_BOCHS] = {
-        /*"Bochs Virtual HD Image", */ /* Untested */ NULL,
-        NULL,
+        /*"Bochs Virtual HD Image", */ /* Untested */
+        0, NULL, NULL,
         LV_LITTLE_ENDIAN, 64, 0x20000,
         32+16+16+4+4+4+4+4, 8, 1, -1, NULL
     },
     [VIR_STORAGE_FILE_CLOOP] = {
-        /*"#!/bin/sh\n#V2.0 Format\nmodprobe cloop file=$0 && mount -r -t iso9660 /dev/cloop $1\n", */ /* Untested */ NULL,
-        NULL,
+        /* #!/bin/sh
+           #V2.0 Format
+           modprobe cloop file=$0 && mount -r -t iso9660 /dev/cloop $1
+        */ /* Untested */
+        0, NULL, NULL,
         LV_LITTLE_ENDIAN, -1, 0,
         -1, 0, 0, -1, NULL
     },
     [VIR_STORAGE_FILE_COW] = {
-        "OOOM", NULL,
+        0, "OOOM", NULL,
         LV_BIG_ENDIAN, 4, 2,
         4+4+1024+4, 8, 1, -1, cowGetBackingStore
     },
     [VIR_STORAGE_FILE_DMG] = {
-        NULL, /* XXX QEMU says there's no magic for dmg, but we should check... */
-        ".dmg",
+        /* XXX QEMU says there's no magic for dmg,
+         * /usr/share/misc/magic says there is double magic both has
+         * to be true) and has it disabled as well */
+        0, NULL, ".dmg",
         0, -1, 0,
         -1, 0, 0, -1, NULL
     },
     [VIR_STORAGE_FILE_ISO] = {
-        NULL, /* XXX there's probably some magic for iso we can validate too... */
-        ".iso",
+        32769, "CD001", ".iso",
         0, -1, 0,
         -1, 0, 0, -1, NULL
     },
     [VIR_STORAGE_FILE_QCOW] = {
-        "QFI", NULL,
+        0, "QFI", NULL,
         LV_BIG_ENDIAN, 4, 1,
         QCOWX_HDR_IMAGE_SIZE, 8, 1, QCOW1_HDR_CRYPT, qcow1GetBackingStore,
     },
     [VIR_STORAGE_FILE_QCOW2] = {
-        "QFI", NULL,
+        0, "QFI", NULL,
         LV_BIG_ENDIAN, 4, 2,
         QCOWX_HDR_IMAGE_SIZE, 8, 1, QCOW2_HDR_CRYPT, qcow2GetBackingStore,
     },
     [VIR_STORAGE_FILE_QED] = {
         /* http://wiki.qemu.org/Features/QED */
-        "QED", NULL,
+        0, "QED", NULL,
         LV_LITTLE_ENDIAN, -2, -1,
         QED_HDR_IMAGE_SIZE, 8, 1, -1, qedGetBackingStore,
     },
     [VIR_STORAGE_FILE_VMDK] = {
-        "KDMV", NULL,
+        0, "KDMV", NULL,
         LV_LITTLE_ENDIAN, 4, 1,
         4+4+4, 8, 512, -1, vmdk4GetBackingStore
     },
     [VIR_STORAGE_FILE_VPC] = {
-        "conectix", NULL,
+        0, "conectix", NULL,
         LV_BIG_ENDIAN, 12, 0x10000,
         8 + 4 + 4 + 8 + 4 + 4 + 2 + 2 + 4, 8, 1, -1, NULL
     },
     /* Not direct file formats, but used for various drivers */
-    [VIR_STORAGE_FILE_FAT] = { NULL, NULL, LV_LITTLE_ENDIAN,
+    [VIR_STORAGE_FILE_FAT] = { 0, NULL, NULL, LV_LITTLE_ENDIAN,
                                -1, 0, 0, 0, 0, 0, NULL },
-    [VIR_STORAGE_FILE_VHD] = { NULL, NULL, LV_LITTLE_ENDIAN,
+    [VIR_STORAGE_FILE_VHD] = { 0, NULL, NULL, LV_LITTLE_ENDIAN,
                                -1, 0, 0, 0, 0, 0, NULL },
 };
 verify(ARRAY_CARDINALITY(fileTypeInfo) == VIR_STORAGE_FILE_LAST);
@@ -571,16 +576,18 @@ virStorageFileMatchesMagic(int format,
                            size_t buflen)
 {
     int mlen;
+    int magicOffset = fileTypeInfo[format].magicOffset;
+    const char *magic = fileTypeInfo[format].magic;

-    if (fileTypeInfo[format].magic == NULL)
+    if (magic == NULL)
         return false;

     /* Validate magic data */
-    mlen = strlen(fileTypeInfo[format].magic);
-    if (mlen > buflen)
+    mlen = strlen(magic);
+    if (magicOffset + mlen > buflen)
         return false;

-    if (memcmp(buf, fileTypeInfo[format].magic, mlen) != 0)
+    if (memcmp(buf + magicOffset, magic, mlen) != 0)
         return false;

     return true;
-- 
1.8.1.2




More information about the libvir-list mailing list