From patchwork Thu Feb 16 23:45:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Elver X-Patchwork-Id: 13144151 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D252BC61DA4 for ; Thu, 16 Feb 2023 23:45:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6808D6B0074; Thu, 16 Feb 2023 18:45:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 62FE96B0075; Thu, 16 Feb 2023 18:45:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4F8046B0078; Thu, 16 Feb 2023 18:45:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 405976B0074 for ; Thu, 16 Feb 2023 18:45:44 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 16626A12D7 for ; Thu, 16 Feb 2023 23:45:44 +0000 (UTC) X-FDA: 80474789808.25.75557FE Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf01.hostedemail.com (Postfix) with ESMTP id 2F0EC4000D for ; Thu, 16 Feb 2023 23:45:40 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=kxkMJWrb; spf=pass (imf01.hostedemail.com: domain of 3I8DuYwUKCIQmt3mzowwotm.kwutqv25-uus3iks.wzo@flex--elver.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3I8DuYwUKCIQmt3mzowwotm.kwutqv25-uus3iks.wzo@flex--elver.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676591141; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=gW18Fl3dm52InVgglE8m5dF8kxrMU+Nd8TZ0107EoLE=; b=P9XeQQtG5gdvKWuKDp5A7ZFaATOOl72z1sWdHqdd+22BaBof/6/vtiAgs2p9Q65RQYyT2k 6j9PGueTMZKUBEbZs+ncw65I2ahI+7OKZYmwZTrNN5G25/0bkKPKpMOFQ/uETZPef9uMBA Xg/vLEIvARugUA+tchAHkvDrYtzfiBU= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=kxkMJWrb; spf=pass (imf01.hostedemail.com: domain of 3I8DuYwUKCIQmt3mzowwotm.kwutqv25-uus3iks.wzo@flex--elver.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3I8DuYwUKCIQmt3mzowwotm.kwutqv25-uus3iks.wzo@flex--elver.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676591141; a=rsa-sha256; cv=none; b=G8ZgzQUqxDYX2fERgfS+vfL/vYZDOTnwgkRCbSPXrFOX4IW4ml0MzELuj+dlaZ/5AVB5Zk 6RXVJMQTrSRCqmdmaLCmjNgZuKzmCs+3/5OlBc9vOiXLGZNPMtgDqwa65tATE9FZTuRNsZ xLiX1LngJHYK07YK+7Kcx0iHko85G+c= Received: by mail-ed1-f74.google.com with SMTP id bo27-20020a0564020b3b00b004a6c2f6a226so3550851edb.15 for ; Thu, 16 Feb 2023 15:45:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=gW18Fl3dm52InVgglE8m5dF8kxrMU+Nd8TZ0107EoLE=; b=kxkMJWrbmyrtSo3LrSby0SwZzdaqZYA+OtPPgxXN6/0QaRyj5lavrbQFjW2bbILnFt 5uPFH5+4U0eiKUrEoG4olWBF7AAs3gt01G7UXt+ZVT2oc+Ioxre902/va0qyIw0IsYNo UIGftrchUxlwRzYYZJHYUWpIGU1U7SBya34EhiVUGPSzSiFz1ty7CaGlT4xX+OP0LO1n IaD4vgSOVs7KgbjfZLPAzA+zD1a1zkIbZerhqZV3N3PeTYV+Kn3ChU+3Xq/a7iBt3NhV JmHVCSOj+q0jzhSDGJ/URyBxo+1KPH+Tvb57p5JAqSc2pdjvVNv5DVVXQ85YmxcCu7Ss xnyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=gW18Fl3dm52InVgglE8m5dF8kxrMU+Nd8TZ0107EoLE=; b=W9QpKig3pV84P9YyO6p+EpwmswNLlTx458va9yreAD86YNvhoNpfPE4rLjzfmjrJmv V8BJkKxxCAds30q30OhYEKl+rkCy6zRqY3/Rcww5prpLvL9N2NqK/2jSSiZwrL15Z8tK 3LXkVfWjB36fJucJVF9faHnX+XpiP/jdDxsIqc0nyuLw5HJ9kiBKuUiZOo1cGgXVML8c Iwb6oPhA3AMcHxIjxde4EVbbGKdWAcYAvE/3gl+qqUXKhQcEZFoPOcwttOar9+i3uSbG 37pLQ5Co0VRqwDyb/2gADmypxt5m0YK/DZ4QL0/quZSynTA4HcOzTJV/zAqJ0twtSNoq gOVA== X-Gm-Message-State: AO0yUKW6qyUfgSgBl4hnQw1ZIBAxwJ7voYwPHsG3PifbWQQzbw7rMvX1 sqeucI+YatSvU8JuXNVkZ+gK/Td7CQ== X-Google-Smtp-Source: AK7set/j8U4qSwH8f7/DIupIA2FVdAxH3q0WmqpUrL8Ezg1GdgSGfKKPajMXy5B6e+X7QpIqBfmsb2AzLA== X-Received: from elver.muc.corp.google.com ([2a00:79e0:9c:201:34a3:b9c:4ef:ef85]) (user=elver job=sendgmr) by 2002:a17:907:206e:b0:8b1:2653:c5f3 with SMTP id qp14-20020a170907206e00b008b12653c5f3mr3693391ejb.4.1676591139804; Thu, 16 Feb 2023 15:45:39 -0800 (PST) Date: Fri, 17 Feb 2023 00:45:22 +0100 In-Reply-To: <20230216234522.3757369-1-elver@google.com> Mime-Version: 1.0 References: <20230216234522.3757369-1-elver@google.com> X-Mailer: git-send-email 2.39.2.637.g21b0678d19-goog Message-ID: <20230216234522.3757369-3-elver@google.com> Subject: [PATCH -tip v4 3/3] kasan: test: Fix test for new meminstrinsic instrumentation From: Marco Elver To: elver@google.com, Peter Zijlstra Cc: Ingo Molnar , Jakub Jelinek , linux-toolchains@vger.kernel.org, Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Andrew Morton , Nathan Chancellor , Nick Desaulniers , kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kbuild@vger.kernel.org, Linux Kernel Functional Testing X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 2F0EC4000D X-Stat-Signature: z6orpo178xp9mgjwdijyso8z6cmr6381 X-HE-Tag: 1676591140-763796 X-HE-Meta: U2FsdGVkX1+N0UDnbR/Qypq2xQykODR4lo1LWqgRtHiY0Q5pvWUE9LUcj8J3wQW4T1HD3EGUZw5v/R3oAzejfid1xoRnwvWNzCGepHhnZ/3K9ytdA1wSeE5VtWHJQhSHkLxIJQCkEU4iLfCAkPG9LLheftDOwEykuse2aJgum5Q7h6CIbeQBlEOf/RhbxOXxqqrPUpqj9fbLb/gIdqdpB5JCKzwmqMw3bOS0w+GX/34Ik4ok99ACnpD0384k19KYNJ2FVcuYJrsR4hkMHTq1LEm+wvAzYZuF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DmVsIKwuQffOyRfkCNyshxg6qdsqLJ3IPSL30w68ktLvFtbAID/m9uigkvwVEO5Z53ddZr6pUXsl0TubtuGwGyE2vU7rBztz1Yd0gTcoD2QSGh+Z4pB5LXdH0Uisfx8nMm2LOTahpxsYFb2fOgAuqLAYxl0xJf9we0f9ckaNtMFIpFW+BFUm9+m3C5UsbN37YJ1ZH+uivW+lPCBoIChnQ/mYgo8lK8MeSy7ezK9BPoQgjZk7DYc3aqZjMuG3gJEPHh7XiyE4FSxTjDIPoVKahjk7pe7g== 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 tests for memset/memmove have been failing since they haven't been instrumented in 69d4c0d32186. Fix the test to recognize when memintrinsics aren't instrumented, and skip test cases accordingly. We also need to conditionally pass -fno-builtin to the test, otherwise the instrumentation pass won't recognize memintrinsics and end up not instrumenting them either. Fixes: 69d4c0d32186 ("entry, kasan, x86: Disallow overriding mem*() functions") Reported-by: Linux Kernel Functional Testing Signed-off-by: Marco Elver Tested-by: Alexander Potapenko Reviewed-by: Andrey Konovalov --- v4: * New patch. --- mm/kasan/Makefile | 9 ++++++++- mm/kasan/kasan_test.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/mm/kasan/Makefile b/mm/kasan/Makefile index d4837bff3b60..7634dd2a6128 100644 --- a/mm/kasan/Makefile +++ b/mm/kasan/Makefile @@ -35,7 +35,14 @@ CFLAGS_shadow.o := $(CC_FLAGS_KASAN_RUNTIME) CFLAGS_hw_tags.o := $(CC_FLAGS_KASAN_RUNTIME) CFLAGS_sw_tags.o := $(CC_FLAGS_KASAN_RUNTIME) -CFLAGS_KASAN_TEST := $(CFLAGS_KASAN) -fno-builtin $(call cc-disable-warning, vla) +CFLAGS_KASAN_TEST := $(CFLAGS_KASAN) $(call cc-disable-warning, vla) +ifndef CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX +# If compiler instruments memintrinsics by prefixing them with __asan/__hwasan, +# we need to treat them normally (as builtins), otherwise the compiler won't +# recognize them as instrumentable. If it doesn't instrument them, we need to +# pass -fno-builtin, so the compiler doesn't inline them. +CFLAGS_KASAN_TEST += -fno-builtin +endif CFLAGS_kasan_test.o := $(CFLAGS_KASAN_TEST) CFLAGS_kasan_test_module.o := $(CFLAGS_KASAN_TEST) diff --git a/mm/kasan/kasan_test.c b/mm/kasan/kasan_test.c index 74cd80c12b25..627eaf1ee1db 100644 --- a/mm/kasan/kasan_test.c +++ b/mm/kasan/kasan_test.c @@ -165,6 +165,15 @@ static void kasan_test_exit(struct kunit *test) kunit_skip((test), "Test requires " #config "=n"); \ } while (0) +#define KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test) do { \ + if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) \ + break; /* No compiler instrumentation. */ \ + if (IS_ENABLED(CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX)) \ + break; /* Should always be instrumented! */ \ + if (IS_ENABLED(CONFIG_GENERIC_ENTRY)) \ + kunit_skip((test), "Test requires checked mem*()"); \ +} while (0) + static void kmalloc_oob_right(struct kunit *test) { char *ptr; @@ -454,6 +463,8 @@ static void kmalloc_oob_16(struct kunit *test) u64 words[2]; } *ptr1, *ptr2; + KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test); + /* This test is specifically crafted for the generic mode. */ KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC); @@ -476,6 +487,8 @@ static void kmalloc_uaf_16(struct kunit *test) u64 words[2]; } *ptr1, *ptr2; + KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test); + ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1); @@ -498,6 +511,8 @@ static void kmalloc_oob_memset_2(struct kunit *test) char *ptr; size_t size = 128 - KASAN_GRANULE_SIZE; + KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test); + ptr = kmalloc(size, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); @@ -511,6 +526,8 @@ static void kmalloc_oob_memset_4(struct kunit *test) char *ptr; size_t size = 128 - KASAN_GRANULE_SIZE; + KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test); + ptr = kmalloc(size, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); @@ -524,6 +541,8 @@ static void kmalloc_oob_memset_8(struct kunit *test) char *ptr; size_t size = 128 - KASAN_GRANULE_SIZE; + KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test); + ptr = kmalloc(size, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); @@ -537,6 +556,8 @@ static void kmalloc_oob_memset_16(struct kunit *test) char *ptr; size_t size = 128 - KASAN_GRANULE_SIZE; + KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test); + ptr = kmalloc(size, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); @@ -550,6 +571,8 @@ static void kmalloc_oob_in_memset(struct kunit *test) char *ptr; size_t size = 128 - KASAN_GRANULE_SIZE; + KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test); + ptr = kmalloc(size, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); @@ -566,6 +589,8 @@ static void kmalloc_memmove_negative_size(struct kunit *test) size_t size = 64; size_t invalid_size = -2; + KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test); + /* * Hardware tag-based mode doesn't check memmove for negative size. * As a result, this test introduces a side-effect memory corruption, @@ -590,6 +615,8 @@ static void kmalloc_memmove_invalid_size(struct kunit *test) size_t size = 64; size_t invalid_size = size; + KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test); + ptr = kmalloc(size, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); @@ -618,6 +645,8 @@ static void kmalloc_uaf_memset(struct kunit *test) char *ptr; size_t size = 33; + KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test); + /* * Only generic KASAN uses quarantine, which is required to avoid a * kernel memory corruption this test causes.