Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp166753pxb; Fri, 15 Jan 2021 09:56:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJxLbj4pChULkijl+ep1YhdPm61CxRbxepKzwIioahYikczHmNdRIO1wkAKIORsDdsXnUlhu X-Received: by 2002:a17:906:e84:: with SMTP id p4mr10007786ejf.141.1610733405632; Fri, 15 Jan 2021 09:56:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610733405; cv=none; d=google.com; s=arc-20160816; b=yQibt076sl/sJNn6VZ46i+SSLIr7fp2rlfjBIqld2PLfO53Z5kv4N+4ylVuagEG1CI 7W/JE/9RafTSUW86Tzs94TLZMm9T8LDVB//31mcygli9VMZd+Sis1pq6+eig8JAcG+lP IBtfBujL6nSvus4+ZzWnKqs8BiDvlud5wro1kxWeK1Ya42u5CooNkfmCWZXyE47Nvp0c tVqQa8YQicGNY17mwryJrbLNYGlmKeMPTFtoD7BU4JexvM9yexshLx8d3RxkRL6aIHOo M+e/jI3cQm/R4Wgg+obxxhwlPfQ/z2CLF3d7Rz+a1xD0B1NoR2t+sS4H+DITlLfJ5JOf lbbA== 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=jaXEIDJDqFs/aljL7PAh1mqq4dzHDTnXLxZuY0g6DDSg780+vIBCxwUfWVbW8Eh69L u86xl21V4F7Ksy2eJ4a3lHP/SC/bJ4W/qbDRmYAQo7Ob1ecipDJdRLi//tqpJSnU7Nkt c7INPtJ5P0kxWLaE/Ec6TLh6WyuH1hvUnRgdGnGofuqrkBEPi16a1NJ0OGroRiBDdo0J DrniKx6bwOmakIEaO/TTs+svcF9Pi97/ZzEuFiiW78PzLp6EC8G7s3r+9I4o2maER1OE KJrzSVP/3FsG1z90vU9ss18SkOCBUtziQmrYHtnl0mF9NOtNAl/LT7zS3vkpKzQFrdQm vpMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=VQk6OorM; 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 n12si2495914edo.512.2021.01.15.09.56.21; Fri, 15 Jan 2021 09:56:45 -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=VQk6OorM; 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 S2387448AbhAORy3 (ORCPT + 99 others); Fri, 15 Jan 2021 12:54:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52178 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387433AbhAORy2 (ORCPT ); Fri, 15 Jan 2021 12:54:28 -0500 Received: from mail-wr1-x44a.google.com (mail-wr1-x44a.google.com [IPv6:2a00:1450:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C5F9C0617A0 for ; Fri, 15 Jan 2021 09:53:32 -0800 (PST) Received: by mail-wr1-x44a.google.com with SMTP id z8so4481327wrh.5 for ; Fri, 15 Jan 2021 09:53:32 -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=VQk6OorMUwLDaqyBJFKoPy2yn3wwkSkoZL+2P8uj4DcKBFvi6k8GNQdU1DS2/rkrYw lKR/egnmlfGdROt2C1EVSe2C4PDZ3AcYEDSefZ/Y8gtSweY+Bw4BJzQTz8Bn5bfCNmn1 UEJJO5gX14yaq9s0wNv9Zs1qsa55y+unS+yna7FUJef9Ah70JPs4Jczj4JWxM1orNbFk wMzGo+Hbkm43oCtOPZFqAWn7+QjtXmvNPUhjl075iBILoiPwYcmf+i9wLtobiLSS4433 i0z73npM9/vUPyyLajdKPDlXxkrvAP5YrwEJ7zOtd6OorNHwf51+fp0gE5V+sDVWznTx kAWQ== 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=iDszDsuzeuSio+mZksHSQYHXtE1ITCUH5Wp4v02K1XcbMd3aOo9n5SxnSBFx1/3z4a 7Skh1jpcenbx6TGQeXkovxem5sB+fISTzErVfb/9zX2CcFu9EcoprOlMt5Rj4Aqm7mgW B+/Ocm1NdDwgEyWCd8Xm5pTmEsRJCjhqem7Ra+1tAjHkzHB9mMhDzHr0j1r5rjX3uYZ1 IYiVEfRYKtRaAk+cu39HF5pcr3ECv1Q+3hoHDACeoxvMI7liHP/7yhpjeqwONjQ3Xcd2 4qI800pLlST1/R+2vxwtF04K2XY2Bu6Bk2W2RL3cGiVQtLIzBEkssnF1lkg48ku/TDHZ mB8A== X-Gm-Message-State: AOAM5337ZCnkbqU7U50WiDrxeqFweU6BIa7c2V9lSM7Zc2NllytUp6Xt s5M+rRR/VX3myV9HbQBWdxLCPayxAtyhuS4O 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:40ca:: with SMTP id m10mr9700946wmh.54.1610733211307; Fri, 15 Jan 2021 09:53:31 -0800 (PST) Date: Fri, 15 Jan 2021 18:52:50 +0100 In-Reply-To: Message-Id: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.30.0.284.gd98b1dd5eaa7-goog Subject: [PATCH v4 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