From patchwork Tue Jan 5 18:27:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 12000073 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, 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 DC01CC433E0 for ; Tue, 5 Jan 2021 18:31:17 +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 41E1522D57 for ; Tue, 5 Jan 2021 18:31:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 41E1522D57 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=ZeTc4+pmWdXt+uvoFR9zvpv2fzLH0sGMcMAcMaE2Z5I=; b=Smo9G/hpxfvFasIpIeGPQvheC DoiuI9ttWrS5P0Nym9/tdjW4UMtwRV7yX8mRtzoJpHE3AmN3DToTLDrvh/aX5wf6hzZmGXpzFUIbw ZYgffrjzha5tiCzvb1dNt9THLzwf2DqL28EXuHe/gOH7sTCjM+t9mbXqLATq+/EIMSvThpk9D0ORf S/BFUzHQg9zIe6IITawbF9py6k7xLTYkKT1QBedVXF7fxxwrd2P84WgqRx3SdJTE7ZwKiXYpPald7 Dvrs1LyfQvgC+dn3BHpC6BxGavdOK7+3YfeRkmnwcnhbGjjxDASxBIaXBIchj7pUSyB1U+t60ez9H exlUpIKFQ==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kwr4X-0007Bo-6d; Tue, 05 Jan 2021 18:28:45 +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 1kwr44-0006zw-KL for linux-arm-kernel@lists.infradead.org; Tue, 05 Jan 2021 18:28:23 +0000 Received: by mail-wm1-x349.google.com with SMTP id r5so171901wma.2 for ; Tue, 05 Jan 2021 10:28:13 -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=GQiVJTM9ZOPgYdzmXpL1IlsdxHwSRDXsv5mwJrm9Wwo=; b=JoaeuIG7HihlaN83F7/kwwxXDu9niWxBRzum03JUDLH2uFs7EnTQFwi8rVZ2IDhwbJ h3zutd8MNUAnXCo8b+QbZnijbklFefySppWdnrudFFYcBhn8QSKUPjxhbe1IzXUCHwIa ue3o4j96pv2bZrQnQ4OxL2JYxJta4YrHtflIXIqwe/lCJZXrd3UB1Kq3HRmCkMxl4HSq 3BeGoXVcjCdyOOvclngnbjhVFkpricrAkjgDjf3Pbuqcc0PraW1GCX1omaqMXQVAUp5h 8dd+HylE0lxHrFIcyWi6Eox0hdZd62HBtIa3VC62nk0a7FGzdzYCs94/R/ISRDZ6cZWK laDA== 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=GQiVJTM9ZOPgYdzmXpL1IlsdxHwSRDXsv5mwJrm9Wwo=; b=mHbPdrMvZqIJEieRRbY7cZyAKYFk2zDpZ3da9BG+UsbT864QT65dK2tnqY5dTaUfov 37AEH1hkWqnTiT7OoVIi6Yp12Fbiyp/7lCRR2HEZG9TMySdwIFz1P1013x7pFTBSbRdm J0iOxCJnnzl2dQ0HP7mou/qNJoGWG+C5ZtFN8GiCURv+CpUCrnoMjOf3t4vFz5OMduhL roWgSWdFh7b3itdx2sgqV/sFgZOAp0eVFpDcD+FkUtwq3tindFVCZUOdmhy41EbYyhlr mPZTHprPH2LOmN2oKbx2ssErWH4bxLNsTvx9dUnSbdTxETc4EGnw4bmQDLjCXXm83qCJ R4mw== X-Gm-Message-State: AOAM530ww31UqA3r293K+ipRMZXQ86F6RGdB++858+zhoY2j/ut6KfZS F2nYZddeftmhJAlhLdCLoaChTrPmMqaIt0w8 X-Google-Smtp-Source: ABdhPJxgQDbM2UB8IUmcpBsgZOgp4XqvnxQOTjQ8KJlUs8FPGe2Sglts3tXhMr+d9129lE4oaJX2FWpIyiueOUJa X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:a1c:804a:: with SMTP id b71mr394950wmd.21.1609871292711; Tue, 05 Jan 2021 10:28:12 -0800 (PST) Date: Tue, 5 Jan 2021 19:27:48 +0100 In-Reply-To: Message-Id: <0f20f867d747b678604a68173a5f20fb8df9b756.1609871239.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.29.2.729.g45daf8777d-goog Subject: [PATCH 04/11] kasan: add match-all tag 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-20210105_132816_801650_4FC98050 X-CRM114-Status: GOOD ( 19.14 ) 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 , Evgenii Stepanov Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add 3 new tests for tag-based KASAN modes: 1. Check that match-all pointer tag is not assigned randomly. 2. Check that 0xff works as a match-all pointer tag. 3. Check that there are no match-all memory tags. Note, that test #3 causes a significant number (255) of KASAN reports to be printed during execution for the SW_TAGS mode. Signed-off-by: Andrey Konovalov Link: https://linux-review.googlesource.com/id/I78f1375efafa162b37f3abcb2c5bc2f3955dfd8e --- lib/test_kasan.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++ mm/kasan/kasan.h | 6 ++++ 2 files changed, 99 insertions(+) diff --git a/lib/test_kasan.c b/lib/test_kasan.c index 46e578c8e842..f1eda0bcc780 100644 --- a/lib/test_kasan.c +++ b/lib/test_kasan.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -790,6 +791,95 @@ static void vmalloc_oob(struct kunit *test) vfree(area); } +/* + * Check that match-all pointer tag is not assigned randomly for + * tag-based modes. + */ +static void match_all_not_assigned(struct kunit *test) +{ + char *ptr; + struct page *pages; + int i, size, order; + + for (i = 0; i < 256; i++) { + size = get_random_int() % KMALLOC_MAX_SIZE; + ptr = kmalloc(128, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); + KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL); + kfree(ptr); + } + + for (i = 0; i < 256; i++) { + order = get_random_int() % 4; + pages = alloc_pages(GFP_KERNEL, order); + ptr = page_address(pages); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); + KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL); + free_pages((unsigned long)ptr, order); + } +} + +/* Check that 0xff works as a match-all pointer tag for tag-based modes. */ +static void match_all_ptr_tag(struct kunit *test) +{ + char *ptr; + u8 tag; + + if (IS_ENABLED(CONFIG_KASAN_GENERIC)) { + kunit_info(test, "skipping, CONFIG_KASAN_SW/HW_TAGS required"); + return; + } + + ptr = kmalloc(128, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); + + /* Backup the assigned tag. */ + tag = get_tag(ptr); + KUNIT_EXPECT_NE(test, tag, (u8)KASAN_TAG_KERNEL); + + /* Reset the tag to 0xff.*/ + ptr = set_tag(ptr, KASAN_TAG_KERNEL); + + /* This access shouldn't trigger a KASAN report. */ + *ptr = 0; + + /* Recover the pointer tag and free. */ + ptr = set_tag(ptr, tag); + kfree(ptr); +} + +/* Check that there are no match-all memory tags for tag-based modes. */ +static void match_all_mem_tag(struct kunit *test) +{ + char *ptr; + int tag; + + if (IS_ENABLED(CONFIG_KASAN_GENERIC)) { + kunit_info(test, "skipping, CONFIG_KASAN_SW/HW_TAGS required"); + return; + } + + ptr = kmalloc(128, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); + KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL); + + /* For each possible tag value not matching the pointer tag. */ + for (tag = KASAN_TAG_MIN; tag <= KASAN_TAG_KERNEL; tag++) { + if (tag == get_tag(ptr)) + continue; + + /* Mark the first memory granule with the chosen memory tag. */ + kasan_poison(ptr, KASAN_GRANULE_SIZE, (u8)tag); + + /* This access must cause a KASAN report. */ + KUNIT_EXPECT_KASAN_FAIL(test, *ptr = 0); + } + + /* Recover the memory tag and free. */ + kasan_poison(ptr, KASAN_GRANULE_SIZE, get_tag(ptr)); + kfree(ptr); +} + static struct kunit_case kasan_kunit_test_cases[] = { KUNIT_CASE(kmalloc_oob_right), KUNIT_CASE(kmalloc_oob_left), @@ -829,6 +919,9 @@ static struct kunit_case kasan_kunit_test_cases[] = { KUNIT_CASE(kasan_bitops_tags), KUNIT_CASE(kmalloc_double_kzfree), KUNIT_CASE(vmalloc_oob), + KUNIT_CASE(match_all_not_assigned), + KUNIT_CASE(match_all_ptr_tag), + KUNIT_CASE(match_all_mem_tag), {} }; diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index 3b38baddec47..c3fb9bf241d3 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -36,6 +36,12 @@ extern bool kasan_flag_panic __ro_after_init; #define KASAN_TAG_INVALID 0xFE /* inaccessible memory tag */ #define KASAN_TAG_MAX 0xFD /* maximum value for random tags */ +#ifdef CONFIG_KASAN_HW_TAGS +#define KASAN_TAG_MIN 0xF0 /* mimimum value for random tags */ +#else +#define KASAN_TAG_MIN 0x00 /* mimimum value for random tags */ +#endif + #ifdef CONFIG_KASAN_GENERIC #define KASAN_FREE_PAGE 0xFF /* page was freed */ #define KASAN_PAGE_REDZONE 0xFE /* redzone for kmalloc_large allocations */