From patchwork Tue Dec 12 23:26:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nico Pache X-Patchwork-Id: 13490132 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 E46C3C4332F for ; Tue, 12 Dec 2023 23:27:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 854A46B034C; Tue, 12 Dec 2023 18:27:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 804F06B034D; Tue, 12 Dec 2023 18:27:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6F40D6B034E; Tue, 12 Dec 2023 18:27:09 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 5F6DD6B034C for ; Tue, 12 Dec 2023 18:27:09 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 3D4431A0A81 for ; Tue, 12 Dec 2023 23:27:09 +0000 (UTC) X-FDA: 81559754178.03.F5703A1 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf11.hostedemail.com (Postfix) with ESMTP id 110184001D for ; Tue, 12 Dec 2023 23:27:05 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=GoGurWP1; spf=pass (imf11.hostedemail.com: domain of npache@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=npache@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702423627; a=rsa-sha256; cv=none; b=ZrbT6flaN8Px1T1ReX2Z/Pm1DxblF4s264MJlgQn6r3NDUB5rR8pOe1fkvKZWFRWm9RRr9 UapjCjA6xl+b0fPS4dKDGtIcdNFPLGDUGZ1rFD0IVrXw3JOAuu53EuifYsEeFQeIsl4OFQ kFq83Uazc2EbQm0L4158AkL2xXNd19w= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=GoGurWP1; spf=pass (imf11.hostedemail.com: domain of npache@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=npache@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702423627; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=ZwFQ3GQCbPFAax8NUohZK89OPMuLW9MEl5mvS51Lzts=; b=AYjIRk3TsmAblNk2hswAPcfA8j6rIJobEVZk2TThe/ooA1R6cNIIENMQvP9Yuwj7EitPoO BDC3YdFqlYsSEGZfT7mTxGACo00MqHgxrfgT2Xvv8/EPcM82JaqT5JdDNFxBjsKxvxH9Uc A+Jaewp38k46iGFCVmQyHciWj7e4tgU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702423625; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=ZwFQ3GQCbPFAax8NUohZK89OPMuLW9MEl5mvS51Lzts=; b=GoGurWP1t/5Ph41a5tLFYZZcijMjq6LnHoSeOZeKXGsgJ4sVA+RwCcKfSE10uWMnzuHgby AOKl2uxF2heVxUxw/ZIOX11BRy0DE86pJ9Wnw4mKnBshiHS2eInDD3/LVh3s5ZdCy2LUvz VMmGqCJ8lod4oNdz207xjrr4MlbBFQ8= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-45-6YG-K_zxMJKQP8zTqBW2aQ-1; Tue, 12 Dec 2023 18:27:03 -0500 X-MC-Unique: 6YG-K_zxMJKQP8zTqBW2aQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4552A80BEC1; Tue, 12 Dec 2023 23:27:02 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.22.34.149]) by smtp.corp.redhat.com (Postfix) with ESMTP id 800551121306; Tue, 12 Dec 2023 23:27:01 +0000 (UTC) From: Nico Pache To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, kasan-dev@googlegroups.com Cc: akpm@linux-foundation.org, vincenzo.frascino@arm.com, dvyukov@google.com, andreyknvl@gmail.com, glider@google.com, ryabinin.a.a@gmail.com Subject: [PATCH] kunit: kasan_test: disable fortify string checker on kmalloc_oob_memset Date: Tue, 12 Dec 2023 16:26:59 -0700 Message-ID: <20231212232659.18839-1-npache@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 110184001D X-Stat-Signature: wajpn37ziee8yo3itjkm6eop43yg5d5h X-Rspam-User: X-HE-Tag: 1702423625-699760 X-HE-Meta: U2FsdGVkX185UBVvWksHT4iV8D5CWbpz5I0EzGIROUHRRN0m/d5y29VlF6+auXJoX2AJXXMqg8zr1uwQll+lYmC+wjeBioPtCM+WTlVkrubLXgBJB0F/FDaltTPQUGXcaswhQY6JgdRZD6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YoIQKjn4APegy5XNPBGEu3OjhtdhQGab0w+0J8QclccjAXLa0m7/HZI4WNBGN6tLq22Vo59U/93+oxEE7L0MEk/N06Ji/68bZgMr6uHZnbuQZf6WmL4A8Q= 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: List-Subscribe: List-Unsubscribe: similar to commit 09c6304e38e4 ("kasan: test: fix compatibility with FORTIFY_SOURCE") the kernel is panicing in kmalloc_oob_memset_*. This is due to the `ptr` not being hidden from the optimizer which would disable the runtime fortify string checker. kernel BUG at lib/string_helpers.c:1048! Call Trace: [<00000000272502e2>] fortify_panic+0x2a/0x30 ([<00000000272502de>] fortify_panic+0x26/0x30) [<001bffff817045c4>] kmalloc_oob_memset_2+0x22c/0x230 [kasan_test] Hide the `ptr` variable from the optimizer to fix the kernel panic. Also define a size2 variable and hide that as well. This cleans up the code and follows the same convention as other tests. Signed-off-by: Nico Pache Reviewed-by: Andrey Konovalov --- mm/kasan/kasan_test.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/mm/kasan/kasan_test.c b/mm/kasan/kasan_test.c index 8281eb42464b..5aeba810ba70 100644 --- a/mm/kasan/kasan_test.c +++ b/mm/kasan/kasan_test.c @@ -493,14 +493,17 @@ static void kmalloc_oob_memset_2(struct kunit *test) { char *ptr; size_t size = 128 - KASAN_GRANULE_SIZE; + size_t size2 = 2; KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test); ptr = kmalloc(size, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); + OPTIMIZER_HIDE_VAR(ptr); OPTIMIZER_HIDE_VAR(size); - KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 1, 0, 2)); + OPTIMIZER_HIDE_VAR(size2); + KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 1, 0, size2)); kfree(ptr); } @@ -508,14 +511,17 @@ static void kmalloc_oob_memset_4(struct kunit *test) { char *ptr; size_t size = 128 - KASAN_GRANULE_SIZE; + size_t size2 = 4; KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test); ptr = kmalloc(size, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); + OPTIMIZER_HIDE_VAR(ptr); OPTIMIZER_HIDE_VAR(size); - KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 3, 0, 4)); + OPTIMIZER_HIDE_VAR(size2); + KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 3, 0, size2)); kfree(ptr); } @@ -523,14 +529,17 @@ static void kmalloc_oob_memset_8(struct kunit *test) { char *ptr; size_t size = 128 - KASAN_GRANULE_SIZE; + size_t size2 = 8; KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test); ptr = kmalloc(size, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); + OPTIMIZER_HIDE_VAR(ptr); OPTIMIZER_HIDE_VAR(size); - KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 7, 0, 8)); + OPTIMIZER_HIDE_VAR(size2); + KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 7, 0, size2)); kfree(ptr); } @@ -538,14 +547,17 @@ static void kmalloc_oob_memset_16(struct kunit *test) { char *ptr; size_t size = 128 - KASAN_GRANULE_SIZE; + size_t size2 = 16; KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test); ptr = kmalloc(size, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); + OPTIMIZER_HIDE_VAR(ptr); OPTIMIZER_HIDE_VAR(size); - KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 15, 0, 16)); + OPTIMIZER_HIDE_VAR(size2); + KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 15, 0, size2)); kfree(ptr); }