]> git.baikalelectronics.ru Git - kernel.git/commitdiff
kasan: separate metadata_fetch_row for each mode
authorAndrey Konovalov <andreyknvl@google.com>
Tue, 22 Dec 2020 20:01:17 +0000 (12:01 -0800)
committerLinus Torvalds <torvalds@linux-foundation.org>
Tue, 22 Dec 2020 20:55:07 +0000 (12:55 -0800)
This is a preparatory commit for the upcoming addition of a new hardware
tag-based (MTE-based) KASAN mode.

Rework print_memory_metadata() to make it agnostic with regard to the way
metadata is stored.  Allow providing a separate metadata_fetch_row()
implementation for each KASAN mode.  Hardware tag-based KASAN will provide
its own implementation that doesn't use shadow memory.

No functional changes for software modes.

Link: https://lkml.kernel.org/r/5fb1ec0152bb1f521505017800387ec3e36ffe18.1606161801.git.andreyknvl@google.com
Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
Signed-off-by: Vincenzo Frascino <vincenzo.frascino@arm.com>
Reviewed-by: Marco Elver <elver@google.com>
Reviewed-by: Alexander Potapenko <glider@google.com>
Tested-by: Vincenzo Frascino <vincenzo.frascino@arm.com>
Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>
Cc: Branislav Rankov <Branislav.Rankov@arm.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Evgenii Stepanov <eugenis@google.com>
Cc: Kevin Brodsky <kevin.brodsky@arm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Will Deacon <will.deacon@arm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
mm/kasan/kasan.h
mm/kasan/report.c
mm/kasan/report_generic.c
mm/kasan/report_sw_tags.c

index c79d30c6fcdbfe45f8a1255c6a392f5fa935ef7b..3b349a6e799dc2d67d15a91c404fc496deb8e153 100644 (file)
 #define KASAN_ABI_VERSION 1
 #endif
 
+/* Metadata layout customization. */
+#define META_BYTES_PER_BLOCK 1
+#define META_BLOCKS_PER_ROW 16
+#define META_BYTES_PER_ROW (META_BLOCKS_PER_ROW * META_BYTES_PER_BLOCK)
+#define META_MEM_BYTES_PER_ROW (META_BYTES_PER_ROW * KASAN_GRANULE_SIZE)
+#define META_ROWS_AROUND_ADDR 2
+
 struct kasan_access_info {
        const void *access_addr;
        const void *first_bad_addr;
@@ -170,6 +177,7 @@ bool check_invalid_free(void *addr);
 
 void *find_first_bad_addr(void *addr, size_t size);
 const char *get_bug_type(struct kasan_access_info *info);
+void metadata_fetch_row(char *buffer, void *row);
 
 #if defined(CONFIG_KASAN_GENERIC) && CONFIG_KASAN_STACK
 void print_address_stack_frame(const void *addr);
index 4148a8bde131212c8c6b3680dc5935be6e635a49..9a6569a57ca28c559ca939a652a27a251b998699 100644 (file)
 #include "kasan.h"
 #include "../slab.h"
 
-/* Metadata layout customization. */
-#define META_BYTES_PER_BLOCK 1
-#define META_BLOCKS_PER_ROW 16
-#define META_BYTES_PER_ROW (META_BLOCKS_PER_ROW * META_BYTES_PER_BLOCK)
-#define META_ROWS_AROUND_ADDR 2
-
 static unsigned long kasan_flags;
 
 #define KASAN_BIT_REPORTED     0
@@ -238,55 +232,59 @@ static void print_address_description(void *addr, u8 tag)
        print_address_stack_frame(addr);
 }
 
-static bool row_is_guilty(const void *row, const void *guilty)
+static bool meta_row_is_guilty(const void *row, const void *addr)
 {
-       return (row <= guilty) && (guilty < row + META_BYTES_PER_ROW);
+       return (row <= addr) && (addr < row + META_MEM_BYTES_PER_ROW);
 }
 
