From: Andrey Konovalov <andreyknvl@google.com>
To: Andrey Ryabinin <aryabinin@virtuozzo.com>
Cc: Alexander Potapenko <glider@google.com>,
Dmitry Vyukov <dvyukov@google.com>,
Jonathan Corbet <corbet@lwn.net>,
Catalin Marinas <catalin.marinas@arm.com>,
Will Deacon <will.deacon@arm.com>,
Christoffer Dall <christoffer.dall@linaro.org>,
Marc Zyngier <marc.zyngier@arm.com>,
Christopher Li <sparse@chrisli.org>,
Christoph Lameter <cl@linux.com>,
Pekka Enberg <penberg@kernel.org>,
David Rientjes <rientjes@google.com>,
Joonsoo Kim <iamjoonsoo.kim@lge.com>,
Andrew Morton <akpm@linux-foundation.org>,
Masahiro Yamada <yamada.masahiro@socionext.com>,
Michal Marek <michal.lkml@markovi.net>,
Mark Rutland <mark.rutland@arm.com>,
Ard Biesheuvel <ard.biesheuvel@linaro.org>,
Yury Norov <ynorov@caviumnetworks.com>,
Nick Desaulniers <ndesaulniers@google.com>,
Suzuki K Poulose <suzuki.poulose@arm.com>,
Kristina Martsenko <kristina.martsenko@arm.com>,
Punit Agrawal <punit.agrawal@arm.com>,
Dave Martin <Dave.Martin@arm.com>,
Michael Weiser <michael.weiser@gmx.de>,
James Morse <james.morse@arm.com>,
Julien Thierry <julien.thierry@arm.com>,
Steve Capper <steve.capper@arm.com>,
Tyler Baicar <tbaicar@codeaurora.org>,
"Eric W . Biederman" <ebiederm@xmission.com>,
Stephen Boyd <stephen.boyd@linaro.org>,
Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@kernel.org>,
Paul Lawrence <paullawrence@google.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
David Woodhouse <dwmw@amazon.co.uk>,
Sandipan Das <sandipan@linux.vnet.ibm.com>,
Kees Cook <keescook@chromium.org>,
Herbert Xu <herbert@gondor.apana.org.au>,
Geert Uytterhoeven <geert@linux-m68k.org>,
Josh Poimboeuf <jpoimboe@redhat.com>,
Arnd Bergmann <arnd@arndb.de>,
kasan-dev <kasan-dev@googlegroups.com>,
linux-doc@vger.kernel.org, LKML <linux-kernel@vger.kernel.org>,
Linux ARM <linux-arm-kernel@lists.infradead.org>,
kvmarm@lists.cs.columbia.edu, linux-sparse@vger.kernel.org,
Linux Memory Management List <linux-mm@kvack.org>,
Linux Kbuild mailing list <linux-kbuild@vger.kernel.org>,
Kostya Serebryany <kcc@google.com>,
Evgeniy Stepanov <eugenis@google.com>,
Lee Smith <Lee.Smith@arm.com>,
Ramana Radhakrishnan <Ramana.Radhakrishnan@arm.com>,
Jacob Bramley <Jacob.Bramley@arm.com>,
Ruben Ayrapetyan <Ruben.Ayrapetyan@arm.com>,
Kees Cook <keescook@google.com>, Jann Horn <jannh@google.com>,
Mark Brand <markbrand@google.com>
Subject: Re: [RFC PATCH v2 05/15] khwasan: initialize shadow to 0xff
Date: Tue, 3 Apr 2018 16:43:10 +0200 [thread overview]
Message-ID: <CAAeHK+z37yRzK1Q6F32G=eB6_k+s_YQOoR8LLqNppxZsKMWiZg@mail.gmail.com> (raw)
In-Reply-To: <2ab69c9c-6e80-ea79-e72e-012753ed3db0@virtuozzo.com>
On Fri, Mar 30, 2018 at 6:07 PM, Andrey Ryabinin
<aryabinin@virtuozzo.com> wrote:
> On 03/23/2018 09:05 PM, Andrey Konovalov wrote:
>> 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 <andreyknvl@google.com>
>> ---
>> arch/arm64/mm/kasan_init.c | 11 ++++++++++-
>> include/linux/kasan.h | 8 ++++++++
>> mm/kasan/common.c | 7 +++++++
>> 3 files changed, 25 insertions(+), 1 deletion(-)
>>
>> diff --git a/arch/arm64/mm/kasan_init.c b/arch/arm64/mm/kasan_init.c
>> index dabfc1ecda3d..d4bceba60010 100644
>> --- a/arch/arm64/mm/kasan_init.c
>> +++ b/arch/arm64/mm/kasan_init.c
>> @@ -90,6 +90,10 @@ 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);
>> +#if KASAN_SHADOW_INIT != 0
>> + if (!early)
>> + memset(__va(page_phys), KASAN_SHADOW_INIT, PAGE_SIZE);
>> +#endif
>
> Less ugly way to do the same:
> if (KASAN_SHADOW_INIT != 0 && !early)
> memset(__va(page_phys), KASAN_SHADOW_INIT, PAGE_SIZE);
>
>
> But the right approach here would be allocating uninitialized memory (see memblock_virt_alloc_try_nid_raw())
> and do "if (!early) memset(.., KASAN_SHADOW_INIT, ..)" afterwards.
Will do!
>
>
>> 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 +143,11 @@ 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));
>> +
>> +#if KASAN_SHADOW_INIT != 0
>> + memset(kasan_zero_page, KASAN_SHADOW_INIT, PAGE_SIZE);
>> +#endif
>> +
>
> if (KASAN_SHADOW_INIT)
> memset(...)
>
> Note that, if poisoning of stack variables will work in the same fashion as classic
> KASAN (compiler generated code writes to shadow in function prologue) than content
> of this page will be ruined very fast. Which makes this initialization questionable.
I think I agree with you on this. Since this page immediately gets
dirty and we ignore all reports until proper shadow is set up anyway,
there's no need to initialize it.
>
>
>
>> kasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE,
>> true);
>> }
>> @@ -235,7 +244,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 3c45e273a936..700734dff218 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_CLASSIC
>>
>> +#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_CLASSIC */
>>
>> +#ifdef CONFIG_KASAN_TAGS
>> +
>> +#define KASAN_SHADOW_INIT 0xFF
>> +
>> +#endif /* CONFIG_KASAN_TAGS */
>> +
>> #endif /* LINUX_KASAN_H */
>> diff --git a/mm/kasan/common.c b/mm/kasan/common.c
>> index 08f6c8cb9f84..f4ccb9425655 100644
>> --- a/mm/kasan/common.c
>> +++ b/mm/kasan/common.c
>> @@ -253,6 +253,9 @@ int kasan_module_alloc(void *addr, size_t size)
>> __builtin_return_address(0));
>>
>> if (ret) {
>> +#if KASAN_SHADOW_INIT != 0
>> + __memset(ret, KASAN_SHADOW_INIT, shadow_size);
>> +#endif
>
> Drop __GFP_ZERO from above and remove this #if/#endif.
Will do!
>
>
>> find_vm_area(addr)->flags |= VM_KASAN;
>> kmemleak_ignore(ret);
>> return 0;
>> @@ -297,6 +300,10 @@ static int __meminit kasan_mem_notifier(struct notifier_block *nb,
>> if (!ret)
>> return NOTIFY_BAD;
>>
>> +#if KASAN_SHADOW_INIT != 0
>> + __memset(ret, KASAN_SHADOW_INIT, shadow_end - shadow_start);
>> +#endif
>> +
>
> No need to initialize anything here, kasan_free_pages() will do this later.
OK, will fix this!
>
>
>> kmemleak_ignore(ret);
>> return NOTIFY_OK;
>> }
>>
Thanks!
next prev parent reply other threads:[~2018-04-03 14:43 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-03-23 18:05 [RFC PATCH v2 00/15] khwasan: kernel hardware assisted address sanitizer Andrey Konovalov
2018-03-23 18:05 ` [RFC PATCH v2 01/15] khwasan, mm: change kasan hooks signatures Andrey Konovalov
2018-03-23 18:05 ` [RFC PATCH v2 02/15] khwasan: move common kasan and khwasan code to common.c Andrey Konovalov
2018-03-23 18:05 ` [RFC PATCH v2 03/15] khwasan: add CONFIG_KASAN_CLASSIC and CONFIG_KASAN_TAGS Andrey Konovalov
2018-03-24 8:43 ` Ingo Molnar
2018-03-27 16:23 ` Andrey Konovalov
2018-03-27 20:02 ` Ingo Molnar
2018-03-23 18:05 ` [RFC PATCH v2 04/15] khwasan, arm64: adjust shadow size for CONFIG_KASAN_TAGS Andrey Konovalov
2018-03-23 18:05 ` [RFC PATCH v2 05/15] khwasan: initialize shadow to 0xff Andrey Konovalov
2018-03-30 16:07 ` Andrey Ryabinin
2018-04-03 14:43 ` Andrey Konovalov [this message]
2018-03-23 18:05 ` [RFC PATCH v2 06/15] khwasan, arm64: untag virt address in __kimg_to_phys Andrey Konovalov
2018-03-23 18:05 ` [RFC PATCH v2 07/15] khwasan, arm64: fix up fault handling logic Andrey Konovalov
2018-03-23 18:05 ` [RFC PATCH v2 08/15] khwasan: add tag related helper functions Andrey Konovalov
2018-03-30 16:13 ` Andrey Ryabinin
2018-04-03 14:45 ` Andrey Konovalov
2018-03-23 18:05 ` [RFC PATCH v2 09/15] khwasan, kvm: untag pointers in kern_hyp_va Andrey Konovalov
2018-03-23 18:05 ` [RFC PATCH v2 10/15] khwasan, arm64: enable top byte ignore for the kernel Andrey Konovalov
2018-03-23 18:05 ` [RFC PATCH v2 11/15] khwasan, mm: perform untagged pointers comparison in krealloc Andrey Konovalov
2018-03-24 8:29 ` Ingo Molnar
2018-03-27 12:20 ` Andrey Konovalov
2018-03-27 20:01 ` Ingo Molnar
2018-03-23 18:05 ` [RFC PATCH v2 12/15] khwasan: add bug reporting routines Andrey Konovalov
2018-03-23 18:05 ` [RFC PATCH v2 13/15] khwasan: add hooks implementation Andrey Konovalov
2018-03-30 17:47 ` Andrey Ryabinin
2018-04-03 14:59 ` Andrey Konovalov
2018-04-04 12:39 ` Andrey Ryabinin
2018-04-04 17:00 ` Andrey Konovalov
2018-04-05 13:02 ` Andrey Ryabinin
2018-04-06 12:14 ` Andrey Konovalov
2018-04-06 12:27 ` Andrey Ryabinin
2018-04-10 16:07 ` Andrey Konovalov
2018-04-10 16:31 ` Andrey Ryabinin
2018-04-12 16:45 ` Andrey Konovalov
2018-04-12 17:20 ` Andrey Ryabinin
2018-04-12 17:37 ` Andrey Konovalov
2018-03-23 18:05 ` [RFC PATCH v2 14/15] khwasan, arm64: add brk handler for inline instrumentation Andrey Konovalov
2018-03-23 18:05 ` [RFC PATCH v2 15/15] khwasan: update kasan documentation Andrey Konovalov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAAeHK+z37yRzK1Q6F32G=eB6_k+s_YQOoR8LLqNppxZsKMWiZg@mail.gmail.com' \
--to=andreyknvl@google.com \
--cc=Dave.Martin@arm.com \
--cc=Jacob.Bramley@arm.com \
--cc=Lee.Smith@arm.com \
--cc=Ramana.Radhakrishnan@arm.com \
--cc=Ruben.Ayrapetyan@arm.com \
--cc=akpm@linux-foundation.org \
--cc=ard.biesheuvel@linaro.org \
--cc=arnd@arndb.de \
--cc=aryabinin@virtuozzo.com \
--cc=catalin.marinas@arm.com \
--cc=christoffer.dall@linaro.org \
--cc=cl@linux.com \
--cc=corbet@lwn.net \
--cc=dvyukov@google.com \
--cc=dwmw@amazon.co.uk \
--cc=ebiederm@xmission.com \
--cc=eugenis@google.com \
--cc=geert@linux-m68k.org \
--cc=glider@google.com \
--cc=gregkh@linuxfoundation.org \
--cc=herbert@gondor.apana.org.au \
--cc=iamjoonsoo.kim@lge.com \
--cc=james.morse@arm.com \
--cc=jannh@google.com \
--cc=jpoimboe@redhat.com \
--cc=julien.thierry@arm.com \
--cc=kasan-dev@googlegroups.com \
--cc=kcc@google.com \
--cc=keescook@chromium.org \
--cc=keescook@google.com \
--cc=kristina.martsenko@arm.com \
--cc=kvmarm@lists.cs.columbia.edu \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-sparse@vger.kernel.org \
--cc=marc.zyngier@arm.com \
--cc=mark.rutland@arm.com \
--cc=markbrand@google.com \
--cc=michael.weiser@gmx.de \
--cc=michal.lkml@markovi.net \
--cc=mingo@kernel.org \
--cc=ndesaulniers@google.com \
--cc=paullawrence@google.com \
--cc=penberg@kernel.org \
--cc=punit.agrawal@arm.com \
--cc=rientjes@google.com \
--cc=sandipan@linux.vnet.ibm.com \
--cc=sparse@chrisli.org \
--cc=stephen.boyd@linaro.org \
--cc=steve.capper@arm.com \
--cc=suzuki.poulose@arm.com \
--cc=tbaicar@codeaurora.org \
--cc=tglx@linutronix.de \
--cc=will.deacon@arm.com \
--cc=yamada.masahiro@socionext.com \
--cc=ynorov@caviumnetworks.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox