kasan: improve kasan_non_canonical_hook
authorAndrey Konovalov <andreyknvl@google.com>
Thu, 21 Dec 2023 20:04:45 +0000 (21:04 +0100)
committerAndrew Morton <akpm@linux-foundation.org>
Fri, 29 Dec 2023 19:58:44 +0000 (11:58 -0800)
Make kasan_non_canonical_hook to be more sure in its report (i.e.  say
"probably" instead of "maybe") if the address belongs to the shadow memory
region for kernel addresses.

Also use the kasan_shadow_to_mem helper to calculate the original address.

Also improve the comments in kasan_non_canonical_hook.

Link: https://lkml.kernel.org/r/af94ef3cb26f8c065048b3158d9f20f6102bfaaa.1703188911.git.andreyknvl@google.com
Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Marco Elver <elver@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/kasan/kasan.h
mm/kasan/report.c

index 69e4f5e58e334970ea6b576445ca8899837e0fc0..0e209b823b2c44e75c4bffdd08beb4d68858355a 100644 (file)
@@ -307,6 +307,12 @@ struct kasan_stack_ring {
 
 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
 
+static __always_inline bool addr_in_shadow(const void *addr)
+{
+       return addr >= (void *)KASAN_SHADOW_START &&
+               addr < (void *)KASAN_SHADOW_END;
+}
+
 #ifndef kasan_shadow_to_mem
 static inline const void *kasan_shadow_to_mem(const void *shadow_addr)
 {
index a938237f6882c3c820d886e3becafe7ec3bed923..4bc7ac9fb37d2d04f755611a3a89db53e9a58905 100644 (file)
@@ -635,37 +635,43 @@ void kasan_report_async(void)
 
 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
 /*
- * With CONFIG_KASAN_INLINE, accesses to bogus pointers (outside the high
- * canonical half of the address space) cause out-of-bounds shadow memory reads
- * before the actual access. For addresses in the low canonical half of the
- * address space, as well as most non-canonical addresses, that out-of-bounds
- * shadow memory access lands in the non-canonical part of the address space.
- * Help the user figure out what the original bogus pointer was.
+ * With compiler-based KASAN modes, accesses to bogus pointers (outside of the
+ * mapped kernel address space regions) cause faults when KASAN tries to check
+ * the shadow memory before the actual memory access. This results in cryptic
+ * GPF reports, which are hard for users to interpret. This hook helps users to
+ * figure out what the original bogus pointer was.
  */
 void kasan_non_canonical_hook(unsigned long addr)
 {
        unsigned long orig_addr;
        const char *bug_type;
 
+       /*
+        * All addresses that came as a result of the memory-to-shadow mapping
+        * (even for bogus pointers) must be >= KASAN_SHADOW_OFFSET.
+        */
        if (addr < KASAN_SHADOW_OFFSET)
                return;
 
-       orig_addr = (addr - KASAN_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT;
+       orig_addr = (unsigned long)kasan_shadow_to_mem((void *)addr);
+
        /*
         * For faults near the shadow address for NULL, we can be fairly certain
         * that this is a KASAN shadow memory access.
-        * For faults that correspond to shadow for low canonical addresses, we
-        * can still be pretty sure - that shadow region is a fairly narrow
-        * chunk of the non-canonical address space.
-        * But faults that look like shadow for non-canonical addresses are a
-        * really large chunk of the address space. In that case, we still
-        * print the decoded address, but make it clear that this is not
-        * necessarily what's actually going on.
+        * For faults that correspond to the shadow for low or high canonical
+        * addresses, we can still be pretty sure: these shadow regions are a
+        * fairly narrow chunk of the address space.
+        * But the shadow for non-canonical addresses is a really large chunk
+        * of the address space. For this case, we still print the decoded
+        * address, but make it clear that this is not necessarily what's
+        * actually going on.
         */
        if (orig_addr < PAGE_SIZE)
                bug_type = "null-ptr-deref";
        else if (orig_addr < TASK_SIZE)
                bug_type = "probably user-memory-access";
+       else if (addr_in_shadow((void *)addr))
+               bug_type = "probably wild-memory-access";
        else
                bug_type = "maybe wild-memory-access";
        pr_alert("KASAN: %s in range [0x%016lx-0x%016lx]\n", bug_type,