From patchwork Fri Feb 2 11:32:59 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: 13542780 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 D3893C47DB3 for ; Fri, 2 Feb 2024 11:33:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 525746B009D; Fri, 2 Feb 2024 06:33:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4AE786B009E; Fri, 2 Feb 2024 06:33:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 327E06B00A0; Fri, 2 Feb 2024 06:33:36 -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 201406B009D for ; Fri, 2 Feb 2024 06:33:36 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id F072640F03 for ; Fri, 2 Feb 2024 11:33:35 +0000 (UTC) X-FDA: 81746653590.04.3B7D29C Received: from postout2.mail.lrz.de (postout2.mail.lrz.de [129.187.255.138]) by imf05.hostedemail.com (Postfix) with ESMTP id 5442F100024 for ; Fri, 2 Feb 2024 11:33:33 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=tum.de header.s=tu-postout21 header.b="L H/t7DF"; spf=pass (imf05.hostedemail.com: domain of paul.heidekrueger@tum.de designates 129.187.255.138 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=1706873614; 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=CFqy2ux5XUG9htHw0BliWENPoh369TclIQ8YDIpQUic=; b=P9KaiUxoqgpFk/PJfHwYKSi0AOcqzC0LJDjRnGuhVssBFzYu+HHgi1VTbAtJVfJbMdvH3y 7/mh3b5N1t9phAUSQK4rN0Ylmqho2P8ofSJ9eu5/3pUvvPhi7IQHG+UqDj9FO4O88+sjTQ Ul0Asau8FjyT1v12LgSc4tZ9yKs8yH4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706873614; a=rsa-sha256; cv=none; b=x2j4CXFvAS6mgE9qEcIZiSHE3YjEBMXwh2JUgNMroN7B08CkhFYHz836scuko0KBZM10Zt KTJgTqJOCLrlQvyc81LHzopSYZcYHKowJHeIy8JjHEE8dJFkjMHHQfc6OEoSngdocaySGh YX68XpspcJdL5HZYZK9lgkoQ4zQYSC0= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=tum.de header.s=tu-postout21 header.b="L H/t7DF"; spf=pass (imf05.hostedemail.com: domain of paul.heidekrueger@tum.de designates 129.187.255.138 as permitted sender) smtp.mailfrom=paul.heidekrueger@tum.de; dmarc=pass (policy=none) header.from=tum.de Received: from lxmhs52.srv.lrz.de (localhost [127.0.0.1]) by postout2.mail.lrz.de (Postfix) with ESMTP id 4TRDDW1kRjzyYQ; Fri, 2 Feb 2024 12:33:31 +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= 1706873609; bh=xKnSOdlNYW4s3a0bxtmbV1BvcFyip1inL+1UdO0HLPQ=; b=L H/t7DFyFL199T3kRDqDacsJpvv5q+9Nd8sxepGlLgm8HLKMT2aPoirqcApySJpuu OpIeuNdMH5sbQTr3XqzoRu6T/oKVD63gYPZMDpKQAJRXpuAxfP7+Z9BAmUF1IQFP HRHeifkXtlBSTE5t5UnEdbU//s0+vZ5D3ZcIybdFPaK2pZZjYmB99d0nAVFISAe+ oeOhP/wkQtyGFHOHiTkmjhgb0g3GLhq5gFKnHV/VhLCZZyoQn0WeEIYmxXq8iKsO qY49jhR7PfXmi6TUE9N2w8c2f7b76ar3NOpfL0cbrlsfGdwQXV3prwRoymAJYa+4 +EezUplbVGYU62BcXvBCQ== 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 1ecYqzhVSBKZ; Fri, 2 Feb 2024 12:33:29 +0100 (CET) Received: from sienna.fritz.box (ppp-93-104-92-119.dynamic.mnet-online.de [93.104.92.119]) (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 4TRDDQ4z8xzyYX; Fri, 2 Feb 2024 12:33:26 +0100 (CET) From: =?utf-8?q?Paul_Heidekr=C3=BCger?= To: elver@google.com Cc: akpm@linux-foundation.org, andreyknvl@gmail.com, dvyukov@google.com, glider@google.com, kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, paul.heidekrueger@tum.de, ryabinin.a.a@gmail.com, vincenzo.frascino@arm.com Subject: [PATCH] kasan: add atomic tests Date: Fri, 2 Feb 2024 11:32:59 +0000 Message-Id: <20240202113259.3045705-1-paul.heidekrueger@tum.de> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 5442F100024 X-Rspam-User: X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GDu8GQ0Z/rixKZ13JLp2UnLY5kaC2dzJXqKNYa07bRDnUQtRKBGHK3hP1TdB0dS5aq8HUgv3PLF0xcJS80W6DzmgbDzuCP+tU+gs3VLo7GtcO0r6HhXmtFkIUyvcgjATfShPzLv3KF52+l6WknjfRf/gzLG/l1N1CNWfFbGTuu8suQDnHlMvyAYRivVXdZA9O3N69wdD3A9FD9t6qL8bRdn/s9ME74FHBdeft2sLU9SJGmbDufcnJbuLBhnfxj5ixubgX6cscRo9M7V7y4OT1R1a3Y+AbTLFglRJN+pMVIdEY7gk/9wZGzaiwRTA+s88UOqBaSKLOPPa240KrSaH/yYvCzA04dy2TlDh3Vl0gL9LE7zfSuUZmm6Ch+pQRw3vyWU03GFUSZWn9w== 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 Signed-off-by: Paul Heidekrüger Reviewed-by: Marco Elver Tested-by: Marco Elver Acked-by: Mark Rutland --- 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..4ef2280c322c 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 = (int *)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(int), 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),