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=-16.6 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT 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 EBEBEC433E6 for ; Tue, 5 Jan 2021 18:28:33 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 9CDD422D06 for ; Tue, 5 Jan 2021 18:28:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9CDD422D06 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 113788D00AC; Tue, 5 Jan 2021 13:28:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0C22A8D00AB; Tue, 5 Jan 2021 13:28:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F18D08D00AC; Tue, 5 Jan 2021 13:28:32 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0048.hostedemail.com [216.40.44.48]) by kanga.kvack.org (Postfix) with ESMTP id DC5898D00AB for ; Tue, 5 Jan 2021 13:28:32 -0500 (EST) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 9989A824999B for ; Tue, 5 Jan 2021 18:28:32 +0000 (UTC) X-FDA: 77672556864.05.fog35_1c0649a274db Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin05.hostedemail.com (Postfix) with ESMTP id 6E19718015CC0 for ; Tue, 5 Jan 2021 18:28:32 +0000 (UTC) X-HE-Tag: fog35_1c0649a274db X-Filterd-Recvd-Size: 6677 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf44.hostedemail.com (Postfix) with ESMTP for ; Tue, 5 Jan 2021 18:28:31 +0000 (UTC) Received: by mail-wm1-f73.google.com with SMTP id u9so60849wmj.1 for ; Tue, 05 Jan 2021 10:28:31 -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=oRho7+sQYL8W8g66lzn3SO+/RCe3iCmUxBQsmdc55nY=; b=VubKJ4tGao6rr39K7dEx8xeHG8th8u0W76NMnX2RgStyrhptBxDAuVLL2jUWFyKyVb CiSab5ZOJMH+oUNih6ugHcAhtNgXlG0WH8r9T70W+LSBjDC/g6VgoyvGP4teEnJffBYA 9vUy5dDJA+86MKvcD0pZN9HyN3Fx+zmW6tD8wvH+/oBohedPUCQSeH8UBzRaLjscXver 3z2flUWE3gPwKy6z5TQ3zusM3e3GepNs/zVftxGPm6lCwhtemL+D3Q6Y/obzjMmWhSVp 4DFFpbjNizMfHnG/4zp4JX6aflPp8f0lzJSigNmUK8b9By/StVB2BOou2FiQ9VY1M0LC k34A== 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=oRho7+sQYL8W8g66lzn3SO+/RCe3iCmUxBQsmdc55nY=; b=PZDWHMXuJvP0ON7DmacQdMAWqzB6NoRT74UtbDzyWr7dNDLZKYPT+NdUkoIWU8J9WI Jy1aGazr5mUTwy9dFAqw3fJMiBjAwcODqX+encOKVLHiRFvpoDHR0GDn1jT8sTIpzAtI YcGQ/IQnYQveztim5MepWlUFBWcGASjJwZHhf/G9GtSQTMAj+OC6ADB9lzbS5ZAj5xjg HiZ7BBPV9QUCxWa8q1udkuIZrsYmCadkL84sCMWShYIjFAATQXSjMdQfvpIXgAKxD+c9 52vfRthQrEVQVUyKP8VFrqZvqDSNvb09E1aMhTnN0kIVxUHVxGpPs1O4SvoKS2z1mvYo qasw== X-Gm-Message-State: AOAM531pkFGiltDAKypswF7jOFtwp84ZhttzcioS1AoKyImX3ynjsSrK nYU4ESK+X2v4KzV8bvlwWJM9k7L3n/Fkk7bf X-Google-Smtp-Source: ABdhPJxoWEP6BJ1reenr7C4SoK8UHxtinLKxkeHYeahf+7ie4bLyMFrFVqrKacmxVO7J7mnxTF4jJg0mL8c0//vU X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:adf:9b98:: with SMTP id d24mr846317wrc.240.1609871311014; Tue, 05 Jan 2021 10:28:31 -0800 (PST) Date: Tue, 5 Jan 2021 19:27:55 +0100 In-Reply-To: Message-Id: <15ca1976b26aa9edcec4a9d0f3b73f5b6536e5d0.1609871239.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.29.2.729.g45daf8777d-goog Subject: [PATCH 11/11] kasan: add proper page allocator tests From: Andrey Konovalov To: Catalin Marinas , Vincenzo Frascino , Dmitry Vyukov , Alexander Potapenko , Marco Elver Cc: Andrew Morton , Will Deacon , Andrey Ryabinin , 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, Andrey Konovalov Content-Type: text/plain; charset="UTF-8" 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: 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(). Signed-off-by: Andrey Konovalov Link: https://linux-review.googlesource.com/id/Ia173d5a1b215fe6b2548d814ef0f4433cf983570 --- lib/test_kasan.c | 54 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/lib/test_kasan.c b/lib/test_kasan.c index 6261521e57ad..24798c034d05 100644 --- a/lib/test_kasan.c +++ b/lib/test_kasan.c @@ -128,6 +128,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; @@ -138,14 +144,11 @@ static void kmalloc_pagealloc_oob_right(struct kunit *test) return; } - /* - * 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); } @@ -161,8 +164,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); } @@ -182,6 +185,45 @@ 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. + */ + if (IS_ENABLED(CONFIG_KASAN_GENERIC)) { + kunit_info(test, "skipping, CONFIG_KASAN_GENERIC enabled"); + return; + } + + 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; @@ -933,6 +975,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), -- 2.29.2.729.g45daf8777d-goog