kasan: add hooks implementation for tag-based mode
[sfrench/cifs-2.6.git] / mm / kasan / tags.c
index 1c4e7ce2e6fe4bb09bf91c52629722adcb98dcf0..1d1b79350e2845671fd2821e7e6d81d8bd4244e9 100644 (file)
@@ -78,15 +78,60 @@ void *kasan_reset_tag(const void *addr)
 void check_memory_region(unsigned long addr, size_t size, bool write,
                                unsigned long ret_ip)
 {
+       u8 tag;
+       u8 *shadow_first, *shadow_last, *shadow;
+       void *untagged_addr;
+
+       if (unlikely(size == 0))
+               return;
+
+       tag = get_tag((const void *)addr);
+
+       /*
+        * Ignore accesses for pointers tagged with 0xff (native kernel
+        * pointer tag) to suppress false positives caused by kmap.
+        *
+        * Some kernel code was written to account for archs that don't keep
+        * high memory mapped all the time, but rather map and unmap particular
+        * pages when needed. Instead of storing a pointer to the kernel memory,
+        * this code saves the address of the page structure and offset within
+        * that page for later use. Those pages are then mapped and unmapped
+        * with kmap/kunmap when necessary and virt_to_page is used to get the
+        * virtual address of the page. For arm64 (that keeps the high memory
+        * mapped all the time), kmap is turned into a page_address call.
+
+        * The issue is that with use of the page_address + virt_to_page
+        * sequence the top byte value of the original pointer gets lost (gets
+        * set to KASAN_TAG_KERNEL (0xFF)).
+        */
+       if (tag == KASAN_TAG_KERNEL)
+               return;
+
+       untagged_addr = reset_tag((const void *)addr);
+       if (unlikely(untagged_addr <
+                       kasan_shadow_to_mem((void *)KASAN_SHADOW_START))) {
+               kasan_report(addr, size, write, ret_ip);
+               return;
+       }
+       shadow_first = kasan_mem_to_shadow(untagged_addr);
+       shadow_last = kasan_mem_to_shadow(untagged_addr + size - 1);
+       for (shadow = shadow_first; shadow <= shadow_last; shadow++) {
+               if (*shadow != tag) {
+                       kasan_report(addr, size, write, ret_ip);
+                       return;
+               }
+       }
 }
 
 #define DEFINE_HWASAN_LOAD_STORE(size)                                 \
        void __hwasan_load##size##_noabort(unsigned long addr)          \
        {                                                               \
+               check_memory_region(addr, size, false, _RET_IP_);       \
        }                                                               \
        EXPORT_SYMBOL(__hwasan_load##size##_noabort);                   \
        void __hwasan_store##size##_noabort(unsigned long addr)         \
        {                                                               \
+               check_memory_region(addr, size, true, _RET_IP_);        \
        }                                                               \
        EXPORT_SYMBOL(__hwasan_store##size##_noabort)
 
@@ -98,15 +143,18 @@ DEFINE_HWASAN_LOAD_STORE(16);
 
 void __hwasan_loadN_noabort(unsigned long addr, unsigned long size)
 {
+       check_memory_region(addr, size, false, _RET_IP_);
 }
 EXPORT_SYMBOL(__hwasan_loadN_noabort);
 
 void __hwasan_storeN_noabort(unsigned long addr, unsigned long size)
 {
+       check_memory_region(addr, size, true, _RET_IP_);
 }
 EXPORT_SYMBOL(__hwasan_storeN_noabort);
 
 void __hwasan_tag_memory(unsigned long addr, u8 tag, unsigned long size)
 {
+       kasan_poison_shadow((void *)addr, size, tag);
 }
 EXPORT_SYMBOL(__hwasan_tag_memory);