From patchwork Thu Jan 14 19:36:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 12020775 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 9A864C433DB for ; Thu, 14 Jan 2021 19:41:30 +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 ADDD72399C for ; Thu, 14 Jan 2021 19:41:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org ADDD72399C 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=ZL3XcPWzjQ6ap3l+jjCmOu0YWeZrwWxUoW//jYBxtbk=; b=bgxhLQC4hipEvkmgax3SUV/Lt iV5jhcHNDFP8kmohhttb763hzbmu4EVB2JXH2t1YmPlQZy+jfS6O6RVyA9CZRupt7im8WUZL3zvH5 xG73YJZaKN2iEG4jVlmF0uTzjsLfSMwJYH9sO3RTrgjdSbyDozkCnfAxl/djuX2oW2daOVgKMFaSt St8x6XrWJZwQPoIUlW4RJgRg93+UJXQmHKGAGH+fKlDXYuW9cR+x2cUhNbTUGO81C2EwoPYHyJFFA 3P0thc85DRj/wTf/x3bOUG+CfaKMCP+Wql5hdI/ArVbphsqML5U2/HCzsiwxja0RstUTs3TTW62Cm JsclGoDng==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1l08SG-000523-Nd; Thu, 14 Jan 2021 19:38:48 +0000 Received: from mail-wm1-x34a.google.com ([2a00:1450:4864:20::34a]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1l08Qe-0004L5-5W for linux-arm-kernel@lists.infradead.org; Thu, 14 Jan 2021 19:37:18 +0000 Received: by mail-wm1-x34a.google.com with SMTP id u18so2239306wmu.4 for ; Thu, 14 Jan 2021 11:37:05 -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=dVTMart08UEQbM+6MiYEncY10URoyB/dLUX7v9OKI+o=; b=d+cZ3zDJlDDghuzVP57ba1H7PPjR+Bb3JSuOZtXz5IHbRCN/8rXuTzjWVPzqj5PZAR jR1beLGfyaqRl58Fm230cgJMCjrV1CTDkH7MDif8NZr/DEOKjJqLU4ukQfQWwwUJIfLd ob3jTqTpKbLb5Pt2Jxmry+trKGCGUZLEGxo6J9E6LHiedljvEMgOgFlPNlJwHPH8cAOp BVdvneV2mYHrhzFXxO9PTGRTq5jG6oeo+M65TUM0hvV7NbelijmgV2Wmo915Aa5JdOD3 Efi8JJIL6uxsfBgM7SN3BzDTL+fULf3kxmrCOBT/1zayReZEPLBm24VQ6VW6gdejGQPZ MIQw== 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=dVTMart08UEQbM+6MiYEncY10URoyB/dLUX7v9OKI+o=; b=SNtKBhaXhXQsfNKgdBbF1p4fpuNDCFHR6l71I/3xn32x0NQeYgx+EPTBjBTi3IVh9m zV6OnBRsXhO4TOfh+8vlOb6KUIgX1YxHqi+0KJvRAhDBbt75paukOEHrpfAMtM/pKjJC RKFrhbsJ00Q9X+XErYsjtYvajjgZ0c/J4i3dz0XdeMJndSMsAFbh8CVnBzztor8L7Ons Xnbv6SiZ9LsfcAzuf8x7QzY1C6NwjDmeL3koImhT0Pgj0X3JEC8OfPNtkk4LjZuYXKmG OHHtEdi97fjb8rOIyNHkPFy159HsE2YOi7z14b20xuRW/tgIyRdJ1x5Hoa3Har+M9w7q ngSA== X-Gm-Message-State: AOAM533oIpF4Coxz1cQscErdk59nNvo9+0jn266h7MOj0dmpPlIGX75d P6pRGziURwaYdGcp3otgGtg8UQTGhJDegdMy X-Google-Smtp-Source: ABdhPJwOhZ2N+3U1G2d0Zh+pyKYPmWW4eTJXItMnymiz5pTTz8EMdXlIUGpJcS5e2kSThmg7OLhPz/5RRJDLrOmq X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:a05:600c:208:: with SMTP id 8mr5434266wmi.143.1610653024740; Thu, 14 Jan 2021 11:37:04 -0800 (PST) Date: Thu, 14 Jan 2021 20:36:29 +0100 In-Reply-To: Message-Id: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.30.0.284.gd98b1dd5eaa7-goog Subject: [PATCH v3 13/15] kasan: add proper page allocator tests From: Andrey Konovalov To: Andrew Morton , 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-20210114_143708_326678_2C83E253 X-CRM114-Status: GOOD ( 14.50 ) 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 , 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 566d894ba20b..ab22a653762e 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),