From patchwork Mon Jul 29 18:56:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jann Horn X-Patchwork-Id: 13745590 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 E7FC4C3DA61 for ; Mon, 29 Jul 2024 18:56:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4FCB46B008C; Mon, 29 Jul 2024 14:56:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 486676B0092; Mon, 29 Jul 2024 14:56:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 327796B0093; Mon, 29 Jul 2024 14:56:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 0F5226B008C for ; Mon, 29 Jul 2024 14:56:27 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id BF3C0140597 for ; Mon, 29 Jul 2024 18:56:26 +0000 (UTC) X-FDA: 82393695972.20.11FC44E Received: from mail-ed1-f45.google.com (mail-ed1-f45.google.com [209.85.208.45]) by imf29.hostedemail.com (Postfix) with ESMTP id D4549120006 for ; Mon, 29 Jul 2024 18:56:24 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=MWrbfR4Z; spf=pass (imf29.hostedemail.com: domain of jannh@google.com designates 209.85.208.45 as permitted sender) smtp.mailfrom=jannh@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722279313; 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: references:dkim-signature; bh=OJFBXmp2nnWSyPQ0vZXV6CH2fcDg2DG8vfwkkfKQGv4=; b=w8nHDUDlMvx0Q7yPiR/ocqwRz7hXTnqMYJug1lJdSiWX2kuh8X9JTAf1IXLZYZKpAy2hIk S/bnjhDmg5FqDvgTizNVFpNoXPoJjObRTbmJrVU5WfRmBfaSXvo+QgZgLxztZObgdeJU7O O2vG5h7vJhyPO0sH/H0vv5qQUuigjFA= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=MWrbfR4Z; spf=pass (imf29.hostedemail.com: domain of jannh@google.com designates 209.85.208.45 as permitted sender) smtp.mailfrom=jannh@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722279313; a=rsa-sha256; cv=none; b=prPBet8x0Awimj+O+SuangXCHA6HxQPXnyEMUCMvlix4oLFTCrHuoFfX0i2BSbXE/GSVoJ NSpjT5cXhSL+b6QuXaAidh1VleHONlQAtkj5FzTXGs46y3JbAa7G8x8CsQYnohzM0uDmXg dsdh1qdVEHSkDJycoiBtpKU8HPEwNfY= Received: by mail-ed1-f45.google.com with SMTP id 4fb4d7f45d1cf-5a869e3e9dfso3083a12.0 for ; Mon, 29 Jul 2024 11:56:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722279383; x=1722884183; darn=kvack.org; h=cc:to:content-transfer-encoding:mime-version:message-id:date :subject:from:from:to:cc:subject:date:message-id:reply-to; bh=OJFBXmp2nnWSyPQ0vZXV6CH2fcDg2DG8vfwkkfKQGv4=; b=MWrbfR4ZViRbIX66MXaXp7gDwMbbhqPXhS5u9tPHkFAdAguOe3rf4+vb3Ws1fcIlX4 iAk+81Vr2kKjgqViSYcnLbJNHzhvZyAXVuqBe6BMkIo+q99wVoJNPKVKePZEJXqYR2Az g8AoLxYIbClCXnT6SiunLOjntr7TJrv5fDxooMhUvRMgeuFovrLNqixnTqwEHUaX/NI4 DdgTZonXWkKkdNiRg1Jk64BJLa5mTmYlYWj87rF5DoB/gxo1+OrmcVKHU187ma5uL9Js olsRBrAYGKvlqJa8+qTHsKt8aairMOHuP0P/m1223OyA9eI5oyc06oAqYmYUy4wGxn8S pv6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722279383; x=1722884183; h=cc:to:content-transfer-encoding:mime-version:message-id:date :subject:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=OJFBXmp2nnWSyPQ0vZXV6CH2fcDg2DG8vfwkkfKQGv4=; b=LmAfNub/2jXnyz0t1Klrix98pfd2Gn9xNoYpQxttn4fgAx9fRs+A7YnFSaU0/RMktS 23dUvMK3O3/IkbmcKr459hv0h6CknrZMK4evOjNKvvpkV7TmaZebJg5FvErDvp08YGxy /au1DjwzTKARuw0EKWQuDotzNrUJV9G95nQRwQcSwExyZEfE9sstnNAl1qEemuARFa9m lmuzw617rocistqBsIeRLhsx5k8xrc/W0QQGKPdduFXIT1QUW7sopHViv0MWbKzMr2+A 66hXTj0I2uDz96xm1yacjKUfaQLi8P3/f2UjZopp7FiOCR4k1mBnVlyhy+tmS4yGRfPf te8w== X-Forwarded-Encrypted: i=1; AJvYcCWe3u/r2Znj4jfiFdTAuxBZ+xLe9teyioIe6iI/G22UKxCANcVIh+EXNQPh15aW0HB5m0tXlUWkm1fvyohQ9EY/+nI= X-Gm-Message-State: AOJu0YzDQtH7MXtK8oTTl8bC7ctW80qKVJfq/kBPyrQh1HLcXZAB1Hwe hKmmXM5MA71mb6K3cKFJT6EKCiIkIM5C3pFEeAxUg/il1BoS2vgNSIgvM/JPyg== X-Google-Smtp-Source: AGHT+IGHDAR6BfxXjOVdnoYWcpWOMI+YQdQQsZduiIDJgRJ/qHn3aYgnBzbWyENtpVxHf1LGg7bDNg== X-Received: by 2002:a05:6402:1d4e:b0:57c:c3a7:dab6 with SMTP id 4fb4d7f45d1cf-5b40cede1e6mr72548a12.3.1722279382616; Mon, 29 Jul 2024 11:56:22 -0700 (PDT) Received: from localhost ([2a00:79e0:9d:4:a1f4:32c9:4fcd:ec6c]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36b36857fdesm13151543f8f.75.2024.07.29.11.56.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jul 2024 11:56:22 -0700 (PDT) From: Jann Horn Subject: [PATCH v4 0/2] allow KASAN to detect UAF in SLAB_TYPESAFE_BY_RCU slabs Date: Mon, 29 Jul 2024 20:56:10 +0200 Message-Id: <20240729-kasan-tsbrcu-v4-0-57ec85ef80c6@google.com> MIME-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAMrlp2YC/13M0QrCIBiG4VsZ/3GGOp2uo+4jOnCmm1QauqQYu /fcIGgdfh+8zwTJRGcSHKoJoskuueDLYLsK9KB8b5C7lA0UU4YFrdFVJeXRmLqon6gThKsWEyt EAyV5RGPda+VO57IHl8YQ36ue6fJ+IbaFMkUYMW5lK3HDWCOPfQj9zex1uMMi5fq35n91XWpOd EuttIRgsqnnef4AQdb6v+cAAAA= To: Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Andrew Morton , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Vlastimil Babka , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: Marco Elver , kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Jann Horn X-Mailer: b4 0.15-dev X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: D4549120006 X-Stat-Signature: acxdr3ig6kawozqiurht7ctjs6bez1fr X-Rspam-User: X-HE-Tag: 1722279384-467766 X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iTpoQF5014nrcG+MHjVDmQWyN6ykUCgZZ81YHcLq4CUsrYKpdL/wjunTi8+7miswaNPSiKIWlOfN9w7W9kGvF445xqAaKS/vaDgehL0aLGabiUcvpLjtrLY45Gj+PXV/+L4J2HSYvPDg6nlMbpFKu9eNkwvdZ4vPoZe5bTrUVK6d7pAvGpeBAS5I+2Z4F7cF6vsKBXdRDz+ryaOZx8JxhM4Z3L0Xwvc27QPtP8HsaTP4ZOgKQZLJoIOrAnQdNd3O6PqmQ3vZbAD2SR/Oi74ySwcCbMMJnXAnUBT2FDYG79eHC8Ru0MNQ0+SusDmtU5aOU109tjiL8T0czu4VWs45dAlGN7vKs6SixA80q5ONqLDIxFsXJl/rX/aqeGEH989Q2HhvSM 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: Hi! The purpose of the series is to allow KASAN to detect use-after-free access in SLAB_TYPESAFE_BY_RCU slab caches, by essentially making them behave as if the cache was not SLAB_TYPESAFE_BY_RCU but instead every kfree() in the cache was a kfree_rcu(). This is gated behind a config flag that is supposed to only be enabled in fuzzing/testing builds where the performance impact doesn't matter. Output of the new kunit testcase I added to the KASAN test suite: ================================================================== BUG: KASAN: slab-use-after-free in kmem_cache_rcu_uaf+0x3ae/0x4d0 Read of size 1 at addr ffff888106224000 by task kunit_try_catch/224 CPU: 7 PID: 224 Comm: kunit_try_catch Tainted: G B N 6.10.0-00003-g065427d4b87f #430 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014 Call Trace: dump_stack_lvl+0x53/0x70 print_report+0xce/0x670 [...] kasan_report+0xa5/0xe0 [...] kmem_cache_rcu_uaf+0x3ae/0x4d0 [...] kunit_try_run_case+0x1b3/0x490 [...] kunit_generic_run_threadfn_adapter+0x80/0xe0 kthread+0x2a5/0x370 [...] ret_from_fork+0x34/0x70 [...] ret_from_fork_asm+0x1a/0x30 Allocated by task 224: kasan_save_stack+0x33/0x60 kasan_save_track+0x14/0x30 __kasan_slab_alloc+0x6e/0x70 kmem_cache_alloc_noprof+0xef/0x2b0 kmem_cache_rcu_uaf+0x10d/0x4d0 kunit_try_run_case+0x1b3/0x490 kunit_generic_run_threadfn_adapter+0x80/0xe0 kthread+0x2a5/0x370 ret_from_fork+0x34/0x70 ret_from_fork_asm+0x1a/0x30 Freed by task 0: kasan_save_stack+0x33/0x60 kasan_save_track+0x14/0x30 kasan_save_free_info+0x3b/0x60 __kasan_slab_free+0x57/0x80 slab_free_after_rcu_debug+0xe3/0x220 rcu_core+0x676/0x15b0 handle_softirqs+0x22f/0x690 irq_exit_rcu+0x84/0xb0 sysvec_apic_timer_interrupt+0x6a/0x80 asm_sysvec_apic_timer_interrupt+0x1a/0x20 Last potentially related work creation: kasan_save_stack+0x33/0x60 __kasan_record_aux_stack+0x8e/0xa0 kmem_cache_free+0x10c/0x420 kmem_cache_rcu_uaf+0x16e/0x4d0 kunit_try_run_case+0x1b3/0x490 kunit_generic_run_threadfn_adapter+0x80/0xe0 kthread+0x2a5/0x370 ret_from_fork+0x34/0x70 ret_from_fork_asm+0x1a/0x30 The buggy address belongs to the object at ffff888106224000 which belongs to the cache test_cache of size 200 The buggy address is located 0 bytes inside of freed 200-byte region [ffff888106224000, ffff8881062240c8) The buggy address belongs to the physical page: page: refcount:1 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x106224 head: order:1 mapcount:0 entire_mapcount:0 nr_pages_mapped:0 pincount:0 flags: 0x200000000000040(head|node=0|zone=2) page_type: 0xffffefff(slab) raw: 0200000000000040 ffff88810621c140 dead000000000122 0000000000000000 raw: 0000000000000000 00000000801f001f 00000001ffffefff 0000000000000000 head: 0200000000000040 ffff88810621c140 dead000000000122 0000000000000000 head: 0000000000000000 00000000801f001f 00000001ffffefff 0000000000000000 head: 0200000000000001 ffffea0004188901 ffffffffffffffff 0000000000000000 head: 0000000000000002 0000000000000000 00000000ffffffff 0000000000000000 page dumped because: kasan: bad access detected Memory state around the buggy address: ffff888106223f00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc ffff888106223f80: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc >ffff888106224000: fa fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb ^ ffff888106224080: fb fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc ffff888106224100: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc ================================================================== ok 38 kmem_cache_rcu_uaf Signed-off-by: Jann Horn --- Changes in v4: - EDITME: describe what is new in this series revision. - EDITME: use bulletpoints and terse descriptions. - Link to v3: https://lore.kernel.org/r/20240725-kasan-tsbrcu-v3-0-51c92f8f1101@google.com Changes in v2: Patch 1/2 is new; it's some necessary prep work for the main patch to work, though the KASAN integration maybe is a bit ugly. Patch 2/2 is a rebased version of the old patch, with some changes to how the config is wired up, with poison/unpoison logic added as suggested by dvyukov@ back then, with cache destruction fixed using rcu_barrier() as pointed out by dvyukov@ and the test robot, and a test added as suggested by elver@. Changes in v3: - in patch 1/2, integrate akpm's fix for !CONFIG_KASAN build failure - in patch 2/2, as suggested by vbabka, use dynamically allocated rcu_head to avoid having to add slab metadata - in patch 2/2, add a warning in the kconfig help text that objects can be recycled immediately under memory pressure - Link to v2: https://lore.kernel.org/r/20240724-kasan-tsbrcu-v2-0-45f898064468@google.com Changes in v4: - note I kept vbabka's ack for the SLUB changes in patch 1/2 since the SLUB part didn't change, even though I refactored a bunch of the KASAN parts - in patch 1/2 (major rework): - fix commit message (Andrey) - add doc comments in header (Andrey) - remove "ip" argument from __kasan_slab_free() - rework the whole check_slab_free() thing and move code around (Andrey) - in patch 2/2: - kconfig description and dependency changes (Andrey) - remove useless linebreak (Andrey) - fix comment style (Andrey) - fix do_slab_free() invocation (kernel test robot) --- Jann Horn (2): kasan: catch invalid free before SLUB reinitializes the object slub: Introduce CONFIG_SLUB_RCU_DEBUG include/linux/kasan.h | 50 +++++++++++++++++++++++++++---- mm/Kconfig.debug | 30 +++++++++++++++++++ mm/kasan/common.c | 60 +++++++++++++++++++++++-------------- mm/kasan/kasan_test.c | 46 ++++++++++++++++++++++++++++ mm/slab_common.c | 12 ++++++++ mm/slub.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++----- 6 files changed, 245 insertions(+), 36 deletions(-) --- base-commit: 0c3836482481200ead7b416ca80c68a29cfdaabd change-id: 20240723-kasan-tsbrcu-b715a901f776