-static int shadow_pointer_offset(const void *row, const void *shadow)
+static int meta_pointer_offset(const void *row, const void *addr)
 {
-       /* The length of ">ff00ff00ff00ff00: " is
-        *    3 + (BITS_PER_LONG/8)*2 chars.
+       /*
+        * Memory state around the buggy address:
+        *  ff00ff00ff00ff00: 00 00 00 05 fe fe fe fe fe fe fe fe fe fe fe fe
+        *  ...
+        *
+        * The length of ">ff00ff00ff00ff00: " is
+        *    3 + (BITS_PER_LONG / 8) * 2 chars.
+        * The length of each granule metadata is 2 bytes
+        *    plus 1 byte for space.
         */
-       return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 +
-               (shadow - row) / META_BYTES_PER_BLOCK + 1;
+       return 3 + (BITS_PER_LONG / 8) * 2 +
+               (addr - row) / KASAN_GRANULE_SIZE * 3 + 1;
 }
 
 static void print_memory_metadata(const void *addr)
 {
        int i;
-       const void *shadow = kasan_mem_to_shadow(addr);
-       const void *shadow_row;
+       void *row;
 
-       shadow_row = (void *)round_down((unsigned long)shadow,
-                                       META_BYTES_PER_ROW)
-               - META_ROWS_AROUND_ADDR * META_BYTES_PER_ROW;
+       row = (void *)round_down((unsigned long)addr, META_MEM_BYTES_PER_ROW)
+                       - META_ROWS_AROUND_ADDR * META_MEM_BYTES_PER_ROW;
 
        pr_err("Memory state around the buggy address:\n");
 
        for (i = -META_ROWS_AROUND_ADDR; i <= META_ROWS_AROUND_ADDR; i++) {
-               const void *kaddr = kasan_shadow_to_mem(shadow_row);
-               char buffer[4 + (BITS_PER_LONG/8)*2];
-               char shadow_buf[META_BYTES_PER_ROW];
+               char buffer[4 + (BITS_PER_LONG / 8) * 2];
+               char metadata[META_BYTES_PER_ROW];
 
                snprintf(buffer, sizeof(buffer),
-                       (i == 0) ? ">%px: " : " %px: ", kaddr);
+                               (i == 0) ? ">%px: " : " %px: ", row);
+
                /*
                 * We should not pass a shadow pointer to generic
                 * function, because generic functions may try to
                 * access kasan mapping for the passed address.
                 */
-               memcpy(shadow_buf, shadow_row, META_BYTES_PER_ROW);
+               metadata_fetch_row(&metadata[0], row);
+
                print_hex_dump(KERN_ERR, buffer,
                        DUMP_PREFIX_NONE, META_BYTES_PER_ROW, 1,
-                       shadow_buf, META_BYTES_PER_ROW, 0);
+                       metadata, META_BYTES_PER_ROW, 0);
 
-               if (row_is_guilty(shadow_row, shadow))
-                       pr_err("%*c\n",
-                               shadow_pointer_offset(shadow_row, shadow),
-                               '^');
+               if (meta_row_is_guilty(row, addr))
+                       pr_err("%*c\n", meta_pointer_offset(row, addr), '^');
 
-               shadow_row += META_BYTES_PER_ROW;
+               row += META_MEM_BYTES_PER_ROW;
        }
 }
 
index 16ed550850e9bbeb0bb534a53c0308ccf59a7992..8a9c889872da3041c31eba2e41b7920aa582c288 100644 (file)
@@ -123,6 +123,11 @@ const char *get_bug_type(struct kasan_access_info *info)
        return get_wild_bug_type(info);
 }
 
+void metadata_fetch_row(char *buffer, void *row)
+{
+       memcpy(buffer, kasan_mem_to_shadow(row), META_BYTES_PER_ROW);
+}
+
 #if CONFIG_KASAN_STACK
 static bool __must_check tokenize_frame_descr(const char **frame_descr,
                                              char *token, size_t max_tok_len,
index c87d5a343b4ef52d56aa19bfae4349d49bf81e4e..add2dfe6169c0bdba57dffa7b8b9f021b214f85e 100644 (file)
@@ -80,6 +80,11 @@ void *find_first_bad_addr(void *addr, size_t size)
        return p;
 }
 
+void metadata_fetch_row(char *buffer, void *row)
+{
+       memcpy(buffer, kasan_mem_to_shadow(row), META_BYTES_PER_ROW);
+}
+
 void print_tags(u8 addr_tag, const void *addr)
 {
        u8 *shadow = (u8 *)kasan_mem_to_shadow(addr);