cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-block-Call-attention-to-truncation-of-long-NBD-expor.patch

902636
From c8ecaea34f03b8ddda7d2b41b0d6f397469c8959 Mon Sep 17 00:00:00 2001
902636
From: Eric Blake <eblake@redhat.com>
902636
Date: Wed, 10 Jun 2020 18:32:02 -0400
902636
Subject: [PATCH 2/2] block: Call attention to truncation of long NBD exports
902636
902636
RH-Author: Eric Blake <eblake@redhat.com>
902636
Message-id: <20200610183202.3780750-3-eblake@redhat.com>
902636
Patchwork-id: 97495
902636
O-Subject: [RHEL-AV-8.2.1 qemu-kvm PATCH 2/2] block: Call attention to truncation of long NBD exports
902636
Bugzilla: 1845384
902636
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
902636
RH-Acked-by: Max Reitz <mreitz@redhat.com>
902636
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
902636
902636
Commit 93676c88 relaxed our NBD client code to request export names up
902636
to the NBD protocol maximum of 4096 bytes without NUL terminator, even
902636
though the block layer can't store anything longer than 4096 bytes
902636
including NUL terminator for display to the user.  Since this means
902636
there are some export names where we have to truncate things, we can
902636
at least try to make the truncation a bit more obvious for the user.
902636
Note that in spite of the truncated display name, we can still
902636
communicate with an NBD server using such a long export name; this was
902636
deemed nicer than refusing to even connect to such a server (since the
902636
server may not be under our control, and since determining our actual
902636
length limits gets tricky when nbd://host:port/export and
902636
nbd+unix:///export?socket=/path are themselves variable-length
902636
expansions beyond the export name but count towards the block layer
902636
name length).
902636
902636
Reported-by: Xueqiang Wei <xuwei@redhat.com>
902636
Fixes: https://bugzilla.redhat.com/1843684
902636
Signed-off-by: Eric Blake <eblake@redhat.com>
902636
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
902636
Message-Id: <20200610163741.3745251-3-eblake@redhat.com>
902636
(cherry picked from commit 5c86bdf1208916ece0b87e1151c9b48ee54faa3e)
902636
Signed-off-by: Eric Blake <eblake@redhat.com>
902636
Signed-off-by: Eduardo Lima (Etrunko) <etrunko@redhat.com>
902636
---
902636
 block.c     |  7 +++++--
902636
 block/nbd.c | 21 +++++++++++++--------
902636
 2 files changed, 18 insertions(+), 10 deletions(-)
902636
902636
diff --git a/block.c b/block.c
902636
index 12c8941879..57740d312e 100644
902636
--- a/block.c
902636
+++ b/block.c
902636
@@ -6683,8 +6683,11 @@ void bdrv_refresh_filename(BlockDriverState *bs)
902636
         pstrcpy(bs->filename, sizeof(bs->filename), bs->exact_filename);
902636
     } else {
902636
         QString *json = qobject_to_json(QOBJECT(bs->full_open_options));
902636
-        snprintf(bs->filename, sizeof(bs->filename), "json:%s",
902636
-                 qstring_get_str(json));
902636
+        if (snprintf(bs->filename, sizeof(bs->filename), "json:%s",
902636
+                     qstring_get_str(json)) >= sizeof(bs->filename)) {
902636
+            /* Give user a hint if we truncated things. */
902636
+            strcpy(bs->filename + sizeof(bs->filename) - 4, "...");
902636
+        }
902636
         qobject_unref(json);
902636
     }
902636
 }
902636
diff --git a/block/nbd.c b/block/nbd.c
902636
index 927915d93d..5bb154017d 100644
902636
--- a/block/nbd.c
902636
+++ b/block/nbd.c
902636
@@ -1978,6 +1978,7 @@ static void nbd_refresh_filename(BlockDriverState *bs)
902636
 {
902636
     BDRVNBDState *s = bs->opaque;
902636
     const char *host = NULL, *port = NULL, *path = NULL;
902636
+    size_t len = 0;
902636
 
902636
     if (s->saddr->type == SOCKET_ADDRESS_TYPE_INET) {
902636
         const InetSocketAddress *inet = &s->saddr->u.inet;
902636
@@ -1990,17 +1991,21 @@ static void nbd_refresh_filename(BlockDriverState *bs)
902636
     } /* else can't represent as pseudo-filename */
902636
 
902636
     if (path && s->export) {
902636
-        snprintf(bs->exact_filename, sizeof(bs->exact_filename),
902636
-                 "nbd+unix:///%s?socket=%s", s->export, path);
902636
+        len = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
902636
+                       "nbd+unix:///%s?socket=%s", s->export, path);
902636
     } else if (path && !s->export) {
902636
-        snprintf(bs->exact_filename, sizeof(bs->exact_filename),
902636
-                 "nbd+unix://?socket=%s", path);
902636
+        len = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
902636
+                       "nbd+unix://?socket=%s", path);
902636
     } else if (host && s->export) {
902636
-        snprintf(bs->exact_filename, sizeof(bs->exact_filename),
902636
-                 "nbd://%s:%s/%s", host, port, s->export);
902636
+        len = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
902636
+                       "nbd://%s:%s/%s", host, port, s->export);
902636
     } else if (host && !s->export) {
902636
-        snprintf(bs->exact_filename, sizeof(bs->exact_filename),
902636
-                 "nbd://%s:%s", host, port);
902636
+        len = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
902636
+                       "nbd://%s:%s", host, port);
902636
+    }
902636
+    if (len > sizeof(bs->exact_filename)) {
902636
+        /* Name is too long to represent exactly, so leave it empty. */
902636
+        bs->exact_filename[0] = '\0';
902636
     }
902636
 }
902636
 
902636
-- 
902636
2.27.0
902636