Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp2173251rdb; Sun, 11 Feb 2024 15:24:26 -0800 (PST) X-Google-Smtp-Source: AGHT+IHZVizGZD5AwbtqWW1NJl7/VhwLGElc/U7L4JmrsmooeodlHl+xWAWBtqsUGxSGBwcnp6NZ X-Received: by 2002:a17:906:c452:b0:a36:fa8e:8796 with SMTP id ck18-20020a170906c45200b00a36fa8e8796mr4531722ejb.21.1707693866521; Sun, 11 Feb 2024 15:24:26 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707693866; cv=pass; d=google.com; s=arc-20160816; b=fIt78j9sBwg8tYBlZqXJ9amM8jZR8gNi9asjN5ElCe3szfI09VK4+gBCHoocSXqyV0 Bu86iy290l/zOWt7bRrIr6WNYaM0EiUJz9ce8Z9xNaTRRe4Im43+hcdw6CBRZHG0tMr3 juIz156QNbzWkjSwPXftc0YW36RvE3JR/jPJ627pnYPOJ2Th3YzALzgOQJ+EGSnoG/OU UkqfLq9gDdCIvBxVuDdsiQWXRvQad9LMAx56qGViafloXgFoWq5tWwrHf5Cx1vMNDuzz 7cBYOL1kVcF94eIxGSKY3TNosxI6VqQnmkBgrVm0HKqoYehJrNQtSslL+wtbGkFFsUAh lqRQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=8BelCqyqaTU+jGU/MDi1SS3yrhgj8pP4yG8n+9l1MNk=; fh=OMLFENTAqg7F5r1YjPr+7wJ7Mi56AGm+WqfNuhxcnzM=; b=unpAxF6jziEYOEkyX3enBRFWAyuaLhJ6SOTY6jjwyMDlEn9G1oe4sqXo6I41Wiefcb i+6Aod0D+OEopQIz1GJWlcvQRUGLCLNJ4Kjc35mZ298jTsr5tCkeig8fkOoDSF2Zyg5+ EtpqnGT5aGKPfiir2Fo3ksJzaP2O7MN6ClmQSGCGAwyshXK+mu2xBPAcvkfIEphjgj4E iFZvariFjgq+wsmqbzNhEgLXAscimWw0rsWTgVhRvQF4EANBW29QmCg1VqUw/jfSboeA OmOQLCMe/aZrbLmDm6iJv5QyFJL3awhPb+MtPf0si1ixJjk948x/VKrezvEs5W1kTrpx 9hpw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=kmbHNase; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-61018-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-61018-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com X-Forwarded-Encrypted: i=2; AJvYcCUF2+Y2A1eMPFZ5J82XVod2hag12dVlFGxPsQDFTn8+yIfbRML82MCeJtgTaju4KP25o3NCNu3s7ujtyd5dmw9wrz6YoBxuIIK/fwkAHw== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id i12-20020a17090671cc00b00a3bd842466asi3150916ejk.241.2024.02.11.15.24.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Feb 2024 15:24:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-61018-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=kmbHNase; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-61018-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-61018-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 0ACC21F25E64 for ; Sun, 11 Feb 2024 23:24:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E846217BCA; Sun, 11 Feb 2024 23:17:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="kmbHNase" Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ACCA71799E for ; Sun, 11 Feb 2024 23:17:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707693433; cv=none; b=YegLMCucLo5xqszb9EfreNQzDbNw0KY3jgUXcpU577sE4yKRtUDh6u/DJpS40icFLGbxKR2d2ThSaU1V+gsJoS/8M7NJ0kkLoCp1QVLpdlzMJGSkLBaVbMvU79OHmm4Sow6sIX+llgUC0+6VhY5g9r8V85T4Qv5QDxCwLCIkAIM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707693433; c=relaxed/simple; bh=5v1qibF1z7pef9W6fHNf4GtAJhT/AJKPPL+wYwD4sIM=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=k6rUOufi09YQOUIW2/JJNTHC3rFKx75YzHZX80Ci4Uwt5gVdIBRas7GXMvRjx16IRaYvBJYcPSy0stEZyMdIIMIHN+GG1rcvBvEdiFRhTNSsvUqzturdB+vAcDtpALd5DnoU/XgPjqJy6KxL8APk9qHyiq6oGAjeXUOQOHx3/Ag= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=kmbHNase; arc=none smtp.client-ip=209.85.221.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-33aeb088324so1736714f8f.2 for ; Sun, 11 Feb 2024 15:17:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707693430; x=1708298230; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=8BelCqyqaTU+jGU/MDi1SS3yrhgj8pP4yG8n+9l1MNk=; b=kmbHNasezYEHT/YKpfImt4tpKnwYUHIqLsE5Q/56dOSizKWxhtql8c4FYPoxUQU7Rb 8FE3jIqoOuzll0mduHO1ZMuWHbpxrX4S98x3c5W25suqJbv7aIUv22dAWur8oRP0b45U jKXaC9FSWlsyGOHmHUmS7StGP1BlBDGB/DJ5HhVa0XtxSYYCF3u4JLpKpZmsYX1Ui7w2 LdT2Bl2L2qJYS190rheuZPKBBzLytWKRz02BjxgNUDOj9c6GhuMbaHueuvC9hd0BNDqr 0S75QuMBaV8H+PjHIe+D6Q9kccnIZcg2820FD5P5hsuNbLH7n1yex+W4mqPCeJePNhoc 1EkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707693430; x=1708298230; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8BelCqyqaTU+jGU/MDi1SS3yrhgj8pP4yG8n+9l1MNk=; b=NiTFhDMvjpiTr84tk8vwb90fEHCE6TtVuKE25/JPgz5WqUfvOL+1tRnO25B12i5nao ahiAeNeY4K3AbcjTfc76d2ooG+3vAFQDW16izl1qe7YvFesDpAbGejYoeplyeElCKIJ1 JQTYSR2w8jjH6KASHs7m9EqKZcQgXgm4vfEYfQSGRc0kIxOnK0ZdrdcORW8qsPoRboZM 2pA5JPtVVZpJ3Nur/LvEg2D5Ndu6XK9Pqg2Q5VQCCHtynzoO2cjto6+86wX1GUkfPxJa lDrIJ6IDaoyjdRybzPXr4sCL0yGHA3pklpnulLZNsRKoPsUdIPTN0MXbxLSmwPEk/029 2Wtg== X-Gm-Message-State: AOJu0YwVa52xYtet7qU9mbcdH6W6spCs3SNH3a+QnpPpEcLWGo8PE0dK eDxzLKfkak8Gs6COHbKj/2my0lliD8EPUBzQyYrFdOvTSNqLmlkjf3OLVut4JVPSx0jEFM4YLJI DB3rFW5+dW+fLr5py6eloJgDbrDA= X-Received: by 2002:adf:f812:0:b0:33b:66a1:d3d5 with SMTP id s18-20020adff812000000b0033b66a1d3d5mr3951831wrp.19.1707693429624; Sun, 11 Feb 2024 15:17:09 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240202113259.3045705-1-paul.heidekrueger@tum.de> <20240211091720.145235-1-paul.heidekrueger@tum.de> In-Reply-To: <20240211091720.145235-1-paul.heidekrueger@tum.de> From: Andrey Konovalov Date: Mon, 12 Feb 2024 00:16:58 +0100 Message-ID: Subject: Re: [PATCH v2] kasan: add atomic tests To: =?UTF-8?Q?Paul_Heidekr=C3=BCger?= Cc: akpm@linux-foundation.org, 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sun, Feb 11, 2024 at 10:17=E2=80=AFAM Paul Heidekr=C3=BCger wrote: > > 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.heidekrueg= er@tum.de/T/#u > Closes: https://bugzilla.kernel.org/show_bug.cgi?id=3D214055 > Reviewed-by: Marco Elver > Tested-by: Marco Elver > Acked-by: Mark Rutland > Signed-off-by: Paul Heidekr=C3=BCger > --- > 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_sto= re_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 =3D 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, saf= e, 42)); > + KUNIT_EXPECT_KASAN_FAIL(test, atomic_long_try_cmpxchg(safe, unsaf= e, 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(uns= afe)); > + KUNIT_EXPECT_KASAN_FAIL(test, atomic_long_dec_unless_positive(uns= afe)); > + 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 memo= ry such > + * that the following 16 bytes will make up the redzone. > + */ > + a1 =3D kzalloc(48, GFP_KERNEL); > + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, a1); > + a2 =3D kzalloc(sizeof(atomic_long_t), GFP_KERNEL); > + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, a1); This should check for a2, not a1. Sorry for not spotting this before. > + > + /* 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[] = =3D { > 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), > -- > 2.40.1 > With the mentioned change: Reviewed-by: Andrey Konovalov Thank you!