From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 696D9C4338F for ; Wed, 4 Aug 2021 12:44:33 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id DCBFA60F22 for ; Wed, 4 Aug 2021 12:44:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org DCBFA60F22 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 512636B00B4; Wed, 4 Aug 2021 08:44:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4C2FE8D005F; Wed, 4 Aug 2021 08:44:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3B0F86B00B6; Wed, 4 Aug 2021 08:44:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0188.hostedemail.com [216.40.44.188]) by kanga.kvack.org (Postfix) with ESMTP id 200886B00B4 for ; Wed, 4 Aug 2021 08:44:32 -0400 (EDT) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id B4EFD18033779 for ; Wed, 4 Aug 2021 12:44:31 +0000 (UTC) X-FDA: 78437366742.28.120A19C Received: from mail-ej1-f43.google.com (mail-ej1-f43.google.com [209.85.218.43]) by imf21.hostedemail.com (Postfix) with ESMTP id 66ACAD019636 for ; Wed, 4 Aug 2021 12:44:31 +0000 (UTC) Received: by mail-ej1-f43.google.com with SMTP id e19so3478356ejs.9 for ; Wed, 04 Aug 2021 05:44:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=rHpM1wB8GC7B0gndgKLXatkqmK6m0EnOPisfXQMQMVs=; b=KB7qH+Qi2MihcmOrId/b+1JPq1nNYa8yJnPN4pHgd/UkXmLoc5+vtEmrKojo3mN7ag rogzWJZQJzGt0oeT5F3iQOn5FowESBrgCfAm1g2BGtY/w1MTc7RaO813ueQQKhZL1XPC aiNCcLemuIlBAMawSlpxd0lQuudzwR5jR8+E1uO1LqABqtyapj6bFbHILx/atswIdyVf x86I/WkXRRjRn+H6tOpt/AibL8pca5QMEHKFcuGgEKZLoTumf954rfP7dquKZv+QMOZB pQnGKgsSqTc4P24eGUMldCVAEttM6SlJxUiWdkJAqeKTKVUbai4uQoB7ymmL+DHf5jBF XVuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=rHpM1wB8GC7B0gndgKLXatkqmK6m0EnOPisfXQMQMVs=; b=aeewXC+KXwvOzqU39U9qmX5pmWFkFSykRrfzBWc2YuZqszAedw2lFEw5cFoMNr/tZW JZkx1LwndoBkWgLBFuE3w1QrdX0d+OaIe4qQQ7Wy8CL50zfk+jyMBoF/r6WXzZdjnt7+ hwRwWj43+Q+HWwFxFxVb/DWHptA37kOZ2F6v4FTx763flja7W089XKi8Se95IlHLq+d7 qjzgJv3+M0vkkOUkNKYHWuW6W/IRFY7fdGjOD/fLy+oK18yBJSS5/9bWW/0W9OwhW73v viLSv94OUKq5Vn13QAVkJbl3xM2bxmhE9KWXCBaLMEDn6qNzDohtWjVpFFOhBT12dkkX 1cQg== X-Gm-Message-State: AOAM530reBL+0N+B9ESJKP6yz7QpFQ2xgrqexTHdq7M2yTx3WaFAzBq8 rWKZeGJoLkJY8e4V0e7j7LxsHpbq5GeMYXavfck= X-Google-Smtp-Source: ABdhPJxLXyga9pGI26y+2jHG2/O5eyxA3lfBtQ75gsUCrsRnEPZpuBvoYtBWGjhtF1HKkw9sbPQ62MCSUI8JseT0vG4= X-Received: by 2002:a17:906:f112:: with SMTP id gv18mr25819921ejb.439.1628081070045; Wed, 04 Aug 2021 05:44:30 -0700 (PDT) MIME-Version: 1.0 References: <20210804090957.12393-1-Kuan-Ying.Lee@mediatek.com> <20210804090957.12393-2-Kuan-Ying.Lee@mediatek.com> In-Reply-To: <20210804090957.12393-2-Kuan-Ying.Lee@mediatek.com> From: Andrey Konovalov Date: Wed, 4 Aug 2021 14:44:19 +0200 Message-ID: Subject: Re: [PATCH v3 1/2] kasan, kmemleak: reset tags when scanning block To: Kuan-Ying Lee Cc: Nicholas Tang , Andrew Yang , Andrey Ryabinin , Alexander Potapenko , Marco Elver , Catalin Marinas , Chinwen Chang , Andrew Morton , kasan-dev , Linux Memory Management List , LKML , Linux ARM , "moderated list:ARM/Mediatek SoC support" Content-Type: text/plain; charset="UTF-8" X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 66ACAD019636 Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20161025 header.b=KB7qH+Qi; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf21.hostedemail.com: domain of andreyknvl@gmail.com designates 209.85.218.43 as permitted sender) smtp.mailfrom=andreyknvl@gmail.com X-Stat-Signature: 184hy1kbm519wgwnsmafj66aaiujiwx4 X-HE-Tag: 1628081071-944610 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Wed, Aug 4, 2021 at 11:10 AM Kuan-Ying Lee wrote: > > Kmemleak need to scan kernel memory to check memory leak. > With hardware tag-based kasan enabled, when it scans on > the invalid slab and dereference, the issue will occur > as below. > > Hardware tag-based KASAN doesn't use compiler instrumentation, we > can not use kasan_disable_current() to ignore tag check. > > Based on the below report, there are 11 0xf7 granules, which amounts to > 176 bytes, and the object is allocated from the kmalloc-256 cache. So > when kmemleak accesses the last 256-176 bytes, it causes faults, as > those are marked with KASAN_KMALLOC_REDZONE == KASAN_TAG_INVALID == > 0xfe. > > Thus, we reset tags before accessing metadata to avoid from false positives. > > [ 151.905804] ================================================================== > [ 151.907120] BUG: KASAN: out-of-bounds in scan_block+0x58/0x170 > [ 151.908773] Read at addr f7ff0000c0074eb0 by task kmemleak/138 > [ 151.909656] Pointer tag: [f7], memory tag: [fe] > [ 151.910195] > [ 151.910876] CPU: 7 PID: 138 Comm: kmemleak Not tainted 5.14.0-rc2-00001-g8cae8cd89f05-dirty #134 > [ 151.912085] Hardware name: linux,dummy-virt (DT) > [ 151.912868] Call trace: > [ 151.913211] dump_backtrace+0x0/0x1b0 > [ 151.913796] show_stack+0x1c/0x30 > [ 151.914248] dump_stack_lvl+0x68/0x84 > [ 151.914778] print_address_description+0x7c/0x2b4 > [ 151.915340] kasan_report+0x138/0x38c > [ 151.915804] __do_kernel_fault+0x190/0x1c4 > [ 151.916386] do_tag_check_fault+0x78/0x90 > [ 151.916856] do_mem_abort+0x44/0xb4 > [ 151.917308] el1_abort+0x40/0x60 > [ 151.917754] el1h_64_sync_handler+0xb4/0xd0 > [ 151.918270] el1h_64_sync+0x78/0x7c > [ 151.918714] scan_block+0x58/0x170 > [ 151.919157] scan_gray_list+0xdc/0x1a0 > [ 151.919626] kmemleak_scan+0x2ac/0x560 > [ 151.920129] kmemleak_scan_thread+0xb0/0xe0 > [ 151.920635] kthread+0x154/0x160 > [ 151.921115] ret_from_fork+0x10/0x18 > [ 151.921717] > [ 151.922077] Allocated by task 0: > [ 151.922523] kasan_save_stack+0x2c/0x60 > [ 151.923099] __kasan_kmalloc+0xec/0x104 > [ 151.923502] __kmalloc+0x224/0x3c4 > [ 151.924172] __register_sysctl_paths+0x200/0x290 > [ 151.924709] register_sysctl_table+0x2c/0x40 > [ 151.925175] sysctl_init+0x20/0x34 > [ 151.925665] proc_sys_init+0x3c/0x48 > [ 151.926136] proc_root_init+0x80/0x9c > [ 151.926547] start_kernel+0x648/0x6a4 > [ 151.926987] __primary_switched+0xc0/0xc8 > [ 151.927557] > [ 151.927994] Freed by task 0: > [ 151.928340] kasan_save_stack+0x2c/0x60 > [ 151.928766] kasan_set_track+0x2c/0x40 > [ 151.929173] kasan_set_free_info+0x44/0x54 > [ 151.929568] ____kasan_slab_free.constprop.0+0x150/0x1b0 > [ 151.930063] __kasan_slab_free+0x14/0x20 > [ 151.930449] slab_free_freelist_hook+0xa4/0x1fc > [ 151.930924] kfree+0x1e8/0x30c > [ 151.931285] put_fs_context+0x124/0x220 > [ 151.931731] vfs_kern_mount.part.0+0x60/0xd4 > [ 151.932280] kern_mount+0x24/0x4c > [ 151.932686] bdev_cache_init+0x70/0x9c > [ 151.933122] vfs_caches_init+0xdc/0xf4 > [ 151.933578] start_kernel+0x638/0x6a4 > [ 151.934014] __primary_switched+0xc0/0xc8 > [ 151.934478] > [ 151.934757] The buggy address belongs to the object at ffff0000c0074e00 > [ 151.934757] which belongs to the cache kmalloc-256 of size 256 > [ 151.935744] The buggy address is located 176 bytes inside of > [ 151.935744] 256-byte region [ffff0000c0074e00, ffff0000c0074f00) > [ 151.936702] The buggy address belongs to the page: > [ 151.937378] page:(____ptrval____) refcount:1 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x100074 > [ 151.938682] head:(____ptrval____) order:2 compound_mapcount:0 compound_pincount:0 > [ 151.939440] flags: 0xbfffc0000010200(slab|head|node=0|zone=2|lastcpupid=0xffff|kasantag=0x0) > [ 151.940886] raw: 0bfffc0000010200 0000000000000000 dead000000000122 f5ff0000c0002300 > [ 151.941634] raw: 0000000000000000 0000000000200020 00000001ffffffff 0000000000000000 > [ 151.942353] page dumped because: kasan: bad access detected > [ 151.942923] > [ 151.943214] Memory state around the buggy address: > [ 151.943896] ffff0000c0074c00: f0 f0 f0 f0 f0 f0 f0 f0 f0 fe fe fe fe fe fe fe > [ 151.944857] ffff0000c0074d00: fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe > [ 151.945892] >ffff0000c0074e00: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 fe fe fe fe fe > [ 151.946407] ^ > [ 151.946939] ffff0000c0074f00: fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe > [ 151.947445] ffff0000c0075000: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb > [ 151.947999] ================================================================== > [ 151.948524] Disabling lock debugging due to kernel taint > [ 156.434569] kmemleak: 181 new suspected memory leaks (see /sys/kernel/debug/kmemleak) > > Signed-off-by: Kuan-Ying Lee > Acked-by: Catalin Marinas > Cc: Marco Elver > Cc: Andrey Konovalov > --- > mm/kmemleak.c | 6 +++--- > 1 file changed, 3 insertions(+), 3 deletions(-) > > diff --git a/mm/kmemleak.c b/mm/kmemleak.c > index 228a2fbe0657..73d46d16d575 100644 > --- a/mm/kmemleak.c > +++ b/mm/kmemleak.c > @@ -290,7 +290,7 @@ static void hex_dump_object(struct seq_file *seq, > warn_or_seq_printf(seq, " hex dump (first %zu bytes):\n", len); > kasan_disable_current(); > warn_or_seq_hex_dump(seq, DUMP_PREFIX_NONE, HEX_ROW_SIZE, > - HEX_GROUP_SIZE, ptr, len, HEX_ASCII); > + HEX_GROUP_SIZE, kasan_reset_tag((void *)ptr), len, HEX_ASCII); > kasan_enable_current(); > } > > @@ -1171,7 +1171,7 @@ static bool update_checksum(struct kmemleak_object *object) > > kasan_disable_current(); > kcsan_disable_current(); > - object->checksum = crc32(0, (void *)object->pointer, object->size); > + object->checksum = crc32(0, kasan_reset_tag((void *)object->pointer), object->size); > kasan_enable_current(); > kcsan_enable_current(); > > @@ -1246,7 +1246,7 @@ static void scan_block(void *_start, void *_end, > break; > > kasan_disable_current(); > - pointer = *ptr; > + pointer = *(unsigned long *)kasan_reset_tag((void *)ptr); > kasan_enable_current(); > > untagged_ptr = (unsigned long)kasan_reset_tag((void *)pointer); > -- > 2.18.0 > Reviewed-by: Andrey Konovalov