kasan: allow architectures to provide an outline readiness check
authorDaniel Axtens <dja@axtens.net>
Tue, 29 Jun 2021 02:40:42 +0000 (19:40 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Tue, 29 Jun 2021 17:53:53 +0000 (10:53 -0700)
Allow architectures to define a kasan_arch_is_ready() hook that bails out
of any function that's about to touch the shadow unless the arch says that
it is ready for the memory to be accessed.  This is fairly uninvasive and
should have a negligible performance penalty.

This will only work in outline mode, so an arch must specify
ARCH_DISABLE_KASAN_INLINE if it requires this.

Link: https://lkml.kernel.org/r/20210624034050.511391-3-dja@axtens.net
Signed-off-by: Daniel Axtens <dja@axtens.net>
Reviewed-by: Marco Elver <elver@google.com>
Suggested-by: Christophe Leroy <christophe.leroy@csgroup.eu>
Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com>
Cc: Balbir Singh <bsingharora@gmail.com>
Cc: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
mm/kasan/common.c
mm/kasan/generic.c
mm/kasan/kasan.h
mm/kasan/shadow.c

index 2586d371860079d735c23d2700439b5ed3c3b926..267500896b1edb89619027abde30aa31e0021ee0 100644 (file)
@@ -331,6 +331,9 @@ static inline bool ____kasan_slab_free(struct kmem_cache *cache, void *object,
        u8 tag;
        void *tagged_object;
 
+       if (!kasan_arch_is_ready())
+               return false;
+
        tag = get_tag(object);
        tagged_object = object;
        object = kasan_reset_tag(object);
index 53cbf28859b5a82e251c286201cf713ab8ddc620..c3f5ba7a294a8ae9c988135c24971e02c6f2660f 100644 (file)
@@ -163,6 +163,9 @@ static __always_inline bool check_region_inline(unsigned long addr,
                                                size_t size, bool write,
                                                unsigned long ret_ip)
 {
+       if (!kasan_arch_is_ready())
+               return true;
+
        if (unlikely(size == 0))
                return true;
 
index 8f450bc280458db0f8d9983e96ba227a06626e41..4dbc8def64f491fd401d6d8964739f9cc7329dbd 100644 (file)
@@ -449,6 +449,12 @@ static inline void kasan_poison_last_granule(const void *address, size_t size) {
 
 #endif /* CONFIG_KASAN_GENERIC */
 
+#ifndef kasan_arch_is_ready
+static inline bool kasan_arch_is_ready(void)   { return true; }
+#elif !defined(CONFIG_KASAN_GENERIC) || !defined(CONFIG_KASAN_OUTLINE)
+#error kasan_arch_is_ready only works in KASAN generic outline mode!
+#endif
+
 /*
  * Exported functions for interfaces called from assembly or from generated
  * code. Declarations here to avoid warning about missing declarations.
index 082ee5b6d9a163a68f065844ee769fcce339fdcc..8d95ee52d0194e1862f1410d6bb20d64b20d734d 100644 (file)
@@ -73,6 +73,9 @@ void kasan_poison(const void *addr, size_t size, u8 value, bool init)
 {
        void *shadow_start, *shadow_end;
 
+       if (!kasan_arch_is_ready())
+               return;
+
        /*
         * Perform shadow offset calculation based on untagged address, as
         * some of the callers (e.g. kasan_poison_object_data) pass tagged
@@ -99,6 +102,9 @@ EXPORT_SYMBOL(kasan_poison);
 #ifdef CONFIG_KASAN_GENERIC
 void kasan_poison_last_granule(const void *addr, size_t size)
 {
+       if (!kasan_arch_is_ready())
+               return;
+
        if (size & KASAN_GRANULE_MASK) {
                u8 *shadow = (u8 *)kasan_mem_to_shadow(addr + size);
                *shadow = size & KASAN_GRANULE_MASK;