From patchwork Tue May 8 17:20:51 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 10386613 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 7FBC360353 for ; Tue, 8 May 2018 17:21:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6D3EA290B3 for ; Tue, 8 May 2018 17:21:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5F73A290B9; Tue, 8 May 2018 17:21:32 +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 BE1F6290B3 for ; Tue, 8 May 2018 17:21:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2021F6B02C4; Tue, 8 May 2018 13:21:22 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 1B2E26B02C5; Tue, 8 May 2018 13:21:22 -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 006BB6B02C6; Tue, 8 May 2018 13:21:21 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-wm0-f69.google.com (mail-wm0-f69.google.com [74.125.82.69]) by kanga.kvack.org (Postfix) with ESMTP id 9982C6B02C4 for ; Tue, 8 May 2018 13:21:21 -0400 (EDT) Received: by mail-wm0-f69.google.com with SMTP id t195-v6so1319470wmt.9 for ; Tue, 08 May 2018 10:21:21 -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:in-reply-to:references; bh=IFwqCsYeJvS9ff1sRi33loEFnxudx8Y7zwbp4h1TJhc=; b=Xa4Vfzn9MrFsmlenHQEjiuSchzpVzvJNXavKjT9ca6c4xuH0YEEkrJ3iVoIUsjxTyu Dzfx8xytTWVW87XQ1/zjEYhKCRL/6tEngXQFXYLiQrjq3qLx+GBnpkbC+9l9L7Ftx2rG jRgccjm9m67FBxDWNUTFpruiRUiZM6uvT+eoGxtP5zefGnA2ey3lyu8bASQQ9DjIffhU XHnvnzUBOX5jGwmBnsqhH1Kd5ciHUSlaSDTL2azHIyGL3sklrWzbF14cmOK4orJ8FkQn OW4V2okCj9nCdtwzFDJm+bUqHl7nOh54cPxZnqUcDC9xFmZSuUiRB7Ox4uVBPV/hFPki /MzA== X-Gm-Message-State: ALQs6tAuii5Nzbuc+4vvvc97HY4GKgm5h/EeoJiQo6umA1Nrntx/RWl+ 6xdXaDluTDr1Zh4Zi57pMneG9ktv3+buaOi7tAXNj5oyWaQ3B/TE9XtRI0rlwA4Rfjs5FMYmKlp hujF2btIdzkECJ7iGtXTev6yL7ihXEGv2ZzwK+bzKFkL80d0j1tccfDP1kOKzEQHKcWeNxKUZLP VzC5L3a9WjxY/1Fsb4XZIsFo6wx3gKYN4jFKscJSnu4Tn7GrZnz/A8Aoh3n7GpE8WR501FqHBmA nEuxVDgjZpQwKSGITRYqn8N/5QQgsB9TgUHun/hgj+jcyA9stOZ2VXUIVWPSuJ7czdiAMcgZiOS cO314ueB7tN/B0xB3iMfHx31u6FdVHthqAsO1Y7kOupLS8xouCHDyy3bMr/WJmpBnMHa1UpDS0C 1 X-Received: by 2002:adf:8e44:: with SMTP id n62-v6mr33809735wrb.159.1525800081195; Tue, 08 May 2018 10:21:21 -0700 (PDT) X-Received: by 2002:adf:8e44:: with SMTP id n62-v6mr33809678wrb.159.1525800080418; Tue, 08 May 2018 10:21:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525800080; cv=none; d=google.com; s=arc-20160816; b=Og8aEyNAixxf2xJgRVZeVZWnoHofbSfHSTbRGaYxwBjWAovuFBm6Q23alIUOqRdIZH aBEWVtG2z+yWCEN6Z6uYuTq2RMKNjFaRHCzM/PxHf7sygdfER0AhNyS8jJ3WREgT52yc AO9SjFR+jEYtVJwpHGpjZ4W0AFCbiWptzbdCgdaAMi8+47/PlTuRfeEJrQ1I395gj/fB a0U9RN8txVX7mn3xKz0VQpFYlGphgYZz6VUfyltBCtXcrq83DXO+fkupiANwEmIhOPOK hYx25oiiRQFBGqACAQXPLy2ZImuhtcqIdNS70cgaAufeSTimSsunprAkgZfVjKEjBGcy kZXw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=IFwqCsYeJvS9ff1sRi33loEFnxudx8Y7zwbp4h1TJhc=; b=k7Dn0nl8uhenskLJVTpK7DDStUlOorXCkiAQEuHYneLuVG7FWyalRQXCJgHin/77t2 mnrniDmoYlztMqyZWmtCQ6cCkZSYOvLpY/bIiVW4Xv+XHocFd0EpsYCZDyHh7y8K/s2m ooDS10D8TiAKyB8fGFFfiK05q7iHWQbZAEBHJ+pMc3aqMfcOzgRhBvQyBs0pAqkNAlUz u+cIrgE2z9gWoEoSC1KqO7/QTFuJo6S9tMLWsQX28fXvRmf0DxRhDSPPsh6ZelFJm63F UtWS83q8J4Zg/T8e9WL/DL3NwQxnhmW6Z99DZI4RhrkCc+KleOhqClj11e/LVUa6EfJu 7k1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ImakKwvs; 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 q16-v6sor11741413wre.26.2018.05.08.10.21.20 for (Google Transport Security); Tue, 08 May 2018 10:21:20 -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=ImakKwvs; 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 :in-reply-to:references; bh=IFwqCsYeJvS9ff1sRi33loEFnxudx8Y7zwbp4h1TJhc=; b=ImakKwvsWpoVHwnKv5r/DYC6/Ov9u1NqhfxFXfi+uLn1fDeuijwEIxvxNEDnRyKTV6 dRN9pd8g6N0jb5RBo5Xs0/MMyfi5Ridc+RHqaH0dWhfwgCIf1cl24uSrBcAuRx/zAOjA ZdzfvGRdVkE+4n1+qli3SdeiAse90Ax1XEpZZROutRMyBYQwGQZiMXoMPSLVVJCUFqgT xZTsnsILRQZ6O0pTR3zl8IHW1clMmXpD8UQcqoTM3MsMIi9NgvCZWTQ8Nc6pxzrvIV+U axtVpHwFQOOAz26ol5oQaOcPZMbF1qQGrndK75UiofGHng/3EUjfb1tya4Xk8EdQ5Fmr RPxA== X-Google-Smtp-Source: AB8JxZpS9fs6QZsV1CqH8H8ummhsjhlQo38urUnCzGDtigeOD05VINaZv84Zs4Pf+D3CUHRCY2mhRw== X-Received: by 2002:adf:bc8f:: with SMTP id g15-v6mr1522875wrh.266.1525800079439; Tue, 08 May 2018 10:21:19 -0700 (PDT) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:10:84be:a42a:826d:c530]) by smtp.gmail.com with ESMTPSA id m134sm14178311wmg.4.2018.05.08.10.21.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 May 2018 10:21:18 -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 v1 05/16] khwasan: initialize shadow to 0xff Date: Tue, 8 May 2018 19:20:51 +0200 Message-Id: <7c1197bce60a0d18c178ab5f025c438afa84be21.1525798754.git.andreyknvl@google.com> X-Mailer: git-send-email 2.17.0.441.gb46fe60e1d-goog In-Reply-To: References: 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 ebb48415e4cf..0c1159feaf5e 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -454,11 +454,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;