From patchwork Thu Dec 14 16:44:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nico Pache X-Patchwork-Id: 13493251 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 9F97DC4332F for ; Thu, 14 Dec 2023 16:44:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0C7AA6B06C7; Thu, 14 Dec 2023 11:44:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0784A6B06C8; Thu, 14 Dec 2023 11:44:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E82356B06C9; Thu, 14 Dec 2023 11:44:34 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id D67CE6B06C7 for ; Thu, 14 Dec 2023 11:44:34 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id A63AE120303 for ; Thu, 14 Dec 2023 16:44:34 +0000 (UTC) X-FDA: 81565997268.13.CAF1CE6 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf17.hostedemail.com (Postfix) with ESMTP id D293340014 for ; Thu, 14 Dec 2023 16:44:32 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=NecJITaX; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf17.hostedemail.com: domain of npache@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=npache@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702572273; 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=w7n7C7cUVujGrFjhyS4KsTPPgLzIdfsh/gqlYQrUTfI=; b=G+ULvi0BjBAP4hlmHZ3VDGf8ktnbr3lOhG2rYqJX7schiHP1u5LTkoYZmbqrk9+1Eh2hvu dkr3cYnY3nF9DksYExiHk2c31MqNTZqiNL03zaJ/ot4Wt/4vHpxsXVjL4I0NIOQ/7T4PY2 mWVG/gqs99eZzmZHXTvQgHokYPMmm9M= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=NecJITaX; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf17.hostedemail.com: domain of npache@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=npache@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702572273; a=rsa-sha256; cv=none; b=ABI5pwHyD6kBlqjaarOr6eNdqFCK0mJgzqkRPIIPfCXDQ7dnZUfx9mHPQEP9D6x04VuMuw lnahTagAf96jVtPzG4ZpdP/ADPnkZ1yiWUwsIzF9vJ9tsr0NKd8D5rAIREAloBWSBQIfpC Rvl3jEsn6YcO+kiFN0h1sRNYbG9FjSw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702572272; 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=w7n7C7cUVujGrFjhyS4KsTPPgLzIdfsh/gqlYQrUTfI=; b=NecJITaXY4jdseAa11TUTVaj/A+7AYWAi+vwZdqU29M826vMrUGOIshtvJAuxl5Ur1mN6Y bKGA1e+DiyVA+rGF4VVdB0wwcRSZC0LmIcJdgbyK+ZVK1V/dJO6w5xAoGvFunWtRKY7ucM ZOTXxDuwEjrw6prKYUxdRijDXiNyNpY= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-212-7MArNrfxMKKcVf6uYur6ow-1; Thu, 14 Dec 2023 11:44:27 -0500 X-MC-Unique: 7MArNrfxMKKcVf6uYur6ow-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (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 7D7D91C0514E; Thu, 14 Dec 2023 16:44:26 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.22.33.120]) by smtp.corp.redhat.com (Postfix) with ESMTP id 28381492BC6; Thu, 14 Dec 2023 16:44:25 +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 v2] kunit: kasan_test: disable fortify string checker on kmalloc_oob_memset Date: Thu, 14 Dec 2023 09:44:23 -0700 Message-ID: <20231214164423.6202-1-npache@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Rspam-User: X-Stat-Signature: o4g1uhffpaf4p8cxpau1u9m5783oq3tp X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: D293340014 X-HE-Tag: 1702572272-230867 X-HE-Meta: U2FsdGVkX1+w0qtffcQzZJivN6j9gijY+GBIn/H/qIFdllM0uoE8FaYnIeP6jiCWqXVnufP8t/IGMu8R3Rpv8mg0lTwi5OEMjx9IGFV0ewlPe4o1GFa9B5OSc3QyC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FPHlHS0IzwYxVPtLWNi5CVLyJpamxdSvTM4oVBOGJyTqcIQN+YXXliUPE1EtPCe8BTWqcMOZ5A5m4iwO0nPD6bZ4oYSKLzzLIZ5SfZedL5YkmOITFRcJcMdrCAu2DEBujylXIQfU= 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 memset_size 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..34515a106ca5 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 memset_size = 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(memset_size); + KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 1, 0, memset_size)); 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 memset_size = 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(memset_size); + KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 3, 0, memset_size)); 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 memset_size = 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(memset_size); + KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 7, 0, memset_size)); 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 memset_size = 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(memset_size); + KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 15, 0, memset_size)); kfree(ptr); }