From patchwork Fri May 25 14:40:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 10427659 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 79832602D6 for ; Fri, 25 May 2018 14:41:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 685D129792 for ; Fri, 25 May 2018 14:41:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5AA3E297BB; Fri, 25 May 2018 14:41:06 +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=-10.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B7996297BF for ; Fri, 25 May 2018 14:41:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 165D46B000D; Fri, 25 May 2018 10:40:52 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 0D14A6B000E; Fri, 25 May 2018 10:40:51 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DE7196B0010; Fri, 25 May 2018 10:40:51 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-wr0-f200.google.com (mail-wr0-f200.google.com [209.85.128.200]) by kanga.kvack.org (Postfix) with ESMTP id 7E23C6B000D for ; Fri, 25 May 2018 10:40:51 -0400 (EDT) Received: by mail-wr0-f200.google.com with SMTP id z7-v6so4326812wrg.11 for ; Fri, 25 May 2018 07:40:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references; bh=lEZ4Hklt2FLmURY/1bzbqiWRNGKh6SQKNVmLqzLrFsM=; b=cbCIC69TaEAaWZFx3MYMtYkcSXoLJmcsvgWOXyim6fphOfRUi2IdNtaOR7LswlFN4D gGF2zdxQe7uKDNO7c7iSzvj1c3c7ciUmZWDYtXoP9ff/fdY/1FYlk/Lbvkg4Tr+GTmzu Gcyfp1YCu1qjWyek7ztOUZ6rmtQyEkm7vxwwL7+MO4AKTMV1z1loO3q5e5hKMqivVdYS XhKISd3H9ITZu5/we4sck2+EW3DvesvM1BHYRQU9vC5XuCRwfFkAAnFe87ToTvB9FWVu ZiF/XcmLYBAT5gS8YeaE0ujdppl22EPgqSaaiRxa7xXujg2pG5ihXXFLTP1rX+0DPMMN ikug== X-Gm-Message-State: ALKqPweMn91V1hlN+rdzWpXO03duO4lTD5Y7eUUu1VOixP23tk+rb3Vb +M2lfek/CoO4y1O6ipetUCrinG97EQbHEWKmZt2EUe/Nx549Z9ud6gdpcuP9U2O03H4rBPhTe6B O+oI0Rk755x3sCoVE/iX4uRMQelCAedUlxzt6sXI2GVX2eDtT9vTSqxa55Cu8GuhErT/G99S6ns q6vSHwaOCEPKULiWr+MGJisIhEdPhioyzn9WjzIsO+j+pUuwgfMcvCQlJeMfp/TAKe6qo70ZaVJ E94sHPrEbJvE0lLUm9PnJbai+pLlTeZMzwjbO4Gtu+F5CNImgx3PNaPKgdFIZUoCy04QAnP7etf Y7fdSJTPy/jC+rV7xzHtfs3Q7ruOq41VwEclfKEIWk03HGJ7qSze07IQCIdFFhgrBYRNSDngQ9X n X-Received: by 2002:adf:8584:: with SMTP id 4-v6mr2810906wrt.15.1527259251076; Fri, 25 May 2018 07:40:51 -0700 (PDT) X-Received: by 2002:adf:8584:: with SMTP id 4-v6mr2810851wrt.15.1527259250263; Fri, 25 May 2018 07:40:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527259250; cv=none; d=google.com; s=arc-20160816; b=xCwken1UeuQ21jHn1Mf0izqqjGuwnvRivUS89V5I2qQ0aspiUFvz23seuGL6ZO6ssI EUhl4BxvmBeJlNxPb/1W+IFZZEuL4m25kFyvbqbpcTgQVaoYyeVq9yE3YcC0hAhrQAAI 5S6L7TyJTHkMy/zLKiZ0CeIExsuQVL99yAr8J/lEbvYp2ApTzmfofoxibO7dB+/eE+JE evshTRdDyUUMiWa7kXB7VxB2U4o5EaaXMVHolifJCPzT/kFrWnqO3fT7tpulC/0l4mmP 4xepoFdXWI+oOePpoVaBLKPX/Oq95RlbMuZF9PzSAAMxQ5jEHEhyY4i3d9lvjUUW2IdI Nozg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=lEZ4Hklt2FLmURY/1bzbqiWRNGKh6SQKNVmLqzLrFsM=; b=S/zXMifBDkPyig6oxZ9w3bPyc3vW5zzFoxKa9yR8n3z0TSvh27Fm7KSjnE7Arv/9CB tVKVXhDlM7+XKbIlqrxx9b9JvXeKUTxCKB7BMSCAaXflz0K2U6gVzXQVMBgSFrraeM4Y HdpNUOIlNnTmSR9f8xTeKOdDpwy7N17yWuYSUcLRNCqiypVVK1qRRXUUFpfa0gRF46zW e2NUypjnv82pYDaGDVVlSR2PwAtLd9Zq1MIyQRcTl/5TIlzGJiZULp1EjqdF/Gu2hBZ2 /i6ObOpwLvz00Nqxns/yTcvLCQV3r8Mi9GLTRd2hGY03W9XZqyY0XNV9HkDy3P4dC8DY G36g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=QYVZ+5YU; spf=pass (google.com: domain of andreyknvl@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=andreyknvl@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id r65-v6sor2188068wmf.11.2018.05.25.07.40.50 for (Google Transport Security); Fri, 25 May 2018 07:40:50 -0700 (PDT) Received-SPF: pass (google.com: domain of andreyknvl@google.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=QYVZ+5YU; spf=pass (google.com: domain of andreyknvl@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=andreyknvl@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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; bh=lEZ4Hklt2FLmURY/1bzbqiWRNGKh6SQKNVmLqzLrFsM=; b=QYVZ+5YUjYatR7g7k8h5fi8hOGVdeXapBFbR/rtIp37aUSKJe6ZjA+GQJn/RymuGZ3 2LmjxVHZImJvvrf8bTWyRFq68FBSM2AY3q+zeTCWbI1wq3FpaE5auvcZprZlkBai4SS/ tSQeuTqgr3qlciOMBVTxUnrGNejLvT7H6/OdaHAmKMZ2L+CIvq2hQqAaLa4ae6c3rVF9 E9haOXvvyLokmF7c2K9cJ6uahQrZ86Nn8jKx7Bu4Bw0dQahhiPD7ICCcSgOZOCrV5U+y 7e3BNP0ZYhKgQIkMGNyqm6bE4EocRQz0Y+oVu+wm8oQ03GJxVqC1ldmmKopH+AdF0tqP HZBA== X-Google-Smtp-Source: ADUXVKLYSODNfdRLd3OkY/y65bX5fAlBcX1YWL0q+lZtJeuB3evgyIaJ1FlP0cOYdxtEKVBDsrfp2A== X-Received: by 2002:a1c:c6ca:: with SMTP id w193-v6mr2000886wmf.68.1527259249204; Fri, 25 May 2018 07:40:49 -0700 (PDT) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:10:84be:a42a:826d:c530]) by smtp.gmail.com with ESMTPSA id q2-v6sm25293885wrm.26.2018.05.25.07.40.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 25 May 2018 07:40:47 -0700 (PDT) From: Andrey Konovalov To: Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Jonathan Corbet , Catalin Marinas , Will Deacon , Christopher Li , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Masahiro Yamada , Michal Marek , Andrey Konovalov , Mark Rutland , Nick Desaulniers , Yury Norov , Marc Zyngier , Kristina Martsenko , Suzuki K Poulose , Punit Agrawal , Dave Martin , Ard Biesheuvel , James Morse , Michael Weiser , Julien Thierry , Tyler Baicar , "Eric W . Biederman" , Thomas Gleixner , Ingo Molnar , Kees Cook , Sandipan Das , David Woodhouse , Paul Lawrence , Herbert Xu , Josh Poimboeuf , Geert Uytterhoeven , Tom Lendacky , Arnd Bergmann , Dan Williams , Michal Hocko , Jan Kara , Ross Zwisler , =?UTF-8?q?J=C3=A9r=C3=B4me=20Glisse?= , Matthew Wilcox , "Kirill A . Shutemov" , Souptick Joarder , Hugh Dickins , Davidlohr Bueso , Greg Kroah-Hartman , Philippe Ombredanne , Kate Stewart , Laura Abbott , Boris Brezillon , Vlastimil Babka , Pintu Agarwal , Doug Berger , Anshuman Khandual , Mike Rapoport , Mel Gorman , Pavel Tatashin , Tetsuo Handa , 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 Cc: Kostya Serebryany , Evgeniy Stepanov , Lee Smith , Ramana Radhakrishnan , Jacob Bramley , Ruben Ayrapetyan , Kees Cook , Jann Horn , Mark Brand , Chintan Pandya Subject: [PATCH v2 05/16] khwasan: initialize shadow to 0xff Date: Fri, 25 May 2018 16:40:21 +0200 Message-Id: <727e82c4aab2391df254c0752fa4eec308e70653.1527259068.git.andreyknvl@google.com> X-Mailer: git-send-email 2.17.0.921.gf22659ad46-goog In-Reply-To: References: X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 99a569e18ec7..3d7277cc1f5b 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -473,11 +473,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;