From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-24.8 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_1,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4A34DC433DB for ; Wed, 3 Feb 2021 14:48:37 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 9F05264F6C for ; Wed, 3 Feb 2021 14:48:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9F05264F6C Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id C911E6B006E; Wed, 3 Feb 2021 09:48:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C40A66B0070; Wed, 3 Feb 2021 09:48:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AE0F66B0071; Wed, 3 Feb 2021 09:48:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0082.hostedemail.com [216.40.44.82]) by kanga.kvack.org (Postfix) with ESMTP id 9918B6B006E for ; Wed, 3 Feb 2021 09:48:35 -0500 (EST) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 51376181AC9B6 for ; Wed, 3 Feb 2021 14:48:35 +0000 (UTC) X-FDA: 77777237790.22.blow14_1f17833275d4 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin22.hostedemail.com (Postfix) with ESMTP id 213BF18038E60 for ; Wed, 3 Feb 2021 14:48:35 +0000 (UTC) X-HE-Tag: blow14_1f17833275d4 X-Filterd-Recvd-Size: 8878 Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) by imf47.hostedemail.com (Postfix) with ESMTP for ; Wed, 3 Feb 2021 14:48:34 +0000 (UTC) Received: by mail-wr1-f53.google.com with SMTP id u14so2773083wri.3 for ; Wed, 03 Feb 2021 06:48:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=9VGn7w2tNlFuEz50GpubxUEDNHCrhMt0WO2PGFie8Ao=; b=fQIVmx988C8vZV4BdPkzNRAlBu0QDAg8OIpAAYxaSbbZN2O6KeG8QEdXDlWxCxdCXY KrpYcmPW5iYelSzb7hKCSLv+9bLhCTwHeB5Ao6mnqbgnTSmj1ZwvFvCRdBUplg6vvpG1 bVNS98Uhs0Jayhzd6YCFHM64y6tyB8gQxqseQ5jNwmxEFtRJMvki764PRI5MbWZZCYVc RcxxeaFl0wgns9OOgtgvBJCu5TzmgL2h/H+AJAvytxKnYpNOs4GMCtzLMu2xzJxyhzVX MgV4YfXK5PWzSBKBEJGac0HNHDyzQjB/483/0mV95pzU+ZA/t5fnb6nQgFH9WpiuyHOi cZuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=9VGn7w2tNlFuEz50GpubxUEDNHCrhMt0WO2PGFie8Ao=; b=OULlcaB3i4bLwsmQuF1CMesdckl6XA09JwH9IFBkucTp5rfXKx5TJeIkULqNEYQVAi Gtyvh2VcnYb1eUXtbXux96zS2q2+qsMZydiCejLMBq8KKtuIxa3T6HVY276Hn18CeN3q ieOJZ5eAmIaAJQwNckYk1rgjNtZrSWE+zqNqZ+c2MxXgHg+imEszpx0X0qItJ8NRtLHg crIpHrLAhHXEZm7stOonHuoJ6nLQr3dwNR4DPx6a86m6VEeIfVpVhI0p+HhwqmydAvL2 YtwcjCBwP8ECjqqd2eznAEbVMQbvFwHzlIzgbl/kejXIzCJi7bcspHsuI5zGpbrpHqmT qYDg== X-Gm-Message-State: AOAM533euCPzgzBvdGkGkK9VaNwqK/DBSSMz9MuX4V+jbiiVNy1KNkuF HB8xp0b2mVaORNetD+qjpEYpVQ== X-Google-Smtp-Source: ABdhPJyUml8gBjD3JNc7G2wMLNsuJxRp8NFSdxFPy/XjyDzTEC8IZoyZ8vUF/vupgLJpqNGQsCsy1g== X-Received: by 2002:adf:c109:: with SMTP id r9mr3973330wre.261.1612363713245; Wed, 03 Feb 2021 06:48:33 -0800 (PST) Received: from elver.google.com ([2a00:79e0:15:13:b1de:c7d:30ce:1840]) by smtp.gmail.com with ESMTPSA id 17sm2919952wmk.48.2021.02.03.06.48.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Feb 2021 06:48:32 -0800 (PST) Date: Wed, 3 Feb 2021 15:48:25 +0100 From: Marco Elver To: Andrey Konovalov Cc: Catalin Marinas , Vincenzo Frascino , Dmitry Vyukov , Alexander Potapenko , Andrew Morton , Will Deacon , Andrey Ryabinin , Peter Collingbourne , Evgenii Stepanov , Branislav Rankov , Kevin Brodsky , kasan-dev@googlegroups.com, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 06/12] kasan: rework krealloc tests Message-ID: References: <995edb531f4f976277d7da9ca8a78a96a2ea356e.1612208222.git.andreyknvl@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <995edb531f4f976277d7da9ca8a78a96a2ea356e.1612208222.git.andreyknvl@google.com> User-Agent: Mutt/2.0.2 (2020-11-20) 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: On Mon, Feb 01, 2021 at 08:43PM +0100, Andrey Konovalov wrote: > This patch reworks KASAN-KUnit tests for krealloc() to: > > 1. Check both slab and page_alloc based krealloc() implementations. > 2. Allow at least one full granule to fit between old and new sizes for > each KASAN mode, and check accesses to that granule accordingly. > > Signed-off-by: Andrey Konovalov Reviewed-by: Marco Elver > --- > lib/test_kasan.c | 91 ++++++++++++++++++++++++++++++++++++++++++------ > 1 file changed, 81 insertions(+), 10 deletions(-) > > diff --git a/lib/test_kasan.c b/lib/test_kasan.c > index 5699e43ca01b..2bb52853f341 100644 > --- a/lib/test_kasan.c > +++ b/lib/test_kasan.c > @@ -258,11 +258,14 @@ static void kmalloc_large_oob_right(struct kunit *test) > kfree(ptr); > } > > -static void kmalloc_oob_krealloc_more(struct kunit *test) > +static void krealloc_more_oob_helper(struct kunit *test, > + size_t size1, size_t size2) > { > char *ptr1, *ptr2; > - size_t size1 = 17; > - size_t size2 = 19; > + size_t middle; > + > + KUNIT_ASSERT_LT(test, size1, size2); > + middle = size1 + (size2 - size1) / 2; > > ptr1 = kmalloc(size1, GFP_KERNEL); > KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1); > @@ -270,15 +273,31 @@ static void kmalloc_oob_krealloc_more(struct kunit *test) > ptr2 = krealloc(ptr1, size2, GFP_KERNEL); > KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2); > > - KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2 + OOB_TAG_OFF] = 'x'); > + /* All offsets up to size2 must be accessible. */ > + ptr2[size1 - 1] = 'x'; > + ptr2[size1] = 'x'; > + ptr2[middle] = 'x'; > + ptr2[size2 - 1] = 'x'; > + > + /* Generic mode is precise, so unaligned size2 must be inaccessible. */ > + if (IS_ENABLED(CONFIG_KASAN_GENERIC)) > + KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x'); > + > + /* For all modes first aligned offset after size2 must be inaccessible. */ > + KUNIT_EXPECT_KASAN_FAIL(test, > + ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x'); > + > kfree(ptr2); > } > > -static void kmalloc_oob_krealloc_less(struct kunit *test) > +static void krealloc_less_oob_helper(struct kunit *test, > + size_t size1, size_t size2) > { > char *ptr1, *ptr2; > - size_t size1 = 17; > - size_t size2 = 15; > + size_t middle; > + > + KUNIT_ASSERT_LT(test, size2, size1); > + middle = size2 + (size1 - size2) / 2; > > ptr1 = kmalloc(size1, GFP_KERNEL); > KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1); > @@ -286,10 +305,60 @@ static void kmalloc_oob_krealloc_less(struct kunit *test) > ptr2 = krealloc(ptr1, size2, GFP_KERNEL); > KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2); > > - KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2 + OOB_TAG_OFF] = 'x'); > + /* Must be accessible for all modes. */ > + ptr2[size2 - 1] = 'x'; > + > + /* Generic mode is precise, so unaligned size2 must be inaccessible. */ > + if (IS_ENABLED(CONFIG_KASAN_GENERIC)) > + KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x'); > + > + /* For all modes first aligned offset after size2 must be inaccessible. */ > + KUNIT_EXPECT_KASAN_FAIL(test, > + ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x'); > + > + /* > + * For all modes both middle and size1 should land in separate granules middle, size1, and size2? > + * and thus be inaccessible. > + */ > + KUNIT_EXPECT_LE(test, round_up(size2, KASAN_GRANULE_SIZE), > + round_down(middle, KASAN_GRANULE_SIZE)); > + KUNIT_EXPECT_LE(test, round_up(middle, KASAN_GRANULE_SIZE), > + round_down(size1, KASAN_GRANULE_SIZE)); > + KUNIT_EXPECT_KASAN_FAIL(test, ptr2[middle] = 'x'); > + KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1 - 1] = 'x'); > + KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1] = 'x'); > + > kfree(ptr2); > } > > +static void krealloc_more_oob(struct kunit *test) > +{ > + krealloc_more_oob_helper(test, 201, 235); > +} > + > +static void krealloc_less_oob(struct kunit *test) > +{ > + krealloc_less_oob_helper(test, 235, 201); > +} > + > +static void krealloc_pagealloc_more_oob(struct kunit *test) > +{ > + /* page_alloc fallback in only implemented for SLUB. */ > + KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB); > + > + krealloc_more_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 201, > + KMALLOC_MAX_CACHE_SIZE + 235); > +} > + > +static void krealloc_pagealloc_less_oob(struct kunit *test) > +{ > + /* page_alloc fallback in only implemented for SLUB. */ > + KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB); > + > + krealloc_less_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 235, > + KMALLOC_MAX_CACHE_SIZE + 201); > +} > + > static void kmalloc_oob_16(struct kunit *test) > { > struct { > @@ -983,8 +1052,10 @@ static struct kunit_case kasan_kunit_test_cases[] = { > KUNIT_CASE(pagealloc_oob_right), > KUNIT_CASE(pagealloc_uaf), > KUNIT_CASE(kmalloc_large_oob_right), > - KUNIT_CASE(kmalloc_oob_krealloc_more), > - KUNIT_CASE(kmalloc_oob_krealloc_less), > + KUNIT_CASE(krealloc_more_oob), > + KUNIT_CASE(krealloc_less_oob), > + KUNIT_CASE(krealloc_pagealloc_more_oob), > + KUNIT_CASE(krealloc_pagealloc_less_oob), > KUNIT_CASE(kmalloc_oob_16), > KUNIT_CASE(kmalloc_uaf_16), > KUNIT_CASE(kmalloc_oob_in_memset), > -- > 2.30.0.365.g02bc693789-goog >