From patchwork Tue Jul 30 11:06:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jann Horn X-Patchwork-Id: 13747258 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 67D5DC3DA49 for ; Tue, 30 Jul 2024 11:06:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E59496B007B; Tue, 30 Jul 2024 07:06:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DE1DA6B0083; Tue, 30 Jul 2024 07:06:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C5B9E6B0085; Tue, 30 Jul 2024 07:06:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id A29276B007B for ; Tue, 30 Jul 2024 07:06:36 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 4A26F40284 for ; Tue, 30 Jul 2024 11:06:36 +0000 (UTC) X-FDA: 82396140792.15.E037D44 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) by imf03.hostedemail.com (Postfix) with ESMTP id 5258120029 for ; Tue, 30 Jul 2024 11:06:34 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=QpwY7Iyf; spf=pass (imf03.hostedemail.com: domain of jannh@google.com designates 209.85.128.41 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=1722337566; a=rsa-sha256; cv=none; b=l70pJzuMSbDdLTwk2gH3Owf+62gxTedn7xWtKNA++Kc6WqYeq9Adxd8xSxNPotIRVSuY7M pXWfSKYvd6QqujTcOZCcxlv25msBolYvEuU87HmyQf/wri9b/hoKWHDxpjuxbDJMdNpGcB edHWOsPJUUVuQtSqNztigcow+lcBivI= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=QpwY7Iyf; spf=pass (imf03.hostedemail.com: domain of jannh@google.com designates 209.85.128.41 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=1722337566; 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=lOneSascSLbcuGu52c+hQB2xrAbcWZqG1U/DIZcnB4s=; b=B2PmEVbRZvQAYapnINr0RiiIk5lyivucmvhLOPSdKH6MnoFdjLfNMEPA8cD/NT7si062Oz 5kZEKCg3r1sazTHxxI5b0mPWs/OlnUUJhs0qchUcSgwygqFXVuZWEZRSZJ7Id6EBN5veKJ khes2QSp5ptG8T2oGJTVE3pIDpBAjpc= Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-428078ebeb9so52555e9.0 for ; Tue, 30 Jul 2024 04:06:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722337593; x=1722942393; 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=lOneSascSLbcuGu52c+hQB2xrAbcWZqG1U/DIZcnB4s=; b=QpwY7IyfAY9DKY6Cqxb7lTrkMCAsYJ81oGIn2CFJV0tUJHvDvnVNehm69BeHlAOore dXX7ef7sv2+UWl/MsdfZwwJTZeav1Wz3deD1zjlrPPUmtk8vMeyfoSU+0/bCKSAOyY3r fX4dLdlTlmNxhhXae5vMLtagG8QJk16e/MfGbUN/KnojsJh48A1k9Rl7Y9YERqvUHNQa PBttx1VwnLubn4Qui385m65pZZmZ3VS80uuX5oBgqhAsCg4eVJbmMDvjJRkPVZIxMXVI oKJyMNGbuR5LClCNmrasTKZEFipu5wBr7sqvLCFUycHGZdx4tbzC2b2tmzRcV47rU5Wz qxTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722337593; x=1722942393; 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=lOneSascSLbcuGu52c+hQB2xrAbcWZqG1U/DIZcnB4s=; b=CA6MrXU4E/okwVrGxhAsCrH6PxMUQ79+NkXljd9W/R+jNVuFK4BizsmMYcTXlyud/d DEkM093eGdKiSgfwr+XEIInG9Pt98Gs1TjOWhnD5+6PKO9H/hvH0awH5KxzpXGaALRD0 AQLejBFiDUV/lg1eq3sOI35tdvpAXLg37t0bFJWva0L1b0FUDRNa93+OnnmZBO+l2pZ9 w73wvlagH0MlUGxH4rcO3h2njyD3PKZMcajegXZtdqbWeeau93DwZEVL8Ei1YE8pdwvY hzv08jJVnBOXsdZe20xFiweoBSJHh3q7ZafrhpDHeEaMgwyxBHna1uT80EHcpDYKuWnE PWHg== X-Forwarded-Encrypted: i=1; AJvYcCXDJwFLuSfjS4m8Nwx++utuPvfCKdQ/xHb2VNnY0RMWvz0QtIO93zO34mYK6r/2zhaa3Mp9/LiJOXkZxv86nYH9ipM= X-Gm-Message-State: AOJu0YxyvTmwGuIYmv2RZ2zWhi1uJKFHlCeyESaTCAH+qN7aXuWqgNUC T+2odKEBv1KTDyVXivXKhJMJK2BsPf7QfzdeTnF3+OUvTnrB9fNG5apDLpn/sw== X-Google-Smtp-Source: AGHT+IF3fRKZZdIsGgIyQAbL3H8VGw37NHFMr/L9IDJsTevf6Z7swn0AgORI6RgMQwTbS9AeZfScRw== X-Received: by 2002:a05:600c:3109:b0:426:6edd:61a7 with SMTP id 5b1f17b1804b1-42824a5715cmr857625e9.7.1722337592271; Tue, 30 Jul 2024 04:06:32 -0700 (PDT) Received: from localhost ([2a00:79e0:9d:4:be6a:cd70:bdf:6a62]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-428089c28f0sm195945165e9.28.2024.07.30.04.06.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jul 2024 04:06:30 -0700 (PDT) From: Jann Horn Subject: [PATCH v5 0/2] allow KASAN to detect UAF in SLAB_TYPESAFE_BY_RCU slabs Date: Tue, 30 Jul 2024 13:06:02 +0200 Message-Id: <20240730-kasan-tsbrcu-v5-0-48d3cbdfccc5@google.com> MIME-Version: 1.0 X-B4-Tracking: v=1; b=H4sIABrJqGYC/2XM3QrCIACG4VsJjzPU+dtR9xEdONNNqhm6pBi79 9wg2Nbh98HzDiDZ6G0Cx90Aos0++dCVwfY7YFrdNRb6a9mAIEKRIBW86aQ72Kc6mhesBWZaIey E4KCQZ7TOv+fc+VJ261Mf4meuZzK9vxBdhzKBCFLmpJKIU8rlqQmhuduDCQ8wlXK11Gyjq6IZN oo46TBG+E/TpVYbTSctrJHMOokMX+lxHL9Id5NxJQEAAA== 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-Stat-Signature: b5y1qs4d5fwxfsgsfr6s9r7ug87kir3a X-Rspamd-Queue-Id: 5258120029 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1722337594-991760 X-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slHh5AlcZoNnycd7s+Xx8KbRm157TGptODRTiadbez/9CRPjhHxml0CCQbqplelUar/XwBojlv8swCI0khbsmBDAAlnlyC2CVsJ2L+T9i1ykG0ec94JIWIZxrRehSCjdC/AGje4bk1RWZhYQW+OousN6AAgKZ1eSdiKt4tRzy1HP5kYOjZqkzBeGKqnstMlC+sOcbU81rHghT9QHoae1KiLVQuCk2hEM6cNrMWAddj73MM2zfFe9dVUfAd3g09R+CPAgVhF0W+FBzXZfYwNTxBNtje+XiJ9raxThjAOoaM6OmaznVMsmHi/JQEuc1BsqRrIG4NBua8SuHACV6UlbuLZC7R7ro+ENxEaXoMEKGbRtgxSN3kewQLE/BRD3E 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 v5: - rebase to latest origin/master (akpm), no other changes from v4 - Link to v4: https://lore.kernel.org/r/20240729-kasan-tsbrcu-v4-0-57ec85ef80c6@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) - Link to v3: https://lore.kernel.org/r/20240725-kasan-tsbrcu-v3-0-51c92f8f1101@google.com 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 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@. --- 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: 94ede2a3e9135764736221c080ac7c0ad993dc2d change-id: 20240723-kasan-tsbrcu-b715a901f776