From nobody Sun Apr 13 18:06:55 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1614964766; cv=none; d=zohomail.com; s=zohoarc; b=NOJjClpdlRL01YPEyGMGqh5bC4pvyrEmttYuE7u679yDgCpx51fnsvEvfslXjtv2/yCqIDUVCayweckKDCQSib2FIt8jybgLnKA/pNrFIHh/lizz6sYj4zsKtTorVgq0kq38IZuRJ9ZUgsAS62vcbapoSvEG+m4ViE9QzvwyNiY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614964766; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=mNzsHHIx58+QMBheKHkUuCRnL0/BzmGRxjdvJTaEdQU=; b=NQ11l+KGFJdWtkK5mU4S4WfmmkPwnBYaIf99I4ckHq1yCI2N5t+031dAsjhEQnVC1cBzc0EcCWsh8oOmblDiVYWhl9W6ZbEg/ZB5VsAF9l8YblZk4bzz1xNJZGMLs0wtfXMn5Uh1g+JN7GOE60JzV2Igi4rGH+FrSXE4OsYyRhA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1614964766428971.4229880294166; Fri, 5 Mar 2021 09:19:26 -0800 (PST) Received: from localhost ([::1]:48166 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lIE6n-00028m-9I for importer@patchew.org; Fri, 05 Mar 2021 12:19:25 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:32954) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lIDkP-00070w-At for qemu-devel@nongnu.org; Fri, 05 Mar 2021 11:56:17 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:32536) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lIDk5-0007D7-HK for qemu-devel@nongnu.org; Fri, 05 Mar 2021 11:56:17 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-341-q9fAAlETNOat5CHMZaPVKA-1; Fri, 05 Mar 2021 11:55:54 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7FCF4E4961; Fri, 5 Mar 2021 16:55:36 +0000 (UTC) Received: from merkur.redhat.com (ovpn-112-36.phx2.redhat.com [10.3.112.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 740EF60C43; Fri, 5 Mar 2021 16:55:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614963356; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mNzsHHIx58+QMBheKHkUuCRnL0/BzmGRxjdvJTaEdQU=; b=Ro8yQuyonebU6Q2IXkEMdHzQvMvirglvQ2XA251pWrDN+XOBxqhoO2Yqxp+S+u1j37F9nU qnJvCDuvF8FWESqaLi0H7L5/kUNQi/1kMab+OuEApJ7WpJzfAijhhsFh+4JftVV7hEdvzj 6gSNQUmOKo7Jk9CoCeVcNshf26imn3A= X-MC-Unique: q9fAAlETNOat5CHMZaPVKA-1 From: Kevin Wolf To: qemu-block@nongnu.org Subject: [PULL 21/31] block/export: port virtio-blk discard/write zeroes input validation Date: Fri, 5 Mar 2021 17:54:44 +0100 Message-Id: <20210305165454.356840-22-kwolf@redhat.com> In-Reply-To: <20210305165454.356840-1-kwolf@redhat.com> References: <20210305165454.356840-1-kwolf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=kwolf@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.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; Received-SPF: pass client-ip=216.205.24.124; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, peter.maydell@linaro.org, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" From: Stefan Hajnoczi Validate discard/write zeroes the same way we do for virtio-blk. Some of these checks are mandated by the VIRTIO specification, others are internal to QEMU. Signed-off-by: Stefan Hajnoczi Message-Id: <20210223144653.811468-11-stefanha@redhat.com> Signed-off-by: Kevin Wolf --- block/export/vhost-user-blk-server.c | 116 +++++++++++++++++++++------ 1 file changed, 93 insertions(+), 23 deletions(-) diff --git a/block/export/vhost-user-blk-server.c b/block/export/vhost-user= -blk-server.c index f74796241c..04044228d4 100644 --- a/block/export/vhost-user-blk-server.c +++ b/block/export/vhost-user-blk-server.c @@ -29,6 +29,8 @@ =20 enum { VHOST_USER_BLK_NUM_QUEUES_DEFAULT =3D 1, + VHOST_USER_BLK_MAX_DISCARD_SECTORS =3D 32768, + VHOST_USER_BLK_MAX_WRITE_ZEROES_SECTORS =3D 32768, }; struct virtio_blk_inhdr { unsigned char status; @@ -65,30 +67,102 @@ static void vu_blk_req_complete(VuBlkReq *req) free(req); } =20 +static bool vu_blk_sect_range_ok(VuBlkExport *vexp, uint64_t sector, + size_t size) +{ + uint64_t nb_sectors =3D size >> BDRV_SECTOR_BITS; + uint64_t total_sectors; + + if (nb_sectors > BDRV_REQUEST_MAX_SECTORS) { + return false; + } + if ((sector << VIRTIO_BLK_SECTOR_BITS) % vexp->blk_size) { + return false; + } + blk_get_geometry(vexp->export.blk, &total_sectors); + if (sector > total_sectors || nb_sectors > total_sectors - sector) { + return false; + } + return true; +} + static int coroutine_fn -vu_blk_discard_write_zeroes(BlockBackend *blk, struct iovec *iov, +vu_blk_discard_write_zeroes(VuBlkExport *vexp, struct iovec *iov, uint32_t iovcnt, uint32_t type) { + BlockBackend *blk =3D vexp->export.blk; struct virtio_blk_discard_write_zeroes desc; - ssize_t size =3D iov_to_buf(iov, iovcnt, 0, &desc, sizeof(desc)); + ssize_t size; + uint64_t sector; + uint32_t num_sectors; + uint32_t max_sectors; + uint32_t flags; + int bytes; + + /* Only one desc is currently supported */ + if (unlikely(iov_size(iov, iovcnt) > sizeof(desc))) { + return VIRTIO_BLK_S_UNSUPP; + } + + size =3D iov_to_buf(iov, iovcnt, 0, &desc, sizeof(desc)); if (unlikely(size !=3D sizeof(desc))) { - error_report("Invalid size %zd, expect %zu", size, sizeof(desc)); - return -EINVAL; + error_report("Invalid size %zd, expected %zu", size, sizeof(desc)); + return VIRTIO_BLK_S_IOERR; } =20 - uint64_t range[2] =3D { le64_to_cpu(desc.sector) << 9, - le32_to_cpu(desc.num_sectors) << 9 }; - if (type =3D=3D VIRTIO_BLK_T_DISCARD) { - if (blk_co_pdiscard(blk, range[0], range[1]) =3D=3D 0) { - return 0; + sector =3D le64_to_cpu(desc.sector); + num_sectors =3D le32_to_cpu(desc.num_sectors); + flags =3D le32_to_cpu(desc.flags); + max_sectors =3D (type =3D=3D VIRTIO_BLK_T_WRITE_ZEROES) ? + VHOST_USER_BLK_MAX_WRITE_ZEROES_SECTORS : + VHOST_USER_BLK_MAX_DISCARD_SECTORS; + + /* This check ensures that 'bytes' fits in an int */ + if (unlikely(num_sectors > max_sectors)) { + return VIRTIO_BLK_S_IOERR; + } + + bytes =3D num_sectors << VIRTIO_BLK_SECTOR_BITS; + + if (unlikely(!vu_blk_sect_range_ok(vexp, sector, bytes))) { + return VIRTIO_BLK_S_IOERR; + } + + /* + * The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for disc= ard + * and write zeroes commands if any unknown flag is set. + */ + if (unlikely(flags & ~VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)) { + return VIRTIO_BLK_S_UNSUPP; + } + + if (type =3D=3D VIRTIO_BLK_T_WRITE_ZEROES) { + int blk_flags =3D 0; + + if (flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP) { + blk_flags |=3D BDRV_REQ_MAY_UNMAP; + } + + if (blk_co_pwrite_zeroes(blk, sector << VIRTIO_BLK_SECTOR_BITS, + bytes, blk_flags) =3D=3D 0) { + return VIRTIO_BLK_S_OK; } - } else if (type =3D=3D VIRTIO_BLK_T_WRITE_ZEROES) { - if (blk_co_pwrite_zeroes(blk, range[0], range[1], 0) =3D=3D 0) { - return 0; + } else if (type =3D=3D VIRTIO_BLK_T_DISCARD) { + /* + * The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for + * discard commands if the unmap flag is set. + */ + if (unlikely(flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)) { + return VIRTIO_BLK_S_UNSUPP; + } + + if (blk_co_pdiscard(blk, sector << VIRTIO_BLK_SECTOR_BITS, + bytes) =3D=3D 0) { + return VIRTIO_BLK_S_OK; } } =20 - return -EINVAL; + return VIRTIO_BLK_S_IOERR; } =20 static void coroutine_fn vu_blk_virtio_process_req(void *opaque) @@ -177,19 +251,13 @@ static void coroutine_fn vu_blk_virtio_process_req(vo= id *opaque) } case VIRTIO_BLK_T_DISCARD: case VIRTIO_BLK_T_WRITE_ZEROES: { - int rc; - if (!vexp->writable) { req->in->status =3D VIRTIO_BLK_S_IOERR; break; } =20 - rc =3D vu_blk_discard_write_zeroes(blk, &elem->out_sg[1], out_num,= type); - if (rc =3D=3D 0) { - req->in->status =3D VIRTIO_BLK_S_OK; - } else { - req->in->status =3D VIRTIO_BLK_S_IOERR; - } + req->in->status =3D vu_blk_discard_write_zeroes(vexp, out_iov, out= _num, + type); break; } default: @@ -362,11 +430,13 @@ vu_blk_initialize_config(BlockDriverState *bs, config->min_io_size =3D cpu_to_le16(1); config->opt_io_size =3D cpu_to_le32(1); config->num_queues =3D cpu_to_le16(num_queues); - config->max_discard_sectors =3D cpu_to_le32(32768); + config->max_discard_sectors =3D + cpu_to_le32(VHOST_USER_BLK_MAX_DISCARD_SECTORS); config->max_discard_seg =3D cpu_to_le32(1); config->discard_sector_alignment =3D cpu_to_le32(blk_size >> VIRTIO_BLK_SECTOR_BITS); - config->max_write_zeroes_sectors =3D cpu_to_le32(32768); + config->max_write_zeroes_sectors + =3D cpu_to_le32(VHOST_USER_BLK_MAX_WRITE_ZEROES_SECTORS); config->max_write_zeroes_seg =3D cpu_to_le32(1); } =20 --=20 2.29.2