Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp167795pxu; Wed, 2 Dec 2020 18:32:09 -0800 (PST) X-Google-Smtp-Source: ABdhPJwcraKabKwZAQwJD6PylRAn+Eb6EvJnN+tMvzcT0cxH6A1UIhQ/GLcff8h42QBqjmWTWFlT X-Received: by 2002:a17:906:7f13:: with SMTP id d19mr722955ejr.54.1606962729151; Wed, 02 Dec 2020 18:32:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606962729; cv=none; d=google.com; s=arc-20160816; b=Os6vjzJMEcS1j/ifFbD8XiyHVKYUetr5zrIL2x3EbELoJlqusf0XSfQFFoUHxGBnPK DNePq2EtXJ7dYID9vggkEPib1TzKPNKSomxIp7VorlbNIcAZ+k6QWMby3tsYyJtGdMvg IO5M9/6WrjbFi+1Hc57X4M9DACgIYyZ2lLij2PvI/FGqx9WulRcjQfObc5BY8dhGL0iR 0AUZibNx3W9NYs4tHOWuwz20WggtiI2euCOjXksMyD9T4o3S92+bBWbUmAMP1SnGp8rk 77TBCqgfQzFo0/QKZegLX52bwdyXetjji807NHpRX47yE6mtg4oQgwdW9m42CZEyT9Qy hyIw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:subject:cc:to:from; bh=nJws5cOrux4qoa6rTbzVJ+Li+KylYzsSbjETzC/4mfY=; b=yWTIiRCBxDpTMNbo7ga4NV0s/mvrmXOS41eGO1gWxzth34vh+zWtyYZg2uU+jrXxaR 1dzVDgsOr1H91MaVWiAnXIS9kpM9br9QhvryHVVubobZTNbd/oZ/ZwvOnGPyrp+FnC09 4K43pKcpLGAtziDfUGf9ZHfJkTPGtfL1RFHtkn3F7oMmnnPbJ+APvUup8gMtv+Fsucmd DxvpZVhaVfAFJyQOXPHC0e7yT3S3LnvPn99iOXEIjsOqwNpYJut2Sjb5l97Cs4xBG7tO J+iUODs6GA9kMhZQMW57aPRoxU7xxkdzvKDYoZobfuMwZxKRicgHjOh6p7y1zCvqCiMU x9jg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mediatek.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k23si358513ejk.626.2020.12.02.18.31.45; Wed, 02 Dec 2020 18:32:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mediatek.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729539AbgLCC2e (ORCPT + 99 others); Wed, 2 Dec 2020 21:28:34 -0500 Received: from mailgw01.mediatek.com ([210.61.82.183]:54397 "EHLO mailgw01.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1729493AbgLCC2e (ORCPT ); Wed, 2 Dec 2020 21:28:34 -0500 X-UUID: 5a3d1b56714f4046bd72a8c5bb642daa-20201203 X-UUID: 5a3d1b56714f4046bd72a8c5bb642daa-20201203 Received: from mtkcas10.mediatek.inc [(172.21.101.39)] by mailgw01.mediatek.com (envelope-from ) (Cellopoint E-mail Firewall v4.1.14 Build 0819 with TLSv1.2 ECDHE-RSA-AES256-SHA384 256/256) with ESMTP id 1617248778; Thu, 03 Dec 2020 10:27:50 +0800 Received: from mtkcas07.mediatek.inc (172.21.101.84) by mtkmbs01n1.mediatek.inc (172.21.101.68) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Thu, 3 Dec 2020 10:27:48 +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; Thu, 3 Dec 2020 10:27:49 +0800 From: Walter Wu To: Andrew Morton , Marco Elver , Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Andrey Konovalov , Matthias Brugger CC: , , , , wsd_upstream , , Walter Wu Subject: [PATCH v5 3/4] lib/test_kasan.c: add workqueue test case Date: Thu, 3 Dec 2020 10:27:48 +0800 Message-ID: <20201203022748.30681-1-walter-zh.wu@mediatek.com> X-Mailer: git-send-email 2.18.0 MIME-Version: 1.0 Content-Type: text/plain X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Acked-by: Marco Elver Reviewed-by: Dmitry Vyukov Reviewed-by: Andrey Konovalov Cc: Andrey Ryabinin Cc: Alexander Potapenko Cc: Matthias Brugger --- v4: - testcase has merge conflict, so that rebase onto the KASAN-KUNIT --- lib/test_kasan_module.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/lib/test_kasan_module.c b/lib/test_kasan_module.c index 2d68db6ae67b..62a87854b120 100644 --- a/lib/test_kasan_module.c +++ b/lib/test_kasan_module.c @@ -91,6 +91,34 @@ static noinline void __init kasan_rcu_uaf(void) call_rcu(&global_rcu_ptr->rcu, kasan_rcu_reclaim); } +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 test_kasan_module_init(void) { @@ -102,6 +130,7 @@ static int __init test_kasan_module_init(void) copy_user_test(); kasan_rcu_uaf(); + kasan_workqueue_uaf(); kasan_restore_multi_shot(multishot); return -EAGAIN; -- 2.18.0