Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp528389pxb; Thu, 14 Jan 2021 11:41:26 -0800 (PST) X-Google-Smtp-Source: ABdhPJwnpmgnhIvcUJK3yfz9zTJ6cJC768sALQRrkXqCHYvnRV1e9GgT87kTYTrufIUrXrXAIrt8 X-Received: by 2002:a50:f299:: with SMTP id f25mr6864503edm.133.1610653286098; Thu, 14 Jan 2021 11:41:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610653286; cv=none; d=google.com; s=arc-20160816; b=qmvaVESye/9KZokQiKb1ycT/Uhjv/OgT8KTdFGeQ59NHKYBFOTzWmNxhOSFdzFFEuX RY8gLJs1WmuKAWJ77Fj3sYh/le4paEntPHwy49CYnGaWOdfWhdfH4svgJj+1LaNqrbRj 1x0b5V2g5qWQpvCse4bBiHGfe7PayPwi2TsaIlblrc8KGU4BHtPwFK2/1D9ZXYlo4qeg /J4PSkcrQrOIC3Ekhk2KXKqLDh7HV2WwInLyZx+csqKb0dRcAKcYzXZFWBW/2zWfdRr8 QRSIXnUPQA05FnyOEs5/xLAnYcbGiS4rtSBYO02AJBmVBy7L3sYBRiDuyQk1iNPJjEhG U1iA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:sender:dkim-signature; bh=dVTMart08UEQbM+6MiYEncY10URoyB/dLUX7v9OKI+o=; b=HVAmT0jTXE84nwWV/Lw8hJSuk5ICPjVqORWHaxYh0V1C7IHHX35DYxQ64u5cQNVi99 kCEQnBAXu8r+Hk0NM7jQIlYI6p9yormnoxqUr4bIINHnpNqmyVp6BagSgltS2cAgcsP3 zt7pyd0acTZc6mW7FMZRy5vAq5UCwtQT2R3l2bHIeOoTBsqnPvderqdyeZtZzoAwkvrk vLNoLsIS4hyMAB3DmK+BW04osngRUl1nGgT9bP3OCHNROVdsQb3t9oZParG9lfP56Tgw YkDKiRkEiuJorqgokG1ghUcJOH9FSZuBY1tnYC5DsmkZEfIlZz51dqn8vvURmkA4paUH OO/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=d+cZ3zDJ; 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 by20si3517941ejc.239.2021.01.14.11.41.01; Thu, 14 Jan 2021 11:41:26 -0800 (PST) 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=d+cZ3zDJ; 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 S1730226AbhANTiR (ORCPT + 99 others); Thu, 14 Jan 2021 14:38:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47034 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728380AbhANTiD (ORCPT ); Thu, 14 Jan 2021 14:38:03 -0500 Received: from mail-wm1-x349.google.com (mail-wm1-x349.google.com [IPv6:2a00:1450:4864:20::349]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03971C061799 for ; Thu, 14 Jan 2021 11:37:06 -0800 (PST) Received: by mail-wm1-x349.google.com with SMTP id f18so2260866wmq.1 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=G+pgYdUX1kFPrmGKXaAIYvAjorAZuAHiLhXKRNWxngdZCLchtq5rniq5VhGKPYSYLv VsuHE+T0Nj+K8tOyPJSqWlSkVbi93HqNIKMxjOZG5oxdn1w0XnI0Cuf13WfZfXdF+rQt I2RlAHOytE43oKsioXiB5BEb+A2XsMOgoCLol6d9RyLaXA2ckWT9+D5lviHkpU9G+gA1 jhx6Ta6E3MCvC77+0EaHyxrW0jxLps989Mdwpdilbh9UkScHYBdX2WQbjV6TAS2S1nZN vEX4BS99mgPQv5uOA8rhrTZ/6G9axtFqOsBkZjFd1jmLwKl0tjF/4utZEPU+AZSi0eV3 hEqg== X-Gm-Message-State: AOAM530dT0TNUzQYKA1aVnX5w1wsEl18I6cRAFAl91glatR0xIMAnK3G WU9bCMGANUYfRXOFsJObXIOb4MIYdePUpBqz Sender: "andreyknvl via sendgmr" 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 Cc: 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, Andrey Konovalov Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.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), -- 2.30.0.284.gd98b1dd5eaa7-goog