From patchwork Mon Apr 22 17:59:18 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 10911311 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2AB6313B5 for ; Mon, 22 Apr 2019 18:00:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1CDAA28786 for ; Mon, 22 Apr 2019 18:00:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0FA8D28789; Mon, 22 Apr 2019 18:00:36 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 78F4F28786 for ; Mon, 22 Apr 2019 18:00:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=1LOotfNAQNtwKclJOc9Tj+8WzDBlye6OePvDsl1mShM=; b=ElvLfV5mfBp41y jpx99CkfRrnsgxjPhk8avAGTm20p2bWBrttAJL2qk4ZC/I2ATmMtCINh8mXJMylN3icZm8UBdXvYO JIhvnbgsmJT7sc/B2zIUI0XgPKgfgOflas96iZsVhNGPUT8Nej4ytuVirX0Z7kwQGvHBqB6DPP4xe iUPsBF0NEdEv8kvMSZkZ+6gctP6g+4MLmV5u7elqvS9xYAxPgIlweGCb/uESBH+Jj/aNlRPRn3KGC Q3b4iH3J9dew4BqXMrgSX9SPAjBwbtnxYQWiV89Vgy14i3d/Urua40iMMFEozqmFq+LBUgtyiSZe7 T5rTrYStzzlvA3bqt/vw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hIdF3-0001ph-As; Mon, 22 Apr 2019 18:00:33 +0000 Received: from 213-225-37-80.nat.highway.a1.net ([213.225.37.80] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1hIdEj-0001R1-7d; Mon, 22 Apr 2019 18:00:13 +0000 From: Christoph Hellwig To: Robin Murphy Subject: [PATCH 02/26] arm64/iommu: improve mmap bounds checking Date: Mon, 22 Apr 2019 19:59:18 +0200 Message-Id: <20190422175942.18788-3-hch@lst.de> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190422175942.18788-1-hch@lst.de> References: <20190422175942.18788-1-hch@lst.de> MIME-Version: 1.0 X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Tom Lendacky , Catalin Marinas , Joerg Roedel , Will Deacon , linux-kernel@vger.kernel.org, iommu@lists.linux-foundation.org, linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP The nr_pages checks should be done for all mmap requests, not just those using remap_pfn_range. Signed-off-by: Christoph Hellwig --- arch/arm64/mm/dma-mapping.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/arch/arm64/mm/dma-mapping.c b/arch/arm64/mm/dma-mapping.c index 674860e3e478..604c638b2787 100644 --- a/arch/arm64/mm/dma-mapping.c +++ b/arch/arm64/mm/dma-mapping.c @@ -73,19 +73,9 @@ static int __swiotlb_get_sgtable_page(struct sg_table *sgt, static int __swiotlb_mmap_pfn(struct vm_area_struct *vma, unsigned long pfn, size_t size) { - int ret = -ENXIO; - unsigned long nr_vma_pages = vma_pages(vma); - unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT; - unsigned long off = vma->vm_pgoff; - - if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) { - ret = remap_pfn_range(vma, vma->vm_start, - pfn + off, - vma->vm_end - vma->vm_start, - vma->vm_page_prot); - } - - return ret; + return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff, + vma->vm_end - vma->vm_start, + vma->vm_page_prot); } #endif /* CONFIG_IOMMU_DMA */ @@ -241,6 +231,8 @@ static int __iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma, void *cpu_addr, dma_addr_t dma_addr, size_t size, unsigned long attrs) { + unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT; + unsigned long off = vma->vm_pgoff; struct vm_struct *area; int ret; @@ -249,6 +241,9 @@ static int __iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma, if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret)) return ret; + if (off >= nr_pages || vma_pages(vma) > nr_pages - off) + return -ENXIO; + if (!is_vmalloc_addr(cpu_addr)) { unsigned long pfn = page_to_pfn(virt_to_page(cpu_addr)); return __swiotlb_mmap_pfn(vma, pfn, size);