Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp785891imm; Fri, 21 Sep 2018 08:16:12 -0700 (PDT) X-Google-Smtp-Source: ANB0VdalcplJzF/dfhog2UfeHMaoodRyLqgaMZkx1FGc3XMOwXhOwWOOBRG8LhOoc7pH1ifewiob X-Received: by 2002:a62:59d5:: with SMTP id k82-v6mr46555139pfj.143.1537542972436; Fri, 21 Sep 2018 08:16:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537542972; cv=none; d=google.com; s=arc-20160816; b=seD5xwzVCu5adTGg0iLUhta0KbesyIvtmYfnMjBrRx4cA+JIaZm6axKMB3XHYj+Kuv Wau/V46/XRj9g0c0KPzZKbeICABF9+EP4Fw6gS4lZrkBgQ0s8mh8+eRug1CKq5c16G3p WerPtoLK/WsJu0OpRQrb5lApW2v5biXVkC/4tT42hfONMo6JPAD8BkKWCfHib8SNLPNQ Jc3YGaLJo9mO4urScwCTDb+Znkh+03QbrJ/UNGcKGHqGIAeAjhRnSn29X/67P93p4I87 wsw9E/IO0hMzELNAytJ8gyP0xWq28VP7kApNuIqN/bF2oZYGsd8mcdmzg+i7zjxtC/VF 4x6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=SbjvhGeXQsWKb08SRsNz/w6wYlXn5JQLAhAa4N66qYU=; b=oUBCMOHIIvCRIQ/t4SPUQYsiZLlPEruL+O5bLMWbuBS3y8VN3gNK5vpUyb/OIRTHus PP0r66edJ5zs39diF8kjS3fbGLDXq8YMoJBzKtLKbQmNm66/x98c429k/aW9Ras3DiyE mZ5yHfr0VV42JUsYBUcEgJsmYE/I8gcEBeo84y0NjuH+n9rX3n7/oE5Pa6CZs09J+sRy rCH2OpDi61pI1e6JeCd8lWdW9A9sBobF0fRU8bConr0qR+X8fKTBf/7KwxETzgu19Nb8 gOjGkv6m9sm+Dmh8f6VWRTHWj338UOL9ZbJGi02WZj2rbrRvivY9/uM3W1FL1I5T5Ti7 cr9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=JjuEEyf1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f17-v6si743425pgf.660.2018.09.21.08.15.46; Fri, 21 Sep 2018 08:16:12 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=JjuEEyf1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390975AbeIUVDV (ORCPT + 99 others); Fri, 21 Sep 2018 17:03:21 -0400 Received: from mail-wm1-f66.google.com ([209.85.128.66]:38132 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390865AbeIUVDS (ORCPT ); Fri, 21 Sep 2018 17:03:18 -0400 Received: by mail-wm1-f66.google.com with SMTP id t25-v6so3670023wmi.3 for ; Fri, 21 Sep 2018 08:13:57 -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=SbjvhGeXQsWKb08SRsNz/w6wYlXn5JQLAhAa4N66qYU=; b=JjuEEyf1owSKCYFWbs7XCtnvgKm79jdW7FWamYxBcWjEOi/mfajAoBePG30i9CJ8lv b0FtdEyFw5VQ/6tm7qol93f6Tn5tMe90A64dNtXJPOnXq5VOWVx7eOt0vgXBVIXCSGlO g7umH53OvppU1QEcm/D5X/B5Dt7Wn0fUUDJggnOEugRgt72oMgi7l0lXSXwpmz6s9MKA Vb+DXawV/TiS5E2AI1MoAOpVw2Bnjy4wcObM+3pVK2Zaq/59GvB8r6Ax2I5HhQ7hRDW7 FSwrpsAzJHs0DEmxu+qe3aenh9mF/J7WkjCkKQk9wpMFIx1XX9X0ElEQX4I76CWE2cbZ hXcg== 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=SbjvhGeXQsWKb08SRsNz/w6wYlXn5JQLAhAa4N66qYU=; b=RhUtJm88wzDjBZyEsAt0LM/nkffARxV6RC0Ml2tr5CPOejWqNjha8m4du+RnfrMdNL U5snUhcrAkRMphSZmp/mYJspDmOaIeBT8XcpNkqKuJxaVOT/WXtGhP2D87EZvuvZJ3jq tXnCWSgegoZIuME4ygOtm6J9GKZ9RKGamFV+WNz/11i8Nx2MAsCSTAOzRpoXe9bGAFhp x5HBYW7OYAac6VdgfFrHoEhsC00fVHEn3sopqorFrmPvKx+vXLAJ7GwaA6OStf7Yulpu S+B8ZH9v5MKD7Sf3SN8LXY6dCMvBOddgwrDjsbskC6J57wHzCNlgj0tDxvsEwsDbF9lp Llzg== X-Gm-Message-State: APzg51DKtHDkC9P2S93vLrBYJ5kKneZ1TFa27EGySw1srTokLf2fC5rU jf9K8dnCQ2D6bZz+b4BV8OcKvQ== X-Received: by 2002:a7b:c04c:: with SMTP id u12-v6mr7969658wmc.24.1537542836804; Fri, 21 Sep 2018 08:13:56 -0700 (PDT) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:10:84be:a42a:826d:c530]) by smtp.gmail.com with ESMTPSA id e7-v6sm27990271wru.46.2018.09.21.08.13.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 21 Sep 2018 08:13:56 -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 v9 06/20] kasan: initialize shadow to 0xff for tag-based mode Date: Fri, 21 Sep 2018 17:13:28 +0200 Message-Id: <4021682747e0fdc24ffffd5c22f006b74d4d4089.1537542735.git.andreyknvl@google.com> X-Mailer: git-send-email 2.19.0.444.g18242da7ef-goog In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org A tag-based KASAN 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 tag-based KASAN we need to initialize shadow memory to 0xff. 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 b66fdf5ea7ab..7f6574c35c62 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -153,6 +153,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); @@ -163,4 +165,10 @@ static inline void kasan_cache_shutdown(struct kmem_cache *cache) {} #endif /* CONFIG_KASAN_GENERIC */ +#ifdef CONFIG_KASAN_SW_TAGS + +#define KASAN_SHADOW_INIT 0xFF + +#endif /* CONFIG_KASAN_SW_TAGS */ + #endif /* LINUX_KASAN_H */ diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 5f68c93734ba..7134e75447ff 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; -- 2.19.0.444.g18242da7ef-goog