From patchwork Wed Jun 26 15:12:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shameerali Kolothum Thodi X-Patchwork-Id: 167836 Delivered-To: patch@linaro.org Received: by 2002:a92:4782:0:0:0:0:0 with SMTP id e2csp1073308ilk; Wed, 26 Jun 2019 08:14:05 -0700 (PDT) X-Google-Smtp-Source: APXvYqwF1X+U3AJKD3xuWD+2DihDarC0MWeGxZMWSJ2tIy1tnXw5MP3mrftUsQedhxdp+sI5l5DD X-Received: by 2002:a17:90a:37e9:: with SMTP id v96mr5278125pjb.10.1561562045750; Wed, 26 Jun 2019 08:14:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561562045; cv=none; d=google.com; s=arc-20160816; b=nYa4+wJ1eLD+482l86ktLaGtZUM6sLvsuGuQTrBPiJQ1h1fAs6592Tw321scMcv6Fp nXR2Zb/kgdeXAe32NlhdKZHqFoBJd/ikYufT/w1hELPaGhxMtV/5oJajzZnGG31QkUae r7eZ3D10o9rFttTh5tnI4dGTsUEfNayExN6ORKQVQWzlsvuenJb8/aSeonyt/yW3HVpg X5zasg8YKHnvWPCzvfuHp5MEVVAklCfMldAxchdHPlysEU/MQWAOa7Xw1rz64bMPolLd HOk0S6sCgiLODMdVKd2JF/sUe9XBV/GwI98ZoAiYBi69lJqpCkriVh24cfMW0M+4CW49 Tt4Q== 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 w7si15618857pgs.168.2019.06.26.08.14.05; Wed, 26 Jun 2019 08:14:05 -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 S1728388AbfFZPNz (ORCPT + 30 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 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(+) -- 2.17.1 Reviewed-by: Eric Auger 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;