From patchwork Tue Aug 25 02:00:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Walter Wu X-Patchwork-Id: 11734691 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 1CA70138A for ; Tue, 25 Aug 2020 02:00:19 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id DD561204FD for ; Tue, 25 Aug 2020 02:00:18 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=mediatek.com header.i=@mediatek.com header.b="snlOgSvp" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DD561204FD 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 2ABF06B00C4; Mon, 24 Aug 2020 22:00:18 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 25CBA8D0002; Mon, 24 Aug 2020 22:00:18 -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 199C16B00C6; Mon, 24 Aug 2020 22:00:18 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0150.hostedemail.com [216.40.44.150]) by kanga.kvack.org (Postfix) with ESMTP id 0572C6B00C4 for ; Mon, 24 Aug 2020 22:00:18 -0400 (EDT) Received: from smtpin26.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id BC3048248047 for ; Tue, 25 Aug 2020 02:00:17 +0000 (UTC) X-FDA: 77187436074.26.guide20_5d060e427058 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin26.hostedemail.com (Postfix) with ESMTP id 83F1E1804B669 for ; Tue, 25 Aug 2020 02:00:17 +0000 (UTC) X-Spam-Summary: 1,0,0,36f6b4f44dd420cd,d41d8cd98f00b204,walter-zh.wu@mediatek.com,,RULES_HIT:41:355:379:541:800:960:966:973:988:989:1185:1260:1277:1311:1313:1314:1345:1431:1437:1514:1515:1516:1518:1535:1542:1585:1711:1730:1747:1777:1792:2196:2198:2199:2200:2393:2559:2562:2915:3138:3139:3140:3141:3142:3352:3865:3867:3868:3870:3872:4250:4385:5007:6261:6653:7514:7903:9707:10004:10400:11026:11658:11914:12043:12048:12291:12296:12297:12438:12521:12555:12679:12895:13161:13229:14181:14394:14721:21080:21451:21627:21740:21990:30054,0,RBL:210.61.82.183:@mediatek.com:.lbl8.mailshell.net-62.2.12.100 64.100.201.201;04y8dsoxkpajkha9ja6xtpsrcks95op86k1nnyp5nabz9p1ijeecy4sfhqj8k5f.ttcmmz6cby164afzzsxz4w3sohe68qybq6frnknm7g6wfj3yxop4zohj3kt8ztx.h-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:25,LUA_SUMMARY:none X-HE-Tag: guide20_5d060e427058 X-Filterd-Recvd-Size: 5339 Received: from mailgw01.mediatek.com (unknown [210.61.82.183]) by imf36.hostedemail.com (Postfix) with ESMTP for ; Tue, 25 Aug 2020 02:00:16 +0000 (UTC) X-UUID: da200875e771434fb8ea2e4074119a4e-20200825 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=+uEb+COBoPnYhqxN0aa+Ix9E1vWCkdkDaOsAXwUlrdg=; b=snlOgSvp6Ydfa9GQsyZ0okgQarXlwCKKK0WFjIUGHC/csH+0BLUWNMK+Fkztw2CgANdzNJmSsWwe0pWEizJ4+AYsTZGfFL3eUCS59qQPWjQLi63O5jinLFRMDX99A9hvK8jKhR3lm9seKXi1sQBQLTeWGUm6JohUr33nVOPq/ac=; X-UUID: da200875e771434fb8ea2e4074119a4e-20200825 Received: from mtkcas11.mediatek.inc [(172.21.101.40)] by mailgw01.mediatek.com (envelope-from ) (Cellopoint E-mail Firewall v4.1.10 Build 0809 with TLS) with ESMTP id 555672964; Tue, 25 Aug 2020 10:00:12 +0800 Received: from mtkcas07.mediatek.inc (172.21.101.84) by mtkmbs06n2.mediatek.inc (172.21.101.130) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Tue, 25 Aug 2020 10:00:11 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by mtkcas07.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Tue, 25 Aug 2020 10:00:11 +0800 From: Walter Wu To: Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Matthias Brugger CC: , , , , wsd_upstream , , Walter Wu , Andrey Konovalov Subject: [PATCH v3 5/6] kasan: add tests for workqueue stack recording Date: Tue, 25 Aug 2020 10:00:08 +0800 Message-ID: <20200825020008.28682-1-walter-zh.wu@mediatek.com> X-Mailer: git-send-email 2.18.0 MIME-Version: 1.0 X-TM-SNTS-SMTP: 6777E2A49D633339955E856E650F8D381A2BE95487C4F41DF024165C8168C8D82000:8 X-MTK: N X-Rspamd-Queue-Id: 83F1E1804B669 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam02 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: Adds a test to verify workqueue stack recording and print it in KASAN report. The KASAN report was as follows(cleaned up slightly): BUG: KASAN: use-after-free in kasan_workqueue_uaf Freed by task 54: kasan_save_stack+0x24/0x50 kasan_set_track+0x24/0x38 kasan_set_free_info+0x20/0x40 __kasan_slab_free+0x10c/0x170 kasan_slab_free+0x10/0x18 kfree+0x98/0x270 kasan_workqueue_work+0xc/0x18 Last potentially related work creation: kasan_save_stack+0x24/0x50 kasan_record_wq_stack+0xa8/0xb8 insert_work+0x48/0x288 __queue_work+0x3e8/0xc40 queue_work_on+0xf4/0x118 kasan_workqueue_uaf+0xfc/0x190 Signed-off-by: Walter Wu Cc: Andrey Ryabinin Cc: Dmitry Vyukov Cc: Alexander Potapenko Cc: Matthias Brugger Cc: Andrey Konovalov --- lib/test_kasan.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/lib/test_kasan.c b/lib/test_kasan.c index 2bd61674c7a3..7293a55ff51c 100644 --- a/lib/test_kasan.c +++ b/lib/test_kasan.c @@ -845,6 +845,34 @@ static noinline void __init kasan_timer_uaf(void) ((volatile struct timer_list *)timer)->expires; } +static noinline void __init kasan_workqueue_work(struct work_struct *work) +{ + kfree(work); +} + +static noinline void __init kasan_workqueue_uaf(void) +{ + struct workqueue_struct *workqueue; + struct work_struct *work; + + workqueue = create_workqueue("kasan_wq_test"); + if (!workqueue) { + pr_err("Allocation failed\n"); + return; + } + work = kmalloc(sizeof(struct work_struct), GFP_KERNEL); + if (!work) { + pr_err("Allocation failed\n"); + return; + } + + INIT_WORK(work, kasan_workqueue_work); + queue_work(workqueue, work); + destroy_workqueue(workqueue); + + pr_info("use-after-free on workqueue\n"); + ((volatile struct work_struct *)work)->data; +} static int __init kmalloc_tests_init(void) { /* @@ -894,6 +922,7 @@ static int __init kmalloc_tests_init(void) vmalloc_oob(); kasan_rcu_uaf(); kasan_timer_uaf(); + kasan_workqueue_uaf(); kasan_restore_multi_shot(multishot);