Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp8937128ybi; Tue, 23 Jul 2019 18:27:04 -0700 (PDT) X-Google-Smtp-Source: APXvYqyGib+LsUV54SMs0SUSOxK6rrEIRGwe1M8blmm4xZYkFtQ/hAfUzi02/MVXcAx7e6akEQV2 X-Received: by 2002:a63:4f58:: with SMTP id p24mr14899470pgl.50.1563931624179; Tue, 23 Jul 2019 18:27:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563931624; cv=none; d=google.com; s=arc-20160816; b=C6lnGA4JjiBNmGL9/aT4VJDxZoqBfMcGcfNvkFNlV+0EBhxug03n7VZh5tQ34avn2i 8sVQKB8jCxDihugRZuicCF69K4uGMvrcroGDrCfzbYn+I17XwRRJKrUiaj+Lnn6eVHJr o48D326So3eDOj/79FoOViSKKgwF+UeUUNhYBDbkNbyASpeB1CG7OtB82GqehJyjs8oA Z/dCxmfiAkdtv7+qKcdgNxw6Bhzjnwoc6KKjQtAA9/fIq4woRJYdZLHxImSj3qdWRaZj Yx+mMaQuL6Kv+vGHJhfF7r5sKTp0xy4ymk+h2ZLlOddSYYXD4QHnfFT7ozUMTdZSGkV4 pLYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=Qsi9pCBY0bVPm3Igrj6wkd5fhfRA20XeonxFuQZ0Flo=; b=IK0bHDkHF6G912CVn1gMItaGsXvXqCBItwhGDuQEuS9UOABJkE6tsQEZUMVG3G8VhQ Tx+KkLoJAzPJGiZIZUJksEAnnHQlRsdifeeh+ws6UcK8kZSUia0MK99IYS074S2bLHtu JXfnVY9Yqul6j9w8F6WTXEWC/wJbX+I7QEjf7WLjTWzs6tXRWySVc8Y/4EawzkiOvpCf z874cspfO2gz7pxueBfZCz/mhVJilzqf+i44ugvG54c+bVckfHWz8SuqCLw/prpZFxpX +6iu+bOcTDpO0hL2uRX1Hoo11s2z0Xl+fhZHxA+4Sl9CuN/jk1rvAtfKoiI5sI8VyahR zMlw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z13si13978823pfr.86.2019.07.23.18.26.48; Tue, 23 Jul 2019 18:27:04 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390738AbfGWQI4 (ORCPT + 99 others); Tue, 23 Jul 2019 12:08:56 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:2741 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2388820AbfGWQIl (ORCPT ); Tue, 23 Jul 2019 12:08:41 -0400 Received: from DGGEMS401-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 3E9C692B2A821DFB467A; Wed, 24 Jul 2019 00:08:39 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS401-HUB.china.huawei.com (10.3.19.201) with Microsoft SMTP Server id 14.3.439.0; Wed, 24 Jul 2019 00:08:28 +0800 From: Shameer Kolothum To: , CC: , , , , , , , Shameer Kolothum Subject: [PATCH v8 4/6] vfio/type1: check dma map request is within a valid iova range Date: Tue, 23 Jul 2019 17:06:35 +0100 Message-ID: <20190723160637.8384-5-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20190723160637.8384-1-shameerali.kolothum.thodi@huawei.com> References: <20190723160637.8384-1-shameerali.kolothum.thodi@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.202.227.237] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This checks and rejects any dma map request outside valid iova range. Signed-off-by: Shameer Kolothum Reviewed-by: Eric Auger --- drivers/vfio/vfio_iommu_type1.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 7005a8cfca1b..56cf55776d6c 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -1038,6 +1038,27 @@ static int vfio_pin_map_dma(struct vfio_iommu *iommu, struct vfio_dma *dma, return ret; } +/* + * Check dma map request is within a valid iova range + */ +static bool vfio_iommu_iova_dma_valid(struct vfio_iommu *iommu, + dma_addr_t start, dma_addr_t end) +{ + struct list_head *iova = &iommu->iova_list; + struct vfio_iova *node; + + list_for_each_entry(node, iova, list) { + if (start >= node->start && end <= node->end) + return true; + } + + /* + * Check for list_empty() as well since a container with + * a single mdev device will have an empty list. + */ + return list_empty(iova); +} + static int vfio_dma_do_map(struct vfio_iommu *iommu, struct vfio_iommu_type1_dma_map *map) { @@ -1081,6 +1102,11 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu, goto out_unlock; } + if (!vfio_iommu_iova_dma_valid(iommu, iova, iova + size - 1)) { + ret = -EINVAL; + goto out_unlock; + } + dma = kzalloc(sizeof(*dma), GFP_KERNEL); if (!dma) { ret = -ENOMEM; -- 2.17.1