From patchwork Mon Apr 15 19:29:51 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Liguori X-Patchwork-Id: 236707 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id DF4172C00DB for ; Tue, 16 Apr 2013 05:31:17 +1000 (EST) Received: from localhost ([::1]:33216 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1URp7U-0002o7-5R for incoming@patchwork.ozlabs.org; Mon, 15 Apr 2013 15:31:16 -0400 Received: from eggs.gnu.org ([208.118.235.92]:55833) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1URp6S-0001y4-2H for qemu-devel@nongnu.org; Mon, 15 Apr 2013 15:30:17 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1URp6Q-0003pC-9e for qemu-devel@nongnu.org; Mon, 15 Apr 2013 15:30:11 -0400 Received: from e23smtp09.au.ibm.com ([202.81.31.142]:60268) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1URp6P-0003ok-PI for qemu-devel@nongnu.org; Mon, 15 Apr 2013 15:30:10 -0400 Received: from /spool/local by e23smtp09.au.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 16 Apr 2013 05:21:00 +1000 Received: from d23dlp02.au.ibm.com (202.81.31.213) by e23smtp09.au.ibm.com (202.81.31.206) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 16 Apr 2013 05:20:59 +1000 Received: from d23relay04.au.ibm.com (d23relay04.au.ibm.com [9.190.234.120]) by d23dlp02.au.ibm.com (Postfix) with ESMTP id 354C12BB0050 for ; Tue, 16 Apr 2013 05:29:56 +1000 (EST) Received: from d23av03.au.ibm.com (d23av03.au.ibm.com [9.190.234.97]) by d23relay04.au.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r3FJGVVY63635586 for ; Tue, 16 Apr 2013 05:16:31 +1000 Received: from d23av03.au.ibm.com (loopback [127.0.0.1]) by d23av03.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r3FJTt2W020215 for ; Tue, 16 Apr 2013 05:29:55 +1000 Received: from titi.austin.rr.com (sig-9-76-131-241.mts.ibm.com [9.76.131.241]) by d23av03.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id r3FJTqeA020182; Tue, 16 Apr 2013 05:29:54 +1000 From: Anthony Liguori To: qemu-devel@nongnu.org Date: Mon, 15 Apr 2013 14:29:51 -0500 Message-Id: <1366054191-26828-1-git-send-email-aliguori@us.ibm.com> X-Mailer: git-send-email 1.8.0 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13041519-3568-0000-0000-0000036F5ACE X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 202.81.31.142 Cc: Paolo Bonzini , Anthony Liguori Subject: [Qemu-devel] [PATCH] Add -f FMT / --format FMT arg to qemu-nbd X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: "Daniel P. Berrange" Currently the qemu-nbd program will auto-detect the format of any disk it is given. This behaviour is known to be insecure. For example, if qemu-nbd initially exposes a 'raw' file to an unprivileged app, and that app runs 'qemu-img create -f qcow2 -o backing_file=/etc/shadow /dev/nbd0' then the next time the app is started, the qemu-nbd will now detect it as a 'qcow2' file and expose /etc/shadow to the unprivileged app. The only way to avoid this is to explicitly tell qemu-nbd what disk format to use on the command line, completely disabling auto-detection. This patch adds a '-f' / '--format' arg for this purpose, mirroring what is already available via qemu-img and qemu commands. qemu-nbd --format raw -p 9000 evil.img will now always use raw, regardless of what format 'evil.img' looks like it contains Signed-off-by: Daniel P. Berrange [Use errx, not err. - Paolo] Signed-off-by: Paolo Bonzini Signed-off-by: Anthony Liguori --- qemu-nbd.c | 20 ++++++++++++++++++-- qemu-nbd.texi | 2 ++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/qemu-nbd.c b/qemu-nbd.c index ca722ed..9c31d45 100644 --- a/qemu-nbd.c +++ b/qemu-nbd.c @@ -306,6 +306,7 @@ static void nbd_accept(void *opaque) int main(int argc, char **argv) { BlockDriverState *bs; + BlockDriver *drv; off_t dev_offset = 0; uint32_t nbdflags = 0; bool disconnect = false; @@ -313,7 +314,7 @@ int main(int argc, char **argv) char *device = NULL; int port = NBD_DEFAULT_PORT; off_t fd_size; - const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t"; + const char *sopt = "hVb:o:p:rsnP:c:dvk:e:f:t"; struct option lopt[] = { { "help", 0, NULL, 'h' }, { "version", 0, NULL, 'V' }, @@ -333,6 +334,7 @@ int main(int argc, char **argv) #endif { "discard", 1, NULL, QEMU_NBD_OPT_DISCARD }, { "shared", 1, NULL, 'e' }, + { "format", 1, NULL, 'f' }, { "persistent", 0, NULL, 't' }, { "verbose", 0, NULL, 'v' }, { NULL, 0, NULL, 0 } @@ -351,6 +353,7 @@ int main(int argc, char **argv) bool seen_aio = false; #endif pthread_t client_thread; + const char *fmt = NULL; /* The client thread uses SIGTERM to interrupt the server. A signal * handler ensures that "qemu-nbd -v -c" exits with a nice status code. @@ -454,6 +457,9 @@ int main(int argc, char **argv) errx(EXIT_FAILURE, "Shared device number must be greater than 0\n"); } break; + case 'f': + fmt = optarg; + break; case 't': persistent = 1; break; @@ -555,9 +561,19 @@ int main(int argc, char **argv) bdrv_init(); atexit(bdrv_close_all); + if (fmt) { + drv = bdrv_find_format(fmt); + if (!drv) { + errx(EXIT_FAILURE, "Unknown file format '%s'", fmt); + } + } else { + drv = NULL; + } + bs = bdrv_new("hda"); srcpath = argv[optind]; - if ((ret = bdrv_open(bs, srcpath, NULL, flags, NULL)) < 0) { + ret = bdrv_open(bs, srcpath, NULL, flags, drv); + if (ret < 0) { errno = -ret; err(EXIT_FAILURE, "Failed to bdrv_open '%s'", argv[optind]); } diff --git a/qemu-nbd.texi b/qemu-nbd.texi index 5f3f3e3..6055ec6 100644 --- a/qemu-nbd.texi +++ b/qemu-nbd.texi @@ -45,6 +45,8 @@ Export QEMU disk image using NBD protocol. disconnect the specified device @item -e, --shared=@var{num} device can be shared by @var{num} clients (default @samp{1}) +@item -f, --format=@var{fmt} + force block driver for format @var{fmt} instead of auto-detecting @item -t, --persistent don't exit on the last connection @item -v, --verbose