linux/mm/kasan
Kuan-Ying Lee 8f17febb34 kasan: infer allocation size by scanning metadata
Make KASAN scan metadata to infer the requested allocation size instead of
printing cache->object_size.

This patch fixes confusing slab-out-of-bounds reports as reported in:

https://bugzilla.kernel.org/show_bug.cgi?id=216457

As an example of the confusing behavior, the report below hints that the
allocation size was 192, while the kernel actually called kmalloc(184):

==================================================================
BUG: KASAN: slab-out-of-bounds in _find_next_bit+0x143/0x160 lib/find_bit.c:109
Read of size 8 at addr ffff8880175766b8 by task kworker/1:1/26
...
The buggy address belongs to the object at ffff888017576600
 which belongs to the cache kmalloc-192 of size 192
The buggy address is located 184 bytes inside of
 192-byte region [ffff888017576600, ffff8880175766c0)
...
Memory state around the buggy address:
 ffff888017576580: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
 ffff888017576600: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
>ffff888017576680: 00 00 00 00 00 00 00 fc fc fc fc fc fc fc fc fc
                                        ^
 ffff888017576700: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
 ffff888017576780: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
==================================================================

With this patch, the report shows:

==================================================================
...
The buggy address belongs to the object at ffff888017576600
 which belongs to the cache kmalloc-192 of size 192
The buggy address is located 0 bytes to the right of
 allocated 184-byte region [ffff888017576600, ffff8880175766b8)
...
==================================================================

Also report slab use-after-free bugs as "slab-use-after-free" and print
"freed" instead of "allocated" in the report when describing the accessed
memory region.

Also improve the metadata-related comment in kasan_find_first_bad_addr
and use addr_has_metadata across KASAN code instead of open-coding
KASAN_SHADOW_START checks.

[akpm@linux-foundation.org: fix printk warning]
Link: https://bugzilla.kernel.org/show_bug.cgi?id=216457
Link: https://lkml.kernel.org/r/20230129021437.18812-1-Kuan-Ying.Lee@mediatek.com
Signed-off-by: Kuan-Ying Lee <Kuan-Ying.Lee@mediatek.com>
Co-developed-by: Andrey Konovalov <andreyknvl@gmail.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>
Cc: Chinwen Chang <chinwen.chang@mediatek.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Matthias Brugger <matthias.bgg@gmail.com>
Cc: Qun-Wei Lin <qun-wei.lin@mediatek.com>
Cc: Vincenzo Frascino <vincenzo.frascino@arm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2023-02-09 16:51:40 -08:00
..
common.c mm/kasan: simplify and refine kasan_cache code 2023-01-18 17:12:55 -08:00
generic.c kasan: infer allocation size by scanning metadata 2023-02-09 16:51:40 -08:00
hw_tags.c kasan: allow sampling page_alloc allocations for HW_TAGS 2023-01-18 17:12:45 -08:00
init.c kasan: use MAX_PTRS_PER_* for early shadow tables 2021-06-29 10:53:53 -07:00
kasan_test_module.c kasan: migrate workqueue_uaf test to kunit 2022-11-08 17:37:14 -08:00
kasan_test.c hardening updates for v6.2-rc1 2022-12-14 12:20:00 -08:00
kasan.h kasan: infer allocation size by scanning metadata 2023-02-09 16:51:40 -08:00
Makefile kasan: move tests to mm/kasan/ 2022-10-03 14:03:02 -07:00
quarantine.c kasan: give better names to shadow values 2022-05-13 07:20:19 -07:00
report_generic.c kasan: infer allocation size by scanning metadata 2023-02-09 16:51:40 -08:00
report_hw_tags.c kasan: infer allocation size by scanning metadata 2023-02-09 16:51:40 -08:00
report_sw_tags.c kasan: infer allocation size by scanning metadata 2023-02-09 16:51:40 -08:00
report_tags.c kasan: infer allocation size by scanning metadata 2023-02-09 16:51:40 -08:00
report.c kasan: infer allocation size by scanning metadata 2023-02-09 16:51:40 -08:00
shadow.c memory: move hotplug memory notifier priority to same file for easy sorting 2022-11-08 17:37:17 -08:00
sw_tags.c kasan: infer allocation size by scanning metadata 2023-02-09 16:51:40 -08:00
tags.c kasan: dynamically allocate stack ring entries 2022-10-03 14:03:02 -07:00