Blame SOURCES/kvm-nbd-server-Hoist-length-check-to-qmp_nbd_server_add.patch

7711c0
From 0591484b860fb6e25f86eceec0ba2e8b8d1673c4 Mon Sep 17 00:00:00 2001
7711c0
From: John Snow <jsnow@redhat.com>
7711c0
Date: Wed, 27 Mar 2019 17:22:39 +0100
7711c0
Subject: [PATCH 101/163] nbd/server: Hoist length check to qmp_nbd_server_add
7711c0
7711c0
RH-Author: John Snow <jsnow@redhat.com>
7711c0
Message-id: <20190327172308.31077-27-jsnow@redhat.com>
7711c0
Patchwork-id: 85201
7711c0
O-Subject: [RHEL-7.7 qemu-kvm-rhev PATCH 26/55] nbd/server: Hoist length check to qmp_nbd_server_add
7711c0
Bugzilla: 1691009
7711c0
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
7711c0
RH-Acked-by: Max Reitz <mreitz@redhat.com>
7711c0
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
7711c0
7711c0
From: Eric Blake <eblake@redhat.com>
7711c0
7711c0
We only had two callers to nbd_export_new; qemu-nbd.c always
7711c0
passed a valid offset/length pair (because it already checked
7711c0
the file length, to ensure that offset was in bounds), while
7711c0
blockdev-nbd.c always passed 0/-1.  Then nbd_export_new reduces
7711c0
the size to a multiple of BDRV_SECTOR_SIZE (can only happen
7711c0
when offset is not sector-aligned, since bdrv_getlength()
7711c0
currently rounds up) (someday, it would be nice to have
7711c0
byte-accurate lengths - but not today).
7711c0
7711c0
However, I'm finding it easier to work with the code if we are
7711c0
consistent on having both callers pass in a valid length, and
7711c0
just assert that things are sane in nbd_export_new, meaning
7711c0
that no negative values were passed, and that offset+size does
7711c0
not exceed 63 bits (as that really is a fundamental limit to
7711c0
later operations, whether we use off_t or uint64_t).
7711c0
7711c0
Signed-off-by: Eric Blake <eblake@redhat.com>
7711c0
Message-Id: <20190117193658.16413-6-eblake@redhat.com>
7711c0
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
7711c0
(cherry picked from commit 7596bbb390838359e4789996f349bda0cad56b0e)
7711c0
Signed-off-by: John Snow <jsnow@redhat.com>
7711c0
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
7711c0
---
7711c0
 blockdev-nbd.c | 10 +++++++++-
7711c0
 nbd/server.c   | 10 +++-------
7711c0
 2 files changed, 12 insertions(+), 8 deletions(-)
7711c0
7711c0
diff --git a/blockdev-nbd.c b/blockdev-nbd.c
7711c0
index c76d541..d73ac1b 100644
7711c0
--- a/blockdev-nbd.c
7711c0
+++ b/blockdev-nbd.c
7711c0
@@ -146,6 +146,7 @@ void qmp_nbd_server_add(const char *device, bool has_name, const char *name,
7711c0
     BlockDriverState *bs = NULL;
7711c0
     BlockBackend *on_eject_blk;
7711c0
     NBDExport *exp;
7711c0
+    int64_t len;
7711c0
 
7711c0
     if (!nbd_server) {
7711c0
         error_setg(errp, "NBD server not running");
7711c0
@@ -168,6 +169,13 @@ void qmp_nbd_server_add(const char *device, bool has_name, const char *name,
7711c0
         return;
7711c0
     }
7711c0
 
7711c0
+    len = bdrv_getlength(bs);
7711c0
+    if (len < 0) {
7711c0
+        error_setg_errno(errp, -len,
7711c0
+                         "Failed to determine the NBD export's length");
7711c0
+        return;
7711c0
+    }
7711c0
+
7711c0
     if (!has_writable) {
7711c0
         writable = false;
7711c0
     }
7711c0
@@ -175,7 +183,7 @@ void qmp_nbd_server_add(const char *device, bool has_name, const char *name,
7711c0
         writable = false;
7711c0
     }
7711c0
 
7711c0
-    exp = nbd_export_new(bs, 0, -1, name, NULL, bitmap,
7711c0
+    exp = nbd_export_new(bs, 0, len, name, NULL, bitmap,
7711c0
                          writable ? 0 : NBD_FLAG_READ_ONLY,
7711c0
                          NULL, false, on_eject_blk, errp);
7711c0
     if (!exp) {
7711c0
diff --git a/nbd/server.c b/nbd/server.c
7711c0
index 6b13601..51ee809 100644
7711c0
--- a/nbd/server.c
7711c0
+++ b/nbd/server.c
7711c0
@@ -1495,17 +1495,13 @@ NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset, off_t size,
7711c0
     exp->refcount = 1;
7711c0
     QTAILQ_INIT(&exp->clients);
7711c0
     exp->blk = blk;
7711c0
+    assert(dev_offset >= 0 && dev_offset <= INT64_MAX);
7711c0
     exp->dev_offset = dev_offset;
7711c0
     exp->name = g_strdup(name);
7711c0
     exp->description = g_strdup(description);
7711c0
     exp->nbdflags = nbdflags;
7711c0
-    exp->size = size < 0 ? blk_getlength(blk) : size;
7711c0
-    if (exp->size < 0) {
7711c0
-        error_setg_errno(errp, -exp->size,
7711c0
-                         "Failed to determine the NBD export's length");
7711c0
-        goto fail;
7711c0
-    }
7711c0
-    exp->size -= exp->size % BDRV_SECTOR_SIZE;
7711c0
+    assert(size >= 0 && size <= INT64_MAX - dev_offset);
7711c0
+    exp->size = QEMU_ALIGN_DOWN(size, BDRV_SECTOR_SIZE);
7711c0
 
7711c0
     if (bitmap) {
7711c0
         BdrvDirtyBitmap *bm = NULL;
7711c0
-- 
7711c0
1.8.3.1
7711c0