The patch titled Subject: kasan: add bug reporting routines for tag-based mode has been added to the -mm tree. Its filename is kasan-add-bug-reporting-routines-for-tag-based-mode.patch This patch should soon appear at http://ozlabs.org/~akpm/mmots/broken-out/kasan-add-bug-reporting-routines-for-tag-based-mode.patch and later at http://ozlabs.org/~akpm/mmotm/broken-out/kasan-add-bug-reporting-routines-for-tag-based-mode.patch Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next and is updated there every 3-4 working days ------------------------------------------------------ From: Andrey Konovalov <andreyknvl@xxxxxxxxxx> Subject: kasan: add bug reporting routines for tag-based mode This commit adds rountines, that print tag-based KASAN error reports. Those are quite similar to generic KASAN, the difference is: 1. The way tag-based KASAN finds the first bad shadow cell (with a mismatching tag). Tag-based KASAN compares memory tags from the shadow memory to the pointer tag. 2. Tag-based KASAN reports all bugs with the "KASAN: invalid-access" header. Also simplify generic KASAN find_first_bad_addr. Link: http://lkml.kernel.org/r/996c09ec2c8f11294c106973f3b1a211417fa74e.1543337629.git.andreyknvl@xxxxxxxxxx Signed-off-by: Andrey Konovalov <andreyknvl@xxxxxxxxxx> Reviewed-by: Andrey Ryabinin <aryabinin@xxxxxxxxxxxxx> Reviewed-by: Dmitry Vyukov <dvyukov@xxxxxxxxxx> Cc: Mark Rutland <mark.rutland@xxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- --- a/mm/kasan/generic_report.c~kasan-add-bug-reporting-routines-for-tag-based-mode +++ a/mm/kasan/generic_report.c @@ -33,16 +33,13 @@ #include "kasan.h" #include "../slab.h" -static const void *find_first_bad_addr(const void *addr, size_t size) +void *find_first_bad_addr(void *addr, size_t size) { - u8 shadow_val = *(u8 *)kasan_mem_to_shadow(addr); - const void *first_bad_addr = addr; + void *p = addr; - while (!shadow_val && first_bad_addr < addr + size) { - first_bad_addr += KASAN_SHADOW_SCALE_SIZE; - shadow_val = *(u8 *)kasan_mem_to_shadow(first_bad_addr); - } - return first_bad_addr; + while (p < addr + size && !(*(u8 *)kasan_mem_to_shadow(p))) + p += KASAN_SHADOW_SCALE_SIZE; + return p; } static const char *get_shadow_bug_type(struct kasan_access_info *info) @@ -50,9 +47,6 @@ static const char *get_shadow_bug_type(s const char *bug_type = "unknown-crash"; u8 *shadow_addr; - info->first_bad_addr = find_first_bad_addr(info->access_addr, - info->access_size); - shadow_addr = (u8 *)kasan_mem_to_shadow(info->first_bad_addr); /* --- a/mm/kasan/kasan.h~kasan-add-bug-reporting-routines-for-tag-based-mode +++ a/mm/kasan/kasan.h @@ -119,6 +119,7 @@ void kasan_poison_shadow(const void *add void check_memory_region(unsigned long addr, size_t size, bool write, unsigned long ret_ip); +void *find_first_bad_addr(void *addr, size_t size); const char *get_bug_type(struct kasan_access_info *info); void kasan_report(unsigned long addr, size_t size, @@ -139,10 +140,14 @@ static inline void quarantine_remove_cac #ifdef CONFIG_KASAN_SW_TAGS +void print_tags(u8 addr_tag, const void *addr); + u8 random_tag(void); #else +static inline void print_tags(u8 addr_tag, const void *addr) { } + static inline u8 random_tag(void) { return 0; --- a/mm/kasan/report.c~kasan-add-bug-reporting-routines-for-tag-based-mode +++ a/mm/kasan/report.c @@ -64,11 +64,10 @@ static int __init kasan_set_multi_shot(c } __setup("kasan_multi_shot", kasan_set_multi_shot); -static void print_error_description(struct kasan_access_info *info, - const char *bug_type) +static void print_error_description(struct kasan_access_info *info) { pr_err("BUG: KASAN: %s in %pS\n", - bug_type, (void *)info->ip); + get_bug_type(info), (void *)info->ip); pr_err("%s of size %zu at addr %px by task %s/%d\n", info->is_write ? "Write" : "Read", info->access_size, info->access_addr, current->comm, task_pid_nr(current)); @@ -272,6 +271,8 @@ void kasan_report_invalid_free(void *obj start_report(&flags); pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", (void *)ip); + print_tags(get_tag(object), reset_tag(object)); + object = reset_tag(object); pr_err("\n"); print_address_description(object); pr_err("\n"); @@ -279,41 +280,45 @@ void kasan_report_invalid_free(void *obj end_report(&flags); } -static void kasan_report_error(struct kasan_access_info *info) -{ - unsigned long flags; - - start_report(&flags); - - print_error_description(info, get_bug_type(info)); - pr_err("\n"); - - if (!addr_has_shadow(info->access_addr)) { - dump_stack(); - } else { - print_address_description((void *)info->access_addr); - pr_err("\n"); - print_shadow_for_address(info->first_bad_addr); - } - - end_report(&flags); -} - void kasan_report(unsigned long addr, size_t size, bool is_write, unsigned long ip) { struct kasan_access_info info; + void *tagged_addr; + void *untagged_addr; + unsigned long flags; if (likely(!report_enabled())) return; disable_trace_on_warning(); - info.access_addr = (void *)addr; - info.first_bad_addr = (void *)addr; + tagged_addr = (void *)addr; + untagged_addr = reset_tag(tagged_addr); + + info.access_addr = tagged_addr; + if (addr_has_shadow(untagged_addr)) + info.first_bad_addr = find_first_bad_addr(tagged_addr, size); + else + info.first_bad_addr = untagged_addr; info.access_size = size; info.is_write = is_write; info.ip = ip; - kasan_report_error(&info); + start_report(&flags); + + print_error_description(&info); + if (addr_has_shadow(untagged_addr)) + print_tags(get_tag(tagged_addr), info.first_bad_addr); + pr_err("\n"); + + if (addr_has_shadow(untagged_addr)) { + print_address_description(untagged_addr); + pr_err("\n"); + print_shadow_for_address(info.first_bad_addr); + } else { + dump_stack(); + } + + end_report(&flags); } --- a/mm/kasan/tags_report.c~kasan-add-bug-reporting-routines-for-tag-based-mode +++ a/mm/kasan/tags_report.c @@ -37,3 +37,21 @@ const char *get_bug_type(struct kasan_ac { return "invalid-access"; } + +void *find_first_bad_addr(void *addr, size_t size) +{ + u8 tag = get_tag(addr); + void *p = reset_tag(addr); + void *end = p + size; + + while (p < end && tag == *(u8 *)kasan_mem_to_shadow(p)) + p += KASAN_SHADOW_SCALE_SIZE; + return p; +} + +void print_tags(u8 addr_tag, const void *addr) +{ + u8 *shadow = (u8 *)kasan_mem_to_shadow(addr); + + pr_err("Pointer tag: [%02x], memory tag: [%02x]\n", addr_tag, *shadow); +} _ Patches currently in -mm which might be from andreyknvl@xxxxxxxxxx are kasan-mm-change-hooks-signatures.patch kasan-slub-handle-pointer-tags-in-early_kmem_cache_node_alloc.patch kasan-move-common-generic-and-tag-based-code-to-commonc.patch kasan-rename-source-files-to-reflect-the-new-naming-scheme.patch kasan-add-config_kasan_generic-and-config_kasan_sw_tags.patch kasan-arm64-adjust-shadow-size-for-tag-based-mode.patch kasan-rename-kasan_zero_page-to-kasan_early_shadow_page.patch kasan-initialize-shadow-to-0xff-for-tag-based-mode.patch arm64-move-untagged_addr-macro-from-uaccessh-to-memoryh.patch kasan-add-tag-related-helper-functions.patch kasan-arm64-untag-address-in-_virt_addr_is_linear.patch kasan-preassign-tags-to-objects-with-ctors-or-slab_typesafe_by_rcu.patch kasan-arm64-fix-up-fault-handling-logic.patch kasan-arm64-enable-top-byte-ignore-for-the-kernel.patch kasan-mm-perform-untagged-pointers-comparison-in-krealloc.patch kasan-split-out-generic_reportc-from-reportc.patch kasan-add-bug-reporting-routines-for-tag-based-mode.patch mm-move-obj_to_index-to-include-linux-slab_defh.patch kasan-add-hooks-implementation-for-tag-based-mode.patch kasan-arm64-add-brk-handler-for-inline-instrumentation.patch kasan-mm-arm64-tag-non-slab-memory-allocated-via-pagealloc.patch kasan-add-__must_check-annotations-to-kasan-hooks.patch kasan-arm64-select-have_arch_kasan_sw_tags.patch kasan-update-documentation.patch kasan-add-spdx-license-identifier-mark-to-source-files.patch