From patchwork Mon Jul 6 02:21:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Walter Wu X-Patchwork-Id: 11644819 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 02B21913 for ; Mon, 6 Jul 2020 02:22:04 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id A64392074F for ; Mon, 6 Jul 2020 02:22:03 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=mediatek.com header.i=@mediatek.com header.b="jouIo6bW" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A64392074F 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 A7B5E6B0003; Sun, 5 Jul 2020 22:22:02 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id A05A66B0005; Sun, 5 Jul 2020 22:22:02 -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 8CC6A6B0006; Sun, 5 Jul 2020 22:22:02 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0105.hostedemail.com [216.40.44.105]) by kanga.kvack.org (Postfix) with ESMTP id 740B66B0003 for ; Sun, 5 Jul 2020 22:22:02 -0400 (EDT) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id CD586181AC9CC for ; Mon, 6 Jul 2020 02:22:01 +0000 (UTC) X-FDA: 77006050842.27.man99_3f0349926ea8 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin27.hostedemail.com (Postfix) with ESMTP id 9D5383D66B for ; Mon, 6 Jul 2020 02:22:01 +0000 (UTC) X-Spam-Summary: 1,0,0,98dbc00366934f58,d41d8cd98f00b204,walter-zh.wu@mediatek.com,,RULES_HIT:2:41:69:355:379:541:800:960:966:973:988:989:1185:1260:1277:1311:1313:1314:1345:1431:1437:1514:1515:1516:1518:1535:1585:1605:1606:1730:1747:1777:1792:1981:2194:2196:2199:2200:2393:2559:2562:2693:2898:2901:3138:3139:3140:3141:3142:3865:3866:3867:3868:3870:3871:3872:3874:4120:4250:4321:4385:5007:6261:6653:7514:7875:8603:10004:11026:11232:11473:11658:11914:12043:12048:12291:12297:12438:12521:12555:12683:12895:12986:13869:14394:21080:21324:21451:21627:21990:30005:30012:30045:30051:30054:30070,0,RBL:210.61.82.183:@mediatek.com:.lbl8.mailshell.net-62.14.12.100 64.201.201.201;04ygegx5oku9idi7965h7h3etbkb5optef5dsfd7aj18o46haefs6ukijnqt3fu.c98baypg6769yxyr3ib455dmjqjzuqyn6kf7ua5csfscnkmg51dc41ajohgr3d5.4-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:ft,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:93,LUA_SUMMARY:none X-HE-Tag: man99_3f0349926ea8 X-Filterd-Recvd-Size: 9616 Received: from mailgw01.mediatek.com (unknown [210.61.82.183]) by imf48.hostedemail.com (Postfix) with ESMTP for ; Mon, 6 Jul 2020 02:22:00 +0000 (UTC) X-UUID: 728303f5350049de813bb76e59e979b3-20200706 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=SiWDphWYGnavflkljTLom2j3mHnFMDqK/JoyQBPF1o4=; b=jouIo6bW2xrjquA3OK2ZX/C1mn8AeTPPLzt6GGIDaMA4oOrwEWe7GkQBbNOy2FP/WfG6VJsdWL+PGX3Ns6q62k34ZNRLUBPWv0bxtjMUAlyGnfNQJNnT6F4qx5b4/zaiJd+80Z621aFhcRKhpi67HrCNmDMk3xMPZxWTxPAVh7Q=; X-UUID: 728303f5350049de813bb76e59e979b3-20200706 Received: from mtkexhb01.mediatek.inc [(172.21.101.102)] by mailgw01.mediatek.com (envelope-from ) (Cellopoint E-mail Firewall v4.1.10 Build 0809 with TLS) with ESMTP id 2007695977; Mon, 06 Jul 2020 10:21:53 +0800 Received: from mtkcas07.mediatek.inc (172.21.101.84) by mtkmbs06n1.mediatek.inc (172.21.101.129) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 6 Jul 2020 10:21:50 +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; Mon, 6 Jul 2020 10:21:52 +0800 From: Walter Wu To: Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Matthias Brugger CC: , , , , wsd_upstream , , Walter Wu , Andrey Konovalov , Andrew Morton Subject: [PATCH v2] kasan: fix KASAN unit tests for tag-based KASAN Date: Mon, 6 Jul 2020 10:21:50 +0800 Message-ID: <20200706022150.20848-1-walter-zh.wu@mediatek.com> X-Mailer: git-send-email 2.18.0 MIME-Version: 1.0 X-MTK: N X-Rspamd-Queue-Id: 9D5383D66B X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam05 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 use tag-based KASAN, then KASAN unit tests don't detect out-of-bounds memory access. They need to be fixed. With tag-based KASAN, the state of each 16 aligned bytes of memory is encoded in one shadow byte and the shadow value is tag of pointer, so we need to read next shadow byte, the shadow value is not equal to tag value of pointer, so that tag-based KASAN will detect out-of-bounds memory access. Signed-off-by: Walter Wu Cc: Andrey Ryabinin Cc: Dmitry Vyukov Cc: Alexander Potapenko Cc: Matthias Brugger Cc: Andrey Konovalov Cc: Andrew Morton --- changes since v1: - Reduce amount of non-compiled code. - KUnit-KASAN Integration patchset are not merged yet. My patch should have conflict with it, if needed, we can continue to wait it. --- lib/test_kasan.c | 81 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 64 insertions(+), 17 deletions(-) diff --git a/lib/test_kasan.c b/lib/test_kasan.c index e3087d90e00d..660664439d52 100644 --- a/lib/test_kasan.c +++ b/lib/test_kasan.c @@ -40,7 +40,11 @@ static noinline void __init kmalloc_oob_right(void) return; } - ptr[size] = 'x'; + if (IS_ENABLED(CONFIG_KASAN_GENERIC)) + ptr[size] = 'x'; + else + ptr[size + 5] = 'x'; + kfree(ptr); } @@ -92,7 +96,11 @@ static noinline void __init kmalloc_pagealloc_oob_right(void) return; } - ptr[size] = 0; + if (IS_ENABLED(CONFIG_KASAN_GENERIC)) + ptr[size] = 0; + else + ptr[size + 6] = 0; + kfree(ptr); } @@ -162,7 +170,11 @@ static noinline void __init kmalloc_oob_krealloc_more(void) return; } - ptr2[size2] = 'x'; + if (IS_ENABLED(CONFIG_KASAN_GENERIC)) + ptr2[size2] = 'x'; + else + ptr2[size2 + 13] = 'x'; + kfree(ptr2); } @@ -180,7 +192,12 @@ static noinline void __init kmalloc_oob_krealloc_less(void) kfree(ptr1); return; } - ptr2[size2] = 'x'; + + if (IS_ENABLED(CONFIG_KASAN_GENERIC)) + ptr2[size2] = 'x'; + else + ptr2[size2 + 2] = 'x'; + kfree(ptr2); } @@ -216,7 +233,11 @@ static noinline void __init kmalloc_oob_memset_2(void) return; } - memset(ptr+7, 0, 2); + if (IS_ENABLED(CONFIG_KASAN_GENERIC)) + memset(ptr+7, 0, 2); + else + memset(ptr+15, 0, 2); + kfree(ptr); } @@ -232,7 +253,11 @@ static noinline void __init kmalloc_oob_memset_4(void) return; } - memset(ptr+5, 0, 4); + if (IS_ENABLED(CONFIG_KASAN_GENERIC)) + memset(ptr+5, 0, 4); + else + memset(ptr+15, 0, 4); + kfree(ptr); } @@ -249,7 +274,11 @@ static noinline void __init kmalloc_oob_memset_8(void) return; } - memset(ptr+1, 0, 8); + if (IS_ENABLED(CONFIG_KASAN_GENERIC)) + memset(ptr+1, 0, 8); + else + memset(ptr+15, 0, 8); + kfree(ptr); } @@ -265,7 +294,11 @@ static noinline void __init kmalloc_oob_memset_16(void) return; } - memset(ptr+1, 0, 16); + if (IS_ENABLED(CONFIG_KASAN_GENERIC)) + memset(ptr+1, 0, 16); + else + memset(ptr+15, 0, 16); + kfree(ptr); } @@ -281,7 +314,11 @@ static noinline void __init kmalloc_oob_in_memset(void) return; } - memset(ptr, 0, size+5); + if (IS_ENABLED(CONFIG_KASAN_GENERIC)) + memset(ptr, 0, size+5); + else + memset(ptr, 0, size+7); + kfree(ptr); } @@ -415,7 +452,11 @@ static noinline void __init kmem_cache_oob(void) return; } - *p = p[size]; + if (IS_ENABLED(CONFIG_KASAN_GENERIC)) + *p = p[size]; + else + *p = p[size + 8]; + kmem_cache_free(cache, p); kmem_cache_destroy(cache); } @@ -497,6 +538,7 @@ static noinline void __init copy_user_test(void) char __user *usermem; size_t size = 10; int unused; + size_t oob_size; kmem = kmalloc(size, GFP_KERNEL); if (!kmem) @@ -511,26 +553,31 @@ static noinline void __init copy_user_test(void) return; } + if (IS_ENABLED(CONFIG_KASAN_GENERIC)) + oob_size = 1; + else + oob_size = 7; + pr_info("out-of-bounds in copy_from_user()\n"); - unused = copy_from_user(kmem, usermem, size + 1); + unused = copy_from_user(kmem, usermem, size + oob_size); pr_info("out-of-bounds in copy_to_user()\n"); - unused = copy_to_user(usermem, kmem, size + 1); + unused = copy_to_user(usermem, kmem, size + oob_size); pr_info("out-of-bounds in __copy_from_user()\n"); - unused = __copy_from_user(kmem, usermem, size + 1); + unused = __copy_from_user(kmem, usermem, size + oob_size); pr_info("out-of-bounds in __copy_to_user()\n"); - unused = __copy_to_user(usermem, kmem, size + 1); + unused = __copy_to_user(usermem, kmem, size + oob_size); pr_info("out-of-bounds in __copy_from_user_inatomic()\n"); - unused = __copy_from_user_inatomic(kmem, usermem, size + 1); + unused = __copy_from_user_inatomic(kmem, usermem, size + oob_size); pr_info("out-of-bounds in __copy_to_user_inatomic()\n"); - unused = __copy_to_user_inatomic(usermem, kmem, size + 1); + unused = __copy_to_user_inatomic(usermem, kmem, size + oob_size); pr_info("out-of-bounds in strncpy_from_user()\n"); - unused = strncpy_from_user(kmem, usermem, size + 1); + unused = strncpy_from_user(kmem, usermem, size + oob_size); vm_munmap((unsigned long)usermem, PAGE_SIZE); kfree(kmem);