From patchwork Tue Jun 19 01:42:42 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 931343 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=none (p=none dis=none) header.from=linux.vnet.ibm.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="RPk6Wxue"; dkim-atps=neutral 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 418tQj2DT4z9s0W for ; Tue, 19 Jun 2018 13:16:13 +1000 (AEST) Received: from localhost ([::1]:38953 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fV77q-0006Q9-VB for incoming@patchwork.ozlabs.org; Mon, 18 Jun 2018 23:16:10 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47005) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fV5ja-00034P-0G for qemu-devel@nongnu.org; Mon, 18 Jun 2018 21:47:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fV5jW-0000pZ-6j for qemu-devel@nongnu.org; Mon, 18 Jun 2018 21:47:02 -0400 Received: from mail-ot0-x231.google.com ([2607:f8b0:4003:c0f::231]:43689) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fV5jW-0000pU-0T; Mon, 18 Jun 2018 21:46:58 -0400 Received: by mail-ot0-x231.google.com with SMTP id i19-v6so20745642otk.10; Mon, 18 Jun 2018 18:46:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=ZpqoWvJ2rwctORfvNpYG0m1cmeNh8t2Re4bsiKGiHs8=; b=RPk6WxuecGCyqk/sBXhFZK+GSBQk6fAQM1KA5wYncUPArdg9dn2Sc7nDxHyCNvFSxo VcgCkDmsPuPM/73ivItuNOZjg4tEvJwQRfaSz2C872qpCgBiAfAfbedCBaGUxCcINMeB 8r4RUr+M6VVmsvaOADBJB3JIP5IX13l2jqAWQvDR7lDPYkkc+yxieiEaqUePecymynR3 Dmvwx5KVtC7S9p3AJVFqOIrPBOMgmY/NiaLSM7Exzx2zwd4HSANuAboxgMOWHnX/xA5i JWKowShdLKLjucRL3aN/ycHyyWI4Fz2Mij0rlIe8nkxKp+pmfcGog5EWS2yKnUUMcbWE sp/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=ZpqoWvJ2rwctORfvNpYG0m1cmeNh8t2Re4bsiKGiHs8=; b=UcXVPDikamxmpwfSRVBWWOC3LS8qMBhDCQi9fNlxwVyxy5J/xJjRs8DH5ZTZ3qsVUQ +/lGMnmS9AFBdwF90+8UQ8zjv+PIYjs8Z/zhivjJSl8pE2YW5dpmuP+eEtRaOGwzNsOw YlEzrdSo6qxqarCo/lfGKCVZLYQQJ7in9gr42TvHuwponjWsfsWH2viEek4izsi/9CnZ t9AhkO6V5hy+9lRdcP2lJBi6t0xBsA7pxIGWCAS4FbhUpTgiDhUDpFUsd9zCdzUg3iAc ZJNXwfhG4/zW/hd8wPSFpTYky0c5Pe5H9jDI/uytnNdW0ASsE5essmgUvmTRQFCpdmmo fmBQ== X-Gm-Message-State: APt69E06a5IAE+Roq5ifmUQf4n1Twm3Gs9WV8tf6h2Snk+cb7hWmL5gH aDEv3fnNZGSXdioBvBfV9gVDxx1zYzA= X-Google-Smtp-Source: ADUXVKJQTp96YRfQ4bsziEyPfBM1CzhS7qurp2tmJBs2QAhedhPj75OZpGieO3W+OAXpUiSaN0Ktdg== X-Received: by 2002:a9d:251d:: with SMTP id k29-v6mr9716393otb.149.1529372816829; Mon, 18 Jun 2018 18:46:56 -0700 (PDT) Received: from localhost (76-251-165-188.lightspeed.austtx.sbcglobal.net. [76.251.165.188]) by smtp.gmail.com with ESMTPSA id c2-v6sm7233001oia.53.2018.06.18.18.46.55 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 18 Jun 2018 18:46:55 -0700 (PDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Mon, 18 Jun 2018 20:42:42 -0500 Message-Id: <20180619014319.28272-77-mdroth@linux.vnet.ibm.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180619014319.28272-1-mdroth@linux.vnet.ibm.com> References: <20180619014319.28272-1-mdroth@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4003:c0f::231 Subject: [Qemu-devel] [PATCH 076/113] raw: Check byte range uniformly 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: Fam Zheng , qemu-stable@nongnu.org, Stefan Hajnoczi Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" From: Fam Zheng We don't verify the request range against s->size in the I/O callbacks except for raw_co_pwritev. This is inconsistent (especially for raw_co_pwrite_zeroes and raw_co_pdiscard), so fix them, in the meanwhile make the helper reusable by the coming new callbacks. Note that in most cases the block layer already verifies the request byte range against our reported image length, before invoking the driver callbacks. The exception is during image creating, after blk_set_allow_write_beyond_eof(blk, true) is called. But in that case, the requests are not directly from the user or guest. So there is no visible behavior change in adding the check code. The int64_t -> uint64_t inconsistency, as shown by the type casting, is pre-existing due to the interface. Reviewed-by: Stefan Hajnoczi Reviewed-by: Eric Blake Signed-off-by: Fam Zheng Message-id: 20180601092648.24614-3-famz@redhat.com Signed-off-by: Stefan Hajnoczi (cherry picked from commit 384455385248762e74a080978f18f0c8f74757fe) Signed-off-by: Michael Roth --- block/raw-format.c | 64 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/block/raw-format.c b/block/raw-format.c index ab552c0954..c77290b93f 100644 --- a/block/raw-format.c +++ b/block/raw-format.c @@ -167,16 +167,37 @@ static void raw_reopen_abort(BDRVReopenState *state) state->opaque = NULL; } +/* Check and adjust the offset, against 'offset' and 'size' options. */ +static inline int raw_adjust_offset(BlockDriverState *bs, uint64_t *offset, + uint64_t bytes, bool is_write) +{ + BDRVRawState *s = bs->opaque; + + if (s->has_size && (*offset > s->size || bytes > (s->size - *offset))) { + /* There's not enough space for the write, or the read request is + * out-of-range. Don't read/write anything to prevent leaking out of + * the size specified in options. */ + return is_write ? -ENOSPC : -EINVAL;; + } + + if (*offset > INT64_MAX - s->offset) { + return -EINVAL; + } + *offset += s->offset; + + return 0; +} + static int coroutine_fn raw_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) { - BDRVRawState *s = bs->opaque; + int ret; - if (offset > UINT64_MAX - s->offset) { - return -EINVAL; + ret = raw_adjust_offset(bs, &offset, bytes, false); + if (ret) { + return ret; } - offset += s->offset; BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags); @@ -186,23 +207,11 @@ static int coroutine_fn raw_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) { - BDRVRawState *s = bs->opaque; void *buf = NULL; BlockDriver *drv; QEMUIOVector local_qiov; int ret; - if (s->has_size && (offset > s->size || bytes > (s->size - offset))) { - /* There's not enough space for the data. Don't write anything and just - * fail to prevent leaking out of the size specified in options. */ - return -ENOSPC; - } - - if (offset > UINT64_MAX - s->offset) { - ret = -EINVAL; - goto fail; - } - if (bs->probed && offset < BLOCK_PROBE_BUF_SIZE && bytes) { /* Handling partial writes would be a pain - so we just * require that guests have 512-byte request alignment if @@ -237,7 +246,10 @@ static int coroutine_fn raw_co_pwritev(BlockDriverState *bs, uint64_t offset, qiov = &local_qiov; } - offset += s->offset; + ret = raw_adjust_offset(bs, &offset, bytes, true); + if (ret) { + goto fail; + } BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO); ret = bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags); @@ -267,22 +279,24 @@ static int coroutine_fn raw_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int bytes, BdrvRequestFlags flags) { - BDRVRawState *s = bs->opaque; - if (offset > UINT64_MAX - s->offset) { - return -EINVAL; + int ret; + + ret = raw_adjust_offset(bs, (uint64_t *)&offset, bytes, true); + if (ret) { + return ret; } - offset += s->offset; return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags); } static int coroutine_fn raw_co_pdiscard(BlockDriverState *bs, int64_t offset, int bytes) { - BDRVRawState *s = bs->opaque; - if (offset > UINT64_MAX - s->offset) { - return -EINVAL; + int ret; + + ret = raw_adjust_offset(bs, (uint64_t *)&offset, bytes, true); + if (ret) { + return ret; } - offset += s->offset; return bdrv_co_pdiscard(bs->file->bs, offset, bytes); }