Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2498307imm; Mon, 24 Sep 2018 05:30:13 -0700 (PDT) X-Google-Smtp-Source: ANB0Vdb5rMVa9lqME6yjtv3g1yAoj9wJ1zKvg8Hs9KgNGvWbkhkxsbaMggHnLq+NFXxUyD1g3cp2 X-Received: by 2002:a62:fc13:: with SMTP id e19-v6mr6451477pfh.101.1537792212962; Mon, 24 Sep 2018 05:30:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537792212; cv=none; d=google.com; s=arc-20160816; b=uKbAZ3iJMtzlBbWKsxIws3oo5AJ1FRlgtqLXeN2h1MEGb1lGF7LkAp1zGbyKTKJToF tKivsBp4zsCWNf3veK5zRYSvrEcXuMy2Ufx7mSmHeMb9AGhceKaeNk1FrpPtb4p7ich5 SfcMq7J9FOSnebemFSJJKJbcQbYSXs7EJgIeO+lw2IF3rLQopfgx4YaH97Vxo3KyMeJB jFYLnhDgdiiSK0Cvg8c7MVaOHcLaMKaJsRnpZwXMkD91874ZUdHgWmrTlYbfjmwfkzUS 6bwGNWauSbZ1m+vvNqCPE7xGrjvFP8YBsij3IY9EkzsKLe96kNtZSPZPgPKunYc6aAs8 Y5JQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from; bh=LY2XsOR2ASrv6oh6G0zugtESvZd/8t+A3WtTYU/RAts=; b=rrS+cVMszS8gTqnn/ILiphMefSJ7kM90oafenJSsENGP0qfdXE+JA2pxwpA0C2hwCp LpSxMsAjyYjmI/Y/YeHxBfZ0tIxWRbOyygVOtVLfcBNzFUEpkeEJ8HVfuEhbdNiMwSOM o9Acfg6uC+b1dQqg9RRLMtoka+GCOZY75oIcpYQpoBe7PpR9lqcgA5G6kZmPBy2YrZa5 /S6npA1m6wPJXtw9kA6T+SFJOAjxZFiWLWpAyFURRD3BF8POMTW4IeV2Qu6c8/WiS3j4 jgO/D+aB1t2T9YczJKbM4LZGBpG5VqtpB/svkRX0qVc/O9SmHozyZVcr8DDOiE2g4C9K cfYg== 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 u127-v6si8524794pgc.234.2018.09.24.05.29.56; Mon, 24 Sep 2018 05:30:12 -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 S1733289AbeIXS3Z (ORCPT + 99 others); Mon, 24 Sep 2018 14:29:25 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:57306 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730233AbeIXS3X (ORCPT ); Mon, 24 Sep 2018 14:29:23 -0400 Received: from localhost (ip-213-127-77-73.ip.prioritytelecom.net [213.127.77.73]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id E6A2F1018; Mon, 24 Sep 2018 12:27:29 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jean-Philippe Brucker , Will Deacon , Sasha Levin Subject: [PATCH 4.18 018/235] iommu/io-pgtable-arm-v7s: Abort allocation when table address overflows the PTE Date: Mon, 24 Sep 2018 13:50:04 +0200 Message-Id: <20180924113106.410621564@linuxfoundation.org> X-Mailer: git-send-email 2.19.0 In-Reply-To: <20180924113103.999624566@linuxfoundation.org> References: <20180924113103.999624566@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Jean-Philippe Brucker [ Upstream commit 29859aeb8a6ea17ba207933a81b6b77b4d4df81a ] When run on a 64-bit system in selftest, the v7s driver may obtain page table with physical addresses larger than 32-bit. Level-2 tables are 1KB and are are allocated with slab, which doesn't accept the GFP_DMA32 flag. Currently map() truncates the address written in the PTE, causing iova_to_phys() or unmap() to access invalid memory. Kasan reports it as a use-after-free. To avoid any nasty surprise, test if the physical address fits in a PTE before returning a new table. 32-bit systems, which are the main users of this page table format, shouldn't see any difference. Signed-off-by: Jean-Philippe Brucker Signed-off-by: Will Deacon Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- drivers/iommu/io-pgtable-arm-v7s.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) --- a/drivers/iommu/io-pgtable-arm-v7s.c +++ b/drivers/iommu/io-pgtable-arm-v7s.c @@ -192,6 +192,7 @@ static void *__arm_v7s_alloc_table(int l { struct io_pgtable_cfg *cfg = &data->iop.cfg; struct device *dev = cfg->iommu_dev; + phys_addr_t phys; dma_addr_t dma; size_t size = ARM_V7S_TABLE_SIZE(lvl); void *table = NULL; @@ -200,6 +201,10 @@ static void *__arm_v7s_alloc_table(int l table = (void *)__get_dma_pages(__GFP_ZERO, get_order(size)); else if (lvl == 2) table = kmem_cache_zalloc(data->l2_tables, gfp | GFP_DMA); + phys = virt_to_phys(table); + if (phys != (arm_v7s_iopte)phys) + /* Doesn't fit in PTE */ + goto out_free; if (table && !(cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA)) { dma = dma_map_single(dev, table, size, DMA_TO_DEVICE); if (dma_mapping_error(dev, dma)) @@ -209,7 +214,7 @@ static void *__arm_v7s_alloc_table(int l * address directly, so if the DMA layer suggests otherwise by * translating or truncating them, that bodes very badly... */ - if (dma != virt_to_phys(table)) + if (dma != phys) goto out_unmap; } kmemleak_ignore(table);