Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp106338ybt; Mon, 6 Jul 2020 05:15:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwEbJtNL50zuuhmt8pEZKDmTyFG8kikHyAwnHx2NvxsqlIbxYVz06Bm/na1keeO0QOHO9s6 X-Received: by 2002:a05:6402:337:: with SMTP id q23mr56627355edw.63.1594037712693; Mon, 06 Jul 2020 05:15:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594037712; cv=none; d=google.com; s=arc-20160816; b=DKB4cLBASoR9hoKmWUhQasgJ3aiLqa8XG+ik/DqUx9q8Ln4tFBp3sRWhIB+XDn6/Ea wOIS4agAXV0aQaWUUYxKhtK2QCkQ1pvWvfQv70C/0/MAcJHIbjztMhOUFi8eVN1g+Mjl uk2RwF3yLbQx85agCenVmKMAYubit4qVM3TVpMsGkSAXcmVcDt+6GSjGX9X2m4fJBTSN fttSTx/NRA81TyMJq05JAmyj4fguYFHIXNwpjyfm5grk5B2MJXK0dRCivn8/AChfqq2N 4BLUKgAyWCTyGw/lgJk3WSY7yJgb+dbIRkJQVAYlMLtWKAyedjX54Ms4OBUEgs+Z+3ae O+sQ== 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=JMy1NuuvAJma8T47+ZbalKDZNjVJ2jnk0BZDVxqbCwQ=; b=z3/Em1L/bQp9SXSTL6eXtLSuK25XaixKMc0ihm0i0wa6xX7AU3rnD9Ggfdh64q6uXw CiXHxqL/XcktCMZYf0su56agXgrPWXFASl5/p44YEDcg3d3Aw2L2IqlX99jYDDXEZ4wv RBfQMzN9oMwGEjlTzSnVefwvW7T6I3fu/DoRdb14ydfuXb1usd2xFD5hckvh2NHbsWpr 8mg3TaRcwKkw1N4jDFnJty/Ddw3xepqsWSlranR43xHEMhj6cU+A1SouVp9fUYnPnDJg mxGbH5zpcD/exFybGJsdTH5S0foHhyUE4VbBn2mPPcbSKYgmdXPWrJKdGW7wwL8AVW1k zqvQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=nko1XfT+; 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 d23si12981439ejz.145.2020.07.06.05.14.49; Mon, 06 Jul 2020 05:15:12 -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=nko1XfT+; 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 S1729013AbgGFMO3 (ORCPT + 99 others); Mon, 6 Jul 2020 08:14:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727896AbgGFMO3 (ORCPT ); Mon, 6 Jul 2020 08:14:29 -0400 Received: from mail-qk1-x743.google.com (mail-qk1-x743.google.com [IPv6:2607:f8b0:4864:20::743]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29C9AC061794 for ; Mon, 6 Jul 2020 05:14:29 -0700 (PDT) Received: by mail-qk1-x743.google.com with SMTP id 80so34461931qko.7 for ; Mon, 06 Jul 2020 05:14:29 -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=JMy1NuuvAJma8T47+ZbalKDZNjVJ2jnk0BZDVxqbCwQ=; b=nko1XfT+397vBzoTAuvb/NwaOO/bEhQhOka5qoSo1wHHzsAjJ2qSBtbij0FSm97MZf gCqkokoYGz2WZosQmEAkbDOLBldBqgRmDOlpDpSg+2GOFiU5ymPt3ZtUR/xERhVOl3C7 GfdO2bazIo5PNN2Aifia9bbkm4Nh0qnrDsQlyqJ1R1shAnHIyYiL7Xke/QTt/b5q7ybP 2N78dlx3kuyI8sjTUCluaJAIt6OxqMWQhdQoy3L/BOu4+0oLz9NDePMS6dZxtnxG1Tme 2SKE26QVvPzTnGSOjruh7dOOUCuFdOLq+/CNQz6Dywj3At2lpCXglqHrpgVJ+5c2FovB P3Qg== 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=JMy1NuuvAJma8T47+ZbalKDZNjVJ2jnk0BZDVxqbCwQ=; b=YFBGlgv8quDs2dH5AtGkOiETEB2xPmCvKtaTS4UiHYe550iX1VM1hft6RpEDGzclK9 /tfSREldA8JZrLUQHOSV7Gy/R8cgWaBaqzxIT/NIuhGHZddIilzxEgVuXJrid0T/Fkh+ 7wmLqLjZQOCuUmMiLwJpdu5btSjZdQpb1J0RUC7aC+rXkfKd2IPdAsNFYS3rV/uVQBCD qNQTN0Qoe4C7JSv38vxxdsk9buhUOTe/0geoFMeSCi2gX1x8Hjk7lrWTc3e/P2Vkbz3b pbOQDS+5rhbmtCD46r18GRuFMBfgS98hVZNeNsaCYUEe8tIegV0NFer7vXA6pNPt2mPw +1/A== X-Gm-Message-State: AOAM533PaN/qO9kZ0IxhgySflp9WfrMDupk1BYCZjWwC54KvQ48hWa4y 8OaENVEwmBwJufPaC8N2sZzoeQo9fepaKkKrEy8Icw== X-Received: by 2002:a05:620a:4ca:: with SMTP id 10mr47782458qks.250.1594037668024; Mon, 06 Jul 2020 05:14:28 -0700 (PDT) MIME-Version: 1.0 References: <20200706115039.16750-1-walter-zh.wu@mediatek.com> In-Reply-To: <20200706115039.16750-1-walter-zh.wu@mediatek.com> From: Dmitry Vyukov Date: Mon, 6 Jul 2020 14:14:16 +0200 Message-ID: Subject: Re: [PATCH v3] kasan: fix KASAN unit tests for tag-based KASAN To: Walter Wu Cc: Andrey Ryabinin , Alexander Potapenko , Matthias Brugger , kasan-dev , Linux-MM , LKML , Linux ARM , wsd_upstream , linux-mediatek@lists.infradead.org, Andrey Konovalov , Andrew Morton 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 Mon, Jul 6, 2020 at 1:50 PM Walter Wu wrote: > > 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 > Suggested-by: Dmitry Vyukov > Cc: Andrey Ryabinin > Cc: Dmitry Vyukov > Cc: Alexander Potapenko > Cc: Matthias Brugger > Cc: Andrey Konovalov > Cc: Andrew Morton Acked-by: Dmitry Vyukov Thanks for fixing these tests! > --- > > changes since v1: > - Reduce amount of non-compiled code. > - KUnit-KASAN Integration patchset is not merged yet. My patch should > have conflict with it, if needed, we can continue to wait it. > > changes since v2: > - Add one marco to make unit tests more readability. > > --- > lib/test_kasan.c | 47 ++++++++++++++++++++++++++++++----------------- > 1 file changed, 30 insertions(+), 17 deletions(-) > > diff --git a/lib/test_kasan.c b/lib/test_kasan.c > index e3087d90e00d..b5049a807e25 100644 > --- a/lib/test_kasan.c > +++ b/lib/test_kasan.c > @@ -23,6 +23,8 @@ > > #include > > +#define OOB_TAG_OFF (IS_ENABLED(CONFIG_KASAN_GENERIC) ? 0 : 13) > + > /* > * Note: test functions are marked noinline so that their names appear in > * reports. > @@ -40,7 +42,8 @@ static noinline void __init kmalloc_oob_right(void) > return; > } > > - ptr[size] = 'x'; > + ptr[size + OOB_TAG_OFF] = 'x'; > + > kfree(ptr); > } > > @@ -92,7 +95,8 @@ static noinline void __init kmalloc_pagealloc_oob_right(void) > return; > } > > - ptr[size] = 0; > + ptr[size + OOB_TAG_OFF] = 0; > + > kfree(ptr); > } > > @@ -162,7 +166,8 @@ static noinline void __init kmalloc_oob_krealloc_more(void) > return; > } > > - ptr2[size2] = 'x'; > + ptr2[size2 + OOB_TAG_OFF] = 'x'; > + > kfree(ptr2); > } > > @@ -180,7 +185,9 @@ static noinline void __init kmalloc_oob_krealloc_less(void) > kfree(ptr1); > return; > } > - ptr2[size2] = 'x'; > + > + ptr2[size2 + OOB_TAG_OFF] = 'x'; > + > kfree(ptr2); > } > > @@ -216,7 +223,8 @@ static noinline void __init kmalloc_oob_memset_2(void) > return; > } > > - memset(ptr+7, 0, 2); > + memset(ptr + 7 + OOB_TAG_OFF, 0, 2); > + > kfree(ptr); > } > > @@ -232,7 +240,8 @@ static noinline void __init kmalloc_oob_memset_4(void) > return; > } > > - memset(ptr+5, 0, 4); > + memset(ptr + 5 + OOB_TAG_OFF, 0, 4); > + > kfree(ptr); > } > > @@ -249,7 +258,8 @@ static noinline void __init kmalloc_oob_memset_8(void) > return; > } > > - memset(ptr+1, 0, 8); > + memset(ptr + 1 + OOB_TAG_OFF, 0, 8); > + > kfree(ptr); > } > > @@ -265,7 +275,8 @@ static noinline void __init kmalloc_oob_memset_16(void) > return; > } > > - memset(ptr+1, 0, 16); > + memset(ptr + 1 + OOB_TAG_OFF, 0, 16); > + > kfree(ptr); > } > > @@ -281,7 +292,8 @@ static noinline void __init kmalloc_oob_in_memset(void) > return; > } > > - memset(ptr, 0, size+5); > + memset(ptr, 0, size + 5 + OOB_TAG_OFF); > + > kfree(ptr); > } > > @@ -415,7 +427,8 @@ static noinline void __init kmem_cache_oob(void) > return; > } > > - *p = p[size]; > + *p = p[size + OOB_TAG_OFF]; > + > kmem_cache_free(cache, p); > kmem_cache_destroy(cache); > } > @@ -512,25 +525,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 + 1 + OOB_TAG_OFF); > > 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 + 1 + OOB_TAG_OFF); > > 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 + 1 + OOB_TAG_OFF); > > 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 + 1 + OOB_TAG_OFF); > > 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 + 1 + OOB_TAG_OFF); > > 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 + 1 + OOB_TAG_OFF); > > 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 + 1 + OOB_TAG_OFF); > > 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/20200706115039.16750-1-walter-zh.wu%40mediatek.com.