From patchwork Tue Jan 3 07:55:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?S3Vhbi1ZaW5nIExlZSAo5p2O5Yag56mOKQ==?= X-Patchwork-Id: 13087293 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 480DFC46467 for ; Tue, 3 Jan 2023 07:57:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 945AA8E0003; Tue, 3 Jan 2023 02:57:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8F4E58E0001; Tue, 3 Jan 2023 02:57:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7BC988E0003; Tue, 3 Jan 2023 02:57:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 69E0F8E0001 for ; Tue, 3 Jan 2023 02:57:00 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 3133D140AA8 for ; Tue, 3 Jan 2023 07:57:00 +0000 (UTC) X-FDA: 80312731800.23.1C288C9 Received: from mailgw02.mediatek.com (mailgw02.mediatek.com [216.200.240.185]) by imf05.hostedemail.com (Postfix) with ESMTP id B04C6100008 for ; Tue, 3 Jan 2023 07:56:57 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=mediatek.com header.s=dk header.b=nR+3dsVK; spf=pass (imf05.hostedemail.com: domain of kuan-ying.lee@mediatek.com designates 216.200.240.185 as permitted sender) smtp.mailfrom=kuan-ying.lee@mediatek.com; dmarc=pass (policy=quarantine) header.from=mediatek.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1672732618; a=rsa-sha256; cv=none; b=x6A/9/A3g/R/3/7E3mP56USbMKbg5oWsFE3UyrBWCR1og2sGs7Y6SCTIW21UFkb1BqISPy usPTfHNnHtxcRXrX1wU0fvnNgx16uPJaOcFzygqWp4NNdQymnlczHPIdOALpZtqhUl6ddd PCD+gRJBuZYfUDLfweB0FSF1ufHD0Mw= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=mediatek.com header.s=dk header.b=nR+3dsVK; spf=pass (imf05.hostedemail.com: domain of kuan-ying.lee@mediatek.com designates 216.200.240.185 as permitted sender) smtp.mailfrom=kuan-ying.lee@mediatek.com; dmarc=pass (policy=quarantine) header.from=mediatek.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1672732618; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=NZ9NhCNssNEj8jHwgbBdhsDJ/+YeWKw4bYC90Z9LVjk=; b=J3fsTTxmnHpJXVAEYeUGq6lL/WvHQsdQgr28tX1Ftjpt7zgx+yKp9/K7gNkyu514HjAMrZ VmC4oPvfTfzbY5V1roKYU0Q9KsFoKs2hlFzLq4O+rgI2i3I9ukUiXqVRQwR0pMDEwNKjEc 5OoQJn7gv6TqYwS+u3GnIPjllG+lQ6I= X-UUID: d6b2201e69d447cda0eca82cc9643180-20230103 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:MIME-Version:Message-ID:Date:Subject:CC:To:From; bh=NZ9NhCNssNEj8jHwgbBdhsDJ/+YeWKw4bYC90Z9LVjk=; b=nR+3dsVKmbCAehJGYivb98X9J3AJZcILW7qt46EDi1sdadR8Lz1yW3Ud+EMun+f4MSMpb6siI89X6f3VLiLZ2INahYO18CfCTVZla7dWIqc/c5VJmClEsltdKAPFFt/mJUdig4q4beRKbolNSB/dotPG5yxbACkhltTu216nJtU=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.16,REQID:949d3e2a-e1ea-486b-9078-8df6da0d89d0,IP:0,U RL:0,TC:0,Content:0,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION: release,TS:0 X-CID-META: VersionHash:09771b1,CLOUDID:d1b9a3f4-ff42-4fb0-b929-626456a83c14,B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:-3,IP:nil,U RL:1,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0 X-CID-BVR: 0 X-UUID: d6b2201e69d447cda0eca82cc9643180-20230103 Received: from mtkmbs11n2.mediatek.inc [(172.21.101.187)] by mailgw02.mediatek.com (envelope-from ) (musrelay.mediatek.com ESMTP with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 1420441518; Tue, 03 Jan 2023 00:56:51 -0700 Received: from mtkmbs11n2.mediatek.inc (172.21.101.187) by mtkmbs10n2.mediatek.inc (172.21.101.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.792.3; Tue, 3 Jan 2023 15:56:18 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by mtkmbs11n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.792.15 via Frontend Transport; Tue, 3 Jan 2023 15:56:18 +0800 From: Kuan-Ying Lee To: Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , "Andrew Morton" , Matthias Brugger CC: , , Kuan-Ying Lee , , , , , Subject: [PATCH] kasan: infer the requested size by scanning shadow memory Date: Tue, 3 Jan 2023 15:55:58 +0800 Message-ID: <20230103075603.12294-1-Kuan-Ying.Lee@mediatek.com> X-Mailer: git-send-email 2.18.0 MIME-Version: 1.0 X-MTK: N X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: B04C6100008 X-Stat-Signature: byr8zuhhkg1txhyu1jcbwbsgnk3nbj17 X-HE-Tag: 1672732617-687564 X-HE-Meta: U2FsdGVkX1+DMEkX5dsypj736HR2BsWZsmkY+1tR6QdFlnkfFK7aZRe9w2x2h3bjDtdkVztoA6d8ID3uYrsynTBRESEuGflYuuBrFhOTCAx3gJL2wro+eef4P/vRofpGFSgkuKkBUXxjYRGYpvXf/Nwb9MDqLeTVcmnFuG6iGuyx0rqN++0KuZSJ5/f3FU4V+XNfE1G9hxaV8G/QHMtSFH4aubbyVecT2FzDsz4jpx+YY8IDrB69I/c/KkGQzj8dD6cSsrxJrv69harlo4lox0vI7ReUAFWCAn+1bptTRB1ef4e2tk3Q002PS6i5ZL9LvUEqrAXOwKdst4Oujxvfg81M8FHCaoO9GRN76HTMCpB3xbViQI1qkp93T4u7qvgcWOwlB+VTuxGd0DCiiLKxthmLsJQKdYjHNQ0CB2Qk5zShplOyUqFycRfxrH2qlYtvuEl8RsCi8ZmS/yv59644ORKBYGfYLoZGefnemgv2UKauzhl1Iw1RmEG4/XPkMf9LUHA6LPXnPn5U1w2jgwsAE02xSOudKgsVBKBk2IbbFpVRVp/6tkeKSTETzIlLkUQWMudSk3TpTrh4KFBM6U0Xv48A2lIPeJGnOk6CmOXtTHQhZhLfDQkiu/wsARiL4l0uVZp+Smt+a43oOv7lJFL8eKix5hpCh26nwP7/+aXKkDS+TqlqMWFCidMUCsZeff4rJL1NwCoU/GSbN4A0VY4BQWO/TCIL4MHWu8rcM53n3i+Bktee6eKIEFaynTxXyJswkq7F9abytIR4m+PNQhqRoFBhdrRQ0vei33WSSx76hTeZe2qvirlAkll582VTgh6jLOUWkrAQG21Wzl2rHz7Vbdq10gECeFBsF8DoJd6B8vYG8AxFRL66Rw2vm+8MrMCdF1GRlwGMwaMcfIHXbZeYSGVSWcA5NaqWepcEP34q1R1H8bZpBrWH7KfXLrZzJgCxLMpOXMoPiE8= 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: We scan the shadow memory to infer the requested size instead of printing cache->object_size directly. This patch will fix the confusing generic kasan report like below. [1] Report shows "cache kmalloc-192 of size 192", but user actually 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 ================================================================== After this patch, report will show "cache kmalloc-192 of size 184". Link: https://bugzilla.kernel.org/show_bug.cgi?id=216457 [1] Signed-off-by: Kuan-Ying Lee --- mm/kasan/kasan.h | 5 +++++ mm/kasan/report.c | 3 ++- mm/kasan/report_generic.c | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index 32413f22aa82..7bb627d21580 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -340,8 +340,13 @@ static inline void kasan_print_address_stack_frame(const void *addr) { } #ifdef CONFIG_KASAN_GENERIC void kasan_print_aux_stacks(struct kmem_cache *cache, const void *object); +int kasan_get_alloc_size(void *object_addr, struct kmem_cache *cache); #else static inline void kasan_print_aux_stacks(struct kmem_cache *cache, const void *object) { } +static inline int kasan_get_alloc_size(void *object_addr, struct kmem_cache *cache) +{ + return cache->object_size; +} #endif bool kasan_report(unsigned long addr, size_t size, diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 1d02757e90a3..6de454bb2cad 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -236,12 +236,13 @@ static void describe_object_addr(const void *addr, struct kmem_cache *cache, { unsigned long access_addr = (unsigned long)addr; unsigned long object_addr = (unsigned long)object; + int real_size = kasan_get_alloc_size((void *)object_addr, cache); const char *rel_type; int rel_bytes; pr_err("The buggy address belongs to the object at %px\n" " which belongs to the cache %s of size %d\n", - object, cache->name, cache->object_size); + object, cache->name, real_size); if (access_addr < object_addr) { rel_type = "to the left"; diff --git a/mm/kasan/report_generic.c b/mm/kasan/report_generic.c index 043c94b04605..01b38e459352 100644 --- a/mm/kasan/report_generic.c +++ b/mm/kasan/report_generic.c @@ -43,6 +43,24 @@ void *kasan_find_first_bad_addr(void *addr, size_t size) return p; } +int kasan_get_alloc_size(void *addr, struct kmem_cache *cache) +{ + int size = 0; + u8 *shadow = (u8 *)kasan_mem_to_shadow(addr); + + while (size < cache->object_size) { + if (*shadow == 0) + size += KASAN_GRANULE_SIZE; + else if (*shadow >= 1 && *shadow <= KASAN_GRANULE_SIZE - 1) + size += *shadow; + else + return size; + shadow++; + } + + return cache->object_size; +} + static const char *get_shadow_bug_type(struct kasan_report_info *info) { const char *bug_type = "unknown-crash";