From patchwork Mon Feb 12 08:33:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Paul_Heidekr=C3=BCger?= X-Patchwork-Id: 13552789 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 D0948C48297 for ; Mon, 12 Feb 2024 08:34:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 06E086B0071; Mon, 12 Feb 2024 03:34:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 01CC06B0074; Mon, 12 Feb 2024 03:34:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E27A98D0001; Mon, 12 Feb 2024 03:34:28 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id D3A646B0071 for ; Mon, 12 Feb 2024 03:34:28 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 6DD2F16032B for ; Mon, 12 Feb 2024 08:34:28 +0000 (UTC) X-FDA: 81782490216.24.53BE460 Received: from postout2.mail.lrz.de (postout2.mail.lrz.de [129.187.255.138]) by imf23.hostedemail.com (Postfix) with ESMTP id F383F14000A for ; Mon, 12 Feb 2024 08:34:24 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=tum.de header.s=tu-postout21 header.b="e P+lL5s"; dmarc=pass (policy=none) header.from=tum.de; spf=pass (imf23.hostedemail.com: domain of paul.heidekrueger@tum.de designates 129.187.255.138 as permitted sender) smtp.mailfrom=paul.heidekrueger@tum.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707726865; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=4Vrq6pH7f9iGB8LwYV2/mtF1jhtmkM8pYMdQmJLjgFk=; b=Hyfgc+KvEvaGjRmjquSZCrxnB6Z2jq9H44meWACDLLNjjPeSGVgNdqiY/NeAp+I27dAEth PPm61O/c/MqrU8rMb2qGIXVeOj9urEFQAulqdYn9KkyJd5EpgvElKj/xsArUW97mYxHufe ZEL7ShOeyrxvWxkAO1JT3ERue0VPM0s= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=tum.de header.s=tu-postout21 header.b="e P+lL5s"; dmarc=pass (policy=none) header.from=tum.de; spf=pass (imf23.hostedemail.com: domain of paul.heidekrueger@tum.de designates 129.187.255.138 as permitted sender) smtp.mailfrom=paul.heidekrueger@tum.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707726865; a=rsa-sha256; cv=none; b=lBT2wmw2HeXf+u2RPqi7nLGxYDYKfyV6j0uNcJRXW6Jw8Ga0Jao7kKJrEiVUqwXOced6Lw rOv7hkxTP0lOH3V5XVobmF3iL0OS6L7CGKVll6vFdoRfkW9q7NXilTTBAvtoyZ6BglO7V/ /YbonTrZJ5v4CiDDlEgROD3lvXTxUko= Received: from lxmhs52.srv.lrz.de (localhost [127.0.0.1]) by postout2.mail.lrz.de (Postfix) with ESMTP id 4TYHnB5NqfzyV2; Mon, 12 Feb 2024 09:34:22 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tum.de; h= content-transfer-encoding:content-type:content-type:mime-version :references:in-reply-to:x-mailer:message-id:date:date:subject :subject:from:from:received:received; s=tu-postout21; t= 1707726862; bh=63/KQ22XZagYlqAxmscXTKOnUwxan5bZ489uORJotHg=; b=e P+lL5soTdN5dYXbkIZyhFde2t9wTh/6jG9u93h937pr9CBBE2G8s9bggKfzNKjFo ZwAAuhyWOGFkoIgI9klA9Hg1Vx9K/GPHYn/G4bAEgKYr+q5P9G+jfQEyWd9Hg9+Y WvCewye1kjmUHiDF9yOo0n8isZ/Oj5zL+VAN2JqICiZrcjRv3NQdP++1UAmLCJkB n90OHiI62UOlIeueycB0kMJyovvJHtDKn4JGSv2H8QMEk64Iu5GokCic2Ed9gIOy dYbCzjH5Jh6eoxQ/Nxla+QUzTiHRNXjDrRnLWYuqQczpKdHEPupIBgyjvNF7pXMC 0lqDnIyP8NpCBbochH4OQ== X-Virus-Scanned: by amavisd-new at lrz.de in lxmhs52.srv.lrz.de Received: from postout2.mail.lrz.de ([127.0.0.1]) by lxmhs52.srv.lrz.de (lxmhs52.srv.lrz.de [127.0.0.1]) (amavisd-new, port 20024) with LMTP id MRHxCgA6u-EA; Mon, 12 Feb 2024 09:34:22 +0100 (CET) Received: from sienna.fritz.box (ppp-93-104-66-45.dynamic.mnet-online.de [93.104.66.45]) (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 postout2.mail.lrz.de (Postfix) with ESMTPSA id 4TYHn85xP7zyRX; Mon, 12 Feb 2024 09:34:20 +0100 (CET) From: =?utf-8?q?Paul_Heidekr=C3=BCger?= To: paul.heidekrueger@tum.de Cc: akpm@linux-foundation.org, andreyknvl@gmail.com, dvyukov@google.com, elver@google.com, glider@google.com, kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, mark.rutland@arm.com, ryabinin.a.a@gmail.com, vincenzo.frascino@arm.com Subject: [PATCH v3] kasan: add atomic tests Date: Mon, 12 Feb 2024 08:33:42 +0000 Message-Id: <20240212083342.3075850-1-paul.heidekrueger@tum.de> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240211091720.145235-1-paul.heidekrueger@tum.de> References: <20240211091720.145235-1-paul.heidekrueger@tum.de> MIME-Version: 1.0 X-Rspamd-Queue-Id: F383F14000A X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: gxwexxukmj6s19441zu4s8qkw9xekrix X-HE-Tag: 1707726864-49952 X-HE-Meta: U2FsdGVkX1/5T8dG0M99gm+2aSYCNhdmAaxdBUPCKPkBTQWZWc/WuztjQ30Jz4a1m4F0EIfgK0X5CttBDJPXw37kf51MMjYQnFSUqyWHXIMKkDc2gdPzZCEsg+14gC0NeOUKmAZhSzoji/eUT513NFH3y+P+bedd72lbxpLPVDcmmWLxMJjE3uFMEDE/YMchN1BQQBz2tR2aN+4bM3e5ozkmF3crLv980mZfbTL+09D8z8+mUCblIsf4+wlmFJOf+M1cELsac9iP0ZAiCkrN9jCBp05vFoYgBA5EsiBH4llg26+84A7bV7wJHPlQhDMF9mIqmw/Nu2nSBhmx+mZYP/6WQRIAP/tOnzae+UZtnNPKtsvyrUhjoQot5+4loeyS4GNsUvU8fH2YZUbtjiKhspbeibR71dVrm4UxM/MSzUUY5Yv0UKF/cOMufXBPqMkU+rXqcMs7DXEgfYrM0tHEyNBaBfyIpkba9NAfjiI9YvwvMHUrjL2fMMzRUre/iR5/2wovWPu5iDHpqoGyrqiBxtiK+E9kkNQu+iQoOp2Mra0h/Fm/Z3USabzyIEC5GWtQyYRXsidlq+qquEwEXiykDz+fcGs2SqdjlWtbrJp9cqpD/oZE6vRw3crWPL7wuCSmwW5BsTg85KyEnVlm/KVkwaSKXkMf8vSTqz6QvcJIkvU+21OQ+yk/L49haFzISergMM2yBJrpGmyKtmSiA4rofGsyMdmg11Rmo7WHzOrEWcVgw1C7MFDAn21EfZSmgW9zqIjRc/sGME+j8YwnQrjq6f5j1xU85WTOSMoh4q126Na8VMASijWASXcHZvqWkLlXtArf4aB25Y+Y6tUdAWxoy5LslcQss9nV1LHNAAA/E8owZmT0hen5TkNKcvbPG8zBSW7eOQw/w26eWDL0aW+eI4qHAAH/rFQkbfNnWtD9RseH44ZXayQz2TjYSMWqNt/tsfm/3EaFSDcuktY0QOu DOTfTEyP 6Q7Zl9qbGxXbQ1BNi3PsJC7ONIsRJeqh/B0lY4LNUH01U65RDZ1+hJn3fQYyMKwnnaLE8NbUIpRcm4y4ecOosfvAu7yXLxS1lqwz9JL/+ZFZ2H6AAfJ4gQ+AayX8Sv6FgMZEKgePNBah5RlCwCgHjYsu3d7yNaDgWdaNsjcLc39UhDNd0/XHyiPpXU1RLWYSPskqE+CROFn6dG6GpvoYaW4MzVkUlMW3f5EQyeIMRRzFUcNFdO1Xm8toE5o43ytbP8JSj3SokEeUvFx9hMw3L/7RadOgBYsXoGmNBiMAgxaHmcyFzC2Wqhd7xkSShkxkCDCDxtYanz8l7SAYMQl4ryo5fO/RPhoB1PSv3RVD0CRry2D7j/3oSlLwGKqyhnEWs2ArvhbfBIz43CTb8Gs/egwvy6ydNcol91G0ZSXjP3XyTemZpNGhjjE3ZDplmTEAyPgRHwJVrzzhkrFy6NaMQ2AK06A== 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: Test that KASan can detect some unsafe atomic accesses. As discussed in the linked thread below, these tests attempt to cover the most common uses of atomics and, therefore, aren't exhaustive. CC: Marco Elver CC: Andrey Konovalov Link: https://lore.kernel.org/all/20240131210041.686657-1-paul.heidekrueger@tum.de/T/#u Closes: https://bugzilla.kernel.org/show_bug.cgi?id=214055 Reviewed-by: Marco Elver Tested-by: Marco Elver Acked-by: Mark Rutland Reviewed-by: Andrey Konovalov Signed-off-by: Paul Heidekrüger --- Changes PATCH v2 -> PATCH v3: * Fix the wrong variable being used when checking a2 after allocation * Add Andrey's reviewed-by tag Changes PATCH v1 -> PATCH v2: * Make explicit cast implicit as per Mark's feedback * Increase the size of the "a2" allocation as per Andrey's feedback * Add tags Changes PATCH RFC v2 -> PATCH v1: * Remove casts to void* * Remove i_safe variable * Add atomic_long_* test cases * Carry over comment from kasan_bitops_tags() Changes PATCH RFC v1 -> PATCH RFC v2: * Adjust size of allocations to make kasan_atomics() work with all KASan modes * Remove comments and move tests closer to the bitops tests * For functions taking two addresses as an input, test each address in a separate function call. * Rename variables for clarity * Add tests for READ_ONCE(), WRITE_ONCE(), smp_load_acquire() and smp_store_release() mm/kasan/kasan_test.c | 79 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/mm/kasan/kasan_test.c b/mm/kasan/kasan_test.c index 8281eb42464b..7f0f87a2c3c4 100644 --- a/mm/kasan/kasan_test.c +++ b/mm/kasan/kasan_test.c @@ -1150,6 +1150,84 @@ static void kasan_bitops_tags(struct kunit *test) kfree(bits); } +static void kasan_atomics_helper(struct kunit *test, void *unsafe, void *safe) +{ + int *i_unsafe = unsafe; + + KUNIT_EXPECT_KASAN_FAIL(test, READ_ONCE(*i_unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, WRITE_ONCE(*i_unsafe, 42)); + KUNIT_EXPECT_KASAN_FAIL(test, smp_load_acquire(i_unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, smp_store_release(i_unsafe, 42)); + + KUNIT_EXPECT_KASAN_FAIL(test, atomic_read(unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_set(unsafe, 42)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_add(42, unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_sub(42, unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_inc(unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_dec(unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_and(42, unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_andnot(42, unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_or(42, unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_xor(42, unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_xchg(unsafe, 42)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_cmpxchg(unsafe, 21, 42)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_try_cmpxchg(unsafe, safe, 42)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_try_cmpxchg(safe, unsafe, 42)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_sub_and_test(42, unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_dec_and_test(unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_inc_and_test(unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_add_negative(42, unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_add_unless(unsafe, 21, 42)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_inc_not_zero(unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_inc_unless_negative(unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_dec_unless_positive(unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_dec_if_positive(unsafe)); + + KUNIT_EXPECT_KASAN_FAIL(test, atomic_long_read(unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_long_set(unsafe, 42)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_long_add(42, unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_long_sub(42, unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_long_inc(unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_long_dec(unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_long_and(42, unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_long_andnot(42, unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_long_or(42, unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_long_xor(42, unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_long_xchg(unsafe, 42)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_long_cmpxchg(unsafe, 21, 42)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_long_try_cmpxchg(unsafe, safe, 42)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_long_try_cmpxchg(safe, unsafe, 42)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_long_sub_and_test(42, unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_long_dec_and_test(unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_long_inc_and_test(unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_long_add_negative(42, unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_long_add_unless(unsafe, 21, 42)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_long_inc_not_zero(unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_long_inc_unless_negative(unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_long_dec_unless_positive(unsafe)); + KUNIT_EXPECT_KASAN_FAIL(test, atomic_long_dec_if_positive(unsafe)); +} + +static void kasan_atomics(struct kunit *test) +{ + void *a1, *a2; + + /* + * Just as with kasan_bitops_tags(), we allocate 48 bytes of memory such + * that the following 16 bytes will make up the redzone. + */ + a1 = kzalloc(48, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, a1); + a2 = kzalloc(sizeof(atomic_long_t), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, a2); + + /* Use atomics to access the redzone. */ + kasan_atomics_helper(test, a1 + 48, a2); + + kfree(a1); + kfree(a2); +} + static void kmalloc_double_kzfree(struct kunit *test) { char *ptr; @@ -1553,6 +1631,7 @@ static struct kunit_case kasan_kunit_test_cases[] = { KUNIT_CASE(kasan_strings), KUNIT_CASE(kasan_bitops_generic), KUNIT_CASE(kasan_bitops_tags), + KUNIT_CASE(kasan_atomics), KUNIT_CASE(kmalloc_double_kzfree), KUNIT_CASE(rcu_uaf), KUNIT_CASE(workqueue_uaf),