From patchwork Fri Dec 23 15:26:21 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 133091 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id D94CFB7074 for ; Sat, 24 Dec 2011 03:05:53 +1100 (EST) Received: from localhost ([::1]:47529 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Re7d0-0002cq-JB for incoming@patchwork.ozlabs.org; Fri, 23 Dec 2011 11:05:50 -0500 Received: from eggs.gnu.org ([140.186.70.92]:41350) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Re73j-0003W7-Nz for qemu-devel@nongnu.org; Fri, 23 Dec 2011 10:29:24 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Re73i-0006kc-GJ for qemu-devel@nongnu.org; Fri, 23 Dec 2011 10:29:23 -0500 Received: from mail-gx0-f173.google.com ([209.85.161.173]:54035) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Re73i-0006gC-9M for qemu-devel@nongnu.org; Fri, 23 Dec 2011 10:29:22 -0500 Received: by mail-gx0-f173.google.com with SMTP id k1so8050701ggn.4 for ; Fri, 23 Dec 2011 07:29:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=sender:from:to:subject:date:message-id:x-mailer:in-reply-to :references; bh=ZZq06yr+OsDyY1sPhUQh9mafpVW9QXEdCbLwcYICAVo=; b=CsYIFqlaT5ZZr4ZIuNNMGiHTem38+BrpqsCXqzPeivVs5sb8b6sZ2Cv9DQEFqU8mPY xVCS6G0pGpeWpYCPBraNrU1bj5s9vxh+RKF2s9qfHirJTiStmaJ7k8C/lW5e19sRppDr xrrv4rMDAAWVHbQ0WL0PI2HuCycKKBf6T6ct0= Received: by 10.50.77.129 with SMTP id s1mr14185137igw.25.1324654161864; Fri, 23 Dec 2011 07:29:21 -0800 (PST) Received: from localhost.localdomain (93-34-178-147.ip50.fastwebnet.it. [93.34.178.147]) by mx.google.com with ESMTPS id aq5sm42055557igc.5.2011.12.23.07.29.17 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 23 Dec 2011 07:29:20 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 23 Dec 2011 16:26:21 +0100 Message-Id: <1324653990-20074-18-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.7.7.1 In-Reply-To: <1324653990-20074-1-git-send-email-pbonzini@redhat.com> References: <1324653990-20074-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 209.85.161.173 Subject: [Qemu-devel] [PATCH 17/26] qemu-nbd: introduce nbd_do_receive_request 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 Group the receiving of a response and the associated data into a new function. Signed-off-by: Paolo Bonzini --- nbd.c | 68 ++++++++++++++++++++++++++++++++++++++++++++-------------------- 1 files changed, 47 insertions(+), 21 deletions(-) diff --git a/nbd.c b/nbd.c index 053ad8d..964a732 100644 --- a/nbd.c +++ b/nbd.c @@ -611,6 +611,47 @@ static int nbd_do_send_reply(int csock, struct nbd_reply *reply, return rc; } +static int nbd_do_receive_request(int csock, struct nbd_request *request, + uint8_t *data) +{ + int rc; + + if (nbd_receive_request(csock, request) == -1) { + rc = -EIO; + goto out; + } + + if (request->len > NBD_BUFFER_SIZE) { + LOG("len (%u) is larger than max len (%u)", + request->len, NBD_BUFFER_SIZE); + rc = -EINVAL; + goto out; + } + + if ((request->from + request->len) < request->from) { + LOG("integer overflow detected! " + "you're probably being attacked"); + rc = -EINVAL; + goto out; + } + + TRACE("Decoding type"); + + if ((request->type & NBD_CMD_MASK_COMMAND) == NBD_CMD_WRITE) { + TRACE("Reading %u byte(s)", request->len); + + if (read_sync(csock, data, request->len) != request->len) { + LOG("reading from socket failed"); + rc = -EIO; + goto out; + } + } + rc = 0; + +out: + return rc; +} + int nbd_trip(BlockDriverState *bs, int csock, off_t size, uint64_t dev_offset, uint32_t nbdflags, uint8_t *data) @@ -621,22 +662,17 @@ int nbd_trip(BlockDriverState *bs, int csock, off_t size, TRACE("Reading request."); - if (nbd_receive_request(csock, &request) == -1) + ret = nbd_do_receive_request(csock, &request, data); + if (ret == -EIO) { return -1; + } reply.handle = request.handle; reply.error = 0; - if (request.len > NBD_BUFFER_SIZE) { - LOG("len (%u) is larger than max len (%u)", - request.len, NBD_BUFFER_SIZE); - goto invalid_request; - } - - if ((request.from + request.len) < request.from) { - LOG("integer overflow detected! " - "you're probably being attacked"); - goto invalid_request; + if (ret < 0) { + reply.error = -ret; + goto error_reply; } if ((request.from + request.len) > size) { @@ -647,8 +683,6 @@ int nbd_trip(BlockDriverState *bs, int csock, off_t size, goto invalid_request; } - TRACE("Decoding type"); - switch (request.type & NBD_CMD_MASK_COMMAND) { case NBD_CMD_READ: TRACE("Request type is READ"); @@ -668,14 +702,6 @@ int nbd_trip(BlockDriverState *bs, int csock, off_t size, case NBD_CMD_WRITE: TRACE("Request type is WRITE"); - TRACE("Reading %u byte(s)", request.len); - - if (read_sync(csock, data, request.len) != request.len) { - LOG("reading from socket failed"); - errno = EINVAL; - return -1; - } - if (nbdflags & NBD_FLAG_READ_ONLY) { TRACE("Server is read-only, return error"); reply.error = EROFS;