Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp124940ybz; Tue, 21 Apr 2020 06:03:53 -0700 (PDT) X-Google-Smtp-Source: APiQypIA92xY4dRn+xxfkVof82N38muRWjICboFnvhUKvdeYaa0T10EA1NO5pGLiLikZDq7tib/g X-Received: by 2002:a05:6402:759:: with SMTP id p25mr1420540edy.102.1587474232911; Tue, 21 Apr 2020 06:03:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587474232; cv=none; d=google.com; s=arc-20160816; b=pZALWe2EEABlVsvSwUTdC2Unh0bdmOHDJSfOEYxFSw7bHzr6rJlWVflTkIMktx6Mai x6xDbAR3EePMGj0Yg0B4FCgemfvn+o9+qO9OgAh1aOoF8HqDYRGuyHC6wlLf8t5Ua6fB bLqjl4NL51pqpnwNJF+h5MdoDNfGi6VEOH0C0tUdBtYphey3VbaWRAIf1pRLzUXBMZBM km0BIWCiOGMyb1R1K+jxc1rhodPV88ap/J4upwxUIDyubztOZM7eY9gv3wcIHsc/QuCV MjBLb/AAJcsAmF0mVUOFmrgPlIeh0WmStsVOScW02dkXewcnt/wIRYDCnGI93ngDyDUs ZLaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=6bxpXR7bk9lriyagOO2UqkV9Fm2IwK+BGPfkmvwlllg=; b=pq+ppUP2Y0O2oyzpGmMIQ+elvDtkmT9LWq06wVAULXd+vEqhNxXYupLjYTmEMav734 6BDVboBdNCJAPsPR1O13gHH6X4WrxJ9LbfFUk9LlwblyCWoal03eGm0Idm1xdCCZIxhh e1M6HQ5y3LkfCoV8Ua109oPgBgaO6yr9E0d4Xo3IOzMcUywDF34Serntlt7PXCGFycyK hcULojPI0hgtHchMzwcKNPwdNm9wuqU+/Ey7fRJ/mueEiuczpyIT30EU0wM6WDMq7rVI 0A4HWRCt+Dd15AF6IVAD23/OIpea0awMFupntTiubTspyFYXrynOn3I1wgoD5+vd1M5I SZ5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Y1fbcTDd; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k17si1508117ejj.219.2020.04.21.06.03.14; Tue, 21 Apr 2020 06:03:52 -0700 (PDT) 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; dkim=pass header.i=@google.com header.s=20161025 header.b=Y1fbcTDd; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728769AbgDUNBq (ORCPT + 99 others); Tue, 21 Apr 2020 09:01:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35052 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1726780AbgDUNBp (ORCPT ); Tue, 21 Apr 2020 09:01:45 -0400 Received: from mail-qv1-xf42.google.com (mail-qv1-xf42.google.com [IPv6:2607:f8b0:4864:20::f42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93D40C061A10 for ; Tue, 21 Apr 2020 06:01:45 -0700 (PDT) Received: by mail-qv1-xf42.google.com with SMTP id p13so6430195qvt.12 for ; Tue, 21 Apr 2020 06:01:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=6bxpXR7bk9lriyagOO2UqkV9Fm2IwK+BGPfkmvwlllg=; b=Y1fbcTDdB4pAfXbWuu0qCG+GiUYb96vpjnmGAFccY+9j1v13cfYYYRjb73MRcZXYkp U6EUTapHN9b9XenIOE4lK9D0skhRRaKRbgIi6UH10YgxB4FSdYbtrmmyy2mFX7rQ+8R/ kGn6S3t1uPAksnkCI4N/P4q0xaxZzwS+9Yp5d8ADm/pq1cwx0wJSHVhSFnoBFkZKOH+E yDdrM2omM7plYRSKEiPH9ZORCCGp7mEKBEyRIXDcFjWKwc7f5WS6l8QkW9Ak/SJoA07r kCGwk+SL2V5/2244fqoqANhTIqFEGclIW9TN9d2rthe3JBNWnOtS58O/PtfRNthm8+61 T37Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=6bxpXR7bk9lriyagOO2UqkV9Fm2IwK+BGPfkmvwlllg=; b=HCOR6mllnzH3s9jMs/+NSgY0HgQemIsmRocK5Ug0E84AQpB9jcRVSzdmJgdnIzCocI UDFSQRF2VNPx4Eah+9eytKSor7X94WL1OUGObO/IQ9nHk+xtaTwxCky9DieEf07ou9Pr MW7SPc8o+GNvYhOIfqSoEUlsMS1DKkKyBjozT6XJHeITpxAggB7E31p5Qtv1XKKBJ1iw m/r6j4LGoPvtBWCvvAm836qYMJmLooIf44G/NvobqoqBnJPO8tr3EygVcvWyiyOzBo1O zvBXbAw3pBfacjpChlDcPkLQ/IXkzQnDIuH2S44P5oW4L+Qx6ggENUGY7ay8fLswExio QycQ== X-Gm-Message-State: AGi0PuYflrviKFl77aRuEnm/H2FDd5mSR8OVJqq+7ydDgl7o95WFqdEu 4nti8MqzrFrzCQJRolW8sZrXkth5tnl1lTDXOA/gsg== X-Received: by 2002:a0c:b2d2:: with SMTP id d18mr2496270qvf.80.1587474103980; Tue, 21 Apr 2020 06:01:43 -0700 (PDT) MIME-Version: 1.0 References: <20200421014007.6012-1-walter-zh.wu@mediatek.com> <1587472005.5870.7.camel@mtksdccf07> In-Reply-To: <1587472005.5870.7.camel@mtksdccf07> From: Dmitry Vyukov Date: Tue, 21 Apr 2020 15:01:31 +0200 Message-ID: Subject: Re: [PATCH] kasan: fix KASAN unit tests for tag-based KASAN To: Walter Wu Cc: David Gow , Brendan Higgins , Patricia Alfonso , Andrey Ryabinin , Alexander Potapenko , Matthias Brugger , Andrey Konovalov , Andrew Morton , kasan-dev , Linux-MM , LKML , Linux ARM , wsd_upstream , linux-mediatek@lists.infradead.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Apr 21, 2020 at 2:26 PM Walter Wu wrote: > > Hi Dmitry, > > On Tue, 2020-04-21 at 13:56 +0200, Dmitry Vyukov wrote: > > On Tue, Apr 21, 2020 at 3:40 AM Walter Wu wrote: > > > > > > When we use tag-based KASAN, then KASAN unit tests don't detect > > > out-of-bounds memory access. Because 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 of pointer, > > > then 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 > > > --- > > > lib/test_kasan.c | 62 ++++++++++++++++++++++++++++++++++++++++++------ > > > 1 file changed, 55 insertions(+), 7 deletions(-) > > > > > > diff --git a/lib/test_kasan.c b/lib/test_kasan.c > > > index e3087d90e00d..a164f6b47fe5 100644 > > > --- a/lib/test_kasan.c > > > +++ b/lib/test_kasan.c > > > @@ -40,7 +40,12 @@ static noinline void __init kmalloc_oob_right(void) > > > return; > > > } > > > > Hi Walter, > > > > This would be great to have! > > But I am concerned about these series that port KASAN tests to KUNIT: > > https://lkml.org/lkml/2020/4/17/1144 > > I suspect it will be one large merge conflict. Not sure what is the > > proper way to resovle this. I've added authors to CC. > > > Yes, it should have conflicts. Thanks for your reminder. > > > > > +#ifdef CONFIG_KASAN_GENERIC > > > ptr[size] = 'x'; > > > +#else > > > + ptr[size + 5] = 'x'; > > > +#endif > > > + > > > > For this particular snippet I think we can reduce amount of idef'ery > > and amount of non-compiled code in each configuration with something > > like: > > > > ptr[size + 5] = 'x'; > > if (ENABLED(CONFIG_KASAN_GENERIC)) > > ptr[size] = 'x'; > > > > One check runs always (it should pass in both configs, right?). The > > There is a problem, With generic KASAN it may trigger two KASAN reports. Why is this a problem? If there are 2, fine. KUNIT can check that if we expect 2, there are indeed 2. > if we change it like: > > if (ENABLED(CONFIG_KASAN_GENERIC)) > ptr[size] = 'x'; > else > ptr[size + 5] = 'x'; > > > only only in GENERIC, but it's C-level if rather than preprocessor. > > KUNIT should make 2 bugs per test easily expressable (and testable). > > > > > > > > > > > > kfree(ptr); > > > } > > > > > > @@ -92,7 +97,12 @@ static noinline void __init kmalloc_pagealloc_oob_right(void) > > > return; > > > } > > > > > > +#ifdef CONFIG_KASAN_GENERIC > > > ptr[size] = 0; > > > +#else > > > + ptr[size + 6] = 0; > > > +#endif > > > + > > > kfree(ptr); > > > } > > > > > > @@ -162,7 +172,11 @@ static noinline void __init kmalloc_oob_krealloc_more(void) > > > return; > > > } > > > > > > +#ifdef CONFIG_KASAN_GENERIC > > > ptr2[size2] = 'x'; > > > +#else > > > + ptr2[size2 + 13] = 'x'; > > > +#endif > > > kfree(ptr2); > > > } > > > > > > @@ -180,7 +194,12 @@ static noinline void __init kmalloc_oob_krealloc_less(void) > > > kfree(ptr1); > > > return; > > > } > > > + > > > +#ifdef CONFIG_KASAN_GENERIC > > > ptr2[size2] = 'x'; > > > +#else > > > + ptr2[size2 + 2] = 'x'; > > > +#endif > > > kfree(ptr2); > > > } > > > > > > @@ -216,7 +235,11 @@ static noinline void __init kmalloc_oob_memset_2(void) > > > return; > > > } > > > > > > +#ifdef CONFIG_KASAN_GENERIC > > > memset(ptr+7, 0, 2); > > > +#else > > > + memset(ptr+15, 0, 2); > > > +#endif > > > kfree(ptr); > > > } > > > > > > @@ -232,7 +255,11 @@ static noinline void __init kmalloc_oob_memset_4(void) > > > return; > > > } > > > > > > +#ifdef CONFIG_KASAN_GENERIC > > > memset(ptr+5, 0, 4); > > > +#else > > > + memset(ptr+15, 0, 4); > > > +#endif > > > kfree(ptr); > > > } > > > > > > @@ -249,7 +276,11 @@ static noinline void __init kmalloc_oob_memset_8(void) > > > return; > > > } > > > > > > +#ifdef CONFIG_KASAN_GENERIC > > > memset(ptr+1, 0, 8); > > > +#else > > > + memset(ptr+15, 0, 8); > > > +#endif > > > kfree(ptr); > > > } > > > > > > @@ -265,7 +296,11 @@ static noinline void __init kmalloc_oob_memset_16(void) > > > return; > > > } > > > > > > +#ifdef CONFIG_KASAN_GENERIC > > > memset(ptr+1, 0, 16); > > > +#else > > > + memset(ptr+15, 0, 16); > > > +#endif > > > kfree(ptr); > > > } > > > > > > @@ -281,7 +316,11 @@ static noinline void __init kmalloc_oob_in_memset(void) > > > return; > > > } > > > > > > +#ifdef CONFIG_KASAN_GENERIC > > > memset(ptr, 0, size+5); > > > +#else > > > + memset(ptr, 0, size+7); > > > +#endif > > > kfree(ptr); > > > } > > > > > > @@ -415,7 +454,11 @@ static noinline void __init kmem_cache_oob(void) > > > return; > > > } > > > > > > +#ifdef CONFIG_KASAN_GENERIC > > > *p = p[size]; > > > +#else > > > + *p = p[size + 8]; > > > +#endif > > > kmem_cache_free(cache, p); > > > kmem_cache_destroy(cache); > > > } > > > @@ -497,6 +540,11 @@ static noinline void __init copy_user_test(void) > > > char __user *usermem; > > > size_t size = 10; > > > int unused; > > > +#ifdef CONFIG_KASAN_GENERIC > > > + size_t oob_size = 1; > > > +#else > > > + size_t oob_size = 7; > > > +#endif > > > > > > kmem = kmalloc(size, GFP_KERNEL); > > > if (!kmem) > > > @@ -512,25 +560,25 @@ static noinline void __init copy_user_test(void) > > > } > > > > > > 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); > > > -- > > > 2.18.0 > > > > > > -- > > > You received this message because you are subscribed to the Google Groups "kasan-dev" group. > > > To unsubscribe from this group and stop receiving emails from it, send an email to kasan-dev+unsubscribe@googlegroups.com. > > > To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20200421014007.6012-1-walter-zh.wu%40mediatek.com. > > -- > You received this message because you are subscribed to the Google Groups "kasan-dev" group. > To unsubscribe from this group and stop receiving emails from it, send an email to kasan-dev+unsubscribe@googlegroups.com. > To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/1587472005.5870.7.camel%40mtksdccf07.