From patchwork Tue Mar 13 17:13:39 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 885329 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 40124K1Rxmz9sSm for ; Wed, 14 Mar 2018 04:32:49 +1100 (AEDT) Received: from localhost ([::1]:41495 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evnn5-0006Wn-1k for incoming@patchwork.ozlabs.org; Tue, 13 Mar 2018 13:32:47 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47321) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evnUw-0007Of-1T for qemu-devel@nongnu.org; Tue, 13 Mar 2018 13:14:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1evnUu-0000xG-MJ for qemu-devel@nongnu.org; Tue, 13 Mar 2018 13:14:01 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:54270 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1evnUo-0000qt-NS; Tue, 13 Mar 2018 13:13:54 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5DB537CBBA; Tue, 13 Mar 2018 17:13:54 +0000 (UTC) Received: from red.redhat.com (ovpn-121-135.rdu2.redhat.com [10.10.121.135]) by smtp.corp.redhat.com (Postfix) with ESMTP id ECB0F202322B; Tue, 13 Mar 2018 17:13:53 +0000 (UTC) From: Eric Blake To: qemu-devel@nongnu.org Date: Tue, 13 Mar 2018 12:13:39 -0500 Message-Id: <20180313171345.659672-12-eblake@redhat.com> In-Reply-To: <20180313171345.659672-1-eblake@redhat.com> References: <20180313171345.659672-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Tue, 13 Mar 2018 17:13:54 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Tue, 13 Mar 2018 17:13:54 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'eblake@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PULL 11/17] nbd/server: add nbd_read_opt_name helper 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: Paolo Bonzini , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" From: Vladimir Sementsov-Ogievskiy Add helper to read name in format: uint32 len (<= NBD_MAX_NAME_SIZE) len bytes string (not 0-terminated) The helper will be reused in following patch. Signed-off-by: Vladimir Sementsov-Ogievskiy Message-Id: <20180312152126.286890-3-vsementsov@virtuozzo.com> Reviewed-by: Eric Blake [eblake: grammar fixes, actually check error] Signed-off-by: Eric Blake --- nbd/server.c | 53 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/nbd/server.c b/nbd/server.c index 01ea97afe98..280bdbb1040 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -273,6 +273,48 @@ static int nbd_opt_read(NBDClient *client, void *buffer, size_t size, return qio_channel_read_all(client->ioc, buffer, size, errp) < 0 ? -EIO : 1; } +/* nbd_opt_read_name + * + * Read a string with the format: + * uint32_t len (<= NBD_MAX_NAME_SIZE) + * len bytes string (not 0-terminated) + * + * @name should be enough to store NBD_MAX_NAME_SIZE+1. + * If @length is non-null, it will be set to the actual string length. + * + * Return -errno on I/O error, 0 if option was completely handled by + * sending a reply about inconsistent lengths, or 1 on success. + */ +static int nbd_opt_read_name(NBDClient *client, char *name, uint32_t *length, + Error **errp) +{ + int ret; + uint32_t len; + + ret = nbd_opt_read(client, &len, sizeof(len), errp); + if (ret <= 0) { + return ret; + } + cpu_to_be32s(&len); + + if (len > NBD_MAX_NAME_SIZE) { + return nbd_opt_invalid(client, errp, + "Invalid name length: %" PRIu32, len); + } + + ret = nbd_opt_read(client, name, len, errp); + if (ret <= 0) { + return ret; + } + name[len] = '\0'; + + if (length) { + *length = len; + } + + return 1; +} + /* Send a single NBD_REP_SERVER reply to NBD_OPT_LIST, including payload. * Return -errno on error, 0 on success. */ static int nbd_negotiate_send_rep_list(NBDClient *client, NBDExport *exp, @@ -455,19 +497,10 @@ static int nbd_negotiate_handle_info(NBDClient *client, uint16_t myflags, 2 bytes: N, number of requests (can be 0) N * 2 bytes: N requests */ - rc = nbd_opt_read(client, &namelen, sizeof(namelen), errp); + rc = nbd_opt_read_name(client, name, &namelen, errp); if (rc <= 0) { return rc; } - be32_to_cpus(&namelen); - if (namelen >= sizeof(name)) { - return nbd_opt_invalid(client, errp, "name too long for qemu"); - } - rc = nbd_opt_read(client, name, namelen, errp); - if (rc <= 0) { - return rc; - } - name[namelen] = '\0'; trace_nbd_negotiate_handle_export_name_request(name); rc = nbd_opt_read(client, &requests, sizeof(requests), errp);