From patchwork Tue Nov 6 17:30:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 10670927 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 B70D613BF for ; Tue, 6 Nov 2018 17:31:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A27682ABBB for ; Tue, 6 Nov 2018 17:31:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 963AF2ABBE; Tue, 6 Nov 2018 17:31:11 +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 761EA2ABCB for ; Tue, 6 Nov 2018 17:31:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3F5F96B0359; Tue, 6 Nov 2018 12:30:55 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 3CD9B6B035A; Tue, 6 Nov 2018 12:30:55 -0500 (EST) 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 221416B035B; Tue, 6 Nov 2018 12:30:55 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by kanga.kvack.org (Postfix) with ESMTP id BF29E6B0359 for ; Tue, 6 Nov 2018 12:30:54 -0500 (EST) Received: by mail-wm1-f72.google.com with SMTP id t130-v6so8417688wmt.3 for ; Tue, 06 Nov 2018 09:30:54 -0800 (PST) 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=Ce4N/nMYo7+RfxBkmlKo7hExteLUxNUpfT4nrnEM84A=; b=auDhzXTIfmaW3MPl2PBIGzyEmagtR9FkLkcHBPXhbAAKcfuqNhh7V/RyCTGjoTjZ5r MMZ4q6cXtnk8zVTkDITIQVYHe+CvJd9398QsOqWU9wYQqcq4ZWTwAg7q4zj/A0HYX76C B6KKAQ9po43fPjpKR9/Z6VUxDsoYOqiXQnTyleBA6q76hB2qdOwG4Y3/Rpdt2umS39fH 4Q2jIZbgWanzqRA6CU2tEJznctp/SJVw6o6zr+z9DtUUyCXmspHPY26PQeLsss/sl3WL imj7oNX4b5J3k7E6c57EN2x0ONVs/pVT08lEkYAp6uk2YBfrpR9F8gxpjJsk/O+GoY1O +rVQ== X-Gm-Message-State: AGRZ1gKA31YXixhRrRQZkG2uPsYVuki7MjFseEv/wSXVRJb5GuKtL4CE 1SaMHOtDg6405UGnNhWZknaBukGiknskTTtHkBdhpe1AzRLZM0KUF5GvcQCp5GfyjtJU5SNzhyB wpZufkCLBBH8sIxhWS6jBZlXpEjmDknxZzy+ad/XCndd9DCi0DUoawVQBDhuCbPyCekhwq4lXoS Xgl1I/IhJc/vSTJW+bzWSdzbz65YCRjLwnh7c7Liq8uRy8APV68taocX+zmMRv0g88zpo0P7kLC srslviIKBYWDdo0D4RNJWmzFJT1IEhcr/Rio4HU5E3BXr0S7q5qGgz17Rq7P6fg6aTZkVewdHpf NKHCv3LTnt4ixNkvy/npCK6bz3HZpTAP+rXuVndPttetVgvLT+RkmseGFm3fyMqTKXpfCjtZOre 7 X-Received: by 2002:a5d:4bd2:: with SMTP id l18-v6mr25432327wrt.168.1541525454317; Tue, 06 Nov 2018 09:30:54 -0800 (PST) X-Received: by 2002:a5d:4bd2:: with SMTP id l18-v6mr25432272wrt.168.1541525453441; Tue, 06 Nov 2018 09:30:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541525453; cv=none; d=google.com; s=arc-20160816; b=ORsaIHdoNRGb4AMAwL4XKOAyvekOSdC+Z+k9nF2gnis3fb37R5CppU49qwOSjAy1O8 nspKI5trxr/2tMA+6AgPrJS3rtWlZt2kMCgBAtFgrQMH7NhgeyJWfKY9CAed5GJyeH+X ltEqWtxDRvxONqsEpPsqPlxNx5Rs21lWa9jrB2CUW8i4zA8qiRs7l1qlbxd5hxu/bRO+ fIm2QSxMbAYL4udlnmCOrRVgXzCqHYaO+DIxvIDNVLVe9AOrvhJ12WPx0fdSCkrfi8Fj zr9NYztqGevpIa+7fiyJbBc6BaMpm/503HvRS5iXPh70gmsWUyWJVWp2CgDPBLqQeQF2 o5nA== 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; bh=Ce4N/nMYo7+RfxBkmlKo7hExteLUxNUpfT4nrnEM84A=; b=dz4/KqFop3VzCKc1giRQVYJ5Zoid0lPMFMni8RxI1a3sJeTcoNlLPyC8i+GnrejYE4 cXRu3yMRIMBT9k6HpSp2vS3qImXJvlK0F5ia5y0rG++Am9YjLeA+7sY6c1/tmDF0AY6d FayAkzZhfD6pU++LgAUTiNRpBqyRKdw2zZMJX4spxIE5MgNaNxeQIIAgfzrP05+x+FPV 29KLrZgV37vJ/3z9sK5jmzO4ZPMns+6i77Kq8pMuSvMqTHcRRpc86tBmS0DT1+LToDM3 bhurc7JBqRohcRBjg7zTf7f37EdpaMP2VRc6NTQNeaPs2wO+mncNXCBDykHlwNjnrYYB AXYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Vsfw56KN; 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 3-v6sor1645468wmd.3.2018.11.06.09.30.53 for (Google Transport Security); Tue, 06 Nov 2018 09:30:53 -0800 (PST) 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=Vsfw56KN; 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=Ce4N/nMYo7+RfxBkmlKo7hExteLUxNUpfT4nrnEM84A=; b=Vsfw56KNSA0j9i4jZTEMw40brJDN+L6Vk6DCkdODL+7mPErtmtlQkMDqok/+LxZTIA Fft8ZXVpiqCJCP8miE5QzIVVR0eJpPs/kLTUgPPTN8npCXvrIHnQywVjXmvpIh5HPIAe Wa7BMy38S7p9H8D/B9xnivP/Ilz8RTzDI0NFXnMY4KF1cUTUopybY81MTIEXSbkQ8k4b +E58TE1QbFn70oHBRkLa8Che+5cPhJvjnPLMHITMfgwQzHhRskF+aYV9mO2sNYi/cltS EV/yZHL9/gfcte5RkSa8/Haz7iYyw4HKPiuISIuf7vrZZhH59ApyKYhy7WYnKkpEF4Va 9sRw== X-Google-Smtp-Source: AJdET5dN2RP29VYyaLlKwxLPMus2CsNsK+huRP1o94zV2LhR/oELAyawrtwYhu4G/QM2rYIsYijVRg== X-Received: by 2002:a1c:d181:: with SMTP id i123-v6mr2789337wmg.124.1541525452833; Tue, 06 Nov 2018 09:30:52 -0800 (PST) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:10:84be:a42a:826d:c530]) by smtp.gmail.com with ESMTPSA id z17-v6sm16328416wrm.65.2018.11.06.09.30.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Nov 2018 09:30:52 -0800 (PST) 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 v10 07/22] kasan: initialize shadow to 0xff for tag-based mode Date: Tue, 6 Nov 2018 18:30:22 +0100 Message-Id: <9405f32797b52616cd0746bcea37df94e8e4256a.1541525354.git.andreyknvl@google.com> X-Mailer: git-send-email 2.19.1.930.g4563a0d9d0-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 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. Reviewed-by: Andrey Ryabinin Reviewed-by: Dmitry Vyukov 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 63527e585aac..18ebc8994a7b 100644 --- a/arch/arm64/mm/kasan_init.c +++ b/arch/arm64/mm/kasan_init.c @@ -43,6 +43,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_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) { @@ -88,7 +97,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))); @@ -138,6 +149,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); } @@ -234,7 +246,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;