mvadkert / rpms / qemu

Forked from rpms/qemu 6 years ago
Clone
5dd4694
From 0dc68802cae7f09529e2eed70be91c1131271c90 Mon Sep 17 00:00:00 2001
5dd4694
From: "Daniel P. Berrange" <berrange@redhat.com>
5dd4694
Date: Tue, 19 Mar 2013 11:20:20 +0000
5dd4694
Subject: [PATCH] Add -f FMT / --format FMT arg to qemu-nbd
5dd4694
5dd4694
Currently the qemu-nbd program will auto-detect the format of
5dd4694
any disk it is given. This behaviour is known to be insecure.
5dd4694
For example, if qemu-nbd initially exposes a 'raw' file to an
5dd4694
unprivileged app, and that app runs
5dd4694
5dd4694
   'qemu-img create -f qcow2 -o backing_file=/etc/shadow /dev/nbd0'
5dd4694
5dd4694
then the next time the app is started, the qemu-nbd will now
5dd4694
detect it as a 'qcow2' file and expose /etc/shadow to the
5dd4694
unprivileged app.
5dd4694
5dd4694
The only way to avoid this is to explicitly tell qemu-nbd what
5dd4694
disk format to use on the command line, completely disabling
5dd4694
auto-detection. This patch adds a '-f' / '--format' arg for
5dd4694
this purpose, mirroring what is already available via qemu-img
5dd4694
and qemu commands.
5dd4694
5dd4694
  qemu-nbd --format raw -p 9000 evil.img
5dd4694
5dd4694
will now always use raw, regardless of what format 'evil.img'
5dd4694
looks like it contains
5dd4694
5dd4694
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
5dd4694
[Use errx, not err. - Paolo]
5dd4694
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
5dd4694
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
5dd4694
(cherry picked from commit e6b636779b51c97e67694be740ee972c52460c59)
5dd4694
5dd4694
Conflicts:
5dd4694
	qemu-nbd.c
5dd4694
---
5dd4694
 qemu-nbd.c    | 20 ++++++++++++++++++--
5dd4694
 qemu-nbd.texi |  2 ++
5dd4694
 2 files changed, 20 insertions(+), 2 deletions(-)
5dd4694
5dd4694
diff --git a/qemu-nbd.c b/qemu-nbd.c
5dd4694
index 291cba2..8fbe2cf 100644
5dd4694
--- a/qemu-nbd.c
5dd4694
+++ b/qemu-nbd.c
5dd4694
@@ -247,6 +247,7 @@ out:
5dd4694
 int main(int argc, char **argv)
5dd4694
 {
5dd4694
     BlockDriverState *bs;
5dd4694
+    BlockDriver *drv;
5dd4694
     off_t dev_offset = 0;
5dd4694
     off_t offset = 0;
5dd4694
     uint32_t nbdflags = 0;
5dd4694
@@ -256,7 +257,7 @@ int main(int argc, char **argv)
5dd4694
     struct sockaddr_in addr;
5dd4694
     socklen_t addr_len = sizeof(addr);
5dd4694
     off_t fd_size;
5dd4694
-    const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t";
5dd4694
+    const char *sopt = "hVb:o:p:rsnP:c:dvk:e:f:t";
5dd4694
     struct option lopt[] = {
5dd4694
         { "help", 0, NULL, 'h' },
5dd4694
         { "version", 0, NULL, 'V' },
5dd4694
@@ -271,6 +272,7 @@ int main(int argc, char **argv)
5dd4694
         { "snapshot", 0, NULL, 's' },
5dd4694
         { "nocache", 0, NULL, 'n' },
5dd4694
         { "shared", 1, NULL, 'e' },
5dd4694
+        { "format", 1, NULL, 'f' },
5dd4694
         { "persistent", 0, NULL, 't' },
5dd4694
         { "verbose", 0, NULL, 'v' },
5dd4694
         { NULL, 0, NULL, 0 }
5dd4694
@@ -292,6 +294,7 @@ int main(int argc, char **argv)
5dd4694
     int max_fd;
5dd4694
     int persistent = 0;
5dd4694
     pthread_t client_thread;
5dd4694
+    const char *fmt = NULL;
5dd4694
 
5dd4694
     /* The client thread uses SIGTERM to interrupt the server.  A signal
5dd4694
      * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
5dd4694
@@ -368,6 +371,9 @@ int main(int argc, char **argv)
5dd4694
                 errx(EXIT_FAILURE, "Shared device number must be greater than 0\n");
5dd4694
             }
5dd4694
             break;
5dd4694
+        case 'f':
5dd4694
+            fmt = optarg;
5dd4694
+            break;
5dd4694
 	case 't':
5dd4694
 	    persistent = 1;
5dd4694
 	    break;
5dd4694
@@ -478,9 +484,19 @@ int main(int argc, char **argv)
5dd4694
     bdrv_init();
5dd4694
     atexit(bdrv_close_all);
5dd4694
 
5dd4694
+    if (fmt) {
5dd4694
+        drv = bdrv_find_format(fmt);
5dd4694
+        if (!drv) {
5dd4694
+            errx(EXIT_FAILURE, "Unknown file format '%s'", fmt);
5dd4694
+        }
5dd4694
+    } else {
5dd4694
+        drv = NULL;
5dd4694
+    }
5dd4694
+
5dd4694
     bs = bdrv_new("hda");
5dd4694
     srcpath = argv[optind];
5dd4694
-    if ((ret = bdrv_open(bs, srcpath, flags, NULL)) < 0) {
5dd4694
+    ret = bdrv_open(bs, srcpath, flags, drv);
5dd4694
+    if (ret < 0) {
5dd4694
         errno = -ret;
5dd4694
         err(EXIT_FAILURE, "Failed to bdrv_open '%s'", argv[optind]);
5dd4694
     }
5dd4694
diff --git a/qemu-nbd.texi b/qemu-nbd.texi
5dd4694
index 44996cc..f56c68e 100644
5dd4694
--- a/qemu-nbd.texi
5dd4694
+++ b/qemu-nbd.texi
5dd4694
@@ -36,6 +36,8 @@ Export Qemu disk image using NBD protocol.
5dd4694
   disconnect the specified device
5dd4694
 @item -e, --shared=@var{num}
5dd4694
   device can be shared by @var{num} clients (default @samp{1})
5dd4694
+@item -f, --format=@var{fmt}
5dd4694
+  force block driver for format @var{fmt} instead of auto-detecting
5dd4694
 @item -t, --persistent
5dd4694
   don't exit on the last connection
5dd4694
 @item -v, --verbose