From nobody Mon Feb 9 21:36:59 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1546697029477908.8595015412187; Sat, 5 Jan 2019 06:03:49 -0800 (PST) Received: from localhost ([127.0.0.1]:47127 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gfmYB-0005XL-AK for importer@patchew.org; Sat, 05 Jan 2019 09:03:43 -0500 Received: from eggsout.gnu.org ([209.51.188.92]:56602 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gfmVv-0003ty-Nm for qemu-devel@nongnu.org; Sat, 05 Jan 2019 09:01:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gfmVt-0007oa-QR for qemu-devel@nongnu.org; Sat, 05 Jan 2019 09:01:23 -0500 Received: from mx1.redhat.com ([209.132.183.28]:41532) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gfmVk-0007fD-3o; Sat, 05 Jan 2019 09:01:15 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id ED6DA58E52; Sat, 5 Jan 2019 14:01:10 +0000 (UTC) Received: from blue.redhat.com (ovpn-116-216.phx2.redhat.com [10.3.116.216]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7EAB55C5BB; Sat, 5 Jan 2019 14:01:10 +0000 (UTC) From: Eric Blake To: qemu-devel@nongnu.org Date: Sat, 5 Jan 2019 08:01:00 -0600 Message-Id: <20190105140100.26913-9-eblake@redhat.com> In-Reply-To: <20190105140100.26913-1-eblake@redhat.com> References: <20190105140100.26913-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Sat, 05 Jan 2019 14:01:11 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 8/8] nbd/client: Drop pointless buf variable X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Vladimir Sementsov-Ogievskiy , "Richard W . M . Jones" , "open list:Network Block Dev..." Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" There's no need to read into a temporary buffer (oversized since commit 7d3123e1) followed by a byteswap into a uint64_t to check for a magic number via memcmp(), when the code immediately below demonstrates reading into the uint64_t then byteswapping in place and checking for a magic number via integer math. What's more, having a different error message when the server's first reply byte is 0 is unusual - it's no different from any other wrong magic number, and we already detected short reads. That whole strlen() issue has been present and useless since commit 1d45f8b5 in 2010; perhaps it was leftover debugging (since the correct magic number happens to be ASCII)? Make the error messages more consistent and detailed while touching things. Signed-off-by: Eric Blake Reviewed-by: Richard W.M. Jones Reviewed-by: Vladimir Sementsov-Ogievskiy Message-Id: <20181215135324.152629-9-eblake@redhat.com> --- nbd/nbd-internal.h | 3 ++- nbd/client.c | 22 +++++++--------------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/nbd/nbd-internal.h b/nbd/nbd-internal.h index f38be9ebaaf..82aa221227f 100644 --- a/nbd/nbd-internal.h +++ b/nbd/nbd-internal.h @@ -46,8 +46,9 @@ /* Size of oldstyle negotiation */ #define NBD_OLDSTYLE_NEGOTIATE_SIZE (8 + 8 + 8 + 4 + 124) +#define NBD_INIT_MAGIC 0x4e42444d41474943LL /* ASCII "NBDMAGI= C" */ #define NBD_REQUEST_MAGIC 0x25609513 -#define NBD_OPTS_MAGIC 0x49484156454F5054LL +#define NBD_OPTS_MAGIC 0x49484156454F5054LL /* ASCII "IHAVEOP= T" */ #define NBD_CLIENT_MAGIC 0x0000420281861253LL #define NBD_REP_MAGIC 0x0003e889045565a9LL diff --git a/nbd/client.c b/nbd/client.c index 5a03a844187..f625c207c54 100644 --- a/nbd/client.c +++ b/nbd/client.c @@ -733,7 +733,6 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *= name, QIOChannel **outioc, NBDExportInfo *info, Error **errp) { - char buf[256]; uint64_t magic; int rc; bool zeroes =3D true; @@ -754,27 +753,20 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char= *name, goto fail; } - if (nbd_read(ioc, buf, 8, errp) < 0) { - error_prepend(errp, "Failed to read data: "); + if (nbd_read(ioc, &magic, sizeof(magic), errp) < 0) { + error_prepend(errp, "Failed to read initial magic: "); goto fail; } - - buf[8] =3D '\0'; - if (strlen(buf) =3D=3D 0) { - error_setg(errp, "Server connection closed unexpectedly"); - goto fail; - } - - magic =3D ldq_be_p(buf); + magic =3D be64_to_cpu(magic); trace_nbd_receive_negotiate_magic(magic); - if (memcmp(buf, "NBDMAGIC", 8) !=3D 0) { - error_setg(errp, "Invalid magic received"); + if (magic !=3D NBD_INIT_MAGIC) { + error_setg(errp, "Bad initial magic received: 0x%" PRIx64, magic); goto fail; } if (nbd_read(ioc, &magic, sizeof(magic), errp) < 0) { - error_prepend(errp, "Failed to read magic: "); + error_prepend(errp, "Failed to read server magic: "); goto fail; } magic =3D be64_to_cpu(magic); @@ -913,7 +905,7 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *= name, } info->flags =3D oldflags; } else { - error_setg(errp, "Bad magic received"); + error_setg(errp, "Bad server magic received: 0x%" PRIx64, magic); goto fail; } --=20 2.20.1