From patchwork Wed Aug 29 11:35:09 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 10579941 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 CEA581709 for ; Wed, 29 Aug 2018 11:35:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 83E962AA9C for ; Wed, 29 Aug 2018 11:35:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 764572ABDA; Wed, 29 Aug 2018 11:35:48 +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=unavailable 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 DCB912AA9C for ; Wed, 29 Aug 2018 11:35:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 656666B4B7C; Wed, 29 Aug 2018 07:35:38 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 607CB6B4B7E; Wed, 29 Aug 2018 07:35:38 -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 484C66B4B7F; Wed, 29 Aug 2018 07:35:38 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-wm0-f72.google.com (mail-wm0-f72.google.com [74.125.82.72]) by kanga.kvack.org (Postfix) with ESMTP id CD9A36B4B7C for ; Wed, 29 Aug 2018 07:35:37 -0400 (EDT) Received: by mail-wm0-f72.google.com with SMTP id r14-v6so2743205wmh.0 for ; Wed, 29 Aug 2018 04:35:37 -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:mime-version :content-transfer-encoding; bh=y36bXvOzaMElAwP/XOX/nFNmvhGdVKzgjaE28JUMW5o=; b=WUMythkGChn6WLS0ib0xFv3WtyM/x/zob2qmRKNVX0o3e6cGJBdwQ65r9XhIenhIle /bnCzfZ6AvnU3GmVwsnJ4t8oCbYtHfG4qh+tUIcx+TrBjUfPvZy02+q6RRDV+gcy0Kjd v3B4QEGdo40pKCXPcxJJLjhOvpRSvJT/Jhjjuhjb4pAbRgFrUN9XbbBh8ZR5KzOCjfVt KN/nsp+ChD6PWjss6YO93MRrSsYSSRNk5DTnz++u4LMpkK+a6VqLDgSu/HVHd9x/wzi/ XH4yoiGIgRJMzms4bKMbP+sGlW6iPG6cfKYGss7OT60fU2YxeuuPJdYv9YsvfbFizFm2 NunQ== X-Gm-Message-State: APzg51BJOnY9dbFZ4pQi0S55sfrQjwWSzxdKpApi3dSwPhkt9aq9pXM1 a8um0Uht5K0jiBSOyxIQLSsBRmoRQIELREIRClqL9sQiX1pIsPb+7fJ9Lv9h8xHRdQ06Bj24Kr9 IMEBvsZlPNVRzGFte5tIKMlqPKqub17gafJpwOPev0xGxINVAW8LahJ+z++sH9/pcgUcbNHAbyv iBBQR8yvBkRVEWdeKd9LO1VVR7aBvb45LyZInDDWM87kSv0jo9u6/zX9vdX8zmgqi30VxiNdw8l NvTTssVq3tOCPHvIpzkv8FlTfkJzeRa0Lzg/0koNL8+X+oyl3tjk9mszSKloJdTWenmrB5cIxVP 0BgoVU2Vmao2HPsoUI8Ti2wRXzrf2XWDToOEC/zZu7NDEYI9ykD4667wAwjIo0Yc2nB6c2g2Vyj D X-Received: by 2002:adf:ed88:: with SMTP id c8-v6mr3983550wro.264.1535542537313; Wed, 29 Aug 2018 04:35:37 -0700 (PDT) X-Received: by 2002:adf:ed88:: with SMTP id c8-v6mr3983513wro.264.1535542536512; Wed, 29 Aug 2018 04:35:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535542536; cv=none; d=google.com; s=arc-20160816; b=vgvLNPBiG5dk6Qe3lTbZVRTdVgSHAqqVb0xx+m4zvKepUxEOONayliHK77wWz+ituB JBUmYRcRVUg543EB5FP3ckTdXaGRRPxKRJDP+LOuoZBCC2dRtF/IdEsjc7hYCUw5JnOK gFkAXUKXypbzqCrG6l9uYTd3/0DWLeBcEU4zp6CuOuUfRkXmvJHU6OweLMsRPNubWvrY EogPgF0s4h76Yv1NDvPTJbys0zFhrb0YIn8rlT7P8whro2nLRdocE+JJn7GFTXxdx7vB LQ1/lr5JyFU3NCJipK8mnjoyJJF41TVGFRb2YWzZ1AiyLRb46sHTOQK78Hnf69k+lvQ4 k0pg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=y36bXvOzaMElAwP/XOX/nFNmvhGdVKzgjaE28JUMW5o=; b=SJLIgMPO40RqFnS7OiqdDbcJ+PsLPDLyafWYf+f+CvYkZHTPJYpWwZ46H2xrOmagVz pOU1z8xp324jfpGkWP78bbRQDuU/wLRTeUo2fJ2LAdL4njP3nc7E9DNFxRNvLAhzmjhB u4f4rIfjftHloy7pwfj37D6V/JneAb3HyPANQ2YsGhrvHkA7UQlMFVe6zzYYhOmN1gPY ExKZ0UnI6Oky1GMbHvDzw3bjZxf60+SnjYV9rDBrKYuq9W+22PYxfZJtmgYpt1bRhN1s UUHyJlJaR0YuE0IMYlIDewznkqrz7o3cIZ0Z06Z+Thbhxn49OWnto9NhxTA4of6c9ZGd batQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=J9lyKo7V; 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 v185-v6sor1102425wma.67.2018.08.29.04.35.36 for (Google Transport Security); Wed, 29 Aug 2018 04:35:36 -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=J9lyKo7V; 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 :mime-version:content-transfer-encoding; bh=y36bXvOzaMElAwP/XOX/nFNmvhGdVKzgjaE28JUMW5o=; b=J9lyKo7VWJ5jsaGwy1wNWax/WJQTvOe9FhDYud9wWj93OpsSu7DwWZsSZXOKoBVILO KPAyje7e5AQ7hfO8pvsIRzbA08oZTVo2DiKUtQXVxd+G8NDg5TtVpy2SWtuseVEcnGvG evNNyx49Q6IjYcQunMadL9ZRoGuPEWDHW8HnvBgxXuM3qx9w/KVTctwf/CAlAHO35Lyb MLUraaA2zKF6IR/MRT7GjoYhU+q8I0cNEAas1+bqk+CoYXEosJnObP4rz2Tj9gxsSIRh D4DFSGIABffoGHKHQPDQsFIniBny68STd7ZdYFa1kbbnspEUWtSqw/eLoqBMT6Ax9vJy JxAg== X-Google-Smtp-Source: ANB0Vdat/kRDBTM8zngqNeGsn+KvYphqXvjyZ5bXDZLZHa+jSCgaLJ+GwsYRrGfyHH7Np21KuGVweA== X-Received: by 2002:a1c:1bca:: with SMTP id b193-v6mr3968967wmb.6.1535542535574; Wed, 29 Aug 2018 04:35:35 -0700 (PDT) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:10:84be:a42a:826d:c530]) by smtp.gmail.com with ESMTPSA id s10-v6sm7800454wmd.22.2018.08.29.04.35.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 29 Aug 2018 04:35:34 -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 Cc: Kostya Serebryany , Evgeniy Stepanov , Lee Smith , Ramana Radhakrishnan , Jacob Bramley , Ruben Ayrapetyan , Jann Horn , Mark Brand , Chintan Pandya , Vishwath Mohan , Andrey Konovalov Subject: [PATCH v6 05/18] khwasan: initialize shadow to 0xff Date: Wed, 29 Aug 2018 13:35:09 +0200 Message-Id: <4dcf8e38bf6ef331e351f0ba4a76c12666f9fc08.1535462971.git.andreyknvl@google.com> X-Mailer: git-send-email 2.19.0.rc0.228.g281dcd1b4d0-goog In-Reply-To: References: MIME-Version: 1.0 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 beb56a26fe9b..1c31bb089154 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -150,6 +150,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); @@ -160,4 +162,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;