From patchwork Fri Aug 2 20:31:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jann Horn X-Patchwork-Id: 13751971 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 331F9C3DA7F for ; Fri, 2 Aug 2024 20:32:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 999C66B00B0; Fri, 2 Aug 2024 16:32:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8FADE6B00B2; Fri, 2 Aug 2024 16:32:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6FE726B00B3; Fri, 2 Aug 2024 16:32:18 -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 4943F6B00B0 for ; Fri, 2 Aug 2024 16:32:18 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id F3841811F1 for ; Fri, 2 Aug 2024 20:32:17 +0000 (UTC) X-FDA: 82408452714.13.BBF4DC2 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) by imf21.hostedemail.com (Postfix) with ESMTP id E334C1C000B for ; Fri, 2 Aug 2024 20:32:15 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=oS3YZvLg; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of jannh@google.com designates 209.85.128.52 as permitted sender) smtp.mailfrom=jannh@google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722630688; a=rsa-sha256; cv=none; b=qI3LakrfeEqI8Y0L2hxo2hXeUEzpv9LgGUaw7WStVT13f5Th6AhHBG6Dt35ZOByhuBluz8 FAY4Cm4jJsmYdnRMfsdDNEDC5clnrUsNEKLSWM8Ebp/ig/mZ9Y4rNx2XTZXw9V9+DC2E1P uR3rrmSpsDYV0rxmqGmjbsizIltMB0k= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=oS3YZvLg; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of jannh@google.com designates 209.85.128.52 as permitted sender) smtp.mailfrom=jannh@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722630688; 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=OtIY/lbfQ818ndlf2SV0BX8ni+uEKDx/O9Hr9lNf93M=; b=kQZeP/1BelfvpXbSW1Ky3Dk3Tpt//ZwUlk4200GFDRoxu6AdNECJ3wJ/CTr8+d2RdfOJh8 Gm5tJBFDGVTq4nhB1R2lgUa64bW3QQbi6Ax2RPTNFEUe9V5hMc6i6epqI8cymZyAYDSY1q k6IrmTPz6blUyG41kYd0PlkKk6n6I/s= Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-42807cb6afdso258385e9.1 for ; Fri, 02 Aug 2024 13:32:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722630734; x=1723235534; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=OtIY/lbfQ818ndlf2SV0BX8ni+uEKDx/O9Hr9lNf93M=; b=oS3YZvLgoIkrNAxxL4G6IesGel1OcYEbKL03Nc+XEQObvaYvNQUPFlg6WfTptlEmtu pDTDU1Ob4a1livMo/uwjT3AJi6s0NIg4oRooz0q4j02x6Xe2yptl4HpsEHTIrMYUKesO Ob+DJ/oK0LFq6W24KMAyf1azI5BKOqeyxkBBMEEEuNi85gt2u7wkFPtYXL57jEf6SrNM 3IOhBUdcxdCzda0+FXKdzwi0bb0ywv2dHdNelnQNhAmZSm9Q46NtWBQD3KtZsnUiPGsE FkaeII7pvi/ce6A3epsL/+MMU2IVTob+T9TE0pPmvZxBxQxEtkPzvpAnGRgyZYx1wS3c kRZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722630734; x=1723235534; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OtIY/lbfQ818ndlf2SV0BX8ni+uEKDx/O9Hr9lNf93M=; b=KJam1YyTmopLpq5BFm6Im8pBJKIeu5yREJtsBomaC7/fHu3MJjcDxF/qYkuI+c5BH2 Y2epGxG52tKLuCLZTt6pq9pVwiJsdDmkHzOO/zuG1bza8AwxdfS5DLCpobm+OFAvQY1n PUQUeWT7fy9SlBNFks0orelH/x12a4b4Uv/10834RUClG2q+GUflAPQvXJS9naNtbnn7 7rOT1lWCLrzyTMRfUvjvAT6cNg2nRp2B9NloIwnPwDdnKqtccDxl7j/4Dvf796p449tL A25M2NnD5NTgXOt/RoRXyF2OsjVVE50kQYcnpAzVC+LZw1FVqFgtKeyLFRV9uc+XY6Cc 9wsg== X-Forwarded-Encrypted: i=1; AJvYcCWh30AW3wFLaDWiPvzFYhQcZndp/3UgcpxY6pidppb4doaIIpyFBxlGS0mTIk1Us0vHzIuYgybRAD4xsyadTTFuAgk= X-Gm-Message-State: AOJu0Yy52lMQ0Av3xmC4E7xezpfqScx89FUsWJBmBEyGQG/IChXbIgJw AT9wb0gIXbuw2P8QWoCUqLpH+ov/vb69MFiwTYNhUyBV4bGSL509w+2+y4P0Kw== X-Google-Smtp-Source: AGHT+IHgObnJcgoST6ZUYoM69nUyHyMSZdwu3GwWheKkgoMDLPs7gmEI4a2Ly4zw5Ykmyy7wwcJ9iw== X-Received: by 2002:a05:600c:a08c:b0:428:e6eb:1340 with SMTP id 5b1f17b1804b1-428ef3cbbdfmr44035e9.4.1722630733443; Fri, 02 Aug 2024 13:32:13 -0700 (PDT) Received: from localhost ([2a00:79e0:9d:4:9337:bd1:a20d:682d]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-428e6e9d1b4sm43954895e9.39.2024.08.02.13.32.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Aug 2024 13:32:12 -0700 (PDT) From: Jann Horn Date: Fri, 02 Aug 2024 22:31:53 +0200 Subject: [PATCH v6 1/2] kasan: catch invalid free before SLUB reinitializes the object MIME-Version: 1.0 Message-Id: <20240802-kasan-tsbrcu-v6-1-60d86ea78416@google.com> References: <20240802-kasan-tsbrcu-v6-0-60d86ea78416@google.com> In-Reply-To: <20240802-kasan-tsbrcu-v6-0-60d86ea78416@google.com> 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-Developer-Signature: v=1; a=ed25519-sha256; t=1722630727; l=8985; i=jannh@google.com; s=20240730; h=from:subject:message-id; bh=n3HTPxz1vqklcUBCM2b1ohNM905PLTWckUn1zsgT95o=; b=pBuk9xoZ3On9sdWadP+S/JMQOgnWggeyhLUucDdlYyDK+oaS4P6P0S2gqDMVcBntQIES2k5g0 G7Dzmk5va7PAV+bbKgU4Ounb6xSvRgiyFg+cTwdckFsSVg/xFZitzGz X-Developer-Key: i=jannh@google.com; a=ed25519; pk=AljNtGOzXeF6khBXDJVVvwSEkVDGnnZZYqfWhP1V+C8= X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: E334C1C000B X-Stat-Signature: nj8udq4bjtoosdyqteyit6g8qdepc9t4 X-Rspam-User: X-HE-Tag: 1722630735-98930 X-HE-Meta: U2FsdGVkX18QvwrloQuRos9M9Z6b7GSHMBkab45EmVl5n2WM3CyvzA0lnGi1LfI3Y36S7tQLgEl8oGSp1v11Z2NMNMGZ2FPI48r94Im5e02IRFto1YrVFNNFPTvweLVpNVhdi+Vh5y3WMOCAcWKrJpPAiJhSi5VTYZOYqc6rozirlS0ee7NbwFy7+q9Da5HIvIwVQLYvPF48rZT5hoeTvBKscM3fXihv3TqxfqNmBJLNtSH1YUEXCW1vFgODxH50w305gWYGxNmlBNTRaXyZwAOgfU6T2zgsFgDKOgSEDTa58tKyglLQEcId30br+WDdBbo0ghPTGmGc6LAPrxBb1WcmbGyUFqn16W4UGGr1qHhYktdYa6ygyuk1UeV0ulnQtiAtVtITAFTCKtBN5rWpKH73qDibs+e3A4mqpnzKMM53/52Tt+NKzbR53aqdwihe1qLTanMv9T2GNwMN38PTHc6Ggvxu09Ewg0hEHvGTLc8yFaRuXQC1qCD1N/mAMoLEymQVdC9vafjVDaa6ehTqjkIF3LI/KMBbfBIIaY2iEOLj7Ue8b0BkD9rvHHSc0T15sXPaBekNJFx90RArOs5F0/0wJX0wwo3wkJmHUZjUHONOjfHJ9yvmVp3x+TYODdM0GxqO+Gi8H6Bc4frcFNyP7SOs9rhqKO2OIOigumN0cYB9AEPgWFWmU0w/fpmhu3v62Ribhu42Pyx+sRzjq86IPJo3fyYZkONNVS4eOYKst2oKTTHbOqSU4ro0GvXkzSW68kxTOSLRPW16vN20XlB82rjkqB7mPWSlxpheJdDjf4JNivgUSUkm4bIR0sTd0OR2hWh/JjBLxJzQt177o5Mub5/YTpGCFf7N+Sdt3U12jhJStR5w7gRRIaiVYaksLnotNQ/Ixn9k0rfHpjIpmp1AG/OUFG0PXoF2Z8L1x4zLcAlJe4+tYf7opFt1mdkkynHbtp9FFNoh9hXjPss2CbP CchHahNB 24w8e2ATr3d1EJT/rcPj7FdLA4rpr473J9odb99MwbRxXJCwX1/QIVX89X7uahI5wSVbbQNFRkijhk/gft8t3Rs+rimjQEUEOJvYfa/IdtwzFYX5tgwHIcbP2RRcO+dR9bQ0T8XQVyWx1bduusnhK/p/oOhoPSSdap0O9m+K3fLJP32Xaq3d/oblIIsEA0d9sbSYcuIHygOp06+E78Y1zYlOp/pIW6kbr7SjITqyjSdDV/JqTAPD+H1+y4Gik5saU2UfLdtPbL/vkahaB8sJvL1lFw9/4YpM8nnkEbOUNCnGkb3E96idUfFW4VfEe7NZS6xydtcfcwyuobdkHKDFD3zLWlr+wKwHbwYtBN3tJJYbTt41/iPg0e1n6RTiOXBVQhvD7ijr0HDGxe50= 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: Currently, when KASAN is combined with init-on-free behavior, the initialization happens before KASAN's "invalid free" checks. More importantly, a subsequent commit will want to RCU-delay the actual SLUB freeing of an object, and we'd like KASAN to still validate synchronously that freeing the object is permitted. (Otherwise this change will make the existing testcase kmem_cache_invalid_free fail.) So add a new KASAN hook that allows KASAN to pre-validate a kmem_cache_free() operation before SLUB actually starts modifying the object or its metadata. Inside KASAN, this: - moves checks from poison_slab_object() into check_slab_allocation() - moves kasan_arch_is_ready() up into callers of poison_slab_object() - removes "ip" argument of poison_slab_object() and __kasan_slab_free() (since those functions no longer do any reporting) Acked-by: Vlastimil Babka #slub Reviewed-by: Andrey Konovalov Signed-off-by: Jann Horn --- include/linux/kasan.h | 54 ++++++++++++++++++++++++++++++++++++++++++--- mm/kasan/common.c | 61 ++++++++++++++++++++++++++++++--------------------- mm/slub.c | 7 ++++++ 3 files changed, 94 insertions(+), 28 deletions(-) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index 70d6a8f6e25d..1570c7191176 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -172,19 +172,61 @@ static __always_inline void * __must_check kasan_init_slab_obj( { if (kasan_enabled()) return __kasan_init_slab_obj(cache, object); return (void *)object; } -bool __kasan_slab_free(struct kmem_cache *s, void *object, - unsigned long ip, bool init); +bool __kasan_slab_pre_free(struct kmem_cache *s, void *object, + unsigned long ip); +/** + * kasan_slab_pre_free - Check whether freeing a slab object is safe. + * @object: Object to be freed. + * + * This function checks whether freeing the given object is safe. It may + * check for double-free and invalid-free bugs and report them. + * + * This function is intended only for use by the slab allocator. + * + * @Return true if freeing the object is unsafe; false otherwise. + */ +static __always_inline bool kasan_slab_pre_free(struct kmem_cache *s, + void *object) +{ + if (kasan_enabled()) + return __kasan_slab_pre_free(s, object, _RET_IP_); + return false; +} + +bool __kasan_slab_free(struct kmem_cache *s, void *object, bool init); +/** + * kasan_slab_free - Poison, initialize, and quarantine a slab object. + * @object: Object to be freed. + * @init: Whether to initialize the object. + * + * This function informs that a slab object has been freed and is not + * supposed to be accessed anymore, except for objects in + * SLAB_TYPESAFE_BY_RCU caches. + * + * For KASAN modes that have integrated memory initialization + * (kasan_has_integrated_init() == true), this function also initializes + * the object's memory. For other modes, the @init argument is ignored. + * + * This function might also take ownership of the object to quarantine it. + * When this happens, KASAN will defer freeing the object to a later + * stage and handle it internally until then. The return value indicates + * whether KASAN took ownership of the object. + * + * This function is intended only for use by the slab allocator. + * + * @Return true if KASAN took ownership of the object; false otherwise. + */ static __always_inline bool kasan_slab_free(struct kmem_cache *s, void *object, bool init) { if (kasan_enabled()) - return __kasan_slab_free(s, object, _RET_IP_, init); + return __kasan_slab_free(s, object, init); return false; } void __kasan_kfree_large(void *ptr, unsigned long ip); static __always_inline void kasan_kfree_large(void *ptr) { @@ -368,12 +410,18 @@ static inline void kasan_poison_new_object(struct kmem_cache *cache, void *object) {} static inline void *kasan_init_slab_obj(struct kmem_cache *cache, const void *object) { return (void *)object; } + +static inline bool kasan_slab_pre_free(struct kmem_cache *s, void *object) +{ + return false; +} + static inline bool kasan_slab_free(struct kmem_cache *s, void *object, bool init) { return false; } static inline void kasan_kfree_large(void *ptr) {} static inline void *kasan_slab_alloc(struct kmem_cache *s, void *object, diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 85e7c6b4575c..f26bbc087b3b 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -205,59 +205,65 @@ void * __must_check __kasan_init_slab_obj(struct kmem_cache *cache, /* Tag is ignored in set_tag() without CONFIG_KASAN_SW/HW_TAGS */ object = set_tag(object, assign_tag(cache, object, true)); return (void *)object; } -static inline bool poison_slab_object(struct kmem_cache *cache, void *object, - unsigned long ip, bool init) +/* Returns true when freeing the object is not safe. */ +static bool check_slab_allocation(struct kmem_cache *cache, void *object, + unsigned long ip) { - void *tagged_object; - - if (!kasan_arch_is_ready()) - return false; + void *tagged_object = object; - tagged_object = object; object = kasan_reset_tag(object); if (unlikely(nearest_obj(cache, virt_to_slab(object), object) != object)) { kasan_report_invalid_free(tagged_object, ip, KASAN_REPORT_INVALID_FREE); return true; } - /* RCU slabs could be legally used after free within the RCU period. */ - if (unlikely(cache->flags & SLAB_TYPESAFE_BY_RCU)) - return false; - if (!kasan_byte_accessible(tagged_object)) { kasan_report_invalid_free(tagged_object, ip, KASAN_REPORT_DOUBLE_FREE); return true; } + return false; +} + +static inline void poison_slab_object(struct kmem_cache *cache, void *object, + bool init) +{ + void *tagged_object = object; + + object = kasan_reset_tag(object); + + /* RCU slabs could be legally used after free within the RCU period. */ + if (unlikely(cache->flags & SLAB_TYPESAFE_BY_RCU)) + return; + kasan_poison(object, round_up(cache->object_size, KASAN_GRANULE_SIZE), KASAN_SLAB_FREE, init); if (kasan_stack_collection_enabled()) kasan_save_free_info(cache, tagged_object); +} - return false; +bool __kasan_slab_pre_free(struct kmem_cache *cache, void *object, + unsigned long ip) +{ + if (!kasan_arch_is_ready() || is_kfence_address(object)) + return false; + return check_slab_allocation(cache, object, ip); } -bool __kasan_slab_free(struct kmem_cache *cache, void *object, - unsigned long ip, bool init) +bool __kasan_slab_free(struct kmem_cache *cache, void *object, bool init) { - if (is_kfence_address(object)) + if (!kasan_arch_is_ready() || is_kfence_address(object)) return false; - /* - * If the object is buggy, do not let slab put the object onto the - * freelist. The object will thus never be allocated again and its - * metadata will never get released. - */ - if (poison_slab_object(cache, object, ip, init)) - return true; + poison_slab_object(cache, object, init); /* * If the object is put into quarantine, do not let slab put the object * onto the freelist for now. The object's metadata is kept until the * object gets evicted from quarantine. */ @@ -501,17 +507,22 @@ bool __kasan_mempool_poison_object(void *ptr, unsigned long ip) if (check_page_allocation(ptr, ip)) return false; kasan_poison(ptr, folio_size(folio), KASAN_PAGE_FREE, false); return true; } - if (is_kfence_address(ptr)) - return false; + if (is_kfence_address(ptr) || !kasan_arch_is_ready()) + return true; slab = folio_slab(folio); - return !poison_slab_object(slab->slab_cache, ptr, ip, false); + + if (check_slab_allocation(slab->slab_cache, ptr, ip)) + return false; + + poison_slab_object(slab->slab_cache, ptr, false); + return true; } void __kasan_mempool_unpoison_object(void *ptr, size_t size, unsigned long ip) { struct slab *slab; gfp_t flags = 0; /* Might be executing under a lock. */ diff --git a/mm/slub.c b/mm/slub.c index 3520acaf9afa..0c98b6a2124f 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -2223,12 +2223,19 @@ bool slab_free_hook(struct kmem_cache *s, void *x, bool init) __kcsan_check_access(x, s->object_size, KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ASSERT); if (kfence_free(x)) return false; + /* + * Give KASAN a chance to notice an invalid free operation before we + * modify the object. + */ + if (kasan_slab_pre_free(s, x)) + return false; + /* * As memory initialization might be integrated into KASAN, * kasan_slab_free and initialization memset's must be * kept together to avoid discrepancies in behavior. * * The initialization memset's clear the object and the metadata, From patchwork Fri Aug 2 20:31:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jann Horn X-Patchwork-Id: 13751972 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 87EA0C52D6F for ; Fri, 2 Aug 2024 20:32:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D90A06B00B2; Fri, 2 Aug 2024 16:32:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D40C16B00B4; Fri, 2 Aug 2024 16:32:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A5C156B00B5; Fri, 2 Aug 2024 16:32:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 7A7856B00B2 for ; Fri, 2 Aug 2024 16:32:20 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 020BDA060E for ; Fri, 2 Aug 2024 20:32:19 +0000 (UTC) X-FDA: 82408452840.15.C709511 Received: from mail-ed1-f53.google.com (mail-ed1-f53.google.com [209.85.208.53]) by imf29.hostedemail.com (Postfix) with ESMTP id BFF09120025 for ; Fri, 2 Aug 2024 20:32:16 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=q82czvDi; spf=pass (imf29.hostedemail.com: domain of jannh@google.com designates 209.85.208.53 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=1722630707; a=rsa-sha256; cv=none; b=Ssqt8sjBGpV34k/J8sHEeDxWRWF8f+hykmxbRH4+qzHMN9L+ItcaaVCvlRltq6zhp8EEfV MpVJlhb4HeU7ZCahg/CZ7/VFaM1aosNJO9/uRsuC3VxkwBDgm4Ou9p/CsWwvFwQA/+/lJt J7LVSS/7aFmi0JlHcKPj1ia386VSdsw= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=q82czvDi; spf=pass (imf29.hostedemail.com: domain of jannh@google.com designates 209.85.208.53 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=1722630707; 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=gS9NPze8LdlLVayadYCCoL3LPpgBZRi8njeMti2tyuw=; b=0NNYWD/CjSdJEJvRvlEJYIz9kxdNreBtGlBnCS9q1muek3l1ndRpeEayLqj24WiwuTFNBb WWf3/Z7zqI3l5DTUdttMYUoneOmUl3Ju2a70ixvkLhTMqpq10q7GfxgoQCkpVU4iucpaCT xeLMsDInLwnvxTAaah7afOshf5o3zTU= Received: by mail-ed1-f53.google.com with SMTP id 4fb4d7f45d1cf-5a869e3e9dfso60251a12.0 for ; Fri, 02 Aug 2024 13:32:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722630735; x=1723235535; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=gS9NPze8LdlLVayadYCCoL3LPpgBZRi8njeMti2tyuw=; b=q82czvDiBKWHlVlBrVXZfLHja0c9zoBY3hsHre9DhEIny5IJHNNDQTe5UzZ3kJYWff oWVHJ6QdEimWg6ZjwkDfhV5WFY8d4T7MuWSinYDt3CXMALrq8Bm/CikNdQ3vyHXP/1U8 a5tcYOG9DP30pBlE2PZXJ7vTWLRBxI5dR9oes3nyrPj1cwVB5VNCx7LdIc+XsiZeImfo 9FjXFDYQDRxE0+Wbzb/yYq4YztIPie0gbG0X9kycf/6ib9g6Ng9NiqUuKRNJdxyPWrZT HW38yo0jckzyrc4+djUyTuX51BtA92EOPg7jwzmlEBYgyIDNvhf1ea+A2lDk2DlK1jf/ 4QaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722630735; x=1723235535; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gS9NPze8LdlLVayadYCCoL3LPpgBZRi8njeMti2tyuw=; b=WbFHKGfTY6NJXErFiZ/KN1sz31wMOULicdz4/CP8NYlsFE/rXnpda99RbZyJu80sXS akbeGFkl/KDwiBvXndrBoacfQ4XeA0z1eu0toFIs9oEuU+bMTwTQDz48/cMDHVhNiKCb jHOBLuQaYUMWFMHBDe/vO2lHARI/UHSM926luofqXLyH+lbK0RdyO74hJ4OHmLVD/M// s064Q4GM/6uV5aQDS17s/IfrzVHvvWPFEYkLIPHSghcpmuTyrFn/6JweOeoS82dGyBdz DNMZ+t4mWOMsgNDo3EKLnDOqv3Ndo8fol0s/2drl6jYqIJffz2xImHsQMYU5ibU4wGaD cGAg== X-Forwarded-Encrypted: i=1; AJvYcCXDql62rMnKNW2W9i68nhqRXE0X9kCkB/AlKgdji72dR9E1lAXKkXoHgGiW6EZcthKcayHbSkQgVAINqOSx89PtGSM= X-Gm-Message-State: AOJu0YxsAfUXJfkFlsLsBBJJFLtCIQe33vCK7+J0V0+7jZQ0dyUpkQv3 DG2NOHV5kStvitEnqr8V9kA8PBgmS+Cskm4HwIM2uZrdZ161jAA5xUcx6Spd8g== X-Google-Smtp-Source: AGHT+IFv0dXVHhpWJXAHPeFcXvpKYJDAr9fM9RpbVoJKsjWjUD0aiU4+SXhrqxN71bsdoQlOV1eO5A== X-Received: by 2002:a05:6402:2816:b0:58b:b1a0:4a2d with SMTP id 4fb4d7f45d1cf-5b9bebbc4ebmr17989a12.1.1722630734696; Fri, 02 Aug 2024 13:32:14 -0700 (PDT) Received: from localhost ([2a00:79e0:9d:4:9337:bd1:a20d:682d]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36bbd059841sm2716966f8f.89.2024.08.02.13.32.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Aug 2024 13:32:14 -0700 (PDT) From: Jann Horn Date: Fri, 02 Aug 2024 22:31:54 +0200 Subject: [PATCH v6 2/2] slub: Introduce CONFIG_SLUB_RCU_DEBUG MIME-Version: 1.0 Message-Id: <20240802-kasan-tsbrcu-v6-2-60d86ea78416@google.com> References: <20240802-kasan-tsbrcu-v6-0-60d86ea78416@google.com> In-Reply-To: <20240802-kasan-tsbrcu-v6-0-60d86ea78416@google.com> 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 , syzbot+263726e59eab6b442723@syzkaller.appspotmail.com X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=ed25519-sha256; t=1722630727; l=18156; i=jannh@google.com; s=20240730; h=from:subject:message-id; bh=J5IOz20kzk9KYQulLnVcj2lXMR6PVa1DwOVNOSWymvw=; b=0H92uCvLY0kw+qDQ7+oe+cgncSTtQqSeKfiDV9+uYaF7HUyE68U4lSJXaRet66PD8xXt5I7yU yVLZhZrHfR2B8lzQaC2fP/BEDYIh055KR/gBWnizqJ6XwYyTWWsnA13 X-Developer-Key: i=jannh@google.com; a=ed25519; pk=AljNtGOzXeF6khBXDJVVvwSEkVDGnnZZYqfWhP1V+C8= X-Stat-Signature: titi7ytkarzne1pjjemtoa1ry3xeiunx X-Rspamd-Queue-Id: BFF09120025 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1722630736-606372 X-HE-Meta: U2FsdGVkX1/RCSG4v0IH1HqHMiuxVx4/l06El8KwnR0fycGmqki7Xyk5yTgj7h3S2byn62PO3NdBFNK4cd/CTDg5iBSkY6JXmSa/pd4ocV0le1uKAKBgNlj2qyY8nIdco/QZ00Z7GA+jDMZ+hM64uV4e/NK+pw7JClGhX42lXTsQCUh4/JQrtJZd2YQUb9vtP4I8h/Y+ToIxAcjpnw/qffDmb7YaH738wUPuTAqg8QnHqGjGOmOj+x+U9Pj8RbKZElkUtgiLWIVIyCeXT/fbup3Tnl57d5841D6fHNxupTWDVKnUsQ1SjGoNWG2GqLvM4nk2qbDf84IlZrKqO7xT/fz6OyuNGHMiKmsbtc90/sk2/LbF0vB3H+2zceas0AtVnoUe1ahSv1bR305d84vSRNOciR9SC+S+KDgo/W8oL67Jf+gYl+PVpTz4PmxpiRGFdlQuDd+nziyuqybGEiibdF+RUm0U024QT2PYyhlwVjRfDHNlka4r0VSraDysIRTEGMa6DK8jN1CbkUQHFJ0noAFrGLd7O/cjYLi0kmY6FvKkYC5YipPV/aE37gnUsFZrW3eKXvcxAmZSsvhya8NzDItz7kXto6NkRLijX+weEaASEViohecDY22LR/49jDxpk2CUh5F7nl4W8GpXTY+ROyRMHD8oRMtcZUY87TtaCZMuNEMADB5yuezrdEXA/sNlUFaN4NrsszDUQeea/r49VsxWBHmCVxPQCxmv/btzKy9zMVLKxG9UjwO7VDSgc5SICcc8qjtebgS9cv1EM4wOfINBDmqR1TTs67omHc0SvxXKEJsaWCwuJdVnVfxN8a1JhBMEGglv3PWLMfBFEsfc6fK4Av9H1HTQctm2xuia5IYz3G9NohSHqoZMVGvEB/z59/sjz1WaA6ThfRFx3nWVg+Wpo1DiLNzrEQXu91f8LS2Yw5NY7B+zUVpFKxQPN5ndUnBfwVXkhX/MqTqyKGj 5FkepCMC OWeq66w8IlVr6WJ0ZBHefJIQDgeo5GpHG5BMjDZyt+m0AtDgkxfRgp9JDFZM3pEjiEerQ4Zr/+5f3+Bq4uLmeR2OJ1s/in91xWQIhoB+oKvsgph4OmI1aMDY6sG6X3eQzPmXx0TX8Y+I4kjsUawCJfujdqS5gjO3pXKtK1h+Q09+gVXH0qWveGP9M00PVvPpiOFMEYJiiWOUORsdWW4RWClmjMBo/ZtRMYEq3u8LCfQuFJNvMxVaVraxPmwNu16bJKsHeSIuX7BTg5rFvTlFSvvljhBTS+ueyLbTlYDcfiIA8B1kQi0n3k0bsth8StNpHFVG9rkysJm868l4lsU4GwHYqb0HV2nNfvfdpvPeZTDnIn7DnWMCJlf9e7MT+5gAu/1UUQG0MDOzXYjtqVwSG0hibxj2aGs+0Dzmo5D6BWTcC8hPJ+cw7GFB0aQLhT+Xk9aTMUZiRkEaGCY8wAjAjW6KkFHn5MU1BNQlngYroQtKszXpYYSHs7yDyBF/9HSaGkygN/oeFHoK8scq4FmJguBgqVh/AAOnDJhc8 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: Currently, KASAN is unable to catch use-after-free in SLAB_TYPESAFE_BY_RCU slabs because use-after-free is allowed within the RCU grace period by design. Add a SLUB debugging feature which RCU-delays every individual kmem_cache_free() before either actually freeing the object or handing it off to KASAN, and change KASAN to poison freed objects as normal when this option is enabled. For now I've configured Kconfig.debug to default-enable this feature in the KASAN GENERIC and SW_TAGS modes; I'm not enabling it by default in HW_TAGS mode because I'm not sure if it might have unwanted performance degradation effects there. Note that this is mostly useful with KASAN in the quarantine-based GENERIC mode; SLAB_TYPESAFE_BY_RCU slabs are basically always also slabs with a ->ctor, and KASAN's assign_tag() currently has to assign fixed tags for those, reducing the effectiveness of SW_TAGS/HW_TAGS mode. (A possible future extension of this work would be to also let SLUB call the ->ctor() on every allocation instead of only when the slab page is allocated; then tag-based modes would be able to assign new tags on every reallocation.) Tested-by: syzbot+263726e59eab6b442723@syzkaller.appspotmail.com Signed-off-by: Jann Horn Reviewed-by: Andrey Konovalov Acked-by: Marco Elver --- include/linux/kasan.h | 17 +++++++---- mm/Kconfig.debug | 30 +++++++++++++++++++ mm/kasan/common.c | 11 +++---- mm/kasan/kasan_test.c | 46 ++++++++++++++++++++++++++++++ mm/slab_common.c | 12 ++++++++ mm/slub.c | 79 +++++++++++++++++++++++++++++++++++++++++++++------ 6 files changed, 176 insertions(+), 19 deletions(-) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index 1570c7191176..00a3bf7c0d8f 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -193,40 +193,44 @@ static __always_inline bool kasan_slab_pre_free(struct kmem_cache *s, { if (kasan_enabled()) return __kasan_slab_pre_free(s, object, _RET_IP_); return false; } -bool __kasan_slab_free(struct kmem_cache *s, void *object, bool init); +bool __kasan_slab_free(struct kmem_cache *s, void *object, bool init, + bool still_accessible); /** * kasan_slab_free - Poison, initialize, and quarantine a slab object. * @object: Object to be freed. * @init: Whether to initialize the object. + * @still_accessible: Whether the object contents are still accessible. * * This function informs that a slab object has been freed and is not - * supposed to be accessed anymore, except for objects in - * SLAB_TYPESAFE_BY_RCU caches. + * supposed to be accessed anymore, except when @still_accessible is set + * (indicating that the object is in a SLAB_TYPESAFE_BY_RCU cache and an RCU + * grace period might not have passed yet). * * For KASAN modes that have integrated memory initialization * (kasan_has_integrated_init() == true), this function also initializes * the object's memory. For other modes, the @init argument is ignored. * * This function might also take ownership of the object to quarantine it. * When this happens, KASAN will defer freeing the object to a later * stage and handle it internally until then. The return value indicates * whether KASAN took ownership of the object. * * This function is intended only for use by the slab allocator. * * @Return true if KASAN took ownership of the object; false otherwise. */ static __always_inline bool kasan_slab_free(struct kmem_cache *s, - void *object, bool init) + void *object, bool init, + bool still_accessible) { if (kasan_enabled()) - return __kasan_slab_free(s, object, init); + return __kasan_slab_free(s, object, init, still_accessible); return false; } void __kasan_kfree_large(void *ptr, unsigned long ip); static __always_inline void kasan_kfree_large(void *ptr) { @@ -416,13 +420,14 @@ static inline void *kasan_init_slab_obj(struct kmem_cache *cache, static inline bool kasan_slab_pre_free(struct kmem_cache *s, void *object) { return false; } -static inline bool kasan_slab_free(struct kmem_cache *s, void *object, bool init) +static inline bool kasan_slab_free(struct kmem_cache *s, void *object, + bool init, bool still_accessible) { return false; } static inline void kasan_kfree_large(void *ptr) {} static inline void *kasan_slab_alloc(struct kmem_cache *s, void *object, gfp_t flags, bool init) diff --git a/mm/Kconfig.debug b/mm/Kconfig.debug index afc72fde0f03..8e440214aac8 100644 --- a/mm/Kconfig.debug +++ b/mm/Kconfig.debug @@ -67,12 +67,42 @@ config SLUB_DEBUG_ON equivalent to specifying the "slab_debug" parameter on boot. There is no support for more fine grained debug control like possible with slab_debug=xxx. SLUB debugging may be switched off in a kernel built with CONFIG_SLUB_DEBUG_ON by specifying "slab_debug=-". +config SLUB_RCU_DEBUG + bool "Enable UAF detection in TYPESAFE_BY_RCU caches (for KASAN)" + depends on SLUB_DEBUG + depends on KASAN # not a real dependency; currently useless without KASAN + default KASAN_GENERIC || KASAN_SW_TAGS + help + Make SLAB_TYPESAFE_BY_RCU caches behave approximately as if the cache + was not marked as SLAB_TYPESAFE_BY_RCU and every caller used + kfree_rcu() instead. + + This is intended for use in combination with KASAN, to enable KASAN to + detect use-after-free accesses in such caches. + (KFENCE is able to do that independent of this flag.) + + This might degrade performance. + Unfortunately this also prevents a very specific bug pattern from + triggering (insufficient checks against an object being recycled + within the RCU grace period); so this option can be turned off even on + KASAN builds, in case you want to test for such a bug. + + If you're using this for testing bugs / fuzzing and care about + catching all the bugs WAY more than performance, you might want to + also turn on CONFIG_RCU_STRICT_GRACE_PERIOD. + + WARNING: + This is designed as a debugging feature, not a security feature. + Objects are sometimes recycled without RCU delay under memory pressure. + + If unsure, say N. + config PAGE_OWNER bool "Track page owner" depends on DEBUG_KERNEL && STACKTRACE_SUPPORT select DEBUG_FS select STACKTRACE select STACKDEPOT diff --git a/mm/kasan/common.c b/mm/kasan/common.c index f26bbc087b3b..ed4873e18c75 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -227,43 +227,44 @@ static bool check_slab_allocation(struct kmem_cache *cache, void *object, } return false; } static inline void poison_slab_object(struct kmem_cache *cache, void *object, - bool init) + bool init, bool still_accessible) { void *tagged_object = object; object = kasan_reset_tag(object); /* RCU slabs could be legally used after free within the RCU period. */ - if (unlikely(cache->flags & SLAB_TYPESAFE_BY_RCU)) + if (unlikely(still_accessible)) return; kasan_poison(object, round_up(cache->object_size, KASAN_GRANULE_SIZE), KASAN_SLAB_FREE, init); if (kasan_stack_collection_enabled()) kasan_save_free_info(cache, tagged_object); } bool __kasan_slab_pre_free(struct kmem_cache *cache, void *object, unsigned long ip) { if (!kasan_arch_is_ready() || is_kfence_address(object)) return false; return check_slab_allocation(cache, object, ip); } -bool __kasan_slab_free(struct kmem_cache *cache, void *object, bool init) +bool __kasan_slab_free(struct kmem_cache *cache, void *object, bool init, + bool still_accessible) { if (!kasan_arch_is_ready() || is_kfence_address(object)) return false; - poison_slab_object(cache, object, init); + poison_slab_object(cache, object, init, still_accessible); /* * If the object is put into quarantine, do not let slab put the object * onto the freelist for now. The object's metadata is kept until the * object gets evicted from quarantine. */ @@ -515,13 +516,13 @@ bool __kasan_mempool_poison_object(void *ptr, unsigned long ip) slab = folio_slab(folio); if (check_slab_allocation(slab->slab_cache, ptr, ip)) return false; - poison_slab_object(slab->slab_cache, ptr, false); + poison_slab_object(slab->slab_cache, ptr, false, false); return true; } void __kasan_mempool_unpoison_object(void *ptr, size_t size, unsigned long ip) { struct slab *slab; diff --git a/mm/kasan/kasan_test.c b/mm/kasan/kasan_test.c index 7b32be2a3cf0..567d33b493e2 100644 --- a/mm/kasan/kasan_test.c +++ b/mm/kasan/kasan_test.c @@ -993,12 +993,57 @@ static void kmem_cache_invalid_free(struct kunit *test) */ kmem_cache_free(cache, p); kmem_cache_destroy(cache); } +static void kmem_cache_rcu_uaf(struct kunit *test) +{ + char *p; + size_t size = 200; + struct kmem_cache *cache; + + KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB_RCU_DEBUG); + + cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU, + NULL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache); + + p = kmem_cache_alloc(cache, GFP_KERNEL); + if (!p) { + kunit_err(test, "Allocation failed: %s\n", __func__); + kmem_cache_destroy(cache); + return; + } + *p = 1; + + rcu_read_lock(); + + /* Free the object - this will internally schedule an RCU callback. */ + kmem_cache_free(cache, p); + + /* + * We should still be allowed to access the object at this point because + * the cache is SLAB_TYPESAFE_BY_RCU and we've been in an RCU read-side + * critical section since before the kmem_cache_free(). + */ + READ_ONCE(*p); + + rcu_read_unlock(); + + /* + * Wait for the RCU callback to execute; after this, the object should + * have actually been freed from KASAN's perspective. + */ + rcu_barrier(); + + KUNIT_EXPECT_KASAN_FAIL(test, READ_ONCE(*p)); + + kmem_cache_destroy(cache); +} + static void empty_cache_ctor(void *object) { } static void kmem_cache_double_destroy(struct kunit *test) { struct kmem_cache *cache; @@ -1934,12 +1979,13 @@ static struct kunit_case kasan_kunit_test_cases[] = { KUNIT_CASE(workqueue_uaf), KUNIT_CASE(kfree_via_page), KUNIT_CASE(kfree_via_phys), KUNIT_CASE(kmem_cache_oob), KUNIT_CASE(kmem_cache_double_free), KUNIT_CASE(kmem_cache_invalid_free), + KUNIT_CASE(kmem_cache_rcu_uaf), KUNIT_CASE(kmem_cache_double_destroy), KUNIT_CASE(kmem_cache_accounted), KUNIT_CASE(kmem_cache_bulk), KUNIT_CASE(mempool_kmalloc_oob_right), KUNIT_CASE(mempool_kmalloc_large_oob_right), KUNIT_CASE(mempool_slab_oob_right), diff --git a/mm/slab_common.c b/mm/slab_common.c index 40b582a014b8..df09066d56fe 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -539,12 +539,24 @@ static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work) kmem_cache_release(s); } } static int shutdown_cache(struct kmem_cache *s) { + if (IS_ENABLED(CONFIG_SLUB_RCU_DEBUG) && + (s->flags & SLAB_TYPESAFE_BY_RCU)) { + /* + * Under CONFIG_SLUB_RCU_DEBUG, when objects in a + * SLAB_TYPESAFE_BY_RCU slab are freed, SLUB will internally + * defer their freeing with call_rcu(). + * Wait for such call_rcu() invocations here before actually + * destroying the cache. + */ + rcu_barrier(); + } + /* free asan quarantined objects */ kasan_cache_shutdown(s); if (__kmem_cache_shutdown(s) != 0) return -EBUSY; diff --git a/mm/slub.c b/mm/slub.c index 0c98b6a2124f..a89f2006d46e 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -2197,45 +2197,81 @@ static inline bool memcg_slab_post_alloc_hook(struct kmem_cache *s, static inline void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p, int objects) { } #endif /* CONFIG_MEMCG */ +#ifdef CONFIG_SLUB_RCU_DEBUG +static void slab_free_after_rcu_debug(struct rcu_head *rcu_head); + +struct rcu_delayed_free { + struct rcu_head head; + void *object; +}; +#endif + /* * Hooks for other subsystems that check memory allocations. In a typical * production configuration these hooks all should produce no code at all. * * Returns true if freeing of the object can proceed, false if its reuse - * was delayed by KASAN quarantine, or it was returned to KFENCE. + * was delayed by CONFIG_SLUB_RCU_DEBUG or KASAN quarantine, or it was returned + * to KFENCE. */ static __always_inline -bool slab_free_hook(struct kmem_cache *s, void *x, bool init) +bool slab_free_hook(struct kmem_cache *s, void *x, bool init, + bool after_rcu_delay) { + /* Are the object contents still accessible? */ + bool still_accessible = (s->flags & SLAB_TYPESAFE_BY_RCU) && !after_rcu_delay; + kmemleak_free_recursive(x, s->flags); kmsan_slab_free(s, x); debug_check_no_locks_freed(x, s->object_size); if (!(s->flags & SLAB_DEBUG_OBJECTS)) debug_check_no_obj_freed(x, s->object_size); /* Use KCSAN to help debug racy use-after-free. */ - if (!(s->flags & SLAB_TYPESAFE_BY_RCU)) + if (!still_accessible) __kcsan_check_access(x, s->object_size, KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ASSERT); if (kfence_free(x)) return false; /* * Give KASAN a chance to notice an invalid free operation before we * modify the object. */ if (kasan_slab_pre_free(s, x)) return false; +#ifdef CONFIG_SLUB_RCU_DEBUG + if (still_accessible) { + struct rcu_delayed_free *delayed_free; + + delayed_free = kmalloc(sizeof(*delayed_free), GFP_NOWAIT); + if (delayed_free) { + /* + * Let KASAN track our call stack as a "related work + * creation", just like if the object had been freed + * normally via kfree_rcu(). + * We have to do this manually because the rcu_head is + * not located inside the object. + */ + kasan_record_aux_stack_noalloc(x); + + delayed_free->object = x; + call_rcu(&delayed_free->head, slab_free_after_rcu_debug); + return false; + } + } +#endif /* CONFIG_SLUB_RCU_DEBUG */ + /* * As memory initialization might be integrated into KASAN, * kasan_slab_free and initialization memset's must be * kept together to avoid discrepancies in behavior. * * The initialization memset's clear the object and the metadata, @@ -2253,42 +2289,42 @@ bool slab_free_hook(struct kmem_cache *s, void *x, bool init) memset(kasan_reset_tag(x), 0, s->object_size); rsize = (s->flags & SLAB_RED_ZONE) ? s->red_left_pad : 0; memset((char *)kasan_reset_tag(x) + inuse, 0, s->size - inuse - rsize); } /* KASAN might put x into memory quarantine, delaying its reuse. */ - return !kasan_slab_free(s, x, init); + return !kasan_slab_free(s, x, init, still_accessible); } static __fastpath_inline bool slab_free_freelist_hook(struct kmem_cache *s, void **head, void **tail, int *cnt) { void *object; void *next = *head; void *old_tail = *tail; bool init; if (is_kfence_address(next)) { - slab_free_hook(s, next, false); + slab_free_hook(s, next, false, false); return false; } /* Head and tail of the reconstructed freelist */ *head = NULL; *tail = NULL; init = slab_want_init_on_free(s); do { object = next; next = get_freepointer(s, object); /* If object's reuse doesn't have to be delayed */ - if (likely(slab_free_hook(s, object, init))) { + if (likely(slab_free_hook(s, object, init, false))) { /* Move object to the new freelist */ set_freepointer(s, object, *head); *head = object; if (!*tail) *tail = object; } else { @@ -4474,40 +4510,67 @@ static __fastpath_inline void slab_free(struct kmem_cache *s, struct slab *slab, void *object, unsigned long addr) { memcg_slab_free_hook(s, slab, &object, 1); alloc_tagging_slab_free_hook(s, slab, &object, 1); - if (likely(slab_free_hook(s, object, slab_want_init_on_free(s)))) + if (likely(slab_free_hook(s, object, slab_want_init_on_free(s), false))) do_slab_free(s, slab, object, object, 1, addr); } #ifdef CONFIG_MEMCG /* Do not inline the rare memcg charging failed path into the allocation path */ static noinline void memcg_alloc_abort_single(struct kmem_cache *s, void *object) { - if (likely(slab_free_hook(s, object, slab_want_init_on_free(s)))) + if (likely(slab_free_hook(s, object, slab_want_init_on_free(s), false))) do_slab_free(s, virt_to_slab(object), object, object, 1, _RET_IP_); } #endif static __fastpath_inline void slab_free_bulk(struct kmem_cache *s, struct slab *slab, void *head, void *tail, void **p, int cnt, unsigned long addr) { memcg_slab_free_hook(s, slab, p, cnt); alloc_tagging_slab_free_hook(s, slab, p, cnt); /* * With KASAN enabled slab_free_freelist_hook modifies the freelist * to remove objects, whose reuse must be delayed. */ if (likely(slab_free_freelist_hook(s, &head, &tail, &cnt))) do_slab_free(s, slab, head, tail, cnt, addr); } +#ifdef CONFIG_SLUB_RCU_DEBUG +static void slab_free_after_rcu_debug(struct rcu_head *rcu_head) +{ + struct rcu_delayed_free *delayed_free = + container_of(rcu_head, struct rcu_delayed_free, head); + void *object = delayed_free->object; + struct slab *slab = virt_to_slab(object); + struct kmem_cache *s; + + if (WARN_ON(is_kfence_address(object))) + return; + + /* find the object and the cache again */ + if (WARN_ON(!slab)) + return; + s = slab->slab_cache; + if (WARN_ON(!(s->flags & SLAB_TYPESAFE_BY_RCU))) + return; + + /* resume freeing */ + if (!slab_free_hook(s, object, slab_want_init_on_free(s), true)) + return; + do_slab_free(s, slab, object, object, 1, _THIS_IP_); + kfree(delayed_free); +} +#endif /* CONFIG_SLUB_RCU_DEBUG */ + #ifdef CONFIG_KASAN_GENERIC void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr) { do_slab_free(cache, virt_to_slab(x), x, x, 1, addr); } #endif