The patch titled Subject: kasan, mm: reset tags when accessing metadata has been added to the -mm tree. Its filename is kasan-mm-reset-tags-when-accessing-metadata.patch This patch should soon appear at https://ozlabs.org/~akpm/mmots/broken-out/kasan-mm-reset-tags-when-accessing-metadata.patch and later at https://ozlabs.org/~akpm/mmotm/broken-out/kasan-mm-reset-tags-when-accessing-metadata.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, mm: reset tags when accessing metadata Kernel allocator code accesses metadata for slab objects, that may lie out-of-bounds of the object itself, or be accessed when an object is freed. Such accesses trigger tag faults and lead to false-positive reports with hardware tag-based KASAN. Software KASAN modes disable instrumentation for allocator code via KASAN_SANITIZE Makefile macro, and rely on kasan_enable/disable_current() annotations which are used to ignore KASAN reports. With hardware tag-based KASAN neither of those options are available, as it doesn't use compiler instrumetation, no tag faults are ignored, and MTE is disabled after the first one. Instead, reset tags when accessing metadata (currently only for SLUB). Link: https://lkml.kernel.org/r/d0060171a76ace31b26fef1f2713da209099fb99.1605046192.git.andreyknvl@xxxxxxxxxx Signed-off-by: Andrey Konovalov <andreyknvl@xxxxxxxxxx> Signed-off-by: Vincenzo Frascino <vincenzo.frascino@xxxxxxx> Acked-by: Marco Elver <elver@xxxxxxxxxx> Cc: Alexander Potapenko <glider@xxxxxxxxxx> Cc: Andrey Ryabinin <aryabinin@xxxxxxxxxxxxx> Cc: Branislav Rankov <Branislav.Rankov@xxxxxxx> Cc: Catalin Marinas <catalin.marinas@xxxxxxx> Cc: Dmitry Vyukov <dvyukov@xxxxxxxxxx> Cc: Evgenii Stepanov <eugenis@xxxxxxxxxx> Cc: Kevin Brodsky <kevin.brodsky@xxxxxxx> Cc: Vasily Gorbik <gor@xxxxxxxxxxxxx> Cc: Will Deacon <will.deacon@xxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- mm/page_alloc.c | 4 +++- mm/page_poison.c | 2 +- mm/slub.c | 29 ++++++++++++++++------------- 3 files changed, 20 insertions(+), 15 deletions(-) --- a/mm/page_alloc.c~kasan-mm-reset-tags-when-accessing-metadata +++ a/mm/page_alloc.c @@ -1202,8 +1202,10 @@ static void kernel_init_free_pages(struc /* s390's use of memset() could override KASAN redzones. */ kasan_disable_current(); - for (i = 0; i < numpages; i++) + for (i = 0; i < numpages; i++) { + page_kasan_tag_reset(page + i); clear_highpage(page + i); + } kasan_enable_current(); } --- a/mm/page_poison.c~kasan-mm-reset-tags-when-accessing-metadata +++ a/mm/page_poison.c @@ -25,7 +25,7 @@ static void poison_page(struct page *pag /* KASAN still think the page is in-use, so skip it. */ kasan_disable_current(); - memset(addr, PAGE_POISON, PAGE_SIZE); + memset(kasan_reset_tag(addr), PAGE_POISON, PAGE_SIZE); kasan_enable_current(); kunmap_atomic(addr); } --- a/mm/slub.c~kasan-mm-reset-tags-when-accessing-metadata +++ a/mm/slub.c @@ -250,7 +250,7 @@ static inline void *freelist_ptr(const s { #ifdef CONFIG_SLAB_FREELIST_HARDENED /* - * When CONFIG_KASAN_SW_TAGS is enabled, ptr_addr might be tagged. + * When CONFIG_KASAN_SW/HW_TAGS is enabled, ptr_addr might be tagged. * Normally, this doesn't cause any issues, as both set_freepointer() * and get_freepointer() are called with a pointer with the same tag. * However, there are some issues with CONFIG_SLUB_DEBUG code. For @@ -276,6 +276,7 @@ static inline void *freelist_dereference static inline void *get_freepointer(struct kmem_cache *s, void *object) { + object = kasan_reset_tag(object); return freelist_dereference(s, object + s->offset); } @@ -305,6 +306,7 @@ static inline void set_freepointer(struc BUG_ON(object == fp); /* naive detection of double free or corruption */ #endif + freeptr_addr = (unsigned long)kasan_reset_tag((void *)freeptr_addr); *(void **)freeptr_addr = freelist_ptr(s, fp, freeptr_addr); } @@ -539,8 +541,8 @@ static void print_section(char *level, c unsigned int length) { metadata_access_enable(); - print_hex_dump(level, text, DUMP_PREFIX_ADDRESS, 16, 1, addr, - length, 1); + print_hex_dump(level, kasan_reset_tag(text), DUMP_PREFIX_ADDRESS, + 16, 1, addr, length, 1); metadata_access_disable(); } @@ -571,7 +573,7 @@ static struct track *get_track(struct km p = object + get_info_end(s); - return p + alloc; + return kasan_reset_tag(p + alloc); } static void set_track(struct kmem_cache *s, void *object, @@ -584,7 +586,8 @@ static void set_track(struct kmem_cache unsigned int nr_entries; metadata_access_enable(); - nr_entries = stack_trace_save(p->addrs, TRACK_ADDRS_COUNT, 3); + nr_entries = stack_trace_save(kasan_reset_tag(p->addrs), + TRACK_ADDRS_COUNT, 3); metadata_access_disable(); if (nr_entries < TRACK_ADDRS_COUNT) @@ -748,7 +751,7 @@ static __printf(3, 4) void slab_err(stru static void init_object(struct kmem_cache *s, void *object, u8 val) { - u8 *p = object; + u8 *p = kasan_reset_tag(object); if (s->flags & SLAB_RED_ZONE) memset(p - s->red_left_pad, val, s->red_left_pad); @@ -778,7 +781,7 @@ static int check_bytes_and_report(struct u8 *addr = page_address(page); metadata_access_enable(); - fault = memchr_inv(start, value, bytes); + fault = memchr_inv(kasan_reset_tag(start), value, bytes); metadata_access_disable(); if (!fault) return 1; @@ -874,7 +877,7 @@ static int slab_pad_check(struct kmem_ca pad = end - remainder; metadata_access_enable(); - fault = memchr_inv(pad, POISON_INUSE, remainder); + fault = memchr_inv(kasan_reset_tag(pad), POISON_INUSE, remainder); metadata_access_disable(); if (!fault) return 1; @@ -1119,7 +1122,7 @@ void setup_page_debug(struct kmem_cache return; metadata_access_enable(); - memset(addr, POISON_INUSE, page_size(page)); + memset(kasan_reset_tag(addr), POISON_INUSE, page_size(page)); metadata_access_disable(); } @@ -1572,10 +1575,10 @@ static inline bool slab_free_freelist_ho * Clear the object and the metadata, but don't touch * the redzone. */ - memset(object, 0, s->object_size); + memset(kasan_reset_tag(object), 0, s->object_size); rsize = (s->flags & SLAB_RED_ZONE) ? s->red_left_pad : 0; - memset((char *)object + s->inuse, 0, + memset((char *)kasan_reset_tag(object) + s->inuse, 0, s->size - s->inuse - rsize); } @@ -2891,10 +2894,10 @@ redo: stat(s, ALLOC_FASTPATH); } - maybe_wipe_obj_freeptr(s, object); + maybe_wipe_obj_freeptr(s, kasan_reset_tag(object)); if (unlikely(slab_want_init_on_alloc(gfpflags, s)) && object) - memset(object, 0, s->object_size); + memset(kasan_reset_tag(object), 0, s->object_size); out: slab_post_alloc_hook(s, objcg, gfpflags, 1, &object); _ Patches currently in -mm which might be from andreyknvl@xxxxxxxxxx are kasan-drop-unnecessary-gpl-text-from-comment-headers.patch kasan-kasan_vmalloc-depends-on-kasan_generic.patch kasan-group-vmalloc-code.patch s390-kasan-include-asm-pageh-from-asm-kasanh.patch kasan-shadow-declarations-only-for-software-modes.patch kasan-rename-unpoison_shadow-to-unpoison_memory.patch kasan-rename-kasan_shadow_-to-kasan_granule_.patch kasan-only-build-initc-for-software-modes.patch kasan-split-out-shadowc-from-commonc.patch kasan-define-kasan_granule_page.patch kasan-rename-report-and-tags-files.patch kasan-dont-duplicate-config-dependencies.patch kasan-hide-invalid-free-check-implementation.patch kasan-decode-stack-frame-only-with-kasan_stack_enable.patch kasan-arm64-only-init-shadow-for-software-modes.patch kasan-arm64-only-use-kasan_depth-for-software-modes.patch kasan-arm64-move-initialization-message.patch kasan-arm64-rename-kasan_init_tags-and-mark-as-__init.patch kasan-rename-addr_has_shadow-to-addr_has_metadata.patch kasan-rename-print_shadow_for_address-to-print_memory_metadata.patch kasan-kasan_non_canonical_hook-only-for-software-modes.patch kasan-rename-shadow-layout-macros-to-meta.patch kasan-separate-metadata_fetch_row-for-each-mode.patch kasan-arm64-dont-allow-sw_tags-with-arm64_mte.patch kasan-introduce-config_kasan_hw_tags.patch arm64-kasan-align-allocations-for-hw_tags.patch arm64-kasan-add-arch-layer-for-memory-tagging-helpers.patch kasan-define-kasan_granule_size-for-hw_tags.patch kasan-x86-s390-update-undef-config_kasan.patch kasan-arm64-expand-config_kasan-checks.patch kasan-arm64-implement-hw_tags-runtime.patch kasan-arm64-print-report-from-tag-fault-handler.patch kasan-mm-reset-tags-when-accessing-metadata.patch kasan-arm64-enable-config_kasan_hw_tags.patch kasan-add-documentation-for-hardware-tag-based-mode.patch