From d5d5a75f22bda80509f02f0669d0619a1cbdaef5 Mon Sep 17 00:00:00 2001
Message-Id: <d5d5a75f22bda80509f02f0669d0619a1cbdaef5.1334770230.git.minovotn@redhat.com>
In-Reply-To: <5e4659718c6d6ee9ab11b269d929a292a71b3ab0.1334770230.git.minovotn@redhat.com>
References: <5e4659718c6d6ee9ab11b269d929a292a71b3ab0.1334770230.git.minovotn@redhat.com>
From: Paolo Bonzini <pbonzini@redhat.com>
Date: Fri, 13 Apr 2012 16:27:14 +0200
Subject: [PATCH 06/18] block: open backing file as read-only when probing for
 size

RH-Author: Paolo Bonzini <pbonzini@redhat.com>
Message-id: <1334334446-31987-5-git-send-email-pbonzini@redhat.com>
Patchwork-id: 39216
O-Subject: [RHEL 6.3 qemu-kvm PATCH 04/16] block: open backing file as read-only when probing for size
Bugzilla: 806432
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>

Bugzilla: 806432

Upstream status: submitted for inclusion in 1.1

bdrv_img_create will temporarily open the backing file to probe its size.
However, this could be done with a read-write open if the wrong flags are
passed to bdrv_img_create.  Since there is really no documentation on
what flags can be passed, assume that the flags would be the flags with
which the new image will be open, and sanitize them when opening the
backing file.
---
 block.c |   10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 block.c |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/block.c b/block.c
index 54f1fc1..46de187 100644
--- a/block.c
+++ b/block.c
@@ -3608,16 +3608,16 @@ int bdrv_img_create(const char *filename, const char *fmt,
     if (get_option_parameter(param, BLOCK_OPT_SIZE)->value.n == -1) {
         if (backing_file && backing_file->value.s) {
             uint64_t size;
-            const char *fmt = NULL;
             char buf[32];
+            int back_flags;
 
-            if (backing_fmt && backing_fmt->value.s) {
-                fmt = backing_fmt->value.s;
-            }
+            /* backing files always opened read-only */
+            back_flags =
+                flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
 
             bs = bdrv_new("");
 
-            ret = bdrv_open(bs, backing_file->value.s, flags, backing_drv);
+            ret = bdrv_open(bs, backing_file->value.s, back_flags, backing_drv);
             if (ret < 0) {
                 error_report("Could not open '%s'", backing_file->value.s);
                 goto out;
-- 
1.7.7.6

