From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.2 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 08BB9C3A5A0 for ; Mon, 19 Aug 2019 16:03:37 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id C913E20651 for ; Mon, 19 Aug 2019 16:03:36 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="VDw70X6f" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C913E20651 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=arm.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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:In-Reply-To:MIME-Version:References: Message-ID:Subject:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=tBaOevoqMiDSQP/n0LdYzgyA/iw0Hpk6eO7+TP1ZL2c=; b=VDw70X6f+5fglV Yf7jWBvH7UkMfiisIClp3vcoWAtyZQW+jzEIT9JRwhcVVsp7t1IabhkM4mpN1YVvearvzwQsZlkQ8 OfyZRemWee15TigUzq5Fuq2bKOrn2SvF/d1ARe7kOz3pLrt0x9i5mOYO82RcGlzvwRs4Ct8L/G+/H EfUr57whV7owkceAJxkdez5652Mr91Qi8wnCKhSKXcML0MmRbBQrMgfYnLb74ia5uKS8kYJzTZDEn LBA6/1GHm0TAfVTE95Tf8nIUsz6xZPvxuoJvPx9Lt7UIMVnFxxkPDlfLZRN1GXHSSwB1pwYwyrY15 /MS8R18q251wNuFIvUag==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92 #3 (Red Hat Linux)) id 1hzk82-0001gH-NW; Mon, 19 Aug 2019 16:03:30 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.92 #3 (Red Hat Linux)) id 1hzk7x-0001fZ-T4; Mon, 19 Aug 2019 16:03:28 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id C82F4344; Mon, 19 Aug 2019 09:03:24 -0700 (PDT) Received: from lakrids.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D1F6B3F718; Mon, 19 Aug 2019 09:03:22 -0700 (PDT) Date: Mon, 19 Aug 2019 17:03:20 +0100 From: Mark Rutland To: Andrey Konovalov Subject: Re: [PATCH] arm64: kasan: fix phys_to_virt() false positive on tag-based kasan Message-ID: <20190819160320.GF9927@lakrids.cambridge.arm.com> References: <20190819114420.2535-1-walter-zh.wu@mediatek.com> <20190819125625.bu3nbrldg7te5kwc@willie-the-truck> <20190819132347.GB9927@lakrids.cambridge.arm.com> <20190819133441.ejomv6cprdcz7hh6@willie-the-truck> <20190819150341.GC9927@lakrids.cambridge.arm.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.11.1+11 (2f07cb52) (2018-12-01) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190819_090327_212636_1F453002 X-CRM114-Status: GOOD ( 37.00 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Walter Wu , wsd_upstream@mediatek.com, Catalin Marinas , Will Deacon , LKML , kasan-dev , linux-mediatek@lists.infradead.org, Alexander Potapenko , Linux ARM , Matthias Brugger , Andrey Ryabinin , Andrew Morton , Will Deacon , Dmitry Vyukov Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Mon, Aug 19, 2019 at 05:37:36PM +0200, Andrey Konovalov wrote: > On Mon, Aug 19, 2019 at 5:03 PM Mark Rutland wrote: > > > > On Mon, Aug 19, 2019 at 04:05:22PM +0200, Andrey Konovalov wrote: > > > On Mon, Aug 19, 2019 at 3:34 PM Will Deacon wrote: > > > > > > > > On Mon, Aug 19, 2019 at 02:23:48PM +0100, Mark Rutland wrote: > > > > > On Mon, Aug 19, 2019 at 01:56:26PM +0100, Will Deacon wrote: > > > > > > On Mon, Aug 19, 2019 at 07:44:20PM +0800, Walter Wu wrote: > > > > > > > __arm_v7s_unmap() call iopte_deref() to translate pyh_to_virt address, > > > > > > > but it will modify pointer tag into 0xff, so there is a false positive. > > > > > > > > > > > > > > When enable tag-based kasan, phys_to_virt() function need to rewrite > > > > > > > its original pointer tag in order to avoid kasan report an incorrect > > > > > > > memory corruption. > > > > > > > > > > > > Hmm. Which tree did you see this on? We've recently queued a load of fixes > > > > > > in this area, but I /thought/ they were only needed after the support for > > > > > > 52-bit virtual addressing in the kernel. > > > > > > > > > > I'm seeing similar issues in the virtio blk code (splat below), atop of > > > > > the arm64 for-next/core branch. I think this is a latent issue, and > > > > > people are only just starting to test with KASAN_SW_TAGS. > > > > > > > > > > It looks like the virtio blk code will round-trip a SLUB-allocated pointer from > > > > > virt->page->virt, losing the per-object tag in the process. > > > > > > > > > > Our page_to_virt() seems to get a per-page tag, but this only makes > > > > > sense if you're dealing with the page allocator, rather than something > > > > > like SLUB which carves a page into smaller objects giving each object a > > > > > distinct tag. > > > > > > > > > > Any round-trip of a pointer from SLUB is going to lose the per-object > > > > > tag. > > > > > > > > Urgh, I wonder how this is supposed to work? > > > > > > > > If we end up having to check the KASAN shadow for *_to_virt(), then why > > > > do we need to store anything in the page flags at all? Andrey? > > > > > > As per 2813b9c0 ("kasan, mm, arm64: tag non slab memory allocated via > > > pagealloc") we should only save a non-0xff tag in page flags for non > > > slab pages. > > > > > > Could you share your .config so I can reproduce this? > > > > I wrote a test (below) to do so. :) > > > > It fires with arm64 defconfig, + CONFIG_TEST_KASAN=m. > > > > With Andrey Ryabinin's patch it works as expected with no KASAN splats > > for the two new test cases. > > OK, Andrey's patch makes sense and fixes both Mark's test patch and > reports from CONFIG_IOMMU_IO_PGTABLE_ARMV7S_SELFTEST. > > Tested-by: Andrey Konovalov > Reviewed-by: Andrey Konovalov > > on both patches. > > > > > Thanks, > > Mark. > > > > ---->8---- > > From 7e8569b558fca21ad4e80fddae659591bc84ce1f Mon Sep 17 00:00:00 2001 > > From: Mark Rutland > > Date: Mon, 19 Aug 2019 15:39:32 +0100 > > Subject: [PATCH] lib/test_kasan: add roundtrip tests > > > > In several places we needs to be able to operate on pointers which have > > "needs" => "need" Thanks! I'll spin a standalone v2 of this with that fixed and your tags folded in. Mark. > > > gone via a roundtrip: > > > > virt -> {phys,page} -> virt > > > > With KASAN_SW_TAGS, we can't preserve the tag for SLUB objects, and the > > {phys,page} -> virt conversion will use KASAN_TAG_KERNEL. > > > > This patch adds tests to ensure that this works as expected, without > > false positives. > > > > Signed-off-by: Mark Rutland > > Cc: Andrey Ryabinin > > Cc: Andrey Konovalov > > Cc: Will Deacon > > --- > > lib/test_kasan.c | 40 ++++++++++++++++++++++++++++++++++++++++ > > 1 file changed, 40 insertions(+) > > > > diff --git a/lib/test_kasan.c b/lib/test_kasan.c > > index b63b367a94e8..cf7b93f0d90c 100644 > > --- a/lib/test_kasan.c > > +++ b/lib/test_kasan.c > > @@ -19,6 +19,8 @@ > > #include > > #include > > > > +#include > > + > > /* > > * Note: test functions are marked noinline so that their names appear in > > * reports. > > @@ -337,6 +339,42 @@ static noinline void __init kmalloc_uaf2(void) > > kfree(ptr2); > > } > > > > +static noinline void __init kfree_via_page(void) > > +{ > > + char *ptr; > > + size_t size = 8; > > + struct page *page; > > + unsigned long offset; > > + > > + pr_info("invalid-free false positive (via page)\n"); > > + ptr = kmalloc(size, GFP_KERNEL); > > + if (!ptr) { > > + pr_err("Allocation failed\n"); > > + return; > > + } > > + > > + page = virt_to_page(ptr); > > + offset = offset_in_page(ptr); > > + kfree(page_address(page) + offset); > > +} > > + > > +static noinline void __init kfree_via_phys(void) > > +{ > > + char *ptr; > > + size_t size = 8; > > + phys_addr_t phys; > > + > > + pr_info("invalid-free false positive (via phys)\n"); > > + ptr = kmalloc(size, GFP_KERNEL); > > + if (!ptr) { > > + pr_err("Allocation failed\n"); > > + return; > > + } > > + > > + phys = virt_to_phys(ptr); > > + kfree(phys_to_virt(phys)); > > +} > > + > > static noinline void __init kmem_cache_oob(void) > > { > > char *p; > > @@ -737,6 +775,8 @@ static int __init kmalloc_tests_init(void) > > kmalloc_uaf(); > > kmalloc_uaf_memset(); > > kmalloc_uaf2(); > > + kfree_via_page(); > > + kfree_via_phys(); > > kmem_cache_oob(); > > memcg_accounted_kmem_cache(); > > kasan_stack_oob(); > > -- > > 2.11.0 > > _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel