From patchwork Wed Jan 13 16:21:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 12017325 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=-17.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable 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 ABA37C433DB for ; Wed, 13 Jan 2021 16:27:16 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 4E9CC2339D for ; Wed, 13 Jan 2021 16:27:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4E9CC2339D Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:To:From:Subject:References:Mime-Version:Message-Id: In-Reply-To:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=t+j4UzJsVpg9zM7n5kdKDdTX2MPA+xhN39H6sJ5Yw/I=; b=dJCE6VHZPS2ORTKeWL0zarq0j 1hjkmzmqtz+wocc8fm1UGTSMz68l0nAebesxnoM7+yAdr3hwGHgn2rXfCkTpOaM7jsZSnsGBb7/Ox c9x9hWenH9Qy5Yqs1Lo/kpIJG6qUJ9wIQJGqMpFngfC8YPzoBDDE4tBzShhlUQAWzWvVzLG796sJx Ask2WGWUmEIPHg8/X6Oyshoz8PRkiMkjHxeoeH9IeqtjeYInHuS2OQ44irNhDydftW4urrYC0U7pM vfhgvUD2GakPXPj9Xn++z/iEykVQpbO/LWkmt1vFd+8oKpwYWjXpap5AUCJn54eAY5N2RwDWxt7in PSmW6oBkw==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kziwF-0007z1-Jh; Wed, 13 Jan 2021 16:24:03 +0000 Received: from mail-wm1-x349.google.com ([2a00:1450:4864:20::349]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kziuZ-0007KA-C2 for linux-arm-kernel@lists.infradead.org; Wed, 13 Jan 2021 16:22:41 +0000 Received: by mail-wm1-x349.google.com with SMTP id u18so1010831wmu.4 for ; Wed, 13 Jan 2021 08:22:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=qQVzFZxhqiBzY9F1wdp+d+EdRYAfotcsjSYx9y4am/0=; b=BR5OmmjyR6sTYkY6yO/wiMj9obAVT5Qvv1Fag/2rpA2efyniv7YkbqqAhVzFjsRd3N XVbx3b1j7PqSVvUqSZHuUCZWsDoIwC26NGoYnX0Zbk7Yn09p6iVqeEstl0xwxsj0ldhK UVLdkpj2EOn3kjsOylI3WQo45ZSJBz82jiByaCu50NemoLylow9ZlA2RaG1sE7SP7056 i9pLyzQU1YzVFyvOjaUpD8F5JAOZrzv0XK9Ys51vkP0eaYVBE8g6R/IQ9NzYMnrfssb4 BGuYpiG+EXafCIrz7lPCgQHTxsEJkNh4ibiJM2BqJKWbujgEox/0v9TiSqmK4QFsAqGh yzFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=qQVzFZxhqiBzY9F1wdp+d+EdRYAfotcsjSYx9y4am/0=; b=YY00QEQTW/lmi6JLZy28iK5BXuApOyn9f74Gf+vWs2RkNKijQjnlBXGSnDX+ZQTVa/ z30L6UUw4DM7jhF5t6wtWiVJ38yIAMIHHJugmWnetdDogvWV4bX4ra3heoAkKwiq5WBP nqvPmt8O3rBiCXBSu0OVCH5XugvmaepeyErJIFJ600qemytVQ74ARRMToIi/HCP5l20C LvX90DXub79WSEUriEs6Dmm6Fh1UkBBctF/Zd8c+d/oRqkDI4t4/PbInYa7PSpJV3R9K pWkTHk7V7+uh7iGWtwvBkSPPDFvJce7SNfC76+5TZvHuhuNL2F9AysXeUiCQBgIezH2x LG9g== X-Gm-Message-State: AOAM531xuQ8goZ5P2qv9nPHlz5A+/fGf8f1fRtUgL1qoQ5YyQ9QlUkwm ZbJQ+4bAnHDnJb+BopOTPpZXbmjBLjOyLxXW X-Google-Smtp-Source: ABdhPJzSKosgGLWyRiKWPdCCVC8Y1iQeq2wYpLHPbwSetVm0yj+8VcVFi5Ojpa3SzDGIXUgPVqRFY8BTble0U4Y1 X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:a5d:4f82:: with SMTP id d2mr3382368wru.87.1610554936199; Wed, 13 Jan 2021 08:22:16 -0800 (PST) Date: Wed, 13 Jan 2021 17:21:39 +0100 In-Reply-To: Message-Id: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.30.0.284.gd98b1dd5eaa7-goog Subject: [PATCH v2 12/14] kasan: add proper page allocator tests From: Andrey Konovalov To: Catalin Marinas , Vincenzo Frascino , Dmitry Vyukov , Alexander Potapenko , Marco Elver X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210113_112219_541987_509E4105 X-CRM114-Status: GOOD ( 14.60 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Branislav Rankov , Andrey Konovalov , Kevin Brodsky , Will Deacon , linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, Andrey Ryabinin , Andrew Morton , Peter Collingbourne , Evgenii Stepanov Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The currently existing page allocator tests rely on kmalloc fallback with large sizes that is only present for SLUB. Add proper tests that use alloc/free_pages(). Link: https://linux-review.googlesource.com/id/Ia173d5a1b215fe6b2548d814ef0f4433cf983570 Reviewed-by: Marco Elver Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- lib/test_kasan.c | 51 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/lib/test_kasan.c b/lib/test_kasan.c index 710e714dc0cb..5e3d054e5b8c 100644 --- a/lib/test_kasan.c +++ b/lib/test_kasan.c @@ -147,6 +147,12 @@ static void kmalloc_node_oob_right(struct kunit *test) kfree(ptr); } +/* + * These kmalloc_pagealloc_* tests try allocating a memory chunk that doesn't + * fit into a slab cache and therefore is allocated via the page allocator + * fallback. Since this kind of fallback is only implemented for SLUB, these + * tests are limited to that allocator. + */ static void kmalloc_pagealloc_oob_right(struct kunit *test) { char *ptr; @@ -154,14 +160,11 @@ static void kmalloc_pagealloc_oob_right(struct kunit *test) KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB); - /* - * Allocate a chunk that does not fit into a SLUB cache to trigger - * the page allocator fallback. - */ ptr = kmalloc(size, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 0); + kfree(ptr); } @@ -174,8 +177,8 @@ static void kmalloc_pagealloc_uaf(struct kunit *test) ptr = kmalloc(size, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); - kfree(ptr); + KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0); } @@ -192,6 +195,42 @@ static void kmalloc_pagealloc_invalid_free(struct kunit *test) KUNIT_EXPECT_KASAN_FAIL(test, kfree(ptr + 1)); } +static void pagealloc_oob_right(struct kunit *test) +{ + char *ptr; + struct page *pages; + size_t order = 4; + size_t size = (1UL << (PAGE_SHIFT + order)); + + /* + * With generic KASAN page allocations have no redzones, thus + * out-of-bounds detection is not guaranteed. + * See https://bugzilla.kernel.org/show_bug.cgi?id=210503. + */ + KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC); + + pages = alloc_pages(GFP_KERNEL, order); + ptr = page_address(pages); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); + + KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0); + free_pages((unsigned long)ptr, order); +} + +static void pagealloc_uaf(struct kunit *test) +{ + char *ptr; + struct page *pages; + size_t order = 4; + + pages = alloc_pages(GFP_KERNEL, order); + ptr = page_address(pages); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); + free_pages((unsigned long)ptr, order); + + KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0); +} + static void kmalloc_large_oob_right(struct kunit *test) { char *ptr; @@ -903,6 +942,8 @@ static struct kunit_case kasan_kunit_test_cases[] = { KUNIT_CASE(kmalloc_pagealloc_oob_right), KUNIT_CASE(kmalloc_pagealloc_uaf), KUNIT_CASE(kmalloc_pagealloc_invalid_free), + 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),