Received: by 2002:a25:f815:0:0:0:0:0 with SMTP id u21csp930042ybd; Wed, 26 Jun 2019 08:16:25 -0700 (PDT) X-Google-Smtp-Source: APXvYqyMqZc9yE+BupNbi8Ljt6RFsNIm0A3BjQ5TnMNNNpAuUyLsoUWMBmg12x8gTZmF7q7cXUeV X-Received: by 2002:a17:90a:35e6:: with SMTP id r93mr5465503pjb.20.1561562185278; Wed, 26 Jun 2019 08:16:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561562185; cv=none; d=google.com; s=arc-20160816; b=L6LBzgFzYuhb9n4VZ32qJK5I3f1dWNqd54VA2s6rQwrTuMNJ/v/e8CVwVd0O3vEcVY OW8PVJHvcoz9HIkuwn+laL3VzDltLt3Va4UhHKQceZstU/5x2lCCGt/emX/6pwr74eK3 MmQDss8yjn846tQB8asKmxG6esUyP7DETTvzJkJBvwECsyU+CgPGfA0mv21nL7N6NDNk UtQ64yywq5Og1EAul1zfmRF6XEgCZHRyUSqmcYHveOwg4wtpopgDBkj6r0suvtNKiLfi ov7dB+MU/maK/9iaKaXNqB11gZTj7xW7oCaxrZSbAcOYSZEQ2rOzmMs9tRDfKvOKoMf5 snwQ== 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=PSGpmATupmfnrZwylgl0MfZKBkio32sCclm631JaAZM=; b=YhczGk3cRozLWhveGDoZzWpRNdVBTjmjUjNFYRgvsuTvVMjxZaLUb88j1QLZA+vo62 t+qELKdv9yD/QDjJVCvsI65n9H0KqMgzVY4aVQ+aewBY2guNKrVuSlL1paD2bl4rbx2Y K4le69hN5123nqjflU/y9xwTnQh6fluGY5qFPSZE2fNAPOrwFs2kTtQGY4SISrUiCx/q 2Vlai+L5zwf9WdinwDmBLNXu53TUv75rbLzhblD/FWoIopABHsi3owo6LshiJ3CauHlA lDmViUbEMtAW1npNhDFyvDgk2JiVNTUtdCudQeNhWNgwZsiAedJTO8Vy+6vdLpNdAvnn BHag== 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 l185si18704349pfl.190.2019.06.26.08.16.09; Wed, 26 Jun 2019 08:16:25 -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 S1728377AbfFZPNz (ORCPT + 99 others); Wed, 26 Jun 2019 11:13:55 -0400 Received: from szxga04-in.huawei.com ([45.249.212.190]:19083 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726157AbfFZPNy (ORCPT ); Wed, 26 Jun 2019 11:13:54 -0400 Received: from DGGEMS406-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id 6AF3767679FA3F4B31A8; Wed, 26 Jun 2019 23:13:52 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS406-HUB.china.huawei.com (10.3.19.206) with Microsoft SMTP Server id 14.3.439.0; Wed, 26 Jun 2019 23:13:42 +0800 From: Shameer Kolothum To: , , CC: , , , , , , , Shameer Kolothum Subject: [PATCH v7 4/6] vfio/type1: check dma map request is within a valid iova range Date: Wed, 26 Jun 2019 16:12:46 +0100 Message-ID: <20190626151248.11776-5-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20190626151248.11776-1-shameerali.kolothum.thodi@huawei.com> References: <20190626151248.11776-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 --- v6 --> v7 Addressed the case where a container with only an mdev device will have an empty list(Suggested by Alex). --- 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 e872fb3a0f39..89ad0da7152c 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -1050,6 +1050,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 + * only an mdev device will have an empty list. + */ + return list_empty(&iommu->iova_list); +} + static int vfio_dma_do_map(struct vfio_iommu *iommu, struct vfio_iommu_type1_dma_map *map) { @@ -1093,6 +1114,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