From patchwork Sun Feb 11 09:17:20 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: 13552394 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 CEACFC4829B for ; Sun, 11 Feb 2024 09:18:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 223C36B0074; Sun, 11 Feb 2024 04:18:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1D3EA6B0075; Sun, 11 Feb 2024 04:18:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 09DEE6B0078; Sun, 11 Feb 2024 04:18:04 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id EC6626B0074 for ; Sun, 11 Feb 2024 04:18:03 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 2A400140757 for ; Sun, 11 Feb 2024 09:18:03 +0000 (UTC) X-FDA: 81778971246.16.A0B3164 Received: from postout1.mail.lrz.de (postout1.mail.lrz.de [129.187.255.137]) by imf01.hostedemail.com (Postfix) with ESMTP id F3BB040010 for ; Sun, 11 Feb 2024 09:18:00 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=tum.de header.s=tu-postout21 header.b="f XFBmet"; spf=pass (imf01.hostedemail.com: domain of paul.heidekrueger@tum.de designates 129.187.255.137 as permitted sender) smtp.mailfrom=paul.heidekrueger@tum.de; dmarc=pass (policy=none) header.from=tum.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707643081; 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=nyUqcldidtnXWwbTceXwthhkZZm9TNzWOm9++Z2b+gM=; b=GXpyYOW9ZVCGg9aMNB1U2mrVa8haTXar0/hMjK3SnlK9Ihzmsazo02lSPnacq5ae+mIQYc vr7nWAiQGJSRfHi8ZbRkgNHTBbxxSgUIwCxiKMxkh0gpSMdyHj04Wc3uD96Ma9WHEcPDmR 84anmfFCPRnQ7n4jgjh2XAtBiLAKSqY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707643081; a=rsa-sha256; cv=none; b=CX33yt5q62SDjfeSeXkjBWQz59Jzv8u73DaxZUJcRWNKK9ZkxMhyA2lFOBsvONWxdgAek9 NLBTxhtSid6AZWMs9p5vqEIkUrJ61yi2cxp+RUgLbOzOLC9tpzagCnFfonv5YJcZYNxUPo i4U9ijs0dS3kDmKAv3Mi+Y9fSc+9J5E= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=tum.de header.s=tu-postout21 header.b="f XFBmet"; spf=pass (imf01.hostedemail.com: domain of paul.heidekrueger@tum.de designates 129.187.255.137 as permitted sender) smtp.mailfrom=paul.heidekrueger@tum.de; dmarc=pass (policy=none) header.from=tum.de Received: from lxmhs51.srv.lrz.de (localhost [127.0.0.1]) by postout1.mail.lrz.de (Postfix) with ESMTP id 4TXhny4cG0zySP; Sun, 11 Feb 2024 10:17:58 +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= 1707643077; bh=aEaq/TzcHg4rMQdODyuar0MImODB9bbF3bqWLrgPZ6Y=; b=f XFBmetfZhZDsxhHssUdJ4P4/rpNM0n5RT2MIBdcyB5bpqrqppaYsNOuzr/gGCkgS nTrGDgH/VL5I5MGiBKzSMHJSGI/5N5q+hekl2twLc6nhB0j2rre590B2OSQ2AAZ0 7AlxNkL9QA8DOGEbfNBAnDvG6h5yVLi5e3L8uAE7pVHUmO2xVKQaXnby9ij5Tivc VexJVFItZhwdYsfMSJsBC4SIS7dVMdhs6fP6IShahynj4Kg0g8d44t2LuwWb6FHH 1JPgLzXzkxaFnmrvSc06tf38Bm8v70CfZdCS+BxAgVwDB2SjVO4nqMx8RAv4E/Ij Rj8RHFknFAcndq+lJSD3w== X-Virus-Scanned: by amavisd-new at lrz.de in lxmhs51.srv.lrz.de Received: from postout1.mail.lrz.de ([127.0.0.1]) by lxmhs51.srv.lrz.de (lxmhs51.srv.lrz.de [127.0.0.1]) (amavisd-new, port 20024) with LMTP id voYhHrpRVFxn; Sun, 11 Feb 2024 10:17:57 +0100 (CET) Received: from sienna.fritz.box (ppp-93-104-92-100.dynamic.mnet-online.de [93.104.92.100]) (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 postout1.mail.lrz.de (Postfix) with ESMTPSA id 4TXhnx1Vk2zySF; Sun, 11 Feb 2024 10:17:57 +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, ryabinin.a.a@gmail.com, vincenzo.frascino@arm.com, Mark Rutland Subject: [PATCH v2] kasan: add atomic tests Date: Sun, 11 Feb 2024 09:17:20 +0000 Message-Id: <20240211091720.145235-1-paul.heidekrueger@tum.de> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240202113259.3045705-1-paul.heidekrueger@tum.de> References: <20240202113259.3045705-1-paul.heidekrueger@tum.de> MIME-Version: 1.0 X-Rspamd-Queue-Id: F3BB040010 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: qjtj1prbqump8yq58afnmis5i9h1fp67 X-HE-Tag: 1707643080-213314 X-HE-Meta: U2FsdGVkX18GSeXZtVVrSCexJOF5vDpl3cK0y6gRSquhGC02IhXS4bMwCylsuJl6Xgt+4atjqm+T5ERrflchqYbbS1OGXbwgJzL7SOSGS35JBfcMA0Xt6d+xBmFGh1Yu9n0yUNNBePky3q4NfkPxKnzyTwO52MgGppBHcpdWi9+NZW/Pui4JQHVWxldtSvB3tDzj27nAFenRsq+FPc7A1bTj6fBuq8xxt4LU8jxBC+QNRCU2TVFljUlPlT3DHmGqEDE+lhPkTovXxrHs5mzB99zLk7+rvHkJ725rkoBVNJ4nkjxcdZsRnNhR6Q5Xwa5C0tT+R/qArhYDUIaKzxxqhj+ZF2WZMudSTW3BbsvCzDG4tqw2jl+VIyY+IMH8ybtE6fzleEjr6hgTkhdNyGfNX/6Z6zXfhLdMBdf0/adsxObJQanVPnmQ2QojCV+0RmHsq79Jmyt83Jn+fajwPotsL87UKK4LdyvTWo5SKILnUEeqRTXw1IhjVMQAQePvjzkidSBuEp8V27Yb20tA5NQYV/FKc+S0JBZXxRsHfaHeIenj6uiiUI3H99VUJ18XsC9Lzp1Lvrtinv/gVRoMfSb5UGyBPoS8zcPSImrE+l1bj9fYShIPXo3vCOJMUvvWMnxUxWsvRzHasnvfO+bs6CNbwFFqkpWVybRlG0lbYBu3VnbMW5A4cyZpwLtMd9iFlR0RA1FE6WJpyvGlebP5xTrOGbvu0RX+kFaV1ayzkwDt8P/+dzqp0HolD6octbHfvySiMehGAscIyTYpEQ15pgiFs+tCA8Gh1Jwg1nVfAu7lfD8R53Mwkjhyy1aJpgI+ogG9hxJc2ubtyiHtnvEAkXIXrW6izN8YfA8QhZVqsNEF0QjmA8snMvRufRErnmNM8s1N+eVHgMdyMWVZOyIEzdD+7oSzG50SMmWVuTuDF7CrQ40o97sXF8wrgZIYYhq0zenITn5sLDd7UNOD/chS+t9 WgPGetXo dnwuAQSSQxxvimOBtlvzciI/CeoCFc9rvsx6aJllJ5mQxWrzxSBv0ug5mJR65JU7MHeksJDZP//8P8LVIPIDQ7cjMAip+VT6/i/4Xq4AYqngFZan1aEgzYic45Q6/yxHdhovQne2EYuVYrkk718n+EVUh30QWkofB4HGC9oukBv15HT8KfVYAAMX6FAVXZY2u8Byp/kt5Wr1690+kz2KPZVKRU3R6nQYuc3NzixdshmJ9S9+yOKhWlcbDLWTPL62Ts55e3kABOaO+ULlfYFf8GjtwJFY9M0Sg4ZXF48VGTjtdw91ttWy9ZSwyuGC+a2aWwqkoNfxSIj7zG2rAs9f5CTFEQxh/qvpsHwPt1WYrpyO7wZ2gi3HgrhKUzz0HUEY2/jl8QRfHP7ogyITPXp8kvbRMhVo/Q/jC3DFpxuP5MyJcZTMAghWNDAR/LQ== 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 Signed-off-by: Paul Heidekrüger Reviewed-by: Andrey Konovalov --- 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..7bf09699b145 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, a1); + + /* 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),