From patchwork Mon Aug 10 07:23:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Walter Wu X-Patchwork-Id: 11707037 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CB2E8138C for ; Mon, 10 Aug 2020 07:23:24 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 959612073E for ; Mon, 10 Aug 2020 07:23:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=mediatek.com header.i=@mediatek.com header.b="mUc+I3UY" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 959612073E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=mediatek.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id AD4C76B0003; Mon, 10 Aug 2020 03:23:23 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id A84536B0005; Mon, 10 Aug 2020 03:23:23 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 99C068D0001; Mon, 10 Aug 2020 03:23:23 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0211.hostedemail.com [216.40.44.211]) by kanga.kvack.org (Postfix) with ESMTP id 84E6E6B0003 for ; Mon, 10 Aug 2020 03:23:23 -0400 (EDT) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 40DC5180AD804 for ; Mon, 10 Aug 2020 07:23:23 +0000 (UTC) X-FDA: 77133818286.05.swim30_231029e26fd8 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin05.hostedemail.com (Postfix) with ESMTP id 16CFA1801788A for ; Mon, 10 Aug 2020 07:23:23 +0000 (UTC) X-Spam-Summary: 1,0,0,2c570c6272c3c1d3,d41d8cd98f00b204,walter-zh.wu@mediatek.com,,RULES_HIT:41:355:379:421:541:800:960:966:973:988:989:1185:1260:1277:1311:1313:1314:1345:1431:1437:1514:1515:1516:1518:1535:1544:1585:1711:1730:1747:1777:1792:1981:2194:2196:2198:2199:2200:2201:2393:2559:2562:2731:2907:3138:3139:3140:3141:3142:3355:3865:3867:3868:3870:3871:3872:4119:4250:4321:4385:4605:5007:6261:6653:8660:8957:10004:11026:11473:11658:11914:12043:12048:12291:12296:12297:12438:12521:12555:12895:13148:13230:14181:14394:14721:21080:21451:21627:21939:21990:30029:30054,0,RBL:210.61.82.183:@mediatek.com:.lbl8.mailshell.net-64.100.201.201 62.2.12.100;04yfttsfk7owmxaj6hpyyr9az3xbyycdiozbaydzgp4qxdpfgaj6s6b4hiiaewj.7rrowa7t394cerxtp1hdreccdzitjwpag3ba4kakbwbfntjpar5s64d1mo3jpb8.s-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: swim30_231029e26fd8 X-Filterd-Recvd-Size: 8313 Received: from mailgw01.mediatek.com (unknown [210.61.82.183]) by imf43.hostedemail.com (Postfix) with ESMTP for ; Mon, 10 Aug 2020 07:23:21 +0000 (UTC) X-UUID: a2a2e3ec4d1e4852ae6c1321513c1dbc-20200810 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Transfer-Encoding:Content-Type:MIME-Version:Message-ID:Date:Subject:CC:To:From; bh=PpSc/tEQuCx3Ee+gj4Ko/cEUAbvYmxsK1TcRUj0zqIE=; b=mUc+I3UYkH6D5bMqSwM0Z2C483rqHVq6lSsH5OADMnuNFWEb24s6QtIuT7rCeJ8hUKIWjczy1obNaHtGA892W56rwWuDunYSHh8s1suqhZ37d64RoeuqFBThFO1sTDEGfKEBmxQyzEGN12cUqmhGgTFZbtR1/SH6t4IRnZLkDic=; X-UUID: a2a2e3ec4d1e4852ae6c1321513c1dbc-20200810 Received: from mtkcas10.mediatek.inc [(172.21.101.39)] by mailgw01.mediatek.com (envelope-from ) (Cellopoint E-mail Firewall v4.1.10 Build 0809 with TLS) with ESMTP id 1729549305; Mon, 10 Aug 2020 15:23:16 +0800 Received: from mtkcas08.mediatek.inc (172.21.101.126) by mtkmbs01n2.mediatek.inc (172.21.101.79) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 10 Aug 2020 15:23:13 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by mtkcas08.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Mon, 10 Aug 2020 15:23:13 +0800 From: Walter Wu To: Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Matthias Brugger , John Stultz , Stephen Boyd , Andrew Morton CC: , , , , wsd_upstream , , Walter Wu , Thomas Gleixner Subject: [PATCH 1/5] timer: kasan: record and print timer stack Date: Mon, 10 Aug 2020 15:23:13 +0800 Message-ID: <20200810072313.529-1-walter-zh.wu@mediatek.com> X-Mailer: git-send-email 2.18.0 MIME-Version: 1.0 X-TM-SNTS-SMTP: 98F01DCE42DA20CE4E3012D87E3ABB3D61BAAF8BDBEAA7561BC5D251A16DF3722000:8 X-MTK: N X-Rspamd-Queue-Id: 16CFA1801788A X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam01 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: This patch records the last two timer queueing stacks and prints up to 2 timer stacks in KASAN report. It is useful for programmers to solve use-after-free or double-free memory timer issues. When timer_setup() or timer_setup_on_stack() is called, then it prepares to use this timer and sets timer callback, we store this call stack in order to print it in KASAN report. Signed-off-by: Walter Wu Cc: Andrey Ryabinin Cc: Dmitry Vyukov Cc: Alexander Potapenko Cc: Thomas Gleixner Cc: John Stultz Cc: Stephen Boyd Cc: Andrew Morton --- include/linux/kasan.h | 2 ++ kernel/time/timer.c | 2 ++ mm/kasan/generic.c | 21 +++++++++++++++++++++ mm/kasan/kasan.h | 4 +++- mm/kasan/report.c | 11 +++++++++++ 5 files changed, 39 insertions(+), 1 deletion(-) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index 23b7ee00572d..763664b36dc6 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -175,12 +175,14 @@ static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; } void kasan_cache_shrink(struct kmem_cache *cache); void kasan_cache_shutdown(struct kmem_cache *cache); void kasan_record_aux_stack(void *ptr); +void kasan_record_tmr_stack(void *ptr); #else /* CONFIG_KASAN_GENERIC */ static inline void kasan_cache_shrink(struct kmem_cache *cache) {} static inline void kasan_cache_shutdown(struct kmem_cache *cache) {} static inline void kasan_record_aux_stack(void *ptr) {} +static inline void kasan_record_tmr_stack(void *ptr) {} #endif /* CONFIG_KASAN_GENERIC */ diff --git a/kernel/time/timer.c b/kernel/time/timer.c index a5221abb4594..ef2da9ddfac7 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -783,6 +783,8 @@ static void do_init_timer(struct timer_list *timer, timer->function = func; timer->flags = flags | raw_smp_processor_id(); lockdep_init_map(&timer->lockdep_map, name, key, 0); + + kasan_record_tmr_stack(timer); } /** diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index 4b3cbad7431b..f35dcec990ab 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -347,6 +347,27 @@ void kasan_record_aux_stack(void *addr) alloc_info->aux_stack[0] = kasan_save_stack(GFP_NOWAIT); } +void kasan_record_tmr_stack(void *addr) +{ + struct page *page = kasan_addr_to_page(addr); + struct kmem_cache *cache; + struct kasan_alloc_meta *alloc_info; + void *object; + + if (!(page && PageSlab(page))) + return; + + cache = page->slab_cache; + object = nearest_obj(cache, page, addr); + alloc_info = get_alloc_info(cache, object); + + /* + * record the last two timer stacks. + */ + alloc_info->tmr_stack[1] = alloc_info->tmr_stack[0]; + alloc_info->tmr_stack[0] = kasan_save_stack(GFP_NOWAIT); +} + void kasan_set_free_info(struct kmem_cache *cache, void *object, u8 tag) { diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index ef655a1c6e15..c50827f388a3 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -108,10 +108,12 @@ struct kasan_alloc_meta { struct kasan_track alloc_track; #ifdef CONFIG_KASAN_GENERIC /* - * call_rcu() call stack is stored into struct kasan_alloc_meta. + * call_rcu() call stack and timer queueing stack are stored + * into struct kasan_alloc_meta. * The free stack is stored into struct kasan_free_meta. */ depot_stack_handle_t aux_stack[2]; + depot_stack_handle_t tmr_stack[2]; #else struct kasan_track free_track[KASAN_NR_FREE_STACKS]; #endif diff --git a/mm/kasan/report.c b/mm/kasan/report.c index fed3c8fdfd25..6fa3bfee381f 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -191,6 +191,17 @@ static void describe_object(struct kmem_cache *cache, void *object, print_stack(alloc_info->aux_stack[1]); pr_err("\n"); } + + if (alloc_info->tmr_stack[0]) { + pr_err("Last timer stack:\n"); + print_stack(alloc_info->tmr_stack[0]); + pr_err("\n"); + } + if (alloc_info->tmr_stack[1]) { + pr_err("Second to last timer stack:\n"); + print_stack(alloc_info->tmr_stack[1]); + pr_err("\n"); + } #endif }