cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
218e99
From e04d52892c7fcd0e2ec70f77f3e5b934d45918c8 Mon Sep 17 00:00:00 2001
218e99
From: Miroslav Rezanina <mrezanin@redhat.com>
218e99
Date: Wed, 31 Jul 2013 09:56:17 +0200
218e99
Subject: block: add block driver read only whitelist
218e99
218e99
Message-id: <6873f36f1d3c26ad7b84bf2150c0a98afd6c5e72.1375208619.git.mrezanin@redhat.com>
218e99
Patchwork-id: 52827
218e99
O-Subject: [RHEL7 qemu-kvm PATCH 4.5/5] block: add block driver read only whitelist
218e99
Bugzilla: 836675
218e99
RH-Acked-by: Fam Zheng <famz@redhat.com>
218e99
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
218e99
RH-Acked-by: Michal Novotny <minovotn@redhat.com>
218e99
218e99
From: Fam Zheng <famz@redhat.com>
218e99
218e99
This is missing patch required for "VMDK Backports and Spec Update" serie.
218e99
218e99
We may want to include a driver in the whitelist for read only tasks
218e99
such as diagnosing or exporting guest data (with libguestfs as a good
218e99
example). This patch introduces a readonly whitelist option, and for
218e99
backward compatibility, the old configure option --block-drv-whitelist
218e99
is now an alias to rw whitelist.
218e99
218e99
Drivers in readonly list is only permitted to open file readonly, and
218e99
returns -ENOTSUP for RW opening.
218e99
218e99
E.g. To include vmdk readonly, and others read+write:
218e99
    ./configure --target-list=x86_64-softmmu \
218e99
                --block-drv-rw-whitelist=qcow2,raw,file,qed \
218e99
                --block-drv-ro-whitelist=vmdk
218e99
218e99
Signed-off-by: Fam Zheng <famz@redhat.com>
218e99
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
218e99
(cherry picked from commit b64ec4e4ade581d662753cdeb0d7e0e27aafbf81)
218e99
218e99
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
218e99
218e99
diff --git a/block.c b/block.c
218e99
index 3f87489..65c0b60 100644
218e99
--- a/block.c
218e99
+++ b/block.c
218e99
@@ -328,28 +328,40 @@ BlockDriver *bdrv_find_format(const char *format_name)
218e99
     return NULL;
218e99
 }
218e99
 
