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: 178797 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp8681926dys; Thu, 14 Dec 2023 08:44:41 -0800 (PST) X-Google-Smtp-Source: AGHT+IFGz+Wj1QpS8DDGQGY1rB0WCI5U1fx6b+mDxElKgmrzRn44JtGm4kluaiR2TVJK/GgT3T7J X-Received: by 2002:a17:902:f54f:b0:1d3:7a10:222a with SMTP id h15-20020a170902f54f00b001d37a10222amr447463plf.106.1702572281221; Thu, 14 Dec 2023 08:44:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702572281; cv=none; d=google.com; s=arc-20160816; b=su1rI/+QYMC0UT6ZXJgdCIOUw08nXraSXm9X+K4kna2Vlwc+2Ru4XUcG5jx4LWMswg OnWdSfhga0TCcLLOF5sOMQGrtqao91b2lognPFT05rQikbIkcEEcYRkbiIeUpieGu39v Qe/rsZq7dAyjlA7xHaVYrE0iXKiffmlWMoejn7tk6qLFadGS1l3D94MPFHFcU0oVLL0Z 005XsS6+l3aCrW/r3aemwF4y4ALIyhD9kP/8LiefnXHbDPTmZe24GQkbBtfI1qNdLgRl g+kB3tIf4qvT08/PBfKkuJOvik1aUG8APFwYxgh6nMB3SfTjgCCcqosb3oo3QszSC6Hj nblQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=w7n7C7cUVujGrFjhyS4KsTPPgLzIdfsh/gqlYQrUTfI=; fh=t4pyiPcCBIC9Y+5xkCTP+ifpPVuOKc4r2DwOkdw1hDY=; b=KiYiHIBPutCJzHxd8imFWvQWEesTTKgbrtaZsY04SNx7+s/uGfrkW9j0w3hGlR+/8v 2TmVUtufemSU4enQC0GzqOAIum3EJSP3dNDaMq4aY/Xh+8cp1QACDBWos2fDMRnWG6bs YozrNb7cPvvDq9+xaPzZZsKHkwkrtRaEdLFN1gUvVuT5miyqMuWL6yXL44ZvfG8fNGBw nXBzoYnDG3L8ieU+2X/HjDmNQWmX4fIZLrPDIYmNmRGJI7jQq7TKvjxZKvQRCbkM4zja q5eozHOj5fJe3B2XpPF1frDca7GXKWUjOIVq+t2ndtZPvJxfWAt2TpwaphhBcqJNRkFK H0vw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=f6pGycap; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id j5-20020a170902c3c500b001d36d94403dsi1010765plj.108.2023.12.14.08.44.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 08:44:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=f6pGycap; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id CEE3285A042A; Thu, 14 Dec 2023 08:44:37 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229543AbjLNQo3 (ORCPT + 99 others); Thu, 14 Dec 2023 11:44:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50770 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229487AbjLNQo1 (ORCPT ); Thu, 14 Dec 2023 11:44:27 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C817E120 for ; Thu, 14 Dec 2023 08:44:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702572273; 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=f6pGycap9gnekhMLWhtWin6FhGYFQkIUvuw1tI9mmAnD5KPgF4rT0gaQE6FVFLbcy2Fp8g HfMwGUdIXAVDgjfXyTpI/+baVFvKZDu3SkhtR0fKWduy7256GSyl3pcpv3j/qPskP8ev7+ 6ehZGGw+eOJeBAoR4GbRf25azGK8roM= 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-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Thu, 14 Dec 2023 08:44:38 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785276432455924456 X-GMAIL-MSGID: 1785276432455924456 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); }