From patchwork Thu Aug 9 19:20:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 10561727 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 E1F73174A for ; Thu, 9 Aug 2018 19:21:42 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BF1122B910 for ; Thu, 9 Aug 2018 19:21:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BC3C82B951; Thu, 9 Aug 2018 19:21:42 +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=-2.9 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,DKIM_VALID,MAILING_LIST_MULTI autolearn=unavailable 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 5CF212B94B for ; Thu, 9 Aug 2018 19:21:42 +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=jVTHNHVNPk6B+5izW5saaJNlQbM69jIIVvFC/zFotkQ=; b=QD7LgK2pBGIsNb d8tz59riUZPeh9CryUbbi9FKRzNgkmXgl1FwrF18KX9mg7coMoo91pJKdNR/zexSuTQPRTE2nurUl GbtSsgkNe4wIR8EGOIzvjjlgZseepKbIEsW8xn4oi6VkzltdmJVlt2W7FImUV+bdCcCrgLIyjfVmF z4PjnyrmKNbVu2HiGz8flt7fONEMvj7nImncm/fwhBBbu4aiBsVAY877DjeeZcaaa52zTfjv+HEZ+ UOWRF+TixGez8dp0/SzdbhtoiZaP31jv3xLGoZDWpUmbp/xvqXG/5YuEhKlo5W+lPmYvWFUaUgATK d543slPxCHOfzVY1Hk1A==; 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 1fnqV3-0000AO-IR; Thu, 09 Aug 2018 19:21:33 +0000 Received: from mail-wr1-x441.google.com ([2a00:1450:4864:20::441]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fnqUz-000067-Ub for linux-arm-kernel@lists.infradead.org; Thu, 09 Aug 2018 19:21:31 +0000 Received: by mail-wr1-x441.google.com with SMTP id b15-v6so6067953wrv.10 for ; Thu, 09 Aug 2018 12:21:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=3ryRmDkrio5+djlWtgIL1XomBIjT3hIM7Bw2C8CpFhE=; b=UxXLnYOXsTtPvMPnq0yBl3j/JI36gd146aOw61VO9TimkxtEC3H4w3CuuBpoGBJ2l4 k4jOBKwSdsBV9M4j/NOLhvMEAlNie09HLGRVEKpJee05ti/9BCejjxR26Q7XepQEjobK qXb7j6ac9uA4mbSKJnK+IXmhFR/62Pcf2uJeV/VV3tYQFFtX66wDkOcE2sUeew9Zf006 X/0Zacy2XHPdihFNBhxxBod8Lxg2QNffvXLxfa8Ph9PvM0eD5lpdBTf6yi0Fp09Egu9l EO+oZ1Xx6Eilm9bAut7o6ds7bvYo8G7x9lAYAwB4NsAVVTzepg/txaIB2zYKntJzZ1fO 1aZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=3ryRmDkrio5+djlWtgIL1XomBIjT3hIM7Bw2C8CpFhE=; b=H/1MyDiE5A6ZRq+Yct3PEcLEelM7G6yBtKTdrTCAhzIVd9CEtzzCk5VsLsFnGnBjlP IuO/TUUTaVUE4IOSALiya+zpG73CfrbLySLP5yFXQlMW4l5vlibh9eqPVALTBwqOOfzn /zxDZtLP8tnIcDKQshAJ6IIej8RqAgyDyd1h25G7ZTjep6g7638EaMZ2Z8Laj6r9LM4K S3Sg3oD6ckQ0yg6XCFl+9dmcVAF+4sZjJ3jG6w3YDyeOgUFjW37hVSOmmjdYTZ1YXWK2 w0tbPSTfXGCPSB9W8n7kM8yWOLz73pAaGPxLOrhjLmRSbkHnd1SSoauEXM/zJxX0sTTi DjyQ== X-Gm-Message-State: AOUpUlGGPMl+12Y9rRricbvBtnB0HQ1HY6ICdeoaaYTNF/DzoU9ko1Pc Rt2NJN82ZHdTmXdlCaVUtVgoTg== X-Google-Smtp-Source: AA+uWPxabhR3PXtafftYVtlNjfKNSe8QwxMJ+0l4uKIG4jea94OGFd0TNxvfTX1RzBmHru8meFV/6Q== X-Received: by 2002:adf:a789:: with SMTP id j9-v6mr2120650wrc.277.1533842482963; Thu, 09 Aug 2018 12:21:22 -0700 (PDT) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:10:84be:a42a:826d:c530]) by smtp.gmail.com with ESMTPSA id o14-v6sm14738797wmd.35.2018.08.09.12.21.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 09 Aug 2018 12:21:22 -0700 (PDT) From: Andrey Konovalov To: Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Catalin Marinas , Will Deacon , Christoph Lameter , Andrew Morton , Mark Rutland , Nick Desaulniers , Marc Zyngier , Dave Martin , Ard Biesheuvel , "Eric W . Biederman" , Ingo Molnar , Paul Lawrence , Geert Uytterhoeven , Arnd Bergmann , "Kirill A . Shutemov" , Greg Kroah-Hartman , Kate Stewart , Mike Rapoport , kasan-dev@googlegroups.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sparse@vger.kernel.org, linux-mm@kvack.org, linux-kbuild@vger.kernel.org Subject: [PATCH v5 05/18] khwasan: initialize shadow to 0xff Date: Thu, 9 Aug 2018 21:20:57 +0200 Message-Id: X-Mailer: git-send-email 2.18.0.597.ga71716f1ad-goog In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180809_122129_992354_7A1EC2F8 X-CRM114-Status: GOOD ( 17.18 ) 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: Vishwath Mohan , Chintan Pandya , Jacob Bramley , Jann Horn , Ruben Ayrapetyan , Andrey Konovalov , Lee Smith , Kostya Serebryany , Mark Brand , Ramana Radhakrishnan , Evgeniy Stepanov 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 A KHWASAN shadow memory cell contains a memory tag, that corresponds to the tag in the top byte of the pointer, that points to that memory. The native top byte value of kernel pointers is 0xff, so with KHWASAN we need to initialize shadow memory to 0xff. This commit does that. Signed-off-by: Andrey Konovalov --- arch/arm64/mm/kasan_init.c | 16 ++++++++++++++-- include/linux/kasan.h | 8 ++++++++ mm/kasan/common.c | 3 ++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/arch/arm64/mm/kasan_init.c b/arch/arm64/mm/kasan_init.c index 12145874c02b..7a31e8ccbad2 100644 --- a/arch/arm64/mm/kasan_init.c +++ b/arch/arm64/mm/kasan_init.c @@ -44,6 +44,15 @@ static phys_addr_t __init kasan_alloc_zeroed_page(int node) return __pa(p); } +static phys_addr_t __init kasan_alloc_raw_page(int node) +{ + void *p = memblock_virt_alloc_try_nid_raw(PAGE_SIZE, PAGE_SIZE, + __pa(MAX_DMA_ADDRESS), + MEMBLOCK_ALLOC_ACCESSIBLE, + node); + return __pa(p); +} + static pte_t *__init kasan_pte_offset(pmd_t *pmdp, unsigned long addr, int node, bool early) { @@ -89,7 +98,9 @@ static void __init kasan_pte_populate(pmd_t *pmdp, unsigned long addr, do { phys_addr_t page_phys = early ? __pa_symbol(kasan_zero_page) - : kasan_alloc_zeroed_page(node); + : kasan_alloc_raw_page(node); + if (!early) + memset(__va(page_phys), KASAN_SHADOW_INIT, PAGE_SIZE); next = addr + PAGE_SIZE; set_pte(ptep, pfn_pte(__phys_to_pfn(page_phys), PAGE_KERNEL)); } while (ptep++, addr = next, addr != end && pte_none(READ_ONCE(*ptep))); @@ -139,6 +150,7 @@ asmlinkage void __init kasan_early_init(void) KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT))); BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_START, PGDIR_SIZE)); BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_END, PGDIR_SIZE)); + kasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE, true); } @@ -235,7 +247,7 @@ void __init kasan_init(void) set_pte(&kasan_zero_pte[i], pfn_pte(sym_to_pfn(kasan_zero_page), PAGE_KERNEL_RO)); - memset(kasan_zero_page, 0, PAGE_SIZE); + memset(kasan_zero_page, KASAN_SHADOW_INIT, PAGE_SIZE); cpu_replace_ttbr1(lm_alias(swapper_pg_dir)); /* At this point kasan is fully initialized. Enable error messages */ diff --git a/include/linux/kasan.h b/include/linux/kasan.h index 6608aa9b35ac..336385baf926 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -139,6 +139,8 @@ static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; } #ifdef CONFIG_KASAN_GENERIC +#define KASAN_SHADOW_INIT 0 + void kasan_cache_shrink(struct kmem_cache *cache); void kasan_cache_shutdown(struct kmem_cache *cache); @@ -149,4 +151,10 @@ static inline void kasan_cache_shutdown(struct kmem_cache *cache) {} #endif /* CONFIG_KASAN_GENERIC */ +#ifdef CONFIG_KASAN_HW + +#define KASAN_SHADOW_INIT 0xFF + +#endif /* CONFIG_KASAN_HW */ + #endif /* LINUX_KASAN_H */ diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 154010ba6c1f..bed8e13c6e1d 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -474,11 +474,12 @@ int kasan_module_alloc(void *addr, size_t size) ret = __vmalloc_node_range(shadow_size, 1, shadow_start, shadow_start + shadow_size, - GFP_KERNEL | __GFP_ZERO, + GFP_KERNEL, PAGE_KERNEL, VM_NO_GUARD, NUMA_NO_NODE, __builtin_return_address(0)); if (ret) { + __memset(ret, KASAN_SHADOW_INIT, shadow_size); find_vm_area(addr)->flags |= VM_KASAN; kmemleak_ignore(ret); return 0;