218e99
-static int bdrv_is_whitelisted(BlockDriver *drv)
218e99
+static int bdrv_is_whitelisted(BlockDriver *drv, bool read_only)
218e99
 {
218e99
-    static const char *whitelist[] = {
218e99
-        CONFIG_BDRV_WHITELIST
218e99
+    static const char *whitelist_rw[] = {
218e99
+        CONFIG_BDRV_RW_WHITELIST
218e99
+    };
218e99
+    static const char *whitelist_ro[] = {
218e99
+        CONFIG_BDRV_RO_WHITELIST
218e99
     };
218e99
     const char **p;
218e99
 
218e99
-    if (!whitelist[0])
218e99
+    if (!whitelist_rw[0] && !whitelist_ro[0]) {
218e99
         return 1;               /* no whitelist, anything goes */
218e99
+    }
218e99
 
218e99
-    for (p = whitelist; *p; p++) {
218e99
+    for (p = whitelist_rw; *p; p++) {
218e99
         if (!strcmp(drv->format_name, *p)) {
218e99
             return 1;
218e99
         }
218e99
     }
218e99
+    if (read_only) {
218e99
+        for (p = whitelist_ro; *p; p++) {
218e99
+            if (!strcmp(drv->format_name, *p)) {
218e99
+                return 1;
218e99
+            }
218e99
+        }
218e99
+    }
218e99
     return 0;
218e99
 }
218e99
 
218e99
-BlockDriver *bdrv_find_whitelisted_format(const char *format_name)
218e99
+BlockDriver *bdrv_find_whitelisted_format(const char *format_name,
218e99
+                                          bool read_only)
218e99
 {
218e99
     BlockDriver *drv = bdrv_find_format(format_name);
218e99
-    return drv && bdrv_is_whitelisted(drv) ? drv : NULL;
218e99
+    return drv && bdrv_is_whitelisted(drv, read_only) ? drv : NULL;
218e99
 }
218e99
 
218e99
 typedef struct CreateCo {
218e99
@@ -684,10 +696,6 @@ static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file,
218e99
 
218e99
     trace_bdrv_open_common(bs, filename ?: "", flags, drv->format_name);
218e99
 
218e99
-    if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
218e99
-        return -ENOTSUP;
218e99
-    }
218e99
-
218e99
     /* bdrv_open() with directly using a protocol as drv. This layer is already
218e99
      * opened, so assign it to bs (while file becomes a closed BlockDriverState)
218e99
      * and return immediately. */
218e99
@@ -698,9 +706,15 @@ static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file,
218e99
 
218e99
     bs->open_flags = flags;
218e99
     bs->buffer_alignment = 512;
218e99
+    open_flags = bdrv_open_flags(bs, flags);
218e99
+    bs->read_only = !(open_flags & BDRV_O_RDWR);
218e99
+
218e99
+    if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) {
218e99
+        return -ENOTSUP;
218e99
+    }
218e99
 
218e99
     assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */
218e99
-    if ((flags & BDRV_O_RDWR) && (flags & BDRV_O_COPY_ON_READ)) {
218e99
+    if (!bs->read_only && (flags & BDRV_O_COPY_ON_READ)) {
218e99
         bdrv_enable_copy_on_read(bs);
218e99
     }
218e99
 
218e99
@@ -714,9 +728,6 @@ static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file,
218e99
     bs->opaque = g_malloc0(drv->instance_size);
218e99
 
218e99
     bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB);
218e99
-    open_flags = bdrv_open_flags(bs, flags);
218e99
-
218e99
-    bs->read_only = !(open_flags & BDRV_O_RDWR);
218e99
 
218e99
     /* Open the image, either directly or using a protocol */
218e99
     if (drv->bdrv_file_open) {
218e99
@@ -801,7 +812,7 @@ int bdrv_file_open(BlockDriverState **pbs, const char *filename,
218e99
     /* Find the right block driver */
218e99
     drvname = qdict_get_try_str(options, "driver");
218e99
     if (drvname) {
218e99
-        drv = bdrv_find_whitelisted_format(drvname);
218e99
+        drv = bdrv_find_whitelisted_format(drvname, !(flags & BDRV_O_RDWR));
218e99
         qdict_del(options, "driver");
218e99
     } else if (filename) {
218e99
         drv = bdrv_find_protocol(filename);
218e99
diff --git a/blockdev.c b/blockdev.c
218e99
index 625d041..6500c47 100644
218e99
--- a/blockdev.c
218e99
+++ b/blockdev.c
218e99
@@ -477,7 +477,7 @@ DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type)
218e99
             error_printf("\n");
218e99
             return NULL;
218e99
         }
218e99
-        drv = bdrv_find_whitelisted_format(buf);
218e99
+        drv = bdrv_find_whitelisted_format(buf, ro);
218e99
         if (!drv) {
218e99
             error_report("'%s' invalid format", buf);
218e99
             return NULL;
218e99
@@ -1024,7 +1024,7 @@ void qmp_change_blockdev(const char *device, const char *filename,
218e99
     }
218e99
 
218e99
     if (format) {
218e99
-        drv = bdrv_find_whitelisted_format(format);
218e99
+        drv = bdrv_find_whitelisted_format(format, bs->read_only);
218e99
         if (!drv) {
218e99
             error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
218e99
             return;
218e99
diff --git a/configure b/configure
218e99
index eb74510..a71e8a1 100755
218e99
--- a/configure
218e99
+++ b/configure
218e99
@@ -123,7 +123,8 @@ interp_prefix="/usr/gnemul/qemu-%M"
218e99
 static="no"
218e99
 cross_prefix=""
218e99
 audio_drv_list=""
218e99
-block_drv_whitelist=""
218e99
+block_drv_rw_whitelist=""
218e99
+block_drv_ro_whitelist=""
218e99
 host_cc="cc"
218e99
 libs_softmmu=""
218e99
 libs_tools=""
218e99
@@ -708,7 +709,9 @@ for opt do
218e99
   ;;
218e99
   --audio-drv-list=*) audio_drv_list="$optarg"
218e99
   ;;
218e99
-  --block-drv-whitelist=*) block_drv_whitelist=`echo "$optarg" | sed -e 's/,/ /g'`
218e99
+  --block-drv-rw-whitelist=*|--block-drv-whitelist=*) block_drv_rw_whitelist=`echo "$optarg" | sed -e 's/,/ /g'`
218e99
+  ;;
218e99
+  --block-drv-ro-whitelist=*) block_drv_ro_whitelist=`echo "$optarg" | sed -e 's/,/ /g'`
218e99
   ;;
218e99
   --enable-debug-tcg) debug_tcg="yes"
218e99
   ;;
218e99
@@ -1105,7 +1108,12 @@ echo "  --disable-cocoa          disable Cocoa (Mac OS X only)"
218e99
 echo "  --enable-cocoa           enable Cocoa (default on Mac OS X)"
218e99
 echo "  --audio-drv-list=LIST    set audio drivers list:"
218e99
 echo "                           Available drivers: $audio_possible_drivers"
218e99
-echo "  --block-drv-whitelist=L  set block driver whitelist"
218e99
+echo "  --block-drv-whitelist=L  Same as --block-drv-rw-whitelist=L"
218e99
+echo "  --block-drv-rw-whitelist=L"
218e99
+echo "                           set block driver read-write whitelist"
218e99
+echo "                           (affects only QEMU, not qemu-img)"
218e99
+echo "  --block-drv-ro-whitelist=L"
218e99
+echo "                           set block driver read-only whitelist"
218e99
 echo "                           (affects only QEMU, not qemu-img)"
218e99
 echo "  --enable-mixemu          enable mixer emulation"
218e99
 echo "  --disable-xen            disable xen backend driver support"
218e99
@@ -3525,7 +3533,8 @@ echo "curses support    $curses"
218e99
 echo "curl support      $curl"
218e99
 echo "mingw32 support   $mingw32"
218e99
 echo "Audio drivers     $audio_drv_list"
218e99
-echo "Block whitelist   $block_drv_whitelist"
218e99
+echo "Block whitelist (rw) $block_drv_rw_whitelist"
218e99
+echo "Block whitelist (ro) $block_drv_ro_whitelist"
218e99
 echo "Mixer emulation   $mixemu"
218e99
 echo "VirtFS support    $virtfs"
218e99
 echo "VNC support       $vnc"
218e99
@@ -3704,7 +3713,8 @@ fi
218e99
 if test "$audio_win_int" = "yes" ; then
218e99
   echo "CONFIG_AUDIO_WIN_INT=y" >> $config_host_mak
218e99
 fi
218e99
-echo "CONFIG_BDRV_WHITELIST=$block_drv_whitelist" >> $config_host_mak
218e99
+echo "CONFIG_BDRV_RW_WHITELIST=$block_drv_rw_whitelist" >> $config_host_mak
218e99
+echo "CONFIG_BDRV_RO_WHITELIST=$block_drv_ro_whitelist" >> $config_host_mak
218e99
 if test "$mixemu" = "yes" ; then
218e99
   echo "CONFIG_MIXEMU=y" >> $config_host_mak
218e99
 fi
218e99
diff --git a/hw/block/xen_disk.c b/hw/block/xen_disk.c
218e99
index 0ac65d4..247f32f 100644
218e99
--- a/hw/block/xen_disk.c
218e99
+++ b/hw/block/xen_disk.c
218e99
@@ -780,11 +780,13 @@ static int blk_connect(struct XenDevice *xendev)
218e99
 {
218e99
     struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
218e99
     int pers, index, qflags;
218e99
+    bool readonly = true;
218e99
 
218e99
     /* read-only ? */
218e99
     qflags = BDRV_O_CACHE_WB | BDRV_O_NATIVE_AIO;
218e99
     if (strcmp(blkdev->mode, "w") == 0) {
218e99
         qflags |= BDRV_O_RDWR;
218e99
+        readonly = false;
218e99
     }
218e99
 
218e99
     /* init qemu block driver */
218e99
@@ -795,8 +797,10 @@ static int blk_connect(struct XenDevice *xendev)
218e99
         xen_be_printf(&blkdev->xendev, 2, "create new bdrv (xenbus setup)\n");
218e99
         blkdev->bs = bdrv_new(blkdev->dev);
218e99
         if (blkdev->bs) {
218e99
-            if (bdrv_open(blkdev->bs, blkdev->filename, NULL, qflags,
218e99
-                        bdrv_find_whitelisted_format(blkdev->fileproto)) != 0) {
218e99
+            BlockDriver *drv = bdrv_find_whitelisted_format(blkdev->fileproto,
218e99
+                                                           readonly);
218e99
+            if (bdrv_open(blkdev->bs,
218e99
+                          blkdev->filename, NULL, qflags, drv) != 0) {
218e99
                 bdrv_delete(blkdev->bs);
218e99
                 blkdev->bs = NULL;
218e99
             }
218e99
diff --git a/include/block/block.h b/include/block/block.h
218e99
index 1251c5c..5604418 100644
218e99
--- a/include/block/block.h
218e99
+++ b/include/block/block.h
218e99
@@ -124,7 +124,8 @@ void bdrv_init(void);
218e99
 void bdrv_init_with_whitelist(void);
218e99
 BlockDriver *bdrv_find_protocol(const char *filename);
218e99
 BlockDriver *bdrv_find_format(const char *format_name);
218e99
-BlockDriver *bdrv_find_whitelisted_format(const char *format_name);
218e99
+BlockDriver *bdrv_find_whitelisted_format(const char *format_name,
218e99
+                                          bool readonly);
218e99
 int bdrv_create(BlockDriver *drv, const char* filename,
218e99
     QEMUOptionParameter *options);
218e99
 int bdrv_create_file(const char* filename, QEMUOptionParameter *options);
218e99
diff --git a/scripts/create_config b/scripts/create_config
218e99
index c471e8c..258513a 100755
218e99
--- a/scripts/create_config
218e99
+++ b/scripts/create_config
218e99
@@ -34,8 +34,15 @@ case $line in
218e99
     done
218e99
     echo ""
218e99
     ;;
218e99
- CONFIG_BDRV_WHITELIST=*)
218e99
-    echo "#define CONFIG_BDRV_WHITELIST \\"
218e99
+ CONFIG_BDRV_RW_WHITELIST=*)
218e99
+    echo "#define CONFIG_BDRV_RW_WHITELIST\\"
218e99
+    for drv in ${line#*=}; do
218e99
+      echo "    \"${drv}\",\\"
218e99
+    done
218e99
+    echo "    NULL"
218e99
+    ;;
218e99
+ CONFIG_BDRV_RO_WHITELIST=*)
218e99
+    echo "#define CONFIG_BDRV_RO_WHITELIST\\"
218e99
     for drv in ${line#*=}; do
218e99
       echo "    \"${drv}\",\\"
218e99
     done