From patchwork Thu May 25 16:26:47 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Catalin Marinas X-Patchwork-Id: 100529 Delivered-To: patch@linaro.org Received: by 10.140.96.100 with SMTP id j91csp843352qge; Thu, 25 May 2017 09:27:04 -0700 (PDT) X-Received: by 10.99.149.20 with SMTP id p20mr46716611pgd.112.1495729624688; Thu, 25 May 2017 09:27:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1495729624; cv=none; d=google.com; s=arc-20160816; b=oxCRo6MzqvcHQ3wMzv+tuCZBHGk9lVqtjltstD99u1jE8KC4kFukbxfHxUDSz0gc9d sOdmCmXUeKO0apWuintdG7j1Ze35q4zrWkkHehxHQEBJfzi76URTJxY3nTFXhA8+YOBL d2XhLe497EfBS7k82aoV8ESkNyONPTUFWTe8ea7JdfVBQztkwf5LFdmJcRpP00+4KqHM Q/vflYp0+jVMIyFGd6Ygj8UhyzdkKI/dcqNXU/RQx9w+GoA4lyfyJ19yh9PQWSvBlp7F yUcMkMiKTcLthrvBFPloRrNNECDRCsS51xLzkIeWzyc6PwrjY8RLs0hXJkPcIN2jIuqO jCXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=XzxyaMcPEFjZbT5HDBJPe1lcGV9sJfmtDoLX065MBZs=; b=0WoanjRzxQzdPBss41FtNDaLmEP+IDnuv6Z21JXfQPq40lfvL0zAR55bRXmBH9TFKm tKPPfjd0QcYNUU6zOLS1OFuSJfAKifzMUrM/5CY+2k8K9XNCZT8zghyIJvae0opstVAg U3D9FPpYSFJPsaNWpn0A/VsmbeVclDVufBm6PQcmVgKI8v1FHpsecTnSTDCk3jP0dQ+t AlaXohUmpjNtg8ydw2jAhBUZhOq2c6xcSv0Emxv2JhVhrzbknLBy9ewuHBxT3WHXzK+T PKFx0Pvm8FrdmUzgMhWKeIj9K0OxwLmRwrwq/bUNlGUGKKwSUnYzzc6dwmZlxmUM7rBQ KRTw== 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 g12si28773387plm.327.2017.05.25.09.27.04; Thu, 25 May 2017 09: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 S969884AbdEYQ1C (ORCPT + 25 others); Thu, 25 May 2017 12:27:02 -0400 Received: from foss.arm.com ([217.140.101.70]:51886 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S967751AbdEYQ06 (ORCPT ); Thu, 25 May 2017 12:26:58 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 6110D344; Thu, 25 May 2017 09:26:58 -0700 (PDT) Received: from e104818-lin.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 92FAA3F41F; Thu, 25 May 2017 09:26:57 -0700 (PDT) From: Catalin Marinas To: Greg Kroah-Hartman Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH v3] drivers: dma-mapping: Do not leave an invalid area->pages pointer in dma_common_contiguous_remap() Date: Thu, 25 May 2017 17:26:47 +0100 Message-Id: <1495729607-12831-1-git-send-email-catalin.marinas@arm.com> X-Mailer: git-send-email 2.1.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The dma_common_pages_remap() function allocates a vm_struct object and initialises the pages pointer to value passed as argument. However, when this function is called dma_common_contiguous_remap(), the pages array is only temporarily allocated, being freed shortly after dma_common_contiguous_remap() returns. Architecture code checking the validity of an area->pages pointer would incorrectly dereference already freed pointers. This has been exposed by the arm64 commit 44176bb38fa4 ("arm64: Add support for DMA_ATTR_FORCE_CONTIGUOUS to IOMMU"). Fixes: 513510ddba96 ("common: dma-mapping: introduce common remapping functions") Cc: Greg Kroah-Hartman Reported-by: Andrzej Hajda Acked-by: Laura Abbott Reviewed-by: Robin Murphy Signed-off-by: Catalin Marinas --- Changes since v2: - Rebased to 4.12-rc2 drivers/base/dma-mapping.c | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/drivers/base/dma-mapping.c b/drivers/base/dma-mapping.c index f3deb6af42ad..9dbef4d1baa4 100644 --- a/drivers/base/dma-mapping.c +++ b/drivers/base/dma-mapping.c @@ -275,6 +275,24 @@ int dma_common_mmap(struct device *dev, struct vm_area_struct *vma, EXPORT_SYMBOL(dma_common_mmap); #ifdef CONFIG_MMU +static struct vm_struct *__dma_common_pages_remap(struct page **pages, + size_t size, unsigned long vm_flags, pgprot_t prot, + const void *caller) +{ + struct vm_struct *area; + + area = get_vm_area_caller(size, vm_flags, caller); + if (!area) + return NULL; + + if (map_vm_area(area, prot, pages)) { + vunmap(area->addr); + return NULL; + } + + return area; +} + /* * remaps an array of PAGE_SIZE pages into another vm_area * Cannot be used in non-sleeping contexts @@ -285,17 +303,12 @@ void *dma_common_pages_remap(struct page **pages, size_t size, { struct vm_struct *area; - area = get_vm_area_caller(size, vm_flags, caller); + area = __dma_common_pages_remap(pages, size, vm_flags, prot, caller); if (!area) return NULL; area->pages = pages; - if (map_vm_area(area, prot, pages)) { - vunmap(area->addr); - return NULL; - } - return area->addr; } @@ -310,7 +323,7 @@ void *dma_common_contiguous_remap(struct page *page, size_t size, { int i; struct page **pages; - void *ptr; + struct vm_struct *area; pages = kmalloc(sizeof(struct page *) << get_order(size), GFP_KERNEL); if (!pages) @@ -319,11 +332,13 @@ void *dma_common_contiguous_remap(struct page *page, size_t size, for (i = 0; i < (size >> PAGE_SHIFT); i++) pages[i] = nth_page(page, i); - ptr = dma_common_pages_remap(pages, size, vm_flags, prot, caller); + area = __dma_common_pages_remap(pages, size, vm_flags, prot, caller); kfree(pages); - return ptr; + if (!area) + return NULL; + return area->addr; } /*